diff --git a/apps/server/src/orchestration-v2/CheckpointRestoreSafety.test.ts b/apps/server/src/orchestration-v2/CheckpointRestoreSafety.test.ts index 145de122f387a..371a28763eaa1 100644 --- a/apps/server/src/orchestration-v2/CheckpointRestoreSafety.test.ts +++ b/apps/server/src/orchestration-v2/CheckpointRestoreSafety.test.ts @@ -252,3 +252,148 @@ 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 with running tool", isolated: false }, + { name: "native child with provider background task", isolated: false }, + { name: "native child with parent background task", isolated: false }, + { name: "native child with another provider parent task", isolated: true }, + { 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: "unrelated child", isolated: false }, + { name: "running child", isolated: false }, + { name: "held queue", isolated: false }, + { name: "failed child", 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 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, + worktreePath, + activeRunId: name === "running child" ? "child-run" : null, + activityRunStatus: 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: null, + pendingBackgroundTasks: [], + lineage: { + parentThreadId: + name === "completed grandchild" || + name === "native child grandchild with root background task" || + name === "grandchild through fork" || + name === "cyclic ancestry" + ? ancestorId + : name === "unrelated child" + ? ThreadId.make("another-parent") + : parentId, + relationshipToParent: "subagent", + rootThreadId: parentId, + }, + }; + 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, + archivedThreads: [], + }), + 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" }], + }, + ] + : name === "native child with provider background task" + ? [{ status: "idle", pendingBackgroundTasks: [{ taskId: "background" }] }] + : [], + turnItems: + 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 === "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); + }).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 df880e49c6ca5..eeb4a3ff60941 100644 --- a/docs/user/composer.md +++ b/docs/user/composer.md @@ -177,8 +177,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