From 4c42d5a442054cd7f67bcfc56d6dac68c435e034 Mon Sep 17 00:00:00 2001 From: Alexander Konstantinov Date: Wed, 18 Nov 2026 13:07:22 +0200 Subject: [PATCH 1/2] refactor(cli): stop a run through an abort signal The runner takes the signal that stops it and returns once the run has stopped, instead of handling SIGINT and SIGTERM and exiting itself. The evaluate command owns the signals and the exit code, so another caller can run an evaluation in the same process and go on afterwards. A signal that comes before a run writes its folder now ends the command without the run. --- packages/cli/src/main.ts | 22 ++++++++++++++++++---- packages/cli/src/openrca.ts | 36 +++++++++++++++++++----------------- 2 files changed, 37 insertions(+), 21 deletions(-) diff --git a/packages/cli/src/main.ts b/packages/cli/src/main.ts index 59e5dee..9258351 100644 --- a/packages/cli/src/main.ts +++ b/packages/cli/src/main.ts @@ -13,9 +13,10 @@ * that copy on disk instead of downloading them. * `evorca evaluate` runs an agent from `agents/` on those scenarios, one Docker * container per task, scores its answers, and keeps each run in - * `data/runs///`. `evorca report` prints the summary of - * finished runs from those folders. `evorca watch` shows a run full screen - * while it runs, from its folder, and follows the newest run without a run ID. + * `data/runs///`. On SIGINT or SIGTERM, it stops the run + * and exits with 130 or 143. `evorca report` prints the summary of finished + * runs from those folders. `evorca watch` shows a run full screen while it + * runs, from its folder, and follows the newest run without a run ID. * * The command takes the real API key from `ANTHROPIC_API_KEY` and then deletes * the variable, so no Docker command, container, or evaluator that it starts @@ -106,7 +107,20 @@ const commands = { apiKey, upstreamUrl: readUpstreamUrl(), }; - if (!(await benchmark.evaluate(options))) process.exitCode = 1; + const stop = new AbortController(); + let stopSignal: NodeJS.Signals | undefined; + const onSignal = (signal: NodeJS.Signals) => { + stopSignal = signal; + console.error("\nStopping. Press ctrl+c again to stop at once."); + stop.abort(); + }; + process.once("SIGINT", onSignal).once("SIGTERM", onSignal); + const evaluation = benchmark.evaluate({ ...options, signal: stop.signal }); + const ok = await evaluation.finally(() => { + process.off("SIGINT", onSignal).off("SIGTERM", onSignal); + if (stopSignal !== undefined) process.exit(stopSignal === "SIGINT" ? 130 : 143); + }); + if (!ok) process.exitCode = 1; }, async report({ benchmark, scenarios, runIds, runsDir }: Invocation) { if (!(await benchmark.report({ scenarios, runIds, runsDir }))) process.exitCode = 1; diff --git a/packages/cli/src/openrca.ts b/packages/cli/src/openrca.ts index 2ea5bc3..e4ed222 100644 --- a/packages/cli/src/openrca.ts +++ b/packages/cli/src/openrca.ts @@ -132,6 +132,7 @@ export async function reproduceOpenRca({ * agent's `jobs`, every row, a random seed, `claude-sonnet-5` at the model's * default effort, a $50 run budget, and a $2 task budget. `apiKey` is the real * API key, and `upstreamUrl` is the API that the model proxy forwards to. + * `signal` stops the runs. */ export type EvaluateRequest = { readonly scenarios: readonly OpenRcaScenario[]; @@ -147,6 +148,7 @@ export type EvaluateRequest = { readonly taskBudgetUsd: number | undefined; readonly apiKey: string | undefined; readonly upstreamUrl: string; + readonly signal: AbortSignal; }; type Dirs = { @@ -219,10 +221,12 @@ const defaultTaskBudgetUsd = 2; * if the relay cannot reach the proxy. It also returns false if the summary of * a series finds no finished run of a scenario. Otherwise it returns true. * - * On SIGINT or SIGTERM, it starts no more tasks and removes the run's + * Once `signal` aborts, it starts no more tasks and removes the run's * containers. It waits for the model requests in flight, so the run records * their cost, then records each running task as `stopped` with what it spent, - * removes the run's networks, and exits with 130 or 143. The run can resume. + * removes the run's networks, prints the command that resumes the run, and + * returns false. The run can resume. If `signal` aborts before a run writes + * its folder, the run does not start, and it returns false. * * @throws Error if git, an image build, a container start, or the evaluator fails. */ @@ -234,7 +238,9 @@ export async function evaluateOpenRca(request: EvaluateRequest & Dirs): Promise< : await readRuns(runsDir, request.resume, request.scenarios); const recorded = resumed?.values().next().value; const place = recorded?.series; - if (place === undefined && (request.runs ?? 1) === 1) return evaluateRun(request, undefined); + if (place === undefined && (request.runs ?? 1) === 1) { + return (await evaluateRun(request, undefined)) && !request.signal.aborted; + } const name = recorded?.agent.name ?? request.agent; if (name === undefined) throw new Error("evaluateOpenRca needs an agent or a run to resume."); @@ -265,7 +271,7 @@ export async function evaluateOpenRca(request: EvaluateRequest & Dirs): Promise< const member = { runId, series: { id, run: index + 1, runs }, sameAs }; const resume = existing.size > 0 ? runId : undefined; const run = { ...request, scenarios, seed: firstSeed + index, resume }; - if (!(await evaluateRun(run, member))) return false; + if (!(await evaluateRun(run, member)) || request.signal.aborted) return false; } sameAs ??= (await readRuns(runsDir, runId, scenarios)).values().next().value; } @@ -412,6 +418,7 @@ async function evaluateRun( console.log(`Warning: ${asked} need more than Docker's ${memoryMiB} MiB of memory.`); } + if (request.signal.aborted) return false; if (recorded !== undefined) await removeRun(runId); const startedAt = new Date().toISOString(); const plans: ScenarioPlan[] = []; @@ -476,20 +483,17 @@ async function evaluateRun( plans.push({ scenario, tasks: scenarioTasks, selected, runDir, record }); } - const stop = new AbortController(); - let stopSignal: NodeJS.Signals | undefined; + const { signal } = request; let sweeper: NodeJS.Timeout | undefined; let modelSession: ModelSession | undefined; - const onSignal = (signal: NodeJS.Signals) => { - stopSignal = signal; - stop.abort(); - console.error("\nStopping. Press ctrl+c again to stop at once."); + const onStop = () => { // A task that was starting when the signal came may start its container after one sweep. const sweep = () => void removeRunContainers(runId).catch(() => {}); sweep(); sweeper = setInterval(sweep, 1000); }; - process.once("SIGINT", onSignal).once("SIGTERM", onSignal); + if (signal.aborted) onStop(); + else signal.addEventListener("abort", onStop, { once: true }); try { if (model !== null && budgetUsd !== null && request.apiKey !== undefined) { const others = [...(runRecords?.values() ?? [])].filter( @@ -511,8 +515,7 @@ async function evaluateRun( spent, }); const relay = await startRelay(runId, proxy.port).catch((error: unknown) => { - if (!stop.signal.aborted) - console.error(error instanceof Error ? error.message : String(error)); + if (!signal.aborted) console.error(error instanceof Error ? error.message : String(error)); return undefined; }); modelSession = relay === undefined ? undefined : { settings: model, budgetUsd, proxy, relay }; @@ -521,7 +524,7 @@ async function evaluateRun( return false; } } - const session: Session = { jobs, dataDir, cacheDir, signal: stop.signal, model: modelSession }; + const session: Session = { jobs, dataDir, cacheDir, signal, model: modelSession }; const scores: ScoredRow[] = []; for (const plan of plans) { const scenarioScores = await evaluateScenario(plan, session); @@ -539,18 +542,17 @@ async function evaluateRun( } return true; } finally { - process.off("SIGINT", onSignal).off("SIGTERM", onSignal); + signal.removeEventListener("abort", onStop); clearInterval(sweeper); if (modelSession !== undefined) await modelSession.proxy.close(); await removeRun(runId).catch((error: unknown) => { const reason = error instanceof Error ? error.message : String(error); console.error(`Could not remove the containers and networks of run ${runId}: ${reason}`); }); - if (stopSignal !== undefined) { + if (signal.aborted) { const resume = `pnpm evorca evaluate --benchmark=openrca --resume=${runId}`; const what = series === undefined ? "Resume" : `Finish the ${series.runs} runs`; console.error(`Stopped. ${what} with: ${resume}`); - process.exit(stopSignal === "SIGINT" ? 130 : 143); } } } From 18caf6a9bd5a7f3c16eb46a24d5717b1dde12641 Mon Sep 17 00:00:00 2001 From: Alexander Konstantinov Date: Wed, 18 Nov 2026 13:07:23 +0200 Subject: [PATCH 2/2] feat(cli): show evorca evaluate full screen in a terminal In a terminal, evorca evaluate shows the run in the view of evorca watch, with the last lines that it printed under the feed. When the run ends or stops, the view closes and the command prints every line, so the scrollback holds the same text as before. --plain, or output without a terminal, prints the lines as the run goes. --- .changeset/cli-evaluate-view.md | 5 + packages/cli/README.md | 7 +- packages/cli/src/evaluate-view.test.ts | 42 ++++++++ packages/cli/src/evaluate-view.tsx | 139 +++++++++++++++++++++++++ packages/cli/src/main.ts | 22 ++-- packages/cli/src/openrca.ts | 5 +- packages/cli/src/watch-view.test.tsx | 25 ++++- packages/cli/src/watch-view.tsx | 32 ++++-- 8 files changed, 259 insertions(+), 18 deletions(-) create mode 100644 .changeset/cli-evaluate-view.md create mode 100644 packages/cli/src/evaluate-view.test.ts create mode 100644 packages/cli/src/evaluate-view.tsx diff --git a/.changeset/cli-evaluate-view.md b/.changeset/cli-evaluate-view.md new file mode 100644 index 0000000..0c54655 --- /dev/null +++ b/.changeset/cli-evaluate-view.md @@ -0,0 +1,5 @@ +--- +"@evorca/cli": minor +--- + +`evorca evaluate` shows the run full screen in a terminal, in the view of `evorca watch`, with the lines that it prints under the feed. When the run ends or stops, the view closes and the command prints those lines, so the scrollback holds the same text as before. `--plain` and output without a terminal print the lines as the run goes, as before. diff --git a/packages/cli/README.md b/packages/cli/README.md index 7017395..9f9a2fd 100644 --- a/packages/cli/README.md +++ b/packages/cli/README.md @@ -31,8 +31,9 @@ evorca reproduce --benchmark= [--scenario=...] [--archive=< evorca evaluate --benchmark= --agent= [--scenario=...] [--runs=] [--rows=,...] [--jobs=] [--model=] [--effort=] [--budget-usd=] [--task-budget-usd=] + [--plain] evorca evaluate --benchmark= --resume= [--scenario=...] - [--jobs=] [--budget-usd=] + [--jobs=] [--budget-usd=] [--plain] evorca report --benchmark= [--scenario=...] ... evorca watch --benchmark= [--scenario=...] [] ``` @@ -161,6 +162,8 @@ A run with the same seed writes the same predictions, byte for byte, whatever it With `--jobs=4`, four containers run at once, and the lines come in the order that the tasks finish. If the agent's tasks together ask for more memory than Docker has, the command prints a warning before the first task. On Docker Desktop, set the memory under **Settings** > **Resources**. +In a terminal, the command shows the run full screen in the view of [`evorca watch`](#watch-a-run), with the last four lines that it printed under the feed. When the run ends or stops, the view closes and the command prints every line as above, the accuracy among them, so the scrollback holds the same text either way. `--plain` prints the lines as the run goes instead, as for a run in the background. Without a terminal, as in CI or with output to a file, the command prints the lines. + For a pilot, run a few rows with `--rows=0,7,21`. The command still writes a `predictions.csv` row for every task, because the evaluator matches predictions to tasks by position, and gives the other rows the empty prediction `{}`. The accuracy and `scores.json` cover only the rows of the run. ### Evaluate an agent that calls a model @@ -199,7 +202,7 @@ Once the run's budget is spent, each task that has not started gets the outcome ### Resume a run -To stop a run, press ctrl+c. The command starts no more tasks and removes the run's containers. It waits for the model requests in flight, so the run records their cost. It records each task that was running with the outcome `stopped` and what its `requests.jsonl` says it spent, so the totals in `run.json` match the request logs at once. Then it removes the run's networks and prints the command that resumes the run. Press ctrl+c again to stop at once. +To stop a run, press ctrl+c. The command starts no more tasks and removes the run's containers. It waits for the model requests in flight, so the run records their cost. It records each task that was running with the outcome `stopped` and what its `requests.jsonl` says it spent, so the totals in `run.json` match the request logs at once. Then it removes the run's networks and prints the command that resumes the run. Press ctrl+c again to stop at once. In the full-screen view, ctrl+c does the same: the first stops the run, and the view shows the stop until the run has stopped. The second closes the view and then stops at once. ```sh pnpm evorca evaluate --benchmark=openrca --resume=20261117T121537Z-random diff --git a/packages/cli/src/evaluate-view.test.ts b/packages/cli/src/evaluate-view.test.ts new file mode 100644 index 0000000..005813d --- /dev/null +++ b/packages/cli/src/evaluate-view.test.ts @@ -0,0 +1,42 @@ +import { join } from "node:path"; +import { describe, expect, it } from "vitest"; +import { captureConsole, followRuns, type LogLine } from "./evaluate-view.js"; +import { runRecord, runsFolder } from "./watch-fixtures.js"; + +describe("captureConsole", () => { + it("sends what the console prints to a function until it restores the console", () => { + const original = console.log; + const lines: LogLine[] = []; + const restore = captureConsole((line) => lines.push(line)); + console.log("Seed %d.", 7); + console.error("Stopped."); + restore(); + + expect(lines).toEqual([ + { text: "Seed 7.", stream: "stdout" }, + { text: "Stopped.", stream: "stderr" }, + ]); + expect(console.log).toBe(original); + }); +}); + +describe("followRuns", () => { + it("waits for the run that the evaluation starts, then shows it even when a newer run exists", async () => { + const folder = await runsFolder(); + const { read, onRun } = followRuns({ + runsDir: folder.runsDir, + dataDir: join(folder.runsDir, "no-data"), + scenarios: ["telecom"], + }); + await folder.writeRun(runRecord({ runId: "20261118T094052Z-react" })); + await folder.writeRun(runRecord({ runId: "20261119T000000Z-react" })); + + expect(await read({ kind: "live" })).toEqual({ kind: "waiting", message: "Starting the run…" }); + + onRun("20261118T094052Z-react"); + expect(await read({ kind: "live" })).toMatchObject({ + kind: "run", + state: { record: { runId: "20261118T094052Z-react" } }, + }); + }); +}); diff --git a/packages/cli/src/evaluate-view.tsx b/packages/cli/src/evaluate-view.tsx new file mode 100644 index 0000000..b5d49b5 --- /dev/null +++ b/packages/cli/src/evaluate-view.tsx @@ -0,0 +1,139 @@ +/** + * `evorca evaluate` in a terminal: the run full screen in the view of `evorca + * watch`, with the last lines that the runner printed under the feed. When + * the runner settles, the view closes, the terminal is restored, and every + * line that the runner printed goes to stdout or stderr in order. So the + * scrollback holds the same text as a plain run, the score tables among it. + * + * Ctrl+c in the view sends SIGINT to the process, as ctrl+c does in a plain + * terminal. When SIGINT or SIGTERM comes and nothing else handles it, so that + * the signal ends the process, the view closes and prints the lines first. + * + * A view that throws while it draws shows the error in place of the run, and + * ctrl+c still reaches the runner, because the key is read outside the view. + * An uncaught error closes the view and prints the lines and the error, and + * the run goes on with plain lines. + * + * @module + */ +// @jsxRuntime automatic +// @jsxImportSource @opentui/react +import { format } from "node:util"; +import { createCliRenderer } from "@opentui/core"; +import { createRoot } from "@opentui/react"; +import { palette } from "./shell.js"; +import { readRunFolders, type Frame, type RunFolders } from "./watch-folder.js"; +import { Watch } from "./watch-view.js"; +import type { Selection } from "./watch.js"; + +/** A line that the runner printed, and the stream it was meant for. */ +export type LogLine = { readonly text: string; readonly stream: "stdout" | "stderr" }; + +const signals = ["SIGINT", "SIGTERM"] as const; +const methods = [ + ["log", "stdout"], + ["info", "stdout"], + ["warn", "stderr"], + ["error", "stderr"], +] as const; + +/** + * Sends what `console.log`, `console.info`, `console.warn`, and + * `console.error` print to `onLine` instead of the terminal, as the console + * formats it, until the returned function restores them. + */ +export function captureConsole(onLine: (line: LogLine) => void): () => void { + const originals = methods.map(([method]) => [method, console[method]] as const); + for (const [method, stream] of methods) { + console[method] = (...args: unknown[]) => onLine({ text: format(...args), stream }); + } + return () => { + for (const [method, original] of originals) console[method] = original; + }; +} + +/** + * Reads the runs of an evaluation for {@link Watch}. The view waits until + * `onRun` names a run, then shows the run that `onRun` named last. + */ +export function followRuns(folders: Omit): { + readonly read: (selection: Selection) => Promise; + readonly onRun: (runId: string) => void; +} { + let runId: string | undefined; + const read = readRunFolders({ ...folders, runId: () => runId }); + return { + read: (selection) => + runId === undefined + ? Promise.resolve({ kind: "waiting", message: "Starting the run…" }) + : read(selection), + onRun: (id) => { + runId = id; + }, + }; +} + +/** + * Calls `evaluate` and shows its runs full screen until it settles, then + * prints what it printed. `evaluate` passes the ID of each run to `onRun` + * once the run's folder holds `run.json`. + */ +export async function evaluateFullScreen( + folders: Omit, + evaluate: (onRun: (runId: string) => void) => Promise, +): Promise { + const lines: LogLine[] = []; + const { read, onRun } = followRuns(folders); + const renderer = await createCliRenderer({ + exitOnCtrlC: false, + exitSignals: [], + consoleMode: "disabled", + openConsoleOnError: false, + useMouse: false, + backgroundColor: palette.background, + }); + const restoreConsole = captureConsole((line) => lines.push(line)); + + let flushed = false; + const flush = () => { + if (flushed) return; + flushed = true; + restoreConsole(); + process.off("exit", flush).off("uncaughtException", crash).off("unhandledRejection", crash); + for (const signal of signals) process.off(signal, onSignal); + for (const { text, stream } of lines) process[stream].write(`${text}\n`); + }; + let closing: Promise | undefined; + const close = () => + (closing ??= (async () => { + renderer.destroy(); + await renderer.closed.catch(() => {}); + flush(); + })()); + const crash = (error: unknown) => { + if (renderer.isDestroyed) console.error(error); + void close(); + }; + const onSignal = (signal: NodeJS.Signals) => { + if (process.listenerCount(signal) > 1) return; + void close().then(() => process.kill(process.pid, signal)); + }; + process.once("exit", flush).on("uncaughtException", crash).on("unhandledRejection", crash); + for (const signal of signals) process.prependListener(signal, onSignal); + renderer.keyInput.on("keypress", (key) => { + if (key.ctrl && key.name === "c") process.kill(process.pid, "SIGINT"); + }); + + createRoot(renderer).render( + new Date()} + log={() => lines.flatMap((line) => line.text.split("\n"))} + />, + ); + try { + return await evaluate(onRun); + } finally { + await close(); + } +} diff --git a/packages/cli/src/main.ts b/packages/cli/src/main.ts index 9258351..550b221 100644 --- a/packages/cli/src/main.ts +++ b/packages/cli/src/main.ts @@ -13,10 +13,12 @@ * that copy on disk instead of downloading them. * `evorca evaluate` runs an agent from `agents/` on those scenarios, one Docker * container per task, scores its answers, and keeps each run in - * `data/runs///`. On SIGINT or SIGTERM, it stops the run - * and exits with 130 or 143. `evorca report` prints the summary of finished - * runs from those folders. `evorca watch` shows a run full screen while it - * runs, from its folder, and follows the newest run without a run ID. + * `data/runs///`. In a terminal, it shows the run in the + * view of `evorca watch` unless `--plain` asks for lines. On SIGINT or + * SIGTERM, it stops the run and exits with 130 or 143. `evorca report` prints + * the summary of finished runs from those folders. `evorca watch` shows a run + * full screen while it runs, from its folder, and follows the newest run + * without a run ID. * * The command takes the real API key from `ANTHROPIC_API_KEY` and then deletes * the variable, so no Docker command, container, or evaluator that it starts @@ -115,7 +117,12 @@ const commands = { stop.abort(); }; process.once("SIGINT", onSignal).once("SIGTERM", onSignal); - const evaluation = benchmark.evaluate({ ...options, signal: stop.signal }); + const run = (onRun?: (runId: string) => void) => + benchmark.evaluate({ ...options, signal: stop.signal, onRun }); + const fullScreen = !flags.plain && process.stdin.isTTY && process.stdout.isTTY; + const view = fullScreen ? await import("./evaluate-view.js") : undefined; + const evaluation = + view === undefined ? run() : view.evaluateFullScreen({ ...dirs, scenarios }, run); const ok = await evaluation.finally(() => { process.off("SIGINT", onSignal).off("SIGTERM", onSignal); if (stopSignal !== undefined) process.exit(stopSignal === "SIGINT" ? 130 : 143); @@ -149,8 +156,9 @@ const usage = [ " evorca evaluate --benchmark= --agent= [--scenario=...]", " [--runs=] [--rows=,...] [--jobs=] [--model=]", " [--effort=] [--budget-usd=] [--task-budget-usd=]", + " [--plain]", " evorca evaluate --benchmark= --resume= [--scenario=...]", - " [--jobs=] [--budget-usd=]", + " [--jobs=] [--budget-usd=] [--plain]", " evorca report --benchmark= [--scenario=...] ...", " evorca watch --benchmark= [--scenario=...] []", ].join("\n"); @@ -212,6 +220,7 @@ type Flags = { readonly effort?: string | undefined; readonly "budget-usd"?: string | undefined; readonly "task-budget-usd"?: string | undefined; + readonly plain?: boolean | undefined; }; function parseCommandLine() { @@ -231,6 +240,7 @@ function parseCommandLine() { effort: { type: "string" }, "budget-usd": { type: "string" }, "task-budget-usd": { type: "string" }, + plain: { type: "boolean" }, }, }); } catch (error: unknown) { diff --git a/packages/cli/src/openrca.ts b/packages/cli/src/openrca.ts index e4ed222..965c526 100644 --- a/packages/cli/src/openrca.ts +++ b/packages/cli/src/openrca.ts @@ -132,7 +132,8 @@ export async function reproduceOpenRca({ * agent's `jobs`, every row, a random seed, `claude-sonnet-5` at the model's * default effort, a $50 run budget, and a $2 task budget. `apiKey` is the real * API key, and `upstreamUrl` is the API that the model proxy forwards to. - * `signal` stops the runs. + * `signal` stops the runs, and `onRun` hears the ID of each run once its + * folder holds `run.json`. */ export type EvaluateRequest = { readonly scenarios: readonly OpenRcaScenario[]; @@ -149,6 +150,7 @@ export type EvaluateRequest = { readonly apiKey: string | undefined; readonly upstreamUrl: string; readonly signal: AbortSignal; + readonly onRun?: (runId: string) => void; }; type Dirs = { @@ -483,6 +485,7 @@ async function evaluateRun( plans.push({ scenario, tasks: scenarioTasks, selected, runDir, record }); } + request.onRun?.(runId); const { signal } = request; let sweeper: NodeJS.Timeout | undefined; let modelSession: ModelSession | undefined; diff --git a/packages/cli/src/watch-view.test.tsx b/packages/cli/src/watch-view.test.tsx index 03c884d..f8965c6 100644 --- a/packages/cli/src/watch-view.test.tsx +++ b/packages/cli/src/watch-view.test.tsx @@ -16,7 +16,7 @@ import { taskRecord, tool, } from "./watch-fixtures.js"; -import { Watch } from "./watch-view.js"; +import { Watch, type WatchProps } from "./watch-view.js"; import type { Selection } from "./watch.js"; let setup: TestRendererSetup; @@ -36,7 +36,7 @@ const local = (time: string) => const lines = (count: number, prefix: string) => Array.from({ length: count }, (_, index) => `${prefix} ${index + 1}`).join("\n"); -async function open(run: RunRecord | null = record, height = 40) { +async function open(run: RunRecord | null = record, height = 40, props: Partial = {}) { folder = await runsFolder(); const dataDir = join(folder.runsDir, "benchmarks"); await mkdir(join(dataDir, "telecom"), { recursive: true }); @@ -61,7 +61,7 @@ async function open(run: RunRecord | null = record, height = 40) { clock = new ManualClock(); onExit = vi.fn(); const wallTime = () => new Date(Date.parse("2026-11-18T10:10:00.000Z") + clock.now()); - setup = await testRender(, { + setup = await testRender(, { width: 120, height, clock, @@ -263,6 +263,25 @@ describe("Watch", () => { expect(await open({ ...record, resumes: [resume] })).toMatch(/10m 00s +since resume/); }); + it("shows the last lines that the runner printed, and ctrl+c to stop it without onExit", async () => { + const log = [ + "Agent random.", + "Seed 7.", + "", + "Run 1 of 2.", + "task 000 answered", + "task 001 answered", + ]; + const frame = await open(record, 40, { log: () => log, onExit: undefined }); + + expect(frame).toMatch( + /Seed 7\. *\n +Run 1 of 2\. *\n +task 000 answered *\n +task 001 answered/, + ); + expect(frame).not.toContain("Agent random."); + expect(frame).toContain("ctrl+c stop"); + expect(frame).not.toContain("q quit"); + }); + it("calls onExit on q", async () => { await open(); await press("q"); diff --git a/packages/cli/src/watch-view.tsx b/packages/cli/src/watch-view.tsx index bcf8f18..cd21bbb 100644 --- a/packages/cli/src/watch-view.tsx +++ b/packages/cli/src/watch-view.tsx @@ -47,8 +47,8 @@ const hints = [ ["←→", "task"], ["tab", "running"], ["f", "live"], - ["q", "quit"], ] as const; +const logLines = 4; const looks = { pending: { glyph: dot, ink: palette.border }, running: { glyph: dot, ink: palette.bright }, @@ -90,12 +90,17 @@ export async function openWatch(folders: RunFolders): Promise { /** * The props of {@link Watch}. `read` reads the run and the trajectory of the * task that a selection picks, and `wallTime` returns the time to measure - * the elapsed time against. + * the elapsed time against. `log` returns the lines that the runner has + * printed, for a view that runs the run itself. The view shows the last of + * them, and reads them again each time it draws, about once a second. + * Without `onExit`, `q` does nothing and the hints name ctrl+c to stop the + * run instead. */ export type WatchProps = { readonly read: (selection: Selection) => Promise; readonly wallTime: () => Date; - readonly onExit: () => void; + readonly onExit?: (() => void) | undefined; + readonly log?: (() => readonly string[]) | undefined; }; /** @@ -103,7 +108,7 @@ export type WatchProps = { * once when the selection changes, and it goes back to the live task when the * run on screen changes. It calls `onExit` on `q`. */ -export function Watch({ read, wallTime, onExit }: WatchProps) { +export function Watch({ read, wallTime, onExit, log = () => [] }: WatchProps) { const renderer = useRenderer(); const { width } = useTerminalDimensions(); const [selection, setSelection] = useState(live); @@ -157,7 +162,7 @@ export function Watch({ read, wallTime, onExit }: WatchProps) { } const keys: Readonly void>> = { - q: onExit, + ...(onExit === undefined ? {} : { q: onExit }), left: () => pick(nextRow(board, row, -1)), right: () => pick(nextRow(board, row, 1)), tab: () => pick(nextRunning(board, row)), @@ -178,6 +183,9 @@ export function Watch({ read, wallTime, onExit }: WatchProps) { const now = wallTime().getTime(); const task = board.find((task) => task.row === row); const running = task?.state.kind === "running"; + const logTail = log() + .filter((line) => line.trim() !== "") + .slice(-logLines); const lastTurn = frame.kind === "run" ? frame.feed.findLastIndex(({ kind }) => kind === "turn") : -1; return ( @@ -221,13 +229,25 @@ export function Watch({ read, wallTime, onExit }: WatchProps) { )} + {logTail.length === 0 ? null : ( + + {logTail.map((line, index) => ( + + {line} + + ))} + + )} {problem === undefined ? null : ( {problem} )} - +