Skip to content

Commit 901ebf2

Browse files
fix(server): background PR checks spend less GitHub quota
Branch PR lookups without an open PR now stay cached for 5 minutes instead of 1, so idle branches stop costing a GraphQL call on every sweep. In-app paths that can open a PR (turn end, push, create PR, user refresh) still bypass the cache. The settlement sweep only re-checks a branch uncached when the linked PR is terminal and the group would actually settle. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 6975efd commit 901ebf2

4 files changed

Lines changed: 105 additions & 9 deletions

File tree

‎apps/server/src/git/GitManager.test.ts‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import * as PlatformError from "effect/PlatformError";
1515
import * as References from "effect/References";
1616
import * as Schema from "effect/Schema";
1717
import * as Scope from "effect/Scope";
18+
import { TestClock } from "effect/testing";
1819
import { ChildProcessSpawner } from "effect/unstable/process";
1920
import { expect } from "vite-plus/test";
2021
import type {
@@ -1490,6 +1491,53 @@ it.layer(GitManagerTestLayer)("GitManager", (it) => {
14901491
}),
14911492
);
14921493

1494+
it.effect("branch PR lookup rechecks open PRs every minute and settled answers less often", () =>
1495+
Effect.gen(function* () {
1496+
const repoDir = yield* makeTempDir("t3code-git-manager-");
1497+
yield* initRepo(repoDir);
1498+
const remoteDir = yield* createBareRemote();
1499+
yield* runGit(repoDir, ["remote", "add", "origin", remoteDir]);
1500+
for (const branch of ["feature/open-pr", "feature/merged-pr", "feature/no-pr"]) {
1501+
yield* runGit(repoDir, ["checkout", "-b", branch, "main"]);
1502+
yield* runGit(repoDir, ["push", "-u", "origin", branch]);
1503+
}
1504+
const pullRequest = (number: number, headRefName: string, state: string) => ({
1505+
number,
1506+
title: headRefName,
1507+
url: `https://github.com/pingdotgg/codething-mvp/pull/${number}`,
1508+
baseRefName: "main",
1509+
headRefName,
1510+
state,
1511+
updatedAt: "2026-04-07T15:00:00Z",
1512+
});
1513+
const { manager, ghCalls } = yield* makeManager({
1514+
ghScenario: {
1515+
prListByHeadSelector: {
1516+
"feature/open-pr": encodeCliJson([pullRequest(301, "feature/open-pr", "OPEN")]),
1517+
"feature/merged-pr": encodeCliJson([pullRequest(302, "feature/merged-pr", "MERGED")]),
1518+
},
1519+
},
1520+
});
1521+
const lookupAll = Effect.forEach(
1522+
["feature/open-pr", "feature/merged-pr", "feature/no-pr"],
1523+
(branch) => manager.branchPullRequest({ cwd: repoDir, branch }),
1524+
);
1525+
const prListCalls = () => ghCalls.filter((call) => call.startsWith("pr list "));
1526+
1527+
yield* lookupAll;
1528+
expect(prListCalls()).toHaveLength(3);
1529+
1530+
yield* TestClock.adjust("61 seconds");
1531+
yield* lookupAll;
1532+
expect(prListCalls()).toHaveLength(4);
1533+
expect(prListCalls().at(-1)).toContain("--head feature/open-pr");
1534+
1535+
yield* TestClock.adjust("5 minutes");
1536+
yield* lookupAll;
1537+
expect(prListCalls()).toHaveLength(7);
1538+
}),
1539+
);
1540+
14931541
it.effect("branch PR lookup propagates provider failures", () =>
14941542
Effect.gen(function* () {
14951543
const repoDir = yield* makeTempDir("t3code-git-manager-");

‎apps/server/src/git/GitManager.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,12 @@ const STATUS_RESULT_CACHE_CAPACITY = 2_048;
144144
// exponentially via prLookupFailureTtl, so throttling pressure still drops
145145
// under 429s instead of amplifying it.
146146
const PR_LOOKUP_CACHE_TTL = Duration.seconds(60);
147+
// Answers without an open PR ("no PR yet", merged, closed) only change when
148+
// someone opens a PR, and the paths that do that in-app (turn end, push,
149+
// create PR, user refresh) bypass this cache. Re-asking every minute for each
150+
// idle branch was the bulk of background GitHub quota use, so these wait out
151+
// a longer TTL and a PR opened outside the app shows up within minutes.
152+
const PR_LOOKUP_NO_OPEN_PR_CACHE_TTL = Duration.minutes(5);
147153
const PR_LOOKUP_FAILURE_BASE_TTL = Duration.seconds(20);
148154
const PR_LOOKUP_FAILURE_MAX_TTL = Duration.minutes(15);
149155
const PR_LOOKUP_CACHE_CAPACITY = 2_048;
@@ -1105,7 +1111,9 @@ export const make = Effect.gen(function* () {
11051111
timeToLive: (exit, key) => {
11061112
if (Exit.isSuccess(exit)) {
11071113
prLookupFailureStreakByKey.delete(key);
1108-
return PR_LOOKUP_CACHE_TTL;
1114+
return exit.value.latest?.state === "open"
1115+
? PR_LOOKUP_CACHE_TTL
1116+
: PR_LOOKUP_NO_OPEN_PR_CACHE_TTL;
11091117
}
11101118
return nextPrLookupFailureTtl(key);
11111119
},

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,16 @@ describe("ThreadSettlementReactor", () => {
547547
new Set((yield* Ref.get(fixture.commands)).map((command) => command.threadId)),
548548
new Set([ThreadId.make("retained-terminal"), ThreadId.make("foreign-branch-pr")]),
549549
);
550+
// Resumed threads would not settle from their merged PR, so their
551+
// shared "main" group must not pay for an uncached branch lookup.
552+
assert.deepStrictEqual(
553+
new Set((yield* Ref.get(fixture.branchCalls)).map((call) => call.branch)),
554+
new Set(["main", "reused", "foreign"]),
555+
);
556+
assert.strictEqual(
557+
(yield* Ref.get(fixture.branchCalls)).filter((call) => call.branch === "main").length,
558+
1,
559+
);
550560
}).pipe(Effect.provide(fixture.layer));
551561
}),
552562
),

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

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,30 @@ export const make = Effect.gen(function* () {
209209
};
210210
const groups = Map.groupBy(lookupCandidates, lookupKey);
211211

212+
const wouldSettle = Effect.fn("ThreadSettlementReactor.wouldSettle")(function* (
213+
group: ReadonlyArray<(typeof candidates)[number]>,
214+
pullRequest: SettlementPullRequest,
215+
) {
216+
const currentSettings = yield* settingsService.getSettings;
217+
const decisionNow = DateTime.formatIso(yield* DateTime.now);
218+
return group.some((thread) => {
219+
const { settings } = resolveProjectSettings(currentSettings, thread.projectId);
220+
return (
221+
resolveAutoSettlementAt({
222+
thread,
223+
pullRequest,
224+
now: decisionNow,
225+
autoSettleAfterDays: settings.sidebarAutoSettleAfterDays,
226+
autoSettleOnMerge: settings.sidebarAutoSettleOnMerge,
227+
}) !== null
228+
);
229+
});
230+
});
231+
212232
const pullRequestFor = Effect.fn("ThreadSettlementReactor.pullRequestFor")(function* (
213-
thread: (typeof candidates)[number],
233+
group: ReadonlyArray<(typeof candidates)[number]>,
214234
) {
235+
const thread = group[0]!;
215236
const reference = thread.linkedPullRequest ?? thread.branchPullRequest;
216237
if (reference != null) {
217238
const matchesMerge =
@@ -236,10 +257,23 @@ export const make = Effect.gen(function* () {
236257
},
237258
{ recoverTransientFailure: false },
238259
);
260+
const terminal = {
261+
state: summary.state,
262+
closedAt: summary.closedAt ?? null,
263+
mergedAt: summary.mergedAt ?? null,
264+
} satisfies SettlementPullRequest;
239265
const cwd = lookupCwdByThreadId.get(thread.id);
240-
if (summary.state !== "open" && thread.branch !== null && cwd !== undefined) {
266+
if (
267+
summary.state !== "open" &&
268+
thread.branch !== null &&
269+
cwd !== undefined &&
270+
(yield* wouldSettle(group, terminal))
271+
) {
241272
// A reused branch can already have a new open PR while discovery
242273
// is replacing its old link. Do not let settlement win that race.
274+
// Only pay for the uncached lookup when this sweep would otherwise
275+
// settle: a terminal link that settles nothing (resumed thread,
276+
// settle-on-merge off) would re-query the host every minute.
243277
const current = yield* git.branchPullRequest(
244278
{ cwd, branch: thread.branch },
245279
{ refresh: true },
@@ -253,11 +287,7 @@ export const make = Effect.gen(function* () {
253287
return current;
254288
}
255289
}
256-
return {
257-
state: summary.state,
258-
closedAt: summary.closedAt ?? null,
259-
mergedAt: summary.mergedAt ?? null,
260-
} satisfies SettlementPullRequest;
290+
return terminal;
261291
}
262292
if (thread.branch === null) return null;
263293
const cwd = lookupCwdByThreadId.get(thread.id);
@@ -271,7 +301,7 @@ export const make = Effect.gen(function* () {
271301
groups.values(),
272302
(group) =>
273303
Effect.gen(function* () {
274-
const pullRequest = yield* pullRequestFor(group[0]!);
304+
const pullRequest = yield* pullRequestFor(group);
275305
yield* Effect.forEach(group, (thread) => settleThread(thread, pullRequest), {
276306
discard: true,
277307
});

0 commit comments

Comments
 (0)