fix(evaluation): authenticate delivered Evidence citations
Authenticate completed successful REA Evidence records for citation accounting and shared fixture grading. Preserve genuine structured and complete text-only delivery, including authenticated inventory substeps. Closes #1728.
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import {
|
||||
deliveredCodexEvidence,
|
||||
evaluateKnownAnswers,
|
||||
type FixtureClaimExpectation,
|
||||
type KnownAnswerAssessment,
|
||||
@@ -67,12 +68,6 @@ const canonicalValue = (value: unknown): unknown => {
|
||||
);
|
||||
};
|
||||
|
||||
const evidenceIdsFrom = (value: unknown): readonly string[] => {
|
||||
const encoded = JSON.stringify(value);
|
||||
if (encoded === undefined) return [];
|
||||
return [...new Set(encoded.match(/ev_[a-f0-9]{64}/gu) ?? [])];
|
||||
};
|
||||
|
||||
const toolResultError = (
|
||||
item: Record<string, unknown>,
|
||||
): Record<string, unknown> | undefined => {
|
||||
@@ -118,8 +113,16 @@ const toolResultFailed = (
|
||||
);
|
||||
};
|
||||
|
||||
const callFromItem = (value: unknown): CodexMcpCall | undefined => {
|
||||
const item = record(value);
|
||||
const callFromEvent = (
|
||||
event: Record<string, unknown>,
|
||||
): CodexMcpCall | undefined => {
|
||||
const item = record(
|
||||
event.type === "item.completed"
|
||||
? event.item
|
||||
: event.type === "mcp_tool_call"
|
||||
? event
|
||||
: undefined,
|
||||
);
|
||||
if (item?.type !== "mcp_tool_call") return undefined;
|
||||
const tool = textValue(item.tool) ?? textValue(item.name);
|
||||
if (tool === undefined) return undefined;
|
||||
@@ -129,7 +132,9 @@ const callFromItem = (value: unknown): CodexMcpCall | undefined => {
|
||||
server: textValue(item.server) ?? textValue(item.server_name) ?? null,
|
||||
tool,
|
||||
arguments: item.arguments ?? item.input ?? {},
|
||||
evidenceIds: evidenceIdsFrom(item.result ?? item.output),
|
||||
evidenceIds: deliveredCodexEvidence(event).map(
|
||||
({ evidence_id }) => evidence_id,
|
||||
),
|
||||
error: toolResultFailed(item, error),
|
||||
errorCode: textValue(error?.code) ?? null,
|
||||
};
|
||||
@@ -163,9 +168,7 @@ const consumeEvent = (state: EvaluationState, value: unknown): void => {
|
||||
if (event === undefined) return;
|
||||
const completedItem =
|
||||
event.type === "item.completed" ? event.item : undefined;
|
||||
const call =
|
||||
callFromItem(completedItem) ??
|
||||
(event.type === "mcp_tool_call" ? callFromItem(event) : undefined);
|
||||
const call = callFromEvent(event);
|
||||
if (call !== undefined) {
|
||||
const identity =
|
||||
call.id ??
|
||||
|
||||
@@ -124,7 +124,7 @@ const sameJson = (left: unknown, right: unknown): boolean => {
|
||||
);
|
||||
};
|
||||
|
||||
const parseClosedAnswer = (text: string): unknown => {
|
||||
const parseClosedJson = (text: string): unknown => {
|
||||
const objects: Set<string>[] = [];
|
||||
let invalid = false;
|
||||
try {
|
||||
@@ -156,7 +156,7 @@ const parseClosedAnswer = (text: string): unknown => {
|
||||
// The JSON visitor checked syntax and duplicate members before JSON.parse can fold them.
|
||||
return JSON.parse(text);
|
||||
} catch (error) {
|
||||
// Syntax and nesting failures are malformed final-answer input, not an evaluation crash.
|
||||
// Syntax and nesting failures are malformed transcript input, not an evaluation crash.
|
||||
if (error instanceof SyntaxError || error instanceof RangeError)
|
||||
return undefined;
|
||||
throw error;
|
||||
@@ -224,20 +224,29 @@ const evidenceFromCall = (
|
||||
(result.error !== undefined && result.error !== null)
|
||||
)
|
||||
return undefined;
|
||||
const structured = record(
|
||||
result.structuredContent ?? result.structured_content,
|
||||
);
|
||||
if (
|
||||
structured === undefined ||
|
||||
(structured.error !== undefined && structured.error !== null)
|
||||
)
|
||||
return undefined;
|
||||
if (
|
||||
result.structuredContent !== undefined &&
|
||||
result.structured_content !== undefined &&
|
||||
!sameJson(result.structuredContent, result.structured_content)
|
||||
)
|
||||
return undefined;
|
||||
let value = result.structuredContent ?? result.structured_content;
|
||||
if (value === undefined || value === null) {
|
||||
// Some Codex transcripts retain only the complete MCP JSON text block.
|
||||
// Prose, multiple blocks, and truncated previews cannot establish delivery.
|
||||
if (!Array.isArray(result.content) || result.content.length !== 1)
|
||||
return undefined;
|
||||
const block = record(result.content[0]);
|
||||
if (block?.type !== "text" || typeof block.text !== "string")
|
||||
return undefined;
|
||||
value = parseClosedJson(block.text);
|
||||
}
|
||||
const structured = record(value);
|
||||
if (
|
||||
structured === undefined ||
|
||||
(structured.error !== undefined && structured.error !== null)
|
||||
)
|
||||
return undefined;
|
||||
let evidence: Evidence;
|
||||
try {
|
||||
evidence = parseEvidence(structured);
|
||||
@@ -249,20 +258,53 @@ const evidenceFromCall = (
|
||||
return evidence;
|
||||
};
|
||||
|
||||
/** Authenticate completed REA deliveries for both citations and fixture grading. */
|
||||
export const deliveredCodexEvidence = (event: unknown): readonly Evidence[] => {
|
||||
const item = completedCall(event);
|
||||
if (item === undefined) return [];
|
||||
const evidence = evidenceFromCall(item);
|
||||
if (evidence === undefined) return [];
|
||||
const records = [evidence];
|
||||
// Artifact inspection delivers its inventory Evidence inline. Bare links and
|
||||
// arbitrary IDs in normalized facts are references, not delivered records.
|
||||
if (evidence.operation !== "inspect_artifact") return records;
|
||||
const substeps = record(evidence.normalized_result)?.substeps;
|
||||
if (!Array.isArray(substeps)) return records;
|
||||
for (const value of substeps) {
|
||||
const substep = record(value);
|
||||
if (
|
||||
substep?.status !== "completed" ||
|
||||
substep.operation !== "inventory_artifact" ||
|
||||
typeof substep.evidence_id !== "string" ||
|
||||
!evidence.evidence_links.includes(substep.evidence_id)
|
||||
)
|
||||
continue;
|
||||
try {
|
||||
const nested = parseEvidence(substep.evidence);
|
||||
if (
|
||||
nested.evidence_id === substep.evidence_id &&
|
||||
nested.operation === substep.operation
|
||||
)
|
||||
records.push(nested);
|
||||
} catch {
|
||||
// An authenticated parent does not authenticate an embedded record's ID.
|
||||
}
|
||||
}
|
||||
return records;
|
||||
};
|
||||
|
||||
const collectDeliveredEvidence = (
|
||||
events: readonly unknown[],
|
||||
): DeliveredEvidence => {
|
||||
const records = new Map<string, Evidence>();
|
||||
const conflicts = new Set<string>();
|
||||
for (const event of events) {
|
||||
const item = completedCall(event);
|
||||
if (item === undefined) continue;
|
||||
const evidence = evidenceFromCall(item);
|
||||
if (evidence === undefined) continue;
|
||||
const previous = records.get(evidence.evidence_id);
|
||||
if (previous !== undefined && !sameJson(previous, evidence))
|
||||
conflicts.add(evidence.evidence_id);
|
||||
else records.set(evidence.evidence_id, evidence);
|
||||
for (const evidence of deliveredCodexEvidence(event)) {
|
||||
const previous = records.get(evidence.evidence_id);
|
||||
if (previous !== undefined && !sameJson(previous, evidence))
|
||||
conflicts.add(evidence.evidence_id);
|
||||
else records.set(evidence.evidence_id, evidence);
|
||||
}
|
||||
}
|
||||
return { records, conflicts };
|
||||
};
|
||||
@@ -382,7 +424,7 @@ export const evaluateKnownAnswers = (
|
||||
manifestFailures: ["duplicate_fixture_claim_ids"],
|
||||
claims: [],
|
||||
};
|
||||
const manifest = finalAnswerSchema.safeParse(parseClosedAnswer(finalMessage));
|
||||
const manifest = finalAnswerSchema.safeParse(parseClosedJson(finalMessage));
|
||||
if (!manifest.success)
|
||||
return {
|
||||
scope,
|
||||
|
||||
Reference in New Issue
Block a user