Skip to content
TypeScript

fix(web): refresh the current branch when opening the picker (#22807)

Utkarsh Patil5b35402623d7
diff --git a/apps/server/src/vcs/VcsStatusBroadcaster.test.ts b/apps/server/src/vcs/VcsStatusBroadcaster.test.ts
index 334c0d4747b..afb56e53e3a 100644
--- a/apps/server/src/vcs/VcsStatusBroadcaster.test.ts
+++ b/apps/server/src/vcs/VcsStatusBroadcaster.test.ts
@@ -156,6 +156,119 @@ function layerBackgroundPolicy(shouldRunScopeWork: (scope: BackgroundScope) => b
}
describe("VcsStatusBroadcaster", () => {
+ it.effect("publishes a terminal branch switch without reading remote status", () => {
+ const state = {
+ currentLocalStatus: baseLocalStatus,
+ currentRemoteStatus: remoteStatusWithPr,
+ localStatusCalls: 0,
+ remoteStatusCalls: 0,
+ localInvalidationCalls: 0,
+ remoteInvalidationCalls: 0,
+ };
+
+ return Effect.gen(function* () {
+ const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster;
+ yield* broadcaster.getStatus({ cwd: "/repo" });
+ const subscribed = yield* Deferred.make<void>();
+ const events = yield* broadcaster.streamStatus({ cwd: "/repo", includeRemote: false }).pipe(
+ Stream.tap((event) =>
+ event._tag === "snapshot" ? Deferred.succeed(subscribed, undefined) : Effect.void,
+ ),
+ Stream.take(2),
+ Stream.runCollect,
+ Effect.forkScoped,
+ );
+ yield* Deferred.await(subscribed);
+
+ state.currentLocalStatus = { ...baseLocalStatus, refName: "main" };
+ const refreshed = yield* broadcaster.refreshStatus("/repo", { localOnly: true });
+ assert.equal(refreshed.refName, "main");
+ assert.isNull(refreshed.pr);
+ assert.equal(state.localInvalidationCalls, 1);
+ assert.equal(state.remoteStatusCalls, 1);
+ assert.equal(state.remoteInvalidationCalls, 1);
+ assert.deepStrictEqual((yield* Fiber.join(events))[1], {
+ _tag: "snapshot",
+ local: state.currentLocalStatus,
+ remote: null,
+ });
+ assert.isNull((yield* broadcaster.getStatus({ cwd: "/repo" })).pr);
+ const snapshot = yield* broadcaster
+ .streamStatus({ cwd: "/repo", includeRemote: false })
+ .pipe(Stream.take(1), Stream.runCollect);
+ assert.deepStrictEqual(snapshot[0], {
+ _tag: "snapshot",
+ local: state.currentLocalStatus,
+ remote: null,
+ });
+ }).pipe(Effect.provide(layerTestFor(state)));
+ });
+
+ it.effect(
+ "preserves cached remote status when a local-only refresh stays on the same branch",
+ () => {
+ const state = {
+ currentLocalStatus: baseLocalStatus,
+ currentRemoteStatus: { ...remoteStatusWithPr, aheadCount: 2, behindCount: 3 },
+ localStatusCalls: 0,
+ remoteStatusCalls: 0,
+ localInvalidationCalls: 0,
+ remoteInvalidationCalls: 0,
+ };
+
+ return Effect.gen(function* () {
+ const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster;
+ const initial = yield* broadcaster.getStatus({ cwd: "/repo" });
+ const refreshed = yield* broadcaster.refreshStatus("/repo", { localOnly: true });
+ assert.deepStrictEqual(refreshed, initial);
+ assert.deepStrictEqual(yield* broadcaster.getStatus({ cwd: "/repo" }), refreshed);
+ assert.equal(state.localInvalidationCalls, 1);
+ assert.equal(state.remoteInvalidationCalls, 0);
+ assert.equal(state.remoteStatusCalls, 1);
+ }).pipe(Effect.provide(layerTestFor(state)));
+ },
+ );
+
+ it.effect("clears the previous branch's remote status after an in-flight PR refresh", () => {
+ const remoteReadStarted = Deferred.makeUnsafe<void>();
+ const releaseRemoteRead = Deferred.makeUnsafe<void>();
+ const state = {
+ currentLocalStatus: baseLocalStatus,
+ currentRemoteStatus: remoteStatusWithPr,
+ localStatusCalls: 0,
+ remoteStatusCalls: 0,
+ localInvalidationCalls: 0,
+ remoteInvalidationCalls: 0,
+ beforeRemoteStatus: Effect.void,
+ };
+
+ return Effect.gen(function* () {
+ const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster;
+ yield* broadcaster.getStatus({ cwd: "/repo" });
+ state.beforeRemoteStatus = Deferred.succeed(remoteReadStarted, undefined).pipe(
+ Effect.andThen(Deferred.await(releaseRemoteRead)),
+ );
+ const remoteRefresh = yield* broadcaster
+ .refreshPullRequestStatus("/repo")
+ .pipe(Effect.forkScoped);
+ yield* Deferred.await(remoteReadStarted);
+ state.currentLocalStatus = { ...baseLocalStatus, refName: "main" };
+ const localRefresh = yield* broadcaster
+ .refreshStatus("/repo", { localOnly: true })
+ .pipe(Effect.forkScoped);
+ yield* Deferred.succeed(releaseRemoteRead, undefined);
+ yield* Fiber.join(remoteRefresh);
+ yield* Fiber.join(localRefresh);
+
+ const status = yield* broadcaster.getStatus({ cwd: "/repo" });
+ assert.equal(status.refName, "main");
+ assert.isNull(status.pr);
+ assert.equal(status.aheadCount, 0);
+ assert.equal(status.behindCount, 0);
+ assert.equal(state.remoteStatusCalls, 2);
+ }).pipe(Effect.provide(layerTestFor(state)));
+ });
+
it.effect.skipIf(!symlinksSupported)(
"automatically pulls an enabled clean default branch when status detects it is behind",
() => {
diff --git a/apps/server/src/vcs/VcsStatusBroadcaster.ts b/apps/server/src/vcs/VcsStatusBroadcaster.ts
index 007a0ea1e31..3cbcb3546e9 100644
--- a/apps/server/src/vcs/VcsStatusBroadcaster.ts
+++ b/apps/server/src/vcs/VcsStatusBroadcaster.ts
@@ -207,7 +207,10 @@ export class VcsStatusBroadcaster extends Context.Service<
readonly refreshLocalStatus: (
cwd: string,
) => Effect.Effect<VcsStatusLocalResult, GitManagerServiceError>;
- readonly refreshStatus: (cwd: string) => Effect.Effect<VcsStatusResult, GitManagerServiceError>;
+ readonly refreshStatus: (
+ cwd: string,
+ options?: { readonly localOnly?: boolean },
+ ) => Effect.Effect<VcsStatusResult, GitManagerServiceError>;
readonly refreshPullRequestStatus: (
cwd: string,
) => Effect.Effect<VcsStatusRemoteResult | null, GitManagerServiceError>;
@@ -506,8 +509,24 @@ export const make = Effect.gen(function* () {
const refreshStatus: VcsStatusBroadcaster["Service"]["refreshStatus"] = Effect.fn(
"VcsStatusBroadcaster.refreshStatus",
- )(function* (rawCwd) {
+ )(function* (rawCwd, options) {
const cwd = yield* withFileSystem(normalizeCwd(rawCwd));
+ if (options?.localOnly) {
+ return yield* withRemoteWriteLock(
+ cwd,
+ Effect.gen(function* () {
+ yield* workflow.invalidateLocalStatus(cwd);
+ const local = yield* workflow.localStatus({ cwd });
+ const previous = yield* getCachedStatus(cwd);
+ if (previous?.local?.value.refName !== local.refName) {
+ yield* workflow.invalidateRemoteStatus(cwd);
+ return yield* updateCachedStatus(cwd, local, null, { publish: true });
+ }
+ yield* updateCachedLocalStatus(cwd, local, { publish: true });
+ return mergeGitStatusParts(local, previous?.remote?.value ?? null);
+ }),
+ );
+ }
// Full invalidation also bypasses GitManager's slow PR cache.
return yield* withRemoteWriteLock(
cwd,
diff --git a/apps/server/src/ws.ts b/apps/server/src/ws.ts
index 4341cd926d3..a03176a455e 100644
--- a/apps/server/src/ws.ts
+++ b/apps/server/src/ws.ts
@@ -2932,7 +2932,8 @@ export const layerWsRpc = (
worktreeSetupTracker
.cancel(input.threadId)
.pipe(Effect.map((cancelled) => ({ cancelled }))),
- [WS_METHODS.vcsRefreshStatus]: (input) => vcsStatusBroadcaster.refreshStatus(input.cwd),
+ [WS_METHODS.vcsRefreshStatus]: (input) =>
+ vcsStatusBroadcaster.refreshStatus(input.cwd, { localOnly: input.localOnly ?? false }),
[WS_METHODS.vcsPull]: (input) =>
gitWorkflow.pullCurrentBranch(input.cwd).pipe(
Effect.matchCauseEffect({
diff --git a/apps/web/src/components/BranchToolbarBranchSelector.tsx b/apps/web/src/components/BranchToolbarBranchSelector.tsx
index 2ec57669564..d29ef7995c1 100644
--- a/apps/web/src/components/BranchToolbarBranchSelector.tsx
+++ b/apps/web/src/components/BranchToolbarBranchSelector.tsx
@@ -126,6 +126,9 @@ export function BranchToolbarBranchSelector({
const switchRef = useAtomCommand(vcsEnvironment.switchRef, {
reportFailure: false,
});
+ const refreshStatus = useAtomCommand(vcsEnvironment.refreshStatus, {
+ reportFailure: false,
+ });
const createRefMutation = useAtomCommand(vcsEnvironment.createRef, {
reportFailure: false,
});
@@ -503,6 +506,9 @@ export function BranchToolbarBranchSelector({
const handleOpenChange = (open: boolean) => {
setIsBranchMenuOpen(open);
+ if (open && !isBranchMenuOpen && branchCwd !== null) {
+ void refreshStatus({ environmentId, input: { cwd: branchCwd, localOnly: true } });
+ }
if (!open) {
setBranchQuery("");
}
diff --git a/packages/contracts/src/rpc.ts b/packages/contracts/src/rpc.ts
index 025d3c0ce96..088935c5291 100644
--- a/packages/contracts/src/rpc.ts
+++ b/packages/contracts/src/rpc.ts
@@ -1361,7 +1361,10 @@ const WsVcsPullRpc = Rpc.make(WS_METHODS.vcsPull, {
});
const WsVcsRefreshStatusRpc = Rpc.make(WS_METHODS.vcsRefreshStatus, {
- payload: VcsStatusInput,
+ payload: Schema.Struct({
+ ...VcsStatusInput.fields,
+ localOnly: Schema.optional(Schema.Boolean),
+ }),
success: VcsStatusResult,
error: Schema.Union([GitManagerServiceError, EnvironmentAuthorizationError]),
});