@feigi/fleet-ctl 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (245) hide show
  1. package/.claude-plugin/plugin.json +9 -0
  2. package/LICENSE +202 -0
  3. package/NOTICE +18 -0
  4. package/agents/fleet-finisher.agent.md +7 -0
  5. package/agents/fleet-implementer-alt.agent.md +210 -0
  6. package/agents/fleet-implementer.agent.md +210 -0
  7. package/agents/fleet-merge-bot.agent.md +7 -0
  8. package/agents/fleet-review-comments.agent.md +40 -0
  9. package/agents/fleet-review-correctness.agent.md +43 -0
  10. package/agents/fleet-review-runner.agent.md +73 -0
  11. package/agents/fleet-review-silent-failure.agent.md +49 -0
  12. package/agents/fleet-review-simplify.agent.md +46 -0
  13. package/agents/fleet-review-snapshot.agent.md +24 -0
  14. package/agents/fleet-review-tests.agent.md +46 -0
  15. package/agents/fleet-review-types.agent.md +41 -0
  16. package/agents/fleet-review-verifier.agent.md +29 -0
  17. package/commands/review-and-fix.md +98 -0
  18. package/commands/run-merge-bot.md +357 -0
  19. package/commands/run-team-help.md +17 -0
  20. package/package.json +23 -0
  21. package/scripts/agent-test-dir-prose.test.mjs +59 -0
  22. package/scripts/agent-test.test.mjs +1636 -0
  23. package/scripts/ambient-git-vars-mjs-prose.test.mjs +354 -0
  24. package/scripts/ambient-git-vars-prose.test.mjs +192 -0
  25. package/scripts/ancestry-check-position-prose.test.mjs +175 -0
  26. package/scripts/apply-ruleset.test.mjs +420 -0
  27. package/scripts/arg-header-probes-prose.test.mjs +393 -0
  28. package/scripts/arg.mjs +600 -0
  29. package/scripts/arg.test.mjs +1354 -0
  30. package/scripts/board-cli.test.mjs +694 -0
  31. package/scripts/board-identity.test.mjs +429 -0
  32. package/scripts/board-liveness-banner.test.mjs +119 -0
  33. package/scripts/board-prev-shape.test.mjs +229 -0
  34. package/scripts/board-tryparse-null.test.mjs +119 -0
  35. package/scripts/board.html +471 -0
  36. package/scripts/board.mjs +2037 -0
  37. package/scripts/board.test.mjs +3086 -0
  38. package/scripts/brief-after-cut-prose.test.mjs +53 -0
  39. package/scripts/candidates-exit3-prose.test.mjs +302 -0
  40. package/scripts/candidates.mjs +674 -0
  41. package/scripts/candidates.test.mjs +2101 -0
  42. package/scripts/caveat-unification-prose.test.mjs +98 -0
  43. package/scripts/ci-comment-rot-prose.test.mjs +2025 -0
  44. package/scripts/ci-completes-premise-prose.test.mjs +257 -0
  45. package/scripts/ci-state-prose.test.mjs +113 -0
  46. package/scripts/ci-state.mjs +761 -0
  47. package/scripts/ci-state.test.mjs +1640 -0
  48. package/scripts/ci-vacuous-green.test.mjs +368 -0
  49. package/scripts/citation-sweep-prose.test.mjs +1572 -0
  50. package/scripts/claim-lifecycle.test.mjs +185 -0
  51. package/scripts/claim-ticket.sh +417 -0
  52. package/scripts/claim-ticket.test.mjs +1121 -0
  53. package/scripts/closing-keyword-prose.test.mjs +193 -0
  54. package/scripts/cockpit-launch-idempotent-prose.test.mjs +165 -0
  55. package/scripts/cockpit-tick-nonblocking.test.mjs +210 -0
  56. package/scripts/compute-board.mjs +579 -0
  57. package/scripts/compute-board.test.mjs +1060 -0
  58. package/scripts/compute-spend.mjs +329 -0
  59. package/scripts/compute-spend.test.mjs +451 -0
  60. package/scripts/cross-repo-citation-prose.test.mjs +156 -0
  61. package/scripts/cwd-isolation-pins.mjs +83 -0
  62. package/scripts/dedupe-guard-prose.test.mjs +243 -0
  63. package/scripts/derive-testcmd.sh +239 -0
  64. package/scripts/derive-testcmd.test.mjs +380 -0
  65. package/scripts/diff-stats.mjs +231 -0
  66. package/scripts/diff-stats.test.mjs +408 -0
  67. package/scripts/dispatch-block-golden-prose.test.mjs +1024 -0
  68. package/scripts/dispatch-block-pins-prose.test.mjs +615 -0
  69. package/scripts/drop-merged-label.sh +173 -0
  70. package/scripts/drop-merged-label.test.mjs +480 -0
  71. package/scripts/expected-jobs-prose.test.mjs +80 -0
  72. package/scripts/filing-bar-seat-prose.test.mjs +242 -0
  73. package/scripts/filing-claim-bar-prose.test.mjs +166 -0
  74. package/scripts/finisher-dispatch-premise-prose.test.mjs +312 -0
  75. package/scripts/finisher-halt-prose.test.mjs +106 -0
  76. package/scripts/finisher-name-prose.test.mjs +134 -0
  77. package/scripts/finisher-own-tree-prose.test.mjs +285 -0
  78. package/scripts/finisher-pin-race-prose.test.mjs +339 -0
  79. package/scripts/fix-applier-correction-rules-prose.test.mjs +201 -0
  80. package/scripts/fix-applier-result-file-prose.test.mjs +100 -0
  81. package/scripts/fleet-bootstrap +155 -0
  82. package/scripts/fleet-heartbeat.mjs +272 -0
  83. package/scripts/fleet-heartbeat.test.mjs +450 -0
  84. package/scripts/fleet-provenance +207 -0
  85. package/scripts/fleet-provenance.test.mjs +64 -0
  86. package/scripts/fleet-run +148 -0
  87. package/scripts/fleet-run.test.mjs +90 -0
  88. package/scripts/fleet-state.mjs +400 -0
  89. package/scripts/fleet-state.test.mjs +373 -0
  90. package/scripts/fleet-tick-prose.test.mjs +299 -0
  91. package/scripts/fleet-tick.mjs +832 -0
  92. package/scripts/fleet-tick.test.mjs +1332 -0
  93. package/scripts/force-with-lease-reason-prose.test.mjs +184 -0
  94. package/scripts/frontmatter-allowlist.json +64 -0
  95. package/scripts/frontmatter-check.mjs +256 -0
  96. package/scripts/frontmatter-check.test.mjs +436 -0
  97. package/scripts/git-env.mjs +124 -0
  98. package/scripts/git-env.test.mjs +149 -0
  99. package/scripts/git-status-untracked-mode-sweep.test.mjs +216 -0
  100. package/scripts/gone-walk.test.mjs +213 -0
  101. package/scripts/gp-sep-invariant-prose.test.mjs +188 -0
  102. package/scripts/head-moved-after-label-prose.test.mjs +233 -0
  103. package/scripts/immutable-body-claim-prose.test.mjs +413 -0
  104. package/scripts/implementer-model-tier.test.mjs +643 -0
  105. package/scripts/inflight-citation-prose.test.mjs +144 -0
  106. package/scripts/inflight-exit2-prose.test.mjs +81 -0
  107. package/scripts/inflight-probe-guard-prose.test.mjs +109 -0
  108. package/scripts/inflight-scan-scope-prose.test.mjs +57 -0
  109. package/scripts/inflight.sh +1106 -0
  110. package/scripts/inflight.test.mjs +2984 -0
  111. package/scripts/injection-control-prose.test.mjs +404 -0
  112. package/scripts/install-root-audit.test.mjs +464 -0
  113. package/scripts/instrument-check-prose.test.mjs +235 -0
  114. package/scripts/instruments.sh +394 -0
  115. package/scripts/instruments.test.mjs +787 -0
  116. package/scripts/issue-tracker-prose.test.mjs +71 -0
  117. package/scripts/json.sh +248 -0
  118. package/scripts/json.test.mjs +364 -0
  119. package/scripts/ledger-dispatch.test.mjs +516 -0
  120. package/scripts/ledger-grammar.mjs +85 -0
  121. package/scripts/ledger-grammar.test.mjs +142 -0
  122. package/scripts/ledger-read-require-file.test.mjs +197 -0
  123. package/scripts/ledger.mjs +1753 -0
  124. package/scripts/ledger.test.mjs +3554 -0
  125. package/scripts/line-distance-prose.test.mjs +245 -0
  126. package/scripts/liveness-rationale-prose.test.mjs +83 -0
  127. package/scripts/locale-pin-prose.test.mjs +189 -0
  128. package/scripts/member-outcome-prose.test.mjs +167 -0
  129. package/scripts/member-outcomes-header.test.mjs +148 -0
  130. package/scripts/member-outcomes-prose.test.mjs +63 -0
  131. package/scripts/member-outcomes.mjs +231 -0
  132. package/scripts/member-outcomes.test.mjs +507 -0
  133. package/scripts/member-prompt-prose.test.mjs +161 -0
  134. package/scripts/member-record.mjs +605 -0
  135. package/scripts/member-record.test.mjs +516 -0
  136. package/scripts/member-write-guard.mjs +279 -0
  137. package/scripts/member-write-guard.test.mjs +211 -0
  138. package/scripts/merge-commit-probe-prose.test.mjs +122 -0
  139. package/scripts/merge-gate.mjs +262 -0
  140. package/scripts/merge-gate.test.mjs +613 -0
  141. package/scripts/muted-git-guard-sweep.test.mjs +493 -0
  142. package/scripts/net-ssh-precedence-prose.test.mjs +91 -0
  143. package/scripts/net-watchdog-prose.test.mjs +79 -0
  144. package/scripts/net.sh +409 -0
  145. package/scripts/net.test.mjs +211 -0
  146. package/scripts/no-ci-gate-prose.test.mjs +108 -0
  147. package/scripts/no-undo-audit.sh +1134 -0
  148. package/scripts/no-undo-audit.test.mjs +3965 -0
  149. package/scripts/no-wave.test.mjs +138 -0
  150. package/scripts/node-floor-sweep.test.mjs +611 -0
  151. package/scripts/pointer-target-prose.test.mjs +186 -0
  152. package/scripts/pr-overlap.mjs +417 -0
  153. package/scripts/pr-overlap.test.mjs +695 -0
  154. package/scripts/printf-die-sweep.test.mjs +384 -0
  155. package/scripts/prompt-renderer.mjs +39 -0
  156. package/scripts/prompt-renderer.test.mjs +68 -0
  157. package/scripts/prose-pin.mjs +808 -0
  158. package/scripts/prose-pin.test.mjs +1034 -0
  159. package/scripts/prove-merge.sh +228 -0
  160. package/scripts/prove-merge.test.mjs +995 -0
  161. package/scripts/pull-table-relabel-prose.test.mjs +60 -0
  162. package/scripts/quiet-payload-prose.test.mjs +222 -0
  163. package/scripts/reap.sh +1768 -0
  164. package/scripts/reap.test.mjs +4395 -0
  165. package/scripts/reaping-prose.test.mjs +159 -0
  166. package/scripts/refuter-scratch-prose.test.mjs +135 -0
  167. package/scripts/release-label-required-prose.test.mjs +71 -0
  168. package/scripts/release-label-split-prose.test.mjs +169 -0
  169. package/scripts/release-ticket.sh +1716 -0
  170. package/scripts/release-ticket.test.mjs +5106 -0
  171. package/scripts/renovate-release-contract.test.mjs +379 -0
  172. package/scripts/repo-root.mjs +544 -0
  173. package/scripts/repo-root.test.mjs +873 -0
  174. package/scripts/reporter-output-rule-prose.test.mjs +208 -0
  175. package/scripts/review-agent-report-only.test.mjs +49 -0
  176. package/scripts/review-core-cwd-isolation.test.mjs +374 -0
  177. package/scripts/review-core-reads.test.mjs +478 -0
  178. package/scripts/review-core-refuter-scratch.test.mjs +148 -0
  179. package/scripts/review-core-snapshot-path.test.mjs +777 -0
  180. package/scripts/review-core-specialist-scratch.test.mjs +189 -0
  181. package/scripts/review-core-testcmd.test.mjs +288 -0
  182. package/scripts/review-core-unrun.test.mjs +530 -0
  183. package/scripts/review-core-unverified-discriminant.test.mjs +208 -0
  184. package/scripts/review-core.mjs +1028 -0
  185. package/scripts/review-eval-resolver-path.test.mjs +55 -0
  186. package/scripts/review-eval.mjs +197 -0
  187. package/scripts/review-host-fixture.mjs +73 -0
  188. package/scripts/review-in-run-retry.test.mjs +187 -0
  189. package/scripts/review-no-vendor-dispatch.test.mjs +45 -0
  190. package/scripts/review-path-default.test.mjs +796 -0
  191. package/scripts/review-runner-spawns.test.mjs +93 -0
  192. package/scripts/review-runner.test.mjs +155 -0
  193. package/scripts/review-tier-audit.test.mjs +140 -0
  194. package/scripts/run-artifact-pr-prose.test.mjs +341 -0
  195. package/scripts/run-merge-bot-prose.test.mjs +1391 -0
  196. package/scripts/scratch-partition-invariant-prose.test.mjs +74 -0
  197. package/scripts/scripts-path-citation-sweep.test.mjs +73 -0
  198. package/scripts/section-between-guard-prose.test.mjs +378 -0
  199. package/scripts/select-dimensions.test.mjs +674 -0
  200. package/scripts/shared-refusal.test.mjs +388 -0
  201. package/scripts/shell-traps-prose.test.mjs +507 -0
  202. package/scripts/shortlist.mjs +355 -0
  203. package/scripts/shortlist.test.mjs +366 -0
  204. package/scripts/slow-transport.mjs +72 -0
  205. package/scripts/snapshot-repo.test.mjs +772 -0
  206. package/scripts/snapshot-runner-audience-prose.test.mjs +129 -0
  207. package/scripts/spend-view.test.mjs +312 -0
  208. package/scripts/staleness-qualifier-prose.test.mjs +251 -0
  209. package/scripts/staleness-verdict-prose.test.mjs +142 -0
  210. package/scripts/staleness.mjs +414 -0
  211. package/scripts/staleness.test.mjs +893 -0
  212. package/scripts/stalled-member-liveness-prose.test.mjs +219 -0
  213. package/scripts/stash-prohibition-prose.test.mjs +313 -0
  214. package/scripts/strip-comments.mjs +44 -0
  215. package/scripts/tier-check.mjs +468 -0
  216. package/scripts/tier-check.test.mjs +726 -0
  217. package/scripts/tier-guard-gate-prose.test.mjs +216 -0
  218. package/scripts/tier-outcomes-header.test.mjs +586 -0
  219. package/scripts/tier-outcomes.mjs +441 -0
  220. package/scripts/tier-outcomes.test.mjs +342 -0
  221. package/scripts/tier-roles.mjs +250 -0
  222. package/scripts/tier-roles.test.mjs +316 -0
  223. package/scripts/tracker-block-copy-prose.test.mjs +288 -0
  224. package/scripts/triage-labels-gloss-prose.test.mjs +97 -0
  225. package/scripts/unattended-git-sweep.test.mjs +142 -0
  226. package/scripts/verify-sha-exit2-prose.test.mjs +269 -0
  227. package/scripts/verify-sha.sh +117 -0
  228. package/scripts/verify-sha.test.mjs +693 -0
  229. package/scripts/watcher-degraded-block.test.mjs +372 -0
  230. package/scripts/watcher-key-prose.test.mjs +106 -0
  231. package/scripts/within-run-pair-prose.test.mjs +208 -0
  232. package/scripts/worktree-audit.sh +482 -0
  233. package/scripts/worktree-audit.test.mjs +1407 -0
  234. package/scripts/worktree-head-prose.test.mjs +276 -0
  235. package/scripts/worktree-listing-sweep.test.mjs +217 -0
  236. package/scripts/worktree-model-prose.test.mjs +216 -0
  237. package/scripts/worktree.sh +470 -0
  238. package/skills/next-ticket/SKILL.md +138 -0
  239. package/skills/run-team/SKILL.md +3603 -0
  240. package/skills/run-team/references/ci-and-staleness.md +75 -0
  241. package/skills/run-team/references/correction-tickets.md +37 -0
  242. package/skills/run-team/references/isolation.md +23 -0
  243. package/skills/run-team/references/member-lifecycle.md +73 -0
  244. package/skills/run-team/references/reaping.md +73 -0
  245. package/skills/sizing-a-ticket/SKILL.md +39 -0
@@ -0,0 +1,373 @@
1
+ // The shared state module — #357. Both of its callers have a CLI test file of
2
+ // their own, and almost everything this module does shows through one of them:
3
+ // the corrupt-file announcements, the per-field validation, the patch write, the
4
+ // failed-write return value.
5
+ //
6
+ // One thing does not, and it is a contract rather than an output: `rest` is the
7
+ // fields OUTSIDE the schema, never a second unvalidated copy of the fields
8
+ // inside it. Two copies of one key — one sanitized, one raw — is a trap for the
9
+ // next caller (#1597's `beat` would have arrived in `rest` had it not been
10
+ // destructured out), and it leaves
11
+ // writeState's key ORDER as the only thing standing between a junk value and
12
+ // the disk. No CLI output differs on that, so it is pinned here.
13
+
14
+ import test from "node:test";
15
+ import assert from "node:assert/strict";
16
+ import { spawnSync } from "node:child_process";
17
+ import { mkdtempSync, mkdirSync, writeFileSync, readFileSync, rmSync, symlinkSync } from "node:fs";
18
+ import { tmpdir } from "node:os";
19
+ import { join } from "node:path";
20
+ import { fileURLToPath } from "node:url";
21
+
22
+ import { readState, writeState, assessBeat, isStalled, stallReport, BEAT_GRACE, DEFAULT_CEILING_S } from "./fleet-state.mjs";
23
+
24
+ const SCRIPT = fileURLToPath(new URL("./fleet-state.mjs", import.meta.url));
25
+
26
+ test("readState: `rest` is what lies outside the schema, never a second copy of it", () => {
27
+ const dir = mkdtempSync(join(tmpdir(), "fleet-state-"));
28
+ const path = join(dir, "heartbeat.json");
29
+ writeFileSync(path, JSON.stringify({ quiet: "bogus", elapsed: 5, digest: "abc", note: "not ours" }));
30
+ const state = readState(path, "fleet-state-test");
31
+ rmSync(dir, { recursive: true, force: true });
32
+
33
+ // The validated view, per field: a junk `quiet` sanitizes to 0 without taking
34
+ // the good `elapsed` down with it.
35
+ assert.equal(state.quiet, 0);
36
+ assert.equal(state.elapsed, 5);
37
+ assert.equal(state.digest, "abc");
38
+ // And `rest` carries the foreign key alone. A raw `quiet: "bogus"` sitting in
39
+ // here is the value writeState would spread back onto the disk — the script
40
+ // that owns neither key re-persisting a fault the read had already repaired.
41
+ assert.deepEqual(state.rest, { note: "not ours" });
42
+ });
43
+
44
+ test("statePath: an ambient GIT_COMMON_DIR is not canonicalised — this caller does not opt in (#1658)", () => {
45
+ // git-env.mjs's own docstring: `canonicalise` is board.mjs's opt-in alone
46
+ // (#1582, a symlinked cockpit route deriving a second port); ledger.mjs and
47
+ // fleet-state.mjs stay on the default because both PRINT the path they
48
+ // resolve, and realpath would change that output without changing which
49
+ // file either reaches. board.test.mjs pins board.mjs's OPPOSITE choice;
50
+ // git-env.test.mjs pins the HELPER's own default, which cannot see whether
51
+ // THIS caller opted in — nothing before this test pinned that fact, so an
52
+ // accidental `{ canonicalise: true }` added here would break no test.
53
+ //
54
+ // statePath() shells out to real `git`, which resolves a symlinked cwd
55
+ // itself (the OS's own getcwd() never carries a symlink component), so the
56
+ // vector has to be an ambient answer git echoes back verbatim instead:
57
+ // GIT_COMMON_DIR, which statePath()'s scrub does not touch (only GIT_DIR
58
+ // and GIT_WORK_TREE are). Measured directly: `GIT_COMMON_DIR=<symlinked
59
+ // path>/.git git rev-parse --git-common-dir` from inside a repository
60
+ // answers with that exact string, unresolved.
61
+ const root = mkdtempSync(join(tmpdir(), "fleet-state-link-"));
62
+ try {
63
+ const real = join(root, "repo");
64
+ assert.equal(spawnSync("git", ["init", "-q", real], { stdio: "ignore" }).status, 0);
65
+ const link = join(root, "link");
66
+ symlinkSync(real, link);
67
+ // A plain repository to run FROM, not this suite's own (linked) worktree:
68
+ // a GIT_COMMON_DIR override from inside a linked worktree collides with
69
+ // that worktree's own admin files (measured: git then reports "not a git
70
+ // repository", expecting GIT_DIR to name the worktree too).
71
+ const cwdRepo = join(root, "cwd-repo");
72
+ mkdirSync(cwdRepo, { recursive: true });
73
+ assert.equal(spawnSync("git", ["init", "-q"], { cwd: cwdRepo, stdio: "ignore" }).status, 0);
74
+ const r = spawnSync(process.execPath,
75
+ ["-e", 'import(process.argv[1]).then((m) => { process.stdout.write(m.statePath("fleet-state-test")); });', SCRIPT],
76
+ { cwd: cwdRepo, encoding: "utf8", env: { ...process.env, GIT_COMMON_DIR: join(link, ".git") } });
77
+ assert.equal(r.status, 0, r.stderr);
78
+ assert.equal(r.stdout, join(link, ".fleet", "heartbeat.json"),
79
+ "statePath() must keep the symlinked spelling — canonicalising here is board.mjs's opt-in alone");
80
+ } finally { rmSync(root, { recursive: true, force: true }); }
81
+ });
82
+
83
+ // --------------------------------------------------------------------------
84
+ // The liveness mark — #1597. `beat` is fleet-heartbeat's key and fleet-tick
85
+ // and board.mjs are its readers, so the contract that matters is what a
86
+ // READER is handed for a file it did not write, and what a NON-OWNER's write
87
+ // leaves behind. Both are invisible to either CLI's own output.
88
+
89
+ test("readState: a half-written mark is no mark, never a stale one", () => {
90
+ // The failure DIRECTION is the whole point. A mark is a time judged against
91
+ // the interval it was promised at, so a record carrying one without the
92
+ // other cannot be judged at all — and the two ways of pretending otherwise
93
+ // are both worse than saying nothing. Defaulting the missing interval to 0
94
+ // makes every mark instantly overdue, which reports a live fleet as dead;
95
+ // defaulting the missing time to now makes a dead one look fresh forever.
96
+ const dir = mkdtempSync(join(tmpdir(), "fleet-state-beat-"));
97
+ const path = join(dir, "heartbeat.json");
98
+ const read = (beat) => {
99
+ writeFileSync(path, JSON.stringify({ quiet: 0, elapsed: 0, digest: "", beat }));
100
+ return readState(path, "fleet-state-test").beat;
101
+ };
102
+ assert.equal(read({ at: 1_700_000_000_000 }), null, "a time with no interval cannot be judged");
103
+ assert.equal(read({ interval: 300 }), null, "an interval with no time is not a sighting");
104
+ assert.equal(read({ at: 0, interval: 300 }), null, "the epoch is not a beat");
105
+ assert.equal(read("1700000000000"), null, "a scalar is not a mark");
106
+ assert.equal(read([1, 2]), null, "an array is not a mark");
107
+ // But a junk REASON does not take the mark down with it: the reason is the
108
+ // one field whose absence already has a meaning the readers state out loud
109
+ // ("stopped without a recorded reason"), so degrading to it loses nothing.
110
+ assert.deepEqual(read({ at: 1_700_000_000_000, interval: 300, stopped: 7 }),
111
+ { at: 1_700_000_000_000, interval: 300, stopped: "" });
112
+ rmSync(dir, { recursive: true, force: true });
113
+ });
114
+
115
+ test("readState: a junk mark does not ride back to disk through `rest`", () => {
116
+ // The same trap the file's header names for quiet/elapsed/digest, one key
117
+ // later. If `beat` were left in `rest`, fleet-tick's streak write would
118
+ // spread the RAW record straight back over the sanitized one, and a mark
119
+ // this read repaired would survive every write that repaired it.
120
+ const dir = mkdtempSync(join(tmpdir(), "fleet-state-beat-rest-"));
121
+ const path = join(dir, "heartbeat.json");
122
+ writeFileSync(path, JSON.stringify({ quiet: 1, elapsed: 0, digest: "d", beat: { at: "soon" }, note: "not ours" }));
123
+ const state = readState(path, "fleet-state-test");
124
+ assert.equal(state.beat, null);
125
+ assert.deepEqual(state.rest, { note: "not ours" }, "`beat` must be destructured out, not left in `rest`");
126
+ rmSync(dir, { recursive: true, force: true });
127
+ });
128
+
129
+ test("writeState: a non-owner's patch carries the mark instead of erasing it", () => {
130
+ // One writer per key is only true if the OTHER writers preserve it.
131
+ // fleet-tick patches quiet/digest on every tick — every five minutes on a
132
+ // busy run — so a write that dropped `beat` would delete the heartbeat's
133
+ // mark almost immediately, and the absence is indistinguishable from a run
134
+ // that never beat: readers would go silent on exactly the run that died.
135
+ const dir = mkdtempSync(join(tmpdir(), "fleet-state-beat-carry-"));
136
+ const path = join(dir, "heartbeat.json");
137
+ const beat = { at: 1_700_000_000_000, interval: 600, stopped: "" };
138
+ writeFileSync(path, JSON.stringify({ quiet: 2, elapsed: 5, digest: "old", beat }));
139
+ const prev = readState(path, "fleet-state-test");
140
+ assert.equal(writeState(path, "fleet-state-test", prev, { quiet: 0, digest: "new" }), true);
141
+ const after = readState(path, "fleet-state-test");
142
+ assert.deepEqual(after.beat, beat, "fleet-tick's streak write erased the heartbeat's mark");
143
+ assert.equal(after.quiet, 0);
144
+ assert.equal(after.digest, "new");
145
+
146
+ // And the absent mark is OMITTED rather than written as an explicit null,
147
+ // so a file that has never been beaten into stays the shape it was.
148
+ writeFileSync(path, JSON.stringify({ quiet: 0, elapsed: 0, digest: "" }));
149
+ const fresh = readState(path, "fleet-state-test");
150
+ assert.equal(writeState(path, "fleet-state-test", fresh, { quiet: 1 }), true);
151
+ assert.equal("beat" in JSON.parse(readFileSync(path, "utf8")), false);
152
+ rmSync(dir, { recursive: true, force: true });
153
+ });
154
+
155
+ test("writeState: `ticked` survives a heartbeat write the same way `beat` survives a tick write", () => {
156
+ // The mirror of the test above, other direction: fleet-heartbeat patches
157
+ // elapsed/beat on every hold, so a write that dropped `ticked` would erase
158
+ // fleet-tick's own liveness key the first time a heartbeat lands after a
159
+ // busy stretch — silently reopening the #1597 follow-up this key exists to
160
+ // close.
161
+ const dir = mkdtempSync(join(tmpdir(), "fleet-state-ticked-carry-"));
162
+ const path = join(dir, "heartbeat.json");
163
+ const ticked = { at: 1_700_000_500_000 };
164
+ writeFileSync(path, JSON.stringify({ quiet: 0, elapsed: 0, digest: "", ticked }));
165
+ const prev = readState(path, "fleet-state-test");
166
+ assert.deepEqual(prev.ticked, ticked, "readState did not parse a well-formed ticked mark");
167
+ const beat = { at: 1_700_000_000_000, interval: 300, stopped: "" };
168
+ assert.equal(writeState(path, "fleet-state-test", prev, { elapsed: 5, beat }), true);
169
+ const after = readState(path, "fleet-state-test");
170
+ assert.deepEqual(after.ticked, ticked, "fleet-heartbeat's mark write erased fleet-tick's ticked key");
171
+ assert.deepEqual(after.beat, beat);
172
+
173
+ // A junk `ticked` degrades to absent, never to a false zero: `at: 0` is a
174
+ // decades-old occurrence, not "no occurrence", and would falsely rescue a
175
+ // stale beat forever.
176
+ writeFileSync(path, JSON.stringify({ quiet: 0, elapsed: 0, digest: "", ticked: { at: "soon" } }));
177
+ assert.equal(readState(path, "fleet-state-test").ticked, null);
178
+ rmSync(dir, { recursive: true, force: true });
179
+ });
180
+
181
+ test("assessBeat: staleness is judged against the RECORDED interval, not a constant", () => {
182
+ // The whole reason the mark carries an interval. A quiet night backs off to
183
+ // the ceiling, so the same twenty-minute silence is a healthy beat at
184
+ // --ceiling 1200 and a dead run at --base 300. A fixed threshold has to
185
+ // pick one and be wrong about the other, every night.
186
+ const now = 2_000_000_000_000;
187
+ const at = now - 20 * 60 * 1000;
188
+ assert.equal(assessBeat({ beat: { at, interval: 1200, stopped: "" }, now }).kind, "beating",
189
+ "a quiet run at the back-off ceiling must not be reported as dead");
190
+ assert.equal(assessBeat({ beat: { at, interval: 300, stopped: "" }, now }).kind, "stale",
191
+ "the same silence at the base interval IS overdue");
192
+
193
+ // The grace is a multiplier on the promise, so the boundary moves with it.
194
+ // Pinned on both sides of the edge rather than at one point: a check that
195
+ // only ever asserts "far past" passes for any threshold at all.
196
+ const edge = 300 * 1000 * BEAT_GRACE;
197
+ assert.equal(assessBeat({ beat: { at: now - edge, interval: 300, stopped: "" }, now }).kind, "beating");
198
+ assert.equal(assessBeat({ beat: { at: now - edge - 1, interval: 300, stopped: "" }, now }).kind, "stale");
199
+
200
+ // No mark is not a stall: a fresh run legitimately has none, and a banner
201
+ // that fires on every first tick is a banner nobody reads by the second.
202
+ assert.equal(assessBeat({ beat: null, now }).kind, "none");
203
+ assert.equal(isStalled(assessBeat({ beat: null, now })), false);
204
+ // A mark from the future is a clock that moved, not a negative overdue.
205
+ const ahead = assessBeat({ beat: { at: now + 60_000, interval: 300, stopped: "" }, now });
206
+ assert.equal(ahead.ageMs, 0);
207
+ assert.equal(ahead.overdueMs, 0);
208
+ });
209
+
210
+ test("assessBeat: a busy run's own `ticked` covers for a `beat` the busy stretch never let refresh", () => {
211
+ // #1597 follow-up. `beat` only refreshes when the queue drains ("beat when
212
+ // there is nothing to do") — a fully-staffed fleet that has been busy for
213
+ // eleven straight minutes never touches it, so the recorded interval stays
214
+ // whatever it was when the stretch started (base, if it started right after a
215
+ // dispatch) and the OLD mark ages straight past its own grace window. A
216
+ // reconcile tick fires on every completion during that same stretch, though,
217
+ // and now leaves its own mark behind — that is the evidence this asserts.
218
+ const now = 2_000_000_000_000;
219
+ const beat = { at: now - 11 * 60 * 1000, interval: 300, stopped: "" };
220
+ assert.equal(assessBeat({ beat, now }).kind, "stale",
221
+ "no ticked evidence at all is exactly the #1597 bug — still correctly overdue");
222
+ assert.equal(assessBeat({ beat, ticked: { at: now - 2 * 60 * 1000 }, now }).kind, "beating",
223
+ "a tick two minutes ago is a busy run working, not a dead one");
224
+
225
+ // The reverse never happens: a stale `ticked` cannot rescue a genuinely
226
+ // dead run, and a healthy `beat` needs no help from `ticked` at all.
227
+ assert.equal(assessBeat({ beat, ticked: { at: now - 3 * DEFAULT_CEILING_S * 1000 }, now }).kind, "stale");
228
+ const healthyBeat = { at: now - 60 * 1000, interval: 300, stopped: "" };
229
+ assert.equal(assessBeat({ beat: healthyBeat, now }).kind, "beating");
230
+
231
+ // `stopped` still outranks everything, ticked included: a run that named
232
+ // its own death is not un-dead because fleet-tick happened to fire once on
233
+ // the way out.
234
+ const stopped = { at: now - 11 * 60 * 1000, interval: 300, stopped: "budget" };
235
+ assert.equal(assessBeat({ beat: stopped, ticked: { at: now - 1000 }, now }).kind, "stopped");
236
+ });
237
+
238
+ test("assessBeat: a recorded stop is reported whatever its age, and outranks staleness", () => {
239
+ // A stop that had to wait two intervals to be believed is a stop reported
240
+ // after the only window in which anyone could act on it — and it is the one
241
+ // verdict that knows its own cause, so it is the last one to sit on.
242
+ const now = 2_000_000_000_000;
243
+ const fresh = assessBeat({ beat: { at: now - 1000, interval: 300, stopped: "budget" }, now });
244
+ assert.equal(fresh.kind, "stopped");
245
+ assert.equal(isStalled(fresh), true);
246
+ assert.equal(fresh.reason, "budget");
247
+ // And an OLD stop stays `stopped`, never degrading into the reasonless
248
+ // wording — the cause does not expire just because the silence grew.
249
+ assert.equal(assessBeat({ beat: { at: now - 86_400_000, interval: 300, stopped: "budget" }, now }).kind, "stopped");
250
+ });
251
+
252
+ test("stallReport: names the four facts, and says `unknown` rather than guessing a zero", () => {
253
+ const now = 2_000_000_000_000;
254
+ const at = now - 90 * 60 * 1000;
255
+ const stale = assessBeat({ beat: { at, interval: 1200, stopped: "" }, now });
256
+ const line = stallReport(stale, { claimed: 7, supply: 3 });
257
+ // When it was last seen, how overdue against the interval it PROMISED, what
258
+ // is claimed, and whether there is still supply. A bare "stale" tells the
259
+ // maintainer nothing they can act on, which is why each is asserted rather
260
+ // than the line merely being non-empty.
261
+ assert.match(line, /last beat 2033-05-18T02:03:20\.000Z/);
262
+ assert.match(line, /90m ago/);
263
+ assert.match(line, /70m past the 20m interval it promised/);
264
+ assert.match(line, /7 ticket\(s\) claimed and in flight/);
265
+ assert.match(line, /pool supply 3/);
266
+ // No reason recorded: say exactly that, never a cause it cannot know.
267
+ assert.match(line, /stopped without a recorded reason/);
268
+
269
+ const stopped = assessBeat({ beat: { at, interval: 1200, stopped: "context ceiling reached" }, now });
270
+ assert.match(stallReport(stopped, { claimed: 0, supply: 0 }),
271
+ /stopped deliberately — recorded reason: context ceiling reached/);
272
+
273
+ // Unknown is not zero. A failed claim query reporting "0 claimed" says the
274
+ // dead run stranded nothing, which is the one answer that makes the whole
275
+ // report safe to ignore — the same refusal fleet-tick's own NOT_ZERO wording
276
+ // makes about a pool it could not read.
277
+ const unknown = stallReport(stale, { claimed: null, supply: null });
278
+ assert.match(unknown, /unknown ticket\(s\) claimed and in flight/);
279
+ assert.match(unknown, /pool supply unknown/);
280
+ assert.doesNotMatch(unknown, /\b0 ticket/);
281
+
282
+ // And a healthy verdict has no report at all, so a caller that skipped
283
+ // isStalled() cannot print a death for a beating run by accident.
284
+ assert.equal(stallReport(assessBeat({ beat: { at: now, interval: 300, stopped: "" }, now }), { claimed: 1, supply: 1 }), null);
285
+ assert.equal(stallReport(assessBeat({ beat: null, now }), { claimed: 1, supply: 1 }), null);
286
+ });
287
+
288
+ test("stallReport: a sub-60s interval renders as seconds, not a floored 0m", () => {
289
+ // #1735: mins() floors to whole minutes, and the CLI guard for --base/
290
+ // --ceiling only requires >= 1 (fleet-heartbeat.test.mjs relies on that —
291
+ // its CLI cases use 2s/3s/8s intervals throughout to keep real
292
+ // Atomics.wait holds fast), so a sub-minute mark is a real, reachable
293
+ // shape here, not an input this module gets to refuse. Flooring it to
294
+ // "0m" read as a division-by-zero defect rather than the true value.
295
+ const now = 2_000_000_000_000;
296
+ const at = now - 45_000;
297
+ const verdict = assessBeat({ beat: { at, interval: 30, stopped: "smoke test" }, now });
298
+ const line = stallReport(verdict, { claimed: 0, supply: 0 });
299
+ assert.match(line, /45s ago/);
300
+ assert.match(line, /15s past the 30s interval it promised/);
301
+ assert.doesNotMatch(line, /\b0m\b/);
302
+
303
+ // The boundary itself: exactly 60s reports as "1m", never "60s" — mins()
304
+ // has exactly one branch point and this pins which side 60000ms falls on.
305
+ const boundary = assessBeat({ beat: { at: now - 60_000, interval: 30, stopped: "smoke test" }, now });
306
+ assert.match(stallReport(boundary, { claimed: 0, supply: 0 }), /1m ago/);
307
+ });
308
+
309
+ test("stallReport: a 60-119s band value does not silently break the trio's arithmetic (#1797)", () => {
310
+ // #1735 only moved the floor for values UNDER 60s. mins() still floored
311
+ // every value >= 60s to whole minutes independently of the other two, so
312
+ // an interval landing in 60-119s (a real, CLI-reachable shape — see the
313
+ // comment above mins()) floored to "1m" or "2m" while its own overdue
314
+ // remainder kept printing in seconds, and the three displayed numbers
315
+ // stopped agreeing with each other. These are the exact reviewer repros.
316
+ const now = 2_000_000_000_000;
317
+ const line = (ageS, intervalS) =>
318
+ stallReport(
319
+ assessBeat({ beat: { at: now - ageS * 1000, interval: intervalS, stopped: "smoke test" }, now }),
320
+ { claimed: 0, supply: 0 },
321
+ );
322
+
323
+ // interval=90s, age=100s: overdue=10s. Old output read "1m ago ... 10s
324
+ // past the 1m interval" — 1m minus 1m is 0, not 10s.
325
+ assert.match(line(100, 90), /\(1m40s ago, 10s past the 1m30s interval it promised\)/);
326
+
327
+ // interval=90s, age=200s: overdue=110s. Old output read "3m ago ... 1m
328
+ // past the 1m interval" — 3m minus 1m is 2m, not 1m.
329
+ assert.match(line(200, 90), /\(3m20s ago, 1m50s past the 1m30s interval it promised\)/);
330
+
331
+ // interval=120s, age=150s: overdue=30s. Old output happened to print "2m
332
+ // ago ... 30s past the 2m interval" only because 120s floors to an exact
333
+ // "2m" with nothing dropped — fragile, not a fix, and now stated to full
334
+ // precision like every other case in this band.
335
+ assert.match(line(150, 120), /\(2m30s ago, 30s past the 2m interval it promised\)/);
336
+ });
337
+
338
+ test("stallReport: age always reconciles to overdue+interval, across every unit boundary the CLI can produce", () => {
339
+ // Property check standing in for the two hand-picked repros above: for a
340
+ // spread of intervals that straddle every unit boundary mins() has (under
341
+ // 60s, the 60-119s band, and clean multi-minute values), and a spread of
342
+ // deltas layered on top so age also straddles those same boundaries, the
343
+ // three numbers stallReport prints must parse back to millisecond figures
344
+ // that satisfy overdue + interval === age — never merely "doesn't crash".
345
+ const now = 2_000_000_000_000;
346
+ const parseDur = (s) => {
347
+ const m = /^(?:(\d+)m)?(?:(\d+)s)?$/.exec(s);
348
+ assert.ok(m, `unparseable duration: ${s}`);
349
+ return ((m[1] ? Number(m[1]) : 0) * 60 + (m[2] ? Number(m[2]) : 0)) * 1000;
350
+ };
351
+ const lineShape = /\((\S+) ago, (\S+) past the (\S+) interval it promised\)/;
352
+
353
+ const intervals = [30, 60, 90, 100, 119, 120, 150, 300];
354
+ const deltas = [1, 15, 29, 30, 31, 59, 60, 61, 89, 90, 91, 119, 120, 121, 200, 599, 3600];
355
+ for (const intervalS of intervals) {
356
+ for (const delta of deltas) {
357
+ const ageS = intervalS + delta;
358
+ const verdict = assessBeat({ beat: { at: now - ageS * 1000, interval: intervalS, stopped: "smoke test" }, now });
359
+ const line = stallReport(verdict, { claimed: 0, supply: 0 });
360
+ const m = lineShape.exec(line);
361
+ assert.ok(m, `line did not match expected shape: ${line}`);
362
+ const [, ageStr, overdueStr, intervalStr] = m;
363
+ const ageMs = parseDur(ageStr);
364
+ const overdueMs = parseDur(overdueStr);
365
+ const intervalMs = parseDur(intervalStr);
366
+ assert.equal(
367
+ overdueMs + intervalMs,
368
+ ageMs,
369
+ `interval=${intervalS}s age=${ageS}s: "${overdueStr}" + "${intervalStr}" != "${ageStr}" (line: ${line})`,
370
+ );
371
+ }
372
+ }
373
+ });