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,377 @@
1
+ // Revert/hotfix mining I/O (GitHub #41). Runs `git log` over the repo's
2
+ // DEFAULT BRANCH and asks `gh api` for the PR numbers and diff sizes, then
3
+ // writes a markdown artifact of CANDIDATES. Every decision — what counts as a
4
+ // revert, which duplicates collapse, how the artifact renders — is pure in
5
+ // pr/reverts-preflight.ts, where the tests live.
6
+ //
7
+ // What this command deliberately does NOT do, straight from the issue's
8
+ // scope: it never runs a review, never scores, never labels what the defect
9
+ // was, and never spends a cent. It is `git log` plus read-only `gh api`.
10
+ //
11
+ // Same git-runner rule as the other shells: args as an ARRAY, never an
12
+ // interpolated shell string.
13
+
14
+ import path from "node:path";
15
+ import { git, resolveRepoRoot } from "#git/git";
16
+ import { parseRemoteHead, repoWebUrlFromRemote } from "#git/refs";
17
+ import type { CliOptions } from "#review/preflight";
18
+ import { log } from "#ui/primitives";
19
+ import { CliError, CliUsageError } from "../errors";
20
+ import {
21
+ type CommitPullRef,
22
+ classifyRevertCommit,
23
+ DEFAULT_REVERTS_SINCE,
24
+ GIT_LOG_FORMAT,
25
+ type PullDetails,
26
+ parseCommitPulls,
27
+ parseGitLogRecords,
28
+ parsePullDetails,
29
+ pickCommitPull,
30
+ type RevertCandidate,
31
+ renderRevertsArtifact,
32
+ repoSlugFromWebUrl,
33
+ selectRevertCandidates,
34
+ } from "./reverts-preflight";
35
+
36
+ // Same shape as pr/pr.ts's gh, and the same single testability seam: `spawnFn`
37
+ // is invisible to production callers, and the `Bun.which("gh")` guard is
38
+ // skipped under a fake spawn so an offline test never depends on whether the
39
+ // machine running it happens to have the GitHub CLI installed.
40
+ async function gh(
41
+ operatorRoot: string,
42
+ args: string[],
43
+ spawnFn?: typeof Bun.spawn,
44
+ ): Promise<{ ok: boolean; stdout: string; stderr: string }> {
45
+ const spawn = spawnFn ?? Bun.spawn;
46
+ // A missing gh must name itself: Bun.spawn's error for a binary that is
47
+ // not there reads like a crash, not like "install the GitHub CLI".
48
+ if (spawnFn === undefined && Bun.which("gh") === null) {
49
+ throw new CliError(
50
+ "gh not found on PATH — reverts resolves PR numbers through the " +
51
+ "GitHub CLI. Install it and authenticate (gh auth login) first.",
52
+ );
53
+ }
54
+ // cwd = operator root, same as pr/pr.ts: gh resolves credentials and host from
55
+ // the checkout, so the API consulted belongs to the repo passed as --repo.
56
+ const proc = spawn(["gh", ...args], {
57
+ cwd: operatorRoot,
58
+ stdout: "pipe",
59
+ stderr: "pipe",
60
+ });
61
+ const [stdout, stderr, exitCode] = await Promise.all([
62
+ new Response(proc.stdout).text(),
63
+ new Response(proc.stderr).text(),
64
+ proc.exited,
65
+ ]);
66
+ return { ok: exitCode === 0, stdout, stderr };
67
+ }
68
+
69
+ // The candidates tried, in order, when origin/HEAD is unset. Named in the
70
+ // error so a failure is one command from being fixed rather than a guess.
71
+ const FALLBACK_DEFAULT_BRANCHES = ["origin/main", "origin/master"];
72
+
73
+ // STEP 1. WHY this is not cosmetic: musive's default branch is `dev`, so a
74
+ // hardcoded `main` mines nothing at all and reports "no candidates" — a wrong
75
+ // answer with a completely plausible face.
76
+ //
77
+ // And WHY only this ref, never `--all`: a revert reachable only from a side
78
+ // branch never shipped to anybody, so it is not ground truth about a
79
+ // regression that reached users. The default branch is the whole point.
80
+ async function resolveDefaultBranchRef(repoRoot: string): Promise<string> {
81
+ const symbolic = await git(repoRoot, [
82
+ "symbolic-ref",
83
+ "--quiet",
84
+ "refs/remotes/origin/HEAD",
85
+ ]);
86
+ if (symbolic.ok) {
87
+ const branch = parseRemoteHead(symbolic.stdout);
88
+ if (branch !== undefined) return `origin/${branch}`;
89
+ }
90
+ for (const candidate of FALLBACK_DEFAULT_BRANCHES) {
91
+ const verified = await git(repoRoot, [
92
+ "rev-parse",
93
+ "--verify",
94
+ "--quiet",
95
+ "--end-of-options",
96
+ `${candidate}^{commit}`,
97
+ ]);
98
+ if (verified.ok) return candidate;
99
+ }
100
+ throw new CliError(
101
+ `cannot resolve the default branch of ${repoRoot}: ` +
102
+ "refs/remotes/origin/HEAD is unset and neither " +
103
+ `${FALLBACK_DEFAULT_BRANCHES.join(" nor ")} exists. Run \`git remote ` +
104
+ "set-head origin --auto` (or fetch the remote) first.",
105
+ );
106
+ }
107
+
108
+ async function resolveRepoSlug(repoRoot: string): Promise<string> {
109
+ const remote = await git(repoRoot, ["remote", "get-url", "origin"]);
110
+ if (!remote.ok) {
111
+ throw new CliError(
112
+ `cannot read origin's url in ${repoRoot}: ${remote.stderr.trim()}`,
113
+ );
114
+ }
115
+ const webUrl = repoWebUrlFromRemote(remote.stdout);
116
+ const slug = webUrl === undefined ? null : repoSlugFromWebUrl(webUrl);
117
+ if (slug === null) {
118
+ throw new CliError(
119
+ `origin of ${repoRoot} does not look like a github.com repository ` +
120
+ `(${remote.stdout.trim()}) — reverts resolves PR numbers through ` +
121
+ "the GitHub API, so it needs one",
122
+ );
123
+ }
124
+ return slug;
125
+ }
126
+
127
+ // A 404 is a fact about ONE pull, not a broken command: a PR from a deleted
128
+ // fork, or a commit that reached the branch outside a PR. It degrades that
129
+ // field to "unresolved" and the scan continues. Anything else — auth, rate
130
+ // limit, network — is systematic, and continuing would produce an artifact
131
+ // that is quietly missing most of its entries.
132
+ function isNotFound(stderr: string): boolean {
133
+ return /HTTP 404/.test(stderr);
134
+ }
135
+
136
+ interface GhSeam {
137
+ spawnFn?: typeof Bun.spawn;
138
+ }
139
+
140
+ async function ghCommitPulls(
141
+ operatorRoot: string,
142
+ slug: string,
143
+ sha: string,
144
+ options?: GhSeam,
145
+ ): Promise<CommitPullRef[]> {
146
+ const result = await gh(
147
+ operatorRoot,
148
+ ["api", `repos/${slug}/commits/${sha}/pulls`],
149
+ options?.spawnFn,
150
+ );
151
+ if (!result.ok) {
152
+ if (isNotFound(result.stderr)) return [];
153
+ throw new CliError(
154
+ `gh api repos/${slug}/commits/${sha}/pulls failed: ` +
155
+ result.stderr.trim(),
156
+ );
157
+ }
158
+ try {
159
+ return parseCommitPulls(result.stdout);
160
+ } catch (error) {
161
+ // The pure reader names the field; only the shell knows the endpoint.
162
+ if (error instanceof CliUsageError) {
163
+ throw new CliError(
164
+ `repos/${slug}/commits/${sha}/pulls: ${error.message}`,
165
+ );
166
+ }
167
+ throw error;
168
+ }
169
+ }
170
+
171
+ async function ghPullDetails(
172
+ operatorRoot: string,
173
+ slug: string,
174
+ pr: number,
175
+ options?: GhSeam,
176
+ ): Promise<PullDetails | null> {
177
+ const result = await gh(
178
+ operatorRoot,
179
+ ["api", `repos/${slug}/pulls/${pr}`],
180
+ options?.spawnFn,
181
+ );
182
+ if (!result.ok) {
183
+ if (isNotFound(result.stderr)) return null;
184
+ throw new CliError(
185
+ `gh api repos/${slug}/pulls/${pr} failed: ${result.stderr.trim()}`,
186
+ );
187
+ }
188
+ try {
189
+ return parsePullDetails(result.stdout);
190
+ } catch (error) {
191
+ if (error instanceof CliUsageError) {
192
+ throw new CliError(`repos/${slug}/pulls/${pr}: ${error.message}`);
193
+ }
194
+ throw error;
195
+ }
196
+ }
197
+
198
+ interface MineResult {
199
+ scannedCommits: number;
200
+ candidates: RevertCandidate[];
201
+ }
202
+
203
+ async function mineReverts(input: {
204
+ repoRoot: string;
205
+ slug: string;
206
+ ref: string;
207
+ since: string;
208
+ gh?: GhSeam;
209
+ }): Promise<MineResult> {
210
+ // STEP 2. `--no-merges` is deliberately NOT passed: a merge commit's own
211
+ // subject (`Merge pull request #<n> from …/hotfix/…`) is one of the two
212
+ // pattern-only signals, so dropping merges would drop half the classes.
213
+ const logged = await git(input.repoRoot, [
214
+ "log",
215
+ input.ref,
216
+ `--since=${input.since}`,
217
+ GIT_LOG_FORMAT,
218
+ ]);
219
+ if (!logged.ok) {
220
+ throw new CliError(
221
+ `git log ${input.ref} --since=${input.since} failed: ` +
222
+ logged.stderr.trim(),
223
+ );
224
+ }
225
+ let records: ReturnType<typeof parseGitLogRecords>;
226
+ try {
227
+ records = parseGitLogRecords(logged.stdout);
228
+ } catch (error) {
229
+ if (error instanceof CliUsageError) {
230
+ throw new CliError(`git log ${input.ref}: ${error.message}`);
231
+ }
232
+ throw error;
233
+ }
234
+
235
+ // STEP 3 + 4: classify, then resolve PR numbers only for what classified.
236
+ const raw: RevertCandidate[] = [];
237
+ for (const record of records) {
238
+ const classified = classifyRevertCommit(record);
239
+ if (classified === null) continue;
240
+
241
+ let revertingPr = classified.mergePr;
242
+ let revertingPrMergedAt: string | null = null;
243
+ if (revertingPr === null) {
244
+ const pulls = await ghCommitPulls(
245
+ input.repoRoot,
246
+ input.slug,
247
+ classified.sha,
248
+ input.gh,
249
+ );
250
+ const picked = pickCommitPull(pulls);
251
+ revertingPr = picked?.number ?? null;
252
+ revertingPrMergedAt = picked?.mergedAt ?? null;
253
+ }
254
+
255
+ let revertedPr = classified.branchPr;
256
+ if (revertedPr === null && classified.revertedSha !== null) {
257
+ const pulls = await ghCommitPulls(
258
+ input.repoRoot,
259
+ input.slug,
260
+ classified.revertedSha,
261
+ input.gh,
262
+ );
263
+ revertedPr = pickCommitPull(pulls)?.number ?? null;
264
+ }
265
+
266
+ raw.push({
267
+ revertCommitSha: classified.sha,
268
+ revertCommittedAtSec: classified.committedAtSec,
269
+ revertSubject: classified.subject,
270
+ revertBody: classified.body,
271
+ confidence: classified.confidence,
272
+ revertingPr,
273
+ revertingPrMergedAt,
274
+ revertedPr,
275
+ revertedPrTitle: null,
276
+ revertedPrMergedAt: null,
277
+ revertedBaseSha: null,
278
+ revertedHeadSha: null,
279
+ additions: null,
280
+ deletions: null,
281
+ changedFiles: null,
282
+ collapsedCommits: 1,
283
+ });
284
+ }
285
+
286
+ // STEP 5: the two filters plus the deterministic order, all pure. Run
287
+ // BEFORE enrichment so the collapsed duplicates cost no gh calls.
288
+ const selected = selectRevertCandidates(raw);
289
+
290
+ // STEP 6: enrich. The commit-pulls endpoint returns additions, deletions
291
+ // and changed_files as NULL (verified), so the size and the replay range
292
+ // can only come from this second call. Cached by PR number because one PR
293
+ // is frequently both sides of neighbouring entries.
294
+ const cache = new Map<number, PullDetails | null>();
295
+ const details = async (pr: number): Promise<PullDetails | null> => {
296
+ const hit = cache.get(pr);
297
+ if (hit !== undefined) return hit;
298
+ const fetched = await ghPullDetails(
299
+ input.repoRoot,
300
+ input.slug,
301
+ pr,
302
+ input.gh,
303
+ );
304
+ cache.set(pr, fetched);
305
+ return fetched;
306
+ };
307
+ const enriched: RevertCandidate[] = [];
308
+ for (const candidate of selected) {
309
+ const next = { ...candidate };
310
+ if (next.revertedPr !== null) {
311
+ const pull = await details(next.revertedPr);
312
+ if (pull !== null) {
313
+ next.revertedPrTitle = pull.title;
314
+ next.revertedPrMergedAt = pull.mergedAt;
315
+ next.revertedBaseSha = pull.baseSha;
316
+ next.revertedHeadSha = pull.headSha;
317
+ next.additions = pull.additions;
318
+ next.deletions = pull.deletions;
319
+ next.changedFiles = pull.changedFiles;
320
+ }
321
+ }
322
+ // The reverting side needs only its merge stamp, and body-linked entries
323
+ // already carry it from the commit-pulls response — so this call is made
324
+ // ONLY for the pattern-only entries whose number came from a merge
325
+ // subject and therefore never touched the API.
326
+ if (next.revertingPr !== null && next.revertingPrMergedAt === null) {
327
+ next.revertingPrMergedAt =
328
+ (await details(next.revertingPr))?.mergedAt ?? null;
329
+ }
330
+ enriched.push(next);
331
+ }
332
+ return { scannedCommits: records.length, candidates: enriched };
333
+ }
334
+
335
+ export async function revertsCommand(options: CliOptions): Promise<number> {
336
+ const repoRoot = await resolveRepoRoot(options.repo);
337
+ const slug = await resolveRepoSlug(repoRoot);
338
+ const ref = await resolveDefaultBranchRef(repoRoot);
339
+ const since = options.since ?? DEFAULT_REVERTS_SINCE;
340
+ log(`reverts: scanning ${slug} ${ref} since "${since}" (read-only, $0)`);
341
+
342
+ const mined = await mineReverts({ repoRoot, slug, ref, since });
343
+
344
+ // An empty result is a valid state of the world (this repo reverted
345
+ // nothing in the window), not an error: note it on stderr, leave stdout
346
+ // clean, exit 0.
347
+ if (mined.candidates.length === 0) {
348
+ log(
349
+ `no revert or hotfix candidates on ${ref} since "${since}" ` +
350
+ `(${mined.scannedCommits} commit(s) scanned) — widen the window with ` +
351
+ "--since",
352
+ );
353
+ return 0;
354
+ }
355
+
356
+ const markdown = renderRevertsArtifact({
357
+ repoSlug: slug,
358
+ ref,
359
+ since,
360
+ scannedCommits: mined.scannedCommits,
361
+ candidates: mined.candidates,
362
+ });
363
+ if (options.out) {
364
+ const outPath = path.resolve(options.out);
365
+ await Bun.write(outPath, markdown);
366
+ log(
367
+ `reverts: wrote ${outPath} (${mined.candidates.length} candidate(s) ` +
368
+ `from ${mined.scannedCommits} commit(s))`,
369
+ );
370
+ return 0;
371
+ }
372
+ // The markdown IS this command's product, and stdout is the one clean
373
+ // channel (everything human-facing goes to stderr), so it can be piped or
374
+ // redirected without the notes riding along.
375
+ process.stdout.write(markdown);
376
+ return 0;
377
+ }