Skip to content

armgit / orca

Public Imported from GitHub
0

Orca is the place for working with a fleet of agents (e.g. Grok Bot, Claude Code). Run any coding agent with your own subscription. Available on desktop, mobile and remote runtime.

32.2 KB
Raw Blame
import { afterEach, describe, it, expect, vi } from 'vitest'
import {
  mainAgentStatusEqual,
  agentSubagentsEqual,
  isFreshNonDoneAgentStatus,
  parseAgentStatusPayload,
  normalizeAgentStatusPayload,
  pickParsedAgentStatusPayload,
  AGENT_STATUS_JSON_STRUCTURE_LIMITS,
  AGENT_STATUS_MAX_FIELD_LENGTH,
  AGENT_STATUS_TOOL_NAME_MAX_LENGTH,
  AGENT_STATUS_TOOL_INPUT_MAX_LENGTH,
  AGENT_STATUS_ASSISTANT_MESSAGE_MAX_LENGTH,
  AGENT_STATUS_INTERACTIVE_PROMPT_MAX_LENGTH,
  AGENT_STATUS_STATES,
  AGENT_TYPE_MAX_LENGTH
} from './agent-status-types'
import type { AgentType } from './agent-status-types'
import {
  ORCA_DISPATCH_PROMPT_LEAD_LINE,
  ORCA_DISPATCH_STATUS_PREAMBLE_PREFIX
} from './orca-dispatch-status-prompt'
import { parseAskFromStatus } from './native-chat/input/native-chat-ask'

afterEach(() => {
  vi.restoreAllMocks()
})

describe('isFreshNonDoneAgentStatus', () => {
  it('treats a within-TTL working entry as fresh', () => {
    expect(isFreshNonDoneAgentStatus({ state: 'working', updatedAt: 1_000 }, 2_000)).toBe(true)
  })

  it('never treats a restored-unconfirmed entry as fresh, regardless of age', () => {
    expect(
      isFreshNonDoneAgentStatus(
        { state: 'working', updatedAt: 1_999, restoredUnconfirmed: true },
        2_000
      )
    ).toBe(false)
  })

  it('stays false for done and for stale entries', () => {
    expect(isFreshNonDoneAgentStatus({ state: 'done', updatedAt: 2_000 }, 2_000)).toBe(false)
    expect(isFreshNonDoneAgentStatus({ state: 'working', updatedAt: 0 }, 10_000, 5_000)).toBe(false)
  })
})

describe('parseAgentStatusPayload', () => {
  it('parses a valid working payload', () => {
    const result = parseAgentStatusPayload(
      '{"state":"working","prompt":"Fix the flaky assertion","agentType":"codex"}'
    )
    expect(result).toEqual({
      state: 'working',
      prompt: 'Fix the flaky assertion',
      agentType: 'codex'
    })
  })

  it('parses all AGENT_STATUS_STATES', () => {
    for (const state of AGENT_STATUS_STATES) {
      const result = parseAgentStatusPayload(`{"state":"${state}"}`)
      expect(result).not.toBeNull()
      expect(result!.state).toBe(state)
    }
  })

  it('accepts monitoring only as an optional working discriminator', () => {
    expect(parseAgentStatusPayload('{"state":"working","workingMode":"monitoring"}')).toMatchObject(
      { state: 'working', workingMode: 'monitoring' }
    )
    expect(
      parseAgentStatusPayload('{"state":"done","workingMode":"monitoring"}')?.workingMode
    ).toBeUndefined()
    expect(
      parseAgentStatusPayload('{"state":"working","workingMode":"unknown"}')?.workingMode
    ).toBeUndefined()
    expect(parseAgentStatusPayload('{"state":"working"}')?.workingMode).toBeUndefined()
  })

  it('returns null for invalid state', () => {
    expect(parseAgentStatusPayload('{"state":"running"}')).toBeNull()
    expect(parseAgentStatusPayload('{"state":"idle"}')).toBeNull()
    expect(parseAgentStatusPayload('{"state":""}')).toBeNull()
  })

  it('returns null when state is a non-string type', () => {
    expect(parseAgentStatusPayload('{"state":123}')).toBeNull()
    expect(parseAgentStatusPayload('{"state":true}')).toBeNull()
    expect(parseAgentStatusPayload('{"state":null}')).toBeNull()
  })

  it('returns null for invalid JSON', () => {
    expect(parseAgentStatusPayload('not json')).toBeNull()
    expect(parseAgentStatusPayload('{broken')).toBeNull()
    expect(parseAgentStatusPayload('')).toBeNull()
  })

  it('rejects excessive nesting before JSON.parse', () => {
    const parseSpy = vi.spyOn(JSON, 'parse')
    const depth = AGENT_STATUS_JSON_STRUCTURE_LIMITS.nestingDepth + 1
    try {
      expect(parseAgentStatusPayload(`${'['.repeat(depth)}0${']'.repeat(depth)}`)).toBeNull()
      expect(parseSpy).not.toHaveBeenCalled()
    } finally {
      parseSpy.mockRestore()
    }
  })

  it('returns null for non-object JSON', () => {
    expect(parseAgentStatusPayload('"just a string"')).toBeNull()
    expect(parseAgentStatusPayload('42')).toBeNull()
    expect(parseAgentStatusPayload('null')).toBeNull()
    expect(parseAgentStatusPayload('[]')).toBeNull()
  })

  it('normalizes multiline prompt to single line', () => {
    const result = parseAgentStatusPayload(
      '{"state":"working","prompt":"line one\\nline two\\nline three"}'
    )
    expect(result!.prompt).toBe('line one line two line three')
  })

  it('normalizes Windows-style line endings (\\r\\n) to single line', () => {
    const result = parseAgentStatusPayload(
      '{"state":"working","prompt":"line one\\r\\nline two\\r\\nline three"}'
    )
    expect(result!.prompt).toBe('line one line two line three')
  })

  it('trims whitespace from the prompt field', () => {
    const result = parseAgentStatusPayload('{"state":"working","prompt":"  padded  "}')
    expect(result!.prompt).toBe('padded')
  })

  it('truncates the prompt beyond max length', () => {
    const longString = 'x'.repeat(300)
    const result = parseAgentStatusPayload(`{"state":"working","prompt":"${longString}"}`)
    expect(result!.prompt).toHaveLength(AGENT_STATUS_MAX_FIELD_LENGTH)
  })

  // Why: dispatch preambles bury the task body after multi-KB CLI text; naive head-truncation would keep only boilerplate.
  it('compacts Orca dispatch preambles so the task body survives 200-char truncation', () => {
    const longCliNoise = Array.from(
      { length: 50 },
      (_, i) => `orca orchestration send --to term_parent --type heartbeat --phase step-${i}`
    ).join('\n')
    const result = parseAgentStatusPayload(
      JSON.stringify({
        state: 'working',
        prompt: `You are working inside Orca, a multi-agent IDE. You are a dispatched worker.
Your task ID is: task_compact_1

=== CLI COMMANDS ===
${longCliNoise}

=== TASK ===
Fix dispatch fallback preview for normalized status prompts`
      })
    )
    expect(result).not.toBeNull()
    expect(result!.prompt.length).toBeLessThanOrEqual(AGENT_STATUS_MAX_FIELD_LENGTH)
    expect(result!.prompt.includes('\n')).toBe(false)
    expect(result!.prompt.startsWith('You are working inside Orca, a multi-agent IDE.')).toBe(true)
    expect(result!.prompt).toContain('Your task ID is: task_compact_1')
    expect(result!.prompt).toContain('=== TASK ===')
    expect(result!.prompt).toContain('Fix dispatch fallback preview')
    expect(result!.prompt).not.toContain('CLI COMMANDS')
    expect(result!.prompt).not.toContain('heartbeat')
  })

  it('ignores task-marker text inside base-drift commit subjects', () => {
    const result = normalizeAgentStatusPayload({
      state: 'working',
      // Why: CRLF covers Windows hook payloads; commit text must not impersonate the task separator.
      prompt: [
        'You are working inside Orca, a multi-agent IDE. You are a dispatched worker.',
        'Your task ID is: task_drift_marker',
        '',
        '--- BASE DRIFT ---',
        '  - docs: explain === TASK === marker parsing',
        '---',
        '',
        '=== TASK ===',
        'Fix the actual dispatch fallback preview'
      ].join('\r\n')
    })

    expect(result!.prompt).toContain('=== TASK === Fix the actual dispatch fallback preview')
    expect(result!.prompt).not.toContain('marker parsing')
  })

  it('compacts a Claude hook prompt carrying the typed lead line and paste wrapper', () => {
    const preamble = `${ORCA_DISPATCH_STATUS_PREAMBLE_PREFIX}\nYour task ID is: task_lead\n\n=== TASK ===\nAdd greet()`
    const normalize = (prompt: string): string =>
      normalizeAgentStatusPayload({ state: 'working', prompt })!.prompt
    const compact = `${ORCA_DISPATCH_STATUS_PREAMBLE_PREFIX} Your task ID is: task_lead === TASK === Add greet()`

    // Why: the shape Claude Code's UserPromptSubmit hook reports for a typed lead plus a paste.
    expect(
      normalize(
        `${ORCA_DISPATCH_PROMPT_LEAD_LINE}\n\n<pasted_content id="aac2">\n${preamble}\n</pasted_content id="aac2">\n`
      )
    ).toBe(compact)
    expect(normalize(`\n\n<pasted_content id="965a">\n${preamble}`)).toBe(compact)
    expect(normalize(`<pasted_content ${'x'.repeat(80)}>${preamble}`)).not.toBe(compact)
    expect(normalize(`<pasted_content ${'x'.repeat(30_000)}`)).not.toContain('TASK')
    expect(normalize(`please review: ${preamble}`)).toBe(
      `please review: ${preamble.replace(/\n+/g, ' ')}`
    )
    // Why: the closing paste tag must not become the task body of an empty spec.
    expect(
      normalize(
        `<pasted_content id="1">\n${ORCA_DISPATCH_STATUS_PREAMBLE_PREFIX}\n=== TASK ===\n</pasted_content id="1">`
      )
    ).toBe(ORCA_DISPATCH_STATUS_PREAMBLE_PREFIX)
  })

  it('keeps dispatch detection bounded for oversized whitespace prompts', () => {
    const trimStartSpy = vi.spyOn(String.prototype, 'trimStart')
    const prompt = ' '.repeat(1_000_000)

    expect(normalizeAgentStatusPayload({ state: 'working', prompt })!.prompt).toBe('')
    expect(
      trimStartSpy.mock.contexts.some((context) => String(context).length === prompt.length)
    ).toBe(false)
  })

  it('defaults missing prompt to empty string', () => {
    const result = parseAgentStatusPayload('{"state":"done"}')
    expect(result!.prompt).toBe('')
  })

  it('handles non-string prompt gracefully', () => {
    const result = parseAgentStatusPayload('{"state":"working","prompt":42}')
    expect(result!.prompt).toBe('')
  })

  it('accepts custom non-empty agentType values', () => {
    const agentType: AgentType = 'some-in-house-agent'
    const result = parseAgentStatusPayload(JSON.stringify({ state: 'working', agentType }))
    expect(result).toEqual({
      state: 'working',
      prompt: '',
      agentType
    })
  })

  it('truncates agentType beyond AGENT_TYPE_MAX_LENGTH', () => {
    const longAgentType = 'a'.repeat(AGENT_TYPE_MAX_LENGTH + 20)
    const result = parseAgentStatusPayload(
      JSON.stringify({ state: 'working', agentType: longAgentType })
    )
    expect(result!.agentType).toHaveLength(AGENT_TYPE_MAX_LENGTH)
  })

  it('treats whitespace-only agentType as undefined', () => {
    const result = parseAgentStatusPayload('{"state":"working","agentType":"   "}')
    expect(result!.agentType).toBeUndefined()
  })

  it('collapses newlines in agentType (single-line field)', () => {
    // Why: agentType is single-line; a newline must not leak into UI rendering or equality checks.
    const result = parseAgentStatusPayload('{"state":"working","agentType":"claude\\nrogue"}')
    expect(result!.agentType).toBe('claude rogue')
  })

  it('parses toolName, toolInput, and lastAssistantMessage', () => {
    const result = parseAgentStatusPayload(
      JSON.stringify({
        state: 'working',
        toolName: 'Edit',
        toolInput: '/path/to/file.ts',
        lastAssistantMessage: 'Here is the edit I made.'
      })
    )
    expect(result).toEqual({
      state: 'working',
      prompt: '',
      agentType: undefined,
      toolName: 'Edit',
      toolInput: '/path/to/file.ts',
      lastAssistantMessage: 'Here is the edit I made.'
    })
  })

  it('parses interactivePrompt without single-line collapse', () => {
    const interactivePrompt = JSON.stringify({
      questions: [{ question: 'Pick one', options: ['a', 'b'] }]
    })
    const result = parseAgentStatusPayload(JSON.stringify({ state: 'waiting', interactivePrompt }))
    // Why: interactivePrompt is raw JSON the client parses back, so content must survive untouched (unlike toolInput).
    expect(result!.interactivePrompt).toBe(interactivePrompt)
  })

  it('preserves newlines inside interactivePrompt JSON', () => {
    const interactivePrompt = '{\n  "questions": []\n}'
    const result = parseAgentStatusPayload(JSON.stringify({ state: 'waiting', interactivePrompt }))
    expect(result!.interactivePrompt).toBe(interactivePrompt)
  })

  it('caps interactivePrompt at its generous max length (not the toolInput cap)', () => {
    const long = 'x'.repeat(AGENT_STATUS_INTERACTIVE_PROMPT_MAX_LENGTH + 500)
    const result = parseAgentStatusPayload(
      JSON.stringify({ state: 'waiting', interactivePrompt: long })
    )
    expect(result!.interactivePrompt).toHaveLength(AGENT_STATUS_INTERACTIVE_PROMPT_MAX_LENGTH)
  })

  it('shortens an oversized question prompt into a card that still parses with every option', () => {
    const interactivePrompt = JSON.stringify({
      questions: [
        {
          question: 'q'.repeat(AGENT_STATUS_INTERACTIVE_PROMPT_MAX_LENGTH + 4000),
          header: 'Plan',
          options: [{ label: 'Ship it', description: 'd'.repeat(3000) }, { label: 'Wait' }]
        }
      ]
    })
    const result = parseAgentStatusPayload(JSON.stringify({ state: 'waiting', interactivePrompt }))
    expect(result!.interactivePrompt!.length).toBeLessThanOrEqual(
      AGENT_STATUS_INTERACTIVE_PROMPT_MAX_LENGTH
    )
    const ask = parseAskFromStatus(result!.interactivePrompt, 'ask_user_question')
    expect(ask!.questions[0].question.startsWith('qqq')).toBe(true)
    expect(ask!.questions[0].options.map((option) => option.label)).toEqual(['Ship it', 'Wait'])
  })

  it('leaves interactivePrompt undefined when absent or non-string', () => {
    expect(parseAgentStatusPayload('{"state":"working"}')!.interactivePrompt).toBeUndefined()
    expect(
      parseAgentStatusPayload('{"state":"working","interactivePrompt":42}')!.interactivePrompt
    ).toBeUndefined()
    expect(
      parseAgentStatusPayload('{"state":"working","interactivePrompt":""}')!.interactivePrompt
    ).toBeUndefined()
  })

  it('truncates each optional field to its own cap', () => {
    const longName = 'n'.repeat(AGENT_STATUS_TOOL_NAME_MAX_LENGTH + 50)
    const longInput = 'i'.repeat(AGENT_STATUS_TOOL_INPUT_MAX_LENGTH + 50)
    const longMessage = 'm'.repeat(AGENT_STATUS_ASSISTANT_MESSAGE_MAX_LENGTH + 500)
    const result = parseAgentStatusPayload(
      JSON.stringify({
        state: 'working',
        toolName: longName,
        toolInput: longInput,
        lastAssistantMessage: longMessage
      })
    )
    expect(result!.toolName).toHaveLength(AGENT_STATUS_TOOL_NAME_MAX_LENGTH)
    expect(result!.toolInput).toHaveLength(AGENT_STATUS_TOOL_INPUT_MAX_LENGTH)
    expect(result!.lastAssistantMessage).toHaveLength(AGENT_STATUS_ASSISTANT_MESSAGE_MAX_LENGTH)
  })

  it('leaves omitted optional fields undefined (not empty string)', () => {
    const result = parseAgentStatusPayload('{"state":"working"}')
    expect(result!.toolName).toBeUndefined()
    expect(result!.toolInput).toBeUndefined()
    expect(result!.lastAssistantMessage).toBeUndefined()
  })

  it('treats non-string optional fields as undefined', () => {
    const result = parseAgentStatusPayload(
      '{"state":"working","toolName":42,"toolInput":null,"lastAssistantMessage":[]}'
    )
    expect(result!.toolName).toBeUndefined()
    expect(result!.toolInput).toBeUndefined()
    expect(result!.lastAssistantMessage).toBeUndefined()
  })

  it('treats empty-string optional fields as undefined', () => {
    const result = parseAgentStatusPayload(
      '{"state":"working","toolName":"   ","toolInput":"","lastAssistantMessage":"   "}'
    )
    expect(result!.toolName).toBeUndefined()
    expect(result!.toolInput).toBeUndefined()
    expect(result!.lastAssistantMessage).toBeUndefined()
  })

  it('collapses newlines in toolInput (single-line preview field)', () => {
    const result = parseAgentStatusPayload('{"state":"working","toolInput":"line one\\nline two"}')
    expect(result!.toolInput).toBe('line one line two')
  })

  it('normalizes large single-line preview fields without full-string replacement passes', () => {
    const replaceSpy = vi.spyOn(String.prototype, 'replace')
    const prompt = `Summary\r\nDetails ${'x'.repeat(20_000)}`
    const toolInput = `src/index.ts${String.fromCharCode(0x2028)}${'line\n'.repeat(10_000)}`

    const result = normalizeAgentStatusPayload({
      state: 'working',
      prompt,
      toolInput
    })

    expect(result!.prompt.startsWith('Summary Details ')).toBe(true)
    expect(result!.prompt).toHaveLength(AGENT_STATUS_MAX_FIELD_LENGTH)
    expect(result!.toolInput?.startsWith('src/index.ts ')).toBe(true)
    expect(result!.toolInput!.length).toBeLessThanOrEqual(AGENT_STATUS_TOOL_INPUT_MAX_LENGTH)
    expect(replaceSpy).not.toHaveBeenCalled()
  })

  it('bounds scanning when oversized single-line previews are mostly line breaks', () => {
    const replaceSpy = vi.spyOn(String.prototype, 'replace')
    const prompt = `Summary${'\n'.repeat(10_000)}Details`

    const result = normalizeAgentStatusPayload({ state: 'working', prompt })

    expect(result!.prompt).toBe('Summary')
    expect(replaceSpy).not.toHaveBeenCalled()
  })

  it('preserves paragraph breaks in lastAssistantMessage', () => {
    // Why: assistant message renders with whitespace-pre-wrap, so paragraph breaks must survive.
    const result = parseAgentStatusPayload(
      '{"state":"done","lastAssistantMessage":"Summary line.\\n\\nDetails paragraph."}'
    )
    expect(result!.lastAssistantMessage).toBe('Summary line.\n\nDetails paragraph.')
  })

  it('normalizes \\r\\n to \\n and caps blank-line runs at one in lastAssistantMessage', () => {
    const result = parseAgentStatusPayload(
      '{"state":"done","lastAssistantMessage":"a\\r\\nb\\n\\n\\n\\nc"}'
    )
    expect(result!.lastAssistantMessage).toBe('a\nb\n\nc')
  })

  it('normalizes large assistant messages without full-string replacement passes', () => {
    const replaceSpy = vi.spyOn(String.prototype, 'replace')
    const lastAssistantMessage = `Summary\r\n${'\r\n'.repeat(10_000)}Details ${'x'.repeat(
      AGENT_STATUS_ASSISTANT_MESSAGE_MAX_LENGTH
    )}`

    const result = parseAgentStatusPayload(JSON.stringify({ state: 'done', lastAssistantMessage }))

    expect(result!.lastAssistantMessage?.startsWith('Summary\n\nDetails ')).toBe(true)
    expect(result!.lastAssistantMessage!.length).toBeLessThanOrEqual(
      AGENT_STATUS_ASSISTANT_MESSAGE_MAX_LENGTH
    )
    const usedMultilineReplace = replaceSpy.mock.calls.some(
      ([pattern]) =>
        pattern instanceof RegExp &&
        ['\\r\\n', '\\r', '[\\u2028\\u2029]', '\\n{3,}'].includes(pattern.source)
    )
    expect(usedMultilineReplace).toBe(false)
  })

  it('folds Unicode line/paragraph separators into \\n and caps blank-line runs in lastAssistantMessage', () => {
    // Why: U+2028/U+2029 render as line breaks under whitespace-pre-wrap; fold to \n so the blank-line cap applies.
    const resultLineSep = parseAgentStatusPayload(
      '{"state":"done","lastAssistantMessage":"a\u2028\u2028\u2028\u2028b"}'
    )
    expect(resultLineSep!.lastAssistantMessage).toBe('a\n\nb')

    const resultParaSep = parseAgentStatusPayload(
      '{"state":"done","lastAssistantMessage":"a\u2029\u2029\u2029\u2029b"}'
    )
    expect(resultParaSep!.lastAssistantMessage).toBe('a\n\nb')

    const resultMixed = parseAgentStatusPayload(
      '{"state":"done","lastAssistantMessage":"a\u2028\u2029\\n\u2028\u2029b"}'
    )
    expect(resultMixed!.lastAssistantMessage).toBe('a\n\nb')
  })

  it('still respects the base prompt cap independent of the new fields', () => {
    const prompt = 'p'.repeat(300)
    const result = parseAgentStatusPayload(
      JSON.stringify({ state: 'working', prompt, toolInput: 'x'.repeat(5) })
    )
    expect(result!.prompt).toHaveLength(AGENT_STATUS_MAX_FIELD_LENGTH)
    expect(result!.toolInput).toBe('xxxxx')
  })

  it('preserves interrupted=true when state is done', () => {
    const result = parseAgentStatusPayload('{"state":"done","interrupted":true}')
    expect(result!.interrupted).toBe(true)
  })

  it('clears interrupted on non-done states (stale-signal suppression)', () => {
    for (const state of ['working', 'blocked', 'waiting'] as const) {
      const result = parseAgentStatusPayload(`{"state":"${state}","interrupted":true}`)
      expect(result!.interrupted).toBeUndefined()
    }
  })

  it('preserves sessionBoundary=true only on done (stale-signal suppression like interrupted)', () => {
    expect(
      parseAgentStatusPayload('{"state":"done","sessionBoundary":true}')!.sessionBoundary
    ).toBe(true)
    for (const state of ['working', 'blocked', 'waiting'] as const) {
      const result = parseAgentStatusPayload(`{"state":"${state}","sessionBoundary":true}`)
      expect(result!.sessionBoundary).toBeUndefined()
    }
    // Why: parser uses `=== true`, so truthy sentinels don't count.
    expect(
      parseAgentStatusPayload('{"state":"done","sessionBoundary":"true"}')!.sessionBoundary
    ).toBeUndefined()
  })

  it('keeps turnCompletedAt on the gated working row and its all-clear done, nowhere else', () => {
    for (const state of ['working', 'done'] as const) {
      expect(
        parseAgentStatusPayload(`{"state":"${state}","turnCompletedAt":1767225601000}`)!
          .turnCompletedAt
      ).toBe(1767225601000)
    }
    for (const state of ['blocked', 'waiting'] as const) {
      expect(
        parseAgentStatusPayload(`{"state":"${state}","turnCompletedAt":1767225601000}`)!
          .turnCompletedAt
      ).toBeUndefined()
    }
    for (const raw of ['"1767225601000"', 'null', 'true']) {
      expect(
        parseAgentStatusPayload(`{"state":"done","turnCompletedAt":${raw}}`)!.turnCompletedAt
      ).toBeUndefined()
    }
    for (const value of [Number.NaN, Number.POSITIVE_INFINITY]) {
      expect(
        normalizeAgentStatusPayload({ state: 'done', turnCompletedAt: value })!.turnCompletedAt
      ).toBeUndefined()
    }
  })

  it('carries turnCompletedAt through the client-visible payload projection', () => {
    expect(
      pickParsedAgentStatusPayload({
        state: 'working',
        prompt: 'run the build',
        turnCompletedAt: 1767225601000
      }).turnCompletedAt
    ).toBe(1767225601000)
  })

  it('requires strict boolean true for interrupted (rejects truthy non-boolean)', () => {
    // Why: parser uses `=== true`, so truthy string/number sentinels don't count.
    expect(
      parseAgentStatusPayload('{"state":"done","interrupted":"true"}')!.interrupted
    ).toBeUndefined()
    expect(parseAgentStatusPayload('{"state":"done","interrupted":1}')!.interrupted).toBeUndefined()
    expect(
      parseAgentStatusPayload('{"state":"done","interrupted":"yes"}')!.interrupted
    ).toBeUndefined()
  })

  it('never leaves a lone high surrogate when truncating mid surrogate-pair', () => {
    // Why: prepend one code unit so truncation lands ON a high surrogate, else the test passes without the guard.
    const prompt = `x${'😀'.repeat(AGENT_STATUS_MAX_FIELD_LENGTH)}`
    const result = parseAgentStatusPayload(JSON.stringify({ state: 'working', prompt }))
    expect(result!.prompt.length).toBeLessThanOrEqual(AGENT_STATUS_MAX_FIELD_LENGTH)
    // Why: guard drops at most ONE trailing high surrogate, so output must still reach max - 1.
    expect(result!.prompt.length).toBeGreaterThanOrEqual(AGENT_STATUS_MAX_FIELD_LENGTH - 1)
    const len = result!.prompt.length
    const last = result!.prompt.charCodeAt(len - 1)
    const secondLast = len >= 2 ? result!.prompt.charCodeAt(len - 2) : 0
    const isLoneHighSurrogate = last >= 0xd800 && last <= 0xdbff
    expect(isLoneHighSurrogate).toBe(false)
    // Why: a trailing low surrogate must follow a high surrogate, else it's also malformed UTF-16.
    if (last >= 0xdc00 && last <= 0xdfff) {
      expect(secondLast >= 0xd800 && secondLast <= 0xdbff).toBe(true)
    }
  })

  it('never leaves a lone high surrogate in lastAssistantMessage truncation', () => {
    // Why: cover the multiline surrogate-pair guard too, so a refactor can't drop it on one side.
    const surrogatePairs = Math.floor(AGENT_STATUS_ASSISTANT_MESSAGE_MAX_LENGTH / 2) + 1
    // Why: prepend one code unit so truncation lands ON a high surrogate, else the test passes without the guard.
    const message = `x${'😀'.repeat(surrogatePairs)}`
    const result = parseAgentStatusPayload(
      JSON.stringify({ state: 'done', lastAssistantMessage: message })
    )
    expect(result!.lastAssistantMessage!.length).toBeLessThanOrEqual(
      AGENT_STATUS_ASSISTANT_MESSAGE_MAX_LENGTH
    )
    // Why: guard drops at most ONE trailing high surrogate, so output must still reach max - 1.
    expect(result!.lastAssistantMessage!.length).toBeGreaterThanOrEqual(
      AGENT_STATUS_ASSISTANT_MESSAGE_MAX_LENGTH - 1
    )
    const len = result!.lastAssistantMessage!.length
    const last = result!.lastAssistantMessage!.charCodeAt(len - 1)
    const secondLast = len >= 2 ? result!.lastAssistantMessage!.charCodeAt(len - 2) : 0
    const isLoneHighSurrogate = last >= 0xd800 && last <= 0xdbff
    expect(isLoneHighSurrogate).toBe(false)
    // Why: a trailing low surrogate must follow a high surrogate, else it's also malformed UTF-16.
    if (last >= 0xdc00 && last <= 0xdfff) {
      expect(secondLast >= 0xd800 && secondLast <= 0xdbff).toBe(true)
    }
  })

  it('normalizes the subagents field, dropping invalid entries and keeping every valid one', () => {
    const result = parseAgentStatusPayload(
      JSON.stringify({
        state: 'working',
        subagents: [
          { id: 'a1', state: 'working', startedAt: 100, agentType: 'general-purpose' },
          { id: 'r1', state: 'idle', startedAt: 'nope', description: 'line\none' },
          { id: 'u1', state: 'unverifiable', startedAt: 200 },
          { id: '', state: 'working', startedAt: 1 },
          { id: 'bad-state', state: 'running', startedAt: 1 },
          'garbage',
          ...Array.from({ length: 37 }, (_, i) => ({
            id: `extra-${i}`,
            state: 'idle',
            startedAt: i
          }))
        ]
      })
    )
    expect(result?.subagents?.length).toBe(40)
    expect(result?.subagents?.[0]).toEqual({
      id: 'a1',
      state: 'working',
      startedAt: 100,
      agentType: 'general-purpose',
      model: undefined,
      description: undefined
    })
    // Why: non-finite startedAt coerces to 0; descriptions fold to one line.
    expect(result?.subagents?.[1]).toMatchObject({
      id: 'r1',
      startedAt: 0,
      description: 'line one'
    })
    expect(result?.subagents?.[2]).toMatchObject({ id: 'u1', state: 'unverifiable' })
  })

  it('omits subagents when absent or empty', () => {
    expect(parseAgentStatusPayload('{"state":"done"}')?.subagents).toBeUndefined()
    expect(parseAgentStatusPayload('{"state":"done","subagents":[]}')?.subagents).toBeUndefined()
  })
})

describe('agentSubagentsEqual', () => {
  const snapshot = { id: 'a1', state: 'working' as const, startedAt: 1 }

  it('compares structurally and treats undefined/empty as distinct from populated', () => {
    expect(agentSubagentsEqual(undefined, undefined)).toBe(true)
    expect(agentSubagentsEqual([snapshot], [{ ...snapshot }])).toBe(true)
    expect(agentSubagentsEqual([snapshot], [{ ...snapshot, state: 'idle' }])).toBe(false)
    expect(agentSubagentsEqual([snapshot], [{ ...snapshot, model: 'gpt-5.4-mini' }])).toBe(false)
    expect(agentSubagentsEqual([snapshot], undefined)).toBe(false)
    expect(agentSubagentsEqual(undefined, [snapshot])).toBe(false)
    expect(agentSubagentsEqual([snapshot], [snapshot, { ...snapshot, id: 'b' }])).toBe(false)
  })
})

// Pin direct normalizer parity with the former JSON round trip, including values stringify alters.
describe('normalizeAgentStatusPayload matches the JSON round trip', () => {
  const CASES: Record<string, unknown>[] = [
    {
      state: 'working',
      workingMode: 'monitoring',
      prompt: 'p',
      agentType: 'grok',
      toolName: 'sh',
      toolInput: 'ls'
    },
    { state: 'done', prompt: '', agentType: 'devin', interrupted: true },
    // stringify DROPS undefined-valued keys; the direct path passes them through
    {
      state: 'working',
      prompt: 'p',
      agentType: 'cursor',
      toolName: undefined,
      toolInput: undefined,
      interactivePrompt: undefined,
      lastAssistantMessage: undefined,
      interrupted: undefined
    },
    // raw JSON inside a field exercises the structure scanner's in-string path
    {
      state: 'working',
      prompt: 'p',
      agentType: 'copilot',
      interactivePrompt: JSON.stringify({ q: 'pick {one}', options: ['a', 'b'] })
    },
    {
      state: 'working',
      prompt: 'a\r\nb c',
      agentType: 'gemini',
      lastAssistantMessage: 'emoji \u{1f389} \u65e5\u672c\u8a9e\r\n\r\n\r\nmulti'
    },
    { state: 'working', prompt: 'p', agentType: 'amp', lastAssistantMessage: 'x'.repeat(50_000) },
    { state: 'done', prompt: 'p', agentType: 'hermes', toolName: '', toolInput: '' },
    {
      state: 'working',
      prompt: 'p',
      agentType: 'droid',
      toolInput: '{"nested":{"deep":{"deeper":[1,2,3]}}}'
    },
    {
      state: 'working',
      prompt: 'p',
      agentType: 'kimi',
      lastAssistantMessage: '"escaped" quotes and \\ backslashes'
    },
    { state: 'working', prompt: 'p', agentType: 'opencode' },
    { state: 'done', prompt: 'p', agentType: 'antigravity', interrupted: false },
    { state: 'working', prompt: 'p', agentType: 'pi', toolName: 'x'.repeat(9000) },
    { state: 'working', prompt: 'x'.repeat(9000), agentType: 'omp' },
    {
      state: 'working',
      prompt: 'p',
      agentType: 'command-code',
      lastAssistantMessage: 'tail with \u001b[0m escape codes'
    },
    // a lone surrogate is the case where stringify and a raw read could diverge
    { state: 'working', prompt: 'p', agentType: 'grok', lastAssistantMessage: 'lone \ud800 pair' }
  ]

  it('produces identical output for every normalizer literal shape', () => {
    for (const [index, payload] of CASES.entries()) {
      expect({
        index,
        agent: payload.agentType,
        value: normalizeAgentStatusPayload(payload)
      }).toEqual({
        index,
        agent: payload.agentType,
        value: parseAgentStatusPayload(JSON.stringify(payload))
      })
    }
  })
})

describe('the main agent field on a status payload', () => {
  it('admits a well-formed main agent with its verdict only while the main agent is done', () => {
    expect(
      parseAgentStatusPayload(
        '{"state":"working","mainAgent":{"state":"done","outcome":"cancellation","stateStartedAt":5}}'
      )?.mainAgent
    ).toEqual({ state: 'done', outcome: 'cancellation', stateStartedAt: 5 })
    // A verdict belongs to a finished turn; one riding on a live main agent state is stale.
    expect(
      parseAgentStatusPayload(
        '{"state":"working","mainAgent":{"state":"working","outcome":"failure","stateStartedAt":5}}'
      )?.mainAgent
    ).toEqual({ state: 'working', stateStartedAt: 5 })
    expect(
      parseAgentStatusPayload(
        '{"state":"done","mainAgent":{"state":"done","outcome":"maybe","stateStartedAt":5}}'
      )?.mainAgent
    ).toEqual({ state: 'done', stateStartedAt: 5 })
  })

  it('admits the host-observed verdicts, and reads an arm it cannot name as no verdict', () => {
    for (const outcome of ['interruption', 'unconfirmed'] as const) {
      expect(
        parseAgentStatusPayload(
          `{"state":"done","mainAgent":{"state":"done","outcome":"${outcome}","stateStartedAt":5}}`
        )?.mainAgent
      ).toEqual({ state: 'done', outcome, stateStartedAt: 5 })
    }
    // A newer host's arm drops the verdict, never the row: the row reads today's done.
    expect(
      parseAgentStatusPayload(
        '{"state":"done","prompt":"keep me","mainAgent":{"state":"done","outcome":"from-a-newer-host","stateStartedAt":5}}'
      )
    ).toMatchObject({ state: 'done', prompt: 'keep me', mainAgent: { state: 'done' } })
  })

  it('drops a malformed main agent but never the row it rides on', () => {
    for (const mainAgent of [
      '"done"',
      '{"state":"running","stateStartedAt":5}',
      '{"state":"done"}',
      '{"state":"done","stateStartedAt":"5"}',
      '{"stateStartedAt":5}',
      'null'
    ]) {
      const parsed = parseAgentStatusPayload(
        `{"state":"working","prompt":"keep me","mainAgent":${mainAgent}}`
      )
      expect(parsed, mainAgent).toMatchObject({ state: 'working', prompt: 'keep me' })
      expect(parsed?.mainAgent, mainAgent).toBeUndefined()
    }
  })

  it('is carried by the client-visible projection and compared structurally', () => {
    const mainAgent = { state: 'done' as const, stateStartedAt: 7 }
    expect(
      pickParsedAgentStatusPayload({ state: 'working', prompt: '', mainAgent }).mainAgent
    ).toEqual(mainAgent)
    expect(pickParsedAgentStatusPayload({ state: 'working', prompt: '' })).not.toHaveProperty(
      'mainAgent'
    )
    expect(mainAgentStatusEqual(mainAgent, { ...mainAgent })).toBe(true)
    expect(mainAgentStatusEqual(mainAgent, { ...mainAgent, outcome: 'failure' })).toBe(false)
    expect(mainAgentStatusEqual(mainAgent, { ...mainAgent, stateStartedAt: 8 })).toBe(false)
    expect(mainAgentStatusEqual(undefined, undefined)).toBe(true)
    expect(mainAgentStatusEqual(mainAgent, undefined)).toBe(false)
  })
})