import type { AgentRunStore } from "./store.js"; import type { CommandRecord, JsonRecord, JsonValue, RunEvent, RunRecord, RunnerJobRecord, TerminalStatus } from "../common/types.js"; import { boundedTextSummary, outputBytesFromPayload, outputTruncatedFromPayload } from "../common/output.js"; const maxToolCallSummaryItems = 40; const toolCallCommandLimitChars = 600; const toolCallFieldLimitChars = 200; const RESULT_EVENT_PAGE_LIMIT = 500; const RESULT_EVENT_MAX_PAGES = 200; interface ResultEventPage { events: RunEvent[]; capped: boolean; nextAfterSeq: number | null; } interface AssistantReplySummary { text: string; seq: number | null; source: string | null; final: boolean; replyAuthority: boolean; textTruncated: boolean; outputTruncated: boolean; } export async function buildRunResult(store: AgentRunStore, runId: string, commandId?: string): Promise { const run = await store.getRun(runId); const command = await selectCommand(store, runId, commandId); const eventPage = await listResultEvents(store, runId); const events = eventPage.events; const scopedEvents = command ? eventsForCommand(events, command.id) : events; const jobs = await store.listRunnerJobs(runId, command?.id); const latestJob = jobs.at(-1) ?? null; const commandTerminal = command ? terminalFromCommand(command) : null; const terminalEventStatus = terminalFromEvents(scopedEvents); const terminal = commandTerminal ?? terminalEventStatus ?? run.terminalStatus; const terminalSource = commandTerminal ? "command-record" : terminalEventStatus ? "terminal_status-event" : run.terminalStatus ? "run-record" : "none"; const failureKind = command ? failureKindFromEvents(scopedEvents) : run.failureKind ?? failureKindFromEvents(scopedEvents); const failureMessage = command ? messageFromEvents(scopedEvents) : run.failureMessage ?? messageFromEvents(scopedEvents); const reply = assistantReply(scopedEvents); const blocker = terminal === "blocked" || terminal === "failed" ? { failureKind, message: failureMessage } : null; const liveness = livenessSnapshot(run, command, events, scopedEvents, terminal); const steerDelivery = command?.type === "steer" ? steerDeliverySummary(events, command.id) : null; return { runId: run.id, commandId: command?.id ?? commandId ?? null, attemptId: latestJob?.attemptId ?? attemptFromEvents(events), runnerId: latestJob?.runnerId ?? null, jobName: latestJob?.jobName ?? null, namespace: latestJob?.namespace ?? null, status: command?.state ?? run.status, runStatus: run.status, commandState: command?.state ?? null, terminalStatus: terminal, terminalSource, completed: terminal === "completed", reply: reply.text, finalResponse: { text: reply.text, seq: reply.seq, source: reply.source, final: reply.final, replyAuthority: reply.replyAuthority, textTruncated: reply.textTruncated, outputTruncated: reply.outputTruncated, }, finalAssistantSeq: reply.seq, finalAssistantSource: reply.source, finalAssistantTextTruncated: reply.textTruncated, finalAssistantOutputTruncated: reply.outputTruncated, failureKind, failureMessage, blocker, liveness, ...(steerDelivery ? { steerDelivery } : {}), lastSeq: events.at(-1)?.seq ?? 0, eventCount: events.length, eventsCapped: eventPage.capped, nextAfterSeq: eventPage.nextAfterSeq, scopedEventCount: scopedEvents.length, scopedLastSeq: scopedEvents.at(-1)?.seq ?? 0, artifactSummary: artifactSummary(scopedEvents), toolCallSummary: toolCallSummary(scopedEvents), sessionRef: sessionSummary(run), resourceBundleRef: resourceBundleSummary(run, events), runnerJobCount: jobs.length, }; } function livenessSnapshot(run: RunRecord, command: CommandRecord | null, events: RunEvent[], scopedEvents: RunEvent[], terminal: TerminalStatus | null): JsonRecord { const nowMs = Date.now(); const active = terminal === null && !runIsTerminal(run) && !commandIsTerminal(command); const lastEvent = events.at(-1) ?? null; const lastVisibleActivity = latestVisibleActivity(scopedEvents); const lastCommandActivity = lastVisibleActivity ?? latestLivenessActivity(scopedEvents); const lease = leaseSummary(run, nowMs); const transportDisconnect = latestTransportDisconnect(scopedEvents); const phase = livenessPhase({ active, command, lastVisibleActivity, leaseExpired: lease.leaseExpired, transportDisconnect }); return { phase, active, observedAt: new Date(nowMs).toISOString(), runStatus: run.status, commandId: command?.id ?? null, commandType: command?.type ?? null, commandState: command?.state ?? null, lastSeq: lastEvent?.seq ?? 0, lastEventAt: lastEvent?.createdAt ?? null, lastEventAgeMs: lastEvent ? ageMs(lastEvent.createdAt, nowMs) : null, lastCommandActivity: livenessActivitySummary(lastCommandActivity, nowMs), lease, transportDisconnect: transportDisconnect ? livenessActivitySummary(transportDisconnect, nowMs) : null, recoveryActions: active ? recoveryActions(run, command, lastEvent?.seq ?? 0) : [], valuesPrinted: false, }; } function livenessPhase(input: { active: boolean; command: CommandRecord | null; lastVisibleActivity: RunEvent | null; leaseExpired: boolean | null; transportDisconnect: RunEvent | null }): string { if (!input.active) return "terminal"; if (input.command?.state === "pending") return "waiting-runner"; if (input.leaseExpired === true) return "runner-heartbeat-stale"; if (input.transportDisconnect) return "transport-disconnected"; if (input.lastVisibleActivity?.type === "tool_call") { const status = input.lastVisibleActivity.payload.status; if (status === "inProgress" || status === "running") return "waiting-tool"; if (status === "completed") return "idle-after-tool"; } return "waiting-model"; } function leaseSummary(run: RunRecord, nowMs: number): JsonRecord & { leaseExpired: boolean | null } { const leaseExpiresMs = run.leaseExpiresAt ? Date.parse(run.leaseExpiresAt) : NaN; const hasLease = Boolean(run.claimedBy && run.leaseExpiresAt && Number.isFinite(leaseExpiresMs)); const leaseExpired = run.claimedBy ? (hasLease ? leaseExpiresMs <= nowMs : true) : null; return { claimedBy: run.claimedBy, leaseExpiresAt: run.leaseExpiresAt, leaseExpired, leaseRemainingMs: hasLease ? Math.max(0, leaseExpiresMs - nowMs) : null, valuesPrinted: false, }; } function latestLivenessActivity(events: RunEvent[]): RunEvent | null { return [...events].reverse().find(isLivenessActivityEvent) ?? null; } function latestVisibleActivity(events: RunEvent[]): RunEvent | null { return [...events].reverse().find((event) => event.type === "assistant_message" || event.type === "tool_call" || event.type === "command_output" || event.type === "diff" || event.type === "error" || event.type === "terminal_status") ?? null; } function isLivenessActivityEvent(event: RunEvent): boolean { if (event.type === "assistant_message" || event.type === "tool_call" || event.type === "command_output" || event.type === "diff" || event.type === "error" || event.type === "terminal_status") return true; if (event.type !== "backend_status") return false; const phase = typeof event.payload.phase === "string" ? event.payload.phase : ""; return ["backend-turn-started", "thread/start:completed", "thread/resume:completed", "turn/start:completed", "backend-turn-finished", "codex-app-server-closed"].includes(phase); } function latestTransportDisconnect(events: RunEvent[]): RunEvent | null { return [...events].reverse().find((event) => { const phase = typeof event.payload.phase === "string" ? event.payload.phase : ""; return (event.type === "error" && phase.includes("transport")) || (event.type === "backend_status" && phase === "codex-app-server-closed"); }) ?? null; } function livenessActivitySummary(event: RunEvent | null, nowMs: number): JsonRecord | null { if (!event) return null; return { seq: event.seq, type: event.type, phase: typeof event.payload.phase === "string" ? event.payload.phase : null, status: typeof event.payload.status === "string" ? event.payload.status : null, toolName: typeof event.payload.toolName === "string" ? event.payload.toolName : null, exitCode: normalizedExitCode(event.payload.exitCode), commandId: typeof event.payload.commandId === "string" ? event.payload.commandId : null, createdAt: event.createdAt, ageMs: ageMs(event.createdAt, nowMs), valuesPrinted: false, }; } function ageMs(value: string, nowMs: number): number | null { const parsed = Date.parse(value); return Number.isFinite(parsed) ? Math.max(0, nowMs - parsed) : null; } function recoveryActions(run: RunRecord, command: CommandRecord | null, afterSeq: number): JsonRecord[] { const actions: JsonRecord[] = [ { action: "poll-trace", runId: run.id, afterSeq }, { action: "poll-output", runId: run.id, afterSeq }, ]; if (command) actions.push({ action: "cancel-command", runId: run.id, commandId: command.id }); else actions.push({ action: "cancel-run", runId: run.id }); return actions; } function steerDeliverySummary(events: RunEvent[], commandId: string): JsonRecord { const related = events.filter((event) => event.payload.commandId === commandId); const completed = latestPhaseEvent(related, "turn/steer:completed"); const acknowledged = latestPhaseEvent(related, "steer-command-acknowledged"); const failed = [...related].reverse().find((event) => event.type === "error") ?? null; const deliverySeq = completed?.seq ?? failed?.seq ?? acknowledged?.seq ?? related.at(-1)?.seq ?? null; const targetCommandId = targetCommandIdFromEvents(related); const targetFollowUp = targetCommandId && deliverySeq !== null ? targetFollowUpAfterSeq(events, targetCommandId, deliverySeq) : null; return { commandId, targetCommandId, deliveryState: failed ? "failed" : completed ? "forwarded-to-backend" : acknowledged ? "acknowledged-by-runner" : "not-observed", backendAccepted: Boolean(completed), targetFollowUpObserved: targetFollowUp?.observed ?? null, targetFollowUpSeq: targetFollowUp?.seq ?? null, targetFollowUpType: targetFollowUp?.type ?? null, targetFollowUpPhase: targetFollowUp?.phase ?? null, semantics: "steer completion means the backend accepted the turn/steer RPC; target turn progress is reported by the target command liveness and is not implied by the steer command terminal state", valuesPrinted: false, }; } function latestPhaseEvent(events: RunEvent[], phase: string): RunEvent | null { return [...events].reverse().find((event) => event.payload.phase === phase) ?? null; } function targetCommandIdFromEvents(events: RunEvent[]): string | null { for (const event of [...events].reverse()) { const targetCommandId = event.payload.targetCommandId; if (typeof targetCommandId === "string" && targetCommandId.length > 0) return targetCommandId; } return null; } function targetFollowUpAfterSeq(events: RunEvent[], targetCommandId: string, afterSeq: number): JsonRecord { const event = events.find((item) => item.seq > afterSeq && item.payload.commandId === targetCommandId && isTargetFollowUpEvent(item)); return { observed: Boolean(event), seq: event?.seq ?? null, type: event?.type ?? null, phase: typeof event?.payload.phase === "string" ? event.payload.phase : null, }; } function isTargetFollowUpEvent(event: RunEvent): boolean { return event.type === "assistant_message" || event.type === "tool_call" || event.type === "command_output" || event.type === "diff" || event.type === "error" || event.type === "terminal_status"; } function runIsTerminal(run: RunRecord): boolean { return run.status === "completed" || run.status === "failed" || run.status === "blocked" || run.status === "cancelled"; } function commandIsTerminal(command: CommandRecord | null): boolean { return command ? terminalFromCommand(command) !== null : false; } async function listResultEvents(store: AgentRunStore, runId: string): Promise { const events: RunEvent[] = []; let afterSeq = 0; for (let pageIndex = 0; pageIndex < RESULT_EVENT_MAX_PAGES; pageIndex += 1) { const page = await store.listEvents(runId, afterSeq, RESULT_EVENT_PAGE_LIMIT); if (page.length === 0) return { events, capped: false, nextAfterSeq: null }; events.push(...page); const nextAfterSeq = page.at(-1)?.seq ?? afterSeq; if (nextAfterSeq <= afterSeq) return { events, capped: true, nextAfterSeq: afterSeq }; afterSeq = nextAfterSeq; if (page.length < RESULT_EVENT_PAGE_LIMIT) return { events, capped: false, nextAfterSeq: null }; } return { events, capped: true, nextAfterSeq: events.at(-1)?.seq ?? afterSeq }; } async function selectCommand(store: AgentRunStore, runId: string, commandId?: string): Promise { if (commandId) { const command = await store.getCommand(commandId); return command.runId === runId ? command : null; } const commands = await store.listCommands(runId, 0, 100); return commands.at(-1) ?? null; } function terminalFromEvents(events: RunEvent[]): TerminalStatus | null { for (const event of [...events].reverse()) { if (event.type !== "terminal_status") continue; const value = event.payload.terminalStatus; if (value === "completed" || value === "failed" || value === "blocked" || value === "cancelled") return value; } return null; } function terminalFromCommand(command: CommandRecord): TerminalStatus | null { if (command.state === "completed") return "completed"; if (command.state === "failed") return "failed"; if (command.state === "cancelled") return "cancelled"; return null; } function eventsForCommand(events: RunEvent[], commandId: string): RunEvent[] { const scoped = events.filter((event) => event.payload.commandId === commandId || typeof event.payload.commandId !== "string"); return scoped.length > 0 ? scoped : events; } function failureKindFromEvents(events: RunEvent[]): string | null { for (const event of [...events].reverse()) { const value = event.payload.failureKind; if (typeof value === "string") return value; } return null; } function messageFromEvents(events: RunEvent[]): string | null { for (const event of [...events].reverse()) { const value = event.payload.message; if (typeof value === "string" && value.length > 0) return value; } return null; } function assistantReply(events: RunEvent[]): AssistantReplySummary { const assistantEvents = events.filter((event) => event.type === "assistant_message"); const latestAuthoritative = [...assistantEvents].reverse().find((event) => (event.payload.replyAuthority === true || event.payload.final === true) && textPayload(event.payload).length > 0); if (latestAuthoritative) return assistantReplySummary(latestAuthoritative); const latestAssistant = [...assistantEvents].reverse().find((event) => textPayload(event.payload).length > 0); if (latestAssistant) return assistantReplySummary(latestAssistant); return { text: "", seq: null, source: null, final: false, replyAuthority: false, textTruncated: false, outputTruncated: false }; } function assistantReplySummary(event: RunEvent): AssistantReplySummary { return { text: textPayload(event.payload), seq: event.seq, source: typeof event.payload.source === "string" ? event.payload.source : null, final: event.payload.final === true, replyAuthority: event.payload.replyAuthority === true, textTruncated: event.payload.textTruncated === true, outputTruncated: event.payload.outputTruncated === true, }; } function textPayload(payload: JsonRecord): string { for (const key of ["text", "content", "delta"]) { const value = payload[key]; if (typeof value === "string") return value; } return ""; } function artifactSummary(events: RunEvent[]): JsonRecord { let commandOutputEvents = 0; let diffEvents = 0; let toolCallEvents = 0; let outputChars = 0; let outputBytes = 0; let outputTruncatedEvents = 0; const streamSummary: Record = { stdout: { events: 0, outputBytes: 0, outputTruncated: false }, stderr: { events: 0, outputBytes: 0, outputTruncated: false }, }; for (const event of events) { if (event.type === "command_output") { commandOutputEvents += 1; const text = textPayload(event.payload); outputChars += text.length; const bytes = outputBytesFromPayload(event.payload); outputBytes += bytes; const truncated = outputTruncatedFromPayload(event.payload); if (truncated) outputTruncatedEvents += 1; const stream = event.payload.stream === "stderr" ? "stderr" : "stdout"; streamSummary[stream].events += 1; streamSummary[stream].outputBytes += bytes; streamSummary[stream].outputTruncated ||= truncated; } if (event.type === "diff") diffEvents += 1; if (event.type === "tool_call") toolCallEvents += 1; } return { commandOutputEvents, diffEvents, toolCallEvents, outputChars, outputBytes, truncatedEvents: outputTruncatedEvents, outputTruncatedEvents, stdoutSummary: streamSummary.stdout, stderrSummary: streamSummary.stderr }; } function toolCallSummary(events: RunEvent[]): JsonRecord { const toolCallEvents = events.filter((event) => event.type === "tool_call"); const statusCounts: Record = {}; const exitCodeCounts: Record = {}; for (const event of toolCallEvents) { const status = typeof event.payload.status === "string" && event.payload.status.length > 0 ? event.payload.status : "unknown"; statusCounts[status] = (statusCounts[status] ?? 0) + 1; const exitCode = normalizedExitCode(event.payload.exitCode); if (exitCode !== null) exitCodeCounts[String(exitCode)] = (exitCodeCounts[String(exitCode)] ?? 0) + 1; } const window = toolCallEvents.slice(-maxToolCallSummaryItems); return { count: toolCallEvents.length, statusCounts, exitCodeCounts, items: window.map((event) => toolCallItemSummary(event)), itemsOmitted: Math.max(0, toolCallEvents.length - window.length), itemWindow: "latest", valuesPrinted: false, }; } function toolCallItemSummary(event: RunEvent): JsonRecord { const payload = event.payload; return { seq: event.seq, createdAt: event.createdAt, method: boundedOptionalString(payload.method, toolCallFieldLimitChars), toolName: boundedOptionalString(payload.toolName, toolCallFieldLimitChars), type: boundedOptionalString(payload.type, toolCallFieldLimitChars), itemId: boundedOptionalString(payload.itemId, toolCallFieldLimitChars), status: boundedOptionalString(payload.status, toolCallFieldLimitChars), exitCode: normalizedExitCode(payload.exitCode), processId: boundedOptionalString(payload.processId, toolCallFieldLimitChars), cwd: boundedOptionalString(payload.cwd, toolCallFieldLimitChars), command: boundedOptionalString(payload.command, toolCallCommandLimitChars), commandTruncated: optionalStringTruncated(payload.command, toolCallCommandLimitChars), valuesPrinted: false, }; } function boundedOptionalString(value: JsonValue | undefined, limitChars: number): string | null { if (typeof value !== "string") return null; return boundedTextSummary(value, { limitChars }).text as string; } function optionalStringTruncated(value: JsonValue | undefined, limitChars: number): boolean { if (typeof value !== "string") return false; return boundedTextSummary(value, { limitChars }).outputTruncated === true; } function normalizedExitCode(value: JsonValue | undefined): number | null { return typeof value === "number" && Number.isFinite(value) ? value : null; } function attemptFromEvents(events: RunEvent[]): string | null { for (const event of [...events].reverse()) { const value = event.payload.attemptId; if (typeof value === "string") return value; } return null; } function sessionSummary(run: RunRecord): JsonRecord | null { if (!run.sessionRef) return null; return { sessionId: run.sessionRef.sessionId, conversationId: run.sessionRef.conversationId ?? null, threadId: run.sessionRef.threadId ?? null, expiresAt: run.sessionRef.expiresAt ?? null, metadataKeys: Object.keys(run.sessionRef.metadata ?? {}).sort(), valuesPrinted: false, }; } function resourceBundleSummary(run: RunRecord, events: RunEvent[]): JsonRecord | null { if (!run.resourceBundleRef) return null; const materialized = events.find((event) => event.type === "backend_status" && event.payload.phase === "resource-bundle-materialized")?.payload ?? null; return { kind: run.resourceBundleRef.kind, repoUrl: run.resourceBundleRef.repoUrl, commitId: run.resourceBundleRef.commitId ?? null, ref: run.resourceBundleRef.ref ?? null, bundles: { count: run.resourceBundleRef.bundles.length, items: run.resourceBundleRef.bundles.map((item) => ({ name: item.name ?? null, repoUrl: item.repoUrl ?? run.resourceBundleRef?.repoUrl ?? null, commitId: item.commitId ?? run.resourceBundleRef?.commitId ?? null, ref: item.ref ?? run.resourceBundleRef?.ref ?? null, subpath: item.subpath, targetPath: item.targetPath, valuesPrinted: false })), valuesPrinted: false, }, promptRefs: run.resourceBundleRef.promptRefs ? { count: run.resourceBundleRef.promptRefs.length, names: run.resourceBundleRef.promptRefs.map((item) => item.name), required: run.resourceBundleRef.promptRefs.filter((item) => item.required === true).map((item) => item.name), valuesPrinted: false } : { count: 0, names: [], required: [], valuesPrinted: false }, materialized: materialized as JsonValue, }; }