From 82edb72c638b76ab34d34a8639dfc71cb51d96b3 Mon Sep 17 00:00:00 2001 From: backpack Date: Fri, 6 Nov 2026 17:57:12 +1000 Subject: [PATCH 1/2] fix(server): let parents revert after their subagents complete --- .../CheckpointRestoreSafety.test.ts | 215 +++++++++++++++++- .../CheckpointRestoreSafety.ts | 106 ++++++++- docs/user/composer.md | 6 +- 3 files changed, 317 insertions(+), 10 deletions(-) diff --git a/apps/server/src/orchestration-v2/CheckpointRestoreSafety.test.ts b/apps/server/src/orchestration-v2/CheckpointRestoreSafety.test.ts index 145de122f387a..058a71dd260b0 100644 --- a/apps/server/src/orchestration-v2/CheckpointRestoreSafety.test.ts +++ b/apps/server/src/orchestration-v2/CheckpointRestoreSafety.test.ts @@ -34,6 +34,7 @@ it.effect.each([ "sibling", "shared-provider", "conversation", + "resumed-child", ] as const)("preserves overlapping workspace files, owner=%s", (owner) => Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; @@ -64,13 +65,24 @@ it.effect.each([ id: otherId, projectId, deletedAt: null, - lineage: { parentThreadId: null, relationshipToParent: null, rootThreadId: otherId }, + lineage: { + parentThreadId: owner === "resumed-child" ? threadId : null, + relationshipToParent: owner === "resumed-child" ? "subagent" : null, + rootThreadId: owner === "resumed-child" ? threadId : otherId, + }, + status: "completed", + activeRunId: null, + activityRunStatus: owner === "resumed-child" ? "starting" : null, + pendingRuntimeRequest: null, + pendingBackgroundTasks: [], worktreePath: - owner === "ancestor" || owner === "conversation" - ? parent - : owner === "nested" - ? nested - : sibling, + owner === "resumed-child" + ? cwd + : owner === "ancestor" || owner === "conversation" + ? parent + : owner === "nested" + ? nested + : sibling, }; const projection = { thread: { @@ -252,3 +264,194 @@ it.effect.each([ assert.equal(result, isolated); }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), ); + +it.effect.each([ + { name: "completed child", isolated: true }, + { name: "completed native child", isolated: true }, + { name: "native child still running", isolated: false }, + { name: "native child with running tool", isolated: false }, + { name: "native child with active provider", isolated: false }, + { name: "native child with provider background task", isolated: false }, + { name: "native child with parent background task", isolated: false }, + { name: "native child with unrelated parent task", isolated: false }, + { name: "native child with another provider parent task", isolated: true }, + { name: "native child with unattributed parent task", isolated: false }, + { name: "native child grandchild with root background task", isolated: false }, + { name: "missing task record", isolated: false }, + { name: "task belongs to another child", isolated: false }, + { name: "completed child with pending task", isolated: false }, + { name: "grandchild through fork", isolated: false }, + { name: "completed grandchild", isolated: true }, + { name: "archived completed child", isolated: true }, + { name: "unrelated child", isolated: false }, + { name: "sibling with the same root", isolated: false }, + { name: "fork", isolated: false }, + { name: "side chat", isolated: false }, + { name: "running child", isolated: false }, + { name: "starting child", isolated: false }, + { name: "pending request", isolated: false }, + { name: "background work", isolated: false }, + { name: "held queue", isolated: false }, + { name: "failed child", isolated: false }, + { name: "different worktree", isolated: false }, + { name: "missing ancestor", isolated: false }, + { name: "cyclic ancestry", isolated: false }, +] as const)("parent file restore with $name: $isolated", ({ name, isolated }) => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const worktreePath = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-restore-child-" }); + const nested = path.join(worktreePath, "nested"); + yield* fileSystem.makeDirectory(nested); + const parentId = ThreadId.make("restore-parent"); + const childId = ThreadId.make("restore-child"); + const ancestorId = ThreadId.make("restore-ancestor"); + const child = { + id: childId, + projectId: ProjectId.make("restore-project"), + deletedAt: null, + archivedAt: name === "archived completed child" ? "2026-01-01T00:00:00Z" : null, + worktreePath: name === "different worktree" ? nested : worktreePath, + activeRunId: name === "running child" ? "child-run" : null, + activityRunStatus: name === "starting child" ? "starting" : null, + latestRunId: name.includes("native child") ? null : "child-run", + creationSource: name.includes("native child") ? "provider" : "user", + forkedFrom: { type: "node", nodeId: "child-task" }, + status: name.includes("native child") + ? "idle" + : name === "running child" + ? "running" + : name === "failed child" + ? "failed" + : "completed", + pendingRuntimeRequest: name === "pending request" ? { id: "approval" } : null, + pendingBackgroundTasks: name === "background work" ? [{ taskId: "background" }] : [], + lineage: { + parentThreadId: + name === "completed grandchild" || + name === "native child grandchild with root background task" || + name === "grandchild through fork" || + name === "missing ancestor" || + name === "cyclic ancestry" + ? ancestorId + : name === "unrelated child" || name === "sibling with the same root" + ? ThreadId.make("another-parent") + : parentId, + relationshipToParent: name === "fork" || name === "side chat" ? "fork" : "subagent", + rootThreadId: parentId, + sideChat: name === "side chat", + }, + }; + const ancestor = { + ...child, + id: ancestorId, + forkedFrom: { type: "node", nodeId: "ancestor-task" }, + lineage: { + ...child.lineage, + relationshipToParent: name === "grandchild through fork" ? "fork" : "subagent", + parentThreadId: name === "cyclic ancestry" ? childId : parentId, + }, + }; + const children = + name === "completed grandchild" || + name === "native child grandchild with root background task" || + name === "grandchild through fork" || + name === "cyclic ancestry" + ? [child, ancestor] + : [child]; + const projections = { + getShellSnapshot: () => + Effect.succeed({ + threads: children.filter((child) => child.archivedAt === null), + archivedThreads: children.filter((child) => child.archivedAt !== null), + }), + getCheckpointContext: () => + Effect.succeed({ + checkpointScopes: [], + runs: name === "held queue" ? [{ status: "queued" }] : [], + }), + getThreadProviderContext: () => Effect.succeed({ providerSessions: [] }), + getThreadRecords: ( + _threadId: unknown, + _fields: readonly string[], + filter: { subagentIds?: string[]; turnItemStatuses?: string[] } = {}, + ) => + Effect.succeed({ + providerThreads: + _threadId === parentId && + (name.includes("parent") || + name === "native child grandchild with root background task") && + name.includes("native child") + ? [ + { + providerInstanceId: + name === "native child with another provider parent task" + ? "other-provider" + : "claude", + status: "idle", + pendingBackgroundTasks: [ + { + taskId: "background", + kind: "command", + parentTaskId: + name === "native child with unattributed parent task" + ? undefined + : name === "native child with unrelated parent task" + ? "unrelated-task" + : "native-task", + }, + ], + }, + ] + : name === "native child with active provider" + ? [{ status: "active", pendingBackgroundTasks: [] }] + : name === "native child with provider background task" + ? [{ status: "idle", pendingBackgroundTasks: [{ taskId: "background" }] }] + : [], + turnItems: + name === "side chat" || + (filter.turnItemStatuses !== undefined && name === "native child with running tool") + ? [{ type: "command_execution", status: "running" }] + : [], + subagents: + name === "missing task record" + ? [] + : [ + { + id: filter.subagentIds?.[0], + providerInstanceId: "claude", + nativeTaskRef: { nativeId: "native-task" }, + childThreadId: + name === "task belongs to another child" + ? "another-child" + : filter.subagentIds?.[0] === "ancestor-task" + ? ancestorId + : childId, + origin: name.includes("native child") ? "provider_native" : "app_owned", + status: + name === "native child still running" || + name === "completed child with pending task" + ? "running" + : "completed", + }, + ], + }), + }; + const result = yield* isCheckpointRestoreIsolated( + { id: parentId, worktreePath }, + { cwd: worktreePath }, + { fileSystem, path, projections: projections as never, projects: {} as never }, + ); + assert.equal(result, isolated); + if (isolated) { + // Admission is not the last check: a child resumed before execution must block restore. + child.activityRunStatus = "starting"; + const resumed = yield* isCheckpointRestoreIsolated( + { id: parentId, worktreePath }, + { cwd: worktreePath }, + { fileSystem, path, projections: projections as never, projects: {} as never }, + ); + assert.isFalse(resumed); + } + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), +); diff --git a/apps/server/src/orchestration-v2/CheckpointRestoreSafety.ts b/apps/server/src/orchestration-v2/CheckpointRestoreSafety.ts index b7dc08018a896..4197691af5eda 100644 --- a/apps/server/src/orchestration-v2/CheckpointRestoreSafety.ts +++ b/apps/server/src/orchestration-v2/CheckpointRestoreSafety.ts @@ -1,4 +1,11 @@ -import type { OrchestrationV2AppThread, OrchestrationV2CheckpointScope } from "@t3tools/contracts"; +import { + isLiveSubagentShell, + isProviderNativeSubagentThread, + type OrchestrationV2AppThread, + type OrchestrationV2CheckpointScope, + type OrchestrationV2ProviderThread, + type OrchestrationV2ThreadShell, +} from "@t3tools/contracts"; import * as Effect from "effect/Effect"; import type * as FileSystem from "effect/FileSystem"; import * as Option from "effect/Option"; @@ -6,6 +13,8 @@ import type * as Path from "effect/Path"; import type { ProjectionStoreV2 } from "./ProjectionStore.ts"; import type * as ProjectStore from "./ProjectStore.ts"; +const UNFINISHED_RUN_STATUSES = new Set(["queued", "preparing", "starting", "running", "waiting"]); + export const SHARED_WORKSPACE_RESTORE_MESSAGE = "File restore requires an isolated worktree. This workspace may contain changes from another thread or a side chat. Rewind the conversation without restoring files instead, or delete side chats you no longer need."; @@ -45,8 +54,29 @@ export const isCheckpointRestoreIsolated = Effect.fn("orchestrationV2.isCheckpoi shared = cwd !== worktreeCwd; if (!shared) { const shell = yield* projections.getShellSnapshot(); + const threads = [...shell.threads, ...shell.archivedThreads]; + const threadsById = new Map(threads.map((candidate) => [candidate.id, candidate])); + const subagentAncestors = (candidate: OrchestrationV2ThreadShell) => { + const ancestors: OrchestrationV2AppThread["id"][] = []; + const seen = new Set(); + let current: OrchestrationV2ThreadShell | undefined = candidate; + while (current !== undefined && !seen.has(current.id)) { + seen.add(current.id); + if (current.lineage.relationshipToParent !== "subagent") return undefined; + const parentId: OrchestrationV2AppThread["id"] | null = current.lineage.parentThreadId; + if (parentId === null) return undefined; + ancestors.push(parentId); + if (parentId === thread.id) return ancestors; + current = threadsById.get(parentId); + } + return undefined; + }; + const ancestorProviders = new Map< + OrchestrationV2AppThread["id"], + ReadonlyArray + >(); const checkedPaths = new Set(); - for (const otherThread of [...shell.threads, ...shell.archivedThreads]) { + for (const otherThread of threads) { if (otherThread.id === thread.id || otherThread.deletedAt !== null) continue; // An idle side chat that only answered questions never touched the shared worktree. if ( @@ -64,6 +94,78 @@ export const isCheckpointRestoreIsolated = Effect.fn("orchestrationV2.isCheckpoi if (turnItems.length === 0) continue; } const other = yield* projections.getCheckpointContext(otherThread.id); + const nativeSubagent = isProviderNativeSubagentThread(otherThread); + const ancestors = subagentAncestors(otherThread); + // Native children can be runless; their completion lives on the parent's task record. + if ( + otherThread.worktreePath === worktreePath && + (otherThread.status === "completed" || + (nativeSubagent && otherThread.status === "idle" && other.runs.length === 0)) && + otherThread.activeRunId === null && + !isLiveSubagentShell(otherThread) && + ancestors !== undefined && + !other.runs.some((run) => UNFINISHED_RUN_STATUSES.has(run.status)) && + otherThread.lineage.parentThreadId !== null && + otherThread.forkedFrom?.type === "node" + ) { + const taskNodeId = otherThread.forkedFrom.nodeId; + const { subagents } = yield* projections.getThreadRecords( + otherThread.lineage.parentThreadId, + ["subagents"], + { subagentIds: [taskNodeId] }, + ); + const task = subagents.find( + (candidate) => + candidate.id === taskNodeId && candidate.childThreadId === otherThread.id, + ); + if ( + task?.status === "completed" && + task.origin === (nativeSubagent ? "provider_native" : "app_owned") + ) { + if (!nativeSubagent) continue; + // Runless shells omit background work; inspect the raw records before restoring files. + const { providerThreads, turnItems } = yield* projections.getThreadRecords( + otherThread.id, + ["providerThreads", "turnItems"], + { + turnItemTypes: ["command_execution", "file_change", "dynamic_tool", "subagent"], + turnItemStatuses: ["pending", "running", "waiting"], + }, + ); + if ( + turnItems.length === 0 && + !providerThreads.some( + (providerThread) => + providerThread.status === "active" || + (providerThread.pendingBackgroundTasks?.length ?? 0) > 0, + ) + ) { + // Claude can keep a child's unattributed background commands on an ancestor session. + let pendingAncestorWork = false; + for (const ancestorId of ancestors) { + let providers = ancestorProviders.get(ancestorId); + if (providers === undefined) { + const records = yield* projections.getThreadRecords(ancestorId, [ + "providerThreads", + ]); + providers = records.providerThreads; + ancestorProviders.set(ancestorId, providers); + } + if ( + providers.some( + (provider) => + provider.providerInstanceId === task.providerInstanceId && + (provider.pendingBackgroundTasks?.length ?? 0) > 0, + ) + ) { + pendingAncestorWork = true; + break; + } + } + if (!pendingAncestorWork) continue; + } + } + } const providerContext = yield* projections.getThreadProviderContext(otherThread.id); const paths = [ otherThread.worktreePath, diff --git a/docs/user/composer.md b/docs/user/composer.md index 13f451e2caa73..ffea40f51af0d 100644 --- a/docs/user/composer.md +++ b/docs/user/composer.md @@ -165,8 +165,10 @@ to its checkpoint from before that message. That also undoes edits you made your File restore is only offered for threads running in a worktree, and it is refused when another thread or agent session also uses that directory, a folder inside it, or a folder that contains it, since -restoring would erase their changes. A thread that works in the project directory -rewinds the conversation only. The selected prompt and its attachments return to the composer for editing and +restoring would erase their changes. Completed subagents belonging to the reverting +thread do not block restoring that same worktree, provided they have no queued or +active work, pending requests, or background tasks. A thread that works in the project +directory rewinds the conversation only. The selected prompt and its attachments return to the composer for editing and resending. Any unsent draft stays above the restored prompt. This removes the selected message and later conversation from the active thread From c0858c298b5ad26b52ab1e21e1184d2b2d6792ce Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Wed, 11 Nov 2026 03:30:16 +0000 Subject: [PATCH 2/2] test(server): trim parent-restore cases to distinct guards AI-assisted; verified with the tests and tools listed above. --- .../CheckpointRestoreSafety.test.ts | 98 ++++--------------- 1 file changed, 20 insertions(+), 78 deletions(-) diff --git a/apps/server/src/orchestration-v2/CheckpointRestoreSafety.test.ts b/apps/server/src/orchestration-v2/CheckpointRestoreSafety.test.ts index 058a71dd260b0..371a28763eaa1 100644 --- a/apps/server/src/orchestration-v2/CheckpointRestoreSafety.test.ts +++ b/apps/server/src/orchestration-v2/CheckpointRestoreSafety.test.ts @@ -34,7 +34,6 @@ it.effect.each([ "sibling", "shared-provider", "conversation", - "resumed-child", ] as const)("preserves overlapping workspace files, owner=%s", (owner) => Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; @@ -65,24 +64,13 @@ it.effect.each([ id: otherId, projectId, deletedAt: null, - lineage: { - parentThreadId: owner === "resumed-child" ? threadId : null, - relationshipToParent: owner === "resumed-child" ? "subagent" : null, - rootThreadId: owner === "resumed-child" ? threadId : otherId, - }, - status: "completed", - activeRunId: null, - activityRunStatus: owner === "resumed-child" ? "starting" : null, - pendingRuntimeRequest: null, - pendingBackgroundTasks: [], + lineage: { parentThreadId: null, relationshipToParent: null, rootThreadId: otherId }, worktreePath: - owner === "resumed-child" - ? cwd - : owner === "ancestor" || owner === "conversation" - ? parent - : owner === "nested" - ? nested - : sibling, + owner === "ancestor" || owner === "conversation" + ? parent + : owner === "nested" + ? nested + : sibling, }; const projection = { thread: { @@ -268,41 +256,26 @@ it.effect.each([ it.effect.each([ { name: "completed child", isolated: true }, { name: "completed native child", isolated: true }, - { name: "native child still running", isolated: false }, { name: "native child with running tool", isolated: false }, - { name: "native child with active provider", isolated: false }, { name: "native child with provider background task", isolated: false }, { name: "native child with parent background task", isolated: false }, - { name: "native child with unrelated parent task", isolated: false }, { name: "native child with another provider parent task", isolated: true }, - { name: "native child with unattributed parent task", isolated: false }, { name: "native child grandchild with root background task", isolated: false }, { name: "missing task record", isolated: false }, { name: "task belongs to another child", isolated: false }, { name: "completed child with pending task", isolated: false }, { name: "grandchild through fork", isolated: false }, { name: "completed grandchild", isolated: true }, - { name: "archived completed child", isolated: true }, { name: "unrelated child", isolated: false }, - { name: "sibling with the same root", isolated: false }, - { name: "fork", isolated: false }, - { name: "side chat", isolated: false }, { name: "running child", isolated: false }, - { name: "starting child", isolated: false }, - { name: "pending request", isolated: false }, - { name: "background work", isolated: false }, { name: "held queue", isolated: false }, { name: "failed child", isolated: false }, - { name: "different worktree", isolated: false }, - { name: "missing ancestor", isolated: false }, { name: "cyclic ancestry", isolated: false }, ] as const)("parent file restore with $name: $isolated", ({ name, isolated }) => Effect.gen(function* () { const fileSystem = yield* FileSystem.FileSystem; const path = yield* Path.Path; const worktreePath = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-restore-child-" }); - const nested = path.join(worktreePath, "nested"); - yield* fileSystem.makeDirectory(nested); const parentId = ThreadId.make("restore-parent"); const childId = ThreadId.make("restore-child"); const ancestorId = ThreadId.make("restore-ancestor"); @@ -310,10 +283,9 @@ it.effect.each([ id: childId, projectId: ProjectId.make("restore-project"), deletedAt: null, - archivedAt: name === "archived completed child" ? "2026-01-01T00:00:00Z" : null, - worktreePath: name === "different worktree" ? nested : worktreePath, + worktreePath, activeRunId: name === "running child" ? "child-run" : null, - activityRunStatus: name === "starting child" ? "starting" : null, + activityRunStatus: null, latestRunId: name.includes("native child") ? null : "child-run", creationSource: name.includes("native child") ? "provider" : "user", forkedFrom: { type: "node", nodeId: "child-task" }, @@ -324,22 +296,20 @@ it.effect.each([ : name === "failed child" ? "failed" : "completed", - pendingRuntimeRequest: name === "pending request" ? { id: "approval" } : null, - pendingBackgroundTasks: name === "background work" ? [{ taskId: "background" }] : [], + pendingRuntimeRequest: null, + pendingBackgroundTasks: [], lineage: { parentThreadId: name === "completed grandchild" || name === "native child grandchild with root background task" || name === "grandchild through fork" || - name === "missing ancestor" || name === "cyclic ancestry" ? ancestorId - : name === "unrelated child" || name === "sibling with the same root" + : name === "unrelated child" ? ThreadId.make("another-parent") : parentId, - relationshipToParent: name === "fork" || name === "side chat" ? "fork" : "subagent", + relationshipToParent: "subagent", rootThreadId: parentId, - sideChat: name === "side chat", }, }; const ancestor = { @@ -362,8 +332,8 @@ it.effect.each([ const projections = { getShellSnapshot: () => Effect.succeed({ - threads: children.filter((child) => child.archivedAt === null), - archivedThreads: children.filter((child) => child.archivedAt !== null), + threads: children, + archivedThreads: [], }), getCheckpointContext: () => Effect.succeed({ @@ -389,28 +359,14 @@ it.effect.each([ ? "other-provider" : "claude", status: "idle", - pendingBackgroundTasks: [ - { - taskId: "background", - kind: "command", - parentTaskId: - name === "native child with unattributed parent task" - ? undefined - : name === "native child with unrelated parent task" - ? "unrelated-task" - : "native-task", - }, - ], + pendingBackgroundTasks: [{ taskId: "background", kind: "command" }], }, ] - : name === "native child with active provider" - ? [{ status: "active", pendingBackgroundTasks: [] }] - : name === "native child with provider background task" - ? [{ status: "idle", pendingBackgroundTasks: [{ taskId: "background" }] }] - : [], + : name === "native child with provider background task" + ? [{ status: "idle", pendingBackgroundTasks: [{ taskId: "background" }] }] + : [], turnItems: - name === "side chat" || - (filter.turnItemStatuses !== undefined && name === "native child with running tool") + filter.turnItemStatuses !== undefined && name === "native child with running tool" ? [{ type: "command_execution", status: "running" }] : [], subagents: @@ -428,11 +384,7 @@ it.effect.each([ ? ancestorId : childId, origin: name.includes("native child") ? "provider_native" : "app_owned", - status: - name === "native child still running" || - name === "completed child with pending task" - ? "running" - : "completed", + status: name === "completed child with pending task" ? "running" : "completed", }, ], }), @@ -443,15 +395,5 @@ it.effect.each([ { fileSystem, path, projections: projections as never, projects: {} as never }, ); assert.equal(result, isolated); - if (isolated) { - // Admission is not the last check: a child resumed before execution must block restore. - child.activityRunStatus = "starting"; - const resumed = yield* isCheckpointRestoreIsolated( - { id: parentId, worktreePath }, - { cwd: worktreePath }, - { fileSystem, path, projections: projections as never, projects: {} as never }, - ); - assert.isFalse(resumed); - } }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), );