Eval harness · Eval runner · results.json and results.md · stage 5 of 5
Writing the report
After the last case, run.mjs records which commit produced the run and whether the demo app's code (demo/), the harness Worker (evals/worker.ts) or the trace (evals/trace.ts) had uncommitted changes, and saves the ledger once more. It writes two files to evals/runs/<run>/. results.json holds the metadata and every case's full record. results.md holds a tally by status, the medians of cost and latency, one table row per case and every answer, for a person to read. Last, it prints the tally, the medians, the spend, the ledger total and the report's path, and exits with status 0.
Runs in: Your machine, the developer's machine: npm test (vitest, demo/tests/), npm run eval (Node and wrangler dev, evals/) and npm run search (Node, examples/). New to Cloudflare? How these fit together
evals/run.mjs52 / 52 lines on this page explainedlines 298-349 of 349; the rest are on other stage pages
In the browser
runs in the visitor's browser, on the demo page
Ingestion
runs when a PDF's text is uploaded
Query time
runs when the Worker answers a question or a search_documents call from the MCP server, and in the request handling every route shares, in the Worker and in the site's functions, which uploads and removals pass through too
MCP request
runs on a request to the MCP server or its OAuth endpoints
Site request
runs on a site request that is not a question, an upload, an MCP call or the cron: a page load, a session read, the demo page's /api/grants calls, an admin read, a file's removal
Scheduled
runs in the Worker's daily cron job
Eval run
runs when the eval harness runs on a developer's machine
MCP host
runs in an MCP host outside the Worker: the search loop example, on a developer's machine (npm run search) or in its test
Module load
runs once, when the module is loaded
Type only
erased by the TypeScript compiler; never runs
Never runs
a comment, a licence or markup with no behaviour of its own
// Results. The spend is already in the ledger (saved after each case); it is saved once more below.// The results name the code that produced them; uncommitted changes under demo/, evals/worker.ts and evals/trace.ts are flagged.const git = execFileSync('git', ['rev-parse', '--short', 'HEAD']).toString().trim()const dirty = execFileSync('git', ['status', '--porcelain', '--', 'demo', 'evals/worker.ts', 'evals/trace.ts']).toString().trim() !== ''
Which commit, and was it clean Eval run
WhatReads the short hash of HEAD, and whether git status --porcelain shows changes under demo/, evals/worker.ts or evals/trace.ts.
WhyTwo runs can be compared only if each names the code it ran. The first comment notes that the spend is already in the ledger, saved after each case, and is saved once more below. The second comment names the paths checked for uncommitted changes. run.mjs (the scorer included) and demo.html are not among them, so an uncommitted change to scoring is not flagged.
The same idea, shown in the course's code: A run's record: its git commit, a dirty mark and its config From Prove it works, Ep 5: Caps, the ledger and comparing runs
// Write the ledger once more, then make the run's folder.saveLedger()mkdirSync(`evals/runs/${runId}`, { recursive: true })
Save the ledger, make the folder Eval run
WhatWrites the ledger again and creates evals/runs/<runId>/.
WhyIf no case ran (cases.jsonl had no cases, or --only matched none), the loop never saved, and this write is the one that puts the run's $0 entry in the ledger file.
// The metadata pins what produced the run (code, cases and corpus hashes, models) and what it cost,// so two runs can be compared knowing what differed between them.const meta = {run: runId, label, at: newDate().toISOString(), git: dirty ? `${git}+uncommitted` : git,cases_hash: sha(readFileSync('evals/cases.jsonl')), corpus: corpusHash,answer_model: 'claude-sonnet-5-5', rerank_model: 'claude-haiku-5-5', embedding_model: '@cf/baai/bge-m3',usd: Number(spent.toFixed(4)), budget, ledger_total_usd: ledger.total_usd,}
The run's metadata Eval run
WhatHolds the run id, the label, the finish time, the commit (with +uncommitted when dirty), the hashes of cases.jsonl and of each uploaded PDF, the three model names, this run's spend, its budget and the ledger total after it.
WhyA difference between two runs can be attributed only when everything else is pinned, so a changed cases_hash or corpus hash shows that the comparison is not like for like. The model names are string literals here, never read from demo/src/models.ts, so a model change in the demo app must be copied here or the metadata misreports it. cases_hash hashes the whole file, so it cannot tell apart --only runs over different subsets.
// results.json: the metadata, then every case's full result, for comparing runs and reading a case back.writeFileSync(`evals/runs/${runId}/results.json`, JSON.stringify({ meta, results }, null, 2) + '\n')
Write results.json Eval run
WhatWrites { meta, results } as indented JSON: the record from score() for each scored case, and { id, status: 'not run', reason } for each capped case.
WhyA later comparison reads this file. It holds every traced model call, every fact with whether it was met, the per-source booleans for the answer's context, and n/m counts for the pool, the context and the citations, so a question about a past run can often be answered without paying for a new run.
// results.md: a header, the tally by status, the medians, one table row per case, then every answer of a case that ran.consttally = (s) => results.filter((r) => r.status === s).length
Tally by status Eval run
Whattally counts the results with a given status.
WhyThe five statuses are counted separately, so a case that was not run or ended in error is never counted as a fail.
// The cases that ran, and the middle value of a list of numbers (the mean of the middle two when even; NaN for none).const ran = results.filter((r) => r.status !== 'not run')constmedian = (xs) => {const sorted = [...xs].sort((a, b) => a - b)const middle = Math.floor(sorted.length / 2)return sorted.length % 2 === 1 ? sorted[middle] : Math.round(((sorted[middle - 1] + sorted[middle]) / 2) * 10000) / 10000}
Cases that ran, and the median Eval run
Whatran excludes the not-run results. median sorts a copy numerically and returns the middle value, or for an even count the mean of the two middle values rounded to 4 decimal places. An empty list gives NaN.
WhyA few slow or long answers move a mean but barely move the median. The medians cover only the cases that ran, so a capped run's medians describe real asks.
`pass ${tally('pass')} · fail ${tally('fail')} · review ${tally('review')} · error ${tally('error')} · not run ${tally('not run')} · of ${results.length}`, '',`median a question: $${median(ran.map((r) => r.usd)).toFixed(4)} · ${median(ran.map((r) => r.ms))} ms · first sentence ${median(ran.filter((r) => r.first_ms !== null && r.first_ms !== undefined).map((r) => r.first_ms))} ms`, '',
Tally line and medians Eval run
WhatOne line counts each status and gives the total. The next gives the median cost per question, the median end-to-end ms, and the median first_ms over the cases that have one.
Whyfirst_ms is the time to the first checked sentence, which is the latency a visitor feels with streaming, so its median next to the end-to-end median shows what streaming gained. Fallbacks have no first_ms and are left out of that median.
evals/runs/2026-10-30T13-49-56-stream-final: pass 24, fail 1, review 5, error 0, not run 0, of 30evals/runs/2026-10-30T13-49-56-stream-final: median $0.0185 a question, 5426 ms, first sentence 4614 ms
'| case | status | behaviour | facts | sources in pool | sources in context | cited support | first sentence ms | ms | $ |', '|---|---|---|---|---|---|---|---|---|---|', ...results.map((r) => r.status === 'not run' ? `| ${r.id} | not run | | | | | | | | |` : `| ${r.id} | ${r.status} | ${r.found ? 'answered' : 'fallback'}${r.refused ? ` (${r.refused})` : ''}${r.stopped ? ` (stopped: ${r.stopped})` : ''} | ${r.facts.filter((f) => f.met).length}/${r.facts.length} | ${r.retrieval.sources_in_pool} of ${r.retrieval.pool} | ${r.retrieval.sources_in_context} of k=${r.retrieval.k} | ${r.citations.sources_supported} | ${r.first_ms ?? ''} | ${r.ms} | ${r.usd.toFixed(4)} |`),
One row per case Eval run
WhatA Markdown table with the case, status, behaviour (answered or fallback, with the refusal reason or the stop reason), facts met, sources in pool out of the pool size, sources in context out of k, cited support, first sentence ms, ms and dollars. A not-run case shows only its id and status.
WhyThe columns follow the pipeline stage by stage, so the column where a count first drops names the stage that lost a source. book-14's "3/3 of 48 | 2/3 of k=7 | 2/3" lost one source between the pool and the answer's context. A citation is always a passage the answer was given, so cited support higher than sources in context (resume-02 has 3/7 in context and 7/7 cited) means the context count missed sources that reached the answer through a widened passage's neighbours, and no source was lost.
WhatUnder ## Answers, for each case that ran: the id in bold and the question, the answer as a one-line blockquote, and the case's errors, if any.
WhyCode checks facts by substring. The review cases, the forbidden claims and any surprising pass need a person to read the answer, and this section puts every answer in one place for that reading.
The results.md that the run wrote. It opens with the run id, then the commit, the hash of evals/cases.jsonl and the spend, then the tally and the medians, then one table row per case. Every answer follows in full after the table. 2026-11-29, Terminal on macOS, in a checkout of the repository at the pinned commit
// The tally and medians lines, the spend, the ledger total and the report's path, printed last; the exit status is 0 whatever the results.console.log(`\n${lines[4]}\n${lines[6]}\n$${meta.usd.toFixed(4)} this run; ledger $${ledger.total_usd.toFixed(2)} of $${TOTAL_CAP}\nevals/runs/${runId}/results.md`)process.exit(0)
Print and exit Eval run
WhatPrints the tally line (lines[4]), the medians line (lines[6]), this run's spend, the ledger total and the report's path, then exits with status 0.
WhyThe explicit exit ends the process even if a handle is still open. It also fires the exit handler, which sends SIGTERM again to the Worker that was already stopped, with no effect. The exit status is always 0, even with fail or error cases, so whether the run passed is read from results.md or results.json, never from the exit code.
The run's output. The harness uploads the corpus through the real upload route and prints one line per case as it scores it. Then it prints the tally, the median cost and time, the spend against the ledger's cap and the path of results.md. 2026-11-29, Terminal on macOS, in a checkout of the repository at the pinned commit