Skip to content

The agent that grows with you

58.6 KB
Raw Blame
import { describe, expect, it } from 'vitest'

import { toolResultRecord } from '@/lib/tool-result-metadata'
import type { SessionMessage } from '@/types/hermes'

import type { ChatMessage, ChatMessagePart } from './chat-messages'
import {
  appendAssistantTextPart,
  appendReasoningPart,
  chatMessageText,
  collectUnspokenTurnSpeech,
  completeOpenTimelineParts,
  mergeFinalAssistantText,
  preserveLocalAssistantErrors,
  reasoningPart,
  renderMediaTags,
  restorePendingClarifyToolCall,
  sealOpenToolParts,
  stripPendingClarifyProjectionForCache,
  toChatMessages,
  upsertToolPart,
  withUniqueToolCallIdsWithinMessage
} from './chat-messages'

const toolCallPart = (toolCallId: string): ChatMessagePart =>
  ({
    type: 'tool-call' as const,
    toolCallId,
    toolName: 'read_file',
    args: {} as never,
    argsText: '{}'
  }) as ChatMessagePart

const assistantWith = (parts: ChatMessagePart[]): ChatMessage =>
  ({ id: 'm1', role: 'assistant', parts, timestamp: 0 }) as unknown as ChatMessage

describe('withUniqueToolCallIdsWithinMessage', () => {
  it('renames a duplicate toolCallId within one message (#87857)', () => {
    const message = assistantWith([toolCallPart('call_x'), toolCallPart('call_x')])

    const result = withUniqueToolCallIdsWithinMessage(message)

    const ids = result.parts
      .filter((part): part is Extract<ChatMessagePart, { type: 'tool-call' }> => part.type === 'tool-call')
      .map(part => part.toolCallId)

    expect(new Set(ids).size).toBe(ids.length)
    expect(ids[0]).toBe('call_x')
    expect(ids[1]).not.toBe('call_x')
  })

  it('returns the same reference when there is no duplicate', () => {
    const message = assistantWith([toolCallPart('call_a'), toolCallPart('call_b')])

    expect(withUniqueToolCallIdsWithinMessage(message)).toBe(message)
  })
})

describe('toChatMessages', () => {
  it('does not render an opaque native_assistant reasoning_details carrier as Thought (#126588)', () => {
    const carrier = JSON.stringify([
      {
        type: 'claude-subscription-directsdk-experimental.native_assistant',
        version: 1,
        messages: [
          {
            content: [
              { type: 'thinking', thinking: '', signature: 'sigabc' },
              { type: 'text', text: 'Public answer' }
            ]
          }
        ],
        projection: { content: 'Public answer', tool_calls: [] }
      }
    ])

    const [message] = toChatMessages([
      {
        role: 'assistant',
        content: 'Public answer',
        reasoning: null,
        reasoning_content: null,
        reasoning_details: carrier,
        timestamp: 1
      }
    ])

    expect(message.parts.filter(part => part.type === 'reasoning')).toHaveLength(0)
    expect(chatMessageText(message)).toContain('Public answer')
    const rendered = JSON.stringify(message.parts)
    expect(rendered).not.toContain('sigabc')
    expect(rendered).not.toContain('native_assistant')
  })

  it('shows readable reasoning text from a serialized reasoning_details envelope', () => {
    const [message] = toChatMessages([
      {
        role: 'assistant',
        content: 'Answer',
        reasoning_details: JSON.stringify([
          { type: 'reasoning.summary', summary: 'Checked the lock.' },
          {
            type: 'demo.native_assistant',
            messages: [{ content: [{ type: 'thinking', thinking: 'Nested thought.', signature: 'sigxyz' }] }]
          }
        ]),
        timestamp: 1
      }
    ])

    const reasoning = message.parts.find(part => part.type === 'reasoning')

    expect(reasoning && 'text' in reasoning ? reasoning.text : '').toBe('Checked the lock.\n\nNested thought.')
    expect(JSON.stringify(message.parts)).not.toContain('sigxyz')
  })

  it('rebuilds the full command from a gateway tool row carrying args', () => {
    // Gateway watch-window hydration projects tool rows as
    // {role:'tool', name, context, args?}. `context` is an 80-char preview;
    // the backend also ships the full args, and the part must carry them so
    // the expanded `$` transcript shows the whole command.
    const longCommand = `echo ${'x'.repeat(200)}`

    const messages = toChatMessages([
      { role: 'user', content: 'run it', timestamp: 1 },
      {
        role: 'tool',
        name: 'terminal',
        content: '',
        context: `${longCommand.slice(0, 79)}…`,
        args: { command: longCommand },
        timestamp: 2
      }
    ])

    const toolPart = messages.flatMap(m => m.parts).find(part => part.type === 'tool-call')

    expect(toolPart).toBeDefined()
    expect((toolPart as { args: { command?: string } }).args.command).toBe(longCommand)
  })

  it('keeps a turn with interleaved tool-only rows in a single bubble', () => {
    const messages = toChatMessages([
      { role: 'assistant', content: 'Planning.', timestamp: 1 },
      {
        role: 'assistant',
        content: '',
        timestamp: 2,
        tool_calls: [{ id: 'tc', function: { name: 'terminal', arguments: '{}' } }]
      },
      { role: 'assistant', content: 'Done.', timestamp: 3 }
    ])

    expect(messages).toHaveLength(1)
    expect(messages[0].parts.map(p => p.type)).toEqual(['text', 'tool-call', 'text'])
    expect(chatMessageText(messages[0])).toBe('Planning.Done.')
    expect(messages[0].timestamp).toBe(1)
    expect(messages[0].parts.map(p => p.timestamp)).toEqual([1, 2, 3])
  })

  it('restores the stopped flag on a persisted interrupted reply', () => {
    const messages = toChatMessages([
      { role: 'user', content: 'tell a story', timestamp: 1 },
      { role: 'assistant', content: 'Once upon', timestamp: 2, display_metadata: '{"interrupted": true}' },
      { role: 'user', content: 'again', timestamp: 3 },
      { role: 'assistant', content: 'The end.', timestamp: 4 }
    ])

    expect(messages.map(message => message.interrupted)).toEqual([undefined, true, undefined, undefined])
  })

  it('starts a hydrated bubble at an earlier leading tool call', () => {
    const messages = toChatMessages([
      {
        role: 'assistant',
        content: '',
        timestamp: 1,
        tool_calls: [{ id: 'tc', function: { name: 'terminal', arguments: '{}' } }]
      },
      { role: 'tool', tool_call_id: 'tc', content: 'ok', timestamp: 2 },
      { role: 'assistant', content: 'Done.', timestamp: 3 }
    ])

    expect(messages).toHaveLength(1)
    expect(messages[0].timestamp).toBe(1)
    expect(messages[0].parts.map(part => part.timestamp)).toEqual([1, 3])
  })

  it('keeps assistant tool-call iterations in one loaded assistant bubble', () => {
    const messages = toChatMessages([
      { role: 'user', content: 'check this repo', timestamp: 1 },
      {
        role: 'assistant',
        content: "Let me also check if there's a top-level lint workflow.",
        timestamp: 2,
        tool_calls: [{ id: 'tc-1', function: { name: 'search_files', arguments: '{"path":".github"}' } }]
      },
      {
        role: 'tool',
        tool_call_id: 'tc-1',
        tool_name: 'search_files',
        content: '{"error":"Path not found: /repo/.github"}',
        timestamp: 3
      },
      {
        role: 'assistant',
        content: 'No CI in this repo. Build is enough.',
        timestamp: 4,
        tool_calls: [{ id: 'tc-2', function: { name: 'terminal', arguments: '{"command":"git status --short"}' } }]
      },
      {
        role: 'tool',
        tool_call_id: 'tc-2',
        tool_name: 'terminal',
        content: '{"output":"M src/ui/components/image-distortion.tsx\\n","exit_code":0}',
        timestamp: 5
      },
      { role: 'assistant', content: 'Now let me check git status and commit.', timestamp: 6 }
    ])

    const assistantMessages = messages.filter(message => message.role === 'assistant')

    expect(assistantMessages).toHaveLength(1)
    expect(assistantMessages[0].timestamp).toBe(2)
    expect(assistantMessages[0].parts.map(part => part.timestamp)).toEqual([2, 2, 4, 4, 6])
    expect(assistantMessages[0].parts.filter(part => part.type === 'tool-call').map(part => part.completedAt)).toEqual([
      3, 5
    ])
    expect(assistantMessages[0].parts.filter(part => part.type === 'tool-call')).toHaveLength(2)
    expect(chatMessageText(assistantMessages[0])).toContain("Let me also check if there's a top-level lint workflow.")
    expect(chatMessageText(assistantMessages[0])).toContain('Now let me check git status and commit.')
  })

  it('hides attached context payloads from user message display', () => {
    const [message] = toChatMessages([
      {
        role: 'user',
        content:
          'what is this file\n\n--- Attached Context ---\n\n📄 @file:tsconfig.tsbuildinfo (981 tokens)\n```json\n{"root":["./src/main.tsx"]}\n```',
        timestamp: 1
      }
    ])

    expect(chatMessageText(message)).toBe('what is this file')
    expect(message.attachmentRefs).toEqual(['@file:tsconfig.tsbuildinfo'])
  })

  it('lifts the leading attachment block into the chip row, like the live bubble (large paste)', () => {
    const paste = '@file:/home/u/.hermes/attachments/pasted_content_2026-09-27_21-33-42-827_55e028-2.txt'

    const [captioned, bare] = toChatMessages([
      {
        role: 'user',
        content: `${paste}\n@image:/tmp/shot.png\n\nfix these\n\n--- Attached Context ---\n\n📄 ${paste} (769 tokens)\n\`\`\`\nlog\n\`\`\``,
        timestamp: 1
      },
      { role: 'user', content: `${paste}\n\n--- Attached Context ---\n\n📄 ${paste} (769 tokens)`, timestamp: 2 }
    ])

    expect(chatMessageText(captioned)).toBe('fix these')
    expect(captioned.attachmentRefs).toEqual([paste, '@image:/tmp/shot.png'])
    expect(chatMessageText(bare)).toBe('')
    expect(bare.attachmentRefs).toEqual([paste])
  })

  it('hides a persisted Discord triggering-message note but keeps the reply pointer (#114719)', () => {
    const note =
      '[Triggering message id: `1550380365858865156` — use as `message_id` for reply/react/pin via the discord tools.]'

    const [plain, , replied, assistant] = toChatMessages([
      { role: 'user', content: `${note}\n\nCreate a project plan for Q4`, timestamp: 1 },
      { role: 'assistant', content: 'ok', timestamp: 2 },
      {
        role: 'user',
        content: `[Replying to: "Create a project plan for Q4"]\n\n${note}\n\nyes do that`,
        timestamp: 3
      },
      { role: 'assistant', content: note, timestamp: 4 }
    ])

    expect(chatMessageText(plain)).toBe('Create a project plan for Q4')
    expect(chatMessageText(replied)).toBe('[Replying to: "Create a project plan for Q4"]\n\nyes do that')
    expect(chatMessageText(assistant)).toBe(note)
  })

  it('renders MEDIA tags as assistant attachment links', () => {
    const [message] = toChatMessages([
      {
        role: 'assistant',
        content: "MEDIA:/Users/brooklyn/.hermes/cache/audio/tts_20260501_222725.mp3\n\nhow's that sound?",
        timestamp: 1
      }
    ])

    expect(chatMessageText(message)).toBe(
      "[Audio: tts_20260501_222725.mp3](#media:%2FUsers%2Fbrooklyn%2F.hermes%2Fcache%2Faudio%2Ftts_20260501_222725.mp3)\n\nhow's that sound?"
    )
  })

  it('keeps the generated image on the tool row while preserving agent prose', () => {
    const [message] = toChatMessages([
      {
        content: '',
        role: 'assistant',
        timestamp: 1,
        tool_calls: [{ id: 'img-1', function: { name: 'image_generate', arguments: '{"prompt":"draw a cat"}' } }]
      },
      {
        content: '{"success":true,"image":"https://cdn.example/cat.png"}',
        role: 'tool',
        timestamp: 2,
        tool_call_id: 'img-1',
        tool_name: 'image_generate'
      },
      {
        content: 'Here you go.\n\n![Generated image](https://cdn.example/cat.png)',
        role: 'assistant',
        timestamp: 3
      }
    ])

    const toolPart = message.parts.find(
      (part): part is Extract<ChatMessagePart, { type: 'tool-call' }> =>
        part.type === 'tool-call' && part.toolName === 'image_generate'
    )

    expect(toolPart?.result).toMatchObject({ image: 'https://cdn.example/cat.png', success: true })
    // The duplicated image is stripped, but the agent's words survive.
    expect(chatMessageText(message)).toBe('Here you go.\n\n')
  })

  it('hydrates canonical native image suffixes without changing durable history or caption prose', () => {
    const caption = 'Explain [Image attached at: /not/an/attachment.png] in this sentence.'
    const paths = ['/cache/native-inputs/hash/a [1].png', 'https://example.test/image.png']
    const content = `${caption}\n\n[Image attached at: ${paths[0]}]\n[Image attached: ${paths[1]}]\n[screenshot]\n[screenshot]`
    const stored = { role: 'user' as const, content, timestamp: 1, row_id: 23 }
    const [message] = toChatMessages([stored])

    expect(chatMessageText(message)).toBe(caption)
    expect(message.attachmentRefs).toEqual([`@image:\`${paths[0]}\``, `@image:${paths[1]}`])
    expect(stored.content).toBe(content)
    const [prose] = toChatMessages([{ ...stored, content: `${caption}\n\n[Image attached at: /literal.png]` }])
    expect(chatMessageText(prose)).toBe(`${caption}\n\n[Image attached at: /literal.png]`)
    expect(prose.attachmentRefs).toBeUndefined()
    const [assistant] = toChatMessages([{ ...stored, role: 'assistant' }])
    expect(chatMessageText(assistant)).toBe(content)
  })

  it('lifts @image directive lines into attachmentRefs instead of inline text', () => {
    const [message] = toChatMessages([
      {
        role: 'user',
        content: '@image:/tmp/cat.png\nwhat is in this photo?',
        timestamp: 1
      }
    ])

    expect(chatMessageText(message)).toBe('what is in this photo?')
    expect((message as { attachmentRefs?: string[] }).attachmentRefs).toEqual(['@image:/tmp/cat.png'])
  })

  it('keeps a user turn that carried only an attached image (no caption)', () => {
    const [message] = toChatMessages([
      {
        role: 'user',
        content: '@image:/tmp/cat.png',
        timestamp: 1
      }
    ])

    // The bubble has no visible text, but must survive the empty-turn filter
    // because it carries attachment refs — otherwise a stand-alone attachment
    // vanishes from the transcript after a session switch / restart.
    expect(chatMessageText(message)).toBe('')
    expect((message as { attachmentRefs?: string[] }).attachmentRefs).toEqual(['@image:/tmp/cat.png'])
  })

  it('renders a native-vision turn as caption plus thumbnail, not raw placeholder text', () => {
    // How a turn sent to a natively-vision-capable model comes back out of the
    // session store: a backtick-quoted ref (the path has spaces) and the
    // `[screenshot]` stand-in left by flattening the parts list.
    const ref = '@image:`/Users/me/Library/Application Support/Hermes/composer-images/a.png`'

    const [message] = toChatMessages([
      {
        role: 'user',
        content: `${ref}\nwhat is in this photo?\n[screenshot]`,
        timestamp: 1
      }
    ])

    expect(chatMessageText(message)).toBe('what is in this photo?\n')
    expect((message as { attachmentRefs?: string[] }).attachmentRefs).toEqual([ref])
  })

  it('leaves a plain user prompt without attachment refs untouched', () => {
    const [message] = toChatMessages([
      {
        role: 'user',
        content: 'just a question',
        timestamp: 1
      }
    ])

    expect((message as { attachmentRefs?: string[] }).attachmentRefs).toBeUndefined()
  })

  it('coerces non-string message content without throwing', () => {
    const [message] = toChatMessages([
      {
        content: {
          text: 'hello from object content'
        },
        role: 'assistant',
        timestamp: 1
      }
    ])

    expect(chatMessageText(message)).toBe('hello from object content')
  })

  it('applies attached-context filtering when user content is object-shaped', () => {
    const [message] = toChatMessages([
      {
        content: {
          text: 'look\n\n--- Attached Context ---\n\n📄 @file:foo.ts (10 tokens)\n```ts\nconst x = 1\n```'
        },
        role: 'user',
        timestamp: 1
      }
    ])

    expect(chatMessageText(message)).toBe('look')
    expect(message.attachmentRefs).toEqual(['@file:foo.ts'])
  })

  it('leaves an inline @ ref in place instead of hoisting a duplicate', () => {
    const [message] = toChatMessages([
      {
        role: 'user',
        content:
          'summarize @file:`src/main.ts` for me\n\n--- Attached Context ---\n\n📄 @file:`src/main.ts` (10 tokens)\n```ts\nconst x = 1\n```',
        timestamp: 1
      }
    ])

    expect(chatMessageText(message)).toBe('summarize @file:`src/main.ts` for me')
  })

  it('never paints redirect scaffolding as an assistant bubble', () => {
    // What the desktop actually receives after a mid-stream steer: the runtime
    // keeps the interrupt scaffolding in a server-only api_content sidecar
    // (never shipped to the client) so content is already clean, and marks a
    // prose-free checkpoint display_kind:'hidden'. The transcript must show the
    // partial reply and the user's correction — never
    // "[This response was interrupted by a user correction.]".
    const messages = toChatMessages([
      { role: 'user', content: 'go', timestamp: 1 },
      {
        role: 'assistant',
        content: 'Hey. I was mid-Figma MCP fix when we paused.',
        timestamp: 2
      },
      { role: 'user', content: 'i love you', timestamp: 3 },
      {
        // Nothing had reached the screen — checkpoint exists only for the model.
        role: 'assistant',
        content: '[This response was interrupted by a user correction.]',
        display_kind: 'hidden',
        timestamp: 4
      },
      { role: 'user', content: 'keep going', timestamp: 5 }
    ])

    expect(messages.map(chatMessageText)).toEqual([
      'go',
      'Hey. I was mid-Figma MCP fix when we paused.',
      'i love you',
      'keep going'
    ])

    for (const message of messages) {
      expect(chatMessageText(message)).not.toContain('This response was interrupted')
      expect(chatMessageText(message)).not.toContain('Visible response before the interruption')
      expect(chatMessageText(message)).not.toContain('Context from the interrupted assistant response')
    }
  })

  it('projects persisted composite compaction carriers to their live user turn', () => {
    const messages = toChatMessages([
      {
        id: 71,
        role: 'user',
        content: 'internal summary scaffold\n\nREAL ASK',
        display_content: 'REAL ASK',
        timestamp: 1
      },
      {
        id: 72,
        role: 'user',
        content: 'prior live ask\n\ninternal summary scaffold',
        display_content: 'prior live ask',
        timestamp: 2
      }
    ])

    expect(messages.map(chatMessageText)).toEqual(['REAL ASK', 'prior live ask'])
    expect(messages.map(message => message.rowId)).toEqual([71, 72])
  })

  it('projects durable timeline kinds without inspecting their text', () => {
    const messages = toChatMessages([
      { role: 'user', content: 'real user turn', timestamp: 1 },
      { role: 'assistant', content: 'real assistant reply', timestamp: 2 },
      {
        role: 'user',
        content: 'opaque compaction payload',
        display_kind: 'hidden',
        timestamp: 3
      },
      {
        role: 'user',
        content: 'opaque model context payload',
        display_kind: 'model_switch',
        timestamp: 4
      },
      {
        role: 'user',
        content: 'opaque delegation context payload',
        display_kind: 'async_delegation_complete',
        timestamp: 5
      },
      {
        role: 'user',
        content: '[System note: Your previous turn was interrupted mid-run…]\n\noriginal prompt',
        display_kind: 'auto_continue',
        timestamp: 6
      },
      {
        role: 'user',
        content: "[System: The user has changed the assistant's personality…]",
        display_kind: 'personality_switch',
        timestamp: 7
      }
    ])

    expect(messages.map(message => message.role)).toEqual(['user', 'assistant', 'system', 'system', 'system', 'system'])
    expect(messages.map(chatMessageText)).toEqual([
      'real user turn',
      'real assistant reply',
      'model changed',
      'background agent work finished',
      'resumed interrupted turn',
      'personality changed'
    ])
  })

  it('never paints a background-process heartbeat wake as a user bubble', () => {
    // Current backends type the wake `display_kind: 'hidden'`; a row persisted by an older
    // backend arrives untyped and must disappear the same way — the user never wrote it.
    const legacyWake =
      '[Background process proc_ea2cdb25d899 heartbeat #7 — still running after 7m2s (next in 60s; you will also be told when it exits).\nCommand: zsh -ic hgui\nOutput since last heartbeat:\n(no new output since the last heartbeat)]'

    const messages = toChatMessages([
      { role: 'user', content: 'start the dev server', timestamp: 1 },
      { role: 'assistant', content: 'Started on slot 0.', timestamp: 2 },
      { role: 'user', content: legacyWake, timestamp: 3 },
      { role: 'assistant', content: 'Still running normally.', timestamp: 4 },
      { role: 'user', content: legacyWake.replace('#7', '#8'), display_kind: 'hidden', timestamp: 5 },
      { role: 'assistant', content: 'HMR rebuilt after the edit.', timestamp: 6 }
    ])

    expect(messages.map(message => message.role)).toEqual(['user', 'assistant', 'assistant', 'assistant'])
    expect(messages.map(chatMessageText)).toEqual([
      'start the dev server',
      'Started on slot 0.',
      'Still running normally.',
      'HMR rebuilt after the edit.'
    ])
  })

  // Hermes closes a failed turn with an assistant-role row (agent/turn_failure_copy.py);
  // painted as the model's reply it read as the assistant refusing the request.
  it('renders the failed-turn boundary as a Hermes notice, not a model reply', () => {
    const messages = toChatMessages([
      { role: 'user', content: 'do the thing', timestamp: 1 },
      { role: 'assistant', content: 'Your request was not processed.', display_kind: 'failed_turn', timestamp: 2 }
    ])

    expect(messages.map(message => message.role)).toEqual(['user', 'system'])
    expect(chatMessageText(messages[1])).toBe('Your request was not processed.')
  })

  it('redraws the failed turn error card kept on the boundary row', () => {
    const messages = toChatMessages([
      { role: 'user', content: 'do the thing', timestamp: 1 },
      {
        role: 'assistant',
        content: 'Your request was not processed.',
        display_kind: 'failed_turn',
        display_metadata: {
          error: 'Connection error.',
          error_surface: { code: 'timeout', layer: 'provider', provider: 'opencode-go', retryable: true }
        },
        timestamp: 2
      }
    ])

    expect(messages.map(message => message.role)).toEqual(['user', 'assistant', 'system'])
    expect(messages[1]).toMatchObject({
      error: 'Connection error.',
      errorSurface: { code: 'timeout', layer: 'provider', provider: 'opencode-go' },
      serverRowSpan: 0
    })
    expect(chatMessageText(messages[2])).toBe('Your request was not processed.')
  })

  it('shows only the notice for a boundary row without a usable error surface', () => {
    const messages = toChatMessages([
      { role: 'user', content: 'do the thing', timestamp: 1 },
      {
        role: 'assistant',
        content: 'Your request was not processed.',
        display_kind: 'failed_turn',
        display_metadata: { error: 'Connection error.', error_surface: { layer: 'bogus' } },
        timestamp: 2
      }
    ])

    expect(messages.map(message => message.role)).toEqual(['user', 'system'])
  })

  // A backend older than this app serves display_metadata as unparsed JSON
  // text. Indexing into that string used to throw and fail the whole resume.
  it.each([
    ['an object', { delegation_id: 'deleg_1', task_count: 2 }, '2 background agents finished'],
    ['JSON text', JSON.stringify({ delegation_id: 'deleg_1', task_count: 1 }), '1 background agent finished'],
    ['unparseable text', '{not-json', 'background agent work finished'],
    ['text that is not an object', '"deleg_1"', 'background agent work finished'],
    ['a missing task count', { delegation_id: 'deleg_1' }, 'background agent work finished']
  ])('labels a delegation event given %s', (_case, displayMetadata, expected) => {
    const read = () =>
      toChatMessages([
        {
          role: 'user',
          content: 'opaque delegation context payload',
          display_kind: 'async_delegation_complete',
          display_metadata: displayMetadata as SessionMessage['display_metadata'],
          timestamp: 1
        }
      ])

    expect(read).not.toThrow()
    expect(chatMessageText(read()[0])).toBe(expected)
  })
})

describe('renderMediaTags', () => {
  it('renders standalone and inline MEDIA tags as links', () => {
    expect(renderMediaTags('here\nMEDIA:/tmp/voice.mp3\nthere')).toBe(
      'here\n[Audio: voice.mp3](#media:%2Ftmp%2Fvoice.mp3)\nthere'
    )
    expect(renderMediaTags('audio: MEDIA:/tmp/voice.mp3 done')).toBe(
      'audio: [Audio: voice.mp3](#media:%2Ftmp%2Fvoice.mp3) done'
    )
    expect(renderMediaTags('MEDIA:/tmp/demo.mp4')).toBe('[Video: demo.mp4](#media:%2Ftmp%2Fdemo.mp4)')
  })

  it('renders streamed assistant media once the tag is complete', () => {
    const parts = appendAssistantTextPart(appendAssistantTextPart([], 'ok\nMEDIA:'), '/tmp/voice.mp3')
    const text = chatMessageText({ id: 'a', role: 'assistant', parts })

    expect(text).toBe('ok\n[Audio: voice.mp3](#media:%2Ftmp%2Fvoice.mp3)')
  })
})

describe('interleaved reasoning/text boundaries', () => {
  it('preserves text → reasoning → text as three ordered activities', () => {
    let parts: ChatMessagePart[] = appendAssistantTextPart([], 'Let me ', 1)
    parts = appendReasoningPart(parts, 'checking the file...', 2)
    parts = appendAssistantTextPart(parts, 'verify the full file is correct:', 3)

    expect(parts.map(p => p.type)).toEqual(['text', 'reasoning', 'text'])
    expect(parts.map(p => p.timestamp)).toEqual([1, 2, 3])
    expect(parts.map(p => p.completedAt)).toEqual([2, 3, undefined])
  })

  it('preserves reasoning → text → reasoning as three ordered activities', () => {
    let parts: ChatMessagePart[] = appendReasoningPart([], 'first thought ', 1)
    parts = appendAssistantTextPart(parts, 'Working on it.', 2)
    parts = appendReasoningPart(parts, 'second thought', 3)

    expect(parts.map(p => p.type)).toEqual(['reasoning', 'text', 'reasoning'])
    expect(parts.map(p => p.timestamp)).toEqual([1, 2, 3])
    expect(parts.map(p => p.completedAt)).toEqual([2, 3, undefined])
  })

  it('starts a fresh text part after a tool call (segment boundary)', () => {
    let parts: ChatMessagePart[] = appendAssistantTextPart([], 'Let me check.', 10.125)
    parts = upsertToolPart(parts, { name: 'read_file', tool_id: 'tc-1' }, 'running', 11.25)
    parts = appendAssistantTextPart(parts, 'Now editing.', 12.5)

    expect(parts.map(p => p.type)).toEqual(['text', 'tool-call', 'text'])
    expect((parts[0] as { text: string }).text).toBe('Let me check.')
    expect((parts[2] as { text: string }).text).toBe('Now editing.')
    expect(parts.map(p => p.timestamp)).toEqual([10.125, 11.25, 12.5])
    expect(parts[0].completedAt).toBe(11.25)
  })

  it('does not merge reasoning across a tool call', () => {
    let parts: ChatMessagePart[] = appendReasoningPart([], 'before tool')
    parts = upsertToolPart(parts, { name: 'read_file', tool_id: 'tc-1' }, 'running')
    parts = appendReasoningPart(parts, 'after tool')

    expect(parts.map(p => p.type)).toEqual(['reasoning', 'tool-call', 'reasoning'])
    expect((parts[0] as { text: string }).text).toBe('before tool')
    expect((parts[2] as { text: string }).text).toBe('after tool')
  })
})

describe('preserveLocalAssistantErrors', () => {
  it('preserves richer live boundaries when the durable row has only its completion time', () => {
    const durable = toChatMessages([{ role: 'assistant', content: 'Done.', timestamp: 3 }])

    const live: ChatMessage[] = [
      {
        completedAt: 3,
        id: 'assistant-stream',
        parts: [{ completedAt: 3, text: 'Done.', timestamp: 1, type: 'text' }],
        role: 'assistant',
        timestamp: 1
      }
    ]

    const [message] = preserveLocalAssistantErrors(durable, live)

    expect([message.timestamp, message.completedAt]).toEqual([1, 3])
    expect(message.parts[0]).toMatchObject({ completedAt: 3, timestamp: 1, type: 'text' })
  })

  it('keeps one error card when the refresh rebuilt it from the failed-turn row', () => {
    const surface = { code: 'timeout', layer: 'provider', retryable: true } as const

    const nextMessages: ChatMessage[] = [
      { id: 'stored-user', parts: [{ text: 'new prompt', type: 'text' }], role: 'user' },
      { error: 'Connection error.', errorSurface: surface, id: 'stored-error', parts: [], role: 'assistant' },
      { id: 'stored-notice', parts: [{ text: 'Your request was not processed.', type: 'text' }], role: 'system' }
    ]

    const currentMessages: ChatMessage[] = [
      { id: 'user-123', parts: [{ text: 'new prompt', type: 'text' }], role: 'user' },
      { error: 'Connection error.', errorSurface: surface, id: 'assistant-error-1', parts: [], role: 'assistant' }
    ]

    const merged = preserveLocalAssistantErrors(nextMessages, currentMessages)

    expect(merged.filter(message => message.error)).toHaveLength(1)
  })

  it('preserves a local user+error pair when hydration omits the failed turn', () => {
    const nextMessages: ChatMessage[] = [
      {
        id: 'stored-user',
        parts: [{ text: 'earlier', type: 'text' }],
        role: 'user'
      }
    ]

    const currentMessages: ChatMessage[] = [
      {
        id: 'stored-user',
        parts: [{ text: 'earlier', type: 'text' }],
        role: 'user'
      },
      {
        id: 'user-123',
        parts: [{ text: 'new prompt', type: 'text' }],
        role: 'user'
      },
      {
        error: 'OpenRouter 403',
        id: 'assistant-error-1',
        parts: [],
        role: 'assistant'
      }
    ]

    const merged = preserveLocalAssistantErrors(nextMessages, currentMessages)

    expect(merged.map(message => message.id)).toEqual(['stored-user', 'user-123', 'assistant-error-1'])
    expect(merged[2]?.error).toBe('OpenRouter 403')
  })

  it('does not keep orphan local user turns when there is no inline assistant error', () => {
    const nextMessages: ChatMessage[] = [
      {
        id: 'stored-user',
        parts: [{ text: 'earlier', type: 'text' }],
        role: 'user'
      }
    ]

    const currentMessages: ChatMessage[] = [
      ...nextMessages,
      {
        id: 'user-123',
        parts: [{ text: 'new prompt', type: 'text' }],
        role: 'user'
      }
    ]

    const merged = preserveLocalAssistantErrors(nextMessages, currentMessages)

    expect(merged.map(message => message.id)).toEqual(['stored-user'])
  })

  it('does not duplicate local user when stored history already has equivalent text', () => {
    const nextMessages: ChatMessage[] = [
      {
        id: 'stored-user',
        parts: [{ text: 'hi', type: 'text' }],
        role: 'user'
      }
    ]

    const currentMessages: ChatMessage[] = [
      {
        id: 'optimistic-user',
        parts: [{ text: 'hi', type: 'text' }],
        role: 'user'
      },
      {
        error: 'OpenRouter 403',
        id: 'assistant-error-1',
        parts: [],
        role: 'assistant'
      }
    ]

    const merged = preserveLocalAssistantErrors(nextMessages, currentMessages)

    expect(merged.map(message => message.id)).toEqual(['stored-user', 'assistant-error-1'])
  })

  it('keeps local user when only older history has equivalent text', () => {
    const nextMessages: ChatMessage[] = [
      {
        id: 'older-user',
        parts: [{ text: 'hi', type: 'text' }],
        role: 'user'
      },
      {
        id: 'older-assistant',
        parts: [{ text: 'hello', type: 'text' }],
        role: 'assistant'
      },
      {
        id: 'tail-user',
        parts: [{ text: 'different prompt', type: 'text' }],
        role: 'user'
      }
    ]

    const currentMessages: ChatMessage[] = [
      {
        id: 'optimistic-user',
        parts: [{ text: 'hi', type: 'text' }],
        role: 'user'
      },
      {
        error: 'OpenRouter 403',
        id: 'assistant-error-1',
        parts: [],
        role: 'assistant'
      }
    ]

    const merged = preserveLocalAssistantErrors(nextMessages, currentMessages)

    expect(merged.map(message => message.id)).toEqual([
      'older-user',
      'older-assistant',
      'tail-user',
      'optimistic-user',
      'assistant-error-1'
    ])
  })

  it('keeps local assistant error when hydrated message reuses same id', () => {
    const nextMessages: ChatMessage[] = [
      {
        id: 'user-1',
        parts: [{ text: 'new prompt', type: 'text' }],
        role: 'user'
      },
      {
        id: 'assistant-stream-1',
        parts: [{ text: '', type: 'text' }],
        role: 'assistant'
      }
    ]

    const currentMessages: ChatMessage[] = [
      {
        id: 'user-1',
        parts: [{ text: 'new prompt', type: 'text' }],
        role: 'user'
      },
      {
        error: 'OpenRouter 403',
        id: 'assistant-stream-1',
        parts: [],
        role: 'assistant'
      }
    ]

    const merged = preserveLocalAssistantErrors(nextMessages, currentMessages)

    const assistant = merged.find(message => message.id === 'assistant-stream-1')

    expect(assistant?.error).toBe('OpenRouter 403')
    expect(assistant?.pending).toBe(false)
  })
})

describe('upsertToolPart', () => {
  it('preserves call time through progress and records completion time', () => {
    const started = upsertToolPart([], { name: 'read_file', tool_id: 'call-1' }, 'running', 100.125)

    const progressed = upsertToolPart(
      started,
      { name: 'read_file', preview: 'still reading', tool_id: 'call-1' },
      'running',
      101.5
    )

    const completed = upsertToolPart(
      progressed,
      { name: 'read_file', result: { content: 'done' }, tool_id: 'call-1' },
      'complete',
      102.875
    )

    expect(completed).toHaveLength(1)
    expect(completed[0].timestamp).toBe(100.125)
    expect(completed[0].completedAt).toBe(102.875)
  })

  it('closes active commentary when a tool starts', () => {
    const text = appendAssistantTextPart([], 'Checking first.', 20)
    const withTool = upsertToolPart(text, { name: 'read_file', tool_id: 'call-2' }, 'running', 21)

    expect(withTool[0].completedAt).toBe(21)
  })

  it('closes active commentary when only a tool completion arrives', () => {
    const text = appendAssistantTextPart([], 'Checking first.', 20)
    const withTool = upsertToolPart(text, { name: 'read_file', tool_id: 'call-2' }, 'complete', 21)

    expect(withTool[0].completedAt).toBe(21)
  })

  it('closes unfinished timeline segments when a turn completes', () => {
    const parts = appendAssistantTextPart([], 'Done.', 30.25)

    expect(completeOpenTimelineParts(parts, 31.75)[0].completedAt).toBe(31.75)
  })

  it('preserves inline diffs from tool completion events', () => {
    const parts = upsertToolPart(
      [],
      {
        inline_diff: '--- a/foo.ts\n+++ b/foo.ts\n@@\n-old\n+new',
        name: 'patch',
        tool_id: 'tool-1'
      },
      'complete'
    )

    const [part] = parts

    expect(part?.type).toBe('tool-call')
    expect(part && 'result' in part ? toolResultRecord(part) : undefined).toMatchObject({
      inline_diff: '--- a/foo.ts\n+++ b/foo.ts\n@@\n-old\n+new'
    })
  })

  it('keeps live todo rows stable across sparse progress payloads', () => {
    const first = upsertToolPart(
      [],
      {
        name: 'todo',
        todos: [{ content: 'Boil water', id: 'boil', status: 'in_progress' }],
        tool_id: 'todo-1'
      },
      'running'
    )

    const progressed = upsertToolPart(
      first,
      {
        name: 'todo',
        preview: 'updating plan',
        tool_id: 'todo-1'
      },
      'running'
    )

    const [part] = progressed
    const args = part && 'args' in part ? (part.args as Record<string, unknown>) : {}

    expect(args.todos).toEqual([{ content: 'Boil water', id: 'boil', status: 'in_progress' }])
  })

  it('archives todo state on completion and accepts explicit empty clears', () => {
    const started = upsertToolPart(
      [],
      {
        name: 'todo',
        todos: [{ content: 'Boil water', id: 'boil', status: 'in_progress' }],
        tool_id: 'todo-1'
      },
      'running'
    )

    const completed = upsertToolPart(
      started,
      {
        name: 'todo',
        tool_id: 'todo-1'
      },
      'complete'
    )

    const cleared = upsertToolPart(
      completed,
      {
        name: 'todo',
        todos: [],
        tool_id: 'todo-1'
      },
      'complete'
    )

    const completedResult = completed[0] && 'result' in completed[0] ? toolResultRecord(completed[0]) : {}

    const clearedResult = cleared[0] && 'result' in cleared[0] ? toolResultRecord(cleared[0]) : {}

    expect(completedResult.todos).toEqual([{ content: 'Boil water', id: 'boil', status: 'in_progress' }])
    expect(clearedResult.todos).toEqual([])
  })

  it('keeps parallel same-name tools distinct without explicit ids', () => {
    const startedTokyo = upsertToolPart(
      [],
      {
        context: 'tokyo weather',
        name: 'web_search'
      },
      'running'
    )

    const startedReykjavik = upsertToolPart(
      startedTokyo,
      {
        context: 'reykjavik weather',
        name: 'web_search'
      },
      'running'
    )

    const completedTokyo = upsertToolPart(
      startedReykjavik,
      {
        context: 'tokyo weather',
        message: 'tokyo done',
        name: 'web_search',
        summary: 'Did 5 searches'
      },
      'complete'
    )

    const completedBoth = upsertToolPart(
      completedTokyo,
      {
        context: 'reykjavik weather',
        message: 'reykjavik done',
        name: 'web_search',
        summary: 'Did 5 searches'
      },
      'complete'
    )

    const webParts = completedBoth.filter(
      (part): part is Extract<ChatMessagePart, { type: 'tool-call' }> =>
        part.type === 'tool-call' && part.toolName === 'web_search'
    )

    const contexts = webParts.map(part => String((part.args as Record<string, unknown>)?.context || ''))

    const summaries = webParts.map(part => String(toolResultRecord(part).summary || ''))

    expect(webParts).toHaveLength(2)
    expect(contexts).toEqual(['tokyo weather', 'reykjavik weather'])
    expect(summaries).toEqual(['Did 5 searches', 'Did 5 searches'])
  })

  it('pairs a terminal completion with its context-only start when event IDs differ', () => {
    const started = upsertToolPart(
      [],
      { context: 'echo "Hello from the terminal"', name: 'terminal', tool_id: 'terminal-start' },
      'running'
    )

    const completed = upsertToolPart(
      started,
      {
        args: { command: 'echo "Hello from the terminal"' },
        name: 'terminal',
        result: { exit_code: 0, stdout: 'Hello from the terminal' },
        tool_id: 'terminal-complete'
      },
      'complete'
    )

    const terminalParts = completed.filter(
      (part): part is Extract<ChatMessagePart, { type: 'tool-call' }> =>
        part.type === 'tool-call' && part.toolName === 'terminal'
    )

    expect(terminalParts).toHaveLength(1)
    expect(terminalParts[0]?.toolCallId).toBe('terminal-complete')
    expect(terminalParts[0] && 'result' in terminalParts[0] ? terminalParts[0].result : undefined).toMatchObject({
      exit_code: 0,
      stdout: 'Hello from the terminal'
    })
  })

  it('preserves query args when completion payload omits context', () => {
    const started = upsertToolPart(
      [],
      {
        context: 'auckland weather today and tomorrow forecast',
        name: 'web_search',
        tool_id: 'search-1'
      },
      'running'
    )

    const completed = upsertToolPart(
      started,
      {
        duration_s: 1.1,
        name: 'web_search',
        summary: 'Did 5 searches in 1.1s',
        tool_id: 'search-1'
      },
      'complete'
    )

    const [part] = completed

    expect(part?.type).toBe('tool-call')
    expect((part as Extract<ChatMessagePart, { type: 'tool-call' }>).args).toMatchObject({
      context: 'auckland weather today and tomorrow forecast'
    })
    expect(toolResultRecord(part as Extract<ChatMessagePart, { type: 'tool-call' }>)).toMatchObject({
      summary: 'Did 5 searches in 1.1s'
    })
  })

  it('does not append phantom same-name tool rows for id-less progress updates', () => {
    const startedA = upsertToolPart(
      [],
      {
        context: 'reykjavik weather today and tomorrow forecast',
        name: 'web_search'
      },
      'running'
    )

    const startedB = upsertToolPart(
      startedA,
      {
        context: 'kathmandu weather today and tomorrow forecast',
        name: 'web_search'
      },
      'running'
    )

    const progressed = upsertToolPart(
      startedB,
      {
        name: 'web_search'
      },
      'running'
    )

    const webParts = progressed.filter(
      (part): part is Extract<ChatMessagePart, { type: 'tool-call' }> =>
        part.type === 'tool-call' && part.toolName === 'web_search'
    )

    expect(webParts).toHaveLength(2)
  })

  it('matches id-less live starts with later identified completions', () => {
    const started = upsertToolPart(
      [],
      {
        context: 'asuncion paraguay weather today and tomorrow forecast',
        name: 'web_search'
      },
      'running'
    )

    const completed = upsertToolPart(
      started,
      {
        context: 'asuncion paraguay weather today and tomorrow forecast',
        duration_s: 1.1,
        name: 'web_search',
        summary: 'Did 5 searches in 1.1s',
        tool_id: 'search-asuncion'
      },
      'complete'
    )

    const webParts = completed.filter(
      (part): part is Extract<ChatMessagePart, { type: 'tool-call' }> =>
        part.type === 'tool-call' && part.toolName === 'web_search'
    )

    expect(webParts).toHaveLength(1)
    expect(webParts[0].toolCallId).toBe('search-asuncion')
    expect(toolResultRecord(webParts[0])).toMatchObject({ summary: 'Did 5 searches in 1.1s' })
  })

  it('matches id-less live starts with later identified progress updates', () => {
    const started = upsertToolPart(
      [],
      {
        context: 'reykjavik tashkent uzbekistan weather today and tomorrow forecast',
        name: 'web_search'
      },
      'running'
    )

    const progressed = upsertToolPart(
      started,
      {
        context: 'reykjavik tashkent uzbekistan weather today and tomorrow forecast',
        name: 'web_search',
        tool_id: 'search-reykjavik'
      },
      'running'
    )

    const webParts = progressed.filter(
      (part): part is Extract<ChatMessagePart, { type: 'tool-call' }> =>
        part.type === 'tool-call' && part.toolName === 'web_search'
    )

    expect(webParts).toHaveLength(1)
    expect(webParts[0].toolCallId).toBe('search-reykjavik')
  })

  it('reconciles preview-first progress rows with later stable-id starts', () => {
    const progressA = upsertToolPart(
      [],
      {
        name: 'web_search',
        preview: 'tokyo weather'
      },
      'running'
    )

    const progressB = upsertToolPart(
      progressA,
      {
        name: 'web_search',
        preview: 'reykjavik weather'
      },
      'running'
    )

    const startedA = upsertToolPart(
      progressB,
      {
        args: { query: 'tokyo weather' },
        name: 'web_search',
        tool_id: 'search-tokyo'
      },
      'running'
    )

    const startedB = upsertToolPart(
      startedA,
      {
        args: { query: 'reykjavik weather' },
        name: 'web_search',
        tool_id: 'search-reykjavik'
      },
      'running'
    )

    const completedA = upsertToolPart(
      startedB,
      {
        name: 'web_search',
        summary: 'Did 5 searches',
        tool_id: 'search-tokyo'
      },
      'complete'
    )

    const completedB = upsertToolPart(
      completedA,
      {
        name: 'web_search',
        summary: 'Did 5 searches',
        tool_id: 'search-reykjavik'
      },
      'complete'
    )

    const webParts = completedB
      .filter(
        (part): part is Extract<ChatMessagePart, { type: 'tool-call' }> =>
          part.type === 'tool-call' && part.toolName === 'web_search'
      )
      .map(part => ({
        id: part.toolCallId,
        query: String((part.args as Record<string, unknown>)?.query || ''),
        summary: String(toolResultRecord(part).summary || '')
      }))

    expect(webParts).toEqual([
      { id: 'search-tokyo', query: 'tokyo weather', summary: 'Did 5 searches' },
      { id: 'search-reykjavik', query: 'reykjavik weather', summary: 'Did 5 searches' }
    ])
  })
})

describe('mergeFinalAssistantText', () => {
  it('keeps confirmed text-reasoning-text boundaries in the final response', () => {
    let parts: ChatMessagePart[] = appendAssistantTextPart([], 'First. ', 1)
    parts = appendReasoningPart(parts, 'Think.', 2)
    parts = appendAssistantTextPart(parts, 'Last.', 3)

    const result = mergeFinalAssistantText(parts, 'First. Last.', 4)

    expect(result.map(part => part.type)).toEqual(['text', 'reasoning', 'text'])
    expect(result.map(part => part.timestamp)).toEqual([1, 2, 3])
  })

  it('timestamps completion-only text when no streamed text preceded it', () => {
    const result = mergeFinalAssistantText([], 'final answer', 12.5)

    expect(result[0]).toMatchObject({ text: 'final answer', timestamp: 12.5, type: 'text' })
  })

  it('preserves pre-tool text and appends the later final response', () => {
    // These deltas precede the tool call: they belong to an earlier model
    // response, not to the provisional draft of the final being settled.
    const parts = [
      { type: 'text' as const, text: 'streamed delta 1' },
      { type: 'text' as const, text: 'streamed delta 2' },
      { type: 'tool-call' as const, toolCallId: 'tc1', toolName: 'terminal', args: {} as never, argsText: '{}' }
    ]

    const result = mergeFinalAssistantText(parts, 'final answer')

    expect(result.filter(p => p.type === 'text').map(p => p.text)).toEqual([
      'streamed delta 1',
      'streamed delta 2',
      'final answer'
    ])
    expect(result.some(p => p.type === 'tool-call')).toBe(true)
  })

  it('drops reasoning that the final text fully covers (reasoning ⊆ final)', () => {
    const parts = [reasoningPart('Let me check the files.'), { type: 'text' as const, text: 'streamed' }]

    const result = mergeFinalAssistantText(parts, 'Let me check the files. Everything looks good.')

    expect(result.filter(p => p.type === 'reasoning')).toHaveLength(0)
    expect(result.filter(p => p.type === 'text')).toHaveLength(1)
  })

  it('keeps a longer reasoning block when the final text is only a short prefix', () => {
    // #61447: a short final ("Done.") must NOT swallow a longer reasoning block
    // that merely starts with it.
    const parts = [
      reasoningPart(
        'Done. The root cause was a bare catch block swallowing Stripe errors. The fix adds proper error logging.'
      ),
      { type: 'text' as const, text: 'streamed' }
    ]

    const result = mergeFinalAssistantText(parts, 'Done.')

    expect(result.filter(p => p.type === 'reasoning')).toHaveLength(1)
    expect(result.filter(p => p.type === 'text')[0]).toMatchObject({ text: 'Done.' })
  })

  it('keeps non-restating reasoning', () => {
    const parts = [
      reasoningPart('I analyzed the issue and found a race condition in the event loop.'),
      { type: 'text' as const, text: 'streamed' }
    ]

    const result = mergeFinalAssistantText(parts, 'Fixed the race condition.')

    expect(result.filter(p => p.type === 'reasoning')).toHaveLength(1)
    expect(result.filter(p => p.type === 'text')).toHaveLength(1)
  })

  it('does not erase streamed text when the final completion is empty (#95514)', () => {
    const parts = [{ type: 'text' as const, text: 'streamed' }, reasoningPart('some reasoning')]

    const result = mergeFinalAssistantText(parts, '')

    expect(result.filter(p => p.type === 'text')).toHaveLength(1)
    expect(result.filter(p => p.type === 'text')[0]).toMatchObject({ text: 'streamed' })
    expect(result.filter(p => p.type === 'reasoning')).toHaveLength(1)
  })

  it('treats whitespace-only final text as non-authoritative (#95514)', () => {
    const parts = [{ type: 'text' as const, text: 'already on screen' }]

    const result = mergeFinalAssistantText(parts, '   \n\t')

    expect(result.filter(p => p.type === 'text')).toHaveLength(1)
    expect(result.filter(p => p.type === 'text')[0]).toMatchObject({ text: 'already on screen' })
  })
})

describe('collectUnspokenTurnSpeech', () => {
  const assistant = (id: string, text: string, extra: Partial<ChatMessage> = {}): ChatMessage => ({
    id,
    role: 'assistant',
    parts: text ? [{ type: 'text', text }] : [],
    ...extra
  })

  const user = (id: string, text: string): ChatMessage => ({
    id,
    role: 'user',
    parts: [{ type: 'text', text }]
  })

  it('includes sealed interim narration AND the final answer of a tool-calling turn', () => {
    const messages = [
      user('u1', 'what time is it?'),
      assistant('a1', 'Let me check the clock.', { interim: true }),
      assistant('a2', 'It is 9 PM.')
    ]

    const speech = collectUnspokenTurnSpeech(messages, null)

    expect(speech).not.toBeNull()
    expect(speech?.id).toBe('a1')
    expect(speech?.text).toBe('Let me check the clock.\n\nIt is 9 PM.')
    expect(speech?.pending).toBe(false)
  })

  it('keeps the binding id stable while later bubbles stream in', () => {
    const turnStart = [user('u1', 'go'), assistant('a1', 'Let me check.', { interim: true })]
    const first = collectUnspokenTurnSpeech(turnStart, null)

    const turnLater = [...turnStart, assistant('a2', 'Still work', { pending: true })]
    const later = collectUnspokenTurnSpeech(turnLater, null)

    expect(first?.id).toBe('a1')
    expect(later?.id).toBe('a1')
    // The earlier snapshot's text is a prefix of the later one — the live
    // session appends by length, so aggregation must be append-only.
    expect(later?.text.startsWith(first?.text ?? '')).toBe(true)
    expect(later?.pending).toBe(true)
  })

  it('starts after the last spoken message and skips hidden/empty bubbles', () => {
    const messages = [
      assistant('a0', 'Spoken last turn.'),
      user('u1', 'next'),
      assistant('a1', '', { pending: false }),
      assistant('a2', 'hidden note', { hidden: true }),
      assistant('a3', 'The real reply.')
    ]

    const speech = collectUnspokenTurnSpeech(messages, 'a0')

    expect(speech?.id).toBe('a3')
    expect(speech?.text).toBe('The real reply.')
  })

  it('reports pending from the newest assistant bubble even when it has no text yet', () => {
    const messages = [assistant('a1', 'Narration done.', { interim: true }), assistant('a2', '', { pending: true })]

    const speech = collectUnspokenTurnSpeech(messages, null)

    expect(speech?.id).toBe('a1')
    expect(speech?.text).toBe('Narration done.')
    expect(speech?.pending).toBe(true)
  })

  it('returns null when everything is spoken or there is no assistant text', () => {
    expect(collectUnspokenTurnSpeech([], null)).toBeNull()
    expect(collectUnspokenTurnSpeech([assistant('a1', 'Done.')], 'a1')).toBeNull()
    expect(collectUnspokenTurnSpeech([user('u1', 'hello'), assistant('a1', '')], null)).toBeNull()
  })

  it('does not replay earlier turns when the spoken id is missing or stale', () => {
    const messages = [
      user('u1', 'old question'),
      assistant('a1', 'Previous output from last turn.'),
      user('u2', 'new question'),
      assistant('a2', 'Live reply only.')
    ]

    expect(collectUnspokenTurnSpeech(messages, null)?.text).toBe('Live reply only.')
    expect(collectUnspokenTurnSpeech(messages, 'vanished-stream-id')?.text).toBe('Live reply only.')
    expect(collectUnspokenTurnSpeech(messages, 'a1')?.text).toBe('Live reply only.')
  })

  it('bounds to a hidden user turn too (widget intents render no bubble)', () => {
    const messages = [
      user('u1', 'old question'),
      assistant('a1', 'Previous output from last turn.'),
      { ...user('u2', 'widget intent'), hidden: true },
      assistant('a2', 'Live reply only.')
    ]

    expect(collectUnspokenTurnSpeech(messages, null)?.text).toBe('Live reply only.')
  })
})

describe('stripPendingClarifyProjectionForCache', () => {
  const clarifyPart = (toolCallId: string): ChatMessagePart => ({
    type: 'tool-call',
    toolCallId,
    toolName: 'clarify',
    args: { choices: ['a'], question: 'Pick' },
    argsText: '{"question":"Pick","choices":["a"]}'
  })

  it('drops a synthetic request-id-only clarify row from the durable cache', () => {
    const messages: ChatMessage[] = [
      { id: 'user', role: 'user', parts: [{ type: 'text', text: 'choose' }] },
      { id: 'synthetic', role: 'assistant', parts: [clarifyPart('req-1')], pending: true }
    ]

    expect(stripPendingClarifyProjectionForCache(messages, 'req-1')).toEqual([messages[0]])
  })

  it('keeps a provider-authored clarify in position but strips its local running bit', () => {
    const messages: ChatMessage[] = [
      {
        id: 'provider',
        role: 'assistant',
        parts: [{ type: 'text', text: 'Choose.' }, clarifyPart('call-provider')],
        pending: true
      }
    ]

    const [cached] = stripPendingClarifyProjectionForCache(messages, 'req-1')
    expect(cached.pending).toBe(false)
    expect(cached.parts.map(part => part.type)).toEqual(['text', 'tool-call'])
  })
})

describe('sealOpenToolParts', () => {
  const toolPart = (over: Partial<ChatMessagePart> = {}): ChatMessagePart =>
    ({
      type: 'tool-call',
      toolCallId: 'call-1',
      toolName: 'terminal',
      args: {},
      argsText: '{}',
      ...over
    }) as ChatMessagePart

  const assistantWithParts = (parts: ChatMessagePart[], over: Partial<ChatMessage> = {}): ChatMessage =>
    ({
      id: 'a1',
      role: 'assistant',
      parts,
      ...over
    }) as ChatMessage

  it('a sealed clarify never becomes the fallback row for a new, uncorrelated clarify request', () => {
    // Turn 1 blocked on a clarify, the user stopped it; settle sealed the call
    // (no result). A later turn raises a *different* clarify whose request id
    // and question match nothing on the transcript.
    const stopped = sealOpenToolParts([
      assistantWithParts(
        upsertToolPart(
          [],
          { tool_id: 'old-provider-id', name: 'clarify', args: { question: 'Old question?', choices: ['A', 'B'] } },
          'running',
          1
        ),
        { id: 'old-turn', pending: false }
      )
    ])

    const messages = [
      ...stopped,
      { id: 'u2', role: 'user', parts: [{ type: 'text', text: 'ask something else' }] } as ChatMessage
    ]

    const restored = restorePendingClarifyToolCall(
      messages,
      { id: 'new-request-id', name: 'clarify', args: { question: 'New question?', choices: ['C', 'D'] } },
      3
    )

    expect(restored.streamId).not.toBe('old-turn')
    const oldTurn = restored.messages.find(message => message.id === 'old-turn')
    expect(oldTurn?.pending).not.toBe(true)

    const newQuestion = restored.messages
      .flatMap(message => message.parts)
      .find(part => part.type === 'tool-call' && part.toolCallId === 'new-request-id')

    expect(newQuestion).toBeDefined()
  })

  it('a sealed clarify is still re-armed when the resume request genuinely correlates to it', () => {
    const stopped = sealOpenToolParts([
      assistantWithParts(
        upsertToolPart(
          [],
          { tool_id: 'provider-id', name: 'clarify', args: { question: 'Same question?', choices: ['A', 'B'] } },
          'running',
          1
        ),
        { id: 'turn', pending: false }
      )
    ])

    const restored = restorePendingClarifyToolCall(
      stopped,
      { id: 'request-id', name: 'clarify', args: { question: 'Same question?', choices: ['A', 'B'] } },
      2
    )

    expect(restored.streamId).toBe('turn')
    expect(restored.messages[0].pending).toBe(true)
    expect(restored.messages).toHaveLength(1)
    // The seal comes off so the row renders as the live question again.
    expect(restored.messages[0].parts[0].completedAt).toBeUndefined()
  })

  it('seals open tool-call parts in settled assistant messages', () => {
    const messages = [assistantWithParts([toolPart()])]

    const next = sealOpenToolParts(messages)

    expect(next[0].parts[0]).not.toHaveProperty('result')
    expect(next[0].parts[0].completedAt).toBeDefined()
  })

  it('leaves pending messages alone', () => {
    const messages = [assistantWithParts([toolPart()], { pending: true })]

    const next = sealOpenToolParts(messages)

    expect(next[0].parts[0].completedAt).toBeUndefined()
  })

  it('leaves non-tool parts untouched', () => {
    const text = { type: 'text', text: 'hello' } as ChatMessagePart
    const messages = [assistantWithParts([text, toolPart()])]

    const next = sealOpenToolParts(messages)

    expect(next[0].parts[0]).toBe(text)
    expect(next[0].parts[1]).not.toHaveProperty('result')
    expect(next[0].parts[1].completedAt).toBeDefined()
  })

  it('returns the same array reference when nothing needs sealing', () => {
    const done = toolPart({ result: { code: 0 } })
    const messages = [assistantWithParts([done])]

    expect(sealOpenToolParts(messages)).toBe(messages)
  })
})

describe('toChatMessages backend-row accounting', () => {
  it('reports the backend rows a folded turn stands for', () => {
    // The older-page offset (transcript-tail) is counted in BACKEND rows, and
    // this fold is what makes a message not one row: one assistant row plus its
    // tool rows, plus a second assistant row that merges into the same bubble.
    const messages = toChatMessages([
      {
        role: 'assistant',
        content: 'Running the checks.',
        timestamp: 1,
        tool_calls: [{ id: 'tc', function: { name: 'terminal', arguments: '{}' } }]
      },
      { role: 'tool', tool_call_id: 'tc', content: 'ok', timestamp: 2 },
      { role: 'assistant', content: 'Done.', timestamp: 3 }
    ])

    expect(messages).toHaveLength(1)
    expect(messages[0].serverRowSpan).toBe(3)

    // A row that stands for one backend row carries no span at all.
    const plain = toChatMessages([{ role: 'user', content: 'hi', timestamp: 1 }])

    expect(plain).toHaveLength(1)
    expect(plain[0]).not.toHaveProperty('serverRowSpan')
  })

  it('counts the current answer row when a page starts on a tool-only turn', () => {
    // A page boundary can start mid-turn: the first row is the tool-only
    // assistant, the second its result, and the answer arrives third with no
    // active bubble to append to. The bubble it creates stands for all three
    // backend rows — counting only the two pending ones would make the release
    // rewind short and skip history the reader then cannot reach.
    const messages = toChatMessages([
      {
        role: 'assistant',
        content: '',
        timestamp: 1,
        tool_calls: [{ id: 'tc', function: { name: 'terminal', arguments: '{}' } }]
      },
      { role: 'tool', tool_call_id: 'tc', content: 'ok', timestamp: 2 },
      { role: 'assistant', content: 'Answer.', timestamp: 3 }
    ])

    expect(messages).toHaveLength(1)
    expect(messages[0].serverRowSpan).toBe(3)
  })
})