@devries-cameron20 / t3code-1 / commits / 2a468acfed
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(server): the auto-settle sweep no longer rereads every idle branch every few minutes (#36371)

Julius Marminge committed Nov 13, 2026 at 17:14 UTC 2a468acfed5fb1c5ea9ca5b9ea0ae4bc38399086
6 files changed +214 -10
apps/server/src/orchestration-v2/ProjectionStore.ts
+2
@@ -1249,6 +1249,7 @@ const PullRequestThread = OrchestrationV2AppThreadJsonSchema.mapFields(
1249 projectId,
1250 lineage,
1251 createdAt,
1252 + updatedAt,
1253 branch,
1254 worktreePath,
1255 archivedAt,
@@ -1262,6 +1263,7 @@ const PullRequestThread = OrchestrationV2AppThreadJsonSchema.mapFields(
1263 projectId,
1264 lineage,
1265 createdAt,
1266 + updatedAt,
1267 branch,
1268 worktreePath,
1269 archivedAt,
apps/server/src/orchestration-v2/ThreadPullRequestService.test.ts
+8 -1
@@ -607,9 +607,16 @@ describe("ThreadPullRequestServiceV2 reads", () => {
607 yield* service.start();
608 yield* Deferred.succeed(activation, undefined);
609
610 - // Startup backfill sweep, then one periodic sweep.
610 + // Startup backfill sweep, then periodic sweeps: a branch without a pull request
611 + // is reread after half an hour, not on every sweep.
612 yield* Queue.take(sweeps);
613 yield* service.drain;
614 + for (let minute = 1; minute <= 29; minute += 1) {
615 + yield* TestClock.adjust("1 minute");
616 + yield* Queue.take(sweeps);
617 + yield* service.drain;
618 + }
619 + expect(lookups.map((lookup) => lookup.branch)).toEqual([parent.branch]);
620 yield* TestClock.adjust("1 minute");
621 yield* Queue.take(sweeps);
622 yield* service.drain;
apps/server/src/orchestration-v2/ThreadPullRequestService.ts
+28 -2
@@ -30,6 +30,7 @@ import { makeLinkedThreadsChangeDetector } from "../pullRequest/linkedThreads.ts
30 import * as RepositoryIdentityResolver from "../project/RepositoryIdentityResolver.ts";
31 import { forkParked } from "../serverActivation.ts";
32 import { sourceControlDefinitions } from "../sourceControl/sourceControlDefinitions.ts";
33 +import { makeNoPullRequestRechecks } from "./noPullRequestRechecks.ts";
34 import * as Orchestrator from "./Orchestrator.ts";
35
36 class ThreadPullRequestServiceV2 extends Context.Service<
@@ -123,6 +124,7 @@ export const make = Effect.gen(function* () {
124 const path = yield* Path.Path;
125 const backgroundPolicy = yield* BackgroundPolicy.BackgroundPolicy;
126 const pendingBackfill = new Map<ThreadId, number>();
127 + const noPullRequestRechecks = makeNoPullRequestRechecks();
128
129 const finishBackfill = (threads: ReadonlyArray<Pick<OrchestrationV2ThreadShell, "id">>) => {
130 for (const thread of threads) pendingBackfill.delete(thread.id);
@@ -156,6 +158,7 @@ export const make = Effect.gen(function* () {
158 )
159 : threadSnapshot.threads;
160 const projects = new Map(projectShells.map((project) => [project.id, project]));
161 + const nowMs = DateTime.toEpochMillis(yield* DateTime.now);
162 if (request.backfill) {
163 for (const thread of snapshotThreads) {
164 if (
@@ -185,13 +188,34 @@ export const make = Effect.gen(function* () {
188 JSON.stringify([thread.projectId, thread.worktreePath, thread.branch]),
189 );
190
191 + // A thread's own events and a backfill read at once; only scheduled sweeps wait.
192 + const scheduled = new Map(
193 + request.threadId !== null
194 + ? []
195 + : [...groups].filter(
196 + ([, group]) =>
197 + group[0]!.branch !== null &&
198 + group.every(
199 + (thread) => thread.branchPullRequest == null && !pendingBackfill.has(thread.id),
200 + ),
201 + ),
202 + );
203 + const scheduledLookups = noPullRequestRechecks.select(
204 + nowMs,
205 + Array.from(scheduled, ([key, group]) => ({
206 + key,
207 + activityAtMs: Math.max(...group.map((thread) => DateTime.toEpochMillis(thread.updatedAt))),
208 + })),
209 + );
210 +
211 yield* Effect.forEach(
189 - groups.values(),
190 - (group) =>
212 + groups,
213 + ([key, group]) =>
214 Effect.gen(function* () {
215 const first = group[0]!;
216 const project = projects.get(first.projectId);
217 if (project === undefined) return finishBackfill(group);
218 + if (scheduled.has(key) && !scheduledLookups.has(key)) return;
219 const { project: resolvedProject, repository } =
220 yield* resolveProjectForPullRequestDiscovery(project, repositoryIdentities, {
221 refresh: request.refresh,
@@ -214,6 +238,7 @@ export const make = Effect.gen(function* () {
238 { cwd, branch: first.branch },
239 { refresh: request.refresh, confirmTerminal: false },
240 );
241 + if (first.branch !== null) noPullRequestRechecks.record(key, nowMs, detected !== null);
242 if (detected !== null && !pullRequestMatchesProject(detected, resolvedProject)) {
243 return finishBackfill(group);
244 }
@@ -375,6 +400,7 @@ export const make = Effect.gen(function* () {
400 // Batch concurrent branch lookups into one GitHub GraphQL document.
401 { concurrency: 32, discard: true },
402 );
403 + if (request.threadId === null) noPullRequestRechecks.retain(groups);
404 });
405
406 // At most one queued request per thread, so a turn end's events share one lookup.
apps/server/src/orchestration-v2/ThreadSettlementService.test.ts
+87
@@ -1728,6 +1728,93 @@ describe("ThreadSettlementServiceV2 worker", () => {
1728 }),
1729 );
1730
1731 + it.effect("rereads a branch without a pull request less often the longer its thread idles", () =>
1732 + Effect.gen(function* () {
1733 + yield* TestClock.setTime(Date.parse(NOW));
1734 + const recent = makeThread("recent-branch", {
1735 + branch: "recent-feature",
1736 + latestUserMessageAt: DateTime.makeUnsafe(Date.parse(NOW) - 30 * 60 * 1_000),
1737 + });
1738 + const old = makeThread("old-branch", {
1739 + branch: "old-feature",
1740 + latestUserMessageAt: DateTime.makeUnsafe(Date.parse(NOW) - 2 * DAY_MS),
1741 + });
1742 + const fixture = yield* makeHarness({ snapshot: makeSnapshot([recent, old]) });
1743 + const branches = Ref.get(fixture.branchCalls).pipe(
1744 + Effect.map((calls) => calls.map((call) => call.branch).sort()),
1745 + );
1746 +
1747 + yield* Effect.gen(function* () {
1748 + const reactor = yield* ThreadSettlementService.ThreadSettlementServiceV2;
1749 + yield* startHarness(reactor, fixture.activation, fixture.snapshotReads);
1750 + for (let minute = 1; minute <= 29; minute += 1) {
1751 + yield* TestClock.adjust("1 minute");
1752 + yield* Queue.take(fixture.snapshotReads);
1753 + yield* reactor.drain;
1754 + }
1755 + assert.deepStrictEqual(yield* branches, ["old-feature", "recent-feature"]);
1756 +
1757 + yield* TestClock.adjust("1 minute");
1758 + yield* Queue.take(fixture.snapshotReads);
1759 + yield* reactor.drain;
1760 + assert.deepStrictEqual(yield* branches, [
1761 + "old-feature",
1762 + "recent-feature",
1763 + "recent-feature",
1764 + ]);
1765 +
1766 + // Work ending in the thread reads its branch at once.
1767 + yield* fixture.publishEvent({
1768 + type: "run.background-work-cancelled",
1769 + id: EventId.make("event:old-branch:run-ended"),
1770 + threadId: old.id,
1771 + occurredAt: DateTime.makeUnsafe(NOW),
1772 + payload: { runId: RunId.make("run:old-branch"), restartCancelledBackgroundWork: [] },
1773 + });
1774 + yield* Queue.take(fixture.snapshotReads);
1775 + yield* reactor.drain;
1776 + assert.deepStrictEqual(yield* branches, [
1777 + "old-feature",
1778 + "old-feature",
1779 + "recent-feature",
1780 + "recent-feature",
1781 + ]);
1782 + }).pipe(Effect.provide(fixture.layer));
1783 + }),
1784 + );
1785 +
1786 + it.effect("a sweep looks up a few branches without a pull request, not every one", () =>
1787 + Effect.gen(function* () {
1788 + yield* TestClock.setTime(Date.parse(NOW));
1789 + const threads = Array.from({ length: 10 }, (_, index) =>
1790 + makeThread(`idle-${index}`, {
1791 + branch: `idle-${index}`,
1792 + latestUserMessageAt: DateTime.makeUnsafe(Date.parse(NOW) - (index + 10) * 60 * 1_000),
1793 + }),
1794 + );
1795 + const fixture = yield* makeHarness({ snapshot: makeSnapshot(threads) });
1796 + const branches = Ref.get(fixture.branchCalls).pipe(
1797 + Effect.map((calls) => calls.map((call) => call.branch).sort()),
1798 + );
1799 +
1800 + yield* Effect.gen(function* () {
1801 + const reactor = yield* ThreadSettlementService.ThreadSettlementServiceV2;
1802 + yield* startHarness(reactor, fixture.activation, fixture.snapshotReads);
1803 + // The most recently active first, then the rest in turn, each once.
1804 + assert.deepStrictEqual(yield* branches, ["idle-0", "idle-1", "idle-2", "idle-3"]);
1805 + for (let minute = 1; minute <= 5; minute += 1) {
1806 + yield* TestClock.adjust("1 minute");
1807 + yield* Queue.take(fixture.snapshotReads);
1808 + yield* reactor.drain;
1809 + }
1810 + assert.deepStrictEqual(
1811 + yield* branches,
1812 + threads.map((thread) => thread.branch),
1813 + );
1814 + }).pipe(Effect.provide(fixture.layer));
1815 + }),
1816 + );
1817 +
1818 it.effect("a merged branch PR that cannot settle its thread rereads no git state", () =>
1819 Effect.gen(function* () {
1820 yield* TestClock.setTime(Date.parse(NOW));
apps/server/src/orchestration-v2/ThreadSettlementService.ts
+32 -7
@@ -35,6 +35,7 @@ import * as TerminalManager from "../terminal/Manager.ts";
35 import * as GitVcsDriver from "../vcs/GitVcsDriver.ts";
36 import * as ProjectStore from "./ProjectStore.ts";
37 import { sourceControlDefinitions } from "../sourceControl/sourceControlDefinitions.ts";
38 +import { makeNoPullRequestRechecks } from "./noPullRequestRechecks.ts";
39 import * as Orchestrator from "./Orchestrator.ts";
40 import * as ProjectionStore from "./ProjectionStore.ts";
41
@@ -60,6 +61,17 @@ function latestMillis(values: ReadonlyArray<number | null>): number | null {
61 return latest;
62 }
63
64 +function threadActivityAtMs(thread: ProjectionStore.ProjectionSettlementCandidate): number {
65 + return (
66 + latestMillis([
67 + toMillis(thread.latestUserMessageAt),
68 + toMillis(thread.latestRunRequestedAt),
69 + toMillis(thread.latestRunStartedAt),
70 + toMillis(thread.latestRunCompletedAt),
71 + ]) ?? DateTime.toEpochMillis(thread.createdAt)
72 + );
73 +}
74 +
75 function pullRequestMatchesProject(
76 pullRequest: GitManager.GitBranchPullRequest,
77 project: {
@@ -228,13 +240,7 @@ export function resolveAutoSettlementAt(input: {
240 };
241 }
242 if (!isAutoSettlementCandidate(thread, input.nowMs)) return null;
231 - const activityAtMs =
232 - latestMillis([
233 - toMillis(thread.latestUserMessageAt),
234 - toMillis(thread.latestRunRequestedAt),
235 - toMillis(thread.latestRunStartedAt),
236 - toMillis(thread.latestRunCompletedAt),
237 - ]) ?? DateTime.toEpochMillis(thread.createdAt);
243 + const activityAtMs = threadActivityAtMs(thread);
244 if (pullRequest !== null && pullRequestSettles(thread, pullRequest, input.autoSettleOnMerge)) {
245 return DateTime.makeUnsafe(activityAtMs);
246 }
@@ -318,6 +324,7 @@ export const make = Effect.gen(function* () {
324 const projectScripts = yield* ProjectSetupScriptRunner.ProjectSetupScriptRunner;
325 // Repeated thread.settled retains settledAt; run the action only once per settlement.
326 const settleActionRunAt = new Map<ThreadId, number>();
327 + const noPullRequestRechecks = makeNoPullRequestRechecks();
328
329 const sweep = Effect.fn("ThreadSettlementServiceV2.sweep")(function* (
330 mergedPullRequest: PullRequestService.PullRequestMergeEvent | null,
@@ -545,17 +552,34 @@ export const make = Effect.gen(function* () {
552 return yield* Effect.die(new Error("thread project not found"));
553 }
554 const refresh = group.filter((member) => shellClosedThreads.delete(member.id)).length > 0;
555 + const key = lookupKey(thread);
556 + if (scheduledLookups !== null && !refresh && !scheduledLookups.has(key)) return undefined;
557 // Remembered git state is enough to rule a thread out; only an answer
558 // that would settle it is confirmed against fresh git state.
559 const remembered = yield* git.branchPullRequest(
560 { cwd, branch: thread.branch },
561 { refresh, confirmTerminal: false },
562 );
563 + noPullRequestRechecks.record(key, nowMs, remembered !== null);
564 if (refresh || remembered === null || remembered.state === "open") return remembered;
565 if (!(yield* wouldSettle(group, remembered))) return undefined;
566 return yield* git.branchPullRequest({ cwd, branch: thread.branch }, { freshGitState: true });
567 });
568
569 + // A merge, a shell close and a thread's own events read at once; only scheduled sweeps wait.
570 + const scheduledLookups =
571 + mergedPullRequest !== null || threadId !== undefined
572 + ? null
573 + : noPullRequestRechecks.select(
574 + nowMs,
575 + [...groups]
576 + .filter(([key]) => key.startsWith('["branch",'))
577 + .map(([key, group]) => ({
578 + key,
579 + activityAtMs: Math.max(...group.map(threadActivityAtMs)),
580 + })),
581 + );
582 +
583 yield* Effect.forEach(
584 groups.values(),
585 (group) =>
@@ -577,6 +601,7 @@ export const make = Effect.gen(function* () {
601 ),
602 { concurrency: 8, discard: true },
603 );
604 + if (threadId === undefined) noPullRequestRechecks.retain(groups);
605 });
606
607 const runSweep = (
apps/server/src/orchestration-v2/noPullRequestRechecks.ts new
+57
@@ -0,0 +1,57 @@
1 +const RECHECK_MIN_MS = 30 * 60 * 1_000;
2 +const RECHECK_MAX_MS = 24 * 60 * 60 * 1_000;
3 +const RECHECK_IDLE_DIVISOR = 12;
4 +const RECHECKS_PER_SWEEP = 4;
5 +
6 +/**
7 + * Schedules a sweep's branch lookups, so their git work does not grow with the number of idle
8 + * threads. GitManager's caches cannot: they expire in minutes and evict past a few thousand branches.
9 + */
10 +export function makeNoPullRequestRechecks() {
11 + const reads = new Map<string, { readonly readAtMs: number; readonly found: boolean }>();
12 + return {
13 + /**
14 + * The branches a scheduled sweep looks up now: every one that last had a pull request, and
15 + * the few longest unread that had none and are due. One idle a day is due every two hours.
16 + */
17 + select: (
18 + nowMs: number,
19 + branches: ReadonlyArray<{ readonly key: string; readonly activityAtMs: number }>,
20 + ): ReadonlySet<string> => {
21 + const selected = new Set<string>();
22 + const due: Array<{ key: string; activityAtMs: number; readAtMs: number }> = [];
23 + for (const branch of branches) {
24 + const read = reads.get(branch.key);
25 + if (read?.found === true) {
26 + selected.add(branch.key);
27 + continue;
28 + }
29 + const delayMs = Math.min(
30 + Math.max((nowMs - branch.activityAtMs) / RECHECK_IDLE_DIVISOR, RECHECK_MIN_MS),
31 + RECHECK_MAX_MS,
32 + );
33 + if (read !== undefined && nowMs - read.readAtMs < delayMs) continue;
34 + due.push({ ...branch, readAtMs: read?.readAtMs ?? Number.NEGATIVE_INFINITY });
35 + }
36 + due.sort((left, right) =>
37 + left.readAtMs === right.readAtMs
38 + ? right.activityAtMs - left.activityAtMs
39 + : left.readAtMs - right.readAtMs,
40 + );
41 + // Stamped before the lookup, so one that keeps failing cannot hold a place in every sweep.
42 + for (const { key } of due.slice(0, RECHECKS_PER_SWEEP)) {
43 + selected.add(key);
44 + reads.set(key, { readAtMs: nowMs, found: false });
45 + }
46 + return selected;
47 + },
48 + record: (key: string, nowMs: number, found: boolean): void => {
49 + reads.set(key, { readAtMs: nowMs, found });
50 + },
51 + retain: (keys: { readonly has: (key: string) => boolean }): void => {
52 + for (const key of reads.keys()) {
53 + if (!keys.has(key)) reads.delete(key);
54 + }
55 + },
56 + };
57 +}