architecture-harness 0.21.0 → 0.21.2

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "architecture-harness",
3
- "version": "0.21.0",
3
+ "version": "0.21.2",
4
4
  "type": "module",
5
5
  "description": "Architecture gates, lint rules and scaffolding for the bounded-vertical-slice architecture",
6
6
  "license": "MIT",
@@ -40,13 +40,29 @@ export function parseArgs(argv) {
40
40
 
41
41
  const isDone = (progress) => existsSync(progress) && readFileSync(progress, "utf8").includes(DONE_MARKER);
42
42
 
43
+ const LEAN_RULES = [
44
+ "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
+ "- 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
+ "- Never re-verify a finished item, and never repeat a probe that already failed.",
47
+ "- Read only the line ranges that the current item needs.",
48
+ "- 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
+ "- One item per session: code, test, commit, push, update the progress file, stop.",
50
+ "- Keep the report under 15 lines.",
51
+ ].join("\n");
52
+
43
53
  function continuationNote(progress) {
44
54
  return (
45
55
  `\n\n---\nThis may be a fresh session that continues earlier work. Read \`git log --oneline\` and \`git status --short\` ` +
46
- `in the worktree, and the progress file ${progress}. Continue from the first unfinished task.`
56
+ `in the worktree, and the progress file ${progress}. Continue from the first unfinished task.\n\n${LEAN_RULES}`
47
57
  );
48
58
  }
49
59
 
60
+ /** The Codex features that start helper agents. Each run turns them off. */
61
+ const HELPER_AGENT_FEATURES = ["features.multi_agent=false", "features.multi_agent_v2=false"];
62
+
63
+ /** The brief is read again for each run, so an edit reaches the next session. */
64
+ const promptFor = (settings) => readFileSync(settings.brief, "utf8") + continuationNote(settings.progress);
65
+
50
66
  /** Runs one codex process with no shell and a closed stdin, appends its output to the log, and resolves with the exit status and the output tail. */
51
67
  function spawnRun({ command, args, env, log }, codexCommand) {
52
68
  const [bin, ...prefix] = codexCommand;
@@ -80,10 +96,16 @@ export async function runCodexImplement(argv, { run, codexCommand = ["codex"], e
80
96
  return 1;
81
97
  }
82
98
  const execute = run ?? ((call) => spawnRun(call, codexCommand));
83
- const prompt = readFileSync(settings.brief, "utf8") + continuationNote(settings.progress);
84
- const args = ["exec", "-s", "danger-full-access", "-C", settings.worktree, prompt];
85
99
  for (let count = 1; count <= settings.maxRuns; count += 1) {
86
100
  if (isDone(settings.progress)) return finish(say, `done after ${count - 1} run(s)`, 0);
101
+ let prompt;
102
+ try {
103
+ prompt = promptFor(settings);
104
+ } catch (error) {
105
+ return finish(say, `cannot read the brief: ${error.message}`, 1);
106
+ }
107
+ const helperOff = HELPER_AGENT_FEATURES.flatMap((feature) => ["-c", feature]);
108
+ const args = ["exec", "-s", "danger-full-access", "-C", settings.worktree, ...helperOff, prompt];
87
109
  let result;
88
110
  try {
89
111
  result = await execute({ command: codexCommand[0], args, env: { ...env, QC_ROLE: "implementer" }, log: settings.log });
@@ -42,16 +42,67 @@ test("a run passes exec, the sandbox, the worktree and one prompt, with QC_ROLE=
42
42
  const [call] = seen;
43
43
  assert.equal(call.command, "codex");
44
44
  assert.deepEqual(call.args.slice(0, 5), ["exec", "-s", "danger-full-access", "-C", worktree]);
45
- assert.equal(call.args.length, 6);
45
+ const prompt = call.args.at(-1);
46
46
  assert.equal(call.env.QC_ROLE, "implementer");
47
- assert.ok(call.args[5].startsWith(readFileSync(brief, "utf8")));
48
- assert.match(call.args[5], /git log --oneline/);
49
- assert.match(call.args[5], /git status --short/);
50
- assert.match(call.args[5], /first unfinished task/);
51
- assert.ok(call.args[5].includes(progress));
47
+ assert.ok(prompt.startsWith(readFileSync(brief, "utf8")));
48
+ assert.match(prompt, /git log --oneline/);
49
+ assert.match(prompt, /git status --short/);
50
+ assert.match(prompt, /first unfinished task/);
51
+ assert.ok(prompt.includes(progress));
52
52
  assert.equal(call.log, `${progress}.log`);
53
53
  });
54
54
 
55
+ test("the brief is read again before each run, so an edit reaches the next session", async (t) => {
56
+ const { argv, brief, progress } = setup(t);
57
+ const prompts = [];
58
+ const run = async (call) => {
59
+ prompts.push(call.args.at(-1));
60
+ if (prompts.length === 1) writeFileSync(brief, "# Brief\n\nDo task one.\nDecision: use the new column name.\n");
61
+ else appendFileSync(progress, `${DONE_MARKER}\n`);
62
+ return { status: 0, tail: "ok" };
63
+ };
64
+ assert.equal(await runCodexImplement(argv, { run, say: quiet }), 0);
65
+ assert.equal(prompts.length, 2);
66
+ assert.ok(!prompts[0].includes("new column name"));
67
+ assert.ok(prompts[1].includes("Decision: use the new column name."));
68
+ });
69
+
70
+ test("every run prompt carries the lean rules after the brief", async (t) => {
71
+ const { argv, brief, progress } = setup(t);
72
+ const seen = [];
73
+ const run = async (call) => {
74
+ seen.push(call.args.at(-1));
75
+ if (seen.length === 2) appendFileSync(progress, `${DONE_MARKER}\n`);
76
+ return { status: 0, tail: "ok" };
77
+ };
78
+ assert.equal(await runCodexImplement(argv, { run, say: quiet }), 0);
79
+ assert.equal(seen.length, 2);
80
+ for (const prompt of seen) {
81
+ assert.ok(prompt.startsWith(readFileSync(brief, "utf8")));
82
+ assert.match(prompt, /at most 40 lines/);
83
+ assert.match(prompt, /re-verify/);
84
+ assert.match(prompt, /line ranges/);
85
+ assert.match(prompt, /helper agents/);
86
+ assert.match(prompt, /one item per session/i);
87
+ assert.match(prompt, /under 15 lines/);
88
+ assert.match(prompt, /never drop a security, tenant, data, or correctness check/i);
89
+ }
90
+ });
91
+
92
+ test("a run turns off the Codex helper agents with -c, before the prompt", async (t) => {
93
+ const { argv, progress } = setup(t);
94
+ let args;
95
+ const run = async (call) => {
96
+ args = call.args;
97
+ appendFileSync(progress, `${DONE_MARKER}\n`);
98
+ return { status: 0, tail: "ok" };
99
+ };
100
+ assert.equal(await runCodexImplement(argv, { run, say: quiet }), 0);
101
+ const index = args.indexOf("features.multi_agent=false");
102
+ assert.ok(index > 0 && args[index - 1] === "-c");
103
+ assert.ok(index < args.length - 1, "the prompt stays the last argument");
104
+ });
105
+
55
106
  test("a fresh session starts after a stop with the progress file not done", async (t) => {
56
107
  const { argv, progress } = setup(t);
57
108
  let calls = 0;
@@ -146,7 +197,7 @@ test("the default runner spawns without a shell, closes stdin, and appends outpu
146
197
  assert.equal(line.role, "implementer");
147
198
  assert.equal(line.input, "");
148
199
  assert.deepEqual(line.args.slice(0, 3), ["exec", "-s", "danger-full-access"]);
149
- assert.ok(line.args[5].startsWith(readFileSync(brief, "utf8")));
200
+ assert.ok(line.args.at(-1).startsWith(readFileSync(brief, "utf8")));
150
201
 
151
202
  writeFileSync(progress, "");
152
203
  process.env.FAKE_MODE = "limit";
@@ -19,10 +19,11 @@ export const REMINDER =
19
19
  "Review a head only once CI on it is green, with the base merged in: fix a CI failure first, and merge `origin/<base>` at Step 0 of a fix round. " +
20
20
  "A PR gets one branch review. After it, review each fix round once, by its increment: add `Reviewed: <sha>`, the last head a branch review named.";
21
21
 
22
- /** One added sentence: a Codex implementer through the runner, and the Claude fallback on a Codex limit. */
22
+ /** Two sentences name Codex first, its controller skill, and the Claude fallback on a Codex limit. */
23
23
  export const CODEX_LINE =
24
- " An implementer may run on Codex through `qc codex-implement --brief <file> --worktree <path> --progress <file>`; " +
25
- "on exit 2 (a Codex limit), dispatch the same brief to an architecture:sdd-implementer (sonnet).";
24
+ " Implementer work runs on Codex first through `qc codex-implement --brief <file> --worktree <path> --progress <file>`; " +
25
+ "load the `architecture:codex` skill before the first dispatch. " +
26
+ "On exit 2 (a Codex limit), dispatch the same brief to an architecture:sdd-implementer (sonnet).";
26
27
 
27
28
  /** The note a Codex implementer gets in place of the controller note, with `QC_ROLE=implementer`. */
28
29
  export const WORKER_NOTE =
@@ -265,9 +265,11 @@ test("the models line names each role's models for the runtime, and how to dispa
265
265
  assert.match(modelsLine({ models: {}, runtime: null }), /unknown runtime \(set QC_RUNTIME to claude or codex\): sdd-planner the session's model/);
266
266
  });
267
267
 
268
- test("the controller note says an implementer may run on Codex, and to fall back to a Claude sonnet on exit 2", () => {
268
+ test("the controller note makes Codex the first choice, names its skill, and falls back to Claude sonnet on exit 2", () => {
269
269
  for (const phrase of [
270
+ /Implementer work runs on Codex first/,
270
271
  /qc codex-implement --brief <file> --worktree <path> --progress <file>/,
272
+ /load the `architecture:codex` skill before the first dispatch/,
271
273
  /exit 2 \(a Codex limit\)/,
272
274
  /architecture:sdd-implementer \(sonnet\)/,
273
275
  /same brief/,
@@ -355,3 +355,121 @@ test("a failing typecheck with no error line does not retry, and a passing one r
355
355
  assert.equal(green.run().status, 0);
356
356
  assert.equal(green.count(), 1);
357
357
  });
358
+
359
+ test("a root typecheck script replaces tsc -b, keeps the TS2307 retry, and the lint call carries a cache", (t) => {
360
+ const ws = workspace(t, { codes: {}, withDoctor: false });
361
+ const base = path.dirname(ws.tmpDir);
362
+ const repo = path.join(base, "repo");
363
+ mkdirSync(path.join(repo, "node_modules", ".bin"), { recursive: true });
364
+ writeFileSync(path.join(repo, "node_modules", ".bin", "eslint"), "");
365
+ writeFileSync(path.join(repo, "package.json"), JSON.stringify({ scripts: { typecheck: "tsgo" } }));
366
+ writeFileSync(path.join(repo, "pnpm-lock.yaml"), "");
367
+ const stubDir = path.join(base, "stub");
368
+ mkdirSync(stubDir, { recursive: true });
369
+ const calls = path.join(stubDir, "calls");
370
+ const counter = path.join(stubDir, "count");
371
+ writeFileSync(
372
+ path.join(stubDir, "pnpm"),
373
+ [
374
+ "#!/usr/bin/env bash",
375
+ `echo "pnpm $*" >> "${calls}"`,
376
+ `n=$(cat "${counter}" 2>/dev/null || echo 0); n=$((n + 1)); echo $n > "${counter}"`,
377
+ `if [ $n -eq 1 ]; then printf '%s' "${TS2307.trim()}"; exit 2; fi`,
378
+ "exit 0",
379
+ ].join("\n") + "\n",
380
+ { mode: 0o755 },
381
+ );
382
+ writeFileSync(path.join(stubDir, "npx"), ["#!/usr/bin/env bash", `echo "npx $*" >> "${calls}"`, "exit 0"].join("\n") + "\n", { mode: 0o755 });
383
+ const result = ws.run({}, { PATH: `${stubDir}${path.delimiter}${process.env.PATH}` });
384
+ assert.equal(result.status, 0, result.stderr);
385
+ const lines = readFileSync(calls, "utf8").trim().split("\n");
386
+ assert.deepEqual(lines.slice(0, 2), ["pnpm run -s typecheck", "pnpm run -s typecheck"]);
387
+ assert.ok(!lines.some((l) => l.startsWith("npx tsc")));
388
+ assert.ok(lines.includes("npx eslint . --max-warnings 0 --cache --cache-location node_modules/.cache/eslint-stop-gate"));
389
+ });
390
+
391
+ /** A workspace whose repository is a git repository with one commit, so the gate can fingerprint it. */
392
+ function gitWorkspace(t, installed = { codes: {}, withDoctor: false }) {
393
+ const ws = workspace(t, installed);
394
+ const repo = path.join(path.dirname(ws.tmpDir), "repo");
395
+ const git = (...args) => {
396
+ const r = spawnSync("git", ["-c", "user.name=t", "-c", "user.email=t@example.com", ...args], { cwd: repo, encoding: "utf8" });
397
+ assert.equal(r.status, 0, r.stderr);
398
+ };
399
+ writeFileSync(path.join(repo, ".gitignore"), "node_modules\n");
400
+ writeFileSync(path.join(repo, "a.txt"), "one\n");
401
+ git("init", "-q");
402
+ git("add", "-A");
403
+ git("commit", "-q", "-m", "first");
404
+ return { ...ws, repo, git };
405
+ }
406
+
407
+ test("a second stop on an unchanged checkout skips the gate", (t) => {
408
+ const ws = gitWorkspace(t);
409
+ assert.equal(ws.run().status, 0);
410
+ assert.deepEqual(ws.ran(), ["installed check"]);
411
+ assert.equal(ws.run().status, 0);
412
+ assert.deepEqual(ws.ran(), ["installed check"]);
413
+ });
414
+
415
+ test("an edited tracked file makes the next stop run the full gate", (t) => {
416
+ const ws = gitWorkspace(t);
417
+ ws.run();
418
+ writeFileSync(path.join(ws.repo, "a.txt"), "two\n");
419
+ ws.run();
420
+ assert.equal(ws.ran().length, 2);
421
+ writeFileSync(path.join(ws.repo, "a.txt"), "three\n");
422
+ ws.run();
423
+ assert.equal(ws.ran().length, 3);
424
+ });
425
+
426
+ test("a new untracked file makes the next stop run the full gate", (t) => {
427
+ const ws = gitWorkspace(t);
428
+ ws.run();
429
+ writeFileSync(path.join(ws.repo, "b.txt"), "new\n");
430
+ ws.run();
431
+ assert.equal(ws.ran().length, 2);
432
+ ws.run();
433
+ assert.equal(ws.ran().length, 2);
434
+ });
435
+
436
+ test("a new commit makes the next stop run the full gate", (t) => {
437
+ const ws = gitWorkspace(t);
438
+ ws.run();
439
+ ws.git("commit", "-q", "--allow-empty", "-m", "second");
440
+ ws.run();
441
+ assert.equal(ws.ran().length, 2);
442
+ });
443
+
444
+ test("a red first run stores nothing, so the next stop runs the full gate again", (t) => {
445
+ const ws = gitWorkspace(t, { codes: { check: 1 }, withDoctor: false });
446
+ assert.equal(ws.run().status, 2);
447
+ assert.equal(ws.run().status, 2);
448
+ assert.equal(ws.ran().length, 2);
449
+ });
450
+
451
+ test("a repository that is no git checkout always runs the full gate", (t) => {
452
+ const ws = workspace(t, { codes: {}, withDoctor: false });
453
+ ws.run();
454
+ ws.run();
455
+ assert.equal(ws.ran().length, 2);
456
+ });
457
+
458
+ test("two checkout roots do not share a fingerprint", (t) => {
459
+ const a = gitWorkspace(t);
460
+ const b = gitWorkspace(t);
461
+ // Same temp folder, same content, same commit shape: only the root path differs.
462
+ const shared = { TMPDIR: a.tmpDir, TEMP: a.tmpDir, TMP: a.tmpDir };
463
+ const runIn = (ws) =>
464
+ spawnSync("bash", [HOOK], {
465
+ input: JSON.stringify({ hook_event_name: "Stop" }),
466
+ env: { ...process.env, CLAUDE_PROJECT_DIR: ws.repo, CLAUDE_PLUGIN_ROOT: path.join(path.dirname(ws.tmpDir), "plugin"), QC_STOP_EXTRA: "", ...shared },
467
+ encoding: "utf8",
468
+ });
469
+ runIn(a);
470
+ runIn(b);
471
+ assert.equal(a.ran().length, 1);
472
+ assert.equal(b.ran().length, 1);
473
+ runIn(a);
474
+ assert.equal(a.ran().length, 1);
475
+ });
@@ -22,7 +22,8 @@ const SNAKE = /\b([a-z][a-z0-9]*_[a-z0-9_]+)\b/g;
22
22
  // Tables are matched by position instead. A single-word table name is
23
23
  // indistinguishable from a keyword by shape, and a stop-list of keywords is the
24
24
  // same trap as a list of column types: the one it misses blocks a unit.
25
- const TABLE_POSITION = /\b(?:from|join|into|update)\s+([a-z][a-z0-9_]*)/g;
25
+ // A name after `from` or `join` that opens a parenthesis is a function, such as `unnest(...)`.
26
+ const TABLE_POSITION = /\b(from|join|into|update)\s+([a-z][a-z0-9_]*)(\s*\()?/g;
26
27
  /** The few words that legally stand where a table would: `do update set`, `join lateral (`. */
27
28
  const NOT_A_TABLE = new Set(["set", "lateral", "only", "select"]);
28
29
  /** Names a statement binds for itself: a common table expression, or an alias. */
@@ -52,6 +53,8 @@ export const SQL_BUILTINS = Object.freeze([
52
53
  "jsonb_set",
53
54
  "current_timestamp",
54
55
  "current_date",
56
+ "clock_timestamp",
57
+ "unnest",
55
58
  ]);
56
59
 
57
60
  /** The text between the parenthesis at `open` and its match. */
@@ -67,10 +70,27 @@ function parens(source, open) {
67
70
  return source.slice(open + 1);
68
71
  }
69
72
 
70
- /** A constraint line declares no column, so only a name followed by a type counts. */
73
+ /** The parts of a column list, split on the commas outside any parenthesis. */
74
+ function topLevelParts(body) {
75
+ const parts = [];
76
+ let depth = 0;
77
+ let start = 0;
78
+ for (let index = 0; index < body.length; index += 1) {
79
+ if (body[index] === "(") depth += 1;
80
+ if (body[index] === ")") depth -= 1;
81
+ if (body[index] === "," && depth === 0) {
82
+ parts.push(body.slice(start, index));
83
+ start = index + 1;
84
+ }
85
+ }
86
+ parts.push(body.slice(start));
87
+ return parts;
88
+ }
89
+
90
+ /** A constraint declares no column, so only a name followed by a type counts. Columns may share a line. */
71
91
  function addColumnLines(columns, body) {
72
- for (const line of body.split("\n")) {
73
- const column = COLUMN_LINE.exec(line.trim());
92
+ for (const part of topLevelParts(blanked(body))) {
93
+ const column = COLUMN_LINE.exec(part.trim());
74
94
  if (column && !CONSTRAINT_OPENER.has(column[1])) columns.add(column[1]);
75
95
  }
76
96
  }
@@ -78,7 +98,9 @@ function addColumnLines(columns, body) {
78
98
  /** @returns {Map<string, Set<string>>} table to its declared columns, across every migration. */
79
99
  export function declaredColumns(sources) {
80
100
  const tables = new Map();
81
- for (const sql of sources) {
101
+ for (const source of sources) {
102
+ // Keywords and unquoted names ignore case, so a migration in capitals declares the same columns.
103
+ const sql = source.toLowerCase();
82
104
  CREATE_TABLE.lastIndex = 0;
83
105
  let match = CREATE_TABLE.exec(sql);
84
106
  while (match !== null) {
@@ -184,7 +206,8 @@ export function usedIdentifiers(source) {
184
206
  TABLE_POSITION.lastIndex = 0;
185
207
  let table = TABLE_POSITION.exec(statement);
186
208
  while (table !== null) {
187
- if (!NOT_A_TABLE.has(table[1])) tables.add(table[1]);
209
+ const isCall = table[3] !== undefined && (table[1] === "from" || table[1] === "join");
210
+ if (!NOT_A_TABLE.has(table[2]) && !isCall) tables.add(table[2]);
188
211
  table = TABLE_POSITION.exec(statement);
189
212
  }
190
213
  }
@@ -60,6 +60,31 @@ test("an add column belongs to its own alter table, never to the one before it",
60
60
  assert.ok(tables.get("sites").has("site_name"));
61
61
  });
62
62
 
63
+ test("a migration in capitals declares the same columns", () => {
64
+ const tables = declaredColumns([ddl, "ALTER TABLE projects ADD COLUMN blackboard_edited boolean NOT NULL DEFAULT false;"]);
65
+ assert.ok(tables.get("projects").has("blackboard_edited"));
66
+ });
67
+
68
+ test("columns that share a line are each declared, a generated column included", () => {
69
+ const tables = declaredColumns([
70
+ `create table results (
71
+ -- one row per item
72
+ item_id uuid not null, sub_pin_id uuid, owner_id uuid generated always as (coalesce(sub_pin_id, pin_id)) stored, checked boolean not null,
73
+ result text not null default 'a, b', corrected_at timestamptz,
74
+ primary key (item_id)
75
+ );`,
76
+ ]);
77
+ assert.deepEqual([...tables.get("results")].sort(), ["checked", "corrected_at", "item_id", "owner_id", "result", "sub_pin_id"]);
78
+ });
79
+
80
+ test("a function in a from clause, and clock_timestamp, are no undeclared names", () => {
81
+ const tables = declaredColumns([ddl]);
82
+ const source = resource(
83
+ "insert into projects (id, created_at) select photo_id, clock_timestamp() from unnest($1::uuid[]) as photo_id",
84
+ );
85
+ assert.deepEqual(checkSqlIdentifiers(tables, [{ path: "r.ts", source }]), []);
86
+ });
87
+
63
88
  test("an agreeing slice has no finding", () => {
64
89
  const tables = declaredColumns([ddl]);
65
90
  const source = resource("select id, tenant_id, code from projects where tenant_id = $1");