diff --git a/apps/server/src/vcs/VcsStatusBroadcaster.test.ts b/apps/server/src/vcs/VcsStatusBroadcaster.test.ts index c89ec2d412..bc5828ead5 100644 --- a/apps/server/src/vcs/VcsStatusBroadcaster.test.ts +++ b/apps/server/src/vcs/VcsStatusBroadcaster.test.ts @@ -7,6 +7,7 @@ import * as Duration from "effect/Duration"; import * as Effect from "effect/Effect"; import * as Exit from "effect/Exit"; import * as FileSystem from "effect/FileSystem"; +import * as Fiber from "effect/Fiber"; import * as Layer from "effect/Layer"; import * as Logger from "effect/Logger"; import * as Option from "effect/Option"; @@ -76,6 +77,8 @@ function makeTestLayer(state: { remoteInvalidationCalls: number; fullInvalidationCalls?: number; remoteStatusRefreshUpstreamValues?: Array; + localStatusEffect?: (call: number) => Effect.Effect; + remoteStatusEffect?: (call: number) => Effect.Effect; }) { return VcsStatusBroadcaster.layer.pipe( Layer.provideMerge(NodeServices.layer), @@ -83,15 +86,21 @@ function makeTestLayer(state: { Layer.provide( Layer.mock(GitWorkflowService.GitWorkflowService)({ localStatus: () => - Effect.sync(() => { + Effect.suspend(() => { state.localStatusCalls += 1; - return state.currentLocalStatus; + return ( + state.localStatusEffect?.(state.localStatusCalls) ?? + Effect.succeed(state.currentLocalStatus) + ); }), remoteStatus: (_input, options) => - Effect.sync(() => { + Effect.suspend(() => { state.remoteStatusCalls += 1; state.remoteStatusRefreshUpstreamValues?.push(options?.refreshUpstream); - return state.currentRemoteStatus; + return ( + state.remoteStatusEffect?.(state.remoteStatusCalls) ?? + Effect.succeed(state.currentRemoteStatus) + ); }), invalidateLocalStatus: () => Effect.sync(() => { @@ -205,6 +214,50 @@ describe("VcsStatusBroadcaster", () => { }).pipe(Effect.provide(makeTestLayer(state))); }); + it.effect("serializes full polling with explicit local refreshes", () => + Effect.gen(function* () { + const pollReadStarted = yield* Deferred.make(); + const releasePollRead = yield* Deferred.make(); + const refreshedLocalStatus = { + ...baseLocalStatus, + refName: "feature/refreshed-after-poll", + }; + const state = { + currentLocalStatus: baseLocalStatus, + currentRemoteStatus: baseRemoteStatus, + localStatusCalls: 0, + remoteStatusCalls: 0, + localInvalidationCalls: 0, + remoteInvalidationCalls: 0, + localStatusEffect: (call: number) => + call === 1 + ? Deferred.succeed(pollReadStarted, undefined).pipe( + Effect.andThen(Deferred.await(releasePollRead)), + Effect.as(baseLocalStatus), + ) + : Effect.succeed(refreshedLocalStatus), + }; + + yield* Effect.gen(function* () { + const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster; + const pollFiber = yield* broadcaster.pollStatus("/repo").pipe(Effect.forkChild); + yield* Deferred.await(pollReadStarted); + const refreshFiber = yield* broadcaster.refreshLocalStatus("/repo").pipe(Effect.forkChild); + yield* Effect.yieldNow; + + assert.equal(state.localStatusCalls, 1); + yield* Deferred.succeed(releasePollRead, undefined); + yield* Fiber.join(pollFiber); + yield* Fiber.join(refreshFiber); + + assert.deepStrictEqual(yield* broadcaster.peekStatus({ cwd: "/repo" }), { + ...refreshedLocalStatus, + ...baseRemoteStatus, + }); + }).pipe(Effect.provide(makeTestLayer(state))); + }), + ); + it.effect("refreshes the cached snapshot after explicit invalidation", () => { const state = { currentLocalStatus: baseLocalStatus, diff --git a/apps/server/src/vcs/VcsStatusBroadcaster.ts b/apps/server/src/vcs/VcsStatusBroadcaster.ts index 8836cfa2d7..1389d28b6b 100644 --- a/apps/server/src/vcs/VcsStatusBroadcaster.ts +++ b/apps/server/src/vcs/VcsStatusBroadcaster.ts @@ -10,6 +10,7 @@ import * as PubSub from "effect/PubSub"; import * as Ref from "effect/Ref"; import * as Schedule from "effect/Schedule"; import * as Scope from "effect/Scope"; +import * as Semaphore from "effect/Semaphore"; import * as Stream from "effect/Stream"; import * as SynchronizedRef from "effect/SynchronizedRef"; import type { @@ -197,6 +198,22 @@ export const make = Effect.gen(function* () { ); const cacheRef = yield* Ref.make(new Map()); const pollersRef = yield* SynchronizedRef.make(new Map()); + const refreshLocksRef = yield* SynchronizedRef.make(new Map()); + + const getRefreshLock = (cwd: string) => + SynchronizedRef.modifyEffect(refreshLocksRef, (current) => { + const existing = current.get(cwd); + if (existing !== undefined) return Effect.succeed([existing, current] as const); + return Semaphore.make(1).pipe( + Effect.map((lock) => [lock, new Map(current).set(cwd, lock)] as const), + ); + }); + + const withRefreshLock = ( + cwd: string, + effect: Effect.Effect, + ): Effect.Effect => + Effect.flatMap(getRefreshLock(cwd), (lock) => lock.withPermits(1)(effect)); const getCachedStatus = Effect.fn("VcsStatusBroadcaster.getCachedStatus")(function* ( cwd: string, @@ -333,14 +350,23 @@ export const make = Effect.gen(function* () { if (cached?.local && cached.remote) { return mergeGitStatusParts(cached.local.value, cached.remote.value); } - const [local, remote] = yield* Effect.all( - [ - cached?.local ? Effect.succeed(cached.local.value) : workflow.localStatus({ cwd }), - cached?.remote ? Effect.succeed(cached.remote.value) : workflow.remoteStatus({ cwd }), - ], - { concurrency: "unbounded" }, + return yield* withRefreshLock( + cwd, + Effect.gen(function* () { + const latest = yield* getCachedStatus(cwd); + if (latest?.local && latest.remote) { + return mergeGitStatusParts(latest.local.value, latest.remote.value); + } + const [local, remote] = yield* Effect.all( + [ + latest?.local ? Effect.succeed(latest.local.value) : workflow.localStatus({ cwd }), + latest?.remote ? Effect.succeed(latest.remote.value) : workflow.remoteStatus({ cwd }), + ], + { concurrency: "unbounded" }, + ); + return yield* updateCachedStatus(cwd, local, remote); + }), ); - return yield* updateCachedStatus(cwd, local, remote); }); const peekStatus: VcsStatusBroadcaster["Service"]["peekStatus"] = Effect.fn( @@ -356,16 +382,22 @@ export const make = Effect.gen(function* () { "VcsStatusBroadcaster.pollStatus", )(function* (rawCwd) { const cwd = yield* withFileSystem(normalizeCwd(rawCwd)); - // Automatic polling refreshes the one-second status caches but deliberately - // leaves GitManager's slower PR cache and failure backoff intact. - yield* Effect.all([workflow.invalidateLocalStatus(cwd), workflow.invalidateRemoteStatus(cwd)], { - concurrency: "unbounded", - }); - const [local, remote] = yield* Effect.all( - [workflow.localStatus({ cwd }), workflow.remoteStatus({ cwd })], - { concurrency: "unbounded" }, + return yield* withRefreshLock( + cwd, + Effect.gen(function* () { + // Automatic polling refreshes the one-second status caches but deliberately + // leaves GitManager's slower PR cache and failure backoff intact. + yield* Effect.all( + [workflow.invalidateLocalStatus(cwd), workflow.invalidateRemoteStatus(cwd)], + { concurrency: "unbounded" }, + ); + const [local, remote] = yield* Effect.all( + [workflow.localStatus({ cwd }), workflow.remoteStatus({ cwd })], + { concurrency: "unbounded" }, + ); + return yield* updateCachedStatus(cwd, local, remote, { publish: true }); + }), ); - return yield* updateCachedStatus(cwd, local, remote, { publish: true }); }); const refreshLocalStatusCore = Effect.fn("VcsStatusBroadcaster.refreshLocalStatusCore")( @@ -380,32 +412,42 @@ export const make = Effect.gen(function* () { "VcsStatusBroadcaster.refreshLocalStatus", )(function* (rawCwd) { const cwd = yield* withFileSystem(normalizeCwd(rawCwd)); - return yield* refreshLocalStatusCore(cwd); + return yield* withRefreshLock(cwd, refreshLocalStatusCore(cwd)); }); const refreshRemoteStatus = Effect.fn("VcsStatusBroadcaster.refreshRemoteStatus")(function* ( cwd: string, options?: { readonly refreshUpstream?: boolean }, ) { - if (options?.refreshUpstream !== false) { - yield* workflow.invalidateRemoteStatus(cwd); - } - const remote = yield* workflow.remoteStatus({ cwd }, options); - return yield* updateCachedRemoteStatus(cwd, remote, { publish: true }); + return yield* withRefreshLock( + cwd, + Effect.gen(function* () { + if (options?.refreshUpstream !== false) { + yield* workflow.invalidateRemoteStatus(cwd); + } + const remote = yield* workflow.remoteStatus({ cwd }, options); + return yield* updateCachedRemoteStatus(cwd, remote, { publish: true }); + }), + ); }); const refreshStatus: VcsStatusBroadcaster["Service"]["refreshStatus"] = Effect.fn( "VcsStatusBroadcaster.refreshStatus", )(function* (rawCwd) { const cwd = yield* withFileSystem(normalizeCwd(rawCwd)); - // invalidateStatus (not the two partial invalidations) so an explicit - // refresh also bypasses GitManager's slow PR-lookup cache. - yield* workflow.invalidateStatus(cwd); - const [local, remote] = yield* Effect.all( - [workflow.localStatus({ cwd }), workflow.remoteStatus({ cwd })], - { concurrency: "unbounded" }, + return yield* withRefreshLock( + cwd, + Effect.gen(function* () { + // invalidateStatus (not the two partial invalidations) so an explicit + // refresh also bypasses GitManager's slow PR-lookup cache. + yield* workflow.invalidateStatus(cwd); + const [local, remote] = yield* Effect.all( + [workflow.localStatus({ cwd }), workflow.remoteStatus({ cwd })], + { concurrency: "unbounded" }, + ); + return yield* updateCachedStatus(cwd, local, remote, { publish: true }); + }), ); - return yield* updateCachedStatus(cwd, local, remote, { publish: true }); }); const makeRemoteRefreshLoop = (