Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

CheckpointRestoreSafety.test.ts

199 lines7.8 KB

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