architecture-harness 0.17.0 → 0.18.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/cli/budget-counter.mjs +25 -4
- package/src/cli/budget-guard.mjs +79 -4
- package/src/cli/budget-guard.test.mjs +146 -0
- package/src/cli/budget.mjs +3 -2
- package/src/cli/context-signal.mjs +92 -0
- package/src/cli/context-signal.test.mjs +99 -0
- package/src/cli/dispatch-guard.mjs +9 -5
- package/src/cli/dispatch-guard.test.mjs +5 -6
- package/src/cli/dispatch.mjs +20 -2
- package/src/cli/dispatch.test.mjs +25 -1
- package/src/cli/explore-hint.mjs +67 -7
- package/src/cli/explore-hint.test.mjs +104 -4
- package/src/cli/handoff-note.mjs +69 -0
- package/src/cli/handoff-note.test.mjs +71 -0
- package/src/cli/ledger.mjs +4 -2
- package/src/cli/plugin-agents.test.mjs +9 -0
- package/src/cli/qc.mjs +6 -0
- package/src/cli/report-stop.mjs +30 -10
- package/src/cli/report-stop.test.mjs +112 -1
- package/src/cli/stop-gate.test.mjs +167 -3
- package/src/cli/task-gate.mjs +34 -12
- package/src/cli/task-gate.test.mjs +88 -2
- package/src/cli/tokens.mjs +258 -0
- package/src/cli/tokens.test.mjs +322 -0
- package/src/cli/transcript-usage.mjs +135 -0
- package/src/cli/transcript-usage.test.mjs +138 -0
- package/src/config.mjs +15 -1
|
@@ -37,7 +37,17 @@ function workspace(t) {
|
|
|
37
37
|
return { on, plain, outside, tmp, head: git(on, "rev-parse", "HEAD"), ledger: path.join(on, ".git", "qc", "ledger.jsonl") };
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
+
// A hook input as Claude Code sends it for a subagent: its own transcript path, whose file may name nothing.
|
|
41
|
+
const unread = (cwd) => {
|
|
42
|
+
const file = path.join(cwd, "unread.jsonl");
|
|
43
|
+
if (!existsSync(file)) writeFileSync(file, "");
|
|
44
|
+
return file;
|
|
45
|
+
};
|
|
46
|
+
/** The same call with no transcript path, the input the hook must say it cannot place. */
|
|
47
|
+
const bare = ({ agent_transcript_path: _, ...call }) => call;
|
|
48
|
+
|
|
40
49
|
const handback = (cwd, agentType, message, event = "PreToolUse") => ({
|
|
50
|
+
agent_transcript_path: unread(cwd),
|
|
41
51
|
session_id: "s",
|
|
42
52
|
cwd,
|
|
43
53
|
hook_event_name: event,
|
|
@@ -47,6 +57,7 @@ const handback = (cwd, agentType, message, event = "PreToolUse") => ({
|
|
|
47
57
|
agent_type: agentType,
|
|
48
58
|
});
|
|
49
59
|
const stop = (cwd, agentType, last, extra = {}) => ({
|
|
60
|
+
agent_transcript_path: unread(cwd),
|
|
50
61
|
session_id: "s",
|
|
51
62
|
cwd,
|
|
52
63
|
hook_event_name: "SubagentStop",
|
|
@@ -306,7 +317,7 @@ test("a resumed implementer keeps its worktree after another dispatch, and needs
|
|
|
306
317
|
decide(stop(ws.on, IMPLEMENTER, REPORT, { agent_id: "agent-A", ...namesWorktree(ws, "ta", a.dir) }), ws.tmp);
|
|
307
318
|
taskWorktree(ws, "wt-b", "feat/b-1");
|
|
308
319
|
const fixed = commitMore(a.dir, "fix.txt");
|
|
309
|
-
assert.equal(decide(stop(ws.on, IMPLEMENTER, REPORT, { agent_id: "agent-A" }), ws.tmp), null);
|
|
320
|
+
assert.equal(decide(bare(stop(ws.on, IMPLEMENTER, REPORT, { agent_id: "agent-A" })), ws.tmp), null);
|
|
310
321
|
const second = stopsOf(ws).at(-1);
|
|
311
322
|
assert.deepEqual([second.worktree, second.branch, second.head], [a.dir, "feat/a-1", fixed]);
|
|
312
323
|
});
|
|
@@ -430,3 +441,103 @@ test("an implementer that edits qc.config.json in its worktree is still judged",
|
|
|
430
441
|
const output = decide(stop(ws.on, IMPLEMENTER, "DONE", namesWorktree(ws, "cfg", linked)), ws.tmp);
|
|
431
442
|
assert.equal(output.decision, "block");
|
|
432
443
|
});
|
|
444
|
+
|
|
445
|
+
const NOTE = [
|
|
446
|
+
"# Hand-off: Task 3",
|
|
447
|
+
"Brief: C:/scratch/briefs/task-3.md",
|
|
448
|
+
"Worktree: C:/work/kit-85",
|
|
449
|
+
"Branch: feat/85-context",
|
|
450
|
+
"Head: 1a2b3c4",
|
|
451
|
+
"",
|
|
452
|
+
"## Done",
|
|
453
|
+
"- 1a2b3c4 the reader",
|
|
454
|
+
"",
|
|
455
|
+
"## Left",
|
|
456
|
+
"1. wire the guard",
|
|
457
|
+
"",
|
|
458
|
+
"## Next step",
|
|
459
|
+
"node --test packages/qc-harness/src/cli/budget-guard.test.mjs",
|
|
460
|
+
].join("\n");
|
|
461
|
+
|
|
462
|
+
test("an implementer that hands off reports a checked note in place of the RED and GREEN lines", (t) => {
|
|
463
|
+
const ws = workspace(t);
|
|
464
|
+
const note = path.join(ws.tmp, "handoffs", "t3-1.md");
|
|
465
|
+
mkdirSync(path.dirname(note), { recursive: true });
|
|
466
|
+
const missing = decide(handback(ws.on, "architecture:sdd-implementer", `DONE_WITH_CONCERNS\nHANDOFF: ${note}`), ws.tmp);
|
|
467
|
+
assert.match(denied(missing) ?? "", /a hand-off note at .*t3-1\.md that exists and reads \(ENOENT\)/);
|
|
468
|
+
writeFileSync(note, NOTE.replace("Head: 1a2b3c4\n", ""));
|
|
469
|
+
assert.match(denied(decide(handback(ws.on, "architecture:sdd-implementer", `DONE\nHANDOFF: ${note}`), ws.tmp)) ?? "", /a line `Head: <value>` in the hand-off note/);
|
|
470
|
+
writeFileSync(note, NOTE);
|
|
471
|
+
assert.equal(decide(handback(ws.on, "architecture:sdd-implementer", `DONE\nHANDOFF: ${note}`), ws.tmp), null);
|
|
472
|
+
const stopped = decide(stop(ws.on, "sdd-implementer", `DONE\nHANDOFF: ${note}`), ws.tmp);
|
|
473
|
+
assert.equal(stopped?.decision, undefined);
|
|
474
|
+
const [record] = readLedger(ws.ledger);
|
|
475
|
+
assert.equal(record.handoff, note);
|
|
476
|
+
});
|
|
477
|
+
|
|
478
|
+
test("the implementer definition names the HANDOFF line and the resume from a note", () => {
|
|
479
|
+
const body = readFileSync(path.join(AGENTS, "sdd-implementer.md"), "utf8");
|
|
480
|
+
assert.match(body, /`HANDOFF: <note path>`/);
|
|
481
|
+
assert.match(body, /git log --oneline <Head>\.\.HEAD/);
|
|
482
|
+
});
|
|
483
|
+
|
|
484
|
+
/** The main transcript of a session and the subagent transcript Claude Code keeps beside it, which names `worktree`. */
|
|
485
|
+
function sessionTranscripts(ws, agentId, worktree) {
|
|
486
|
+
const dir = path.join(ws.tmp, "projects", "p");
|
|
487
|
+
const main = path.join(dir, "sess.jsonl");
|
|
488
|
+
mkdirSync(path.join(dir, "sess", "subagents"), { recursive: true });
|
|
489
|
+
writeFileSync(main, "");
|
|
490
|
+
const prompt = { type: "user", message: { role: "user", content: `Read the brief.\nWorktree: ${worktree}\nGo.` } };
|
|
491
|
+
writeFileSync(path.join(dir, "sess", "subagents", `agent-${agentId}.jsonl`), `${JSON.stringify(prompt)}\n`);
|
|
492
|
+
return main;
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
test("a hand-back with no agent_transcript_path finds its worktree in the subagent transcript beside the main one", (t) => {
|
|
496
|
+
const ws = workspace(t);
|
|
497
|
+
const message = "VERDICT: APPROVED deadbee\nRED-CHECKED: x";
|
|
498
|
+
const input = { ...bare(handback(ws.on, REVIEWER, message)), transcript_path: sessionTranscripts(ws, "agent-1", ws.on) };
|
|
499
|
+
assert.match(denied(decide(input, ws.tmp)) ?? "", /deadbee names no commit/);
|
|
500
|
+
assert.doesNotMatch(denied(decide(input, ws.tmp)) ?? "", /names no transcript/);
|
|
501
|
+
});
|
|
502
|
+
|
|
503
|
+
const LOST = /the hook input names no transcript for this agent, so the checks judge against the repository of the cwd or the session/;
|
|
504
|
+
|
|
505
|
+
test("an agent_transcript_path to a missing file falls back to the subagent transcript, and says so when that is missing too", (t) => {
|
|
506
|
+
const ws = workspace(t);
|
|
507
|
+
const message = "VERDICT: APPROVED deadbee\nRED-CHECKED: x";
|
|
508
|
+
const missing = { agent_transcript_path: path.join(ws.tmp, "gone", "agent-1.jsonl") };
|
|
509
|
+
const found = { ...handback(ws.on, REVIEWER, message), ...missing, transcript_path: sessionTranscripts(ws, "agent-1", ws.on) };
|
|
510
|
+
assert.match(denied(decide(found, ws.tmp)) ?? "", /deadbee names no commit/);
|
|
511
|
+
assert.doesNotMatch(denied(decide(found, ws.tmp)) ?? "", LOST);
|
|
512
|
+
const lost = decide({ ...handback(ws.on, REVIEWER, "Looks fine."), ...missing }, ws.tmp);
|
|
513
|
+
assert.match(denied(lost) ?? "", LOST, "the file named is missing, and no other transcript exists");
|
|
514
|
+
});
|
|
515
|
+
|
|
516
|
+
test("with no transcript that finds the agent, a hand-back or a stop says what it was judged against, and keeps its decision", (t) => {
|
|
517
|
+
const ws = workspace(t);
|
|
518
|
+
const refused = decide(bare(handback(ws.on, REVIEWER, "Looks fine.")), ws.tmp);
|
|
519
|
+
assert.match(denied(refused) ?? "", /VERDICT: APPROVED <sha>/, "a deny stays a deny");
|
|
520
|
+
assert.match(denied(refused) ?? "", LOST);
|
|
521
|
+
assert.ok((denied(refused) ?? "").includes(ws.on), "the line names the repository it judged against");
|
|
522
|
+
const message = `VERDICT: APPROVED ${ws.head.slice(0, 8)}\nRED-CHECKED: the test failed before the fix`;
|
|
523
|
+
const allowed = decide(bare(handback(ws.on, REVIEWER, message)), ws.tmp);
|
|
524
|
+
assert.equal(allowed.hookSpecificOutput.permissionDecision, undefined, "an allow stays an allow");
|
|
525
|
+
assert.match(allowed.hookSpecificOutput.additionalContext, LOST);
|
|
526
|
+
const blocked = decide(bare(stop(ws.on, REVIEWER, "All good.")), ws.tmp);
|
|
527
|
+
assert.equal(blocked.decision, "block");
|
|
528
|
+
assert.match(blocked.reason, LOST);
|
|
529
|
+
const held = decide(bare(stop(ws.on, REVIEWER, "All good.", { stop_hook_active: true })), ws.tmp);
|
|
530
|
+
assert.equal(held.decision, undefined);
|
|
531
|
+
assert.match(held.systemMessage, LOST);
|
|
532
|
+
decide(bare(handback(ws.on, REVIEWER, message, "PostToolUse")), ws.tmp);
|
|
533
|
+
const passed = decide(bare(stop(ws.on, REVIEWER, "Sent.")), ws.tmp);
|
|
534
|
+
assert.equal(passed.decision, undefined);
|
|
535
|
+
assert.match(passed.systemMessage, LOST);
|
|
536
|
+
assert.deepEqual(readLedger(ws.ledger).map((record) => record.type), ["stop", "verdict"], "the note leaves the record alone");
|
|
537
|
+
});
|
|
538
|
+
|
|
539
|
+
test("the no-transcript line is left off for an agent type the workflow does not track", (t) => {
|
|
540
|
+
const ws = workspace(t);
|
|
541
|
+
assert.equal(decide(bare(stop(ws.on, "Explore", "done")), ws.tmp), null);
|
|
542
|
+
assert.equal(decide(bare(handback(ws.on, "Explore", "done")), ws.tmp), null);
|
|
543
|
+
});
|
|
@@ -44,13 +44,25 @@ function workspace(t, installed) {
|
|
|
44
44
|
stubHarness(cli, "installed", log, installed.codes, installed.withDoctor, installed.outputs);
|
|
45
45
|
}
|
|
46
46
|
const ran = () => (existsSync(log) ? readFileSync(log, "utf8").trim().split("\n") : []);
|
|
47
|
-
|
|
47
|
+
// Each workspace owns its temp folder, so the gate's memory of a block is shared with no other test.
|
|
48
|
+
const tmp = path.join(base, "tmp");
|
|
49
|
+
mkdirSync(tmp, { recursive: true });
|
|
50
|
+
const run = (input = {}, env = {}) =>
|
|
48
51
|
spawnSync("bash", [HOOK], {
|
|
49
52
|
input: JSON.stringify({ hook_event_name: "Stop", ...input }),
|
|
50
|
-
env: {
|
|
53
|
+
env: {
|
|
54
|
+
...process.env,
|
|
55
|
+
CLAUDE_PROJECT_DIR: repo,
|
|
56
|
+
CLAUDE_PLUGIN_ROOT: pluginRoot,
|
|
57
|
+
QC_STOP_EXTRA: "",
|
|
58
|
+
TMPDIR: tmp,
|
|
59
|
+
TEMP: tmp,
|
|
60
|
+
TMP: tmp,
|
|
61
|
+
...env,
|
|
62
|
+
},
|
|
51
63
|
encoding: "utf8",
|
|
52
64
|
});
|
|
53
|
-
return { ran, run };
|
|
65
|
+
return { ran, run, tmpDir: tmp, installedCli: installed ? path.join(repo, "node_modules", "architecture-harness", "src", "cli") : null };
|
|
54
66
|
}
|
|
55
67
|
|
|
56
68
|
test("an installed harness newer than the bundled one judges the repository, and the stop passes", (t) => {
|
|
@@ -111,3 +123,155 @@ test("hook drift beside a real gate failure still refuses a stop after a refused
|
|
|
111
123
|
assert.equal(result.status, 2);
|
|
112
124
|
assert.match(result.stderr, /citations: ADR-0099/);
|
|
113
125
|
});
|
|
126
|
+
|
|
127
|
+
test("a failure text with backslashes reaches stderr byte for byte", (t) => {
|
|
128
|
+
const text = String.raw`FAIL C:\Users\x\AppData\Local\Temp\tmp.x\new a \t b \U c \n d`;
|
|
129
|
+
const ws = workspace(t, { codes: { check: 1 }, withDoctor: false, outputs: { check: text } });
|
|
130
|
+
const result = ws.run();
|
|
131
|
+
assert.equal(result.status, 2);
|
|
132
|
+
assert.ok(result.stderr.includes(text), result.stderr);
|
|
133
|
+
assert.doesNotMatch(result.stderr, /printf/);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
const SESSION_FAIL = "FAIL orders citations: ADR-0099 is cited but not defined";
|
|
137
|
+
|
|
138
|
+
test("a repeat stop with the same failure ends with a system message that names it", (t) => {
|
|
139
|
+
const ws = workspace(t, { codes: { check: 1 }, withDoctor: false, outputs: { check: SESSION_FAIL } });
|
|
140
|
+
const first = ws.run({ session_id: "session-same" });
|
|
141
|
+
assert.equal(first.status, 2);
|
|
142
|
+
const repeat = ws.run({ session_id: "session-same", stop_hook_active: true });
|
|
143
|
+
assert.equal(repeat.status, 0, repeat.stderr);
|
|
144
|
+
const message = JSON.parse(repeat.stdout).systemMessage;
|
|
145
|
+
assert.match(message, /still fails/);
|
|
146
|
+
assert.ok(message.includes("ADR-0099 is cited but not defined"), message);
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
test("a repeat stop with a changed failure blocks again", (t) => {
|
|
150
|
+
const ws = workspace(t, { codes: { check: 1 }, withDoctor: false, outputs: { check: SESSION_FAIL } });
|
|
151
|
+
assert.equal(ws.run({ session_id: "session-changed" }).status, 2);
|
|
152
|
+
writeFileSync(
|
|
153
|
+
path.join(ws.installedCli, "qc.mjs"),
|
|
154
|
+
`console.error(${JSON.stringify(SESSION_FAIL.replace("0099", "0100"))}); process.exit(1);`,
|
|
155
|
+
);
|
|
156
|
+
const repeat = ws.run({ session_id: "session-changed", stop_hook_active: true });
|
|
157
|
+
assert.equal(repeat.status, 2);
|
|
158
|
+
assert.match(repeat.stderr, /ADR-0100/);
|
|
159
|
+
const third = ws.run({ session_id: "session-changed", stop_hook_active: true });
|
|
160
|
+
assert.equal(third.status, 0, third.stderr);
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
test("a repeat stop with no session id blocks", (t) => {
|
|
164
|
+
const ws = workspace(t, { codes: { check: 1 }, withDoctor: false, outputs: { check: SESSION_FAIL } });
|
|
165
|
+
assert.equal(ws.run().status, 2);
|
|
166
|
+
assert.equal(ws.run({ stop_hook_active: true }).status, 2);
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
test("a first stop with stop_hook_active set and no earlier block still blocks", (t) => {
|
|
170
|
+
const ws = workspace(t, { codes: { check: 1 }, withDoctor: false, outputs: { check: SESSION_FAIL } });
|
|
171
|
+
assert.equal(ws.run({ session_id: "session-fresh", stop_hook_active: true }).status, 2);
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
test("a green gate forgets the last block, so the same failure later blocks again", (t) => {
|
|
175
|
+
const ws = workspace(t, { codes: { check: 1 }, withDoctor: false, outputs: { check: SESSION_FAIL } });
|
|
176
|
+
assert.equal(ws.run({ session_id: "session-green" }).status, 2);
|
|
177
|
+
const qc = readFileSync(path.join(ws.installedCli, "qc.mjs"), "utf8");
|
|
178
|
+
writeFileSync(path.join(ws.installedCli, "qc.mjs"), "process.exit(0);");
|
|
179
|
+
assert.equal(ws.run({ session_id: "session-green" }).status, 0);
|
|
180
|
+
writeFileSync(path.join(ws.installedCli, "qc.mjs"), qc);
|
|
181
|
+
assert.equal(ws.run({ session_id: "session-green", stop_hook_active: true }).status, 2);
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
test("a session id with path characters cannot write outside the memory folder", (t) => {
|
|
185
|
+
const ws = workspace(t, { codes: { check: 1 }, withDoctor: false, outputs: { check: SESSION_FAIL } });
|
|
186
|
+
assert.equal(ws.run({ session_id: "../../escape" }).status, 2);
|
|
187
|
+
assert.equal(existsSync(path.join(ws.tmpDir, "..", "escape.last")), false);
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
test("the same failure at a stop that follows no refusal blocks, because only a repeat block ends", (t) => {
|
|
191
|
+
const ws = workspace(t, { codes: { check: 1 }, withDoctor: false, outputs: { check: SESSION_FAIL } });
|
|
192
|
+
assert.equal(ws.run({ session_id: "session-plain" }).status, 2);
|
|
193
|
+
assert.equal(ws.run({ session_id: "session-plain" }).status, 2);
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
test("a repeat stop names the first line of a failure that has no FAIL line", (t) => {
|
|
197
|
+
const text = ["error TS2322: Type 'string' is not assignable to type 'number'.", "src/a.ts(1,1): more"].join("\n");
|
|
198
|
+
const ws = workspace(t, { codes: { check: 1 }, withDoctor: false, outputs: { check: text } });
|
|
199
|
+
assert.equal(ws.run({ session_id: "session-bare" }).status, 2);
|
|
200
|
+
const repeat = ws.run({ session_id: "session-bare", stop_hook_active: true });
|
|
201
|
+
assert.equal(repeat.status, 0, repeat.stderr);
|
|
202
|
+
const message = JSON.parse(repeat.stdout).systemMessage;
|
|
203
|
+
assert.ok(message.includes("error TS2322"), message);
|
|
204
|
+
assert.ok(!message.includes("structure failed:"), message);
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
test("a repeat stop names the FAIL line when other output comes before it", (t) => {
|
|
208
|
+
const text = ["OK orders structure", SESSION_FAIL].join("\n");
|
|
209
|
+
const ws = workspace(t, { codes: { check: 1 }, withDoctor: false, outputs: { check: text } });
|
|
210
|
+
assert.equal(ws.run({ session_id: "session-later" }).status, 2);
|
|
211
|
+
const repeat = ws.run({ session_id: "session-later", stop_hook_active: true });
|
|
212
|
+
assert.equal(repeat.status, 0, repeat.stderr);
|
|
213
|
+
const message = JSON.parse(repeat.stdout).systemMessage;
|
|
214
|
+
assert.ok(message.includes("ADR-0099 is cited but not defined"), message);
|
|
215
|
+
assert.ok(!message.includes("OK orders"), message);
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
/** A stop gate whose only failing step is `QC_STOP_EXTRA`, which prints the file the test rewrites and fails. */
|
|
219
|
+
function extraWorkspace(t) {
|
|
220
|
+
const ws = workspace(t, { codes: {}, withDoctor: false });
|
|
221
|
+
const file = path.join(ws.tmpDir, "extra.txt");
|
|
222
|
+
const env = { QC_STOP_EXTRA: 'cat "$QC_TEST_EXTRA_FILE"; false', QC_TEST_EXTRA_FILE: file };
|
|
223
|
+
return { run: (input) => ws.run(input, env), set: (lines) => writeFileSync(file, lines.map((line) => `${line}\n`).join("")) };
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
test("a repeat stop blocks when only an early error line beyond the last 20 changed", (t) => {
|
|
227
|
+
const gate = extraWorkspace(t);
|
|
228
|
+
const tail = Array.from({ length: 24 }, (_, i) => `error TS2322: line ${i + 2}`);
|
|
229
|
+
gate.set(["error TS0001: first A", ...tail]);
|
|
230
|
+
assert.equal(gate.run({ session_id: "session-long" }).status, 2);
|
|
231
|
+
gate.set(["error TS0001: first B", ...tail]);
|
|
232
|
+
const repeat = gate.run({ session_id: "session-long", stop_hook_active: true });
|
|
233
|
+
assert.equal(repeat.status, 2, repeat.stdout);
|
|
234
|
+
assert.equal(gate.run({ session_id: "session-long", stop_hook_active: true }).status, 0, "the same text again ends the turn");
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
test("a repeat stop whose only change is a clock time and a duration ends the turn", (t) => {
|
|
238
|
+
const gate = extraWorkspace(t);
|
|
239
|
+
gate.set(["Start at 10:22:11", "FAIL orders citations: ADR-0099 is cited but not defined", "Duration 12.34s (transform 3ms)", " ✓ src/a.test.ts (3 tests) 12ms", "ℹ duration_ms 812.5"]);
|
|
240
|
+
assert.equal(gate.run({ session_id: "session-clock" }).status, 2);
|
|
241
|
+
gate.set(["Start at 10:23:45.678", "FAIL orders citations: ADR-0099 is cited but not defined", "Duration 9ms (transform 41ms)", " ✓ src/a.test.ts (3 tests) 9ms", "ℹ duration_ms 79.1"]);
|
|
242
|
+
const repeat = gate.run({ session_id: "session-clock", stop_hook_active: true });
|
|
243
|
+
assert.equal(repeat.status, 0, repeat.stderr);
|
|
244
|
+
assert.match(JSON.parse(repeat.stdout).systemMessage, /still fails/);
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
test("a repeat stop that changes a number beside a clock time and a duration still blocks", (t) => {
|
|
248
|
+
const gate = extraWorkspace(t);
|
|
249
|
+
gate.set(["Start at 10:22:11", "FAIL ADR-0099 is cited but not defined", "Duration 12.34s"]);
|
|
250
|
+
assert.equal(gate.run({ session_id: "session-digit" }).status, 2);
|
|
251
|
+
gate.set(["Start at 10:23:45", "FAIL ADR-0100 is cited but not defined", "Duration 1.5s"]);
|
|
252
|
+
assert.equal(gate.run({ session_id: "session-digit", stop_hook_active: true }).status, 2);
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
test("a repeat stop whose only change is a duration inside an assertion message still blocks", (t) => {
|
|
256
|
+
const gate = extraWorkspace(t);
|
|
257
|
+
gate.set([" ✓ src/a.test.ts (3 tests) 12ms", "AssertionError: expected 200ms, received 400ms"]);
|
|
258
|
+
assert.equal(gate.run({ session_id: "session-assert-ms" }).status, 2);
|
|
259
|
+
gate.set([" ✓ src/a.test.ts (3 tests) 9ms", "AssertionError: expected 200ms, received 100ms"]);
|
|
260
|
+
assert.equal(gate.run({ session_id: "session-assert-ms", stop_hook_active: true }).status, 2);
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
test("a repeat stop whose only change is a clock value inside an assertion message still blocks", (t) => {
|
|
264
|
+
const gate = extraWorkspace(t);
|
|
265
|
+
gate.set(["Start at 10:22:11", "AssertionError: expected 10:00:00, received 10:22:12"]);
|
|
266
|
+
assert.equal(gate.run({ session_id: "session-assert-clock" }).status, 2);
|
|
267
|
+
gate.set(["Start at 10:23:45", "AssertionError: expected 10:00:00, received 10:22:13"]);
|
|
268
|
+
assert.equal(gate.run({ session_id: "session-assert-clock", stop_hook_active: true }).status, 2);
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
test("the memory file holds a hash of the failure text, not the text", (t) => {
|
|
272
|
+
const ws = workspace(t, { codes: { check: 1 }, withDoctor: false, outputs: { check: SESSION_FAIL } });
|
|
273
|
+
assert.equal(ws.run({ session_id: "session-hash" }).status, 2);
|
|
274
|
+
const stored = readFileSync(path.join(ws.tmpDir, "qc-stop-gate", "session-hash.last"), "utf8");
|
|
275
|
+
assert.match(stored.trim(), /^[0-9a-f]{64}$/);
|
|
276
|
+
assert.ok(!stored.includes("ADR-0099"));
|
|
277
|
+
});
|
package/src/cli/task-gate.mjs
CHANGED
|
@@ -5,6 +5,7 @@ import os from "node:os";
|
|
|
5
5
|
import path from "node:path";
|
|
6
6
|
import { checkoutRootOf } from "./checkout-root.mjs";
|
|
7
7
|
import { typeOf } from "./dispatch.mjs";
|
|
8
|
+
import { handoffPathOf, resolveHandoff } from "./handoff-note.mjs";
|
|
8
9
|
import { branchAt, gitOut, linkedWorktrees, pathKey } from "./git-read.mjs";
|
|
9
10
|
import { appendRecord, latestVerdictFor, latestVerdictOn, readLedger, shaMatches } from "./ledger.mjs";
|
|
10
11
|
import { resolveNamedWorktree, workflowOfRepo, worktreeNamed } from "./workflow-place.mjs";
|
|
@@ -26,15 +27,16 @@ const fixRoundRefusal = (branch, sha, type) =>
|
|
|
26
27
|
const unreviewedRefusal = (branch, stopHead, head) =>
|
|
27
28
|
`Task gate: an implementer stopped on ${branch} at ${short(stopHead)}, and no review in the ledger names ${short(stopHead)} or a later commit. ` +
|
|
28
29
|
`Dispatch the task reviewer on ${short(head)} first; its first line \`VERDICT: <APPROVED|CHANGES_REQUIRED> <sha>\` records the review. ` +
|
|
30
|
+
`If that stop handed off, its report named \`HANDOFF: <note path>\`; the same line in the next implementer prompt continues the task. ` +
|
|
29
31
|
`\`qc ledger ${branch}\` lists what is recorded.`;
|
|
30
32
|
|
|
31
33
|
const HISTORY_LIMIT = 5000;
|
|
32
34
|
|
|
33
35
|
/**
|
|
34
|
-
* The
|
|
36
|
+
* The newest implementer stop in this branch's history that no later verdict on the branch covers,
|
|
35
37
|
* or null. Older stops are ancestors of it, so its verdict covers them. Two git calls at most; a git error passes.
|
|
36
38
|
*/
|
|
37
|
-
function
|
|
39
|
+
function unreviewedImplementerStop({ records, branch, head, cwd, git }) {
|
|
38
40
|
if (cwd === undefined) return null;
|
|
39
41
|
const history = git(cwd, "rev-list", `--max-count=${HISTORY_LIMIT}`, head);
|
|
40
42
|
if (history === null) return null;
|
|
@@ -44,28 +46,45 @@ function unreviewedImplementerHead({ records, branch, head, cwd, git }) {
|
|
|
44
46
|
record.type === "stop" && record.role === "implementer" && record.branch === branch && typeof record.head === "string" && inHistory.has(record.head.toLowerCase()),
|
|
45
47
|
);
|
|
46
48
|
if (stopAt < 0) return null;
|
|
47
|
-
const
|
|
49
|
+
const stop = records[stopAt];
|
|
50
|
+
const stopHead = stop.head;
|
|
48
51
|
// A commit is reviewed once, wherever and whenever the verdict on it was written.
|
|
49
52
|
if (records.some((record) => record.type === "verdict" && shaMatches(record.sha, stopHead))) return null;
|
|
50
53
|
const later = records.slice(stopAt + 1).filter((record) => record.type === "verdict" && record.branch === branch && typeof record.sha === "string");
|
|
51
|
-
if (later.length === 0 || shaMatches(stopHead, head)) return
|
|
54
|
+
if (later.length === 0 || shaMatches(stopHead, head)) return stop;
|
|
52
55
|
const below = git(cwd, "rev-list", "--ancestry-path", `${stopHead}..${head}`);
|
|
53
56
|
if (below === null) return null;
|
|
54
57
|
const descendants = below.split(/\r?\n/).filter(Boolean);
|
|
55
|
-
return later.some((verdict) => descendants.some((sha) => shaMatches(verdict.sha, sha))) ? null :
|
|
58
|
+
return later.some((verdict) => descendants.some((sha) => shaMatches(verdict.sha, sha))) ? null : stop;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* The newest implementer stop on `branch` when it left the note `handoff` and no verdict came after it, else null.
|
|
63
|
+
* A later verdict on the branch, or one that names the stop's head, reviewed the stop, so its note is spent.
|
|
64
|
+
*/
|
|
65
|
+
export function continuedStop(records, branch, handoff) {
|
|
66
|
+
if (handoff === null) return null;
|
|
67
|
+
const stopAt = records.findLastIndex((record) => record.type === "stop" && record.role === "implementer" && record.branch === branch);
|
|
68
|
+
const stop = records[stopAt];
|
|
69
|
+
if (!stop || typeof stop.handoff !== "string" || pathKey(stop.handoff) !== pathKey(handoff)) return null;
|
|
70
|
+
const reviewed = records.slice(stopAt + 1).some((record) => record.type === "verdict" && (record.branch === branch || shaMatches(record.sha, stop.head)));
|
|
71
|
+
return reviewed ? null : stop;
|
|
56
72
|
}
|
|
57
73
|
|
|
58
74
|
/**
|
|
59
75
|
* @returns the reason an implementer may not start on `branch` now, or null. Only implementer commits need a
|
|
60
76
|
* review, so a controller commit after a reviewed head passes. `cwd` is any checkout; `git` reads it.
|
|
61
77
|
*/
|
|
62
|
-
export function gateRefusal({ records, branch, head, reason, type, cwd, git = gitOut }) {
|
|
78
|
+
export function gateRefusal({ records, branch, head, reason, type, cwd, git = gitOut, handoff = null }) {
|
|
79
|
+
// A continuation of the newest stop's hand-off is the same task, so it is neither a fix round nor a new task.
|
|
80
|
+
const continued = continuedStop(records, branch, handoff);
|
|
81
|
+
const why = reason ?? (continued ? `continues the hand-off ${continued.handoff}` : null);
|
|
63
82
|
const last = latestVerdictOn(records, branch);
|
|
64
|
-
if (last?.verdict === "CHANGES_REQUIRED" &&
|
|
65
|
-
const unreviewed =
|
|
66
|
-
if (unreviewed) return unreviewedRefusal(branch, unreviewed, head);
|
|
83
|
+
if (last?.verdict === "CHANGES_REQUIRED" && why === null) return fixRoundRefusal(branch, last.sha, type);
|
|
84
|
+
const unreviewed = unreviewedImplementerStop({ records, branch, head, cwd, git });
|
|
85
|
+
if (unreviewed && unreviewed !== continued) return unreviewedRefusal(branch, unreviewed.head, head);
|
|
67
86
|
const current = latestVerdictFor(records, head, ["task", "branch"]);
|
|
68
|
-
if (current?.verdict === "CHANGES_REQUIRED" &&
|
|
87
|
+
if (current?.verdict === "CHANGES_REQUIRED" && why === null) return fixRoundRefusal(branch, current.sha, type);
|
|
69
88
|
return null;
|
|
70
89
|
}
|
|
71
90
|
|
|
@@ -140,9 +159,12 @@ export function judgeTask(call, tmp = os.tmpdir()) {
|
|
|
140
159
|
return pass(null, null, `Task gate could not read the ledger (${error.message}), so this dispatch is not judged.`);
|
|
141
160
|
}
|
|
142
161
|
const reason = noResumeReason(prompt);
|
|
143
|
-
const
|
|
162
|
+
const spelled = handoffPathOf(prompt);
|
|
163
|
+
const handoff = spelled === null ? null : resolveHandoff(spelled, cwd);
|
|
164
|
+
const refusal = gateRefusal({ records, branch: worktree.branch, head: worktree.head, reason, type, cwd: workflow.root, handoff });
|
|
144
165
|
if (refusal) return refuse(refusal);
|
|
145
|
-
|
|
166
|
+
const continues = handoff ? { handoff } : {};
|
|
167
|
+
return pass(workflow, { ...base, task: true, worktree: worktree.path, branch: worktree.branch, head: worktree.head, resumeReason: reason, ...continues });
|
|
146
168
|
}
|
|
147
169
|
|
|
148
170
|
/** Writes the dispatch record of a judged call. @returns a note for the session, or null. */
|
|
@@ -7,7 +7,7 @@ import { test } from "node:test";
|
|
|
7
7
|
import { decide as dispatchDecide } from "./dispatch-guard.mjs";
|
|
8
8
|
import { appendRecord, readLedger } from "./ledger.mjs";
|
|
9
9
|
import { gitOut } from "./git-read.mjs";
|
|
10
|
-
import { gateRefusal, judgeTask, noResumeReason, recordDispatch, worktreeNamed } from "./task-gate.mjs";
|
|
10
|
+
import { continuedStop, gateRefusal, judgeTask, noResumeReason, recordDispatch, worktreeNamed } from "./task-gate.mjs";
|
|
11
11
|
import { sessionDirOf } from "./workflow-settings.mjs";
|
|
12
12
|
|
|
13
13
|
const git = (cwd, ...args) => execFileSync("git", args, { cwd, stdio: "pipe", encoding: "utf8" }).trim();
|
|
@@ -52,7 +52,7 @@ const dispatch = (cwd, prompt, subagent_type = "architecture:sdd-implementer", e
|
|
|
52
52
|
...extra,
|
|
53
53
|
});
|
|
54
54
|
const taskPrompt = (worktree, more = "") => `Read the brief at C:/scratch/brief.md.\nWorktree: ${worktree}\n${more}`;
|
|
55
|
-
const implementerStop = (ws, head, branch = "feat/1-x") =>
|
|
55
|
+
const implementerStop = (ws, head, branch = "feat/1-x", handoff = null) =>
|
|
56
56
|
appendRecord(ws.ledger, {
|
|
57
57
|
type: "stop",
|
|
58
58
|
session: "s",
|
|
@@ -61,6 +61,7 @@ const implementerStop = (ws, head, branch = "feat/1-x") =>
|
|
|
61
61
|
role: "implementer",
|
|
62
62
|
branch,
|
|
63
63
|
head,
|
|
64
|
+
...(handoff ? { handoff } : {}),
|
|
64
65
|
});
|
|
65
66
|
const verdict = (ws, sha, value = "APPROVED", kind = "task") =>
|
|
66
67
|
appendRecord(ws.ledger, { type: "verdict", kind, verdict: value, sha, branch: "feat/1-x" });
|
|
@@ -419,3 +420,88 @@ test("an implementer that edits qc.config.json in its worktree cannot turn its o
|
|
|
419
420
|
implementerStop(ws, commitIn(ws.linked, "b.txt"));
|
|
420
421
|
assert.match(refusalOf(ws) ?? "", /no review in the ledger/);
|
|
421
422
|
});
|
|
423
|
+
|
|
424
|
+
test("a fresh implementer with the HANDOFF line of the newest stop continues it without a review first", (t) => {
|
|
425
|
+
const ws = workspace(t);
|
|
426
|
+
const note = path.join(ws.tmp, "handoffs", "t1-1.md");
|
|
427
|
+
const moved = commitIn(ws.linked, "b.txt");
|
|
428
|
+
implementerStop(ws, moved, "feat/1-x", note);
|
|
429
|
+
assert.match(refusalOf(ws) ?? "", /no review in the ledger names/);
|
|
430
|
+
assert.match(refusalOf(ws) ?? "", /`HANDOFF: <note path>`/);
|
|
431
|
+
assert.equal(refusalOf(ws, `HANDOFF: ${note}`), null);
|
|
432
|
+
const { record } = judgeTask(dispatch(ws.main, taskPrompt(ws.linked, `HANDOFF: ${note}`)), ws.tmp);
|
|
433
|
+
assert.equal(record.handoff, note);
|
|
434
|
+
});
|
|
435
|
+
|
|
436
|
+
test("the continuation's own stop needs a review, and a review of the later head covers both", (t) => {
|
|
437
|
+
const ws = workspace(t);
|
|
438
|
+
const note = path.join(ws.tmp, "handoffs", "t1-1.md");
|
|
439
|
+
implementerStop(ws, commitIn(ws.linked, "b.txt"), "feat/1-x", note);
|
|
440
|
+
const later = commitIn(ws.linked, "c.txt");
|
|
441
|
+
implementerStop(ws, later);
|
|
442
|
+
assert.match(refusalOf(ws, `HANDOFF: ${note}`) ?? "", /no review in the ledger names/);
|
|
443
|
+
verdict(ws, later);
|
|
444
|
+
assert.equal(refusalOf(ws), null);
|
|
445
|
+
});
|
|
446
|
+
|
|
447
|
+
test("a HANDOFF line of an older stop, of another branch, or of another note does not excuse a review", (t) => {
|
|
448
|
+
const ws = workspace(t);
|
|
449
|
+
const old = path.join(ws.tmp, "handoffs", "old.md");
|
|
450
|
+
implementerStop(ws, commitIn(ws.linked, "b.txt"), "feat/1-x", old);
|
|
451
|
+
implementerStop(ws, commitIn(ws.linked, "c.txt"));
|
|
452
|
+
assert.match(refusalOf(ws, `HANDOFF: ${old}`) ?? "", /no review in the ledger names/);
|
|
453
|
+
assert.match(refusalOf(ws, `HANDOFF: ${path.join(ws.tmp, "handoffs", "other.md")}`) ?? "", /no review in the ledger names/);
|
|
454
|
+
assert.equal(continuedStop(readLedger(ws.ledger), "feat/2-y", old), null);
|
|
455
|
+
});
|
|
456
|
+
|
|
457
|
+
test("the newest stop of another branch with the named note is not a continuation of this branch", (t) => {
|
|
458
|
+
const ws = workspace(t);
|
|
459
|
+
const note = path.join(ws.tmp, "handoffs", "t1-1.md");
|
|
460
|
+
implementerStop(ws, commitIn(ws.linked, "b.txt"));
|
|
461
|
+
implementerStop(ws, "abc1234", "feat/2-y", note);
|
|
462
|
+
assert.equal(continuedStop(readLedger(ws.ledger), "feat/1-x", note), null);
|
|
463
|
+
assert.equal(continuedStop(readLedger(ws.ledger), "feat/2-y", note)?.branch, "feat/2-y");
|
|
464
|
+
});
|
|
465
|
+
|
|
466
|
+
test("after CHANGES_REQUIRED, a continuation of the fix round's hand-off needs no NO-RESUME line", (t) => {
|
|
467
|
+
const ws = workspace(t);
|
|
468
|
+
const reviewed = commitIn(ws.linked, "b.txt");
|
|
469
|
+
implementerStop(ws, reviewed);
|
|
470
|
+
verdict(ws, reviewed, "CHANGES_REQUIRED");
|
|
471
|
+
const note = path.join(ws.tmp, "handoffs", "fix-1.md");
|
|
472
|
+
const partial = commitIn(ws.linked, "c.txt");
|
|
473
|
+
implementerStop(ws, partial, "feat/1-x", note);
|
|
474
|
+
assert.match(refusalOf(ws) ?? "", /CHANGES_REQUIRED|no review/);
|
|
475
|
+
assert.equal(refusalOf(ws, `HANDOFF: ${note}`), null);
|
|
476
|
+
});
|
|
477
|
+
|
|
478
|
+
test("a stop that a later CHANGES_REQUIRED reviewed is no longer continued by its HANDOFF line", (t) => {
|
|
479
|
+
const ws = workspace(t);
|
|
480
|
+
const note = path.join(ws.tmp, "handoffs", "t1-1.md");
|
|
481
|
+
const stopped = commitIn(ws.linked, "b.txt");
|
|
482
|
+
implementerStop(ws, stopped, "feat/1-x", note);
|
|
483
|
+
assert.equal(refusalOf(ws, `HANDOFF: ${note}`), null);
|
|
484
|
+
verdict(ws, stopped, "CHANGES_REQUIRED");
|
|
485
|
+
assert.equal(continuedStop(readLedger(ws.ledger), "feat/1-x", note), null);
|
|
486
|
+
assert.match(refusalOf(ws, `HANDOFF: ${note}`) ?? "", /CHANGES_REQUIRED/);
|
|
487
|
+
assert.equal(refusalOf(ws, `HANDOFF: ${note}\nNO-RESUME: the budget is spent`), null);
|
|
488
|
+
});
|
|
489
|
+
|
|
490
|
+
test("a later verdict that names the stop's head ends the continuation, whatever branch it records", (t) => {
|
|
491
|
+
const ws = workspace(t);
|
|
492
|
+
const note = path.join(ws.tmp, "handoffs", "t1-1.md");
|
|
493
|
+
const stopped = commitIn(ws.linked, "b.txt");
|
|
494
|
+
implementerStop(ws, stopped, "feat/1-x", note);
|
|
495
|
+
appendRecord(ws.ledger, { type: "verdict", kind: "task", verdict: "CHANGES_REQUIRED", sha: stopped });
|
|
496
|
+
assert.equal(continuedStop(readLedger(ws.ledger), "feat/1-x", note), null);
|
|
497
|
+
assert.match(refusalOf(ws, `HANDOFF: ${note}`) ?? "", /CHANGES_REQUIRED/);
|
|
498
|
+
});
|
|
499
|
+
|
|
500
|
+
test("a later verdict on the branch ends the continuation, whatever commit it names", (t) => {
|
|
501
|
+
const ws = workspace(t);
|
|
502
|
+
const note = path.join(ws.tmp, "handoffs", "t1-1.md");
|
|
503
|
+
implementerStop(ws, commitIn(ws.linked, "b.txt"), "feat/1-x", note);
|
|
504
|
+
verdict(ws, "abc1234", "CHANGES_REQUIRED");
|
|
505
|
+
assert.equal(continuedStop(readLedger(ws.ledger), "feat/1-x", note), null);
|
|
506
|
+
assert.match(refusalOf(ws, `HANDOFF: ${note}`) ?? "", /CHANGES_REQUIRED/);
|
|
507
|
+
});
|