@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,364 @@
1
+ // Regression gate for json.sh, the JSON-escaping library every fleet script
2
+ // that emits a payload now sources. Zero deps:
3
+ // `node --test plugin/scripts/json.test.mjs`.
4
+ //
5
+ // The library exists because the same ~40 lines were copy-pasted into
6
+ // inflight.sh, release-ticket.sh and no-undo-audit.sh, and every fix to the
7
+ // rule list had to be written out three times (five, counting jarr and
8
+ // jarr_rewritten). #119.
9
+ //
10
+ // The load-bearing case here is `a failing sed makes jstr report failure`.
11
+ // Measured on PR #425 and recorded on #119: `printf | sed | tr` returns only
12
+ // the LAST stage's status, so forcing `sed` to fail left jstr exiting 0 with a
13
+ // truncated or empty value while forcing `tr` — the actual tail — worked. A
14
+ // test that only forces the tail stage passes against the unfixed helper and
15
+ // pins nothing, which is exactly why both stages are forced below and why the
16
+ // sed case asserts on the OUTPUT as well as the status: an assertion that only
17
+ // reads the status cannot tell "the shim fired and jstr caught it" from "the
18
+ // shim never ran at all".
19
+ //
20
+ // Its opposite number is `a healthy call is silent and exits 0`. A guard that
21
+ // reports failure on a string it should have escaped fine is a different bug
22
+ // from the one above, not the same bug inverted, so the accept path is pinned
23
+ // on its own rather than left implied by the escaping table.
24
+
25
+ import { test } from "node:test";
26
+ import assert from "node:assert/strict";
27
+ import { spawnSync } from "node:child_process";
28
+ import { chmodSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
29
+ import { tmpdir } from "node:os";
30
+ import { join } from "node:path";
31
+ import { fileURLToPath } from "node:url";
32
+
33
+ const LIB = fileURLToPath(new URL("./json.sh", import.meta.url));
34
+
35
+ /**
36
+ * Run `body` in a fresh /bin/sh that has sourced the library.
37
+ *
38
+ * No `set -e` in the driver, deliberately: these tests measure the status a
39
+ * helper RETURNS, and under -e a non-zero return would abort the driver before
40
+ * it could print that status. The scripts that source this library do run under
41
+ * -e; that interaction is pinned in each script's own suite, not here.
42
+ *
43
+ * `break` names a tool to shadow with a failing stub — the only way to observe
44
+ * a stage failing without editing the library under test.
45
+ *
46
+ * `raw` skips Node's own UTF-8 decoding of stdout, returning a Buffer instead
47
+ * of a string. Required for any test asserting on a raw invalid-UTF-8 byte
48
+ * (#613): `encoding: "utf8"` makes Node itself silently substitute U+FFFD on
49
+ * the way OUT, which would hide the exact bug under test — a caller (`jq`,
50
+ * `python3 json.load`) that does not launder the bytes first.
51
+ */
52
+ function drive(body, { args = [], input, break: broken, raw = false } = {}) {
53
+ const dir = mkdtempSync(join(tmpdir(), "json-sh-"));
54
+ try {
55
+ const env = { ...process.env };
56
+ if (broken) {
57
+ const stub = join(dir, broken);
58
+ // Exit 1 and emit nothing — how BSD `tr` behaves on a byte that is not
59
+ // valid UTF-8, the real-world failure this stands in for (#582).
60
+ writeFileSync(stub, "#!/bin/sh\nexit 1\n");
61
+ chmodSync(stub, 0o755);
62
+ env.PATH = `${dir}:${process.env.PATH}`;
63
+ }
64
+ const f = join(dir, "drive.sh");
65
+ writeFileSync(f, `. ${JSON.stringify(LIB)}\n${body}\n`);
66
+ return spawnSync("sh", [f, ...args], { env, input, encoding: raw ? "buffer" : "utf8" });
67
+ } finally {
68
+ rmSync(dir, { recursive: true, force: true });
69
+ }
70
+ }
71
+
72
+ /** Call a one-argument helper; returns {rc, out}. */
73
+ function call(fn, arg, opts = {}) {
74
+ const r = drive(`${fn} "$1"; printf '\\nRC=%s' "$?"`, { args: [arg], ...opts });
75
+ assert.equal(r.status, 0, `driver itself failed: ${r.stderr}`);
76
+ const at = r.stdout.lastIndexOf("\nRC=");
77
+ return { out: r.stdout.slice(0, at), rc: Number(r.stdout.slice(at + 4)) };
78
+ }
79
+
80
+ /** Call a stdin helper; returns {rc, out}. */
81
+ function pipe(fn, input, opts = {}) {
82
+ const r = drive(`${fn}; printf '\\nRC=%s' "$?"`, { input, ...opts });
83
+ assert.equal(r.status, 0, `driver itself failed: ${r.stderr}`);
84
+ const at = r.stdout.lastIndexOf("\nRC=");
85
+ return { out: r.stdout.slice(0, at), rc: Number(r.stdout.slice(at + 4)) };
86
+ }
87
+
88
+ // ---------------------------------------------------------------- jstr
89
+
90
+ // The escaping table. Each row is a string a fleet script can genuinely be
91
+ // handed — git accepts `"` in a ref, a worktree path is a filename and accepts
92
+ // `\` too — and the expectation is what makes `"<out>"` parse back to it.
93
+ const ESCAPES = [
94
+ ["plain", "feature/ordinary-branch", "feature/ordinary-branch"],
95
+ ["a double quote", 'evil"branch', 'evil\\"branch'],
96
+ ["a backslash", "a\\b", "a\\\\b"],
97
+ ["backslash before quote", '\\"', '\\\\\\"'],
98
+ ["a tab", "a\tb", "a\\tb"],
99
+ ["a newline", "a\nb", "a\\nb"],
100
+ ["a carriage return", "a\rb", "a\\rb"],
101
+ ["a backspace", "a\bb", "a\\bb"],
102
+ ["a form feed", "a\fb", "a\\fb"],
103
+ ["UTF-8, untouched", "brée/ünïcode", "brée/ünïcode"],
104
+ ];
105
+
106
+ for (const [name, input, expected] of ESCAPES) {
107
+ test(`jstr escapes ${name}`, () => {
108
+ const { rc, out } = call("jstr", input);
109
+ assert.equal(rc, 0);
110
+ assert.equal(out, expected);
111
+ assert.equal(JSON.parse(`"${out}"`), input, "must round-trip through a real JSON parser");
112
+ });
113
+ }
114
+
115
+ test("jstr replaces a C0 byte with no JSON short form, rather than emitting it raw", () => {
116
+ // \013 (VT) is the example RFC 8259 does not give a short form. Emitted raw
117
+ // it is a control character in a JSON string, which no parser accepts.
118
+ const { rc, out } = call("jstr", "a\x0bb");
119
+ assert.equal(rc, 0);
120
+ assert.equal(out, "a b");
121
+ assert.doesNotThrow(() => JSON.parse(`"${out}"`));
122
+ });
123
+
124
+ test("jstr leaves DEL alone — \\177 is not a C0 byte and JSON permits it", () => {
125
+ const { rc, out } = call("jstr", "a\x7fb");
126
+ assert.equal(rc, 0);
127
+ assert.equal(out, "a\x7fb");
128
+ assert.equal(JSON.parse(`"${out}"`), "a\x7fb");
129
+ });
130
+
131
+ // A byte that is not valid UTF-8 at all — not merely a C0 control byte, a
132
+ // byte no UTF-8 sequence starts or continues with. Built as a raw Buffer
133
+ // rather than a JS string: `execFileSync`/`spawnSync` re-encode a JS string
134
+ // argv as UTF-8, so a JS `"\xFF"` (U+00FF) would arrive as the two-byte
135
+ // sequence \303\277 — valid UTF-8, which reproduces nothing (same pitfall
136
+ // no-undo-audit.test.mjs's `byteConflictRepo` comment names). Piped to stdin
137
+ // as a Buffer and captured inside the driven script with `$(cat)` sidesteps
138
+ // it: Node writes a Buffer to a child's stdin unchanged, and shell command
139
+ // substitution is byte-transparent.
140
+ const INVALID_UTF8 = Buffer.concat([Buffer.from("b"), Buffer.from([0xff]), Buffer.from("ad")]);
141
+
142
+ test("jstr replaces an invalid UTF-8 byte with U+FFFD rather than emitting it raw, and the result is valid UTF-8 JSON (#613)", () => {
143
+ const r = drive(`x=$(cat); jstr "$x"`, { input: INVALID_UTF8, raw: true });
144
+ assert.equal(r.status, 0, `driver failed: ${r.stderr}`);
145
+ const wrapped = Buffer.concat([Buffer.from('"'), r.stdout, Buffer.from('"')]);
146
+ const decoded = wrapped.toString("utf8");
147
+ // Strict UTF-8 validity via round-trip: Node's decoder is itself lossy —
148
+ // it substitutes U+FFFD for a raw invalid byte on the way IN — so
149
+ // re-encoding the decoded string reproduces the same bytes only when the
150
+ // buffer was already valid UTF-8. A raw invalid byte that jstr failed to
151
+ // repair shows up here as a length/content mismatch, not a thrown error.
152
+ assert.deepEqual(Buffer.from(decoded, "utf8"), wrapped,
153
+ "jstr's output is not valid UTF-8 — a raw invalid byte reached the JSON body");
154
+ assert.equal(JSON.parse(decoded), "b�ad",
155
+ "the fault must render as U+FFFD, not vanish or truncate the string");
156
+ });
157
+
158
+ test("a failing python3 makes jstr report failure", () => {
159
+ const { rc, out } = call("jstr", 'evil"branch', { break: "python3" });
160
+ assert.notEqual(out, 'evil\\"branch', "the python3 stub did not shadow the real python3 — this test proves nothing");
161
+ assert.notEqual(rc, 0);
162
+ });
163
+
164
+ test("jstr on an empty string is empty at exit 0, forking nothing", () => {
165
+ const { rc, out } = call("jstr", "");
166
+ assert.equal(rc, 0);
167
+ assert.equal(out, "");
168
+ });
169
+
170
+ test("jstr on an empty string survives a broken sed and tr — it never calls them", () => {
171
+ // The short circuit is what lets a field that legitimately found nothing stay
172
+ // empty during the PATH-wide tool outage inflight.sh measures at its own top:
173
+ // that field never runs the broken tool, so it cannot observe the failure.
174
+ for (const broken of ["sed", "tr", "python3"]) {
175
+ const { rc, out } = call("jstr", "", { break: broken });
176
+ assert.equal(rc, 0, `empty jstr must not fail on a broken ${broken}`);
177
+ assert.equal(out, "");
178
+ }
179
+ });
180
+
181
+ // ------------------------------------------------- the internal-status half
182
+
183
+ test("a failing sed makes jstr report failure", () => {
184
+ const { rc, out } = call("jstr", 'evil"branch', { break: "sed" });
185
+ // The shim really engaged: with sed working this is `evil\"branch`.
186
+ assert.notEqual(out, 'evil\\"branch', "the sed stub did not shadow the real sed — this test proves nothing");
187
+ assert.notEqual(rc, 0,
188
+ "jstr swallowed a failed sed. `printf | sed | tr` reports only tr's status, so the caller gets a truncated value at exit 0 and its `|| die` never fires (#119, measured on PR #425).");
189
+ });
190
+
191
+ test("a failing tr makes jstr report failure", () => {
192
+ const { rc, out } = call("jstr", 'evil"branch', { break: "tr" });
193
+ assert.notEqual(out, 'evil\\"branch', "the tr stub did not shadow the real tr — this test proves nothing");
194
+ assert.notEqual(rc, 0);
195
+ });
196
+
197
+ test("a healthy jstr exits 0 and is not caught by its own guard", () => {
198
+ // The false-positive half. The status fix above must not start refusing
199
+ // strings it has always escaped correctly, and every one of these is ordinary
200
+ // input a fleet script sees on its healthy path.
201
+ for (const s of ["fix/119-json-sh-extract", ".worktrees/119-json-sh-extract", "no-rebase", "npm ci", "brée", "a\tb", "x".repeat(4096)]) {
202
+ const { rc, out } = call("jstr", s);
203
+ assert.equal(rc, 0, `jstr wrongly refused ${JSON.stringify(s)}`);
204
+ assert.equal(JSON.parse(`"${out}"`), s);
205
+ }
206
+ });
207
+
208
+ // ---------------------------------------------------------- jrewritten
209
+
210
+ test("jrewritten is false when every byte was escaped rather than replaced", () => {
211
+ for (const s of ["", "plain", 'a"b', "a\\b", "a\tb", "a\nb", "a\rb", "a\bb", "a\fb", "a\x7fb"]) {
212
+ const { rc, out } = call("jrewritten", s);
213
+ assert.equal(rc, 0);
214
+ assert.equal(out, "false", `${JSON.stringify(s)} loses no bytes, so nothing was rewritten`);
215
+ }
216
+ });
217
+
218
+ test("jrewritten is true when a byte was replaced", () => {
219
+ const { rc, out } = call("jrewritten", "a\x0bb");
220
+ assert.equal(rc, 0);
221
+ assert.equal(out, "true");
222
+ });
223
+
224
+ test("a failing tr makes jrewritten report failure rather than a confident answer", () => {
225
+ // Its last line is `[ … ] && printf false || printf true`, an AND-OR list
226
+ // that always exits 0. Without an explicit return, bash 5.x in every mode —
227
+ // every distro whose /bin/sh is bash 5 — runs on to it and hands back a
228
+ // confident `true` about bytes nothing ever examined.
229
+ const { rc } = call("jrewritten", "a\x0bb", { break: "tr" });
230
+ assert.notEqual(rc, 0);
231
+ });
232
+
233
+ test("a failing python3 makes jrewritten report failure rather than a confident answer", () => {
234
+ // Must hold a byte >= \200 and no C0 byte — jrewritten now short-circuits
235
+ // to `true` on a C0 byte alone (it already has its answer) and to `false`
236
+ // on pure ASCII (already valid UTF-8 by construction), and neither path
237
+ // reaches python3 at all, so an input that hits either would pass this
238
+ // test against a jrewritten that never calls python3 in the first place.
239
+ const { rc } = call("jrewritten", "café", { break: "python3" });
240
+ assert.notEqual(rc, 0);
241
+ });
242
+
243
+ test("jrewritten is true when a byte was not valid UTF-8, not just for the C0 replacement set (#613)", () => {
244
+ const r = drive(`x=$(cat); jrewritten "$x"`, { input: INVALID_UTF8 });
245
+ assert.equal(r.status, 0, `driver failed: ${r.stderr}`);
246
+ assert.equal(r.stdout, "true");
247
+ });
248
+
249
+ test("jrewritten stays false for valid multi-byte UTF-8 under the new UTF-8 check (#613)", () => {
250
+ const { rc, out } = call("jrewritten", "brée/ünïcode");
251
+ assert.equal(rc, 0);
252
+ assert.equal(out, "false");
253
+ });
254
+
255
+ // ---------------------------------------------------------------- jarr
256
+
257
+ test("jarr emits one quoted JSON string per line, comma-joined", () => {
258
+ const { rc, out } = pipe("jarr", 'a\nb"c\nd\\e\n');
259
+ assert.equal(rc, 0);
260
+ assert.deepEqual(JSON.parse(`[${out}]`), ["a", 'b"c', "d\\e"]);
261
+ });
262
+
263
+ test("a failing sed makes jarr report failure", () => {
264
+ const { rc, out } = pipe("jarr", 'a\nb"c\n', { break: "sed" });
265
+ assert.notEqual(out, '"a","b\\"c"', "the sed stub did not shadow the real sed — this test proves nothing");
266
+ assert.notEqual(rc, 0,
267
+ "jarr swallowed a failed sed. Its `sed | tr | paste` reports only paste's status, the same mask jstr carries (#119).");
268
+ });
269
+
270
+ test("a failing tr makes jarr report failure", () => {
271
+ const { rc } = pipe("jarr", 'a\nb"c\n', { break: "tr" });
272
+ assert.notEqual(rc, 0);
273
+ });
274
+
275
+ test("a failing python3 makes jarr report failure", () => {
276
+ const { rc } = pipe("jarr", 'a\nb"c\n', { break: "python3" });
277
+ assert.notEqual(rc, 0);
278
+ });
279
+
280
+ test("jarr replaces an invalid UTF-8 byte with U+FFFD rather than emitting it raw, other elements untouched (#613)", () => {
281
+ const input = Buffer.concat([Buffer.from("plain\n"), INVALID_UTF8]);
282
+ const r = drive("jarr", { input, raw: true });
283
+ assert.equal(r.status, 0, `driver failed: ${r.stderr}`);
284
+ const wrapped = Buffer.concat([Buffer.from("["), r.stdout, Buffer.from("]")]);
285
+ const decoded = wrapped.toString("utf8");
286
+ assert.deepEqual(Buffer.from(decoded, "utf8"), wrapped,
287
+ "jarr's output is not valid UTF-8 — a raw invalid byte reached the JSON body");
288
+ assert.deepEqual(JSON.parse(decoded), ["plain", "b�ad"]);
289
+ });
290
+
291
+ test("jarr_rewritten is a parallel boolean array, last line included", () => {
292
+ // `read` alone drops a final line with no trailing newline, so the last
293
+ // element is the one that goes missing — pinned with input that has none.
294
+ const { rc, out } = pipe("jarr_rewritten", "plain\na\x0bb");
295
+ assert.equal(rc, 0);
296
+ assert.deepEqual(JSON.parse(`[${out}]`), [false, true]);
297
+ });
298
+
299
+ test("jarr_rewritten flags a line holding an invalid UTF-8 byte, not just the C0 replacement set (#613)", () => {
300
+ // jarr_rewritten delegates to jrewritten per line, so this pins the
301
+ // delegation carries the new check rather than re-testing jrewritten itself.
302
+ const input = Buffer.concat([Buffer.from("plain\n"), INVALID_UTF8]);
303
+ const r = drive("jarr_rewritten", { input });
304
+ assert.equal(r.status, 0, `driver failed: ${r.stderr}`);
305
+ assert.deepEqual(JSON.parse(`[${r.stdout}]`), [false, true]);
306
+ });
307
+
308
+ test("a failing tr makes jarr_rewritten report failure, not a short array", () => {
309
+ // Its own last stage is `paste`, which exits 0 on whatever the aborted loop
310
+ // managed to hand it. Without the `|| exit 1` inside the loop and the
311
+ // `|| return 1` on the capture, a broken `tr` yields a SHORTER boolean array
312
+ // than the string array it is supposed to parallel — and the caller has no
313
+ // way to notice, because the two are emitted as separate JSON fields.
314
+ const { rc } = pipe("jarr_rewritten", "plain\na\x0bb\n", { break: "tr" });
315
+ assert.notEqual(rc, 0);
316
+ });
317
+
318
+ test("jarr keeps a trailing empty element — the same input must yield the same length as jarr_rewritten's parallel array (#613 regression)", () => {
319
+ // The #613 UTF-8 repair stage captured python3's output through `$()`,
320
+ // which strips ALL trailing newlines before jarr's own blank-line-sensitive
321
+ // sed/paste stages ever saw them — silently dropping trailing empty array
322
+ // elements. `jarr_rewritten` reads stdin line-by-line, not through a
323
+ // capture, so it did NOT drop them: the two arrays desynced in length,
324
+ // breaking the "parallel boolean array to jarr's own output" invariant
325
+ // both are documented to hold. Pinned here so the LENGTH match — not just
326
+ // the content of either array alone — is what future edits are checked
327
+ // against.
328
+ const input = "a\n\n";
329
+ const { rc: rc1, out: out1 } = pipe("jarr", input);
330
+ const { rc: rc2, out: out2 } = pipe("jarr_rewritten", input);
331
+ assert.equal(rc1, 0);
332
+ assert.equal(rc2, 0);
333
+ const arr1 = JSON.parse(`[${out1}]`);
334
+ const arr2 = JSON.parse(`[${out2}]`);
335
+ assert.equal(arr1.length, arr2.length,
336
+ `jarr produced ${arr1.length} elements but jarr_rewritten produced ${arr2.length} for the same input`);
337
+ assert.deepEqual(arr1, ["a", ""]);
338
+ assert.deepEqual(arr2, [false, false]);
339
+ });
340
+
341
+ test("jarr on stdin holding only a newline is a proper one-element array, not indistinguishable from zero elements (#613 regression)", () => {
342
+ // Same root cause as the test above: `$()` stripping the python3 stage's
343
+ // only trailing newline turned `[""]` (one element, an empty string) into
344
+ // empty output — the same shape as `jarr` on genuinely empty stdin, a
345
+ // distinction this file's header and the empty-stdin test below both
346
+ // guard explicitly.
347
+ const { rc, out } = pipe("jarr", "\n");
348
+ assert.equal(rc, 0);
349
+ assert.deepEqual(JSON.parse(`[${out}]`), [""]);
350
+ });
351
+
352
+ test("jarr on empty stdin is empty, not a one-element array holding nothing", () => {
353
+ // The original `sed | tr | paste` emitted nothing at all on empty input
354
+ // (measured). Splitting the pipeline to read each stage's status introduced a
355
+ // `printf '%s\n'` re-emit that would turn "no elements" into one empty line,
356
+ // and `paste` would answer `""` — inventing an element. The `[ -n ]` guard is
357
+ // what preserves the original answer.
358
+ const { rc, out } = pipe("jarr", "");
359
+ assert.equal(rc, 0);
360
+ assert.equal(out, "", "empty in, empty out — `[]` and `[\"\"]` are different answers");
361
+ const { rc: rc2, out: out2 } = pipe("jarr_rewritten", "");
362
+ assert.equal(rc2, 0);
363
+ assert.equal(out2, "");
364
+ });