Skip to content

Commit 8dbe185

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 8dbe185

2 files changed

Lines changed: 359 additions & 31 deletions

File tree

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

Lines changed: 300 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,302 @@ 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 warnings: Array<ReadonlyArray<unknown>> = [];
330+
const logger = Logger.make(({ message }) => {
331+
if (Array.isArray(message)) warnings.push(message);
332+
if (Array.isArray(message) && message[0] === "VCS remote status refresh failed") {
333+
Deferred.doneUnsafe(outcome, Effect.succeed("failed"));
334+
}
335+
});
336+
const layerTest = VcsStatusBroadcaster.layer.pipe(
337+
Layer.provideMerge(NodeServices.layer),
338+
Layer.provide(layerBackgroundPolicy(() => true)),
339+
Layer.provide(
340+
Layer.succeed(VcsStatusBroadcaster.VcsAutoPullPolicy, {
341+
isEnabled: () => Effect.succeed(true),
342+
}),
343+
),
344+
Layer.provide(
345+
Layer.mock(GitWorkflowService.GitWorkflowService)({
346+
localStatus: () =>
347+
Effect.suspend(() =>
348+
guardScanFails
349+
? Effect.fail(
350+
new GitManagerError({
351+
operation: "test",
352+
cwd: "/repo",
353+
detail: "guard scan failed",
354+
}),
355+
)
356+
: Effect.succeed({ ...baseLocalStatus, isDefaultRef: true, refName: "main" }),
357+
),
358+
remoteStatus: () => Effect.sync(() => remote),
359+
invalidateLocalStatus: () => Effect.void,
360+
invalidateRemoteStatus: () => Effect.void,
361+
pullCurrentBranch: () =>
362+
Effect.sync(() => {
363+
pullCalls += 1;
364+
return { status: "pulled" as const, refName: "main", upstreamRef: "origin/main" };
365+
}),
366+
}),
367+
),
368+
);
369+
return Effect.gen(function* () {
370+
const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster;
371+
yield* broadcaster.getStatus({ cwd: "/repo" });
372+
const subscribed = yield* Deferred.make<void>();
373+
yield* Stream.runForEach(broadcaster.streamStatus({ cwd: "/repo" }), (event) =>
374+
event._tag === "snapshot"
375+
? Deferred.succeed(subscribed, undefined).pipe(Effect.asVoid)
376+
: event._tag === "remoteUpdated"
377+
? Deferred.succeed(outcome, "updated").pipe(Effect.asVoid)
378+
: Effect.void,
379+
).pipe(Effect.forkScoped);
380+
yield* Deferred.await(subscribed);
381+
guardScanFails = true;
382+
remote = { ...remoteStatusWithPr, behindCount: 2 };
383+
yield* TestClock.adjust(Duration.minutes(1));
384+
assert.equal(yield* Deferred.await(outcome), "updated");
385+
assert.equal(pullCalls, 0);
386+
assert.deepStrictEqual((yield* broadcaster.getStatus({ cwd: "/repo" })).pr, remote.pr);
387+
assert.deepStrictEqual(
388+
warnings.find((message) => message[0] === "Automatic project pull guard scan failed"),
389+
[
390+
"Automatic project pull guard scan failed",
391+
{ cwdLength: "/repo".length, errorTag: "GitManagerError" },
392+
],
393+
);
394+
}).pipe(
395+
Effect.scoped,
396+
Effect.provide(
397+
Layer.mergeAll(
398+
layerTest,
399+
TestClock.layer(),
400+
Logger.layer([logger], { mergeWithExisting: true }),
401+
),
402+
),
403+
);
404+
},
405+
);
406+
407+
it.effect.each(["pull", "post-pull remote read", "recovery scan"] as const)(
408+
"background refresh handles failure at %s",
409+
(failure) => {
410+
let local = { ...baseLocalStatus, isDefaultRef: true, refName: "main" };
411+
const afterPull = { ...local, hasWorkingTreeChanges: true };
412+
let pullCalls = 0;
413+
const outcome = Deferred.makeUnsafe<"updated" | "failed">();
414+
const warnings: Array<ReadonlyArray<unknown>> = [];
415+
const logger = Logger.make(({ message }) => {
416+
if (Array.isArray(message)) warnings.push(message);
417+
if (Array.isArray(message) && message[0] === "VCS remote status refresh failed") {
418+
Deferred.doneUnsafe(outcome, Effect.succeed("failed"));
419+
}
420+
});
421+
const layerTest = VcsStatusBroadcaster.layer.pipe(
422+
Layer.provideMerge(NodeServices.layer),
423+
Layer.provide(layerBackgroundPolicy(() => true)),
424+
Layer.provide(
425+
Layer.succeed(VcsStatusBroadcaster.VcsAutoPullPolicy, {
426+
isEnabled: () => Effect.succeed(true),
427+
}),
428+
),
429+
Layer.provide(
430+
Layer.mock(GitWorkflowService.GitWorkflowService)({
431+
localStatus: () =>
432+
Effect.suspend(() =>
433+
failure === "recovery scan" && pullCalls > 0
434+
? Effect.fail(
435+
new GitManagerError({
436+
operation: "test",
437+
cwd: "/repo",
438+
detail: "recovery scan failed",
439+
}),
440+
)
441+
: Effect.succeed(local),
442+
),
443+
remoteStatus: () =>
444+
Effect.suspend(() =>
445+
failure === "post-pull remote read" && pullCalls > 0
446+
? Effect.fail(
447+
new GitManagerError({
448+
operation: "test",
449+
cwd: "/repo",
450+
detail: "post-pull read failed",
451+
}),
452+
)
453+
: Effect.succeed({ ...baseRemoteStatus, behindCount: 2 }),
454+
),
455+
invalidateLocalStatus: () => Effect.void,
456+
invalidateRemoteStatus: () => Effect.void,
457+
invalidateStatus: () => Effect.void,
458+
pullCurrentBranch: () =>
459+
Effect.suspend(() => {
460+
pullCalls += 1;
461+
local = afterPull;
462+
return failure !== "post-pull remote read"
463+
? Effect.fail(
464+
new GitCommandError({
465+
operation: "test",
466+
command: "pull",
467+
cwd: "/repo",
468+
detail: "pull failed",
469+
}),
470+
)
471+
: Effect.succeed({
472+
status: "pulled" as const,
473+
refName: "main",
474+
upstreamRef: "origin/main",
475+
});
476+
}),
477+
}),
478+
),
479+
);
480+
return Effect.gen(function* () {
481+
const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster;
482+
yield* Stream.runForEach(broadcaster.streamStatus({ cwd: "/repo" }), (event) =>
483+
event._tag === "remoteUpdated" || (event._tag === "snapshot" && event.remote !== null)
484+
? Deferred.succeed(outcome, "updated").pipe(Effect.asVoid)
485+
: Effect.void,
486+
).pipe(Effect.forkScoped);
487+
assert.equal(
488+
yield* Deferred.await(outcome),
489+
failure === "recovery scan" ? "failed" : "updated",
490+
);
491+
assert.equal(pullCalls, 1);
492+
assert.deepStrictEqual(
493+
warnings.find((message) => message[0] === "Automatic project pull failed"),
494+
[
495+
"Automatic project pull failed",
496+
{
497+
cwdLength: "/repo".length,
498+
errorTag: failure === "post-pull remote read" ? "GitManagerError" : "GitCommandError",
499+
},
500+
],
501+
);
502+
if (failure !== "recovery scan") {
503+
assert.isTrue((yield* broadcaster.getStatus({ cwd: "/repo" })).hasWorkingTreeChanges);
504+
}
505+
}).pipe(
506+
Effect.scoped,
507+
Effect.provide(Layer.merge(layerTest, Logger.layer([logger], { mergeWithExisting: true }))),
508+
);
509+
},
510+
);
511+
216512
it.effect("reuses the cached VCS status across repeated reads", () => {
217513
const state = {
218514
currentLocalStatus: baseLocalStatus,
@@ -341,6 +637,7 @@ describe("VcsStatusBroadcaster", () => {
341637
}
342638
return remoteStatusWithPr;
343639
}),
640+
invalidateLocalStatus: () => Effect.void,
344641
invalidateStatus: () => Effect.void,
345642
}),
346643
),
@@ -417,7 +714,8 @@ describe("VcsStatusBroadcaster", () => {
417714
});
418715
assert.equal(state.localStatusCalls, 2);
419716
assert.equal(state.remoteStatusCalls, 2);
420-
assert.equal(state.localInvalidationCalls, 1);
717+
// Once up front, and once more after the fetch.
718+
assert.equal(state.localInvalidationCalls, 2);
421719
assert.equal(state.remoteInvalidationCalls, 1);
422720
}).pipe(Effect.provide(layerTestFor(state)));
423721
});

0 commit comments

Comments
 (0)