@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,408 @@
1
+ // Regression gate for the diff-sizing classifier and profile ladder that scale a
2
+ // review's fan-out. Zero deps: `node --test plugin/scripts/diff-stats.test.mjs`.
3
+ // Locks the load-bearing behaviours a "simplification" could silently break —
4
+ // especially that code under docs/ or .github/ keeps `src`, and that a mixed
5
+ // docs+src PR is NOT docsOnly (so it keeps the fuller review).
6
+
7
+ import { test } from "node:test";
8
+ import assert from "node:assert/strict";
9
+ import { readFileSync, mkdtempSync, writeFileSync, chmodSync, rmSync } from "node:fs";
10
+ import { tmpdir } from "node:os";
11
+ import { join } from "node:path";
12
+ import { spawnSync } from "node:child_process";
13
+ import { classify, computeStats } from "./diff-stats.mjs";
14
+
15
+ // `main()` runs only when this file is executed directly, so the CLI wiring is
16
+ // unreachable from a unit test. Pinned as source text instead — see the
17
+ // truncation test at the bottom for why it needs pinning at all.
18
+ const SCRIPT = join(import.meta.dirname, "diff-stats.mjs");
19
+ const CLI = readFileSync(SCRIPT, "utf8");
20
+
21
+ test("classifier priority: test > code-ext > docs/config-dir", () => {
22
+ // src is the residue
23
+ assert.equal(classify("src/a.ts"), "src");
24
+ assert.equal(classify("scripts/review-core.mjs"), "src");
25
+ // test wins even though a .test.ts is also a .ts
26
+ assert.equal(classify("a.test.ts"), "test");
27
+ assert.equal(classify("pkg/__tests__/a.ts"), "test");
28
+ // prose
29
+ assert.equal(classify("README.md"), "docs");
30
+ assert.equal(classify("docs/guide.md"), "docs");
31
+ // config by extension / location / naming convention
32
+ assert.equal(classify(".github/workflows/ci.yml"), "config");
33
+ assert.equal(classify("package.json"), "config");
34
+ assert.equal(classify("vite.config.ts"), "config");
35
+ assert.equal(classify(".eslintrc"), "config");
36
+ // M1: a code file stays `src` even under docs/ or .github/, so the src-gated
37
+ // dimensions (types, silent-failure, simplify) are not silently dropped on it.
38
+ assert.equal(classify("docs/examples/deploy.ts"), "src");
39
+ assert.equal(classify(".github/scripts/action.mjs"), "src");
40
+ assert.equal(classify("packages/x/docs/gen.mjs"), "src");
41
+ });
42
+
43
+ test("computeStats: profile ladder", () => {
44
+ assert.equal(computeStats([]).profile, "empty");
45
+ assert.equal(computeStats([{ path: "a.md", additions: 3, deletions: 1 }]).profile, "docs");
46
+ assert.equal(computeStats([{ path: "a.test.ts", additions: 5, deletions: 0 }]).profile, "tests-only");
47
+ assert.equal(computeStats([{ path: "a.ts", additions: 1, deletions: 0 }]).profile, "single-file");
48
+ assert.equal(
49
+ computeStats([
50
+ { path: "a.ts", additions: 2, deletions: 0 },
51
+ { path: "b.ts", additions: 2, deletions: 0 },
52
+ ]).profile,
53
+ "small",
54
+ );
55
+ assert.equal(
56
+ computeStats([
57
+ { path: "a.ts", additions: 20, deletions: 0 },
58
+ { path: "b.ts", additions: 20, deletions: 0 },
59
+ ]).profile,
60
+ "production",
61
+ );
62
+ });
63
+
64
+ test("computeStats: docsOnly is strict — docs+src keeps the fuller review", () => {
65
+ const docs = computeStats([{ path: "a.md", additions: 3, deletions: 1 }]);
66
+ assert.equal(docs.docsOnly, true);
67
+ assert.equal(docs.hasSrc, false);
68
+ assert.equal(docs.loc, 4);
69
+
70
+ const mixed = computeStats([
71
+ { path: "a.md", additions: 2, deletions: 0 },
72
+ { path: "b.ts", additions: 2, deletions: 0 },
73
+ ]);
74
+ assert.equal(mixed.docsOnly, false, "docs+src must not be docsOnly");
75
+ assert.equal(mixed.hasSrc, true);
76
+
77
+ // docs + a CI workflow is also not docsOnly (config present)
78
+ const docsPlusCi = computeStats([
79
+ { path: "a.md", additions: 2, deletions: 0 },
80
+ { path: ".github/workflows/ci.yml", additions: 1, deletions: 0 },
81
+ ]);
82
+ assert.equal(docsPlusCi.docsOnly, false);
83
+ assert.equal(docsPlusCi.hasConfig, true);
84
+ });
85
+
86
+ test("computeStats: the four routing booleans are pinned together on a production PR", () => {
87
+ // review-core.mjs selectDimensions routes on exactly hasSrc/hasTests/hasConfig/
88
+ // docsOnly. Assert all four at once so any single one flipping is caught here,
89
+ // not only transitively via the profile-ladder test.
90
+ const prod = computeStats([
91
+ { path: "src/a.ts", additions: 5, deletions: 0 },
92
+ { path: "a.test.ts", additions: 5, deletions: 0 },
93
+ { path: "package.json", additions: 1, deletions: 0 },
94
+ ]);
95
+ assert.deepEqual(
96
+ { hasSrc: prod.hasSrc, hasTests: prod.hasTests, hasConfig: prod.hasConfig, docsOnly: prod.docsOnly },
97
+ { hasSrc: true, hasTests: true, hasConfig: true, docsOnly: false },
98
+ );
99
+ });
100
+
101
+ test("classify: extensionless files fall to src (fail-open residue)", () => {
102
+ // The residue default keeps the src-gated dimensions (types/silent-failure/
103
+ // simplify) running on unknown files — the safe direction. Pin it so a future
104
+ // "tidy the residue" edit can't silently reroute unknowns to docs/config.
105
+ assert.equal(classify("Dockerfile"), "src");
106
+ assert.equal(classify("Makefile"), "src");
107
+ assert.equal(classify("bin/deploy"), "src");
108
+ assert.equal(classify("foo.config.mjs"), "config"); // config-by-name still wins
109
+ });
110
+
111
+ test("computeStats: loc tolerates a file missing additions/deletions", () => {
112
+ assert.equal(computeStats([{ path: "a.ts" }]).loc, 0);
113
+ assert.equal(computeStats([{ path: "a.ts", additions: 3 }]).loc, 3);
114
+ });
115
+
116
+ // `gh pr view --json files` pages at 100 and exits 0, so a 124-file PR arrives
117
+ // as a fully-formed 100-file measurement with nothing contradicting it —
118
+ // measured on microsoft/vscode#329568. `changedFiles` from the same query is the
119
+ // only disagreement available. Absent on every normal PR: `review-core.mjs` reads
120
+ // its PRESENCE as "widen the fan-out, this list is short", so a flag set when
121
+ // the counts agree would widen every review.
122
+ test("computeStats: truncated is set only when gh's file list is short", () => {
123
+ const files = Array.from({ length: 100 }, (_, i) => ({ path: `src/f${i}.ts`, additions: 1 }));
124
+ assert.equal(computeStats(files, 124).truncated, 124);
125
+ assert.equal(computeStats(files, 100).truncated, undefined, "counts agree — a complete list is not truncated");
126
+ assert.equal(computeStats(files).truncated, undefined, "gh omitted changedFiles — no cap can be inferred");
127
+ // A short list under-reports `files` too, so nothing else in the blob can
128
+ // stand in for the flag.
129
+ assert.equal(computeStats(files, 124).files, 100);
130
+ });
131
+
132
+ // `truncated` is computed from an argument, and nothing above proves the CLI
133
+ // ever passes one. Both tokens are needed and each disconnects the whole cap
134
+ // detection alone: drop `changedFiles` from the query and `info.changedFiles` is
135
+ // undefined, drop the second argument and the value never reaches computeStats.
136
+ // Either way every PR reports as complete, `review-core.mjs` keeps stamping its
137
+ // file list "and no others", and the tests above stay green against a copy
138
+ // nothing calls.
139
+ test("the CLI actually asks gh for changedFiles and passes it through", () => {
140
+ assert.match(
141
+ CLI,
142
+ /"pr", "view", String\(pr\), "--json", "files,changedFiles"/,
143
+ "the gh query no longer requests changedFiles — nothing can reveal the 100-file cap",
144
+ );
145
+ assert.match(
146
+ CLI,
147
+ /computeStats\(info\.files, info\.changedFiles\)/,
148
+ "the CLI computes stats without the uncapped count — `truncated` is never set in production",
149
+ );
150
+ });
151
+
152
+ // #169: `--pr` given with no value was already caught by `if (!pr) die(...)`
153
+ // below — `undefined` is falsy — so this was never a silent-widening site.
154
+ // Pin that it now refuses explicitly, by name, instead of falling through to
155
+ // the generic usage message; and that `--pr=5` (invisible to `indexOf`) is
156
+ // rejected the same way rather than reading as absent.
157
+ test("CLI: trailing --pr (no value) dies (exit 2) naming the flag", () => {
158
+ const r = spawnSync(process.execPath, [SCRIPT, "--pr"], { encoding: "utf8" });
159
+ assert.equal(r.status, 2);
160
+ assert.match(r.stderr, /--pr needs a value/);
161
+ });
162
+
163
+ test("CLI: --pr=5 form dies by name, not silently read as absent", () => {
164
+ const r = spawnSync(process.execPath, [SCRIPT, "--pr=5"], { encoding: "utf8" });
165
+ assert.equal(r.status, 2);
166
+ assert.match(r.stderr, /--pr needs a space-separated value/);
167
+ });
168
+
169
+ // The other two branches of the same guard. `--pr` is this script's only flag,
170
+ // so the next-flag case needs an unknown one to collide with — the guard is on
171
+ // the value's shape, not on the following flag being real.
172
+ test("CLI: --pr followed by another flag is rejected, not consumed as the PR ref", () => {
173
+ const r = spawnSync(process.execPath, [SCRIPT, "--pr", "--json"], { encoding: "utf8" });
174
+ assert.equal(r.status, 2);
175
+ assert.match(r.stderr, /--pr needs a value/);
176
+ });
177
+
178
+ test("CLI: --pr given an empty value dies naming the flag", () => {
179
+ const r = spawnSync(process.execPath, [SCRIPT, "--pr", ""], { encoding: "utf8" });
180
+ assert.equal(r.status, 2);
181
+ assert.match(r.stderr, /--pr needs a value/);
182
+ });
183
+
184
+ // The other half of arg()'s guard: every case above is a value it must
185
+ // REFUSE. Nothing above ever hands main() a well-formed value, so #367's
186
+ // shared arg() could return `undefined` for every accepted input and this
187
+ // file would stay green. `--pr 5` must reach gh and succeed.
188
+ // The fail-closed guard this file never exercised: `gh pr view --json
189
+ // files,changedFiles` can exit 0 with a body holding no `files` key at all
190
+ // (an unexpected `gh` output shape, or a proxy's JSON error envelope). Before
191
+ // this guard, `info.files` read as `undefined` and computeStats([]) silently
192
+ // reported `profile: "empty"` — exit 0, indistinguishable from a real empty
193
+ // PR, review-core.mjs WIDENS that to the full specialist set on the strength of
194
+ // a lie. The guard must refuse instead.
195
+ test("CLI: gh returning no files array dies (exit 2) rather than reporting a fabricated empty PR", () => {
196
+ const bin = mkdtempSync(join(tmpdir(), "diff-stats-bin-"));
197
+ const gh = join(bin, "gh");
198
+ writeFileSync(gh, '#!/bin/sh\necho \'{"changedFiles":1}\'\n');
199
+ chmodSync(gh, 0o755);
200
+ const r = spawnSync(process.execPath, [SCRIPT, "--pr", "5"], {
201
+ encoding: "utf8",
202
+ env: { ...process.env, PATH: `${bin}:${process.env.PATH}` },
203
+ });
204
+ rmSync(bin, { recursive: true, force: true });
205
+ assert.equal(r.status, 2, `a missing files array must refuse, not report an empty PR; got ${r.status} ${r.stdout}${r.stderr}`);
206
+ assert.equal(r.stdout.trim(), "", "exit 2 emits no payload — a payload here would be the fabricated measurement this guard exists to prevent");
207
+ assert.match(r.stderr, /gh returned no files array/);
208
+ });
209
+
210
+ // The one case the guard above still missed: `JSON.parse("null")` succeeds
211
+ // and returns `null`, not an object — so `info.files` threw a TypeError
212
+ // before `!Array.isArray(info.files)` ever ran, crashing at exit 1 with a raw
213
+ // stack trace instead of refusing at the named exit 2 this file's own
214
+ // comment says the guard exists to produce (#1307). The message is worded
215
+ // "no body" rather than reusing "no files array", so a reader chasing a
216
+ // missing key is not sent looking for a key that was never the problem —
217
+ // nothing came back at all.
218
+ test("CLI: gh returning a null body dies (exit 2) naming the missing body, not a TypeError", () => {
219
+ const bin = mkdtempSync(join(tmpdir(), "diff-stats-bin-"));
220
+ const gh = join(bin, "gh");
221
+ writeFileSync(gh, "#!/bin/sh\necho null\n");
222
+ chmodSync(gh, 0o755);
223
+ const r = spawnSync(process.execPath, [SCRIPT, "--pr", "5"], {
224
+ encoding: "utf8",
225
+ env: { ...process.env, PATH: `${bin}:${process.env.PATH}` },
226
+ });
227
+ rmSync(bin, { recursive: true, force: true });
228
+ assert.equal(r.status, 2, `a null body must refuse, not crash; got ${r.status} ${r.stdout}${r.stderr}`);
229
+ assert.equal(r.stdout.trim(), "", "exit 2 emits no payload");
230
+ assert.match(r.stderr, /gh returned no body/);
231
+ assert.doesNotMatch(r.stderr, /TypeError/, "a null body must not reach the raw TypeError this guard exists to prevent");
232
+ });
233
+
234
+ test("CLI: a well-formed --pr value is accepted and the CLI succeeds", () => {
235
+ const bin = mkdtempSync(join(tmpdir(), "diff-stats-bin-"));
236
+ const gh = join(bin, "gh");
237
+ writeFileSync(gh, '#!/bin/sh\necho \'{"files":[{"path":"a.ts","additions":1,"deletions":0}],"changedFiles":1}\'\n');
238
+ chmodSync(gh, 0o755);
239
+ const r = spawnSync(process.execPath, [SCRIPT, "--pr", "5"], {
240
+ encoding: "utf8",
241
+ env: { ...process.env, PATH: `${bin}:${process.env.PATH}` },
242
+ });
243
+ rmSync(bin, { recursive: true, force: true });
244
+ assert.equal(r.status, 0, r.stdout + r.stderr);
245
+ const payload = JSON.parse(r.stdout);
246
+ assert.equal(payload.pr, 5);
247
+ assert.equal(payload.profile, "single-file");
248
+ });
249
+
250
+ // #932: the non-JSON die above quoted `raw.trim().slice(0, 120)` with NO
251
+ // marker, so a 269 KB portal page and a 90-character one rendered identically —
252
+ // a reader could not tell gh's whole output from this script's clip of it. The
253
+ // length quoted is gh's, not this script's, which is the lie the marker closes.
254
+ // Nothing in this file reached that path at all before these four tests: the
255
+ // guards above cover a missing `files` key and a `null` body, both of which
256
+ // PARSE. A suite that never enters the mode cannot see the defect in it.
257
+ //
258
+ // DIRECTION, measured here rather than inherited (#638/PR #931 fixed the same
259
+ // unmarked clip in ledger.mjs by keeping the TAIL, and copying that here would
260
+ // keep the noise and discard the cause):
261
+ // - `run()` passes no `stdio`, so execFileSync forwards the child's stderr to
262
+ // this process's stderr and returns stdout ALONE. `raw` is gh's stdout, not
263
+ // its stderr — the stream-check below pins exactly that, and it is the whole
264
+ // of why ledger.mjs's tail rationale (a FATAL line behind seven warnings)
265
+ // does not transfer.
266
+ // - Real gh 2.100.0 never puts non-JSON on stdout: a missing PR, bad
267
+ // credentials and an intercepting proxy all report on stderr at exit 1,
268
+ // which `run()`'s catch takes, and stdout stays empty. Forcing a TTY, a
269
+ // pager and `GH_DEBUG=api` each left stdout pure JSON. A payload past
270
+ // execFileSync's 1 MiB maxBuffer throws ENOBUFS rather than returning a
271
+ // mid-JSON cut, so the one shape whose cause would sit at the tail cannot
272
+ // reach `raw` either.
273
+ // - So this path is only ever reached when something OTHER than gh owns that
274
+ // stdout — a wrapper, shim or portal interposed on it — and such a producer
275
+ // leads with its own message. Measured on real bytes: github.com's 404 body
276
+ // ends `</div>\n </body>\n</html>`, boilerplate every HTML page on earth
277
+ // shares, while its first 120 characters carry `<!DOCTYPE html>`; a wrapper
278
+ // that prints a notice and then delegates ends in `"changeType":"MODIFIED"}]}`,
279
+ // which reads as gh having answered perfectly and the parser being at fault.
280
+ // The head carries the cause on both, and the tail actively misleads on one.
281
+ const runWithGhStdout = (body) => {
282
+ const bin = mkdtempSync(join(tmpdir(), "diff-stats-bin-"));
283
+ const payload = join(bin, "payload");
284
+ writeFileSync(payload, body);
285
+ // `cat` of a file, not `echo` of an inlined string: the payloads below are
286
+ // real captured bytes, quotes and all, and must reach stdout unmangled.
287
+ writeFileSync(join(bin, "gh"), `#!/bin/sh\ncat "${payload}"\n`);
288
+ chmodSync(join(bin, "gh"), 0o755);
289
+ const r = spawnSync(process.execPath, [SCRIPT, "--pr", "5"], {
290
+ encoding: "utf8",
291
+ env: { ...process.env, PATH: `${bin}:${process.env.PATH}` },
292
+ });
293
+ rmSync(bin, { recursive: true, force: true });
294
+ return r;
295
+ };
296
+
297
+ // What the refusal actually quoted: everything after the em-dash to the end of
298
+ // stderr. `[\s\S]*` (not `.`, which excludes `\n`) reads a multi-line clip in
299
+ // full — a `.`-based capture stopped at the first newline and would pass a
300
+ // mutant that clips to gh's first line alone, unmarked, whenever that first
301
+ // line is itself under the cap (the exact #932 defect the fixture below pins).
302
+ const quoted = (stderr) => {
303
+ const m = /returned no JSON — ([\s\S]*)\n$/.exec(stderr);
304
+ assert.ok(m, `no non-JSON refusal on stderr: ${stderr}`);
305
+ return m[1];
306
+ };
307
+
308
+ // A real intercepting-proxy body: nginx's 502 template, the measured shape of
309
+ // what a corporate portal hands back in place of api.github.com's JSON.
310
+ const PORTAL = '<!DOCTYPE html><html><head><title>502 Bad Gateway</title></head><body><center><h1>502 Bad Gateway</h1></center><center>corp-proxy-7 could not reach api.github.com: upstream refused</center><hr><center>nginx/1.24.0</center></body></html>';
311
+
312
+ // The opening of a real github.com 404 body (fetched live, trimmed to its
313
+ // first 366 bytes — enough to prove the shape without vendoring the page's
314
+ // full ~269 KB). Every fixture above is single-line — `.split("\n")` and
315
+ // unsplit `s` behave identically on them — so none can distinguish the real
316
+ // `cut()` from a mutant that clips gh's FIRST LINE alone and skips the marker
317
+ // whenever that line is itself under the cap: on `PORTAL` the mutant's first
318
+ // "line" is the whole 274-char string, over the cap either way. This body is
319
+ // genuinely multi-line and its own first line — `<!DOCTYPE html>`, 15
320
+ // characters — is nowhere near 120, so a first-line-only mutant returns it
321
+ // verbatim, dropping the other 351 bytes with no marker at all: the exact
322
+ // #932 defect, at real-page scale a ~269 KB silent drop.
323
+ const GITHUB_404 =
324
+ '<!DOCTYPE html>\n<html\n lang="en"\n \n data-color-mode="auto" data-light-theme="light" data-dark-theme="dark"\n data-a11y-animated-images="system" data-a11y-link-underlines="true"\n \n >\n\n\n\n\n <head>\n <meta charset="utf-8">\n <link rel="dns-prefetch" href="https://github.githubassets.com">\n <link rel="dns-prefetch" href="https://avatars.githubusercontent.com">';
325
+
326
+ test("CLI: an over-long non-JSON payload is quoted behind a visible truncation marker", () => {
327
+ const r = runWithGhStdout(PORTAL);
328
+ assert.equal(r.status, 2, `a non-JSON body must refuse; got ${r.status} ${r.stdout}${r.stderr}`);
329
+ assert.equal(r.stdout.trim(), "", "exit 2 emits no payload");
330
+ assert.match(
331
+ quoted(r.stderr),
332
+ /… \(truncated\)$/,
333
+ "a clipped diagnostic must say it was clipped — unmarked, the reader takes this script's 120 characters for the whole of gh's output",
334
+ );
335
+ });
336
+
337
+ // The other half of the guard. A marker appended unconditionally would make
338
+ // every complete diagnostic read as a cut one — the same lie inverted, and a
339
+ // suite that only ever feeds this path an over-long payload pins neither half.
340
+ // 88 characters, the measured length of a real portal's 403 body, is a payload
341
+ // it must ACCEPT and quote whole.
342
+ test("CLI: a non-JSON payload that fits is quoted whole, with no marker", () => {
343
+ const short = '<!DOCTYPE html><html><head><title>403 Forbidden</title></head><body>portal</body></html>';
344
+ assert.ok(short.length <= 120, "this fixture only tests the accept path while it fits the cap");
345
+ const r = runWithGhStdout(short);
346
+ assert.equal(r.status, 2);
347
+ assert.equal(quoted(r.stderr), short, "a payload that fits must be quoted exactly, not reshaped");
348
+ assert.doesNotMatch(quoted(r.stderr), /truncated/, "nothing was cut, so nothing may claim it was");
349
+ });
350
+
351
+ test("CLI: the clip keeps the HEAD of gh's stdout, where this path's cause sits", () => {
352
+ const q = quoted(runWithGhStdout(PORTAL).stderr);
353
+ assert.match(q, /^<!DOCTYPE html>/, "the head identifies the payload as an HTML page — the one fact this refusal exists to deliver");
354
+ assert.match(q, /502 Bad Gateway/, "and carries the proxy's own status line");
355
+ assert.doesNotMatch(
356
+ q,
357
+ /<\/html>/,
358
+ "keeping the tail here would quote `</body></html>` — boilerplate shared by every HTML error page, naming no cause at all",
359
+ );
360
+ });
361
+
362
+ // ci-state.mjs's sibling `cut` (#1479) makes the opposite choice: its
363
+ // `s.slice(0, n)` appends the marker unconditionally, so a clipped value
364
+ // there measures 133 characters against its own 120-char cap — a cap the
365
+ // marker can push past is not a cap. This script's stderr is read by
366
+ // review-core.mjs's snapshot agent — markdown fed to a model, the same context
367
+ // budget `run()`'s comment measures in bytes — so the cap is the contract
368
+ // and the marker is part of what it bounds.
369
+ test("CLI: the marker lives inside the 120-char cap, not past it", () => {
370
+ const q = quoted(runWithGhStdout(PORTAL).stderr);
371
+ assert.ok(
372
+ q.length <= 120,
373
+ `the quoted payload including its marker must fit the 120-char cap; got ${q.length}: ${JSON.stringify(q)}`,
374
+ );
375
+ });
376
+
377
+ // Every fixture above is single-line, so none can tell the real `cut()` from
378
+ // a mutant that clips gh's first line alone and skips the marker whenever
379
+ // that line is itself under the cap — the exact #932 defect. `GITHUB_404` is
380
+ // genuinely multi-line with a short first line, so it pins both halves: the
381
+ // marker is present, and the whole quote (marker included) still fits 120.
382
+ test("CLI: a multi-line non-JSON payload is still clipped and marked, not just its first line", () => {
383
+ const q = quoted(runWithGhStdout(GITHUB_404).stderr);
384
+ assert.match(
385
+ q,
386
+ /… \(truncated\)$/,
387
+ "a first-line-only clip would return `<!DOCTYPE html>` (15 chars) unmarked, silently dropping the other 351 fixture bytes",
388
+ );
389
+ assert.ok(q.length <= 120, `the quoted payload including its marker must fit the 120-char cap; got ${q.length}: ${JSON.stringify(q)}`);
390
+ });
391
+
392
+ // The premise the direction above rests on, pinned so it cannot rot silently:
393
+ // `raw` is gh's stdout ALONE. If a future edit added `stdio: ["ignore","pipe","pipe"]`
394
+ // to run() and folded stderr in, the cause would move to the tail and the head
395
+ // clip would start discarding it — the inversion this ticket exists to avoid.
396
+ test("CLI: the quoted payload is gh's stdout alone — stderr never enters it", () => {
397
+ const bin = mkdtempSync(join(tmpdir(), "diff-stats-bin-"));
398
+ writeFileSync(join(bin, "gh"), '#!/bin/sh\necho "STDERR-ONLY-LINE" >&2\necho "<html>not json</html>"\n');
399
+ chmodSync(join(bin, "gh"), 0o755);
400
+ const r = spawnSync(process.execPath, [SCRIPT, "--pr", "5"], {
401
+ encoding: "utf8",
402
+ env: { ...process.env, PATH: `${bin}:${process.env.PATH}` },
403
+ });
404
+ rmSync(bin, { recursive: true, force: true });
405
+ assert.equal(r.status, 2);
406
+ assert.equal(quoted(r.stderr), "<html>not json</html>", "the payload is stdout");
407
+ assert.match(r.stderr, /STDERR-ONLY-LINE/, "the child's stderr is still forwarded — it is not swallowed, just not quoted");
408
+ });