architecture-harness 0.20.2 → 0.20.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/cli/bash-command-guard.mjs +70 -11
- package/src/cli/bash-command-guard.test.mjs +219 -0
- package/src/cli/bash-edit-guard.mjs +19 -16
- package/src/cli/bash-edit-guard.test.mjs +19 -0
- package/src/cli/ci-state.mjs +54 -0
- package/src/cli/ci-state.test.mjs +76 -0
- package/src/cli/cost-stop.mjs +98 -0
- package/src/cli/cost-stop.test.mjs +149 -0
- package/src/cli/dispatch-guard.mjs +3 -3
- package/src/cli/dispatch-guard.test.mjs +14 -6
- package/src/cli/dispatch-reminder.mjs +7 -3
- package/src/cli/dispatch-reminder.test.mjs +16 -1
- package/src/cli/git-read.mjs +41 -3
- package/src/cli/git-read.test.mjs +31 -1
- package/src/cli/ledger.mjs +52 -3
- package/src/cli/ledger.test.mjs +60 -0
- package/src/cli/merge-guard.mjs +1 -14
- package/src/cli/plugin-agents.test.mjs +11 -0
- package/src/cli/powershell-command.mjs +8 -13
- package/src/cli/powershell-command.test.mjs +12 -1
- package/src/cli/qc.mjs +1 -1
- package/src/cli/report-stop.test.mjs +2 -1
- package/src/cli/review-gate.mjs +224 -0
- package/src/cli/review-gate.test.mjs +334 -0
- package/src/cli/shell-command.mjs +77 -13
- package/src/cli/shell-command.test.mjs +44 -3
- package/src/cli/skill-guard.mjs +56 -0
- package/src/cli/skill-guard.test.mjs +87 -0
- package/src/cli/stop-gate.test.mjs +80 -0
- package/src/cli/task-gate-review.test.mjs +225 -0
- package/src/cli/task-gate.mjs +54 -16
- package/src/cli/task-gate.test.mjs +65 -6
- package/src/cli/test-brief.mjs +12 -0
- package/src/cli/transcript-usage.mjs +28 -0
- package/src/cli/transcript-usage.test.mjs +9 -0
- package/src/cli/workflow-hooks.test.mjs +38 -0
- package/src/cli/workflow-settings.mjs +14 -3
- package/src/cli/workflow-settings.test.mjs +24 -0
- package/src/config.mjs +13 -0
- package/templates/docs/enforcement.md +12 -1
package/package.json
CHANGED
|
@@ -46,6 +46,12 @@ const NESTED = Object.fromEntries([
|
|
|
46
46
|
]);
|
|
47
47
|
|
|
48
48
|
export const programName = (word) => path.basename(word ?? "").toLowerCase().replace(/\.exe$/, "");
|
|
49
|
+
/** Git reads the repository from these settings, wherever the command spells them. */
|
|
50
|
+
const REPOSITORY_SETTING = /GIT_DIR|GIT_COMMON_DIR|GIT_WORK_TREE|core\.worktree|core\.bare/i;
|
|
51
|
+
/** A shell variable or command substitution in a path: `$K`, `${K}`, `$env:K`, `$(cmd)`, a backtick, or `%K%`. */
|
|
52
|
+
const UNEXPANDED = /[$`]|%[A-Za-z_][^%\s]*%/;
|
|
53
|
+
const WORKTREE_ADD = /\bworktree\s+add\b/i;
|
|
54
|
+
const EXPLICIT_DIR = /--git-dir|--work-tree|GIT_DIR|GIT_COMMON_DIR|GIT_WORK_TREE|core\.worktree|core\.bare/i;
|
|
49
55
|
const isNoVerify = (arg) => arg.length >= NO_VERIFY_MIN && NO_VERIFY.startsWith(arg);
|
|
50
56
|
|
|
51
57
|
/** True when a commit or push names `--no-verify`, or a commit names `-n` alone or in a flag cluster. */
|
|
@@ -117,12 +123,15 @@ function worktreeAddPath(args) {
|
|
|
117
123
|
return null;
|
|
118
124
|
}
|
|
119
125
|
|
|
120
|
-
/** A path as
|
|
121
|
-
function
|
|
126
|
+
/** A path as the shell spells it, in the form of this platform. PowerShell also reads a backslash as a separator on Linux and macOS. */
|
|
127
|
+
function spelledPath(spelled, parse) {
|
|
122
128
|
const word = parse === powershellSegments ? spelled.replace(/\\/g, "/") : spelled;
|
|
123
|
-
return
|
|
129
|
+
return nativePath(word.replace(/^~(?=$|[\\/])/, os.homedir()));
|
|
124
130
|
}
|
|
125
131
|
|
|
132
|
+
/** A path as a shell resolves it from `cwd`. */
|
|
133
|
+
const resolveFrom = (cwd, spelled, parse) => path.resolve(cwd, spelledPath(spelled, parse));
|
|
134
|
+
|
|
126
135
|
/** True when `target` is one folder directly under `folder`, as `qc worktree add` makes it. */
|
|
127
136
|
function inFolder(folder, target) {
|
|
128
137
|
const parts = path.relative(keyOf(folder), keyOf(target)).split(path.sep);
|
|
@@ -138,37 +147,85 @@ const REASONS = {
|
|
|
138
147
|
script:
|
|
139
148
|
"a hand run of a hook script writes a lease or a ledger record under a made-up session id, and that lease blocks later " +
|
|
140
149
|
"edits in the worktree for hours. Test a hook only through its own suite (`node --test`), in temporary folders.",
|
|
150
|
+
explicitDir:
|
|
151
|
+
"`--git-dir`, `GIT_DIR`, `GIT_COMMON_DIR`, `--work-tree`, `GIT_WORK_TREE`, `core.worktree`, and `core.bare` make git read the repository from a path it resolves " +
|
|
152
|
+
"after every `-C`, so this guard cannot tell which repository gets the worktree. It looks for them in the whole command, in any order. " +
|
|
153
|
+
"Run `git worktree add` from inside the target repository instead: " +
|
|
154
|
+
"use `-C <repository>`, `cd <repository>`, or `Set-Location <repository>`, and drop the git directory and work tree settings.",
|
|
155
|
+
variable:
|
|
156
|
+
"a `-C`, `cd`, or `Set-Location` path holds a variable the guard cannot expand (`$K`, `${K}`, `$env:K`, `%K%`, or a command substitution), " +
|
|
157
|
+
"so it cannot tell which repository gets the worktree. Write the path literally.",
|
|
141
158
|
worktree:
|
|
142
159
|
"a worktree lives in the worktree folder of the main checkout, so git ignores it and `qc worktree remove` finds it. " +
|
|
143
160
|
"Run `npx qc worktree add <name> <branch>` from the repository, or give `git worktree add` a path one level under that folder.",
|
|
144
161
|
};
|
|
145
162
|
|
|
146
163
|
/**
|
|
147
|
-
*
|
|
164
|
+
* The worktree folder that judges `git worktree add`: the one of the repository the command targets. A `cd`,
|
|
165
|
+
* `Set-Location`, or `-C` into a repository names it, and else the session's own
|
|
166
|
+
* repository does. The path that `add` names never decides, because git adds the worktree to the repository
|
|
167
|
+
* of the working directory wherever the path is. `null` means the targeted repository is not guarded.
|
|
168
|
+
*/
|
|
169
|
+
function folderFor(place, from) {
|
|
170
|
+
// `undefined` means no checkout holds `from`, and `null` means one does that is not guarded.
|
|
171
|
+
const worked = place.folderAt(from);
|
|
172
|
+
return worked === undefined ? place.folder : worked;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* @param {{cwd: string, folder: string, folderAt: (dir: string) => string | null | undefined} | null} [place] the working directory,
|
|
177
|
+
* the session's worktree folder, and the folder of the repository that holds a directory (`undefined` when none does, `null` when
|
|
178
|
+
* it is not guarded); null skips the worktree rule
|
|
179
|
+
* @param {string[]} [refused] receives the folder of each refused worktree path
|
|
148
180
|
* @returns the reasons the command breaks a rule, in a stable order.
|
|
149
181
|
*/
|
|
150
|
-
export function violations(command, parse = segmentsOf, place = null) {
|
|
182
|
+
export function violations(command, parse = segmentsOf, place = null, refused = []) {
|
|
151
183
|
const segments = parse(command);
|
|
152
184
|
const found = new Set();
|
|
185
|
+
const scoped = Boolean(place) && (place.scoped === true || REPOSITORY_SETTING.test(command));
|
|
153
186
|
let cwd = place?.cwd;
|
|
187
|
+
// A move into a path with a variable leaves the repository unknown, until a literal absolute path names it again.
|
|
188
|
+
let unknown = place?.unknown === true;
|
|
154
189
|
for (const segment of segments) {
|
|
155
190
|
const moved = place ? MOVES.get(parse)?.(segment) : null;
|
|
156
|
-
if (moved != null
|
|
191
|
+
if (moved != null && UNEXPANDED.test(moved)) {
|
|
192
|
+
unknown = true;
|
|
193
|
+
} else if (moved != null) {
|
|
194
|
+
cwd = resolveFrom(cwd, moved, parse);
|
|
195
|
+
if (path.isAbsolute(spelledPath(moved, parse))) unknown = false;
|
|
196
|
+
}
|
|
157
197
|
const git = gitCall(segment);
|
|
158
198
|
if (git && skipsHooks(git)) found.add("hooks");
|
|
159
199
|
if (git && setsHooksPath(git)) found.add("hooksPath");
|
|
160
200
|
const added = place && git?.sub === "worktree" ? worktreeAddPath(git.args) : null;
|
|
161
|
-
|
|
201
|
+
// A subexpression after `-C` ends the git segment, so the `worktree add` after it never parses as git.
|
|
202
|
+
if (place && git?.sub === null && git.dirs.some((dir) => !dir || UNEXPANDED.test(dir)) && WORKTREE_ADD.test(command)) found.add("variable");
|
|
203
|
+
if (place && git?.sub === null && WORKTREE_ADD.test(command) && EXPLICIT_DIR.test(command)) found.add("explicitDir");
|
|
204
|
+
if (added !== null && (scoped || git.explicitDir)) {
|
|
205
|
+
// The session is guarded, because `place` exists only then.
|
|
206
|
+
found.add("explicitDir");
|
|
207
|
+
} else if (added !== null && (unknown || git.dirs.some((dir) => UNEXPANDED.test(dir)))) {
|
|
208
|
+
found.add("variable");
|
|
209
|
+
} else if (added !== null) {
|
|
162
210
|
const from = git.dirs.filter(Boolean).reduce((dir, next) => resolveFrom(dir, next, parse), cwd);
|
|
163
|
-
|
|
211
|
+
const folder = folderFor(place, from);
|
|
212
|
+
if (folder !== null && !inFolder(folder, resolveFrom(from, added, parse))) {
|
|
213
|
+
found.add("worktree");
|
|
214
|
+
refused.push(folder);
|
|
215
|
+
}
|
|
164
216
|
}
|
|
165
217
|
const nested = nestedCommand(segment);
|
|
166
|
-
if (nested) for (const key of violations(nested.command, nested.parse, place && { ...place, cwd })) found.add(key);
|
|
218
|
+
if (nested) for (const key of violations(nested.command, nested.parse, place && { ...place, cwd, scoped, unknown }, refused)) found.add(key);
|
|
167
219
|
}
|
|
168
220
|
if (!segments.some(runsTests) && segments.some(runsHookScript)) found.add("script");
|
|
169
221
|
return Object.keys(REASONS).filter((key) => found.has(key));
|
|
170
222
|
}
|
|
171
223
|
|
|
224
|
+
/** The worktree folder of the repository that holds `dir`: undefined when none does, null when it has no config or turns the rule off. */
|
|
225
|
+
function folderAt(dir) {
|
|
226
|
+
return checkoutRootOf(dir) === null ? undefined : (worktreeRuleAt(dir)?.folder ?? null);
|
|
227
|
+
}
|
|
228
|
+
|
|
172
229
|
/** The verdict on one Bash or PowerShell call. @returns the hook output, or null to let the call run with no message. */
|
|
173
230
|
export function decide(call) {
|
|
174
231
|
const command = call.tool_input?.command;
|
|
@@ -179,9 +236,11 @@ export function decide(call) {
|
|
|
179
236
|
if (!root || !existsSync(path.join(root, CONFIG_FILE))) return null;
|
|
180
237
|
// The config is read only for a command that can add a worktree, so every other call stays cheap.
|
|
181
238
|
const rule = /\bworktree\b/i.test(command) ? worktreeRuleAt(cwd) : null;
|
|
182
|
-
const
|
|
239
|
+
const refused = [];
|
|
240
|
+
const place = rule && { cwd: path.resolve(cwd), folder: rule.folder, folderAt };
|
|
241
|
+
const found = violations(command, parse, place, refused);
|
|
183
242
|
if (found.length === 0) return null;
|
|
184
|
-
const reasonOf = (key) => (key === "worktree" ? `${REASONS.worktree} The folder is ${
|
|
243
|
+
const reasonOf = (key) => (key === "worktree" ? `${REASONS.worktree} The folder is ${[...new Set(refused)].join(" or ")}.` : REASONS[key]);
|
|
185
244
|
return {
|
|
186
245
|
hookSpecificOutput: {
|
|
187
246
|
hookEventName: "PreToolUse",
|
|
@@ -234,3 +234,222 @@ test("inside a linked worktree, the folder is the main checkout's", (t) => {
|
|
|
234
234
|
assert.equal(decide(bash(linked, "git worktree add ../x")), null);
|
|
235
235
|
assert.ok(outsideFolder(decide(bash(linked, "git worktree add x"))), "a worktree nested in a worktree is refused");
|
|
236
236
|
});
|
|
237
|
+
|
|
238
|
+
/** The adopted checkout plus a second adopted checkout, `kit`, and a checkout with no config. Forward slashes, as a Bash command spells a path. */
|
|
239
|
+
function twoRepos(t) {
|
|
240
|
+
const ws = workspace(t);
|
|
241
|
+
const kit = path.join(path.dirname(ws.adopted), "kit");
|
|
242
|
+
mkdirSync(kit);
|
|
243
|
+
execFileSync("git", ["init", "-q"], { cwd: kit, stdio: "pipe" });
|
|
244
|
+
writeFileSync(path.join(kit, "qc.config.json"), "{}");
|
|
245
|
+
return { ...ws, kit, slash: (dir) => dir.replaceAll("\\", "/") };
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
test("a worktree add judges the repository the command targets, through -C, cd, or Set-Location", (t) => {
|
|
249
|
+
const ws = twoRepos(t);
|
|
250
|
+
const kit = ws.slash(ws.kit);
|
|
251
|
+
for (const command of [
|
|
252
|
+
`git -C ${kit} worktree add ${kit}/.worktree/x -b feat/x`,
|
|
253
|
+
`git -C ${kit} worktree add .worktree/x`,
|
|
254
|
+
`cd ${kit} && git worktree add .worktree/x`,
|
|
255
|
+
`cd ${kit} && git worktree add ${kit}/.worktree/x`,
|
|
256
|
+
`bash -c 'cd ${kit} && git worktree add .worktree/x'`,
|
|
257
|
+
]) {
|
|
258
|
+
assert.equal(decide(bash(ws.adopted, command)), null, command);
|
|
259
|
+
}
|
|
260
|
+
for (const command of [`Set-Location ${kit}; git worktree add .worktree/x`, `Set-Location ${kit}; git worktree add ${kit}/.worktree/x`, `git -C ${kit} worktree add .worktree/x`]) {
|
|
261
|
+
assert.equal(decide(powershell(ws.adopted, command)), null, command);
|
|
262
|
+
}
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
test("a worktree add in the targeted repository is still held to that repository's folder, and the deny names it", (t) => {
|
|
266
|
+
const ws = twoRepos(t);
|
|
267
|
+
const kit = ws.slash(ws.kit);
|
|
268
|
+
for (const command of [`git -C ${kit} worktree add ../x`, `git -C ${kit} worktree add ${ws.slash(ws.adopted)}/.worktree/x`, `cd ${kit} && git worktree add x`]) {
|
|
269
|
+
const reason = reasonOf(decide(bash(ws.adopted, command))) ?? "";
|
|
270
|
+
assert.match(reason, /npx qc worktree add <name> <branch>/, command);
|
|
271
|
+
assert.ok(reason.includes(`The folder is ${path.join(ws.kit, ".worktree")}.`), `${command}: ${reason}`);
|
|
272
|
+
}
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
test("an absolute worktree path alone does not name the repository: git adds to the session's repository", (t) => {
|
|
276
|
+
const ws = twoRepos(t);
|
|
277
|
+
const kit = ws.slash(ws.kit);
|
|
278
|
+
for (const command of [`git worktree add ${kit}/.worktree/x`, `git worktree add ${kit}/x`, `git worktree add ../x`]) {
|
|
279
|
+
const reason = reasonOf(decide(bash(ws.adopted, command))) ?? "";
|
|
280
|
+
assert.ok(reason.includes(`The folder is ${path.join(ws.adopted, ".worktree")}.`), `${command}: ${reason}`);
|
|
281
|
+
}
|
|
282
|
+
assert.equal(decide(bash(ws.adopted, `git worktree add ${ws.slash(ws.adopted)}/.worktree/x`)), null);
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
/** The refusal for an explicit git directory or work tree: it says to run `git worktree add` from inside the target repository. */
|
|
286
|
+
const explicitDirReason = (output) => {
|
|
287
|
+
const reason = reasonOf(output) ?? "";
|
|
288
|
+
assert.match(reason, /--git-dir|GIT_DIR|--work-tree|GIT_WORK_TREE/, reason);
|
|
289
|
+
assert.match(reason, /from inside the target repository/, reason);
|
|
290
|
+
assert.match(reason, /-C/, reason);
|
|
291
|
+
return reason;
|
|
292
|
+
};
|
|
293
|
+
|
|
294
|
+
test("a worktree add with --git-dir, GIT_DIR, --work-tree, or GIT_WORK_TREE is refused from a guarded session", (t) => {
|
|
295
|
+
const ws = twoRepos(t);
|
|
296
|
+
const kit = ws.slash(ws.kit);
|
|
297
|
+
const plain = ws.slash(ws.plain);
|
|
298
|
+
const adopted = ws.slash(ws.adopted);
|
|
299
|
+
for (const command of [
|
|
300
|
+
`git --git-dir=${kit}/.git worktree add ${kit}/.worktree/x`,
|
|
301
|
+
`git --git-dir ${kit}/.git worktree add ${kit}/.worktree/x`,
|
|
302
|
+
`GIT_DIR=${kit}/.git git worktree add ${kit}/.worktree/x`,
|
|
303
|
+
`git -C ${kit} --git-dir=${kit}/.git worktree add .worktree/x`,
|
|
304
|
+
`git -C ${plain} --git-dir=${kit}/.git worktree add ${kit}/.worktree/x`,
|
|
305
|
+
`git --git-dir=${kit}/.git worktree add ../x`,
|
|
306
|
+
`GIT_DIR=${kit}/.git git worktree add ../x`,
|
|
307
|
+
`git --git-dir=${kit}/.git worktree add .worktree/x`,
|
|
308
|
+
`GIT_DIR=${kit}/.git git worktree add .worktree/x`,
|
|
309
|
+
`cd ${plain} && git --git-dir=${kit}/.git worktree add .worktree/x`,
|
|
310
|
+
`git -C ${plain} --git-dir=${adopted}/.git worktree add ../x`,
|
|
311
|
+
`git --git-dir=${plain}/.git worktree add ../x`,
|
|
312
|
+
`git --git-dir=${adopted}/.git worktree add ${adopted}/.worktree/x`,
|
|
313
|
+
`env GIT_DIR=${kit}/.git git worktree add ../x`,
|
|
314
|
+
`bash -c 'git --git-dir=${kit}/.git worktree add ../x'`,
|
|
315
|
+
`git --work-tree=${plain} worktree add ../x`,
|
|
316
|
+
`git --work-tree ${plain} worktree add ../x`,
|
|
317
|
+
`GIT_WORK_TREE=${plain} git worktree add ../x`,
|
|
318
|
+
`git --work-tree=${kit} worktree add ../x`,
|
|
319
|
+
`GIT_WORK_TREE=${kit} git worktree add ../x`,
|
|
320
|
+
`git --work-tree=${plain} worktree add .worktree/x`,
|
|
321
|
+
]) {
|
|
322
|
+
explicitDirReason(decide(bash(ws.adopted, command)));
|
|
323
|
+
}
|
|
324
|
+
explicitDirReason(decide(powershell(ws.adopted, `git --git-dir=${kit}/.git worktree add ../x`)));
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
test("git resolves a relative git dir from the working directory after every -C, so these probes are refused", (t) => {
|
|
328
|
+
const ws = twoRepos(t);
|
|
329
|
+
const adopted = ws.slash(ws.adopted);
|
|
330
|
+
const plain = ws.slash(ws.plain);
|
|
331
|
+
// Real git adds ../x1 as a worktree of `adopted` from these.
|
|
332
|
+
for (const command of [
|
|
333
|
+
`cd ${plain} && git --git-dir=.git -C ${adopted} worktree add ../x1`,
|
|
334
|
+
`cd ${plain} && GIT_DIR=.git git -C ${adopted} worktree add ../x1`,
|
|
335
|
+
]) {
|
|
336
|
+
explicitDirReason(decide(bash(ws.adopted, command)));
|
|
337
|
+
}
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
test("an explicit git dir on a command other than worktree add is not refused by the worktree rule", (t) => {
|
|
341
|
+
const ws = twoRepos(t);
|
|
342
|
+
const kit = ws.slash(ws.kit);
|
|
343
|
+
for (const command of [`git --git-dir=${kit}/.git status`, `git --git-dir=${kit}/.git worktree list`, `GIT_DIR=${kit}/.git git log`]) {
|
|
344
|
+
assert.equal(decide(bash(ws.adopted, command)), null, command);
|
|
345
|
+
}
|
|
346
|
+
});
|
|
347
|
+
|
|
348
|
+
test("a worktree add in a repository with no qc.config.json is not guarded", (t) => {
|
|
349
|
+
const ws = twoRepos(t);
|
|
350
|
+
const plain = ws.slash(ws.plain);
|
|
351
|
+
for (const command of [`git -C ${plain} worktree add ../x`, `cd ${plain} && git worktree add ../x`]) {
|
|
352
|
+
assert.equal(decide(bash(ws.adopted, command)), null, command);
|
|
353
|
+
}
|
|
354
|
+
assert.equal(decide(bash(ws.plain, "git worktree add ../x")), null, "a session in it is not guarded either");
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
test("a worktree add is refused when its command text sets GIT_DIR, GIT_WORK_TREE, core.worktree, or core.bare in any segment", (t) => {
|
|
358
|
+
const ws = twoRepos(t);
|
|
359
|
+
const kit = ws.slash(ws.kit);
|
|
360
|
+
for (const command of [
|
|
361
|
+
`export GIT_DIR=${kit}/.git; git worktree add .worktree/x`,
|
|
362
|
+
`export GIT_WORK_TREE=${kit}; cd ${kit} && git worktree add .worktree/x`,
|
|
363
|
+
`env GIT_DIR=${kit}/.git bash -c 'cd ${kit}'; git worktree add .worktree/x`,
|
|
364
|
+
`git worktree add .worktree/x; export GIT_DIR=${kit}/.git`,
|
|
365
|
+
`export GIT_DIR=${kit}/.git; bash -c 'git worktree add .worktree/x'`,
|
|
366
|
+
`bash -c 'export GIT_DIR=${kit}/.git'; git worktree add .worktree/x`,
|
|
367
|
+
`git -c core.worktree=${kit} worktree add .worktree/x`,
|
|
368
|
+
`git -c core.bare=true worktree add .worktree/x`,
|
|
369
|
+
`git config core.worktree ${kit} && git worktree add .worktree/x`,
|
|
370
|
+
`set GIT_DIR=${kit}/.git && git worktree add .worktree/x`,
|
|
371
|
+
`export git_dir=${kit}/.git; git worktree add .worktree/x`,
|
|
372
|
+
`export GIT_COMMON_DIR=${kit}/.git; git worktree add .worktree/x`,
|
|
373
|
+
"git -c core.worktree=$(pwd) worktree add ../out",
|
|
374
|
+
"git --git-dir=$(pwd)/.git worktree add ../out",
|
|
375
|
+
"git --work-tree=$(pwd) worktree add ../out",
|
|
376
|
+
]) {
|
|
377
|
+
explicitDirReason(decide(bash(ws.adopted, command)));
|
|
378
|
+
}
|
|
379
|
+
for (const command of [
|
|
380
|
+
`$env:GIT_DIR = "${kit}/.git"; git worktree add .worktree/x`,
|
|
381
|
+
`$env:GIT_WORK_TREE = "${kit}"; Set-Location ${kit}; git worktree add .worktree/x`,
|
|
382
|
+
`git -c core.worktree=${kit} worktree add .worktree/x`,
|
|
383
|
+
`$env:git_dir="${kit}/.git"; git worktree add .worktree/x`,
|
|
384
|
+
]) {
|
|
385
|
+
explicitDirReason(decide(powershell(ws.adopted, command)));
|
|
386
|
+
}
|
|
387
|
+
});
|
|
388
|
+
|
|
389
|
+
test("a worktree add with none of those settings still passes, and so does a mention without a worktree add", (t) => {
|
|
390
|
+
const ws = twoRepos(t);
|
|
391
|
+
const kit = ws.slash(ws.kit);
|
|
392
|
+
assert.equal(decide(bash(ws.adopted, "git worktree add .worktree/x")), null);
|
|
393
|
+
assert.equal(decide(bash(ws.adopted, `export GIT_DIR=${kit}/.git; git status`)), null);
|
|
394
|
+
assert.equal(decide(bash(ws.adopted, `git -c core.bare=true status`)), null);
|
|
395
|
+
assert.equal(decide(bash(ws.plain, `export GIT_DIR=${kit}/.git; git worktree add ../x`)), null, "a repository with no qc.config.json is not guarded");
|
|
396
|
+
});
|
|
397
|
+
|
|
398
|
+
/** The refusal for a path that holds an unexpanded variable: it says to write the path literally. */
|
|
399
|
+
const variableReason = (output) => {
|
|
400
|
+
const reason = reasonOf(output) ?? "";
|
|
401
|
+
assert.match(reason, /literally/, reason);
|
|
402
|
+
assert.match(reason, /variable/, reason);
|
|
403
|
+
return reason;
|
|
404
|
+
};
|
|
405
|
+
|
|
406
|
+
test("a worktree add after a -C, cd, or Set-Location path with an unexpanded variable is refused, and the reason says to write the path literally", (t) => {
|
|
407
|
+
const ws = twoRepos(t);
|
|
408
|
+
const kit = ws.slash(ws.kit);
|
|
409
|
+
for (const command of [
|
|
410
|
+
"git -C $K worktree add .worktree/x",
|
|
411
|
+
"git -C ${K} worktree add .worktree/x",
|
|
412
|
+
`git -C "$HOME/kit" worktree add .worktree/x`,
|
|
413
|
+
"cd $K && git worktree add .worktree/x",
|
|
414
|
+
"cd ${K}/sub && git worktree add .worktree/x",
|
|
415
|
+
"cd $(git rev-parse --show-toplevel) && git worktree add .worktree/x",
|
|
416
|
+
`bash -c 'cd $K && git worktree add .worktree/x'`,
|
|
417
|
+
"cd %K% && git worktree add .worktree/x",
|
|
418
|
+
`cd $K; cd sub; git worktree add .worktree/x`,
|
|
419
|
+
"cd `pwd` && git worktree add .worktree/x",
|
|
420
|
+
"cd $(pwd)/sub && git worktree add .worktree/x",
|
|
421
|
+
"pushd $(pwd) && git worktree add .worktree/x",
|
|
422
|
+
"git -C $(git rev-parse --show-toplevel) worktree add ../out",
|
|
423
|
+
"git -C `pwd` worktree add ../out",
|
|
424
|
+
]) {
|
|
425
|
+
variableReason(decide(bash(ws.adopted, command)));
|
|
426
|
+
}
|
|
427
|
+
for (const command of [
|
|
428
|
+
"Set-Location $env:K; git worktree add .worktree/x",
|
|
429
|
+
"Set-Location $K; git worktree add .worktree/x",
|
|
430
|
+
"git -C $env:K worktree add .worktree/x",
|
|
431
|
+
"cd %K%; git worktree add .worktree/x",
|
|
432
|
+
"Set-Location (Join-Path $K 'sub'); git worktree add .worktree/x",
|
|
433
|
+
"Push-Location ($env:K + '\\sub'); git worktree add .worktree/x",
|
|
434
|
+
"Set-Location -Path (Join-Path $K sub); git worktree add .worktree/x",
|
|
435
|
+
"Set-Location -LiteralPath (Get-Location); git worktree add .worktree/x",
|
|
436
|
+
"cd (Resolve-Path ..); git worktree add .worktree/x",
|
|
437
|
+
"Set-Location $(Join-Path $K sub); git worktree add .worktree/x",
|
|
438
|
+
"git -C (Join-Path $K 'x') worktree add ../out",
|
|
439
|
+
"git -C $(Get-Location) worktree add ../out",
|
|
440
|
+
]) {
|
|
441
|
+
variableReason(decide(powershell(ws.adopted, command)));
|
|
442
|
+
}
|
|
443
|
+
// A literal path after the variable names the repository again.
|
|
444
|
+
assert.equal(decide(bash(ws.adopted, `cd $K; cd ${kit} && git worktree add .worktree/x`)), null);
|
|
445
|
+
});
|
|
446
|
+
|
|
447
|
+
test("a variable in a path of a command that adds no worktree, or in the worktree path alone, is no refusal for it", (t) => {
|
|
448
|
+
const ws = twoRepos(t);
|
|
449
|
+
for (const command of ["cd $K && git status", "git -C $K worktree list", "cd $HOME && ls", "git worktree add .worktree/$NAME"]) {
|
|
450
|
+
assert.doesNotMatch(reasonOf(decide(bash(ws.adopted, command))) ?? "", /literally/, command);
|
|
451
|
+
}
|
|
452
|
+
const plain = ws.slash(ws.plain);
|
|
453
|
+
assert.equal(decide(bash(ws.plain, "cd $K && git worktree add ../x")), null, "a repository with no qc.config.json is not guarded");
|
|
454
|
+
assert.equal(decide(bash(ws.adopted, `git -C ${plain} worktree add ../x`)), null);
|
|
455
|
+
});
|
|
@@ -144,25 +144,28 @@ export async function report(call, projectRoot) {
|
|
|
144
144
|
const findings = [];
|
|
145
145
|
const isMainSession = !(typeof call.agent_id === "string" && call.agent_id !== "");
|
|
146
146
|
for (const root of checkoutsOf(cwd, command, dialect)) findings.push(...(await findingsIn(root, manifest, project, isMainSession)));
|
|
147
|
-
|
|
147
|
+
const unresolved = dialect.unresolvedGitDirs(command);
|
|
148
|
+
if (findings.length === 0 && unresolved.length === 0) return null;
|
|
148
149
|
|
|
149
150
|
const listed = findings.slice(0, MAX_LISTED).map((line) => `- ${line}`);
|
|
150
151
|
if (findings.length > MAX_LISTED) listed.push(`- and ${findings.length - MAX_LISTED} more`);
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
152
|
+
const lines = [];
|
|
153
|
+
if (findings.length > 0) {
|
|
154
|
+
lines.push("Bash edit guard: the checkout holds uncommitted changes that the swarm rules do not allow.", ...listed);
|
|
155
|
+
lines.push(
|
|
156
|
+
findings.some((line) => line.includes(CONTROLLER_RULE))
|
|
157
|
+
? "Dispatch an implementer for a change outside the controller paths. Revert a change only if you made it by mistake. The guard does not revert anything."
|
|
158
|
+
: "Revert a change only if your work order made it and should not have. " +
|
|
159
|
+
"For any other change, stop and tell the orchestrator. The guard does not revert anything.",
|
|
160
|
+
);
|
|
161
|
+
}
|
|
162
|
+
if (unresolved.length > 0) {
|
|
163
|
+
lines.push(
|
|
164
|
+
`Bash edit guard: the command names a git directory with no work tree (${unresolved.join(", ")}), ` +
|
|
165
|
+
"so the guard cannot tell which checkout it changed. Run `git status` in that repository.",
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
return { hookSpecificOutput: { hookEventName: "PostToolUse", additionalContext: lines.join("\n") } };
|
|
166
169
|
}
|
|
167
170
|
|
|
168
171
|
async function readStdin() {
|
|
@@ -136,3 +136,22 @@ test("porcelain output names each changed path, and both sides of a rename", ()
|
|
|
136
136
|
const status = [" M src/a.ts", "?? docs/new file.md", "R lib/new.ts", "lib/old.ts", ""].join("\u0000");
|
|
137
137
|
assert.deepEqual(changedPaths(status), ["src/a.ts", "docs/new file.md", "lib/new.ts", "lib/old.ts"]);
|
|
138
138
|
});
|
|
139
|
+
|
|
140
|
+
test("a --work-tree directory is a checkout the command could have changed", async (t) => {
|
|
141
|
+
const ws = workspace(t);
|
|
142
|
+
ws.put(ws.linked, "docs/b.md");
|
|
143
|
+
const slash = (dir) => dir.replaceAll("\\", "/");
|
|
144
|
+
const output = await report(bash(ws.outside, `git --work-tree=${slash(ws.linked)} checkout -- docs`), ws.linked);
|
|
145
|
+
assert.match(contextOf(output), /docs\/b\.md: outside the work order's declared paths/);
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
test("a git dir with no work tree is reported as unresolved, not dropped", async (t) => {
|
|
149
|
+
const ws = workspace(t);
|
|
150
|
+
const slash = (dir) => dir.replaceAll("\\", "/");
|
|
151
|
+
const text = contextOf(await report(bash(ws.outside, `git --git-dir=${slash(ws.primary)}/.git checkout main`), ws.linked));
|
|
152
|
+
assert.match(text, /git directory with no work tree/);
|
|
153
|
+
assert.ok(text.includes(`${slash(ws.primary)}/.git`), text);
|
|
154
|
+
assert.match(text, /cannot tell which checkout/);
|
|
155
|
+
const withTree = await report(bash(ws.outside, `git --git-dir=${slash(ws.primary)}/.git --work-tree=${slash(ws.outside)} checkout main`), ws.linked);
|
|
156
|
+
assert.doesNotMatch(contextOf(withTree), /git directory with no work tree/);
|
|
157
|
+
});
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
// The CI state of a commit, read from its GitHub check runs. The task gate asks it before a review: a
|
|
2
|
+
// head whose CI is not green changes before it is reviewed, so the review waits.
|
|
3
|
+
|
|
4
|
+
import { runGh } from "./gh-run.mjs";
|
|
5
|
+
import { originOf } from "./git-read.mjs";
|
|
6
|
+
|
|
7
|
+
// The hook that asks has a 10 second limit, and a hook that the limit kills lets the call through. The two reads
|
|
8
|
+
// together stay well under it, so a slow gh leaves the head unknown and the review refusal stands.
|
|
9
|
+
const GH_TIMEOUT_MS = 3_000;
|
|
10
|
+
const ORIGIN_TIMEOUT_MS = 1_000;
|
|
11
|
+
const FAILED = new Set(["failure", "cancelled", "timed_out", "action_required"]);
|
|
12
|
+
const short = (sha) => sha.slice(0, 7);
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* @param runs the `check_runs` array of the GitHub API
|
|
16
|
+
* @returns why the head is not green, naming the run, or null when every run completed without a failure
|
|
17
|
+
* or when there are no runs, because a head with no CI is unknown.
|
|
18
|
+
*/
|
|
19
|
+
export function notGreenReason(runs) {
|
|
20
|
+
const failed = runs.find((entry) => entry.status === "completed" && FAILED.has(entry.conclusion));
|
|
21
|
+
if (failed) return `${failed.name} ended ${failed.conclusion}`;
|
|
22
|
+
const open = runs.find((entry) => entry.status !== "completed");
|
|
23
|
+
return open ? `${open.name} is ${open.status}` : null;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* What CI says about `sha`: `reason` names a run that failed or still runs, as `CI on <short sha>: <why>`.
|
|
28
|
+
* `unknown` says why nothing is known: no sha, no `origin`, a gh that fails or times out, a reply that does not parse, or no check runs.
|
|
29
|
+
* `gh` and `origin` replace the real reads, for a test.
|
|
30
|
+
*/
|
|
31
|
+
export function ciState(sha, cwd, { gh = runGh, origin = originOf } = {}) {
|
|
32
|
+
const unknown = (why) => ({ reason: null, unknown: why });
|
|
33
|
+
if (typeof sha !== "string" || sha === "") return unknown("there is no commit to ask about");
|
|
34
|
+
const repo = origin(cwd, ORIGIN_TIMEOUT_MS);
|
|
35
|
+
if (repo === null) return unknown("the checkout has no readable origin");
|
|
36
|
+
const out = gh(["api", `repos/${repo}/commits/${sha}/check-runs?per_page=100`], { cwd, timeoutMs: GH_TIMEOUT_MS });
|
|
37
|
+
if (out === null) return unknown("gh failed or timed out");
|
|
38
|
+
let runs;
|
|
39
|
+
try {
|
|
40
|
+
runs = JSON.parse(out)?.check_runs;
|
|
41
|
+
} catch {
|
|
42
|
+
return unknown("gh answered with text that is not JSON");
|
|
43
|
+
}
|
|
44
|
+
if (!Array.isArray(runs)) return unknown("gh answered with no check_runs list");
|
|
45
|
+
if (runs.length === 0) return unknown(`${short(sha)} has no check runs`);
|
|
46
|
+
const why = notGreenReason(runs);
|
|
47
|
+
return { reason: why === null ? null : `CI on ${short(sha)}: ${why}`, unknown: null };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* @returns the reason CI on `sha` is not green, as `CI on <short sha>: <why>`, or null when it is green or
|
|
52
|
+
* unknown. `ciState` tells those two apart.
|
|
53
|
+
*/
|
|
54
|
+
export const ciNotGreen = (sha, cwd, deps) => ciState(sha, cwd, deps).reason;
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { strict as assert } from "node:assert";
|
|
2
|
+
import { test } from "node:test";
|
|
3
|
+
import { ciNotGreen, ciState, notGreenReason } from "./ci-state.mjs";
|
|
4
|
+
|
|
5
|
+
const SHA = "a".repeat(40);
|
|
6
|
+
const run = (name, status, conclusion = null) => ({ name, status, conclusion });
|
|
7
|
+
const reply = (...runs) => JSON.stringify({ total_count: runs.length, check_runs: runs });
|
|
8
|
+
|
|
9
|
+
test("a head whose check runs all completed green is green", () => {
|
|
10
|
+
assert.equal(notGreenReason([run("lint", "completed", "success"), run("docs", "completed", "skipped"), run("x", "completed", "neutral")]), null);
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
test("each failing conclusion makes the head not green, and the reason names the run", () => {
|
|
14
|
+
for (const conclusion of ["failure", "cancelled", "timed_out", "action_required"]) {
|
|
15
|
+
assert.match(notGreenReason([run("ok", "completed", "success"), run("build", "completed", conclusion)]) ?? "", new RegExp(`build.*${conclusion}`));
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
test("a run that has not completed makes the head not green", () => {
|
|
20
|
+
for (const status of ["queued", "in_progress", "waiting", "pending"]) {
|
|
21
|
+
assert.match(notGreenReason([run("build", status)]) ?? "", new RegExp(`build.*${status}`));
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
test("a head with no check runs is unknown, not red", () => {
|
|
26
|
+
assert.equal(notGreenReason([]), null);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
test("ciNotGreen reads the check runs of the sha from the repository of origin", () => {
|
|
30
|
+
const calls = [];
|
|
31
|
+
const gh = (args, options) => {
|
|
32
|
+
calls.push({ args, options });
|
|
33
|
+
return reply(run("build", "completed", "failure"));
|
|
34
|
+
};
|
|
35
|
+
const reason = ciNotGreen(SHA, "/checkout", { gh, origin: () => "o/r" });
|
|
36
|
+
assert.match(reason ?? "", /^CI on aaaaaaa:.*build.*failure/);
|
|
37
|
+
assert.equal(calls.length, 1);
|
|
38
|
+
assert.equal(calls[0].args[0], "api");
|
|
39
|
+
assert.ok(calls[0].args[1].startsWith(`repos/o/r/commits/${SHA}/check-runs`));
|
|
40
|
+
assert.equal(calls[0].options.cwd, "/checkout");
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test("ciNotGreen is null when the head is green, gh fails, the reply is not JSON, or origin is unknown", () => {
|
|
44
|
+
const origin = () => "o/r";
|
|
45
|
+
assert.equal(ciNotGreen(SHA, "/c", { gh: () => reply(run("a", "completed", "success")), origin }), null);
|
|
46
|
+
assert.equal(ciNotGreen(SHA, "/c", { gh: () => null, origin }), null);
|
|
47
|
+
assert.equal(ciNotGreen(SHA, "/c", { gh: () => "not json", origin }), null);
|
|
48
|
+
assert.equal(ciNotGreen(SHA, "/c", { gh: () => "{}", origin }), null);
|
|
49
|
+
assert.equal(ciNotGreen(SHA, "/c", { gh: () => reply(run("a", "completed", "failure")), origin: () => null }), null);
|
|
50
|
+
assert.equal(ciNotGreen(null, "/c", { gh: () => reply(run("a", "completed", "failure")), origin }), null);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
test("ciNotGreen bounds its reads so the whole gate stays under the hook's 10 second limit", () => {
|
|
54
|
+
let ghOptions;
|
|
55
|
+
let originTimeout;
|
|
56
|
+
const gh = (_args, options) => ((ghOptions = options), null);
|
|
57
|
+
const origin = (_cwd, timeoutMs) => ((originTimeout = timeoutMs), "o/r");
|
|
58
|
+
assert.equal(ciNotGreen(SHA, "/c", { gh, origin }), null, "a gh that timed out leaves the head unknown, so the review refusal stands");
|
|
59
|
+
assert.ok(ghOptions.timeoutMs > 0 && ghOptions.timeoutMs <= 3000, `gh timeout ${ghOptions.timeoutMs}`);
|
|
60
|
+
assert.ok(originTimeout > 0 && originTimeout <= 1000, `origin timeout ${originTimeout}`);
|
|
61
|
+
assert.ok(ghOptions.timeoutMs + originTimeout < 5000);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
test("ciState separates a head that is not green, a green head, and a head it cannot read", () => {
|
|
65
|
+
const origin = () => "o/r";
|
|
66
|
+
const state = (gh, from = origin) => ciState(SHA, "/c", { gh, origin: from });
|
|
67
|
+
assert.deepEqual(state(() => reply(run("a", "completed", "success"))), { reason: null, unknown: null });
|
|
68
|
+
assert.match(state(() => reply(run("build", "in_progress"))).reason, /^CI on aaaaaaa: build is in_progress/);
|
|
69
|
+
assert.match(state(() => reply()).unknown, /no check runs/);
|
|
70
|
+
assert.match(state(() => null).unknown, /gh/);
|
|
71
|
+
assert.match(state(() => "not json").unknown, /gh/);
|
|
72
|
+
assert.match(state(() => "{}").unknown, /gh/);
|
|
73
|
+
assert.match(state(() => reply(), () => null).unknown, /origin/);
|
|
74
|
+
assert.match(ciState("", "/c", { gh: () => null, origin }).unknown, /no commit/);
|
|
75
|
+
for (const answer of [state(() => null), state(() => reply())]) assert.equal(answer.reason, null);
|
|
76
|
+
});
|