Make bootstrap resilient when codex startup fails
Co-authored-by: Theo Browne <[email protected]>
This commit is contained in:
1 parent
29aeea5bdb
commit
e58c71a30e
5 files changed
+82
-13
No files matched your search
@@ -339,7 +339,11 @@ export default function ChatView() {
|
||||
|
||||
const ensureSession = async (): Promise<EnsuredSessionInfo | null> => {
|
||||
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
|
||||
|
||||
@@ -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({
|
||||
|
||||
@@ -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,
|
||||
)
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@ export interface AppBootstrapResult {
|
||||
provider: ProviderKind;
|
||||
model: string;
|
||||
session: ProviderSession;
|
||||
bootstrapError?: string;
|
||||
}
|
||||
|
||||
export interface NativeApi {
|
||||
|
||||
Reference in new issue
Block a user