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.

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

import {
  isCompactContinuationUserTurnText,
  isKnownHarnessInjectedUserTurnText
} from './harness-injected-user-turns'

describe('isKnownHarnessInjectedUserTurnText', () => {
  it('matches every known harness tag, including attribute-carrying forms', () => {
    // Inventory mirrors the tags the harness itself classifies as machinery.
    const injected = [
      '<task-notification> <task-id>bzthj2b8r</task-id> <tool-use-id>toolu_01abc</tool-use-id>',
      '<task-notification summary="Agent finished">',
      '<system-reminder>context</system-reminder>',
      '<agent-message from="reviewer-A-r3b"> REVIEW the diff',
      '<teammate-message teammate_id="worker-1">status?',
      '<cross-session-message from="other session">hello',
      '<channel source="general">new post',
      '<fork-boilerplate>Your directive: ship it',
      '<user-memory-input>remember this',
      '<mcp-resource-update uri="db://x">',
      '<mcp-polling-update>tick',
      '<command-name>/review</command-name>',
      '<command-message>review</command-message>',
      '<command-args>--fix</command-args>',
      '<local-command-stdout>ok</local-command-stdout>',
      '<local-command-stderr>boom</local-command-stderr>',
      '<local-command-caveat>Caveat: tool output</local-command-caveat>',
      '<bash-input>ls</bash-input>',
      '<bash-stdout>file.txt</bash-stdout>',
      '<bash-stderr>err</bash-stderr>',
      '<user-prompt-submit-hook>hook context</user-prompt-submit-hook>'
    ]
    for (const text of injected) {
      expect(isKnownHarnessInjectedUserTurnText(text), text).toBe(true)
    }
  })

  it('matches harness prose wrappers and notices', () => {
    expect(
      isKnownHarnessInjectedUserTurnText('A message arrived from teammate-b:\n<agent-message>hi')
    ).toBe(true)
    expect(
      isKnownHarnessInjectedUserTurnText(
        'Another Claude session sent a message:\n<agent-message>hi'
      )
    ).toBe(true)
    expect(isKnownHarnessInjectedUserTurnText('No response requested.')).toBe(true)
    expect(isKnownHarnessInjectedUserTurnText('[Request interrupted by user]')).toBe(true)
    expect(isKnownHarnessInjectedUserTurnText('[Request interrupted by user for tool use]')).toBe(
      true
    )
    expect(
      isKnownHarnessInjectedUserTurnText(
        'This session is being continued from a previous conversation.'
      )
    ).toBe(true)
    expect(
      isKnownHarnessInjectedUserTurnText(
        'Caveat: the messages below were generated by the user while running local commands.'
      )
    ).toBe(true)
  })

  it('distinguishes compact continuation from other harness turns', () => {
    expect(
      isCompactContinuationUserTurnText(
        'This session is being continued from a previous conversation. Summary follows.'
      )
    ).toBe(true)
    expect(isCompactContinuationUserTurnText('<task-notification>done')).toBe(false)
    expect(isCompactContinuationUserTurnText('<command-name>/review</command-name>')).toBe(false)
  })

  it('is case-insensitive and ignores surrounding whitespace', () => {
    expect(isKnownHarnessInjectedUserTurnText('  <TASK-NOTIFICATION> done')).toBe(true)
    expect(isKnownHarnessInjectedUserTurnText('\n<System-Reminder> hi')).toBe(true)
  })

  it('keeps real user prompts, including ones that mention the tags', () => {
    expect(isKnownHarnessInjectedUserTurnText('fix the login bug')).toBe(false)
    expect(
      isKnownHarnessInjectedUserTurnText('why does <task-notification> show in the sidebar?')
    ).toBe(false)
    expect(isKnownHarnessInjectedUserTurnText('')).toBe(false)
    expect(isKnownHarnessInjectedUserTurnText('   ')).toBe(false)
  })

  it('classifies harness and user prefixes in multi-KB pastes', () => {
    const largePrompt = `<task-notification>done</task-notification>\n${'x'.repeat(20_000)}`
    expect(isKnownHarnessInjectedUserTurnText(largePrompt)).toBe(true)
    expect(isKnownHarnessInjectedUserTurnText(`fix login ${'y'.repeat(20_000)}`)).toBe(false)
  })

  it('keeps single-word tag pastes, custom elements, and underscore wrappers', () => {
    // Grok wraps REAL typed prompts in <user_query> — never classify as noise.
    expect(isKnownHarnessInjectedUserTurnText('<user_query>fix the bug</user_query>')).toBe(false)
    expect(isKnownHarnessInjectedUserTurnText('<div class="x">pasted html</div>')).toBe(false)
    expect(
      isKnownHarnessInjectedUserTurnText('<script>alert(1)</script> — why is this flagged?')
    ).toBe(false)
    expect(isKnownHarnessInjectedUserTurnText('<https://example.com/a-b> what is this?')).toBe(
      false
    )
    expect(isKnownHarnessInjectedUserTurnText('<[email protected]> sent me this')).toBe(false)
  })

  it('does not treat unknown kebab tags as machinery', () => {
    // Only catalogued harness tags are noise; unknown/custom elements remain user input.
    expect(isKnownHarnessInjectedUserTurnText('<my-custom-element>pasted code')).toBe(false)
    expect(isKnownHarnessInjectedUserTurnText('<brand-new-harness-tag id="1">payload')).toBe(false)
    expect(isKnownHarnessInjectedUserTurnText('<queued-notice>\nlater')).toBe(false)
  })

  it('only treats the attributed <channel source=…> form as machinery', () => {
    // Only attributed channel source tags are machinery; bare channel is legitimate RSS/XML input.
    expect(isKnownHarnessInjectedUserTurnText('<channel source="general">new post')).toBe(true)
    expect(
      isKnownHarnessInjectedUserTurnText('<channel>general</channel> explain this feed element')
    ).toBe(false)
  })
})