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

RunFinalizationQueue.test.ts

172 lines7.6 KB

1import { assert, it } from "@effect/vitest";
2import { CheckpointScopeId, CommandId, GitCommandError, RunId, ThreadId } from "@t3tools/contracts";
3import * as Deferred from "effect/Deferred";
4import * as Effect from "effect/Effect";
5import * as Fiber from "effect/Fiber";
6import * as Layer from "effect/Layer";
7import * as Option from "effect/Option";
8​
9import * as SqlitePersistence from "../persistence/Sqlite.ts";
10import * as ProjectEnrichment from "../project/ProjectEnrichmentService.ts";
11import * as ProjectService from "../project/ProjectService.ts";
12import * as PullRequestService from "../pullRequest/PullRequestService.ts";
13import * as ServerSettings from "../serverSettings.ts";
14import * as VcsStatusBroadcaster from "../vcs/VcsStatusBroadcaster.ts";
15import * as WorkspaceEntries from "../workspace/WorkspaceEntries.ts";
16import * as CheckpointCapture from "./CheckpointCaptureService.ts";
17import * as CheckpointRollback from "./CheckpointRollbackService.ts";
18import * as EffectOutbox from "./EffectOutbox.ts";
19import * as EffectWorker from "./EffectWorker.ts";
20import * as ProjectionMaintenance from "./ProjectionMaintenance.ts";
21import * as ProjectionStore from "./ProjectionStore.ts";
22import * as ProviderSessions from "./ProviderSessionManager.ts";
23import * as ProviderTurnControl from "./ProviderTurnControlService.ts";
24import * as ProviderTurnStart from "./ProviderTurnStartService.ts";
25import * as RunFinalization from "./RunFinalizationService.ts";
26import * as RuntimeRequests from "./RuntimeRequestService.ts";
27import * as ThreadManagement from "./ThreadManagementService.ts";
28import * as ThreadTitles from "./ThreadTitleRegenerationService.ts";
29​
30const threadId = ThreadId.make("thread:finalization-queue");
31const runId = RunId.make("run:finalization-queue");
32const scopeId = CheckpointScopeId.make("scope:finalization-queue");
33const captureEffect = {
34 id: "effect:finalization-queue:capture",
35 commandId: CommandId.make("command:finalization-queue:capture"),
36 threadId,
37 request: { type: "checkpoint.capture" as const, runId, scopeId },
38};
39const startEffect = {
40 id: "effect:finalization-queue:start",
41 commandId: CommandId.make("command:finalization-queue:start"),
42 threadId,
43 request: { type: "provider-turn.start" as const, runId: RunId.make("run:next") },
44};
45​
46const makeWorker = (
47 calls: string[],
48 capture: Effect.Effect<void, CheckpointCapture.CheckpointCaptureExecutionError>,
49) => {
50 const projections = Layer.mock(ProjectionStore.ProjectionStoreV2)({
51 getCheckpointContext: () =>
52 Effect.succeed({
53 runs: [],
54 checkpointScopes: [{ id: scopeId, runId, kind: "root_run" as const, cwd: "/repo" }],
55 checkpoints: [],
56 }),
57 getThreadShell: () => Effect.succeed(null),
58 });
59 const observer = RunFinalization.layerObserver.pipe(
60 Layer.provide(
61 Layer.mergeAll(
62 projections,
63 Layer.mock(ProjectService.ProjectService)({}),
64 Layer.mock(ProjectEnrichment.ProjectEnrichmentService)({}),
65 Layer.mock(WorkspaceEntries.WorkspaceEntries)({ refresh: () => Effect.void }),
66 Layer.mock(PullRequestService.PullRequestService)({ refreshAfterTurn: () => Effect.void }),
67 Layer.mock(VcsStatusBroadcaster.VcsStatusBroadcaster)({
68 refreshLocalStatus: () =>
69 Effect.gen(function* () {
70 calls.push("refresh");
71 return yield* new GitCommandError({
72 operation: "statusDetailsLocal",
73 command: "git",
74 cwd: "/repo",
75 detail: "VCS process timed out after 30000ms",
76 });
77 }),
78 }),
79 ),
80 ),
81 );
82 const finalization = RunFinalization.layer.pipe(
83 Layer.provide(
84 Layer.mergeAll(
85 projections,
86 observer,
87 Layer.mock(CheckpointCapture.CheckpointCaptureServiceV2)({
88 execute: () => Effect.sync(() => calls.push("capture")).pipe(Effect.andThen(capture)),
89 }),
90 ),
91 ),
92 );
93 const executor = EffectWorker.layerExecutor.pipe(
94 Layer.provide(
95 Layer.mergeAll(
96 finalization,
97 Layer.mock(ProviderSessions.ProviderSessionManagerV2)({}),
98 Layer.mock(ProviderTurnControl.ProviderTurnControlServiceV2)({}),
99 Layer.mock(CheckpointRollback.CheckpointRollbackServiceV2)({}),
100 Layer.mock(RuntimeRequests.RuntimeRequestServiceV2)({}),
101 Layer.mock(ThreadTitles.ThreadTitleRegenerationService)({}),
102 Layer.mock(ThreadManagement.ThreadManagementService)({
103 recoverDelegatedTask: () => Effect.void,
104 }),
105 Layer.mock(ProjectionStore.ProjectionStoreV2)({}),
106 Layer.mock(ProjectionMaintenance.ProjectionMaintenanceV2)({}),
107 ServerSettings.layerTest(),
108 Layer.mock(ProviderTurnStart.ProviderTurnStartServiceV2)({
109 start: () => Effect.sync(() => calls.push("start")).pipe(Effect.asVoid),
110 }),
111 ),
112 ),
113 );
114 const outbox = EffectOutbox.layer.pipe(Layer.provide(SqlitePersistence.layerMemory));
115 return Layer.fresh(EffectWorker.layer.pipe(Layer.provideMerge(Layer.merge(executor, outbox))));
116};
117​
118it.effect.each([
119 ["starts the next turn after a post-capture Git status failure", false],
120 ["keeps the next turn queued when checkpoint capture fails", true],
121] as const)("%s", ([, captureFails]) => {
122 const calls: string[] = [];
123 const checkpointFailure = new CheckpointCapture.CheckpointCaptureExecutionError({
124 threadId,
125 runId,
126 scopeId,
127 cause: "checkpoint storage unavailable",
128 });
129 return Effect.gen(function* () {
130 const queue = yield* EffectOutbox.EffectOutboxV2;
131 const effectWorker = yield* EffectWorker.OrchestrationEffectWorkerV2;
132 yield* queue.enqueue([captureEffect, startEffect]);
133​
134 assert.equal(yield* effectWorker.drain(2), captureFails ? 1 : 2);
135 const captured = Option.getOrThrow(yield* queue.get("effect:finalization-queue:capture"));
136 const started = Option.getOrThrow(yield* queue.get("effect:finalization-queue:start"));
137 assert.equal(captured.status, captureFails ? "pending" : "succeeded");
138 assert.equal(captured.attemptCount, 1);
139 assert.equal(started.status, captureFails ? "pending" : "succeeded");
140 assert.equal(started.attemptCount, captureFails ? 0 : 1);
141 assert.deepEqual(calls, captureFails ? ["capture"] : ["capture", "refresh", "start"]);
142 if (captureFails) assert.include(captured.lastError ?? "", "checkpoint storage unavailable");
143 else assert.equal(captured.lastError, null);
144 }).pipe(Effect.provide(makeWorker(calls, captureFails ? checkpointFailure : Effect.void)));
145});
146​
147// A run completes before its capture, so the next turn's start is enqueued while capture runs.
148it.effect("keeps a next turn started during capture behind that capture", () => {
149 const calls: string[] = [];
150 return Effect.gen(function* () {
151 const captureStarted = yield* Deferred.make<void>();
152 const releaseCapture = yield* Deferred.make<void>();
153 const capture = Deferred.succeed(captureStarted, undefined).pipe(
154 Effect.andThen(Deferred.await(releaseCapture)),
155 );
156 return yield* Effect.gen(function* () {
157 const queue = yield* EffectOutbox.EffectOutboxV2;
158 const effectWorker = yield* EffectWorker.OrchestrationEffectWorkerV2;
159 yield* queue.enqueue([captureEffect]);
160 const draining = yield* Effect.forkChild(effectWorker.drain(2));
161 yield* Deferred.await(captureStarted);
162​
163 yield* queue.enqueue([startEffect]);
164 assert.isTrue(Option.isNone(yield* queue.claimNext({ workerId: "second-slot" })));
165 assert.deepEqual(calls, ["capture"]);
166​
167 yield* Deferred.succeed(releaseCapture, undefined);
168 assert.equal(yield* Fiber.join(draining), 2);
169 assert.deepEqual(calls, ["capture", "refresh", "start"]);
170 }).pipe(Effect.provide(makeWorker(calls, capture)));
171 });
172});