@devries-cameron20 / t3code Mirror · read-only / commits / a760f374f9
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 22, 05:45 UTC

perf(server): settled PR lookups stop re-spawning git every sweep (#29721)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Nov 3, 2026 at 10:32 UTC a760f374f9ffd8c2f053850f8a120832722ea2e2
2 files changed +141 -15
apps/server/src/git/GitManager.test.ts
+73
@@ -20,6 +20,7 @@ import * as PlatformError from "effect/PlatformError";
20 import * as Result from "effect/Result";
21 import * as Scope from "effect/Scope";
22 import * as Stream from "effect/Stream";
23 +import * as TestClock from "effect/testing/TestClock";
24 import { ChildProcessSpawner } from "effect/process";
25 import { expect } from "vite-plus/test";
26 import type {
@@ -1077,6 +1078,78 @@ it.layer(layerGitManagerTest)("GitManager", (it) => {
1078 }),
1079 );
1080
1081 + it.effect("a settled branch PR is rechecked only when the branch moves or every 15 minutes", () =>
1082 + Effect.gen(function* () {
1083 + const repoDir = yield* makeTempDir("t3code-git-manager-");
1084 + yield* initRepo(repoDir);
1085 + const remoteDir = yield* createBareRemote();
1086 + yield* runGit(repoDir, ["remote", "add", "origin", remoteDir]);
1087 + yield* runGit(repoDir, ["push", "-u", "origin", "main"]);
1088 + yield* runGit(repoDir, ["checkout", "-b", "feature/settled"]);
1089 + yield* runGit(repoDir, ["push", "-u", "origin", "feature/settled"]);
1090 + const pullRequest = (number: number, state: "open" | "merged"): FakeChangeRequest => ({
1091 + number,
1092 + title: "Settled",
1093 + url: `https://github.com/pingdotgg/codething-mvp/pull/${number}`,
1094 + baseRefName: "main",
1095 + headRefName: "feature/settled",
1096 + state,
1097 + updatedAt: `2026-04-0${number}T15:00:00Z`,
1098 + });
1099 + const gitCommands: string[] = [];
1100 + const { manager } = yield* makeManager({
1101 + gitCommands,
1102 + providerScenario: {
1103 + prListSequence: [
1104 + [pullRequest(1, "merged")],
1105 + [pullRequest(1, "merged")],
1106 + [pullRequest(1, "merged")],
1107 + ],
1108 + prListOtherwise: [pullRequest(2, "open")],
1109 + },
1110 + });
1111 + const lookup = manager.branchPullRequest({ cwd: repoDir, branch: "feature/settled" });
1112 + const freshLookups = () => gitCommands.filter((args) => args === "remote").length;
1113 +
1114 + expect((yield* lookup)?.state).toBe("merged");
1115 + gitCommands.length = 0;
1116 + expect((yield* lookup)?.state).toBe("merged");
1117 + expect((yield* lookup)?.state).toBe("merged");
1118 + expect(gitCommands.map((args) => args.split(" ", 1)[0]).toSorted()).toEqual([
1119 + "config",
1120 + "config",
1121 + "for-each-ref",
1122 + "for-each-ref",
1123 + ]);
1124 + expect(freshLookups()).toBe(0);
1125 +
1126 + NodeFS.writeFileSync(NodePath.join(repoDir, "settled.txt"), "new work\n");
1127 + yield* runGit(repoDir, ["add", "settled.txt"]);
1128 + yield* runGit(repoDir, ["commit", "-m", "New work"]);
1129 + gitCommands.length = 0;
1130 + expect((yield* lookup)?.state).toBe("merged");
1131 + expect(freshLookups()).toBe(1);
1132 + gitCommands.length = 0;
1133 + yield* lookup;
1134 + expect(freshLookups()).toBe(0);
1135 +
1136 + // Remembered git state expires after 5 minutes; that rereads git without a recheck.
1137 + yield* TestClock.adjust("10 minutes");
1138 + gitCommands.length = 0;
1139 + expect((yield* lookup)?.state).toBe("merged");
1140 + expect(freshLookups()).toBe(1);
1141 +
1142 + yield* TestClock.adjust("5 minutes");
1143 + gitCommands.length = 0;
1144 + expect((yield* lookup)?.state).toBe("merged");
1145 + expect(freshLookups()).toBe(2);
1146 +
1147 + // A new PR from the reused branch shows on the next host query.
1148 + yield* TestClock.adjust("5 minutes");
1149 + expect((yield* lookup)?.number).toBe(2);
1150 + }),
1151 + );
1152 +
1153 it.effect("branch PR lookup uses a saved tracked branch without changing checkout", () =>
1154 Effect.gen(function* () {
1155 const repoDir = yield* makeTempDir("t3code-git-manager-");
apps/server/src/git/GitManager.ts
+68 -15
@@ -171,6 +171,7 @@ const PR_LOOKUP_FAILURE_BASE_TTL = Duration.seconds(20);
171 const PR_LOOKUP_FAILURE_MAX_TTL = Duration.minutes(15);
172 const PR_LOOKUP_CACHE_CAPACITY = 2_048;
173 const PR_LOOKUP_GIT_STATE_TTL = Duration.minutes(5);
174 +const SETTLED_PR_LOOKUP_RECHECK_INTERVAL = Duration.minutes(15);
175 const PR_THREAD_CONTEXT_BYTES = 16_000;
176 const PR_THREAD_COMMAND_OUTPUT_CHARS = 2_000;
177 const isSourceControlProviderError = Schema.is(SourceControlProviderError);
@@ -2425,6 +2426,10 @@ export const make = Effect.gen(function* () {
2426 prLookupGitStateByKey.set(entryKey, { readAtMs, value });
2427 return value;
2428 });
2429 + const settledPrLookupByBranchKey = new Map<
2430 + string,
2431 + { readonly url: string; readonly fingerprint: string; readonly confirmedAtMs: number }
2432 + >();
2433 const forgetPrLookupGitState = (cwd: string) =>
2434 normalizeStatusCacheKey(cwd).pipe(
2435 Effect.map((cacheCwd) => {
@@ -2470,6 +2475,39 @@ export const make = Effect.gen(function* () {
2475 };
2476 },
2477 );
2478 + // objectname lets a settled PR result notice new commits on its branch.
2479 + const readPrLookupBranchRef = (cacheCwd: string, branch: string, fresh: boolean) =>
2480 + rememberPrLookupGitState(
2481 + cacheCwd,
2482 + `branchRef\u0000${branch}`,
2483 + fresh,
2484 + gitCore
2485 + .execute({
2486 + operation: "GitManager.branchPullRequest.branchRef",
2487 + cwd: cacheCwd,
2488 + args: [
2489 + "for-each-ref",
2490 + "--format=%(refname)%00%(upstream:short)%00%(upstream:remotename)%00%(upstream:remoteref)%00%(objectname)",
2491 + `refs/heads/${branch}`,
2492 + ],
2493 + })
2494 + .pipe(Effect.map((result) => result.stdout)),
2495 + );
2496 + const readSettledPrLookupFingerprint = (cacheCwd: string, branch: string) =>
2497 + Effect.all(
2498 + [
2499 + readPrLookupBranchRef(cacheCwd, branch, true),
2500 + gitCore
2501 + .execute({
2502 + operation: "GitManager.branchPullRequest.remoteUrls",
2503 + cwd: cacheCwd,
2504 + args: ["config", "--get-regexp", "^remote\\..*\\.url$"],
2505 + allowNonZeroExit: true,
2506 + })
2507 + .pipe(Effect.map((result) => result.stdout)),
2508 + ],
2509 + { concurrency: "unbounded" },
2510 + ).pipe(Effect.map((parts) => parts.join("\u0000")));
2511 const lookupBranchPullRequest = Effect.fn("lookupBranchPullRequest")(function* (
2512 cacheCwd: string,
2513 branch: string,
@@ -2493,20 +2531,7 @@ export const make = Effect.gen(function* () {
2531 .filter((remoteName) => remoteName.length > 0);
2532 const [firstRemoteName] = remoteNames;
2533 if (firstRemoteName === undefined) return null;
2496 - const branchRef = yield* remember(
2497 - `branchRef\u0000${branch}`,
2498 - gitCore
2499 - .execute({
2500 - operation: "GitManager.branchPullRequest.branchRef",
2501 - cwd: cacheCwd,
2502 - args: [
2503 - "for-each-ref",
2504 - "--format=%(refname)%00%(upstream:short)%00%(upstream:remotename)%00%(upstream:remoteref)",
2505 - `refs/heads/${branch}`,
2506 - ],
2507 - })
2508 - .pipe(Effect.map((result) => result.stdout)),
2509 - );
2534 + const branchRef = yield* readPrLookupBranchRef(cacheCwd, branch, options.freshGitState);
2535 const expectedRefName = `refs/heads/${branch}`;
2536 const exactBranch = (branchRef ?? "")
2537 .split("\n")
@@ -2657,10 +2682,38 @@ export const make = Effect.gen(function* () {
2682 freshGitState,
2683 });
2684 if (pullRequest === null || pullRequest.state === "open" || freshGitState) return pullRequest;
2660 - return yield* lookupBranchPullRequest(cacheCwd, branch, {
2685 + // A confirmed closed or merged result stays final until the branch or remotes change,
2686 + // with a periodic fresh lookup so a new PR from a reused branch still shows up.
2687 + const settledKey = `${cacheCwd}\u0000${branch}`;
2688 + const now = yield* Clock.currentTimeMillis;
2689 + const settled = settledPrLookupByBranchKey.get(settledKey);
2690 + // Read before any fresh lookup, so a change made during it forces another one.
2691 + const fingerprint = yield* readSettledPrLookupFingerprint(cacheCwd, branch);
2692 + if (
2693 + settled !== undefined &&
2694 + settled.url === pullRequest.url &&
2695 + settled.fingerprint === fingerprint &&
2696 + now - settled.confirmedAtMs < Duration.toMillis(SETTLED_PR_LOOKUP_RECHECK_INTERVAL)
2697 + ) {
2698 + return pullRequest;
2699 + }
2700 + const confirmed = yield* lookupBranchPullRequest(cacheCwd, branch, {
2701 refresh: false,
2702 freshGitState: true,
2703 });
2704 + settledPrLookupByBranchKey.delete(settledKey);
2705 + if (confirmed !== null && confirmed.state !== "open") {
2706 + if (settledPrLookupByBranchKey.size >= PR_LOOKUP_CACHE_CAPACITY) {
2707 + const oldestKey = settledPrLookupByBranchKey.keys().next().value;
2708 + if (oldestKey !== undefined) settledPrLookupByBranchKey.delete(oldestKey);
2709 + }
2710 + settledPrLookupByBranchKey.set(settledKey, {
2711 + url: confirmed.url,
2712 + fingerprint,
2713 + confirmedAtMs: now,
2714 + });
2715 + }
2716 + return confirmed;
2717 });
2718 const invalidateLocalStatus: GitManager["Service"]["invalidateLocalStatus"] = Effect.fn(
2719 "invalidateLocalStatus",