diff --git a/web/src/lib/pty-keyboard-shortcuts.test.ts b/web/src/lib/pty-keyboard-shortcuts.test.ts index be6f52be06..ad645f05c8 100644 --- a/web/src/lib/pty-keyboard-shortcuts.test.ts +++ b/web/src/lib/pty-keyboard-shortcuts.test.ts @@ -1,6 +1,10 @@ -import { describe, expect, it } from 'vitest' +import { describe, expect, it, vi } from 'vitest' -import { resolvePtyKeyboardShortcut } from './pty-keyboard-shortcuts' +import { + resolvePtyKeyboardShortcut, + sendPtyShortcutSequence, +} from './pty-keyboard-shortcuts' +import type { PtyConnectionState } from './pty-reconnect' const key = (overrides: Partial = {}) => ({ @@ -41,3 +45,36 @@ describe('resolvePtyKeyboardShortcut', () => { ).toBe('delete-word-forward') }) }) + +describe('sendPtyShortcutSequence', () => { + const socket = (readyState: number = WebSocket.OPEN) => ({ + readyState, + send: vi.fn(), + }) + + it('sends the sequence when the socket and PTY state are open', () => { + const ws = socket() + + expect(sendPtyShortcutSequence(ws, 'open', '\x17')).toBe(true) + expect(ws.send).toHaveBeenCalledOnce() + expect(ws.send).toHaveBeenCalledWith('\x17') + }) + + it.each(['connecting', 'reconnecting', 'closed', 'ended'])( + 'blocks shortcut bytes while the PTY state is %s', + (state) => { + const ws = socket() + + expect(sendPtyShortcutSequence(ws, state, '\x1bd')).toBe(false) + expect(ws.send).not.toHaveBeenCalled() + }, + ) + + it('blocks shortcut bytes when the socket is missing or not open', () => { + const ws = socket(WebSocket.CLOSING) + + expect(sendPtyShortcutSequence(null, 'open', '\x17')).toBe(false) + expect(sendPtyShortcutSequence(ws, 'open', '\x17')).toBe(false) + expect(ws.send).not.toHaveBeenCalled() + }) +}) diff --git a/web/src/lib/pty-keyboard-shortcuts.ts b/web/src/lib/pty-keyboard-shortcuts.ts index db501d0e83..3ed94d6e86 100644 --- a/web/src/lib/pty-keyboard-shortcuts.ts +++ b/web/src/lib/pty-keyboard-shortcuts.ts @@ -1,3 +1,7 @@ +import { shouldBlockPtyInput, type PtyConnectionState } from "./pty-reconnect"; + +const WS_OPEN = 1; + export type PtyKeyboardShortcut = | "copy" | "delete-word-backward" @@ -40,3 +44,20 @@ export function resolvePtyKeyboardShortcut( return "pass"; } + +// Guarded sender for shortcut escape sequences. Applies the same gate as the +// term.onData path (socket OPEN + shouldBlockPtyInput) so a reconnecting or +// closed session never receives shortcut bytes. +export function sendPtyShortcutSequence( + ws: Pick | null, + ptyState: PtyConnectionState, + sequence: string, +): boolean { + if (!ws || ws.readyState !== WS_OPEN || shouldBlockPtyInput(ptyState)) { + return false; + } + + ws.send(sequence); + + return true; +} diff --git a/web/src/pages/ChatPage.tsx b/web/src/pages/ChatPage.tsx index ffc90771ec..6aeda855ed 100644 --- a/web/src/pages/ChatPage.tsx +++ b/web/src/pages/ChatPage.tsx @@ -60,7 +60,10 @@ import { normalizePtyMobileInput, shouldTreatInputAsMobileReplacement, } from "@/lib/pty-mobile-input"; -import { resolvePtyKeyboardShortcut } from "@/lib/pty-keyboard-shortcuts"; +import { + resolvePtyKeyboardShortcut, + sendPtyShortcutSequence, +} from "@/lib/pty-keyboard-shortcuts"; import { isViewportPinnedToBottom, shouldFollowPtyOutput, @@ -708,8 +711,7 @@ export default function ChatPage({ isActive = true }: { isActive?: boolean }) { // for Ctrl+W muscle memory use the Electron desktop app.) if (shortcut === "delete-word-backward") { ev.preventDefault(); - const ws = wsRef.current; - if (ws && ws.readyState === WebSocket.OPEN) ws.send("\x17"); + sendPtyShortcutSequence(wsRef.current, ptyStateRef.current, "\x17"); return false; } @@ -717,8 +719,7 @@ export default function ChatPage({ isActive = true }: { isActive?: boolean }) { // (ESC d), the readline / prompt_toolkit kill-word-forward binding. if (shortcut === "delete-word-forward") { ev.preventDefault(); - const ws = wsRef.current; - if (ws && ws.readyState === WebSocket.OPEN) ws.send("\x1bd"); + sendPtyShortcutSequence(wsRef.current, ptyStateRef.current, "\x1bd"); return false; }