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.

11.3 KB
Raw Blame
import { describe, expect, it } from 'vitest'
import { applyCommand, type Command, type CommandContext } from '../commands.js'
import { createSuperTab, type Actor } from '../model.js'
import { openSwarmBuddyChat } from '../buddies.js'
import { touchesFor } from './touches.js'

const HOLD = { kind: 'person', personId: 'owner', defaultAt: '2026-11-06T02:00:00.000Z' } as const

const owner: Extract<Actor, { kind: 'person' }> = { kind: 'person', userId: 'owner', email: '[email protected]' }
const config = { engine: 'codex', model: 'm', effort: 'high', contextWindow: null, engineVersion: null }

/** A Swarm with a Lead and two workers, each with its own assigned group. */
function swarm() {
  let state = createSuperTab('tab', 'Pilot', { id: 'owner', email: owner.email }, 'prod')
  let n = 0
  const run = (command: Command, actor: Actor = owner, context: Partial<CommandContext> = {}) => {
    const before = state
    const effects = applyCommand(state, actor, command, { now: 1000, operationId: `op-${++n}`, ...context })
    state = effects.state
    return { effects, before, touches: touchesFor(command, actor, before, effects, context) }
  }
  const contribution = { contribution: { machineOwnerId: 'host-owner', contributorId: 'owner' } }
  const add = (harnessId: string, conversationId: string, lead = false) => run({ type: 'agent.add', conversationId, machineId: 'host', harnessId,
    name: harnessId, configuration: config, lead }, owner, contribution).effects.result.agentId as string
  const lead = add('lead', 'lead', true)
  const group = (title: string) => {
    const id = run({ type: 'conversation.create', title, brief: 'Do it', checks: [{ description: 'Done', independent: false }] }).effects.result.conversationId as string
    const worker = add(`${title}-worker`, id)
    const assignmentId = run({ type: 'work.assign', conversationId: id, agentId: worker }).effects.result.assignmentId as string
    return { id, worker, assignmentId }
  }
  const agentActor = (agentId: string): Actor => ({ kind: 'agent', agentId, machineOwnerId: 'host-owner' })
  return { run, lead, group, agentActor, get state() { return state } }
}
const has = (touches: Array<{ kind: string; key: string }>, kind: string, key: string) => touches.some(t => t.kind === kind && t.key === key)

describe('the parts a command touches (design section 1.2, column T)', () => {
  it('touches the actor\'s own part on every command', () => {
    const f = swarm(), a = f.group('A')
    expect(has(f.run({ type: 'message.send', conversationId: a.id, text: 'Hi', replyTo: null, recipientIds: [] }).touches, 'member', 'owner')).toBe(true)
    expect(has(f.run({ type: 'message.send', conversationId: a.id, text: 'Hi', replyTo: null, recipientIds: [] }, f.agentActor(a.worker)).touches, 'agent', a.worker)).toBe(true)
  })

  it('makes a Lead\'s change to an agent\'s avatar meet on the Lead conversation its check reads', () => {
    const f = swarm(), a = f.group('A')
    const { touches } = f.run({ type: 'agent.avatar', agentId: a.worker, avatar: { kind: 'preset', key: 'owl' } }, f.agentActor(f.lead))
    expect(has(touches, 'group', 'lead')).toBe(true)
    expect(has(touches, 'agent', a.worker)).toBe(true)
  })

  it('makes the coordinator\'s instructions save, notes and name meet a Lead change on the Lead conversation its check reads (review of phase 2)', () => {
    const f = swarm(), lead = f.agentActor(f.lead)
    const notes = [{ id: 'n1', text: 'Not machine A.', askedBy: 'owner', at: 500, expiresAt: null }]
    for (const [command, context] of [
      [{ type: 'instructions.save', projectId: 'lead', baseRevision: 1, text: 'PM text.', summary: 'PM text', askedBy: 'owner' }, {}],
      [{ type: 'note.add', projectId: 'lead', text: 'Not machine B.', askedBy: 'owner' }, {}],
      [{ type: 'note.remove', projectId: 'lead', noteId: 'n1' }, { notes }],
      [{ type: 'project.coordinatorName', projectId: 'lead', name: 'Lead engineer' }, {}],
      [{ type: 'project.answers', projectId: 'lead', inThread: true }, {}],
      [{ type: 'project.about', projectId: 'lead', about: 'The Harness app' }, {}],
      // An older CLI's rule, a save to the instructions after the one above (project instructions, phase 4).
      [{ type: 'rule.set', rule: { kind: 'preferIdle' }, text: 'Idle first.', askedIn: { conversationId: 'lead', messageId: 'm1' } },
        { askedIn: { author: { kind: 'person', id: 'owner', name: 'owner', membershipGeneration: 'g' }, source: false, createdAt: 0 },
          instructions: { revision: 2, text: 'PM text.' } }],
    ] as Array<[Command, Partial<CommandContext>]>) {
      expect(has(f.run(command, lead, context).touches, 'group', 'lead'), command.type).toBe(true)
    }
  })

  it('leaves a post\'s recipients alone, so workers posting to their Lead in different groups never meet on the Lead', () => {
    const f = swarm(), a = f.group('A')
    const { effects, touches } = f.run({ type: 'message.send', conversationId: a.id, text: 'Report', replyTo: null, recipientIds: [f.lead] }, f.agentActor(a.worker))
    expect(effects.messages[0]!.recipients).toEqual([f.lead])
    expect(has(touches, 'agent', f.lead)).toBe(false)
  })

  it('touches an agent a post names from outside its group, which joins, but not one already there', () => {
    const f = swarm(), a = f.group('A'), b = f.group('B')
    const { effects, touches } = f.run({ type: 'message.send', conversationId: a.id, text: 'Look', replyTo: null, recipientIds: [b.worker] }, f.agentActor(a.worker))
    expect(effects.messages[0]!.recipients).toEqual([b.worker])
    expect(has(touches, 'agent', b.worker)).toBe(true)
    // Already a member now: an ordinary post to it no longer touches it.
    expect(has(f.run({ type: 'message.send', conversationId: a.id, text: 'Again', replyTo: null, recipientIds: [b.worker] }, f.agentActor(a.worker)).touches, 'agent', b.worker)).toBe(false)
  })
  // The project's progress part was also the fence of the worker decisions the Lead had to make before its next start,
  // assignment or route (design ruling R5). No command reads those decisions now (project instructions, phase 4, decision 17),
  // so finishing work and the next start, assignment or route no longer meet there (final review of the branch).
  it('leaves the project\'s progress part to its reports when work finishes, and when the next is defined, routed or assigned', () => {
    const f = swarm(), a = f.group('A')
    f.run({ type: 'work.check', conversationId: a.id, assignmentId: a.assignmentId, checkId: f.state.conversations.find(c => c.id === a.id)!.checks[0]!.id,
      passed: true, evidence: 'Shown' }, f.agentActor(f.lead))
    const done = f.run({ type: 'work.status', conversationId: a.id, assignmentId: a.assignmentId, status: 'done', result: 'Shipped' }, f.agentActor(a.worker))
    expect(has(done.touches, 'progress', 'lead')).toBe(false)
    const cancelled = f.group('B')
    expect(has(f.run({ type: 'work.cancel', conversationId: cancelled.id, assignmentId: cancelled.assignmentId, reason: 'Not needed' }).touches, 'progress', 'lead')).toBe(false)
    expect(has(f.run({ type: 'work.define', conversationId: cancelled.id, brief: 'Again', checks: [] }).touches, 'progress', 'lead')).toBe(false)
    const next = f.run({ type: 'conversation.create', title: 'C', brief: 'More', checks: [{ description: 'Done', independent: false }] }).effects.result.conversationId as string
    expect(has(f.run({ type: 'work.assign', conversationId: next, agentId: a.worker }).touches, 'progress', 'lead')).toBe(false)
  })

  it('makes naming a requester meet that person\'s removal on their member part', () => {
    const f = swarm()
    const invited = f.run({ type: 'invite.create', email: '[email protected]', role: 'collaborator', conversationId: 'lead' }).effects.result.inviteId as string
    f.run({ type: 'invite.accept', inviteId: invited }, { kind: 'person', userId: 'peer', email: '[email protected]' })
    const opened = f.run({ type: 'conversation.create', title: 'R', brief: 'Do it', checks: [{ description: 'Done', independent: false }], priority: 'P2', requesterId: 'peer' },
      f.agentActor(f.lead))
    expect(has(opened.touches, 'member', 'peer')).toBe(true)
    const id = opened.effects.result.conversationId as string
    const named = f.run({ type: 'conversation.requester', conversationId: id, personId: 'peer' }, f.agentActor(f.lead))
    expect(has(named.touches, 'member', 'peer')).toBe(true)
    expect(has(named.touches, 'group', 'lead')).toBe(true)
  })

  it('makes a priority change meet a change of who is PM on the project\'s chat (task #3117)', () => {
    const f = swarm()
    const id = f.run({ type: 'conversation.create', title: 'R', brief: 'Do it', checks: [{ description: 'Done', independent: false }], priority: 'P2', requesterId: 'owner' }, f.agentActor(f.lead)).effects.result.conversationId as string
    const changed = f.run({ type: 'work.priority', conversationId: id, priority: 'P0' }, f.agentActor(f.lead))
    expect(has(changed.touches, 'group', 'lead')).toBe(true)
  })

  it('makes an answer to waiting work meet a change to whom it waits on', () => {
    const f = swarm(), a = f.group('A')
    f.run({ type: 'conversation.requester', conversationId: a.id, personId: 'owner' })
    f.run({ type: 'work.status', conversationId: a.id, assignmentId: a.assignmentId, status: 'needs_human', hold: HOLD, result: 'Flag?' }, f.agentActor(a.worker))
    const answer = f.run({ type: 'message.send', conversationId: a.id, text: 'Flag.', replyTo: null, recipientIds: [] }, f.agentActor(f.lead))
    expect(has(answer.touches, 'member', 'owner')).toBe(true)
  })

  it('makes two starts on one machine meet on its slot ledger and on the head', () => {
    const f = swarm()
    const request = { conversationId: f.group('A').id, machineId: 'host', engine: 'codex', name: 'New', cwd: '/w', worktree: false,
      requirements: { memoryBytes: 0, diskBytes: 0, maxCpuPercent: 100, tools: [], devices: [], quotaRequired: false } }
    const { touches } = f.run({ type: 'agent.start', request }, owner, { contribution: { machineOwnerId: 'owner', contributorId: 'owner' } })
    expect(has(touches, 'machine', 'host')).toBe(true)
    expect(has(touches, 'head', 'head')).toBe(true)
  })

  // Review of #3560: + New agent picks a name free across the Swarm, so two at once must meet, as Buddy names do.
  it('makes two new agents meet on the head, where their names are claimed', () => {
    const f = swarm()
    const { touches } = f.run({ type: 'agent.new', kind: 'regular', machineId: 'host', engine: 'claude', local: true }, owner,
      { contribution: { machineOwnerId: 'owner', contributorId: 'owner' } })
    expect(has(touches, 'head', 'head')).toBe(true)
  })
})


it('guards an already awake Buddy hearing its own chat against a concurrent sleep', () => {
  const f = swarm(), a = f.group('A')
  const buddy = f.state.agents.find(agent => agent.id === a.worker)!
  buddy.buddyOf = { personId: 'owner', generation: f.state.people[0]!.generation }
  buddy.buddyScope = 'swarm'
  const chat = openSwarmBuddyChat(f.state, f.state.people[0]!)
  chat.agentIds.push(buddy.id)
  const touches = touchesFor({ type: 'message.send', conversationId: chat.id, text: 'Hi', replyTo: null, recipientIds: [] },
    owner, f.state, { state: f.state, messages: [{ id: 'input', conversationId: chat.id, text: 'Hi', replyTo: null,
      recipients: [buddy.id], author: { kind: 'person', id: 'owner', name: 'Owner', membershipGeneration: f.state.people[0]!.generation } }] })
  expect(has(touches, 'agent', buddy.id)).toBe(true)
})