Skip to content

Commit 4b04e7f

Browse files
Alb11747codex
andcommitted
fix(server): refresh local status after an automatic pull fails
Reconcile local status after a failed pull attempt or follow-up read, and let reconciliation failures reach the refresh caller or poller backoff. Keep pre-pull guards and normal refresh scan order unchanged. Refs: #19684 Co-Authored-By: GPT-6.1-Sol at high effort <[email protected]> Co-Authored-By: GPT-6.1-Sol at xhigh effort, review subagent <[email protected]>
1 parent 742c229 commit 4b04e7f

2 files changed

Lines changed: 145 additions & 3 deletions

File tree

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

Lines changed: 129 additions & 1 deletion
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,134 @@ describe("VcsStatusBroadcaster", () => {
213213
},
214214
);
215215

216+
it.effect.each([
217+
{ refresh: "explicit", failure: "pull" },
218+
{ refresh: "explicit", failure: "post-pull remote read" },
219+
{ refresh: "explicit", failure: "recovery scan" },
220+
{ refresh: "background", failure: "pull" },
221+
{ refresh: "background", failure: "post-pull remote read" },
222+
{ refresh: "background", failure: "recovery scan" },
223+
] as const)(
224+
"$refresh refresh reconciles local status after failure at $failure",
225+
({ refresh, failure }) => {
226+
let local = { ...baseLocalStatus, isDefaultRef: true, refName: "main" };
227+
const afterPull = { ...local, hasWorkingTreeChanges: true };
228+
let pullCalls = 0;
229+
let seeded = false;
230+
const subscribed = Deferred.makeUnsafe<void>();
231+
const outcome = Deferred.makeUnsafe<"updated" | "failed">();
232+
const recoveryError = new GitManagerError({
233+
operation: "test",
234+
cwd: "/repo",
235+
detail: "recovery scan failed",
236+
});
237+
const logger = Logger.make(({ message }) => {
238+
if (Array.isArray(message) && message[0] === "VCS remote status refresh failed") {
239+
Deferred.doneUnsafe(outcome, Effect.succeed("failed"));
240+
}
241+
});
242+
const layerTest = VcsStatusBroadcaster.layer.pipe(
243+
Layer.provideMerge(NodeServices.layer),
244+
Layer.provide(layerBackgroundPolicy(() => true)),
245+
Layer.provide(
246+
Layer.succeed(VcsStatusBroadcaster.VcsAutoPullPolicy, {
247+
isEnabled: () => Effect.succeed(true),
248+
}),
249+
),
250+
Layer.provide(
251+
Layer.mock(GitWorkflowService.GitWorkflowService)({
252+
localStatus: () =>
253+
Effect.suspend(() =>
254+
failure === "recovery scan" && pullCalls > 0
255+
? Effect.fail(recoveryError)
256+
: Effect.succeed(local),
257+
),
258+
remoteStatus: () =>
259+
Effect.suspend(() =>
260+
failure === "post-pull remote read" && pullCalls > 0
261+
? Effect.fail(
262+
new GitManagerError({
263+
operation: "test",
264+
cwd: "/repo",
265+
detail: "post-pull read failed",
266+
}),
267+
)
268+
: Effect.succeed({
269+
...baseRemoteStatus,
270+
behindCount: 2,
271+
pr: seeded ? remoteStatusWithPr.pr : null,
272+
}),
273+
),
274+
invalidateLocalStatus: () => Effect.void,
275+
invalidateRemoteStatus: () => Effect.void,
276+
invalidateStatus: () => Effect.void,
277+
pullCurrentBranch: () =>
278+
Effect.suspend(() => {
279+
pullCalls += 1;
280+
local = afterPull;
281+
return failure !== "post-pull remote read"
282+
? Effect.fail(
283+
new GitCommandError({
284+
operation: "test",
285+
command: "pull",
286+
cwd: "/repo",
287+
detail: "pull failed",
288+
}),
289+
)
290+
: Effect.succeed({
291+
status: "pulled" as const,
292+
refName: "main",
293+
upstreamRef: "origin/main",
294+
});
295+
}),
296+
}),
297+
),
298+
);
299+
return Effect.gen(function* () {
300+
const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster;
301+
// Seed the same divergence so background recovery cannot rely on a moved remote ref.
302+
yield* broadcaster.getStatus({ cwd: "/repo" });
303+
seeded = true;
304+
if (refresh === "explicit") {
305+
const result = yield* Effect.result(broadcaster.refreshStatus("/repo"));
306+
if (failure === "recovery scan") {
307+
assert.isTrue(result._tag === "Failure");
308+
if (result._tag === "Failure") assert.strictEqual(result.failure, recoveryError);
309+
return;
310+
}
311+
assert.isTrue(result._tag === "Success");
312+
if (result._tag === "Success") assert.isTrue(result.success.hasWorkingTreeChanges);
313+
} else {
314+
yield* Stream.runForEach(broadcaster.streamStatus({ cwd: "/repo" }), (event) => {
315+
if (event._tag === "snapshot" && !event.local.hasWorkingTreeChanges) {
316+
return Deferred.succeed(subscribed, undefined).pipe(Effect.asVoid);
317+
}
318+
return Deferred.succeed(outcome, "updated").pipe(Effect.asVoid);
319+
}).pipe(Effect.forkScoped);
320+
yield* Deferred.await(subscribed);
321+
yield* TestClock.adjust(Duration.seconds(30));
322+
assert.equal(
323+
yield* Deferred.await(outcome),
324+
failure === "recovery scan" ? "failed" : "updated",
325+
);
326+
}
327+
assert.equal(pullCalls, 1);
328+
if (failure !== "recovery scan") {
329+
assert.isTrue((yield* broadcaster.getStatus({ cwd: "/repo" })).hasWorkingTreeChanges);
330+
}
331+
}).pipe(
332+
Effect.scoped,
333+
Effect.provide(
334+
Layer.mergeAll(
335+
layerTest,
336+
TestClock.layer(),
337+
Logger.layer([logger], { mergeWithExisting: true }),
338+
),
339+
),
340+
);
341+
},
342+
);
343+
216344
it.effect("reuses the cached VCS status across repeated reads", () => {
217345
const state = {
218346
currentLocalStatus: baseLocalStatus,

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,7 @@ export const make = Effect.gen(function* () {
411411
remote: VcsStatusRemoteResult | null,
412412
policyCwds: ReadonlyArray<string>,
413413
) {
414+
let pullAttempted = false;
414415
return yield* Effect.gen(function* () {
415416
const autoPullEnabled = (yield* Effect.forEach(policyCwds, autoPullPolicy.isEnabled, {
416417
concurrency: "unbounded",
@@ -429,6 +430,7 @@ export const make = Effect.gen(function* () {
429430
const local = yield* workflow.localStatus({ cwd });
430431
if (!local.isRepo || !local.isDefaultRef || local.hasWorkingTreeChanges) return null;
431432

433+
pullAttempted = true;
432434
yield* workflow.pullCurrentBranch(cwd);
433435
yield* workflow.invalidateStatus(cwd);
434436
const [refreshedLocal, refreshedRemote] = yield* Effect.all(
@@ -438,8 +440,20 @@ export const make = Effect.gen(function* () {
438440
yield* updateCachedStatus(cwd, refreshedLocal, refreshedRemote, { publish: true });
439441
return { local: refreshedLocal, remote: refreshedRemote };
440442
}).pipe(
441-
Effect.catch(() =>
442-
Effect.logWarning("Automatic project pull failed", { cwd }).pipe(Effect.as(null)),
443+
Effect.catch((error) =>
444+
Effect.gen(function* () {
445+
yield* Effect.logWarning("Automatic project pull failed", {
446+
cwdLength: cwd.length,
447+
errorTag: error._tag,
448+
});
449+
if (!pullAttempted) return null;
450+
// A pull or its follow-up reads can fail after local state has changed.
451+
// Let a failed reconciliation reach the caller instead of publishing old status.
452+
yield* workflow.invalidateLocalStatus(cwd);
453+
const local = yield* workflow.localStatus({ cwd });
454+
yield* updateCachedStatus(cwd, local, remote, { publish: true });
455+
return { local, remote };
456+
}),
443457
),
444458
);
445459
});

0 commit comments

Comments
 (0)