stage_probe.py¶
scripts/stage_probe.py
MEASURE ONE STAGE BY RE-RUNNING IT, not by diffing two builds.
_counts
dict[str, int] stage_probe._counts(Path db)
Count the rows of interest for a stage, before and after.
Source · 24 lines
def _counts(db: Path) -> dict[str, int]:
"""@brief Snapshot the edge counts a stage diff is read from.
@return Label to count.
@version 1
"""
conn = sqlite3.connect(db)
try:
rows = dict(
conn.execute(
"SELECT confidence, COUNT(*) FROM call_edges "
"WHERE source LIKE 'ast%' GROUP BY confidence"
)
)
extra = {}
for table in ("unresolved_inbound",):
try:
extra[table] = conn.execute(f"SELECT COUNT(*) FROM {table}").fetchone()[0] # noqa: S608
except sqlite3.OperationalError:
pass
finally:
conn.close()
return {**{f"ast/{k}": v for k, v in rows.items()}, **extra}
Activity · 10 steps
Flows
main
int stage_probe.main()
Copy an index, re-run one stage against the copy, and print the diff.
Source · 78 lines
def main() -> int:
"""@brief Re-run one stage in isolation and report what moved.
@return 0 on success, 2 on a bad argument.
@version 1
"""
parser = argparse.ArgumentParser(prog="stage_probe", description=__doc__)
parser.add_argument("--target", required=True, help="Repo root whose index to copy.")
parser.add_argument("--stage", default="ast_call_edges", choices=sorted(_STAGES))
parser.add_argument(
"--out",
default=".claude/tmp/stage_probe.db",
help="Where to write the working COPY. The source index is never modified.",
)
parser.add_argument(
"--allow-stale",
action="store_true",
help=(
"Measure even when the target's sources have moved since the index was built. "
"The diff is then not attributable to the stage — use it to explore, never to "
"quote a figure."
),
)
args = parser.parse_args()
target = Path(args.target).expanduser().resolve()
registry = TargetRegistry()
entry = registry.register(str(target))
source = Path(entry.db_path)
if not source.exists():
print(f"no index for {target} at {source} — build it first", file=sys.stderr)
return 2
## REFUSES ON A DRIFTED BASELINE, and that guard is the difference between a measurement
## and a number. Caught the first time this script was run: entropic's index read
## `resolved 4889` while a re-run produced 4839, because the working tree had moved since
## the index was built — so "before" described one source tree and "after" another, and
## the -50 was attributable to nothing. A diff whose two sides do not share a source tree
## is exactly the unreproducible instrument this approach was adopted to replace.
status = db_status(entry)
drifted = int(status.get("source_changed_files") or 0)
if drifted and not args.allow_stale:
print(
f"REFUSING: {drifted} indexed source file(s) have changed since this index was "
f"built, so a before/after diff would compare two different trees. Refresh the "
f"target first, or pass --allow-stale to measure anyway and read the numbers as "
f"indicative only.",
file=sys.stderr,
)
return 2
dest = Path(args.out)
dest.parent.mkdir(parents=True, exist_ok=True)
shutil.copy(source, dest)
## The harvest sidecar rides along, so the re-run reuses cached per-file parses and
## measures the RESOLUTION logic rather than the cost of parsing the tree again.
sidecar = source.with_suffix(source.suffix + ".idxcache")
if sidecar.exists():
shutil.copy(sidecar, dest.with_suffix(dest.suffix + ".idxcache"))
predicate, run = _STAGES[args.stage]
before = _counts(dest)
conn = sqlite3.connect(dest)
conn.execute(predicate)
conn.commit()
conn.close()
run(dest, target)
after = _counts(dest)
print(f"\n{args.stage} on {target.name}, re-run against a copy:\n")
for key in sorted(set(before) | set(after)):
was, now = before.get(key, 0), after.get(key, 0)
arrow = "" if was == now else f" ({now - was:+d})"
print(f" {key:<24} {was:>7} -> {now:<7}{arrow}")
print(f"\ncopy left at {dest}")
return 0