@devries-cameron20 / t3code Mirror · read-only / commits / 274474dd19
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 17:15 UTC

fix(server): refresh sync status after agent merges (#35919)

Yash Singh committed Nov 12, 2026 at 21:53 UTC 274474dd19b459b339628b5c427b12c6664d2cb3
3 files changed +107 -2
apps/server/src/git/GitWorkflowService.ts
+18
@@ -53,6 +53,14 @@ export class GitWorkflowService extends Context.Service<
53 input: VcsStatusInput,
54 ) => Effect.Effect<VcsStatusLocalResult, GitManagerServiceError>;
55 readonly currentBranch: (cwd: string) => Effect.Effect<string | null, GitManagerServiceError>;
56 + readonly upstreamDivergence: (cwd: string) => Effect.Effect<
57 + {
58 + readonly refName: string;
59 + readonly aheadCount: number;
60 + readonly behindCount: number;
61 + } | null,
62 + GitManagerServiceError
63 + >;
64 readonly remoteStatus: (
65 input: VcsStatusInput,
66 options?: GitManager.GitRemoteStatusOptions,
@@ -363,6 +371,16 @@ export const make = Effect.gen(function* () {
371 detail: "Failed to read the checked-out branch.",
372 });
373 }),
374 + upstreamDivergence: Effect.fn("GitWorkflowService.upstreamDivergence")(function* (cwd) {
375 + const details = yield* git.statusDetailsRemote(cwd, { refreshUpstream: false });
376 + return details.hasUpstream && details.branch !== null
377 + ? {
378 + refName: details.branch,
379 + aheadCount: details.aheadCount,
380 + behindCount: details.behindCount,
381 + }
382 + : null;
383 + }),
384 remoteStatus: Effect.fn("GitWorkflowService.remoteStatus")(function (input, options) {
385 return detectGitRepositoryForStatus("GitWorkflowService.remoteStatus", input.cwd).pipe(
386 Effect.flatMap((isGitRepository) =>
apps/server/src/vcs/VcsStatusBroadcaster.test.ts
+53
@@ -71,6 +71,7 @@ const remoteStatusWithPr: VcsStatusRemoteResult = {
71 function layerTestFor(state: {
72 currentLocalStatus: VcsStatusLocalResult;
73 currentRemoteStatus: VcsStatusRemoteResult | null;
74 + currentDivergence?: Pick<VcsStatusRemoteResult, "aheadCount" | "behindCount">;
75 localStatusCalls: number;
76 remoteStatusCalls: number;
77 localInvalidationCalls: number;
@@ -133,6 +134,16 @@ function layerTestFor(state: {
134 : (state.beforeCurrentBranch ?? Effect.void).pipe(
135 Effect.andThen(Effect.sync(() => state.currentLocalStatus.refName)),
136 ),
137 + upstreamDivergence: () =>
138 + Effect.sync(() =>
139 + state.currentRemoteStatus?.hasUpstream && state.currentLocalStatus.refName !== null
140 + ? {
141 + refName: state.currentLocalStatus.refName,
142 + aheadCount: (state.currentDivergence ?? state.currentRemoteStatus).aheadCount,
143 + behindCount: (state.currentDivergence ?? state.currentRemoteStatus).behindCount,
144 + }
145 + : null,
146 + ),
147 remoteStatus: (_input, options) =>
148 Effect.suspend(() => {
149 return (state.beforeRemoteStatus ?? Effect.void).pipe(
@@ -528,6 +539,48 @@ describe("VcsStatusBroadcaster", () => {
539 },
540 );
541
542 + it.effect.each([0, 2])(
543 + "refreshes merge divergence while a lookup ends with %s commits behind",
544 + (behindAfterLookup) => {
545 + const state: Parameters<typeof layerTestFor>[0] = {
546 + currentLocalStatus: baseLocalStatus,
547 + currentRemoteStatus: { ...baseRemoteStatus, aheadCount: 2, behindCount: 3 },
548 + localStatusCalls: 0,
549 + remoteStatusCalls: 0,
550 + localInvalidationCalls: 0,
551 + remoteInvalidationCalls: 0,
552 + backgroundWorkEnabled: false,
553 + };
554 + return Effect.gen(function* () {
555 + const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster;
556 + yield* broadcaster.getStatus({ cwd: "/repo" });
557 + const started = yield* Deferred.make<void>();
558 + const release = yield* Deferred.make<void>();
559 + state.beforeRemoteStatus = Deferred.succeed(started, undefined).pipe(
560 + Effect.andThen(Deferred.await(release)),
561 + );
562 + const lookup = yield* broadcaster.refreshPullRequestStatus("/repo").pipe(Effect.forkScoped);
563 + yield* Deferred.await(started);
564 + state.currentRemoteStatus = { ...baseRemoteStatus, aheadCount: 4, behindCount: 0 };
565 + yield* broadcaster.refreshLocalStatus("/repo");
566 + const status = yield* broadcaster.getStatus({ cwd: "/repo" });
567 + assert.equal(status.aheadCount, 4);
568 + assert.equal(status.behindCount, 0);
569 + assert.equal(state.remoteStatusCalls, 1);
570 + state.currentDivergence = { aheadCount: 4, behindCount: behindAfterLookup };
571 + state.currentRemoteStatus =
572 + behindAfterLookup > 0
573 + ? { ...baseRemoteStatus, ...state.currentDivergence }
574 + : { ...baseRemoteStatus, aheadCount: 2, behindCount: 3 };
575 + yield* Deferred.succeed(release, undefined);
576 + yield* Fiber.join(lookup);
577 + const afterLookup = yield* broadcaster.getStatus({ cwd: "/repo" });
578 + assert.equal(afterLookup.aheadCount, 4);
579 + assert.equal(afterLookup.behindCount, behindAfterLookup);
580 + }).pipe(Effect.provide(layerTestFor(state)), Effect.scoped);
581 + },
582 + );
583 +
584 it.effect("a local-only refresh after a branch switch leaves the PR refresh able to read", () => {
585 const state: Parameters<typeof layerTestFor>[0] = {
586 currentLocalStatus: baseLocalStatus,
apps/server/src/vcs/VcsStatusBroadcaster.ts
+36 -2
@@ -451,7 +451,30 @@ export const make = Effect.gen(function* () {
451 "VcsStatusBroadcaster.refreshLocalStatus",
452 )(function* (rawCwd) {
453 const cwd = yield* withFileSystem(normalizeCwd(rawCwd));
454 - return yield* refreshLocalStatusCore(cwd);
454 + const local = yield* refreshLocalStatusCore(cwd);
455 + const cached = yield* getCachedStatus(cwd);
456 + // Divergence is local Git data; a slow forge lookup must not delay turn-end updates.
457 + if (cached?.remote?.value?.hasUpstream) {
458 + const divergence = yield* workflow.upstreamDivergence(cwd);
459 + const latest = yield* getCachedStatus(cwd);
460 + if (divergence && latest?.remote?.value && latest.remote.branch === divergence.refName) {
461 + const value = {
462 + ...latest.remote.value,
463 + aheadCount: divergence.aheadCount,
464 + behindCount: divergence.behindCount,
465 + };
466 + yield* updateCachedRemoteStatus(
467 + cwd,
468 + {
469 + branch: divergence.refName,
470 + value,
471 + fingerprint: fingerprintStatusPart(value),
472 + },
473 + { publish: true },
474 + );
475 + }
476 + }
477 + return local;
478 });
479
480 const readRemoteStatus = Effect.fn("VcsStatusBroadcaster.readRemoteStatus")(function* (
@@ -461,7 +484,18 @@ export const make = Effect.gen(function* () {
484 const branch = yield* workflow.currentBranch(cwd);
485 // GitManager's remote TTL cache is keyed by cwd, not the checked-out branch.
486 yield* workflow.invalidateRemoteStatus(cwd);
464 - const value = yield* workflow.remoteStatus({ cwd }, options);
487 + const previousRemote = (yield* getCachedStatus(cwd))?.remote;
488 + const remote = yield* workflow.remoteStatus({ cwd }, options);
489 + const latest = yield* getCachedStatus(cwd);
490 + // An overlapping turn refresh and fetch can both move divergence during the forge lookup.
491 + const refreshed =
492 + remote?.hasUpstream && latest?.remote !== previousRemote && latest?.remote?.branch === branch
493 + ? yield* workflow.upstreamDivergence(cwd)
494 + : null;
495 + const value =
496 + remote && refreshed?.refName === branch
497 + ? { ...remote, aheadCount: refreshed.aheadCount, behindCount: refreshed.behindCount }
498 + : remote;
499 const currentBranch = yield* workflow.currentBranch(cwd);
500 const cached = yield* getCachedStatus(cwd);
501 if (branch !== currentBranch || cached?.local?.value.refName !== currentBranch) {