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 ProjectId,
7 ProviderInstanceId,
8 ProviderSessionId,
9 ProviderThreadId,
10 ThreadId,
11} from "@t3tools/contracts";
12import * as Effect from "effect/Effect";
13import * as FileSystem from "effect/FileSystem";
14import * as Layer from "effect/Layer";
15import * as Option from "effect/Option";
16import * as Path from "effect/Path";
17import * as ServerConfig from "../config.ts";
18import * as ServerSettings from "../serverSettings.ts";
19import * as GitWorkflowService from "../git/GitWorkflowService.ts";
20import * as CheckpointRollbackService from "./CheckpointRollbackService.ts";
21import { isCheckpointRestoreIsolated } from "./CheckpointRestoreSafety.ts";
22import * as ThreadCommandExecutor from "./ThreadCommandExecutor.ts";
23import { CheckpointServiceV2 } from "./CheckpointService.ts";
24import { EventSinkV2 } from "./EventSink.ts";
25import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator";
26import { ProjectionStoreV2, type ProjectionCheckpointRollbackContext } 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 "provider",
35 "sibling",
36 "shared-provider",
37 "conversation",
38] as const)("preserves overlapping workspace files, owner=%s", (owner) =>
39 Effect.gen(function* () {
40 const fs = yield* FileSystem.FileSystem;
41 const path = yield* Path.Path;
42 const parent = yield* fs.makeTempDirectoryScoped({ prefix: "t3-v2-restore-" });
43 const cwd = path.join(parent, "worktree");
44 const nested = path.join(cwd, "nested");
45 const sibling = path.join(parent, "worktree2");
46 yield* fs.makeDirectory(nested, { recursive: true });
47 yield* fs.makeDirectory(sibling);
48 const otherFile = path.join(nested, "other-thread.txt");
49 yield* fs.writeFileString(otherFile, "other thread's uncommitted work");
50 const threadId = ThreadId.make("restore-current");
51 const otherId = ThreadId.make("restore-other");
52 const projectId = ProjectId.make("restore-project");
53 const providerThreadId = ProviderThreadId.make("restore-provider");
54 const providerSessionId = ProviderSessionId.make("restore-session");
55 const instanceId = ProviderInstanceId.make("restore-instance");
56 const checkpointId = CheckpointId.make("restore-checkpoint");
57 const scopeId = CheckpointScopeId.make("restore-scope");
58 const calls: string[] = [];
59 const providerThread = {
60 id: providerThreadId,
61 providerSessionId,
62 providerInstanceId: instanceId,
63 };
64 const otherThread = {
65 id: otherId,
66 projectId,
67 deletedAt: null,
68 worktreePath:
69 owner === "ancestor" || owner === "conversation"
70 ? parent
71 : owner === "nested"
72 ? nested
73 : sibling,
74 };
75 const projection = {
76 thread: {
77 id: threadId,
78 projectId,
79 worktreePath: cwd,
80 activeProviderThreadId: providerThreadId,
81 modelSelection: { instanceId, model: "test" },
82 },
83 providerThread,
84 providerSession: undefined,
85 providerTurns: [],
86 rootNodes: [],
87 targetAttempt: undefined,
88 rolledBackAttemptIds: [],
89 staleCheckpoints: [],
90 checkpoint: { id: checkpointId, scopeId, status: "ready", appRunOrdinal: null },
91 scope: { id: scopeId, cwd },
92 runsToRollback: [{ id: "later-run", ordinal: 1, status: "completed", rootNodeId: null }],
93 } as unknown as ProjectionCheckpointRollbackContext;
94 const layerTest = CheckpointRollbackService.layer.pipe(
95 Layer.provide(
96 Layer.mergeAll(
97 NodeServices.layer,
98 Layer.mock(ServerConfig.ServerConfig)({ worktreesDir: "/synthetic-worktrees" } as never),
99 ServerSettings.layerTest(),
100 ThreadCommandExecutor.layer,
101 IdAllocator.layer,
102 Layer.mock(GitWorkflowService.GitWorkflowService)({}),
103 Layer.mock(ProjectStore.ProjectStoreV2)({
104 get: () => Effect.succeed(Option.some({ workspaceRoot: parent } as never)),
105 }),
106 Layer.mock(ProjectionStoreV2)({
107 getThread: () => Effect.succeed(projection.thread),
108 getCheckpointRollbackContext: () => Effect.succeed(projection),
109 getShellSnapshot: () =>
110 Effect.succeed({
111 schemaVersion: 1,
112 snapshotSequence: 0,
113 threads: [otherThread] as never,
114 archivedThreads: [],
115 }),
116 getCheckpointContext: () =>
117 Effect.succeed({
118 runs: [],
119 checkpoints: [],
120 checkpointScopes: [],
121 }),
122 getThreadProviderContext: () =>
123 Effect.succeed({
124 thread: otherThread as never,
125 providerThreads: [],
126 providerSessions:
127 owner === "provider" || owner === "shared-provider"
128 ? ([
129 {
130 cwd: owner === "shared-provider" ? cwd : nested,
131 status: "running",
132 capabilities: {
133 sessions: {
134 supportsMultipleProviderThreadsPerSession:
135 owner === "shared-provider",
136 },
137 },
138 },
139 ] as never)
140 : [],
141 }),
142 }),
143 Layer.mock(CheckpointServiceV2)({
144 restore: () =>
145 Effect.gen(function* () {
146 calls.push("files");
147 yield* fs.remove(otherFile).pipe(Effect.orDie);
148 }),
149 }),
150 Layer.mock(EventSinkV2)({ write: () => Effect.succeed([]) }),
151 Layer.mock(ProviderSessionManagerV2)({
152 open: () =>
153 Effect.succeed({
154 rollbackThread: () =>
155 Effect.sync(() => {
156 calls.push("provider");
157 return { providerThread };
158 }),
159 } as never),
160 }),
161 Layer.mock(RuntimePolicyV2)({ resolve: () => Effect.succeed({} as never) }),
162 ),
163 ),
164 );
165 const service = yield* CheckpointRollbackService.CheckpointRollbackServiceV2.pipe(
166 Effect.provide(layerTest),
167 );
168 const restoreFiles = owner !== "conversation";
169 const rejected = !["sibling", "shared-provider", "conversation"].includes(owner);
170 if (rejected) {
171 const error = yield* service
172 .execute({ threadId, providerThreadId, checkpointId, scopeId, restoreFiles })
173 .pipe(Effect.flip);
174 assert.equal(error.reason, "shared-workspace");
175 assert.deepEqual(calls, []);
176 assert.equal(yield* fs.readFileString(otherFile), "other thread's uncommitted work");
177 } else {
178 yield* service.execute({ threadId, providerThreadId, checkpointId, scopeId, restoreFiles });
179 assert.deepEqual(calls, restoreFiles ? ["provider", "files"] : ["provider"]);
180 assert.equal(yield* fs.exists(otherFile), !restoreFiles);
181 }
182 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
183);
184
185it.effect("lets rollback recreate a worktree storage cleanup removed", () =>
186 Effect.gen(function* () {
187 const fileSystem = yield* FileSystem.FileSystem;
188 const path = yield* Path.Path;
189 const parent = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-v2-restore-removed-" });
190 const worktreePath = path.join(parent, "worktree");
191 const isolated = yield* isCheckpointRestoreIsolated(
192 { id: ThreadId.make("restore-removed"), worktreePath },
193 { cwd: worktreePath },
194 {
195 fileSystem,
196 path,
197 projections: {
198 getShellSnapshot: () => Effect.succeed({ threads: [], archivedThreads: [] }),
199 } as never,
200 projects: {} as never,
201 },
202 );
203 assert.isTrue(isolated);
204 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
205);