architecture-harness 0.20.0 → 0.20.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "architecture-harness",
3
- "version": "0.20.0",
3
+ "version": "0.20.1",
4
4
  "type": "module",
5
5
  "description": "Architecture gates, lint rules and scaffolding for the bounded-vertical-slice architecture",
6
6
  "license": "MIT",
@@ -45,12 +45,12 @@ export function decide(call, platform = process.platform) {
45
45
  const rel = path.relative(fileRoot, file).replaceAll("\\", "/");
46
46
  const { controllerPaths } = workflow.review;
47
47
  if (isControllerPath(rel, controllerPaths, platform)) return null;
48
- const { direct } = workflow;
49
- const lane = direct
50
- ? editLane({ root: fileRoot, base: workflow.base, direct, protectedBranches: workflow.protectedBranches, rel, toolName: call.tool_name, input })
48
+ // The checkout's `swarm.direct` only turns the lane off; `editLane` reads the limits from the base.
49
+ const lane = workflow.direct
50
+ ? editLane({ root: fileRoot, base: workflow.base, protectedBranches: workflow.protectedBranches, rel, toolName: call.tool_name, input })
51
51
  : workflow.directProblem && { problem: workflow.directProblem };
52
52
  if (lane && lane.problem === null) {
53
- return context(`Direct lane: with this edit the branch changes ${sizeLine(lane.size, direct)}, so the fix needs no implementer. Merge it with \`--match-head-commit <sha>\`.`);
53
+ return context(`Direct lane: with this edit the branch changes ${sizeLine(lane.size, lane.direct)}, so the fix needs no implementer. Merge it with \`--match-head-commit <sha>\`.`);
54
54
  }
55
55
  const laneNote = lane ? ` The direct lane of swarm.direct does not apply: ${lane.problem}.` : "";
56
56
  return deny(
@@ -22,9 +22,9 @@ function workspace(t, dispatchConfig = { swarm: { dispatch: {} } }) {
22
22
  git(main, "config", key, value);
23
23
  }
24
24
  writeFileSync(path.join(main, "a.txt"), "a\n");
25
+ if (dispatchConfig !== null) writeFileSync(path.join(main, "qc.config.json"), JSON.stringify(dispatchConfig));
25
26
  git(main, "add", ".");
26
27
  git(main, "commit", "-q", "-m", "init");
27
- writeFileSync(path.join(main, "qc.config.json"), JSON.stringify(dispatchConfig));
28
28
  const linked = path.join(base, "linked");
29
29
  git(main, "worktree", "add", "-q", "-b", "feat/1-x", linked);
30
30
  const scratch = path.join(base, "scratch");
@@ -161,3 +161,25 @@ test("a wrong swarm.direct closes the lane and keeps the guard on", (t) => {
161
161
  assert.match(reason, /^Controller guard: while swarm.dispatch is on/);
162
162
  assert.match(reason, /does not apply: swarm.direct.maxLines in qc.config.json must be a positive whole number/);
163
163
  });
164
+
165
+ test("the lane reads its limits from the base, and names a base that holds no config", (t) => {
166
+ const ws = laneWorkspace(t, { swarm: { dispatch: {}, direct: { maxLines: 5 } } });
167
+ const linked = path.join(ws.linked, "src", "a.ts");
168
+ writeFileSync(path.join(ws.main, "qc.config.json"), JSON.stringify({ swarm: { dispatch: {}, direct: { maxLines: 500 } } }));
169
+ const output = decide(edit(ws.main, linked, "Edit", { tool_input: { file_path: linked, ...small } }));
170
+ assert.match(output?.hookSpecificOutput?.additionalContext ?? "", /the branch changes 2 of 5 lines in 1 of 2 files/);
171
+ writeFileSync(path.join(ws.linked, "qc.config.json"), JSON.stringify({ swarm: { dispatch: {}, direct: { maxLines: 500 } } }));
172
+ const loosened = decide(edit(ws.main, linked, "Edit", { tool_input: { file_path: linked, ...small } }));
173
+ assert.match(denied(loosened) ?? "", /does not apply: qc\.config\.json changes on the branch, so it runs the subagent workflow/);
174
+ const bare = laneWorkspace(t, null);
175
+ writeFileSync(path.join(bare.main, "qc.config.json"), JSON.stringify({ swarm: { dispatch: {} } }));
176
+ const closed = decide(edit(bare.main, path.join(bare.linked, "src", "a.ts"), "Edit", { tool_input: { file_path: path.join(bare.linked, "src", "a.ts"), ...small } }));
177
+ assert.match(denied(closed) ?? "", /does not apply: git cannot read qc\.config\.json at origin\/main/);
178
+ });
179
+
180
+ test("a pending edit of qc.config.json leaves the lane when the controller does not own the file", (t) => {
181
+ const ws = laneWorkspace(t, { swarm: { dispatch: {}, review: { controllerPaths: ["docs/**"] } } });
182
+ const config = path.join(ws.linked, "qc.config.json");
183
+ const reason = denied(decide(edit(ws.main, config, "Edit", { tool_input: { file_path: config, ...small } }))) ?? "";
184
+ assert.match(reason, /does not apply: qc\.config\.json changes on the branch, so it runs the subagent workflow/);
185
+ });
@@ -15,6 +15,9 @@ const KINDS = ["task", "branch"];
15
15
  const GITLINK_MODE = "160000";
16
16
  const SNIFF_BYTES = 8_000;
17
17
  const MAX_TEXT_BYTES = 1_000_000;
18
+ const OBJECT_ID = /^(?:[0-9a-f]{40}|[0-9a-f]{64})$/i;
19
+ const PR_BASE = "the PR base";
20
+ const NOTE_READ_MS = 2_000;
18
21
  // Under the 10 s timeout of a PreToolUse hook, for all git and gh reads of one call: a hook that times out lets its call run.
19
22
  export const LANE_BUDGET_MS = 6_000;
20
23
 
@@ -37,11 +40,24 @@ export function directSettings(swarm = {}) {
37
40
  return settings;
38
41
  }
39
42
 
40
- /** @returns the lane's limits of the checkout that holds `cwd`, or null when it has no config or the lane is off. */
41
- export function directSettingsAt(cwd) {
43
+ /**
44
+ * @returns the lane's limits for the checkout that holds `cwd`, or null when it has no config or the lane is off. The
45
+ * checkout's `swarm.direct` can turn the lane off. The limits are the ones at `worktree.base` when one git read gets
46
+ * them, as the lane judges, and the checkout's own when it does not. The read waits `readMs` at most.
47
+ */
48
+ export function directSettingsAt(cwd, { readMs = NOTE_READ_MS } = {}) {
42
49
  const root = checkoutRootOf(cwd);
43
50
  if (!root || !existsSync(path.join(root, CONFIG_FILE))) return null;
44
- return directSettings(load(root).swarm);
51
+ const config = load(root);
52
+ const local = directSettings(config.swarm);
53
+ if (!local) return null;
54
+ const text = gitOutWithin(readMs, root, "show", `${config.worktree?.base ?? "origin/main"}:${CONFIG_FILE}`);
55
+ if (text === null) return local;
56
+ try {
57
+ return directSettings(JSON.parse(text).swarm);
58
+ } catch {
59
+ return local;
60
+ }
45
61
  }
46
62
 
47
63
  /** The lines of a text as git counts them: a last line with no newline still counts. */
@@ -72,6 +88,32 @@ function gitBy(budgetMs) {
72
88
 
73
89
  const gitFailed = (git, what) => (git.late?.() ? `git ran past the ${LANE_BUDGET_MS} ms budget of the lane` : what);
74
90
 
91
+ /**
92
+ * The limits of `swarm.direct` in `qc.config.json` at the commit `base`, read through `git`. A branch cannot loosen its own
93
+ * limits, so the lane never reads the checkout's file. `label` names the base in a problem.
94
+ * @returns `{ direct }`, or `{ problem }` naming why the lane is closed.
95
+ */
96
+ function directAtBase(root, base, git, label = base) {
97
+ const text = git(root, "show", `${base}:${CONFIG_FILE}`);
98
+ if (text === null) return { problem: gitFailed(git, `git cannot read ${CONFIG_FILE} at ${label}`) };
99
+ try {
100
+ const config = JSON.parse(text);
101
+ if (config === null || typeof config !== "object" || Array.isArray(config)) return { problem: `${CONFIG_FILE} at ${label} is not a JSON object` };
102
+ const { swarm } = config;
103
+ if (swarm !== undefined && (swarm === null || typeof swarm !== "object" || Array.isArray(swarm))) {
104
+ return { problem: `swarm in ${CONFIG_FILE} at ${label} must be an object, got ${JSON.stringify(swarm)}` };
105
+ }
106
+ const direct = directSettings(swarm);
107
+ return direct ? { direct } : { problem: `${KEY} is false at ${label}` };
108
+ } catch (error) {
109
+ return { problem: error instanceof SyntaxError ? `${CONFIG_FILE} at ${label} is not JSON` : error.message };
110
+ }
111
+ }
112
+
113
+ // The checkout also sets `worktree.base` and `swarm.isolation.protectedBranches`, so a branch that edits the file could point `base` at looser settings.
114
+ const touchesConfig = (changes) => [...changes.keys()].some((file) => file.toLowerCase() === CONFIG_FILE);
115
+ const CONFIG_PROBLEM = `${CONFIG_FILE} changes on the branch, so it runs the subagent workflow`;
116
+
75
117
  function startsWithNul(file) {
76
118
  const fd = openSync(file, "r");
77
119
  try {
@@ -152,40 +194,57 @@ export function sizeProblem(changes, direct) {
152
194
 
153
195
  /**
154
196
  * Judges one main-session edit of `rel` in the checkout at `root`. The branch diff and the pending edit together
155
- * must fit, on a branch that is not protected. @returns `{ problem }`, or `{ problem: null, size }` when it fits.
197
+ * must fit the limits at `base`, on a branch that is not protected, and neither may touch `qc.config.json`.
198
+ * @returns `{ problem }`, or `{ problem: null, size, direct }` when it fits.
156
199
  */
157
- export function editLane({ root, base, direct, protectedBranches, rel, toolName, input, budgetMs = LANE_BUDGET_MS }) {
200
+ export function editLane({ root, base, protectedBranches, rel, toolName, input, budgetMs = LANE_BUDGET_MS }) {
158
201
  const git = gitBy(budgetMs);
159
202
  const branch = git(root, "branch", "--show-current");
160
203
  if (branch === null) return { problem: gitFailed(git, "git cannot name the checked-out branch") };
161
204
  if (branch === "") return { problem: "the checkout has a detached head" };
162
205
  if (protectedBranches.includes(branch)) return { problem: `${branch} is a protected branch, so start a branch for the fix` };
163
206
  const fork = git(root, "merge-base", base, "HEAD");
164
- const changes = fork ? changesSince(root, fork, { maxFiles: direct.maxFiles, git }) : null;
207
+ if (!fork) return { problem: gitFailed(git, `git cannot diff the branch against ${base}`) };
208
+ const settings = directAtBase(root, base, git);
209
+ if (!settings.direct) return settings;
210
+ const { direct } = settings;
211
+ const changes = changesSince(root, fork, { maxFiles: direct.maxFiles, git });
165
212
  if (!changes) return { problem: gitFailed(git, `git cannot diff the branch against ${base}`) };
166
213
  const old = toolName === "Write" || toolName === "NotebookEdit" ? fileLines(root, rel) : undefined;
167
214
  const replaced = old === undefined ? 0 : old;
168
215
  const before = changes.has(rel) ? changes.get(rel) : 0;
169
216
  changes.set(rel, before === null || replaced === null ? null : before + replaced + pendingLines(toolName, input));
217
+ if (touchesConfig(changes)) return { problem: CONFIG_PROBLEM };
170
218
  const problem = sizeProblem(changes, direct);
171
- return problem ? { problem } : { problem: null, size: sizeOf(changes) };
219
+ return problem ? { problem } : { problem: null, size: sizeOf(changes), direct };
172
220
  }
173
221
 
174
222
  /**
175
223
  * Judges the merge of head `sha` with no review of the merge kind. A record belongs to the branch when it names
176
224
  * a commit of the branch, or the name of a local branch at `sha`, so an amend or a rebase cannot drop it.
177
225
  * The newest review on the branch must not ask for changes, and no implementer may have worked on it.
178
- * @returns `{ problem }`, or `{ problem: null, size }` when the merge fits.
226
+ * `baseOid` is the commit the PR merges into, as GitHub names it: the lane reads the limits at that commit and
227
+ * measures the branch from its merge base with it, so no local ref decides. A change to `qc.config.json` closes the lane.
228
+ * @returns `{ problem }`, or `{ problem: null, size, direct, review }` when the merge fits. `review` is the
229
+ * APPROVED verdict that a path of `reviewPaths` needed, or null.
179
230
  */
180
- export function mergeLane({ root, base, direct, protectedBranches = [], sha, records, budgetMs = LANE_BUDGET_MS }) {
231
+ export function mergeLane({ root, baseOid, protectedBranches = [], sha, records, budgetMs = LANE_BUDGET_MS }) {
232
+ if (typeof baseOid !== "string" || !OBJECT_ID.test(baseOid)) return { problem: "gh returned no base commit for the PR, so the lane cannot tell what the PR merges into" };
181
233
  const git = gitBy(budgetMs);
182
- const fork = git(root, "merge-base", base, sha);
183
- if (!fork) return { problem: gitFailed(git, `git cannot find the merge base of ${base} and ${sha}`) };
234
+ if (git(root, "cat-file", "-e", `${baseOid}^{commit}`) === null) {
235
+ return { problem: gitFailed(git, `the PR base commit ${baseOid.slice(0, 12)} is not in this repository, so run \`git fetch\``) };
236
+ }
237
+ const fork = git(root, "merge-base", baseOid, sha);
238
+ if (!fork) return { problem: gitFailed(git, `git cannot find the merge base of ${PR_BASE} and ${sha}`) };
239
+ const settings = directAtBase(root, baseOid, git, PR_BASE);
240
+ if (!settings.direct) return settings;
241
+ const { direct } = settings;
184
242
  const history = git(root, "rev-list", `${fork}..${sha}`);
185
243
  const changes = history === null ? null : changesSince(root, fork, { head: sha, git });
186
- if (!changes) return { problem: gitFailed(git, `git cannot diff ${sha} against ${base}`) };
244
+ if (!changes) return { problem: gitFailed(git, `git cannot diff ${sha} against ${PR_BASE}`) };
245
+ if (touchesConfig(changes)) return { problem: CONFIG_PROBLEM };
187
246
  const shas = history.split(/\r?\n/).filter(Boolean);
188
- if (shas.length === 0) return { problem: `${sha} has no commit past ${base}, so the lane cannot measure what the PR merges` };
247
+ if (shas.length === 0) return { problem: `${sha} has no commit past ${PR_BASE}, so the lane cannot measure what the PR merges` };
189
248
  const branches = branchesAt(root, sha, git);
190
249
  if (branches.length === 0) return { problem: gitFailed(git, `no local branch points at ${sha}, so the ledger cannot be tied to its branch`) };
191
250
  const guarded = branches.find((branch) => protectedBranches.includes(branch));
@@ -202,8 +261,9 @@ export function mergeLane({ root, base, direct, protectedBranches = [], sha, rec
202
261
  if (problem) return { problem };
203
262
  const reviewed = pathMatcher(direct.reviewPaths);
204
263
  const needsReview = [...changes.keys()].find((file) => reviewed(file));
205
- if (needsReview !== undefined && latestVerdictFor(records, sha, KINDS)?.verdict !== "APPROVED") {
264
+ const approval = needsReview === undefined ? null : latestVerdictFor(records, sha, KINDS);
265
+ if (needsReview !== undefined && approval?.verdict !== "APPROVED") {
206
266
  return { problem: `${needsReview} matches ${KEY}.reviewPaths, so dispatch the task reviewer (sdd-reviewer) on ${sha}` };
207
267
  }
208
- return { problem: null, size: sizeOf(changes) };
268
+ return { problem: null, size: sizeOf(changes), direct, review: approval };
209
269
  }
@@ -11,13 +11,14 @@ const git = (cwd, ...args) => execFileSync("git", args, { cwd, stdio: "pipe", en
11
11
  const BASE = "origin/main";
12
12
  const lines = (n) => Array.from({ length: n }, (_, i) => `line ${i}`).join("\n") + "\n";
13
13
 
14
- /** A repository with `origin/main` at its first commit and a branch `feat/1-x` checked out. */
15
- function repo(t) {
14
+ /** A repository with `origin/main` at its first commit, which holds a `qc.config.json` with `swarm`, and a branch `feat/1-x` checked out. */
15
+ function repo(t, swarm = { dispatch: {} }, config = JSON.stringify({ swarm })) {
16
16
  const root = realpathSync.native(mkdtempSync(path.join(os.tmpdir(), "qc-direct-")));
17
17
  t.after(() => rmSync(root, { recursive: true, force: true }));
18
18
  git(root, "init", "-q", "-b", "main");
19
19
  for (const [key, value] of [["user.name", "qc"], ["user.email", "qc@example.com"], ["commit.gpgsign", "false"]]) git(root, "config", key, value);
20
20
  mkdirSync(path.join(root, "src"));
21
+ if (config !== null) writeFileSync(path.join(root, "qc.config.json"), config);
21
22
  writeFileSync(path.join(root, "src", "a.ts"), lines(10));
22
23
  git(root, "add", ".");
23
24
  git(root, "commit", "-q", "-m", "init");
@@ -34,8 +35,11 @@ const commit = (root, file, text) => {
34
35
  return git(root, "rev-parse", "HEAD");
35
36
  };
36
37
 
37
- const edit = (root, rel, input, direct = directDefaults) =>
38
- editLane({ root, base: BASE, direct, protectedBranches: ["main"], rel, toolName: "Edit", input });
38
+ /** The commit `origin/main` names: the base commit a PR merges into. */
39
+ const oidOf = (root) => git(root, "rev-parse", BASE);
40
+
41
+ const edit = (root, rel, input) =>
42
+ editLane({ root, base: BASE, protectedBranches: ["main"], rel, toolName: "Edit", input });
39
43
 
40
44
  test("the settings fill their defaults, accept false, and name a wrong key", () => {
41
45
  assert.deepEqual(directSettings({}), directDefaults);
@@ -98,7 +102,7 @@ test("a protected branch, a detached head, or a missing base ref leaves the lane
98
102
  test("a small head with no review merges through the lane", (t) => {
99
103
  const root = repo(t);
100
104
  const sha = commit(root, "src/a.ts", lines(10).replace("line 3", "line three"));
101
- const fit = mergeLane({ root, base: BASE, direct: directDefaults, sha, records: [] });
105
+ const fit = mergeLane({ root, baseOid: oidOf(root), sha, records: [] });
102
106
  assert.equal(fit.problem, null);
103
107
  assert.deepEqual(fit.size, { files: 1, lines: 2 });
104
108
  });
@@ -107,21 +111,20 @@ test("a review that is not APPROVED, an implementer commit, or a large head keep
107
111
  const root = repo(t);
108
112
  const sha = commit(root, "src/a.ts", lines(10).replace("line 3", "line three"));
109
113
  const changes = [{ type: "verdict", kind: "task", verdict: "CHANGES_REQUIRED", sha }];
110
- assert.match(mergeLane({ root, base: BASE, direct: directDefaults, sha, records: changes }).problem, /latest review on the branch, of .*, is CHANGES_REQUIRED/);
114
+ assert.match(mergeLane({ root, baseOid: oidOf(root), sha, records: changes }).problem, /latest review on the branch, of .*, is CHANGES_REQUIRED/);
111
115
  const stop = [{ type: "stop", role: "implementer", head: sha, branch: "feat/1-x" }];
112
- assert.match(mergeLane({ root, base: BASE, direct: directDefaults, sha, records: stop }).problem, /an implementer worked on the branch/);
116
+ assert.match(mergeLane({ root, baseOid: oidOf(root), sha, records: stop }).problem, /an implementer worked on the branch/);
113
117
  const big = commit(root, "src/b.ts", lines(30));
114
- assert.match(mergeLane({ root, base: BASE, direct: directDefaults, sha: big, records: [] }).problem, /32 of 20 lines/);
115
- assert.match(mergeLane({ root, base: "origin/none", direct: directDefaults, sha, records: [] }).problem, /merge base of origin\/none/);
118
+ assert.match(mergeLane({ root, baseOid: oidOf(root), sha: big, records: [] }).problem, /32 of 20 lines/);
119
+ assert.match(mergeLane({ root, baseOid: "1".repeat(40), sha, records: [] }).problem, /is not in this repository, so run `git fetch`/);
116
120
  });
117
121
 
118
122
  test("a head that touches a review path needs one APPROVED review of either kind", (t) => {
119
- const root = repo(t);
123
+ const root = repo(t, { dispatch: {}, direct: { reviewPaths: ["src/**"] } });
120
124
  const sha = commit(root, "src/a.ts", lines(10).replace("line 3", "line three"));
121
- const direct = { ...directDefaults, reviewPaths: ["src/**"] };
122
- assert.match(mergeLane({ root, base: BASE, direct, sha, records: [] }).problem, /src\/a\.ts matches swarm\.direct\.reviewPaths/);
125
+ assert.match(mergeLane({ root, baseOid: oidOf(root), sha, records: [] }).problem, /src\/a\.ts matches swarm\.direct\.reviewPaths/);
123
126
  const approved = [{ type: "verdict", kind: "task", verdict: "APPROVED", sha }];
124
- assert.equal(mergeLane({ root, base: BASE, direct, sha, records: approved }).problem, null);
127
+ assert.equal(mergeLane({ root, baseOid: oidOf(root), sha, records: approved }).problem, null);
125
128
  });
126
129
 
127
130
  test("a commit after a review that asked for changes stays out of the lane until a review approves it", (t) => {
@@ -129,9 +132,9 @@ test("a commit after a review that asked for changes stays out of the lane until
129
132
  const first = commit(root, "src/a.ts", lines(10).replace("line 3", "line three"));
130
133
  const records = [{ type: "verdict", kind: "task", verdict: "CHANGES_REQUIRED", sha: first }];
131
134
  const second = commit(root, "src/a.ts", lines(10).replace("line 3", "line 3!"));
132
- assert.match(mergeLane({ root, base: BASE, direct: directDefaults, sha: second, records }).problem, /CHANGES_REQUIRED/);
135
+ assert.match(mergeLane({ root, baseOid: oidOf(root), sha: second, records }).problem, /CHANGES_REQUIRED/);
133
136
  records.push({ type: "verdict", kind: "task", verdict: "APPROVED", sha: second });
134
- assert.equal(mergeLane({ root, base: BASE, direct: directDefaults, sha: second, records }).problem, null);
137
+ assert.equal(mergeLane({ root, baseOid: oidOf(root), sha: second, records }).problem, null);
135
138
  });
136
139
 
137
140
  test("an amend after a review that asked for changes, or after an implementer commit, stays out of the lane", (t) => {
@@ -142,15 +145,15 @@ test("an amend after a review that asked for changes, or after an implementer co
142
145
  writeFileSync(path.join(root, "src", "a.ts"), lines(10).replace("line 3", "line 3!"));
143
146
  git(root, "commit", "-q", "--amend", "-am", "amended");
144
147
  const amended = git(root, "rev-parse", "HEAD");
145
- assert.match(mergeLane({ root, base: BASE, direct: directDefaults, sha: amended, records: verdicts }).problem, /CHANGES_REQUIRED/);
146
- assert.match(mergeLane({ root, base: BASE, direct: directDefaults, sha: amended, records: stops }).problem, /an implementer worked/);
148
+ assert.match(mergeLane({ root, baseOid: oidOf(root), sha: amended, records: verdicts }).problem, /CHANGES_REQUIRED/);
149
+ assert.match(mergeLane({ root, baseOid: oidOf(root), sha: amended, records: stops }).problem, /an implementer worked/);
147
150
  });
148
151
 
149
152
  test("a head that no local branch points at stays out of the lane", (t) => {
150
153
  const root = repo(t);
151
154
  const sha = commit(root, "src/a.ts", lines(10).replace("line 3", "line three"));
152
155
  git(root, "reset", "-q", "--hard", "HEAD~1");
153
- assert.match(mergeLane({ root, base: BASE, direct: directDefaults, sha, records: [] }).problem, /no local branch points at/);
156
+ assert.match(mergeLane({ root, baseOid: oidOf(root), sha, records: [] }).problem, /no local branch points at/);
154
157
  });
155
158
 
156
159
  test("a binary file, a submodule, or an untracked binary has no size the lane can judge", (t) => {
@@ -158,39 +161,129 @@ test("a binary file, a submodule, or an untracked binary has no size the lane ca
158
161
  writeFileSync(path.join(root, "src", "lib.dll"), Buffer.from([0, 1, 2, 3]));
159
162
  assert.match(edit(root, "src/a.ts", { old_string: "a", new_string: "b" }).problem, /src\/lib\.dll is a binary file/);
160
163
  const binary = commit(root, "src/lib.dll", Buffer.from([0, 1, 2, 3]));
161
- assert.match(mergeLane({ root, base: BASE, direct: directDefaults, sha: binary, records: [] }).problem, /src\/lib\.dll is a binary file/);
164
+ assert.match(mergeLane({ root, baseOid: oidOf(root), sha: binary, records: [] }).problem, /src\/lib\.dll is a binary file/);
162
165
  git(root, "reset", "-q", "--hard", "HEAD~1");
163
166
  git(root, "update-index", "--add", "--cacheinfo", `160000,${git(root, "rev-parse", "HEAD")},vendor/sub`);
164
167
  git(root, "commit", "-q", "-m", "submodule");
165
- assert.match(mergeLane({ root, base: BASE, direct: directDefaults, sha: git(root, "rev-parse", "HEAD"), records: [] }).problem, /vendor\/sub is a binary file, a large file, or a submodule/);
168
+ assert.match(mergeLane({ root, baseOid: oidOf(root), sha: git(root, "rev-parse", "HEAD"), records: [] }).problem, /vendor\/sub is a binary file, a large file, or a submodule/);
166
169
  });
167
170
 
168
171
  test("a lane that runs out of time closes", (t) => {
169
172
  const root = repo(t);
170
173
  const sha = commit(root, "src/a.ts", lines(10).replace("line 3", "line three"));
171
- assert.match(editLane({ root, base: BASE, direct: directDefaults, protectedBranches: [], rel: "src/a.ts", toolName: "Edit", input: {}, budgetMs: 0 }).problem, /budget of the lane/);
172
- assert.match(mergeLane({ root, base: BASE, direct: directDefaults, sha, records: [], budgetMs: 0 }).problem, /budget of the lane/);
174
+ assert.match(editLane({ root, base: BASE, protectedBranches: [], rel: "src/a.ts", toolName: "Edit", input: {}, budgetMs: 0 }).problem, /budget of the lane/);
175
+ assert.match(mergeLane({ root, baseOid: oidOf(root), sha, records: [], budgetMs: 0 }).problem, /budget of the lane/);
173
176
  });
174
177
 
175
178
  test("a Write over a file counts the lines it replaces", (t) => {
176
179
  const root = repo(t);
177
- const write = editLane({ root, base: BASE, direct: directDefaults, protectedBranches: [], rel: "src/a.ts", toolName: "Write", input: { content: lines(11) } });
180
+ const write = editLane({ root, base: BASE, protectedBranches: [], rel: "src/a.ts", toolName: "Write", input: { content: lines(11) } });
178
181
  assert.match(write.problem, /21 of 20 lines/);
179
182
  });
180
183
 
181
184
  test("a head with no commit past the base, the head of a protected branch, or a branch an implementer was sent to stays out of the lane", (t) => {
182
185
  const root = repo(t);
183
186
  const fork = git(root, "rev-parse", "HEAD");
184
- assert.match(mergeLane({ root, base: BASE, direct: directDefaults, sha: fork, records: [] }).problem, /has no commit past origin\/main/);
187
+ assert.match(mergeLane({ root, baseOid: oidOf(root), sha: fork, records: [] }).problem, /has no commit past the PR base/);
185
188
  const sha = commit(root, "src/a.ts", lines(10).replace("line 3", "line three"));
186
- assert.match(mergeLane({ root, base: BASE, direct: directDefaults, protectedBranches: ["feat/1-x"], sha, records: [] }).problem, /head of the protected branch feat\/1-x/);
189
+ assert.match(mergeLane({ root, baseOid: oidOf(root), protectedBranches: ["feat/1-x"], sha, records: [] }).problem, /head of the protected branch feat\/1-x/);
187
190
  const sent = [{ type: "dispatch", task: true, branch: "feat/1-x", head: fork }];
188
- assert.match(mergeLane({ root, base: BASE, direct: directDefaults, sha, records: sent }).problem, /an implementer worked on the branch/);
191
+ assert.match(mergeLane({ root, baseOid: oidOf(root), sha, records: sent }).problem, /an implementer worked on the branch/);
189
192
  });
190
193
 
191
194
  test("a Write over a tracked binary has no size the lane can judge", (t) => {
192
195
  const root = repo(t);
193
196
  commit(root, "src/lib.dll", Buffer.from([0, 1, 2, 3]));
194
- const write = editLane({ root, base: BASE, direct: directDefaults, protectedBranches: [], rel: "src/lib.dll", toolName: "Write", input: { content: "x" } });
197
+ const write = editLane({ root, base: BASE, protectedBranches: [], rel: "src/lib.dll", toolName: "Write", input: { content: "x" } });
195
198
  assert.match(write.problem, /src\/lib\.dll is a binary file/);
196
199
  });
200
+
201
+ const withDirect = (direct) => ({ dispatch: {}, direct });
202
+
203
+ test("the lane judges with the limits of the base, not the limits of the checkout", (t) => {
204
+ const root = repo(t, withDirect({ maxLines: 5 }));
205
+ const big = { old_string: "a\n".repeat(3), new_string: "b\n".repeat(3) };
206
+ const fit = edit(root, "src/a.ts", big);
207
+ assert.match(fit.problem, /changes 6 of 5 lines in 1 of 2 files/);
208
+ const sha = commit(root, "src/a.ts", lines(10).replace("line 3", "x\n".repeat(5)));
209
+ assert.match(mergeLane({ root, baseOid: oidOf(root), sha, records: [] }).problem, /changes 7 of 5 lines/);
210
+ });
211
+
212
+ test("a base that sets reviewPaths holds a merge to its review, though the checkout drops the setting", (t) => {
213
+ const root = repo(t, withDirect({ reviewPaths: ["**"] }));
214
+ const sha = commit(root, "src/a.ts", lines(10).replace("line 3", "line three"));
215
+ writeFileSync(path.join(root, "qc.config.json"), JSON.stringify({ swarm: { dispatch: {} } }));
216
+ const loose = mergeLane({ root, baseOid: oidOf(root), sha, records: [] });
217
+ assert.match(loose.problem, /src\/a\.ts matches swarm\.direct\.reviewPaths/);
218
+ const approved = [{ type: "verdict", kind: "task", verdict: "APPROVED", sha }];
219
+ const fit = mergeLane({ root, baseOid: oidOf(root), sha, records: approved });
220
+ assert.equal(fit.problem, null);
221
+ assert.equal(fit.review.kind, "task");
222
+ assert.equal(fit.review.sha, sha);
223
+ });
224
+
225
+ test("a base with no qc.config.json closes the lane", (t) => {
226
+ const root = repo(t, undefined, null);
227
+ const sha = commit(root, "src/a.ts", lines(10).replace("line 3", "line three"));
228
+ assert.match(mergeLane({ root, baseOid: oidOf(root), sha, records: [] }).problem, /git cannot read qc\.config\.json at the PR base/);
229
+ assert.match(edit(root, "src/a.ts", { old_string: "a", new_string: "b" }).problem, /git cannot read qc\.config\.json at origin\/main/);
230
+ });
231
+
232
+ test("a base config that is not JSON, or holds a wrong swarm or swarm.direct, closes the lane and names the cause", (t) => {
233
+ for (const [config, cause] of [
234
+ ["{ not json", /qc\.config\.json at (?:origin\/main|the PR base) is not JSON/],
235
+ ["null", /qc\.config\.json at (?:origin\/main|the PR base) is not a JSON object/],
236
+ [JSON.stringify({ swarm: null }), /swarm in qc\.config\.json at (?:origin\/main|the PR base) must be an object, got null/],
237
+ [JSON.stringify({ swarm: [] }), /swarm in qc\.config\.json at (?:origin\/main|the PR base) must be an object, got \[\]/],
238
+ [JSON.stringify({ swarm: { direct: { maxLines: 0 } } }), /swarm\.direct\.maxLines in qc\.config\.json must be a positive whole number/],
239
+ ]) {
240
+ const root = repo(t, undefined, config);
241
+ const sha = commit(root, "src/a.ts", lines(10).replace("line 3", "line three"));
242
+ assert.match(mergeLane({ root, baseOid: oidOf(root), sha, records: [] }).problem, cause);
243
+ assert.match(edit(root, "src/a.ts", { old_string: "a", new_string: "b" }).problem, cause);
244
+ }
245
+ });
246
+
247
+ test("a base that sets swarm.direct to false closes the lane", (t) => {
248
+ const root = repo(t, withDirect(false));
249
+ const sha = commit(root, "src/a.ts", lines(10).replace("line 3", "line three"));
250
+ assert.match(mergeLane({ root, baseOid: oidOf(root), sha, records: [] }).problem, /swarm\.direct is false at the PR base/);
251
+ assert.match(edit(root, "src/a.ts", { old_string: "a", new_string: "b" }).problem, /swarm\.direct is false at origin\/main/);
252
+ });
253
+
254
+ test("a branch that changes qc.config.json leaves the lane, and so does a pending edit of it", (t) => {
255
+ const root = repo(t);
256
+ const sha = commit(root, "qc.config.json", JSON.stringify({ swarm: { dispatch: {}, direct: { maxLines: 500 } } }));
257
+ assert.match(mergeLane({ root, baseOid: oidOf(root), sha, records: [] }).problem, /qc\.config\.json changes on the branch, so it runs the subagent workflow/);
258
+ assert.match(edit(root, "src/a.ts", { old_string: "a", new_string: "b" }).problem, /qc\.config\.json changes on the branch, so it runs the subagent workflow/);
259
+ const clean = repo(t);
260
+ assert.match(edit(clean, "qc.config.json", { old_string: "{", new_string: "{ " }).problem, /qc\.config\.json changes on the branch/);
261
+ assert.match(editLane({ root: clean, base: BASE, protectedBranches: [], rel: "qc.config.json", toolName: "Write", input: { content: "{}" } }).problem, /qc\.config\.json changes on the branch/);
262
+ });
263
+
264
+ test("a config change in a folder below the root does not close the lane", (t) => {
265
+ const root = repo(t);
266
+ const sha = commit(root, "pkg/qc.config.json", "{}\n");
267
+ assert.equal(mergeLane({ root, baseOid: oidOf(root), sha, records: [] }).problem, null);
268
+ });
269
+
270
+ test("a merge with no base commit, or a base commit the repository lacks, closes the lane", (t) => {
271
+ const root = repo(t);
272
+ const sha = commit(root, "src/a.ts", lines(10).replace("line 3", "line three"));
273
+ assert.match(mergeLane({ root, baseOid: undefined, sha, records: [] }).problem, /no base commit/);
274
+ assert.match(mergeLane({ root, baseOid: "origin/main", sha, records: [] }).problem, /no base commit/);
275
+ assert.match(mergeLane({ root, baseOid: "1".repeat(40), sha, records: [] }).problem, /the PR base commit 111111111111 is not in this repository, so run `git fetch`/);
276
+ });
277
+
278
+ test("the lane measures the merge from the base commit it is given, not from a local ref", (t) => {
279
+ const root = repo(t, withDirect({ reviewPaths: ["**"] }));
280
+ git(root, "switch", "-q", "-c", "tip", BASE);
281
+ writeFileSync(path.join(root, "qc.config.json"), JSON.stringify({ swarm: { dispatch: {} } }));
282
+ git(root, "commit", "-q", "-am", "loosen");
283
+ git(root, "update-ref", "refs/remotes/origin/main", "HEAD");
284
+ const sha = commit(root, "src/a.ts", lines(10).replace("line 3", "line three"));
285
+ const loose = mergeLane({ root, baseOid: git(root, "rev-parse", BASE), sha, records: [] });
286
+ assert.equal(loose.problem, null, "the local ref is the base here");
287
+ const strict = mergeLane({ root, baseOid: git(root, "rev-parse", `${BASE}~1`), sha, records: [] });
288
+ assert.match(strict.problem, /qc\.config\.json changes on the branch/);
289
+ });
@@ -33,10 +33,17 @@ function directLine(cwd) {
33
33
  }
34
34
  return direct
35
35
  ? ` A fix of at most ${direct.maxLines} changed lines in ${direct.maxFiles} ${direct.maxFiles === 1 ? "file" : "files"} needs no subagent: edit it in the main session on a branch, ` +
36
- "and merge it with `--match-head-commit <sha>` and no review. The guards measure the branch against its base (swarm.direct)."
36
+ `and merge it with \`--match-head-commit <sha>\`${reviewClause(direct.reviewPaths)} The guards measure the branch against its base (swarm.direct).`
37
37
  : "";
38
38
  }
39
39
 
40
+ /** The end of the merge sentence: no review, or the one APPROVED review that `swarm.direct.reviewPaths` asks for. */
41
+ function reviewClause(reviewPaths) {
42
+ if (reviewPaths.length === 0) return " and no review.";
43
+ if (reviewPaths.includes("**")) return ". Every direct merge needs one APPROVED review of the head (sdd-reviewer).";
44
+ return ". A merge needs one APPROVED review of the head (sdd-reviewer) when a changed path matches swarm.direct.reviewPaths.";
45
+ }
46
+
40
47
  /** The explore tools, or null when the section is off, names none, or does not parse. */
41
48
  function exploreToolsAt(cwd) {
42
49
  let settings;
@@ -193,3 +193,46 @@ test("a swarm.direct section that does not parse names the key and leaves the re
193
193
  assert.ok(text.startsWith(REMINDER));
194
194
  assert.match(text, /Direct lane is off: swarm\.direct\.maxLines/);
195
195
  });
196
+
197
+ test("the direct lane note says when a merge needs a review", (t) => {
198
+ const [none, some, every] = checkouts(t, [
199
+ { swarm: { dispatch: {} } },
200
+ { swarm: { dispatch: {}, direct: { reviewPaths: ["src/**", "api/**"] } } },
201
+ { swarm: { dispatch: {}, direct: { reviewPaths: ["**"] } } },
202
+ ]);
203
+ const noteOf = (dir) => decide(start(dir, "startup")).hookSpecificOutput.additionalContext;
204
+ assert.match(noteOf(none), /--match-head-commit <sha>` and no review\./);
205
+ const partial = noteOf(some);
206
+ assert.match(partial, /--match-head-commit <sha>`\. A merge needs one APPROVED review of the head \(sdd-reviewer\) when a changed path matches swarm\.direct\.reviewPaths\./);
207
+ assert.doesNotMatch(partial, /no review/);
208
+ const all = noteOf(every);
209
+ assert.match(all, /--match-head-commit <sha>`\. Every direct merge needs one APPROVED review of the head \(sdd-reviewer\)\./);
210
+ assert.doesNotMatch(all, /no review/);
211
+ });
212
+
213
+ test("the direct lane note reads the limits at worktree.base, and the checkout's when that read fails", (t) => {
214
+ const [dir] = checkouts(t, [{ swarm: { dispatch: {}, direct: { maxLines: 5, reviewPaths: ["**"] } } }]);
215
+ const git = (...args) => execFileSync("git", args, { cwd: dir, stdio: "pipe" });
216
+ for (const [key, value] of [["user.name", "qc"], ["user.email", "qc@example.com"], ["commit.gpgsign", "false"]]) git("config", key, value);
217
+ git("add", ".");
218
+ git("commit", "-q", "-m", "init");
219
+ git("update-ref", "refs/remotes/origin/main", "HEAD");
220
+ writeFileSync(path.join(dir, "qc.config.json"), JSON.stringify({ swarm: { dispatch: {}, direct: { maxLines: 500 } } }));
221
+ const noteOf = () => decide(start(dir, "startup")).hookSpecificOutput.additionalContext;
222
+ const text = noteOf();
223
+ assert.match(text, /at most 5 changed lines in 2 files needs no subagent/);
224
+ assert.match(text, /Every direct merge needs one APPROVED review/);
225
+ git("update-ref", "-d", "refs/remotes/origin/main");
226
+ assert.match(noteOf(), /at most 500 changed lines in 2 files needs no subagent/);
227
+ });
228
+
229
+ test("a local swarm.direct of false turns the direct lane note off, whatever the base says", (t) => {
230
+ const [dir] = checkouts(t, [{ swarm: { dispatch: {} } }]);
231
+ const git = (...args) => execFileSync("git", args, { cwd: dir, stdio: "pipe" });
232
+ for (const [key, value] of [["user.name", "qc"], ["user.email", "qc@example.com"], ["commit.gpgsign", "false"]]) git("config", key, value);
233
+ git("add", ".");
234
+ git("commit", "-q", "-m", "init");
235
+ git("update-ref", "refs/remotes/origin/main", "HEAD");
236
+ writeFileSync(path.join(dir, "qc.config.json"), JSON.stringify({ swarm: { dispatch: {}, direct: false } }));
237
+ assert.doesNotMatch(decide(start(dir, "startup")).hookSpecificOutput.additionalContext, /needs no subagent/);
238
+ });
@@ -60,33 +60,35 @@ export function mergeVerdict(merges, records, kind, lane = null) {
60
60
  /** @returns the reason the first of `merges` may not run, or null when each pins an approved head. */
61
61
  export const mergeRefusal = (merges, records, kind, lane = null) => mergeVerdict(merges, records, kind, lane).refusal;
62
62
 
63
- /** The base branch the PR of `merge` merges into, as gh reads it before the merge, or null. */
63
+ /** The base the PR of `merge` merges into, as gh reads it before the merge: `{ name, oid }`, or null. `oid` is null when gh names none. */
64
64
  function prBaseOf(merge, cwd, gh, timeoutMs) {
65
- const args = ["pr", "view", ...(merge.selector ? [merge.selector] : []), ...(merge.repo ? ["-R", merge.repo] : []), "--json", "baseRefName"];
65
+ const args = ["pr", "view", ...(merge.selector ? [merge.selector] : []), ...(merge.repo ? ["-R", merge.repo] : []), "--json", "baseRefName,baseRefOid"];
66
66
  try {
67
- return JSON.parse(gh(args, { cwd, env: merge.env, timeoutMs }) ?? "null")?.baseRefName ?? null;
67
+ const pr = JSON.parse(gh(args, { cwd, env: merge.env, timeoutMs }) ?? "null");
68
+ return typeof pr?.baseRefName === "string" ? { name: pr.baseRefName, oid: pr.baseRefOid ?? null } : null;
68
69
  } catch {
69
70
  return null;
70
71
  }
71
72
  }
72
73
 
73
74
  /**
74
- * The direct lane of one workflow, as `mergeVerdict` asks it. Null while `swarm.direct` is false; a wrong value
75
- * names itself. The lane measures against `worktree.base`, so a PR into another base stays out of it.
76
- * The merges of one call share one time budget.
75
+ * The direct lane of one workflow, as `mergeVerdict` asks it. Null while the checkout's `swarm.direct` is false; a wrong
76
+ * value names itself. The lane judges against the base commit that gh names for the PR, so no local ref decides.
77
+ * A PR into a branch other than `worktree.base` stays out of it. The merges of one call share one time budget.
77
78
  */
78
79
  function laneOf(workflow, records, cwd, gh) {
79
80
  const { direct, directProblem, base } = workflow;
80
81
  if (!direct) return directProblem ? () => ({ problem: directProblem }) : null;
81
82
  const deadline = Date.now() + LANE_BUDGET_MS;
82
83
  return (merge) => {
83
- const fit = mergeLane({ root: workflow.root, base, direct, protectedBranches: workflow.protectedBranches, sha: merge.sha, records, budgetMs: deadline - Date.now() });
84
- if (fit.problem !== null) return fit;
85
84
  const left = deadline - Date.now();
86
- const prBase = left > 0 ? prBaseOf(merge, cwd, gh, left) : null;
87
- if (prBase === null) return { problem: "gh cannot read the base branch of the PR within the time budget of the lane" };
88
- if (prBase !== base && `origin/${prBase}` !== base) return { problem: `the PR merges into ${prBase}, and the lane measures against ${base}` };
89
- return { problem: null, note: `Merge guard: ${merge.sha} merges through the direct lane, with ${sizeLine(fit.size, direct)} and no review.` };
85
+ const pr = left > 0 ? prBaseOf(merge, cwd, gh, left) : null;
86
+ if (pr === null) return { problem: "gh cannot read the base branch of the PR within the time budget of the lane" };
87
+ if (pr.name !== base && `origin/${pr.name}` !== base) return { problem: `the PR merges into ${pr.name}, and the lane measures against ${base}` };
88
+ const fit = mergeLane({ root: workflow.root, baseOid: pr.oid, protectedBranches: workflow.protectedBranches, sha: merge.sha, records, budgetMs: deadline - Date.now() });
89
+ if (fit.problem !== null) return fit;
90
+ const reviewed = fit.review ? `the APPROVED ${fit.review.kind} review of ${fit.review.sha}` : "no review";
91
+ return { problem: null, note: `Merge guard: ${merge.sha} merges through the direct lane, with ${sizeLine(fit.size, fit.direct)} and ${reviewed}.` };
90
92
  };
91
93
  }
92
94
 
@@ -346,38 +346,42 @@ test("with swarm.dispatch off, a merge whose command failed after GitHub merged
346
346
  assert.deepEqual(readLedger(ws.ledgerOf(ws.off)).map(({ type, pr }) => ({ type, pr })), [{ type: "merge", pr: 60 }]);
347
347
  });
348
348
 
349
- /** A checkout with the checks on, `origin/main` at its first commit, and a small fix on `feat/1-x`. */
350
- function laneRepo(t, config = { swarm: { dispatch: {} } }) {
349
+ /** A checkout with the checks on, `origin/main` at its first commit, and a small fix on `feat/1-x`. `tracked: false` keeps the config out of that commit. */
350
+ function laneRepo(t, config = { swarm: { dispatch: {} } }, { tracked = true } = {}) {
351
351
  const dir = realpathSync.native(mkdtempSync(path.join(os.tmpdir(), "qc-merge-lane-")));
352
352
  t.after(() => rmSync(dir, { recursive: true, force: true }));
353
353
  const git = (...args) => execFileSync("git", args, { cwd: dir, stdio: "pipe", encoding: "utf8" }).trim();
354
354
  git("init", "-q", "-b", "main");
355
355
  for (const [key, value] of [["user.name", "qc"], ["user.email", "qc@example.com"], ["commit.gpgsign", "false"]]) git("config", key, value);
356
- writeFileSync(path.join(dir, "qc.config.json"), JSON.stringify(config));
356
+ if (tracked) writeFileSync(path.join(dir, "qc.config.json"), JSON.stringify(config));
357
357
  writeFileSync(path.join(dir, "a.ts"), "one\ntwo\n");
358
358
  git("add", ".");
359
359
  git("commit", "-q", "-m", "init");
360
+ if (!tracked) writeFileSync(path.join(dir, "qc.config.json"), JSON.stringify(config));
360
361
  git("update-ref", "refs/remotes/origin/main", "HEAD");
361
362
  git("switch", "-q", "-c", "feat/1-x");
362
363
  writeFileSync(path.join(dir, "a.ts"), "one\n2\n");
363
364
  git("commit", "-q", "-am", "fix");
364
- return { dir, head: git("rev-parse", "HEAD"), ledger: path.join(dir, ".git", "qc", "ledger.jsonl") };
365
+ return { dir, head: git("rev-parse", "HEAD"), base: git("rev-parse", "origin/main"), ledger: path.join(dir, ".git", "qc", "ledger.jsonl") };
365
366
  }
366
367
 
368
+ /** What `gh pr view --json baseRefName,baseRefOid` answers for a PR into `main` whose base commit is `oid`. */
369
+ const prView = (oid, name = "main") => JSON.stringify({ baseRefName: name, baseRefOid: oid });
370
+
367
371
  test("a small head with no review merges through the direct lane, with a note", (t) => {
368
- const { dir, head } = laneRepo(t);
369
- const { gh, calls } = fakeGh(JSON.stringify({ baseRefName: "main" }));
372
+ const { dir, head, base } = laneRepo(t);
373
+ const { gh, calls } = fakeGh(prView(base));
370
374
  const output = decide(shell(dir, `gh pr merge 7 --squash --match-head-commit ${head}`), { gh });
371
375
  assert.equal(denied(output), null);
372
- assert.deepEqual(calls[0].args, ["pr", "view", "7", "--json", "baseRefName"]);
376
+ assert.deepEqual(calls[0].args, ["pr", "view", "7", "--json", "baseRefName,baseRefOid"]);
373
377
  assert.match(output?.hookSpecificOutput?.additionalContext ?? "", /merges through the direct lane, with 2 of 20 lines in 1 of 2 files and no review/);
374
378
  assert.match(denied(decide(shell(dir, "gh pr merge 7 --squash"))) ?? "", /--match-head-commit <sha>/);
375
379
  });
376
380
 
377
381
  test("a review that asked for changes keeps a small head out of the direct lane", (t) => {
378
- const { dir, head, ledger } = laneRepo(t);
382
+ const { dir, head, base, ledger } = laneRepo(t);
379
383
  appendRecord(ledger, verdict("task", "CHANGES_REQUIRED", head));
380
- const reason = denied(decide(shell(dir, `gh pr merge 7 --squash --match-head-commit ${head}`))) ?? "";
384
+ const reason = denied(decide(shell(dir, `gh pr merge 7 --squash --match-head-commit ${head}`), { gh: fakeGh(prView(base)).gh })) ?? "";
381
385
  assert.match(reason, /no APPROVED branch review/);
382
386
  assert.match(reason, /The direct lane of swarm\.direct does not apply: the latest review on the branch, of .*, is CHANGES_REQUIRED/);
383
387
  });
@@ -397,8 +401,93 @@ test("a wrong swarm.direct closes the lane and keeps the merge guard on", (t) =>
397
401
  });
398
402
 
399
403
  test("a PR into another base, or a PR whose base gh cannot read, stays out of the direct lane", (t) => {
400
- const { dir, head } = laneRepo(t);
404
+ const { dir, head, base } = laneRepo(t);
401
405
  const merge = shell(dir, `gh pr merge 7 --squash --match-head-commit ${head}`);
402
- assert.match(denied(decide(merge, { gh: fakeGh(JSON.stringify({ baseRefName: "release/2.0" })).gh })) ?? "", /the PR merges into release\/2\.0, and the lane measures against origin\/main/);
406
+ assert.match(denied(decide(merge, { gh: fakeGh(prView(base, "release/2.0")).gh })) ?? "", /the PR merges into release\/2\.0, and the lane measures against origin\/main/);
403
407
  assert.match(denied(decide(merge, { gh: fakeGh(null).gh })) ?? "", /gh cannot read the base branch of the PR/);
404
408
  });
409
+
410
+ test("a head that a review path covers merges through the lane after an APPROVED review, and the note names that review", (t) => {
411
+ const { dir, head, base, ledger } = laneRepo(t, { swarm: { dispatch: {}, direct: { reviewPaths: ["**"] } } });
412
+ const merge = shell(dir, `gh pr merge 7 --squash --match-head-commit ${head}`);
413
+ const gh = fakeGh(prView(base)).gh;
414
+ assert.match(denied(decide(merge, { gh })) ?? "", /a\.ts matches swarm\.direct\.reviewPaths/);
415
+ appendRecord(ledger, verdict("task", "APPROVED", head));
416
+ const output = decide(merge, { gh });
417
+ assert.equal(denied(output), null);
418
+ const note = output?.hookSpecificOutput?.additionalContext ?? "";
419
+ assert.match(note, new RegExp(`merges through the direct lane, with 2 of 20 lines in 1 of 2 files and the APPROVED task review of ${head}\.`));
420
+ assert.doesNotMatch(note, /no review/);
421
+ });
422
+
423
+ test("the merge guard judges with the limits of the base, so a loosened checkout does not open the lane", (t) => {
424
+ const { dir, head, base } = laneRepo(t, { swarm: { dispatch: {}, direct: { reviewPaths: ["**"] } } });
425
+ writeFileSync(path.join(dir, "qc.config.json"), JSON.stringify({ swarm: { dispatch: {} } }));
426
+ const reason = denied(decide(shell(dir, `gh pr merge 7 --squash --match-head-commit ${head}`), { gh: fakeGh(prView(base)).gh })) ?? "";
427
+ assert.match(reason, /does not apply: a\.ts matches swarm\.direct\.reviewPaths/);
428
+ });
429
+
430
+ test("a head that changes qc.config.json stays out of the direct lane", (t) => {
431
+ const { dir, base } = laneRepo(t);
432
+ const git = (...args) => execFileSync("git", args, { cwd: dir, stdio: "pipe", encoding: "utf8" }).trim();
433
+ git("switch", "-q", "-c", "feat/2-y", "origin/main");
434
+ writeFileSync(path.join(dir, "qc.config.json"), JSON.stringify({ swarm: { dispatch: {}, direct: { maxLines: 500 } } }));
435
+ git("commit", "-q", "-am", "loosen");
436
+ const merge = shell(dir, `gh pr merge 7 --squash --match-head-commit ${git("rev-parse", "HEAD")}`);
437
+ const reason = denied(decide(merge, { gh: fakeGh(prView(base)).gh })) ?? "";
438
+ assert.match(reason, /does not apply: qc\.config\.json changes on the branch, so it runs the subagent workflow/);
439
+ });
440
+
441
+ test("a base that holds no qc.config.json keeps the head out of the direct lane", (t) => {
442
+ const { dir, head, base } = laneRepo(t, { swarm: { dispatch: {} } }, { tracked: false });
443
+ const reason = denied(decide(shell(dir, `gh pr merge 7 --squash --match-head-commit ${head}`), { gh: fakeGh(prView(base)).gh })) ?? "";
444
+ assert.match(reason, /does not apply: git cannot read qc\.config\.json at the PR base/);
445
+ });
446
+
447
+ test("the note states the size against the limits of the base, not the checkout", (t) => {
448
+ const { dir, head, base } = laneRepo(t, { swarm: { dispatch: {}, direct: { maxLines: 5 } } });
449
+ writeFileSync(path.join(dir, "qc.config.json"), JSON.stringify({ swarm: { dispatch: {}, direct: { maxLines: 500 } } }));
450
+ const output = decide(shell(dir, `gh pr merge 7 --squash --match-head-commit ${head}`), { gh: fakeGh(prView(base)).gh });
451
+ assert.match(output?.hookSpecificOutput?.additionalContext ?? "", /with 2 of 5 lines in 1 of 2 files and no review/);
452
+ });
453
+
454
+ const gitIn = (dir, ...args) => execFileSync("git", args, { cwd: dir, stdio: "pipe", encoding: "utf8" }).trim();
455
+ const merge7 = (dir, head) => shell(dir, `gh pr merge 7 --squash --match-head-commit ${head}`);
456
+
457
+ test("a loosening commit on the local base ref, absent from the PR base commit, does not open the lane", (t) => {
458
+ const strict = { swarm: { dispatch: {}, direct: { reviewPaths: ["**"] } }, worktree: { base: "main" } };
459
+ const { dir, base } = laneRepo(t, strict);
460
+ gitIn(dir, "switch", "-q", "main");
461
+ writeFileSync(path.join(dir, "qc.config.json"), JSON.stringify({ swarm: { dispatch: {} }, worktree: { base: "main" } }));
462
+ gitIn(dir, "commit", "-q", "-am", "loosen");
463
+ gitIn(dir, "switch", "-q", "-c", "fix/2-y");
464
+ writeFileSync(path.join(dir, "a.ts"), "one\nthree\n");
465
+ gitIn(dir, "commit", "-q", "-am", "one-line fix");
466
+ const reason = denied(decide(merge7(dir, gitIn(dir, "rev-parse", "HEAD")), { gh: fakeGh(prView(base)).gh })) ?? "";
467
+ assert.match(reason, /does not apply: qc\.config\.json changes on the branch, so it runs the subagent workflow/);
468
+ });
469
+
470
+ test("a stale local base ref does not hide the review paths that the PR base commit sets", (t) => {
471
+ const { dir } = laneRepo(t);
472
+ gitIn(dir, "switch", "-q", "-c", "newbase", "origin/main");
473
+ writeFileSync(path.join(dir, "qc.config.json"), JSON.stringify({ swarm: { dispatch: {}, direct: { reviewPaths: ["**"] } } }));
474
+ gitIn(dir, "commit", "-q", "-am", "require reviews");
475
+ const newBase = gitIn(dir, "rev-parse", "HEAD");
476
+ gitIn(dir, "switch", "-q", "feat/1-x");
477
+ gitIn(dir, "rebase", "-q", newBase);
478
+ const head = gitIn(dir, "rev-parse", "HEAD");
479
+ const reason = denied(decide(merge7(dir, head), { gh: fakeGh(prView(newBase)).gh })) ?? "";
480
+ assert.match(reason, /does not apply: a\.ts matches swarm\.direct\.reviewPaths/);
481
+ });
482
+
483
+ test("a PR base commit that the local repository lacks closes the lane and says to fetch", (t) => {
484
+ const { dir, head } = laneRepo(t);
485
+ const reason = denied(decide(merge7(dir, head), { gh: fakeGh(prView("1234567890123456789012345678901234567890")).gh })) ?? "";
486
+ assert.match(reason, /does not apply: the PR base commit 1234567890\w* is not in this repository, so run `git fetch`/);
487
+ });
488
+
489
+ test("a gh answer with no base commit closes the lane", (t) => {
490
+ const { dir, head } = laneRepo(t);
491
+ const reason = denied(decide(merge7(dir, head), { gh: fakeGh(JSON.stringify({ baseRefName: "main" })).gh })) ?? "";
492
+ assert.match(reason, /does not apply: gh returned no base commit for the PR/);
493
+ });
@@ -67,9 +67,9 @@ recorded.
67
67
  | An implementer reports the failing test before the passing one | `report-stop.mjs` |
68
68
  | A task is reviewed before the next implementer starts on its branch | `dispatch-guard.mjs`, through `task-gate.mjs` |
69
69
  | A fix round goes back to the implementer that wrote the task | `dispatch-guard.mjs`, through `task-gate.mjs` |
70
- | A merge pins the head that an APPROVED review named, or a head inside the direct lane | `merge-guard.mjs`, through `direct-lane.mjs` |
70
+ | A merge pins the head that an APPROVED review named, or a head inside the direct lane, whose limits come from the commit the PR merges into | `merge-guard.mjs`, through `direct-lane.mjs` |
71
71
  | An issue that a merge names is closed, or has a comment after the merge | `issue-gate.mjs`, on `Stop` |
72
- | The controller edits only its own paths, or a small fix inside the direct lane | `controller-guard.mjs`, through `direct-lane.mjs` |
72
+ | The controller edits only its own paths, or a small fix inside the direct lane, whose limits come from the base | `controller-guard.mjs`, through `direct-lane.mjs` |
73
73
  | A plan task names its files, a test step, and a commit step, within the budget | `qc plan-check`, and `plan-stop.mjs` |
74
74
 
75
75
  ## Worktrees — QC-015