@feigi/fleet-ctl 3.23.36 → 3.23.38

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.
Files changed (205) hide show
  1. package/package.json +1 -1
  2. package/scripts/cell-readout.mjs +38 -11
  3. package/scripts/main-checkout.mjs +1 -1
  4. package/scripts/member-write-guard.mjs +1 -1
  5. package/scripts/repo-root.mjs +1 -1
  6. package/skills/run-team/references/reaping.md +1 -1
  7. package/scripts/agent-test-dir-prose.test.mjs +0 -59
  8. package/scripts/agent-test.test.mjs +0 -1640
  9. package/scripts/ambient-git-vars-mjs-prose.test.mjs +0 -397
  10. package/scripts/ambient-git-vars-prose.test.mjs +0 -192
  11. package/scripts/ancestry-check-position-prose.test.mjs +0 -175
  12. package/scripts/apply-ruleset.test.mjs +0 -421
  13. package/scripts/arg-header-probes-prose.test.mjs +0 -394
  14. package/scripts/arg.test.mjs +0 -1354
  15. package/scripts/board-cli.test.mjs +0 -710
  16. package/scripts/board-identity.test.mjs +0 -429
  17. package/scripts/board-liveness-banner.test.mjs +0 -119
  18. package/scripts/board-prev-shape.test.mjs +0 -228
  19. package/scripts/board-tryparse-null.test.mjs +0 -119
  20. package/scripts/board.test.mjs +0 -3121
  21. package/scripts/brief-after-cut-prose.test.mjs +0 -53
  22. package/scripts/candidates-exit3-prose.test.mjs +0 -302
  23. package/scripts/candidates.test.mjs +0 -2312
  24. package/scripts/caveat-unification-prose.test.mjs +0 -98
  25. package/scripts/cell-readout.test.mjs +0 -615
  26. package/scripts/ci-comment-rot-prose.test.mjs +0 -2025
  27. package/scripts/ci-completes-premise-prose.test.mjs +0 -257
  28. package/scripts/ci-state-prose.test.mjs +0 -113
  29. package/scripts/ci-state.test.mjs +0 -1933
  30. package/scripts/ci-vacuous-green.test.mjs +0 -368
  31. package/scripts/citation-sweep-prose.test.mjs +0 -1712
  32. package/scripts/claim-lifecycle.test.mjs +0 -187
  33. package/scripts/claim-ticket.test.mjs +0 -1122
  34. package/scripts/closing-keyword-prose.test.mjs +0 -193
  35. package/scripts/cockpit-launch-idempotent-prose.test.mjs +0 -165
  36. package/scripts/cockpit-tick-nonblocking.test.mjs +0 -210
  37. package/scripts/compute-board.test.mjs +0 -1214
  38. package/scripts/compute-spend.test.mjs +0 -517
  39. package/scripts/consumer-technology-prose.test.mjs +0 -126
  40. package/scripts/cross-repo-citation-prose.test.mjs +0 -155
  41. package/scripts/cwd-isolation-pins.mjs +0 -83
  42. package/scripts/dedupe-guard-prose.test.mjs +0 -243
  43. package/scripts/delete-merged-branch.test.mjs +0 -576
  44. package/scripts/derive-testcmd.test.mjs +0 -595
  45. package/scripts/diff-stats.test.mjs +0 -404
  46. package/scripts/dispatch-block-golden-prose.test.mjs +0 -1130
  47. package/scripts/dispatch-block-pins-prose.test.mjs +0 -615
  48. package/scripts/dispositions-check.test.mjs +0 -936
  49. package/scripts/dispositions-gate-prose.test.mjs +0 -129
  50. package/scripts/drop-merged-label.test.mjs +0 -479
  51. package/scripts/exec-stub.mjs +0 -93
  52. package/scripts/exec-stub.test.mjs +0 -69
  53. package/scripts/expected-jobs-prose.test.mjs +0 -80
  54. package/scripts/fan-out-authorization-prose.test.mjs +0 -80
  55. package/scripts/filing-bar-seat-prose.test.mjs +0 -242
  56. package/scripts/filing-claim-bar-prose.test.mjs +0 -166
  57. package/scripts/finisher-died-prose.test.mjs +0 -31
  58. package/scripts/finisher-dispatch-premise-prose.test.mjs +0 -312
  59. package/scripts/finisher-halt-prose.test.mjs +0 -106
  60. package/scripts/finisher-name-prose.test.mjs +0 -134
  61. package/scripts/finisher-own-tree-prose.test.mjs +0 -299
  62. package/scripts/finisher-pin-race-prose.test.mjs +0 -339
  63. package/scripts/finisher-test-verdict-prose.test.mjs +0 -102
  64. package/scripts/fix-applier-correction-rules-prose.test.mjs +0 -202
  65. package/scripts/fix-applier-result-file-prose.test.mjs +0 -149
  66. package/scripts/fleet-bootstrap.test.mjs +0 -75
  67. package/scripts/fleet-heartbeat.test.mjs +0 -450
  68. package/scripts/fleet-provenance.test.mjs +0 -138
  69. package/scripts/fleet-run.test.mjs +0 -90
  70. package/scripts/fleet-state.test.mjs +0 -460
  71. package/scripts/fleet-tick-prose.test.mjs +0 -300
  72. package/scripts/fleet-tick.test.mjs +0 -2444
  73. package/scripts/force-with-lease-reason-prose.test.mjs +0 -184
  74. package/scripts/frontmatter-check.test.mjs +0 -436
  75. package/scripts/git-env.test.mjs +0 -149
  76. package/scripts/git-status-untracked-mode-sweep.test.mjs +0 -216
  77. package/scripts/gone-walk.test.mjs +0 -213
  78. package/scripts/gp-sep-invariant-prose.test.mjs +0 -188
  79. package/scripts/head-moved-after-label-prose.test.mjs +0 -233
  80. package/scripts/immutable-body-claim-prose.test.mjs +0 -204
  81. package/scripts/implementer-model-tier.test.mjs +0 -235
  82. package/scripts/inflight-citation-prose.test.mjs +0 -144
  83. package/scripts/inflight-exit2-prose.test.mjs +0 -81
  84. package/scripts/inflight-probe-guard-prose.test.mjs +0 -109
  85. package/scripts/inflight-scan-scope-prose.test.mjs +0 -57
  86. package/scripts/inflight.test.mjs +0 -2882
  87. package/scripts/injection-control-prose.test.mjs +0 -404
  88. package/scripts/install-root-audit.test.mjs +0 -464
  89. package/scripts/instrument-check-prose.test.mjs +0 -235
  90. package/scripts/instruments.test.mjs +0 -787
  91. package/scripts/is-cli.test.mjs +0 -99
  92. package/scripts/issue-tracker-prose.test.mjs +0 -71
  93. package/scripts/json.test.mjs +0 -364
  94. package/scripts/ledger-dispatch.test.mjs +0 -927
  95. package/scripts/ledger-grammar.test.mjs +0 -227
  96. package/scripts/ledger-read-require-file.test.mjs +0 -197
  97. package/scripts/ledger-rotate.test.mjs +0 -142
  98. package/scripts/ledger.test.mjs +0 -3436
  99. package/scripts/line-distance-prose.test.mjs +0 -245
  100. package/scripts/liveness-rationale-prose.test.mjs +0 -83
  101. package/scripts/locale-pin-prose.test.mjs +0 -189
  102. package/scripts/main-checkout.test.mjs +0 -332
  103. package/scripts/member-outcome-prose.test.mjs +0 -167
  104. package/scripts/member-outcomes-header.test.mjs +0 -129
  105. package/scripts/member-outcomes-prose.test.mjs +0 -63
  106. package/scripts/member-outcomes.test.mjs +0 -638
  107. package/scripts/member-prompt-prose.test.mjs +0 -165
  108. package/scripts/member-record.test.mjs +0 -541
  109. package/scripts/member-write-guard.test.mjs +0 -214
  110. package/scripts/merge-commit-probe-prose.test.mjs +0 -122
  111. package/scripts/merge-gate.test.mjs +0 -613
  112. package/scripts/muted-git-guard-sweep.test.mjs +0 -493
  113. package/scripts/net-ssh-precedence-prose.test.mjs +0 -91
  114. package/scripts/net-watchdog-prose.test.mjs +0 -79
  115. package/scripts/net.test.mjs +0 -211
  116. package/scripts/no-ci-gate-prose.test.mjs +0 -108
  117. package/scripts/no-undo-audit.test.mjs +0 -3961
  118. package/scripts/no-wave.test.mjs +0 -138
  119. package/scripts/node-floor-sweep.test.mjs +0 -611
  120. package/scripts/pointer-target-prose.test.mjs +0 -186
  121. package/scripts/pr-cost.test.mjs +0 -521
  122. package/scripts/pr-overlap.test.mjs +0 -692
  123. package/scripts/printf-die-sweep.test.mjs +0 -384
  124. package/scripts/prompt-renderer.mjs +0 -40
  125. package/scripts/prompt-renderer.test.mjs +0 -68
  126. package/scripts/prose-pin.mjs +0 -807
  127. package/scripts/prose-pin.test.mjs +0 -1034
  128. package/scripts/prove-merge.test.mjs +0 -995
  129. package/scripts/pull-table-relabel-prose.test.mjs +0 -60
  130. package/scripts/quiet-payload-prose.test.mjs +0 -222
  131. package/scripts/reap-cherry-check-prose.test.mjs +0 -80
  132. package/scripts/reap.test.mjs +0 -4432
  133. package/scripts/reaping-prose.test.mjs +0 -239
  134. package/scripts/reaping-reap-authorizers-prose.test.mjs +0 -112
  135. package/scripts/reaping-release-authorizers-prose.test.mjs +0 -85
  136. package/scripts/recipe-derivation-prose.test.mjs +0 -112
  137. package/scripts/recipe-prove.test.mjs +0 -1042
  138. package/scripts/refuter-scratch-prose.test.mjs +0 -135
  139. package/scripts/release-label-required-prose.test.mjs +0 -71
  140. package/scripts/release-label-split-prose.test.mjs +0 -184
  141. package/scripts/release-ticket-reap-pairing-prose.test.mjs +0 -63
  142. package/scripts/release-ticket.test.mjs +0 -5099
  143. package/scripts/renovate-release-contract.test.mjs +0 -379
  144. package/scripts/repo-root.test.mjs +0 -873
  145. package/scripts/reporter-output-rule-prose.test.mjs +0 -208
  146. package/scripts/review-agent-report-only.test.mjs +0 -49
  147. package/scripts/review-core-booking.test.mjs +0 -35
  148. package/scripts/review-core-cwd-isolation.test.mjs +0 -374
  149. package/scripts/review-core-reads.test.mjs +0 -478
  150. package/scripts/review-core-refuter-scratch.test.mjs +0 -148
  151. package/scripts/review-core-scratch-refusal.test.mjs +0 -64
  152. package/scripts/review-core-shared-test-run.test.mjs +0 -275
  153. package/scripts/review-core-snapshot-path.test.mjs +0 -830
  154. package/scripts/review-core-specialist-scratch.test.mjs +0 -191
  155. package/scripts/review-core-testcmd.test.mjs +0 -335
  156. package/scripts/review-core-unrun.test.mjs +0 -566
  157. package/scripts/review-core-unverified-discriminant.test.mjs +0 -208
  158. package/scripts/review-eval-resolver-path.test.mjs +0 -55
  159. package/scripts/review-host-fixture.mjs +0 -89
  160. package/scripts/review-in-run-retry.test.mjs +0 -211
  161. package/scripts/review-no-vendor-dispatch.test.mjs +0 -45
  162. package/scripts/review-path-default.test.mjs +0 -796
  163. package/scripts/review-runner-spawns.test.mjs +0 -95
  164. package/scripts/review-runner.test.mjs +0 -166
  165. package/scripts/review-tier-audit.test.mjs +0 -140
  166. package/scripts/run-artifact-pr-prose.test.mjs +0 -341
  167. package/scripts/run-merge-bot-prose.test.mjs +0 -1413
  168. package/scripts/run-team-local-hook.test.mjs +0 -442
  169. package/scripts/scratch-partition-invariant-prose.test.mjs +0 -74
  170. package/scripts/scripts-path-citation-sweep.test.mjs +0 -73
  171. package/scripts/section-between-guard-prose.test.mjs +0 -379
  172. package/scripts/select-dimensions.test.mjs +0 -674
  173. package/scripts/shared-refusal.test.mjs +0 -388
  174. package/scripts/shell-traps-prose.test.mjs +0 -504
  175. package/scripts/shortlist.test.mjs +0 -380
  176. package/scripts/slow-transport.mjs +0 -72
  177. package/scripts/snapshot-repo.test.mjs +0 -1006
  178. package/scripts/snapshot-runner-audience-prose.test.mjs +0 -130
  179. package/scripts/spend-view.test.mjs +0 -312
  180. package/scripts/staleness-qualifier-prose.test.mjs +0 -251
  181. package/scripts/staleness-verdict-prose.test.mjs +0 -142
  182. package/scripts/staleness.test.mjs +0 -893
  183. package/scripts/stalled-member-liveness-prose.test.mjs +0 -219
  184. package/scripts/stash-prohibition-prose.test.mjs +0 -313
  185. package/scripts/strip-comments.mjs +0 -44
  186. package/scripts/temp-dir.mjs +0 -47
  187. package/scripts/ticket-router.test.mjs +0 -981
  188. package/scripts/tier-check.test.mjs +0 -828
  189. package/scripts/tier-outcomes-header.test.mjs +0 -518
  190. package/scripts/tier-outcomes.test.mjs +0 -572
  191. package/scripts/tier-roles.test.mjs +0 -535
  192. package/scripts/tracker-block-copy-prose.test.mjs +0 -288
  193. package/scripts/triage-labels-gloss-prose.test.mjs +0 -97
  194. package/scripts/unattended-git-sweep.test.mjs +0 -142
  195. package/scripts/verify-sha-exit2-prose.test.mjs +0 -269
  196. package/scripts/verify-sha.test.mjs +0 -692
  197. package/scripts/watcher-degraded-block.test.mjs +0 -369
  198. package/scripts/watcher-key-prose.test.mjs +0 -106
  199. package/scripts/within-run-pair-prose.test.mjs +0 -212
  200. package/scripts/worktree-audit.test.mjs +0 -1408
  201. package/scripts/worktree-head-prose.test.mjs +0 -276
  202. package/scripts/worktree-listing-sweep.test.mjs +0 -217
  203. package/scripts/worktree-model-prose.test.mjs +0 -216
  204. package/scripts/worktree-porcelain.mjs +0 -35
  205. package/scripts/worktree-porcelain.test.mjs +0 -102
@@ -1,1122 +0,0 @@
1
- import { test } from "node:test";
2
- import assert from "node:assert/strict";
3
- import { execFileSync, spawnSync } from "node:child_process";
4
- import { mkdtempSync, mkdirSync, writeFileSync, readFileSync, chmodSync, existsSync, symlinkSync, copyFileSync } from "node:fs";
5
- import { tmpdir } from "node:os";
6
- import { tempDir } from "./temp-dir.mjs";
7
- import { join, dirname } from "node:path";
8
- import { writeExecStub } from "./exec-stub.mjs";
9
-
10
- const SCRIPT = join(import.meta.dirname, "claim-ticket.sh");
11
-
12
- // Fixture construction must not inherit the two variables this file's own
13
- // #1020 cases set deliberately at the bottom. Under an ambient GIT_DIR the
14
- // `git init` in `repo()` exits 0 and creates NOTHING in `dir` — it re-inits
15
- // whatever GIT_DIR names — so every fixture in this file would be built
16
- // against the wrong repository while the suite stayed green (ledger.test.mjs
17
- // records the same trap). The two cases that need these variables pass them
18
- // explicitly to the SCRIPT, never to the builder.
19
- const FIXTURE_ENV = { ...process.env, GIT_DIR: undefined, GIT_WORK_TREE: undefined };
20
-
21
- // Build a repo whose origin/main holds `files`. `local` is written to the
22
- // working tree afterwards WITHOUT committing — that is how a checkout diverges
23
- // from the ref the worktree is actually built from.
24
- //
25
- // `recipe` is the repository's Recipe cache (ADR 0015), written untracked to
26
- // `.fleet/recipe.json` beside the main checkout's git dir — where
27
- // derive-testcmd.sh reads it — proven at origin/main. Every field is
28
- // overridable; `null` writes no cache at all. The default is the smallest
29
- // Recipe that runs anywhere: nothing to install, nothing to fail.
30
- function repo(files, local = {}, recipe = {}) {
31
- const dir = tempDir("claim-");
32
- const git = (...a) => execFileSync("git", a, { cwd: dir, stdio: "pipe", env: FIXTURE_ENV, encoding: "utf8" });
33
- git("init", "-q");
34
- git("config", "user.email", "t@t");
35
- git("config", "user.name", "t");
36
- for (const [name, body] of Object.entries(files)) {
37
- mkdirSync(dirname(join(dir, name)), { recursive: true });
38
- writeFileSync(join(dir, name), body);
39
- }
40
- git("add", "-A");
41
- git("commit", "-q", "--allow-empty", "-m", "x");
42
- git("update-ref", "refs/remotes/origin/main", "HEAD");
43
- for (const [name, body] of Object.entries(local)) {
44
- mkdirSync(dirname(join(dir, name)), { recursive: true });
45
- writeFileSync(join(dir, name), body);
46
- }
47
- if (recipe !== null) {
48
- mkdirSync(join(dir, ".fleet"), { recursive: true });
49
- writeFileSync(join(dir, ".fleet", "recipe.json"), JSON.stringify({
50
- install: "true",
51
- test: "true",
52
- derivedAt: git("rev-parse", "HEAD").trim(),
53
- installClean: true,
54
- testCount: 1,
55
- ...recipe,
56
- }));
57
- }
58
- return dir;
59
- }
60
-
61
- // Returns {install, testcmd} on success, or {err} with the refusal message.
62
- function claim(dir) {
63
- const r = spawnSync("sh", [SCRIPT, "42", "slug", "fix"], { cwd: dir, encoding: "utf8" });
64
- const out = r.stdout + r.stderr;
65
- if (r.status !== 0) return { err: out };
66
- return {
67
- install: out.match(/Install step → (.*)/)?.[1],
68
- testcmd: out.match(/test entrypoint → (.*)/)?.[1],
69
- };
70
- }
71
-
72
- const TESTS = "run-tests.sh";
73
-
74
- // Runs the script for real and returns the emitted runner plus its worktree.
75
- // `--apply` labels the issue, so `gh` is stubbed; everything else — the
76
- // worktree, the install, the exclude file, the runner — is the real thing.
77
- // The runner is what members actually invoke, so it is what gets asserted on.
78
- // `script` defaults to the real one; pass a copy to claim from a different
79
- // template.
80
- function apply(files, script = SCRIPT, recipe = {}) {
81
- const dir = repo(files, {}, recipe);
82
- const bin = tempDir("claim-bin-");
83
- writeExecStub(join(bin, "gh"), "#!/bin/sh\nexit 0\n");
84
- const r = spawnSync("sh", [script, "42", "slug", "fix", "--apply"], {
85
- cwd: dir,
86
- encoding: "utf8",
87
- env: { ...process.env, PATH: `${bin}:${process.env.PATH}` },
88
- });
89
- assert.equal(r.status, 0, r.stdout + r.stderr);
90
- const wt = join(dir, ".worktrees", "42-slug");
91
- return {
92
- dir,
93
- wt,
94
- receipt: JSON.parse(r.stdout),
95
- text: readFileSync(join(wt, "agent-test"), "utf8"),
96
- run: (...args) => spawnSync(join(wt, "agent-test"), args, { cwd: wt, encoding: "utf8" }),
97
- };
98
- }
99
-
100
- test("a zero-padded issue is refused", () => {
101
- // #121: `$issue` reaches a JSON number slot (`"issue":%s`), where all-digits
102
- // is not enough — RFC 8259 forbids a leading zero, so `007` emitted
103
- // `{"issue":007,…}` that no parser accepts. It also reaches `$((16000 +
104
- // issue))`, where /bin/sh reads `010` as octal 8 and refuses `008` outright,
105
- // so a padded number could silently derive another claim's ports.
106
- // Two widths, because one does not pin the guard: `0?*` narrowed to `0??*`
107
- // still refuses `007` and re-admits `01`, which is the same bug back.
108
- for (const padded of ["007", "01"]) {
109
- const r = spawnSync("sh", [SCRIPT, padded, "slug", "fix"], { cwd: tmpdir(), encoding: "utf8" });
110
- assert.equal(r.status, 2, padded);
111
- assert.match(r.stderr, /issue must be a number/);
112
- }
113
- });
114
-
115
- test("a bare 0 clears the numeric guard", () => {
116
- // `0?*`, not `0*`: #121 lists `sh inflight.sh 0 -> parses` among its PASSING
117
- // cases, beside `42`. A bare `0` is a valid RFC 8259 number and `$((0))` is
118
- // `0`, so neither hazard the guard exists to close applies to it. Widening
119
- // the arm would refuse a value the ticket's own worked example shows working.
120
- const r = spawnSync("sh", [SCRIPT, "0", "slug", "fix"], { cwd: tmpdir(), encoding: "utf8" });
121
- assert.doesNotMatch(r.stderr, /issue must be a number/);
122
- // Positive, not just the absence of one string: `0` has to reach the *next*
123
- // precondition. A refusal worded differently, or added ahead of the guard,
124
- // never gets here and cannot leave this message behind.
125
- assert.match(r.stderr, /not inside a git repository/);
126
- });
127
-
128
- // #760 — that a claim carries no upstream, and what that means to reap.sh and
129
- // release-ticket.sh — is pinned in claim-lifecycle.test.mjs, not here. This
130
- // file's `repo()` fixture fabricates refs/remotes/origin/main with
131
- // `update-ref` and configures no `origin` remote, so git declines to set an
132
- // upstream from it at all: every assertion about upstream config passes here
133
- // whatever `worktree add` was handed. Measured — a config pin in this fixture
134
- // stayed green with `--no-track` removed.
135
-
136
- // --- The runner: a thin exec of the Recipe's Test entrypoint (ADR 0015).
137
-
138
- // A script that reports exactly what it was handed — its argv one per line,
139
- // then the isolation triple — so the runner's thinness is measured rather than
140
- // assumed.
141
- const ECHO_TESTS = '#!/bin/sh\nfor a do printf "arg:%s\\n" "$a"; done\nprintf "env:%s %s %s\\n" "$TEST_COMPOSE_PROJECT" "$TEST_POSTGRES_PORT" "$TEST_OLLAMA_PORT"\n';
142
-
143
- // The ticket's own acceptance row: a repository with NO package.json — nothing
144
- // any technology table could have recognised — claims on its cache alone, and
145
- // `./agent-test` runs the Test entrypoint, arguments through verbatim (one
146
- // holding a space stays one), under the per-claim isolation exports.
147
- test("runner: a repo with no package.json claims on its Recipe cache and ./agent-test runs the Test entrypoint", () => {
148
- const a = apply({ "run-tests.sh": ECHO_TESTS }, SCRIPT, { install: "true", test: "sh ./run-tests.sh" });
149
- assert.equal(existsSync(join(a.wt, "package.json")), false, "fixture: nothing Node-shaped in the tree");
150
- assert.equal(a.receipt.install, "true");
151
- const r = a.run("one", "two words");
152
- assert.equal(r.status, 0, r.stdout + r.stderr);
153
- assert.equal(r.stdout, "arg:one\narg:two words\nenv:ab-42 16042 22042\n");
154
- });
155
-
156
- // Thin means the runner adds no verdict of its own: the suite's exit status
157
- // IS the runner's. A red suite is a finding, and must reach the caller as red.
158
- test("runner: the Test entrypoint's own exit status is the runner's", () => {
159
- const a = apply({ "run-tests.sh": "#!/bin/sh\necho red\nexit 3\n" }, SCRIPT, { test: "sh ./run-tests.sh" });
160
- const r = a.run();
161
- assert.equal(r.status, 3);
162
- assert.equal(r.stdout, "red\n");
163
- });
164
-
165
- // The Test entrypoint is a shell command, and the runner must not reinterpret
166
- // it: a single quote survives the runner's own quoting, nothing in it expands
167
- // at write time, and a compound command keeps its meaning with the runner's
168
- // arguments appended to its last simple command.
169
- test("runner: a quoted or compound Test entrypoint runs exactly as written", () => {
170
- const quoted = apply({}, SCRIPT, { test: `echo "it's \\$HOME's"` });
171
- assert.match(quoted.text, /^exec sh -c '/m);
172
- const q = quoted.run("x");
173
- assert.equal(q.status, 0, q.stderr);
174
- assert.equal(q.stdout, "it's $HOME's x\n");
175
-
176
- const compound = apply({ "sub/t.sh": ECHO_TESTS }, SCRIPT, { test: "cd sub && sh ./t.sh" });
177
- const c = compound.run("y");
178
- assert.equal(c.status, 0, c.stderr);
179
- assert.match(c.stdout, /^arg:y\n/);
180
- });
181
-
182
- // A repository that tracks its own `agent-test` keeps it — the repo-local
183
- // runner ADR 0015 sends any convenience beyond the thin exec to (this repo's
184
- // own node --test shim is one). Written over, it would be a modified TRACKED
185
- // path every release then strands on (#1262), so it is left byte-identical and
186
- // not added to the exclude list.
187
- test("runner: a tracked repo-local agent-test is left as is", () => {
188
- const own = "#!/bin/sh\necho repo-local\n";
189
- const a = apply({ "agent-test": own }, SCRIPT, { test: "sh ./run-tests.sh" });
190
- assert.equal(a.text, own);
191
- const status = execFileSync("git", ["-C", a.wt, "status", "--porcelain"], { encoding: "utf8", env: FIXTURE_ENV });
192
- assert.equal(status, "", "the tracked runner must not show as modified");
193
- });
194
-
195
- // regardless of whether the runner branch above actually exported it. A
196
- // tracked repo-local agent-test never receives those exports (previous
197
- // test), so a receipt claiming them would tell a caller to trust isolation
198
- // that is not there (measured: the echoed env was empty against a real
199
- // tracked runner). `null` is the honest value — the object appears only when
200
- // this script wrote the exports itself.
201
- test("runner: a tracked repo-local agent-test's receipt does not claim ports it never exported", () => {
202
- const own = "#!/bin/sh\necho repo-local\n";
203
- const a = apply({ "agent-test": own }, SCRIPT, { test: "sh ./run-tests.sh" });
204
- assert.equal(a.receipt.ports, null, "isolation was never exported into the tracked runner");
205
- assert.equal(a.receipt.applied, true, "the claim itself still went through");
206
- });
207
-
208
- // #124: the runner is written once at claim time and never rewritten, so an
209
- // old worktree can hold a runner a later template fix never reached. Nothing
210
- // in the file said which template produced it — this pins the fix.
211
- const STAMP_RE = /^# agent-test template: (\S+)$/m;
212
-
213
- test("runner: carries a template stamp", () => {
214
- const { text } = apply({});
215
- assert.match(text, STAMP_RE);
216
- });
217
-
218
- // Same script, two claims — the stamp is a property of this script's own
219
- // bytes, not of the claim, so it must not vary with the issue number, ports,
220
- // or the Recipe baked into the rest of the file.
221
- test("runner: the stamp is stable across claims of the same template", () => {
222
- const a = apply({}, SCRIPT, { test: "true" }).text.match(STAMP_RE)[1];
223
- const b = apply({}, SCRIPT, { test: "false" }).text.match(STAMP_RE)[1];
224
- assert.equal(a, b);
225
- });
226
-
227
- // The other half: point a claim at a byte-for-byte-different copy of the
228
- // script and the stamp must move. Copying rather than editing the real
229
- // script in place keeps this test from mutating the file under test.
230
- test("runner: the stamp changes when the script's content changes", () => {
231
- const scriptDir = tempDir("claim-script-");
232
- const editedScript = join(scriptDir, "claim-ticket.sh");
233
- writeFileSync(editedScript, readFileSync(SCRIPT, "utf8") + "\n# a harmless edit\n");
234
- // claim-ticket.sh resolves three siblings relative to itself
235
- // (`$(dirname -- "$0")`): derive-testcmd.sh for the testcmd, json.sh for the
236
- // payload escaping (#119), and worktree.sh for `gone()`, the established-absent
237
- // predicate the worktree guard asks (#727). All three have to travel with this
238
- // edited copy or the script refuses before it emits anything — which is the
239
- // guard working, not a regression.
240
- const sibling = join(scriptDir, "derive-testcmd.sh");
241
- writeExecStub(sibling, readFileSync(join(import.meta.dirname, "derive-testcmd.sh"), "utf8"));
242
- for (const lib of ["json.sh", "worktree.sh"]) {
243
- copyFileSync(join(import.meta.dirname, lib), join(scriptDir, lib));
244
- }
245
-
246
- const after = apply({}, editedScript).text.match(STAMP_RE)[1];
247
- const before = apply({}).text.match(STAMP_RE)[1];
248
-
249
- assert.notEqual(after, before);
250
- });
251
-
252
- // #263: the stamp was derived by `cksum "$0" | cut -d' ' -f1`, whose status is
253
- // `cut`'s, so `set -eu` never saw a `cksum` that could not read its operand —
254
- // measured on that form, the runner carried nothing after the colon and the
255
- // claim exited 0 reporting itself applied. And it was derived after the issue
256
- // had been labelled and after `git worktree add`, so reading the status ALONE
257
- // is the change that was refused: it exchanges a blank stamp for a refusal on
258
- // a ticket that is already half-claimed. Reading the status is also not enough
259
- // by itself — a `cksum` that exits 0 printing nothing leaves the stamp empty
260
- // and ships that same blank line at exit 0 with the ticket claimed, which is
261
- // why the value is read as well as the status. All three are pinned here.
262
- //
263
- // The stubs are the fixture, not a route: every call site this repo has, in
264
- // the skill text and in this file, names the script by an absolute path, so
265
- // `$0` always resolves and no argv makes the real `cksum` fail or come back
266
- // empty. Nothing here claims a reachable failure — what is pinned is that the
267
- // derivation reports on its own result and does so before anything is
268
- // claimed.
269
- //
270
- // Exit 2, an empty stdout and a matching stderr are each reproducible by some
271
- // other guard, so none of them establishes WHERE the refusal fired. The `gh`
272
- // log is what only a refusal ahead of every mutation can satisfy — it is the
273
- // criterion that separates this fix from the one that was refused. Measured:
274
- // `gh issue edit` is the first mutation the apply branch makes, so under every
275
- // placement of the derivation this script allows, the `gh` log is the
276
- // assertion that reds. A registration check and a branch-ref check stood here
277
- // too and could never fire ahead of it; the worktree check that remains states
278
- // the same refusal in the form the json.sh guard's own test uses.
279
- //
280
- // The dry run is measured too, in the same fixture: it is this script's
281
- // default mode, and the refusal reaching it is what shows the derivation sits
282
- // with the pre-branch derivations rather than inside the apply branch, where
283
- // the default mode never reaches it at all.
284
- // Three stubs, because the two halves of the guard cover each other on the
285
- // obvious ones and a stub each half owns alone is what discriminates.
286
- // Measured on this tree: with only the first two, restoring the pipeline form
287
- // `cksum "$0" | cut -d' ' -f1` — the #263 bug itself — leaves both green,
288
- // because a failing `cksum` prints nothing, `cut` succeeds on empty input, and
289
- // the value check refuses what the status check was meant to. Dropping the
290
- // `|| die` to a bare `|| true` goes green the same way.
291
- // - "cannot be read" rc 1, prints nothing. The realistic shape. Reds
292
- // only when BOTH halves are gone; either alone
293
- // catches it.
294
- // - "comes back empty" rc 0, prints nothing. Owned by the value check —
295
- // deleting that line is the mutation it reds.
296
- // - "fails despite printing" rc 1, prints a plausible checksum line. Owned
297
- // by the status check: the value survives `cut`, so
298
- // this is the stub the pipeline form and the bare
299
- // `|| true` both red.
300
- for (const [what, stub] of [
301
- ["cannot be read", "#!/bin/sh\nexit 1\n"],
302
- ["comes back empty", "#!/bin/sh\nexit 0\n"],
303
- ["fails despite printing", "#!/bin/sh\necho '111 222 x'\nexit 1\n"],
304
- ]) {
305
- test(`a checksum that ${what} refuses before anything is claimed, in both modes`, () => {
306
- const dir = repo({ [TESTS]: "" });
307
- const bin = tempDir("claim cksum-");
308
- assert.match(bin, / /, "fixture: the stub's directory must hold a space, or this pins nothing");
309
- const ghLog = join(bin, "gh.log");
310
- // `>> "$GH_LOG"`, not the interpolated path (#880). The path comes from
311
- // `mkdtempSync(join(tmpdir(), …))` and so inherits `TMPDIR`; unquoted, a
312
- // space in it word-split the redirect, the shell wrote to the path's first
313
- // word and `echo` took the rest as an argument, and the log was never
314
- // created — which the assertion below reads as "gh was never invoked".
315
- // ci-state.test.mjs's GH_STUB form keeps the path out of the script text.
316
- // The prefix above now holds a space so this shape is exercised on every
317
- // machine, not only one whose TMPDIR has one.
318
- writeExecStub(join(bin, "gh"), `#!/bin/sh\necho "$@" >> "$GH_LOG"\nexit 0\n`);
319
- writeExecStub(join(bin, "cksum"), stub);
320
- const env = { ...process.env, PATH: `${bin}:${process.env.PATH}`, GH_LOG: ghLog };
321
-
322
- const r = spawnSync("sh", [SCRIPT, "42", "slug", "fix", "--apply"], { cwd: dir, encoding: "utf8", env });
323
-
324
- assert.equal(r.status, 2, `a stamp this script cannot derive is a refusal — its only failure code\n${r.stderr}`);
325
- assert.match(r.stderr, /refusing to claim without a runner template stamp/,
326
- "and it names the stamp rather than blaming whatever ran next");
327
- assert.equal(r.stdout, "", "no payload: the refusal fires before the claim exists, so there is nothing to report");
328
-
329
- // Nothing claimed. Both were TRUE under the guard-in-place form that was
330
- // refused, which is why the code and the message above cannot stand in
331
- // for them.
332
- assert.equal(existsSync(ghLog), false,
333
- "the issue is unlabelled — `gh` was never invoked, so there is no label to undo");
334
- assert.equal(existsSync(join(dir, ".worktrees", "42-slug")), false, "and no worktree on disk");
335
-
336
- // The default mode reaches the same derivation. Moved back inside the apply
337
- // branch it would not, and this claim would be predicted as makeable.
338
- const d = spawnSync("sh", [SCRIPT, "42", "slug", "fix"], { cwd: dir, encoding: "utf8", env });
339
- assert.equal(d.status, 2, `the dry run refuses too\n${d.stderr}`);
340
- assert.match(d.stderr, /refusing to claim without a runner template stamp/);
341
- assert.equal(d.stdout, "", "and predicts no claim it could not make");
342
-
343
- // The must-ACCEPT half for the stub itself, not for the script: the
344
- // assertion above is satisfied by a `gh` that cannot record, so without
345
- // this the pin cannot tell a refusal from a broken fixture. Runs last so
346
- // it cannot perturb either measurement above.
347
- assert.equal(spawnSync("sh", ["-c", "gh issue edit 42"], { cwd: dir, encoding: "utf8", env }).status, 0,
348
- "fixture: the stub must be the gh on PATH");
349
- assert.equal(existsSync(ghLog), true,
350
- "the stub cannot record a call it did receive, so the assertion above passes vacuously");
351
- });
352
- }
353
-
354
- // --- The Recipe (ADR 0015): both commands are READ from the cache through
355
- // derive-testcmd.sh, never inferred from the tree.
356
-
357
- test("the Install step and the Test entrypoint come from the Recipe cache, verbatim", () => {
358
- const { install, testcmd, err } = claim(repo({ [TESTS]: "" }, {}, { install: "exit 0", test: "sh ./run-tests.sh --all" }));
359
- assert.equal(err, undefined);
360
- assert.equal(install, "exit 0");
361
- assert.equal(testcmd, "sh ./run-tests.sh --all");
362
- });
363
-
364
- // Absent means "derive", never "infer" — and the refusal must say which step
365
- // derives it, in claim-ticket's own voice so a reader sees whose claim failed.
366
- // Both modes, before anything exists: the dry run is where a controller learns
367
- // it, and --apply must not have labelled the issue first.
368
- test("an absent Recipe cache refuses the claim before anything is claimed, naming the derivation step", () => {
369
- const dir = repo({ [TESTS]: "" }, {}, null);
370
- const dry = claim(dir);
371
- assert.match(dry.err, /claim-ticket: derive-testcmd: no Recipe cache at .*\/\.fleet\/recipe\.json/);
372
- assert.match(dry.err, /run the Recipe derivation step \(run-team phase 0/);
373
-
374
- const bin = tempDir("claim-bin-");
375
- const ghLog = join(bin, "gh.log");
376
- writeExecStub(join(bin, "gh"), '#!/bin/sh\necho "$@" >> "$GH_LOG"\nexit 0\n');
377
- const r = spawnSync("sh", [SCRIPT, "42", "slug", "fix", "--apply"], {
378
- cwd: dir, encoding: "utf8", env: { ...process.env, PATH: `${bin}:${process.env.PATH}`, GH_LOG: ghLog },
379
- });
380
- assert.equal(r.status, 2);
381
- assert.equal(r.stdout, "", "no receipt for a claim that was never made");
382
- assert.equal(existsSync(ghLog), false, "the issue was never labelled");
383
- assert.equal(existsSync(join(dir, ".worktrees", "42-slug")), false, "and no worktree exists");
384
- });
385
-
386
- // The acceptance row: a Test entrypoint naming a binary that does not exist
387
- // is a Recipe that cannot RUN — an invalid cache, refused with the invalidation
388
- // message rather than handed to a runner that would die on every invocation.
389
- test("a Recipe whose Test entrypoint names a missing binary refuses as an invalid cache", () => {
390
- const { err } = claim(repo({ [TESTS]: "" }, {}, { test: "no-such-runner-2117 --all" }));
391
- assert.match(err, /^claim-ticket: derive-testcmd: the Recipe cache at .* is invalid: its test command 'no-such-runner-2117' is not found or not executable/m);
392
- assert.match(err, /run the Recipe derivation step/);
393
- });
394
-
395
- // The worktree is built from origin/main, and the cache read no longer reads
396
- // any ref — so the precondition is asked directly, in the dry run too, where
397
- // nothing downstream would otherwise refuse before `worktree add`.
398
- test("a repo with no origin/main refuses before anything is claimed", () => {
399
- const dir = repo({ [TESTS]: "" });
400
- execFileSync("git", ["update-ref", "-d", "refs/remotes/origin/main"], { cwd: dir, env: FIXTURE_ENV });
401
- const { err } = claim(dir);
402
- assert.match(err, /claim-ticket: origin\/main does not resolve to a commit/);
403
- });
404
-
405
- // Installs a claim with the given Recipe and returns the spawn result — the
406
- // Install step only runs under --apply.
407
- function applyRecipe(recipe, files = { [TESTS]: "" }, local = {}) {
408
- const dir = repo(files, local, recipe);
409
- const bin = tempDir("claim-bin-");
410
- writeExecStub(join(bin, "gh"), "#!/bin/sh\nexit 0\n");
411
- const r = spawnSync("sh", [SCRIPT, "42", "slug", "fix", "--apply"], {
412
- cwd: dir, encoding: "utf8", env: { ...process.env, PATH: `${bin}:${process.env.PATH}` },
413
- });
414
- return { dir, r };
415
- }
416
-
417
- // The acceptance row for the Install step: modifying ANY tracked file — not a
418
- // lockfile by name, fleet-ctl keeps no list of those — is a Recipe that no
419
- // longer holds, refused with the invalidation message and the file named.
420
- test("an Install step that modifies a tracked file refuses as an invalid cache", () => {
421
- const { r } = applyRecipe({ install: "echo drift >> run-tests.sh" });
422
- assert.equal(r.status, 2, r.stdout + r.stderr);
423
- assert.match(r.stderr, /the Install step 'echo drift >> run-tests\.sh' changed the tree in \.worktrees\/42-slug \(first: +M run-tests\.sh\) — the Recipe cache is invalid; run the Recipe derivation step/);
424
- assert.doesNotMatch(r.stdout, /"applied":true/);
425
- });
426
-
427
- // The must-ACCEPT half of the whole-tree guard, which is where it can go
428
- // wrong: an install writing only what the repository IGNORES (a dependency
429
- // directory, a build output) leaves `git status --porcelain` empty — the proof
430
- // the deriving agent ran — and must claim.
431
- test("an Install step that writes only ignored files claims cleanly", () => {
432
- const { r } = applyRecipe({ install: "mkdir -p deps && touch deps/x" }, { ".gitignore": "deps/\n", [TESTS]: "" });
433
- assert.equal(r.status, 0, r.stdout + r.stderr);
434
- assert.match(r.stderr, /tree clean after the Install step/);
435
- assert.match(r.stdout, /"applied":true/);
436
- });
437
-
438
- // Failure to RUN versus failure: 127 is the shell's own "not found" — an
439
- // invalid Recipe — while any other non-zero is the install failing, named as
440
- // that. The unrunnable one gets past derive-testcmd.sh's probe the realistic
441
- // way: the script exists (executable) in the main checkout the probe reads
442
- // from, but not at origin/main, which the worktree is built from.
443
- test("an Install step that cannot run is named an invalid cache; one that fails is named failing", () => {
444
- const dir = repo({ [TESTS]: "" }, { "setup.sh": "#!/bin/sh\n" }, { install: "./setup.sh" });
445
- chmodSync(join(dir, "setup.sh"), 0o755);
446
- const bin = tempDir("claim-bin-");
447
- writeExecStub(join(bin, "gh"), "#!/bin/sh\nexit 0\n");
448
- const env = { ...process.env, PATH: `${bin}:${process.env.PATH}` };
449
- const r = spawnSync("sh", [SCRIPT, "42", "slug", "fix", "--apply"], { cwd: dir, encoding: "utf8", env });
450
- assert.equal(r.status, 2, r.stdout + r.stderr);
451
- assert.match(r.stderr, /the Install step '\.\/setup\.sh' did not run in \.worktrees\/42-slug \(exit 127: not executable or not found\) — the Recipe cache is invalid/);
452
-
453
- const failing = applyRecipe({ install: "exit 4" });
454
- assert.equal(failing.r.status, 2);
455
- assert.match(failing.r.stderr, /install failed in \.worktrees\/42-slug \(exit 4\)/);
456
- assert.doesNotMatch(failing.r.stderr, /Recipe cache is invalid/, "a failing install is not a stale Recipe");
457
- });
458
-
459
- // The 126 half of the same classification: a script that IS at the resolved
460
- // path but lacks the execute bit is "found but not executable" — the shell's
461
- // own exit 126, distinct from 127's "not found" but pinned to the identical
462
- // invalid-cache wording since both mean the Recipe cannot run, not that it
463
- // failed. Committed non-executable (default `writeFileSync` mode, never
464
- // chmod'd before `git add`), so `origin/main`'s tree entry is 100644 and a
465
- // fresh worktree checks it out that way — but chmod'd +x on disk in the MAIN
466
- // checkout only, AFTER the commit, so derive-testcmd.sh's own resolvability
467
- // probe (which reads "." — the main checkout — directly, not through git)
468
- // still passes and this script gets to run it for real, the same asymmetry
469
- // the 127 case above exploits in the other direction.
470
- test("an Install step present but not executable is also named an invalid cache (exit 126)", () => {
471
- const dir = repo({ [TESTS]: "", "setup.sh": "#!/bin/sh\n" }, {}, { install: "./setup.sh" });
472
- chmodSync(join(dir, "setup.sh"), 0o755);
473
- const bin = tempDir("claim-bin-");
474
- writeExecStub(join(bin, "gh"), "#!/bin/sh\nexit 0\n");
475
- const env = { ...process.env, PATH: `${bin}:${process.env.PATH}` };
476
- const r = spawnSync("sh", [SCRIPT, "42", "slug", "fix", "--apply"], { cwd: dir, encoding: "utf8", env });
477
- assert.equal(r.status, 2, r.stdout + r.stderr);
478
- assert.match(r.stderr, /the Install step '\.\/setup\.sh' did not run in \.worktrees\/42-slug \(exit 126: not executable or not found\) — the Recipe cache is invalid/);
479
- });
480
-
481
- // #128: the tree-mutation check reads the same worktree-status hole
482
- // no-undo-audit.sh, reap.sh and worktree-audit.sh share. Delete the
483
- // worktree's own .git between `worktree add` and this check and `git -C`
484
- // does not fail — it walks UP to the enclosing repo and answers about THAT
485
- // at rc 0, which the old check would read as an untouched tree it never
486
- // actually looked at. Simulated with an Install step that corrupts the
487
- // worktree's own linkage, whatever a real cause for that would be — this
488
- // guard does not get to assume a cause, only detect the hole.
489
- // The other half of the same guard: `-f` is false for a `.git` that is absent
490
- // AND for one this process may not stat, so an install that leaves $wt
491
- // unsearchable was reported as a deletion — sending whoever cleans up (a
492
- // created worktree, a created branch and an in-progress label are left behind)
493
- // after a `.git` file that is sitting right there. Both refusals exit 2; only
494
- // the stated cause differs, which is exactly what triage reads.
495
- test("an unsearchable worktree refuses with git's own denial, never an absence nothing established", (t) => {
496
- if (process.getuid?.() === 0) return t.skip("root searches every directory");
497
- // Runs with cwd=$wt, so this strips the search bit off the worktree itself
498
- // and leaves .git entirely intact — the discriminating input.
499
- const { dir, r } = applyRecipe({ install: "chmod 000 ." });
500
- // Before the first assert: a red must not strand a directory nothing can
501
- // remove.
502
- chmodSync(join(dir, ".worktrees", "42-slug"), 0o755);
503
-
504
- assert.equal(r.status, 2);
505
- assert.doesNotMatch(r.stderr, /has no \.git file/, "the .git file was never deleted, only made unreachable");
506
- assert.match(r.stderr, /could not verify the tree state/);
507
- assert.match(r.stderr, /Permission denied/, "git's own denial, not one this script invented");
508
- assert.equal(existsSync(join(dir, ".worktrees", "42-slug", ".git")), true);
509
- });
510
-
511
- // #730 (see reap.sh's branch sweep for the full explanation) — the untracked
512
- // mode is CONFIG, so an install that CREATES a file the tree neither tracks
513
- // nor ignores is invisible at rc 0 unpinned.
514
- test("an install that creates an UNTRACKED file is caught under status.showUntrackedFiles=no (#730)", () => {
515
- const dir = repo({ [TESTS]: "" }, {}, { install: "printf '{}' > yarn.lock" });
516
- // On the repo's own config, so the linked worktree the check runs in shares it.
517
- execFileSync("git", ["config", "status.showUntrackedFiles", "no"], { cwd: dir, stdio: "pipe" });
518
- const bin = tempDir("claim-bin-");
519
- writeExecStub(join(bin, "gh"), "#!/bin/sh\nexit 0\n");
520
- const r = spawnSync("sh", [SCRIPT, "42", "slug", "fix", "--apply"], {
521
- cwd: dir,
522
- encoding: "utf8",
523
- env: { ...process.env, PATH: `${bin}:${process.env.PATH}` },
524
- });
525
-
526
- assert.equal(r.status, 2, r.stdout + r.stderr);
527
- // The stated cause, not merely a refusal: several guards in this chain exit 2,
528
- // and triage reads the reason.
529
- assert.match(r.stderr, /changed the tree in \.worktrees\/42-slug \(first: \?\? yarn\.lock\)/);
530
- });
531
-
532
- test("a worktree whose .git vanishes during install refuses instead of trusting a leaked parent status", () => {
533
- const { r } = applyRecipe({ install: "rm -rf .git" });
534
- assert.equal(r.status, 2);
535
- assert.match(r.stderr, /has no \.git file — cannot verify the Install step left the tree clean/);
536
- });
537
-
538
- // #188: `[ -e ]` stats, so it FOLLOWS symlinks, while `git worktree add` refuses
539
- // on lstat semantics. A DANGLING symlink at $wt splits the two — the guard looks
540
- // through it and sees nothing, while the path is still occupied and still fails
541
- // the `worktree add` below. `-L` closes that gap, and it is the same predicate
542
- // release-ticket.sh already ships as `occupied()`, for residue THIS fleet
543
- // leaves: where a symlink points AT the registered worktree directory, `git
544
- // worktree remove` deletes the directory and returns 0, leaving the link behind
545
- // and now dangling.
546
- //
547
- // The DRY RUN is the discriminating mode, and it is this script's default. Under
548
- // `--apply` both spellings do land on exit 2, but base only gets there having
549
- // already labelled the issue and stranded the branch ref `git worktree add`
550
- // created before dying — the ticket's own framing — and the dry run is where
551
- // they diverge outright (measured, git 2.50.1):
552
- //
553
- // dangling symlink, base exit 0 + a receipt naming the path as claimable
554
- // dangling symlink, fixed exit 2 claim-ticket: … already exists
555
- // real directory, both exit 2 claim-ticket: … already exists
556
- //
557
- // So base does not merely misdiagnose the path, it PREDICTS a claim that cannot
558
- // be made. `--apply` is left unpinned deliberately: it reaches this same guard on
559
- // the same line, so the cheap mode is already the one that fails.
560
- //
561
- // The third row is this test's own, and was unheld until now: #188 did not touch
562
- // the `-e` half, and deleting it outright — leaving `[ -L "$wt" ] && die` — left
563
- // the whole suite green. It is pinned in the dry run for the same reason the
564
- // first row is: `git worktree add` never runs in this mode to refuse the occupied
565
- // path on its own, so a regression there surfaces as exit 0 and a receipt naming
566
- // the path claimable, not as somebody else's later refusal.
567
- //
568
- // Both directions, because a guard that refused everything would satisfy the
569
- // refusal alone. `-L` is true for ANY symlink, so the accept case is what
570
- // establishes it did not widen into one: measured on git 2.50.1, every path `-L`
571
- // newly refuses (dangling link, symlink loop) is a path `git worktree add`
572
- // refuses too, and an unoccupied path must still claim.
573
- test("a dangling symlink and a real directory are both refused, and a free path still claims", () => {
574
- const dir = repo({ [TESTS]: "" });
575
- mkdirSync(join(dir, ".worktrees"), { recursive: true });
576
- symlinkSync("/nonexistent-target", join(dir, ".worktrees", "42-slug"));
577
-
578
- const r = spawnSync("sh", [SCRIPT, "42", "slug", "fix"], { cwd: dir, encoding: "utf8" });
579
- assert.equal(r.status, 2, "a path occupied by a dangling link is a refusal, not a claimable path");
580
- assert.match(r.stderr, /\.worktrees\/42-slug already exists — ticket may already be claimed/,
581
- "this script's own diagnosis, not git's bare `fatal: … already exists` from inside the next mutation");
582
- assert.equal(r.stdout, "", "and no receipt: nothing here is claimable, so there is nothing to predict");
583
-
584
- // The `-e` half of the same line — a REAL directory at $wt, no symlink.
585
- const occupied = repo({ [TESTS]: "" });
586
- mkdirSync(join(occupied, ".worktrees", "42-slug"), { recursive: true });
587
- const d = spawnSync("sh", [SCRIPT, "42", "slug", "fix"], { cwd: occupied, encoding: "utf8" });
588
- assert.equal(d.status, 2, "an occupied directory is a refusal too, in the mode with no downstream net");
589
- assert.match(d.stderr, /\.worktrees\/42-slug already exists — ticket may already be claimed/);
590
- assert.equal(d.stdout, "", "and no receipt: an unguarded real directory is exit 0 and a claim prediction");
591
-
592
- // The input the guard must ACCEPT — same script, same mode, nothing at $wt.
593
- const free = repo({ [TESTS]: "" });
594
- const ok = spawnSync("sh", [SCRIPT, "42", "slug", "fix"], { cwd: free, encoding: "utf8" });
595
- assert.equal(ok.status, 0, "`-L` must not refuse a path that is simply not there");
596
- assert.match(ok.stdout, /"worktree":"\.worktrees\/42-slug"/);
597
- });
598
-
599
- // --- #727: the THIRD answer, which the `-e`/`-L` pair collapsed into the first.
600
- //
601
- // `-e` and `-L` both answer false when the stat could not RUN — an unreadable or
602
- // unsearchable ancestor fails them EACCES, and the shell discards the errno. So
603
- // a path that is occupied read as claimable, and nothing reached stderr saying
604
- // the question had gone unanswered. Measured on the pre-fix script with a real
605
- // directory at $wt and `.worktrees` chmod 000: exit 0 and the receipt below,
606
- // byte-identical to one for a genuinely free path.
607
- //
608
- // The DRY RUN is the sharp mode and is why this is pinned there: it is the
609
- // default, and the mode in which `git worktree add` never runs, so no downstream
610
- // refusal exists to catch the mistake. `--apply` is pinned too, but for a
611
- // different property — ORDERING. There the failure did surface, from inside `git
612
- // worktree add`, but only AFTER `gh issue edit --add-label in-progress` had
613
- // already fired, leaving a labelled issue with no worktree behind it.
614
- //
615
- // The fix routes through `gone()`, the established-absent predicate #725 moved
616
- // into worktree.sh, so this script stops asking a question two of its three
617
- // answers cannot distinguish.
618
- test("an unreadable ancestor refuses rather than predicting a claim", (t) => {
619
- if (process.getuid?.() === 0) return t.skip("root searches every directory");
620
- const dir = repo({ [TESTS]: "" });
621
- mkdirSync(join(dir, ".worktrees", "42-slug"), { recursive: true });
622
- chmodSync(join(dir, ".worktrees"), 0o000);
623
- t.after(() => chmodSync(join(dir, ".worktrees"), 0o755));
624
-
625
- const r = spawnSync("sh", [SCRIPT, "42", "slug", "fix"], { cwd: dir, encoding: "utf8" });
626
- assert.equal(r.status, 2, "a path whose state could not be established is not a claimable path");
627
- assert.equal(r.stdout, "", "above all no receipt — the dry run is where nothing downstream refuses");
628
- // Named as unestablished, not as occupied: the script did not measure the
629
- // path, and saying it exists would assert something it cannot see either.
630
- assert.match(r.stderr, /could not establish whether \.worktrees\/42-slug exists/,
631
- "the refusal has to say the probe could not look, not guess an answer");
632
- });
633
-
634
- test("--apply refuses an unreadable ancestor BEFORE the in-progress label", (t) => {
635
- if (process.getuid?.() === 0) return t.skip("root searches every directory");
636
- const dir = repo({ [TESTS]: "" });
637
- mkdirSync(join(dir, ".worktrees", "42-slug"), { recursive: true });
638
- chmodSync(join(dir, ".worktrees"), 0o000);
639
- t.after(() => chmodSync(join(dir, ".worktrees"), 0o755));
640
-
641
- // A `gh` that RECORDS rather than one that merely succeeds: the assertion is
642
- // about whether the tracker was mutated at all, which a silent stub cannot
643
- // answer. Before the fix this file existed — the label landed, and only then
644
- // did `git worktree add` die on the leading directories it could not create.
645
- // Same `$GH_LOG` form and deliberately spaced prefix as the checksum test
646
- // above (#880): interpolated unquoted, this marker path word-split under a
647
- // spaced `TMPDIR` and recorded nothing, turning the assertion below into a
648
- // pin that passes hardest exactly when the stub is broken.
649
- const bin = tempDir("claim bin-");
650
- assert.match(bin, / /, "fixture: the stub's directory must hold a space, or this pins nothing");
651
- const marker = join(bin, "gh-ran");
652
- writeExecStub(join(bin, "gh"), `#!/bin/sh\necho "$@" >> "$GH_LOG"\nexit 0\n`);
653
- const env = { ...process.env, PATH: `${bin}:${process.env.PATH}`, GH_LOG: marker };
654
-
655
- const r = spawnSync("sh", [SCRIPT, "42", "slug", "fix", "--apply"], {
656
- cwd: dir,
657
- encoding: "utf8",
658
- env,
659
- });
660
-
661
- assert.equal(r.status, 2, r.stdout + r.stderr);
662
- assert.equal(existsSync(marker), false,
663
- "the guard must refuse ahead of every mutation — a labelled issue with no worktree is the half-claim this ordering exists to prevent");
664
-
665
- // The must-ACCEPT half, as above: proves the marker's absence came from the
666
- // guard refusing and not from a stub that could never have written it.
667
- assert.equal(spawnSync("sh", ["-c", "gh issue edit 42"], { cwd: dir, encoding: "utf8", env }).status, 0,
668
- "fixture: the stub must be the gh on PATH");
669
- assert.equal(existsSync(marker), true,
670
- "the stub cannot record a call it did receive, so the assertion above passes vacuously");
671
- });
672
-
673
- // The must-ACCEPT half for `.worktrees` PRESENT — the `.worktrees` ABSENT
674
- // shape is already covered by the `free` case in the dangling-symlink test
675
- // above, byte-identical input and assertions. `gone()` walks UP to the
676
- // nearest existing ancestor and asks whether that one is searchable, so this
677
- // shape exercises a DIFFERENT walk: it stops at `.worktrees` itself rather
678
- // than at the repo root. Measured, and the reason this test exists: handed
679
- // the RELATIVE `$wt` the script carries, the walk terminates at the bare
680
- // `.worktrees` component it cannot strip further, finds it missing, and
681
- // answers "not established absent" — turning every first claim in a repo
682
- // into a refusal. The absolute path is what keeps the accept case an accept.
683
- test("a free path still claims, with the worktrees directory present", () => {
684
- const dir = repo({ [TESTS]: "" });
685
- mkdirSync(join(dir, ".worktrees"), { recursive: true });
686
- const r = spawnSync("sh", [SCRIPT, "42", "slug", "fix"], { cwd: dir, encoding: "utf8" });
687
- assert.equal(r.status, 0, r.stdout + r.stderr);
688
- assert.match(r.stdout, /"worktree":"\.worktrees\/42-slug"/);
689
- });
690
-
691
- // --- #119: the payload's own string fields.
692
- //
693
- // `$issue` is guarded (`case … ''|*[!0-9]*|0?*`), but `<slug>` and `<type>` are
694
- // not, and all four string fields derive from them: `branch` is
695
- // `$type/$issue-$slug`, `worktree` is `.worktrees/$issue-$slug`, `runner` is
696
- // `$worktree/agent-test`. Spliced raw, a quote in either argument emitted a
697
- // payload no parser accepts — at exit 0, and under `--apply` after the worktree
698
- // and the label had already been created.
699
- test("a quote in the slug still emits parseable JSON", () => {
700
- const dir = repo({ [TESTS]: "" });
701
-
702
- const r = spawnSync("sh", [SCRIPT, "42", 'sl"ug', "fix"], { cwd: dir, encoding: "utf8" });
703
-
704
- assert.equal(r.status, 0, r.stdout + r.stderr);
705
- const json = JSON.parse(r.stdout);
706
- assert.equal(json.branch, 'fix/42-sl"ug');
707
- assert.equal(json.worktree, '.worktrees/42-sl"ug');
708
- assert.equal(json.runner, '.worktrees/42-sl"ug/agent-test');
709
- assert.equal(json.issue, 42, "still a JSON number, not a string — the numeric fields are not wrapped");
710
- });
711
-
712
- test("a backslash in the slug is escaped too", () => {
713
- // The worktree path is a filename and carries `\` fine, where a ref could
714
- // not; `branch` and `worktree` are built from the same argument, so one
715
- // argument exercises both the ref-legal and the path-only vector.
716
- const dir = repo({ [TESTS]: "" });
717
-
718
- const r = spawnSync("sh", [SCRIPT, "42", "sl\\ug", "fix"], { cwd: dir, encoding: "utf8" });
719
-
720
- assert.equal(r.status, 0, r.stdout + r.stderr);
721
- assert.equal(JSON.parse(r.stdout).worktree, ".worktrees/42-sl\\ug");
722
- });
723
-
724
- test("an ordinary slug is byte-identical — the escaping accepts what it should", () => {
725
- // The false-positive half: nothing here has anything to escape, so the
726
- // payload must be exactly what this script has always emitted.
727
- const dir = repo({ [TESTS]: "" });
728
-
729
- const r = spawnSync("sh", [SCRIPT, "42", "slug", "fix"], { cwd: dir, encoding: "utf8" });
730
-
731
- assert.equal(r.status, 0, r.stdout + r.stderr);
732
- assert.equal(
733
- r.stdout,
734
- '{"issue":42,"branch":"fix/42-slug","worktree":".worktrees/42-slug","install":"true","ports":{"postgres":16042,"ollama":22042},"runner":".worktrees/42-slug/agent-test","applied":false}\n',
735
- );
736
- });
737
-
738
- // `.` is a POSIX special builtin, so failing to open its operand aborts a
739
- // non-interactive shell before any `||` on the line can run. This script's
740
- // contract is exit 2 for every refusal and 0 otherwise — there is no exit 1 —
741
- // and the guard sits ahead of every mutation, so a missing library refuses
742
- // before a worktree, a label or a runner exists.
743
- test("a missing json.sh is exit 2, before anything is created", () => {
744
- const dir = repo({ [TESTS]: "" });
745
- const lone = tempDir("claim-nolib-");
746
- copyFileSync(SCRIPT, join(lone, "claim-ticket.sh"));
747
- const bin = tempDir("claim-nolib-bin-");
748
- writeExecStub(join(bin, "gh"), "#!/bin/sh\nexit 0\n");
749
-
750
- const r = spawnSync("sh", [join(lone, "claim-ticket.sh"), "42", "slug", "fix", "--apply"], {
751
- cwd: dir, encoding: "utf8", env: { ...process.env, PATH: `${bin}:${process.env.PATH}` },
752
- });
753
-
754
- assert.equal(r.status, 2, "a missing library is a refusal — this script's only failure code");
755
- assert.match(r.stderr, /json\.sh/, "and it names the file rather than blaming the Recipe read");
756
- assert.equal(r.stdout, "", "no payload: this refusal fires before the claim exists, so there is nothing to report");
757
- assert.equal(existsSync(join(dir, ".worktrees", "42-slug")), false,
758
- "and no worktree — the guard fires ahead of every mutation, so this is a clean refusal and not a half-claim");
759
- });
760
-
761
- // The twin of the json.sh test above, for worktree.sh's own `[ -r ]` guard
762
- // (#727's fourth caller of `gone()`) — json.sh present, worktree.sh absent.
763
- test("a missing worktree.sh is exit 2, before anything is created", () => {
764
- const dir = repo({ [TESTS]: "" });
765
- const lone = tempDir("claim-nowt-");
766
- copyFileSync(SCRIPT, join(lone, "claim-ticket.sh"));
767
- copyFileSync(join(dirname(SCRIPT), "json.sh"), join(lone, "json.sh"));
768
- const bin = tempDir("claim-nowt-bin-");
769
- writeExecStub(join(bin, "gh"), "#!/bin/sh\nexit 0\n");
770
-
771
- const r = spawnSync("sh", [join(lone, "claim-ticket.sh"), "42", "slug", "fix", "--apply"], {
772
- cwd: dir, encoding: "utf8", env: { ...process.env, PATH: `${bin}:${process.env.PATH}` },
773
- });
774
-
775
- assert.equal(r.status, 2, "a missing library is a refusal — this script's only failure code");
776
- assert.match(r.stderr, /worktree\.sh/, "and it names the file rather than blaming the Recipe read");
777
- assert.equal(r.stdout, "", "no payload: this refusal fires before the claim exists, so there is nothing to report");
778
- assert.equal(existsSync(join(dir, ".worktrees", "42-slug")), false,
779
- "and no worktree — the guard fires ahead of every mutation, so this is a clean refusal and not a half-claim");
780
- });
781
-
782
- // --- #896: that guard's FATALITY, which none of the three payload cases above
783
- // reaches. Each asserts what a WORKING escaper produces, so nothing here runs
784
- // the `|| die` covering the escaper itself failing — measured, downgrading it
785
- // to a message-preserving warning left this whole file green.
786
- //
787
- // The downgrade does not emit a malformed payload. `issue_branch`, `issue_wt`,
788
- // `issue_install` and `issue_runner` are assigned by one `&&` chain, so the
789
- // first `jstr` that fails short-circuits the rest and leaves those names unset;
790
- // with the guard advisory the receipt `printf` reads one and `set -u` aborts
791
- // the shell instead.
792
- //
793
- // This script is the exception among the four that carry this guard: it uses
794
- // exit 2 for EVERY refusal and defines no exit 1 at all, so the 1 a bash-family
795
- // `sh` aborts with is a code its caller has no reading for — not a refusal it
796
- // can retry, not a claim it can record. The receipt is the claim's only
797
- // machine-readable record, and it is the last thing this script emits.
798
- //
799
- // Which abort it is, though, is the shell's to choose and not this script's,
800
- // and dash's lands on 2 — the very status a firing guard returns, under the
801
- // same message a message-preserving downgrade still prints. This file spawns a
802
- // bare `sh`, and `.github/workflows/ci.yml`'s `check` job runs on
803
- // `ubuntu-latest`, where that name resolves to dash: an exit-code assertion
804
- // therefore pins this guard on a developer's Mac and lets the mutant through
805
- // on the runner that gates the merge, and a wording assertion pins nothing in
806
- // either shell. `issue_wt` is what discriminates instead — both shells name it,
807
- // and it reaches stderr only from that nounset abort.
808
- //
809
- // The shim is selected on CONTENT rather than argv. Only the slug reaches
810
- // `jstr` here, `$install` is the fixture Recipe's `true`, and a `sed` that
811
- // failed unconditionally could not say which stage it broke.
812
-
813
- const REAL_SED = execFileSync("sh", ["-c", "command -v sed"], { encoding: "utf8" }).trim();
814
-
815
- /** A dir holding a `sed` shim with the given body, prepended to PATH. */
816
- function sedShim(body) {
817
- const bin = tempDir("claim-sed-shim-");
818
- writeExecStub(join(bin, "sed"), `#!/bin/sh\n${body}\n`);
819
- return `${bin}:${process.env.PATH}`;
820
- }
821
-
822
- // `:a` appears in `jstr`'s rule list and in no other `sed` this script reaches.
823
- const FAILS_ON_SLUG = `case " $* " in
824
- *:a*)
825
- in=$(cat)
826
- case "$in" in *esc-boom*) echo "sed: outage" >&2; exit 1 ;; esac
827
- printf '%s\\n' "$in" | exec ${REAL_SED} "$@" ;;
828
- esac
829
- exec ${REAL_SED} "$@"`;
830
-
831
- test("an escaper that cannot run is exit 2 — this script has no other failure code", () => {
832
- const dir = repo({ [TESTS]: "" });
833
-
834
- const r = spawnSync("sh", [SCRIPT, "42", "esc-boom", "fix"], {
835
- cwd: dir, encoding: "utf8", env: { ...process.env, PATH: sedShim(FAILS_ON_SLUG) },
836
- });
837
-
838
- // Whether this fixture measured the guard at all is settled before its
839
- // verdict is read: an assertion that fails masks every one after it, and
840
- // "the shim broke something else" and "the guard is not fatal" are not
841
- // interchangeable diagnoses.
842
- assert.match(r.stderr, /sed: outage/, "the escaper ran and failed, which is the failure under test");
843
- assert.match(r.stderr, /DRY RUN — nothing created/,
844
- "the whole plan was settled — this is the guard after it, not an earlier one the shim happened to break");
845
-
846
- assert.equal(r.status, 2,
847
- "a receipt that could not be escaped is a refusal, and 2 is the only refusal this script defines. Exit 1 is a code its caller has no reading for.");
848
- assert.equal(r.stdout, "", "no receipt: an unescaped payload is not a record of a claim");
849
- assert.doesNotMatch(r.stderr, /issue_wt/,
850
- "the guard must stop the script, not warn and leave the receipt `printf` reading names the `&&` chain never assigned. The NAME, never the wording: bash says `issue_wt: unbound variable` at exit 1 and dash `issue_wt: parameter not set` at exit 2, so the exit-2 assertion above passes on the mutant under the shell CI actually runs, and the guard's own message survives a downgrade that preserves it.");
851
- });
852
-
853
- test("a shadowed `sed` that works claims normally — the guard refuses only a real outage", () => {
854
- // The false-positive half, and the control the case above needs: shadowing
855
- // `sed` on PATH is not by itself fatal to this script. Same fixture and the
856
- // same shadowed name, a passthrough body — so the exit 2 up there is the
857
- // escaper failing, not the shim's mere presence. Without this, that case
858
- // could be measuring a PATH it broke wholesale and still read green.
859
- const dir = repo({ [TESTS]: "" });
860
-
861
- const r = spawnSync("sh", [SCRIPT, "42", "esc-boom", "fix"], {
862
- cwd: dir, encoding: "utf8", env: { ...process.env, PATH: sedShim(`exec ${REAL_SED} "$@"`) },
863
- });
864
-
865
- assert.equal(r.status, 0, r.stdout + r.stderr);
866
- assert.equal(
867
- r.stdout,
868
- '{"issue":42,"branch":"fix/42-esc-boom","worktree":".worktrees/42-esc-boom","install":"true","ports":{"postgres":16042,"ollama":22042},"runner":".worktrees/42-esc-boom/agent-test","applied":false}\n',
869
- "byte-identical to the receipt this script emits with no shim in the way",
870
- );
871
- });
872
-
873
- // #1141, delegated: this script reads no JSON itself — derive-testcmd.sh does,
874
- // and it is where the interpreter first has to resolve. Its refusal travels
875
- // back through the `2>&1` capture, and what arrives must still name the
876
- // interpreter rather than the Recipe cache.
877
- //
878
- // The fixture is the repo's PATH-shadow convention: a directory of symlinks
879
- // to the REAL binaries the script reaches for, resolved from the ambient PATH
880
- // up front, and `PATH` REPLACED by it rather than prepended — prepending
881
- // leaves the interpreter reachable behind the shim and the fixture asks
882
- // nothing. Resolving up front is the same reason `node` itself is linked from
883
- // `process.execPath` rather than by name: a shim dir assembled by asking the
884
- // child to look names up is the very PATH question the fixture controls.
885
- // Nothing here touches the ambient PATH, which siblings on this machine
886
- // inherit.
887
- //
888
- // The list is closed over what a dry run reaches, transitive calls included:
889
- // `sed`, `tr` and `python3` are json.sh's `jstr` (the UTF-8 repair stage
890
- // added for #613 is a new call site of its own) and the Test entrypoint's
891
- // quoting, `cksum` is the runner's hash, and `mktemp`, `cat` and `rm` are
892
- // derive-testcmd.sh's stderr capture.
893
- const SHIMMED = ["sh", "git", "sed", "tr", "python3", "dirname", "cksum", "grep", "mktemp", "cat", "rm"];
894
- function shimPath({ node }) {
895
- const bin = tempDir("claim-path-");
896
- for (const name of SHIMMED) {
897
- const real = execFileSync("sh", ["-c", `command -v ${name}`], { encoding: "utf8" }).trim();
898
- symlinkSync(real, join(bin, name));
899
- }
900
- if (node) symlinkSync(process.execPath, join(bin, "node"));
901
- return bin;
902
- }
903
- const onShimmedPath = (dir, bin) =>
904
- spawnSync("sh", [SCRIPT, "42", "slug", "fix"], { cwd: dir, encoding: "utf8", env: { ...process.env, PATH: bin } });
905
-
906
- test("an unresolvable interpreter in the delegated Recipe read carries that cause through", () => {
907
- const r = onShimmedPath(repo({ [TESTS]: "" }), shimPath({ node: false }));
908
- assert.equal(r.status, 2, `the wrapping refusal keeps this script's only failure code\n${r.stderr}`);
909
- assert.match(r.stderr, /claim-ticket: derive-testcmd: node is unusable/, "the delegate's own voice, naming the interpreter");
910
- assert.doesNotMatch(r.stderr, /Recipe cache at .* is unusable/, "an interpreter that never ran establishes nothing about the cache");
911
- });
912
-
913
- // The false-positive control for the shim dir itself: the refusal above rests
914
- // on a stripped PATH, and a PATH too thin for the script to work at all would
915
- // produce it for a reason that is not the interpreter. This drives a claim to
916
- // completion under exactly that PATH.
917
- test("a resolvable interpreter on the shimmed PATH still reads both Recipe commands", () => {
918
- const r = onShimmedPath(repo({ [TESTS]: "" }), shimPath({ node: true }));
919
- assert.equal(r.status, 0, r.stdout + r.stderr);
920
- const out = r.stdout + r.stderr;
921
- assert.match(out, /Install step → true/);
922
- assert.match(out, /test entrypoint → true/);
923
- });
924
-
925
- // ---------------------------------------------------------------------------
926
- // #804 — the argument slot. `claim-ticket.sh` was the last of the four scripts
927
- // taking `--apply` to carry #250's demotion: the arity check was a lower bound
928
- // alone and nothing looked at the flag's VALUE.
929
- //
930
- // Both halves are pinned, because a guard's false-positive class is not its
931
- // false-negative class and the refusal tests alone are satisfied by a guard
932
- // that refuses everything. The accept test is the one that would have caught
933
- // the regression this shape has produced before — a guard hoisted or widened
934
- // until the default dry run stopped exiting 0.
935
-
936
- // A PATH whose `gh` records that it ran. The direction that actually mutates
937
- // is only pinned by proving the tracker was never reached: an argument refused
938
- // AFTER `gh issue edit` still exits 2 and still looks like a refusal from the
939
- // outside, which is exactly how the trailing-argument case read as one.
940
- function ghSpy() {
941
- // Spaced prefix and the `$GH_LOG` form, one convention with the two stubs
942
- // above (#880). This spy was already safe — it interpolated through
943
- // `JSON.stringify`, which emits its own quotes — but a second spelling of
944
- // the same job is what lets the unquoted one look normal, and the
945
- // `ran() === true` assertion in the accept test below is what holds the env
946
- // threading here honest.
947
- const bin = tempDir("claim ghspy-");
948
- assert.match(bin, / /, "fixture: the stub's directory must hold a space, or this pins nothing");
949
- const marker = join(bin, "ran");
950
- writeExecStub(join(bin, "gh"), `#!/bin/sh\necho "$@" >> "$GH_LOG"\nexit 0\n`);
951
- return { env: { ...process.env, PATH: `${bin}:${process.env.PATH}`, GH_LOG: marker }, ran: () => existsSync(marker) };
952
- }
953
-
954
- test("#804: a mistyped --apply is refused, never demoted to a dry run", () => {
955
- // The signature that made this invisible: `42 slug fix` and `42 slug fix
956
- // --aply` were byte-identical on stdout AND stderr at exit 0. Nothing
957
- // anywhere said the flag was not understood, so a caller reading `applied`
958
- // saw an honest `false` for a run it believed had applied.
959
- //
960
- // `--apply=true` and `-n` are here because the `=` spelling and the short
961
- // form are what a caller reaches for when the exact-match rule is not
962
- // obvious, and both matched nothing before. `apply` unprefixed covers the
963
- // dropped dashes, which no `--`-prefix rule would catch, and `extra` covers
964
- // a bare positional landing in the slot — within arity, so the bound below
965
- // never sees it and only a value check refuses it.
966
- for (const arg of ["--aply", "--apply=true", "--APPLY", "-n", "apply", "--", "extra"]) {
967
- const r = spawnSync("sh", [SCRIPT, "42", "slug", "fix", arg], { cwd: tmpdir(), encoding: "utf8" });
968
- assert.equal(r.status, 2, `${arg} must be refused: ${r.stdout}${r.stderr}`);
969
- assert.match(r.stderr, /unknown argument/, `${arg} must say the flag was not understood`);
970
- assert.equal(r.stdout.trim(), "", `${arg} must not emit a receipt: ${r.stdout}`);
971
- }
972
-
973
- // The discriminator, asserted rather than assumed. At this cwd the FLAGLESS
974
- // run also exits 2 with empty stdout — it dies on the repository check — so
975
- // "exit 2 and no receipt" is a signature the unguarded script already
976
- // produced and pins nothing on its own. What has to be true is that the two
977
- // runs are now distinguishable, which is the whole of #250.
978
- const flagless = spawnSync("sh", [SCRIPT, "42", "slug", "fix"], { cwd: tmpdir(), encoding: "utf8" });
979
- assert.doesNotMatch(flagless.stderr, /unknown argument/, "the slot rule fired on a run that passed no flag at all");
980
- });
981
-
982
- test("#804: a trailing argument is refused on both entry paths, before the tracker is touched", () => {
983
- // The worse direction. `--apply extra --whatever` dropped args five and six
984
- // without a word and entered APPLY mode, reaching `gh issue edit 42
985
- // --add-label in-progress`; the exit 2 it ended on came from that gh call
986
- // failing, not from any argument refusal. So the assertion that matters is
987
- // that `gh` never ran — not the exit code, which was already 2.
988
- //
989
- // The last row carries no flag in the trailing slot: the arity bound has to
990
- // hold on its own, without the value rule underneath it happening to catch
991
- // the same argv. (A bare `extra` in slot FOUR is within arity and is the
992
- // value rule's to refuse — it is asserted in the mistyped-flag test above,
993
- // not here, so that each row measures the guard it names.)
994
- for (const args of [
995
- ["42", "slug", "fix", "--apply", "extra"],
996
- ["42", "slug", "fix", "--apply", "extra", "--whatever"],
997
- ["42", "slug", "fix", "--apply", ""],
998
- ["42", "slug", "fix", "", "extra"],
999
- ]) {
1000
- const spy = ghSpy();
1001
- const r = spawnSync("sh", [SCRIPT, ...args], { cwd: tmpdir(), encoding: "utf8", env: spy.env });
1002
- assert.equal(r.status, 2, `${args.join(" ")} must be refused: ${r.stdout}${r.stderr}`);
1003
- assert.match(r.stderr, /usage: claim-ticket\.sh <issue> <slug> <type>/, `${args.join(" ")} must refuse on arity`);
1004
- assert.equal(r.stdout.trim(), "", `${args.join(" ")} must not emit a receipt: ${r.stdout}`);
1005
- assert.equal(spy.ran(), false, `${args.join(" ")} reached the tracker before refusing`);
1006
- }
1007
- });
1008
-
1009
- test("#804: the slot rule accepts every argv it must — the false-positive half", () => {
1010
- // A rule that refused everything satisfies both tests above. This is the
1011
- // shape that regressed once already: a guard widened until it refused the
1012
- // legitimate flags it was never meant to see. The default dry run leads,
1013
- // because it is the mode with no flag in it — the one a suite whose call
1014
- // sites all pass `--apply` cannot see at all.
1015
- const dry = spawnSync("sh", [SCRIPT, "42", "slug", "fix"], { cwd: repo({ [TESTS]: "" }), encoding: "utf8" });
1016
- assert.equal(dry.status, 0, `the default dry run must still exit 0\n${dry.stdout}${dry.stderr}`);
1017
- assert.match(dry.stdout, /"applied":false/, "and must still emit its receipt");
1018
-
1019
- // `--apply` still reaches apply mode. Asserted as "the tracker WAS reached",
1020
- // never as "it exited 0": a flag swallowed by a guard that then fell through
1021
- // to the dry-run branch also exits 0, which is the fail-open the slot rule
1022
- // exists to close.
1023
- const spy = ghSpy();
1024
- const applied = spawnSync("sh", [SCRIPT, "42", "slug", "fix", "--apply"], { cwd: repo({ [TESTS]: "" }), encoding: "utf8", env: spy.env });
1025
- assert.equal(applied.status, 0, `${applied.stdout}${applied.stderr}`);
1026
- assert.match(applied.stdout, /"applied":true/, "--apply must still apply");
1027
- assert.equal(spy.ran(), true, "--apply must still reach the tracker");
1028
-
1029
- // An explicitly empty fourth argument. `${4:-}` cannot tell it from an
1030
- // absent one, so it is a dry run — the same reading both already-guarded
1031
- // siblings give it, and a decision on the record rather than a hole.
1032
- const empty = spawnSync("sh", [SCRIPT, "42", "slug", "fix", ""], { cwd: repo({ [TESTS]: "" }), encoding: "utf8" });
1033
- assert.equal(empty.status, 0, `${empty.stdout}${empty.stderr}`);
1034
- assert.match(empty.stdout, /"applied":false/, "an empty flag slot reads as absent, as it does in the siblings");
1035
-
1036
- // The neighbours the slot rule is not allowed to cost. It reads argument
1037
- // four and no other position, so a <slug> or <type> that merely LOOKS like a
1038
- // flag is none of its business — and neither is refused anywhere else.
1039
- const odd = spawnSync("sh", [SCRIPT, "42", "-weird--slug", "fix"], { cwd: repo({ [TESTS]: "" }), encoding: "utf8" });
1040
- assert.equal(odd.status, 0, `a flag-shaped slug must still claim\n${odd.stdout}${odd.stderr}`);
1041
- assert.match(odd.stdout, /"branch":"fix\/42--weird--slug"/, "and must reach the branch name unaltered");
1042
- });
1043
-
1044
- // --- #1020: the ambient git variables, one fixture each.
1045
- //
1046
- // Not one case setting both. PR #1015 measured the cost of that shortcut on
1047
- // release-ticket.sh: a fixture overriding only one of the pair leaves the
1048
- // other half of `unset GIT_DIR GIT_WORK_TREE` unpinned and green. Here the
1049
- // two halves do not even defeat the same guard — GIT_DIR walks past the
1050
- // branch-collision check before anything is created, GIT_WORK_TREE walks past
1051
- // the tree-mutation check after the install has already run — so one
1052
- // detector could not see both.
1053
-
1054
- test("an ambient GIT_DIR does not look for the claim's branch in another repository (#1020)", () => {
1055
- // No git call in this script carries a `-C` until after `worktree add`, so
1056
- // an ambient GIT_DIR moves the whole claim elsewhere. The detector is the
1057
- // branch-collision guard, because that is the one whose WRONG answer is
1058
- // the double-claim: two members dispatched onto one ticket, each believing
1059
- // it holds it.
1060
- const dir = repo({ [TESTS]: "" });
1061
- execFileSync("git", ["branch", "fix/42-slug", "HEAD"], { cwd: dir, stdio: "pipe", env: FIXTURE_ENV });
1062
- const other = repo({ [TESTS]: "" });
1063
-
1064
- // The fixture's own positive control: without it, a case where the branch
1065
- // was never created passes while pinning nothing, and a case where BOTH
1066
- // repos carry the branch passes for the wrong reason.
1067
- assert.equal(
1068
- spawnSync("git", ["rev-parse", "--verify", "--quiet", "refs/heads/fix/42-slug"], { cwd: other, env: FIXTURE_ENV }).status,
1069
- 1,
1070
- "fixture: the other repository must NOT carry the branch, or looking in the wrong place would give the right answer",
1071
- );
1072
-
1073
- const r = spawnSync("sh", [SCRIPT, "42", "slug", "fix"], {
1074
- cwd: dir, encoding: "utf8", env: { ...FIXTURE_ENV, GIT_DIR: join(other, ".git") },
1075
- });
1076
-
1077
- assert.equal(r.status, 2,
1078
- `an ambient GIT_DIR must not make an already-claimed ticket look free; got\n${r.stdout}${r.stderr}`);
1079
- assert.match(r.stderr, /branch fix\/42-slug already exists/,
1080
- "and it must refuse for the real reason rather than tripping over something else");
1081
- assert.equal(r.stdout, "", "a refusal emits no receipt — a receipt here is a claim the caller would act on");
1082
- });
1083
-
1084
- test("an ambient GIT_WORK_TREE does not make a mutated tree look clean (#1020)", () => {
1085
- // GIT_WORK_TREE outranks `-C`, so `git -C "$wt" status --porcelain -uall`
1086
- // reads the ambient tree against $wt's index. Pointed at the repo root —
1087
- // where the tracked file is untouched, and the Recipe cache and the
1088
- // worktrees are ignored — it answers EMPTY at rc 0 while the fresh
1089
- // worktree's copy has been rewritten.
1090
- //
1091
- // The install really does mutate: this guard runs immediately after the
1092
- // install and there is no seam between them to write a file into. The
1093
- // mutating Install step IS the hazard the guard exists for — npm@11 pruning
1094
- // cross-platform optional deps out of a lockfile is the header's own example.
1095
- const files = { [TESTS]: "", ".gitignore": ".fleet/\n.worktrees/\n" };
1096
- const recipe = { install: "echo MUTATED >> run-tests.sh" };
1097
- const bin = tempDir("claim-bin-");
1098
- writeExecStub(join(bin, "gh"), "#!/bin/sh\nexit 0\n");
1099
- const env = (extra) => ({ ...FIXTURE_ENV, PATH: `${bin}:${process.env.PATH}`, ...extra });
1100
-
1101
- // The control, and it is not optional: it proves the install really mutates
1102
- // and the guard really fires on it. Without it an install that silently did
1103
- // nothing would leave the poisoned run exiting 0 for an innocent reason, and
1104
- // the assertion below would be measuring the absence of a hazard rather than
1105
- // its containment.
1106
- const control = spawnSync("sh", [SCRIPT, "42", "slug", "fix", "--apply"], {
1107
- cwd: repo(files, {}, recipe), encoding: "utf8", env: env(),
1108
- });
1109
- assert.equal(control.status, 2, `fixture: the unpoisoned run must refuse\n${control.stdout}${control.stderr}`);
1110
- assert.match(control.stderr, /changed the tree/);
1111
-
1112
- const dir = repo(files, {}, recipe);
1113
- const r = spawnSync("sh", [SCRIPT, "42", "slug", "fix", "--apply"], {
1114
- cwd: dir, encoding: "utf8", env: env({ GIT_WORK_TREE: dir }),
1115
- });
1116
-
1117
- assert.equal(r.status, 2,
1118
- `an ambient GIT_WORK_TREE must not make the tree guard answer about the repo root; got\n${r.stdout}${r.stderr}`);
1119
- assert.match(r.stderr, /changed the tree in \.worktrees\/42-slug \(first: +M run-tests\.sh\)/,
1120
- "and for the real reason — this guard is the only thing between a wrong Install step and a worktree corrupted for everyone");
1121
- assert.doesNotMatch(r.stdout, /"applied":true/, "a claim must not be handed out over an unverified tree");
1122
- });