Skip to content

Commit d5d50ac

Browse files
Alb11747codexclaude
committed
fix(server): Git status refresh no longer scans the working tree twice before auto-pull
Reuse the local status already read by explicit and background refreshes instead of scanning the working tree again before auto-pull. Invalidate local status after the fetch so concurrent refreshes cannot leave a stale snapshot in the cache. Resolve auto-pull policy before taking the shared local snapshot, so edits made while the policy lookup is pending still prevent an automatic pull. Cover both refresh paths with deterministic deferred-policy regressions. Reconcile local state after a failed pull or post-pull status read rather than retaining the pre-pull snapshot in background refreshes. Let a failed recovery scan reach refresh failure handling and backoff. Co-Authored-By: GPT-6.1-Sol at high effort <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]> Co-Authored-By: GPT-6.1-Sol at xhigh effort, review subagents <[email protected]>
1 parent 7ac952c commit d5d50ac

2 files changed

Lines changed: 333 additions & 30 deletions

File tree

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

Lines changed: 279 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import type {
2323
VcsStatusResult,
2424
VcsStatusStreamEvent,
2525
} from "@t3tools/contracts";
26-
import { GitManagerError } from "@t3tools/contracts";
26+
import { GitCommandError, GitManagerError } from "@t3tools/contracts";
2727

2828
import * as VcsStatusBroadcaster from "./VcsStatusBroadcaster.ts";
2929
import * as BackgroundPolicy from "../background/BackgroundPolicy.ts";
@@ -213,6 +213,281 @@ describe("VcsStatusBroadcaster", () => {
213213
},
214214
);
215215

216+
it.effect("an explicit refresh scans local status once when auto-pull skips a dirty tree", () => {
217+
const state = {
218+
currentLocalStatus: {
219+
...baseLocalStatus,
220+
isDefaultRef: true,
221+
refName: "main",
222+
hasWorkingTreeChanges: true,
223+
},
224+
currentRemoteStatus: { ...baseRemoteStatus, behindCount: 2 },
225+
localStatusCalls: 0,
226+
remoteStatusCalls: 0,
227+
localInvalidationCalls: 0,
228+
remoteInvalidationCalls: 0,
229+
};
230+
231+
return Effect.gen(function* () {
232+
const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster;
233+
const status = yield* broadcaster.refreshStatus("/repo");
234+
235+
assert.equal(status.behindCount, 2);
236+
assert.equal(state.localStatusCalls, 1);
237+
}).pipe(
238+
Effect.provide(
239+
layerTestFor(state).pipe(
240+
Layer.provide(
241+
Layer.succeed(VcsStatusBroadcaster.VcsAutoPullPolicy, {
242+
isEnabled: () => Effect.succeed(true),
243+
}),
244+
),
245+
),
246+
),
247+
);
248+
});
249+
250+
it.effect.each(["explicit", "background"] as const)(
251+
"%s refresh skips auto-pull for edits made while policy resolves",
252+
(refresh) => {
253+
const policyStarted = Deferred.makeUnsafe<void>();
254+
const releasePolicy = Deferred.makeUnsafe<void>();
255+
const remotePublished = Deferred.makeUnsafe<void>();
256+
let local: VcsStatusLocalResult = {
257+
...baseLocalStatus,
258+
isDefaultRef: true,
259+
refName: "main",
260+
};
261+
let pullCalls = 0;
262+
let localReads = 0;
263+
const layerTest = VcsStatusBroadcaster.layer.pipe(
264+
Layer.provideMerge(NodeServices.layer),
265+
Layer.provide(layerBackgroundPolicy(() => true)),
266+
Layer.provide(
267+
Layer.succeed(VcsStatusBroadcaster.VcsAutoPullPolicy, {
268+
isEnabled: () =>
269+
Deferred.succeed(policyStarted, undefined).pipe(
270+
Effect.andThen(Deferred.await(releasePolicy)),
271+
Effect.as(true),
272+
),
273+
}),
274+
),
275+
Layer.provide(
276+
Layer.mock(GitWorkflowService.GitWorkflowService)({
277+
localStatus: () =>
278+
Effect.sync(() => {
279+
localReads += 1;
280+
return local;
281+
}),
282+
remoteStatus: () => Effect.succeed({ ...baseRemoteStatus, behindCount: 2 }),
283+
invalidateLocalStatus: () => Effect.void,
284+
invalidateRemoteStatus: () => Effect.void,
285+
invalidateStatus: () => Effect.void,
286+
pullCurrentBranch: () =>
287+
Effect.sync(() => {
288+
pullCalls += 1;
289+
return { status: "pulled" as const, refName: "main", upstreamRef: "origin/main" };
290+
}),
291+
}),
292+
),
293+
);
294+
295+
return Effect.gen(function* () {
296+
const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster;
297+
const work =
298+
refresh === "explicit"
299+
? broadcaster.refreshStatus("/repo")
300+
: Stream.runForEach(broadcaster.streamStatus({ cwd: "/repo" }), (event) =>
301+
event._tag === "remoteUpdated" ||
302+
(event._tag === "snapshot" && event.remote !== null)
303+
? Deferred.succeed(remotePublished, undefined).pipe(Effect.asVoid)
304+
: Effect.void,
305+
);
306+
const fiber = yield* work.pipe(Effect.forkScoped);
307+
yield* Deferred.await(policyStarted);
308+
local = { ...local, hasWorkingTreeChanges: true };
309+
yield* Deferred.succeed(releasePolicy, undefined);
310+
if (refresh === "explicit") {
311+
yield* Fiber.join(fiber);
312+
} else {
313+
yield* Deferred.await(remotePublished);
314+
}
315+
assert.equal(pullCalls, 0);
316+
// The background stream also reads its initial local snapshot.
317+
assert.equal(localReads, refresh === "explicit" ? 1 : 2);
318+
}).pipe(Effect.scoped, Effect.provide(layerTest));
319+
},
320+
);
321+
322+
it.effect(
323+
"background refresh publishes remote changes when the optional pull guard scan fails",
324+
() => {
325+
let guardScanFails = false;
326+
let remote = { ...baseRemoteStatus, behindCount: 2 };
327+
let pullCalls = 0;
328+
const outcome = Deferred.makeUnsafe<"updated" | "failed">();
329+
const logger = Logger.make(({ message }) => {
330+
if (Array.isArray(message) && message[0] === "VCS remote status refresh failed") {
331+
Deferred.doneUnsafe(outcome, Effect.succeed("failed"));
332+
}
333+
});
334+
const layerTest = VcsStatusBroadcaster.layer.pipe(
335+
Layer.provideMerge(NodeServices.layer),
336+
Layer.provide(layerBackgroundPolicy(() => true)),
337+
Layer.provide(
338+
Layer.succeed(VcsStatusBroadcaster.VcsAutoPullPolicy, {
339+
isEnabled: () => Effect.succeed(true),
340+
}),
341+
),
342+
Layer.provide(
343+
Layer.mock(GitWorkflowService.GitWorkflowService)({
344+
localStatus: () =>
345+
Effect.suspend(() =>
346+
guardScanFails
347+
? Effect.fail(
348+
new GitManagerError({
349+
operation: "test",
350+
cwd: "/repo",
351+
detail: "guard scan failed",
352+
}),
353+
)
354+
: Effect.succeed({ ...baseLocalStatus, isDefaultRef: true, refName: "main" }),
355+
),
356+
remoteStatus: () => Effect.sync(() => remote),
357+
invalidateLocalStatus: () => Effect.void,
358+
invalidateRemoteStatus: () => Effect.void,
359+
pullCurrentBranch: () =>
360+
Effect.sync(() => {
361+
pullCalls += 1;
362+
return { status: "pulled" as const, refName: "main", upstreamRef: "origin/main" };
363+
}),
364+
}),
365+
),
366+
);
367+
return Effect.gen(function* () {
368+
const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster;
369+
yield* broadcaster.getStatus({ cwd: "/repo" });
370+
const subscribed = yield* Deferred.make<void>();
371+
yield* Stream.runForEach(broadcaster.streamStatus({ cwd: "/repo" }), (event) =>
372+
event._tag === "snapshot"
373+
? Deferred.succeed(subscribed, undefined).pipe(Effect.asVoid)
374+
: event._tag === "remoteUpdated"
375+
? Deferred.succeed(outcome, "updated").pipe(Effect.asVoid)
376+
: Effect.void,
377+
).pipe(Effect.forkScoped);
378+
yield* Deferred.await(subscribed);
379+
guardScanFails = true;
380+
remote = { ...remoteStatusWithPr, behindCount: 2 };
381+
yield* TestClock.adjust(Duration.minutes(1));
382+
assert.equal(yield* Deferred.await(outcome), "updated");
383+
assert.equal(pullCalls, 0);
384+
assert.deepStrictEqual((yield* broadcaster.getStatus({ cwd: "/repo" })).pr, remote.pr);
385+
}).pipe(
386+
Effect.scoped,
387+
Effect.provide(
388+
Layer.mergeAll(
389+
layerTest,
390+
TestClock.layer(),
391+
Logger.layer([logger], { mergeWithExisting: true }),
392+
),
393+
),
394+
);
395+
},
396+
);
397+
398+
it.effect.each(["pull", "post-pull remote read", "recovery scan"] as const)(
399+
"background refresh handles failure at %s",
400+
(failure) => {
401+
let local = { ...baseLocalStatus, isDefaultRef: true, refName: "main" };
402+
const afterPull = { ...local, hasWorkingTreeChanges: true };
403+
let pullCalls = 0;
404+
const outcome = Deferred.makeUnsafe<"updated" | "failed">();
405+
const logger = Logger.make(({ message }) => {
406+
if (Array.isArray(message) && message[0] === "VCS remote status refresh failed") {
407+
Deferred.doneUnsafe(outcome, Effect.succeed("failed"));
408+
}
409+
});
410+
const layerTest = VcsStatusBroadcaster.layer.pipe(
411+
Layer.provideMerge(NodeServices.layer),
412+
Layer.provide(layerBackgroundPolicy(() => true)),
413+
Layer.provide(
414+
Layer.succeed(VcsStatusBroadcaster.VcsAutoPullPolicy, {
415+
isEnabled: () => Effect.succeed(true),
416+
}),
417+
),
418+
Layer.provide(
419+
Layer.mock(GitWorkflowService.GitWorkflowService)({
420+
localStatus: () =>
421+
Effect.suspend(() =>
422+
failure === "recovery scan" && pullCalls > 0
423+
? Effect.fail(
424+
new GitManagerError({
425+
operation: "test",
426+
cwd: "/repo",
427+
detail: "recovery scan failed",
428+
}),
429+
)
430+
: Effect.succeed(local),
431+
),
432+
remoteStatus: () =>
433+
Effect.suspend(() =>
434+
failure === "post-pull remote read" && pullCalls > 0
435+
? Effect.fail(
436+
new GitManagerError({
437+
operation: "test",
438+
cwd: "/repo",
439+
detail: "post-pull read failed",
440+
}),
441+
)
442+
: Effect.succeed({ ...baseRemoteStatus, behindCount: 2 }),
443+
),
444+
invalidateLocalStatus: () => Effect.void,
445+
invalidateRemoteStatus: () => Effect.void,
446+
invalidateStatus: () => Effect.void,
447+
pullCurrentBranch: () =>
448+
Effect.suspend(() => {
449+
pullCalls += 1;
450+
local = afterPull;
451+
return failure !== "post-pull remote read"
452+
? Effect.fail(
453+
new GitCommandError({
454+
operation: "test",
455+
command: "pull",
456+
cwd: "/repo",
457+
detail: "pull failed",
458+
}),
459+
)
460+
: Effect.succeed({
461+
status: "pulled" as const,
462+
refName: "main",
463+
upstreamRef: "origin/main",
464+
});
465+
}),
466+
}),
467+
),
468+
);
469+
return Effect.gen(function* () {
470+
const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster;
471+
yield* Stream.runForEach(broadcaster.streamStatus({ cwd: "/repo" }), (event) =>
472+
event._tag === "remoteUpdated" || (event._tag === "snapshot" && event.remote !== null)
473+
? Deferred.succeed(outcome, "updated").pipe(Effect.asVoid)
474+
: Effect.void,
475+
).pipe(Effect.forkScoped);
476+
assert.equal(
477+
yield* Deferred.await(outcome),
478+
failure === "recovery scan" ? "failed" : "updated",
479+
);
480+
assert.equal(pullCalls, 1);
481+
if (failure !== "recovery scan") {
482+
assert.isTrue((yield* broadcaster.getStatus({ cwd: "/repo" })).hasWorkingTreeChanges);
483+
}
484+
}).pipe(
485+
Effect.scoped,
486+
Effect.provide(Layer.merge(layerTest, Logger.layer([logger], { mergeWithExisting: true }))),
487+
);
488+
},
489+
);
490+
216491
it.effect("reuses the cached VCS status across repeated reads", () => {
217492
const state = {
218493
currentLocalStatus: baseLocalStatus,
@@ -341,6 +616,7 @@ describe("VcsStatusBroadcaster", () => {
341616
}
342617
return remoteStatusWithPr;
343618
}),
619+
invalidateLocalStatus: () => Effect.void,
344620
invalidateStatus: () => Effect.void,
345621
}),
346622
),
@@ -417,7 +693,8 @@ describe("VcsStatusBroadcaster", () => {
417693
});
418694
assert.equal(state.localStatusCalls, 2);
419695
assert.equal(state.remoteStatusCalls, 2);
420-
assert.equal(state.localInvalidationCalls, 1);
696+
// Once up front, and once more after the fetch.
697+
assert.equal(state.localInvalidationCalls, 2);
421698
assert.equal(state.remoteInvalidationCalls, 1);
422699
}).pipe(Effect.provide(layerTestFor(state)));
423700
});

0 commit comments

Comments
 (0)