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()
This commit is contained in:
Cursor Agent committed 2026-02-22 01:28:10 +00:00
1 parent 2001d39470
commit 052fd8a52d
6 files changed
+70 -27

No files matched your search

+1
View File
@@ -219,6 +219,7 @@ export class OrchestrationEngine {
await Runtime.runPromise(this.runtime)(Fiber.interrupt(this.workerFiber));
this.workerFiber = null;
}
this.eventStore.close();
}
getSnapshot(): OrchestrationReadModel {
@@ -99,4 +99,8 @@ export class SqliteEventStore {
readAll(): Effect.Effect<OrchestrationEvent[]> {
return this.readFromSequence(0, Number.MAX_SAFE_INTEGER);
}
close(): void {
this.db.close();
}
}
+1
View File
@@ -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),
+39 -4
View File
@@ -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<string, unknown> | undefined;
const turn = payload?.turn as Record<string, unknown> | undefined;
if (typeof turn?.status === "string" && turn.status === "failed") {
turnStatus = "error";
const turnError = turn.error as Record<string, unknown> | 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);
+23 -21
View File
@@ -132,7 +132,7 @@ export default function Sidebar() {
const pendingApprovalByThreadId = useMemo(() => new Map<string, boolean>(), []);
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);
}}
>
<span className="text-foreground">+</span>
@@ -578,7 +580,7 @@ export default function Sidebar() {
<button
type="button"
className="flex w-full items-center gap-1 px-2 py-1 text-[10px] text-muted-foreground/60 transition-colors duration-150 hover:text-muted-foreground/80"
onClick={() => handleNewThread(project.id)}
onClick={() => void handleNewThread(project.id)}
>
<span>+</span> New thread
</button>
+2 -2
View File
@@ -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,