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,241 @@
|
|
|
1
|
+
// Verification-leg namespace for item 7 (`docs/item7-rereview-design.md` §3.4).
|
|
2
|
+
// Pure. Follows `runRefuter`'s step shape (one subject, nonced tags, engine-
|
|
3
|
+
// owned verdict vocabulary) but NEVER shares ids, artifacts, or the verdict
|
|
4
|
+
// map with the refuter in the same run (C3, R2-S1).
|
|
5
|
+
//
|
|
6
|
+
// `runVerify` itself is not here yet — this module is the contract the
|
|
7
|
+
// pipeline spawn has to call, so the first live wiring cannot invent a
|
|
8
|
+
// second `F001` collision.
|
|
9
|
+
|
|
10
|
+
import path from "node:path";
|
|
11
|
+
import { blockForgesNonce, wrapBlock } from "#review/boundary";
|
|
12
|
+
import type { Severity } from "#review/findings";
|
|
13
|
+
import {
|
|
14
|
+
type GateStatus,
|
|
15
|
+
type RecoveredSeverity,
|
|
16
|
+
SEVERITY_UNRECOVERABLE,
|
|
17
|
+
type VerifyTrigger,
|
|
18
|
+
} from "./classify";
|
|
19
|
+
import {
|
|
20
|
+
type IdentityInput,
|
|
21
|
+
identitiesMatch,
|
|
22
|
+
identityFromFinding,
|
|
23
|
+
identityFromLocs,
|
|
24
|
+
} from "./identity";
|
|
25
|
+
|
|
26
|
+
export const VERIFY_BATCH_FILE = "verify-batch.json";
|
|
27
|
+
export const VERIFY_STEPS_DIR = "verify";
|
|
28
|
+
export const VERIFIER_AGENT = "verifier";
|
|
29
|
+
|
|
30
|
+
export interface VerifyQueueEntry {
|
|
31
|
+
priorId: string;
|
|
32
|
+
sev: RecoveredSeverity;
|
|
33
|
+
trigger: VerifyTrigger;
|
|
34
|
+
claim: string;
|
|
35
|
+
locs: readonly string[];
|
|
36
|
+
authorReply: string;
|
|
37
|
+
commentBody: string;
|
|
38
|
+
triageTag: string;
|
|
39
|
+
deltaHunks: string;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface VerifySubject extends VerifyQueueEntry {
|
|
43
|
+
vId: string;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export const VERIFY_OUTPUT_CONTRACT = [
|
|
47
|
+
"Your final message must be exactly one JSON object — no prose, no code",
|
|
48
|
+
'fences — of the shape {"results":[{"finding_id":"...","outcome":',
|
|
49
|
+
'"corroborated|refuted|downgraded-latent|inconclusive","proof_refs":',
|
|
50
|
+
'["..."]}]} with exactly one verdict per submitted V### id — never',
|
|
51
|
+
"implied, never extra.",
|
|
52
|
+
].join("\n");
|
|
53
|
+
|
|
54
|
+
export function verifySubjectId(index: number): string {
|
|
55
|
+
return `V${String(index).padStart(3, "0")}`;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function verifyStepName(vId: string): string {
|
|
59
|
+
return `verify-${vId}`;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function verifyArtifactDir(stepsDir: string, vId: string): string {
|
|
63
|
+
return path.join(stepsDir, VERIFY_STEPS_DIR, vId);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function verifyBatchPath(stepsDir: string): string {
|
|
67
|
+
return path.join(stepsDir, VERIFY_BATCH_FILE);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// First trigger wins. Case D/E verify-all must not double-charge a prior
|
|
71
|
+
// already queued by applied/touched/overlap (round 3).
|
|
72
|
+
export function dedupeVerifyQueue(
|
|
73
|
+
queued: readonly VerifyQueueEntry[],
|
|
74
|
+
): VerifyQueueEntry[] {
|
|
75
|
+
const seen = new Set<string>();
|
|
76
|
+
const out: VerifyQueueEntry[] = [];
|
|
77
|
+
for (const entry of queued) {
|
|
78
|
+
if (seen.has(entry.priorId)) continue;
|
|
79
|
+
seen.add(entry.priorId);
|
|
80
|
+
out.push(entry);
|
|
81
|
+
}
|
|
82
|
+
return out;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const SEV_RANK: Record<Severity, number> = {
|
|
86
|
+
BLOCKER: 0,
|
|
87
|
+
CRITICAL: 1,
|
|
88
|
+
WARNING: 2,
|
|
89
|
+
SUGGESTION: 3,
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
// An unrecoverable severity ranks BEFORE BLOCKER, not after SUGGESTION: we
|
|
93
|
+
// genuinely do not know whether the underlying finding is a live BLOCKER
|
|
94
|
+
// (#206), so treating it as low priority would let a capacity cut evict the
|
|
95
|
+
// one entry most worth re-checking. Ranking it most urgent means a known
|
|
96
|
+
// SUGGESTION is evicted before an unknown severity ever is.
|
|
97
|
+
function verifyQueueRank(sev: RecoveredSeverity): number {
|
|
98
|
+
return sev === SEVERITY_UNRECOVERABLE ? -1 : SEV_RANK[sev];
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export function capVerificationQueue(
|
|
102
|
+
queued: readonly VerifyQueueEntry[],
|
|
103
|
+
max: number,
|
|
104
|
+
): { verify: VerifyQueueEntry[]; capped: VerifyQueueEntry[] } {
|
|
105
|
+
const unique = dedupeVerifyQueue(queued);
|
|
106
|
+
if (max < 0) {
|
|
107
|
+
return { verify: [], capped: unique };
|
|
108
|
+
}
|
|
109
|
+
const ranked = unique
|
|
110
|
+
.map((entry, index) => ({ entry, index }))
|
|
111
|
+
.sort((a, b) => {
|
|
112
|
+
const rank = verifyQueueRank(a.entry.sev) - verifyQueueRank(b.entry.sev);
|
|
113
|
+
return rank !== 0 ? rank : a.index - b.index;
|
|
114
|
+
})
|
|
115
|
+
.map((row) => row.entry);
|
|
116
|
+
return {
|
|
117
|
+
verify: ranked.slice(0, max),
|
|
118
|
+
capped: ranked.slice(max),
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export function assignVerifyIds(
|
|
123
|
+
queued: readonly VerifyQueueEntry[],
|
|
124
|
+
): VerifySubject[] {
|
|
125
|
+
return queued.map((entry, i) => ({
|
|
126
|
+
...entry,
|
|
127
|
+
vId: verifySubjectId(i + 1),
|
|
128
|
+
}));
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// Phase E (W-order): the queue closes AFTER dedupe. A discovery survivor
|
|
132
|
+
// whose identity overlaps a prior that was not already queued appends that
|
|
133
|
+
// prior with trigger `overlap`. First trigger still wins (dedupe).
|
|
134
|
+
export function appendOverlapTriggers(
|
|
135
|
+
queued: readonly VerifyQueueEntry[],
|
|
136
|
+
candidates: readonly VerifyQueueEntry[],
|
|
137
|
+
survivors: readonly IdentityInput[],
|
|
138
|
+
): VerifyQueueEntry[] {
|
|
139
|
+
const extra: VerifyQueueEntry[] = [];
|
|
140
|
+
for (const candidate of candidates) {
|
|
141
|
+
const priorIdent = identityFromLocs(candidate.locs);
|
|
142
|
+
if (priorIdent.size === 0) continue;
|
|
143
|
+
const hits = survivors.some((survivor) =>
|
|
144
|
+
identitiesMatch(priorIdent, identityFromFinding(survivor)),
|
|
145
|
+
);
|
|
146
|
+
if (!hits) continue;
|
|
147
|
+
extra.push({ ...candidate, trigger: "overlap" });
|
|
148
|
+
}
|
|
149
|
+
return dedupeVerifyQueue([...queued, ...extra]);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export function triggerCounts(queued: readonly VerifyQueueEntry[]): {
|
|
153
|
+
applied: number;
|
|
154
|
+
touched: number;
|
|
155
|
+
overlap: number;
|
|
156
|
+
verify_all: number;
|
|
157
|
+
} {
|
|
158
|
+
const counts = { applied: 0, touched: 0, overlap: 0, verify_all: 0 };
|
|
159
|
+
for (const entry of queued) {
|
|
160
|
+
if (entry.trigger === "case_b_reply") {
|
|
161
|
+
counts.applied++;
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
counts[entry.trigger]++;
|
|
165
|
+
}
|
|
166
|
+
return counts;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
export function closeVerifyQueue(input: {
|
|
170
|
+
queued: readonly VerifyQueueEntry[];
|
|
171
|
+
overlapCandidates?: readonly VerifyQueueEntry[];
|
|
172
|
+
survivors?: readonly IdentityInput[];
|
|
173
|
+
max: number;
|
|
174
|
+
}): { verify: VerifyQueueEntry[]; capped: VerifyQueueEntry[] } {
|
|
175
|
+
const closed = appendOverlapTriggers(
|
|
176
|
+
input.queued,
|
|
177
|
+
input.overlapCandidates ?? [],
|
|
178
|
+
input.survivors ?? [],
|
|
179
|
+
);
|
|
180
|
+
return capVerificationQueue(closed, input.max);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
export function mapVerifyVerdict(
|
|
184
|
+
outcome: "refuted" | "corroborated" | "downgraded-latent" | "inconclusive",
|
|
185
|
+
): GateStatus {
|
|
186
|
+
if (outcome === "refuted") return "verified-gone";
|
|
187
|
+
if (outcome === "inconclusive") return "unconfirmed";
|
|
188
|
+
return "carried";
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// A judge-proposed semantic match is a verification trigger, never a
|
|
192
|
+
// classification. The return type cannot name `carried`, `suppressed`, or
|
|
193
|
+
// `verified-gone` (J-trigger).
|
|
194
|
+
export function judgeProposedMatch(priorId: string): {
|
|
195
|
+
action: "queue";
|
|
196
|
+
trigger: "overlap";
|
|
197
|
+
priorId: string;
|
|
198
|
+
} {
|
|
199
|
+
return { action: "queue", trigger: "overlap", priorId };
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
export function composeVerifyPrompt(
|
|
203
|
+
subject: VerifySubject,
|
|
204
|
+
nonce: string,
|
|
205
|
+
): string | null {
|
|
206
|
+
const previousFinding = JSON.stringify(
|
|
207
|
+
{
|
|
208
|
+
id: subject.vId,
|
|
209
|
+
prior_id: subject.priorId,
|
|
210
|
+
locs: subject.locs,
|
|
211
|
+
claim: subject.claim,
|
|
212
|
+
},
|
|
213
|
+
null,
|
|
214
|
+
2,
|
|
215
|
+
);
|
|
216
|
+
const blocks = [
|
|
217
|
+
previousFinding,
|
|
218
|
+
subject.deltaHunks,
|
|
219
|
+
subject.authorReply,
|
|
220
|
+
subject.commentBody,
|
|
221
|
+
subject.triageTag,
|
|
222
|
+
];
|
|
223
|
+
if (blocks.some((block) => blockForgesNonce(block, nonce))) return null;
|
|
224
|
+
return [
|
|
225
|
+
"Is this specific defect still present at these locations, at H?",
|
|
226
|
+
"",
|
|
227
|
+
wrapBlock("previous_finding", nonce, previousFinding),
|
|
228
|
+
wrapBlock("patch", nonce, subject.deltaHunks),
|
|
229
|
+
wrapBlock("author_reply", nonce, subject.authorReply),
|
|
230
|
+
wrapBlock("comment_body", nonce, subject.commentBody),
|
|
231
|
+
wrapBlock("triage_tag", nonce, subject.triageTag),
|
|
232
|
+
"",
|
|
233
|
+
"`refuted` means the code at H positively contradicts the claim — cite",
|
|
234
|
+
"the contradicting lines. Absence of a hunter re-finding it is not",
|
|
235
|
+
"disproof. `inconclusive` means you could not tell.",
|
|
236
|
+
"",
|
|
237
|
+
VERIFY_OUTPUT_CONTRACT,
|
|
238
|
+
]
|
|
239
|
+
.filter((line) => line !== "")
|
|
240
|
+
.join("\n");
|
|
241
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
// The boundary-tag rule (C4, `docs/c4-preamble-design.md` §3.3-§3.4): every
|
|
2
|
+
// block of a prompt that this engine did not author is wrapped in a named tag
|
|
3
|
+
// carrying a per-run nonce, so text inside it cannot end its own block and
|
|
4
|
+
// speak as the engine.
|
|
5
|
+
//
|
|
6
|
+
// This module is driver source: it is covered by the engine version, NOT by
|
|
7
|
+
// the prompt-set fingerprint. A prompt-set edit can neither add a tag nor
|
|
8
|
+
// remove one.
|
|
9
|
+
//
|
|
10
|
+
// WHY a nonce instead of Cloudflare's strip (`docs/cloudflare-ai-code-review.md`
|
|
11
|
+
// :186-193). Cloudflare deletes boundary-tag names from user content because a
|
|
12
|
+
// FIXED tag vocabulary can be forged from inside that content. Their inputs are
|
|
13
|
+
// MR bodies and comments, where deleting a stray `</mr_body>` costs nothing.
|
|
14
|
+
// Our first and largest block is a diff: stripping `</patch>`-shaped strings
|
|
15
|
+
// out of it corrupts the code under review — a legitimate PR can add exactly
|
|
16
|
+
// those literals, and a reviewer reading mutated source produces findings about
|
|
17
|
+
// code that does not exist. That is a worse failure than the one being
|
|
18
|
+
// prevented. A nonce closes the same hole without touching a byte, because the
|
|
19
|
+
// tag name cannot be forged by content that was fixed before the nonce existed.
|
|
20
|
+
|
|
21
|
+
// Closed vocabulary. Item 7 widens it here, not at a call site, so a new
|
|
22
|
+
// untrusted block cannot be wrapped under an invented tag name.
|
|
23
|
+
export type BoundaryTag =
|
|
24
|
+
| "patch"
|
|
25
|
+
| "scout_leads"
|
|
26
|
+
| "finding"
|
|
27
|
+
| "findings"
|
|
28
|
+
| "gotchas"
|
|
29
|
+
| "priors"
|
|
30
|
+
| "previous_finding"
|
|
31
|
+
| "author_reply"
|
|
32
|
+
| "comment_body"
|
|
33
|
+
| "triage_tag";
|
|
34
|
+
|
|
35
|
+
// 8 hex characters: short enough to cost nothing in a prompt, wide enough
|
|
36
|
+
// (2^32) that guessing it from inside attacker-authored content is not a
|
|
37
|
+
// strategy. Fixed here rather than at the call site so every artifact this
|
|
38
|
+
// engine writes carries the same shape.
|
|
39
|
+
export const BOUNDARY_NONCE_CHARS = 8;
|
|
40
|
+
|
|
41
|
+
// Bounded, because an unbounded regeneration loop against content that somehow
|
|
42
|
+
// contains every draw would hang a paid run instead of failing it.
|
|
43
|
+
export const MAX_NONCE_ATTEMPTS = 16;
|
|
44
|
+
|
|
45
|
+
export class BoundaryNonceError extends Error {}
|
|
46
|
+
|
|
47
|
+
export function generateBoundaryNonce(): string {
|
|
48
|
+
const bytes = new Uint8Array(BOUNDARY_NONCE_CHARS / 2);
|
|
49
|
+
crypto.getRandomValues(bytes);
|
|
50
|
+
return Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("");
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// ONE definition of "this block could forge its own closing tag", shared by
|
|
54
|
+
// nonce selection and by every driver-side guard. Two spellings of this
|
|
55
|
+
// predicate is how one of them ends up subtly weaker than the other.
|
|
56
|
+
export function blockForgesNonce(content: string, nonce: string): boolean {
|
|
57
|
+
return content.includes(nonce);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// O-3.3. The nonce is chosen against the blocks that exist when it is chosen —
|
|
61
|
+
// the patch, the operator gotchas, the operator priors — and regenerated on
|
|
62
|
+
// collision. Astronomically unlikely, cheap to check, and the check is what
|
|
63
|
+
// makes the guarantee a guarantee rather than an argument about probability.
|
|
64
|
+
//
|
|
65
|
+
// `generate` is injectable so a test can force a collision on the first draw;
|
|
66
|
+
// production never passes it.
|
|
67
|
+
export function selectBoundaryNonce(
|
|
68
|
+
blocks: string[],
|
|
69
|
+
generate: () => string = generateBoundaryNonce,
|
|
70
|
+
): string {
|
|
71
|
+
for (let attempt = 0; attempt < MAX_NONCE_ATTEMPTS; attempt++) {
|
|
72
|
+
const nonce = generate();
|
|
73
|
+
if (!blocks.some((block) => blockForgesNonce(block, nonce))) return nonce;
|
|
74
|
+
}
|
|
75
|
+
throw new BoundaryNonceError(
|
|
76
|
+
`no boundary nonce survived ${MAX_NONCE_ATTEMPTS} attempts against ${blocks.length} blocks`,
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// EMPTY CONTENT RENDERS TO THE EMPTY STRING, never to an empty tag pair. This
|
|
81
|
+
// is load-bearing twice over: `renderLeadsBlock` already returns "" so a scout
|
|
82
|
+
// that found nothing produces the control arm's byte-identical hunter prompt
|
|
83
|
+
// (`review/scout.ts`'s note on M6), and an empty priors list must likewise leave no
|
|
84
|
+
// trace in a system prompt. A `<scout_leads nonce></scout_leads nonce>` pair
|
|
85
|
+
// would break both.
|
|
86
|
+
//
|
|
87
|
+
// Throwing on a forged nonce is the last line of defence, not the first: every
|
|
88
|
+
// block whose content can arrive after the nonce was committed is guarded by
|
|
89
|
+
// the driver before it reaches here (see `runScout` and `runRefuter`), because
|
|
90
|
+
// a throw at prompt-composition time would kill a paid run. A throw that does
|
|
91
|
+
// escape means a block reached composition unguarded — which is a defect to
|
|
92
|
+
// see, not one to paper over by shipping a forgeable boundary.
|
|
93
|
+
export function wrapBlock(
|
|
94
|
+
tag: BoundaryTag,
|
|
95
|
+
nonce: string,
|
|
96
|
+
content: string,
|
|
97
|
+
): string {
|
|
98
|
+
if (content.length === 0) return "";
|
|
99
|
+
if (blockForgesNonce(content, nonce)) {
|
|
100
|
+
throw new BoundaryNonceError(
|
|
101
|
+
`<${tag}> content carries the run's boundary nonce and could forge its own closing tag`,
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
return `<${tag} ${nonce}>\n${content}\n</${tag} ${nonce}>`;
|
|
105
|
+
}
|
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
import { wrapBlock } from "#review/boundary";
|
|
2
|
+
import type { Finding } from "#review/findings";
|
|
3
|
+
|
|
4
|
+
// The consolidation stage's pure half, measured offline on the Martian 50
|
|
5
|
+
// (lab bench/METRICS.md 2026-10-27, base variant, F1 39.7 -> 42.3).
|
|
6
|
+
// scripts/consolidate-replay.ts imports these, so the bench and the engine
|
|
7
|
+
// cannot drift.
|
|
8
|
+
|
|
9
|
+
export interface PlanEntry {
|
|
10
|
+
primary: string;
|
|
11
|
+
sources: string[];
|
|
12
|
+
split?: boolean;
|
|
13
|
+
// A rescued merged-away draft: its own location and evidence are posted.
|
|
14
|
+
draft?: string;
|
|
15
|
+
claim: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface MergedDraft {
|
|
19
|
+
id: string;
|
|
20
|
+
merged_into: string;
|
|
21
|
+
path: string;
|
|
22
|
+
line: number;
|
|
23
|
+
severity: Finding["severity"];
|
|
24
|
+
claim: string;
|
|
25
|
+
proof_refs: string[];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// Rejects a plan that drops a finding, names an unknown one, or detaches a
|
|
29
|
+
// primary from its sources. A rejected plan falls back to the input as is,
|
|
30
|
+
// so a bad call can cost precision but never recall.
|
|
31
|
+
export function validatePlan(
|
|
32
|
+
raw: unknown,
|
|
33
|
+
ids: string[],
|
|
34
|
+
draftIds: Set<string>,
|
|
35
|
+
): PlanEntry[] {
|
|
36
|
+
const plan = (raw as { findings?: unknown })?.findings;
|
|
37
|
+
if (!Array.isArray(plan) || plan.length === 0) {
|
|
38
|
+
throw new Error("no findings array");
|
|
39
|
+
}
|
|
40
|
+
const known = new Set(ids);
|
|
41
|
+
const covered = new Set<string>();
|
|
42
|
+
const out: PlanEntry[] = [];
|
|
43
|
+
for (const e of plan as PlanEntry[]) {
|
|
44
|
+
if (typeof e?.primary !== "string" || !known.has(e.primary)) {
|
|
45
|
+
throw new Error(`unknown primary ${String(e?.primary)}`);
|
|
46
|
+
}
|
|
47
|
+
if (!Array.isArray(e.sources) || !e.sources.includes(e.primary)) {
|
|
48
|
+
throw new Error(`${e.primary}: primary missing from sources`);
|
|
49
|
+
}
|
|
50
|
+
for (const s of e.sources) {
|
|
51
|
+
if (!known.has(s)) throw new Error(`unknown source ${s}`);
|
|
52
|
+
covered.add(s);
|
|
53
|
+
}
|
|
54
|
+
if (typeof e.claim !== "string" || e.claim.trim().length === 0) {
|
|
55
|
+
throw new Error(`${e.primary}: empty claim`);
|
|
56
|
+
}
|
|
57
|
+
if (e.draft !== undefined) {
|
|
58
|
+
if (!draftIds.has(e.draft)) throw new Error(`unknown draft ${e.draft}`);
|
|
59
|
+
if (e.split !== true)
|
|
60
|
+
throw new Error(`${e.draft}: a rescue must be a split`);
|
|
61
|
+
}
|
|
62
|
+
out.push({ ...e, split: e.split === true });
|
|
63
|
+
}
|
|
64
|
+
const missing = ids.filter((id) => !covered.has(id));
|
|
65
|
+
if (missing.length > 0) throw new Error(`dropped ${missing.join(",")}`);
|
|
66
|
+
return out;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// One output finding per plan entry. A merge keeps the primary's location,
|
|
70
|
+
// severity, tier and verdict, and pools the evidence. A split aside was never
|
|
71
|
+
// refuted on its own, so it posts advisory with `not_submitted`.
|
|
72
|
+
export function applyPlan(
|
|
73
|
+
findings: Finding[],
|
|
74
|
+
plan: PlanEntry[],
|
|
75
|
+
drafts: MergedDraft[],
|
|
76
|
+
): Finding[] {
|
|
77
|
+
const byId = new Map(findings.map((f) => [f.id, f]));
|
|
78
|
+
const draftById = new Map(drafts.map((d) => [d.id, d]));
|
|
79
|
+
const splits = new Map<string, number>();
|
|
80
|
+
return plan.map((e) => {
|
|
81
|
+
const primary = byId.get(e.primary) as Finding;
|
|
82
|
+
const proofRefs = [
|
|
83
|
+
...new Set(e.sources.flatMap((s) => byId.get(s)?.proof_refs ?? [])),
|
|
84
|
+
];
|
|
85
|
+
if (!e.split) {
|
|
86
|
+
return { ...primary, claim: e.claim, proof_refs: proofRefs };
|
|
87
|
+
}
|
|
88
|
+
const n = (splits.get(e.primary) ?? 0) + 1;
|
|
89
|
+
splits.set(e.primary, n);
|
|
90
|
+
const draft = e.draft === undefined ? undefined : draftById.get(e.draft);
|
|
91
|
+
return {
|
|
92
|
+
...primary,
|
|
93
|
+
id: `${primary.id}s${n}`,
|
|
94
|
+
claim: e.claim,
|
|
95
|
+
...(draft === undefined
|
|
96
|
+
? { proof_refs: primary.proof_refs }
|
|
97
|
+
: {
|
|
98
|
+
path: draft.path,
|
|
99
|
+
line: draft.line,
|
|
100
|
+
severity: draft.severity,
|
|
101
|
+
proof_refs: draft.proof_refs,
|
|
102
|
+
}),
|
|
103
|
+
tier: "advisory",
|
|
104
|
+
refuter_verdict: "not_submitted",
|
|
105
|
+
};
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// The fields the consolidator sees, in input order. It never sees evidence,
|
|
110
|
+
// tier or verdict: it works from the claims alone (prompts/consolidate.md).
|
|
111
|
+
export function consolidationInput(findings: readonly Finding[]) {
|
|
112
|
+
return findings.map((f) => ({
|
|
113
|
+
id: f.id,
|
|
114
|
+
path: f.path,
|
|
115
|
+
line: f.line,
|
|
116
|
+
severity: f.severity,
|
|
117
|
+
hunter: f.hunter,
|
|
118
|
+
claim: f.claim,
|
|
119
|
+
}));
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// The measured user prompt was `Findings:\n<JSON>`. The engine wraps the JSON
|
|
123
|
+
// in the run's nonce boundary (§3.4): claims are hunter prose about
|
|
124
|
+
// attacker-controlled code. wrapBlock throws BoundaryNonceError when a claim
|
|
125
|
+
// carries the nonce; the caller treats that as a passthrough.
|
|
126
|
+
export function consolidationPrompt(
|
|
127
|
+
findings: readonly Finding[],
|
|
128
|
+
nonce: string,
|
|
129
|
+
): string {
|
|
130
|
+
return `Findings:\n${wrapBlock(
|
|
131
|
+
"findings",
|
|
132
|
+
nonce,
|
|
133
|
+
JSON.stringify(consolidationInput(findings), null, 2),
|
|
134
|
+
)}`;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// Engine-side guard over a validated plan, applied in finish() where tiers are
|
|
138
|
+
// known. It enforces in code the rules the prompt already states ("primary is
|
|
139
|
+
// the most severe source"; one output finding per defect), because the model
|
|
140
|
+
// does not always keep them, and a slip must never move a blocking finding off
|
|
141
|
+
// the blocking tier or post it twice. Measured effect: of the 49 Martian 50
|
|
142
|
+
// plans, 3 change (sentry-3, discourse-4, cal-11059).
|
|
143
|
+
//
|
|
144
|
+
// `stats`, when passed, receives the number of entries the rules changed:
|
|
145
|
+
// promoted, folded away, or appended. pipeline.json records it so a repair is
|
|
146
|
+
// never silent. Rule 0 reorders evidence and changes no posted finding, so it
|
|
147
|
+
// is not counted.
|
|
148
|
+
export function enforcePlanInvariants(
|
|
149
|
+
plan: PlanEntry[],
|
|
150
|
+
findings: readonly Finding[],
|
|
151
|
+
stats?: { repairs: number },
|
|
152
|
+
): PlanEntry[] {
|
|
153
|
+
const blocking = new Set(
|
|
154
|
+
findings.filter((f) => f.tier === "blocking").map((f) => f.id),
|
|
155
|
+
);
|
|
156
|
+
let repairs = 0;
|
|
157
|
+
|
|
158
|
+
// Rule 0 — primary first: applyPlan pools proof_refs in sources order, and
|
|
159
|
+
// clusterByRootCause keys on proof_refs[0] ("where the defect lives",
|
|
160
|
+
// root-cause.ts), so the primary's own refs must lead, as the dedupe fold
|
|
161
|
+
// already does (dedupe.ts). Splits build from their primary alone, so they
|
|
162
|
+
// are left as they are.
|
|
163
|
+
const primaryFirst = (e: PlanEntry): PlanEntry =>
|
|
164
|
+
e.split || e.sources[0] === e.primary
|
|
165
|
+
? e
|
|
166
|
+
: {
|
|
167
|
+
...e,
|
|
168
|
+
sources: [e.primary, ...e.sources.filter((s) => s !== e.primary)],
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
// Rule 1 — promotion: a merge whose primary is not blocking would post the
|
|
172
|
+
// merged finding off the blocking tier, so the first blocking source leads.
|
|
173
|
+
// Promotion changes the primary, so Rule 0 runs again on its output.
|
|
174
|
+
const promoted = plan
|
|
175
|
+
.map(primaryFirst)
|
|
176
|
+
.map((e) => {
|
|
177
|
+
if (e.split || blocking.has(e.primary)) return e;
|
|
178
|
+
const first = e.sources.find((s) => blocking.has(s));
|
|
179
|
+
if (first === undefined) return e;
|
|
180
|
+
repairs += 1;
|
|
181
|
+
return { ...e, primary: first };
|
|
182
|
+
})
|
|
183
|
+
.map(primaryFirst);
|
|
184
|
+
|
|
185
|
+
// Rule 2 — fold: two non-split entries with one primary would post that
|
|
186
|
+
// finding twice. Splits are asides with their own ids and never fold.
|
|
187
|
+
const folded: PlanEntry[] = [];
|
|
188
|
+
const firstByPrimary = new Map<string, PlanEntry>();
|
|
189
|
+
for (const e of promoted) {
|
|
190
|
+
if (e.split) {
|
|
191
|
+
folded.push(e);
|
|
192
|
+
continue;
|
|
193
|
+
}
|
|
194
|
+
const first = firstByPrimary.get(e.primary);
|
|
195
|
+
if (first === undefined) {
|
|
196
|
+
const copy = { ...e, sources: [...e.sources] };
|
|
197
|
+
firstByPrimary.set(e.primary, copy);
|
|
198
|
+
folded.push(copy);
|
|
199
|
+
continue;
|
|
200
|
+
}
|
|
201
|
+
for (const s of e.sources) {
|
|
202
|
+
if (!first.sources.includes(s)) first.sources.push(s);
|
|
203
|
+
}
|
|
204
|
+
repairs += 1;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// Rule 3 — orphan: a blocking finding carried only by a split (or by a
|
|
208
|
+
// non-blocking merge) would no longer post on the blocking tier.
|
|
209
|
+
const covered = new Set(
|
|
210
|
+
folded
|
|
211
|
+
.filter((e) => !e.split && blocking.has(e.primary))
|
|
212
|
+
.flatMap((e) => e.sources),
|
|
213
|
+
);
|
|
214
|
+
for (const f of findings) {
|
|
215
|
+
if (f.tier !== "blocking" || covered.has(f.id)) continue;
|
|
216
|
+
folded.push({
|
|
217
|
+
primary: f.id,
|
|
218
|
+
sources: [f.id],
|
|
219
|
+
split: false,
|
|
220
|
+
claim: f.claim,
|
|
221
|
+
});
|
|
222
|
+
repairs += 1;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
// Rule 4 — every finding keeps a home: validatePlan counts a split entry's
|
|
226
|
+
// sources as covered, but applyPlan builds a split from its primary alone, so
|
|
227
|
+
// a finding named only in a split's sources would be posted nowhere. The
|
|
228
|
+
// prompt forbids dropping a whole finding (1 of the 49 measured plans did:
|
|
229
|
+
// cal-11059).
|
|
230
|
+
const homed = new Set(
|
|
231
|
+
folded.filter((e) => !e.split).flatMap((e) => e.sources),
|
|
232
|
+
);
|
|
233
|
+
for (const f of findings) {
|
|
234
|
+
if (homed.has(f.id)) continue;
|
|
235
|
+
folded.push({
|
|
236
|
+
primary: f.id,
|
|
237
|
+
sources: [f.id],
|
|
238
|
+
split: false,
|
|
239
|
+
claim: f.claim,
|
|
240
|
+
});
|
|
241
|
+
repairs += 1;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
if (stats !== undefined) stats.repairs = repairs;
|
|
245
|
+
return folded;
|
|
246
|
+
}
|