@devries-cameron20 / t3code Mirror · read-only / commits / 52f4ec6f94
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, 12:00 UTC

perf(gitcafe): PR sync makes 2x fewer GitCafe requests for unchanged PRs (#27100)

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

Julius Marminge committed Oct 29, 2026 at 13:35 UTC 52f4ec6f9420abc8f4c033528f95397fd2716f0b
2 files changed +97 -35
packages/source-control-gitcafe/src/server/GitCafePullRequestProvider.test.ts
+48 -21
@@ -1226,43 +1226,70 @@ describe("GitCafePullRequestProvider", () => {
1226
1227 it.effect("recounts summary lines only when the compared commits move", () => {
1228 const landedBase = "fedcba9876543210fedcba9876543210fedcba98";
1229 + const mainTip = "0123012301230123012301230123012301230123";
1230 let comparisonBaseOid = baseOid;
1231 + let tips = { targetBranch: "main", observedBaseOid: baseOid };
1232 let pullReads = 0;
1233 + let listingCut = true;
1234 + let hunksDown = false;
1235 + const hunks = (request: SentRequest) => ({
1236 + version: 4,
1237 + headOid,
1238 + comparisonBaseOid,
1239 + items: (request.body as { paths: ReadonlyArray<string> }).paths.map((path) => ({
1240 + path,
1241 + additions: comparisonBaseOid === baseOid ? 3 : 1,
1242 + deletions: 1,
1243 + })),
1244 + });
1245 const server = fakeGitCafe({
1246 // A comment moves the version without moving the compared commits.
1233 - "GET /repos/owner/repo/pulls/7": () => ({ ...pull, version: 4 + pullReads++ }),
1234 - "GET /repos/owner/repo/pulls/7/changes": () => ({
1235 - version: 4,
1236 - headOid,
1237 - comparisonBaseOid,
1238 - items: [{ path: "a.ts" }, { path: "b.ts" }],
1239 - next: null,
1240 - }),
1241 - "POST /repos/owner/repo/pulls/7/diff-files": (request: SentRequest) => ({
1247 + "GET /repos/owner/repo/pulls/7": () => ({ ...pull, ...tips, version: 4 + pullReads++ }),
1248 + "GET /repos/owner/repo/pulls/7/changes": (request: SentRequest) => ({
1249 version: 4,
1250 headOid,
1251 comparisonBaseOid,
1245 - items: (request.body as { paths: ReadonlyArray<string> }).paths.map((path) => ({
1246 - path,
1247 - additions: comparisonBaseOid === baseOid ? 3 : 1,
1248 - deletions: 1,
1249 - })),
1252 + ...(!listingCut
1253 + ? { items: [{ path: "a.ts" }, { path: "b.ts" }], next: null }
1254 + : request.query.has("after")
1255 + ? { items: [], next: "more" }
1256 + : { items: [{ path: "a.ts" }], next: "more" }),
1257 }),
1258 + get "POST /repos/owner/repo/pulls/7/diff-files"() {
1259 + return hunksDown
1260 + ? new Response(encodeJson({ type: "https://cafe.sh/errors/conflict" }), { status: 409 })
1261 + : hunks;
1262 + },
1263 });
1252 - const hunkReads = () =>
1253 - server.sent.filter((request) => request.path.endsWith("/diff-files")).length;
1264 + const reads = (suffix: string) =>
1265 + server.sent.filter((request) => request.path.endsWith(suffix)).length;
1266 return Effect.gen(function* () {
1267 const provider = yield* GitCafePullRequestProvider.make;
1268 + // A listing that stops before its last page holds nothing for its commits.
1269 + yield* provider.getChangeRequestSummary!(target);
1270 + listingCut = false;
1271 + hunksDown = true;
1272 + // Unread hunks leave the lines unknown, and are read again next time.
1273 + const unread = yield* provider.getChangeRequestSummary!(target);
1274 + assert.strictEqual(unread.additions, undefined);
1275 + hunksDown = false;
1276 const first = yield* provider.getChangeRequestSummary!(target);
1277 const second = yield* provider.getChangeRequestSummary!(target);
1278 assert.deepInclude(first, { additions: 6, deletions: 2, changedFiles: 2 });
1279 assert.deepInclude(second, { additions: 6, deletions: 2, changedFiles: 2 });
1260 - assert.strictEqual(hunkReads(), 1);
1261 - // A landed parent layer moves the comparison base under the same head.
1262 - comparisonBaseOid = landedBase;
1280 + // Neither the head nor the target tip moved, so the file list is not read again.
1281 + assert.deepStrictEqual([reads("/changes"), reads("/diff-files")], [4, 3]);
1282 + // The target branch advancing keeps the merge base, so only the file list is read.
1283 + tips = { ...tips, observedBaseOid: mainTip };
1284 const third = yield* provider.getChangeRequestSummary!(target);
1264 - assert.deepInclude(third, { additions: 2, deletions: 2, changedFiles: 2 });
1265 - assert.strictEqual(hunkReads(), 2);
1285 + assert.deepInclude(third, { additions: 6, deletions: 2, changedFiles: 2 });
1286 + assert.deepStrictEqual([reads("/changes"), reads("/diff-files")], [5, 3]);
1287 + // A landed parent layer retargets the pull and moves the comparison base under the same head.
1288 + comparisonBaseOid = landedBase;
1289 + tips = { targetBranch: "trunk", observedBaseOid: mainTip };
1290 + const fourth = yield* provider.getChangeRequestSummary!(target);
1291 + assert.deepInclude(fourth, { additions: 2, deletions: 2, changedFiles: 2 });
1292 + assert.deepStrictEqual([reads("/changes"), reads("/diff-files")], [6, 4]);
1293 }).pipe(Effect.provide(server.layer));
1294 });
1295
packages/source-control-gitcafe/src/server/GitCafePullRequestProvider.ts
+49 -14
@@ -636,11 +636,19 @@ export const make = Effect.gen(function* () {
636 // so hunk totals are held per compared pair of commits. The file list that names the pair is
637 // cheap; a retarget or a landed parent layer moves the base without moving the head.
638 const lineTotals = new Map<string, { readonly additions: number; readonly deletions: number }>();
639 + // The compared pair follows from the head and the target tip the pull reports, so while neither
640 + // moves the stats are reused without listing the files again.
641 + const statsByTips = new Map<
642 + string,
643 + { readonly changedFiles: number; readonly additions?: number; readonly deletions?: number }
644 + >();
645 + const remember = <K, V>(cache: Map<K, V>, key: K, value: V) => {
646 + if (cache.size >= LINE_STATS_CACHE_SIZE) cache.delete(cache.keys().next().value!);
647 + cache.set(key, value);
648 + };
649
640 - const readLineStats = Effect.fn("GitCafePullRequestProvider.readLineStats")(function* (
641 - input: PullRef,
642 - version: number,
643 - ) {
650 + // readLineStats owns the span.
651 + const countLines = Effect.fnUntraced(function* (input: PullRef, version: number) {
652 const path = yield* pullPath("getChangeRequest", input);
653 const files: Array<Json.GitCafeDiffFile> = [];
654 let revision: { version: number; headOid: string; baseOid: string } | undefined;
@@ -667,13 +675,23 @@ export const make = Effect.gen(function* () {
675 after = batch.next;
676 if (after === null || after === previous || batch.items.length === 0) break;
677 }
670 - if (revision === undefined) return { changedFiles: 0, additions: 0, deletions: 0 };
678 + if (revision === undefined)
679 + return {
680 + headOid: null,
681 + complete: true,
682 + stats: { changedFiles: 0, additions: 0, deletions: 0 },
683 + };
684 const key = `${input.host} ${input.repository.toLowerCase()} ${input.number} ${revision.baseOid} ${revision.headOid}`;
685 const held = lineTotals.get(key);
673 - if (held !== undefined) return { changedFiles: files.length, ...held };
686 + const { headOid } = revision;
687 + // A listing that stopped before its last page counts only part of the diff, so nothing is held.
688 + const listed = after === null;
689 + if (held !== undefined)
690 + return { headOid, complete: listed, stats: { changedFiles: files.length, ...held } };
691 + const tooMany = files.length > LINE_STATS_MAX_FILES;
692 const details = files.some((file) => file.additions !== undefined)
693 ? Option.some(files)
676 - : files.length > LINE_STATS_MAX_FILES
694 + : tooMany
695 ? Option.none()
696 : yield* Effect.forEach(
697 chunk(detailablePaths(files), DIFF_FILES_BATCH),
@@ -684,16 +702,33 @@ export const make = Effect.gen(function* () {
702 Effect.option,
703 );
704 // Unread hunks leave the lines unknown rather than zero; the file count still stands.
687 - if (Option.isNone(details)) return { changedFiles: files.length };
705 + if (Option.isNone(details))
706 + return { headOid, complete: listed && tooMany, stats: { changedFiles: files.length } };
707 const counted = details.value;
708 const totals = {
709 additions: counted.reduce((total, file) => total + (file.additions ?? 0), 0),
710 deletions: counted.reduce((total, file) => total + (file.deletions ?? 0), 0),
711 };
693 - if (lineTotals.size >= LINE_STATS_CACHE_SIZE)
694 - lineTotals.delete(lineTotals.keys().next().value!);
695 - lineTotals.set(key, totals);
696 - return { changedFiles: files.length, ...totals };
712 + if (listed) remember(lineTotals, key, totals);
713 + return { headOid, complete: listed, stats: { changedFiles: files.length, ...totals } };
714 + });
715 +
716 + const readLineStats = Effect.fn("GitCafePullRequestProvider.readLineStats")(function* (
717 + input: PullRef,
718 + pull: Json.GitCafePullDetail,
719 + ) {
720 + const tipsKey =
721 + pull.headOid === null || pull.observedBaseOid == null
722 + ? null
723 + : `${input.host} ${input.repository.toLowerCase()} ${input.number} ${pull.targetBranch} ${pull.observedBaseOid} ${pull.headOid}`;
724 + const heldStats = tipsKey === null ? undefined : statsByTips.get(tipsKey);
725 + if (heldStats !== undefined) return heldStats;
726 + const stats = yield* countLines(input, pull.version);
727 + // A failed hunk read is retried next time rather than held as unknown, and a head that moved
728 + // between the two reads would hold another pair's stats.
729 + if (tipsKey !== null && stats.complete && stats.headOid === pull.headOid)
730 + remember(statsByTips, tipsKey, stats.stats);
731 + return stats.stats;
732 });
733
734 const merge = Effect.fn("GitCafePullRequestProvider.merge")(function* (
@@ -870,7 +905,7 @@ export const make = Effect.gen(function* () {
905 const changes =
906 pull.headOid === null
907 ? null
873 - : yield* readLineStats(input, pull.version).pipe(Effect.orElseSucceed(() => null));
908 + : yield* readLineStats(input, pull).pipe(Effect.orElseSucceed(() => null));
909 // A count that could not be read is absent rather than zero.
910 const {
911 additions: _additions,
@@ -904,7 +939,7 @@ export const make = Effect.gen(function* () {
939 ),
940 pull.headOid === null
941 ? Effect.succeed(null)
907 - : readLineStats(input, pull.version).pipe(Effect.orElseSucceed(() => null)),
942 + : readLineStats(input, pull).pipe(Effect.orElseSucceed(() => null)),
943 // Reviewers need `pr:review:read`, which a contents-only token lacks.
944 readPages("getChangeRequest", input, `${path}/reviewers`, Json.GitCafeActorEntries).pipe(
945 // @effect-diagnostics-next-line catchConditionalRefailToCatchIf:off - catchIf widens this paginated result to any in tsc-rs.