Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

12.9 KB
Raw Blame
import { describe, expect, it } from 'vitest'
import { applyCommand, type Command, type CommandContext } from './commands.js'
import { createSuperTab, type Actor, type AgentConfiguration } from './model.js'
import type { ContributedMachine } from './machines.js'
import { builtBy, reviewerCandidates, stepCandidates, strengthOf } from './reviewerPick.js'

const owner: Extract<Actor, { kind: 'person' }> = { kind: 'person', userId: 'owner', email: '[email protected]' }
const opus: AgentConfiguration = { engine: 'claude', model: 'opus', effort: 'high', contextWindow: 200000, engineVersion: 'fixture' }
const sonnet: AgentConfiguration = { ...opus, model: 'sonnet' }
const sol: AgentConfiguration = { ...opus, engine: 'codex', model: 'gpt-6.1-sol' }

function fixture(engineer: AgentConfiguration = opus) {
  let state = createSuperTab('tab', 'Pilot', { id: 'owner', email: owner.email }, 'prod')
  let sequence = 0
  const now = 1_000_000
  const run = (command: Command, actor: Actor = owner, context: Partial<CommandContext> = {}) => {
    const effects = applyCommand(state, actor, command, { now, operationId: `op-${++sequence}`, ...context })
    state = effects.state
    return effects
  }
  const as = (agentId: string): Actor => ({ kind: 'agent', agentId, machineOwnerId: 'host-owner' })
  const add = (harnessId: string, group: string, configuration = opus, extra: Record<string, unknown> = {}) => run({ type: 'agent.add', conversationId: group,
    machineId: 'host', harnessId, name: harnessId, configuration, lead: false, ...extra } as Command, owner,
    { contribution: { machineOwnerId: 'host-owner', contributorId: 'owner' } }).result.agentId as string
  const lead = run({ type: 'agent.add', conversationId: 'lead', machineId: 'host', harnessId: 'pm', name: 'pm', configuration: opus, lead: true }, owner,
    { contribution: { machineOwnerId: 'host-owner', contributorId: 'owner' } }).result.agentId as string
  const task = run({ type: 'conversation.create', title: 'Fix login', brief: 'Sign in works offline.', checks: [{ description: 'Signs in offline', independent: true }], priority: 'P2', requesterId: 'owner' }, as(lead)).result.conversationId as string
  const dev = add('dev', task, engineer)
  run({ type: 'work.assign', conversationId: task, agentId: dev }, as(lead))
  const group = (id: string) => state.conversations.find(c => c.id === id)!
  const pick = (kind: 'review' | 'security' | 'test' = 'review', head: string | null = 'abc1234') =>
    reviewerCandidates(state, group(task), kind, head, now).map(a => a.name)
  return { run, as, add, lead, task, dev, group, pick, now, get state() { return state } }
}

describe('strengthOf', () => {
  it('reads the catalog by engine, model and effort; a grid model or an unknown one has none', () => {
    const f = fixture()
    const agent = (name: string) => f.state.agents.find(a => a.name === name)!
    f.add('o', 'lead', opus); f.add('s', 'lead', sonnet); f.add('x', 'lead', { ...opus, model: 'mystery' })
    expect(strengthOf(f.state, agent('o'))).toBe(3)
    expect(strengthOf(f.state, agent('s'))).toBe(2)
    expect(strengthOf(f.state, agent('x'))).toBeNull()
    const grid = agent('o')
    grid.gridModel = 'kimi k3'
    expect(strengthOf(f.state, grid)).toBeNull()
  })
})

describe('builtBy', () => {
  it('names the assignee and every agent that ran the task', () => {
    const f = fixture()
    expect([...builtBy(f.group(f.task))]).toEqual([f.dev])
  })
})

describe('reviewerCandidates', () => {
  it('is never the engineer, nor an agent that built the task before', () => {
    const f = fixture()
    const before = f.add('before', f.task)
    f.group(f.task).ledger!.runs!.push({ ...f.group(f.task).ledger!.runs![0]!, agentId: before })
    f.add('rev', f.task, opus, { roleName: 'Reviewer' })
    expect(f.pick()).toEqual(['rev'])
  })

  it('is never a grid model, nor weaker than the engineer, nor of unknown strength', () => {
    const f = fixture()
    f.add('weak', f.task, sonnet, { roleName: 'Reviewer' })
    f.add('mystery', f.task, { ...opus, model: 'mystery' }, { roleName: 'Reviewer' })
    const grid = f.add('grid', f.task, opus, { roleName: 'Reviewer' })
    f.state.agents.find(a => a.id === grid)!.gridModel = 'kimi k3'
    f.add('strong', f.task, sol, { roleName: 'Reviewer' })
    expect(f.pick()).toEqual(['strong'])
  })

  it('takes a weaker engineer\'s reviewer at its own strength or more', () => {
    const f = fixture(sonnet)
    f.add('s', f.task, sonnet, { roleName: 'Reviewer' })
    f.add('o', f.task, opus, { roleName: 'Reviewer' })
    expect(f.pick().sort()).toEqual(['o', 's'])
  })

  it('is never an agent with a verdict at this head; one at an older head may review again', () => {
    const f = fixture()
    const read = f.add('read', f.task, opus, { roleName: 'Reviewer' })
    const older = f.add('older', f.task, opus, { roleName: 'Reviewer' })
    f.group(f.task).checks = [{ id: 'c', description: 'Reviewed', independent: true, evidence: null, passedBy: null, verdicts: [
      { by: `agent:${read}`, passed: true, evidence: 'Yes', at: 5, head: 'abc1234' },
      { by: `agent:${older}`, passed: true, evidence: 'Yes', at: 5, head: 'fff0000' },
    ] } as never]
    expect(f.pick('security', 'abc1234')).toEqual(['older'])
  })

  it('compares whole heads: a verdict naming no head, or a prefix of this one, is not at this head (#3394)', () => {
    const f = fixture()
    const bare = f.add('bare', f.task, opus, { roleName: 'Reviewer' })
    const prefix = f.add('prefix', f.task, opus, { roleName: 'Reviewer' })
    const head = 'abc1234'.padEnd(40, '0')
    f.group(f.task).checks = [{ id: 'c', description: 'Reviewed', independent: true, evidence: null, passedBy: null, verdicts: [
      { by: `agent:${bare}`, passed: true, evidence: 'Yes', at: 50 },
      { by: `agent:${prefix}`, passed: true, evidence: 'Yes', at: 50, head: 'abc1234' } as never,
    ] }]
    expect(f.pick('security', head).sort()).toEqual(['bare', 'prefix'])
  })

  // Task #4534: Ownrev6 did Security review 2 of #3542 and of #3545, then was handed Security review 1 at the next head.
  it('never offers one security review to the agent that did or judged the other; its own it may take again', () => {
    const f = fixture()
    const did = f.add('did', f.task, opus, { roleName: 'Reviewer' }), judged = f.add('judged', f.task, opus, { roleName: 'Reviewer' })
    f.add('free', f.task, opus, { roleName: 'Reviewer' })
    const open = (name: string) => f.run({ type: 'conversation.create', title: name, reviewOf: f.task, step: name, brief: 'Read it.', checks: [] } as Command,
      f.as(f.lead)).result.conversationId as string
    const one = open('Security review 1'), two = open('Security review 2')
    f.run({ type: 'work.assign', conversationId: two, agentId: did }, f.as(f.lead))
    f.run({ type: 'work.status', conversationId: two, assignmentId: f.group(two).assignment!.id, status: 'done', result: 'Ready to merge: Yes' } as Command, f.as(did))
    // A verdict on the other step at an older head counts too: the two reviews are of one change.
    f.group(two).checks = [{ id: 'c', description: 'Reviewed', independent: true, evidence: null, passedBy: null,
      verdicts: [{ by: `agent:${judged}`, passed: true, evidence: 'Yes', at: 5, head: 'fff0000' }] } as never]
    const names = (step: string) => stepCandidates(f.state, f.group(step), f.now).map(a => a.name).sort()
    expect(names(one)).toEqual(['free'])
    expect(names(two)).toEqual(['did', 'free', 'judged'])
  })

  it('is never busy, a PM, a Buddy, or holding another step of this task', () => {
    const f = fixture()
    const busy = f.add('busy', f.task, opus, { roleName: 'Reviewer' })
    const other = f.run({ type: 'conversation.create', title: 'Other', brief: 'Other work.', checks: [{ description: 'Done', independent: false }], priority: 'P2', requesterId: 'owner' }, f.as(f.lead)).result.conversationId as string
    f.run({ type: 'work.assign', conversationId: other, agentId: busy }, f.as(f.lead))
    const buddy = f.add('buddy', f.task, opus, { roleName: 'Reviewer' })
    f.state.agents.find(a => a.id === buddy)!.buddyOf = { personId: 'owner', generation: 'g' }
    const holder = f.add('holder', f.task, opus, { roleName: 'Reviewer' })
    const step = f.run({ type: 'conversation.create', title: 'Security review 1', reviewOf: f.task, step: 'Security review 1', brief: 'Read it.', checks: [] } as Command, f.as(f.dev)).result.conversationId as string
    f.run({ type: 'work.assign', conversationId: step, agentId: holder }, f.as(f.dev))
    f.add('free', f.task, opus, { roleName: 'Reviewer' })
    expect(f.pick('security')).toEqual(['free'])
  })

  // Task #3602: only the step's own role, with no other free agent standing in.
  it('takes only the step\'s role; never an Engineer, a Guide or an Integrator', () => {
    const f = fixture()
    f.add('eng', f.task, opus, { roleName: 'Engineer' })
    f.add('guide', f.task, opus, { roleName: 'Guide' })
    f.add('integrator', f.task, opus, { roleName: 'Integrator' })
    f.add('tst', f.task, opus, { roleName: 'Tester' })
    f.add('rev', f.task, opus, { roleName: 'Reviewer' })
    expect(f.pick('review')).toEqual(['rev'])
    expect(f.pick('test')).toEqual(['tst'])
  })

  // Review of #3227 (Digest): on the live Swarm the Guides carry the role Reviewer and the Integrator none (an Engineer).
  // Task #3602: #3576's Test step went to an Engineer. A server-opened step goes only to its own role, with no stand-in.
  it('is only the step\'s own role: never an Engineer, nor an agent of no role', () => {
    const f = fixture()
    f.add('eng', f.task, opus, { roleName: 'Engineer' }); f.add('plain', f.task, opus)
    expect(f.pick('review')).toEqual([])
    expect(f.pick('test')).toEqual([])
    f.add('rev', f.task, opus, { roleName: 'Reviewer' }); f.add('tst', f.task, opus, { roleName: 'Tester' })
    expect(f.pick('review')).toEqual(['rev'])
    expect(f.pick('security')).toEqual(['rev'])
    expect(f.pick('test')).toEqual(['tst'])
  })

  it('is never the Guide or the Integrator, whatever role name they carry', () => {
    const f = fixture()
    f.add('Guide', f.task, opus, { roleName: 'Reviewer' })
    f.add('Guide2', f.task, opus, { roleName: 'Reviewer' })
    f.add('Integrator', f.task, opus)
    f.add('rev', f.task, opus, { roleName: 'Reviewer' })
    expect(f.pick()).toEqual(['rev'])
  })

  it('is never an agent whose machine cannot start work now: out of quota, signed out, paused or offline', () => {
    const f = fixture()
    const machine = (machineId: string, usedPercent: number, extra: Partial<ContributedMachine> = {}): ContributedMachine => ({ machineId, name: machineId,
      contributorId: 'owner', membershipGeneration: 'g', generation: 'g1', cwd: '/swarm', scope: 'swarm', engines: ['claude'], maxAgents: 4, paused: false,
      observation: { generation: 'g1', receivedAt: f.now - 1000, sessionKind: 'computer', resources: {}, tools: {}, runningHarnessIds: [], stoppedHarnessIds: [],
        readiness: { engines: [{ engine: 'claude', installed: true, signedIn: true }] },
        quotas: [{ engine: 'claude', known: true, windows: [{ name: 'weekly', usedPercent, resetsAt: null }] }] }, ...extra } as unknown as ContributedMachine)
    f.add('tired-rev', f.task, opus, { roleName: 'Reviewer', machineId: 'tired' })
    f.add('paused-rev', f.task, opus, { roleName: 'Reviewer', machineId: 'resting' })
    f.add('rested-rev', f.task, opus, { roleName: 'Reviewer', machineId: 'rested' })
    f.state.machines = [machine('tired', 100), machine('rested', 15), machine('resting', 15, { paused: true })]
    expect(f.pick()).toEqual(['rested-rev'])
  })

  it('ranks by the model an agent runs, its engine\'s default included', () => {
    const f = fixture(sol)
    f.add('opus-rev', f.task, opus, { roleName: 'Reviewer' })
    const own = f.add('default-rev', f.task, { ...sol, model: null, effort: null }, { roleName: 'Reviewer' })
    f.state.agents.find(a => a.id === own)!.launchedDefault = true
    expect(f.pick()).toEqual(['default-rev', 'opus-rev'])
  })

  it('is never an agent of another project', () => {
    const f = fixture()
    f.run({ type: 'project.create', name: 'Other', visibility: 'public' } as Command)
    const otherLead = f.state.projects!.find(p => p.name === 'Other')!.leadConversationId
    f.add('away', otherLead, opus, { roleName: 'Reviewer' })
    expect(f.pick()).toEqual([])
  })

  // Task #3774: a test step's Tester needs a mid-strength model or stronger, whatever the engineer runs; a review keeps its bar.
  it('takes a Sonnet Tester for an Opus engineer\'s test step, never for its review', () => {
    const f = fixture()
    f.add('s-tst', f.task, sonnet, { roleName: 'Tester' })
    f.add('s-rev', f.task, sonnet, { roleName: 'Reviewer' })
    const grid = f.add('g-tst', f.task, opus, { roleName: 'Tester' })
    f.state.agents.find(a => a.id === grid)!.gridModel = 'kimi k3'
    f.add('h-tst', f.task, { ...opus, model: 'haiku', effort: 'auto' }, { roleName: 'Tester' })
    expect(f.pick('test')).toEqual(['s-tst'])
    expect(f.pick('review')).toEqual([])
    expect(f.pick('security')).toEqual([])
  })
})