Skip to content

Commit 8c051c4

Browse files
t3dotggclaude
andcommitted
fix(server): auto-settle lands while clients still hold the thread
Clients hold a thread in Working for two seconds after it stops. Settlement could land later, so a thread appeared in the inbox and then vanished. - Read a pull request as soon as a shell merge or close command ends, not when the run ends, so the merged state is known before the thread stops. - Sweep a thread when its background work can end. Before, only the minute timer noticed. - Sweep stopped threads on their own queue, so they never wait behind a full sweep's host reads. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 4c92219 commit 8c051c4

4 files changed

Lines changed: 177 additions & 25 deletions

File tree

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

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1340,7 +1340,7 @@ describe("PullRequestSyncReactor", () => {
13401340
"gh pr merge 7 --squash 2>&1 | tail -3",
13411341
"cafe pr merge 7 --squash",
13421342
"az repos pr update --id 7 --status completed",
1343-
])("reads open links fresh only when a run that ran `%s` ends", (mergeCommand) =>
1343+
])("reads open links fresh only for a thread that ran `%s`", (mergeCommand) =>
13441344
Effect.scoped(
13451345
Effect.gen(function* () {
13461346
yield* TestClock.setTime(Date.parse(NOW));
@@ -1392,4 +1392,45 @@ describe("PullRequestSyncReactor", () => {
13921392
}),
13931393
),
13941394
);
1395+
1396+
it.effect("reads open links fresh as soon as a merge command ends, before its run ends", () =>
1397+
Effect.scoped(
1398+
Effect.gen(function* () {
1399+
yield* TestClock.setTime(Date.parse(NOW));
1400+
const state = yield* Ref.make<PullRequestSummary["state"]>("open");
1401+
const fixture = yield* makeHarness({
1402+
snapshot: makeSnapshot([
1403+
makeThread("agent", { pullRequests: [makeLink(7, { state: "open" })] }),
1404+
]),
1405+
summary: (input) =>
1406+
Ref.get(state).pipe(
1407+
Effect.map((current) =>
1408+
makeSummary(input, current === "merged" ? { state: current, mergedAt: NOW } : {}),
1409+
),
1410+
),
1411+
});
1412+
1413+
yield* Effect.gen(function* () {
1414+
const reactor = yield* startAndSweep(fixture);
1415+
yield* Ref.set(state, "merged");
1416+
yield* Ref.set(fixture.summaryCalls, []);
1417+
1418+
yield* Queue.offer(
1419+
fixture.domainEvents,
1420+
commandRan(ThreadId.make("agent"), "gh pr merge 7"),
1421+
);
1422+
yield* Queue.take(fixture.snapshotReads);
1423+
yield* reactor.drain;
1424+
1425+
assert.deepStrictEqual(
1426+
(yield* Ref.get(fixture.syncCommands)).map((command) => [
1427+
command.number,
1428+
command.snapshot.state,
1429+
]),
1430+
[[7, "merged"]],
1431+
);
1432+
}).pipe(Effect.provide(fixture.layer));
1433+
}),
1434+
),
1435+
);
13951436
});

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

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
CommandId,
3+
isOrchestrationV2WorkActive,
34
type PullRequestSummary,
45
type ThreadId,
56
type ThreadPullRequestKey,
@@ -481,7 +482,8 @@ export const make = Effect.gen(function* () {
481482
}).pipe(Effect.catchCause(logSkipped("pull request sync sweep failed", {}))),
482483
);
483484

484-
const closeCommandThreads = new Set<ThreadId>();
485+
// Close commands per thread, by turn item, and whether each has ended and been read for.
486+
const closeCommands = new Map<ThreadId, Map<string, boolean>>();
485487
const refreshOpenLinks = (threadId: ThreadId) =>
486488
projections.getThreadsWithPullRequests(threadId).pipe(
487489
Effect.flatMap((threads) =>
@@ -525,22 +527,29 @@ export const make = Effect.gen(function* () {
525527
{ discard: true },
526528
);
527529
// An agent can merge or close its pull request from a shell with its host's CLI, which
528-
// sends no merge notification. When a run that ran such a command ends, read the
529-
// thread's open links fresh, so settlement does not wait for the next sweep and the
530-
// cached summary. Other runs add no host reads.
531-
case "turn-item.updated":
532-
if (
533-
event.payload.type === "command_execution" &&
534-
isChangeRequestCloseCommand(event.payload.input)
535-
) {
536-
closeCommandThreads.add(event.threadId);
530+
// sends no merge notification. Read the thread's open links fresh once that command
531+
// ends, while the agent usually still replies, so settlement can land before the run
532+
// ends. A run whose command never reported an end reads them when it ends.
533+
case "turn-item.updated": {
534+
const item = event.payload;
535+
if (item.type !== "command_execution" || !isChangeRequestCloseCommand(item.input)) {
536+
return Effect.void;
537537
}
538-
return Effect.void;
539-
case "run.updated":
540-
return isTerminalRunStatus(event.payload.status) &&
541-
closeCommandThreads.delete(event.threadId)
538+
const commands = closeCommands.get(event.threadId) ?? new Map<string, boolean>();
539+
closeCommands.set(event.threadId, commands);
540+
const ended = !isOrchestrationV2WorkActive(item.status);
541+
if (commands.get(item.id) === true) return Effect.void;
542+
commands.set(item.id, ended);
543+
return ended ? refreshOpenLinks(event.threadId) : Effect.void;
544+
}
545+
case "run.updated": {
546+
if (!isTerminalRunStatus(event.payload.status)) return Effect.void;
547+
const commands = closeCommands.get(event.threadId);
548+
closeCommands.delete(event.threadId);
549+
return commands !== undefined && [...commands.values()].some((ended) => !ended)
542550
? refreshOpenLinks(event.threadId)
543551
: Effect.void;
552+
}
544553
default:
545554
return Effect.void;
546555
}

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

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
ProjectId,
77
EventId,
88
ProviderInstanceId,
9+
RunId,
910
ThreadId,
1011
type OrchestrationProjectShell,
1112
type OrchestrationV2AppThread,
@@ -892,6 +893,82 @@ describe("ThreadSettlementServiceV2 worker", () => {
892893
),
893894
);
894895

896+
it.effect("settles a merged thread when its background work ends, during a slow full sweep", () =>
897+
Effect.scoped(
898+
Effect.gen(function* () {
899+
yield* TestClock.setTime(Date.parse(NOW));
900+
const periodicLookupStarted = yield* Deferred.make<void>();
901+
const releasePeriodicLookup = yield* Deferred.make<void>();
902+
const settled = yield* Deferred.make<void>();
903+
const branchLookupCount = yield* Ref.make(0);
904+
const mergedLink: NonNullable<SettlementShell["pullRequests"]>[number] = {
905+
...persistedLink("open", DateTime.makeUnsafe("2026-08-20T00:00:00.000Z")),
906+
snapshot: {
907+
state: "merged",
908+
title: "Pull request",
909+
headBranch: "feature",
910+
baseBranch: "main",
911+
isDraft: false,
912+
updatedAt: NOW,
913+
syncedAt: NOW,
914+
mergedAt: NOW,
915+
},
916+
};
917+
const waiting = makeThread("waiting-on-subagent", {
918+
pullRequests: [mergedLink],
919+
pendingBackgroundTasks: [{ taskId: "child", kind: "subagent", description: "Review" }],
920+
});
921+
const slow = makeThread("slow-periodic-lookup", {
922+
branch: "another-feature",
923+
latestUserMessageAt: DateTime.makeUnsafe("2026-08-27T00:00:00.000Z"),
924+
});
925+
const fixture = yield* makeHarness({
926+
snapshot: makeSnapshot([waiting, slow]),
927+
branchPullRequest: () =>
928+
Ref.updateAndGet(branchLookupCount, (count) => count + 1).pipe(
929+
Effect.flatMap((count) =>
930+
count === 1
931+
? Effect.succeed(makeBranchPullRequest("open"))
932+
: Deferred.succeed(periodicLookupStarted, undefined).pipe(
933+
Effect.andThen(Deferred.await(releasePeriodicLookup)),
934+
Effect.as(makeBranchPullRequest("open")),
935+
),
936+
),
937+
),
938+
onDispatch: () => Deferred.succeed(settled, undefined),
939+
});
940+
941+
yield* Effect.gen(function* () {
942+
const reactor = yield* ThreadSettlementService.ThreadSettlementServiceV2;
943+
yield* startHarness(reactor, fixture.activation, fixture.snapshotReads);
944+
assert.deepStrictEqual(yield* Ref.get(fixture.commands), []);
945+
yield* fixture.updateSettings({ sidebarAutoSettleAfterDays: 4 });
946+
yield* Deferred.await(periodicLookupStarted);
947+
948+
yield* Ref.set(
949+
fixture.snapshots,
950+
makeSnapshot([{ ...waiting, pendingBackgroundTasks: [] }, slow]),
951+
);
952+
yield* fixture.publishEvent({
953+
type: "run.background-work-cancelled",
954+
id: EventId.make("event:background-ended"),
955+
threadId: waiting.id,
956+
occurredAt: DateTime.makeUnsafe(NOW),
957+
payload: { runId: RunId.make("run:waiting"), restartCancelledBackgroundWork: [] },
958+
});
959+
yield* Deferred.await(settled);
960+
961+
assert.deepStrictEqual(
962+
(yield* Ref.get(fixture.commands)).map((command) => command.threadId),
963+
[waiting.id],
964+
);
965+
yield* Deferred.succeed(releasePeriodicLookup, undefined);
966+
yield* reactor.drain;
967+
}).pipe(Effect.provide(fixture.layer));
968+
}),
969+
),
970+
);
971+
895972
it.effect(
896973
"settles branch threads on a pull request merge without waiting for the next sweep",
897974
() =>

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

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { backgroundWorkHoldsCompletion } from "@t3tools/shared/orchestrationV2PendingBackgroundWork";
1+
import {
2+
backgroundWorkHoldsCompletion,
3+
turnItemUpdateCanEndBackgroundWork,
4+
} from "@t3tools/shared/orchestrationV2PendingBackgroundWork";
25
import { resolveProjectSettings } from "@t3tools/shared/projectSettings";
36
import { visibleThreadPullRequests } from "@t3tools/shared/threadPullRequests";
47
import {
@@ -523,9 +526,21 @@ export const make = Effect.gen(function* () {
523526
}),
524527
),
525528
);
526-
const worker = yield* makeDrainableWorker((threadId: ThreadId | undefined) =>
527-
runSweep(null, threadId),
529+
// A thread that just stopped must not wait behind a full sweep's host reads: clients
530+
// hold it in Working only briefly, so a late settle makes it appear and then vanish.
531+
const fullSweeps = yield* makeDrainableWorker(() => runSweep(null));
532+
const queuedThreads = new Set<ThreadId>();
533+
const threadSweeps = yield* makeDrainableWorker((threadId: ThreadId) =>
534+
Effect.sync(() => queuedThreads.delete(threadId)).pipe(
535+
Effect.andThen(runSweep(null, threadId)),
536+
),
528537
);
538+
const enqueueThread = (threadId: ThreadId) =>
539+
Effect.suspend(() => {
540+
if (queuedThreads.has(threadId)) return Effect.void;
541+
queuedThreads.add(threadId);
542+
return threadSweeps.enqueue(threadId);
543+
}).pipe(Effect.uninterruptible);
529544

530545
// Skip settle scripts in shared checkouts where other threads may still work.
531546
const cleanUpSettledThread = Effect.fn("ThreadSettlementServiceV2.cleanUpSettledThread")(
@@ -572,16 +587,23 @@ export const make = Effect.gen(function* () {
572587
case "thread.pull-request-link-updated":
573588
case "thread.pull-request-synced":
574589
case "provider-session.detached":
575-
return worker.enqueue(event.threadId);
590+
case "provider-thread.updated":
591+
case "subagent.updated":
592+
case "run.background-work-cancelled":
593+
return enqueueThread(event.threadId);
594+
case "turn-item.updated":
595+
return turnItemUpdateCanEndBackgroundWork(event.payload)
596+
? enqueueThread(event.threadId)
597+
: Effect.void;
576598
case "provider-session.updated":
577599
return event.payload.status !== "starting" && event.payload.status !== "running"
578-
? worker.enqueue(event.threadId)
600+
? enqueueThread(event.threadId)
579601
: Effect.void;
580602
case "run.updated":
581603
return ["completed", "interrupted", "failed", "cancelled", "rolled_back"].includes(
582604
event.payload.status,
583605
)
584-
? worker.enqueue(event.threadId)
606+
? enqueueThread(event.threadId)
585607
: Effect.void;
586608
default:
587609
return Effect.void;
@@ -598,8 +620,8 @@ export const make = Effect.gen(function* () {
598620
let lastSettlementSettings = autoSettlementSettingsKey(initialSettings);
599621
yield* forkParked(
600622
Effect.gen(function* () {
601-
yield* worker.enqueue(undefined);
602-
yield* worker.drain;
623+
yield* fullSweeps.enqueue(undefined);
624+
yield* fullSweeps.drain;
603625
}).pipe(Effect.repeat(Schedule.spaced("1 minute")), Effect.asVoid),
604626
);
605627
yield* forkParked(
@@ -609,7 +631,7 @@ export const make = Effect.gen(function* () {
609631
return Effect.void;
610632
}
611633
lastSettlementSettings = key;
612-
return worker.enqueue(undefined);
634+
return fullSweeps.enqueue(undefined);
613635
}),
614636
);
615637
yield* forkParked(Stream.runForEach(mergedPullRequests, (event) => runSweep(event)));
@@ -622,7 +644,10 @@ export const make = Effect.gen(function* () {
622644
);
623645
});
624646

625-
return { start, drain: worker.drain } satisfies ThreadSettlementServiceV2["Service"];
647+
return {
648+
start,
649+
drain: Effect.andThen(fullSweeps.drain, threadSweeps.drain),
650+
} satisfies ThreadSettlementServiceV2["Service"];
626651
});
627652

628653
export const layer = Layer.effect(ThreadSettlementServiceV2, make);

0 commit comments

Comments
 (0)