architecture-harness 0.21.2 → 0.23.0
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 +16 -4
- package/src/cli/check.mjs +61 -0
- package/src/cli/child-run.mjs +39 -0
- package/src/cli/child-run.test.mjs +16 -0
- package/src/cli/ci-template.test.mjs +28 -0
- package/src/cli/cli.test.mjs +6 -1
- package/src/cli/codex-implement.mjs +79 -5
- package/src/cli/codex-implement.test.mjs +170 -4
- package/src/cli/dispatch-guard.mjs +24 -1
- package/src/cli/dispatch-guard.test.mjs +30 -0
- package/src/cli/dispatch-reminder.mjs +7 -4
- package/src/cli/dispatch-reminder.test.mjs +8 -4
- package/src/cli/dispatch-slot.mjs +12 -0
- package/src/cli/gate-slot.mjs +151 -0
- package/src/cli/gate-slot.test.mjs +90 -0
- package/src/cli/git-hooks.test.mjs +5 -0
- package/src/cli/integrate.mjs +122 -0
- package/src/cli/integrate.test.mjs +94 -0
- package/src/cli/ledger.mjs +25 -3
- package/src/cli/ledger.test.mjs +9 -0
- package/src/cli/merge-guard.mjs +13 -3
- package/src/cli/merge-guard.test.mjs +24 -1
- package/src/cli/migrations.mjs +127 -0
- package/src/cli/migrations.test.mjs +94 -0
- package/src/cli/plugin-agents.test.mjs +35 -8
- package/src/cli/qc.mjs +58 -1
- package/src/cli/report-stop.mjs +3 -0
- package/src/cli/report-stop.test.mjs +8 -0
- package/src/cli/retry-failed.mjs +80 -0
- package/src/cli/retry-failed.test.mjs +51 -0
- package/src/cli/review-gate.mjs +16 -7
- package/src/cli/screens-discovery.mjs +60 -0
- package/src/cli/screens.mjs +84 -0
- package/src/cli/slot-guard.mjs +43 -0
- package/src/cli/slot-guard.test.mjs +28 -0
- package/src/cli/stack.mjs +14 -0
- package/src/cli/task-gate-review.test.mjs +10 -0
- package/src/cli/task-gate.mjs +39 -7
- package/src/cli/task-gate.test.mjs +45 -0
- package/src/cli/throwaway-db.mjs +127 -0
- package/src/cli/throwaway-db.test.mjs +83 -0
- package/src/cli/ui-reuse-hint.mjs +89 -0
- package/src/cli/ui-reuse-hint.test.mjs +60 -0
- package/src/cli/ui.mjs +181 -0
- package/src/cli/ui.test.mjs +137 -0
- package/src/cli/watch.mjs +211 -0
- package/src/cli/watch.test.mjs +246 -0
- package/src/config.mjs +27 -2
- package/src/descriptions.mjs +3 -0
- package/src/gates/contract-keys.mjs +33 -0
- package/src/gates/contract-keys.test.mjs +38 -0
- package/src/gates/migration-numbers.mjs +1 -1
- package/src/gates/migration-numbers.test.mjs +1 -0
- package/src/gates/ui-catalog.mjs +45 -0
- package/src/gates/ui-catalog.test.mjs +48 -0
- package/src/gates/ui-dupes.mjs +36 -0
- package/src/gates/ui-dupes.test.mjs +29 -0
- package/src/screens/auth-context.test.mjs +41 -0
- package/src/screens/auth-run.test.mjs +144 -0
- package/src/screens/auth.mjs +223 -0
- package/src/screens/auth.test.mjs +76 -0
- package/src/screens/capture-idle.test.mjs +26 -0
- package/src/screens/capture.browser-test.mjs +73 -0
- package/src/screens/capture.mjs +175 -0
- package/src/screens/capture.test.mjs +28 -0
- package/src/screens/catalog.browser-test.mjs +32 -0
- package/src/screens/catalog.mjs +72 -0
- package/src/screens/catalog.test.mjs +36 -0
- package/src/screens/compare.mjs +40 -0
- package/src/screens/compare.test.mjs +30 -0
- package/src/screens/discovery.browser-test.mjs +29 -0
- package/src/screens/discovery.mjs +158 -0
- package/src/screens/discovery.test.mjs +27 -0
- package/src/screens/inventory.mjs +69 -0
- package/src/screens/inventory.test.mjs +10 -0
- package/src/screens/map.mjs +63 -0
- package/src/screens/map.test.mjs +35 -0
- package/src/screens/open.browser-test.mjs +50 -0
- package/src/screens/regions.mjs +137 -0
- package/src/screens/regions.test.mjs +38 -0
- package/src/screens/session-files.mjs +39 -0
- package/src/screens/session-files.test.mjs +39 -0
- package/src/screens/setup.browser-test.mjs +49 -0
- package/src/screens/setup.mjs +78 -0
- package/src/screens/stack-auth.browser-test.mjs +30 -0
- package/src/screens/stack-auth.mjs +106 -0
- package/src/screens/stack-auth.test.mjs +65 -0
- package/src/screens/write-guard.mjs +26 -0
- package/src/stack/stack.mjs +131 -0
- package/src/stack/stack.test.mjs +40 -0
- package/src/stack/supervisor.mjs +30 -0
- package/src/ui/catalog.mjs +87 -0
- package/src/ui/catalog.test.mjs +54 -0
- package/src/ui/class-sites.mjs +151 -0
- package/src/ui/class-sites.test.mjs +41 -0
- package/src/ui/codemod.mjs +138 -0
- package/src/ui/codemod.test.mjs +140 -0
- package/src/ui/components.mjs +176 -0
- package/src/ui/components.test.mjs +87 -0
- package/src/ui/dupes.mjs +141 -0
- package/src/ui/dupes.test.mjs +85 -0
- package/src/ui/find.mjs +45 -0
- package/src/ui/find.test.mjs +48 -0
- package/src/ui/options.mjs +40 -0
- package/src/ui/scan.mjs +78 -0
- package/src/ui/typescript.mjs +29 -0
- package/templates/github/workflows/ci.yml +54 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "architecture-harness",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.23.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Architecture gates, lint rules and scaffolding for the bounded-vertical-slice architecture",
|
|
6
6
|
"license": "MIT",
|
|
@@ -39,8 +39,10 @@
|
|
|
39
39
|
"./eslint": "./src/eslint/index.mjs",
|
|
40
40
|
"./preset": "./src/eslint/preset.mjs",
|
|
41
41
|
"./config": "./src/config.mjs",
|
|
42
|
+
"./screens": "./src/screens/catalog.mjs",
|
|
42
43
|
"./gates/*": "./src/gates/*.mjs",
|
|
43
|
-
"./package.json": "./package.json"
|
|
44
|
+
"./package.json": "./package.json",
|
|
45
|
+
"./ui/*": "./src/ui/*.mjs"
|
|
44
46
|
},
|
|
45
47
|
"files": [
|
|
46
48
|
"src",
|
|
@@ -48,7 +50,8 @@
|
|
|
48
50
|
"README.md"
|
|
49
51
|
],
|
|
50
52
|
"scripts": {
|
|
51
|
-
"test": "node src/eslint/rules.test.mjs && node --test \"src/**/*.test.mjs\""
|
|
53
|
+
"test": "node src/eslint/rules.test.mjs && node --test \"src/**/*.test.mjs\"",
|
|
54
|
+
"test:browser": "node --test \"src/**/*.browser-test.mjs\""
|
|
52
55
|
},
|
|
53
56
|
"peerDependencies": {
|
|
54
57
|
"@typescript-eslint/eslint-plugin": ">=8",
|
|
@@ -58,7 +61,8 @@
|
|
|
58
61
|
"eslint-plugin-sonarjs": ">=3",
|
|
59
62
|
"@commitlint/lint": ">=19",
|
|
60
63
|
"@commitlint/config-conventional": ">=19",
|
|
61
|
-
"yaml": ">=2"
|
|
64
|
+
"yaml": ">=2",
|
|
65
|
+
"typescript": ">=5"
|
|
62
66
|
},
|
|
63
67
|
"peerDependenciesMeta": {
|
|
64
68
|
"@typescript-eslint/eslint-plugin": {
|
|
@@ -81,6 +85,9 @@
|
|
|
81
85
|
},
|
|
82
86
|
"yaml": {
|
|
83
87
|
"optional": true
|
|
88
|
+
},
|
|
89
|
+
"typescript": {
|
|
90
|
+
"optional": true
|
|
84
91
|
}
|
|
85
92
|
},
|
|
86
93
|
"devDependencies": {
|
|
@@ -93,5 +100,10 @@
|
|
|
93
100
|
"eslint-plugin-sonarjs": "^3",
|
|
94
101
|
"typescript": "^5",
|
|
95
102
|
"yaml": "^2.9.1"
|
|
103
|
+
},
|
|
104
|
+
"dependencies": {
|
|
105
|
+
"pixelmatch": "^8.0.0",
|
|
106
|
+
"playwright": "^1.64.1",
|
|
107
|
+
"pngjs": "^7.0.0"
|
|
96
108
|
}
|
|
97
109
|
}
|
package/src/cli/check.mjs
CHANGED
|
@@ -55,10 +55,15 @@ import { checkDocClaims, parseDocClaims } from "../gates/doc-claims.mjs";
|
|
|
55
55
|
import { checkContractIdempotency, checkLedgerGrowth } from "../gates/contract-idempotency.mjs";
|
|
56
56
|
import { contractHistory } from "./contract-history.mjs";
|
|
57
57
|
import { contractOperations } from "../gates/contract-operations.mjs";
|
|
58
|
+
import { checkContractKeys } from "../gates/contract-keys.mjs";
|
|
58
59
|
import { checkContractRoutes, declaredTriggerRoutes } from "../gates/contract-routes.mjs";
|
|
59
60
|
import { ratchetInputs } from "./registry-history.mjs";
|
|
60
61
|
import { hookDrift } from "./git-hooks.mjs";
|
|
61
62
|
import { repoFiles } from "./repo-files.mjs";
|
|
63
|
+
import { checkUiCatalog } from "../gates/ui-catalog.mjs";
|
|
64
|
+
import { checkUiDupes } from "../gates/ui-dupes.mjs";
|
|
65
|
+
import { findDupes } from "../ui/dupes.mjs";
|
|
66
|
+
import { CATALOG_COMMAND, catalogText, scanComponents } from "../ui/scan.mjs";
|
|
62
67
|
|
|
63
68
|
// Each skip is tested on "/" + the relative path, so it reads a path the way it read a full one.
|
|
64
69
|
const SKIP = /node_modules|\/dist\/|\.test\.[cm]?[jt]sx?$/;
|
|
@@ -499,6 +504,24 @@ async function docClaims(config, lines) {
|
|
|
499
504
|
return problems;
|
|
500
505
|
}
|
|
501
506
|
|
|
507
|
+
/** No key of the composed contract ends with a dot or holds no value. */
|
|
508
|
+
async function contractKeys(config, lines) {
|
|
509
|
+
const { contract } = config;
|
|
510
|
+
const text = await read(path.join(config.root, contract));
|
|
511
|
+
if (text === null) return [];
|
|
512
|
+
let document;
|
|
513
|
+
try {
|
|
514
|
+
const { parse } = await import("yaml");
|
|
515
|
+
document = parse(text);
|
|
516
|
+
} catch (error) {
|
|
517
|
+
const rule = error?.code === "ERR_MODULE_NOT_FOUND" ? "contract-reader-unavailable" : "unreadable-contract";
|
|
518
|
+
return [{ path: contract, rule, detail: error.message }];
|
|
519
|
+
}
|
|
520
|
+
const problems = checkContractKeys(document, contract);
|
|
521
|
+
if (problems.length === 0) lines.push("OK contract no key ends with a dot, and every key holds a value");
|
|
522
|
+
return problems;
|
|
523
|
+
}
|
|
524
|
+
|
|
502
525
|
/** Every write of the composed contract names its idempotency key, or sits in the ledger. */
|
|
503
526
|
async function contractIdempotency(config, lines) {
|
|
504
527
|
const { contract } = config;
|
|
@@ -555,6 +578,36 @@ async function contractRouteAgreement(config, routes, lines) {
|
|
|
555
578
|
return problems;
|
|
556
579
|
}
|
|
557
580
|
|
|
581
|
+
/** The UI gates: the catalog and its documentation, and the duplicates. A repository with no component under its roots passes both. */
|
|
582
|
+
async function uiAgreement(config, tree, lines) {
|
|
583
|
+
let scan;
|
|
584
|
+
try {
|
|
585
|
+
scan = await scanComponents(config, tree.files);
|
|
586
|
+
} catch (cause) {
|
|
587
|
+
return [{ path: "qc.config.json", rule: "ui-no-typescript", detail: cause.message }];
|
|
588
|
+
}
|
|
589
|
+
const { options, files, entries, ts } = scan;
|
|
590
|
+
const baselineOf = async (file, key) => parsedJson(await read(path.join(config.root, file)))?.[key] ?? null;
|
|
591
|
+
const problems = [];
|
|
592
|
+
if (enabled(config.gates, "ui-catalog")) {
|
|
593
|
+
const actual = await read(path.join(config.root, options.catalog));
|
|
594
|
+
problems.push(
|
|
595
|
+
...checkUiCatalog(entries, {
|
|
596
|
+
catalog: { path: options.catalog, actual, expected: catalogText(entries, options), command: CATALOG_COMMAND },
|
|
597
|
+
baseline: await baselineOf(options.catalogBaseline, "undocumented"),
|
|
598
|
+
baselineFile: options.catalogBaseline,
|
|
599
|
+
}),
|
|
600
|
+
);
|
|
601
|
+
lines.push(`OK ui-catalog ${entries.length} component(s), each described, and ${options.catalog} matches the code`);
|
|
602
|
+
}
|
|
603
|
+
if (enabled(config.gates, "ui-dupes")) {
|
|
604
|
+
const found = ts ? findDupes(ts, files, { entries, callees: options.classCallees, ...options.dupes }) : [];
|
|
605
|
+
problems.push(...checkUiDupes(found, { baseline: await baselineOf(options.dupes.baseline, "findings"), baselineFile: options.dupes.baseline }));
|
|
606
|
+
lines.push("OK ui-dupes no class string, component name or JSX tree is written twice beyond the baseline");
|
|
607
|
+
}
|
|
608
|
+
return problems;
|
|
609
|
+
}
|
|
610
|
+
|
|
558
611
|
/**
|
|
559
612
|
* @param {object} config
|
|
560
613
|
* @param {string | string[]} [only] files to check on the fast path a hook takes; none is the full check
|
|
@@ -877,5 +930,13 @@ export async function runCheck(config, only = [], { lister = repoFiles } = {}) {
|
|
|
877
930
|
problems.push(...(await contractRouteAgreement(config, routes, lines)));
|
|
878
931
|
}
|
|
879
932
|
|
|
933
|
+
if (enabled(config.gates, "contract-keys")) {
|
|
934
|
+
problems.push(...(await contractKeys(config, lines)));
|
|
935
|
+
}
|
|
936
|
+
|
|
937
|
+
if (enabled(config.gates, "ui-catalog") || enabled(config.gates, "ui-dupes")) {
|
|
938
|
+
problems.push(...(await uiAgreement(config, tree, lines)));
|
|
939
|
+
}
|
|
940
|
+
|
|
880
941
|
return { problems, lines };
|
|
881
942
|
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// A fragment that runs one command line in the shell, the way a Windows `.cmd` shim such as `pnpm` needs.
|
|
2
|
+
// The caller ignores Ctrl+C while the command runs, so it can clean up after the command exits.
|
|
3
|
+
|
|
4
|
+
import { spawn } from "node:child_process";
|
|
5
|
+
|
|
6
|
+
/** One shell command line from argv words; a word with a space or a quote is quoted. */
|
|
7
|
+
export function commandLine(words) {
|
|
8
|
+
const args = words[0] === "--" ? words.slice(1) : words;
|
|
9
|
+
return args.map((arg) => (/^$|[\s"]/.test(arg) ? `"${arg.replaceAll('"', '\\"')}"` : arg)).join(" ");
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** Splits argv at its first `--`: the flags before it, and the command after it. */
|
|
13
|
+
export function splitAtDashes(argv) {
|
|
14
|
+
const at = argv.indexOf("--");
|
|
15
|
+
return at < 0 ? { flags: argv, command: [] } : { flags: argv.slice(0, at), command: argv.slice(at + 1) };
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** Runs `command` in the shell. @returns a promise of its exit code; a command ended by a signal returns 1 */
|
|
19
|
+
export function runInShell(command, env = process.env, cwd = process.cwd()) {
|
|
20
|
+
return new Promise((resolve, reject) => {
|
|
21
|
+
const child = spawn(command, { shell: true, stdio: "inherit", env, cwd, windowsHide: true });
|
|
22
|
+
const ignore = () => {};
|
|
23
|
+
const stop = () => child.kill("SIGTERM");
|
|
24
|
+
process.on("SIGINT", ignore);
|
|
25
|
+
process.on("SIGTERM", stop);
|
|
26
|
+
const done = () => {
|
|
27
|
+
process.off("SIGINT", ignore);
|
|
28
|
+
process.off("SIGTERM", stop);
|
|
29
|
+
};
|
|
30
|
+
child.on("error", (error) => {
|
|
31
|
+
done();
|
|
32
|
+
reject(error);
|
|
33
|
+
});
|
|
34
|
+
child.on("exit", (code) => {
|
|
35
|
+
done();
|
|
36
|
+
resolve(code ?? 1);
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { strict as assert } from "node:assert";
|
|
2
|
+
import { test } from "node:test";
|
|
3
|
+
import { commandLine, runInShell, splitAtDashes } from "./child-run.mjs";
|
|
4
|
+
|
|
5
|
+
test("a word with a space or a quote is quoted, and a leading -- is dropped", () => {
|
|
6
|
+
assert.equal(commandLine(["--", "pnpm", "vitest", "run", "a b.test.ts", 'say "hi"']), 'pnpm vitest run "a b.test.ts" "say \\"hi\\""');
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
test("argv splits at its first --", () => {
|
|
10
|
+
assert.deepEqual(splitAtDashes(["--port", "auto", "--", "pnpm", "--", "x"]), { flags: ["--port", "auto"], command: ["pnpm", "--", "x"] });
|
|
11
|
+
assert.deepEqual(splitAtDashes(["--status"]), { flags: ["--status"], command: [] });
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
test("the shell runs the command and returns its exit code", async () => {
|
|
15
|
+
assert.equal(await runInShell(`"${process.execPath}" -e "process.exit(3)"`), 3);
|
|
16
|
+
});
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { strict as assert } from "node:assert";
|
|
2
|
+
import { readFileSync } from "node:fs";
|
|
3
|
+
import { test } from "node:test";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
import { parse } from "yaml";
|
|
6
|
+
|
|
7
|
+
const WORKFLOW = fileURLToPath(new URL("../../templates/github/workflows/ci.yml", import.meta.url));
|
|
8
|
+
|
|
9
|
+
test("the template runs unit tests in eight shards and integration tests in two, all on hosted runners", () => {
|
|
10
|
+
const workflow = parse(readFileSync(WORKFLOW, "utf8"));
|
|
11
|
+
assert.deepEqual(workflow.jobs.unit.strategy.matrix.shard, [1, 2, 3, 4, 5, 6, 7, 8]);
|
|
12
|
+
assert.deepEqual(workflow.jobs.integration.strategy.matrix.shard, [1, 2]);
|
|
13
|
+
assert.equal(workflow.jobs.unit.strategy["fail-fast"], false);
|
|
14
|
+
for (const [name, job] of Object.entries(workflow.jobs)) assert.equal(job["runs-on"], "ubuntu-latest", name);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
test("the template runs the affected browser tests on a pull request and every one on a push", () => {
|
|
18
|
+
const steps = JSON.stringify(parse(readFileSync(WORKFLOW, "utf8")).jobs.e2e.steps);
|
|
19
|
+
assert.match(steps, /pnpm e2e:affected/);
|
|
20
|
+
assert.match(steps, /pnpm e2e"/);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
test("the e2e job installs the browsers before it runs a spec", () => {
|
|
24
|
+
const steps = parse(readFileSync(WORKFLOW, "utf8")).jobs.e2e.steps;
|
|
25
|
+
const install = steps.findIndex((step) => /playwright install --with-deps/.test(step.run ?? ""));
|
|
26
|
+
const run = steps.findIndex((step) => /pnpm e2e/.test(step.run ?? ""));
|
|
27
|
+
assert.ok(install >= 0 && install < run);
|
|
28
|
+
});
|
package/src/cli/cli.test.mjs
CHANGED
|
@@ -267,8 +267,13 @@ test("a clean mirror run names every root it judged", async () => {
|
|
|
267
267
|
rmSync(dir, { recursive: true, force: true });
|
|
268
268
|
});
|
|
269
269
|
|
|
270
|
-
test("a fresh init repository with the default config passes the hooks check", async
|
|
270
|
+
test("a fresh init repository with the default config passes the hooks check", async t => {
|
|
271
271
|
const dir = repo();
|
|
272
|
+
const globalConfig = process.env.GIT_CONFIG_GLOBAL;
|
|
273
|
+
const fixtureConfig = path.join(dir, ".git", "fixture-global-config");
|
|
274
|
+
writeFileSync(fixtureConfig, "");
|
|
275
|
+
process.env.GIT_CONFIG_GLOBAL = fixtureConfig;
|
|
276
|
+
t.after(() => { if (globalConfig === undefined) delete process.env.GIT_CONFIG_GLOBAL; else process.env.GIT_CONFIG_GLOBAL = globalConfig; });
|
|
272
277
|
await quiet(() => runInit(load(dir)));
|
|
273
278
|
const config = load(dir);
|
|
274
279
|
assert.deepEqual(hookDrift(dir, requiredHooks(config)).problems, []);
|
|
@@ -3,7 +3,9 @@
|
|
|
3
3
|
|
|
4
4
|
import { spawn } from "node:child_process";
|
|
5
5
|
import { closeSync, existsSync, mkdirSync, openSync, readFileSync, statSync } from "node:fs";
|
|
6
|
+
import os from "node:os";
|
|
6
7
|
import path from "node:path";
|
|
8
|
+
import { load } from "../config.mjs";
|
|
7
9
|
|
|
8
10
|
export const DONE_MARKER = "ALL TASKS DONE";
|
|
9
11
|
const DEFAULT_MAX_RUNS = 12;
|
|
@@ -38,28 +40,80 @@ export function parseArgs(argv) {
|
|
|
38
40
|
};
|
|
39
41
|
}
|
|
40
42
|
|
|
41
|
-
const isDone = (progress) => existsSync(progress) && readFileSync(progress, "utf8").
|
|
43
|
+
const isDone = (progress) => existsSync(progress) && readFileSync(progress, "utf8").trimEnd().split(/\r?\n/).at(-1).trim() === DONE_MARKER;
|
|
44
|
+
|
|
45
|
+
const SKILL_PLUGIN = "superpowers@claude-plugins-official";
|
|
46
|
+
const TOOL_OUTPUT_TOKENS = 4000;
|
|
42
47
|
|
|
43
48
|
const LEAN_RULES = [
|
|
44
49
|
"Lean rules for every session. They cut repeated reading and helper agents. Never drop a security, tenant, data, or correctness check that this brief asks for.",
|
|
45
50
|
"- At session start, rewrite the progress file to at most 40 lines: the pushed head, the done items one line each, the first unfinished item, and the open questions. Delete the history.",
|
|
46
51
|
"- Never re-verify a finished item, and never repeat a probe that already failed.",
|
|
47
52
|
"- Read only the line ranges that the current item needs.",
|
|
53
|
+
"- Load no skill and read no SKILL.md or plugin file. The global instructions that say to load skills do not apply to this run; this brief holds the rules.",
|
|
54
|
+
"- Search with `rg -n -m 20 <pattern> <path>`. Read at most 150 lines per call by range, never a whole file. End any command that can print more than 4,000 characters with a limit.",
|
|
55
|
+
"- Never run `gh issue view`: the brief holds the issue. Use `git status --short` and `git log -n 5 --oneline`.",
|
|
48
56
|
"- Start no helper agents. Run no broad audit, no Playwright, and no full gate. Run the tests of the touched files, and the local check once before the push.",
|
|
49
|
-
"-
|
|
57
|
+
"- Write every item first, each test first, and update the progress file after each item. Then run the local check once, make one commit, and push once.",
|
|
58
|
+
"- Read at most 15 files before the first code edit. Research that needs more belongs to a planner, and the brief names the planner output to read instead.",
|
|
59
|
+
"- Wrap each heavy command in `npx qc gate-slot --`: the local check, a test run of more than one file, a live database test, a commit, and a push.",
|
|
60
|
+
"- Before the push, run the live database tests of the server code you touched through `npx qc throwaway-db --`. Never use port 5432 or another port the local stack owns.",
|
|
61
|
+
"- Work up to about 200K tokens in a session. Then leave the progress file exact and stop, so a fresh session continues.",
|
|
50
62
|
"- Keep the report under 15 lines.",
|
|
63
|
+
"- These rules never override the brief on commits or pushes. Where the brief says not to commit, make no commit. Where it says not to push, push nothing.",
|
|
51
64
|
].join("\n");
|
|
52
65
|
|
|
53
66
|
function continuationNote(progress) {
|
|
54
67
|
return (
|
|
55
68
|
`\n\n---\nThis may be a fresh session that continues earlier work. Read \`git log --oneline\` and \`git status --short\` ` +
|
|
56
|
-
`in the worktree, and the progress file ${progress}. Continue from the first unfinished task.\n\n
|
|
69
|
+
`in the worktree, and the progress file ${progress}. Continue from the first unfinished task.\n\n` +
|
|
70
|
+
`The runner owns the done marker: ${DONE_MARKER}. The brief never needs to supply it. ` +
|
|
71
|
+
`Done means the last non-empty line of the progress file equals the marker exactly, after trimming whitespace. ` +
|
|
72
|
+
`Write it as the last line, alone, only when the exit criteria hold. ` +
|
|
73
|
+
`Never quote it anywhere else. Never wait for anyone to supply it.\n\n${LEAN_RULES}`
|
|
57
74
|
);
|
|
58
75
|
}
|
|
59
76
|
|
|
60
77
|
/** The Codex features that start helper agents. Each run turns them off. */
|
|
61
78
|
const HELPER_AGENT_FEATURES = ["features.multi_agent=false", "features.multi_agent_v2=false"];
|
|
62
79
|
|
|
80
|
+
/** @returns the names of the plugins that a Codex `config.toml` turns on, from its `[plugins."name"]` tables. */
|
|
81
|
+
export function enabledPlugins(toml) {
|
|
82
|
+
const names = [];
|
|
83
|
+
let current = null;
|
|
84
|
+
for (const raw of toml.split(/\r?\n/)) {
|
|
85
|
+
const line = raw.trim();
|
|
86
|
+
const table = /^\[(.*)\]\s*(#.*)?$/.exec(line);
|
|
87
|
+
if (table) {
|
|
88
|
+
current = /^plugins\.(?:"([^"]+)"|'([^']+)')$/.exec(table[1]);
|
|
89
|
+
current = current ? (current[1] ?? current[2]) : null;
|
|
90
|
+
} else if (current && /^enabled\s*=\s*true\s*(#.*)?$/.test(line)) {
|
|
91
|
+
names.push(current);
|
|
92
|
+
current = null;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return names;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** The `-c` overrides that turn off each plugin the user enabled, and the skill plugin, for this run only. */
|
|
99
|
+
function pluginOverrides(env) {
|
|
100
|
+
const file = path.join(env.CODEX_HOME || path.join(os.homedir(), ".codex"), "config.toml");
|
|
101
|
+
let enabled = [];
|
|
102
|
+
try {
|
|
103
|
+
enabled = enabledPlugins(readFileSync(file, "utf8"));
|
|
104
|
+
} catch {
|
|
105
|
+
// A missing or unreadable config still lets the run start, with the superpowers override alone.
|
|
106
|
+
}
|
|
107
|
+
return [...new Set([...enabled, SKILL_PLUGIN])].flatMap((name) => ["-c", `plugins.${JSON.stringify(name)}.enabled=false`]);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** @returns `codex.compactTokens` of the worktree's config; throws when it is not a positive whole number. */
|
|
111
|
+
function compactTokensOf(worktree) {
|
|
112
|
+
const value = load(worktree).codex.compactTokens;
|
|
113
|
+
if (!Number.isInteger(value) || value < 1) throw new Error(`codex.compactTokens in qc.config.json must be a positive whole number, got ${JSON.stringify(value)}`);
|
|
114
|
+
return value;
|
|
115
|
+
}
|
|
116
|
+
|
|
63
117
|
/** The brief is read again for each run, so an edit reaches the next session. */
|
|
64
118
|
const promptFor = (settings) => readFileSync(settings.brief, "utf8") + continuationNote(settings.progress);
|
|
65
119
|
|
|
@@ -83,11 +137,24 @@ function spawnRun({ command, args, env, log }, codexCommand) {
|
|
|
83
137
|
});
|
|
84
138
|
}
|
|
85
139
|
|
|
140
|
+
/** Writes one stdout line and resolves once it is flushed, so the `process.exit` that follows cannot cut it. */
|
|
141
|
+
const writeLine = (line) => new Promise((resolve) => process.stdout.write(`${line}
|
|
142
|
+
`, resolve));
|
|
143
|
+
|
|
86
144
|
/**
|
|
145
|
+
* The last stdout line is `exit <code>` on every path, so a redirect of the runner into a wrapper log carries
|
|
146
|
+
* the end of the job for `qc watch --codex`, in any shell.
|
|
87
147
|
* @param run `({ command, args, env, log }) => Promise<{ status, tail }>`; the default spawns `codexCommand`.
|
|
148
|
+
* @param out `(line) => Promise|void` writes the `exit` line; the default writes to stdout.
|
|
88
149
|
* @returns the exit code: 0 done, 1 bad input or no codex, 2 a limit, 3 out of runs.
|
|
89
150
|
*/
|
|
90
|
-
export async function runCodexImplement(argv, {
|
|
151
|
+
export async function runCodexImplement(argv, { out = writeLine, ...options } = {}) {
|
|
152
|
+
const code = await runJob(argv, options);
|
|
153
|
+
await out(`exit ${code}`);
|
|
154
|
+
return code;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
async function runJob(argv, { run, codexCommand = ["codex"], env = process.env, say = (line) => console.error(line) } = {}) {
|
|
91
158
|
let settings;
|
|
92
159
|
try {
|
|
93
160
|
settings = parseArgs(argv);
|
|
@@ -95,6 +162,12 @@ export async function runCodexImplement(argv, { run, codexCommand = ["codex"], e
|
|
|
95
162
|
say(`qc codex-implement: ${error.message}\n${USAGE}`);
|
|
96
163
|
return 1;
|
|
97
164
|
}
|
|
165
|
+
let compactTokens;
|
|
166
|
+
try {
|
|
167
|
+
compactTokens = compactTokensOf(settings.worktree);
|
|
168
|
+
} catch (error) {
|
|
169
|
+
return finish(say, error.message, 1);
|
|
170
|
+
}
|
|
98
171
|
const execute = run ?? ((call) => spawnRun(call, codexCommand));
|
|
99
172
|
for (let count = 1; count <= settings.maxRuns; count += 1) {
|
|
100
173
|
if (isDone(settings.progress)) return finish(say, `done after ${count - 1} run(s)`, 0);
|
|
@@ -105,7 +178,8 @@ export async function runCodexImplement(argv, { run, codexCommand = ["codex"], e
|
|
|
105
178
|
return finish(say, `cannot read the brief: ${error.message}`, 1);
|
|
106
179
|
}
|
|
107
180
|
const helperOff = HELPER_AGENT_FEATURES.flatMap((feature) => ["-c", feature]);
|
|
108
|
-
const
|
|
181
|
+
const limits = ["-c", `model_auto_compact_token_limit=${compactTokens}`, "-c", `tool_output_token_limit=${TOOL_OUTPUT_TOKENS}`];
|
|
182
|
+
const args = ["exec", "-s", "danger-full-access", "-C", settings.worktree, ...helperOff, ...pluginOverrides(env), ...limits, prompt];
|
|
109
183
|
let result;
|
|
110
184
|
try {
|
|
111
185
|
result = await execute({ command: codexCommand[0], args, env: { ...env, QC_ROLE: "implementer" }, log: settings.log });
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { strict as assert } from "node:assert";
|
|
2
2
|
import { spawnSync } from "node:child_process";
|
|
3
|
-
import { appendFileSync, mkdtempSync, readFileSync, realpathSync, rmSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { appendFileSync, mkdirSync, mkdtempSync, readFileSync, realpathSync, rmSync, writeFileSync } from "node:fs";
|
|
4
4
|
import os from "node:os";
|
|
5
5
|
import path from "node:path";
|
|
6
6
|
import { test } from "node:test";
|
|
7
7
|
import { fileURLToPath } from "node:url";
|
|
8
|
-
import { DONE_MARKER, parseArgs, runCodexImplement } from "./codex-implement.mjs";
|
|
8
|
+
import { DONE_MARKER, enabledPlugins, parseArgs, runCodexImplement } from "./codex-implement.mjs";
|
|
9
9
|
|
|
10
10
|
const QC = fileURLToPath(new URL("./qc.mjs", import.meta.url));
|
|
11
11
|
|
|
@@ -21,6 +21,29 @@ function setup(t, { progress = "" } = {}) {
|
|
|
21
21
|
|
|
22
22
|
const quiet = () => {};
|
|
23
23
|
|
|
24
|
+
for (const [name, progress] of [
|
|
25
|
+
["marker in prose", `Do not append ${DONE_MARKER} until the push succeeds.\n`],
|
|
26
|
+
["marker before more lines", `${DONE_MARKER}\nPush remains unfinished.\n`],
|
|
27
|
+
["quoted marker", `"${DONE_MARKER}"\n`],
|
|
28
|
+
["empty progress", "\n \n"],
|
|
29
|
+
]) {
|
|
30
|
+
test(`${name} never ends the job before or after a run`, async (t) => {
|
|
31
|
+
const { argv } = setup(t, { progress });
|
|
32
|
+
let calls = 0;
|
|
33
|
+
const run = async () => {
|
|
34
|
+
calls += 1;
|
|
35
|
+
return { status: 0, tail: "work remains" };
|
|
36
|
+
};
|
|
37
|
+
assert.equal(await runCodexImplement([...argv, "--max-runs", "1"], { run, say: quiet }), 3);
|
|
38
|
+
assert.equal(calls, 1);
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
test("the last non-empty line accepts the exact marker with surrounding whitespace", async (t) => {
|
|
43
|
+
const { argv } = setup(t, { progress: `Tasks pushed.\r\n \t${DONE_MARKER}\t \r\n \r\n\t\r\n` });
|
|
44
|
+
assert.equal(await runCodexImplement(argv, { run: async () => assert.fail("no run"), say: quiet }), 0);
|
|
45
|
+
});
|
|
46
|
+
|
|
24
47
|
test("a progress file that already says ALL TASKS DONE exits 0 with no run", async (t) => {
|
|
25
48
|
const { argv } = setup(t, { progress: `task 1 committed\n${DONE_MARKER}\n` });
|
|
26
49
|
let calls = 0;
|
|
@@ -45,7 +68,7 @@ test("a run passes exec, the sandbox, the worktree and one prompt, with QC_ROLE=
|
|
|
45
68
|
const prompt = call.args.at(-1);
|
|
46
69
|
assert.equal(call.env.QC_ROLE, "implementer");
|
|
47
70
|
assert.ok(prompt.startsWith(readFileSync(brief, "utf8")));
|
|
48
|
-
assert.match(prompt, /git log --oneline/);
|
|
71
|
+
assert.match(prompt, /git log -n 5 --oneline/);
|
|
49
72
|
assert.match(prompt, /git status --short/);
|
|
50
73
|
assert.match(prompt, /first unfinished task/);
|
|
51
74
|
assert.ok(prompt.includes(progress));
|
|
@@ -79,11 +102,21 @@ test("every run prompt carries the lean rules after the brief", async (t) => {
|
|
|
79
102
|
assert.equal(seen.length, 2);
|
|
80
103
|
for (const prompt of seen) {
|
|
81
104
|
assert.ok(prompt.startsWith(readFileSync(brief, "utf8")));
|
|
105
|
+
assert.ok(prompt.includes(DONE_MARKER));
|
|
106
|
+
assert.match(prompt, /last non-empty line.*equals.*exactly.*trimming whitespace/i);
|
|
107
|
+
assert.match(prompt, /last line, alone.*only when.*exit criteria/i);
|
|
108
|
+
assert.match(prompt, /never quote.*anywhere else/i);
|
|
109
|
+
assert.match(prompt, /never wait for anyone to supply/i);
|
|
82
110
|
assert.match(prompt, /at most 40 lines/);
|
|
83
111
|
assert.match(prompt, /re-verify/);
|
|
84
112
|
assert.match(prompt, /line ranges/);
|
|
85
113
|
assert.match(prompt, /helper agents/);
|
|
86
|
-
assert.match(prompt, /one
|
|
114
|
+
assert.match(prompt, /Then run the local check once, make one commit, and push once\./);
|
|
115
|
+
assert.match(prompt, /never override the brief on commits or pushes/);
|
|
116
|
+
assert.match(prompt, /npx qc gate-slot --/);
|
|
117
|
+
assert.match(prompt, /npx qc throwaway-db --/);
|
|
118
|
+
assert.match(prompt, /Never use port 5432/);
|
|
119
|
+
assert.match(prompt, /at most 15 files before the first code edit/i);
|
|
87
120
|
assert.match(prompt, /under 15 lines/);
|
|
88
121
|
assert.match(prompt, /never drop a security, tenant, data, or correctness check/i);
|
|
89
122
|
}
|
|
@@ -103,6 +136,117 @@ test("a run turns off the Codex helper agents with -c, before the prompt", async
|
|
|
103
136
|
assert.ok(index < args.length - 1, "the prompt stays the last argument");
|
|
104
137
|
});
|
|
105
138
|
|
|
139
|
+
const CODEX_TOML = [
|
|
140
|
+
"model = 'x'",
|
|
141
|
+
'[plugins."browser@openai-bundled"]',
|
|
142
|
+
"enabled = true",
|
|
143
|
+
'[plugins."chrome@openai-bundled"]',
|
|
144
|
+
"enabled = false",
|
|
145
|
+
"[plugins.'pdf@openai-primary-runtime']",
|
|
146
|
+
"enabled = true # on",
|
|
147
|
+
'[projects."C:/repo"]',
|
|
148
|
+
"enabled = true",
|
|
149
|
+
'[plugins."superpowers@claude-plugins-official"]',
|
|
150
|
+
"enabled = true",
|
|
151
|
+
].join("\n");
|
|
152
|
+
|
|
153
|
+
function codexHome(t, toml = CODEX_TOML) {
|
|
154
|
+
const home = realpathSync.native(mkdtempSync(path.join(os.tmpdir(), "qc-codex-home-")));
|
|
155
|
+
t.after(() => rmSync(home, { recursive: true, force: true }));
|
|
156
|
+
if (toml !== null) writeFileSync(path.join(home, "config.toml"), toml);
|
|
157
|
+
return home;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
async function firstCall(t, { env = {}, config } = {}) {
|
|
161
|
+
const { argv, progress, worktree } = setup(t);
|
|
162
|
+
if (config !== undefined) {
|
|
163
|
+
mkdirSync(worktree, { recursive: true });
|
|
164
|
+
writeFileSync(path.join(worktree, "qc.config.json"), JSON.stringify(config));
|
|
165
|
+
}
|
|
166
|
+
let call;
|
|
167
|
+
const run = async (made) => {
|
|
168
|
+
call = made;
|
|
169
|
+
appendFileSync(progress, `${DONE_MARKER}\n`);
|
|
170
|
+
return { status: 0, tail: "ok" };
|
|
171
|
+
};
|
|
172
|
+
const code = await runCodexImplement(argv, { run, say: quiet, env: { CODEX_HOME: codexHome(t), ...env } });
|
|
173
|
+
return { code, call };
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
const overrides = (args) => args.flatMap((arg, index) => (args[index - 1] === "-c" ? [arg] : []));
|
|
177
|
+
|
|
178
|
+
test("enabledPlugins lists the plugins the config file turns on, and no table that only looks like one", () => {
|
|
179
|
+
assert.deepEqual(enabledPlugins(CODEX_TOML), [
|
|
180
|
+
"browser@openai-bundled",
|
|
181
|
+
"pdf@openai-primary-runtime",
|
|
182
|
+
"superpowers@claude-plugins-official",
|
|
183
|
+
]);
|
|
184
|
+
assert.deepEqual(enabledPlugins(""), []);
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
test("a run turns off every plugin the user's Codex config enables, and superpowers by name", async (t) => {
|
|
188
|
+
const { call } = await firstCall(t);
|
|
189
|
+
const set = overrides(call.args);
|
|
190
|
+
for (const name of ["browser@openai-bundled", "pdf@openai-primary-runtime", "superpowers@claude-plugins-official"]) {
|
|
191
|
+
assert.ok(set.includes(`plugins."${name}".enabled=false`), name);
|
|
192
|
+
}
|
|
193
|
+
assert.ok(!set.some((entry) => entry.includes("chrome@openai-bundled")), "a plugin already off needs no override");
|
|
194
|
+
assert.ok(set.includes("features.multi_agent=false"), "the helper-agent overrides stay");
|
|
195
|
+
assert.ok(set.includes("features.multi_agent_v2=false"));
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
test("superpowers is turned off when the config file is missing, and nothing else is", async (t) => {
|
|
199
|
+
const { call } = await firstCall(t, { env: { CODEX_HOME: codexHome(t, null) } });
|
|
200
|
+
const plugins = overrides(call.args).filter((entry) => entry.startsWith("plugins."));
|
|
201
|
+
assert.deepEqual(plugins, ['plugins."superpowers@claude-plugins-official".enabled=false']);
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
test("a config.toml that is a directory falls back to the superpowers override and does not throw", async (t) => {
|
|
205
|
+
const home = codexHome(t, null);
|
|
206
|
+
mkdirSync(path.join(home, "config.toml"));
|
|
207
|
+
const { code, call } = await firstCall(t, { env: { CODEX_HOME: home } });
|
|
208
|
+
assert.equal(code, 0);
|
|
209
|
+
const plugins = overrides(call.args).filter((entry) => entry.startsWith("plugins."));
|
|
210
|
+
assert.deepEqual(plugins, ['plugins."superpowers@claude-plugins-official".enabled=false']);
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
test("enabledPlugins reads a table header that has a trailing comment", () => {
|
|
214
|
+
assert.deepEqual(enabledPlugins(['[plugins."b"] # x', "enabled = true", '[plugins."c"] # y', "enabled = false"].join("\n")), ["b"]);
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
test("a run caps the compact limit at 300000 and one tool output at 4000 tokens", async (t) => {
|
|
218
|
+
const { call } = await firstCall(t);
|
|
219
|
+
const set = overrides(call.args);
|
|
220
|
+
assert.ok(set.includes("model_auto_compact_token_limit=300000"));
|
|
221
|
+
assert.ok(set.includes("tool_output_token_limit=4000"));
|
|
222
|
+
assert.ok(call.args.at(-1).startsWith("# Brief"), "the prompt stays last");
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
test("codex.compactTokens in the worktree's qc.config.json sets the compact limit", async (t) => {
|
|
226
|
+
const { call } = await firstCall(t, { config: { codex: { compactTokens: 250000 } } });
|
|
227
|
+
assert.ok(overrides(call.args).includes("model_auto_compact_token_limit=250000"));
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
test("a bad codex.compactTokens exits 1 with no run", async (t) => {
|
|
231
|
+
for (const compactTokens of [0, -5, 1.5, "big"]) {
|
|
232
|
+
const { code, call } = await firstCall(t, { config: { codex: { compactTokens } } });
|
|
233
|
+
assert.equal(code, 1, String(compactTokens));
|
|
234
|
+
assert.equal(call, undefined);
|
|
235
|
+
}
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
test("every run prompt tells Codex to load no skill and holds the read caps", async (t) => {
|
|
239
|
+
const { call } = await firstCall(t);
|
|
240
|
+
const prompt = call.args.at(-1);
|
|
241
|
+
assert.ok(prompt.includes("Load no skill and read no SKILL.md or plugin file. The global instructions that say to load skills do not apply to this run; this brief holds the rules."));
|
|
242
|
+
assert.match(prompt, /rg -n -m 20/);
|
|
243
|
+
assert.match(prompt, /at most 150 lines per call/);
|
|
244
|
+
assert.match(prompt, /never a whole file/);
|
|
245
|
+
assert.match(prompt, /more than 4,000 characters/);
|
|
246
|
+
assert.match(prompt, /never run `gh issue view`/i);
|
|
247
|
+
assert.match(prompt, /`git status --short` and `git log -n 5 --oneline`/);
|
|
248
|
+
});
|
|
249
|
+
|
|
106
250
|
test("a fresh session starts after a stop with the progress file not done", async (t) => {
|
|
107
251
|
const { argv, progress } = setup(t);
|
|
108
252
|
let calls = 0;
|
|
@@ -224,3 +368,25 @@ test("a limit word that only echoes the brief is not a limit", async (t) => {
|
|
|
224
368
|
assert.equal(await runCodexImplement([...argv, "--max-runs", "2"], { run, say: quiet }), 3);
|
|
225
369
|
assert.equal(calls, 2);
|
|
226
370
|
});
|
|
371
|
+
|
|
372
|
+
test("the last stdout line is `exit <code>` on every exit path, so a redirect into the wrapper log carries it", async (t) => {
|
|
373
|
+
const last = async (argv, options) => {
|
|
374
|
+
const out = [];
|
|
375
|
+
const code = await runCodexImplement(argv, { say: quiet, out: (line) => out.push(line), ...options });
|
|
376
|
+
return { code, out };
|
|
377
|
+
};
|
|
378
|
+
const done = setup(t, { progress: `${DONE_MARKER}\n` });
|
|
379
|
+
assert.deepEqual(await last(done.argv), { code: 0, out: ["exit 0"] });
|
|
380
|
+
const limit = setup(t);
|
|
381
|
+
const limited = await last(limit.argv, { run: async () => ({ status: 1, tail: "You have hit your usage limit" }) });
|
|
382
|
+
assert.deepEqual(limited, { code: 2, out: ["exit 2"] });
|
|
383
|
+
const spent = setup(t);
|
|
384
|
+
assert.deepEqual(await last([...spent.argv, "--max-runs", "1"], { run: async () => ({ status: 0, tail: "" }) }), { code: 3, out: ["exit 3"] });
|
|
385
|
+
assert.deepEqual(await last(["--nope", "x"]), { code: 1, out: ["exit 1"] });
|
|
386
|
+
});
|
|
387
|
+
|
|
388
|
+
test("the CLI prints `exit 1` as the last stdout line after a usage error", () => {
|
|
389
|
+
const result = spawnSync(process.execPath, [QC, "codex-implement"], { encoding: "utf8" });
|
|
390
|
+
assert.equal(result.status, 1);
|
|
391
|
+
assert.equal(result.stdout.trimEnd().split("\n").at(-1), "exit 1");
|
|
392
|
+
});
|
|
@@ -6,7 +6,7 @@ import { existsSync } from "node:fs";
|
|
|
6
6
|
import os from "node:os";
|
|
7
7
|
import { fileURLToPath } from "node:url";
|
|
8
8
|
import { dispatchNote, dispatchRefusal, dispatchSettingsAt, isImplementer, slotRefusal, typeOf } from "./dispatch.mjs";
|
|
9
|
-
import { claimSlot, reclaimSlot, releaseSlot, slotDirOf } from "./dispatch-slot.mjs";
|
|
9
|
+
import { claimSlot, clearSlots, reclaimSlot, releaseHolder, releaseSlot, slotDirOf } from "./dispatch-slot.mjs";
|
|
10
10
|
import { judgeTask, recordDispatch } from "./task-gate.mjs";
|
|
11
11
|
|
|
12
12
|
const output = (fields) => ({ hookSpecificOutput: { hookEventName: "PreToolUse", ...fields } });
|
|
@@ -27,9 +27,32 @@ function onSubagent(call, dir, now) {
|
|
|
27
27
|
}
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
+
const FRESH_STARTS = new Set(["startup", "resume"]);
|
|
31
|
+
|
|
32
|
+
/** Frees the slot of the agent a TaskStop ended, since that agent sends no SubagentStop. */
|
|
33
|
+
function onTaskStop(call, dir) {
|
|
34
|
+
const id = idOf(call.tool_input?.task_id) ?? idOf(call.tool_input?.shell_id);
|
|
35
|
+
if (!id || !existsSync(dir)) return null;
|
|
36
|
+
try {
|
|
37
|
+
if (!releaseHolder(dir, id)) return null;
|
|
38
|
+
} catch {
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
return { hookSpecificOutput: { hookEventName: "PostToolUse", additionalContext: `Dispatch guard: freed the implementer slot that ${id} held.` } };
|
|
42
|
+
}
|
|
43
|
+
|
|
30
44
|
/** The verdict on one hook event. `reads` replaces the task gate's CI and base reads, for a test. @returns the hook output, or null to let the call run with no message. */
|
|
31
45
|
export function decide(call, tmp = os.tmpdir(), now = Date.now(), reads = {}) {
|
|
32
46
|
const dir = slotDirOf(call.session_id ?? "session", tmp);
|
|
47
|
+
if (call.hook_event_name === "SessionStart") {
|
|
48
|
+
try {
|
|
49
|
+
if (FRESH_STARTS.has(call.source) && existsSync(dir)) clearSlots(dir);
|
|
50
|
+
} catch {
|
|
51
|
+
// A busy temp folder costs only a slot that still expires.
|
|
52
|
+
}
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
if (call.hook_event_name === "PostToolUse" && call.tool_name === "TaskStop") return onTaskStop(call, dir);
|
|
33
56
|
if (call.hook_event_name === "SubagentStart" || call.hook_event_name === "SubagentStop") {
|
|
34
57
|
onSubagent(call, dir, now);
|
|
35
58
|
return null;
|