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.

7.4 KB
Raw Blame
import { describe, expect, it } from 'vitest'
import {
  AGENT_PROMPT_BRACKETED_PASTE_END,
  AGENT_PROMPT_BRACKETED_PASTE_START,
  buildAgentPromptPasteBytes,
  agentPromptSubmitJoinsPasteFrame,
  agentPromptTakesLeadLine,
  getAgentPromptSubmitDelayMs,
  getMaxTerminalPasteBytesForIngestMs,
  getTerminalPasteIngestMs,
  isHeldAgentPromptDraft,
  iterateAgentPromptPasteChunks,
  resolveAgentPromptSubmitDelayForAgent
} from './agent-prompt-injection'

const BEGIN = AGENT_PROMPT_BRACKETED_PASTE_START
const END = AGENT_PROMPT_BRACKETED_PASTE_END

describe('agent prompt injection bytes', () => {
  it('joins submit only for OMP', () => {
    expect(agentPromptSubmitJoinsPasteFrame('omp')).toBe(true)
    expect(agentPromptSubmitJoinsPasteFrame('claude')).toBe(false)
    expect(agentPromptSubmitJoinsPasteFrame('codex')).toBe(false)
    expect(agentPromptSubmitJoinsPasteFrame(undefined)).toBe(false)
  })

  it('always bracket-pastes prompts so agent TUIs treat newlines as content', () => {
    expect(buildAgentPromptPasteBytes('line one\nline two')).toBe(
      '\x1b[200~line one\nline two\x1b[201~'
    )
  })

  it('types the lead line only for Claude agents and unidentified ones', () => {
    expect(agentPromptTakesLeadLine('claude')).toBe(true)
    expect(agentPromptTakesLeadLine('claude-agent-teams')).toBe(true)
    expect(agentPromptTakesLeadLine(null)).toBe(true)
    expect(agentPromptTakesLeadLine('codex')).toBe(false)
    expect(agentPromptTakesLeadLine('opencode')).toBe(false)
  })

  it('types the lead line ahead of the paste frame on one line', () => {
    expect(buildAgentPromptPasteBytes('brief', 'Please\r\nfollow\x03\x1b[201~\x7f')).toBe(
      `Please follow [201~  ${BEGIN}brief${END}`
    )
    expect(buildAgentPromptPasteBytes('brief', '')).toBe(`${BEGIN}brief${END}`)
  })

  it('keeps generic prompt behavior for recognition-only agents', () => {
    expect(agentPromptTakesLeadLine('dsb')).toBe(agentPromptTakesLeadLine(undefined))
    expect(agentPromptSubmitJoinsPasteFrame('dsb')).toBe(false)
    for (const platform of ['darwin', 'linux', 'win32'] as const) {
      expect(resolveAgentPromptSubmitDelayForAgent(platform, 'first\nsecond', 'dsb')).toBe(
        resolveAgentPromptSubmitDelayForAgent(platform, 'first\nsecond', undefined)
      )
    }
  })

  it('costs a common-sized prompt far less than the old flat Windows delay', () => {
    // Measured ingest: 2 KB 14-25 ms, 8 KB 60-89 ms. The old constant charged 1_500 ms for both.
    expect(getAgentPromptSubmitDelayMs('win32', 2_000)).toBeLessThan(700)
    expect(getAgentPromptSubmitDelayMs('win32', 8_000)).toBeLessThan(700)
    expect(getAgentPromptSubmitDelayMs('darwin', 8_000)).toBeLessThan(700)
  })

  it.each([
    // Pin the slower measured Win11 ingest samples so Enter cannot race incomplete paste.
    [2_000, 25],
    [8_000, 89],
    [40_000, 440],
    [80_000, 858],
    [160_000, 1_662],
    [320_000, 3_342]
  ])('outlasts the slowest measured ConPTY ingest of %i bytes', (bytes, measuredMs) => {
    expect(getTerminalPasteIngestMs('win32', bytes)).toBeGreaterThan(measuredMs)
    expect(getAgentPromptSubmitDelayMs('win32', bytes)).toBeGreaterThan(measuredMs)
  })

  it('keeps a real margin over the slowest measured slope', () => {
    // 1.5x of 0.0104 ms/byte, so a host ~50% slower than either measured one is still covered.
    expect(getTerminalPasteIngestMs('win32', 320_000)).toBeGreaterThan(3_342 * 1.4)
  })

  it('outgrows the old 1_500 ms constant before ConPTY ingest does', () => {
    // Ingest crossed 1_500 ms at ~145 KB; the delay must already exceed it there.
    expect(getAgentPromptSubmitDelayMs('win32', 145_000)).toBeGreaterThan(1_500)
  })

  it('scales without a cap all the way to the terminal input ceiling', () => {
    const ceilingBytes = 16 * 1024 * 1024
    expect(getAgentPromptSubmitDelayMs('win32', ceilingBytes)).toBeGreaterThan(250_000)
    // Even the fast platforms outrun a flat 500 ms at the ceiling.
    expect(getAgentPromptSubmitDelayMs('linux', ceilingBytes)).toBeGreaterThan(4_000)
  })

  it('charges non-Windows hosts nothing measurable for a real prompt', () => {
    expect(getTerminalPasteIngestMs('darwin', 8_000)).toBeLessThanOrEqual(2)
    expect(getTerminalPasteIngestMs('linux', 0)).toBe(0)
    expect(getTerminalPasteIngestMs('linux', Number.NaN)).toBe(0)
    expect(getTerminalPasteIngestMs('win32', -5)).toBe(0)
  })

  it('grows monotonically with payload size on every platform', () => {
    for (const platform of ['win32', 'darwin', 'linux'] as const) {
      expect(getAgentPromptSubmitDelayMs(platform, 400_000)).toBeGreaterThan(
        getAgentPromptSubmitDelayMs(platform, 40_000)
      )
    }
    expect(getTerminalPasteIngestMs('win32', 320_000)).toBeGreaterThan(
      getTerminalPasteIngestMs('darwin', 320_000)
    )
  })

  it('adds per-line settle time for antigravity multiline prompts', () => {
    const short = resolveAgentPromptSubmitDelayForAgent('darwin', 'one line', 'antigravity')
    const long = resolveAgentPromptSubmitDelayForAgent(
      'darwin',
      `${'Filler line\n'.repeat(100)}AGY_LONG_OK`,
      'antigravity'
    )
    expect(long - short).toBeGreaterThanOrEqual(100 * 45)
    expect(resolveAgentPromptSubmitDelayForAgent('darwin', 'one line', 'aider')).toBe(short - 45)
  })

  it('inverts the host ingest budget without crossing it', () => {
    const bytes = getMaxTerminalPasteBytesForIngestMs('win32', 20_000)
    expect(getTerminalPasteIngestMs('win32', bytes)).toBe(20_000)
    expect(getTerminalPasteIngestMs('win32', bytes + 1)).toBe(20_001)
  })

  it('sanitizes embedded escape bytes before framing', () => {
    const bytes = buildAgentPromptPasteBytes('before\x1b[201~after\x1b')
    expect(bytes).toBe(`${BEGIN}before<ESC>[201~after<ESC>${END}`)
    expect(bytes.slice(BEGIN.length, -END.length)).not.toContain('\x1b')
  })

  it('chunks a sanitized paste frame within the UTF-8 byte limit', () => {
    const body = 'a😀é'.repeat(200)
    const prompt = `header\n${body}\x1b[201~`
    const chunks = [...iterateAgentPromptPasteChunks(prompt, 31)]
    expect(chunks.length).toBeGreaterThan(1)
    expect(chunks.join('')).toBe(`\x1b[200~header\n${body}<ESC>[201~\x1b[201~`)
    for (const chunk of chunks) {
      expect(Buffer.byteLength(chunk, 'utf8')).toBeLessThanOrEqual(31)
      expect(Buffer.from(chunk, 'utf8').toString('utf8')).toBe(chunk)
    }
  })
})

describe('isHeldAgentPromptDraft', () => {
  // 142 LFs, as in the measured 10,312-char dispatch that Windows Codex showed as 10,597 chars.
  const prompt = Array.from({ length: 143 }, (_, line) => `${line}`.padEnd(71, 'x')).join('\n')

  it.each([
    ['the measured Windows count', `[Pasted Content ${prompt.length + 142 * 2 + 1} chars]`, true],
    [
      'a burst split into two placeholders',
      '[Pasted Content 4000 chars][Pasted Content 6312 chars] #2',
      true
    ],
    ['the prompt shown as text', prompt, true],
    ['its tail in a scrolled composer', prompt.slice(-200), true],
    ['a line from its middle', prompt.split('\n')[5], false],
    [
      'the user typing beside it',
      `[Pasted Content ${prompt.length} chars] also fix the readme`,
      false
    ],
    ['a larger unrelated paste', `[Pasted Content ${prompt.length * 2} chars]`, false],
    ['a shorter paste', `[Pasted Content ${prompt.length - 1} chars]`, false],
    ['a rotating placeholder', 'Explain this codebase', false]
  ])('%s -> %s', (_case, draft, held) => {
    expect(isHeldAgentPromptDraft(draft, prompt, 1)).toBe(held)
  })
})