Skip to content

Commit 7efaa84

Browse files
fix(server): pull request discovery no longer rereads every idle branch every few minutes
The discovery sweep in ThreadPullRequestServiceV2 runs every minute while a client is in the foreground and looked up every unsettled branch group, the same recurring load the settlement sweep had. The recheck deadline moves into a helper both sweeps use. A scheduled discovery sweep skips a branch group that last had no pull request until a twelfth of the time since its threads last changed has passed, at least five minutes and at most a day. A thread's own events, a backfill and a thread with a saved branch pull request still read at once. Fixes #35319 Co-Authored-By: Claude Fable 5.2 <[email protected]>
1 parent fdc8bc2 commit 7efaa84

5 files changed

Lines changed: 70 additions & 31 deletions

File tree

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,6 +1217,7 @@ const PullRequestThread = OrchestrationV2AppThreadJsonSchema.mapFields(
12171217
projectId,
12181218
lineage,
12191219
createdAt,
1220+
updatedAt,
12201221
branch,
12211222
worktreePath,
12221223
archivedAt,
@@ -1230,6 +1231,7 @@ const PullRequestThread = OrchestrationV2AppThreadJsonSchema.mapFields(
12301231
projectId,
12311232
lineage,
12321233
createdAt,
1234+
updatedAt,
12331235
branch,
12341236
worktreePath,
12351237
archivedAt,

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,9 +607,16 @@ describe("ThreadPullRequestServiceV2 reads", () => {
607607
yield* service.start();
608608
yield* Deferred.succeed(activation, undefined);
609609

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 five minutes, not on every sweep.
611612
yield* Queue.take(sweeps);
612613
yield* service.drain;
614+
for (let minute = 1; minute <= 4; 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]);
613620
yield* TestClock.adjust("1 minute");
614621
yield* Queue.take(sweeps);
615622
yield* service.drain;

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import { makeLinkedThreadsChangeDetector } from "../pullRequest/linkedThreads.ts
3030
import * as RepositoryIdentityResolver from "../project/RepositoryIdentityResolver.ts";
3131
import { forkParked } from "../serverActivation.ts";
3232
import { sourceControlDefinitions } from "../sourceControl/sourceControlDefinitions.ts";
33+
import { makeNoPullRequestRechecks } from "./noPullRequestRechecks.ts";
3334
import * as Orchestrator from "./Orchestrator.ts";
3435

3536
class ThreadPullRequestServiceV2 extends Context.Service<
@@ -123,6 +124,7 @@ export const make = Effect.gen(function* () {
123124
const path = yield* Path.Path;
124125
const backgroundPolicy = yield* BackgroundPolicy.BackgroundPolicy;
125126
const pendingBackfill = new Map<ThreadId, number>();
127+
const noPullRequestRechecks = makeNoPullRequestRechecks();
126128

127129
const finishBackfill = (threads: ReadonlyArray<Pick<OrchestrationV2ThreadShell, "id">>) => {
128130
for (const thread of threads) pendingBackfill.delete(thread.id);
@@ -156,6 +158,7 @@ export const make = Effect.gen(function* () {
156158
)
157159
: threadSnapshot.threads;
158160
const projects = new Map(projectShells.map((project) => [project.id, project]));
161+
const nowMs = DateTime.toEpochMillis(yield* DateTime.now);
159162
if (request.backfill) {
160163
for (const thread of snapshotThreads) {
161164
if (
@@ -186,12 +189,26 @@ export const make = Effect.gen(function* () {
186189
);
187190

188191
yield* Effect.forEach(
189-
groups.values(),
190-
(group) =>
192+
groups,
193+
([key, group]) =>
191194
Effect.gen(function* () {
192195
const first = group[0]!;
193196
const project = projects.get(first.projectId);
194197
if (project === undefined) return finishBackfill(group);
198+
// A thread's own events and a backfill read at once; only scheduled sweeps wait.
199+
if (
200+
request.threadId === null &&
201+
group.every(
202+
(thread) => thread.branchPullRequest == null && !pendingBackfill.has(thread.id),
203+
) &&
204+
noPullRequestRechecks.waiting(
205+
key,
206+
nowMs,
207+
Math.max(...group.map((thread) => DateTime.toEpochMillis(thread.updatedAt))),
208+
)
209+
) {
210+
return;
211+
}
195212
const { project: resolvedProject, repository } =
196213
yield* resolveProjectForPullRequestDiscovery(project, repositoryIdentities, {
197214
refresh: request.refresh,
@@ -214,6 +231,7 @@ export const make = Effect.gen(function* () {
214231
{ cwd, branch: first.branch },
215232
{ refresh: request.refresh, confirmTerminal: false },
216233
);
234+
if (first.branch !== null) noPullRequestRechecks.record(key, nowMs, detected !== null);
217235
if (detected !== null && !pullRequestMatchesProject(detected, resolvedProject)) {
218236
return finishBackfill(group);
219237
}
@@ -375,6 +393,7 @@ export const make = Effect.gen(function* () {
375393
// Batch concurrent branch lookups into one GitHub GraphQL document.
376394
{ concurrency: 32, discard: true },
377395
);
396+
if (request.threadId === null) noPullRequestRechecks.retain(groups);
378397
});
379398

380399
// At most one queued request per thread, so a turn end's events share one lookup.

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

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import * as TerminalManager from "../terminal/Manager.ts";
3535
import * as GitVcsDriver from "../vcs/GitVcsDriver.ts";
3636
import * as ProjectStore from "./ProjectStore.ts";
3737
import { sourceControlDefinitions } from "../sourceControl/sourceControlDefinitions.ts";
38+
import { makeNoPullRequestRechecks } from "./noPullRequestRechecks.ts";
3839
import * as Orchestrator from "./Orchestrator.ts";
3940
import * as ProjectionStore from "./ProjectionStore.ts";
4041

@@ -46,8 +47,6 @@ export interface SettlementPullRequest {
4647

4748
const DAY_MS = 24 * 60 * 60 * 1_000;
4849
const QUEUED_TURN_START_GRACE_MS = 2 * 60 * 1_000;
49-
const BRANCH_LOOKUP_RECHECK_MIN_MS = 5 * 60 * 1_000;
50-
const BRANCH_LOOKUP_RECHECK_IDLE_DIVISOR = 12;
5150

5251
function toMillis(value: DateTime.Utc | null | undefined): number | null {
5352
return value == null ? null : DateTime.toEpochMillis(value);
@@ -73,14 +72,6 @@ function threadActivityAtMs(thread: ProjectionStore.ProjectionSettlementCandidat
7372
);
7473
}
7574

76-
/** A thread idle for a day is reread every two hours; one idle twelve days or more, daily. */
77-
function branchLookupRecheckDelayMs(idleMs: number): number {
78-
return Math.min(
79-
Math.max(idleMs / BRANCH_LOOKUP_RECHECK_IDLE_DIVISOR, BRANCH_LOOKUP_RECHECK_MIN_MS),
80-
DAY_MS,
81-
);
82-
}
83-
8475
function pullRequestMatchesProject(
8576
pullRequest: GitManager.GitBranchPullRequest,
8677
project: {
@@ -333,9 +324,7 @@ export const make = Effect.gen(function* () {
333324
const projectScripts = yield* ProjectSetupScriptRunner.ProjectSetupScriptRunner;
334325
// Repeated thread.settled retains settledAt; run the action only once per settlement.
335326
const settleActionRunAt = new Map<ThreadId, number>();
336-
// When each branch lookup last found no pull request. Scheduled sweeps wait on it instead of
337-
// GitManager's caches, which expire in minutes and evict past a few thousand branches.
338-
const noPullRequestReadAtMs = new Map<string, number>();
327+
const noPullRequestRechecks = makeNoPullRequestRechecks();
339328

340329
const sweep = Effect.fn("ThreadSettlementServiceV2.sweep")(function* (
341330
mergedPullRequest: PullRequestService.PullRequestMergeEvent | null,
@@ -564,28 +553,22 @@ export const make = Effect.gen(function* () {
564553
}
565554
const refresh = group.filter((member) => shellClosedThreads.delete(member.id)).length > 0;
566555
const key = lookupKey(thread);
567-
const readAtMs = noPullRequestReadAtMs.get(key);
568556
// A merge, a shell close and a thread's own events read at once; only scheduled sweeps wait.
569557
if (
570-
readAtMs !== undefined &&
571558
mergedPullRequest === null &&
572559
threadId === undefined &&
573-
!refresh
560+
!refresh &&
561+
noPullRequestRechecks.waiting(key, nowMs, Math.max(...group.map(threadActivityAtMs)))
574562
) {
575-
const activityAtMs = group.reduce(
576-
(latest, member) => Math.max(latest, threadActivityAtMs(member)),
577-
Number.NEGATIVE_INFINITY,
578-
);
579-
if (nowMs - readAtMs < branchLookupRecheckDelayMs(nowMs - activityAtMs)) return undefined;
563+
return undefined;
580564
}
581565
// Remembered git state is enough to rule a thread out; only an answer
582566
// that would settle it is confirmed against fresh git state.
583567
const remembered = yield* git.branchPullRequest(
584568
{ cwd, branch: thread.branch },
585569
{ refresh, confirmTerminal: false },
586570
);
587-
if (remembered === null) noPullRequestReadAtMs.set(key, nowMs);
588-
else noPullRequestReadAtMs.delete(key);
571+
noPullRequestRechecks.record(key, nowMs, remembered !== null);
589572
if (refresh || remembered === null || remembered.state === "open") return remembered;
590573
if (!(yield* wouldSettle(group, remembered))) return undefined;
591574
return yield* git.branchPullRequest({ cwd, branch: thread.branch }, { freshGitState: true });
@@ -612,11 +595,7 @@ export const make = Effect.gen(function* () {
612595
),
613596
{ concurrency: 8, discard: true },
614597
);
615-
if (threadId === undefined) {
616-
for (const key of noPullRequestReadAtMs.keys()) {
617-
if (!groups.has(key)) noPullRequestReadAtMs.delete(key);
618-
}
619-
}
598+
if (threadId === undefined) noPullRequestRechecks.retain(groups);
620599
});
621600

622601
const runSweep = (
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const RECHECK_MIN_MS = 5 * 60 * 1_000;
2+
const RECHECK_MAX_MS = 24 * 60 * 60 * 1_000;
3+
const RECHECK_IDLE_DIVISOR = 12;
4+
5+
/**
6+
* When a scheduled sweep last found no pull request for a branch. Sweeps wait on this instead of
7+
* GitManager's caches, which expire in minutes and evict past a few thousand branches.
8+
*/
9+
export function makeNoPullRequestRechecks() {
10+
const readAtMs = new Map<string, number>();
11+
return {
12+
/** A thread idle for a day is reread every two hours; one idle twelve days or more, daily. */
13+
waiting: (key: string, nowMs: number, activityAtMs: number): boolean => {
14+
const lastReadAtMs = readAtMs.get(key);
15+
if (lastReadAtMs === undefined) return false;
16+
const delayMs = Math.min(
17+
Math.max((nowMs - activityAtMs) / RECHECK_IDLE_DIVISOR, RECHECK_MIN_MS),
18+
RECHECK_MAX_MS,
19+
);
20+
return nowMs - lastReadAtMs < delayMs;
21+
},
22+
record: (key: string, nowMs: number, pullRequestFound: boolean): void => {
23+
if (pullRequestFound) readAtMs.delete(key);
24+
else readAtMs.set(key, nowMs);
25+
},
26+
retain: (keys: { readonly has: (key: string) => boolean }): void => {
27+
for (const key of readAtMs.keys()) {
28+
if (!keys.has(key)) readAtMs.delete(key);
29+
}
30+
},
31+
};
32+
}

0 commit comments

Comments
 (0)