diff --git a/src/main/managed-server/lifecycle/orcad-stdio-bridge-listener.ts b/src/main/managed-server/lifecycle/orcad-stdio-bridge-listener.ts index 80713f9b122..3b1057c1a36 100644 --- a/src/main/managed-server/lifecycle/orcad-stdio-bridge-listener.ts +++ b/src/main/managed-server/lifecycle/orcad-stdio-bridge-listener.ts @@ -70,7 +70,8 @@ export async function listenOrcadStdioBridge( pipeSocketThroughBridge(socket, channel, mode) } - const server = createServer((socket) => { + // Why noDelay: bridged orcad RPC carries keystroke echo; Nagle would hold small writes behind an unacked segment. + const server = createServer({ noDelay: true }, (socket) => { sockets.add(socket) socket.on('error', () => socket.destroy()) const entry = { diff --git a/src/main/managed-server/lifecycle/orcad-stdio-bridge-script.ts b/src/main/managed-server/lifecycle/orcad-stdio-bridge-script.ts index b4dc0fd264f..19238852856 100644 --- a/src/main/managed-server/lifecycle/orcad-stdio-bridge-script.ts +++ b/src/main/managed-server/lifecycle/orcad-stdio-bridge-script.ts @@ -19,7 +19,7 @@ export const ORCAD_STDIO_BRIDGE_FUNCTION = `function orcadStdioBridge(port, mode const exit = () => out.write('', () => process.exit(0)) out.on('error', () => process.exit(0)) let connected = false - const socket = net.connect({ host: '127.0.0.1', port }) + const socket = net.connect({ host: '127.0.0.1', port, noDelay: true }) socket.on('error', (error) => { if (!connected) out.write(${text(`${ORCAD_STDIO_BRIDGE_REFUSED} `)} + String(error.code || 'ERROR') + '\\n', () => process.exit(0)) }) diff --git a/src/main/ssh/port-forwarding/ssh-port-forward.test.ts b/src/main/ssh/port-forwarding/ssh-port-forward.test.ts index d971ab014ee..5ba8ac9564e 100644 --- a/src/main/ssh/port-forwarding/ssh-port-forward.test.ts +++ b/src/main/ssh/port-forwarding/ssh-port-forward.test.ts @@ -103,7 +103,7 @@ function getLastMockServer() { vi.mock('net', () => { return { - createServer: vi.fn().mockImplementation((connectionHandler) => { + createServer: vi.fn().mockImplementation((_options, connectionHandler) => { const listeners = new Map void>() let boundPort = 0 const server = { diff --git a/src/main/ssh/port-forwarding/ssh2-port-forward-provider.ts b/src/main/ssh/port-forwarding/ssh2-port-forward-provider.ts index f9a53ef2dac..47f3bf13a51 100644 --- a/src/main/ssh/port-forwarding/ssh2-port-forward-provider.ts +++ b/src/main/ssh/port-forwarding/ssh2-port-forward-provider.ts @@ -21,7 +21,8 @@ export class Ssh2PortForwardProvider implements SshPortForwardProvider { const activeSockets = new Set() let closed = false - const server = createServer((socket) => { + // Why noDelay: forwarded orcad RPC carries keystroke echo; Nagle would hold small writes behind an unacked segment. + const server = createServer({ noDelay: true }, (socket) => { activeSockets.add(socket) socket.on('close', () => activeSockets.delete(socket)) socket.on('error', () => socket.destroy()) diff --git a/src/renderer/src/components/terminal-pane/pty-transport/remote-runtime/output/remote-runtime-pty-batching.test.ts b/src/renderer/src/components/terminal-pane/pty-transport/remote-runtime/output/remote-runtime-pty-batching.test.ts index 71b3641992c..fed1cfe15b5 100644 --- a/src/renderer/src/components/terminal-pane/pty-transport/remote-runtime/output/remote-runtime-pty-batching.test.ts +++ b/src/renderer/src/components/terminal-pane/pty-transport/remote-runtime/output/remote-runtime-pty-batching.test.ts @@ -7,7 +7,7 @@ import { } from './remote-runtime-pty-batching' describe('createRemoteRuntimePtyTextBatcher', () => { - it('coalesces small input until the debounce flush', async () => { + it('sends idle input at once and coalesces input inside the window', async () => { vi.useFakeTimers() try { const flushes: string[] = [] @@ -15,38 +15,67 @@ describe('createRemoteRuntimePtyTextBatcher', () => { maxPendingBytes: 8 }) - expect(batcher.hasPendingInput()).toBe(false) expect(batcher.push('a')).toBe(true) + expect(flushes).toEqual(['a']) expect(batcher.push('b')).toBe(true) - expect(flushes).toEqual([]) - // Why: a remount during the debounce discards these, and the pane must know to say so. - await vi.advanceTimersByTimeAsync(0) + expect(batcher.push('c')).toBe(true) + expect(flushes).toEqual(['a']) + // Why: a remount during the window discards these, and the pane must know to say so. expect(batcher.hasPendingInput()).toBe(true) await vi.advanceTimersByTimeAsync(10) - - expect(flushes).toEqual(['ab']) + expect(flushes).toEqual(['a', 'bc']) expect(batcher.hasPendingInput()).toBe(false) + + // The window stays open after a trailing flush; once it lapses idle, the next key goes at once. + expect(batcher.push('d')).toBe(true) + expect(flushes).toEqual(['a', 'bc']) + await vi.advanceTimersByTimeAsync(10) + expect(flushes).toEqual(['a', 'bc', 'd']) + await vi.advanceTimersByTimeAsync(10) + expect(batcher.push('e')).toBe(true) + expect(flushes).toEqual(['a', 'bc', 'd', 'e']) + } finally { + vi.useRealTimers() + } + }) + + it('keeps idle input trailing while the leading edge is disabled', () => { + vi.useFakeTimers() + try { + const flushes: string[] = [] + const batcher = createRemoteRuntimePtyTextBatcher(10, (text) => flushes.push(text), { + sendLeadingEdge: () => false + }) + + expect(batcher.push('a')).toBe(true) + expect(batcher.push('b')).toBe(true) + expect(flushes).toEqual([]) + vi.advanceTimersByTime(10) + expect(flushes).toEqual(['ab']) } finally { vi.useRealTimers() } }) it('flushes before pending input exceeds the byte ceiling', () => { - const flushes: string[] = [] - const batcher = createRemoteRuntimePtyTextBatcher(10, (text) => flushes.push(text), { - maxPendingBytes: 4 - }) + vi.useFakeTimers() + try { + const flushes: string[] = [] + const batcher = createRemoteRuntimePtyTextBatcher(10, (text) => flushes.push(text), { + maxPendingBytes: 4 + }) - expect(batcher.push('ab')).toBe(true) - expect(batcher.push('cd')).toBe(true) - expect(flushes).toEqual([]) + expect(batcher.push('x')).toBe(true) + expect(batcher.push('ab')).toBe(true) + expect(batcher.push('cd')).toBe(true) + expect(flushes).toEqual(['x']) - expect(batcher.push('e')).toBe(true) - - expect(flushes).toEqual(['abcd']) - batcher.flush() - expect(flushes).toEqual(['abcd', 'e']) + expect(batcher.push('e')).toBe(true) + expect(flushes).toEqual(['x', 'abcd', 'e']) + } finally { + vi.useRealTimers() + } }) it('splits one large UTF-8 input without splitting a code point', () => { @@ -75,7 +104,7 @@ describe('createRemoteRuntimePtyTextBatcher', () => { expect(flushes).toEqual([]) }) - it('yields while validating large accepted input before debounce flushing', async () => { + it('yields while validating large accepted input before sending it', async () => { vi.useFakeTimers() try { const flushes: string[] = [] @@ -91,10 +120,6 @@ describe('createRemoteRuntimePtyTextBatcher', () => { await vi.advanceTimersByTimeAsync(0) await drained - expect(flushes).toEqual([]) - - await vi.advanceTimersByTimeAsync(10) - expect(flushes).toEqual([text]) } finally { vi.useRealTimers() @@ -117,8 +142,8 @@ describe('createRemoteRuntimePtyTextBatcher', () => { await vi.advanceTimersByTimeAsync(0) await drained - expect(batcher.takePending()).toBe(`${text}tail`) - expect(flushes).toEqual([]) + expect(flushes).toEqual([text]) + expect(batcher.takePending()).toBe('tail') } finally { vi.useRealTimers() } diff --git a/src/renderer/src/components/terminal-pane/pty-transport/remote-runtime/output/remote-runtime-pty-batching.ts b/src/renderer/src/components/terminal-pane/pty-transport/remote-runtime/output/remote-runtime-pty-batching.ts index c1a1699e832..941f5cbe11e 100644 --- a/src/renderer/src/components/terminal-pane/pty-transport/remote-runtime/output/remote-runtime-pty-batching.ts +++ b/src/renderer/src/components/terminal-pane/pty-transport/remote-runtime/output/remote-runtime-pty-batching.ts @@ -27,6 +27,8 @@ export type RemoteRuntimeViewportBatcher = { export type RemoteRuntimePtyTextBatcherOptions = { maxPendingBytes?: number maxBytes?: number + /** False keeps input trailing even when idle; defaults to sending idle input at once. */ + sendLeadingEdge?: () => boolean } export function createRemoteRuntimePtyTextBatcher( @@ -75,12 +77,24 @@ export function createRemoteRuntimePtyTextBatcher( return text } + // Why: the window only coalesces input that follows a send, so an isolated keystroke never waits. + const closeWindow = (): void => { + timer = null + if (pending) { + flush() + timer ??= setTimeout(closeWindow, delayMs) + } + } + const queuePending = (chunk: string, chunkBytes: number): void => { + if (!timer && !pending && (options.sendLeadingEdge?.() ?? true)) { + timer = setTimeout(closeWindow, delayMs) + onFlush(chunk) + return + } pending += chunk pendingBytes += chunkBytes - if (!timer) { - timer = setTimeout(flush, delayMs) - } + timer ??= setTimeout(closeWindow, delayMs) } const pushValidatedInput = (data: string): void => { diff --git a/src/renderer/src/components/terminal-pane/pty-transport/remote-runtime/remote-runtime-pty-transport-input-replay.test.ts b/src/renderer/src/components/terminal-pane/pty-transport/remote-runtime/remote-runtime-pty-transport-input-replay.test.ts index e5c23b65eca..dc17c7684ab 100644 --- a/src/renderer/src/components/terminal-pane/pty-transport/remote-runtime/remote-runtime-pty-transport-input-replay.test.ts +++ b/src/renderer/src/components/terminal-pane/pty-transport/remote-runtime/remote-runtime-pty-transport-input-replay.test.ts @@ -214,6 +214,21 @@ describe('remote pane input across a silent outage', () => { transport.destroy?.() }) + it.each([ + ['acknowledging', { inputAck: 1 }, ['a']], + ['legacy', { outputPause: 1 }, []] + ] as const)( + 'sends an idle key at once only to a host that acknowledges input (%s)', + async (_host, capabilities, immediate) => { + const { transport } = await connectPane(capabilities) + subscriptionSendBinary.mockClear() + transport.sendInput('a', 'driving') + expect(sentInputs().map((input) => input.text)).toEqual(immediate) + transport.sendInput('b', 'driving') + await vi.waitFor(() => expect(sentText()).toBe('ab')) + } + ) + it('preserves the acknowledged input session across reconnects', async () => { const { transport, streamId } = await connectPane({ inputAck: 1 }) const payload = latestSubscribePayload() diff --git a/src/renderer/src/components/terminal-pane/pty-transport/remote-runtime/remote-runtime-pty-transport.ts b/src/renderer/src/components/terminal-pane/pty-transport/remote-runtime/remote-runtime-pty-transport.ts index 7309ab943fc..001247f07c7 100644 --- a/src/renderer/src/components/terminal-pane/pty-transport/remote-runtime/remote-runtime-pty-transport.ts +++ b/src/renderer/src/components/terminal-pane/pty-transport/remote-runtime/remote-runtime-pty-transport.ts @@ -2276,11 +2276,22 @@ export function createRemoteRuntimePtyTransport( return true } - const inputBatcher = createRemoteRuntimePtyTextBatcher(REMOTE_TERMINAL_INPUT_FLUSH_MS, (text) => { - if (!sendUnacknowledgedInput(text)) { - holdUnsentInput(text) + const inputBatcher = createRemoteRuntimePtyTextBatcher( + REMOTE_TERMINAL_INPUT_FLUSH_MS, + (text) => { + if (!sendUnacknowledgedInput(text)) { + holdUnsentInput(text) + } + }, + { + // Why: without the ack journal an accepted write's separate RPC could overtake an immediate key. + sendLeadingEdge: () => + // A hold replaying through sendInputNow must not re-enter its own queue. + !recoveryInputHold.isHolding() && + handle !== null && + getCurrentMultiplexedStream(handle)?.acknowledgesInput() === true } - }) + ) function deferViewportForRecovery(claim: boolean): void { // Why: the replay sends the latest viewport; an older queued batch flushing after it would win. @@ -4107,7 +4118,7 @@ export function createRemoteRuntimePtyTransport( return sendInputNow(data) }, - // Why: query replies (CPR/DSR/DA/OSC) are read in raw mode with a short timeout; the 8ms debounce would miss it and echo the reply onto the prompt (#7329). + // Why: query replies (CPR/DSR/DA/OSC) are read in raw mode with a short timeout; the 8ms coalescing window would miss it and echo the reply onto the prompt (#7329). sendInputImmediate: (data: string): boolean => sendInputImmediateNow(data), sendInputAccepted(data, inputKind, options) { diff --git a/src/shared/terminal/stream/terminal-stream-protocol.ts b/src/shared/terminal/stream/terminal-stream-protocol.ts index d68adbf902f..20b9b670e51 100644 --- a/src/shared/terminal/stream/terminal-stream-protocol.ts +++ b/src/shared/terminal/stream/terminal-stream-protocol.ts @@ -100,7 +100,8 @@ export function decodeTerminalStreamFrame(bytes: Uint8Array): TerminalStreamFram opcode, streamId: view.getUint32(4, true), seq: high * 0x100000000 + low, - payload: bytes.slice(HEADER_BYTES) + // Why a view: each message is a fresh buffer, and only the header is ever rewritten in place. + payload: bytes.subarray(HEADER_BYTES) } }