TypeScript
fix(client-runtime): branch picker shows branches changed outside T3 Code (#27438)
Ebrahim Tayabali1d80d0e215a0
diff --git a/packages/client-runtime/src/state/vcs.test.ts b/packages/client-runtime/src/state/vcs.test.tsindex da62615e890..e46ab146c0c 100644--- a/packages/client-runtime/src/state/vcs.test.ts+++ b/packages/client-runtime/src/state/vcs.test.ts@@ -4,6 +4,7 @@ import {AuthSourceControlWriteScope,WS_METHODS,VcsProcessExitError,+ type VcsListRefsInput,type VcsListRefsResult,type VcsStatusResult,} from "@t3tools/contracts";@@ -154,6 +155,98 @@ describe("cached VCS refs", () => {registry.dispose();});+ it.effect("bypasses the server ref snapshot when a mounted list is invalidated", () =>+ Effect.scoped(+ Effect.gen(function* () {+ const requests = yield* Queue.unbounded<VcsListRefsInput>();+ const client = {+ [WS_METHODS.vcsListRefs]: (input: VcsListRefsInput) =>+ Effect.sync(() => {+ Queue.offerUnsafe(requests, input);+ return LIVE_REFS;+ }),+ } as unknown as WsRpcProtocolClient;+ const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({+ target: TARGET,+ state: yield* SubscriptionRef.make(CONNECTED_CONNECTION_STATE),+ session: yield* SubscriptionRef.make(Option.some(session(client))),+ prepared: yield* SubscriptionRef.make(Option.none<PreparedConnection>()),+ connect: Effect.void,+ disconnect: Effect.void,+ retryNow: Effect.void,+ } satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]);+ const followStream: EnvironmentRegistry.EnvironmentRegistry["Service"]["followStream"] = (+ _environmentId,+ stream,+ ) => Stream.provideService(stream, EnvironmentSupervisor.EnvironmentSupervisor, supervisor);+ const environmentRegistry = EnvironmentRegistry.EnvironmentRegistry.of({+ followStream,+ } as unknown as EnvironmentRegistry.EnvironmentRegistry["Service"]);+ const runtime = Atom.runtime(+ Layer.merge(+ Layer.succeed(EnvironmentRegistry.EnvironmentRegistry, environmentRegistry),+ Layer.succeed(Persistence.EnvironmentCacheStore, cacheWithRefs(Option.none())),+ ),+ );+ const atoms = createVcsEnvironmentAtoms(runtime);+ const registry = yield* Effect.acquireRelease(Effect.sync(AtomRegistry.make), (registry) =>+ Effect.sync(() => registry.dispose()),+ );+ yield* Effect.acquireRelease(+ Effect.sync(() =>+ registry.mount(+ atoms.listRefs({+ environmentId: TARGET.environmentId,+ input: { cwd: "/repo", limit: 100 },+ }),+ ),+ ),+ (unmount) => Effect.sync(unmount),+ );++ expect(yield* Queue.take(requests)).toEqual({ cwd: "/repo", limit: 100 });++ invalidateVcsRefs(registry, TARGET);++ expect(yield* Queue.take(requests)).toEqual({ cwd: "/repo", limit: 100, refresh: true });++ yield* Effect.acquireRelease(+ Effect.sync(() =>+ registry.mount(+ atoms.listRefs({+ environmentId: TARGET.environmentId,+ input: { cwd: "/other-repo", limit: 100 },+ }),+ ),+ ),+ (unmount) => Effect.sync(unmount),+ );+ expect(yield* Queue.take(requests)).toEqual({+ cwd: "/other-repo",+ limit: 100,+ refresh: true,+ });++ yield* Effect.acquireRelease(+ Effect.sync(() =>+ registry.mount(+ atoms.listRefs({+ environmentId: TARGET.environmentId,+ input: { cwd: "/other-repo", query: "release", limit: 100 },+ }),+ ),+ ),+ (unmount) => Effect.sync(unmount),+ );+ expect(yield* Queue.take(requests)).toEqual({+ cwd: "/other-repo",+ query: "release",+ limit: 100,+ });+ }),+ ),+ );+it.effect("does not repersist a refresh superseded by ref invalidation", () =>Effect.scoped(Effect.gen(function* () {diff --git a/packages/client-runtime/src/state/vcs.ts b/packages/client-runtime/src/state/vcs.tsindex e05ec4a2c8e..ef508f2126c 100644--- a/packages/client-runtime/src/state/vcs.ts+++ b/packages/client-runtime/src/state/vcs.ts@@ -33,6 +33,9 @@ import {withVcsRefsPersistenceLock,} from "./vcsRefInvalidation.ts";+// Share freshness across search and pagination keys, but keep it local to this registry.+const serverRefsRevisionAtom = Atom.family((_key: string) => Atom.make(0).pipe(Atom.keepAlive));+const OFFLINE_BRANCH_LIST_LIMIT = 100;const VCS_REFS_IDLE_TTL_MS = 30_000;const VCS_STATUS_IDLE_TTL_MS = 10_000;@@ -143,9 +146,17 @@ export const makeCachedVcsRefsChanges = Effect.fn("CachedVcsRefsState.makeChange): Option.none<VcsListRefsResult>();const refresh = Effect.fn("CachedVcsRefsState.refresh")(function* () {- const refs = yield* request(WS_METHODS.vcsListRefs, input).pipe(- Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),+ const revisionAtom = serverRefsRevisionAtom(+ `${environmentId.length}:${environmentId}${input.cwd}`,);+ const bypassServerSnapshot =+ expectedRevision !== undefined &&+ registry !== undefined &&+ expectedRevision > registry.get(revisionAtom);+ const refs = yield* request(+ WS_METHODS.vcsListRefs,+ bypassServerSnapshot ? { ...input, refresh: true } : input,+ ).pipe(Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor));const persist = cache.saveVcsRefs(environmentId, input.cwd, refs).pipe(Effect.catch((error) =>Effect.logWarning("Could not persist cached Git refs.").pipe(@@ -168,6 +179,7 @@ export const makeCachedVcsRefsChanges = Effect.fn("CachedVcsRefsState.makeChangeexpectedRevision,persist: useCache,});+ if (committed && bypassServerSnapshot) registry.set(revisionAtom, expectedRevision);return committed ? Option.some(refs) : Option.none<VcsListRefsResult>();});