diff --git a/apps/server/src/vcs/GitVcsDriverCore.test.ts b/apps/server/src/vcs/GitVcsDriverCore.test.ts index f7280e7a2..9d0aaa6ff 100644 --- a/apps/server/src/vcs/GitVcsDriverCore.test.ts +++ b/apps/server/src/vcs/GitVcsDriverCore.test.ts @@ -1264,6 +1264,49 @@ it.layer(TestLayer)("GitVcsDriver core integration", (it) => { }), ); + it.effect("shows branches a background fetch brought in to the next fresh listing", () => + Effect.gen(function* () { + const cwd = yield* makeTmpDir(); + const remote = yield* makeTmpDir("git-vcs-driver-remote-"); + const { initialBranch } = yield* initRepoWithCommit(cwd); + const driver = yield* GitVcsDriver.GitVcsDriver; + yield* git(remote, ["init", "--bare"]); + yield* git(cwd, ["remote", "add", "origin", remote]); + yield* git(cwd, ["push", "-u", "origin", initialBranch]); + yield* pushRemoteBranchFromPeer({ + remote, + baseBranch: initialBranch, + branch: "fetched-later", + subject: "remote branch", + fileName: "later.txt", + }); + + // A fresh listing starts the background fetch and is shared for a few + // seconds with the next fresh one. (It may already include the branch + // if the fetch beat its read.) + yield* driver.listRefs({ cwd, query: "fetched-later", refresh: true }); + yield* waitForGitOutput( + cwd, + ["branch", "--remotes", "--list", "origin/fetched-later"], + (output) => output.trim().length > 0, + ); + + // The fetch has landed (the server notes it a moment after git does): + // the shared listing from before it isn't handed out again, which + // would otherwise last until its few seconds ran out. + const startedAtMs = Date.now(); + let after = yield* driver.listRefs({ cwd, query: "fetched-later", refresh: true }); + while (after.refs.length === 0 && Date.now() - startedAtMs < 5_000) { + yield* Effect.promise(() => sleepRealTime(50)); + after = yield* driver.listRefs({ cwd, query: "fetched-later", refresh: true }); + } + assert.deepStrictEqual( + after.refs.map((ref) => ref.name), + ["origin/fetched-later"], + ); + }), + ); + it.effect("puts the branch named exactly what was searched for on the first page", () => Effect.gen(function* () { const cwd = yield* makeTmpDir(); diff --git a/apps/server/src/vcs/GitVcsDriverCore.ts b/apps/server/src/vcs/GitVcsDriverCore.ts index 576684926..96e1f3013 100644 --- a/apps/server/src/vcs/GitVcsDriverCore.ts +++ b/apps/server/src/vcs/GitVcsDriverCore.ts @@ -1198,6 +1198,8 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* // the snapshot implementation's ordering in this service constructor. let invalidateListRefsSnapshotByCommonDir = (_gitCommonDir: string): Effect.Effect => Effect.void; + let markRemoteRefsFetchedByCommonDir = (_gitCommonDir: string): Effect.Effect => + Effect.void; const executeRaw: GitVcsDriver.GitVcsDriverShape["execute"] = Effect.fnUntraced( function* (input) { @@ -1874,7 +1876,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* includeTags: cacheKey.includeTags, }), { discard: true, concurrency: "unbounded" }, - ).pipe(Effect.ensuring(invalidateListRefsSnapshotByCommonDir(cacheKey.gitCommonDir))); + ).pipe(Effect.ensuring(markRemoteRefsFetchedByCommonDir(cacheKey.gitCommonDir))); return true as const; }); @@ -4985,11 +4987,30 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* } }); + // A change made here (a branch made, switched or deleted): a listing read + // while it landed may predate it, so it reads again (resolveListRefsSnapshot). invalidateListRefsSnapshotByCommonDir = (gitCommonDir) => Effect.sync(() => { bumpListRefsGeneration(gitCommonDir); bumpListRefsEpoch(gitCommonDir); }); + // The background fetch of remote branches finished. The next listing reads + // them, but one already reading keeps its answer, as if it had come a + // moment sooner: reading again doubled the wait (seconds on a busy repository) + // for every listing a fetch landed during, roughly every two minutes. + markRemoteRefsFetchedByCommonDir = (gitCommonDir) => + Effect.suspend(() => { + bumpListRefsEpoch(gitCommonDir); + // Fresh reads are shared for a few seconds per generation; the one from + // before the fetch mustn't be handed out again. + return Cache.invalidate( + listRefsRefreshCache, + new ListRefsRefreshCacheKey({ + gitCommonDir, + generation: currentListRefsGeneration(gitCommonDir), + }), + ); + }); const invalidateListRefsSnapshot = (cwd: string): Effect.Effect => resolveGitCommonDir(cwd).pipe( Effect.flatMap(invalidateListRefsSnapshotByCommonDir),