Skip to content

The agent that grows with you

16.0 KB
Raw Blame
import type { GatewayEventName } from '@hermes/shared'
import { QueryClient } from '@tanstack/react-query'
import { act, cleanup, renderHook } from '@testing-library/react'
import { useRef } from 'react'
import { afterEach, beforeEach, expect, it, vi } from 'vitest'

import { graftRefreshedTailOntoBackfill } from '@/app/chat/transcript-backfill'
import { type ChatMessage, chatMessageText, toChatMessages } from '@/lib/chat-messages'
import {
  clearInFlightTurnJournal,
  readInFlightTurnJournal,
  recoverInFlightTurnJournal
} from '@/lib/inflight-turn-journal'
import { $messages, setActiveSessionId, setMessages } from '@/store/session'
import { $sessionStates } from '@/store/session-states'

import { usePromptActions } from '../use-prompt-actions'
import { reconcileDurableHistory } from '../use-session-actions/utils'
import { useSessionStateCache } from '../use-session-state-cache'

import { useMessageStream } from './index'

const SID = 'completion-runtime'
const STORED = 'completion-stored'
const ANSWER = 'The answer is unchanged.'
const noop = async () => undefined

// The canonical admission receipt echoes the client's submission id.
const requestGateway = async <T,>(method: string, params?: Record<string, unknown>): Promise<T> =>
  (method === 'prompt.submit'
    ? { admission_id: params?.submission_id, session_id: SID, status: 'started' }
    : { status: 'redirected' }) as T

// Real submit/redirect, stream reducer, cache and view publication; only RPC
// acceptance and history/metadata I/O are stand-ins.
function mount(rpc = requestGateway) {
  const queryClient = new QueryClient()
  const hydrate = vi.fn(noop)

  const hook = renderHook(() => {
    const busyRef = useRef(false)

    const cache = useSessionStateCache({
      activeSessionId: SID,
      selectedStoredSessionId: STORED,
      busyRef,
      setAwaitingResponse: () => undefined,
      setBusy: value => {
        busyRef.current = value
      },
      setMessages
    })

    const stream = useMessageStream({
      activeSessionIdRef: cache.activeSessionIdRef,
      sessionStateByRuntimeIdRef: cache.sessionStateByRuntimeIdRef,
      updateSessionState: cache.updateSessionState,
      queryClient,
      hydrateFromStoredSession: hydrate,
      refreshHermesConfig: noop,
      refreshSessions: noop
    })

    const actions = usePromptActions({
      activeSessionId: SID,
      activeSessionIdRef: cache.activeSessionIdRef,
      branchCurrentSession: async () => true,
      busyRef,
      createBackendSessionForSend: async () => SID,
      getRoutedStoredSessionId: () => STORED,
      getRuntimeIdForStoredSession: () => SID,
      getRouteToken: () => 'completion-route',
      handleSkinCommand: () => '',
      openMemoryGraph: () => undefined,
      refreshSessions: noop,
      requestGateway: rpc,
      resumeStoredSession: noop,
      runtimeIdByStoredSessionIdRef: cache.runtimeIdByStoredSessionIdRef,
      selectedStoredSessionIdRef: cache.selectedStoredSessionIdRef,
      startFreshSessionDraft: () => undefined,
      sttEnabled: false,
      updateSessionState: cache.updateSessionState
    })

    return { cache, stream, actions }
  })

  act(() => hook.result.current.cache.ensureSessionState(SID, STORED))

  return {
    hydrate,
    state: () => hook.result.current.cache.sessionStateByRuntimeIdRef.current.get(SID)!,
    send: (type: GatewayEventName, payload: Record<string, unknown> = {}) =>
      act(() => hook.result.current.stream.handleGatewayEvent({ type, payload, session_id: SID })),
    submit: () =>
      act(async () => {
        expect(await hook.result.current.actions.submitText('Give the answer.', { attachments: [] })).toBe(true)
      }),
    redirect: () =>
      act(async () => {
        expect(await hook.result.current.actions.redirectPrompt('Revise the answer.')).toBe(true)
      }),
    update: (updater: Parameters<typeof hook.result.current.cache.updateSessionState>[1]) =>
      act(() => {
        hook.result.current.cache.updateSessionState(SID, updater)
      }),
    dispose: () => {
      hook.unmount()
      queryClient.clear()
    }
  }
}

it('binds a late submit acknowledgement to its exact optimistic prompt without reviving the turn', async () => {
  const laterUser: ChatMessage = { id: 'later-user', role: 'user', parts: [{ type: 'text', text: 'Give the answer.' }] }

  const h = mount(async <T,>(_method: string, params?: Record<string, unknown>): Promise<T> => {
    await h.send('message.start')
    await h.send('message.delta', { text: ANSWER })
    await h.send('message.complete', { text: ANSWER })
    h.update(state => ({ ...state, messages: [...state.messages, laterUser], needsInput: true }))
    await flush()

    return { admission_id: params?.submission_id, session_id: SID, status: 'started', user_row_id: 71 } as T
  })

  await h.submit()
  await flush()

  expect(h.state().messages[0].rowId).toBe(71)
  expect($messages.get()[0].rowId).toBe(71)
  expect(h.state().messages.at(-1)).toBe(laterUser)
  expect(h.state()).toMatchObject({ busy: false, awaitingResponse: false, needsInput: true, streamId: null })
  h.dispose()
})

it.each([true, false, undefined])(
  'keeps the final occurrence receipt (complete=%s) before a queued prompt',
  async complete => {
    const h = mount()
    await h.submit()
    await h.send('message.start')
    await h.send('message.delta', { text: ANSWER })
    await flush()
    const assistantId = h.state().streamId
    const queued: ChatMessage = { id: `user-queued-${SID}`, role: 'user', parts: [{ type: 'text', text: 'Next' }] }
    h.update(state => ({ ...state, messages: [...state.messages, queued] }))

    const receipt =
      complete === undefined
        ? undefined
        : {
            row_ids: [71, 72, 73],
            user_row_id: 71,
            final_assistant_row_id: 73,
            complete
          }

    await h.send('message.complete', { text: ANSWER, persisted_turn: receipt })

    const settled = h.state().messages.find(message => message.id === assistantId)!
    expect(settled.pending).toBe(false)
    expect(settled.rowId).toBe(receipt?.final_assistant_row_id)
    expect(settled.parts.findLast(part => part.type === 'text')?.sourceRowId).toBe(receipt?.final_assistant_row_id)
    expect(settled.durableComplete === true).toBe(complete === true)
    expect(settled.persistedTurn).toEqual(receipt)
    expect(h.state().messages.at(-1)).toBe(queued)

    if (complete === true) {
      const latestPage = toChatMessages([{ id: 73, role: 'assistant', content: ANSWER }])
      h.update(state => ({ ...state, messages: reconcileDurableHistory(latestPage, state.messages) }))
      await flush()
      expect(timeline($messages.get())).toEqual([
        ['assistant', ANSWER],
        ['user', 'Next']
      ])
    }

    h.dispose()
  }
)

// The session authority acknowledges a queued admission before the prompt row
// exists, and writes a steer's row mid-turn, so no submit acknowledgement names
// them. The completion receipt does; a stored page read after the turn must
// replace those bubbles, not paint each prompt a second time beside its row.
const submissionIdOf = (h: ReturnType<typeof mount>) => h.state().messages[0].id.replace(/^user-/, '')

it('binds the sent prompt to the receipt row named by its submission', async () => {
  const h = mount()
  await h.submit()
  expect(h.state().messages[0].rowId).toBeUndefined()
  await h.send('message.start')
  await h.send('message.delta', { text: ANSWER })
  await h.send('message.complete', {
    text: ANSWER,
    persisted_turn: {
      row_ids: [71, 72], user_row_id: 71, user_row_ids: [71], final_assistant_row_id: 72, complete: true,
      submission_id: submissionIdOf(h)
    }
  })

  const latestPage = toChatMessages([
    { id: 71, role: 'user', content: 'Give the answer.' },
    { id: 72, role: 'assistant', content: ANSWER }
  ])

  expect(timeline(graftRefreshedTailOntoBackfill(latestPage, h.state().messages))).toEqual([
    ['user', 'Give the answer.'],
    ['assistant', ANSWER]
  ])
  h.dispose()
})

it('binds the prompt and the steer of a turn whose receipt is not complete', async () => {
  const h = mount()
  await h.submit()
  await h.send('message.start')
  await h.send('message.delta', { text: 'Partial' })
  await flush()
  await h.redirect()
  await h.send('message.delta', { text: ' then revised.' })
  await h.send('message.complete', {
    text: 'Revised.',
    persisted_turn: {
      row_ids: [71, 72, 73, 74], user_row_id: 71, user_row_ids: [71, 73], final_assistant_row_id: 74,
      complete: false, submission_id: submissionIdOf(h)
    }
  })

  expect(h.state().messages.filter(m => m.role === 'user').map(m => m.rowId)).toEqual([71, 73])
  h.dispose()
})

it('binds a viewer that never sent the turn only when it shows exactly the receipt rows', async () => {
  const h = mount()

  const earlier = toChatMessages([
    { id: 60, role: 'user', content: 'Earlier.' },
    { id: 61, role: 'assistant', content: 'Before.' }
  ])

  h.update(state => ({ ...state, messages: [...earlier, { id: 'user-peer', role: 'user', parts: [{ type: 'text', text: 'Give the answer.' }] }] }))
  await h.send('message.start')
  await h.send('message.delta', { text: ANSWER })
  await h.send('message.complete', {
    text: ANSWER,
    persisted_turn: {
      row_ids: [71, 72], user_row_id: 71, user_row_ids: [71], final_assistant_row_id: 72, complete: true,
      submission_id: 'sent-from-another-window'
    }
  })

  expect(h.state().messages.find(m => m.id === 'user-peer')?.rowId).toBe(71)
  h.dispose()
})

const timeline = (messages: ChatMessage[]) => messages.map(message => [message.role, chatMessageText(message)])

const flush = () =>
  act(async () => {
    await vi.advanceTimersByTimeAsync(50)
  })

beforeEach(() => {
  vi.useFakeTimers()
  localStorage.clear()
  $sessionStates.set({})
  setMessages([])
  setActiveSessionId(SID)
})
afterEach(() => {
  cleanup()
  clearInFlightTurnJournal(STORED)
  vi.clearAllTimers()
  vi.useRealTimers()
  vi.restoreAllMocks()
})

it.each([false, true].flatMap(previousFailure => [ANSWER, ''].map(reply => ({ previousFailure, reply }))))(
  'keeps a new completion after a real second submit (previousFailure=$previousFailure, reply="$reply")',
  async ({ previousFailure, reply }) => {
    const h = mount()
    await h.submit()
    await h.send('message.start')
    await h.send('message.delta', { text: ANSWER })
    await h.send('message.complete', {
      text: ANSWER,
      ...(previousFailure ? { status: 'error', error: 'Interrupted provider', partial: true } : {})
    })
    const firstAnswer = h.state().messages.at(-1)!
    expect(h.hydrate).not.toHaveBeenCalled()
    await flush()
    await h.submit() // Even identical prompt text is a new occurrence.
    await h.send('message.start')
    await h.send('message.complete', { text: reply })

    const expected = [
      ['user', 'Give the answer.'],
      ['assistant', ANSWER],
      ['user', 'Give the answer.'],
      ...(reply ? [['assistant', reply]] : [])
    ]

    expect(timeline(h.state().messages)).toEqual(expected)
    expect(timeline($messages.get())).toEqual(expected)
    expect(h.state().messages.find(message => message.id === firstAnswer.id)).toEqual(firstAnswer)
    expect(h.hydrate).toHaveBeenCalledWith(3, STORED, SID)

    if (reply) {
      const ids = h.state().messages.map(message => message.id)
      await h.send('message.complete', { text: reply })
      expect(h.state().messages.map(message => message.id)).toEqual(ids)
      expect(timeline($messages.get())).toEqual(expected)
    }

    h.dispose()
  }
)

it.each([
  { before: 'Answer', after: 'Answer with details' },
  { before: 'Answer with details', after: 'Answer' },
  { before: 'Answer', after: 'Rewritten reply', previewed: true },
  { before: 'Answer', after: 'Answer with details', hidden: true },
  { before: 'Answer', after: 'Answer with details', staleStream: true }
])('keeps redirect boundaries and settles duplicates: $before → $after ($hidden, $staleStream)', async fixture => {
  const h = mount()
  await h.submit()
  await h.send('message.start')
  await h.send('message.delta', { text: fixture.before })
  await h.send('message.interim', { text: fixture.before, already_streamed: true })
  expect(timeline(h.state().messages)).toEqual([
    ['user', 'Give the answer.'],
    ['assistant', fixture.before]
  ])
  const before = h.state().messages.at(-1)!
  await flush()
  await h.redirect()

  // Visibility is not identity; a hidden user row still divides occurrences.
  // A stale stream reference must not authorize completion across that row.
  if (fixture.hidden || fixture.staleStream) {
    h.update(state => ({
      ...state,
      streamId: fixture.staleStream ? before.id : state.streamId,
      messages: fixture.hidden
        ? state.messages.map(message => (message === state.messages.at(-1) ? { ...message, hidden: true } : message))
        : state.messages
    }))
  }

  await h.send('message.complete', { text: fixture.after, response_previewed: fixture.previewed })

  const expected = [
    ['user', 'Give the answer.'],
    ['assistant', fixture.before],
    ['user', 'Revise the answer.'],
    ['assistant', fixture.after]
  ]

  expect(timeline(h.state().messages)).toEqual(expected)
  expect(h.state().messages.find(message => message.id === before.id)).toEqual(before)
  expect(h.state().messages.at(-1)?.interim).toBeFalsy()
  const ids = h.state().messages.map(message => message.id)
  await h.send('message.complete', { text: fixture.after, response_previewed: fixture.previewed })
  expect(h.state().messages.map(message => message.id)).toEqual(ids)
  expect(timeline($messages.get())).toEqual(expected)
  h.dispose()
})

// A completion whose text IS the sealed pre-redirect reply, with nothing
// streamed since, is that reply's own completion (rejected redirect race, or a
// steer the model absorbed without new output): it settles the seal and the
// correction stays the tail — see steer-arrival-order.test.tsx for the
// streaming-side tripwire.
it('settles an equal no-delta completion onto the sealed pre-redirect reply', async () => {
  const h = mount()
  await h.submit()
  await h.send('message.start')
  await h.send('message.delta', { text: 'Answer' })
  await h.send('message.interim', { text: 'Answer', already_streamed: true })
  await flush()
  await h.redirect()
  await h.send('message.complete', { text: 'Answer' })

  expect(timeline(h.state().messages)).toEqual([
    ['user', 'Give the answer.'],
    ['assistant', 'Answer'],
    ['user', 'Revise the answer.']
  ])
  expect(h.state().messages[1].interim).toBeFalsy()
  h.dispose()
})

it.each(['fallback', 'stream', 'tool-interim'] as const)(
  'retires recovered metadata when completion settles the %s row',
  async path => {
    const h = mount()
    await h.submit()
    await h.send('message.start')
    await h.send('message.delta', { text: ANSWER })

    if (path === 'tool-interim') {
      await h.send('message.interim', { text: ANSWER, already_streamed: true })
    }

    await act(async () => {
      await vi.advanceTimersByTimeAsync(1000)
    })
    expect(readInFlightTurnJournal(STORED)).not.toBeNull()
    const recovered = recoverInFlightTurnJournal(STORED, h.state().messages.slice(0, 1), { keepPending: false })
    expect(recovered.applied).toBe(true)
    const recoveredId = recovered.messages.at(-1)!.id
    h.update(state => ({
      ...state,
      busy: false,
      awaitingResponse: false,
      turnLive: false,
      streamId: path === 'stream' ? recoveredId : null,
      messages: recovered.messages
    }))
    expect(h.state().messages.at(-1)).toMatchObject({ recovered: true, pending: false })

    if (path === 'tool-interim') {
      await h.send('tool.start', { name: 'terminal', tool_id: 'after-recovery' })
      await h.send('tool.complete', { name: 'terminal', tool_id: 'after-recovery', result: 'ok' })
    }

    await h.send('message.complete', { text: ANSWER })
    expect(h.state().messages).toHaveLength(2)
    expect(h.state().messages.at(-1)).toMatchObject({ id: recoveredId, recovered: false, pending: false })
    expect(chatMessageText(h.state().messages.at(-1)!)).toBe(ANSWER)
    expect(readInFlightTurnJournal(STORED)).toBeNull()
    expect(h.state()).toMatchObject({ busy: false, awaitingResponse: false, streamId: null })
    h.dispose()
  }
)