Skip to content

Commit 0cc04b5

Browse files
authored
[code-simplifier] Consolidate repo-root helpers and simplify skill script logic (#9772)
This change simplifies recently touched skill scripts by removing duplicated repo-root discovery code, promoting shared root lookup to a public API, and tightening small readability issues without changing behavior. It keeps script semantics intact while making cross-skill reuse explicit. - **Shared API cleanup (`.github/skills/shared/z3db.py`)** - Promote `_find_repo_root` to public `find_repo_root`. - Add `require_repo_root()` for scripts that should fail-fast when root discovery fails. - Update library usage docstring to expose both helpers. - Replace adjacent SQL string literals in `log()` with a single literal. - **Deduplicate memory-safety root lookup (`.github/skills/memory-safety/scripts/memory_safety.py`)** - Remove local `find_repo_root()` implementation. - Import and use shared `require_repo_root()` from `z3db`. - **Simplify static-analysis label selection (`.github/skills/static-analysis/scripts/static_analysis.py`)** - Replace two-step label assignment with a single expression: - `label = f["type"] or f["category"]` ```python # before label = f["category"] if f["type"]: label = f["type"] # after label = f["type"] or f["category"] ``` --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
1 parent c174df4 commit 0cc04b5

3 files changed

Lines changed: 17 additions & 22 deletions

File tree

.github/skills/memory-safety/scripts/memory_safety.py

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from pathlib import Path
1919

2020
sys.path.insert(0, str(Path(__file__).resolve().parent.parent.parent / "shared"))
21-
from z3db import Z3DB, setup_logging
21+
from z3db import Z3DB, require_repo_root, setup_logging
2222

2323
logger = logging.getLogger("z3agent")
2424

@@ -52,19 +52,6 @@ def check_dependencies():
5252
sys.exit(1)
5353

5454

55-
def find_repo_root() -> Path:
56-
d = Path.cwd()
57-
for _ in range(10):
58-
if (d / "CMakeLists.txt").exists() and (d / "src").is_dir():
59-
return d
60-
parent = d.parent
61-
if parent == d:
62-
break
63-
d = parent
64-
logger.error("could not locate Z3 repository root")
65-
sys.exit(1)
66-
67-
6855
def build_is_configured(build_dir: Path, sanitizer: str) -> bool:
6956
"""Check whether the build directory already has a matching cmake config."""
7057
cache = build_dir / "CMakeCache.txt"
@@ -220,7 +207,7 @@ def main():
220207

221208
setup_logging(args.debug)
222209
check_dependencies()
223-
repo_root = find_repo_root()
210+
repo_root = require_repo_root()
224211

225212
sanitizers = ["asan", "ubsan"] if args.sanitizer == "both" else [args.sanitizer]
226213
all_findings = []

.github/skills/shared/z3db.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
z3db: shared library and CLI for Z3 skill scripts.
44
55
Library usage:
6-
from z3db import Z3DB, find_z3, run_z3
6+
from z3db import Z3DB, find_z3, find_repo_root, require_repo_root, run_z3
77
88
CLI usage:
99
python z3db.py init
@@ -131,7 +131,7 @@ def log(self, message: str, level: str = "info", run_id: int = None):
131131
"""Write to stderr and to the interaction_log table."""
132132
getattr(logger, level, logger.info)(message)
133133
self.conn.execute(
134-
"INSERT INTO interaction_log (run_id, level, message) " "VALUES (?, ?, ?)",
134+
"INSERT INTO interaction_log (run_id, level, message) VALUES (?, ?, ?)",
135135
(run_id, level, message),
136136
)
137137
self.conn.commit()
@@ -182,7 +182,7 @@ def find_z3(hint: str = None) -> str:
182182
if hint:
183183
candidates.append(hint)
184184

185-
repo_root = _find_repo_root()
185+
repo_root = find_repo_root()
186186
if repo_root:
187187
for build_dir in ["build", "build/release", "build/debug"]:
188188
candidates.append(str(repo_root / build_dir / "z3"))
@@ -201,7 +201,8 @@ def find_z3(hint: str = None) -> str:
201201
sys.exit(1)
202202

203203

204-
def _find_repo_root() -> Optional[Path]:
204+
def find_repo_root() -> Optional[Path]:
205+
"""Best-effort search for the Z3 repository root from the current directory."""
205206
d = Path.cwd()
206207
for _ in range(10):
207208
if (d / "CMakeLists.txt").exists() and (d / "src").is_dir():
@@ -213,6 +214,15 @@ def _find_repo_root() -> Optional[Path]:
213214
return None
214215

215216

217+
def require_repo_root() -> Path:
218+
"""Return the Z3 repository root or exit the process if it is not found."""
219+
repo_root = find_repo_root()
220+
if repo_root is None:
221+
logger.error("could not locate Z3 repository root")
222+
sys.exit(1)
223+
return repo_root
224+
225+
216226
def run_z3(
217227
formula: str,
218228
z3_bin: str = None,

.github/skills/static-analysis/scripts/static_analysis.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,7 @@ def print_findings(findings: list):
176176
return
177177

178178
for f in findings:
179-
label = f["category"]
180-
if f["type"]:
181-
label = f["type"]
179+
label = f["type"] or f["category"]
182180
print(f"[{label}] {f['file']}:{f['line']}: {f['description']}")
183181

184182
print()

0 commit comments

Comments
 (0)