Skip to content

fix(terminal): require press-time evidence for the Windows IME Enter-keyup newline - #11937

Closed
dogfooter8750 wants to merge 2 commits into
stablyai:mainfrom
dogfooter8750:fix/ime-enter-keyup-rollover-newline
Closed

dogfooter8750 wants to merge 2 commits into
stablyai:mainfrom
dogfooter8750:fix/ime-enter-keyup-rollover-newline

Conversation

@dogfooter8750

Copy link
Copy Markdown
Contributor

Summary

Fixes a residual "spontaneous newline" path of #11878 that survives #11616: on Windows, the IME Enter-keyup synthesis branch (keyboard-handlers.ts onKeyUp) infers the Shift/Ctrl+Enter chord from release-time modifier state. Two user-visible consequences with a Korean IME:

  • A plain committing Enter (delivered as Process/229 with no modifiers) followed by a rolled-over Shift for the next doubled consonant (fast typing of 했다 / 아빠 / 얘기 patterns) makes the Enter keyup report shiftKey=true, and the branch synthesizes a Shift+Enter newline (\x1b\r) the user never chorded. Because the deferred sender fires it on session end or its 200 ms fallback, the newline appears decoupled from any keystroke — exactly the "random newline during lag" reports on [Bug]: Korean double consonants (쌍자음) trigger automatic line breaks in the Windows integrated terminal #11878.
  • A directly-sent Shift+Enter (pressed with no composition active) can send a second newline from its keyup once the next Hangul composition has started, since the direct-send path never claims the modified-enter chord.

The fix records observed Enter keydowns (code → timeStamp) and lets the keyup synthesis run only for presses whose keydown the IME swallowed entirely — the case that branch exists for. A balancing keyup that copies the keydown's native timeStamp keeps the evidence armed for the later physical release (mirroring releaseRedispatchedEnter's same-timeStamp semantics). Blank-code Process/229 keydowns are deliberately not recorded, so #11616's "blank code falls back to the Enter keyup path" design is preserved.

Interplay with #11616 (verified locally): #11616 applies cleanly on top of this branch and the combined tree passes all four IME suites, 66/66.

Screenshots

No visual change.

Testing

  • Added or updated high-quality tests that would catch regressions: 4 new tests in keyboard-handlers-ime-enter-keyup.test.tsx — rollover suppression, balancing-keyup retention, keyup-only synthesis preserved (positive control), and no double-send after a direct send. The two suppression tests fail against the pre-fix handler (sendInput receives "�\r") and pass with it.
  • vitest on the affected IME suites: 54/54 on this branch; 66/66 with [P1] fix(terminal): require a physical Enter for Windows IME process-enter #11616 applied on top (keyboard-handlers-ime-enter-keyup, keyboard-handlers-ime, terminal-ime-deferred-newline, terminal-ime-composition-route)
  • oxlint (default and config/oxlint-react-doctor.json) and oxfmt --check on the changed files
  • tsc --noEmit -p config/tsconfig.tc.web.json (the change is renderer-only)
  • Full pnpm test / pnpm build were not run in this environment (native runtime build unavailable); the change surface is two renderer TS files

AI Review Report

Three independent review passes (adversarial correctness, cross-platform, security/perf) all approved; two of their findings were folded back into the change:

  • Correctness: verified the swallowed-keydown keyup path is preserved (pinned by the positive-control test), chord-absorb/releaseRedispatchedEnter housekeeping still runs on the suppressed path, and per-code keying keeps Enter and NumpadEnter evidence independent. Review-driven changes: (1) evidence is recorded ahead of every onKeyDown early return, so keydowns on editable/out-of-scope targets also disqualify their keyup; (2) the original Set was replaced with a code → timeStamp map so a balancing keyup (timeStamp copied from the keydown) does not consume the evidence before the physical release.
  • Cross-platform (macOS/Linux/SSH): recording and the only consumer both sit behind isWindows; macOS/Linux behavior is byte-identical. Remote/SSH panes are unaffected — the change only suppresses sends ahead of the unchanged transport abstraction.
  • [P1] fix(terminal): require a physical Enter for Windows IME process-enter #11616 coexistence: the recording predicate excludes blank-code 229 keydowns by construction, so [P1] fix(terminal): require a physical Enter for Windows IME process-enter #11616's blank-code keyup fallback still synthesizes. Verified empirically (66/66 with [P1] fix(terminal): require a physical Enter for Windows IME process-enter #11616 overlaid).
  • Accepted residual (documented): if an observed keydown's keyup never arrives as key === 'Enter', a stale entry can suppress at most one future keyup-only synthesis for that code; it self-heals on the next keyup and is cleared on window blur and effect cleanup.

Security Audit

  • Input handling: the change strictly narrows the conditions under which a synthesized \x1b\r reaches the PTY; no new byte-injection path is added.
  • Hot path: O(1) map operations with no per-event allocation; non-Enter keys keep their existing early returns on both keydown and keyup.
  • Memory: the map is bounded (realistically Enter/NumpadEnter only) and cleared on keyup consumption, window blur, and effect cleanup.
  • No new data crosses IPC or remote boundaries; nothing is logged.

Notes

🤖 Generated with Claude Code

… Enter-keyup newline

On Windows, the Enter-keyup synthesis path inferred the modified-Enter
chord from release-time modifier state. A plain committing Enter
(Process/229, no modifiers) followed by a rolled-over Shift for the next
doubled consonant made the keyup report shiftKey=true and synthesized a
Shift+Enter the user never chorded; a directly-sent Shift+Enter could
likewise send a second newline from its keyup once the next composition
started. Record observed Enter keydowns (code -> timeStamp) and let the
keyup synthesis run only for presses whose keydown the IME swallowed
entirely; a balancing keyup that copies the keydown timeStamp keeps the
evidence for the later physical release.

Refs stablyai#11878

Co-Authored-By: Claude Fable 5 <[email protected]>
@greptile-apps

greptile-apps Bot commented Aug 1, 2026 •

Copy link
Copy Markdown

Greptile Summary

This PR narrows the Windows IME Enter-keyup synthesis path in keyboard-handlers.ts so it fires only when no matching keydown was observed — the case the branch exists for. A Map<string, number[]> tracks IME Enter keydown timestamps per code; each physical release drains one entry, balancing keyups (same timestamp) don't consume evidence, auto-repeat is handled via the !e.repeat guard, and the map is cleared on blur and unmount.

  • keyboard-handlers.ts: Records IME Enter keydowns ahead of every early return; gates the keyup synthesis behind !enterKeydownWasObserved; cleans up the evidence map on blur and cleanup.
  • keyboard-handlers-ime-enter-keyup.test.tsx (new): Six tests covering rollover suppression, balancing-keyup retention, rapid double-press guarding, auto-repeat drain, positive-control synthesis, and no-double-send after a direct send.

Confidence Score: 5/5

The change strictly narrows an existing synthesis path with no new byte-injection surface, guarded behind the existing isWindows check.

The evidence map logic is correct across all edge cases: recording, draining, balancing-keyup non-drain, auto-repeat, and cleanup are all properly placed and verified by the new test suite.

Files Needing Attention: No files require special attention.

Important Files Changed

Filename Overview
src/renderer/src/components/terminal-pane/keyboard-handlers.ts Adds an evidence map that gates the IME Enter-keyup synthesis branch; recording, draining, and cleanup are all correctly placed and the IME-swallowed-keydown path (no observed keydown) is preserved by construction.
src/renderer/src/components/terminal-pane/keyboard-handlers-ime-enter-keyup.test.tsx New test suite with six cases covering the critical paths: rollover suppression, balancing-keyup non-drain, rapid double-press, auto-repeat, positive-control synthesis, and direct-send non-duplication.

Sequence Diagram

sequenceDiagram
    participant KB as KeyboardEvent
    participant OKD as observedEnterKeydownTimeStamps
    participant KD as onKeyDown
    participant KU as onKeyUp
    participant SYN as IME Synthesis

    Note over KB,SYN: Normal IME commit (keydown swallowed entirely)
    KB->>KD: "keydown key=Process/229, code=Enter, ts=10"
    Note over KD: blank-code 229 not recorded
    KB->>KU: "keyup key=Enter, code=Enter, ts=30, shiftKey=true"
    KU->>OKD: get('Enter') undefined
    Note over KU: enterKeydownWasObserved = false
    KU->>SYN: synthesize Shift+Enter

    Note over KB,SYN: Rollover scenario
    KB->>KD: "keydown key=Process/229, code=Enter, ts=10, isComposing"
    KD->>OKD: set('Enter', [10])
    KB->>KU: "keyup key=Enter, ts=30, shiftKey=true"
    KU->>OKD: get('Enter') [10], shift() delete
    Note over KU: enterKeydownWasObserved = true BLOCKED

    Note over KB,SYN: Balancing keyup (same timeStamp)
    KB->>KD: "keydown key=Process/229, code=Enter, ts=10"
    KD->>OKD: set('Enter', [10])
    KB->>KU: "keyup key=Enter, ts=10 balancing"
    Note over KU: includes(10) true do NOT drain, BLOCKED
    KB->>KU: "keyup key=Enter, ts=30 physical release"
    KU->>OKD: shift() delete
    Note over KU: enterKeydownWasObserved = true BLOCKED
Loading

Reviews (2): Last reviewed commit: "fix(terminal): keep press evidence per E..." | Re-trigger Greptile

@coderabbitai

coderabbitai Bot commented Aug 1, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: a1ce4520-2114-4a29-92b3-f721e9e4b5bd

📥 Commits

Reviewing files that changed from the base of the PR and between c865c0b and e17d22c.

📒 Files selected for processing (2)
  • src/renderer/src/components/terminal-pane/keyboard-handlers-ime-enter-keyup.test.tsx
  • src/renderer/src/components/terminal-pane/keyboard-handlers.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/renderer/src/components/terminal-pane/keyboard-handlers.ts

📝 Walkthrough

Walkthrough

The keyboard handler records qualifying Windows and IME Enter keydowns before early returns. It uses bounded timestamped evidence to distinguish balancing keyups from physical releases and prevents incorrect newline synthesis. The state clears on window blur and effect cleanup. New tests cover modifier rollover, balancing keyups, swallowed Enter keydowns, auto-repeat, rapid presses, and duplicate prevention for directly handled Shift+Enter.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely identifies the Windows IME Enter-keyup newline fix.
Description check ✅ Passed The description covers all required sections, testing results, limitations, review findings, security risks, and platform-specific notes.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

A single slot per code let a rapid second Enter press go unguarded: the
first release found a mismatched timeStamp, dropped the only entry, and
the second release then synthesized the Shift+Enter this guard exists to
prevent. Track one entry per press and drain exactly one per physical
release, so every press stays guarded until its own release. Auto-repeat
keydowns do not stack an entry, since the whole run ends in one release,
and the list is bounded so a press whose release never arrives cannot
grow it without end.

Refs stablyai#11878

Co-Authored-By: Claude Fable 5 <[email protected]>
@dogfooter8750

Copy link
Copy Markdown
Contributor Author

Good catch on the map-overwrite edge case — it was real, and I reproduced it before fixing it. Driving the harness with two Enter keydowns (ts 10, 20) before either release, then two keyups (ts 30, 40) with a rolled-over Shift, the second keyup synthesized \x1b\r: the first release saw a mismatched timeStamp, dropped the only entry, and left the second press unguarded.

Fixed in e17d22c by tracking one entry per press instead of one slot per code (Map<string, number[]>), draining exactly one entry per physical release. Balancing-keyup semantics are unchanged — a keyup whose timeStamp matches any recorded press is still non-consuming.

Two details worth flagging since they are new surface:

  • Auto-repeat keydowns deliberately do not stack an entry. A repeat run re-fires keydown with no intervening keyup and ends in a single release, so stacking would leave residue that release cannot drain and would suppress a later legitimate keyup-only synthesis.
  • The list is bounded (MAX_OBSERVED_ENTER_KEYDOWNS_PER_CODE = 8) so a press whose release never arrives cannot grow it without end.

Tests: added guards every keyup of a rapid double Enter press (fails on the previous commit, passes now) and drains one press worth of evidence per release, so a later swallowed keydown still synthesizes, which pins both the repeat handling and the fact that the keyup-only path stays reachable. Locally on Windows: 56/56 across the four IME suites, and 68/68 with #11616 applied on top. oxlint (default + react-doctor), oxfmt --check, and tsc -p config/tsconfig.tc.web.json are clean.

@holdn2

holdn2 commented Aug 2, 2026

Copy link
Copy Markdown

Heads up — I've opened #12120 as a draft that carries your commit c865c0b
(press-time evidence for the Enter keyup) with --author preserved, alongside the
minimum hunks from #11616, #11896 and #11870.

One change I should flag: rather than adding keyboard-handlers-ime-enter-keyup.test.tsx
as a new file, I folded your four tests into the existing colocated
keyboard-handlers-ime.test.tsx suite, to keep the combined branch to one handler test
file. The test bodies and intent are unchanged.

Your rollover case turned out to be load-bearing for the combined fix: the regression test
I added for a Shift-jamo followed by a committing Enter only passes with your keyup gate
in place.

This is not meant to supersede your PR — maintainers may well prefer landing it directly,
which is fine by me. Happy to close mine on request, and to correct anything I've
misrepresented.

@dogfooter8750

Copy link
Copy Markdown
Contributor Author

Nothing misrepresented — the attribution is more careful than it needed to be, and folding the tests into keyboard-handlers-ime.test.tsx is the right call for a combined branch.

On your offer to close: please don't. A single combined fix is what actually helps the people on #11878, and one branch carrying the keydown predicate, the keyup gate, and the deferred-commit work is a much better thing for a maintainer to review than four PRs landing in some arbitrary order.

I'll leave this PR open, but explicitly as a fallback rather than as competition. #12120 is a draft carrying work from four authors, so if it needs time in review, this one is two files with green checks and is cheap to take on its own for the keyup half. Whichever lands, I'm happy — and if a maintainer signals they want #12120, I'll close this myself so it isn't in the way.

One carry-over when you next touch the branch, detail and measurements in my comment there: e17d22cf closes a rapid-double-Enter hole that c865c0b left open — the single slot per code meant the first release consumed the only entry and left the second press unguarded. It applies cleanly on top of your branch and takes that case from one spurious \x1b\r to zero.

holdn2 pushed a commit to holdn2/orca that referenced this pull request Aug 3, 2026
A single slot per code let a rapid second Enter press go unguarded: the first release found a mismatched timeStamp, dropped the only entry, and the second release then synthesized the Shift+Enter this guard exists to prevent. Track one entry per press and drain exactly one per physical release, so every press stays guarded until its own release. Auto-repeat keydowns do not stack an entry, since the whole run ends in one release, and the list is bounded so a press whose release never arrives cannot grow it without end.

Adapted from stablyai#11937 commit e17d22c; tests were folded into the existing colocated suite.

Refs stablyai#11878
holdn2 pushed a commit to holdn2/orca that referenced this pull request Aug 3, 2026
Adapted from stablyai#11937 commit c865c0b; tests were folded into the existing colocated suite.
holdn2 pushed a commit to holdn2/orca that referenced this pull request Aug 3, 2026
A Shift-jamo consumed as Process/229, the plain committing Enter that follows, and the rollover Shift for the next doubled consonant exercise the stablyai#11616 and stablyai#11937 fixes in one session. Each half was covered separately; this fails on main and passes here.
holdn2 pushed a commit to holdn2/orca that referenced this pull request Aug 3, 2026
A single slot per code let a rapid second Enter press go unguarded: the first release found a mismatched timeStamp, dropped the only entry, and the second release then synthesized the Shift+Enter this guard exists to prevent. Track one entry per press and drain exactly one per physical release, so every press stays guarded until its own release. Auto-repeat keydowns do not stack an entry, since the whole run ends in one release, and the list is bounded so a press whose release never arrives cannot grow it without end.

Adapted from stablyai#11937 commit e17d22c; tests were folded into the existing colocated suite.

Refs stablyai#11878
@dogfooter8750

Copy link
Copy Markdown
Contributor Author

Closing — this landed on main via #12265, which carries both commits from this branch with authorship intact:

  • ad0a70aa14 require press-time evidence before synthesizing an IME Enter-keyup newline
  • bacdf3a31d keep press evidence per Enter press, not per code

Thanks @holdn2 for carrying it into the combined branch, and in particular for reproducing each case against the handler before taking it rather than trusting my description — both the rapid-double-press hole and the corrected re-render mechanism surfaced because you did that. And thanks to the maintainers for the wider fix; the keyup half was only one of several paths that needed closing.

I have since root-caused the re-render amplifier I flagged, and opened it separately rather than leave it as an unexplained aside. Summary is in my comment on #12120.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants