diff --git a/apps/server/src/vcs/VcsStatusBroadcaster.test.ts b/apps/server/src/vcs/VcsStatusBroadcaster.test.ts index d30997266c7b7..9d8b2bf2117b3 100644 --- a/apps/server/src/vcs/VcsStatusBroadcaster.test.ts +++ b/apps/server/src/vcs/VcsStatusBroadcaster.test.ts @@ -717,7 +717,10 @@ describe("VcsStatusBroadcaster", () => { return Effect.gen(function* () { const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster; const initial = yield* broadcaster.getStatus({ cwd: "/repo" }); - const refreshed = yield* broadcaster.refreshStatus("/repo", { localOnly: true }); + const { refsDigest, ...refreshed } = yield* broadcaster.refreshStatus("/repo", { + localOnly: true, + }); + assert.isString(refsDigest); assert.deepStrictEqual(refreshed, initial); assert.deepStrictEqual(yield* broadcaster.getStatus({ cwd: "/repo" }), refreshed); assert.equal(state.localInvalidationCalls, 1); diff --git a/apps/server/src/vcs/VcsStatusBroadcaster.ts b/apps/server/src/vcs/VcsStatusBroadcaster.ts index 5842478fffe76..92a83c0230f71 100644 --- a/apps/server/src/vcs/VcsStatusBroadcaster.ts +++ b/apps/server/src/vcs/VcsStatusBroadcaster.ts @@ -736,21 +736,27 @@ export const make = Effect.gen(function* () { )(function* (rawCwd, options) { const cwd = yield* withFileSystem(normalizeCwd(rawCwd)); yield* acquireBranchChangesDemand(cwd); - const refsBeforeLocalRead = options?.localOnly ? null : yield* workflow.readRefSnapshot(cwd); - yield* refreshLocalStatusCore(cwd); + // Lets the client keep its cached branch list when no ref moved since its last refresh. + const withRefsDigest = (status: VcsStatusResult, refs: string | null) => + refs === null + ? status + : { + ...status, + refsDigest: NodeCrypto.createHash("sha256").update(refs).digest("hex").slice(0, 32), + }; if (options?.localOnly) { + const [refs] = yield* Effect.all( + [workflow.readRefSnapshot(cwd), refreshLocalStatusCore(cwd)], + { concurrency: "unbounded" }, + ); // Never cache a remote here: readers take `value: null` as "read and found no PR". const parts = yield* getStatusParts(cwd); - return mergeGitStatusParts(parts.local, parts.remote); + return withRefsDigest(mergeGitStatusParts(parts.local, parts.remote), refs); } + const refsBeforeLocalRead = yield* workflow.readRefSnapshot(cwd); + yield* refreshLocalStatusCore(cwd); const { status, refs } = yield* refreshRemoteAndLocalTotals(cwd, rawCwd, refsBeforeLocalRead); - // Lets the client keep its cached branch list when no ref moved since its last refresh. - return refs === null - ? status - : { - ...status, - refsDigest: NodeCrypto.createHash("sha256").update(refs).digest("hex").slice(0, 32), - }; + return withRefsDigest(status, refs); }, Effect.scoped); const refreshAfterCheckout: VcsStatusBroadcaster["Service"]["refreshAfterCheckout"] = Effect.fn( diff --git a/packages/client-runtime/src/state/vcs.test.ts b/packages/client-runtime/src/state/vcs.test.ts index f58b0d120598d..edc005263b432 100644 --- a/packages/client-runtime/src/state/vcs.test.ts +++ b/packages/client-runtime/src/state/vcs.test.ts @@ -464,6 +464,13 @@ describe("cached VCS refs", () => { expect(yield* Ref.get(clears)).toBe(3); expect(registry.get(vcsRefsCacheStateAtom(TARGET)).revision).toBe(3); + yield* atoms.refreshLocalStatusForPicker.run(registry, { + environmentId: TARGET.environmentId, + input: { cwd: "/repo", localOnly: true }, + }); + expect(yield* Ref.get(clears)).toBe(3); + expect(registry.get(vcsRefsCacheStateAtom(TARGET)).revision).toBe(3); + const prepareResult = yield* Effect.exit( createGitEnvironmentAtoms(runtime).preparePullRequestThread.run(registry, { environmentId: TARGET.environmentId, @@ -485,6 +492,7 @@ describe("cached VCS refs", () => { [WS_METHODS.vcsRefreshStatus]: () => Deferred.succeed(refreshStarted, undefined).pipe( Effect.andThen(Deferred.await(releaseRefresh)), + Effect.as({}), ), [WS_METHODS.vcsSwitchRef]: () => Effect.succeed({ refName: "feature" }), } as unknown as WsRpcProtocolClient; diff --git a/packages/client-runtime/src/state/vcs.ts b/packages/client-runtime/src/state/vcs.ts index a6299aab0ff64..dab79e282a4d2 100644 --- a/packages/client-runtime/src/state/vcs.ts +++ b/packages/client-runtime/src/state/vcs.ts @@ -2,6 +2,7 @@ import { type EnvironmentId, type VcsListRefsInput, type VcsListRefsResult, + type VcsRefreshStatusResult, type VcsStatusResult, WS_METHODS, } from "@t3tools/contracts"; @@ -294,6 +295,17 @@ export function createVcsEnvironmentAtoms( cwd: target.input.cwd, }); + const invalidateRefsIfChanged = ( + target: { readonly environmentId: EnvironmentId; readonly input: { readonly cwd: string } }, + registry: AtomRegistry.AtomRegistry, + exit: Exit.Exit, + ) => + invalidateCachedVcsRefsIfChanged( + registry, + { environmentId: target.environmentId, cwd: target.input.cwd }, + Exit.isSuccess(exit) ? exit.value.refsDigest : undefined, + ); + return { listRefs, // An uncached read for decisions that must match the repository now, not @@ -341,19 +353,14 @@ export function createVcsEnvironmentAtoms( tag: WS_METHODS.vcsRefreshStatus, scheduler: vcsCommandScheduler, concurrency: vcsCommandConcurrency, - onSettled: (target, registry, exit) => - invalidateCachedVcsRefsIfChanged( - registry, - { environmentId: target.environmentId, cwd: target.input.cwd }, - Exit.isSuccess(exit) ? exit.value.refsDigest : undefined, - ), + onSettled: invalidateRefsIfChanged, }), // Outside the serial queue: it only reads status, and a checkout picked right after opening // the picker must not wait behind that read. refreshLocalStatusForPicker: createEnvironmentRpcCommand(runtime, { label: "environment-data:vcs:refresh-local-status-for-picker", tag: WS_METHODS.vcsRefreshStatus, - onSettled: invalidateRefs, + onSettled: invalidateRefsIfChanged, }), createWorktree: createEnvironmentRpcCommand(runtime, { label: "environment-data:vcs:create-worktree",