The best way to control Claude Code, Codex and more from any computer or phone
fix(server): bound retained VCS status snapshots (#35631)
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Alex Southwell committed
Nov 19, 2026 at 16:58 UTC
1167eb10abe3032e1070f43486306296ed970250
2 files changed
+228
-16
apps/server/src/vcs/VcsStatusBroadcaster.test.ts
+169
@@ -221,6 +221,175 @@ function layerBackgroundPolicy(shouldRunScopeWork: (scope: BackgroundScope) => b
221
}
222
223
describe("VcsStatusBroadcaster", () => {
224
+ it.effect.each(["passive subscriptions", "status reads", "local refreshes", "full refreshes"])(
225
+ "bounds historical snapshots from %s while retaining recently read entries",
226
+ (source) => {
227
+ const state = {
228
+ currentLocalStatus: baseLocalStatus,
229
+ currentRemoteStatus: baseRemoteStatus,
230
+ localStatusCalls: 0,
231
+ remoteStatusCalls: 0,
232
+ localInvalidationCalls: 0,
233
+ remoteInvalidationCalls: 0,
234
+ };
235
+
236
+ return Effect.gen(function* () {
237
+ const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster;
238
+ yield* broadcaster.getStatus({ cwd: "/hot-repo" });
239
+ for (let index = 0; index < 1_000; index += 1) {
240
+ const cwd = `/visited-repo-${index}`;
241
+ if (source === "passive subscriptions") {
242
+ yield* Stream.runHead(broadcaster.streamStatus({ cwd, includeRemote: false }));
243
+ } else if (source === "status reads") {
244
+ yield* broadcaster.getStatus({ cwd });
245
+ } else if (source === "local refreshes") {
246
+ yield* broadcaster.refreshLocalStatus(cwd);
247
+ } else {
248
+ yield* broadcaster.refreshStatus(cwd);
249
+ }
250
+ const localCalls = state.localStatusCalls;
251
+ const remoteCalls = state.remoteStatusCalls;
252
+ yield* broadcaster.getStatus({ cwd: "/hot-repo" });
253
+ assert.equal(state.localStatusCalls, localCalls);
254
+ assert.equal(state.remoteStatusCalls, remoteCalls);
255
+ }
256
+
257
+ const localCalls = state.localStatusCalls;
258
+ yield* Stream.runHead(
259
+ broadcaster.streamStatus({ cwd: "/visited-repo-999", includeRemote: false }),
260
+ );
261
+ assert.equal(state.localStatusCalls, localCalls);
262
+ yield* Stream.runHead(
263
+ broadcaster.streamStatus({ cwd: "/visited-repo-0", includeRemote: false }),
264
+ );
265
+ assert.equal(state.localStatusCalls, localCalls + 1);
266
+ }).pipe(Effect.provide(layerTestFor(state)));
267
+ },
268
+ );
269
+
270
+ it.effect("keeps a watched cwd's snapshot until its last subscriber leaves", () => {
271
+ const state = {
272
+ currentLocalStatus: baseLocalStatus,
273
+ currentRemoteStatus: remoteStatusWithPr,
274
+ localStatusCalls: 0,
275
+ remoteStatusCalls: 0,
276
+ localInvalidationCalls: 0,
277
+ remoteInvalidationCalls: 0,
278
+ backgroundWorkEnabled: false,
279
+ automaticFetchInterval: Effect.succeed(Duration.zero),
280
+ };
281
+
282
+ return Effect.gen(function* () {
283
+ const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster;
284
+ const subscription = yield* Scope.make();
285
+ const remoteLoaded = yield* Deferred.make<void>();
286
+ yield* Stream.runForEach(broadcaster.streamStatus({ cwd: "/active-repo" }), (event) =>
287
+ event._tag === "remoteUpdated" ? Deferred.succeed(remoteLoaded, undefined) : Effect.void,
288
+ ).pipe(Effect.forkIn(subscription));
289
+ yield* Deferred.await(remoteLoaded);
290
+ for (let index = 0; index < 1_000; index += 1) {
291
+ yield* broadcaster.getStatus({ cwd: `/visited-repo-${index}` });
292
+ }
293
+
294
+ // With fetching disabled, nothing would reload a remote dropped from the cache.
295
+ const localCalls = state.localStatusCalls;
296
+ const remoteCalls = state.remoteStatusCalls;
297
+ const snapshot = yield* Stream.runHead(broadcaster.streamStatus({ cwd: "/active-repo" }));
298
+ assert.deepStrictEqual(Option.getOrThrow(snapshot), {
299
+ _tag: "snapshot",
300
+ local: baseLocalStatus,
301
+ remote: remoteStatusWithPr,
302
+ });
303
+ assert.equal(state.localStatusCalls, localCalls);
304
+ assert.equal(state.remoteStatusCalls, remoteCalls);
305
+ // Turn completion skips a cwd it has no snapshot for.
306
+ const turnEndRemote = { ...remoteStatusWithPr, aheadCount: 1 };
307
+ state.currentRemoteStatus = turnEndRemote;
308
+ assert.deepStrictEqual(
309
+ yield* broadcaster.refreshPullRequestStatus("/active-repo"),
310
+ turnEndRemote,
311
+ );
312
+
313
+ yield* Scope.close(subscription, Exit.void);
314
+ for (let index = 1_000; index < 2_000; index += 1) {
315
+ yield* broadcaster.getStatus({ cwd: `/visited-repo-${index}` });
316
+ }
317
+ const releasedCalls = state.localStatusCalls;
318
+ yield* broadcaster.getStatus({ cwd: "/active-repo" });
319
+ assert.equal(state.localStatusCalls, releasedCalls + 1);
320
+ }).pipe(Effect.provide(layerTestFor(state)));
321
+ });
322
+
323
+ it.effect("keeps an unwatched refresh coherent while other directories fill the cache", () => {
324
+ const state = {
325
+ currentLocalStatus: baseLocalStatus,
326
+ currentRemoteStatus: { ...remoteStatusWithPr, aheadCount: 2 },
327
+ localStatusCalls: 0,
328
+ remoteStatusCalls: 0,
329
+ localInvalidationCalls: 0,
330
+ remoteInvalidationCalls: 0,
331
+ };
332
+ const policyStarted = Deferred.makeUnsafe<void>();
333
+ const releasePolicy = Deferred.makeUnsafe<void>();
334
+ const layer = layerTestFor(state).pipe(
335
+ Layer.provide(
336
+ Layer.succeed(VcsStatusBroadcaster.VcsAutoPullPolicy, {
337
+ isEnabled: () =>
338
+ Deferred.succeed(policyStarted, undefined).pipe(
339
+ Effect.andThen(Deferred.await(releasePolicy)),
340
+ Effect.as(false),
341
+ ),
342
+ }),
343
+ ),
344
+ );
345
+ return Effect.gen(function* () {
346
+ const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster;
347
+ const refresh = yield* broadcaster.refreshStatus("/refreshing-repo").pipe(Effect.forkScoped);
348
+ yield* Deferred.await(policyStarted);
349
+ for (let index = 0; index < 256; index += 1) {
350
+ yield* broadcaster.refreshLocalStatus(`/other-repo-${index}`);
351
+ }
352
+ yield* Deferred.succeed(releasePolicy, undefined);
353
+ const result = yield* Fiber.join(refresh);
354
+ assert.deepStrictEqual(result, { ...baseLocalStatus, ...state.currentRemoteStatus });
355
+ }).pipe(Effect.provide(layer), Effect.scoped);
356
+ });
357
+
358
+ it.effect("trims excess snapshots when the last watched directories are released", () => {
359
+ const state = {
360
+ currentLocalStatus: baseLocalStatus,
361
+ currentRemoteStatus: baseRemoteStatus,
362
+ localStatusCalls: 0,
363
+ remoteStatusCalls: 0,
364
+ localInvalidationCalls: 0,
365
+ remoteInvalidationCalls: 0,
366
+ backgroundWorkEnabled: false,
367
+ };
368
+ return Effect.gen(function* () {
369
+ const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster;
370
+ const scopes = [];
371
+ for (let index = 0; index < 257; index += 1) {
372
+ const scope = yield* Effect.acquireRelease(Scope.make(), (scope) =>
373
+ Scope.close(scope, Exit.void),
374
+ );
375
+ scopes.push(scope);
376
+ const ready = yield* Deferred.make<void>();
377
+ yield* Stream.runForEach(
378
+ broadcaster.streamStatus({ cwd: `/watched-repo-${index}`, includeRemote: false }),
379
+ () => Deferred.succeed(ready, undefined),
380
+ ).pipe(Effect.forkIn(scope));
381
+ yield* Deferred.await(ready);
382
+ }
383
+ for (const scope of scopes) yield* Scope.close(scope, Exit.void);
384
+ const reads = state.localStatusCalls;
385
+ // A cache read protects its own cwd before trimming; only teardown can evict this oldest cwd.
386
+ yield* Stream.runHead(
387
+ broadcaster.streamStatus({ cwd: "/watched-repo-0", includeRemote: false }),
388
+ );
389
+ assert.equal(state.localStatusCalls, reads + 1);
390
+ }).pipe(Effect.provide(layerTestFor(state)), Effect.scoped);
391
+ });
392
+
393
it.effect("refreshes queued behind a running one share the next git status read", () => {
394
const state: Parameters<typeof layerTestFor>[0] = {
395
currentLocalStatus: baseLocalStatus,
apps/server/src/vcs/VcsStatusBroadcaster.ts
+59
-16
@@ -37,6 +37,7 @@ import { VcsAutomaticFetchInterval } from "./VcsAutomaticFetchInterval.ts";
37
38
const VCS_STATUS_REFRESH_FAILURE_BASE_DELAY = Duration.seconds(30);
39
const VCS_STATUS_REFRESH_FAILURE_MAX_DELAY = Duration.minutes(15);
40
+const VCS_STATUS_CACHE_CAPACITY = 256;
41
const MAX_FAILURE_DIAGNOSTIC_VALUES = 8;
42
// A held index.lock fails the read, and its removal moves no HEAD to trigger another.
43
const CHECKOUT_REFRESH_RETRY = Schedule.exponential(Duration.seconds(1)).pipe(
@@ -308,15 +309,68 @@ export const make = Effect.gen(function* () {
309
// periodic poll that started before an agent creates a PR can finish after the
310
// turn-end refresh and overwrite the fresh PR with its stale `pr: null`.
311
const remoteWriteLocks = yield* KeyedLock.make<string>();
312
+ const activeRemoteReads = new Map<string, number>();
313
const withRemoteWriteLock = <A, E, R>(cwd: string, effect: Effect.Effect<A, E, R>) =>
312
- remoteWriteLocks.withLock(cwd, effect);
314
+ Effect.acquireUseRelease(
315
+ Effect.sync(() => activeRemoteReads.set(cwd, (activeRemoteReads.get(cwd) ?? 0) + 1)),
316
+ () => remoteWriteLocks.withLock(cwd, effect),
317
+ () =>
318
+ Effect.sync(() => {
319
+ const remaining = (activeRemoteReads.get(cwd) ?? 1) - 1;
320
+ if (remaining === 0) activeRemoteReads.delete(cwd);
321
+ else activeRemoteReads.set(cwd, remaining);
322
+ }).pipe(Effect.andThen(trimCachedStatuses)),
323
+ );
324
const pollersRef = yield* SynchronizedRef.make(new Map<string, ActiveRemotePoller>());
325
const headWatchersRef = yield* SynchronizedRef.make(new Map<string, ActiveHeadWatcher>());
326
+ // A refresh that started after a request saw every change that request could mean, so queued
327
+ // callers share it instead of each running git status again.
328
+ const localRefreshRequests = new Map<string, number>();
329
+ const localRefreshResults = new Map<
330
+ string,
331
+ {
332
+ readonly startedAt: number;
333
+ readonly local: VcsStatusLocalResult;
334
+ readonly includesBranchChanges: boolean;
335
+ }
336
+ >();
337
+
338
+ // Keep watched snapshots and remote operations coherent until their consumers finish.
339
+ const evictExcessStatuses = (cache: Map<string, CachedVcsStatus>, retainedCwd?: string) => {
340
+ for (const candidate of cache.keys()) {
341
+ if (cache.size <= VCS_STATUS_CACHE_CAPACITY) break;
342
+ if (
343
+ candidate === retainedCwd ||
344
+ detailedSubscribers.has(candidate) ||
345
+ activeRemoteReads.has(candidate)
346
+ )
347
+ continue;
348
+ cache.delete(candidate);
349
+ // A queued caller can read git again. Keep the monotonic request counter so it cannot
350
+ // mistake an older result for a later request.
351
+ localRefreshResults.delete(candidate);
352
+ }
353
+ return cache;
354
+ };
355
+ const trimCachedStatuses = Ref.update(cacheRef, (cache) => evictExcessStatuses(new Map(cache)));
356
+ const retainCachedStatus = (
357
+ cache: ReadonlyMap<string, CachedVcsStatus>,
358
+ cwd: string,
359
+ status: CachedVcsStatus,
360
+ ) => {
361
+ const next = new Map(cache);
362
+ next.delete(cwd);
363
+ next.set(cwd, status);
364
+ return evictExcessStatuses(next, cwd);
365
+ };
366
367
const getCachedStatus = Effect.fn("VcsStatusBroadcaster.getCachedStatus")(function* (
368
cwd: string,
369
) {
319
- return yield* Ref.get(cacheRef).pipe(Effect.map((cache) => cache.get(cwd) ?? null));
370
+ return yield* Ref.modify(cacheRef, (cache) => {
371
+ const cached = cache.get(cwd) ?? null;
372
+ return [cached, cached ? retainCachedStatus(cache, cwd, cached) : cache] as const;
373
+ });
374
});
375
376
// Only full subscribers and direct reads show Changes totals. Command palette rows subscribe
@@ -369,7 +423,7 @@ export const make = Effect.gen(function* () {
423
const remote = previous.remote?.branch === local.refName ? previous.remote : null;
424
return [
425
[previous.local?.fingerprint !== nextLocal.fingerprint, previous.remote !== remote],
372
- new Map(cache).set(cwd, { local: nextLocal, remote }),
426
+ retainCachedStatus(cache, cwd, { local: nextLocal, remote }),
427
] as const;
428
});
429
yield* publishBranch(cwd, local, observation);
@@ -396,7 +450,7 @@ export const make = Effect.gen(function* () {
450
const next = remote?.branch === previous.local?.value.refName ? remote : null;
451
return [
452
[previous.remote?.fingerprint !== next?.fingerprint, next?.value ?? null],
399
- new Map(cache).set(cwd, { ...previous, remote: next }),
453
+ retainCachedStatus(cache, cwd, { ...previous, remote: next }),
454
] as const;
455
});
456
if (options?.publish && changed) {
@@ -468,17 +522,6 @@ export const make = Effect.gen(function* () {
522
);
523
}, Effect.scoped);
524
471
- // A refresh that started after a request saw every change that request could mean, so queued
472
- // callers share it instead of each running git status again.
473
- const localRefreshRequests = new Map<string, number>();
474
- const localRefreshResults = new Map<
475
- string,
476
- {
477
- readonly startedAt: number;
478
- readonly local: VcsStatusLocalResult;
479
- readonly includesBranchChanges: boolean;
480
- }
481
- >();
525
const refreshLocalStatusCore = Effect.fn("VcsStatusBroadcaster.refreshLocalStatusCore")(
526
function* (cwd: string) {
527
const requestedAt = (localRefreshRequests.get(cwd) ?? 0) + 1;
@@ -1107,7 +1150,7 @@ export const make = Effect.gen(function* () {
1150
const remaining = (detailedSubscribers.get(cwd) ?? 1) - 1;
1151
if (remaining === 0) detailedSubscribers.delete(cwd);
1152
else detailedSubscribers.set(cwd, remaining);
1110
- }),
1153
+ }).pipe(Effect.andThen(trimCachedStatuses)),
1154
);
1155
if (input.includeRemote !== false) yield* acquireBranchChangesDemand(cwd);
1156
const subscription = yield* PubSub.subscribe(changesPubSub);