Open History to see the last commit
1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { assert, it } from "@effect/vitest";
3import {
4 CheckpointId,
5 CheckpointScopeId,
6 type OrchestrationV2ThreadProjection,
7 ProjectId,
8 ProviderInstanceId,
9 ProviderSessionId,
10 ProviderThreadId,
11 ThreadId,
12} from "@t3tools/contracts";
13import * as Effect from "effect/Effect";
14import * as FileSystem from "effect/FileSystem";
15import * as Layer from "effect/Layer";
16import * as Option from "effect/Option";
17import * as Path from "effect/Path";
18import { symlinksSupported } from "@t3tools/shared/testing/symlinks";
19import {
20 CheckpointRollbackServiceV2,
21 layer as rollbackLayer,
22} from "./CheckpointRollbackService.ts";
23import { CheckpointServiceV2 } from "./CheckpointService.ts";
24import { EventSinkV2 } from "./EventSink.ts";
25import { layer as idAllocatorLayer } from "./IdAllocator.ts";
26import { ProjectionStoreV2 } from "./ProjectionStore.ts";
27import * as ProjectStore from "./ProjectStore.ts";
28import { ProviderSessionManagerV2 } from "./ProviderSessionManager.ts";
29import { RuntimePolicyV2 } from "./RuntimePolicy.ts";
30
31it.effect.each([
32 "nested",
33 "ancestor",
34 "archived-nested",
35 ...(symlinksSupported ? (["aliased-nested"] as const) : []),
36 "project",
37 "provider",
38 "scope",
39 "sibling",
40 "stopped-provider",
41 "errored-provider",
42 "shared-provider",
43 "conversation",
44] as const)("preserves overlapping workspace files, owner=%s", (owner) =>
45 Effect.gen(function* () {
46 const fs = yield* FileSystem.FileSystem;
47 const path = yield* Path.Path;
48 const parent = yield* fs.makeTempDirectoryScoped({ prefix: "t3-v2-restore-" });
49 const cwd = path.join(parent, "worktree");
50 const nested = path.join(cwd, "nested");
51 const sibling = path.join(parent, "worktree2");
52 yield* fs.makeDirectory(nested, { recursive: true });
53 yield* fs.makeDirectory(sibling);
54 const otherFile = path.join(nested, "other-thread.txt");
55 yield* fs.writeFileString(otherFile, "other thread's uncommitted work");
56 const alias = path.join(parent, "alias");
57 if (owner === "aliased-nested") yield* fs.symlink(nested, alias);
58 const threadId = ThreadId.make("restore-current");
59 const otherId = ThreadId.make("restore-other");
60 const projectId = ProjectId.make("restore-project");
61 const providerThreadId = ProviderThreadId.make("restore-provider");
62 const providerSessionId = ProviderSessionId.make("restore-session");
63 const instanceId = ProviderInstanceId.make("restore-instance");
64 const checkpointId = CheckpointId.make("restore-checkpoint");
65 const scopeId = CheckpointScopeId.make("restore-scope");
66 const calls: string[] = [];
67 const providerThread = {
68 id: providerThreadId,
69 providerSessionId,
70 providerInstanceId: instanceId,
71 };
72 const otherThread = {
73 id: otherId,
74 projectId,
75 deletedAt: null,
76 worktreePath:
77 owner === "project"
78 ? null
79 : owner === "ancestor" || owner === "conversation"
80 ? parent
81 : owner === "nested" || owner === "archived-nested"
82 ? nested
83 : owner === "aliased-nested"
84 ? alias
85 : sibling,
86 };
87 const projection = {
88 thread: {
89 id: threadId,
90 projectId,
91 worktreePath: cwd,
92 activeProviderThreadId: providerThreadId,
93 modelSelection: { instanceId, model: "test" },
94 },
95 providerThreads: [providerThread],
96 providerSessions: [],
97 providerTurns: [],
98 nodes: [],
99 attempts: [],
100 checkpoints: [{ id: checkpointId, scopeId, status: "ready", appRunOrdinal: null }],
101 checkpointScopes: [{ id: scopeId, cwd }],
102 runs: [{ id: "later-run", ordinal: 1, status: "completed", rootNodeId: null }],
103 } as unknown as OrchestrationV2ThreadProjection;
104 const testLayer = rollbackLayer.pipe(
105 Layer.provide(
106 Layer.mergeAll(
107 NodeServices.layer,
108 idAllocatorLayer,
109 Layer.mock(ProjectStore.ProjectStoreV2)({
110 get: () => Effect.succeed(Option.some({ workspaceRoot: parent } as never)),
111 }),
112 Layer.mock(ProjectionStoreV2)({
113 getThreadRecords: () => Effect.succeed(projection),
114 getShellSnapshot: () =>
115 Effect.succeed({
116 schemaVersion: 1,
117 snapshotSequence: 0,
118 threads: owner === "archived-nested" ? [] : ([otherThread] as never),
119 archivedThreads: owner === "archived-nested" ? ([otherThread] as never) : [],
120 }),
121 getCheckpointContext: () =>
122 Effect.succeed({
123 runs: [],
124 checkpoints: [],
125 checkpointScopes: owner === "scope" ? ([{ cwd: nested }] as never) : [],
126 }),
127 getThreadProviderContext: () =>
128 Effect.succeed({
129 thread: otherThread as never,
130 providerThreads: [],
131 providerSessions: [
132 "provider",
133 "stopped-provider",
134 "errored-provider",
135 "shared-provider",
136 ].includes(owner)
137 ? ([
138 {
139 // One session shared by every thread keeps the cwd of
140 // the thread that opened it, here this thread's.
141 cwd: owner === "shared-provider" ? cwd : nested,
142 status:
143 owner === "provider" || owner === "shared-provider"
144 ? "running"
145 : owner === "errored-provider"
146 ? "error"
147 : "stopped",
148 capabilities: {
149 sessions: {
150 supportsMultipleProviderThreadsPerSession: owner === "shared-provider",
151 },
152 },
153 },
154 ] as never)
155 : [],
156 }),
157 }),
158 Layer.mock(CheckpointServiceV2)({
159 restore: () =>
160 Effect.gen(function* () {
161 calls.push("files");
162 yield* fs.remove(otherFile).pipe(Effect.orDie);
163 }),
164 }),
165 Layer.mock(EventSinkV2)({ write: () => Effect.succeed([]) }),
166 Layer.mock(ProviderSessionManagerV2)({
167 open: () =>
168 Effect.succeed({
169 rollbackThread: () =>
170 Effect.sync(() => {
171 calls.push("provider");
172 return { providerThread };
173 }),
174 } as never),
175 }),
176 Layer.mock(RuntimePolicyV2)({ resolve: () => Effect.succeed({} as never) }),
177 ),
178 ),
179 );
180 const service = yield* CheckpointRollbackServiceV2.pipe(Effect.provide(testLayer));
181 const restoreFiles = owner !== "conversation";
182 const rejected = !["sibling", "stopped-provider", "shared-provider", "conversation"].includes(
183 owner,
184 );
185 if (rejected) {
186 const error = yield* service
187 .execute({ threadId, providerThreadId, checkpointId, scopeId, restoreFiles })
188 .pipe(Effect.flip);
189 assert.equal(error.reason, "shared-workspace");
190 assert.deepEqual(calls, []);
191 assert.equal(yield* fs.readFileString(otherFile), "other thread's uncommitted work");
192 } else {
193 yield* service.execute({ threadId, providerThreadId, checkpointId, scopeId, restoreFiles });
194 assert.deepEqual(calls, restoreFiles ? ["provider", "files"] : ["provider"]);
195 assert.equal(yield* fs.exists(otherFile), !restoreFiles);
196 }
197 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
198);