Fix Ctrl+Alt terminal input on Windows by repairing xterm's AltGr misclassification (#8810)

* fix(terminal): forward Windows Ctrl Alt chords

* fix(terminal): route rescued Windows Ctrl+Alt chords through xterm's own key encoders

Replace the hand-rolled Alt-prefix encoder in the custom key handler with a
narrow repair of xterm's third-level-shift classification. xterm's keyboard
service already computes the correct bytes for every input protocol (legacy
ESC-prefixed, kitty CSI-u, win32-input-mode) before _isThirdLevelShift
discards them on Windows Ctrl+Alt; rescuing only provably-genuine chords
(Chromium's layout-wide AltGraph simulation, crbug 762557) lets those
encoders deliver protocol-correct, layout-aware bytes with no duplicated
encoding knowledge in Orca.

Fixes vs the previous approach: kitty-mode TUIs now receive CSI-u instead of
legacy bytes, digits/punctuation no longer alias to plain Alt chords,
letters follow the logical layout (Dvorak/Colemak), Ctrl+Alt+Shift and
Ctrl+Alt+F-keys gain Linux parity, and handled keys get xterm's stock
preventDefault/stopPropagation. Firefox web clients keep stock behavior; a
real-Terminal contract test fails loudly if an xterm upgrade removes the
seam, degrading at runtime to the historical dropped-chord behavior.

Co-authored-by: Orca <help@stably.ai>

* Refactor Windows Ctrl+Alt chord test helpers and clarify AltGraph commen

- Extract a shared getCore() helper in the test file to dedupe repeated
  `_core` casts across third-level-shift and keyboard-service lookups.
- Correct the AltGraph comment: Chromium simulates AltGraph per composing
  keypress, not for the whole Ctrl+Alt press duration.
- Warn via console when xterm no longer exposes `_core._isThirdLevelShift`,
  so a silent classification-repair failure is diagnosable in the wild.

* Clarify comment explaining why Windows Ctrl+Alt chords bypass AltGraph c

The comment previously implied Chromium always sets AltGraph=true for
composable chords; the revised wording states the actual mechanism
(Alt+Ctrl modifiers get replaced by AltGraph) so the inverse case is
unambiguous.

---------

Co-authored-by: Jinjing <6427696+AmethystLiang@users.noreply.github.com>
Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
BingZ
2026-07-16 16:38:20 -07:00
committed by GitHub
co-authored by Jinjing Orca
parent dd0f4c39c8
commit 8461df2139
4 changed files with 281 additions and 54 deletions
+18 -54
View File
@@ -29,60 +29,11 @@ import {
focusActiveTerminalInput
} from './helpers/terminal'
import { waitForSessionReady, waitForActiveWorktree, ensureTerminalVisible } from './helpers/store'
// Why: contextBridge freezes window.api so the renderer cannot spy on
// pty.write directly. Intercept in the main process instead — pty:write is an
// ipcMain.on listener, so prepending a listener lets us capture every call
// without disturbing the real handler.
async function installMainProcessPtyWriteSpy(app: ElectronApplication): Promise<void> {
await app.evaluate(({ ipcMain }) => {
const g = globalThis as unknown as {
__ptyWriteLog?: { id: string; data: string }[]
__ptyWriteSpyInstalled?: boolean
__ptyWriteAcceptedSpyInstalled?: boolean
}
if (g.__ptyWriteSpyInstalled) {
return
}
g.__ptyWriteLog = []
g.__ptyWriteSpyInstalled = true
ipcMain.prependListener('pty:write', (_event: unknown, args: { id: string; data: string }) => {
g.__ptyWriteLog!.push({ id: args.id, data: args.data })
})
const invokeHandlers = (
ipcMain as unknown as {
_invokeHandlers?: Map<
string,
(event: unknown, args: { id: string; data: string }) => unknown
>
}
)._invokeHandlers
const writeAcceptedHandler = invokeHandlers?.get('pty:writeAccepted')
if (writeAcceptedHandler && !g.__ptyWriteAcceptedSpyInstalled) {
g.__ptyWriteAcceptedSpyInstalled = true
invokeHandlers?.set('pty:writeAccepted', (event, args) => {
g.__ptyWriteLog!.push({ id: args.id, data: args.data })
return writeAcceptedHandler(event, args)
})
}
})
}
async function clearPtyWriteLog(app: ElectronApplication): Promise<void> {
await app.evaluate(() => {
const g = globalThis as unknown as { __ptyWriteLog?: { id: string; data: string }[] }
if (g.__ptyWriteLog) {
g.__ptyWriteLog.length = 0
}
})
}
async function getPtyWrites(app: ElectronApplication): Promise<string[]> {
return app.evaluate(() => {
const g = globalThis as unknown as { __ptyWriteLog?: { id: string; data: string }[] }
return (g.__ptyWriteLog ?? []).map((e) => e.data)
})
}
import {
clearTerminalPtyWriteLog as clearPtyWriteLog,
installTerminalPtyWriteSpy as installMainProcessPtyWriteSpy,
readTerminalPtyWrites as getPtyWrites
} from './helpers/terminal-pty-write-spy'
async function setActivePaneForegroundAgent(
page: Page,
@@ -562,6 +513,19 @@ test.describe('Terminal Shortcuts', () => {
}
})
test('Windows forwards genuine Ctrl+Alt text chords to the PTY', async ({
orcaPage,
electronApp
}) => {
test.skip(process.platform !== 'win32', 'Windows xterm AltGr classification regression')
await installMainProcessPtyWriteSpy(electronApp)
await waitForActivePanePtyId(orcaPage)
await pressAndExpectWrite(orcaPage, electronApp, 'Control+Alt+u', '\x1b\x15')
await pressAndExpectWrite(orcaPage, electronApp, 'Control+Alt+2', '\x1b2')
await pressAndExpectWrite(orcaPage, electronApp, 'Control+Alt+;', '\x1b;')
})
test('Ctrl+Enter writes the kitty modified-enter chord for terminal TUIs', async ({
orcaPage,
electronApp