Skip to content

armgit / orca

Public Imported from GitHub
0

Orca is the place for working with a fleet of agents (e.g. Grok Bot, Claude Code). Run any coding agent with your own subscription. Available on desktop, mobile and remote runtime.

5.9 KB
Raw Blame
import { describe, expect, it } from 'vitest'
import {
  continueMainAgentStatus,
  mainAgentTurnInterrupted,
  foldAgentLeadStatus,
  isAgentTimeAccruing,
  isAgentStatusHeldOpenByChildWork
} from './agent-lead-status-fold'

describe('foldAgentLeadStatus', () => {
  it('keeps a blocked lead while child work is working', () => {
    expect(foldAgentLeadStatus({ leadState: 'blocked', childWorkLiveness: 'working' })).toEqual({
      stateName: 'blocked'
    })
  })

  it('reads a settled lead with live agent work as working', () => {
    expect(foldAgentLeadStatus({ leadState: 'done', childWorkLiveness: 'working' })).toEqual({
      stateName: 'working'
    })
  })

  it('reads a settled lead with only watch loops as monitoring', () => {
    expect(foldAgentLeadStatus({ leadState: 'done', childWorkLiveness: 'monitoring' })).toEqual({
      stateName: 'working',
      workingMode: 'monitoring'
    })
  })

  it('settles when nothing is running', () => {
    expect(foldAgentLeadStatus({ leadState: 'done', childWorkLiveness: null })).toEqual({
      stateName: 'done'
    })
  })

  describe('a child waiting on a human', () => {
    it('makes a working or settled main agent wait', () => {
      for (const leadState of ['working', 'done'] as const) {
        expect(foldAgentLeadStatus({ leadState, childWorkLiveness: 'waiting' })).toEqual({
          stateName: 'waiting'
        })
      }
    })

    it("yields to the main agent's own request for a human, in the main agent's own vocabulary", () => {
      for (const leadState of ['waiting', 'blocked'] as const) {
        expect(foldAgentLeadStatus({ leadState, childWorkLiveness: 'waiting' })).toEqual({
          stateName: leadState
        })
      }
    })
  })
})

describe('mainAgentTurnInterrupted', () => {
  it('reads only a cancellation verdict as an interrupt', () => {
    expect(mainAgentTurnInterrupted({ outcome: 'cancellation' })).toBe(true)
    expect(mainAgentTurnInterrupted({ outcome: 'failure' })).toBe(false)
    // The legacy flag means the user stopped it; a newer request's replacement names no one.
    expect(mainAgentTurnInterrupted({ outcome: 'superseded' })).toBe(false)
    expect(mainAgentTurnInterrupted({})).toBe(false)
    expect(mainAgentTurnInterrupted(undefined)).toBe(false)
  })

  // Older readers take the flag as "the user stopped it": a crash or an unproven end never sets it.
  it('stays down for a turn cut off by a crash and for an end nobody can prove', () => {
    expect(mainAgentTurnInterrupted({ outcome: 'interruption' })).toBe(false)
    expect(mainAgentTurnInterrupted({ outcome: 'unconfirmed' })).toBe(false)
  })
})

describe('isAgentStatusHeldOpenByChildWork', () => {
  it('is true only when a settled main agent sits under a row that is not settled', () => {
    expect(
      isAgentStatusHeldOpenByChildWork({ state: 'working', mainAgent: { state: 'done' } })
    ).toBe(true)
    expect(isAgentStatusHeldOpenByChildWork({ state: 'done', mainAgent: { state: 'done' } })).toBe(
      false
    )
    expect(
      isAgentStatusHeldOpenByChildWork({ state: 'working', mainAgent: { state: 'working' } })
    ).toBe(false)
    // No main agent fact means no claim: an old host's row is never read as child-held.
    expect(isAgentStatusHeldOpenByChildWork({ state: 'working' })).toBe(false)
  })
})

describe('isAgentTimeAccruing', () => {
  it('accrues while the row works for the main agent or its live agent child work', () => {
    expect(isAgentTimeAccruing({ state: 'working' })).toBe(true)
  })

  it('pauses while the row waits on the user, whoever raised the prompt', () => {
    expect(isAgentTimeAccruing({ state: 'waiting' })).toBe(false)
    expect(isAgentTimeAccruing({ state: 'blocked' })).toBe(false)
  })

  it('does not accrue for a watch loop or a settled row', () => {
    expect(isAgentTimeAccruing({ state: 'working', workingMode: 'monitoring' })).toBe(false)
    expect(isAgentTimeAccruing({ state: 'done' })).toBe(false)
  })

  it('relies on the fold emitting monitoring only for a settled main agent', () => {
    // Monitoring requires a settled lead; live lead turns must still accrue time.
    const leadStates = ['working', 'waiting', 'blocked', 'done'] as const
    const liveness = ['waiting', 'working', 'monitoring', null] as const
    for (const leadState of leadStates) {
      for (const childWorkLiveness of liveness) {
        const folded = foldAgentLeadStatus({ leadState, childWorkLiveness })
        if (folded.workingMode === 'monitoring') {
          expect(leadState).toBe('done')
        }
        const accrues = isAgentTimeAccruing({
          state: folded.stateName,
          workingMode: folded.workingMode
        })
        // A child waiting on a human pauses the row, whatever the main agent is doing.
        expect(accrues).toBe(
          childWorkLiveness !== 'waiting' &&
            (leadState === 'working' || (leadState === 'done' && childWorkLiveness === 'working'))
        )
      }
    }
  })
})

describe('continueMainAgentStatus', () => {
  it('keeps the clock across an unchanged state and restarts it on a change', () => {
    const first = continueMainAgentStatus(undefined, { state: 'working' }, 10)
    expect(first).toEqual({ state: 'working', stateStartedAt: 10 })
    expect(continueMainAgentStatus(first, { state: 'working' }, 20)).toEqual({
      state: 'working',
      stateStartedAt: 10
    })
    expect(continueMainAgentStatus(first, { state: 'done', outcome: 'failure' }, 30)).toEqual({
      state: 'done',
      outcome: 'failure',
      stateStartedAt: 30
    })
  })

  it('lets a caller that knows the instant win, and never carries a verdict onto a live state', () => {
    expect(continueMainAgentStatus(undefined, { state: 'done', stateStartedAt: 4 }, 30)).toEqual({
      state: 'done',
      stateStartedAt: 4
    })
    for (const state of ['working', 'waiting', 'blocked'] as const) {
      expect(continueMainAgentStatus(undefined, { state, outcome: 'cancellation' }, 30)).toEqual({
        state,
        stateStartedAt: 30
      })
    }
  })
})