|
| 1 | +/** |
| 2 | + * Real-zsh proof for the typed launch lines `startup-line-prompt-carry.ts` lets a prompt ride: a |
| 3 | + * multi-line line whose every line fits the per-line budget, up to the whole-line budget, reaches |
| 4 | + * zsh intact through Orca's own wrapper, ready marker and startup write. |
| 5 | + * |
| 6 | + * Why a slow user config too: the write is released 1.5 s after spawn even without the marker, and |
| 7 | + * then lands while the terminal is still line-buffered, where macOS keeps at most MAX_CANON bytes of |
| 8 | + * one line. The per-line budget is what survives that; a single 1.1 KB line did not. |
| 9 | + */ |
| 10 | +import { spawnSync } from 'node:child_process' |
| 11 | +import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs' |
| 12 | +import { tmpdir } from 'node:os' |
| 13 | +import { join } from 'node:path' |
| 14 | +import * as pty from 'node-pty' |
| 15 | +import { afterEach, beforeEach, describe, expect, it } from 'vitest' |
| 16 | +import { |
| 17 | + createShellStartupOutputScanState, |
| 18 | + scanShellStartupOutput |
| 19 | +} from '../shell-startup-output-scanner' |
| 20 | +import { selectShellStartupFeatures } from '../shell-startup-features' |
| 21 | +import { isBracketedPasteSafeShell } from '../../shared/startup-command-submission' |
| 22 | +import { |
| 23 | + TYPED_STARTUP_LINE_PROMPT_BUDGET_BYTES, |
| 24 | + ZSH_MULTI_LINE_STARTUP_LINE_BUDGET_BYTES |
| 25 | +} from '../../shared/startup-line-prompt-carry' |
| 26 | +import { |
| 27 | + restoreUserDataPathAfterEach, |
| 28 | + setTestUserDataPath |
| 29 | +} from './local-pty-shell-ready-test-harness' |
| 30 | + |
| 31 | +const ZSH_PATH = |
| 32 | + process.platform === 'win32' |
| 33 | + ? '' |
| 34 | + : (spawnSync('sh', ['-c', 'command -v zsh'], { encoding: 'utf8' }).stdout ?? '').trim() |
| 35 | + |
| 36 | +/** Lines of prompt text, each `perLine` bytes. */ |
| 37 | +function promptLines(lineCount: number, perLine: number): string { |
| 38 | + const words = 'Fix the failing checks on this branch, then explain what changed and why.' |
| 39 | + return Array.from({ length: lineCount }, (_unused, index) => { |
| 40 | + let line = `L${index}:` |
| 41 | + while (line.length < perLine) { |
| 42 | + line += ` ${words}` |
| 43 | + } |
| 44 | + return line.slice(0, perLine) |
| 45 | + }).join('\n') |
| 46 | +} |
| 47 | + |
| 48 | +let home = '' |
| 49 | + |
| 50 | +beforeEach(() => { |
| 51 | + home = mkdtempSync(join(tmpdir(), 'orca-typed-line-home-')) |
| 52 | + setTestUserDataPath(mkdtempSync(join(tmpdir(), 'orca-typed-line-ud-'))) |
| 53 | +}) |
| 54 | + |
| 55 | +afterEach(() => { |
| 56 | + rmSync(home, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 }) |
| 57 | +}) |
| 58 | + |
| 59 | +restoreUserDataPathAfterEach() |
| 60 | + |
| 61 | +/** Types `printf '%s' '<text>' > out` the way a local pane types a launch line; returns what ran. */ |
| 62 | +async function typeIntoZsh(text: string, userConfigSeconds: number): Promise<string | null> { |
| 63 | + if (userConfigSeconds > 0) { |
| 64 | + writeFileSync(join(home, '.zshrc'), `sleep ${userConfigSeconds}\n`) |
| 65 | + } |
| 66 | + const out = join(home, 'out.txt') |
| 67 | + const done = join(home, 'done.txt') |
| 68 | + const { ensureShellReadyWrappers } = await import('./local-pty-shell-ready-wrapper-generation') |
| 69 | + const { getShellLaunchConfig } = await import('./local-pty-shell-ready') |
| 70 | + const { writeStartupCommandWhenShellReady, STARTUP_COMMAND_READY_MAX_WAIT_MS } = |
| 71 | + await import('./local-pty-shell-ready-startup-command') |
| 72 | + ensureShellReadyWrappers() |
| 73 | + const env: Record<string, string> = { PATH: '/usr/bin:/bin', HOME: home } |
| 74 | + const launch = getShellLaunchConfig( |
| 75 | + ZSH_PATH, |
| 76 | + selectShellStartupFeatures({ |
| 77 | + shellPath: ZSH_PATH, |
| 78 | + env, |
| 79 | + hasStartupCommand: true, |
| 80 | + waitsForShellReady: true, |
| 81 | + emitsStartupIdentity: false |
| 82 | + }) |
| 83 | + ) |
| 84 | + expect(launch.supportsReadyMarker).toBe(true) |
| 85 | + const proc = pty.spawn(ZSH_PATH, launch.args ?? [], { |
| 86 | + name: 'xterm-256color', |
| 87 | + cols: 200, |
| 88 | + rows: 40, |
| 89 | + cwd: home, |
| 90 | + // Why ORCA_ORIG_ZDOTDIR: the user's config is read from the sandbox home, not the real one. |
| 91 | + env: { ...env, ...launch.env, ORCA_ORIG_ZDOTDIR: home, TERM: 'xterm-256color' } |
| 92 | + }) |
| 93 | + const scan = createShellStartupOutputScanState() |
| 94 | + let resolveReady: ((signal: { postMarkerBytesObserved: boolean }) => void) | null = null |
| 95 | + const ready = new Promise<{ postMarkerBytesObserved: boolean }>((resolve) => { |
| 96 | + resolveReady = resolve |
| 97 | + }) |
| 98 | + // The provider's cap (local-pty-shell-readiness-session.ts): a later marker releases the write. |
| 99 | + const cap = setTimeout(() => { |
| 100 | + resolveReady?.({ postMarkerBytesObserved: false }) |
| 101 | + resolveReady = null |
| 102 | + }, STARTUP_COMMAND_READY_MAX_WAIT_MS) |
| 103 | + proc.onData((data) => { |
| 104 | + if (resolveReady && scanShellStartupOutput(scan, data).ready) { |
| 105 | + resolveReady({ postMarkerBytesObserved: true }) |
| 106 | + resolveReady = null |
| 107 | + } |
| 108 | + }) |
| 109 | + writeStartupCommandWhenShellReady( |
| 110 | + ready, |
| 111 | + proc, |
| 112 | + `printf '%s' '${text}' > '${out}'; printf ok > '${done}'`, |
| 113 | + () => {}, |
| 114 | + { |
| 115 | + bracketedPasteSafe: isBracketedPasteSafeShell({ |
| 116 | + shellName: 'zsh', |
| 117 | + waitsForShellReady: launch.supportsReadyMarker === true |
| 118 | + }) |
| 119 | + } |
| 120 | + ) |
| 121 | + const deadline = Date.now() + 20_000 + userConfigSeconds * 1000 |
| 122 | + while (!existsSync(done) && Date.now() < deadline) { |
| 123 | + await new Promise((resolve) => setTimeout(resolve, 50)) |
| 124 | + } |
| 125 | + clearTimeout(cap) |
| 126 | + const ran = existsSync(out) ? readFileSync(out, 'utf8') : null |
| 127 | + const exited = new Promise<void>((resolve) => proc.onExit(() => resolve())) |
| 128 | + proc.kill() |
| 129 | + await Promise.race([exited, new Promise((resolve) => setTimeout(resolve, 2000))]) |
| 130 | + return ran |
| 131 | +} |
| 132 | + |
| 133 | +describe.skipIf(!ZSH_PATH)('a multi-line launch line typed into zsh', () => { |
| 134 | + // Lines at the per-line budget, as many as the whole-line budget leaves room for. |
| 135 | + const perLine = TYPED_STARTUP_LINE_PROMPT_BUDGET_BYTES - 1 |
| 136 | + const text = promptLines( |
| 137 | + Math.floor((ZSH_MULTI_LINE_STARTUP_LINE_BUDGET_BYTES - 200) / (perLine + 1)), |
| 138 | + perLine |
| 139 | + ) |
| 140 | + |
| 141 | + it('arrives whole through the ready barrier', async () => { |
| 142 | + expect(await typeIntoZsh(text, 0)).toBe(text) |
| 143 | + }, 60_000) |
| 144 | + |
| 145 | + it('arrives whole when the user config outlasts the barrier and the write lands early', async () => { |
| 146 | + expect(await typeIntoZsh(text, 3)).toBe(text) |
| 147 | + }, 60_000) |
| 148 | +}) |
0 commit comments