Skip to content

Commit 274474d

Browse files
authored
fix(server): refresh sync status after agent merges (#35919)
1 parent afff0d3 commit 274474d

3 files changed

Lines changed: 107 additions & 2 deletions

File tree

‎apps/server/src/git/GitWorkflowService.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ export class GitWorkflowService extends Context.Service<
5353
input: VcsStatusInput,
5454
) => Effect.Effect<VcsStatusLocalResult, GitManagerServiceError>;
5555
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+
>;
5664
readonly remoteStatus: (
5765
input: VcsStatusInput,
5866
options?: GitManager.GitRemoteStatusOptions,
@@ -363,6 +371,16 @@ export const make = Effect.gen(function* () {
363371
detail: "Failed to read the checked-out branch.",
364372
});
365373
}),
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+
}),
366384
remoteStatus: Effect.fn("GitWorkflowService.remoteStatus")(function (input, options) {
367385
return detectGitRepositoryForStatus("GitWorkflowService.remoteStatus", input.cwd).pipe(
368386
Effect.flatMap((isGitRepository) =>

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ const remoteStatusWithPr: VcsStatusRemoteResult = {
7171
function layerTestFor(state: {
7272
currentLocalStatus: VcsStatusLocalResult;
7373
currentRemoteStatus: VcsStatusRemoteResult | null;
74+
currentDivergence?: Pick<VcsStatusRemoteResult, "aheadCount" | "behindCount">;
7475
localStatusCalls: number;
7576
remoteStatusCalls: number;
7677
localInvalidationCalls: number;
@@ -133,6 +134,16 @@ function layerTestFor(state: {
133134
: (state.beforeCurrentBranch ?? Effect.void).pipe(
134135
Effect.andThen(Effect.sync(() => state.currentLocalStatus.refName)),
135136
),
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+
),
136147
remoteStatus: (_input, options) =>
137148
Effect.suspend(() => {
138149
return (state.beforeRemoteStatus ?? Effect.void).pipe(
@@ -528,6 +539,48 @@ describe("VcsStatusBroadcaster", () => {
528539
},
529540
);
530541

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+
531584
it.effect("a local-only refresh after a branch switch leaves the PR refresh able to read", () => {
532585
const state: Parameters<typeof layerTestFor>[0] = {
533586
currentLocalStatus: baseLocalStatus,

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

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,30 @@ export const make = Effect.gen(function* () {
451451
"VcsStatusBroadcaster.refreshLocalStatus",
452452
)(function* (rawCwd) {
453453
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;
455478
});
456479

457480
const readRemoteStatus = Effect.fn("VcsStatusBroadcaster.readRemoteStatus")(function* (
@@ -461,7 +484,18 @@ export const make = Effect.gen(function* () {
461484
const branch = yield* workflow.currentBranch(cwd);
462485
// GitManager's remote TTL cache is keyed by cwd, not the checked-out branch.
463486
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;
465499
const currentBranch = yield* workflow.currentBranch(cwd);
466500
const cached = yield* getCachedStatus(cwd);
467501
if (branch !== currentBranch || cached?.local?.value.refName !== currentBranch) {

0 commit comments

Comments
 (0)