crysk 0.0.0-stage → 0.3.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 (229) hide show
  1. package/LICENSE +176 -0
  2. package/README.md +610 -2
  3. package/bin/pr-hero.js +166 -0
  4. package/config/models/anthropic.json +5 -0
  5. package/dist/SKILL-7e3725zn.md +496 -0
  6. package/dist/SKILL-7q35wcaz.md +66 -0
  7. package/dist/SKILL-9at22381.md +74 -0
  8. package/dist/SKILL-ex2hxxh3.md +134 -0
  9. package/dist/adjudicator-dtg2axnh.md +86 -0
  10. package/dist/admission-config.example-a9vqmmad.json +9 -0
  11. package/dist/ci-admission-camfbx2b.md +122 -0
  12. package/dist/cli.js +42292 -0
  13. package/dist/consolidate-ac0mnfyj.md +24 -0
  14. package/dist/deep-review-lifecycle-m698ybjp.md +362 -0
  15. package/dist/deep-review-logic-a6b1gczf.md +156 -0
  16. package/dist/deep-review-parity-0ec904db.md +120 -0
  17. package/dist/deep-review-reliability-qyakbjb9.md +125 -0
  18. package/dist/deep-review-resilience-3tz8y26e.md +118 -0
  19. package/dist/index.js +20190 -0
  20. package/dist/opencode-ci-7b0be64p.md +141 -0
  21. package/dist/review-refuter-j4e9gezy.md +71 -0
  22. package/dist/scout-yvp153q7.md +140 -0
  23. package/dist/summarizer-zca44nxa.md +18 -0
  24. package/dist/workflow-w1g9szna.yml +151 -0
  25. package/package.json +82 -4
  26. package/prompts/consolidate.md +24 -0
  27. package/prompts/default/PROVENANCE.md +18 -0
  28. package/prompts/default/deep-review-lifecycle.md +362 -0
  29. package/prompts/default/deep-review-logic.md +156 -0
  30. package/prompts/default/deep-review-parity.md +120 -0
  31. package/prompts/default/deep-review-reliability.md +125 -0
  32. package/prompts/default/deep-review-resilience.md +118 -0
  33. package/prompts/default/review-refuter.md +71 -0
  34. package/prompts/scout.md +140 -0
  35. package/prompts/summarizer.md +18 -0
  36. package/skills/crysk/SKILL.md +74 -0
  37. package/skills/crysk-ci-setup/SKILL.md +134 -0
  38. package/skills/crysk-ci-setup/assets/admission-config.example.json +9 -0
  39. package/skills/crysk-ci-setup/assets/workflow.yml +151 -0
  40. package/skills/crysk-ci-setup/references/ci-admission.md +122 -0
  41. package/skills/crysk-ci-setup/references/opencode-ci.md +141 -0
  42. package/skills/crysk-loop/SKILL.md +496 -0
  43. package/skills/crysk-loop/adjudicator.md +86 -0
  44. package/skills/crysk-pr/SKILL.md +66 -0
  45. package/src/agent-env.ts +1084 -0
  46. package/src/asset-manifest.ts +83 -0
  47. package/src/assets.d.ts +6 -0
  48. package/src/assets.ts +183 -0
  49. package/src/brand-paths.ts +155 -0
  50. package/src/brand.ts +255 -0
  51. package/src/ci/admission-ledger.ts +350 -0
  52. package/src/ci/classify-error.ts +73 -0
  53. package/src/ci/cli-record.ts +271 -0
  54. package/src/ci/force-comment.ts +39 -0
  55. package/src/ci/gates.ts +923 -0
  56. package/src/ci/outcome.ts +2181 -0
  57. package/src/ci/override.ts +187 -0
  58. package/src/ci/publish.ts +1639 -0
  59. package/src/ci/reporter.ts +626 -0
  60. package/src/ci/review-admission.ts +901 -0
  61. package/src/ci/review-levels.ts +310 -0
  62. package/src/ci/review-policy.ts +18 -0
  63. package/src/ci/review-risk.ts +184 -0
  64. package/src/ci/setup.ts +844 -0
  65. package/src/ci/trigger.ts +1353 -0
  66. package/src/cli.ts +348 -0
  67. package/src/commands/activity.ts +67 -0
  68. package/src/commands/ci-setup-questions.ts +397 -0
  69. package/src/commands/ci-setup.ts +381 -0
  70. package/src/commands/config.ts +99 -0
  71. package/src/commands/doctor.ts +108 -0
  72. package/src/commands/init.ts +139 -0
  73. package/src/commands/mcp.ts +10 -0
  74. package/src/commands/menu.ts +129 -0
  75. package/src/commands/post.ts +302 -0
  76. package/src/commands/skills.ts +83 -0
  77. package/src/commands/status.ts +173 -0
  78. package/src/commands/triage.ts +342 -0
  79. package/src/commands/uninstall.ts +37 -0
  80. package/src/commands/upgrade.ts +214 -0
  81. package/src/commands/usage.ts +98 -0
  82. package/src/compare/compare.ts +28 -0
  83. package/src/compare/floor-test.ts +471 -0
  84. package/src/compare/martian-adapter.ts +80 -0
  85. package/src/config/config.ts +267 -0
  86. package/src/corpus/corpus.ts +1094 -0
  87. package/src/corpus/preflight.ts +1789 -0
  88. package/src/diversity/accounting.ts +90 -0
  89. package/src/diversity/adjudication.ts +145 -0
  90. package/src/diversity/admission.ts +60 -0
  91. package/src/diversity/benchmark.ts +195 -0
  92. package/src/diversity/clustering.ts +285 -0
  93. package/src/diversity/errors.ts +7 -0
  94. package/src/diversity/identity.ts +285 -0
  95. package/src/diversity/index.ts +11 -0
  96. package/src/diversity/pipeline-integration.ts +448 -0
  97. package/src/diversity/projection.ts +57 -0
  98. package/src/diversity/self-contained.ts +68 -0
  99. package/src/diversity/target-validation.ts +179 -0
  100. package/src/doctor-migration.ts +209 -0
  101. package/src/doctor.ts +723 -0
  102. package/src/errors.ts +83 -0
  103. package/src/execution/admission.ts +75 -0
  104. package/src/execution/atomic-write.ts +35 -0
  105. package/src/execution/attempt-evidence.ts +415 -0
  106. package/src/execution/bucket-id.ts +136 -0
  107. package/src/execution/concurrency-limiter.ts +175 -0
  108. package/src/execution/contracts.ts +443 -0
  109. package/src/execution/failure-classification.ts +42 -0
  110. package/src/execution/failure-policy.ts +240 -0
  111. package/src/execution/harness.ts +2548 -0
  112. package/src/execution/settlement.ts +348 -0
  113. package/src/execution/spawned-process.ts +66 -0
  114. package/src/execution/spend-limiter.ts +424 -0
  115. package/src/execution/step-artifacts.ts +55 -0
  116. package/src/execution/usage-normalized.ts +469 -0
  117. package/src/git/git.ts +396 -0
  118. package/src/git/identity.ts +64 -0
  119. package/src/git/refs.ts +140 -0
  120. package/src/home-preflight.ts +443 -0
  121. package/src/home.ts +243 -0
  122. package/src/ignore-file.ts +336 -0
  123. package/src/ignore-read.ts +118 -0
  124. package/src/index.ts +34 -0
  125. package/src/legacy-launchd.ts +29 -0
  126. package/src/mcp/mcp.ts +150 -0
  127. package/src/mcp/preflight.ts +407 -0
  128. package/src/menu-context.ts +192 -0
  129. package/src/model/catalog.ts +251 -0
  130. package/src/model/exec-snapshots.ts +390 -0
  131. package/src/model/free-discovery.ts +302 -0
  132. package/src/model/provider-capabilities.ts +814 -0
  133. package/src/model/routing.ts +600 -0
  134. package/src/pr/admission.ts +847 -0
  135. package/src/pr/ci-admission-gate.ts +665 -0
  136. package/src/pr/ci-publish.ts +76 -0
  137. package/src/pr/discovery.ts +432 -0
  138. package/src/pr/inline.ts +507 -0
  139. package/src/pr/plan.ts +366 -0
  140. package/src/pr/posting.ts +115 -0
  141. package/src/pr/pr-status.ts +787 -0
  142. package/src/pr/pr.ts +3642 -0
  143. package/src/pr/preflight.ts +1031 -0
  144. package/src/pr/publish-outcome.ts +361 -0
  145. package/src/pr/range.ts +119 -0
  146. package/src/pr/reverts-preflight.ts +606 -0
  147. package/src/pr/reverts.ts +377 -0
  148. package/src/pr/review-pr.ts +927 -0
  149. package/src/pr/status.ts +154 -0
  150. package/src/pr/target.ts +388 -0
  151. package/src/pr/teardown.ts +437 -0
  152. package/src/pr/worktree-setup.ts +60 -0
  153. package/src/production-runtime.ts +1877 -0
  154. package/src/progress.ts +487 -0
  155. package/src/rereview/adjudicate.ts +89 -0
  156. package/src/rereview/classify.ts +362 -0
  157. package/src/rereview/identity.ts +211 -0
  158. package/src/rereview/plan.ts +308 -0
  159. package/src/rereview/prepare.ts +1025 -0
  160. package/src/rereview/state.ts +767 -0
  161. package/src/rereview/verify.ts +241 -0
  162. package/src/review/boundary.ts +105 -0
  163. package/src/review/consolidate.ts +246 -0
  164. package/src/review/dedupe.ts +368 -0
  165. package/src/review/drafts.ts +448 -0
  166. package/src/review/findings-conformance.ts +120 -0
  167. package/src/review/findings.ts +714 -0
  168. package/src/review/pipeline.ts +3977 -0
  169. package/src/review/preflight.ts +3575 -0
  170. package/src/review/prompt-set.ts +177 -0
  171. package/src/review/proof-refs.ts +187 -0
  172. package/src/review/report.ts +1821 -0
  173. package/src/review/review.ts +490 -0
  174. package/src/review/root-cause.ts +120 -0
  175. package/src/review/route-preflight.ts +289 -0
  176. package/src/review/run.ts +601 -0
  177. package/src/review/scout.ts +323 -0
  178. package/src/review/size-gate.ts +595 -0
  179. package/src/review/spec.ts +235 -0
  180. package/src/review/step-runner.ts +233 -0
  181. package/src/runner-authority.ts +596 -0
  182. package/src/security/binding-policy.ts +115 -0
  183. package/src/security/credential-broker.ts +773 -0
  184. package/src/security/evidence-redaction.ts +62 -0
  185. package/src/security/execution-authority.ts +27 -0
  186. package/src/security/redact.ts +71 -0
  187. package/src/security/workspace-read-broker.ts +466 -0
  188. package/src/server/client.ts +190 -0
  189. package/src/server/preflight.ts +217 -0
  190. package/src/server/server.ts +327 -0
  191. package/src/signal-handler.ts +50 -0
  192. package/src/store/activity.ts +413 -0
  193. package/src/store/backfill-preflight.ts +49 -0
  194. package/src/store/backfill.ts +141 -0
  195. package/src/store/gc-preflight.ts +206 -0
  196. package/src/store/gc.ts +424 -0
  197. package/src/store/metrics-preflight.ts +229 -0
  198. package/src/store/metrics.ts +236 -0
  199. package/src/store/preflight.ts +748 -0
  200. package/src/store/store.ts +693 -0
  201. package/src/system-tools.ts +355 -0
  202. package/src/transport-registry.ts +1124 -0
  203. package/src/transports/claude-code-cli.ts +1348 -0
  204. package/src/transports/opencode-admission.ts +515 -0
  205. package/src/transports/opencode-client.ts +3328 -0
  206. package/src/transports/opencode-evidence.ts +557 -0
  207. package/src/transports/opencode-mcp.ts +248 -0
  208. package/src/transports/opencode-sdk.ts +2294 -0
  209. package/src/transports/opencode-server.ts +535 -0
  210. package/src/triage/adjudication.ts +113 -0
  211. package/src/triage/reply.ts +125 -0
  212. package/src/triage/triage.ts +255 -0
  213. package/src/ui/activity.ts +173 -0
  214. package/src/ui/config-edit.ts +816 -0
  215. package/src/ui/config.ts +462 -0
  216. package/src/ui/menu.ts +1325 -0
  217. package/src/ui/plan.ts +1039 -0
  218. package/src/ui/primitives.ts +300 -0
  219. package/src/ui/progress.ts +298 -0
  220. package/src/ui/result.ts +552 -0
  221. package/src/ui/review-menu.ts +312 -0
  222. package/src/ui/select.ts +528 -0
  223. package/src/ui/tree.ts +178 -0
  224. package/src/uninstaller.ts +479 -0
  225. package/src/updater.ts +836 -0
  226. package/src/usage.ts +77 -0
  227. package/src/watch/preflight.ts +1389 -0
  228. package/src/watch/watch.ts +1235 -0
  229. package/src/wizard.ts +877 -0
@@ -0,0 +1,1094 @@
1
+ // Known-bad corpus mining I/O (GitHub #43). Walks the repository's merged
2
+ // PRs through `gh api graphql`, diffs and blames through git, and writes a
3
+ // markdown artifact of CANDIDATES. Every decision — what counts as a fix
4
+ // subject, how blame picks an introducer, the proximity window, how the
5
+ // artifact renders — is pure in corpus/preflight.ts, where the tests live.
6
+ //
7
+ // Same scope discipline as reverts: never runs a review, never scores, never
8
+ // labels what the defect was, never spends a cent. Read-only git + gh.
9
+ //
10
+ // Same git-runner rule as the other shells: args as an ARRAY, never an
11
+ // interpolated shell string.
12
+
13
+ import path from "node:path";
14
+ import { git, resolveRepoRoot } from "#git/git";
15
+ import { parseRemoteHead, repoWebUrlFromRemote } from "#git/refs";
16
+ import {
17
+ type CommitPullRef,
18
+ DEFAULT_REVERTS_SINCE,
19
+ GIT_LOG_FIELD_SEP,
20
+ type PullDetails,
21
+ parseCommitPulls,
22
+ parsePullDetails,
23
+ pickCommitPull,
24
+ repoSlugFromWebUrl,
25
+ } from "#pr/reverts-preflight";
26
+ import type { CliOptions } from "#review/preflight";
27
+ import { log } from "#ui/primitives";
28
+ import { CliError, CliUsageError } from "../errors";
29
+ import {
30
+ blameArgv,
31
+ buildThreadBatchQuery,
32
+ type CommitIndexEntry,
33
+ type CommitPrRef,
34
+ type CorpusLookupFailures,
35
+ type CorpusSource,
36
+ type CorpusWorking,
37
+ evidenceExcerpt,
38
+ type IntroducerInfo,
39
+ type IssueLabels,
40
+ isFixSubject,
41
+ isIncidentText,
42
+ isSelfIntroducer,
43
+ issueRefsFromBody,
44
+ joinProximity,
45
+ MAX_BLAMED_FILES,
46
+ MAX_MERGED_RANGES,
47
+ type MergedPrNode,
48
+ type MergedPrPage,
49
+ matchBugLabels,
50
+ type ProximityFix,
51
+ type PullCommitRef,
52
+ parentBelongsToFix,
53
+ parseBlamePorcelain,
54
+ parseCommitDates,
55
+ parseCommitIndex,
56
+ parseCommitParents,
57
+ parseCutoffTimestamp,
58
+ parseDiffHunks,
59
+ parseIssueLabels,
60
+ parseMergedPrPage,
61
+ parsePullCommits,
62
+ parsePullFiles,
63
+ parseThreadBatch,
64
+ pickIntroducer,
65
+ qualifyThreads,
66
+ renderCorpusArtifact,
67
+ resolvedThreadsWithPath,
68
+ selectCorpus,
69
+ splitBugLabels,
70
+ THREAD_BATCH_SIZE,
71
+ THREAD_PAGE_SIZE,
72
+ type ThreadCandidate,
73
+ validateProximityDays,
74
+ walkPageKept,
75
+ } from "./preflight";
76
+
77
+ // Same shape as pr/reverts.ts's gh. gh stays behind the same single seam
78
+ // (`spawnFn`) so an offline fake spawn never depends on the GitHub CLI being
79
+ // installed on the machine running the test.
80
+ async function gh(
81
+ operatorRoot: string,
82
+ args: string[],
83
+ spawnFn?: typeof Bun.spawn,
84
+ ): Promise<{ ok: boolean; stdout: string; stderr: string }> {
85
+ const spawn = spawnFn ?? Bun.spawn;
86
+ // A missing gh must name itself: Bun.spawn's error for a binary that is
87
+ // not there reads like a crash, not like "install the GitHub CLI".
88
+ if (spawnFn === undefined && Bun.which("gh") === null) {
89
+ throw new CliError(
90
+ "gh not found on PATH — corpus resolves PR and issue data through the " +
91
+ "GitHub CLI. Install it and authenticate (gh auth login) first.",
92
+ );
93
+ }
94
+ // cwd = operator root: gh resolves credentials and host from the checkout,
95
+ // so the API consulted belongs to the repo passed as --repo.
96
+ // Retry-by-default on failure, and the runs that taught the rule: a
97
+ // ~25-minute musive scan died on `dial tcp: network is unreachable`, the
98
+ // retry-fixed run died on `error connecting to api.github.com` (a different
99
+ // transient signature the allowlist missed), and the run after THAT died on
100
+ // gh's own `unexpected end of JSON input` (a truncated 200). Enumerating
101
+ // transient signatures is whack-a-mole, so the list inverted: everything
102
+ // retries EXCEPT what retrying cannot fix — a 404 is a fact about one PR
103
+ // (callers degrade it), 401/403/bad-credentials are auth, and a rate limit
104
+ // only burns faster when hammered.
105
+ const NOT_TRANSIENT =
106
+ /HTTP 40[134]|rate limit|bad credentials|HTTP 404|was previously flagged/i;
107
+ let attempt = 0;
108
+ for (;;) {
109
+ const proc = spawn(["gh", ...args], {
110
+ cwd: operatorRoot,
111
+ stdout: "pipe",
112
+ stderr: "pipe",
113
+ });
114
+ const [stdout, stderr, exitCode] = await Promise.all([
115
+ new Response(proc.stdout).text(),
116
+ new Response(proc.stderr).text(),
117
+ proc.exited,
118
+ ]);
119
+ if (exitCode === 0 || attempt >= 3 || NOT_TRANSIENT.test(stderr)) {
120
+ return { ok: exitCode === 0, stdout, stderr };
121
+ }
122
+ attempt++;
123
+ // 1s, 3s, 8s: exponential-ish, sized for a wifi blip that outlives the
124
+ // first retry (observed: two resets inside ten seconds).
125
+ const backoffMs = attempt === 1 ? 1000 : attempt === 2 ? 3000 : 8000;
126
+ log(
127
+ `corpus: transient gh failure (attempt ${attempt}/3), retrying in ` +
128
+ `${backoffMs}ms: ${stderr.trim().split("\n")[0] ?? "no detail"}`,
129
+ );
130
+ await new Promise((resolve) => setTimeout(resolve, backoffMs));
131
+ }
132
+ }
133
+
134
+ // The candidates tried, in order, when origin/HEAD is unset. Named in the
135
+ // error so a failure is one command from being fixed rather than a guess.
136
+ const FALLBACK_DEFAULT_BRANCHES = ["origin/main", "origin/master"];
137
+
138
+ // WHY this is not cosmetic: musive's default branch is `dev`, so a hardcoded
139
+ // `main` walks PRs that never shipped and misses the ones that did — a wrong
140
+ // answer with a completely plausible face. Ported from pr/reverts.ts for the
141
+ // same reason: only the default branch's merged PRs reached users.
142
+ async function resolveDefaultBranchRef(repoRoot: string): Promise<string> {
143
+ const symbolic = await git(repoRoot, [
144
+ "symbolic-ref",
145
+ "--quiet",
146
+ "refs/remotes/origin/HEAD",
147
+ ]);
148
+ if (symbolic.ok) {
149
+ const branch = parseRemoteHead(symbolic.stdout);
150
+ if (branch !== undefined) return `origin/${branch}`;
151
+ }
152
+ for (const candidate of FALLBACK_DEFAULT_BRANCHES) {
153
+ const verified = await git(repoRoot, [
154
+ "rev-parse",
155
+ "--verify",
156
+ "--quiet",
157
+ "--end-of-options",
158
+ `${candidate}^{commit}`,
159
+ ]);
160
+ if (verified.ok) return candidate;
161
+ }
162
+ throw new CliError(
163
+ `cannot resolve the default branch of ${repoRoot}: ` +
164
+ "refs/remotes/origin/HEAD is unset and neither " +
165
+ `${FALLBACK_DEFAULT_BRANCHES.join(" nor ")} exists. Run \`git remote ` +
166
+ "set-head origin --auto` (or fetch the remote) first.",
167
+ );
168
+ }
169
+
170
+ async function resolveRepoSlug(repoRoot: string): Promise<string> {
171
+ const remote = await git(repoRoot, ["remote", "get-url", "origin"]);
172
+ if (!remote.ok) {
173
+ throw new CliError(
174
+ `cannot read origin's url in ${repoRoot}: ${remote.stderr.trim()}`,
175
+ );
176
+ }
177
+ const webUrl = repoWebUrlFromRemote(remote.stdout);
178
+ const slug = webUrl === undefined ? null : repoSlugFromWebUrl(webUrl);
179
+ if (slug === null) {
180
+ throw new CliError(
181
+ `origin of ${repoRoot} does not look like a github.com repository ` +
182
+ `(${remote.stdout.trim()}) — corpus resolves PR and issue data ` +
183
+ "through the GitHub API, so it needs one",
184
+ );
185
+ }
186
+ return slug;
187
+ }
188
+
189
+ // A 404 is a fact about ONE object, not a broken command: a deleted issue or
190
+ // a PR from a deleted fork. It degrades that one lookup and the scan
191
+ // continues. Anything else — auth, rate limit, network — is systematic, and
192
+ // continuing would produce an artifact quietly missing most of its entries.
193
+ function isNotFound(stderr: string): boolean {
194
+ return /HTTP 404/.test(stderr);
195
+ }
196
+
197
+ // Fake-gh tests never send the document to GitHub, so they cannot catch a
198
+ // truncated query. Ported from pr/pr.ts (its W1 failure on #34): count the
199
+ // braces before spawn.
200
+ function assertBalancedGraphql(document: string, what: string): void {
201
+ const opens = (document.match(/{/g) ?? []).length;
202
+ const closes = (document.match(/}/g) ?? []).length;
203
+ if (opens !== closes) {
204
+ throw new CliError(
205
+ `gh api graphql (${what}) query is unbalanced: ${opens} { vs ${closes} }`,
206
+ );
207
+ }
208
+ }
209
+
210
+ // A 404 and a 200 listing zero PRs are DIFFERENT answers and this result type
211
+ // exists so a caller cannot confuse them: `{found:false}` is "GitHub did not
212
+ // answer for this commit", `{found:true, pulls:[]}` is "GitHub answered, and
213
+ // this commit belongs to no PR" — a direct push, which is real evidence. They
214
+ // used to share the `[]` return, so a run degraded by 404s produced an artifact
215
+ // byte-indistinguishable from a complete one (measured 2026-08-17: 12
216
+ // blame-linked candidates where a clean re-run found 428).
217
+ type CommitPullsLookup =
218
+ | { found: true; pulls: CommitPullRef[] }
219
+ | { found: false };
220
+
221
+ // Exported for test/corpus/corpus.test.ts, which pins the two outcomes apart; the
222
+ // `spawnFn` seam is pr/reverts.ts's and pr/pr.ts's, invisible to production callers.
223
+ export async function ghCommitPulls(
224
+ operatorRoot: string,
225
+ slug: string,
226
+ sha: string,
227
+ options?: { spawnFn?: typeof Bun.spawn },
228
+ ): Promise<CommitPullsLookup> {
229
+ const result = await gh(
230
+ operatorRoot,
231
+ ["api", `repos/${slug}/commits/${sha}/pulls`],
232
+ options?.spawnFn,
233
+ );
234
+ if (!result.ok) {
235
+ if (isNotFound(result.stderr)) return { found: false };
236
+ // Everything that is not a 404 still throws: fail-loud is correct for auth,
237
+ // rate limits and network, and must never become one more counter.
238
+ throw new CliError(
239
+ `gh api repos/${slug}/commits/${sha}/pulls failed: ` +
240
+ result.stderr.trim(),
241
+ );
242
+ }
243
+ try {
244
+ return { found: true, pulls: parseCommitPulls(result.stdout) };
245
+ } catch (error) {
246
+ // The pure reader names the field; only the shell knows the endpoint.
247
+ if (error instanceof CliUsageError) {
248
+ throw new CliError(
249
+ `repos/${slug}/commits/${sha}/pulls: ${error.message}`,
250
+ );
251
+ }
252
+ throw error;
253
+ }
254
+ }
255
+
256
+ async function ghPullDetails(
257
+ operatorRoot: string,
258
+ slug: string,
259
+ pr: number,
260
+ ): Promise<PullDetails | null> {
261
+ const result = await gh(operatorRoot, ["api", `repos/${slug}/pulls/${pr}`]);
262
+ if (!result.ok) {
263
+ if (isNotFound(result.stderr)) return null;
264
+ throw new CliError(
265
+ `gh api repos/${slug}/pulls/${pr} failed: ${result.stderr.trim()}`,
266
+ );
267
+ }
268
+ try {
269
+ return parsePullDetails(result.stdout);
270
+ } catch (error) {
271
+ if (error instanceof CliUsageError) {
272
+ throw new CliError(`repos/${slug}/pulls/${pr}: ${error.message}`);
273
+ }
274
+ throw error;
275
+ }
276
+ }
277
+
278
+ async function ghPullCommits(
279
+ repoRoot: string,
280
+ slug: string,
281
+ pr: number,
282
+ ): Promise<PullCommitRef[]> {
283
+ const result = await gh(repoRoot, [
284
+ "api",
285
+ "--paginate",
286
+ `repos/${slug}/pulls/${pr}/commits?per_page=100`,
287
+ ]);
288
+ if (!result.ok) {
289
+ if (isNotFound(result.stderr)) return [];
290
+ throw new CliError(
291
+ `gh api repos/${slug}/pulls/${pr}/commits failed: ` +
292
+ result.stderr.trim(),
293
+ );
294
+ }
295
+ try {
296
+ return parsePullCommits(result.stdout);
297
+ } catch (error) {
298
+ if (error instanceof CliUsageError) {
299
+ throw new CliError(`repos/${slug}/pulls/${pr}/commits: ${error.message}`);
300
+ }
301
+ throw error;
302
+ }
303
+ }
304
+
305
+ async function ghPullFiles(
306
+ repoRoot: string,
307
+ slug: string,
308
+ pr: number,
309
+ ): Promise<string[]> {
310
+ const result = await gh(repoRoot, [
311
+ "api",
312
+ "--paginate",
313
+ `repos/${slug}/pulls/${pr}/files?per_page=100`,
314
+ ]);
315
+ if (!result.ok) {
316
+ if (isNotFound(result.stderr)) return [];
317
+ throw new CliError(
318
+ `gh api repos/${slug}/pulls/${pr}/files failed: ${result.stderr.trim()}`,
319
+ );
320
+ }
321
+ try {
322
+ return parsePullFiles(result.stdout);
323
+ } catch (error) {
324
+ if (error instanceof CliUsageError) {
325
+ throw new CliError(`repos/${slug}/pulls/${pr}/files: ${error.message}`);
326
+ }
327
+ throw error;
328
+ }
329
+ }
330
+
331
+ // The merged-PR walk (STEP A). Everything travels as graphql VARIABLES —
332
+ // owner, name and cursor — so no user- or API-derived string is ever
333
+ // interpolated into the document. Ordered by UPDATED_AT, not mergedAt:
334
+ // IssueOrderField has no MERGED_AT value (the first live run failed closed on
335
+ // exactly that), and updatedAt >= mergedAt keeps the walk complete — the
336
+ // completeness argument lives with walkPageKept.
337
+ const WALK_QUERY =
338
+ "query($repoOwner:String!,$repoName:String!,$cursor:String){" +
339
+ "repository(owner:$repoOwner,name:$repoName){" +
340
+ "pullRequests(first:100,states:MERGED," +
341
+ "orderBy:{field:UPDATED_AT,direction:DESC},after:$cursor){" +
342
+ "pageInfo{endCursor hasNextPage}" +
343
+ "nodes{number title body mergedAt updatedAt mergeCommit{oid} baseRefName}}}}";
344
+
345
+ interface WalkResult {
346
+ kept: MergedPrNode[];
347
+ pagesSeen: number;
348
+ }
349
+
350
+ async function walkMergedPrs(input: {
351
+ repoRoot: string;
352
+ slug: string;
353
+ cutoffMs: number | null;
354
+ defaultBranch: string;
355
+ }): Promise<WalkResult> {
356
+ assertBalancedGraphql(WALK_QUERY, "merged PR walk");
357
+ const slash = input.slug.indexOf("/");
358
+ const owner = input.slug.slice(0, slash);
359
+ const name = input.slug.slice(slash + 1);
360
+ const kept: MergedPrNode[] = [];
361
+ let cursor: string | null = null;
362
+ let hasNextPage = true;
363
+ let pagesSeen = 0;
364
+ // Pages are walked SEQUENTIALLY: each page's cursor comes from the last,
365
+ // and a parallel walk would destroy the DESC ordering the stop rule (a
366
+ // fully-older page means every later page is older still) depends on.
367
+ while (hasNextPage) {
368
+ const args = [
369
+ "api",
370
+ "graphql",
371
+ "-f",
372
+ `query=${WALK_QUERY}`,
373
+ "-f",
374
+ `repoOwner=${owner}`,
375
+ "-f",
376
+ `repoName=${name}`,
377
+ ];
378
+ if (cursor !== null) {
379
+ args.push("-f", `cursor=${cursor}`);
380
+ }
381
+ const result = await gh(input.repoRoot, args);
382
+ if (!result.ok) {
383
+ throw new CliError(
384
+ `gh api graphql (merged PR walk) failed: ${result.stderr.trim()}`,
385
+ );
386
+ }
387
+ let page: MergedPrPage;
388
+ try {
389
+ page = parseMergedPrPage(result.stdout);
390
+ } catch (error) {
391
+ if (error instanceof CliUsageError) {
392
+ throw new CliError(`gh api graphql: ${error.message}`);
393
+ }
394
+ throw error;
395
+ }
396
+ pagesSeen++;
397
+ const filtered = walkPageKept({
398
+ nodes: page.nodes,
399
+ cutoffMs: input.cutoffMs,
400
+ defaultBranch: input.defaultBranch,
401
+ });
402
+ kept.push(...filtered.kept);
403
+ if (filtered.olderExhausted) break;
404
+ cursor = page.endCursor;
405
+ hasNextPage = page.hasNextPage;
406
+ }
407
+ return { kept, pagesSeen };
408
+ }
409
+
410
+ // The cutoff instant, resolved INSIDE git (STEP A's agreement rule): a
411
+ // relative --since ("24 months ago") is relative to a clock this command
412
+ // refuses to read, so git resolves it — `rev-list` names the newest commit
413
+ // at/before the boundary and we take its committer seconds. Empty output
414
+ // means the whole history fits the window: no cutoff, walk everything.
415
+ async function resolveCutoffSec(
416
+ repoRoot: string,
417
+ ref: string,
418
+ since: string,
419
+ ): Promise<number | null> {
420
+ const listed = await git(repoRoot, [
421
+ "rev-list",
422
+ "--max-count=1",
423
+ "--format=%ct",
424
+ `--before=${since}`,
425
+ "--end-of-options",
426
+ ref,
427
+ ]);
428
+ if (!listed.ok) {
429
+ throw new CliError(
430
+ `git rev-list --before=${since} ${ref} failed: ${listed.stderr.trim()}`,
431
+ );
432
+ }
433
+ try {
434
+ return parseCutoffTimestamp(listed.stdout);
435
+ } catch (error) {
436
+ if (error instanceof CliUsageError) {
437
+ throw new CliError(`git rev-list ${ref}: ${error.message}`);
438
+ }
439
+ throw error;
440
+ }
441
+ }
442
+
443
+ // STEP C's blame pass for one classified PR. The ranges come from the fix's
444
+ // own diff pre-image (the lines the fix changed are where the bug lived);
445
+ // each range is one `git blame --porcelain` against the merge's PARENT —
446
+ // the last tree in which the bug was still alive.
447
+ async function blameResolve(input: {
448
+ repoRoot: string;
449
+ slug: string;
450
+ pr: MergedPrNode;
451
+ entry: CorpusWorking;
452
+ // Run-level, mutated in place: every degradation this pass tolerates has to
453
+ // reach the artifact's header, or the run reads as a complete one.
454
+ failures: CorpusLookupFailures;
455
+ }): Promise<string[]> {
456
+ const mergeSha = input.pr.mergeCommitSha;
457
+ if (mergeSha === null) return [];
458
+ const parent = await git(input.repoRoot, ["rev-parse", `${mergeSha}^`]);
459
+ if (!parent.ok) {
460
+ // GitHub keeps merge commits that this clone no longer (or never) had —
461
+ // observed live on musive PR #423: dev's history was rewritten after the
462
+ // merge, the old merge commit survives on GitHub only through the PR
463
+ // ref, and the local object store pruned it. That is clone-vs-GitHub
464
+ // divergence, not corruption: this PR keeps its detector evidence with
465
+ // no introducer (the tier falls accordingly), said so on stderr, and the
466
+ // scan continues.
467
+ input.failures.mergeCommitAbsent++;
468
+ log(
469
+ `corpus: PR #${input.pr.number} — merge commit ${mergeSha.slice(0, 12)} ` +
470
+ "is not present in this clone (stale clone or rewritten history); " +
471
+ "no blame evidence, continuing",
472
+ );
473
+ return [];
474
+ }
475
+ const parentSha = parent.stdout.trim();
476
+ const listed = await git(input.repoRoot, [
477
+ "rev-list",
478
+ "--parents",
479
+ "-n",
480
+ "1",
481
+ "--end-of-options",
482
+ mergeSha,
483
+ ]);
484
+ if (!listed.ok) {
485
+ throw new CliError(
486
+ `git rev-list --parents ${mergeSha} failed: ${listed.stderr.trim()}`,
487
+ );
488
+ }
489
+ let parents: string[];
490
+ try {
491
+ parents = parseCommitParents(listed.stdout);
492
+ } catch (error) {
493
+ if (error instanceof CliUsageError) {
494
+ throw new CliError(
495
+ `git rev-list --parents ${mergeSha}: ${error.message}`,
496
+ );
497
+ }
498
+ throw error;
499
+ }
500
+ // One parent: squash OR rebase. Squash's parent is the previous
501
+ // default-branch tip (another PR, or a direct push). Rebase-and-merge
502
+ // of 2+ commits: the parent is still THIS PR, so mergeSha^..mergeSha
503
+ // is one commit of the PR instead of the whole thing — skip, same as
504
+ // a missing merge commit. A 1-commit rebase looks like squash and the
505
+ // one-commit diff IS the whole PR, so blame proceeds.
506
+ const soleParent = parents[0];
507
+ if (parents.length === 1 && soleParent !== undefined) {
508
+ const parentPulls = await ghCommitPulls(
509
+ input.repoRoot,
510
+ input.slug,
511
+ soleParent,
512
+ );
513
+ // A 404 here cannot tell a rebase from a squash, so the pass continues as
514
+ // before (blame runs) — but the run is no longer clean and says so.
515
+ if (!parentPulls.found) input.failures.commitPrLookup404++;
516
+ if (
517
+ parentPulls.found &&
518
+ parentBelongsToFix(input.pr.number, parentPulls.pulls)
519
+ ) {
520
+ log(
521
+ `corpus: PR #${input.pr.number} — merge commit ${mergeSha.slice(0, 12)} ` +
522
+ "is a rebase (its parent still belongs to this PR); blame would " +
523
+ "cover one commit instead of the whole PR, skipping",
524
+ );
525
+ return [];
526
+ }
527
+ }
528
+ const diff = await git(input.repoRoot, [
529
+ "diff",
530
+ "--no-color",
531
+ "--unified=0",
532
+ "--end-of-options",
533
+ parentSha,
534
+ mergeSha,
535
+ ]);
536
+ if (!diff.ok) {
537
+ throw new CliError(
538
+ `git diff ${parentSha} ${mergeSha} failed: ${diff.stderr.trim()}`,
539
+ );
540
+ }
541
+ const plan = parseDiffHunks(diff.stdout);
542
+ input.entry.blameSkippedRenames = plan.renamedPaths.length;
543
+ if (plan.droppedFiles > 0 || plan.droppedRanges > 0) {
544
+ log(
545
+ `corpus: PR #${input.pr.number} — blame plan capped at ` +
546
+ `${MAX_BLAMED_FILES} files / ${MAX_MERGED_RANGES} ranges per file ` +
547
+ `(${plan.droppedFiles} file(s), ${plan.droppedRanges} range(s) dropped)`,
548
+ );
549
+ }
550
+ const files = plan.files.map((file) => file.path);
551
+ const blamed: { sha: string; file: string; range: string }[] = [];
552
+ for (const file of plan.files) {
553
+ for (const range of file.ranges) {
554
+ const blame = await git(
555
+ input.repoRoot,
556
+ blameArgv(parentSha, file.path, range),
557
+ );
558
+ if (!blame.ok) {
559
+ input.failures.blameRangeSkipped++;
560
+ // One undatable file must not kill a whole-repo scan: the range is
561
+ // skipped, said so on stderr, and the candidate keeps its other
562
+ // evidence. The blame evidence is simply missing from the entry, so
563
+ // the artifact's counts stay honest.
564
+ log(
565
+ `corpus: PR #${input.pr.number} — blame failed on ${file.path} ` +
566
+ `(${blame.stderr.trim().split("\n")[0] ?? "no detail"}); skipped`,
567
+ );
568
+ continue;
569
+ }
570
+ for (const sha of parseBlamePorcelain(blame.stdout)) {
571
+ blamed.push({
572
+ sha,
573
+ file: file.path,
574
+ range: `${range.start},${range.end}`,
575
+ });
576
+ }
577
+ }
578
+ }
579
+ if (blamed.length > 0) {
580
+ // One batch per PR: the dates exist to pick ONE introducer, and a spawn
581
+ // per sha would bill the latency this command is trying not to have.
582
+ const distinctShas = [...new Set(blamed.map((entry) => entry.sha))];
583
+ const shown = await git(input.repoRoot, [
584
+ "show",
585
+ "-s",
586
+ `--format=%H${GIT_LOG_FIELD_SEP}%ct`,
587
+ ...distinctShas,
588
+ ]);
589
+ if (!shown.ok) {
590
+ throw new CliError(
591
+ `git show -s (${distinctShas.length} sha(s)) failed: ` +
592
+ shown.stderr.trim(),
593
+ );
594
+ }
595
+ const dates = parseCommitDates(shown.stdout);
596
+ const withDates = blamed.flatMap((entry) => {
597
+ const committedAtSec = dates.get(entry.sha);
598
+ return committedAtSec === undefined ? [] : [{ ...entry, committedAtSec }];
599
+ });
600
+ if (withDates.length < blamed.length) {
601
+ log(
602
+ `corpus: PR #${input.pr.number} — ` +
603
+ `${blamed.length - withDates.length} blamed sha(s) had no date row; ` +
604
+ "dropped",
605
+ );
606
+ }
607
+ const pick = pickIntroducer(withDates);
608
+ if (pick !== null) {
609
+ const lookup = await ghCommitPulls(input.repoRoot, input.slug, pick.sha);
610
+ // THE defect this counter exists for: without it, a 404 lands in the
611
+ // entry as `introducer.pr = null`, which the tier ladder reads as a
612
+ // direct push and demotes the candidate for. The entry still records
613
+ // exactly what it recorded before — only the run stops claiming to be
614
+ // clean.
615
+ if (!lookup.found) input.failures.commitPrLookup404++;
616
+ const primary = lookup.found ? pickCommitPull(lookup.pulls) : null;
617
+ if (
618
+ primary !== null &&
619
+ isSelfIntroducer(input.pr.number, primary.number)
620
+ ) {
621
+ log(
622
+ `corpus: PR #${input.pr.number} — blame resolved to the fix itself; ` +
623
+ "introducer dropped (a PR cannot introduce what it fixed)",
624
+ );
625
+ } else {
626
+ const introducer: IntroducerInfo = {
627
+ pr: primary?.number ?? null,
628
+ title: primary?.title ?? null,
629
+ mergedAt: primary?.mergedAt ?? null,
630
+ blamedSha: pick.sha,
631
+ blamedFile: pick.file,
632
+ blamedRange: pick.range,
633
+ };
634
+ input.entry.introducer = introducer;
635
+ input.entry.alsoBlamedCount = pick.alsoBlamedCount;
636
+ log(
637
+ `corpus: PR #${input.pr.number} — introducer ` +
638
+ (primary === null
639
+ ? `direct push ${pick.sha.slice(0, 12)}`
640
+ : `PR #${primary.number}`),
641
+ );
642
+ }
643
+ }
644
+ }
645
+ return files;
646
+ }
647
+
648
+ // STEP F — review threads. One graphql document per THREAD_BATCH_SIZE PRs
649
+ // (aliases p0..pN over validated integers — the only text interpolation in
650
+ // any query here), then per survivor the two REST reads the pure join needs.
651
+ // A gh failure names the rate-limit exit: a narrower --since is the one knob
652
+ // that shrinks this walk.
653
+ async function mineThreads(input: {
654
+ repoRoot: string;
655
+ slug: string;
656
+ mergedPrs: MergedPrNode[];
657
+ }): Promise<ThreadCandidate[]> {
658
+ const slash = input.slug.indexOf("/");
659
+ const owner = input.slug.slice(0, slash);
660
+ const name = input.slug.slice(slash + 1);
661
+ const out: ThreadCandidate[] = [];
662
+ for (
663
+ let offset = 0;
664
+ offset < input.mergedPrs.length;
665
+ offset += THREAD_BATCH_SIZE
666
+ ) {
667
+ const batch = input.mergedPrs.slice(offset, offset + THREAD_BATCH_SIZE);
668
+ const numbers = batch.map((pr) => pr.number);
669
+ const query = buildThreadBatchQuery(numbers);
670
+ assertBalancedGraphql(query, "reviewThreads batch");
671
+ const result = await gh(input.repoRoot, [
672
+ "api",
673
+ "graphql",
674
+ "-f",
675
+ `query=${query}`,
676
+ "-f",
677
+ `repoOwner=${owner}`,
678
+ "-f",
679
+ `repoName=${name}`,
680
+ ]);
681
+ if (!result.ok) {
682
+ throw new CliError(
683
+ `gh api graphql (reviewThreads batch) failed: ${result.stderr.trim()}` +
684
+ " — if this is a rate limit, re-run with a narrower --since",
685
+ );
686
+ }
687
+ let parsed: ReturnType<typeof parseThreadBatch>;
688
+ try {
689
+ parsed = parseThreadBatch(result.stdout, numbers);
690
+ } catch (error) {
691
+ if (error instanceof CliUsageError) {
692
+ throw new CliError(`gh api graphql: ${error.message}`);
693
+ }
694
+ throw error;
695
+ }
696
+ if (parsed.nullAliases > 0) {
697
+ // Visible, not fatal: a null alias is one PR's threads unread (partial
698
+ // GraphQL execution under load, or a PR invisible to this token). A
699
+ // systemic outbreak shows as a count worth re-running, a single null
700
+ // must not kill the scan.
701
+ log(
702
+ `corpus: reviewThreads batch returned ${parsed.nullAliases} null ` +
703
+ `alias(es) (batch ${Math.floor(offset / THREAD_BATCH_SIZE) + 1}) — ` +
704
+ "those PRs contribute no threads",
705
+ );
706
+ }
707
+ for (const [i, pr] of batch.entries()) {
708
+ const entry = parsed.entries[i];
709
+ if (entry === undefined) continue;
710
+ const resolved = resolvedThreadsWithPath(entry.threads);
711
+ if (resolved.length === 0) continue;
712
+ const commits = await ghPullCommits(
713
+ input.repoRoot,
714
+ input.slug,
715
+ pr.number,
716
+ );
717
+ const files = await ghPullFiles(input.repoRoot, input.slug, pr.number);
718
+ const qualified = qualifyThreads(
719
+ resolved.map((thread) => ({
720
+ path: thread.path ?? "",
721
+ line: thread.line,
722
+ firstCommentAt: thread.firstCommentAt ?? "",
723
+ excerpt: thread.excerpt ?? "",
724
+ })),
725
+ commits,
726
+ files,
727
+ );
728
+ // A PR with zero QUALIFYING threads is not a candidate: a resolved
729
+ // thread no push ever addressed is a conversation, not a caught defect.
730
+ if (qualified.length === 0) continue;
731
+ out.push({
732
+ pr: pr.number,
733
+ title: pr.title,
734
+ mergedAt: pr.mergedAt,
735
+ threads: qualified,
736
+ threadsTruncated: entry.truncated,
737
+ baseSha: null,
738
+ headSha: null,
739
+ additions: null,
740
+ deletions: null,
741
+ changedFiles: null,
742
+ });
743
+ log(
744
+ `corpus: PR #${pr.number} — ${qualified.length} qualifying resolved ` +
745
+ "thread(s) (caught in review)",
746
+ );
747
+ }
748
+ }
749
+ return out;
750
+ }
751
+
752
+ export async function corpusCommand(options: CliOptions): Promise<number> {
753
+ const repoRoot = await resolveRepoRoot(options.repo);
754
+ const slug = await resolveRepoSlug(repoRoot);
755
+ const ref = await resolveDefaultBranchRef(repoRoot);
756
+ const since = options.since ?? DEFAULT_REVERTS_SINCE;
757
+ const proximityDays = validateProximityDays(options.proximityDays);
758
+ const bugLabels = options.issues
759
+ ? new Set(splitBugLabels(options.bugLabels))
760
+ : new Set<string>();
761
+ // baseRefName is the SHORT branch name; ref is the remote-tracking path.
762
+ const defaultBranch = ref.replace(/^origin\//, "");
763
+ const sourcesRun = [
764
+ ...(options.fixes ? ["--fixes"] : []),
765
+ ...(options.incidents ? ["--incidents"] : []),
766
+ ...(options.issues ? ["--issues"] : []),
767
+ ...(options.proximity ? ["--proximity"] : []),
768
+ ...(options.threads ? ["--threads"] : []),
769
+ ];
770
+ log(
771
+ `corpus: scanning ${slug} ${ref} since "${since}" (sources: ${sourcesRun.join(", ")}; ` +
772
+ "read-only, $0)",
773
+ );
774
+
775
+ const cutoffSec = await resolveCutoffSec(repoRoot, ref, since);
776
+ const cutoffMs = cutoffSec === null ? null : cutoffSec * 1000;
777
+ log(
778
+ cutoffMs === null
779
+ ? "corpus: whole history is inside the window — walking all merged PRs"
780
+ : `corpus: window cutoff ${new Date(cutoffMs).toISOString()} (resolved by git)`,
781
+ );
782
+
783
+ // STEP A — the walk. Every source consumes this list.
784
+ const walked = await walkMergedPrs({
785
+ repoRoot,
786
+ slug,
787
+ cutoffMs,
788
+ defaultBranch,
789
+ });
790
+ const mergedPrs = walked.kept;
791
+ log(
792
+ `corpus: ${mergedPrs.length} merged PR(s) on ${defaultBranch} in the ` +
793
+ `window (${walked.pagesSeen} page(s))`,
794
+ );
795
+
796
+ // STEP C — classify (fix subjects / incident keywords) + blame-resolve.
797
+ // Issue refs alone do NOT enter the set: they only upgrade confidence in
798
+ // STEP D's label check.
799
+ const working = new Map<number, CorpusWorking>();
800
+ const blameFilesByPr = new Map<number, string[]>();
801
+ const failures: CorpusLookupFailures = {
802
+ commitPrLookup404: 0,
803
+ mergeCommitAbsent: 0,
804
+ blameRangeSkipped: 0,
805
+ };
806
+ if (options.fixes || options.incidents) {
807
+ for (const pr of mergedPrs) {
808
+ const body = pr.body ?? "";
809
+ const fixMatch = options.fixes && isFixSubject(pr.title);
810
+ const incidentMatch = options.incidents
811
+ ? isIncidentText(pr.title, body)
812
+ : null;
813
+ if (!fixMatch && incidentMatch === null) continue;
814
+ const matchedSources: CorpusSource[] = [];
815
+ if (fixMatch) matchedSources.push("fix-subject");
816
+ if (incidentMatch !== null) matchedSources.push("incident-keyword");
817
+ const entry: CorpusWorking = {
818
+ fixPr: pr.number,
819
+ fixTitle: pr.title,
820
+ fixMergedAt: pr.mergedAt,
821
+ matchedSources,
822
+ // The title IS the fix evidence when the subject matched — the
823
+ // position-0 anchor is the whole point; otherwise the incident's
824
+ // matched line.
825
+ matchedText: fixMatch
826
+ ? evidenceExcerpt(pr.title)
827
+ : (incidentMatch ?? ""),
828
+ issueRefs: [],
829
+ fixBaseSha: null,
830
+ fixHeadSha: null,
831
+ additions: null,
832
+ deletions: null,
833
+ changedFiles: null,
834
+ introducer: null,
835
+ alsoBlamedCount: 0,
836
+ blameSkippedRenames: 0,
837
+ proximitySuspects: [],
838
+ };
839
+ working.set(pr.number, entry);
840
+ log(`corpus: PR #${pr.number} classified (${matchedSources.join(", ")})`);
841
+ blameFilesByPr.set(
842
+ pr.number,
843
+ await blameResolve({ repoRoot, slug, pr, entry, failures }),
844
+ );
845
+ }
846
+ }
847
+
848
+ // STEP D — bug-issue labels, over the classified set's refs. Cached per
849
+ // issue number: one fix frequently references the same tracking issue.
850
+ // Gated on --issues: the other four sources each have a flag, and running
851
+ // this whenever --fixes/--incidents classified anything billed a source
852
+ // the operator did not request (and made `sources run:` never list it).
853
+ if (options.issues && working.size > 0) {
854
+ const uniqueRefs: number[] = [];
855
+ for (const pr of mergedPrs) {
856
+ const entry = working.get(pr.number);
857
+ if (entry === undefined) continue;
858
+ const refs = issueRefsFromBody(pr.body ?? "");
859
+ entry.issueRefs = refs.map((number) => ({ number, matchedLabels: [] }));
860
+ for (const number of refs) {
861
+ if (!uniqueRefs.includes(number)) uniqueRefs.push(number);
862
+ }
863
+ }
864
+ const issueCache = new Map<number, IssueLabels | null>();
865
+ for (const number of uniqueRefs) {
866
+ const result = await gh(repoRoot, [
867
+ "api",
868
+ `repos/${slug}/issues/${number}`,
869
+ ]);
870
+ if (!result.ok) {
871
+ // A 404 ref degrades to "unresolved": listed without labels, confers
872
+ // nothing. It is a fact about one issue (deleted, or another repo's
873
+ // number pasted in), not a broken scan.
874
+ if (isNotFound(result.stderr)) {
875
+ issueCache.set(number, null);
876
+ log(`corpus: issue #${number} is unresolved (404)`);
877
+ continue;
878
+ }
879
+ throw new CliError(
880
+ `gh api repos/${slug}/issues/${number} failed: ` +
881
+ result.stderr.trim(),
882
+ );
883
+ }
884
+ try {
885
+ issueCache.set(number, parseIssueLabels(result.stdout));
886
+ } catch (error) {
887
+ if (error instanceof CliUsageError) {
888
+ throw new CliError(
889
+ `repos/${slug}/issues/${number}: ${error.message}`,
890
+ );
891
+ }
892
+ throw error;
893
+ }
894
+ }
895
+ for (const entry of working.values()) {
896
+ const { byRef, anyMatch } = matchBugLabels(
897
+ entry.issueRefs.map((ref) => ref.number),
898
+ issueCache,
899
+ bugLabels,
900
+ );
901
+ for (const ref of entry.issueRefs) {
902
+ ref.matchedLabels = byRef.get(ref.number) ?? [];
903
+ }
904
+ if (anyMatch && !entry.matchedSources.includes("bug-issue")) {
905
+ entry.matchedSources.push("bug-issue");
906
+ }
907
+ }
908
+ }
909
+
910
+ // STEP B — the commit/file index, only when --proximity asked for it.
911
+ if (options.proximity && mergedPrs.length > 0) {
912
+ const stamps = mergedPrs
913
+ .map((pr) =>
914
+ pr.mergedAt === null ? Number.NaN : Date.parse(pr.mergedAt),
915
+ )
916
+ .filter((ms) => !Number.isNaN(ms));
917
+ const earliestMs = stamps.length === 0 ? Number.NaN : Math.min(...stamps);
918
+ if (!Number.isNaN(earliestMs)) {
919
+ // window-start derives from RECORDED stamps only (earliest kept merge
920
+ // minus the window), rendered as ISO for git — no clock read anywhere.
921
+ const windowStartIso = new Date(
922
+ earliestMs - proximityDays * 86_400_000,
923
+ ).toISOString();
924
+ // `-m --first-parent` or the index comes back EMPTY on any merge-based
925
+ // repo. Without them git emits no numstat at all for a merge commit
926
+ // (measured: the merge itself yields only the format header, zero file
927
+ // lines) — and since `prBySha` below keys on `mergeCommitSha`, those
928
+ // fileless merges are the ONLY commits the join can resolve to a PR, so
929
+ // every candidate silently scored zero suspects. musive, the target
930
+ // repo, merges every PR this way. `--first-parent` also narrows the walk
931
+ // to one commit per PR plus the direct pushes, which is exactly the set
932
+ // `prBySha` can answer for — the side-branch commits it drops were
933
+ // unresolvable anyway. Both flags are no-ops on squash-merge history
934
+ // (verified: byte-identical output on a single-parent commit), so a
935
+ // linear repo sees no behaviour change.
936
+ const logged = await git(repoRoot, [
937
+ "log",
938
+ ref,
939
+ `--since=${windowStartIso}`,
940
+ "-m",
941
+ "--first-parent",
942
+ `--format=%H${GIT_LOG_FIELD_SEP}%ct`,
943
+ "--no-renames",
944
+ "--numstat",
945
+ ]);
946
+ if (!logged.ok) {
947
+ throw new CliError(
948
+ `git log ${ref} --since=${windowStartIso} failed: ` +
949
+ logged.stderr.trim(),
950
+ );
951
+ }
952
+ let commitIndex: CommitIndexEntry[];
953
+ try {
954
+ commitIndex = parseCommitIndex(logged.stdout);
955
+ } catch (error) {
956
+ if (error instanceof CliUsageError) {
957
+ throw new CliError(`git log ${ref}: ${error.message}`);
958
+ }
959
+ throw error;
960
+ }
961
+ log(
962
+ `corpus: proximity index — ${commitIndex.length} commit(s) since ` +
963
+ windowStartIso,
964
+ );
965
+ // Commit→PR via mergeCommitSha, over the walk's kept PRs: a commit
966
+ // whose merge the walk never saw cannot be a suspect, which bounds
967
+ // suspects to the window the artifact claims to have scanned.
968
+ const prBySha = new Map<string, CommitPrRef>();
969
+ for (const pr of mergedPrs) {
970
+ if (pr.mergeCommitSha === null) continue;
971
+ prBySha.set(pr.mergeCommitSha, {
972
+ pr: pr.number,
973
+ title: pr.title,
974
+ mergedAt: pr.mergedAt,
975
+ });
976
+ }
977
+ // ONLY entries whose introducer did not resolve (the pure join's
978
+ // caller-side rule): a resolved introducer needs no heuristic.
979
+ const unresolved: ProximityFix[] = [];
980
+ for (const entry of working.values()) {
981
+ if (entry.introducer !== null && entry.introducer.pr !== null) continue;
982
+ unresolved.push({
983
+ fixPr: entry.fixPr,
984
+ fixMergedAt: entry.fixMergedAt,
985
+ files: blameFilesByPr.get(entry.fixPr) ?? [],
986
+ });
987
+ }
988
+ const suspects = joinProximity(
989
+ unresolved,
990
+ commitIndex,
991
+ proximityDays,
992
+ prBySha,
993
+ );
994
+ for (const entry of working.values()) {
995
+ entry.proximitySuspects = suspects.get(entry.fixPr) ?? [];
996
+ }
997
+ }
998
+ }
999
+
1000
+ // STEP E — tier resolution + dedupe + order, all pure.
1001
+ const selected = selectCorpus([...working.values()]);
1002
+ // The same-PR introducer filter ran inside selectCorpus; the shell only
1003
+ // NOTES the drops it can still see in its own working set.
1004
+ for (const entry of working.values()) {
1005
+ if (entry.introducer?.pr === entry.fixPr) {
1006
+ log(
1007
+ `corpus: PR #${entry.fixPr} — blame resolved to the fix itself; ` +
1008
+ "introducer dropped (a PR cannot introduce what it fixed)",
1009
+ );
1010
+ }
1011
+ }
1012
+ log(
1013
+ `corpus: ${selected.length} fix-shaped candidate(s) across the ` +
1014
+ "issue-linked/blame-linked/keyword-only/proximity tiers",
1015
+ );
1016
+
1017
+ // STEP F — review threads.
1018
+ const threadCandidates: ThreadCandidate[] = options.threads
1019
+ ? await mineThreads({ repoRoot, slug, mergedPrs })
1020
+ : [];
1021
+ if (options.threads) {
1022
+ log(
1023
+ `corpus: ${threadCandidates.length} caught-in-review PR(s) from ` +
1024
+ `resolved threads (capped at ${THREAD_PAGE_SIZE} threads per PR, ` +
1025
+ `${THREAD_BATCH_SIZE} PRs per batch)`,
1026
+ );
1027
+ }
1028
+
1029
+ // An empty result is a valid state of the world, not an error: note it on
1030
+ // stderr naming the sources that ran, leave stdout clean, exit 0.
1031
+ if (selected.length === 0 && threadCandidates.length === 0) {
1032
+ log(
1033
+ `no corpus candidates from ${sourcesRun} over ${mergedPrs.length} ` +
1034
+ `scanned PR(s) on ${ref} since "${since}" — widen the window with ` +
1035
+ "--since or enable another source",
1036
+ );
1037
+ return 0;
1038
+ }
1039
+
1040
+ // STEP G — enrichment. One shared cache: a PR can appear in both sets, and
1041
+ // the walk's nodes never carried the size/range fields.
1042
+ const detailsCache = new Map<number, PullDetails | null>();
1043
+ const details = async (pr: number): Promise<PullDetails | null> => {
1044
+ const hit = detailsCache.get(pr);
1045
+ if (hit !== undefined) return hit;
1046
+ const fetched = await ghPullDetails(repoRoot, slug, pr);
1047
+ detailsCache.set(pr, fetched);
1048
+ return fetched;
1049
+ };
1050
+ for (const candidate of selected) {
1051
+ const pull = await details(candidate.fixPr);
1052
+ if (pull !== null) {
1053
+ candidate.fixBaseSha = pull.baseSha;
1054
+ candidate.fixHeadSha = pull.headSha;
1055
+ candidate.additions = pull.additions;
1056
+ candidate.deletions = pull.deletions;
1057
+ candidate.changedFiles = pull.changedFiles;
1058
+ }
1059
+ }
1060
+ for (const thread of threadCandidates) {
1061
+ const pull = await details(thread.pr);
1062
+ thread.baseSha = pull?.baseSha ?? null;
1063
+ thread.headSha = pull?.headSha ?? null;
1064
+ thread.additions = pull?.additions ?? null;
1065
+ thread.deletions = pull?.deletions ?? null;
1066
+ thread.changedFiles = pull?.changedFiles ?? null;
1067
+ }
1068
+
1069
+ const markdown = renderCorpusArtifact({
1070
+ repoSlug: slug,
1071
+ ref,
1072
+ since,
1073
+ scannedPrs: mergedPrs.length,
1074
+ sourcesRun,
1075
+ lookupFailures: failures,
1076
+ candidates: selected,
1077
+ threadCandidates,
1078
+ });
1079
+ if (options.out) {
1080
+ const outPath = path.resolve(options.out);
1081
+ await Bun.write(outPath, markdown);
1082
+ log(
1083
+ `corpus: wrote ${outPath} (${selected.length} fix-shaped + ` +
1084
+ `${threadCandidates.length} caught-in-review candidate(s) from ` +
1085
+ `${mergedPrs.length} scanned PR(s))`,
1086
+ );
1087
+ return 0;
1088
+ }
1089
+ // The markdown IS this command's product, and stdout is the one clean
1090
+ // channel (everything human-facing goes to stderr), so it can be piped or
1091
+ // redirected without the notes riding along.
1092
+ process.stdout.write(markdown);
1093
+ return 0;
1094
+ }