rungate 0.5.0 → 0.7.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 (142) hide show
  1. package/lib/affected-tests.ts +398 -0
  2. package/lib/agent-audit.ts +2 -3
  3. package/lib/agent-worktree.ts +287 -0
  4. package/lib/agents-md-sections.ts +209 -0
  5. package/lib/behavioral-cache.ts +150 -2
  6. package/lib/branch-diff.ts +139 -0
  7. package/lib/branch-protection.ts +790 -0
  8. package/lib/brief-ac-alignment.ts +104 -0
  9. package/lib/brief-references.ts +205 -0
  10. package/lib/bun-version.ts +347 -0
  11. package/lib/check-remedy.ts +140 -0
  12. package/lib/ci-checkout.ts +18 -1
  13. package/lib/ci-history-depth.ts +159 -0
  14. package/lib/ci-setup-step.ts +96 -0
  15. package/lib/claude-code-action.ts +85 -0
  16. package/lib/claude-tool-vocabulary.ts +188 -0
  17. package/lib/cli-commands.ts +15 -0
  18. package/lib/commit-sha.ts +42 -0
  19. package/lib/commit-subject.ts +90 -0
  20. package/lib/compliance-report.ts +377 -45
  21. package/lib/compliance.ts +196 -20
  22. package/lib/config-loader.ts +51 -1
  23. package/lib/config-trackable.ts +391 -0
  24. package/lib/conformity.ts +1272 -189
  25. package/lib/containment.ts +238 -0
  26. package/lib/content-hash.ts +24 -0
  27. package/lib/context-docs.ts +143 -28
  28. package/lib/create-brief.ts +2 -1
  29. package/lib/create-sc.ts +323 -4
  30. package/lib/dev-liveness.ts +180 -0
  31. package/lib/discovery-branch-points.ts +432 -0
  32. package/lib/discovery-stability.ts +481 -0
  33. package/lib/distribution-currency.ts +186 -0
  34. package/lib/doc-routing.ts +157 -0
  35. package/lib/doc-scan-targets.ts +9 -0
  36. package/lib/evidence-command-transport.ts +160 -0
  37. package/lib/evidence-prevalidator.ts +246 -2
  38. package/lib/evidence-satisfiability.ts +534 -12
  39. package/lib/failure-intake.ts +317 -0
  40. package/lib/fallow-availability.ts +286 -0
  41. package/lib/fs-write-capabilities.ts +232 -0
  42. package/lib/gate-refusal-currency.ts +112 -0
  43. package/lib/gate-run-split.ts +147 -0
  44. package/lib/gate-timeouts.ts +82 -0
  45. package/lib/generators/agent-briefs.ts +4 -1
  46. package/lib/generators/agents-md.ts +204 -48
  47. package/lib/generators/code-map.ts +2 -1
  48. package/lib/generators/types.ts +8 -0
  49. package/lib/git-dates.ts +55 -0
  50. package/lib/git-isolation.ts +140 -0
  51. package/lib/github.ts +15 -0
  52. package/lib/google-auth-action.ts +93 -0
  53. package/lib/harness-project.ts +48 -0
  54. package/lib/harness-provenance.ts +255 -0
  55. package/lib/hook-evidence.ts +235 -0
  56. package/lib/hook-scopes.ts +386 -0
  57. package/lib/hook-target-tree.ts +398 -0
  58. package/lib/hook-trees.ts +447 -0
  59. package/lib/js-top-level-code.ts +78 -10
  60. package/lib/junit-checks.ts +536 -0
  61. package/lib/launch-base.ts +253 -0
  62. package/lib/line-citation.ts +372 -0
  63. package/lib/local-calendar-day.ts +78 -0
  64. package/lib/orphan-reaper.ts +468 -0
  65. package/lib/parity-binding.ts +205 -0
  66. package/lib/per-test-timeouts.ts +219 -0
  67. package/lib/plan-coverage.ts +151 -0
  68. package/lib/prior-branch.ts +201 -8
  69. package/lib/process-table.ts +146 -0
  70. package/lib/project-state-derivation.ts +219 -0
  71. package/lib/project-state-root.ts +258 -0
  72. package/lib/project-state-shape.ts +59 -0
  73. package/lib/promote-outputs.ts +6 -2
  74. package/lib/published-artifact.ts +525 -0
  75. package/lib/readdir-sorted.ts +45 -0
  76. package/lib/report-provenance.ts +156 -0
  77. package/lib/review-execution-reading.ts +206 -0
  78. package/lib/review-tool-agreement.ts +433 -0
  79. package/lib/rule-promotion.ts +234 -0
  80. package/lib/rule-retirement.ts +525 -0
  81. package/lib/run-branch.ts +612 -0
  82. package/lib/rungate-schema.ts +19 -0
  83. package/lib/runner-home.ts +274 -0
  84. package/lib/sc-claim-forms.ts +75 -0
  85. package/lib/sc-id-cross-tree.ts +323 -0
  86. package/lib/sc-ids.ts +121 -13
  87. package/lib/sc-reservations.ts +289 -0
  88. package/lib/scaffold/ai-review-findings.ts +403 -0
  89. package/lib/scaffold/ai-review-step.ts +4777 -0
  90. package/lib/scaffold/managed-workflow.ts +587 -192
  91. package/lib/scaffold/owned-files.ts +186 -39
  92. package/lib/scaffold/pre-push-hook.ts +49 -5
  93. package/lib/scaffold/rehearsal.ts +205 -71
  94. package/lib/scaffold/steps.ts +1517 -293
  95. package/lib/scaffold/workflow-census.ts +217 -0
  96. package/lib/scaffold/workflow-drift.ts +217 -0
  97. package/lib/scaffold/write-steps.ts +10 -4
  98. package/lib/scan-budget.ts +247 -0
  99. package/lib/scanner.ts +80 -62
  100. package/lib/scratch-roots.ts +50 -5
  101. package/lib/screenshot-discovery.ts +121 -0
  102. package/lib/screenshot-upload.ts +41 -1
  103. package/lib/secret-patterns.ts +511 -8
  104. package/lib/ship-mode.ts +219 -0
  105. package/lib/source-slice.ts +294 -0
  106. package/lib/spec-listing.ts +148 -0
  107. package/lib/spec-requirements.ts +209 -0
  108. package/lib/stale-cleanup.ts +37 -3
  109. package/lib/suite-flake.ts +278 -0
  110. package/lib/suite-measurement.ts +23 -4
  111. package/lib/suite-run-reading.ts +411 -1
  112. package/lib/suite-timeout-margin.ts +193 -0
  113. package/lib/test-case-bodies.ts +883 -0
  114. package/lib/test-case-deletion.ts +653 -0
  115. package/lib/test-census.ts +97 -29
  116. package/lib/test-command-positions.ts +274 -0
  117. package/lib/test-command.ts +133 -0
  118. package/lib/test-suite-lock.ts +325 -26
  119. package/lib/tool-probe.ts +142 -0
  120. package/lib/top-level-cd.ts +393 -0
  121. package/lib/tracked-vs-ignored.ts +234 -0
  122. package/lib/tree-entries.ts +89 -0
  123. package/lib/ts-ast.ts +200 -0
  124. package/lib/validators/spec-validators.ts +3 -2
  125. package/lib/walk-test-files.ts +97 -0
  126. package/lib/workflow-actions.ts +263 -0
  127. package/lib/worktree-collect.ts +177 -5
  128. package/lib/worktree-repo.ts +36 -1
  129. package/package.json +2 -1
  130. package/scripts/ai-review-findings.ts +45 -0
  131. package/scripts/audit-specs.ts +247 -12
  132. package/scripts/audit-transcript.ts +2 -1
  133. package/scripts/convert-spec.ts +2 -1
  134. package/scripts/create-sc.ts +37 -7
  135. package/scripts/create-spec.ts +2 -1
  136. package/scripts/create-success.ts +2 -1
  137. package/scripts/extract-constraints.ts +197 -26
  138. package/scripts/generate-code-map.ts +62 -28
  139. package/scripts/scaffold-project.ts +12 -2
  140. package/scripts/split-spec.ts +2 -1
  141. package/scripts/sync-sc-status.ts +127 -14
  142. package/scripts/sync-spec-tests.ts +2 -1
@@ -0,0 +1,232 @@
1
+ /**
2
+ * Can this module write to the filesystem? (#968, hardening #323 SC-6.)
3
+ *
4
+ * ## Why this exists
5
+ *
6
+ * `test/scratch-paths-unique.test.ts` excludes itself from its own scan,
7
+ * because its negative fixtures are source text and the detector reads its
8
+ * own test data as violations. An unchecked exclusion is how an exemption
9
+ * becomes a hole, so the exclusion is paired with a case asserting the file
10
+ * creates nothing. That case was:
11
+ *
12
+ * ```ts
13
+ * const fsImport = /import\s*\{([^}]*)\}\s*from\s*"fs";/.exec(self);
14
+ * ```
15
+ *
16
+ * `exec` returns the FIRST match. MEASURED by mutating the real file and
17
+ * running it: adding `import { mkdirSync } from "fs";` as a SECOND import
18
+ * leaves the suite at 8 pass / 0 fail, and so does
19
+ * `import { mkdirSync } from "node:fs";`. The file gains a write primitive
20
+ * and the case that exists to notice stays green.
21
+ *
22
+ * One claim in #968 did NOT reproduce and is recorded here so it is not
23
+ * re-fixed: the old check does not fail open. Replacing the braced import
24
+ * with `import * as fs from "fs"` makes `exec` return null, and the
25
+ * `.not.toBeNull()` line goes red — 1 fail, measured. The defect is the first
26
+ * match, not the missing match.
27
+ *
28
+ * ## Capability, not use
29
+ *
30
+ * This reports what the module CAN do, not what it does. An imported but
31
+ * uncalled `mkdirSync` is reported, deliberately: the exclusion it guards is
32
+ * about what the file could hide, and "imported but currently unused" is one
33
+ * edit away from used. That is also what the regex it replaces did, so the
34
+ * false-positive surface is unchanged on that axis.
35
+ *
36
+ * ## Why an AST
37
+ *
38
+ * The file being checked is full of `mkdtempSync(` and `rmSync(` as STRING
39
+ * DATA — its own negative fixtures. A source-text count reads them as calls;
40
+ * the old check sidestepped that by reading only the import list, which is
41
+ * what made it blind to the second import. In an AST a string is a `Literal`
42
+ * and a call is a `CallExpression`, so the detector can read both the imports
43
+ * and the calls without its fixtures lying to it.
44
+ *
45
+ * `null` means the source did not parse — a REFUSAL, distinct from `[]`.
46
+ *
47
+ * ## WHAT THIS DOES NOT COVER (stated, because an unstated boundary reads as
48
+ * coverage, and a clean report from an incomplete detector is worse than no
49
+ * detector — #968's own thesis)
50
+ *
51
+ * MEASURED against the current implementation, each of these returns `[]`:
52
+ *
53
+ * - **An indirection through a variable.** `const w = mkdirSync` after a
54
+ * read-only import, `const m = await import("fs"); m.mkdirSync(…)`, or a
55
+ * write primitive received as a function argument. Only bindings created
56
+ * by an import statement, `require` or `Bun.*` are read.
57
+ * - **A write primitive re-exported by a local module.** `import { write }
58
+ * from "./helpers"` is not an fs module and is not reported, by design —
59
+ * the alternative is following the import graph, which this is not.
60
+ * - **`process.binding`, `Deno.*`, raw syscalls, and native addons.**
61
+ * - **A write performed by code this module merely CALLS.** The reading is
62
+ * per-module, not transitive.
63
+ *
64
+ * What IS covered, and measured: named/namespace/default imports from `fs`,
65
+ * `node:fs`, `fs/promises`, `node:fs/promises`; `require()` and dynamic
66
+ * `import()` of those; `child_process` and `node:child_process`; and
67
+ * `Bun.write`, `Bun.$`, `Bun.spawn`, `Bun.spawnSync`.
68
+ *
69
+ * The consumer that matters — `test/scratch-paths-unique.test.ts` — holds
70
+ * none of the uncovered forms, and the covered set includes every form that
71
+ * file could plausibly acquire. The gap is bounded by the thing it guards
72
+ * rather than argued away.
73
+ */
74
+
75
+ import { type AstNode, calleeRoot, parseTsModule, walkNodes } from "./ts-ast";
76
+
77
+ /** Module specifiers that resolve to node's filesystem API. */
78
+ const FS_MODULES = new Set(["fs", "node:fs", "fs/promises", "node:fs/promises"]);
79
+
80
+ /**
81
+ * Named exports of `fs` that CANNOT modify the filesystem. Everything else
82
+ * an fs module exports is treated as a write capability.
83
+ *
84
+ * ## The ratchet direction, which is the whole point of this list
85
+ *
86
+ * This was an allowlist of write names, and an allowlist has its default
87
+ * pointing the wrong way: every export node adds in future arrives
88
+ * classified as safe, silently, and the detector reports clean on it. The
89
+ * list was already five short — MEASURED on the allowlist version, all of
90
+ * `link`, `chmod`, `utimes`, `writev` and `ftruncate` returned `[]`.
91
+ *
92
+ * Inverted, an unknown name is a write. A name on THIS list is a name
93
+ * somebody considered and classified, which is the property an exemption
94
+ * list has and an allowlist does not: naming what is exempt is evidence the
95
+ * entry was looked at, naming what is safe is not.
96
+ *
97
+ * The cost of the inversion is false positives on read-only exports not
98
+ * listed here, and it is measured rather than asserted — see the census in
99
+ * the PR and the over-report cases in
100
+ * `test/fs-write-capabilities.test.ts`. `readFileSync` and `readdirSync` are
101
+ * how the detector's own consumer does its job and must not be refused.
102
+ */
103
+ const FS_READ_ONLY_NAMES = new Set([
104
+ "access", "accessSync",
105
+ "createReadStream",
106
+ "exists", "existsSync",
107
+ "lstat", "lstatSync",
108
+ "opendir", "opendirSync",
109
+ "readFile", "readFileSync",
110
+ "readdir", "readdirSync",
111
+ "readlink", "readlinkSync",
112
+ "realpath", "realpathSync",
113
+ "stat", "statSync",
114
+ "statfs", "statfsSync",
115
+ "watch", "watchFile",
116
+ // Added after the inversion's census over 667 files flagged them. Both are
117
+ // genuinely read-only and were the ONLY false positives the inversion
118
+ // introduced: `glob` enumerates, `close` releases a descriptor. They are
119
+ // listed rather than quietly tolerated, which is exactly the property the
120
+ // inverted list is for — a name here is a name somebody classified.
121
+ "glob", "globSync",
122
+ "close", "closeSync",
123
+ "fstat", "fstatSync",
124
+ // Type-only and constant exports, which carry no capability at all.
125
+ "constants", "Dirent", "Dir", "Stats", "promises",
126
+ ]);
127
+
128
+ /**
129
+ * Modules whose exports can write without touching `fs` — a shell or a
130
+ * spawn is a write capability however it is spelled.
131
+ */
132
+ const PROCESS_MODULES = new Set([
133
+ "child_process",
134
+ "node:child_process",
135
+ "bun",
136
+ ]);
137
+
138
+ function literalString(node: unknown): string | null {
139
+ const n = node as AstNode | undefined;
140
+ if (n?.type === "Literal" && typeof n.value === "string") return n.value;
141
+ return null;
142
+ }
143
+
144
+ function isFsModule(spec: string | null): boolean {
145
+ return spec !== null && FS_MODULES.has(spec);
146
+ }
147
+
148
+ function isProcessModule(spec: string | null): boolean {
149
+ return spec !== null && PROCESS_MODULES.has(spec);
150
+ }
151
+
152
+ /**
153
+ * Every filesystem-write capability `source` holds, as human sentences, in
154
+ * document order. Empty means the module cannot write. `null` means the
155
+ * source did not parse.
156
+ */
157
+ export function fsWriteCapabilities(source: string): string[] | null {
158
+ const parsed = parseTsModule(source);
159
+ if (parsed === null) return null;
160
+
161
+ const out: string[] = [];
162
+ for (const node of walkNodes(parsed.tree)) {
163
+ if (node.type === "ImportDeclaration") {
164
+ const spec = literalString(node.source);
165
+ if (isProcessModule(spec)) {
166
+ out.push(`"${spec}" is imported, and a spawned process can write anything`);
167
+ continue;
168
+ }
169
+ if (!isFsModule(spec)) continue;
170
+ for (const s of (node.specifiers ?? []) as AstNode[]) {
171
+ // A namespace or default binding carries the WHOLE module, so every
172
+ // write primitive comes with it whether or not it is named here.
173
+ // Reported on the BINDING rather than on `fs.writeFileSync(…)` member
174
+ // access: the binding is the capability, and tracking member access
175
+ // would be a narrower reading that an indirection defeats.
176
+ if (s.type === "ImportNamespaceSpecifier" || s.type === "ImportDefaultSpecifier") {
177
+ out.push(`the whole of "${spec}" is bound as \`${(s.local as AstNode).name}\``);
178
+ continue;
179
+ }
180
+ const imported = (s.imported as AstNode | undefined)?.name;
181
+ // INVERTED (#968 review): anything not known to be read-only is a
182
+ // write. An unknown export defaults to reported, not to safe.
183
+ if (typeof imported === "string" && !FS_READ_ONLY_NAMES.has(imported)) {
184
+ out.push(`\`${imported}\` is imported from "${spec}" and is not a known read-only export`);
185
+ }
186
+ }
187
+ continue;
188
+ }
189
+
190
+ if (node.type === "ImportExpression") {
191
+ const spec = literalString(node.source);
192
+ if (isFsModule(spec)) out.push(`"${spec}" is imported dynamically`);
193
+ else if (isProcessModule(spec)) out.push(`"${spec}" is imported dynamically`);
194
+ continue;
195
+ }
196
+
197
+ // `Bun.$\`…\`` is a tagged template, not a call. Checked separately or it
198
+ // is the one shell spelling this detector cannot see — the exact shape of
199
+ // defect #968 is about.
200
+ if (node.type === "TaggedTemplateExpression") {
201
+ const tag = node.tag as AstNode | undefined;
202
+ if (tag && calleeRoot(tag) === "Bun") {
203
+ const text = parsed.js.slice(tag.start, tag.end);
204
+ if (/^Bun\s*\.\s*\$$/.test(text)) out.push(`\`${text}\` can write without importing fs`);
205
+ }
206
+ continue;
207
+ }
208
+
209
+ if (node.type !== "CallExpression") continue;
210
+ const args = (node.arguments ?? []) as AstNode[];
211
+
212
+ // `require("fs")` — the whole module, same as a namespace import.
213
+ if ((node.callee as AstNode)?.type === "Identifier" && (node.callee as AstNode).name === "require") {
214
+ const spec = literalString(args[0]);
215
+ if (isFsModule(spec) || isProcessModule(spec)) out.push(`"${spec}" is loaded with require()`);
216
+ continue;
217
+ }
218
+
219
+ // Bun's own writers and spawners, which import nothing and so are
220
+ // invisible to any import-list check. `Bun.$` is reported even though a
221
+ // read-only shell call writes nothing: a shell is a write capability,
222
+ // and the cost of over-reporting it is named in the caller.
223
+ const callee = node.callee as AstNode | undefined;
224
+ if (callee && calleeRoot(callee) === "Bun") {
225
+ const text = parsed.js.slice(callee.start, callee.end);
226
+ if (/^Bun\s*\.\s*(write|\$|spawn|spawnSync)$/.test(text)) {
227
+ out.push(`\`${text}\` can write without importing fs`);
228
+ }
229
+ }
230
+ }
231
+ return out;
232
+ }
@@ -0,0 +1,112 @@
1
+ /**
2
+ * Which recorded refusals are still true at the end of the run (#532).
3
+ *
4
+ * The ship gate RE-RUNS the verify checks. `runGateTests` spawns
5
+ * `bun test gates/ --test-name-pattern "schema|scope|verify|ship|B1|B2"` for
6
+ * the ship gate, so every `verify checks > …` name is measured a second
7
+ * time, later, against a tree the heal rounds have since changed.
8
+ *
9
+ * Measured on run `wf_e230fddc-a71`:
10
+ *
11
+ * | Check | `gates.verify.failures` | `gates.ship.failures` |
12
+ * |---|---|---|
13
+ * | `verify checks > tests-pass` | FAIL | FAIL |
14
+ * | `verify checks > evidence-prevalidation` | **FAIL** | **absent** |
15
+ * | `verify checks > tsc-pass` | FAIL | FAIL |
16
+ *
17
+ * `state.evidencePrevalidation` in that same file reads
18
+ * `{"verdict":"PASS", acs: [… all "ok" …]}`. The refusal was round one, a
19
+ * heal fixed it, the ship gate re-ran it and it passed — and the terminal
20
+ * report listed it anyway, because the report is the UNION of every gate's
21
+ * list rather than the latest reading of each check.
22
+ *
23
+ * The run returned 19 refusals. Several were already resolved, so the real
24
+ * count was smaller, and the one that actually blocked the run — a
25
+ * `SECURITY_REVIEW_STALE` — was buried among them. A report with false
26
+ * entries in it is a report nobody reads twice.
27
+ *
28
+ * ## Absence is not a pass
29
+ *
30
+ * The whole thing turns on one distinction, and getting it wrong is worse
31
+ * than the bug it fixes: a check missing from a later gate's FAILURES means
32
+ * either *it passed there* or *that gate never ran it*. Those are opposite
33
+ * facts and a failure list cannot tell them apart. Retiring on absence
34
+ * would silently drop every refusal a later gate's narrower pattern skipped.
35
+ *
36
+ * So retirement requires a positive witness — `checksRun`, the names the
37
+ * later gate reported a verdict for, recorded by `gates/orchestrator.ts`
38
+ * from the same `results` array the failures come from. A gate with no
39
+ * `checksRun` retires NOTHING. `null` and `[]` are different answers here
40
+ * and only one of them is a measurement
41
+ * (.claude/rules/config-access.md, Silent Default Substitution).
42
+ *
43
+ * ## Why this is duplicated into ship.js
44
+ *
45
+ * `workflows/ship.js` runs in a sandbox with no module loading (#69), so it
46
+ * cannot import this. It carries an inlined copy inside
47
+ * `REFUSAL-CURRENCY` markers, and `test/gate-refusal-currency.test.ts`
48
+ * extracts that copy, executes it, and runs both over one input matrix —
49
+ * the same technique `test/suite-measurement-parity.test.ts` uses for
50
+ * `suiteCurrency`, and for the same reason: two copies of a rule is a
51
+ * defect waiting for one of them to drift, and the drift is silent.
52
+ */
53
+
54
+ /** What a gate run reported back: the names it measured and the ones it failed. */
55
+ export interface GateReport {
56
+ checksRun?: unknown;
57
+ failedChecks?: unknown;
58
+ }
59
+
60
+ /** One recorded refusal. `check` is absent for refusals no gate produced. */
61
+ export interface RefusalEntry {
62
+ check?: unknown;
63
+ [k: string]: unknown;
64
+ }
65
+
66
+ /**
67
+ * The checks a gate ran and did NOT fail — the only thing that can retire an
68
+ * earlier refusal.
69
+ *
70
+ * `null` when the gate reported no `checksRun`, and that is the
71
+ * load-bearing return rather than an empty array: a caller has to be able
72
+ * to tell "this gate said nothing about what it ran" from "this gate ran
73
+ * nothing".
74
+ */
75
+ export function resolvedCheckNames(report: GateReport | null | undefined): string[] | null {
76
+ if (!report || !Array.isArray(report.checksRun)) return null;
77
+ const failed = new Set(
78
+ (Array.isArray(report.failedChecks) ? report.failedChecks : []).filter(
79
+ (c: unknown): c is string => typeof c === "string" && c !== "",
80
+ ),
81
+ );
82
+ return report.checksRun.filter(
83
+ (c: unknown): c is string => typeof c === "string" && c !== "" && !failed.has(c),
84
+ );
85
+ }
86
+
87
+ /**
88
+ * Remove, IN PLACE, every earlier refusal about a check this gate re-ran and
89
+ * did not reproduce. Returns the entries it removed.
90
+ *
91
+ * In place because the ledger is one array the whole run appends to, and
92
+ * returning a copy would leave the original to be reported by whichever
93
+ * reader got there first. The removed entries are returned rather than
94
+ * discarded: a refusal that was retired is a thing an operator may want to
95
+ * see, and silently shortening a list is how a report stops being checkable.
96
+ */
97
+ export function retireSupersededRefusals(
98
+ entries: RefusalEntry[],
99
+ report: GateReport | null | undefined,
100
+ ): RefusalEntry[] {
101
+ const resolved = resolvedCheckNames(report);
102
+ if (resolved === null || resolved.length === 0) return [];
103
+ const names = new Set(resolved);
104
+ const dropped: RefusalEntry[] = [];
105
+ for (let i = entries.length - 1; i >= 0; i--) {
106
+ const check = entries[i]?.check;
107
+ if (typeof check === "string" && check !== "" && names.has(check)) {
108
+ dropped.unshift(entries.splice(i, 1)[0]);
109
+ }
110
+ }
111
+ return dropped;
112
+ }
@@ -0,0 +1,147 @@
1
+ /**
2
+ * One runner buffer, split into the runs it actually holds (#737).
3
+ *
4
+ * A LEAF module, in the shape `lib/git-dates.ts` established (#508): one
5
+ * exported reading, no relative imports, so there is exactly one thing to
6
+ * mutate and every consumer names the same rule.
7
+ *
8
+ * ## Why this exists
9
+ *
10
+ * `parseGateResults` read one buffer two ways. The summary reading collected
11
+ * every `N pass` / `N fail` line and took the LAST — something nobody writes
12
+ * unless they know the buffer can hold more than one run. Three lines later
13
+ * `parseTestResults(testOutput)` was handed the whole buffer with no scoping
14
+ * at all, and pushed a result for every `(fail)` marker anywhere in it.
15
+ *
16
+ * On `main` at `71e3f413`, run `39325676895`:
17
+ *
18
+ * Test results: 158 pass, 1 fail, 1 warn
19
+ * BATCH DIAGNOSIS: 2 failures — fix all before re-running
20
+ * AC/Evidence (2): brief-ac-alignment: ..., brief-ac-alignment: ...
21
+ * Gate verify: FAIL (attempt 2)
22
+ *
23
+ * One check, named twice, counted as one. `1 fail` is attempt 2's summary;
24
+ * `2 failures` is one check failing in both attempts. Two deciders for "how
25
+ * many checks failed" that agree on every single-run output — which is every
26
+ * output anyone had looked at — and diverge only on a retry
27
+ * (`.claude/rules/checks-must-be-able-to-fail.md`, the fourth shape).
28
+ *
29
+ * **And the disagreement was printed.** Those two numbers appeared eleven
30
+ * lines apart in every retried gate run this project has ever produced, and
31
+ * nobody read them as contradicting each other, because each is individually
32
+ * believable. #700 was being diagnosed off the corrupted one.
33
+ *
34
+ * ## The semantics, decided rather than fallen into (#737 SC-5)
35
+ *
36
+ * **Last attempt wins.** A retry exists precisely so the later attempt is the
37
+ * verdict: the branch moved, and attempt 1's failures describe a tree that
38
+ * has been superseded. Reporting them under attempt 2's heading sends an
39
+ * operator to reasons that may already be fixed.
40
+ *
41
+ * The alternative — accumulate every attempt — was rejected because it makes
42
+ * a gate that eventually passed still report failures, and there is no way to
43
+ * tell those from failures that are still live.
44
+ *
45
+ * This is also written down in `specs/GATE-CONTRACTS-SPEC.md`, because a
46
+ * parser is a bad place to learn what a retry means.
47
+ *
48
+ * ## Why an absent final summary is UNMEASURED and not an earlier run's number
49
+ *
50
+ * #469's shape, which `lib/suite-run-reading.ts` already refuses: a run that
51
+ * produced no summary did not report zero failures, it reported nothing.
52
+ * Falling back to the previous run's numbers is *Silent Default
53
+ * Substitution* — `0`, `{}`, `null` and silence are indistinguishable from
54
+ * success, and the pre-#737 code did exactly that by taking the last match
55
+ * in the whole buffer regardless of which run it came from.
56
+ */
57
+
58
+ /** Bun prints this once per invocation; it is the only run boundary available. */
59
+ const RUN_BANNER = /^bun test v.*$/gm;
60
+
61
+ const PASS_LINE = /^\s*(\d+)\s+pass\s*$/gm;
62
+ const FAIL_LINE = /^\s*(\d+)\s+fail\s*$/gm;
63
+
64
+ export interface RunSummary {
65
+ pass: number;
66
+ fail: number;
67
+ }
68
+
69
+ export interface GateRun {
70
+ /** 0-based position in the buffer. */
71
+ index: number;
72
+ /** This run's slice of the buffer, verbatim. Concatenated, the runs ARE the buffer. */
73
+ text: string;
74
+ /** The run's own summary, or null when it printed none. */
75
+ summary: RunSummary | null;
76
+ }
77
+
78
+ export interface GateBufferReading {
79
+ /** The one discriminator. Everything else is a projection of `runs`. */
80
+ reading: "MEASURED" | "UNMEASURED";
81
+ unmeasuredReasons: string[];
82
+ runs: GateRun[];
83
+ /**
84
+ * The run whose verdict counts — the last one. `null` under an UNMEASURED
85
+ * reading, so a caller cannot read numbers off a buffer that has none.
86
+ */
87
+ verdict: GateRun | null;
88
+ }
89
+
90
+ function summaryOf(text: string): RunSummary | null {
91
+ // Last within THIS run: bun prints one summary per invocation, but a
92
+ // nested runner or a wrapper can emit more, and the final one is the
93
+ // run's own. Scoped to the slice, never to the document.
94
+ const passes = [...text.matchAll(PASS_LINE)];
95
+ const fails = [...text.matchAll(FAIL_LINE)];
96
+ if (passes.length === 0 && fails.length === 0) return null;
97
+ return {
98
+ pass: passes.length > 0 ? parseInt(passes[passes.length - 1][1], 10) : 0,
99
+ fail: fails.length > 0 ? parseInt(fails[fails.length - 1][1], 10) : 0,
100
+ };
101
+ }
102
+
103
+ /**
104
+ * Split a runner buffer into runs, and say which one is the verdict.
105
+ *
106
+ * The ONE binding. `fails`, `passes` and the per-check results are all
107
+ * derived from this split by `parseGateResults`, so the two readings cannot
108
+ * be scoped differently again — which was the whole defect.
109
+ *
110
+ * Text before the first banner belongs to the first run, so for a buffer
111
+ * with one run the slice is the buffer byte for byte. That is asserted
112
+ * rather than promised (#737 SC-6): this change has to be provably confined
113
+ * to the multi-run case.
114
+ */
115
+ export function splitGateRuns(buffer: string): GateBufferReading {
116
+ const starts = [...buffer.matchAll(RUN_BANNER)].map(m => m.index!);
117
+ // Run 0 starts at byte 0 whether or not a banner does, so a preamble
118
+ // belongs to the first run rather than becoming a run of its own. Only
119
+ // the SECOND banner onwards opens a new run.
120
+ const bounds = [0, ...starts.slice(1)];
121
+
122
+ const runs: GateRun[] = bounds.map((start, i) => {
123
+ const end = i + 1 < bounds.length ? bounds[i + 1] : buffer.length;
124
+ const text = buffer.slice(start, end);
125
+ return { index: i, text, summary: summaryOf(text) };
126
+ });
127
+
128
+ const reasons: string[] = [];
129
+ // An empty buffer agrees with every claim. Said first, because the
130
+ // summary check below cannot distinguish "no runs" from "one clean run".
131
+ if (buffer.trim() === "") {
132
+ reasons.push("the runner produced no output at all, so no run can be read from it");
133
+ } else if (runs[runs.length - 1].summary === null) {
134
+ reasons.push(
135
+ `the last of ${runs.length} run(s) in the buffer printed no summary line, so its ` +
136
+ `pass/fail counts are unknown. Taking an earlier run's numbers would report a ` +
137
+ `superseded attempt as this one's result (#469).`,
138
+ );
139
+ }
140
+
141
+ return {
142
+ reading: reasons.length === 0 ? "MEASURED" : "UNMEASURED",
143
+ unmeasuredReasons: reasons,
144
+ runs,
145
+ verdict: reasons.length === 0 ? runs[runs.length - 1] : null,
146
+ };
147
+ }
@@ -0,0 +1,82 @@
1
+ /**
2
+ * The gate run's three nested time budgets, and the order they must be in (#550).
3
+ *
4
+ * A gate check is three processes deep, and each layer can kill the one
5
+ * inside it. Before this file the three numbers were written in three places
6
+ * by three issues and **two of the three orderings were backwards**:
7
+ *
8
+ * | Layer | Was | Where |
9
+ * |---|---|---|
10
+ * | the compiler `tsc-pass` spawns | 240000 | `gates/workflow.test.ts` |
11
+ * | the `test()` around that spawn | 5000 (bun's default) | nowhere — undeclared |
12
+ * | the whole `bun test gates/` run | 120000 | `gates/gate-executor.ts` |
13
+ *
14
+ * So the compiler was allowed four minutes by a call killed after five
15
+ * seconds, inside a run killed after two. `tsc-pass` **could not pass on any
16
+ * project whose typecheck takes longer than five seconds**, which is every
17
+ * project large enough to have a gate run. Observed live: the check printed
18
+ * `Type check PASS: bunx tsc --noEmit` to its own stdout and recorded FAIL,
19
+ * and the timeout line that said why was attributed to the NEXT check by the
20
+ * parser (the other half of #550).
21
+ *
22
+ * Nothing could say so, because a timeout and a genuine failure are the same
23
+ * red, and the 240000 beside the spawn reads as deliberate.
24
+ *
25
+ * **The rule is one line: each layer must strictly outlive the one inside
26
+ * it.** Equal is not enough — a spawn killed at the same instant as its test
27
+ * is a race, and which of the two messages you get is not decided by this
28
+ * repo.
29
+ *
30
+ * Declared here, once, so the ordering is a property of four exported
31
+ * constants rather than of four numbers nobody reads together. The numbers
32
+ * themselves are judgement, and the judgement is written down:
33
+ *
34
+ * - `EXEC_TIMEOUT_MS` — the `git`/shell helpers. Unchanged at 30s; these are
35
+ * local repository reads and one that takes longer is hung, not slow.
36
+ * - `TYPECHECK_TIMEOUT_MS` — 90s, LOWERED from 240s. 240 was never reachable,
37
+ * and a single check is not entitled to twice the whole gate's budget.
38
+ * - `GATE_TEST_TIMEOUT_MS` — 120s, the per-test default. Declared at file
39
+ * level with `setDefaultTimeout` and not as a per-test argument, because
40
+ * `}, 120000);` on `tsc-pass` alone covers exactly one check and the next
41
+ * spawning check added inherits nothing. A rule naming one test is a rule
42
+ * the next one evades.
43
+ * - `GATE_SUITE_TIMEOUT_MS` — 180s, RAISED from 120s, so the outer kill is
44
+ * above the per-test budget rather than below it.
45
+ */
46
+
47
+ /** `exec`, `execArgv`, `execArgvOf` — local git and shell reads. */
48
+ export const EXEC_TIMEOUT_MS = 30_000;
49
+
50
+ /** `bunx tsc --noEmit`, the slowest single subprocess a check starts. */
51
+ export const TYPECHECK_TIMEOUT_MS = 90_000;
52
+
53
+ /** The per-test default for `gates/workflow.test.ts`. */
54
+ export const GATE_TEST_TIMEOUT_MS = 120_000;
55
+
56
+ /** The whole `bun test gates/` run, spawned by `runGateTests`. */
57
+ export const GATE_SUITE_TIMEOUT_MS = 180_000;
58
+
59
+ /**
60
+ * The ordering, as a function so it can be driven over values that are not
61
+ * these ones — otherwise the check compares the constants against themselves
62
+ * and no mutation of them can be observed.
63
+ */
64
+ export function gateTimeoutOrderingViolations(t: {
65
+ exec: number;
66
+ typecheck: number;
67
+ test: number;
68
+ suite: number;
69
+ }): string[] {
70
+ const layers: Array<[string, number, string, number]> = [
71
+ ["the exec helpers", t.exec, "the typecheck spawn", t.typecheck],
72
+ ["the typecheck spawn", t.typecheck, "the test around it", t.test],
73
+ ["the test", t.test, "the gate suite run", t.suite],
74
+ ];
75
+ return layers
76
+ .filter(([, inner, , outer]) => !(inner < outer))
77
+ .map(
78
+ ([innerName, inner, outerName, outer]) =>
79
+ `${innerName} is allowed ${inner}ms inside ${outerName}'s ${outer}ms — the inner process ` +
80
+ `outlives the thing that started it, so it is killed before it can report (#550)`,
81
+ );
82
+ }
@@ -65,7 +65,10 @@ export function generateAgentBriefs(scan: ProjectScan): Record<string, string> {
65
65
  const harness = scan.harnessConfig;
66
66
  const devUi = harness?.dev?.uiBase || null;
67
67
  const devApi = harness?.dev?.apiBase || null;
68
- const testCmd = harness?.dev?.testCmd || scan.testCmd || "bun test";
68
+ // #488 — `scan.testCmd` already IS the decision: `testCommandDecision`
69
+ // puts `dev.testCmd` on its first rung, so re-asking here was a second
70
+ // ladder that could disagree with the first and never be noticed.
71
+ const testCmd = scan.testCmd || "bun test";
69
72
  const typeCheck = harness?.dev?.typeCheck || "bunx tsc --noEmit";
70
73
  const pages = harness?.pages || {};
71
74
  const pagesTable = Object.keys(pages).length > 0