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
package/src/cli.ts ADDED
@@ -0,0 +1,348 @@
1
+ #!/usr/bin/env bun
2
+
3
+ // Local mode (ROADMAP B0): point the engine at a real repo + branch and get a
4
+ // human-readable review back. The lab drives this engine to MEASURE it; this
5
+ // is the other consumer — a developer, on their own tree, before the PR.
6
+ //
7
+ // The shape here is deliberate: every pure decision lives in preflight.ts and
8
+ // report.ts, and this file is the I/O shell — git, filesystem, stdin, spawn.
9
+ // That split is why the whole preflight can be tested offline, which matters
10
+ // more than usual when the alternative is testing it live at ~$10 a run.
11
+ //
12
+ // Two hard rules run through the sequence below:
13
+ // 1. every failure is loud and lands BEFORE any spend, and
14
+ // 2. human-readable output goes to stderr so stdout stays clean.
15
+ //
16
+ // cli-decomp Phase 2 (odd/tasks/cli-decomposition.md) moved the three
17
+ // orchestrators that used to live here — review(), reviewPr(), menuCommand()
18
+ // — out to their own domain modules (src/review/review.ts, src/pr/review-pr.ts,
19
+ // src/commands/menu.ts). This file now holds only the entry points: main()
20
+ // (argv parsing + command dispatch) and runCli() (signal handling + process
21
+ // exit).
22
+
23
+ import { activityCommand } from "#commands/activity";
24
+ import { ciSetupCommand } from "#commands/ci-setup";
25
+ import { configCommand } from "#commands/config";
26
+ import { doctorCommand } from "#commands/doctor";
27
+ import { init } from "#commands/init";
28
+ import { mcpCommand } from "#commands/mcp";
29
+ import { menuCommand } from "#commands/menu";
30
+ import { postCommand } from "#commands/post";
31
+ import { skillsInstallCommand } from "#commands/skills";
32
+ import { statusCommand } from "#commands/status";
33
+ import { triageCommand } from "#commands/triage";
34
+ import { uninstallCommand } from "#commands/uninstall";
35
+ import { upgradeCommand } from "#commands/upgrade";
36
+ import { usageCommand } from "#commands/usage";
37
+ import { resolveOptionalRepoRoot } from "#config/config";
38
+ import { corpusCommand } from "#corpus/corpus";
39
+ import { settleHeldAdmissionLedgerOnSignal } from "#pr/admission";
40
+ import {
41
+ type PrDryRunSizeGateResult,
42
+ resolvePrDryRunSizeGate,
43
+ } from "#pr/preflight";
44
+ import { revertsCommand } from "#pr/reverts";
45
+ import {
46
+ heldCommitStatusLock,
47
+ holdCommitStatusLock,
48
+ releaseCommitStatusLock,
49
+ settleHeldCommitStatusOnSignal,
50
+ } from "#pr/status";
51
+ import { createSignalHandler, settleOnSignal } from "./signal-handler";
52
+
53
+ export {
54
+ heldCommitStatusLock,
55
+ holdCommitStatusLock,
56
+ type PrDryRunSizeGateResult,
57
+ releaseCommitStatusLock,
58
+ resolvePrDryRunSizeGate,
59
+ settleHeldCommitStatusOnSignal,
60
+ };
61
+
62
+ import { classifyCliError } from "#ci/classify-error";
63
+ import { writeFatalCliRecord } from "#ci/cli-record";
64
+ import {
65
+ reportFatalCiError,
66
+ reportFatalCiErrorIfInJobStep,
67
+ } from "#ci/reporter";
68
+ import {
69
+ createRunDir,
70
+ DEFAULT_HEAD_REF,
71
+ DEFAULT_HOP_BUDGET,
72
+ HELP_TEXT,
73
+ parseArgs,
74
+ preflightAgentsDir,
75
+ resolveAgentsDir,
76
+ } from "#review/preflight";
77
+ import { CliError, CliUsageError } from "./errors";
78
+
79
+ export { createRunDir, preflightAgentsDir, resolveAgentsDir };
80
+
81
+ import {
82
+ type ProductionRoutePlanResult,
83
+ pipelineScoutInput,
84
+ pipelineSummarizerInput,
85
+ resolveProductionRoutePlanAtConfirm,
86
+ resolveRoutePlanAtConfirm,
87
+ } from "#review/route-preflight";
88
+
89
+ export {
90
+ type ProductionRoutePlanResult,
91
+ pipelineScoutInput,
92
+ pipelineSummarizerInput,
93
+ resolveProductionRoutePlanAtConfirm,
94
+ resolveRoutePlanAtConfirm,
95
+ };
96
+
97
+ import { review } from "#review/review";
98
+ import { unregisterActiveRun } from "#store/activity";
99
+ import { gcCommand } from "#store/gc";
100
+ import {
101
+ type ConfigProvenance,
102
+ configProvenanceOf,
103
+ dryRunHunterCount,
104
+ type PlanContext,
105
+ type PrPlanContext,
106
+ planDetails,
107
+ prPlanDetails,
108
+ renderPlan,
109
+ renderPrPlan,
110
+ } from "#ui/plan";
111
+
112
+ export {
113
+ type ConfigProvenance,
114
+ configProvenanceOf,
115
+ dryRunHunterCount,
116
+ type PlanContext,
117
+ type PrPlanContext,
118
+ planDetails,
119
+ prPlanDetails,
120
+ renderPlan,
121
+ renderPrPlan,
122
+ };
123
+
124
+ import { log, terminalWidth } from "#ui/primitives";
125
+ import { startPanelRenderer } from "#ui/progress";
126
+
127
+ export { startPanelRenderer };
128
+
129
+ import { watchCommand } from "#watch/watch";
130
+ import { readBrandEnv } from "./brand";
131
+ import {
132
+ closeAllServers,
133
+ killAllChildProcesses,
134
+ } from "./execution/spawned-process";
135
+ import { isMachineOnboarded, runWizard } from "./wizard";
136
+
137
+ // bin/pr-hero.js and the `import.meta.main` guard both go through the exported
138
+ // runCli() below, which is this function's only production caller. It is
139
+ // exported all the same because cli.test.ts drives it directly — a test IS a
140
+ // real consumer (project rule 3), and it is the only way to prove the two
141
+ // internal catches below write `status=error` without spawning a subprocess
142
+ // and guessing at its exit code.
143
+ export async function main(argv: string[]): Promise<number> {
144
+ // Bare zero-argument entry
145
+ if (argv.length === 0) {
146
+ if (readBrandEnv(process.env, "NO_TUI") !== undefined) {
147
+ log(HELP_TEXT);
148
+ return 0;
149
+ }
150
+ if (!process.stdin.isTTY || !process.stderr.isTTY) {
151
+ log(HELP_TEXT);
152
+ log();
153
+ log("error: no command given (interactive TTY required for menu)");
154
+ return 2;
155
+ }
156
+ if (terminalWidth() < 24) {
157
+ log("terminal too narrow (width < 24 columns)");
158
+ log();
159
+ log(HELP_TEXT);
160
+ return 2;
161
+ }
162
+ if (!isMachineOnboarded()) {
163
+ return await runWizard();
164
+ }
165
+ return await menuCommand({
166
+ repo: ".",
167
+ head: DEFAULT_HEAD_REF,
168
+ hopBudget: DEFAULT_HOP_BUDGET,
169
+ scout: false,
170
+ full: false,
171
+ dryRun: false,
172
+ yes: false,
173
+ post: false,
174
+ twoDot: false,
175
+ onPush: false,
176
+ force: false,
177
+ all: false,
178
+ fixes: false,
179
+ incidents: false,
180
+ issues: false,
181
+ proximity: false,
182
+ threads: false,
183
+ });
184
+ }
185
+
186
+ let parsed: ReturnType<typeof parseArgs>;
187
+ try {
188
+ parsed = parseArgs(argv);
189
+ } catch (error) {
190
+ log(HELP_TEXT);
191
+ log();
192
+ log(`error: ${(error as Error).message}`);
193
+ await reportFatalCiErrorIfInJobStep(error);
194
+ return 2;
195
+ }
196
+ if (parsed.command === "help") {
197
+ log(HELP_TEXT);
198
+ return 0;
199
+ }
200
+ if (parsed.command === "menu") {
201
+ if (!process.stdin.isTTY || !process.stderr.isTTY) {
202
+ log(HELP_TEXT);
203
+ log();
204
+ log("error: interactive TTY required for menu");
205
+ return 2;
206
+ }
207
+ if (terminalWidth() < 24) {
208
+ log("terminal too narrow (width < 24 columns)");
209
+ log();
210
+ log(HELP_TEXT);
211
+ return 2;
212
+ }
213
+ return await menuCommand(parsed.options);
214
+ }
215
+ try {
216
+ return parsed.command === "init"
217
+ ? await init(parsed.options)
218
+ : parsed.command === "setup"
219
+ ? parsed.options.ci
220
+ ? await ciSetupCommand(parsed.options)
221
+ : await runWizard({
222
+ cwd:
223
+ (await resolveOptionalRepoRoot(parsed.options)) ??
224
+ process.cwd(),
225
+ })
226
+ : parsed.command === "ci"
227
+ ? await ciSetupCommand(parsed.options)
228
+ : parsed.command === "doctor"
229
+ ? await doctorCommand(parsed.options)
230
+ : parsed.command === "activity"
231
+ ? await activityCommand(parsed.options)
232
+ : parsed.command === "watch"
233
+ ? await watchCommand(parsed.options)
234
+ : parsed.command === "post"
235
+ ? await postCommand(parsed.options)
236
+ : parsed.command === "status"
237
+ ? await statusCommand(parsed.options)
238
+ : parsed.command === "skills"
239
+ ? await skillsInstallCommand(parsed.options)
240
+ : parsed.command === "triage"
241
+ ? await triageCommand(parsed.options)
242
+ : parsed.command === "gc"
243
+ ? await gcCommand(parsed.options)
244
+ : parsed.command === "usage"
245
+ ? await usageCommand(parsed.options)
246
+ : parsed.command === "reverts"
247
+ ? await revertsCommand(parsed.options)
248
+ : parsed.command === "corpus"
249
+ ? await corpusCommand(parsed.options)
250
+ : parsed.command === "config"
251
+ ? await configCommand(parsed.options)
252
+ : parsed.command === "mcp"
253
+ ? await mcpCommand(parsed.options)
254
+ : parsed.command === "upgrade"
255
+ ? await upgradeCommand(parsed.options)
256
+ : parsed.command === "uninstall"
257
+ ? await uninstallCommand(parsed.options)
258
+ : await review(parsed.options);
259
+ } catch (error) {
260
+ if (error instanceof CliError || error instanceof CliUsageError) {
261
+ writeFatalCliRecord(classifyCliError(error));
262
+ log(`error: ${error.message}`);
263
+ await reportFatalCiErrorIfInJobStep(error);
264
+ return 1;
265
+ }
266
+ throw error;
267
+ }
268
+ }
269
+
270
+ // Exported so bin/pr-hero.js can drive the exact same signal-handling +
271
+ // exit-code path as a direct `bun run src/cli.ts` invocation. `bun bin/pr-hero.js
272
+ // ...` (the npm-installed entrypoint) reaches this file through `import`, and
273
+ // `import.meta.main` is false for every imported module — only the directly
274
+ // executed entry file gets `true` — so the guard below never ran for it and the
275
+ // installed `pr-hero` command was a silent, zero-output, exit-0 no-op. Covered
276
+ // by packaging.test.ts's subprocess spawn of bin/pr-hero.js.
277
+ export async function runCli(
278
+ argv: string[] = Bun.argv.slice(2),
279
+ ): Promise<void> {
280
+ // WHY only on a TTY: the escape only undoes a cursor the menu hid on a
281
+ // terminal. On a pipe or a CI log it prints as literal "[?25h" after every
282
+ // command, so a non-TTY run writes nothing.
283
+ const restoreCursor = () => {
284
+ if (process.stderr.isTTY !== true) return;
285
+ try {
286
+ process.stderr.write("\x1b[?25h");
287
+ } catch {
288
+ // Ignore
289
+ }
290
+ };
291
+ // WHY one settle per process: a second signal must not cut the first one's
292
+ // settle short. Actions cancels with SIGINT and follows with SIGTERM before
293
+ // its grace period ends, and under npx a terminal Ctrl-C with a piped stdin
294
+ // reaches the CLI twice (the process group, and the launcher's forward,
295
+ // bin/pr-hero.js). The settles are take-and-clear, so a second handler found
296
+ // nothing to post and exited at once, killing the first handler's in-flight
297
+ // commit-status and ledger writes. Every later signal now waits for the
298
+ // first settle, which exits the process.
299
+ const onSignal = createSignalHandler({
300
+ settle: async () => {
301
+ restoreCursor();
302
+ // After killing the children (stop spending first), before exiting: the
303
+ // in-flight lock this process took is released by its holder, or the next
304
+ // run skips the head for the rest of the 90-minute TTL (#146). The
305
+ // OpenCode server is closed, not killed: its close() also destroys the
306
+ // projected credential it runs under.
307
+ await settleOnSignal({
308
+ killChildren: killAllChildProcesses,
309
+ closeServers: closeAllServers,
310
+ unregisterRun: () => unregisterActiveRun(process.pid),
311
+ settleCommitStatus: settleHeldCommitStatusOnSignal,
312
+ settleAdmissionLedger: settleHeldAdmissionLedgerOnSignal,
313
+ });
314
+ },
315
+ exit: (code) => process.exit(code),
316
+ });
317
+ process.on("SIGTERM", onSignal(143));
318
+ process.on("SIGINT", onSignal(130));
319
+ process.on("exit", () => {
320
+ restoreCursor();
321
+ });
322
+ let exitCode: number;
323
+ try {
324
+ exitCode = await main(argv);
325
+ } catch (error) {
326
+ // Before the rethrow below, and whatever GITHUB_OUTPUT says: a crash in
327
+ // action mode is the record's to name, unless this job already recorded its
328
+ // terminal outcome (writeFatalCliRecord keeps that one).
329
+ writeFatalCliRecord(classifyCliError(error));
330
+ const outputPath = process.env.GITHUB_OUTPUT;
331
+ if (outputPath === undefined || outputPath.length === 0) {
332
+ // Not a real GitHub Actions job step — preserve the original
333
+ // uncaught-exception path so a local `bun run src/cli.ts` crash still
334
+ // prints its full stack trace instead of a swallowed one-line message.
335
+ throw error;
336
+ }
337
+ await reportFatalCiError(error, outputPath);
338
+ exitCode = 1;
339
+ }
340
+ process.exit(exitCode);
341
+ }
342
+
343
+ // Only when executed, never on import — the pure helpers (and runCli itself)
344
+ // stay importable from tests / bin/pr-hero.js without the CLI trying to run a
345
+ // review as a side effect of the import.
346
+ if (import.meta.main) {
347
+ await runCli();
348
+ }
@@ -0,0 +1,67 @@
1
+ import os from "node:os";
2
+ import type { CliOptions } from "#review/preflight";
3
+ import {
4
+ getWatcherSpend,
5
+ killActiveRun,
6
+ listActiveRuns,
7
+ queryRecentRuns,
8
+ } from "#store/activity";
9
+ import { renderActivityScreen } from "#ui/activity";
10
+ import { log, styleEnabled, terminalWidth } from "#ui/primitives";
11
+ import { CliError } from "../errors";
12
+
13
+ export async function activityCommand(options: CliOptions): Promise<number> {
14
+ const home = os.homedir();
15
+
16
+ if (options.kill !== undefined) {
17
+ const pid = options.kill;
18
+ if (!options.yes) {
19
+ if (!process.stdin.isTTY) {
20
+ throw new CliError(
21
+ "--yes is required to terminate a review in non-interactive mode",
22
+ );
23
+ }
24
+ process.stderr.write(`Terminate review process (PID ${pid})? [y/N] `);
25
+ const reader = process.stdin[Symbol.asyncIterator]();
26
+ const chunk = (await reader.next()).value;
27
+ const answer = chunk ? chunk.toString().trim().toLowerCase() : "";
28
+ if (answer !== "y" && answer !== "yes") {
29
+ log("Aborted.");
30
+ return 0;
31
+ }
32
+ }
33
+
34
+ const res = await killActiveRun(pid, { home });
35
+ if (res.status === "not_found") {
36
+ log(`error: ${res.message}`);
37
+ return 1;
38
+ }
39
+ if (res.status === "identity_mismatch") {
40
+ log(`error: ${res.message}`);
41
+ return 1;
42
+ }
43
+ if (res.status === "terminated") {
44
+ log(`✓ Terminated review process ${res.pid} (${res.signal}).`);
45
+ if (res.warning) {
46
+ log(`warning: ${res.warning}`);
47
+ }
48
+ return 0;
49
+ }
50
+ return 0;
51
+ }
52
+
53
+ const runs = await listActiveRuns({ home });
54
+ const spend = await getWatcherSpend({ home });
55
+ const history = await queryRecentRuns({ home, limit: 10 });
56
+
57
+ const lines = renderActivityScreen(
58
+ { runs, spend, history },
59
+ { styles: styleEnabled(), width: terminalWidth() },
60
+ );
61
+
62
+ for (const line of lines) {
63
+ log(line);
64
+ }
65
+
66
+ return 0;
67
+ }