Skip to content

Commit f2e2c4c

Browse files
committed
fix(server): settle branch threads immediately on pull request merge
1 parent 44701ef commit f2e2c4c

2 files changed

Lines changed: 61 additions & 10 deletions

File tree

‎apps/server/src/orchestration/ThreadSettlementReactor.test.ts‎

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ const makeHarness = Effect.fn("makeThreadSettlementHarness")(function* (options:
161161
}>
162162
>([]);
163163
const summaryRecovery = yield* Ref.make<ReadonlyArray<boolean | undefined>>([]);
164+
const invalidatedCwds = yield* Ref.make<ReadonlyArray<string>>([]);
164165

165166
const updateSettings = (patch: ServerSettingsPatch) =>
166167
Effect.gen(function* () {
@@ -221,7 +222,10 @@ const makeHarness = Effect.fn("makeThreadSettlementHarness")(function* (options:
221222
Effect.andThen(Ref.get(snapshots)),
222223
),
223224
}),
224-
Layer.mock(GitManager)({ branchPullRequest }),
225+
Layer.mock(GitManager)({
226+
branchPullRequest,
227+
invalidateStatus: (cwd) => Ref.update(invalidatedCwds, (cwds) => [...cwds, cwd]),
228+
}),
225229
Layer.mock(PullRequestService)({
226230
summary: pullRequestSummary,
227231
subscribeMerges: PubSub.subscribe(mergedPullRequests).pipe(
@@ -251,6 +255,7 @@ const makeHarness = Effect.fn("makeThreadSettlementHarness")(function* (options:
251255
branchCalls,
252256
summaryCalls,
253257
summaryRecovery,
258+
invalidatedCwds,
254259
updateSettings,
255260
publishMerge: PubSub.publish(mergedPullRequests, {
256261
projectId: PROJECT_ID,
@@ -446,6 +451,42 @@ describe("ThreadSettlementReactor", () => {
446451
),
447452
);
448453

454+
it.effect("settles branch threads on a pull request merge without waiting for the next sweep", () =>
455+
Effect.scoped(
456+
Effect.gen(function* () {
457+
yield* TestClock.setTime(Date.parse(NOW));
458+
const state = yield* Ref.make<"open" | "merged">("open");
459+
const mergedThreadSettled = yield* Deferred.make<void>();
460+
const fixture = yield* makeHarness({
461+
snapshot: makeSnapshot([makeThread("branch-thread", { branch: "saved-feature" })]),
462+
branchPullRequest: () =>
463+
Ref.get(state).pipe(
464+
Effect.map((pullRequestState) => ({ state: pullRequestState, updatedAt: NOW })),
465+
),
466+
onDispatch: () => Deferred.succeed(mergedThreadSettled, undefined),
467+
});
468+
469+
yield* Effect.gen(function* () {
470+
const reactor = yield* ThreadSettlementReactor.ThreadSettlementReactor;
471+
yield* startHarness(reactor, fixture.activation, fixture.snapshotReads);
472+
assert.deepStrictEqual(yield* Ref.get(fixture.commands), []);
473+
assert.deepStrictEqual(yield* Ref.get(fixture.invalidatedCwds), []);
474+
475+
yield* Ref.set(state, "merged");
476+
yield* fixture.publishMerge;
477+
yield* Deferred.await(mergedThreadSettled);
478+
479+
assert.deepStrictEqual(
480+
(yield* Ref.get(fixture.commands)).map((command) => command.threadId),
481+
[ThreadId.make("branch-thread")],
482+
);
483+
assert.deepStrictEqual(yield* Ref.get(fixture.invalidatedCwds), ["/workspace/project"]);
484+
yield* reactor.drain;
485+
}).pipe(Effect.provide(fixture.layer));
486+
}),
487+
),
488+
);
489+
449490
it.effect("uses fresh settlement settings after lookup and ignores unrelated changes", () =>
450491
Effect.scoped(
451492
Effect.gen(function* () {

‎apps/server/src/orchestration/ThreadSettlementReactor.ts‎

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,12 @@ export const make = Effect.gen(function* () {
4646
const snapshot = yield* snapshots.getShellSnapshot();
4747
const now = DateTime.formatIso(yield* DateTime.now);
4848
const projects = new Map(snapshot.projects.map((project) => [project.id, project]));
49-
const candidates = snapshot.threads.filter(
50-
(thread) =>
51-
isAutoSettlementCandidate(thread, now) &&
52-
(mergedPullRequest === null ||
53-
(thread.linkedPullRequest != null &&
54-
thread.linkedPullRequest.projectId === mergedPullRequest.projectId &&
55-
thread.linkedPullRequest.repository.toLowerCase() ===
56-
mergedPullRequest.repository.toLowerCase() &&
57-
thread.linkedPullRequest.number === mergedPullRequest.number)),
49+
// A merge event re-sweeps every candidate, not just the threads linked to
50+
// the merged pull request: most threads carry no link and settle from
51+
// their branch lookup, which would otherwise wait for the next minute's
52+
// sweep on a possibly stale cached answer.
53+
const candidates = snapshot.threads.filter((thread) =>
54+
isAutoSettlementCandidate(thread, now),
5855
);
5956
// Use the same cwd as the sidebar so both paths share GitManager's PR cache.
6057
const lookupCwdByThreadId = new Map<string, string>();
@@ -76,6 +73,19 @@ export const make = Effect.gen(function* () {
7673
}),
7774
{ concurrency: 8, discard: true },
7875
);
76+
if (mergedPullRequest !== null) {
77+
// The merge just confirmed a terminal state the lookup caches can still
78+
// call open (branch answers live two minutes, the sweep runs every
79+
// minute). Drop the swept checkouts' cached answers so the merge settles
80+
// its branch threads now instead of on a later sweep. Threads linked to
81+
// the merged pull request settle from the event itself below and need no
82+
// lookup, so they are absent from this map by construction.
83+
const cwds = [...new Set(lookupCwdByThreadId.values())];
84+
yield* Effect.forEach(cwds, (cwd) => git.invalidateStatus(cwd), {
85+
concurrency: 8,
86+
discard: true,
87+
});
88+
}
7989
const lookupKey = (thread: (typeof candidates)[number]) => {
8090
if (thread.linkedPullRequest != null) {
8191
return JSON.stringify([

0 commit comments

Comments
 (0)