The best way to control Claude Code, Codex and more from any computer or phone
fix(server): threads settle right after a shell merge of an unlinked PR (#24830)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Theo Browne committed
Oct 25, 2026 at 02:31 UTC
639dc20e43fd981b6a0f3f932449ffd1b8f8be7a
2 files changed
+93
-5
apps/server/src/orchestration-v2/ThreadSettlementService.test.ts
+75
-2
@@ -8,6 +8,7 @@ import {
8
ProviderInstanceId,
9
RunId,
10
ThreadId,
11
+ TurnItemId,
12
type OrchestrationProjectShell,
13
type OrchestrationV2AppThread,
14
type OrchestrationV2Command,
@@ -598,9 +599,12 @@ const makeHarness = Effect.fn("makeThreadSettlementHarness")(function* (options:
599
return next;
600
});
601
601
- const branchPullRequest: GitManager.GitManager["Service"]["branchPullRequest"] = (input) =>
602
+ const branchPullRequest: GitManager.GitManager["Service"]["branchPullRequest"] = (
603
+ input,
604
+ lookupOptions,
605
+ ) =>
606
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)),
608
);
609
const pullRequestSummary: PullRequestService.PullRequestService["Service"]["summary"] = (
610
input,
@@ -1051,6 +1055,75 @@ describe("ThreadSettlementServiceV2 worker", () => {
1055
),
1056
);
1057
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
+
1127
it.effect(
1128
"settles branch threads on a pull request merge without waiting for the next sweep",
1129
() =>
apps/server/src/orchestration-v2/ThreadSettlementService.ts
+18
-3
@@ -298,6 +298,9 @@ export const make = Effect.gen(function* () {
298
const pullRequests = yield* PullRequestService.PullRequestService;
299
const crypto = yield* Crypto.Crypto;
300
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>();
304
const terminals = yield* TerminalManager.TerminalManager;
305
const projectScripts = yield* ProjectSetupScriptRunner.ProjectSetupScriptRunner;
306
// Repeated thread.settled retains settledAt; run the action only once per settlement.
@@ -490,7 +493,8 @@ export const make = Effect.gen(function* () {
493
if (cwd === undefined) {
494
return yield* Effect.die(new Error("thread project not found"));
495
}
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 });
498
});
499
500
yield* Effect.forEach(
@@ -615,10 +619,21 @@ export const make = Effect.gen(function* () {
619
return backgroundWorkHoldsCompletion(event.payload.pendingBackgroundTasks ?? [])
620
? Effect.void
621
: 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)
634
? enqueueThread(event.threadId)
635
: Effect.void;
636
+ }
637
case "provider-session.updated":
638
return event.payload.status !== "starting" && event.payload.status !== "running"
639
? enqueueThread(event.threadId)