Skip to content

The agent that grows with you

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

import { setRuntimeI18nLocale } from '@/i18n'

import {
  buildToolView,
  clampForDisplay,
  countDiffLineStats,
  inlineDiffFromResult,
  isPreviewableTarget,
  MAX_TOOL_RENDER_CHARS,
  prettyJson,
  type ToolPart,
  toolPreviewOutcome
} from './fallback-model'

const part = (overrides: Partial<ToolPart>): ToolPart => ({
  args: {},
  isError: false,
  result: {},
  toolCallId: 'call_1',
  toolName: 'vision_analyze',
  type: 'tool-call',
  ...overrides
})

afterEach(() => {
  setRuntimeI18nLocale('en')
})

describe('buildToolView image handling', () => {
  it('keeps local image paths for the activity renderer to resolve', () => {
    expect(buildToolView(part({ args: { path: '/Users/me/shot.png' } }), '').imageUrl).toBe('/Users/me/shot.png')
    expect(buildToolView(part({ result: { image_path: '/tmp/out.jpg' } }), '').imageUrl).toBe('/tmp/out.jpg')
  })

  it('keeps fetchable data URLs', () => {
    const dataUrl = 'data:image/png;base64,AAAA'

    expect(buildToolView(part({ result: { image_url: dataUrl } }), '').imageUrl).toBe(dataUrl)
  })

  it('keeps remote http(s) image URLs', () => {
    const url = 'https://example.com/pic.webp'

    expect(buildToolView(part({ result: { url } }), '').imageUrl).toBe(url)
  })
})

describe('buildToolView terminal exit-code status', () => {
  const terminal = (result: Record<string, unknown>) => buildToolView(part({ result, toolName: 'terminal' }), '')

  // A non-zero exit code with real output is not a failure (grep no-match,
  // diff differences, piped commands surfacing the last stage's code, etc.) —
  // it should render as success so the card isn't painted red.
  it('treats non-zero exit with output as success', () => {
    expect(terminal({ exit_code: 7, output: 'node ... 5174 (LISTEN)' }).status).toBe('success')
    expect(terminal({ exit_code: 1, stdout: 'partial results' }).status).toBe('success')
  })

  it('distinguishes a command failure from an empty no-match exit', () => {
    expect(terminal({ exit_code: 127, output: '' }).status).toBe('error')
    expect(terminal({ exit_code: 1, output: '' }).status).toBe('notice')
  })

  it('treats zero exit as success', () => {
    expect(terminal({ exit_code: 0, output: 'done' }).status).toBe('success')
  })

  // Explicit error signals still win regardless of output presence.
  it('keeps explicit error signals red even with output', () => {
    expect(terminal({ error: 'boom', exit_code: 0, output: 'partial' }).status).toBe('error')
    expect(buildToolView(part({ isError: true, result: { output: 'x' }, toolName: 'terminal' }), '').status).toBe(
      'error'
    )
  })

  it('keeps the command and exit code for the terminal transcript', () => {
    const view = buildToolView(
      part({
        args: { command: 'npm run check --workspace=apps/desktop' },
        result: { exit_code: 0, output: 'done' },
        toolName: 'terminal'
      }),
      ''
    )

    expect(view.terminalCommand).toBe('npm run check --workspace=apps/desktop')
    expect(view.terminalExitCode).toBe(0)
  })
})

describe('buildToolView error confidence', () => {
  it('keeps routine misses and returned diagnostic data out of destructive status', () => {
    const cases: Array<[Partial<ToolPart>, ReturnType<typeof buildToolView>['status']]> = [
      [
        {
          toolName: 'read_file',
          result: { error: 'File not found: /repo/session-view.ts', similar_files: ['/repo/session-view.tsx'] }
        },
        'notice'
      ],
      [{ toolName: 'read_file', isError: true, result: { error: 'File not found: /repo/session-view.ts' } }, 'notice'],
      [{ toolName: 'terminal', result: { exit_code: 0, output: '{"error":"a logged failure"}' } }, 'success'],
      [{ result: { error: 'none', message: 'No changes needed' } }, 'success'],
      [{ result: { status: 'no error', message: 'Ready' } }, 'success'],
      [{ result: { meta: { error: 'a previous attempt' }, data: { count: 1 } } }, 'success'],
      [{ toolName: 'read_file', result: { error: 'Permission denied reading /repo/private.ts' } }, 'error'],
      [{ toolName: 'patch', result: { error: 'File not found: /repo/session-view.ts' } }, 'error'],
      [
        {
          toolName: 'skill_manage',
          result: { success: false, error: 'SKILL.md content is over the limit', file_preview: 'ROLLBACK FAILED notes' }
        },
        'notice'
      ],
      [{ toolName: 'skill_manage', result: { success: false, error: "batch aborted, ROLLBACK FAILED for 'demo'" } }, 'error'],
      [{ result: { success: false, result: { output: { error: { message: 'Connection refused' } } } } }, 'error']
    ]

    for (const [overrides, status] of cases) {
      expect(buildToolView(part(overrides), '').status, JSON.stringify(overrides)).toBe(status)
    }
  })
})

describe('buildToolView envelope errors', () => {
  it('shows the event error when the result carries no explanation', () => {
    const view = buildToolView(
      part({
        isError: true,
        result: 'partial output',
        toolName: 'terminal',
        toolResultMetadata: { error: 'killed by signal 9' }
      }),
      ''
    )

    expect(view.status).toBe('error')
    expect(view.subtitle).toBe('killed by signal 9')
  })

  it('keeps an envelope-only read miss on the notice tier', () => {
    const view = buildToolView(
      part({
        isError: true,
        result: undefined,
        completedAt: 5,
        toolName: 'read_file',
        toolResultMetadata: { error: 'File not found: /repo/missing.ts' }
      }),
      ''
    )

    expect(view.status).toBe('notice')
  })
})

describe('buildToolView calls sealed without a result', () => {
  it('warns that a lost result is unavailable', () => {
    const view = buildToolView(part({ completedAt: 5, result: undefined, toolName: 'terminal' }), '')

    expect(view.status).toBe('warning')
  })

  it('shows a call the user interrupted as a neutral notice', () => {
    const view = buildToolView(part({ completedAt: 5, interrupted: true, result: undefined, toolName: 'terminal' }), '')

    expect(view.status).toBe('notice')
  })

  it('shows the real result when one arrived after the interruption', () => {
    const view = buildToolView(part({ completedAt: 5, interrupted: true, result: 'ok', toolName: 'terminal' }), '')

    expect(view.status).toBe('success')
    expect(view.title).not.toBe('Interrupted')
  })
})

describe('buildToolView browser_exec step label', () => {
  const bexec = (code: string) =>
    buildToolView(part({ args: { code }, result: undefined, toolName: 'browser_exec' }), '')

  it('uses the leading # comment as the title', () => {
    expect(bexec('# Searching Amazon for paper towels\nnew_tab("https://amazon.com")').title).toBe(
      'Searching Amazon for paper towels'
    )
  })

  it('falls back to the generic title when code has no leading comment', () => {
    const view = bexec('new_tab("https://amazon.com")')

    expect(view.title).not.toBe('')
    expect(view.title).not.toContain('new_tab')
  })

  it('truncates long labels and keeps the ellipsis', () => {
    const long = `# ${'x'.repeat(120)}`

    expect(bexec(long).title.length).toBeLessThanOrEqual(80)
    expect(bexec(long).title.endsWith('…')).toBe(true)
  })

  it('keeps the label after the result arrives', () => {
    const view = buildToolView(
      part({
        args: { code: '# Checking workspace persistence\nprint(1)' },
        result: { output: 'ok', success: true },
        toolName: 'browser_exec'
      }),
      ''
    )

    expect(view.title).toBe('Checking workspace persistence')
  })
})

describe('buildToolView web-search query', () => {
  it('keeps the query separate from structured search results', () => {
    const view = buildToolView(
      part({
        args: { query: 'Hermes Agent Desktop tool calls' },
        result: { web: [{ snippet: 'Desktop docs', title: 'Hermes docs', url: 'https://example.com/docs' }] },
        toolName: 'web_search'
      }),
      ''
    )

    expect(view.searchQuery).toBe('Hermes Agent Desktop tool calls')
    expect(view.searchHits).toEqual([
      { snippet: 'Desktop docs', title: 'Hermes docs', url: 'https://example.com/docs' }
    ])
  })
})

describe('buildToolView browser_navigate title', () => {
  it('shows failed title when navigate returns success=false', () => {
    const view = buildToolView(
      part({
        toolName: 'browser_navigate',
        args: { url: 'https://hermes-agent.nousresearch.com/docs' },
        result: { success: false, error: 'Command timed out after 60 seconds' }
      }),
      ''
    )

    expect(view.status).toBe('error')
    expect(view.title).toContain('hermes-agent.nousresearch.com/docs')
  })

  it('shows opened title on success', () => {
    const view = buildToolView(
      part({
        toolName: 'browser_navigate',
        args: { url: 'https://hermes-agent.nousresearch.com/docs' },
        result: { success: true, url: 'https://hermes-agent.nousresearch.com/docs', title: 'Docs' }
      }),
      ''
    )

    expect(view.status).toBe('success')
    expect(view.title).toContain('hermes-agent.nousresearch.com/docs')
  })
})

describe('buildToolView file edit diffs', () => {
  const patchDiff = '--- a/src/demo.ts\n+++ b/src/demo.ts\n@@ -1 +1 @@\n-old\n+new'

  it('reads inline_diff and diff fields from patch results', () => {
    expect(inlineDiffFromResult({ inline_diff: patchDiff })).toBe(patchDiff)
    expect(inlineDiffFromResult({ diff: patchDiff })).toBe(patchDiff)
  })

  it('suppresses raw patch args when a diff is available', () => {
    const view = buildToolView(
      part({
        args: { context: 'src/demo.ts', mode: 'replace', new_string: 'new', path: 'src/demo.ts' },
        result: { diff: patchDiff, success: true },
        toolName: 'patch'
      }),
      patchDiff
    )

    expect(view.title).toBe('demo.ts')
    expect(view.subtitle).toBe('src/demo.ts')
    expect(view.detail).toBe('')
    expect(view.inlineDiff).toBe(patchDiff)
  })

  it('shows path subtitle instead of patch args JSON while pending', () => {
    const view = buildToolView(
      part({
        args: { context: 'src/demo.ts', mode: 'replace', new_string: 'new', path: 'src/demo.ts' },
        result: undefined,
        toolName: 'patch'
      }),
      ''
    )

    expect(view.title).toBe('demo.ts')
    expect(view.subtitle).toBe('src/demo.ts')
    expect(view.detail).toBe('')
  })
})

describe('buildToolView title actions', () => {
  it('does not mark completed tool titles as pending actions', () => {
    const view = buildToolView(part({ args: { url: 'https://example.com/docs' }, toolName: 'web_extract' }), '')

    expect(view.title).toBe('Read example.com/docs')
    expect(view.titleAction).toBeUndefined()
  })

  it('uses the filename for completed read_file rows', () => {
    const view = buildToolView(
      part({ args: { path: './package.json' }, result: { content: '1|{"name":"demo"}' }, toolName: 'read_file' }),
      ''
    )

    expect(view.title).toBe('Read package.json')
    expect(view.subtitle).toBe('')
    expect(view.titleAction).toBeUndefined()
  })

  it('adds a compact line range to line-scoped read_file rows', () => {
    const view = buildToolView(
      part({
        args: { limit: 10, offset: 25, path: './src/main.ts' },
        result: { content: '25|function toggleDock() {\n26|  dock.classList.toggle("hidden");\n34|}' },
        toolName: 'read_file'
      }),
      ''
    )

    expect(view.title).toBe('Read main.ts L25-34')
    expect(view.subtitle).toBe('')
  })

  it('uses the requested positive offset/limit for read_file row line ranges', () => {
    const view = buildToolView(
      part({
        args: { limit: 5, offset: 1, path: './package.json' },
        result: {
          content:
            '1|{\n2|  "name": "bb-rainbows",\n3|  "private": true,\n4|  "version": "0.0.1",\n5|  "type": "module",\n6|  "description": "extra"'
        },
        toolName: 'read_file'
      }),
      ''
    )

    expect(view.title).toBe('Read package.json L1-5')
  })

  it('uses inherited backend context for live read_file rows', () => {
    const view = buildToolView(
      part({
        args: { context: 'package.json L1-5', path: './package.json' },
        result: undefined,
        toolName: 'read_file'
      }),
      ''
    )

    expect(view.title).toBe('Reading package.json L1-5')
    expect(view.titleAction).toEqual({ prefix: '', text: 'Reading', suffix: ' package.json L1-5' })
  })

  it('uses returned line numbers for negative-offset read_file rows', () => {
    const view = buildToolView(
      part({
        args: { limit: 2, offset: -2, path: './src/main.ts' },
        result: { content: '99|lastLine();\n100|done();' },
        toolName: 'read_file'
      }),
      ''
    )

    expect(view.title).toBe('Read main.ts L99-100')
  })

  it('renders compact terminal titles for session 20260624_231846_bdbd1e commands', () => {
    const rows = [
      [
        'cd /Users/brooklyn/www/bb-rainbows && pnpm run lint 2>&1 | tail -20; echo "lint_exit=${PIPESTATUS[0]}"',
        'Ran pnpm run lint'
      ],
      [
        'cd /Users/brooklyn/www/bb-rainbows && pnpm run build 2>&1 | tail -20; echo "build_exit=${PIPESTATUS[0]}"',
        'Ran pnpm run build'
      ],
      [
        'which node pnpm corepack; node -v; echo "---"; corepack --version 2>&1; echo "---pnpm via corepack---"; pnpm --version 2>&1 | tail -5',
        'Ran which node pnpm corepack + 3 commands'
      ],
      [
        'echo "--- proto pnpm direct ---"; ~/.proto/tools/node/24.11.0/bin/pnpm --version 2>&1 | tail -3; echo "--- proto node ---"; ls ~/.proto/tools/node/ 2>&1; echo "--- corepack cache ---"; ls ~/.cache/node/corepack/v1/pnpm/ 2>&1',
        'Ran ~/.proto/tools/node/24.11.0/bin/pnpm --version + 2 commands'
      ],
      [
        'cd /Users/brooklyn/www/bb-rainbows && COREPACK_ENABLE_DOWNLOAD_PROMPT=0 corepack [email protected] --version 2>&1 | tail -3',
        'Ran COREPACK_ENABLE_DOWNLOAD_PROMPT=0 corepack [email protected] --version'
      ],
      [
        'cd /Users/brooklyn/www/bb-rainbows && COREPACK_ENABLE_DOWNLOAD_PROMPT=0 corepack use [email protected] 2>&1 | tail -10; echo "exit=$?"',
        'Ran COREPACK_ENABLE_DOWNLOAD_PROMPT=0 corepack use [email protected]'
      ]
    ] as const

    for (const [command, expectedTitle] of rows) {
      const view = buildToolView(
        part({ args: { command }, result: { output: 'ok', exit_code: 0 }, toolName: 'terminal' }),
        ''
      )

      expect(view.title).toBe(expectedTitle)
    }
  })

  it('uses inherited backend context for live terminal rows', () => {
    const view = buildToolView(
      part({
        args: {
          command: 'cd /Users/brooklyn/www/bb-rainbows && pnpm run lint 2>&1 | tail -20',
          context: 'pnpm run lint'
        },
        result: undefined,
        toolName: 'terminal'
      }),
      ''
    )

    expect(view.title).toBe('Running pnpm run lint')
    expect(view.subtitle).toBe('')
    expect(view.titleAction).toEqual({ prefix: '', text: 'Running', suffix: ' pnpm run lint' })
  })

  it('never stutters the verb or echoes the command when the backend context is a phrased label', () => {
    // Older backends stamped tool.start with a *phrased* label
    // ("Running sleep 70 + 2 commands") rather than a raw arg preview, and the
    // desktop merges that into args.context. The row must still prepend its own
    // verb exactly once, show the real command in the `$` transcript, and not
    // repeat either string as detail.
    const command = 'sleep 70; echo "a"; echo "b"'

    const view = buildToolView(
      part({
        args: { command, context: 'Running sleep 70 + 2 commands' },
        result: { exit_code: 0 },
        toolName: 'terminal'
      }),
      ''
    )

    expect(view.title).toBe('Ran sleep 70 + 2 commands')
    expect(view.terminalCommand).toBe(command)
    expect(view.detail).toBe('')
  })

  it('does not double the action verb when the context already starts with it', () => {
    // A model-authored `context` that opens with the same verb the template
    // prepends ("Running grep …") otherwise renders as "Running Running grep …".
    const view = buildToolView(
      part({
        args: { context: 'Running grep -rn -i "bedrock" ~/.hermes/' },
        result: undefined,
        toolName: 'terminal'
      }),
      ''
    )

    expect(view.title.startsWith('Running Running')).toBe(false)
    expect(view.title).toBe('Running grep -rn -i "bedrock" ~/.hermes/')
    expect(view.titleAction).toEqual({
      prefix: '',
      text: 'Running',
      suffix: ' grep -rn -i "bedrock" ~/.hermes/'
    })
  })

  it('uses the runtime locale for title text and action placement', () => {
    setRuntimeI18nLocale('ja')

    const read = buildToolView(part({ args: { path: '/tmp/demo.txt' }, result: undefined, toolName: 'read_file' }), '')

    const web = buildToolView(
      part({ args: { url: 'https://example.com/docs' }, result: undefined, toolName: 'web_extract' }),
      ''
    )

    expect(read.title).toBe('demo.txt を読み取り中')
    expect(read.titleAction).toEqual({ prefix: 'demo.txt を', text: '読み取り中', suffix: '' })
    expect(web.title).toBe('example.com/docs を読み取り中')
    expect(web.titleAction).toEqual({ prefix: 'example.com/docs を', text: '読み取り中', suffix: '' })
  })
})

// #85132: Windows agents write `C:\\...` / UNC paths; those must get the same
// artifact preview tag a POSIX `/Users/...` path gets.
describe('Windows absolute preview targets', () => {
  it.each(['C:\\Users\\me\\report.html', 'D:/work/report.htm', '\\\\server\\share\\report.html'])(
    'tags a written %s as a previewable artifact',
    path => {
      const outcome = toolPreviewOutcome(
        part({ args: { content: '<h1>hi</h1>', path }, result: { bytes_written: 11 }, toolName: 'write_file' })
      )

      expect(outcome.previewTarget).toBe(path)
      expect(isPreviewableTarget(outcome.previewTarget)).toBe(true)
    }
  )

  it('keeps non-HTML Windows files out of the preview tag, like POSIX ones', () => {
    expect(isPreviewableTarget('C:\\Users\\me\\notes.txt')).toBe(isPreviewableTarget('/Users/me/notes.txt'))
    expect(isPreviewableTarget('C:\\Users\\me\\notes.txt')).toBe(false)
  })
})

describe('clampForDisplay', () => {
  it('passes short payloads through untouched', () => {
    expect(clampForDisplay('hello')).toBe('hello')
    expect(clampForDisplay('x'.repeat(MAX_TOOL_RENDER_CHARS))).toHaveLength(MAX_TOOL_RENDER_CHARS)
  })

  it('truncates oversized payloads and reports the omitted count', () => {
    const oversized = 'x'.repeat(MAX_TOOL_RENDER_CHARS + 5_000)
    const clamped = clampForDisplay(oversized)

    expect(clamped.length).toBeLessThan(oversized.length)
    expect(clamped.startsWith('x'.repeat(MAX_TOOL_RENDER_CHARS))).toBe(true)
    expect(clamped).toContain(`${new Intl.NumberFormat().format(5_000)} more characters truncated`)
  })
})

// A large tool result (e.g. a 100KB read_file during a `/learn` run) must not
// be serialized at full size — that JSON.stringify payload is what floods the
// renderer. buildToolView no longer prettyJson's every result eagerly; the
// web_search drilldown serializes lazily via prettyJson, which clamps.
describe('prettyJson caps serialized result size', () => {
  it('clamps an oversized result', () => {
    const huge = 'y'.repeat(MAX_TOOL_RENDER_CHARS * 3)
    const out = prettyJson({ content: huge })

    expect(out.length).toBeLessThanOrEqual(MAX_TOOL_RENDER_CHARS + 200)
    expect(out).toContain('truncated')
  })
})

describe('countDiffLineStats', () => {
  it('counts added and removed lines', () => {
    expect(countDiffLineStats(`--- a/x\n+++ b/x\n@@\n-old\n+new\n context\n+another`)).toEqual({ added: 2, removed: 1 })
  })
})

describe('buildToolView memory status', () => {
  const memory = (overrides: Partial<Parameters<typeof part>[0]> = {}) =>
    buildToolView(part({ toolName: 'memory', ...overrides }), '')

  it('treats an explicit success payload as success even with isError', () => {
    const view = memory({
      isError: true,
      result: {
        success: true,
        entry_count: 13,
        message: 'Applied 1 operation(s).',
        duration_s: 0.003
      }
    })

    expect(view.status).toBe('success')
    expect(view.countLabel).toBe('13 entries')
    expect(view.subtitle).toBe('Applied 1 operation(s).')
  })

  it('uses soft warning copy for over-budget refusals, not "Saved"', () => {
    const view = memory({
      result: {
        success: false,
        error: 'Memory is full (2,200/2,200). Consolidate before adding more.'
      }
    })

    expect(view.status).toBe('warning')
    expect(view.subtitle).toContain('Memory is full')
  })
})