From 052fd8a52dd3254790d8a87552ad25b8db974b02 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Sun, 22 Feb 2026 01:28:10 +0000 Subject: [PATCH] Fix multiple bugs: failed turn status, duplicate project, navigation race, session lifecycle, snapshot consistency, timeline IDs, test stateDir, and DB close - Bug 1: Check turn/completed payload for failed status instead of unconditionally setting 'ready' - Bug 2: In Electron add-project flow, use server-returned project ID to avoid duplicate project.create dispatch - Bug 3: Await dispatchCommand for thread.create before navigating to avoid race with store sync - Bug 4: Handle session/closed and session/exited events to update orchestration session status - Bug 5: Capture orchestration snapshot once to avoid fragile double getSnapshot() call - Bug 6: Restore 'message:' and 'work:' prefixes on timeline entry IDs for uniqueness - Bug 9: Pass stateDir to createServer in test helper to isolate SQLite from home directory - Bug 10: Add close() method to SqliteEventStore and call it from OrchestrationEngine.stop() --- apps/server/src/orchestration/engine.ts | 1 + apps/server/src/orchestration/eventStore.ts | 4 ++ apps/server/src/wsServer.test.ts | 1 + apps/server/src/wsServer.ts | 43 ++++++++++++++++++-- apps/web/src/components/Sidebar.tsx | 44 +++++++++++---------- apps/web/src/session-logic.ts | 4 +- 6 files changed, 70 insertions(+), 27 deletions(-) diff --git a/apps/server/src/orchestration/engine.ts b/apps/server/src/orchestration/engine.ts index 5b93c507a3..7060787729 100644 --- a/apps/server/src/orchestration/engine.ts +++ b/apps/server/src/orchestration/engine.ts @@ -219,6 +219,7 @@ export class OrchestrationEngine { await Runtime.runPromise(this.runtime)(Fiber.interrupt(this.workerFiber)); this.workerFiber = null; } + this.eventStore.close(); } getSnapshot(): OrchestrationReadModel { diff --git a/apps/server/src/orchestration/eventStore.ts b/apps/server/src/orchestration/eventStore.ts index bc54c9767d..11630869c9 100644 --- a/apps/server/src/orchestration/eventStore.ts +++ b/apps/server/src/orchestration/eventStore.ts @@ -99,4 +99,8 @@ export class SqliteEventStore { readAll(): Effect.Effect { return this.readFromSequence(0, Number.MAX_SAFE_INTEGER); } + + close(): void { + this.db.close(); + } } diff --git a/apps/server/src/wsServer.test.ts b/apps/server/src/wsServer.test.ts index a9b9cc1102..faadfaa6ac 100644 --- a/apps/server/src/wsServer.test.ts +++ b/apps/server/src/wsServer.test.ts @@ -249,6 +249,7 @@ describe("WebSocket Server", () => { return createServer({ port: 0, cwd: options.cwd ?? "/test/project", + stateDir, ...(options.devUrl ? { devUrl: options.devUrl } : {}), ...(options.authToken ? { authToken: options.authToken } : {}), projectRegistry: new ProjectRegistry(stateDir), diff --git a/apps/server/src/wsServer.ts b/apps/server/src/wsServer.ts index a4cfef7dad..bcd0707458 100644 --- a/apps/server/src/wsServer.ts +++ b/apps/server/src/wsServer.ts @@ -157,6 +157,19 @@ export function createServer(options: ServerOptions) { if (!thread) return; const now = event.createdAt; if (event.method === "turn/started" || event.method === "turn/completed") { + let turnStatus: "running" | "ready" | "error" = "ready"; + let lastError: string | null = null; + if (event.method === "turn/started") { + turnStatus = "running"; + } else { + const payload = event.payload as Record | undefined; + const turn = payload?.turn as Record | undefined; + if (typeof turn?.status === "string" && turn.status === "failed") { + turnStatus = "error"; + const turnError = turn.error as Record | undefined; + lastError = typeof turnError?.message === "string" ? turnError.message : null; + } + } await orchestrationEngine.dispatch({ type: "thread.session", commandId: crypto.randomUUID(), @@ -164,12 +177,12 @@ export function createServer(options: ServerOptions) { session: { sessionId: event.sessionId, provider: event.provider, - status: event.method === "turn/started" ? "running" : "ready", + status: turnStatus, threadId: thread.id, activeTurnId: event.method === "turn/started" ? (event.turnId ?? null) : null, createdAt: thread.session?.createdAt ?? now, updatedAt: now, - lastError: null, + lastError, }, createdAt: now, }); @@ -200,6 +213,27 @@ export function createServer(options: ServerOptions) { createdAt: now, }); } + if ( + event.kind === "session" && + (event.method === "session/closed" || event.method === "session/exited") + ) { + await orchestrationEngine.dispatch({ + type: "thread.session", + commandId: crypto.randomUUID(), + threadId: thread.id, + session: { + sessionId: event.sessionId, + provider: event.provider, + status: "closed", + threadId: thread.id, + activeTurnId: null, + createdAt: thread.session?.createdAt ?? now, + updatedAt: now, + lastError: event.message ?? null, + }, + createdAt: now, + }); + } if (event.kind === "error" && event.message) { await orchestrationEngine.dispatch({ type: "thread.session", @@ -347,12 +381,13 @@ export function createServer(options: ServerOptions) { logOutgoingPush(welcome, 1); ws.send(JSON.stringify(welcome)); + const initialSnapshot = orchestrationEngine.getSnapshot(); const snapshotPush: WsPush = { type: "push", channel: ORCHESTRATION_WS_CHANNELS.readModel, data: { - sequence: orchestrationEngine.getSnapshot().sequence, - snapshot: orchestrationEngine.getSnapshot(), + sequence: initialSnapshot.sequence, + snapshot: initialSnapshot, } satisfies OrchestrationReadModelPush, }; logOutgoingPush(snapshotPush, 1); diff --git a/apps/web/src/components/Sidebar.tsx b/apps/web/src/components/Sidebar.tsx index 0d2ad398f2..543fa2514f 100644 --- a/apps/web/src/components/Sidebar.tsx +++ b/apps/web/src/components/Sidebar.tsx @@ -132,7 +132,7 @@ export default function Sidebar() { const pendingApprovalByThreadId = useMemo(() => new Map(), []); const handleNewThread = useCallback( - ( + async ( projectId: string, options?: { branch?: string | null; @@ -143,7 +143,7 @@ export default function Sidebar() { const threadId = crypto.randomUUID(); const createdAt = new Date().toISOString(); const model = state.projects.find((project) => project.id === projectId)?.model ?? DEFAULT_MODEL; - void api.orchestration.dispatchCommand({ + await api.orchestration.dispatchCommand({ type: "thread.create", commandId: crypto.randomUUID(), threadId, @@ -195,23 +195,25 @@ export default function Sidebar() { return; } - const projectId = crypto.randomUUID(); - const now = new Date().toISOString(); - const name = inferProjectName(cwd); + let projectId: string; if (isElectron) { - // Keep project registry in sync for desktop integrations. - await api.projects.add({ cwd }); + const result = await api.projects.add({ cwd }); + projectId = result.project.id; + } else { + projectId = crypto.randomUUID(); + const now = new Date().toISOString(); + const name = inferProjectName(cwd); + await api.orchestration.dispatchCommand({ + type: "project.create", + commandId: crypto.randomUUID(), + projectId, + name, + cwd, + model: DEFAULT_MODEL, + createdAt: now, + }); } - await api.orchestration.dispatchCommand({ - type: "project.create", - commandId: crypto.randomUUID(), - projectId, - name, - cwd, - model: DEFAULT_MODEL, - createdAt: now, - }); - handleNewThread(projectId); + await handleNewThread(projectId); } finally { setIsAddingProject(false); setNewCwd(""); @@ -416,7 +418,7 @@ export default function Sidebar() { const projectId = activeThread?.projectId ?? state.projects[0]?.id; if (!projectId) return; event.preventDefault(); - handleNewThread(projectId); + void handleNewThread(projectId); return; } @@ -424,7 +426,7 @@ export default function Sidebar() { const projectId = activeThread?.projectId ?? state.projects[0]?.id; if (!projectId) return; event.preventDefault(); - handleNewThread(projectId, { + void handleNewThread(projectId, { branch: activeThread?.branch ?? null, worktreePath: activeThread?.worktreePath ?? null, }); @@ -463,7 +465,7 @@ export default function Sidebar() { return; } const firstProject = state.projects[0]; - if (firstProject) handleNewThread(firstProject.id); + if (firstProject) void handleNewThread(firstProject.id); }} > + @@ -578,7 +580,7 @@ export default function Sidebar() { diff --git a/apps/web/src/session-logic.ts b/apps/web/src/session-logic.ts index f8a5592be8..0cb9eb69e2 100644 --- a/apps/web/src/session-logic.ts +++ b/apps/web/src/session-logic.ts @@ -86,13 +86,13 @@ export function deriveTimelineEntries( workEntries: WorkLogEntry[], ): TimelineEntry[] { const messageRows: TimelineEntry[] = messages.map((message) => ({ - id: message.id, + id: `message:${message.id}`, kind: "message", createdAt: message.createdAt, message, })); const workRows: TimelineEntry[] = workEntries.map((entry) => ({ - id: entry.id, + id: `work:${entry.id}`, kind: "work", createdAt: entry.createdAt, entry,