Skip to content

Commit 80bd4c8

Browse files
t3dotggclaude
andcommitted
perf(server): restarts no longer replay PR discovery for every old thread
Each boot marked every settled branch thread without a saved PR for discovery. A "no PR" answer is not saved, so every restart repeated the whole pass: 4-6 git spawns per thread plus a gh call, retried up to 5 times. With gh missing, the scale bench measured about 3,850 git spawns/min, 17k spans/min and 24 MB/min of trace for about 5 min after each boot. The backfill came from #10101 as a one-time migration. Limit it to threads settled in the last 7 days (BACKFILL_WINDOW). The check uses settledAt, not updatedAt: auto-settle stamps updatedAt with the settle time, so a mass auto-settle would put every old thread back in the window. A branch or link change on a settled thread still gets its own single-thread pass. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 92f0af2 commit 80bd4c8

2 files changed

Lines changed: 46 additions & 4 deletions

File tree

‎apps/server/src/orchestration/ThreadPullRequestReactor.test.ts‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ import {
1717
} from "@t3tools/contracts";
1818
import { describe, expect, it } from "@effect/vitest";
1919
import * as Crypto from "effect/Crypto";
20+
import * as DateTime from "effect/DateTime";
2021
import * as Deferred from "effect/Deferred";
22+
import * as Duration from "effect/Duration";
2123
import * as Effect from "effect/Effect";
2224
import * as FileSystem from "effect/FileSystem";
2325
import * as Layer from "effect/Layer";
@@ -644,6 +646,34 @@ describe("ThreadPullRequestReactor", () => {
644646
),
645647
);
646648

649+
it.effect("skips the startup backfill for threads older than the backfill window", () =>
650+
Effect.scoped(
651+
Effect.gen(function* () {
652+
const old = DateTime.formatIso(
653+
DateTime.makeUnsafe(
654+
Date.parse(NOW) - Duration.toMillis(ThreadPullRequestReactor.BACKFILL_WINDOW) - 1,
655+
),
656+
);
657+
const fixture = yield* makeHarness({
658+
threads: [
659+
thread("recent", { branch: "recent", settledAt: NOW }),
660+
thread("old", { branch: "old", settledAt: old, updatedAt: old }),
661+
// Auto-settle keeps the last activity as settledAt but stamps updatedAt now.
662+
thread("auto-settled", { branch: "auto-settled", settledAt: old }),
663+
],
664+
branchPullRequest: () => Effect.succeed(branchPullRequest()),
665+
});
666+
yield* TestClock.setTime(Date.parse(NOW));
667+
yield* Effect.gen(function* () {
668+
yield* fixture.start();
669+
expect((yield* Ref.get(fixture.commands)).map((command) => command.threadId)).toEqual([
670+
"recent",
671+
]);
672+
}).pipe(Effect.provide(fixture.layer));
673+
}),
674+
),
675+
);
676+
647677
it.effect("matches Azure SSH projects to HTTPS PRs with the provider repository selector", () =>
648678
Effect.scoped(
649679
Effect.gen(function* () {

‎apps/server/src/orchestration/ThreadPullRequestReactor.ts‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ import {
1212
} from "@t3tools/contracts";
1313
import { makeDrainableWorker } from "@t3tools/shared/DrainableWorker";
1414
import * as Cause from "effect/Cause";
15+
import * as Clock from "effect/Clock";
1516
import * as Context from "effect/Context";
1617
import * as Crypto from "effect/Crypto";
18+
import * as Duration from "effect/Duration";
1719
import * as Effect from "effect/Effect";
1820
import * as FileSystem from "effect/FileSystem";
1921
import * as Layer from "effect/Layer";
@@ -54,6 +56,14 @@ function samePullRequest(
5456
/** Startup lookups per settled thread before discovery gives up on it. */
5557
export const BACKFILL_ATTEMPTS = 5;
5658

59+
/**
60+
* Startup discovery only covers threads settled this recently. Use settledAt,
61+
* not updatedAt: auto-settle stamps updatedAt with the settle time, so a mass
62+
* auto-settle would put every old thread back in the window. Older threads
63+
* were checked while active or on an earlier boot.
64+
*/
65+
export const BACKFILL_WINDOW = Duration.days(7);
66+
5767
interface RefreshRequest {
5868
readonly threadId: ThreadId | null;
5969
readonly refresh: boolean;
@@ -110,9 +120,9 @@ export const make = Effect.gen(function* () {
110120
const repositoryIdentities = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
111121
const crypto = yield* Crypto.Crypto;
112122
const fileSystem = yield* FileSystem.FileSystem;
113-
// Settled threads get one link discovery at startup. Failed lookups retry on
114-
// the periodic pass a few times, then stop until the thread changes or the
115-
// server restarts, so a missing or logged-out CLI cannot loop forever.
123+
// Recently settled threads get one link discovery at startup. Failed lookups
124+
// retry on the periodic pass a few times, then stop until the thread changes
125+
// or the server restarts, so a missing or logged-out CLI cannot loop forever.
116126
const pendingBackfill = new Map<ThreadId, number>();
117127
const finishBackfill = (threads: ReadonlyArray<{ readonly id: ThreadId }>) => {
118128
for (const thread of threads) pendingBackfill.delete(thread.id);
@@ -132,10 +142,12 @@ export const make = Effect.gen(function* () {
132142
const snapshot = yield* readSweepSnapshot(snapshots, request.threadId);
133143
const projects = new Map(snapshot.projects.map((project) => [project.id, project]));
134144
if (request.backfill) {
145+
const cutoff = (yield* Clock.currentTimeMillis) - Duration.toMillis(BACKFILL_WINDOW);
135146
for (const thread of snapshot.threads) {
136147
if (
137148
(thread.settledOverride === "settled" || thread.settledAt !== null) &&
138-
thread.branchPullRequest == null
149+
thread.branchPullRequest == null &&
150+
Date.parse(thread.settledAt ?? thread.updatedAt) >= cutoff
139151
) {
140152
pendingBackfill.set(thread.id, BACKFILL_ATTEMPTS);
141153
}

0 commit comments

Comments
 (0)