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.

16.8 KB
Raw Blame
import { describe, expect, it } from 'vitest'
import {
  AgentJournalItemBodySchema,
  isAdmissibleAgentJournalItemBody,
  isAdmissibleAgentJournalMessageBody,
  isAdmissibleAgentJournalRenderItem
} from './agent-session-journal-schemas'
import { isAdmissibleAgentJournalSubmission } from './agent-session-journal-submission-schema'
import type {
  AgentJournalItemBody,
  AgentJournalRenderItem,
  AgentJournalSubmission
} from './agent-session-journal-types'

const PAYLOAD = { head: 'x', byteLength: 4, digest: 'd'.repeat(64), truncated: true }
const RESOLUTION = {
  state: 'pending',
  selectedOptionId: null,
  resolvedBy: null,
  resolvedAt: null
} as const

// Typed fixtures ensure the schema audit validates the canonical model.
const CANONICAL_BODIES: AgentJournalItemBody[] = [
  {
    kind: 'agent-start',
    dispatchId: 'dispatch',
    task: 'Complete task',
    worker: {
      party: { address: 'term_worker', terminalHandle: 'term_worker', orcaSessionId: null },
      name: 'Worker'
    }
  },
  {
    kind: 'message',
    role: 'reasoning',
    blocks: [{ type: 'text', text: 'Inspecting the request' }]
  },
  {
    kind: 'message',
    role: 'reasoning',
    blocks: [{ type: 'text', text: 'Inspecting the request' }],
    state: 'completed',
    completedAt: 2_000
  },
  {
    kind: 'message',
    role: 'user',
    blocks: [
      {
        type: 'text',
        text: 'hi',
        providerFrame: { provider: 'codex', kind: 'raw', payload: PAYLOAD }
      },
      { type: 'tool-call', name: 'Read', input: { path: 'a' } },
      { type: 'tool-result', output: 'ok', isError: false },
      { type: 'image-ref', path: '/tmp/a.png', alt: 'screenshot' },
      {
        type: 'subagent-group',
        groupId: 'claude-session:turn-1',
        agents: [
          { id: 'task-1', label: 'Explore', state: 'working', startedAt: 1_000 },
          { id: 'task-2', label: 'Review', state: 'completed', tokens: 42, settledAt: 2_000 }
        ]
      }
    ]
  },
  { kind: 'tool-call', name: 'Read', input: undefined, state: 'running' },
  { kind: 'tool-call', name: 'Read', input: {}, state: 'failed', output: PAYLOAD },
  { kind: 'diff', path: 'a.ts', patch: PAYLOAD },
  {
    kind: 'approval',
    title: 'Claude wants to present a plan',
    displayName: 'Present plan',
    description: 'Review the proposed implementation steps.',
    decisionReason: 'Plan mode requires approval.',
    blockedPath: '/repo/PLAN.md',
    matchedAskRule: { source: 'project', toolName: 'ExitPlanMode', ruleContent: 'ask' },
    subject: { kind: 'plan', text: '# Plan\n\n- Ship it', filePath: '/repo/PLAN.md' },
    detail: '# Plan\n\n- Ship it',
    options: [{ id: 'a', label: 'Yes' }],
    resolution: RESOLUTION
  },
  {
    kind: 'question',
    question: 'Deploy?',
    options: [{ id: 'a', label: 'Yes' }],
    freeTextQuestionId: 'q-free',
    resolution: { state: 'resolved', selectedOptionId: 'a', resolvedBy: 'client', resolvedAt: 5 }
  },
  { kind: 'status', text: 'working' },
  {
    kind: 'status',
    text: 'turn',
    turnLifecycle: { turnId: 'turn-1', state: 'running' },
    providerFrame: { provider: 'codex', kind: 'raw', payload: PAYLOAD }
  },
  {
    kind: 'status',
    text: 'turn',
    turnLifecycle: { turnId: 'turn-2', state: 'completed', startedAt: 1_000, completedAt: 188_000 }
  },
  {
    kind: 'status',
    text: 'turn',
    turnLifecycle: { turnId: 'turn-3', state: 'unverifiable', startedAt: 1_000 }
  },
  {
    kind: 'status',
    text: 'turn',
    turnLifecycle: { turnId: 'turn-4', state: 'completed', outcome: 'failure', startedAt: 1_000 }
  },
  {
    kind: 'turn',
    turnId: 'turn-5',
    state: 'interrupted',
    outcome: 'cancellation',
    startedAt: 1_000,
    completedAt: 2_000
  }
]

describe('canonical admission', () => {
  it('admits every body shape this build writes', () => {
    for (const body of CANONICAL_BODIES) {
      expect(isAdmissibleAgentJournalItemBody(body)).toBe(true)
    }
  })

  it('admits a canonical render item and submission', () => {
    const item: AgentJournalRenderItem = {
      itemId: 'codex:t:turn:0',
      revision: 1,
      body: { kind: 'message', role: 'user', blocks: [] },
      sequence: 1,
      observedAt: 1_000,
      recovered: true
    }
    expect(isAdmissibleAgentJournalRenderItem(item)).toBe(true)
    const submission: AgentJournalSubmission = {
      clientMessageId: 'm-1',
      fence: 1,
      payloadFingerprint: 'a'.repeat(64),
      dispatchState: 'unknown',
      providerItemId: null,
      reason: null,
      submittedAt: 1_000,
      resolvedAt: null
    }
    expect(isAdmissibleAgentJournalSubmission(submission)).toBe(true)
  })
})

describe('nested corruption is rejected', () => {
  it('rejects prompt bodies whose options or resolution cannot be rendered', () => {
    expect(
      isAdmissibleAgentJournalItemBody({
        kind: 'question',
        question: 'Deploy?',
        options: null,
        resolution: { state: 'resolved', selectedOptionId: 'a', resolvedBy: 'c', resolvedAt: 1 }
      })
    ).toBe(false)
    expect(
      isAdmissibleAgentJournalItemBody({
        kind: 'question',
        question: 'Deploy?',
        options: [],
        resolution: null
      })
    ).toBe(false)
    expect(
      isAdmissibleAgentJournalItemBody({
        kind: 'approval',
        title: 'Run?',
        detail: null,
        options: [{ id: 'a' }],
        resolution: RESOLUTION
      })
    ).toBe(false)
  })

  it('rejects broken payload, lifecycle, and block shapes', () => {
    expect(
      isAdmissibleAgentJournalItemBody({ kind: 'diff', path: 'a.ts', patch: { head: 'x' } })
    ).toBe(false)
    expect(
      isAdmissibleAgentJournalItemBody({ kind: 'status', text: 'x', turnLifecycle: true })
    ).toBe(false)
    expect(
      isAdmissibleAgentJournalItemBody({
        kind: 'message',
        role: 'user',
        blocks: [{ type: 'text', text: null }]
      })
    ).toBe(false)
    expect(
      isAdmissibleAgentJournalItemBody({ kind: 'message', role: 'user', blocks: 'not-blocks' })
    ).toBe(false)
  })

  it('rejects a subagent roster whose entries are malformed', () => {
    expect(
      isAdmissibleAgentJournalItemBody({
        kind: 'message',
        role: 'system',
        blocks: [{ type: 'subagent-group', groupId: 'g', agents: [{ id: 'a', label: 'x' }] }]
      })
    ).toBe(false)
    expect(
      isAdmissibleAgentJournalItemBody({
        kind: 'message',
        role: 'system',
        blocks: [{ type: 'subagent-group', groupId: 'g', agents: 'not-a-roster' }]
      })
    ).toBe(false)
  })

  it('keeps a state string a newer build might write admissible', () => {
    expect(
      isAdmissibleAgentJournalItemBody({
        kind: 'message',
        role: 'system',
        blocks: [
          {
            type: 'subagent-group',
            groupId: 'g',
            agents: [{ id: 'a', label: 'x', state: 'some-future-state' }]
          }
        ]
      })
    ).toBe(true)
  })

  it('refuses an empty producer id, which a presence test would read as a subagent', () => {
    const base = {
      itemId: 'codex:t:turn:0',
      revision: 1,
      body: { kind: 'message', role: 'user', blocks: [] },
      sequence: 1,
      observedAt: 1_000
    }
    expect(isAdmissibleAgentJournalRenderItem({ ...base, agentId: 'task-1' })).toBe(true)
    // An empty id is present and would hide the row from its author on parent-scoped surfaces.
    expect(isAdmissibleAgentJournalRenderItem({ ...base, agentId: '' })).toBe(false)
    expect(isAdmissibleAgentJournalRenderItem({ ...base, parentAgentId: '' })).toBe(false)
    expect(isAdmissibleAgentJournalRenderItem({ ...base, providerParentRef: '' })).toBe(false)
    expect(isAdmissibleAgentJournalRenderItem({ ...base, producerKind: '' })).toBe(false)
  })

  it('rejects shallow render items and submissions', () => {
    expect(
      isAdmissibleAgentJournalRenderItem({
        itemId: 'i-1',
        revision: 1,
        body: { kind: 'status', text: 'x' }
      })
    ).toBe(false)
    expect(isAdmissibleAgentJournalSubmission({ clientMessageId: 'm-1' })).toBe(false)
  })

  it('only admits message bodies for submissions', () => {
    expect(isAdmissibleAgentJournalMessageBody({ kind: 'status', text: 'x' })).toBe(false)
    expect(isAdmissibleAgentJournalMessageBody({ kind: 'message', role: 'user', blocks: [] })).toBe(
      true
    )
  })
})

describe('forward tolerance', () => {
  it('keeps unknown block types, wider state strings, and extra keys admissible', () => {
    expect(
      isAdmissibleAgentJournalItemBody({
        kind: 'message',
        role: 'narrator',
        blocks: [{ type: 'future-block', data: 1 }],
        futureField: 'ignored'
      })
    ).toBe(true)
    expect(
      isAdmissibleAgentJournalItemBody({ kind: 'tool-call', name: 'Read', state: 'paused' })
    ).toBe(true)
    expect(
      isAdmissibleAgentJournalSubmission({
        clientMessageId: 'm-1',
        fence: 1,
        payloadFingerprint: 'a'.repeat(64),
        dispatchState: 'some-future-state',
        providerItemId: null,
        reason: null,
        submittedAt: 1_000,
        resolvedAt: null
      })
    ).toBe(true)
  })

  it('admits a turn outcome from a later vocabulary but rejects a non-string one', () => {
    // Keep newer string verdicts readable for the outcome reader; nonstrings remain structural damage.
    const turn = { kind: 'turn', turnId: 'turn-1', state: 'completed' }
    expect(isAdmissibleAgentJournalItemBody({ ...turn, outcome: 'partially-refused' })).toBe(true)
    expect(isAdmissibleAgentJournalItemBody({ ...turn, outcome: 7 })).toBe(false)
    expect(isAdmissibleAgentJournalItemBody({ ...turn, outcome: '' })).toBe(false)
    expect(
      isAdmissibleAgentJournalItemBody({
        kind: 'status',
        text: 'turn',
        turnLifecycle: { turnId: 'turn-1', state: 'completed', outcome: 'partially-refused' }
      })
    ).toBe(true)
    expect(
      isAdmissibleAgentJournalItemBody({
        kind: 'status',
        text: 'turn',
        turnLifecycle: { turnId: 'turn-1', state: 'completed', outcome: 7 }
      })
    ).toBe(false)
  })
})

describe('optional notice metadata', () => {
  it.each([
    {},
    { presentation: 'compaction' },
    { presentation: 'compaction-skipped', tone: 'warning' },
    { presentation: 'plan-document' },
    { tone: 'warning' },
    { tone: 'error' },
    { tone: 'notice' },
    { presentation: 'future-presentation', tone: 'future-tone' }
  ])('admits existing status and text kinds with %j', (metadata) => {
    expect(
      isAdmissibleAgentJournalItemBody({ kind: 'status', text: 'Readable fallback', ...metadata })
    ).toBe(true)
    expect(
      isAdmissibleAgentJournalItemBody({
        kind: 'message',
        role: 'system',
        blocks: [{ type: 'text', text: 'Readable fallback', ...metadata }]
      })
    ).toBe(true)
  })
  it.each([{ tone: false }, { presentation: {} }])('rejects malformed metadata: %j', (metadata) => {
    expect(isAdmissibleAgentJournalItemBody({ kind: 'status', text: 'Text', ...metadata })).toBe(
      false
    )
  })
})

describe('typed failure facts', () => {
  it('admits a status row and a submission with a fact, and the same rows without one', () => {
    const failure = {
      kind: 'providerStartFailed',
      detail: { text: 'exit status 1', audience: 'log' }
    }
    expect(isAdmissibleAgentJournalItemBody({ kind: 'status', text: 'Stopped.', failure })).toBe(
      true
    )
    expect(isAdmissibleAgentJournalItemBody({ kind: 'status', text: 'Stopped.' })).toBe(true)
    const submission = {
      clientMessageId: 'cm-1',
      fence: 1,
      payloadFingerprint: 'fp',
      dispatchState: 'rejected',
      providerItemId: null,
      reason: 'Not sent.',
      submittedAt: 1,
      resolvedAt: 2
    }
    expect(isAdmissibleAgentJournalSubmission(submission)).toBe(true)
    expect(isAdmissibleAgentJournalSubmission({ ...submission, rejection: failure })).toBe(true)
  })

  it("admits a refusal's details, and a row an earlier build wrote with its cause", () => {
    const refused = (refusal: Record<string, unknown>) =>
      isAdmissibleAgentJournalItemBody({
        kind: 'status',
        text: "Codex couldn't restart.",
        failure: { kind: 'restartFailed', refusal }
      })
    expect(
      refused({
        code: 'agent_session_conflict',
        details: { reason: 'claimConflicted', futureFact: 1 }
      })
    ).toBe(true)
    expect(refused({ code: 'agent_session_conflict', cause: 'claimConflicted' })).toBe(true)
  })

  it('keeps a kind or audience a newer host writes admissible', () => {
    expect(
      isAdmissibleAgentJournalItemBody({
        kind: 'status',
        text: 'Stopped.',
        failure: { kind: 'futureKind', detail: { text: 'x', audience: 'future' } }
      })
    ).toBe(true)
  })
})

describe('optional tool annotations', () => {
  const body = { kind: 'tool-call', name: 'shell', input: null, state: 'completed' }
  it('admits old rows and rows with optional annotations without a new kind', () => {
    expect(isAdmissibleAgentJournalItemBody(body)).toBe(true)
    expect(
      isAdmissibleAgentJournalItemBody({ ...body, callId: 'call-1', exitCode: 0, durationMs: 0 })
    ).toBe(true)
    expect(
      isAdmissibleAgentJournalItemBody({
        ...body,
        webSearchResults: [{ title: 'Docs', url: 'https://example.com' }]
      })
    ).toBe(true)
    const padded = AgentJournalItemBodySchema.safeParse({ ...body, callId: ' call-1 ' })
    expect(padded.success).toBe(true)
    if (padded.success) {
      expect(padded.data).toMatchObject({ kind: 'tool-call', callId: ' call-1 ' })
    }
  })
  it('admits explicit MCP identity without constraining the raw name', () => {
    expect(
      isAdmissibleAgentJournalItemBody({
        ...body,
        name: 'my_server/ns.tool',
        mcpIdentity: { server: 'my_server', tool: 'ns.tool' }
      })
    ).toBe(true)
  })
  it.each([
    { callId: '' },
    { callId: ' \t' },
    { callId: 1 },
    { exitCode: '127' },
    { exitCode: 1.5 },
    { durationMs: -1 },
    { webSearchResults: [null] }
  ])('rejects malformed annotation %s', (metadata) =>
    expect(isAdmissibleAgentJournalItemBody({ ...body, ...metadata })).toBe(false)
  )

  it('rejects whitespace-only provider IDs in message blocks too', () => {
    expect(
      isAdmissibleAgentJournalItemBody({
        kind: 'message',
        role: 'assistant',
        blocks: [{ type: 'tool-call', name: 'shell', input: null, callId: '\n\t' }]
      })
    ).toBe(false)
  })
})

describe('thread goal fields', () => {
  const GOAL = {
    objective: 'Ship the parser',
    status: 'active',
    tokenBudget: null,
    tokensUsed: 0,
    timeUsedSeconds: 0,
    createdAt: 1_000,
    updatedAt: 1_000
  } as const

  it('admits a user message sent as a goal and a typed goal transition', () => {
    const bodies: AgentJournalItemBody[] = [
      {
        kind: 'message',
        role: 'user',
        blocks: [{ type: 'text', text: 'Ship the parser' }],
        sentAs: 'goal'
      },
      {
        kind: 'status',
        text: 'Goal set: Ship the parser',
        threadGoal: { state: 'set', goal: GOAL }
      },
      { kind: 'status', text: 'Goal cleared', threadGoal: { state: 'cleared' } }
    ]
    for (const body of bodies) {
      expect(isAdmissibleAgentJournalItemBody(body)).toBe(true)
    }
    expect(isAdmissibleAgentJournalMessageBody(bodies[0])).toBe(true)
  })

  it('keeps a send mode or goal state a newer build writes admissible', () => {
    expect(
      isAdmissibleAgentJournalItemBody({
        kind: 'message',
        role: 'user',
        blocks: [],
        sentAs: 'scheduled'
      })
    ).toBe(true)
    expect(
      isAdmissibleAgentJournalItemBody({
        kind: 'message',
        role: 'reasoning',
        blocks: [],
        state: 'paused'
      })
    ).toBe(true)
    expect(
      isAdmissibleAgentJournalItemBody({
        kind: 'status',
        text: 'Goal archived',
        threadGoal: { state: 'archived' }
      })
    ).toBe(true)
    expect(
      isAdmissibleAgentJournalItemBody({
        kind: 'status',
        text: 'Goal set',
        threadGoal: { state: 'set', goal: { ...GOAL, status: 'snoozed' } }
      })
    ).toBe(true)
  })

  it('rejects a malformed send mode or goal snapshot', () => {
    for (const body of [
      { kind: 'message', role: 'user', blocks: [], sentAs: 5 },
      { kind: 'message', role: 'user', blocks: [], sentAs: '' },
      { kind: 'message', role: 'reasoning', blocks: [], state: '' },
      { kind: 'message', role: 'reasoning', blocks: [], completedAt: 'later' },
      { kind: 'status', text: 'Goal set', threadGoal: { state: 'set' } },
      {
        kind: 'status',
        text: 'Goal set',
        threadGoal: { state: 'set', goal: { ...GOAL, objective: null } }
      },
      {
        kind: 'status',
        text: 'Goal set',
        threadGoal: { state: 'set', goal: { ...GOAL, timeUsedSeconds: 'soon' } }
      },
      { kind: 'status', text: 'Goal set', threadGoal: 'set' }
    ]) {
      expect(isAdmissibleAgentJournalItemBody(body)).toBe(false)
    }
  })
})