fix(dashboard): gate shortcut sends on PTY state

The word-delete branches sent ^W / ESC d whenever the socket was OPEN,
bypassing the shouldBlockPtyInput gate that term.onData applies to
every other keystroke, so a reconnecting/closed session could still
receive shortcut bytes. Route both branches through a shared guarded
sender that applies the identical socket + PTY state check, and cover
the non-open PTY states in the lib tests.
This commit is contained in:
null-runner
2026-07-16 15:20:29 +02:00
committed by Teknium
parent be250390db
commit 4e6d863d0e
3 changed files with 66 additions and 7 deletions
+39 -2
View File
@@ -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<KeyboardEvent> = {}) =>
({
@@ -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<PtyConnectionState>(['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()
})
})
+21
View File
@@ -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<WebSocket, "readyState" | "send"> | null,
ptyState: PtyConnectionState,
sequence: string,
): boolean {
if (!ws || ws.readyState !== WS_OPEN || shouldBlockPtyInput(ptyState)) {
return false;
}
ws.send(sequence);
return true;
}
+6 -5
View File
@@ -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;
}