crysk 0.0.0 → 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 +609 -3
  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 +81 -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,787 @@
1
+ // The pure builder behind `pr-hero status` (ROADMAP S9): what a skill needs to drive
2
+ // review -> reply -> fix -> wait, read from the PR's own GitHub facts. Raw inputs in, the
3
+ // status object out, no I/O: src/commands/status.ts reads GitHub and hands the results here.
4
+ //
5
+ // Every reader is one the engine already uses: the gate (preflight.ts latestStatusInContext,
6
+ // isInFlightCommitStatus), the summary and its state block (findMarkedCommentId,
7
+ // parsePrCommentMarker, parseStateBlock), each finding (parseFindingMarker, and the header
8
+ // badge of report.ts), the triage a finding was answered with (parseTriageMarker, bound by
9
+ // the review thread, or by the link an outside-channel reply carries), and the newest outcome
10
+ // (parseOutcomeCode). Nothing here re-parses a
11
+ // format another module owns, so a drift in one of them moves this command with it.
12
+
13
+ import { OUTCOME_MARKER_PREFIX, parseOutcomeCode } from "#ci/outcome";
14
+ import { isOverrideStatus, overrideActorOf } from "#ci/override";
15
+ import {
16
+ OVERRIDE_COMMAND,
17
+ RECHECK_COMMAND,
18
+ REVIEW_COMMAND,
19
+ REVIEW_FULL_COMMAND,
20
+ } from "#ci/trigger";
21
+ import {
22
+ COMMIT_STATUS_CONTEXT,
23
+ type CommitStatusFact,
24
+ findMarkedCommentId,
25
+ IN_FLIGHT_TTL_MS,
26
+ isInFlightCommitStatus,
27
+ latestStatusInContext,
28
+ type ParsedFindingMarker,
29
+ parseFindingMarker,
30
+ } from "#pr/preflight";
31
+ import {
32
+ type RecoveredTier,
33
+ SEVERITY_UNRECOVERABLE,
34
+ TIER_UNRECOVERABLE,
35
+ } from "#rereview/classify";
36
+ import { parseProofRefLocs } from "#rereview/identity";
37
+ import { bindPriorsToPosted } from "#rereview/prepare";
38
+ import { parseStateBlock, type StateFinding } from "#rereview/state";
39
+ import { parseFindingCommentBadge } from "#review/report";
40
+ import {
41
+ CI_ADJUDICATOR_LOGIN,
42
+ parseAdjudicationMarker,
43
+ } from "#triage/adjudication";
44
+ import {
45
+ type ParsedTriageMarker,
46
+ parseTriageMarker,
47
+ type TriageActor,
48
+ type TriageTag,
49
+ type TriageVerdict,
50
+ } from "#triage/triage";
51
+ import { parsePrCommentMarker } from "#watch/preflight";
52
+ import {
53
+ canonicalStatusText,
54
+ commandHint,
55
+ isCommand,
56
+ matchMarkerPrefix,
57
+ } from "../brand";
58
+
59
+ export const PR_STATUS_SCHEMA = "pr-hero.status/v1" as const;
60
+
61
+ export type PrGateState = "success" | "failure" | "error" | "pending" | "none";
62
+ export type PrNextAction =
63
+ | "wait"
64
+ | "review"
65
+ | "triage"
66
+ | "fix"
67
+ | "override-or-fix"
68
+ | "recheck"
69
+ | "done";
70
+
71
+ export interface PrStatusGate {
72
+ state: PrGateState;
73
+ description: string | null;
74
+ target_url: string | null;
75
+ created_at: string | null;
76
+ pending_in_flight: boolean;
77
+ override: { active: boolean; by: string | null };
78
+ }
79
+
80
+ export interface PrStatusReport {
81
+ head: string | null;
82
+ complete: boolean | null;
83
+ reviews: number | null;
84
+ outcome_code: string | null;
85
+ }
86
+
87
+ export interface PrStatusTriage {
88
+ tag: TriageTag;
89
+ verdict: TriageVerdict | null;
90
+ actor: TriageActor;
91
+ head: string;
92
+ }
93
+
94
+ export interface PrStatusFinding {
95
+ id: string | null;
96
+ path: string;
97
+ line: number;
98
+ // The head the finding was posted on. Null for a state row no comment carries: the row has no
99
+ // head of its own, and nothing was posted here.
100
+ head: string | null;
101
+ // Null for a finding with no comment thread (a state row no comment bound: summary-only, or
102
+ // never posted inline). Such a finding cannot be answered with a triage reply.
103
+ comment_id: number | null;
104
+ channel: "inline" | "outside";
105
+ severity: string | null;
106
+ // The tier the badge or the state row reports: "blocking" or "advisory". "unrecoverable" (the
107
+ // engine's sentinel, rereview/classify.ts) when neither can give one. Never null.
108
+ // `blocking` is the field every blocking decision reads.
109
+ tier: RecoveredTier;
110
+ // True for every tier but advisory. An unrecoverable tier counts as blocking because the gate
111
+ // counts it that way (rereview/state.ts unresolvedBlockingCount): fail closed, so a finding
112
+ // that cannot be read as advisory is never waved through.
113
+ blocking: boolean;
114
+ live_on_head: boolean;
115
+ triage: PrStatusTriage | null;
116
+ // The CI adjudicator's ruling on the newest triage reply, counted only when github-actions[bot] posted it on the head being read (src/triage/adjudication.ts). Null when there is none.
117
+ ci_verdict: "upheld" | "rejected" | "inconclusive" | null;
118
+ }
119
+
120
+ export interface PrStatusJson {
121
+ schema: typeof PR_STATUS_SCHEMA;
122
+ pr: number;
123
+ head: string;
124
+ gate: PrStatusGate;
125
+ report: PrStatusReport;
126
+ findings: PrStatusFinding[];
127
+ unresolved_blocking: number;
128
+ next_action: PrNextAction;
129
+ reason: string;
130
+ }
131
+
132
+ // A GitHub comment as the shell reads it. Review comments also carry the thread link a
133
+ // triage reply is bound by (in_reply_to_id); issue comments have none.
134
+ export interface PrStatusComment {
135
+ id: number;
136
+ body: string;
137
+ created_at?: string;
138
+ }
139
+
140
+ export const STATUS_WAIT_POLL_INTERVAL_MS = 15_000;
141
+ export const STATUS_WAIT_STARTUP_GRACE_MS = 180_000;
142
+ export const STATUS_WAIT_MAX_MS = IN_FLIGHT_TTL_MS;
143
+
144
+ const TRIGGER_COMMANDS = [REVIEW_COMMAND, REVIEW_FULL_COMMAND, RECHECK_COMMAND];
145
+
146
+ // A review comment's live projection (pr/pr.ts PrReviewComment) carries the path and line
147
+ // GitHub shows now, which follow a rename or a re-anchor. Optional, as the engine's own posted
148
+ // shape has them (rereview/prepare.ts PostedForPrior): without them the marker's anchor stands.
149
+ export interface PrStatusReviewComment extends PrStatusComment {
150
+ in_reply_to_id: number | null;
151
+ // The GitHub login that posted it. The CI adjudicator's ruling is trusted only from CI_ADJUDICATOR_LOGIN.
152
+ user?: string;
153
+ path?: string;
154
+ line?: number | null;
155
+ }
156
+
157
+ export interface PrStatusInput {
158
+ pr: number;
159
+ head: string;
160
+ nowMs: number;
161
+ statuses: CommitStatusFact[];
162
+ issueComments: PrStatusComment[];
163
+ reviewComments: PrStatusReviewComment[];
164
+ }
165
+
166
+ // The window a pending status is trusted for, named from the constant that defines it, so
167
+ // the reason a stale pending reads as "review" never drifts from the watcher's own rule.
168
+ const IN_FLIGHT_WINDOW_MINUTES = Math.round(IN_FLIGHT_TTL_MS / 60_000);
169
+
170
+ // A finding as the decision needs it: the output row, plus whether a triage reply sits at
171
+ // the head the finding was posted on (a reply at another head does not answer it).
172
+ interface AnalyzedFinding {
173
+ finding: PrStatusFinding;
174
+ triagedAtPostedHead: boolean;
175
+ }
176
+
177
+ export function buildPrStatus(input: PrStatusInput): PrStatusJson {
178
+ const gate = readGate(input);
179
+ const summary = readSummary(input.issueComments);
180
+ const analyzed = readFindings(input, summary.rows);
181
+ const liveBlocking = analyzed.filter(
182
+ (a) => a.finding.blocking && a.finding.live_on_head,
183
+ );
184
+ const decision = decideNext(gate, analyzed, input.head, summary);
185
+ return {
186
+ schema: PR_STATUS_SCHEMA,
187
+ pr: input.pr,
188
+ head: input.head,
189
+ gate,
190
+ report: {
191
+ head: summary.head,
192
+ complete: summary.complete,
193
+ reviews: summary.reviews,
194
+ outcome_code: outcomeCodeOf(input.issueComments),
195
+ },
196
+ findings: analyzed.map((a) => a.finding),
197
+ // A dismissal the CI adjudicator upheld on this head leaves the gate's count (review #348
198
+ // F001), so the fallback count leaves it out too.
199
+ unresolved_blocking: unresolvedBlocking(
200
+ gate,
201
+ liveBlocking.filter((a) => !upheldDismissal(a)).length,
202
+ ),
203
+ next_action: decision.next_action,
204
+ reason: decision.reason,
205
+ };
206
+ }
207
+
208
+ // The gate is crysk's own commit status on the head, read by its context alone (the legacy
209
+ // pr-hero name counts as the same context), so a local run's crysk/local status never
210
+ // answers for the gate.
211
+ function readGate(input: PrStatusInput): PrStatusGate {
212
+ const latest = latestStatusInContext(input.statuses, COMMIT_STATUS_CONTEXT);
213
+ if (latest === null) {
214
+ return {
215
+ state: "none",
216
+ description: null,
217
+ target_url: null,
218
+ created_at: null,
219
+ pending_in_flight: false,
220
+ override: { active: false, by: null },
221
+ };
222
+ }
223
+ const active = isOverrideStatus(latest);
224
+ const description = latest.description ?? null;
225
+ return {
226
+ state: gateStateOf(latest.state),
227
+ description,
228
+ target_url: latest.target_url ?? null,
229
+ created_at: latest.created_at,
230
+ pending_in_flight: isInFlightCommitStatus(input.statuses, input.nowMs),
231
+ override: {
232
+ active,
233
+ by: active && description !== null ? overrideActorOf(description) : null,
234
+ },
235
+ };
236
+ }
237
+
238
+ // A state the reader does not know is read as error, which sends a review out. It never
239
+ // reads as success, so an unrecognised gate cannot pass a PR.
240
+ function gateStateOf(state: string): PrGateState {
241
+ if (
242
+ state === "success" ||
243
+ state === "failure" ||
244
+ state === "error" ||
245
+ state === "pending"
246
+ ) {
247
+ return state;
248
+ }
249
+ return "error";
250
+ }
251
+
252
+ // The summary is the newest comment carrying the report marker (findMarkedCommentId picks
253
+ // the newest). Its head and completeness come from the marker; its reviews count and its
254
+ // live rows come from the state block that the summary carries.
255
+ function readSummary(comments: PrStatusComment[]): {
256
+ head: string | null;
257
+ complete: boolean | null;
258
+ reviews: number | null;
259
+ rows: StateFinding[];
260
+ } {
261
+ const id = findMarkedCommentId(comments);
262
+ const body = comments.find((c) => c.id === id)?.body ?? null;
263
+ if (id === null || body === null) {
264
+ return { head: null, complete: null, reviews: null, rows: [] };
265
+ }
266
+ const marker = parsePrCommentMarker(body);
267
+ const state = parseStateBlock(body);
268
+ return {
269
+ head: marker?.head ?? null,
270
+ complete: marker?.complete ?? null,
271
+ reviews: state?.reviews ?? null,
272
+ rows: state?.findings ?? [],
273
+ };
274
+ }
275
+
276
+ // The newest outcome comment's code. Only the newest is read: an older run's code says
277
+ // nothing about the run the gate now reflects. Null when the newest has no catalogued code.
278
+ function outcomeCodeOf(comments: PrStatusComment[]): string | null {
279
+ let newest: PrStatusComment | null = null;
280
+ for (const comment of comments) {
281
+ if (matchMarkerPrefix(comment.body, OUTCOME_MARKER_PREFIX) === undefined) {
282
+ continue;
283
+ }
284
+ if (newest === null || comment.id > newest.id) newest = comment;
285
+ }
286
+ return newest === null ? null : parseOutcomeCode(newest.body);
287
+ }
288
+
289
+ // Every posted finding, from both streams, in comment order. Only a comment that parses
290
+ // as a finding marker is a finding, so the summary and a human's reply drop out here.
291
+ function readFindings(
292
+ input: PrStatusInput,
293
+ rows: StateFinding[],
294
+ ): AnalyzedFinding[] {
295
+ const sources: {
296
+ id: number;
297
+ body: string;
298
+ channel: "inline" | "outside";
299
+ // A review comment's live path and line, as pr/discovery.ts reads them. An outside comment
300
+ // has no live projection, so its marker's anchor is the only one.
301
+ livePath?: string;
302
+ liveLine?: number;
303
+ }[] = [
304
+ ...input.reviewComments.map((c) => ({
305
+ id: c.id,
306
+ body: c.body,
307
+ channel: "inline" as const,
308
+ livePath: c.path,
309
+ liveLine: c.line ?? undefined,
310
+ })),
311
+ ...input.issueComments.map((c) => ({
312
+ id: c.id,
313
+ body: c.body,
314
+ channel: "outside" as const,
315
+ })),
316
+ ].sort((a, b) => a.id - b.id);
317
+ const posted: {
318
+ source: (typeof sources)[number];
319
+ marker: ParsedFindingMarker;
320
+ }[] = [];
321
+ for (const source of sources) {
322
+ const marker = parseFindingMarker(source.body);
323
+ if (marker !== null) posted.push({ source, marker });
324
+ }
325
+ // The join is the engine's own prior-to-comment binding (bindPriorsToPosted, rereview/
326
+ // prepare.ts): a row binds a posted finding only when the comment's anchor lands within one
327
+ // of the row's locs on the same path, and the claim fingerprint `c` breaks a tie and nothing
328
+ // else. Keying on `c` alone let two findings with the same claim on different paths share one
329
+ // row. The binding is one-to-one, so it runs once over every posted finding.
330
+ const bound = bindPriorsToPosted(
331
+ rows,
332
+ posted.map(({ source, marker }) => ({
333
+ id: source.id,
334
+ marker,
335
+ livePath: source.livePath,
336
+ liveLine: source.liveLine,
337
+ })),
338
+ );
339
+ const rowByComment = new Map<number, StateFinding>();
340
+ for (const row of rows) {
341
+ const comment = bound.get(row.id);
342
+ if (comment !== undefined) rowByComment.set(comment.id, row);
343
+ }
344
+ const out: AnalyzedFinding[] = [];
345
+ for (const { source, marker } of posted) {
346
+ const row = rowByComment.get(source.id);
347
+ const badge = parseFindingCommentBadge(source.body);
348
+ const replies = repliesTo(source, input);
349
+ const newest = newestReply(replies);
350
+ const triage = newest?.marker ?? null;
351
+ // The badge's tier, else the row's, else the engine's unrecoverable sentinel: the same fallback
352
+ // priorsFromPostedMarkers (rereview/prepare.ts) applies to a comment it cannot read.
353
+ const tier: RecoveredTier = badge?.tier ?? row?.tier ?? TIER_UNRECOVERABLE;
354
+ out.push({
355
+ finding: {
356
+ id: row?.id ?? null,
357
+ path: marker.path,
358
+ line: marker.line,
359
+ head: marker.headSha,
360
+ comment_id: source.id,
361
+ channel: source.channel,
362
+ severity:
363
+ badge?.sev ??
364
+ (row !== undefined && row.sev !== SEVERITY_UNRECOVERABLE
365
+ ? row.sev
366
+ : null),
367
+ tier,
368
+ // Fail closed: only an advisory tier clears a finding, so an unrecoverable one blocks.
369
+ blocking: tier !== "advisory",
370
+ // A finding is live when the summary's state block still carries its row, or when
371
+ // it was posted on the head being read. A row is the gate's own memory of a live
372
+ // finding, so a carried finding from an older head stays live.
373
+ live_on_head: row !== undefined || marker.headSha === input.head,
374
+ triage:
375
+ triage === null
376
+ ? null
377
+ : {
378
+ tag: triage.tag,
379
+ verdict: triage.verdict ?? null,
380
+ actor: triage.actor,
381
+ head: triage.headSha,
382
+ },
383
+ ci_verdict:
384
+ source.channel === "inline"
385
+ ? ciVerdictOf(source.id, newest?.id ?? null, input)
386
+ : null,
387
+ },
388
+ // A reply at another head does not answer the finding, so only a reply carrying the
389
+ // posted head counts.
390
+ triagedAtPostedHead: replies.some(
391
+ (r) => r.marker.headSha === marker.headSha,
392
+ ),
393
+ });
394
+ }
395
+ // A live row no comment bound to (summary-only, or never posted inline) is still a finding the
396
+ // gate counts. Without this row the status could not see a blocking finding the gate names.
397
+ for (const row of rows) {
398
+ if (bound.has(row.id)) continue;
399
+ out.push({ finding: unthreadedFinding(row), triagedAtPostedHead: false });
400
+ }
401
+ return out;
402
+ }
403
+
404
+ // A state row with no comment: its anchor is the first of its locs (path:line or path:a-b, the
405
+ // start line), read with the same parser the binder uses. A row with no parseable loc keeps its raw
406
+ // text and line 0, so it still blocks and still reports.
407
+ function unthreadedFinding(row: StateFinding): PrStatusFinding {
408
+ const first = row.locs[0];
409
+ const span = first === undefined ? undefined : parseProofRefLocs(first)[0];
410
+ // Fail closed: only an advisory tier clears a row, as for a comment.
411
+ const tier: RecoveredTier = row.tier;
412
+ return {
413
+ id: row.id,
414
+ path: span?.path ?? first ?? "",
415
+ line: span?.span.start ?? 0,
416
+ head: null,
417
+ comment_id: null,
418
+ channel: row.channel,
419
+ severity: row.sev !== SEVERITY_UNRECOVERABLE ? row.sev : null,
420
+ tier,
421
+ blocking: tier !== "advisory",
422
+ live_on_head: true,
423
+ triage: null,
424
+ ci_verdict: null,
425
+ };
426
+ }
427
+
428
+ // The CI adjudicator's ruling on the newest triage reply of one inline finding. Only a reply
429
+ // github-actions[bot] posted, on the head under review, that names this newest reply counts; the
430
+ // highest comment id wins. Null when there is no such ruling.
431
+ function ciVerdictOf(
432
+ threadId: number,
433
+ newestTriageId: number | null,
434
+ input: PrStatusInput,
435
+ ): "upheld" | "rejected" | "inconclusive" | null {
436
+ if (newestTriageId === null) return null;
437
+ let best: {
438
+ id: number;
439
+ verdict: "upheld" | "rejected" | "inconclusive";
440
+ } | null = null;
441
+ for (const comment of input.reviewComments) {
442
+ if (comment.in_reply_to_id !== threadId) continue;
443
+ if (comment.user !== CI_ADJUDICATOR_LOGIN) continue;
444
+ const marker = parseAdjudicationMarker(comment.body);
445
+ if (marker === null) continue;
446
+ if (marker.reply !== newestTriageId || marker.head !== input.head) continue;
447
+ if (best === null || comment.id > best.id) {
448
+ best = { id: comment.id, verdict: marker.verdict };
449
+ }
450
+ }
451
+ return best?.verdict ?? null;
452
+ }
453
+
454
+ interface TriageReplyRow {
455
+ id: number;
456
+ marker: ParsedTriageMarker;
457
+ }
458
+
459
+ // Every triage reply that answers one posted finding. An inline finding's replies sit in its
460
+ // review thread (in_reply_to_id). An outside-channel finding has no thread: its replies are
461
+ // top-level issue comments, and the link triage/reply writes binds one to its finding
462
+ // (`In reply to: <url>#issuecomment-<id>`). A comment that does not parse as a triage marker
463
+ // is not a reply, whatever it links.
464
+ function repliesTo(
465
+ source: { id: number; channel: "inline" | "outside" },
466
+ input: PrStatusInput,
467
+ ): TriageReplyRow[] {
468
+ const out: TriageReplyRow[] = [];
469
+ if (source.channel === "inline") {
470
+ for (const reply of input.reviewComments) {
471
+ if (reply.in_reply_to_id !== source.id) continue;
472
+ const marker = parseTriageMarker(reply.body);
473
+ if (marker !== null) out.push({ id: reply.id, marker });
474
+ }
475
+ return out;
476
+ }
477
+ const link = new RegExp(`#issuecomment-${source.id}(?!\\d)`);
478
+ for (const comment of input.issueComments) {
479
+ const marker = parseTriageMarker(comment.body);
480
+ if (marker === null || !link.test(comment.body)) continue;
481
+ out.push({ id: comment.id, marker });
482
+ }
483
+ return out;
484
+ }
485
+
486
+ // The newest reply by comment id (the API's own order is not relied on).
487
+ function newestReply(
488
+ replies: readonly TriageReplyRow[],
489
+ ): TriageReplyRow | null {
490
+ let newest: TriageReplyRow | null = null;
491
+ for (const reply of replies) {
492
+ if (newest === null || reply.id > newest.id) newest = reply;
493
+ }
494
+ return newest;
495
+ }
496
+
497
+ // The red partial-review verdict (preflight.ts partialReviewDescription) and its carried form
498
+ // (admission.ts carriedPartialDescription). The prefix is what this reader needs: the full text is
499
+ // written by those builders, and the gate's own state already says the review is red.
500
+ const PARTIAL_REVIEW_GATE =
501
+ /^crysk: review partial \(\d+ of \d+ hunters failed(?:, set [0-9a-f]{6})?\)/;
502
+
503
+ // W3-PARTIAL F004: the repeat form, the same N of M failure set as the head's previous partial verdict.
504
+ // A retry already failed that way, so the unlock is a route fix or an override, never review again.
505
+ const PARTIAL_REVIEW_AGAIN_GATE =
506
+ /^crysk: review partial again \(\d+ of \d+ hunters failed(?:, set [0-9a-f]{6})?\)/;
507
+
508
+ // unresolved_blocking has two sources, and the description wins when it has one. A red
509
+ // gate's description names the count its own rule counted (rereview/state.ts
510
+ // unresolvedBlockingCount), which can include a carried row that no comment in this read
511
+ // shows. Every other state has no such count, so it is the live blocking findings this read
512
+ // can see. Nothing else is counted.
513
+ function unresolvedBlocking(
514
+ gate: PrStatusGate,
515
+ liveBlockingCount: number,
516
+ ): number {
517
+ return gateBlockingCount(gate) ?? liveBlockingCount;
518
+ }
519
+
520
+ // The count a red gate's own description names, or null when it names none.
521
+ function gateBlockingCount(gate: PrStatusGate): number | null {
522
+ if (gate.state !== "failure" || gate.description === null) return null;
523
+ const counted = /^crysk: (\d+) blocking finding\(s\)/.exec(
524
+ canonicalStatusText(gate.description),
525
+ );
526
+ return counted?.[1] === undefined ? null : Number(counted[1]);
527
+ }
528
+
529
+ // The gate descriptions that name no review of this head: the automatic budget is used up, or the
530
+ // policy is manual (src/pr/admission.ts). Their text is the gate's own, so no count is read.
531
+ const REVIEW_TRIGGER_GATE =
532
+ /^crysk: review (budget for this PR is used up|needs a manual trigger)/;
533
+
534
+ // A blocking finding whose newest reply is a dismissal the CI adjudicator upheld on this head.
535
+ function upheldDismissal(a: AnalyzedFinding): boolean {
536
+ return (
537
+ a.finding.triage?.tag === "dismissed" && a.finding.ci_verdict === "upheld"
538
+ );
539
+ }
540
+
541
+ // The next action, first match wins. The branches are in the order the skill must act on
542
+ // them, and each carries its own reason sentence so the human reads the same decision the
543
+ // skill made.
544
+ function decideNext(
545
+ gate: PrStatusGate,
546
+ analyzed: AnalyzedFinding[],
547
+ head: string,
548
+ summary: { head: string | null; complete: boolean | null },
549
+ ): { next_action: PrNextAction; reason: string } {
550
+ const liveBlocking = analyzed.filter(
551
+ (a) => a.finding.blocking && a.finding.live_on_head,
552
+ );
553
+ if (gate.state === "pending") {
554
+ if (gate.pending_in_flight) {
555
+ return {
556
+ next_action: "wait",
557
+ reason: `A crysk review is in flight on this head (pending since ${gate.created_at}); wait for it to finish.`,
558
+ };
559
+ }
560
+ // A pending past the in-flight window is not held by any run. The watcher's own rule
561
+ // lets the next tick start a review then, so the skill starts one too.
562
+ return {
563
+ next_action: "review",
564
+ reason: `The pending crysk status on this head is older than the ${IN_FLIGHT_WINDOW_MINUTES}-minute in-flight window and no run holds it; start a review.`,
565
+ };
566
+ }
567
+ if (gate.state === "none") {
568
+ return {
569
+ next_action: "review",
570
+ reason:
571
+ "No crysk gate status is on this head, so no review has run here yet; start one.",
572
+ };
573
+ }
574
+ if (gate.state === "error") {
575
+ return {
576
+ next_action: "review",
577
+ reason: `The last crysk review on this head did not finish (${gate.description ?? "no description"}); start a review.`,
578
+ };
579
+ }
580
+ if (gate.state === "success") {
581
+ // An override is a human's unlock of the head, so it stays done whatever the findings say.
582
+ if (gate.override.active) {
583
+ return {
584
+ next_action: "done",
585
+ reason: `The gate is green through an override by @${gate.override.by ?? "an unknown login"}; nothing is owed on this head.`,
586
+ };
587
+ }
588
+ // A partial review of this head (some reviewers failed) is not a finished review, and the
589
+ // engine's own admission runs the same head again (partialSameHead, ci/review-admission.ts).
590
+ // A green gate over it is therefore review. The same marker on an older head is history.
591
+ if (summary.head === head && summary.complete === false) {
592
+ return {
593
+ next_action: "review",
594
+ reason: `The last review of this head was partial (some reviewers failed); run ${commandHint(REVIEW_COMMAND, "")}.`,
595
+ };
596
+ }
597
+ // The triage skill answers every finding, advisory included, so a green gate is not the
598
+ // end while a finding posted on this head has no reply at that head. A finding posted on an
599
+ // older head is not this head's to answer.
600
+ const owed = analyzed.filter(
601
+ (a) => a.finding.head === head && !a.triagedAtPostedHead,
602
+ );
603
+ if (owed.length > 0) {
604
+ return {
605
+ next_action: "triage",
606
+ reason: `The gate is green, but ${owed.length} finding(s) on this head have no reply yet; triage them.`,
607
+ };
608
+ }
609
+ return {
610
+ next_action: "done",
611
+ reason:
612
+ "The review on this head passed the gate, and every finding on this head has a reply.",
613
+ };
614
+ }
615
+ // failure: the blocking findings on this head decide between triage, fix, and override.
616
+ // G12, first: the gate is red because no review may run on this head (the automatic budget is
617
+ // used up, or the policy is manual_only). No push gets an automatic review there, so a fix cannot
618
+ // clear anything; the review the human asks for is the only unlock, whatever the summary's rows
619
+ // (from an older head) say (review #348 R006).
620
+ if (REVIEW_TRIGGER_GATE.test(canonicalStatusText(gate.description ?? ""))) {
621
+ return {
622
+ next_action: "review",
623
+ reason: `The automatic review budget for this PR is used up, or the review policy is manual (ci_review_policy is manual_only); comment ${commandHint(REVIEW_COMMAND, "")} to review this head.`,
624
+ };
625
+ }
626
+ // A row with no comment has no thread a triage reply could land in, so it is never "untriaged".
627
+ const untriaged = liveBlocking.filter(
628
+ (a) => a.finding.comment_id !== null && !a.triagedAtPostedHead,
629
+ );
630
+ if (untriaged.length > 0) {
631
+ return {
632
+ next_action: "triage",
633
+ reason: `${untriaged.length} live blocking finding(s) on this head have no triage reply at the head they were posted on; triage them first.`,
634
+ };
635
+ }
636
+ // WHY this precedes the upheld-dismissal review branch: a blocking row with no thread cannot be
637
+ // answered, and only a fix clears it. Answering review here would re-run a gate that still counts
638
+ // it, and the review never stops counting it (review #348 R001/R003).
639
+ // Only a gate that states a blocking count is the verdict of a review the summary's rows belong
640
+ // to. A skipped or partial gate reviewed nothing, and its rows may be an older head's, so it falls
641
+ // through to its own branches below (review #348 R006).
642
+ const unthreaded = liveBlocking.filter((a) => a.finding.comment_id === null);
643
+ if (unthreaded.length > 0 && gateBlockingCount(gate) !== null) {
644
+ const ids = unthreaded.map((a) => a.finding.id ?? "unnamed").join(", ");
645
+ return {
646
+ next_action: "fix",
647
+ reason: `${unthreaded.length} live blocking finding(s) (${ids}) have no comment thread to answer (summary-only or never posted inline); fix them and push, or ${commandHint(OVERRIDE_COMMAND, "")}.`,
648
+ };
649
+ }
650
+ const applied = liveBlocking.filter(
651
+ (a) => a.finding.triage?.tag === "applied",
652
+ );
653
+ if (applied.length > 0) {
654
+ return {
655
+ next_action: "fix",
656
+ reason:
657
+ "A live blocking finding is tagged applied; push the fix, and the next review verifies it.",
658
+ };
659
+ }
660
+ // A dismissal leaves the count only on a CI ruling of "upheld" (src/triage/adjudication.ts). A
661
+ // dismissal with no ruling on this head, or an inconclusive one, is pending: recheck rules on it.
662
+ const dismissed = liveBlocking.filter(
663
+ (a) => a.finding.triage?.tag === "dismissed",
664
+ );
665
+ // Only an inline finding can get a CI ruling: the adjudicator answers on the finding's own review
666
+ // thread, and an outside-diff finding has none, so recheck would never settle it.
667
+ const pendingRulings = dismissed.filter(
668
+ (a) =>
669
+ a.finding.channel === "inline" &&
670
+ (a.finding.ci_verdict === null ||
671
+ a.finding.ci_verdict === "inconclusive"),
672
+ );
673
+ if (pendingRulings.length > 0) {
674
+ return {
675
+ next_action: "recheck",
676
+ reason: `${pendingRulings.length} live blocking finding(s) are answered dismissed with no CI ruling on this head (or an inconclusive one); comment ${commandHint(RECHECK_COMMAND, "")} so the CI adjudicator rules on them.`,
677
+ };
678
+ }
679
+ // WHY the gate's own count is checked too (review #348 F002): the gate can count a blocking
680
+ // finding this read cannot see (one rendered only in the summary). A review that already
681
+ // consumed the rulings leaves that one counted, and answering review again would loop.
682
+ const gateCount = gateBlockingCount(gate);
683
+ if (
684
+ dismissed.length > 0 &&
685
+ dismissed.length === liveBlocking.length &&
686
+ dismissed.every(upheldDismissal) &&
687
+ (gateCount === null || gateCount <= dismissed.length)
688
+ ) {
689
+ return {
690
+ next_action: "review",
691
+ reason: `The CI adjudicator upheld every dismissal on this head; run ${commandHint(REVIEW_COMMAND, "")}, and that review no longer counts them.`,
692
+ };
693
+ }
694
+ if (
695
+ canonicalStatusText(gate.description ?? "").startsWith(
696
+ "crysk skipped the review",
697
+ )
698
+ ) {
699
+ return {
700
+ next_action: "override-or-fix",
701
+ reason: `The review on this head was skipped (${gate.description}); run ${commandHint(REVIEW_COMMAND, "")} to review it anyway, or override the gate.`,
702
+ };
703
+ }
704
+ // S6: a red gate that is a partial review (its own text, or the carried form) is not a finding to fix.
705
+ // Nothing blocking was found, so the retry is the unlock. Its text is the gate's own, so no count is read.
706
+ if (
707
+ PARTIAL_REVIEW_AGAIN_GATE.test(canonicalStatusText(gate.description ?? ""))
708
+ ) {
709
+ return {
710
+ next_action: "override-or-fix",
711
+ reason: `The review is partial again with the same hunters failing: a retry already failed the same way; fix the failing hunter's route (model or provider) or run ${commandHint(OVERRIDE_COMMAND, "")}.`,
712
+ };
713
+ }
714
+ if (PARTIAL_REVIEW_GATE.test(canonicalStatusText(gate.description ?? ""))) {
715
+ return {
716
+ next_action: "review",
717
+ reason: `The last review of this head was partial (some reviewers failed); run ${commandHint(REVIEW_COMMAND, "")}, which retries it. If the same reviewers fail every time (an unavailable model, a broken route), fix that route or override the gate.`,
718
+ };
719
+ }
720
+ if (liveBlocking.length > 0) {
721
+ return {
722
+ next_action: "override-or-fix",
723
+ reason: `The gate is red with ${liveBlocking.length} live blocking finding(s), all triaged and none applied or pending a CI ruling (a rejected dismissal, or a deferred or misclassified tag, never leaves the count): fix and push, or override the gate.`,
724
+ };
725
+ }
726
+ return {
727
+ next_action: "override-or-fix",
728
+ reason: `The gate is red and names no live blocking finding on this head (${gate.description ?? "no description"}); read the gate description.`,
729
+ };
730
+ }
731
+
732
+ // Pure predicate for `pr-hero status --pr <n> --wait`: whether the caller should keep polling
733
+ // for a review to start or finish before emitting the settled status.
734
+ export function shouldWaitPrStatus(input: {
735
+ status: PrStatusJson;
736
+ issueComments: PrStatusComment[];
737
+ nowMs: number;
738
+ elapsedMs: number;
739
+ startupGraceMs?: number;
740
+ }): boolean {
741
+ const { status, issueComments, nowMs, elapsedMs } = input;
742
+ const startupGraceMs = input.startupGraceMs ?? STATUS_WAIT_STARTUP_GRACE_MS;
743
+
744
+ if (status.next_action === "wait") return true;
745
+
746
+ // Fresh push: no status on this head yet, within the initial startup grace window.
747
+ if (status.gate.state === "none" && elapsedMs < startupGraceMs) {
748
+ return true;
749
+ }
750
+
751
+ // Fresh `/crysk review` or `/crysk recheck` comment: posted within the startup grace
752
+ // window, and no pr-hero commit status has been posted since that comment.
753
+ const gateMs =
754
+ status.gate.created_at !== null
755
+ ? Date.parse(status.gate.created_at)
756
+ : Number.NEGATIVE_INFINITY;
757
+
758
+ for (const comment of issueComments) {
759
+ if (!TRIGGER_COMMANDS.some((command) => isCommand(comment.body, command))) {
760
+ continue;
761
+ }
762
+ if (comment.created_at === undefined) continue;
763
+ const commentMs = Date.parse(comment.created_at);
764
+ if (!Number.isFinite(commentMs)) continue;
765
+ const ageMs = nowMs - commentMs;
766
+ if (ageMs >= 0 && ageMs < startupGraceMs && commentMs > gateMs) {
767
+ return true;
768
+ }
769
+ }
770
+
771
+ return false;
772
+ }
773
+
774
+ // The human lines `pr-hero status` prints on stderr without --json. A renderer: it returns
775
+ // lines, never writes them, and carries no escape bytes (the caller styles nothing here).
776
+ export function renderPrStatusLines(status: PrStatusJson): string[] {
777
+ const gate =
778
+ status.gate.description === null
779
+ ? status.gate.state
780
+ : `${status.gate.state} (${status.gate.description})`;
781
+ return [
782
+ `crysk status: PR #${status.pr} at ${status.head.slice(0, 7)}`,
783
+ `gate: ${gate}`,
784
+ `findings: ${status.findings.length} posted, ${status.unresolved_blocking} unresolved blocking`,
785
+ `next: ${status.next_action} — ${status.reason}`,
786
+ ];
787
+ }