rungate 0.6.0 → 0.8.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 (164) hide show
  1. package/hooks/ActionVerificationGate.hook.ts +96 -0
  2. package/hooks/AgentBriefGuard.hook.ts +38 -0
  3. package/hooks/AgentVerdictCapture.hook.ts +87 -0
  4. package/hooks/AutoVerifyGate.hook.ts +79 -0
  5. package/hooks/BashToolGuard.hook.ts +65 -0
  6. package/hooks/BashWriteGuard.hook.ts +60 -0
  7. package/hooks/CommitEnforcement.hook.ts +130 -0
  8. package/hooks/DocHygiene.hook.ts +148 -0
  9. package/hooks/GateEnforcement.hook.ts +58 -0
  10. package/hooks/GateResultProtection.hook.ts +69 -0
  11. package/hooks/HarnessAFKGate.hook.ts +85 -0
  12. package/hooks/HookCurrency.hook.ts +99 -0
  13. package/hooks/IssueCloseGuard.hook.ts +149 -0
  14. package/hooks/MergeGuard.hook.ts +58 -0
  15. package/hooks/PostCompact.hook.ts +60 -0
  16. package/hooks/ProjectStateSync.hook.ts +103 -0
  17. package/hooks/SkillGuard.hook.ts +69 -0
  18. package/hooks/SpecConformityTrigger.hook.ts +43 -0
  19. package/hooks/SpecSCGuard.hook.ts +123 -0
  20. package/hooks/StaleTTLCleanup.hook.ts +141 -0
  21. package/hooks/TaskCompleted.hook.ts +51 -0
  22. package/hooks/TeamInit.hook.ts +85 -0
  23. package/hooks/TestSuiteGuard.hook.ts +70 -0
  24. package/hooks/TestSuiteRelease.hook.ts +85 -0
  25. package/hooks/TopLevelCdGuard.hook.ts +108 -0
  26. package/hooks/VerifyPhaseLock.hook.ts +84 -0
  27. package/hooks/WorkflowStateGuard.hook.ts +40 -0
  28. package/hooks/lib/agent-config.json +9 -0
  29. package/hooks/lib/agentDetection.ts +201 -0
  30. package/hooks/lib/comment-template.ts +86 -0
  31. package/hooks/lib/findWorkflow.ts +101 -0
  32. package/hooks/lib/parseStdin.ts +37 -0
  33. package/hooks/lib/paths.ts +111 -0
  34. package/hooks/lib/utils.ts +338 -0
  35. package/lib/affected-tests.ts +427 -0
  36. package/lib/agent-audit.ts +2 -3
  37. package/lib/agent-worktree.ts +287 -0
  38. package/lib/agents-md-sections.ts +81 -16
  39. package/lib/bash-write-guard.ts +373 -0
  40. package/lib/behavioral-cache.ts +150 -2
  41. package/lib/branch-cleanup.ts +279 -7
  42. package/lib/branch-protection.ts +790 -0
  43. package/lib/brief-ac-alignment.ts +104 -0
  44. package/lib/brief-references.ts +205 -0
  45. package/lib/bun-version.ts +93 -14
  46. package/lib/check-remedy.ts +140 -0
  47. package/lib/ci-checkout.ts +17 -0
  48. package/lib/ci-history-depth.ts +159 -0
  49. package/lib/ci-setup-step.ts +96 -0
  50. package/lib/claude-code-action.ts +85 -0
  51. package/lib/claude-tool-vocabulary.ts +188 -0
  52. package/lib/cli-commands.ts +15 -0
  53. package/lib/commit-sha.ts +42 -0
  54. package/lib/commit-subject.ts +90 -0
  55. package/lib/compliance-report.ts +377 -45
  56. package/lib/compliance.ts +191 -17
  57. package/lib/config-loader.ts +39 -1
  58. package/lib/config-trackable.ts +400 -0
  59. package/lib/conformity.ts +1087 -100
  60. package/lib/containment.ts +238 -0
  61. package/lib/content-hash.ts +24 -0
  62. package/lib/create-sc.ts +249 -16
  63. package/lib/dev-liveness.ts +180 -0
  64. package/lib/discovery-branch-points.ts +432 -0
  65. package/lib/discovery-stability.ts +481 -0
  66. package/lib/distribution-currency.ts +236 -0
  67. package/lib/doc-routing.ts +157 -0
  68. package/lib/evidence-prevalidator.ts +224 -2
  69. package/lib/evidence-satisfiability.ts +534 -12
  70. package/lib/failure-intake.ts +323 -0
  71. package/lib/fallow-availability.ts +157 -0
  72. package/lib/fs-write-capabilities.ts +232 -0
  73. package/lib/gate-enforcement.ts +222 -28
  74. package/lib/gate-names.ts +72 -0
  75. package/lib/gate-result-protection.ts +427 -0
  76. package/lib/gate-run-split.ts +147 -0
  77. package/lib/generators/agents-md.ts +140 -32
  78. package/lib/git-isolation.ts +140 -0
  79. package/lib/github.ts +15 -0
  80. package/lib/google-auth-action.ts +93 -0
  81. package/lib/harness-project.ts +48 -0
  82. package/lib/harness-provenance.ts +255 -0
  83. package/lib/hook-evidence.ts +235 -0
  84. package/lib/hook-scopes.ts +386 -0
  85. package/lib/hook-target-tree.ts +398 -0
  86. package/lib/hook-trees.ts +447 -0
  87. package/lib/intake-sources.ts +380 -0
  88. package/lib/js-top-level-code.ts +78 -10
  89. package/lib/junit-checks.ts +52 -0
  90. package/lib/late-env-spawn.ts +331 -0
  91. package/lib/launch-base.ts +376 -0
  92. package/lib/line-citation.ts +372 -0
  93. package/lib/orphan-reaper.ts +468 -0
  94. package/lib/parity-binding.ts +205 -0
  95. package/lib/paths.ts +96 -3
  96. package/lib/per-test-timeouts.ts +219 -0
  97. package/lib/phase2-decomposition.ts +1 -1
  98. package/lib/plan-coverage.ts +151 -0
  99. package/lib/prior-branch.ts +201 -8
  100. package/lib/process-table.ts +146 -0
  101. package/lib/project-state-derivation.ts +219 -0
  102. package/lib/project-state-root.ts +258 -0
  103. package/lib/published-artifact.ts +669 -0
  104. package/lib/readdir-sorted.ts +45 -0
  105. package/lib/review-execution-reading.ts +206 -0
  106. package/lib/review-tool-agreement.ts +433 -0
  107. package/lib/rule-promotion.ts +234 -0
  108. package/lib/rule-retirement.ts +525 -0
  109. package/lib/run-branch.ts +612 -0
  110. package/lib/rungate-schema.ts +19 -0
  111. package/lib/runner-home.ts +274 -0
  112. package/lib/sc-id-cross-tree.ts +103 -15
  113. package/lib/sc-ids.ts +121 -13
  114. package/lib/sc-reservations.ts +289 -0
  115. package/lib/scaffold/ai-review-findings.ts +409 -0
  116. package/lib/scaffold/ai-review-step.ts +5406 -64
  117. package/lib/scaffold/defaults.ts +82 -1
  118. package/lib/scaffold/managed-workflow.ts +587 -192
  119. package/lib/scaffold/owned-files.ts +152 -45
  120. package/lib/scaffold/pre-push-hook.ts +49 -5
  121. package/lib/scaffold/steps.ts +1836 -275
  122. package/lib/scaffold/workflow-census.ts +217 -0
  123. package/lib/scaffold/workflow-drift.ts +217 -0
  124. package/lib/scaffold/write-steps.ts +4 -0
  125. package/lib/scan-budget.ts +247 -0
  126. package/lib/scanner.ts +43 -51
  127. package/lib/scratch-roots.ts +50 -5
  128. package/lib/screenshot-discovery.ts +121 -0
  129. package/lib/screenshot-upload.ts +41 -1
  130. package/lib/secret-patterns.ts +511 -8
  131. package/lib/security-verdict.ts +1 -1
  132. package/lib/shell-path-interpolation.ts +594 -0
  133. package/lib/ship-mode.ts +219 -0
  134. package/lib/source-slice.ts +294 -0
  135. package/lib/stale-cleanup.ts +37 -3
  136. package/lib/suite-flake.ts +278 -0
  137. package/lib/suite-measurement.ts +23 -4
  138. package/lib/suite-run-reading.ts +241 -1
  139. package/lib/suite-timeout-margin.ts +193 -0
  140. package/lib/test-case-bodies.ts +883 -0
  141. package/lib/test-case-deletion.ts +653 -0
  142. package/lib/test-census.ts +97 -29
  143. package/lib/test-command-positions.ts +274 -0
  144. package/lib/test-suite-lock.ts +159 -16
  145. package/lib/text-dependents.ts +875 -0
  146. package/lib/tool-probe.ts +31 -9
  147. package/lib/top-level-cd.ts +393 -0
  148. package/lib/tree-entries.ts +89 -0
  149. package/lib/ts-ast.ts +200 -0
  150. package/lib/walk-test-files.ts +97 -0
  151. package/lib/workflow-actions.ts +263 -0
  152. package/lib/workflow-security.ts +29 -0
  153. package/lib/workflow-taint.ts +732 -0
  154. package/lib/worktree-cleanup.ts +65 -38
  155. package/lib/worktree-collect.ts +177 -5
  156. package/lib/worktree-repo.ts +68 -3
  157. package/package.json +4 -1
  158. package/scripts/ai-review-findings.ts +45 -0
  159. package/scripts/check-installed-hook-currency.ts +825 -0
  160. package/scripts/create-sc.ts +21 -1
  161. package/scripts/extract-constraints.ts +107 -7
  162. package/scripts/generate-code-map.ts +23 -30
  163. package/scripts/scaffold-project.ts +8 -1
  164. package/scripts/sync-sc-status.ts +77 -16
@@ -0,0 +1,380 @@
1
+ /**
2
+ * Where observed-failure candidates come from, and how a source refuses when
3
+ * its input is not there (#545).
4
+ *
5
+ * ## The decision this file is, and why it comes before a fourth scanner
6
+ *
7
+ * #545 names four intake sources. `lib/failure-intake.ts` ships one — `git
8
+ * log` — and its docblock records why the other three stopped there: each
9
+ * needs something a check running in the configuration CI runs it in does not
10
+ * have.
11
+ *
12
+ * | Source | Needs | Present under an empty `HOME`, no credentials |
13
+ * |---|---|---|
14
+ * | `git log` fix/revert | the repository | yes |
15
+ * | gate refusals | a populated run store | no |
16
+ * | CI failures on merged PRs | an authenticated API client | no |
17
+ * | transcript corrections | a session store, and a confidentiality decision | no |
18
+ *
19
+ * **A miner whose input is absent has no failing input, so it passes
20
+ * vacuously.** Adding a second scanner before deciding how absence is
21
+ * reported produces a detector that is green everywhere it matters — the
22
+ * defect class `.claude/rules/checks-must-be-able-to-fail.md` opens with, one
23
+ * level up, because here the thing that cannot fail is the thing that decides
24
+ * what the rules are.
25
+ *
26
+ * So this module is the contract, not a scanner:
27
+ *
28
+ * - every source declares a **precondition** naming what it needs;
29
+ * - a source whose precondition is unmet reports `UNMEASURED` with a reason
30
+ * that names the source AND the missing input — never an empty candidate
31
+ * list;
32
+ * - the aggregate reads `MEASURED` only if **every registered source** did,
33
+ * so a reading cannot be completed by the sources that happened to work;
34
+ * - and `test/intake-sources.test.ts` drives every registered source in a
35
+ * **barren context** — an empty directory, no repository, no store — and
36
+ * requires each to refuse there. A scanner added without a reachable
37
+ * unavailable state fails that case rather than passing silently.
38
+ *
39
+ * That last point is the mechanical half. It is cheap to write a source that
40
+ * reports `MEASURED` over nothing; it is not possible to register one here
41
+ * and keep the suite green.
42
+ *
43
+ * ## Why gate refusals are the second source and the other two are not
44
+ *
45
+ * The run store is **offline, local, and needs no credential** — the same
46
+ * three properties that made `git log` first. CI failures need an
47
+ * authenticated client, and transcripts need a confidentiality decision
48
+ * nobody has made; both are still unregistered, deliberately, because
49
+ * registering a source whose `read` cannot run is dead code wearing a
50
+ * contract.
51
+ *
52
+ * ## What a gate refusal contributes that a commit subject cannot
53
+ *
54
+ * A `fix:` subject is one author's account of one failure. A gate refusal is
55
+ * the harness's own record that a check stopped a run, and it REPEATS.
56
+ * Measured across this machine's 44 run stores: 111 refusal entries over 19
57
+ * distinct check names, 28 of the 44 runs carrying at least one. The most
58
+ * frequent single check accounts for 22 of them.
59
+ *
60
+ * That ratio is the reason the dedup key here is the CHECK NAME and not the
61
+ * entry: one candidate per entry would produce 111 candidates ranked by
62
+ * recency, dominated by repeats of one check, against a promotion budget of
63
+ * 16. One candidate per distinct check produces 19, and the repetition
64
+ * becomes evidence rather than noise.
65
+ *
66
+ * ## WHAT THIS DOES NOT COVER
67
+ *
68
+ * - **A check that fails because it is broken, not because the tree is.** A
69
+ * flaky or mis-written gate check repeats exactly like a real one, and
70
+ * nothing here reads the detail to tell them apart. The repetition this
71
+ * module treats as evidence is the same signal in both cases.
72
+ * - **Occurrence count does not reach the ranker.** It is written into the
73
+ * candidate's `context`, where a human reads it, and `lib/rule-promotion.ts`
74
+ * ranks on `evidence` alone. Wiring frequency into the score is a change to
75
+ * #544's criterion and is deliberately not taken here.
76
+ * - **The store is this machine's, not the project's.** A candidate mined
77
+ * here cites a run id nobody else can resolve. That is why the source is
78
+ * UNMEASURED on a runner rather than merely empty — the citation is as
79
+ * local as the evidence.
80
+ */
81
+
82
+ import { existsSync, readdirSync, readFileSync } from "fs";
83
+ import { join } from "path";
84
+
85
+ import { contentHash } from "./content-hash";
86
+ import type { EvidenceCandidate, FailureIntakeReading } from "./failure-intake";
87
+ import { mineFixCommits } from "./failure-intake";
88
+ import { workStoreRoot } from "./paths";
89
+
90
+ /**
91
+ * Prefixes a reason that means "this source could not be read here", as
92
+ * distinct from "this source was read and found nothing".
93
+ *
94
+ * Exported because a caller distinguishing the cases must compare against the
95
+ * producer's own spelling rather than a second copy of it.
96
+ */
97
+ export const SOURCE_UNAVAILABLE = "SOURCE_UNAVAILABLE";
98
+
99
+ /** The run store held no `workflow-state.json` this process could read. */
100
+ export const NO_RUN_STORE = "NO_RUN_STORE";
101
+ /** The store exists and a state file in it would not parse. */
102
+ export const RUN_STORE_UNREADABLE = "RUN_STORE_UNREADABLE";
103
+
104
+ /** Everything a source is allowed to read. Explicit so a test can be barren. */
105
+ export interface IntakeContext {
106
+ /** The project being mined. */
107
+ projectRoot: string;
108
+ /** The run store root — `lib/paths.ts` owns the default. */
109
+ workStore: string;
110
+ }
111
+
112
+ /**
113
+ * The default context. Separate from `mineObservedFailures` so a caller can
114
+ * mine a project without inheriting this machine's store, and so the test can
115
+ * build one that is barren by construction.
116
+ */
117
+ export function defaultIntakeContext(projectRoot: string): IntakeContext {
118
+ return { projectRoot, workStore: workStoreRoot() };
119
+ }
120
+
121
+ export interface IntakeSource {
122
+ /** Stable id. Appears in every reason this source produces. */
123
+ id: string;
124
+ /** What the source needs, in words a reader can act on. */
125
+ needs: string;
126
+ /**
127
+ * `null` when the source can be read here; otherwise a short reason naming
128
+ * what is absent. A source that can never return non-null has no reachable
129
+ * unavailable state, which `test/intake-sources.test.ts` refuses.
130
+ */
131
+ precondition(ctx: IntakeContext): string | null;
132
+ /** Only called when `precondition` returned `null`. */
133
+ read(ctx: IntakeContext): FailureIntakeReading;
134
+ }
135
+
136
+ /**
137
+ * The aggregate reading — `MEASURED` only when every registered source was,
138
+ * and a union rather than a record so that **`candidates` exists only on the
139
+ * measured variant**.
140
+ *
141
+ * The hazard that shape is for: a reader who takes `candidates.length`
142
+ * without consulting `reading` gets a number that looks like a measurement
143
+ * and is a function of something else entirely. The worked instance is this
144
+ * repository's own development checkout — `mineFixCommits` correctly reports
145
+ * `UNMEASURED` / `SHALLOW_HISTORY` there and still returns 182 candidates,
146
+ * because the clone has a depth. 182 is a reading of a clone parameter, not
147
+ * of the project's history, and nothing about the list says so.
148
+ *
149
+ * Adjacency does not fix that. A warning beside a number is read after the
150
+ * number, if at all. So on the `UNMEASURED` variant the key is **absent**: a
151
+ * consumer that narrows on `reading` is handed `candidates`, and one that
152
+ * does not gets a type error at compile time and `undefined` at runtime,
153
+ * which throws instead of returning something plausible. The partial list is
154
+ * still reachable — withholding it would make a partial reading and an empty
155
+ * one look alike — but under a name nobody reaches for by accident.
156
+ *
157
+ * **Applied here and deliberately not to `FailureIntakeReading`.** That type
158
+ * is shipped and has a production consumer, and that consumer already does
159
+ * the right thing: `scripts/extract-constraints.ts` spreads
160
+ * `evidence.candidates` into the promotion AND passes the reading as an
161
+ * `upstream`, so `promoteCandidates` folds `UNMEASURED` into its own verdict.
162
+ * The hazard is latent there, not live. Converting a shipped type to prevent
163
+ * a hazard nobody has hit is a wider change than this one should make.
164
+ */
165
+ export type AggregateIntakeReading =
166
+ | {
167
+ reading: "MEASURED";
168
+ unmeasuredReasons: string[];
169
+ /** Per source, so "read and empty" and "not readable" stay tellable apart. */
170
+ bySource: Record<string, FailureIntakeReading>;
171
+ /** Every candidate every source produced. Present only on this variant. */
172
+ candidates: EvidenceCandidate[];
173
+ }
174
+ | {
175
+ reading: "UNMEASURED";
176
+ unmeasuredReasons: string[];
177
+ bySource: Record<string, FailureIntakeReading>;
178
+ /**
179
+ * What the sources that COULD be read produced. Deliberately not
180
+ * spelled `candidates`: counting these is a decision, and the name is
181
+ * the place that decision gets made.
182
+ */
183
+ partialCandidates: EvidenceCandidate[];
184
+ };
185
+
186
+ // ── The gate-refusal source ──────────────────────────────────────────────
187
+
188
+ /** One refusal entry, as `workflow-state.json` writes it. */
189
+ interface StoredFailure {
190
+ check?: unknown;
191
+ result?: unknown;
192
+ }
193
+
194
+ function refusalsIn(state: unknown): Array<{ gate: string; check: string; ts: string }> {
195
+ const out: Array<{ gate: string; check: string; ts: string }> = [];
196
+ const gates = (state as { gates?: unknown } | null)?.gates;
197
+ if (!gates || typeof gates !== "object") return out;
198
+ for (const [gate, entry] of Object.entries(gates as Record<string, unknown>)) {
199
+ const e = entry as { failures?: unknown; ts?: unknown } | null;
200
+ if (!e || !Array.isArray(e.failures)) continue;
201
+ const ts = typeof e.ts === "string" ? e.ts : "";
202
+ for (const f of e.failures as StoredFailure[]) {
203
+ if (!f || typeof f.check !== "string" || f.check.trim().length === 0) continue;
204
+ out.push({ gate, check: f.check.trim(), ts });
205
+ }
206
+ }
207
+ return out;
208
+ }
209
+
210
+ /**
211
+ * Mine gate refusals out of the run store.
212
+ *
213
+ * **An unreadable state file is a refusal, not a skip.** `lib/gate-
214
+ * enforcement.ts`'s reader of the same files wraps its walk in
215
+ * `catch { /* skip *\/ }`, which converts a corrupt store into an empty one —
216
+ * the fail-open `.claude/rules/config-access.md` names by name. Here a file
217
+ * that will not parse pushes `RUN_STORE_UNREADABLE` and the reading says so,
218
+ * because "no run ever refused" and "I could not read the runs" are opposite
219
+ * facts about this project's history.
220
+ */
221
+ export function mineGateRefusals(workStore: string): FailureIntakeReading {
222
+ const unmeasuredReasons: string[] = [];
223
+ if (!existsSync(workStore)) {
224
+ return { reading: "UNMEASURED", unmeasuredReasons: [NO_RUN_STORE], commitsScanned: 0, candidates: [] };
225
+ }
226
+
227
+ let dirs: string[];
228
+ try {
229
+ dirs = readdirSync(workStore, { withFileTypes: true })
230
+ .filter((d) => d.isDirectory())
231
+ .map((d) => d.name);
232
+ } catch (e) {
233
+ return {
234
+ reading: "UNMEASURED",
235
+ unmeasuredReasons: [`${RUN_STORE_UNREADABLE}: ${(e as Error).message}`],
236
+ commitsScanned: 0,
237
+ candidates: [],
238
+ };
239
+ }
240
+
241
+ /** check name → the evidence, newest kept. */
242
+ const byCheck = new Map<
243
+ string,
244
+ { gate: string; ts: string; slug: string; issues: number[]; occurrences: number; runs: Set<string> }
245
+ >();
246
+ let statesRead = 0;
247
+
248
+ for (const slug of dirs.sort()) {
249
+ const path = join(workStore, slug, "workflow-state.json");
250
+ if (!existsSync(path)) continue;
251
+ let state: unknown;
252
+ try {
253
+ state = JSON.parse(readFileSync(path, "utf-8"));
254
+ } catch (e) {
255
+ unmeasuredReasons.push(`${RUN_STORE_UNREADABLE}: ${slug}: ${(e as Error).message}`);
256
+ continue;
257
+ }
258
+ statesRead++;
259
+ const issue = (state as { issue?: unknown }).issue;
260
+ const issues = typeof issue === "number" ? [issue] : [];
261
+ for (const r of refusalsIn(state)) {
262
+ const prior = byCheck.get(r.check);
263
+ if (prior === undefined) {
264
+ byCheck.set(r.check, {
265
+ gate: r.gate, ts: r.ts, slug, issues, occurrences: 1, runs: new Set([slug]),
266
+ });
267
+ continue;
268
+ }
269
+ prior.occurrences++;
270
+ prior.runs.add(slug);
271
+ // Newest citation wins, matching `mineFixCommits`' newest-first rule, so
272
+ // a reader following the citation lands on the most recent observation.
273
+ // An entry with no `ts` never displaces one that has a date.
274
+ if (r.ts !== "" && r.ts > prior.ts) {
275
+ prior.ts = r.ts;
276
+ prior.gate = r.gate;
277
+ prior.slug = slug;
278
+ prior.issues = issues;
279
+ }
280
+ }
281
+ }
282
+
283
+ // A store directory with no state files in it is not a measured zero: the
284
+ // walk found nothing to read, which is the NO_RUN_STORE case arriving by a
285
+ // different route.
286
+ if (statesRead === 0) unmeasuredReasons.push(NO_RUN_STORE);
287
+
288
+ const candidates: EvidenceCandidate[] = [];
289
+ for (const [check, ev] of [...byCheck.entries()].sort((a, b) => a[0].localeCompare(b[0]))) {
290
+ // A refusal with no date is not evidence-backed — `CandidateEvidence`
291
+ // says so in its own comment — so it is dropped rather than dated `""`.
292
+ if (ev.ts === "") continue;
293
+ candidates.push({
294
+ rule: check,
295
+ source: `run:${ev.slug}`,
296
+ context:
297
+ `gate '${ev.gate}' refused on this check — ` +
298
+ `${ev.occurrences} occurrence(s) across ${ev.runs.size} run(s)`,
299
+ hash: contentHash(check),
300
+ evidence: { kind: "gate-refusal", date: ev.ts, citation: ev.slug, issues: ev.issues },
301
+ });
302
+ }
303
+
304
+ return {
305
+ reading: unmeasuredReasons.length > 0 ? "UNMEASURED" : "MEASURED",
306
+ unmeasuredReasons,
307
+ // Reusing the field's meaning: units of history this source actually read.
308
+ commitsScanned: statesRead,
309
+ candidates,
310
+ };
311
+ }
312
+
313
+ // ── The registry ─────────────────────────────────────────────────────────
314
+
315
+ /**
316
+ * Every source that is wired up. Adding one here puts it under
317
+ * `test/intake-sources.test.ts`'s barren-context case immediately — which is
318
+ * the point, and is why the two unregistered sources below are named in prose
319
+ * rather than stubbed in code.
320
+ */
321
+ export const INTAKE_SOURCES: readonly IntakeSource[] = [
322
+ {
323
+ id: "fix-commits",
324
+ needs: "a git repository with unshallowed history",
325
+ precondition: (ctx) =>
326
+ existsSync(join(ctx.projectRoot, ".git")) ? null : "no git repository at the project root",
327
+ read: (ctx) => mineFixCommits(ctx.projectRoot),
328
+ },
329
+ {
330
+ id: "gate-refusals",
331
+ needs: "a populated rungate run store (RUNGATE_WORK_DIR, or $HOME/.rungate)",
332
+ precondition: (ctx) =>
333
+ existsSync(ctx.workStore) ? null : `no run store at ${ctx.workStore}`,
334
+ read: (ctx) => mineGateRefusals(ctx.workStore),
335
+ },
336
+ ];
337
+
338
+ /**
339
+ * Read every registered source, and say which ones could not be read.
340
+ *
341
+ * The aggregate is `MEASURED` only when every source was. A reading completed
342
+ * by the sources that happened to work is the Silent Default Substitution
343
+ * this whole module exists to refuse: zero CI-failure candidates because
344
+ * there is no API client reads exactly like zero because CI never failed.
345
+ */
346
+ export function mineObservedFailures(
347
+ ctx: IntakeContext,
348
+ sources: readonly IntakeSource[] = INTAKE_SOURCES,
349
+ ): AggregateIntakeReading {
350
+ const bySource: Record<string, FailureIntakeReading> = {};
351
+ const unmeasuredReasons: string[] = [];
352
+ const candidates: EvidenceCandidate[] = [];
353
+
354
+ for (const source of sources) {
355
+ const missing = source.precondition(ctx);
356
+ if (missing !== null) {
357
+ const reason = `${SOURCE_UNAVAILABLE}: ${source.id}: ${missing} (needs ${source.needs})`;
358
+ bySource[source.id] = {
359
+ reading: "UNMEASURED",
360
+ unmeasuredReasons: [reason],
361
+ commitsScanned: 0,
362
+ candidates: [],
363
+ };
364
+ unmeasuredReasons.push(reason);
365
+ continue;
366
+ }
367
+ const reading = source.read(ctx);
368
+ bySource[source.id] = reading;
369
+ for (const r of reading.unmeasuredReasons) unmeasuredReasons.push(`${source.id}: ${r}`);
370
+ candidates.push(...reading.candidates);
371
+ }
372
+
373
+ // The discriminator and the key are chosen together, in one place, so the
374
+ // two cannot disagree — there is no path that reports UNMEASURED and still
375
+ // hands back a `candidates` a caller can count without asking.
376
+ if (unmeasuredReasons.length > 0) {
377
+ return { reading: "UNMEASURED", unmeasuredReasons, bySource, partialCandidates: candidates };
378
+ }
379
+ return { reading: "MEASURED", unmeasuredReasons, bySource, candidates };
380
+ }
@@ -27,12 +27,66 @@
27
27
  * not express.
28
28
  */
29
29
 
30
- /** Characters after which a `/` begins a regex literal rather than division. */
31
- function regexCanFollow(prev: string): boolean {
32
- // Scanning back over the last significant character is enough in practice:
33
- // after a value (identifier, number, closing bracket) a slash is division;
34
- // after an operator, keyword or opening bracket it starts a regex.
35
- return !/[A-Za-z0-9_$)\]]/.test(prev);
30
+ /**
31
+ * Every ECMAScript reserved word. A closed list taken from the grammar, not a
32
+ * selection of the ones that have bitten us — which is the whole point of
33
+ * #905: the previous decision was "scanning back over the last significant
34
+ * CHARACTER is enough in practice", and it was not. `return` ends in `n`, so
35
+ * `return /re/` read as division, and the first `/` the tokenizer then
36
+ * accepted was one inside the literal.
37
+ */
38
+ const RESERVED_WORDS = new Set([
39
+ "await", "break", "case", "catch", "class", "const", "continue", "debugger",
40
+ "default", "delete", "do", "else", "enum", "export", "extends", "false",
41
+ "finally", "for", "function", "if", "import", "in", "instanceof", "new",
42
+ "null", "return", "super", "switch", "this", "throw", "true", "try",
43
+ "typeof", "var", "void", "while", "with", "yield",
44
+ ]);
45
+
46
+ /**
47
+ * The reserved words that are themselves VALUES. A `/` after one of these
48
+ * divides; after every other reserved word it opens a regex literal.
49
+ *
50
+ * Stated as the complement on purpose. The set of keywords a regex may follow
51
+ * is large and open to being under-enumerated one keyword at a time; the set
52
+ * of keywords that are values is five and closed. The remaining reserved
53
+ * words either take an expression (`return`, `throw`, `case`, `of`, …) or
54
+ * cannot be followed by `/` at all in valid source (`if`, `while`, `var`, …),
55
+ * and both readings want the same answer.
56
+ *
57
+ * `of` and `as` are contextual, not reserved. `of` is listed below rather
58
+ * than here because `for (const c of /re/.source)` is real code; everything
59
+ * else contextual (`let`, `async`, `static`, `get`, `set`, `from`) is an
60
+ * ordinary identifier in the positions where a `/` can follow it.
61
+ */
62
+ const VALUE_KEYWORDS = new Set(["this", "super", "null", "true", "false"]);
63
+
64
+ /** Contextual keywords that take an expression. */
65
+ const EXPRESSION_CONTEXTUAL = new Set(["of"]);
66
+
67
+ /**
68
+ * Whether a `/` immediately after `token` begins a regex literal.
69
+ *
70
+ * `token` is a whole token, never a fragment: a word, a numeric run, a single
71
+ * punctuator, `""` at the start of input, or a word prefixed with `.` when it
72
+ * was reached through a member access. The one binding that decides this, so
73
+ * there is one thing to mutate.
74
+ */
75
+ export function regexCanFollowToken(token: string): boolean {
76
+ if (token === "") return true; // start of input: expression position
77
+ // `o.return` is a property read, so the word is a value whatever it spells.
78
+ if (token.startsWith(".")) return false;
79
+ if (/^[0-9]/.test(token)) return false; // numeric literal
80
+ if (/^[A-Za-z_$]/.test(token)) {
81
+ if (EXPRESSION_CONTEXTUAL.has(token)) return true;
82
+ // A plain identifier is a value. Only a reserved word that is not itself
83
+ // a value leaves the parser in expression position.
84
+ return RESERVED_WORDS.has(token) && !VALUE_KEYWORDS.has(token);
85
+ }
86
+ // Punctuators. `)` and `]` close a value; every other one — `=`, `(`, `,`,
87
+ // `:`, `[`, `!`, `&`, `|`, `?`, `{`, `;`, `+`, `-`, `*`, `%`, `<`, `>`, `~`,
88
+ // `^` and whitespace at the start of input — leaves expression position.
89
+ return !/[)\]]/.test(token);
36
90
  }
37
91
 
38
92
  /**
@@ -62,7 +116,8 @@ export function stripStringsAndComments(
62
116
  // interpolation the depth counts { } so the template's closing backtick is
63
117
  // not confused with a backtick in nested code.
64
118
  const templates: number[] = [];
65
- let lastSignificant = "\n";
119
+ // The last significant TOKEN, not the last significant character (#905).
120
+ let lastSignificant = "";
66
121
 
67
122
  while (i < src.length) {
68
123
  const c = src[i];
@@ -77,7 +132,7 @@ export function stripStringsAndComments(
77
132
  if (c === "`") {
78
133
  templates.pop();
79
134
  out += "`";
80
- lastSignificant = "`";
135
+ lastSignificant = ")"; // a completed template is a value
81
136
  i++;
82
137
  continue;
83
138
  }
@@ -138,12 +193,12 @@ export function stripStringsAndComments(
138
193
  out += quote;
139
194
  i++;
140
195
  }
141
- lastSignificant = quote;
196
+ lastSignificant = ")"; // a completed string is a value
142
197
  continue;
143
198
  }
144
199
 
145
200
  // Regex literal. Only when a `/` cannot be division here.
146
- if (c === "/" && regexCanFollow(lastSignificant)) {
201
+ if (c === "/" && regexCanFollowToken(lastSignificant)) {
147
202
  let j = i + 1;
148
203
  let inClass = false;
149
204
  let closed = false;
@@ -179,6 +234,19 @@ export function stripStringsAndComments(
179
234
  // Fall through: it was division.
180
235
  }
181
236
 
237
+ // Identifier, keyword or numeric run. Consumed WHOLE so that the
238
+ // regex-vs-division decision above sees a token rather than the character
239
+ // the token happens to end with (#905).
240
+ if (/[A-Za-z0-9_$]/.test(c)) {
241
+ let j = i;
242
+ while (j < src.length && /[A-Za-z0-9_$]/.test(src[j])) j++;
243
+ const word = src.slice(i, j);
244
+ out += word;
245
+ lastSignificant = lastSignificant === "." ? "." + word : word;
246
+ i = j;
247
+ continue;
248
+ }
249
+
182
250
  // Template start.
183
251
  if (c === "`") {
184
252
  templates.push(0);
@@ -232,6 +232,58 @@ export function readJunitCheckNames(path: string): JunitReading {
232
232
  return parseJunitCheckNames(xml);
233
233
  }
234
234
 
235
+ /** What one JUnit report says about which checks PASSED (#659). */
236
+ export interface JunitOutcomeReading {
237
+ /**
238
+ * Full names of the testcases that ran and did not fail. `null` when the
239
+ * report could not be read — a different answer from `[]`, which says the
240
+ * report ran nothing, and the two must stay different all the way to the
241
+ * consumer.
242
+ */
243
+ passed: string[] | null;
244
+ /** Full names of the testcases that ran and failed. Empty when unreadable. */
245
+ failed: string[];
246
+ /** Why `passed` is null. */
247
+ reason?: string;
248
+ }
249
+
250
+ /**
251
+ * Pass and fail, off the SAME scan the names reading uses (#659).
252
+ *
253
+ * `lib/test-case-deletion.ts` needs to prove a deleted case was GREEN before
254
+ * it was deleted, and `parseJunitCheckNames` answers "did it run", which is
255
+ * satisfied by a case that ran and failed. A second scanner for the second
256
+ * question would be two deciders for one document: both would agree on every
257
+ * report anyone has, and a mutation of either would leave the other holding
258
+ * the answer up (`.claude/rules/checks-must-be-able-to-fail.md`, the fourth
259
+ * shape). So the answer comes out of `scanJunitTestcases`, which already
260
+ * records each testcase's `<failure>`.
261
+ *
262
+ * A `<skipped />` testcase is in NEITHER list. It did not run, so it did not
263
+ * pass — and reading a filtered-out case as green is exactly the trap #532
264
+ * records about this report format.
265
+ */
266
+ export function parseJunitOutcomes(xml: string): JunitOutcomeReading {
267
+ const scan = scanJunitTestcases(xml);
268
+ if (scan === null) return { passed: null, failed: [], reason: NOT_A_REPORT };
269
+ const ran = scan.cases.filter(c => c.ran);
270
+ return {
271
+ passed: ran.filter(c => c.failure === null).map(c => c.name),
272
+ failed: ran.filter(c => c.failure !== null).map(c => c.name),
273
+ };
274
+ }
275
+
276
+ /** `parseJunitOutcomes` over a file, with the read failure as a reading. */
277
+ export function readJunitOutcomes(path: string): JunitOutcomeReading {
278
+ let xml: string;
279
+ try {
280
+ xml = readFileSync(path, "utf-8");
281
+ } catch (e: any) {
282
+ return { passed: null, failed: [], reason: `the JUnit report could not be read (${e?.code || e?.name || "unknown"})` };
283
+ }
284
+ return parseJunitOutcomes(xml);
285
+ }
286
+
235
287
  /**
236
288
  * The full witness: every check the gate reported a verdict for.
237
289
  *