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.

9.1 KB
Raw Blame
import { beforeAll, describe, expect, it } from 'vitest'
import { importReleaseCheckoutModule, materializeReleaseCheckout } from './release-checkout'

// Pin the pre-proof host and run both client ledgers over real retirement frames; optional fields alone cannot prove behavioral compatibility.
// Cover renderer-first removal where the later PTY exit finds no persisted surface to retire.
const PRE_STACK_REF = 'v1.4.199'

const SUITE_TIMEOUT_MS = 180_000

const WORKTREE_ID = 'repo::/worktree'
const LEAF_ID = '11111111-1111-4111-8111-111111111111'
const PARENT_TAB_ID = 'tab'
const PTY_ID = 'pty-left'
const TERMINAL_HANDLE = 'remote:terminal-handle-1'

type Snapshot = {
  worktree: string
  publicationEpoch: string
  snapshotVersion: number
  activeGroupId: null
  activeTabId: string | null
  activeTabType: string | null
  tabs: Record<string, unknown>[]
  retiredTerminalSurfaces?: Record<string, unknown>[]
}

type ProofLedger = {
  appendRetiredTerminalSurfaceProofs: (
    existing: readonly Record<string, unknown>[] | undefined,
    retired: readonly Record<string, unknown>[]
  ) => Record<string, unknown>[]
  dropRetirementProofsForLiveSurfaces: (
    retired: readonly Record<string, unknown>[],
    tabs: readonly Record<string, unknown>[]
  ) => Record<string, unknown>[]
}

type HostProofPublisher = {
  attachRetirementProofsToSnapshot?: (
    snapshot: Snapshot,
    proofs: readonly Record<string, unknown>[]
  ) => Snapshot | null
  retireTerminalSurfacesFromSnapshot: (
    args: Record<string, unknown>
  ) => { snapshot: Snapshot } | null
}

type Build = {
  label: string
  ledger: ProofLedger
  host: HostProofPublisher
}

/** The surface as the host still holds it, before the close's two halves land. */
function liveSnapshot(): Snapshot {
  return {
    worktree: WORKTREE_ID,
    publicationEpoch: 'renderer',
    snapshotVersion: 1,
    activeGroupId: null,
    activeTabId: `tab::${LEAF_ID}`,
    activeTabType: 'terminal',
    tabs: [
      {
        type: 'terminal',
        id: `tab::${LEAF_ID}`,
        parentTabId: PARENT_TAB_ID,
        leafId: LEAF_ID,
        ptyId: PTY_ID,
        title: 'Left',
        isActive: true
      }
    ]
  }
}

// Renderer-first removal republishes without the surface; the later PTY exit finds nothing left to persist.
function snapshotAfterRendererRepublished(): Snapshot {
  return { ...liveSnapshot(), snapshotVersion: 2, tabs: [], activeTabId: null, activeTabType: null }
}

function exitProof(): Record<string, unknown> {
  return {
    parentTabId: PARENT_TAB_ID,
    leafId: LEAF_ID,
    ptyId: PTY_ID,
    terminal: TERMINAL_HANDLE,
    incarnationId: 'inc-1'
  }
}

async function loadBuild(ref: string | null): Promise<Build> {
  if (ref === null) {
    const [ledger, proof, retirement] = await Promise.all([
      import('../../../src/shared/terminal-retirement-proof-ledger'),
      import('../../../src/main/runtime/mobile-session-terminal-retirement-proof'),
      import('../../../src/main/runtime/mobile-session-terminal-retirement')
    ])
    return {
      label: 'stack',
      // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: a dynamic import is typed unknown; this module is the proof ledger by path.
      ledger: ledger as unknown as ProofLedger,
      // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: a dynamic import is typed unknown; the two modules together are the host publisher surface this spec drives.
      host: { ...proof, ...retirement } as unknown as HostProofPublisher
    }
  }
  const checkout = await materializeReleaseCheckout(ref)
  const [ledger, proof, retirement] = await Promise.all([
    importReleaseCheckoutModule(checkout, 'src/shared/terminal-retirement-proof-ledger.ts'),
    importReleaseCheckoutModule(
      checkout,
      'src/main/runtime/mobile-session-terminal-retirement-proof.ts'
    ),
    importReleaseCheckoutModule(checkout, 'src/main/runtime/mobile-session-terminal-retirement.ts')
  ])
  return {
    label: ref,
    // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: the release checkout is loaded by path, so its exports arrive unknown.
    ledger: ledger as unknown as ProofLedger,
    // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: the release checkout is loaded by path, so its exports arrive unknown.
    host: { ...proof, ...retirement } as unknown as HostProofPublisher
  }
}

// Null publication after renderer removal is the stuck-pane defect this old-host probe must expose.
function hostPublishesOnExit(build: Build, snapshot: Snapshot): Snapshot | null {
  const attach = build.host.attachRetirementProofsToSnapshot
  if (typeof attach !== 'function') {
    // Derived, not written down: this build's only route to a proof is the removal helper,
    // and with nothing left to remove it declines to produce a frame.
    return (
      build.host.retireTerminalSurfacesFromSnapshot({
        snapshot,
        ptyId: PTY_ID,
        exactSurfaces: [],
        exactOnly: true,
        retirementProofs: [exitProof()]
      })?.snapshot ?? null
    )
  }
  return attach(snapshot, [exitProof()])
}

/** What this build's client retains after the host frame, i.e. the evidence it can act on. */
function clientRetains(build: Build, frame: Snapshot | null): Record<string, unknown>[] {
  if (frame === null) {
    return []
  }
  return build.ledger.dropRetirementProofsForLiveSurfaces(
    build.ledger.appendRetiredTerminalSurfaceProofs(undefined, frame.retiredTerminalSurfaces ?? []),
    frame.tabs
  )
}

let preStack: Build
let stack: Build

beforeAll(async () => {
  ;[preStack, stack] = await Promise.all([loadBuild(PRE_STACK_REF), loadBuild(null)])
}, SUITE_TIMEOUT_MS)

describe('cross-version session-tabs retirement proof', () => {
  it('pairs the stack against a real pre-stack release', () => {
    expect(preStack.label).toBe(PRE_STACK_REF)
    expect(typeof preStack.ledger.dropRetirementProofsForLiveSurfaces).toBe('function')
    expect(typeof stack.ledger.dropRetirementProofsForLiveSurfaces).toBe('function')
    // Prove distinct build identities or every skew cell could silently test the same module twice.
    expect(preStack.ledger).not.toBe(stack.ledger)
    expect(preStack.ledger.dropRetirementProofsForLiveSurfaces).not.toBe(
      stack.ledger.dropRetirementProofsForLiveSurfaces
    )
    // The pinned old host cannot publish proof-only frames; this must not merely observe a helper declining once.
    expect(preStack.host.attachRetirementProofsToSnapshot).toBeUndefined()
    expect(typeof stack.host.attachRetirementProofsToSnapshot).toBe('function')
  })

  it('old host against old client publishes no proof on the renderer-first close (the defect)', () => {
    const frame = hostPublishesOnExit(preStack, snapshotAfterRendererRepublished())
    expect(frame).toBeNull()
    expect(clientRetains(preStack, frame)).toEqual([])
  })

  it('new host against new client publishes a proof the client retains (the fix)', () => {
    const frame = hostPublishesOnExit(stack, snapshotAfterRendererRepublished())
    expect(frame).not.toBeNull()
    expect(clientRetains(stack, frame)).toEqual([exitProof()])
  })

  it('new host against OLD client: the old client acts on the proof-only frame', () => {
    const frame = hostPublishesOnExit(stack, snapshotAfterRendererRepublished())
    expect(frame).not.toBeNull()
    // An old client must act on the new frame or the dead pane remains in its tab bar.
    expect(clientRetains(preStack, frame)).toEqual([exitProof()])
  })

  it('new host bumps snapshotVersion so a version-gating old client accepts the frame', () => {
    const before = snapshotAfterRendererRepublished()
    const frame = hostPublishesOnExit(stack, before)
    // A client that drops a frame whose version did not advance would silently ignore the
    // proof; this is what makes the proof-only frame reachable at all.
    expect(frame?.snapshotVersion).toBeGreaterThan(before.snapshotVersion)
  })

  it('old host against NEW client degrades to the two-inventory route, with no crash', () => {
    const frame = hostPublishesOnExit(preStack, snapshotAfterRendererRepublished())
    expect(frame).toBeNull()
    expect(clientRetains(stack, frame)).toEqual([])
  })

  it('both builds drop a proof whose surface is published live again, identically', () => {
    const stillLive = liveSnapshot()
    const proofs = [exitProof()]
    // Rule 3 hazard: a proof naming a surface the host is still publishing must not retire
    // it. Both builds must agree, or a skewed pairing retires a live pane.
    expect(preStack.ledger.dropRetirementProofsForLiveSurfaces(proofs, stillLive.tabs)).toEqual([])
    expect(stack.ledger.dropRetirementProofsForLiveSurfaces(proofs, stillLive.tabs)).toEqual([])
  })

  it('re-delivering the same exit does not fan out a second frame', () => {
    const first = hostPublishesOnExit(stack, snapshotAfterRendererRepublished())
    expect(first).not.toBeNull()
    // A version bump carrying nothing new would wake every paired client for no reason.
    // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: the assertion above already proves `first` is a published snapshot, not null.
    expect(hostPublishesOnExit(stack, first as Snapshot)).toBeNull()
  })
})