@xaccefy/pi-casefile 0.9.3 → 0.10.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/README.md +31 -67
- package/package.json +14 -17
- package/src/confirmation.ts +729 -0
- package/src/evidence.ts +4 -4
- package/src/index.ts +189 -293
- package/src/ledger-internal.ts +321 -0
- package/src/ledger.ts +75 -1142
- package/src/oob-oracle.ts +279 -0
- package/src/poc-runner.ts +10 -0
- package/src/scratchpad.ts +5 -6
- package/src/workflow.ts +46 -327
- package/skills/casefile/SKILL.md +0 -44
- package/src/ledger-worker-entry.ts +0 -35
- package/src/ledger-worker.ts +0 -77
- package/src/pipeline-submit.ts +0 -797
|
@@ -0,0 +1,729 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Two-phase PoC confirmation gate (phase 1: PromoteFinding evidence bundle;
|
|
3
|
+
* phase 2: main-agent ConfirmFinding commit).
|
|
4
|
+
*
|
|
5
|
+
* Extracted verbatim from ledger.ts so the trust-critical machinery lives in
|
|
6
|
+
* one readable module. ledger.ts re-exports every public symbol here, so
|
|
7
|
+
* callers and tests are unchanged.
|
|
8
|
+
*
|
|
9
|
+
* The gate's contract (docs/confirmation-design.md):
|
|
10
|
+
* - Zero exit + complete output capture = run integrity, never proof.
|
|
11
|
+
* - Evidence must be nonce-bound, schema-valid, carry a discriminating
|
|
12
|
+
* response-body predicate, and survive durable-hash re-verification.
|
|
13
|
+
* - Target-dependence requires a machine differential: inter-host control run,
|
|
14
|
+
* intra-target same-host baseline, or a source-separated OOB token delta.
|
|
15
|
+
* - Phase 2 requires a fresh harness-owned replay bound to the verdict; only
|
|
16
|
+
* the ledger can transition a case to confirmed.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { createHash } from "node:crypto";
|
|
20
|
+
import { readFileSync } from "node:fs";
|
|
21
|
+
|
|
22
|
+
import { basename } from "node:path";
|
|
23
|
+
|
|
24
|
+
import {
|
|
25
|
+
evidenceNonceMatches,
|
|
26
|
+
type MainAgentVerdict,
|
|
27
|
+
normalizeEvidence,
|
|
28
|
+
parsePoCEvidence,
|
|
29
|
+
validateMainAgentVerdict,
|
|
30
|
+
} from "./evidence.ts";
|
|
31
|
+
import { type HarnessVerifyResult, sameRequest, verifyUrlBindingError } from "./harness-verify.ts";
|
|
32
|
+
import type {
|
|
33
|
+
CaseRecord,
|
|
34
|
+
CaseUpdateResult,
|
|
35
|
+
EvidenceItem,
|
|
36
|
+
MainAgentVerdictRecord,
|
|
37
|
+
MainAgentVerification,
|
|
38
|
+
NormalizedCaseInput,
|
|
39
|
+
PendingConfirmation,
|
|
40
|
+
PocEvidenceRun,
|
|
41
|
+
} from "./ledger.ts";
|
|
42
|
+
import { getCaseById, readWorkspaceArtifact } from "./ledger.ts";
|
|
43
|
+
import {
|
|
44
|
+
buildRecord,
|
|
45
|
+
getDb,
|
|
46
|
+
insertEvidenceItem,
|
|
47
|
+
upsertCase,
|
|
48
|
+
validateCase,
|
|
49
|
+
withImmediateTransaction,
|
|
50
|
+
} from "./ledger-internal.ts";
|
|
51
|
+
|
|
52
|
+
/** PoC evidence has a tighter runner-side cap and must remain equally bounded on re-read. */
|
|
53
|
+
const POC_EVIDENCE_MAX_BYTES = 256 * 1024;
|
|
54
|
+
|
|
55
|
+
/** Immutable module-start role; child shells cannot upgrade this process by unsetting an env var. */
|
|
56
|
+
const PROCESS_STARTED_AS_SUBAGENT = process.env.PI_SUBAGENT_CHILD === "1";
|
|
57
|
+
|
|
58
|
+
/** Pending confirmation expires after 1h — re-run PromoteFinding for a fresh bundle. */
|
|
59
|
+
export const PENDING_CONFIRM_TTL_MS = 60 * 60 * 1000;
|
|
60
|
+
|
|
61
|
+
// Re-declared here as narrow internal helpers; ledger.ts keeps the shared copies.
|
|
62
|
+
function stableShortId(input: string): string {
|
|
63
|
+
return createHash("sha1").update(input).digest("hex").slice(0, 10);
|
|
64
|
+
}
|
|
65
|
+
function validateRunEvidence(run: PocEvidenceRun, label: string): void {
|
|
66
|
+
if (!run.completed) {
|
|
67
|
+
throw new Error(`${label} did not complete; a crash is not evidence`);
|
|
68
|
+
}
|
|
69
|
+
if (!run.outputComplete) {
|
|
70
|
+
throw new Error(`${label} output capture was incomplete; evidence checks are unsafe`);
|
|
71
|
+
}
|
|
72
|
+
if (run.exitCode !== 0) {
|
|
73
|
+
throw new Error(
|
|
74
|
+
`${label} exited with ${run.exitCode}; exit 0 is required for a complete run but is never sufficient proof`,
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
if (!run.evidence || !run.evidenceSha256) {
|
|
78
|
+
throw new Error(
|
|
79
|
+
`${label} has no evidence.json — the PoC must write evidence to $PI_POC_EVIDENCE_DIR`,
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
if (!evidenceNonceMatches(run.evidence, run.nonce)) {
|
|
83
|
+
throw new Error(`${label} evidence nonce mismatch — evidence not bound to this run`);
|
|
84
|
+
}
|
|
85
|
+
const parsed = parsePoCEvidence(run.evidence);
|
|
86
|
+
if (!parsed.ok) {
|
|
87
|
+
throw new Error(`${label} evidence contract invalid: ${parsed.error}`);
|
|
88
|
+
}
|
|
89
|
+
if (!run.evidencePath) {
|
|
90
|
+
throw new Error(`${label} has no durable evidencePath; ephemeral evidence cannot confirm`);
|
|
91
|
+
}
|
|
92
|
+
const artifact = readWorkspaceArtifact(run.evidencePath);
|
|
93
|
+
if (artifact.bytes.byteLength > POC_EVIDENCE_MAX_BYTES) {
|
|
94
|
+
throw new Error(
|
|
95
|
+
`${label} durable evidence exceeds ${POC_EVIDENCE_MAX_BYTES} bytes; evidence cannot be revalidated safely`,
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
const durableHash = createHash("sha256").update(artifact.bytes).digest("hex");
|
|
99
|
+
if (durableHash !== run.evidenceSha256) {
|
|
100
|
+
throw new Error(`${label} durable evidence hash does not match evidenceSha256`);
|
|
101
|
+
}
|
|
102
|
+
let durableRaw: unknown;
|
|
103
|
+
try {
|
|
104
|
+
durableRaw = JSON.parse(artifact.bytes.toString("utf8"));
|
|
105
|
+
} catch (error) {
|
|
106
|
+
throw new Error(`${label} durable evidence is not valid JSON: ${(error as Error).message}`);
|
|
107
|
+
}
|
|
108
|
+
const durable = parsePoCEvidence(durableRaw);
|
|
109
|
+
if (!durable.ok) {
|
|
110
|
+
throw new Error(`${label} durable evidence contract invalid: ${durable.error}`);
|
|
111
|
+
}
|
|
112
|
+
if (
|
|
113
|
+
normalizeEvidence(durable.evidence) !== normalizeEvidence(run.evidence) ||
|
|
114
|
+
JSON.stringify(durable.evidence.observations) !== JSON.stringify(run.evidence.observations)
|
|
115
|
+
) {
|
|
116
|
+
throw new Error(`${label} durable evidence bytes do not match the stored evidence object`);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** Determinism + differential on normalized evidence (nonce/observations stripped). */
|
|
121
|
+
function assertEvidenceDifferential(bundle: PendingConfirmation, isIntra = false): void {
|
|
122
|
+
const [r1, r2] = bundle.targetRuns;
|
|
123
|
+
if (normalizeEvidence(r1.evidence) !== normalizeEvidence(r2.evidence)) {
|
|
124
|
+
throw new Error(
|
|
125
|
+
"Target runs produced inconsistent evidence — the exploit did not reproduce deterministically",
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
// Intra-target target-dependence is proven by the harness attack-vs-baseline
|
|
129
|
+
// replay (same host), not by comparing a target run to a separate control run.
|
|
130
|
+
if (isIntra) return;
|
|
131
|
+
// OOB-only bundles prove target-dependence via the oracle token differential
|
|
132
|
+
// (assertMachineConfirmation judges callbackVerified); no control run exists.
|
|
133
|
+
if (!bundle.controlRun && bundle.callbackVerified?.attempted) return;
|
|
134
|
+
if (!bundle.controlRun) {
|
|
135
|
+
throw new Error("inter-host confirmation requires a control run");
|
|
136
|
+
}
|
|
137
|
+
if (normalizeEvidence(r1.evidence) === normalizeEvidence(bundle.controlRun.evidence)) {
|
|
138
|
+
throw new Error(
|
|
139
|
+
"Control run produced identical evidence to the target — the claimed impact is not target-dependent",
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function assertMachineConfirmation(bundle: PendingConfirmation): void {
|
|
145
|
+
const oob = bundle.callbackVerified;
|
|
146
|
+
if (oob?.attempted) {
|
|
147
|
+
if (oob.targetHits === 0) {
|
|
148
|
+
throw new Error(
|
|
149
|
+
`OOB VERIFY FAILED: no interaction with the target-run callback token. ${oob.note}`,
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
if (oob.controlHits > 0) {
|
|
153
|
+
throw new Error(
|
|
154
|
+
`OOB VERIFY FAILED: the control-run callback token received ${oob.controlHits} interaction(s) — the callback is not target-dependent. ${oob.note}`,
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
if (oob.sourceSeparated !== true) {
|
|
158
|
+
throw new Error(
|
|
159
|
+
"OOB VERIFY FAILED: callback source separation was not established. " +
|
|
160
|
+
"A loopback listener reachable by the PoC is diagnostic telemetry, not proof that the target caused the interaction.",
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
assertHarnessTargetOnly(
|
|
167
|
+
bundle.harnessVerified,
|
|
168
|
+
"HARNESS DIFFERENTIAL FAILED",
|
|
169
|
+
"no machine-owned target/control replay was recorded",
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function assertHarnessTargetOnly(
|
|
174
|
+
harness: HarnessVerifyResult | undefined,
|
|
175
|
+
label: string,
|
|
176
|
+
missingNote: string,
|
|
177
|
+
): asserts harness is HarnessVerifyResult {
|
|
178
|
+
if (
|
|
179
|
+
!harness?.attempted ||
|
|
180
|
+
harness.pass !== true ||
|
|
181
|
+
harness.differential !== "target_only" ||
|
|
182
|
+
harness.target?.matched !== true ||
|
|
183
|
+
harness.control?.matched !== false
|
|
184
|
+
) {
|
|
185
|
+
throw new Error(`${label}: ${harness?.note ?? missingNote}`);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function assertHarnessCanary(
|
|
190
|
+
harness: HarnessVerifyResult | undefined,
|
|
191
|
+
required: boolean,
|
|
192
|
+
label: string,
|
|
193
|
+
): void {
|
|
194
|
+
if (!required) return;
|
|
195
|
+
if (
|
|
196
|
+
harness?.canary?.attempted !== true ||
|
|
197
|
+
harness.canary.pass !== true ||
|
|
198
|
+
harness.canary.targetObserved !== true ||
|
|
199
|
+
harness.canary.controlObserved !== false ||
|
|
200
|
+
harness.proofStrength !== "canary_differential"
|
|
201
|
+
) {
|
|
202
|
+
throw new Error(`${label}: ${harness?.canary?.note ?? "required canary transcript missing"}`);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
function assertMainAgentVerification(
|
|
207
|
+
bundle: PendingConfirmation,
|
|
208
|
+
verification: MainAgentVerification | undefined,
|
|
209
|
+
isIntra = false,
|
|
210
|
+
): asserts verification is MainAgentVerification {
|
|
211
|
+
if (!verification) {
|
|
212
|
+
throw new Error(
|
|
213
|
+
"MAIN-AGENT REPLAY REQUIRED: ConfirmFinding must produce a fresh harness-owned target/control transcript",
|
|
214
|
+
);
|
|
215
|
+
}
|
|
216
|
+
const at = Date.parse(verification.at);
|
|
217
|
+
const bundleAt = Date.parse(bundle.ranAt);
|
|
218
|
+
const now = Date.now();
|
|
219
|
+
if (
|
|
220
|
+
!Number.isFinite(at) ||
|
|
221
|
+
!Number.isFinite(bundleAt) ||
|
|
222
|
+
at < bundleAt ||
|
|
223
|
+
at > now + 30_000 ||
|
|
224
|
+
now - at > 5 * 60 * 1000
|
|
225
|
+
) {
|
|
226
|
+
throw new Error(
|
|
227
|
+
"MAIN-AGENT REPLAY FAILED: transcript timestamp must be valid, newer than phase 1, and no more than 5 minutes old",
|
|
228
|
+
);
|
|
229
|
+
}
|
|
230
|
+
assertHarnessTargetOnly(
|
|
231
|
+
verification.result,
|
|
232
|
+
"MAIN-AGENT REPLAY FAILED",
|
|
233
|
+
"no fresh phase-2 target/control replay was recorded",
|
|
234
|
+
);
|
|
235
|
+
assertHarnessCanary(
|
|
236
|
+
verification.result,
|
|
237
|
+
bundle.targetRuns[0].evidence.verify.canary !== undefined,
|
|
238
|
+
"MAIN-AGENT CANARY FAILED",
|
|
239
|
+
);
|
|
240
|
+
// OOB-only bundles bind by TOKEN identity (enforced at store time on
|
|
241
|
+
// evidence.verify.url); there is no control host to bind a transcript to.
|
|
242
|
+
if (bundle.callbackVerified?.attempted && bundle.oobTokens) return;
|
|
243
|
+
const targetUrl = verification.result.target?.url;
|
|
244
|
+
const controlUrl = verification.result.control?.url;
|
|
245
|
+
const targetIdentity = bundle.targetRuns[0].target;
|
|
246
|
+
if (!targetUrl || verifyUrlBindingError(targetUrl, targetIdentity)) {
|
|
247
|
+
throw new Error("MAIN-AGENT REPLAY FAILED: target transcript is not bound to the case target");
|
|
248
|
+
}
|
|
249
|
+
// Intra-target: the "control" transcript is the legitimate baseline request,
|
|
250
|
+
// which is bound to the SAME case target. Inter-host: it is bound to the
|
|
251
|
+
// distinct control target.
|
|
252
|
+
const controlBindTarget = isIntra ? targetIdentity : bundle.controlTarget;
|
|
253
|
+
if (!controlUrl || !controlBindTarget || verifyUrlBindingError(controlUrl, controlBindTarget)) {
|
|
254
|
+
throw new Error(
|
|
255
|
+
isIntra
|
|
256
|
+
? "MAIN-AGENT REPLAY FAILED: baseline transcript is not bound to the case target"
|
|
257
|
+
: "MAIN-AGENT REPLAY FAILED: control transcript is not bound to control_target",
|
|
258
|
+
);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Gate for phase 1 of promotion: case must exist, be investigating, and have
|
|
264
|
+
* poc/evidence/impact/severity/target. The disconfirmation is provided by the
|
|
265
|
+
* main agent at confirm time, so it is NOT a precondition here. Returns the
|
|
266
|
+
* record when promotable, throws otherwise. Exported so PromoteFinding can
|
|
267
|
+
* validate BEFORE paying for (potentially slow) sandboxed PoC runs.
|
|
268
|
+
*/
|
|
269
|
+
export function assertPromotable(id: string): CaseRecord {
|
|
270
|
+
const current = getCaseById(id);
|
|
271
|
+
if (!current) {
|
|
272
|
+
throw new Error(`Case not found: ${id}`);
|
|
273
|
+
}
|
|
274
|
+
if (current.status !== "investigating") {
|
|
275
|
+
throw new Error(`PromoteFinding requires an investigating case (current: ${current.status})`);
|
|
276
|
+
}
|
|
277
|
+
if (!current.poc) {
|
|
278
|
+
throw new Error("CONFIRMED requires poc; set poc on the case first");
|
|
279
|
+
}
|
|
280
|
+
if (!current.evidence) {
|
|
281
|
+
throw new Error("CONFIRMED requires evidence; set evidence on the case first");
|
|
282
|
+
}
|
|
283
|
+
if (!current.impact) {
|
|
284
|
+
throw new Error("CONFIRMED requires impact; set impact on the case first");
|
|
285
|
+
}
|
|
286
|
+
if (!current.severity) {
|
|
287
|
+
throw new Error("CONFIRMED requires severity; set severity on the case first");
|
|
288
|
+
}
|
|
289
|
+
if (!current.target) {
|
|
290
|
+
throw new Error(
|
|
291
|
+
"CONFIRMED requires target (what host/repo/scope this affects); set target on the case first",
|
|
292
|
+
);
|
|
293
|
+
}
|
|
294
|
+
// Evidence-chain closure: the observation item must be ARTIFACT-BACKED. A
|
|
295
|
+
// summary-only observation is agent prose about itself — promotion requires
|
|
296
|
+
// a real file with its SHA-256 as the initial signal. (The reproduction item
|
|
297
|
+
// is always artifact-backed: the gate writes it from the evidence hash.)
|
|
298
|
+
if (!current.evidenceItems?.some((e: EvidenceItem) => e.role === "observation" && e.sha256)) {
|
|
299
|
+
throw new Error(
|
|
300
|
+
"Evidence chain incomplete: CONFIRMED requires an artifact-backed observation evidence item " +
|
|
301
|
+
"(EvidenceAdd role=observation with artifact_path — the initial signal, stored as basename + SHA-256) " +
|
|
302
|
+
"in addition to the auto-recorded reproduction item. Add the artifact-backed observation item and retry promotion.",
|
|
303
|
+
);
|
|
304
|
+
}
|
|
305
|
+
return current;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* Phase 1 (intra-target): validate a same-host attack-vs-baseline bundle. The
|
|
310
|
+
* differential is proven by the harness replay (attack matched, baseline did
|
|
311
|
+
* not, both against the case target), not by a separate control run — the
|
|
312
|
+
* discriminating variable is the request's identity or a parameter, not the host.
|
|
313
|
+
*/
|
|
314
|
+
function validateIntraTargetBundle(
|
|
315
|
+
current: CaseRecord,
|
|
316
|
+
id: string,
|
|
317
|
+
bundle: PendingConfirmation,
|
|
318
|
+
): CaseRecord {
|
|
319
|
+
if (bundle.targetRuns.length !== 2) {
|
|
320
|
+
throw new Error("Intra-target confirmation requires two target runs");
|
|
321
|
+
}
|
|
322
|
+
if (bundle.controlRun || bundle.controlTarget) {
|
|
323
|
+
throw new Error(
|
|
324
|
+
"Intra-target confirmation must not carry a control run or control target — the baseline is a same-host request inside the evidence",
|
|
325
|
+
);
|
|
326
|
+
}
|
|
327
|
+
const targetRunTarget = bundle.targetRuns[0]?.target;
|
|
328
|
+
if (!targetRunTarget || bundle.targetRuns.some((r) => r.target !== targetRunTarget)) {
|
|
329
|
+
throw new Error("Intra-target confirmation requires both runs against the same case target");
|
|
330
|
+
}
|
|
331
|
+
let pocHash: string | undefined;
|
|
332
|
+
try {
|
|
333
|
+
pocHash = createHash("sha256").update(readFileSync(bundle.pocPath)).digest("hex");
|
|
334
|
+
} catch {
|
|
335
|
+
pocHash = undefined;
|
|
336
|
+
}
|
|
337
|
+
if (!pocHash || (bundle.pocSha256 && bundle.pocSha256 !== pocHash)) {
|
|
338
|
+
throw new Error("pocSha256 does not match the PoC file on disk");
|
|
339
|
+
}
|
|
340
|
+
for (const run of bundle.targetRuns) {
|
|
341
|
+
validateRunEvidence(run, `${run.mode} run`);
|
|
342
|
+
const ev = run.evidence;
|
|
343
|
+
if (ev.verify.mode !== "intra_target") {
|
|
344
|
+
throw new Error(
|
|
345
|
+
"INTRA-TARGET FAILED: each run's evidence.verify.mode must be 'intra_target'",
|
|
346
|
+
);
|
|
347
|
+
}
|
|
348
|
+
if (!ev.baseline) {
|
|
349
|
+
throw new Error(
|
|
350
|
+
"INTRA-TARGET FAILED: evidence.baseline (a legitimate same-host request) is required",
|
|
351
|
+
);
|
|
352
|
+
}
|
|
353
|
+
const attackBinding = verifyUrlBindingError(ev.verify.url, targetRunTarget);
|
|
354
|
+
if (attackBinding) throw new Error(`ATTACK BINDING FAILED: ${attackBinding}`);
|
|
355
|
+
const baselineBinding = verifyUrlBindingError(ev.baseline.url, targetRunTarget);
|
|
356
|
+
if (baselineBinding) throw new Error(`BASELINE BINDING FAILED: ${baselineBinding}`);
|
|
357
|
+
if (ev.baseline && sameRequest(ev.verify, ev.baseline)) {
|
|
358
|
+
throw new Error(
|
|
359
|
+
"INTRA-TARGET FAILED: attack and baseline requests are identical — vary identity or a parameter",
|
|
360
|
+
);
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
if (bundle.caseId !== id) throw new Error("Pending confirmation caseId mismatch");
|
|
364
|
+
assertEvidenceDifferential(bundle, true);
|
|
365
|
+
// Machine floor: attack matched, baseline did not, both against the case target.
|
|
366
|
+
assertMachineConfirmation(bundle);
|
|
367
|
+
assertHarnessCanary(
|
|
368
|
+
bundle.harnessVerified,
|
|
369
|
+
bundle.targetRuns[0].evidence.verify.canary !== undefined,
|
|
370
|
+
"PHASE-1 CANARY FAILED",
|
|
371
|
+
);
|
|
372
|
+
const next = buildRecord({ pendingConfirmation: bundle }, current);
|
|
373
|
+
validateCase(next);
|
|
374
|
+
return next;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/**
|
|
378
|
+
* Phase 1: record the harness-observed evidence bundle on the case. The whole
|
|
379
|
+
* contract is validated here — same-file control, nonce binding, run
|
|
380
|
+
* completion, determinism across the two target runs, and the target/control
|
|
381
|
+
* differential — so a bundle that cannot promote is rejected before the
|
|
382
|
+
* main agent performs phase-2 review.
|
|
383
|
+
*/
|
|
384
|
+
export function storePendingConfirmation(id: string, bundle: PendingConfirmation): CaseRecord {
|
|
385
|
+
const db = getDb();
|
|
386
|
+
return withImmediateTransaction(db, () => {
|
|
387
|
+
const current = getCaseById(id);
|
|
388
|
+
if (!current) throw new Error(`Case not found: ${id}`);
|
|
389
|
+
if (current.status !== "investigating") {
|
|
390
|
+
throw new Error(
|
|
391
|
+
`Pending confirmation requires an investigating case (current: ${current.status})`,
|
|
392
|
+
);
|
|
393
|
+
}
|
|
394
|
+
if (bundle.caseId !== id) throw new Error("Pending confirmation caseId mismatch");
|
|
395
|
+
if (bundle.mode === "intra_target") {
|
|
396
|
+
const next = validateIntraTargetBundle(current, id, bundle);
|
|
397
|
+
upsertCase(db, next);
|
|
398
|
+
return next;
|
|
399
|
+
}
|
|
400
|
+
// Control-run requirements key off controlRun PRESENCE, not the OOB flag:
|
|
401
|
+
// an OOB-only bundle has no control run (token differential instead), but
|
|
402
|
+
// an OOB+control bundle still carries one and gets the full checks.
|
|
403
|
+
if (!bundle.controlRun && !bundle.callbackVerified) {
|
|
404
|
+
throw new Error("Pending confirmation requires two target runs and one control run");
|
|
405
|
+
}
|
|
406
|
+
if (bundle.controlRun && (!bundle.pocPath || !bundle.controlPath || !bundle.controlTarget)) {
|
|
407
|
+
throw new Error("Pending confirmation requires pocPath, controlPath, and controlTarget");
|
|
408
|
+
}
|
|
409
|
+
// Control-target binding (machine-verified here, not just in the tool
|
|
410
|
+
// layer): the control run must actually have targeted the declared
|
|
411
|
+
// control_target, that target must differ from the target runs' target,
|
|
412
|
+
// and the control target must differ from the case's target — otherwise
|
|
413
|
+
// "the control demonstrated nothing on the vulnerable target" passes.
|
|
414
|
+
const targetRunTarget = bundle.targetRuns[0]?.target;
|
|
415
|
+
if (!targetRunTarget || bundle.targetRuns.some((r) => r.target !== targetRunTarget)) {
|
|
416
|
+
throw new Error(
|
|
417
|
+
"Pending confirmation requires both target runs against the same case target",
|
|
418
|
+
);
|
|
419
|
+
}
|
|
420
|
+
if (bundle.controlRun) {
|
|
421
|
+
if (!bundle.controlRun.target || bundle.controlRun.target !== bundle.controlTarget) {
|
|
422
|
+
throw new Error(
|
|
423
|
+
"CONTROL BINDING FAILED: controlRun.target must equal control_target — a control run " +
|
|
424
|
+
"against a different host than the one declared proves nothing.",
|
|
425
|
+
);
|
|
426
|
+
}
|
|
427
|
+
if (bundle.controlRun.target === targetRunTarget) {
|
|
428
|
+
throw new Error(
|
|
429
|
+
"CONTROL BINDING FAILED: the control run targeted the same host as the target runs — " +
|
|
430
|
+
"the claimed impact is not target-dependent.",
|
|
431
|
+
);
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
if (bundle.controlTarget && bundle.controlTarget === current.target) {
|
|
435
|
+
throw new Error(
|
|
436
|
+
"CONTROL BINDING FAILED: control_target must differ from the case target; a control run " +
|
|
437
|
+
"against the vulnerable target proves nothing.",
|
|
438
|
+
);
|
|
439
|
+
}
|
|
440
|
+
// Same-file contract re-checked at store time (the tool already checked).
|
|
441
|
+
// OOB-only bundles carry no separate control script — the PoC hash alone
|
|
442
|
+
// is re-verified against the file on disk.
|
|
443
|
+
let pocHash: string | undefined;
|
|
444
|
+
let controlHash: string | undefined;
|
|
445
|
+
try {
|
|
446
|
+
pocHash = createHash("sha256").update(readFileSync(bundle.pocPath)).digest("hex");
|
|
447
|
+
controlHash = bundle.controlPath
|
|
448
|
+
? createHash("sha256").update(readFileSync(bundle.controlPath)).digest("hex")
|
|
449
|
+
: pocHash;
|
|
450
|
+
} catch {
|
|
451
|
+
pocHash = undefined;
|
|
452
|
+
controlHash = undefined;
|
|
453
|
+
}
|
|
454
|
+
if (!pocHash || !controlHash || pocHash !== controlHash) {
|
|
455
|
+
throw new Error(
|
|
456
|
+
"CONTROL CHECK FAILED: control_path must be the SAME script as poc_path " +
|
|
457
|
+
"(sha256 mismatch). A separately written control file proves nothing.",
|
|
458
|
+
);
|
|
459
|
+
}
|
|
460
|
+
if (bundle.pocSha256 && bundle.pocSha256 !== pocHash) {
|
|
461
|
+
throw new Error("pocSha256 does not match the PoC file on disk");
|
|
462
|
+
}
|
|
463
|
+
// Validate every run that exists — OOB-only bundles have no control run;
|
|
464
|
+
// OOB+control bundles validate all three.
|
|
465
|
+
const runsToValidate = bundle.controlRun
|
|
466
|
+
? [...bundle.targetRuns, bundle.controlRun]
|
|
467
|
+
: [...bundle.targetRuns];
|
|
468
|
+
for (const run of runsToValidate) {
|
|
469
|
+
validateRunEvidence(run, `${run.mode} run`);
|
|
470
|
+
}
|
|
471
|
+
assertEvidenceDifferential(bundle);
|
|
472
|
+
// Target binding applies to EVERY mode — an OOB bundle's verify.url must
|
|
473
|
+
// still belong to the case target, or the PoC could anchor its evidence on
|
|
474
|
+
// an unrelated host while the callback alone carries the proof.
|
|
475
|
+
for (const run of bundle.targetRuns) {
|
|
476
|
+
const bindingError = verifyUrlBindingError(run.evidence.verify.url, targetRunTarget);
|
|
477
|
+
if (bindingError) throw new Error(`TARGET BINDING FAILED: ${bindingError}`);
|
|
478
|
+
}
|
|
479
|
+
if (bundle.controlRun) {
|
|
480
|
+
const controlBindingError = verifyUrlBindingError(
|
|
481
|
+
bundle.controlRun.evidence.verify.url,
|
|
482
|
+
bundle.controlTarget!,
|
|
483
|
+
);
|
|
484
|
+
if (controlBindingError) {
|
|
485
|
+
throw new Error(`CONTROL BINDING FAILED: ${controlBindingError}`);
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
// A clean exit and model-authored evidence are necessary inputs, never the
|
|
489
|
+
// proof. Promotion requires a harness-observed target/control differential
|
|
490
|
+
// or a harness-owned OOB interaction differential.
|
|
491
|
+
assertMachineConfirmation(bundle);
|
|
492
|
+
|
|
493
|
+
const next = buildRecord({ pendingConfirmation: bundle }, current);
|
|
494
|
+
validateCase(next);
|
|
495
|
+
upsertCase(db, next);
|
|
496
|
+
return next;
|
|
497
|
+
});
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
/**
|
|
501
|
+
* Phase 2: commit (or refuse) the promotion on the main agent's verdict.
|
|
502
|
+
*
|
|
503
|
+
* CONFIRMED requires the full bundle to still hold (completion, nonce,
|
|
504
|
+
* determinism, differential), the PoC script to be unchanged since the runs
|
|
505
|
+
* (pocSha256 — otherwise the main agent reviewed different bytes), and a
|
|
506
|
+
* verdict accompanied by a fresh harness-owned target-only replay, a concrete
|
|
507
|
+
* review note, and a disconfirmation attempt. NOT_CONFIRMED (positively
|
|
508
|
+
* disproved) and INCONCLUSIVE (neither reproduced nor disproved) both record
|
|
509
|
+
* the verdict and keep the case investigating — INCONCLUSIVE preserves it for
|
|
510
|
+
* manual review rather than dropping it.
|
|
511
|
+
*/
|
|
512
|
+
export function applyConfirmationResult(
|
|
513
|
+
id: string,
|
|
514
|
+
verdictInput: MainAgentVerdict,
|
|
515
|
+
phase2Verification?: MainAgentVerification,
|
|
516
|
+
authority: { startedAsSubagent: boolean } = {
|
|
517
|
+
startedAsSubagent: PROCESS_STARTED_AS_SUBAGENT || process.env.PI_SUBAGENT_CHILD === "1",
|
|
518
|
+
},
|
|
519
|
+
): CaseUpdateResult {
|
|
520
|
+
if (authority.startedAsSubagent) {
|
|
521
|
+
throw new Error(
|
|
522
|
+
"ConfirmFinding is reserved for the main/coordinator agent; worker processes cannot commit confirmation",
|
|
523
|
+
);
|
|
524
|
+
}
|
|
525
|
+
const db = getDb();
|
|
526
|
+
return withImmediateTransaction(db, () => {
|
|
527
|
+
const current = getCaseById(id);
|
|
528
|
+
if (!current) throw new Error(`Case not found: ${id}`);
|
|
529
|
+
if (current.status !== "investigating") {
|
|
530
|
+
throw new Error(`ConfirmFinding requires an investigating case (current: ${current.status})`);
|
|
531
|
+
}
|
|
532
|
+
const bundle = current.pendingConfirmation;
|
|
533
|
+
if (!bundle) {
|
|
534
|
+
throw new Error("No pending confirmation on this case — run PromoteFinding first");
|
|
535
|
+
}
|
|
536
|
+
// Fail closed on an unparseable ranAt: Date.parse(garbage) is NaN, and
|
|
537
|
+
// NaN > TTL is false — a malformed timestamp must NOT make the bundle
|
|
538
|
+
// immortal. Treat it as expired (re-run PromoteFinding for a fresh one).
|
|
539
|
+
const ranAtMs = Date.parse(bundle.ranAt);
|
|
540
|
+
if (!Number.isFinite(ranAtMs) || Date.now() - ranAtMs > PENDING_CONFIRM_TTL_MS) {
|
|
541
|
+
throw new Error(
|
|
542
|
+
"Pending confirmation expired or has an invalid timestamp (1h TTL) — re-run PromoteFinding for a fresh bundle",
|
|
543
|
+
);
|
|
544
|
+
}
|
|
545
|
+
const parsed = validateMainAgentVerdict(verdictInput);
|
|
546
|
+
if (!parsed.ok) throw new Error(`Invalid main-agent confirmation verdict: ${parsed.error}`);
|
|
547
|
+
const verdict = parsed.verdict;
|
|
548
|
+
const canaryRequested = bundle.targetRuns[0].evidence.verify.canary !== undefined;
|
|
549
|
+
if (verdict.verdict === "CONFIRMED") {
|
|
550
|
+
if (canaryRequested && verdict.canary_assessment !== "verified") {
|
|
551
|
+
throw new Error(
|
|
552
|
+
"CONFIRMED canary mismatch: evidence requested a harness canary, so canary_assessment must be verified",
|
|
553
|
+
);
|
|
554
|
+
}
|
|
555
|
+
if (!canaryRequested && verdict.canary_assessment !== "not_applicable") {
|
|
556
|
+
throw new Error(
|
|
557
|
+
"CONFIRMED canary mismatch: this evidence has no canary template; record canary_assessment=not_applicable and explain why",
|
|
558
|
+
);
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
const recorded: MainAgentVerdictRecord = {
|
|
562
|
+
...verdict,
|
|
563
|
+
at: new Date().toISOString(),
|
|
564
|
+
reviewer: "main_agent",
|
|
565
|
+
phase2Verification: verdict.verdict === "CONFIRMED" ? phase2Verification : undefined,
|
|
566
|
+
proofStrength:
|
|
567
|
+
verdict.verdict === "CONFIRMED"
|
|
568
|
+
? canaryRequested
|
|
569
|
+
? "canary_differential"
|
|
570
|
+
: "predicate_differential"
|
|
571
|
+
: undefined,
|
|
572
|
+
};
|
|
573
|
+
|
|
574
|
+
if (verdict.verdict !== "CONFIRMED") {
|
|
575
|
+
// NOT_CONFIRMED (positively disproved) and INCONCLUSIVE (neither reproduced
|
|
576
|
+
// nor disproved) both record the verdict, consume the attempt, and keep the
|
|
577
|
+
// case investigating — neither auto-kills. INCONCLUSIVE is the fail-safe:
|
|
578
|
+
// the finding is preserved for manual review, not dropped.
|
|
579
|
+
const model = verdict.model ? ` (${verdict.model})` : "";
|
|
580
|
+
const note =
|
|
581
|
+
verdict.verdict === "INCONCLUSIVE"
|
|
582
|
+
? `main agent INCONCLUSIVE${model}: ${verdict.reasoning} — preserved for manual review, not disproved`
|
|
583
|
+
: `main agent NOT_CONFIRMED${model}: ${verdict.reasoning}`;
|
|
584
|
+
const next = buildRecord(
|
|
585
|
+
{
|
|
586
|
+
confirmerVerdict: recorded,
|
|
587
|
+
pendingConfirmation: undefined,
|
|
588
|
+
assumptions: [...(current.assumptions ?? []), note],
|
|
589
|
+
},
|
|
590
|
+
current,
|
|
591
|
+
);
|
|
592
|
+
// buildRecord's nullish fallback preserves the old value; consume the
|
|
593
|
+
// rejected attempt explicitly so a retry must produce fresh evidence.
|
|
594
|
+
next.pendingConfirmation = undefined;
|
|
595
|
+
validateCase(next);
|
|
596
|
+
upsertCase(db, next);
|
|
597
|
+
return { record: next, changed: true };
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
// CONFIRMED — re-validate the whole bundle (defense in depth; the case may
|
|
601
|
+
// have been touched between phase 1 and the verdict).
|
|
602
|
+
const isIntra = bundle.mode === "intra_target";
|
|
603
|
+
const allRuns = isIntra
|
|
604
|
+
? [...bundle.targetRuns]
|
|
605
|
+
: [...bundle.targetRuns, ...(bundle.controlRun ? [bundle.controlRun] : [])];
|
|
606
|
+
for (const run of allRuns) {
|
|
607
|
+
validateRunEvidence(run, `${run.mode} run`);
|
|
608
|
+
}
|
|
609
|
+
assertEvidenceDifferential(bundle, isIntra);
|
|
610
|
+
assertMachineConfirmation(bundle);
|
|
611
|
+
assertHarnessCanary(bundle.harnessVerified, canaryRequested, "PHASE-1 CANARY FAILED");
|
|
612
|
+
let pocHash: string | undefined;
|
|
613
|
+
try {
|
|
614
|
+
pocHash = createHash("sha256").update(readFileSync(bundle.pocPath)).digest("hex");
|
|
615
|
+
} catch {
|
|
616
|
+
pocHash = undefined;
|
|
617
|
+
}
|
|
618
|
+
if (!pocHash || pocHash !== bundle.pocSha256) {
|
|
619
|
+
throw new Error(
|
|
620
|
+
"PoC script changed since the runs — re-run PromoteFinding (the main agent must review the exact bytes that ran)",
|
|
621
|
+
);
|
|
622
|
+
}
|
|
623
|
+
// The case target must still be the host the PoC ran against, and still
|
|
624
|
+
// differ from the control target. The evidence proves nothing about a
|
|
625
|
+
// target the case adopted after the runs.
|
|
626
|
+
const targetRun = bundle.targetRuns[0];
|
|
627
|
+
if (!current.target || current.target !== targetRun.target) {
|
|
628
|
+
throw new Error(
|
|
629
|
+
"Case target changed since the PoC runs — re-run PromoteFinding against the current target " +
|
|
630
|
+
`(bundle target: ${targetRun.target}, case target: ${current.target ?? "(none)"}).`,
|
|
631
|
+
);
|
|
632
|
+
}
|
|
633
|
+
if (!isIntra && current.target === bundle.controlTarget) {
|
|
634
|
+
throw new Error(
|
|
635
|
+
"Case target now equals the control target — the claimed impact is not target-dependent; " +
|
|
636
|
+
"re-run PromoteFinding with a distinct control_target.",
|
|
637
|
+
);
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
// The observation must predate the repro (provenance guard).
|
|
641
|
+
const observation = current.evidenceItems?.find(
|
|
642
|
+
(e: EvidenceItem) => e.role === "observation" && e.sha256,
|
|
643
|
+
);
|
|
644
|
+
if (observation && observation.createdAt > bundle.targetRuns[0].ranAt) {
|
|
645
|
+
throw new Error(
|
|
646
|
+
"Evidence chain invalid: the observation item was recorded after the PoC ran " +
|
|
647
|
+
`(${observation.createdAt} > ${bundle.targetRuns[0].ranAt}). The observation must predate the repro.`,
|
|
648
|
+
);
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
// Phase 1 proves the evidence floor. Phase 2 must freshly replay that same
|
|
652
|
+
// request inside the main agent's ConfirmFinding call; a caller-provided
|
|
653
|
+
// boolean is not accepted as proof of re-execution.
|
|
654
|
+
assertMainAgentVerification(bundle, phase2Verification, isIntra);
|
|
655
|
+
|
|
656
|
+
const reproductionItem: EvidenceItem = {
|
|
657
|
+
id: `ev_${stableShortId(`${id}\nreproduction\n${targetRun.ranAt}`)}`,
|
|
658
|
+
caseId: id,
|
|
659
|
+
role: "reproduction",
|
|
660
|
+
// The runner preserves each run's evidence.json in a durable dir
|
|
661
|
+
// (.pi/poc-evidence/) — the artifact the hash was computed over still
|
|
662
|
+
// exists, so the item stays artifact-backed and re-verifiable.
|
|
663
|
+
artifactPath: targetRun.evidencePath ? basename(targetRun.evidencePath) : "evidence.json",
|
|
664
|
+
sha256: targetRun.evidenceSha256,
|
|
665
|
+
summary: `PoC evidence accepted (2 target runs + ${isIntra ? "same-host baseline" : "control"}; ${recorded.proofStrength}) — main agent semantic confirmation${verdict.model ? ` (${verdict.model})` : ""}`,
|
|
666
|
+
createdAt: targetRun.ranAt,
|
|
667
|
+
};
|
|
668
|
+
|
|
669
|
+
const newEvidence =
|
|
670
|
+
(current.evidence ? `${current.evidence}\n\n` : "") +
|
|
671
|
+
`### PoC Execution Capture (${targetRun.ranAt})\n` +
|
|
672
|
+
`- **Evidence sha256:** ${targetRun.evidenceSha256}\n` +
|
|
673
|
+
`- **Target:** ${targetRun.target}\n` +
|
|
674
|
+
`- **Machine evidence:** ${recorded.proofStrength} (a differential is not by itself proof of exploitation)\n` +
|
|
675
|
+
`- **Main-agent reviewer:** ${verdict.model ?? "unknown model"} — semantic confirmation\n` +
|
|
676
|
+
`#### Target Run Output\n\`\`\`\n${targetRun.output ?? ""}\n\`\`\``;
|
|
677
|
+
|
|
678
|
+
const update: NormalizedCaseInput = {
|
|
679
|
+
status: "confirmed",
|
|
680
|
+
pocVerified: {
|
|
681
|
+
path: bundle.pocPath,
|
|
682
|
+
exitCode: targetRun.exitCode,
|
|
683
|
+
ranAt: targetRun.ranAt,
|
|
684
|
+
output: targetRun.output,
|
|
685
|
+
sandbox: targetRun.sandbox,
|
|
686
|
+
completed: true,
|
|
687
|
+
outputComplete: true,
|
|
688
|
+
mode: "poc",
|
|
689
|
+
target: targetRun.target,
|
|
690
|
+
},
|
|
691
|
+
controlVerified:
|
|
692
|
+
isIntra || !bundle.controlRun
|
|
693
|
+
? {
|
|
694
|
+
path: bundle.pocPath,
|
|
695
|
+
exitCode: targetRun.exitCode,
|
|
696
|
+
ranAt: targetRun.ranAt,
|
|
697
|
+
output: `intra-target baseline (same host): ${bundle.harnessVerified?.control?.note ?? "baseline did not satisfy the attack predicate"}`,
|
|
698
|
+
sandbox: targetRun.sandbox,
|
|
699
|
+
completed: true,
|
|
700
|
+
outputComplete: true,
|
|
701
|
+
mode: "baseline",
|
|
702
|
+
target: targetRun.target,
|
|
703
|
+
}
|
|
704
|
+
: {
|
|
705
|
+
path: bundle.controlPath ?? bundle.pocPath,
|
|
706
|
+
exitCode: bundle.controlRun.exitCode,
|
|
707
|
+
ranAt: bundle.controlRun.ranAt,
|
|
708
|
+
output: bundle.controlRun.output,
|
|
709
|
+
sandbox: bundle.controlRun.sandbox,
|
|
710
|
+
completed: true,
|
|
711
|
+
outputComplete: true,
|
|
712
|
+
mode: "control",
|
|
713
|
+
target: bundle.controlRun.target,
|
|
714
|
+
},
|
|
715
|
+
disconfirmation: verdict.disconfirmation_attempt,
|
|
716
|
+
confirmerVerdict: recorded,
|
|
717
|
+
pendingConfirmation: undefined,
|
|
718
|
+
evidence: newEvidence,
|
|
719
|
+
};
|
|
720
|
+
|
|
721
|
+
const next = buildRecord(update, current);
|
|
722
|
+
next.pendingConfirmation = undefined; // buildRecord's ?? existing keeps it; clear explicitly
|
|
723
|
+
validateCase(next);
|
|
724
|
+
insertEvidenceItem(db, reproductionItem);
|
|
725
|
+
upsertCase(db, next);
|
|
726
|
+
next.evidenceItems = [...(next.evidenceItems ?? []), reproductionItem];
|
|
727
|
+
return { record: next, changed: true };
|
|
728
|
+
});
|
|
729
|
+
}
|