Skip to content

The agent that grows with you

fix(desktop): retain open pane gateway owners

joaomarcos3cf4f7abf7fa
diff --git a/apps/desktop/src/app/gateway/hooks/use-gateway-boot.ts b/apps/desktop/src/app/gateway/hooks/use-gateway-boot.ts
index 8b9d2d198b8..52b6cefaedc 100644
--- a/apps/desktop/src/app/gateway/hooks/use-gateway-boot.ts
+++ b/apps/desktop/src/app/gateway/hooks/use-gateway-boot.ts
@@ -61,8 +61,8 @@ import {
} from '@/store/session'
import {
$attentionSessionIds,
- $workingSessionIds,
$sessionTiles,
+ $workingSessionIds,
foregroundSessionScopes,
liveSessionScopes,
openTileGatewayScopes,
@@ -811,6 +811,8 @@ export function useGatewayBoot({
const offWorking = $workingSessionIds.subscribe(() => recomputeKeptGateways())
const offAttention = $attentionSessionIds.subscribe(() => recomputeKeptGateways())
+ const offActiveSession = $activeSessionId.subscribe(() => recomputeKeptGateways())
+ const offSessionTiles = $sessionTiles.subscribe(() => recomputeKeptGateways())
const offActiveProfile = $activeGatewayProfile.subscribe(() => recomputeKeptGateways())
const offTiles = $sessionTiles.subscribe(() => recomputeKeptGateways())
@@ -1006,6 +1008,8 @@ export function useGatewayBoot({
clearInterval(keepaliveTimer)
offWorking()
offAttention()
+ offActiveSession()
+ offSessionTiles()
offActiveProfile()
offTiles()
window.removeEventListener('online', onOnline)
diff --git a/apps/desktop/src/store/gateway-connection-scope.test.ts b/apps/desktop/src/store/gateway-connection-scope.test.ts
index 9c0105cb061..bd86ec20b10 100644
--- a/apps/desktop/src/store/gateway-connection-scope.test.ts
+++ b/apps/desktop/src/store/gateway-connection-scope.test.ts
@@ -40,6 +40,7 @@ vi.mock('@/store/session', () => ({
vi.mock('@/store/notify-baseline', () => ({ markNativeNotifyBaseline: vi.fn() }))
const {
+ closeLegacySecondaryGateways,
closeSecondaryGateways,
configureGatewayRegistry,
ensureGatewayForAgent,
@@ -149,4 +150,18 @@ describe('pruneSecondaryGateways with registry-scoped entries', () => {
expect(gatewayMocks.closed).toEqual(['wss://local.invalid/api/ws?token=t'])
})
+
+ it('does not classify an explicit local registry source as legacy', async () => {
+ await openGatewayForAgent(null, 'writer')
+ await openGatewayForAgent('local', 'writer')
+
+ expect(gatewayMocks.closed).toEqual([])
+
+ closeLegacySecondaryGateways()
+
+ // The bare profile socket follows the v1 mode configuration and is
+ // retired. The explicit `local` registry socket is a v2 source and must
+ // survive the mode apply just like a registered remote source.
+ expect(gatewayMocks.closed).toEqual(['wss://local.invalid/api/ws?token=t'])
+ })
})
diff --git a/apps/desktop/src/store/gateway.ts b/apps/desktop/src/store/gateway.ts
index fd7fd8d2298..e76d44ea2a5 100644
--- a/apps/desktop/src/store/gateway.ts
+++ b/apps/desktop/src/store/gateway.ts
@@ -1431,6 +1431,15 @@ function closeSecondariesWhere(shouldClose: (entry: Secondary) => boolean): void
restoreActiveToPrimaryIfEvicted()
}
+function isLegacySecondary(entry: Secondary): boolean {
+ // Every v2 registry route is created with an explicit connection id,
+ // including the registry's `local` source. A missing id is reserved for the
+ // old profile-only pool; the loose null check also retires HMR entries from
+ // builds that predate the field instead of leaving an old legacy socket
+ // behind during a mode apply.
+ return entry.connectionId == null
+}
+
/**
* Close only profile sockets that follow the legacy v1 connection config.
*
@@ -1441,7 +1450,7 @@ function closeSecondariesWhere(shouldClose: (entry: Secondary) => boolean): void
* retired because their endpoint is derived from the v1 config being changed.
*/
export function closeLegacySecondaryGateways(): void {
- closeSecondariesWhere(entry => entry.connectionId == null)
+ closeSecondariesWhere(isLegacySecondary)
}
export function closeSecondaryGateways(): void {
diff --git a/apps/desktop/src/store/session-states.test.ts b/apps/desktop/src/store/session-states.test.ts
index 73790dc223b..f21e701e755 100644
--- a/apps/desktop/src/store/session-states.test.ts
+++ b/apps/desktop/src/store/session-states.test.ts
@@ -39,11 +39,13 @@ describe('foregroundSessionScopes', () => {
beforeEach(() => {
clearAllSessionStates()
$activeSessionId.set(null)
+ $sessionTiles.set([])
})
afterEach(() => {
clearAllSessionStates()
$activeSessionId.set(null)
+ $sessionTiles.set([])
})
it('keeps the exact registry owner of an idle foreground runtime', () => {
@@ -58,6 +60,37 @@ describe('foregroundSessionScopes', () => {
expect(foregroundSessionScopes()).toEqual(new Set())
})
+
+ it('keeps every open pane owner, not only the focused runtime', () => {
+ recordSessionEventScope({ connectionId: 'cloud-a', profile: 'default', session_id: 'runtime-a' })
+ recordSessionEventScope({ connectionId: 'cloud-b', profile: 'default', session_id: 'runtime-b' })
+ $sessionTiles.set([
+ { runtimeId: 'runtime-a', storedSessionId: 'stored-a' },
+ {
+ ownerRoute: { connectionId: 'cloud-b', profile: 'default' },
+ storedSessionId: 'stored-b'
+ }
+ ])
+
+ expect(foregroundSessionScopes()).toEqual(
+ new Set(['conn:cloud-a::default', 'conn:cloud-b::default'])
+ )
+ })
+
+ it('releases an idle pane owner when the pane closes', () => {
+ $sessionTiles.set([
+ {
+ ownerRoute: { connectionId: 'cloud', profile: 'default' },
+ storedSessionId: 'stored-cloud'
+ }
+ ])
+
+ expect(foregroundSessionScopes()).toEqual(new Set(['conn:cloud::default']))
+
+ $sessionTiles.set([])
+
+ expect(foregroundSessionScopes()).toEqual(new Set())
+ })
})
describe('resetTileRuntimeBindings', () => {
diff --git a/apps/desktop/src/store/session-states.ts b/apps/desktop/src/store/session-states.ts
index 01339e09ba8..59449da07c8 100644
--- a/apps/desktop/src/store/session-states.ts
+++ b/apps/desktop/src/store/session-states.ts
@@ -100,19 +100,43 @@ export function liveSessionScopes(): Set<string> {
}
/**
- * The exact registry scope that owns the foreground runtime, when known.
+ * Registry scopes owned by an open foreground surface, when known.
*
* The secondary-gateway pruner normally keeps only busy/needs-input work. A
- * source switch briefly changes the active gateway before the old foreground
- * runtime is cleared, though, and an idle conversation can otherwise look
- * disposable during that handoff. Preserve the owner for that narrow window;
- * tiles have their own persisted owner route and are handled separately.
+ * source switch briefly changes the active gateway before an idle conversation
+ * is cleared, so the primary runtime must survive that handoff. Open panes have
+ * the same ownership contract: a non-focused idle tile is still user-visible
+ * state and must not be evicted just because another pane has focus. Prefer the
+ * live event scope, with the tile's persisted route as the pre-bind fallback.
*/
export function foregroundSessionScopes(): Set<string> {
- const runtimeId = $activeSessionId.get()
- const scope = runtimeId ? sessionScopeByRuntimeId.get(runtimeId) : undefined
+ const scopes = new Set<string>()
+
+ const addRuntimeScope = (runtimeId: string | undefined) => {
+ const scope = runtimeId ? sessionScopeByRuntimeId.get(runtimeId) : undefined
- return scope ? new Set([scope]) : new Set()
+ if (scope) {
+ scopes.add(scope)
+ }
+ }
+
+ const addRouteScope = (route: SessionProfileRoute | undefined) => {
+ const connectionId = route?.connectionId?.trim()
+ const profile = route?.profile?.trim()
+
+ if (connectionId && profile) {
+ scopes.add(registryBackendScopeKey(connectionId, profile))
+ }
+ }
+
+ addRuntimeScope($activeSessionId.get() ?? undefined)
+
+ for (const tile of $sessionTiles.get()) {
+ addRuntimeScope(tile.runtimeId)
+ addRouteScope(tile.ownerRoute)
+ }
+
+ return scopes
}
// Stored session ids whose authoritative state is still busy, but whose