jcode icon indicating copy to clipboard operation
jcode copied to clipboard

macOS: Ctrl+5 prompt-rank jump is dead; a legacy Ctrl+] alias consumes it before any handler runs

Open ianalitis opened this issue 3 months ago • 2 comments

Summary

On macOS, Ctrl+5 never reaches the recency prompt-jump handler. The help overlay documents Ctrl+5..9 as "Jump by recency (5 = 5th most recent)", but rank 5 is the one entry in that range that does nothing. Ranks 6-9 work.

Environment

  • Jcode: v0.81.4 (325236119), also present on current master
  • macOS 26.x, arm64
  • Reproduced under both the local and remote key paths

Root cause

A legacy tty encodes Ctrl+] as the single byte 0x1D. Crossterm decodes 0x1C..=0x1F as Ctrl+4..Ctrl+7 (crossterm-0.29.0, src/event/sys/unix/parse.rs:110), so a real Ctrl+] arrives as KeyCode::Char('5') + CONTROL.

To recover the intended chord, ctrl_bracket_fallback_to_esc rewrites it back:

// crates/jcode-tui/src/tui/app/helpers.rs
#[cfg(target_os = "macos")]
pub(super) fn ctrl_bracket_fallback_to_esc(code: &mut KeyCode, modifiers: &mut KeyModifiers) {
    if !modifiers.contains(KeyModifiers::CONTROL) { return; }
    match code {
        KeyCode::Esc => { *code = KeyCode::Char('['); }
        KeyCode::Char('5') => {
            // Legacy tty mapping for Ctrl+]
            *code = KeyCode::Char(']');
        }
        _ => {}
    }
}

This runs at the top of handle_key_core (and the remote equivalent), before any handler sees the key. App::ctrl_prompt_rank matches Char('5')..Char('9'), so by the time it is consulted the 5 is already a ]. The rewrite is unconditional, so it also consumes the genuine Ctrl+5.

The resulting behavior is a silent no-op rather than an error: Ctrl+] is the next-prompt jump, and from the bottom of the transcript there is no next prompt, so nothing moves.

Ctrl+[ has no such conflict, because Esc is not a chord jcode needs to preserve under Ctrl.

Reproduction

  1. On macOS, open a session with several user prompts in the transcript.
  2. Stay pinned to the bottom (do not scroll).
  3. Press Ctrl+5.

Actual: nothing happens; the viewport stays at the tail. Expected: the viewport jumps to the 5th most recent user prompt, as Ctrl+6..Ctrl+9 do for their ranks.

A failing test at the same layer (this is what surfaced it):

// crates/jcode-tui/src/tui/app/tests/scroll_copy_01/part_02.rs
app.handle_key(KeyCode::Char('5'), KeyModifiers::CONTROL).unwrap();
assert!(app.scroll_offset > 0);   // fails on macOS, passes elsewhere

test_prompt_jump_ctrl_digit_is_recency_rank_in_app and test_remote_prompt_jump_ctrl_digit_is_recency_rank both fail deterministically on macOS on current master.

Suggested fix

Drop the Char('5') arm and keep the Esc one. Ctrl+] still reaches the next-prompt jump through its documented sibling Ctrl+J, and a terminal with keyboard enhancement (kitty protocol, which jcode already enables) reports a real Ctrl+] distinctly and never takes this path.

The tradeoff is explicit: a documented, unambiguous binding beats a legacy alias for a chord that has another working route.

Notes

I have a patch and a regression test (test_ctrl_5_is_a_prompt_rank_jump_on_macos) on my fork if a reference is useful. Happy to open a PR, or leave this as a report if you would rather write it yourself.

ianalitis avatar Sep 01 '26 18:09 ianalitis

A patch is ready and verified on a fork branch, based directly on master (325236119), one commit, no unrelated changes:

https://github.com/ianalitis/jcode/tree/fix/macos-ctrl5-prompt-rank

Diff against master: https://github.com/1jehuang/jcode/compare/master...ianalitis:jcode:fix/macos-ctrl5-prompt-rank

The change drops the Char('5') arm from ctrl_bracket_fallback_to_esc and keeps the Esc one, plus a regression test.

cargo test -p jcode-tui --lib -- --test-threads=1 prompt_jump ctrl_5
9 passed; 0 failed

That includes the two tests that fail on macOS on current master (test_prompt_jump_ctrl_digit_is_recency_rank_in_app, test_remote_prompt_jump_ctrl_digit_is_recency_rank), both ctrl_brackets tests, and test_prompt_jump_ctrl_esc_fallback_on_macos, so the Ctrl+[ path is unchanged.

I could not open a PR: this repository is set to pull_request_creation_policy: collaborators_only, so CreatePullRequest is rejected for non-collaborators. Entirely your call whether to change that. Per CONTRIBUTING I am happy for the branch to serve purely as a reference, and the regression test may be the more useful half either way.

ianalitis avatar Sep 01 '26 18:09 ianalitis

Thanks for the precise root-cause analysis. I removed the ambiguous Char('5') rewrite in #1137 while retaining the Esc fallback for Ctrl+[. Both local and remote Ctrl-digit prompt-jump tests pass, and the TUI crate checks successfully. The platform-specific behavior is called out for macOS review validation.

--- — Jcode agent (automated triage), on behalf of @1jehuang

1jehuang avatar Sep 01 '26 19:09 1jehuang