diff --git a/packages/app/src/session/timeline/controller-projection.test.ts b/packages/app/src/session/timeline/controller-projection.test.ts index 9f032c1c4e8..ee0a775cbff 100644 --- a/packages/app/src/session/timeline/controller-projection.test.ts +++ b/packages/app/src/session/timeline/controller-projection.test.ts @@ -179,6 +179,21 @@ describe("visibleTimelineMessages", () => { ]) expect(visibleTimelineMessages(messages, [], "msg_0")).toEqual([]) }) + + test("keeps a pre-promotion failed idle marker after the pending input that triggered it", () => { + const failed = [ + { id: "msg_3", type: "user", text: "queued", time: { created: 3 } }, + { + id: "msg_idle", + type: "idle", + outcome: "failed", + error: { type: "unknown", message: 'Agent not found: "build"' }, + time: { created: 4 }, + }, + ] satisfies SessionMessageInfo[] + + expect(visibleTimelineMessages(failed, [steer]).map((message) => message.id)).toEqual(["msg_3", "msg_idle"]) + }) }) describe("applyTimelineMessageHandoff", () => { diff --git a/packages/app/src/session/timeline/controller-projection.ts b/packages/app/src/session/timeline/controller-projection.ts index 33f7650fed6..ac11f5200f3 100644 --- a/packages/app/src/session/timeline/controller-projection.ts +++ b/packages/app/src/session/timeline/controller-projection.ts @@ -37,6 +37,27 @@ export function visibleTimelineMessages( // Undelivered inputs do not own assistant work, so they stay below the active work like the TUI. // They keep admission order: the server delivers steers in that order, so delivery moves nothing. + // A pre-promotion failure ends in a failed idle marker with no assistant work, so keep that marker + // after the input that triggered it. + const tail = visible.at(-1) + + if (tail?.type === "idle" && tail.outcome === "failed") { + const start = visible.slice(0, -1).findLastIndex((message) => message.type === "idle") + 1 + + if ( + visible.slice(start, -1).some((message) => inputs.has(message.id)) && + !visible.slice(start, -1).some((message) => message.type === "assistant") + ) { + const rest = visible.slice(0, -1) + + return [ + ...rest.filter((message) => !inputs.has(message.id)), + ...rest.filter((message) => inputs.has(message.id)), + tail, + ] + } + } + return [ ...visible.filter((message) => !inputs.has(message.id)), ...visible.filter((message) => inputs.has(message.id)), diff --git a/packages/client/src/promise/generated/types.ts b/packages/client/src/promise/generated/types.ts index ecf60bf9877..2c3577fa490 100644 --- a/packages/client/src/promise/generated/types.ts +++ b/packages/client/src/promise/generated/types.ts @@ -161,14 +161,6 @@ export type SessionProviderContextProvenance = { endpoint: string } -export type SessionMessageIdle = { - id: string - metadata?: { [x: string]: JsonValue } - time: { created: number } - type: "idle" - outcome: "succeeded" | "failed" | "interrupted" -} - export type SessionActive = { type: "running" } export type SessionInboxDelivery = "steer" | "queue" @@ -559,6 +551,15 @@ export type SessionMessageCompactionFailed = { tokens?: TokenUsageInfo } +export type SessionMessageIdle = { + id: string + metadata?: { [x: string]: JsonValue } + time: { created: number } + type: "idle" + outcome: "succeeded" | "failed" | "interrupted" + error?: SessionStructuredError +} + export type SessionProviderContext = { version: 1; provenance: SessionProviderContextProvenance; messages: JsonValue } export type SessionInboxSynthetic = { @@ -3400,6 +3401,12 @@ export type SessionImportInput = { readonly time: { readonly created: number } readonly type: "idle" readonly outcome: "succeeded" | "failed" | "interrupted" + readonly error?: { + readonly type: string + readonly message: string + readonly status?: number + readonly response?: { readonly body: string } + } } > readonly location?: { readonly directory: string } | null @@ -3737,6 +3744,12 @@ export type SessionImportInput = { readonly time: { readonly created: number } readonly type: "idle" readonly outcome: "succeeded" | "failed" | "interrupted" + readonly error?: { + readonly type: string + readonly message: string + readonly status?: number + readonly response?: { readonly body: string } + } } > readonly location?: { readonly directory: string } | null @@ -4074,6 +4087,12 @@ export type SessionImportInput = { readonly time: { readonly created: number } readonly type: "idle" readonly outcome: "succeeded" | "failed" | "interrupted" + readonly error?: { + readonly type: string + readonly message: string + readonly status?: number + readonly response?: { readonly body: string } + } } > readonly location?: { readonly directory: string } | null diff --git a/packages/client/src/solid/data.ts b/packages/client/src/solid/data.ts index d06f5a7901f..d064bb9cda2 100644 --- a/packages/client/src/solid/data.ts +++ b/packages/client/src/solid/data.ts @@ -1047,6 +1047,7 @@ export function createData(config: CreateDataInput) { : event.type === "session.execution.failed" ? "failed" : "interrupted", + ...(event.type === "session.execution.failed" ? { error: event.data.error } : {}), time: { created: event.created }, }) if ( @@ -1054,7 +1055,8 @@ export function createData(config: CreateDataInput) { (item) => item.type === "assistant" && item.content.some( - (part) => part.type === "tool" && (part.state.status === "streaming" || part.state.status === "running"), + (part) => + part.type === "tool" && (part.state.status === "streaming" || part.state.status === "running"), ), ) ) { diff --git a/packages/client/test/solid-data.test.ts b/packages/client/test/solid-data.test.ts index 07b8dfaef15..25ba41e4a7f 100644 --- a/packages/client/test/solid-data.test.ts +++ b/packages/client/test/solid-data.test.ts @@ -1004,9 +1004,24 @@ test.each([ created: 2, type, durable: { aggregateID: "ses_refresh", seq: 2, version: 1 }, - data: { sessionID: "ses_refresh", reason: "user" }, + data: { + sessionID: "ses_refresh", + reason: "user", + ...(type === "session.execution.failed" + ? { error: { type: "provider.no-route", message: "Model unavailable: opencode/gpt-5.2" } } + : {}), + }, }) expect(setup.data.session.status("ses_refresh")).toBe(type === "session.execution.started" ? "running" : "idle") + if (type === "session.execution.failed") { + expect(setup.data.session.message.list("ses_refresh").at(-1)).toEqual({ + id: "msg_activity", + type: "idle", + outcome: "failed", + error: { type: "provider.no-route", message: "Model unavailable: opencode/gpt-5.2" }, + time: { created: 2 }, + }) + } release.resolve() await wait(() => setup.data.session.status("ses_hydrated") === "running") expect(setup.data.session.status("ses_refresh")).toBe(type === "session.execution.started" ? "running" : "idle") diff --git a/packages/core/src/session/message-updater.ts b/packages/core/src/session/message-updater.ts index 6d2a40e4195..14143bbfa8e 100644 --- a/packages/core/src/session/message-updater.ts +++ b/packages/core/src/session/message-updater.ts @@ -60,7 +60,7 @@ export function update(adapter: Adapter, event: SessionEvent.DurableEvent) { ) }) - const idle = (outcome: SessionMessage.Idle["outcome"]) => + const idle = (outcome: SessionMessage.Idle["outcome"], error?: SessionMessage.Idle["error"]) => clearCurrentRetry.pipe( Effect.andThen( adapter.appendMessage( @@ -68,6 +68,7 @@ export function update(adapter: Adapter, event: SessionEvent.DurableEvent) { id: SessionMessage.ID.fromEvent(event.id), type: "idle", outcome, + error, metadata: event.metadata, time: { created }, }), @@ -141,7 +142,7 @@ export function update(adapter: Adapter, event: SessionEvent.DurableEvent) { "session.inbox.delivery.changed": () => Effect.void, "session.execution.started": () => Effect.void, "session.execution.succeeded": () => idle("succeeded"), - "session.execution.failed": () => idle("failed"), + "session.execution.failed": (event) => idle("failed", event.data.error), // Shutdown keeps the execution claim and the resumed drain continues the turn. "session.execution.interrupted": (event) => event.data.reason === "shutdown" ? clearCurrentRetry : idle("interrupted"), diff --git a/packages/core/test/session-view.test.ts b/packages/core/test/session-view.test.ts index 1092abf68e0..2bca2a79eaf 100644 --- a/packages/core/test/session-view.test.ts +++ b/packages/core/test/session-view.test.ts @@ -84,6 +84,11 @@ describe("Session.view", () => { if (!unread.time.idle || !unread.time.viewed) return yield* Effect.die(new Error("Expected attention times")) expect(DateTime.toEpochMillis(unread.time.idle)).toBeGreaterThan(DateTime.toEpochMillis(unread.time.viewed)) expect(unread.outcome).toBe("failed") + expect((yield* session.messages({ sessionID: created.id, order: "asc" })).at(-1)).toMatchObject({ + type: "idle", + outcome: "failed", + error: { type: "unknown", message: "failed" }, + }) yield* session.view({ sessionID: created.id, idle: DateTime.toEpochMillis(unread.time.idle) }) expect((yield* session.get(created.id)).time.viewed).toEqual(unread.time.idle) diff --git a/packages/schema/src/session-message.ts b/packages/schema/src/session-message.ts index 55ef745584d..f8a3e64b01c 100644 --- a/packages/schema/src/session-message.ts +++ b/packages/schema/src/session-message.ts @@ -290,6 +290,7 @@ export const Idle = Schema.Struct({ ...Base, type: Schema.tag("idle"), outcome: Schema.Literals(["succeeded", "failed", "interrupted"]), + error: SessionError.Error.pipe(optional), }).annotate({ identifier: "Session.Message.Idle" }) export const Info = Schema.Union([ diff --git a/packages/session-ui/src/timeline/error-card.stories.tsx b/packages/session-ui/src/timeline/error-card.stories.tsx index 1b395a79827..c785b1d1c0c 100644 --- a/packages/session-ui/src/timeline/error-card.stories.tsx +++ b/packages/session-ui/src/timeline/error-card.stories.tsx @@ -52,3 +52,49 @@ export const ProviderErrors = { /> ), } + +const executionErrorsDocument = { + sessionID: CURRENT_SESSION_ID, + messages: [ + { + id: "msg_story_exec_user_1", + type: "user", + text: "Reply with the single word ok.", + time: { created: STORY_TIME + 40_000 }, + }, + { + id: "msg_story_exec_idle_1", + type: "idle", + outcome: "failed", + error: { type: "provider.no-route", message: "Model unavailable: opencode/gpt-5.2" }, + time: { created: STORY_TIME + 40_500 }, + }, + { + id: "msg_story_exec_user_2", + type: "user", + text: "Check the changed files and run lint.", + time: { created: STORY_TIME + 42_000 }, + }, + ...thinkingDocument.messages.slice(1), + { + id: "msg_story_exec_idle_2", + type: "idle", + outcome: "failed", + error: { type: "unknown", message: "Failed to execute statement" }, + time: { created: STORY_TIME + 45_000 }, + }, + ], + status: { type: "idle" }, + diffs: [], +} satisfies SessionDocument + +export const ExecutionErrors = { + render: () => ( + + ), +} diff --git a/packages/session-ui/src/timeline/projection.ts b/packages/session-ui/src/timeline/projection.ts index e2e2dfad8fd..eee6dcd9eed 100644 --- a/packages/session-ui/src/timeline/projection.ts +++ b/packages/session-ui/src/timeline/projection.ts @@ -1,6 +1,7 @@ import type { ModelRef, SessionMessageAssistant, + SessionMessageIdle, SessionMessageInfo, SessionMessageShell, SessionMessageUser, @@ -23,7 +24,15 @@ export type ReasoningMode = "hidden" | "compact" | "full" type Notice = Exclude -type Entry = { type: "assistant"; message: SessionMessageAssistant } | { type: "notice"; message: Notice } +type FailedIdle = SessionMessageIdle & { + outcome: "failed" + error: NonNullable +} + +type Entry = + | { type: "assistant"; message: SessionMessageAssistant } + | { type: "notice"; message: Notice } + | { type: "idle"; message: FailedIdle } type Content = SessionMessageAssistant["content"][number] @@ -264,6 +273,34 @@ export namespace Timeline { return } + if (isFailedIdle(message)) { + const last = current?.entries.at(-1) + + if ( + last?.type === "notice" && + last.message.type === "compaction" && + last.message.status === "failed" && + !isInterrupted(last.message.error) + ) + return + + const lastStep = current?.entries.findLast((entry) => entry.type === "assistant" || entry.type === "idle") + + if (lastStep?.type === "assistant" && lastStep.message.error && !isInterrupted(lastStep.message.error)) return + + if (current && !current.shell) { + current.entries.push({ type: "idle", message }) + + return + } + + const turn: Turn = { id: message.id, time: message.time, entries: [{ type: "idle", message }] } + turns.push(turn) + current = turn + + return + } + if (message.type !== "assistant") return const existing = current?.user ? current : undefined @@ -285,7 +322,9 @@ export namespace Timeline { current = turn }) - const activeMessageID = turns.findLast((turn) => !pendingInputIDs?.has(turn.id))?.id ?? turns.at(-1)?.id + const activeMessageID = + turns.findLast((turn) => !pendingInputIDs?.has(turn.id) || turn.entries.some((entry) => entry.type === "idle")) + ?.id ?? turns.at(-1)?.id const visibleNotice = (message: Notice) => !detail || detail.notices.placement !== "hidden" || timelineNoticeRequired(message) @@ -299,7 +338,8 @@ export namespace Timeline { return turn.entries.some((entry) => entry.type === "notice" ? visibleNotice(entry.message) - : !!entry.message.error || + : entry.type === "idle" || + !!entry.message.error || !!entry.message.retry || entry.message.content.some((content) => isRenderable(content, showReasoning, detail)), ) @@ -394,6 +434,7 @@ export namespace Timeline { const rows: TimelineRow.TimelineRow[] = [] const assistantMessages = entries.flatMap((entry) => (entry.type === "assistant" ? [entry.message] : [])) const lastAssistant = assistantMessages.at(-1) + const lastStep = entries.findLast((entry) => entry.type === "assistant" || entry.type === "idle") const previousUserMessage = index > 0 const compaction = entries.some((entry) => entry.type === "notice" && entry.message.type === "compaction") const lastContent = lastAssistant?.content.at(-1) @@ -401,6 +442,7 @@ export namespace Timeline { const working = isActive && status.type === "busy" && + lastStep?.type === "assistant" && lastAssistant?.time.completed === undefined && !lastAssistant?.error && !lastAssistant?.retry @@ -482,6 +524,8 @@ export namespace Timeline { }), ) + if (lastStep?.type !== "assistant") return + if (isActive && lastAssistant?.retry) rows.push(new TimelineRow.Retry({ userMessageID: turnID })) else if (lastAssistant?.error && !isInterrupted(lastAssistant.error)) rows.push( @@ -495,6 +539,19 @@ export namespace Timeline { case "assistant": assistantSegment.push(entry.message) + return + case "idle": + appendAssistantSegment(assistantSegment) + assistantSegment = [] + + if (entry === lastStep) + rows.push( + new TimelineRow.Error({ + userMessageID: turnID, + text: unwrapErrorMessage(entry.message.error.message), + }), + ) + return case "notice": if (detail?.notices.placement === "hidden" && !timelineNoticeRequired(entry.message)) return @@ -533,6 +590,10 @@ function isInterrupted(error: SessionMessageAssistant["error"]) { return error?.type.toLowerCase().includes("abort") || error?.type.toLowerCase().includes("interrupt") } +function isFailedIdle(message: SessionMessageInfo): message is FailedIdle { + return message.type === "idle" && message.outcome === "failed" && !!message.error +} + function shellFailed(message: SessionMessageShell) { return ( message.status === "timeout" || (message.status === "exited" && message.exit !== undefined && message.exit !== 0) diff --git a/packages/session-ui/src/timeline/rows-current.test.ts b/packages/session-ui/src/timeline/rows-current.test.ts index a08290feadc..1ce07da0696 100644 --- a/packages/session-ui/src/timeline/rows-current.test.ts +++ b/packages/session-ui/src/timeline/rows-current.test.ts @@ -1304,4 +1304,108 @@ describe("current session timeline rows", () => { ["UserMessage", "AssistantPart", "Notice", "AssistantPart"], ) }) + + test("surfaces failed idle errors when no assistant step owns the failure", () => { + const preStep = [ + { id: "msg_user", type: "user", text: "prompt", time: { created: 1 } }, + { + id: "msg_idle", + type: "idle", + outcome: "failed", + error: { type: "provider.no-route", message: "Model unavailable: opencode/gpt-5.2" }, + time: { created: 2 }, + }, + { + id: "msg_model", + type: "model-switched", + model: { id: "next", providerID: "provider" }, + time: { created: 3 }, + }, + ] satisfies SessionMessageInfo[] + + const preStepRows = Timeline.constructSessionMessageRows(preStep, true, { type: "idle" }).rows + expect(preStepRows.map((row) => row._tag)).toEqual(["UserMessage", "Error", "Notice"]) + expect(preStepRows[1]).toEqual( + new TimelineRow.Error({ userMessageID: "msg_user", text: "Model unavailable: opencode/gpt-5.2" }), + ) + + const betweenSteps = [ + { id: "msg_user", type: "user", text: "prompt", time: { created: 1 } }, + { + id: "msg_assistant", + type: "assistant", + agent: "build", + model: { id: "model", providerID: "provider" }, + content: [storyTool("tool_read", "read", "completed", {})], + finish: "tool-calls", + time: { created: 2, completed: 3 }, + }, + { + id: "msg_idle", + type: "idle", + outcome: "failed", + error: { type: "unknown", message: "Failed to execute statement" }, + time: { created: 4 }, + }, + ] satisfies SessionMessageInfo[] + + expect( + Timeline.constructSessionMessageRows(betweenSteps, true, { type: "idle" }).rows.map((row) => row._tag), + ).toEqual(["UserMessage", "AssistantPart", "Error"]) + + const duplicate = [ + { id: "msg_user", type: "user", text: "prompt", time: { created: 1 } }, + { + id: "msg_assistant", + type: "assistant", + agent: "build", + model: { id: "model", providerID: "provider" }, + content: [], + finish: "error", + error: { type: "provider.auth", message: "Your organization does not have access to this model." }, + time: { created: 2, completed: 3 }, + }, + { + id: "msg_idle", + type: "idle", + outcome: "failed", + error: { type: "provider.auth", message: "Your organization does not have access to this model." }, + time: { created: 4 }, + }, + ] satisfies SessionMessageInfo[] + + expect(Timeline.constructSessionMessageRows(duplicate, true, { type: "idle" }).rows.map((row) => row._tag)).toEqual( + ["UserMessage", "Error"], + ) + + const exhausted = [ + { id: "msg_user", type: "user", text: "prompt", time: { created: 1 } }, + { + id: "msg_assistant", + type: "assistant", + agent: "build", + model: { id: "model", providerID: "provider" }, + content: [], + error: { type: "aborted", message: "Step interrupted" }, + time: { created: 2, completed: 3 }, + }, + { + id: "msg_idle", + type: "idle", + outcome: "failed", + error: { + type: "aborted", + message: "Execution was interrupted repeatedly and will not be resumed automatically.", + }, + time: { created: 4 }, + }, + ] satisfies SessionMessageInfo[] + + expect(Timeline.constructSessionMessageRows(exhausted, true, { type: "idle" }).rows.at(-1)).toEqual( + new TimelineRow.Error({ + userMessageID: "msg_user", + text: "Execution was interrupted repeatedly and will not be resumed automatically.", + }), + ) + }) }) diff --git a/packages/tui/src/routes/session/index.tsx b/packages/tui/src/routes/session/index.tsx index 80e60d1f842..a89ed127b3d 100644 --- a/packages/tui/src/routes/session/index.tsx +++ b/packages/tui/src/routes/session/index.tsx @@ -1837,10 +1837,22 @@ function SessionMessageView(props: { message: SessionMessageInfo }) { } /> + + {(error) => } + ) } +function ExecutionError(props: { error: NonNullable["error"]> }) { + const theme = useTheme() + return ( + + Error: {errorMessage(props.error)} + + ) +} + function SessionPartView(props: { partRef: PartRef message: (messageID: string) => SessionMessageInfo | undefined diff --git a/packages/tui/src/routes/session/rows.ts b/packages/tui/src/routes/session/rows.ts index 52e45c94a30..9bcd0cf8b3e 100644 --- a/packages/tui/src/routes/session/rows.ts +++ b/packages/tui/src/routes/session/rows.ts @@ -370,17 +370,35 @@ export function reduceSessionRows( }) usage.previousTurnCache = { read: last.tokens.cache.read, model: last.model } } + // A failure before promotion leaves its prompt queued, so keep the failure after that prompt. + const tail = messages.at(-1) + const period = messages.slice(messages.slice(0, -1).findLastIndex((message) => message.type === "idle") + 1, -1) + const deferred = + tail?.type === "idle" && + tail.outcome === "failed" && + period.some(isInput) && + !period.some((message) => message.type === "assistant") + ? tail + : undefined + // The last step or compaction may already show the failure that ended the busy period. + let failureShown = false const entries = [ - ...messages.filter((message) => !pending.has(message.id)), + ...messages.filter((message) => !pending.has(message.id) && message !== deferred), ...pendingCompactions, ...messages.filter(isInput), + ...(deferred ? [deferred] : []), ].reduce((rows, message) => { if (message.type !== "assistant") { if (message.type === "idle") { flushTurn(rows) + if (message.outcome === "failed" && message.error && !failureShown) + rows.push({ entry: { type: "message", messageID: message.id }, path: [], closesPrevious: true }) + failureShown = false return rows } if (message.type === "synthetic" && !message.description?.trim()) return rows + if (message.type === "compaction" && message.status !== "running") + failureShown = message.status === "failed" && message.error.type !== "aborted" if (message.type === "compaction" && message.status === "completed" && usage) usage.previousTurnCache = undefined rows.push({ entry: { type: "message", messageID: message.id }, @@ -390,6 +408,7 @@ export function reduceSessionRows( return rows } usage?.steps.push(message) + failureShown = !!message.error && message.error.type !== "aborted" const ordinals = { text: 0, reasoning: 0 } message.content.forEach((part) => { const partID = part.type === "tool" ? part.id : `${part.type}:${ordinals[part.type]++}` diff --git a/packages/tui/test/cli/tui/session-rows.test.ts b/packages/tui/test/cli/tui/session-rows.test.ts index bc7cc3c723c..643997640b7 100644 --- a/packages/tui/test/cli/tui/session-rows.test.ts +++ b/packages/tui/test/cli/tui/session-rows.test.ts @@ -603,6 +603,43 @@ test("renders a footer for a pre-output retry assistant after replay", () => { expect(reduceSessionRows([message])).toEqual([{ type: "assistant-footer", messageID: "assistant-retry" }]) }) +test("shows a persisted execution failure once, after the prompt that caused it", () => { + const user: SessionMessageInfo = { type: "user", id: "user", text: "Go", time: { created: 1 } } + const failed: SessionMessageInfo = { + type: "idle", + id: "idle", + outcome: "failed", + error: { type: "provider.no-route", message: "Model unavailable: opencode/gpt-5.2" }, + time: { created: 3 }, + } + const clean: SessionMessageAssistant = { ...assistant("assistant", []), finish: "stop" } + const errored = { ...clean, error: { type: "provider.quota", message: "Insufficient account funds" } } + + expect(reduceSessionRows([user, failed])).toEqual([ + { type: "message", messageID: "user" }, + { type: "message", messageID: "idle" }, + ]) + expect(reduceSessionRows([user, clean, failed])).toEqual([ + { type: "message", messageID: "user" }, + { type: "assistant-footer", messageID: "assistant" }, + { type: "message", messageID: "idle" }, + ]) + expect(reduceSessionRows([user, errored, failed])).toEqual([ + { type: "message", messageID: "user" }, + { type: "assistant-footer", messageID: "assistant" }, + ]) + const interrupted = { ...clean, error: { type: "aborted", message: "Step interrupted" } } + const exhausted = { + ...failed, + error: { type: "aborted", message: "Execution was interrupted repeatedly and will not be resumed automatically." }, + } + expect(reduceSessionRows([user, interrupted, exhausted]).at(-1)).toEqual({ type: "message", messageID: "idle" }) + expect(reduceSessionRows([user, failed], new Set(["user"]))).toEqual([ + { type: "message", messageID: "user" }, + { type: "message", messageID: "idle" }, + ]) +}) + test("places a running compaction barrier before every queued user message", () => { const queued = (id: string, text: string, created: number): SessionMessageInfo => ({ type: "user",