Skip to content

The agent that grows with you

fix(desktop): keep primary SSH session resumes remote

Oliver Mee61534aac4501
diff --git a/apps/desktop/src/app/contrib/wiring.tsx b/apps/desktop/src/app/contrib/wiring.tsx
index ac006953b58..90d328001d2 100644
--- a/apps/desktop/src/app/contrib/wiring.tsx
+++ b/apps/desktop/src/app/contrib/wiring.tsx
@@ -72,8 +72,10 @@ import {
$selectedStoredSessionId,
$sessionResumeRequest,
$sessions,
+ forgetSessionOwnerHintsForSession,
requestSessionResume,
sessionMatchesStoredId,
+ sessionOwnerRouteFromRow,
sessionPinId,
setAwaitingResponse,
setBusy,
@@ -992,17 +994,18 @@ export function ContribWiring({ children }: { children: ReactNode }) {
// against whichever cached row is found first — the user clicks a row
// previewing profile A and the resume dials profile B. Pin the row's own
// (connection, profile) as the resume owner before navigating; untagged
- // rows (single-profile installs, legacy pages) keep the id-only path.
+ // rows (single-profile installs and the legacy primary-SSH path) keep the
+ // ambient/id-only path. Clear any stale explicit hint first: older builds
+ // incorrectly persisted those rows as `local`, which made a remote session
+ // click switch to the Mac backend and fail with "session not found".
onResumeSession: (sessionId, session) => {
- const rowProfile = session?.profile?.trim()
-
- if (rowProfile) {
- requestSessionResume(sessionId, {
- connectionId: session?.connection_id?.trim() || 'local',
- ...(session?.connection_id?.trim() ? {} : { mode: 'local' as const }),
- profile: rowProfile,
- targetProfile: rowProfile
- })
+ const ownerRoute = sessionOwnerRouteFromRow(session)
+
+ if (ownerRoute) {
+ requestSessionResume(sessionId, ownerRoute)
+ } else {
+ forgetSessionOwnerHintsForSession(sessionId)
+ requestSessionResume(sessionId)
}
openSession(sessionId, navigate)
diff --git a/apps/desktop/src/store/session.test.ts b/apps/desktop/src/store/session.test.ts
index 8b129436f4c..8d2d69a1f2f 100644
--- a/apps/desktop/src/store/session.test.ts
+++ b/apps/desktop/src/store/session.test.ts
@@ -31,10 +31,12 @@ import {
commitWorkspaceCwdForSelectedSession,
ensureDefaultWorkspaceCwd,
forgetSessionOwnerHintsForConnection,
+ forgetSessionOwnerHintsForSession,
getConfiguredDefaultProjectDir,
getRememberedRoute,
getRememberedSessionId,
getSessionOwnerHint,
+ getSessionOwnerHints,
hydrateSessionOwnerHints,
knownSessionOwner,
knownSessionProfile,
@@ -42,6 +44,7 @@ import {
rememberedSessionProfile,
resolveComposerSessionKey,
sessionBelongsToProfile,
+ sessionOwnerRouteFromRow,
sessionPinId,
setCurrentCwd,
setCurrentCwdTransient,
@@ -176,6 +179,30 @@ describe('session owner hints', () => {
expect(getSessionOwnerHint('stored-a')).toBeUndefined()
expect(getSessionOwnerHint('stored-c')).toEqual({ connectionId: 'local', profile: 'omar' })
})
+
+ it('forgets every route for one session without disturbing other sessions', () => {
+ setSessionOwnerHint('poisoned', { connectionId: 'local', mode: 'local', profile: 'default' })
+ setSessionOwnerHint('poisoned', { connectionId: 'remote-a', mode: 'remote', profile: 'default' })
+ setSessionOwnerHint('healthy', { connectionId: 'remote-a', mode: 'remote', profile: 'default' })
+
+ forgetSessionOwnerHintsForSession('poisoned')
+
+ expect(getSessionOwnerHints('poisoned')).toEqual([])
+ expect(getSessionOwnerHint('healthy')).toMatchObject({ connectionId: 'remote-a' })
+
+ _resetSessionOwnerHintsForTests()
+ hydrateSessionOwnerHints()
+ expect(getSessionOwnerHints('poisoned')).toEqual([])
+ expect(getSessionOwnerHint('healthy')).toMatchObject({ connectionId: 'remote-a' })
+ })
+
+ it('pins only connection-tagged rows and leaves primary SSH rows ambient', () => {
+ expect(
+ sessionOwnerRouteFromRow(session({ connection_id: 'source-a', profile: 'worker' }))
+ ).toEqual({ connectionId: 'source-a', profile: 'worker', targetProfile: 'worker' })
+ expect(sessionOwnerRouteFromRow(session({ profile: 'default' }))).toBeUndefined()
+ expect(sessionOwnerRouteFromRow(session({ connection_id: ' ', profile: 'default' }))).toBeUndefined()
+ })
})
describe('knownSessionOwner', () => {
diff --git a/apps/desktop/src/store/session.ts b/apps/desktop/src/store/session.ts
index 7615c19fa5d..620db95cf74 100644
--- a/apps/desktop/src/store/session.ts
+++ b/apps/desktop/src/store/session.ts
@@ -867,6 +867,46 @@ export function forgetSessionOwnerHintsForConnection(connectionId: string): void
}
}
+/** Drop every persisted route for one session. Untagged rows are owned by the
+ * ambient backend that returned them, so a stale explicit hint must not force a
+ * later resume onto a different connection. */
+export function forgetSessionOwnerHintsForSession(sessionId: string): void {
+ const id = sessionId.trim()
+
+ if (!id) {
+ return
+ }
+
+ let changed = false
+
+ for (const [key, entry] of [...sessionOwnerHints]) {
+ if (entry.id === id) {
+ sessionOwnerHints.delete(key)
+ changed = true
+ }
+ }
+
+ if (changed) {
+ persistSessionOwnerHints()
+ }
+}
+
+/** Exact route carried by a connection-tagged row. An untagged row deliberately
+ * returns undefined: it belongs to the ambient backend that supplied the list,
+ * including the legacy primary-SSH path whose rows have no registry id. */
+export function sessionOwnerRouteFromRow(
+ session?: Pick<SessionInfo, 'connection_id' | 'profile'>
+): SessionOwnerRoute | undefined {
+ const connectionId = (session?.connection_id ?? '').trim()
+ const profile = (session?.profile ?? '').trim()
+
+ if (!connectionId || !profile) {
+ return undefined
+ }
+
+ return { connectionId, profile, targetProfile: profile }
+}
+
/** @internal Tests: forget every in-memory hint (storage untouched unless asked). */
export function _resetSessionOwnerHintsForTests({ storage = false }: { storage?: boolean } = {}): void {
sessionOwnerHints.clear()