Skip to content

abc_mine.stages.rank

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.rank

RANK — collapse viable screening summaries into a ranked shortlist.

One decision-making agent reads every viable candidate in a single context and selects the commits genuinely worth building into benchmarks. It may dispatch research subagents to settle a specific uncertainty about a promising candidate, but the selection itself is one judgement made with all candidates visible — which is precisely the comparison the per-commit screening pass cannot make.

The only filtering done here in Python is dropping commits screening marked disqualified (they failed a rubric gate, and are unusable by construction). No score threshold is applied: the scores travel along as informational context, and the agent is explicitly told it may disagree with them.

.. _rank-limitations:

Known limitations — deliberate, not oversights

Single-context only. This stage assumes every candidate fits in one model context. There is no chunk-and-merge, no multi-round tournament, no hierarchical reduction. That is a conscious scoping decision: those designs introduce real complexity (how to compare candidates that never shared a context, how to merge partial orderings fairly, how to keep a merge agent from simply deferring to whichever chunk it saw last) and are not attempted here.

The assumption is therefore checked rather than hoped for. The candidates are embedded in the prompt itself, not left in a file for the agent to page through with a read tool (which adds a per-line overhead and a tool round-trip per 50 KB, and invites skimming). Before any agent runs, the fully composed prompt — the harness system prompt plus this stage's task prompt — is counted through the endpoint's own token counter and compared against rank.max_prompt_tokens; exceeding it is a hard, actionable error. There is no estimate to fall back on: if the count cannot be obtained, the stage refuses to start. (The count is only as accurate as the endpoint's counter; a gateway whose counter disagrees with its models' real tokenizer makes the budget a bound in its units.) Silently overflowing a context and receiving a shortlist quietly drawn from the first 60% of candidates would be far worse than refusing to start, because nothing in the output would reveal it had happened.

No mid-stage resume. One agent doing one long task either finishes or does not. If it dies forty minutes in, rerunning starts over. Per-item checkpointing has no meaning when there is one item. Bounding the cost of that is what execution.timeout_seconds is for. A partial-progress design is not possible without the multi-agent machinery described above.

Both limitations are recorded in the run manifest when this stage runs, so a shortlist is never mistaken for the product of a more sophisticated process than actually produced it.

RankError

Bases: RuntimeError

Raised when ranking cannot proceed (no candidates, budget exceeded, ...).

Source code in abc_mine/stages/rank.py
class RankError(RuntimeError):
    """Raised when ranking cannot proceed (no candidates, budget exceeded, ...)."""

TokenCountError

Bases: RankError

The prompt's token count could not be obtained. Never estimated instead.

Source code in abc_mine/stages/rank.py
class TokenCountError(RankError):
    """The prompt's token count could not be obtained. Never estimated instead."""

RankPlan dataclass

What a ranking invocation intends to do.

Source code in abc_mine/stages/rank.py
@dataclass
class RankPlan:
    """What a ranking invocation intends to do."""

    candidates: list[dict[str, Any]] = field(default_factory=list)
    n_screened: int = 0
    n_disqualified: int = 0
    n_dropped_by_cap: int = 0
    #: The fully composed task prompt, candidates embedded.
    prompt: str = ""
    #: Endpoint token count of the harness system prompt plus :attr:`prompt`,
    #: or ``None`` when no budget is configured (and so nothing was counted).
    prompt_tokens: int | None = None
    budget: int | None = None
    #: Group label under ``interleaving`` (e.g. ``"group 1 of 2"``); empty for a
    #: single-context plan.
    label: str = ""

    @property
    def over_budget(self) -> bool:
        """True if the measured prompt exceeds the configured token budget."""
        return (
            self.budget is not None
            and self.prompt_tokens is not None
            and self.prompt_tokens > self.budget
        )

    def describe(self) -> str:
        """Render a human-readable plan."""
        lines = [
            f"Screened commits:        {self.n_screened}",
            f"  disqualified:          {self.n_disqualified}",
            f"  viable candidates:     {len(self.candidates)}",
        ]
        if self.n_dropped_by_cap:
            lines.append(
                f"  dropped by max_candidates: {self.n_dropped_by_cap} "
                f"(lowest-scoring)"
            )
        if self.prompt_tokens is None:
            lines.append(
                f"Prompt:                  {len(self.prompt):,} chars "
                "(not counted: rank.max_prompt_tokens is unset)"
            )
        else:
            budget = f" of {self.budget:,} budget" if self.budget else ""
            headroom = (
                f"; {self.budget - self.prompt_tokens:,} left for the agent's work"
                if self.budget and not self.over_budget else ""
            )
            lines.append(
                f"Prompt:                  {self.prompt_tokens:,} tokens "
                f"(endpoint count){budget}{headroom}"
            )
        return "\n".join(lines)

over_budget property

over_budget: bool

True if the measured prompt exceeds the configured token budget.

describe

describe() -> str

Render a human-readable plan.

Source code in abc_mine/stages/rank.py
def describe(self) -> str:
    """Render a human-readable plan."""
    lines = [
        f"Screened commits:        {self.n_screened}",
        f"  disqualified:          {self.n_disqualified}",
        f"  viable candidates:     {len(self.candidates)}",
    ]
    if self.n_dropped_by_cap:
        lines.append(
            f"  dropped by max_candidates: {self.n_dropped_by_cap} "
            f"(lowest-scoring)"
        )
    if self.prompt_tokens is None:
        lines.append(
            f"Prompt:                  {len(self.prompt):,} chars "
            "(not counted: rank.max_prompt_tokens is unset)"
        )
    else:
        budget = f" of {self.budget:,} budget" if self.budget else ""
        headroom = (
            f"; {self.budget - self.prompt_tokens:,} left for the agent's work"
            if self.budget and not self.over_budget else ""
        )
        lines.append(
            f"Prompt:                  {self.prompt_tokens:,} tokens "
            f"(endpoint count){budget}{headroom}"
        )
    return "\n".join(lines)

CandidatePool dataclass

The viable candidates, after disqualification and the max_candidates cap.

Source code in abc_mine/stages/rank.py
@dataclass
class CandidatePool:
    """The viable candidates, after disqualification and the ``max_candidates`` cap."""

    candidates: list[dict[str, Any]]
    n_screened: int
    n_disqualified: int
    n_dropped_by_cap: int

GroupResult dataclass

A validated shortlist from one ranking agent, plus its provenance.

Source code in abc_mine/stages/rank.py
@dataclass
class GroupResult:
    """A validated shortlist from one ranking agent, plus its provenance."""

    shortlist: dict[str, Any]
    rationale: str
    usage: dict[str, Any]
    seconds: float

shortlist_path

shortlist_path(cfg: MineConfig) -> Path

Return the durable shortlist path.

Source code in abc_mine/stages/rank.py
def shortlist_path(cfg: MineConfig) -> Path:
    """Return the durable shortlist path."""
    return cfg.rank_dir / SHORTLIST_FILENAME

rationale_path

rationale_path(cfg: MineConfig) -> Path

Return the durable rationale path.

Source code in abc_mine/stages/rank.py
def rationale_path(cfg: MineConfig) -> Path:
    """Return the durable rationale path."""
    return cfg.rank_dir / RATIONALE_FILENAME

attempts_path

attempts_path(cfg: MineConfig) -> Path

Return the rank stage's attempt log path.

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

build_candidates

build_candidates(summaries: list[dict[str, Any]]) -> list[dict[str, Any]]

Build the compact candidate payload from screening summaries.

Drops disqualified commits, keeps a trimmed projection of the rest, and sorts by score descending purely as a reading order. Pure and side-effect free, so the budget check and the tests can exercise it without a container.

Source code in abc_mine/stages/rank.py
def build_candidates(summaries: list[dict[str, Any]]) -> list[dict[str, Any]]:
    """Build the compact candidate payload from screening summaries.

    Drops disqualified commits, keeps a trimmed projection of the rest, and sorts by
    score descending purely as a reading order. Pure and side-effect free, so the
    budget check and the tests can exercise it without a container.
    """
    candidates: list[dict[str, Any]] = []
    for summary in summaries:
        if summary.get("disqualified"):
            continue
        candidate = {field: summary.get(field) for field in CANDIDATE_FIELDS}
        axes = summary.get("axes") or {}
        candidate["axes"] = {
            key: {"score": entry.get("score"), "why": entry.get("why")}
            for key, entry in axes.items()
            if isinstance(entry, dict)
        }
        candidates.append(candidate)

    candidates.sort(key=lambda c: c.get("benchmark_score") or 0, reverse=True)
    return candidates

render_candidate

render_candidate(candidate: dict[str, Any]) -> str

Render one candidate as a compact, self-delimiting text block.

Carries exactly the projected fields of :func:build_candidates, but as labelled lines rather than indented JSON: the braces, quotes, and indentation of pretty-printed JSON cost about 15% more tokens for no gain in legibility, and with a thousand candidates in one context that margin is the difference between fitting and not.

Source code in abc_mine/stages/rank.py
def render_candidate(candidate: dict[str, Any]) -> str:
    """Render one candidate as a compact, self-delimiting text block.

    Carries exactly the projected fields of :func:`build_candidates`, but as
    labelled lines rather than indented JSON: the braces, quotes, and
    indentation of pretty-printed JSON cost about 15% more tokens for no gain in
    legibility, and with a thousand candidates in one context that margin is
    the difference between fitting and not.
    """

    def value(v: Any) -> str:
        if v is None or v == "":
            return "none"
        if isinstance(v, (list, tuple)):
            return ", ".join(str(x) for x in v) if v else "none"
        return str(v)

    lines = [
        f"### {candidate.get('sha')}",
        f"subject: {value(candidate.get('subject'))}",
        (
            f"benchmark_score: {value(candidate.get('benchmark_score'))} | "
            f"est_difficulty: {value(candidate.get('est_difficulty'))} | "
            f"task_type: {value(candidate.get('task_type'))} | "
            f"confidence: {value(candidate.get('confidence'))} | "
            f"build_attempted: {value(candidate.get('build_attempted'))} | "
            f"build_outcome: {value(candidate.get('build_outcome'))}"
        ),
        f"one_line: {value(candidate.get('one_line'))}",
        f"proposed_task: {value(candidate.get('proposed_task'))}",
        f"proposed_verification: {value(candidate.get('proposed_verification'))}",
    ]
    axes = candidate.get("axes") or {}
    if axes:
        lines.append("axes:")
        lines += [
            f"- {key} {value(entry.get('score'))}: {value(entry.get('why'))}"
            for key, entry in axes.items()
        ]
    return "\n".join(lines)

render_candidates

render_candidates(candidates: list[dict[str, Any]]) -> str

Render the whole candidate list for embedding in the prompt.

Source code in abc_mine/stages/rank.py
def render_candidates(candidates: list[dict[str, Any]]) -> str:
    """Render the whole candidate list for embedding in the prompt."""
    return "\n\n".join(render_candidate(c) for c in candidates)

count_tokens

count_tokens(text: str, model: dict[str, str], *, timeout: float = 600.0) -> int

Return the endpoint's input-token count for text.

Uses Anthropic's /v1/messages/count_tokens, which runs no inference. Transient failures (5xx, connection errors) are retried; anything else is a :class:TokenCountError. There is deliberately no estimate fallback: with a hard context limit, a wrong guess surfaces as a failed or truncated run far from its cause.

Source code in abc_mine/stages/rank.py
def count_tokens(text: str, model: dict[str, str], *, timeout: float = 600.0) -> int:
    """Return the endpoint's input-token count for ``text``.

    Uses Anthropic's ``/v1/messages/count_tokens``, which runs no inference.
    Transient failures (5xx, connection errors) are retried; anything else is a
    :class:`TokenCountError`. There is deliberately **no estimate fallback**:
    with a hard context limit, a wrong guess surfaces as a failed or truncated
    run far from its cause.
    """
    if model.get("api") != "anthropic_messages":
        raise TokenCountError(
            "prompt sizing needs LLM_API=anthropic_messages (the endpoint's "
            f"/v1/messages/count_tokens), but LLM_API is {model.get('api')!r}.\n"
            "Set rank.max_prompt_tokens: null to run without a size check."
        )
    api_key = os.environ.get("LLM_API_KEY", "")
    if not api_key:
        raise TokenCountError("LLM_API_KEY is not set; cannot count prompt tokens")
    base = str(model["base_url"]).rstrip("/")
    url = f"{base}/messages/count_tokens" if base.endswith("/v1") else f"{base}/v1/messages/count_tokens"
    body = json.dumps(
        {"model": model["model"], "messages": [{"role": "user", "content": text}]}
    ).encode()
    request = urllib.request.Request(
        url,
        data=body,
        headers={
            "x-api-key": api_key,
            "anthropic-version": ANTHROPIC_VERSION,
            "content-type": "application/json",
            "user-agent": USER_AGENT,
        },
    )
    backoff = 2.0
    for attempt in range(1, 5):
        try:
            with urllib.request.urlopen(request, timeout=timeout) as response:
                payload = json.load(response)
            break
        except urllib.error.HTTPError as exc:
            detail = exc.read()[:300].decode("utf-8", "replace").strip()
            if exc.code < 500 or attempt == 4:
                raise TokenCountError(f"count_tokens HTTP {exc.code}: {detail}") from exc
        except (urllib.error.URLError, TimeoutError, OSError, json.JSONDecodeError) as exc:
            if attempt == 4:
                raise TokenCountError(
                    f"count_tokens failed: {type(exc).__name__}: {exc}"
                ) from exc
        time.sleep(backoff)
        backoff *= 2
    tokens = payload.get("input_tokens") if isinstance(payload, dict) else None
    if not isinstance(tokens, int):
        raise TokenCountError(f"count_tokens returned no input_tokens: {str(payload)[:200]}")
    return tokens

harness_prompt_text

harness_prompt_text(task_prompt: str) -> str

Return the text the harness actually sends: its system prompt + the task.

Mirrors the composition in harness/run_benchmark.sh so the size check measures what the model receives, not just the part this stage wrote.

Source code in abc_mine/stages/rank.py
def harness_prompt_text(task_prompt: str) -> str:
    """Return the text the harness actually sends: its system prompt + the task.

    Mirrors the composition in ``harness/run_benchmark.sh`` so the size check
    measures what the model receives, not just the part this stage wrote.
    """
    system = HARNESS_SYSTEM_PROMPT.read_text()
    return f"# System Instructions\n\n{system}\n---\n\n# Task\n\n{task_prompt}"

load_pool

load_pool(cfg: MineConfig) -> CandidatePool

Load summaries and build the candidate pool.

Raises:

Type Description
RankError

If there are no summaries.

Source code in abc_mine/stages/rank.py
def load_pool(cfg: MineConfig) -> CandidatePool:
    """Load summaries and build the candidate pool.

    Raises
    ------
    RankError
        If there are no summaries.
    """
    summaries = screen_stage.load_summaries(cfg)
    if not summaries:
        raise RankError(
            f"no screening summaries found in {cfg.summaries_dir}\n"
            "Run the screen stage first:  abc-mine screen --config <config>"
        )

    candidates = build_candidates(summaries)
    dropped = 0
    if cfg.rank.max_candidates is not None and len(candidates) > cfg.rank.max_candidates:
        dropped = len(candidates) - cfg.rank.max_candidates
        candidates = candidates[: cfg.rank.max_candidates]

    return CandidatePool(
        candidates=candidates,
        n_screened=len(summaries),
        n_disqualified=sum(1 for s in summaries if s.get("disqualified")),
        n_dropped_by_cap=dropped,
    )

plan_group

plan_group(cfg: MineConfig, pack: PromptPack, model: dict[str, str], pool: CandidatePool, candidates: list[dict[str, Any]], *, label: str = '', counter: Any = None) -> RankPlan

Compose the ranking prompt for candidates and measure it.

counter(text) -> int replaces the endpoint call (tests). Candidates are re-sorted by score as a reading order, whatever order they arrive in.

Raises:

Type Description
TokenCountError

If a budget is set but the prompt cannot be counted.

Source code in abc_mine/stages/rank.py
def plan_group(
    cfg: MineConfig,
    pack: PromptPack,
    model: dict[str, str],
    pool: CandidatePool,
    candidates: list[dict[str, Any]],
    *,
    label: str = "",
    counter: Any = None,
) -> RankPlan:
    """Compose the ranking prompt for ``candidates`` and measure it.

    ``counter(text) -> int`` replaces the endpoint call (tests). Candidates are
    re-sorted by score as a reading order, whatever order they arrive in.

    Raises
    ------
    TokenCountError
        If a budget is set but the prompt cannot be counted.
    """
    ordered = sorted(candidates, key=lambda c: c.get("benchmark_score") or 0, reverse=True)
    work_plan = RankPlan(
        candidates=ordered,
        n_screened=pool.n_screened,
        n_disqualified=pool.n_disqualified,
        n_dropped_by_cap=pool.n_dropped_by_cap,
        budget=cfg.rank.max_prompt_tokens,
        label=label,
    )
    work_plan.prompt = _compose_prompt(
        cfg, pack, work_plan, n_total=len(pool.candidates)
    )
    if work_plan.budget is not None and ordered:
        count = counter or (lambda text: count_tokens(text, model))
        work_plan.prompt_tokens = count(harness_prompt_text(work_plan.prompt))
    return work_plan

plan

plan(cfg: MineConfig, pack: PromptPack, model: dict[str, str], *, counter: Any = None) -> RankPlan

Compose the single-context ranking prompt and measure it against the budget.

Raises:

Type Description
RankError

If there are no summaries.

TokenCountError

If a budget is set but the prompt cannot be counted.

Source code in abc_mine/stages/rank.py
def plan(
    cfg: MineConfig,
    pack: PromptPack,
    model: dict[str, str],
    *,
    counter: Any = None,
) -> RankPlan:
    """Compose the single-context ranking prompt and measure it against the budget.

    Raises
    ------
    RankError
        If there are no summaries.
    TokenCountError
        If a budget is set but the prompt cannot be counted.
    """
    pool = load_pool(cfg)
    return plan_group(cfg, pack, model, pool, pool.candidates, counter=counter)

rank_group

rank_group(cfg: MineConfig, model: dict[str, str], work_plan: RankPlan, *, logs_dir: Path, item_id: str, dry_run: bool = False) -> GroupResult | None

Run one ranking agent over work_plan and return its validated result.

The unit of work shared by every method: one agent, one context, one prompt (the one work_plan measured, byte for byte). Returns None under a dry run.

Raises:

Type Description
RankError

If the agent fails or produces no usable shortlist. The attempt is recorded under item_id and the transcript is kept in logs_dir.

Source code in abc_mine/stages/rank.py
def rank_group(
    cfg: MineConfig,
    model: dict[str, str],
    work_plan: RankPlan,
    *,
    logs_dir: Path,
    item_id: str,
    dry_run: bool = False,
) -> GroupResult | None:
    """Run one ranking agent over ``work_plan`` and return its validated result.

    The unit of work shared by every method: one agent, one context, one prompt
    (the one ``work_plan`` measured, byte for byte). Returns ``None`` under a
    dry run.

    Raises
    ------
    RankError
        If the agent fails or produces no usable shortlist. The attempt is
        recorded under ``item_id`` and the transcript is kept in ``logs_dir``.
    """
    automaton_cfg = sandbox_mod.build_automaton_config(model)
    agent = sandbox_mod.make_agent(automaton_cfg)
    harness = sandbox_mod.make_harness()
    sandbox = sandbox_mod.make_sandbox(cfg, dry_run=dry_run)
    attempts = attempts_path(cfg)
    started = time.monotonic()

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

        # A read-only worktree at HEAD, so the agent (and any research subagent)
        # can inspect real code. Read-only in intent: nothing here is shipped.
        source_name = Path(cfg.codebase.source_mount).name
        from .. import repo as repo_mod

        full = cfg.rank.output == "full"
        if full:
            # The exact candidate set and the checker the prompt tells the agent
            # to run: the same module this stage validates with.
            (work / CANDIDATE_SHAS_FILENAME).write_text(
                "".join(f"{c['sha']}\n" for c in work_plan.candidates)
            )
            shutil.copy2(ranking_check.__file__, work / CHECKER_FILENAME)

        with repo_mod.worktree(cfg.codebase.repo, "HEAD", work / source_name):
            agent_started = time.monotonic()
            run_result = sandbox_mod.run_agent(
                cfg,
                sandbox=sandbox,
                harness=harness,
                agent=agent,
                work_dir=work,
                run_dir=run_dir,
                prompt_text=work_plan.prompt,
            )
            agent_seconds = time.monotonic() - agent_started

        if run_result.dry_run:
            return None

        # Telemetry item: the run id (single context) or "group-N" (interleaving).
        usage, _ = telemetry_mod.record_run(
            cfg.rank_dir, item_id.rsplit(":", 1)[-1], run_result.run_dir,
            keep=cfg.execution.keep_telemetry,
            pricing=model.get("pricing"),
            agent_seconds=agent_seconds,
            label_prefix="attempt",
            meta={"stage": "rank", "item": item_id, "label": work_plan.label,
                  "ok": run_result.ok, "model_id": model.get("model", "")},
        )

        output_name = RANKING_FILENAME if full else SHORTLIST_FILENAME
        output_file = run_result.out_path(output_name)
        rationale_file = run_result.out_path(RATIONALE_FILENAME)

        failure: str | None = None
        shortlist: dict[str, Any] | None = None

        if not run_result.ok:
            failure = run_result.detail or "agent execution failed"
        elif not output_file.is_file():
            failure = f"agent finished successfully but wrote no {output_name}"
        elif full:
            shortlist, error = _validate_ranking(
                output_file.read_text(errors="replace"), work_plan
            )
            if shortlist is None:
                failure = f"{output_name} was rejected: {error}"
        else:
            try:
                raw = json.loads(output_file.read_text())
            except json.JSONDecodeError as exc:
                failure = f"{output_name} is not valid JSON: {exc}"
            else:
                shortlist, error = _validate_shortlist(raw, work_plan)
                if shortlist is None:
                    failure = f"{output_name} is unusable: {error}"

        # Always keep the ranking transcript when something went wrong: a ranking
        # agent runs once, expensively, so a lost transcript means re-spending the
        # whole cost to learn what happened.
        kept = sandbox_mod.preserve_logs(
            run_result.run_dir,
            logs_dir,
            keep_logs="all" if failure else cfg.execution.keep_logs,
            failed=bool(failure),
        )
        # A rejected output is kept too: the agent's reasoning is in it, and
        # seeing exactly what was wrong is what makes the next attempt succeed.
        if failure and kept is not None and output_file.is_file():
            shutil.copy2(output_file, kept / f"rejected-{output_name}")

        if failure is not None or shortlist is None:
            record_attempt(
                attempts, item_id, "failed", detail=failure or "unknown",
                model_id=model.get("model", ""),
                usage=telemetry_mod.compact(usage),
            )
            where = f" ({work_plan.label})" if work_plan.label else ""
            raise RankError(f"ranking failed{where}: {failure}")

        seconds = round(time.monotonic() - started, 1)
        record_attempt(
            attempts, item_id, "ok",
            model_id=model.get("model", ""),
            n_selected=shortlist["n_selected"],
            n_candidates=len(work_plan.candidates),
            seconds=seconds,
            usage=telemetry_mod.compact(usage),
        )
        return GroupResult(
            shortlist=shortlist,
            rationale=rationale_file.read_text() if rationale_file.is_file() else "",
            usage=usage,
            seconds=seconds,
        )

write_outputs

write_outputs(cfg: MineConfig, shortlist: dict[str, Any], rationale: str) -> Path

Persist the shortlist (atomically, backing up any previous one) and rationale.

Source code in abc_mine/stages/rank.py
def write_outputs(cfg: MineConfig, shortlist: dict[str, Any], rationale: str) -> Path:
    """Persist the shortlist (atomically, backing up any previous one) and rationale."""
    existing = shortlist_path(cfg)
    cfg.rank_dir.mkdir(parents=True, exist_ok=True)
    if existing.is_file():
        shutil.copy2(existing, existing.with_suffix(".json.bak"))
    tmp = existing.with_suffix(".json.tmp")
    tmp.write_text(json.dumps(shortlist, indent=2, sort_keys=True) + "\n")
    tmp.replace(existing)

    rationale_path(cfg).write_text(
        rationale
        or (
            "# Ranking rationale\n\n"
            "The ranking agent did not emit a rationale document. "
            f"{shortlist['n_selected']} candidate(s) were selected; see "
            f"`{SHORTLIST_FILENAME}` for the per-candidate reasoning fields.\n"
        )
    )
    return existing

run

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

Execute the rank stage with the configured method; return the shortlist.

Raises:

Type Description
RankError

If there are no candidates, a prompt exceeds the budget or cannot be measured, or an agent fails to produce a usable shortlist.

Source code in abc_mine/stages/rank.py
def run(
    cfg: MineConfig,
    pack: PromptPack,
    model: dict[str, str],
    *,
    dry_run: bool = False,
    force: bool = False,
    progress: bool = True,
) -> dict[str, Any]:
    """Execute the rank stage with the configured method; return the shortlist.

    Raises
    ------
    RankError
        If there are no candidates, a prompt exceeds the budget or cannot be
        measured, or an agent fails to produce a usable shortlist.
    """
    if cfg.rank.method == "interleaving":
        from . import rank_interleaving

        return rank_interleaving.run(
            cfg, pack, model, dry_run=dry_run, force=force, progress=progress
        )

    from .. import parallel

    if parallel.launcher_size() > 1:
        # One agent, one context: there is nothing to share out, and N copies
        # of this function would each run the whole ranking, racing on the same
        # shortlist. Refuse on every rank (the CLI reports from rank 0 only).
        raise RankError(
            f"rank.method: single_context runs one agent, but this process was "
            f"launched as one of {parallel.launcher_size()} tasks. Launch it once "
            "(without srun -n N), or use rank.method: interleaving to rank groups "
            "in parallel."
        )

    work_plan = plan(cfg, pack, model)
    pool = CandidatePool(
        work_plan.candidates, work_plan.n_screened,
        work_plan.n_disqualified, work_plan.n_dropped_by_cap,
    )

    if progress:
        print(work_plan.describe())
        print()

    if not work_plan.candidates:
        raise _no_candidates_error(pool)
    if work_plan.over_budget:
        raise RankError(_budget_error(work_plan))
    _require_no_shortlist(cfg, force)

    result = rank_group(
        cfg, model, work_plan,
        logs_dir=cfg.rank_dir / "logs", item_id=cfg.run_id, dry_run=dry_run,
    )
    if result is None:
        if progress:
            print("Dry run: no agent was executed.")
        return _dry_run_result(cfg, pool)

    shortlist = result.shortlist
    shortlist["run_id"] = cfg.run_id
    shortlist["ranked_at"] = time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())
    shortlist["model_id"] = model.get("model", "")
    shortlist["usage"] = result.usage
    # Recorded so a shortlist always carries evidence of the process that made
    # it, and cannot later be mistaken for a multi-agent result.
    shortlist["method"] = {
        "name": "single_context",
        "output": cfg.rank.output,
        "single_context": True,
        "rounds": 1,
        "prompt_tokens": work_plan.prompt_tokens,
        "note": (
            ("Every candidate ranked" if cfg.rank.output == "full" else "Selected")
            + " by one agent holding all candidates in a single context. "
            "No chunk-and-merge or multi-round ranking was performed."
        ),
    }
    path = write_outputs(cfg, shortlist, result.rationale)

    if progress:
        print()
        verb = "Ranked" if cfg.rank.output == "full" else "Selected"
        print(
            f"{verb} {shortlist['n_selected']} of {len(work_plan.candidates)} "
            f"candidate(s)."
        )
        print(f"  shortlist: {path}")
        print(f"  rationale: {rationale_path(cfg)}")

    return shortlist

load_shortlist

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

Load this run's shortlist.

Raises:

Type Description
RankError

If it is missing or unparseable.

Source code in abc_mine/stages/rank.py
def load_shortlist(cfg: MineConfig) -> dict[str, Any]:
    """Load this run's shortlist.

    Raises
    ------
    RankError
        If it is missing or unparseable.
    """
    path = shortlist_path(cfg)
    if not path.is_file():
        raise RankError(
            f"no shortlist at {path}\n"
            "Run the rank stage first:  abc-mine rank --config <config>"
        )
    try:
        return json.loads(path.read_text())
    except json.JSONDecodeError as exc:
        raise RankError(f"shortlist is not valid JSON: {path} ({exc})") from exc