Skip to content

Commit 5344bb3

Browse files
committed
fix(vcs): do not reuse a pre-invalidation in-flight status refresh
Overlapping explicit refreshes still share one run. After invalidateStatus, a later refreshStatus waits for a follow-up recompute so pull, publish, and branch rename cannot cache pre-mutation status.
1 parent bd22561 commit 5344bb3

8 files changed

Lines changed: 196 additions & 30 deletions

File tree

‎apps/server/integration/OrchestrationEngineHarness.integration.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ export const makeOrchestrationIntegrationHarness = (
351351
hasWorkingTreeChanges: false,
352352
workingTree: { files: [], insertions: 0, deletions: 0 },
353353
}),
354+
invalidateStatus: () => Effect.void,
354355
refreshStatus: () => Effect.die("refreshStatus should not be called in this test"),
355356
streamStatus: () => Stream.empty,
356357
}),

‎apps/server/src/orchestration/Layers/CheckpointReactor.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ describe("CheckpointReactor", () => {
330330
workingTree: { files: [], insertions: 0, deletions: 0 },
331331
}),
332332
),
333+
invalidateStatus: () => Effect.void,
333334
refreshStatus: () => Effect.die("refreshStatus should not be called in this test"),
334335
streamStatus: () => Stream.empty,
335336
});

‎apps/server/src/orchestration/Layers/ProviderCommandReactor.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,7 @@ describe("ProviderCommandReactor", () => {
402402
getStatus: () => Effect.die("getStatus should not be called in this test"),
403403
refreshLocalStatus: () =>
404404
Effect.die("refreshLocalStatus should not be called in this test"),
405+
invalidateStatus: () => Effect.void,
405406
refreshStatus,
406407
streamStatus: () => Stream.die("streamStatus should not be called in this test"),
407408
}),

‎apps/server/src/orchestration/Layers/ProviderCommandReactor.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -829,7 +829,12 @@ const make = Effect.gen(function* () {
829829
branch: renamed.branch,
830830
worktreePath: cwd,
831831
});
832-
yield* vcsStatusBroadcaster.refreshStatus(cwd).pipe(Effect.ignoreCause({ log: true }));
832+
yield* vcsStatusBroadcaster
833+
.invalidateStatus(cwd)
834+
.pipe(
835+
Effect.andThen(vcsStatusBroadcaster.refreshStatus(cwd)),
836+
Effect.ignoreCause({ log: true }),
837+
);
833838
}).pipe(
834839
Effect.catchCause((cause) =>
835840
Effect.logWarning("provider command reactor failed to generate or rename worktree branch", {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5273,6 +5273,7 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
52735273
switchRef: (input) => Effect.succeed({ refName: input.refName }),
52745274
},
52755275
vcsStatusBroadcaster: {
5276+
invalidateStatus: () => Effect.void,
52765277
refreshStatus: () =>
52775278
Effect.succeed({
52785279
isRepo: true,
@@ -7380,6 +7381,7 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
73807381
createWorktree,
73817382
},
73827383
vcsStatusBroadcaster: {
7384+
invalidateStatus: () => Effect.void,
73837385
refreshStatus,
73847386
},
73857387
orchestrationEngine: {

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

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,20 +146,22 @@ function makeGatedRefreshLayer(state: GatedRefreshState, gates: GatedRefreshGate
146146
Effect.suspend(() => {
147147
state.seenCwds?.push(input.cwd);
148148
state.localStatusCalls += 1;
149+
const local = state.currentLocalStatus;
149150
return (
150151
gates.started === null || state.localStatusCalls < startedAfter
151152
? Effect.void
152153
: Deferred.succeed(gates.started, undefined).pipe(Effect.ignore)
153154
).pipe(
154155
Effect.andThen(gates.release === null ? Effect.void : Deferred.await(gates.release)),
155-
Effect.as(state.currentLocalStatus),
156+
Effect.as(local),
156157
);
157158
}),
158159
remoteStatus: () =>
159160
Effect.suspend(() => {
160161
state.remoteStatusCalls += 1;
162+
const remote = state.currentRemoteStatus;
161163
return (gates.release === null ? Effect.void : Deferred.await(gates.release)).pipe(
162-
Effect.as(state.currentRemoteStatus),
164+
Effect.as(remote),
163165
);
164166
}),
165167
invalidateLocalStatus: () =>
@@ -1012,4 +1014,61 @@ describe("VcsStatusBroadcaster", () => {
10121014
assert.deepStrictEqual([...state.seenCwds].toSorted(), ["/repo-a", "/repo-b"]);
10131015
}).pipe(Effect.provide(makeGatedRefreshLayer(state, gates)));
10141016
});
1017+
1018+
it.effect("does not reuse a pre-invalidation in-flight status refresh", () => {
1019+
const firstLocal = baseLocalStatus;
1020+
const firstRemote = baseRemoteStatus;
1021+
const nextLocal = {
1022+
...baseLocalStatus,
1023+
refName: "feature/after-mutation",
1024+
};
1025+
const nextRemote = {
1026+
...baseRemoteStatus,
1027+
aheadCount: 4,
1028+
};
1029+
const state = {
1030+
currentLocalStatus: firstLocal,
1031+
currentRemoteStatus: firstRemote,
1032+
localStatusCalls: 0,
1033+
remoteStatusCalls: 0,
1034+
localInvalidationCalls: 0,
1035+
remoteInvalidationCalls: 0,
1036+
};
1037+
const gates: GatedRefreshGates = { started: null, release: null };
1038+
1039+
return Effect.gen(function* () {
1040+
gates.started = yield* Deferred.make<void>();
1041+
gates.release = yield* Deferred.make<void>();
1042+
const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster;
1043+
1044+
const first = yield* broadcaster
1045+
.refreshStatus("/repo")
1046+
.pipe(Effect.forkChild({ startImmediately: true }));
1047+
1048+
yield* Deferred.await(gates.started);
1049+
state.currentLocalStatus = nextLocal;
1050+
state.currentRemoteStatus = nextRemote;
1051+
yield* broadcaster.invalidateStatus("/repo");
1052+
const afterMutation = yield* broadcaster
1053+
.refreshStatus("/repo")
1054+
.pipe(Effect.forkChild({ startImmediately: true }));
1055+
1056+
yield* Effect.yieldNow;
1057+
yield* Deferred.succeed(gates.release, undefined);
1058+
1059+
const firstResult = yield* Fiber.join(first);
1060+
const afterMutationResult = yield* Fiber.join(afterMutation);
1061+
const cached = yield* broadcaster.getStatus({ cwd: "/repo" });
1062+
const nextStatus = {
1063+
...nextLocal,
1064+
...nextRemote,
1065+
};
1066+
1067+
assert.deepStrictEqual(afterMutationResult, nextStatus);
1068+
assert.deepStrictEqual(cached, nextStatus);
1069+
assert.deepStrictEqual(firstResult, nextStatus);
1070+
assert.equal(state.localStatusCalls, 2);
1071+
assert.equal(state.remoteStatusCalls, 2);
1072+
}).pipe(Effect.provide(makeGatedRefreshLayer(state, gates)));
1073+
});
10151074
});

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

Lines changed: 117 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,12 @@ interface ActiveRemotePoller {
135135
readonly demandCwds: Ref.Ref<ReadonlyMap<string, number>>;
136136
}
137137

138+
interface InFlightRefresh {
139+
readonly fiber: Fiber.Fiber<VcsStatusResult, GitManagerServiceError>;
140+
readonly startedGeneration: number;
141+
readonly pendingRerun: boolean;
142+
}
143+
138144
interface StreamStatusOptions {
139145
readonly automaticRemoteRefreshInterval?: Effect.Effect<Duration.Duration, never>;
140146
}
@@ -162,6 +168,7 @@ export class VcsStatusBroadcaster extends Context.Service<
162168
readonly refreshLocalStatus: (
163169
cwd: string,
164170
) => Effect.Effect<VcsStatusLocalResult, GitManagerServiceError>;
171+
readonly invalidateStatus: (cwd: string) => Effect.Effect<void>;
165172
readonly refreshStatus: (cwd: string) => Effect.Effect<VcsStatusResult, GitManagerServiceError>;
166173
readonly streamStatus: (
167174
input: VcsStatusInput,
@@ -193,9 +200,8 @@ export const make = Effect.gen(function* () {
193200
);
194201
const cacheRef = yield* Ref.make(new Map<string, CachedVcsStatus>());
195202
const pollersRef = yield* SynchronizedRef.make(new Map<string, ActiveRemotePoller>());
196-
const inFlightRefreshesRef = yield* SynchronizedRef.make(
197-
new Map<string, Fiber.Fiber<VcsStatusResult, GitManagerServiceError>>(),
198-
);
203+
const inFlightRefreshesRef = yield* SynchronizedRef.make(new Map<string, InFlightRefresh>());
204+
const invalidateGenerationsRef = yield* Ref.make(new Map<string, number>());
199205

200206
const getCachedStatus = Effect.fn("VcsStatusBroadcaster.getCachedStatus")(function* (
201207
cwd: string,
@@ -381,35 +387,120 @@ export const make = Effect.gen(function* () {
381387
return yield* updateCachedStatus(cwd, local, remote, { publish: true });
382388
});
383389

384-
const refreshStatus: VcsStatusBroadcaster["Service"]["refreshStatus"] = Effect.fn(
385-
"VcsStatusBroadcaster.refreshStatus",
390+
const invalidateGenerationOf = (cwd: string) =>
391+
Ref.get(invalidateGenerationsRef).pipe(Effect.map((generations) => generations.get(cwd) ?? 0));
392+
393+
function startRefreshFiber(
394+
cwd: string,
395+
startedGeneration: number,
396+
): Effect.Effect<Fiber.Fiber<VcsStatusResult, GitManagerServiceError>> {
397+
return refreshStatusCore(cwd).pipe(
398+
Effect.ensuring(settleRefreshFiber(cwd, startedGeneration)),
399+
Effect.forkIn(broadcasterScope),
400+
);
401+
}
402+
403+
function settleRefreshFiber(cwd: string, startedGeneration: number) {
404+
return Effect.gen(function* () {
405+
const shouldRerun = yield* SynchronizedRef.modify(inFlightRefreshesRef, (inFlight) => {
406+
const existing = inFlight.get(cwd);
407+
if (existing === undefined || existing.startedGeneration !== startedGeneration) {
408+
return [false, inFlight] as const;
409+
}
410+
if (!existing.pendingRerun) {
411+
const next = new Map(inFlight);
412+
next.delete(cwd);
413+
return [false, next] as const;
414+
}
415+
const next = new Map(inFlight);
416+
next.set(cwd, { ...existing, pendingRerun: false });
417+
return [true, next] as const;
418+
});
419+
if (!shouldRerun) {
420+
return;
421+
}
422+
423+
const nextGeneration = yield* invalidateGenerationOf(cwd);
424+
const nextFiber = yield* startRefreshFiber(cwd, nextGeneration);
425+
yield* SynchronizedRef.update(inFlightRefreshesRef, (inFlight) => {
426+
const existing = inFlight.get(cwd);
427+
if (existing === undefined || existing.startedGeneration !== startedGeneration) {
428+
return inFlight;
429+
}
430+
const next = new Map(inFlight);
431+
next.set(cwd, {
432+
fiber: nextFiber,
433+
startedGeneration: nextGeneration,
434+
pendingRerun: existing.pendingRerun,
435+
});
436+
return next;
437+
});
438+
});
439+
}
440+
441+
const invalidateStatus: VcsStatusBroadcaster["Service"]["invalidateStatus"] = Effect.fn(
442+
"VcsStatusBroadcaster.invalidateStatus",
386443
)(function* (rawCwd) {
387444
const cwd = yield* withFileSystem(normalizeCwd(rawCwd));
388-
// Overlapping callers share one invalidate+recompute. The work is forked
389-
// into the broadcaster scope so interrupting a waiter cannot cancel it.
390-
const fiber = yield* SynchronizedRef.modifyEffect(inFlightRefreshesRef, (inFlight) => {
445+
yield* workflow.invalidateStatus(cwd);
446+
yield* Ref.update(invalidateGenerationsRef, (generations) => {
447+
const next = new Map(generations);
448+
next.set(cwd, (generations.get(cwd) ?? 0) + 1);
449+
return next;
450+
});
451+
// A mutation after an in-flight refresh started must not reuse that run.
452+
yield* SynchronizedRef.update(inFlightRefreshesRef, (inFlight) => {
391453
const existing = inFlight.get(cwd);
392-
if (existing) {
393-
return Effect.succeed([existing, inFlight] as const);
454+
if (existing === undefined || existing.pendingRerun) {
455+
return inFlight;
394456
}
457+
const next = new Map(inFlight);
458+
next.set(cwd, { ...existing, pendingRerun: true });
459+
return next;
460+
});
461+
});
395462

396-
return refreshStatusCore(cwd).pipe(
397-
Effect.ensuring(
398-
SynchronizedRef.update(inFlightRefreshesRef, (current) => {
463+
const refreshStatus: VcsStatusBroadcaster["Service"]["refreshStatus"] = Effect.fn(
464+
"VcsStatusBroadcaster.refreshStatus",
465+
)(function* (rawCwd) {
466+
const cwd = yield* withFileSystem(normalizeCwd(rawCwd));
467+
// Same-generation callers share one invalidate+recompute. The work is
468+
// forked into the broadcaster scope so interrupting a waiter cannot
469+
// cancel it. A later invalidate marks that run stale and this waiter
470+
// joins one follow-up refresh after it completes.
471+
while (true) {
472+
const requestGeneration = yield* invalidateGenerationOf(cwd);
473+
const inFlight = yield* SynchronizedRef.modifyEffect(inFlightRefreshesRef, (current) => {
474+
const existing = current.get(cwd);
475+
if (existing) {
476+
if (existing.startedGeneration >= requestGeneration) {
477+
return Effect.succeed([existing, current] as const);
478+
}
479+
const joined = { ...existing, pendingRerun: true };
480+
const next = new Map(current);
481+
next.set(cwd, joined);
482+
return Effect.succeed([joined, next] as const);
483+
}
484+
485+
return startRefreshFiber(cwd, requestGeneration).pipe(
486+
Effect.map((started) => {
487+
const created: InFlightRefresh = {
488+
fiber: started,
489+
startedGeneration: requestGeneration,
490+
pendingRerun: false,
491+
};
399492
const next = new Map(current);
400-
next.delete(cwd);
401-
return next;
493+
next.set(cwd, created);
494+
return [created, next] as const;
402495
}),
403-
),
404-
Effect.forkIn(broadcasterScope),
405-
Effect.map((started) => {
406-
const next = new Map(inFlight);
407-
next.set(cwd, started);
408-
return [started, next] as const;
409-
}),
410-
);
411-
});
412-
return yield* Fiber.join(fiber);
496+
);
497+
});
498+
const result = yield* Fiber.join(inFlight.fiber);
499+
const latestGeneration = yield* invalidateGenerationOf(cwd);
500+
if (inFlight.startedGeneration >= latestGeneration) {
501+
return result;
502+
}
503+
}
413504
});
414505

415506
const makeRemoteRefreshLoop = (
@@ -621,6 +712,7 @@ export const make = Effect.gen(function* () {
621712
return VcsStatusBroadcaster.of({
622713
getStatus,
623714
refreshLocalStatus,
715+
invalidateStatus,
624716
refreshStatus,
625717
streamStatus,
626718
});

‎apps/server/src/ws.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,8 +1036,13 @@ const makeWsRpcLayer = (
10361036

10371037
const refreshGitStatus = (cwd: string) =>
10381038
vcsStatusBroadcaster
1039-
.refreshStatus(cwd)
1040-
.pipe(Effect.ignoreCause({ log: true }), Effect.forkDetach, Effect.asVoid);
1039+
.invalidateStatus(cwd)
1040+
.pipe(
1041+
Effect.andThen(vcsStatusBroadcaster.refreshStatus(cwd)),
1042+
Effect.ignoreCause({ log: true }),
1043+
Effect.forkDetach,
1044+
Effect.asVoid,
1045+
);
10411046

10421047
return WsRpcGroup.of({
10431048
[ORCHESTRATION_WS_METHODS.dispatchCommand]: (command) =>

0 commit comments

Comments
 (0)