Skip to content

Commit 189af1a

Browse files
Alb11747codex
andauthored
fix(server): refresh local status after an automatic pull fails (pingdotgg#19686)
Co-authored-by: GPT-6.1-Sol at high effort <[email protected]>
1 parent 95c99a5 commit 189af1a

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";
@@ -212,6 +212,134 @@ describe("VcsStatusBroadcaster", () => {
212212
},
213213
);
214214

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

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,7 @@ export const make = Effect.gen(function* () {
406406
remote: VcsStatusRemoteResult | null,
407407
policyCwds: ReadonlyArray<string>,
408408
) {
409+
let pullAttempted = false;
409410
return yield* Effect.gen(function* () {
410411
const autoPullEnabled = (yield* Effect.forEach(policyCwds, autoPullPolicy.isEnabled, {
411412
concurrency: "unbounded",
@@ -424,6 +425,7 @@ export const make = Effect.gen(function* () {
424425
const local = yield* workflow.localStatus({ cwd });
425426
if (!local.isRepo || !local.isDefaultRef || local.hasWorkingTreeChanges) return null;
426427

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

0 commit comments

Comments
 (0)