Skip to content

Commit 639dc20

Browse files
t3dotggclaude
andauthored
fix(server): threads settle right after a shell merge of an unlinked PR (#24830)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent e0b1940 commit 639dc20

2 files changed

Lines changed: 93 additions & 5 deletions

File tree

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

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
ProviderInstanceId,
99
RunId,
1010
ThreadId,
11+
TurnItemId,
1112
type OrchestrationProjectShell,
1213
type OrchestrationV2AppThread,
1314
type OrchestrationV2Command,
@@ -598,9 +599,12 @@ const makeHarness = Effect.fn("makeThreadSettlementHarness")(function* (options:
598599
return next;
599600
});
600601

601-
const branchPullRequest: GitManager.GitManager["Service"]["branchPullRequest"] = (input) =>
602+
const branchPullRequest: GitManager.GitManager["Service"]["branchPullRequest"] = (
603+
input,
604+
lookupOptions,
605+
) =>
602606
Ref.update(branchCalls, (calls) => [...calls, input]).pipe(
603-
Effect.andThen(options.branchPullRequest?.(input) ?? Effect.succeed(null)),
607+
Effect.andThen(options.branchPullRequest?.(input, lookupOptions) ?? Effect.succeed(null)),
604608
);
605609
const pullRequestSummary: PullRequestService.PullRequestService["Service"]["summary"] = (
606610
input,
@@ -1051,6 +1055,75 @@ describe("ThreadSettlementServiceV2 worker", () => {
10511055
),
10521056
);
10531057

1058+
it.effect("settles an unlinked branch thread its agent merged from a shell, past the cache", () =>
1059+
Effect.scoped(
1060+
Effect.gen(function* () {
1061+
yield* TestClock.setTime(Date.parse(NOW));
1062+
const settled = yield* Deferred.make<void>();
1063+
const thread = makeThread("shell-merged", {
1064+
branch: "saved-feature",
1065+
latestUserMessageAt: DateTime.makeUnsafe("2026-08-27T00:00:00.000Z"),
1066+
});
1067+
const fixture = yield* makeHarness({
1068+
snapshot: makeSnapshot([thread]),
1069+
// The host already merged it; only a fresh read sees that past the cached open lookup.
1070+
branchPullRequest: (_input, lookupOptions) =>
1071+
Effect.succeed(makeBranchPullRequest(lookupOptions?.refresh ? "merged" : "open")),
1072+
onDispatch: () => Deferred.succeed(settled, undefined),
1073+
});
1074+
const at = DateTime.makeUnsafe(NOW);
1075+
const runId = RunId.make("run:shell-merged");
1076+
const runEnded: OrchestrationV2DomainEvent = {
1077+
type: "run.background-work-cancelled",
1078+
id: EventId.make("event:shell-merged:run-ended"),
1079+
threadId: thread.id,
1080+
occurredAt: at,
1081+
payload: { runId, restartCancelledBackgroundWork: [] },
1082+
};
1083+
1084+
yield* Effect.gen(function* () {
1085+
const reactor = yield* ThreadSettlementService.ThreadSettlementServiceV2;
1086+
yield* startHarness(reactor, fixture.activation, fixture.snapshotReads);
1087+
yield* fixture.publishEvent(runEnded);
1088+
yield* Queue.take(fixture.snapshotReads);
1089+
yield* reactor.drain;
1090+
assert.deepStrictEqual(yield* Ref.get(fixture.commands), []);
1091+
1092+
yield* fixture.publishEvent({
1093+
type: "turn-item.updated",
1094+
id: EventId.make("event:shell-merged:command"),
1095+
threadId: thread.id,
1096+
runId,
1097+
occurredAt: at,
1098+
payload: {
1099+
id: TurnItemId.make("item:shell-merged:command"),
1100+
threadId: thread.id,
1101+
runId,
1102+
nodeId: null,
1103+
providerThreadId: null,
1104+
providerTurnId: null,
1105+
nativeItemRef: null,
1106+
parentItemId: null,
1107+
ordinal: 1,
1108+
status: "completed",
1109+
title: null,
1110+
startedAt: at,
1111+
completedAt: at,
1112+
updatedAt: at,
1113+
type: "command_execution",
1114+
input: "gh pr merge 42 --squash",
1115+
},
1116+
});
1117+
yield* Deferred.await(settled);
1118+
assert.deepStrictEqual(
1119+
(yield* Ref.get(fixture.commands)).map((command) => command.threadId),
1120+
[thread.id],
1121+
);
1122+
}).pipe(Effect.provide(fixture.layer));
1123+
}),
1124+
),
1125+
);
1126+
10541127
it.effect(
10551128
"settles branch threads on a pull request merge without waiting for the next sweep",
10561129
() =>

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,9 @@ export const make = Effect.gen(function* () {
298298
const pullRequests = yield* PullRequestService.PullRequestService;
299299
const crypto = yield* Crypto.Crypto;
300300
const fileSystem = yield* FileSystem.FileSystem;
301+
// A shell merge or close sends no event, and the branch lookup caches an open pull request for
302+
// a minute, so these threads' next lookup reads the host fresh.
303+
const shellClosedThreads = new Set<ThreadId>();
301304
const terminals = yield* TerminalManager.TerminalManager;
302305
const projectScripts = yield* ProjectSetupScriptRunner.ProjectSetupScriptRunner;
303306
// Repeated thread.settled retains settledAt; run the action only once per settlement.
@@ -490,7 +493,8 @@ export const make = Effect.gen(function* () {
490493
if (cwd === undefined) {
491494
return yield* Effect.die(new Error("thread project not found"));
492495
}
493-
return yield* git.branchPullRequest({ cwd, branch: thread.branch });
496+
const refresh = group.filter((member) => shellClosedThreads.delete(member.id)).length > 0;
497+
return yield* git.branchPullRequest({ cwd, branch: thread.branch }, { refresh });
494498
});
495499

496500
yield* Effect.forEach(
@@ -615,10 +619,21 @@ export const make = Effect.gen(function* () {
615619
return backgroundWorkHoldsCompletion(event.payload.pendingBackgroundTasks ?? [])
616620
? Effect.void
617621
: enqueueThread(event.threadId);
618-
case "turn-item.updated":
619-
return turnItemUpdateCanEndBackgroundWork(event.payload)
622+
case "turn-item.updated": {
623+
const item = event.payload;
624+
if (
625+
item.type === "command_execution" &&
626+
!isOrchestrationV2WorkActive(item.status) &&
627+
sourceControlDefinitions.definitions.some(
628+
(definition) => definition.changeRequestCloseCommand?.test(item.input) === true,
629+
)
630+
) {
631+
shellClosedThreads.add(event.threadId);
632+
}
633+
return turnItemUpdateCanEndBackgroundWork(item)
620634
? enqueueThread(event.threadId)
621635
: Effect.void;
636+
}
622637
case "provider-session.updated":
623638
return event.payload.status !== "starting" && event.payload.status !== "running"
624639
? enqueueThread(event.threadId)

0 commit comments

Comments
 (0)