Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

fix(terminal): leave Caps Lock composition commits to compositionend

review24597cbf7516
diff --git a/CLAUDE.md b/CLAUDE.md
index b3d6908df..e018ad9bd 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -4708,3 +4708,11 @@ snapshot (bounded to 512 events / 3 seconds); live permission alerts are not del
No disk restore, transcript backlog, completed-card replay, or authorization evidence is provided.
A host restart or a missed original start is still outside this recovery.
+
+## IME mode switching (#680)
+
+`terminal/ime-mode-switch.ts` adapts xterm 5.5's composition helper after `open()` in both terminal
+surfaces. Caps Lock must not finalize an active composition: the subsequent native compositionend
+owns that commit. The test executes the dependency's real helper, with an unpatched double-send
+control. This pins one event ordering, not every native IME; macOS Chinese Caps Lock still needs a
+device run, including insertText and compositionend orderings. Revalidate the adapter on xterm upgrades.
diff --git a/src/renderer/components/kanban/ModalTerminal.tsx b/src/renderer/components/kanban/ModalTerminal.tsx
index 5ffc557c9..27ed48748 100644
--- a/src/renderer/components/kanban/ModalTerminal.tsx
+++ b/src/renderer/components/kanban/ModalTerminal.tsx
@@ -1,3 +1,4 @@
+import { patchImeModeSwitch } from '../../terminal/ime-mode-switch'
import { useCallback, useEffect, useRef, useState } from 'react'
import { Terminal } from '@xterm/xterm'
import { FitAddon } from '@xterm/addon-fit'
@@ -193,6 +194,7 @@ export function ModalTerminal({ nodeId, spawn, searchOpen, onCloseSearch }: Moda
fitRef.current = fit
transportRef.current = transport
term.open(hostRef.current!)
+ patchImeModeSwitch(term)
// Renderer-parity with the canvas terminals (see char-size-quantize): the modal co-views
// the same session, so its column math must match what the canvas draws.
quantizeCharSize(term)
diff --git a/src/renderer/nodes/TerminalNode.tsx b/src/renderer/nodes/TerminalNode.tsx
index d7c01531f..22b7519bb 100644
--- a/src/renderer/nodes/TerminalNode.tsx
+++ b/src/renderer/nodes/TerminalNode.tsx
@@ -1,3 +1,4 @@
+import { patchImeModeSwitch } from '../terminal/ime-mode-switch'
import { Suspense, lazy, useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react'
import { NODE_MIN_SIZES } from '../lib/nodeSizing'
import {
@@ -2834,6 +2835,7 @@ export function TerminalNode({
term.loadAddon(fit)
term.loadAddon(searchAddon)
term.open(container)
+ patchImeModeSwitch(term)
// Renderer-parity: quantize the char measurement to the device-pixel grid, so a budget
// grant/release swaps renderers without the text visibly reflowing (see the helper).
quantizeCharSize(term)
diff --git a/src/renderer/terminal/ime-mode-switch.test.ts b/src/renderer/terminal/ime-mode-switch.test.ts
new file mode 100644
index 000000000..d798b6110
--- /dev/null
+++ b/src/renderer/terminal/ime-mode-switch.test.ts
@@ -0,0 +1,63 @@
+import { afterEach, expect, it, vi } from 'vitest'
+import { buildSync } from 'esbuild'
+import { resolve, dirname } from 'node:path'
+import { createRequire } from 'node:module'
+import type { Terminal } from '@xterm/xterm'
+import { patchImeModeSwitch } from './ime-mode-switch'
+
+// Execute the dependency's real composition helper, not a hand-written copy of its algorithm.
+// Bundling in memory resolves xterm's source aliases without writing into node_modules.
+const require = createRequire(import.meta.url)
+const { JSDOM } = require('jsdom') as { JSDOM: new (html: string) => { window: { document: Document } } }
+const src = resolve(dirname(require.resolve('@xterm/xterm/package.json')), 'src')
+const built = buildSync({
+ entryPoints: [resolve(src, 'browser/input/CompositionHelper.ts')], bundle: true,
+ platform: 'node', format: 'cjs', write: false,
+ alias: { common: resolve(src, 'common'), browser: resolve(src, 'browser') },
+ tsconfigRaw: { compilerOptions: { experimentalDecorators: true } }
+})
+const mod = { exports: {} as { CompositionHelper: new (...args: unknown[]) => any } }
+new Function('module', 'exports', built.outputFiles[0].text)(mod, mod.exports)
+const document = new JSDOM('').window.document
+function fixture(patch = true) {
+ const textarea = document.createElement('textarea')
+ const sent: string[] = []
+ const helper = new mod.exports.CompositionHelper(textarea, document.createElement('div'), {}, {}, {
+ triggerDataEvent: (s: string) => sent.push(s)
+ }, {})
+ helper.updateCompositionElements = () => {} // geometry is unrelated to the input lifecycle
+ const term = { _core: { _compositionHelper: helper } } as unknown as Terminal
+ if (patch) { patchImeModeSwitch(term); patchImeModeSwitch(term) }
+ return { textarea, helper, sent }
+}
+async function compose(f: ReturnType<typeof fixture>, text = '中文') {
+ f.helper.compositionstart()
+ f.textarea.value += text
+ f.helper.compositionupdate({ data: text })
+ await vi.advanceTimersByTimeAsync(1)
+}
+afterEach(() => vi.useRealTimers())
+it('CONTROL: upstream sends twice when Caps Lock precedes compositionend', async () => {
+ vi.useFakeTimers(); const f = fixture(false)
+ await compose(f)
+ f.helper.keydown({ key: 'CapsLock', keyCode: 20 })
+ f.helper.compositionend(); await vi.runAllTimersAsync()
+ expect(f.sent).toEqual(['中文', '中文'])
+})
+it('Caps Lock leaves one commit to compositionend and permits intentional repetition', async () => {
+ vi.useFakeTimers(); const f = fixture()
+ for (let i = 0; i < 2; i++) {
+ await compose(f)
+ expect(f.helper.keydown({ key: 'CapsLock', keyCode: 20 })).toBe(false)
+ expect(f.sent).toHaveLength(i)
+ f.helper.compositionend(); await vi.runAllTimersAsync()
+ }
+ expect(f.sent).toEqual(['中文', '中文'])
+})
+it('ordinary Enter still commits synchronously before terminal submission', async () => {
+ vi.useFakeTimers(); const f = fixture()
+ await compose(f)
+ expect(f.helper.keydown({ key: 'Enter', keyCode: 13 })).toBe(true)
+ expect(f.sent).toEqual(['中文'])
+ expect(f.helper.keydown({ key: 'CapsLock', keyCode: 20 })).toBe(true)
+})
diff --git a/src/renderer/terminal/ime-mode-switch.ts b/src/renderer/terminal/ime-mode-switch.ts
new file mode 100644
index 000000000..777b10898
--- /dev/null
+++ b/src/renderer/terminal/ime-mode-switch.ts
@@ -0,0 +1,23 @@
+import type { Terminal } from '@xterm/xterm'
+
+type CompositionHelper = {
+ isComposing: boolean
+ keydown(event: KeyboardEvent): boolean
+}
+const patched = new WeakSet<CompositionHelper>()
+
+/** xterm 5.5 treats Caps Lock as a commit key and sends the composition immediately. A
+ * following native compositionend sends the same text again. Let that event own the commit:
+ * Caps Lock changes input mode, it is not terminal input. Enter and ordinary keys keep xterm's
+ * existing finalize-before-key ordering. Install after open(), including in the board modal.
+ * This is a version-bound adapter, like scale-fix; the test executes xterm's actual helper. */
+export function patchImeModeSwitch(term: Terminal): void {
+ const helper = (term as unknown as { _core?: { _compositionHelper?: CompositionHelper } })._core?._compositionHelper
+ if (!helper || patched.has(helper)) return
+ const keydown = helper.keydown.bind(helper)
+ helper.keydown = (event) => {
+ if (helper.isComposing && (event.key === 'CapsLock' || event.keyCode === 20)) return false
+ return keydown(event)
+ }
+ patched.add(helper)
+}