Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/main/daemon/daemon-pty-startup-delivery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe('DaemonPtyAdapter startup delivery', () => {
await vi.advanceTimersByTimeAsync(299)
expect(lastSubprocess.write).not.toHaveBeenCalled()
await vi.advanceTimersByTimeAsync(1)
expect(lastSubprocess.write).toHaveBeenCalledExactlyOnceWith('codex\n')
expect(lastSubprocess.write).toHaveBeenCalledExactlyOnceWith('codex\r')
expect(lastSpawnOpts).not.toEqual(
expect.objectContaining({ startupCommandDelivery: 'shell-ready' })
)
Expand Down Expand Up @@ -82,7 +82,7 @@ describe('DaemonPtyAdapter startup delivery', () => {
)
lastSubprocess._simulateData('\x1b]777;orca-shell-ready\x07\r\nuser@host $ ')
await waitFor(() => vi.mocked(lastSubprocess.write).mock.calls.length > 0)
expect(lastSubprocess.write).toHaveBeenCalledExactlyOnceWith('codex\n')
expect(lastSubprocess.write).toHaveBeenCalledExactlyOnceWith('codex\r')
}
)

Expand All @@ -102,6 +102,6 @@ describe('DaemonPtyAdapter startup delivery', () => {
lastSubprocess._simulateData('\r\nuser@host $ ')

await waitFor(() => vi.mocked(lastSubprocess.write).mock.calls.length > 0)
expect(lastSubprocess.write).toHaveBeenCalledExactlyOnceWith(`${startup.command}\n`)
expect(lastSubprocess.write).toHaveBeenCalledExactlyOnceWith(`${startup.command}\r`)
})
})
2 changes: 0 additions & 2 deletions src/main/daemon/terminal-host-session-create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,9 @@ async function spawnAndPublishSession(
// Diagnostics must never turn a live PTY into a failed create.
}
if (startupCommandWritten && opts.command) {
const submit = process.platform === 'win32' ? '\r' : '\n'
// Why: only Orca-wrapped shells advertise the paste-safe startup barrier.
session.write(
buildStartupCommandSubmission(opts.command, {
submit,
bracketedPasteSafe: shellReadySupported
})
)
Expand Down
15 changes: 7 additions & 8 deletions src/main/daemon/terminal-host-startup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,8 @@ function mockSubprocess(): SubprocessHandle {
} as SubprocessHandle
}

// Why: Windows shells (PowerShell/cmd.exe) submit on CR, not LF. Without CR
// the startup command sits typed at the prompt but unexecuted — forcing the
// user to press Enter after "claude" (or a setup script) is injected.
// POSIX shells (bash/zsh) keep the LF behaviour. A caller-supplied terminator
// must not be doubled.
// Why: without CR (Enter) the startup command sits typed at the prompt but
// unexecuted (#23250). A caller-supplied terminator is replaced, not doubled.
describe('TerminalHost startup command terminator', () => {
const origPlatform = process.platform
afterEach(() => {
Expand All @@ -39,9 +36,11 @@ describe('TerminalHost startup command terminator', () => {

it.each([
['win32', 'claude', 'claude\r'],
['darwin', 'claude', 'claude\n'],
['darwin', 'claude', 'claude\r'],
['linux', 'claude', 'claude\r'],
['win32', 'claude\r', 'claude\r'],
['darwin', 'claude\n', 'claude\n']
['darwin', 'claude\n', 'claude\r'],
['win32', 'claude\r\n', 'claude\r']
])('submits startup with correct terminator on %s', async (platform, cmd, sent) => {
Object.defineProperty(process, 'platform', { value: platform })
await host.createOrAttach({
Expand Down Expand Up @@ -128,6 +127,6 @@ describe('TerminalHost startup command delivery logging', () => {
streamClient: { onData: vi.fn(), onExit: vi.fn() }
})
).resolves.toMatchObject({ isNew: true })
expect(sub.write).toHaveBeenCalledWith(`codex${process.platform === 'win32' ? '\r' : '\n'}`)
expect(sub.write).toHaveBeenCalledWith('codex\r')
})
})
15 changes: 5 additions & 10 deletions src/main/daemon/terminal-host.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,7 @@ describe('TerminalHost', () => {

lastSubprocess._onDataCb?.('\r\nuser@host $ ')
await new Promise((r) => setTimeout(r, 40))
expect(lastSubprocess.write).toHaveBeenCalledWith(
process.platform === 'win32' ? 'echo hello\r' : 'echo hello\n'
)
expect(lastSubprocess.write).toHaveBeenCalledWith('echo hello\r')
})

it('uses the short daemon settle path when marker and prompt arrive together', async () => {
Expand All @@ -211,9 +209,7 @@ describe('TerminalHost', () => {
expect(lastSubprocess.write).not.toHaveBeenCalled()

vi.advanceTimersByTime(1)
expect(lastSubprocess.write).toHaveBeenCalledWith(
process.platform === 'win32' ? 'echo hello\r' : 'echo hello\n'
)
expect(lastSubprocess.write).toHaveBeenCalledWith('echo hello\r')
} finally {
vi.useRealTimers()
}
Expand Down Expand Up @@ -242,9 +238,7 @@ describe('TerminalHost', () => {
streamClient: { onData: vi.fn(), onExit: vi.fn() }
})

expect(lastSubprocess.write).toHaveBeenCalledWith(
process.platform === 'win32' ? 'echo hello\r' : 'echo hello\n'
)
expect(lastSubprocess.write).toHaveBeenCalledWith('echo hello\r')
})

it('does not bracketed-paste-wrap multiline commands for a fallback shell without paste mode', async () => {
Expand Down Expand Up @@ -272,7 +266,8 @@ describe('TerminalHost', () => {

const written = (lastSubprocess.write as ReturnType<typeof vi.fn>).mock.calls[0]?.[0]
expect(written).not.toContain('\x1b[200~')
expect(written).toContain('line one\nline two')
// Why CR between the lines: without bracketed paste each break submits its own line.
expect(written).toContain('line one\rline two')
})

it('keeps the shell-ready barrier when the spawned shell supports the marker', async () => {
Expand Down
4 changes: 2 additions & 2 deletions src/main/ipc/pty-login-shell-startup-commands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ describe('registerPtyHandlers', () => {
mockProc.emitData('\x1b]133;A\x07% ')
await Promise.resolve()
vi.runAllTimers()
expect(mockProc.proc.write).toHaveBeenCalledWith('claude\n')
expect(mockProc.proc.write).toHaveBeenCalledWith('claude\r')
} finally {
vi.useRealTimers()
}
Expand Down Expand Up @@ -396,7 +396,7 @@ describe('registerPtyHandlers', () => {
vi.advanceTimersByTime(1)
await Promise.resolve()
vi.runAllTimers()
expect(mockProc.proc.write).toHaveBeenCalledWith('printf "hello"\n')
expect(mockProc.proc.write).toHaveBeenCalledWith('printf "hello"\r')
} finally {
vi.useRealTimers()
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/ipc/pty-spawn-env-codex-resume-provenance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ describe('registerPtyHandlers', () => {
await Promise.resolve()
vi.runAllTimers()

expect(mockProc.proc.write).toHaveBeenCalledWith(`${command}\n`)
expect(mockProc.proc.write).toHaveBeenCalledWith(`${command}\r`)
expect(spawned.agentResumeUnavailable).toBeUndefined()
} finally {
vi.useRealTimers()
Expand Down
6 changes: 3 additions & 3 deletions src/main/providers/local-pty-provider-shell-readiness.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ describe('LocalPtyProvider', () => {
expect(mockProc.write).not.toHaveBeenCalled()

await vi.advanceTimersByTimeAsync(200)
expect(mockProc.write).toHaveBeenCalledWith(`${command}\n`)
expect(mockProc.write).toHaveBeenCalledWith(`${command}\r`)
} finally {
vi.useRealTimers()
}
Expand Down Expand Up @@ -269,7 +269,7 @@ describe('LocalPtyProvider', () => {

vi.advanceTimersByTime(1)
await Promise.resolve()
expect(mockProc.write).toHaveBeenCalledWith("printf 'linked issue context'\n")
expect(mockProc.write).toHaveBeenCalledWith("printf 'linked issue context'\r")
} finally {
vi.useRealTimers()
}
Expand Down Expand Up @@ -298,7 +298,7 @@ describe('LocalPtyProvider', () => {

vi.advanceTimersByTime(200)
await Promise.resolve()
expect(mockProc.write).toHaveBeenCalledWith('printf ready\n')
expect(mockProc.write).toHaveBeenCalledWith('printf ready\r')
} finally {
vi.useRealTimers()
}
Expand Down
41 changes: 11 additions & 30 deletions src/main/providers/local-pty-shell-ready-startup-command.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ describe('writeStartupCommandWhenShellReady', () => {
Object.defineProperty(process, 'platform', { value: origPlatform })
})

it('appends LF on POSIX so bash/zsh submit the line', async () => {
Object.defineProperty(process, 'platform', { value: 'darwin' })
it.each(['darwin', 'linux', 'win32'])('submits with CR on %s', async (platform) => {
Object.defineProperty(process, 'platform', { value: platform })
const proc = createMockProc()
const ready = Promise.resolve()
writeStartupCommandWhenShellReady(ready, proc, 'claude', () => {})
Expand All @@ -69,39 +69,23 @@ describe('writeStartupCommandWhenShellReady', () => {
vi.advanceTimersByTime(30)
await Promise.resolve()

expect(proc._writes).toEqual(['claude\n'])
})

it('appends CR on Windows so PowerShell/cmd.exe submit the line', async () => {
Object.defineProperty(process, 'platform', { value: 'win32' })
const proc = createMockProc()
const ready = Promise.resolve()
writeStartupCommandWhenShellReady(ready, proc, 'claude', () => {})

await ready
proc._emitData('\r\nPS> ')
vi.advanceTimersByTime(30)
await Promise.resolve()

expect(proc._writes).toEqual(['claude\r'])
})

it('does not re-append a submit byte if the command already ends in CR or LF', async () => {
Object.defineProperty(process, 'platform', { value: 'win32' })
it('replaces a caller-supplied LF terminator with CR', async () => {
const proc = createMockProc()
const ready = Promise.resolve()
writeStartupCommandWhenShellReady(ready, proc, 'claude\n', () => {})

await ready
proc._emitData('\r\nPS> ')
proc._emitData('\r\nuser@host % ')
vi.advanceTimersByTime(30)
await Promise.resolve()

expect(proc._writes).toEqual(['claude\n'])
expect(proc._writes).toEqual(['claude\r'])
})

it('keeps the no-prompt fallback conservative to avoid duplicate shell echo', async () => {
Object.defineProperty(process, 'platform', { value: 'darwin' })
const proc = createMockProc()
const ready = Promise.resolve()
writeStartupCommandWhenShellReady(ready, proc, 'codex', () => {})
Expand All @@ -115,11 +99,10 @@ describe('writeStartupCommandWhenShellReady', () => {
vi.advanceTimersByTime(150)
await Promise.resolve()

expect(proc._writes).toEqual(['codex\n'])
expect(proc._writes).toEqual(['codex\r'])
})

it('uses the short settle delay when marker scan already observed post-marker bytes', async () => {
Object.defineProperty(process, 'platform', { value: 'darwin' })
const proc = createMockProc()
const ready = Promise.resolve({ postMarkerBytesObserved: true })
writeStartupCommandWhenShellReady(ready, proc, 'codex', () => {})
Expand All @@ -131,12 +114,11 @@ describe('writeStartupCommandWhenShellReady', () => {

vi.advanceTimersByTime(1)
await Promise.resolve()
expect(proc._writes).toEqual(['codex\n'])
expect(proc._writes).toEqual(['codex\r'])
})

// Why: multiline startup commands must be bracketed-paste wrapped (ESC[200~ … ESC[201~) so shells insert them literally instead of treating each LF as Enter.
it('wraps a multiline startup command in bracketed paste when the shell supports it', async () => {
Object.defineProperty(process, 'platform', { value: 'darwin' })
const proc = createMockProc()
const ready = Promise.resolve()
const command = "claude '--dangerously-skip-permissions' 'line one\nline two'"
Expand All @@ -149,11 +131,10 @@ describe('writeStartupCommandWhenShellReady', () => {
vi.advanceTimersByTime(30)
await Promise.resolve()

expect(proc._writes).toEqual([`\x1b[200~${command}\x1b[201~\n`])
expect(proc._writes).toEqual([`\x1b[200~${command}\x1b[201~\r`])
})

it('leaves a single-line command on the raw submit path even when bracketed paste is safe', async () => {
Object.defineProperty(process, 'platform', { value: 'darwin' })
const proc = createMockProc()
const ready = Promise.resolve()
writeStartupCommandWhenShellReady(ready, proc, 'claude', () => {}, {
Expand All @@ -165,14 +146,14 @@ describe('writeStartupCommandWhenShellReady', () => {
vi.advanceTimersByTime(30)
await Promise.resolve()

expect(proc._writes).toEqual(['claude\n'])
expect(proc._writes).toEqual(['claude\r'])
})

it('does not bracket-wrap a multiline command when the shell lacks bracketed paste', async () => {
Object.defineProperty(process, 'platform', { value: 'darwin' })
const proc = createMockProc()
const ready = Promise.resolve()
const command = 'echo one\necho two'
// Why CR between the lines: without bracketed paste each break submits its own line.
// Why: bracketedPasteSafe defaults false, so keep the raw path to avoid echoing ESC[200~ on shells without bracketed paste.
writeStartupCommandWhenShellReady(ready, proc, command, () => {})

Expand All @@ -181,6 +162,6 @@ describe('writeStartupCommandWhenShellReady', () => {
vi.advanceTimersByTime(30)
await Promise.resolve()

expect(proc._writes).toEqual([`${command}\n`])
expect(proc._writes).toEqual(['echo one\recho two\r'])
})
})
3 changes: 0 additions & 3 deletions src/main/providers/local-pty-shell-ready-startup-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,9 @@ export function writeStartupCommandWhenShellReady(
postReadyTimer = null
}
// Why: run in the same interactive shell (not `shell -c`) so the session survives after the agent exits.
// Why CR on Windows: PSReadLine/cmd.exe submit on `\r`, not LF; POSIX treats either as Enter under ICRNL.
const submit = process.platform === 'win32' ? '\r' : '\n'
// Why: single write after the ready barrier avoids incremental-paste char drops; multiline is bracketed-paste wrapped so newlines don't submit early.
proc.write(
buildStartupCommandSubmission(startupCommand, {
submit,
bracketedPasteSafe: options.bracketedPasteSafe === true
})
)
Expand Down
9 changes: 4 additions & 5 deletions src/relay/pty-handler-startup-command-delivery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ describe('PtyHandler', () => {
expect(term.write).not.toHaveBeenCalled()

vi.advanceTimersByTime(1)
const submit = process.platform === 'win32' ? '\r' : '\n'
expect(term.write).toHaveBeenCalledWith(`echo provider-owned${submit}`)
expect(term.write).toHaveBeenCalledWith('echo provider-owned\r')
expect(handler.retainedStartupCommandCount).toBe(0)
})

Expand Down Expand Up @@ -364,7 +363,7 @@ describe('PtyHandler', () => {
expect(term.write).not.toHaveBeenCalled()
vi.advanceTimersByTime(1)

expect(term.write).toHaveBeenCalledWith('echo after-ready\n')
expect(term.write).toHaveBeenCalledWith('echo after-ready\r')
expect(handler.retainedStartupCommandCount).toBe(0)
vi.advanceTimersByTime(8)
expect(dispatcher.notify).toHaveBeenCalledWith('pty.data', {
Expand Down Expand Up @@ -421,7 +420,7 @@ describe('PtyHandler', () => {
promptOptions.onPromptReady()
await vi.advanceTimersByTimeAsync(50)

expect(term.write).toHaveBeenCalledWith('echo after-exec\n')
expect(term.write).toHaveBeenCalledWith('echo after-exec\r')
expect(handler.retainedStartupCommandCount).toBe(0)
}
)
Expand Down Expand Up @@ -635,7 +634,7 @@ describe('PtyHandler', () => {
dataCallback?.('\x1b]777;orca-shell-ready')
vi.advanceTimersByTime(1500)

expect(term.write).toHaveBeenCalledWith('echo fallback\n')
expect(term.write).toHaveBeenCalledWith('echo fallback\r')
vi.advanceTimersByTime(8)
expect(dispatcher.notify).toHaveBeenCalledWith('pty.data', {
id: PTY_1,
Expand Down
2 changes: 0 additions & 2 deletions src/relay/pty-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1025,10 +1025,8 @@ export class PtyHandler {
if (heldBytes) {
managed.startupIngress?.accept(heldBytes)
}
const submit = process.platform === 'win32' ? '\r' : '\n'
// Why: only the shell-ready wrapper arms bracketed-paste; other shells use raw submit so ESC[200~ markers aren't echoed.
const payload = buildStartupCommandSubmission(startup.command, {
submit,
bracketedPasteSafe: startup.waitForShellReady
})
managed.startupCommand = undefined
Expand Down
4 changes: 1 addition & 3 deletions src/renderer/src/lib/ssh-background-startup-delivery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,10 @@ export function createSshBackgroundStartupDelivery(
// PTYs; hidden automation tabs still submit the command themselves.
// Why bracketed paste: multiline prompts are pasted literally only when we
// synchronized on the Orca shell-ready marker — that is the bash/zsh overlay
// with bracketed-paste mode armed. Submit with CR since the relay drives a
// remote shell.
// with bracketed-paste mode armed.
options.write(
ptyId,
buildStartupCommandSubmission(command, {
submit: '\r',
bracketedPasteSafe: markerObserved
})
)
Expand Down
17 changes: 17 additions & 0 deletions src/shared/powershell-native-argument.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,27 @@ describe('PowerShell native argument quoting', () => {
)
})

it('keeps a value with a line break on one physical line', () => {
expect(quotePowerShellLiteral('line one\nline two')).toBe('"line one`nline two"')
expect(quotePowerShellLiteral('a\r\nb\rc')).toBe('"a`r`nb`rc"')
})

it('escapes the double-quoted specials in a multi-line value', () => {
expect(
quotePowerShellLiteral('$(Remove-Item x) `t "q" \u201Cl\u201D \u201Elow \'single\'\nend')
).toBe('"`$(Remove-Item x) ``t `"q`" `\u201Cl`\u201D `\u201Elow \'single\'`nend"')
})

it('leaves single-line values on the single-quoted form', () => {
expect(quotePowerShellLiteral('a $b `c "d"')).toBe('\'a $b `c "d"\'')
})

it('pre-escapes embedded quotes for Windows native argv parsing', () => {
expect(quotePowerShellNativeArgument('eval "decoded"')).toBe(String.raw`'eval \"decoded\"'`)
expect(quotePowerShellNativeArgument(String.raw`before\"after`)).toBe(
String.raw`'before\\\"after'`
)
// Why: the argv pre-escape survives the multi-line form, so 5.1 still passes `\"` to argv.
expect(quotePowerShellNativeArgument('say "hi"\nbye')).toBe('"say \\`"hi\\`"`nbye"')
})
})
Loading
Loading