feat(contributor-scan): a scan writes its run to the user cache, and the ledger only when it learns (3.1, part A)

Where a scan writes. A native scan now creates its run under the user cache,
~/.cache/openultrasast/runs/<scan-id>/ by default ($XDG_CACHE_HOME when it is
set and absolute), prints run_dir=<path> once, and no longer writes anything
under the analysed tree. OPENULTRASAST_RUNS_DIR still wins: an absolute value
is used as given, a relative one resolves against the working directory.
Pre-push storage is unchanged.

The calibration ledger, by maintainer decision: .openultrasast/calibration/
stays the store, and a scan writes the false-positive ledger into the tree
only when its content would change. On a read-only tree the scan records
ledger_not_written, with a sentence in the report, and completes; before, a
read-only target with one rejected finding crashed before report.md existed.

benchmarks/repos/measure.py follows the printed run directory. It read the
run from under the scanned copy, which would have recorded every repository
as failed, and it also read a stale run left in a copy.

Tests: tests/test_scan_output_location.py drives the real CLI and compares
the whole tree before and after; tests/test_repo_measure.py pins the
measurement script; an autouse fixture keeps every test's scan runs out of
the developer's cache; fourteen test files follow the run to its new place.
README, docs/examples.md and the scan skill name the ledger as the one write
a scan makes. _run_scan is touched by one line and stays over the complexity
cap.

Task 3.1 stays open for part B, the readable summary.
This commit is contained in:
Marius Ciepluch
2026-11-24 15:33:35 +01:00
parent d45fc98c4e
commit 6edae3a585
29 changed files with 509 additions and 50 deletions
+11 -3
View File
@@ -89,6 +89,14 @@ def main(argv: list[str] | None = None) -> int:
return 0
def _printed_run_dir(stdout: str) -> Path | None:
"""The `run_dir=<path>` line the scan prints: where the run went, wherever the default or the override puts it."""
for line in stdout.splitlines():
if line.startswith("run_dir="):
return Path(line.removeprefix("run_dir="))
return None
def _copy(root: Path, work: Path) -> None:
if work.exists():
shutil.rmtree(work)
@@ -107,8 +115,8 @@ def _scan(work: Path, mode: str, config: Path | None = None) -> dict[str, object
wall = round(time.monotonic() - started, 2)
peak_kb = max(resource.getrusage(resource.RUSAGE_CHILDREN).ru_maxrss, before)
runs = sorted((work / ".openultrasast" / "runs").glob("*/"))
manifest = runs[-1] / "manifest.json" if runs else None
run_dir = _printed_run_dir(proc.stdout)
manifest = run_dir / "manifest.json" if run_dir is not None else None
if proc.returncode not in (0, 1) or manifest is None or not manifest.is_file():
# Req 4.3: a repository that cannot be processed records the stage it failed at, not an opaque error.
return {
@@ -123,7 +131,7 @@ def _scan(work: Path, mode: str, config: Path | None = None) -> dict[str, object
data = json.loads(manifest.read_text())
model = dict(data.get("model", {}))
findings = _findings(runs[-1])
findings = _findings(run_dir)
return {
"completed": True,
"exit_code": proc.returncode,