Skip to content
TypeScript

fix(server): background PR checks spend less GitHub quota (#13189)

Julius Marminge5975ec78b7ea
diff --git a/apps/server/src/git/GitManager.test.ts b/apps/server/src/git/GitManager.test.ts
index 96d91ec170e..a502506ef9f 100644
--- a/apps/server/src/git/GitManager.test.ts
+++ b/apps/server/src/git/GitManager.test.ts
@@ -15,6 +15,7 @@ import * as PlatformError from "effect/PlatformError";
import * as References from "effect/References";
import * as Schema from "effect/Schema";
import * as Scope from "effect/Scope";
+import { TestClock } from "effect/testing";
import { ChildProcessSpawner } from "effect/unstable/process";
import { expect } from "vite-plus/test";
import type {
@@ -1490,6 +1491,61 @@ it.layer(GitManagerTestLayer)("GitManager", (it) => {
}),
);
+ it.effect("branch PR lookup rechecks open PRs every minute and settled answers less often", () =>
+ Effect.gen(function* () {
+ const repoDir = yield* makeTempDir("t3code-git-manager-");
+ yield* initRepo(repoDir);
+ const remoteDir = yield* createBareRemote();
+ yield* runGit(repoDir, ["remote", "add", "origin", remoteDir]);
+ for (const branch of ["feature/open-pr", "feature/merged-pr", "feature/no-pr"]) {
+ yield* runGit(repoDir, ["checkout", "-b", branch, "main"]);
+ yield* runGit(repoDir, ["push", "-u", "origin", branch]);
+ }
+ const pullRequest = (number: number, headRefName: string, state: string) => ({
+ number,
+ title: headRefName,
+ url: `https://github.com/pingdotgg/codething-mvp/pull/${number}`,
+ baseRefName: "main",
+ headRefName,
+ state,
+ updatedAt: "2026-04-07T15:00:00Z",
+ });
+ const { manager, ghCalls } = yield* makeManager({
+ ghScenario: {
+ prListByHeadSelector: {
+ "feature/open-pr": encodeCliJson([pullRequest(301, "feature/open-pr", "OPEN")]),
+ "feature/merged-pr": encodeCliJson([pullRequest(302, "feature/merged-pr", "MERGED")]),
+ },
+ },
+ });
+ const lookupAll = Effect.forEach(
+ ["feature/open-pr", "feature/merged-pr", "feature/no-pr"],
+ (branch) => manager.branchPullRequest({ cwd: repoDir, branch }),
+ );
+ const prListCalls = () => ghCalls.filter((call) => call.startsWith("pr list "));
+
+ yield* lookupAll;
+ expect(prListCalls()).toHaveLength(3);
+
+ yield* TestClock.adjust("61 seconds");
+ yield* lookupAll;
+ expect(prListCalls()).toHaveLength(4);
+ expect(prListCalls().at(-1)).toContain("--head feature/open-pr");
+
+ // Just inside the 5-minute window only the open PR is asked again.
+ yield* TestClock.adjust("238 seconds");
+ yield* lookupAll;
+ expect(prListCalls()).toHaveLength(5);
+ expect(prListCalls().at(-1)).toContain("--head feature/open-pr");
+
+ // Just past it the settled answers expire too.
+ yield* TestClock.adjust("2 seconds");
+ yield* lookupAll;
+ expect(prListCalls()).toHaveLength(7);
+ expect(prListCalls().slice(-2).join("\n")).not.toContain("--head feature/open-pr");
+ }),
+ );
+
it.effect("branch PR lookup propagates provider failures", () =>
Effect.gen(function* () {
const repoDir = yield* makeTempDir("t3code-git-manager-");
diff --git a/apps/server/src/git/GitManager.ts b/apps/server/src/git/GitManager.ts
index 820c6a781f8..dc2b8bcdec3 100644
--- a/apps/server/src/git/GitManager.ts
+++ b/apps/server/src/git/GitManager.ts
@@ -144,6 +144,12 @@ const STATUS_RESULT_CACHE_CAPACITY = 2_048;
// exponentially via prLookupFailureTtl, so throttling pressure still drops
// under 429s instead of amplifying it.
const PR_LOOKUP_CACHE_TTL = Duration.seconds(60);
+// Answers without an open PR ("no PR yet", merged, closed) only change when
+// someone opens a PR, and the paths that do that in-app (turn end, push,
+// create PR, user refresh) bypass this cache. Re-asking every minute for each
+// idle branch was the bulk of background GitHub quota use, so these wait out
+// a longer TTL and a PR opened outside the app shows up within minutes.
+const PR_LOOKUP_NO_OPEN_PR_CACHE_TTL = Duration.minutes(5);
const PR_LOOKUP_FAILURE_BASE_TTL = Duration.seconds(20);
const PR_LOOKUP_FAILURE_MAX_TTL = Duration.minutes(15);
const PR_LOOKUP_CACHE_CAPACITY = 2_048;
@@ -1105,7 +1111,9 @@ export const make = Effect.gen(function* () {
timeToLive: (exit, key) => {
if (Exit.isSuccess(exit)) {
prLookupFailureStreakByKey.delete(key);
- return PR_LOOKUP_CACHE_TTL;
+ return exit.value.latest?.state === "open"
+ ? PR_LOOKUP_CACHE_TTL
+ : PR_LOOKUP_NO_OPEN_PR_CACHE_TTL;
}
return nextPrLookupFailureTtl(key);
},
diff --git a/apps/server/src/orchestration/ThreadSettlementReactor.test.ts b/apps/server/src/orchestration/ThreadSettlementReactor.test.ts
index 314c0f87488..07afbaf05e6 100644
--- a/apps/server/src/orchestration/ThreadSettlementReactor.test.ts
+++ b/apps/server/src/orchestration/ThreadSettlementReactor.test.ts
@@ -475,6 +475,49 @@ describe("ThreadSettlementReactor", () => {
}),
),
);
+ it.effect("skips the branch recheck when a terminal link would settle nothing", () =>
+ Effect.scoped(
+ Effect.gen(function* () {
+ yield* TestClock.setTime(Date.parse(NOW));
+ const previous = {
+ projectId: PROJECT_ID,
+ repository: "owner/repository",
+ number: 1,
+ url: "https://example.test/owner/repository/pull/1",
+ };
+ const fixture = yield* makeHarness({
+ snapshot: makeSnapshot(
+ [
+ makeThread("resumed-manual", {
+ branch: "main",
+ linkedPullRequest: previous,
+ latestUserMessageAt: "2026-08-28T00:00:00.000Z",
+ }),
+ ],
+ [makeProject()],
+ ),
+ settings: {
+ ...DEFAULT_SERVER_SETTINGS,
+ sidebarAutoSettleAfterDays: null,
+ sidebarAutoSettleOnMerge: true,
+ },
+ branchPullRequest: () => Effect.succeed(makeBranchPullRequest("open")),
+ pullRequestSummary: (input) =>
+ Effect.succeed({
+ ...makePullRequestSummary({ ...input, state: "merged" }),
+ mergedAt: "2026-08-27T00:00:00.000Z",
+ }),
+ });
+ yield* Effect.gen(function* () {
+ const reactor = yield* ThreadSettlementReactor.ThreadSettlementReactor;
+ yield* startHarness(reactor, fixture.activation, fixture.snapshotReads);
+ assert.deepStrictEqual(yield* Ref.get(fixture.commands), []);
+ assert.strictEqual((yield* Ref.get(fixture.summaryCalls)).length, 1);
+ assert.deepStrictEqual(yield* Ref.get(fixture.branchCalls), []);
+ }).pipe(Effect.provide(fixture.layer));
+ }),
+ ),
+ );
it.effect("uses saved PRs without settling resumed threads or branches with newer PRs", () =>
Effect.scoped(
Effect.gen(function* () {
diff --git a/apps/server/src/orchestration/ThreadSettlementReactor.ts b/apps/server/src/orchestration/ThreadSettlementReactor.ts
index 0dec28a7df6..ff6f995df46 100644
--- a/apps/server/src/orchestration/ThreadSettlementReactor.ts
+++ b/apps/server/src/orchestration/ThreadSettlementReactor.ts
@@ -209,9 +209,30 @@ export const make = Effect.gen(function* () {
};
const groups = Map.groupBy(lookupCandidates, lookupKey);
+ const wouldSettle = Effect.fn("ThreadSettlementReactor.wouldSettle")(function* (
+ group: ReadonlyArray<(typeof candidates)[number]>,
+ pullRequest: SettlementPullRequest,
+ ) {
+ const currentSettings = yield* settingsService.getSettings;
+ const decisionNow = DateTime.formatIso(yield* DateTime.now);
+ return group.some((thread) => {
+ const { settings } = resolveProjectSettings(currentSettings, thread.projectId);
+ return (
+ resolveAutoSettlementAt({
+ thread,
+ pullRequest,
+ now: decisionNow,
+ autoSettleAfterDays: settings.sidebarAutoSettleAfterDays,
+ autoSettleOnMerge: settings.sidebarAutoSettleOnMerge,
+ }) !== null
+ );
+ });
+ });
+
const pullRequestFor = Effect.fn("ThreadSettlementReactor.pullRequestFor")(function* (
- thread: (typeof candidates)[number],
+ group: ReadonlyArray<(typeof candidates)[number]>,
) {
+ const thread = group[0]!;
const reference = thread.linkedPullRequest ?? thread.branchPullRequest;
if (reference != null) {
const matchesMerge =
@@ -236,10 +257,21 @@ export const make = Effect.gen(function* () {
},
{ recoverTransientFailure: false },
);
+ const terminal = {
+ state: summary.state,
+ closedAt: summary.closedAt ?? null,
+ mergedAt: summary.mergedAt ?? null,
+ } satisfies SettlementPullRequest;
const cwd = lookupCwdByThreadId.get(thread.id);
if (summary.state !== "open" && thread.branch !== null && cwd !== undefined) {
// A reused branch can already have a new open PR while discovery
// is replacing its old link. Do not let settlement win that race.
+ // Only pay for the uncached lookup when this sweep would otherwise
+ // settle: a terminal link that settles nothing (resumed thread,
+ // settle-on-merge off) would re-query the host every minute. A
+ // group that becomes eligible after this check waits for the next
+ // sweep rather than settling on the unverified link.
+ if (!(yield* wouldSettle(group, terminal))) return undefined;
const current = yield* git.branchPullRequest(
{ cwd, branch: thread.branch },
{ refresh: true },
@@ -253,11 +285,7 @@ export const make = Effect.gen(function* () {
return current;
}
}
- return {
- state: summary.state,
- closedAt: summary.closedAt ?? null,
- mergedAt: summary.mergedAt ?? null,
- } satisfies SettlementPullRequest;
+ return terminal;
}
if (thread.branch === null) return null;
const cwd = lookupCwdByThreadId.get(thread.id);
@@ -271,7 +299,8 @@ export const make = Effect.gen(function* () {
groups.values(),
(group) =>
Effect.gen(function* () {
- const pullRequest = yield* pullRequestFor(group[0]!);
+ const pullRequest = yield* pullRequestFor(group);
+ if (pullRequest === undefined) return;
yield* Effect.forEach(group, (thread) => settleThread(thread, pullRequest), {
discard: true,
});