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.

8.0 KB
Raw Blame
import { describe, expect, it } from 'vitest'
import { agentDisplayName, assignAgentDisplayNames, AGENT_NAMES, freeAgentName, type NamedAgent } from './agentNames.js'
import { applyCommand } from './commands.js'
import { author, createSuperTab, visibleState, type Actor, type Agent } from './model.js'

const owner: Actor = { kind: 'person', userId: 'owner', email: '[email protected]' }
const peer: Actor = { kind: 'person', userId: 'peer', email: '[email protected]' }
const configuration = { engine: 'codex', model: null, effort: null, contextWindow: null, engineVersion: null }
function legacyState() {
  const state = createSuperTab('team', 'Team', { id: 'owner', email: '[email protected]' }, 'test', 'public')
  state.people.push({ id: 'peer', email: '[email protected]', role: 'collaborator', generation: 'first' })
  state.projects!.push({ ...state.projects![0]!, id: 'private', name: 'Private', visibility: 'private', leadConversationId: 'private-lead' })
  state.conversations.push({ ...state.conversations[0]!, id: 'private-lead', projectId: 'private', agentIds: [] })
  state.agents = Array.from({ length: 40 }, (_, i): Agent => ({
    id: `agent-${i}`, name: i % 2 ? 'Builder' : 'Lead', projectId: i % 2 ? 'private' : 'lead',
    machineId: 'machine', harnessId: `native-${i}`, machineOwnerId: 'host', contributorId: 'owner',
    membershipGeneration: 'team', configuration,
  }))
  return state
}

describe('individual agent names', () => {
  it('gives default roles distinct names while preserving chosen and persisted identities', () => {
    const agents: NamedAgent[] = [
      { id: 'custom', name: 'Multiply' },
      { id: 'existing', name: 'Lead', displayName: 'Atlas' },
      ...Array.from({ length: 80 }, (_, i) => ({ id: `a-${i}`, name: i % 2 ? 'Builder' : 'Lead' })),
      { id: 'clock', name: 'claude-2026-10-21-12-34' },
    ]
    const reversed = structuredClone(agents).reverse()
    assignAgentDisplayNames(agents)
    assignAgentDisplayNames(reversed)
    expect(new Set(agents.map(a => a.displayName)).size).toBe(agents.length)
    expect(agents[0]!.displayName).toBe('Multiply')
    expect(agents[1]!.displayName).toBe('Atlas')
    expect(agents.at(-1)!.displayName).not.toBe(agents.at(-1)!.name)
    expect(Object.fromEntries(agents.map(a => [a.id, a.displayName])))
      .toEqual(Object.fromEntries(reversed.map(a => [a.id, a.displayName])))
    const before = structuredClone(agents)
    agents.splice(2, 1)
    agents.push({ id: 'new', name: 'Lead' })
    assignAgentDisplayNames(agents)
    for (const a of agents.filter(a => a.id !== 'new')) {
      expect(a.displayName).toBe(before.find(b => b.id === a.id)!.displayName)
    }
    expect(agents.map(a => a.name)).toContain('Lead')
  })

  it('gives Reviewers member names like Builders, also one that kept "Reviewer" as its name (Dee, 2026-10-30)', () => {
    // Three Reviewers once read "Reviewer" three times: the role was not a role's name here, so it stuck.
    const agents: NamedAgent[] = [
      { id: 'r-1', name: 'Reviewer' },
      { id: 'r-2', name: 'Reviewer', displayName: 'Reviewer' },
      { id: 'r-3', name: 'reviewer 2' },
      { id: 'kept', name: 'Reviewer', displayName: 'Quinn' },
    ]
    assignAgentDisplayNames(agents)
    const names = agents.map(a => a.displayName!)
    for (const name of names.slice(0, 3)) expect(name).not.toMatch(/reviewer/i)
    expect(new Set(names).size).toBe(names.length)
    expect(agents[3]!.displayName).toBe('Quinn')
    expect(agentDisplayName([{ id: 'r', name: 'Reviewer', displayName: 'Reviewer' }], 'r')).not.toBe('Reviewer')
  })

  it('keeps every other stored name, even one that reads like a role or an automatic session (review of #2281)', () => {
    // A Buddy may be named "claude-2026-10-30" (buddies.ts accepts it); only a stored "Reviewer" is replaced.
    const kept = ['Dev', 'PM', 'Lead 2', 'claude-2026-10-30', 'codex-2026-10-30T09']
    const agents: NamedAgent[] = kept.map((displayName, i) => ({ id: `k-${i}`, name: 'Builder', displayName }))
    assignAgentDisplayNames(agents)
    expect(agents.map(a => a.displayName)).toEqual(kept)
    for (const [i, name] of kept.entries()) expect(agentDisplayName(agents, `k-${i}`)).toBe(name)
  })

  it('resolves legacy identities before filtering private projects without mutating reads', () => {
    const state = legacyState(), original = structuredClone(state)
    const all = visibleState(state, owner), publicOnly = visibleState(state, peer)
    expect(publicOnly.agents).toHaveLength(20)
    for (const a of publicOnly.agents) {
      expect(a.displayName).toBe(all.agents.find(b => b.id === a.id)!.displayName)
      expect(agentDisplayName(state.agents, a.id)).toBe(a.displayName)
    }
    expect(state).toEqual(original)
    expect(agentDisplayName(state.agents, 'removed')).toBeUndefined()
  })

  it('persists the displayed names on an authorized command and keeps routing identities unchanged', () => {
    const state = legacyState(), before = visibleState(state, owner)
    const command = { type: 'tab.rename' as const, name: 'Team renamed' }
    const context = { operationId: 'rename', now: 1000 }
    const result = applyCommand(state, owner, command, context)
    expect(result.state.agents).toEqual(before.agents)
    expect(applyCommand(state, owner, command, context)).toEqual(result)
    expect(state.agents.every(a => a.displayName === undefined)).toBe(true)
    const sender = author(result.state, { kind: 'agent', agentId: 'agent-0', machineOwnerId: 'host' })
    expect(sender).toMatchObject({ id: 'agent-0', name: 'Lead', displayName: before.agents[0]!.displayName })
  })
})

// Task #4568 (anh Dee, 11-07 19:40): every agent has a real name, and the list never runs out.
describe('a free agent name', () => {
  const none = () => false
  it('comes from a list of 200 distinct names', () => {
    expect(AGENT_NAMES).toHaveLength(200)
    expect(new Set(AGENT_NAMES.map(n => n.toLowerCase())).size).toBe(200)
    expect(AGENT_NAMES).toContain(freeAgentName(none, 'seed'))
  })

  it('is the same for the same seed, and skips a taken one', () => {
    const first = freeAgentName(none, 'agent-1')
    expect(freeAgentName(none, 'agent-1')).toBe(first)
    const next = freeAgentName(name => name.toLowerCase() === first.toLowerCase(), 'agent-1')
    expect(next).not.toBe(first)
    expect(AGENT_NAMES).toContain(next)
  })

  it('reads as two names once every list name is taken, and is numbered only past every pair', () => {
    const single = new Set(AGENT_NAMES.map(n => n.toLowerCase()))
    const pair = freeAgentName(name => single.has(name.toLowerCase()), 'seed')
    const [given, family, more] = pair.split(' ')
    expect(AGENT_NAMES).toContain(given)
    expect(AGENT_NAMES).toContain(family)
    expect(given).not.toBe(family)
    expect(more).toBeUndefined()
    expect(freeAgentName(name => !/\d/.test(name), 'seed')).toMatch(/^[A-Z][a-z]+ \d+$/)
  })

  it('gives an agent named for its engine a member name', () => {
    // An agent whose engine name was already kept as its shown name (Desktop's local start, before this) gets one too.
    const agents: NamedAgent[] = [{ id: 'a', name: 'Claude Code' }, { id: 'b', name: 'Codex' }, { id: 'c', name: 'claude code' },
      { id: 'd', name: 'Claude Code', displayName: 'Claude Code' }]
    assignAgentDisplayNames(agents)
    for (const agent of agents) expect(AGENT_NAMES).toContain(agent.displayName)
    expect(new Set(agents.map(a => a.displayName)).size).toBe(4)
    expect(agentDisplayName([{ id: 'd', name: 'Codex', displayName: 'Codex' }], 'd')).not.toBe('Codex')
  })

  // Mac test of #3560: a Buddy started before real names still goes by "<person>'s Buddy" until it restarts.
  it('gives an agent still named "<person>\'s Buddy" a member name at once', () => {
    const agents: NamedAgent[] = [{ id: 'b', name: "owner's Buddy", displayName: "owner's Buddy" }, { id: 'c', name: "ken.tran's Buddy" }]
    assignAgentDisplayNames(agents)
    for (const agent of agents) expect(AGENT_NAMES).toContain(agent.displayName)
    expect(agentDisplayName([{ id: 'b', name: "dee's Buddy", displayName: "dee's Buddy" }], 'b')).not.toBe("dee's Buddy")
  })
})