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