Skip to content

The agent that grows with you

fix(web): drop mobile IME double-sends in the chat PTY input path

Konstantin Khlopkov3293dd33e311
diff --git a/web/src/lib/pty-composition.test.ts b/web/src/lib/pty-composition.test.ts
index 95429082a64..3d0f5693552 100644
--- a/web/src/lib/pty-composition.test.ts
+++ b/web/src/lib/pty-composition.test.ts
@@ -133,3 +133,120 @@ describe("createPtyCompositionForwarder", () => {
expect(send).not.toHaveBeenCalled();
});
});
+
+describe("mobile IME double-send dedup", () => {
+ afterEach(() => vi.useRealTimers());
+
+ it("ignores a duplicate compositionend carrying identical data", () => {
+ vi.useFakeTimers();
+ const send = vi.fn();
+ const forwarder = createPtyCompositionForwarder(send);
+
+ forwarder.onCompositionEnd("hello");
+ vi.runAllTimers();
+ forwarder.onCompositionEnd("hello");
+ vi.runAllTimers();
+
+ expect(send).toHaveBeenCalledTimes(1);
+ expect(send).toHaveBeenCalledWith("hello");
+ });
+
+ it("still forwards a re-typed word once the echo window has passed", () => {
+ vi.useFakeTimers();
+ const send = vi.fn();
+ const forwarder = createPtyCompositionForwarder(send);
+
+ forwarder.onCompositionEnd("hello");
+ vi.runAllTimers();
+ vi.advanceTimersByTime(81);
+ forwarder.onCompositionEnd("hello");
+ vi.runAllTimers();
+
+ expect(send).toHaveBeenCalledTimes(2);
+ });
+
+ it("ignores a compositionend trailing terminal data that already carried the commit", () => {
+ vi.useFakeTimers();
+ const send = vi.fn();
+ const forwarder = createPtyCompositionForwarder(send);
+
+ // Android: xterm onData fires first with the committed word…
+ forwarder.noteTerminalData("hello");
+ // …and the late compositionend would arm a fresh fallback duplicate.
+ forwarder.onCompositionEnd("hello");
+ vi.runAllTimers();
+
+ expect(send).not.toHaveBeenCalled();
+ });
+
+ it("recognizes the commit when onData carries it split across chunks", () => {
+ vi.useFakeTimers();
+ const send = vi.fn();
+ const forwarder = createPtyCompositionForwarder(send);
+
+ forwarder.noteTerminalData("he");
+ forwarder.noteTerminalData("llo");
+ forwarder.onCompositionEnd("hello");
+ vi.runAllTimers();
+
+ expect(send).not.toHaveBeenCalled();
+ });
+
+ it("still sends when terminal data differs from the trailing compositionend", () => {
+ vi.useFakeTimers();
+ const send = vi.fn();
+ const forwarder = createPtyCompositionForwarder(send);
+
+ forwarder.noteTerminalData("hello");
+ forwarder.onCompositionEnd("world");
+ vi.runAllTimers();
+
+ expect(send).toHaveBeenCalledExactlyOnceWith("world");
+ });
+
+ it("drops an onData payload that echoes just-committed composition text", () => {
+ vi.useFakeTimers();
+ const send = vi.fn();
+ const forwarder = createPtyCompositionForwarder(send);
+
+ forwarder.onCompositionEnd("hello");
+ vi.runAllTimers();
+ expect(send).toHaveBeenCalledExactlyOnceWith("hello");
+
+ expect(forwarder.filterTerminalData("hello")).toBe("");
+ });
+
+ it("forwards only the new suffix of a strict extension", () => {
+ vi.useFakeTimers();
+ const send = vi.fn();
+ const forwarder = createPtyCompositionForwarder(send);
+
+ forwarder.onCompositionEnd("hello");
+ vi.runAllTimers();
+
+ expect(forwarder.filterTerminalData("hello world")).toBe(" world");
+ });
+
+ it("passes unrelated onData through unchanged", () => {
+ vi.useFakeTimers();
+ const send = vi.fn();
+ const forwarder = createPtyCompositionForwarder(send);
+
+ forwarder.onCompositionEnd("hello");
+ vi.runAllTimers();
+
+ expect(forwarder.filterTerminalData("world")).toBe("world");
+ });
+
+ it("passes onData through once the echo window has passed", () => {
+ vi.useFakeTimers();
+ const send = vi.fn();
+ const forwarder = createPtyCompositionForwarder(send);
+
+ forwarder.onCompositionEnd("hello");
+ vi.runAllTimers();
+ vi.advanceTimersByTime(81);
+
+ expect(forwarder.filterTerminalData("hello")).toBe("hello");
+ });
+});
diff --git a/web/src/lib/pty-composition.ts b/web/src/lib/pty-composition.ts
index 82876c58804..ce53b7cda83 100644
--- a/web/src/lib/pty-composition.ts
+++ b/web/src/lib/pty-composition.ts
@@ -1,14 +1,40 @@
/**
* Delays an IME/dead-key commit just long enough for xterm to emit onData.
*
- * xterm is authoritative when it emits the commit. Browsers/layouts where it
+ * xterm is authoritative when it emits onData. Browsers/layouts where it
* does not emit onData still forward the compositionend text on the next turn.
+ *
+ * Mobile IME keyboards (Android/Gboard) turn composition events and onData
+ * into overlapping sources of the same user intent instead of exclusive
+ * ones: compositionend can fire twice with identical data, it can trail
+ * onData that already carried the committed text, and onData can echo a
+ * word the composition path just forwarded. The forwarder keeps a short
+ * record of both delivery channels and drops these re-sends.
*/
+
+// How long committed text stays comparable across the two input channels.
+// IME re-fires land within a few milliseconds; a human repeating the same
+// commit takes far longer, so the window will not swallow real retypes.
+const ECHO_WINDOW_MS = 80;
+
+// Cap on retained plain terminal input; only recent text is compared, and
+// this keeps a long typing burst from growing the record without bound.
+const MAX_DELIVERED_TRACK_CHARS = 256;
+
+interface DeliveredText {
+ text: string;
+ at: number;
+}
+
export function createPtyCompositionForwarder(send: (data: string) => void) {
let pending: string | null = null;
let timer: ReturnType<typeof setTimeout> | null = null;
let matchedTerminalPrefix = "";
let sawUnrelatedTerminalData = false;
+ // Text recently delivered to the PTY through this forwarder's own send.
+ let lastSent: DeliveredText | null = null;
+ // Plain text recently delivered to the PTY through xterm's onData.
+ let lastTerminalData: DeliveredText | null = null;
const clearPending = () => {
pending = null;
@@ -20,21 +46,49 @@ export function createPtyCompositionForwarder(send: (data: string) => void) {
}
};
+ const sendCommitted = (data: string) => {
+ lastSent = { text: data, at: Date.now() };
+ send(data);
+ };
+
+ const withinEchoWindow = (entry: DeliveredText | null): entry is DeliveredText =>
+ entry !== null && Date.now() - entry.at <= ECHO_WINDOW_MS;
+
return {
onCompositionEnd(data: string | null) {
if (!data) return;
+ // A compositionend trailing terminal data that already carried the
+ // commit is a late duplicate, not a new commit — re-sending it
+ // through the fallback duplicates the word in the PTY.
+ if (withinEchoWindow(lastTerminalData) && lastTerminalData.text.endsWith(data)) {
+ return;
+ }
+ // The same IME can fire compositionend twice with identical data;
+ // the first copy has already been delivered.
+ if (withinEchoWindow(lastSent) && lastSent.text === data) {
+ return;
+ }
// Preserve rapid consecutive commits instead of discarding the first.
const previous = pending;
clearPending();
- if (previous) send(previous);
+ if (previous) sendCommitted(previous);
pending = data;
timer = setTimeout(() => {
const committed = pending;
clearPending();
- if (committed) send(committed);
+ if (committed) sendCommitted(committed);
}, 16);
},
noteTerminalData(data: string) {
+ if (!data.startsWith("\x1b")) {
+ const now = Date.now();
+ const carried = withinEchoWindow(lastTerminalData) ? lastTerminalData.text : "";
+ lastTerminalData = {
+ text: (carried + data).slice(-MAX_DELIVERED_TRACK_CHARS),
+ at: now,
+ };
+ }
+
if (!pending || data.startsWith("\x1b") || sawUnrelatedTerminalData) return;
// xterm may split committed text across callbacks, but only a clean,
@@ -49,6 +103,22 @@ export function createPtyCompositionForwarder(send: (data: string) => void) {
sawUnrelatedTerminalData = true;
}
},
- dispose: clearPending,
+ // A mobile IME can re-emit just-committed composition text through
+ // xterm's onData after the composition path already forwarded it.
+ // Returns the portion still worth forwarding: "" for a full duplicate,
+ // the new suffix for a strict extension, the input unchanged otherwise.
+ filterTerminalData(data: string): string {
+ if (!withinEchoWindow(lastSent)) return data;
+ if (data === lastSent.text) return "";
+ if (data.length > lastSent.text.length && data.startsWith(lastSent.text)) {
+ return data.slice(lastSent.text.length);
+ }
+ return data;
+ },
+ dispose: () => {
+ clearPending();
+ lastSent = null;
+ lastTerminalData = null;
+ },
};
}
diff --git a/web/src/pages/ChatPage.tsx b/web/src/pages/ChatPage.tsx
index fd3d6b7ea10..aa7f4017acd 100644
--- a/web/src/pages/ChatPage.tsx
+++ b/web/src/pages/ChatPage.tsx
@@ -1553,7 +1553,12 @@ export default function ChatPage({ isActive = true }: { isActive?: boolean }) {
if (!SGR_MOUSE_RE.test(data)) {
compositionForwarder.noteTerminalData(data);
}
- forwardPtyData(data);
+ // A mobile IME can re-emit just-committed composition text through
+ // onData; only the part that is not an echo of that commit is real.
+ const unechoed = compositionForwarder.filterTerminalData(data);
+ if (unechoed) {
+ forwardPtyData(unechoed);
+ }
});
onResizeDisposable = term.onResize(({ cols, rows }) => {