crysk 0.3.0 → 0.3.1

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.
@@ -0,0 +1,169 @@
1
+ import { liveBlockingFindings } from "#pr/pr-status";
2
+ import { parseFindingMarker } from "#pr/preflight";
3
+ import { TIER_UNRECOVERABLE } from "#rereview/classify";
4
+ import { CI_ADJUDICATOR_LOGIN } from "#triage/adjudication";
5
+ import { parseTriageMarker } from "#triage/triage";
6
+ import { parsePrCommentMarker } from "#watch/preflight";
7
+
8
+ // budget-exhausted-carry S3-S5: whether every blocking finding of the reviewed head is answered, read when a
9
+ // skip carries a blocking red and no review is left to verify the fixes. Pure: the impure half (who may write
10
+ // to the repository) is the gate's, which passes its lookups in as `writers`.
11
+
12
+ // The blocking findings the read could see, and how many of them are still open. A finding is answered when
13
+ // its newest triage reply sits at the head it was posted on (the rule crysk status uses) and is `applied`, or
14
+ // is a `dismissed` the CI adjudicator upheld. `deferred` and `misclassified` stay open: they name a real
15
+ // finding, and only a fix or a human override may close one.
16
+ export interface AnsweredFindings {
17
+ blocking: number;
18
+ open: number;
19
+ }
20
+
21
+ export interface AnswerIssueComment {
22
+ id: number;
23
+ user: string;
24
+ body: string;
25
+ created_at?: string;
26
+ }
27
+
28
+ export interface AnswerReviewComment extends AnswerIssueComment {
29
+ in_reply_to_id: number | null;
30
+ path?: string;
31
+ line?: number | null;
32
+ }
33
+
34
+ function isTriageReply(body: string): boolean {
35
+ return parseTriageMarker(body) !== null;
36
+ }
37
+
38
+ // The logins whose permission the gate must look up: every author of a triage reply.
39
+ export function triageReplyAuthors(
40
+ comments: readonly AnswerIssueComment[],
41
+ ): string[] {
42
+ const authors = new Set<string>();
43
+ for (const comment of comments) {
44
+ if (isTriageReply(comment.body)) authors.add(comment.user);
45
+ }
46
+ return [...authors];
47
+ }
48
+
49
+ // WHY the CI bot is trusted for a finding or a summary: Crysk posts both as github-actions[bot], which only a
50
+ // workflow of this repository can write as. `trustedActors` (resolveCiTrustedActors) adds the configured
51
+ // writers; when it is undefined no author is known, and every author passes, as scanPostedFindingTiers does.
52
+ function trustedPoster(
53
+ user: string,
54
+ trustedActors: ReadonlySet<string> | undefined,
55
+ ): boolean {
56
+ if (trustedActors === undefined) return true;
57
+ return user === CI_ADJUDICATOR_LOGIN || trustedActors.has(user);
58
+ }
59
+
60
+ function isGateInput(body: string): boolean {
61
+ return (
62
+ parseFindingMarker(body) !== null || parsePrCommentMarker(body) !== null
63
+ );
64
+ }
65
+
66
+ // WHY a reply to a finding from an older head must be newer than the carried verdict: such a finding is live
67
+ // on the reviewed head through the summary's rows, so the review that wrote the verdict saw its reply and kept
68
+ // it. A reply from before that verdict is the claim the review already contradicted. A reply carries the
69
+ // head its finding was posted on (triage/reply.ts), so its time is the only thing that says it came after.
70
+ // A reply at the reviewed head itself always answers the review of that head.
71
+ function isStaleReply(
72
+ comment: AnswerIssueComment,
73
+ reviewedHead: string,
74
+ verdictAt: number | null,
75
+ ): boolean {
76
+ const marker = parseTriageMarker(comment.body);
77
+ if (marker === null || marker.headSha === reviewedHead) return false;
78
+ const createdAt =
79
+ comment.created_at === undefined
80
+ ? Number.NaN
81
+ : Date.parse(comment.created_at);
82
+ return (
83
+ verdictAt === null || Number.isNaN(createdAt) || createdAt <= verdictAt
84
+ );
85
+ }
86
+
87
+ // The comments the read may trust, or null when one cannot be judged (S5: fail closed).
88
+ // WHY a triage reply needs write access (S4): it is the only thing that turns a red green here, and anyone
89
+ // can comment on a pull request. A reply from a login with no write access is dropped; a login whose
90
+ // permission did not read (null, or never looked up) fails the whole read closed.
91
+ function trustedComments<T extends AnswerIssueComment>(
92
+ comments: readonly T[],
93
+ writers: ReadonlyMap<string, boolean | null>,
94
+ trustedActors: ReadonlySet<string> | undefined,
95
+ reviewedHead: string,
96
+ verdictAt: number | null,
97
+ ): T[] | null {
98
+ const kept: T[] = [];
99
+ for (const comment of comments) {
100
+ if (isTriageReply(comment.body)) {
101
+ if (isStaleReply(comment, reviewedHead, verdictAt)) continue;
102
+ const writer = writers.get(comment.user);
103
+ if (writer === undefined || writer === null) return null;
104
+ if (writer) kept.push(comment);
105
+ continue;
106
+ }
107
+ if (
108
+ isGateInput(comment.body) &&
109
+ !trustedPoster(comment.user, trustedActors)
110
+ )
111
+ continue;
112
+ kept.push(comment);
113
+ }
114
+ return kept;
115
+ }
116
+
117
+ export function answeredBlockingFindings(input: {
118
+ pr: number;
119
+ reviewedHead: string;
120
+ issueComments: readonly AnswerIssueComment[];
121
+ reviewComments: readonly AnswerReviewComment[];
122
+ trustedActors: ReadonlySet<string> | undefined;
123
+ writers: ReadonlyMap<string, boolean | null>;
124
+ // When the carried verdict was written (its commit status's created_at), or null when unknown.
125
+ verdictAt: string | null;
126
+ }): AnsweredFindings | null {
127
+ const parsedAt =
128
+ input.verdictAt === null ? Number.NaN : Date.parse(input.verdictAt);
129
+ const verdictAt = Number.isNaN(parsedAt) ? null : parsedAt;
130
+ const issueComments = trustedComments(
131
+ input.issueComments,
132
+ input.writers,
133
+ input.trustedActors,
134
+ input.reviewedHead,
135
+ verdictAt,
136
+ );
137
+ const reviewComments = trustedComments(
138
+ input.reviewComments,
139
+ input.writers,
140
+ input.trustedActors,
141
+ input.reviewedHead,
142
+ verdictAt,
143
+ );
144
+ if (issueComments === null || reviewComments === null) return null;
145
+ const findings = liveBlockingFindings({
146
+ pr: input.pr,
147
+ head: input.reviewedHead,
148
+ nowMs: 0,
149
+ statuses: [],
150
+ issueComments,
151
+ reviewComments,
152
+ });
153
+ // S5: a finding whose tier could not be read is blocking only by fail-closed default; it cannot be
154
+ // the evidence that unlocks the gate.
155
+ if (findings.some((finding) => finding.tier === TIER_UNRECOVERABLE))
156
+ return null;
157
+ let open = 0;
158
+ for (const finding of findings) {
159
+ const triage = finding.triage;
160
+ const answered =
161
+ triage !== null &&
162
+ finding.head !== null &&
163
+ triage.head === finding.head &&
164
+ (triage.tag === "applied" ||
165
+ (triage.tag === "dismissed" && finding.ci_verdict === "upheld"));
166
+ if (!answered) open++;
167
+ }
168
+ return { blocking: findings.length, open };
169
+ }
@@ -56,6 +56,11 @@ import {
56
56
  settleCiAdmissionLedger,
57
57
  settleCiGateSkip,
58
58
  } from "#pr/admission";
59
+ import {
60
+ type AnsweredFindings,
61
+ answeredBlockingFindings,
62
+ triageReplyAuthors,
63
+ } from "#pr/answered-findings";
59
64
  import {
60
65
  CommentsTruncatedError,
61
66
  fetchCommitStatuses,
@@ -174,6 +179,16 @@ export async function evaluateCiAdmissionGate(input: {
174
179
  extra: config.ci_trusted_actors,
175
180
  });
176
181
  const allComments = [...reviewComments, ...issueComments];
182
+ const readAnswered = answeredReader({
183
+ operatorRoot,
184
+ prNumber,
185
+ reviewedHead: authorityFailOpen ? null : summaryHead,
186
+ issueComments,
187
+ reviewComments,
188
+ trustedActors,
189
+ spawnFn,
190
+ sleepFn: input.sleepFn ?? Bun.sleep,
191
+ });
177
192
  const postedFindings =
178
193
  summaryHead === null
179
194
  ? null
@@ -352,6 +367,7 @@ export async function evaluateCiAdmissionGate(input: {
352
367
  reason: admissionVerdict.reason,
353
368
  reviewedHead: summaryHead,
354
369
  priorBlocking: admissionVerdict.prior.blocking,
370
+ readAnswered,
355
371
  },
356
372
  });
357
373
  return { ciPolicy, ciPolicyHash, ledgerRecords, exitCode };
@@ -370,7 +386,8 @@ export async function evaluateCiAdmissionGate(input: {
370
386
  blockingCount: admissionVerdict.prior.blocking,
371
387
  advisoryCount: admissionVerdict.prior.advisory,
372
388
  });
373
- // S4: manual-required is a failure in gate mode: its code was never reviewed.
389
+ // S2: manual-required carries the last verdict like every other skip (publishCiSkip,
390
+ // admissionSkipDecision); a carried red with every blocking finding answered turns green, unverified.
374
391
  const plan = planCiReviewManualRequired({
375
392
  prNumber,
376
393
  verdict: admissionVerdict,
@@ -384,7 +401,7 @@ export async function evaluateCiAdmissionGate(input: {
384
401
  isCi,
385
402
  stepSummaryFlag: options.stepSummary,
386
403
  plan,
387
- noticeMessage: `crysk review requires manual override — ${manualReason}`,
404
+ noticeMessage: `crysk automatic reviews are done for this PR — ${manualReason}`,
388
405
  ciMode: input.ciMode,
389
406
  headSha,
390
407
  spawnFn,
@@ -392,6 +409,7 @@ export async function evaluateCiAdmissionGate(input: {
392
409
  reason: admissionVerdict.reason,
393
410
  reviewedHead: summaryHead,
394
411
  priorBlocking: admissionVerdict.prior.blocking,
412
+ readAnswered,
395
413
  },
396
414
  });
397
415
  return { ciPolicy, ciPolicyHash, ledgerRecords, exitCode };
@@ -641,6 +659,66 @@ async function stoppedByMaintainer(
641
659
  return false;
642
660
  }
643
661
 
662
+ // budget-exhausted-carry S3, S4: the reader publishCiSkip calls when a skip carries a blocking red. Each
663
+ // triage reply's author is looked up once, with the stop lookup's bounds; a lookup that did not read is null,
664
+ // which answeredBlockingFindings fails closed on. A truncated comment read (authorityFailOpen) or no reviewed
665
+ // head reads nothing: the red stays.
666
+ function answeredReader(input: {
667
+ operatorRoot: string;
668
+ prNumber: number;
669
+ reviewedHead: string | null;
670
+ issueComments: readonly {
671
+ id: number;
672
+ user: string;
673
+ body: string;
674
+ created_at?: string;
675
+ }[];
676
+ reviewComments: readonly {
677
+ id: number;
678
+ user: string;
679
+ body: string;
680
+ created_at?: string;
681
+ in_reply_to_id: number | null;
682
+ path?: string;
683
+ line?: number | null;
684
+ }[];
685
+ trustedActors: ReadonlySet<string> | undefined;
686
+ spawnFn: typeof Bun.spawn | undefined;
687
+ sleepFn: (ms: number) => Promise<void>;
688
+ }): (verdictAt: string | null) => Promise<AnsweredFindings | null> {
689
+ return async (verdictAt) => {
690
+ const reviewedHead = input.reviewedHead;
691
+ if (reviewedHead === null) return null;
692
+ const writers = new Map<string, boolean | null>();
693
+ for (const login of triageReplyAuthors([
694
+ ...input.reviewComments,
695
+ ...input.issueComments,
696
+ ])) {
697
+ const permission = await stopAuthorPermission(
698
+ input.operatorRoot,
699
+ login,
700
+ input.spawnFn,
701
+ input.sleepFn,
702
+ );
703
+ writers.set(
704
+ login,
705
+ permission === null
706
+ ? null
707
+ : permission === "admin" || permission === "write",
708
+ );
709
+ }
710
+ return answeredBlockingFindings({
711
+ pr: input.prNumber,
712
+ reviewedHead,
713
+ issueComments: input.issueComments,
714
+ reviewComments: input.reviewComments,
715
+ trustedActors: input.trustedActors,
716
+ writers,
717
+ verdictAt,
718
+ });
719
+ };
720
+ }
721
+
644
722
  // Same bounds as the trigger's commenter lookup (lookupCommenterPermission):
645
723
  // one retry after a short wait, so a single API blip does not override a
646
724
  // maintainer's stop, and a timeout, so a hung gh call cannot stall admission.
@@ -249,6 +249,15 @@ function gateStateOf(state: string): PrGateState {
249
249
  return "error";
250
250
  }
251
251
 
252
+ // The live blocking findings at input.head, each with its newest triage reply and CI ruling, read exactly as
253
+ // buildPrStatus reads them. The admission gate reads them for the reviewed head (pr/answered-findings.ts).
254
+ export function liveBlockingFindings(input: PrStatusInput): PrStatusFinding[] {
255
+ const summary = readSummary(input.issueComments);
256
+ return readFindings(input, summary.rows)
257
+ .filter((a) => a.finding.blocking && a.finding.live_on_head)
258
+ .map((a) => a.finding);
259
+ }
260
+
252
261
  // The summary is the newest comment carrying the report marker (findMarkedCommentId picks
253
262
  // the newest). Its head and completeness come from the marker; its reviews count and its
254
263
  // live rows come from the state block that the summary carries.
@@ -527,10 +536,19 @@ function gateBlockingCount(gate: PrStatusGate): number | null {
527
536
  }
528
537
 
529
538
  // 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.
539
+ // policy is manual. A gate stopped writing them on 2026-10-30 (budget-exhausted-carry S2: both carry the
540
+ // last verdict now), so only a head statused before that still carries one. Their text is the gate's own,
541
+ // so no count is read.
531
542
  const REVIEW_TRIGGER_GATE =
532
543
  /^crysk: review (budget for this PR is used up|needs a manual trigger)/;
533
544
 
545
+ // A blocking red a skip carried from an earlier commit (admission.ts carriedBlockingDescription).
546
+ const CARRIED_BLOCKING_GATE = /^crysk: \d+ blocking finding\(s\) carried from /;
547
+
548
+ // The green a skip writes over answered findings no review verified (admission.ts unverifiedFixesDescription).
549
+ const UNVERIFIED_FIXES_GATE =
550
+ /^crysk: \d+ answered finding\(s\), fixes unverified \(/;
551
+
534
552
  // A blocking finding whose newest reply is a dismissal the CI adjudicator upheld on this head.
535
553
  function upheldDismissal(a: AnalyzedFinding): boolean {
536
554
  return (
@@ -606,6 +624,16 @@ function decideNext(
606
624
  reason: `The gate is green, but ${owed.length} finding(s) on this head have no reply yet; triage them.`,
607
625
  };
608
626
  }
627
+ // budget-exhausted-carry S6: a green no review verified. Nothing is owed, and the reason says why
628
+ // it is green, so the skill reports the fixes as unverified rather than reviewed.
629
+ if (
630
+ UNVERIFIED_FIXES_GATE.test(canonicalStatusText(gate.description ?? ""))
631
+ ) {
632
+ return {
633
+ next_action: "done",
634
+ reason: `The gate is green on answered findings no review verified (${gate.description}); the PR is ready to merge, or comment ${commandHint(REVIEW_COMMAND, "")} for another review.`,
635
+ };
636
+ }
609
637
  return {
610
638
  next_action: "done",
611
639
  reason:
@@ -651,6 +679,36 @@ function decideNext(
651
679
  (a) => a.finding.triage?.tag === "applied",
652
680
  );
653
681
  if (applied.length > 0) {
682
+ // budget-exhausted-carry S3: a carried blocking red means a review rule skipped this head. The next push
683
+ // may be skipped too, and then the gate reads the replies instead of a review, but only every blocking
684
+ // finding answered unlocks it (answered-findings.ts), so the reason promises that only when it holds.
685
+ // A carried partial red is never unlocked by replies, so it keeps the plain reason.
686
+ if (
687
+ CARRIED_BLOCKING_GATE.test(canonicalStatusText(gate.description ?? ""))
688
+ ) {
689
+ // The gate's own rules (admission.ts admissionSkipDecision, answered-findings.ts): a finding is
690
+ // answered only by its newest reply at the head it was posted on, and the gate must see at least as
691
+ // many blocking findings as the red counts. The stale-reply cutoff needs the carried verdict's time,
692
+ // which this read does not have, so the reason says the push can turn green, never that it will.
693
+ const open = liveBlocking.filter(
694
+ (a) =>
695
+ !(
696
+ (a.finding.triage?.tag === "applied" &&
697
+ a.finding.triage.head === a.finding.head) ||
698
+ upheldDismissal(a)
699
+ ),
700
+ ).length;
701
+ const counted = gateBlockingCount(gate);
702
+ const unseen =
703
+ counted === null ? 0 : Math.max(0, counted - liveBlocking.length);
704
+ return {
705
+ next_action: "fix",
706
+ reason:
707
+ open === 0 && unseen === 0
708
+ ? "Every live blocking finding is answered and a review rule skipped this head; push the fix. If a rule skips that push too, the gate reads the replies on it and can turn green, marked unverified; otherwise the review verifies it."
709
+ : `A live blocking finding is tagged applied, but ${open + unseen} blocking finding(s) are still open (deferred, misclassified, a dismissal not upheld, a reply at another head, or one this read cannot see), so replies alone will not unlock the carried red; push the fix, and answer or fix the rest.`,
710
+ };
711
+ }
654
712
  return {
655
713
  next_action: "fix",
656
714
  reason:
package/src/updater.ts CHANGED
@@ -54,7 +54,7 @@ export function detectInstallMethod(
54
54
  ): InstallMethod {
55
55
  const home = options.home ?? os.homedir();
56
56
  const execPath = options.execPath ?? process.execPath;
57
- const version = options.version ?? "0.3.0";
57
+ const version = options.version ?? "0.3.1";
58
58
  const exists = options.exists ?? existsSync;
59
59
 
60
60
  const isSource =