crysk 0.0.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.
- package/LICENSE +176 -0
- package/README.md +611 -3
- package/bin/pr-hero.js +166 -0
- package/config/models/anthropic.json +5 -0
- package/dist/SKILL-7e3725zn.md +496 -0
- package/dist/SKILL-7q35wcaz.md +66 -0
- package/dist/SKILL-9at22381.md +74 -0
- package/dist/SKILL-ex2hxxh3.md +134 -0
- package/dist/SKILL-y8rv8msh.md +134 -0
- package/dist/SKILL-ygka1bs0.md +515 -0
- package/dist/adjudicator-dtg2axnh.md +86 -0
- package/dist/admission-config.example-a9vqmmad.json +9 -0
- package/dist/ci-admission-camfbx2b.md +122 -0
- package/dist/ci-admission-qk9r9taq.md +124 -0
- package/dist/cli.js +42501 -0
- package/dist/consolidate-ac0mnfyj.md +24 -0
- package/dist/deep-review-lifecycle-m698ybjp.md +362 -0
- package/dist/deep-review-logic-a6b1gczf.md +156 -0
- package/dist/deep-review-parity-0ec904db.md +120 -0
- package/dist/deep-review-reliability-qyakbjb9.md +125 -0
- package/dist/deep-review-resilience-3tz8y26e.md +118 -0
- package/dist/index.js +20190 -0
- package/dist/opencode-ci-7b0be64p.md +141 -0
- package/dist/review-refuter-j4e9gezy.md +71 -0
- package/dist/scout-yvp153q7.md +140 -0
- package/dist/summarizer-zca44nxa.md +18 -0
- package/dist/workflow-w1g9szna.yml +151 -0
- package/package.json +81 -4
- package/prompts/consolidate.md +24 -0
- package/prompts/default/PROVENANCE.md +18 -0
- package/prompts/default/deep-review-lifecycle.md +362 -0
- package/prompts/default/deep-review-logic.md +156 -0
- package/prompts/default/deep-review-parity.md +120 -0
- package/prompts/default/deep-review-reliability.md +125 -0
- package/prompts/default/deep-review-resilience.md +118 -0
- package/prompts/default/review-refuter.md +71 -0
- package/prompts/scout.md +140 -0
- package/prompts/summarizer.md +18 -0
- package/skills/crysk/SKILL.md +74 -0
- package/skills/crysk-ci-setup/SKILL.md +134 -0
- package/skills/crysk-ci-setup/assets/admission-config.example.json +9 -0
- package/skills/crysk-ci-setup/assets/workflow.yml +151 -0
- package/skills/crysk-ci-setup/references/ci-admission.md +124 -0
- package/skills/crysk-ci-setup/references/opencode-ci.md +141 -0
- package/skills/crysk-loop/SKILL.md +515 -0
- package/skills/crysk-loop/adjudicator.md +86 -0
- package/skills/crysk-pr/SKILL.md +66 -0
- package/src/agent-env.ts +1084 -0
- package/src/asset-manifest.ts +83 -0
- package/src/assets.d.ts +6 -0
- package/src/assets.ts +183 -0
- package/src/brand-paths.ts +155 -0
- package/src/brand.ts +255 -0
- package/src/ci/admission-ledger.ts +350 -0
- package/src/ci/classify-error.ts +73 -0
- package/src/ci/cli-record.ts +271 -0
- package/src/ci/force-comment.ts +39 -0
- package/src/ci/gates.ts +945 -0
- package/src/ci/outcome.ts +2181 -0
- package/src/ci/override.ts +187 -0
- package/src/ci/publish.ts +1639 -0
- package/src/ci/reporter.ts +626 -0
- package/src/ci/review-admission.ts +910 -0
- package/src/ci/review-levels.ts +310 -0
- package/src/ci/review-policy.ts +18 -0
- package/src/ci/review-risk.ts +184 -0
- package/src/ci/setup.ts +844 -0
- package/src/ci/trigger.ts +1353 -0
- package/src/cli.ts +348 -0
- package/src/commands/activity.ts +67 -0
- package/src/commands/ci-setup-questions.ts +397 -0
- package/src/commands/ci-setup.ts +381 -0
- package/src/commands/config.ts +99 -0
- package/src/commands/doctor.ts +108 -0
- package/src/commands/init.ts +139 -0
- package/src/commands/mcp.ts +10 -0
- package/src/commands/menu.ts +129 -0
- package/src/commands/post.ts +302 -0
- package/src/commands/skills.ts +83 -0
- package/src/commands/status.ts +173 -0
- package/src/commands/triage.ts +342 -0
- package/src/commands/uninstall.ts +37 -0
- package/src/commands/upgrade.ts +214 -0
- package/src/commands/usage.ts +98 -0
- package/src/compare/compare.ts +28 -0
- package/src/compare/floor-test.ts +471 -0
- package/src/compare/martian-adapter.ts +80 -0
- package/src/config/config.ts +267 -0
- package/src/corpus/corpus.ts +1094 -0
- package/src/corpus/preflight.ts +1789 -0
- package/src/diversity/accounting.ts +90 -0
- package/src/diversity/adjudication.ts +145 -0
- package/src/diversity/admission.ts +60 -0
- package/src/diversity/benchmark.ts +195 -0
- package/src/diversity/clustering.ts +285 -0
- package/src/diversity/errors.ts +7 -0
- package/src/diversity/identity.ts +285 -0
- package/src/diversity/index.ts +11 -0
- package/src/diversity/pipeline-integration.ts +448 -0
- package/src/diversity/projection.ts +57 -0
- package/src/diversity/self-contained.ts +68 -0
- package/src/diversity/target-validation.ts +179 -0
- package/src/doctor-migration.ts +209 -0
- package/src/doctor.ts +723 -0
- package/src/errors.ts +83 -0
- package/src/execution/admission.ts +75 -0
- package/src/execution/atomic-write.ts +35 -0
- package/src/execution/attempt-evidence.ts +415 -0
- package/src/execution/bucket-id.ts +136 -0
- package/src/execution/concurrency-limiter.ts +175 -0
- package/src/execution/contracts.ts +443 -0
- package/src/execution/failure-classification.ts +42 -0
- package/src/execution/failure-policy.ts +240 -0
- package/src/execution/harness.ts +2548 -0
- package/src/execution/settlement.ts +348 -0
- package/src/execution/spawned-process.ts +66 -0
- package/src/execution/spend-limiter.ts +424 -0
- package/src/execution/step-artifacts.ts +55 -0
- package/src/execution/usage-normalized.ts +469 -0
- package/src/git/git.ts +396 -0
- package/src/git/identity.ts +64 -0
- package/src/git/refs.ts +140 -0
- package/src/home-preflight.ts +443 -0
- package/src/home.ts +243 -0
- package/src/ignore-file.ts +336 -0
- package/src/ignore-read.ts +118 -0
- package/src/index.ts +34 -0
- package/src/legacy-launchd.ts +29 -0
- package/src/mcp/mcp.ts +150 -0
- package/src/mcp/preflight.ts +407 -0
- package/src/menu-context.ts +192 -0
- package/src/model/catalog.ts +251 -0
- package/src/model/exec-snapshots.ts +390 -0
- package/src/model/free-discovery.ts +302 -0
- package/src/model/provider-capabilities.ts +814 -0
- package/src/model/routing.ts +600 -0
- package/src/pr/admission.ts +984 -0
- package/src/pr/answered-findings.ts +169 -0
- package/src/pr/ci-admission-gate.ts +743 -0
- package/src/pr/ci-publish.ts +76 -0
- package/src/pr/discovery.ts +432 -0
- package/src/pr/inline.ts +507 -0
- package/src/pr/plan.ts +366 -0
- package/src/pr/posting.ts +115 -0
- package/src/pr/pr-status.ts +845 -0
- package/src/pr/pr.ts +3642 -0
- package/src/pr/preflight.ts +1031 -0
- package/src/pr/publish-outcome.ts +361 -0
- package/src/pr/range.ts +119 -0
- package/src/pr/reverts-preflight.ts +606 -0
- package/src/pr/reverts.ts +377 -0
- package/src/pr/review-pr.ts +927 -0
- package/src/pr/status.ts +154 -0
- package/src/pr/target.ts +388 -0
- package/src/pr/teardown.ts +437 -0
- package/src/pr/worktree-setup.ts +60 -0
- package/src/production-runtime.ts +1877 -0
- package/src/progress.ts +487 -0
- package/src/rereview/adjudicate.ts +89 -0
- package/src/rereview/classify.ts +362 -0
- package/src/rereview/identity.ts +211 -0
- package/src/rereview/plan.ts +308 -0
- package/src/rereview/prepare.ts +1025 -0
- package/src/rereview/state.ts +767 -0
- package/src/rereview/verify.ts +241 -0
- package/src/review/boundary.ts +105 -0
- package/src/review/consolidate.ts +246 -0
- package/src/review/dedupe.ts +368 -0
- package/src/review/drafts.ts +448 -0
- package/src/review/findings-conformance.ts +120 -0
- package/src/review/findings.ts +714 -0
- package/src/review/pipeline.ts +3977 -0
- package/src/review/preflight.ts +3575 -0
- package/src/review/prompt-set.ts +177 -0
- package/src/review/proof-refs.ts +187 -0
- package/src/review/report.ts +1821 -0
- package/src/review/review.ts +490 -0
- package/src/review/root-cause.ts +120 -0
- package/src/review/route-preflight.ts +289 -0
- package/src/review/run.ts +601 -0
- package/src/review/scout.ts +323 -0
- package/src/review/size-gate.ts +595 -0
- package/src/review/spec.ts +235 -0
- package/src/review/step-runner.ts +233 -0
- package/src/runner-authority.ts +596 -0
- package/src/security/binding-policy.ts +115 -0
- package/src/security/credential-broker.ts +773 -0
- package/src/security/evidence-redaction.ts +62 -0
- package/src/security/execution-authority.ts +27 -0
- package/src/security/redact.ts +71 -0
- package/src/security/workspace-read-broker.ts +466 -0
- package/src/server/client.ts +190 -0
- package/src/server/preflight.ts +217 -0
- package/src/server/server.ts +327 -0
- package/src/signal-handler.ts +50 -0
- package/src/store/activity.ts +413 -0
- package/src/store/backfill-preflight.ts +49 -0
- package/src/store/backfill.ts +141 -0
- package/src/store/gc-preflight.ts +206 -0
- package/src/store/gc.ts +424 -0
- package/src/store/metrics-preflight.ts +229 -0
- package/src/store/metrics.ts +236 -0
- package/src/store/preflight.ts +748 -0
- package/src/store/store.ts +693 -0
- package/src/system-tools.ts +355 -0
- package/src/transport-registry.ts +1124 -0
- package/src/transports/claude-code-cli.ts +1348 -0
- package/src/transports/opencode-admission.ts +515 -0
- package/src/transports/opencode-client.ts +3328 -0
- package/src/transports/opencode-evidence.ts +557 -0
- package/src/transports/opencode-mcp.ts +248 -0
- package/src/transports/opencode-sdk.ts +2294 -0
- package/src/transports/opencode-server.ts +535 -0
- package/src/triage/adjudication.ts +113 -0
- package/src/triage/reply.ts +125 -0
- package/src/triage/triage.ts +255 -0
- package/src/ui/activity.ts +173 -0
- package/src/ui/config-edit.ts +816 -0
- package/src/ui/config.ts +462 -0
- package/src/ui/menu.ts +1325 -0
- package/src/ui/plan.ts +1039 -0
- package/src/ui/primitives.ts +300 -0
- package/src/ui/progress.ts +298 -0
- package/src/ui/result.ts +552 -0
- package/src/ui/review-menu.ts +312 -0
- package/src/ui/select.ts +528 -0
- package/src/ui/tree.ts +178 -0
- package/src/uninstaller.ts +479 -0
- package/src/updater.ts +836 -0
- package/src/usage.ts +77 -0
- package/src/watch/preflight.ts +1389 -0
- package/src/watch/watch.ts +1235 -0
- package/src/wizard.ts +877 -0
|
@@ -0,0 +1,743 @@
|
|
|
1
|
+
// PR mode's CI admission gate (ROADMAP Pillar 3): decides, before any git
|
|
2
|
+
// fetch or worktree is touched, whether a CI-triggered review of an
|
|
3
|
+
// already-reviewed head should run, skip, or fall back to a human decision.
|
|
4
|
+
// Relocated out of reviewPr() (src/pr/review-pr.ts): identifiers renamed to
|
|
5
|
+
// explicit parameters (`target.headSha` -> `headSha`, etc.), control flow
|
|
6
|
+
// unchanged. The WHY comments below explain decisions made while this code
|
|
7
|
+
// still lived inline, and they move with it.
|
|
8
|
+
//
|
|
9
|
+
// A no-op on --dry-run (a dry run creates nothing, including ledger writes)
|
|
10
|
+
// and on --force (an explicit override answers the same question this gate
|
|
11
|
+
// asks). Both guards are read straight off the caller's own CliOptions.
|
|
12
|
+
//
|
|
13
|
+
// yieldToInFlightReview (below) is the later, second stand-down point: a
|
|
14
|
+
// review already in flight on the same head. It lives here rather than
|
|
15
|
+
// inline in reviewPr() so its ledger settlement is reachable from a test.
|
|
16
|
+
|
|
17
|
+
import {
|
|
18
|
+
type AdmissionRecord,
|
|
19
|
+
selectActiveReservation,
|
|
20
|
+
} from "#ci/admission-ledger";
|
|
21
|
+
import { writeCliRecord } from "#ci/cli-record";
|
|
22
|
+
import {
|
|
23
|
+
type AdmissionContext,
|
|
24
|
+
planCiReviewManualRequired,
|
|
25
|
+
planCiReviewSkip,
|
|
26
|
+
} from "#ci/gates";
|
|
27
|
+
import { formatWorkflowCommand } from "#ci/reporter";
|
|
28
|
+
import {
|
|
29
|
+
type CiReviewPolicy,
|
|
30
|
+
ciReviewManualRequiredDetail,
|
|
31
|
+
ciReviewPolicyHash,
|
|
32
|
+
ciReviewSkipDetail,
|
|
33
|
+
countableWorkflowHeads,
|
|
34
|
+
deltaTouchesPriorFindings,
|
|
35
|
+
evaluateCiReviewAdmission,
|
|
36
|
+
formatCiAdmissionObserveNotice,
|
|
37
|
+
parseCiAdmissionBlock,
|
|
38
|
+
pathsFromPostedFindingMarkers,
|
|
39
|
+
resolveCiAdmissionAttemptCount,
|
|
40
|
+
resolveCiReviewPolicy,
|
|
41
|
+
resolveCiTrustedActors,
|
|
42
|
+
scanPostedFindingTiers,
|
|
43
|
+
stateReviewCount,
|
|
44
|
+
stopCommandsNewestFirst,
|
|
45
|
+
validateAdmissionAuthority,
|
|
46
|
+
} from "#ci/review-admission";
|
|
47
|
+
import {
|
|
48
|
+
classifyChangedPaths,
|
|
49
|
+
type DeltaRiskAssessment,
|
|
50
|
+
} from "#ci/review-risk";
|
|
51
|
+
import {
|
|
52
|
+
type CiAdmissionLedgerState,
|
|
53
|
+
publishCiSkip,
|
|
54
|
+
recordCiAdmissionGateSkip,
|
|
55
|
+
reserveCiAdmissionLedger,
|
|
56
|
+
settleCiAdmissionLedger,
|
|
57
|
+
settleCiGateSkip,
|
|
58
|
+
} from "#pr/admission";
|
|
59
|
+
import {
|
|
60
|
+
type AnsweredFindings,
|
|
61
|
+
answeredBlockingFindings,
|
|
62
|
+
triageReplyAuthors,
|
|
63
|
+
} from "#pr/answered-findings";
|
|
64
|
+
import {
|
|
65
|
+
CommentsTruncatedError,
|
|
66
|
+
fetchCommitStatuses,
|
|
67
|
+
fetchPrComments,
|
|
68
|
+
fetchPrReviewComments,
|
|
69
|
+
ghCollaboratorPermission,
|
|
70
|
+
ghCompareChangedFilesWithStatus,
|
|
71
|
+
ghPrHeroWorkflowRunHeads,
|
|
72
|
+
listAdmissionCheckRuns,
|
|
73
|
+
} from "#pr/pr";
|
|
74
|
+
import {
|
|
75
|
+
type CiMode,
|
|
76
|
+
type CommitStatusContext,
|
|
77
|
+
findMarkedCommentId,
|
|
78
|
+
IN_FLIGHT_TTL_MS,
|
|
79
|
+
inFlightContextsFor,
|
|
80
|
+
isInFlightCommitStatus,
|
|
81
|
+
} from "#pr/preflight";
|
|
82
|
+
import { parseStateBlock } from "#rereview/state";
|
|
83
|
+
import type { CliOptions, LocalConfig } from "#review/preflight";
|
|
84
|
+
import { log } from "#ui/primitives";
|
|
85
|
+
import {
|
|
86
|
+
markerCommentSeen,
|
|
87
|
+
parsePrCommentMarker,
|
|
88
|
+
summaryMarkerFields,
|
|
89
|
+
} from "#watch/preflight";
|
|
90
|
+
import { CliError } from "../errors";
|
|
91
|
+
|
|
92
|
+
export interface CiAdmissionGateResult {
|
|
93
|
+
ciPolicy: CiReviewPolicy;
|
|
94
|
+
ciPolicyHash: string;
|
|
95
|
+
ledgerRecords: AdmissionRecord[];
|
|
96
|
+
// Defined only when the gate decided the run must stop here — the caller
|
|
97
|
+
// returns this value immediately, exactly as reviewPr() used to `return`
|
|
98
|
+
// inline from inside this block.
|
|
99
|
+
exitCode: number | undefined;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export async function evaluateCiAdmissionGate(input: {
|
|
103
|
+
operatorRoot: string;
|
|
104
|
+
prNumber: number;
|
|
105
|
+
headSha: string;
|
|
106
|
+
config: LocalConfig;
|
|
107
|
+
isCi: boolean;
|
|
108
|
+
// The run's one resolved CI mode (resolveCiMode, reviewPr): the footer of the
|
|
109
|
+
// admission skip's summary follows it.
|
|
110
|
+
ciMode: CiMode;
|
|
111
|
+
options: CliOptions;
|
|
112
|
+
// Test-only seams (defaults: the real Bun.spawn / recordCiAdmissionGateSkip).
|
|
113
|
+
// Every gh() caller below already threads an invisible-to-production
|
|
114
|
+
// `spawnFn` (see pr/pr.ts's own WHY); this gate had no way to reach any of
|
|
115
|
+
// them because it never exposed the option itself. `recordSkip` covers
|
|
116
|
+
// recordCiAdmissionGateSkip specifically because it (via
|
|
117
|
+
// reserveCiAdmissionLedger -> upsertAdmissionCheckRun) has no spawnFn
|
|
118
|
+
// threading of its own and admission.ts is out of scope for this slice.
|
|
119
|
+
// publishCiSkip is deliberately NOT seamed here: with `options.post`
|
|
120
|
+
// unset it never calls gh at all, only the two env-path file writers.
|
|
121
|
+
spawnFn?: typeof Bun.spawn;
|
|
122
|
+
recordSkip?: typeof recordCiAdmissionGateSkip;
|
|
123
|
+
// Test-only: the wait before a stop's permission lookup is retried.
|
|
124
|
+
sleepFn?: (ms: number) => Promise<void>;
|
|
125
|
+
}): Promise<CiAdmissionGateResult> {
|
|
126
|
+
const { operatorRoot, prNumber, headSha, config, isCi, options } = input;
|
|
127
|
+
const spawnFn = input.spawnFn;
|
|
128
|
+
const recordSkip = input.recordSkip ?? recordCiAdmissionGateSkip;
|
|
129
|
+
const ciPolicy = resolveCiReviewPolicy(config);
|
|
130
|
+
const ciPolicyHash = ciReviewPolicyHash(ciPolicy);
|
|
131
|
+
let ledgerRecords: AdmissionRecord[] = [];
|
|
132
|
+
|
|
133
|
+
if (!options.dryRun && isCi) {
|
|
134
|
+
ledgerRecords = await listAdmissionCheckRuns(operatorRoot, headSha, {
|
|
135
|
+
spawnFn,
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if (!options.dryRun && !options.force && isCi) {
|
|
140
|
+
let issueComments: Awaited<ReturnType<typeof fetchPrComments>>;
|
|
141
|
+
let reviewComments: Awaited<ReturnType<typeof fetchPrReviewComments>>;
|
|
142
|
+
let authorityFailOpen = false;
|
|
143
|
+
try {
|
|
144
|
+
[issueComments, reviewComments] = await Promise.all([
|
|
145
|
+
fetchPrComments(operatorRoot, prNumber, { spawnFn }),
|
|
146
|
+
fetchPrReviewComments(operatorRoot, prNumber, { spawnFn }),
|
|
147
|
+
]);
|
|
148
|
+
} catch (error) {
|
|
149
|
+
if (error instanceof CommentsTruncatedError) {
|
|
150
|
+
authorityFailOpen = true;
|
|
151
|
+
issueComments = [];
|
|
152
|
+
reviewComments = [];
|
|
153
|
+
} else {
|
|
154
|
+
throw error;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
const existingSummaryId = findMarkedCommentId(issueComments);
|
|
158
|
+
const summaryBody =
|
|
159
|
+
existingSummaryId === null
|
|
160
|
+
? null
|
|
161
|
+
: (issueComments.find((c) => c.id === existingSummaryId)?.body ?? null);
|
|
162
|
+
const summaryMarker =
|
|
163
|
+
summaryBody === null ? null : parsePrCommentMarker(summaryBody);
|
|
164
|
+
const { summaryHead, summaryComplete } = summaryMarkerFields(summaryMarker);
|
|
165
|
+
const state = summaryBody === null ? null : parseStateBlock(summaryBody);
|
|
166
|
+
const parsedAdmission =
|
|
167
|
+
summaryBody === null ? null : parseCiAdmissionBlock(summaryBody);
|
|
168
|
+
const authority = validateAdmissionAuthority({
|
|
169
|
+
summaryHead,
|
|
170
|
+
reportMarkerHead: summaryHead,
|
|
171
|
+
state,
|
|
172
|
+
admission: parsedAdmission,
|
|
173
|
+
});
|
|
174
|
+
if (!authority.ok) {
|
|
175
|
+
authorityFailOpen = true;
|
|
176
|
+
}
|
|
177
|
+
const trustedActors = resolveCiTrustedActors({
|
|
178
|
+
githubActor: process.env.GITHUB_ACTOR,
|
|
179
|
+
extra: config.ci_trusted_actors,
|
|
180
|
+
});
|
|
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
|
+
});
|
|
192
|
+
const postedFindings =
|
|
193
|
+
summaryHead === null
|
|
194
|
+
? null
|
|
195
|
+
: scanPostedFindingTiers({
|
|
196
|
+
summaryHead,
|
|
197
|
+
comments: allComments,
|
|
198
|
+
trustedActors,
|
|
199
|
+
});
|
|
200
|
+
const markerSeen = markerCommentSeen(issueComments);
|
|
201
|
+
const stateCount = stateReviewCount(state, markerSeen, parsedAdmission);
|
|
202
|
+
const headBranch = process.env.GITHUB_HEAD_REF;
|
|
203
|
+
const workflowHeads =
|
|
204
|
+
headBranch === undefined || headBranch.length === 0
|
|
205
|
+
? new Set<string>()
|
|
206
|
+
: await ghPrHeroWorkflowRunHeads(operatorRoot, headBranch, {
|
|
207
|
+
spawnFn,
|
|
208
|
+
});
|
|
209
|
+
// #326: a run concluded `success` even when the gate skipped it (a skip
|
|
210
|
+
// exits 0), so the run list alone over-counts. Each workflow head's ledger
|
|
211
|
+
// rows say whether it spent. One read per distinct head: `gh run list`
|
|
212
|
+
// returns at most 20 runs by default, so that is at most 20 reads per gate
|
|
213
|
+
// evaluation. Each read fails open to [], which keeps that head counted (the
|
|
214
|
+
// pre-fix behavior), so a failed read can only over-count, never under-count.
|
|
215
|
+
// Each read asks for the head's full attempt history (`includeHistory`): the
|
|
216
|
+
// default returns only the newest row, which would hide a spent attempt
|
|
217
|
+
// behind a later skip.
|
|
218
|
+
//
|
|
219
|
+
// WHY every head reads history, the current head included: the current head
|
|
220
|
+
// can be a workflow head too (a prior concluded run on this same head), and
|
|
221
|
+
// its newest row alone can be a gate-skip that hides an earlier spent
|
|
222
|
+
// attempt. Reusing ledgerRecords (latest-only) here would drop that head and
|
|
223
|
+
// admit an extra paid review past ci_max_attempts. ledgerRecords itself stays
|
|
224
|
+
// latest-only because its other readers (the active-reservation check and
|
|
225
|
+
// countTerminalAttempts) want current state; changing them is a separate
|
|
226
|
+
// decision.
|
|
227
|
+
const ledgerByHead = new Map<string, readonly AdmissionRecord[]>();
|
|
228
|
+
await Promise.all(
|
|
229
|
+
[...workflowHeads].map(async (head) => {
|
|
230
|
+
ledgerByHead.set(
|
|
231
|
+
head,
|
|
232
|
+
await listAdmissionCheckRuns(operatorRoot, head, {
|
|
233
|
+
spawnFn,
|
|
234
|
+
includeHistory: true,
|
|
235
|
+
}),
|
|
236
|
+
);
|
|
237
|
+
}),
|
|
238
|
+
);
|
|
239
|
+
const countedHeads = countableWorkflowHeads(workflowHeads, ledgerByHead);
|
|
240
|
+
const reviewCount = resolveCiAdmissionAttemptCount({
|
|
241
|
+
stateCount,
|
|
242
|
+
workflowHeads: countedHeads,
|
|
243
|
+
ledgerRecords,
|
|
244
|
+
});
|
|
245
|
+
let deltaTouchesPriorFindingsFlag = false;
|
|
246
|
+
let deltaRisk: DeltaRiskAssessment | null = null;
|
|
247
|
+
if (summaryHead !== null && summaryHead !== headSha) {
|
|
248
|
+
const compareFiles = await ghCompareChangedFilesWithStatus(
|
|
249
|
+
operatorRoot,
|
|
250
|
+
summaryHead,
|
|
251
|
+
headSha,
|
|
252
|
+
{ spawnFn },
|
|
253
|
+
);
|
|
254
|
+
const changedPaths = compareFiles.map((entry) => entry.path);
|
|
255
|
+
deltaRisk = classifyChangedPaths(
|
|
256
|
+
changedPaths,
|
|
257
|
+
compareFiles.map((entry) => ({
|
|
258
|
+
path: entry.path,
|
|
259
|
+
status: entry.status,
|
|
260
|
+
})),
|
|
261
|
+
);
|
|
262
|
+
const priorPaths = pathsFromPostedFindingMarkers(
|
|
263
|
+
allComments,
|
|
264
|
+
summaryHead,
|
|
265
|
+
trustedActors,
|
|
266
|
+
);
|
|
267
|
+
if (priorPaths.length > 0) {
|
|
268
|
+
deltaTouchesPriorFindingsFlag = deltaTouchesPriorFindings(
|
|
269
|
+
changedPaths,
|
|
270
|
+
priorPaths,
|
|
271
|
+
);
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
const stopped = await stoppedByMaintainer(
|
|
275
|
+
operatorRoot,
|
|
276
|
+
issueComments,
|
|
277
|
+
spawnFn,
|
|
278
|
+
input.sleepFn ?? Bun.sleep,
|
|
279
|
+
);
|
|
280
|
+
const admissionVerdict = evaluateCiReviewAdmission({
|
|
281
|
+
currentHead: headSha,
|
|
282
|
+
summaryHead,
|
|
283
|
+
summaryComplete,
|
|
284
|
+
markerSeen,
|
|
285
|
+
reviewCount,
|
|
286
|
+
state,
|
|
287
|
+
admission: parsedAdmission,
|
|
288
|
+
postedFindings,
|
|
289
|
+
policy: ciPolicy,
|
|
290
|
+
deltaTouchesPriorFindings: deltaTouchesPriorFindingsFlag,
|
|
291
|
+
deltaRisk,
|
|
292
|
+
authorityFailOpen,
|
|
293
|
+
stopped,
|
|
294
|
+
});
|
|
295
|
+
const admissionContext: AdmissionContext = {
|
|
296
|
+
currentHead: headSha,
|
|
297
|
+
reviewedHead: summaryHead,
|
|
298
|
+
policyMode: ciPolicy.mode,
|
|
299
|
+
policyHash: ciPolicyHash,
|
|
300
|
+
deltaRisk,
|
|
301
|
+
};
|
|
302
|
+
// WHY a stop skips even in observe-only mode: observe-only is a rollout
|
|
303
|
+
// setting for automatic decisions, and a stop is a maintainer's explicit one.
|
|
304
|
+
const observeOnly =
|
|
305
|
+
config.ci_admission_observe_only === true &&
|
|
306
|
+
!(
|
|
307
|
+
admissionVerdict.action === "skip" &&
|
|
308
|
+
admissionVerdict.reason === "stopped"
|
|
309
|
+
);
|
|
310
|
+
if (
|
|
311
|
+
observeOnly &&
|
|
312
|
+
(admissionVerdict.action === "skip" ||
|
|
313
|
+
admissionVerdict.action === "manual-required")
|
|
314
|
+
) {
|
|
315
|
+
log(
|
|
316
|
+
formatWorkflowCommand(
|
|
317
|
+
"notice",
|
|
318
|
+
formatCiAdmissionObserveNotice({
|
|
319
|
+
verdict: admissionVerdict,
|
|
320
|
+
currentHead: headSha,
|
|
321
|
+
reviewedHead: summaryHead,
|
|
322
|
+
policyMode: ciPolicy.mode,
|
|
323
|
+
policyHash: ciPolicyHash,
|
|
324
|
+
deltaRisk,
|
|
325
|
+
}),
|
|
326
|
+
),
|
|
327
|
+
);
|
|
328
|
+
}
|
|
329
|
+
if (!observeOnly && admissionVerdict.action === "skip") {
|
|
330
|
+
const skipReason = ciReviewSkipDetail(admissionVerdict);
|
|
331
|
+
await recordSkip({
|
|
332
|
+
operatorRoot,
|
|
333
|
+
prNumber,
|
|
334
|
+
headSha,
|
|
335
|
+
policy: ciPolicy,
|
|
336
|
+
policyHash: ciPolicyHash,
|
|
337
|
+
existing: ledgerRecords,
|
|
338
|
+
reason: skipReason,
|
|
339
|
+
priorScore: admissionVerdict.prior.score,
|
|
340
|
+
blockingCount: admissionVerdict.prior.blocking,
|
|
341
|
+
advisoryCount: admissionVerdict.prior.advisory,
|
|
342
|
+
// WHY unspent: a stop is lifted by the next maintainer review command, and
|
|
343
|
+
// stopped pushes counted as attempts would leave that PR with no budget.
|
|
344
|
+
unspent: admissionVerdict.reason === "stopped",
|
|
345
|
+
});
|
|
346
|
+
// S4: in gate mode the head inherits the verdict of the PR's own last reviewed commit, read from
|
|
347
|
+
// that commit's statuses (publishCiSkip, admissionSkipDecision): a skipped head never reads as one
|
|
348
|
+
// nobody decided about.
|
|
349
|
+
const plan = planCiReviewSkip({
|
|
350
|
+
prNumber,
|
|
351
|
+
verdict: admissionVerdict,
|
|
352
|
+
admission: admissionContext,
|
|
353
|
+
ciMode: input.ciMode,
|
|
354
|
+
});
|
|
355
|
+
const exitCode = await publishCiSkip({
|
|
356
|
+
operatorRoot,
|
|
357
|
+
prNumber,
|
|
358
|
+
post: options.post === true,
|
|
359
|
+
isCi,
|
|
360
|
+
stepSummaryFlag: options.stepSummary,
|
|
361
|
+
plan,
|
|
362
|
+
noticeMessage: `crysk review skipped — ${skipReason}`,
|
|
363
|
+
ciMode: input.ciMode,
|
|
364
|
+
headSha,
|
|
365
|
+
spawnFn,
|
|
366
|
+
admission: {
|
|
367
|
+
reason: admissionVerdict.reason,
|
|
368
|
+
reviewedHead: summaryHead,
|
|
369
|
+
priorBlocking: admissionVerdict.prior.blocking,
|
|
370
|
+
readAnswered,
|
|
371
|
+
},
|
|
372
|
+
});
|
|
373
|
+
return { ciPolicy, ciPolicyHash, ledgerRecords, exitCode };
|
|
374
|
+
}
|
|
375
|
+
if (!observeOnly && admissionVerdict.action === "manual-required") {
|
|
376
|
+
const manualReason = ciReviewManualRequiredDetail(admissionVerdict);
|
|
377
|
+
await recordSkip({
|
|
378
|
+
operatorRoot,
|
|
379
|
+
prNumber,
|
|
380
|
+
headSha,
|
|
381
|
+
policy: ciPolicy,
|
|
382
|
+
policyHash: ciPolicyHash,
|
|
383
|
+
existing: ledgerRecords,
|
|
384
|
+
reason: manualReason,
|
|
385
|
+
priorScore: admissionVerdict.prior.score,
|
|
386
|
+
blockingCount: admissionVerdict.prior.blocking,
|
|
387
|
+
advisoryCount: admissionVerdict.prior.advisory,
|
|
388
|
+
});
|
|
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.
|
|
391
|
+
const plan = planCiReviewManualRequired({
|
|
392
|
+
prNumber,
|
|
393
|
+
verdict: admissionVerdict,
|
|
394
|
+
admission: admissionContext,
|
|
395
|
+
ciMode: input.ciMode,
|
|
396
|
+
});
|
|
397
|
+
const exitCode = await publishCiSkip({
|
|
398
|
+
operatorRoot,
|
|
399
|
+
prNumber,
|
|
400
|
+
post: options.post === true,
|
|
401
|
+
isCi,
|
|
402
|
+
stepSummaryFlag: options.stepSummary,
|
|
403
|
+
plan,
|
|
404
|
+
noticeMessage: `crysk automatic reviews are done for this PR — ${manualReason}`,
|
|
405
|
+
ciMode: input.ciMode,
|
|
406
|
+
headSha,
|
|
407
|
+
spawnFn,
|
|
408
|
+
admission: {
|
|
409
|
+
reason: admissionVerdict.reason,
|
|
410
|
+
reviewedHead: summaryHead,
|
|
411
|
+
priorBlocking: admissionVerdict.prior.blocking,
|
|
412
|
+
readAnswered,
|
|
413
|
+
},
|
|
414
|
+
});
|
|
415
|
+
return { ciPolicy, ciPolicyHash, ledgerRecords, exitCode };
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
return { ciPolicy, ciPolicyHash, ledgerRecords, exitCode: undefined };
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
// Cross-machine TOCTOU: the watcher already skipped fresh pendings at
|
|
422
|
+
// gather, but a CLI and a watcher can still overlap between that fetch
|
|
423
|
+
// and this process posting its own pending. --yes (the watcher child)
|
|
424
|
+
// aborts before createPrRunDir so it consumes no poison-PR attempt.
|
|
425
|
+
// Interactive continues: a stuck pending must not trap the operator
|
|
426
|
+
// behind the 90-minute TTL.
|
|
427
|
+
//
|
|
428
|
+
// Relocated out of reviewPr() (src/pr/review-pr.ts) with its log lines
|
|
429
|
+
// byte-identical. One addition: a CI run reaches this check AFTER reviewPr
|
|
430
|
+
// reserved its admission ledger row, so a stand-down must settle that row
|
|
431
|
+
// as `"yielded"` before returning. Left `"reserved"`, it reached teardown's
|
|
432
|
+
// safety net, which settles it `"failed"` (musive #1935, run 37018094654: a
|
|
433
|
+
// red pr-hero/ci-admission check and a spent attempt for a run that reviewed
|
|
434
|
+
// nothing, while the local review it yielded to posted fine). Outside CI the
|
|
435
|
+
// ledger is null and the settle is a no-op, so the watcher child behaves as
|
|
436
|
+
// before. The check stays where it is, after the reservation, on purpose:
|
|
437
|
+
// moving it earlier would change the CI path's ordering for every other exit.
|
|
438
|
+
//
|
|
439
|
+
// Which pendings stand the run down depends on its posture. A watcher child or a
|
|
440
|
+
// local run (no --ci) reads every run context, because local runs and the watcher
|
|
441
|
+
// post their pending on pr-hero/local and would otherwise double-spend on the
|
|
442
|
+
// same head. A --ci run reads only the gate: it settles pr-hero alone, and a stand-
|
|
443
|
+
// down on a local pending would leave pr-hero unwritten, since no local run writes
|
|
444
|
+
// that context. action.yml runs CI as `review --ci --yes`, so the yield is live
|
|
445
|
+
// there, and this is why the --ci narrowing matters.
|
|
446
|
+
//
|
|
447
|
+
// Returns the exit code when the run must stop here, `undefined` to proceed.
|
|
448
|
+
// `spawnFn` and `nowMs` are test-only seams (defaults: the real Bun.spawn and
|
|
449
|
+
// Date.now()). The one spawnFn reaches both gh calls made here: the
|
|
450
|
+
// commit-status read and the check-run upsert inside settleCiAdmissionLedger.
|
|
451
|
+
export async function yieldToInFlightReview(input: {
|
|
452
|
+
operatorRoot: string;
|
|
453
|
+
headSha: string;
|
|
454
|
+
options: Pick<CliOptions, "yes" | "force">;
|
|
455
|
+
// The run's posture (resolveCiPosture): decides which pending contexts stand it down.
|
|
456
|
+
statusContext: CommitStatusContext;
|
|
457
|
+
ciAdmissionLedger: CiAdmissionLedgerState | null;
|
|
458
|
+
spawnFn?: typeof Bun.spawn;
|
|
459
|
+
nowMs?: number;
|
|
460
|
+
}): Promise<number | undefined> {
|
|
461
|
+
const { operatorRoot, headSha, options, ciAdmissionLedger, spawnFn } = input;
|
|
462
|
+
const statuses = await fetchCommitStatuses(operatorRoot, headSha, {
|
|
463
|
+
spawnFn,
|
|
464
|
+
});
|
|
465
|
+
if (
|
|
466
|
+
!isInFlightCommitStatus(
|
|
467
|
+
statuses,
|
|
468
|
+
input.nowMs ?? Date.now(),
|
|
469
|
+
IN_FLIGHT_TTL_MS,
|
|
470
|
+
inFlightContextsFor(input.statusContext),
|
|
471
|
+
)
|
|
472
|
+
) {
|
|
473
|
+
return undefined;
|
|
474
|
+
}
|
|
475
|
+
// `--yes` without `--force` is the watcher child: a second launch
|
|
476
|
+
// would double-spend. `--force` is the CI comment override, and the
|
|
477
|
+
// workflow's concurrency group cancels the run already holding this
|
|
478
|
+
// pending status. Treating that leftover pending as a skip would
|
|
479
|
+
// cancel the review the comment just asked for and then refuse to
|
|
480
|
+
// start the replacement.
|
|
481
|
+
if (options.yes && !options.force) {
|
|
482
|
+
log("skip: a crysk review is already in-flight on this head");
|
|
483
|
+
await settleCiAdmissionLedger(
|
|
484
|
+
ciAdmissionLedger,
|
|
485
|
+
"yielded",
|
|
486
|
+
"another crysk review is already in flight on this head",
|
|
487
|
+
{ spawnFn },
|
|
488
|
+
);
|
|
489
|
+
writeCliRecord({ v: 1, outcome: "yielded" });
|
|
490
|
+
return 0;
|
|
491
|
+
}
|
|
492
|
+
log(
|
|
493
|
+
"warning: a crysk review is already in-flight on this head; " +
|
|
494
|
+
"continuing",
|
|
495
|
+
);
|
|
496
|
+
return undefined;
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
// A configuration error that stops a CI run before it reviews anything: the
|
|
500
|
+
// gotchas file (review/run.ts validateGotchas) or the prompt set (review/
|
|
501
|
+
// preflight.ts). Both throw a CliError with a classified `cls`, and only those.
|
|
502
|
+
// WHY the class and not the message: a wording change must not move an error
|
|
503
|
+
// in or out of the budget rule.
|
|
504
|
+
export function isCiConfigError(error: unknown): boolean {
|
|
505
|
+
if (!(error instanceof CliError) || error.cls === undefined) return false;
|
|
506
|
+
return error.cls.kind === "gotchas" || error.cls.kind === "prompt-set";
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
// WHY this writes a row: the attempt count reads each head's ledger rows, and a
|
|
510
|
+
// head with no row counts as a spent attempt (its run is in the workflow-run
|
|
511
|
+
// list). A run that stops on a configuration error before it reviews spent
|
|
512
|
+
// nothing, so it writes the same `gate-skipped` row the size and budget gates
|
|
513
|
+
// write (#326). The caller rethrows the ORIGINAL error after this returns, so
|
|
514
|
+
// the error, the exit code and the outcome code are unchanged.
|
|
515
|
+
// WHY every step is swallowed: the row is bookkeeping for a run that is already
|
|
516
|
+
// failing. A gh or check-run failure here must not replace the configuration
|
|
517
|
+
// error the operator needs to see. A head that cannot be recorded simply keeps
|
|
518
|
+
// counting, which is the behavior before this function existed.
|
|
519
|
+
// WHY an active reservation is left alone: settling it would finish another
|
|
520
|
+
// run's reservation on the same head as if it were ours.
|
|
521
|
+
export const CONFIG_SKIP_RECORD_TIMEOUT_MS = 15_000;
|
|
522
|
+
|
|
523
|
+
export async function recordCiConfigErrorSkip(input: {
|
|
524
|
+
operatorRoot: string;
|
|
525
|
+
prNumber: number;
|
|
526
|
+
config: LocalConfig;
|
|
527
|
+
resolveHeadSha: () => Promise<string>;
|
|
528
|
+
reason: string;
|
|
529
|
+
spawnFn?: typeof Bun.spawn;
|
|
530
|
+
// Test-only seam: the bound on the READS before the reservation.
|
|
531
|
+
timeoutMs?: number;
|
|
532
|
+
}): Promise<void> {
|
|
533
|
+
// WHY the deadline covers only the reads: the head lookup and the ledger list
|
|
534
|
+
// have no bound of their own, and the caller rethrows the configuration error
|
|
535
|
+
// right after this returns. Once a reservation exists, though, it must be
|
|
536
|
+
// settled, or it stays `reserved` and counts as a spent attempt forever. So the
|
|
537
|
+
// deadline ends before the reserve, and the settle always runs. Its gh calls
|
|
538
|
+
// are already bounded by the check-run upsert (ADMISSION_CHECK_RUN_TIMEOUT_MS,
|
|
539
|
+
// two attempts), and a failed settle is swallowed by tryPersistCiAdmissionLedger.
|
|
540
|
+
const reads = await readBeforeReservation(input);
|
|
541
|
+
if (reads === null) return;
|
|
542
|
+
try {
|
|
543
|
+
const active = selectActiveReservation(
|
|
544
|
+
reads.existing,
|
|
545
|
+
reads.headSha,
|
|
546
|
+
reads.ciPolicyHash,
|
|
547
|
+
reads.ciPolicy.reservationTtlSeconds,
|
|
548
|
+
new Date(),
|
|
549
|
+
);
|
|
550
|
+
if (active !== null) return;
|
|
551
|
+
// If the reserve fails there is no row, so there is nothing to settle.
|
|
552
|
+
const state = await reserveCiAdmissionLedger({
|
|
553
|
+
operatorRoot: input.operatorRoot,
|
|
554
|
+
prNumber: input.prNumber,
|
|
555
|
+
headSha: reads.headSha,
|
|
556
|
+
policy: reads.ciPolicy,
|
|
557
|
+
policyHash: reads.ciPolicyHash,
|
|
558
|
+
existing: reads.existing,
|
|
559
|
+
decisionReason: input.reason,
|
|
560
|
+
spawnFn: input.spawnFn,
|
|
561
|
+
});
|
|
562
|
+
await settleCiGateSkip(state, input.reason, { spawnFn: input.spawnFn });
|
|
563
|
+
} catch {
|
|
564
|
+
// Swallowed on purpose: see the WHY above.
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
// The reads before the reservation, under one deadline. Returns null when the
|
|
569
|
+
// deadline wins or a read fails: nothing was reserved, so there is nothing to
|
|
570
|
+
// settle, and the caller rethrows the original error.
|
|
571
|
+
async function readBeforeReservation(input: {
|
|
572
|
+
operatorRoot: string;
|
|
573
|
+
config: LocalConfig;
|
|
574
|
+
resolveHeadSha: () => Promise<string>;
|
|
575
|
+
spawnFn?: typeof Bun.spawn;
|
|
576
|
+
timeoutMs?: number;
|
|
577
|
+
}): Promise<{
|
|
578
|
+
headSha: string;
|
|
579
|
+
ciPolicy: CiReviewPolicy;
|
|
580
|
+
ciPolicyHash: string;
|
|
581
|
+
existing: AdmissionRecord[];
|
|
582
|
+
} | null> {
|
|
583
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
584
|
+
const deadline = new Promise<"timeout">((resolve) => {
|
|
585
|
+
timer = setTimeout(
|
|
586
|
+
() => resolve("timeout"),
|
|
587
|
+
input.timeoutMs ?? CONFIG_SKIP_RECORD_TIMEOUT_MS,
|
|
588
|
+
);
|
|
589
|
+
});
|
|
590
|
+
const work = (async () => {
|
|
591
|
+
const headSha = await input.resolveHeadSha();
|
|
592
|
+
const ciPolicy = resolveCiReviewPolicy(input.config);
|
|
593
|
+
const ciPolicyHash = ciReviewPolicyHash(ciPolicy);
|
|
594
|
+
const existing = await listAdmissionCheckRuns(input.operatorRoot, headSha, {
|
|
595
|
+
spawnFn: input.spawnFn,
|
|
596
|
+
});
|
|
597
|
+
return { headSha, ciPolicy, ciPolicyHash, existing };
|
|
598
|
+
})();
|
|
599
|
+
try {
|
|
600
|
+
const outcome = await Promise.race([work, deadline]);
|
|
601
|
+
if (outcome === "timeout") {
|
|
602
|
+
log(
|
|
603
|
+
"config-error ledger row not recorded: GitHub did not answer in time",
|
|
604
|
+
);
|
|
605
|
+
return null;
|
|
606
|
+
}
|
|
607
|
+
return outcome;
|
|
608
|
+
} catch {
|
|
609
|
+
return null;
|
|
610
|
+
} finally {
|
|
611
|
+
clearTimeout(timer);
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
// Whether a write-access maintainer's `/crysk stop` is in force: the
|
|
616
|
+
// newest stop, review or recheck command from a write or admin author decides.
|
|
617
|
+
// Commands from anyone else are passed over, so a stranger can neither set a
|
|
618
|
+
// stop nor lift one. Fails open: an author whose permission cannot be read
|
|
619
|
+
// counts as a maintainer for a lift and as nobody for a stop, so either way
|
|
620
|
+
// the review runs. A wrong skip loses a review someone wanted; a wrong run
|
|
621
|
+
// only costs one.
|
|
622
|
+
async function stoppedByMaintainer(
|
|
623
|
+
operatorRoot: string,
|
|
624
|
+
issueComments: readonly { user: string; body: string }[],
|
|
625
|
+
spawnFn: typeof Bun.spawn | undefined,
|
|
626
|
+
sleepFn: (ms: number) => Promise<void>,
|
|
627
|
+
): Promise<boolean> {
|
|
628
|
+
const commands = stopCommandsNewestFirst(issueComments);
|
|
629
|
+
// Nothing older than the oldest stop can change the answer: with no stop
|
|
630
|
+
// left below it, the walk ends "not stopped" anyway. So a PR without a stop
|
|
631
|
+
// looks up nobody, however many review commands it has.
|
|
632
|
+
const oldestStop = commands.map((c) => c.kind).lastIndexOf("stop");
|
|
633
|
+
const permissions = new Map<string, string | null>();
|
|
634
|
+
for (const command of commands.slice(0, oldestStop + 1)) {
|
|
635
|
+
let permission = permissions.get(command.user);
|
|
636
|
+
if (!permissions.has(command.user)) {
|
|
637
|
+
permission = await stopAuthorPermission(
|
|
638
|
+
operatorRoot,
|
|
639
|
+
command.user,
|
|
640
|
+
spawnFn,
|
|
641
|
+
sleepFn,
|
|
642
|
+
);
|
|
643
|
+
permissions.set(command.user, permission ?? null);
|
|
644
|
+
}
|
|
645
|
+
if (permission === null || permission === undefined) {
|
|
646
|
+
if (command.kind === "lift") return false;
|
|
647
|
+
log(
|
|
648
|
+
formatWorkflowCommand(
|
|
649
|
+
"warning",
|
|
650
|
+
`Could not read the permission of ${command.user}, who posted a stop command, so the stop does not count and the review runs.`,
|
|
651
|
+
),
|
|
652
|
+
);
|
|
653
|
+
continue;
|
|
654
|
+
}
|
|
655
|
+
if (permission === "admin" || permission === "write") {
|
|
656
|
+
return command.kind === "stop";
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
return false;
|
|
660
|
+
}
|
|
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
|
+
|
|
722
|
+
// Same bounds as the trigger's commenter lookup (lookupCommenterPermission):
|
|
723
|
+
// one retry after a short wait, so a single API blip does not override a
|
|
724
|
+
// maintainer's stop, and a timeout, so a hung gh call cannot stall admission.
|
|
725
|
+
const STOP_PERMISSION_RETRY_DELAY_MS = 2_000;
|
|
726
|
+
const STOP_PERMISSION_TIMEOUT_MS = 30_000;
|
|
727
|
+
|
|
728
|
+
async function stopAuthorPermission(
|
|
729
|
+
operatorRoot: string,
|
|
730
|
+
login: string,
|
|
731
|
+
spawnFn: typeof Bun.spawn | undefined,
|
|
732
|
+
sleepFn: (ms: number) => Promise<void>,
|
|
733
|
+
): Promise<string | null> {
|
|
734
|
+
const ask = () =>
|
|
735
|
+
ghCollaboratorPermission(operatorRoot, login, {
|
|
736
|
+
spawnFn,
|
|
737
|
+
timeoutMs: STOP_PERMISSION_TIMEOUT_MS,
|
|
738
|
+
});
|
|
739
|
+
const first = await ask();
|
|
740
|
+
if (first !== null) return first;
|
|
741
|
+
await sleepFn(STOP_PERMISSION_RETRY_DELAY_MS);
|
|
742
|
+
return ask();
|
|
743
|
+
}
|