Skip to content

Commit 6598a8f

Browse files
committed
fix(server): keep VCS refresh responsive during slow remote lookups
1 parent aff9318 commit 6598a8f

3 files changed

Lines changed: 159 additions & 5 deletions

File tree

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

Lines changed: 131 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ function makeTestLayer(state: {
7878
remoteInvalidationCalls: number;
7979
remoteStatusRefreshUpstreamValues?: Array<boolean | undefined>;
8080
backgroundWorkEnabled?: boolean;
81+
beforeRemoteStatus?: Effect.Effect<void, GitManagerError>;
8182
}) {
8283
return VcsStatusBroadcaster.layer.pipe(
8384
Layer.provideMerge(NodeServices.layer),
@@ -90,9 +91,10 @@ function makeTestLayer(state: {
9091
return state.currentLocalStatus;
9192
}),
9293
remoteStatus: (_input, options) =>
93-
Effect.sync(() => {
94+
Effect.gen(function* () {
9495
state.remoteStatusCalls += 1;
9596
state.remoteStatusRefreshUpstreamValues?.push(options?.refreshUpstream);
97+
yield* state.beforeRemoteStatus ?? Effect.void;
9698
return state.currentRemoteStatus;
9799
}),
98100
invalidateLocalStatus: () =>
@@ -415,6 +417,134 @@ describe("VcsStatusBroadcaster", () => {
415417
}).pipe(Effect.provide(makeTestLayer(state)));
416418
});
417419

420+
it.effect("publishes local changes while an older remote refresh holds the write lock", () => {
421+
const remoteStarted = Deferred.makeUnsafe<void>();
422+
const releaseRemote = Deferred.makeUnsafe<void>();
423+
const initialSnapshot = Deferred.makeUnsafe<void>();
424+
const localUpdated = Deferred.makeUnsafe<VcsStatusLocalResult>();
425+
const refreshedSnapshot = Deferred.makeUnsafe<VcsStatusStreamEvent>();
426+
const state = {
427+
currentLocalStatus: baseLocalStatus,
428+
currentRemoteStatus: baseRemoteStatus,
429+
localStatusCalls: 0,
430+
remoteStatusCalls: 0,
431+
localInvalidationCalls: 0,
432+
remoteInvalidationCalls: 0,
433+
beforeRemoteStatus: Effect.void,
434+
};
435+
436+
return Effect.gen(function* () {
437+
const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster;
438+
yield* broadcaster.getStatus({ cwd: "/repo" });
439+
yield* broadcaster
440+
.streamStatus(
441+
{ cwd: "/repo" },
442+
{ automaticRemoteRefreshInterval: Effect.succeed(Duration.zero) },
443+
)
444+
.pipe(
445+
Stream.runForEach((event) => {
446+
if (event._tag === "localUpdated") return Deferred.succeed(localUpdated, event.local);
447+
if (event._tag === "snapshot" && event.local.hasWorkingTreeChanges) {
448+
return Deferred.succeed(refreshedSnapshot, event);
449+
}
450+
return Deferred.succeed(initialSnapshot, undefined);
451+
}),
452+
Effect.forkScoped,
453+
);
454+
yield* Deferred.await(initialSnapshot);
455+
state.beforeRemoteStatus = Deferred.succeed(remoteStarted, undefined).pipe(
456+
Effect.andThen(Deferred.await(releaseRemote)),
457+
);
458+
const olderRefresh = yield* broadcaster.refreshStatus("/repo").pipe(Effect.forkScoped);
459+
yield* Deferred.await(remoteStarted);
460+
state.currentLocalStatus = { ...baseLocalStatus, hasWorkingTreeChanges: true };
461+
state.currentRemoteStatus = remoteStatusWithPr;
462+
463+
const immediate = yield* broadcaster.refreshStatus("/repo", { waitForRemote: false });
464+
assert.isTrue(immediate.hasWorkingTreeChanges);
465+
assert.isNull(immediate.pr);
466+
assert.deepStrictEqual(yield* Deferred.await(localUpdated), state.currentLocalStatus);
467+
468+
yield* Deferred.succeed(releaseRemote, undefined);
469+
yield* Fiber.join(olderRefresh);
470+
assert.deepStrictEqual(yield* Deferred.await(refreshedSnapshot), {
471+
_tag: "snapshot",
472+
local: state.currentLocalStatus,
473+
remote: remoteStatusWithPr,
474+
});
475+
}).pipe(Effect.provide(makeTestLayer(state)), Effect.scoped);
476+
});
477+
478+
it.effect("coalesces remote work and cancels it when the broadcaster closes", () => {
479+
const remoteStarted = Deferred.makeUnsafe<void>();
480+
const remoteInterrupted = Deferred.makeUnsafe<void>();
481+
const state = {
482+
currentLocalStatus: baseLocalStatus,
483+
currentRemoteStatus: baseRemoteStatus,
484+
localStatusCalls: 0,
485+
remoteStatusCalls: 0,
486+
localInvalidationCalls: 0,
487+
remoteInvalidationCalls: 0,
488+
beforeRemoteStatus: Deferred.succeed(remoteStarted, undefined).pipe(
489+
Effect.andThen(Effect.never),
490+
Effect.onInterrupt(() => Deferred.succeed(remoteInterrupted, undefined)),
491+
),
492+
};
493+
494+
return Effect.gen(function* () {
495+
yield* Effect.gen(function* () {
496+
const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster;
497+
// No cached remote status: even the first request must return locally.
498+
const immediate = yield* broadcaster.refreshStatus("/repo", { waitForRemote: false });
499+
assert.equal(immediate.refName, baseLocalStatus.refName);
500+
yield* Deferred.await(remoteStarted);
501+
yield* Effect.forEach(
502+
Array.from({ length: 20 }),
503+
() => broadcaster.refreshStatus("/repo", { waitForRemote: false }),
504+
{ concurrency: "unbounded" },
505+
);
506+
assert.equal(state.remoteStatusCalls, 1);
507+
}).pipe(Effect.provide(makeTestLayer(state)));
508+
assert.isTrue(Option.isSome(yield* Deferred.poll(remoteInterrupted)));
509+
});
510+
});
511+
512+
it.effect("clears failed remote work and does not return a previous branch's PR", () => {
513+
const state: Parameters<typeof makeTestLayer>[0] = {
514+
currentLocalStatus: baseLocalStatus,
515+
currentRemoteStatus: remoteStatusWithPr,
516+
localStatusCalls: 0,
517+
remoteStatusCalls: 0,
518+
localInvalidationCalls: 0,
519+
remoteInvalidationCalls: 0,
520+
beforeRemoteStatus: Effect.void,
521+
};
522+
523+
return Effect.gen(function* () {
524+
const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster;
525+
yield* broadcaster.getStatus({ cwd: "/repo" });
526+
state.beforeRemoteStatus = Effect.fail(
527+
new GitManagerError({
528+
operation: "remoteStatus",
529+
cwd: "/repo",
530+
detail: "Network unavailable",
531+
}),
532+
);
533+
const cached = yield* broadcaster.refreshStatus("/repo", { waitForRemote: false });
534+
assert.deepStrictEqual(cached.pr, remoteStatusWithPr.pr);
535+
yield* TestClock.adjust(Duration.zero);
536+
537+
state.currentLocalStatus = { ...baseLocalStatus, refName: "feature/next" };
538+
state.currentRemoteStatus = baseRemoteStatus;
539+
state.beforeRemoteStatus = Effect.void;
540+
const switched = yield* broadcaster.refreshStatus("/repo", { waitForRemote: false });
541+
assert.isNull(switched.pr);
542+
yield* TestClock.adjust(Duration.zero);
543+
assert.equal(state.remoteStatusCalls, 3);
544+
assert.isNull((yield* broadcaster.getStatus({ cwd: "/repo" })).pr);
545+
}).pipe(Effect.provide(makeTestLayer(state)));
546+
});
547+
418548
it.effect("keeps the cached snapshot unchanged when a refresh branch fails", () => {
419549
const state = {
420550
currentLocalStatus: baseLocalStatus,

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

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,10 @@ export class VcsStatusBroadcaster extends Context.Service<
191191
readonly refreshLocalStatus: (
192192
cwd: string,
193193
) => Effect.Effect<VcsStatusLocalResult, GitManagerServiceError>;
194-
readonly refreshStatus: (cwd: string) => Effect.Effect<VcsStatusResult, GitManagerServiceError>;
194+
readonly refreshStatus: (
195+
cwd: string,
196+
options?: { readonly waitForRemote?: boolean },
197+
) => Effect.Effect<VcsStatusResult, GitManagerServiceError>;
195198
/**
196199
* Refresh a loaded cwd after a turn if background policy allows it.
197200
* GitManager retries missing PRs for the current branch and keeps known
@@ -470,13 +473,14 @@ export const make = Effect.gen(function* () {
470473
);
471474
});
472475

476+
const pendingStatusRefreshes = new Set<string>();
473477
const refreshStatus: VcsStatusBroadcaster["Service"]["refreshStatus"] = Effect.fn(
474478
"VcsStatusBroadcaster.refreshStatus",
475-
)(function* (rawCwd) {
479+
)(function* (rawCwd, options) {
476480
const cwd = yield* withFileSystem(normalizeCwd(rawCwd));
477481
// invalidateStatus (not the two partial invalidations) so an explicit
478482
// refresh also bypasses GitManager's slow PR-lookup cache.
479-
return yield* withRemoteWriteLock(
483+
const refresh = withRemoteWriteLock(
480484
cwd,
481485
Effect.gen(function* () {
482486
yield* workflow.invalidateStatus(cwd);
@@ -489,6 +493,26 @@ export const make = Effect.gen(function* () {
489493
return yield* updateCachedStatus(cwd, local, remote, { publish: true });
490494
}),
491495
);
496+
if (options?.waitForRemote !== false) return yield* refresh;
497+
498+
const cached = yield* getCachedStatus(cwd);
499+
const local = yield* refreshLocalStatusCore(cwd);
500+
// The UI already subscribes to status updates. A slow fetch or PR lookup
501+
// must not hold its refresh request open or queue duplicate remote reads.
502+
yield* Effect.uninterruptibleMask((restore) =>
503+
Effect.gen(function* () {
504+
if (pendingStatusRefreshes.has(cwd)) return;
505+
pendingStatusRefreshes.add(cwd);
506+
yield* restore(refresh).pipe(
507+
Effect.ignoreCause({ log: true }),
508+
Effect.onExit(() => Effect.sync(() => pendingStatusRefreshes.delete(cwd))),
509+
Effect.forkIn(broadcasterScope),
510+
);
511+
}),
512+
);
513+
const remote =
514+
cached?.local?.value.refName === local.refName ? (cached?.remote?.value ?? null) : null;
515+
return mergeGitStatusParts(local, remote);
492516
});
493517

494518
const refreshPullRequestStatus: VcsStatusBroadcaster["Service"]["refreshPullRequestStatus"] =

‎apps/server/src/ws.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3261,7 +3261,7 @@ const makeWsRpcLayer = (
32613261
[WS_METHODS.vcsRefreshStatus]: (input) =>
32623262
observeRpcEffect(
32633263
WS_METHODS.vcsRefreshStatus,
3264-
vcsStatusBroadcaster.refreshStatus(input.cwd),
3264+
vcsStatusBroadcaster.refreshStatus(input.cwd, { waitForRemote: false }),
32653265
{
32663266
"rpc.aggregate": "vcs",
32673267
},

0 commit comments

Comments
 (0)