Open History to see the last commit
1import { assert, it } from "@effect/vitest";
2import { CheckpointScopeId, CommandId, GitCommandError, RunId, ThreadId } from "@t3tools/contracts";
3import * as Effect from "effect/Effect";
4import * as Layer from "effect/Layer";
5import * as Option from "effect/Option";
6
7import * as SqlitePersistence from "../persistence/Sqlite.ts";
8import * as PullRequestService from "../pullRequest/PullRequestService.ts";
9import * as ServerSettings from "../serverSettings.ts";
10import * as VcsStatusBroadcaster from "../vcs/VcsStatusBroadcaster.ts";
11import * as WorkspaceEntries from "../workspace/WorkspaceEntries.ts";
12import * as WorkspaceRepositories from "../workspace/WorkspaceRepositories.ts";
13import * as CheckpointCapture from "./CheckpointCaptureService.ts";
14import * as CheckpointRollback from "./CheckpointRollbackService.ts";
15import * as EffectOutbox from "./EffectOutbox.ts";
16import * as EffectWorker from "./EffectWorker.ts";
17import * as ProjectionStore from "./ProjectionStore.ts";
18import * as ProviderSessions from "./ProviderSessionManager.ts";
19import * as ProviderTurnControl from "./ProviderTurnControlService.ts";
20import * as ProviderTurnStart from "./ProviderTurnStartService.ts";
21import * as RunFinalization from "./RunFinalizationService.ts";
22import * as RuntimeRequests from "./RuntimeRequestService.ts";
23import * as ThreadManagement from "./ThreadManagementService.ts";
24import * as ThreadTitles from "./ThreadTitleRegenerationService.ts";
25
26it.effect.each([
27 ["starts the next turn after a post-capture Git status failure", false],
28 ["keeps the next turn queued when checkpoint capture fails", true],
29] as const)("%s", ([, captureFails]) => {
30 const threadId = ThreadId.make("thread:finalization-queue");
31 const runId = RunId.make("run:finalization-queue");
32 const scopeId = CheckpointScopeId.make("scope:finalization-queue");
33 const calls: string[] = [];
34 const checkpointFailure = new CheckpointCapture.CheckpointCaptureExecutionError({
35 threadId,
36 runId,
37 scopeId,
38 cause: "checkpoint storage unavailable",
39 });
40 const projections = Layer.mock(ProjectionStore.ProjectionStoreV2)({
41 getCheckpointContext: () =>
42 Effect.succeed({
43 runs: [],
44 checkpointScopes: [{ id: scopeId, runId, kind: "root_run" as const, cwd: "/repo" }],
45 checkpoints: [],
46 }),
47 });
48 const observer = RunFinalization.layerObserver.pipe(
49 Layer.provide(
50 Layer.mergeAll(
51 projections,
52 Layer.mock(WorkspaceEntries.WorkspaceEntries)({ refresh: () => Effect.void }),
53 Layer.mock(WorkspaceRepositories.WorkspaceRepositories)({ list: () => Effect.succeed([]) }),
54 Layer.mock(PullRequestService.PullRequestService)({ refreshAfterTurn: () => Effect.void }),
55 Layer.mock(VcsStatusBroadcaster.VcsStatusBroadcaster)({
56 refreshLocalStatus: () =>
57 Effect.gen(function* () {
58 calls.push("refresh");
59 return yield* new GitCommandError({
60 operation: "statusDetailsLocal",
61 command: "git",
62 cwd: "/repo",
63 detail: "VCS process timed out after 30000ms",
64 });
65 }),
66 }),
67 ),
68 ),
69 );
70 const finalization = RunFinalization.layer.pipe(
71 Layer.provide(
72 Layer.mergeAll(
73 projections,
74 observer,
75 Layer.mock(CheckpointCapture.CheckpointCaptureServiceV2)({
76 execute: () =>
77 Effect.gen(function* () {
78 calls.push("capture");
79 if (captureFails) return yield* checkpointFailure;
80 }),
81 }),
82 ),
83 ),
84 );
85 const executor = EffectWorker.layerExecutor.pipe(
86 Layer.provide(
87 Layer.mergeAll(
88 finalization,
89 Layer.mock(ProviderSessions.ProviderSessionManagerV2)({}),
90 Layer.mock(ProviderTurnControl.ProviderTurnControlServiceV2)({}),
91 Layer.mock(CheckpointRollback.CheckpointRollbackServiceV2)({}),
92 Layer.mock(RuntimeRequests.RuntimeRequestServiceV2)({}),
93 Layer.mock(ThreadTitles.ThreadTitleRegenerationService)({}),
94 Layer.mock(ThreadManagement.ThreadManagementService)({}),
95 ServerSettings.layerTest(),
96 Layer.mock(ProviderTurnStart.ProviderTurnStartServiceV2)({
97 start: () => Effect.sync(() => calls.push("start")).pipe(Effect.asVoid),
98 }),
99 ),
100 ),
101 );
102 const outbox = EffectOutbox.layer.pipe(Layer.provide(SqlitePersistence.layerMemory));
103 const worker = EffectWorker.layer.pipe(Layer.provideMerge(Layer.merge(executor, outbox)));
104
105 return Effect.gen(function* () {
106 const queue = yield* EffectOutbox.EffectOutboxV2;
107 const effectWorker = yield* EffectWorker.OrchestrationEffectWorkerV2;
108 yield* queue.enqueue([
109 {
110 id: "effect:finalization-queue:capture",
111 commandId: CommandId.make("command:finalization-queue:capture"),
112 threadId,
113 request: { type: "checkpoint.capture", runId, scopeId },
114 },
115 {
116 id: "effect:finalization-queue:start",
117 commandId: CommandId.make("command:finalization-queue:start"),
118 threadId,
119 request: { type: "provider-turn.start", runId: RunId.make("run:next") },
120 },
121 ]);
122
123 assert.equal(yield* effectWorker.drain(2), captureFails ? 1 : 2);
124 const captured = Option.getOrThrow(yield* queue.get("effect:finalization-queue:capture"));
125 const started = Option.getOrThrow(yield* queue.get("effect:finalization-queue:start"));
126 assert.equal(captured.status, captureFails ? "pending" : "succeeded");
127 assert.equal(captured.attemptCount, 1);
128 assert.equal(started.status, captureFails ? "pending" : "succeeded");
129 assert.equal(started.attemptCount, captureFails ? 0 : 1);
130 assert.deepEqual(calls, captureFails ? ["capture"] : ["capture", "refresh", "start"]);
131 if (captureFails) assert.include(captured.lastError ?? "", "checkpoint storage unavailable");
132 else assert.equal(captured.lastError, null);
133 }).pipe(Effect.provide(Layer.fresh(worker)));
134});