fix: repair ambiguous top-level trace snapshots

This commit is contained in:
Codex Agent
2026-06-06 12:15:06 +08:00
parent a210728045
commit f4b33b5cbb
2 changed files with 26 additions and 7 deletions
+21 -1
View File
@@ -616,7 +616,12 @@ export async function loadPersistedAgentRunResult(traceId, options = {}) {
const topLevelFinalTraceId = safeTraceId(session?.session?.finalResponse?.traceId ?? session?.session?.traceSummary?.traceId);
const topLevelConflictsTrace = (agentRunTraceId && agentRunTraceId !== safeId) || (topLevelFinalTraceId && topLevelFinalTraceId !== safeId);
const mayUseTopLevelAgentRun = !topLevelConflictsTrace && (latestTraceId === safeId || agentRunTraceId === safeId || topLevelFinalTraceId === safeId || (!latestTraceId && !agentRunTraceId && !topLevelFinalTraceId));
const agentRun = traceEvidence?.agentRun ?? (mayUseTopLevelAgentRun ? topLevelAgentRun : null);
const topLevelTraceEvidence = mayUseTopLevelAgentRun ? agentSessionTopLevelTraceEvidence(session, safeId) : null;
if (!traceEvidence && topLevelTraceEvidence && ambiguousTopLevelAgentRunTrace(topLevelAgentRun)) {
const repaired = await repairPersistedAgentRunResultFromRunCommands({ traceId: safeId, session, options }).catch(() => null);
if (repaired) return repaired;
}
const agentRun = traceEvidence?.agentRun ?? (topLevelTraceEvidence ? topLevelAgentRun : null);
const restored = persistedAgentRunResultFromSession({ traceId: safeId, session, agentRun, traceEvidence, options });
if (restored) return restored;
return await repairPersistedAgentRunResultFromRunCommands({ traceId: safeId, session, options });
@@ -774,6 +779,21 @@ function validAgentSessionTraceEvidence(session, traceId) {
return agentRunEvidenceMatchesTrace(evidence.agentRun, traceId, evidence, { requireAgentRunTraceId: true }) ? evidence : null;
}
function agentSessionTopLevelTraceEvidence(session, traceId) {
const snapshot = session?.session && typeof session.session === "object" ? session.session : null;
if (!snapshot) return null;
const evidence = {
finalResponse: snapshot.finalResponse,
traceSummary: snapshot.traceSummary
};
return agentRunEvidenceMatchesTrace(snapshot.agentRun, traceId, evidence) ? evidence : null;
}
function ambiguousTopLevelAgentRunTrace(agentRun) {
const run = agentRun && typeof agentRun === "object" ? agentRun : null;
return Boolean(run?.runId && run?.commandId && !safeTraceId(run.traceId));
}
function agentRunEvidenceMatchesTrace(agentRun, traceId, traceEvidence = null, options = {}) {
const id = safeTraceId(traceId);
if (!id) return false;