Add bin/eval to score a classifier on saved merge requests

What does this MR do and why?

Part of #40: the read-only evaluation (B3) its upstream review needs before it goes live, including the replay of the upstream stage (B3a, merged in from !98 (merged)). #42 also lists landing a harness as make eval, but nothing here depends on #42.

Related to gitlab-org/gitlab#637889, which it does not close. bin/eval records a tier and its reasons without changing any approval, label or merge, but only for saved merge requests in the flow's categories. That work item still needs its sample, runs on new merge requests, and output in Snowflake.

It adds bin/eval and make eval, which run a classifier on saved merge requests and score its answers against labels. Nothing is written to any merge request, but every run is a model call and a Duo session in the project origin points at. It combines the read-only harnesses that exist today:

From What it brings
The docs suite in the closed !26 (closed) The classifier and its prompt come out of the compiled flow, so nothing is copied. The diff is handed over in the tool's own bytes. The 31 documentation cases, including the adversarial ones with a floor.
The read-only evaluation snippet ($6071062) The 37 pilot cases and the 30 gitlab-org/gitlab cases. Refusing a flow that keeps a write tool.
The harness behind #42's research Pinning the model and checking modelMetadataName for every session. Concurrent runs that resume. Running the steps after the model.

How it works

  • Each run's flow is cut from flows/<flow>.yml. The two merge request reads are replaced by saved responses, and the flow stops before apply_labels, and before clear_labels on the refusal and failure routes. EvalFlow.check! refuses a flow with any tool beyond create_file_with_contents, read_file and run_command, and spec/eval/eval_flow_spec.rb asserts that against both flows.
  • By default a run goes through the gate, the model, approval_rules, raise_below_one and decide_labels, so it records what the flow would decide. --classifier-only scores the model alone. A saved merge request is replayed as open and ready, because the gate refuses any other.
  • --variant main=git:origin/main --variant branch=flows/deps.yml compares two flows. The report lists the cases the second one rates low, or approves, more often, and in the flow mode the cases it labels less cautiously.
  • With no --variant, a run sends the suite's variants, or the working tree's flow where it names none. deps-pilots names production: git:0.26.0, the deps flow the three pilots pin in the AI catalog (version 1.3.0, whose definition_hash matches that tag's flows/deps.yml), so a default run scores what they run, without approval_rules. The monolith's flows/flow.yml is the same at 0.26.0 and on this branch, so its suites keep the working tree's flow.
  • bin/eval snapshot <suite> saves a case by running only the flow's own two reads. Saved merge requests keep no one's email address, and snapshot refuses a case whose project's merge requests are not public, since they are committed to this public repository.
  • --out resumes a stopped run. A resume keeps every variant, model and case the directory has, and refuses a label that names a different flow than before, or a different suite or mode. Options refuse a value that is itself a flag, so --out --show-flow can't start a real run.

What production does that it copies or works around

  • glab duo cli run reads and writes files in the root of the git checkout around its working directory. Each run gets a temporary directory outside every checkout, and bin/eval refuses to run otherwise.
  • The CLI's read_file masks strings it takes for secrets and appends a note, and a go.sum hash can look like one. Saved responses are therefore passed to the steps that read them as literal inputs, never read back from a file.
  • Duo filters every tool response before a step reads it: it strips HTML comments and other hidden content, and encodes the tags its prompts use as delimiters. A saved response has been through those filters. A response built from a .diff, and the fetch's replayed output, go through a port of them, evals/lib/tool_filter.rb, which refuses what it can't mirror.

The upstream stage (from !98 (merged))

The deps flow gains an upstream stage in #40: a fetch of the Go module's two zips, a judge with no tools, and keep_higher_tier, which keeps the higher tier at the start of the decide_labels step. It is built in a separate MR to a shared contract, so either can land first. This MR replays it:

  1. One answer file per agent. The suite's classifier saves its answer to answer.txt, and every other agent to answer.<agent>.txt, because the file tool won't overwrite. Result rows record the judge's answer as judge.
    • On deps-gitlab, whose flows/flow.yml has three classifiers, a case the gate sends to classify_impact or classify_spec_risk counts as "No answer", rather than having that classifier's tier scored as a deps tier.
  2. Replaying the fetch. The sandbox can't reach upstream, but run_command steps pass EvalFlow.local?.
    • bin/eval snapshot takes fetch_upstream's compiled command from flows/deps.yml and runs it with Ruby, outside the sandbox. It runs in a scratch directory holding the case's saved mr.json, mr_diff.json and a tier.txt of low. It saves <case>.upstream.out, .upstream.json and .upstream_route.txt beside the case. The runner is Upstream.fetch in evals/lib/upstream.rb, so specs can reach it.
    • It never overwrites a saved file, and it skips a case with only some of the three saved.
    • It saves nothing from a fetch that timed out, wrote no JSON or route, or failed for a reason of the machine or the moment (fetch failed, a 429 or 5xx from a host, no git), so the next snapshot tries again. The fetch runs in a process group of its own, so a timeout or an interrupt stops everything sh started.
    • In a run, EvalFlow swaps fetch_upstream for a stand-in that starts as the fetch does, with rm -f upstream.json upstream_route.txt judge.txt && mv tier.txt tier_model.txt. It then copies the saved upstream.json and route when the tier that reaches it, after approval_rules and raise_below_one, is low, or writes route skip when it isn't.
    • review_upstream's upstream input becomes Exit code: 0 and the saved stdout, through Duo's tool filters, as a literal, because that is what the CLI passes on from a run_command. Reads are given the same way.
    • The snapshot's fetch runs with no git config, attributes or checkout of the developer's around it, so the diff it saves is the one the stage ships.
    • A flow without fetch_upstream is unchanged. A flow that keeps it but has no saved output raises EvalFlow::Unsaved, and bin/eval aborts with run bin/eval snapshot. --show-flow uses a placeholder instead, so it needs no fetch.
  3. Report.
    • A new Upstream stage section has per-group totals: judged, check, incomplete, skipped, judge unreadable, as expected, and lowered by the stage.
    • Its per-case table shows what the stage did, the judge's tiers, the tiers labelled, and whether those are more or less cautious than the first variant's. The judge's answer is read as keep_higher_tier reads it: one JSON object whose tier is low, medium or high. Less cautious is in bold. x marks a run its upstream doesn't expect, and lowered by the stage marks a run labelled below the tier that reached the stage. A skipped run shows that tier.
    • Right after the summary, a new Less cautious than main section lists every case a later variant labels less cautiously, so none can be missed.
  4. Suites.
    • deps-pilots.yml documents an optional upstream: key: keep, raise, check or incomplete. Every case has one. 28 are set from their saved output, where it decides the outcome without the judge. The nine the fetch routes to the judge were set by a person reading the judge's saved input against its rubric: keep for seven, and raise for gitlab-org/gitlab-elasticsearch-indexer!860 (merged) (a new exported function that opens a file) and !865 (an unescaping fix the release notes don't mention). With the stage, 4 of the 14 low-labelled cases are left for the flow to approve; the other 10 should go to a person.
    • upstream: holds only a variant whose flow has the stage, which meta.json records per variant. There, approving a case whose upstream is raise, check or incomplete counts as a wrong approval, and "Approvable cases approved" leaves the case out. A flow without the stage is held to the labels alone, and "low cases rated low" counts every case the labels accept as low either way.
    • deps-upstream.yml scaffolds the six planted cases from #40's evaluation plan. Each is one of the eight low Go module bumps in deps-pilots, with its merge request files copied unchanged; es-indexer-859 and -865, two gitaly bumps the fetch routes to the judge, are not used. planted: says what to edit in the saved upstream output. The cases the judge must read sit on the three zoekt bumps the fetch routes to the judge. The three es-indexer bumps are over the fetch's 48 KB cap, so over-the-cap needs no edit. All six are planted.

Checked against the stage as built

The stage is !99 (closed), at 8973f78f, where keep_higher_tier runs at the start of the decide_labels step rather than as a step of its own, and the fetch also clears judge.txt, since the file tool won't overwrite one left in place. spec/eval passes on this branch with its flows/deps.yml laid over, with no example skipped, and so does its spec/deps. A replay of the stand-in, then that flow's own classify_change and decide_labels commands, across sixteen upstream results, ends on the tier the report expects in every one, including one with a stale judge.txt. It also provides what the contract left open:

  1. fetch_upstream reads only mr.json, mr_diff.json and tier_model.txt.
  2. It needs only Ruby, git and the two hosts, with timeouts of 10 and 30 seconds per request.
  3. It prints only the judge's input, on the judge route, and discards git's stderr.
  4. It reads the tier as decide_labels does.
  5. keep_higher_tier raises any checksum but ok, unchecked included.
  6. Its red-flag rules are os/exec, net/http, unsafe, plugin, cgo, go:linkname, func init, encoded string and binary file.
  7. An unexpected error is reported as fetch failed: <class>.

Once the stage lands, bin/eval run on a deps suite aborts until bin/eval snapshot has saved each case's upstream output. spec/eval passes saved output to every eval_flow call on the real flows that reaches the fetch, and the deps chain spec pins only the classifier, its answer and write_tier_file, so the stage landing won't break spec/eval. Specs that hold the stage's fetch to the stand-in are skipped until then, and pass with !99 (closed)'s flow laid over.

Validation

One pass on claude_sonnet_5_5_vertex, with every session confirmed through modelMetadataName:

Suite Mode Accepted Wrong low Wrong approval
deps-pilots classifier only 29/32 2 (1 provisional) n/a
deps-pilots flow 28/32 2 (1 provisional) 0
deps-gitlab, runtime below 1.0 flow 5/6 1 1: gitlab-org/gitlab!260517 (merged) (httparty)
docs, 3 cases classifier only 1/3 1 n/a

The deps-pilots flow run sent the working tree's flows/deps.yml, with approval_rules; a default run now sends the pilots' 0.26.0. These runs predate the upstream replay, the tool filters for .diff cases and the relabelled docs adversarial cases, which are now labelled on their content. The replay changes no flow deps-pilots or docs sends. On deps-gitlab, a case the gate sends to another classifier now counts as "No answer".

deps-upstream against !99 (closed)'s flow at 46ff512d, on the same model, gave each planted case its expected outcome. checksum-mismatch and red-flag went to a person without the judge. undocumented-change was judged medium, and text-for-the-judge high, the judge naming the comment aimed at it. over-the-cap was incomplete. tests-only was judged low and approved. Its first planting kept release notes about code its diff no longer showed, which the judge rated medium, so it was rebuilt and run again. Every judge answer was valid JSON. Since then, 8973f78f has rewrapped the judge's prompt with the same words and changed no fetch output, so the saved upstream files still hold.

On 36 of the 37 pilot cases, the model's tier is within #42's per-case range on Sonnet 5.5. The other one, gitlab-org/gitlab-zoekt-indexer!1039 (merged), came back low in classifier-only mode, which is within #42's raw-model rate for that case.

Labels to confirm

Not in this MR

  • A harness that is only in a local clone, which pins more models. --model takes any model identifier.
  • Labelled cases for classify_spec_risk.
  • Runs of the deps suites on this branch's own flows/deps.yml, which has no fetch_upstream until !99 (closed) lands. --variant stage=git:origin/jecsanders-40-upstream-review runs the stage before then.

How to set up and validate locally

  1. make check
  2. ruby bin/eval run deps-pilots --show-flow prints the flow a run would send, with no model call.
  3. make eval SUITE=deps-pilots ARGS="--repeat 1 --only zoekt-1073" makes one model call.

🤖 Generated with Claude Code

Edited by Connor Sanders

Merge request reports

Loading
Loading