crysk 0.0.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +176 -0
- package/README.md +609 -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/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 +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 +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,767 @@
|
|
|
1
|
+
// Summary-comment state block for item 7 (`docs/item7-rereview-design.md` §3.6).
|
|
2
|
+
// Pure. Second marker family on the summary, disjoint from `pr-hero-report`
|
|
3
|
+
// and `pr-hero-finding`. Recovers cross-run identity (including Outside Diff)
|
|
4
|
+
// from the PR alone — no ~/.prhero, no run directory (O-4).
|
|
5
|
+
//
|
|
6
|
+
// Placement is load-bearing (R2-S9): AFTER the report marker and visible
|
|
7
|
+
// body. `findMarkedCommentId` requires `body.startsWith(pr-hero-report)`,
|
|
8
|
+
// so a leading state block would orphan the summary every run.
|
|
9
|
+
//
|
|
10
|
+
// `R###` ids are stable and never reused (R2-S1). `claim` is required
|
|
11
|
+
// because a `channel:"outside"` finding's only copy lives in this block;
|
|
12
|
+
// the next PATCH would otherwise erase it (R2-S3).
|
|
13
|
+
|
|
14
|
+
import { isFullCommitId } from "#git/refs";
|
|
15
|
+
import { claimFingerprint } from "#pr/preflight";
|
|
16
|
+
import {
|
|
17
|
+
formatLocs,
|
|
18
|
+
identitiesMatch,
|
|
19
|
+
identityFromFinding,
|
|
20
|
+
identityFromLocs,
|
|
21
|
+
} from "#rereview/identity";
|
|
22
|
+
import type { Finding, Severity } from "#review/findings";
|
|
23
|
+
import { findMarker, MARKER_BRAND_PATTERN, MARKER_OPEN } from "../brand";
|
|
24
|
+
import {
|
|
25
|
+
type FindingChannel,
|
|
26
|
+
type GateStatus,
|
|
27
|
+
isRecoveredSeverity,
|
|
28
|
+
isRecoveredTier,
|
|
29
|
+
type PhaseBResult,
|
|
30
|
+
type PriorRecord,
|
|
31
|
+
type RecoveredSeverity,
|
|
32
|
+
type RecoveredTier,
|
|
33
|
+
TIER_UNRECOVERABLE,
|
|
34
|
+
} from "./classify";
|
|
35
|
+
|
|
36
|
+
export const PR_STATE_MARKER_PREFIX = `${MARKER_OPEN}state `;
|
|
37
|
+
export const GITHUB_ISSUE_COMMENT_MAX = 65536;
|
|
38
|
+
|
|
39
|
+
export type LiveStatus = "carried" | "unconfirmed" | "suppressed" | "deferred";
|
|
40
|
+
|
|
41
|
+
export interface StateFinding {
|
|
42
|
+
id: string;
|
|
43
|
+
sev: RecoveredSeverity;
|
|
44
|
+
tier: RecoveredTier;
|
|
45
|
+
channel: FindingChannel;
|
|
46
|
+
locs: string[];
|
|
47
|
+
c: string;
|
|
48
|
+
claim: string;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface LiveFinding extends StateFinding {
|
|
52
|
+
status: LiveStatus;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export interface ParsedStateBlock {
|
|
56
|
+
headSha: string;
|
|
57
|
+
findings: StateFinding[];
|
|
58
|
+
reviews?: number;
|
|
59
|
+
// The highest R### number ever assigned on this PR (see renderStateBlock).
|
|
60
|
+
// An old block without the field reads as the highest id among its rows.
|
|
61
|
+
hw: number;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const STATE_HEADER = new RegExp(
|
|
65
|
+
`^<!-- ${MARKER_BRAND_PATTERN}-state v=1 head=([0-9a-f]{40}) -->`,
|
|
66
|
+
);
|
|
67
|
+
const HTML_COMMENT = /<!--([\s\S]*?)-->/;
|
|
68
|
+
const R_ID = /^R(\d{3,})$/;
|
|
69
|
+
|
|
70
|
+
export function stateMarker(headSha: string): string {
|
|
71
|
+
return `${PR_STATE_MARKER_PREFIX}v=1 head=${headSha} -->`;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function parseStateBlock(body: string): ParsedStateBlock | null {
|
|
75
|
+
const start = findMarker(body, PR_STATE_MARKER_PREFIX)?.index;
|
|
76
|
+
if (start === undefined) return null;
|
|
77
|
+
const fromMarker = body.slice(start);
|
|
78
|
+
const headerLine = fromMarker.split("\n", 1)[0] ?? "";
|
|
79
|
+
const header = STATE_HEADER.exec(headerLine);
|
|
80
|
+
if (header?.[1] === undefined) return null;
|
|
81
|
+
const headSha = header[1];
|
|
82
|
+
if (!isFullCommitId(headSha)) return null;
|
|
83
|
+
const afterHeader = fromMarker.slice(headerLine.length);
|
|
84
|
+
const jsonComment = HTML_COMMENT.exec(afterHeader);
|
|
85
|
+
if (jsonComment?.[1] === undefined) return null;
|
|
86
|
+
try {
|
|
87
|
+
const parsed: unknown = JSON.parse(jsonComment[1].trim());
|
|
88
|
+
const findings = asStateFindings(parsed);
|
|
89
|
+
if (findings === null) return null;
|
|
90
|
+
const reviews = asStateReviews(parsed);
|
|
91
|
+
if (reviews === null) return null;
|
|
92
|
+
const hw = asStateHw(parsed, findings);
|
|
93
|
+
if (hw === null) return null;
|
|
94
|
+
return {
|
|
95
|
+
headSha,
|
|
96
|
+
findings,
|
|
97
|
+
hw,
|
|
98
|
+
...(reviews === undefined ? {} : { reviews }),
|
|
99
|
+
};
|
|
100
|
+
} catch {
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// WHY hw is written on every block: a retired row leaves the block, and its id
|
|
106
|
+
// must not be handed to a new finding on a later run. The merge sees only the
|
|
107
|
+
// rows a block still holds, so the high-water mark is the one record that
|
|
108
|
+
// outlives the row. It is never below the highest row id, so an absent or
|
|
109
|
+
// stale caller value cannot let an id be reused.
|
|
110
|
+
export function renderStateBlock(
|
|
111
|
+
headSha: string,
|
|
112
|
+
findings: readonly StateFinding[],
|
|
113
|
+
reviews?: number,
|
|
114
|
+
hw?: number,
|
|
115
|
+
): string {
|
|
116
|
+
const payload: Record<string, unknown> = {
|
|
117
|
+
findings: findings.map((f) => ({
|
|
118
|
+
id: f.id,
|
|
119
|
+
sev: f.sev,
|
|
120
|
+
tier: f.tier,
|
|
121
|
+
channel: f.channel,
|
|
122
|
+
locs: f.locs,
|
|
123
|
+
c: f.c,
|
|
124
|
+
claim: f.claim,
|
|
125
|
+
})),
|
|
126
|
+
};
|
|
127
|
+
if (reviews !== undefined && Number.isInteger(reviews) && reviews >= 1) {
|
|
128
|
+
payload.reviews = reviews;
|
|
129
|
+
}
|
|
130
|
+
payload.hw = Math.max(
|
|
131
|
+
hw ?? 0,
|
|
132
|
+
highestStableNumber(findings.map((f) => f.id)),
|
|
133
|
+
);
|
|
134
|
+
return `${stateMarker(headSha)}\n<!-- ${encodeStateJson(payload)} -->`;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// `claimFingerprint("")` is a REAL constant hash (`e3b0c44298fc`), not an
|
|
138
|
+
// absence marker — every claim-less row would otherwise share ONE identity
|
|
139
|
+
// on `c` (#206's second-order bug: `resolvePriorTie`/`resolveRowTie` in
|
|
140
|
+
// rereview/prepare.ts already refuse to trust that constant for a tie-break,
|
|
141
|
+
// but the state block itself still wrote it, and nothing guarantees every
|
|
142
|
+
// FUTURE consumer of `c` will re-derive that same caution). `id` is unique
|
|
143
|
+
// within one state block by construction (a fresh id is always above the
|
|
144
|
+
// high-water mark, so it never reuses one),
|
|
145
|
+
// so a claim-less row fingerprints its own id instead of nothing.
|
|
146
|
+
function liveFingerprint(id: string, claim: string): string {
|
|
147
|
+
if (claim.trim().length > 0) return claimFingerprint(claim);
|
|
148
|
+
return claimFingerprint(`pr-hero:unrecoverable-claim:${id}`);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export function stateFinding(input: {
|
|
152
|
+
id: string;
|
|
153
|
+
sev: RecoveredSeverity;
|
|
154
|
+
tier: RecoveredTier;
|
|
155
|
+
channel: FindingChannel;
|
|
156
|
+
locs: readonly string[];
|
|
157
|
+
claim: string;
|
|
158
|
+
}): StateFinding {
|
|
159
|
+
return {
|
|
160
|
+
id: input.id,
|
|
161
|
+
sev: input.sev,
|
|
162
|
+
tier: input.tier,
|
|
163
|
+
channel: input.channel,
|
|
164
|
+
locs: [...input.locs],
|
|
165
|
+
c: liveFingerprint(input.id, input.claim),
|
|
166
|
+
claim: input.claim,
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function highestStableNumber(ids: readonly string[]): number {
|
|
171
|
+
let max = 0;
|
|
172
|
+
for (const id of ids) {
|
|
173
|
+
const match = R_ID.exec(id);
|
|
174
|
+
if (match?.[1] === undefined) continue;
|
|
175
|
+
const n = Number(match[1]);
|
|
176
|
+
if (n > max) max = n;
|
|
177
|
+
}
|
|
178
|
+
return max;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
export interface CapResult {
|
|
182
|
+
kept: LiveFinding[];
|
|
183
|
+
droppedUnconfirmed: number;
|
|
184
|
+
droppedCarried: number;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
export interface CapOptions {
|
|
188
|
+
reviews?: number;
|
|
189
|
+
hw?: number;
|
|
190
|
+
// Ids this run's survivors touched or added. They are never evicted and they
|
|
191
|
+
// still count toward the size, so the block reports what this run found.
|
|
192
|
+
protectedIds?: ReadonlySet<string>;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// On overflow, evict unconfirmed first, then carried, and only PRIOR rows of
|
|
196
|
+
// tier advisory. Never suppressed (would resurface as fresh), never deferred
|
|
197
|
+
// (O-3), and never a blocking row. Round 3 C-R3-1.
|
|
198
|
+
// WHY a blocking row is never evicted: the next review's gate counts the rows
|
|
199
|
+
// the state block holds. A blocking row dropped here makes that gate read green
|
|
200
|
+
// with the finding still open, which is self-grading (S7). When the block is
|
|
201
|
+
// still too large after every advisory prior is gone, the caller keeps the
|
|
202
|
+
// render as is, and GitHub reports the size failure.
|
|
203
|
+
export function capLiveFindings(
|
|
204
|
+
entries: readonly LiveFinding[],
|
|
205
|
+
bodyWithoutState: string,
|
|
206
|
+
headSha: string,
|
|
207
|
+
maxBody: number = GITHUB_ISSUE_COMMENT_MAX,
|
|
208
|
+
options: CapOptions = {},
|
|
209
|
+
): CapResult {
|
|
210
|
+
const kept = [...entries];
|
|
211
|
+
let droppedUnconfirmed = 0;
|
|
212
|
+
let droppedCarried = 0;
|
|
213
|
+
const protectedIds = options.protectedIds ?? new Set<string>();
|
|
214
|
+
while (
|
|
215
|
+
kept.length > 0 &&
|
|
216
|
+
!fits(bodyWithoutState, headSha, kept, maxBody, options)
|
|
217
|
+
) {
|
|
218
|
+
const unconfirmed = lastEvictableIndex(kept, "unconfirmed", protectedIds);
|
|
219
|
+
if (unconfirmed !== -1) {
|
|
220
|
+
kept.splice(unconfirmed, 1);
|
|
221
|
+
droppedUnconfirmed++;
|
|
222
|
+
continue;
|
|
223
|
+
}
|
|
224
|
+
const carried = lastEvictableIndex(kept, "carried", protectedIds);
|
|
225
|
+
if (carried !== -1) {
|
|
226
|
+
kept.splice(carried, 1);
|
|
227
|
+
droppedCarried++;
|
|
228
|
+
continue;
|
|
229
|
+
}
|
|
230
|
+
break;
|
|
231
|
+
}
|
|
232
|
+
return { kept, droppedUnconfirmed, droppedCarried };
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
function fits(
|
|
236
|
+
bodyWithoutState: string,
|
|
237
|
+
headSha: string,
|
|
238
|
+
entries: readonly LiveFinding[],
|
|
239
|
+
maxBody: number,
|
|
240
|
+
options: CapOptions,
|
|
241
|
+
): boolean {
|
|
242
|
+
const stored = entries.map(stripStatus);
|
|
243
|
+
const block = renderStateBlock(headSha, stored, options.reviews, options.hw);
|
|
244
|
+
return bodyWithoutState.length + block.length <= maxBody;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
function stripStatus(entry: LiveFinding): StateFinding {
|
|
248
|
+
const { status: _status, ...stored } = entry;
|
|
249
|
+
return stored;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
function lastEvictableIndex(
|
|
253
|
+
entries: readonly LiveFinding[],
|
|
254
|
+
status: LiveStatus,
|
|
255
|
+
protectedIds: ReadonlySet<string>,
|
|
256
|
+
): number {
|
|
257
|
+
for (let i = entries.length - 1; i >= 0; i--) {
|
|
258
|
+
const entry = entries[i];
|
|
259
|
+
if (entry?.status !== status) continue;
|
|
260
|
+
if (entry.tier !== "advisory" || protectedIds.has(entry.id)) continue;
|
|
261
|
+
return i;
|
|
262
|
+
}
|
|
263
|
+
return -1;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
function encodeStateJson(value: unknown): string {
|
|
267
|
+
return JSON.stringify(value)
|
|
268
|
+
.replaceAll("\u2028", "\\u2028")
|
|
269
|
+
.replaceAll("\u2029", "\\u2029")
|
|
270
|
+
.replaceAll("-->", "--\\u003e");
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
function asStateFindings(parsed: unknown): StateFinding[] | null {
|
|
274
|
+
if (typeof parsed !== "object" || parsed === null) return null;
|
|
275
|
+
const findings = (parsed as { findings?: unknown }).findings;
|
|
276
|
+
if (!Array.isArray(findings)) return null;
|
|
277
|
+
const out: StateFinding[] = [];
|
|
278
|
+
for (const row of findings) {
|
|
279
|
+
const finding = asStateFinding(row);
|
|
280
|
+
if (finding === null) return null;
|
|
281
|
+
out.push(finding);
|
|
282
|
+
}
|
|
283
|
+
return out;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
// An absent hw reads as the highest id among the block's rows, which is what a
|
|
287
|
+
// block written before the mark existed can prove. A present hw must be a
|
|
288
|
+
// non-negative integer, or the whole block is rejected like any other malformed
|
|
289
|
+
// field.
|
|
290
|
+
function asStateHw(
|
|
291
|
+
parsed: unknown,
|
|
292
|
+
findings: readonly StateFinding[],
|
|
293
|
+
): number | null {
|
|
294
|
+
const raw = (parsed as { hw?: unknown }).hw;
|
|
295
|
+
if (raw === undefined) return highestStableNumber(findings.map((f) => f.id));
|
|
296
|
+
if (typeof raw !== "number" || !Number.isInteger(raw) || raw < 0) return null;
|
|
297
|
+
return raw;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
function asStateReviews(parsed: unknown): number | undefined | null {
|
|
301
|
+
if (typeof parsed !== "object" || parsed === null) return null;
|
|
302
|
+
const reviews = (parsed as { reviews?: unknown }).reviews;
|
|
303
|
+
if (reviews === undefined) return undefined;
|
|
304
|
+
if (
|
|
305
|
+
typeof reviews !== "number" ||
|
|
306
|
+
!Number.isInteger(reviews) ||
|
|
307
|
+
reviews < 1
|
|
308
|
+
) {
|
|
309
|
+
return null;
|
|
310
|
+
}
|
|
311
|
+
return reviews;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
function asStateFinding(row: unknown): StateFinding | null {
|
|
315
|
+
if (typeof row !== "object" || row === null) return null;
|
|
316
|
+
const r = row as Record<string, unknown>;
|
|
317
|
+
if (typeof r.id !== "string" || !R_ID.test(r.id)) return null;
|
|
318
|
+
if (!isRecoveredSeverity(r.sev) || !isRecoveredTier(r.tier)) return null;
|
|
319
|
+
if (r.channel !== "inline" && r.channel !== "outside") return null;
|
|
320
|
+
if (
|
|
321
|
+
!Array.isArray(r.locs) ||
|
|
322
|
+
!r.locs.every((loc) => typeof loc === "string")
|
|
323
|
+
) {
|
|
324
|
+
return null;
|
|
325
|
+
}
|
|
326
|
+
if (typeof r.c !== "string" || !/^[0-9a-f]{12}$/.test(r.c)) return null;
|
|
327
|
+
if (typeof r.claim !== "string") return null;
|
|
328
|
+
return {
|
|
329
|
+
id: r.id,
|
|
330
|
+
sev: r.sev,
|
|
331
|
+
tier: r.tier,
|
|
332
|
+
channel: r.channel,
|
|
333
|
+
locs: r.locs as string[],
|
|
334
|
+
c: r.c,
|
|
335
|
+
claim: r.claim,
|
|
336
|
+
};
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
function formatR(n: number): string {
|
|
340
|
+
return `R${String(n).padStart(3, "0")}`;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
export function isLiveStatus(status: GateStatus): status is LiveStatus {
|
|
344
|
+
return (
|
|
345
|
+
status === "carried" ||
|
|
346
|
+
status === "unconfirmed" ||
|
|
347
|
+
status === "suppressed" ||
|
|
348
|
+
status === "deferred"
|
|
349
|
+
);
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
// WHY a triage-driven return or re-tier is persisted as `carried`: the state
|
|
353
|
+
// block is the only memory the next run has, and its live rows are the next
|
|
354
|
+
// run's priors. The author's triage stays on the thread and is re-read every
|
|
355
|
+
// run, so a misclassified prior, or a dismissal the adjudicator rejected,
|
|
356
|
+
// settles the same way again. Dropping it here let two pushes clear a blocking
|
|
357
|
+
// finding: run N counted it, the block forgot it, and run N+1 read zero. That
|
|
358
|
+
// is self-grading (S7). A worsening return is not kept, because this run's own
|
|
359
|
+
// stronger survivor is what carries that finding forward.
|
|
360
|
+
export function assembleLive(input: {
|
|
361
|
+
settled: readonly PhaseBResult[];
|
|
362
|
+
priors: readonly PriorRecord[];
|
|
363
|
+
verifyVerdicts: ReadonlyMap<string, GateStatus>;
|
|
364
|
+
}): {
|
|
365
|
+
live: LiveFinding[];
|
|
366
|
+
// Every prior the re-review did not verify gone, returned and re-tiered rows
|
|
367
|
+
// included. `live` keeps the triage-driven ones (see the WHY above
|
|
368
|
+
// assembleLive) and drops only a worsening return. The gate keeps all of
|
|
369
|
+
// them: see unresolvedBlockingCount below.
|
|
370
|
+
// Written to the in-memory result only, never to the state block or
|
|
371
|
+
// pipeline.json, so the persisted `live[]` contract is unchanged.
|
|
372
|
+
gateRows: GateRow[];
|
|
373
|
+
verifiedGone: number;
|
|
374
|
+
verifiedGoneIds: string[];
|
|
375
|
+
returned: number;
|
|
376
|
+
reTiered: number;
|
|
377
|
+
} {
|
|
378
|
+
const priorById = new Map(input.priors.map((prior) => [prior.id, prior]));
|
|
379
|
+
const live: LiveFinding[] = [];
|
|
380
|
+
const gateRows: GateRow[] = [];
|
|
381
|
+
const verifiedGoneIds: string[] = [];
|
|
382
|
+
let returned = 0;
|
|
383
|
+
let reTiered = 0;
|
|
384
|
+
for (const row of input.settled) {
|
|
385
|
+
let status: GateStatus = row.status;
|
|
386
|
+
if (status === "queued" && row.held !== undefined) {
|
|
387
|
+
// WHY a held row settles only on a refutation: the triage it carries is
|
|
388
|
+
// what the gate counted before the push touched the finding. A verifier
|
|
389
|
+
// that finds it still present, or cannot decide, leaves the triage
|
|
390
|
+
// semantics in place (suppressed stays suppressed, deferred stays
|
|
391
|
+
// deferred, the persisted row is the same as before the verify).
|
|
392
|
+
status =
|
|
393
|
+
input.verifyVerdicts.get(row.id) === "verified-gone"
|
|
394
|
+
? "verified-gone"
|
|
395
|
+
: row.held;
|
|
396
|
+
} else if (status === "queued") {
|
|
397
|
+
status = input.verifyVerdicts.get(row.id) ?? "unconfirmed";
|
|
398
|
+
}
|
|
399
|
+
if (status === "verified-gone") {
|
|
400
|
+
verifiedGoneIds.push(row.id);
|
|
401
|
+
continue;
|
|
402
|
+
}
|
|
403
|
+
const gatePrior = priorById.get(row.id);
|
|
404
|
+
if (gatePrior !== undefined) {
|
|
405
|
+
gateRows.push({
|
|
406
|
+
id: gatePrior.id,
|
|
407
|
+
tier: gatePrior.tier,
|
|
408
|
+
locs: row.locs,
|
|
409
|
+
status,
|
|
410
|
+
});
|
|
411
|
+
}
|
|
412
|
+
// Only a triage-driven settlement persists, as `carried`: the author's
|
|
413
|
+
// misclassified tag, or a dismissal the adjudicator rejected. A worsening
|
|
414
|
+
// return stays out, because this run's own stronger survivor carries it.
|
|
415
|
+
let persisted: LiveStatus | undefined;
|
|
416
|
+
if (
|
|
417
|
+
status === "returned" &&
|
|
418
|
+
gatePrior?.triage?.tag === "dismissed" &&
|
|
419
|
+
gatePrior.triage.verdict === "rejected"
|
|
420
|
+
) {
|
|
421
|
+
persisted = "carried";
|
|
422
|
+
} else if (status === "re-tiered") {
|
|
423
|
+
persisted = "carried";
|
|
424
|
+
} else if (status === "ci-dismissed") {
|
|
425
|
+
persisted = "suppressed";
|
|
426
|
+
} else if (isLiveStatus(status)) {
|
|
427
|
+
persisted = status;
|
|
428
|
+
}
|
|
429
|
+
// WHY a persisted row counts only as carried: the delta line draws carried,
|
|
430
|
+
// re-tiered and returned as disjoint buckets, so a prior also counted here
|
|
431
|
+
// rendered as "1 carried · 1 re-tiered". Only a return or re-tier that stays
|
|
432
|
+
// out of live keeps its own count, which is a worsening return. So
|
|
433
|
+
// `reTiered` is zero for every settlement that has a prior: a re-tier always
|
|
434
|
+
// persists. The field stays because fillRereviewProvenance writes
|
|
435
|
+
// re_tiered to pipeline.json and reads it back when phaseB is absent.
|
|
436
|
+
if (persisted !== undefined && gatePrior !== undefined) {
|
|
437
|
+
live.push({
|
|
438
|
+
id: gatePrior.id,
|
|
439
|
+
sev: gatePrior.sev,
|
|
440
|
+
tier: gatePrior.tier,
|
|
441
|
+
channel: gatePrior.channel,
|
|
442
|
+
locs: row.locs,
|
|
443
|
+
c: liveFingerprint(gatePrior.id, gatePrior.claim),
|
|
444
|
+
claim: gatePrior.claim,
|
|
445
|
+
status: persisted,
|
|
446
|
+
});
|
|
447
|
+
} else if (status === "returned") {
|
|
448
|
+
returned++;
|
|
449
|
+
} else if (status === "re-tiered") {
|
|
450
|
+
reTiered++;
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
return {
|
|
454
|
+
live,
|
|
455
|
+
gateRows,
|
|
456
|
+
verifiedGone: verifiedGoneIds.length,
|
|
457
|
+
verifiedGoneIds,
|
|
458
|
+
returned,
|
|
459
|
+
reTiered,
|
|
460
|
+
};
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
// ---------------------------------------------------------------------------
|
|
464
|
+
// The gate's unresolved-blocking count (S4, S6, S7). Pure: this run's
|
|
465
|
+
// survivors plus the re-review's gate rows in, one number out.
|
|
466
|
+
//
|
|
467
|
+
// The gate must count what the author still owes, not only what this run
|
|
468
|
+
// happened to find. A carried blocking prior that the re-review did not touch
|
|
469
|
+
// is still open, and so is a same-head forced re-review, which runs no hunters
|
|
470
|
+
// and so has no survivors at all. Before this count existed, both read green.
|
|
471
|
+
//
|
|
472
|
+
// A prior leaves the count only when something outside the author cleared it:
|
|
473
|
+
// - verified-gone: the verifier refuted the finding, or every file it names
|
|
474
|
+
// was deleted (classifyPrior settles that as verified-gone).
|
|
475
|
+
// - ci-dismissed: the author's latest dismissal, upheld by the CI adjudicator (src/triage/adjudication.ts, D5).
|
|
476
|
+
// Nothing this run concludes about a prior lowers it. An advisory survivor at a
|
|
477
|
+
// blocking prior's spot does not clear that prior: identity matching is a
|
|
478
|
+
// 5-line window, so a different issue can sit there, and the gate fails closed.
|
|
479
|
+
// A real fix leaves the count when the re-review VERIFIES it gone, and
|
|
480
|
+
// `/pr-hero override` covers a team that accepts the finding.
|
|
481
|
+
// A self-asserted triage tag never leaves the count (S7). `misclassified`
|
|
482
|
+
// lands as `re-tiered` (classifyPrior), and its tier is the author's claim,
|
|
483
|
+
// not the pipeline's, so it is not evidence. A dismissal the adjudicator upheld
|
|
484
|
+
// is upheld only by the author's LOCAL adjudicator, it is `suppressed`, and it stays counted.
|
|
485
|
+
// The next run's count reads the state block, so assembleLive keeps a
|
|
486
|
+
// triage-returned prior there as `carried`. Dropping it let two pushes clear it (S7).
|
|
487
|
+
//
|
|
488
|
+
// Dedupe key. Two rules, both stated so a reader can check them:
|
|
489
|
+
// - a gate row is one prior, keyed by its id (R###). A duplicate row for the
|
|
490
|
+
// same id counts once;
|
|
491
|
+
// - only a BLOCKING survivor pairs, and it pairs with at most one gate row, by
|
|
492
|
+
// the identity the worsening step already uses (identityFromLocs of the
|
|
493
|
+
// prior against identityFromFinding of the survivor). It takes the first
|
|
494
|
+
// unpaired match in PAIR_PREFERENCE order: a blocking row, an unrecoverable
|
|
495
|
+
// row, then an advisory row. A paired row is carried by its survivor and not
|
|
496
|
+
// counted again; a blocking survivor counts by its own tier. An advisory
|
|
497
|
+
// survivor never pairs, so it cannot remove a blocking prior from the count.
|
|
498
|
+
// One-to-one is the fail-closed choice: two priors at one spot and one survivor
|
|
499
|
+
// count as two, because the survivor can be only one of them.
|
|
500
|
+
//
|
|
501
|
+
// Unrecoverable tiers ARE counted, as blocking. A prior whose tier could not be
|
|
502
|
+
// read back is unclassified, and the fail-visible rule (classify.ts) and S7 both
|
|
503
|
+
// forbid a green gate on a finding we cannot classify. Only a verifier verdict or
|
|
504
|
+
// a deleted file lowers such a row, and an advisory survivor at the same spot
|
|
505
|
+
// does not pair with it.
|
|
506
|
+
|
|
507
|
+
export interface GateRow {
|
|
508
|
+
id: string;
|
|
509
|
+
tier: RecoveredTier;
|
|
510
|
+
locs: readonly string[];
|
|
511
|
+
status: GateStatus;
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
export type GateSurvivor = Pick<
|
|
515
|
+
Finding,
|
|
516
|
+
"path" | "line" | "proof_refs" | "tier"
|
|
517
|
+
>;
|
|
518
|
+
|
|
519
|
+
// The order a blocking survivor pairs with a prior: its own tier first, then the
|
|
520
|
+
// unclassified tier, then advisory. The advisory row never changes the count,
|
|
521
|
+
// so it is only ever a last resort and never takes a row the count reads.
|
|
522
|
+
const PAIR_PREFERENCE: readonly RecoveredTier[] = [
|
|
523
|
+
"blocking",
|
|
524
|
+
TIER_UNRECOVERABLE,
|
|
525
|
+
"advisory",
|
|
526
|
+
];
|
|
527
|
+
|
|
528
|
+
export interface SurvivorPair {
|
|
529
|
+
survivor: number;
|
|
530
|
+
row: number;
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
// The one pairing rule, shared by the gate count and the state merge. Each
|
|
534
|
+
// blocking survivor takes the first unpaired row in PAIR_PREFERENCE order whose
|
|
535
|
+
// identity matches it; one-to-one; an advisory survivor never pairs.
|
|
536
|
+
export function pairBlockingSurvivors(
|
|
537
|
+
survivors: readonly GateSurvivor[],
|
|
538
|
+
rows: readonly { tier: RecoveredTier; locs: readonly string[] }[],
|
|
539
|
+
): SurvivorPair[] {
|
|
540
|
+
const identities = rows.map((row) => identityFromLocs(row.locs));
|
|
541
|
+
const paired = new Set<number>();
|
|
542
|
+
const pairs: SurvivorPair[] = [];
|
|
543
|
+
survivors.forEach((survivor, s) => {
|
|
544
|
+
// An advisory survivor never pairs: it cannot clear a blocking prior.
|
|
545
|
+
if (survivor.tier !== "blocking") return;
|
|
546
|
+
const survivorIdentity = identityFromFinding({
|
|
547
|
+
path: survivor.path,
|
|
548
|
+
line: survivor.line,
|
|
549
|
+
proof_refs: survivor.proof_refs,
|
|
550
|
+
});
|
|
551
|
+
for (const tier of PAIR_PREFERENCE) {
|
|
552
|
+
const pick = rows.findIndex(
|
|
553
|
+
(row, i) =>
|
|
554
|
+
!paired.has(i) &&
|
|
555
|
+
row.tier === tier &&
|
|
556
|
+
identitiesMatch(identities[i] ?? new Map(), survivorIdentity),
|
|
557
|
+
);
|
|
558
|
+
if (pick !== -1) {
|
|
559
|
+
paired.add(pick);
|
|
560
|
+
pairs.push({ survivor: s, row: pick });
|
|
561
|
+
return;
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
});
|
|
565
|
+
return pairs;
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
export function unresolvedBlockingCount(
|
|
569
|
+
survivors: readonly GateSurvivor[],
|
|
570
|
+
rows: readonly GateRow[],
|
|
571
|
+
): number {
|
|
572
|
+
const blockingSurvivors = survivors.filter(
|
|
573
|
+
(survivor) => survivor.tier === "blocking",
|
|
574
|
+
).length;
|
|
575
|
+
|
|
576
|
+
// One entry per prior id, and verified-gone never enters the count.
|
|
577
|
+
const seen = new Set<string>();
|
|
578
|
+
const open: GateRow[] = [];
|
|
579
|
+
for (const row of rows) {
|
|
580
|
+
if (row.status === "verified-gone" || row.status === "ci-dismissed") {
|
|
581
|
+
continue;
|
|
582
|
+
}
|
|
583
|
+
if (seen.has(row.id)) continue;
|
|
584
|
+
seen.add(row.id);
|
|
585
|
+
open.push(row);
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
const paired = new Set(
|
|
589
|
+
pairBlockingSurvivors(survivors, open).map((p) => p.row),
|
|
590
|
+
);
|
|
591
|
+
|
|
592
|
+
// WHY unrecoverable counts: fail closed, see the note above the GateRow type.
|
|
593
|
+
// Counting only "blocking" would let an unreadable tier turn the gate green.
|
|
594
|
+
let unpairedBlocking = 0;
|
|
595
|
+
for (let i = 0; i < open.length; i++) {
|
|
596
|
+
if (paired.has(i)) continue;
|
|
597
|
+
const tier = open[i]?.tier;
|
|
598
|
+
if (tier === "blocking" || tier === TIER_UNRECOVERABLE) unpairedBlocking++;
|
|
599
|
+
}
|
|
600
|
+
return blockingSurvivors + unpairedBlocking;
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
// The state merge (S1, S2, S6, S7). Pure. The live priors of this run, plus the
|
|
604
|
+
// previous block's rows and high-water mark, plus this run's survivors, in; the
|
|
605
|
+
// rows the state block should hold out, the new high-water mark, and how many
|
|
606
|
+
// prior advisory identities the size cap dropped.
|
|
607
|
+
//
|
|
608
|
+
// WHY the POST and the PATCH of one run call this with the same inputs: the
|
|
609
|
+
// create-first POST and the closing PATCH must write the same state block, or a
|
|
610
|
+
// PATCH that re-renders the state can drop or renumber rows the POST recorded.
|
|
611
|
+
|
|
612
|
+
export interface FreshStateFinding extends GateSurvivor {
|
|
613
|
+
severity: Severity;
|
|
614
|
+
claim: string;
|
|
615
|
+
channel: FindingChannel;
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
export interface MergeStateInput {
|
|
619
|
+
headSha: string;
|
|
620
|
+
reviews: number;
|
|
621
|
+
live: readonly LiveFinding[];
|
|
622
|
+
previous: { findings: readonly StateFinding[]; hw: number } | null;
|
|
623
|
+
// Ids this run retired (verified gone, or returned by worsening) that `live` no
|
|
624
|
+
// longer holds. On review 2 no state block exists yet, so they are the only record
|
|
625
|
+
// that those ids were ever issued.
|
|
626
|
+
retiredIds?: readonly string[];
|
|
627
|
+
// The head the previous block was written for. WHY it is an input: a re-post
|
|
628
|
+
// of the same head (a retried PATCH, a re-run of the post step) must find the
|
|
629
|
+
// rows it already wrote, or each retry appends them again with a new id.
|
|
630
|
+
previousHead?: string | null;
|
|
631
|
+
fresh: readonly FreshStateFinding[];
|
|
632
|
+
stateBudget: number;
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
export interface MergedState {
|
|
636
|
+
rows: LiveFinding[];
|
|
637
|
+
hw: number;
|
|
638
|
+
dropped: number;
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
// Visible body the state block must leave room for. The PATCH body is longer
|
|
642
|
+
// than the POST body (review links, a moved head line) and the drop note is one
|
|
643
|
+
// line, so a fixed reserve covers both and keeps the state block a function of
|
|
644
|
+
// the POST inputs alone.
|
|
645
|
+
const VISIBLE_BODY_RESERVE = 4096;
|
|
646
|
+
|
|
647
|
+
export function stateBlockBudget(referenceBodyLength: number): number {
|
|
648
|
+
return Math.max(
|
|
649
|
+
0,
|
|
650
|
+
GITHUB_ISSUE_COMMENT_MAX - referenceBodyLength - VISIBLE_BODY_RESERVE,
|
|
651
|
+
);
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
export function mergeStateFindings(input: MergeStateInput): MergedState {
|
|
655
|
+
const priorIds = [
|
|
656
|
+
...(input.previous?.findings ?? []).map((f) => f.id),
|
|
657
|
+
...input.live.map((row) => row.id),
|
|
658
|
+
...(input.retiredIds ?? []),
|
|
659
|
+
];
|
|
660
|
+
let hw = Math.max(input.previous?.hw ?? 0, highestStableNumber(priorIds));
|
|
661
|
+
const rows: LiveFinding[] = input.live.map((row) => ({ ...row }));
|
|
662
|
+
// S2 — same-head re-post: the previous block's rows join the candidates
|
|
663
|
+
// BEFORE pairing, so a fresh survivor re-pairs to its earlier id instead of
|
|
664
|
+
// appending a new one (no new id, no hw increment). A row this run already
|
|
665
|
+
// holds (live) or retired is not repeated. The status is "unconfirmed",
|
|
666
|
+
// the status every appended fresh row gets, because the block never
|
|
667
|
+
// persists status and that is the only value a re-read row can carry.
|
|
668
|
+
if (input.previousHead != null && input.previousHead === input.headSha) {
|
|
669
|
+
const held = new Set([
|
|
670
|
+
...input.live.map((row) => row.id),
|
|
671
|
+
...(input.retiredIds ?? []),
|
|
672
|
+
]);
|
|
673
|
+
for (const row of input.previous?.findings ?? []) {
|
|
674
|
+
if (held.has(row.id)) continue;
|
|
675
|
+
rows.push({ ...row, status: "unconfirmed" });
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
const touched = new Set<string>();
|
|
679
|
+
|
|
680
|
+
const pairs = pairBlockingSurvivors(
|
|
681
|
+
input.fresh,
|
|
682
|
+
rows.map((row) => ({ tier: row.tier, locs: row.locs })),
|
|
683
|
+
);
|
|
684
|
+
const rowOfSurvivor = new Map(pairs.map((p) => [p.survivor, p.row]));
|
|
685
|
+
// WHY a second, advisory-only pass: without it a re-found advisory appends a
|
|
686
|
+
// new row every review, and the state grows until the cap evicts it. Pairing
|
|
687
|
+
// an advisory survivor with an advisory row is safe, because neither side is
|
|
688
|
+
// a row the gate count reads. It never takes a blocking or unrecoverable row,
|
|
689
|
+
// so the fail-closed rule of pairBlockingSurvivors still holds.
|
|
690
|
+
const takenRows = new Set(pairs.map((p) => p.row));
|
|
691
|
+
const rowIdentities = rows.map((row) => identityFromLocs(row.locs));
|
|
692
|
+
for (const [s, finding] of input.fresh.entries()) {
|
|
693
|
+
if (finding.tier !== "advisory" || rowOfSurvivor.has(s)) continue;
|
|
694
|
+
const identity = identityFromFinding(finding);
|
|
695
|
+
const pick = rows.findIndex(
|
|
696
|
+
(row, i) =>
|
|
697
|
+
!takenRows.has(i) &&
|
|
698
|
+
row.tier === "advisory" &&
|
|
699
|
+
identitiesMatch(rowIdentities[i] ?? new Map(), identity),
|
|
700
|
+
);
|
|
701
|
+
if (pick === -1) continue;
|
|
702
|
+
takenRows.add(pick);
|
|
703
|
+
rowOfSurvivor.set(s, pick);
|
|
704
|
+
}
|
|
705
|
+
for (const [s, finding] of input.fresh.entries()) {
|
|
706
|
+
const locs = formatLocs(identityFromFinding(finding));
|
|
707
|
+
const rowIndex = rowOfSurvivor.get(s);
|
|
708
|
+
if (rowIndex !== undefined) {
|
|
709
|
+
// A suppressed prior keeps its severity and tier: the adjudicator's
|
|
710
|
+
// upheld dismissal is the record, and this run's survivor only moves
|
|
711
|
+
// where the finding sits and what it says.
|
|
712
|
+
const prior = rows[rowIndex];
|
|
713
|
+
if (prior === undefined) continue;
|
|
714
|
+
const keepsRating = prior.status === "suppressed";
|
|
715
|
+
rows[rowIndex] = {
|
|
716
|
+
...stateFinding({
|
|
717
|
+
id: prior.id,
|
|
718
|
+
sev: keepsRating ? prior.sev : finding.severity,
|
|
719
|
+
tier: keepsRating ? prior.tier : finding.tier,
|
|
720
|
+
channel: finding.channel,
|
|
721
|
+
locs,
|
|
722
|
+
claim: finding.claim,
|
|
723
|
+
}),
|
|
724
|
+
status: prior.status,
|
|
725
|
+
};
|
|
726
|
+
touched.add(prior.id);
|
|
727
|
+
continue;
|
|
728
|
+
}
|
|
729
|
+
// WHY an unpaired survivor appends: a blocking survivor with no matching row
|
|
730
|
+
// is new, and an advisory one matched no advisory row. An advisory never
|
|
731
|
+
// takes a blocking row (see pairBlockingSurvivors), so it cannot clear a
|
|
732
|
+
// blocking prior that sits at its spot. Appending is the fail-closed choice.
|
|
733
|
+
hw += 1;
|
|
734
|
+
const id = formatR(hw);
|
|
735
|
+
rows.push({
|
|
736
|
+
...stateFinding({
|
|
737
|
+
id,
|
|
738
|
+
sev: finding.severity,
|
|
739
|
+
tier: finding.tier,
|
|
740
|
+
channel: finding.channel,
|
|
741
|
+
locs,
|
|
742
|
+
claim: finding.claim,
|
|
743
|
+
}),
|
|
744
|
+
status: "unconfirmed",
|
|
745
|
+
});
|
|
746
|
+
touched.add(id);
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
const capped = capLiveFindings(rows, "", input.headSha, input.stateBudget, {
|
|
750
|
+
reviews: input.reviews,
|
|
751
|
+
hw,
|
|
752
|
+
protectedIds: touched,
|
|
753
|
+
});
|
|
754
|
+
return {
|
|
755
|
+
rows: capped.kept,
|
|
756
|
+
hw,
|
|
757
|
+
dropped: capped.droppedUnconfirmed + capped.droppedCarried,
|
|
758
|
+
};
|
|
759
|
+
}
|
|
760
|
+
|
|
761
|
+
// One visible line, placed next to the state block, saying the size cap dropped
|
|
762
|
+
// identities from the state. Empty when nothing was dropped.
|
|
763
|
+
export function droppedStateNote(count: number): string {
|
|
764
|
+
if (count <= 0) return "";
|
|
765
|
+
const noun = count === 1 ? "finding identity was" : "finding identities were";
|
|
766
|
+
return `_${count} ${noun} dropped from the state block to keep this comment within GitHub's size limit._`;
|
|
767
|
+
}
|