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.

7.4 KB
Raw Blame
import { describe, expect, it } from 'vitest'
import {
  bridgedParityMembershipDrift,
  bridgedParityTallyDrift,
  classifyBridgedParity,
  BRIDGED_PARITY_BASELINE,
  BRIDGED_PARITY_MEMBERS,
  type BridgedParityClass,
  type BridgedParityEvidence,
  type BridgedParityTally
} from './divergence-classes'

const base: BridgedParityEvidence = {
  fixedByReplyMeta: false,
  threwWhileRecording: false,
  divergingFields: [],
  absentResultExplainsResiduals: false,
  paramsMismatch: null,
  refusalReleasedStream: false
}

const droppedUndefinedKey: BridgedParityEvidence = {
  ...base,
  threwWhileRecording: true,
  paramsMismatch: {
    step: 'linear.listIssues#1',
    undefinedValuedKeys: ['.workspaceId'],
    differingKeys: ['.workspaceId']
  }
}

describe('classifying one diverging golden', () => {
  it('names the missing field first, but only where supplying it was enough', () => {
    const absent = {
      ...base,
      absentResultExplainsResiduals: true,
      divergingFields: ['sender[0].settlement']
    }
    expect(classifyBridgedParity({ ...absent, fixedByReplyMeta: true })).toBe('reply-meta-required')
    expect(classifyBridgedParity(absent)).toBe('result-absent-settlement')
  })

  it('splits the absent-result partition by what moved first', () => {
    const absent = { ...base, absentResultExplainsResiduals: true }
    expect(
      classifyBridgedParity({ ...absent, divergingFields: ['sender[0].settlement', 'effects'] })
    ).toBe('result-absent-settlement')
    expect(classifyBridgedParity({ ...absent, divergingFields: ['effects'] })).toBe(
      'result-absent-observation'
    )
    expect(classifyBridgedParity({ ...absent, divergingFields: ['checkpoints'] })).toBe(
      'result-absent-observation'
    )
  })

  it('names a throw only when every param that moved is one the scenario valued `undefined`', () => {
    expect(classifyBridgedParity(droppedUndefinedKey)).toBe('params-undefined')
    expect(classifyBridgedParity({ ...base, threwWhileRecording: true })).toBe('unclassified')
  })

  it('refuses the class to a run where something else moved in the same params', () => {
    // Seed a real extra-key bug inside undefined-param scenarios so classification must compare moved keys, not just scenario membership.
    expect(
      classifyBridgedParity({
        ...droppedUndefinedKey,
        paramsMismatch: {
          step: 'linear.listIssues#1',
          undefinedValuedKeys: ['.workspaceId'],
          differingKeys: ['.seeded', '.workspaceId']
        }
      })
    ).toBe('unclassified')
  })

  it('refuses the class to a throw that moved no param at all', () => {
    expect(
      classifyBridgedParity({
        ...droppedUndefinedKey,
        paramsMismatch: {
          step: 'linear.listIssues#1',
          undefinedValuedKeys: ['.workspaceId'],
          differingKeys: []
        }
      })
    ).toBe('unclassified')
  })

  it('names a throw that came of the page releasing a stream it refused a frame on', () => {
    const released = {
      ...base,
      threwWhileRecording: true,
      absentResultExplainsResiduals: true,
      refusalReleasedStream: true
    }
    expect(classifyBridgedParity(released)).toBe('result-absent-stream-release')
    expect(classifyBridgedParity({ ...released, refusalReleasedStream: false })).toBe(
      'unclassified'
    )
  })

  it('requires every difference to be a recorder ordinal before naming that class', () => {
    expect(classifyBridgedParity({ ...base, divergingFields: ['sender[0].ordinal'] })).toBe(
      'write-ordinal'
    )
    expect(
      classifyBridgedParity({
        ...base,
        absentResultExplainsResiduals: false,
        divergingFields: ['payloads[0].ordinal', 'effects[0].value.ordinal']
      })
    ).toBe('unclassified')
    expect(classifyBridgedParity({ ...base, divergingFields: ['state.ordinal'] })).toBe(
      'unclassified'
    )
  })

  it('keeps an explicit legacy membership label distinct from strict certification', () => {
    const mixed = {
      ...base,
      divergingFields: ['sender[0].ordinal', 'payloads[1].json']
    }
    expect(classifyBridgedParity(mixed)).toBe('unclassified')
    expect(
      classifyBridgedParity({
        ...mixed,
        legacyBaseline: { firstField: 'sender[0].ordinal', scriptsAbsentResultReply: true }
      })
    ).toBe('write-ordinal')
  })

  it('refuses to name a golden that diverged in no field at all', () => {
    expect(classifyBridgedParity(base)).toBe('unclassified')
  })
})

describe('what the pin still admits', () => {
  const classes = Object.keys(BRIDGED_PARITY_BASELINE).filter(
    (name): name is BridgedParityClass => name !== 'identical'
  )

  function asPinned(): Map<string, readonly string[]> {
    const run = new Map<string, readonly string[]>()
    for (const name of classes) {
      const pinned = BRIDGED_PARITY_MEMBERS[name]
      if (pinned !== undefined) {
        run.set(name, pinned)
      }
    }
    return run
  }

  it('holds such a class to which goldens are in it, not only to how many', () => {
    // Swap class membership without changing totals; per-id pins must catch real refusals hidden by scenario-wide predicates.
    expect(bridgedParityMembershipDrift(asPinned())).toEqual([])
    const pinned = BRIDGED_PARITY_MEMBERS['write-ordinal'] ?? []
    const traded = asPinned()
    traded.set('write-ordinal', [...pinned.slice(1), 'matrix-regressed-golden-1-1'])
    const drift = bridgedParityMembershipDrift(traded)
    expect(drift.length).toBe(1)
    expect(drift[0]).toContain('matrix-regressed-golden-1-1')
    expect(drift[0]).toContain(pinned[0])
  })

  it('says nothing about a class too large for the run to name', () => {
    const run = asPinned()
    run.set('result-absent-settlement', ['whatever-diverged'])
    expect(bridgedParityMembershipDrift(run)).toEqual([])
  })

  function asCounted(): BridgedParityTally {
    const counts: Record<BridgedParityClass, number> = {
      'reply-meta-required': 0,
      'result-absent-settlement': 0,
      'result-absent-observation': 0,
      'result-absent-stream-release': 0,
      'params-undefined': 0,
      'write-ordinal': 0,
      unclassified: 0
    }
    for (const name of classes) {
      counts[name] = BRIDGED_PARITY_BASELINE[name]
    }
    return { identical: BRIDGED_PARITY_BASELINE.identical, counts }
  }

  it('goes red on a golden that stopped diverging, which every other check lets through', () => {
    // Excluded-to-identical moves leave broad parity gates satisfied; exact tallies must detect that direction too.
    const tally = asCounted()
    const moved: BridgedParityTally = {
      identical: tally.identical + 1,
      counts: {
        ...tally.counts,
        'result-absent-settlement': tally.counts['result-absent-settlement'] - 1
      }
    }
    const drift = bridgedParityTallyDrift(moved)
    expect(drift.length).toBe(2)
    expect(drift.join('\n')).toContain(
      `identical: pinned ${tally.identical}, ran ${moved.identical}`
    )
    expect(drift.join('\n')).toContain('result-absent-settlement: pinned 350, ran 349')
  })

  it('goes red on a class that grew and on the corpus losing a golden', () => {
    const tally = asCounted()
    expect(
      bridgedParityTallyDrift({
        ...tally,
        counts: { ...tally.counts, unclassified: 1 }
      })
    ).toEqual(['unclassified: pinned 0, ran 1'])
    expect(bridgedParityTallyDrift({ ...tally, identical: tally.identical - 1 })).toEqual([
      `identical: pinned ${tally.identical}, ran ${tally.identical - 1}`
    ])
  })
})