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.

218 KB
Raw Blame
import { describe, expect, it, vi, type Mock } from 'vitest'
import type { openDaemon } from '../agentClient/daemon.js'
import { mkdtemp, rm, writeFile } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { PM_ACCOUNT_NOTE } from './pmAccounts.js'
import { ELSEWHERE, heldByStarts, heldLocalModels, leftOut, localProbes, NO_SWARM_AGENTS_ON, noteClause, offlineModels, routeAnswer, routeTask, routingSection, servedModels, STALL_RETRY_MS, faulted, stalled } from './routeCommand.js'
import type { describeScmProject } from '../scm/scmProjects.js'
import { admission, quotaOf, USED_UP_WINDOW } from './capacity.js'
import { NONE_COVERS, OTHER_REPOSITORY, type Catalog, type Probe, type RoutePlugin } from './route.js'
import { MAKER_TIMEOUT_MS, type PluginInput, type PluginMaker } from './routePlugins.js'
import { placedHeavyWork } from './machineFit.js'
import { inspectCapacity } from './start.js'
import { EARLIER_UNREADABLE, FIRST_ATTEMPT_ONLY, GRID_FIRST, GRID_LIMITS, NEVER_REVIEWS } from './routeCheapFirst.js'

// Sonnet rated 3, as an owner who wants Sonnet Builders rates it (a build needs 3: route.ts buildRefusals), so the member these
// specs probe can build; the specs of the floor itself use a table of their own ([floorPolicy]).
const policy = { version: 1, floor: { low: 1, medium: 2, high: 3 }, quotaReserve: 10, notes: '', models: [
  { engine: 'claude', model: 'opus', effort: 'high', quality: 3, cost: 3, enabled: true },
  { engine: 'claude', model: 'sonnet', effort: 'medium', quality: 3, cost: 2, enabled: true },
  { engine: 'codex', model: 'default', effort: 'default', quality: 3, cost: 2, enabled: false },
  { engine: 'codex', model: 'gpt-6.1-sol', effort: 'high', quality: 3, cost: 2, enabled: false },
] }
/** The table with Sonnet at its real rating, 2, for the specs of strength and floors. */
const floorPolicy = { ...policy, models: policy.models.map(m => m.model === 'sonnet' ? { ...m, quality: 2 } : m) }
const agents = [
  { id: 'a1', name: 'Multiply', machineId: 'm1', harnessId: 'h1', configuration: { engine: 'claude', model: 'sonnet', effort: 'medium' } },
  { id: 'lead', name: 'Lead', machineId: 'm1', harnessId: 'hl', configuration: { engine: 'claude', model: 'opus', effort: 'high' } },
  // An engine the route does not offer: it only puts m2 in the Swarm's Machines list, as a person adding the machine does.
  { id: 'a2', name: 'Elsewhere', machineId: 'm2', harnessId: 'h2', configuration: { engine: 'pi', model: 'default', effort: 'default' } },
]
const conversations = (assignment: unknown = null) => [
  { id: 'lead', title: 'Lead', leadId: 'lead', agentIds: ['lead', 'a1'], archived: false, brief: '', checks: [], assignment: null },
  { id: 'g1', title: 'API', leadId: 'lead', agentIds: ['lead'], archived: false, brief: 'Add the endpoint.', checks: [{ description: 'Tests pass' }], assignment },
]
const tab = (assignment: unknown = null) => ({ revision: 3, state: { id: 'tab', agents, conversations: conversations(assignment) } })
const usage = { providers: [{ provider: 'claude', outcome: 'answered', httpStatus: 200, account: 'acct', body: { five_hour: { utilization: 40, resets_at: new Date(Date.now() + 3 * 3_600_000).toISOString() }, seven_day: { utilization: 10 } } }] }
function daemon(machineId: string, options: { agents?: string[]; frames?: Record<string, Record<string, unknown>>; capacity?: Record<string, unknown>; terminalAvailable?: boolean; tools?: Record<string, boolean>; devices?: Record<string, boolean>; usage?: unknown; listFails?: boolean; engines?: unknown } = {}) {
  return {
    machineId, close: vi.fn(),
    request: vi.fn(async (type: string, _payload?: unknown, _timeout?: number) => {
      if (type === 'agents_list') {
        if (options.listFails) throw new Error('timeout')
        return { agents: (options.agents ?? []).map(id => ({ id, terminal: { available: options.terminalAvailable ?? true }, ...options.frames?.[id] })) }
      }
      if (type === 'super_tabs') return options.capacity ?? { resources: { memoryTotalBytes: 8e9, memoryUsedBytes: 1e9, cpuPercent: 10, projectDiskFreeBytes: 1e11 }, tools: options.tools ?? { git: true }, devices: options.devices ?? {} }
      if (type === 'engines_probe') return { engines: options.engines ?? [{ engine: 'claude', installed: true, signedIn: true }] }
      if (type === 'usage_read') return options.usage ?? usage
      return {}
    }),
  }
}
type Answer = Record<string, unknown>
/** Runs [body] with [os] on the list of systems Swarm agents are not started on, as a future entry would put it. */
async function withBlocked(os: string, body: () => Promise<void>) {
  const blocked = NO_SWARM_AGENTS_ON as string[]
  blocked.push(os)
  try { await body() } finally { blocked.splice(blocked.indexOf(os), 1) }
}
function scoped(answers: { record?: Answer; tab?: unknown; context?: Answer; history?: Answer; instructions?: Answer; historyHangs?: boolean; recordThrows?: boolean; policy?: typeof policy; machineRules?: Answer } = {}) {
  return vi.fn(async (payload: Record<string, any>): Promise<Record<string, unknown>> => {
    if (payload.action === 'machineRules') return answers.machineRules ?? { ok: true, value: { projectId: payload.projectId, revision: 0, machines: [] } }
    if (payload.action === 'routeContext') return answers.context ?? { ok: true, value: { agentId: 'lead', tab: answers.tab ?? tab(), policy: { policy: answers.policy ?? policy, warnings: ['w'] }, machines: [{ machineId: 'm1', name: 'remote-1' }, { machineId: 'm2', name: 'mac' }] } }
    if (payload.action === 'routeHistory') return answers.historyHangs ? new Promise(() => {}) : answers.history ?? { ok: true, value: { decisions: [], attempts: [] } }
    if (payload.action === 'instructions') return answers.instructions ?? { ok: true, value: { projectId: 'p1', revision: 2, hash: 'h', text: 'Work by this.', template: false, coordinatorName: 'Ana', notes: [{ id: 'n1', text: 'Be quick', askedBy: 'Ken', at: 't', expiresAt: null }] } }
    if (payload.action === 'command') {
      if (answers.recordThrows) throw new Error('socket closed')
      return answers.record ?? { ok: true, value: { result: { routeDecisionId: 'd1' } } }
    }
    return { error: 'UNEXPECTED' }
  })
}
const where = { port: 1, dataDir: '/nonexistent', machineId: 'm1' }
const input = { tabId: 'tab', conversationId: 'g1', assessment: { difficulty: 'medium', risk: 'low', why: 'Endpoint.' } }
const opener = (make: (id: string) => ReturnType<typeof daemon> = id => daemon(id, { agents: id === 'm1' ? ['h1'] : [] })) => vi.fn(async (_w: unknown, id = '') => make(id)) as unknown as typeof openDaemon & Mock
const recorded = (s: ReturnType<typeof scoped>) => s.mock.calls.find(c => c[0].action === 'command')![0]
/** Each candidate's line, as the route log keeps it. */
/** The answer's first new agent row: what a start there reads of its machine. */
const firstNew = (answer: Answer) => (answer.candidates as Array<Record<string, unknown>>).find(r => r.kind === 'new')!
const labels = (s: ReturnType<typeof scoped>) => (recorded(s).command.decision.candidates as Array<{ label: string }>).map(c => c.label)

describe('routeTask', () => {
  it('never offers OpenCode or Kilo even if an old policy enables them', async () => {
    const oldPolicy = { ...policy, models: [...policy.models.map(model => ({ ...model, enabled: false })),
      { engine: 'opencode', model: 'default', effort: 'default', quality: 3, cost: 1, enabled: true },
      { engine: 'kilo', model: 'default', effort: 'default', quality: 3, cost: 1, enabled: true }] }
    const oldTab = tab(); oldTab.state.agents = [agents[1]]
    const s = scoped({ context: { ok: true, value: { agentId: 'lead', tab: oldTab, policy: { policy: oldPolicy },
      machines: [{ machineId: 'm1', name: 'remote-1' }] } } })
    const result = await routeTask(input, s, where as never, opener(id => daemon(id, { agents: id === 'm1' ? ['h1'] : [],
      engines: [{ engine: 'claude', installed: true }, { engine: 'opencode', installed: true }, { engine: 'kilo', installed: true }] })))
    expect(result).toMatchObject({ error: 'NO_CANDIDATE' })
  })
  it('offers the same member after Change agent when only its replacement terminal is live', async () => {
    const rebound = tab()
    rebound.state.agents = agents.map(a => a.id === 'a1' ? { ...a, harnessId: 'replacement' } : a)
    const s = scoped({ tab: rebound })
    const result = await routeTask(input, s, where as never, opener(id => daemon(id, { agents: ['replacement'] })), () => 'op')
    expect(result).toMatchObject({ ok: true, candidates: expect.arrayContaining([expect.objectContaining({ kind: 'member', agentId: 'a1' })]) })
    expect(recorded(s).command.decision.excluded.some((c: { title: string }) => c.title.startsWith('Multiply'))).toBe(false)
  })
  it('shows the group\'s project instructions and notes in its answer, and still answers when they cannot be read', async () => {
    const s = scoped()
    const result = await routeTask(input, s, where as never, opener(), () => 'op')
    expect(result).toMatchObject({ ok: true, instructions: { revision: 2, text: 'Work by this.', notes: [{ text: 'Be quick' }] } })
    expect(result).not.toHaveProperty('instructionsUnreadable')
    expect(s.mock.calls.find(c => c[0].action === 'instructions')![0]).toMatchObject({ projectId: 'lead' })
    const old = scoped({ instructions: { error: 'INSTRUCTIONS_UNAVAILABLE', detail: 'x' } })
    const unread = await routeTask(input, old, where as never, opener(), () => 'op')
    expect(unread).toMatchObject({ ok: true, instructionsUnreadable: true })
    expect(unread).not.toHaveProperty('instructions')
  })
  it('collects members and new agents, records and returns them all, and never asks Jev', async () => {
    const s = scoped()
    const result = await routeTask(input, s, where as never, opener(), () => 'op')
    expect(result).toMatchObject({ ok: true, routeDecisionId: 'd1', via: 'order', p: null, candidates: expect.arrayContaining([expect.objectContaining({ id: 'c0' })]), warnings: ['w'] })
    expect(s.mock.calls.map(c => c[0].action)).toEqual(['routeContext', 'routeHistory', 'instructions', 'machineRules', 'command'])
    expect(recorded(s).command.decision).not.toHaveProperty('context')
    const record = recorded(s)
    expect(record).toMatchObject({ operationId: 'route-op', command: { type: 'route.record', conversationId: 'g1' } })
    // The idle member Multiply is a candidate. (The idle coordinator is too: 'routing by facts', below.)
    const titles = (record.command.decision.candidates as Array<{ title: string }>).map(c => c.title)
    expect(titles).toContain('Multiply · Claude sonnet medium · remote-1')
    // Every recorded row is in the answer, members on the same account too: the coordinator may reuse a particular one.
    expect((result.candidates as Array<{ id: string }>).map(r => r.id)).toEqual((record.command.decision.candidates as Array<{ id: string }>).map(c => c.id))
    expect((result.candidates as Array<{ title: string }>).map(r => r.title)).toContain('Multiply · Claude sonnet medium · remote-1')
  })
  it('runs the plug-ins its makers make: their reasons in the answer and the record, a failed one named in the warnings', async () => {
    const s = scoped()
    const plugins: RoutePlugin[] = [{ name: 'Machine fit', adjust: c => c.kind === 'new' ? { strength: 5, reason: 'a fresh worktree here' } : null },
      { name: 'Broken', adjust: () => { throw new Error('secret') } }]
    const inputs: PluginInput[] = []
    const result = await routeTask(input, s, where as never, opener(), () => 'op', undefined, plugins.map(p => ({ name: p.name, make: async (i: PluginInput) => { inputs.push(i); return p } })))
    expect(result).toMatchObject({ ok: true, candidates: expect.arrayContaining([expect.objectContaining({ kind: 'new', notes: expect.arrayContaining(['Machine fit: a fresh worktree here']) })]),
      warnings: ['w', 'The routing plug-in "Broken" was skipped: it failed. The route is shown without it.'] })
    expect(labels(s)[0]).toContain('Machine fit: a fresh worktree here')
    expect(JSON.stringify(result)).not.toContain('secret')
    // Each maker gets the task, the route's backend read, the group's history (read once, shared) and the agent's request.
    expect(inputs[0]).toMatchObject({ conversationId: 'g1', assessment: { why: 'Endpoint.' }, read: { agentId: 'lead' }, catalog: { models: expect.any(Array) } })
    expect(await inputs[0]!.history).toEqual({ ok: true, value: { decisions: [], attempts: [] } })
    expect(s.mock.calls.filter(c => c[0].action === 'routeHistory')).toHaveLength(1)
    expect(await inputs[0]!.request({ action: 'instructions' })).toMatchObject({ ok: true })
    // Nothing can run: the answer still names the failed plug-in and the failed maker.
    const none = await routeTask(input, scoped(), where as never, opener(), () => 'op', undefined, [
      { name: 'Nobody', make: async () => ({ name: 'Nobody', adjust: () => ({ refuse: true, reason: 'no room' }) }) },
      { name: 'Broken', make: async () => ({ name: 'Broken', candidates: () => { throw new Error('secret') } }) },
      { name: 'Maker fails', make: async () => { throw new Error('secret') } }])
    expect(none).toMatchObject({ error: 'NO_CANDIDATE', warnings: ['The routing plug-in "Maker fails" was skipped: its maker failed. The route is shown without it.',
      'The routing plug-in "Broken" was skipped: it failed. The route is shown without it.'],
      leftOut: expect.arrayContaining([{ machine: 'remote-1', reasons: expect.arrayContaining(['Nobody: no room']) }]) })
  })
  it('reads instructions without notes as none, and cuts a long reason a machine is left out for', async () => {
    const s = scoped({ instructions: { ok: true, value: { projectId: 'p1', revision: 1, text: 'Work by this.' } } })
    expect(await routeTask(input, s, where as never, opener(), () => 'op')).toMatchObject({ instructions: { revision: 1, notes: [] } })
    expect(leftOut([{ title: 't', reason: 'x'.repeat(301), machine: 'm' }], [])).toEqual([{ machine: 'm', reasons: [`${'x'.repeat(299)}…`] }])
  })
  it('pulls the task onto the account whose weekly window resets soon with quota left, through its listed maker (piece 6)', async () => {
    const reading = (account: string, weeklyUsed: number, weeklyResetsIn: number) => ({ providers: [{ provider: 'claude', outcome: 'answered', httpStatus: 200, account,
      body: { five_hour: { utilization: 20, resets_at: new Date(Date.now() + 3 * 3_600_000).toISOString() },
        seven_day: { utilization: weeklyUsed, resets_at: new Date(Date.now() + weeklyResetsIn).toISOString() } } }] })
    // m1 (remote-1) has a fresh week; m2 (mac) resets in 12 hours with 60% left.
    const machines = opener(id => daemon(id, { agents: id === 'm1' ? ['h1'] : [], usage: id === 'm2' ? reading('acct-soon', 40, 12 * 3_600_000) : reading('acct-fresh', 10, 5 * 86_400_000) }))
    const plain = await routeTask(input, scoped(), where as never, machines, () => 'op', undefined, [])
    expect(plain).toMatchObject({ ok: true, candidates: expect.arrayContaining([expect.objectContaining({ title: expect.stringContaining('remote-1') })]) })
    const s = scoped()
    const pulled = await routeTask(input, s, where as never, machines, () => 'op')
    expect(pulled).toMatchObject({ ok: true, candidates: expect.arrayContaining([expect.objectContaining({ title: expect.stringContaining('mac') })]) })
    expect(labels(s)[0]).toContain('Window pull: its account\'s weekly window resets in 12h')
    expect(JSON.stringify(pulled)).not.toContain('acct-soon')
  })
  it('answers exactly as before with no makers, and with makers that have nothing to say', async () => {
    vi.useFakeTimers({ toFake: ['Date'] })
    try {
      vi.setSystemTime(Date.parse('2026-10-31T04:00:00Z'))
      const answer = async (makers?: PluginMaker[]) => { const s = scoped(); return { answer: await routeTask(input, s, where as never, opener(), () => 'op', undefined, makers), record: recorded(s) } }
      const before = JSON.stringify(await answer())
      expect(JSON.stringify(await answer([]))).toBe(before)
      expect(JSON.stringify(await answer([{ name: 'Idle', make: async () => null }]))).toBe(before)
    } finally { vi.useRealTimers() }
  })
  it('a slow or failing maker is left out with a warning, and the route answers as it would without it', async () => {
    vi.useFakeTimers({ toFake: ['Date'] })
    vi.setSystemTime(Date.parse('2026-10-31T04:00:00Z'))
    let plain: Answer
    try { plain = await routeTask(input, scoped(), where as never, opener(), () => 'op') } finally { vi.useRealTimers() }
    vi.useFakeTimers({ toFake: ['Date', 'setTimeout', 'clearTimeout'] })
    try {
      vi.setSystemTime(Date.parse('2026-10-31T04:00:00Z'))
      const pending = routeTask(input, scoped(), where as never, opener(), () => 'op', undefined, [
        { name: 'Slow', make: () => new Promise(() => {}) }, { name: 'Fails', make: async () => { throw new Error('secret') } }])
      await vi.advanceTimersByTimeAsync(MAKER_TIMEOUT_MS + 1)
      vi.setSystemTime(Date.parse('2026-10-31T04:00:00Z'))
      const { warnings, ...rest } = await pending as Answer
      const { warnings: plainWarnings, ...plainRest } = plain
      expect(rest).toEqual(plainRest)
      expect(warnings).toEqual([...(plainWarnings as string[]), 'The routing plug-in "Slow" was skipped: its maker took longer than 3 s. The route is shown without it.',
        'The routing plug-in "Fails" was skipped: its maker failed. The route is shown without it.'])
    } finally { vi.useRealTimers() }
  })
  // A member at work is not offered; one whose task was lost is, with a note ('a member whose last task was lost').
  it('judges heavy work by a machine reached directly, its members too, by the disk its capacity read measured', async () => {
    const heavy = (needs: Record<string, unknown>) => ({ ...input, assessment: { ...input.assessment, needs: { heavy: ['build'], ...needs } } })
    const reading = (resources: Record<string, unknown>) => opener(id => daemon(id, { agents: id === 'm1' ? ['h1'] : [], capacity: { resources, tools: { git: true }, devices: {} } }))
    const disky = { memoryTotalBytes: 16e9, memoryUsedBytes: 2e9, cpuPercent: 10, projectDiskFreeBytes: 20e9 }
    const short = 'Machine fit: Short of free disk for a heavy build: it needs 8 GB memory and 30 GB disk free, so prefer an equally strong candidate that has it.'
    const answer = await routeTask(heavy({}), scoped(), where as never, reading(disky))
    const shown = answer.candidates as Array<{ kind: string; notes?: string[] }>
    expect(new Set(shown.map(c => c.kind))).toEqual(new Set(['member', 'new']))
    for (const c of shown) expect(c.notes).toContain(short)
    const folder = { describe: async () => null, isDirectory: async () => true }
    expect(await routeTask(heavy({ cwd: '/repo' }), scoped(), where as never, reading(disky), () => 'op', folder)).toMatchObject({ ok: true, candidates: expect.arrayContaining([expect.objectContaining({ notes: expect.arrayContaining([short]) })]) })
    // A read that measured no disk says so; its new agents' starts could not verify the disk either.
    const blind = await routeTask(heavy({}), scoped(), where as never, reading({ memoryTotalBytes: 16e9, memoryUsedBytes: 2e9, cpuPercent: 10 }))
    expect(blind).toMatchObject({ candidates: expect.arrayContaining([expect.objectContaining({ kind: 'member', notes: expect.arrayContaining(['Machine fit: Free disk unknown here; a heavy build: it needs 8 GB memory and 30 GB disk free.']) })]) })
  })
  it('answers work that starts heavy CI while the account is at its limit as waiting, with when to route it again', async () => {
    const base = scoped()
    const s = vi.fn(async (payload: Record<string, any>) => payload.action === 'ciLoad'
      ? { ok: true, value: { state: 'read', load: { running: 15, queued: 4, macosRunning: 1, macosQueued: 0 } } } : base(payload)) as typeof base
    const held = await routeTask({ ...input, assessment: { ...input.assessment, needs: { ci: { jobs: 2, macos: 0 } } } }, s, where as never, opener())
    expect(held).toMatchObject({ error: 'NO_CANDIDATE', waiting: true, retryAfterMinutes: 10,
      detail: 'This repository\'s CI jobs are at the account\'s limit: 19 running or queued against the account\'s limit of 20, 1 on macOS against 5, and this task starts 2 more. Route it again in 10 minutes.' })
    expect(s.mock.calls.some(c => c[0].action === 'command')).toBe(false)
    // Any other task routes as before, without asking for the CI.
    const plain = await routeTask(input, s, where as never, opener())
    expect(plain).toMatchObject({ ok: true })
    expect(plain).not.toHaveProperty('waiting')
  })
  // Lead, 10-31: a routed heavy start is never refused on the machine the route offered.
  it('notes a machine between half the need and the need, and the start there is admitted and counts as heavy', async () => {
    const between = { memoryTotalBytes: 16e9, memoryUsedBytes: 10e9, cpuPercent: 10, projectDiskFreeBytes: 100e9 }
    const roomy = { memoryTotalBytes: 32e9, memoryUsedBytes: 2e9, cpuPercent: 10, projectDiskFreeBytes: 100e9 }
    const machines = (id: string) => daemon(id, { capacity: { resources: id === 'm1' ? between : roomy, tools: { git: true }, devices: {} } })
    const heavy = { ...input, assessment: { ...input.assessment, needs: { heavy: ['build'] } } }
    const noMembers = tab(); noMembers.state.agents = agents.filter(a => a.id === 'a2') as never
    const answer = await routeTask(heavy, scoped({ tab: noMembers }), where as never, opener(machines))
    expect(answer).toMatchObject({ ok: true, candidates: expect.arrayContaining([expect.objectContaining({ machineId: 'm2' })]) })
    const later = (answer.candidates as Array<{ machineId: string; engine: string; notes?: string[] }>).find(c => c.machineId === 'm1')!
    expect(later.notes).toContain('Machine fit: Short of free memory for a heavy build: it needs 8 GB memory and 30 GB disk free, so prefer an equally strong candidate that has it.')
    // The start the route suggests, on that machine: its own check admits it, and later routes count it as heavy work there.
    const hint = JSON.parse(/start OPERATION_ID (\{.*\}), then/.exec(String(answer.next))![1]!)
    const request = { ...hint, machineId: 'm1', engine: later.engine, model: 'opus', effort: 'high' }
    expect(await inspectCapacity(machines('m1') as never, request)).toMatchObject({ allowed: true })
    expect(request.heavy).toEqual(['build'])
    expect(placedHeavyWork([{ request, requestedAt: Date.now() }], Date.now()).get('m1')).toMatchObject({ agents: 1, memoryBytes: 8e9 })
  })
  // Ken, 11-05: a PM put work on the account another project's PM ran on; the row now says whose PM it keeps running.
  it('notes on each candidate whose account a project\'s PM runs on which project and PM that is', async () => {
    const project = { id: 'p1', name: 'openharness', leadConversationId: 'lead' }
    const s = scoped({ context: { ok: true, value: { agentId: 'lead', tab: { state: { id: 'tab', agents, conversations: conversations(), projects: [project] } },
      policy: { policy, warnings: [] }, machines: [] } } })
    const answer = await routeTask(input, s, where as never, opener(), () => 'op') as { candidates: Array<{ machineId: string; engine?: string; title: string; notes?: string[] }> }
    const onPm = answer.candidates.filter(c => c.machineId === 'm1' && c.title.includes('Claude'))
    expect(onPm.length).toBeGreaterThan(0)
    for (const row of onPm) expect(row.notes).toContain(PM_ACCOUNT_NOTE(['openharness (Lead)']))
    expect(answer.candidates.filter(c => !(c.machineId === 'm1' && c.title.includes('Claude'))).every(c => !(c.notes ?? []).some(n => n.startsWith('This account runs the PM')))).toBe(true)
  })

  it('does not offer a member whose task is working', async () => {
    const state = tab({ id: 'x', agentId: 'a1', status: 'working' })
    const s = scoped({ tab: state })
    await routeTask(input, s, where as never, opener(id => daemon(id, { agents: ['h1'] })))
    expect((recorded(s).command.decision.candidates as Array<{ title: string }>).some(c => c.title.startsWith('Multiply'))).toBe(false)
  })
  // v2: one Codex account answered 429 for 40 minutes and stalled reviews. Since the project instructions (phase 4) the
  // account that lost the task is a note on its candidates, not a filter: the agent choosing decides.
  describe('a review whose agent lost it', () => {
    const accountOn = (account: string) => ({ providers: [{ ...usage.providers[0]!, account }] })
    /** `removed`: the Lead stopped the failed Reviewer and its host removed it, so only the group remembers where it failed. */
    const route = async (failed: boolean, machines: string[] = ['m1', 'm2'], removed = false) => {
      const state = tab()
      const reviewer = { id: 'r1', name: 'Reviewer', role: 'reviewer', machineId: 'm1', harnessId: 'hr', configuration: { engine: 'claude', model: 'sonnet', effort: 'medium' },
        ...(failed ? { lostTask: { conversationId: 'g1', assignmentId: 'x', reason: 'its model keeps failing', at: 1 } } : {}) }
      Object.assign(state.state.conversations[1]!, { reviewOf: 'lead', ...(failed ? { assignment: { id: 'x', agentId: 'r1', status: 'lost', lost: { reason: 'its model keeps failing', at: 1 } },
        lostAttempt: { id: 'x', agentId: 'r1', machineId: 'm1', engine: 'claude' } } : {}) })
      state.state.agents = [...agents.filter(a => a.id !== 'a2' || machines.includes('m2')), ...removed ? [] : [reviewer]]
      const s = scoped({ tab: state, context: { ok: true, value: { agentId: 'lead', tab: state, policy: { policy, warnings: [] },
        machines: machines.map(id => ({ machineId: id, name: id === 'm1' ? 'remote-1' : 'mac' })) } } })
      const result = await routeTask(input, s, where as never, opener(id => daemon(id, { agents: ['hr'], usage: accountOn(id === 'm1' ? 'acct-1' : 'acct-2') })), () => 'op')
      return { result, s }
    }
    const noted = (s: ReturnType<typeof scoped>) => [...new Set((recorded(s).command.decision.candidates as Array<{ machineId: string; label: string }>)
      .filter(c => c.label.includes('Its account lost this task before')).map(c => c.machineId))]
    it('notes the account whose model failed on its candidates, and still offers them', async () => {
      expect(noted((await route(false)).s)).toEqual([])
      const { result, s } = await route(true)
      expect(result).toMatchObject({ ok: true })
      expect(noted(s)).toEqual(['m1'])
      expect(JSON.stringify(recorded(s).command.decision.excluded)).not.toContain('lost this task')
      // With no other machine, that account is offered, with the note.
      const alone = await route(true, ['m1'])
      expect(alone.result).toMatchObject({ ok: true, candidates: expect.arrayContaining([expect.objectContaining({ machineId: 'm1', notes: expect.arrayContaining([expect.stringContaining('Its account lost this task before')]) })]) })
    })
    it('remembers the failed account after its agent is stopped and removed', async () => {
      expect(noted((await route(true, ['m1', 'm2'], true)).s)).toEqual(['m1'])
    })
  })

  // Final review, Important 3, and project instructions, phase 4: an agent whose last task was lost is offered, with a note
  // while the backend keeps its mark (backend superTabs/model.ts lostTask), which recovery or any assignment clears.
  describe('a member whose last task was lost', () => {
    const lostTask = { conversationId: 'g0', assignmentId: 'x', reason: 'no progress for 30 minutes', at: 1 }
    const healthy = { id: 'a3', name: 'Healthy', machineId: 'm1', harnessId: 'h3', configuration: { engine: 'claude', model: 'sonnet', effort: 'medium' } }
    const earlier = (assignment: Record<string, unknown>, more: Record<string, unknown> = {}) =>
      ({ id: 'g0', title: 'Old', leadId: 'lead', agentIds: ['lead', 'a1'], archived: false, brief: 'Old work.', checks: [], assignment, ...more })
    const lost = { id: 'x', agentId: 'a1', lost: { reason: lostTask.reason, at: 1 } }
    const route = async (members: unknown[], groups: unknown[]) => {
      const state = tab(); Object.assign(state.state, { agents: members, conversations: [...state.state.conversations, ...groups] })
      const s = scoped({ tab: state })
      const result = await routeTask(input, s, where as never, opener(id => daemon(id, { agents: ['h1', 'h3'] })), () => 'op')
      const decision = recorded(s).command.decision
      return { result, candidates: (decision.candidates as Array<{ title: string; agentId?: string; label: string }>), excluded: decision.excluded as Array<{ title: string; reason: string }> }
    }
    const marked = agents.map(a => a.id === 'a1' ? { ...a, lostTask } : a)
    const note = 'It lost its last task (no progress for 30 minutes).'
    const lineOf = (candidates: Array<{ title: string; label: string }>, name: string) => candidates.find(c => c.title.startsWith(name))?.label

    it('is offered with a note after its lost task is cancelled, beside a healthy member', async () => {
      const { result, candidates, excluded } = await route([...marked, healthy], [earlier({ ...lost, status: 'cancelled' })])
      expect(lineOf(candidates, 'Multiply')).toContain(note)
      expect(lineOf(candidates, 'Healthy')).not.toContain('lost its last task')
      expect(excluded.some(e => e.title.startsWith('Multiply'))).toBe(false)
      expect(result).toMatchObject({ ok: true, candidates: expect.arrayContaining([expect.objectContaining({ kind: 'member', agentId: 'a3' })]) })
    })
    it('is offered with a note after its lost task is handed to another agent', async () => {
      const { result, candidates } = await route([...marked, healthy],
        [earlier({ id: 'y', agentId: 'a3', status: 'working' }, { lostAttempt: { id: 'x', agentId: 'a1' } })])
      expect(lineOf(candidates, 'Multiply')).toContain(note)
      // a1 shares the Lead's account (m1, Claude), so the PM-account note sits beside it (pmAccounts.ts).
      expect(result).toMatchObject({ ok: true, candidates: expect.arrayContaining([expect.objectContaining({ kind: 'member', agentId: 'a1', notes: [note, PM_ACCOUNT_NOTE(['lead (Lead)'])] })]) })
    })
    it('carries no note once it finished a new task, which clears its mark', async () => {
      const { candidates } = await route([...agents, healthy],
        [earlier({ ...lost, status: 'cancelled' }), earlier({ id: 'z', agentId: 'a1', status: 'done' }, { id: 'g2' })])
      expect(lineOf(candidates, 'Multiply')).not.toContain('lost its last task')
    })
    it('is offered with a note while its task is still lost', async () => {
      const { candidates } = await route(marked, [earlier({ ...lost, status: 'lost' })])
      expect(lineOf(candidates, 'Multiply')).toContain(note)
    })
    // Re-check of #1749: recovery clears the mark while the old task may still be lost; that task alone says nothing.
    it('carries no note after its terminal was replaced, though its old task is still lost', async () => {
      const replaced = agents.map(a => a.id === 'a1' ? { ...a, harnessId: 'h1b' } : a)
      const state = tab(); Object.assign(state.state, { agents: replaced, conversations: [...state.state.conversations, earlier({ ...lost, status: 'lost' })] })
      const s = scoped({ tab: state })
      await routeTask(input, s, where as never, opener(id => daemon(id, { agents: ['h1b'] })), () => 'op')
      expect(lineOf(recorded(s).command.decision.candidates, 'Multiply')).not.toContain('lost its last task')
    })
    it('carries no note after it finished a newer task, though its old task is still lost', async () => {
      const { candidates } = await route(agents, [earlier({ ...lost, status: 'lost' }), earlier({ id: 'z', agentId: 'a1', status: 'done' }, { id: 'g2' })])
      expect(lineOf(candidates, 'Multiply')).not.toContain('lost its last task')
    })
  })
  // Project instructions, phase 4: an older backend's rules are its own to apply; the route reads none (the instructions'
  // Routing section says what the owner wants).
  it('ignores the owner\'s rules in the tab', async () => {
    const ruled = tab(); (ruled.state as Record<string, unknown>).rules = [{ id: 'r1', rule: { kind: 'forbidModel', model: 'sonnet' }, text: 'No Sonnet.' }, { id: 'r2', text: 7 }]
    const s = scoped({ tab: ruled })
    const result = await routeTask(input, s, where as never, opener(), () => 'op')
    expect(result).toMatchObject({ ok: true, candidates: expect.arrayContaining([expect.objectContaining({ model: 'sonnet' })]) })
    const decision = recorded(s).command.decision
    expect(decision.candidates.some((c: { model: string }) => c.model === 'sonnet')).toBe(true)
    expect(JSON.stringify(decision)).not.toContain('Breaks the owner')
  })
  it('never offers a Buddy: it brainstorms with its owner and is never assigned work', async () => {
    const t = tab(); t.state.agents = agents.map(a => a.id === 'a1' ? { ...a, buddyOf: { personId: 'peer', generation: 'g' } } : a)
    const s = scoped({ tab: t })
    await routeTask(input, s, where as never, opener(), () => 'op')
    const titles = (recorded(s).command.decision.candidates as Array<{ title: string }>).map(c => c.title)
    expect(titles.some(c => c.startsWith('Multiply'))).toBe(false)
  })
  // Project instructions, phase 4: an agent's role decides nothing here; an older client's Reviewer is a member like any other.
  it('offers an agent of any role for any group', async () => {
    const offered = async (reviewer: boolean, reviewGroup: boolean) => {
      const t = tab(); t.state.agents = agents.map(a => a.id === 'a1' && reviewer ? { ...a, role: 'reviewer' } : a)
      if (reviewGroup) (t.state.conversations[1] as Record<string, unknown>).reviewOf = 'g0'
      const s = scoped({ tab: t })
      const result = await routeTask(input, s, where as never, opener(), () => 'op')
      const titles = (recorded(s).command.decision.candidates as Array<{ title: string }>).map(c => c.title)
      return { member: titles.some(c => c.startsWith('Multiply')), assessment: recorded(s).command.decision.assessment, result }
    }
    for (const [reviewer, reviewGroup] of [[true, false], [false, true], [true, true], [false, false]]) {
      const { member, assessment } = await offered(reviewer!, reviewGroup!)
      expect(member).toBe(true)
      // No kind is forced on a linked task: the agent's own, or none.
      expect(assessment).not.toHaveProperty('kind')
    }
  })
  // Ken, 2026-10-30: reviews of Claude builds waited all morning for Codex quota. Any strong engine reviews any build now.
  it('offers a review the reviewed Builder\'s own engine at once while another engine could review, with no same-engine note', async () => {
    const t = tab(); t.state.agents = agents.map(a => a.id === 'a1' ? { ...a, role: 'reviewer', configuration: { engine: 'claude', model: 'opus', effort: 'high' } } : a)
    t.state.conversations.push({ id: 'g0', title: 'Built', leadId: 'lead', agentIds: ['lead'], archived: false, brief: '', checks: [],
      assignment: { id: 'b', agentId: 'gone', status: 'review', configuration: { engine: 'claude', model: 'opus', effort: 'high' } } } as never)
    Object.assign(t.state.conversations[1]!, { reviewOf: 'g0' })
    // Codex is on in the policy and signed in with quota, so a Codex Reviewer could start too.
    const both = { ...floorPolicy, models: floorPolicy.models.map(m => ({ ...m, enabled: true })) }
    const context = { ok: true, value: { agentId: 'lead', tab: t, policy: { policy: both, warnings: [] }, machines: [{ machineId: 'm1', name: 'remote-1' }] } }
    const s = scoped({ context })
    const result = await routeTask(input, s, where as never, opener(id => daemon(id, { agents: id === 'm1' ? ['h1'] : [],
      engines: [{ engine: 'claude', installed: true, signedIn: true }, { engine: 'codex', installed: true, signedIn: true }] })), () => 'op')
    // The idle Claude Opus agent is a candidate at once, beside Codex's top tier.
    expect(result).toMatchObject({ ok: true, candidates: expect.arrayContaining([expect.objectContaining({ kind: 'new', engine: 'codex' })]) })
    const decision = recorded(s).command.decision
    expect(decision.candidates.map((c: { title: string }) => c.title)).toContain('Multiply · Claude opus high · remote-1')
    expect(decision.why).not.toMatch(/same-engine/)
    expect(JSON.stringify(decision.excluded)).not.toMatch(/another engine|same-engine|top tier/)
  })
  // 2026-10-30: every Codex Reviewer was out of weekly quota for days, and Claude builds' reviews waited all morning for it.
  // Since Ken's ruling that day the review takes whichever strong Reviewer can start, and waits for no other engine.
  describe('a review while Codex is short of quota', () => {
    const DAY = 86_400_000, MIN = 60_000
    const pool = (machineId: string, engines: string[], weekly: { usedPercent: number; resetsIn: number } = { usedPercent: 10, resetsIn: 5 * DAY }) => ({
      machineId, name: machineId, cwd: '/w', engines, paused: false, contributorId: 'owner',
      observation: { receivedAt: Date.now(), resources: { memoryTotalBytes: 32e9, memoryUsedBytes: 2e9, projectDiskFreeBytes: 200e9 }, tools: { git: true }, runningHarnessIds: [],
        quotas: engines.map(engine => ({ engine, known: true, windows: [{ name: '5-hour', usedPercent: 20, resetsAt: Date.now() + 3600000 },
          { name: 'weekly', usedPercent: engine === 'codex' ? weekly.usedPercent : 10, resetsAt: Date.now() + (engine === 'codex' ? weekly.resetsIn : 5 * DAY) }] })) } })
    const both = { ...floorPolicy, models: floorPolicy.models.map(m => ({ ...m, enabled: true })) }
    const routeWith = async (codexBox: Record<string, unknown>) => {
      const t = tab(); t.state.agents = [] as never
      t.state.conversations.push({ id: 'g0', title: 'Built', leadId: 'lead', agentIds: ['lead'], archived: false, brief: '', checks: [],
        assignment: { id: 'b', agentId: 'gone', status: 'review', configuration: { engine: 'claude', model: 'opus', effort: 'high' } } } as never)
      Object.assign(t.state.conversations[1]!, { reviewOf: 'g0' })
      Object.assign(t.state, { machines: [pool('m1', ['claude']), codexBox] })
      const s = scoped({ context: { ok: true, value: { agentId: 'lead', tab: t, policy: { policy: both, warnings: [] }, machines: [] } } })
      const result = await routeTask(input, s, where as never, opener(), () => 'op')
      return { result, decision: 'error' in result ? null : recorded(s).command.decision }
    }
    const opusNow = { ok: true, candidates: expect.arrayContaining([expect.objectContaining({ engine: 'claude', model: 'opus', machineId: 'm1' })]) }

    it('starts Claude Opus at once for a Claude build while Codex is out of quota for days, with no note', async () => {
      const { result, decision } = await routeWith(pool('m2', ['codex'], { usedPercent: 100, resetsIn: 3 * DAY }))
      expect(result).toMatchObject(opusNow)
      expect(decision.why).not.toMatch(/same-engine/)
    })

    it('waits for no Codex window that resets soon', async () => {
      const { result, decision } = await routeWith(pool('m2', ['codex'], { usedPercent: 100, resetsIn: 10 * MIN }))
      expect(result).toMatchObject(opusNow)
      expect(JSON.stringify(decision.excluded)).not.toMatch(/another engine/)
    })
  })
  // Project instructions, phase 4: strength does not filter. The idle Sonnet member is offered beside the top tier.
  it('offers an idle Sonnet member beside the top tier, and tells the server when a person named the model', async () => {
    const s = scoped({ policy: floorPolicy })
    expect(await routeTask(input, s, where as never, opener(), () => 'op')).toMatchObject({ ok: true, candidates: expect.arrayContaining([expect.objectContaining({ kind: 'new', model: 'opus' })]) })
    expect(recorded(s).command.decision.candidates).toEqual(expect.arrayContaining([expect.objectContaining({ title: 'Multiply · Claude sonnet medium · remote-1', quality: 2 })]))
    expect(JSON.stringify(recorded(s).command.decision.excluded)).not.toContain('rated 3')
    const named = scoped({ policy: floorPolicy })
    expect(await routeTask({ ...input, assessment: { ...input.assessment, model: 'sonnet' } }, named, where as never, opener(), () => 'op'))
      .toMatchObject({ ok: true, candidates: expect.arrayContaining([expect.objectContaining({ kind: 'member', agentId: 'a1' })]) })
    expect(recorded(named).command.decision.assessment).toMatchObject({ model: 'sonnet' })
  })
  it('tells the agent how to assign a member or start a new agent with the decision', async () => {
    const member = await routeTask(input, scoped(), where as never, opener(), () => 'op')
    expect(member).toMatchObject({ candidates: expect.arrayContaining([expect.objectContaining({ kind: 'member', agentId: 'a1' })]) })
    // The chosen row's agent; the route names the pick (task #3576), so no rule is asked for.
    expect(member.next).toContain('"type":"work.assign","conversationId":"g1","agentId":"AGENT","routeDecisionId":"d1"}')
    const fresh = await routeTask({ ...input, assessment: { ...input.assessment, difficulty: 'high' } }, scoped({ policy: floorPolicy }), where as never, opener(), () => 'op')
    expect(fresh).toMatchObject({ candidates: expect.arrayContaining([expect.objectContaining({ kind: 'new', engine: 'claude', model: 'opus', effort: 'high' })]) })
    expect(fresh.next).toContain('"machineId":"MACHINE","engine":"ENGINE","model":"MODEL","effort":"EFFORT","routeDecisionId":"d1"}')
    // Without a branch the agent starts from the default one, and the hint says how to choose another.
    expect(fresh.next).toContain('"branchRef":"refs/remotes/origin/BRANCH"')
    expect(fresh.next).toContain('default branch, fetched first')
    // A complete start: every field its schema requires, with the defaults the help gives.
    const hint = JSON.parse(/start OPERATION_ID (\{.*\}), then/.exec(fresh.next as string)![1]!)
    expect(hint).toEqual({ conversationId: 'g1', name: 'NAME', roleName: 'ROLE', cwd: 'CWD', worktree: true,
      requirements: { memoryBytes: 2147483648, diskBytes: 10737418240, maxCpuPercent: 100, tools: ['git'], devices: [], quotaRequired: false },
      machineId: 'MACHINE', engine: 'ENGINE', model: 'MODEL', effort: 'EFFORT', routeDecisionId: 'd1' })
    // Each new agent's row says the folder it starts in.
    for (const row of (fresh.candidates as Array<{ kind: string; cwd?: string }>).filter(r => r.kind === 'new')) expect(row.cwd).toBe('/absolute/repo/on/that/machine')
    // The task's own repository, when the assessment named one, and no decision when none was recorded.
    const placed = await routeTask({ ...input, assessment: { ...input.assessment, difficulty: 'high', needs: { cwd: '/repo/api' } } },
      scoped({ record: { error: 'BACKEND_UNAVAILABLE' }, policy: floorPolicy }), where as never, opener(), () => 'op')
    expect((placed.candidates as Array<{ kind: string; cwd?: string }>).find(r => r.kind === 'new')).toMatchObject({ cwd: '/repo/api' })
    expect(placed.next).not.toContain('routeDecisionId')
    expect(placed.next).not.toContain('RULE')
  })
  it('admits a candidate on the requirements its start hint asks for, with the task\'s tools and devices', async () => {
    const needs = { tools: ['git', 'docker'], devices: ['/dev/ttyUSB0'] }
    const hard = { ...input, assessment: { ...input.assessment, difficulty: 'high', needs } }
    const daemons: ReturnType<typeof daemon>[] = []
    const make = opener((id) => { const d = daemon(id, { tools: { git: true, docker: true }, devices: { '/dev/ttyUSB0': true } }); daemons.push(d); return d })
    const result = await routeTask(hard, scoped(), where as never, make, () => 'op')
    const hint = JSON.parse(/start OPERATION_ID (\{.*\}), then/.exec(result.next as string)![1]!)
    expect(hint.requirements).toEqual({ memoryBytes: 2147483648, diskBytes: 10737418240, maxCpuPercent: 100, tools: ['git', 'docker'], devices: ['/dev/ttyUSB0'], quotaRequired: false })
    // The probe asked each machine about the same tools and devices, so what it admitted starts.
    const capacity = daemons.flatMap(d => d.request.mock.calls).filter(c => c[0] === 'super_tabs').map(c => c[1] as Record<string, unknown>)
    expect(capacity.length).toBeGreaterThan(0)
    for (const asked of capacity) expect(asked).toMatchObject({ tools: ['git', 'docker'], devices: ['/dev/ttyUSB0'] })
  })
  it('keeps a machine in the route when its CPU reads busy, because CPU use is only a momentary reading', async () => {
    const make = opener(id => {
      const d = daemon(id, { agents: id === 'm1' ? ['h1'] : [] }), request = d.request.getMockImplementation()!
      d.request.mockImplementation(async (type, payload, ms) => type === 'super_tabs' && (payload as Record<string, unknown>)?.action === 'capacity'
        ? { resources: { memoryTotalBytes: 8e9, memoryUsedBytes: 1e9, cpuPercent: 98, projectDiskFreeBytes: 1e11 }, tools: { git: true }, devices: {} }
        : request(type, payload, ms))
      return d
    })
    const result = await routeTask({ ...input, assessment: { ...input.assessment, difficulty: 'high' } }, scoped({ policy: floorPolicy }), where as never, make, () => 'op')
    expect(result).toMatchObject({ ok: true, candidates: expect.arrayContaining([expect.objectContaining({ kind: 'new' })]) })
  })
  it('warns when the account\'s machines could not be read, and still offers the Lead\'s own machine', async () => {
    const context = { ok: true, value: { agentId: 'lead', tab: tab(), policy: { policy, warnings: ['w'] }, machines: [], machinesUnreadable: true } }
    const result = await routeTask(input, scoped({ context }), where as never, opener(), () => 'op')
    expect(result).toMatchObject({ ok: true, warnings: ['w', "The account's machine list could not be read; only known machines were considered."] })
    const fine = await routeTask(input, scoped(), where as never, opener(), () => 'op')
    expect(fine.warnings).toEqual(['w'])
  })
  it('considers only the account machines the Swarm lists, and always the Lead\'s own', async () => {
    const context = { ok: true, value: { agentId: 'lead', tab: tab(), policy: { policy }, machines: [{ machineId: 'm1', name: 'remote-1' }, { machineId: 'm2', name: 'mac' }, { machineId: 'm9', name: 'vm' }] } }
    const s = scoped({ context }), open = opener()
    await routeTask(input, s, where as never, open, () => 'op')
    // m9 is the account's but not in the Swarm's list: it is neither probed nor a candidate nor an exclusion.
    expect(open.mock.calls.map(c => c[1]).sort()).toEqual(['m1', 'm2'])
    const decision = recorded(s).command.decision
    expect([...decision.candidates, ...decision.excluded].some((c: { title: string }) => c.title.includes('vm'))).toBe(false)
    expect(decision.candidates.some((c: { machineId?: string; title: string }) => c.title.includes('mac'))).toBe(true)
  })
  it('offers new agents on the Lead\'s own machine when the account list could not be read', async () => {
    const context = { ok: true, value: { agentId: 'lead', tab: tab(), policy: { policy }, machines: [], machinesUnreadable: true } }
    const s = scoped({ context }), open = opener()
    await routeTask({ ...input, assessment: { ...input.assessment, difficulty: 'high' } }, s, where as never, open, () => 'op')
    expect(open.mock.calls.map(c => c[1])).toEqual(['m1'])
    expect(recorded(s).command.decision.candidates.some((c: { title: string }) => c.title.startsWith('New agent') && c.title.includes('m1'))).toBe(true)
  })
  it('starts no new agent on an account machine whose system is on the list, and keeps a member already there', async () => {
    // The list is empty now (#1969); it stays for a system Swarm agents cannot run on yet.
    const os = 'Plan 9'
    const plan9 = opener(id => daemon(id, { agents: id === 'm1' ? ['h1'] : [], capacity: { resources: { memoryTotalBytes: 8e9, memoryUsedBytes: 1e9, cpuPercent: 10, projectDiskFreeBytes: 1e11, os }, tools: { git: true }, devices: {} } }))
    const s = scoped()
    await withBlocked(os, async () => expect(await routeTask(input, s, where, plan9)).toMatchObject({ ok: true, candidates: expect.arrayContaining([expect.objectContaining({ kind: 'member' })]) }))
    expect(recorded(s).command.decision.excluded).toEqual(expect.arrayContaining([expect.objectContaining({ title: expect.stringContaining('New agent'),
      reason: expect.stringContaining('Swarm agents do not run on Plan 9 yet.') })]))
  })
  it('offers new agents on an account machine that runs Windows, as on any other, and notes where the last two starts started nothing', async () => {
    // Swarm agents were kept off Windows while grid's CLI did not run there (2026-10-26). It does since grid 0.3.61 and CLI
    // 0.4.95: a Claude Code agent on a grid model started and answered on DESKTOP-0TFH0OJ.
    const windows = opener(id => daemon(id, { agents: id === 'm1' ? ['h1'] : [], capacity: { resources: { memoryTotalBytes: 8e9, memoryUsedBytes: 1e9, cpuPercent: 10, projectDiskFreeBytes: 1e11, os: 'Windows' }, tools: { git: true }, devices: {} } }))
    const s = scoped()
    await routeTask({ ...input, assessment: { ...input.assessment, difficulty: 'high' } }, s, where, windows)
    const decision = recorded(s).command.decision
    expect(decision.candidates.some((c: { title: string }) => c.title.startsWith('New agent'))).toBe(true)
    expect(JSON.stringify(decision.excluded)).not.toContain('Windows')
    const failed = { id: 's', request: { machineId: 'm1' }, harnessId: null, dispatched: true, error: 'The engine did not start.' }
    const t = tab(); Object.assign(t.state, { starts: [failed, { ...failed, id: 's2' }] })
    const stuck = scoped({ tab: t })
    await routeTask(input, stuck, where, opener())
    // A note on its new agents, not a filter (project instructions, phase 4); a member there is not a start.
    const rows = recorded(stuck).command.decision.candidates as Array<{ title: string; label: string; machineId: string }>
    expect(rows.filter(r => r.machineId === 'm1' && r.title.startsWith('New agent')).every(r => r.label.includes('last 2 starts'))).toBe(true)
    expect(rows.filter(r => r.machineId === 'm1' && r.title.startsWith('New agent'))).not.toHaveLength(0)
    expect(rows.find(r => r.title.startsWith('Multiply'))!.label).not.toContain('last 2 starts')
  })
  // Task #3861 (River, 11-06): a start that failed on this machine's own storage or clone sent the next one to the same machine, five
  // times in a day. One such failure is enough to leave it out, with the reason, until it is fixed.
  describe('a machine whose last start failed on its own storage or clone', () => {
    const now = Date.parse('2026-11-06T10:00:00Z')
    const fault = (id: string, error: string, at = now - 60_000, version?: string) => ({ id, request: { machineId: 'm1' }, harnessId: null, dispatched: true, error,
      dispatchedAt: at, ...(version ? { dispatchedVersion: version } : {}) })
    const storage = 'CREATION_STORAGE_FAILED: The creation folder ~/.harness/cli/data/agent-creations is not usable by this account (permissions). It must belong to this account with mode 700; the owner of this computer fixes that, Harness does not change it.'
    const clone = "This machine could not clone the project repository with the Harness GitHub App's token: Cloning stopped making progress. Check the connection and retry."

    it.each([[storage, 'creation folder'], [clone, 'clone the project repository']])('is left out by one such failure, with why (%#)', (error, said) => {
      const reason = faulted([fault('s1', error)], 'm1', {}, now)
      expect(reason).toContain(said)
      expect(reason).toContain('30 minutes')
    })
    it('is not left out for any other failure, an older one, or one a later start got past', () => {
      expect(faulted([fault('s1', 'The engine did not start.')], 'm1', {}, now)).toBeNull()
      expect(faulted([fault('s1', storage, now - STALL_RETRY_MS - 1)], 'm1', {}, now)).toBeNull()
      expect(faulted([fault('s1', storage), { ...fault('s2', ''), error: null, harnessId: 'h' }], 'm1', {}, now)).toBeNull()
      expect(faulted([fault('s1', storage, now - 60_000, '0.5.1')], 'm1', { clientVersion: '0.5.6' }, now)).toBeNull()
      expect(faulted([fault('s1', storage)], 'm2', {}, now)).toBeNull()
    })
    it('keeps the machine out of the route\'s new agents, naming the reason', async () => {
      const t = tab(); Object.assign(t.state, { starts: [fault('s', clone, Date.now() - 60_000)] })
      const s = scoped({ tab: t })
      await routeTask({ ...input, assessment: { ...input.assessment, difficulty: 'high' } }, s, where, opener())
      const decision = recorded(s).command.decision
      expect(decision.candidates.some((c: { title: string; machineId: string }) => c.title.startsWith('New agent') && c.machineId === 'm1')).toBe(false)
      expect(JSON.stringify(decision.excluded)).toContain('clone the project repository')
    })
  })
  // Mac Auto, Swarm autonomous-harness, 2026-10-29: two starts failed on Harness bugs later fixed and released, and the machine
  // stayed out of every route until a person started an agent there by hand.
  it('lets a machine back into routes once its failed starts say nothing about it: updated since, or 30 minutes old', () => {
    const now = Date.parse('2026-10-29T10:00:00Z')
    const failed = (id: string, at: number, version?: string) => ({ id, request: { machineId: 'm1' }, harnessId: null, dispatched: true,
      error: 'Mac Auto did not start it within 3 minutes', dispatchedAt: at, ...(version ? { dispatchedVersion: version } : {}) })
    const two = [failed('s1', now - 20 * 60_000, '0.5.1'), failed('s2', now - 10 * 60_000, '0.5.1')]
    expect(stalled(two, 'm1', { clientVersion: '0.5.1' }, now)).toBe(true)
    // Its daemon now runs another version than the newer failure was dispatched to.
    expect(stalled(two, 'm1', { clientVersion: '0.5.6' }, now)).toBe(false)
    // A failure on the version it runs now still counts, whatever the one before was dispatched to.
    expect(stalled([two[0]!, failed('s3', now - 60_000, '0.5.6')], 'm1', { clientVersion: '0.5.6' }, now)).toBe(true)
    // Routing tries once again after the window, and a start that fails then keeps it out another window.
    expect(stalled(two, 'm1', {}, now - 10 * 60_000 + STALL_RETRY_MS)).toBe(false)
    expect(stalled([two[1]!, failed('s4', now + STALL_RETRY_MS, '0.5.1')], 'm1', {}, now + STALL_RETRY_MS + 60_000)).toBe(true)
    // A start from before either was recorded counts as before; one that started an agent, or one still being created, never stalls.
    const old = { id: 'o', request: { machineId: 'm1' }, harnessId: null, dispatched: true, error: 'failed' }
    expect(stalled([old, { ...old, id: 'o2' }], 'm1', { clientVersion: '0.5.6' }, now)).toBe(true)
    expect(stalled([two[0]!, { ...two[1]!, harnessId: 'h', error: null }], 'm1', {}, now)).toBe(false)
    expect(stalled([two[0]!, { ...two[1]!, error: null }], 'm1', {}, now)).toBe(false)
    // A Team account's refusal of a model its owner is not offered says the request was wrong, not the machine: it never
    // counts, so the two failures before it decide (start.ts, Fork A's refusal before anything launches).
    const refused = (id: string) => ({ id, request: { machineId: 'm1' }, harnessId: null, dispatched: false, requestedAt: now,
      error: "This machine's ChatGPT account (its owner's subscription, used through the owner's relay) does not offer gpt-6.1-sol; it offers gpt-6-luna, gpt-5.6-terra." })
    expect(stalled([refused('r1'), refused('r2')], 'm1', {}, now)).toBe(false)
    expect(stalled([two[0]!, refused('r3'), refused('r4')], 'm1', {}, now)).toBe(false)
    expect(stalled([...two, refused('r5')], 'm1', { clientVersion: '0.5.1' }, now)).toBe(true)
    // A start queued and never picked up is dated by its request.
    const queued = (id: string) => ({ id, request: { machineId: 'm1' }, harnessId: null, dispatched: false, error: null, requestedAt: now - STALL_RETRY_MS - 1 })
    expect(stalled([queued('q1'), queued('q2')], 'm1', {}, now)).toBe(false)
  })
  it('routes again to a machine whose last 2 starts failed more than 30 minutes ago', async () => {
    const failed = { id: 's', request: { machineId: 'm1' }, harnessId: null, dispatched: true, error: 'The engine did not start.',
      dispatchedAt: Date.now() - STALL_RETRY_MS - 60_000 }
    const t = tab(); Object.assign(t.state, { starts: [failed, { ...failed, id: 's2' }] })
    const s = scoped({ tab: t })
    await routeTask(input, s, where, opener())
    expect(JSON.stringify(recorded(s).command.decision)).not.toContain('last 2 starts')
  })
  it('says a listed machine without the project folder needs it cloned, not that its disk could not be verified', async () => {
    const needs = { cwd: '/repo/api' }
    const noFolder = { ok: true, resources: { memoryTotalBytes: 8e9, memoryUsedBytes: 1e9, cpuPercent: 10 }, tools: {}, devices: {} }
    const s = scoped()
    await routeTask({ ...input, assessment: { ...input.assessment, needs } }, s, where as never, opener(id => daemon(id, { agents: id === 'm1' ? ['h1'] : [], capacity: id === 'm1' ? { ...noFolder, tools: { git: true } } : noFolder })), () => 'op')
    const excluded = recorded(s).command.decision.excluded as Array<{ title: string; reason: string }>
    const reasonOf = (machine: string) => excluded.filter(e => e.title.includes(machine)).map(e => e.reason)
    expect(reasonOf('mac').some(r => r.includes('This machine does not have the project folder. Add it to the Swarm so it can clone the project.'))).toBe(true)
    expect(reasonOf('mac').some(r => /Project disk space/.test(r))).toBe(false)
    // Its other reasons stay.
    expect(reasonOf('mac').some(r => /Required tool not verified: git/.test(r))).toBe(true)
    // The Lead's own machine keeps the plain reason: its folder was named by the Lead itself.
    expect(reasonOf('remote-1').some(r => r.includes('Project disk space could not be verified.'))).toBe(true)
  })
  it('offers a listed machine whose capacity read measured the project folder', async () => {
    const s = scoped()
    await routeTask({ ...input, assessment: { ...input.assessment, needs: { cwd: '/repo/api' } } }, s, where as never,
      opener(id => daemon(id, { agents: id === 'm1' ? ['h1'] : [], capacity: { ok: true, resources: { memoryTotalBytes: 8e9, memoryUsedBytes: 1e9, cpuPercent: 10, projectDiskFreeBytes: 1e11 }, tools: { git: true }, devices: {} } })), () => 'op')
    expect(recorded(s).command.decision.candidates.some((c: { title: string }) => c.title.startsWith('New agent') && c.title.includes('mac'))).toBe(true)
  })
  it('keeps "could not be verified" on a listed machine whose capacity read failed, because a failed read says nothing about the folder', async () => {
    const s = scoped()
    await routeTask({ ...input, assessment: { ...input.assessment, needs: { cwd: '/repo/api' } } }, s, where as never,
      opener(id => daemon(id, { agents: id === 'm1' ? ['h1'] : [], ...(id === 'm2' ? { capacity: { ok: false, error: 'UNSUPPORTED' } } : {}) })), () => 'op')
    const reasons = (recorded(s).command.decision.excluded as Array<{ title: string; reason: string }>).filter(e => e.title.includes('mac')).map(e => e.reason)
    expect(reasons.length).toBeGreaterThan(0)
    for (const reason of reasons) {
      expect(reason).toContain('Project disk space could not be verified.')
      expect(reason).not.toContain('does not have the project folder')
    }
  })
  it('names a member that has no name, model or effort of its own, and leaves the decision blank when it is unrecorded', async () => {
    const plain = tab(); plain.state.agents = [{ id: 'a1', machineId: 'm1', harnessId: 'h1', configuration: { engine: 'claude' } }, agents[1]] as never
    // A member launched on Claude's own model is rated 3 where the catalog rates that model 3; with no row it is unrated, at 1.
    const options = { tab: plain, record: { error: 'BACKEND_UNAVAILABLE' }, policy: floorPolicy }
    const rated = { ...options, policy: { ...floorPolicy, models: [...floorPolicy.models, { engine: 'claude', model: 'default', effort: 'default', quality: 3, cost: 1, enabled: true }] } }
    const easy = await routeTask({ ...input, assessment: { ...input.assessment, difficulty: 'low' } }, scoped(rated), where as never, opener())
    expect(easy).toMatchObject({ unrecorded: true, candidates: expect.arrayContaining([expect.objectContaining({ kind: 'member', title: 'Member · Claude default default · remote-1' })]) })
    expect(easy.next).not.toContain('routeDecisionId')
    const hard = await routeTask({ ...input, assessment: { ...input.assessment, difficulty: 'high' } }, scoped(options), where as never, opener())
    expect(hard).toMatchObject({ unrecorded: true, candidates: expect.arrayContaining([expect.objectContaining({ kind: 'new' })]) })
    expect(hard.next).not.toMatch(/routeDecisionId"|same routeDecisionId/)
  })
  it('returns a record refusal as an error, and only a transport failure as unrecorded', async () => {
    expect(await routeTask(input, scoped({ record: { error: 'RATE_LIMITED', detail: 'slow down', status: 429 } }), where as never, opener()))
      .toMatchObject({ error: 'RATE_LIMITED' })
    for (const error of ['BACKEND_UNAVAILABLE', 'BACKEND_TIMEOUT', 'BACKEND_UNREACHABLE', 'AGENT_UNAVAILABLE', 'SUPER_TAB_UNAVAILABLE']) {
      expect(await routeTask(input, scoped({ record: { error } }), where as never, opener())).toMatchObject({ ok: true, unrecorded: true })
    }
  })
  it('returns NO_CANDIDATE with reasons when every account machine is offline, without opening any', async () => {
    const open = vi.fn()
    const offline = { ok: true, value: { agentId: 'lead', tab: tab(), policy: { policy }, machines: [{ machineId: 'm1', name: 'remote-1', status: 'offline' }, { machineId: 'm2', name: 'mac', status: 'offline' }] } }
    const result = await routeTask(input, scoped({ context: offline }), where as never, open as never)
    expect(result).toMatchObject({ error: 'NO_CANDIDATE' })
    expect((result.excluded as Array<{ reason: string }>).every(e => /offline or unreachable/.test(e.reason))).toBe(true)
    expect((result.excluded as unknown[]).length).toBeGreaterThan(2)
    expect(open).not.toHaveBeenCalled()
  })
  it('keeps a member on a machine this account cannot open as unknown, not offline, and routes to it when nothing known is left', async () => {
    const s = scoped()
    const open = vi.fn(async () => { throw Object.assign(new Error('gone'), { code: 'MACHINE_UNAVAILABLE' }) })
    const result = await routeTask(input, s, where as never, open as never)
    expect(result).toMatchObject({ ok: true, candidates: expect.arrayContaining([expect.objectContaining({ kind: 'member', agentId: 'a1' })]) })
    const decision = recorded(s).command.decision
    // The two members there, the idle coordinator among them, by title.
    expect(decision.candidates.map((c: { agentId?: string }) => c.agentId)).toEqual(['lead', 'a1'])
    // New agents there stay excluded: nothing can start on a machine nobody can open.
    expect(decision.excluded).toEqual(expect.arrayContaining([expect.objectContaining({ reason: 'Machine is offline or unreachable.' })]))
    expect(decision.excluded.some((e: { title: string }) => e.title.startsWith('Multiply'))).toBe(false)
  })
  it('shows a member whose quota is known and one on a machine that cannot be read', async () => {
    const two = tab(); two.state.agents = [...agents, { id: 'a2', name: 'Collab', machineId: 'theirs', harnessId: 'h2', configuration: { engine: 'claude', model: 'sonnet', effort: 'medium' } }] as never
    const s = scoped({ tab: two })
    const open = vi.fn(async (_w: unknown, id: string) => id === 'theirs' ? Promise.reject(new Error('not yours')) : daemon(id, { agents: id === 'm1' ? ['h1'] : [] })) as never
    // theirs is not in the account's list, m1 is; the collaborator's member there has an unknown quota.
    const result = await routeTask(input, s, where as never, open)
    expect(result).toMatchObject({ ok: true, candidates: expect.arrayContaining([expect.objectContaining({ agentId: 'a1' })]) })
    const titles = (recorded(s).command.decision.candidates as Array<{ title: string }>).map(c => c.title)
    expect(titles.indexOf('Multiply · Claude sonnet medium · remote-1')).toBeLessThan(titles.indexOf('Collab · Claude sonnet medium · theirs'))
    expect(JSON.stringify(recorded(s).command.decision.excluded)).not.toContain('Collab')
  })
  it('opens a machine only members know and probes its members, but starts nothing new there', async () => {
    const two = tab(); two.state.agents = [{ ...agents[0], machineId: 'theirs' }, agents[1]] as never
    const s = scoped({ tab: two })
    const open = opener(id => daemon(id, { agents: id === 'theirs' ? ['h1'] : [] }))
    await routeTask(input, s, where as never, open)
    const titles = (recorded(s).command.decision.candidates as Array<{ title: string }>).map(c => c.title)
    expect(titles).toContain('Multiply · Claude sonnet medium · theirs')
    expect(titles.filter(t => t.endsWith('· theirs'))).toHaveLength(1)
  })
  it('rates members by the model their engine reported after work', async () => {
    const working = tab()
    working.state.agents = [
      { id: 'a1', name: 'Opus hand', machineId: 'm1', harnessId: 'h1', configuration: { engine: 'claude', model: 'claude-opus-4-7', effort: 'high' } },
      { id: 'a3', name: 'Sonnet hand', machineId: 'm1', harnessId: 'h3', configuration: { engine: 'claude', model: 'claude-sonnet-4-5[1m]', effort: 'medium' } },
      { id: 'a4', name: 'Codex hand', machineId: 'm1', harnessId: 'h4', configuration: { engine: 'codex', model: 'gpt-5.5', effort: 'high' } },
      { id: 'a5', name: 'Mini hand', machineId: 'm1', harnessId: 'h5', configuration: { engine: 'codex', model: 'gpt-5.5-mini', effort: 'high' } },
      agents[1]] as never
    // Codex hand was launched on the engine's own model, which it reports by its slug; Mini hand was launched on a model the
    // policy does not name (review of #2137).
    Object.assign(working.state, { starts: [{ harnessId: 'h4', request: { model: 'default' } }, { harnessId: 'h5', request: { model: 'gpt-5.5-mini' } }] })
    const s = scoped({ tab: working, policy: floorPolicy })
    const open = opener(id => daemon(id, { agents: id === 'm1' ? ['h1', 'h3', 'h4', 'h5'] : [], usage: { providers: [] }, engines: [{ engine: 'claude', installed: true, signedIn: true }, { engine: 'codex', installed: true, signedIn: true }] }))
    await routeTask({ ...input, assessment: { ...input.assessment, difficulty: 'low' } }, s, where as never, open)
    const candidates = recorded(s).command.decision.candidates as Array<{ title: string; quality: number; cost: number; label: string }>
    const rated = Object.fromEntries(candidates.filter(c => /hand/.test(c.title)).map(c => [c.title.split(' · ')[0], [c.quality, c.cost]]))
    expect(rated).toEqual({ 'Opus hand': [3, 3], 'Codex hand': [3, 2], 'Sonnet hand': [2, 2], 'Mini hand': [1, 1] })
    // A model the catalog does not rate is offered unrated, and its line says so.
    expect(candidates.find(c => c.title.startsWith('Mini hand'))!.label).toContain('unrated')
    expect(candidates.find(c => c.title.startsWith('Sonnet hand'))!.label).toContain('strength 2')
  })
  it('counts the quota open picks hold on the same account, and tells the Lead when nobody is expected to cover the task', async () => {
    const pick = { conversationId: 'g1', title: 'Pick', outcome: null, assignmentId: 'as1', createdAt: new Date().toISOString(), final: { kind: 'new', engine: 'claude', model: 'opus' } }
    const decisions = [{ ...pick, id: 'd1', account: 'acct', spend: { '5-hour': 45, weekly: 6 }, size: 'medium' },
      // A finished pick, one not yet started and one from before phase 1 hold nothing.
      { ...pick, id: 'd2', account: 'acct', spend: { '5-hour': 45 }, size: 'medium', outcome: 'done' }, { ...pick, id: 'd3', account: 'acct', spend: { '5-hour': 45 }, size: 'medium', final: null }, { ...pick, id: 'd4' }]
    const s = scoped({ history: { ok: true, value: { messages: [], decisions, attempts: [] } } })
    const result = await routeTask(input, s, where as never, opener(), () => 'op')
    // 60% left less the 10% reserve and the 45% the open pick holds leaves 5% for a task that needs 30%.
    expect(recorded(s).command.decision.candidates.every((c: { fit: { status: string; account: string } }) => c.fit.status === 'short' && c.fit.account === 'acct')).toBe(true)
    expect(result.warnings).toEqual(['w', NONE_COVERS])
  })
  it('keeps a queued start in the reroute quota hold until its linked work finishes', async () => {
    const queued = { id: 'queued', conversationId: 'g1', account: 'acct', spend: { '5-hour': 45 }, size: 'medium',
      final: { kind: 'new', startId: 'start-1', engine: 'claude', model: 'opus' }, assignmentId: null,
      outcome: null as string | null, createdAt: new Date().toISOString() }
    const routeWith = async (decision: typeof queued) => {
      const s = scoped({ history: { ok: true, value: { decisions: [decision], attempts: [] } } })
      await routeTask(input, s, where as never, opener(), () => 'op')
      return recorded(s).command.decision.candidates as Array<{ fit: { status: string } }>
    }
    expect((await routeWith(queued)).every(candidate => candidate.fit.status === 'short')).toBe(true)
    expect((await routeWith({ ...queued, outcome: 'done' })).some(candidate => candidate.fit.status === 'fits')).toBe(true)
  })
  // Task #3163: each candidate's weekly % left and reset go with its fit; a backend from before them refuses them, and the
  // decision is then recorded without them, still with no pick, before any older shape is tried.
  it('records each candidate\'s quota, and without it for a backend that refuses it, still with no pick', async () => {
    const s = scoped(), base = s.getMockImplementation()!
    s.mockImplementation(async payload => payload.action === 'command' && JSON.stringify(payload.command).includes('"left"')
      ? { error: 'VALIDATION_ERROR', detail: 'decision.candidates.0.fit: Unrecognized key: "left"', status: 400 } : base(payload))
    expect(await routeTask(input, s, where as never, opener(), () => 'op')).toMatchObject({ ok: true, routeDecisionId: 'd1' })
    const [quoted, unquoted, ...rest] = s.mock.calls.filter(c => c[0].action === 'command').map(c => c[0])
    expect(rest).toEqual([])
    expect(quoted.command.decision.candidates[0].fit).toHaveProperty('left')
    expect(quoted.command.decision.candidates[0].fit).toHaveProperty('resetsAt')
    expect(unquoted.operationId).toBe(quoted.operationId)
    expect(unquoted.command.decision).toHaveProperty('pick')
    expect(unquoted.command.decision.candidates[0].fit).not.toHaveProperty('left')
    expect(unquoted.command.decision.candidates[0].fit).toHaveProperty('status')
  })
  // Review of #3117: small work steps up to Opus or Astra after its second failed attempt (Routing, Models). The route counts
  // the task's ended attempts from the history it reads; when that read fails it keeps both models.
  it('offers Opus to a small task once two of its attempts ended, and both models when its attempts cannot be read', async () => {
    const small = { ...input, assessment: { ...input.assessment, size: 'small' } }
    const offered = async (history: Answer) => {
      const answer = await routeTask(small, scoped({ history }), where as never, opener(), () => 'op')
      return [...new Set((answer.candidates as Array<{ kind: string; engine: string; model: string }>).filter(c => c.kind === 'new' && c.engine === 'claude').map(c => c.model))].sort()
    }
    const ended = (n: number) => Array.from({ length: n }, (_, i) => ({ id: `as-${i}`, agentId: `gone-${i}`, status: 'stopped', result: 'Review failed.' }))
    expect(await offered({ ok: true, value: { decisions: [], attempts: ended(1) } })).toEqual(['sonnet'])
    expect(await offered({ ok: true, value: { decisions: [], attempts: ended(2) } })).toEqual(['opus'])
    expect(await offered({ error: 'BACKEND_UNAVAILABLE' })).toEqual(['opus', 'sonnet'])
  })
  it('records again in the shape an older backend knows when it refuses the new fields as invalid', async () => {
    const s = scoped(), base = s.getMockImplementation()!
    s.mockImplementation(async payload => payload.action === 'command' && JSON.stringify(payload.command).includes('"fit"')
      ? { error: 'VALIDATION_ERROR', detail: 'decision.candidates.0: Unrecognized key: "fit"', status: 400 } : base(payload))
    expect(await routeTask(input, s, where as never, opener(), () => 'op')).toMatchObject({ ok: true, routeDecisionId: 'd1' })
    // With the pick (task #3576), then without the candidates' quota (task #3163), then with c0 standing in for one, then in the
    // shape from before phase 1.
    const [current, unquoted, phase1, v1] = s.mock.calls.filter(c => c[0].action === 'command').map(c => c[0])
    expect(current.command.decision).toHaveProperty('pick')
    expect(unquoted.command.decision).toHaveProperty('pick')
    expect(phase1.command.decision).toMatchObject({ pick: expect.stringMatching(/^c\d+$/), assessment: { size: 'medium' } })
    expect(phase1.command.decision.candidates[0]).toHaveProperty('fit')
    expect(v1.operationId).toBe(phase1.operationId)
    expect(v1.command.decision).toMatchObject({ pick: expect.stringMatching(/^c\d+$/), via: 'order' })
    expect(v1.command.decision.assessment).not.toHaveProperty('size')
    expect(v1.command.decision.candidates.some((c: object) => 'fit' in c)).toBe(false)
  })
  it('records the job kind, leaving only it out for a backend from before kinds, and phase 1 fields only after a second refusal', async () => {
    const kinded = { ...input, assessment: { ...input.assessment, kind: 'docs' } }
    const commands = (s: ReturnType<typeof scoped>) => s.mock.calls.filter(c => c[0].action === 'command').map(c => c[0].command.decision.assessment)
    const current = scoped()
    await routeTask(kinded, current, where as never, opener(), () => 'op')
    expect(commands(current)).toEqual([{ difficulty: 'medium', risk: 'low', why: 'Endpoint.', size: 'medium', kind: 'docs' }])
    const refusing = (keys: string[]) => {
      const s = scoped(), base = s.getMockImplementation()!
      s.mockImplementation(async payload => {
        const sent = payload.action === 'command' ? JSON.stringify(payload.command) : ''
        return keys.some(key => sent.includes(`"${key}"`)) ? { error: 'VALIDATION_ERROR', detail: `Unrecognized key: "${keys[0]}"`, status: 400 } : base(payload)
      })
      return s
    }
    const beforeKinds = refusing(['kind":"docs'])
    expect(await routeTask(kinded, beforeKinds, where as never, opener(), () => 'op')).toMatchObject({ ok: true, routeDecisionId: 'd1' })
    const kind = ['difficulty', 'risk', 'why', 'size', 'kind']
    // Each refusal of the record with no pick tries it without the candidates' quota (task #3163), then with c0 as the pick, the
    // shape a backend from before task #2901 takes.
    expect(commands(beforeKinds).map(a => Object.keys(a))).toEqual([kind, kind, kind, ['difficulty', 'risk', 'why', 'size']])
    const beforePhase1 = refusing(['kind":"docs', 'size'])
    expect(await routeTask(kinded, beforePhase1, where as never, opener(), () => 'op')).toMatchObject({ ok: true, routeDecisionId: 'd1' })
    expect(commands(beforePhase1).map(a => Object.keys(a))).toEqual([kind, kind, kind, ['difficulty', 'risk', 'why', 'size'], ['difficulty', 'risk', 'why']])
    // A task with no kind never needs the extra try.
    const plain = refusing(['size'])
    await routeTask(input, plain, where as never, opener(), () => 'op')
    expect(commands(plain)).toHaveLength(4)
  })
  it('still routes when recording fails or the connection is lost', async () => {
    const result = await routeTask(input, scoped({ record: { error: 'BACKEND_UNAVAILABLE' } }), where as never, opener())
    expect(result).toMatchObject({ ok: true, via: 'order', unrecorded: true })
    expect(result).not.toHaveProperty('routeDecisionId')
    const lost = await routeTask(input, scoped({ recordThrows: true }), where as never, opener())
    expect(lost).toMatchObject({ ok: true, via: 'order', unrecorded: true })
  })
  it('says when one candidate is the only one that can run the task', async () => {
    const s = scoped()
    // Only the member's machine answers, and its engine is not installed for a new agent.
    const open = vi.fn(async (_w: unknown, id: string) => id === 'm1'
      ? daemon(id, { agents: ['h1'], engines: [{ engine: 'claude', installed: false }] }) as never : Promise.reject(new Error('gone')))
    expect(await routeTask(input, s, where as never, open as never)).toMatchObject({ ok: true, via: 'only', candidates: expect.arrayContaining([expect.objectContaining({ agentId: 'a1' })]) })
  })
  it('refuses an invalid assessment, an unknown group and a failed context read', async () => {
    const open = vi.fn()
    expect(await routeTask({ ...input, assessment: { difficulty: 'x' } }, scoped(), where as never, open as never)).toMatchObject({ error: 'INVALID_REQUEST' })
    expect(await routeTask({ ...input, conversationId: 'nope' }, scoped(), where as never, open as never)).toMatchObject({ error: 'NOT_FOUND' })
    const failing = vi.fn(async () => ({ error: 'AGENT_UNAVAILABLE', detail: 'x' }))
    expect(await routeTask(input, failing, where as never, open as never)).toMatchObject({ error: 'AGENT_UNAVAILABLE' })
    const refused = vi.fn(async () => { throw new Error('socket closed') })
    expect(await routeTask(input, refused, where as never, open as never)).toEqual({ error: 'AGENT_UNAVAILABLE', detail: 'socket closed' })
    expect(await routeTask(input, vi.fn(async () => { throw 'odd' }), where as never, open as never)).toMatchObject({ error: 'AGENT_UNAVAILABLE' })
    expect(await routeTask(input, scoped({ context: { ok: false } }), where as never, open as never)).toMatchObject({ ok: false })
    expect(open).not.toHaveBeenCalled()
  })
  it('tolerates a tab without agents, conversations or machines, and names a machine only the members know', async () => {
    const bare = { ok: true, value: { tab: { state: {} }, policy: { policy } } }
    expect(await routeTask(input, scoped({ context: bare }), where as never, vi.fn() as never)).toMatchObject({ error: 'NOT_FOUND' })
    const s = scoped({ context: { ok: true, value: { agentId: 'lead', tab: tab(), policy: { policy } } } })
    const result = await routeTask(input, s, where as never, opener())
    expect(result).toMatchObject({ ok: true, warnings: [] })
    expect((recorded(s).command.decision.candidates as Array<{ title: string }>).some(c => c.title.endsWith('· m1'))).toBe(true)
  })
  it('excludes a member whose terminal is gone or not running, and survives a failed agent list', async () => {
    const s = scoped()
    await routeTask(input, s, where as never, opener(id => daemon(id, { agents: ['h1'], terminalAvailable: false })))
    expect(recorded(s).command.decision.excluded).toEqual(expect.arrayContaining([{ title: 'Multiply · Claude sonnet medium · remote-1', reason: 'Its terminal is not running.' }]))
    const t = scoped()
    await routeTask(input, t, where as never, opener(id => daemon(id, { listFails: true })))
    expect(recorded(t).command.decision.excluded).toEqual(expect.arrayContaining([expect.objectContaining({ reason: 'Its terminal is not running.' })]))
  })
  it('keeps a group without brief or checks routable', async () => {
    const odd = tab(); const group = odd.state.conversations[1] as Record<string, unknown>
    delete group.brief; group.title = undefined; group.checks = [{}]
    const s = scoped({ tab: odd })
    expect(await routeTask(input, s, where as never, opener())).toMatchObject({ ok: true })
    delete group.checks
    expect(await routeTask(input, s, where as never, opener())).toMatchObject({ ok: true })
  })
  it('turns the task needs and the real admission refusals into member and new-agent exclusions', async () => {
    const needs = { tools: ['git'], devices: ['/dev/ttyUSB0'], cwd: '/repo' }
    const s = scoped()
    const open = opener(id => daemon(id, { agents: id === 'm1' ? ['h1'] : [], tools: { git: false }, devices: {} }))
    const result = await routeTask({ ...input, assessment: { ...input.assessment, needs } }, s, where as never, open)
    expect(result).toMatchObject({ error: 'NO_CANDIDATE' })
    const reasons = (result.excluded as Array<{ reason: string }>).map(e => e.reason)
    expect(reasons).toContain('Required tool missing: git. Required device unavailable: /dev/ttyUSB0.')
    const d = open.mock.results[0] && await open.mock.results[0].value as ReturnType<typeof daemon>
    expect(d.request).toHaveBeenCalledWith('super_tabs', expect.objectContaining({ action: 'capacity', cwd: '/repo', tools: ['git'], devices: ['/dev/ttyUSB0'] }), 20_000)
  })
  // Project instructions, phase 4: quota is information, not a gate.
  it('offers a member on a used-up account with a note, and one whose quota is merely unknown without one', async () => {
    const exhausted = { providers: [{ provider: 'claude', outcome: 'answered', httpStatus: 200, body: { five_hour: { utilization: 100 } } }] }
    const line = (s: ReturnType<typeof scoped>) => (recorded(s).command.decision.candidates as Array<{ title: string; label: string }>).find(c => c.title.startsWith('Multiply'))?.label
    const s = scoped()
    await routeTask(input, s, where as never, opener(id => daemon(id, { agents: id === 'm1' ? ['h1'] : [], usage: id === 'm1' ? exhausted : usage })))
    expect(line(s)).toContain(USED_UP_WINDOW)
    const t = scoped()
    await routeTask(input, t, where as never, opener(id => daemon(id, { agents: id === 'm1' ? ['h1'] : [], usage: { providers: [] } })))
    expect(line(t)).toBeDefined()
    expect(line(t)).not.toContain(USED_UP_WINDOW)
  })
  it('notes a member whose screen showed a usage limit until its reset time, and not after', async () => {
    const limited = (until: string | null) => { const t = tab(); t.state.agents = agents.map(a => a.id === 'a1' ? { ...a, limitedUntil: until } : a); return t }
    const line = (s: ReturnType<typeof scoped>) => (recorded(s).command.decision.candidates as Array<{ title: string; label: string }>).find(c => c.title.startsWith('Multiply'))?.label
    const s = scoped({ tab: limited(new Date(Date.now() + 30 * 60_000).toISOString()) })
    await routeTask(input, s, where as never, opener())
    expect(line(s)).toContain('usage limit')
    for (const until of [new Date(Date.now() - 60_000).toISOString(), null]) {
      const t = scoped({ tab: limited(until) })
      await routeTask(input, t, where as never, opener())
      expect(line(t)).not.toContain('usage limit')
    }
  })
  it('uses the real admission texts the member refusal filter keys on', () => {
    const quota = quotaOf({ providers: [{ provider: 'claude', outcome: 'answered', httpStatus: 200, body: { five_hour: { utilization: 100 } } }] }, 'claude', false)
    const requirements = { memoryBytes: 0, diskBytes: 0, maxCpuPercent: 100, tools: ['git'], devices: ['/dev/x'], quotaRequired: false }
    const { reasons, unknown: unknowns } = admission({ resources: {}, tools: {}, devices: {} }, { installed: true, signedIn: true }, quota, requirements)
    expect(unknowns).toContain(USED_UP_WINDOW)
    expect(reasons).toEqual([
      'Required tool not verified: git. Ask the machine owner to check it and refresh the machine reading.', 'Required device unavailable: /dev/x.'])
    const unknown = admission({ resources: {}, tools: {}, devices: {} }, { installed: true, signedIn: true }, quotaOf({}, 'claude', false), { ...requirements, tools: [], devices: [] })
    expect(unknown).toMatchObject({ allowed: true, reasons: [] })
  })
})

// Ken, 2026-11-04 (task #2901): PMs took the route's pick, nearly always Opus or Astra, against the project's Routing
// instructions. The answer lists the candidates by machine with every fact, names none, and says how to start the chosen one.
// Anh Dee, 2026-11-06 (task #3576): rules that can be checked run as code. The route picks by the Routing order (routePick.ts), so
// the pick is first in the answer with its reasons, and the PM follows it or gives a reason for another candidate.
describe('route answers with the pick (task #3576, after #2901)', () => {
  const weekly = { providers: [{ provider: 'claude', outcome: 'answered', httpStatus: 200, account: 'acct', body: {
    five_hour: { utilization: 40, resets_at: new Date(Date.now() + 3 * 3_600_000).toISOString() },
    seven_day: { utilization: 25, resets_at: new Date(Date.now() + 4 * 86_400_000 + 30 * 60_000).toISOString() } } }] }
  const both = () => opener(id => daemon(id, { agents: id === 'm1' ? ['h1'] : [], usage: weekly }))
  type Row = { id: string; kind: string; machineId: string; title: string; quota: string; cwd?: string }

  it('lists every candidate by machine, with the pick first and its reasons, and records the pick', async () => {
    const s = scoped()
    const answer = await routeTask(input, s, where as never, both(), () => 'op')
    expect(answer).toMatchObject({ ok: true, routeDecisionId: 'd1', via: 'order', p: null, pick: { id: expect.stringMatching(/^c\d+$/), title: expect.any(String) },
      reasons: [expect.stringMatching(/^1\. Need:/), expect.stringMatching(/^2\. Quota:/), expect.stringMatching(/^3\. Idle:/), expect.stringMatching(/^4\. Load and price:/)] })
    expect(Object.keys(answer).slice(0, 4)).toEqual(['ok', 'routeDecisionId', 'pick', 'reasons'])
    const rows = answer.candidates as Row[]
    // The candidates stay in the neutral order, mac (m2) before remote-1 (m1), whatever is stronger or cheaper.
    const machines = rows.map(r => r.title.split(' · ').at(-1)!)
    expect(machines).toEqual([...machines].sort((x, y) => x.localeCompare(y)))
    expect(new Set(machines)).toEqual(new Set(['mac', 'remote-1']))
    expect(rows[0]!.machineId).toBe('m2')
    const decision = recorded(s).command.decision
    expect(decision).toMatchObject({ via: 'order', pick: (answer.pick as { id: string }).id, why: answer.why })
    expect(rows.some(r => r.id === (answer.pick as { id: string }).id)).toBe(true)
  })
  it('carries each candidate\'s weekly quota left and its reset, and a new agent\'s folder on its machine', async () => {
    const answer = await routeTask(input, scoped(), where as never, both(), () => 'op')
    const rows = answer.candidates as Row[]
    expect(rows.length).toBeGreaterThan(1)
    for (const row of rows) expect(row.quota).toMatch(/^fits: weekly 75% left, resets in 4d(1h)?$/)
    for (const row of rows.filter(r => r.kind === 'new')) expect(row.cwd).toBe('/absolute/repo/on/that/machine')
  })
  it('says how to start or assign the pick, and that another candidate needs a reason', async () => {
    const answer = await routeTask(input, scoped(), where as never, both(), () => 'op')
    const next = String(answer.next)
    expect(next).toContain('"type":"work.assign","conversationId":"g1","agentId":"AGENT","routeDecisionId":"d1"}')
    const hint = JSON.parse(/start OPERATION_ID (\{.*?\}), then/.exec(next)![1]!)
    expect(hint).toMatchObject({ conversationId: 'g1', machineId: 'MACHINE', engine: 'ENGINE', model: 'MODEL', effort: 'EFFORT', routeDecisionId: 'd1' })
    expect(hint).not.toHaveProperty('overrideReason')
    expect(next).toContain(`the pick (${(answer.pick as { id: string }).id})`)
    expect(next).toMatch(/another candidate.*overrideReason/)
    // The pick applies the project's machine rules from its record (#3964): no line asks the PM to apply them by hand.
    expect(next).not.toMatch(/still apply/)
  })
})

describe('what the route log says about each candidate', () => {
  const git = (patch: Record<string, unknown> = {}) => ({ kind: 'git', name: 'api', cwd: '/w/api', root: '/w/api', remote: 'github.com/acme/api', branch: 'main', ...patch })
  /** The Lead's machine, as the folder reader sees it: the folder is there (unless said otherwise) and the SCM describes it. */
  const describeGit = (found: unknown = git(), isDirectory = true) => ({ describe: vi.fn(async () => found) as unknown as typeof describeScmProject & Mock, isDirectory: vi.fn(async () => isDirectory) })
  const hoursAgo = (h: number) => new Date(Date.now() - h * 3_600_000).toISOString()
  const frame = (patch: Record<string, unknown> = {}) => ({ title: 'Fix auth', updatedAt: hoursAgo(2), project: git({ cwd: '/w/api-wt', root: '/w/api-wt', branch: 'feat/auth' }),
    gitContext: { current: { branch: 'feat/auth' } }, ...patch })
  const withCwd = { ...input, assessment: { ...input.assessment, needs: { cwd: '/w/api' } } }
  /** The context read and the history read of one tab; `extra` sets the tab, the messages and the decisions. */
  const world = (extra: Record<string, unknown> = {}, assignment: unknown = null) => {
    const { tab: given, ...past } = extra
    return { context: { ok: true, value: { agentId: 'lead', tab: given ?? tab(assignment), policy: { policy, warnings: [] }, machines: [{ machineId: 'm1', name: 'remote-1' }, { machineId: 'm2', name: 'mac' }] } },
      history: { ok: true, value: { decisions: [], attempts: [], ...past } } }
  }
  const memberLabel = (s: ReturnType<typeof scoped>) => labels(s).find(l => l.startsWith('Member Multiply'))!
  const newLabel = (s: ReturnType<typeof scoped>) => labels(s).find(l => l.startsWith('New agent'))!

  it('reads the task folder, the member frames and the machines, and labels each candidate with what they say', async () => {
    const s = scoped(world())
    const make = (id: string) => daemon(id, { agents: id === 'm1' ? ['h1', 'x', 'y'] : ['z'], frames: { h1: frame() } })
    const describeRepo = describeGit()
    await routeTask(withCwd, s, where as never, opener(make), () => 'op', describeRepo)
    expect(describeRepo.describe).toHaveBeenCalledWith('/w/api')
    expect(describeRepo.isDirectory).toHaveBeenCalledWith('/w/api')
    expect(memberLabel(s)).toContain('same repo · on "Fix auth" · in api, branch feat/auth · last active 2h ago')
    expect(labels(s).some(l => /^New agent.*remote-1.*has the task folder · 3 agents running · 7\.0 GB memory free/.test(l))).toBe(true)
  })
  it('marks a member in another repository, and one it cannot compare', async () => {
    const other = frame({ project: git({ name: 'web', remote: 'github.com/acme/web', root: '/w/web', cwd: '/w/web' }) })
    const s = scoped(world())
    await routeTask(withCwd, s, where as never, opener(id => daemon(id, { agents: id === 'm1' ? ['h1'] : [], frames: { h1: other } })), () => 'op', describeGit())
    // The task names a folder and the member works in another repository (Diego, 2026-10-30, #2377): it cannot run there, so
    // it is left out with why.
    expect(memberLabel(s)).toBeUndefined()
    expect(recorded(s).command.decision.excluded).toContainEqual(expect.objectContaining({ title: expect.stringContaining('Multiply'), reason: OTHER_REPOSITORY }))
    const t = scoped(world())
    await routeTask(withCwd, t, where as never, opener(id => daemon(id, { agents: id === 'm1' ? ['h1'] : [] })), () => 'op', describeGit())
    expect(memberLabel(t)).toContain('repo unknown · last active unknown')
    const u = scoped(world())
    await routeTask(input, u, where as never, opener(id => daemon(id, { agents: id === 'm1' ? ['h1'] : [], frames: { h1: frame() } })), () => 'op')
    expect(memberLabel(u)).not.toMatch(/repo/)
  })
  it('compares a member on another machine by remote only, and uses the branch the frame\'s project reports', async () => {
    const two = tab(); two.state.agents = [{ ...agents[0], machineId: 'm2' }, agents[1]] as never
    const elsewhere = frame({ gitContext: undefined, project: git({ remote: null, root: '/other/api', cwd: '/other/api', branch: 'from-project' }) })
    const s = scoped(world({ tab: two }))
    await routeTask(withCwd, s, where as never, opener(id => daemon(id, { agents: id === 'm2' ? ['h1'] : [], frames: { h1: elsewhere } })), () => 'op', describeGit({ ...git(), remote: null }))
    expect(memberLabel(s)).toContain('repo unknown')
    expect(memberLabel(s)).toContain('branch from-project')
  })
  it('keeps a frame without a project, a title or a time as what the member does not say', async () => {
    const s = scoped(world())
    await routeTask(withCwd, s, where as never, opener(id => daemon(id, { agents: id === 'm1' ? ['h1'] : [], frames: { h1: { title: 5, updatedAt: 'later', project: null } } })), () => 'op', describeGit({ kind: 'none', cwd: '/w/api', root: null, remote: null, name: 'api', branch: null }))
    expect(memberLabel(s)).toMatch(/repo unknown · last active unknown/)
    expect(memberLabel(s)).not.toMatch(/on "|branch/)
  })
  it('labels a member with its earlier work in this Super Tab, from the state and the log', async () => {
    const decision = (id: string, conversationId: string, patch: Record<string, unknown> = {}) => ({ id, conversationId, title: `Pick ${id}`, via: 'jev', outcome: 'stopped', assignmentId: `as-${id}`,
      createdAt: '2026-10-19T10:00:00Z', final: { kind: 'new', engine: 'claude', model: 'opus' }, ...patch })
    const decisions = [{ ...decision('d7', 'g1', { outcome: null, assignmentId: null, final: null }), via: undefined }, decision('d6', 'g1', { outcome: 'done', assignmentId: 'x', final: { kind: 'member', agentId: 'a1' } }), decision('d5', 'g1'),
      decision('d4', 'other', { final: { kind: 'member', agentId: 'a1' }, outcome: 'done' }), decision('d3', 'g1', { final: null }), decision('d2', 'lead'), decision('d1', 'g1', { outcome: 'superseded' }), decision('d0', 'g1', { final: { kind: 'member', agentId: 'gone' } })]
    const s = scoped(world({ decisions }, { id: 'x', agentId: 'a1', status: 'done', result: 'Shipped.' }))
    await routeTask(input, s, where as never, opener())
    // Multiply's earlier work in this Super Tab: the finished assignment (also in the log), and the log's one in a group this tab no longer has.
    expect(memberLabel(s)).toContain('earlier here: API done; another group done')
  })
  // Review 2 of #3296: the pick's "work that follows on" rule compares the route's own group by id; the label shows its title.
  it('picks the member that worked on this very task before, whatever its account, because the earlier work carries the task\'s id', async () => {
    const decision = { id: 'd6', conversationId: 'g1', title: 'Pick d6', via: 'order', outcome: 'done', assignmentId: 'x', createdAt: '2026-10-19T10:00:00Z',
      final: { kind: 'member', agentId: 'a1' } }
    // At high effort: the fixture's Sonnet at medium is not a candidate the pick may take.
    const t = tab(); t.state.agents = [{ ...agents[0], configuration: { engine: 'claude', model: 'sonnet', effort: 'high' } }, agents[1], agents[2]] as never
    const s = scoped(world({ tab: t, decisions: [decision] }, { id: 'x', agentId: 'a1', status: 'done', result: 'Shipped.' }))
    const answer = await routeTask(input, s, where as never, opener(), () => 'op')
    const pick = (answer.candidates as Array<{ id: string; agentId?: string }>).find(c => c.id === (answer.pick as { id: string }).id)
    expect(pick).toMatchObject({ agentId: 'a1' })
    expect((answer.reasons as string[])[2]).toContain('worked on this task before')
  })
  it('labels a member with the effort it was launched with when it reports none, and keeps an effort it reports', async () => {
    const label = async (effort: string | undefined, start: unknown) => {
      const t = tab(); t.state.agents = [{ ...agents[0], configuration: { engine: 'claude', model: 'sonnet', effort } }, agents[1]] as never
      ;(t.state as Record<string, unknown>).starts = start === undefined ? undefined : [{ harnessId: 'other', request: { effort: 'low' } }, { harnessId: 'h1', request: start }]
      const s = scoped(world({ tab: t }))
      await routeTask(input, s, where as never, opener())
      return memberLabel(s)
    }
    expect(await label('auto', { effort: 'medium' })).toContain('Claude sonnet medium')
    expect(await label(undefined, { effort: 'medium' })).toContain('Claude sonnet medium')
    expect(await label('high', { effort: 'medium' })).toContain('Claude sonnet high')
    expect(await label('auto', undefined)).toContain('Claude sonnet auto')
    expect(await label('auto', { effort: 'default' })).toContain('Claude sonnet auto')
    expect(await label('auto', {})).toContain('Claude sonnet auto')
    // The rating follows the launch effort too: opus rows exist only at high.
    const t = tab(); t.state.agents = [{ ...agents[0], configuration: { engine: 'claude', model: 'opus', effort: 'auto' } }, agents[1]] as never
    ;(t.state as Record<string, unknown>).starts = [{ harnessId: 'h1', request: { effort: 'high' } }]
    const s = scoped(world({ tab: t }))
    await routeTask(input, s, where as never, opener())
    expect(memberLabel(s)).toContain('Claude opus high')
  })
  it('keeps a stopped attempt in the member\'s earlier work after the group was defined again', async () => {
    // `work.define` took the stopped assignment out of the state and no decision of the log names it (a person assigned it).
    // A row without an id names no assignment and is left out.
    const attempts = [{ agentId: 'a1', status: 'done' }, { id: 'as-2', agentId: 'a1', status: 'done', result: 'Shipped.' }, { id: 'as-1', agentId: 'a1', status: 'stopped', result: 'Tests failed.' }]
    const s = scoped(world({ attempts }))
    await routeTask(input, s, where as never, opener())
    expect(memberLabel(s)).toContain('earlier here: API done; API stopped')
  })
  it('orders the member\'s attempts oldest first, a person\'s assignment before a routed one', async () => {
    // as-1 was assigned by a person (no decision); as-2 was routed. The log alone would list as-2 first.
    const decision = { id: 'd2', conversationId: 'g1', title: 'Pick', via: 'jev', outcome: 'done', assignmentId: 'as-2', createdAt: '2026-10-19T11:00:00Z', final: { kind: 'member', agentId: 'a1' } }
    const attempts = [{ id: 'as-2', agentId: 'a1', status: 'done', result: 'Second.', createdAt: '2026-10-19T11:00:00Z' }, { id: 'as-1', agentId: 'a1', status: 'stopped', result: 'First.', createdAt: '2026-10-19T10:00:00Z' }]
    const s = scoped(world({ decisions: [decision], attempts }, { id: 'as-3', agentId: 'a1', status: 'stopped', result: 'Third.' }))
    await routeTask(input, s, where as never, opener())
    expect(memberLabel(s)).toContain('earlier here: API stopped; API done; API stopped')
    // Without the attempts read the log's order stays.
    const old = scoped(world({ decisions: [decision], attempts: undefined, attemptsUnreadable: true }, { id: 'as-3', agentId: 'a1', status: 'stopped', result: 'Third.' }))
    await routeTask(input, old, where as never, opener())
    expect(memberLabel(old)).toContain('earlier here: API stopped; API done')
  })
  it('lists an attempt once when the log and the attempts both know it', async () => {
    const decision = { id: 'd5', conversationId: 'g1', title: 'Pick', via: 'jev', outcome: 'stopped', assignmentId: 'as-1', createdAt: '2026-10-19T10:00:00Z', final: { kind: 'new', engine: 'claude', model: 'opus' } }
    const s = scoped(world({ decisions: [decision], attempts: [{ id: 'as-1', agentId: 'gone', status: 'stopped', result: 'Out of memory.' }] }, { id: 'as-2', agentId: 'a1', status: 'done', result: 'Shipped.' }))
    await routeTask(input, s, where as never, opener())
    expect(memberLabel(s)).toContain('earlier here: API done')
  })
  it('finds nothing to admit on a machine that reports no capacity', async () => {
    // A machine that reports no capacity measured nothing, so nothing there can be admitted; the route says so.
    expect(await routeTask(withCwd, scoped(world()), where as never, opener(id => daemon(id, { agents: id === 'm1' ? ['h1'] : [], capacity: {} })), () => 'op', describeGit())).toMatchObject({ error: 'NO_CANDIDATE' })
  })
  it('lists a member\'s earlier work newest first, three at most', async () => {
    const many = tab(); many.state.conversations = [conversations()[0],
      ...[1, 2, 3, 4].map(i => ({ id: `e${i}`, title: `Group ${i}`, leadId: 'lead', agentIds: [], archived: false, brief: '', checks: [], assignment: { id: `x${i}`, agentId: 'a1', status: 'stopped', result: null } })),
      conversations()[1]] as never
    const s = scoped(world({ tab: many }))
    await routeTask(input, s, where as never, opener(id => daemon(id, { agents: id === 'm1' ? ['h1'] : [] })))
    expect(memberLabel(s)).toContain('earlier here: Group 4 stopped; Group 3 stopped; Group 2 stopped')
    expect(memberLabel(s)).not.toContain('Group 1')
  })
  it('still routes when a source cannot be read', async () => {
    const run = async (extra: Record<string, unknown>, options: Record<string, unknown> = {}, describeRepo: ReturnType<typeof describeGit> = describeGit()) => {
      const s = scoped(world(extra))
      const answer = await routeTask(withCwd, s, where as never, opener(id => daemon(id, { agents: id === 'm1' ? ['h1'] : [], frames: { h1: frame() }, ...options })), () => 'op', describeRepo)
      expect(answer).toMatchObject({ ok: true })
      return s
    }
    expect(memberLabel(await run({}))).toContain('same repo')
    // An older backend (404) and a failed read both leave the attempts and the log out.
    expect(memberLabel(await run({ attempts: undefined, attemptsUnreadable: true }))).toContain('same repo')
    expect(memberLabel(await run({ decisions: undefined, decisionsUnreadable: true }))).toContain('same repo')
    for (const failing of [vi.fn(async () => { throw new Error('git') }), vi.fn(async () => null)]) {
      expect(memberLabel(await run({}, {}, { ...describeGit(), describe: failing as never }))).toContain('repo unknown')
    }
    // A folder read that never answers does not hold the route: it is given up after three seconds.
    vi.useFakeTimers()
    try {
      const pending = run({}, {}, { ...describeGit(), describe: vi.fn(() => new Promise(() => {})) as never })
      await vi.advanceTimersByTimeAsync(3000)
      expect(memberLabel(await pending)).toContain('repo unknown')
    } finally { vi.useRealTimers() }
    // Without the agent list there is no machine load; the members there cannot run, so only new agents are shown.
    expect(newLabel(await run({}, { listFails: true }))).toContain('agents running unknown')
  })
  it('finds a member in the task\'s folder when the Lead\'s machine does not have it', async () => {
    const two = tab(); two.state.agents = [{ ...agents[0], machineId: 'm2' }, agents[1]] as never
    const s = scoped(world({ tab: two }))
    const there = frame({ project: git({ remote: null, root: '/w/api', cwd: '/w/api' }) })
    const reader = describeGit(git(), false)
    await routeTask(withCwd, s, where as never, opener(id => daemon(id, { agents: id === 'm2' ? ['h1'] : [], frames: { h1: there } })), () => 'op', reader)
    expect(memberLabel(s)).toContain('same repo')
    expect(reader.describe).not.toHaveBeenCalled()
  })
  it('says a folder is unknown, not missing, when it cannot be told apart from a refusal', async () => {
    for (const code of ['EACCES', 'ENOTDIR']) {
      const s = scoped(world())
      await routeTask(withCwd, s, where as never, opener(), () => 'op', { describe: vi.fn() as never, isDirectory: async () => { throw Object.assign(new Error(code), { code }) } })
      expect(memberLabel(s)).toContain('repo unknown')
    }
  })
  it('reads the task folder on this machine when no reader is given', async () => {
    const dir = await mkdtemp(join(tmpdir(), 'route-folder-'))
    try {
      await writeFile(join(dir, 'file'), 'x')
      // A member standing in the folder is in it when the folder is there or absent here; a broken path says nothing.
      for (const [cwd, repo] of [[dir, 'same repo'], [join(dir, 'absent'), 'same repo'], [join(dir, 'file', 'inside'), 'repo unknown']] as const) {
        const s = scoped(world())
        const there = frame({ project: { name: 'x', cwd, root: null, remote: null } })
        await routeTask({ ...input, assessment: { ...input.assessment, needs: { cwd } } }, s, where as never, opener(id => daemon(id, { agents: id === 'm1' ? ['h1'] : [], frames: { h1: there } })), () => 'op')
        expect(memberLabel(s)).toContain(repo)
      }
    } finally { await rm(dir, { recursive: true, force: true }) }
  })
  it('starts the probes before the context reads settle, and gives up on a read that hangs', async () => {
    const s = scoped({ ...world(), historyHangs: true })
    const open = opener()
    vi.useFakeTimers()
    try {
      const pending = routeTask(withCwd, s, where as never, open, () => 'op', { describe: vi.fn(() => new Promise(() => {})) as never, isDirectory: async () => true })
      await vi.advanceTimersByTimeAsync(0)
      // Both context reads are still waiting, and the machines have already been opened.
      expect(s.mock.calls.some(c => c[0].action === 'routeHistory')).toBe(true)
      expect(open).toHaveBeenCalled()
      await vi.advanceTimersByTimeAsync(3000)
      expect(await pending).toMatchObject({ ok: true })
    } finally { vi.useRealTimers() }
  })
  it('lists a member whose machine cannot be opened as one whose activity is unknown', async () => {
    const two = tab(); two.state.agents = [...agents, { id: 'a2', name: 'Collab', machineId: 'theirs', harnessId: 'h2', configuration: { engine: 'claude', model: 'sonnet', effort: 'medium' } }] as never
    const s = scoped(world({ tab: two }))
    const open = vi.fn(async (_w: unknown, id: string) => id === 'theirs' ? Promise.reject(new Error('not yours')) : daemon(id, { agents: id === 'm1' ? ['h1'] : [], frames: { h1: frame() }, usage: { providers: [] } })) as never
    await routeTask(input, s, where as never, open)
    expect(labels(s).find(l => l.startsWith('Member Collab'))).toContain('activity unknown')
  })
})


describe('routing contributed capacity', () => {
  const healthy = () => ({ machineId: 'm1', name: 'Contributed laptop', cwd: '/owner/project', engines: ['claude'], paused: false,
    observation: { receivedAt: Date.now(), resources: { memoryTotalBytes: 32e9, memoryUsedBytes: 2e9, projectDiskFreeBytes: 200e9 },
      tools: { git: true }, runningHarnessIds: ['h1'], quotas: [{ engine: 'claude', known: true, windows: [{ name: '5-hour', usedPercent: 20, resetsAt: Date.now() + 3600000 }] }] } })
  async function runPool(pool: Record<string, any>, members = false, needs: Record<string, unknown> = {}, project?: Record<string, unknown>,
    more: { state?: Record<string, unknown>; tab?: Record<string, unknown>; review?: boolean; kind?: string } = {}) {
    const groups = conversations()
    if (project) Object.assign(groups[1]!, { projectId: project.id })
    if (more.review) Object.assign(groups[1]!, { reviewOf: 'g0' })
    // Without members, no agent at all: the idle coordinator is a member candidate too (project instructions, phase 4).
    const crew = members ? agents.map(a => more.review && a.id === 'a1' ? { ...a, role: 'reviewer' } : a) : []
    const s = scoped({ context: { ok: true, value: { agentId: 'lead', tab: { ...more.tab, state: { id: 'tab', agents: crew, conversations: groups, machines: [pool], ...(project ? { projects: [project] } : {}), ...more.state } }, policy: { policy, warnings: [] }, machines: [] } } })
    const open = opener()
    const answer = await routeTask({ ...input, assessment: { ...input.assessment, needs, ...(more.kind ? { kind: more.kind } : {}) } }, s, where, open)
    expect(open).not.toHaveBeenCalled()
    return { answer, s }
  }
  it('routes using owner measurements and the contributed project without opening that machine', async () => {
    const { answer } = await runPool(healthy())
    expect(answer.ok).toBe(true)
    expect(JSON.stringify(answer.candidates)).toContain('"cwd":"/owner/project"')
    const member = await runPool(healthy(), true)
    expect(member.answer.ok).toBe(true)
  })
  // Scholes's goals, piece 5 (machineFit.ts): heavy work is judged by the machine's reading, for its members as for new agents.
  // 2026-11-03: 9 of 25 failed starts in 10 hours were refused by their host for memory or disk set aside for agents still
  // starting (cli superTabs/reservations.ts), which the route did not count, so it kept offering those machines.
  const GiB = 2 ** 30
  const starting = (now: number) => {
    const req = (memoryBytes: number, diskBytes: number) => ({ machineId: 'm1', requirements: { memoryBytes, diskBytes } })
    return [
      // Being created: it holds what it asked for.
      { id: 's1', request: req(2 * GiB, 1 * GiB), requestedAt: now - 60_000, error: null },
      // Launched a minute ago: still in its start window.
      { id: 's2', request: req(2 * GiB, 1 * GiB), requestedAt: now - 120_000, harnessId: 'h9', dispatchedAt: now - 60_000, error: null },
      // Past the deadline of a start, failed, launched long ago, or on another machine: nothing held.
      { id: 's3', request: req(8 * GiB, 8 * GiB), requestedAt: now - 30 * 60_000, error: null },
      { id: 's4', request: req(8 * GiB, 8 * GiB), requestedAt: now - 60_000, error: 'Cloning took too long.' },
      { id: 's5', request: req(8 * GiB, 8 * GiB), requestedAt: now - 20 * 60_000, harnessId: 'h8', dispatchedAt: now - 10 * 60_000, error: null },
      { id: 's6', request: { ...req(8 * GiB, 8 * GiB), machineId: 'm2' }, requestedAt: now - 60_000, error: null },
      // Waited 20 minutes in the Swarm, handed to its host a minute ago: the host reserved it then (review).
      { id: 's7', request: req(1 * GiB, 1 * GiB), requestedAt: now - 20 * 60_000, dispatchedAt: now - 60_000, error: null },
    ]
  }
  it('counts what starts still starting on a machine hold of its memory and disk, as its host does', () => {
    const now = Date.now()
    expect(heldByStarts(starting(now), 'm1', now)).toEqual({ memoryBytes: 5 * GiB, diskBytes: 3 * GiB, heavyMemoryBytes: 0, heavyDiskBytes: 0 })
    expect(heldByStarts([], 'm1', now)).toEqual({ memoryBytes: 0, diskBytes: 0, heavyMemoryBytes: 0, heavyDiskBytes: 0 })
    // A heavy start's hold is told apart: the machine fit counts heavy work placed there itself (machineFit.ts placedHeavyWork).
    const heavy = { id: 'h', request: { machineId: 'm1', heavy: ['build'], requirements: { memoryBytes: 4 * GiB, diskBytes: 15 * GiB } }, requestedAt: now - 60_000, error: null }
    expect(heldByStarts([heavy], 'm1', now)).toEqual({ memoryBytes: 4 * GiB, diskBytes: 15 * GiB, heavyMemoryBytes: 4 * GiB, heavyDiskBytes: 15 * GiB })
  })
  it('leaves out new agents on a machine whose free memory is set aside for agents still starting', async () => {
    const pool = healthy(); Object.assign(pool.observation.resources, { memoryUsedBytes: 26e9 })
    expect((await runPool(pool)).answer.ok).toBe(true)
    const { answer } = await runPool(pool, false, {}, undefined, { state: { starts: starting(Date.now()) } })
    expect(answer.ok).not.toBe(true)
    expect(JSON.stringify(answer)).toContain('Insufficient measured free memory, with 5.4 GB set aside for agents still starting.')
  })
  it('counts a heavy start once for heavy work: the machine fit holds it, the route\'s reading does not hold it again', async () => {
    const now = Date.now(), pool = healthy(); Object.assign(pool.observation.resources, { projectDiskFreeBytes: 70e9 })
    const heavy = { id: 'h', request: { machineId: 'm1', heavy: ['build'], requirements: { memoryBytes: 4 * GiB, diskBytes: 15 * GiB } }, requestedAt: now - 60_000, error: null }
    const { answer } = await runPool(pool, false, { heavy: ['build'] }, undefined, { state: { starts: [heavy] } })
    expect(JSON.stringify(answer.candidates)).not.toContain('Machine fit')
  })
  it('says on each row what its machine has: free slots, free memory less what starts hold, and free disk', async () => {
    const pool = { ...healthy(), availableSlots: 3 }
    const { answer } = await runPool(pool, false, {}, undefined, { state: { starts: starting(Date.now()) } })
    expect((firstNew(answer) as { machine: string }).machine).toBe('3 slots free · 24.6 GB memory free (5.4 GB held by starts) · 196.8 GB disk free')
    const plain = await runPool({ ...healthy(), availableSlots: 1 })
    expect((firstNew(plain.answer) as { machine: string }).machine).toBe('1 slot free · 30.0 GB memory free · 200.0 GB disk free')
  })
  it('judges heavy work by the contributed machine\'s free memory, disk and CPU, for members too, and its start says it is heavy', async () => {
    const pool = healthy(); Object.assign(pool.observation.resources, { memoryUsedBytes: 26e9, cpuPercent: 92 })
    const { answer } = await runPool(pool, true, { heavy: ['build'] })
    const short = 'Machine fit: Short of free memory for a heavy build: it needs 8 GB memory and 30 GB disk free, so prefer an equally strong candidate that has it. Its CPU is busy.'
    const shown = answer.candidates as Array<{ kind: string; notes?: string[] }>
    expect(new Set(shown.map(c => c.kind))).toEqual(new Set(['member', 'new']))
    for (const c of shown) expect(c.notes).toContain(short)
    expect((await runPool(healthy(), false, { heavy: ['build'] })).answer.next).toContain('"requirements":{"memoryBytes":4000000000,"diskBytes":15000000000')
    expect((await runPool(healthy(), false, { heavy: ['build'] })).answer.next).toContain('"heavy":["build"]')
    expect((await runPool(healthy(), false)).answer.next).not.toContain('heavy')
    // A reading with no CPU says nothing of it, and a machine that has what it needs says nothing at all.
    const roomy = await runPool(healthy(), true, { heavy: ['build'] })
    expect(JSON.stringify(roomy.answer.candidates)).not.toContain('Machine fit')
  })
  // Review of #2426 (privacy): every reader of the route log reads the reasons, also for a machine the route shows nothing else
  // of. A paused machine offers no new agent, so its reading was never shown; its member's reason must not show it either.
  it('never shows a paused machine\'s measured memory, disk or CPU in its member\'s reason, the answer or the record', async () => {
    const pool = { ...healthy(), paused: true }; Object.assign(pool.observation.resources, { memoryUsedBytes: 26e9, cpuPercent: 92 })
    const { answer, s } = await runPool(pool, true, { heavy: ['build'] })
    expect(answer).toMatchObject({ ok: true, candidates: expect.arrayContaining([expect.objectContaining({ kind: 'member', notes: expect.arrayContaining([
      'Machine fit: Short of free memory for a heavy build: it needs 8 GB memory and 30 GB disk free, so prefer an equally strong candidate that has it. Its CPU is busy.']) })]) })
    for (const text of [JSON.stringify(answer), JSON.stringify(recorded(s).command.decision)]) {
      for (const measured of ['6.0 GB', '200.0 GB', '92%', '6 GB', '200 GB']) expect(text).not.toContain(measured)
    }
  })
  it('reads an old OpenCode contribution as its installed Claude observation', async () => {
    const pool = healthy()
    pool.engines = ['opencode']
    const { answer, s } = await runPool(pool)
    expect(answer).toMatchObject({ ok: true, candidates: expect.arrayContaining([expect.objectContaining({ engine: 'claude' })]) })
    expect(recorded(s).command.decision.candidates.every((c: { engine: string }) => c.engine === 'claude')).toBe(true)
  })
  it('keeps a healthy contributed member pickable', async () => {
    const { answer, s } = await runPool(healthy(), true)
    expect(answer.ok).toBe(true)
    expect(recorded(s).command.decision.candidates).toEqual(expect.arrayContaining([expect.objectContaining({ title: expect.stringContaining('Multiply') })]))
  })
  // Project instructions, phase 4: quota is information, not a gate.
  it('offers a contributed member and new agents on a used-up account, each with a note', async () => {
    const pool = healthy(); pool.observation.quotas[0]!.windows[0]!.usedPercent = 100
    const { answer, s } = await runPool(pool, true)
    expect(answer).toMatchObject({ ok: true })
    const rows = recorded(s).command.decision.candidates as Array<{ title: string; label: string }>
    expect(rows.map(r => r.title)).toEqual(expect.arrayContaining([expect.stringContaining('Multiply'), expect.stringContaining('New agent')]))
    expect(rows.every(r => r.label.includes(USED_UP_WINDOW))).toBe(true)
  })
  it('withholds a contributed member when a required tool was reported missing', async () => {
    const pool = healthy(); pool.observation.tools.git = false
    const { answer } = await runPool(pool, true)
    expect(answer).toMatchObject({ error: 'NO_CANDIDATE' })
    expect(answer.excluded).toEqual(expect.arrayContaining([expect.objectContaining({ title: expect.stringContaining('Multiply'),
      reason: expect.stringContaining('Required tool missing: git.') })]))
  })
  it('explains a contributed member tool probe that did not complete', async () => {
    const pool = healthy()
    delete (pool.observation.tools as Record<string, unknown>).git
    Object.assign(pool.observation, { toolProbeErrors: { git: 'interactive shell timed out' } })
    const { answer } = await runPool(pool, true)
    expect(answer).toMatchObject({ error: 'NO_CANDIDATE' })
    expect(answer.excluded).toEqual(expect.arrayContaining([expect.objectContaining({ title: expect.stringContaining('Multiply'),
      reason: expect.stringMatching(/Required tool not verified: git.*interactive shell timed out.*check it/) })]))
  })
  it('withholds a contributed member absent from an explicit running-terminal list', async () => {
    const pool = healthy(); pool.observation.runningHarnessIds = []
    const { answer, s } = await runPool(pool, true)
    expect(answer).toMatchObject({ ok: true, candidates: expect.arrayContaining([expect.objectContaining({ kind: 'new' })]) })
    expect(recorded(s).command.decision.excluded).toEqual(expect.arrayContaining([expect.objectContaining({ title: expect.stringContaining('Multiply'),
      reason: 'Its terminal is not running.' })]))
  })
  // Task #2744: the idle cleanup closed Tern, and the PM assigned it anyway, over the route's pick; the reason says what to do.
  // Only for one its idle cleanup closed: one listed stopped alone may be a Change agent before its rebind (review of #2953).
  it('says a contributed member its idle cleanup closed was closed, and to start a new agent', async () => {
    for (const [closed, reason] of [[['h1'], 'Its session was closed. Start a new agent.'], [[], 'Its terminal is not running.']] as const) {
      const pool = healthy(); pool.observation.runningHarnessIds = []
      Object.assign(pool.observation, { stoppedHarnessIds: ['h1'], closedHarnessIds: closed })
      const { answer, s } = await runPool(pool, true)
      expect(answer).toMatchObject({ ok: true, candidates: expect.arrayContaining([expect.objectContaining({ kind: 'new' })]) })
      expect(recorded(s).command.decision.excluded).toEqual(expect.arrayContaining([expect.objectContaining({ title: expect.stringContaining('Multiply'), reason })]))
    }
  })
  it('keeps a contributed member with no running-terminal list pickable and warns that its terminal is unverified', async () => {
    const pool = healthy(); delete (pool.observation as Partial<typeof pool.observation>).runningHarnessIds
    const { answer, s } = await runPool(pool, true)
    expect(answer.ok).toBe(true)
    expect(recorded(s).command.decision.candidates).toEqual(expect.arrayContaining([expect.objectContaining({ title: expect.stringContaining('Multiply') })]))
    expect(answer.warnings).toEqual(expect.arrayContaining([expect.stringContaining('terminal could not be verified')]))
  })
  it('withholds a contributed member when a required device was not reported', async () => {
    const { answer } = await runPool(healthy(), true, { devices: ['/dev/ttyUSB0'] })
    expect(answer).toMatchObject({ error: 'NO_CANDIDATE' })
    expect(answer.excluded).toEqual(expect.arrayContaining([expect.objectContaining({ title: expect.stringContaining('Multiply'),
      reason: expect.stringContaining('Required device unavailable: /dev/ttyUSB0.') })]))
  })
  it('routes each swarm project to its own checkout and never widens an old folder grant', async () => {
    const project = { id: 'project-two', repository: { machineId: 'other', cwd: '/elsewhere', repositoryUrl: 'https://github.com/example/two' } }
    expect((await runPool(healthy(), false, {}, project)).answer.ok).not.toBe(true)
    const shared = { ...healthy(), scope: 'swarm', cwd: '/swarm-root' }
    expect(JSON.stringify((await runPool(shared, false, {}, project)).answer.candidates)).toContain('"cwd":"/swarm-root/project-two"')
    project.repository.machineId = 'm1'
    expect(JSON.stringify((await runPool(shared, false, {}, project)).answer.candidates)).toContain('"cwd":"/elsewhere"')
    expect((await runPool(shared, false, {}, { id: 'project-two' })).answer.ok).not.toBe(true)
    expect((await runPool(shared)).answer.ok).not.toBe(true)
  })
  // v1 routing (#229): the route sent work to a Windows machine that never started agents, to machines without the
  // project and to machines that could not push. A machine that says it cannot do the work is left out, with why; one
  // that says nothing is not. Since the project instructions (phase 4), a login that cannot push is a note: whether the
  // work pushes is the instructions' to say.
  describe('what a machine says it is ready for', () => {
    const reasons = (answer: Record<string, unknown>) => (answer.excluded as Array<{ title: string; reason: string }>)
    const project = { id: 'p2', ownerId: 'owner', repository: { machineId: 'other', cwd: '/elsewhere', repositoryUrl: 'https://github.com/Example/Two.git' } }
    const swarm = (checkouts?: unknown[], engines?: unknown[]) => {
      const pool: Record<string, any> = { ...healthy(), scope: 'swarm', cwd: '/swarm-root', contributorId: 'owner' }
      pool.observation.readiness = { ...(checkouts ? { checkouts } : {}), ...(engines ? { engines } : {}) }
      return pool
    }
    const checkout = (extra: Record<string, unknown> = {}) => ({ folder: 'p2', repository: 'github.com/example/two', git: true, push: true, ...extra })

    it('never starts a Swarm agent on a contributed machine whose system is on the list, and keeps a member already there', async () => {
      const plan9 = healthy(); Object.assign(plan9.observation.resources, { os: 'Plan 9' })
      await withBlocked('Plan 9', async () => {
        const { answer } = await runPool(plan9)
        expect(answer).toMatchObject({ error: 'NO_CANDIDATE' })
        expect(reasons(answer)[0]!.reason).toContain('Swarm agents do not run on Plan 9 yet')
        expect((await runPool(plan9, true)).answer).toMatchObject({ ok: true, candidates: expect.arrayContaining([expect.objectContaining({ kind: 'member' })]) })
      })
    })

    it('routes on Windows exactly as on any other machine, with or without members there', async () => {
      const windows = healthy(); Object.assign(windows.observation.resources, { os: 'Windows' })
      for (const members of [false, true]) {
        const onWindows = (await runPool(windows, members)).answer
        expect(onWindows.ok).toBe(true)
        expect(onWindows.candidates).toEqual((await runPool(healthy(), members)).answer.candidates)
      }
    })

    it('leaves out an engine the machine says is signed out or not installed, and keeps one whose sign-in is unknown', async () => {
      const out = await runPool(swarm(undefined, [{ engine: 'claude', installed: true, signedIn: false }]), false, {}, project)
      expect(out.answer).toMatchObject({ error: 'NO_CANDIDATE' })
      expect(reasons(out.answer)[0]!.reason).toContain('Claude Code is not signed in to a subscription on this machine.')
      const missing = await runPool(swarm(undefined, [{ engine: 'claude', installed: false, signedIn: null }]), false, {}, project)
      expect(reasons(missing.answer)[0]!.reason).toContain('Claude Code is not installed on this machine.')
      for (const engines of [[{ engine: 'claude', installed: true, signedIn: null }], [{ engine: 'claude', installed: null, signedIn: null }], [{ engine: 'codex', installed: true, signedIn: false }], [{ engine: 'cursor', installed: false, signedIn: null }], []]) {
        expect((await runPool(swarm(undefined, engines), false, {}, project)).answer.ok).toBe(true)
      }
    })

    it('says why an engine\'s allowance is unknown, from the machine\'s reading', async () => {
      const pool = swarm()
      pool.observation.quotas = [{ engine: 'claude', known: false, windows: [] }]
      pool.observation.quotaReasons = { claude: 'Anthropic is limiting how often the allowance can be read.' }
      const { answer, s } = await runPool(pool, false, {}, project)
      expect(answer.ok).toBe(true)
      expect(JSON.stringify(recorded(s).command.decision)).toContain('quota unknown (Anthropic is limiting how often the allowance can be read.)')
      delete pool.observation.quotaReasons
      const older = JSON.stringify(recorded((await runPool(pool, false, {}, project)).s).command.decision)
      expect(older).toContain('quota unknown')
      expect(older).not.toContain('quota unknown (')
    })

    it('leaves out an engine the server says is suspended on the machine, with why, and keeps the others', async () => {
      const pool = swarm()
      pool.suspendedEngines = { claude: 'Claude Code is suspended on this machine: its last 2 starts there failed.' }
      const out = await runPool(pool, false, {}, project)
      expect(out.answer).toMatchObject({ error: 'NO_CANDIDATE' })
      expect(reasons(out.answer)[0]!.reason).toContain('Claude Code is suspended on this machine: its last 2 starts there failed.')
      pool.suspendedEngines = { codex: 'Codex is suspended on this machine: its last 2 starts there failed.' }
      expect((await runPool(pool, false, {}, project)).answer.ok).toBe(true)
      // The machine's own reading says more, and goes first: what its owner can fix there, not the count of failed starts.
      // An engine this CLI does not run, or a reason that is not words, is not read.
      const unsigned = swarm(undefined, [{ engine: 'claude', installed: true, signedIn: false }])
      unsigned.suspendedEngines = { claude: 'Claude Code is suspended on this machine: its last 2 starts there failed.', gemini: 'Suspended.', codex: 2 }
      const both = await runPool(unsigned, false, {}, project)
      expect(both.answer).toMatchObject({ error: 'NO_CANDIDATE' })
      expect(reasons(both.answer)[0]!.reason).toContain('not signed in to a subscription')
      expect(JSON.stringify(reasons(both.answer))).not.toMatch(/suspended|Suspended/)
    })

    it('leaves out all new starts, including grid models, during a machine cooldown', async () => {
      const grid = 'swarm-0123456789ab', pool = swarm()
      pool.machineSuspended = 'This machine is suspended for new work: its last 2 starts timed out. It is offered again after 30 minutes.'
      pool.observation.localModels = [{ grid, model: 'qwen3' }]
      const { answer } = await runPool(pool, false, {}, project, { state: { localModels: { grid, enabled: true } }, kind: 'tests' })
      expect(answer).toMatchObject({ error: 'NO_CANDIDATE' })
      expect(reasons(answer).some(row => row.reason.includes('This machine is suspended'))).toBe(true)
      delete pool.machineSuspended
      expect((await runPool(pool, false, {}, project)).answer.ok).toBe(true)
    })

    it.each([
      ['Could not clone the repository at /private/project.', 'repository'],
      ['The Harness server answered without a GitHub token for this project.', 'GitHub'],
      ['The project folder does not exist: /private/project.', 'folder'],
      ['The repository has no commits.', 'commit'],
      ['Required tool gh is not installed.', 'tool'],
      ["This machine's ChatGPT account (private-account) does not offer gpt-6-astra; choose another model.", 'model'],
      ["This machine held the agent's launch for 3 minutes and stopped it: Waiting to confirm the selected executable’s permission flags.", 'permission'],
    ])('notes setup failures while keeping the engine available: %s', async (error, category) => {
      const { answer, s } = await runPool(swarm(), false, {}, project, { state: { starts: [
        { id: 'failed', requestedAt: Date.now(), error, request: { machineId: 'm1', engine: 'claude', conversationId: 'group' } },
      ] } })
      expect(answer.ok).toBe(true)
      const candidates = answer.candidates as Array<{ notes: string[] }>
      expect(candidates.some(row => (row.notes ?? []).some(note => note.includes(category)))).toBe(true)
      const recordedText = JSON.stringify(recorded(s).command.decision)
      expect(recordedText).not.toContain('/private/project')
      expect(recordedText).not.toContain('private-account')
    })

    it('drops setup notes after a later success or 30 minutes, without noting another engine', async () => {
      const failed = { requestedAt: Date.now(), error: 'The project folder does not exist.', request: { machineId: 'm1', engine: 'claude' } }
      for (const starts of [
        [{ ...failed, requestedAt: Date.now() - 30 * 60_000 }],
        [failed, { ...failed, harnessId: 'created', error: null }],
        [{ ...failed, request: { machineId: 'm1', engine: 'codex' } }],
      ]) {
        const { answer } = await runPool(swarm(), false, {}, project, { state: { starts } })
        expect(answer.ok).toBe(true)
        expect(JSON.stringify(answer.candidates)).not.toContain('Its last start failed while preparing')
      }
    })

    it('still offers a local model on a machine whose engine is signed out, since the grid needs no subscription', async () => {
      const grid = 'swarm-0123456789ab', pool = swarm(undefined, [{ engine: 'claude', installed: true, signedIn: false }])
      pool.observation.localModels = [{ grid, model: 'qwen3' }]
      const { answer, s } = await runPool(pool, false, {}, project, { state: { localModels: { grid, enabled: true } }, kind: 'tests' })
      expect(answer).toMatchObject({ ok: true, candidates: expect.arrayContaining([expect.objectContaining({ gridModel: 'qwen3' })]) })
      expect(recorded(s).command.decision.excluded).toEqual(expect.arrayContaining([expect.objectContaining({ reason: expect.stringContaining('not signed in') })]))
    })

    it('leaves out a project folder that is not a checkout of the project\'s repository', async () => {
      const plain = await runPool(swarm([checkout({ git: false, repository: null, push: null })]), false, {}, project)
      expect(plain.answer).toMatchObject({ error: 'NO_CANDIDATE' })
      expect(reasons(plain.answer)[0]!.reason).toContain('is not a git checkout')
      const other = await runPool(swarm([checkout({ repository: 'github.com/example/other' })]), false, {}, project)
      expect(reasons(other.answer)[0]!.reason).toContain('checkout of another repository')
      // Its own repository under another address, no folder yet (it is cloned on start), or a remote it cannot name.
      for (const checkouts of [[checkout()], [checkout({ folder: 'p9', git: false })], [checkout({ repository: null })]]) {
        expect((await runPool(swarm(checkouts), false, {}, project)).answer.ok).toBe(true)
      }
      expect((await runPool(swarm([checkout({ repository: 'github.com/example/other' })]), false, {}, { ...project, repository: { ...project.repository, repositoryUrl: undefined, machineId: 'm1' } })).answer.ok).toBe(true)
    })

    const lines = (s: ReturnType<typeof scoped>) => recorded(s).command.decision.candidates as Array<{ title: string; label: string }>
    it('notes a machine whose login cannot push, on members and new agents, for any task', async () => {
      const denied = swarm([checkout({ push: false })])
      for (const review of [false, true]) {
        const work = await runPool(denied, true, {}, project, { review })
        expect(work.answer).toMatchObject({ ok: true })
        const rows = lines(work.s).filter(r => !r.title.startsWith('Lead'))
        expect(rows.map(r => r.title)).toEqual(expect.arrayContaining([expect.stringContaining('Multiply'), expect.stringContaining('New agent')]))
        expect(rows.every(r => r.label.includes('cannot push to the project repository'))).toBe(true)
      }
      // An unknown push says nothing.
      expect(JSON.stringify(lines((await runPool(swarm([checkout({ push: null })]), false, {}, project)).s))).not.toContain('cannot push')
    })

    it('ignores the machine\'s own login when a teammate\'s project pushes through the Harness GitHub App', async () => {
      const mates = { ...project, ownerId: 'mate' }
      const noted = async (p: Record<string, unknown>, tab?: Record<string, unknown>) =>
        JSON.stringify(lines((await runPool(swarm([checkout({ push: false })]), false, {}, p, tab ? { tab } : {})).s)).includes('cannot push')
      expect(await noted(mates, { githubApp: true })).toBe(false)
      // The owner's own project still pushes with the owner's login, and without the App so does a teammate's.
      expect(await noted(project, { githubApp: true })).toBe(true)
      expect(await noted(mates)).toBe(true)
    })

    it('notes a machine whose last two starts started nothing, until one does', async () => {
      const start = (extra: Record<string, unknown>) => ({ id: `s${Math.random()}`, request: { machineId: 'm1' }, harnessId: null, dispatched: false, error: null, ...extra })
      const failed = start({ error: 'Claude Code is not signed in.' }), queued = start({}), started = start({ harnessId: 'h9', dispatched: true })
      const stuck = await runPool(healthy(), true, {}, undefined, { state: { starts: [started, failed, queued] } })
      expect(stuck.answer).toMatchObject({ ok: true, candidates: expect.arrayContaining([expect.objectContaining({ kind: 'member' })]) })
      const rows = lines(stuck.s)
      expect(rows.filter(r => r.title.startsWith('New agent')).every(r => r.label.includes('last 2 starts'))).toBe(true)
      expect(rows.find(r => r.title.startsWith('Multiply'))!.label).not.toContain('last 2 starts')
      for (const starts of [[failed, started], [failed], [failed, start({ dispatched: true })], [failed, queued].map(s => ({ ...s, request: { machineId: 'm9' } }))]) {
        const { answer, s } = await runPool(healthy(), false, {}, undefined, { state: { starts } })
        expect(answer.ok).toBe(true)
        expect(JSON.stringify(lines(s))).not.toContain('last 2 starts')
      }
    })
  })

  // 2026-10-27: a task to check a device plugged into one person's Mac went to another machine by policy order. Since the
  // project instructions (phase 4) the requirement is not enforced: every other machine says it is not the required one.
  describe('a task that requires a machine', () => {
    const two = (diego: (p: any) => void = () => {}) => {
      const other = { ...healthy(), machineId: 'm3', name: 'Diego Mac', observation: { ...healthy().observation, runningHarnessIds: [] } }
      diego(other)
      return [healthy(), other]
    }
    const requiring = (requires: Record<string, unknown>) => {
      const groups = conversations()
      Object.assign(groups[1]!, { task: { goal: 'Check the device', changes: 'None', constraints: 'None', owns: ['devices'], requires } })
      return groups
    }
    async function route(machines: unknown[], requires: Record<string, unknown>, members = true) {
      const s = scoped({ context: { ok: true, value: { agentId: 'lead', tab: { state: { id: 'tab', agents: members ? agents : [agents[1]], conversations: requiring(requires), machines } },
        policy: { policy, warnings: [] }, machines: [] } } })
      return { answer: await routeTask(input, s, where, opener()), s }
    }
    const elsewhere = (s: ReturnType<typeof scoped>) => [...new Set((recorded(s).command.decision.candidates as Array<{ machineId: string; label: string }>)
      .filter(c => c.label.includes(ELSEWHERE)).map(c => c.machineId))]
    it('offers every machine, and notes on the others that the requirement names another', async () => {
      const { answer, s } = await route(two(), { machines: ['m3'] })
      expect(answer).toMatchObject({ ok: true })
      const machines = new Set((recorded(s).command.decision.candidates as Array<{ machineId: string }>).map(c => c.machineId))
      expect(machines).toEqual(new Set(['m1', 'm3']))
      expect(elsewhere(s)).toEqual(['m1'])
      expect(JSON.stringify(recorded(s).command.decision.excluded)).not.toContain(ELSEWHERE)
    })
    it('routes elsewhere, with the note, while the required machine has no free slot', async () => {
      const full = await route(two(p => { p.availableSlots = 0 }), { machines: ['m3'] })
      expect(full.answer).toMatchObject({ ok: true, candidates: expect.arrayContaining([expect.objectContaining({ machineId: 'm1', notes: expect.arrayContaining([ELSEWHERE]) })]) })
    })
    it('notes every machine when the required one is one the agent cannot see', async () => {
      const { answer, s } = await route(two(), { machines: [], unseenMachines: 1 })
      expect(answer).toMatchObject({ ok: true })
      expect(elsewhere(s).sort()).toEqual(['m1', 'm3'])
    })
    it('counts a Mac\'s Harness Team account as that Mac', async () => {
      const team = { ...healthy(), machineId: 't3', name: 'Diego Mac', hostMachineId: 'm3', contributorId: 'owner', observation: { ...healthy().observation, runningHarnessIds: [] } }
      const { s } = await route([healthy(), team], { machines: ['m3'] }, false)
      expect(elsewhere(s)).toEqual(['m1'])
    })
    it('keeps routing a task without a requirement as before', async () => {
      const { answer } = await route(two(), undefined as never)
      expect(answer).toMatchObject({ ok: true })
    })
  })
  describe('a task that requires a device', () => {
    const requiring = (starts: unknown[] = []) => {
      const groups = conversations()
      Object.assign(groups[1]!, { task: { goal: 'g', changes: 'c', constraints: 'k', owns: ['o'], requires: { devices: ['/dev/cu.usbmodem1'] } } })
      return { revision: 3, state: { id: 'tab', agents, conversations: groups, starts } }
    }
    it('waits when no machine has the device connected', async () => {
      const answer = await routeTask(input, scoped({ tab: requiring() }), where as never, opener(id => daemon(id, { agents: id === 'm1' ? ['h1'] : [] })), () => 'op')
      expect(answer).toMatchObject({ error: 'NO_CANDIDATE', waiting: true,
        detail: 'This task waits for the machine it requires; route it again once that machine can take it.' })
    })
    it('notes a member whose own start did not have its machine check the device', async () => {
      const connected = opener(id => daemon(id, { agents: id === 'm1' ? ['h1'] : [], devices: { '/dev/cu.usbmodem1': true } }))
      const members = async (starts: unknown[]) => {
        const s = scoped({ tab: requiring(starts) })
        await routeTask(input, s, where as never, connected, () => 'op')
        return (recorded(s).command.decision.candidates as Array<{ title: string; label: string }>).find(c => c.title.startsWith('Multiply'))!.label
      }
      expect(await members([])).toContain('Its start did not have its machine check the device this task requires.')
      const checked = await members([{ harnessId: 'h1', request: { machineId: 'm1', requirements: { devices: ['/dev/cu.usbmodem1'] } } }])
      expect(checked).not.toContain('check the device')
    })
  })
  it('asks the machine for the devices the group\'s task requires, beside those the Lead names', async () => {
    const groups = conversations()
    Object.assign(groups[1]!, { task: { goal: 'g', changes: 'c', constraints: 'k', owns: ['o'], requires: { devices: ['/dev/cu.usbmodem1'] } } })
    const s = scoped({ tab: { revision: 3, state: { id: 'tab', agents, conversations: groups } } })
    const make = opener(id => daemon(id, { devices: { '/dev/cu.usbmodem1': id === 'm2', '/dev/ttyUSB0': true } }))
    const answer = await routeTask({ ...input, assessment: { ...input.assessment, needs: { devices: ['/dev/ttyUSB0'] } } }, s, where as never, make, () => 'op')
    expect(answer).toMatchObject({ ok: true, candidates: expect.arrayContaining([expect.objectContaining({ machineId: 'm2' })]) })
    const hint = JSON.parse(/start OPERATION_ID (\{.*\}), then/.exec(answer.next as string)![1]!)
    expect(hint.requirements.devices).toEqual(['/dev/ttyUSB0', '/dev/cu.usbmodem1'])
  })

  it.each([
    ['stale', (p: any) => { p.observation.receivedAt = 0 }],
    ['paused', (p: any) => { p.paused = true }],
    ['full', (p: any) => { p.availableSlots = 0 }],
    ['low disk', (p: any) => { p.observation.resources.projectDiskFreeBytes = 1 }],
    ['unknown memory', (p: any) => { delete p.observation.resources.memoryUsedBytes }],
    ['no engines', (p: any) => { delete p.engines }],
  ])('withholds new starts when %s', async (_, change) => {
    const p = healthy(); change(p)
    const { answer } = await runPool(p)
    expect(answer.ok).not.toBe(true)
  })
  it('keeps unknown allowance and load distinct from zero', async () => {
    const p = healthy(); delete (p.observation.quotas[0].windows[0] as any).name
    expect((await runPool(p)).answer.ok).toBe(true)
    p.observation.quotas = []
    delete (p.observation as any).runningHarnessIds
    expect((await runPool(p)).answer.ok).toBe(true)
    expect((await runPool({ ...p, observation: {} }, true)).answer.ok).not.toBe(true)
  })
  it('lists API-key machines beside a subscription one, a row for every engine of every machine', async () => {
    const keyed = (machineId: string) => ({ ...healthy(), machineId, name: `Keyed ${machineId}`, observation: { ...healthy().observation, runningHarnessIds: [],
      quotas: [{ engine: 'claude', known: true, windows: [] }] } })
    // The subscription machine has two engines, so its two best rows come before any other machine's.
    const subscription = healthy(), reading = { usedPercent: 20, resetsAt: Date.now() + 5 * 86_400_000 }
    Object.assign(subscription, { engines: ['claude', 'codex'] })
    subscription.observation.quotas = ['claude', 'codex'].map(engine => ({ engine, known: true, windows: [{ ...reading, name: '5-hour' }, { ...reading, name: 'weekly' }] }))
    const enabled = { ...floorPolicy, models: floorPolicy.models.map(m => ({ ...m, enabled: true })) }
    const s = scoped({ context: { ok: true, value: { agentId: 'lead', tab: { state: { id: 'tab', agents: [], conversations: conversations(), machines: [subscription, keyed('m2'), keyed('m3')] } },
      policy: { policy: enabled, warnings: [] }, machines: [] } } })
    const answer = await routeTask(input, s, where, opener())
    const rows = answer.candidates as Array<{ machineId: string; engine: string; quota: string }>
    expect(new Set(rows.map(x => `${x.machineId} ${x.engine}`))).toEqual(new Set(['m1 claude', 'm1 codex', 'm2 claude', 'm3 claude']))
    // Each row says its account's weekly quota, which the project's order picks by (Ken, 2026-11-03).
    for (const row of rows) expect(row.quota).toBe(row.machineId === 'm1' ? 'fits: weekly 80% left, resets in 5d' : 'metered')
  })
  // The coordinator picks by the project's order among accounts, so every account is an alternative, once: a Claude account with
  // 99% left on a machine whose Codex was used up never reached it (Ken, 2026-11-03).
  it('offers a row of every engine on every machine, with its weekly quota', async () => {
    const week = (usedPercent: number) => [{ name: 'weekly', usedPercent, resetsAt: Date.now() + 30 * 3_600_000 }]
    const box = (machineId: string) => Object.assign({ ...healthy(), machineId, name: `Box ${machineId}`, engines: ['claude', 'codex'] }, {
      observation: { ...healthy().observation, runningHarnessIds: [], quotas: [{ engine: 'claude', known: true, windows: week(1) }, { engine: 'codex', known: true, windows: week(100) }] } })
    const enabled = { ...floorPolicy, models: floorPolicy.models.map(m => ({ ...m, enabled: true })) }
    const s = scoped({ context: { ok: true, value: { agentId: 'lead', tab: { state: { id: 'tab', agents: [], conversations: conversations(), machines: ['m1', 'm2', 'm3'].map(box) } },
      policy: { policy: enabled, warnings: [] }, machines: [] } } })
    const answer = await routeTask(input, s, where, opener())
    const rows = answer.candidates as Array<{ machineId: string; engine: string; quota: string }>
    expect(new Set(rows.map(x => `${x.machineId} ${x.engine}`))).toEqual(new Set(['m1 claude', 'm1 codex', 'm2 claude', 'm2 codex', 'm3 claude', 'm3 codex']))
    expect(rows.find(x => x.engine === 'claude')!.quota).toBe('fits: weekly 99% left, resets in 30h')
    expect(rows.find(x => x.engine === 'codex')!.quota).toBe('short: weekly 0% left, resets in 30h')
  })
  // A backend from before 2026-11-03 keeps 12 candidates of a decision: the route records the first 12 and offers only those, as the
  // agent choosing assigns by a recorded candidate.
  it('records and offers the first 12 rows when the backend keeps no more', async () => {
    const week = [{ name: 'weekly', usedPercent: 10, resetsAt: Date.now() + 30 * 3_600_000 }]
    const box = (machineId: string) => Object.assign({ ...healthy(), machineId, name: `Box ${machineId}`, engines: ['claude', 'codex'] }, {
      observation: { ...healthy().observation, runningHarnessIds: [], quotas: [{ engine: 'claude', known: true, windows: week }, { engine: 'codex', known: true, windows: week }] } })
    const enabled = { ...floorPolicy, models: floorPolicy.models.map(m => ({ ...m, enabled: true })) }
    const base = scoped({ context: { ok: true, value: { agentId: 'lead', tab: { state: { id: 'tab', agents: [], conversations: conversations(),
      machines: Array.from({ length: 8 }, (_, i) => box(`m${i}`)) } }, policy: { policy: enabled, warnings: [] }, machines: [] } } })
    const s = vi.fn(async (payload: Record<string, any>) => payload.action === 'command' && payload.command.decision.candidates.length > 12
      ? { error: 'VALIDATION_ERROR', detail: 'command.decision.candidates: Too big' } : base(payload))
    const answer = await routeTask(input, s as never, where, opener())
    expect(answer).toMatchObject({ ok: true, routeDecisionId: 'd1' })
    const sent = s.mock.calls.filter(c => c[0].action === 'command').map(c => c[0].command.decision.candidates.length)
    // With no pick, then without the candidates' quota, then with c0 as the pick (a backend from before task #2901), then the
    // first 12 of those. The task gives no size, so each machine's Claude keeps both Opus and Sonnet (task #3163): 8 machines,
    // 3 rows each.
    expect(sent).toEqual([24, 24, 24, 12])
    const recordedIds = new Set(s.mock.calls.filter(c => c[0].action === 'command').at(-1)![0].command.decision.candidates.map((c: { id: string }) => c.id))
    for (const row of answer.candidates as Array<{ id: string }>) expect(recordedIds.has(row.id)).toBe(true)
  })
  it('leaves out a machine that cannot take a teammate\'s project, with the backend\'s reason, and keeps its owner\'s own work there', async () => {
    const reason = 'Contributed laptop has no Harness Team account yet, so it runs only its owner\'s projects.'
    const refusing = (contributorId: string) => ({ ...healthy(), contributorId, teamWork: { allowed: false, code: 'TEAM_ACCOUNT_REQUIRED', reason } })
    const owners = { id: 'lead', ownerId: 'owner' }
    // The Swarm owner's project on a teammate's personal laptop: no new agent there, and not its member either.
    const teammate = await runPool(refusing('mate'), true, {}, owners)
    expect(teammate.answer).toMatchObject({ error: 'NO_CANDIDATE' })
    expect(teammate.answer.excluded).toEqual(expect.arrayContaining([
      expect.objectContaining({ title: expect.stringContaining('Multiply'), reason }),
      expect.objectContaining({ title: expect.not.stringContaining('Multiply'), reason })]))
    // The same laptop's own owner's project, as before.
    expect((await runPool(refusing('mate'), false, {}, { id: 'lead', ownerId: 'mate' })).answer.ok).toBe(true)
    // A Team machine that takes it, and a backend from before the rule that says nothing.
    expect((await runPool({ ...healthy(), contributorId: 'mate', teamWork: { allowed: true } }, false, {}, owners)).answer.ok).toBe(true)
    expect((await runPool({ ...healthy(), contributorId: 'mate' }, false, {}, owners)).answer.ok).toBe(true)
    // Offline is said once, by the reading's own age.
    const offline = { ...refusing('mate'), teamWork: { allowed: false, code: 'MACHINE_OFFLINE', reason: 'Contributed laptop is offline.' } }
    offline.observation.receivedAt = 0
    const silent = await runPool(offline, false, {}, owners)
    expect(silent.answer.excluded).not.toEqual(expect.arrayContaining([expect.objectContaining({ reason: expect.stringContaining('Contributed laptop is offline.') })]))
  })
  // Task #2806 (River, 11-04): a machine whose owner lets teammates' work run in their own account (backend machines.ts
  // ownAccountWork). The teammate whose task lands there did not choose it, so the route says so, for new agents and members.
  it('says a machine runs a teammate\'s work in its owner\'s own account, and says nothing for the owner\'s own project', async () => {
    const said = 'Contributed laptop runs teammates\' work in Mate\'s own account.'
    const pool = { ...healthy(), contributorId: 'mate', teamWork: { allowed: true, ownAccount: said } }
    const teammate = (await runPool(pool, true, {}, { id: 'lead', ownerId: 'owner' })).answer
    const rows = teammate.candidates as Array<{ title: string; notes?: string[] }>
    expect(rows.length).toBeGreaterThan(1)
    for (const row of rows) expect(row.notes ?? [], row.title).toContain(said)
    const own = (await runPool(pool, true, {}, { id: 'lead', ownerId: 'mate' })).answer
    expect(JSON.stringify(own)).not.toContain(said)
  })
  // Task #2390 (Tony, 11-03): a machine its owner keeps for some projects (backend machines.ts machine.projects) is left out of
  // every other project's route, new agents and members alike, with why. The backend refuses it too.
  it('leaves out a machine its owner keeps for other projects, new agents and members alike, and offers it to those projects', async () => {
    const projects = [{ id: 'lead', ownerId: 'owner', name: 'Pilot', visibility: 'public' }, { id: 'oh', ownerId: 'owner', name: 'openharness', visibility: 'public' },
      { id: 'hidden', ownerId: 'owner', name: 'secret', visibility: 'private' }]
    const kept = (ids: string[]) => ({ ...healthy(), projects: ids })
    const other = await runPool(kept(['oh']), true, {}, undefined, { state: { projects } })
    expect(other.answer).toMatchObject({ error: 'NO_CANDIDATE' })
    expect(other.answer.excluded).toEqual(expect.arrayContaining([
      expect.objectContaining({ title: expect.stringContaining('Multiply'), reason: 'Contributed laptop runs only openharness.' }),
      expect.objectContaining({ title: expect.not.stringContaining('Multiply'), reason: 'Contributed laptop runs only openharness.' })]))
    // A private project's name is never written into another project's route.
    const secret = await runPool(kept(['hidden']), false, {}, undefined, { state: { projects } })
    expect(secret.answer.excluded).toEqual(expect.arrayContaining([
      expect.objectContaining({ reason: 'Contributed laptop is kept by its owner for other projects.' })]))
    expect(JSON.stringify(secret.answer)).not.toContain('secret')
    // Kept for this project, or for none in particular: offered as before.
    expect((await runPool(kept(['oh', 'lead']), true, {}, undefined, { state: { projects } })).answer.ok).toBe(true)
    expect((await runPool(healthy(), true, {}, undefined, { state: { projects } })).answer.ok).toBe(true)
  })
  // One machine (docs/plans/2026-10-24-swarm-team-account.md): a Mac's Harness Team account is that Mac. The Lead is offered
  // the Mac once: a teammate's project starts in the team account, the Mac's owner's own projects stay with the owner.
  describe('a Mac and its Harness Team account', () => {
    const NO_ACCOUNT = 'MacBook Pro has no Harness Team account yet, so it runs only its owner\'s projects.'
    const mac = () => ({ ...healthy(), name: 'MacBook Pro', scope: 'swarm', cwd: '/mate/swarm', contributorId: 'mate',
      teamWork: { allowed: false, code: 'TEAM_ACCOUNT_REQUIRED', reason: NO_ACCOUNT } })
    const team = () => ({ ...healthy(), machineId: 'm1-team', hostMachineId: 'm1', name: 'MacBook Pro', scope: 'swarm', cwd: '/team/swarm', contributorId: 'mate',
      teamWork: { allowed: true } })
    async function route(ownerId: string, machines: Record<string, unknown>[] = [mac(), team()], groups: Record<string, unknown>[] = conversations()) {
      const s = scoped({ context: { ok: true, value: { agentId: 'lead', tab: { state: { id: 'tab', agents: [], conversations: groups, machines,
        projects: [{ id: 'lead', ownerId, repository: { repositoryUrl: 'https://github.com/example/project' } }] } },
        policy: { policy, warnings: [] }, machines: [] } } })
      const answer = await routeTask(input, s, where, opener())
      const decision = s.mock.calls.find(c => c[0].action === 'command')?.[0].command.decision
      return { answer, candidates: (decision?.candidates ?? []) as Array<{ machineId: string; kind: string; title: string }>,
        excluded: (decision?.excluded ?? answer.excluded ?? []) as Array<{ reason: string }> }
    }

    it('starts a teammate\'s project in the team account, under the Mac\'s name, without calling the Mac unfit', async () => {
      const { answer, candidates, excluded } = await route('owner')
      expect(answer).toMatchObject({ ok: true, candidates: expect.arrayContaining([expect.objectContaining({ machineId: 'm1-team' })]) })
      expect(JSON.stringify(answer.candidates)).toContain('"cwd":"/team/swarm/lead"')
      expect(candidates.filter(c => c.kind === 'new').map(c => c.machineId)).toEqual(expect.arrayContaining(['m1-team']))
      expect(candidates.some(c => c.kind === 'new' && c.machineId === 'm1')).toBe(false)
      expect(excluded.some(e => e.reason === NO_ACCOUNT)).toBe(false)
    })

    it('keeps the Mac\'s owner\'s own project with the owner, never the team account', async () => {
      const { answer, candidates } = await route('mate')
      expect(answer).toMatchObject({ ok: true, candidates: expect.arrayContaining([expect.objectContaining({ machineId: 'm1' })]) })
      expect(candidates.some(c => c.machineId === 'm1-team')).toBe(false)
    })

    it('says the team account\'s own reason when it cannot take the work, and still offers nothing as the owner', async () => {
      const reason = 'The Harness Team account on MacBook Pro is not private (gitconfig: it could read its owner\'s ~/.gitconfig), so it runs only its owner\'s projects.'
      const { answer, excluded } = await route('owner', [mac(), { ...team(), teamWork: { allowed: false, code: 'PRIVACY_CHECK_FAILED', reason } }])
      expect(answer).toMatchObject({ error: 'NO_CANDIDATE' })
      expect(excluded.map(e => e.reason)).toContain(reason)
      expect(excluded.some(e => e.reason === NO_ACCOUNT)).toBe(false)
    })

    // Task #3060 (Scholes, 11-05): the Mac's owner turned on own-account work (backend machines.ts ownAccountWork), and a lamp
    // start still went to their Team account. A teammate's work no Team agent wrote is offered on the Mac itself; work a Team
    // agent wrote stays in the Team account.
    describe('when the Mac\'s owner runs teammates\' work in their own account', () => {
      const said = 'MacBook Pro runs teammates\' work in Mate\'s own account.'
      const ownAccountMac = () => ({ ...mac(), teamWork: { allowed: true, ownAccount: said } })
      const newOn = (candidates: Array<{ machineId: string; kind: string }>) => candidates.filter(c => c.kind === 'new').map(c => c.machineId)

      it('offers the Mac\'s own entry for a teammate\'s task, never its Team account entry', async () => {
        const { answer, candidates } = await route('owner', [ownAccountMac(), team()])
        expect(answer).toMatchObject({ ok: true })
        expect(newOn(candidates)).toContain('m1')
        expect(newOn(candidates)).not.toContain('m1-team')
        expect(JSON.stringify(answer.candidates)).not.toContain('"cwd":"/team/swarm/lead"')
      })

      it('still offers the Team account entry, and not the Mac, for a task a Team agent wrote', async () => {
        const written = conversations().map(c => c.id === 'g1' ? { ...c, teamWritten: true } : c)
        const { candidates } = await route('owner', [ownAccountMac(), team()], written)
        expect(newOn(candidates)).toContain('m1-team')
        expect(newOn(candidates)).not.toContain('m1')
      })

      it('reads a step of a Team-written task as Team-written too', async () => {
        const groups = [...conversations().map(c => c.id === 'g1' ? { ...c, id: 'task', teamWritten: true } : c),
          { id: 'g1', title: 'Review', leadId: 'lead', agentIds: ['lead'], archived: false, brief: 'Review it.', checks: [], assignment: null, reviewOf: 'task' }]
        const { candidates } = await route('owner', [ownAccountMac(), team()], groups)
        expect(newOn(candidates)).toContain('m1-team')
        expect(newOn(candidates)).not.toContain('m1')
      })

      it('reads a task from before the mark, assigned to an agent in a Team account, as Team-written, as the backend does', async () => {
        const assigned = conversations({ agentId: 'tw', status: 'working' })
        const s = scoped({ context: { ok: true, value: { agentId: 'lead', tab: { state: { id: 'tab', conversations: assigned, machines: [ownAccountMac(), team()],
          agents: [{ id: 'tw', name: 'Writer', machineId: 'm9', harnessId: 'htw', teamAccount: true, configuration: { engine: 'claude', model: 'opus', effort: 'high' } }],
          projects: [{ id: 'lead', ownerId: 'owner', repository: { repositoryUrl: 'https://github.com/example/project' } }] } },
          policy: { policy, warnings: [] }, machines: [] } } })
        await routeTask(input, s, where, opener())
        const candidates = (recorded(s).command.decision.candidates ?? []) as Array<{ machineId: string; kind: string }>
        expect(newOn(candidates)).toContain('m1-team')
        expect(newOn(candidates)).not.toContain('m1')
      })

      it('keeps the Mac\'s owner\'s own project on the Mac', async () => {
        const { candidates } = await route('mate', [ownAccountMac(), team()])
        expect(newOn(candidates)).toContain('m1')
        expect(newOn(candidates)).not.toContain('m1-team')
      })
    })

    it('offers a team account whose Mac is not in the Swarm as itself, folded into nothing', async () => {
      // Probed for new agents under its own name, not left out as folded.
      const { candidates } = await route('owner', [{ ...team(), hostMachineId: 'm-elsewhere' }])
      expect(candidates.map(c => c.title)).toContain('New agent · Claude opus high · MacBook Pro')
    })

    // 2026-10-30 overnight: Codex in Tony's team account was never offered for reviews: its quota read as unknown, and the
    // route dropped every unknown candidate while another machine's quota fit. Its allowance is its own CLIs' report now
    // (#4137): nothing spends the Mac's subscription, so nothing borrows the Mac's reading.
    describe('a team account whose allowance is unknown', () => {
      const relayed = (why = 'No subscription sign-in is stored on this machine.') => ({ ...team(), observation: { ...team().observation,
        quotas: [{ engine: 'claude', known: false, windows: [] }], quotaReasons: { claude: why } } })
      // Full readings, both windows, as every Claude account reports them.
      const full = <T extends ReturnType<typeof healthy>>(pool: T): T => ({ ...pool, observation: { ...pool.observation, quotas: [{ engine: 'claude', known: true,
        windows: [{ name: '5-hour', usedPercent: 20, resetsAt: Date.now() + 3_600_000 }, { name: 'weekly', usedPercent: 30, resetsAt: Date.now() + 4 * 86_400_000 }] }] } })
      const fullMac = () => full(mac())
      // Another machine whose quota fits, as MacBookPro2021's Codex read that night.
      const other = () => full({ ...healthy(), machineId: 'm9', name: 'Other Mac', scope: 'swarm', cwd: '/other/swarm', contributorId: 'owner' })
      const fit = (s: ReturnType<typeof scoped>, machineId: string) =>
        (recorded(s).command.decision.candidates as Array<{ machineId: string; kind: string; label: string; fit: { status: string; account: string } }>)
          .find(c => c.machineId === machineId && c.kind === 'new')
      async function routeWith(machines: Record<string, unknown>[]) {
        const s = scoped({ context: { ok: true, value: { agentId: 'lead', tab: { state: { id: 'tab', agents: [], conversations: conversations(), machines,
          projects: [{ id: 'lead', ownerId: 'owner', repository: { repositoryUrl: 'https://github.com/example/project' } }] } },
          policy: { policy, warnings: [] }, machines: [] } } })
        return { answer: await routeTask(input, s, where, opener()), s }
      }

      it('never reads the Mac\'s quota for it, even from a daemon that still says the account spends the Mac\'s subscription', async () => {
        const { s } = await routeWith([fullMac(), relayed("This account runs on its owner's subscription; its allowance is read on the owner's machine."), other()])
        const row = fit(s, 'm1-team')
        expect(row).toMatchObject({ fit: { status: 'unknown' } })
        expect(row!.label).not.toContain('quota read on')
        expect(recorded(s).command.decision.excluded).not.toEqual(expect.arrayContaining([expect.objectContaining({ title: expect.stringContaining('MacBook Pro') })]))
      })

      it('keeps the quota unknown when the account says it cannot read it for another reason, and still offers it', async () => {
        const { s } = await routeWith([fullMac(), relayed('The allowance reader on this machine did not answer.'), other()])
        expect(fit(s, 'm1-team')).toMatchObject({ fit: { status: 'unknown' } })
        expect(fit(s, 'm1-team')!.label).toContain('quota unknown (The allowance reader on this machine did not answer.)')
        expect(recorded(s).command.decision.excluded).toEqual([])
      })

      // The decision kept its exclusions, but the Lead's answer showed none, so nobody saw why the Mac was never offered.
      it('tells the Lead each machine it left out and why, whether it picked another or found none', async () => {
        const reason = 'The Harness Team account on MacBook Pro is not private (gitconfig: it could read its owner\'s ~/.gitconfig), so it runs only its owner\'s projects.'
        const refused = { ...relayed(), teamWork: { allowed: false, code: 'PRIVACY_CHECK_FAILED', reason } }
        const picked = await routeWith([fullMac(), refused, other()])
        expect(picked.answer).toMatchObject({ ok: true, candidates: expect.arrayContaining([expect.objectContaining({ machineId: 'm9' })]) })
        expect(picked.answer.leftOut).toEqual([{ machine: 'MacBook Pro', reasons: [reason] }])
        const none = await routeWith([fullMac(), refused])
        expect(none.answer).toMatchObject({ error: 'NO_CANDIDATE' })
        expect(none.answer.leftOut).toEqual(expect.arrayContaining([{ machine: 'MacBook Pro', reasons: [reason] }]))
        // A machine with a row offered is not left out, though its other rows were.
        expect((await routeWith([fullMac(), relayed(), other()])).answer.leftOut).toEqual([])
      })
    })
  })
  it('refuses unobserved tools and devices, without probing arbitrary paths on a teammate computer', async () => {
    expect((await runPool(healthy(), false, { tools: ['docker'], devices: ['/dev/fixture'] })).answer.ok).not.toBe(true)
  })
  it('distinguishes reported missing tools from unverified contributed probes in route reasons', async () => {
    const missing = healthy(); missing.observation.tools.git = false
    const absent = await runPool(missing)
    expect(absent.answer.excluded).toEqual(expect.arrayContaining([expect.objectContaining({ reason: expect.stringContaining('Required tool missing: git.') })]))

    const unverified = healthy(); delete (unverified.observation.tools as Record<string, unknown>).git
    Object.assign(unverified.observation, { toolProbeErrors: { git: 'interactive shell timed out' } })
    const failed = await runPool(unverified)
    expect(failed.answer.excluded).toEqual(expect.arrayContaining([expect.objectContaining({ reason: expect.stringMatching(/Required tool not verified: git.*interactive shell timed out/) })]))
  })
})

describe('local models on the Swarm\'s grid', () => {
  const GRID = 'swarm-0123456789ab', NOW = 1_000_000_000
  const served = (models: Array<Record<string, unknown>>, receivedAt: unknown = NOW - 1_000) => ({ machineId: 'm1', observation: { receivedAt, localModels: models } })
  const state = (patch: Record<string, unknown> = {}) => ({ localModels: { grid: GRID, enabled: true }, machines: [served([{ grid: GRID, model: 'qwen3', name: 'Qwen 3' }])], agents: [], starts: [], ...patch })
  const base = (patch: Partial<Probe> = {}): Probe => ({ kind: 'new', machineId: 'm1', machineName: 'remote-1', engine: 'claude', model: 'sonnet', effort: 'medium', quality: 2, cost: 2,
    quota: { known: true, leftPercent: 50, resetsAt: null }, activity: 'new agent', known: true, refusals: [], notes: [], ...patch })
  const catalog: Catalog = { models: [], grid: { strength: 1, models: [] } }
  const offered = (s: Record<string, unknown>, opts: { probes?: Probe[]; catalog?: Catalog } = {}) =>
    localProbes(s, opts.catalog ?? catalog, opts.probes ?? [base()], NOW)

  it('offers one new agent per machine and engine on each served model, free and at the grid\'s strength', () => {
    const probes = [base({ notes: ['Its subscription is used up.', 'Stalled.'] }), base({ model: 'opus', effort: 'high' }),
      base({ engine: 'codex', refusals: ['Its subscription is used up.', 'Too little memory.'] }), base({ kind: 'member', agentId: 'a1' }), base({ gridModel: 'other' })]
    const rows = offered(state(), { probes, catalog: { ...catalog, grid: { strength: 2, models: [] } } })
    expect(rows.map(r => [r.engine, r.model, r.effort, r.quality, r.cost, r.gridModel, r.gridName, r.gridLabel, r.refusals, r.notes])).toEqual([
      ['claude', 'default', 'default', 2, 1, 'qwen3', GRID, 'Qwen 3', [], ['Stalled.']],
      ['codex', 'default', 'default', 2, 1, 'qwen3', GRID, 'Qwen 3', ['Too little memory.'], []],
    ])
    expect(rows[0]!.quota).toMatchObject({ known: false })
    // A backend before the add-on serves no grid strength: 1. A model with no name is called by its model.
    const unnamed = state({ machines: [served([{ grid: GRID, model: 'qwen3' }])] })
    expect(offered(unnamed)[0]).toMatchObject({ quality: 1, gridLabel: 'qwen3' })
  })
  it('rates each served model by the catalog\'s grid rows: Kimi K3 and DeepSeek v4.1 at 2, a small Qwen at the grid\'s strength', () => {
    const models = [{ model: 'kimi k3', strength: 2 }, { model: 'deepseek v4.1', strength: 2 }]
    const three = state({ machines: [served([{ grid: GRID, model: 'moonshotai/kimi-k3', kind: 'api' }, { grid: GRID, model: 'deepseek/deepseek-v4.1-flash-1017', kind: 'api' },
      { grid: GRID, model: 'qwen3:4b', name: 'Qwen 3 4B' }])] })
    const rows = offered(three, { catalog: { ...catalog, grid: { strength: 1, models } } })
    expect(rows.map(r => [r.gridModel, r.quality])).toEqual([['moonshotai/kimi-k3', 2], ['deepseek/deepseek-v4.1-flash-1017', 2], ['qwen3:4b', 1]])
  })
  it('offers nothing with the add-on off', () => {
    expect(offered(state({ localModels: undefined }))).toEqual([])
    expect(offered(state({ localModels: { grid: GRID, enabled: false } }))).toEqual([])
    expect(offered(state({ localModels: { enabled: true } }))).toEqual([])
  })
  it('reads only fresh readings, and of a model on another grid only on the machine that reaches it', () => {
    expect(offered(state({ machines: [served([{ grid: GRID, model: 'qwen3' }], NOW - 60_000)] }))).toEqual([])
    expect(offered(state({ machines: [served([{ grid: GRID, model: 'qwen3' }], 'soon')] }))).toEqual([])
    expect(offered(state({ machines: [served([{ grid: GRID, model: 7 }, { grid: 3, model: 'x' }])] }))).toEqual([])
    // Shared with m1's owner on their team grid: offered on m1 alone, while one on the Swarm's grid is offered on both.
    const reach = state({ machines: [served([{ grid: 'team-grid', model: 'team-model', kind: 'shared' }]), { machineId: 'm2', observation: { receivedAt: NOW - 1_000, localModels: [{ grid: GRID, model: 'qwen3' }] } }] })
    const rows = offered(reach, { probes: [base(), base({ machineId: 'm2', machineName: 'mac' })] })
    expect(rows.map(r => [r.machineId, r.gridName, r.gridModel, r.gridKind])).toEqual([
      ['m1', 'team-grid', 'team-model', 'shared'], ['m1', GRID, 'qwen3', 'local'], ['m2', GRID, 'qwen3', 'local'],
    ])
    // An API a member shares, by its kind; another grid's model without one is shared with that machine's owner.
    const kinds = state({ machines: [served([{ grid: GRID, model: 'ds', kind: 'api' }, { grid: 'team-grid', model: 'tm' }])] })
    expect(offered(kinds).map(r => [r.gridModel, r.gridKind])).toEqual([['ds', 'api'], ['tm', 'shared']])
    // An agent started on the shared model takes it, by its own grid.
    expect(offered({ ...reach, starts: [{ request: { gridModel: 'team-model', gridName: 'team-grid' } }] }, { probes: [base()] }).map(r => r.gridModel)).toEqual(['qwen3'])
  })
  it('keeps a model for the agent started on it until that agent leaves the Swarm', () => {
    const start = (patch: Record<string, unknown>) => ({ request: { gridModel: 'qwen3' }, ...patch })
    expect(offered(state({ starts: [start({})] }))).toEqual([])
    expect(offered(state({ starts: [start({ harnessId: 'h9' })], agents: [{ harnessId: 'h9' }] }))).toEqual([])
    expect(offered(state({ starts: [start({ harnessId: 'h9' })] }))).toHaveLength(1)
    expect(offered(state({ starts: [start({ error: 'NO_CAPACITY' })] }))).toHaveLength(1)
    expect(offered(state({ starts: [{ request: { model: 'opus' } }, {}] }))).toHaveLength(1)
  })
  it('never offers a model that is still starting: the grid does not list it yet', () => {
    expect(offered(state({ machines: [served([{ grid: GRID, model: 'qwen3', name: 'Qwen 3', starting: true }])] }))).toEqual([])
    expect(offered(state({ machines: [served([{ grid: GRID, model: 'qwen3', name: 'Qwen 3', starting: false }])] }))).toHaveLength(1)
  })
  it('lets up to four agents share an API model, which answers several requests at once', () => {
    const api = state({ machines: [served([{ grid: GRID, model: 'ds', name: 'DeepSeek', kind: 'api' }])] })
    const starts = (n: number) => Array.from({ length: n }, (_, i) => ({ request: { gridModel: 'ds' }, harnessId: `h${i}` }))
    const agents = (n: number) => Array.from({ length: n }, (_, i) => ({ harnessId: `h${i}`, name: `B${i}` }))
    expect(offered({ ...api, starts: starts(3), agents: agents(3) })).toHaveLength(1)
    expect(offered({ ...api, starts: starts(4), agents: agents(4) })).toEqual([])
  })
  it('names the agents on each model that takes no more, so the route can say why it is not offered', () => {
    const api = served([{ grid: GRID, model: 'ds', name: 'DeepSeek', kind: 'api' }, { grid: GRID, model: 'qwen3', name: 'Qwen 3' }])
    const s = state({ machines: [api],
      starts: [{ request: { gridModel: 'qwen3' }, harnessId: 'h1' }, { request: { gridModel: 'qwen3' } },
        ...['h2', 'h3', 'h4', 'h5'].map(harnessId => ({ request: { gridModel: 'ds' }, harnessId }))],
      agents: [{ harnessId: 'h1', displayName: 'Faye', name: 'Builder' }, { harnessId: 'h2', name: 'Wren' }, { harnessId: 'h3' }, { harnessId: 'h4', name: 'Ada' }, { harnessId: 'h5', name: 'Bo' }] })
    expect(heldLocalModels(s, NOW)).toEqual([
      { model: 'ds', label: 'DeepSeek', kind: 'api', holders: ['Wren', 'an agent', 'Ada', 'Bo'] },
      { model: 'qwen3', label: 'Qwen 3', kind: 'local', holders: ['Faye', 'an agent that is starting'] },
    ])
    // Nothing is held while the add-on is off, or with a model nobody is on.
    expect(heldLocalModels(state({ localModels: { grid: GRID, enabled: false } }), NOW)).toEqual([])
    expect(heldLocalModels(state(), NOW)).toEqual([])
    expect(heldLocalModels(state({ localModels: undefined }))).toEqual([])
  })
  // Project instructions, phase 4, decision 19: a model too small or too slow for a coding agent is offered, with why.
  it('serves the models a fresh reading says are too small or too slow, with why as a note, once each', () => {
    const why = 'llama3.2:3b is too small for a coding agent.'
    const refusing = (receivedAt: unknown, refusedModels: unknown) => ({ machineId: 'm2', observation: { receivedAt, refusedModels } })
    const s = state({ machines: [served([{ grid: GRID, model: 'fast', name: 'Fast' }]), refusing(NOW - 1_000, [
      { grid: GRID, model: 'llama3.2:3b', why }, { grid: GRID, model: 'llama3.2:3b', why }, { grid: GRID, model: 'tiny', name: 'Tiny 1B', why: 'small' },
      { grid: 'other-grid', model: 'x', why }, { grid: GRID, model: 7, why }, { grid: GRID, model: 'no-why' },
      // Another machine serves it fit: no note.
      { grid: GRID, model: 'fast', why: 'slow here' }]),
      refusing(NOW - 60_000, [{ grid: GRID, model: 'stale', why }]), refusing('soon', [{ grid: GRID, model: 'odd', why }])] })
    expect([...servedModels(s, GRID, NOW).values()].map(m => [m.model, m.label, m.note])).toEqual([
      ['fast', 'Fast', undefined], ['llama3.2:3b', 'llama3.2:3b', why], ['tiny', 'Tiny 1B', 'small']])
    expect(offered(s).map(r => [r.gridModel, r.notes])).toEqual([['fast', []], ['llama3.2:3b', [why]], ['tiny', ['small']]])
    // None of them is held: nobody is on them.
    expect(heldLocalModels(s, NOW)).toEqual([])
  })
  it('leaves out a model whose host is offline, with that reason, unless another host serves it', async () => {
    const t = tab(); Object.assign(t.state, { localModels: { grid: GRID, enabled: true }, machines: [
      { machineId: 'm1', name: 'remote-1', observation: { receivedAt: Date.now(), localModels: [{ grid: GRID, model: 'deepseek/deepseek-v4.1', kind: 'api' }] } },
      { machineId: 'rig', name: '2x 4090 Rig', observation: { receivedAt: Date.now() - 10 * 60_000, localModels: [{ grid: GRID, model: 'moonshotai/kimi-k3', name: 'Kimi K3' },
        { grid: GRID, model: 'deepseek/deepseek-v4.1', kind: 'api' }, { grid: GRID, model: 'starting', starting: true }, { grid: 7, model: 'odd' }] } },
      { machineId: 'old', observation: { receivedAt: 'never', localModels: [{ grid: GRID, model: 'unnamed-host' }] } }] })
    const s = scoped({ tab: t })
    // A grid model takes only work of a listed kind (routeCheapFirst.ts GRID_KINDS).
    const result = await routeTask({ ...input, assessment: { ...input.assessment, difficulty: 'low', local: true, kind: 'docs' } }, s, where as never, opener(), () => 'op')
    const excluded = recorded(s).command.decision.excluded as Array<{ title: string; reason: string }>
    expect(excluded.filter(e => /offline/.test(e.reason))).toEqual([
      { title: 'Kimi K3', reason: 'Its host 2x 4090 Rig is offline, so an agent on it would never start.' },
      { title: 'unnamed-host', reason: 'Its host, a machine of this Swarm, is offline, so an agent on it would never start.' },
    ])
    expect(result).toMatchObject({ ok: true, candidates: expect.arrayContaining([expect.objectContaining({ gridModel: 'deepseek/deepseek-v4.1' })]) })
    // A linked task is routed like any other (project instructions, phase 4): the same is said for it. Nothing with the add-on off.
    Object.assign(t.state.conversations[1]!, { reviewOf: 'lead' })
    const review = scoped({ tab: t })
    await routeTask(input, review, where as never, opener(), () => 'op')
    expect(JSON.stringify(recorded(review).command.decision.excluded)).toContain('offline, so an agent')
    expect(offlineModels(t.state)).toHaveLength(2)
    expect(offlineModels({ ...t.state, localModels: { grid: GRID, enabled: false } })).toEqual([])
    expect(offlineModels({ ...t.state, localModels: undefined })).toEqual([])
  })
  it('routes a low task to a served model and starts it on the grid, with no subscription model', async () => {
    const t = tab(); Object.assign(t.state, { localModels: { grid: GRID, enabled: true },
      machines: [{ machineId: 'm1', observation: { receivedAt: Date.now(), localModels: [{ grid: GRID, model: 'qwen3', name: 'Qwen 3' }] } }] })
    const s = scoped({ tab: t })
    // A grid model takes only work of a listed kind (routeCheapFirst.ts GRID_KINDS).
    const result = await routeTask({ ...input, assessment: { ...input.assessment, difficulty: 'low', local: true, kind: 'docs' } }, s, where as never, opener(), () => 'op')
    expect(result).toMatchObject({ ok: true, candidates: expect.arrayContaining([expect.objectContaining({ kind: 'new', gridModel: 'qwen3', gridName: GRID })]) })
    // The row's grid model and its name go in the start, in place of model and effort.
    const row = (result.candidates as Array<Record<string, unknown>>).find(r => r.gridModel === 'qwen3')!
    expect(row).toMatchObject({ engine: 'claude', gridName: GRID })
    expect(result.next).toContain('takes its gridModel and gridName in place of model and effort')
    expect(recorded(s).command.decision.candidates).toContainEqual(expect.objectContaining({ id: row.id, gridModel: 'qwen3', gridName: GRID }))
  })
})

// Project instructions, phase 4: any agent of the project routes, every member is a candidate whatever its kind, and what a
// rule, a lost task, a stall or an avoided account used to filter is a note on the candidate.
describe('routing by facts', () => {
  const accountOn = (account: string) => ({ providers: [{ ...usage.providers[0]!, account }] })
  const shown = (s: ReturnType<typeof scoped>) => recorded(s).command.decision.candidates as Array<{ title: string; label: string; quality: number; machineId: string; gridModel?: string }>

  it('lets any agent of the group\'s project route', async () => {
    const open = opener()
    const s = scoped({ context: { ok: true, value: { agentId: 'a1', tab: tab(), policy: { policy }, machines: [{ machineId: 'm1', name: 'remote-1' }] } } })
    expect(await routeTask(input, s, where as never, open, () => 'op')).toMatchObject({ ok: true, routeDecisionId: 'd1' })
    expect(open).toHaveBeenCalled()
  })

  it('shows lost, stalled and avoided accounts as notes', async () => {
    const t = tab()
    t.state.agents = [
      { ...agents[0]!, lostTask: { conversationId: 'g0', assignmentId: 'x', reason: 'stalled', at: 1 } },
      agents[1]!,
      { id: 'r1', name: 'Reviewer', role: 'reviewer', machineId: 'm1', harnessId: 'hr', configuration: { engine: 'claude', model: 'sonnet', effort: 'medium' } },
    ] as never
    // The group's last attempt failed on m1's Claude account; m2's last two starts started nothing.
    Object.assign(t.state.conversations[1]!, { lostAttempt: { id: 'x', agentId: 'gone', machineId: 'm1', engine: 'claude' } })
    const failed = { id: 's', request: { machineId: 'm2' }, harnessId: null, dispatched: true, error: 'The engine did not start.' }
    Object.assign(t.state, { starts: [failed, { ...failed, id: 's2' }] })
    const s = scoped({ tab: t })
    const result = await routeTask(input, s, where as never, opener(id => daemon(id, { agents: id === 'm1' ? ['h1', 'hl', 'hr'] : [],
      usage: accountOn(id === 'm1' ? 'acct-1' : 'acct-2') })), () => 'op')
    expect(result).toMatchObject({ ok: true })
    const rows = shown(s), line = (prefix: string) => rows.find(r => r.title.startsWith(prefix))!.label
    // The idle coordinator and a reviewer-kind member are candidates for a build.
    expect(rows.map(r => r.title)).toEqual(expect.arrayContaining(['Multiply · Claude sonnet medium · remote-1', 'Lead · Claude opus high · remote-1',
      'Reviewer · Claude sonnet medium · remote-1']))
    expect(line('Multiply')).toContain('It lost its last task (stalled).')
    for (const row of rows.filter(r => r.machineId === 'm1')) expect(row.label).toContain('Its account lost this task before')
    for (const row of rows.filter(r => r.machineId === 'm2')) expect(row.label).toContain('last 2 starts')
    expect(recorded(s).command.decision.excluded).toEqual([])
    // The answer says the same of what it offers.
    const offered = result.candidates as Array<{ machineId: string; notes?: string[] }>
    expect(offered.filter(o => o.machineId === 'm1').every(o => o.notes?.some(n => n.includes('Its account lost this task before')))).toBe(true)
  })

  it('reads the catalog, or an older backend\'s policy', async () => {
    const catalog = { models: [{ engine: 'claude', model: 'opus', effort: 'high', strength: 3, cost: 3, offered: true },
      { engine: 'claude', model: 'sonnet', effort: 'medium', strength: 2, cost: 2, offered: false, note: 'Not offered by default.' }],
      grid: { strength: 1, models: [] }, priors: {}, prices: [] }
    const served = scoped({ context: { ok: true, value: { agentId: 'lead', tab: tab(), policy: { policy, warnings: [], catalog }, machines: [{ machineId: 'm1', name: 'remote-1' }] } } })
    await routeTask(input, served, where as never, opener(), () => 'op')
    const rows = shown(served)
    expect(rows.find(r => r.title.startsWith('Multiply'))).toMatchObject({ quality: 2, label: expect.stringContaining('Not offered by default.') })
    expect(rows.filter(r => r.title.startsWith('New agent')).map(r => r.title)).toEqual(['New agent · Claude opus high · remote-1'])
    // An older backend serves only its policy: its rows, rated as it rates them.
    const old = scoped()
    await routeTask(input, old, where as never, opener(), () => 'op')
    expect(shown(old).find(r => r.title.startsWith('Multiply'))).toMatchObject({ quality: 3 })
    expect(shown(old).map(r => r.title)).toContain('New agent · Claude sonnet medium · remote-1')
  })

  // Task #2972: 95% of Codex candidates were `Codex default default` (6,570 against 122 Sol and 116 Astra), and 67 of 71 Codex picks;
  // it ran whatever the account's CLI defaulted to (gpt-6-luna on one owner's account), which the catalog does not rate.
  it('offers a Codex machine only the models the catalog rates, never `default default`, and gpt-6-luna only when named', async () => {
    const catalog = { models: [{ engine: 'claude', model: 'opus', effort: 'high', strength: 3, cost: 3, offered: true },
      { engine: 'codex', model: 'default', effort: 'default', strength: 3, cost: 2, offered: true },
      { engine: 'codex', model: 'gpt-6.1-sol', effort: 'high', strength: 3, cost: 2, offered: true, engineDefault: true },
      { engine: 'codex', model: 'gpt-6-astra', effort: 'xhigh', strength: 3, cost: 5, offered: true }],
      grid: { strength: 1, models: [] }, priors: {}, prices: [] }
    const both = () => opener(id => daemon(id, { agents: id === 'm1' ? ['h1'] : [],
      engines: [{ engine: 'claude', installed: true, signedIn: true }, { engine: 'codex', installed: true, signedIn: true }] }))
    const context = (extra: Record<string, unknown> = {}) => scoped({ context: { ok: true, value: { agentId: 'lead', tab: tab(), policy: { policy, warnings: [], catalog }, machines: [{ machineId: 'm1', name: 'remote-1' }], ...extra } } })
    const served = context()
    await routeTask(input, served, where as never, both(), () => 'op')
    const codex = shown(served).filter(r => r.title.startsWith('New agent · Codex'))
    expect(codex.map(r => r.title)).toEqual(['New agent · Codex gpt-6-astra xhigh · remote-1', 'New agent · Codex gpt-6.1-sol high · remote-1'])
    expect(codex.map(r => r.label).join('\n')).toMatch(/strength 3, cost 5[\s\S]*strength 3, cost 2/)
    expect(shown(served).map(r => r.title).join('\n')).not.toMatch(/Codex default|luna/)
    // A person who names the model gets it, unrated: gpt-6-luna is no row of the catalog.
    const luna = context()
    await routeTask({ ...input, assessment: { ...input.assessment, model: 'gpt-6-luna' } }, luna, where as never, both(), () => 'op')
    expect(shown(luna).filter(r => r.title.startsWith('New agent')).map(r => r.title)).toEqual(['New agent · Codex gpt-6-luna default · remote-1'])
    expect(shown(luna)[0]!.label).toContain('unrated')
  })

  // Final review: a named grid model whose host is down became an unrated Codex candidate; nothing serves it, and the route says
  // why. An unreadable catalog became an empty one without a word: no new agent was offered and nobody heard why.
  it('names no stand-in for a named model whose host is offline, and says why', async () => {
    const GRID = 'swarm-0123456789ab'
    const t = tab(); Object.assign(t.state, { localModels: { grid: GRID, enabled: true }, machines: [{ machineId: 'rig', name: '2x 4090 Rig',
      observation: { receivedAt: Date.now() - 10 * 60_000, localModels: [{ grid: GRID, model: 'moonshotai/kimi-k3', name: 'Kimi K3' }] } }] })
    const opened = opener(id => daemon(id, { agents: id === 'm1' ? ['h1'] : [], engines: [{ engine: 'claude', installed: true, signedIn: true }, { engine: 'codex', installed: true, signedIn: true }] }))
    const result = await routeTask({ ...input, assessment: { ...input.assessment, model: 'kimi k3' } }, scoped({ tab: t }), where as never, opened, () => 'op')
    expect(result).toMatchObject({ error: 'NO_CANDIDATE' })
    expect(result.excluded).toEqual(expect.arrayContaining([{ title: 'Named model', reason: expect.stringContaining('No candidate runs "kimi k3"') },
      { title: 'Kimi K3', reason: 'Its host 2x 4090 Rig is offline, so an agent on it would never start.' }]))
  })
  it('says when the catalog could not be read, so no new agent is offered', async () => {
    const empty = scoped({ context: { ok: true, value: { agentId: 'lead', tab: tab(), policy: {}, machines: [{ machineId: 'm1', name: 'remote-1' }] } } })
    const result = await routeTask(input, empty, where as never, opener(), () => 'op')
    expect(result).toMatchObject({ ok: true, candidates: expect.arrayContaining([expect.objectContaining({ kind: 'member' })]) })
    expect(result.warnings).toContain('The model catalog could not be read, or lists no models: no new agent on Claude Code or Codex is offered, only agents already in the Swarm and models served on its grid.')
    expect(recorded(empty).command.decision.excluded).toContainEqual({ title: 'New agents', reason: expect.stringContaining('The model catalog could not be read') })
  })

  it('offers a small or slow grid model with a note', async () => {
    const GRID = 'swarm-0123456789ab', why = 'llama3.2:3b is too small for a coding agent (about 3B parameters; a coding agent needs 7B or more).'
    const t = tab(); Object.assign(t.state, { localModels: { grid: GRID, enabled: true },
      machines: [{ machineId: 'rig', name: 'Rig', observation: { receivedAt: Date.now(), localModels: [], refusedModels: [{ grid: GRID, model: 'llama3.2:3b', why }] } }] })
    const s = scoped({ tab: t })
    // A grid model takes only work of a listed kind (routeCheapFirst.ts GRID_KINDS).
    const result = await routeTask({ ...input, assessment: { ...input.assessment, local: true, kind: 'tests' } }, s, where as never, opener(), () => 'op')
    expect(result).toMatchObject({ ok: true, candidates: expect.arrayContaining([expect.objectContaining({ gridModel: 'llama3.2:3b', notes: [why] })]) })
    expect(shown(s).every(r => r.gridModel === 'llama3.2:3b' && r.label.includes(why))).toBe(true)
  })

  it('gives a neutral next step: the role comes from the project\'s instructions', async () => {
    const hard = { ...input, assessment: { ...input.assessment, difficulty: 'high' } }
    // Only the Sonnet member, so a stronger new agent is offered too.
    const crew = (reviewOf?: string) => {
      const t = tab(); t.state.agents = [agents[0]] as never
      if (reviewOf) Object.assign(t.state.conversations[1]!, { reviewOf })
      return t
    }
    const build = await routeTask(hard, scoped({ tab: crew(), policy: floorPolicy }), where as never, opener(), () => 'op')
    const reviewing = await routeTask(hard, scoped({ tab: crew('lead'), policy: floorPolicy }), where as never, opener(), () => 'op')
    for (const answer of [build, reviewing]) {
      expect(answer).toMatchObject({ ok: true, candidates: expect.arrayContaining([expect.objectContaining({ kind: 'new' })]) })
      expect(answer.next).toContain('"roleName":"ROLE"')
      expect(answer.next).not.toMatch(/Builder|Reviewer|"role":"reviewer"/)
    }
    // A backend without instructions takes no roleName.
    const old = await routeTask(hard, scoped({ tab: crew(), policy: floorPolicy, instructions: { error: 'NOT_FOUND' } }), where as never, opener(), () => 'op')
    expect(old.next).not.toContain('roleName')
  })
})

/** Cheap first (Scholes's goals, piece 4) through the whole route: what it reads of the task comes from the tab and the log. */
describe('cheap first, through the route', () => {
  const GRID = 'swarm-0123456789ab', KIMI = 'moonshotai/kimi-k3'
  /** The Swarm serves Kimi K3 on its grid beside the Claude agents of remote-1 and the member Multiply; g1 is a one-file fix. */
  const cheap = (group: Record<string, unknown> = {}, more: Record<string, unknown> = {}) => {
    const t = tab()
    Object.assign(t.state, { localModels: { grid: GRID, enabled: true },
      machines: [{ machineId: 'gpu', name: 'rig', observation: { receivedAt: Date.now(), localModels: [{ grid: GRID, model: KIMI, name: 'Kimi K3', kind: 'api' }] } }], ...more })
    Object.assign(t.state.conversations[1]!, { task: { goal: 'Fix the typo.', owns: ['docs/guide.md'] } }, group)
    return t
  }
  const small = { ...input, assessment: { difficulty: 'low', risk: 'low', size: 'small', why: 'A typo in one doc.', kind: 'docs' } }
  /** The table with the catalog's grid ratings: Kimi K3 at 2. */
  const rated = { ...policy, localModels: { quality: 1, enabled: true, models: [{ model: 'kimi k3', quality: 2 }] } } as typeof policy
  const route = (t: ReturnType<typeof tab>, task = small, history?: Record<string, unknown>) => {
    const s = scoped({ tab: t, policy: rated, ...(history ? { history } : {}) })
    return routeTask(task, s, where as never, opener(), () => 'op').then(answer => ({ answer, s, excluded: recorded(s).command.decision.excluded as Array<{ reason: string }> }))
  }

  it('a small, low-risk build\'s first attempt is started on Kimi K3, with why', async () => {
    const { answer, s } = await route(cheap())
    expect(answer).toMatchObject({ ok: true, candidates: expect.arrayContaining([expect.objectContaining({ kind: 'new', gridModel: KIMI, gridName: GRID, notes: [`Cheap first: ${GRID_FIRST}`] })]) })
    expect(recorded(s).command.decision.candidates).toContainEqual(expect.objectContaining({ gridModel: KIMI, quality: 2 }))
    // A medium task still offers the strong ones.
    const medium = await route(cheap(), { ...small, assessment: { ...small.assessment, difficulty: 'medium' } })
    expect(medium.answer).toMatchObject({ ok: true, candidates: expect.arrayContaining([expect.objectContaining({ kind: 'member', model: 'sonnet' })]) })
  })

  it('steps up: once the task had an attempt on Kimi K3, the next route leaves the grid out and starts a strong model (Sonnet is rated 3 in this table)', async () => {
    const tried = cheap({ assignment: { id: 'w1', agentId: 'a3', status: 'cancelled', result: 'Its first review said Ready no.' } }, {
      agents: [...agents, { id: 'a3', name: 'Kestrel', machineId: 'm1', harnessId: 'h3', configuration: { engine: 'claude', model: 'default', effort: 'default' } }],
      starts: [{ id: 's1', harnessId: 'h3', request: { conversationId: 'g1', machineId: 'm1', gridModel: KIMI, gridName: GRID } }] })
    const { answer, excluded } = await route(tried)
    expect(answer).toMatchObject({ ok: true, candidates: expect.arrayContaining([expect.objectContaining({ kind: 'member', model: 'sonnet' })]) })
    expect((answer.candidates as Array<Record<string, unknown>>).every(r => r.gridModel === undefined)).toBe(true)
    expect(excluded).toContainEqual(expect.objectContaining({ reason: `Cheap first: ${FIRST_ATTEMPT_ONLY} Its earlier attempt ran on ${KIMI} on the Swarm's grid.` }))
    // A log that could not be read is no proof of a first attempt.
    const unread = await route(cheap(), small, { error: 'BACKEND_UNAVAILABLE' })
    expect(unread.answer).toMatchObject({ ok: true, candidates: expect.arrayContaining([expect.objectContaining({ kind: 'member', model: 'sonnet' })]) })
    expect(unread.excluded).toContainEqual(expect.objectContaining({ reason: `Cheap first: ${FIRST_ATTEMPT_ONLY} ${EARLIER_UNREADABLE}` }))
  })

  it('the review of a grid-built pull request still runs on a strong model: the grid is left out with why', async () => {
    // g1 was built on Kimi K3; its review task r1 is routed as small and low-risk as the build was, with no kind given.
    const built = cheap({ assignment: { id: 'w1', agentId: 'a3', status: 'review' }, pullRequestUrls: ['https://github.com/o/r/pull/1'] }, {
      agents: [...agents, { id: 'a3', name: 'Kestrel', machineId: 'm1', harnessId: 'h3', configuration: { engine: 'claude', model: 'default', effort: 'default' } }],
      starts: [{ id: 's1', harnessId: 'h3', request: { conversationId: 'g1', machineId: 'm1', gridModel: KIMI, gridName: GRID } }] })
    built.state.conversations.push({ id: 'r1', title: 'Review: API', leadId: 'lead', agentIds: ['lead'], archived: false, brief: '', checks: [{ description: 'Verdict' }],
      assignment: null, reviewOf: 'g1', task: { goal: 'Review it.', owns: ['docs/guide.md'] } } as never)
    const { answer, s, excluded } = await route(built, { ...small, conversationId: 'r1' })
    expect(answer).toMatchObject({ ok: true, candidates: expect.arrayContaining([expect.objectContaining({ kind: 'member', model: 'sonnet' })]) })
    const candidates = recorded(s).command.decision.candidates as Array<{ gridModel?: string; quality: number }>
    expect(candidates.some(c => c.gridModel)).toBe(false)
    expect(candidates[0]!.quality).toBe(3)
    expect(excluded).toContainEqual(expect.objectContaining({ reason: `${GRID_LIMITS}: ${NEVER_REVIEWS}` }))
    // Even when a person names the grid model for it.
    const named = await routeTask({ ...small, conversationId: 'r1', assessment: { ...small.assessment, model: 'kimi' } }, scoped({ tab: built, policy: rated }), where as never, opener(), () => 'op')
    expect(named).toMatchObject({ error: 'NO_CANDIDATE' })
  })
})

/** Piece 3 through the route itself, with the makers it ships with (routePlugins.ts ROUTE_PLUGIN_MAKERS). */
describe('routing by measured cost and results', () => {
  const row = (model: string, effort: string, fullCostPerShipped: number) => ({ engine: 'claude', model, effort, kind: 'build', firstReviews: 10,
    firstReviewPassed: 8, shipped: 6, costPerShipped: 1, fullCostPerShipped, fullCostKnown: 1 })
  const built = { ...input, assessment: { ...input.assessment, kind: 'build' } }
  const withCosts = (base: ReturnType<typeof scoped>, answer: (payload: Record<string, unknown>) => Promise<Answer>) =>
    vi.fn(async (payload: Record<string, any>) => payload.action === 'costs' ? answer(payload) : base(payload))

  it('reads the last 7 days\' results and notes the choice that costs less per shipped change, with its numbers', async () => {
    const base = scoped()
    // Sonnet's catalog cost is 2 against Opus's 3; measured in full, Opus costs $3 per shipped change and Sonnet $9.
    const plain = await routeTask(input, scoped(), where as never, opener(), () => 'op')
    expect(plain).toMatchObject({ candidates: expect.arrayContaining([expect.objectContaining({ model: 'sonnet' })]) })
    const s = withCosts(base, async () => ({ ok: true, value: { truncated: false, rows: [row('opus', 'high', 3), row('sonnet', 'medium', 9)] } }))
    const result = await routeTask(built, s, where as never, opener(), () => 'op')
    expect(s.mock.calls.filter(c => c[0].action === 'costs').map(c => c[0])).toEqual([{ action: 'costs', days: 7 }])
    expect(result).toMatchObject({ ok: true, candidates: expect.arrayContaining([expect.objectContaining({ model: 'opus', notes: expect.arrayContaining([expect.stringMatching(/^measured results: build work, last 7 days: 8 of 10 first reviews passed, \$3\.00 per shipped change over 6/)]) })]) })
    expect(labels(base)[0]).toContain('measured results: build work')
  })

  it('a failed read leaves today\'s ranking and says so', async () => {
    const s = withCosts(scoped(), async () => ({ error: 'FORBIDDEN' }))
    const result = await routeTask(built, s, where as never, opener(), () => 'op')
    expect(result).toMatchObject({ ok: true, candidates: expect.arrayContaining([expect.objectContaining({ model: 'sonnet' })]),
      warnings: expect.arrayContaining(['The routing plug-in "measured results" was skipped: its maker failed. The route is shown without it.']) })
  })
})

// Task #3047, phase 1 of docs/design/2026-11-05-machine-access.md: a task names what it needs to reach, and the route leaves
// out every machine and account that does not have it, with why. The backend refuses those starts too (machineAccess.ts).
describe('needs.access', () => {
  const reading = () => ({ receivedAt: Date.now(), resources: { memoryTotalBytes: 32e9, memoryUsedBytes: 2e9, projectDiskFreeBytes: 200e9 },
    tools: { git: true }, runningHarnessIds: ['h1'], quotas: [{ engine: 'claude', known: true, windows: [{ name: '5-hour', usedPercent: 20, resetsAt: Date.now() + 3600000 }] }] })
  const entry = (name: string, account: 'own' | 'team' = 'own', extra: Record<string, unknown> = {}) =>
    ({ id: `e${name.length}${account}`, name, account, state: 'ok', addedBy: 'mate', addedAt: '2026-11-05T00:00:00.000Z', ...extra })
  /** Mate's Mac, with its Harness Team account (healthy), and the list on the Mac's own entry. */
  const mac = (access?: unknown[]) => ({ machineId: 'm1', name: 'MacBook Pro', scope: 'swarm', cwd: '/mate/swarm', engines: ['claude'], paused: false,
    contributorId: 'mate', observation: reading(), teamWork: { allowed: true }, ...(access ? { access } : {}) })
  const team = () => ({ machineId: 'm1-team', hostMachineId: 'm1', name: 'MacBook Pro', scope: 'swarm', cwd: '/team/swarm', engines: ['claude'], paused: false,
    contributorId: 'mate', observation: reading(), teamWork: { allowed: true } })
  /** The project owner's own machine. */
  const other = (access?: unknown[]) => ({ machineId: 'm9', name: 'Studio', scope: 'swarm', cwd: '/owner/swarm', engines: ['claude'], paused: false,
    contributorId: 'owner', observation: reading(), teamWork: { allowed: true }, ...(access ? { access } : {}) })
  const people = [{ id: 'owner', name: 'Ken' }, { id: 'mate', name: 'Gray' }]
  async function route(machines: Record<string, unknown>[], { needs = { access: ['Lamp'] } as Record<string, unknown>, requires, members = [] as unknown[], ownerId = 'owner' }:
    { needs?: Record<string, unknown>; requires?: Record<string, unknown>; members?: unknown[]; ownerId?: string } = {}) {
    const groups = conversations().map(g => g.id === 'g1' && requires ? { ...g, task: { requires } } : g)
    const s = scoped({ context: { ok: true, value: { agentId: 'lead', tab: { state: { id: 'tab', agents: members, conversations: groups, machines, people,
      projects: [{ id: 'lead', ownerId, repository: { repositoryUrl: 'https://github.com/example/project' } }] } },
      policy: { policy, warnings: [] }, machines: [] } } })
    const answer = await routeTask({ ...input, assessment: { ...input.assessment, needs } }, s, where, opener())
    const decision = s.mock.calls.find(c => c[0].action === 'command')?.[0].command.decision
    return { answer, candidates: (decision?.candidates ?? []) as Array<{ machineId: string; kind: string; title: string; notes?: string[] }>,
      excluded: (decision?.excluded ?? answer.excluded ?? []) as Array<{ title: string; reason: string }> }
  }
  const reasonsFor = (excluded: Array<{ title: string; reason: string }>, name: string) => excluded.filter(e => e.title.includes(name)).map(e => e.reason)

  it('leaves out a machine without it, from needs.access or from the task\'s requires.access, names matched without case', async () => {
    for (const ask of [{ needs: { access: ['  lamp '] } }, { needs: {}, requires: { access: ['Lamp'] } }]) {
      const { candidates, excluded } = await route([mac([entry('Lamp', 'team')]), team(), other()], ask)
      expect([...new Set(candidates.filter(c => c.kind === 'new').map(c => c.machineId))], JSON.stringify(ask)).toEqual(['m1-team'])
      expect(reasonsFor(excluded, 'Studio'), JSON.stringify(ask)).toContain(`No ${ask.requires ? 'Lamp' : 'lamp'} here.`)
    }
  })

  it('suggests the closest name it saw when nothing matches', async () => {
    const { excluded } = await route([other([entry('Desk lamp')])], { ownerId: 'owner' })
    expect(reasonsFor(excluded, 'Studio')).toContain('No Lamp here. Did you mean Desk lamp?')
    const far = await route([other([entry('Fusion 360')])])
    expect(reasonsFor(far.excluded, 'Studio')).toContain('No Lamp here.')
    // The nearest of several: a letter off beats a longer name holding it.
    const near = await route([other([entry('Desk lamp'), entry('Lamb'), entry('Lamps and more')])])
    expect(reasonsFor(near.excluded, 'Studio')).toContain('No Lamp here. Did you mean Lamb?')
  })

  it('takes the task\'s access beside the Lead\'s, each name once', async () => {
    const { excluded } = await route([other([entry('Lamp')])], { needs: { access: ['lamp'] }, requires: { access: ['Lamp', 'Fusion 360'] } })
    expect(reasonsFor(excluded, 'Studio')).toEqual(expect.arrayContaining(['No Fusion 360 here.']))
    expect(reasonsFor(excluded, 'Studio').some(r => /No lamp here/i.test(r))).toBe(false)
  })

  it('never offers a teammate\'s task the owner\'s unshared own-account entry, nor counts it for the Team account', async () => {
    const { answer, excluded } = await route([mac([entry('Lamp')]), team()])
    expect(answer).toMatchObject({ error: 'NO_CANDIDATE' })
    expect(excluded.map(e => e.reason)).toContain('Lamp is in Gray\'s own account, not shared with teammates.')
  })

  it('offers the Mac itself, in its owner\'s account and saying so, when its owner shared that entry and the task names it', async () => {
    const { answer, candidates } = await route([mac([entry('Lamp', 'own', { teammates: true })]), team()], { needs: {}, requires: { access: ['Lamp'] } })
    expect([...new Set(candidates.filter(c => c.kind === 'new').map(c => c.machineId))]).toEqual(['m1'])
    const rows = answer.candidates as Array<{ title: string; notes?: string[] }>
    expect(rows.length).toBeGreaterThan(0)
    for (const row of rows) expect(row.notes ?? [], row.title).toContain('MacBook Pro runs this task in Gray\'s own account: Lamp is shared with teammates.')
  })

  // Security review 2: the start keeps a teammate's task on the Mac only for access its task names (backend teamMachineFor), so
  // a route asked with needs.access alone never offers the owner's account: it would start in the Team account.
  it('offers a shared own-account entry only to a task that names it, as the start places it', async () => {
    const { candidates, excluded } = await route([mac([entry('Lamp', 'own', { teammates: true })]), team()])
    expect(candidates.some(c => c.kind === 'new' && c.machineId === 'm1')).toBe(false)
    expect(excluded.map(e => e.reason)).toContain('Lamp is shared from Gray\'s own account only with tasks that name it in their access.')
  })

  it('notes a production system on every candidate that would use one', async () => {
    const { answer } = await route([mac([entry('Prod DB read-only', 'team', { production: true })]), team()], { needs: { access: ['Prod DB read-only'] } })
    const rows = answer.candidates as Array<{ title: string; notes?: string[] }>
    expect(rows.length).toBeGreaterThan(0)
    for (const row of rows) expect(row.notes ?? [], row.title).toContain('Prod DB read-only is a production system: writes still need a person\'s go.')
    const plain = await route([mac([entry('Lamp', 'team')]), team()])
    expect(JSON.stringify(plain.answer)).not.toContain('production system')
  })

  it('names every shared entry the task needs on the Mac', async () => {
    const { answer } = await route([mac([entry('Lamp', 'own', { teammates: true }), entry('Fusion 360', 'own', { teammates: true })]), team()],
      { needs: {}, requires: { access: ['Lamp', 'Fusion 360'] } })
    for (const row of answer.candidates as Array<{ title: string; notes?: string[] }>) {
      expect(row.notes ?? [], row.title).toContain('MacBook Pro runs this task in Gray\'s own account: Lamp, Fusion 360 are shared with teammates.')
    }
  })

  it('leaves out an entry waiting for its owner to confirm, and a Team-account entry for the owner\'s own project', async () => {
    const waiting = await route([mac([entry('Lamp', 'team', { state: 'unconfirmed' })]), team()])
    expect(waiting.excluded.map(e => e.reason)).toContain('Lamp on MacBook Pro is waiting for Gray to confirm.')
    const own = await route([mac([entry('Lamp', 'team')]), team()], { ownerId: 'mate' })
    expect(own.excluded.map(e => e.reason)).toContain('Lamp is in the Harness Team account on MacBook Pro, which runs only teammates\' work.')
  })

  it('leaves out a member on a machine without it too', async () => {
    const member = { id: 'a1', name: 'Multiply', machineId: 'm9', harnessId: 'h1', contributorId: 'owner', configuration: { engine: 'claude', model: 'sonnet', effort: 'medium' } }
    const { candidates, excluded } = await route([mac([entry('Lamp', 'team')]), team(), other()], { members: [member] })
    expect(candidates.some(c => c.kind === 'member')).toBe(false)
    expect(reasonsFor(excluded, 'Multiply')).toContain('No Lamp here.')
  })

  it('adds nothing to a start\'s requirements, which older hosts read strictly', async () => {
    const { answer } = await route([mac([entry('Lamp', 'team')]), team()])
    const requirements = String(answer.next).match(/"requirements":\{[^}]*\}/g) ?? []
    expect(requirements.length).toBeGreaterThan(0)
    for (const r of requirements) expect(r).not.toContain('access')
  })
})

// Ken, 2026-11-05 (task #3294): each route answer was ~45k characters (the whole instructions, every candidate as an object,
// long left-out reasons), 467 a day in one project's PM context. The answer printed is a few thousand.
describe('the route answer an agent reads (task #3294)', () => {
  const names = ['Ken\'s MacBook Pro', 'Mac Studio', 'Tony\'s Mac mini', 'Diego\'s review machine', 'Huy\'s MacBook Air', 'Scholes\'s iMac',
    'Dee\'s Mac mini', 'Amon\'s MacBook Pro', 'Build box', 'Kelvin\'s Mac Studio']
  const machineIds = names.map((_, i) => `${String(i).padStart(2, '0')}f3923b927e36499865b0bd2b6223a4`)
  const gone = ['Old Linux box', 'Windows laptop', 'Lab Raspberry Pi', 'Office NUC', 'Spare Mac mini'].map((name, i) => ({ machineId: `gone${i}9865b0bd2b6223a457f3923b927`, name }))
  const fleetPolicy = { ...policy, models: [...policy.models.filter(m => m.model !== 'sonnet'), { engine: 'codex', model: 'gpt-6-astra', effort: 'xhigh', quality: 3, cost: 3, enabled: true }] }
  const week = (provider: string, used: number) => ({ provider, outcome: 'answered', httpStatus: 200, account: `${provider}-acct`, body: {
    five_hour: { utilization: 20, resets_at: new Date(Date.now() + 3 * 3_600_000).toISOString() },
    seven_day: { utilization: used, resets_at: new Date(Date.now() + 5 * 86_400_000).toISOString() } } })
  // Every machine runs an idle Claude and an idle Codex member, and offers a new agent of each: 40 candidates.
  const crew = machineIds.flatMap((machineId, i) => [
    { id: `${machineId.slice(0, 20)}cl${i}`, name: `Aspen${i}`, machineId, harnessId: `hc${i}`, configuration: { engine: 'claude', model: 'opus', effort: 'high' } },
    { id: `${machineId.slice(0, 20)}cx${i}`, name: `Birch${i}`, machineId, harnessId: `hx${i}`, configuration: { engine: 'codex', model: 'gpt-6-astra', effort: 'xhigh' } }])
  const fleet = () => {
    const state = tab().state
    state.agents = [agents[1]!, ...crew]
    state.conversations[0]!.agentIds = ['lead', ...crew.map(a => a.id)]
    return { revision: 3, state }
  }
  const routing = ['## Routing', '', '### Models', '', ...Array.from({ length: 12 }, (_, i) => `- Rule ${i}: ${'route by quota and machine. '.repeat(12).trim()}`)].join('\n')
  const document = ['# Project instructions', '', '## Roles', '', 'x'.repeat(20_000), '', routing, '', '## Rules for every agent', '', 'y'.repeat(6_000)].join('\n')
  const fleetScoped = (revision = 74) => scoped({ policy: fleetPolicy as typeof policy,
    context: { ok: true, value: { agentId: 'lead', tab: fleet(), policy: { policy: fleetPolicy, warnings: [] },
      machines: [...machineIds.map((machineId, i) => ({ machineId, name: names[i] })), ...gone] } },
    instructions: { ok: true, value: { projectId: 'p1', revision, text: document, notes: [{ id: 'n1', text: 'Release Amon\'s hotfix first.', askedBy: 'Ken' }] } } })
  const fleetOpener = () => opener(id => {
    const i = machineIds.indexOf(id)
    if (i < 0) return { ...daemon(id), request: vi.fn(async () => { throw new Error('offline') }) }
    return daemon(id, { agents: [`hc${i}`, `hx${i}`], usage: { providers: [week('claude', 38), week('codex', 55)] },
      engines: [{ engine: 'claude', installed: true, signedIn: true }, { engine: 'codex', installed: true, signedIn: true }] })
  })
  const fit: PluginMaker = { name: 'Machine fit', make: async () => ({ name: 'Machine fit',
    adjust: c => c.kind === 'new' ? { strength: 0, reason: 'its 3 agents use 61% of the CPU; a build here waits on the others, and its disk has 40 GB free, enough for this task.' } : null }) }
  const fleetRoute = () => routeTask(input, fleetScoped(), where as never, fleetOpener(), () => 'op', undefined, [fit])

  it('is under 8,000 characters for 40 candidates, and every row still says what a start or an assignment needs', async () => {
    const answer = await fleetRoute()
    expect((answer.candidates as unknown[]).length).toBe(40)
    const printed = routeAnswer(answer, { instructionsRead: 74 })
    expect(JSON.stringify(printed).length).toBeLessThan(8_000)
    // The first route of a revision adds only its Routing section.
    expect(JSON.stringify(routeAnswer(answer)).length).toBeLessThan(8_000 + routing.length)
    // Each machine once, with what a start there takes; every candidate a line under it, with what a start or an assignment takes.
    const machines = Object.entries(printed.candidates as Record<string, string[]>)
    expect(machines).toHaveLength(10)
    const lines = machines.flatMap(([machine, rows]) => rows.map(line => ({ machine, line })))
    expect(lines).toHaveLength(40)
    for (const [i, row] of (answer.candidates as Array<{ id: string; kind: string; agentId?: string; machineId: string; engine: string; model: string; effort: string; cwd?: string }>).entries()) {
      const { machine, line } = lines[i]!
      expect(line).toMatch(new RegExp(`^${row.id} ${row.kind === 'new' ? 'new' : `\\S+ \\(agent ${row.agentId}, idle\\)`} · ${row.engine} ${row.model} ${row.effort} · `))
      expect(machine).toMatch(new RegExp(`· machineId ${row.machineId} · 2 agents$`))
    }
    // A pick's why is its reasons, which the answer prints; an unranked route's is the next's first sentence. The record keeps it whole.
    expect(printed).not.toHaveProperty('why')
    expect(printed.next).toMatch(/^Use the pick \(c\d+\), chosen by the project's Routing order/)
    expect(routeAnswer({ ok: true, via: 'only', why: 'Only candidate that can run this task.' })).toMatchObject({ why: 'Only candidate that can run this task.' })
    // The folder a start takes is said once when every machine shares it (a live route: 23 machines, one folder of 85 characters).
    expect(printed.cwd).toBe('/absolute/repo/on/that/machine')
    expect(lines[0]!.line).toMatch(/· fits 62% · resets 5d$/)
    // A note is its first clause, long enough to keep the name or the cause it carries (review of #3169).
    expect(noteClause(PM_ACCOUNT_NOTE(['lamp (Faye)', 'computer (Sage)']))).toBe('This account runs the PM of lamp (Faye) and computer (Sage)')
    expect(noteClause('Its last start failed while preparing the project folder. The start has the details; this does not suspend the engine.'))
      .toBe('Its last start failed while preparing the project folder')
    expect(noteClause('x'.repeat(100))).toBe(`${'x'.repeat(71)}…`)
    // A note is its first clause; the record for Details keeps it whole.
    expect(lines[1]!.line).toMatch(/· Machine fit: its 3 agents use 61% of the CPU$/)
  })
  it('gives the instructions as their Routing section and revision, the whole text on request, and none of it again for a revision read', async () => {
    const answer = await fleetRoute()
    const plain = routeAnswer(answer).instructions as Record<string, unknown>
    expect(plain).toEqual({ revision: 74, routing, notes: ['Release Amon\'s hotfix first.'], more: expect.stringContaining('--full-instructions') })
    expect(JSON.stringify(routeAnswer(answer))).not.toContain('x'.repeat(100))
    expect(routeAnswer(answer, { fullInstructions: true }).instructions).toMatchObject({ revision: 74, text: document, notes: ['Release Amon\'s hotfix first.'] })
    // A revision already read is not sent again; an older one is.
    const read = routeAnswer(answer, { instructionsRead: 74 }).instructions as Record<string, unknown>
    // Its notes still come: adding one does not change the revision (review of #3169).
    expect(read).toEqual({ revision: 74, unchanged: expect.stringContaining('revision 74'), notes: ['Release Amon\'s hotfix first.'] })
    expect(routeAnswer(answer, { instructionsRead: 73 }).instructions).toMatchObject({ revision: 74, routing })
    // The whole text wins over a revision read.
    expect(routeAnswer(answer, { fullInstructions: true, instructionsRead: 74 }).instructions).toMatchObject({ text: document })
    // Instructions with no Routing section are sent whole, never as nothing; unreadable ones say so.
    const bare = { ...answer, instructions: { revision: 2, text: '# Rules\n\nWork by this.', notes: [] } }
    expect(routeAnswer(bare).instructions).toMatchObject({ revision: 2, text: '# Rules\n\nWork by this.' })
    expect(routeAnswer({ ...answer, instructions: undefined, instructionsUnreadable: true })).toMatchObject({ instructionsUnreadable: true })
  })
  it('says a machine\'s own folder only where it differs from the one most share, and no room, which the record keeps', () => {
    const row = (id: string, machineName: string, cwd: string) => ({ id, kind: 'new', machineId: `id-${machineName}`, machineName, cwd, agents: 1, machine: '6 slots free · 14 GB memory free', line: `${id} new` })
    const printed = routeAnswer({ ok: true, candidates: [row('c0', 'a', '/Users/t/repo'), row('c1', 'b', '/home/t/repo'), row('c2', 'c', '/Users/t/repo')] })
    expect(printed).toEqual({ ok: true, cwd: '/Users/t/repo', candidates: { 'a · machineId id-a · 1 agents': ['c0 new'],
      'b · machineId id-b · 1 agents · cwd /home/t/repo': ['c1 new'], 'c · machineId id-c · 1 agents': ['c2 new'] } })
    expect(JSON.stringify(printed)).not.toContain('slots')
    // Members only: no folder to say.
    expect(routeAnswer({ ok: true, candidates: [{ id: 'c0', kind: 'member', machineId: 'm', machineName: 'a', line: 'c0 M' }] })).not.toHaveProperty('cwd')
  })
  it('says each machine left out in one line, and what is left out of no machine when nothing can run', () => {
    const long = 'z'.repeat(400)
    const printed = routeAnswer({ error: 'NO_CANDIDATE', excluded: [{ title: 'Named model', reason: long }, { title: 't', reason: 'x', machine: 'mac' }],
      leftOut: [{ machine: 'mac', reasons: ['Machine is offline or unreachable.', 'b', 'c'] }, { machine: 'pi', reasons: [long] },
        { machine: 'nuc', reasons: ['Machine is offline or unreachable.'] }], warnings: [long] })
    expect(printed).toEqual({ error: 'NO_CANDIDATE', excluded: [`Named model: ${'z'.repeat(119)}…`],
      // Machines left out for the same first reason share its line (a live route: five machines offline).
      leftOut: ['mac (+2 more reasons), nuc: Machine is offline or unreachable.', `pi: ${'z'.repeat(119)}…`], warnings: [`${'z'.repeat(239)}…`] })
  })
  it('tells two machines of one name apart by id', async () => {
    const twin = fleetScoped(), base = twin.getMockImplementation()!
    twin.mockImplementation(async payload => {
      const answer = await base(payload)
      if (payload.action !== 'routeContext') return answer
      const value = answer.value as { machines: Array<{ machineId: string; name: string }> }
      return { ...answer, value: { ...value, machines: value.machines.map(m => m.machineId === machineIds[1] ? { ...m, name: names[0]! } : m) } }
    })
    const answer = await routeTask(input, twin, where as never, fleetOpener(), () => 'op', undefined, [fit])
    const machines = Object.keys(routeAnswer(answer).candidates as Record<string, string[]>)
    expect(machines.filter(m => m.startsWith(names[0]!))).toEqual(expect.arrayContaining([
      `Ken's MacBook Pro · machineId ${machineIds[0]} · 2 agents`, `Ken's MacBook Pro · machineId ${machineIds[1]} · 2 agents`]))
  })
  it('finds the Routing section up to the next heading of its level or higher, its own subsections kept', () => {
    expect(routingSection('# P\n\n## Roles\nr\n\n## Routing\n\n### Models\nm\n\n## Rules\nx')).toBe('## Routing\n\n### Models\nm')
    expect(routingSection('## Routing\nonly')).toBe('## Routing\nonly')
    expect(routingSection('# Routing\nTop\n# Next')).toBe('# Routing\nTop')
    expect(routingSection('## Roles\nRouting is mentioned, not a heading.\n### Routing notes\nn')).toBeNull()
    // A shell comment in a fenced block is not a heading (review of #3169), nor a Routing heading inside one.
    expect(routingSection('## Routing\n```sh\n# start it\nharness start\n```\nafter\n## Rules\nx')).toBe('## Routing\n```sh\n# start it\nharness start\n```\nafter')
    expect(routingSection('```\n## Routing\n```\n## Roles\nr')).toBeNull()
  })
})


// Task #3964 (part 2 of #3576): the route applies the project's machine rules from its rules record, by machine id.
describe('the project\'s machine rules in the route', () => {
  const weekly = { providers: [{ provider: 'claude', outcome: 'answered', httpStatus: 200, account: 'acct', body: {
    five_hour: { utilization: 40, resets_at: new Date(Date.now() + 3 * 3_600_000).toISOString() },
    seven_day: { utilization: 25, resets_at: new Date(Date.now() + 4 * 86_400_000).toISOString() } } }] }
  const open = () => opener(id => daemon(id, { agents: id === 'm1' ? ['h1'] : [], usage: weekly }))
  type Row = { id: string; machineId: string; title: string }
  const rulesAnswer = (machines: unknown[]) => ({ ok: true, value: { projectId: 'lead', revision: 1, machines } })
  const route = async (machines: unknown[] | null, more: { assessment?: Record<string, unknown>; tab?: unknown } = {}) => {
    const s = scoped({ machineRules: machines ? rulesAnswer(machines) : { error: 'SWARM_BUSY', detail: 'Busy' }, ...more.tab ? { tab: more.tab } : {} })
    const answer = await routeTask({ ...input, assessment: { ...input.assessment, ...more.assessment } }, s, where as never, open(), () => 'op')
    return { answer, s, rows: answer.candidates as Row[], machinesOf: () => new Set((answer.candidates as Row[]).map(r => r.machineId)) }
  }
  // Every day, all day: the rule holds whenever the spec runs.
  const always = { days: [0, 1, 2, 3, 4, 5, 6], windows: [['00:00', '23:59']], timeZone: 'UTC' }

  it('reads the rules of the task\'s own project', async () => {
    const { s } = await route([])
    expect(s.mock.calls.find(c => c[0].action === 'machineRules')![0]).toMatchObject({ projectId: 'lead' })
  })

  it('leaves out a machine while it is away, with the rule\'s why', async () => {
    const before = await route([])
    expect(before.machinesOf()).toEqual(new Set(['m1', 'm2']))
    const { answer, machinesOf } = await route([{ machineIds: ['m2'], why: 'Away while its owner travels', away: always }])
    expect(machinesOf()).toEqual(new Set(['m1']))
    expect(answer.leftOut).toEqual(expect.arrayContaining([{ machine: 'mac', reasons: expect.arrayContaining([expect.stringContaining('Away while its owner travels')]) }]))
  })

  it('leaves out an engine a machine refuses, and keeps the others there', async () => {
    const { rows } = await route([{ machineIds: ['m1'], why: 'No Claude there', noEngines: ['claude'] }])
    expect(rows.filter(r => r.machineId === 'm1')).toEqual([])
    expect(rows.some(r => r.machineId === 'm2')).toBe(true)
  })

  it('leaves out a machine where no Reviewer runs for a review, and only for a review', async () => {
    const rule = [{ machineIds: ['m1'], why: 'Its Codex defaults to a local model', noReviews: true }]
    expect((await route(rule, { assessment: { kind: 'review' } })).machinesOf().has('m1')).toBe(false)
    expect((await route(rule, { assessment: { kind: 'test' } })).machinesOf().has('m1')).toBe(true)
    expect((await route(rule)).machinesOf().has('m1')).toBe(true)
  })

  it('picks a lane\'s first machine over an equal other, and says so', async () => {
    const plain = await route([])
    const picked = (a: Answer) => (a.candidates as Row[]).find(r => r.id === (a.pick as { id: string }).id)!.machineId
    const other = picked(plain.answer) === 'm1' ? 'm2' : 'm1'
    const phone = { revision: 3, state: { id: 'tab', agents, conversations: conversations().map(c => c.id === 'g1' ? { ...c, handOutLane: 'mobile' } : c) } }
    const { answer } = await route([{ machineIds: [other], why: 'The phone setup is there', first: { lanes: ['mobile'] } }], { tab: phone })
    expect(picked(answer)).toBe(other)
    expect((answer.reasons as string[])[0]).toContain('The phone setup is there')
    // Another lane's task is not moved.
    expect(picked((await route([{ machineIds: [other], why: 'The phone setup is there', first: { lanes: ['mobile'] } }])).answer)).toBe(picked(plain.answer))
  })

  it('says the instructions\' Machines rules still apply when the rules cannot be read, and no longer when they are', async () => {
    const unread = await route(null)
    expect(unread.machinesOf()).toEqual(new Set(['m1', 'm2']))
    expect(unread.answer.warnings).toEqual(expect.arrayContaining([expect.stringMatching(/machine rules could not be read.*Machines rules.*still apply/)]))
    expect(String(unread.answer.next)).toMatch(/Machines rules.*still apply/s)
    const read = await route([])
    expect(String(read.answer.next)).not.toMatch(/still apply/)
  })
})