Merge pull request #975 from pikasTech/fix/issue955-repair-ambiguous-top-level

fix: repair ambiguous top-level trace snapshots
This commit is contained in:
Lyon
2026-06-06 12:15:50 +08:00
committed by GitHub
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;
+5 -6
View File
@@ -1817,11 +1817,11 @@ test("cloud api repairs historical same-session AgentRun trace after lastTraceId
messages: [
{ id: "msg_issue955_user_first", role: "user", text: "看看有几个hwpod可用", traceId: firstTraceId, status: "submitted" },
{ id: "msg_issue955_user_second", role: "user", text: "试一下编译 d601-f103-v2", traceId: secondTraceId, status: "submitted" },
{ id: "msg_issue955_second", role: "agent", text: secondFinalText, traceId: secondTraceId, status: "idle" }
{ id: "msg_issue955_second", role: "agent", text: firstFinalText, traceId: secondTraceId, status: "idle" }
],
agentRun: { adapter: "agentrun-v01", managerUrl: `http://127.0.0.1:${agentRunPort}`, runId, commandId: secondCommandId, traceId: secondTraceId, backendProfile: "deepseek", terminalStatus: "completed", lastSeq: 67, valuesPrinted: false },
finalResponse: { text: secondFinalText, textChars: secondFinalText.length, role: "assistant", status: "completed", traceId: secondTraceId, valuesPrinted: false },
traceSummary: { traceId: secondTraceId, source: "agent-session-snapshot", sourceEventCount: 31, terminalStatus: "completed", agentRun: { runId, commandId: secondCommandId, lastSeq: 67, valuesPrinted: false }, valuesPrinted: false },
agentRun: { adapter: "agentrun-v01", managerUrl: `http://127.0.0.1:${agentRunPort}`, runId, commandId: firstCommandId, backendProfile: "deepseek", terminalStatus: "completed", lastSeq: 35, valuesPrinted: false },
finalResponse: { text: firstFinalText, textChars: firstFinalText.length, role: "assistant", status: "completed", traceId: secondTraceId, valuesPrinted: false },
traceSummary: { traceId: secondTraceId, source: "agent-session-snapshot", sourceEventCount: 36, terminalStatus: "completed", agentRun: { runId, commandId: firstCommandId, lastSeq: 35, valuesPrinted: false }, valuesPrinted: false },
traceResults: {
[secondTraceId]: {
traceId: secondTraceId,
@@ -1895,8 +1895,7 @@ test("cloud api repairs historical same-session AgentRun trace after lastTraceId
assert.equal(ownerSessions.get("ses_issue955_historical").session.traceResults[firstTraceId].finalResponse.text, firstFinalText);
const repairedSession = ownerSessions.get("ses_issue955_historical");
assert.equal(repairedSession.lastTraceId, secondTraceId);
assert.equal(repairedSession.session.agentRun.commandId, secondCommandId);
assert.equal(repairedSession.session.finalResponse.text, secondFinalText);
assert.equal(repairedSession.session.traceResults[firstTraceId].agentRun.commandId, firstCommandId);
const secondResultResponse = await fetch(`http://127.0.0.1:${port}/v1/agent/chat/result/${secondTraceId}`, {
headers: { cookie: "hwlab_session=test-stub-session" }