diff --git a/apps/server/src/vcs/VcsStatusBroadcaster.test.ts b/apps/server/src/vcs/VcsStatusBroadcaster.test.ts index d30997266c7b..995c3d5e6158 100644 --- a/apps/server/src/vcs/VcsStatusBroadcaster.test.ts +++ b/apps/server/src/vcs/VcsStatusBroadcaster.test.ts @@ -2030,6 +2030,55 @@ describe("VcsStatusBroadcaster", () => { }), ); + it.effect("a checkout's own refresh also serves the HEAD watch it set off", () => + Effect.gen(function* () { + const events = yield* Queue.unbounded(); + const state: Parameters[0] = { + currentLocalStatus: { ...baseLocalStatus, isDefaultRef: true, refName: "main" }, + currentRemoteStatus: baseRemoteStatus, + localStatusCalls: 0, + remoteStatusCalls: 0, + localInvalidationCalls: 0, + remoteInvalidationCalls: 0, + headPath: "/repo/.git/HEAD", + fileSystem: FileSystem.makeNoop({ + realPath: (target) => Effect.succeed(target), + exists: () => Effect.succeed(true), + watch: (_target, options) => + Stream.fromQueue(events).pipe(Stream.onStart(Effect.sync(() => options?.onReady?.()))), + }), + }; + + yield* Effect.gen(function* () { + const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster; + yield* broadcaster.getStatus({ cwd: "/repo" }); + const subscribed = yield* Deferred.make(); + yield* broadcaster.streamStatus({ cwd: "/repo" }).pipe( + Stream.runForEach(() => Deferred.succeed(subscribed, undefined)), + Effect.forkScoped, + ); + yield* Deferred.await(subscribed); + + // `git switch` moves HEAD before the switchRef RPC starts its own refresh. + state.currentLocalStatus = { ...baseLocalStatus, isDefaultRef: false, refName: "other" }; + state.localStatusCalls = 0; + const watchReading = yield* Deferred.make(); + const releaseWatch = yield* Deferred.make(); + state.beforeRemoteStatus = Deferred.succeed(watchReading, undefined).pipe( + Effect.andThen(Deferred.await(releaseWatch)), + ); + yield* Queue.offer(events, { _tag: "Update", path: "HEAD" }); + yield* Deferred.await(watchReading); + state.beforeRemoteStatus = Effect.void; + yield* broadcaster.refreshAfterCheckout("/repo"); + yield* Deferred.succeed(releaseWatch, undefined); + // Queued behind the watch's and the checkout's remote reads. + yield* broadcaster.refreshPullRequestStatus("/repo"); + assert.strictEqual(state.localStatusCalls, 1); + }).pipe(Effect.provide(layerTestFor(state)), Effect.scoped); + }), + ); + it.effect("subscribers share one HEAD watch that ends with the last of them", () => Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; @@ -2501,3 +2550,108 @@ it.effect("does not replay older queued branches after a new subscriber snapshot yield* Fiber.join(subscriber); }).pipe(Effect.provide(layerTestFor(state))); }); + +// `remoteUpdated` is published after the first poll has decided whether to re-read local status. +const subscribeForFirstPoll = Effect.fn("test.subscribeForFirstPoll")(function* ( + b: VcsStatusBroadcaster.VcsStatusBroadcaster["Service"], +) { + const polled = yield* Deferred.make(); + yield* b.streamStatus({ cwd: "/repo" }).pipe( + Stream.runForEach((event) => + event._tag === "remoteUpdated" ? Deferred.succeed(polled, undefined) : Effect.void, + ), + Effect.forkScoped({ startImmediately: true }), + ); + return polled; +}); + +it.effect("a retried first poll reads local status again", () => { + const state = newState(); + state.currentRemoteStatus = baseRemoteStatus; + return Effect.gen(function* () { + const b = yield* VcsStatusBroadcaster.VcsStatusBroadcaster; + const firstFailed = yield* Deferred.make(); + let attempts = 0; + state.beforeRemoteStatus = Effect.suspend(() => + attempts++ === 0 + ? Deferred.succeed(firstFailed, undefined).pipe( + Effect.andThen( + Effect.fail( + new GitManagerError({ operation: "test", cwd: "/repo", detail: "offline" }), + ), + ), + ) + : Effect.void, + ); + const polled = yield* subscribeForFirstPoll(b); + yield* Deferred.await(firstFailed); + yield* TestClock.adjust("30 seconds"); + yield* Deferred.await(polled); + // The initial load, then the retry: its baseline was spent on the failed attempt. + assert.strictEqual(state.localStatusCalls, 2); + }).pipe(Effect.provide(layerTestFor(state))); +}); + +it.effect("a first subscription's poll re-reads when its load reused a cached result", () => { + const state = newState(); + state.currentRemoteStatus = baseRemoteStatus; + return Effect.gen(function* () { + const b = yield* VcsStatusBroadcaster.VcsStatusBroadcaster; + // Caches local status without a remote, so the subscription's load reads nothing itself. + yield* b.refreshStatus("/repo", { localOnly: true }); + yield* Deferred.await(yield* subscribeForFirstPoll(b)); + // The refresh's read, then the poll's: a snapshot cannot vouch for a read it did not precede. + assert.strictEqual(state.localStatusCalls, 2); + }).pipe(Effect.provide(layerTestFor(state))); +}); + +it.effect.each([ + { refs: "unchanged", localReads: 1, invalidations: 1 }, + { refs: "moved", localReads: 2, invalidations: 2 }, +] as const)( + "a first subscription's poll re-reads local status only when refs moved (%j)", + ({ refs, localReads, invalidations }) => { + const state = newState(); + state.currentRemoteStatus = baseRemoteStatus; + if (refs === "moved") { + state.beforeRemoteStatus = Effect.sync(() => { + state.refSnapshot = "moved"; + }); + } + return Effect.gen(function* () { + const b = yield* VcsStatusBroadcaster.VcsStatusBroadcaster; + yield* Deferred.await(yield* subscribeForFirstPoll(b)); + assert.strictEqual(state.localStatusCalls, localReads); + // The certified read must come from git, not the workflow's one-second cache. + assert.strictEqual(state.localInvalidationCalls, invalidations); + }).pipe(Effect.provide(layerTestFor(state))); + }, +); + +it.effect.each([ + { refs: "unchanged", localReads: 1 }, + { refs: "moved", localReads: 2 }, +] as const)( + "a checkout re-reads local status after its remote read only when refs moved (%j)", + ({ refs, localReads }) => { + const state = newState(); + state.currentRemoteStatus = baseRemoteStatus; + return Effect.gen(function* () { + const b = yield* VcsStatusBroadcaster.VcsStatusBroadcaster; + yield* b.getStatus({ cwd: "/repo" }); + state.localStatusCalls = 0; + const remoteStarted = yield* Deferred.make(); + const releaseRemote = yield* Deferred.make(); + state.beforeRemoteStatus = Deferred.succeed(remoteStarted, undefined).pipe( + Effect.andThen(Deferred.await(releaseRemote)), + ); + yield* b.refreshAfterCheckout("/repo"); + yield* Deferred.await(remoteStarted); + if (refs === "moved") state.refSnapshot = "moved"; + yield* Deferred.succeed(releaseRemote, undefined); + // The checkout's background refresh holds the remote lock until its local decision is made. + yield* b.refreshPullRequestStatus("/repo"); + assert.strictEqual(state.localStatusCalls, localReads); + }).pipe(Effect.provide(layerTestFor(state))); + }, +); diff --git a/apps/server/src/vcs/VcsStatusBroadcaster.ts b/apps/server/src/vcs/VcsStatusBroadcaster.ts index 5842478fffe7..94e1d92cf07e 100644 --- a/apps/server/src/vcs/VcsStatusBroadcaster.ts +++ b/apps/server/src/vcs/VcsStatusBroadcaster.ts @@ -163,6 +163,11 @@ interface ActiveHeadWatcher { readonly subscriberCount: number; } +interface InitialRefs { + /** Emptied by the poll that reads it, so the long-lived loop does not keep the snapshot. */ + refs: string | null; +} + interface ActiveRemotePoller { readonly fiber: Fiber.Fiber; readonly subscriberCount: number; @@ -476,7 +481,7 @@ export const make = Effect.gen(function* () { }); const getOrLoadLocalStatusUnlocked = Effect.fn("VcsStatusBroadcaster.getOrLoadLocalStatus")( - function* (cwd: string) { + function* (cwd: string, initialRefs?: InitialRefs) { const cached = yield* getCachedStatus(cwd); if (cached?.local) { if (cached.local.includesBranchChanges || !wantsBranchChanges(cwd)) { @@ -486,12 +491,16 @@ export const make = Effect.gen(function* () { yield* workflow.invalidateLocalStatus(cwd); return yield* loadLocalStatus(cwd, { publish: true }); } + if (initialRefs === undefined) return yield* loadLocalStatus(cwd); + // Only a fresh git read follows the snapshot; the workflow's short cache may predate it. + yield* workflow.invalidateLocalStatus(cwd); + initialRefs.refs = yield* workflow.readRefSnapshot(cwd); return yield* loadLocalStatus(cwd); }, ); - const getOrLoadLocalStatus = (cwd: string) => - localRefreshLocks.withLock(cwd, getOrLoadLocalStatusUnlocked(cwd)); + const getOrLoadLocalStatus = (cwd: string, initialRefs?: InitialRefs) => + localRefreshLocks.withLock(cwd, getOrLoadLocalStatusUnlocked(cwd, initialRefs)); const getStatusParts = Effect.fn("VcsStatusBroadcaster.getStatusParts")(function* (cwd: string) { const cached = yield* getCachedStatus(cwd); @@ -526,10 +535,16 @@ export const make = Effect.gen(function* () { ); }, Effect.scoped); + const requestLocalRefresh = (cwd: string) => { + const requestedAt = (localRefreshRequests.get(cwd) ?? 0) + 1; + localRefreshRequests.set(cwd, requestedAt); + return requestedAt; + }; + + // `earlierRequest` lets a caller that learned of a change earlier share any read started since. const refreshLocalStatusCore = Effect.fn("VcsStatusBroadcaster.refreshLocalStatusCore")( - function* (cwd: string) { - const requestedAt = (localRefreshRequests.get(cwd) ?? 0) + 1; - localRefreshRequests.set(cwd, requestedAt); + function* (cwd: string, earlierRequest?: number) { + const requestedAt = earlierRequest ?? requestLocalRefresh(cwd); return yield* localRefreshLocks.withLock( cwd, Effect.gen(function* () { @@ -674,6 +689,7 @@ export const make = Effect.gen(function* () { readonly policyCwds?: ReadonlyArray; /** Refs seen by this caller's last local read; skips the re-read while they hold. */ readonly refsAtLocalRead?: Ref.Ref; + readonly localRequestedAt?: number; }, ) { return yield* withRemoteWriteLock( @@ -688,12 +704,12 @@ export const make = Effect.gen(function* () { // Base refs and merge bases can move without changing divergence counts. const refsAtLocalRead = options?.refsAtLocalRead; if (refsAtLocalRead === undefined) { - yield* refreshLocalStatusCore(cwd); + yield* refreshLocalStatusCore(cwd, options?.localRequestedAt); } else { const refs = yield* workflow.readRefSnapshot(cwd); if (refs === null || refs !== (yield* Ref.get(refsAtLocalRead))) { yield* Ref.set(refsAtLocalRead, null); - yield* refreshLocalStatusCore(cwd); + yield* refreshLocalStatusCore(cwd, options?.localRequestedAt); yield* Ref.set(refsAtLocalRead, refs); } } @@ -759,9 +775,10 @@ export const make = Effect.gen(function* () { const cwd = yield* withFileSystem(normalizeCwd(rawCwd)); const localDone = yield* Deferred.make(); // Owned by the broadcaster so a client disconnecting mid-switch still leaves status correct. - yield* refreshLocalStatusCore(cwd).pipe( + yield* workflow.readRefSnapshot(cwd).pipe( + Effect.tap(() => refreshLocalStatusCore(cwd)), Effect.ensuring(Deferred.succeed(localDone, undefined)), - Effect.andThen(refreshRemoteAndLocalTotals(cwd, rawCwd, null)), + Effect.flatMap((refs) => refreshRemoteAndLocalTotals(cwd, rawCwd, refs)), Effect.ignoreCause({ log: true }), Effect.forkIn(broadcasterScope), ); @@ -791,6 +808,7 @@ export const make = Effect.gen(function* () { cwd: string, demandCwdsRef: Ref.Ref>, refreshImmediately: boolean, + initialRefs: InitialRefs, ) { const consecutiveFailuresRef = yield* Ref.make(0); const needsInitialRefreshRef = yield* Ref.make(refreshImmediately); @@ -821,6 +839,11 @@ export const make = Effect.gen(function* () { return activeInterval; } + // The read made with this subscription vouches for the first attempt only; a failed + // attempt drops it, so the retry reads again. + const subscriptionRefs = initialRefs.refs; + initialRefs.refs = null; + if (subscriptionRefs !== null) yield* Ref.set(refsAtLocalReadRef, subscriptionRefs); const exit = yield* refreshRemoteStatus(cwd, { refreshUpstream: !Duration.isZero(configuredInterval), policyCwds: [...demandCwds.keys()], @@ -831,6 +854,7 @@ export const make = Effect.gen(function* () { yield* Ref.set(consecutiveFailuresRef, 0); return activeInterval; } + if (subscriptionRefs !== null) yield* Ref.set(refsAtLocalReadRef, null); const interruptionReasons = exit.cause.reasons.filter(Cause.isInterruptReason); if (interruptionReasons.length > 0) { @@ -874,6 +898,7 @@ export const make = Effect.gen(function* () { cwd: string, demandCwd: string, refreshImmediately: boolean, + initialRefs: InitialRefs, ) { yield* SynchronizedRef.modifyEffect(pollersRef, (activePollers) => { const existing = activePollers.get(cwd); @@ -896,7 +921,7 @@ export const make = Effect.gen(function* () { return Ref.make>(new Map([[demandCwd, 1]])).pipe( Effect.flatMap((demandCwds) => - makeRemoteRefreshLoop(cwd, demandCwds, refreshImmediately).pipe( + makeRemoteRefreshLoop(cwd, demandCwds, refreshImmediately, initialRefs).pipe( Effect.forkIn(broadcasterScope), Effect.map((fiber) => { const nextPollers = new Map(activePollers); @@ -1057,12 +1082,12 @@ export const make = Effect.gen(function* () { }); const reconcileCheckedOutRef = Effect.fn("VcsStatusBroadcaster.reconcileCheckedOutRef")( - function* (cwd: string) { + function* (cwd: string, localRequestedAt: number) { if (!detailedSubscribers.has(cwd)) return; if ((yield* getCachedStatus(cwd))?.remote) { - yield* refreshRemoteStatus(cwd, { refreshUpstream: false }); + yield* refreshRemoteStatus(cwd, { refreshUpstream: false, localRequestedAt }); } else { - yield* refreshLocalStatusCore(cwd); + yield* refreshLocalStatusCore(cwd, localRequestedAt); } }, ); @@ -1096,12 +1121,16 @@ export const make = Effect.gen(function* () { : Effect.succeed(false), ), Stream.filter((event) => path.basename(event.path) === "HEAD"), + // Requested on arrival, so a checkout's own refresh that starts later also serves this move. + Stream.map(() => requestLocalRefresh(cwd)), // A rebase moves HEAD many times; refresh once for whatever arrived during a refresh. Stream.buffer({ capacity: 1, strategy: "sliding" }), - Stream.mapEffect(() => retryCheckedOutRef(cwd, publishCheckedOutRef(cwd))), + Stream.tap(() => retryCheckedOutRef(cwd, publishCheckedOutRef(cwd))), // Detailed status waits behind its own reads; the branch keeps up on the stage above. Stream.buffer({ capacity: 1, strategy: "sliding" }), - Stream.runForEach(() => retryCheckedOutRef(cwd, reconcileCheckedOutRef(cwd))), + Stream.runForEach((requestedAt) => + retryCheckedOutRef(cwd, reconcileCheckedOutRef(cwd, requestedAt)), + ), ); }); @@ -1181,14 +1210,25 @@ export const make = Effect.gen(function* () { const subscription = yield* PubSub.subscribe(changesPubSub); // Watch before reading so a checkout in between still publishes. yield* Effect.acquireRelease(retainHeadWatcher(cwd), () => releaseHeadWatcher(cwd)); - const initialLocal = yield* getOrLoadLocalStatus(cwd); + // A cached remote means no immediate poll, so nothing would use the refs. Otherwise a fresh + // load records them for that poll. + const initialRefs: InitialRefs = { refs: null }; + const initialLocal = yield* getOrLoadLocalStatus( + cwd, + input.includeRemote !== false && (yield* getCachedStatus(cwd))?.remote == null + ? initialRefs + : undefined, + ); const cachedStatus = yield* getCachedStatus(cwd); const initialRemote = cachedStatus?.remote?.value ?? null; if (input.includeRemote !== false) { + const refreshImmediately = + cachedStatus?.remote === null || cachedStatus?.remote === undefined; yield* retainRemotePoller( cwd, input.cwd, - cachedStatus?.remote === null || cachedStatus?.remote === undefined, + refreshImmediately, + refreshImmediately ? initialRefs : { refs: null }, ); } const release =