crysk 0.0.0-stage → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +176 -0
- package/README.md +610 -2
- 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/adjudicator-dtg2axnh.md +86 -0
- package/dist/admission-config.example-a9vqmmad.json +9 -0
- package/dist/ci-admission-camfbx2b.md +122 -0
- package/dist/cli.js +42292 -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 +82 -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 +122 -0
- package/skills/crysk-ci-setup/references/opencode-ci.md +141 -0
- package/skills/crysk-loop/SKILL.md +496 -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 +923 -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 +901 -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 +847 -0
- package/src/pr/ci-admission-gate.ts +665 -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 +787 -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,847 @@
|
|
|
1
|
+
// CI admission ledger state and skip publishing helpers.
|
|
2
|
+
// Extracted from cli.ts (cli-decomp S5).
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
type AdmissionAttemptStatus,
|
|
6
|
+
type AdmissionRecord,
|
|
7
|
+
reserveAdmissionAttempt,
|
|
8
|
+
settleAdmissionAttempt,
|
|
9
|
+
} from "#ci/admission-ledger";
|
|
10
|
+
import { classifyCliError } from "#ci/classify-error";
|
|
11
|
+
import { writeCliRecord } from "#ci/cli-record";
|
|
12
|
+
import {
|
|
13
|
+
type CiGateSkipPlan,
|
|
14
|
+
shouldWriteCiOutputs,
|
|
15
|
+
shouldWriteStepSummary,
|
|
16
|
+
} from "#ci/gates";
|
|
17
|
+
import type { CliRecord } from "#ci/outcome";
|
|
18
|
+
import { isOverrideStatus, newestStatusInContext } from "#ci/override";
|
|
19
|
+
import {
|
|
20
|
+
appendCiOutputs,
|
|
21
|
+
appendStepSummary,
|
|
22
|
+
formatWorkflowCommand,
|
|
23
|
+
} from "#ci/reporter";
|
|
24
|
+
import { type CiReviewPolicy, ciReviewSkipLabel } from "#ci/review-admission";
|
|
25
|
+
import {
|
|
26
|
+
fetchCommitStatuses,
|
|
27
|
+
fetchCommitStatusList,
|
|
28
|
+
fetchPrCommitShas,
|
|
29
|
+
ghRepoWebUrl,
|
|
30
|
+
repostMarkedPrComment,
|
|
31
|
+
upsertAdmissionCheckRun,
|
|
32
|
+
} from "#pr/pr";
|
|
33
|
+
import {
|
|
34
|
+
blockingCommitStatusRequest,
|
|
35
|
+
type CiMode,
|
|
36
|
+
COMMIT_STATUS_CONTEXT,
|
|
37
|
+
type CommitStatusFact,
|
|
38
|
+
clipStatusDescription,
|
|
39
|
+
commitStatusRequest,
|
|
40
|
+
partialReviewAgainDescription,
|
|
41
|
+
partialReviewDescription,
|
|
42
|
+
prHtmlUrl,
|
|
43
|
+
skipCommitStatusRequest,
|
|
44
|
+
} from "#pr/preflight";
|
|
45
|
+
import { publishFinalCommitStatus } from "#pr/status";
|
|
46
|
+
import { log } from "#ui/primitives";
|
|
47
|
+
import { canonicalStatusText, STATUS_CONTEXT } from "../brand";
|
|
48
|
+
|
|
49
|
+
// ROADMAP Pillar 3 (GitHub Actions CI). The mechanical glue behind a gate
|
|
50
|
+
// skip in CI mode: every DECISION (what to say, which marker, what the
|
|
51
|
+
// outputs are) already happened in `plan` (planCiSizeSkip/planCiBudgetSkip,
|
|
52
|
+
// ci/gates.ts) — this function has nothing left to decide, only three
|
|
53
|
+
// straight-line I/O calls gated by shouldWriteStepSummary/shouldWriteCiOutputs.
|
|
54
|
+
// `repostMarkedPrComment` keeps one notice per marker without hiding it: a
|
|
55
|
+
// repeat CI run on the same still-failing PR posts the fresh notice at the
|
|
56
|
+
// bottom and deletes its own older ones, instead of silently editing the old
|
|
57
|
+
// one in place (an edit neither notifies nor moves down the timeline).
|
|
58
|
+
export type CiAdmissionLedgerState = {
|
|
59
|
+
record: AdmissionRecord;
|
|
60
|
+
checkRunId: number;
|
|
61
|
+
headSha: string;
|
|
62
|
+
operatorRoot: string;
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
export async function persistCiAdmissionLedger(
|
|
66
|
+
state: CiAdmissionLedgerState,
|
|
67
|
+
options?: { spawnFn?: typeof Bun.spawn },
|
|
68
|
+
): Promise<void> {
|
|
69
|
+
state.checkRunId = await upsertAdmissionCheckRun(
|
|
70
|
+
state.operatorRoot,
|
|
71
|
+
{
|
|
72
|
+
headSha: state.headSha,
|
|
73
|
+
record: state.record,
|
|
74
|
+
checkRunId: state.checkRunId,
|
|
75
|
+
},
|
|
76
|
+
options,
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export async function tryPersistCiAdmissionLedger(
|
|
81
|
+
state: CiAdmissionLedgerState | null,
|
|
82
|
+
options?: { spawnFn?: typeof Bun.spawn },
|
|
83
|
+
): Promise<void> {
|
|
84
|
+
if (state === null) return;
|
|
85
|
+
try {
|
|
86
|
+
await persistCiAdmissionLedger(state, options);
|
|
87
|
+
} catch {
|
|
88
|
+
// Best-effort: settlement must not mask the underlying review failure.
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// `"yielded"` is terminal too: without it here, teardown's safety net (or
|
|
93
|
+
// any later exit) could overwrite a yield with `"failed"`, which is the red
|
|
94
|
+
// check this status exists to prevent. `"gate-skipped"` (#326) is the same:
|
|
95
|
+
// a later settle must not rewrite a gate skip as `"failed"`.
|
|
96
|
+
const TERMINAL_ADMISSION_STATUSES: ReadonlySet<AdmissionAttemptStatus> =
|
|
97
|
+
new Set([
|
|
98
|
+
"completed",
|
|
99
|
+
"failed",
|
|
100
|
+
"cancelled",
|
|
101
|
+
"skipped",
|
|
102
|
+
"yielded",
|
|
103
|
+
"gate-skipped",
|
|
104
|
+
]);
|
|
105
|
+
|
|
106
|
+
// A record that already reached a terminal status is never re-settled: the
|
|
107
|
+
// review shells call this from several exits (and teardown's finally), and
|
|
108
|
+
// a later call must not overwrite an earlier `completed` with `failed`.
|
|
109
|
+
// `spawnFn` is the gh boundary for offline tests; production omits it.
|
|
110
|
+
// Same shape as the commit-status lock in status.ts, and for the same reason:
|
|
111
|
+
// the SIGTERM/SIGINT handlers live in runCli and end in process.exit(), which
|
|
112
|
+
// skips reviewPr's finally. A cancel-in-progress run otherwise leaves its
|
|
113
|
+
// ledger row at "provider-started" forever, and that status both counts as a
|
|
114
|
+
// spent attempt and blocks the next reservation (#164).
|
|
115
|
+
let heldAdmission: CiAdmissionLedgerState | null = null;
|
|
116
|
+
|
|
117
|
+
export function holdCiAdmissionLedger(
|
|
118
|
+
state: CiAdmissionLedgerState | null,
|
|
119
|
+
): void {
|
|
120
|
+
heldAdmission = state;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export function releaseCiAdmissionLedger(): void {
|
|
124
|
+
heldAdmission = null;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// Take-and-clear BEFORE the await. Actions sends SIGINT and then SIGTERM
|
|
128
|
+
// inside the grace window, so both handlers can be in flight; the second
|
|
129
|
+
// must see nothing left to settle. A row that already reached a terminal
|
|
130
|
+
// status (completed, failed, skipped, cancelled, yielded) is left alone —
|
|
131
|
+
// settleCiAdmissionLedger's own guard. Every error is swallowed: failing to
|
|
132
|
+
// settle is the old bug, and a throw here would cost the exit code.
|
|
133
|
+
export async function settleHeldAdmissionLedgerOnSignal(
|
|
134
|
+
settle: typeof settleCiAdmissionLedger = settleCiAdmissionLedger,
|
|
135
|
+
): Promise<void> {
|
|
136
|
+
const state = heldAdmission;
|
|
137
|
+
heldAdmission = null;
|
|
138
|
+
if (state === null) return;
|
|
139
|
+
if (
|
|
140
|
+
state.record.status !== "reserved" &&
|
|
141
|
+
state.record.status !== "provider-started"
|
|
142
|
+
) {
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
try {
|
|
146
|
+
await settle(
|
|
147
|
+
state,
|
|
148
|
+
"cancelled",
|
|
149
|
+
"workflow cancelled before the review produced anything",
|
|
150
|
+
);
|
|
151
|
+
} catch {
|
|
152
|
+
// Ignore
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export async function settleCiAdmissionLedger(
|
|
157
|
+
state: CiAdmissionLedgerState | null,
|
|
158
|
+
status: AdmissionAttemptStatus,
|
|
159
|
+
reason: string,
|
|
160
|
+
options?: { spawnFn?: typeof Bun.spawn },
|
|
161
|
+
): Promise<void> {
|
|
162
|
+
if (state === null) return;
|
|
163
|
+
if (TERMINAL_ADMISSION_STATUSES.has(state.record.status)) return;
|
|
164
|
+
state.record = settleAdmissionAttempt(state.record, status, reason);
|
|
165
|
+
await tryPersistCiAdmissionLedger(state, options);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// #326: the SIZE gate and the BUDGET gate settle through here, not with
|
|
169
|
+
// `"skipped"`. An admission skip (recordCiAdmissionGateSkip below) keeps
|
|
170
|
+
// `"skipped"` and keeps counting; a gate skip spent nothing, so its row must
|
|
171
|
+
// not count against the PR's attempts (see TERMINAL_BUDGET_STATUSES).
|
|
172
|
+
export async function settleCiGateSkip(
|
|
173
|
+
state: CiAdmissionLedgerState | null,
|
|
174
|
+
reason: string,
|
|
175
|
+
options?: { spawnFn?: typeof Bun.spawn },
|
|
176
|
+
): Promise<void> {
|
|
177
|
+
await settleCiAdmissionLedger(state, "gate-skipped", reason, options);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export async function reserveCiAdmissionLedger(input: {
|
|
181
|
+
operatorRoot: string;
|
|
182
|
+
prNumber: number;
|
|
183
|
+
headSha: string;
|
|
184
|
+
policy: CiReviewPolicy;
|
|
185
|
+
policyHash: string;
|
|
186
|
+
existing: readonly AdmissionRecord[];
|
|
187
|
+
decisionReason: string;
|
|
188
|
+
priorScore?: number | null;
|
|
189
|
+
blockingCount?: number | null;
|
|
190
|
+
advisoryCount?: number | null;
|
|
191
|
+
// A comment-triggered run (`/pr-hero review`, `/pr-hero recheck`): its row is
|
|
192
|
+
// marked so the attempt count leaves it out (see countTerminalAttempts).
|
|
193
|
+
forced?: boolean;
|
|
194
|
+
// Test-only seam (default: the real Bun.spawn), threaded to the check-run write.
|
|
195
|
+
spawnFn?: typeof Bun.spawn;
|
|
196
|
+
}): Promise<CiAdmissionLedgerState> {
|
|
197
|
+
const { record } = reserveAdmissionAttempt({
|
|
198
|
+
existing: input.existing,
|
|
199
|
+
prNumber: input.prNumber,
|
|
200
|
+
headSha: input.headSha,
|
|
201
|
+
policyHash: input.policyHash,
|
|
202
|
+
workflowRunId: process.env.GITHUB_RUN_ID ?? null,
|
|
203
|
+
decisionReason: input.decisionReason,
|
|
204
|
+
priorScore: input.priorScore ?? null,
|
|
205
|
+
blockingCount: input.blockingCount ?? null,
|
|
206
|
+
advisoryCount: input.advisoryCount ?? null,
|
|
207
|
+
reservationTtlSeconds: input.policy.reservationTtlSeconds,
|
|
208
|
+
forced: input.forced,
|
|
209
|
+
});
|
|
210
|
+
const checkRunId = await upsertAdmissionCheckRun(
|
|
211
|
+
input.operatorRoot,
|
|
212
|
+
{
|
|
213
|
+
headSha: input.headSha,
|
|
214
|
+
record,
|
|
215
|
+
},
|
|
216
|
+
{ spawnFn: input.spawnFn },
|
|
217
|
+
);
|
|
218
|
+
return {
|
|
219
|
+
record,
|
|
220
|
+
checkRunId,
|
|
221
|
+
headSha: input.headSha,
|
|
222
|
+
operatorRoot: input.operatorRoot,
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
export async function recordCiAdmissionGateSkip(input: {
|
|
227
|
+
operatorRoot: string;
|
|
228
|
+
prNumber: number;
|
|
229
|
+
headSha: string;
|
|
230
|
+
policy: CiReviewPolicy;
|
|
231
|
+
policyHash: string;
|
|
232
|
+
existing: readonly AdmissionRecord[];
|
|
233
|
+
reason: string;
|
|
234
|
+
priorScore: number | null;
|
|
235
|
+
blockingCount: number | null;
|
|
236
|
+
advisoryCount: number | null;
|
|
237
|
+
// A skip that spent nothing and must not count against ci_max_attempts
|
|
238
|
+
// (a /pr-hero stop): settled as "gate-skipped", like a size or budget skip.
|
|
239
|
+
unspent?: boolean;
|
|
240
|
+
}): Promise<void> {
|
|
241
|
+
try {
|
|
242
|
+
const state = await reserveCiAdmissionLedger({
|
|
243
|
+
operatorRoot: input.operatorRoot,
|
|
244
|
+
prNumber: input.prNumber,
|
|
245
|
+
headSha: input.headSha,
|
|
246
|
+
policy: input.policy,
|
|
247
|
+
policyHash: input.policyHash,
|
|
248
|
+
existing: input.existing,
|
|
249
|
+
decisionReason: input.reason,
|
|
250
|
+
priorScore: input.priorScore,
|
|
251
|
+
blockingCount: input.blockingCount,
|
|
252
|
+
advisoryCount: input.advisoryCount,
|
|
253
|
+
});
|
|
254
|
+
await settleCiAdmissionLedger(
|
|
255
|
+
state,
|
|
256
|
+
input.unspent === true ? "gate-skipped" : "skipped",
|
|
257
|
+
input.reason,
|
|
258
|
+
);
|
|
259
|
+
} catch {
|
|
260
|
+
// The skip notice is the operator-facing outcome; a check-run write must
|
|
261
|
+
// not block publishing it.
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
// S4 carry-forward (the owner's decision). A head an admission gate skips inherits the verdict of the
|
|
266
|
+
// last reviewed commit of this PR, so the required check reads what that review decided, never a green
|
|
267
|
+
// the review did not earn. The source is the PR's own commit statuses, never comment content: the walk
|
|
268
|
+
// (findCarriedVerdict) reads this PR's commits newest-first from the head, and takes the first commit
|
|
269
|
+
// whose status list carries a review's verdict (isReviewVerdictStatus). A skip, a fail-closed, a budget
|
|
270
|
+
// or a manual-only failure is not a verdict, so the walk passes over it.
|
|
271
|
+
//
|
|
272
|
+
// WHY the commit statuses and not the summary marker: a marker is a comment, and any commenter can post
|
|
273
|
+
// one naming a green head. Only a write-access actor can write a commit status, so a verdict read from
|
|
274
|
+
// the statuses is one a review wrote. The summary's head is never read for the carry.
|
|
275
|
+
// WHY the walk: a multi-commit push statuses only its tip, so the commits before it may carry no status.
|
|
276
|
+
// The walk skips those and stops at the first commit with a verdict, within CARRY_WALK_CAP commits.
|
|
277
|
+
// WHY a carried verdict may be carried again: a carried status on an earlier skipped head is a verdict a
|
|
278
|
+
// gate wrote from a real review, so chaining it keeps the verdict honest.
|
|
279
|
+
//
|
|
280
|
+
// WHY an override is not carried: an override is a human's decision for the code at that head only.
|
|
281
|
+
// The skipped head was never reviewed, so an override carried onto it would turn green with no review.
|
|
282
|
+
// WHY fail closed: no verdict within the cap, a failed read, a head missing from the PR's commits (a
|
|
283
|
+
// force-push, or a list cut short by GitHub's cap), or no reviewed head writes failure, so a trivial push
|
|
284
|
+
// cannot pass the gate without a verdict behind it.
|
|
285
|
+
// WHY manual-required is always failure: its code was never reviewed, so carrying a green verdict over it
|
|
286
|
+
// would be a bypass. Same-head writes nothing, because that head's own status already exists.
|
|
287
|
+
export type AdmissionStatusReason =
|
|
288
|
+
| "same-head"
|
|
289
|
+
| "once-per-pr"
|
|
290
|
+
| "below-threshold"
|
|
291
|
+
| "low-risk-delta"
|
|
292
|
+
| "stopped"
|
|
293
|
+
| "max-attempts-exhausted"
|
|
294
|
+
| "manual-only-policy";
|
|
295
|
+
|
|
296
|
+
export interface AdmissionStatusInput {
|
|
297
|
+
reason: AdmissionStatusReason;
|
|
298
|
+
// The head the summary names as reviewed; null when no summary names one, and a null here fails the
|
|
299
|
+
// carry closed with no read. The carry never reads this head's verdict: it comes from the PR's own
|
|
300
|
+
// commit statuses (findCarriedVerdict).
|
|
301
|
+
reviewedHead: string | null;
|
|
302
|
+
// The admission's own blocking-tier count for the reviewed head. It stands in only when the carried
|
|
303
|
+
// failure's description does not name a count. Every text isReviewVerdictStatus admits names one, so this
|
|
304
|
+
// is unreachable through the walk today; it stays because ci-admission-gate.ts still supplies it.
|
|
305
|
+
priorBlocking: number;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
export interface AdmissionStatusDecision {
|
|
309
|
+
state: "success" | "failure";
|
|
310
|
+
description: string;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
// The verdict a carried skip inherits: the commit it was found on, and that commit's status list.
|
|
314
|
+
export interface CarriedVerdict {
|
|
315
|
+
sha: string;
|
|
316
|
+
statuses: readonly CommitStatusFact[];
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
type CarriedReason =
|
|
320
|
+
| "once-per-pr"
|
|
321
|
+
| "below-threshold"
|
|
322
|
+
| "low-risk-delta"
|
|
323
|
+
| "stopped";
|
|
324
|
+
|
|
325
|
+
function isCarriedReason(
|
|
326
|
+
reason: AdmissionStatusReason,
|
|
327
|
+
): reason is CarriedReason {
|
|
328
|
+
return (
|
|
329
|
+
reason === "once-per-pr" ||
|
|
330
|
+
reason === "below-threshold" ||
|
|
331
|
+
reason === "low-risk-delta" ||
|
|
332
|
+
reason === "stopped"
|
|
333
|
+
);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
// The readers below rewrite a legacy `pr-hero` lead into `crysk` first (canonicalStatusText), so the
|
|
337
|
+
// patterns and the builders speak one spelling.
|
|
338
|
+
const BLOCKING_COUNT = /^crysk: (\d+) blocking finding\(s\)/;
|
|
339
|
+
|
|
340
|
+
function blockingCountIn(
|
|
341
|
+
description: string | null | undefined,
|
|
342
|
+
): number | null {
|
|
343
|
+
const match = BLOCKING_COUNT.exec(canonicalStatusText(description ?? ""));
|
|
344
|
+
if (match === null) return null;
|
|
345
|
+
const count = Number(match[1]);
|
|
346
|
+
return count > 0 ? count : null;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
const CARRIED_FROM = "carried from ";
|
|
350
|
+
// Reads the carried sha out of a carried text. It only proposes the sha: the builders below must then
|
|
351
|
+
// reproduce the whole text, so a prefix or a suffix alone never qualifies.
|
|
352
|
+
const CARRIED_SHA = new RegExp(`${CARRIED_FROM}([0-9a-f]{7})`);
|
|
353
|
+
|
|
354
|
+
// The text a skip writes when it inherits a finished review's success, for the review's label and the
|
|
355
|
+
// carried-from sha. The carry writes it, and isReviewVerdictStatus rebuilds it, so the two cannot drift.
|
|
356
|
+
export function carriedSuccessDescription(label: string, sha: string): string {
|
|
357
|
+
return clipStatusDescription(
|
|
358
|
+
`${STATUS_CONTEXT}: no review needed (${label}); ${CARRIED_FROM}${sha.slice(0, 7)}`,
|
|
359
|
+
);
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
// The text a skip writes when it inherits a finished review's blocking failure, with that review's count.
|
|
363
|
+
export function carriedBlockingDescription(count: number, sha: string): string {
|
|
364
|
+
return clipStatusDescription(
|
|
365
|
+
`${STATUS_CONTEXT}: ${count} blocking finding(s) ${CARRIED_FROM}${sha.slice(0, 7)} — fix and push, or /pr-hero override`,
|
|
366
|
+
);
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
// The text a skip writes when it inherits a finished review's partial verdict (some hunters failed).
|
|
370
|
+
export function carriedPartialDescription(
|
|
371
|
+
failed: number,
|
|
372
|
+
total: number,
|
|
373
|
+
sha: string,
|
|
374
|
+
set?: string,
|
|
375
|
+
): string {
|
|
376
|
+
const suffix = set === undefined ? "" : `, set ${set}`;
|
|
377
|
+
return clipStatusDescription(
|
|
378
|
+
`${STATUS_CONTEXT}: review partial (${failed} of ${total} hunters failed${suffix}) ${CARRIED_FROM}${sha.slice(0, 7)} — /pr-hero review retries it`,
|
|
379
|
+
);
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
const CARRY_REASONS: readonly CarriedReason[] = [
|
|
383
|
+
"once-per-pr",
|
|
384
|
+
"below-threshold",
|
|
385
|
+
"low-risk-delta",
|
|
386
|
+
"stopped",
|
|
387
|
+
];
|
|
388
|
+
|
|
389
|
+
function finishedSuccessMatches(rawDescription: string): boolean {
|
|
390
|
+
const description = canonicalStatusText(rawDescription);
|
|
391
|
+
return [true, false].some(
|
|
392
|
+
(posted) =>
|
|
393
|
+
commitStatusRequest({
|
|
394
|
+
phase: "success",
|
|
395
|
+
posted,
|
|
396
|
+
targetUrl: undefined,
|
|
397
|
+
context: COMMIT_STATUS_CONTEXT,
|
|
398
|
+
}).description === description,
|
|
399
|
+
);
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
function finishedBlockingMatches(rawDescription: string): boolean {
|
|
403
|
+
const description = canonicalStatusText(rawDescription);
|
|
404
|
+
const count = blockingCountIn(description);
|
|
405
|
+
return (
|
|
406
|
+
count !== null &&
|
|
407
|
+
blockingCommitStatusRequest({
|
|
408
|
+
blockingCount: count,
|
|
409
|
+
targetUrl: undefined,
|
|
410
|
+
context: COMMIT_STATUS_CONTEXT,
|
|
411
|
+
}).description === description
|
|
412
|
+
);
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
function carriedSha(description: string): string | null {
|
|
416
|
+
return CARRIED_SHA.exec(description)?.[1] ?? null;
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
function carriedSuccessMatches(rawDescription: string): boolean {
|
|
420
|
+
const description = canonicalStatusText(rawDescription);
|
|
421
|
+
const sha = carriedSha(description);
|
|
422
|
+
return (
|
|
423
|
+
sha !== null &&
|
|
424
|
+
CARRY_REASONS.some(
|
|
425
|
+
(reason) =>
|
|
426
|
+
carriedSuccessDescription(ciReviewSkipLabel(reason), sha) ===
|
|
427
|
+
description,
|
|
428
|
+
)
|
|
429
|
+
);
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
function carriedBlockingMatches(rawDescription: string): boolean {
|
|
433
|
+
const description = canonicalStatusText(rawDescription);
|
|
434
|
+
const sha = carriedSha(description);
|
|
435
|
+
const count = blockingCountIn(description);
|
|
436
|
+
return (
|
|
437
|
+
sha !== null &&
|
|
438
|
+
count !== null &&
|
|
439
|
+
carriedBlockingDescription(count, sha) === description
|
|
440
|
+
);
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
// S5: a partial review is a red verdict with its own text (N of M hunters failed). Its counts are read
|
|
444
|
+
// first, then the builder must reproduce the whole text, the same rule as the blocking verdict.
|
|
445
|
+
// W3-PARTIAL S2: the repeat form ('again') reads the same counts; the flag only picks the builder that must rebuild the text.
|
|
446
|
+
// W4-R363 S1: the failed-set key (", set abc123") is read when present; a text from before the key has none.
|
|
447
|
+
const PARTIAL_COUNTS =
|
|
448
|
+
/^crysk: review partial( again)? \((\d+) of (\d+) hunters failed(?:, set ([0-9a-f]{6}))?\)/;
|
|
449
|
+
|
|
450
|
+
export function partialCountsIn(
|
|
451
|
+
description: string | null | undefined,
|
|
452
|
+
): { failed: number; total: number; again: boolean; set?: string } | null {
|
|
453
|
+
const match = PARTIAL_COUNTS.exec(canonicalStatusText(description ?? ""));
|
|
454
|
+
if (match === null) return null;
|
|
455
|
+
return {
|
|
456
|
+
failed: Number(match[2]),
|
|
457
|
+
total: Number(match[3]),
|
|
458
|
+
again: match[1] !== undefined,
|
|
459
|
+
...(match[4] === undefined ? {} : { set: match[4] }),
|
|
460
|
+
};
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
// Exported for the teardown's repeat check (teardown.ts previousPartialIsSame): the one rule for a finished
|
|
464
|
+
// partial verdict, so a carried row never counts as a review's own verdict.
|
|
465
|
+
export function finishedPartialMatches(rawDescription: string): boolean {
|
|
466
|
+
const description = canonicalStatusText(rawDescription);
|
|
467
|
+
const counts = partialCountsIn(description);
|
|
468
|
+
if (counts === null) return false;
|
|
469
|
+
const rebuilt = counts.again
|
|
470
|
+
? partialReviewAgainDescription(counts.failed, counts.total, counts.set)
|
|
471
|
+
: partialReviewDescription(counts.failed, counts.total, counts.set);
|
|
472
|
+
return rebuilt === description;
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
function carriedPartialMatches(rawDescription: string): boolean {
|
|
476
|
+
const description = canonicalStatusText(rawDescription);
|
|
477
|
+
const sha = carriedSha(description);
|
|
478
|
+
const counts = partialCountsIn(description);
|
|
479
|
+
return (
|
|
480
|
+
sha !== null &&
|
|
481
|
+
counts !== null &&
|
|
482
|
+
carriedPartialDescription(counts.failed, counts.total, sha, counts.set) ===
|
|
483
|
+
description
|
|
484
|
+
);
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
// WHY the walk takes only these: a skip, a fail-closed, a budget or a manual-only failure is written with
|
|
488
|
+
// no review behind it. Counting it as a verdict would stop the walk on it, so a green review on an older
|
|
489
|
+
// commit would be lost, and one skip would turn the gate red until a human acts. Overrides need no check of
|
|
490
|
+
// their own: an override's text is none of the texts below.
|
|
491
|
+
// WHY each text is rebuilt rather than matched by a pattern: the writer of a verdict is the only authority
|
|
492
|
+
// on its exact text. The variable parts (a count, a carried sha) are read first, then the same builder must
|
|
493
|
+
// reproduce the whole description.
|
|
494
|
+
// WHY no context check here: the walk reads only the gate's own context (newestCarriedVerdict).
|
|
495
|
+
export function isReviewVerdictStatus(
|
|
496
|
+
status: Pick<CommitStatusFact, "state" | "description">,
|
|
497
|
+
): boolean {
|
|
498
|
+
const description = status.description;
|
|
499
|
+
if (typeof description !== "string") return false;
|
|
500
|
+
if (status.state === "success") {
|
|
501
|
+
return (
|
|
502
|
+
finishedSuccessMatches(description) || carriedSuccessMatches(description)
|
|
503
|
+
);
|
|
504
|
+
}
|
|
505
|
+
if (status.state === "failure") {
|
|
506
|
+
return (
|
|
507
|
+
finishedBlockingMatches(description) ||
|
|
508
|
+
carriedBlockingMatches(description) ||
|
|
509
|
+
finishedPartialMatches(description) ||
|
|
510
|
+
carriedPartialMatches(description)
|
|
511
|
+
);
|
|
512
|
+
}
|
|
513
|
+
return false;
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
// The newest verdict on the head: a review's verdict of the gate's own context (isReviewVerdictStatus),
|
|
517
|
+
// newest by created_at, the rule every other reader of this context uses (newestStatusInContext).
|
|
518
|
+
function newestCarriedVerdict(statuses: readonly CommitStatusFact[]) {
|
|
519
|
+
return newestStatusInContext(
|
|
520
|
+
statuses.filter((status) => isReviewVerdictStatus(status)),
|
|
521
|
+
COMMIT_STATUS_CONTEXT,
|
|
522
|
+
);
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
function failClosed(label: string): AdmissionStatusDecision {
|
|
526
|
+
return {
|
|
527
|
+
state: "failure",
|
|
528
|
+
description: clipStatusDescription(
|
|
529
|
+
`${STATUS_CONTEXT} skipped the review (${label}) and found no earlier verdict — /pr-hero review reviews it`,
|
|
530
|
+
),
|
|
531
|
+
};
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
// What the gate's skip writes on the new head, or null for no write. `carried` is the verdict the skip
|
|
535
|
+
// inherits (findCarriedVerdict), or null when none was read or found. A reason that never carries reads none.
|
|
536
|
+
export function admissionSkipDecision(
|
|
537
|
+
input: AdmissionStatusInput & { carried: CarriedVerdict | null },
|
|
538
|
+
): AdmissionStatusDecision | null {
|
|
539
|
+
switch (input.reason) {
|
|
540
|
+
case "same-head":
|
|
541
|
+
return null;
|
|
542
|
+
case "max-attempts-exhausted":
|
|
543
|
+
return {
|
|
544
|
+
state: "failure",
|
|
545
|
+
description: clipStatusDescription(
|
|
546
|
+
`${STATUS_CONTEXT}: review budget for this PR is used up — /pr-hero review reviews this head`,
|
|
547
|
+
),
|
|
548
|
+
};
|
|
549
|
+
case "manual-only-policy":
|
|
550
|
+
return {
|
|
551
|
+
state: "failure",
|
|
552
|
+
description: clipStatusDescription(
|
|
553
|
+
`${STATUS_CONTEXT}: review needs a manual trigger (ci_review_policy is manual_only) — /pr-hero review reviews this head`,
|
|
554
|
+
),
|
|
555
|
+
};
|
|
556
|
+
case "once-per-pr":
|
|
557
|
+
case "below-threshold":
|
|
558
|
+
case "low-risk-delta":
|
|
559
|
+
case "stopped":
|
|
560
|
+
break;
|
|
561
|
+
}
|
|
562
|
+
const label = ciReviewSkipLabel(input.reason);
|
|
563
|
+
if (input.carried === null) return failClosed(label);
|
|
564
|
+
const verdict = newestCarriedVerdict(input.carried.statuses);
|
|
565
|
+
if (verdict === undefined) return failClosed(label);
|
|
566
|
+
if (verdict.state === "success") {
|
|
567
|
+
return {
|
|
568
|
+
state: "success",
|
|
569
|
+
description: carriedSuccessDescription(label, input.carried.sha),
|
|
570
|
+
};
|
|
571
|
+
}
|
|
572
|
+
// S5: a partial verdict is red, and it is never read as a blocking count (it has none). Taking the
|
|
573
|
+
// admission's own priorBlocking here would turn a partial review into a count it never had.
|
|
574
|
+
const partial = partialCountsIn(verdict.description);
|
|
575
|
+
if (partial !== null) {
|
|
576
|
+
return {
|
|
577
|
+
state: "failure",
|
|
578
|
+
description: carriedPartialDescription(
|
|
579
|
+
partial.failed,
|
|
580
|
+
partial.total,
|
|
581
|
+
input.carried.sha,
|
|
582
|
+
partial.set,
|
|
583
|
+
),
|
|
584
|
+
};
|
|
585
|
+
}
|
|
586
|
+
const blocking =
|
|
587
|
+
blockingCountIn(verdict.description) ??
|
|
588
|
+
(input.priorBlocking > 0 ? input.priorBlocking : null);
|
|
589
|
+
if (blocking === null) return failClosed(label);
|
|
590
|
+
return {
|
|
591
|
+
state: "failure",
|
|
592
|
+
description: carriedBlockingDescription(blocking, input.carried.sha),
|
|
593
|
+
};
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
// The most earlier commits the carry reads for a verdict. A push on top of a longer run of commits with
|
|
597
|
+
// no verdict fails closed rather than walking the whole PR.
|
|
598
|
+
const CARRY_WALK_CAP = 20;
|
|
599
|
+
|
|
600
|
+
// The carry's verdict source: the newest earlier commit of this PR whose status list carries a verdict,
|
|
601
|
+
// or null. GitHub lists the commits oldest first; the walk takes the commits before the head, newest first.
|
|
602
|
+
// WHY the head's index, not the list's last element: the walk starts from the head this gate evaluates, so
|
|
603
|
+
// a head that moved on, or one missing from the list, cannot send the walk to the wrong commits. A head
|
|
604
|
+
// missing from the list fails closed.
|
|
605
|
+
// WHY a failed read ends the walk: a commit whose statuses did not read is not a commit with no verdict,
|
|
606
|
+
// so skipping it would carry an older verdict across a read that never happened.
|
|
607
|
+
async function findCarriedVerdict(input: {
|
|
608
|
+
operatorRoot: string;
|
|
609
|
+
prNumber: number;
|
|
610
|
+
headSha: string;
|
|
611
|
+
spawnFn?: typeof Bun.spawn;
|
|
612
|
+
listCommits: typeof fetchPrCommitShas;
|
|
613
|
+
readStatuses: typeof fetchCommitStatusList;
|
|
614
|
+
}): Promise<CarriedVerdict | null> {
|
|
615
|
+
const shas = await input.listCommits(input.operatorRoot, input.prNumber, {
|
|
616
|
+
spawnFn: input.spawnFn,
|
|
617
|
+
});
|
|
618
|
+
if (shas === null) return null;
|
|
619
|
+
const headIndex = shas.indexOf(input.headSha);
|
|
620
|
+
if (headIndex < 0) return null;
|
|
621
|
+
const earlier = shas.slice(0, headIndex).reverse().slice(0, CARRY_WALK_CAP);
|
|
622
|
+
for (const sha of earlier) {
|
|
623
|
+
const statuses = await input.readStatuses(input.operatorRoot, sha, {
|
|
624
|
+
spawnFn: input.spawnFn,
|
|
625
|
+
});
|
|
626
|
+
if (statuses === null) return null;
|
|
627
|
+
if (newestCarriedVerdict(statuses) !== undefined) return { sha, statuses };
|
|
628
|
+
}
|
|
629
|
+
return null;
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
// WHY the head's override holds: a human's `/pr-hero override` on this head is the decision for it. A
|
|
633
|
+
// re-run or reopen of the gate on that unchanged head would otherwise write the skip's verdict over it.
|
|
634
|
+
// So where the newest pr-hero status on the head is an override, the skip writes nothing. This holds when
|
|
635
|
+
// the head's statuses can be read.
|
|
636
|
+
// WHY the read is strict: the reader is asked with failClosed, so a failed read throws instead of looking
|
|
637
|
+
// like a head with no rows (fetchCommitStatuses, src/pr/pr.ts).
|
|
638
|
+
// WHY a failed read still writes the status: on a failed read we cannot tell whether an override stands, so
|
|
639
|
+
// the skip's status is written as planned. That is a tradeoff, not a guarantee: this write can replace an
|
|
640
|
+
// override on the head that we could not read. Leaving a fresh head with no status is the original bug, a
|
|
641
|
+
// head that reads as nothing decided, so we take the rarer overwrite and log a warning that says so.
|
|
642
|
+
async function overrideStandsOnHead(input: {
|
|
643
|
+
operatorRoot: string;
|
|
644
|
+
headSha: string;
|
|
645
|
+
spawnFn?: typeof Bun.spawn;
|
|
646
|
+
readHeadStatus: typeof fetchCommitStatuses;
|
|
647
|
+
}): Promise<boolean> {
|
|
648
|
+
let statuses: readonly CommitStatusFact[];
|
|
649
|
+
try {
|
|
650
|
+
statuses = await input.readHeadStatus(input.operatorRoot, input.headSha, {
|
|
651
|
+
spawnFn: input.spawnFn,
|
|
652
|
+
failClosed: true,
|
|
653
|
+
});
|
|
654
|
+
} catch {
|
|
655
|
+
log(
|
|
656
|
+
formatWorkflowCommand(
|
|
657
|
+
"warning",
|
|
658
|
+
`crysk: could not read the statuses of ${input.headSha.slice(0, 7)}, so an override on it cannot be checked; the skip's status is written as planned`,
|
|
659
|
+
),
|
|
660
|
+
);
|
|
661
|
+
return false;
|
|
662
|
+
}
|
|
663
|
+
const newest = newestStatusInContext(statuses, COMMIT_STATUS_CONTEXT);
|
|
664
|
+
return newest !== undefined && isOverrideStatus(newest);
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
// The status a gate-mode skip writes, or null for none. A size or budget skip is a fixed failure.
|
|
668
|
+
// An admission skip or manual-required is decided by admissionSkipDecision; the carry's walk runs only
|
|
669
|
+
// when the verdict carries forward, so same-head and manual-required make no read.
|
|
670
|
+
async function gateSkipDecision(
|
|
671
|
+
input: {
|
|
672
|
+
operatorRoot: string;
|
|
673
|
+
prNumber: number;
|
|
674
|
+
headSha: string;
|
|
675
|
+
spawnFn?: typeof Bun.spawn;
|
|
676
|
+
admission?: AdmissionStatusInput;
|
|
677
|
+
readStatuses?: typeof fetchCommitStatusList;
|
|
678
|
+
listCommits?: typeof fetchPrCommitShas;
|
|
679
|
+
},
|
|
680
|
+
skip: CliRecord["skip"],
|
|
681
|
+
): Promise<AdmissionStatusDecision | null> {
|
|
682
|
+
if (skip === "size" || skip === "budget") {
|
|
683
|
+
const request = skipCommitStatusRequest({
|
|
684
|
+
kind: skip,
|
|
685
|
+
targetUrl: undefined,
|
|
686
|
+
context: COMMIT_STATUS_CONTEXT,
|
|
687
|
+
});
|
|
688
|
+
// skipCommitStatusRequest only ever builds a failure; the literal narrows its type.
|
|
689
|
+
return { state: "failure", description: request.description };
|
|
690
|
+
}
|
|
691
|
+
if (input.admission === undefined) return null;
|
|
692
|
+
const { reason, reviewedHead } = input.admission;
|
|
693
|
+
// WHY the summary's presence still gates the walk: a skip whose summary names no reviewed head fails
|
|
694
|
+
// closed with no read, as it always has. The carried head is never taken from the summary.
|
|
695
|
+
const carried =
|
|
696
|
+
isCarriedReason(reason) && reviewedHead !== null
|
|
697
|
+
? await findCarriedVerdict({
|
|
698
|
+
operatorRoot: input.operatorRoot,
|
|
699
|
+
prNumber: input.prNumber,
|
|
700
|
+
headSha: input.headSha,
|
|
701
|
+
spawnFn: input.spawnFn,
|
|
702
|
+
listCommits: input.listCommits ?? fetchPrCommitShas,
|
|
703
|
+
readStatuses: input.readStatuses ?? fetchCommitStatusList,
|
|
704
|
+
})
|
|
705
|
+
: null;
|
|
706
|
+
return admissionSkipDecision({ ...input.admission, carried });
|
|
707
|
+
}
|
|
708
|
+
|
|
709
|
+
// The skip kind a plan stands for, read from the status its outputs carry. The
|
|
710
|
+
// status is a plain string in CiOutputs, so an unknown one yields no kind and no
|
|
711
|
+
// record: a skip the publisher cannot name is not a skip it may report.
|
|
712
|
+
function cliSkipKindOf(plan: CiGateSkipPlan): CliRecord["skip"] {
|
|
713
|
+
switch (plan.outputs.status) {
|
|
714
|
+
case "skipped-size":
|
|
715
|
+
return "size";
|
|
716
|
+
case "skipped-budget":
|
|
717
|
+
return "budget";
|
|
718
|
+
case "skipped-coverage":
|
|
719
|
+
return "coverage";
|
|
720
|
+
case "manual-required":
|
|
721
|
+
return "manual-required";
|
|
722
|
+
default:
|
|
723
|
+
return undefined;
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
export async function publishCiSkip(input: {
|
|
728
|
+
operatorRoot: string;
|
|
729
|
+
prNumber: number;
|
|
730
|
+
post: boolean;
|
|
731
|
+
isCi: boolean;
|
|
732
|
+
stepSummaryFlag: boolean | undefined;
|
|
733
|
+
plan: CiGateSkipPlan;
|
|
734
|
+
noticeMessage: string;
|
|
735
|
+
// S4. In gate mode with a known head, a size or budget skip posts a failure
|
|
736
|
+
// status. Absent means no status, which is every caller but the two gates.
|
|
737
|
+
ciMode?: CiMode;
|
|
738
|
+
headSha?: string;
|
|
739
|
+
// The gate's admission skip or manual-required verdict (S4 carry-forward, gate mode only).
|
|
740
|
+
// Absent means no status: advisory, a non-CI run, and every caller but the gate's two admission skips.
|
|
741
|
+
admission?: AdmissionStatusInput;
|
|
742
|
+
// The run's spawnFn, threaded to the status read, the status write and the repo lookup.
|
|
743
|
+
spawnFn?: typeof Bun.spawn;
|
|
744
|
+
// Test-only seams (defaults: the real repostMarkedPrComment, publishFinalCommitStatus,
|
|
745
|
+
// fetchCommitStatusList for each commit's statuses, fetchPrCommitShas for the PR's commits, and
|
|
746
|
+
// fetchCommitStatuses for the head's override check).
|
|
747
|
+
repost?: typeof repostMarkedPrComment;
|
|
748
|
+
postStatus?: typeof publishFinalCommitStatus;
|
|
749
|
+
repoWebUrl?: typeof ghRepoWebUrl;
|
|
750
|
+
readStatuses?: typeof fetchCommitStatusList;
|
|
751
|
+
listCommits?: typeof fetchPrCommitShas;
|
|
752
|
+
readHeadStatus?: typeof fetchCommitStatuses;
|
|
753
|
+
}): Promise<number> {
|
|
754
|
+
log(formatWorkflowCommand("notice", input.noticeMessage));
|
|
755
|
+
// The record names the skip kind only. The plan carries no head, so none is
|
|
756
|
+
// written: the publisher takes the head from the trigger's record.
|
|
757
|
+
const skip = cliSkipKindOf(input.plan);
|
|
758
|
+
if (input.post) {
|
|
759
|
+
// Repost, never edit in place: an edited notice is invisible on GitHub,
|
|
760
|
+
// so a later push that was skipped again looked like nothing happened.
|
|
761
|
+
// See repostMarkedPrComment's WHY. A repost that throws is the notice's own
|
|
762
|
+
// failure, so its record names the step, and the error is rethrown unchanged.
|
|
763
|
+
try {
|
|
764
|
+
await (input.repost ?? repostMarkedPrComment)({
|
|
765
|
+
operatorRoot: input.operatorRoot,
|
|
766
|
+
pr: input.prNumber,
|
|
767
|
+
body: input.plan.comment,
|
|
768
|
+
markerPrefix: input.plan.markerPrefix,
|
|
769
|
+
});
|
|
770
|
+
} catch (error) {
|
|
771
|
+
writeCliRecord({
|
|
772
|
+
v: 1,
|
|
773
|
+
outcome: "error",
|
|
774
|
+
cls: classifyCliError(error),
|
|
775
|
+
params: { step: "posting the skip notice" },
|
|
776
|
+
});
|
|
777
|
+
throw error;
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
// S4: the head's status is part of "this skip reached the PR", so it is
|
|
781
|
+
// written before the record. A failed write rethrows, leaving no skipped
|
|
782
|
+
// record behind. Gate mode only: a size or budget skip is a fixed failure, and
|
|
783
|
+
// an admission skip or manual-required writes the verdict admissionSkipDecision
|
|
784
|
+
// reaches (the carry-forward above).
|
|
785
|
+
const decision =
|
|
786
|
+
input.ciMode === "gate" && input.headSha !== undefined
|
|
787
|
+
? await gateSkipDecision({ ...input, headSha: input.headSha }, skip)
|
|
788
|
+
: null;
|
|
789
|
+
// WHY the override is read before the write: a re-run or reopen of the gate on an overridden head would
|
|
790
|
+
// otherwise write this skip's verdict over the human's decision. The notice, record and outputs still
|
|
791
|
+
// write; only the status is held back, and one line says why.
|
|
792
|
+
const headOverridden =
|
|
793
|
+
decision !== null && input.headSha !== undefined
|
|
794
|
+
? await overrideStandsOnHead({
|
|
795
|
+
operatorRoot: input.operatorRoot,
|
|
796
|
+
headSha: input.headSha,
|
|
797
|
+
spawnFn: input.spawnFn,
|
|
798
|
+
readHeadStatus: input.readHeadStatus ?? fetchCommitStatuses,
|
|
799
|
+
})
|
|
800
|
+
: false;
|
|
801
|
+
if (headOverridden && input.headSha !== undefined) {
|
|
802
|
+
log(
|
|
803
|
+
`crysk: the override on ${input.headSha.slice(0, 7)} stands; no status is written over it`,
|
|
804
|
+
);
|
|
805
|
+
}
|
|
806
|
+
if (decision !== null && input.headSha !== undefined && !headOverridden) {
|
|
807
|
+
// The run's pull URL, like every other status the CLI writes (T3 advisory
|
|
808
|
+
// 3): a skip is a red check, and the link is how a reader reaches the PR.
|
|
809
|
+
// Best effort: ghRepoWebUrl answers undefined when it cannot, and the status
|
|
810
|
+
// still lands, without a link.
|
|
811
|
+
const targetUrl = prHtmlUrl(
|
|
812
|
+
await (
|
|
813
|
+
input.repoWebUrl ??
|
|
814
|
+
((root: string) => ghRepoWebUrl(root, { spawnFn: input.spawnFn }))
|
|
815
|
+
)(input.operatorRoot),
|
|
816
|
+
input.prNumber,
|
|
817
|
+
);
|
|
818
|
+
// Gate-only, and gate implies `--ci` (resolveCiPosture), so this is the gate's
|
|
819
|
+
// own context: the one skip write that names pr-hero.
|
|
820
|
+
await (input.postStatus ?? publishFinalCommitStatus)(
|
|
821
|
+
input.operatorRoot,
|
|
822
|
+
input.headSha,
|
|
823
|
+
{
|
|
824
|
+
state: decision.state,
|
|
825
|
+
description: decision.description,
|
|
826
|
+
context: COMMIT_STATUS_CONTEXT,
|
|
827
|
+
targetUrl,
|
|
828
|
+
},
|
|
829
|
+
input.spawnFn,
|
|
830
|
+
);
|
|
831
|
+
}
|
|
832
|
+
// Written only once the notice is on the PR, or once the log line is out when
|
|
833
|
+
// posting is off: the record is what the publisher reads as "this skip was
|
|
834
|
+
// reported", so it must not come before the repost it claims.
|
|
835
|
+
if (skip !== undefined) {
|
|
836
|
+
writeCliRecord({ v: 1, outcome: "skipped", skip });
|
|
837
|
+
}
|
|
838
|
+
const summaryPath = process.env.GITHUB_STEP_SUMMARY;
|
|
839
|
+
if (shouldWriteStepSummary(input.isCi, input.stepSummaryFlag, summaryPath)) {
|
|
840
|
+
await appendStepSummary(summaryPath as string, input.plan.summaryMarkdown);
|
|
841
|
+
}
|
|
842
|
+
const outputPath = process.env.GITHUB_OUTPUT;
|
|
843
|
+
if (shouldWriteCiOutputs(input.isCi, outputPath)) {
|
|
844
|
+
await appendCiOutputs(outputPath as string, input.plan.outputs);
|
|
845
|
+
}
|
|
846
|
+
return 0;
|
|
847
|
+
}
|