Open History to see the last commit
1import type { OrchestrationV2AppThread, OrchestrationV2CheckpointScope } from "@t3tools/contracts";
2import * as Effect from "effect/Effect";
3import type * as FileSystem from "effect/FileSystem";
4import * as Option from "effect/Option";
5import type * as Path from "effect/Path";
6import type { ProjectionStoreV2 } from "./ProjectionStore.ts";
7import type * as ProjectStore from "./ProjectStore.ts";
8
9export const SHARED_WORKSPACE_RESTORE_MESSAGE =
10 "File restore requires an isolated worktree. This workspace may contain changes from another thread. Rewind the conversation without restoring files instead.";
11
12// Check isolation at admission and rollback; checkpoints restore whole checkouts, which may become shared.
13export const isCheckpointRestoreIsolated = Effect.fn("orchestrationV2.isCheckpointRestoreIsolated")(
14 function* (
15 thread: Pick<OrchestrationV2AppThread, "id" | "worktreePath">,
16 scope: Pick<OrchestrationV2CheckpointScope, "cwd">,
17 dependencies: {
18 readonly fileSystem: FileSystem.FileSystem;
19 readonly projections: ProjectionStoreV2["Service"];
20 readonly projects: ProjectStore.ProjectStoreV2["Service"];
21 readonly path: Path.Path;
22 },
23 ) {
24 const { fileSystem, projections, projects, path } = dependencies;
25 const contains = (parent: string, child: string) => {
26 const relative = path.relative(parent, child);
27 return (
28 relative === "" ||
29 (!path.isAbsolute(relative) && relative !== ".." && !relative.startsWith(`..${path.sep}`))
30 );
31 };
32 const worktreePath = thread.worktreePath;
33 let shared = worktreePath == null;
34 if (!shared && worktreePath !== null) {
35 // Storage cleanup may have removed the worktree; rollback recreates it.
36 const resolve = (target: string) =>
37 fileSystem
38 .realPath(target)
39 .pipe(
40 Effect.catch((error) =>
41 error.reason._tag === "NotFound"
42 ? Effect.succeed(path.resolve(target))
43 : Effect.fail(error),
44 ),
45 );
46 const cwd = yield* resolve(scope.cwd);
47 const worktreeCwd = yield* resolve(worktreePath);
48 shared = cwd !== worktreeCwd;
49 if (!shared) {
50 const shell = yield* projections.getShellSnapshot();
51 const checkedPaths = new Set<string>();
52 for (const otherThread of [...shell.threads, ...shell.archivedThreads]) {
53 if (otherThread.id === thread.id || otherThread.deletedAt !== null) continue;
54 const other = yield* projections.getCheckpointContext(otherThread.id);
55 const providerContext = yield* projections.getThreadProviderContext(otherThread.id);
56 const paths = [
57 otherThread.worktreePath,
58 ...other.checkpointScopes.map((candidate) => candidate.cwd),
59 // Errored sessions can still stream. Shared sessions retain another thread's opening cwd;
60 // turn worktrees and project roots are already covered here.
61 ...providerContext.providerSessions
62 .filter(
63 (session) =>
64 session.status !== "stopped" &&
65 !session.capabilities.sessions.supportsMultipleProviderThreadsPerSession,
66 )
67 .map((session) => session.cwd),
68 ].filter((value): value is string => value !== null);
69 if (otherThread.worktreePath === null) {
70 const project = yield* projects.get(otherThread.projectId, { includeDeleted: true });
71 if (Option.isNone(project)) return false;
72 paths.push(project.value.workspaceRoot);
73 }
74 for (const candidate of paths) {
75 if (checkedPaths.has(candidate)) continue;
76 checkedPaths.add(candidate);
77 const otherCwd = yield* fileSystem
78 .realPath(candidate)
79 .pipe(
80 Effect.catch((error) =>
81 error.reason._tag === "NotFound" ? Effect.succeed(null) : Effect.fail(error),
82 ),
83 );
84 if (otherCwd !== null && (contains(cwd, otherCwd) || contains(otherCwd, cwd))) {
85 shared = true;
86 break;
87 }
88 }
89 if (shared) break;
90 }
91 }
92 }
93 return !shared;
94 },
95);