Skip to content

Commit abb4206

Browse files
committed
fix(server): preserve newer VCS state during background refresh
1 parent 6598a8f commit abb4206

2 files changed

Lines changed: 128 additions & 16 deletions

File tree

‎apps/server/src/vcs/VcsStatusBroadcaster.test.ts‎

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,96 @@ describe("VcsStatusBroadcaster", () => {
475475
}).pipe(Effect.provide(makeTestLayer(state)), Effect.scoped);
476476
});
477477

478+
it.effect(
479+
"clears a previous branch's PR for cached reads and subscribers during remote work",
480+
() => {
481+
const started = Deferred.makeUnsafe<void>();
482+
const release = Deferred.makeUnsafe<void>();
483+
const subscribed = Deferred.makeUnsafe<void>();
484+
const changedBranch = Deferred.makeUnsafe<VcsStatusStreamEvent>();
485+
const state = {
486+
currentLocalStatus: baseLocalStatus,
487+
currentRemoteStatus: remoteStatusWithPr,
488+
localStatusCalls: 0,
489+
remoteStatusCalls: 0,
490+
localInvalidationCalls: 0,
491+
remoteInvalidationCalls: 0,
492+
beforeRemoteStatus: Effect.void,
493+
};
494+
return Effect.gen(function* () {
495+
const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster;
496+
yield* broadcaster.getStatus({ cwd: "/repo" });
497+
yield* broadcaster
498+
.streamStatus(
499+
{ cwd: "/repo" },
500+
{
501+
automaticRemoteRefreshInterval: Effect.succeed(Duration.zero),
502+
},
503+
)
504+
.pipe(
505+
Stream.runForEach((event) => {
506+
if (event._tag === "snapshot" && event.local.refName === "feature/next") {
507+
return Deferred.succeed(changedBranch, event);
508+
}
509+
return Deferred.succeed(subscribed, undefined);
510+
}),
511+
Effect.forkScoped,
512+
);
513+
yield* Deferred.await(subscribed);
514+
state.beforeRemoteStatus = Deferred.succeed(started, undefined).pipe(
515+
Effect.andThen(Deferred.await(release)),
516+
);
517+
yield* broadcaster.refreshStatus("/repo", { waitForRemote: false });
518+
yield* Deferred.await(started);
519+
state.currentLocalStatus = { ...baseLocalStatus, refName: "feature/next" };
520+
yield* broadcaster.refreshLocalStatus("/repo");
521+
assert.isNull((yield* broadcaster.getStatus({ cwd: "/repo" })).pr);
522+
assert.deepStrictEqual(yield* Deferred.await(changedBranch), {
523+
_tag: "snapshot",
524+
local: state.currentLocalStatus,
525+
remote: null,
526+
});
527+
assert.isNull((yield* broadcaster.getStatus({ cwd: "/repo" })).pr);
528+
assert.isNull((yield* broadcaster.refreshStatus("/repo", { waitForRemote: false })).pr);
529+
// The delayed result still describes the old branch.
530+
yield* Deferred.succeed(release, undefined);
531+
yield* TestClock.adjust(Duration.zero);
532+
const final = yield* broadcaster.getStatus({ cwd: "/repo" });
533+
assert.equal(final.refName, "feature/next");
534+
assert.isNull(final.pr);
535+
}).pipe(Effect.provide(makeTestLayer(state)), Effect.scoped);
536+
},
537+
);
538+
539+
it.effect("preserves newer working-tree status when a background refresh completes", () => {
540+
const started = Deferred.makeUnsafe<void>();
541+
const release = Deferred.makeUnsafe<void>();
542+
const state = {
543+
currentLocalStatus: baseLocalStatus,
544+
currentRemoteStatus: baseRemoteStatus,
545+
localStatusCalls: 0,
546+
remoteStatusCalls: 0,
547+
localInvalidationCalls: 0,
548+
remoteInvalidationCalls: 0,
549+
beforeRemoteStatus: Deferred.succeed(started, undefined).pipe(
550+
Effect.andThen(Deferred.await(release)),
551+
),
552+
};
553+
return Effect.gen(function* () {
554+
const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster;
555+
yield* broadcaster.refreshStatus("/repo", { waitForRemote: false });
556+
yield* Deferred.await(started);
557+
state.currentLocalStatus = { ...baseLocalStatus, hasWorkingTreeChanges: true };
558+
yield* broadcaster.refreshLocalStatus("/repo");
559+
state.currentRemoteStatus = remoteStatusWithPr;
560+
yield* Deferred.succeed(release, undefined);
561+
yield* TestClock.adjust(Duration.zero);
562+
const final = yield* broadcaster.getStatus({ cwd: "/repo" });
563+
assert.isTrue(final.hasWorkingTreeChanges);
564+
assert.deepStrictEqual(final.pr, remoteStatusWithPr.pr);
565+
}).pipe(Effect.provide(makeTestLayer(state)));
566+
});
567+
478568
it.effect("coalesces remote work and cancels it when the broadcaster closes", () => {
479569
const remoteStarted = Deferred.makeUnsafe<void>();
480570
const remoteInterrupted = Deferred.makeUnsafe<void>();
@@ -633,7 +723,6 @@ describe("VcsStatusBroadcaster", () => {
633723

634724
state.currentLocalStatus = {
635725
...baseLocalStatus,
636-
refName: "feature/local-only-refresh",
637726
hasWorkingTreeChanges: true,
638727
};
639728

‎apps/server/src/vcs/VcsStatusBroadcaster.ts‎

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -260,23 +260,29 @@ export const make = Effect.gen(function* () {
260260
fingerprint: fingerprintStatusPart(local),
261261
value: local,
262262
} satisfies CachedValue<VcsStatusLocalResult>;
263-
const shouldPublish = yield* Ref.modify(cacheRef, (cache) => {
263+
const change = yield* Ref.modify(cacheRef, (cache) => {
264264
const previous = cache.get(cwd) ?? { local: null, remote: null };
265+
const branchChanged =
266+
previous.local !== null && previous.local.value.refName !== local.refName;
265267
const nextCache = new Map(cache);
266268
nextCache.set(cwd, {
267-
...previous,
268269
local: nextLocal,
270+
remote: branchChanged
271+
? { fingerprint: fingerprintStatusPart(null), value: null }
272+
: previous.remote,
269273
});
270-
return [previous.local?.fingerprint !== nextLocal.fingerprint, nextCache] as const;
274+
return [
275+
{ changed: previous.local?.fingerprint !== nextLocal.fingerprint, branchChanged },
276+
nextCache,
277+
] as const;
271278
});
272279

273-
if (options?.publish && shouldPublish) {
280+
if (options?.publish && change.changed) {
274281
yield* PubSub.publish(changesPubSub, {
275282
cwd,
276-
event: {
277-
_tag: "localUpdated",
278-
local,
279-
},
283+
event: change.branchChanged
284+
? { _tag: "snapshot", local, remote: null }
285+
: { _tag: "localUpdated", local },
280286
});
281287
}
282288

@@ -318,18 +324,33 @@ export const make = Effect.gen(function* () {
318324
cwd: string,
319325
local: VcsStatusLocalResult,
320326
remote: VcsStatusRemoteResult | null,
321-
options?: { publish?: boolean },
327+
options?: { publish?: boolean; expectedLocal?: CachedValue<VcsStatusLocalResult> | null },
322328
) {
323-
const nextLocal = {
329+
let nextLocal = {
324330
fingerprint: fingerprintStatusPart(local),
325331
value: local,
326332
} satisfies CachedValue<VcsStatusLocalResult>;
327-
const nextRemote = {
333+
let nextRemote = {
328334
fingerprint: fingerprintStatusPart(remote),
329335
value: remote,
330336
} satisfies CachedValue<VcsStatusRemoteResult | null>;
331337
const shouldPublish = yield* Ref.modify(cacheRef, (cache) => {
332338
const previous = cache.get(cwd) ?? { local: null, remote: null };
339+
if (
340+
options &&
341+
"expectedLocal" in options &&
342+
previous.local !== options.expectedLocal &&
343+
previous.local
344+
) {
345+
// A local refresh completed while this remote read was in flight.
346+
// Preserve its local state, and never attach an old branch's remote result.
347+
if (previous.local.value.refName !== local.refName) {
348+
remote = previous.remote?.value ?? null;
349+
nextRemote = { fingerprint: fingerprintStatusPart(remote), value: remote };
350+
}
351+
local = previous.local.value;
352+
nextLocal = previous.local;
353+
}
333354
const nextCache = new Map(cache);
334355
nextCache.set(cwd, {
335356
local: nextLocal,
@@ -483,20 +504,22 @@ export const make = Effect.gen(function* () {
483504
const refresh = withRemoteWriteLock(
484505
cwd,
485506
Effect.gen(function* () {
507+
const expectedLocal = (yield* getCachedStatus(cwd))?.local ?? null;
486508
yield* workflow.invalidateStatus(cwd);
487509
const [local, remote] = yield* Effect.all(
488510
[workflow.localStatus({ cwd }), workflow.remoteStatus({ cwd })],
489511
{ concurrency: "unbounded" },
490512
);
491-
const pulled = yield* maybeAutoPull(cwd, remote, [rawCwd]);
513+
const unchanged = ((yield* getCachedStatus(cwd))?.local ?? null) === expectedLocal;
514+
const pulled = unchanged ? yield* maybeAutoPull(cwd, remote, [rawCwd]) : null;
492515
if (pulled !== null) return mergeGitStatusParts(pulled.local, pulled.remote);
493-
return yield* updateCachedStatus(cwd, local, remote, { publish: true });
516+
return yield* updateCachedStatus(cwd, local, remote, { publish: true, expectedLocal });
494517
}),
495518
);
496519
if (options?.waitForRemote !== false) return yield* refresh;
497520

498-
const cached = yield* getCachedStatus(cwd);
499521
const local = yield* refreshLocalStatusCore(cwd);
522+
const cached = yield* getCachedStatus(cwd);
500523
// The UI already subscribes to status updates. A slow fetch or PR lookup
501524
// must not hold its refresh request open or queue duplicate remote reads.
502525
yield* Effect.uninterruptibleMask((restore) =>
@@ -522,7 +545,7 @@ export const make = Effect.gen(function* () {
522545
cwd,
523546
Effect.gen(function* () {
524547
const cached = yield* getCachedStatus(cwd);
525-
if (cached?.remote?.value == null) return null;
548+
if (cached?.remote == null || !cached.local?.value.isRepo) return null;
526549
const poller = (yield* SynchronizedRef.get(pollersRef)).get(cwd);
527550
const demandCwds = poller ? [...(yield* Ref.get(poller.demandCwds)).keys()] : [rawCwd];
528551
const shouldRefresh = (yield* Effect.forEach(

0 commit comments

Comments
 (0)