Skip to content

The agent that grows with you

fix(desktop): validate remembered owner hints against the row, not the foreground socket

Brooklyn Nicholson7abc06df1365
diff --git a/apps/desktop/src/app/contrib/hooks/use-desktop-integrations.test.tsx b/apps/desktop/src/app/contrib/hooks/use-desktop-integrations.test.tsx
index 42a8e0d4c33..cab4f50ca91 100644
--- a/apps/desktop/src/app/contrib/hooks/use-desktop-integrations.test.tsx
+++ b/apps/desktop/src/app/contrib/hooks/use-desktop-integrations.test.tsx
@@ -334,6 +334,40 @@ describe('useDesktopIntegrations', () => {
expect(window.localStorage.getItem('hermes.desktop.lastSessionId.profile.default')).toBe('parent-session')
})
+ // Pins the reachability contract of the by-id resolve branch: the
+ // `sessionBelongsToProfile(sessions, remembered, activeProfile)` gate
+ // above the repair uses the SAME `sessions` array and the SAME
+ // `sessionMatchesStoredId` predicate as `rowFor`, so whenever
+ // `repairOwnerHintsForRestore(remembered)` runs, `rowFor(remembered)`
+ // finds the row — an id absent from the list is cleared before the
+ // repair, it never reaches it (evidence for the #97809 review reply).
+ it('clears an unlisted remembered id instead of restoring it', async () => {
+ window.localStorage.setItem('hermes.desktop.lastSessionId.profile.default', 'unlisted-session')
+ stubGetSession({ connection_id: 'ssh-proxmox', id: 'unlisted-session', profile: 'default' })
+ setSessionOwnerHint('unlisted-session', { connectionId: 'ssh-proxmox', profile: 'default' })
+
+ render({ profileReady: true, sessions: [session({ id: 'other-session', profile: 'default' })] })
+
+ await waitFor(() =>
+ expect(window.localStorage.getItem('hermes.desktop.lastSessionId.profile.default')).toBeNull()
+ )
+ expect(navigate).not.toHaveBeenCalledWith('/unlisted-session', { replace: true })
+ })
+
+ it('drops a legacy local owner hint when auto-restoring an untagged delegate parent (#97809)', async () => {
+ // The delegate child resolves to its parent, and the parent row (listed,
+ // untagged) is what the repair consults: an explicit `local` hint is
+ // stale, exactly as on the click path.
+ window.localStorage.setItem('hermes.desktop.lastSessionId.profile.default', 'delegate-child')
+ stubGetSession({ id: 'delegate-child', parent_session_id: 'parent-session', source: 'subagent' })
+ setSessionOwnerHint('parent-session', { connectionId: 'local', profile: 'default' })
+
+ render({ profileReady: true, sessions: [session({ id: 'parent-session', profile: 'default' })] })
+
+ await waitFor(() => expect(navigate).toHaveBeenCalledWith('/parent-session', { replace: true }))
+ expect(getSessionOwnerHint('parent-session')).toBeUndefined()
+ })
+
it('remembers the parent, never the delegate child, when routed to one', () => {
// A messaging slice can serve the child row, so list membership alone
// must not make it rememberable.
diff --git a/apps/desktop/src/app/session/hooks/use-session-actions.test.tsx b/apps/desktop/src/app/session/hooks/use-session-actions.test.tsx
index 649fc22ce97..05276a3fbb7 100644
--- a/apps/desktop/src/app/session/hooks/use-session-actions.test.tsx
+++ b/apps/desktop/src/app/session/hooks/use-session-actions.test.tsx
@@ -1737,7 +1737,9 @@ describe('resumeSession failure recovery', () => {
clearSessionTodos('runtime-1')
// Persisted owner hints are global module state; the hint-hygiene tests
// below write them and must not leak into later describes' resumes.
- _resetSessionOwnerHintsForTests()
+ // storage: true also clears the persisted copy (cf. the integrations test
+ // file) — this suite's hint writes must not survive into a fresh suite run.
+ _resetSessionOwnerHintsForTests({ storage: true })
// Same for this describe's source-override: mockReset() restores the
// default-preserving spy (the real registry read), unlike
// restoreAllMocks() below, which is a no-op for factory-created vi.fn().
@@ -2628,14 +2630,17 @@ describe('resumeSession failure recovery', () => {
// rows), but every pathname-driven resume (boot auto-restore, reconnect
// re-resume, stranded-view self-heal) funnels through here and used to
// trust the hint verbatim — dialing the Mac backend for a remote session
- // and dying with "session not found". A hint naming a connection that is
- // not this window's live primary is stale by definition and must be
- // dropped, not honored.
- it('drops a legacy local owner hint when the window primary is a remote connection (#97809)', async () => {
- _resetSessionOwnerHintsForTests()
+ // and dying with "session not found". The row is the authority (same
+ // predicate as openStoredSession): a hint that disagrees with a
+ // connection-tagged row is stale by definition and must be dropped, not
+ // honored — repaired in the map too, so the poison does not survive into
+ // the next resume or any session-scoped RPC dispatch.
+ it('drops a legacy local owner hint when the row is untagged (#97809)', async () => {
+ _resetSessionOwnerHintsForTests({ storage: true })
setSessionOwnerHint('stored-1', { connectionId: 'local', profile: 'default' })
- // The window's live primary is the SSH connection, not `local`.
- vi.mocked(activeGatewayConnectionId).mockReturnValue('ssh-proxmox')
+ // The row carries no connection tag: the session belongs to whichever
+ // backend served the list, so an explicit `local` hint is stale.
+ setSessions([storedSession({ id: 'stored-1', profile: 'default' })])
const requestGateway = vi.fn(async (method: string) => {
if (method === 'session.resume') {
@@ -2662,10 +2667,14 @@ describe('resumeSession failure recovery', () => {
expect(getSessionOwnerHint('stored-1')).toBeUndefined()
})
- it('keeps a current owner hint that names the live primary connection', async () => {
- _resetSessionOwnerHintsForTests()
+ it('keeps a current owner hint that agrees with the row\'s connection tag', async () => {
+ _resetSessionOwnerHintsForTests({ storage: true })
setSessionOwnerHint('stored-1', { connectionId: 'ssh-proxmox', profile: 'default' })
- vi.mocked(activeGatewayConnectionId).mockReturnValue('ssh-proxmox')
+ // A connection-tagged row is the authority: the hint naming the same
+ // connection is current and must survive the resume. This is the case
+ // the foreground-socket predicate got wrong — the hint legitimately
+ // names a connection the window is not currently looking at.
+ setSessions([storedSession({ connection_id: 'ssh-proxmox', id: 'stored-1', profile: 'default' })])
const requestGateway = vi.fn(async (method: string) => {
if (method === 'session.resume') {
@@ -2688,6 +2697,34 @@ describe('resumeSession failure recovery', () => {
expect(getSessionOwnerHint('stored-1')).toMatchObject({ connectionId: 'ssh-proxmox' })
})
+
+ it('drops a remembered hint whose connection disagrees with the row\'s tag', async () => {
+ _resetSessionOwnerHintsForTests({ storage: true })
+ // The hint names a different connection than the row: the row wins.
+ setSessionOwnerHint('stored-1', { connectionId: 'ssh-proxmox', profile: 'default' })
+ setSessions([storedSession({ connection_id: 'ssh-vps', id: 'stored-1', profile: 'default' })])
+
+ const requestGateway = vi.fn(async (method: string) => {
+ if (method === 'session.resume') {
+ return {
+ info: {},
+ message_count: 0,
+ messages: [],
+ resumed: 'stored-1',
+ session_id: 'runtime-1',
+ session_key: 'stored-1'
+ } as never
+ }
+
+ return {} as never
+ })
+
+ vi.mocked(getLatestSessionMessages).mockResolvedValue({ messages: [], session_id: 'stored-1' } as never)
+
+ await runResume(requestGateway)
+
+ expect(getSessionOwnerHint('stored-1')).toBeUndefined()
+ })
})
describe('session.resume turn timer contract', () => {
diff --git a/apps/desktop/src/app/session/hooks/use-session-actions/index.ts b/apps/desktop/src/app/session/hooks/use-session-actions/index.ts
index bcb8ec5c8c0..1dabcbf4007 100644
--- a/apps/desktop/src/app/session/hooks/use-session-actions/index.ts
+++ b/apps/desktop/src/app/session/hooks/use-session-actions/index.ts
@@ -39,7 +39,6 @@ import { resetSessionBackground } from '@/store/composer-status'
import { $connectionRequests } from '@/store/connection-request'
import {
$gateway,
- activeGatewayConnectionId,
isActivePrimary,
openGatewayForAgent,
openGatewayForProfile,
@@ -1434,28 +1433,31 @@ export function useSessionActions({
// resolveStoredSession finds the row by id (cheap), so an uncached pasted
// id loads as fast as a sidebar click instead of hanging on a list scan.
//
- // A persisted owner hint is only trustworthy when the connection it
- // names is still this window's live primary. Current builds mint hints
- // from the real registry id at create/resume time, but older builds
- // persisted `local` for rows that actually live on a remote primary
- // (the legacy primary-SSH path): an auto-restored resume that trusts
- // that hint dials the Mac backend and dies with "session not found"
- // (#97809). The click path (openStoredSession) already drops such hints
- // for untagged rows; every OTHER pathname-driven resume funnels through
- // here, so the same hygiene applies at this seam. The hint is repaired,
- // not just ignored: a stale route left in the map re-poisons the next
- // resume, the row ladder and every session-scoped RPC dispatch.
+ // A persisted owner hint is only trustworthy when it agrees with the
+ // best cached row for the session — the same predicate the click path
+ // (openStoredSession) and the boot auto-restore (repairOwnerHintsForRestore)
+ // use, so a session is not repaired on one path and destroyed on
+ // another. Comparing against the live foreground socket (the original
+ // #97809 draft) is wrong in exactly the case the hint exists for: hints are
+ // minted from the AMBIENT connection at create/open time (sdk openSession),
+ // which in the all-profiles view is not the foreground, and resumeSession
+ // itself moves the foreground below — so "names a connection that isn't
+ // active" is true for correct hints. The row is the authority: a
+ // connection-tagged row pins the hint's route (older builds persisted
+ // `local` for rows that actually live on a remote primary — the legacy
+ // #97809 repro), and an untagged or absent row leaves no basis to trust an
+ // explicit persisted hint, so it is dropped rather than dialed.
//
// An explicitly captured owner (requestSessionResume with a row route,
// a plugin open) is authoritative as given; only the REMEMBERED hint
// is validated, never the caller's capture.
const rememberedHint = capturedOwner ? undefined : getSessionOwnerHint(storedSessionId)
+ const rowOwnerRoute = sessionOwnerRouteFromRow(cachedSessionRow(storedSessionId))
- const rememberedOwner = rememberedHint
- ? rememberedHint.connectionId === (activeGatewayConnectionId() ?? 'local')
+ const rememberedOwner =
+ rememberedHint && rowOwnerRoute && rememberedHint.connectionId === rowOwnerRoute.connectionId
? rememberedHint
: undefined
- : undefined
if (rememberedHint && !rememberedOwner) {
forgetSessionOwnerHintsForSession(storedSessionId)