Skip to content

The agent that grows with you

38.5 KB
Raw Blame
import { renderHook } from '@testing-library/react'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'

import { reasoningEffortPending } from '@/app/chat/session-view'
import type { ClientSessionState } from '@/app/types'
import type * as HermesModule from '@/hermes'
import { textPart } from '@/lib/chat-messages'
import { createClientSessionState } from '@/lib/chat-runtime'
import { clearSessionDraft, stashSessionDraft, takeSessionDraft } from '@/store/composer'
import { $notifications } from '@/store/notifications'
import { $cronRunReadOnlyVerdicts, recordCronRunVerdict } from '@/store/read-only-transcript'
import { $activeSessionId, setSessionOwnerHint, setSessions } from '@/store/session'
import { $sessionTiles, sessionTileDelegate } from '@/store/session-states'
import type { SessionInfo } from '@/types/hermes'

import {
  clearSingleFlightSessionResumeState
} from '../../session/hooks/use-prompt-actions/single-flight-resume'

import { useSessionTileDelegate } from './use-session-tile-delegate'

vi.mock('@/hermes', async importActual => ({
  ...(await importActual<typeof HermesModule>()),
  getLatestSessionMessages: vi.fn(async () => ({ messages: [], session_id: '' })),
  getSession: vi.fn()
}))
vi.mock('@/store/confirm', () => ({ confirm: vi.fn(async () => false) }))
vi.mock('@/store/gateway', async importActual => ({
  ...(await importActual<Record<string, unknown>>()),
  requestGatewayForAgent: vi.fn(),
  requestGatewayForProfile: vi.fn()
}))

const { getLatestSessionMessages, getSession, PROMPT_SUBMIT_REQUEST_TIMEOUT_MS } = await import('@/hermes')
const { requestGatewayForAgent, requestGatewayForProfile } = await import('@/store/gateway')
const { confirm } = await import('@/store/confirm')

const row = (over: Partial<SessionInfo>): SessionInfo =>
  ({
    ended_at: null,
    id: 'live',
    input_tokens: 0,
    is_active: false,
    last_active: 0,
    message_count: 1,
    model: null,
    output_tokens: 0,
    preview: null,
    profile: 'default',
    source: null,
    started_at: 0,
    title: null,
    ...over
  }) as SessionInfo

function renderTile(
  requestGateway: ReturnType<typeof vi.fn>,
  options: {
    branchLoadedSession?: ReturnType<typeof vi.fn>
    runtimeIdByStoredSessionIdRef?: { current: Map<string, string> }
    sessionStateByRuntimeIdRef?: { current: Map<string, unknown> }
    updateSessionState?: ReturnType<typeof vi.fn>
  } = {}
) {
  renderHook(() =>
    useSessionTileDelegate({
      archiveSession: vi.fn(async () => undefined),
      branchLoadedSession: (options.branchLoadedSession ?? vi.fn(async () => false)) as never,
      branchStoredSession: vi.fn(async () => undefined),
      executeSlashCommand: vi.fn(async () => undefined) as never,
      removeSession: vi.fn(async () => undefined),
      requestGateway: requestGateway as never,
      runtimeIdByStoredSessionIdRef: (options.runtimeIdByStoredSessionIdRef ?? { current: new Map() }) as never,
      sessionStateByRuntimeIdRef: (options.sessionStateByRuntimeIdRef ?? { current: new Map() }) as never,
      updateSessionState: (options.updateSessionState ?? vi.fn()) as never
    })
  )
}

describe('useSessionTileDelegate resumeTile', () => {
  beforeEach(() => {
    clearSingleFlightSessionResumeState()
    setSessions([])
    vi.mocked(getLatestSessionMessages).mockClear()
  })

  afterEach(() => {
    clearSingleFlightSessionResumeState()
    setSessions([])
  })

  it('carries the owning profile into a cold tile resume so it cannot fork profiles', async () => {
    // A tile opens a session owned by another profile. Resuming without the
    // profile lets the gateway fall back to the launch-profile DB and clone the
    // conversation into the wrong profile (#67603). The owning profile must ride
    // both the transcript prefetch and the resume RPC.
    setSessions([row({ id: 'stored-x', profile: 'ai-engineer' })])

    const requestGateway = vi.fn(async (method: string) =>
      method === 'session.resume' ? ({ session_id: 'runtime-1' } as never) : ({} as never)
    )

    vi.mocked(requestGatewayForProfile).mockResolvedValueOnce({ session_id: 'runtime-1' } as never)

    renderTile(requestGateway)
    const runtimeId = await sessionTileDelegate()!.resumeTile('stored-x')

    expect(runtimeId).toBe('runtime-1')
    expect(getLatestSessionMessages).toHaveBeenCalledWith('stored-x', 'ai-engineer')
    expect(requestGatewayForProfile).toHaveBeenCalledWith(
      'ai-engineer',
      'session.resume',
      {
        session_id: 'stored-x',
        cols: 96,
        profile: 'ai-engineer',
        omit_messages: true
      },
      undefined,
      undefined
    )
    expect(requestGateway).not.toHaveBeenCalled()
  })

  it('resolves and carries a default-profile session explicitly', async () => {
    setSessions([row({ id: 'stored-y', profile: 'default' })])

    const requestGateway = vi.fn(async () => ({}) as never)

    // #92961: a known owner is ALWAYS routed through the profile router —
    // even 'default' — never dispatched on the ambient socket.
    vi.mocked(requestGatewayForProfile).mockResolvedValueOnce({ session_id: 'runtime-2' } as never)

    renderTile(requestGateway)
    const runtimeId = await sessionTileDelegate()!.resumeTile('stored-y')

    expect(runtimeId).toBe('runtime-2')
    expect(requestGatewayForProfile).toHaveBeenCalledWith(
      'default',
      'session.resume',
      {
        session_id: 'stored-y',
        cols: 96,
        profile: 'default',
        omit_messages: true
      },
      undefined,
      undefined
    )
    expect(requestGateway).not.toHaveBeenCalled()
  })

  it('carries a session row connection owner into a same-named tile resume', async () => {
    setSessions([row({ connection_id: 'source-b', id: 'stored-shared', profile: 'default' })])

    const ambientRequest = vi.fn(async () => ({}) as never)
    vi.mocked(requestGatewayForAgent).mockResolvedValueOnce({ session_id: 'runtime-shared' } as never)

    renderTile(ambientRequest)
    const runtimeId = await sessionTileDelegate()!.resumeTile('stored-shared')

    expect(runtimeId).toBe('runtime-shared')
    expect(requestGatewayForAgent).toHaveBeenCalledWith('source-b', 'default', 'session.resume', {
      session_id: 'stored-shared',
      cols: 96,
      omit_messages: true,
      profile: 'default'
    })
    expect(ambientRequest).not.toHaveBeenCalled()
  })

  it('routes a Bot tile prefetch and resume through its exact connection owner', async () => {
    const route = {
      connectionId: 'barry',
      mode: 'remote' as const,
      profile: 'oxcoder',
      targetProfile: 'backend-oxcoder'
    }

    setSessionOwnerHint('stored-remote', route)
    vi.mocked(requestGatewayForAgent).mockResolvedValueOnce({ session_id: 'runtime-remote' } as never)
    const ambientRequest = vi.fn(async () => ({}) as never)

    renderTile(ambientRequest)
    const runtimeId = await sessionTileDelegate()!.resumeTile('stored-remote')

    expect(runtimeId).toBe('runtime-remote')
    expect(getLatestSessionMessages).toHaveBeenCalledWith('stored-remote', {
      connectionId: 'barry',
      profile: 'backend-oxcoder'
    })
    expect(requestGatewayForAgent).toHaveBeenCalledWith('barry', 'oxcoder', 'session.resume', {
      session_id: 'stored-remote',
      cols: 96,
      omit_messages: true,
      profile: 'backend-oxcoder'
    })
    expect(ambientRequest).not.toHaveBeenCalled()
  })

  it('reuses a warm binding that still carries a transcript', async () => {
    const stateA = { busy: false, messages: [{ id: 'm1' }], storedSessionId: 'stored-a' }
    const runtimeIdByStoredSessionIdRef = { current: new Map([['stored-a', 'runtime-a']]) }
    const sessionStateByRuntimeIdRef = { current: new Map([['runtime-a', stateA]]) }
    const requestGateway = vi.fn(async () => ({}) as never)

    renderTile(requestGateway, { runtimeIdByStoredSessionIdRef, sessionStateByRuntimeIdRef })
    const runtimeId = await sessionTileDelegate()!.resumeTile('stored-a')

    expect(runtimeId).toBe('runtime-a')
    expect(requestGateway).not.toHaveBeenCalled()
    expect(getLatestSessionMessages).not.toHaveBeenCalled()
  })

  it('forces an owner-routed message snapshot into a warm tile without touching foreground state', async () => {
    const ownerRoute = { connectionId: 'remote-tile', profile: 'tile-profile', targetProfile: 'backend-tile' }

    const pending = {
      id: 'user-pending',
      parts: [{ type: 'text', text: 'local pending prompt' }],
      pending: true,
      role: 'user'
    }

    const warm = { busy: false, messages: [pending], storedSessionId: 'stored-tile' }
    const runtimeIdByStoredSessionIdRef = { current: new Map([['stored-tile', 'runtime-tile']]) }
    const states = { current: new Map([['runtime-tile', warm]]) }

    const update = vi.fn((id, updater) => {
      const next = updater(states.current.get(id))
      states.current.set(id, next)

      return next
    })

    $activeSessionId.set('runtime-foreground')
    $sessionTiles.set([{ ownerRoute, runtimeId: 'runtime-tile', storedSessionId: 'stored-tile' }] as never)
    stashSessionDraft('stored-tile', 'unsent tile draft', [])
    vi.mocked(requestGatewayForAgent).mockResolvedValueOnce({
      info: { model: 'snapshot-model' },
      message_count: 2,
      messages: [
        { content: 'new prompt', role: 'user', timestamp: 1 },
        { content: 'new answer', role: 'assistant', timestamp: 2 }
      ],
      resumed: 'stored-tile',
      running: true,
      session_id: 'runtime-tile'
    } as never)

    renderTile(vi.fn(), {
      runtimeIdByStoredSessionIdRef,
      sessionStateByRuntimeIdRef: states,
      updateSessionState: update
    })

    await sessionTileDelegate()!.resumeTile('stored-tile', { authoritativeSnapshot: true })

    expect(requestGatewayForAgent).toHaveBeenCalledWith('remote-tile', 'tile-profile', 'session.resume', {
      cols: 96,
      omit_messages: false,
      profile: 'backend-tile',
      session_id: 'stored-tile'
    })
    expect(getLatestSessionMessages).not.toHaveBeenCalled()
    expect(states.current.get('runtime-tile')).toMatchObject({ busy: true, model: 'snapshot-model' })
    expect(JSON.stringify(states.current.get('runtime-tile')?.messages)).toContain('new answer')
    expect(states.current.get('runtime-tile')?.messages).toContainEqual(pending)
    expect($activeSessionId.get()).toBe('runtime-foreground')
    expect(takeSessionDraft('stored-tile').text).toBe('unsent tile draft')

    clearSessionDraft('stored-tile')
    $activeSessionId.set(null)
    $sessionTiles.set([])
  })

  it('merges persisted messages into a warm tile on explicit reopen (#96183)', async () => {
    const stateA = {
      busy: false,
      messages: [{ id: 'm1', parts: [{ type: 'text', text: 'old' }], role: 'user' }],
      storedSessionId: 'stored-a'
    }

    const runtimeIdByStoredSessionIdRef = { current: new Map([['stored-a', 'runtime-a']]) }
    const sessionStateByRuntimeIdRef = { current: new Map([['runtime-a', stateA]]) }
    const updateSessionState = vi.fn((_id, updater) => updater(stateA))
    const requestGateway = vi.fn(async () => ({}) as never)

    vi.mocked(getLatestSessionMessages).mockResolvedValueOnce({
      messages: [
        { id: 'm1', content: 'old', role: 'user' },
        { id: 'm2', content: 'cron delivery', role: 'user' }
      ],
      session_id: 'stored-a'
    } as never)

    renderTile(requestGateway, { runtimeIdByStoredSessionIdRef, sessionStateByRuntimeIdRef, updateSessionState })
    const runtimeId = await sessionTileDelegate()!.resumeTile('stored-a', { refreshTranscript: true })

    expect(runtimeId).toBe('runtime-a')
    expect(requestGateway).not.toHaveBeenCalled()
    expect(getLatestSessionMessages).toHaveBeenCalled()
    expect(updateSessionState).toHaveBeenCalled()

    const updater = updateSessionState.mock.calls[0][1] as (state: typeof stateA) => {
      messages: Array<{ parts?: Array<{ text?: string }> }>
    }

    const next = updater(stateA)
    const texts = next.messages.flatMap(message => (message.parts ?? []).map(part => part.text ?? ''))

    expect(texts.some(text => text.includes('cron delivery'))).toBe(true)
  })

  it('keeps a completed tool card when a Bot Chat tile refreshes its final reply', async () => {
    const state = {
      busy: false,
      storedSessionId: 'stored-bot',
      streamId: null,
      messages: [
        { id: 'prompt', rowId: 10, role: 'user', parts: [{ type: 'text', text: 'Find the answer' }] },
        {
          id: 'tool-stream',
          role: 'assistant',
          interim: true,
          pending: false,
          parts: [
            {
              type: 'tool-call',
              toolCallId: 'search-1',
              toolName: 'web_search',
              args: { query: 'answer' },
              result: 'Found it',
              completedAt: 2
            }
          ]
        },
        {
          id: 'reply',
          rowId: 13,
          role: 'assistant',
          pending: false,
          parts: [{ type: 'text', text: 'The answer is here.' }]
        }
      ]
    } as ClientSessionState

    const states = { current: new Map([['runtime-bot', state]]) }

    const update = vi.fn((_id, updater) => {
      const next = updater(states.current.get(_id))
      states.current.set(_id, next)

      return next
    })

    setSessions([row({ id: 'stored-bot', profile: 'bot', title: 'Bot Chat' })])
    vi.mocked(getLatestSessionMessages).mockResolvedValueOnce({
      session_id: 'stored-bot',
      messages: [
        { role: 'user', row_id: 10, content: 'Find the answer', timestamp: 1 },
        { role: 'assistant', row_id: 13, content: 'The answer is here.', timestamp: 3 }
      ]
    } as never)
    renderTile(vi.fn(), {
      runtimeIdByStoredSessionIdRef: { current: new Map([['stored-bot', 'runtime-bot']]) },
      sessionStateByRuntimeIdRef: states,
      updateSessionState: update
    })

    await sessionTileDelegate()!.resumeTile('stored-bot', { refreshTranscript: true })

    expect(
      states.current
        .get('runtime-bot')!
        .messages.flatMap(message => message.parts)
        .filter(part => part.type === 'tool-call')
    ).toMatchObject([{ toolCallId: 'search-1', result: 'Found it' }])

    // The same reply text on another durable row must not inherit this tool.
    states.current.set('runtime-bot', state)
    vi.mocked(getLatestSessionMessages).mockResolvedValueOnce({
      session_id: 'stored-bot',
      messages: [
        { role: 'user', row_id: 10, content: 'Find the answer', timestamp: 1 },
        { role: 'assistant', row_id: 14, content: 'The answer is here.', timestamp: 4 }
      ]
    } as never)

    await sessionTileDelegate()!.resumeTile('stored-bot', { refreshTranscript: true })

    expect(
      states.current
        .get('runtime-bot')!
        .messages.flatMap(message => message.parts)
        .filter(part => part.type === 'tool-call')
    ).toEqual([])
  })

  it('refreshes a retained live tile even when the reverse lookup is absent', async () => {
    const state = {
      busy: true,
      streamId: 'assistant-stream-live',
      storedSessionId: 'stored-retained',
      messages: [
        { id: 'old-user', role: 'user', parts: [{ type: 'text', text: 'earlier prompt' }] },
        { id: 'old-assistant', role: 'assistant', parts: [{ type: 'text', text: 'Hello earlier answer' }] },
        { id: 'user-live', role: 'user', parts: [{ type: 'text', text: 'prompt' }] },
        { id: 'assistant-stream-live', role: 'assistant', pending: true, parts: [{ type: 'text', text: 'Hello' }] }
      ]
    }

    const states = { current: new Map([['runtime-retained', state]]) }

    const update = vi.fn((_id, updater) => {
      const next = updater(states.current.get(_id))
      states.current.set(_id, next)

      return next
    })

    setSessions([row({ id: 'stored-retained', profile: 'default' })])
    $sessionTiles.set([{ storedSessionId: 'stored-retained', runtimeId: 'runtime-retained' }] as never)
    vi.mocked(getLatestSessionMessages).mockResolvedValueOnce({
      session_id: 'stored-retained',
      messages: [
        { role: 'user', content: 'earlier prompt', timestamp: 0.5 },
        { role: 'assistant', content: 'Hello earlier answer', timestamp: 0.6 },
        { role: 'user', content: 'prompt', timestamp: 1 },
        { role: 'system', content: 'external notice', timestamp: 2 }
      ]
    } as never)
    vi.mocked(requestGatewayForProfile).mockResolvedValueOnce({
      session_id: 'runtime-retained',
      info: { running: true }
    } as never)
    const request = vi.fn()
    renderTile(request, { sessionStateByRuntimeIdRef: states, updateSessionState: update })

    try {
      expect(await sessionTileDelegate()!.resumeTile('stored-retained', { refreshTranscript: true })).toBe(
        'runtime-retained'
      )
      const refreshed = states.current.get('runtime-retained')!
      expect(JSON.stringify(refreshed.messages)).toContain('external notice')
      expect(refreshed.messages.find(message => message.id === state.streamId)).toEqual(state.messages[3])
      expect(refreshed.busy).toBe(true)

      // REST may finish before the next WS delta; keep one, fuller answer.
      states.current.set('runtime-retained', state)
      vi.mocked(getLatestSessionMessages).mockResolvedValueOnce({
        session_id: 'stored-retained',
        messages: [
          { role: 'user', content: 'earlier prompt', timestamp: 0.5 },
          { role: 'assistant', content: 'Hello earlier answer', timestamp: 0.6 },
          { role: 'user', content: 'prompt', timestamp: 1 },
          { role: 'assistant', content: 'Hello world', timestamp: 2 }
        ]
      } as never)
      await sessionTileDelegate()!.resumeTile('stored-retained', { refreshTranscript: true })
      const answers = states.current.get('runtime-retained')!.messages.filter(message => message.role === 'assistant')
      expect(answers.map(message => message.parts.map(part => part.text).join(''))).toEqual([
        'Hello earlier answer',
        'Hello world'
      ])
    } finally {
      vi.mocked(requestGatewayForProfile).mockReset()
      $sessionTiles.set([])
    }
  })

  it('merges delayed refreshes against the latest streaming and completed state', async () => {
    const initial = {
      busy: true,
      streamId: 'assistant-stream-live',
      storedSessionId: 'stored-delay',
      messages: [
        { id: 'user-live', role: 'user', parts: [{ type: 'text', text: 'prompt' }] },
        { id: 'assistant-stream-live', role: 'assistant', pending: true, parts: [{ type: 'text', text: 'Hello' }] }
      ]
    }

    const states = { current: new Map([['runtime-delay', initial]]) }

    const update = vi.fn((_id, updater) => {
      const next = updater(states.current.get(_id))
      states.current.set(_id, next)

      return next
    })

    renderTile(vi.fn(), {
      runtimeIdByStoredSessionIdRef: { current: new Map([['stored-delay', 'runtime-delay']]) },
      sessionStateByRuntimeIdRef: states,
      updateSessionState: update
    })

    for (const scenario of ['streaming', 'completed', 'compacted']) {
      const pending = scenario === 'streaming'
      states.current.set('runtime-delay', initial)
      let release!: (value: never) => void
      let started!: () => void

      const fetching = new Promise<void>(resolve => {
        started = resolve
      })

      vi.mocked(getLatestSessionMessages).mockImplementationOnce(() => {
        started()

        return new Promise(resolve => {
          release = resolve
        })
      })
      const refreshing = sessionTileDelegate()!.resumeTile('stored-delay', { refreshTranscript: true })
      await fetching

      const current = {
        ...initial,
        busy: pending,
        streamId: pending ? initial.streamId : null,
        messages: [
          ...(scenario === 'compacted'
            ? [{ id: 'old-answer', role: 'assistant', parts: [{ type: 'text', text: 'Hello earlier answer' }] }]
            : []),
          initial.messages[0],
          {
            ...initial.messages[1],
            pending,
            parts: [{ type: 'text', text: pending ? 'Hello newer delta' : 'Hello completed' }]
          }
        ]
      }

      states.current.set('runtime-delay', current as never)
      release({
        session_id: 'stored-delay',
        messages: [
          { role: 'user', content: 'prompt', timestamp: 1 },
          { role: 'system', content: 'external notice', timestamp: 2 },
          ...(scenario === 'compacted' ? [{ role: 'assistant', content: 'Hello completed', timestamp: 3 }] : [])
        ]
      } as never)
      await refreshing
      const refreshed = states.current.get('runtime-delay')!
      expect(refreshed.busy).toBe(pending)
      expect(refreshed.streamId).toBe(current.streamId)
      expect(refreshed.messages.filter(message => message.role === 'assistant')).toEqual([
        current.messages[current.messages.length - 1]
      ])
      expect(JSON.stringify(refreshed.messages)).toContain('external notice')
    }
  })

  it('falls through to a real resume when the warm binding has no transcript (post-wake empty tile)', async () => {
    // Sleep/wake regression: a released/stale cached state (messages: []) must
    // NOT satisfy the warm path — reusing it re-bound the tile to a dead
    // runtime id and painted the pane permanently empty.
    setSessions([row({ id: 'stored-b', profile: 'default' })])

    const staleState = { busy: false, messages: [], storedSessionId: 'stored-b' }
    const runtimeIdByStoredSessionIdRef = { current: new Map([['stored-b', 'runtime-dead']]) }
    const sessionStateByRuntimeIdRef = { current: new Map([['runtime-dead', staleState]]) }

    const requestGateway = vi.fn(async () => ({}) as never)

    vi.mocked(requestGatewayForProfile).mockResolvedValueOnce({ session_id: 'runtime-fresh' } as never)

    renderTile(requestGateway, { runtimeIdByStoredSessionIdRef, sessionStateByRuntimeIdRef })
    const runtimeId = await sessionTileDelegate()!.resumeTile('stored-b')

    expect(runtimeId).toBe('runtime-fresh')
    expect(requestGatewayForProfile).toHaveBeenCalledWith(
      'default',
      'session.resume',
      {
        session_id: 'stored-b',
        cols: 96,
        profile: 'default',
        omit_messages: true
      },
      undefined,
      undefined
    )
  })

  it('hydrates the tile model and provider from resume info', async () => {
    setSessions([row({ id: 'stored-model', profile: 'default' })])

    const updateSessionState = vi.fn()

    vi.mocked(requestGatewayForProfile).mockResolvedValueOnce({
      info: { fast: true, model: 'gpt-5', provider: 'openai', reasoning_effort: 'high', running: false },
      session_id: 'runtime-model'
    } as never)

    renderTile(vi.fn(), { updateSessionState })
    const runtimeId = await sessionTileDelegate()!.resumeTile('stored-model')

    expect(runtimeId).toBe('runtime-model')
    expect(updateSessionState).toHaveBeenCalled()

    const updater = updateSessionState.mock.calls[0][1] as (state: { messages: unknown[] }) => Record<string, unknown>
    const next = updater({ messages: [] })

    expect(next.model).toBe('gpt-5')
    expect(next.provider).toBe('openai')
    expect(next.reasoningEffort).toBe('high')
    expect(next.reasoningEffortPending).toBe(false)
    expect(next.fast).toBe(true)
  })

  it("keeps the tile's effort pending when the deferred-build resume has not reported it (#79807)", async () => {
    setSessions([row({ id: 'stored-lazy', profile: 'default' })])

    const updateSessionState = vi.fn()

    vi.mocked(requestGatewayForProfile).mockResolvedValueOnce({
      info: { lazy: true, model: 'gpt-5', running: false },
      session_id: 'runtime-lazy'
    } as never)

    renderTile(vi.fn(), { updateSessionState })
    await sessionTileDelegate()!.resumeTile('stored-lazy')

    const updater = updateSessionState.mock.calls[0][1] as (state: ClientSessionState) => ClientSessionState
    const next = updater(createClientSessionState('stored-lazy'))

    expect(reasoningEffortPending(next)).toBe(true)
  })

  it('invalidateRuntimeBindings clears the stored→runtime map so tiles re-resume after reconnect', async () => {
    setSessions([row({ id: 'stored-c', profile: 'default' })])

    const liveState = { busy: false, messages: [{ id: 'm1' }], storedSessionId: 'stored-c' }

    const runtimeIdByStoredSessionIdRef = {
      current: new Map([
        ['stored-c', 'runtime-dead'],
        // A canonical binding: the runtime id IS the durable id, which no backend can re-mint.
        ['local-open', 'local-open']
      ])
    }

    const sessionStateByRuntimeIdRef = { current: new Map([['runtime-dead', liveState]]) }

    const requestGateway = vi.fn(async () => ({}) as never)

    vi.mocked(requestGatewayForProfile).mockResolvedValueOnce({ session_id: 'runtime-fresh' } as never)

    renderTile(requestGateway, { runtimeIdByStoredSessionIdRef, sessionStateByRuntimeIdRef })

    // Gateway reconnect (what resetTileRuntimeBindings calls on wake):
    sessionTileDelegate()!.invalidateRuntimeBindings!()
    expect([...runtimeIdByStoredSessionIdRef.current]).toEqual([['local-open', 'local-open']])

    // The next resume goes cold instead of reusing the dead binding.
    const runtimeId = await sessionTileDelegate()!.resumeTile('stored-c')
    expect(runtimeId).toBe('runtime-fresh')
  })

  it('branches a tile from the live message array that rendered the clicked action', async () => {
    const messages = [
      { id: 'q1', role: 'user' as const, parts: [{ type: 'text' as const, text: 'question one' }] },
      { id: 'a1', role: 'assistant' as const, parts: [{ type: 'text' as const, text: 'answer one' }] },
      { id: 'q2', role: 'user' as const, parts: [{ type: 'text' as const, text: 'question two' }] }
    ]

    const state = { ...createClientSessionState('stored-x', messages), cwd: '/repo' }
    const branchLoadedSession = vi.fn(async () => true)

    renderTile(vi.fn(), {
      branchLoadedSession,
      sessionStateByRuntimeIdRef: { current: new Map([['runtime-x', state]]) }
    })

    await expect(sessionTileDelegate()!.branchSessionAtMessage('stored-x', 'runtime-x', 'a1')).resolves.toBe(true)
    expect(branchLoadedSession).toHaveBeenCalledWith({
      busy: false,
      cwd: '/repo',
      messageId: 'a1',
      messages,
      runtimeId: 'runtime-x',
      storedSessionId: 'stored-x'
    })
  })

  it('does not branch when the clicked tile no longer has live state', async () => {
    const branchLoadedSession = vi.fn(async () => true)

    renderTile(vi.fn(), { branchLoadedSession })

    await expect(sessionTileDelegate()!.branchSessionAtMessage('stored-x', 'runtime-gone', 'a1')).resolves.toBe(false)
    expect(branchLoadedSession).not.toHaveBeenCalled()
  })
})

describe('useSessionTileDelegate switchSessionModel', () => {
  it('answers applied only after a guarded target is confirmed, and never on "keep current model"', async () => {
    const asked = { key: 'model', session_id: 'rt-1', value: 'example/model-a --provider nous --session' }

    const requestGateway = vi.fn(async (_method: string, params: Record<string, unknown>) =>
      params.confirm_expensive_model ? {} : { confirm_message: 'Large model', confirm_required: true }
    )

    renderTile(requestGateway)
    const switchSessionModel = sessionTileDelegate()!.switchSessionModel!

    vi.mocked(confirm).mockResolvedValueOnce(false)
    await expect(switchSessionModel('rt-1', 'example/model-a', 'nous')).resolves.toBe(false)
    expect(requestGateway).toHaveBeenCalledTimes(1)
    expect(requestGateway).toHaveBeenCalledWith('config.set', asked)

    vi.mocked(confirm).mockResolvedValueOnce(true)
    await expect(switchSessionModel('rt-1', 'example/model-a', 'nous')).resolves.toBe(true)
    expect(requestGateway).toHaveBeenLastCalledWith('config.set', { ...asked, confirm_expensive_model: true })
  })
})

describe('useSessionTileDelegate retireBusyClaim', () => {
  it('retires a stale busy claim through the session-state write path (#93059)', () => {
    const busyState = {
      awaitingResponse: true,
      busy: true,
      messages: [{ id: 'm1', pending: true }],
      storedSessionId: 'stored-d',
      turnLive: true,
      turnStartedAt: 1_000
    }

    const sessionStateByRuntimeIdRef = { current: new Map([['runtime-dead', busyState]]) }
    const updateSessionState = vi.fn()

    renderTile(
      vi.fn(async () => ({}) as never),
      { sessionStateByRuntimeIdRef, updateSessionState }
    )

    expect(sessionTileDelegate()!.retireBusyClaim!('runtime-dead')).toBe(true)
    expect(updateSessionState).toHaveBeenCalledWith('runtime-dead', expect.any(Function))

    // Retire the running claim and its clock; a pending bubble can remain
    // until the transcript is rehydrated from the recovered backend.
    const updater = updateSessionState.mock.calls[0][1] as (state: typeof busyState) => typeof busyState

    expect(updater(busyState)).toEqual({
      ...busyState,
      awaitingResponse: false,
      busy: false,
      turnLive: false,
      turnStartedAt: null
    })
  })

  it('reports a miss instead of minting a cache entry for a runtime it never held', () => {
    // No phantoms: updateSessionState mints a state for any id it is handed,
    // and prune never collects a transcript-less entry — so a miss must not
    // reach the write path; the store retires its own mirror instead.
    const idle = { awaitingResponse: false, busy: false, messages: [{ id: 'm1' }], storedSessionId: 'stored-e' }
    const sessionStateByRuntimeIdRef = { current: new Map([['runtime-idle', idle]]) }
    const updateSessionState = vi.fn()

    renderTile(
      vi.fn(async () => ({}) as never),
      { sessionStateByRuntimeIdRef, updateSessionState }
    )

    expect(sessionTileDelegate()!.retireBusyClaim!('runtime-unknown')).toBe(false)
    expect(sessionTileDelegate()!.retireBusyClaim!('runtime-idle')).toBe(false)
    expect(updateSessionState).not.toHaveBeenCalled()
  })
})

describe('useSessionTileDelegate interruptSession', () => {
  beforeEach(() => {
    setSessions([])
  })

  afterEach(async () => {
    setSessions([])
    const { clearSessionRecentlyInterrupted } = await import('../../session/hooks/use-prompt-actions/utils')
    clearSessionRecentlyInterrupted()
  })

  it('marks the session recently interrupted so a quick tile edit/resend still interrupt-firsts (#83855)', async () => {
    const { isSessionRecentlyInterrupted } = await import('../../session/hooks/use-prompt-actions/utils')

    const requestGateway = vi.fn(async () => ({}) as never)

    renderTile(requestGateway)
    await sessionTileDelegate()!.interruptSession('runtime-tile-1')

    expect(requestGateway).toHaveBeenCalledWith('session.interrupt', { session_id: 'runtime-tile-1' })
    // Same 3s cooldown the primary chat's Stop sets: busy reads false while the
    // gateway winds down, so the rewind path must still interrupt-first.
    expect(isSessionRecentlyInterrupted('runtime-tile-1')).toBe(true)
  })
})

describe('useSessionTileDelegate send from a tile behind the stored transcript', () => {
  const storedId = 'stored-tile-peer'
  const runtimeId = 'rt-tile-peer'

  beforeEach(() => {
    setSessions([])
    $notifications.set([])
    vi.mocked(getLatestSessionMessages).mockReset()
    vi.mocked(getLatestSessionMessages).mockImplementation(async () => ({ messages: [], session_id: storedId }))
  })

  afterEach(() => {
    setSessions([])
    $notifications.set([])
  })

  it('submits without a pre-send transcript read or a warning when a peer window advanced the chat (#65047)', async () => {
    setSessions([row({ id: storedId, profile: 'work-vps' })])
    vi.mocked(getLatestSessionMessages).mockResolvedValue({
      session_id: storedId,
      messages: [
        { content: 'a', role: 'user', timestamp: 1 },
        { content: 'b', role: 'assistant', timestamp: 2 },
        { content: 'c', role: 'user', timestamp: 3 },
        { content: 'd', role: 'assistant', timestamp: 4 }
      ]
    })

    const behind = createClientSessionState(storedId, [
      { id: 'u1', role: 'user', parts: [textPart('a')] },
      { id: 'a1', role: 'assistant', parts: [textPart('b')] }
    ])

    const requestGateway = vi.fn(async () => ({}) as never)

    renderTile(requestGateway, {
      runtimeIdByStoredSessionIdRef: { current: new Map([[storedId, runtimeId]]) },
      sessionStateByRuntimeIdRef: { current: new Map([[runtimeId, behind]]) }
    })

    await sessionTileDelegate()!.submitToSession(runtimeId, 'tile send from behind')

    expect(requestGatewayForProfile).toHaveBeenCalledWith(
      'work-vps',
      'prompt.submit',
      { session_id: runtimeId, text: 'tile send from behind' },
      1_800_000,
      undefined
    )
    expect(getLatestSessionMessages).not.toHaveBeenCalled()
    expect($notifications.get().some(note => note.kind === 'warning')).toBe(false)
  })
})

describe('useSessionTileDelegate submitToSession', () => {
  beforeEach(() => {
    setSessions([])
    // A leftover mockResolvedValueOnce on getLatestSessionMessages from an
    // earlier describe leaks into this suite's full-file run, so reset the
    // mock and restore its default empty-transcript implementation (same
    // pattern as the #65047 describe above).
    vi.mocked(getLatestSessionMessages).mockReset()
    vi.mocked(getLatestSessionMessages).mockImplementation(async () => ({ messages: [], session_id: '' }))
  })

  afterEach(() => {
    setSessions([])
  })

  it('returns the accepted runtime and its stored binding', async () => {
    setSessions([row({ id: 'stored-submit', profile: 'default' })])

    const state = { busy: false, messages: [{ id: 'm1' }], storedSessionId: 'stored-submit' }
    const runtimeIdByStoredSessionIdRef = { current: new Map([['stored-submit', 'runtime-dead']]) }
    const sessionStateByRuntimeIdRef = { current: new Map([['runtime-dead', state]]) }
    // #92961: a known owner always routes through the profile router, even
    // 'default', never the ambient socket — so the routed seam carries the
    // failed submit, the recovery resume, and the retry.
    const routed = vi.mocked(requestGatewayForProfile)
    routed.mockReset()
    routed.mockImplementation(async (_profile: string, method: string) => {
      if (method === 'prompt.submit') {
        return {} as never
      }

      if (method === 'session.resume') {
        return { session_id: 'runtime-recovered' } as never
      }

      throw new Error(`unexpected gateway method: ${method}`)
    })

    let promptAttempts = 0
    routed.mockImplementationOnce(async () => {
      promptAttempts += 1
      throw new Error('session not found')
    })

    renderTile(vi.fn(), { runtimeIdByStoredSessionIdRef, sessionStateByRuntimeIdRef })
    const delegate = sessionTileDelegate()!

    const recovered = await delegate.submitToSession('runtime-dead', 'Send from Quick Entry')
    expect(recovered).toEqual({
      runtimeSessionId: 'runtime-recovered',
      storedSessionId: 'stored-submit'
    })

    const accepted = await delegate.submitToSession('runtime-recovered', 'Send again')
    expect(accepted).toEqual({
      runtimeSessionId: 'runtime-recovered',
      storedSessionId: 'stored-submit'
    })
    expect(promptAttempts).toBe(1)
    expect(runtimeIdByStoredSessionIdRef.current.get('stored-submit')).toBe('runtime-recovered')
    expect(routed).toHaveBeenNthCalledWith(
      1,
      'default',
      'prompt.submit',
      { session_id: 'runtime-dead', text: 'Send from Quick Entry' },
      PROMPT_SUBMIT_REQUEST_TIMEOUT_MS,
      undefined
    )
    expect(routed).toHaveBeenNthCalledWith(
      2,
      'default',
      'session.resume',
      {
        session_id: 'stored-submit',
        source: 'desktop',
        omit_messages: true,
        profile: 'default'
      },
      undefined,
      undefined
    )
    expect(routed).toHaveBeenNthCalledWith(
      3,
      'default',
      'prompt.submit',
      { session_id: 'runtime-recovered', text: 'Send from Quick Entry' },
      PROMPT_SUBMIT_REQUEST_TIMEOUT_MS,
      undefined
    )
    expect(routed).toHaveBeenNthCalledWith(
      4,
      'default',
      'prompt.submit',
      { session_id: 'runtime-recovered', text: 'Send again' },
      PROMPT_SUBMIT_REQUEST_TIMEOUT_MS,
      undefined
    )
  })
})

describe('useSessionTileDelegate read-only cron run (#88443)', () => {
  const storedId = 'cron_job-1_20260929_120000'
  const runtimeId = 'rt-cron-run'

  beforeEach(() => {
    setSessions([])
    $notifications.set([])
    $cronRunReadOnlyVerdicts.set(new Map())
    vi.mocked(requestGatewayForProfile).mockClear()
    vi.mocked(getLatestSessionMessages).mockReset()
    vi.mocked(getLatestSessionMessages).mockImplementation(async () => ({ messages: [], session_id: storedId }))
    vi.mocked(getSession).mockReset()
  })

  afterEach(() => {
    setSessions([])
    $notifications.set([])
    $cronRunReadOnlyVerdicts.set(new Map())
  })

  const submitIntoRun = async (text: string) => {
    const requestGateway = vi.fn(async () => ({}) as never)

    renderTile(requestGateway, {
      runtimeIdByStoredSessionIdRef: { current: new Map([[storedId, runtimeId]]) },
      sessionStateByRuntimeIdRef: { current: new Map([[runtimeId, createClientSessionState(storedId, [])]]) }
    })

    await sessionTileDelegate()!.submitToSession(runtimeId, text)

    return requestGateway
  }

  const promptSubmitted = (text: string) =>
    vi
      .mocked(requestGatewayForProfile)
      .mock.calls.some(call => call[1] === 'prompt.submit' && (call[2] as { text?: string })?.text === text)

  // A tile holding a run the scheduler no longer owns (never closed) must
  // honour the same gate as the primary chat's `submit` — including a tile
  // restored after a restart, which no Cron surface ever evaluated.
  it('refuses a send into a never-closed run the scheduler does not own', async () => {
    setSessions([row({ id: storedId, profile: 'work-vps', source: 'cron' })])
    vi.mocked(getSession).mockResolvedValue(
      row({ ended_at: null, id: storedId, scheduler_owned: false, source: 'cron' })
    )

    const requestGateway = await submitIntoRun('into the dead cron session')

    expect(getSession).toHaveBeenCalledWith(storedId, expect.anything())
    expect(requestGateway).not.toHaveBeenCalledWith('prompt.submit', expect.anything(), expect.anything())
    expect(promptSubmitted('into the dead cron session')).toBe(false)
    expect($notifications.get().some(note => note.kind === 'info')).toBe(true)
  })

  // The review's blocker: a run marked read-only while it looked idle (a long
  // tool call) must not stay locked once the authoritative row says the
  // scheduler still owns it.
  it('re-evaluates a stale read-only verdict and sends once the run is owned', async () => {
    setSessions([row({ id: storedId, profile: 'work-vps', source: 'cron' })])
    recordCronRunVerdict(storedId, true)
    vi.mocked(getSession).mockResolvedValue(
      row({ ended_at: null, id: storedId, scheduler_owned: true, source: 'cron' })
    )

    await submitIntoRun('still running, send it')

    expect(promptSubmitted('still running, send it')).toBe(true)
    expect($notifications.get().some(note => note.kind === 'info')).toBe(false)
  })
})