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,205 @@
1
+ /**
2
+ * ONE binding decides whether the contract-parity check has one name and one
3
+ * home (#637 AC-8).
4
+ *
5
+ * ## Why this is a check and not a comment
6
+ *
7
+ * #626 asked for a `contract-parity` command and said, in the issue body, why
8
+ * it must not be called a preflight: `preflightResults` in `workflows/ship.js`
9
+ * is SSH host reachability and `lib/release-preflight.ts` is npm release
10
+ * verification, so a third meaning of the word would be its own parity defect
11
+ * — the fix for the class committing an instance of the class.
12
+ *
13
+ * A sentence in an issue is a sentence. The thing that stops a second copy of
14
+ * `commandParity` appearing in `workflows/ship.js` eight weeks from now, or a
15
+ * `preflightParity` growing beside `contractParity`, is a scan somebody has to
16
+ * get past.
17
+ *
18
+ * ## Why ONE exported binding and not three
19
+ *
20
+ * There are three properties below and it is tempting to export three
21
+ * predicates. Two deciders that agree are behaviourally one guard — no
22
+ * behavioural test can tell them apart, and a mutation of either leaves the
23
+ * other holding every case up (`.claude/rules/checks-must-be-able-to-fail.md`,
24
+ * fourth shape). Three would be worse. So the three properties are three
25
+ * VIOLATION CLASSES out of one function, and each has an input only it can
26
+ * reach, written down as a separate case in
27
+ * `test/contract-parity-derivation.test.ts`:
28
+ *
29
+ * | Class | The input only it reaches |
30
+ * |---|---|
31
+ * | `DUPLICATE_PARITY_BINDING` | a tree declaring `contractParity` twice |
32
+ * | `PREFLIGHT_NAMED_PARITY` | a tree declaring `preflightParity` beside one correct `contractParity` |
33
+ * | `UNMEASURED_PARITY_SCAN` | a tree declaring no `contractParity` at all |
34
+ *
35
+ * Varying exactly one thing per input is #585's pair discipline: a fixture
36
+ * that moved two of those at once would be red under any of the three and
37
+ * would measure none of them.
38
+ *
39
+ * ## Why the scan refuses when it finds nothing
40
+ *
41
+ * A scan over a directory tree that matched no declarations reports the same
42
+ * empty violation list as a tree that is perfectly in order. That is the
43
+ * third shape, and it is how a regex that stops matching after a reformat
44
+ * becomes a green check nobody can see through. So an empty reading is a
45
+ * REFUSAL naming the scan — `UNMEASURED_PARITY_SCAN` — and the control is the
46
+ * `preflight` family itself: seven declarations in this tree that cannot all
47
+ * vanish, named here because a string that must be present if the store holds
48
+ * anything is the only thing that distinguishes an absent problem from an
49
+ * absent instrument (`.claude/rules/checks-must-be-able-to-fail.md`, #646/#657).
50
+ */
51
+ import { existsSync, readFileSync, readdirSync, statSync } from "fs";
52
+ import { join } from "path";
53
+
54
+ /**
55
+ * The directories AC-8 names. `workflows/` is in the list because it is where
56
+ * the second copy would go: `workflows/ship.js` cannot import (#69), so every
57
+ * comparison it needs is inlined, and an inlined parity check is a second
58
+ * decider by construction.
59
+ */
60
+ export const PARITY_SCAN_DIRS = ["lib", "scripts", "gates", "workflows"] as const;
61
+
62
+ /** The one name the contract-parity entry point is allowed to have. */
63
+ export const PARITY_BINDING = "contractParity";
64
+
65
+ /**
66
+ * The word a parity binding must never wear, and the word that proves the
67
+ * scan is reading something.
68
+ */
69
+ export const FOREIGN_PARITY_WORD = "preflight";
70
+
71
+ export const PARITY_VIOLATION_CLASSES = [
72
+ "DUPLICATE_PARITY_BINDING",
73
+ "PREFLIGHT_NAMED_PARITY",
74
+ "UNMEASURED_PARITY_SCAN",
75
+ ] as const;
76
+ export type ParityViolationClass = (typeof PARITY_VIOLATION_CLASSES)[number];
77
+
78
+ /** A value declaration — the things that can decide something. */
79
+ const DECLARATION =
80
+ /(?:^|\n)\s*(export\s+)?(?:default\s+)?(?:async\s+)?(?:function\s+|class\s+|const\s+|let\s+|var\s+)([A-Za-z_$][A-Za-z0-9_$]*)/g;
81
+
82
+ export interface ParityDeclaration {
83
+ name: string;
84
+ /** Repo-relative path. */
85
+ file: string;
86
+ line: number;
87
+ /**
88
+ * AC-8 is about the EXPORTED symbol, and the distinction is load-bearing in
89
+ * both directions: a module-local `const parity` is nobody's second decider,
90
+ * and `preflightResults` — a local in `workflows/ship.js` — is one of the
91
+ * seven declarations that prove this scan is reading the tree at all.
92
+ */
93
+ exported: boolean;
94
+ }
95
+
96
+ function walk(dir: string, out: string[] = []): string[] {
97
+ if (!existsSync(dir)) return out;
98
+ for (const entry of readdirSync(dir)) {
99
+ if (entry === "node_modules" || entry.startsWith(".")) continue;
100
+ const p = join(dir, entry);
101
+ if (statSync(p).isDirectory()) walk(p, out);
102
+ else if (/\.(ts|js)$/.test(p) && !/\.test\.(ts|js)$/.test(p)) out.push(p);
103
+ }
104
+ return out;
105
+ }
106
+
107
+ /**
108
+ * Every value declaration in the four directories whose name contains `word`,
109
+ * case-insensitively.
110
+ *
111
+ * Exported for the cases to read, NOT as a second decider: nothing in this
112
+ * module's verdict is computed anywhere but `parityBindingViolations`, so
113
+ * there is one thing to mutate. A case that asserted on this reading instead
114
+ * of on the violations would be asserting the input it just supplied.
115
+ */
116
+ export function declarationsNaming(root: string, word: string): ParityDeclaration[] {
117
+ const needle = word.toLowerCase();
118
+ const out: ParityDeclaration[] = [];
119
+ for (const dir of PARITY_SCAN_DIRS) {
120
+ for (const abs of walk(join(root, dir))) {
121
+ const text = readFileSync(abs, "utf-8");
122
+ const relative = abs.slice(root.endsWith("/") ? root.length : root.length + 1);
123
+ for (const m of text.matchAll(DECLARATION)) {
124
+ if (!m[2].toLowerCase().includes(needle)) continue;
125
+ out.push({
126
+ name: m[2],
127
+ file: relative,
128
+ line: text.slice(0, m.index!).split("\n").length,
129
+ exported: m[1] !== undefined,
130
+ });
131
+ }
132
+ }
133
+ }
134
+ return out;
135
+ }
136
+
137
+ /**
138
+ * The whole verdict. Empty means the contract-parity binding has one name and
139
+ * one home; every string names its class first so a reader can tell the three
140
+ * apart without parsing prose.
141
+ */
142
+ export function parityBindingViolations(root: string): string[] {
143
+ const violations: string[] = [];
144
+ const parity = declarationsNaming(root, "parity");
145
+ const foreign = declarationsNaming(root, FOREIGN_PARITY_WORD);
146
+
147
+ // The instrument, before any finding. An empty reading over four
148
+ // directories is not a clean tree, and a reading that found no `preflight`
149
+ // in a tree that has seven of them is a scan that has stopped working.
150
+ const exported = parity.filter(d => d.exported);
151
+ const entry = exported.filter(d => d.name === PARITY_BINDING);
152
+ if (entry.length === 0) {
153
+ violations.push(
154
+ `UNMEASURED_PARITY_SCAN: the scan over ${PARITY_SCAN_DIRS.join(", ")} found no declaration of ` +
155
+ `\`${PARITY_BINDING}\` at all. A comparison that could not be performed is not a comparison that ` +
156
+ `passed — either the binding was renamed, or this scan has stopped reading the tree ` +
157
+ `(it saw ${parity.length} parity declaration(s) and ${foreign.length} ${FOREIGN_PARITY_WORD} declaration(s))`,
158
+ );
159
+ }
160
+
161
+ // One name, one home. Counted per name rather than only for the entry
162
+ // point: a second `commandParity` is the same defect one level down, and
163
+ // the category runners are what `contractParity` dispatches to.
164
+ //
165
+ // **The entry point is counted whether or not it is exported, and that is
166
+ // the fifth shape applied in advance.** AC-8's words are "the exported
167
+ // symbol is declared exactly once", so the cheapest repair when this goes
168
+ // red is to delete the `export` keyword from the duplicate — one word, the
169
+ // symptom disappears, and the tree still holds two deciders answering one
170
+ // question, each called from inside its own module. A check that counted
171
+ // only exported declarations would CERTIFY that repair. A module-local
172
+ // `const parity` is a different matter and stays out of this: it is nobody's
173
+ // second decider, which is why the widening is to the entry-point NAME and
174
+ // not to every name containing the word.
175
+ const entryAnywhere = parity.filter(d => d.name === PARITY_BINDING);
176
+ const counted = [...exported, ...entryAnywhere.filter(d => !d.exported)];
177
+ const byName = new Map<string, ParityDeclaration[]>();
178
+ for (const d of counted) {
179
+ if (!byName.has(d.name)) byName.set(d.name, []);
180
+ byName.get(d.name)!.push(d);
181
+ }
182
+ for (const [name, found] of [...byName].sort(([a], [b]) => a.localeCompare(b))) {
183
+ if (found.length <= 1) continue;
184
+ violations.push(
185
+ `DUPLICATE_PARITY_BINDING: \`${name}\` is declared ${found.length} times — ` +
186
+ `${found.map(d => `${d.file}:${d.line}`).join(", ")}. Two deciders answering one question about one ` +
187
+ `input are behaviourally one guard, and a mutation of either leaves the other holding the case up (#637 AC-8)`,
188
+ );
189
+ }
190
+
191
+ // Not a third preflight. The repository already has two unrelated meanings
192
+ // of that word; a parity binding wearing it would be #626's own subject,
193
+ // committed by the fix for it.
194
+ for (const d of exported) {
195
+ if (!d.name.toLowerCase().includes(FOREIGN_PARITY_WORD)) continue;
196
+ violations.push(
197
+ `PREFLIGHT_NAMED_PARITY: \`${d.name}\` at ${d.file}:${d.line} decides parity under the name ` +
198
+ `\`${FOREIGN_PARITY_WORD}\`. This repository already has two unrelated things wearing that name — ` +
199
+ `SSH host reachability in workflows/ship.js and npm release verification in lib/release-preflight.ts — ` +
200
+ `and a third meaning is its own parity defect (#626, #637 AC-8)`,
201
+ );
202
+ }
203
+
204
+ return violations;
205
+ }
@@ -0,0 +1,219 @@
1
+ /**
2
+ * Every construct in a test file that raises the per-test budget above the
3
+ * one `bunfig.toml` declares (#968, from #804/#621).
4
+ *
5
+ * ## The subject, and why it is wider than one spelling
6
+ *
7
+ * `timeoutProximityViolations` in `lib/junit-checks.ts` compares each case's
8
+ * measured duration against ONE number — the budget in force. A case that
9
+ * carries a budget of its own leaves that reading two ways: it is never
10
+ * killed at the global budget, so the `was KILLED` row cannot fire; and once
11
+ * it runs past the global budget the proximity row cannot fire either, since
12
+ * that row requires `ms < timeoutMs`. That is the mechanism by which #802's
13
+ * `}, 60_000)` took the SC-6 scan out of the one instrument that would have
14
+ * warned before the kill — the headline finding of #804.
15
+ *
16
+ * The detector this replaces, in `test/scan-budget.test.ts`, was
17
+ * `/\}\s*,\s*([0-9_]+)\s*\)\s*;/g`. MEASURED on bun 1.3.14 against a
18
+ * `--timeout 300` budget, with a case that sleeps 600ms: the no-override
19
+ * control is killed at 300ms, and all five of these run to completion.
20
+ *
21
+ * | form | raises the budget | old regex |
22
+ * |---|---|---|
23
+ * | `}, 60_000);` | yes | matched |
24
+ * | `}, TIMEOUT);` | yes | **missed** |
25
+ * | `}, 60 * 1000);` | yes | **missed** |
26
+ * | `}, 60_000)` no `;` | yes | **missed** |
27
+ * | `}, { timeout: 5000 });` | yes | **missed** |
28
+ * | `setDefaultTimeout(…)` | yes | **missed** |
29
+ *
30
+ * So the guard against reintroducing #802's override was walked around by
31
+ * extracting the number into a constant. Four of the five ways to write the
32
+ * exact defect it was written for went unseen.
33
+ *
34
+ * ## What it reports, and what it deliberately does not
35
+ *
36
+ * It reports DECLARATIONS, not values. `}, TIMEOUT)` is reported as the text
37
+ * `TIMEOUT`, with no attempt to resolve it — resolving constants would be a
38
+ * second interpreter, and the question here is "does this case have a budget
39
+ * of its own", which the declaration answers on its own. A declaration that
40
+ * happens to equal the global budget is still a declaration, and still takes
41
+ * the case out of the `was KILLED` row.
42
+ *
43
+ * `null` means the source did not parse. That is a REFUSAL, distinct from
44
+ * `[]`, and the caller must keep them distinct — a parser that stopped
45
+ * parsing reports exactly as clean as a file with no overrides, which is the
46
+ * defect class #968 exists to close.
47
+ *
48
+ * ## Import aliases are resolved, because matching the local name was a hole
49
+ *
50
+ * The review of this PR found the defect class reproduced in its own fix:
51
+ * `CASE_ROOTS` matched the LOCAL identifier, so
52
+ * `import { test as t } from "bun:test"` silently defeated the detector —
53
+ * MEASURED at 0 declarations for `t("slow", () => {}, 60_000)`, and the same
54
+ * for `setDefaultTimeout as sdt` and `beforeAll as bA`. Callees are now
55
+ * resolved through the module's import bindings to the name bun:test
56
+ * exports, so the alias, the namespace form (`import * as bt`, `bt.test(…)`)
57
+ * and the injected global are one question.
58
+ *
59
+ * ## WHAT THIS DOES NOT COVER (stated rather than implied)
60
+ *
61
+ * MEASURED to return `[]`:
62
+ *
63
+ * - **An indirection through a variable.** `const t = test; t("a", fn, 1000)`.
64
+ * Only import bindings are resolved, not assignments.
65
+ * - **Computed member access.** `bt["test"](…)` on a namespace import.
66
+ * - **A wrapper that declares the budget for you.** A local
67
+ * `slowTest(name, fn)` that calls `test(name, fn, 60_000)` internally IS
68
+ * reported, at the call inside the wrapper — but only because that call
69
+ * is in the same module. A wrapper imported from elsewhere is not.
70
+ *
71
+ * None of these appear in this repo: the 594-file census in the PR found no
72
+ * aliased, namespaced or indirected case declaration.
73
+ */
74
+
75
+ import {
76
+ type AstNode,
77
+ calleeRoot,
78
+ importBindings,
79
+ parseTsModule,
80
+ rootProperty,
81
+ walkNodes,
82
+ } from "./ts-ast";
83
+
84
+ /** The callees that declare a test case and accept a per-test budget. */
85
+ const CASE_ROOTS = new Set(["test", "it"]);
86
+
87
+ /**
88
+ * The hooks that accept a budget of their own.
89
+ *
90
+ * Included because a hook that is killed is reported as `(unnamed)` in
91
+ * bun's JUnit output — MEASURED on 1.3.14 — so work moved into a hook with
92
+ * a budget of its own is as far out of `timeoutProximityViolations`'s reach
93
+ * as a case with one. Same AST rule, different root set; leaving them out
94
+ * would be a gap for the sake of a shorter list.
95
+ */
96
+ const HOOK_ROOTS = new Set(["beforeAll", "beforeEach", "afterAll", "afterEach"]);
97
+
98
+ /** The bun:test call that raises the default budget for a whole file. */
99
+ const DEFAULT_TIMEOUT_FN = "setDefaultTimeout";
100
+
101
+ /** How a budget was raised. */
102
+ export type TimeoutKind =
103
+ /** A third argument to a `test`/`it` call: `}, 60_000)`, `}, { timeout: 60_000 })`. */
104
+ | "per-test"
105
+ /** A second argument to a `beforeAll`/`afterEach`/… call. */
106
+ | "hook"
107
+ /** A `setDefaultTimeout(…)` call, which raises it for every case in the file. */
108
+ | "file-default";
109
+
110
+ /** One construct that gives a case a budget other than the declared one. */
111
+ export interface TimeoutDeclaration {
112
+ kind: TimeoutKind;
113
+ /**
114
+ * The source text of the declaration, as it appears in the TRANSPILED
115
+ * module. Reported rather than evaluated — see the module comment.
116
+ */
117
+ expression: string;
118
+ /** The case name when it is a string literal, else `null`. */
119
+ caseName: string | null;
120
+ }
121
+
122
+ function literalString(node: unknown): string | null {
123
+ const n = node as AstNode | undefined;
124
+ if (n?.type === "Literal" && typeof n.value === "string") return n.value;
125
+ return null;
126
+ }
127
+
128
+ /**
129
+ * Every per-test and file-default budget declaration in `source`, in document
130
+ * order. `null` when the source does not parse.
131
+ */
132
+ export function perTestTimeoutDeclarations(source: string): TimeoutDeclaration[] | null {
133
+ const parsed = parseTsModule(source);
134
+ if (parsed === null) return null;
135
+ const bindings = importBindings(parsed.tree);
136
+
137
+ /**
138
+ * The bun:test name a callee resolves to, following import aliases.
139
+ *
140
+ * Matching the LOCAL identifier was the alias hole this closes:
141
+ * `import { test as t } from "bun:test"` made the detector report clean on
142
+ * its own subject (MEASURED: 0 declarations for `t("slow", () => {}, 60_000)`).
143
+ *
144
+ * Three cases, in order:
145
+ * - the local name is bound by a bun:test import → its EXPORTED name,
146
+ * so `t` resolves to `test`;
147
+ * - the local name is a bun:test NAMESPACE binding → the property called
148
+ * on it, so `bt.test(…)` resolves to `test`;
149
+ * - the local name is bound by some OTHER import → `null`, because
150
+ * `import { test } from "./helpers"` is not bun:test's;
151
+ * - otherwise the bare name, which is how bun's injected globals work in
152
+ * a file that imports nothing from bun:test.
153
+ */
154
+ function resolved(callee: unknown): string | null {
155
+ const root = calleeRoot(callee);
156
+ if (root === null) return null;
157
+ const binding = bindings.get(root);
158
+ if (binding === undefined) return root;
159
+ if (binding.module !== "bun:test") return null;
160
+ if (binding.imported === "*") return rootProperty(callee);
161
+ return binding.imported;
162
+ }
163
+
164
+ const out: TimeoutDeclaration[] = [];
165
+ for (const node of walkNodes(parsed.tree)) {
166
+ if (node.type !== "CallExpression") continue;
167
+ const args = node.arguments as AstNode[] | undefined;
168
+ if (!Array.isArray(args)) continue;
169
+ const root = resolved(node.callee);
170
+
171
+ if (root === DEFAULT_TIMEOUT_FN && args.length >= 1) {
172
+ out.push({
173
+ kind: "file-default",
174
+ expression: parsed.js.slice(args[0]!.start, args[0]!.end),
175
+ caseName: null,
176
+ });
177
+ continue;
178
+ }
179
+
180
+ // A per-test budget is the THIRD argument of a call rooted at test/it.
181
+ // Rooted, so `test.only`, `test.skip`, `test.if(x)` and
182
+ // `test.each([…])(…)` are all the same question rather than a list of
183
+ // spellings. `test.each([1])` on its own has one argument and is not a
184
+ // case declaration, so the arity check excludes it without naming it.
185
+ if (root !== null && CASE_ROOTS.has(root) && args.length >= 3) {
186
+ const arg = args[2]!;
187
+ out.push({
188
+ kind: "per-test",
189
+ expression: parsed.js.slice(arg.start, arg.end),
190
+ caseName: literalString(args[0]),
191
+ });
192
+ continue;
193
+ }
194
+
195
+ // A hook takes its budget as the SECOND argument, there being no name.
196
+ if (root !== null && HOOK_ROOTS.has(root) && args.length >= 2) {
197
+ const arg = args[1]!;
198
+ out.push({
199
+ kind: "hook",
200
+ expression: parsed.js.slice(arg.start, arg.end),
201
+ caseName: root,
202
+ });
203
+ }
204
+ }
205
+ return out;
206
+ }
207
+
208
+ /** One line per declaration, for a failure message that names the repair. */
209
+ export function describeTimeoutDeclarations(found: TimeoutDeclaration[]): string {
210
+ return found
211
+ .map(d =>
212
+ d.kind === "file-default"
213
+ ? `setDefaultTimeout(${d.expression}) raises the budget for every case in the file`
214
+ : d.kind === "hook"
215
+ ? `a ${d.caseName} hook declares a budget of ${d.expression}, and a killed hook is reported as \`(unnamed)\``
216
+ : `${d.caseName === null ? "a case" : `"${d.caseName}"`} declares a per-test budget of ${d.expression}`,
217
+ )
218
+ .join("\n");
219
+ }
@@ -0,0 +1,151 @@
1
+ /**
2
+ * Does a decomposition's plan allocate every file discovery said the issue
3
+ * touches? (#751)
4
+ *
5
+ * `workflows/ship.js` interpolates `discovery.filesToModify` into the
6
+ * to-issues prompt as "Files to modify" and then validates the answer two
7
+ * ways: no sub-issue may claim an out-of-scope file, and no two sub-issues
8
+ * may claim the same one. **Nothing compared the UNION of the sub-issues'
9
+ * `filesToModify` against the list it had just asked about.** The prompt
10
+ * asks; nothing checked that the answer covers it — the same gap the comment
11
+ * beside `denylistViolations` names for the deny-list and was never extended
12
+ * past.
13
+ *
14
+ * On the consumer run that motivated #751, the file the issue was *about* was
15
+ * claimed by no sub-issue, edited by two worktrees, and the run reached
16
+ * Commit before SC-413 refused with *"no worktree claimed that file"* —
17
+ * 2h06m and 2.03M subagent tokens of correct work ending `SHIP_FAILED` /
18
+ * `NO_PR`. The run is named on the issue; naming a consumer repository in
19
+ * harness source is what `test/anti.test.ts` SC-A1 refuses, and it is right
20
+ * to — this module ships to every project rungate scaffolds. That
21
+ * refusal is correct and is not loosened here; widening it is the documented
22
+ * cheapest wrong fix for this family (`.claude/rules/checks-must-be-able-to-fail.md`,
23
+ * the #627 section). This moves the detection to plan time, where it costs
24
+ * nothing.
25
+ *
26
+ * ## The decision #751 asked to be made out loud
27
+ *
28
+ * Three candidates, all precedented in `ship.js`. **Option 1: refuse the
29
+ * parallel plan and fall back to single-agent implement** — the same branch
30
+ * `hasOverlap` already takes, one line away.
31
+ *
32
+ * - **Not auto-claim (option 2).** The decomposer had the file list and chose
33
+ * not to allocate it. Assigning the file by a heuristic — same directory,
34
+ * first sub-issue, largest — hands work to an agent whose brief never
35
+ * mentions it, and whose claim audit then PASSES while the allocation is
36
+ * wrong. It converts a loud, early, correct refusal into a quiet
37
+ * misallocation, which is strictly worse than the state being fixed.
38
+ * - **Not warn-and-proceed (option 3).** That is the status quo, and the
39
+ * status quo is a run spending its whole agent budget to discover at Commit
40
+ * what was knowable before the first agent spawned.
41
+ * - **Option 1 loses nothing.** The single-agent path may modify every file,
42
+ * so the work still ships; what is given up is parallelism, which is an
43
+ * optimisation and not a requirement. The failure is moved from the end of
44
+ * the run to the start of it.
45
+ *
46
+ * ## An empty discovery list is UNMEASURED, not "fully covered"
47
+ *
48
+ * The union of nothing covers nothing perfectly — the third shape, and #751
49
+ * names it as the cheapest wrong fix for this check specifically: *treat an
50
+ * absent or empty `discovery.filesToModify` as satisfied*. It silences every
51
+ * synthetic fixture and leaves exactly the #1585 case uncaught, because the
52
+ * thing that needs covering is a list nobody is obliged to produce.
53
+ *
54
+ * **That branch is unreachable from today's call site, and saying so is the
55
+ * point** (#492). `ship.js` only decomposes when
56
+ * `discovery.sizing === 'M' && (discovery.filesToModify || []).length >= 3`,
57
+ * so every live plan arrives with at least three files and the empty reading
58
+ * cannot occur. It is reported anyway, because the precondition is one `if`
59
+ * in another function that nothing holds still, and a branch that only exists
60
+ * while an unrelated guard keeps its shape is a branch that goes silently
61
+ * wrong the day that guard moves.
62
+ */
63
+
64
+ import { normalisePath } from "./scope-denylist";
65
+
66
+ export interface PlanSubIssue {
67
+ title?: string;
68
+ filesToModify?: string[];
69
+ }
70
+
71
+ export interface PlanCoverageReading {
72
+ reading: "MEASURED" | "UNMEASURED";
73
+ unmeasuredReasons: string[];
74
+ /** Discovered files no sub-issue claims, in the spelling discovery used. */
75
+ uncovered: string[];
76
+ /** How many files were compared, so a PASS can report its own coverage. */
77
+ discovered: number;
78
+ /** How many distinct files the plan allocates, including any not discovered. */
79
+ claimed: number;
80
+ }
81
+
82
+ /**
83
+ * Compare the plan's union against what discovery found.
84
+ *
85
+ * Path equality goes through `normalisePath`, the same binding the deny-list
86
+ * uses, so `./lib/a.ts` and `lib/a.ts` are one file here exactly as they are
87
+ * there. A second notion of "same path" in the same workflow would agree on
88
+ * every input anyone has and diverge on the one that matters.
89
+ */
90
+ export function planCoverage(
91
+ discoveryFiles: string[] | undefined | null,
92
+ subIssues: PlanSubIssue[] | undefined | null,
93
+ ): PlanCoverageReading {
94
+ const discovered = (discoveryFiles || []).map(f => String(f)).filter(f => f.trim() !== "");
95
+ const subs = subIssues || [];
96
+
97
+ const claimedSet = new Set<string>();
98
+ for (const s of subs) {
99
+ for (const f of s.filesToModify || []) {
100
+ const n = normalisePath(f);
101
+ if (n !== "") claimedSet.add(n);
102
+ }
103
+ }
104
+
105
+ const reasons: string[] = [];
106
+ if (discovered.length === 0) {
107
+ reasons.push(
108
+ "discovery named no files to modify, so there is nothing to compare the plan against. " +
109
+ 'The union of an empty list is covered by any plan at all, which is not "the plan is ' +
110
+ 'complete" — it is "nobody looked" (#751).',
111
+ );
112
+ }
113
+ if (subs.length === 0) {
114
+ reasons.push(
115
+ "the decomposition produced no sub-issues, so there is no plan to compare. An empty " +
116
+ "plan agrees with every discovery.",
117
+ );
118
+ }
119
+
120
+ return {
121
+ reading: reasons.length === 0 ? "MEASURED" : "UNMEASURED",
122
+ unmeasuredReasons: reasons,
123
+ uncovered:
124
+ reasons.length === 0 ? discovered.filter(f => !claimedSet.has(normalisePath(f))) : [],
125
+ discovered: discovered.length,
126
+ claimed: claimedSet.size,
127
+ };
128
+ }
129
+
130
+ /**
131
+ * The sentence `ship.js` logs when the plan must not be used, or `null`.
132
+ *
133
+ * One binding so there is one thing to mutate, and so the refusal and the
134
+ * reading cannot disagree about whether a plan is usable. An UNMEASURED
135
+ * reading refuses too: a plan nobody could check is not a plan that passed.
136
+ */
137
+ export function planCoverageRefusal(r: PlanCoverageReading): string | null {
138
+ if (r.reading === "UNMEASURED") {
139
+ return (
140
+ `PLAN COVERAGE UNMEASURED: ${r.unmeasuredReasons.join(" ")} ` +
141
+ `Falling back to single-agent implement, which may touch any file.`
142
+ );
143
+ }
144
+ if (r.uncovered.length === 0) return null;
145
+ return (
146
+ `PLAN COVERAGE: ${r.uncovered.length} of ${r.discovered} discovered file(s) are claimed by ` +
147
+ `no sub-issue — ${r.uncovered.join(", ")}. A file nobody claims is a file two worktrees ` +
148
+ `may both edit, and the SC-413 claim audit refuses it at Commit after the whole agent ` +
149
+ `budget is spent (#751). Falling back to single-agent implement.`
150
+ );
151
+ }