Open History to see the last commit
1import { assert, it, vi } from "@effect/vitest";
2import {
3 CheckpointId,
4 CheckpointScopeId,
5 type OrchestrationV2ThreadProjection,
6 ProviderInstanceId,
7 ProviderSessionId,
8 ProviderThreadId,
9 ThreadId,
10} from "@t3tools/contracts";
11import * as Effect from "effect/Effect";
12import * as Layer from "effect/Layer";
13
14import { CheckpointServiceV2 } from "./CheckpointService.ts";
15import {
16 CheckpointRollbackServiceV2,
17 layer as checkpointRollbackServiceLayer,
18} from "./CheckpointRollbackService.ts";
19import { EventSinkV2 } from "./EventSink.ts";
20import { layer as idAllocatorLayer } from "./IdAllocator.ts";
21import { ProjectionStoreReadError, ProjectionStoreV2 } from "./ProjectionStore.ts";
22import { ProviderSessionManagerV2 } from "./ProviderSessionManager.ts";
23import { RuntimePolicyV2 } from "./RuntimePolicy.ts";
24
25it.effect("rejects a non-ready checkpoint before opening a session or restoring files", () => {
26 const threadId = ThreadId.make("thread:rollback-non-ready");
27 const providerThreadId = ProviderThreadId.make("provider-thread:rollback-non-ready");
28 const providerSessionId = ProviderSessionId.make("provider-session:rollback-non-ready");
29 const checkpointId = CheckpointId.make("checkpoint:rollback-non-ready");
30 const scopeId = CheckpointScopeId.make("checkpoint-scope:rollback-non-ready");
31 const providerInstanceId = ProviderInstanceId.make("provider_rollback_non_ready");
32 const restore = vi.fn(() => Effect.die("checkpoint restore must not run"));
33 const open = vi.fn(() => Effect.die("provider session open must not run"));
34 const resolveRuntimePolicy = vi.fn(() => Effect.die("runtime policy resolution must not run"));
35 const projection = {
36 thread: {
37 activeProviderThreadId: providerThreadId,
38 modelSelection: { instanceId: providerInstanceId, model: "test-model" },
39 },
40 providerThreads: [{ id: providerThreadId, providerSessionId, providerInstanceId }],
41 checkpoints: [{ id: checkpointId, scopeId, status: "stale" }],
42 checkpointScopes: [{ id: scopeId }],
43 } as unknown as OrchestrationV2ThreadProjection;
44 const testLayer = checkpointRollbackServiceLayer.pipe(
45 Layer.provide(
46 Layer.mergeAll(
47 Layer.mock(CheckpointServiceV2)({ restore }),
48 Layer.mock(EventSinkV2)({}),
49 idAllocatorLayer,
50 Layer.mock(ProjectionStoreV2)({
51 getThreadProjection: () => Effect.succeed(projection),
52 }),
53 Layer.mock(ProviderSessionManagerV2)({ open }),
54 Layer.mock(RuntimePolicyV2)({ resolve: resolveRuntimePolicy }),
55 ),
56 ),
57 );
58
59 return Effect.gen(function* () {
60 const service = yield* CheckpointRollbackServiceV2;
61 const error = yield* service
62 .execute({
63 threadId,
64 providerThreadId,
65 checkpointId,
66 scopeId,
67 })
68 .pipe(Effect.flip);
69
70 assert.equal(error.reason, "rollback-target-invalid");
71 assert.equal(
72 error.message,
73 `Rollback target ${checkpointId} for provider thread ${providerThreadId} on thread ${threadId} is incomplete or invalid.`,
74 );
75 assert.equal(error.cause, undefined);
76 assert.equal(resolveRuntimePolicy.mock.calls.length, 0);
77 assert.equal(open.mock.calls.length, 0);
78 assert.equal(restore.mock.calls.length, 0);
79 }).pipe(Effect.provide(testLayer));
80});
81
82it.effect("rejects a rollback when another provider thread became active", () => {
83 const threadId = ThreadId.make("thread:rollback-inactive-provider-thread");
84 const requestedProviderThreadId = ProviderThreadId.make(
85 "provider-thread:rollback-inactive-provider-thread:requested",
86 );
87 const activeProviderThreadId = ProviderThreadId.make(
88 "provider-thread:rollback-inactive-provider-thread:active",
89 );
90 const providerSessionId = ProviderSessionId.make(
91 "provider-session:rollback-inactive-provider-thread",
92 );
93 const checkpointId = CheckpointId.make("checkpoint:rollback-inactive-provider-thread");
94 const scopeId = CheckpointScopeId.make("checkpoint-scope:rollback-inactive-provider-thread");
95 const providerInstanceId = ProviderInstanceId.make("provider_rollback_inactive_provider_thread");
96 const restore = vi.fn(() => Effect.die("checkpoint restore must not run"));
97 const open = vi.fn(() => Effect.die("provider session open must not run"));
98 const resolveRuntimePolicy = vi.fn(() => Effect.die("runtime policy resolution must not run"));
99 const projection = {
100 thread: {
101 activeProviderThreadId,
102 modelSelection: { instanceId: providerInstanceId, model: "test-model" },
103 },
104 providerThreads: [
105 {
106 id: requestedProviderThreadId,
107 providerSessionId,
108 providerInstanceId,
109 },
110 ],
111 checkpoints: [{ id: checkpointId, scopeId, status: "ready" }],
112 checkpointScopes: [{ id: scopeId }],
113 } as unknown as OrchestrationV2ThreadProjection;
114 const testLayer = checkpointRollbackServiceLayer.pipe(
115 Layer.provide(
116 Layer.mergeAll(
117 Layer.mock(CheckpointServiceV2)({ restore }),
118 Layer.mock(EventSinkV2)({}),
119 idAllocatorLayer,
120 Layer.mock(ProjectionStoreV2)({
121 getThreadProjection: () => Effect.succeed(projection),
122 }),
123 Layer.mock(ProviderSessionManagerV2)({ open }),
124 Layer.mock(RuntimePolicyV2)({ resolve: resolveRuntimePolicy }),
125 ),
126 ),
127 );
128
129 return Effect.gen(function* () {
130 const service = yield* CheckpointRollbackServiceV2;
131 const error = yield* service
132 .execute({
133 threadId,
134 providerThreadId: requestedProviderThreadId,
135 checkpointId,
136 scopeId,
137 })
138 .pipe(Effect.flip);
139
140 assert.equal(error.reason, "active-provider-changed");
141 assert.equal(
142 error.message,
143 `Active provider changed before rollback target ${checkpointId} could execute on thread ${threadId}.`,
144 );
145 assert.equal(error.cause, undefined);
146 assert.equal(resolveRuntimePolicy.mock.calls.length, 0);
147 assert.equal(open.mock.calls.length, 0);
148 assert.equal(restore.mock.calls.length, 0);
149 }).pipe(Effect.provide(testLayer));
150});
151
152it.effect("rejects a rollback when provider selection changed before execution", () => {
153 const threadId = ThreadId.make("thread:rollback-provider-selection-changed");
154 const providerThreadId = ProviderThreadId.make(
155 "provider-thread:rollback-provider-selection-changed",
156 );
157 const providerSessionId = ProviderSessionId.make(
158 "provider-session:rollback-provider-selection-changed",
159 );
160 const checkpointId = CheckpointId.make("checkpoint:rollback-provider-selection-changed");
161 const scopeId = CheckpointScopeId.make("checkpoint-scope:rollback-provider-selection-changed");
162 const originalProviderInstanceId = ProviderInstanceId.make(
163 "provider_rollback_provider_selection_changed_original",
164 );
165 const selectedProviderInstanceId = ProviderInstanceId.make(
166 "provider_rollback_provider_selection_changed_selected",
167 );
168 const restore = vi.fn(() => Effect.die("checkpoint restore must not run"));
169 const open = vi.fn(() => Effect.die("provider session open must not run"));
170 const resolveRuntimePolicy = vi.fn(() => Effect.die("runtime policy resolution must not run"));
171 const projection = {
172 thread: {
173 activeProviderThreadId: providerThreadId,
174 modelSelection: { instanceId: selectedProviderInstanceId, model: "test-model" },
175 },
176 providerThreads: [
177 {
178 id: providerThreadId,
179 providerSessionId,
180 providerInstanceId: originalProviderInstanceId,
181 },
182 ],
183 checkpoints: [{ id: checkpointId, scopeId, status: "ready" }],
184 checkpointScopes: [{ id: scopeId }],
185 } as unknown as OrchestrationV2ThreadProjection;
186 const testLayer = checkpointRollbackServiceLayer.pipe(
187 Layer.provide(
188 Layer.mergeAll(
189 Layer.mock(CheckpointServiceV2)({ restore }),
190 Layer.mock(EventSinkV2)({}),
191 idAllocatorLayer,
192 Layer.mock(ProjectionStoreV2)({
193 getThreadProjection: () => Effect.succeed(projection),
194 }),
195 Layer.mock(ProviderSessionManagerV2)({ open }),
196 Layer.mock(RuntimePolicyV2)({ resolve: resolveRuntimePolicy }),
197 ),
198 ),
199 );
200
201 return Effect.gen(function* () {
202 const service = yield* CheckpointRollbackServiceV2;
203 const error = yield* service
204 .execute({
205 threadId,
206 providerThreadId,
207 checkpointId,
208 scopeId,
209 })
210 .pipe(Effect.flip);
211
212 assert.equal(error.reason, "active-provider-changed");
213 assert.equal(
214 error.message,
215 `Active provider changed before rollback target ${checkpointId} could execute on thread ${threadId}.`,
216 );
217 assert.equal(error.cause, undefined);
218 assert.equal(resolveRuntimePolicy.mock.calls.length, 0);
219 assert.equal(open.mock.calls.length, 0);
220 assert.equal(restore.mock.calls.length, 0);
221 }).pipe(Effect.provide(testLayer));
222});
223
224it.effect("reports a missing provider turn as a structured rollback failure", () => {
225 const threadId = ThreadId.make("thread:rollback-provider-turn-unavailable");
226 const providerThreadId = ProviderThreadId.make(
227 "provider-thread:rollback-provider-turn-unavailable",
228 );
229 const providerSessionId = ProviderSessionId.make(
230 "provider-session:rollback-provider-turn-unavailable",
231 );
232 const checkpointId = CheckpointId.make("checkpoint:rollback-provider-turn-unavailable");
233 const scopeId = CheckpointScopeId.make("checkpoint-scope:rollback-provider-turn-unavailable");
234 const providerInstanceId = ProviderInstanceId.make("provider_rollback_provider_turn_unavailable");
235 const restore = vi.fn(() => Effect.die("checkpoint restore must not run"));
236 const projection = {
237 thread: {
238 activeProviderThreadId: providerThreadId,
239 modelSelection: { instanceId: providerInstanceId, model: "test-model" },
240 },
241 providerThreads: [{ id: providerThreadId, providerSessionId, providerInstanceId }],
242 providerSessions: [],
243 checkpoints: [{ id: checkpointId, scopeId, status: "ready", appRunOrdinal: 1 }],
244 checkpointScopes: [{ id: scopeId }],
245 runs: [],
246 attempts: [],
247 providerTurns: [],
248 } as unknown as OrchestrationV2ThreadProjection;
249 const testLayer = checkpointRollbackServiceLayer.pipe(
250 Layer.provide(
251 Layer.mergeAll(
252 Layer.mock(CheckpointServiceV2)({ restore }),
253 Layer.mock(EventSinkV2)({}),
254 idAllocatorLayer,
255 Layer.mock(ProjectionStoreV2)({
256 getThreadProjection: () => Effect.succeed(projection),
257 }),
258 Layer.mock(ProviderSessionManagerV2)({
259 open: () => Effect.succeed({} as never),
260 }),
261 Layer.mock(RuntimePolicyV2)({
262 resolve: () => Effect.succeed({} as never),
263 }),
264 ),
265 ),
266 );
267
268 return Effect.gen(function* () {
269 const service = yield* CheckpointRollbackServiceV2;
270 const error = yield* service
271 .execute({
272 threadId,
273 providerThreadId,
274 checkpointId,
275 scopeId,
276 })
277 .pipe(Effect.flip);
278
279 assert.equal(error.reason, "provider-turn-unavailable");
280 assert.equal(
281 error.message,
282 `Provider turn for rollback target ${checkpointId} is unavailable on provider thread ${providerThreadId}.`,
283 );
284 assert.equal(error.cause, undefined);
285 assert.equal(restore.mock.calls.length, 0);
286 }).pipe(Effect.provide(testLayer));
287});
288
289it.effect("wraps underlying failures with an unexpected-failure reason and cause", () => {
290 const threadId = ThreadId.make("thread:rollback-unexpected-failure");
291 const providerThreadId = ProviderThreadId.make("provider-thread:rollback-unexpected-failure");
292 const checkpointId = CheckpointId.make("checkpoint:rollback-unexpected-failure");
293 const scopeId = CheckpointScopeId.make("checkpoint-scope:rollback-unexpected-failure");
294 const projectionError = new ProjectionStoreReadError({
295 threadId,
296 cause: new Error("database read failed"),
297 });
298 const testLayer = checkpointRollbackServiceLayer.pipe(
299 Layer.provide(
300 Layer.mergeAll(
301 Layer.mock(CheckpointServiceV2)({}),
302 Layer.mock(EventSinkV2)({}),
303 idAllocatorLayer,
304 Layer.mock(ProjectionStoreV2)({
305 getThreadProjection: () => Effect.fail(projectionError),
306 }),
307 Layer.mock(ProviderSessionManagerV2)({}),
308 Layer.mock(RuntimePolicyV2)({}),
309 ),
310 ),
311 );
312
313 return Effect.gen(function* () {
314 const service = yield* CheckpointRollbackServiceV2;
315 const error = yield* service
316 .execute({
317 threadId,
318 providerThreadId,
319 checkpointId,
320 scopeId,
321 })
322 .pipe(Effect.flip);
323
324 assert.equal(error.reason, "unexpected-failure");
325 assert.equal(
326 error.message,
327 `Failed to execute rollback target ${checkpointId} on provider thread ${providerThreadId} for thread ${threadId}.`,
328 );
329 assert.strictEqual(error.cause, projectionError);
330 }).pipe(Effect.provide(testLayer));
331});