From 9b5063bcd4fafd53485ceced08a987118ab4a9ee Mon Sep 17 00:00:00 2001 From: Theo Browne Date: Fri, 24 Jul 2026 07:54:30 -0700 Subject: [PATCH] fix: preserve task tab invariants --- .../orchestration/decider.taskTabs.test.ts | 79 +++++++++++++++++++ apps/server/src/orchestration/decider.ts | 30 ++++++- apps/web/src/composerDraftStore.test.ts | 7 ++ apps/web/src/composerDraftStore.ts | 5 ++ 4 files changed, 118 insertions(+), 3 deletions(-) diff --git a/apps/server/src/orchestration/decider.taskTabs.test.ts b/apps/server/src/orchestration/decider.taskTabs.test.ts index 31fd707136..d2d2dfd0ee 100644 --- a/apps/server/src/orchestration/decider.taskTabs.test.ts +++ b/apps/server/src/orchestration/decider.taskTabs.test.ts @@ -75,6 +75,30 @@ const seededReadModel = Effect.gen(function* () { }); }); +const seededReadModelWithSibling = Effect.gen(function* () { + const readModel = yield* seededReadModel; + const decided = yield* decideOrchestrationCommand({ + readModel, + command: { + type: "thread.create", + commandId: CommandId.make("command-seed-child"), + threadId: ThreadId.make("thread-child"), + projectId, + workspaceTaskId: taskId, + tabPosition: 1, + title: "Alternative", + modelSelection, + runtimeMode: "full-access", + interactionMode: "default", + branch: "feature/task", + worktreePath: "/tmp/project-task", + createdAt: now, + }, + }); + const event = Array.isArray(decided) ? decided[0]! : decided; + return yield* projectEvent(readModel, event); +}); + it.layer(NodeServices.layer)("task tab invariants", (it) => { it.effect("accepts a sibling tab on the same project and worktree", () => Effect.gen(function* () { @@ -134,4 +158,59 @@ it.layer(NodeServices.layer)("task tab invariants", (it) => { expect(failure.message).toContain("must keep project and worktree identity"); }), ); + + it.effect("rejects moving one tab to another worktree after task creation", () => + Effect.gen(function* () { + const failure = yield* Effect.flip( + decideOrchestrationCommand({ + readModel: yield* seededReadModelWithSibling, + command: { + type: "thread.meta.update", + commandId: CommandId.make("command-move-root"), + threadId: rootThreadId, + worktreePath: "/tmp/other-tree", + }, + }), + ); + expect(failure.message).toContain("must keep project and worktree identity"); + }), + ); + + it.effect("rejects a deleted source for a portable fork", () => + Effect.gen(function* () { + const readModel = yield* seededReadModel; + const withDeletedSource = { + ...readModel, + threads: readModel.threads.map((thread) => + thread.id === rootThreadId ? { ...thread, deletedAt: now } : thread, + ), + }; + const failure = yield* Effect.flip( + decideOrchestrationCommand({ + readModel: withDeletedSource, + command: { + type: "thread.create", + commandId: CommandId.make("command-deleted-source"), + threadId: ThreadId.make("thread-from-deleted-source"), + projectId, + workspaceTaskId: taskId, + tabPosition: 1, + forkProvenance: { + mode: "portable", + sourceThreadId: rootThreadId, + createdAt: now, + }, + title: "Deleted source", + modelSelection, + runtimeMode: "full-access", + interactionMode: "default", + branch: "feature/task", + worktreePath: "/tmp/project-task", + createdAt: now, + }, + }), + ); + expect(failure.message).toContain("portable fork source must be an existing tab"); + }), + ); }); diff --git a/apps/server/src/orchestration/decider.ts b/apps/server/src/orchestration/decider.ts index 60fcab3503..463d7758c3 100644 --- a/apps/server/src/orchestration/decider.ts +++ b/apps/server/src/orchestration/decider.ts @@ -25,6 +25,10 @@ import { projectEvent } from "./projector.ts"; const nowIso = Effect.map(DateTime.now, DateTime.formatIso); +function workspaceTaskIdForThread(thread: OrchestrationReadModel["threads"][number]) { + return thread.workspaceTaskId ?? WorkspaceTaskId.make(String(thread.id)); +} + // Session adoption takes seconds; a user message still unadopted after this // window is a failed/stale start, not pending work. Mirrors the client's // QUEUED_TURN_START_GRACE_MS in client-runtime threadSettled.ts. @@ -360,8 +364,7 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand" command.workspaceTaskId ?? WorkspaceTaskId.make(String(command.threadId)); const sibling = readModel.threads.find( (thread) => - thread.deletedAt === null && - (thread.workspaceTaskId ?? WorkspaceTaskId.make(String(thread.id))) === workspaceTaskId, + thread.deletedAt === null && workspaceTaskIdForThread(thread) === workspaceTaskId, ); if ( sibling && @@ -387,7 +390,10 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand" ); if ( !source || - (source.workspaceTaskId ?? WorkspaceTaskId.make(String(source.id))) !== workspaceTaskId + source.deletedAt !== null || + workspaceTaskIdForThread(source) !== workspaceTaskId || + source.projectId !== command.projectId || + source.worktreePath !== command.worktreePath ) { return yield* new OrchestrationCommandInvariantError({ commandType: command.type, @@ -687,6 +693,24 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand" thread.branch !== command.expectedBranch ? thread.branch : command.branch; + if (command.worktreePath !== undefined) { + const workspaceTaskId = workspaceTaskIdForThread(thread); + const sibling = readModel.threads.find( + (candidate) => + candidate.id !== thread.id && + candidate.deletedAt === null && + workspaceTaskIdForThread(candidate) === workspaceTaskId, + ); + if ( + sibling && + (sibling.projectId !== thread.projectId || sibling.worktreePath !== command.worktreePath) + ) { + return yield* new OrchestrationCommandInvariantError({ + commandType: command.type, + detail: `workspace task ${workspaceTaskId} must keep project and worktree identity from sibling thread ${sibling.id}`, + }); + } + } const occurredAt = yield* nowIso; return { ...(yield* withEventBase({ diff --git a/apps/web/src/composerDraftStore.test.ts b/apps/web/src/composerDraftStore.test.ts index 8dd556fa91..921da33b74 100644 --- a/apps/web/src/composerDraftStore.test.ts +++ b/apps/web/src/composerDraftStore.test.ts @@ -873,6 +873,12 @@ describe("composerDraftStore project draft thread mapping", () => { }, retainPreviousDraft: true, }); + store.setPrompt(otherDraftId, "fork tab draft"); + + // Other call sites remap an already-existing draft without passing the + // creation-only retention option. Task identity itself must keep the + // sibling registered and preserve its unsent composer. + store.setProjectDraftThreadId(projectRef, draftId, { threadId }); expect(useComposerDraftStore.getState().getDraftThread(draftId)).toMatchObject({ threadId, @@ -889,6 +895,7 @@ describe("composerDraftStore project draft thread mapping", () => { sourceThreadId: threadId, }, }); + expect(draftByKey(otherDraftId)?.prompt).toBe("fork tab draft"); }); it("keeps composer drafts when the thread is still mapped by another project", () => { diff --git a/apps/web/src/composerDraftStore.ts b/apps/web/src/composerDraftStore.ts index f11238b391..e6185dcba8 100644 --- a/apps/web/src/composerDraftStore.ts +++ b/apps/web/src/composerDraftStore.ts @@ -2363,10 +2363,15 @@ const composerDraftStore = create()( previousThreadKeyForLogicalProject === undefined ? undefined : nextDraftThreadsByThreadKey[previousThreadKeyForLogicalProject]; + const sharesWorkspaceTask = + previousDraftThread?.workspaceTaskId !== undefined && + nextDraftThread.workspaceTaskId !== undefined && + previousDraftThread.workspaceTaskId === nextDraftThread.workspaceTaskId; if ( previousThreadKeyForLogicalProject && previousThreadKeyForLogicalProject !== draftId && options?.retainPreviousDraft !== true && + !sharesWorkspaceTask && !isComposerThreadKeyInUse( nextLogicalProjectDraftThreadKeyByLogicalProjectKey, previousThreadKeyForLogicalProject,