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.

2.7 KB
Raw Blame
// Harness machinery uses user-role turns and fires prompt hooks, but must not become prompt-derived UI.
// Match only observed tags: custom elements and Grok user_query are real input; false positives hide transcripts/titles or leave interrupted agents marked done.
const LEADING_TAG_NAME = /^<([a-z][a-z0-9-]*)(?:[\s>]|$)/

const KNOWN_HARNESS_TAG_NAMES = new Set([
  'agent-message',
  'bash-input',
  'bash-stderr',
  'bash-stdout',
  'command-args',
  'command-message',
  'command-name',
  'cross-session-message',
  'fork-boilerplate',
  'local-command-caveat',
  'local-command-stderr',
  'local-command-stdout',
  'mcp-polling-update',
  'mcp-resource-update',
  'system-reminder',
  'task-notification',
  'teammate-message',
  'user-memory-input',
  'user-prompt-submit-hook'
])

// Match attributed channel source envelopes separately; bare channel is legitimate RSS/XML. Also recognize observed prose notices.
const COMPACT_CONTINUATION_PREFIX = 'this session is being continued from a previous conversation'
const HARNESS_INJECTED_TURN_PREFIXES = [
  '<channel source=',
  '[request interrupted',
  'a message arrived from ',
  'another claude session sent a message',
  'no response requested.',
  'caveat: the messages below were generated by the user while running local commands',
  COMPACT_CONTINUATION_PREFIX
]

// Bound lowercasing of leading tags/prefixes so vault/prompt scans stay constant-cost on large pastes.
const HARNESS_CLASSIFY_HEAD_LIMIT = 256
const HARNESS_CLASSIFY_LEADING_WS_LIMIT = 64

/** Match only observed harness shapes on trimmed, lowercased text; unknown tags remain user input. */
export function isKnownHarnessInjectedUserTurnText(text: string): boolean {
  const normalized = normalizedHarnessTurnHead(text)
  if (!normalized) {
    return false
  }
  const tagName = LEADING_TAG_NAME.exec(normalized)?.[1]
  if (tagName && KNOWN_HARNESS_TAG_NAMES.has(tagName)) {
    return true
  }
  return HARNESS_INJECTED_TURN_PREFIXES.some((prefix) => normalized.startsWith(prefix))
}

/** True only for the observed post-compaction continuation prompt. */
export function isCompactContinuationUserTurnText(text: string): boolean {
  return normalizedHarnessTurnHead(text).startsWith(COMPACT_CONTINUATION_PREFIX)
}

function normalizedHarnessTurnHead(text: string): string {
  let start = 0
  const wsScanEnd = Math.min(text.length, HARNESS_CLASSIFY_LEADING_WS_LIMIT)
  while (start < wsScanEnd && isAsciiWhitespace(text.charCodeAt(start))) {
    start += 1
  }
  if (start >= text.length) {
    return ''
  }
  const headEnd = Math.min(text.length, start + HARNESS_CLASSIFY_HEAD_LIMIT)
  return text.slice(start, headEnd).toLowerCase()
}

function isAsciiWhitespace(code: number): boolean {
  return code === 32 || code === 9 || code === 10 || code === 13 || code === 12
}