diff --git a/apps/factory/agent/channels/workspace.ts b/apps/factory/agent/channels/workspace.ts index 65ee19e35dc6d..1834fb183b190 100644 --- a/apps/factory/agent/channels/workspace.ts +++ b/apps/factory/agent/channels/workspace.ts @@ -94,7 +94,11 @@ export default defineChannel< headers: { "cache-control": "no-store" } }); } catch (error) { - await failWorkspace(id, "Could not start the Eve workspace session."); + const reason = + error instanceof Error && error.message.trim() + ? `Could not start the workspace session: ${error.message.trim()}` + : "Could not start the workspace session."; + await failWorkspace(id, reason); throw error; } }) diff --git a/apps/factory/app/workspace-client.tsx b/apps/factory/app/workspace-client.tsx index 9daeb086a2a4c..51029eb80f9f4 100644 --- a/apps/factory/app/workspace-client.tsx +++ b/apps/factory/app/workspace-client.tsx @@ -48,7 +48,11 @@ import { import { sandboxSshCommand } from "../agent/lib/sandbox-ssh"; import { CopyCommand } from "../components/copy-command"; import { Button } from "../components/ui/button"; -import type { PublicWorkspace } from "./workspace-types"; +import { + latestWorkspaceFailure, + type PublicWorkspace, + type WorkspaceFailure +} from "./workspace-types"; const CONSOLE_HEADERS = { [OPERATOR_ACTION_HEADER]: OPERATOR_SESSION_ACTION }; const streamdownPlugins: PluginConfig = { @@ -123,7 +127,7 @@ export function WorkspaceClient({ workspaceId }: WorkspaceClientProps) {

); - if (!loaded?.workspace.sessionId) + if (!loaded) return (

@@ -131,6 +135,26 @@ export function WorkspaceClient({ workspaceId }: WorkspaceClientProps) {

); + if (!loaded.workspace.sessionId) + return ( +
+ {loaded.workspace.error ? ( +
+

Workspace failed to start

+

+ {loaded.workspace.error} +

+
+ ) : ( +

+ Starting workspace session… +

+ )} +
+ ); return ( ); @@ -202,6 +226,12 @@ function WorkspaceChat({ const busy = serverBusy || agent.status === "submitted" || agent.status === "streaming"; const hasAssistantProgress = hasRenderableAssistantProgress(messages.at(-1)); + const streamedFailure = useMemo( + () => latestWorkspaceFailure(events), + [events] + ); + const failure = + streamedFailure ?? fallbackWorkspaceFailure(agent.error?.message); useEffect(() => { if ( @@ -270,11 +300,11 @@ function WorkspaceChat({
{busy ? : null} - {busy ? "Working" : agent.status === "error" ? "Error" : "Ready"} + {busy ? "Working" : failure ? "Error" : "Ready"}
@@ -328,15 +358,7 @@ function WorkspaceChat({
- {agent.error ? ( -
- - {agent.error.message} -
- ) : null} + {failure ? : null} + +
+

{failureHeadline(failure)}

+ {failure.hint ? ( +

{failure.hint}

+ ) : null} + {failure.detail ? ( +
+            {failure.detail}
+          
+ ) : null} +
+
+ ); +} + function ChatConversation({ children }: { @@ -746,7 +794,9 @@ function ActivityFeed({ > {new Date(event.meta.at).toLocaleTimeString()} - {event.type} + + {describeActivityEvent(event)} + ))} @@ -803,15 +853,18 @@ function appendOptimisticMessage( } function hasLatestUserMessage(messages: readonly EveMessage[], text: string) { - return ( - [...messages] - .reverse() - .find((message) => message.role === "user") - ?.parts.filter((part) => part.type === "text") - .map((part) => part.text) - .join("\n") - .trim() === text.trim() - ); + for (let index = messages.length - 1; index >= 0; index -= 1) { + const message = messages[index]; + if (message?.role !== "user") continue; + return ( + message.parts + .filter((part) => part.type === "text") + .map((part) => part.text) + .join("\n") + .trim() === text.trim() + ); + } + return false; } function hasRenderableAssistantProgress(message: EveMessage | undefined) { @@ -856,3 +909,22 @@ function partKey(part: EveMessagePart, index: number) { ? part.toolCallId : `${part.type}:${index}`; } + +function fallbackWorkspaceFailure( + message: string | undefined +): WorkspaceFailure | undefined { + const normalized = message?.trim(); + return normalized ? { message: normalized } : undefined; +} + +function failureHeadline(failure: WorkspaceFailure): string { + const { code, message } = failure; + return !code || message === code || message.startsWith(`${code}:`) + ? message + : `${code}: ${message}`; +} + +function describeActivityEvent(event: MessageStreamEvent): string { + const failure = latestWorkspaceFailure([event]); + return failure ? `${event.type} — ${failureHeadline(failure)}` : event.type; +} diff --git a/apps/factory/app/workspace-types.ts b/apps/factory/app/workspace-types.ts index b88fed73ce9cd..886df88fafc3b 100644 --- a/apps/factory/app/workspace-types.ts +++ b/apps/factory/app/workspace-types.ts @@ -50,3 +50,74 @@ export function workspaceStatusLabel(status: string): string { if (status === "error") return "Error"; return status; } + +export interface WorkspaceFailure { + readonly code?: string; + readonly detail?: string; + readonly hint?: string; + readonly message: string; +} + +type WorkspaceEvent = { + readonly data?: unknown; + readonly type: string; +}; + +const FAILURE_EVENTS = new Set([ + "step.failed", + "turn.failed", + "session.failed" +]); +const FAILURE_RESET_EVENTS = new Set([ + "turn.started", + "turn.completed", + "turn.cancelled", + "session.completed" +]); + +/** + * Projects the current run failure from an agent event stream. Keeping this + * boundary independent of Eve's event types lets another harness map its + * failures into the workspace UI without changing the presentation. + */ +export function latestWorkspaceFailure( + events: readonly WorkspaceEvent[] +): WorkspaceFailure | undefined { + let failure: WorkspaceFailure | undefined; + for (const event of events) { + if (FAILURE_RESET_EVENTS.has(event.type)) { + failure = undefined; + continue; + } + if (!FAILURE_EVENTS.has(event.type)) continue; + const candidate = workspaceFailureFrom(event.data); + if (candidate) failure = candidate; + } + return failure; +} + +function workspaceFailureFrom(data: unknown): WorkspaceFailure | undefined { + if (!isRecord(data) || typeof data.message !== "string") return undefined; + const message = data.message.trim(); + if (!message) return undefined; + const details = isRecord(data.details) ? data.details : undefined; + const code = nonEmptyString(data.code); + const hint = nonEmptyString(details?.hint); + const rawDetail = nonEmptyString(details?.detail); + return { + ...(code ? { code } : {}), + ...(rawDetail && rawDetail !== message ? { detail: rawDetail } : {}), + ...(hint ? { hint } : {}), + message + }; +} + +function isRecord(value: unknown): value is Record { + return typeof value === "object" && value !== null && !Array.isArray(value); +} + +function nonEmptyString(value: unknown): string | undefined { + if (typeof value !== "string") return undefined; + const normalized = value.trim(); + return normalized || undefined; +} diff --git a/apps/factory/tests/workspace-types.test.mjs b/apps/factory/tests/workspace-types.test.mjs index 3a67500140bfe..b5dacb4b6421f 100644 --- a/apps/factory/tests/workspace-types.test.mjs +++ b/apps/factory/tests/workspace-types.test.mjs @@ -3,6 +3,7 @@ import test from "node:test"; import { isWorkspaceRunning, + latestWorkspaceFailure, workspaceStatusLabel } from "../app/workspace-types.ts"; @@ -14,3 +15,59 @@ test("workspace status labels describe operator-visible state", () => { assert.equal(isWorkspaceRunning("running"), true); assert.equal(isWorkspaceRunning("idle"), false); }); + +test("projects actionable failure information from workspace events", () => { + const events = [ + { type: "turn.started", data: { turnId: "turn_1" } }, + { + type: "step.failed", + data: { + code: "gateway-auth", + message: "Failed", + details: { + hint: "Refresh the AI Gateway credentials.", + detail: "401 Unauthorized\nCaused by an expired token." + } + } + }, + { + type: "turn.failed", + data: { + code: "gateway-auth", + message: "Failed", + details: { + hint: "Refresh the AI Gateway credentials.", + detail: "401 Unauthorized\nCaused by an expired token." + } + } + } + ]; + assert.deepEqual(latestWorkspaceFailure(events), { + code: "gateway-auth", + message: "Failed", + hint: "Refresh the AI Gateway credentials.", + detail: "401 Unauthorized\nCaused by an expired token." + }); +}); + +test("clears an old workspace failure when a later turn starts", () => { + assert.equal( + latestWorkspaceFailure([ + { + type: "session.failed", + data: { code: "failed", message: "First run failed" } + }, + { type: "turn.started", data: { turnId: "turn_2" } } + ]), + undefined + ); +}); + +test("ignores malformed workspace failure events", () => { + assert.equal( + latestWorkspaceFailure([ + { type: "turn.failed", data: { code: "failed", message: " " } } + ]), + undefined + ); +});