From ed0591335e8fca48ce555fafeab9afd1ee163d91 Mon Sep 17 00:00:00 2001 From: Ben Davis Date: Fri, 26 Jun 2026 11:18:24 -0700 Subject: [PATCH] fix(server): bound VCS status broadcaster state --- .../src/vcs/VcsStatusBroadcaster.test.ts | 71 ++++++++++++++++ apps/server/src/vcs/VcsStatusBroadcaster.ts | 83 ++++++++++++------- 2 files changed, 126 insertions(+), 28 deletions(-) diff --git a/apps/server/src/vcs/VcsStatusBroadcaster.test.ts b/apps/server/src/vcs/VcsStatusBroadcaster.test.ts index 26a0e99b05..5e85345eb3 100644 --- a/apps/server/src/vcs/VcsStatusBroadcaster.test.ts +++ b/apps/server/src/vcs/VcsStatusBroadcaster.test.ts @@ -130,6 +130,40 @@ describe("VcsStatusBroadcaster", () => { }).pipe(Effect.provide(makeTestLayer(state))); }); + it.effect("evicts the least recently used status after reaching cache capacity", () => { + const state = { + currentLocalStatus: baseLocalStatus, + currentRemoteStatus: baseRemoteStatus, + localStatusCalls: 0, + remoteStatusCalls: 0, + localInvalidationCalls: 0, + remoteInvalidationCalls: 0, + }; + + return Effect.gen(function* () { + const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster; + + for (let index = 0; index < VcsStatusBroadcaster.VCS_STATUS_CACHE_CAPACITY; index += 1) { + yield* broadcaster.getStatus({ cwd: `/repo-${index}` }); + } + + assert.equal(state.localStatusCalls, VcsStatusBroadcaster.VCS_STATUS_CACHE_CAPACITY); + assert.equal(state.remoteStatusCalls, VcsStatusBroadcaster.VCS_STATUS_CACHE_CAPACITY); + + yield* broadcaster.getStatus({ cwd: "/repo-0" }); + yield* broadcaster.getStatus({ cwd: "/repo-overflow" }); + yield* broadcaster.getStatus({ cwd: "/repo-0" }); + + assert.equal(state.localStatusCalls, VcsStatusBroadcaster.VCS_STATUS_CACHE_CAPACITY + 1); + assert.equal(state.remoteStatusCalls, VcsStatusBroadcaster.VCS_STATUS_CACHE_CAPACITY + 1); + + yield* broadcaster.getStatus({ cwd: "/repo-1" }); + + assert.equal(state.localStatusCalls, VcsStatusBroadcaster.VCS_STATUS_CACHE_CAPACITY + 2); + assert.equal(state.remoteStatusCalls, VcsStatusBroadcaster.VCS_STATUS_CACHE_CAPACITY + 2); + }).pipe(Effect.provide(makeTestLayer(state))); + }); + it.effect("refreshes the cached snapshot after explicit invalidation", () => { const state = { currentLocalStatus: baseLocalStatus, @@ -726,6 +760,43 @@ describe("VcsStatusBroadcaster", () => { }); }); + it.effect("evicts the cached status after the last stream subscriber disconnects", () => { + const state = { + currentLocalStatus: baseLocalStatus, + currentRemoteStatus: baseRemoteStatus, + localStatusCalls: 0, + remoteStatusCalls: 0, + localInvalidationCalls: 0, + remoteInvalidationCalls: 0, + }; + + return Effect.gen(function* () { + const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster; + const remoteUpdated = yield* Deferred.make(); + const scope = yield* Scope.make(); + yield* Stream.runForEach( + broadcaster.streamStatus( + { cwd: "/repo" }, + { automaticRemoteRefreshInterval: Effect.succeed(Duration.zero) }, + ), + (event) => + event._tag === "remoteUpdated" + ? Deferred.succeed(remoteUpdated, event).pipe(Effect.ignore) + : Effect.void, + ).pipe(Effect.forkIn(scope)); + + yield* Deferred.await(remoteUpdated); + assert.equal(state.localStatusCalls, 1); + assert.equal(state.remoteStatusCalls, 1); + + yield* Scope.close(scope, Exit.void); + yield* broadcaster.getStatus({ cwd: "/repo" }); + + assert.equal(state.localStatusCalls, 2); + assert.equal(state.remoteStatusCalls, 2); + }).pipe(Effect.provide(makeTestLayer(state))); + }); + it.effect("stops the remote poller after the last stream subscriber disconnects", () => { const state = { currentLocalStatus: baseLocalStatus, diff --git a/apps/server/src/vcs/VcsStatusBroadcaster.ts b/apps/server/src/vcs/VcsStatusBroadcaster.ts index b74c40caff..cf89f77e9c 100644 --- a/apps/server/src/vcs/VcsStatusBroadcaster.ts +++ b/apps/server/src/vcs/VcsStatusBroadcaster.ts @@ -29,6 +29,7 @@ const VCS_STATUS_REFRESH_FAILURE_BASE_DELAY = Duration.seconds(30); const VCS_STATUS_REFRESH_FAILURE_MAX_DELAY = Duration.minutes(15); const MAX_FAILURE_DIAGNOSTIC_VALUES = 8; const MAX_FAILURE_DIAGNOSTIC_VALUE_LENGTH = 128; +export const VCS_STATUS_CACHE_CAPACITY = 128; function boundedDiagnosticValue(value: string): string { return value.slice(0, MAX_FAILURE_DIAGNOSTIC_VALUE_LENGTH); @@ -188,13 +189,32 @@ export const make = Effect.gen(function* () { const broadcasterScope = yield* Effect.acquireRelease(Scope.make(), (scope) => Scope.close(scope, Exit.void), ); - const cacheRef = yield* Ref.make(new Map()); + const statusCache = new Map(); const pollersRef = yield* SynchronizedRef.make(new Map()); + const setCachedStatus = (cwd: string, status: CachedVcsStatus) => { + statusCache.delete(cwd); + statusCache.set(cwd, status); + + while (statusCache.size > VCS_STATUS_CACHE_CAPACITY) { + const oldestCwd = statusCache.keys().next().value; + if (oldestCwd === undefined) { + break; + } + statusCache.delete(oldestCwd); + } + }; + const getCachedStatus = Effect.fn("VcsStatusBroadcaster.getCachedStatus")(function* ( cwd: string, ) { - return yield* Ref.get(cacheRef).pipe(Effect.map((cache) => cache.get(cwd) ?? null)); + return yield* Effect.sync(() => { + const cached = statusCache.get(cwd) ?? null; + if (cached) { + setCachedStatus(cwd, cached); + } + return cached; + }); }); const updateCachedLocalStatus = Effect.fn("VcsStatusBroadcaster.updateCachedLocalStatus")( @@ -203,14 +223,13 @@ export const make = Effect.gen(function* () { fingerprint: fingerprintStatusPart(local), value: local, } satisfies CachedValue; - const shouldPublish = yield* Ref.modify(cacheRef, (cache) => { - const previous = cache.get(cwd) ?? { local: null, remote: null }; - const nextCache = new Map(cache); - nextCache.set(cwd, { + const shouldPublish = yield* Effect.sync(() => { + const previous = statusCache.get(cwd) ?? { local: null, remote: null }; + setCachedStatus(cwd, { ...previous, local: nextLocal, }); - return [previous.local?.fingerprint !== nextLocal.fingerprint, nextCache] as const; + return previous.local?.fingerprint !== nextLocal.fingerprint; }); if (options?.publish && shouldPublish) { @@ -233,14 +252,13 @@ export const make = Effect.gen(function* () { fingerprint: fingerprintStatusPart(remote), value: remote, } satisfies CachedValue; - const shouldPublish = yield* Ref.modify(cacheRef, (cache) => { - const previous = cache.get(cwd) ?? { local: null, remote: null }; - const nextCache = new Map(cache); - nextCache.set(cwd, { + const shouldPublish = yield* Effect.sync(() => { + const previous = statusCache.get(cwd) ?? { local: null, remote: null }; + setCachedStatus(cwd, { ...previous, remote: nextRemote, }); - return [previous.remote?.fingerprint !== nextRemote.fingerprint, nextCache] as const; + return previous.remote?.fingerprint !== nextRemote.fingerprint; }); if (options?.publish && shouldPublish) { @@ -271,18 +289,16 @@ export const make = Effect.gen(function* () { fingerprint: fingerprintStatusPart(remote), value: remote, } satisfies CachedValue; - const shouldPublish = yield* Ref.modify(cacheRef, (cache) => { - const previous = cache.get(cwd) ?? { local: null, remote: null }; - const nextCache = new Map(cache); - nextCache.set(cwd, { + const shouldPublish = yield* Effect.sync(() => { + const previous = statusCache.get(cwd) ?? { local: null, remote: null }; + setCachedStatus(cwd, { local: nextLocal, remote: nextRemote, }); - return [ + return ( previous.local?.fingerprint !== nextLocal.fingerprint || - previous.remote?.fingerprint !== nextRemote.fingerprint, - nextCache, - ] as const; + previous.remote?.fingerprint !== nextRemote.fingerprint + ); }); if (options?.publish && shouldPublish) { @@ -506,6 +522,16 @@ export const make = Effect.gen(function* () { if (pollerToInterrupt) { yield* Fiber.interrupt(pollerToInterrupt).pipe(Effect.ignore); + yield* SynchronizedRef.modifyEffect(pollersRef, (activePollers) => { + if (activePollers.has(cwd)) { + return Effect.succeed([undefined, activePollers] as const); + } + + return Effect.sync(() => { + statusCache.delete(cwd); + return [undefined, activePollers] as const; + }); + }); } }); @@ -517,15 +543,16 @@ export const make = Effect.gen(function* () { const initialLocal = yield* getOrLoadLocalStatus(cwd); const cachedStatus = yield* getCachedStatus(cwd); const initialRemote = cachedStatus?.remote?.value ?? null; - yield* retainRemotePoller( - cwd, - options?.automaticRemoteRefreshInterval ?? - Effect.succeed(DEFAULT_VCS_STATUS_REFRESH_INTERVAL), - cachedStatus?.remote === null || cachedStatus?.remote === undefined, + yield* Effect.acquireRelease( + retainRemotePoller( + cwd, + options?.automaticRemoteRefreshInterval ?? + Effect.succeed(DEFAULT_VCS_STATUS_REFRESH_INTERVAL), + cachedStatus?.remote === null || cachedStatus?.remote === undefined, + ), + () => releaseRemotePoller(cwd), ); - const release = releaseRemotePoller(cwd).pipe(Effect.ignore, Effect.asVoid); - return Stream.concat( Stream.make({ _tag: "snapshot" as const, @@ -536,7 +563,7 @@ export const make = Effect.gen(function* () { Stream.filter((event) => event.cwd === cwd), Stream.map((event) => event.event), ), - ).pipe(Stream.ensuring(release)); + ); }), );