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.

22.9 KB
Raw Blame
import { describe, expect, it } from 'vitest'
import { applyCommand, type Command, type CommandContext } from './commands.js'
import { createSuperTab, forRecipients, type Actor, type Conversation, type MessageDraft } from './model.js'
import { buddyChatId } from './buddies.js'
import { assignedNotice, buddiesTold } from './buddyNews.js'
import { SHIP_KIND } from './ship.js'
import { pendingKey, settleReleases } from './releases.js'
import { TASK_LINE_KIND } from './taskLine.js'
import { leaderboard } from './leaderboard/stats.js'
import { weekStart } from './leaderboard/week.js'

const owner: Extract<Actor, { kind: 'person' }> = { kind: 'person', userId: 'owner', email: '[email protected]' }
const peer: Extract<Actor, { kind: 'person' }> = { kind: 'person', userId: 'peer', email: '[email protected]' }
const configuration = { engine: 'claude', model: null, effort: null, contextWindow: null, engineVersion: null }
const agentActor = (agentId: string): Actor => ({ kind: 'agent', agentId, machineOwnerId: 'host' })
const proposal = { text: 'Sync edits made offline', goal: 'Sync edits made offline', scope: 'Desktop drafts only', priority: 'high' as const }

/** A public project with its PM, a worker, and peer's Buddy with its chat, as buddy.create and its start leave them. */
function setup() {
  let state = createSuperTab('swarm', 'Team', { id: 'owner', email: owner.email }, 'test')
  state.people.push({ id: 'peer', email: peer.email, role: 'collaborator', generation: 'first' })
  let next = 0
  const run = (command: Command, actor: Actor = owner, context: Partial<CommandContext> = {}) => {
    const effects = applyCommand(state, actor, command, { operationId: `op${next++}`, now: 1000,
      contribution: { machineOwnerId: 'host', contributorId: actor.kind === 'person' ? actor.userId : 'owner' }, ...context })
    state = effects.state
    return effects
  }
  const app = run({ type: 'project.create', name: 'App', visibility: 'public', brief: '' }).result as { projectId: string; conversationId: string }
  const add = (conversationId: string, harnessId: string, lead = false) => run({ type: 'agent.add', conversationId, harnessId, machineId: 'machine',
    name: harnessId, configuration, lead }).result.agentId as string
  const pm = add(app.conversationId, 'pm', true)
  state.agents.push({ id: 'buddy', projectId: app.projectId, machineId: 'machine', harnessId: 'buddy-harness', machineOwnerId: 'host',
    contributorId: 'peer', membershipGeneration: 'first', name: 'Buddy', configuration, buddyOf: { personId: 'peer', generation: 'first' } })
  const chat: Conversation = { id: buddyChatId(app.projectId, { id: 'peer', generation: 'first' }), kind: 'buddy', projectId: app.projectId, title: 'Buddy',
    leadId: null, agentIds: ['buddy'], archived: false, brief: '', checks: [], assignment: null, lastMessageSequence: 0,
    audience: { people: [{ id: 'peer', generation: 'first' }] } }
  state.conversations.push(chat)
  /** peer's Buddy sends their proposal; the store reads the ask it names. */
  const proposed = () => run({ type: 'buddy.propose', conversationId: chat.id, ownerMessageId: 'ask', ...proposal }, agentActor('buddy'),
    { proposalAsk: { id: 'ask', conversationId: chat.id, source: false, createdAt: 900, text: proposal.text,
      author: { kind: 'person', id: 'peer', name: 'peer', membershipGeneration: 'first' } } }).messages
  const propose = () => proposed()[0]!
  /** The PM's post in the project chat, replying to [root] or to a reply in [root]'s thread, as the store loads that reply. */
  const pmPost = (text: string, extra: { replyTo?: MessageDraft; root?: MessageDraft; mentions?: string[] } = {}, actor: Actor = agentActor(pm)) => {
    const reply = extra.replyTo && { id: extra.replyTo.id, conversationId: extra.replyTo.conversationId, author: extra.replyTo.author, createdAt: 900,
      thread: (extra.root ?? extra.replyTo).id, threadSource: (extra.root ?? extra.replyTo).source ?? null }
    return run({ type: 'message.send', conversationId: app.conversationId, text, recipientIds: [], replyTo: extra.replyTo?.id ?? null,
      ...(extra.mentions ? { mentions: extra.mentions } : {}) }, actor, reply ? { reply } : {}).messages.at(-1)!
  }
  return { run, add, app, pm, chat: chat.id, proposed, propose, pmPost, get state() { return state } }
}

describe('which Buddy a project-chat message reaches (Diego, 10-29)', () => {
  it('is the Buddy whose person\'s proposal it replies to, in its thread too', () => {
    const f = setup(), sent = f.propose()
    const answer = f.pmPost('Taking it: #12 will build this.', { replyTo: sent })
    expect(answer.buddyRecipients).toEqual(['buddy'])
    expect(answer.recipients).not.toContain('buddy')
    expect(f.pmPost('Builder Orion has it.', { replyTo: answer, root: sent }).buddyRecipients).toEqual(['buddy'])
  })

  it('is the Buddy of a person the PM tags', () => {
    const f = setup()
    expect(f.pmPost('@peer does this cover your case?', { mentions: ['peer'] }).buddyRecipients).toEqual(['buddy'])
  })

  it('is the Buddy of the person who requested work that shipped', () => {
    const f = setup()
    const ship = (requesterId?: string): MessageDraft => ({ id: 'ship', conversationId: f.app.conversationId, author: { kind: 'agent', id: f.pm, name: 'pm',
      membershipGeneration: 'first' }, text: 'Shipped', replyTo: null, recipients: [],
      source: { kind: SHIP_KIND, conversationId: 'g', title: 'Offline', pullRequests: [], ...(requesterId ? { requesterId } : {}) } })
    expect(buddiesTold(f.state, ship('peer'), null)).toEqual(['buddy'])
    expect(buddiesTold(f.state, ship('owner'), null)).toEqual([])
    expect(buddiesTold(f.state, ship(), null)).toEqual([])
  })

  it('is nobody for anything else in the project chat: a plain post, a reply to a plain post, another person\'s tag', () => {
    const f = setup()
    const plain = f.pmPost('Batch AB is merging.')
    expect(plain.buddyRecipients).toBeUndefined()
    expect(f.pmPost('Done.', { replyTo: plain }).buddyRecipients).toBeUndefined()
    expect(f.pmPost('@owner please approve.', { mentions: ['owner'] }).buddyRecipients).toBeUndefined()
  })

  it('is nobody for a routing row, whatever it carries: a Buddy reads no routing (security review 2 of #2115)', () => {
    const f = setup()
    const row = (kind: string): MessageDraft => ({ id: kind, conversationId: f.app.conversationId, author: { kind: 'agent', id: f.pm, name: 'pm',
      membershipGeneration: 'first' }, text: 'Rule set.', replyTo: null, recipients: [], mentions: ['peer'], source: { kind } })
    for (const kind of ['route', 'route-override', 'rule']) expect(buddiesTold(f.state, row(kind), null)).toEqual([])
  })

  it('is nobody for a notice only its recipients read, by kind or marked so (review of #2177)', () => {
    const f = setup()
    const notice = (source: Record<string, unknown>): MessageDraft => ({ id: 'notice', conversationId: f.app.conversationId, author: { kind: 'agent', id: f.pm,
      name: 'pm', membershipGeneration: 'first' }, text: '@peer #12 stalled.', replyTo: null, recipients: [f.pm], mentions: ['peer'], source })
    expect(buddiesTold(f.state, notice({ kind: 'work-stalled' }), null)).toEqual([])
    expect(buddiesTold(f.state, notice(forRecipients({ kind: 'work-cancelled' })), null)).toEqual([])
  })

  it('is nobody for the person\'s own words: they know what they wrote', () => {
    const f = setup(), sent = f.propose()
    expect(f.pmPost('Thanks, go ahead.', { replyTo: sent }, peer).buddyRecipients).toBeUndefined()
  })

  it('is nobody once its person no longer reads the project, has left, or rejoined at a new membership', () => {
    const lost = setup(), sent = lost.propose()
    lost.state.projects!.find(p => p.id === lost.app.projectId)!.visibility = 'private'
    expect(lost.pmPost('Taking this as its own task.', { replyTo: sent }).buddyRecipients).toBeUndefined()
    const rejoined = setup(), asked = rejoined.propose()
    rejoined.state.people.find(p => p.id === 'peer')!.generation = 'second'
    expect(buddiesTold(rejoined.state, { ...asked, id: 'answer', replyTo: asked.id, thread: asked.id, author: { kind: 'agent', id: rejoined.pm, name: 'pm',
      membershipGeneration: 'first' } }, asked.source ?? null)).toEqual([])
  })

  it('is nobody outside the project chat: a tag in a workgroup stays there', () => {
    const f = setup()
    const group = f.run({ type: 'conversation.create', projectId: f.app.projectId, title: 'Offline', brief: 'Save edits.', checks: [] }).result.conversationId as string
    const tagged = f.run({ type: 'message.send', conversationId: group, text: '@peer which drafts?', recipientIds: [], replyTo: null, mentions: ['peer'] },
      agentActor(f.pm)).messages.at(-1)!
    expect(tagged.buddyRecipients).toBeUndefined()
  })
})

describe('the assignment notice a Buddy gets for work its person requested', () => {
  /** A group peer requested, briefed and ready, and a worker in it. */
  function requested(extra: Record<string, unknown> = {}) {
    const f = setup()
    const group = f.run({ type: 'conversation.create', projectId: f.app.projectId, title: 'Offline edits', brief: 'Save edits offline.',
      checks: [{ description: 'Tests pass', independent: false }], requesterId: 'peer', ...extra } as Command).result.conversationId as string
    const worker = f.add(group, 'worker')
    const assign = () => f.run({ type: 'work.assign', conversationId: group, agentId: worker }).messages
    const number = f.state.conversations.find(c => c.id === group)!.number
    return { f, group, assign, number }
  }
  const notices = (messages: MessageDraft[]) => messages.filter(m => m.recipients.includes('buddy'))

  it('is one line inside the task, for the Buddy alone: its number, title and status, nothing more', () => {
    const { group, assign, number } = requested()
    expect(notices(assign())).toEqual([expect.objectContaining({ conversationId: group, recipients: ['buddy'], source: { kind: 'agent-only' },
      text: `News for your owner: #${number} "Offline edits", recorded as their request, is assigned and working.` })])
  })

  it('is sent once per task: a replacement agent taking the work over is no new news (task #4217)', () => {
    const { f, group, assign } = requested()
    expect(notices(assign())).toHaveLength(1)
    const first = f.state.conversations.find(c => c.id === group)!.assignment!
    f.run({ type: 'agent.alert', agentId: first.agentId, alert: 'model', active: true }, { kind: 'agent', agentId: first.agentId, machineOwnerId: 'host' })
    expect(f.state.conversations.find(c => c.id === group)!.assignment!.status).toBe('lost')
    const replacement = f.add(group, 'replacement')
    expect(notices(f.run({ type: 'work.assign', conversationId: group, agentId: replacement }).messages)).toEqual([])
  })

  it('is not sent for a private task in a public project, though its person requested it (security review 2 of #2178)', () => {
    const { f, group, assign } = requested()
    assign()
    const task = f.state.conversations.find(c => c.id === group)!
    task.audience = { people: [{ id: 'peer', generation: 'first' }] }
    expect(assignedNotice(f.state, task, { kind: 'agent', id: f.pm, name: 'pm', membershipGeneration: 'first' }, 'op')).toBeNull()
  })

  // The Shipped line's rule, kept for the news (ship.test.ts before #2633): work Done without a pull request never shipped.
  it('says nothing has shipped for work Done without a pull request, or Done before its merge', () => {
    for (const result of ['Ready to merge: Yes.', 'Review passed: https://github.com/o/r/pull/7']) {
      const { f, group, assign } = requested()
      assign()
      const task = f.state.conversations.find(c => c.id === group)!
      const worker = task.assignment!.agentId
      const done = f.run({ type: 'work.status', conversationId: group, assignmentId: task.assignment!.id, status: 'done', result },
        agentActor(worker))
      expect(notices(done.messages)).toEqual([])
    }
  })

  it('is not sent when its person cannot read the task, when nobody requested it, or when another person did', () => {
    const hidden = requested()
    hidden.f.state.projects!.find(p => p.id === hidden.f.app.projectId)!.visibility = 'private'
    expect(notices(hidden.assign())).toEqual([])
    const nobody = requested()
    delete nobody.f.state.conversations.find(c => c.id === nobody.group)!.requesterId
    expect(notices(nobody.assign())).toEqual([])
    expect(notices(requested({ requesterId: 'owner' }).assign())).toEqual([])
  })
})

describe('work the PM opens from a Buddy\'s proposal without naming anyone (groups #294 and #327, 10-30)', () => {
  /** peer's Buddy proposes; the PM opens a group from it, the store loading the proposal row by its id. */
  function opened(extra: Record<string, unknown> = {}) {
    const f = setup(), sent = f.propose()
    const created = f.run({ type: 'conversation.create', projectId: f.app.projectId, title: 'Offline edits', brief: 'Save edits offline.',
      checks: [{ description: 'Tests pass', independent: false }], proposalMessageId: sent.id, ...extra } as Command, agentActor(f.pm),
      { proposal: { id: sent.id, conversationId: sent.conversationId, author: sent.author, source: sent.source ?? null } })
    const group = created.result.conversationId as string
    return { f, group, created, find: () => f.state.conversations.find(c => c.id === group)! }
  }

  it('is requested by the person the proposal was sent for, who gets the assignment news, the ship news and the Shipped points', () => {
    const { f, group, created, find } = opened()
    expect(find().requesterId).toBe('peer')
    expect(created.result.requesterUnknown).toBeUndefined()
    const worker = f.add(group, 'worker')
    expect(f.run({ type: 'work.assign', conversationId: group, agentId: worker }).messages.filter(m => m.recipients.includes('buddy')))
      .toEqual([expect.objectContaining({ conversationId: group, recipients: ['buddy'], source: { kind: 'agent-only' } })])
    const assignmentId = find().assignment!.id
    for (const check of find().checks) f.run({ type: 'work.check', conversationId: group, assignmentId, checkId: check.id, passed: true, evidence: 'ok' })
    // Done with its pull request reads ready and is no news yet; its recorded merge ships it (taskLine.ts; PM, review of #2633),
    // and their Buddy hears it inside the task, once.
    const done = f.run({ type: 'work.status', conversationId: group, assignmentId, status: 'done', result: 'Review passed: https://github.com/o/r/pull/7' },
      agentActor(worker))
    expect(done.messages.filter(m => m.thread === find().cards!.line!.messageId)).toEqual([])
    expect(done.messages.filter(m => m.recipients.includes('buddy'))).toEqual([])
    const merged = f.run({ type: 'conversation.merged', conversationId: group, pullRequestUrl: 'https://github.com/o/r/pull/7' }, agentActor(worker))
    expect(merged.messages).toEqual(expect.arrayContaining([expect.objectContaining({ thread: find().cards!.line!.messageId, text: 'Merged: PR #7. Ships in the next release.' })]))
    // A merge is not shipped: the news waits for the release that holds it (task #4217).
    expect(merged.messages.filter(m => m.recipients.includes('buddy'))).toEqual([])
    expect(f.run({ type: 'conversation.merged', conversationId: group, pullRequestUrl: 'https://github.com/o/r/pull/7' }, agentActor(worker))
      .messages.filter(m => m.recipients.includes('buddy'))).toEqual([])
    const board = leaderboard({ state: f.state, groups: f.state.conversations, machines: [], projectId: f.app.projectId, week: weekStart(1000), now: 1000 })
    expect(board.people.find(p => p.personId === 'peer')).toMatchObject({ points: 3, shipped: { count: 1 } })
  })

  it('tells the Buddy its work shipped only when a verified release holds it, once (task #4217)', () => {
    const { f, group, find } = opened()
    const worker = f.add(group, 'worker')
    f.run({ type: 'work.assign', conversationId: group, agentId: worker })
    const url = 'https://github.com/o/r/pull/7'
    expect(f.run({ type: 'conversation.merged', conversationId: group, pullRequestUrl: url }, agentActor(worker))
      .messages.filter(m => m.recipients.includes('buddy'))).toEqual([])
    const key = pendingKey({ groupId: group, url })
    const settle = (tag: string, carries: string[], at: number) => {
      const state = structuredClone(f.state)
      const out = settleReleases(state, f.app.projectId, { resolved: [{ groupId: group, url, components: ['backend'], sha: 'merge' }],
        releases: { backend: { tag, sha: tag, at, carries, compared: [key] } } }, at)
      Object.assign(f.state, state)
      return out.messages.filter(m => m.recipients.includes('buddy'))
    }
    // A release that does not hold the merge says nothing; the one that does tells the Buddy, inside the task, once.
    expect(settle('v1.0.1_backend', [], 5000)).toEqual([])
    expect(settle('v1.0.2_backend', [key], 6000)).toEqual([expect.objectContaining({ conversationId: group, recipients: ['buddy'],
      source: { kind: 'agent-only' }, text: `News for your owner: #${find().number} "Offline edits", recorded as their request, shipped.` })])
    expect(settle('v1.0.3_backend', [key], 7000)).toEqual([])
  })

  it('keeps the requester the PM names, even someone else', () => {
    const { find } = opened({ requesterId: 'owner' })
    expect(find().requesterId).toBe('owner')
  })

  it('is refused naming the requester when the proposal\'s person left the Swarm or no longer reads the project, and opens once the PM names one (task #3389)', () => {
    for (const lose of [(f: ReturnType<typeof setup>) => { f.state.people = f.state.people.filter(p => p.id !== 'peer') },
      (f: ReturnType<typeof setup>) => { f.state.projects!.find(p => p.id === f.app.projectId)!.visibility = 'private' }]) {
      const f = setup(), sent = f.propose()
      lose(f)
      const open = (extra: Record<string, unknown> = {}) => f.run({ type: 'conversation.create', projectId: f.app.projectId, title: 'Offline edits', brief: 'Save edits offline.',
        checks: [{ description: 'Tests pass', independent: false }], proposalMessageId: sent.id, ...extra } as Command, agentActor(f.pm),
        { proposal: { id: sent.id, conversationId: sent.conversationId, author: sent.author, source: sent.source ?? null } })
      expect(() => open()).toThrow(expect.objectContaining({ code: 'TASK_CONTRACT_MISSING', message: expect.stringContaining('requester') }))
      const opened = open({ requesterId: 'owner' }).result.conversationId
      const group = f.state.conversations.find(c => c.id === opened)!
      expect(group.requesterId).toBe('owner')
      expect(group.ledger?.proposal).toEqual({ buddyId: 'buddy', messageId: sent.id })
    }
  })

  // A proposal stored before task #2926 has its Buddy's first reply holding its details, the row its PM was delivered.
  describe('named by its details, the first reply and the row the PM is delivered (Scholes, 11-04)', () => {
    /** A proposal's root and the Buddy's details reply under it, as buddy.propose wrote them before task #2926. */
    const stored = (f: ReturnType<typeof setup>): [MessageDraft, MessageDraft] => {
      const root = f.propose()
      return [root, { id: 'details', conversationId: root.conversationId, author: { kind: 'agent', id: 'buddy', name: 'Buddy', membershipGeneration: 'first' },
        text: 'Goal: Sync edits made offline\nScope: Desktop drafts only\nPriority: high', replyTo: root.id, thread: root.id, recipients: [f.pm] }]
    }
    /** The row the store loads for the named reply, with the row it replies to. */
    const loaded = (reply: MessageDraft, root: MessageDraft) => ({ proposal: { id: reply.id, conversationId: reply.conversationId, author: reply.author,
      source: reply.source ?? null, root: { id: root.id, conversationId: root.conversationId, author: root.author, source: root.source ?? null } } })
    const open = (f: ReturnType<typeof setup>, reply: MessageDraft, root: MessageDraft) => f.run({ type: 'conversation.create', projectId: f.app.projectId,
      title: 'Offline edits', brief: 'Save edits offline.', checks: [{ description: 'Tests pass', independent: false }], proposalMessageId: reply.id } as Command,
    agentActor(f.pm), loaded(reply, root))

    it('links the proposal itself: its person asked, its row is the task\'s only line, its Buddy is credited', () => {
      const f = setup(), [root, details] = stored(f)
      const created = open(f, details, root)
      const group = f.state.conversations.find(c => c.id === created.result.conversationId)!
      expect(group.requesterId).toBe('peer')
      expect(group.ledger?.proposal).toEqual({ buddyId: 'buddy', messageId: root.id })
      expect(group.cards?.line).toMatchObject({ messageId: root.id, conversationId: f.app.conversationId })
      expect(created.messages.filter(m => m.conversationId === f.app.conversationId)).toEqual([])
    })

    it('refuses any other reply in its thread, and the Buddy\'s reply under anything but its own proposal: nothing opens', () => {
      const f = setup(), [root, details] = stored(f)
      const before = f.state.conversations.length
      const wrongs: Array<[MessageDraft, MessageDraft]> = [
        [{ ...details, author: { ...details.author, id: f.pm } }, root],
        [{ ...details, author: { kind: 'person', id: 'peer', name: 'peer', membershipGeneration: 'first' } }, root],
        [{ ...details, source: { kind: 'route' } }, root],
        [details, { ...root, source: { kind: 'route', personId: 'peer', buddyId: 'buddy' } }],
        [details, { ...root, source: { ...root.source, buddyId: 'other-buddy' } }],
        [details, { ...root, conversationId: f.chat }],
      ]
      for (const [reply, under] of wrongs) {
        expect(() => open(f, reply, under), JSON.stringify([reply, under])).toThrow('Name a Buddy\'s proposal posted in this project\'s chat, or leave the proposal out.')
      }
      expect(f.state.conversations.length).toBe(before)
    })
  })

  it('credits nobody from a proposal posted in another project\'s chat, a private chat or by anyone but a Buddy: refused alike, nothing opened', () => {
    const f = setup(), sent = f.propose()
    const web = f.run({ type: 'project.create', name: 'Web', visibility: 'public', brief: '' }).result as { projectId: string; conversationId: string }
    const before = f.state.conversations.length
    const wrongs = [{ ...sent, conversationId: web.conversationId }, { ...sent, conversationId: f.chat },
      { ...sent, author: { kind: 'person' as const, id: 'owner', name: 'owner', membershipGeneration: 'first' } },
      { ...sent, source: { ...sent.source, buddyId: f.pm } },
      { ...sent, author: { ...sent.author, id: f.pm } }, { ...sent, source: { kind: 'route', personId: 'peer' } }]
    for (const wrong of wrongs) {
      expect(() => f.run({ type: 'conversation.create', projectId: f.app.projectId, title: 'Offline', brief: 'Save.',
        checks: [{ description: 'Done', independent: false }], proposalMessageId: wrong.id } as Command, agentActor(f.pm),
        { proposal: { id: wrong.id, conversationId: wrong.conversationId, author: wrong.author, source: wrong.source ?? null } }), JSON.stringify(wrong))
        .toThrow('Name a Buddy\'s proposal posted in this project\'s chat, or leave the proposal out.')
    }
    expect(f.state.conversations.length).toBe(before)
  })
})