@feigi/fleet-ctl 3.23.35 → 3.23.37

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 (204) hide show
  1. package/package.json +1 -1
  2. package/scripts/main-checkout.mjs +1 -1
  3. package/scripts/member-write-guard.mjs +1 -1
  4. package/scripts/repo-root.mjs +1 -1
  5. package/skills/run-team/references/reaping.md +1 -1
  6. package/scripts/agent-test-dir-prose.test.mjs +0 -59
  7. package/scripts/agent-test.test.mjs +0 -1640
  8. package/scripts/ambient-git-vars-mjs-prose.test.mjs +0 -397
  9. package/scripts/ambient-git-vars-prose.test.mjs +0 -192
  10. package/scripts/ancestry-check-position-prose.test.mjs +0 -175
  11. package/scripts/apply-ruleset.test.mjs +0 -421
  12. package/scripts/arg-header-probes-prose.test.mjs +0 -394
  13. package/scripts/arg.test.mjs +0 -1354
  14. package/scripts/board-cli.test.mjs +0 -710
  15. package/scripts/board-identity.test.mjs +0 -429
  16. package/scripts/board-liveness-banner.test.mjs +0 -119
  17. package/scripts/board-prev-shape.test.mjs +0 -228
  18. package/scripts/board-tryparse-null.test.mjs +0 -119
  19. package/scripts/board.test.mjs +0 -3121
  20. package/scripts/brief-after-cut-prose.test.mjs +0 -53
  21. package/scripts/candidates-exit3-prose.test.mjs +0 -302
  22. package/scripts/candidates.test.mjs +0 -2312
  23. package/scripts/caveat-unification-prose.test.mjs +0 -98
  24. package/scripts/cell-readout.test.mjs +0 -615
  25. package/scripts/ci-comment-rot-prose.test.mjs +0 -2025
  26. package/scripts/ci-completes-premise-prose.test.mjs +0 -257
  27. package/scripts/ci-state-prose.test.mjs +0 -113
  28. package/scripts/ci-state.test.mjs +0 -1933
  29. package/scripts/ci-vacuous-green.test.mjs +0 -368
  30. package/scripts/citation-sweep-prose.test.mjs +0 -1712
  31. package/scripts/claim-lifecycle.test.mjs +0 -187
  32. package/scripts/claim-ticket.test.mjs +0 -1122
  33. package/scripts/closing-keyword-prose.test.mjs +0 -193
  34. package/scripts/cockpit-launch-idempotent-prose.test.mjs +0 -165
  35. package/scripts/cockpit-tick-nonblocking.test.mjs +0 -210
  36. package/scripts/compute-board.test.mjs +0 -1214
  37. package/scripts/compute-spend.test.mjs +0 -517
  38. package/scripts/consumer-technology-prose.test.mjs +0 -126
  39. package/scripts/cross-repo-citation-prose.test.mjs +0 -155
  40. package/scripts/cwd-isolation-pins.mjs +0 -83
  41. package/scripts/dedupe-guard-prose.test.mjs +0 -243
  42. package/scripts/delete-merged-branch.test.mjs +0 -576
  43. package/scripts/derive-testcmd.test.mjs +0 -595
  44. package/scripts/diff-stats.test.mjs +0 -404
  45. package/scripts/dispatch-block-golden-prose.test.mjs +0 -1130
  46. package/scripts/dispatch-block-pins-prose.test.mjs +0 -615
  47. package/scripts/dispositions-check.test.mjs +0 -936
  48. package/scripts/dispositions-gate-prose.test.mjs +0 -129
  49. package/scripts/drop-merged-label.test.mjs +0 -479
  50. package/scripts/exec-stub.mjs +0 -93
  51. package/scripts/exec-stub.test.mjs +0 -69
  52. package/scripts/expected-jobs-prose.test.mjs +0 -80
  53. package/scripts/fan-out-authorization-prose.test.mjs +0 -80
  54. package/scripts/filing-bar-seat-prose.test.mjs +0 -242
  55. package/scripts/filing-claim-bar-prose.test.mjs +0 -166
  56. package/scripts/finisher-died-prose.test.mjs +0 -31
  57. package/scripts/finisher-dispatch-premise-prose.test.mjs +0 -312
  58. package/scripts/finisher-halt-prose.test.mjs +0 -106
  59. package/scripts/finisher-name-prose.test.mjs +0 -134
  60. package/scripts/finisher-own-tree-prose.test.mjs +0 -299
  61. package/scripts/finisher-pin-race-prose.test.mjs +0 -339
  62. package/scripts/finisher-test-verdict-prose.test.mjs +0 -102
  63. package/scripts/fix-applier-correction-rules-prose.test.mjs +0 -202
  64. package/scripts/fix-applier-result-file-prose.test.mjs +0 -149
  65. package/scripts/fleet-bootstrap.test.mjs +0 -75
  66. package/scripts/fleet-heartbeat.test.mjs +0 -450
  67. package/scripts/fleet-provenance.test.mjs +0 -138
  68. package/scripts/fleet-run.test.mjs +0 -90
  69. package/scripts/fleet-state.test.mjs +0 -460
  70. package/scripts/fleet-tick-prose.test.mjs +0 -300
  71. package/scripts/fleet-tick.test.mjs +0 -2444
  72. package/scripts/force-with-lease-reason-prose.test.mjs +0 -184
  73. package/scripts/frontmatter-check.test.mjs +0 -436
  74. package/scripts/git-env.test.mjs +0 -149
  75. package/scripts/git-status-untracked-mode-sweep.test.mjs +0 -216
  76. package/scripts/gone-walk.test.mjs +0 -213
  77. package/scripts/gp-sep-invariant-prose.test.mjs +0 -188
  78. package/scripts/head-moved-after-label-prose.test.mjs +0 -233
  79. package/scripts/immutable-body-claim-prose.test.mjs +0 -204
  80. package/scripts/implementer-model-tier.test.mjs +0 -235
  81. package/scripts/inflight-citation-prose.test.mjs +0 -144
  82. package/scripts/inflight-exit2-prose.test.mjs +0 -81
  83. package/scripts/inflight-probe-guard-prose.test.mjs +0 -109
  84. package/scripts/inflight-scan-scope-prose.test.mjs +0 -57
  85. package/scripts/inflight.test.mjs +0 -2882
  86. package/scripts/injection-control-prose.test.mjs +0 -404
  87. package/scripts/install-root-audit.test.mjs +0 -464
  88. package/scripts/instrument-check-prose.test.mjs +0 -235
  89. package/scripts/instruments.test.mjs +0 -787
  90. package/scripts/is-cli.test.mjs +0 -99
  91. package/scripts/issue-tracker-prose.test.mjs +0 -71
  92. package/scripts/json.test.mjs +0 -364
  93. package/scripts/ledger-dispatch.test.mjs +0 -927
  94. package/scripts/ledger-grammar.test.mjs +0 -227
  95. package/scripts/ledger-read-require-file.test.mjs +0 -197
  96. package/scripts/ledger-rotate.test.mjs +0 -142
  97. package/scripts/ledger.test.mjs +0 -3436
  98. package/scripts/line-distance-prose.test.mjs +0 -245
  99. package/scripts/liveness-rationale-prose.test.mjs +0 -83
  100. package/scripts/locale-pin-prose.test.mjs +0 -189
  101. package/scripts/main-checkout.test.mjs +0 -332
  102. package/scripts/member-outcome-prose.test.mjs +0 -167
  103. package/scripts/member-outcomes-header.test.mjs +0 -129
  104. package/scripts/member-outcomes-prose.test.mjs +0 -63
  105. package/scripts/member-outcomes.test.mjs +0 -638
  106. package/scripts/member-prompt-prose.test.mjs +0 -165
  107. package/scripts/member-record.test.mjs +0 -541
  108. package/scripts/member-write-guard.test.mjs +0 -214
  109. package/scripts/merge-commit-probe-prose.test.mjs +0 -122
  110. package/scripts/merge-gate.test.mjs +0 -613
  111. package/scripts/muted-git-guard-sweep.test.mjs +0 -493
  112. package/scripts/net-ssh-precedence-prose.test.mjs +0 -91
  113. package/scripts/net-watchdog-prose.test.mjs +0 -79
  114. package/scripts/net.test.mjs +0 -211
  115. package/scripts/no-ci-gate-prose.test.mjs +0 -108
  116. package/scripts/no-undo-audit.test.mjs +0 -3961
  117. package/scripts/no-wave.test.mjs +0 -138
  118. package/scripts/node-floor-sweep.test.mjs +0 -611
  119. package/scripts/pointer-target-prose.test.mjs +0 -186
  120. package/scripts/pr-cost.test.mjs +0 -521
  121. package/scripts/pr-overlap.test.mjs +0 -692
  122. package/scripts/printf-die-sweep.test.mjs +0 -384
  123. package/scripts/prompt-renderer.mjs +0 -40
  124. package/scripts/prompt-renderer.test.mjs +0 -68
  125. package/scripts/prose-pin.mjs +0 -807
  126. package/scripts/prose-pin.test.mjs +0 -1034
  127. package/scripts/prove-merge.test.mjs +0 -995
  128. package/scripts/pull-table-relabel-prose.test.mjs +0 -60
  129. package/scripts/quiet-payload-prose.test.mjs +0 -222
  130. package/scripts/reap-cherry-check-prose.test.mjs +0 -80
  131. package/scripts/reap.test.mjs +0 -4432
  132. package/scripts/reaping-prose.test.mjs +0 -239
  133. package/scripts/reaping-reap-authorizers-prose.test.mjs +0 -112
  134. package/scripts/reaping-release-authorizers-prose.test.mjs +0 -85
  135. package/scripts/recipe-derivation-prose.test.mjs +0 -112
  136. package/scripts/recipe-prove.test.mjs +0 -1042
  137. package/scripts/refuter-scratch-prose.test.mjs +0 -135
  138. package/scripts/release-label-required-prose.test.mjs +0 -71
  139. package/scripts/release-label-split-prose.test.mjs +0 -184
  140. package/scripts/release-ticket-reap-pairing-prose.test.mjs +0 -63
  141. package/scripts/release-ticket.test.mjs +0 -5099
  142. package/scripts/renovate-release-contract.test.mjs +0 -379
  143. package/scripts/repo-root.test.mjs +0 -873
  144. package/scripts/reporter-output-rule-prose.test.mjs +0 -208
  145. package/scripts/review-agent-report-only.test.mjs +0 -49
  146. package/scripts/review-core-booking.test.mjs +0 -35
  147. package/scripts/review-core-cwd-isolation.test.mjs +0 -374
  148. package/scripts/review-core-reads.test.mjs +0 -478
  149. package/scripts/review-core-refuter-scratch.test.mjs +0 -148
  150. package/scripts/review-core-scratch-refusal.test.mjs +0 -64
  151. package/scripts/review-core-shared-test-run.test.mjs +0 -275
  152. package/scripts/review-core-snapshot-path.test.mjs +0 -830
  153. package/scripts/review-core-specialist-scratch.test.mjs +0 -191
  154. package/scripts/review-core-testcmd.test.mjs +0 -335
  155. package/scripts/review-core-unrun.test.mjs +0 -566
  156. package/scripts/review-core-unverified-discriminant.test.mjs +0 -208
  157. package/scripts/review-eval-resolver-path.test.mjs +0 -55
  158. package/scripts/review-host-fixture.mjs +0 -89
  159. package/scripts/review-in-run-retry.test.mjs +0 -211
  160. package/scripts/review-no-vendor-dispatch.test.mjs +0 -45
  161. package/scripts/review-path-default.test.mjs +0 -796
  162. package/scripts/review-runner-spawns.test.mjs +0 -95
  163. package/scripts/review-runner.test.mjs +0 -166
  164. package/scripts/review-tier-audit.test.mjs +0 -140
  165. package/scripts/run-artifact-pr-prose.test.mjs +0 -341
  166. package/scripts/run-merge-bot-prose.test.mjs +0 -1413
  167. package/scripts/run-team-local-hook.test.mjs +0 -442
  168. package/scripts/scratch-partition-invariant-prose.test.mjs +0 -74
  169. package/scripts/scripts-path-citation-sweep.test.mjs +0 -73
  170. package/scripts/section-between-guard-prose.test.mjs +0 -379
  171. package/scripts/select-dimensions.test.mjs +0 -674
  172. package/scripts/shared-refusal.test.mjs +0 -388
  173. package/scripts/shell-traps-prose.test.mjs +0 -504
  174. package/scripts/shortlist.test.mjs +0 -380
  175. package/scripts/slow-transport.mjs +0 -72
  176. package/scripts/snapshot-repo.test.mjs +0 -1006
  177. package/scripts/snapshot-runner-audience-prose.test.mjs +0 -130
  178. package/scripts/spend-view.test.mjs +0 -312
  179. package/scripts/staleness-qualifier-prose.test.mjs +0 -251
  180. package/scripts/staleness-verdict-prose.test.mjs +0 -142
  181. package/scripts/staleness.test.mjs +0 -893
  182. package/scripts/stalled-member-liveness-prose.test.mjs +0 -219
  183. package/scripts/stash-prohibition-prose.test.mjs +0 -313
  184. package/scripts/strip-comments.mjs +0 -44
  185. package/scripts/temp-dir.mjs +0 -47
  186. package/scripts/ticket-router.test.mjs +0 -981
  187. package/scripts/tier-check.test.mjs +0 -828
  188. package/scripts/tier-outcomes-header.test.mjs +0 -518
  189. package/scripts/tier-outcomes.test.mjs +0 -572
  190. package/scripts/tier-roles.test.mjs +0 -535
  191. package/scripts/tracker-block-copy-prose.test.mjs +0 -288
  192. package/scripts/triage-labels-gloss-prose.test.mjs +0 -97
  193. package/scripts/unattended-git-sweep.test.mjs +0 -142
  194. package/scripts/verify-sha-exit2-prose.test.mjs +0 -269
  195. package/scripts/verify-sha.test.mjs +0 -692
  196. package/scripts/watcher-degraded-block.test.mjs +0 -369
  197. package/scripts/watcher-key-prose.test.mjs +0 -106
  198. package/scripts/within-run-pair-prose.test.mjs +0 -212
  199. package/scripts/worktree-audit.test.mjs +0 -1408
  200. package/scripts/worktree-head-prose.test.mjs +0 -276
  201. package/scripts/worktree-listing-sweep.test.mjs +0 -217
  202. package/scripts/worktree-model-prose.test.mjs +0 -216
  203. package/scripts/worktree-porcelain.mjs +0 -35
  204. package/scripts/worktree-porcelain.test.mjs +0 -102
@@ -1,2444 +0,0 @@
1
- // Regression gate for the reconcile tick. Zero deps:
2
- // node --test plugin/scripts/fleet-tick.test.mjs
3
- //
4
- // The pure half locks the guard table and the ledger reading — the whole point
5
- // of #3 is that the table stops being prose the controller must remember, and
6
- // the whole point of #1803 is that the counts stop being numbers the controller
7
- // recites. A "simplification" that drops a row, or a reading that counts a
8
- // settled member live, has to go red here.
9
- import { test } from "node:test";
10
- import assert from "node:assert/strict";
11
- import { reconcile, formatLines, actionable, deriveRun, parseShortlist, unclaimed, refreshWhy, dispositionsToken, currentDispositions } from "./fleet-tick.mjs";
12
-
13
- // Every field named, so a test that cares about one number still states the
14
- // rest — a defaulted field is a guard nobody is pinning.
15
- const state = (over = {}) => ({
16
- implCap: 2, reviewerCap: 6, maxReviews: 6,
17
- implLive: 0, draining: null, tierMismatch: [], tierUnchecked: [], implNames: [],
18
- heads: [], supply: 0, shortlistStatus: "ok", refresh: null,
19
- reviewsLive: 0, fixLive: 0, fixDue: [], reviewDue: [], unlabelled: [],
20
- mergeBotLive: 0, mergeQueue: 0, mergeHeld: 0, mergeConflictHeld: 0,
21
- mainCheckout: { state: "clean" },
22
- ...over,
23
- });
24
- const rowsOf = (s, role) => reconcile(s).filter((r) => r.role === role);
25
- const row = (s, role) => rowsOf(s, role)[0];
26
-
27
- // ---------------------------------------------------------------------------
28
- // The implementer row.
29
-
30
- test("implementers: free slots PULL the next unclaimed heads, one per slot, oldest first", () => {
31
- const r = row(state({ heads: [412, 415, 420], supply: 9 }), "implementers");
32
- assert.equal(r.action, "PULL #412 #415");
33
- assert.equal(r.actual, 0);
34
- assert.equal(r.target, 2);
35
- assert.equal(r.acts, true);
36
- });
37
-
38
- test("implementers: a PULL never takes live past the cap", () => {
39
- for (const implCap of [1, 2, 5, 8]) {
40
- for (const implLive of [0, 1, 2, 5, 9]) {
41
- const r = row(state({ implCap, implLive, heads: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] }), "implementers");
42
- const n = r.action.startsWith("PULL") ? r.action.split(" ").length - 1 : 0;
43
- assert.ok(n === 0 || implLive + n <= implCap, `cap ${implCap} live ${implLive} → ${r.action}`);
44
- }
45
- }
46
- });
47
-
48
- test("implementers: the PULL is bounded by the heads there are, not only by the deficit", () => {
49
- assert.equal(row(state({ implCap: 5, heads: [7] }), "implementers").action, "PULL #7");
50
- });
51
-
52
- test("implementers: at cap nothing is pulled, however many heads are waiting", () => {
53
- const r = row(state({ implLive: 2, heads: [1, 2, 3] }), "implementers");
54
- assert.equal(r.action, "AT CAP");
55
- assert.equal(r.acts, false);
56
- });
57
-
58
- test("implementers: nothing unclaimed suggests /triage and holds idle — never actionable", () => {
59
- const r = row(state({ heads: [] }), "implementers");
60
- assert.equal(r.action, "SUGGEST /triage, hold idle");
61
- assert.equal(r.acts, false);
62
- });
63
-
64
- test("implementers: the backlog gate holds only when an unreviewed PR is left AND no reviewer slot is free", () => {
65
- // Saturated: six units live, one PR still owed its review.
66
- const held = row(state({ heads: [9], reviewsLive: 1, fixLive: 5, reviewDue: [501] }), "implementers");
67
- assert.equal(held.action, "HOLD (review side saturated)");
68
- assert.equal(held.acts, false);
69
- // The narrowing: a deep review backlog with slots free is NOT a hold — the
70
- // reviewer row dispatches into those slots on this same tick. The old gate
71
- // held here at backlog >= 2.
72
- const deep = row(state({ heads: [9], reviewDue: [501, 502, 503] }), "implementers");
73
- assert.equal(deep.action, "PULL #9");
74
- // Saturated slots with nothing owed is not a hold either.
75
- assert.equal(row(state({ heads: [9], fixLive: 6 }), "implementers").action, "PULL #9");
76
- });
77
-
78
- test("implementers: the gate counts the slots this tick's own dispatches fill", () => {
79
- // Five units live, one slot free, two PRs owed: the one free slot takes a
80
- // review now, one PR is left owed and nothing is free — saturated.
81
- const s = state({ heads: [9], reviewsLive: 0, fixLive: 5, reviewDue: [501, 502] });
82
- assert.equal(row(s, "reviewers").action, "DISPATCH review PR#501");
83
- assert.equal(row(s, "implementers").action, "HOLD (review side saturated)");
84
- });
85
-
86
- test("implementers: --max-reviews leaving a PR owed with slots free does not hold implementers", () => {
87
- // The review side is throttled, not saturated: fix-appliers can still take
88
- // the free slots, so more PRs are not a pipeline that gains nothing.
89
- const s = state({ heads: [9], maxReviews: 1, reviewsLive: 1, reviewDue: [501] });
90
- assert.equal(row(s, "implementers").action, "PULL #9");
91
- });
92
-
93
- test("implementers: draining holds the row and outranks every other branch", () => {
94
- for (const over of [{}, { implLive: 2 }, { tierMismatch: ["impl-4"] }, { tierUnchecked: ["impl-4"] }, { reviewDue: [1], fixLive: 6 }]) {
95
- const r = row(state({ heads: [7], draining: "maintainer asked", ...over }), "implementers");
96
- assert.equal(r.action, "HOLD (draining)", JSON.stringify(over));
97
- assert.equal(r.acts, false);
98
- }
99
- });
100
-
101
- // #2255: a tier hold's detail names the step that clears it, so the row
102
- // carries its own command when SKILL.md's prose is gone from context.
103
- test("implementers: a tier mismatch holds the row, names the member, asks the controller to act, and names the replacement", () => {
104
- const r = row(state({ heads: [7], tierMismatch: ["impl-412"] }), "implementers");
105
- assert.equal(r.action, "HOLD (tier mismatch impl-412)");
106
- assert.equal(r.acts, true, "the controller fixes a mismatch unattended — backing off on it is a stall");
107
- assert.match(r.detail, /^unclaimed=1 supply=0 unreviewed=0 — /, "the counts stay in front of the clearing step");
108
- assert.match(r.detail, /dispatch impl-412-b at its row's tier/);
109
- });
110
-
111
- test("implementers: a mismatched replacement names the NEXT retry letter, one per held member", () => {
112
- const r = row(state({ tierMismatch: ["impl-8-b", "impl-9"] }), "implementers");
113
- assert.equal(r.action, "HOLD (tier mismatch impl-8-b impl-9)");
114
- assert.match(r.detail, /dispatch impl-8-c, impl-9-b each at its row's tier/);
115
- assert.doesNotMatch(r.detail, /impl-8-b-b|impl-9-c/);
116
- // `-z` is the grammar's last letter: no legal replacement name to print, so
117
- // the row says so instead of naming one ledger.mjs would refuse.
118
- assert.match(row(state({ tierMismatch: ["impl-8-z"] }), "implementers").detail, /impl-8-z has no retry letter left/);
119
- });
120
-
121
- test("implementers: a mismatch batch with some members named and some out of letters prints both clauses, `; `-joined", () => {
122
- const counts = "unclaimed=0 supply=0 unreviewed=0";
123
- assert.equal(row(state({ tierMismatch: ["impl-8-b", "impl-9-z"] }), "implementers").detail,
124
- `${counts} — dispatch impl-8-c at its row's tier; impl-9-z has no retry letter left`);
125
- assert.equal(row(state({ tierMismatch: ["impl-7-z", "impl-9-z"] }), "implementers").detail,
126
- `${counts} — impl-7-z, impl-9-z have no retry letter left`);
127
- });
128
-
129
- test("implementers: an unchecked implementer holds the row and asks the controller to run the check; a mismatch outranks it", () => {
130
- const r = row(state({ heads: [7], tierUnchecked: ["impl-412"] }), "implementers");
131
- assert.equal(r.action, "HOLD (tier unchecked impl-412)");
132
- assert.equal(r.acts, true, "running tier-check is the controller's own step — backing off on it is a stall");
133
- assert.ok(r.detail.includes(
134
- `run ~/.fleet/bin/fleet-run tier-check.mjs --batch <file> with [{"member":"impl-412","session":"<session>"}]`), r.detail);
135
- const both = row(state({ heads: [7], tierMismatch: ["impl-9"], tierUnchecked: ["impl-412"] }), "implementers");
136
- assert.equal(both.action, "HOLD (tier mismatch impl-9)");
137
- assert.doesNotMatch(both.detail, /tier-check/, "the detail names the step for the hold the row prints, not the one it outranks");
138
- });
139
-
140
- test("implementers: several unchecked members share one batch file, one entry each", () => {
141
- const r = row(state({ tierUnchecked: ["impl-7", "impl-8-b"] }), "implementers");
142
- assert.ok(r.detail.includes(
143
- `[{"member":"impl-7","session":"<session>"},{"member":"impl-8-b","session":"<session>"}]`), r.detail);
144
- });
145
-
146
- test("implementers: no tier hold, no clearing step in the detail", () => {
147
- for (const over of [{ heads: [7] }, { draining: "x", tierUnchecked: ["impl-4"] }, {}]) {
148
- assert.doesNotMatch(row(state(over), "implementers").detail, /—/, JSON.stringify(over));
149
- }
150
- });
151
-
152
- test("implementers: the detail carries the numbers the branch turned on", () => {
153
- const r = row(state({ heads: [4, 5, 6], supply: 57, reviewDue: [1] }), "implementers");
154
- assert.match(r.detail, /unclaimed=3/);
155
- assert.match(r.detail, /supply=57/);
156
- assert.match(r.detail, /unreviewed=0/);
157
- assert.match(row(state({ supply: null, shortlistStatus: "missing" }), "implementers").detail, /supply=\? unreviewed=0 shortlist=missing$/);
158
- });
159
-
160
- // ---------------------------------------------------------------------------
161
- // The reviewer rows.
162
-
163
- test("reviewers: nothing owed and nothing in flight is idle", () => {
164
- const r = row(state(), "reviewers");
165
- assert.equal(r.action, "IDLE OK");
166
- assert.equal(r.target, 6);
167
- assert.equal(r.acts, false);
168
- });
169
-
170
- test("reviewers: fix-appliers are named before reviews — finish what is started", () => {
171
- const rs = rowsOf(state({ fixDue: [346, 348], reviewDue: [350, 351] }), "reviewers");
172
- assert.deepEqual(rs.map((r) => r.action), ["DISPATCH fix-pr PR#346 PR#348", "DISPATCH review PR#350 PR#351"]);
173
- assert.ok(rs.every((r) => r.acts));
174
- // One free slot: the fix-applier takes it and the review waits.
175
- const one = rowsOf(state({ fixLive: 5, fixDue: [346], reviewDue: [350] }), "reviewers");
176
- assert.deepEqual(one.map((r) => r.action), ["DISPATCH fix-pr PR#346"]);
177
- });
178
-
179
- test("reviewers: dispatches never take live units past the reviewer cap", () => {
180
- for (const reviewerCap of [1, 3, 6]) {
181
- for (const [reviewsLive, fixLive] of [[0, 0], [1, 0], [0, 3], [2, 4], [4, 4]]) {
182
- const rs = rowsOf(state({
183
- reviewerCap, maxReviews: reviewerCap, reviewsLive, fixLive,
184
- fixDue: [1, 2, 3, 4], reviewDue: [5, 6, 7, 8],
185
- }), "reviewers");
186
- const n = rs.filter((r) => r.action.startsWith("DISPATCH")).reduce((k, r) => k + r.action.match(/PR#/g).length, 0);
187
- assert.ok(n === 0 || reviewsLive + fixLive + n <= reviewerCap,
188
- `cap ${reviewerCap} live ${reviewsLive}+${fixLive} → ${rs.map((r) => r.action)}`);
189
- }
190
- }
191
- });
192
-
193
- test("reviewers: --max-reviews bounds reviews in flight, and a throttled review is a HOLD that says why", () => {
194
- assert.equal(row(state({ maxReviews: 1, reviewDue: [350, 351] }), "reviewers").action, "DISPATCH review PR#350");
195
- const held = row(state({ maxReviews: 1, reviewsLive: 1, reviewDue: [351] }), "reviewers");
196
- assert.equal(held.action, "HOLD (max-reviews 1 in flight)");
197
- assert.equal(held.acts, false);
198
- // The bound is on REVIEWS: fix-appliers still take the free slots.
199
- const rs = rowsOf(state({ maxReviews: 1, reviewsLive: 1, fixDue: [346], reviewDue: [351] }), "reviewers");
200
- assert.deepEqual(rs.map((r) => r.action), ["DISPATCH fix-pr PR#346"]);
201
- });
202
-
203
- test("reviewers: work owed with every slot full is AT CAP", () => {
204
- assert.equal(row(state({ fixLive: 6, fixDue: [1], reviewDue: [2] }), "reviewers").action, "AT CAP");
205
- });
206
-
207
- // #2331: a finisher that settled `labelled` on a PR still lacking
208
- // `ready-to-merge` is repaired by one fresh finisher, and escalated past that.
209
- // Finishers are uncapped, so neither row ever waits on a reviewer slot.
210
- test("reviewers: one labelled finisher on an unlabelled PR dispatches a finisher, uncapped and actionable", () => {
211
- const unlabelled = [{ pr: 40, labelled: ["finisher-pr-40"] }, { pr: 44, labelled: ["finisher-pr-44-b"] }];
212
- const rs = rowsOf(state({ unlabelled }), "reviewers");
213
- assert.deepEqual(rs.map((r) => r.action), ["DISPATCH finisher PR#40 PR#44"], "IDLE OK never prints beside it");
214
- assert.equal(rs[0].acts, true);
215
- assert.match(rs[0].detail, /finisher-pr-40, finisher-pr-44-b settled labelled; no ready-to-merge/);
216
- const full = rowsOf(state({ fixLive: 6, fixDue: [1], unlabelled }), "reviewers");
217
- assert.deepEqual(full.map((r) => r.action), ["AT CAP", "DISPATCH finisher PR#40 PR#44"], "a full reviewer side holds no finisher");
218
- });
219
-
220
- test("reviewers: a second labelled finisher in one stretch escalates instead — never actionable, never a dispatch", () => {
221
- const rs = rowsOf(state({ unlabelled: [{ pr: 40, labelled: ["finisher-pr-40", "finisher-pr-40-b"] }] }), "reviewers");
222
- assert.deepEqual(rs.map((r) => r.action), ["ESCALATE unlabelled PR#40"]);
223
- assert.equal(rs[0].acts, false);
224
- assert.match(rs[0].detail, /finisher-pr-40, finisher-pr-40-b settled labelled/);
225
- const both = rowsOf(state({ fixDue: [7], unlabelled: [
226
- { pr: 40, labelled: ["finisher-pr-40", "finisher-pr-40-b"] }, { pr: 41, labelled: ["finisher-pr-41"] },
227
- ] }), "reviewers");
228
- assert.deepEqual(both.map((r) => r.action), ["DISPATCH fix-pr PR#7", "DISPATCH finisher PR#41", "ESCALATE unlabelled PR#40"]);
229
- });
230
-
231
- // ---------------------------------------------------------------------------
232
- // The merge-bot row.
233
-
234
- test("merge-bot: a queued, unheld PR with no bot live dispatches one", () => {
235
- const r = row(state({ mergeQueue: 3 }), "merge-bot");
236
- assert.equal(r.action, "DISPATCH merge-bot");
237
- assert.equal(r.acts, true);
238
- assert.match(r.detail, /merge-queue=3 held=0/);
239
- });
240
-
241
- test("merge-bot: never a second bot, an empty queue is idle, a fully held queue holds", () => {
242
- assert.equal(row(state({ mergeBotLive: 1, mergeQueue: 9 }), "merge-bot").action, "AT CAP");
243
- assert.equal(row(state({ mergeQueue: 0 }), "merge-bot").action, "IDLE OK");
244
- const held = row(state({ mergeQueue: 2, mergeHeld: 2 }), "merge-bot");
245
- assert.equal(held.action, "HOLD");
246
- assert.equal(held.acts, false);
247
- assert.equal(row(state({ mergeQueue: 3, mergeHeld: 2 }), "merge-bot").action, "DISPATCH merge-bot");
248
- });
249
-
250
- // ---------------------------------------------------------------------------
251
- // The shortlist row.
252
-
253
- test("shortlist: a refresh prints REFRESHED with its counts, and is actionable only when it changed the list", () => {
254
- const changed = reconcile(state({ refresh: { ok: true, entries: 5, lifted: 1, changed: true, trigger: "unclaimed 0 < cap 2" } })).at(-1);
255
- assert.equal(changed.role, "shortlist");
256
- assert.equal(changed.action, "REFRESHED shortlist: 5 entries; 1 lifted");
257
- assert.equal(changed.acts, true);
258
- // Unchanged: every idle heartbeat tick refreshes while the shortlist is
259
- // short of the cap, so an unconditionally actionable REFRESHED would pin the
260
- // beat at its base interval all night — ADR 0008 §6's back-off, undone.
261
- const same = reconcile(state({ refresh: { ok: true, entries: 0, lifted: 0, changed: false, trigger: "shortlist empty" } })).at(-1);
262
- assert.equal(same.action, "REFRESHED shortlist: 0 entries; 0 lifted");
263
- assert.equal(same.acts, false);
264
- assert.match(same.detail, /unchanged/);
265
- assert.equal(actionable(reconcile(state({ refresh: { ok: true, entries: 0, lifted: 0, changed: false, trigger: "x" } }))), false);
266
- });
267
-
268
- test("shortlist: a failed refresh is named and actionable, never a refusal of the tick", () => {
269
- const rows = reconcile(state({ mergeQueue: 1, refresh: { ok: false, why: "shortlist.mjs exited 2: boom", trigger: "shortlist missing" } }));
270
- const r = rows.at(-1);
271
- assert.equal(r.action, "REFRESH FAILED");
272
- assert.match(r.detail, /shortlist missing — shortlist\.mjs exited 2: boom/);
273
- assert.equal(r.acts, true);
274
- assert.equal(row(state({ mergeQueue: 1 }), "merge-bot").action, "DISPATCH merge-bot");
275
- });
276
-
277
- test("no refresh, no shortlist row", () => {
278
- assert.deepEqual(reconcile(state()).map((r) => r.role), ["implementers", "reviewers", "merge-bot"]);
279
- });
280
-
281
- // ---------------------------------------------------------------------------
282
- // The router row: pr-cost.mjs --guard's verdict, read off a guard file the way
283
- // the tick reads it. One case per state, each against a fixture file.
284
-
285
- import { routerRows, readCostGuard } from "./fleet-tick.mjs";
286
-
287
- const BASE = { cell: "slow-high", n: 31, mean_usd: 31, fail_rate: 0.29 };
288
- const guardJson = (over = {}) => JSON.stringify({
289
- computed_at: "2026-10-03T00:00:00.000Z", window_start: "2026-10-03", baseline: BASE,
290
- cells: [{ cell: "slow-high", n: 31, mean_usd: 31, fail_rate: 0.29 }, { cell: "task-high", n: 24, mean_usd: 12.3, fail_rate: 0.25 }],
291
- tripped: [], verdict: "ok", retire: false, min_n: 20, ...over,
292
- });
293
- function routerRowOf(text) {
294
- const dir = mkdtempSync(join(tmpdir(), "fleet-tick-guard-"));
295
- try {
296
- const path = join(dir, "cost-guard.json");
297
- if (text !== null) writeFileSync(path, text);
298
- const rows = reconcile(state({ router: readCostGuard(path) }));
299
- assert.equal(actionable(rows), false, "no router state is work for the controller");
300
- const r = rows.filter((x) => x.role === "router");
301
- assert.equal(r.length, 1);
302
- assert.equal(rows.at(-1), r[0], "the router row prints last");
303
- return { ...r[0], line: formatLines(rows).at(-1) };
304
- } finally {
305
- rmSync(dir, { recursive: true, force: true });
306
- }
307
- }
308
-
309
- test("router: a guard with no cell tripped is OK, naming each cell against the baseline", () => {
310
- const r = routerRowOf(guardJson());
311
- assert.equal(r.action, "OK");
312
- assert.equal(r.acts, false);
313
- assert.equal(r.line, "router → OK (task-high $12.30 vs $31.00, fail 25% vs 29%, n=24/31; guard computed 2026-10-03T00:00:00.000Z)");
314
- });
315
-
316
- test("router: the row carries the guard's own computed_at, so a stale OK reads as old", () => {
317
- const r = routerRowOf(guardJson({ computed_at: "2026-09-01T08:00:00.000Z" }));
318
- assert.equal(r.action, "OK");
319
- assert.match(r.detail, /; guard computed 2026-09-01T08:00:00\.000Z$/);
320
- });
321
-
322
- test("router: a tripped cell is DEFAULT-ONLY, naming the cost guard and the cell it tripped", () => {
323
- const r = routerRowOf(guardJson({
324
- verdict: "tripped", tripped: ["smol-high"],
325
- cells: [{ ...BASE }, { cell: "smol-high", n: 21, mean_usd: 9.5, fail_rate: 0.45 }, { cell: "task-high", n: 24, mean_usd: 12.3, fail_rate: 0.25 }],
326
- }));
327
- assert.equal(r.action, "DEFAULT-ONLY");
328
- assert.equal(r.detail, "cost guard: smol-high $9.50 vs $31.00, fail 45% vs 29%, n=21/31; guard computed 2026-10-03T00:00:00.000Z");
329
- const retired = routerRowOf(guardJson({ verdict: "tripped", tripped: ["task-high"], retire: true }));
330
- assert.match(retired.detail, /^cost guard: task-high .*; every non-default stage-1 cell tripped — the router retires; guard computed /);
331
- });
332
-
333
- test("router: a baseline short of its n is NO VERDICT, with the count and the n it needs", () => {
334
- const r = routerRowOf(guardJson({ verdict: "none", baseline: { ...BASE, n: 7 } }));
335
- assert.equal(r.action, "NO VERDICT");
336
- assert.equal(r.line, "router → NO VERDICT (baseline n=7/20; guard computed 2026-10-03T00:00:00.000Z)");
337
- });
338
-
339
- test("router: no guard file is DEFAULT-ONLY, naming the command that writes one", () => {
340
- const r = routerRowOf(null);
341
- assert.equal(r.line, "router → DEFAULT-ONLY (cost-guard.json missing — run pr-cost.mjs --guard)");
342
- });
343
-
344
- test("router: a guard file that is not a verdict reads DEFAULT-ONLY too — deleting or breaking it cannot evade the guard", () => {
345
- const cell = { cell: "task-high", n: 24, mean_usd: 1, fail_rate: 0 };
346
- for (const [text, why] of [["{", "not JSON"], [guardJson({ verdict: "maybe" }), "not a guard verdict"],
347
- [guardJson({ tripped: "smol-high" }), "not a guard verdict"], [guardJson({ min_n: undefined }), "not a guard verdict"],
348
- [guardJson({ computed_at: undefined }), "not a guard verdict"],
349
- [guardJson({ cells: [{ ...cell, n: "24" }] }), "not a guard verdict"],
350
- [guardJson({ cells: [{ ...cell, mean_usd: "12" }] }), "not a guard verdict"],
351
- [guardJson({ cells: [{ ...cell, fail_rate: "0.2" }] }), "not a guard verdict"],
352
- [guardJson({ baseline: { ...BASE, mean_usd: "31" } }), "not a guard verdict"],
353
- // Self-contradicting files: a verdict that disagrees with tripped[], a tripped cell the file never reports, a non-boolean retire.
354
- [guardJson({ verdict: "ok", tripped: ["task-high"] }), "not a guard verdict"],
355
- [guardJson({ verdict: "tripped", tripped: [] }), "not a guard verdict"],
356
- [guardJson({ verdict: "tripped", tripped: ["ghost"] }), "not a guard verdict"],
357
- [guardJson({ retire: "yes" }), "not a guard verdict"]]) {
358
- const r = routerRowOf(text);
359
- assert.equal(r.action, "DEFAULT-ONLY", text);
360
- assert.equal(r.detail, `cost-guard.json unreadable (${why}) — run pr-cost.mjs --guard`, text);
361
- }
362
- });
363
-
364
- test("router: with no guard state read at all, no router row", () => {
365
- assert.deepEqual(routerRows(state()), []);
366
- });
367
-
368
- test("formatLines prints role, actual/target and the ACTION on one line each", () => {
369
- assert.deepEqual(
370
- formatLines(reconcile(state({
371
- heads: [412], supply: 57, fixDue: [346], reviewDue: [350], mergeQueue: 2, mergeHeld: 1,
372
- refresh: { ok: true, entries: 3, lifted: 0, changed: true, trigger: "unclaimed 1 < cap 2" },
373
- }))),
374
- [
375
- "implementers 0/2 → PULL #412 (unclaimed=1 supply=57 unreviewed=0)",
376
- "reviewers 0/6 → DISPATCH fix-pr PR#346 (reviews=0 fix-pr=0 fix-due=1 review-due=1 max-reviews=6)",
377
- "reviewers 0/6 → DISPATCH review PR#350 (reviews=0 fix-pr=0 fix-due=1 review-due=1 max-reviews=6)",
378
- "merge-bot 0/1 → DISPATCH merge-bot (merge-queue=2 held=1)",
379
- "shortlist → REFRESHED shortlist: 3 entries; 0 lifted (unclaimed 1 < cap 2)",
380
- ],
381
- );
382
- });
383
-
384
- test("actionable: exactly the rows that name work the controller can do unattended", () => {
385
- const acts = (over) => actionable(reconcile(state(over)));
386
- assert.equal(acts({}), false, "an idle tick asks for nothing");
387
- assert.equal(acts({ heads: [1] }), true, "PULL");
388
- assert.equal(acts({ fixDue: [1] }), true, "DISPATCH fix-pr");
389
- assert.equal(acts({ reviewDue: [1] }), true, "DISPATCH review");
390
- assert.equal(acts({ mergeQueue: 1 }), true, "DISPATCH merge-bot");
391
- assert.equal(acts({ implLive: 2, heads: [1] }), false, "AT CAP");
392
- assert.equal(acts({ draining: "x", heads: [1] }), false, "HOLD (draining)");
393
- });
394
-
395
- // ---------------------------------------------------------------------------
396
- // The main-checkout hold (#2210): anything but `clean` holds every
397
- // dispatching row.
398
-
399
- const BUSY = {
400
- heads: [412], fixDue: [346], reviewDue: [350], mergeQueue: 1,
401
- refresh: { ok: true, entries: 3, lifted: 0, changed: true, trigger: "unclaimed 1 < cap 2" },
402
- };
403
- const summary = (s) => reconcile(s).map((r) => [r.role, r.action, r.acts]);
404
-
405
- test("main checkout: dirty holds every dispatching row once per role, never actionable; the shortlist row still reports", () => {
406
- const hold = "HOLD (main checkout dirty)";
407
- assert.deepEqual(summary(state({ ...BUSY, mainCheckout: { state: "dirty", changed: ["x"] } })), [
408
- ["implementers", hold, false],
409
- ["reviewers", hold, false],
410
- ["merge-bot", hold, false],
411
- ["shortlist", "REFRESHED shortlist: 3 entries; 0 lifted", true],
412
- ]);
413
- const [impl] = reconcile(state({ ...BUSY, implLive: 1, mainCheckout: { state: "dirty", changed: ["x"] } }));
414
- assert.equal(`${impl.actual}/${impl.target}`, "1/2", "the held row lost its counts");
415
- });
416
-
417
- test("main checkout: unknown holds too, never clean; so does a check that gave no answer at all", () => {
418
- for (const mainCheckout of [{ state: "unknown", cause: "read", why: "boom" }, { state: "unknown", cause: "baseline", why: "x" }, undefined]) {
419
- assert.deepEqual(summary(state({ ...BUSY, refresh: null, mainCheckout })), [
420
- ["implementers", "HOLD (main checkout unknown)", false],
421
- ["reviewers", "HOLD (main checkout unknown)", false],
422
- ["merge-bot", "HOLD (main checkout unknown)", false],
423
- ], JSON.stringify(mainCheckout));
424
- }
425
- });
426
-
427
- test("main checkout: no baseline holds every dispatching row and asks the controller to record one", () => {
428
- const rows = reconcile(state({ ...BUSY, refresh: null, mainCheckout: { state: "absent", baseline: "/x" } }));
429
- assert.deepEqual(rows.map((r) => r.action), Array(3).fill("HOLD (main checkout no baseline)"));
430
- assert.equal(actionable(rows), true);
431
- });
432
-
433
- test("main checkout: the hold outranks every role's own guards — drain and tier holds included", () => {
434
- const s = state({ ...BUSY, refresh: null, draining: "x", tierMismatch: ["impl-1"], mainCheckout: { state: "dirty", changed: ["x"] } });
435
- assert.equal(row(s, "implementers").action, "HOLD (main checkout dirty)");
436
- assert.equal(actionable(reconcile(s)), false, "a tier mismatch under a dirty checkout still asks the controller to dispatch");
437
- });
438
-
439
- test("main checkout: clean holds nothing", () => {
440
- assert.deepEqual(summary(state({ ...BUSY, refresh: null })).map(([, action]) => action),
441
- ["PULL #412", "DISPATCH fix-pr PR#346", "DISPATCH review PR#350", "DISPATCH merge-bot"]);
442
- });
443
-
444
- // ---------------------------------------------------------------------------
445
- // Reading the run: deriveRun() over `ledger.mjs read`'s payload and the open
446
- // PR list. Rows are spelled the way ledger.mjs dispatch/settle write them.
447
-
448
- const HEAD_A = "abc1234" + "0".repeat(33);
449
- const HEAD_B = "def5678" + "0".repeat(33);
450
- const pr = (number, labels = [], closes = [number + 1000], headRefOid = HEAD_B) => ({
451
- number, labels: labels.map((name) => ({ name })),
452
- closingIssuesReferences: closes.map((n) => ({ number: n })),
453
- headRefOid,
454
- });
455
- const run = (ledger, prs = [], closed, finished) => deriveRun({ rows: [], dispatched: [], drain: null, ...ledger }, prs, closed, finished);
456
-
457
- test("deriveRun: live implementers are the unsettled impl- tokens — the #1692 shape, read rather than recited", () => {
458
- // #1692 was two sources disagreeing about the same fleet. There is one
459
- // source now: two live tokens are two live implementers, whatever else the
460
- // file says.
461
- const r = run({
462
- rows: ["#412 impl-412 · class=routine", "#415 impl-415", "#300 impl-300=PR#344 → PR#344", "#301 impl-301=bailed"],
463
- dispatched: ["impl-412", "impl-415", "impl-300=PR#344", "impl-301=bailed"],
464
- });
465
- assert.equal(r.implLive, 2);
466
- assert.deepEqual([...r.claimed].sort((a, b) => a - b), [300, 301, 412, 415]);
467
- });
468
-
469
- test("deriveRun: a member settled anywhere is settled, and a -b replacement is its own member", () => {
470
- const r = run({
471
- rows: ["#412 impl-412=killed · impl-412-b"],
472
- dispatched: ["impl-412=killed", "impl-412-b"],
473
- });
474
- assert.equal(r.implLive, 1);
475
- // A hand-edited row still carrying the bare token of a member `## Dispatched`
476
- // records as settled: settled wins, because `settle` is the one writer of an
477
- // outcome and a stale bare copy is what a whole-line `row` rewrite leaves.
478
- assert.equal(run({ rows: ["#9 impl-9"], dispatched: ["impl-9=PR#12"] }).implLive, 0);
479
- });
480
-
481
- test("deriveRun: live reviewer units are in-flight reviews plus unsettled fix-appliers", () => {
482
- const r = run({
483
- rows: [
484
- "#10 impl-10=PR#20 → PR#20 · review=wf:run1",
485
- "#11 impl-11=PR#21 → PR#21 · review=wf:run2 reviewed=abc1234:2/1/0 · fix-pr-21",
486
- "#12 impl-12=PR#22 → PR#22 · review=member:review-pr-22 reviewed=abc1234:0/0/0",
487
- "#13 impl-13=PR#23 → PR#23 · review=wf:run3=failed",
488
- "#14 impl-14=PR#24 → PR#24 · review=wf:run4=failed review=fallback:review-pr-24",
489
- ],
490
- dispatched: ["fix-pr-21"],
491
- }, [pr(20), pr(21), pr(22), pr(23), pr(24)]);
492
- assert.equal(r.reviewsLive, 2, "run1 and the fallback are in flight; run2 returned, run3 is dead");
493
- assert.equal(r.fixLive, 1);
494
- });
495
-
496
- test("deriveRun: live names every unsettled member and each in-flight review by its PR — a review=wf: run has no member token of its own", () => {
497
- const r = run({
498
- rows: [
499
- "#10 impl-10=PR#20 → PR#20 · review=wf:run1",
500
- "#11 impl-11=PR#21 → PR#21 · review=wf:run2 reviewed=abc1234:2/1/0 · fix-pr-21",
501
- "#13 impl-13=PR#23 → PR#23 · review=wf:run3=failed",
502
- "#15 impl-15",
503
- ],
504
- dispatched: ["fix-pr-21"],
505
- }, [pr(20), pr(21), pr(23)]);
506
- assert.deepEqual(r.live, ["fix-pr-21", "impl-15", "review:PR#20"], "run2 returned and run3 is dead, so neither is live");
507
- });
508
-
509
- // A review whose own `reviewed=` write never landed stays in flight in the
510
- // ledger forever. The ledger cannot say the PR has since merged; the caller's
511
- // probe can, and hands the merged or closed PR numbers to the fold.
512
- const ZOMBIE_ROWS = [20, 21, 22].map((n) => `#${n - 10} impl-${n - 10}=PR#${n} → PR#${n} · review=member:review-pr-${n}`);
513
-
514
- test("deriveRun: an in-flight review whose PR has left the open list is named for a probe, and still counts until the probe answers", () => {
515
- const r = run({ rows: ZOMBIE_ROWS }, [pr(21)]);
516
- assert.deepEqual(r.reviewsOffList, [20, 22], "only the in-flight reviews of PRs not on the open list");
517
- assert.equal(r.reviewsLive, 3, "no probe answer: fail closed, every one still reads as live");
518
- assert.deepEqual(r.live, ["review:PR#20", "review:PR#21", "review:PR#22"]);
519
- });
520
-
521
- test("deriveRun: reviewsOffList is ascending whatever order the ledger rows arrive in", () => {
522
- const r = run({ rows: [...ZOMBIE_ROWS].reverse() }, [pr(21)]);
523
- assert.deepEqual(r.reviewsOffList, [20, 22], "rows arrive 22, 21, 20: the sort, not ledger order, makes it ascending");
524
- });
525
-
526
- test("deriveRun: a review in flight on a PR the caller found merged or closed is not live", () => {
527
- const r = run({ rows: ZOMBIE_ROWS }, [pr(21)], undefined, new Set([20, 22]));
528
- assert.equal(r.reviewsLive, 1, "only PR 21's review, on the open list, is live");
529
- assert.deepEqual(r.live, ["review:PR#21"]);
530
- });
531
-
532
- test("deriveRun: a review in flight on an OPEN PR is live whatever the finished set says", () => {
533
- // A probe answer that contradicts the open list is not trusted to retire a
534
- // review: the open list is the fold's own read of which PRs are open.
535
- const r = run({ rows: ZOMBIE_ROWS }, [pr(20), pr(21), pr(22)], undefined, new Set([20, 21, 22]));
536
- assert.equal(r.reviewsLive, 3);
537
- assert.deepEqual(r.reviewsOffList, []);
538
- });
539
-
540
- test("deriveRun: a finished PR's returned review and settled members read as before", () => {
541
- const rows = [...ZOMBIE_ROWS, "#40 impl-40=PR#50 → PR#50 · review=member:review-pr-50 reviewed=abc1234:0/0/0"];
542
- const r = run({ rows }, [], undefined, new Set([20, 21, 22, 50]));
543
- assert.deepEqual([r.reviewsLive, r.reviewsOffList, r.live], [0, [20, 21, 22], []]);
544
- assert.deepEqual(r.reviewed.map((x) => x.pr), [50], "the returned review is still on record");
545
- });
546
-
547
- test("deriveRun: fix-pr is due on a returned review with survivors and no fix-applier since", () => {
548
- const r = run({
549
- rows: [
550
- "#10 impl-10=PR#20 → PR#20 · review=wf:a reviewed=abc1234:2/0/0",
551
- "#11 impl-11=PR#21 → PR#21 · review=wf:b reviewed=abc1234:0/3/1",
552
- "#12 impl-12=PR#22 → PR#22 · review=wf:c reviewed=abc1234:1/0/0 · fix-pr-22=applied:def5678",
553
- // Re-reviewed after its fix, and the second review found more.
554
- "#13 impl-13=PR#23 → PR#23 · review=wf:d reviewed=abc1234:1/0/0 · fix-pr-23=applied:def5678 review=wf:e reviewed=def5678:1/0/0",
555
- // A closed PR is nobody's work.
556
- "#14 impl-14=PR#24 → PR#24 · review=wf:f reviewed=abc1234:4/0/0",
557
- ],
558
- dispatched: ["fix-pr-22=applied:def5678", "fix-pr-23=applied:def5678"],
559
- }, [pr(20), pr(21), pr(22), pr(23)]);
560
- assert.deepEqual(r.fixDue, [20, 23]);
561
- });
562
-
563
- test("deriveRun: review is due on open PRs that close an issue and carry no review token, oldest first", () => {
564
- const r = run({
565
- rows: [
566
- "#10 impl-10=PR#30 → PR#30 · review=wf:a",
567
- "#11 impl-11=PR#31 → PR#31",
568
- "#12 impl-12=PR#32 → PR#32 · review=wf:b=failed",
569
- ],
570
- }, [
571
- pr(33), pr(31), pr(30), pr(32),
572
- pr(34, ["ready-to-merge"]), // signed off — not review work
573
- pr(35, [], []), // closes no issue — not review work (#590)
574
- ]);
575
- assert.deepEqual(r.reviewDue, [31, 32, 33]);
576
- });
577
-
578
- test("deriveRun: a PR row keyed by the PR's own number is read as that PR's", () => {
579
- // A PR this run's implementers did not open: `dispatch 350 fix-pr-350` keys
580
- // its row `#350`, the same fallback ledger.mjs's memberRowIndex() takes.
581
- const r = run({ rows: ["#350 review=wf:x reviewed=abc1234:1/0/0"] }, [pr(350)]);
582
- assert.deepEqual(r.fixDue, [350]);
583
- assert.deepEqual(r.reviewDue, []);
584
- });
585
-
586
- // #2083: a finisher that halts `past-pin` found commits past the head the
587
- // review read. Once a review has RETURNED, `reviewedAny` holds it out of
588
- // review-due for good, so marking the review failed cannot re-queue it —
589
- // the halt itself does, until a new review is running or has returned.
590
- test("deriveRun: a finisher halted past-pin re-queues the review while the head is past the reviewed one", () => {
591
- const row = "#10 impl-10=PR#40 → PR#40 · review=wf:a reviewed=abc1234:0/0/0 · finisher-pr-40=halted:past-pin";
592
- assert.deepEqual(run({ rows: [row] }, [pr(40, [], [10], HEAD_B)]).reviewDue, [40]);
593
- // Settled only in `## Dispatched`, a bare copy on the row: settled anywhere is settled.
594
- assert.deepEqual(run({
595
- rows: ["#10 impl-10=PR#40 → PR#40 · review=wf:a reviewed=abc1234:0/0/0 · finisher-pr-40"],
596
- dispatched: ["finisher-pr-40=halted:past-pin"],
597
- }, [pr(40, [], [10], HEAD_B)]).reviewDue, [40]);
598
- // A re-review that died before returning leaves it owed.
599
- assert.deepEqual(run({ rows: [`${row} review=wf:b=failed`] }, [pr(40, [], [10], HEAD_B)]).reviewDue, [40]);
600
- });
601
-
602
- test("deriveRun: a head past reviewed= with no past-pin halt stays not due — a fix-applier's push is finisher duty 2's", () => {
603
- const base = "#10 impl-10=PR#40 → PR#40 · review=wf:a reviewed=abc1234:1/0/0 · fix-pr-40=applied:def5678";
604
- for (const row of [
605
- base,
606
- `${base} · finisher-pr-40=labelled`,
607
- `${base} · finisher-pr-40`,
608
- `${base} · finisher-pr-40=failed`,
609
- // Every other halt cause escalates to a human; none re-queues a review.
610
- `${base} · finisher-pr-40=halted:live-editor`,
611
- `${base} · finisher-pr-40=halted:rebase`,
612
- `${base} · finisher-pr-40=halted:other`,
613
- // A later finisher attempt replaces the halted one, live or settled —
614
- // by retry suffix (compute-board.mjs's laterAttempt ordering), never by
615
- // row-text position: a row rewrite can leave `-b` sitting BEFORE the
616
- // older halted token (#2083).
617
- `${base} · finisher-pr-40=halted:past-pin · finisher-pr-40-b`,
618
- `${base} · finisher-pr-40=halted:past-pin · finisher-pr-40-b=halted:live-editor`,
619
- `${base} · finisher-pr-40-b · finisher-pr-40=halted:past-pin`,
620
- `${base} · finisher-pr-40-b=halted:live-editor · finisher-pr-40=halted:past-pin`,
621
- // Another PR's finisher on the row is not this PR's halt.
622
- `${base} · finisher-pr-41=halted:past-pin`,
623
- ]) {
624
- assert.deepEqual(run({ rows: [row] }, [pr(40, [], [10], HEAD_B)]).reviewDue, [], row);
625
- }
626
- });
627
-
628
- test("deriveRun: a past-pin halt is answered once the head is the reviewed one, or a review is running or returned after it", () => {
629
- const halted = "#10 impl-10=PR#40 → PR#40 · review=wf:a reviewed=abc1234:0/0/0 · finisher-pr-40=halted:past-pin";
630
- const not = (row, head) => assert.deepEqual(run({ rows: [row] }, [pr(40, [], [10], head)]).reviewDue, [], `${row} @ ${head}`);
631
- not(halted, HEAD_A); // the reviewed head IS the PR head — nothing unread
632
- not(halted, HEAD_A.toUpperCase());
633
- not(`${halted} review=wf:b`, HEAD_B); // the re-review is in flight
634
- not(`${halted} review=wf:b reviewed=def5678:0/0/0`, HEAD_B); // and returned
635
- // A fix-applier's push after that re-review is duty 2's again, not a second re-review.
636
- not(`${halted} review=wf:b reviewed=def5678:1/0/0 · fix-pr-40=applied:0123456`, "0123456" + "0".repeat(33));
637
- // Signed off, or closing no issue, is no review work whatever the halt says.
638
- assert.deepEqual(run({ rows: [halted] }, [pr(40, ["ready-to-merge"], [10], HEAD_B), pr(41, [], [], HEAD_B)]).reviewDue, []);
639
- });
640
-
641
- test("deriveRun: a ticket row settled =PR#M shares PR M's state with M's own row, in either order (#2283)", () => {
642
- // ledger.mjs's `settle impl-658-c=PR#724` names PR 724 on the ticket row, and
643
- // a `#724` row can carry that PR's tokens beside it. Both rows are PR 724's:
644
- // whichever sorts first must not swallow what the other records.
645
- const prs = [pr(724, [], [658])];
646
- const ticket = "#658 impl-658-c=PR#724";
647
- for (const rows of [[ticket, "#724 review=member:review-pr-724"], ["#724 review=member:review-pr-724", ticket]]) {
648
- const r = run({ rows }, prs);
649
- assert.equal(r.reviewsLive, 1, `the live review is counted: ${rows}`);
650
- assert.deepEqual(r.reviewDue, [], `a reviewed PR is not re-offered: ${rows}`);
651
- }
652
- for (const rows of [[ticket, "#724 review=member:review-pr-724 reviewed=abc1234:2/1/0"], ["#724 review=member:review-pr-724 reviewed=abc1234:2/1/0", ticket]]) {
653
- const r = run({ rows }, prs);
654
- assert.deepEqual([r.reviewsLive, r.fixDue, r.reviewDue], [0, [724], []], `${rows}`);
655
- assert.ok(r.claimed.has(658), "the ticket row still reads as claimed");
656
- }
657
- });
658
-
659
- // #2331: a finisher can settle `labelled` without ever adding the label. The
660
- // open-PR list the tick already reads says so; a `label-off=<attempt>` token
661
- // marks the controller's own deliberate removals, so they never read as a miss.
662
- const LABELLED = "#10 impl-10=PR#40 → PR#40 · review=wf:a reviewed=abc1234:0/0/0 · finisher-pr-40=labelled";
663
-
664
- test("deriveRun: a labelled finisher on a PR without ready-to-merge is unlabelled, and the tick dispatches a finisher", () => {
665
- const r = run({ rows: [LABELLED], dispatched: ["finisher-pr-40=labelled"] }, [pr(40, ["minor"], [10])]);
666
- assert.deepEqual(r.unlabelled, [{ pr: 40, labelled: ["finisher-pr-40"] }]);
667
- const rows = reconcile({ ...state(), ...r }).filter((x) => x.role === "reviewers");
668
- assert.deepEqual(rows.map((x) => x.action), ["DISPATCH finisher PR#40"]);
669
- // Settled in `## Dispatched` alone, a bare row copy beside it: settled anywhere is settled.
670
- assert.deepEqual(run({ rows: ["#10 impl-10=PR#40 → PR#40 · finisher-pr-40"], dispatched: ["finisher-pr-40=labelled"] },
671
- [pr(40, ["minor"], [10])]).unlabelled, [{ pr: 40, labelled: ["finisher-pr-40"] }]);
672
- });
673
-
674
- test("deriveRun: a labelled finisher whose PR carries ready-to-merge is nothing — labelled settles exactly as before", () => {
675
- const r = run({ rows: [LABELLED], dispatched: ["finisher-pr-40=labelled"] }, [pr(40, ["minor", "ready-to-merge"], [10])]);
676
- assert.deepEqual(r.unlabelled, []);
677
- assert.deepEqual(reconcile({ ...state(), ...r }).filter((x) => x.role === "reviewers").map((x) => x.action), ["IDLE OK"]);
678
- });
679
-
680
- test("deriveRun: only a settled-labelled LATEST attempt is a miss — live, halted, failed and closed PRs are not", () => {
681
- const base = "#10 impl-10=PR#40 → PR#40";
682
- const open = [pr(40, ["minor"], [10])];
683
- for (const row of [
684
- `${base} · finisher-pr-40`, // live, not yet reported
685
- `${base} · finisher-pr-40=labelled · finisher-pr-40-b`, // the repair is live
686
- `${base} · finisher-pr-40-b · finisher-pr-40=labelled`, // the same, by suffix not position
687
- `${base} · finisher-pr-40=halted:rebase`, // a halt refused to label, and says so
688
- `${base} · finisher-pr-40=labelled · finisher-pr-40-b=halted:past-pin`,
689
- `${base} · finisher-pr-40=failed`, // died, no label: the cockpit's to flag, no repair here
690
- `${base} · finisher-pr-40=killed`,
691
- `${base} · finisher-pr-40=labelled · finisher-pr-40-b=failed`,
692
- `${base} · fix-pr-40=applied:def5678`, // no finisher at all
693
- ]) {
694
- assert.deepEqual(run({ rows: [row] }, open).unlabelled, [], row);
695
- }
696
- assert.deepEqual(run({ rows: [LABELLED] }, []).unlabelled, [], "a PR off the open list is nobody's work");
697
- });
698
-
699
- test("deriveRun: a failed or killed latest finisher prints no DISPATCH or ESCALATE finisher line", () => {
700
- const open = [pr(40, ["minor"], [10])];
701
- for (const outcome of ["failed", "killed"]) {
702
- const r = run({ rows: [`${LABELLED} · finisher-pr-40-b=${outcome}`] }, open);
703
- assert.deepEqual(r.unlabelled, [], outcome);
704
- assert.deepEqual(reconcile({ ...state(), ...r }).filter((x) => x.role === "reviewers").map((x) => x.action), ["IDLE OK"], outcome);
705
- }
706
- });
707
-
708
- test("deriveRun: a label-off'd attempt is the controller's own removal — nothing until a later attempt labels again", () => {
709
- const open = [pr(40, ["minor"], [10])];
710
- for (const row of [
711
- `${LABELLED} label-off=finisher-pr-40`,
712
- "#10 label-off=finisher-pr-40 impl-10=PR#40 → PR#40 · finisher-pr-40=labelled", // before the attempt it names
713
- `${LABELLED} · finisher-pr-40-b=labelled label-off=finisher-pr-40-b`,
714
- `${LABELLED} · label-off=finisher-pr-40-b · finisher-pr-40-b=labelled`, // suffix order, never position
715
- ]) {
716
- assert.deepEqual(run({ rows: [row] }, open).unlabelled, [], row);
717
- }
718
- // A label-off resets the stretch: the attempt after it is the stretch's first.
719
- assert.deepEqual(run({ rows: [`${LABELLED} label-off=finisher-pr-40 · finisher-pr-40-b=labelled`] }, open).unlabelled,
720
- [{ pr: 40, labelled: ["finisher-pr-40-b"] }]);
721
- // A label-off for another PR's attempt leaves this one's miss standing, wherever it sits.
722
- assert.deepEqual(run({ rows: [`${LABELLED} label-off=finisher-pr-41`] }, open).unlabelled,
723
- [{ pr: 40, labelled: ["finisher-pr-40"] }]);
724
- // Two label-offs for one PR: the HIGHEST attempt named starts the stretch,
725
- // whichever token comes first on the row.
726
- const three = `${LABELLED} · finisher-pr-40-b=labelled · finisher-pr-40-c=labelled`;
727
- for (const offs of ["label-off=finisher-pr-40-b label-off=finisher-pr-40", "label-off=finisher-pr-40 label-off=finisher-pr-40-b"]) {
728
- assert.deepEqual(run({ rows: [`${three} ${offs}`] }, open).unlabelled, [{ pr: 40, labelled: ["finisher-pr-40-c"] }], offs);
729
- }
730
- });
731
-
732
- test("deriveRun: two labelled attempts in one stretch escalate rather than dispatch a third", () => {
733
- const open = [pr(40, ["minor"], [10])];
734
- const r = run({ rows: [`${LABELLED} · finisher-pr-40-b=labelled`] }, open);
735
- assert.deepEqual(r.unlabelled, [{ pr: 40, labelled: ["finisher-pr-40", "finisher-pr-40-b"] }]);
736
- assert.deepEqual(reconcile({ ...state(), ...r }).filter((x) => x.role === "reviewers").map((x) => x.action),
737
- ["ESCALATE unlabelled PR#40"]);
738
- // A halt between the two still leaves both labelled ones in the stretch.
739
- assert.deepEqual(run({ rows: [`${LABELLED} · finisher-pr-40-b=halted:live-editor · finisher-pr-40-c=labelled`] }, open).unlabelled,
740
- [{ pr: 40, labelled: ["finisher-pr-40", "finisher-pr-40-c"] }]);
741
- });
742
-
743
- test("deriveRun: a finisher token on another PR's row is a stray — it neither makes nor masks a miss", () => {
744
- const prs = [pr(40, ["minor"], [10]), pr(41, ["minor"], [11])];
745
- const r = run({ rows: ["#10 impl-10=PR#40 → PR#40", "#11 impl-11=PR#41 → PR#41 · finisher-pr-40=labelled"] }, prs);
746
- assert.deepEqual(r.unlabelled, []);
747
- const masked = run({ rows: [LABELLED, "#11 impl-11=PR#41 → PR#41 · finisher-pr-40-b"] }, prs);
748
- assert.deepEqual(masked.unlabelled, [{ pr: 40, labelled: ["finisher-pr-40"] }]);
749
- });
750
-
751
- test("deriveRun: a label-off naming no finisher-pr member refuses the ledger, naming the token", () => {
752
- for (const tok of ["label-off=impl-10", "label-off=", "label-off=finisher-pr-40=labelled", "label-off=merge-bot-1"]) {
753
- assert.throws(() => run({ rows: [`${LABELLED} ${tok}`] }, [pr(40, [], [10])]), (e) => e.message.includes(`'${tok}'`), tok);
754
- }
755
- });
756
- test("deriveRun: merge holds are held-behind rows whose premise PR is still open", () => {
757
- const r = run({
758
- rows: [
759
- "#10 impl-10=PR#40 → PR#40 · held-behind:#38",
760
- "#11 impl-11=PR#41 → PR#41 · held-behind:#39",
761
- ],
762
- }, [pr(38), pr(40, ["ready-to-merge"]), pr(41, ["ready-to-merge"])]);
763
- assert.equal(r.mergeQueue, 2);
764
- assert.equal(r.mergeHeld, 1, "#39 is no longer open, so #41's hold has lifted");
765
- });
766
-
767
- // #2064: merge-bot's `conflict-hold:#<pr>` — a conflict its local-rebase
768
- // fallback would not force — feeds the same fix-due list a review's
769
- // survivors do, and holds the merge until a fix-applier AFTER it settles.
770
- const HOLD_ROW = (n, tail) => `#${n - 30} impl-${n - 30}=PR#${n} → PR#${n} · reviewed=abc1234:0/2/0 · ${tail}`;
771
-
772
- test("deriveRun: a conflict hold is fix-due until a fix-applier after it lands, whatever settled before it", () => {
773
- const r = run({
774
- rows: [
775
- HOLD_ROW(40, "conflict-hold:#40"),
776
- HOLD_ROW(41, "conflict-hold:#41 · fix-pr-41=applied:def5678"),
777
- HOLD_ROW(42, "conflict-hold:#42 · fix-pr-42=no-op"),
778
- // A settle BEFORE the hold is stale: it cannot clear a newer conflict.
779
- HOLD_ROW(43, "fix-pr-43=applied:def5678 · conflict-hold:#43"),
780
- // Cleared once, then held again — the second hold is unresolved.
781
- HOLD_ROW(44, "conflict-hold:#44 · fix-pr-44=applied:def5678 · conflict-hold:#44"),
782
- // A fix-applier that died leaves the conflict where it was.
783
- HOLD_ROW(45, "conflict-hold:#45 · fix-pr-45=failed"),
784
- // A review after the cleared hold leaves the hold cleared.
785
- HOLD_ROW(46, "conflict-hold:#46 · fix-pr-46=applied:def5678 · review=wf:x reviewed=def5678:0/1/0"),
786
- // A closed PR is nobody's work.
787
- HOLD_ROW(47, "conflict-hold:#47"),
788
- ],
789
- }, [40, 41, 42, 43, 44, 45, 46].map((n) => pr(n)));
790
- assert.deepEqual(r.fixDue, [40, 43, 44, 45]);
791
- });
792
-
793
- test("deriveRun: a queued PR under a conflict hold is merge-held until its fix-applier SETTLES — dispatch alone does not lift it", () => {
794
- const at = (tail) => run({ rows: [HOLD_ROW(40, tail)] }, [pr(40, ["ready-to-merge"])]);
795
- const held = at("conflict-hold:#40");
796
- assert.deepEqual([held.fixDue, held.mergeQueue, held.mergeHeld, held.mergeConflictHeld], [[40], 1, 1, 1]);
797
- const working = at("conflict-hold:#40 · fix-pr-40");
798
- assert.deepEqual(working.fixDue, [], "a live fix-applier is not re-offered");
799
- assert.equal(working.mergeHeld, 1, "…and the merge bot does not re-select a PR still being fixed");
800
- const landed = at("conflict-hold:#40 · fix-pr-40=applied:def5678");
801
- assert.deepEqual([landed.fixDue, landed.mergeHeld, landed.mergeConflictHeld], [[], 0, 0]);
802
- // Both sources on one PR count it once.
803
- const both = run({ rows: [HOLD_ROW(40, "held-behind:#38 · conflict-hold:#40")] }, [pr(38), pr(40, ["ready-to-merge"])]);
804
- assert.equal(both.mergeHeld, 1);
805
- });
806
-
807
- test("deriveRun: a conflict hold with a live review= in flight is not re-offered either — the merge hold still counts it", () => {
808
- const r = run({ rows: ["#10 impl-10=PR#350 → PR#350 · conflict-hold:#350 review=wf:x"] }, [pr(350, ["ready-to-merge"])]);
809
- assert.deepEqual(r.fixDue, [], "a review already running claims the row the same as a live fix-applier does");
810
- assert.deepEqual([r.mergeQueue, r.mergeHeld, r.mergeConflictHeld], [1, 1, 1]);
811
- });
812
-
813
- test("deriveRun: a fix-applier settled in ## Dispatched clears a conflict hold even if a later row rewrite drops the =outcome suffix back to bare", () => {
814
- const r = run({
815
- rows: [HOLD_ROW(40, "conflict-hold:#40 · fix-pr-40")],
816
- dispatched: ["fix-pr-40=applied:def5678"],
817
- }, [pr(40, ["ready-to-merge"])]);
818
- assert.deepEqual([r.fixDue, r.mergeHeld, r.mergeConflictHeld], [[], 0, 0],
819
- "the member settled in ## Dispatched, not the row's own stale bare copy");
820
- });
821
-
822
- test("deriveRun: a redundant re-hold on an already-unresolved conflict does not re-offer the PR to a second fix-applier while the first is live", () => {
823
- const r = run({ rows: [HOLD_ROW(40, "conflict-hold:#40 · fix-pr-40 · conflict-hold:#40")] }, [pr(40, ["ready-to-merge"])]);
824
- assert.deepEqual(r.fixDue, [], "a live fix-applier working the first hold is not re-offered by a duplicate hold token");
825
- assert.deepEqual([r.mergeHeld, r.mergeConflictHeld], [1, 1], "still held — the duplicate hold changes nothing about the merge gate");
826
- });
827
-
828
- // #2328: a fix-applier dispatched on an unresolved hold is the conflict one —
829
- // it rebases and never reads the review file — so its landing clears the hold
830
- // and leaves a returned review's survivors exactly as unanswered as they were.
831
- test("deriveRun: a landed conflict fix-applier clears the hold but not the survivors — a review fix-applier follows", () => {
832
- const R = (tail) => `#20 impl-20=PR#21 → PR#21 · reviewed=abc1234:2/0/0 · ${tail}`;
833
- const at = (tail, dispatched = []) => run({ rows: [R(tail)], dispatched }, [pr(21, ["ready-to-merge"])]);
834
- const held = at("conflict-hold:#21");
835
- assert.deepEqual([held.fixDue, held.conflictHeld], [[21], [21]]);
836
- const working = at("conflict-hold:#21 · fix-pr-21");
837
- assert.deepEqual([working.fixDue, working.conflictHeld], [[], [21]], "the live conflict fix-applier is not joined by a review one");
838
- const landed = at("conflict-hold:#21 · fix-pr-21=applied:def5678");
839
- assert.deepEqual([landed.fixDue, landed.conflictHeld, landed.mergeHeld], [[21], [], 0], "the survivors are re-offered, the hold is gone");
840
- // The hold read before the review is the same conflict fix-applier.
841
- assert.deepEqual(run({ rows: ["#20 impl-20=PR#21 → PR#21 · conflict-hold:#21 · reviewed=abc1234:2/0/0 · fix-pr-21=no-op"] }, [pr(21)]).fixDue, [21]);
842
- // The review fix-applier that follows answers them.
843
- assert.deepEqual(at("conflict-hold:#21 · fix-pr-21=applied:def5678 · fix-pr-21-b=applied:0123abc").fixDue, []);
844
- assert.deepEqual(at("conflict-hold:#21 · fix-pr-21=applied:def5678 · fix-pr-21-b").fixDue, [], "…and is not re-offered while live");
845
- assert.deepEqual(at("conflict-hold:#21 · fix-pr-21=applied:def5678 · fix-pr-21-b=failed").fixDue, [21], "a dead one leaves them due");
846
- });
847
-
848
- test("deriveRun: survivors a review fix-applier already answered stay answered through a later hold and its fix", () => {
849
- const R = (tail) => `#20 impl-20=PR#21 → PR#21 · reviewed=abc1234:2/0/0 · fix-pr-21=applied:def5678 · ${tail}`;
850
- const held = run({ rows: [R("conflict-hold:#21")] }, [pr(21)]);
851
- assert.deepEqual([held.fixDue, held.conflictHeld], [[21], [21]], "the hold is due on its own");
852
- const cleared = run({ rows: [R("conflict-hold:#21 · fix-pr-21-b=applied:0123abc")] }, [pr(21)]);
853
- assert.deepEqual([cleared.fixDue, cleared.conflictHeld], [[], []], "nothing left: the survivors were answered before the hold");
854
- // A live review fix-applier when the hold lands is not joined by a conflict one.
855
- const live = run({ rows: ["#20 impl-20=PR#21 → PR#21 · reviewed=abc1234:2/0/0 · fix-pr-21 · conflict-hold:#21"] }, [pr(21)]);
856
- assert.deepEqual([live.fixDue, live.conflictHeld], [[], [21]]);
857
- });
858
-
859
- // A dispositions mismatch is answered by one automatic retry: the PR returns
860
- // to fix-due on the row that already dispatches fix-appliers, once, until the
861
- // retry has been checked. A second failure is escalate, and escalate is a hold
862
- // no fix-applier answers.
863
- const D = (fix, ...tokens) => `#20 impl-20=PR#21 → PR#21 · reviewed=abc1234:2/0/0 · ${[...fix, ...tokens].join(" · ")}`;
864
- const MISMATCH = "dispositions-mismatch=fix-pr-21:abc1234";
865
- const dueAt = (tail, prs = [pr(21)]) => run({ rows: [D(["fix-pr-21=applied:def5678"], ...tail)] }, prs).fixDue;
866
-
867
- test("deriveRun: a first dispositions mismatch puts the PR back in fixDue, and the reviewers row prints it", () => {
868
- const r = run({ rows: [D(["fix-pr-21=applied:def5678"], MISMATCH)] }, [pr(21)]);
869
- assert.deepEqual(r.fixDue, [21]);
870
- assert.equal(row(state({ fixDue: r.fixDue }), "reviewers").action, "DISPATCH fix-pr PR#21");
871
- // The survivors alone were answered: with an ok verdict the PR is not due.
872
- assert.deepEqual(dueAt(["dispositions-ok=fix-pr-21:abc1234"]), []);
873
- // No other role's row moves on the mismatch.
874
- const others = (fixDue) => reconcile(state({ fixDue })).filter((x) => x.role !== "reviewers");
875
- assert.deepEqual(others(r.fixDue), others([]));
876
- });
877
-
878
- test("deriveRun: a mismatch is due whether the review counted survivors or only unverified findings", () => {
879
- const unverifiedOnly = run({ rows: [`#20 impl-20=PR#21 → PR#21 · reviewed=abc1234:0/0/2 · fix-pr-21=applied:def5678 · ${MISMATCH}`] }, [pr(21)]);
880
- assert.deepEqual(unverifiedOnly.fixDue, [21]);
881
- });
882
-
883
- test("deriveRun: the retry is not re-offered while live, nor before its own check has run, nor after any verdict of its own", () => {
884
- assert.deepEqual(dueAt([MISMATCH, "fix-pr-21-b"]), [], "live");
885
- assert.deepEqual(dueAt([MISMATCH, "fix-pr-21-b=applied:0123abc"]), [], "settled, its check not yet run: the earlier mismatch is not the retry's answer");
886
- assert.deepEqual(dueAt([MISMATCH, "fix-pr-21-b=applied:0123abc", "dispositions-ok=fix-pr-21-b:abc1234"]), [], "ok ends it");
887
- assert.deepEqual(dueAt([MISMATCH, "fix-pr-21-b=applied:0123abc", "dispositions-escalate=fix-pr-21-b:abc1234"]), [], "escalate is never due");
888
- assert.deepEqual(dueAt([MISMATCH, "fix-pr-21-b=failed"]), [21], "a retry that died leaves the mismatch for a replacement");
889
- });
890
-
891
- test("deriveRun: a landed conflict fix-applier does not answer a mismatch — the retry is still due", () => {
892
- assert.deepEqual(dueAt([MISMATCH, "conflict-hold:#21", "fix-pr-21-b"]), [], "the conflict one is live: nothing is re-offered");
893
- assert.deepEqual(dueAt([MISMATCH, "conflict-hold:#21", "fix-pr-21-b=applied:0123abc"]), [21], "hold cleared by -b, the mismatch still stands");
894
- assert.deepEqual(dueAt([MISMATCH, "fix-pr-21-b=applied:0123abc"]), [], "control: a review -b that landed is awaiting its own check");
895
- });
896
-
897
- test("deriveRun: a dispositions verdict on a row that never recorded a review head answers nothing", () => {
898
- const r = run({ rows: [`#20 impl-20=PR#21 → PR#21 · review=wf:x · fix-pr-21=applied:def5678 · ${MISMATCH}`] }, [pr(21)]);
899
- assert.deepEqual(r.fixDue, []);
900
- });
901
-
902
- test("deriveRun: the verdict is read by review head and highest retry, never by position in the row", () => {
903
- const escalated = ["dispositions-escalate=fix-pr-21-b:abc1234", MISMATCH];
904
- assert.deepEqual(dueAt(["fix-pr-21-b=applied:0123abc", ...escalated]), [], "the -b escalate answers, wherever it sits");
905
- // A mismatch on an older review does not answer a newer one.
906
- const newer = run({ rows: [`#20 impl-20=PR#21 → PR#21 · reviewed=abc1234:2/0/0 · fix-pr-21=applied:def5678 · ${MISMATCH} · review=wf:x reviewed=fedcba9:0/1/0`] }, [pr(21)]);
907
- assert.deepEqual(newer.fixDue, [], "the new review has no verdict and no survivors");
908
- const rev = run({ rows: [`#20 impl-20=PR#21 → PR#21 · reviewed=abc1234:2/0/0 · fix-pr-21=applied:def5678 · ${MISMATCH} · review=wf:x`] }, [pr(21)]);
909
- assert.deepEqual(rev.fixDue, [], "a review running on the PR holds it off");
910
- assert.deepEqual(dueAt([MISMATCH], []), [], "a PR no longer open is nobody's work");
911
- });
912
-
913
- test("currentDispositions: escalate parses, and outranks mismatch which outranks ok at one retry suffix", () => {
914
- assert.deepEqual(dispositionsToken("dispositions-escalate=fix-pr-21-b:abc1234")?.verdict, "escalate");
915
- assert.equal(dispositionsToken("dispositions-escalate=finisher-pr-21:abc1234"), null);
916
- const heads = (...s) => currentDispositions(s.map(dispositionsToken), "abc1234");
917
- assert.deepEqual(heads("dispositions-ok=fix-pr-21:abc1234", "dispositions-escalate=fix-pr-21:abc1234"), { verdict: "escalate", member: "fix-pr-21" });
918
- assert.deepEqual(heads("dispositions-escalate=fix-pr-21:abc1234", "dispositions-mismatch=fix-pr-21:abc1234"), { verdict: "escalate", member: "fix-pr-21" });
919
- assert.deepEqual(heads("dispositions-escalate=fix-pr-21:abc1234", "dispositions-ok=fix-pr-21-b:abc1234"), { verdict: "ok", member: "fix-pr-21-b" });
920
- });
921
-
922
- test("deriveRun: a newer review's survivors are not offered beside a live fix-applier, and are due once it settles", () => {
923
- // `dispatch` would refuse a second live one. Settled, it was dispatched
924
- // before that review, so it never answered the newer survivors.
925
- const rereviewed = (fix) => run({ rows: [`#20 impl-20=PR#21 → PR#21 · reviewed=abc1234:2/0/0 · ${fix} · review=wf:x reviewed=def5678:1/0/0`] }, [pr(21)]).fixDue;
926
- assert.deepEqual(rereviewed("fix-pr-21"), []);
927
- assert.deepEqual(rereviewed("fix-pr-21=applied:def5678"), [21]);
928
- assert.deepEqual(rereviewed("fix-pr-21=failed"), [21]);
929
- });
930
-
931
- // One fix-applier does one job, so its landing folds once per PR however many
932
- // copies of its token the rows carry — a whole-line `row` rewrite leaves bare
933
- // copies wherever it chose, and the in-place settled copy is the one that says
934
- // where it was dispatched.
935
- test("deriveRun: duplicate copies of one fix-applier's token land it once — a conflict fix-applier never also answers the survivors", () => {
936
- const R = "#20 impl-20=PR#21 → PR#21 · reviewed=abc1234:2/0/0";
937
- const F = "fix-pr-21=applied:def5678";
938
- const D = [F];
939
- const due = (rows, dispatched = []) => {
940
- const r = run({ rows, dispatched }, [pr(21)]);
941
- return [r.fixDue, r.conflictHeld];
942
- };
943
- // A bare copy on the PR's own row, after the hold-clearing settled one.
944
- assert.deepEqual(due([`${R} · conflict-hold:#21 · ${F}`, "#21 fix-pr-21"]), [[21], []]);
945
- assert.deepEqual(due([`${R} · conflict-hold:#21 · ${F}`, "#21 fix-pr-21"], D), [[21], []]);
946
- // The settled copy twice on one row.
947
- assert.deepEqual(due([`${R} · conflict-hold:#21 · ${F} · ${F}`]), [[21], []]);
948
- // A stale bare copy BEFORE the hold, the in-place settled one after it:
949
- // the settled one cleared the hold, whatever `## Dispatched` says.
950
- assert.deepEqual(due([`${R} · fix-pr-21 · conflict-hold:#21`, `#21 ${F}`]), [[21], []]);
951
- assert.deepEqual(due([`${R} · fix-pr-21 · conflict-hold:#21`, `#21 ${F}`], D), [[21], []]);
952
- // A review fix-applier that landed before a hold: a stale bare copy of it
953
- // after the hold does not clear that hold.
954
- assert.deepEqual(due([`${R} · ${F} · conflict-hold:#21 · fix-pr-21`], D), [[21], [21]]);
955
- // Liveness is read off the member once every row is read: a bare copy on an
956
- // earlier row of one a later row settled `failed` is dead, not live.
957
- assert.deepEqual(due([`${R} · fix-pr-21`, "#21 fix-pr-21=failed"]), [[21], []]);
958
- // Must accept: a DIFFERENT member after the conflict one still answers them.
959
- assert.deepEqual(due([`${R} · conflict-hold:#21 · ${F} · fix-pr-21-b=no-op`, "#21 fix-pr-21"]), [[], []]);
960
- });
961
-
962
- // #2329: a fix-applier answers the PR its token names, like the finisher-pr
963
- // rule beside it. `dispatch`/`settle` only ever put `fix-pr-<M>` on PR #M's
964
- // row, so a foreign one there is a hand-written stray — it must read as if
965
- // absent, never clear that PR's hold, answer its survivors or hold it off.
966
- test("deriveRun: a fix-pr token for another PR leaves this PR's hold and survivors exactly as if it were absent", () => {
967
- const H = "#20 impl-20=PR#21 · conflict-hold:#21";
968
- const due = (rows, dispatched = [], prs = [pr(21)]) => {
969
- const r = run({ rows, dispatched }, prs);
970
- return [r.fixDue, r.conflictHeld];
971
- };
972
- assert.deepEqual(due([H]), [[21], [21]], "control: the hold stands and is due");
973
- assert.deepEqual(due([`${H} · fix-pr-99=applied:def5678`]), [[21], [21]], "a foreign landed fix-applier does not clear the hold");
974
- assert.deepEqual(due([`${H} · fix-pr-99-b=no-op`]), [[21], [21]], "…a retry-suffixed one neither");
975
- assert.deepEqual(due([`${H} · fix-pr-99`]), [[21], [21]], "a foreign live fix-applier does not hold off this PR's own");
976
- // The real fix-pr-99 settled elsewhere: a stray bare copy on #21's row
977
- // inherits nothing from it.
978
- assert.deepEqual(due([`${H} · fix-pr-99`], ["fix-pr-99=applied:def5678"]), [[21], [21]]);
979
- assert.deepEqual(due([`${H} · fix-pr-99`, "#98 impl-98=PR#99 · fix-pr-99=applied:def5678"], [], [pr(21), pr(99)]), [[21], [21]]);
980
- // Nor does a settled stray on #21's row stand in for PR #99's own in-place
981
- // copy: #99's bare token, the only one on #99's row, still lands its member.
982
- assert.deepEqual(
983
- due(["#20 impl-20=PR#21 · fix-pr-99=applied:def5678", "#98 impl-98=PR#99 · conflict-hold:#99 · fix-pr-99"],
984
- ["fix-pr-99=applied:def5678"], [pr(21), pr(99)]),
985
- [[], []], "a foreign settled copy does not keep the owner's bare copy from landing");
986
- // …and lends it no outcome: a settled stray of fix-pr-21 on #99's row, read
987
- // before #21's own row, does not land #21's bare copy and clear its hold.
988
- assert.deepEqual(run({ rows: ["#98 impl-98=PR#99 · fix-pr-21=applied:abc1234", `${H} · fix-pr-21`] }, [pr(21), pr(99)]).conflictHeld,
989
- [21], "a foreign settled copy does not clear the owner's hold");
990
- // A row naming no PR at all (no `#<n>` key, no `PR#`) is no PR's row, so a
991
- // settled copy there is a stray as well: the owner's bare copy still lands.
992
- assert.deepEqual(due(["notes fix-pr-21=applied:def5678", `${H} · fix-pr-21`], ["fix-pr-21=applied:def5678"]),
993
- [[], []], "a settled copy on a PR-less row does not keep the owner's bare copy from landing");
994
- // A row's PR is its `PR#` mention even where its key names the token's
995
- // number: `fix-pr-21` on `#21 …=PR#22` is a stray on PR #22's row, neither
996
- // clearing #22's hold nor standing in for PR #21's own copies.
997
- const K = "#21 impl-21=PR#22 · conflict-hold:#22";
998
- assert.deepEqual(due([`${K} · fix-pr-21=applied:def5678`], [], [pr(22)]), [[22], [22]], "the key does not make it #22's own");
999
- assert.deepEqual(due([`${K} · fix-pr-21=applied:def5678`, `${H} · fix-pr-21`], ["fix-pr-21=applied:def5678"], [pr(21), pr(22)]),
1000
- [[22], [22]], "…nor keep PR #21's bare copy from landing");
1001
- // Nor does a foreign one answer a returned review's survivors.
1002
- assert.deepEqual(due(["#20 impl-20=PR#21 · reviewed=abc1234:2/0/0 · fix-pr-99=applied:def5678"]), [[21], []]);
1003
- // Must accept: the PR's own fix-applier, live and landed.
1004
- assert.deepEqual(due([`${H} · fix-pr-21`]), [[], [21]], "own live: not re-offered, still held");
1005
- assert.deepEqual(due([`${H} · fix-pr-21=applied:def5678`]), [[], []], "own landed: the hold is cleared");
1006
- // …and on the PR's own `#21` row, which names no `PR#` — its key is its PR.
1007
- assert.deepEqual(due([H, "#21 fix-pr-21=applied:def5678"]), [[], []]);
1008
- // An in-place settled copy on the ticket row, whose key is the ticket's and
1009
- // whose PR is its `PR#` mention, is the PR's own: it still says where its
1010
- // member landed, so a stale bare copy read first does not land it instead.
1011
- assert.deepEqual(
1012
- due(["#21 fix-pr-21", "#20 impl-20=PR#21 · reviewed=abc1234:2/0/0 · conflict-hold:#21 · fix-pr-21=applied:def5678"],
1013
- ["fix-pr-21=applied:def5678"]),
1014
- [[21], []], "own in-place copy on the ticket row: it cleared the hold and answered no survivor");
1015
- });
1016
-
1017
- // #2391: the other direction of #2329 — a settled stray reaching its OWNER
1018
- // through the member record `fixRunning`/`fixLive` (and a finisher's
1019
- // `past-pin` read) share. A PR-bound member's outcome counts off `##
1020
- // Dispatched` and its own PR's row only, whichever order the rows sit in.
1021
- test("deriveRun: a settled stray of a PR-bound member on another PR's row does not settle its owner's live one", () => {
1022
- const own = "#20 impl-20=PR#21 · conflict-hold:#21 · fix-pr-21";
1023
- const stray = "#98 impl-98=PR#99 · fix-pr-21=applied:abc1234";
1024
- const seen = (rows, dispatched = []) => {
1025
- const r = run({ rows, dispatched }, [pr(21), pr(99)]);
1026
- return [r.fixDue, r.fixLive, r.reviewed.flatMap((x) => x.fixLive)];
1027
- };
1028
- for (const rows of [[own, stray], [stray, own], [own]]) {
1029
- const [due, n] = seen(rows);
1030
- assert.deepEqual([due, n], [[], 1], `fix-pr-21 is still working #21: ${JSON.stringify(rows)}`);
1031
- }
1032
- // The same reading through `reviewed.fixLive`, which gates the finisher.
1033
- const reviewed = "#20 impl-20=PR#21 · reviewed=abc1234:0/0/0 · fix-pr-21";
1034
- assert.deepEqual(seen([stray, reviewed])[2], ["fix-pr-21"]);
1035
- // Must accept: the member's real settle — in `## Dispatched` or on its own
1036
- // PR's row — still settles it, and a stray's own PR is unaffected.
1037
- assert.deepEqual(seen([own], ["fix-pr-21=applied:abc1234"]).slice(0, 2), [[], 0], "settled in ## Dispatched");
1038
- assert.deepEqual(seen(["#20 impl-20=PR#21 · conflict-hold:#21 · fix-pr-21=failed"]).slice(0, 2), [[21], 0], "settled on its own row");
1039
- // A stray's token still counts as a live member when nothing settles it.
1040
- assert.equal(seen(["#98 impl-98=PR#99 · fix-pr-21"])[1], 1);
1041
-
1042
- // A finisher's `halted:past-pin` read follows the same rule: a settled stray
1043
- // on PR #99's row must not read PR #21's live finisher as halted past-pin,
1044
- // wherever the rows sit.
1045
- const fin = "#20 impl-20=PR#21 · reviewed=abc1234:0/0/0 · finisher-pr-21";
1046
- const finStray = "#98 impl-98=PR#99 · finisher-pr-21=halted:past-pin";
1047
- for (const rows of [[fin, finStray], [finStray, fin], [fin]]) {
1048
- assert.deepEqual(run({ rows }, [pr(21), pr(99)]).reviewDue, [99], `live finisher, no halt: ${JSON.stringify(rows)}`);
1049
- }
1050
- assert.deepEqual(
1051
- run({ rows: ["#20 impl-20=PR#21 · reviewed=abc1234:0/0/0 · finisher-pr-21=halted:past-pin"] }, [pr(21)]).reviewDue,
1052
- [21], "own-row halted:past-pin still re-offers the review");
1053
- });
1054
-
1055
- test("deriveRun: a conflict hold is read in either spelling and on a PR-keyed row", () => {
1056
- assert.deepEqual(run({ rows: [HOLD_ROW(40, "conflict-hold-#40")] }, [pr(40)]).fixDue, [40]);
1057
- assert.deepEqual(run({ rows: ["#350 review=wf:x reviewed=abc1234:0/1/0 · conflict-hold:#350"] }, [pr(350)]).fixDue, [350]);
1058
- // `held-behind` rows and prose that merely mentions a conflict are not holds.
1059
- const prose = run({ rows: [HOLD_ROW(40, "(conflict vs #38, resolved by rebase-pr-40) merge-conflict-resolved:ef82065a")] }, [pr(40, ["ready-to-merge"])]);
1060
- assert.deepEqual([prose.fixDue, prose.mergeHeld], [[], 0]);
1061
- });
1062
-
1063
- test("deriveRun: the merge bot is live while its ## Dispatched entry is unsettled", () => {
1064
- assert.equal(run({ dispatched: ["merge-bot-1"] }).mergeBotLive, 1);
1065
- assert.equal(run({ dispatched: ["merge-bot-1=done"] }).mergeBotLive, 0);
1066
- assert.equal(run({ dispatched: ["merge-bot-1=done", "merge-bot-2=killed", "merge-bot-3"] }).mergeBotLive, 1);
1067
- });
1068
-
1069
- test("deriveRun: drain and tier mismatches come off the file", () => {
1070
- const r = run({
1071
- rows: ["#7 impl-7=tier-mismatch", "#8 impl-8=tier-mismatch · impl-8-b"],
1072
- dispatched: ["impl-7=tier-mismatch", "impl-8=tier-mismatch", "impl-8-b"],
1073
- drain: "maintainer asked",
1074
- });
1075
- assert.equal(r.draining, "maintainer asked");
1076
- assert.deepEqual(r.tierMismatch, ["impl-7"], "#8's replacement is the fix");
1077
- });
1078
-
1079
- // #2485: a mismatch on a ticket whose issue is CLOSED holds nothing.
1080
- test("deriveRun: a tier mismatch on a closed ticket stops holding; one on an open ticket still does", () => {
1081
- const ledger = { rows: ["#7 impl-7=tier-mismatch", "#8 impl-8 · tier-mismatch=impl-8:fleet-implementer"], dispatched: ["impl-7=tier-mismatch", "impl-8"] };
1082
- assert.deepEqual(run(ledger).tierMismatch, ["impl-7", "impl-8"], "no probe answer: every mismatch holds");
1083
- const r = run(ledger, [], new Set([7]));
1084
- assert.deepEqual(r.tierMismatch, ["impl-8"], "#7 is closed, #8 is not");
1085
- assert.deepEqual(r.tierUnchecked, [], "a lifted mismatch does not fall through to unchecked");
1086
- assert.deepEqual(run(ledger, [], new Set([7, 8])).tierMismatch, []);
1087
- });
1088
-
1089
- test("deriveRun: a closed ticket lifts only its own mismatch, not a replacement's on another ticket", () => {
1090
- const r = run({ rows: ["#7 impl-7=tier-mismatch · impl-7-b=tier-mismatch", "#9 impl-9=tier-mismatch"] }, [], new Set([9]));
1091
- assert.deepEqual(r.tierMismatch, ["impl-7-b"]);
1092
- });
1093
-
1094
- test("deriveRun: tier mismatch holds while the LATEST replacement is also tier-mismatch", () => {
1095
- const r = run({
1096
- rows: ["#8 impl-8=tier-mismatch · impl-8-b=tier-mismatch"],
1097
- dispatched: ["impl-8=tier-mismatch", "impl-8-b=tier-mismatch"],
1098
- });
1099
- assert.deepEqual(r.tierMismatch, ["impl-8-b"], "a replacement that is ALSO tier-mismatch keeps the row held");
1100
- });
1101
-
1102
- // #1398: tier-check.mjs writes `tier-ok=<member>:<definition>` on a pass;
1103
- // the newest implementer of a ticket with no verdict holds as unchecked,
1104
- // live or settled alike.
1105
- test("deriveRun: the newest implementer with no tier verdict is unchecked, live or settled; tier-ok clears it", () => {
1106
- assert.deepEqual(run({ rows: ["#7 impl-7 · class=routine"] }).tierUnchecked, ["impl-7"]);
1107
- assert.deepEqual(run({ rows: ["#7 impl-7=bailed"] }).tierUnchecked, ["impl-7"], "a settled member is still owed its check");
1108
- const ok = run({ rows: ["#7 impl-7 · class=routine · tier-ok=impl-7:fleet-implementer"] });
1109
- assert.deepEqual([ok.tierUnchecked, ok.tierMismatch], [[], []]);
1110
- // A verdict names ITS member: another member's tier-ok clears nothing.
1111
- assert.deepEqual(run({ rows: ["#7 impl-7 · tier-ok=impl-17:fleet-implementer"] }).tierUnchecked, ["impl-7"]);
1112
- });
1113
-
1114
- test("deriveRun: a settled tier-mismatch keeps its own hold and is never also unchecked", () => {
1115
- const r = run({ rows: ["#7 impl-7=tier-mismatch"], dispatched: ["impl-7=tier-mismatch"] });
1116
- assert.deepEqual([r.tierMismatch, r.tierUnchecked], [["impl-7"], []]);
1117
- // A member settled some other way before its check found the mismatch
1118
- // carries the verdict as a row token instead.
1119
- const token = run({ rows: ["#7 impl-7=bailed · tier-mismatch=impl-7:fleet-implementer"] });
1120
- assert.deepEqual([token.tierMismatch, token.tierUnchecked], [["impl-7"], []]);
1121
- });
1122
-
1123
- test("deriveRun: tier-unverifiable is a verdict for its own member — it clears unchecked, never reads as a mismatch, and a mismatch still outranks it", () => {
1124
- const r = run({ rows: ["#7 impl-7=killed · tier-unverifiable=impl-7:no-transcript"] });
1125
- assert.deepEqual([r.tierUnchecked, r.tierMismatch], [[], []]);
1126
- const both = run({ rows: ["#7 impl-7=tier-mismatch · tier-unverifiable=impl-7:no-transcript"] });
1127
- assert.deepEqual([both.tierMismatch, both.tierUnchecked], [["impl-7"], []]);
1128
- assert.deepEqual(run({ rows: ["#7 impl-7=killed · tier-unverifiable=impl-17:no-transcript"] }).tierUnchecked, ["impl-7"]);
1129
- });
1130
-
1131
- test("deriveRun: only the newest member of a ticket counts toward the tier holds", () => {
1132
- const replaced = run({ rows: ["#8 impl-8=tier-mismatch · impl-8-b"], dispatched: ["impl-8=tier-mismatch", "impl-8-b"] });
1133
- assert.deepEqual([replaced.tierMismatch, replaced.tierUnchecked], [[], ["impl-8-b"]], "the replacement is what is owed a check now");
1134
- const checked = run({ rows: ["#8 impl-8=tier-mismatch · impl-8-b · tier-ok=impl-8-b:fleet-implementer"] });
1135
- assert.deepEqual([checked.tierMismatch, checked.tierUnchecked], [[], []]);
1136
- // The predecessor's own pass does not stand in for its replacement's.
1137
- const stale = run({ rows: ["#8 impl-8=killed · tier-ok=impl-8:fleet-implementer · impl-8-b"] });
1138
- assert.deepEqual(stale.tierUnchecked, ["impl-8-b"]);
1139
- // Nor does a predecessor killed before it was ever checked hold the row
1140
- // once its replacement has passed.
1141
- assert.deepEqual(run({ rows: ["#8 impl-8=killed · impl-8-b · tier-ok=impl-8-b:fleet-implementer"] }).tierUnchecked, []);
1142
- });
1143
-
1144
- // PR #2302 review: the replacement climbs past the HIGHEST letter the ticket
1145
- // has used, not the held member's own — `-c` dispatched before `-b` left the
1146
- // tick naming `impl-7-c`, which ledger.mjs refuses as already dispatched.
1147
- test("deriveRun → implementers: a mismatch's replacement skips every retry letter the ticket already used", () => {
1148
- const step = (rows) => {
1149
- const d = run({ rows });
1150
- return row(state({ tierMismatch: d.tierMismatch, implNames: d.implNames }), "implementers").detail.split(" — ")[1];
1151
- };
1152
- assert.equal(step(["#7 impl-7=killed · impl-7-c=killed · impl-7-b=tier-mismatch"]), "dispatch impl-7-d at its row's tier");
1153
- assert.equal(step(["#7 impl-7-z=killed · impl-7-c=tier-mismatch"]), "impl-7-c has no retry letter left");
1154
- // Another ticket's letters are not this one's.
1155
- assert.equal(step(["#7 impl-7=tier-mismatch", "#8 impl-8-c=killed"]), "dispatch impl-7-b at its row's tier");
1156
- });
1157
-
1158
- test("deriveRun: excluded rows claim their ticket while their premise still holds, and carry their premises", () => {
1159
- const r = run({ rows: ["#50 excluded · behind-pr:#44", "#51 excluded · behind-issue:#9 behind-pr:feat/x"] }, [pr(44)]);
1160
- assert.ok(r.claimed.has(50) && r.claimed.has(51));
1161
- assert.deepEqual(r.excluded, [
1162
- { n: 50, premises: [{ kind: "pr", target: "44" }] },
1163
- { n: 51, premises: [{ kind: "issue", target: "9" }, { kind: "pr", target: "feat/x" }] },
1164
- ]);
1165
- });
1166
-
1167
- test("deriveRun: a behind-pr premise no longer open lifts the claim, but the row is still reported excluded", () => {
1168
- const r = run({ rows: ["#50 excluded · behind-pr:#44"] }, []);
1169
- assert.equal(r.claimed.has(50), false, "#44 is not in the open list — the premise has lifted");
1170
- assert.deepEqual(r.excluded, [{ n: 50, premises: [{ kind: "pr", target: "44" }] }]);
1171
- });
1172
-
1173
- test("deriveRun: one still-open premise among several keeps the claim, even if another has lifted", () => {
1174
- const r = run({ rows: ["#50 excluded · behind-pr:#44 behind-pr:#45"] }, [pr(45)]);
1175
- assert.ok(r.claimed.has(50), "#45 is still open — #44 alone lifting is not enough");
1176
- });
1177
-
1178
- test("deriveRun: the row text it does not own is accepted as it stands", () => {
1179
- // The accept side of every refusal below: freeform row text, the arrow, the
1180
- // board's own tokens and an empty ledger are the ordinary case.
1181
- assert.doesNotThrow(() => run({
1182
- rows: ["#324 impl-324=PR#346 → PR#346 · fix-pr-346=no-op · ruled:6-applies · ci=123:1:success · ports=16324 · tier=task-high"],
1183
- dispatched: ["impl-324=PR#346", "fix-pr-346=no-op"],
1184
- }, [pr(346)]));
1185
- const empty = run({});
1186
- assert.equal(empty.implLive + empty.reviewsLive + empty.fixLive + empty.mergeBotLive, 0);
1187
- });
1188
-
1189
- test("deriveRun: a token it cannot read refuses by naming it, never counts it live or gone", () => {
1190
- for (const [what, ledger, why] of [
1191
- ["an outcome outside the vocabulary", { rows: ["#9 impl-9=merged"] }, /impl-9.*'merged' is not an outcome/],
1192
- ["a malformed ## Dispatched entry", { dispatched: ["impl-9 oops"] }, /## Dispatched entry 'impl-9 oops'/],
1193
- ["a reviewed= with no counts", { rows: ["#9 review=wf:a reviewed=abc1234"] }, /reviewed=abc1234/],
1194
- ["a review= of no known kind", { rows: ["#9 review=bogus"] }, /review=bogus/],
1195
- ["a conflict hold naming another PR", { rows: ["#10 impl-10=PR#40 → PR#40 · conflict-hold:#38"] }, /conflict-hold:#38.*PR #40/],
1196
- ["a conflict hold on a row with no PR mention or PR-keyed impl token at all", { rows: ["conflict-hold:#40"] }, /no PR's/],
1197
- ]) {
1198
- assert.throws(() => run(ledger), why, what);
1199
- }
1200
- });
1201
-
1202
- test("unclaimed: shortlist entries with no impl- or excluded row, in the file's order", () => {
1203
- const r = run({ rows: ["#2 impl-2", "#4 excluded · behind-pr:#1", "#5 impl-5=bailed"] }, [pr(1)]);
1204
- assert.deepEqual(unclaimed([1, 2, 3, 4, 5, 6].map((n) => ({ n, t: `t${n}` })), r), [1, 3, 6]);
1205
- });
1206
-
1207
- test("unclaimed: a lifted behind-pr exclusion no longer claims its ticket, once it reaches the heads", () => {
1208
- const r = run({ rows: ["#4 excluded · behind-pr:#1"] }, []);
1209
- assert.deepEqual(unclaimed([1, 2, 3, 4].map((n) => ({ n, t: `t${n}` })), r), [1, 2, 3, 4]);
1210
- });
1211
-
1212
- test("unclaimed: a behind-issue exclusion still claims its ticket while absent from entries", () => {
1213
- const r = run({ rows: ["#9 excluded · behind-issue:#5"] });
1214
- assert.deepEqual(unclaimed([1, 2, 3].map((n) => ({ n, t: `t${n}` })), r), [1, 2, 3]);
1215
- });
1216
-
1217
- test("unclaimed: a behind-issue exclusion's ticket reappearing in entries is admitted — the ledger row is stale, the scan is not", () => {
1218
- const r = run({ rows: ["#9 excluded · behind-issue:#5"] });
1219
- assert.deepEqual(unclaimed([1, 9].map((n) => ({ n, t: `t${n}` })), r), [1, 9]);
1220
- });
1221
-
1222
- test("unclaimed: a still-open behind-pr exclusion blocks its ticket even if it appears in entries", () => {
1223
- const r = run({ rows: ["#9 excluded · behind-pr:#44"] }, [pr(44)]);
1224
- assert.deepEqual(unclaimed([1, 9].map((n) => ({ n, t: `t${n}` })), r), [1]);
1225
- });
1226
-
1227
- test("parseShortlist: shortlist.mjs's payload reads; anything else is unparsable, never a refusal", () => {
1228
- assert.deepEqual(parseShortlist('{"scanned":3,"shortlist":[{"n":7,"t":"a"}]}'),
1229
- { status: "ok", scanned: 3, entries: [{ n: 7, t: "a" }] });
1230
- for (const bad of ["", "{", "[]", '{"shortlist":[]}', '{"scanned":1,"shortlist":{}}', '{"scanned":1,"shortlist":[{"n":"7","t":"a"}]}',
1231
- '{"scanned":1,"shortlist":[{"n":0,"t":"a"}]}', '{"scanned":1,"shortlist":[{"n":-1,"t":"a"}]}']) {
1232
- assert.deepEqual(parseShortlist(bad), { status: "unparsable", scanned: null, entries: [] }, bad);
1233
- }
1234
- });
1235
-
1236
- test("refreshWhy: short of the cap, missing or empty refreshes; draining never does", () => {
1237
- const base = { draining: null, status: "ok", entries: 5, unclaimed: 2, implCap: 2 };
1238
- assert.equal(refreshWhy(base), null);
1239
- assert.equal(refreshWhy({ ...base, unclaimed: 1 }), "unclaimed 1 < cap 2");
1240
- assert.equal(refreshWhy({ ...base, status: "missing", entries: 0, unclaimed: 0 }), "shortlist missing");
1241
- assert.equal(refreshWhy({ ...base, status: "unparsable", entries: 0, unclaimed: 0 }), "shortlist unparsable");
1242
- assert.equal(refreshWhy({ ...base, entries: 0, unclaimed: 0, implCap: 0 }), "shortlist empty");
1243
- assert.equal(refreshWhy({ ...base, status: "missing", draining: "x" }), null);
1244
- });
1245
-
1246
- // ---------------------------------------------------------------------------
1247
- // The CLI half. Every case runs a COPY of the script from a stub directory,
1248
- // inside its own throwaway git repository, so the ledger and shortlist it
1249
- // reads are the case's own `.fleet/` files, resolved the way the real run
1250
- // resolves them — against the git common dir.
1251
-
1252
- import { spawnSync } from "node:child_process";
1253
- import { mkdtempSync, mkdirSync, writeFileSync, chmodSync, rmSync, readFileSync, realpathSync, existsSync } from "node:fs";
1254
- import { writeExecStub } from "./exec-stub.mjs";
1255
- import { tmpdir } from "node:os";
1256
- import { join } from "node:path";
1257
- import { fileURLToPath } from "node:url";
1258
-
1259
- const SCRIPT = fileURLToPath(new URL("./fleet-tick.mjs", import.meta.url));
1260
- // Every non-builtin module the copied script needs at startup, direct import
1261
- // or transitive — and ledger.mjs with its own, because the tick reads the
1262
- // ledger through `ledger.mjs read`. An unlisted sibling is a module-not-found
1263
- // at startup: exit 1, a shape no case below expects.
1264
- const SIBLING_MODULES = ["arg.mjs", "fleet-state.mjs", "git-env.mjs", "is-cli.mjs", "ledger.mjs", "ledger-grammar.mjs", "main-checkout.mjs"].map(
1265
- (m) => [m, fileURLToPath(new URL(`./${m}`, import.meta.url))],
1266
- );
1267
-
1268
- // Every subprocess below is spawned synchronously, so a hung one blocks this
1269
- // file's event loop: no per-test or runner timeout can fire, and only the
1270
- // runner's wall-clock kill ends it, as a cancelled file that reads like a hang
1271
- // somewhere else (#2320). Every spawn goes through `spawnBounded`, which kills
1272
- // the child at the bound and fails its own test naming the timeout and the
1273
- // command. The bound is per spawn and generous on purpose: #2320's triage
1274
- // measured the slowest whole case at ~1.5s solo on an idle host and ~3.85x
1275
- // that under the full suite, but on a host loaded by other agents the slowest
1276
- // case has taken ~13s solo, and a case can run several spawns.
1277
- const SPAWN_TIMEOUT_MS = 60_000;
1278
- const spawnBounded = (cmd, args, opts = {}) => {
1279
- const r = spawnSync(cmd, args, { encoding: "utf8", ...opts, timeout: SPAWN_TIMEOUT_MS });
1280
- if (r.error) {
1281
- const what = [cmd, ...args].join(" ");
1282
- throw new Error(r.error.code === "ETIMEDOUT"
1283
- ? `timed out after ${SPAWN_TIMEOUT_MS}ms, killed with ${r.signal}: ${what}`
1284
- : `could not run ${what}: ${r.error.message}`);
1285
- }
1286
- return r;
1287
- };
1288
-
1289
- // The bound holds only while every spawn takes it: a raw synchronous spawn
1290
- // added anywhere else in this file brings #2320's unbounded hang back, and
1291
- // every case still passes until the day that child hangs. So this reads the
1292
- // file's own source and names any `spawnSync`/`execFileSync`/`execSync` call
1293
- // outside the helper. A comment spelling one of them with its call paren fails
1294
- // it too (the loud direction); an import renamed with `as` walks past it.
1295
- test("CLI: every synchronous spawn in this file goes through spawnBounded (#2320)", () => {
1296
- const src = readFileSync(fileURLToPath(import.meta.url), "utf8");
1297
- const start = src.indexOf("const spawnBounded = ");
1298
- assert.ok(start > 0, "the spawnBounded helper is gone; this sweep has nothing to measure against");
1299
- const end = src.indexOf("\n};\n", start);
1300
- const lineOf = (i) => src.slice(0, i).split("\n").length;
1301
- const raw = [...src.matchAll(/\b(?:spawnSync|execFileSync|execSync)\s*\(/g)]
1302
- .filter((m) => m.index < start || m.index > end)
1303
- .map((m) => `line ${lineOf(m.index)}: ${m[0]}`);
1304
- assert.deepEqual(raw, []);
1305
- });
1306
-
1307
- // `pr list` for the open PRs, `issue view` for a behind-issue premise's state,
1308
- // `pr view` for the state of a PR an in-flight review names that is no longer
1309
- // on the open list, `issue list --label in-progress` for the stall report's
1310
- // claimed count. `GH_HANG` names one of those (`pr list`, `issue view`, …) to
1311
- // stall for `GH_HANG_S` seconds before answering as usual, so a tick that waits
1312
- // a hang out still reads a normal reply. The sleep holds none of the stub's
1313
- // pipes: killed at the tick's bound, the stub leaves no child keeping the
1314
- // tick's read open.
1315
- const GH_STUB = `#!/bin/sh
1316
- [ -n "$GH_HANG" ] && [ "$1 $2" = "$GH_HANG" ] && sleep "$GH_HANG_S" </dev/null >/dev/null 2>&1
1317
- case "$1 $2" in
1318
- "pr list") [ -n "$PR_FAIL" ] && { echo "boom" >&2; exit 1; }; cat "$FIXTURE_PRS" ;;
1319
- "issue view")
1320
- echo "$3" >> "$ISSUE_VIEW_LOG"
1321
- [ -n "$ISSUE_VIEW_FAIL" ] && { echo "gh: issue view failed" >&2; exit 1; }
1322
- [ -n "$ISSUE_VIEW_BODY" ] && { printf '%s\n' "$ISSUE_VIEW_BODY"; exit 0; }
1323
- # Real gh answers for the repository an inherited GIT_DIR or GH_REPO names;
1324
- # this one answers CLOSED for every issue there, so a probe that forgot to
1325
- # scrub them lifts an exclusion the case's own repository still holds.
1326
- [ -n "$GIT_DIR$GIT_WORK_TREE$GH_REPO" ] && { echo '{"state":"CLOSED"}'; exit 0; }
1327
- exec jq -c --arg n "$3" '{state: (.[$n] // error("no such issue"))}' "$FIXTURE_ISSUE_STATES" ;;
1328
- "pr view")
1329
- echo "$3" >> "$PR_VIEW_LOG"
1330
- [ -n "$PR_VIEW_FAIL" ] && { echo "gh: pr view failed" >&2; exit 1; }
1331
- [ -n "$PR_VIEW_BODY" ] && { printf '%s\n' "$PR_VIEW_BODY"; exit 0; }
1332
- # Real gh answers for the repository an inherited GIT_DIR or GH_REPO names;
1333
- # this one answers MERGED for every PR there, so a probe that forgot to
1334
- # scrub them retires a review the case's own repository still holds.
1335
- [ -n "$GIT_DIR$GIT_WORK_TREE$GH_REPO" ] && { echo '{"state":"MERGED"}'; exit 0; }
1336
- exec jq -c --arg n "$3" '{state: (.[$n] // error("no such pull request"))}' "$FIXTURE_PR_STATES" ;;
1337
- "issue list")
1338
- [ -n "$CLAIMED_FAIL" ] && { echo "boom" >&2; exit 1; }
1339
- expr=""
1340
- while [ $# -gt 0 ]; do
1341
- case "$1" in --jq) shift; expr="$1" ;; esac
1342
- shift
1343
- done
1344
- exec jq -c "$expr" "$FIXTURE_CLAIMED" ;;
1345
- *) echo "unexpected gh $*" >&2; exit 1 ;;
1346
- esac
1347
- `;
1348
-
1349
- // Stands in for shortlist.mjs, whose own suite is shortlist.test.mjs: logs
1350
- // that it ran, then either fails or writes the case's refreshed payload to the
1351
- // file the real one writes, and prints it with \`file\` the way the real one does.
1352
- const SHORTLIST_STUB = `import { writeFileSync, mkdirSync, appendFileSync, readFileSync } from "node:fs";
1353
- import { join } from "node:path";
1354
- appendFileSync(process.env.REFRESH_LOG, "ran\\n");
1355
- if (process.env.REFRESH_FAIL) { console.error("shortlist: could not answer"); process.exit(2); }
1356
- const payload = readFileSync(process.env.FIXTURE_REFRESH, "utf8");
1357
- const file = join(process.cwd(), ".fleet", "shortlist.json");
1358
- mkdirSync(join(process.cwd(), ".fleet"), { recursive: true });
1359
- writeFileSync(file, payload);
1360
- console.log(JSON.stringify({ file, ...JSON.parse(payload) }));
1361
- `;
1362
-
1363
- // The ledger on disk, in the layout ledger.mjs's save() writes.
1364
- const ledgerText = ({ rows = [], dispatched = [], drain = null } = {}) => {
1365
- const list = (xs) => xs.map((x) => `- ${x}`).join("\n");
1366
- return `# Fleet run ledger\n\n## Rows\n\n${list(rows)}\n\n## Dispatched\n\n${list(dispatched)}\n\n## Filed\n\n\n\n## Ruled\n\n`
1367
- + (drain === null ? "" : `\n\n## Drain\n\n- ${drain}`) + "\n";
1368
- };
1369
- const shortlistText = (ns, scanned = ns.length) => JSON.stringify({ scanned, shortlist: ns.map((n) => ({ n, t: `t${n}` })) });
1370
-
1371
- // `baseline` records the run's main-checkout baseline the way Phase 0 does,
1372
- // after `beforeRun`; `afterBaseline` is a stray write landing mid-run. The
1373
- // repo ignores `.fleet/` and `.worktrees/` as a fleet repo's .gitignore does,
1374
- // through info/exclude so no tracked file says so.
1375
- function runCli(args = [], {
1376
- prs = [], ledger, shortlist, refresh = shortlistText([]), refreshFail = false,
1377
- issueStates = {}, prStates = {}, claimed = [], env: extraEnv = {}, defaultState = false, keep = false, beforeRun = () => {},
1378
- baseline = true, afterBaseline = () => {},
1379
- } = {}) {
1380
- // On macOS tmpdir() is /var -> /private/var; resolving it up front makes
1381
- // every path below the resolved one. (A symlinked path no longer stops a
1382
- // copy running its own main(): is-cli.mjs compares by realpath.)
1383
- const dir = realpathSync(mkdtempSync(join(tmpdir(), "fleet-tick-")));
1384
- // Everything from here to the return can throw: a failed `git init` or
1385
- // `--record`, or spawnBounded's timeout (#2320). The dir goes whenever that
1386
- // happens, `keep` or not — a throw hands the caller no `r.dir` to remove.
1387
- // `keep` only spares it on a normal return.
1388
- let returned = false;
1389
- try {
1390
- const bin = join(dir, "bin");
1391
- const repo = join(dir, "repo");
1392
- mkdirSync(bin);
1393
- mkdirSync(join(repo, ".fleet"), { recursive: true });
1394
- assert.equal(spawnBounded("git", ["init", "-q", repo]).status, 0);
1395
- writeFileSync(join(repo, ".git", "info", "exclude"), ".fleet/\n.worktrees/\n");
1396
- writeExecStub(join(bin, "gh"), GH_STUB);
1397
- const script = join(bin, "fleet-tick.mjs");
1398
- writeFileSync(script, readFileSync(SCRIPT));
1399
- for (const [name, path] of SIBLING_MODULES) writeFileSync(join(bin, name), readFileSync(path));
1400
- writeFileSync(join(bin, "shortlist.mjs"), SHORTLIST_STUB);
1401
- const fx = (name, content) => { const p = join(dir, name); writeFileSync(p, content); return p; };
1402
- if (ledger !== undefined) writeFileSync(join(repo, ".fleet", "ledger.md"), ledgerText(ledger));
1403
- if (shortlist !== undefined) writeFileSync(join(repo, ".fleet", "shortlist.json"), shortlist);
1404
- beforeRun(repo);
1405
- if (baseline) {
1406
- const rec = spawnBounded(process.execPath, [join(bin, "main-checkout.mjs"), "--record"], { cwd: repo });
1407
- assert.equal(rec.status, 0, rec.stderr);
1408
- }
1409
- afterBaseline(repo);
1410
- const refreshLog = fx("refresh.log", "");
1411
- const issueViewLog = fx("issue-view.log", "");
1412
- const prViewLog = fx("pr-view.log", "");
1413
- // Every case gets its own state file unless it names one: the default path
1414
- // resolves against the git common dir, and a shared streak and digest would
1415
- // make the fold cases order-dependent. `defaultState` opts out for the case
1416
- // whose subject IS that resolution.
1417
- const stateArg = args.includes("--state") || defaultState ? [] : ["--state", join(dir, "heartbeat.json")];
1418
- const r = spawnBounded(process.execPath, [script, ...args, ...stateArg], {
1419
- cwd: repo,
1420
- env: {
1421
- ...process.env, PATH: `${bin}:${process.env.PATH}`,
1422
- FIXTURE_PRS: fx("prs.json", JSON.stringify(prs)),
1423
- FIXTURE_CLAIMED: fx("claimed.json", JSON.stringify(claimed)),
1424
- FIXTURE_ISSUE_STATES: fx("issue-states.json", JSON.stringify(issueStates)),
1425
- FIXTURE_PR_STATES: fx("pr-states.json", JSON.stringify(prStates)),
1426
- FIXTURE_REFRESH: fx("refresh.json", refresh),
1427
- REFRESH_LOG: refreshLog, ISSUE_VIEW_LOG: issueViewLog, PR_VIEW_LOG: prViewLog,
1428
- ...(refreshFail ? { REFRESH_FAIL: "1" } : {}),
1429
- ...extraEnv,
1430
- },
1431
- });
1432
- r.refreshed = readFileSync(refreshLog, "utf8").split("\n").filter(Boolean).length;
1433
- r.issueViews = readFileSync(issueViewLog, "utf8").split("\n").filter(Boolean);
1434
- r.prViews = readFileSync(prViewLog, "utf8").split("\n").filter(Boolean);
1435
- r.repo = repo;
1436
- r.dir = dir;
1437
- returned = true;
1438
- return r;
1439
- } finally {
1440
- if (!returned || !keep) rmSync(dir, { recursive: true, force: true });
1441
- }
1442
- }
1443
- const lineOf = (r, role) => r.stdout.split("\n").filter((l) => l.startsWith(role));
1444
-
1445
- // A throw inside runCli (a failed `git init`/`--record` assert, spawnBounded's
1446
- // timeout) used to leave its `fleet-tick-*` fixture in the tmpdir.
1447
- // `beforeRun` runs after the dir exists, so it stands in for any such throw.
1448
- for (const keep of [false, true]) {
1449
- test(`CLI: runCli removes its fixture dir when it throws mid-helper, keep: ${keep}`, () => {
1450
- let dir;
1451
- assert.throws(() => runCli([], { keep, beforeRun: (repo) => { dir = join(repo, ".."); throw new Error("boom"); } }), /boom/);
1452
- assert.ok(dir, "beforeRun never ran, so the throw happened before the dir existed");
1453
- assert.equal(existsSync(dir), false);
1454
- });
1455
- }
1456
-
1457
- test("CLI: runCli with keep: true hands back a fixture dir that still exists", () => {
1458
- const r = runCli([], { keep: true });
1459
- try {
1460
- assert.equal(existsSync(r.dir), true);
1461
- } finally {
1462
- rmSync(r.dir, { recursive: true, force: true });
1463
- }
1464
- assert.equal(existsSync(runCli([]).dir), false, "without keep the dir is gone on return");
1465
- });
1466
-
1467
- test("CLI: the router row reads the run's .fleet/cost-guard.json, and prints DEFAULT-ONLY without one", () => {
1468
- const none = runCli([], { shortlist: shortlistText([]) });
1469
- assert.equal(none.status, 0, none.stderr);
1470
- assert.deepEqual(lineOf(none, "router"), ["router → DEFAULT-ONLY (cost-guard.json missing — run pr-cost.mjs --guard)"]);
1471
- const ok = runCli([], {
1472
- shortlist: shortlistText([]),
1473
- beforeRun: (repo) => writeFileSync(join(repo, ".fleet", "cost-guard.json"), guardJson({ verdict: "none", baseline: { ...BASE, n: 12 } })),
1474
- });
1475
- assert.equal(ok.status, 0, ok.stderr);
1476
- assert.deepEqual(lineOf(ok, "router"), ["router → NO VERDICT (baseline n=12/20; guard computed 2026-10-03T00:00:00.000Z)"]);
1477
- });
1478
-
1479
- test("CLI: the #1692 shape, read off the ledger — two live implementers at cap 2 pull nothing", () => {
1480
- const r = runCli([], {
1481
- ledger: { rows: ["#412 impl-412 · tier-ok=impl-412:fleet-implementer", "#415 impl-415 · tier-ok=impl-415:fleet-implementer"], dispatched: ["impl-412", "impl-415"] },
1482
- shortlist: shortlistText([412, 415, 420, 421, 422]),
1483
- });
1484
- assert.equal(r.status, 0, r.stderr);
1485
- assert.match(r.stdout, /^implementers 2\/2 → AT CAP/m);
1486
- assert.doesNotMatch(r.stdout, /PULL/);
1487
- assert.equal(r.refreshed, 0, "three unclaimed at cap 2 is not short of the cap");
1488
-
1489
- // One settles: exactly one slot frees, and the next unclaimed head fills it.
1490
- const one = runCli([], {
1491
- ledger: { rows: ["#412 impl-412=PR#500 → PR#500 · tier-ok=impl-412:fleet-implementer", "#415 impl-415 · tier-ok=impl-415:fleet-implementer"], dispatched: ["impl-412=PR#500", "impl-415"] },
1492
- shortlist: shortlistText([412, 415, 420, 421, 422]),
1493
- });
1494
- assert.equal(one.status, 0, one.stderr);
1495
- assert.match(one.stdout, /^implementers 1\/2 → PULL #420 {3}\(/m);
1496
- });
1497
-
1498
- test("CLI: a fresh run with no ledger reads zero live members, and no shortlist is depth 0 plus a refresh", () => {
1499
- const r = runCli([], { refresh: shortlistText([7, 8, 9], 12) });
1500
- assert.equal(r.status, 0, r.stderr);
1501
- assert.equal(r.refreshed, 1);
1502
- // Pulled from the file the refresh just wrote — not a tick of waiting.
1503
- assert.match(r.stdout, /^implementers 0\/2 → PULL #7 #8 {3}\(unclaimed=3 supply=12 unreviewed=0 shortlist=missing\)/m);
1504
- assert.match(r.stdout, /^shortlist {4}→ REFRESHED shortlist: 3 entries; 0 lifted {3}\(shortlist missing\)$/m);
1505
- });
1506
-
1507
- test("CLI: an unparsable shortlist is depth 0 plus a refresh, never a refusal", () => {
1508
- const r = runCli([], { shortlist: "{not json", refresh: shortlistText([]) });
1509
- assert.equal(r.status, 0, r.stderr);
1510
- assert.equal(r.refreshed, 1);
1511
- assert.match(r.stdout, /^implementers 0\/2 → SUGGEST \/triage, hold idle {3}\(unclaimed=0 supply=0 unreviewed=0 shortlist=unparsable\)/m);
1512
- assert.match(r.stdout, /REFRESHED shortlist: 0 entries; 0 lifted {3}\(shortlist unparsable; unchanged\)/);
1513
- });
1514
-
1515
- test("CLI: a permission-denied shortlist reads as unreadable, not unparsable — the fault is the filesystem's, not the JSON's", () => {
1516
- const r = runCli([], {
1517
- shortlist: shortlistText([1]), refresh: shortlistText([]),
1518
- beforeRun: (repo) => chmodSync(join(repo, ".fleet", "shortlist.json"), 0o000),
1519
- });
1520
- assert.equal(r.status, 0, r.stderr);
1521
- assert.equal(r.refreshed, 1, "unreadable, like unparsable, is a refresh trigger — never a refusal");
1522
- assert.match(r.stdout, /shortlist=unreadable/, "EACCES must not be mislabeled as malformed JSON");
1523
- assert.doesNotMatch(r.stdout, /shortlist=unparsable/);
1524
- });
1525
-
1526
- test("CLI: a failed refresh prints REFRESH FAILED and every other row, at exit 0", () => {
1527
- const r = runCli([], { refreshFail: true, prs: [pr(60, ["ready-to-merge"])] });
1528
- assert.equal(r.status, 0, r.stderr);
1529
- assert.match(r.stdout, /^shortlist {4}→ REFRESH FAILED {3}\(shortlist missing — shortlist\.mjs exited 2: shortlist: could not answer\)$/m);
1530
- assert.match(r.stdout, /^merge-bot {4}0\/1 → DISPATCH merge-bot/m);
1531
- });
1532
-
1533
- test("CLI: PULL names unclaimed heads only — claimed, bailed and excluded tickets are skipped", () => {
1534
- const r = runCli(["--implementer-cap", "3"], {
1535
- ledger: { rows: ["#1 impl-1=bailed · tier-ok=impl-1:fleet-implementer", "#2 impl-2 · tier-ok=impl-2:fleet-implementer", "#3 excluded · behind-pr:#99"], dispatched: ["impl-1=bailed", "impl-2"] },
1536
- shortlist: shortlistText([1, 2, 3, 4, 5, 6]),
1537
- prs: [pr(99)],
1538
- });
1539
- assert.equal(r.status, 0, r.stderr);
1540
- assert.match(r.stdout, /^implementers 1\/3 → PULL #4 #5 /m);
1541
- assert.equal(r.refreshed, 0, "three unclaimed at cap 3 is not short");
1542
- });
1543
-
1544
- test("CLI: reviewer rows are named per PR, fix-appliers first, reviews oldest first", () => {
1545
- const r = runCli([], {
1546
- ledger: {
1547
- rows: [
1548
- "#10 impl-10=PR#346 → PR#346 · review=wf:a reviewed=abc1234:2/0/0",
1549
- "#11 impl-11=PR#350 → PR#350",
1550
- "#12 impl-12=PR#349 → PR#349",
1551
- ],
1552
- dispatched: ["impl-10=PR#346", "impl-11=PR#350", "impl-12=PR#349"],
1553
- },
1554
- shortlist: shortlistText([]),
1555
- prs: [pr(350), pr(349), pr(346)],
1556
- });
1557
- assert.equal(r.status, 0, r.stderr);
1558
- assert.deepEqual(lineOf(r, "reviewers").map((l) => l.replace(/ {3}\(.*$/, "")), [
1559
- "reviewers 0/6 → DISPATCH fix-pr PR#346",
1560
- "reviewers 0/6 → DISPATCH review PR#349 PR#350",
1561
- ]);
1562
- });
1563
-
1564
- test("CLI: --max-reviews bounds the reviews one tick starts", () => {
1565
- const r = runCli(["--max-reviews", "1"], { shortlist: shortlistText([]), prs: [pr(51), pr(52), pr(53)] });
1566
- assert.equal(r.status, 0, r.stderr);
1567
- assert.match(r.stdout, /^reviewers {4}0\/6 → DISPATCH review PR#51 {3}\(.*max-reviews=1\)$/m);
1568
- const busy = runCli(["--max-reviews", "1"], {
1569
- ledger: { rows: ["#51 review=wf:x"] }, shortlist: shortlistText([]), prs: [pr(51), pr(52)],
1570
- });
1571
- assert.match(busy.stdout, /^reviewers {4}1\/6 → HOLD \(max-reviews 1 in flight\)/m);
1572
- // Default = the reviewer cap.
1573
- const open = runCli(["--reviewer-cap", "2"], { shortlist: shortlistText([]), prs: [pr(51), pr(52), pr(53)] });
1574
- assert.match(open.stdout, /^reviewers {4}0\/2 → DISPATCH review PR#51 PR#52 /m);
1575
- });
1576
-
1577
- test("CLI: the narrowed backlog gate — a deep backlog with free reviewer slots still pulls", () => {
1578
- const r = runCli([], { shortlist: shortlistText([7, 8]), prs: [pr(51), pr(52), pr(53)] });
1579
- assert.equal(r.status, 0, r.stderr);
1580
- assert.match(r.stdout, /^implementers 0\/2 → PULL #7 #8 /m);
1581
- const saturated = runCli(["--reviewer-cap", "1"], {
1582
- ledger: { rows: ["#51 review=wf:x"] }, shortlist: shortlistText([7, 8]), prs: [pr(51), pr(52)],
1583
- });
1584
- assert.match(saturated.stdout, /^implementers 0\/2 → HOLD \(review side saturated\)/m);
1585
- });
1586
-
1587
- test("CLI: drain holds the implementer row, stops refreshing, and keeps the review and merge rows firing", () => {
1588
- const r = runCli([], {
1589
- ledger: { rows: ["#9 impl-9=released"], dispatched: ["impl-9=released"], drain: "maintainer asked" },
1590
- prs: [pr(51), pr(60, ["ready-to-merge"])],
1591
- });
1592
- assert.equal(r.status, 0, r.stderr);
1593
- assert.match(r.stdout, /^implementers 0\/2 → HOLD \(draining\)/m);
1594
- assert.equal(r.refreshed, 0, "a draining run's missing shortlist is not a reason to restart supply");
1595
- assert.doesNotMatch(r.stdout, /REFRESH/);
1596
- assert.match(r.stdout, /^reviewers {4}0\/6 → DISPATCH review PR#51 /m);
1597
- assert.match(r.stdout, /^merge-bot {4}0\/1 → DISPATCH merge-bot /m);
1598
- });
1599
-
1600
- test("CLI: merge-bot liveness and holds come off the ledger", () => {
1601
- const prs = [pr(38), pr(40, ["ready-to-merge"])];
1602
- const live = runCli([], { ledger: { dispatched: ["merge-bot-1"] }, shortlist: shortlistText([]), prs });
1603
- assert.match(live.stdout, /^merge-bot {4}1\/1 → AT CAP/m);
1604
- const held = runCli([], {
1605
- ledger: { rows: ["#10 impl-10=PR#40 → PR#40 · held-behind:#38"], dispatched: ["merge-bot-1=done"] },
1606
- shortlist: shortlistText([]), prs,
1607
- });
1608
- assert.match(held.stdout, /^merge-bot {4}0\/1 → HOLD {3}\(merge-queue=1 held=1/m);
1609
- const lifted = runCli([], {
1610
- ledger: { rows: ["#10 impl-10=PR#40 → PR#40 · held-behind:#38"], dispatched: ["merge-bot-1=done"] },
1611
- shortlist: shortlistText([]), prs: [pr(40, ["ready-to-merge"])],
1612
- });
1613
- assert.match(lifted.stdout, /^merge-bot {4}0\/1 → DISPATCH merge-bot {3}\(merge-queue=1 held=0\)/m);
1614
- });
1615
-
1616
- test("CLI: a merge hold accepts both the held-behind:#M and held-behind-#M spellings", () => {
1617
- const colon = runCli([], {
1618
- ledger: { rows: ["#10 impl-10=PR#40 → PR#40 · held-behind:#38"], dispatched: ["merge-bot-1=done"] },
1619
- shortlist: shortlistText([]), prs: [pr(38), pr(40, ["ready-to-merge"])],
1620
- });
1621
- assert.match(colon.stdout, /^merge-bot {4}0\/1 → HOLD {3}\(merge-queue=1 held=1/m);
1622
- const hyphen = runCli([], {
1623
- ledger: { rows: ["#11 impl-11=PR#41 → PR#41 · held-behind-#38"], dispatched: ["merge-bot-1=done"] },
1624
- shortlist: shortlistText([]), prs: [pr(38), pr(41, ["ready-to-merge"])],
1625
- });
1626
- assert.match(hyphen.stdout, /^merge-bot {4}0\/1 → HOLD {3}\(merge-queue=1 held=1/m,
1627
- "run-merge-bot.md's own documented report format (#38) must be recognized too");
1628
- });
1629
-
1630
- test("CLI: a conflict hold prints DISPATCH fix-pr and holds the merge bot until the fix-applier settles", () => {
1631
- const tick = (tail, dispatched) => runCli([], {
1632
- ledger: { rows: [`#10 impl-10=PR#40 → PR#40 · reviewed=abc1234:0/2/0 · ${tail}`], dispatched: ["merge-bot-1=done", ...dispatched] },
1633
- shortlist: shortlistText([]), prs: [pr(40, ["ready-to-merge"])],
1634
- }).stdout;
1635
- const held = tick("conflict-hold:#40", []);
1636
- assert.match(held, /^reviewers +0\/6 → DISPATCH fix-pr PR#40 /m);
1637
- assert.match(held, /^merge-bot {4}0\/1 → HOLD {3}\(merge-queue=1 held=1 — every queued candidate is held behind a lower PR or on a merge conflict no fix-pr has cleared\)$/m);
1638
- const working = tick("conflict-hold:#40 · fix-pr-40", ["fix-pr-40"]);
1639
- assert.doesNotMatch(working, /DISPATCH fix-pr/);
1640
- assert.match(working, /^merge-bot {4}0\/1 → HOLD {3}\(merge-queue=1 held=1/m);
1641
- const landed = tick("conflict-hold:#40 · fix-pr-40=applied:def5678", ["fix-pr-40=applied:def5678"]);
1642
- assert.doesNotMatch(landed, /DISPATCH fix-pr/);
1643
- assert.match(landed, /^merge-bot {4}0\/1 → DISPATCH merge-bot {3}\(merge-queue=1 held=0\)$/m);
1644
- });
1645
-
1646
- test("CLI: a ledger token outside the grammar refuses the tick, printing no row", () => {
1647
- const r = runCli([], { ledger: { rows: ["#9 impl-9=merged"] }, shortlist: shortlistText([]) });
1648
- assert.equal(r.status, 2);
1649
- assert.equal(r.stdout.trim(), "");
1650
- assert.match(r.stderr, /^fleet-tick: .*impl-9.*'merged' is not an outcome/m);
1651
- });
1652
-
1653
- test("CLI: a ledger ledger.mjs itself refuses is a refusal here too", (t) => {
1654
- // Two drain markers: ledger.mjs read exits 2 on the broken invariant.
1655
- const dir = runCli([], { shortlist: shortlistText([]), keep: true });
1656
- t.after(() => rmSync(dir.dir, { recursive: true, force: true }));
1657
- writeFileSync(join(dir.repo, ".fleet", "ledger.md"), `${ledgerText({ drain: "a" })}\n- b\n`);
1658
- const r = spawnBounded(process.execPath, [join(dir.dir, "bin", "fleet-tick.mjs"), "--state", join(dir.dir, "hb.json")], {
1659
- cwd: dir.repo,
1660
- env: { ...process.env, PATH: `${join(dir.dir, "bin")}:${process.env.PATH}`, FIXTURE_PRS: join(dir.dir, "prs.json") },
1661
- });
1662
- assert.equal(r.status, 2, r.stderr);
1663
- assert.equal(r.stdout.trim(), "");
1664
- assert.match(r.stderr, /fleet-tick: ledger\.mjs read exited 2/);
1665
- });
1666
-
1667
- test("CLI: a tier mismatch holds the implementer row until a replacement is dispatched", () => {
1668
- const r = runCli([], { ledger: { rows: ["#7 impl-7=tier-mismatch"], dispatched: ["impl-7=tier-mismatch"] }, shortlist: shortlistText([8, 9]) });
1669
- assert.match(r.stdout, /^implementers 0\/2 → HOLD \(tier mismatch impl-7\) +\(.* — dispatch impl-7-b at its row's tier\)$/m);
1670
- });
1671
-
1672
- test("CLI: an implementer with no tier verdict on the ledger holds the implementer row until tier-check writes one", () => {
1673
- const r = runCli([], { ledger: { rows: ["#7 impl-7 · class=routine"], dispatched: ["impl-7"] }, shortlist: shortlistText([8, 9]) });
1674
- assert.match(r.stdout, /^implementers 1\/2 → HOLD \(tier unchecked impl-7\) +\(.* — run ~\/\.fleet\/bin\/fleet-run tier-check\.mjs --batch <file> with \[\{"member":"impl-7","session":"<session>"\}\]\)$/m);
1675
- const ok = runCli([], {
1676
- ledger: { rows: ["#7 impl-7 · class=routine · tier-ok=impl-7:fleet-implementer"], dispatched: ["impl-7"] },
1677
- shortlist: shortlistText([8, 9]),
1678
- });
1679
- assert.match(ok.stdout, /^implementers 1\/2 → PULL #8 /m);
1680
- });
1681
-
1682
- test("CLI: a lifted exclusion premise triggers a refresh even with the shortlist full", () => {
1683
- const full = { shortlist: shortlistText([1, 2, 3]), refresh: shortlistText([1, 2, 3, 50]) };
1684
- const standing = runCli([], { ...full, ledger: { rows: ["#50 excluded · behind-pr:#44"] }, prs: [pr(44)] });
1685
- assert.equal(standing.refreshed, 0, "#44 is still open — the exclusion stands");
1686
- const lifted = runCli([], { ...full, ledger: { rows: ["#50 excluded · behind-pr:#44"] }, prs: [] });
1687
- assert.equal(lifted.refreshed, 1);
1688
- assert.match(lifted.stdout, /REFRESHED shortlist: 4 entries; 1 lifted {3}\(behind-pr:#44 no longer open\)/);
1689
- const issue = runCli([], { ...full, ledger: { rows: ["#50 excluded · behind-issue:#9"] }, issueStates: { 9: "CLOSED" } });
1690
- assert.equal(issue.refreshed, 1);
1691
- assert.deepEqual(issue.issueViews, ["9"]);
1692
- const openIssue = runCli([], { ...full, ledger: { rows: ["#50 excluded · behind-issue:#9"] }, issueStates: { 9: "OPEN" } });
1693
- assert.equal(openIssue.refreshed, 0);
1694
- // Already back on the current file: the refresh that lifted it has run.
1695
- const reflected = runCli([], {
1696
- shortlist: shortlistText([1, 2, 50]), ledger: { rows: ["#50 excluded · behind-issue:#9"] }, issueStates: { 9: "CLOSED" },
1697
- });
1698
- assert.equal(reflected.refreshed, 0);
1699
- assert.deepEqual(reflected.issueViews, [], "no probe for a premise the file already reflects");
1700
- });
1701
-
1702
- test("CLI: a lifted behind-pr exclusion names its ticket in the PULL row, with no refresh needed", () => {
1703
- const r = runCli(["--implementer-cap", "1"], {
1704
- shortlist: shortlistText([50]), ledger: { rows: ["#50 excluded · behind-pr:#44"] }, prs: [],
1705
- });
1706
- assert.equal(r.status, 0, r.stderr);
1707
- assert.equal(r.refreshed, 0, "the open-PR list already proves #44 is gone — no refresh needed to know it");
1708
- assert.match(r.stdout, /^implementers 0\/1 → PULL #50 /m);
1709
- });
1710
-
1711
- test("CLI: a behind-issue exclusion already reflected in the current file names its ticket in the PULL row", () => {
1712
- // The confirmed live bug (#1803 review): a prior tick already refreshed and
1713
- // printed "lifted", so the file has #50 back — but the ledger's `excluded`
1714
- // row is untouched until a Pull rewrites it. This tick gets no refresh
1715
- // trigger at all (the file already carries #50), so the fix has to live in
1716
- // how a claim from that stale row is read, not in another gh call.
1717
- const r = runCli(["--implementer-cap", "3"], {
1718
- shortlist: shortlistText([1, 2, 50]), ledger: { rows: ["#50 excluded · behind-issue:#9"] }, issueStates: { 9: "CLOSED" },
1719
- });
1720
- assert.equal(r.status, 0, r.stderr);
1721
- assert.equal(r.refreshed, 0, "already reflected — no refresh needed to admit it");
1722
- assert.match(r.stdout, /^implementers 0\/3 → PULL #1 #2 #50 /m);
1723
- });
1724
-
1725
- // #2485: the mismatch hold is lifted by the mismatched ticket's own issue
1726
- // being CLOSED, probed live; an open one, or a probe that cannot answer, holds.
1727
- test("CLI: a tier mismatch on a CLOSED ticket no longer holds the implementer row", () => {
1728
- const mismatch = { rows: ["#7 impl-7=tier-mismatch"], dispatched: ["impl-7=tier-mismatch"] };
1729
- const base = { shortlist: shortlistText([1, 2, 3]) };
1730
- const closed = runCli([], { ...base, ledger: mismatch, issueStates: { 7: "CLOSED" } });
1731
- assert.equal(closed.status, 0, closed.stderr);
1732
- assert.match(closed.stdout, /^implementers 0\/2 → PULL #1 #2 /m);
1733
- assert.doesNotMatch(closed.stdout, /tier mismatch/);
1734
- assert.deepEqual(closed.issueViews, ["7"]);
1735
- const open = runCli([], { ...base, ledger: mismatch, issueStates: { 7: "OPEN" } });
1736
- assert.match(open.stdout, /^implementers 0\/2 → HOLD \(tier mismatch impl-7\)/m);
1737
- const none = runCli([], { ...base, ledger: { rows: ["#7 impl-7 · tier-ok=impl-7:fleet-implementer"], dispatched: ["impl-7"] } });
1738
- assert.deepEqual(none.issueViews, [], "nothing mismatched: no probe");
1739
- });
1740
-
1741
- test("CLI: a failed probe of a mismatched ticket is disclosed and the hold stands", () => {
1742
- const r = runCli([], {
1743
- shortlist: shortlistText([1, 2, 3]), ledger: { rows: ["#7 impl-7=tier-mismatch"], dispatched: ["impl-7=tier-mismatch"] },
1744
- issueStates: { 7: "CLOSED" }, env: { ISSUE_VIEW_FAIL: "1" },
1745
- });
1746
- assert.equal(r.status, 0, r.stderr);
1747
- assert.match(r.stdout, /^implementers 0\/2 → HOLD \(tier mismatch impl-7\)/m);
1748
- assert.match(r.stderr, /fleet-tick: gh issue view 7 exited 1: gh: issue view failed — tier mismatch on #7 unconfirmed closed, hold stands/);
1749
- });
1750
-
1751
- test("CLI: a probe answering exit 0 with no issue state is disclosed and the hold stands", () => {
1752
- for (const body of ["<html>rate limited</html>", "{}", "null"]) {
1753
- const r = runCli([], {
1754
- shortlist: shortlistText([1, 2, 3]), ledger: { rows: ["#7 impl-7=tier-mismatch"], dispatched: ["impl-7=tier-mismatch"] },
1755
- issueStates: { 7: "CLOSED" }, env: { ISSUE_VIEW_BODY: body },
1756
- });
1757
- assert.equal(r.status, 0, r.stderr);
1758
- assert.match(r.stdout, /^implementers 0\/2 → HOLD \(tier mismatch impl-7\)/m, body);
1759
- assert.match(r.stderr, /fleet-tick: gh issue view 7 printed no state — tier mismatch on #7 unconfirmed closed, hold stands/, body);
1760
- }
1761
- });
1762
-
1763
- // Only the exact string CLOSED lifts the hold: another casing, a prefix, or a
1764
- // state an issue never has (MERGED) is a state, but not a closed one.
1765
- test("CLI: a state other than exactly CLOSED leaves the mismatch hold standing", () => {
1766
- for (const state of ["closed", "REOPENED", "MERGED", "CLOSED_AS_DUPLICATE"]) {
1767
- const r = runCli([], {
1768
- shortlist: shortlistText([1, 2, 3]), ledger: { rows: ["#7 impl-7=tier-mismatch"], dispatched: ["impl-7=tier-mismatch"] },
1769
- issueStates: { 7: "CLOSED" }, env: { ISSUE_VIEW_BODY: JSON.stringify({ state }) },
1770
- });
1771
- assert.equal(r.status, 0, r.stderr);
1772
- assert.match(r.stdout, /^implementers 0\/2 → HOLD \(tier mismatch impl-7\)/m, state);
1773
- }
1774
- });
1775
-
1776
- // Every mismatched ticket is probed on its own: one CLOSED does not lift, and
1777
- // one failed or OPEN does not hold back, another.
1778
- test("CLI: each mismatched ticket is probed and released or held on its own issue", () => {
1779
- const two = { rows: ["#7 impl-7=tier-mismatch", "#8 impl-8=tier-mismatch"], dispatched: ["impl-7=tier-mismatch", "impl-8=tier-mismatch"] };
1780
- const base = { shortlist: shortlistText([1, 2, 3]), ledger: two };
1781
- const mixed = runCli([], { ...base, issueStates: { 7: "CLOSED", 8: "OPEN" } });
1782
- assert.equal(mixed.status, 0, mixed.stderr);
1783
- assert.deepEqual(mixed.issueViews, ["7", "8"]);
1784
- assert.match(mixed.stdout, /HOLD \(tier mismatch impl-8\)/);
1785
- assert.doesNotMatch(mixed.stdout, /impl-7/);
1786
- const reversed = runCli([], { ...base, issueStates: { 7: "OPEN", 8: "CLOSED" } });
1787
- assert.deepEqual(reversed.issueViews, ["7", "8"]);
1788
- assert.match(reversed.stdout, /HOLD \(tier mismatch impl-7\)/);
1789
- assert.doesNotMatch(reversed.stdout, /impl-8/);
1790
- const both = runCli([], { ...base, issueStates: { 7: "CLOSED", 8: "CLOSED" } });
1791
- assert.deepEqual(both.issueViews, ["7", "8"]);
1792
- assert.doesNotMatch(both.stdout, /tier mismatch/);
1793
- const failed = runCli([], { ...base, issueStates: { 7: "CLOSED" }, env: { ISSUE_VIEW_FAIL: "1" } });
1794
- assert.deepEqual(failed.issueViews, ["7", "8"], "a failed probe of #7 does not stop the probe of #8");
1795
- });
1796
-
1797
- test("CLI: a failed gh issue view probe is disclosed, not silently swallowed", () => {
1798
- const full = { shortlist: shortlistText([1, 2, 3]), refresh: shortlistText([1, 2, 3, 50]) };
1799
- const r = runCli([], {
1800
- ...full, ledger: { rows: ["#50 excluded · behind-issue:#9"] }, issueStates: { 9: "CLOSED" },
1801
- env: { ISSUE_VIEW_FAIL: "1" },
1802
- });
1803
- assert.equal(r.status, 0, r.stderr);
1804
- assert.equal(r.refreshed, 0, "a failed probe cannot confirm the lift — the exclusion stands");
1805
- assert.match(r.stderr, /fleet-tick: gh issue view 9 exited 1: gh: issue view failed — behind-issue:#9 premise unconfirmed, exclusion stands/);
1806
- });
1807
-
1808
- test("CLI: the six caller-stated flags are gone — each refuses as unknown", () => {
1809
- for (const flag of ["--implementers", "--reviewers", "--merge-bots", "--pool", "--reviews-ready", "--merge-holds"]) {
1810
- const r = runCli([flag, "1"], { shortlist: shortlistText([]) });
1811
- assert.equal(r.status, 2, flag);
1812
- assert.match(r.stderr, /accepted: --implementer-cap, --reviewer-cap, --max-reviews, --fold-unchanged, --state/, flag);
1813
- assert.equal(r.stdout.trim(), "", flag);
1814
- }
1815
- });
1816
-
1817
- test("CLI: caps must be positive integers, with no upper bound", () => {
1818
- for (const bad of [["--implementer-cap", "0"], ["--reviewer-cap", "0"], ["--max-reviews", "0"], ["--implementer-cap=x"], ["--max-reviews="]]) {
1819
- const r = runCli(bad, { shortlist: shortlistText([]) });
1820
- assert.equal(r.status, 2, bad.join(" "));
1821
- assert.equal(r.stdout.trim(), "");
1822
- }
1823
- const big = runCli(["--implementer-cap", "9", "--reviewer-cap", "12"], { shortlist: shortlistText([]) });
1824
- assert.equal(big.status, 0, big.stderr);
1825
- assert.match(big.stdout, /^implementers 0\/9 /m);
1826
- assert.match(big.stdout, /^reviewers {4}0\/12 /m);
1827
- });
1828
-
1829
- test("CLI: a failed gh read refuses — it is not an empty backlog", () => {
1830
- const r = runCli([], { env: { PR_FAIL: "1" }, shortlist: shortlistText([]) });
1831
- assert.equal(r.status, 2);
1832
- assert.equal(r.stdout.trim(), "");
1833
- assert.match(r.stderr, /gh pr list/);
1834
- });
1835
-
1836
- test("CLI: a PR list at the limit refuses rather than serving a truncated one", () => {
1837
- const r = runCli([], { prs: Array.from({ length: 200 }, (_, i) => pr(i + 1)), shortlist: shortlistText([]) });
1838
- assert.equal(r.status, 2);
1839
- assert.match(r.stderr, /truncated|capped/i);
1840
- });
1841
-
1842
- test("CLI: a gh row missing closingIssuesReferences refuses rather than counting as no-issue", () => {
1843
- const r = runCli([], { prs: [{ number: 1, labels: [] }], shortlist: shortlistText([]) });
1844
- assert.equal(r.status, 2);
1845
- assert.equal(r.stdout.trim(), "");
1846
- assert.match(r.stderr, /closingIssuesReferences/);
1847
- });
1848
-
1849
- test("CLI: a gh row missing headRefOid refuses rather than reading every past-pin halt as moved", () => {
1850
- const { headRefOid, ...headless } = pr(1);
1851
- const r = runCli([], { prs: [headless], shortlist: shortlistText([]) });
1852
- assert.equal(r.status, 2);
1853
- assert.equal(r.stdout.trim(), "");
1854
- assert.match(r.stderr, /headRefOid/);
1855
- });
1856
-
1857
- test("CLI: a past-pin halt prints DISPATCH review for its PR (#2083)", () => {
1858
- const r = runCli([], {
1859
- ledger: { rows: ["#10 impl-10=PR#40 → PR#40 · review=wf:a reviewed=abc1234:0/0/0 · finisher-pr-40=halted:past-pin"] },
1860
- shortlist: shortlistText([]), prs: [pr(40, [], [10], HEAD_B)],
1861
- });
1862
- assert.equal(r.status, 0, r.stderr);
1863
- assert.match(r.stdout, /^reviewers {4}0\/6 → DISPATCH review PR#40 /m);
1864
- });
1865
-
1866
- test("CLI: an ambient GIT_DIR naming another repository cannot move the shortlist read", (t) => {
1867
- const decoy = realpathSync(mkdtempSync(join(tmpdir(), "fleet-tick-decoy-")));
1868
- t.after(() => rmSync(decoy, { recursive: true, force: true }));
1869
- spawnBounded("git", ["init", "-q", decoy]);
1870
- mkdirSync(join(decoy, ".fleet"));
1871
- writeFileSync(join(decoy, ".fleet", "shortlist.json"), shortlistText([666]));
1872
- const r = runCli([], { shortlist: shortlistText([7, 8]), env: { GIT_DIR: join(decoy, ".git") } });
1873
- assert.equal(r.status, 0, r.stderr);
1874
- assert.match(r.stdout, /^implementers 0\/2 → PULL #7 #8 /m);
1875
- });
1876
-
1877
- test("CLI: an inherited GIT_DIR cannot retarget the tier-mismatch closed-ticket probe", (t) => {
1878
- const decoy = realpathSync(mkdtempSync(join(tmpdir(), "fleet-tick-decoy-tier-")));
1879
- t.after(() => rmSync(decoy, { recursive: true, force: true }));
1880
- spawnBounded("git", ["init", "-q", decoy]);
1881
- const r = runCli([], {
1882
- shortlist: shortlistText([1, 2, 3]), ledger: { rows: ["#7 impl-7=tier-mismatch"], dispatched: ["impl-7=tier-mismatch"] },
1883
- issueStates: { 7: "OPEN" }, env: { GIT_DIR: join(decoy, ".git"), GH_REPO: "someone/else" },
1884
- });
1885
- assert.equal(r.status, 0, r.stderr);
1886
- assert.deepEqual(r.issueViews, ["7"], "the probe must still run");
1887
- assert.match(r.stdout, /^implementers 0\/2 → HOLD \(tier mismatch impl-7\)/m, "#7 is OPEN in the case's own repository - the hold stands");
1888
- });
1889
-
1890
- test("CLI: an inherited GIT_DIR cannot retarget the behind-issue premise probe", (t) => {
1891
- const decoy = realpathSync(mkdtempSync(join(tmpdir(), "fleet-tick-decoy-gh-")));
1892
- t.after(() => rmSync(decoy, { recursive: true, force: true }));
1893
- spawnBounded("git", ["init", "-q", decoy]);
1894
- const r = runCli([], {
1895
- shortlist: shortlistText([1, 2, 3]), ledger: { rows: ["#50 excluded · behind-issue:#9"] }, issueStates: { 9: "OPEN" },
1896
- env: { GIT_DIR: join(decoy, ".git"), GH_REPO: "someone/else" },
1897
- });
1898
- assert.equal(r.status, 0, r.stderr);
1899
- assert.deepEqual(r.issueViews, ["9"], "the probe must still run");
1900
- assert.equal(r.refreshed, 0, "#9 is OPEN in the case's own repository — the exclusion stands");
1901
- });
1902
-
1903
- // A review whose `reviewed=` write never landed stays in flight in the ledger
1904
- // after its PR merges. Each such row holds one reviewer slot unless the tick
1905
- // asks gh about the PR and finds it finished.
1906
- const zombieRows = (numbers) => numbers.map((n) => `#${n - 90} impl-${n - 90}=PR#${n} → PR#${n} · review=member:review-pr-${n}`);
1907
- const ZOMBIES = Array.from({ length: 10 }, (_, i) => 101 + i);
1908
- const REVIEW_DUE = pr(500, [], [1500], HEAD_B);
1909
- const CAPS = ["--reviewer-cap", "10", "--max-reviews", "10"];
1910
-
1911
- test("CLI: dangling in-flight reviews of merged PRs do not starve review dispatch", () => {
1912
- const r = runCli(CAPS, {
1913
- shortlist: shortlistText([]), prs: [REVIEW_DUE], ledger: { rows: zombieRows(ZOMBIES) },
1914
- prStates: Object.fromEntries(ZOMBIES.map((n) => [n, "MERGED"])),
1915
- });
1916
- assert.equal(r.status, 0, r.stderr);
1917
- assert.deepEqual(r.prViews.map(Number).sort((a, b) => a - b), ZOMBIES, "each off-list PR is asked about");
1918
- assert.match(r.stdout, /^reviewers +0\/10 → DISPATCH review PR#500/m);
1919
- });
1920
-
1921
- test("CLI: a CLOSED PR's dangling review is released too", () => {
1922
- const r = runCli(["--reviewer-cap", "1", "--max-reviews", "1"], {
1923
- shortlist: shortlistText([]), prs: [REVIEW_DUE], ledger: { rows: zombieRows([101]) }, prStates: { 101: "CLOSED" },
1924
- });
1925
- assert.equal(r.status, 0, r.stderr);
1926
- assert.match(r.stdout, /^reviewers +0\/1 → DISPATCH review PR#500/m);
1927
- });
1928
-
1929
- test("CLI: an in-flight review on an open PR keeps its slot and is never probed", () => {
1930
- const r = runCli(["--reviewer-cap", "1", "--max-reviews", "1"], {
1931
- shortlist: shortlistText([]), prs: [pr(101, [], [1101]), REVIEW_DUE], ledger: { rows: zombieRows([101]) },
1932
- });
1933
- assert.equal(r.status, 0, r.stderr);
1934
- assert.deepEqual(r.prViews, [], "an open PR needs no probe");
1935
- assert.match(r.stdout, /^reviewers +1\/1 → AT CAP/m);
1936
- assert.doesNotMatch(r.stdout, /DISPATCH review/);
1937
- });
1938
-
1939
- test("CLI: a PR off the open list that gh still calls OPEN keeps its review's slot", () => {
1940
- const r = runCli(["--reviewer-cap", "1", "--max-reviews", "1"], {
1941
- shortlist: shortlistText([]), prs: [REVIEW_DUE], ledger: { rows: zombieRows([101]) }, prStates: { 101: "OPEN" },
1942
- });
1943
- assert.equal(r.status, 0, r.stderr);
1944
- assert.deepEqual(r.prViews, ["101"]);
1945
- assert.match(r.stdout, /^reviewers +1\/1 → AT CAP/m);
1946
- assert.doesNotMatch(r.stdout, /DISPATCH review/);
1947
- });
1948
-
1949
- test("CLI: a PR gh cannot answer for is not read as merged - the slot is held and the failure disclosed", () => {
1950
- const cases = [
1951
- ["a nonzero exit", { PR_VIEW_FAIL: "1" }, /gh pr view 101 exited 1: gh: pr view failed/],
1952
- ["a reply with no state", { PR_VIEW_BODY: "<html>proxy error</html>" }, /gh pr view 101 printed no PR state/],
1953
- ["a state that is not MERGED or CLOSED", { PR_VIEW_BODY: JSON.stringify({ state: "merged" }) }, /gh pr view 101 printed no PR state/],
1954
- ];
1955
- for (const [what, env, shown] of cases) {
1956
- const r = runCli(["--reviewer-cap", "1", "--max-reviews", "1"], {
1957
- shortlist: shortlistText([]), prs: [REVIEW_DUE], ledger: { rows: zombieRows([101]) }, prStates: { 101: "MERGED" }, env,
1958
- });
1959
- assert.equal(r.status, 0, `${what}: ${r.stderr}`);
1960
- assert.match(r.stderr, shown, what);
1961
- assert.match(r.stdout, /^reviewers +1\/1 → AT CAP/m, what);
1962
- assert.doesNotMatch(r.stdout, /DISPATCH review/, what);
1963
- }
1964
- });
1965
-
1966
- test("CLI: an inherited GIT_DIR cannot retarget the merged-PR review probe", (t) => {
1967
- const decoy = realpathSync(mkdtempSync(join(tmpdir(), "fleet-tick-decoy-pr-")));
1968
- t.after(() => rmSync(decoy, { recursive: true, force: true }));
1969
- spawnBounded("git", ["init", "-q", decoy]);
1970
- const r = runCli(["--reviewer-cap", "1", "--max-reviews", "1"], {
1971
- shortlist: shortlistText([]), prs: [REVIEW_DUE], ledger: { rows: zombieRows([101]) }, prStates: { 101: "OPEN" },
1972
- env: { GIT_DIR: join(decoy, ".git"), GH_REPO: "someone/else" },
1973
- });
1974
- assert.equal(r.status, 0, r.stderr);
1975
- assert.deepEqual(r.prViews, ["101"], "the probe must still run");
1976
- assert.match(r.stdout, /^reviewers +1\/1 → AT CAP/m, "101 is OPEN in the case's own repository - its review keeps the slot");
1977
- });
1978
-
1979
- // #2210, end to end: a real repo, a real baseline, a real stray write.
1980
- const LIVE = {
1981
- ledger: { rows: ["#412 impl-412 · tier-ok=impl-412:fleet-implementer"], dispatched: ["impl-412"] },
1982
- shortlist: shortlistText([412, 420, 421]),
1983
- };
1984
- // One more tick over a kept runCli fixture, as the next wake would run it.
1985
- const tickAgain = (r) => spawnBounded(process.execPath, [join(r.dir, "bin", "fleet-tick.mjs"), "--state", join(r.dir, "hb.json")], {
1986
- cwd: r.repo,
1987
- env: { ...process.env, PATH: `${join(r.dir, "bin")}:${process.env.PATH}`, FIXTURE_PRS: join(r.dir, "prs.json") },
1988
- });
1989
-
1990
- test("CLI: a stray write after the baseline prints MAIN-CHECKOUT-DIRTY first, naming the path and the live members, and holds every dispatching row", (t) => {
1991
- const r = runCli([], {
1992
- ...LIVE, prs: [pr(350)], keep: true,
1993
- afterBaseline: (repo) => writeFileSync(join(repo, "stray.mjs"), "x\n"),
1994
- });
1995
- t.after(() => rmSync(r.dir, { recursive: true, force: true }));
1996
- assert.equal(r.status, 0, r.stderr);
1997
- const lines = r.stdout.trim().split("\n");
1998
- assert.match(lines[0], /^MAIN-CHECKOUT-DIRTY stray\.mjs — changed since the run's baseline; live members: impl-412 — dispatch held: resolve the stray paths FIRST, then re-baseline with ~\/\.fleet\/bin\/fleet-run main-checkout\.mjs --record/);
1999
- assert.match(r.stdout, /^implementers 1\/2 → HOLD \(main checkout dirty\)/m);
2000
- assert.match(r.stdout, /^reviewers +0\/6 → HOLD \(main checkout dirty\)/m);
2001
- assert.match(r.stdout, /^merge-bot +0\/1 → HOLD \(main checkout dirty\)/m);
2002
- assert.doesNotMatch(r.stdout, /PULL|DISPATCH/);
2003
-
2004
- // Not a one-tick warning: the next tick holds the same way, and only an
2005
- // explicit re-baseline after the path is resolved lets dispatch through.
2006
- assert.match(tickAgain(r).stdout, /^MAIN-CHECKOUT-DIRTY stray\.mjs /m);
2007
- rmSync(join(r.repo, "stray.mjs"));
2008
- const rec = spawnBounded(process.execPath, [join(r.dir, "bin", "main-checkout.mjs"), "--record"], { cwd: r.repo });
2009
- assert.equal(rec.status, 0, rec.stderr);
2010
- const after = tickAgain(r);
2011
- assert.equal(after.status, 0, after.stderr);
2012
- assert.doesNotMatch(after.stdout, /MAIN-CHECKOUT|main checkout/);
2013
- assert.match(after.stdout, /^implementers 1\/2 → PULL #420 /m);
2014
- });
2015
-
2016
- test("CLI: uncommitted work in the main checkout at run start does not hold the run", () => {
2017
- const r = runCli([], {
2018
- ...LIVE,
2019
- beforeRun: (repo) => writeFileSync(join(repo, "my-notes.md"), "the maintainer's own\n"),
2020
- });
2021
- assert.equal(r.status, 0, r.stderr);
2022
- assert.doesNotMatch(r.stdout, /MAIN-CHECKOUT|main checkout/);
2023
- assert.match(r.stdout, /^implementers 1\/2 → PULL #420 /m);
2024
- });
2025
-
2026
- test("CLI: a run that never recorded a baseline is held, never read as clean", () => {
2027
- const r = runCli([], { ...LIVE, baseline: false });
2028
- assert.equal(r.status, 0, r.stderr);
2029
- assert.match(r.stdout, /^MAIN-CHECKOUT-NO-BASELINE no baseline at .*\.fleet\/main-checkout\.sha — dispatch held: record it at run start with /);
2030
- assert.match(r.stdout, /^implementers 1\/2 → HOLD \(main checkout no baseline\)/m);
2031
- });
2032
-
2033
- test("CLI: a main checkout git cannot read is MAIN-CHECKOUT-UNKNOWN and held, never clean", () => {
2034
- const r = runCli([], {
2035
- ...LIVE,
2036
- afterBaseline: (repo) => writeFileSync(join(repo, ".git", "index"), "not an index"),
2037
- });
2038
- assert.equal(r.status, 0, r.stderr);
2039
- assert.match(r.stdout, /^MAIN-CHECKOUT-UNKNOWN could not look: git status --porcelain -uall exited \d+.*; live members: impl-412 — .*NEVER re-baseline over it/);
2040
- assert.match(r.stdout, /^implementers 1\/2 → HOLD \(main checkout unknown\)/m);
2041
- });
2042
-
2043
- // #2307: a folded tick under a dirty or unknown hold still names the hold, and
2044
- // the hold stays non-actionable — `quiet` rises on every tick, the fold fires.
2045
- // Three `--fold-unchanged` ticks over one kept fixture and one state file.
2046
- const foldTicks = (fixture) => {
2047
- const first = runCli(["--fold-unchanged"], { ...fixture, keep: true });
2048
- try {
2049
- const state = join(first.dir, "heartbeat.json");
2050
- const quiet = () => JSON.parse(readFileSync(state, "utf8")).quiet;
2051
- const ticks = [{ stdout: first.stdout, status: first.status, stderr: first.stderr, quiet: quiet() }];
2052
- for (let i = 0; i < 2; i++) {
2053
- const t = spawnBounded(process.execPath, [join(first.dir, "bin", "fleet-tick.mjs"), "--fold-unchanged", "--state", state], {
2054
- cwd: first.repo,
2055
- env: {
2056
- ...process.env, PATH: `${join(first.dir, "bin")}:${process.env.PATH}`,
2057
- FIXTURE_PRS: join(first.dir, "prs.json"), FIXTURE_CLAIMED: join(first.dir, "claimed.json"),
2058
- FIXTURE_ISSUE_STATES: join(first.dir, "issue-states.json"), FIXTURE_REFRESH: join(first.dir, "refresh.json"),
2059
- REFRESH_LOG: join(first.dir, "refresh.log"), ISSUE_VIEW_LOG: join(first.dir, "issue-view.log"),
2060
- },
2061
- });
2062
- ticks.push({ stdout: t.stdout, status: t.status, stderr: t.stderr, quiet: quiet() });
2063
- }
2064
- return ticks;
2065
- } finally {
2066
- rmSync(first.dir, { recursive: true, force: true });
2067
- }
2068
- };
2069
-
2070
- // foldTicks keeps its fixture across three ticks, so a throw after the first
2071
- // run (here: `heartbeat.json` is a directory, so reading the state fails)
2072
- // must still remove the dir — the caller never gets it back.
2073
- test("CLI: foldTicks removes its kept fixture dir when a tick throws", () => {
2074
- let dir;
2075
- assert.throws(() => foldTicks({
2076
- ...LIVE,
2077
- afterBaseline: (repo) => { dir = join(repo, ".."); mkdirSync(join(dir, "heartbeat.json")); },
2078
- }), /EISDIR/);
2079
- assert.ok(dir, "afterBaseline never ran, so the throw happened before the dir existed");
2080
- assert.equal(existsSync(dir), false);
2081
- });
2082
-
2083
- for (const [state, afterBaseline] of [
2084
- ["dirty", (repo) => writeFileSync(join(repo, "stray.mjs"), "x\n")],
2085
- ["unknown", (repo) => writeFileSync(join(repo, ".git", "index"), "not an index")],
2086
- ]) {
2087
- test(`CLI: --fold-unchanged under a ${state} main checkout folds to one line naming the hold, quiet rising every tick`, () => {
2088
- const ticks = foldTicks({ ...LIVE, afterBaseline });
2089
- for (const t of ticks) assert.equal(t.status, 0, t.stderr);
2090
- assert.match(ticks[0].stdout, new RegExp(`^implementers 1/2 → HOLD \\(main checkout ${state}\\)`, "m"));
2091
- assert.deepEqual(ticks.map((t) => t.quiet), [1, 2, 3]);
2092
- for (const [i, t] of ticks.slice(1).entries()) {
2093
- assert.deepEqual(t.stdout.trim().split("\n"), [
2094
- `fleet-tick: unchanged, nothing to act on (quiet=${i + 2}); HOLD (main checkout ${state}) persists — full rows on the next change`,
2095
- ]);
2096
- }
2097
- });
2098
- }
2099
-
2100
- test("CLI: --fold-unchanged never folds a missing-baseline hold — it asks the controller to act", () => {
2101
- const ticks = foldTicks({ ...LIVE, baseline: false });
2102
- for (const t of ticks) {
2103
- assert.equal(t.status, 0, t.stderr);
2104
- assert.match(t.stdout, /^MAIN-CHECKOUT-NO-BASELINE /);
2105
- assert.match(t.stdout, /^implementers 1\/2 → HOLD \(main checkout no baseline\)/m);
2106
- assert.doesNotMatch(t.stdout, /nothing to act on/);
2107
- assert.equal(t.quiet, 0);
2108
- }
2109
- assert.equal(ticks[2].stdout, ticks[1].stdout);
2110
- });
2111
-
2112
- test("CLI: an in-flight review with no member token is named as review:PR#<n> on the MAIN-CHECKOUT line", () => {
2113
- const r = runCli([], {
2114
- ledger: { rows: ["#51 review=wf:x"] }, shortlist: shortlistText([]), prs: [pr(51)],
2115
- afterBaseline: (repo) => writeFileSync(join(repo, "stray.mjs"), "x\n"),
2116
- });
2117
- assert.equal(r.status, 0, r.stderr);
2118
- assert.match(r.stdout, /^MAIN-CHECKOUT-DIRTY stray\.mjs — changed since the run's baseline; live members: review:PR#51 — /m);
2119
- });
2120
-
2121
- test("CLI: an ambient GIT_DIR naming a dirty repository does not move the main-checkout check", (t) => {
2122
- const decoy = realpathSync(mkdtempSync(join(tmpdir(), "fleet-tick-decoy-mc-")));
2123
- t.after(() => rmSync(decoy, { recursive: true, force: true }));
2124
- spawnBounded("git", ["init", "-q", decoy]);
2125
- writeFileSync(join(decoy, "decoy-stray.txt"), "x\n");
2126
- const r = runCli([], { ...LIVE, env: { GIT_DIR: join(decoy, ".git"), GIT_WORK_TREE: decoy } });
2127
- assert.equal(r.status, 0, r.stderr);
2128
- assert.doesNotMatch(r.stdout, /MAIN-CHECKOUT/);
2129
- assert.match(r.stdout, /^implementers 1\/2 → PULL #420 /m);
2130
- });
2131
-
2132
- // ---------------------------------------------------------------------------
2133
- // The heartbeat's half - #357. The tick is the only writer of the back-off
2134
- // streak and the fold digest, so these pin the contract fleet-heartbeat.mjs
2135
- // reads rather than the heartbeat's own arithmetic (that is its test's job).
2136
-
2137
- const IDLE = { shortlist: shortlistText([]), refresh: shortlistText([]) };
2138
-
2139
- test("CLI: the quiet streak lengthens on an idle tick and resets on an actionable one", (t) => {
2140
- const dir = realpathSync(mkdtempSync(join(tmpdir(), "fleet-tick-state-")));
2141
- t.after(() => rmSync(dir, { recursive: true, force: true }));
2142
- const path = join(dir, "heartbeat.json");
2143
- runCli(["--state", path], IDLE);
2144
- assert.equal(JSON.parse(readFileSync(path, "utf8")).quiet, 1);
2145
- runCli(["--state", path], IDLE);
2146
- assert.equal(JSON.parse(readFileSync(path, "utf8")).quiet, 2);
2147
- runCli(["--state", path], { ...IDLE, shortlist: shortlistText([9]) });
2148
- assert.equal(JSON.parse(readFileSync(path, "utf8")).quiet, 0);
2149
- });
2150
-
2151
- test("CLI: --fold-unchanged folds a repeated idle tick to one line", (t) => {
2152
- const dir = realpathSync(mkdtempSync(join(tmpdir(), "fleet-tick-fold-")));
2153
- t.after(() => rmSync(dir, { recursive: true, force: true }));
2154
- const path = join(dir, "heartbeat.json");
2155
- const first = runCli(["--fold-unchanged", "--state", path], IDLE);
2156
- assert.equal(first.status, 0, first.stderr);
2157
- assert.ok(first.stdout.trim().split("\n").length >= 3);
2158
- const second = runCli(["--fold-unchanged", "--state", path], IDLE);
2159
- assert.deepEqual(second.stdout.trim().split("\n"), ["fleet-tick: unchanged, nothing to act on (quiet=2) — full rows on the next change"]);
2160
- });
2161
-
2162
- test("CLI: --fold-unchanged never folds an actionable tick, even an identical one", (t) => {
2163
- const dir = realpathSync(mkdtempSync(join(tmpdir(), "fleet-tick-fold-act-")));
2164
- t.after(() => rmSync(dir, { recursive: true, force: true }));
2165
- const path = join(dir, "heartbeat.json");
2166
- const busy = { shortlist: shortlistText([9, 10]) };
2167
- const first = runCli(["--fold-unchanged", "--state", path], busy);
2168
- const second = runCli(["--fold-unchanged", "--state", path], busy);
2169
- assert.equal(second.stdout, first.stdout);
2170
- assert.match(second.stdout, /PULL #9 #10/);
2171
- assert.equal(JSON.parse(readFileSync(path, "utf8")).quiet, 0);
2172
- });
2173
-
2174
- test("CLI: --fold-unchanged does not fold when the rows change, even with nothing to act on", (t) => {
2175
- const dir = realpathSync(mkdtempSync(join(tmpdir(), "fleet-tick-fold-differs-")));
2176
- t.after(() => rmSync(dir, { recursive: true, force: true }));
2177
- const path = join(dir, "heartbeat.json");
2178
- runCli(["--fold-unchanged", "--state", path], IDLE);
2179
- // A queued candidate, held behind a lower PR: still nothing to act on.
2180
- const held = {
2181
- ...IDLE, ledger: { rows: ["#10 impl-10=PR#40 → PR#40 · held-behind:#38 · tier-ok=impl-10:fleet-implementer"] },
2182
- prs: [pr(38, [], []), pr(40, ["ready-to-merge"])],
2183
- };
2184
- const second = runCli(["--fold-unchanged", "--state", path], held);
2185
- assert.match(second.stdout, /^merge-bot {4}0\/1 → HOLD\b/m);
2186
- assert.doesNotMatch(second.stdout, /nothing to act on/);
2187
- const third = runCli(["--fold-unchanged", "--state", path], held);
2188
- assert.match(third.stdout, /^fleet-tick: unchanged, nothing to act on \(quiet=3\)/);
2189
- });
2190
-
2191
- test("CLI: with no --state, the tick resolves the run's shared default", (t) => {
2192
- const r = runCli([], { ...IDLE, defaultState: true, keep: true });
2193
- t.after(() => rmSync(r.dir, { recursive: true, force: true }));
2194
- const state = join(r.repo, ".fleet", "heartbeat.json");
2195
- assert.equal(r.status, 0, r.stderr);
2196
- assert.doesNotMatch(r.stderr, /WARNING/);
2197
- assert.ok(existsSync(state));
2198
- assert.equal(JSON.parse(readFileSync(state, "utf8")).quiet, 1);
2199
- });
2200
-
2201
- // The prior run's liveness - #1597.
2202
- const beat = (ms, interval, stopped = "") =>
2203
- JSON.stringify({ quiet: 0, elapsed: 0, digest: "", beat: { at: Date.now() - ms, interval, stopped } });
2204
-
2205
- test("CLI: a stale prior beat is reported, and reported FIRST, with the stranded claims and the supply", (t) => {
2206
- const dir = realpathSync(mkdtempSync(join(tmpdir(), "fleet-tick-stall-")));
2207
- t.after(() => rmSync(dir, { recursive: true, force: true }));
2208
- const path = join(dir, "heartbeat.json");
2209
- writeFileSync(path, beat(90 * 60_000, 1200));
2210
- const r = runCli(["--state", path], { shortlist: shortlistText([9], 1), claimed: [{ number: 41 }, { number: 42 }, { number: 43 }] });
2211
- assert.equal(r.status, 0, r.stderr);
2212
- const lines = r.stdout.trim().split("\n");
2213
- assert.match(lines[0], /^heartbeat STALLED/);
2214
- assert.match(lines[1], /^implementers/);
2215
- assert.match(lines[0], /3 ticket\(s\) claimed and in flight/);
2216
- assert.match(lines[0], /pool supply 1/);
2217
- });
2218
-
2219
- test("CLI: an unreadable shortlist makes the stall report's supply unknown, never zero", (t) => {
2220
- const dir = realpathSync(mkdtempSync(join(tmpdir(), "fleet-tick-stall-supply-")));
2221
- t.after(() => rmSync(dir, { recursive: true, force: true }));
2222
- const path = join(dir, "heartbeat.json");
2223
- writeFileSync(path, beat(90 * 60_000, 1200));
2224
- const r = runCli(["--state", path], { shortlist: "{" });
2225
- assert.match(r.stdout.split("\n")[0], /pool supply unknown/);
2226
- });
2227
-
2228
- test("CLI: the stall is announced even when the reconcile then refuses", (t) => {
2229
- const dir = realpathSync(mkdtempSync(join(tmpdir(), "fleet-tick-stall-refuse-")));
2230
- t.after(() => rmSync(dir, { recursive: true, force: true }));
2231
- const path = join(dir, "heartbeat.json");
2232
- writeFileSync(path, beat(90 * 60_000, 1200));
2233
- const r = runCli(["--state", path], { ...IDLE, claimed: [{ number: 41 }], env: { PR_FAIL: "1" } });
2234
- assert.equal(r.status, 2);
2235
- assert.match(r.stderr, /gh pr list failed/);
2236
- assert.match(r.stdout, /heartbeat STALLED/);
2237
- });
2238
-
2239
- test("CLI: an unreadable claim count is `unknown`, never zero", (t) => {
2240
- const dir = realpathSync(mkdtempSync(join(tmpdir(), "fleet-tick-stall-unknown-")));
2241
- t.after(() => rmSync(dir, { recursive: true, force: true }));
2242
- const path = join(dir, "heartbeat.json");
2243
- writeFileSync(path, beat(90 * 60_000, 1200));
2244
- const r = runCli(["--state", path], { ...IDLE, env: { CLAIMED_FAIL: "1" } });
2245
- assert.equal(r.status, 0, r.stderr);
2246
- assert.match(r.stdout, /unknown ticket\(s\) claimed and in flight/);
2247
- assert.match(r.stderr, /claimed ticket count unknown/);
2248
- assert.match(r.stderr, /boom/);
2249
- });
2250
-
2251
- // The claimed-count query is capped at CLAIMED_LIMIT (200). At exactly the cap
2252
- // the list may be truncated, so the report must say the count is a floor
2253
- // (`200+`); one below the cap it is exact and must carry no `+`. The stub
2254
- // answers `--jq length` over the fixture array, so the array's size is the
2255
- // count the tick reads. #1760.
2256
- // Mutation-checked both ways against claimed()'s truncation branch: making it
2257
- // `return n;` reds the 200 case ("…; 200 ticket(s) claimed and in flight…"
2258
- // did not match /; 200\+ ticket/), and making it always return `${n}+` reds the
2259
- // 199 case ("…; 199+ ticket(s) claimed and in flight…" did not match
2260
- // /; 199 ticket/). The literal `199 ticket(s)` adjacency — no `+` allowed
2261
- // between the digits and the space — is what refuses `199+`; the `; ` prefix
2262
- // only anchors the match to the claimed-count field.
2263
- test("CLI: a claim count AT the query cap is disclosed as a floor (`200+`); one below it is exact", (t) => {
2264
- const dir = realpathSync(mkdtempSync(join(tmpdir(), "fleet-tick-stall-cap-")));
2265
- t.after(() => rmSync(dir, { recursive: true, force: true }));
2266
- const path = join(dir, "heartbeat.json");
2267
- const stalled = (count) => {
2268
- // Re-staled every call: each run records its own tick in the state file.
2269
- writeFileSync(path, beat(90 * 60_000, 1200));
2270
- const claimed = Array.from({ length: count }, (_, i) => ({ number: i + 1 }));
2271
- const r = runCli(["--state", path], { ...IDLE, claimed });
2272
- assert.equal(r.status, 0, r.stderr);
2273
- const line = r.stdout.split("\n")[0];
2274
- assert.match(line, /^heartbeat STALLED/);
2275
- return line;
2276
- };
2277
- assert.match(stalled(200), /; 200\+ ticket\(s\) claimed and in flight/);
2278
- assert.match(stalled(199), /; 199 ticket\(s\) claimed and in flight/);
2279
- });
2280
-
2281
- test("CLI: a beat within the interval it promised is not reported at all", (t) => {
2282
- const dir = realpathSync(mkdtempSync(join(tmpdir(), "fleet-tick-quiet-")));
2283
- t.after(() => rmSync(dir, { recursive: true, force: true }));
2284
- const path = join(dir, "heartbeat.json");
2285
- writeFileSync(path, beat(20 * 60_000, 1200));
2286
- assert.doesNotMatch(runCli(["--state", path], IDLE).stdout, /STALLED/);
2287
- writeFileSync(path, beat(20 * 60_000, 300));
2288
- assert.match(runCli(["--state", path], IDLE).stdout, /STALLED/);
2289
- });
2290
-
2291
- test("CLI: the streak write carries the heartbeat's mark instead of erasing it, and writes its own", (t) => {
2292
- const dir = realpathSync(mkdtempSync(join(tmpdir(), "fleet-tick-mark-carry-")));
2293
- t.after(() => rmSync(dir, { recursive: true, force: true }));
2294
- const path = join(dir, "heartbeat.json");
2295
- const mark = { at: Date.now() - 60_000, interval: 300, stopped: "" };
2296
- writeFileSync(path, JSON.stringify({ quiet: 3, elapsed: 7, digest: "old", beat: mark }));
2297
- const r = runCli(["--state", path], IDLE);
2298
- assert.equal(r.status, 0, r.stderr);
2299
- const after = JSON.parse(readFileSync(path, "utf8"));
2300
- assert.deepEqual(after.beat, mark);
2301
- assert.equal(after.quiet, 4);
2302
- assert.ok(after.ticked && after.ticked.at >= Date.now() - 5000);
2303
- });
2304
-
2305
- test("CLI: a deliberate stop is reported with its reason, and a run that never beat says nothing", (t) => {
2306
- const dir = realpathSync(mkdtempSync(join(tmpdir(), "fleet-tick-stopped-")));
2307
- t.after(() => rmSync(dir, { recursive: true, force: true }));
2308
- const path = join(dir, "heartbeat.json");
2309
- writeFileSync(path, beat(6 * 60_000, 300, "context ceiling reached"));
2310
- const r = runCli(["--state", path], IDLE);
2311
- assert.equal(r.status, 0, r.stderr);
2312
- assert.match(r.stdout, /stopped deliberately — recorded reason: context ceiling reached/);
2313
- assert.doesNotMatch(r.stdout, /without a recorded reason/);
2314
- writeFileSync(path, JSON.stringify({ quiet: 0, elapsed: 0, digest: "" }));
2315
- assert.doesNotMatch(runCli(["--state", path], IDLE).stdout, /STALLED/);
2316
- });
2317
-
2318
- test("CLI: a busy, fully-staffed fleet is not reported STALLED just because the queue never drained", (t) => {
2319
- // `beat` refreshes only when the queue drains; a tick two minutes ago on a
2320
- // completion edge is what tells a busy run from a dead one.
2321
- const dir = realpathSync(mkdtempSync(join(tmpdir(), "fleet-tick-busy-not-stalled-")));
2322
- t.after(() => rmSync(dir, { recursive: true, force: true }));
2323
- const path = join(dir, "heartbeat.json");
2324
- writeFileSync(path, JSON.stringify({
2325
- quiet: 0, elapsed: 0, digest: "",
2326
- beat: { at: Date.now() - 11 * 60_000, interval: 300, stopped: "" },
2327
- ticked: { at: Date.now() - 2 * 60_000 },
2328
- }));
2329
- const r = runCli(["--state", path], {
2330
- ledger: { rows: ["#1 impl-1 · tier-ok=impl-1:fleet-implementer", "#2 impl-2 · tier-ok=impl-2:fleet-implementer"], dispatched: ["impl-1", "impl-2"] },
2331
- shortlist: shortlistText([3, 4, 5]), claimed: [{ number: 41 }],
2332
- });
2333
- assert.equal(r.status, 0, r.stderr);
2334
- assert.doesNotMatch(r.stdout, /STALLED/);
2335
- assert.match(r.stdout, /^implementers 2\/2 → AT CAP/m);
2336
- });
2337
-
2338
- // Every gh spawn in the tick carries a bound: a gh that never answers is a
2339
- // failed read on that call's own failure path, never a tick that hangs. Each
2340
- // case stalls one gh call for 30s under a 1s override, so a spawn that lost its
2341
- // bound waits the stall out, reads the stub's normal reply, and fails the
2342
- // assertions on the disclosed timeout — inside spawnBounded's own backstop.
2343
- import { ghBudget } from "./fleet-tick.mjs";
2344
-
2345
- const hang = (call, seconds = "30", budget = "1") => ({ GH_HANG: call, GH_HANG_S: seconds, FLEET_TICK_GH_TIMEOUT: budget });
2346
-
2347
- test("CLI: a hung gh pr list refuses the tick at the bound — a read that timed out is not an empty queue", () => {
2348
- const r = runCli([], { shortlist: shortlistText([]), env: hang("pr list") });
2349
- assert.equal(r.status, 2, r.stderr);
2350
- assert.equal(r.stdout.trim(), "");
2351
- assert.match(r.stderr, /gh pr list failed: timed out after 1s — a failed read is not an empty queue/);
2352
- });
2353
-
2354
- test("CLI: a hung probe of a mismatched ticket is disclosed at the bound and the hold stands", () => {
2355
- const r = runCli([], {
2356
- shortlist: shortlistText([1, 2, 3]), ledger: { rows: ["#7 impl-7=tier-mismatch"], dispatched: ["impl-7=tier-mismatch"] },
2357
- issueStates: { 7: "CLOSED" }, env: hang("issue view"),
2358
- });
2359
- assert.equal(r.status, 0, r.stderr);
2360
- assert.match(r.stdout, /^implementers 0\/2 → HOLD \(tier mismatch impl-7\)/m);
2361
- assert.match(r.stderr, /fleet-tick: gh issue view 7 timed out after 1s — tier mismatch on #7 unconfirmed closed, hold stands/);
2362
- });
2363
-
2364
- test("CLI: a hung behind-issue probe is disclosed at the bound and the exclusion stands", () => {
2365
- const r = runCli([], {
2366
- shortlist: shortlistText([1, 2, 3]), refresh: shortlistText([1, 2, 3, 50]),
2367
- ledger: { rows: ["#50 excluded · behind-issue:#9"] }, issueStates: { 9: "CLOSED" }, env: hang("issue view"),
2368
- });
2369
- assert.equal(r.status, 0, r.stderr);
2370
- assert.equal(r.refreshed, 0, "a probe that timed out cannot confirm the lift");
2371
- assert.match(r.stderr, /fleet-tick: gh issue view 9 timed out after 1s — behind-issue:#9 premise unconfirmed, exclusion stands/);
2372
- });
2373
-
2374
- test("CLI: a hung probe of a PR off the open list is disclosed at the bound and its review keeps the slot", () => {
2375
- const r = runCli(["--reviewer-cap", "1", "--max-reviews", "1"], {
2376
- shortlist: shortlistText([]), prs: [REVIEW_DUE], ledger: { rows: zombieRows([101]) }, prStates: { 101: "MERGED" },
2377
- env: hang("pr view"),
2378
- });
2379
- assert.equal(r.status, 0, r.stderr);
2380
- assert.match(r.stderr, /fleet-tick: gh pr view 101 timed out after 1s — review on PR#101 unconfirmed finished, its slot stands/);
2381
- assert.match(r.stdout, /^reviewers +1\/1 → AT CAP/m);
2382
- });
2383
-
2384
- test("CLI: a hung claimed-count read makes the stall report's count unknown at the bound", (t) => {
2385
- const dir = realpathSync(mkdtempSync(join(tmpdir(), "fleet-tick-stall-hung-")));
2386
- t.after(() => rmSync(dir, { recursive: true, force: true }));
2387
- const path = join(dir, "heartbeat.json");
2388
- writeFileSync(path, beat(90 * 60_000, 1200));
2389
- const r = runCli(["--state", path], { ...IDLE, claimed: [{ number: 41 }], env: hang("issue list") });
2390
- assert.equal(r.status, 0, r.stderr);
2391
- assert.match(r.stdout, /unknown ticket\(s\) claimed and in flight/);
2392
- assert.match(r.stderr, /fleet-tick: gh timed out after 1s — claimed ticket count unknown/);
2393
- });
2394
-
2395
- // The one case that proves the DEFAULT is a bound, and pays it in full: an
2396
- // override past the default is refused, so the hang is cut at 20s rather than
2397
- // waited out at 40s.
2398
- test("CLI: FLEET_TICK_GH_TIMEOUT cannot lengthen the default bound", () => {
2399
- const r = runCli([], { shortlist: shortlistText([]), env: hang("pr list", "40", "600") });
2400
- assert.equal(r.status, 2, r.stderr);
2401
- assert.match(r.stderr, /gh pr list failed: timed out after 20s/);
2402
- });
2403
-
2404
- // What the bound must ACCEPT: a gh that answers slowly, inside the bound, is a
2405
- // normal read, not a failure.
2406
- test("CLI: a slow gh that answers inside the bound is read as usual", () => {
2407
- const r = runCli([], {
2408
- shortlist: shortlistText([1, 2, 3]), ledger: { rows: ["#7 impl-7=tier-mismatch"], dispatched: ["impl-7=tier-mismatch"] },
2409
- issueStates: { 7: "CLOSED" }, env: hang("issue view", "2", "10"),
2410
- });
2411
- assert.equal(r.status, 0, r.stderr);
2412
- assert.doesNotMatch(r.stderr, /timed out/);
2413
- assert.deepEqual(r.issueViews, ["7"]);
2414
- assert.match(r.stdout, /^implementers 0\/2 → PULL #1 #2 /m, "the slow CLOSED answer was read and lifted the hold");
2415
- });
2416
-
2417
- // The cases above pin the gh spawns the tick has today; this pins the next
2418
- // one, on the script's code with its comments blanked. Every call through
2419
- // spawnSync or execFileSync that does not name process.execPath or git must
2420
- // pass the shared bound — as `timeout: GH_TIMEOUT_MS` closed by a comma or a
2421
- // brace, never a longer expression — before its call closes. A gh spawned
2422
- // through any other function must name child_process again, and the import pin
2423
- // fails on every line that does.
2424
- test("CLI: every gh spawn in fleet-tick.mjs passes the shared bound", () => {
2425
- const code = readFileSync(SCRIPT, "utf8").replace(/\/\*[\s\S]*?\*\/|\/\/[^\n]*/g, (c) => c.replace(/[^\n]/g, " "));
2426
- assert.deepEqual(code.split("\n").map((l) => l.trimEnd()).filter((l) => l.includes("child_process")),
2427
- ['import { execFileSync, spawnSync } from "node:child_process";'],
2428
- "the script spawns through something new — extend this sweep to it");
2429
- const lineOf = (i) => code.slice(0, i).split("\n").length;
2430
- const calls = [...code.matchAll(/\b(?:spawnSync|execFileSync)\(\s*(?=\S)(?!process\.execPath\b|["']git["'])/g)];
2431
- assert.ok(calls.length > 0, "no gh spawn found; this sweep has nothing to measure");
2432
- const unbounded = calls
2433
- .filter((m) => !/\btimeout:\s*GH_TIMEOUT_MS\s*[,}]/.test(code.slice(m.index, code.indexOf(");", m.index))))
2434
- .map((m) => `line ${lineOf(m.index)}`);
2435
- assert.deepEqual(unbounded, []);
2436
- });
2437
-
2438
- test("ghBudget: the override only ever shortens, and anything but digits leaves the default", () => {
2439
- const cases = [
2440
- [undefined, 20_000], ["", 20_000], ["5", 5_000], ["1", 1_000], ["19", 19_000],
2441
- ["20", 20_000], ["600", 20_000], ["0", 20_000], ["-1", 20_000], ["3.5", 20_000], ["1e1", 20_000], [" 5", 20_000], ["abc", 20_000],
2442
- ];
2443
- for (const [override, ms] of cases) assert.equal(ghBudget(20, override), ms, JSON.stringify(override));
2444
- });