Files
orca/src/main/daemon/session.ts
T
Neil 1be13d58f3 fix(daemon): defer startup flush past shell raw-mode switch (#1060)
* fix(daemon): defer startup-command flush past shell raw-mode switch

When launching an agent (e.g. `claude`) through the quick-launch menu,
the command name appeared twice in the terminal — once from kernel
echo, once from readline's prompt redraw. The daemon session was
flushing its pre-ready stdin queue the moment the OSC 777 shell-ready
marker arrived, but that marker fires from precmd_functions /
PROMPT_COMMAND — before the shell draws its prompt and before
zle/readline flips the PTY into raw mode. Writing while ECHO was still
on produced the visible duplicate.

Mirror the gating already used by the non-daemon path
(local-pty-shell-ready.ts::writeStartupCommandWhenShellReady): wait
for the next data chunk after the marker (the prompt draw) plus a
short 30ms delay, with a 50ms wall-clock fallback for the case where
the prompt arrives in the same chunk as the marker.

This regression became visible after #1025 made the shell-ready
wrappers persist reliably — previously the marker was often missed
and the 15s fallback path fired long after the shell was already in
raw mode, masking the race.

* refactor(daemon): extract PostReadyFlushGate out of Session

Factor the post-ready flush gating out of Session into its own class in
post-ready-flush-gate.ts. Session's only responsibility is now to
arm() the gate on shell-ready, notifyData() on subsequent PTY data,
and clear() on teardown. Removes the max-lines oxlint-disable added in
the previous commit and puts the timing behavior behind focused unit
tests.

No behavior change — kept as a separate refactor commit from the
behavior fix for reviewability.

* fix(daemon): keep queueing writes while post-ready flush gate is pending

Codex review caught an ordering regression: once transitionToReady()
sets _shellState to 'ready', any Session.write() that arrives during
the 30–50ms gate window was being written directly to the subprocess,
bypassing the still-unflushed preReadyStdinQueue. That let late input
race ahead of the buffered startup command.

Expose PostReadyFlushGate.isPending and continue queuing while the gate
is armed so queued writes drain in their original order before any
fresh input reaches the subprocess.
2026-04-24 15:26:50 -07:00

349 lines
8.9 KiB
TypeScript

import { HeadlessEmulator } from './headless-emulator'
import { isValidPtySize, normalizePtySize } from './daemon-pty-size'
import { PostReadyFlushGate } from './post-ready-flush-gate'
import type { SessionState, ShellReadyState, TerminalSnapshot } from './types'
const SHELL_READY_TIMEOUT_MS = 15_000
const KILL_TIMEOUT_MS = 5_000
const SHELL_READY_MARKER = '\x1b]777;orca-shell-ready\x07'
export type SubprocessHandle = {
pid: number
write(data: string): void
resize(cols: number, rows: number): void
kill(): void
forceKill(): void
signal(sig: string): void
onData(cb: (data: string) => void): void
onExit(cb: (code: number) => void): void
}
export type SessionOptions = {
sessionId: string
cols: number
rows: number
subprocess: SubprocessHandle
shellReadySupported: boolean
scrollback?: number
}
type AttachedClient = {
token: symbol
onData: (data: string) => void
onExit: (code: number) => void
}
export class Session {
readonly sessionId: string
private _state: SessionState = 'running'
private _shellState: ShellReadyState
private _exitCode: number | null = null
private _isTerminating = false
private _disposed = false
private emulator: HeadlessEmulator
private subprocess: SubprocessHandle
private attachedClients: AttachedClient[] = []
private preReadyStdinQueue: string[] = []
private markerBuffer = ''
private shellReadyTimer: ReturnType<typeof setTimeout> | null = null
private killTimer: ReturnType<typeof setTimeout> | null = null
private postReadyFlushGate: PostReadyFlushGate
constructor(opts: SessionOptions) {
this.sessionId = opts.sessionId
this.subprocess = opts.subprocess
const size = normalizePtySize(opts.cols, opts.rows)
this.emulator = new HeadlessEmulator({
cols: size.cols,
rows: size.rows,
scrollback: opts.scrollback
// No onData wiring: the daemon-side emulator must never reply to
// terminal query sequences. The renderer's xterm is the authoritative
// responder; any daemon reply races ahead via in-process parsing and
// clobbers the renderer's answer. See the comment in HeadlessEmulator.
})
if (opts.shellReadySupported) {
this._shellState = 'pending'
this.shellReadyTimer = setTimeout(() => {
this.onShellReadyTimeout()
}, SHELL_READY_TIMEOUT_MS)
} else {
this._shellState = 'unsupported'
}
this.postReadyFlushGate = new PostReadyFlushGate(() => this.flushPreReadyQueue())
this.subprocess.onData((data) => this.handleSubprocessData(data))
this.subprocess.onExit((code) => this.handleSubprocessExit(code))
}
get state(): SessionState {
return this._state
}
get shellState(): ShellReadyState {
return this._shellState
}
get exitCode(): number | null {
return this._exitCode
}
get isAlive(): boolean {
return this._state !== 'exited'
}
get isTerminating(): boolean {
return this._isTerminating
}
get pid(): number {
return this.subprocess.pid
}
write(data: string): void {
if (this._state === 'exited' || this._disposed) {
return
}
// Why: during the post-ready flush gate window (shellState is already
// 'ready' but the queue hasn't flushed yet) we must keep queuing. Writing
// directly would let fresh input race ahead of the buffered startup
// command, changing execution order.
if (this._shellState === 'pending' || this.postReadyFlushGate.isPending) {
this.preReadyStdinQueue.push(data)
return
}
this.subprocess.write(data)
}
resize(cols: number, rows: number): void {
if (this._state === 'exited' || this._disposed) {
return
}
if (!isValidPtySize(cols, rows)) {
return
}
this.emulator.resize(cols, rows)
this.subprocess.resize(cols, rows)
}
kill(): void {
if (this._state === 'exited' || this._isTerminating) {
return
}
this._isTerminating = true
this.subprocess.kill()
this.killTimer = setTimeout(() => {
if (this._state !== 'exited') {
this.forceDispose()
}
}, KILL_TIMEOUT_MS)
}
signal(sig: string): void {
if (this._state === 'exited') {
return
}
this.subprocess.signal(sig)
}
attachClient(client: { onData: (data: string) => void; onExit: (code: number) => void }): symbol {
const token = Symbol('attach')
this.attachedClients.push({ token, ...client })
return token
}
detachClient(token: symbol): void {
const idx = this.attachedClients.findIndex((c) => c.token === token)
if (idx !== -1) {
this.attachedClients.splice(idx, 1)
}
}
detachAllClients(): void {
this.attachedClients.length = 0
}
getSnapshot(): TerminalSnapshot | null {
if (this._disposed) {
return null
}
return this.emulator.getSnapshot()
}
getCwd(): string | null {
return this.emulator.getCwd()
}
clearScrollback(): void {
if (this._disposed) {
return
}
this.emulator.clearScrollback()
}
dispose(): void {
if (this._disposed) {
return
}
// Why: if kill() was called but the subprocess hasn't exited yet, our
// killTimer is the only thing that would forceKill a stuck subprocess.
// Clearing it below without force-killing would leak an orphaned process,
// so mirror forceDispose(): issue the force-kill, notify attached clients
// (handleSubprocessExit is guarded by _disposed and won't broadcast later),
// and clear the terminating flag for a consistent final state.
const wasTerminating = this._isTerminating && this._state !== 'exited'
const clientsToNotify = wasTerminating ? this.attachedClients.slice() : []
if (wasTerminating) {
this.subprocess.forceKill()
this._exitCode = -1
this._isTerminating = false
}
this._disposed = true
this._state = 'exited'
if (this.shellReadyTimer) {
clearTimeout(this.shellReadyTimer)
this.shellReadyTimer = null
}
if (this.killTimer) {
clearTimeout(this.killTimer)
this.killTimer = null
}
this.postReadyFlushGate.clear()
this.attachedClients = []
this.preReadyStdinQueue = []
this.emulator.dispose()
for (const client of clientsToNotify) {
client.onExit(-1)
}
}
private handleSubprocessData(data: string): void {
if (this._disposed) {
return
}
// Feed data to headless emulator for state tracking
this.emulator.write(data)
if (this._shellState === 'pending') {
this.scanForShellMarker(data)
} else {
this.postReadyFlushGate.notifyData()
}
// Broadcast to attached clients
for (const client of this.attachedClients) {
client.onData(data)
}
}
private handleSubprocessExit(code: number): void {
if (this._disposed) {
return
}
this._exitCode = code
this._state = 'exited'
if (this.killTimer) {
clearTimeout(this.killTimer)
this.killTimer = null
}
if (this.shellReadyTimer) {
clearTimeout(this.shellReadyTimer)
this.shellReadyTimer = null
}
this.postReadyFlushGate.clear()
for (const client of this.attachedClients) {
client.onExit(code)
}
}
private scanForShellMarker(data: string): void {
this.markerBuffer += data
const markerIdx = this.markerBuffer.indexOf(SHELL_READY_MARKER)
if (markerIdx !== -1) {
this.markerBuffer = ''
this.transitionToReady()
return
}
// Keep only the tail that could be the start of a partial marker match
const maxPartial = SHELL_READY_MARKER.length - 1
if (this.markerBuffer.length > maxPartial) {
this.markerBuffer = this.markerBuffer.slice(-maxPartial)
}
}
private transitionToReady(): void {
this._shellState = 'ready'
if (this.shellReadyTimer) {
clearTimeout(this.shellReadyTimer)
this.shellReadyTimer = null
}
if (this.preReadyStdinQueue.length === 0) {
return
}
this.postReadyFlushGate.arm()
}
private onShellReadyTimeout(): void {
this.shellReadyTimer = null
if (this._shellState !== 'pending') {
return
}
this._shellState = 'timed_out'
this.flushPreReadyQueue()
}
private flushPreReadyQueue(): void {
const queued = this.preReadyStdinQueue
this.preReadyStdinQueue = []
for (const data of queued) {
this.subprocess.write(data)
}
}
private forceDispose(): void {
if (this._state === 'exited') {
return
}
this.subprocess.forceKill()
this._disposed = true
this._exitCode = -1
this._state = 'exited'
this._isTerminating = false
if (this.shellReadyTimer) {
clearTimeout(this.shellReadyTimer)
this.shellReadyTimer = null
}
if (this.killTimer) {
clearTimeout(this.killTimer)
this.killTimer = null
}
this.postReadyFlushGate.clear()
const clients = this.attachedClients
this.attachedClients = []
this.preReadyStdinQueue = []
this.emulator.dispose()
for (const client of clients) {
client.onExit(-1)
}
}
}