Skip to content

Commit 73da637

Browse files
Yuruzuuclaude
andcommitted
perf(server): PR status comes back sooner after a branch switch
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 5823ea5 commit 73da637

8 files changed

Lines changed: 221 additions & 6 deletions

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2433,6 +2433,42 @@ it.layer(layerGitManagerTest)("GitManager", (it) => {
24332433
}),
24342434
);
24352435

2436+
it.effect("a checkout's invalidation keeps the branch's cached PR lookup", () =>
2437+
Effect.gen(function* () {
2438+
const repoDir = yield* makeTempDir("t3code-git-manager-");
2439+
yield* initRepo(repoDir);
2440+
yield* runGit(repoDir, ["checkout", "-b", "feature/pr-cached"]);
2441+
const remoteDir = yield* createBareRemote();
2442+
yield* runGit(repoDir, ["remote", "add", "origin", remoteDir]);
2443+
yield* runGit(repoDir, ["push", "-u", "origin", "feature/pr-cached"]);
2444+
2445+
const { manager } = yield* makeManager({
2446+
providerScenario: {
2447+
prListSequence: [
2448+
[
2449+
{
2450+
number: 215,
2451+
title: "Cached PR",
2452+
url: "https://github.com/pingdotgg/codething-mvp/pull/215",
2453+
baseRefName: "main",
2454+
headRefName: "feature/pr-cached",
2455+
},
2456+
],
2457+
[],
2458+
],
2459+
},
2460+
});
2461+
2462+
expect((yield* manager.status({ cwd: repoDir })).pr?.number).toBe(215);
2463+
2464+
yield* manager.invalidateStatus(repoDir, { keepPullRequestLookups: true });
2465+
expect((yield* manager.status({ cwd: repoDir })).pr?.number).toBe(215);
2466+
2467+
yield* manager.invalidateStatus(repoDir);
2468+
expect((yield* manager.status({ cwd: repoDir })).pr).toBeNull();
2469+
}),
2470+
);
2471+
24362472
it.effect(
24372473
"status does not reuse a stale PR after the branch is retargeted to a different upstream",
24382474
() =>

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,11 @@ export class GitManager extends Context.Service<
140140
) => Effect.Effect<GitBranchPullRequest | null, GitManagerServiceError>;
141141
readonly invalidateLocalStatus: (cwd: string) => Effect.Effect<void, never>;
142142
readonly invalidateRemoteStatus: (cwd: string) => Effect.Effect<void, never>;
143-
readonly invalidateStatus: (cwd: string) => Effect.Effect<void, never>;
143+
/** keepPullRequestLookups is for changes that cannot affect a PR, such as a checkout. */
144+
readonly invalidateStatus: (
145+
cwd: string,
146+
options?: { readonly keepPullRequestLookups?: boolean },
147+
) => Effect.Effect<void, never>;
144148
readonly resolvePullRequest: (
145149
input: GitPullRequestRefInput,
146150
) => Effect.Effect<GitResolvePullRequestResult, GitManagerServiceError>;
@@ -2665,10 +2669,10 @@ export const make = Effect.gen(function* () {
26652669
yield* invalidateRemoteStatusResultCache(cwd);
26662670
});
26672671
const invalidateStatus: GitManager["Service"]["invalidateStatus"] = Effect.fn("invalidateStatus")(
2668-
function* (cwd) {
2672+
function* (cwd, options) {
26692673
yield* invalidateLocalStatusResultCache(cwd);
26702674
yield* invalidateRemoteStatusResultCache(cwd);
2671-
yield* bumpPrLookupEpoch(cwd);
2675+
if (!options?.keepPullRequestLookups) yield* bumpPrLookupEpoch(cwd);
26722676
yield* forgetPrLookupGitState(cwd);
26732677
},
26742678
);

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ export class GitWorkflowService extends Context.Service<
5959
) => Effect.Effect<VcsStatusRemoteResult | null, GitManagerServiceError>;
6060
readonly invalidateLocalStatus: (cwd: string) => Effect.Effect<void, never>;
6161
readonly invalidateRemoteStatus: (cwd: string) => Effect.Effect<void, never>;
62-
readonly invalidateStatus: (cwd: string) => Effect.Effect<void, never>;
62+
readonly invalidateStatus: GitManager.GitManager["Service"]["invalidateStatus"];
63+
readonly statusUpstreamFetchCount: GitVcsDriver.GitVcsDriver["Service"]["statusUpstreamFetchCount"];
6364
readonly pullCurrentBranch: (cwd: string) => Effect.Effect<VcsPullResult, GitCommandError>;
6465
readonly runStackedAction: (
6566
input: GitRunStackedActionInput,
@@ -369,6 +370,7 @@ export const make = Effect.gen(function* () {
369370
invalidateLocalStatus: gitManager.invalidateLocalStatus,
370371
invalidateRemoteStatus: gitManager.invalidateRemoteStatus,
371372
invalidateStatus: gitManager.invalidateStatus,
373+
statusUpstreamFetchCount: git.statusUpstreamFetchCount,
372374
pullCurrentBranch: Effect.fn("GitWorkflowService.pullCurrentBranch")(function (cwd) {
373375
return ensureGitCommand("GitWorkflowService.pullCurrentBranch", cwd).pipe(
374376
Effect.andThen(git.pullCurrentBranch(cwd)),

‎apps/server/src/vcs/GitVcsDriver.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,8 @@ export class GitVcsDriver extends Context.Service<
310310
cwd: string,
311311
options?: GitRemoteStatusOptions,
312312
) => Effect.Effect<GitRemoteStatusDetails, GitCommandError>;
313+
/** Changes whenever a status read fetches this repository, so callers can tell if refs may have moved. */
314+
readonly statusUpstreamFetchCount: (cwd: string) => Effect.Effect<number>;
313315
readonly prepareCommitContext: (
314316
cwd: string,
315317
filePaths?: readonly string[],

‎apps/server/src/vcs/GitVcsDriverCore.test.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3386,6 +3386,31 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
33863386
}),
33873387
);
33883388

3389+
it.effect("counts a status read's upstream fetch only when one actually ran", () =>
3390+
Effect.gen(function* () {
3391+
const cwd = yield* makeTmpDir();
3392+
const remote = yield* makeTmpDir("git-vcs-driver-remote-");
3393+
const { initialBranch } = yield* initRepoWithCommit(cwd);
3394+
yield* git(remote, ["init", "--bare"]);
3395+
yield* git(cwd, ["remote", "add", "origin", remote]);
3396+
yield* git(cwd, ["push", "-u", "origin", initialBranch]);
3397+
3398+
const driver = yield* GitVcsDriver.GitVcsDriver;
3399+
const initial = yield* driver.statusUpstreamFetchCount(cwd);
3400+
3401+
yield* driver.statusDetailsRemote(cwd, { refreshUpstream: false });
3402+
assert.equal(yield* driver.statusUpstreamFetchCount(cwd), initial);
3403+
3404+
yield* driver.statusDetailsRemote(cwd);
3405+
const afterFetch = yield* driver.statusUpstreamFetchCount(cwd);
3406+
assert.equal(afterFetch, initial + 1);
3407+
3408+
// Still inside the refresh interval, so this read reuses the last fetch.
3409+
yield* driver.statusDetailsRemote(cwd);
3410+
assert.equal(yield* driver.statusUpstreamFetchCount(cwd), afterFetch);
3411+
}),
3412+
);
3413+
33893414
it.effect("does not start Git auto-maintenance from background upstream fetches", () =>
33903415
Effect.gen(function* () {
33913416
const cwd = yield* makeTmpDir();

‎apps/server/src/vcs/GitVcsDriverCore.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1621,10 +1621,20 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
16211621
}
16221622
}
16231623
});
1624+
const statusUpstreamFetches = new Map<string, number>();
16241625
const refreshStatusRemoteCacheEntry = Effect.fn("refreshStatusRemoteCacheEntry")(function* (
16251626
cacheKey: StatusRemoteRefreshCacheKey,
16261627
) {
16271628
return yield* fetchRemoteForStatus(cacheKey.gitCommonDir, cacheKey.remoteName).pipe(
1629+
// Counted on failure too: a fetch can move some refs before it fails.
1630+
Effect.ensuring(
1631+
Effect.sync(() => {
1632+
statusUpstreamFetches.set(
1633+
cacheKey.gitCommonDir,
1634+
(statusUpstreamFetches.get(cacheKey.gitCommonDir) ?? 0) + 1,
1635+
);
1636+
}),
1637+
),
16281638
Effect.as(true as const),
16291639
);
16301640
});
@@ -1640,6 +1650,13 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
16401650
),
16411651
});
16421652

1653+
const statusUpstreamFetchCount: GitVcsDriver.GitVcsDriver["Service"]["statusUpstreamFetchCount"] =
1654+
(cwd) =>
1655+
resolveGitCommonDir(cwd).pipe(
1656+
Effect.map((gitCommonDir) => statusUpstreamFetches.get(gitCommonDir) ?? 0),
1657+
Effect.orElseSucceed(() => 0),
1658+
);
1659+
16431660
const refreshStatusUpstreamIfStale = Effect.fn("refreshStatusUpstreamIfStale")(function* (
16441661
cwd: string,
16451662
) {
@@ -4936,6 +4953,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
49364953
statusDetails,
49374954
statusDetailsLocal,
49384955
statusDetailsRemote,
4956+
statusUpstreamFetchCount,
49394957
prepareCommitContext,
49404958
commit: Effect.fnUntraced(function* (cwd, subject, body, options) {
49414959
return yield* withListRefsInvalidation(cwd, commit(cwd, subject, body, options));

‎apps/server/src/vcs/VcsStatusBroadcaster.test.ts‎

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import type {
2323
VcsStatusStreamEvent,
2424
} from "@t3tools/contracts";
2525
import { GitCommandError, GitManagerError } from "@t3tools/contracts";
26+
import * as SourceControlRateLimit from "@t3tools/source-control-core/server/SourceControlRateLimit";
2627

2728
import * as VcsStatusBroadcaster from "./VcsStatusBroadcaster.ts";
2829
import * as BackgroundPolicy from "../background/BackgroundPolicy.ts";
@@ -72,6 +73,9 @@ function layerTestFor(state: {
7273
localInvalidationCalls: number;
7374
remoteInvalidationCalls: number;
7475
remoteStatusRefreshUpstreamValues?: Array<boolean | undefined>;
76+
remoteStatusInteractiveValues?: Array<boolean>;
77+
invalidateStatusKeepLookupValues?: Array<boolean | undefined>;
78+
upstreamFetchCount?: number;
7579
remoteStatusError?: GitManagerError;
7680
backgroundWorkEnabled?: boolean;
7781
automaticFetchInterval?: Effect.Effect<Duration.Duration>;
@@ -113,6 +117,13 @@ function layerTestFor(state: {
113117
return state.currentRemoteStatus;
114118
}),
115119
),
120+
Effect.tap(() =>
121+
Effect.gen(function* () {
122+
state.remoteStatusInteractiveValues?.push(
123+
yield* SourceControlRateLimit.Interactive,
124+
);
125+
}),
126+
),
116127
Effect.tap(() =>
117128
state.remoteStatusError ? Effect.fail(state.remoteStatusError) : Effect.void,
118129
),
@@ -126,11 +137,13 @@ function layerTestFor(state: {
126137
Effect.sync(() => {
127138
state.remoteInvalidationCalls += 1;
128139
}),
129-
invalidateStatus: () =>
140+
invalidateStatus: (_cwd, options) =>
130141
Effect.sync(() => {
131142
state.localInvalidationCalls += 1;
132143
state.remoteInvalidationCalls += 1;
144+
state.invalidateStatusKeepLookupValues?.push(options?.keepPullRequestLookups);
133145
}),
146+
statusUpstreamFetchCount: () => Effect.sync(() => state.upstreamFetchCount ?? 0),
134147
...(state.pullCurrentBranch ? { pullCurrentBranch: state.pullCurrentBranch } : {}),
135148
}),
136149
),
@@ -699,6 +712,93 @@ describe("VcsStatusBroadcaster", () => {
699712
}).pipe(Effect.provide(layerTestFor(state)), Effect.scoped);
700713
});
701714

715+
it.effect("a checkout's PR lookup is a user action that reuses cached lookups", () => {
716+
const state: Parameters<typeof layerTestFor>[0] = {
717+
currentLocalStatus: baseLocalStatus,
718+
currentRemoteStatus: baseRemoteStatus,
719+
localStatusCalls: 0,
720+
remoteStatusCalls: 0,
721+
localInvalidationCalls: 0,
722+
remoteInvalidationCalls: 0,
723+
remoteStatusInteractiveValues: [],
724+
invalidateStatusKeepLookupValues: [],
725+
};
726+
727+
return Effect.gen(function* () {
728+
const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster;
729+
const remotePublished = yield* Deferred.make<void>();
730+
const subscribed = yield* Deferred.make<void>();
731+
yield* broadcaster.streamStatus({ cwd: "/repo", includeRemote: false }).pipe(
732+
Stream.runForEach((event) => {
733+
if (event._tag === "snapshot") return Deferred.succeed(subscribed, undefined);
734+
return event._tag === "remoteUpdated" && event.remote !== null
735+
? Deferred.succeed(remotePublished, undefined)
736+
: Effect.void;
737+
}),
738+
Effect.forkScoped,
739+
);
740+
yield* Deferred.await(subscribed);
741+
742+
yield* broadcaster.refreshAfterCheckout("/repo");
743+
yield* Deferred.await(remotePublished);
744+
745+
assert.deepStrictEqual(state.remoteStatusInteractiveValues, [true]);
746+
assert.deepStrictEqual(state.invalidateStatusKeepLookupValues, [true]);
747+
}).pipe(Effect.provide(layerTestFor(state)), Effect.scoped);
748+
});
749+
750+
it.effect("a checkout re-reads local status only when a fetch ran", () => {
751+
const state: Parameters<typeof layerTestFor>[0] = {
752+
currentLocalStatus: baseLocalStatus,
753+
currentRemoteStatus: baseRemoteStatus,
754+
localStatusCalls: 0,
755+
remoteStatusCalls: 0,
756+
localInvalidationCalls: 0,
757+
remoteInvalidationCalls: 0,
758+
};
759+
760+
return Effect.gen(function* () {
761+
const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster;
762+
const subscribed = yield* Deferred.make<void>();
763+
const firstRemote = yield* Deferred.make<void>();
764+
const secondRemote = yield* Deferred.make<void>();
765+
yield* broadcaster.streamStatus({ cwd: "/repo", includeRemote: false }).pipe(
766+
Stream.runForEach((event) => {
767+
if (event._tag === "snapshot") return Deferred.succeed(subscribed, undefined);
768+
if (event._tag !== "remoteUpdated" || event.remote === null) return Effect.void;
769+
return event.remote.pr === null
770+
? Deferred.succeed(firstRemote, undefined)
771+
: Deferred.succeed(secondRemote, undefined);
772+
}),
773+
Effect.forkScoped,
774+
);
775+
yield* Deferred.await(subscribed);
776+
const readsBeforeCheckout = state.localStatusCalls;
777+
778+
yield* broadcaster.refreshAfterCheckout("/repo");
779+
yield* Deferred.await(firstRemote);
780+
assert.equal(state.localStatusCalls, readsBeforeCheckout + 1);
781+
782+
const fetchedLocal: VcsStatusLocalResult = {
783+
...baseLocalStatus,
784+
branchChanges: { baseRef: "origin/main", insertions: 0, deletions: 0 },
785+
};
786+
state.currentRemoteStatus = remoteStatusWithPr;
787+
state.beforeRemoteStatus = Effect.sync(() => {
788+
state.upstreamFetchCount = (state.upstreamFetchCount ?? 0) + 1;
789+
state.currentLocalStatus = fetchedLocal;
790+
});
791+
yield* broadcaster.refreshAfterCheckout("/repo");
792+
yield* Deferred.await(secondRemote);
793+
794+
assert.equal(state.localStatusCalls, readsBeforeCheckout + 3);
795+
assert.deepStrictEqual(
796+
(yield* broadcaster.getStatus({ cwd: "/repo" })).branchChanges,
797+
fetchedLocal.branchChanges,
798+
);
799+
}).pipe(Effect.provide(layerTestFor(state)), Effect.scoped);
800+
});
801+
702802
it.effect("an explicit refresh reads local totals after the fetch", () => {
703803
const state: Parameters<typeof layerTestFor>[0] = {
704804
currentLocalStatus: baseLocalStatus,

‎apps/server/src/vcs/VcsStatusBroadcaster.ts‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import type {
2424
} from "@t3tools/contracts";
2525
import { DEFAULT_AUTOMATIC_GIT_FETCH_INTERVAL } from "@t3tools/contracts";
2626
import { resolveServerBackgroundActivitySettings } from "@t3tools/shared/backgroundActivitySettings";
27+
import * as SourceControlRateLimit from "@t3tools/source-control-core/server/SourceControlRateLimit";
2728
import { mergeGitStatusParts } from "@t3tools/shared/git";
2829
import * as KeyedLock from "@t3tools/shared/KeyedLock";
2930
import { resolveProjectSettings } from "@t3tools/shared/projectSettings";
@@ -556,6 +557,33 @@ export const make = Effect.gen(function* () {
556557
return yield* refreshRemoteAndLocalTotals(cwd, rawCwd);
557558
});
558559

560+
const refreshRemoteAfterCheckout = Effect.fn("VcsStatusBroadcaster.refreshRemoteAfterCheckout")(
561+
function* (cwd: string, policyCwd: string) {
562+
yield* withRemoteWriteLock(
563+
cwd,
564+
Effect.gen(function* () {
565+
const refreshUpstream = !Duration.isZero(yield* automaticFetchInterval);
566+
yield* workflow.invalidateStatus(cwd, { keepPullRequestLookups: true });
567+
const fetchesBefore = yield* workflow.statusUpstreamFetchCount(cwd);
568+
const remoteRead = yield* readRemoteStatus(cwd, { refreshUpstream });
569+
// Only a fetch can move the base used for local Changes totals.
570+
if ((yield* workflow.statusUpstreamFetchCount(cwd)) !== fetchesBefore) {
571+
yield* refreshLocalStatusCore(cwd);
572+
}
573+
// Without a fetch, the cached behind count cannot justify an automatic pull.
574+
const pulled = refreshUpstream
575+
? yield* maybeAutoPull(cwd, remoteRead, [policyCwd])
576+
: null;
577+
if (pulled === null) {
578+
yield* updateCachedRemoteStatus(cwd, remoteRead, { publish: true });
579+
}
580+
}),
581+
);
582+
},
583+
// Someone just picked this branch, so its PR lookup skips the background batching delay.
584+
Effect.provideService(SourceControlRateLimit.Interactive, true),
585+
);
586+
559587
const refreshAfterCheckout: VcsStatusBroadcaster["Service"]["refreshAfterCheckout"] = Effect.fn(
560588
"VcsStatusBroadcaster.refreshAfterCheckout",
561589
)(function* (rawCwd) {
@@ -564,7 +592,7 @@ export const make = Effect.gen(function* () {
564592
// Owned by the broadcaster so a client disconnecting mid-switch still leaves status correct.
565593
yield* refreshLocalStatusCore(cwd).pipe(
566594
Effect.ensuring(Deferred.succeed(localDone, undefined)),
567-
Effect.andThen(refreshRemoteAndLocalTotals(cwd, rawCwd)),
595+
Effect.andThen(refreshRemoteAfterCheckout(cwd, rawCwd)),
568596
Effect.ignoreCause({ log: true }),
569597
Effect.forkIn(broadcasterScope),
570598
);

0 commit comments

Comments
 (0)