fix: preserve task tab invariants

This commit is contained in:
Theo Browne committed 2026-07-24 07:54:30 -07:00
1 parent 0ed495a122
commit 9b5063bcd4
4 files changed
+118 -3

No files matched your search

@@ -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");
}),
);
});
+27 -3
View File
@@ -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({
+7
View File
@@ -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", () => {
+5
View File
@@ -2363,10 +2363,15 @@ const composerDraftStore = create<ComposerDraftStoreState>()(
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,