From e58c71a30e6279b0fc8dca23d9ad047870ea869a Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Mon, 9 Feb 2026 22:12:27 +0000 Subject: [PATCH] Make bootstrap resilient when codex startup fails Co-authored-by: Theo Browne --- apps/renderer/src/components/ChatView.tsx | 6 ++- apps/renderer/src/store.test.ts | 27 +++++++++++ apps/renderer/src/store.ts | 3 +- apps/t3/src/runtimeApiServer.ts | 58 ++++++++++++++++++----- packages/contracts/src/ipc.ts | 1 + 5 files changed, 82 insertions(+), 13 deletions(-) diff --git a/apps/renderer/src/components/ChatView.tsx b/apps/renderer/src/components/ChatView.tsx index c382788db0..8f3b0d11b9 100644 --- a/apps/renderer/src/components/ChatView.tsx +++ b/apps/renderer/src/components/ChatView.tsx @@ -339,7 +339,11 @@ export default function ChatView() { const ensureSession = async (): Promise => { if (!api || !activeThread || !activeProject) return null; - if (activeThread.session && activeThread.session.status !== "closed") { + if ( + activeThread.session && + activeThread.session.status !== "closed" && + activeThread.session.status !== "error" + ) { const sessionThreadId = activeThread.session.threadId ?? null; const continuityState: SessionContinuityState = activeThread.codexThreadId === null diff --git a/apps/renderer/src/store.test.ts b/apps/renderer/src/store.test.ts index 5038fa9701..b7ffa00d65 100644 --- a/apps/renderer/src/store.test.ts +++ b/apps/renderer/src/store.test.ts @@ -93,6 +93,33 @@ describe("store reducer thread continuity", () => { expect(next.activeThreadId).toBe(next.threads[0]?.id ?? null); }); + it("surfaces bootstrap errors on the active thread", () => { + const state: AppState = { + projects: [], + threads: [], + activeThreadId: null, + runtimeMode: "full-access", + diffOpen: false, + }; + + const next = reducer(state, { + type: "BOOTSTRAP_FROM_SERVER", + bootstrap: { + launchCwd: "/workspace", + projectName: "workspace", + provider: "codex", + model: "gpt-5.3-codex", + session: makeSession({ + sessionId: "sess-bootstrap-error", + status: "error", + }), + bootstrapError: "Timed out waiting for initialize.", + }, + }); + + expect(next.threads[0]?.error).toBe("Timed out waiting for initialize."); + }); + it("stores codexThreadId from UPDATE_SESSION", () => { const state = makeState( makeThread({ diff --git a/apps/renderer/src/store.ts b/apps/renderer/src/store.ts index a5dc47269a..24a28ab76f 100644 --- a/apps/renderer/src/store.ts +++ b/apps/renderer/src/store.ts @@ -341,7 +341,7 @@ export function reducer(state: AppState, action: Action): AppState { session: bootstrap.session, messages: [], events: [], - error: null, + error: bootstrap.bootstrapError ?? null, createdAt: new Date().toISOString(), } satisfies Thread); @@ -364,6 +364,7 @@ export function reducer(state: AppState, action: Action): AppState { ...entry, session: bootstrap.session, codexThreadId: bootstrap.session.threadId ?? entry.codexThreadId, + error: bootstrap.bootstrapError ?? entry.error, } : entry, ) diff --git a/apps/t3/src/runtimeApiServer.ts b/apps/t3/src/runtimeApiServer.ts index 417c15dd23..2384428449 100644 --- a/apps/t3/src/runtimeApiServer.ts +++ b/apps/t3/src/runtimeApiServer.ts @@ -8,6 +8,7 @@ import { EDITORS, DEFAULT_MODEL, type AppBootstrapResult, + type ProviderSession, type WsClientMessage, type WsResponseMessage, WS_EVENT_CHANNELS, @@ -273,21 +274,53 @@ export async function startRuntimeApiServer( emitEvent(WS_EVENT_CHANNELS.providerEvent, payload); }); + const createBootstrapErrorSession = (message: string): ProviderSession => { + const timestamp = new Date().toISOString(); + return { + sessionId: `bootstrap-error-${Date.now()}`, + provider: "codex", + status: "error", + cwd: launchCwd, + model: DEFAULT_MODEL, + createdAt: timestamp, + updatedAt: timestamp, + lastError: message, + }; + }; + const ensureLaunchSession = async () => { const existingSession = providerManager .listSessions() .find((session) => session.cwd === launchCwd && session.status !== "closed"); if (existingSession) { - return existingSession; + return { + session: existingSession, + bootstrapError: undefined, + }; } - return providerManager.startSession({ - provider: "codex", - cwd: launchCwd, - model: DEFAULT_MODEL, - approvalPolicy: "never", - sandboxMode: "danger-full-access", - }); + try { + const startedSession = await providerManager.startSession({ + provider: "codex", + cwd: launchCwd, + model: DEFAULT_MODEL, + approvalPolicy: "never", + sandboxMode: "danger-full-access", + }); + return { + session: startedSession, + bootstrapError: undefined, + }; + } catch (error) { + const message = + error instanceof Error + ? error.message + : "Failed to initialize Codex launch session."; + return { + session: createBootstrapErrorSession(message), + bootstrapError: message, + }; + } }; const wss = new WebSocketServer({ @@ -297,13 +330,16 @@ export async function startRuntimeApiServer( const resolveMethod = async (method: string, params: unknown) => { if (method === "app.bootstrap") { - const session = await ensureLaunchSession(); + const bootstrap = await ensureLaunchSession(); const payload: AppBootstrapResult = { launchCwd, projectName: path.basename(launchCwd) || launchCwd, provider: "codex", - model: session.model ?? DEFAULT_MODEL, - session, + model: bootstrap.session.model ?? DEFAULT_MODEL, + session: bootstrap.session, + ...(bootstrap.bootstrapError + ? { bootstrapError: bootstrap.bootstrapError } + : {}), }; return payload; } diff --git a/packages/contracts/src/ipc.ts b/packages/contracts/src/ipc.ts index 8ea5b99b51..dc3bd16222 100644 --- a/packages/contracts/src/ipc.ts +++ b/packages/contracts/src/ipc.ts @@ -26,6 +26,7 @@ export interface AppBootstrapResult { provider: ProviderKind; model: string; session: ProviderSession; + bootstrapError?: string; } export interface NativeApi {