#1303·terax-ai

[Bug] macOS: fast typing drops characters with Doubao IME in English mode (v0.8.6)

Author: metanqyCreated Sep 21, 2026Updated Sep 21, 2026

Terax version

0.8.6 (latest stable release at the time of reporting), based on tag v0.8.6, commit 1fdbc50e53b3ac53db3ba80057805a2d54258545.

Operating system

macOS (Apple Silicon)

OS version

macOS 26.6.2 (25G83)

What happened?

When using Doubao (豆包) IME in English mode, typing qwer or 1234 quickly frequently drops characters in Terax's terminal, including programs running inside it. The same input method does not drop these characters in Notes.app on the same machine.

This is ordinary English/digit input while Doubao remains the selected input source, not Chinese candidate composition.

What did you expect to happen?

Every committed character should reach the terminal exactly once and in order, regardless of whether the previous physical key has been released.

Steps to reproduce

  1. Open Terax 0.8.6 on macOS.
  2. Select Doubao IME and switch it to English mode. Do not switch the system input source to ABC.
  3. Focus a terminal shell prompt or an interactive program's input.
  4. Quickly type qwer and 1234, with successive key presses overlapping before the previous key is released. Repeat a few times.
  5. Observe missing characters. Compare with Notes.app using the same IME.

Diagnosis

This matches xtermjs/xterm.js#5887, which documents the same Doubao English-mode behavior.

Terax 0.8.6 ships xterm.js 6.0.0. Doubao can emit committed input / insertText before keydown, with keyCode=229 even for English letters and digits and without an active composition. The next input can arrive while xterm's _keyDownSeen is still set by the preceding key. xterm then suppresses the committed text. Terax's custom key handler also returns false for keyCode=229, so the normal key path does not recover it.

The event-order diagnosis was reproduced with the actual xterm.js 6.0.0 component inside a native macOS WKWebView. This was synthetic DOM event replay, not a recording of the reporter's physical keystrokes.

Local fix and validation

A local compatibility patch against v0.8.6 recovers committed insertText only when neither the corresponding key event nor xterm's input handler has already emitted it. It uses public terminal APIs, is installed only on macOS, leaves native composition and its deferred commit to xterm, respects disabled input/screen-reader mode, and resets/disposes state with the renderer slot.

Controlled event replay with input preceding keydown and key releases delayed:

Input Unmodified v0.8.6 input path With local patch
qwer q qwer
1234 1 1234
qqqq / 1111 first character only all four characters
qwer1234 repeated 1,000 times 1 character emitted all 8,000 characters, exact order

The long sequence deliberately stresses the stale-key-state path; it is not a claim about 8,000 hardware keystrokes typed in one burst.

Additional validation:

  • All 12 native WKWebView replay scenarios passed, including normal ABC input, space followed by IME input, repeated characters, input-method transitions, Chinese composition and trailing text, and disabled input.
  • In a compiled Terax app with an isolated profile, 800 characters entered through native UI automation were received byte-for-byte by a program reading the PTY. This is a separate end-to-end smoke test, not the overlapping-key event replay.
  • 19 new regression tests; all 537 frontend tests passed.
  • TypeScript and frontend lint completed successfully (existing lint warnings remain); all 258 Rust tests and Clippy passed.
  • The patched macOS app built successfully and was installed locally.

Scope: this report and patch target the latest stable 0.8.6 release. The current development branch has migrated away from xterm; its new terminal backend has not been tested for this specific Doubao behavior. Related Korean/CJK reports #147 and #1105 concern different input symptoms.

Complete v0.8.6 patch, including regression tests

Save the following as a patch file and apply it to a clean checkout of v0.8.6 with git apply.

diff
diff --git a/src/modules/terminal/lib/committedInput.test.ts b/src/modules/terminal/lib/committedInput.test.ts
new file mode 100644
index 0000000..f102bda
--- /dev/null
+++ b/src/modules/terminal/lib/committedInput.test.ts
@@ -0,0 +1,180 @@
+import { afterEach, describe, expect, it, vi } from "vitest";
+import { installCommittedInputFallback } from "@/modules/terminal/lib/committedInput";
+import type { Terminal } from "@xterm/xterm";
+
+function harness() {
+  const element = new EventTarget();
+  const textarea = new EventTarget();
+  const dataListeners = new Set<(data: string) => void>();
+  const keyListeners = new Set<
+    (event: { key: string; domEvent: KeyboardEvent }) => void
+  >();
+  const output: string[] = [];
+  const options = { disableStdin: false, screenReaderMode: false };
+  const emit = (data: string) => {
+    if (options.disableStdin) return;
+    output.push(data);
+    for (const listener of dataListeners) listener(data);
+  };
+  const bridge = installCommittedInputFallback({
+    element: element as HTMLElement,
+    textarea: textarea as HTMLTextAreaElement,
+    options,
+    input: emit,
+    onData: (listener) => {
+      dataListeners.add(listener);
+      return { dispose: () => dataListeners.delete(listener) };
+    },
+    onKey: (listener) => {
+      keyListeners.add(listener);
+      return { dispose: () => keyListeners.delete(listener) };
+    },
+  } as Pick<
+    Terminal,
+    "element" | "textarea" | "options" | "input" | "onData" | "onKey"
+  >);
+  return {
+    bridge,
+    options,
+    emit,
+    text: () => output.join(""),
+    key(data?: string, cancelled = false) {
+      const event = new Event("keydown", { cancelable: true });
+      element.dispatchEvent(event);
+      if (data) {
+        for (const listener of keyListeners) {
+          listener({ key: data, domEvent: event as KeyboardEvent });
+        }
+        emit(data);
+      }
+      if (cancelled) event.preventDefault();
+    },
+    input(
+      data: string | null,
+      nativeOutput = "",
+      inputType = "insertText",
+      isComposing = false,
+    ) {
+      const event = Object.assign(new Event("input"), {
+        data,
+        inputType,
+        isComposing,
+      });
+      // Replay the ancestor capture, xterm capture, and fallback capture order.
+      element.dispatchEvent(event);
+      if (nativeOutput) emit(nativeOutput);
+      textarea.dispatchEvent(event);
+    },
+    event: (type: string) => textarea.dispatchEvent(new Event(type)),
+  };
+}
+
+afterEach(() => vi.useRealTimers());
+
+describe("macOS committed terminal input", () => {
+  it.each(["qwer", "1234", "qqqq", "1111", "qwer1234".repeat(1000)])(
+    "preserves overlapping input-before-keydown events: %.20s",
+    (text) => {
+      const h = harness();
+      for (const [index, char] of [...text].entries()) {
+        h.input(char, index === 0 ? char : "");
+        h.key();
+      }
+      expect(h.text()).toBe(text);
+    },
+  );
+
+  it("does not duplicate input xterm already delivered", () => {
+    const h = harness();
+    for (const char of "qwer 1234你好") h.input(char, char);
+    expect(h.text()).toBe("qwer 1234你好");
+  });
+
+  it("does not duplicate normal keypress input and recovers the next IME key", () => {
+    const h = harness();
+    h.key(" ");
+    h.input(" ");
+    h.input("q");
+    h.key();
+    expect(h.text()).toBe(" q");
+  });
+
+  it("does not consume the next identical character after a cancelled keydown", () => {
+    const h = harness();
+    h.key("q", true);
+    h.input("q");
+    h.key();
+    expect(h.text()).toBe("qq");
+  });
+
+  it("clears key correlation on the next keydown", () => {
+    const h = harness();
+    h.key("q");
+    h.key();
+    h.input("q");
+    expect(h.text()).toBe("qq");
+  });
+
+  it("leaves real composition and its deferred trailing text to xterm", () => {
+    vi.useFakeTimers();
+    const h = harness();
+    h.event("compositionstart");
+    h.input("ni", "", "insertCompositionText", true);
+    h.input("你");
+    h.event("compositionend");
+    h.input("1");
+    expect(h.text()).toBe("");
+    h.emit("你1");
+    vi.runAllTimers();
+    h.input("q");
+    expect(h.text()).toBe("你1q");
+  });
+
+  it("leaves composition-marked events alone even without compositionstart", () => {
+    const h = harness();
+    h.input("n", "", "insertText", true);
+    expect(h.text()).toBe("");
+  });
+
+  it.each([
+    "insertFromPaste",
+    "insertReplacementText",
+    "deleteContentBackward",
+    "insertFromComposition",
+  ])("does not reinterpret %s", (type) => {
+    const h = harness();
+    h.input("q", "", type);
+    expect(h.text()).toBe("");
+  });
+
+  it.each(["disableStdin", "screenReaderMode"] as const)(
+    "respects %s",
+    (option) => {
+      const h = harness();
+      h.options[option] = true;
+      h.input("q");
+      expect(h.text()).toBe("");
+    },
+  );
+
+  it("clears old ownership and composition state on reset and blur", () => {
+    const h = harness();
+    h.key("q");
+    h.bridge.reset();
+    h.input("q");
+    h.event("compositionstart");
+    h.event("blur");
+    h.input("1");
+    expect(h.text()).toBe("qq1");
+  });
+
+  it("removes listeners and pending timers on disposal", () => {
+    vi.useFakeTimers();
+    const h = harness();
+    h.event("compositionend");
+    h.bridge.dispose();
+    h.input("q");
+    expect(h.text()).toBe("");
+    expect(vi.getTimerCount()).toBe(0);
+  });
+});
diff --git a/src/modules/terminal/lib/committedInput.ts b/src/modules/terminal/lib/committedInput.ts
new file mode 100644
index 0000000..e6c6e03
--- /dev/null
+++ b/src/modules/terminal/lib/committedInput.ts
@@ -0,0 +1,102 @@
+import type { Terminal } from "@xterm/xterm";
+
+export type CommittedInputFallback = {
+  reset: () => void;
+  dispose: () => void;
+};
+
+export function installCommittedInputFallback(
+  term: Pick<
+    Terminal,
+    "textarea" | "element" | "options" | "onKey" | "onData" | "input"
+  >,
+): CommittedInputFallback {
+  const textarea = term.textarea;
+  const element = term.element;
+  if (!textarea || !element) throw new Error("Terminal must be open");
+
+  let key: { key: string; domEvent: KeyboardEvent } | null = null;
+  let inputEvent: Event | null = null;
+  let delivered = false;
+  let composing = false;
+  let compositionTimer: ReturnType<typeof setTimeout> | null = null;
+
+  const clearKey = () => {
+    key = null;
+  };
+  const reset = () => {
+    clearKey();
+    inputEvent = null;
+    composing = false;
+    if (compositionTimer !== null) clearTimeout(compositionTimer);
+    compositionTimer = null;
+  };
+  const beginInput = (event: Event) => {
+    inputEvent = event;
+    delivered = false;
+  };
+  const keyListener = term.onKey((event) => {
+    key = event;
+  });
+  const dataListener = term.onData(() => {
+    if (inputEvent) delivered = true;
+  });
+  const finishInput = (event: Event) => {
+    const e = event as InputEvent;
+    const sentByKey = key?.key === e.data && !key.domEvent.defaultPrevented;
+    clearKey();
+    const sentByInput = inputEvent === event && delivered;
+    inputEvent = null;
+    if (
+      e.inputType !== "insertText" ||
+      !e.data ||
+      e.isComposing ||
+      composing ||
+      compositionTimer !== null ||
+      term.options.disableStdin ||
+      term.options.screenReaderMode ||
+      sentByKey ||
+      sentByInput
+    ) {
+      return;
+    }
+    // Some macOS IMEs emit input before keydown; xterm's previous-key flags
+    // can discard that committed text. Recover only text it did not emit.
+    term.input(e.data, true);
+  };
+  const compositionStart = () => {
+    reset();
+    composing = true;
+  };
+  const compositionEnd = () => {
+    composing = false;
+    clearKey();
+    if (compositionTimer !== null) clearTimeout(compositionTimer);
+    // xterm defers its composition commit and includes trailing text in it.
+    compositionTimer = setTimeout(() => {
+      compositionTimer = null;
+    }, 0);
+  };
+
+  element.addEventListener("input", beginInput, { capture: true });
+  element.addEventListener("keydown", clearKey, { capture: true });
+  textarea.addEventListener("input", finishInput, { capture: true });
+  textarea.addEventListener("compositionstart", compositionStart);
+  textarea.addEventListener("compositionend", compositionEnd);
+  textarea.addEventListener("blur", reset);
+
+  return {
+    reset,
+    dispose: () => {
+      reset();
+      keyListener.dispose();
+      dataListener.dispose();
+      element.removeEventListener("input", beginInput, { capture: true });
+      element.removeEventListener("keydown", clearKey, { capture: true });
+      textarea.removeEventListener("input", finishInput, { capture: true });
+      textarea.removeEventListener("compositionstart", compositionStart);
+      textarea.removeEventListener("compositionend", compositionEnd);
+      textarea.removeEventListener("blur", reset);
+    },
+  };
+}
diff --git a/src/modules/terminal/lib/rendererPool.ts b/src/modules/terminal/lib/rendererPool.ts
index 4ec4023..79978e8 100644
--- a/src/modules/terminal/lib/rendererPool.ts
+++ b/src/modules/terminal/lib/rendererPool.ts
@@ -1,4 +1,8 @@
 import { resolveFontFamily } from "@/lib/fonts";
+import {
+  type CommittedInputFallback,
+  installCommittedInputFallback,
+} from "@/modules/terminal/lib/committedInput";
 import { usePreferencesStore } from "@/modules/settings/preferences";
 import { buildTerminalTheme } from "@/styles/terminalTheme";
 import { openUrl } from "@tauri-apps/plugin-opener";
@@ -48,6 +52,7 @@ export type Slot = {
   readonly searchAddon: SearchAddon;
   readonly serializeAddon: SerializeAddon;
   readonly host: HTMLDivElement;
+  inputFallback: CommittedInputFallback | null;
   webglAddon: WebglAddon | null;
   webglCanvases: HTMLCanvasElement[];
   currentLeafId: number | null;
@@ -227,6 +232,7 @@ function createSlot(): Slot {
     searchAddon,
     serializeAddon,
     host,
+    inputFallback: null,
     webglAddon: null,
     webglCanvases: [],
     currentLeafId: null,
@@ -246,6 +252,8 @@ function createSlot(): Slot {
     lastUsedAt: 0,
   };
 
+  if (IS_MAC) slot.inputFallback = installCommittedInputFallback(term);
+
   term.attachCustomKeyEventHandler((event) => {
     // During IME composition the browser is assembling a multi-keystroke
     // character (Chinese pinyin → hanzi, Korean jamo → syllable, etc.).
@@ -430,6 +438,7 @@ function bindSlot(slot: Slot, p: AcquireParams): void {
   const hadWebgl = !!slot.webglAddon;
   slot.retainedLeafId = null;
   slot.currentLeafId = p.leafId;
+  slot.inputFallback?.reset();
   slot.lastUsedAt = performance.now();
 
   cancelPendingUnhide(slot);
@@ -665,6 +674,7 @@ function detachSlotFromLeaf(slot: Slot, retain: boolean): void {
   slot.host.style.visibility = "";
 
   slot.currentLeafId = null;
+  slot.inputFallback?.reset();
   slot.lastUsedAt = performance.now();
   scheduleWebglReap(slot);
   scheduleSlotReap(slot);
@@ -744,6 +754,8 @@ function disposeSlot(slot: Slot): void {
     } catch {}
   }
   slot.oscDisposers = [];
+  slot.inputFallback?.dispose();
+  slot.inputFallback = null;
   disposeSlotWebgl(slot);
   try {
     slot.term.dispose();

Before submitting

  • I searched existing issues and did not find a duplicate for Doubao English-mode fast typing.
  • This was reproduced on the latest stable release, v0.8.6. The development branch is explicitly outside the tested scope above.