From cee71b9bf73962cb97a760bde05966d66df7abdb Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sat, 21 Nov 2026 14:42:45 +0000 Subject: [PATCH] perf(client-runtime): Git menu status refresh keeps the branch list when no ref moved Every vcs.refreshStatus cleared the environment's persisted refs and made the mounted branch selector re-send its two listRefs with refresh, 9 git per Git menu open on Windows even when no ref changed. The server now returns a digest of the ref snapshot it already reads after the refresh, and the client clears the refs cache only when that digest differs from the one its last refresh saw for the cwd, or the refresh failed. Co-Authored-By: Claude Opus 5.5 --- .../src/vcs/VcsStatusBroadcaster.test.ts | 10 ++++-- apps/server/src/vcs/VcsStatusBroadcaster.ts | 31 +++++++++++++------ packages/client-runtime/src/state/runtime.ts | 3 +- packages/client-runtime/src/state/vcs.test.ts | 21 +++++++++++-- packages/client-runtime/src/state/vcs.ts | 9 +++++- .../src/state/vcsRefInvalidation.ts | 29 +++++++++++++++++ packages/contracts/src/git.ts | 7 +++++ packages/contracts/src/rpc.ts | 4 +-- 8 files changed, 94 insertions(+), 20 deletions(-) diff --git a/apps/server/src/vcs/VcsStatusBroadcaster.test.ts b/apps/server/src/vcs/VcsStatusBroadcaster.test.ts index 69c33b785bd41..d30997266c7b7 100644 --- a/apps/server/src/vcs/VcsStatusBroadcaster.test.ts +++ b/apps/server/src/vcs/VcsStatusBroadcaster.test.ts @@ -350,7 +350,7 @@ describe("VcsStatusBroadcaster", () => { yield* broadcaster.refreshLocalStatus(`/other-repo-${index}`); } yield* Deferred.succeed(releasePolicy, undefined); - const result = yield* Fiber.join(refresh); + const { refsDigest: _refsDigest, ...result } = yield* Fiber.join(refresh); assert.deepStrictEqual(result, { ...baseLocalStatus, ...state.currentRemoteStatus }); }).pipe(Effect.provide(layer), Effect.scoped); }); @@ -1568,8 +1568,9 @@ describe("VcsStatusBroadcaster", () => { yield* Deferred.await(subscribed); state.localStatusCalls = 0; - yield* broadcaster.refreshStatus("/repo"); + const unmoved = yield* broadcaster.refreshStatus("/repo"); assert.equal(state.localStatusCalls, 1); + assert.isString(unmoved.refsDigest); state.localStatusCalls = 0; state.currentRemoteStatus = baseRemoteStatus; @@ -1580,6 +1581,8 @@ describe("VcsStatusBroadcaster", () => { const moved = yield* broadcaster.refreshStatus("/repo"); assert.equal(state.localStatusCalls, 2); assert.deepStrictEqual(moved.branchChanges, totals(0).branchChanges); + assert.isString(moved.refsDigest); + assert.notEqual(moved.refsDigest, unmoved.refsDigest); assert.deepStrictEqual( [...(yield* Fiber.join(events))].map((event) => event._tag), // The first refresh adds the totals this palette-style subscription skipped. @@ -1588,8 +1591,9 @@ describe("VcsStatusBroadcaster", () => { state.localStatusCalls = 0; state.refSnapshot = null; - yield* broadcaster.refreshStatus("/repo"); + const unreadable = yield* broadcaster.refreshStatus("/repo"); assert.equal(state.localStatusCalls, 2); + assert.isUndefined(unreadable.refsDigest); }).pipe(Effect.provide(layerTestFor(state)), Effect.scoped); }); diff --git a/apps/server/src/vcs/VcsStatusBroadcaster.ts b/apps/server/src/vcs/VcsStatusBroadcaster.ts index 8a507e799c827..5842478fffe76 100644 --- a/apps/server/src/vcs/VcsStatusBroadcaster.ts +++ b/apps/server/src/vcs/VcsStatusBroadcaster.ts @@ -1,3 +1,6 @@ +// @effect-diagnostics-next-line nodeBuiltinImport:off -- A synchronous digest of the ref snapshot already in memory. +import * as NodeCrypto from "node:crypto"; + import * as Cause from "effect/Cause"; import * as Context from "effect/Context"; import * as Deferred from "effect/Deferred"; @@ -20,6 +23,7 @@ import type { VcsBranchStatus, VcsStatusLocalResult, VcsStatusRemoteResult, + VcsRefreshStatusResult, VcsStatusResult, VcsStatusStreamEvent, VcsStatusSubscriptionInput, @@ -231,7 +235,7 @@ export class VcsStatusBroadcaster extends Context.Service< readonly refreshStatus: ( cwd: string, options?: { readonly localOnly?: boolean }, - ) => Effect.Effect; + ) => Effect.Effect; readonly refreshPullRequestStatus: ( cwd: string, ) => Effect.Effect; @@ -711,17 +715,17 @@ export const make = Effect.gen(function* () { const pulled = refreshUpstream ? yield* maybeAutoPull(cwd, remoteRead, [policyCwd]) : null; - if (pulled !== null) return mergeGitStatusParts(pulled.local, pulled.remote); + if (pulled !== null) { + return { status: mergeGitStatusParts(pulled.local, pulled.remote), refs: null }; + } yield* updateCachedRemoteStatus(cwd, remoteRead, { publish: true }); // A fetch, ours or not, can move the base used for local Changes totals. - if ( - refsBeforeLocalRead === null || - (yield* workflow.readRefSnapshot(cwd)) !== refsBeforeLocalRead - ) { + const refs = refsBeforeLocalRead === null ? null : yield* workflow.readRefSnapshot(cwd); + if (refs === null || refs !== refsBeforeLocalRead) { yield* refreshLocalStatusCore(cwd); } const parts = yield* getStatusParts(cwd); - return mergeGitStatusParts(parts.local, parts.remote); + return { status: mergeGitStatusParts(parts.local, parts.remote), refs }; }), ); }, @@ -739,7 +743,14 @@ export const make = Effect.gen(function* () { const parts = yield* getStatusParts(cwd); return mergeGitStatusParts(parts.local, parts.remote); } - return yield* refreshRemoteAndLocalTotals(cwd, rawCwd, refsBeforeLocalRead); + 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), + }; }, Effect.scoped); const refreshAfterCheckout: VcsStatusBroadcaster["Service"]["refreshAfterCheckout"] = Effect.fn( @@ -947,7 +958,7 @@ export const make = Effect.gen(function* () { // A request made during a refresh needs a read that starts after it, so overlapping requests // share one follow-up. Keyed by raw cwd because auto-pull policy depends on it. // Missing means idle; null means running with no follow-up queued. - type SharedRefresh = Deferred.Deferred; + type SharedRefresh = Deferred.Deferred; const queuedRefreshesRef = yield* Ref.make(new Map()); const runQueuedRefreshes = Effect.fn("VcsStatusBroadcaster.runQueuedRefreshes")(function* ( @@ -974,7 +985,7 @@ export const make = Effect.gen(function* () { "VcsStatusBroadcaster.shareQueuedRefreshStatus", )(function* (rawCwd, options) { if (options?.localOnly) return yield* refreshStatus(rawCwd, options); - const requested = yield* Deferred.make(); + const requested = yield* Deferred.make(); // Registering and forking together keeps an interrupt from leaving an entry with no worker. const shared = yield* Effect.uninterruptible( Effect.gen(function* () { diff --git a/packages/client-runtime/src/state/runtime.ts b/packages/client-runtime/src/state/runtime.ts index f96da6e04ed21..f91f838db78b0 100644 --- a/packages/client-runtime/src/state/runtime.ts +++ b/packages/client-runtime/src/state/runtime.ts @@ -1150,6 +1150,7 @@ export function createEnvironmentRpcCommand< readonly input: NoInfer; }, registry: AtomRegistry.AtomRegistry, + exit: Exit.Exit, unknown>, ) => Effect.Effect; }, ) { @@ -1180,7 +1181,7 @@ export function createEnvironmentRpcCommand< Effect.tap( (result) => options.onSuccess?.(target, registry, result, started) ?? Effect.void, ), - Effect.ensuring(options.onSettled?.(target, registry) ?? Effect.void), + Effect.onExit((exit) => options.onSettled?.(target, registry, exit) ?? Effect.void), ); }, }); diff --git a/packages/client-runtime/src/state/vcs.test.ts b/packages/client-runtime/src/state/vcs.test.ts index 5626c8eef10eb..f58b0d120598d 100644 --- a/packages/client-runtime/src/state/vcs.test.ts +++ b/packages/client-runtime/src/state/vcs.test.ts @@ -384,13 +384,15 @@ describe("cached VCS refs", () => { it.effect("invalidates persisted refs when ref-affecting commands settle", () => Effect.gen(function* () { + const refsDigest = yield* Ref.make("refs-a"); const expectedError = new EnvironmentRpcUnavailableError({ environmentId: TARGET.environmentId, message: "pull failed after fetching refs", }); const client = { [WS_METHODS.vcsPull]: () => Effect.fail(expectedError), - [WS_METHODS.vcsRefreshStatus]: () => Effect.void, + [WS_METHODS.vcsRefreshStatus]: () => + Ref.get(refsDigest).pipe(Effect.map((digest) => ({ refsDigest: digest }))), [WS_METHODS.gitPreparePullRequestThread]: () => Effect.succeed({ branch: "feature/pr", worktreePath: null }), } as unknown as WsRpcProtocolClient; @@ -449,6 +451,19 @@ describe("cached VCS refs", () => { expect(yield* Ref.get(clears)).toBe(2); expect(registry.get(vcsRefsCacheStateAtom(TARGET)).revision).toBe(2); + const refreshStatus = atoms.refreshStatus.run(registry, { + environmentId: TARGET.environmentId, + input: { cwd: "/repo" }, + }); + yield* refreshStatus; + expect(yield* Ref.get(clears)).toBe(2); + expect(registry.get(vcsRefsCacheStateAtom(TARGET)).revision).toBe(2); + + yield* Ref.set(refsDigest, "refs-b"); + yield* refreshStatus; + 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, @@ -457,8 +472,8 @@ describe("cached VCS refs", () => { ); expect(Exit.isSuccess(prepareResult)).toBe(true); - expect(yield* Ref.get(clears)).toBe(3); - expect(registry.get(vcsRefsCacheStateAtom(TARGET)).revision).toBe(3); + expect(yield* Ref.get(clears)).toBe(4); + expect(registry.get(vcsRefsCacheStateAtom(TARGET)).revision).toBe(4); }), ); diff --git a/packages/client-runtime/src/state/vcs.ts b/packages/client-runtime/src/state/vcs.ts index 310515ceb9915..a6299aab0ff64 100644 --- a/packages/client-runtime/src/state/vcs.ts +++ b/packages/client-runtime/src/state/vcs.ts @@ -8,6 +8,7 @@ import { import { applyGitStatusStreamEvent } from "@t3tools/shared/git"; import * as Duration from "effect/Duration"; import * as Effect from "effect/Effect"; +import * as Exit from "effect/Exit"; import * as Option from "effect/Option"; import * as Result from "effect/Result"; import * as Schedule from "effect/Schedule"; @@ -35,6 +36,7 @@ import { followStreamInEnvironment } from "./runtime.ts"; import { vcsCommandConcurrency, vcsCommandScheduler } from "./vcsCommandScheduler.ts"; import { invalidateCachedVcsRefs, + invalidateCachedVcsRefsIfChanged, vcsRefsCacheStateAtom, withVcsRefsPersistenceLock, } from "./vcsRefInvalidation.ts"; @@ -339,7 +341,12 @@ export function createVcsEnvironmentAtoms( tag: WS_METHODS.vcsRefreshStatus, scheduler: vcsCommandScheduler, concurrency: vcsCommandConcurrency, - onSettled: invalidateRefs, + onSettled: (target, registry, exit) => + invalidateCachedVcsRefsIfChanged( + registry, + { environmentId: target.environmentId, cwd: target.input.cwd }, + Exit.isSuccess(exit) ? exit.value.refsDigest : undefined, + ), }), // Outside the serial queue: it only reads status, and a checkout picked right after opening // the picker must not wait behind that read. diff --git a/packages/client-runtime/src/state/vcsRefInvalidation.ts b/packages/client-runtime/src/state/vcsRefInvalidation.ts index 6b695d35e668b..54f4a84a3aaac 100644 --- a/packages/client-runtime/src/state/vcsRefInvalidation.ts +++ b/packages/client-runtime/src/state/vcsRefInvalidation.ts @@ -79,3 +79,32 @@ export const invalidateCachedVcsRefs = Effect.fn("VcsRefsState.invalidateCached" }), ); }); + +// Ref digests from each cwd's last status refresh; the cached branch list is at least that new. +const refsDigestsByEnvironment = Atom.family((environmentId: EnvironmentId) => + Atom.make>(new Map()).pipe( + Atom.keepAlive, + Atom.withLabel(`environment-data:vcs:refs-digests:${environmentId}`), + ), +); + +/** Skips the branch list reload when a status refresh saw the same refs as the last one. */ +export const invalidateCachedVcsRefsIfChanged = Effect.fn("VcsRefsState.invalidateCachedIfChanged")( + function* ( + registry: AtomRegistry.AtomRegistry, + target: CachedVcsRefsInvalidationTarget, + refsDigest: string | undefined, + ) { + const digestsAtom = refsDigestsByEnvironment(target.environmentId); + if (refsDigest !== undefined && registry.get(digestsAtom).get(target.cwd) === refsDigest) { + return; + } + registry.update(digestsAtom, (digests) => { + const next = new Map(digests); + if (refsDigest === undefined) next.delete(target.cwd); + else next.set(target.cwd, refsDigest); + return next; + }); + yield* invalidateCachedVcsRefs(registry, target); + }, +); diff --git a/packages/contracts/src/git.ts b/packages/contracts/src/git.ts index 45d68c5e9ca44..751f240f0709e 100644 --- a/packages/contracts/src/git.ts +++ b/packages/contracts/src/git.ts @@ -270,6 +270,13 @@ export const VcsStatusResult = Schema.Struct({ }); export type VcsStatusResult = typeof VcsStatusResult.Type; +export const VcsRefreshStatusResult = Schema.Struct({ + ...VcsStatusResult.fields, + // Digest of the repository's refs after the refresh; absent when they could not be read. + refsDigest: Schema.optional(TrimmedNonEmptyString), +}); +export type VcsRefreshStatusResult = typeof VcsRefreshStatusResult.Type; + export const VcsStatusStreamEvent = Schema.Union([ Schema.TaggedStruct("snapshot", { local: VcsStatusLocalResult, diff --git a/packages/contracts/src/rpc.ts b/packages/contracts/src/rpc.ts index b0892de7b13d9..e30bf9af2937c 100644 --- a/packages/contracts/src/rpc.ts +++ b/packages/contracts/src/rpc.ts @@ -169,7 +169,7 @@ import { VcsStatusInput, VcsBranchStatus, VcsStatusSubscriptionInput, - VcsStatusResult, + VcsRefreshStatusResult, VcsStatusStreamEvent, } from "./git.ts"; import { @@ -1809,7 +1809,7 @@ const WsVcsRefreshStatusRpc = Rpc.make(WS_METHODS.vcsRefreshStatus, { ...VcsStatusInput.fields, localOnly: Schema.optional(Schema.Boolean), }), - success: VcsStatusResult, + success: VcsRefreshStatusResult, error: Schema.Union([GitManagerServiceError, EnvironmentAuthorizationError]), });