Skip to content

The agent that grows with you

fix(desktop): a resume keeps the owner hint of a session with no row

Tekniumc5b6b68680fe
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 1dabcbf4007..d60e31454d0 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
@@ -83,9 +83,7 @@ import {
$newChatWorkspaceTarget,
$sessions,
$yoloActive,
- forgetSessionOwnerHintsForSession,
getCurrentModelSource,
- getSessionOwnerHint,
idsShareLineage,
type NewChatWorkspaceTarget,
resolveComposerSessionKey,
@@ -169,6 +167,7 @@ import { markSessionCreatedThisRun, sessionCreatedThisRun } from './created-this
import { captureDisplayHydration } from './display-hydration'
import { reconcilePersistedLiveTurn } from './persisted-live-turn'
import { provisionalTranscriptPaint, transcriptRestScope } from './provisional-transcript'
+import { rememberedOwnerForResume } from './remembered-owner'
import { pendingClarifyToolPayload, restorePendingClarifyFromSnapshot } from './restore-pending-clarify'
import { projectPendingConnection, restorePendingConnectionFromSnapshot } from './restore-pending-connection'
import { createGatewaySession } from './session-create-request'
@@ -1433,35 +1432,9 @@ 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 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 && rowOwnerRoute && rememberedHint.connectionId === rowOwnerRoute.connectionId
- ? rememberedHint
- : undefined
-
- if (rememberedHint && !rememberedOwner) {
- forgetSessionOwnerHintsForSession(storedSessionId)
- }
+ // Only the REMEMBERED hint is validated (remembered-owner.ts); an explicitly captured owner
+ // (requestSessionResume with a row route, a plugin open) is authoritative as given.
+ const rememberedOwner = capturedOwner ? undefined : rememberedOwnerForResume(storedSessionId)
// An explicit capture outranks the remembered hint; the hint only
// fills in when the caller had no route to give.
diff --git a/apps/desktop/src/app/session/hooks/use-session-actions/remembered-owner.test.ts b/apps/desktop/src/app/session/hooks/use-session-actions/remembered-owner.test.ts
new file mode 100644
index 00000000000..919b509e8d1
--- /dev/null
+++ b/apps/desktop/src/app/session/hooks/use-session-actions/remembered-owner.test.ts
@@ -0,0 +1,40 @@
+import { afterEach, beforeEach, describe, expect, it } from 'vitest'
+
+import {
+ $sessions,
+ _resetSessionOwnerHintsForTests,
+ getSessionOwnerHint,
+ setSessionOwnerHint
+} from '@/store/session'
+import type { SessionInfo } from '@/types/hermes'
+
+import { rememberedOwnerForResume } from './remembered-owner'
+
+const REMOTE = { connectionId: 'ssh-proxmox', profile: 'bot' }
+
+describe('rememberedOwnerForResume', () => {
+ beforeEach(() => {
+ _resetSessionOwnerHintsForTests({ storage: true })
+ $sessions.set([])
+ })
+
+ afterEach(() => {
+ _resetSessionOwnerHintsForTests({ storage: true })
+ $sessions.set([])
+ })
+
+ it('keeps the hint of a session with no row (hidden Bot Chat)', () => {
+ setSessionOwnerHint('stored-1', REMOTE)
+
+ expect(rememberedOwnerForResume('stored-1')).toMatchObject(REMOTE)
+ expect(getSessionOwnerHint('stored-1')).toMatchObject(REMOTE)
+ })
+
+ it('drops a hint an untagged row contradicts (#97809)', () => {
+ setSessionOwnerHint('stored-1', REMOTE)
+ $sessions.set([{ id: 'stored-1' } as SessionInfo])
+
+ expect(rememberedOwnerForResume('stored-1')).toBeUndefined()
+ expect(getSessionOwnerHint('stored-1')).toBeUndefined()
+ })
+})
diff --git a/apps/desktop/src/app/session/hooks/use-session-actions/remembered-owner.ts b/apps/desktop/src/app/session/hooks/use-session-actions/remembered-owner.ts
new file mode 100644
index 00000000000..b762c33b8b5
--- /dev/null
+++ b/apps/desktop/src/app/session/hooks/use-session-actions/remembered-owner.ts
@@ -0,0 +1,36 @@
+import { forgetSessionOwnerHintsForSession, getSessionOwnerHint } from '@/store/session'
+import { type SessionOwnerRoute, sessionOwnerRouteFromRow } from '@/store/session-request-router'
+
+import { cachedSessionRow } from './utils'
+
+/**
+ * The persisted owner hint a pathname-driven resume may still trust, repairing a stale one in
+ * the map so it cannot re-poison the next resume or a session-scoped RPC dispatch.
+ *
+ * The cached row is the authority, with the same predicate as the click path (openStoredSession)
+ * and the boot auto-restore (repairOwnerHintsForRestore). Comparing against the live foreground
+ * socket is wrong: hints are minted from the ambient connection at open time, and resumeSession
+ * itself moves the foreground. A connection-tagged row that agrees keeps the hint. An untagged or
+ * disagreeing row drops it: older builds persisted `local` for rows that live on a remote primary
+ * (#97809). No row at all keeps it: hidden sessions (Bot Chat, plugin opens) never get a sidebar
+ * row, and for them the hint is the only owner record (sdk openSession). Dropping it sent a remote
+ * bot's resume to the local primary.
+ */
+export function rememberedOwnerForResume(storedSessionId: string): SessionOwnerRoute | undefined {
+ const hint = getSessionOwnerHint(storedSessionId)
+
+ if (!hint) {
+ return undefined
+ }
+
+ const row = cachedSessionRow(storedSessionId)
+ const rowRoute = sessionOwnerRouteFromRow(row)
+
+ if (!row || (rowRoute && hint.connectionId === rowRoute.connectionId)) {
+ return hint
+ }
+
+ forgetSessionOwnerHintsForSession(storedSessionId)
+
+ return undefined
+}