fix: recognize top-level AgentRun terminal events

This commit is contained in:
lyon
2026-06-22 13:01:28 +08:00
parent a1277c91b1
commit 51f21d4bc2
+10 -8
View File
@@ -753,12 +753,13 @@ function agentRunTerminalStatusFromEvents(events = []) {
if (!Array.isArray(events)) return null;
for (const event of [...events].reverse()) {
const payload = event?.payload && typeof event.payload === "object" ? event.payload : {};
const phase = payload.phase ?? event?.phase;
const candidates = [
event?.type === "terminal_status" ? payload.terminalStatus : null,
payload.phase === "command-terminal" ? payload.terminalStatus : null,
payload.phase === "turn:completed" || payload.phase === "turn/steer:completed" ? "completed" : null,
event?.terminal === true || event?.final === true || event?.replyAuthority === true ? payload.terminalStatus ?? payload.status ?? event?.status ?? "completed" : null,
payload.terminal === true ? payload.terminalStatus ?? payload.status ?? "completed" : null
event?.type === "terminal_status" ? payload.terminalStatus ?? event?.terminalStatus ?? payload.status ?? event?.status : null,
phase === "command-terminal" ? payload.terminalStatus ?? event?.terminalStatus ?? payload.status ?? event?.status : null,
phase === "turn:completed" || phase === "turn/steer:completed" ? "completed" : null,
event?.terminal === true || event?.final === true || event?.replyAuthority === true ? payload.terminalStatus ?? event?.terminalStatus ?? payload.status ?? event?.status ?? "completed" : null,
payload.terminal === true || payload.final === true || payload.replyAuthority === true ? payload.terminalStatus ?? event?.terminalStatus ?? payload.status ?? event?.status ?? "completed" : null
];
for (const value of candidates) {
const status = normalizeAgentRunStatus(value);
@@ -2400,6 +2401,7 @@ function agentRunEventBelongsToTrace(event, { currentCommandId = "", afterSeq =
function agentRunEventCommandId(event) {
const payload = event?.payload && typeof event.payload === "object" ? event.payload : {};
if (typeof event?.commandId === "string") return event.commandId;
return typeof payload.commandId === "string" ? payload.commandId : "";
}
@@ -2506,11 +2508,11 @@ function mapAgentRunEvent(event, mapping = {}) {
return { ...base, type: "error", eventType: "error", status: "failed", label: `agentrun:error:${failureKind}`, errorCode: failureKind, failureKind, willRetry: payload.willRetry === true ? true : undefined, message: textPayload(payload, "AgentRun error") };
}
if (event.type === "terminal_status") {
const terminalStatus = String(payload.terminalStatus ?? "failed");
const failureKind = normalizedFailureKind(payload.failureKind ?? payload.errorCode) ?? null;
const terminalStatus = String(payload.terminalStatus ?? event.terminalStatus ?? payload.status ?? event.status ?? "failed");
const failureKind = normalizedFailureKind(payload.failureKind ?? payload.errorCode ?? event.failureKind ?? event.errorCode) ?? null;
const normalizedPayload = failureKind ? { ...payload, failureKind, errorCode: failureKind } : payload;
if (isRetryableProviderInterruptionPayload(normalizedPayload)) return agentRunProviderRetryEvent({ base, payload: normalizedPayload, status: "retrying", label: `agentrun:provider-retry:${terminalStatus}` });
return { ...base, type: "result", eventType: "terminal", status: terminalStatus === "cancelled" ? "canceled" : terminalStatus, label: `agentrun:terminal:${terminalStatus}`, errorCode: failureKind, failureKind, willRetry: payload.willRetry === true ? true : undefined, message: textPayload(payload, `AgentRun terminal status ${terminalStatus}`), terminal: true };
return { ...base, type: "result", eventType: "terminal", status: terminalStatus === "cancelled" ? "canceled" : terminalStatus, label: `agentrun:terminal:${terminalStatus}`, errorCode: failureKind, failureKind, willRetry: payload.willRetry === true || event.willRetry === true ? true : undefined, message: textPayload({ ...event, ...payload }, `AgentRun terminal status ${terminalStatus}`), terminal: true };
}
return { ...base, type: "backend", eventType: "backend", status: "running", label: `agentrun:event:${String(event.type ?? "unknown")}`, message: textPayload(payload, "AgentRun event") };
}