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

CheckpointCaptureService.test.ts

737 lines26.8 KB

1import { assert, it } from "@effect/vitest";
2import {
3 CheckpointId,
4 CheckpointRef,
5 CheckpointScopeId,
6 EventId,
7 MessageId,
8 NodeId,
9 type OrchestrationV2DomainEvent,
10 type OrchestrationV2Run,
11 ProjectId,
12 ProviderDriverKind,
13 ProviderInstanceId,
14 ProviderThreadId,
15 RunId,
16 ThreadId,
17} from "@t3tools/contracts";
18import * as DateTime from "effect/DateTime";
19import * as Effect from "effect/Effect";
20import * as Layer from "effect/Layer";
21import * as Ref from "effect/Ref";
22import * as SqlClient from "effect/unstable/sql/SqlClient";
23​
24import { SqlitePersistenceMemory } from "../persistence/Layers/Sqlite.ts";
25import * as CheckpointStore from "../checkpointing/CheckpointStore.ts";
26import { VcsProcessTimeoutError } from "@t3tools/contracts";
27import * as CheckpointService from "./CheckpointService.ts";
28import * as CheckpointCaptureService from "./CheckpointCaptureService.ts";
29import * as EventSink from "./EventSink.ts";
30import * as IdAllocator from "./IdAllocator.ts";
31import * as ProjectionStore from "./ProjectionStore.ts";
32​
33const ProjectionStoreTestLayer = Layer.mergeAll(
34 ProjectionStore.layer.pipe(Layer.provideMerge(SqlitePersistenceMemory)),
35 SqlitePersistenceMemory,
36);
37​
38const threadId = ThreadId.make("thread:checkpoint-capture-delegated");
39const projectId = ProjectId.make("project:checkpoint-capture-delegated");
40const runId = RunId.make("run:checkpoint-capture-delegated");
41const scopeId = CheckpointScopeId.make("scope:checkpoint-capture-delegated");
42const rootNodeId = NodeId.make("node:checkpoint-capture-delegated-root");
43const taskId = NodeId.make("node:checkpoint-capture-task");
44const deliveryMessageId = MessageId.make("message:checkpoint-capture-delivery");
45const providerThreadId = ProviderThreadId.make("provider-thread:checkpoint-capture-delegated");
46const providerInstanceId = ProviderInstanceId.make("codex");
47const driver = ProviderDriverKind.make("codex");
48const modelSelection = {
49 instanceId: providerInstanceId,
50 model: "gpt-5.4",
51} as const;
52​
53it.layer(ProjectionStoreTestLayer)("CheckpointCaptureServiceV2", (it) => {
54 it.effect.each([false, true])(
55 "captures without decoding history or losing newer delegated completion, ref lookup fails=%s",
56 (refLookupFails) =>
57 Effect.gen(function* () {
58 const projectionStore = yield* ProjectionStore.ProjectionStoreV2;
59 const now = yield* DateTime.now;
60 const later = DateTime.add(now, { seconds: 1 });
61​
62 const staleDelegatedCompletion = {
63 disposition: "open" as const,
64 nextGeneration: 1,
65 delivery: null,
66 };
67 const newerCohort = {
68 disposition: "open" as const,
69 nextGeneration: 2,
70 delivery: {
71 generation: 1,
72 messageId: deliveryMessageId,
73 taskIds: [taskId],
74 },
75 };
76 const staleRun: OrchestrationV2Run = {
77 id: runId,
78 threadId,
79 ordinal: 1,
80 providerInstanceId,
81 modelSelection,
82 providerThreadId,
83 userMessageId: MessageId.make("message:checkpoint-capture-user"),
84 rootNodeId,
85 activeAttemptId: null,
86 status: "waiting",
87 requestedAt: now,
88 startedAt: now,
89 completedAt: null,
90 checkpointId: null,
91 contextHandoffId: null,
92 // Snapshot taken before a concurrent cohort advanced during capture work.
93 delegatedCompletion: staleDelegatedCompletion,
94 };
95 const rootNode = {
96 id: rootNodeId,
97 threadId,
98 runId,
99 parentNodeId: null,
100 rootNodeId,
101 kind: "root_turn" as const,
102 status: "waiting" as const,
103 countsForRun: true,
104 providerThreadId,
105 providerTurnId: null,
106 nativeItemRef: null,
107 runtimeRequestId: null,
108 checkpointScopeId: scopeId,
109 startedAt: now,
110 completedAt: null,
111 };
112 const scope = {
113 id: scopeId,
114 threadId,
115 runId,
116 nodeId: rootNodeId,
117 parentScopeId: null,
118 providerThreadId,
119 kind: "root_run" as const,
120 ordinalWithinParent: 0,
121 advancesAppRunCount: true,
122 cwd: "/repo",
123 createdAt: now,
124 };
125 const providerThread = {
126 id: providerThreadId,
127 driver,
128 providerInstanceId,
129 providerSessionId: null,
130 appThreadId: threadId,
131 ownerNodeId: rootNodeId,
132 nativeThreadRef: null,
133 nativeConversationHeadRef: null,
134 status: "idle" as const,
135 firstRunOrdinal: 1,
136 lastRunOrdinal: 1,
137 handoffIds: [],
138 forkedFrom: null,
139 createdAt: now,
140 updatedAt: now,
141 };
142 const readyBaseline = {
143 id: CheckpointId.make("checkpoint:baseline-0"),
144 threadId,
145 scopeId,
146 runId: null,
147 nodeId: rootNodeId,
148 parentCheckpointId: null,
149 ordinalWithinScope: 0,
150 appRunOrdinal: null,
151 ref: CheckpointRef.make("checkpoint-ref:baseline-0"),
152 status: "ready" as const,
153 files: [],
154 capturedAt: now,
155 };
156 const captured = {
157 id: CheckpointId.make("checkpoint:captured-1"),
158 threadId,
159 scopeId,
160 runId,
161 nodeId: rootNodeId,
162 parentCheckpointId: readyBaseline.id,
163 ordinalWithinScope: 1,
164 appRunOrdinal: 1,
165 ref: CheckpointRef.make("checkpoint-ref:captured-1"),
166 status: "ready" as const,
167 files: [],
168 capturedAt: now,
169 };
170​
171 yield* projectionStore.apply({
172 id: EventId.make("event:checkpoint-capture:thread"),
173 type: "thread.created",
174 threadId,
175 occurredAt: now,
176 payload: {
177 createdBy: "user",
178 creationSource: "web",
179 id: threadId,
180 projectId,
181 title: "Checkpoint capture delegated completion",
182 providerInstanceId,
183 modelSelection,
184 runtimeMode: "full-access",
185 interactionMode: "default",
186 branch: null,
187 worktreePath: null,
188 activeProviderThreadId: null,
189 lineage: {
190 parentThreadId: null,
191 relationshipToParent: null,
192 rootThreadId: threadId,
193 },
194 forkedFrom: null,
195 createdAt: now,
196 updatedAt: now,
197 archivedAt: null,
198 settledOverride: null,
199 settledAt: null,
200 lastVisitedAt: null,
201 deletedAt: null,
202 },
203 });
204 yield* projectionStore.apply({
205 id: EventId.make("event:checkpoint-capture:run-stale"),
206 type: "run.updated",
207 threadId,
208 runId,
209 nodeId: rootNodeId,
210 providerInstanceId,
211 occurredAt: now,
212 payload: staleRun,
213 });
214 yield* projectionStore.apply({
215 id: EventId.make("event:checkpoint-capture:node"),
216 type: "node.updated",
217 threadId,
218 runId,
219 nodeId: rootNodeId,
220 providerInstanceId,
221 occurredAt: now,
222 payload: rootNode,
223 });
224 yield* projectionStore.apply({
225 id: EventId.make("event:checkpoint-capture:provider-thread"),
226 type: "provider-thread.updated",
227 threadId,
228 nodeId: rootNodeId,
229 driver,
230 providerInstanceId,
231 occurredAt: now,
232 payload: providerThread,
233 });
234 yield* projectionStore.apply({
235 id: EventId.make("event:checkpoint-capture:scope"),
236 type: "checkpoint-scope.created",
237 threadId,
238 runId,
239 nodeId: rootNodeId,
240 occurredAt: now,
241 payload: scope,
242 });
243 yield* projectionStore.apply({
244 id: EventId.make("event:checkpoint-capture:baseline"),
245 type: "checkpoint.captured",
246 threadId,
247 nodeId: rootNodeId,
248 driver,
249 providerInstanceId,
250 occurredAt: now,
251 payload: readyBaseline,
252 });
253 const sql = yield* SqlClient.SqlClient;
254 yield* sql`INSERT OR REPLACE INTO orchestration_v2_projection_turn_items
255 (turn_item_id, thread_id, run_id, node_id, provider_thread_id, provider_turn_id,
256 parent_item_id, ordinal, type, status, updated_at, payload_json)
257 VALUES ('obsolete-history', ${threadId}, ${runId}, ${rootNodeId}, NULL, NULL,
258 NULL, 1, 'assistant_message', 'completed', ${DateTime.formatIso(now)}, '{"obsolete":true}')`;
259 yield* sql`UPDATE orchestration_v2_projection_checkpoints
260 SET payload_json = json_set(payload_json, '$.files', 'obsolete file summary')
261 WHERE checkpoint_id = ${readyBaseline.id}`;
262 assert.equal(
263 (yield* Effect.exit(projectionStore.getThreadProjection(threadId)))._tag,
264 "Failure",
265 );
266​
267 const committed = yield* Ref.make<ReadonlyArray<OrchestrationV2DomainEvent>>([]);
268 const captureLayer = CheckpointCaptureService.layer.pipe(
269 Layer.provide(
270 Layer.mergeAll(
271 IdAllocator.layer,
272 refLookupFails
273 ? CheckpointService.layer.pipe(
274 Layer.provide(
275 Layer.mergeAll(
276 IdAllocator.layer,
277 Layer.mock(CheckpointStore.CheckpointStore)({
278 isGitRepository: () => Effect.succeed(true),
279 captureCheckpoint: () => Effect.void,
280 hasCheckpointRef: () =>
281 Effect.fail(
282 new VcsProcessTimeoutError({
283 operation: "test.hasCheckpointRef",
284 command: "git",
285 cwd: "/repo",
286 timeoutMs: 30000,
287 }),
288 ),
289 }),
290 ),
291 ),
292 )
293 : Layer.mock(CheckpointService.CheckpointServiceV2)({
294 materializeBaselineCheckpoint: () =>
295 Effect.die(
296 "baseline materialization must be skipped when ordinal 0 is ready",
297 ),
298 capture: () => Effect.succeed(captured),
299 }),
300 Layer.mock(EventSink.EventSinkV2)({
301 commitCommand: (input) =>
302 Ref.set(committed, input.events).pipe(
303 Effect.as({
304 commandId: input.commandId,
305 committed: true,
306 sequence: 1,
307 events: input.events,
308 effects: [],
309 } as never),
310 ),
311 }),
312 ),
313 ),
314 );
315​
316 yield* Effect.gen(function* () {
317 const service = yield* CheckpointCaptureService.CheckpointCaptureServiceV2;
318 const incomplete = yield* service
319 .execute({ threadId, runId, scopeId: CheckpointScopeId.make("missing-scope") })
320 .pipe(Effect.flip);
321 assert.instanceOf(incomplete, CheckpointCaptureService.CheckpointCaptureExecutionError);
322 // Capture reads the waiting run while the projection still holds the stale cohort.
323 yield* service.execute({ threadId, runId, scopeId });
324​
325 const events = yield* Ref.get(committed);
326 const runUpdated = events.find((event) => event.type === "run.updated");
327 assert.isDefined(runUpdated);
328 if (runUpdated?.type !== "run.updated") {
329 return;
330 }
331 assert.equal(runUpdated.payload.status, "completed");
332 const capturedEvent = events.find((event) => event.type === "checkpoint.captured");
333 assert.equal(
334 runUpdated.payload.checkpointId,
335 refLookupFails ? capturedEvent?.payload.id : captured.id,
336 );
337 if (refLookupFails && capturedEvent?.type === "checkpoint.captured") {
338 assert.equal(capturedEvent.payload.status, "ready");
339 assert.deepEqual(capturedEvent.payload.files, []);
340 }
341 assert.isUndefined(
342 runUpdated.payload.delegatedCompletion,
343 "checkpoint capture must omit delegatedCompletion so ProjectionStore can keep a newer cohort",
344 );
345​
346 // Race: a newer cohort lands on the projection after capture read the stale
347 // waiting run and before the capture command's run.updated is applied.
348 yield* projectionStore.apply({
349 id: EventId.make("event:checkpoint-capture:newer-cohort"),
350 type: "run.updated",
351 threadId,
352 runId,
353 nodeId: rootNodeId,
354 providerInstanceId,
355 occurredAt: later,
356 payload: {
357 ...staleRun,
358 delegatedCompletion: newerCohort,
359 },
360 });
361​
362 // Apply the real capture-emitted run.updated through ProjectionStore.
363 yield* projectionStore.apply(runUpdated);
364​
365 const projectedRun = (yield* projectionStore.getCheckpointCaptureContext(threadId, {
366 runId,
367 scopeId,
368 })).run;
369 assert.isDefined(projectedRun);
370 assert.equal(projectedRun?.status, "completed");
371 assert.equal(projectedRun?.checkpointId, runUpdated.payload.checkpointId);
372 assert.deepEqual(projectedRun?.delegatedCompletion, newerCohort);
373 assert.equal(projectedRun?.delegatedCompletion?.delivery?.messageId, deliveryMessageId);
374 assert.deepEqual(projectedRun?.delegatedCompletion?.delivery?.taskIds, [taskId]);
375 // The persisted completion is the at-least-once capture receipt.
376 yield* Ref.set(committed, []);
377 yield* service.execute({ threadId, runId, scopeId });
378 assert.deepEqual(yield* Ref.get(committed), []);
379 }).pipe(Effect.provide(captureLayer));
380 }),
381 );
382​
383 // Capture and rollback share the thread's effect lane, so a rollback can only
384 // commit before a capture runs: while it waits out a retry, or when a restart
385 // requeues it behind a pending rollback. It must not revive the run.
386 it.effect("does not capture a stopped run that a rollback already discarded", () =>
387 Effect.gen(function* () {
388 const projectionStore = yield* ProjectionStore.ProjectionStoreV2;
389 const now = yield* DateTime.now;
390 const stoppedThreadId = ThreadId.make("thread:checkpoint-capture-rolled-back");
391 const stoppedRunId = RunId.make("run:checkpoint-capture-rolled-back");
392 yield* projectionStore.apply({
393 id: EventId.make("event:checkpoint-capture-rolled-back:thread"),
394 type: "thread.created",
395 threadId: stoppedThreadId,
396 occurredAt: now,
397 payload: {
398 createdBy: "user",
399 creationSource: "web",
400 id: stoppedThreadId,
401 projectId,
402 title: "Checkpoint capture after rollback",
403 providerInstanceId,
404 modelSelection,
405 runtimeMode: "full-access",
406 interactionMode: "default",
407 branch: null,
408 worktreePath: null,
409 activeProviderThreadId: null,
410 lineage: {
411 parentThreadId: null,
412 relationshipToParent: null,
413 rootThreadId: stoppedThreadId,
414 },
415 forkedFrom: null,
416 createdAt: now,
417 updatedAt: now,
418 archivedAt: null,
419 settledOverride: null,
420 settledAt: null,
421 lastVisitedAt: null,
422 deletedAt: null,
423 },
424 });
425 const stoppedRun: OrchestrationV2Run = {
426 id: stoppedRunId,
427 threadId: stoppedThreadId,
428 ordinal: 2,
429 providerInstanceId,
430 modelSelection,
431 providerThreadId,
432 userMessageId: MessageId.make("message:checkpoint-capture-rolled-back"),
433 rootNodeId,
434 activeAttemptId: null,
435 status: "interrupted",
436 requestedAt: now,
437 startedAt: now,
438 completedAt: now,
439 checkpointId: null,
440 contextHandoffId: null,
441 };
442 // The rollback's write, which landed before the capture ran.
443 yield* projectionStore.apply({
444 id: EventId.make("event:checkpoint-capture-rolled-back:run"),
445 type: "run.updated",
446 threadId: stoppedThreadId,
447 runId: stoppedRunId,
448 providerInstanceId,
449 occurredAt: now,
450 payload: { ...stoppedRun, status: "rolled_back" },
451 });
452​
453 const committed = yield* Ref.make<ReadonlyArray<OrchestrationV2DomainEvent>>([]);
454 const captureLayer = CheckpointCaptureService.layer.pipe(
455 Layer.provide(
456 Layer.mergeAll(
457 IdAllocator.layer,
458 Layer.mock(CheckpointService.CheckpointServiceV2)({
459 materializeBaselineCheckpoint: () => Effect.die("a discarded run has no baseline"),
460 capture: () => Effect.die("a discarded run must not be captured"),
461 }),
462 Layer.mock(EventSink.EventSinkV2)({
463 commitCommand: (input) =>
464 Ref.set(committed, input.events).pipe(Effect.as({ committed: true } as never)),
465 }),
466 ),
467 ),
468 );
469​
470 // Settles without retrying, and commits nothing that would revive the run.
471 yield* CheckpointCaptureService.CheckpointCaptureServiceV2.pipe(
472 Effect.flatMap((service) =>
473 service.execute({ threadId: stoppedThreadId, runId: stoppedRunId, scopeId }),
474 ),
475 Effect.provide(captureLayer),
476 );
477​
478 assert.deepEqual(yield* Ref.get(committed), []);
479 const projected = yield* projectionStore.getCheckpointCaptureContext(stoppedThreadId, {
480 runId: stoppedRunId,
481 scopeId,
482 });
483 assert.equal(projected.run?.status, "rolled_back");
484 assert.isNull(projected.run?.checkpointId ?? null);
485 }),
486 );
487​
488 // A cancelled run's checkpoint is the rollback point for the message after
489 // it, so capture records it without reporting the run as completed.
490 it.effect("records the checkpoint of a cancelled run and keeps it cancelled", () =>
491 Effect.gen(function* () {
492 const projectionStore = yield* ProjectionStore.ProjectionStoreV2;
493 const now = yield* DateTime.now;
494 const cancelledAt = DateTime.add(now, { seconds: 1 });
495 const cancelledThreadId = ThreadId.make("thread:checkpoint-capture-cancelled");
496 const cancelledRunId = RunId.make("run:checkpoint-capture-cancelled");
497 const cancelledScopeId = CheckpointScopeId.make("scope:checkpoint-capture-cancelled");
498 const cancelledRootNodeId = NodeId.make("node:checkpoint-capture-cancelled-root");
499 const cancelledProviderThreadId = ProviderThreadId.make(
500 "provider-thread:checkpoint-capture-cancelled",
501 );
502 yield* projectionStore.apply({
503 id: EventId.make("event:checkpoint-capture-cancelled:thread"),
504 type: "thread.created",
505 threadId: cancelledThreadId,
506 occurredAt: now,
507 payload: {
508 createdBy: "user",
509 creationSource: "web",
510 id: cancelledThreadId,
511 projectId,
512 title: "Checkpoint capture after cancel",
513 providerInstanceId,
514 modelSelection,
515 runtimeMode: "full-access",
516 interactionMode: "default",
517 branch: null,
518 worktreePath: null,
519 activeProviderThreadId: null,
520 lineage: {
521 parentThreadId: null,
522 relationshipToParent: null,
523 rootThreadId: cancelledThreadId,
524 },
525 forkedFrom: null,
526 createdAt: now,
527 updatedAt: now,
528 archivedAt: null,
529 settledOverride: null,
530 settledAt: null,
531 lastVisitedAt: null,
532 deletedAt: null,
533 },
534 });
535 const runningRun: OrchestrationV2Run = {
536 id: cancelledRunId,
537 threadId: cancelledThreadId,
538 ordinal: 1,
539 providerInstanceId,
540 modelSelection,
541 providerThreadId: cancelledProviderThreadId,
542 userMessageId: MessageId.make("message:checkpoint-capture-cancelled"),
543 rootNodeId: cancelledRootNodeId,
544 activeAttemptId: null,
545 status: "running",
546 requestedAt: now,
547 startedAt: now,
548 completedAt: null,
549 checkpointId: null,
550 contextHandoffId: null,
551 };
552 const runningRootNode = {
553 id: cancelledRootNodeId,
554 threadId: cancelledThreadId,
555 runId: cancelledRunId,
556 parentNodeId: null,
557 rootNodeId: cancelledRootNodeId,
558 kind: "root_turn" as const,
559 status: "running" as const,
560 countsForRun: true,
561 providerThreadId: cancelledProviderThreadId,
562 providerTurnId: null,
563 nativeItemRef: null,
564 runtimeRequestId: null,
565 checkpointScopeId: cancelledScopeId,
566 startedAt: now,
567 completedAt: null,
568 };
569 const scope = {
570 id: cancelledScopeId,
571 threadId: cancelledThreadId,
572 runId: cancelledRunId,
573 nodeId: cancelledRootNodeId,
574 parentScopeId: null,
575 providerThreadId: cancelledProviderThreadId,
576 kind: "root_run" as const,
577 ordinalWithinParent: 0,
578 advancesAppRunCount: true,
579 cwd: "/repo",
580 createdAt: now,
581 };
582 yield* projectionStore.apply({
583 id: EventId.make("event:checkpoint-capture-cancelled:provider-thread"),
584 type: "provider-thread.updated",
585 threadId: cancelledThreadId,
586 nodeId: cancelledRootNodeId,
587 driver,
588 providerInstanceId,
589 occurredAt: now,
590 payload: {
591 id: cancelledProviderThreadId,
592 driver,
593 providerInstanceId,
594 providerSessionId: null,
595 appThreadId: cancelledThreadId,
596 ownerNodeId: cancelledRootNodeId,
597 nativeThreadRef: null,
598 nativeConversationHeadRef: null,
599 status: "idle",
600 firstRunOrdinal: 1,
601 lastRunOrdinal: 1,
602 handoffIds: [],
603 forkedFrom: null,
604 createdAt: now,
605 updatedAt: now,
606 },
607 });
608 yield* projectionStore.apply({
609 id: EventId.make("event:checkpoint-capture-cancelled:scope"),
610 type: "checkpoint-scope.created",
611 threadId: cancelledThreadId,
612 runId: cancelledRunId,
613 nodeId: cancelledRootNodeId,
614 occurredAt: now,
615 payload: scope,
616 });
617 yield* projectionStore.apply({
618 id: EventId.make("event:checkpoint-capture-cancelled:baseline"),
619 type: "checkpoint.captured",
620 threadId: cancelledThreadId,
621 nodeId: cancelledRootNodeId,
622 driver,
623 providerInstanceId,
624 occurredAt: now,
625 payload: {
626 id: CheckpointId.make("checkpoint:cancelled-baseline-0"),
627 threadId: cancelledThreadId,
628 scopeId: cancelledScopeId,
629 runId: null,
630 nodeId: cancelledRootNodeId,
631 parentCheckpointId: null,
632 ordinalWithinScope: 0,
633 appRunOrdinal: null,
634 ref: CheckpointRef.make("checkpoint-ref:cancelled-baseline-0"),
635 status: "ready",
636 files: [],
637 capturedAt: now,
638 },
639 });
640 // The turn runs, then finalizes as cancelled the way RunExecutionService
641 // writes it, which also enqueues this capture.
642 for (const [status, completedAt] of [
643 ["running", null],
644 ["cancelled", cancelledAt],
645 ] as const) {
646 yield* projectionStore.apply({
647 id: EventId.make(`event:checkpoint-capture-cancelled:run-${status}`),
648 type: "run.updated",
649 threadId: cancelledThreadId,
650 runId: cancelledRunId,
651 nodeId: cancelledRootNodeId,
652 providerInstanceId,
653 occurredAt: completedAt ?? now,
654 payload: { ...runningRun, status, completedAt },
655 });
656 yield* projectionStore.apply({
657 id: EventId.make(`event:checkpoint-capture-cancelled:node-${status}`),
658 type: "node.updated",
659 threadId: cancelledThreadId,
660 runId: cancelledRunId,
661 nodeId: cancelledRootNodeId,
662 providerInstanceId,
663 occurredAt: completedAt ?? now,
664 payload: { ...runningRootNode, status, completedAt },
665 });
666 }
667​
668 const captured = {
669 id: CheckpointId.make("checkpoint:cancelled-captured-1"),
670 threadId: cancelledThreadId,
671 scopeId: cancelledScopeId,
672 runId: cancelledRunId,
673 nodeId: cancelledRootNodeId,
674 parentCheckpointId: CheckpointId.make("checkpoint:cancelled-baseline-0"),
675 ordinalWithinScope: 1,
676 appRunOrdinal: 1,
677 ref: CheckpointRef.make("checkpoint-ref:cancelled-captured-1"),
678 status: "ready" as const,
679 files: [],
680 capturedAt: cancelledAt,
681 };
682 const commits = yield* Ref.make(0);
683 const captureLayer = CheckpointCaptureService.layer.pipe(
684 Layer.provide(
685 Layer.mergeAll(
686 IdAllocator.layer,
687 Layer.mock(CheckpointService.CheckpointServiceV2)({
688 materializeBaselineCheckpoint: () =>
689 Effect.die("baseline materialization must be skipped when ordinal 0 is ready"),
690 capture: () => Effect.succeed(captured),
691 }),
692 // Commit straight into the projection so the test reads what a
693 // client would see after the capture lands.
694 Layer.mock(EventSink.EventSinkV2)({
695 commitCommand: (input) =>
696 Effect.forEach(input.events, (event) => projectionStore.apply(event)).pipe(
697 Effect.andThen(Ref.update(commits, (count) => count + 1)),
698 Effect.as({ committed: true } as never),
699 Effect.orDie,
700 ),
701 }),
702 ),
703 ),
704 );
705​
706 yield* Effect.gen(function* () {
707 const service = yield* CheckpointCaptureService.CheckpointCaptureServiceV2;
708 yield* service.execute({
709 threadId: cancelledThreadId,
710 runId: cancelledRunId,
711 scopeId: cancelledScopeId,
712 });
713 // A redelivered effect finds the recorded checkpoint and commits nothing.
714 yield* service.execute({
715 threadId: cancelledThreadId,
716 runId: cancelledRunId,
717 scopeId: cancelledScopeId,
718 });
719 }).pipe(Effect.provide(captureLayer));
720​
721 assert.equal(yield* Ref.get(commits), 1);
722 const projected = yield* projectionStore.getCheckpointCaptureContext(cancelledThreadId, {
723 runId: cancelledRunId,
724 scopeId: cancelledScopeId,
725 });
726 assert.equal(projected.run?.status, "cancelled");
727 assert.equal(projected.run?.checkpointId, captured.id);
728 const completedAt = projected.run?.completedAt;
729 assert.equal(
730 completedAt ? DateTime.formatIso(completedAt) : completedAt,
731 DateTime.formatIso(cancelledAt),
732 );
733 assert.equal(projected.rootNode?.status, "cancelled");
734 assert.deepEqual([...projected.readyCheckpointOrdinals].toSorted(), [0, 1]);
735 }),
736 );
737});