Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/studio/development.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
- `hook/tests/answer.rs`: the answer hook's contract (Seam 4), including a blocked command's denial returned at once with no decision, a payload on stdin and the studio played by the test in a scratch `HOME`: an answer's `allow` with the questions and each label, a multi-select's joined; an approved plan's `allow` with its input and a rejected one's `deny`; a permission's `allow` or `deny`; each card answer logged as `StudioAnswer`, a terminal one not; for questions, plans and permissions alike, an immediate return with no decision when nobody watches, another session is watched, the studio's pid is dead or its heartbeat old; no decision at the timeout or for "Answer in the terminal"; a wrong nonce or an answer file others may write refused;
- the unit tests of the event log's path and of the turn the shell's `StudioInterrupted` line ends, its own and never a later one (`hook`), of what `scrub` keeps (`studio-core`), of the weekly rates' dates (`understanding`), of the shell's risk-note log lines, which keep no reply and no command, and of a worker's view joining its session's risk notes and asks and of the cards key changing with a second card of a judged command and of the server's own lines waiting for the log's lock, then landing whole (`server/src/lib.rs`) and of the settings file's reads (`server/src/settings.rs`: the writer, `briefBackfill.hours` and its older name `catchUp.hours`, `prBriefs.model`, Opus unless it says another, `prBriefs.effort`, low unless it names another claude takes or `default`, and a background run's `backgroundRuns`, `checks` and `setup`, its project's own with no default) and of the settings' write: a comment and a trailing comma beside the edited key kept byte for byte, a value replaced, a missing parent inserted, reset removing the key, a text that does not parse or a file that changed refused and left as it was, a missing file created, the file replaced whole with nothing left beside it, and a symbolic link written through and kept. and of the PR briefs (`server/src/prs.rs`: which PRs are ready from the sessions' branches and the repository's open list, a failed lookup keeping what was known; `pr_briefs.rs`: the write once per head, stale on a new head, no lock held across a write, a failed head not retried, what waits for Daniel and what a chapter lights, and which ready PRs the start writes, at most 3, the newest head first within `briefBackfill.hours`; `part_catch_ups.rs`: which fogged part gets a part catch-up and which opens its one PR's chapter, completion clearing the part's fog and completing no brief, a stored catch-up read only for the fog list it covers, and, with a fake writer, a shown map writing at most 8 fogged Core parts' catch-ups oldest fog first and none of the others, a part that is not Core written on click, the catch-up's state carrying the stage as each call starts, a new fog entry rewriting it on the next show and an unchanged one never, and two shows in a row starting one write; `ratify.rs`: the decisions file read and written back whole, what has landed; `fog.rs`: reads and the fog a push after reading files; `history.rs`: a PR's files for the drawer).

Each crate's integration tests are one test binary, `it`: its `Cargo.toml` sets `autotests = false`, and `tests/main.rs` declares each `tests/*.rs` file as a module, so a new test file needs a `mod` line there. Helpers the files share live in `tests/common.rs`; the git helpers that `hook`, `understanding` and `workbench` share (an isolated `git`, a one-commit repository, also in a folder the test names, a bare `origin`) and the `LauncherGuard` that `server`'s tests hold to stop the `plotkeeper-run` launchers a test leaves running live in the `studio-test-support` crate (`test-support/`), a dev-dependency that depends on no workspace crate. The fake writers of `understanding/tests` are `Scripted` in its `common.rs`. In `ui/tests/`, `dom.mjs` holds the fake document (`fakeDocument`) and the `invoke` stub (`tauri`).
Each crate's integration tests are one test binary, `it`: its `Cargo.toml` sets `autotests = false`, and `tests/main.rs` declares each `tests/*.rs` file as a module, so a new test file needs a `mod` line there. Helpers the files share live in `tests/common.rs`; the git helpers that `hook`, `understanding` and `workbench` share (an isolated `git`, a one-commit repository, also in a folder the test names, a bare `origin`) and the `LauncherGuard` that `server`'s tests hold to stop the `plotkeeper-run` launchers a test leaves running live in the `studio-test-support` crate (`test-support/`), a dev-dependency that depends on no workspace crate. The fake writers of `understanding/tests` are `Scripted` in its `common.rs`. In `ui/tests/`, `dom.mjs` holds the fake document (`fakeDocument`) and the `invoke` stub (`tauri`), and `delegate-fixtures.mjs` the stand-in programs and scratch checkouts the `delegate-*.test.mjs` files share.

`npm test` (after `npm ci`), or `npm run test:node`, builds the UI with `tsc`, type-checks the tests against the build's `dist/*.d.ts` (`npm run test:types`, `ui/tests/tsconfig.json`, #554) and runs `ui/tests/`: the UI's pure functions on the `tsc` output in `dist/`, such as the page's frame: the list's width and focus mode (`layout`), the page's key mapping (`keys`), what "now" holds in each state, a folded turn's line, and the story's draws keeping its turns, folds, buttons, counting times, focus and open folds the same nodes, on a stand-in that is in a page (`rail`), the evidence line (`evidence`), the banner's 14 and 15 minutes (`banner`), hover as a state machine on an injected clock (`hover`), and Ask and the diff pane (`ask`, `diff`), the agent logos and the rows that show them (`logos`), each text the layout cuts, whole in its own element and scrolled into view in place, or, shortened or very long, whole in a hint, and no `title` left (`full-text`), a listener-path hint across redraws: the trigger `patch` keeps opens the hint of its current words, a hint gains no listeners per draw and goes once its trigger is dropped, and a step's detail drops the last step's hint (`hint-redraw`), the note toast, its fade held by the pointer or the focus on an injected clock, and the unseen chip (`notes`), the what's-new toast's decision after an update (`whats-new`), the toasts in the keyboard (`focus`), the PR brief's logic (`map-brief`: a story's and a chapter's playlist, the player that reopens where it left off, the reading gate on the last story step, the checks, the story steps without the Ratify one), the tray's rows (`map-tray`), the background runs' rows, Cancel and its wait for the server's answer, the pause's indicator, the prompt a failed run copies and its note kept across a redraw (`map-runs`), back a law's rows, the patch review, Open draft PR's line and Guard with a law's confirm step (`patch-review`), Explore this's offers and the micro-world's sandboxed frame and overlay (`micro-world`), the host check's network guard on a fake and, when `PLOTKEEPER_DSH_VISUAL` names it, the real `verify.mjs` (`micro-world-guard`), the rail's row of a brief waiting (`rail-brief`) and the accept and overturn rows (`ratify`), the snap page and its fixtures (`snap`: the stub, the rail's and the recorded terminal's), and the README's limits, links and npm rewrite (`readme`), and the Map view's logic: the map file, placement, drag, links and the layered Esc, and which scrub is the newest (`map`), live agents on the parts and the not-placed line (`map-agents`), broken laws and the proof drawer (`map-drawer`), when an element counts as seen (`seen`), and the stop rules' review (`stop-rules`); opening a thing in place leaves every other node of the rail the same object, on the tests' small DOM (`dom.mjs`).

Expand Down
134 changes: 134 additions & 0 deletions ui/tests/delegate-critic.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
// `node scripts/agents/delegate.mjs pick critic`, with a stand-in `agy` first on the PATH.
// These tests run no real `agy`. Run: `npm test`.
import { test } from "node:test";
import assert from "node:assert/strict";
import { spawnSync } from "node:child_process";
import { existsSync } from "node:fs";
import { join } from "node:path";
import { pickCritic, script } from "./delegate-fixtures.mjs";

test("with both Gemini windows at their floors, pick critic runs the Gemini critic read-only and prints its findings", () => {
const ran = pickCritic({ FAKE_AGY_5H: "0.5", FAKE_AGY_WEEKLY: "0.3", FAKE_AGY_RUN: "ok" });
assert.equal(ran.status, 0, ran.stderr);
assert.equal(
ran.stdout,
"critic: agy gemini-test-model\n\n1. high, contradiction: The store reads src/store.rs:7\n Evidence: src/store.rs:1\n The file has one line.\n",
);
const critic = ran.calls.find((call) => !call.argv.includes("/usage"));
assert.ok(critic, "agy -p was not called for the critic");
const argv = critic.argv;
const after = (flag) => argv[argv.indexOf(flag) + 1];
// Not plan mode, which ends the run at the first denied command (#618): the deny hook keeps it read-only.
assert.equal(argv.includes("--mode"), false);
assert.equal(after("--model"), "gemini-test-model");
assert.equal(after("--print-timeout"), "15m");
assert.equal(after("--output-format"), "json");
assert.equal(after("--json-schema"), join(critic.cwd, "review/schema.json"));
assert.match(after("-p"), /check every claim/i);
assert.match(after("-p"), /Your only working tool is read_file/);
assert.equal(after("-p").includes("@"), false, "an at-sign may read a file past the hook");
assert.notEqual(critic.cwd, ran.repo);
assert.equal(critic.internal, "1");
// It read the draft through the hook, in a worktree of HEAD whose files tree.txt lists.
assert.deepEqual(ran.seen, [{ draft: "# A draft\n\nThe store reads `src/store.rs:7`.\n", worktree: true, tree: `${join(critic.cwd, "repo/src/store.rs")}\n` }]);
// And left no throwaway folder or worktree behind.
assert.equal(existsSync(critic.cwd), false);
assert.equal(ran.worktrees.match(/^worktree /gm).length, 1, ran.worktrees);
});

test("a Gemini critic run with no findings says so", () => {
const ran = pickCritic({ FAKE_AGY_5H: "0.9", FAKE_AGY_WEEKLY: "0.9", FAKE_AGY_RUN: "none" });
assert.equal(ran.status, 0, ran.stderr);
assert.equal(ran.stdout, "critic: agy gemini-test-model\n\nNo findings.\n");
});

// A breakdown's critic gets a brief of its own: the routing rules to hold the routes to, not only claims to check.
const BREAKDOWN = /^You are the critic of a draft ticket breakdown[\s\S]*Routing section of docs\/agents\/issue-tracker\.md[\s\S]*Risk isolation[\s\S]*CI[\s\S]*departs from the spec[\s\S]*Daniel/;

test("pick critic --breakdown gives the Gemini critic and the Claude fallback the breakdown brief, then the breakdown", () => {
const gemini = pickCritic({ FAKE_AGY_5H: "0.9", FAKE_AGY_WEEKLY: "0.9", FAKE_AGY_RUN: "ok" }, "breakdown");
assert.equal(gemini.status, 0, gemini.stderr);
const argv = gemini.calls.find((call) => !call.argv.includes("/usage")).argv;
const brief = argv[argv.indexOf("-p") + 1];
assert.match(brief, BREAKDOWN);
assert.match(brief, /The breakdown, with its spec: \S+\/review\/draft\.md/);
assert.equal(gemini.seen[0].draft, "# A draft\n\nThe store reads `src/store.rs:7`.\n");
const claude = pickCritic({ FAKE_AGY_5H: "0.49", FAKE_AGY_WEEKLY: "0.9" }, "breakdown");
assert.equal(claude.status, 0, claude.stderr);
const { head, prompt } = claudeCritic(claude.stdout);
assert.match(head, /^critic: claude opus effort high\n/);
assert.match(prompt, BREAKDOWN);
assert.match(prompt, /\n\nThe breakdown, with its spec:\n\n# A draft\n\nThe store reads `src\/store\.rs:7`\.\n$/);
assert.ok(brief.startsWith(prompt.slice(0, prompt.indexOf("\n\nThe breakdown, with its spec:"))), "the Claude critic gets the brief Gemini got");
});

for (const [what, args] of [
["neither --spec nor --breakdown", []],
["both --spec and --breakdown", ["--spec", "a.md", "--breakdown", "b.md"]],
]) {
test(`pick critic with ${what} is a usage error`, () => {
const ran = spawnSync(process.execPath, [script, "pick", "critic", ...args], { encoding: "utf8" });
assert.equal(ran.status, 2);
assert.match(ran.stderr, /one of --spec <draft> or --breakdown <draft> is required/);
});
}

const ranCritic = (calls) => calls.some((call) => !call.argv.includes("/usage"));

/** Splits what `pick critic` printed for a Claude critic into its first lines and the prompt it printed after them. */
const claudeCritic = (stdout) => {
const [head, ...rest] = stdout.split("\n\n");
return { head, prompt: rest.join("\n\n") };
};

// The prompt a Claude critic gets: the critic's brief, then the draft.
const BRIEFED = /^You are the critic of a draft spec[\s\S]*Check every claim[\s\S]*\n\nThe draft:\n\n# A draft\n\nThe store reads `src\/store\.rs:7`\.\n$/;

test("with the 5-hour window below its floor, pick critic names Opus at high and prints its prompt, without running Gemini", () => {
const ran = pickCritic({ FAKE_AGY_5H: "0.49", FAKE_AGY_WEEKLY: "0.9", FAKE_AGY_RUN: "ok" });
assert.equal(ran.status, 0, ran.stderr);
const { head, prompt } = claudeCritic(ran.stdout);
assert.equal(head, "critic: claude opus effort high\nwhy: agy gemini-test-model has 49% of its 5h window and 90% of its weekly window left, under its floors of 50% and 30%");
assert.match(prompt, BRIEFED);
assert.equal(ranCritic(ran.calls), false);
});

test("with the weekly window below its floor, pick critic names Opus at high without running Gemini", () => {
const ran = pickCritic({ FAKE_AGY_5H: "0.9", FAKE_AGY_WEEKLY: "0.29", FAKE_AGY_RUN: "ok" });
assert.equal(ran.status, 0, ran.stderr);
const { head, prompt } = claudeCritic(ran.stdout);
assert.equal(head, "critic: claude opus effort high\nwhy: agy gemini-test-model has 90% of its 5h window and 29% of its weekly window left, under its floors of 50% and 30%");
assert.match(prompt, BRIEFED);
assert.equal(ranCritic(ran.calls), false);
});

test("with no quota reading, pick critic names Opus at high without running Gemini", () => {
const ran = pickCritic({ FAKE_AGY_USAGE: "none", FAKE_AGY_RUN: "ok" });
assert.equal(ran.status, 0, ran.stderr);
const { head, prompt } = claudeCritic(ran.stdout);
assert.equal(head, "critic: claude opus effort high\nwhy: agy gemini-test-model gave no quota reading");
assert.match(prompt, BRIEFED);
assert.equal(ran.calls.length, 1);
assert.ok(ran.calls[0].argv.includes("/usage"));
});

for (const [run, said] of [
["fail", "agy gemini-test-model failed: exit 1: something broke"],
["error", "agy gemini-test-model failed: status ERROR (conversation conv-test)"],
["empty", "agy gemini-test-model answered nothing (status SUCCESS, conversation conv-test)"],
["pointer", "agy gemini-test-model answered only with a pointer to implementation_plan.md (conversation conv-test)"],
["noread", "agy gemini-test-model read no file, so it saw no draft (conversation conv-test)"],
["timeout", "agy gemini-test-model was cut at 15 minutes"],
]) {
test(`a Gemini critic run that ends with "${run}" names Opus at high as the fallback, with the brief Gemini got`, () => {
const ran = pickCritic({ FAKE_AGY_5H: "0.9", FAKE_AGY_WEEKLY: "0.9", FAKE_AGY_RUN: run });
assert.equal(ran.status, 0, ran.stderr);
const { head, prompt } = claudeCritic(ran.stdout);
assert.equal(head, `critic: claude opus effort high\nwhy: ${said}`);
assert.equal(ranCritic(ran.calls), true);
assert.match(prompt, BRIEFED);
const gemini = ran.calls.find((call) => !call.argv.includes("/usage")).argv;
const critic = prompt.slice(0, prompt.indexOf("\n\nThe draft:"));
assert.ok(gemini[gemini.indexOf("-p") + 1].startsWith(critic), "the Claude critic gets the brief Gemini got");
});
}
Loading
Loading