Skip to content

The agent that grows with you

fix(desktop): rebind legacy-remote-primary Bot tiles via live-connection scoping

Teknium58d84d3e0325
diff --git a/apps/desktop/src/app/gateway/hooks/use-gateway-boot.test.tsx b/apps/desktop/src/app/gateway/hooks/use-gateway-boot.test.tsx
index fe3f14343af..741d9627ab7 100644
--- a/apps/desktop/src/app/gateway/hooks/use-gateway-boot.test.tsx
+++ b/apps/desktop/src/app/gateway/hooks/use-gateway-boot.test.tsx
@@ -34,7 +34,7 @@ describe('primaryRuntimeConnectionId', () => {
expect(primaryRuntimeConnectionId({ mode: 'local' })).toBe('local')
})
- it('preserves legacy remote Bot bindings when the primary identity is unknown', () => {
+ it('returns null for an unknown remote identity so the caller falls back to live-connection scoping', () => {
expect(primaryRuntimeConnectionId({ mode: 'remote' })).toBeNull()
})
})
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 b1eddf11394..30d76bf929a 100644
--- a/apps/desktop/src/app/gateway/hooks/use-gateway-boot.ts
+++ b/apps/desktop/src/app/gateway/hooks/use-gateway-boot.ts
@@ -23,6 +23,7 @@ import {
ensureGatewayForProfile,
gatewayActivationEpoch,
isActivePrimary,
+ liveSecondaryConnectionIds,
pruneSecondaryGateways,
reconnectSecondaryGateways,
reportPrimaryGatewayState,
@@ -264,7 +265,12 @@ export function useGatewayBoot({
reconnectFailingSince = null
// A respawned backend re-mints (recycles) runtime ids, so any tile's
// bound runtime id is now stale — drop them so each tile re-resumes.
- resetTileRuntimeBindings(primaryRuntimeConnectionId(conn))
+ // A legacy remote primary has no registry identity to scope by; fall
+ // back to preserving only Bot runtimes owned by provably-live
+ // secondaries so the restarted backend's own tiles still rebind.
+ resetTileRuntimeBindings(
+ primaryRuntimeConnectionId(conn) ?? { liveConnectionIds: liveSecondaryConnectionIds() }
+ )
// Same staleness, other half: pre-reconnect busy flags are keyed by
// those dead runtime ids and would never receive their terminal
// busy:false — clear them or the sidebar running arc lies forever
diff --git a/apps/desktop/src/store/gateway.ts b/apps/desktop/src/store/gateway.ts
index 0a36e2cc209..0ca9f4ae6d5 100644
--- a/apps/desktop/src/store/gateway.ts
+++ b/apps/desktop/src/store/gateway.ts
@@ -218,6 +218,24 @@ export function activeGatewayConnectionId(): null | string {
return g.secondaries.get(g.activeKey)?.connectionId ?? null
}
+/**
+ * Registry connections currently served by a live (open-socket) secondary.
+ * Used by the reconnect path when the restarted primary's own registry
+ * identity is unknown: Bot runtimes owned by these connections are provably
+ * NOT the restarted backend and keep their bindings; everything else re-resumes.
+ */
+export function liveSecondaryConnectionIds(): Set<string> {
+ const live = new Set<string>()
+
+ for (const entry of g.secondaries.values()) {
+ if (entry.connectionId && isOpen(entry.gateway)) {
+ live.add(entry.connectionId)
+ }
+ }
+
+ return live
+}
+
// Mirror a backend's connection state into the global composer state, but only
// when that backend is the one the user is currently looking at. Lets the
// composer reflect the active profile's socket without a background reconnect
diff --git a/apps/desktop/src/store/session-states.test.ts b/apps/desktop/src/store/session-states.test.ts
index 4f6b15ce289..8cf8435a7d0 100644
--- a/apps/desktop/src/store/session-states.test.ts
+++ b/apps/desktop/src/store/session-states.test.ts
@@ -125,6 +125,41 @@ describe('resetTileRuntimeBindings', () => {
expect(ordinarySession).not.toHaveProperty('runtimeId')
expect(invalidateRuntimeBindings).toHaveBeenCalledWith(new Set(['stored-barry-sibling-bot', 'stored-work-bot']))
})
+
+ it('unknown restarted identity preserves only Bot runtimes owned by provably-live connections', () => {
+ // Legacy remote primary: no registry connectionId to scope by. The dead
+ // owner can't be named, so keep only owners we know are alive elsewhere —
+ // the restarted backend's own Bot tile must still drop its binding.
+ const invalidateRuntimeBindings = vi.fn()
+ setSessionTileDelegate({ invalidateRuntimeBindings } as unknown as SessionTileDelegate)
+ $sessionTiles.set([
+ {
+ ownerRoute: { connectionId: 'legacy-remote', mode: 'remote', profile: 'writer', targetProfile: 'writer' },
+ runtimeId: 'runtime-legacy-dead',
+ storedSessionId: 'stored-legacy-bot',
+ workspaceMode: 'bots',
+ workspaceOwnerKey: 'bot:legacy-remote::writer'
+ },
+ {
+ ownerRoute: { connectionId: 'work-vps', mode: 'remote', profile: 'ceo', targetProfile: 'ceo' },
+ runtimeId: 'runtime-work-live',
+ storedSessionId: 'stored-work-bot',
+ workspaceMode: 'bots',
+ workspaceOwnerKey: 'bot:work-vps::ceo'
+ },
+ { runtimeId: 'runtime-session-dead', storedSessionId: 'stored-session' }
+ ])
+
+ resetTileRuntimeBindings({ liveConnectionIds: new Set(['work-vps']) })
+
+ const [legacyBot, workBot, ordinarySession] = $sessionTiles.get()
+
+ expect(legacyBot).toMatchObject({ storedSessionId: 'stored-legacy-bot' })
+ expect(legacyBot).not.toHaveProperty('runtimeId')
+ expect(workBot).toMatchObject({ runtimeId: 'runtime-work-live', storedSessionId: 'stored-work-bot' })
+ expect(ordinarySession).not.toHaveProperty('runtimeId')
+ expect(invalidateRuntimeBindings).toHaveBeenCalledWith(new Set(['stored-work-bot']))
+ })
})
describe('SessionTile workspace scope', () => {
diff --git a/apps/desktop/src/store/session-states.ts b/apps/desktop/src/store/session-states.ts
index 704927b685f..fd8961e54a8 100644
--- a/apps/desktop/src/store/session-states.ts
+++ b/apps/desktop/src/store/session-states.ts
@@ -805,22 +805,44 @@ export interface RuntimeReconnectScope {
profile?: null | string
}
-export function resetTileRuntimeBindings(reconnectedScope?: null | string | RuntimeReconnectScope) {
+/** Fallback scope for a restarted connection whose registry identity is
+ * unknown (a legacy remote primary with no connectionId). We cannot name the
+ * dead owner, so instead preserve only Bot runtimes whose owner is provably
+ * alive elsewhere; every other binding is dropped and re-resumes. A reset
+ * only costs a re-resume, so unknown owners fail toward recovery. */
+export interface UnknownRuntimeReconnectScope {
+ liveConnectionIds: ReadonlySet<string>
+}
+
+export function resetTileRuntimeBindings(
+ reconnectedScope?: null | string | RuntimeReconnectScope | UnknownRuntimeReconnectScope
+) {
const tiles = $sessionTiles.get()
+ const liveConnectionIds =
+ reconnectedScope && typeof reconnectedScope === 'object' && 'liveConnectionIds' in reconnectedScope
+ ? reconnectedScope.liveConnectionIds
+ : null
+
const reconnected =
typeof reconnectedScope === 'string'
? { connectionId: reconnectedScope.trim(), profile: null }
- : reconnectedScope
+ : reconnectedScope && !liveConnectionIds
? {
- connectionId: reconnectedScope.connectionId.trim(),
- profile: reconnectedScope.profile?.trim() || null
+ connectionId: (reconnectedScope as RuntimeReconnectScope).connectionId.trim(),
+ profile: (reconnectedScope as RuntimeReconnectScope).profile?.trim() || null
}
: null
const belongsToReconnectedRuntime = (tile: SessionTile): boolean => {
const route = tile.ownerRoute
+ if (liveConnectionIds) {
+ // Unknown restarted identity: a tile survives only when its owner is a
+ // connection we know is still live — anything else rebinds on resume.
+ return !route?.connectionId || !liveConnectionIds.has(route.connectionId)
+ }
+
if (!reconnected?.connectionId || route?.connectionId !== reconnected.connectionId) {
return false
}
@@ -834,7 +856,7 @@ export function resetTileRuntimeBindings(reconnectedScope?: null | string | Runt
tile =>
tile.workspaceMode === 'bots' &&
Boolean(tile.ownerRoute?.connectionId) &&
- (!reconnected || !belongsToReconnectedRuntime(tile))
+ (!(reconnected || liveConnectionIds) || !belongsToReconnectedRuntime(tile))
)
.map(tile => tile.storedSessionId)
)