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.3 KB
Raw Blame
// #7951/#7240 (v1.4.136): reveal can fit a new grid while an applied-size read still holds the old target, then incorrectly resize PTY backward.
// Busy daemon/SSH ordering exposes the stale forward; idle output/layout provide no later correction until manual resize.
// Exercise natural ordering and delayed dispatch through pty-applied-size-read-e2e-delay.ts. Fast local follow-up may heal in milliseconds;
// pty-size-reassertion.test.ts deterministically rejects the stale forward, while this arm catches persistent desync along the full reveal path.

import type { Page, TestInfo } from '@stablyai/playwright-test'
import { writeFileSync } from 'node:fs'
import path from 'node:path'
import { test, expect } from './helpers/orca-app'
import {
  ensureTerminalVisible,
  getActiveWorktreeId,
  getAllWorktreeIds,
  switchToWorktree,
  waitForActiveWorktree,
  waitForSessionReady
} from './helpers/store'
import {
  sendToTerminal,
  waitForActivePanePtyId,
  waitForActiveTerminalManager
} from './helpers/terminal'
import { waitForPtyShellEcho } from './terminal-pty-readiness'

type StaleResizeReproWindow = Window & {
  __paneManagers?: Map<
    string,
    {
      getPanes?: () => {
        container?: { dataset?: { ptyId?: string } }
        terminal?: { cols?: number; rows?: number }
      }[]
    }
  >
  __e2ePtyAppliedSizeReadDelayMs?: number
}

type GridSnapshot = {
  xterm: { cols: number; rows: number } | null
  applied: { cols: number; rows: number } | null
}

type CycleFailure = {
  cycle: number
  snapshot: GridSnapshot
}

const CONVERGE_TIMEOUT_MS = 6_000
// Sweep read delay across fit resize→ResizeObserver follow-up; idle local runs heal in ~30–50ms, loaded/remote runs may not.
const GETSIZE_DELAY_SWEEP_MS = [15, 20, 25, 30, 40, 60]
const VIEWPORTS = [
  { width: 1280, height: 800 },
  { width: 940, height: 640 },
  { width: 1120, height: 760 }
]

function bottomBarTuiScript(runId: string): string {
  // Redraw only on SIGWINCH: periodic output would trigger grid-drift healing and mask idle-TUI desync.
  // Assert applied pty:getSize against xterm, not the illustrative bar: ConPTY can resize correctly without notifying a long-lived Node process.
  return [
    'const draw = () => {',
    '  const rows = process.stdout.rows || 24',
    '  const cols = process.stdout.columns || 80',
    `  const bar = 'BOTTOM_BAR_${runId} rows=' + rows + ' cols=' + cols + ' ' + '='.repeat(200)`,
    "  process.stdout.write('\\x1b7\\x1b[' + rows + ';1H\\x1b[2K' + bar.slice(0, Math.max(1, cols - 1)) + '\\x1b8')",
    '}',
    "process.stdout.on('resize', draw)",
    'draw()',
    'setTimeout(() => process.exit(0), 600000)'
  ].join('\n')
}

async function readGridSnapshot(page: Page, ptyId: string): Promise<GridSnapshot> {
  return page.evaluate(async (ptyId) => {
    const win = window as StaleResizeReproWindow
    let xterm: { cols: number; rows: number } | null = null
    for (const manager of win.__paneManagers?.values() ?? []) {
      for (const pane of manager.getPanes?.() ?? []) {
        if (pane.container?.dataset?.ptyId === ptyId) {
          xterm = { cols: pane.terminal?.cols ?? 0, rows: pane.terminal?.rows ?? 0 }
        }
      }
    }
    // Direct pty.getSize probes stay fast; the seam delays only product readback wiring.
    const applied = (await window.api?.pty?.getSize?.(ptyId)) ?? null
    return { xterm, applied }
  }, ptyId)
}

async function closeRightSidebarAndFeatureTips(page: Page): Promise<void> {
  await page.evaluate(() => {
    const store = window.__store
    if (!store) {
      return
    }
    store
      .getState()
      .markFeatureTipsSeen(['orca-cli', 'cmd-j-palette', 'voice-dictation', 'agent-session-search'])
    if (store.getState().rightSidebarOpen) {
      store.getState().setRightSidebarOpen(false)
    }
  })
}

function gridsConverged(snapshot: GridSnapshot): boolean {
  return (
    snapshot.xterm !== null &&
    snapshot.applied !== null &&
    snapshot.xterm.cols > 0 &&
    snapshot.xterm.rows > 0 &&
    snapshot.applied.cols === snapshot.xterm.cols &&
    snapshot.applied.rows === snapshot.xterm.rows
  )
}

// Delay reassertion read dispatch past reveal fit to model busy daemon/SSH ordering.
async function armSlowAppliedSizeRead(page: Page, delayMs: number): Promise<void> {
  await page.evaluate((delayMs) => {
    ;(window as StaleResizeReproWindow).__e2ePtyAppliedSizeReadDelayMs = delayMs
  }, delayMs)
}

type CycleDriverArgs = {
  page: Page
  testInfo: TestInfo
  testRepoPath: string
  cycles: number
  label: string
  delaySweepMs?: readonly number[]
  onRevealSample?: (sample: { cycle: number; snapshot: GridSnapshot }) => void
}

async function driveHiddenResizeRevealCycles(args: CycleDriverArgs): Promise<CycleFailure[]> {
  const { page, testInfo, testRepoPath, cycles, label } = args
  await waitForSessionReady(page)
  const firstWorktreeId = await waitForActiveWorktree(page)
  const secondWorktreeId = (await getAllWorktreeIds(page)).find((id) => id !== firstWorktreeId)
  test.skip(!secondWorktreeId, 'stale-resize repro needs the seeded secondary worktree')
  if (!secondWorktreeId) {
    return []
  }

  await page.setViewportSize(VIEWPORTS[0])
  await closeRightSidebarAndFeatureTips(page)

  await switchToWorktree(page, secondWorktreeId)
  await ensureTerminalVisible(page)
  await waitForActiveTerminalManager(page, 30_000)
  const ptyId = await waitForActivePanePtyId(page)
  await waitForPtyShellEcho(page, ptyId, 15_000)

  const runId = Math.random().toString(36).slice(2, 10)
  const scriptPath = path.join(testRepoPath, `.orca-bottom-bar-${runId}.cjs`)
  writeFileSync(scriptPath, bottomBarTuiScript(runId))
  await sendToTerminal(page, ptyId, `node ${JSON.stringify(scriptPath)}\r`)
  await expect
    .poll(() => readGridSnapshot(page, ptyId).then(gridsConverged), {
      timeout: 15_000,
      message: `${label}: applied PTY size should match xterm before cycling`
    })
    .toBe(true)

  const failures: CycleFailure[] = []
  let viewportIndex = 0
  for (let cycle = 0; cycle < cycles; cycle += 1) {
    if (args.delaySweepMs) {
      await armSlowAppliedSizeRead(page, args.delaySweepMs[cycle % args.delaySweepMs.length])
    }
    await switchToWorktree(page, firstWorktreeId)
    await expect.poll(() => getActiveWorktreeId(page), { timeout: 10_000 }).toBe(firstWorktreeId)
    // Resize while hidden so renderer PTY forwarding is non-authoritative and reveal readback owns correction.
    viewportIndex = (viewportIndex + 1) % VIEWPORTS.length
    await page.setViewportSize(VIEWPORTS[viewportIndex])
    await page.waitForTimeout(400)

    await switchToWorktree(page, secondWorktreeId)
    await expect.poll(() => getActiveWorktreeId(page), { timeout: 10_000 }).toBe(secondWorktreeId)

    // Sample immediately and record every desync, including stale forwards later healed after a wrong-grid TUI redraw.
    for (let sample = 0; sample < 20; sample += 1) {
      const snapshot = await readGridSnapshot(page, ptyId)
      if (!gridsConverged(snapshot) && snapshot.xterm !== null && snapshot.applied !== null) {
        args.onRevealSample?.({ cycle, snapshot })
      }
      await page.waitForTimeout(10)
    }

    let lastSnapshot: GridSnapshot = { xterm: null, applied: null }
    const deadline = Date.now() + CONVERGE_TIMEOUT_MS
    let converged = false
    while (Date.now() < deadline) {
      lastSnapshot = await readGridSnapshot(page, ptyId)
      if (gridsConverged(lastSnapshot)) {
        converged = true
        break
      }
      await page.waitForTimeout(150)
    }
    if (!converged) {
      failures.push({ cycle, snapshot: lastSnapshot })
      if (failures.length <= 3) {
        const screenshotPath = testInfo.outputPath(`${label}-cycle-${cycle}.png`)
        await page.screenshot({ path: screenshotPath, fullPage: true })
        await testInfo.attach(`${label}-cycle-${cycle}.png`, {
          path: screenshotPath,
          contentType: 'image/png'
        })
      }
    }
  }
  return failures
}

test.describe('Terminal reveal stale PTY resize repro', () => {
  test('applied PTY size converges across hidden-resize/reveal cycles (natural read ordering)', async ({
    orcaPage,
    testRepoPath
  }, testInfo: TestInfo) => {
    test.setTimeout(300_000)
    const failures = await driveHiddenResizeRevealCycles({
      page: orcaPage,
      testInfo,
      testRepoPath,
      cycles: 8,
      label: 'natural-order'
    })
    expect(
      failures,
      `PTY applied size stayed desynced from xterm after reveal: ${JSON.stringify(failures)}`
    ).toEqual([])
  })

  test('the PTY is never resized back to its stale pre-reveal grid under slow applied-size reads', async ({
    orcaPage,
    testRepoPath
  }, testInfo: TestInfo) => {
    test.setTimeout(300_000)
    const staleSamples: { cycle: number; snapshot: GridSnapshot }[] = []
    const failures = await driveHiddenResizeRevealCycles({
      page: orcaPage,
      testInfo,
      testRepoPath,
      cycles: 12,
      label: 'slow-read',
      delaySweepMs: GETSIZE_DELAY_SWEEP_MS,
      onRevealSample: (sample) => staleSamples.push(sample)
    })
    if (staleSamples.length > 0) {
      console.log(`[repro] desynced post-reveal samples: ${JSON.stringify(staleSamples)}`)
    }
    expect(
      staleSamples,
      `PTY applied size diverged from xterm after reveal (stale resize fired): ${JSON.stringify(staleSamples)}`
    ).toEqual([])
    expect(
      failures,
      `PTY stayed desynced from xterm after reveal: ${JSON.stringify(failures)}`
    ).toEqual([])
  })
})