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,910 @@
|
|
|
1
|
+
// CI review admission (Pillar 3): decides whether a CI run should spend on
|
|
2
|
+
// this push BEFORE fetch/worktree/pipeline. Pure — no I/O. Re-review internals
|
|
3
|
+
// (case machine, verify leg) are unchanged; when admission says run, they
|
|
4
|
+
// behave exactly as today.
|
|
5
|
+
//
|
|
6
|
+
// Policy default: risk_aware mode, at most two attempts per PR (initial +
|
|
7
|
+
// one re-review), and a re-review when prior findings score >= 4 with
|
|
8
|
+
// blocking×2 + advisory×1 or when the delta touches prior finding paths.
|
|
9
|
+
// Same-head dedup applies per head SHA; maxAttempts applies per PR.
|
|
10
|
+
|
|
11
|
+
import { createHash } from "node:crypto";
|
|
12
|
+
import { PR_FINDING_MARKER_PREFIX, parseFindingMarker } from "#pr/preflight";
|
|
13
|
+
import type { RecoveredTier } from "#rereview/classify";
|
|
14
|
+
import type { ParsedStateBlock } from "#rereview/state";
|
|
15
|
+
import type { Tier } from "#review/findings";
|
|
16
|
+
import type { LocalConfig } from "#review/preflight";
|
|
17
|
+
import {
|
|
18
|
+
findMarker,
|
|
19
|
+
isCommand,
|
|
20
|
+
legacyCommand,
|
|
21
|
+
MARKER_BRAND_PATTERN,
|
|
22
|
+
MARKER_OPEN,
|
|
23
|
+
matchMarkerPrefix,
|
|
24
|
+
} from "../brand";
|
|
25
|
+
import {
|
|
26
|
+
type AdmissionRecord,
|
|
27
|
+
countTerminalAttempts,
|
|
28
|
+
PROVABLY_UNSPENT_STATUSES,
|
|
29
|
+
} from "./admission-ledger";
|
|
30
|
+
import type { CiReviewPolicyMode } from "./review-policy";
|
|
31
|
+
import {
|
|
32
|
+
type DeltaRiskAssessment,
|
|
33
|
+
deltaRiskTriggersReview,
|
|
34
|
+
} from "./review-risk";
|
|
35
|
+
import { parseReviewCommand, REVIEW_COMMAND, STOP_COMMAND } from "./trigger";
|
|
36
|
+
|
|
37
|
+
export const CI_REVIEW_POLICY_SCHEMA_VERSION = 1;
|
|
38
|
+
export const DEFAULT_CI_REVIEW_POLICY_MODE = "risk_aware" as const;
|
|
39
|
+
export const DEFAULT_CI_MAX_ATTEMPTS = 2;
|
|
40
|
+
/** @deprecated Use {@link DEFAULT_CI_MAX_ATTEMPTS}. */
|
|
41
|
+
export const DEFAULT_CI_MAX_REVIEWS = DEFAULT_CI_MAX_ATTEMPTS;
|
|
42
|
+
export const DEFAULT_CI_REREVIEW_MIN_SCORE = 4;
|
|
43
|
+
export const DEFAULT_CI_BLOCKING_WEIGHT = 2;
|
|
44
|
+
export const DEFAULT_CI_ADVISORY_WEIGHT = 1;
|
|
45
|
+
export const DEFAULT_CI_RESERVATION_TTL_SECONDS = 3600;
|
|
46
|
+
|
|
47
|
+
export interface CiReviewPolicy {
|
|
48
|
+
schemaVersion: typeof CI_REVIEW_POLICY_SCHEMA_VERSION;
|
|
49
|
+
mode: CiReviewPolicyMode;
|
|
50
|
+
/** Per-PR automatic attempt budget (same-head dedup is per head SHA). */
|
|
51
|
+
maxAttempts: number;
|
|
52
|
+
rereviewMinScore: number;
|
|
53
|
+
blockingWeight: number;
|
|
54
|
+
advisoryWeight: number;
|
|
55
|
+
reservationTtlSeconds: number;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export type CiReviewSkipReason =
|
|
59
|
+
| "same-head"
|
|
60
|
+
| "below-threshold"
|
|
61
|
+
| "low-risk-delta"
|
|
62
|
+
| "once-per-pr"
|
|
63
|
+
| "stopped";
|
|
64
|
+
|
|
65
|
+
export type CiReviewManualRequiredReason =
|
|
66
|
+
| "max-attempts-exhausted"
|
|
67
|
+
| "manual-only-policy";
|
|
68
|
+
|
|
69
|
+
export interface PriorTierScore {
|
|
70
|
+
blocking: number;
|
|
71
|
+
advisory: number;
|
|
72
|
+
score: number;
|
|
73
|
+
source: "state" | "admission" | "posted-findings" | "none";
|
|
74
|
+
// Markers for the prior head exist but tier lines could not be read — run
|
|
75
|
+
// rather than treat unknown as zero (silent-loss direction).
|
|
76
|
+
failOpen?: boolean;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export interface PostedFindingAdmissionScan {
|
|
80
|
+
blocking: number;
|
|
81
|
+
advisory: number;
|
|
82
|
+
matchedMarkers: number;
|
|
83
|
+
parsedTiers: number;
|
|
84
|
+
// Markers for the prior head exist from untrusted actors only — run rather
|
|
85
|
+
// than let forged comments drive a skip.
|
|
86
|
+
failOpen?: boolean;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export interface CiReviewAdmissionInput {
|
|
90
|
+
currentHead: string;
|
|
91
|
+
summaryHead: string | null;
|
|
92
|
+
markerSeen: boolean;
|
|
93
|
+
reviewCount: number;
|
|
94
|
+
state: ParsedStateBlock | null;
|
|
95
|
+
admission: ParsedCiAdmissionBlock | null;
|
|
96
|
+
postedFindings: PostedFindingAdmissionScan | null;
|
|
97
|
+
policy: CiReviewPolicy;
|
|
98
|
+
// New commits since the prior summary touch a path where a prior finding
|
|
99
|
+
// was posted — bypass the below-threshold skip in risk_aware mode.
|
|
100
|
+
deltaTouchesPriorFindings: boolean;
|
|
101
|
+
// Current-head delta risk classification (WU-04). Absent when not computed.
|
|
102
|
+
deltaRisk?: DeltaRiskAssessment | null;
|
|
103
|
+
// Mismatched summary/state/admission heads or truncated comment fetch —
|
|
104
|
+
// run rather than trust a forged or incomplete prior score.
|
|
105
|
+
authorityFailOpen?: boolean;
|
|
106
|
+
// Explicit manual override (--force). When true, always return run.
|
|
107
|
+
forceOverride?: boolean;
|
|
108
|
+
// Whether the run that posted summaryHead finished (its marker carried no
|
|
109
|
+
// coverage=partial token). Absent means complete, so every existing caller
|
|
110
|
+
// keeps today's verdicts. A partial review on the current head is a floor,
|
|
111
|
+
// not a verdict: it must not satisfy the same-head skip, or a CI re-run of
|
|
112
|
+
// that commit is skipped before the re-review can force full coverage.
|
|
113
|
+
summaryComplete?: boolean;
|
|
114
|
+
// A write-access maintainer posted `/crysk stop` and no review or recheck
|
|
115
|
+
// command came after it. The gate decides it (it owns the permission
|
|
116
|
+
// lookup); absent means not stopped, so every existing caller keeps today's
|
|
117
|
+
// verdicts.
|
|
118
|
+
stopped?: boolean;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
type CiReviewAdmissionTerminalFields = {
|
|
122
|
+
prior: PriorTierScore;
|
|
123
|
+
reviewCount: number;
|
|
124
|
+
maxAttempts: number;
|
|
125
|
+
minScore: number;
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
export type CiReviewAdmissionVerdict =
|
|
129
|
+
| { action: "run" }
|
|
130
|
+
| ({
|
|
131
|
+
action: "skip";
|
|
132
|
+
reason: CiReviewSkipReason;
|
|
133
|
+
} & CiReviewAdmissionTerminalFields)
|
|
134
|
+
| ({
|
|
135
|
+
action: "manual-required";
|
|
136
|
+
reason: CiReviewManualRequiredReason;
|
|
137
|
+
} & CiReviewAdmissionTerminalFields);
|
|
138
|
+
|
|
139
|
+
export function resolveCiReviewPolicy(
|
|
140
|
+
config: Pick<
|
|
141
|
+
LocalConfig,
|
|
142
|
+
| "ci_review_policy"
|
|
143
|
+
| "ci_max_attempts"
|
|
144
|
+
| "ci_max_reviews"
|
|
145
|
+
| "ci_rereview_min_score"
|
|
146
|
+
| "ci_blocking_weight"
|
|
147
|
+
| "ci_advisory_weight"
|
|
148
|
+
>,
|
|
149
|
+
): CiReviewPolicy {
|
|
150
|
+
return {
|
|
151
|
+
schemaVersion: CI_REVIEW_POLICY_SCHEMA_VERSION,
|
|
152
|
+
mode: config.ci_review_policy ?? DEFAULT_CI_REVIEW_POLICY_MODE,
|
|
153
|
+
maxAttempts:
|
|
154
|
+
config.ci_max_attempts ??
|
|
155
|
+
config.ci_max_reviews ??
|
|
156
|
+
DEFAULT_CI_MAX_ATTEMPTS,
|
|
157
|
+
rereviewMinScore:
|
|
158
|
+
config.ci_rereview_min_score ?? DEFAULT_CI_REREVIEW_MIN_SCORE,
|
|
159
|
+
blockingWeight: config.ci_blocking_weight ?? DEFAULT_CI_BLOCKING_WEIGHT,
|
|
160
|
+
advisoryWeight: config.ci_advisory_weight ?? DEFAULT_CI_ADVISORY_WEIGHT,
|
|
161
|
+
reservationTtlSeconds: DEFAULT_CI_RESERVATION_TTL_SECONDS,
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export function ciReviewPolicyHash(policy: CiReviewPolicy): string {
|
|
166
|
+
const canonical = JSON.stringify(policy, Object.keys(policy).sort());
|
|
167
|
+
return createHash("sha256").update(canonical).digest("hex").slice(0, 16);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// GITHUB_ACTOR in Actions plus optional repo-config extras. Undefined when no
|
|
171
|
+
// actor is known — callers treat that as "do not filter by author".
|
|
172
|
+
export function resolveCiTrustedActors(input: {
|
|
173
|
+
githubActor?: string;
|
|
174
|
+
extra?: readonly string[];
|
|
175
|
+
}): ReadonlySet<string> | undefined {
|
|
176
|
+
const actors = new Set<string>();
|
|
177
|
+
if (input.githubActor !== undefined && input.githubActor.length > 0) {
|
|
178
|
+
actors.add(input.githubActor);
|
|
179
|
+
}
|
|
180
|
+
for (const login of input.extra ?? []) {
|
|
181
|
+
if (login.length > 0) actors.add(login);
|
|
182
|
+
}
|
|
183
|
+
return actors.size > 0 ? actors : undefined;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
export function stateReviewCount(
|
|
187
|
+
state: ParsedStateBlock | null,
|
|
188
|
+
markerSeen: boolean,
|
|
189
|
+
admission: ParsedCiAdmissionBlock | null = null,
|
|
190
|
+
): number {
|
|
191
|
+
if (
|
|
192
|
+
state?.reviews !== undefined &&
|
|
193
|
+
Number.isInteger(state.reviews) &&
|
|
194
|
+
state.reviews >= 1
|
|
195
|
+
) {
|
|
196
|
+
return state.reviews;
|
|
197
|
+
}
|
|
198
|
+
if (admission !== null) {
|
|
199
|
+
return admission.reviews;
|
|
200
|
+
}
|
|
201
|
+
return markerSeen ? 1 : 0;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// Workflow runs that completed (success or failure) may have consumed budget
|
|
205
|
+
// without posting — take the max of the summary counter and distinct run heads.
|
|
206
|
+
export function resolveReviewAttemptCount(input: {
|
|
207
|
+
stateCount: number;
|
|
208
|
+
workflowHeads: ReadonlySet<string>;
|
|
209
|
+
}): number {
|
|
210
|
+
return Math.max(input.stateCount, input.workflowHeads.size);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// #326: a workflow head counts toward the attempt budget only when its
|
|
214
|
+
// pr-hero run could have spent. The run list cannot tell them apart, because a
|
|
215
|
+
// gate skip exits 0 and concludes `success`. So each head's ledger rows decide:
|
|
216
|
+
// a head is dropped only when it has ledger rows and EVERY row is provably
|
|
217
|
+
// unspent (PROVABLY_UNSPENT_STATUSES: gate-skipped, yielded) or forced (a
|
|
218
|
+
// comment-triggered run is outside the automatic budget). A `cancelled` row
|
|
219
|
+
// is not proof: a cancel signal can land after the provider started, and a
|
|
220
|
+
// `timeout-minutes` kill concludes the run `failure`, which this list does
|
|
221
|
+
// include, so that head stays counted. (A run GitHub concludes `cancelled`
|
|
222
|
+
// never reaches this function, because ghPrHeroWorkflowRunHeads keeps only
|
|
223
|
+
// success and failure, COMPLETED_WORKFLOW_CONCLUSIONS.) A `reserved` row is not
|
|
224
|
+
// proof either, because its provider-started settle is best-effort and the run
|
|
225
|
+
// may have spent anyway, so that head stays counted, and so does any row with a
|
|
226
|
+
// status this engine does not know. A head with no
|
|
227
|
+
// ledger row at all stays counted, because it is a run older than the ledger
|
|
228
|
+
// and the run list is the only evidence it spent. An unreadable ledger reads as
|
|
229
|
+
// no rows (listAdmissionCheckRuns fails open to []), which keeps the head
|
|
230
|
+
// counted, the pre-fix behavior. So a failed read can only over-count and
|
|
231
|
+
// refuse a review early; it never admits one the budget would have refused.
|
|
232
|
+
// A forced row (a `/crysk review` or `/crysk recheck` comment run) also
|
|
233
|
+
// leaves the count whatever its status, so a head whose rows are all forced is
|
|
234
|
+
// dropped as well. A head with one automatic row next to a forced one stays
|
|
235
|
+
// counted, through that automatic row.
|
|
236
|
+
export function countableWorkflowHeads(
|
|
237
|
+
workflowHeads: ReadonlySet<string>,
|
|
238
|
+
ledgerByHead: ReadonlyMap<string, readonly AdmissionRecord[]>,
|
|
239
|
+
): Set<string> {
|
|
240
|
+
const countable = new Set<string>();
|
|
241
|
+
for (const head of workflowHeads) {
|
|
242
|
+
const rows = ledgerByHead.get(head);
|
|
243
|
+
const provablyUnspent =
|
|
244
|
+
rows !== undefined &&
|
|
245
|
+
rows.length > 0 &&
|
|
246
|
+
rows.every(
|
|
247
|
+
(row) =>
|
|
248
|
+
row.forced === true || PROVABLY_UNSPENT_STATUSES.has(row.status),
|
|
249
|
+
);
|
|
250
|
+
if (!provablyUnspent) {
|
|
251
|
+
countable.add(head);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
return countable;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
// Authoritative attempt count for admission: summary counter, workflow heads,
|
|
258
|
+
// and durable ledger terminal rows — whichever is highest.
|
|
259
|
+
export function resolveCiAdmissionAttemptCount(input: {
|
|
260
|
+
stateCount: number;
|
|
261
|
+
workflowHeads: ReadonlySet<string>;
|
|
262
|
+
ledgerRecords: readonly AdmissionRecord[];
|
|
263
|
+
}): number {
|
|
264
|
+
return Math.max(
|
|
265
|
+
resolveReviewAttemptCount({
|
|
266
|
+
stateCount: input.stateCount,
|
|
267
|
+
workflowHeads: input.workflowHeads,
|
|
268
|
+
}),
|
|
269
|
+
countTerminalAttempts(input.ledgerRecords),
|
|
270
|
+
);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
// WHY a forced run keeps the count, unfloored: the budget bounds AUTOMATIC
|
|
274
|
+
// reviews. A writer's explicit comment (`/crysk review`, `/crysk recheck`)
|
|
275
|
+
// is a deliberate spend outside that budget, so it must not move the counter
|
|
276
|
+
// the budget reads. A forced FIRST review therefore writes 0, and the summary
|
|
277
|
+
// it posts reads as zero automatic reviews (parseCiAdmissionBlock accepts 0 for
|
|
278
|
+
// that reason). A floor of 1 here would charge that run one automatic attempt.
|
|
279
|
+
export function nextStateReviewCount(input: {
|
|
280
|
+
existingSummaryId: number | null;
|
|
281
|
+
state: ParsedStateBlock | null;
|
|
282
|
+
summaryBody?: string | null;
|
|
283
|
+
force?: boolean;
|
|
284
|
+
}): number {
|
|
285
|
+
const admission =
|
|
286
|
+
input.summaryBody === undefined || input.summaryBody === null
|
|
287
|
+
? null
|
|
288
|
+
: parseCiAdmissionBlock(input.summaryBody);
|
|
289
|
+
const current = stateReviewCount(
|
|
290
|
+
input.state,
|
|
291
|
+
input.existingSummaryId !== null,
|
|
292
|
+
admission,
|
|
293
|
+
);
|
|
294
|
+
if (input.force === true) return current;
|
|
295
|
+
return current + 1;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
export function priorTierScore(
|
|
299
|
+
findings: readonly { tier: RecoveredTier }[],
|
|
300
|
+
weights: Pick<CiReviewPolicy, "blockingWeight" | "advisoryWeight">,
|
|
301
|
+
): PriorTierScore {
|
|
302
|
+
let blocking = 0;
|
|
303
|
+
let advisory = 0;
|
|
304
|
+
// A state-block row can now carry #206's `TIER_UNRECOVERABLE` sentinel.
|
|
305
|
+
// Counting it as advisory (the old `else` branch's behavior) would
|
|
306
|
+
// silently under-weight the score exactly like the bug this reuses the
|
|
307
|
+
// sentinel to fix — so it sets `failOpen` instead, reusing the SAME
|
|
308
|
+
// "run rather than trust an incomplete count" signal this type already
|
|
309
|
+
// carries for the untrusted-actor case below.
|
|
310
|
+
let unrecoverable = 0;
|
|
311
|
+
for (const finding of findings) {
|
|
312
|
+
if (finding.tier === "blocking") blocking++;
|
|
313
|
+
else if (finding.tier === "advisory") advisory++;
|
|
314
|
+
else unrecoverable++;
|
|
315
|
+
}
|
|
316
|
+
return {
|
|
317
|
+
blocking,
|
|
318
|
+
advisory,
|
|
319
|
+
score:
|
|
320
|
+
blocking * weights.blockingWeight + advisory * weights.advisoryWeight,
|
|
321
|
+
source: "state",
|
|
322
|
+
...(unrecoverable > 0 ? { failOpen: true } : {}),
|
|
323
|
+
};
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
// First reviews carry no item-7 `pr-hero-state` block (W-prov), but CI admission
|
|
327
|
+
// still needs true tier counts. A separate marker keeps item-7 semantics intact
|
|
328
|
+
// while giving the gate tier-accurate data on every post.
|
|
329
|
+
export const PR_CI_ADMISSION_PREFIX = `${MARKER_OPEN}ci-admission `;
|
|
330
|
+
const CI_ADMISSION_HEADER = new RegExp(
|
|
331
|
+
`^<!-- ${MARKER_BRAND_PATTERN}-ci-admission v=1 head=([0-9a-f]{40}) -->`,
|
|
332
|
+
);
|
|
333
|
+
const HTML_COMMENT = /<!--([\s\S]*?)-->/;
|
|
334
|
+
|
|
335
|
+
export interface ParsedCiAdmissionBlock {
|
|
336
|
+
headSha: string;
|
|
337
|
+
blocking: number;
|
|
338
|
+
advisory: number;
|
|
339
|
+
reviews: number;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
export function tierCountsFromFindings(findings: readonly { tier: Tier }[]): {
|
|
343
|
+
blocking: number;
|
|
344
|
+
advisory: number;
|
|
345
|
+
} {
|
|
346
|
+
let blocking = 0;
|
|
347
|
+
let advisory = 0;
|
|
348
|
+
for (const finding of findings) {
|
|
349
|
+
if (finding.tier === "blocking") blocking++;
|
|
350
|
+
else advisory++;
|
|
351
|
+
}
|
|
352
|
+
return { blocking, advisory };
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
// Outside-diff findings are a subset of doc.findings; dedupe by id when a
|
|
356
|
+
// caller still merges inline + outside buckets.
|
|
357
|
+
export function canonicalAdmissionFindings(
|
|
358
|
+
findings: readonly { id: string; tier: Tier }[],
|
|
359
|
+
): { id: string; tier: Tier }[] {
|
|
360
|
+
const seen = new Set<string>();
|
|
361
|
+
const out: { id: string; tier: Tier }[] = [];
|
|
362
|
+
for (const finding of findings) {
|
|
363
|
+
if (seen.has(finding.id)) continue;
|
|
364
|
+
seen.add(finding.id);
|
|
365
|
+
out.push(finding);
|
|
366
|
+
}
|
|
367
|
+
return out;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
export function renderCiAdmissionBlock(
|
|
371
|
+
headSha: string,
|
|
372
|
+
counts: { blocking: number; advisory: number },
|
|
373
|
+
reviews: number,
|
|
374
|
+
): string {
|
|
375
|
+
const payload = JSON.stringify({
|
|
376
|
+
blocking: counts.blocking,
|
|
377
|
+
advisory: counts.advisory,
|
|
378
|
+
reviews,
|
|
379
|
+
});
|
|
380
|
+
return (
|
|
381
|
+
`${PR_CI_ADMISSION_PREFIX}v=1 head=${headSha} -->\n` +
|
|
382
|
+
`<!-- ${encodeCiAdmissionJson(payload)} -->`
|
|
383
|
+
);
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
export function parseCiAdmissionBlock(
|
|
387
|
+
body: string,
|
|
388
|
+
): ParsedCiAdmissionBlock | null {
|
|
389
|
+
const start = findMarker(body, PR_CI_ADMISSION_PREFIX)?.index;
|
|
390
|
+
if (start === undefined) return null;
|
|
391
|
+
const fromMarker = body.slice(start);
|
|
392
|
+
const headerLine = fromMarker.split("\n", 1)[0] ?? "";
|
|
393
|
+
const header = CI_ADMISSION_HEADER.exec(headerLine);
|
|
394
|
+
if (header?.[1] === undefined) return null;
|
|
395
|
+
const afterHeader = fromMarker.slice(headerLine.length);
|
|
396
|
+
const jsonComment = HTML_COMMENT.exec(afterHeader);
|
|
397
|
+
if (jsonComment?.[1] === undefined) return null;
|
|
398
|
+
try {
|
|
399
|
+
const parsed: unknown = JSON.parse(jsonComment[1].trim());
|
|
400
|
+
if (typeof parsed !== "object" || parsed === null) return null;
|
|
401
|
+
const row = parsed as Record<string, unknown>;
|
|
402
|
+
const blocking = row.blocking;
|
|
403
|
+
const advisory = row.advisory;
|
|
404
|
+
const reviews = row.reviews;
|
|
405
|
+
if (
|
|
406
|
+
typeof blocking !== "number" ||
|
|
407
|
+
!Number.isInteger(blocking) ||
|
|
408
|
+
blocking < 0 ||
|
|
409
|
+
typeof advisory !== "number" ||
|
|
410
|
+
!Number.isInteger(advisory) ||
|
|
411
|
+
advisory < 0 ||
|
|
412
|
+
typeof reviews !== "number" ||
|
|
413
|
+
!Number.isInteger(reviews) ||
|
|
414
|
+
// WHY 0 is valid here (and not in the state block's asStateReviews):
|
|
415
|
+
// the first summary a forced run posts carries no automatic review, so
|
|
416
|
+
// its count is 0. Rejecting 0 made the parse fail, and stateReviewCount
|
|
417
|
+
// then fell back to "a summary exists, so one review happened", charging
|
|
418
|
+
// that forced run an automatic attempt. renderStateBlock never writes 0,
|
|
419
|
+
// so the state block keeps requiring >= 1.
|
|
420
|
+
reviews < 0
|
|
421
|
+
) {
|
|
422
|
+
return null;
|
|
423
|
+
}
|
|
424
|
+
return { headSha: header[1], blocking, advisory, reviews };
|
|
425
|
+
} catch {
|
|
426
|
+
return null;
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
function encodeCiAdmissionJson(value: string): string {
|
|
431
|
+
return value
|
|
432
|
+
.replaceAll("\u2028", "\\u2028")
|
|
433
|
+
.replaceAll("\u2029", "\\u2029")
|
|
434
|
+
.replaceAll("-->", "--\\u003e");
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
// Posted inline/issue finding comments carry tier in the header line
|
|
438
|
+
// (`report.ts` findingHeaderLine), not in the marker. This is the bootstrap
|
|
439
|
+
// fallback when a summary predates `pr-hero-ci-admission` or lost its block.
|
|
440
|
+
const FINDING_HEADER_TIER = /^\S+\s+(blocking|advisory)\s+·\s+/;
|
|
441
|
+
|
|
442
|
+
export function parseFindingCommentTier(body: string): Tier | null {
|
|
443
|
+
if (findMarker(body, PR_FINDING_MARKER_PREFIX) === undefined) return null;
|
|
444
|
+
let afterMarker = false;
|
|
445
|
+
for (const line of body.split("\n")) {
|
|
446
|
+
if (matchMarkerPrefix(line, PR_FINDING_MARKER_PREFIX) !== undefined) {
|
|
447
|
+
afterMarker = true;
|
|
448
|
+
continue;
|
|
449
|
+
}
|
|
450
|
+
if (!afterMarker) continue;
|
|
451
|
+
const trimmed = line.trim();
|
|
452
|
+
if (trimmed.length === 0) continue;
|
|
453
|
+
const match = FINDING_HEADER_TIER.exec(trimmed);
|
|
454
|
+
if (match?.[1] === "blocking" || match?.[1] === "advisory") {
|
|
455
|
+
return match[1];
|
|
456
|
+
}
|
|
457
|
+
return null;
|
|
458
|
+
}
|
|
459
|
+
return null;
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
function isTrustedActor(
|
|
463
|
+
user: string | undefined,
|
|
464
|
+
trustedActors: ReadonlySet<string> | undefined,
|
|
465
|
+
): boolean {
|
|
466
|
+
if (trustedActors === undefined) return true;
|
|
467
|
+
return user !== undefined && trustedActors.has(user);
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
export function postedFindingFingerprint(marker: {
|
|
471
|
+
headSha: string;
|
|
472
|
+
path: string;
|
|
473
|
+
line: number;
|
|
474
|
+
c: string;
|
|
475
|
+
}): string {
|
|
476
|
+
return `${marker.headSha}:${marker.path}:${marker.line}:${marker.c}`;
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
export function validateAdmissionAuthority(input: {
|
|
480
|
+
summaryHead: string | null;
|
|
481
|
+
reportMarkerHead: string | null;
|
|
482
|
+
state: ParsedStateBlock | null;
|
|
483
|
+
admission: ParsedCiAdmissionBlock | null;
|
|
484
|
+
}):
|
|
485
|
+
| { ok: true; authoritativeHead: string }
|
|
486
|
+
| { ok: false; failOpen: true; reason: string } {
|
|
487
|
+
const { summaryHead, reportMarkerHead, state, admission } = input;
|
|
488
|
+
if (
|
|
489
|
+
reportMarkerHead !== null &&
|
|
490
|
+
admission !== null &&
|
|
491
|
+
reportMarkerHead !== admission.headSha
|
|
492
|
+
) {
|
|
493
|
+
return {
|
|
494
|
+
ok: false,
|
|
495
|
+
failOpen: true,
|
|
496
|
+
reason: "report marker head does not match admission block head",
|
|
497
|
+
};
|
|
498
|
+
}
|
|
499
|
+
if (summaryHead !== null && state !== null && state.headSha !== summaryHead) {
|
|
500
|
+
return {
|
|
501
|
+
ok: false,
|
|
502
|
+
failOpen: true,
|
|
503
|
+
reason: "state block head does not match summary head",
|
|
504
|
+
};
|
|
505
|
+
}
|
|
506
|
+
if (
|
|
507
|
+
summaryHead !== null &&
|
|
508
|
+
admission !== null &&
|
|
509
|
+
admission.headSha !== summaryHead
|
|
510
|
+
) {
|
|
511
|
+
return {
|
|
512
|
+
ok: false,
|
|
513
|
+
failOpen: true,
|
|
514
|
+
reason: "admission block head does not match summary head",
|
|
515
|
+
};
|
|
516
|
+
}
|
|
517
|
+
const authoritativeHead =
|
|
518
|
+
summaryHead ??
|
|
519
|
+
reportMarkerHead ??
|
|
520
|
+
state?.headSha ??
|
|
521
|
+
admission?.headSha ??
|
|
522
|
+
"";
|
|
523
|
+
return { ok: true, authoritativeHead };
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
export function pathsFromPostedFindingMarkers(
|
|
527
|
+
comments: readonly { body: string; user?: string }[],
|
|
528
|
+
summaryHead: string,
|
|
529
|
+
trustedActors?: ReadonlySet<string>,
|
|
530
|
+
): string[] {
|
|
531
|
+
const paths = new Set<string>();
|
|
532
|
+
for (const comment of comments) {
|
|
533
|
+
const marker = parseFindingMarker(comment.body);
|
|
534
|
+
if (marker === null || marker.headSha !== summaryHead) continue;
|
|
535
|
+
if (!isTrustedActor(comment.user, trustedActors)) continue;
|
|
536
|
+
paths.add(marker.path);
|
|
537
|
+
}
|
|
538
|
+
return [...paths];
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
export function deltaTouchesPriorFindings(
|
|
542
|
+
changedPaths: readonly string[],
|
|
543
|
+
priorPaths: readonly string[],
|
|
544
|
+
): boolean {
|
|
545
|
+
if (priorPaths.length === 0) return false;
|
|
546
|
+
const changed = new Set(changedPaths);
|
|
547
|
+
return priorPaths.some((path) => changed.has(path));
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
export function scanPostedFindingTiers(input: {
|
|
551
|
+
summaryHead: string;
|
|
552
|
+
comments: readonly { body: string; user?: string }[];
|
|
553
|
+
trustedActors?: ReadonlySet<string>;
|
|
554
|
+
}): PostedFindingAdmissionScan | null {
|
|
555
|
+
let blocking = 0;
|
|
556
|
+
let advisory = 0;
|
|
557
|
+
let matchedMarkers = 0;
|
|
558
|
+
let parsedTiers = 0;
|
|
559
|
+
let untrustedMarkers = 0;
|
|
560
|
+
const seenFingerprints = new Set<string>();
|
|
561
|
+
for (const comment of input.comments) {
|
|
562
|
+
const marker = parseFindingMarker(comment.body);
|
|
563
|
+
if (marker === null || marker.headSha !== input.summaryHead) continue;
|
|
564
|
+
if (!isTrustedActor(comment.user, input.trustedActors)) {
|
|
565
|
+
untrustedMarkers++;
|
|
566
|
+
continue;
|
|
567
|
+
}
|
|
568
|
+
const fingerprint = postedFindingFingerprint(marker);
|
|
569
|
+
if (seenFingerprints.has(fingerprint)) continue;
|
|
570
|
+
seenFingerprints.add(fingerprint);
|
|
571
|
+
matchedMarkers++;
|
|
572
|
+
const tier = parseFindingCommentTier(comment.body);
|
|
573
|
+
if (tier === null) continue;
|
|
574
|
+
parsedTiers++;
|
|
575
|
+
if (tier === "blocking") blocking++;
|
|
576
|
+
else advisory++;
|
|
577
|
+
}
|
|
578
|
+
if (matchedMarkers === 0) {
|
|
579
|
+
if (untrustedMarkers > 0) {
|
|
580
|
+
return {
|
|
581
|
+
blocking: 0,
|
|
582
|
+
advisory: 0,
|
|
583
|
+
matchedMarkers: 0,
|
|
584
|
+
parsedTiers: 0,
|
|
585
|
+
failOpen: true,
|
|
586
|
+
};
|
|
587
|
+
}
|
|
588
|
+
return null;
|
|
589
|
+
}
|
|
590
|
+
return { blocking, advisory, matchedMarkers, parsedTiers };
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
function tierScoreFromCounts(
|
|
594
|
+
counts: { blocking: number; advisory: number },
|
|
595
|
+
policy: CiReviewPolicy,
|
|
596
|
+
source: Exclude<PriorTierScore["source"], "none">,
|
|
597
|
+
): PriorTierScore {
|
|
598
|
+
return {
|
|
599
|
+
blocking: counts.blocking,
|
|
600
|
+
advisory: counts.advisory,
|
|
601
|
+
score:
|
|
602
|
+
counts.blocking * policy.blockingWeight +
|
|
603
|
+
counts.advisory * policy.advisoryWeight,
|
|
604
|
+
source,
|
|
605
|
+
};
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
export function priorScoreForAdmission(input: {
|
|
609
|
+
state: ParsedStateBlock | null;
|
|
610
|
+
admission: ParsedCiAdmissionBlock | null;
|
|
611
|
+
postedFindings: PostedFindingAdmissionScan | null;
|
|
612
|
+
policy: CiReviewPolicy;
|
|
613
|
+
}): PriorTierScore {
|
|
614
|
+
if (input.state !== null && input.state.findings.length > 0) {
|
|
615
|
+
return priorTierScore(input.state.findings, input.policy);
|
|
616
|
+
}
|
|
617
|
+
if (input.admission !== null) {
|
|
618
|
+
return tierScoreFromCounts(input.admission, input.policy, "admission");
|
|
619
|
+
}
|
|
620
|
+
if (input.postedFindings !== null) {
|
|
621
|
+
if (input.postedFindings.failOpen === true) {
|
|
622
|
+
return {
|
|
623
|
+
blocking: 0,
|
|
624
|
+
advisory: 0,
|
|
625
|
+
score: 0,
|
|
626
|
+
source: "none",
|
|
627
|
+
failOpen: true,
|
|
628
|
+
};
|
|
629
|
+
}
|
|
630
|
+
if (input.postedFindings.parsedTiers > 0) {
|
|
631
|
+
return tierScoreFromCounts(
|
|
632
|
+
input.postedFindings,
|
|
633
|
+
input.policy,
|
|
634
|
+
"posted-findings",
|
|
635
|
+
);
|
|
636
|
+
}
|
|
637
|
+
return {
|
|
638
|
+
blocking: 0,
|
|
639
|
+
advisory: 0,
|
|
640
|
+
score: 0,
|
|
641
|
+
source: "none",
|
|
642
|
+
failOpen: true,
|
|
643
|
+
};
|
|
644
|
+
}
|
|
645
|
+
return { blocking: 0, advisory: 0, score: 0, source: "none" };
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
function priorForAdmissionInput(input: CiReviewAdmissionInput): PriorTierScore {
|
|
649
|
+
return priorScoreForAdmission({
|
|
650
|
+
state: input.state,
|
|
651
|
+
admission: input.admission,
|
|
652
|
+
postedFindings: input.postedFindings,
|
|
653
|
+
policy: input.policy,
|
|
654
|
+
});
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
function terminalFields(
|
|
658
|
+
input: CiReviewAdmissionInput,
|
|
659
|
+
prior: PriorTierScore,
|
|
660
|
+
): CiReviewAdmissionTerminalFields {
|
|
661
|
+
return {
|
|
662
|
+
prior,
|
|
663
|
+
reviewCount: input.reviewCount,
|
|
664
|
+
maxAttempts: input.policy.maxAttempts,
|
|
665
|
+
minScore: input.policy.rereviewMinScore,
|
|
666
|
+
};
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
export function evaluateCiReviewAdmission(
|
|
670
|
+
input: CiReviewAdmissionInput,
|
|
671
|
+
): CiReviewAdmissionVerdict {
|
|
672
|
+
if (input.forceOverride === true) {
|
|
673
|
+
return { action: "run" };
|
|
674
|
+
}
|
|
675
|
+
const partialSameHead =
|
|
676
|
+
input.summaryHead !== null &&
|
|
677
|
+
input.summaryHead === input.currentHead &&
|
|
678
|
+
input.summaryComplete === false;
|
|
679
|
+
if (
|
|
680
|
+
input.summaryHead !== null &&
|
|
681
|
+
input.summaryHead === input.currentHead &&
|
|
682
|
+
!partialSameHead
|
|
683
|
+
) {
|
|
684
|
+
const prior = priorForAdmissionInput(input);
|
|
685
|
+
return {
|
|
686
|
+
action: "skip",
|
|
687
|
+
reason: "same-head",
|
|
688
|
+
...terminalFields(input, prior),
|
|
689
|
+
};
|
|
690
|
+
}
|
|
691
|
+
// WHY before the attempt cap and the policy modes: a stop is a human's
|
|
692
|
+
// explicit decision that this PR needs no more automatic reviews, so it
|
|
693
|
+
// outranks every automatic reason to run one. It still skips, never passes:
|
|
694
|
+
// the skip carries the last verdict, and a red gate still needs an override.
|
|
695
|
+
// WHY not on a partial same head: that is a retry of a review that already
|
|
696
|
+
// ran here, not a new push. Skipping it would let the carry write an older
|
|
697
|
+
// commit's verdict over this head's own red partial status.
|
|
698
|
+
if (input.stopped === true && !partialSameHead) {
|
|
699
|
+
const prior = priorForAdmissionInput(input);
|
|
700
|
+
return {
|
|
701
|
+
action: "skip",
|
|
702
|
+
reason: "stopped",
|
|
703
|
+
...terminalFields(input, prior),
|
|
704
|
+
};
|
|
705
|
+
}
|
|
706
|
+
if (!input.markerSeen && input.summaryHead === null) {
|
|
707
|
+
return { action: "run" };
|
|
708
|
+
}
|
|
709
|
+
if (input.reviewCount >= input.policy.maxAttempts) {
|
|
710
|
+
const prior = priorForAdmissionInput(input);
|
|
711
|
+
return {
|
|
712
|
+
action: "manual-required",
|
|
713
|
+
reason: "max-attempts-exhausted",
|
|
714
|
+
...terminalFields(input, prior),
|
|
715
|
+
};
|
|
716
|
+
}
|
|
717
|
+
if (input.policy.mode === "manual_only" && input.markerSeen) {
|
|
718
|
+
const prior = priorForAdmissionInput(input);
|
|
719
|
+
return {
|
|
720
|
+
action: "manual-required",
|
|
721
|
+
reason: "manual-only-policy",
|
|
722
|
+
...terminalFields(input, prior),
|
|
723
|
+
};
|
|
724
|
+
}
|
|
725
|
+
if (input.policy.mode === "once_per_pr" && input.reviewCount >= 1) {
|
|
726
|
+
const prior = priorForAdmissionInput(input);
|
|
727
|
+
return {
|
|
728
|
+
action: "skip",
|
|
729
|
+
reason: "once-per-pr",
|
|
730
|
+
...terminalFields(input, prior),
|
|
731
|
+
};
|
|
732
|
+
}
|
|
733
|
+
// A partial review's own score is a floor, so the score and delta-risk
|
|
734
|
+
// gates below cannot justify skipping this head. The attempt cap and the
|
|
735
|
+
// explicit manual_only/once_per_pr policies above have already applied.
|
|
736
|
+
if (partialSameHead) {
|
|
737
|
+
return { action: "run" };
|
|
738
|
+
}
|
|
739
|
+
const prior = priorForAdmissionInput(input);
|
|
740
|
+
if (prior.failOpen === true || input.authorityFailOpen === true) {
|
|
741
|
+
return { action: "run" };
|
|
742
|
+
}
|
|
743
|
+
if (input.policy.mode === "every_push") {
|
|
744
|
+
return { action: "run" };
|
|
745
|
+
}
|
|
746
|
+
if (input.policy.mode === "risk_aware") {
|
|
747
|
+
if (prior.score >= input.policy.rereviewMinScore) {
|
|
748
|
+
return { action: "run" };
|
|
749
|
+
}
|
|
750
|
+
if (input.deltaTouchesPriorFindings) {
|
|
751
|
+
return { action: "run" };
|
|
752
|
+
}
|
|
753
|
+
if (input.deltaRisk != null) {
|
|
754
|
+
if (deltaRiskTriggersReview(input.deltaRisk)) {
|
|
755
|
+
return { action: "run" };
|
|
756
|
+
}
|
|
757
|
+
if (input.deltaRisk.class === "low") {
|
|
758
|
+
return {
|
|
759
|
+
action: "skip",
|
|
760
|
+
reason: "low-risk-delta",
|
|
761
|
+
...terminalFields(input, prior),
|
|
762
|
+
};
|
|
763
|
+
}
|
|
764
|
+
}
|
|
765
|
+
}
|
|
766
|
+
if (
|
|
767
|
+
(input.policy.mode === "thresholded" ||
|
|
768
|
+
input.policy.mode === "risk_aware") &&
|
|
769
|
+
prior.score < input.policy.rereviewMinScore
|
|
770
|
+
) {
|
|
771
|
+
return {
|
|
772
|
+
action: "skip",
|
|
773
|
+
reason: "below-threshold",
|
|
774
|
+
...terminalFields(input, prior),
|
|
775
|
+
};
|
|
776
|
+
}
|
|
777
|
+
return { action: "run" };
|
|
778
|
+
}
|
|
779
|
+
|
|
780
|
+
export function ciReviewSkipDetail(
|
|
781
|
+
verdict: Extract<CiReviewAdmissionVerdict, { action: "skip" }>,
|
|
782
|
+
): string {
|
|
783
|
+
const { prior, reason, reviewCount, maxAttempts, minScore } = verdict;
|
|
784
|
+
const counts =
|
|
785
|
+
prior.source === "none"
|
|
786
|
+
? "no prior findings recorded"
|
|
787
|
+
: `${prior.blocking} blocking-tier · ${prior.advisory} advisory-tier (score ${prior.score})`;
|
|
788
|
+
switch (reason) {
|
|
789
|
+
case "same-head":
|
|
790
|
+
return "this commit was already reviewed";
|
|
791
|
+
case "once-per-pr":
|
|
792
|
+
return `once_per_pr policy allows one automatic review (${reviewCount}/${maxAttempts} attempts used on this PR)`;
|
|
793
|
+
case "below-threshold":
|
|
794
|
+
return `${counts}; re-review needs score ≥ ${minScore} to justify another run`;
|
|
795
|
+
case "low-risk-delta":
|
|
796
|
+
return `${counts}; delta touches only low-risk paths (docs, tests, fixtures)`;
|
|
797
|
+
case "stopped":
|
|
798
|
+
// A status description has little room, so it names the one spelling
|
|
799
|
+
// every workflow accepts during the rename.
|
|
800
|
+
return `a maintainer stopped automatic reviews; ${legacyCommand(REVIEW_COMMAND)} reviews this head`;
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
|
|
804
|
+
// The short label a status description names for a skip (the carry-forward in
|
|
805
|
+
// src/pr/admission.ts). The same wording as the coverage comment (ci/gates.ts COVERAGE_REASON_LABEL,
|
|
806
|
+
// which is not exported), kept here so a description reads the same as the comment beside it.
|
|
807
|
+
export function ciReviewSkipLabel(
|
|
808
|
+
reason:
|
|
809
|
+
| "once-per-pr"
|
|
810
|
+
| "below-threshold"
|
|
811
|
+
| "low-risk-delta"
|
|
812
|
+
| "stopped"
|
|
813
|
+
| CiReviewManualRequiredReason,
|
|
814
|
+
): string {
|
|
815
|
+
switch (reason) {
|
|
816
|
+
case "max-attempts-exhausted":
|
|
817
|
+
return "review budget for this PR is used up";
|
|
818
|
+
case "manual-only-policy":
|
|
819
|
+
return "ci_review_policy is manual_only";
|
|
820
|
+
case "once-per-pr":
|
|
821
|
+
return "once_per_pr policy allows one automatic review";
|
|
822
|
+
case "below-threshold":
|
|
823
|
+
return "prior findings below the re-review score floor";
|
|
824
|
+
case "low-risk-delta":
|
|
825
|
+
return "delta touches only low-risk paths";
|
|
826
|
+
case "stopped":
|
|
827
|
+
return "automatic reviews stopped by a maintainer";
|
|
828
|
+
}
|
|
829
|
+
}
|
|
830
|
+
|
|
831
|
+
export interface StopCommand {
|
|
832
|
+
user: string;
|
|
833
|
+
// "stop" sets the stop; "lift" is a review or recheck command, which ends it.
|
|
834
|
+
kind: "stop" | "lift";
|
|
835
|
+
}
|
|
836
|
+
|
|
837
|
+
// The exact stop, review and recheck commands on the PR, newest first.
|
|
838
|
+
// `comments` is the PR's issue comments in API order (oldest first). The gate
|
|
839
|
+
// walks them and lets the newest one from a write-access author decide, so a
|
|
840
|
+
// stranger can neither set a stop nor lift a maintainer's.
|
|
841
|
+
export function stopCommandsNewestFirst(
|
|
842
|
+
comments: readonly { user: string; body: string }[],
|
|
843
|
+
): StopCommand[] {
|
|
844
|
+
const commands: StopCommand[] = [];
|
|
845
|
+
for (let i = comments.length - 1; i >= 0; i--) {
|
|
846
|
+
const comment = comments[i];
|
|
847
|
+
if (comment === undefined || comment.user === "") continue;
|
|
848
|
+
if (parseReviewCommand(comment.body) !== undefined) {
|
|
849
|
+
commands.push({ user: comment.user, kind: "lift" });
|
|
850
|
+
} else if (isCommand(comment.body, STOP_COMMAND)) {
|
|
851
|
+
commands.push({ user: comment.user, kind: "stop" });
|
|
852
|
+
}
|
|
853
|
+
}
|
|
854
|
+
return commands;
|
|
855
|
+
}
|
|
856
|
+
|
|
857
|
+
export function ciAdmissionRemainingBudget(
|
|
858
|
+
reviewCount: number,
|
|
859
|
+
maxAttempts: number,
|
|
860
|
+
): number {
|
|
861
|
+
return Math.max(0, maxAttempts - reviewCount);
|
|
862
|
+
}
|
|
863
|
+
|
|
864
|
+
export function formatCiAdmissionObserveNotice(input: {
|
|
865
|
+
verdict: Extract<
|
|
866
|
+
CiReviewAdmissionVerdict,
|
|
867
|
+
{ action: "skip" | "manual-required" }
|
|
868
|
+
>;
|
|
869
|
+
currentHead: string;
|
|
870
|
+
reviewedHead: string | null;
|
|
871
|
+
policyMode: CiReviewPolicyMode;
|
|
872
|
+
policyHash: string;
|
|
873
|
+
deltaRisk?: DeltaRiskAssessment | null;
|
|
874
|
+
}): string {
|
|
875
|
+
const { verdict, currentHead, reviewedHead, policyMode, policyHash } = input;
|
|
876
|
+
const would =
|
|
877
|
+
verdict.action === "skip" ? "would skip" : "would require manual override";
|
|
878
|
+
const detail =
|
|
879
|
+
verdict.action === "skip"
|
|
880
|
+
? ciReviewSkipDetail(verdict)
|
|
881
|
+
: ciReviewManualRequiredDetail(verdict);
|
|
882
|
+
const remaining = ciAdmissionRemainingBudget(
|
|
883
|
+
verdict.reviewCount,
|
|
884
|
+
verdict.maxAttempts,
|
|
885
|
+
);
|
|
886
|
+
const risk =
|
|
887
|
+
input.deltaRisk === undefined || input.deltaRisk === null
|
|
888
|
+
? ""
|
|
889
|
+
: ` · risk=${input.deltaRisk.class}`;
|
|
890
|
+
return (
|
|
891
|
+
`crysk admission (observe-only): ${would} — ${detail} ` +
|
|
892
|
+
`(current \`${currentHead.slice(0, 8)}\`, reviewed ` +
|
|
893
|
+
`${reviewedHead === null ? "none" : `\`${reviewedHead.slice(0, 8)}\``}, ` +
|
|
894
|
+
`attempts ${verdict.reviewCount}/${verdict.maxAttempts}, ` +
|
|
895
|
+
`remaining budget ${remaining}, policy ${policyMode}, ` +
|
|
896
|
+
`hash ${policyHash.slice(0, 8)}${risk})`
|
|
897
|
+
);
|
|
898
|
+
}
|
|
899
|
+
|
|
900
|
+
export function ciReviewManualRequiredDetail(
|
|
901
|
+
verdict: Extract<CiReviewAdmissionVerdict, { action: "manual-required" }>,
|
|
902
|
+
): string {
|
|
903
|
+
const { reason, reviewCount, maxAttempts } = verdict;
|
|
904
|
+
switch (reason) {
|
|
905
|
+
case "max-attempts-exhausted":
|
|
906
|
+
return `automatic review budget exhausted (${reviewCount}/${maxAttempts} attempts on this PR).`;
|
|
907
|
+
case "manual-only-policy":
|
|
908
|
+
return "ci_review_policy is manual_only and this PR already has a review.";
|
|
909
|
+
}
|
|
910
|
+
}
|