Open History to see the last commit
1import { assert, it } from "@effect/vitest";
2import {
3 CheckpointScopeId,
4 RunId,
5 ThreadId,
6 type OrchestrationV2ThreadShell,
7} from "@t3tools/contracts";
8import * as Deferred from "effect/Deferred";
9import * as Effect from "effect/Effect";
10import * as Fiber from "effect/Fiber";
11import * as Layer from "effect/Layer";
12import * as Option from "effect/Option";
13import * as TestClock from "effect/testing/TestClock";
14
15import * as PullRequestService from "../pullRequest/PullRequestService.ts";
16import * as VcsStatusBroadcaster from "../vcs/VcsStatusBroadcaster.ts";
17import * as WorkspaceEntries from "../workspace/WorkspaceEntries.ts";
18import * as CheckpointCapture from "./CheckpointCaptureService.ts";
19import * as ProjectionStore from "./ProjectionStore.ts";
20import * as RunFinalization from "./RunFinalizationService.ts";
21
22it.effect("completes finalization when the post-checkpoint workspace refresh fails", () => {
23 const threadId = ThreadId.make("thread_refresh_failure");
24 const runId = RunId.make("run_refresh_failure");
25 const scopeId = CheckpointScopeId.make("scope_refresh_failure");
26 const captured: string[] = [];
27 const layer = RunFinalization.layer.pipe(
28 Layer.provide(
29 Layer.mergeAll(
30 Layer.mock(CheckpointCapture.CheckpointCaptureServiceV2)({
31 execute: () =>
32 Effect.sync(() => {
33 captured.push("checkpoint");
34 }),
35 }),
36 Layer.mock(ProjectionStore.ProjectionStoreV2)({
37 getCheckpointContext: () =>
38 Effect.succeed({
39 runs: [],
40 checkpointScopes: [{ id: scopeId, runId, kind: "root_run" as const, cwd: "/repo" }],
41 checkpoints: [],
42 }),
43 }),
44 Layer.succeed(RunFinalization.RunFinalizationObserver, {
45 refresh: () =>
46 Effect.gen(function* () {
47 captured.push("refresh");
48 return yield* new RunFinalization.RunFinalizationRefreshError({
49 cwd: "/repo",
50 cause: new Error("Git command timed out"),
51 });
52 }),
53 refreshAfterTurn: () => Effect.void,
54 }),
55 ),
56 ),
57 );
58 return Effect.gen(function* () {
59 const service = yield* RunFinalization.RunFinalizationService;
60 yield* service.finalize({ threadId, runId, scopeId });
61 assert.deepEqual(captured, ["checkpoint", "refresh"]);
62 }).pipe(Effect.provide(layer));
63});
64
65it.effect.each(
66 (
67 [
68 {
69 label: "discovers a new PR for the completed run's branch",
70 branch: "feature",
71 checkedOut: "feature",
72 activeRun: null,
73 expected: ["/repo"],
74 },
75 {
76 label: "leaves the default branch's PR cache alone",
77 branch: "main",
78 checkedOut: "main",
79 activeRun: null,
80 expected: [],
81 },
82 {
83 label: "does not refresh another thread's checkout",
84 branch: "feature",
85 checkedOut: "other",
86 activeRun: null,
87 expected: [],
88 },
89 {
90 label: "does not refresh during a newer active run",
91 branch: "feature",
92 checkedOut: "feature",
93 activeRun: "newer-run",
94 expected: [],
95 },
96 ] as const
97 ).map((scenario) => [scenario.label, scenario] as const),
98)("%s", ([, scenario]) => {
99 const refreshed: string[] = [];
100 const lookedUp = Deferred.makeUnsafe<void>();
101 const threadId = ThreadId.make("thread-pr-refresh");
102 const runId = RunId.make("completed-run");
103 const layer = RunFinalization.layerObserver.pipe(
104 Layer.provide(
105 Layer.mergeAll(
106 Layer.mock(WorkspaceEntries.WorkspaceEntries)({ refresh: () => Effect.void }),
107 Layer.mock(PullRequestService.PullRequestService)({
108 refreshAfterTurn: () => Effect.void,
109 }),
110 Layer.mock(VcsStatusBroadcaster.VcsStatusBroadcaster)({
111 refreshLocalStatus: () =>
112 Effect.succeed({
113 isRepo: true,
114 hasPrimaryRemote: true,
115 isDefaultRef: scenario.checkedOut === "main",
116 refName: scenario.checkedOut,
117 hasWorkingTreeChanges: false,
118 workingTree: { files: [], insertions: 0, deletions: 0 },
119 }),
120 refreshStatus: () =>
121 Effect.die("turn completion must preserve known PRs and lookup backoff"),
122 // Requested synchronously by refresh; the lookup itself runs in the background.
123 refreshPullRequestStatus: (cwd) => {
124 refreshed.push(cwd);
125 return Deferred.succeed(lookedUp, undefined).pipe(Effect.as(null));
126 },
127 }),
128 Layer.mock(ProjectionStore.ProjectionStoreV2)({
129 getThreadShell: () =>
130 Effect.succeed({
131 id: threadId,
132 branch: scenario.branch,
133 activeRunId: scenario.activeRun === null ? null : RunId.make(scenario.activeRun),
134 } as OrchestrationV2ThreadShell),
135 }),
136 ),
137 ),
138 );
139 return Effect.gen(function* () {
140 const observer = yield* RunFinalization.RunFinalizationObserver;
141 yield* observer.refresh({ cwd: "/repo", threadId, runId });
142 if (scenario.expected.length > 0) yield* Deferred.await(lookedUp);
143 assert.deepEqual(refreshed, [...scenario.expected]);
144 }).pipe(Effect.provide(layer));
145});
146
147it.effect("returns from the post-turn refresh without waiting for the PR lookup", () => {
148 const threadId = ThreadId.make("thread-slow-forge");
149 const runId = RunId.make("completed-run");
150 const layer = RunFinalization.layerObserver.pipe(
151 Layer.provide(
152 Layer.mergeAll(
153 Layer.mock(WorkspaceEntries.WorkspaceEntries)({ refresh: () => Effect.void }),
154 Layer.mock(PullRequestService.PullRequestService)({
155 refreshAfterTurn: () => Effect.void,
156 }),
157 Layer.mock(VcsStatusBroadcaster.VcsStatusBroadcaster)({
158 refreshLocalStatus: () =>
159 Effect.succeed({
160 isRepo: true,
161 hasPrimaryRemote: true,
162 isDefaultRef: false,
163 refName: "feature",
164 hasWorkingTreeChanges: false,
165 workingTree: { files: [], insertions: 0, deletions: 0 },
166 }),
167 refreshPullRequestStatus: () => Effect.never,
168 }),
169 Layer.mock(ProjectionStore.ProjectionStoreV2)({
170 getThreadShell: () =>
171 Effect.succeed({
172 id: threadId,
173 branch: "feature",
174 activeRunId: null,
175 } as OrchestrationV2ThreadShell),
176 }),
177 ),
178 ),
179 );
180 return Effect.gen(function* () {
181 const observer = yield* RunFinalization.RunFinalizationObserver;
182 const refresh = yield* observer
183 .refresh({ cwd: "/repo", threadId, runId })
184 .pipe(Effect.timeoutOption("1 minute"), Effect.forkChild({ startImmediately: true }));
185 yield* TestClock.adjust("1 minute");
186 const result = yield* Fiber.join(refresh);
187 assert.isTrue(
188 Option.isSome(result),
189 "turn finalization waited on the forge PR lookup, blocking the thread's next turn",
190 );
191 }).pipe(Effect.provide(layer));
192});
193
194it.effect("replaces a still-running PR lookup when a newer turn finishes", () => {
195 const threadId = ThreadId.make("thread-back-to-back");
196 const runId = RunId.make("completed-run");
197 const firstInterrupted = Deferred.makeUnsafe<void>();
198 const secondLookedUp = Deferred.makeUnsafe<void>();
199 let lookups = 0;
200 const layer = RunFinalization.layerObserver.pipe(
201 Layer.provide(
202 Layer.mergeAll(
203 Layer.mock(WorkspaceEntries.WorkspaceEntries)({ refresh: () => Effect.void }),
204 Layer.mock(PullRequestService.PullRequestService)({
205 refreshAfterTurn: () => Effect.void,
206 }),
207 Layer.mock(VcsStatusBroadcaster.VcsStatusBroadcaster)({
208 refreshLocalStatus: () =>
209 Effect.succeed({
210 isRepo: true,
211 hasPrimaryRemote: true,
212 isDefaultRef: false,
213 refName: "feature",
214 hasWorkingTreeChanges: false,
215 workingTree: { files: [], insertions: 0, deletions: 0 },
216 }),
217 refreshPullRequestStatus: () =>
218 ++lookups === 1
219 ? Effect.never.pipe(
220 Effect.onInterrupt(() => Deferred.succeed(firstInterrupted, undefined)),
221 )
222 : Deferred.succeed(secondLookedUp, undefined).pipe(Effect.as(null)),
223 }),
224 Layer.mock(ProjectionStore.ProjectionStoreV2)({
225 getThreadShell: () =>
226 Effect.succeed({
227 id: threadId,
228 branch: "feature",
229 activeRunId: null,
230 } as OrchestrationV2ThreadShell),
231 }),
232 ),
233 ),
234 );
235 return Effect.gen(function* () {
236 const observer = yield* RunFinalization.RunFinalizationObserver;
237 yield* observer.refresh({ cwd: "/repo", threadId, runId });
238 yield* observer.refresh({ cwd: "/repo", threadId, runId });
239 yield* Deferred.await(firstInterrupted);
240 yield* Deferred.await(secondLookedUp);
241 assert.strictEqual(lookups, 2);
242 }).pipe(Effect.provide(layer));
243});