Skip to content

Commit 796cfdf

Browse files
fix(server): turn finalization no longer waits for the forge PR lookup
The post-turn refresh awaited the forge CLI PR lookup inside the thread's checkpoint.capture effect, so a slow `tea` call delayed the thread's next turn. The checkpoint, workspace entries and local VCS status are still awaited; the PR lookup now runs in a per-cwd FiberMap owned by the observer layer, so a newer turn's lookup replaces a running one instead of queueing behind the cwd lock. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 6fb3d55 commit 796cfdf

2 files changed

Lines changed: 64 additions & 5 deletions

File tree

‎apps/server/src/orchestration-v2/RunFinalizationService.test.ts‎

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
ThreadId,
66
type OrchestrationV2ThreadShell,
77
} from "@t3tools/contracts";
8+
import * as Deferred from "effect/Deferred";
89
import * as Effect from "effect/Effect";
910
import * as Fiber from "effect/Fiber";
1011
import * as Layer from "effect/Layer";
@@ -96,6 +97,7 @@ it.effect.each(
9697
).map((scenario) => [scenario.label, scenario] as const),
9798
)("%s", ([, scenario]) => {
9899
const refreshed: string[] = [];
100+
const lookedUp = Deferred.makeUnsafe<void>();
99101
const threadId = ThreadId.make("thread-pr-refresh");
100102
const runId = RunId.make("completed-run");
101103
const layer = RunFinalization.layerObserver.pipe(
@@ -117,11 +119,11 @@ it.effect.each(
117119
}),
118120
refreshStatus: () =>
119121
Effect.die("turn completion must preserve known PRs and lookup backoff"),
120-
refreshPullRequestStatus: (cwd) =>
121-
Effect.sync(() => {
122-
refreshed.push(cwd);
123-
return null;
124-
}),
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+
},
125127
}),
126128
Layer.mock(ProjectionStore.ProjectionStoreV2)({
127129
getThreadShell: () =>
@@ -137,6 +139,7 @@ it.effect.each(
137139
return Effect.gen(function* () {
138140
const observer = yield* RunFinalization.RunFinalizationObserver;
139141
yield* observer.refresh({ cwd: "/repo", threadId, runId });
142+
if (scenario.expected.length > 0) yield* Deferred.await(lookedUp);
140143
assert.deepEqual(refreshed, [...scenario.expected]);
141144
}).pipe(Effect.provide(layer));
142145
});
@@ -187,3 +190,54 @@ it.effect("returns from the post-turn refresh without waiting for the PR lookup"
187190
);
188191
}).pipe(Effect.provide(layer));
189192
});
193+
194+
it.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+
});

‎apps/server/src/orchestration-v2/RunFinalizationService.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { CheckpointScopeId, ProjectId, RunId, ThreadId } from "@t3tools/contracts";
22
import * as Context from "effect/Context";
33
import * as Effect from "effect/Effect";
4+
import * as FiberMap from "effect/FiberMap";
45
import * as Layer from "effect/Layer";
56
import * as Schema from "effect/Schema";
67

@@ -96,6 +97,7 @@ export const layerObserver = Layer.effect(
9697
const vcsStatus = yield* VcsStatusBroadcaster.VcsStatusBroadcaster;
9798
const projections = yield* ProjectionStore.ProjectionStoreV2;
9899
const pullRequests = yield* PullRequestService.PullRequestService;
100+
const pullRequestLookups = yield* FiberMap.make<string>();
99101
return {
100102
refreshAfterTurn: pullRequests.refreshAfterTurn,
101103
refresh: Effect.fn("RunFinalizationService.refresh")(
@@ -108,6 +110,8 @@ export const layerObserver = Layer.effect(
108110
const thread = yield* projections.getThreadShell(threadId);
109111
if (!thread || thread.branch !== local.refName) return;
110112
if (thread.activeRunId !== null && thread.activeRunId !== runId) return;
113+
// The forge CLI can take minutes, so the next turn must not wait for it. A newer
114+
// turn's lookup replaces a running one instead of queueing behind the cwd's lock.
111115
yield* vcsStatus.refreshPullRequestStatus(cwd).pipe(
112116
Effect.catch((error) =>
113117
Effect.logWarning("failed to refresh pull request status after run completion", {
@@ -116,6 +120,7 @@ export const layerObserver = Layer.effect(
116120
detail: error.message,
117121
}),
118122
),
123+
FiberMap.run(pullRequestLookups, cwd),
119124
);
120125
},
121126
(effect, { cwd }) =>

0 commit comments

Comments
 (0)