Skip to content

fix(tui): request extended keys from tmux so Shift+Enter arrives - #1251

Merged
1jehuang merged 1823 commits into
1jehuang:masterfrom
rafi:fix/tmux-extended-key-negotiation
Sep 19, 2026
Merged

1jehuang merged 1823 commits into
1jehuang:masterfrom
rafi:fix/tmux-extended-key-negotiation

Conversation

@rafi

@rafi rafi commented Sep 14, 2026

Copy link
Copy Markdown
Contributor

Closes #1250

Problem

Inside tmux with the settings /terminal-setup writes, Shift+Enter submits instead of inserting a newline:

set -s extended-keys on
set -s extended-keys-format csi-u
set -as terminal-features ',xterm*:extkeys'

Only extended-keys always worked, which forces extended-key reporting on every other application in the server.

enable_keyboard_enhancement() pushed Kitty keyboard flags but never requested xterm modifyOtherKeys mode. tmux does not treat a Kitty push as a request for extended keys, so with extended-keys on it collapsed Shift+Enter to a bare CR.

Configuring the outer terminal does not help. Mapping Shift+Enter to ESC[13;2u in Kitty controls only what Kitty sends to tmux. tmux decodes keys and re-encodes them per pane, so the opt-in has to come from the application.

Change

Send ESC[>4;2m when TMUX is set, reassert it in reapply_terminal_modes_to alongside the existing Kitty set form, and reset it with ESC[>4;0m on cleanup. Behavior outside tmux is byte-for-byte unchanged.

The sequences are extracted into small writer-taking functions so the whole lifecycle is testable without a terminal.

Validation

Protocol level. An isolated tmux 3.7c server with extended-keys on and extended-keys-format csi-u, sending S-Enter to a raw-mode pane:

Application output Key received
Kitty push only (ESC[>7u) \r
ESC[>4;2m + Kitty push ESC[13;2u
ESC[>4;2m + Kitty set ESC[13;2u
After ESC[>4;0m + Kitty pop \r

End to end, running the built binary in a tmux pane with extended-keys on, typing alpha, pressing Shift+Enter, then typing beta:

  • Without the opt-in (same binary, TMUX unset): the prompt submitted and the model answered alpha.
  • With the opt-in: alpha and beta sit on two lines of one input box, nothing submitted.

Tests. Four unit tests in tui::terminal_mode_tests cover the tmux lifecycle, the unchanged non-tmux lifecycle, reapplication without a second Kitty stack push, and no keyboard sequences when enhancement is disabled. The tmux test fails without the fix.

cargo test -p jcode-tui --lib -- terminal_mode_tests terminal_setup shift_enter bare_carriage_return keyboard: 41 passed.

Not from this change: import_continue_reaches_ready_quality_first_openai_model and recent_project_review_falls_back_cleanly_when_no_repo_is_known also fail on the unmodified base commit, and several ssh_remote_* tests hang in my environment.

Notes

  • Gated on the TMUX environment variable, matching how terminal_setup::diagnose already detects tmux.
  • ESC[>4;2m is sent unconditionally under tmux rather than after a capability probe, since tmux consumes it and a terminal that does not understand it ignores it.
  • Users keep extended-keys on; always is no longer needed.
  • docs/SHIFT_ENTER.md records why terminal-side mappings alone cannot fix this.

1jehuang and others added 30 commits August 25, 2026 19:47
jcode and others added 23 commits September 12, 2026 03:48
# Conflicts:
#	crates/jcode-harness-api-server/src/translate_tests.rs
#	crates/jcode-harness-api/src/lib.rs
#	sdk/typescript/src/protocol.ts
…20260913

Welcome pull requests from all contributors
jcode only pushed Kitty keyboard flags, which tmux does not treat as a
request for extended keys. With the documented `extended-keys on`, tmux
collapsed Shift+Enter to a bare CR, so the chord submitted instead of
inserting a newline. Only `extended-keys always` worked, which forces
reporting for every other application in the server.

An outer terminal mapping that sends CSI-u to tmux does not help either:
tmux decodes keys and re-encodes them per pane, so the opt-in has to come
from the application.

Request xterm modifyOtherKeys mode 2 when TMUX is set, reassert it when
reapplying terminal modes, and reset it on cleanup. Behavior outside tmux
is unchanged.

Verified against an isolated tmux 3.7c server with `extended-keys on` and
`extended-keys-format csi-u`, sending S-Enter to a raw-mode pane:

  Kitty push only            -> \r
  modifyOtherKeys + push/set -> \e[13;2u
  after reset                -> \r

Closes #1250
@rafi

rafi commented Sep 14, 2026

Copy link
Copy Markdown
Contributor Author

Two further checks since opening:

Cleanup with the real binary. Previously only the enable path was proven end to end. Running the built binary in a tmux pane with extended-keys on, quitting it, then handing the same pane to a raw-mode probe: S-Enter delivers \r. The ESC[>4;0m reset lands, so the mode does not leak to whatever runs next in that pane.

Against a real user config rather than a minimal -f /dev/null server. Using a config that sets extended-keys on, extended-keys-format csi-u and terminal-features ,xterm*:extkeys, Shift+Enter puts alpha and beta on two lines of one input box with nothing submitted.

cargo clippy -p jcode-tui --lib reports no warnings for the changed file.

@greptile-apps

greptile-apps Bot commented Sep 14, 2026

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

Safe to merge based on the exercised terminal lifecycle behavior.

What we checked:

  • The focused terminal keyboard lifecycle test suite was executed with default features disabled, and all five tests passed with exit code 0. T-Rex
  • The test run was validated against the exact validation script and the captured output log, confirming that the tests reported five passes with exit code 0. T-Rex
  • A missing tmux runtime in the environment prevented a live tmux session, so a deterministic writer path was exercised instead. T-Rex

Summary

  • Requests tmux extended-key reporting alongside Kitty keyboard reporting so modified Enter reaches the TUI correctly. The lifecycle behavior is covered by passing byte-level tests for tmux setup, reapplication, cleanup, non-tmux operation, and disabled keyboard enhancement.
  • T-Rex validation blocked A live tmux-server session could not be exercised because tmux is not installed in this environment. The terminal-writer behavior was exercised successfully.

Reviews (1) · Last reviewed commit: "fix(tui): request extended keys from tmu..."

@rafi

rafi commented Sep 14, 2026

Copy link
Copy Markdown
Contributor Author

Re-ran the full check set against the exact pushed commit (e0092557, clean tree, byte-identical rebuild), and correcting one detail in the original description while doing so.

Correction. My first end-to-end runs classified submission by whether alpha and beta landed on the same captured line. That proxy is wrong: after a submission the transcript can still render them on separate lines, so a re-run reported the no-opt-in case as not submitting. Captured screens show what actually happens without the opt-in: sending context… and a new prompt ordinal 2…, while the opt-in case stays in the editor at 1>. Re-classifying on submission activity rather than line layout:

check expected observed
Shift+Enter with opt-in (TMUX set) newline newline
Shift+Enter without opt-in (TMUX unset) submits submits
pane after jcode exits plain \r plain \r

The conclusion is unchanged, and the underlying claim is now backed by a discriminator I checked against real screens rather than an assumed one.

Mutation check. Deleting just the two write_all opt-in/reset lines makes tmux_keyboard_lifecycle_requests_and_resets_extended_keys fail and the other four pass; restoring them turns it green. The test fails for the intended reason.

Also on this commit: terminal_mode_tests 5/5, the wider keyboard/terminal-setup selection 40/40, and clippy clean on the changed file.

@1jehuang
1jehuang merged commit 98cfc08 into 1jehuang:master Sep 19, 2026
1 check passed
@1jehuang

Copy link
Copy Markdown
Owner

Merged into master. Validated the built Jcode TUI inside an attached tmux 3.7c client PTY: physical CSI-u Shift+Enter produces a two-line composer, does not submit a message, and normal /quit resets translation back to CR. The pre-change binary reproduced the erroneous submission. Combined terminal-mode, KV-cache and Shift+Enter tests also passed.

Integrated with original PR ancestry and contributor attribution preserved, applying only the actual contribution rather than the divergent old upstream tree. Validation is local targeted testing and isolated public-interface acceptance, not a claim that hosted CI is green.

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.

Shift+Enter requires extended-keys always because Jcode does not opt in under tmux

3 participants