Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

simplify: version-stamp the mirrored xterm guard and name its deliberate omissions

[._.]/ Adam Eivy6533c944bb3c
diff --git a/client/src/lib/terminalDictation.js b/client/src/lib/terminalDictation.js
index dbf9fbca8a..c986d95696 100644
--- a/client/src/lib/terminalDictation.js
+++ b/client/src/lib/terminalDictation.js
@@ -25,6 +25,9 @@
// through xterm and may clear the textarea out from under us.
//
// ── The keypress seam ──────────────────────────────────────────────────────────
+// Mirrored from @xterm/xterm 6.0.0 — `CoreBrowserTerminal#_keyPress` and
+// `#_inputEvent`. Re-read those two before trusting this section against a newer
+// xterm; the real-Terminal tests fail loudly if either mechanism moves.
// Not every physical keystroke is settled on `keydown`. xterm cancels that event
// for most printable keys (so the character never reaches the textarea and no
// input event fires), but it deliberately defers A-Z and keys its keyboard map
@@ -38,6 +41,11 @@
// xterm's: it counts as a resync point, not ours to forward, and the event goes
// through untouched.
//
+// We mirror `_keyPressHandled` and only that flag. xterm's `_inputEvent` also gates
+// on `_keyDownSeen` and clears an `_unprocessedDeadKey`; the omissions are
+// deliberate, because our own resync points already cover both — the keydown resync
+// for the first, the `compositionend` resync for the second.
+//
// ── Boundaries ─────────────────────────────────────────────────────────────────
// Corrections are streamed live, which assumes the far end treats 0x7f as "erase
// the previous character" — true at a shell prompt and in the line editors of the
@@ -176,12 +184,12 @@ export const attachDictationBridge = (terminal, sendData) => {
const handleInput = (ev) => {
if (ev.target !== textarea) return;
- // The character reached the PTY through xterm's keypress path already. Take the
- // insertion into the floor so a later correction can't rewind through it, and
- // leave the event alone — xterm's `_keyPressHandled` guard no-ops it there.
- // Consumed ahead of every other branch so the latch is strictly one-shot.
+ // The character reached the PTY through xterm's keypress path already (see "The
+ // keypress seam"). Take the insertion into the floor so a later correction can't
+ // rewind through it, and leave the event alone. Consumed ahead of the guards
+ // below so the latch is strictly one-shot.
if (keyPressSeen) {
- keyPressSeen = false;
+ disarmKeyPress();
resyncNow();
return;
}
diff --git a/client/src/lib/terminalDictation.test.js b/client/src/lib/terminalDictation.test.js
index 5ff63c992d..25d7383211 100644
--- a/client/src/lib/terminalDictation.test.js
+++ b/client/src/lib/terminalDictation.test.js
@@ -78,6 +78,12 @@ describe('attachDictationBridge', () => {
textarea.dispatchEvent(new InputEvent('input', { inputType, data, bubbles: true }));
};
+ // Key events the latch reads. 84 is 'T' — a capital, the case this bridge exists
+ // for, so the tests that don't care about the specific key take it by default.
+ const fireKey = (type, keyCode = 84) => {
+ textarea.dispatchEvent(new KeyboardEvent(type, { keyCode, bubbles: true }));
+ };
+
// Stands in for xterm's own textarea listener, which appends the raw insertion.
const attachXtermStub = () => {
const spy = vi.fn();
@@ -193,7 +199,7 @@ describe('attachDictationBridge', () => {
textarea.value = 'abc';
fireInput('insertText');
// Enter: xterm sends CR and clears the textarea.
- textarea.dispatchEvent(new KeyboardEvent('keydown', { keyCode: 13, bubbles: true }));
+ fireKey('keydown', 13);
textarea.value = '';
vi.runAllTimers();
// Next dictation starts from a clean mirror — no phantom DELs for 'abc'.
@@ -206,8 +212,8 @@ describe('attachDictationBridge', () => {
// Capitals and space are the keys xterm forwards from `keypress` without
// cancelling it, so the character lands in the textarea and fires `input`
// AFTER the PTY already has it. Diffing that insertion doubled every one.
- textarea.dispatchEvent(new KeyboardEvent('keydown', { keyCode: 84, bubbles: true }));
- textarea.dispatchEvent(new KeyboardEvent('keypress', { keyCode: 84, bubbles: true }));
+ fireKey('keydown');
+ fireKey('keypress');
textarea.value = 'T';
fireInput('insertText');
expect(sent).toEqual([]);
@@ -222,8 +228,8 @@ describe('attachDictationBridge', () => {
// The insertion a keypress produces can fail to arrive. Dictation then follows
// with no key events at all, so only keyup can clear the latch in time —
// waiting for the next keydown would swallow the phrase into the floor.
- textarea.dispatchEvent(new KeyboardEvent('keypress', { keyCode: 84, bubbles: true }));
- textarea.dispatchEvent(new KeyboardEvent('keyup', { keyCode: 84, bubbles: true }));
+ fireKey('keypress');
+ fireKey('keyup');
textarea.value = 'ab';
fireInput('insertText');
expect(sent).toEqual(['ab']);
@@ -231,7 +237,7 @@ describe('attachDictationBridge', () => {
it('disarms the latch when focus leaves mid-keystroke', () => {
// Blur can land between the keypress and the keyup that would have cleared it.
- textarea.dispatchEvent(new KeyboardEvent('keypress', { keyCode: 84, bubbles: true }));
+ fireKey('keypress');
textarea.dispatchEvent(new FocusEvent('blur'));
vi.runAllTimers();
textarea.value = 'ab';
@@ -240,7 +246,7 @@ describe('attachDictationBridge', () => {
});
it('does not resync on the composition keycode soft keyboards report', () => {
- textarea.dispatchEvent(new KeyboardEvent('keydown', { keyCode: 229, bubbles: true }));
+ fireKey('keydown', 229);
textarea.value = 'ab';
fireInput('insertText');
vi.runAllTimers();
@@ -267,7 +273,7 @@ describe('attachDictationBridge', () => {
textarea.value = 'their';
fireInput('insertText');
// Field is non-empty now, so this keystroke really does arm a resync.
- textarea.dispatchEvent(new KeyboardEvent('keydown', { keyCode: 32, bubbles: true }));
+ fireKey('keydown', 32);
textarea.value = 'there';
fireInput('insertReplacementText');
vi.runAllTimers();
@@ -280,7 +286,7 @@ describe('attachDictationBridge', () => {
it('arms no timer while typing leaves nothing to reconcile', () => {
const timer = vi.spyOn(globalThis, 'setTimeout');
- textarea.dispatchEvent(new KeyboardEvent('keydown', { keyCode: 65, bubbles: true }));
+ fireKey('keydown', 65);
expect(timer).not.toHaveBeenCalled();
timer.mockRestore();
});