GeezerVison is showing github.com — perf(server): turn end reads upstream divergence once, not twice (#40… · pingdotgg/t3code@194025e
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/server/src/git/GitManager.ts +7-3Lines changed: 7 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -111,6 +111,8 @@ export interface GitLocalStatusOptions { | 111 | 111 |
| 112 | 112 | export interface GitRemoteStatusOptions extends GitVcsDriver.GitRemoteStatusOptions { | 113 | 113 | readonly refreshMissingPullRequest?: boolean; | | 114 | + /** Details the caller just read without fetching; used instead of reading them again. */ | | 115 | + readonly statusDetails?: GitVcsDriver.GitRemoteStatusDetails; | 114 | 116 | } | 115 | 117 |
| 116 | 118 | export type GitBranchPullRequest = NonNullable<VcsStatusResult["pr"]> & { | @@ -1603,9 +1605,11 @@ export const make = Effect.gen(function* () { | 1603 | 1605 | cwd: string, | 1604 | 1606 | options?: GitRemoteStatusOptions, | 1605 | 1607 | ) { | 1606 | | - const details = yield* gitCore | 1607 | | - .statusDetailsRemote(cwd, options) | 1608 | | - .pipe(Effect.catchIf(isNotGitRepositoryError, () => Effect.succeed(null))); | | 1608 | + const details = | | 1609 | + options?.statusDetails ?? | | 1610 | + (yield* gitCore | | 1611 | + .statusDetailsRemote(cwd, options) | | 1612 | + .pipe(Effect.catchIf(isNotGitRepositoryError, () => Effect.succeed(null)))); | 1609 | 1613 | if (details === null || !details.isRepo) { | 1610 | 1614 | return null; | 1611 | 1615 | } | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/git/GitWorkflowService.ts +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -59,6 +59,7 @@ export class GitWorkflowService extends Context.Service< | 59 | 59 | readonly refName: string; | 60 | 60 | readonly aheadCount: number; | 61 | 61 | readonly behindCount: number; | | 62 | + readonly details: GitVcsDriver.GitRemoteStatusDetails; | 62 | 63 | } | null, | 63 | 64 | GitManagerServiceError | 64 | 65 | >; | @@ -384,6 +385,7 @@ export const make = Effect.gen(function* () { | 384 | 385 | refName: details.branch, | 385 | 386 | aheadCount: details.aheadCount, | 386 | 387 | behindCount: details.behindCount, | | 388 | + details, | 387 | 389 | } | 388 | 390 | : null; | 389 | 391 | }), | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/vcs/VcsStatusBroadcaster.test.ts +46-9Lines changed: 46 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -78,6 +78,7 @@ function layerTestFor(state: { | 78 | 78 | localInvalidationCalls: number; | 79 | 79 | remoteInvalidationCalls: number; | 80 | 80 | remoteStatusRefreshUpstreamValues?: Array<boolean | undefined>; | | 81 | + remoteStatusReusedDetails?: Array<boolean>; | 81 | 82 | localStatusBranchChanges?: Array<boolean>; | 82 | 83 | refSnapshot?: string | null; | 83 | 84 | remoteStatusError?: GitManagerError; | @@ -144,22 +145,37 @@ function layerTestFor(state: { | 144 | 145 | readRefSnapshot: () => | 145 | 146 | Effect.sync(() => (state.refSnapshot === undefined ? "refs" : state.refSnapshot)), | 146 | 147 | upstreamDivergence: () => | 147 | | - Effect.sync(() => | 148 | | - state.currentRemoteStatus?.hasUpstream && state.currentLocalStatus.refName !== null | 149 | | - ? { | 150 | | - refName: state.currentLocalStatus.refName, | 151 | | - aheadCount: (state.currentDivergence ?? state.currentRemoteStatus).aheadCount, | 152 | | - behindCount: (state.currentDivergence ?? state.currentRemoteStatus).behindCount, | 153 | | - } | 154 | | - : null, | 155 | | - ), | | 148 | + Effect.sync(() => { | | 149 | + const refName = state.currentLocalStatus.refName; | | 150 | + if (!state.currentRemoteStatus?.hasUpstream || refName === null) return null; | | 151 | + const { aheadCount, behindCount } = | | 152 | + state.currentDivergence ?? state.currentRemoteStatus; | | 153 | + return { | | 154 | + refName, | | 155 | + aheadCount, | | 156 | + behindCount, | | 157 | + details: { | | 158 | + isRepo: true, | | 159 | + hasPrimaryRemote: true, | | 160 | + defaultBranch: "main", | | 161 | + isDefaultBranch: false, | | 162 | + branch: refName, | | 163 | + upstreamRef: `origin/${refName}`, | | 164 | + hasUpstream: true, | | 165 | + aheadCount, | | 166 | + behindCount, | | 167 | + aheadOfDefaultCount: aheadCount, | | 168 | + }, | | 169 | + }; | | 170 | + }), | 156 | 171 | remoteStatus: (_input, options) => | 157 | 172 | Effect.suspend(() => { | 158 | 173 | return (state.beforeRemoteStatus ?? Effect.void).pipe( | 159 | 174 | Effect.andThen( | 160 | 175 | Effect.sync(() => { | 161 | 176 | state.remoteStatusCalls += 1; | 162 | 177 | state.remoteStatusRefreshUpstreamValues?.push(options?.refreshUpstream); | | 178 | + state.remoteStatusReusedDetails?.push(options?.statusDetails !== undefined); | 163 | 179 | return state.currentRemoteStatus; | 164 | 180 | }), | 165 | 181 | ), | @@ -794,6 +810,27 @@ describe("VcsStatusBroadcaster", () => { | 794 | 810 | }, | 795 | 811 | ); | 796 | 812 |
| | 813 | + it.effect("the turn-end PR lookup reuses the upstream read it follows, once", () => { | | 814 | + const state: Parameters<typeof layerTestFor>[0] = { | | 815 | + currentLocalStatus: baseLocalStatus, | | 816 | + currentRemoteStatus: remoteStatusWithPr, | | 817 | + localStatusCalls: 0, | | 818 | + remoteStatusCalls: 0, | | 819 | + localInvalidationCalls: 0, | | 820 | + remoteInvalidationCalls: 0, | | 821 | + remoteStatusReusedDetails: [], | | 822 | + }; | | 823 | + | | 824 | + return Effect.gen(function* () { | | 825 | + const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster; | | 826 | + yield* broadcaster.getStatus({ cwd: "/repo" }); | | 827 | + yield* broadcaster.refreshLocalStatus("/repo"); | | 828 | + yield* broadcaster.refreshPullRequestStatus("/repo"); | | 829 | + yield* broadcaster.refreshPullRequestStatus("/repo"); | | 830 | + assert.deepStrictEqual(state.remoteStatusReusedDetails, [false, true, false]); | | 831 | + }).pipe(Effect.provide(layerTestFor(state))); | | 832 | + }); | | 833 | + | 797 | 834 | it.effect.each([0, 2])( | 798 | 835 | "refreshes merge divergence while a lookup ends with %s commits behind", | 799 | 836 | (behindAfterLookup) => { | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/vcs/VcsStatusBroadcaster.ts +16-1Lines changed: 16 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -37,6 +37,7 @@ import * as BackgroundPolicy from "../background/BackgroundPolicy.ts"; | 37 | 37 | import * as GitWorkflowService from "../git/GitWorkflowService.ts"; | 38 | 38 | import * as ProjectStore from "../orchestration-v2/ProjectStore.ts"; | 39 | 39 | import * as ServerSettings from "../serverSettings.ts"; | | 40 | +import type * as GitVcsDriver from "./GitVcsDriver.ts"; | 40 | 41 | import { VcsAutomaticFetchInterval } from "./VcsAutomaticFetchInterval.ts"; | 41 | 42 |
| 42 | 43 | const VCS_STATUS_REFRESH_FAILURE_BASE_DELAY = Duration.seconds(30); | @@ -330,6 +331,9 @@ export const make = Effect.gen(function* () { | 330 | 331 | // A refresh that started after a request saw every change that request could mean, so queued | 331 | 332 | // callers share it instead of each running git status again. | 332 | 333 | const localRefreshRequests = new Map<string, number>(); | | 334 | + // Upstream details the turn-end refreshLocalStatus just read, handed once to the PR lookup that | | 335 | + // follows it so the same no-fetch read is not repeated. Any remote write in between drops them. | | 336 | + const upstreamReads = new Map<string, GitVcsDriver.GitRemoteStatusDetails>(); | 333 | 337 | const localRefreshResults = new Map< | 334 | 338 | string, | 335 | 339 | { | @@ -353,6 +357,7 @@ export const make = Effect.gen(function* () { | 353 | 357 | // A queued caller can read git again. Keep the monotonic request counter so it cannot | 354 | 358 | // mistake an older result for a later request. | 355 | 359 | localRefreshResults.delete(candidate); | | 360 | + upstreamReads.delete(candidate); | 356 | 361 | } | 357 | 362 | return cache; | 358 | 363 | }; | @@ -449,6 +454,7 @@ export const make = Effect.gen(function* () { | 449 | 454 |
| 450 | 455 | const updateCachedRemoteStatus = Effect.fn("VcsStatusBroadcaster.updateCachedRemoteStatus")( | 451 | 456 | function* (cwd: string, remote: CachedRemoteStatus | null, options?: { publish?: boolean }) { | | 457 | + upstreamReads.delete(cwd); | 452 | 458 | const [changed, value] = yield* Ref.modify(cacheRef, (cache) => { | 453 | 459 | const previous = cache.get(cwd) ?? { local: null, remote: null }; | 454 | 460 | const next = remote?.branch === previous.local?.value.refName ? remote : null; | @@ -556,6 +562,7 @@ export const make = Effect.gen(function* () { | 556 | 562 | "VcsStatusBroadcaster.refreshLocalStatus", | 557 | 563 | )(function* (rawCwd) { | 558 | 564 | const cwd = yield* withFileSystem(normalizeCwd(rawCwd)); | | 565 | + upstreamReads.delete(cwd); | 559 | 566 | const local = yield* refreshLocalStatusCore(cwd); | 560 | 567 | const cached = yield* getCachedStatus(cwd); | 561 | 568 | // Divergence is local Git data; a slow forge lookup must not delay turn-end updates. | @@ -577,6 +584,7 @@ export const make = Effect.gen(function* () { | 577 | 584 | }, | 578 | 585 | { publish: true }, | 579 | 586 | ); | | 587 | + upstreamReads.set(cwd, divergence.details); | 580 | 588 | } | 581 | 589 | } | 582 | 590 | return local; | @@ -590,7 +598,11 @@ export const make = Effect.gen(function* () { | 590 | 598 | // GitManager's remote TTL cache is keyed by cwd, not the checked-out branch. | 591 | 599 | yield* workflow.invalidateRemoteStatus(cwd); | 592 | 600 | const previousRemote = (yield* getCachedStatus(cwd))?.remote; | 593 | | - const remote = yield* workflow.remoteStatus({ cwd }, options); | | 601 | + const { statusDetails, ...remoteOptions } = options ?? {}; | | 602 | + const remote = yield* workflow.remoteStatus( | | 603 | + { cwd }, | | 604 | + statusDetails?.branch === branch ? { ...remoteOptions, statusDetails } : remoteOptions, | | 605 | + ); | 594 | 606 | const latest = yield* getCachedStatus(cwd); | 595 | 607 | // An overlapping turn refresh and fetch can both move divergence during the forge lookup. | 596 | 608 | const refreshed = | @@ -783,9 +795,12 @@ export const make = Effect.gen(function* () { | 783 | 795 | const cached = yield* getCachedStatus(cwd); | 784 | 796 | if (!cached || cached.remote?.value === null) return null; | 785 | 797 | // Turn completion must update divergence even when background polling is paused. | | 798 | + const statusDetails = upstreamReads.get(cwd); | | 799 | + upstreamReads.delete(cwd); | 786 | 800 | const remoteRead = yield* readRemoteStatus(cwd, { | 787 | 801 | refreshUpstream: false, | 788 | 802 | refreshMissingPullRequest: true, | | 803 | + ...(statusDetails ? { statusDetails } : {}), | 789 | 804 | }); | 790 | 805 | return yield* updateCachedRemoteStatus(cwd, remoteRead, { publish: true }); | 791 | 806 | }), | |
|