Skip to content

Commit e4f1339

Browse files
Bil0000claude
andcommitted
fix(orchestration): run post-turn refreshes one at a time per checkout
Background refreshes could overlap, so an older status read could publish after a newer one. A per-cwd lock keeps them in order without blocking finalize. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent a6116da commit e4f1339

2 files changed

Lines changed: 18 additions & 12 deletions

File tree

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import * as CheckpointCapture from "./CheckpointCaptureService.ts";
1515
import * as ProjectionStore from "./ProjectionStore.ts";
1616
import * as RunFinalization from "./RunFinalizationService.ts";
1717

18-
it.effect("finalizes without awaiting the post-checkpoint workspace refresh", () => {
18+
it.effect("runs refreshes per checkout one at a time, without blocking finalize", () => {
1919
const threadId = ThreadId.make("thread_refresh_failure");
2020
const runId = RunId.make("run_refresh_failure");
2121
const scopeId = CheckpointScopeId.make("scope_refresh_failure");
@@ -50,7 +50,8 @@ it.effect("finalizes without awaiting the post-checkpoint workspace refresh", ()
5050
return Effect.gen(function* () {
5151
const service = yield* RunFinalization.RunFinalizationService;
5252
yield* service.finalize({ threadId, runId, scopeId });
53-
assert.deepEqual(captured, ["checkpoint", "refresh"]);
53+
yield* service.finalize({ threadId, runId, scopeId });
54+
assert.deepEqual(captured, ["checkpoint", "refresh", "checkpoint"]);
5455
}).pipe(Effect.provide(layer));
5556
});
5657

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

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as Effect from "effect/Effect";
44
import * as FiberSet from "effect/FiberSet";
55
import * as Layer from "effect/Layer";
66
import * as Schema from "effect/Schema";
7+
import * as KeyedLock from "@t3tools/shared/KeyedLock";
78

89
import * as PullRequestService from "../pullRequest/PullRequestService.ts";
910
import * as VcsStatusBroadcaster from "../vcs/VcsStatusBroadcaster.ts";
@@ -54,6 +55,7 @@ const make = Effect.gen(function* () {
5455
const projections = yield* ProjectionStore.ProjectionStoreV2;
5556
const observer = yield* RunFinalizationObserver;
5657
const refreshes = yield* FiberSet.make<void>();
58+
const refreshLocks = yield* KeyedLock.make<string>();
5759

5860
const finalize: RunFinalizationService["Service"]["finalize"] = Effect.fn(
5961
"RunFinalizationService.finalize",
@@ -75,16 +77,19 @@ const make = Effect.gen(function* () {
7577
const cwd = projection.checkpointScopes.find((scope) => scope.id === input.scopeId)?.cwd;
7678
if (cwd !== undefined) {
7779
// The refresh only updates the display; awaiting it holds the thread's next turn in the outbox.
78-
yield* observer.refresh({ cwd, threadId: input.threadId, runId: input.runId }).pipe(
79-
Effect.catchTags({
80-
RunFinalizationRefreshError: (cause) =>
81-
Effect.logWarning("Failed to refresh workspace after checkpoint capture", {
82-
...input,
83-
cause,
84-
}),
85-
}),
86-
FiberSet.run(refreshes),
87-
);
80+
// Serialized per cwd so an older status read cannot publish after a newer one.
81+
yield* refreshLocks
82+
.withLock(cwd, observer.refresh({ cwd, threadId: input.threadId, runId: input.runId }))
83+
.pipe(
84+
Effect.catchTags({
85+
RunFinalizationRefreshError: (cause) =>
86+
Effect.logWarning("Failed to refresh workspace after checkpoint capture", {
87+
...input,
88+
cause,
89+
}),
90+
}),
91+
FiberSet.run(refreshes),
92+
);
8893
}
8994
});
9095
return RunFinalizationService.of({ finalize });

0 commit comments

Comments
 (0)