Skip to content

The agent that grows with you

refactor(desktop): make the tile owner ladder testable behavior

Brooklyn Nicholson2659c917c1f2
diff --git a/apps/desktop/src/app/chat/session-tile-owner-route.test.ts b/apps/desktop/src/app/chat/session-tile-owner-route.test.ts
deleted file mode 100644
index 0decaa29946..00000000000
--- a/apps/desktop/src/app/chat/session-tile-owner-route.test.ts
+++ /dev/null
@@ -1,38 +0,0 @@
-import { readFileSync } from 'node:fs'
-import { resolve } from 'node:path'
-
-import { describe, expect, it } from 'vitest'
-
-const source = readFileSync(resolve(process.cwd(), 'src/app/chat/session-tile.tsx'), 'utf8')
-
-describe('SessionTilePane owner-scoped listing', () => {
- it('resolves a newly active tile on its persisted owner route', () => {
- expect(source).toContain('void resolveStoredSession(storedSessionId, ownerRoute)')
- expect(source).not.toMatch(/void resolveStoredSession\(storedSessionId\)\s*\n/)
- })
-})
-
-describe('SessionTileChrome owner ladder', () => {
- // A tile opened without an explicit route (openSessionTile with no
- // workspaceScope — how a branch child is opened) has no tile ownerRoute, so
- // the tile route ALONE leaves ownerRoute undefined and requestForSessionProfile
- // falls back to the ambient socket. The session's own row/hint rung is what
- // keeps its model + composer RPCs on the backend that owns it.
- it('falls back to the session row owner when the tile carries no route', () => {
- expect(source).toContain(
- 'sessionTileOwnerRoute(storedSessionId) ?? knownSessionOwner(ownerLookupSessionRows(), storedSessionId)'
- )
- })
-
- it('does not resolve the chrome owner from the tile route alone', () => {
- // The pre-fix shape: `const ownerRoute = sessionTileOwnerRoute(storedSessionId)`
- // with no fallback rung.
- expect(source).not.toMatch(/const ownerRoute = sessionTileOwnerRoute\(storedSessionId\)\s*\n/)
- })
-
- it('narrows a bare profile owner to an object route', () => {
- // knownSessionOwner may return a bare profile string, which carries no
- // connection and must not be handed to requestForSessionProfile as a route.
- expect(source).toContain("typeof resolvedOwner === 'object'")
- })
-})
diff --git a/apps/desktop/src/app/chat/session-tile-owner.test.ts b/apps/desktop/src/app/chat/session-tile-owner.test.ts
new file mode 100644
index 00000000000..57df6228356
--- /dev/null
+++ b/apps/desktop/src/app/chat/session-tile-owner.test.ts
@@ -0,0 +1,70 @@
+import { beforeEach, describe, expect, it } from 'vitest'
+
+import { _resetSessionOwnerHintsForTests, setSessionOwnerHint } from '@/store/session'
+import type { SessionTile } from '@/store/session-states'
+import type { SessionInfo } from '@/types/hermes'
+
+import { tileOwnerRoute } from './session-tile-owner'
+
+const row = (over: Partial<SessionInfo>): SessionInfo => over as SessionInfo
+
+const tile = (over: Partial<SessionTile> & Pick<SessionTile, 'storedSessionId'>): SessionTile => over as SessionTile
+
+describe('tileOwnerRoute', () => {
+ beforeEach(() => {
+ _resetSessionOwnerHintsForTests()
+ })
+
+ it('prefers the tile own explicit route', () => {
+ const route = tileOwnerRoute(
+ [tile({ ownerRoute: { connectionId: 'pandora', profile: 'work' }, storedSessionId: 's1' })],
+ [row({ connection_id: 'other-box', id: 's1', profile: 'default' })],
+ 's1'
+ )
+
+ expect(route).toEqual({ connectionId: 'pandora', profile: 'work' })
+ })
+
+ it('falls back to the session row owner when the tile carries no route', () => {
+ // How a branch child is opened: openSessionTile with no workspaceScope, so
+ // the tile route alone leaves the owner undefined and every RPC drops to
+ // the ambient socket.
+ const route = tileOwnerRoute(
+ [tile({ storedSessionId: 's1' })],
+ [row({ connection_id: 'rigremote', id: 's1', profile: 'default' })],
+ 's1'
+ )
+
+ expect(route).toEqual({ connectionId: 'rigremote', profile: 'default' })
+ })
+
+ it('falls back to the owner hint when neither tile nor row is tagged', () => {
+ setSessionOwnerHint('s1', { connectionId: 'pandora', profile: 'work' })
+
+ expect(tileOwnerRoute([tile({ storedSessionId: 's1' })], [], 's1')).toMatchObject({ connectionId: 'pandora' })
+ })
+
+ it('carries a targetProfile through, and omits it when absent', () => {
+ const routed = tileOwnerRoute(
+ [tile({ ownerRoute: { connectionId: 'pandora', profile: 'work', targetProfile: 'ceo' }, storedSessionId: 's1' })],
+ [],
+ 's1'
+ )
+
+ expect(routed).toEqual({ connectionId: 'pandora', profile: 'work', targetProfile: 'ceo' })
+ expect(tileOwnerRoute([tile({ ownerRoute: { connectionId: 'p', profile: 'w' }, storedSessionId: 's1' })], [], 's1'))
+ .not.toHaveProperty('targetProfile')
+ })
+
+ it('narrows a bare profile owner away', () => {
+ // knownSessionOwner returns a bare profile string for a row that names a
+ // profile but no connection. It carries no backend identity, so handing it
+ // on as a route would resolve against whichever connection is active.
+ expect(tileOwnerRoute([], [row({ id: 's1', profile: 'work' })], 's1')).toBeUndefined()
+ })
+
+ it('is undefined for an untagged session, preserving ambient routing', () => {
+ expect(tileOwnerRoute([], [row({ id: 's1' })], 's1')).toBeUndefined()
+ expect(tileOwnerRoute([], [], 'missing')).toBeUndefined()
+ })
+})
diff --git a/apps/desktop/src/app/chat/session-tile-owner.ts b/apps/desktop/src/app/chat/session-tile-owner.ts
new file mode 100644
index 00000000000..e99a3df8968
--- /dev/null
+++ b/apps/desktop/src/app/chat/session-tile-owner.ts
@@ -0,0 +1,38 @@
+import { knownSessionOwner } from '@/store/session'
+import type { SessionOwnerRoute, SessionOwnerScope } from '@/store/session-request-router'
+import type { SessionTile } from '@/store/session-states'
+import type { SessionInfo } from '@/types/hermes'
+
+/**
+ * The owner a session tile routes its own RPCs through — the tile's explicit
+ * route first, then the session row's `(connection, profile)` tag, with
+ * `knownSessionOwner` folding in the owner hint.
+ *
+ * A tile opened without an explicit route — a branch child, which
+ * `openSessionTile` creates with no `workspaceScope` — has no tile route, so
+ * the row/hint rung is the only thing keeping its model and composer RPCs on
+ * the backend that owns the session instead of the ambient one.
+ *
+ * A bare profile string carries no connection and is not a usable route:
+ * handing it to `requestForSessionProfile` would resolve it against whichever
+ * connection is active, which is the bug this ladder exists to avoid.
+ */
+export function tileOwnerRoute(
+ tiles: readonly SessionTile[],
+ rows: readonly SessionInfo[],
+ storedSessionId: string
+): SessionOwnerRoute | undefined {
+ const owner: SessionOwnerScope =
+ tiles.find(tile => tile.storedSessionId === storedSessionId)?.ownerRoute ??
+ knownSessionOwner(rows, storedSessionId)
+
+ if (!owner || typeof owner !== 'object' || !owner.connectionId) {
+ return undefined
+ }
+
+ return {
+ connectionId: owner.connectionId,
+ profile: owner.profile,
+ ...(owner.targetProfile ? { targetProfile: owner.targetProfile } : {})
+ }
+}
diff --git a/apps/desktop/src/app/chat/session-tile.tsx b/apps/desktop/src/app/chat/session-tile.tsx
index 7f25ac24db7..35f4bdad9a1 100644
--- a/apps/desktop/src/app/chat/session-tile.tsx
+++ b/apps/desktop/src/app/chat/session-tile.tsx
@@ -46,11 +46,10 @@ import {
$messagingSessions,
$selectedStoredSessionId,
$sessions,
- knownSessionOwner,
sessionMatchesStoredId,
sessionPinId
} from '@/store/session'
-import { requestForSessionProfile, type SessionOwnerRoute } from '@/store/session-request-router'
+import { requestForSessionProfile } from '@/store/session-request-router'
import {
$sessionStates,
$sessionTileDelegateRevision,
@@ -70,6 +69,7 @@ import { SessionDraftTitle } from './session-draft-title'
import { startSessionDrag } from './session-drag'
import { SessionStatusDot } from './session-status-dot'
import { useSessionTileActions } from './session-tile-actions'
+import { tileOwnerRoute } from './session-tile-owner'
import { type SessionView, SessionViewProvider } from './session-view'
import { SessionContextMenu } from './sidebar/session-actions-menu'
import { lastVisibleMessageIsUser } from './thread-loading'
@@ -160,50 +160,21 @@ function TileChat({
const { gateway, requestGateway } = useGatewayRequest()
const queryClient = useQueryClient()
- // Owner ladder, same as useSessionTileActions (session-tile-actions.ts:99-103):
- // this tile's explicit route first, then the session row's own
- // (connection, profile) tag — knownSessionOwner also folds in the owner hint.
- // A tile opened without an explicit route — e.g. a branch child, which
- // openSessionTile creates with no workspaceScope — has no tile route, so the
- // row/hint rung is the only thing keeping this tile's model + composer RPCs
- // on the backend that owns the session instead of the ambient one.
- //
+ // Owner ladder, same as useSessionTileActions (session-tile-actions.ts:99-103).
// Recomputed when the tile store or any owner-bearing session list changes,
// NOT on every render: this component re-renders per streamed token, and the
- // lookup spreads three arrays before scanning them. Only the resulting
- // IDENTITY is memoised downstream, on primitives, because knownSessionOwner
- // mints a fresh object per call and requestTileGateway is keyed on it.
+ // lookup spreads three arrays before scanning them.
const tiles = useStore($sessionTiles)
const sessionRows = useStore($sessions)
const cronRows = useStore($cronSessions)
const messagingRows = useStore($messagingSessions)
- const resolvedOwner = useMemo(() => {
+ const ownerRoute = useMemo(() => {
const rows = cronRows.length || messagingRows.length ? [...sessionRows, ...cronRows, ...messagingRows] : sessionRows
- return (
- tiles.find(tile => tile.storedSessionId === storedSessionId)?.ownerRoute ??
- knownSessionOwner(rows, storedSessionId)
- )
+ return tileOwnerRoute(tiles, rows, storedSessionId)
}, [cronRows, messagingRows, sessionRows, storedSessionId, tiles])
- const ownerConnectionId = resolvedOwner && typeof resolvedOwner === 'object' ? resolvedOwner.connectionId : ''
- const ownerProfile = resolvedOwner && typeof resolvedOwner === 'object' ? resolvedOwner.profile : ''
- const ownerTargetProfile = resolvedOwner && typeof resolvedOwner === 'object' ? resolvedOwner.targetProfile : undefined
-
- // A bare profile string carries no connection and is not a usable route here.
- const ownerRoute = useMemo<SessionOwnerRoute | undefined>(
- () =>
- ownerConnectionId
- ? {
- connectionId: ownerConnectionId,
- profile: ownerProfile,
- ...(ownerTargetProfile ? { targetProfile: ownerTargetProfile } : {})
- }
- : undefined,
- [ownerConnectionId, ownerProfile, ownerTargetProfile]
- )
-
const requestTileGateway = useCallback(
<T,>(method: string, params?: Record<string, unknown>, timeoutMs?: number, signal?: AbortSignal): Promise<T> =>
requestForSessionProfile<T>(ownerRoute, requestGateway, method, params, timeoutMs, signal),