Skip to content

The agent that grows with you

7.1 KB
Raw Blame
import { describe, expect, it } from 'vitest'

import {
  registerAgentTerminalWriter,
  syncAgentTerminalSnapshot,
  writeAgentTerminalChunk
} from './agent-terminal-stream'

// Module-level state is process-keyed and shared across cases, so every test uses
// unique procIds.
const CHUNK = 'x'.repeat(64_000)

function replay(proc: string): string {
  let out = ''
  const stop = registerAgentTerminalWriter(proc, chunk => (out += chunk))

  stop()

  return out
}

function runFinishedProc(id: string, chunks = 6): void {
  for (let i = 0; i < chunks; i++) {
    writeAgentTerminalChunk(id, CHUNK)
  }
}

describe('agent terminal retention is bounded', () => {
  it('enforces the cold budget when mounted writers close without another output event', () => {
    const stops = Array.from({ length: 40 }, (_, index) => registerAgentTerminalWriter(`closed-${index}`, () => {}))

    for (let index = 0; index < stops.length; index++) {
      runFinishedProc(`closed-${index}`)
    }

    for (const stop of stops) {
      stop()
    }

    const retained = stops.map((_, index) => replay(`closed-${index}`))

    expect(retained.reduce((chars, output) => chars + output.length, 0)).toBeLessThanOrEqual(2_000_000)
    expect(retained.at(-1)).toBe(CHUNK.repeat(4))
  })

  it('a finished process does not pin its backlog for the life of the renderer', () => {
    // 200 background commands, none mounted — the shape that grew to ~93 MB.
    for (let i = 0; i < 200; i++) {
      runFinishedProc(`bound-${i}`)
    }

    // The oldest must be gone: reopening it replays nothing until the registry
    // snapshot re-seeds it.
    let replayed = ''
    const stop = registerAgentTerminalWriter('bound-0', chunk => (replayed += chunk))

    stop()
    expect(replayed).toBe('')
  })

  it('keeps the most recent process replayable', () => {
    for (let i = 0; i < 200; i++) {
      runFinishedProc(`recent-${i}`)
    }

    let replayed = ''
    const stop = registerAgentTerminalWriter('recent-199', chunk => (replayed += chunk))

    stop()
    expect(replayed.length).toBeGreaterThan(0)
  })

  it('never evicts a process whose terminal is mounted', () => {
    // A long-lived mounted tab, then a flood of unrelated finished processes.
    const mounted = 'mounted-proc'

    registerAgentTerminalWriter(mounted, () => {})
    runFinishedProc(mounted)

    for (let i = 0; i < 200; i++) {
      runFinishedProc(`flood-${i}`)
    }

    // Still replayable: on-screen state is not a cache.
    let replayed = ''
    const stop = registerAgentTerminalWriter(mounted, chunk => (replayed += chunk))

    stop()
    expect(replayed.length).toBeGreaterThan(0)
  })

  it('recency is the LAST write, not the first', () => {
    // Small chunks so the CHARACTER ceiling never fires: this case is purely about
    // the entry ceiling and the order eviction walks. `backlog` insertion order is
    // the LRU clock, and a plain re-set leaves a key in its ORIGINAL slot — so
    // without the delete-then-set refresh, touching a process would not protect it.
    const line = 'x'.repeat(64)

    for (let i = 0; i < 20; i++) {
      writeAgentTerminalChunk(`age-${i}`, `age-${i}-MARK ${line}\n`)
    }

    // Touch the OLDEST while it is still resident, then overflow the ceiling.
    writeAgentTerminalChunk('age-0', 'TOUCHED\n')

    for (let i = 20; i < 32; i++) {
      writeAgentTerminalChunk(`age-${i}`, `age-${i}-MARK ${line}\n`)
    }

    // The touched process kept its ORIGINAL content (it was never dropped and
    // re-created); its untouched neighbour from the same era is gone.
    expect(replay('age-0')).toContain('age-0-MARK')
    expect(replay('age-1')).toBe('')
  })

  it('a full-tail reset is a write: it refreshes recency', () => {
    // A snapshot whose rolling tail slid resets the backlog wholesale. That process
    // was just synced, so it must move to the LRU tail like a chunk write does.
    const line = 'x'.repeat(64)

    // Seeding through the snapshot sets each process's delta fence, so a later
    // snapshot that shares no prefix with it takes the reset path.
    for (let i = 0; i < 20; i++) {
      syncAgentTerminalSnapshot(`slide-${i}`, `slide-${i}-MARK ${line}\n`)
    }

    syncAgentTerminalSnapshot('slide-0', 'SLID TAIL')

    for (let i = 20; i < 32; i++) {
      writeAgentTerminalChunk(`slide-${i}`, `slide-${i}-MARK ${line}\n`)
    }

    expect(replay('slide-0')).toContain('SLID TAIL')
    expect(replay('slide-1')).toBe('')
  })

  it('full-tail resets alone stay under the character ceiling', () => {
    // Nothing but resets: no chunk write follows to run eviction on their behalf.
    const tail = 'y'.repeat(300_000)

    for (let i = 0; i < 12; i++) {
      syncAgentTerminalSnapshot(`reset-${i}`, `reset-${i}\n`)
    }

    for (let i = 0; i < 12; i++) {
      syncAgentTerminalSnapshot(`reset-${i}`, `${i}${tail}`)
    }

    expect(replay('reset-0')).toBe('')
    expect(replay('reset-11')).toContain(tail.slice(0, 1000))
  })

  it('a catch-up snapshot does not re-print bytes already streamed by live chunks', () => {
    // Live `agent.terminal.output` chunks advance the backlog but never the
    // lastSnapshots fence. A catch-up snapshot that contains everything shown so
    // far matches BOTH prefix checks, and the fence-first order emitted
    // output.slice(previous.length) — re-appending bytes the chunks had already
    // written, so the screen showed `AAABBBAAABBBCCC` for `AAABBB` + `CCC`.
    const proc = 'dup-proc'
    let screen = ''

    const stop = registerAgentTerminalWriter(proc, chunk => {
      screen = chunk.startsWith('\x1bc') ? chunk.slice(2) : screen + chunk
    })

    writeAgentTerminalChunk(proc, 'AAA')
    writeAgentTerminalChunk(proc, 'BBB')
    syncAgentTerminalSnapshot(proc, 'AAABBBCCC')
    stop()
    expect(screen).toBe('AAABBBCCC')
  })

  it('an evicted process does not diff against a tail it no longer has', () => {
    // `lastSnapshots` is the delta fence for `backlog`. If eviction dropped the
    // backlog but KEPT the fence, the next snapshot takes the `startsWith(previous)`
    // path and writes only the delta — so the screen shows `BBB` where the process
    // actually printed `AAABBB`.
    const proc = 'fence-proc'

    writeAgentTerminalChunk(proc, 'AAA')
    syncAgentTerminalSnapshot(proc, 'AAA')

    for (let i = 0; i < 200; i++) {
      runFinishedProc(`fence-flood-${i}`)
    }

    let screen = ''

    const stop = registerAgentTerminalWriter(proc, chunk => {
      screen = chunk.startsWith('\x1bc') ? chunk.slice(2) : screen + chunk
    })

    syncAgentTerminalSnapshot(proc, 'AAABBB')
    stop()
    expect(screen).toContain('AAABBB')
  })

  it('an evicted process re-seeds cleanly from the registry snapshot', () => {
    // Eviction drops backlog AND its lastSnapshots delta fence together, so the
    // next snapshot must reset the screen rather than diff against a missing tail.
    for (let i = 0; i < 200; i++) {
      runFinishedProc(`reseed-${i}`)
    }

    let screen = ''

    const stop = registerAgentTerminalWriter('reseed-0', chunk => {
      screen = chunk.startsWith('\x1bc') ? chunk.slice(2) : screen + chunk
    })

    syncAgentTerminalSnapshot('reseed-0', 'recovered output')
    stop()
    expect(screen).toContain('recovered output')
  })
})