@devries-cameron20 / t3code-1 / commits / 189af1aaa4
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(server): refresh local status after an automatic pull fails (#19686)

Co-authored-by: GPT-6.1-Sol at high effort <noreply@openai.com>

Alb11747 committed Oct 18, 2026 at 15:27 UTC 189af1aaa431a87ab34a419e1db9dd5ee87fe1a4
2 files changed +145 -3
apps/server/src/vcs/VcsStatusBroadcaster.test.ts
+129 -1
@@ -23,7 +23,7 @@ import type {
23 VcsStatusResult,
24 VcsStatusStreamEvent,
25 } from "@t3tools/contracts";
26 -import { GitManagerError } from "@t3tools/contracts";
26 +import { GitCommandError, GitManagerError } from "@t3tools/contracts";
27
28 import * as VcsStatusBroadcaster from "./VcsStatusBroadcaster.ts";
29 import * as BackgroundPolicy from "../background/BackgroundPolicy.ts";
@@ -212,6 +212,134 @@ describe("VcsStatusBroadcaster", () => {
212 },
213 );
214
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 +
343 it.effect("reuses the cached VCS status across repeated reads", () => {
344 const state = {
345 currentLocalStatus: baseLocalStatus,
apps/server/src/vcs/VcsStatusBroadcaster.ts
+16 -2
@@ -406,6 +406,7 @@ export const make = Effect.gen(function* () {
406 remote: VcsStatusRemoteResult | null,
407 policyCwds: ReadonlyArray<string>,
408 ) {
409 + let pullAttempted = false;
410 return yield* Effect.gen(function* () {
411 const autoPullEnabled = (yield* Effect.forEach(policyCwds, autoPullPolicy.isEnabled, {
412 concurrency: "unbounded",
@@ -424,6 +425,7 @@ export const make = Effect.gen(function* () {
425 const local = yield* workflow.localStatus({ cwd });
426 if (!local.isRepo || !local.isDefaultRef || local.hasWorkingTreeChanges) return null;
427
428 + pullAttempted = true;
429 yield* workflow.pullCurrentBranch(cwd);
430 yield* workflow.invalidateStatus(cwd);
431 const [refreshedLocal, refreshedRemote] = yield* Effect.all(
@@ -433,8 +435,20 @@ export const make = Effect.gen(function* () {
435 yield* updateCachedStatus(cwd, refreshedLocal, refreshedRemote, { publish: true });
436 return { local: refreshedLocal, remote: refreshedRemote };
437 }).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 + }),
452 ),
453 );
454 });