Files
Open_Ultra_SAST/src/openultrasast/hunt/runner.py
T

368 lines
15 KiB
Python

"""Run a hunt program over a frozen manifest and write records the benchmark scorer reads.
One record per change, in the shape `benchmarks/valid/hunter_arm.py` writes: the instrument proof (base and head
verified, bytes read per changed file), the defects under `push.admission.defects`, the status. Resumable by
change id, input digest, program id and mask. The pilot manifest is refused by digest unless the run is a pilot
report run, so the loop cannot tune on the held-out set by accident.
"""
from __future__ import annotations
import hashlib
import json
import subprocess
import sys
import tempfile
import time
from collections.abc import Iterable, Mapping, Sequence
from datetime import UTC, datetime
from pathlib import Path
from typing import Any
from .. import source
from ..model.endpoint import _PRICES
from ..plane.budget import cost_of
from ..preprocess import detect_language
from .artifact import RunArtifact, UsageLedger
from .labels import label_rows
from .producers import compute_rows
from .table import FeatureTable
PILOT_FREEZE_DIGEST = "c866c0e812c0b54476c7168fdb0e4a20652de0d30485270afecbe51e5c35e424"
class InstrumentFailure(RuntimeError):
"""A bounded, secret-free reason the record could not be measured."""
def _git(root: Path, *args: str) -> str:
done = subprocess.run(["git", "-C", str(root), "-c", "core.hooksPath=/dev/null", *args], capture_output=True, check=True)
return done.stdout.decode("utf-8", errors="replace")
def _write_json(path: Path, value: Any) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
tmp = path.with_suffix(path.suffix + ".partial")
tmp.write_text(json.dumps(value, indent=2, sort_keys=True) + "\n")
tmp.replace(path)
def record_path(out: Path, change_id: str) -> Path:
return Path(out) / (hashlib.sha256(change_id.encode("utf-8")).hexdigest() + ".json")
def input_digest(change: Mapping[str, Any], program_id: str, mask: Iterable[str]) -> str:
payload = json.dumps({"change": change, "program": program_id, "mask": sorted(set(mask))}, sort_keys=True)
return hashlib.sha256(payload.encode("utf-8")).hexdigest()
def refuse_pilot(manifest: Path, *, pilot_report: bool) -> None:
"""The held-out pilot is never a training or tuning input (requirement 2.4)."""
freeze = Path(manifest).with_name("freeze-" + Path(manifest).stem.removeprefix("pool-") + ".json")
if freeze.is_file():
digest = json.loads(freeze.read_text()).get("freeze_digest")
if digest == PILOT_FREEZE_DIGEST and not pilot_report:
raise ValueError("the pilot manifest is held out; pass pilot_report=True only for a report run")
def defect_from(finding: Mapping[str, Any], *, program_id: str, mask: tuple[str, ...]) -> dict[str, Any]:
"""The record's defect shape, with the program and mask that produced it."""
path, line = str(finding.get("path", "")), int(finding.get("line", 0) or 0)
return {
"defect_id": f"hunt:{program_id[:12]}:{path}:{line}",
"family": finding.get("family", "unknown"),
"cwe": None,
"function": finding.get("function"),
"locations": [[path, line]],
"source": "hunt",
"title": f"{finding.get('family', 'unknown')} at {path}:{line}",
"rationale": finding.get("rationale", ""),
"confidence": finding.get("confidence"),
"cited_lines": list(finding.get("cited_lines") or []),
"reported_family": finding.get("reported_family"),
"unknown_reason": finding.get("unknown_reason"),
"program_id": program_id,
"mask": list(mask),
}
def hunt_change(
change: Mapping[str, Any], repo_root: Path, *, program: Any, program_id: str, mask: tuple[str, ...], render: Any, tools_for: Any
) -> dict[str, Any]:
"""Check out the change's head, compute its feature rows, run the program once, return the record."""
instrument: dict[str, Any] = {
"hook_exit": 0,
"hook_bytes_read": 0,
"changed_bytes": {},
"head_verified": False,
"base_verified": False,
"failure": None,
}
record: dict[str, Any] = {
"id": change["id"],
"done": True,
"status": "hunt_covered",
"instrument": instrument,
"push": {"admission": {"defects": [], "dispositions": []}, "quick_tier": []},
"rows": 0,
"program_id": program_id,
"mask": list(mask),
"inputs_present": [],
"seconds": 0.0,
}
started = time.monotonic()
try:
_git(repo_root, "cat-file", "-e", change["base"] + "^{commit}")
instrument["base_verified"] = True
with tempfile.TemporaryDirectory(prefix="ousast-hunt-") as temporary:
root = Path(temporary) / "head"
added = False
try:
_git(repo_root, "worktree", "add", "--detach", str(root), change["head"])
added = True
_full_checkout(root)
if _git(root, "rev-parse", "HEAD").strip() != change["head"]:
raise InstrumentFailure("head_mismatch")
instrument["head_verified"] = True
texts: dict[str, list[str]] = {}
for relative in dict.fromkeys(file["path"] for file in change.get("files", [])):
target = root / relative
if not target.is_file():
continue
data = target.read_bytes()
print(f"{change['id']} {relative}: {len(data)} bytes read", flush=True)
instrument["changed_bytes"][relative] = len(data)
instrument["hook_bytes_read"] += len(data)
texts[relative] = data.decode("utf-8", errors="replace").splitlines()
if not instrument["changed_bytes"]:
raise InstrumentFailure("no_readable_hotspots")
rows = label_rows(compute_rows(root, change), change)
FeatureTable(rows) # validates the schema before anything is rendered
record["rows"] = len(rows)
inputs = render(change, rows, texts, mask=mask)
record["inputs_present"] = sorted(inputs)
if tools_for is not None and hasattr(program, "bind"):
program.bind(root)
prediction = program(**inputs)
record["parse_failed"] = bool(getattr(prediction, "parse_failed", False))
record["loop_parse_failed"] = bool(getattr(prediction, "loop_parse_failed", False))
for finding in prediction.findings:
if finding.get("path") in texts and not finding.get("function") and (span := _function_at(root, finding)):
finding["function"] = span
record["push"]["admission"]["defects"].append(defect_from(finding, program_id=program_id, mask=mask))
finally:
if added:
_git(repo_root, "worktree", "remove", "--force", str(root))
except Exception as error: # noqa: BLE001 (provider messages can carry a key: type only)
reason = str(error) if isinstance(error, InstrumentFailure) else type(error).__name__
instrument.update(hook_exit=2, failure=reason)
record["status"] = "instrument_failure"
record["push"]["admission"]["defects"] = []
print(f"{change['id']}: instrument_failure ({reason})", file=sys.stderr, flush=True)
record["seconds"] = round(time.monotonic() - started, 3)
return record
def spent_since(history: list[Mapping[str, Any]] | None, start: int) -> float:
"""Dollars of the LM calls appended to a DSPy LM history since `start`, at the registered prices.
Before this the runner booked 0.0 per change and `--budget-usd` never stopped anything: the first full
access-control run was flagged at 5 dollars and cost 6.13."""
total = 0.0
for entry in (history or [])[start:]:
usage = entry.get("usage") or {}
prices = _PRICES.get(str(entry.get("model") or "").split("/")[-1])
if not prices or not isinstance(usage, Mapping):
continue
merged = dict(usage)
details = usage.get("prompt_tokens_details")
if "prompt_cache_hit_tokens" not in merged and isinstance(details, Mapping) and details.get("cached_tokens") is not None:
merged["prompt_cache_hit_tokens"] = details["cached_tokens"]
total += cost_of(merged, prices)
return total
def _lm_history() -> list[Mapping[str, Any]] | None:
try:
import dspy
except ImportError: # pragma: no cover
return None
return getattr(getattr(dspy.settings, "lm", None), "history", None)
def _full_checkout(root: Path) -> None:
"""Undo a sparse-checkout a worktree inherits from a clone whose working tree was emptied to save disk.
Without this every file read fails and the producers report an unread tree, which must never pass as a
clean result."""
try:
sparse = _git(root, "config", "--get", "core.sparseCheckout").strip()
except subprocess.CalledProcessError:
sparse = "false"
if sparse == "true":
_git(root, "sparse-checkout", "disable")
# The proof: a fresh worktree has nothing modified. Truncated files (seen once while two processes checked out
# from the same clone) show up here as modified; restore them once, then refuse rather than read an unread tree.
if _git(root, "status", "--porcelain", "--untracked-files=no").strip():
_git(root, "checkout", "--", ".")
if _git(root, "status", "--porcelain", "--untracked-files=no").strip():
raise InstrumentFailure("checkout_incomplete")
def _function_at(root: Path, finding: Mapping[str, Any]) -> str | None:
try:
path = str(finding["path"])
text = (root / path).read_text(errors="replace")
language = source.canonical_language(detect_language(root / path))
return source.enclosing(source.ranges(path, text, language), int(finding.get("line", 0) or 0))
except Exception: # noqa: BLE001
return None
def _run_entries(out: Path) -> tuple[Path, list[dict[str, Any]], dict[str, dict[str, Any]]]:
inputs_path = out / "inputs.json"
entries = json.loads(inputs_path.read_text()) if inputs_path.is_file() else []
return inputs_path, entries, {entry["item_id"]: entry for entry in entries}
def _change_entry(
change: Mapping[str, Any],
program_id: str,
mask: tuple[str, ...],
entries: list[dict[str, Any]],
indexed: dict[str, dict[str, Any]],
) -> str:
digest = input_digest(change, program_id, mask)
entry = indexed.get(change["id"])
if entry is None:
entry = {"item_id": change["id"], "change": change, "input_digest": digest}
entries.append(entry)
indexed[change["id"]] = entry
elif entry["input_digest"] != digest:
entry.update(change=change, input_digest=digest)
return digest
def _record_is_complete(out: Path, change_id: str, digest: str) -> bool:
path = record_path(out, change_id)
if not path.is_file():
return False
previous = json.loads(path.read_text())
return bool(previous.get("done") and previous.get("input_digest") == digest)
def _run_change(
change: Mapping[str, Any],
repos: Mapping[str, str],
out: Path,
program: Any,
program_id: str,
mask: tuple[str, ...],
render: Any,
tools_for: Any,
ledger: UsageLedger,
) -> None:
history = _lm_history()
first_call = len(history) if history is not None else 0
record = hunt_change(
change,
Path(repos[change["repo"]]).expanduser(),
program=program,
program_id=program_id,
mask=mask,
render=render,
tools_for=tools_for,
)
record["input_digest"] = input_digest(change, program_id, mask)
record["usd"] = round(spent_since(history, first_call), 6)
ledger.add({"calls": 1, "usd": record["usd"]})
_write_json(record_path(out, change["id"]), record)
def _process_changes(
changes: Sequence[Mapping[str, Any]],
repos: Mapping[str, str],
out: Path,
program: Any,
program_id: str,
mask: tuple[str, ...],
render: Any,
tools_for: Any,
budget: Mapping[str, float] | None,
entries: list[dict[str, Any]],
indexed: dict[str, dict[str, Any]],
inputs_path: Path,
) -> UsageLedger:
ledger = UsageLedger()
for change in changes:
digest = _change_entry(change, program_id, mask, entries, indexed)
if _record_is_complete(out, change["id"], digest):
continue
if budget and ledger.exhausted(budget):
print("budget exhausted; stopping before the next change", flush=True)
break
_run_change(change, repos, out, program, program_id, mask, render, tools_for, ledger)
_write_json(inputs_path, entries)
_write_json(inputs_path, entries)
return ledger
def _run_artifact(
manifest: Path,
program_id: str,
mask: tuple[str, ...],
only: str,
slice_id: str,
changes: Sequence[Mapping[str, Any]],
ledger: UsageLedger,
versions: Mapping[str, str] | None,
started: float,
) -> RunArtifact:
usage = dict(ledger.usage)
usage["wall_seconds"] = round(time.monotonic() - started, 1)
return RunArtifact(
program_id=program_id,
schema_version=1,
mask=list(mask),
data_digests={"manifest": hashlib.sha256(manifest.read_bytes()).hexdigest()},
configurations=[{"only": only, "slice": slice_id, "changes": len(changes)}],
usage=usage,
versions=dict(versions or {}),
created=datetime.now(UTC).isoformat(timespec="seconds"),
)
def run(
manifest: Path,
repos: Mapping[str, str],
out: Path,
*,
program: Any,
program_id: str,
mask: Iterable[str] = (),
only: str = "all",
slice_id: str = "1",
budget: Mapping[str, float] | None = None,
pilot_report: bool = False,
render: Any = None,
tools_for: Any = None,
load_changes: Any = None,
versions: Mapping[str, str] | None = None,
) -> RunArtifact | None:
"""Run the program over every change of the manifest's slice; resumable; one artifact at the end."""
manifest, out = Path(manifest), Path(out)
refuse_pilot(manifest, pilot_report=pilot_report)
mask = tuple(sorted(set(mask)))
if render is None:
from .program import render_rows as render # noqa: PLC0415 (dspy import stays optional)
changes = load_changes(manifest, slice_id)
changes = [change for change in changes if only == "all" or change["kind"] == only]
out.mkdir(parents=True, exist_ok=True)
inputs_path, entries, indexed = _run_entries(out)
started = time.monotonic()
ledger = _process_changes(changes, repos, out, program, program_id, mask, render, tools_for, budget, entries, indexed, inputs_path)
artifact = _run_artifact(manifest, program_id, mask, only, slice_id, changes, ledger, versions, started)
completed: RunArtifact | None = artifact.finish(budget) if budget else artifact
artifact.write(out, reserve_attempt=True)
return completed