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