Skip to content

abc_mine.stages.screen

WARNING

This documentation is almost entirely "vibe coded"; while a helpful resource for humans and agents to understand this codebase, it should not be considered a source of ground truth in the way documentation normally is, and the user is responsible for verifying that all instructions given here are safe.

abc_mine.stages.screen

SCREEN — score every selected commit against the rubric, in parallel.

One fresh agent per commit. Each reads the commit's diff, message, and surrounding source, then emits per-axis scores plus prose. The total score and the disqualification decision are computed here in Python, never by the model, so scoring stays deterministic and a run can be re-weighted afterwards without re-invoking a single agent.

Durable artifact and resume

The stage's source of truth is one file per commit: <run>/screen/summaries/<short-sha>.json. Resume is simply "skip commits that already have one". That choice has two properties worth naming:

  • It is backend-independent — a run started serially can be resumed under MPI, because progress lives in files a human can read rather than in a pool's internal bookkeeping.
  • It is all-or-nothing per commit. A commit either has a complete summary or has nothing. There is no partial state to reason about, and no way for a half-finished judgement to leak into ranking.

Failures are recorded separately, in an append-only attempts.jsonl. This closes a real gap in the predecessor pipeline, where a failed screen wrote nothing at all: "never attempted" and "failed six times" were indistinguishable, retries were unbounded across resumes, and the transcripts had already been deleted. Here failures are counted (so max_attempts can be enforced) and, under the default retention policy, their transcripts are kept.

ScreenPlan dataclass

What a screen invocation intends to do.

Separating the plan from execution lets the CLI show a meaningful --plan without starting any container, and lets every MPI rank compute an identical work list from read-only state.

Source code in abc_mine/stages/screen.py
@dataclass
class ScreenPlan:
    """What a screen invocation intends to do.

    Separating the plan from execution lets the CLI show a meaningful ``--plan``
    without starting any container, and lets every MPI rank compute an identical
    work list from read-only state.
    """

    selected: list[repo_mod.Commit]
    pending: list[repo_mod.Commit]
    already_done: int
    exhausted: dict[str, int]
    report: repo_mod.SelectionReport

    def describe(self, max_attempts: int) -> str:
        """Render a human-readable plan."""
        lines = [self.report.describe()]
        if self.already_done:
            lines.append(f"  {self.already_done} already screened (skipping)")
        if self.exhausted:
            lines.append(
                f"  {len(self.exhausted)} skipped: failed {max_attempts} time(s) "
                f"already (raise execution.max_attempts to retry)"
            )
        lines.append(f"  {len(self.pending)} to screen now")
        return "\n".join(lines)

describe

describe(max_attempts: int) -> str

Render a human-readable plan.

Source code in abc_mine/stages/screen.py
def describe(self, max_attempts: int) -> str:
    """Render a human-readable plan."""
    lines = [self.report.describe()]
    if self.already_done:
        lines.append(f"  {self.already_done} already screened (skipping)")
    if self.exhausted:
        lines.append(
            f"  {len(self.exhausted)} skipped: failed {max_attempts} time(s) "
            f"already (raise execution.max_attempts to retry)"
        )
    lines.append(f"  {len(self.pending)} to screen now")
    return "\n".join(lines)

summaries_dir

summaries_dir(cfg: MineConfig) -> Path

Return (and create) the per-run summaries directory.

Source code in abc_mine/stages/screen.py
def summaries_dir(cfg: MineConfig) -> Path:
    """Return (and create) the per-run summaries directory."""
    path = cfg.summaries_dir
    path.mkdir(parents=True, exist_ok=True)
    return path

summary_path

summary_path(cfg: MineConfig, sha: str) -> Path

Return the durable summary path for sha.

Source code in abc_mine/stages/screen.py
def summary_path(cfg: MineConfig, sha: str) -> Path:
    """Return the durable summary path for ``sha``."""
    return summaries_dir(cfg) / f"{sha[:repo_mod.SHORT_SHA_LEN]}.json"

attempts_path

attempts_path(cfg: MineConfig) -> Path

Return the screen stage's attempt log path.

Source code in abc_mine/stages/screen.py
def attempts_path(cfg: MineConfig) -> Path:
    """Return the screen stage's attempt log path."""
    return cfg.screen_dir / ATTEMPTS_NAME

completed

completed(cfg: MineConfig) -> set[str]

Return short SHAs that already have a durable summary.

Source code in abc_mine/stages/screen.py
def completed(cfg: MineConfig) -> set[str]:
    """Return short SHAs that already have a durable summary."""
    return {p.stem for p in summaries_dir(cfg).glob("*.json")}

load_summaries

load_summaries(cfg: MineConfig) -> list[dict[str, Any]]

Load every summary written for this run, sorted by score descending.

Unreadable files are skipped rather than fatal: one corrupt summary (a job killed mid-write, say) should not block ranking over hundreds of good ones.

Source code in abc_mine/stages/screen.py
def load_summaries(cfg: MineConfig) -> list[dict[str, Any]]:
    """Load every summary written for this run, sorted by score descending.

    Unreadable files are skipped rather than fatal: one corrupt summary (a job
    killed mid-write, say) should not block ranking over hundreds of good ones.
    """
    out: list[dict[str, Any]] = []
    for path in sorted(summaries_dir(cfg).glob("*.json")):
        try:
            out.append(json.loads(path.read_text()))
        except (json.JSONDecodeError, OSError):
            continue
    out.sort(key=lambda s: s.get("benchmark_score") or 0, reverse=True)
    return out

plan

plan(cfg: MineConfig, *, only: Sequence[str] = ()) -> ScreenPlan

Compute the work list. Read-only, and identical on every rank.

Parameters:

Name Type Description Default
only sequence of str

Restrict to these SHAs (prefix-matched), for targeted debugging.

()
Source code in abc_mine/stages/screen.py
def plan(cfg: MineConfig, *, only: Sequence[str] = ()) -> ScreenPlan:
    """Compute the work list. Read-only, and identical on every rank.

    Parameters
    ----------
    only : sequence of str, optional
        Restrict to these SHAs (prefix-matched), for targeted debugging.
    """
    commits = repo_mod.enumerate_commits(cfg.codebase.repo, paths=cfg.selection.paths)
    commits = repo_mod.annotate(cfg.codebase.repo, commits)

    report = repo_mod.SelectionReport()
    selected = repo_mod.select(commits, cfg.selection, report=report)

    if only:
        wanted = tuple(only)
        selected = [c for c in selected if any(c.sha.startswith(w) for w in wanted)]
        report.selected = len(selected)

    done = completed(cfg)
    spent = exhausted_items(attempts_path(cfg), cfg.execution.max_attempts)

    pending = [
        c for c in selected if c.short not in done and c.short not in spent
    ]
    return ScreenPlan(
        selected=selected,
        pending=pending,
        already_done=sum(1 for c in selected if c.short in done),
        exhausted={c.short: spent[c.short] for c in selected if c.short in spent},
        report=report,
    )

make_worker

make_worker(cfg: MineConfig, pack: PromptPack, model: dict[str, str], *, dry_run: bool = False) -> Callable[[Commit], ItemResult]

Build the per-commit worker callable.

Returned as a closure so the same function serves all three parallel backends and can be exercised directly in a test without any pool.

Source code in abc_mine/stages/screen.py
def make_worker(
    cfg: MineConfig,
    pack: PromptPack,
    model: dict[str, str],
    *,
    dry_run: bool = False,
) -> Callable[[repo_mod.Commit], parallel.ItemResult]:
    """Build the per-commit worker callable.

    Returned as a closure so the same function serves all three parallel backends
    and can be exercised directly in a test without any pool.
    """
    automaton_cfg = sandbox_mod.build_automaton_config(model)
    agent = sandbox_mod.make_agent(automaton_cfg)
    harness = sandbox_mod.make_harness()
    log_root = cfg.screen_dir / "logs"

    def worker(commit: repo_mod.Commit) -> parallel.ItemResult:
        """Screen one commit end to end."""
        attempts = attempts_path(cfg)
        sandbox = sandbox_mod.make_sandbox(cfg, dry_run=dry_run)

        with sandbox_mod.scratch_dir(cfg, f"lbm-screen-{commit.short}-") as scratch:
            work = scratch / "work"
            run_dir = scratch / "run"
            work.mkdir(parents=True, exist_ok=True)

            # Materialize the source tree inside the single mounted directory, at
            # the basename of the configured mount point, so one bind presents it
            # exactly where the prompt says it is.
            source_name = Path(cfg.codebase.source_mount).name
            with repo_mod.worktree(cfg.codebase.repo, commit.sha, work / source_name):
                diff, truncated = repo_mod.commit_diff(
                    cfg.codebase.repo, commit.sha, max_bytes=MAX_DIFF_BYTES
                )
                touched = repo_mod.touched_files(cfg.codebase.repo, commit.sha)

                (work / "commit.diff").write_text(diff)
                (work / "commit_message.txt").write_text(
                    repo_mod.commit_message(cfg.codebase.repo, commit.sha) + "\n"
                )
                (work / "touched_files.txt").write_text("\n".join(touched) + "\n")

                prompt = _compose_prompt(
                    cfg, pack, commit, truncated=truncated, touched=touched
                )

                agent_started = time.monotonic()
                run = sandbox_mod.run_agent(
                    cfg,
                    sandbox=sandbox,
                    harness=harness,
                    agent=agent,
                    work_dir=work,
                    run_dir=run_dir,
                    prompt_text=prompt,
                )
                agent_seconds = time.monotonic() - agent_started

            if run.dry_run:
                return parallel.ItemResult(item_id=commit.short, status="dry_run")

            # Kept for every item, success or not: spend is spend.
            usage, _ = telemetry_mod.record_run(
                cfg.screen_dir, commit.short, run.run_dir,
                keep=cfg.execution.keep_telemetry,
                pricing=model.get("pricing"),
                agent_seconds=agent_seconds,
                label_prefix="attempt",
                meta={"stage": "screen", "item": commit.short, "sha": commit.sha,
                      "ok": run.ok, "model_id": model.get("model", "")},
            )

            verdict_file = run.out_path(VERDICT_FILENAME)
            failure: str | None = None

            if not run.ok:
                failure = run.detail or "agent execution failed"
            elif not verdict_file.is_file():
                failure = (
                    f"agent finished successfully but wrote no verdict to "
                    f"{VERDICT_FILENAME}"
                )
            else:
                try:
                    raw = json.loads(verdict_file.read_text())
                except json.JSONDecodeError as exc:
                    failure = f"verdict is not valid JSON: {exc}"
                else:
                    if not isinstance(raw, dict):
                        failure = (
                            f"verdict must be a JSON object, got "
                            f"{type(raw).__name__}"
                        )

            if failure is not None:
                sandbox_mod.preserve_logs(
                    run.run_dir,
                    log_root / commit.short,
                    keep_logs=cfg.execution.keep_logs,
                    failed=True,
                )
                record_attempt(
                    attempts,
                    commit.short,
                    "failed",
                    detail=failure,
                    model_id=model.get("model", ""),
                    sha=commit.sha,
                    usage=telemetry_mod.compact(usage),
                )
                return parallel.ItemResult(
                    item_id=commit.short, status="failed", detail=failure
                )

            summary = _finalize(cfg, commit, raw, run, usage)

            # Write the durable artifact atomically. Resume keys on this file's
            # existence, so a torn write would be indistinguishable from a
            # completed screen — hence temp-then-rename rather than a direct write.
            dest = summary_path(cfg, commit.sha)
            tmp = dest.with_suffix(".json.tmp")
            tmp.write_text(json.dumps(summary, indent=2, sort_keys=True) + "\n")
            tmp.replace(dest)

            sandbox_mod.preserve_logs(
                run.run_dir,
                log_root / commit.short,
                keep_logs=cfg.execution.keep_logs,
                failed=False,
            )
            record_attempt(
                attempts,
                commit.short,
                "ok",
                model_id=model.get("model", ""),
                sha=commit.sha,
                benchmark_score=summary["benchmark_score"],
                disqualified=summary["disqualified"],
            )

            return parallel.ItemResult(
                item_id=commit.short,
                status="ok",
                data={
                    "sha": commit.sha,
                    "benchmark_score": summary["benchmark_score"],
                    "disqualified": summary["disqualified"],
                },
            )

    return worker

run

run(cfg: MineConfig, pack: PromptPack, model: dict[str, str], *, dry_run: bool = False, only: Sequence[str] = (), progress: bool = True) -> BatchOutcome

Execute the screen stage.

Returns the batch outcome; on MPI worker ranks this is empty and is_primary is False, so callers must not report from it.

Source code in abc_mine/stages/screen.py
def run(
    cfg: MineConfig,
    pack: PromptPack,
    model: dict[str, str],
    *,
    dry_run: bool = False,
    only: Sequence[str] = (),
    progress: bool = True,
) -> parallel.BatchOutcome:
    """Execute the screen stage.

    Returns the batch outcome; on MPI worker ranks this is empty and
    ``is_primary`` is False, so callers must not report from it.
    """
    work_plan = plan(cfg, only=only)

    if progress:
        print(work_plan.describe(cfg.execution.max_attempts))
        print()

    if not work_plan.pending:
        return parallel.BatchOutcome(results=[], is_primary=True)

    worker = make_worker(cfg, pack, model, dry_run=dry_run)

    def on_result(result: parallel.ItemResult, index: int, total: int) -> None:
        """Report one completed item."""
        if not progress:
            return
        if result.status == "ok":
            data = result.data
            flag = " DISQUALIFIED" if data.get("disqualified") else ""
            tail = f"score {data.get('benchmark_score', 0):>3}{flag}"
        elif result.status == "dry_run":
            tail = "dry run"
        else:
            tail = f"FAILED: {result.detail.splitlines()[0][:70] if result.detail else ''}"
        print(f"  [{index:>4}/{total}] {result.item_id}  {tail}", flush=True)

    return parallel.run_batch(
        work_plan.pending,
        worker,
        backend=cfg.execution.parallel,
        workers=cfg.execution.workers,
        scratch_dir=cfg.execution.scratch_dir,
        run_id=cfg.run_id,
        on_result=on_result,
    )

summarize

summarize(cfg: MineConfig) -> dict[str, Any]

Return aggregate statistics over this run's summaries.

Reads from disk rather than from a batch outcome so it reflects the whole run, including items screened in earlier allocations.

Source code in abc_mine/stages/screen.py
def summarize(cfg: MineConfig) -> dict[str, Any]:
    """Return aggregate statistics over this run's summaries.

    Reads from disk rather than from a batch outcome so it reflects the whole run,
    including items screened in earlier allocations.
    """
    summaries = load_summaries(cfg)
    if not summaries:
        return {"screened": 0, "viable": 0, "disqualified": 0}

    viable = [s for s in summaries if not s.get("disqualified")]
    scores = [s.get("benchmark_score", 0) for s in viable]
    by_type: dict[str, int] = {}
    for s in viable:
        for tag in s.get("task_type", []) or ["(untagged)"]:
            by_type[tag] = by_type.get(tag, 0) + 1

    # Every attempt's spend (failures and retries included) where telemetry
    # was kept; the summaries' own usage otherwise.
    spend = telemetry_mod.stage_usage(
        cfg.screen_dir,
        {str(s.get("sha", ""))[: repo_mod.SHORT_SHA_LEN]: s.get("usage") for s in summaries},
    )
    usage = spend["usage"]

    return {
        "screened": len(summaries),
        "viable": len(viable),
        "disqualified": len(summaries) - len(viable),
        "score_max": max(scores) if scores else 0,
        "score_mean": round(sum(scores) / len(scores), 1) if scores else 0.0,
        "by_task_type": dict(sorted(by_type.items(), key=lambda kv: -kv[1])),
        "usage": usage,
        "usage_source": spend["source"],
    }