@feigi/fleet-ctl 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (245) hide show
  1. package/.claude-plugin/plugin.json +9 -0
  2. package/LICENSE +202 -0
  3. package/NOTICE +18 -0
  4. package/agents/fleet-finisher.agent.md +7 -0
  5. package/agents/fleet-implementer-alt.agent.md +210 -0
  6. package/agents/fleet-implementer.agent.md +210 -0
  7. package/agents/fleet-merge-bot.agent.md +7 -0
  8. package/agents/fleet-review-comments.agent.md +40 -0
  9. package/agents/fleet-review-correctness.agent.md +43 -0
  10. package/agents/fleet-review-runner.agent.md +73 -0
  11. package/agents/fleet-review-silent-failure.agent.md +49 -0
  12. package/agents/fleet-review-simplify.agent.md +46 -0
  13. package/agents/fleet-review-snapshot.agent.md +24 -0
  14. package/agents/fleet-review-tests.agent.md +46 -0
  15. package/agents/fleet-review-types.agent.md +41 -0
  16. package/agents/fleet-review-verifier.agent.md +29 -0
  17. package/commands/review-and-fix.md +98 -0
  18. package/commands/run-merge-bot.md +357 -0
  19. package/commands/run-team-help.md +17 -0
  20. package/package.json +23 -0
  21. package/scripts/agent-test-dir-prose.test.mjs +59 -0
  22. package/scripts/agent-test.test.mjs +1636 -0
  23. package/scripts/ambient-git-vars-mjs-prose.test.mjs +354 -0
  24. package/scripts/ambient-git-vars-prose.test.mjs +192 -0
  25. package/scripts/ancestry-check-position-prose.test.mjs +175 -0
  26. package/scripts/apply-ruleset.test.mjs +420 -0
  27. package/scripts/arg-header-probes-prose.test.mjs +393 -0
  28. package/scripts/arg.mjs +600 -0
  29. package/scripts/arg.test.mjs +1354 -0
  30. package/scripts/board-cli.test.mjs +694 -0
  31. package/scripts/board-identity.test.mjs +429 -0
  32. package/scripts/board-liveness-banner.test.mjs +119 -0
  33. package/scripts/board-prev-shape.test.mjs +229 -0
  34. package/scripts/board-tryparse-null.test.mjs +119 -0
  35. package/scripts/board.html +471 -0
  36. package/scripts/board.mjs +2037 -0
  37. package/scripts/board.test.mjs +3086 -0
  38. package/scripts/brief-after-cut-prose.test.mjs +53 -0
  39. package/scripts/candidates-exit3-prose.test.mjs +302 -0
  40. package/scripts/candidates.mjs +674 -0
  41. package/scripts/candidates.test.mjs +2101 -0
  42. package/scripts/caveat-unification-prose.test.mjs +98 -0
  43. package/scripts/ci-comment-rot-prose.test.mjs +2025 -0
  44. package/scripts/ci-completes-premise-prose.test.mjs +257 -0
  45. package/scripts/ci-state-prose.test.mjs +113 -0
  46. package/scripts/ci-state.mjs +761 -0
  47. package/scripts/ci-state.test.mjs +1640 -0
  48. package/scripts/ci-vacuous-green.test.mjs +368 -0
  49. package/scripts/citation-sweep-prose.test.mjs +1572 -0
  50. package/scripts/claim-lifecycle.test.mjs +185 -0
  51. package/scripts/claim-ticket.sh +417 -0
  52. package/scripts/claim-ticket.test.mjs +1121 -0
  53. package/scripts/closing-keyword-prose.test.mjs +193 -0
  54. package/scripts/cockpit-launch-idempotent-prose.test.mjs +165 -0
  55. package/scripts/cockpit-tick-nonblocking.test.mjs +210 -0
  56. package/scripts/compute-board.mjs +579 -0
  57. package/scripts/compute-board.test.mjs +1060 -0
  58. package/scripts/compute-spend.mjs +329 -0
  59. package/scripts/compute-spend.test.mjs +451 -0
  60. package/scripts/cross-repo-citation-prose.test.mjs +156 -0
  61. package/scripts/cwd-isolation-pins.mjs +83 -0
  62. package/scripts/dedupe-guard-prose.test.mjs +243 -0
  63. package/scripts/derive-testcmd.sh +239 -0
  64. package/scripts/derive-testcmd.test.mjs +380 -0
  65. package/scripts/diff-stats.mjs +231 -0
  66. package/scripts/diff-stats.test.mjs +408 -0
  67. package/scripts/dispatch-block-golden-prose.test.mjs +1024 -0
  68. package/scripts/dispatch-block-pins-prose.test.mjs +615 -0
  69. package/scripts/drop-merged-label.sh +173 -0
  70. package/scripts/drop-merged-label.test.mjs +480 -0
  71. package/scripts/expected-jobs-prose.test.mjs +80 -0
  72. package/scripts/filing-bar-seat-prose.test.mjs +242 -0
  73. package/scripts/filing-claim-bar-prose.test.mjs +166 -0
  74. package/scripts/finisher-dispatch-premise-prose.test.mjs +312 -0
  75. package/scripts/finisher-halt-prose.test.mjs +106 -0
  76. package/scripts/finisher-name-prose.test.mjs +134 -0
  77. package/scripts/finisher-own-tree-prose.test.mjs +285 -0
  78. package/scripts/finisher-pin-race-prose.test.mjs +339 -0
  79. package/scripts/fix-applier-correction-rules-prose.test.mjs +201 -0
  80. package/scripts/fix-applier-result-file-prose.test.mjs +100 -0
  81. package/scripts/fleet-bootstrap +155 -0
  82. package/scripts/fleet-heartbeat.mjs +272 -0
  83. package/scripts/fleet-heartbeat.test.mjs +450 -0
  84. package/scripts/fleet-provenance +207 -0
  85. package/scripts/fleet-provenance.test.mjs +64 -0
  86. package/scripts/fleet-run +148 -0
  87. package/scripts/fleet-run.test.mjs +90 -0
  88. package/scripts/fleet-state.mjs +400 -0
  89. package/scripts/fleet-state.test.mjs +373 -0
  90. package/scripts/fleet-tick-prose.test.mjs +299 -0
  91. package/scripts/fleet-tick.mjs +832 -0
  92. package/scripts/fleet-tick.test.mjs +1332 -0
  93. package/scripts/force-with-lease-reason-prose.test.mjs +184 -0
  94. package/scripts/frontmatter-allowlist.json +64 -0
  95. package/scripts/frontmatter-check.mjs +256 -0
  96. package/scripts/frontmatter-check.test.mjs +436 -0
  97. package/scripts/git-env.mjs +124 -0
  98. package/scripts/git-env.test.mjs +149 -0
  99. package/scripts/git-status-untracked-mode-sweep.test.mjs +216 -0
  100. package/scripts/gone-walk.test.mjs +213 -0
  101. package/scripts/gp-sep-invariant-prose.test.mjs +188 -0
  102. package/scripts/head-moved-after-label-prose.test.mjs +233 -0
  103. package/scripts/immutable-body-claim-prose.test.mjs +413 -0
  104. package/scripts/implementer-model-tier.test.mjs +643 -0
  105. package/scripts/inflight-citation-prose.test.mjs +144 -0
  106. package/scripts/inflight-exit2-prose.test.mjs +81 -0
  107. package/scripts/inflight-probe-guard-prose.test.mjs +109 -0
  108. package/scripts/inflight-scan-scope-prose.test.mjs +57 -0
  109. package/scripts/inflight.sh +1106 -0
  110. package/scripts/inflight.test.mjs +2984 -0
  111. package/scripts/injection-control-prose.test.mjs +404 -0
  112. package/scripts/install-root-audit.test.mjs +464 -0
  113. package/scripts/instrument-check-prose.test.mjs +235 -0
  114. package/scripts/instruments.sh +394 -0
  115. package/scripts/instruments.test.mjs +787 -0
  116. package/scripts/issue-tracker-prose.test.mjs +71 -0
  117. package/scripts/json.sh +248 -0
  118. package/scripts/json.test.mjs +364 -0
  119. package/scripts/ledger-dispatch.test.mjs +516 -0
  120. package/scripts/ledger-grammar.mjs +85 -0
  121. package/scripts/ledger-grammar.test.mjs +142 -0
  122. package/scripts/ledger-read-require-file.test.mjs +197 -0
  123. package/scripts/ledger.mjs +1753 -0
  124. package/scripts/ledger.test.mjs +3554 -0
  125. package/scripts/line-distance-prose.test.mjs +245 -0
  126. package/scripts/liveness-rationale-prose.test.mjs +83 -0
  127. package/scripts/locale-pin-prose.test.mjs +189 -0
  128. package/scripts/member-outcome-prose.test.mjs +167 -0
  129. package/scripts/member-outcomes-header.test.mjs +148 -0
  130. package/scripts/member-outcomes-prose.test.mjs +63 -0
  131. package/scripts/member-outcomes.mjs +231 -0
  132. package/scripts/member-outcomes.test.mjs +507 -0
  133. package/scripts/member-prompt-prose.test.mjs +161 -0
  134. package/scripts/member-record.mjs +605 -0
  135. package/scripts/member-record.test.mjs +516 -0
  136. package/scripts/member-write-guard.mjs +279 -0
  137. package/scripts/member-write-guard.test.mjs +211 -0
  138. package/scripts/merge-commit-probe-prose.test.mjs +122 -0
  139. package/scripts/merge-gate.mjs +262 -0
  140. package/scripts/merge-gate.test.mjs +613 -0
  141. package/scripts/muted-git-guard-sweep.test.mjs +493 -0
  142. package/scripts/net-ssh-precedence-prose.test.mjs +91 -0
  143. package/scripts/net-watchdog-prose.test.mjs +79 -0
  144. package/scripts/net.sh +409 -0
  145. package/scripts/net.test.mjs +211 -0
  146. package/scripts/no-ci-gate-prose.test.mjs +108 -0
  147. package/scripts/no-undo-audit.sh +1134 -0
  148. package/scripts/no-undo-audit.test.mjs +3965 -0
  149. package/scripts/no-wave.test.mjs +138 -0
  150. package/scripts/node-floor-sweep.test.mjs +611 -0
  151. package/scripts/pointer-target-prose.test.mjs +186 -0
  152. package/scripts/pr-overlap.mjs +417 -0
  153. package/scripts/pr-overlap.test.mjs +695 -0
  154. package/scripts/printf-die-sweep.test.mjs +384 -0
  155. package/scripts/prompt-renderer.mjs +39 -0
  156. package/scripts/prompt-renderer.test.mjs +68 -0
  157. package/scripts/prose-pin.mjs +808 -0
  158. package/scripts/prose-pin.test.mjs +1034 -0
  159. package/scripts/prove-merge.sh +228 -0
  160. package/scripts/prove-merge.test.mjs +995 -0
  161. package/scripts/pull-table-relabel-prose.test.mjs +60 -0
  162. package/scripts/quiet-payload-prose.test.mjs +222 -0
  163. package/scripts/reap.sh +1768 -0
  164. package/scripts/reap.test.mjs +4395 -0
  165. package/scripts/reaping-prose.test.mjs +159 -0
  166. package/scripts/refuter-scratch-prose.test.mjs +135 -0
  167. package/scripts/release-label-required-prose.test.mjs +71 -0
  168. package/scripts/release-label-split-prose.test.mjs +169 -0
  169. package/scripts/release-ticket.sh +1716 -0
  170. package/scripts/release-ticket.test.mjs +5106 -0
  171. package/scripts/renovate-release-contract.test.mjs +379 -0
  172. package/scripts/repo-root.mjs +544 -0
  173. package/scripts/repo-root.test.mjs +873 -0
  174. package/scripts/reporter-output-rule-prose.test.mjs +208 -0
  175. package/scripts/review-agent-report-only.test.mjs +49 -0
  176. package/scripts/review-core-cwd-isolation.test.mjs +374 -0
  177. package/scripts/review-core-reads.test.mjs +478 -0
  178. package/scripts/review-core-refuter-scratch.test.mjs +148 -0
  179. package/scripts/review-core-snapshot-path.test.mjs +777 -0
  180. package/scripts/review-core-specialist-scratch.test.mjs +189 -0
  181. package/scripts/review-core-testcmd.test.mjs +288 -0
  182. package/scripts/review-core-unrun.test.mjs +530 -0
  183. package/scripts/review-core-unverified-discriminant.test.mjs +208 -0
  184. package/scripts/review-core.mjs +1028 -0
  185. package/scripts/review-eval-resolver-path.test.mjs +55 -0
  186. package/scripts/review-eval.mjs +197 -0
  187. package/scripts/review-host-fixture.mjs +73 -0
  188. package/scripts/review-in-run-retry.test.mjs +187 -0
  189. package/scripts/review-no-vendor-dispatch.test.mjs +45 -0
  190. package/scripts/review-path-default.test.mjs +796 -0
  191. package/scripts/review-runner-spawns.test.mjs +93 -0
  192. package/scripts/review-runner.test.mjs +155 -0
  193. package/scripts/review-tier-audit.test.mjs +140 -0
  194. package/scripts/run-artifact-pr-prose.test.mjs +341 -0
  195. package/scripts/run-merge-bot-prose.test.mjs +1391 -0
  196. package/scripts/scratch-partition-invariant-prose.test.mjs +74 -0
  197. package/scripts/scripts-path-citation-sweep.test.mjs +73 -0
  198. package/scripts/section-between-guard-prose.test.mjs +378 -0
  199. package/scripts/select-dimensions.test.mjs +674 -0
  200. package/scripts/shared-refusal.test.mjs +388 -0
  201. package/scripts/shell-traps-prose.test.mjs +507 -0
  202. package/scripts/shortlist.mjs +355 -0
  203. package/scripts/shortlist.test.mjs +366 -0
  204. package/scripts/slow-transport.mjs +72 -0
  205. package/scripts/snapshot-repo.test.mjs +772 -0
  206. package/scripts/snapshot-runner-audience-prose.test.mjs +129 -0
  207. package/scripts/spend-view.test.mjs +312 -0
  208. package/scripts/staleness-qualifier-prose.test.mjs +251 -0
  209. package/scripts/staleness-verdict-prose.test.mjs +142 -0
  210. package/scripts/staleness.mjs +414 -0
  211. package/scripts/staleness.test.mjs +893 -0
  212. package/scripts/stalled-member-liveness-prose.test.mjs +219 -0
  213. package/scripts/stash-prohibition-prose.test.mjs +313 -0
  214. package/scripts/strip-comments.mjs +44 -0
  215. package/scripts/tier-check.mjs +468 -0
  216. package/scripts/tier-check.test.mjs +726 -0
  217. package/scripts/tier-guard-gate-prose.test.mjs +216 -0
  218. package/scripts/tier-outcomes-header.test.mjs +586 -0
  219. package/scripts/tier-outcomes.mjs +441 -0
  220. package/scripts/tier-outcomes.test.mjs +342 -0
  221. package/scripts/tier-roles.mjs +250 -0
  222. package/scripts/tier-roles.test.mjs +316 -0
  223. package/scripts/tracker-block-copy-prose.test.mjs +288 -0
  224. package/scripts/triage-labels-gloss-prose.test.mjs +97 -0
  225. package/scripts/unattended-git-sweep.test.mjs +142 -0
  226. package/scripts/verify-sha-exit2-prose.test.mjs +269 -0
  227. package/scripts/verify-sha.sh +117 -0
  228. package/scripts/verify-sha.test.mjs +693 -0
  229. package/scripts/watcher-degraded-block.test.mjs +372 -0
  230. package/scripts/watcher-key-prose.test.mjs +106 -0
  231. package/scripts/within-run-pair-prose.test.mjs +208 -0
  232. package/scripts/worktree-audit.sh +482 -0
  233. package/scripts/worktree-audit.test.mjs +1407 -0
  234. package/scripts/worktree-head-prose.test.mjs +276 -0
  235. package/scripts/worktree-listing-sweep.test.mjs +217 -0
  236. package/scripts/worktree-model-prose.test.mjs +216 -0
  237. package/scripts/worktree.sh +470 -0
  238. package/skills/next-ticket/SKILL.md +138 -0
  239. package/skills/run-team/SKILL.md +3603 -0
  240. package/skills/run-team/references/ci-and-staleness.md +75 -0
  241. package/skills/run-team/references/correction-tickets.md +37 -0
  242. package/skills/run-team/references/isolation.md +23 -0
  243. package/skills/run-team/references/member-lifecycle.md +73 -0
  244. package/skills/run-team/references/reaping.md +73 -0
  245. package/skills/sizing-a-ticket/SKILL.md +39 -0
@@ -0,0 +1,185 @@
1
+ // The claim → push → merge → reap lifecycle, driven end to end against a real
2
+ // bare origin. #760 is a bug about a branch's UPSTREAM, and an upstream is only
3
+ // observable through what the other scripts do with it — so pinning the config
4
+ // values claim-ticket.sh writes proves nothing about whether reap.sh can see
5
+ // the branch or release-ticket.sh can delete it. A first cut of #760 did pin
6
+ // exactly those values, went green, and had in fact made a freshly claimed
7
+ // branch read `[gone]` immediately: `reap.sh --apply` deleted the worktree and
8
+ // the branch before any work was done in them. These tests are the consequence
9
+ // coverage that gap needed.
10
+ //
11
+ // Zero deps: `node --test plugin/scripts/claim-lifecycle.test.mjs`.
12
+
13
+ import { test } from "node:test";
14
+ import assert from "node:assert/strict";
15
+ import { execFileSync, spawnSync } from "node:child_process";
16
+ import { mkdtempSync, realpathSync, rmSync, writeFileSync } from "node:fs";
17
+ import { tmpdir } from "node:os";
18
+ import { join } from "node:path";
19
+ import { fileURLToPath } from "node:url";
20
+
21
+ const CLAIM = fileURLToPath(new URL("./claim-ticket.sh", import.meta.url));
22
+ const REAP = fileURLToPath(new URL("./reap.sh", import.meta.url));
23
+ const RELEASE = fileURLToPath(new URL("./release-ticket.sh", import.meta.url));
24
+
25
+ // Cut the developer's ~/.gitconfig out of the fixture: a local `push.default`
26
+ // or `branch.autoSetupMerge` decides the very behaviour under test here.
27
+ const BASE_ENV = {
28
+ ...process.env,
29
+ BASE_REF: undefined,
30
+ GIT_DIR: undefined,
31
+ GIT_WORK_TREE: undefined,
32
+ GIT_AUTHOR_NAME: "t",
33
+ GIT_AUTHOR_EMAIL: "t@example.com",
34
+ GIT_COMMITTER_NAME: "t",
35
+ GIT_COMMITTER_EMAIL: "t@example.com",
36
+ GIT_CONFIG_GLOBAL: "/dev/null",
37
+ GIT_CONFIG_SYSTEM: "/dev/null",
38
+ };
39
+
40
+ /**
41
+ * Bare origin + a clone standing in for the fleet's checkout, plus a `gh` stub
42
+ * on PATH. `gh issue view` answers `in-progress` because release-ticket.sh
43
+ * reads the label before it deletes anything.
44
+ */
45
+ function fixture(t) {
46
+ // realpathSync: macOS resolves /var through /private, and git canonicalises
47
+ // the paths it reports, so a raw mkdtemp path never string-matches them.
48
+ const root = realpathSync(mkdtempSync(join(tmpdir(), "lifecycle-")));
49
+ t.after(() => rmSync(root, { recursive: true, force: true }));
50
+ const origin = join(root, "origin.git");
51
+ const w = join(root, "w");
52
+ const bin = join(root, "bin");
53
+ execFileSync("git", ["-c", "init.defaultBranch=main", "init", "-q", "--bare", origin], { env: BASE_ENV });
54
+ execFileSync("git", ["clone", "-q", origin, w], { env: BASE_ENV });
55
+ execFileSync("mkdir", ["-p", bin]);
56
+ writeFileSync(join(bin, "gh"), '#!/bin/sh\ncase "$*" in *"issue view"*) echo in-progress;; esac\nexit 0\n', { mode: 0o755 });
57
+ const env = { ...BASE_ENV, PATH: `${bin}:${process.env.PATH}` };
58
+ const git = (cwd, ...a) => execFileSync("git", a, { cwd, env, encoding: "utf8" }).trim();
59
+ writeFileSync(join(w, "README.md"), "fixture\n");
60
+ git(w, "add", "-A");
61
+ git(w, "commit", "-q", "-m", "root");
62
+ git(w, "branch", "-M", "main");
63
+ git(w, "push", "-q", "-u", "origin", "main");
64
+ // claim-ticket.sh refuses a repo with no Recipe cache (ADR 0015). The
65
+ // smallest proven one: nothing to install, nothing to fail. Untracked, in
66
+ // the checkout's own `.fleet/`, which is where derive-testcmd.sh reads it.
67
+ execFileSync("mkdir", ["-p", join(w, ".fleet")]);
68
+ writeFileSync(join(w, ".fleet", "recipe.json"), JSON.stringify({
69
+ install: "true", test: "true", derivedAt: git(w, "rev-parse", "HEAD"), installClean: true, testCount: 1,
70
+ }));
71
+ return { root, origin, w, env, git };
72
+ }
73
+
74
+ const sh = (script, cwd, env, ...args) =>
75
+ spawnSync("sh", [script, ...args], { cwd, env, encoding: "utf8" });
76
+
77
+ /** What reap.sh's sweep selects on: `%(upstream:track)` for one branch. */
78
+ const track = (git, w, branch) =>
79
+ git(w, "for-each-ref", "--format=%(upstream:track)", `refs/heads/${branch}`);
80
+
81
+ /** Advance origin/main past the claim's base, without merging it locally. */
82
+ function staleLocalMain({ origin, root, env, git }) {
83
+ const other = join(root, "other");
84
+ execFileSync("git", ["clone", "-q", origin, other], { env });
85
+ git(other, "commit", "-q", "--allow-empty", "-m", "later");
86
+ git(other, "push", "-q", "origin", "HEAD:main");
87
+ }
88
+
89
+ test("a freshly claimed branch is not [gone], and reap.sh leaves it alone", (t) => {
90
+ const f = fixture(t);
91
+ const claim = sh(CLAIM, f.w, f.env, "42", "slug", "fix", "--apply");
92
+ assert.equal(claim.status, 0, claim.stdout + claim.stderr);
93
+
94
+ const wt = join(f.w, ".worktrees", "42-slug");
95
+ // #760's own measurement: an upstream of origin/main makes `@{u}` RESOLVE,
96
+ // so every "did my push land?" check answers about main instead. It has to
97
+ // fail loudly, and the failure has to be the missing-upstream one — a claim
98
+ // whose upstream merely names an absent ref resolves here just fine.
99
+ const u = spawnSync("git", ["-C", wt, "rev-parse", "--abbrev-ref", "@{u}"], { env: f.env, encoding: "utf8" });
100
+ assert.notEqual(u.status, 0, u.stdout);
101
+ assert.match(u.stderr, /no upstream configured/);
102
+
103
+ // An upstream naming a remote ref that has never existed reads `[gone]` here
104
+ // — indistinguishable, to reap.sh, from a merged branch whose remote was
105
+ // deleted. No upstream at all reads empty.
106
+ assert.equal(track(f.git, f.w, "fix/42-slug"), "");
107
+
108
+ const reaped = sh(REAP, f.w, f.env, "--apply");
109
+ assert.equal(reaped.status, 0, reaped.stdout + reaped.stderr);
110
+ assert.deepEqual(JSON.parse(reaped.stdout).reaped, []);
111
+ // Positive control on the same run: the branch and its worktree survived.
112
+ assert.equal(f.git(f.w, "rev-parse", "--verify", "refs/heads/fix/42-slug").length, 40);
113
+ assert.match(f.git(f.w, "worktree", "list"), /42-slug/);
114
+ });
115
+
116
+ test("after push -u, merge and prune, reap.sh does see the branch as [gone]", (t) => {
117
+ const f = fixture(t);
118
+ assert.equal(sh(CLAIM, f.w, f.env, "42", "slug", "fix", "--apply").status, 0);
119
+ const wt = join(f.w, ".worktrees", "42-slug");
120
+
121
+ // The documented first push (skills/next-ticket/SKILL.md step 7) is what
122
+ // establishes the real upstream — nothing before it should have.
123
+ f.git(wt, "commit", "-q", "--allow-empty", "-m", "work");
124
+ f.git(wt, "push", "-q", "-u", "origin", "HEAD");
125
+ assert.equal(f.git(f.w, "config", "--get", "branch.fix/42-slug.merge"), "refs/heads/fix/42-slug");
126
+
127
+ // Merge and delete the remote branch, the way GitHub does.
128
+ f.git(wt, "push", "-q", "origin", "HEAD:main");
129
+ f.git(wt, "push", "-q", "origin", ":refs/heads/fix/42-slug");
130
+ f.git(f.w, "fetch", "-q", "--prune", "origin");
131
+ assert.equal(track(f.git, f.w, "fix/42-slug"), "[gone]");
132
+
133
+ const reaped = sh(REAP, f.w, f.env, "--apply");
134
+ assert.equal(reaped.status, 0, reaped.stdout + reaped.stderr);
135
+ assert.deepEqual(JSON.parse(reaped.stdout).reaped, ["fix/42-slug"]);
136
+ assert.equal(
137
+ spawnSync("git", ["-C", f.w, "rev-parse", "--verify", "refs/heads/fix/42-slug"], { env: f.env }).status,
138
+ 128,
139
+ );
140
+ });
141
+
142
+ test("release-ticket.sh --apply releases an untouched claim while local main is stale", (t) => {
143
+ const f = fixture(t);
144
+ // ORDER IS THE WHOLE TEST. origin/main advances and is fetched BEFORE the
145
+ // claim, so the branch is cut from a commit local main has never reached.
146
+ // Advance origin AFTER the claim instead and the branch's base still equals
147
+ // local HEAD, `git branch -d` finds it trivially merged, and the test goes
148
+ // green against the very bug it exists to catch — measured, that ordering
149
+ // let a `-d` mutant through.
150
+ staleLocalMain(f);
151
+ f.git(f.w, "fetch", "-q", "origin");
152
+ assert.equal(sh(CLAIM, f.w, f.env, "42", "slug", "fix", "--apply").status, 0);
153
+ // The claim's base is not an ancestor of local HEAD, so `git branch -d`
154
+ // — which falls back to comparing against HEAD once the branch has no
155
+ // resolvable upstream — refuses, half-releasing the claim: worktree deleted,
156
+ // branch stranded, in-progress still on the issue.
157
+ assert.notEqual(f.git(f.w, "rev-parse", "main"), f.git(f.w, "rev-parse", "origin/main"));
158
+ assert.equal(f.git(f.w, "rev-parse", "refs/heads/fix/42-slug"), f.git(f.w, "rev-parse", "origin/main"));
159
+
160
+ const rel = sh(RELEASE, f.w, f.env, "42", "slug", "fix", "--apply");
161
+ assert.equal(rel.status, 0, rel.stdout + rel.stderr);
162
+ const receipt = JSON.parse(rel.stdout.trim().split("\n").pop());
163
+ assert.equal(receipt.released, true);
164
+ assert.deepEqual(receipt.blockers, []);
165
+ assert.equal(
166
+ spawnSync("git", ["-C", f.w, "rev-parse", "--verify", "refs/heads/fix/42-slug"], { env: f.env }).status,
167
+ 128,
168
+ );
169
+ });
170
+
171
+ test("release-ticket.sh still refuses a claim that carries work", (t) => {
172
+ // The control the previous test needs: `-D` is authorized by the `ahead` and
173
+ // `git cherry` guards, so those guards have to still block. Without this a
174
+ // fix that deletes unconditionally passes the test above.
175
+ const f = fixture(t);
176
+ assert.equal(sh(CLAIM, f.w, f.env, "42", "slug", "fix", "--apply").status, 0);
177
+ f.git(join(f.w, ".worktrees", "42-slug"), "commit", "-q", "--allow-empty", "-m", "work");
178
+
179
+ const rel = sh(RELEASE, f.w, f.env, "42", "slug", "fix", "--apply");
180
+ assert.equal(rel.status, 1, rel.stdout + rel.stderr);
181
+ const receipt = JSON.parse(rel.stdout.trim().split("\n").pop());
182
+ assert.equal(receipt.released, false);
183
+ assert.match(receipt.blockers.join(" "), /commit\(s\) ahead/);
184
+ assert.equal(f.git(f.w, "rev-parse", "--verify", "refs/heads/fix/42-slug").length, 40);
185
+ });
@@ -0,0 +1,417 @@
1
+ #!/bin/sh
2
+ # Claim a ticket: label it, create its worktree and branch, run the Recipe's
3
+ # Install step, and materialise the isolation runner.
4
+ #
5
+ # Dry-run by default; --apply mutates the tracker and the filesystem.
6
+ #
7
+ # The claim is branch `<type>/<issue>-<slug>` and worktree
8
+ # `.worktrees/<issue>-<slug>`. The fleet always calls it as
9
+ # `claim-ticket.sh <N> impl-<N> implementer` (run-team SKILL.md, **Claiming**),
10
+ # giving `implementer/<N>-impl-<N>` and `.worktrees/<N>-impl-<N>`, and releases
11
+ # with the same pair through release-ticket.sh. <slug> and <type> are
12
+ # free text — neither derived nor validated here — so a caller outside the fleet
13
+ # can pass its own shape, e.g. next-ticket's human-facing `fix/<N>-<slug>`.
14
+ #
15
+ # The Install step and the Test entrypoint are READ from the repository's
16
+ # proven Recipe cache through derive-testcmd.sh — never inferred, never
17
+ # defaulted (ADR 0015): this script keeps no table of technologies. An Install
18
+ # step that modifies the tree in a throwaway worktree corrupts it for everyone
19
+ # (npm@11 pruning cross-platform @esbuild optional deps out of a lockfile broke
20
+ # CI and the Docker build), so a dirty tree after it is a refusal.
21
+ set -eu
22
+
23
+ # Directly below `set -eu`, not below a locale pin: this script has none, and
24
+ # locale-pin-prose.test.mjs deliberately leaves it off the PINNED list (whether
25
+ # its generated runner needs one is #600's question). The five siblings that DO
26
+ # carry a pin put this line under it instead, because that file's PROLOGUE
27
+ # regex admits only comments, blanks and `set -[eux]+` above the pin. Here
28
+ # there is no pin to sit under, so the only constraint left is the real one:
29
+ # above the first git call. #1020
30
+ #
31
+ # Both halves are measured on this script, and each defeats a different guard.
32
+ #
33
+ # GIT_DIR: not one git call here carries a `-C` until after `worktree add`, so
34
+ # an ambient one moves the whole claim to another repository. Measured,
35
+ # standing in clone A whose `fix/777-ccc` already exists, with `GIT_DIR`
36
+ # naming clone B's `.git`: the `rev-parse --verify refs/heads/$branch`
37
+ # collision guard looks in B, finds nothing, and the run reports the ticket
38
+ # claimable at rc 0 with a full receipt — the double-claim that guard is the
39
+ # whole defence against. `--apply` then builds the worktree and the branch
40
+ # over there.
41
+ #
42
+ # GIT_WORK_TREE: it outranks `-C`, so the tree-mutation check below — `git -C
43
+ # "$wt" status --porcelain -uall`, the one thing standing between a wrong
44
+ # Install step and a worktree corrupted for everyone — reads the AMBIENT tree
45
+ # against $wt's index. Measured (against the lockfile-only form this check
46
+ # had before ADR 0015) with an install that really does rewrite a tracked file
47
+ # in the fresh worktree: reported clean, rc 0, claim handed out, where the
48
+ # unpoisoned run refuses at exit 2.
49
+ unset GIT_DIR GIT_WORK_TREE
50
+
51
+ NAME=claim-ticket
52
+ die() { printf '%s: %s\n' "$NAME" "$1" >&2; exit 2; }
53
+
54
+ # The escaping helpers (#119). json.sh's header holds the sourcing contract and
55
+ # the measurements behind it. This script uses exit 2 for every refusal and has
56
+ # no exit 1, so a bare 1 out of it is a code its caller has no reading for. The
57
+ # guard sits ahead of every mutation, so a missing library refuses before a
58
+ # worktree, a branch, a label or a runner exists.
59
+ json_lib="$(dirname "$0")/json.sh"
60
+ [ -r "$json_lib" ] || die "cannot read $json_lib — refusing to claim without the JSON escaping helpers"
61
+ # shellcheck source-path=SCRIPTDIR
62
+ # shellcheck source=json.sh
63
+ . "$json_lib" || die "$json_lib failed to load"
64
+
65
+ # The worktree readers (#551, #725). worktree.sh's header holds the sourcing
66
+ # contract; the `[ -r ]` ahead of the `.` is load-bearing there, not decoration.
67
+ # Sourced for `gone()` alone — this script reads no listing.
68
+ wt_lib="$(dirname "$0")/worktree.sh"
69
+ [ -r "$wt_lib" ] || die "cannot read $wt_lib — refusing to claim without the worktree predicates"
70
+ # shellcheck source-path=SCRIPTDIR
71
+ # shellcheck source=worktree.sh
72
+ . "$wt_lib" || die "$wt_lib failed to load"
73
+
74
+ [ $# -ge 3 ] && [ $# -le 4 ] || die "usage: claim-ticket.sh <issue> <slug> <type> [--apply]"
75
+ issue=$1
76
+ slug=$2
77
+ type=$3
78
+ # Exact match, and nothing else tolerated in the slot — the #250 demotion, in
79
+ # the file that still carried it. Measured on the unguarded script: `5 slug fix`
80
+ # and `5 slug fix --aply` were byte-identical on stdout AND stderr at exit 0,
81
+ # so nothing anywhere said the flag was not understood; and the arity check
82
+ # above was a lower bound alone, so `5 slug fix --apply extra --whatever`
83
+ # dropped both extras and reached `gh issue edit --add-label in-progress`,
84
+ # mutating the tracker on an argv the script never agreed to.
85
+ case "${4:-}" in
86
+ ''|--apply) ;;
87
+ *) die "unknown argument '$4' — the only option is --apply";;
88
+ esac
89
+ apply=false
90
+ [ "${4:-}" = "--apply" ] && apply=true
91
+
92
+ case "$issue" in ''|*[!0-9]*|0?*) die "issue must be a number, got '$issue'";; esac
93
+
94
+ branch="$type/$issue-$slug"
95
+ wt=".worktrees/$issue-$slug"
96
+ runner="$wt/agent-test"
97
+
98
+ # A repository probe, and deliberately no more. The git-dir identity invariant
99
+ # release-ticket.sh and no-undo-audit.sh check (the git dir answering for $wt
100
+ # must belong to $wt) is not owed here (#421): this script never asks git
101
+ # anything through a $wt it did not just create. The pair below establishes the
102
+ # path is absent, `git worktree add` then writes the .git and its admin dir
103
+ # itself, and the one later `git -C "$wt"` call (the tree-mutation check) runs
104
+ # against that fresh linkage, so there is no prior one to verify. The ambient
105
+ # GIT_DIR / GIT_WORK_TREE version of "which repo answered" is closed by the
106
+ # unset above.
107
+ git rev-parse --git-dir >/dev/null 2>&1 || die "not inside a git repository"
108
+ # `-e` alone STATS, so it follows the link and reads a DANGLING symlink as an
109
+ # absent path, while `git worktree add` refuses it on lstat semantics (`fatal:
110
+ # '…' already exists`) — leaving the refusal to fire only inside the NEXT
111
+ # mutation, `git worktree add` itself, stranding the branch ref it just created,
112
+ # with the in-progress label already on the issue, and in the DEFAULT dry run
113
+ # leaving it not to fire at all: exit 0 and a receipt naming the path claimable.
114
+ # Same predicate release-ticket.sh's `occupied()` already carries, for residue
115
+ # this fleet leaves itself — a symlink pointing AT the registered directory
116
+ # survives the `git worktree remove` that deletes its target. Measured on git
117
+ # 2.50.1: every path `-L` adds here (dangling link, symlink loop) is one `git
118
+ # worktree add` refuses too, so it cannot refuse a claim that would have worked.
119
+ #
120
+ # But the pair answers only TWO of the three states, and silently folds the
121
+ # third into "absent": both `-e` and `-L` are false when the stat could not RUN.
122
+ # An unreadable or unsearchable ancestor fails them EACCES and the shell
123
+ # discards the errno, so an occupied path read as claimable with nothing on
124
+ # stderr — in the DEFAULT dry run, where `git worktree add` never runs to refuse
125
+ # it downstream, that is exit 0 and a receipt byte-identical to a free path's.
126
+ # `gone()` is the predicate that keeps established-absent and could-not-measure
127
+ # apart (worktree.sh; #725 made it one definition rather than three), so the
128
+ # refusal below is the existing helper, not a new EACCES-aware stat. #727
129
+ #
130
+ # ABSOLUTE, and this is the term to leave alone. `gone()` walks up to the
131
+ # nearest existing ancestor and asks whether IT is searchable; git hands every
132
+ # other caller an absolute path, and `$wt` is the first relative one. Measured:
133
+ # handed `.worktrees/42-slug`, the walk reaches the bare `.worktrees` component,
134
+ # cannot strip further, finds it missing on a repo that has never had a
135
+ # worktree, and answers "not established absent" — turning every first claim in
136
+ # a repo into a refusal. `$PWD` is what the walk needs to reach the repo root.
137
+ # Measured on /bin/sh, dash and bash: each sets PWD from getcwd() at startup, so
138
+ # a stale inherited value is corrected and an unset one does not trip `set -u` —
139
+ # the two ways this term could have asked about the wrong path.
140
+ #
141
+ # Kept as a PAIR rather than `gone` alone, the way gone()'s own header prescribes
142
+ # for a caller that needs the two refusals apart. The messages are not
143
+ # interchangeable: an occupied path is diagnosed, an unmeasured one is admitted
144
+ # to. And the wording of the first stays hedged — a dangling link here has two
145
+ # provenances with opposite claim states (rc-0 residue, and a release halted
146
+ # mid-flight with the branch and the label still live), and only the hedge is
147
+ # true of both. #728
148
+ if [ -e "$wt" ] || [ -L "$wt" ]; then
149
+ die "$wt already exists — ticket may already be claimed"
150
+ elif ! gone "$PWD/$wt"; then
151
+ die "could not establish whether $wt exists — an ancestor is unreadable, not searchable, or not a directory; refusing to claim a path nothing measured"
152
+ fi
153
+ git rev-parse --verify --quiet "refs/heads/$branch" >/dev/null && die "branch $branch already exists"
154
+
155
+ # The worktree is built FROM origin/main, so it must resolve before anything is
156
+ # claimed. The lockfile probe that stood here before ADR 0015 read origin/main
157
+ # and refused on its absence as a side effect; reading the Recipe cache reads
158
+ # no ref at all, so the precondition is asked for directly — unasked, the
159
+ # DEFAULT dry run would report a ticket claimable that `git worktree add` then
160
+ # refuses under --apply, after the in-progress label is already on the issue.
161
+ # No `--quiet`: git's own reason for not resolving it reaches the terminal
162
+ # beside this refusal (muted-git-guard-sweep.test.mjs).
163
+ git rev-parse --verify "origin/main^{commit}" >/dev/null \
164
+ || die "origin/main does not resolve to a commit — nothing to build the worktree from"
165
+
166
+ # The Recipe: its Install step and Test entrypoint, READ from the repository's
167
+ # Recipe cache by derive-testcmd.sh — the one reader, reused rather than
168
+ # reimplemented, so the claim and the review snapshot cannot disagree about
169
+ # the cache's shape or what makes it usable (#142). Nothing here infers either
170
+ # command: an absent, unproven or unrunnable cache is derive-testcmd.sh's own
171
+ # refusal, passed through in its own words, which name the step that derives
172
+ # the Recipe (ADR 0015).
173
+ #
174
+ # `2>&1` so the reason travels: the child refuses on its STDERR and `$(...)`
175
+ # captures stdout only, so without the merge `die` fires with an empty
176
+ # argument and prints the bare line `claim-ticket: `. It is safe on the success
177
+ # path only because derive-testcmd.sh writes nothing to stderr when it
178
+ # succeeds, even under an interpreter made deliberately chatty (#1175) — a
179
+ # cross-file invariant, so derive-testcmd.test.mjs pins it where the behaviour
180
+ # lives. The sibling is found beside this script (`dirname -- "$0"`), never on
181
+ # PATH, for the reason json.sh and worktree.sh are.
182
+ script_dir=$(dirname -- "$0")
183
+ # Held in one variable, not repeated as a literal at each die below — the two
184
+ # copies could drift apart, and derive-testcmd.sh already keeps its own
185
+ # version the same way (`$derive`).
186
+ rederive="the Recipe cache is invalid; run the Recipe derivation step (run-team phase 0, before the first claim — ADR 0015) to re-derive it"
187
+ install=$("$script_dir/derive-testcmd.sh" . install 2>&1) || die "$install"
188
+ echo " Install step → $install" >&2
189
+ testcmd=$("$script_dir/derive-testcmd.sh" . test 2>&1) || die "$testcmd"
190
+ echo " test entrypoint → $testcmd" >&2
191
+
192
+ # The Test entrypoint is a shell COMMAND, not an argv: the runner hands it to
193
+ # `sh -c`, so a compound one (`cd sub && make test`) keeps its meaning, with the
194
+ # runner's own arguments appended as "$@". It reaches the runner's source
195
+ # single-quoted — each embedded `'` closed, escaped and reopened — so nothing
196
+ # in it expands at write time. `LC_ALL=C` so sed reads the command as bytes
197
+ # (#582). Derived here, ahead of every mutation, so a failure refuses before
198
+ # the label.
199
+ quoted=$(printf '%s\n' "$testcmd" | LC_ALL=C sed "s/'/'\\\\''/g") \
200
+ || die "could not quote the Test entrypoint for the runner"
201
+
202
+ pg=$((16000 + issue))
203
+ ollama=$((22000 + issue))
204
+ echo " ports derive from the issue number: postgres=$pg ollama=$ollama" >&2
205
+
206
+ # Ports above are only ever exported into a FRESH runner this script writes
207
+ # (below): a tracked repo-local agent-test is left byte-identical (#1262), so
208
+ # it never carries them. Starts true and flips to false in that branch, so
209
+ # the receipt can report which happened instead of asserting exports that
210
+ # never reached the runner.
211
+ runner_ports_applied=true
212
+
213
+ # The stamp the runner carries. The runner is written once at claim time and
214
+ # never rewritten (#124), so an old worktree can be sitting on a runner a later
215
+ # template fix never reached. This stamp does not detect or fix that — nothing
216
+ # reads it, nothing refuses on a mismatch — it only makes staleness legible:
217
+ # diff the stamp against a fresh `cksum` of this script to see if they match.
218
+ # It is a checksum of the WHOLE script, not of the emitted template, so it
219
+ # over-reports: any edit to this file moves it — a reworded die message, a
220
+ # comment — while the runner it produces stays byte-identical. It under-reports
221
+ # too, so the error is not one-way and a match does not mean fresh: the command
222
+ # the runner execs is the Recipe cache's Test entrypoint, which this checksum
223
+ # does not cover — two claims under one stamp exec different commands once the
224
+ # Recipe is re-derived between them. So what the stamp covers is
225
+ # this file's own bytes: a mismatch means "re-materialize to be sure", and a
226
+ # match means only that this script has not changed.
227
+ #
228
+ # One command, not `cksum | cut`: the convention inflight.sh states in its own
229
+ # comments — a pipeline reports only its last stage's status, so `set -eu`
230
+ # reads `cut`'s success and a `cksum` that could not read its operand yields a
231
+ # stamp line with nothing after the colon, at exit 0 and a claim reported as
232
+ # applied. Measured on the pipeline form. The trailing fields come off by
233
+ # parameter expansion rather than another process, since `cksum` prints the
234
+ # checksum, the byte count and the path.
235
+ #
236
+ # Reading the status is only half of it: this derivation sits with the others
237
+ # that run before the apply branch, not beside the heredoc that consumes it.
238
+ # Guarded where that heredoc is, the refusal fires after the issue has been
239
+ # labelled and after `git worktree add`, so it exchanges a blank stamp for a
240
+ # half-claimed ticket — label applied, branch ref and worktree on disk, and a
241
+ # claim reporting failure — needing a manual release-ticket. Measured on that
242
+ # form too. Derived here it has nothing to clean up, the same property the
243
+ # json.sh guard is placed for. Nothing blocks the move: the value depends on
244
+ # this script's own path and on nothing the branch establishes.
245
+ tmpl_stamp=$(cksum "$0") || die "could not checksum $0 — refusing to claim without a runner template stamp"
246
+ tmpl_stamp=${tmpl_stamp%% *}
247
+ # Status is not content, so that is two parts and not one — the same shape the
248
+ # tree-mutation guard uses, status then value. A `cksum` that exits 0 printing
249
+ # nothing leaves this empty, and `set -u` catches an unset variable, never an
250
+ # empty one, so without this the blank stamp line ships at exit 0 with the
251
+ # ticket claimed: the rc-0 half of the same hole the pipeline form opened.
252
+ [ -n "$tmpl_stamp" ] || die "cksum $0 produced no checksum — refusing to claim without a runner template stamp"
253
+
254
+ if [ "$apply" = false ]; then
255
+ echo "$NAME: DRY RUN — nothing created. Pass --apply to act." >&2
256
+ echo " would: gh issue edit $issue --add-label in-progress" >&2
257
+ printf ' would: git worktree add --no-track %s -b %s origin/main\n' "$wt" "$branch" >&2
258
+ printf ' would: (cd %s && %s)\n' "$wt" "$install" >&2
259
+ printf ' would: write %s and add it to .git/info/exclude — skipped if %s is\n' "$runner" "$runner" >&2
260
+ printf ' already present, a repo-local runner checked out with the worktree\n' >&2
261
+ else
262
+ echo "\$ gh issue edit $issue --add-label in-progress" >&2
263
+ gh issue edit "$issue" --add-label in-progress >/dev/null || die "could not label issue $issue"
264
+
265
+ # `--no-track`: without it, `-b … origin/main` leaves $branch tracking
266
+ # origin/main, so `@{u}` RESOLVES — to main. Every "did my push land?" check a
267
+ # member might reach for (`git rev-parse HEAD @{u}`, `git status -sb`) then
268
+ # answers a question about main and reads healthy no matter what the push did.
269
+ # That is #760's sharp half, measured on two live worktrees. Under
270
+ # push.default=upstream the same config is a live hazard rather than a
271
+ # misleading guard: a bare `git push` would push $branch's commits onto
272
+ # origin/main. (Under the default push.default=simple it refuses loudly, exit
273
+ # 128 — so the ticket's "silently no-ops" framing does not reproduce; the
274
+ # observed silent no-op came from a push through an explicit URL, which sets
275
+ # no upstream either way.) With no upstream, all of those fail loudly instead.
276
+ #
277
+ # No upstream at all, rather than one pre-seeded at refs/heads/$branch: git
278
+ # computes `%(upstream:track)` against the REMOTE ref, and an upstream naming
279
+ # a remote ref that has never existed reads as `[gone]`, not as "no upstream"
280
+ # — measured, a freshly claimed branch was reaped by `reap.sh --apply` before
281
+ # any work was done in it. The real upstream arrives with the documented first
282
+ # push, `git push --force-with-lease -u origin HEAD` (skills/next-ticket/SKILL.md
283
+ # step 7; its no-lease fallback carries `-u` too), and only then can be `[gone]`.
284
+ #
285
+ # `--no-track` is also why no branch delete uses `-d` (#760): `-d` measures an
286
+ # upstream-less branch against local HEAD, refusing a pristine claim behind
287
+ # origin/main. release-ticket.sh's is `git update-ref -d` on a tip read once.
288
+ printf '$ git worktree add --no-track %s -b %s origin/main\n' "$wt" "$branch" >&2
289
+ git worktree add --no-track "$wt" -b "$branch" origin/main >/dev/null || die "worktree add failed"
290
+
291
+ printf '$ (cd %s && %s)\n' "$wt" "$install" >&2
292
+ # Through `sh -c`, as the runner runs the Test entrypoint: the Install step
293
+ # is a shell command out of the cache, not an argv to split. 126 and 127 are
294
+ # the shell's own "cannot execute" and "not found" — the Recipe failed to
295
+ # RUN, which is what invalidates a cache (ADR 0015) — so they are named as
296
+ # that; any other non-zero is the install itself failing, reported as such.
297
+ irc=0
298
+ (cd "$wt" && sh -c "$install" >/dev/null 2>&1) || irc=$?
299
+ case $irc in
300
+ 0) ;;
301
+ 126|127) die "the Install step '$install' did not run in $wt (exit $irc: not executable or not found) — $rederive" ;;
302
+ *) die "install failed in $wt (exit $irc)" ;;
303
+ esac
304
+
305
+ # The Install step must leave the tree exactly as `worktree add` checked it
306
+ # out: the `installClean` proof the cache records, re-asserted on every
307
+ # claim, over the WHOLE tree rather than a list of lockfile names — fleet-ctl
308
+ # keeps no table of which files an install may touch (ADR 0015). Non-empty
309
+ # means the Recipe no longer holds, and the worktree is now corrupt for
310
+ # everyone. Check git's exit status too: a failed status prints nothing,
311
+ # which is byte-identical to "clean" and would let this guard pass without
312
+ # having verified anything.
313
+ #
314
+ # That same guard does not cover the rc-0 form of the identical hole: delete
315
+ # $wt's .git outright (or empty it into a directory, or leave a dangling
316
+ # symlink) between the `worktree add` above and here, and `git -C` does not
317
+ # fail — it walks UP to the enclosing repo and answers about THAT at rc 0,
318
+ # which this check would read as an untouched tree it never actually
319
+ # looked at. `-f`: `git worktree add` writes $wt's `.git` as a regular file,
320
+ # so no healthy run trips this; reference shape and same reason as
321
+ # release-ticket.sh's own linkage guard (#128). `-x "$wt"` for the reason
322
+ # reap.sh and worktree-audit.sh give their own copy: `-f` is equally false
323
+ # for a `.git` that is absent and for one this process may not stat, and an
324
+ # unsearchable $wt must not be reported as an absence nothing established.
325
+ # Left ungated it ate the case before the `git -C` below could reach it —
326
+ # measured: `.git` still sitting there while the run blamed its deletion.
327
+ # Gated, git answers with its own denial through the elif, which is what
328
+ # release-ticket.test.mjs already pins as the wording to prefer over one this
329
+ # script invents.
330
+ if [ -x "$wt" ] && [ ! -f "$wt/.git" ]; then
331
+ die "$wt has no .git file — cannot verify the Install step left the tree clean"
332
+ # No `2>&1` here, unlike the two captures above: those capture a refusal
333
+ # REASON, this captures DATA that is then compared. The comment above
334
+ # release-ticket.sh's own dirty-worktree status capture already states it
335
+ # — folded-in stderr would be counted as a change. A git
336
+ # that exits 0 still writes to stderr for a malformed `.gitattributes` line
337
+ # or a chatty `core.fsmonitor`, and merged that chatter became the whole of
338
+ # $dirty and refused a tree it had just verified as clean — after the
339
+ # label, the branch and the worktree were already created. git's denial
340
+ # reaches this terminal on its own, which is the "its own denial" the
341
+ # paragraph above means; the die names the failure, not the reason.
342
+ #
343
+ # `-uall`: #730 (see reap.sh's branch sweep for the full explanation) —
344
+ # the untracked mode is CONFIG: an install that CREATES a file the tree does
345
+ # not track or ignore (a lockfile it was never given) dirties the tree as
346
+ # surely as one that rewrites a tracked file, and under
347
+ # `status.showUntrackedFiles=no` it is invisible at rc 0 unless pinned. It
348
+ # is also the form the deriving agent's own proof reads, so a Recipe proven
349
+ # clean there reads clean here.
350
+ elif ! dirty=$(git -C "$wt" status --porcelain -uall); then
351
+ die "could not verify the tree state in $wt after the Install step"
352
+ elif [ -n "$dirty" ]; then
353
+ nl='
354
+ '
355
+ die "the Install step '$install' changed the tree in $wt (first: ${dirty%%"$nl"*}) — $rederive"
356
+ fi
357
+ echo " tree clean after the Install step" >&2
358
+
359
+ # A runner is already there: the repo tracks its own `agent-test`, and `git
360
+ # worktree add` checked it out with everything else — a repo-local runner,
361
+ # the home ADR 0015 gives any convenience beyond "exec the Test entrypoint"
362
+ # (this repository's own test-argument shim lives in one). Writing
363
+ # over it is what must not happen: the file is TRACKED, so the write leaves a
364
+ # modified tracked path, and `reap.sh` calls `git worktree remove` without
365
+ # `--force` (its own comment: "refuses on modified and untracked files").
366
+ # Every release of every claim would then strand on a file this script wrote
367
+ # itself (#1262). On this path $runner is always the fresh worktree's own
368
+ # `agent-test`, so existence IS trackedness: nothing else could have put a
369
+ # file there between `worktree add` and here.
370
+ if [ -e "$runner" ]; then
371
+ runner_ports_applied=false
372
+ printf ' %s already present — a repo-local runner, checked out with the worktree; left as is (its own script owns isolation — the ports below were not exported into it)\n' "$runner" >&2
373
+ else
374
+ # Isolation as a file, not a briefing. Env vars in a prompt were missed five
375
+ # times in one run — including by an agent whose parent was briefed but did
376
+ # not pass them down. Anyone who finds the worktree finds the runner. The
377
+ # isolation triple is per claim and language-neutral; everything else is the
378
+ # Test entrypoint's own business, so the runner is a thin exec of it.
379
+ cat > "$runner" <<SH
380
+ #!/bin/sh
381
+ # agent-test template: $tmpl_stamp
382
+ export TEST_COMPOSE_PROJECT=ab-$issue TEST_POSTGRES_PORT=$pg TEST_OLLAMA_PORT=$ollama
383
+ exec sh -c '$quoted "\$@"' agent-test "\$@"
384
+ SH
385
+ chmod +x "$runner"
386
+ excl="$(git rev-parse --git-common-dir)/info/exclude"
387
+ grep -qx agent-test "$excl" 2>/dev/null || echo "agent-test" >> "$excl"
388
+ printf ' wrote %s and excluded it\n' "$runner" >&2
389
+ fi
390
+ fi
391
+
392
+ # `$issue` is guarded above (`case … ''|*[!0-9]*|0?*`), but <slug> and <type> are
393
+ # not, and all four string fields derive from them — `$branch` is
394
+ # `$type/$issue-$slug`, `$wt` is `.worktrees/$issue-$slug`, `$runner` is
395
+ # `$wt/agent-test`. A quote in either argument emitted a payload no parser
396
+ # accepts, at exit 0 and — under `--apply` — after the worktree and the label
397
+ # already existed (#119). `$install` is the Recipe cache's own string and can
398
+ # carry any byte a shell command can, a quote included; jstr is what keeps it a
399
+ # JSON string. The numeric fields stay unwrapped: `$issue` is a JSON
400
+ # number by the guard above, and the ports are arithmetic on it.
401
+ #
402
+ # Assigned before the printf, never inline in its argument list — a `$()` there
403
+ # sits outside this `|| die`, contributes an empty argument on failure, and
404
+ # printf still exits 0 with a malformed payload.
405
+ issue_branch=$(jstr "$branch") && issue_wt=$(jstr "$wt") \
406
+ && issue_install=$(jstr "$install") && issue_runner=$(jstr "$runner") \
407
+ || die "could not escape the receipt fields for #$issue"
408
+ # `null`, not the arithmetic object, when a tracked runner kept its own
409
+ # isolation and never received these exports — a receipt claiming ports were
410
+ # applied to a script that does not carry them would be false.
411
+ if [ "$runner_ports_applied" = true ]; then
412
+ ports_json="{\"postgres\":$pg,\"ollama\":$ollama}"
413
+ else
414
+ ports_json=null
415
+ fi
416
+ printf '{"issue":%s,"branch":"%s","worktree":"%s","install":"%s","ports":%s,"runner":"%s","applied":%s}\n' \
417
+ "$issue" "$issue_branch" "$issue_wt" "$issue_install" "$ports_json" "$issue_runner" "$apply"