Skip to content

Commit 061f25d

Browse files
fix(server): creating and switching to a branch also answers once its status is published
The branch picker's create action runs vcsCreateRef with switchRef and had the same stale-label window as a plain switch. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 2a06f09 commit 061f25d

2 files changed

Lines changed: 125 additions & 99 deletions

File tree

‎apps/server/src/ws.test.ts‎

Lines changed: 116 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1436,110 +1436,128 @@ it.effect("a publish whose push fails still refreshes the new remote", () =>
14361436
),
14371437
);
14381438

1439-
it.effect("a branch switch answers only after status subscribers can see the new branch", () =>
1440-
Effect.gen(function* () {
1441-
let checkedOut = "main";
1442-
const published = yield* Ref.make("main");
1443-
const refreshStarted = yield* Deferred.make<void>();
1444-
const statusAtResponse = yield* Queue.unbounded<string>();
1445-
const local: VcsStatusLocalResult = {
1446-
isRepo: true,
1447-
hasPrimaryRemote: false,
1448-
isDefaultRef: false,
1449-
refName: "main",
1450-
hasWorkingTreeChanges: false,
1451-
workingTree: { files: [], insertions: 0, deletions: 0 },
1452-
};
1453-
// A large repository's git status takes seconds after the checkout itself has finished.
1454-
const readAndPublishStatus = Effect.gen(function* () {
1455-
const refName = checkedOut;
1456-
yield* Deferred.succeed(refreshStarted, undefined);
1457-
yield* Effect.sleep("2 seconds");
1458-
yield* Ref.set(published, refName);
1459-
return { ...local, refName };
1460-
});
1461-
const services = Layer.mergeAll(
1462-
layerUnusedConfigRpcServices,
1463-
Layer.mock(GitWorkflowService.GitWorkflowService)({
1464-
switchRef: (input) =>
1465-
Effect.sync(() => {
1466-
checkedOut = input.refName;
1467-
return { refName: input.refName };
1468-
}),
1469-
}),
1470-
Layer.mock(VcsStatusBroadcaster.VcsStatusBroadcaster)({
1471-
refreshLocalStatus: () => readAndPublishStatus,
1472-
refreshStatus: () =>
1473-
readAndPublishStatus.pipe(
1474-
Effect.map((status) => ({
1475-
...status,
1476-
hasUpstream: false,
1477-
aheadCount: 0,
1478-
behindCount: 0,
1479-
pr: null,
1480-
})),
1439+
it.effect.each([
1440+
{
1441+
name: "switching",
1442+
tag: WS_METHODS.vcsSwitchRef,
1443+
payload: { cwd: "/repo", refName: "feature" },
1444+
},
1445+
{
1446+
name: "creating and switching to",
1447+
tag: WS_METHODS.vcsCreateRef,
1448+
payload: { cwd: "/repo", refName: "feature", switchRef: true },
1449+
},
1450+
] as const)(
1451+
"$name a branch answers only after status subscribers can see the new branch",
1452+
(request) =>
1453+
Effect.gen(function* () {
1454+
let checkedOut = "main";
1455+
const published = yield* Ref.make("main");
1456+
const refreshStarted = yield* Deferred.make<void>();
1457+
const statusAtResponse = yield* Queue.unbounded<string>();
1458+
const local: VcsStatusLocalResult = {
1459+
isRepo: true,
1460+
hasPrimaryRemote: false,
1461+
isDefaultRef: false,
1462+
refName: "main",
1463+
hasWorkingTreeChanges: false,
1464+
workingTree: { files: [], insertions: 0, deletions: 0 },
1465+
};
1466+
// A large repository's git status takes seconds after the checkout itself has finished.
1467+
const readAndPublishStatus = Effect.gen(function* () {
1468+
const refName = checkedOut;
1469+
yield* Deferred.succeed(refreshStarted, undefined);
1470+
yield* Effect.sleep("2 seconds");
1471+
yield* Ref.set(published, refName);
1472+
return { ...local, refName };
1473+
});
1474+
const services = Layer.mergeAll(
1475+
layerUnusedConfigRpcServices,
1476+
Layer.mock(GitWorkflowService.GitWorkflowService)({
1477+
switchRef: (input) =>
1478+
Effect.sync(() => {
1479+
checkedOut = input.refName;
1480+
return { refName: input.refName };
1481+
}),
1482+
createRef: (input) =>
1483+
Effect.sync(() => {
1484+
if (input.switchRef) checkedOut = input.refName;
1485+
return { refName: input.refName };
1486+
}),
1487+
}),
1488+
Layer.mock(VcsStatusBroadcaster.VcsStatusBroadcaster)({
1489+
refreshLocalStatus: () => readAndPublishStatus,
1490+
refreshStatus: () =>
1491+
readAndPublishStatus.pipe(
1492+
Effect.map((status) => ({
1493+
...status,
1494+
hasUpstream: false,
1495+
aheadCount: 0,
1496+
behindCount: 0,
1497+
pr: null,
1498+
})),
1499+
),
1500+
}),
1501+
Layer.mock(ExternalLauncher.ExternalLauncher)({}),
1502+
Layer.mock(ProviderRegistry.ProviderRegistry)({}),
1503+
Layer.mock(UsageLimitSources.UsageLimitSources)({}),
1504+
Layer.mock(RemoteOpenTargets.RemoteOpenTargets)({}),
1505+
Layer.mock(DirectEndpoints.DirectEndpoints)({}),
1506+
Layer.mock(ManagedProjectFolders.ManagedProjectFolders)({ namedProjectsRoot: "/unused" }),
1507+
Layer.mock(ScratchThreadPromotion.ScratchThreadPromotion)({}),
1508+
Layer.mock(ServerEnvironment.ServerEnvironment)({}),
1509+
Layer.mock(EnvironmentAuth.EnvironmentAuth)({}),
1510+
);
1511+
const server = yield* RpcServer.makeNoSerialization(WsRpcGroup, {
1512+
...WS_RPC_SERVER_OPTIONS,
1513+
// Runs as the response is produced, before any later fiber can publish status.
1514+
onFromServer: (response) =>
1515+
response._tag === "Exit"
1516+
? Ref.get(published).pipe(Effect.flatMap((ref) => Queue.offer(statusAtResponse, ref)))
1517+
: Effect.void,
1518+
}).pipe(
1519+
Effect.provide(
1520+
layerWsRpc(
1521+
{
1522+
sessionId: AuthSessionId.make("switch-ref-test"),
1523+
subject: "switch-ref-test",
1524+
method: "browser-session-cookie",
1525+
scopes: [AuthSourceControlWriteScope],
1526+
},
1527+
{},
1528+
yield* ServerBrowser.ServerBrowser,
1529+
() => Effect.void,
1530+
{ secretReferences: true },
1531+
).pipe(
1532+
Layer.provide(services),
1533+
Layer.merge(RpcAuthorization.layer([AuthSourceControlWriteScope])),
14811534
),
1482-
}),
1483-
Layer.mock(ExternalLauncher.ExternalLauncher)({}),
1484-
Layer.mock(ProviderRegistry.ProviderRegistry)({}),
1485-
Layer.mock(UsageLimitSources.UsageLimitSources)({}),
1486-
Layer.mock(RemoteOpenTargets.RemoteOpenTargets)({}),
1487-
Layer.mock(DirectEndpoints.DirectEndpoints)({}),
1488-
Layer.mock(ManagedProjectFolders.ManagedProjectFolders)({ namedProjectsRoot: "/unused" }),
1489-
Layer.mock(ScratchThreadPromotion.ScratchThreadPromotion)({}),
1490-
Layer.mock(ServerEnvironment.ServerEnvironment)({}),
1491-
Layer.mock(EnvironmentAuth.EnvironmentAuth)({}),
1492-
);
1493-
const server = yield* RpcServer.makeNoSerialization(WsRpcGroup, {
1494-
...WS_RPC_SERVER_OPTIONS,
1495-
// Runs as the response is produced, before any later fiber can publish status.
1496-
onFromServer: (response) =>
1497-
response._tag === "Exit"
1498-
? Ref.get(published).pipe(Effect.flatMap((ref) => Queue.offer(statusAtResponse, ref)))
1499-
: Effect.void,
1535+
),
1536+
);
1537+
yield* server.write(0, {
1538+
_tag: "Request",
1539+
id: RpcMessage.RequestId("switch"),
1540+
tag: request.tag,
1541+
payload: request.payload,
1542+
headers: Headers.empty,
1543+
});
1544+
yield* Deferred.await(refreshStarted);
1545+
yield* TestClock.adjust("2 seconds");
1546+
assert.equal(
1547+
yield* Queue.take(statusAtResponse),
1548+
"feature",
1549+
`${request.tag} resolved while status subscribers still saw the previous branch`,
1550+
);
15001551
}).pipe(
15011552
Effect.provide(
1502-
layerWsRpc(
1503-
{
1504-
sessionId: AuthSessionId.make("switch-ref-test"),
1505-
subject: "switch-ref-test",
1506-
method: "browser-session-cookie",
1507-
scopes: [AuthSourceControlWriteScope],
1508-
},
1509-
{},
1510-
yield* ServerBrowser.ServerBrowser,
1511-
() => Effect.void,
1512-
{ secretReferences: true },
1513-
).pipe(
1514-
Layer.provide(services),
1515-
Layer.merge(RpcAuthorization.layer([AuthSourceControlWriteScope])),
1553+
Layer.mergeAll(
1554+
Layer.fresh(layerConfigSources),
1555+
Layer.mock(PreviewAutomationBroker.PreviewAutomationBroker)({}),
1556+
Layer.mock(ServerBrowser.ServerBrowser)({}),
15161557
),
15171558
),
1518-
);
1519-
yield* server.write(0, {
1520-
_tag: "Request",
1521-
id: RpcMessage.RequestId("switch"),
1522-
tag: WS_METHODS.vcsSwitchRef,
1523-
payload: { cwd: "/repo", refName: "feature" },
1524-
headers: Headers.empty,
1525-
});
1526-
yield* Deferred.await(refreshStarted);
1527-
yield* TestClock.adjust("2 seconds");
1528-
assert.equal(
1529-
yield* Queue.take(statusAtResponse),
1530-
"feature",
1531-
"vcsSwitchRef resolved while status subscribers still saw the previous branch",
1532-
);
1533-
}).pipe(
1534-
Effect.provide(
1535-
Layer.mergeAll(
1536-
Layer.fresh(layerConfigSources),
1537-
Layer.mock(PreviewAutomationBroker.PreviewAutomationBroker)({}),
1538-
Layer.mock(ServerBrowser.ServerBrowser)({}),
1539-
),
1559+
Effect.scoped,
15401560
),
1541-
Effect.scoped,
1542-
),
15431561
);
15441562

15451563
// Pruned history cannot be replayed: a cursor below the boundary must resync from a snapshot.

‎apps/server/src/ws.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3188,7 +3188,15 @@ export const layerWsRpc = (
31883188
[WS_METHODS.vcsRemoveWorktree]: (input) =>
31893189
gitWorkflow.removeWorktree(input).pipe(Effect.tap(() => refreshGitStatus(input.cwd))),
31903190
[WS_METHODS.vcsCreateRef]: (input) =>
3191-
gitWorkflow.createRef(input).pipe(Effect.tap(() => refreshGitStatus(input.cwd))),
3191+
gitWorkflow
3192+
.createRef(input)
3193+
.pipe(
3194+
Effect.tap(() =>
3195+
input.switchRef
3196+
? refreshGitStatusAfterCheckout(input.cwd)
3197+
: refreshGitStatus(input.cwd),
3198+
),
3199+
),
31923200
[WS_METHODS.vcsSwitchRef]: (input) =>
31933201
gitWorkflow
31943202
.switchRef(input)

0 commit comments

Comments
 (0)