diff --git a/apps/server/src/vcs/VcsStatusBroadcaster.test.ts b/apps/server/src/vcs/VcsStatusBroadcaster.test.ts index e367bacb5a7d..69c33b785bd4 100644 --- a/apps/server/src/vcs/VcsStatusBroadcaster.test.ts +++ b/apps/server/src/vcs/VcsStatusBroadcaster.test.ts @@ -1332,6 +1332,50 @@ describe("VcsStatusBroadcaster", () => { }).pipe(Effect.provide(layerTestFor(state))); }); + it.effect("a remote poll that moves no ref does not re-read local status", () => { + const state: Parameters[0] = { + currentLocalStatus: baseLocalStatus, + currentRemoteStatus: baseRemoteStatus, + localStatusCalls: 0, + remoteStatusCalls: 0, + localInvalidationCalls: 0, + remoteInvalidationCalls: 0, + }; + + return Effect.gen(function* () { + const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster; + yield* broadcaster.getStatus({ cwd: "/repo" }); + const subscribed = yield* Deferred.make(); + const remoteUpdates = yield* Queue.unbounded(); + yield* broadcaster.streamStatus({ cwd: "/repo" }).pipe( + Stream.runForEach((event) => { + if (event._tag === "snapshot") return Deferred.succeed(subscribed, undefined); + if (event._tag === "remoteUpdated") return Queue.offer(remoteUpdates, undefined); + return Effect.void; + }), + Effect.forkScoped, + ); + yield* Deferred.await(subscribed); + state.currentRemoteStatus = remoteStatusWithPr; + yield* TestClock.adjust("30 seconds"); + yield* Queue.take(remoteUpdates); + const localCalls = state.localStatusCalls; + const remoteCalls = state.remoteStatusCalls; + + state.currentRemoteStatus = baseRemoteStatus; + yield* TestClock.adjust("30 seconds"); + yield* Queue.take(remoteUpdates); + assert.equal(state.remoteStatusCalls, remoteCalls + 1); + assert.equal(state.localStatusCalls, localCalls); + + state.refSnapshot = "fetched"; + state.currentRemoteStatus = remoteStatusWithPr; + yield* TestClock.adjust("30 seconds"); + yield* Queue.take(remoteUpdates); + assert.equal(state.localStatusCalls, localCalls + 1); + }).pipe(Effect.provide(layerTestFor(state)), Effect.scoped); + }); + it.effect("reads Changes totals only while a subscriber shows them", () => { const branchChanges = { baseRef: "origin/main", insertions: 3, deletions: 1 }; const state: Parameters[0] = { diff --git a/apps/server/src/vcs/VcsStatusBroadcaster.ts b/apps/server/src/vcs/VcsStatusBroadcaster.ts index c39b82695102..8a507e799c82 100644 --- a/apps/server/src/vcs/VcsStatusBroadcaster.ts +++ b/apps/server/src/vcs/VcsStatusBroadcaster.ts @@ -668,6 +668,8 @@ export const make = Effect.gen(function* () { options?: { readonly refreshUpstream?: boolean; readonly policyCwds?: ReadonlyArray; + /** Refs seen by this caller's last local read; skips the re-read while they hold. */ + readonly refsAtLocalRead?: Ref.Ref; }, ) { return yield* withRemoteWriteLock( @@ -680,7 +682,17 @@ export const make = Effect.gen(function* () { : yield* maybeAutoPull(cwd, remoteRead, options?.policyCwds ?? [cwd]); if (pulled !== null) return pulled.remote; // Base refs and merge bases can move without changing divergence counts. - yield* refreshLocalStatusCore(cwd); + const refsAtLocalRead = options?.refsAtLocalRead; + if (refsAtLocalRead === undefined) { + yield* refreshLocalStatusCore(cwd); + } else { + const refs = yield* workflow.readRefSnapshot(cwd); + if (refs === null || refs !== (yield* Ref.get(refsAtLocalRead))) { + yield* Ref.set(refsAtLocalRead, null); + yield* refreshLocalStatusCore(cwd); + yield* Ref.set(refsAtLocalRead, refs); + } + } return yield* updateCachedRemoteStatus(cwd, remoteRead, { publish: true }); }), ); @@ -771,6 +783,7 @@ export const make = Effect.gen(function* () { ) { const consecutiveFailuresRef = yield* Ref.make(0); const needsInitialRefreshRef = yield* Ref.make(refreshImmediately); + const refsAtLocalReadRef = yield* Ref.make(null); const refreshRemoteStatusIfEnabled = Effect.gen(function* () { const configuredInterval = yield* automaticFetchInterval; const activeInterval = Duration.isZero(configuredInterval) @@ -800,6 +813,7 @@ export const make = Effect.gen(function* () { const exit = yield* refreshRemoteStatus(cwd, { refreshUpstream: !Duration.isZero(configuredInterval), policyCwds: [...demandCwds.keys()], + refsAtLocalRead: refsAtLocalReadRef, }).pipe(Effect.exit); if (Exit.isSuccess(exit)) { yield* Ref.set(needsInitialRefreshRef, false);