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 21, 18:30 UTC
perf(server): status refresh on a fork's default branch is 2.5x faster (median 7,826 to 3,168 ms) (#33954)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
Andrei committed
Nov 13, 2026 at 05:06 UTC
a1e0e507e1532c9ce79cce03f33b71a9822a3ccd
5 files changed
+160
-35
apps/server/src/git/GitManager.test.ts
+9
-6
@@ -94,8 +94,9 @@ interface FakeProviderScenario {
94
/** Check out the pull request head from its pull ref: fast-forward only unless forced. */
95
syncCheckoutWithPullHead?: boolean;
96
/**
97
- * The host's `headBranchProbe`. By default the fake probes only bare branch names, a hundred
98
- * at a time, so tests see GitManager defer to the hook; null leaves the probe to GitManager.
97
+ * The host's `headBranchProbe`. By default the fake probes bare branch names, a hundred at a
98
+ * time, or only the owner-qualified default branch, as GitHub does, so tests see GitManager
99
+ * defer to the hook; null leaves the probe to GitManager.
100
*/
101
headBranchProbe?: SourceControlProvider["Service"]["headBranchProbe"] | null;
102
}
@@ -438,8 +439,10 @@ function createFakeSourceControlProvider(scenario: FakeProviderScenario = {}): {
439
: {
440
headBranchProbe:
441
scenario.headBranchProbe ??
441
- (({ headSelectors }: { readonly headSelectors: ReadonlyArray<string> }) => ({
442
- headSelectors: headSelectors.filter((selector) => !selector.includes(":")),
442
+ (({ headSelectors, defaultBranchHeadSelector }) => ({
443
+ headSelectors: defaultBranchHeadSelector
444
+ ? [defaultBranchHeadSelector]
445
+ : headSelectors.filter((selector) => !selector.includes(":")),
446
limit: 100,
447
})),
448
};
@@ -2388,7 +2391,7 @@ it.layer(layerGitManagerTest)("GitManager", (it) => {
2391
const { manager, providerCalls } = yield* makeManager({
2392
providerScenario: {
2393
prListByHeadSelector: {
2391
- main: [
2394
+ "contributor:main": [
2395
{
2396
number: 777,
2397
title: "Fork PR from main",
@@ -2419,7 +2422,7 @@ it.layer(layerGitManagerTest)("GitManager", (it) => {
2422
});
2423
expect(providerCalls).toContainEqual({
2424
operation: "listChangeRequests",
2422
- headSelector: "main",
2425
+ headSelector: "contributor:main",
2426
state: "all",
2427
limit: 100,
2428
});
apps/server/src/git/GitManager.ts
+12
-3
@@ -575,9 +575,10 @@ function headBranchProbe(
575
provider: SourceControlProvider.SourceControlProvider["Service"],
576
headSelectors: ReadonlyArray<string>,
577
state: "open" | "all",
578
+ defaultBranchHeadSelector?: string,
579
) {
580
return (
580
- provider.headBranchProbe?.({ headSelectors, state }) ?? {
581
+ provider.headBranchProbe?.({ headSelectors, state, defaultBranchHeadSelector }) ?? {
582
headSelectors,
583
limit: state === "open" ? 1 : 20,
584
}
@@ -1266,7 +1267,7 @@ export const make = Effect.gen(function* () {
1267
) {
1268
return { latest: null, headContext };
1269
}
1269
- const latest = yield* findLatestPrForHeadContext(cwd, headContext);
1270
+ const latest = yield* findLatestPrForHeadContext(cwd, headContext, details.defaultBranch);
1271
if (latest !== null) yield* noteLookupState(latest);
1272
return { latest, headContext };
1273
});
@@ -1926,11 +1927,19 @@ export const make = Effect.gen(function* () {
1927
const findLatestPrForHeadContext = Effect.fn("findLatestPrForHeadContext")(function* (
1928
cwd: string,
1929
headContext: BranchHeadContext,
1930
+ defaultBranch: string | null,
1931
) {
1932
const parsedByNumber = new Map<number, PullRequestInfo>();
1933
1934
const provider = yield* sourceControlProvider(cwd);
1933
- const probe = headBranchProbe(provider, headContext.headSelectors, "all");
1935
+ const probe = headBranchProbe(
1936
+ provider,
1937
+ headContext.headSelectors,
1938
+ "all",
1939
+ headContext.headBranch === defaultBranch && headContext.headRepositoryOwnerLogin
1940
+ ? `${headContext.headRepositoryOwnerLogin}:${defaultBranch}`
1941
+ : undefined,
1942
+ );
1943
for (const headSelector of probe.headSelectors) {
1944
const pullRequests = yield* provider.listChangeRequests({
1945
cwd,
packages/source-control-core/src/server/SourceControlProvider.ts
+2
@@ -122,6 +122,8 @@ export class SourceControlProvider extends Context.Service<
122
readonly headBranchProbe?: (input: {
123
readonly headSelectors: ReadonlyArray<string>;
124
readonly state: "open" | "all";
125
+ /** `owner:branch` when the head is the default branch, a name most forks share. */
126
+ readonly defaultBranchHeadSelector?: string | undefined;
127
}) => { readonly headSelectors: ReadonlyArray<string>; readonly limit: number };
128
readonly readChangeRequestTemplate?: (input: {
129
readonly cwd: string;
packages/source-control-github/src/server/GitHubSourceControlProvider.test.ts
+47
-16
@@ -421,28 +421,59 @@ describe("GitHubSourceControlProvider repository resolution", () => {
421
describe("GitHubSourceControlProvider change request lookups by head", () => {
422
const remotes = remotesOutput(["origin", "git@github.com:acme/web.git"]);
423
424
- it.effect("matches an owner:branch selector on the head owner", () => {
425
- const { layer } = harness({
424
+ it.effect("filters an owner:branch selector by owner through REST", () => {
425
+ const paths: string[] = [];
426
+ const restNode = (number: number, mergedAt: string | null) => ({
427
+ number,
428
+ title: `PR ${number}`,
429
+ html_url: `https://github.com/acme/web/pull/${number}`,
430
+ state: mergedAt === null ? "open" : "closed",
431
+ merged_at: mergedAt,
432
+ closed_at: mergedAt,
433
+ updated_at: "2026-01-02T00:00:00Z",
434
+ base: { ref: "main", repo: { full_name: "acme/web" } },
435
+ head: {
436
+ ref: "main",
437
+ sha: "f".repeat(40),
438
+ repo: { name: "web", full_name: "me/web", owner: { login: "me", type: "User" } },
439
+ },
440
+ });
441
+ const { layer, graphqlCalls } = harness({
442
remotes,
427
- graphql: (lookup) => ({
428
- pullRequests: {
429
- nodes:
430
- lookup.head === "main"
431
- ? [node(9, "main", "someone"), node(8, "main", "me"), node(7, "main", "me")]
432
- : [],
433
- },
434
- }),
443
+ api: {
444
+ rest: (input) =>
445
+ Effect.sync(() => {
446
+ paths.push(input.path);
447
+ return restResponse([restNode(8, null), restNode(7, "2026-01-01T00:00:00Z")]);
448
+ }),
449
+ },
450
});
451
return Effect.gen(function* () {
452
const gh = yield* GitHubSourceControlProvider.make;
438
- const open = yield* gh
439
- .listChangeRequests({ cwd: "/repo", headSelector: "me:main", state: "open", limit: 1 })
440
- .pipe(Effect.forkChild);
441
- yield* TestClock.adjust("50 millis");
453
+ const probe = (defaultBranchHeadSelector?: string) =>
454
+ gh.headBranchProbe?.({
455
+ headSelectors: ["me:main", "main"],
456
+ state: "all",
457
+ defaultBranchHeadSelector,
458
+ }).headSelectors;
459
+ assert.deepStrictEqual([probe(), probe("me:main")], [["main"], ["me:main"]]);
460
+ const read = (state: "open" | "merged") =>
461
+ gh.listChangeRequests({ cwd: "/repo", headSelector: "me:main", state, limit: 1 });
462
+ const [open] = yield* read("open");
463
+ const [merged] = yield* read("merged");
464
+ assert.deepStrictEqual(
465
+ [open, merged].map((pr) => [pr?.number, pr?.state, pr?.headRepositoryOwnerLogin]),
466
+ [
467
+ [8, "open", "me"],
468
+ [7, "merged", "me"],
469
+ ],
470
+ );
471
assert.deepStrictEqual(
443
- (yield* Fiber.join(open)).map((pr) => pr.number),
444
- [8],
472
+ paths.map((path) => new URL(path, "https://api.test/").searchParams.get("state")),
473
+ ["open", "closed"],
474
);
475
+ assert.include(paths[0], "repos/acme/web/pulls?head=me%3Amain");
476
+ assert.lengthOf(graphqlCalls, 0);
477
}).pipe(Effect.provide(layer));
478
});
479
packages/source-control-github/src/server/GitHubSourceControlProvider.ts
+90
-10
@@ -394,6 +394,60 @@ function repositoryCloneUrls(
394
395
const decodeViewerLogin = decodeJsonResult(Schema.Struct({ login: TrimmedNonEmptyString }));
396
397
+const decodeRestPullRequests = decodeJsonResult(Schema.Array(Schema.Unknown));
398
+const decodeRestPullRequest = Schema.decodeUnknownOption(
399
+ Schema.Struct({
400
+ number: Schema.Number,
401
+ title: Schema.String,
402
+ html_url: Schema.String,
403
+ state: Schema.String,
404
+ draft: Schema.optional(Schema.Boolean),
405
+ merged_at: Schema.NullOr(Schema.String),
406
+ closed_at: Schema.NullOr(Schema.String),
407
+ updated_at: Schema.String,
408
+ base: Schema.Struct({ ref: Schema.String, repo: Schema.Struct({ full_name: Schema.String }) }),
409
+ head: Schema.Struct({
410
+ ref: Schema.String,
411
+ sha: Schema.String,
412
+ repo: Schema.NullOr(
413
+ Schema.Struct({
414
+ name: Schema.String,
415
+ full_name: Schema.String,
416
+ owner: Schema.Struct({ login: Schema.String, type: Schema.String }),
417
+ }),
418
+ ),
419
+ }),
420
+ }),
421
+);
422
+
423
+/** Reshapes a REST pull request into the GraphQL node so both decode the same way. */
424
+function restPullRequestNode(entry: unknown) {
425
+ const pr = Option.getOrNull(decodeRestPullRequest(entry));
426
+ if (pr === null) return null;
427
+ return {
428
+ number: pr.number,
429
+ title: pr.title,
430
+ url: pr.html_url,
431
+ baseRefName: pr.base.ref,
432
+ headRefName: pr.head.ref,
433
+ headRefOid: pr.head.sha,
434
+ state: pr.merged_at === null ? pr.state.toUpperCase() : "MERGED",
435
+ isDraft: pr.draft === true,
436
+ mergedAt: pr.merged_at,
437
+ closedAt: pr.closed_at,
438
+ updatedAt: pr.updated_at,
439
+ isCrossRepository: pr.head.repo?.full_name !== pr.base.repo.full_name,
440
+ headRepository: pr.head.repo && {
441
+ name: pr.head.repo.name,
442
+ nameWithOwner: pr.head.repo.full_name,
443
+ },
444
+ headRepositoryOwner: pr.head.repo && {
445
+ __typename: pr.head.repo.owner.type,
446
+ login: pr.head.repo.owner.login,
447
+ },
448
+ };
449
+}
450
+
451
type PullRequestListState = "open" | "closed" | "merged" | "all";
452
453
const GRAPHQL_STATES: Record<
@@ -567,6 +621,34 @@ export const make = Effect.gen(function* () {
621
const limit = Math.min(Math.max(Math.trunc(input.limit), 1), 100);
622
const ownerMatch = /^([^:/\s]+):(.+)$/u.exec(input.headSelector.trim());
623
const headRefName = ownerMatch?.[2] ?? input.headSelector.trim();
624
+ if (ownerMatch) {
625
+ // GraphQL cannot filter heads by owner, and names like a fork's `main` match thousands.
626
+ const query = new URLSearchParams({
627
+ head: `${ownerMatch[1]}:${headRefName}`,
628
+ state: input.state === "all" || input.state === "open" ? input.state : "closed",
629
+ sort: "created",
630
+ direction: "desc",
631
+ per_page: String(OWNER_HEAD_SCAN_LIMIT),
632
+ });
633
+ const response = yield* rest({
634
+ host: locator.host,
635
+ operation: "listPullRequestsByHead",
636
+ allowReserve: input.allowReserve,
637
+ path: `repos/${encodeURIComponent(locator.owner)}/${encodeURIComponent(locator.name)}/pulls?${query}`,
638
+ });
639
+ const decoded = decodeRestPullRequests(response.body);
640
+ if (Result.isFailure(decoded)) {
641
+ return yield* failure("GitHub returned an invalid change request list.", decoded.failure);
642
+ }
643
+ return decoded.success
644
+ .flatMap((entry) => {
645
+ const node = Option.getOrNull(decodePullRequestNode(restPullRequestNode(entry)));
646
+ const record = node === null ? null : toPullRequestRecord(node);
647
+ return record === null ? [] : [record];
648
+ })
649
+ .filter((row) => input.state === "all" || row.state === input.state)
650
+ .slice(0, limit);
651
+ }
652
const result = yield* graphqlRead(
653
{
654
host: locator.host,
@@ -582,22 +664,17 @@ export const make = Effect.gen(function* () {
664
name: locator.name,
665
head: headRefName,
666
states: GRAPHQL_STATES[input.state],
585
- first: ownerMatch ? OWNER_HEAD_SCAN_LIMIT : limit,
667
+ first: limit,
668
},
669
input.allowReserve ? interactiveHeadResolver : backgroundHeadResolver,
670
),
671
);
672
if (result.repository === null) return yield* notFound("The repository could not be read.");
591
- const rows = (result.repository.pullRequests.nodes ?? []).flatMap((entry) => {
673
+ return (result.repository.pullRequests.nodes ?? []).flatMap((entry) => {
674
const node = Option.getOrNull(decodePullRequestNode(entry));
675
const record = node === null ? null : toPullRequestRecord(node);
676
return record === null ? [] : [record];
677
});
596
- if (!ownerMatch) return rows;
597
- const headOwner = ownerMatch[1]!.toLowerCase();
598
- return rows
599
- .filter((row) => row.headRepositoryOwnerLogin?.toLowerCase() === headOwner)
600
- .slice(0, limit);
678
});
679
680
const readPullRequest = Effect.fn("GitHubSourceControlProvider.readPullRequest")(
@@ -884,9 +961,12 @@ export const make = Effect.gen(function* () {
961
resolveRemoteWebHost: Effect.fn("GitHubSourceControlProvider.resolveRemoteWebHost")(
962
({ context }) => Effect.succeed(gitHubApiHostForRemote(context.remoteUrl)),
963
),
887
- // GitHub matches bare heads; read a full page to disambiguate forks by owner.
888
- headBranchProbe: ({ headSelectors }) => ({
889
- headSelectors: headSelectors.filter((selector) => !selector.includes(":")),
964
+ // GitHub matches bare heads; read a full page to disambiguate forks by owner. A default branch
965
+ // collides with most forks, so its owner-qualified lookup has GitHub filter by owner instead.
966
+ headBranchProbe: ({ headSelectors, defaultBranchHeadSelector }) => ({
967
+ headSelectors: defaultBranchHeadSelector
968
+ ? [defaultBranchHeadSelector]
969
+ : headSelectors.filter((selector) => !selector.includes(":")),
970
limit: 100,
971
}),
972
readChangeRequestTemplate: Effect.fn("GitHubSourceControlProvider.readChangeRequestTemplate")(