Skip to content

fix(tui): restore kitty keyboard protocol and TUI modes on signal exit (closes #1635) - #1707

Closed
alecuba16 wants to merge 1 commit into
1jehuang:masterfrom
alecuba16:fix/terminal-restore-sigint-kitty-pop
Closed

alecuba16 wants to merge 1 commit into
1jehuang:masterfrom
alecuba16:fix/terminal-restore-sigint-kitty-pop

Conversation

@alecuba16

Copy link
Copy Markdown
Contributor

Closes #1635. Related #898.

The signal handler (SIGINT/SIGTERM/SIGHUP/SIGQUIT) restored raw mode, the primary screen and the cursor but never popped the kitty keyboard stack nor disabled bracketed paste, focus reporting or mouse capture. A signal exit could therefore leave the terminal in CSI u mode and the shell echoed raw sequences like e1;1:3u.

The handler now disables every TUI mode and calls disable_keyboard_enhancement() before exiting. The pop is a no-op on terminals with no kitty stack, so it is safe unconditionally. This is the signal-exit path; #898 covers the same leak on the normal-exit path inside tmux.

Also one-line ..Default::default() fix in provider_setup to keep a struct literal compiling after new fields.

11 insertions, no behavior change on the normal-exit path. Single commit rebased onto current master.

@greptile-apps

greptile-apps Bot commented Oct 4, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

[High impact] No identified blocking issues remain in this review; the PR is safe to merge based on the available evidence.

Summary

The PR restores terminal modes on signal exit and tracks who owns the terminal during external-editor use.

  • Signal exits restore the TUI modes before returning control to the shell.
  • Signals leave the terminal alone while an editor is using it.
  • Restarted jcode adopts its inherited Kitty keyboard entry without pushing it again.
  • The code-size budget now matches the expanded TUI files.

No new blocking issues were identified.

Reviews (14) · Last reviewed commit: "fix(tui): restore kitty keyboard protoco..." · Reviewed by Greptile

Comment thread src/cli/terminal.rs Resolved
Comment thread src/cli/commands/provider_setup.rs Resolved
@alecuba16
alecuba16 force-pushed the fix/terminal-restore-sigint-kitty-pop branch from 24f25e3 to af8e737 Compare October 9, 2026 08:12
Comment thread src/cli/terminal.rs Resolved
Comment thread crates/jcode-tui/src/tui/mod.rs Resolved
Comment thread crates/jcode-tui/src/tui/mod.rs Resolved
Comment thread crates/jcode-tui/src/tui/mod.rs Resolved
@alecuba16
alecuba16 force-pushed the fix/terminal-restore-sigint-kitty-pop branch from bce08bd to 624defc Compare October 9, 2026 12:37
Comment thread src/cli/terminal.rs Resolved
@alecuba16
alecuba16 force-pushed the fix/terminal-restore-sigint-kitty-pop branch from cf361f5 to 67f65d2 Compare October 9, 2026 17:14
@alecuba16
alecuba16 force-pushed the fix/terminal-restore-sigint-kitty-pop branch from 67f65d2 to 05d8200 Compare October 9, 2026 18:18
@alecuba16
alecuba16 force-pushed the fix/terminal-restore-sigint-kitty-pop branch 3 times, most recently from 664eef2 to 36f3d12 Compare October 10, 2026 17:26
The SIGINT/SIGTERM/SIGHUP/SIGQUIT handler restored raw mode, the primary screen and the cursor but never popped the kitty keyboard stack nor disabled bracketed paste, focus reporting or mouse capture, so a signal exit left the shell echoing raw sequences like e1;1:3u. The cleanup now disables every TUI mode and pops the kitty stack, writing the disable sequences to stdout where setup_terminal armed them, and skips the pop when this process owns no stack entry. Ownership is tracked process-wide, set on enable, adopted when an exec handoff inherits the previous process's entry, and the push, ownership flip and signal cleanup serialize under one mutex so termination waits out an in-flight push/pop instead of exiting with the entry active.

While an editor owns the tty the cleanup leaves terminal modes alone, and the editor handoff publishes only after suspend finished and reclaims before resume re-arms, both under the kitty state lock, so the cleanup never sees a half-applied handoff. Terminal-mode tests pin the skip, single-pop and no-test-bytes-on-stdout semantics, and the editor test child waits on a sentinel the sampler writes once it observes ownership instead of a fixed sleep.

Closes 1jehuang#1635. Related 1jehuang#898.
@alecuba16
alecuba16 force-pushed the fix/terminal-restore-sigint-kitty-pop branch from 36f3d12 to cc05b1f Compare October 10, 2026 17:57
@1jehuang

Copy link
Copy Markdown
Owner

Thanks @alecuba16! Landed on master with your authorship (only the budget JSON conflicted, so I regenerated it). clippy -D warnings, 28 TUI terminal/editor tests and 3 signal-cleanup tests pass. End-to-end check: started the real TUI in a pty with TERM=xterm-kitty, sent SIGTERM, and confirmed the kitty pop (ESC[<1u) and alt-screen exit are emitted. --- Jcode agent, on behalf of @1jehuang

@1jehuang 1jehuang closed this Oct 11, 2026
@alecuba16
alecuba16 deleted the fix/terminal-restore-sigint-kitty-pop branch October 11, 2026 18:37
KooshaPari added a commit to KooshaPari/KCode that referenced this pull request Nov 21, 2026
* fix(tui): restore every terminal mode on signal and panic

The SIGHUP/SIGTERM cleanup emitted only `?1049l ?25h`, leaving kitty CSI-u (`ESC[>1u`), mouse capture, bracketed paste and focus reporting armed, so the next shell received CSI-u keys and typing looked dead. The panic hook had a third, narrower variant that also missed mouse capture and wrote to stderr.

One shared `write_terminal_disable_sequence` now backs both the signal cleanup and the panic hook: stdout (the stream the TUI armed modes on), inside `with_kitty_state_lock`, guarded by `editor_owns_terminal()`, with the kitty pop after the lock is released. The exec-handoff resume path adopts the inherited kitty entry (`adopt_keyboard_enhancement_ownership`) so a later exit pops it instead of leaking it.

Also fixes the resume hint: `crash_resume_hint_lines` and the `write_session_resume_hint` fallback named `jcode --resume`, which sends the operator to upstream jcode reading `~/.jcode` instead of `~/.kcode`; both now use `resume_binary_stem()` = "kcode". The picker line literal is unchanged.

Ports upstream 3222d46 (PR 1jehuang#1707, issue 1jehuang#1635), which this fork predates. `kcode server reload` was measured harmless (627 B repaint, same as control), so the defect is the client termination/exec path, not reload.

tx-agent: jcode
tx-validated: test
tx-lane: jcode:session_unicorn_1795249137497_783907915dc7755e

* refactor(tui): move terminal-mode ownership into tui/terminal_modes.rs

The A25 terminal-restore fix grew tui/mod.rs (2122 -> 2423) and
tui/app/commands.rs (3771 -> 3955), both already over the oversized-file
ratchet, so scripts/check_code_size_budget.py failed on this branch.

Pure relocation, no behavior change: the kitty push/pop statics, the
ownership and termination gates, the editor handoff, and their tests move
into a new module as-is. Only mechanical rewrites: `pub(crate)` on the
items other modules call, an explicit `use crate::tui::keyboard_protocol;`
for the bare `keyboard_protocol::` paths, and `pub use terminal_modes::*`
so every existing `crate::tui::*` / `jcode_tui::tui::*` path still resolves.
Disable-byte order, lock discipline, and item names are unchanged.

tui/mod.rs 2122 -> 1879, tui/app/commands.rs 3771 -> 3671,
terminal_modes.rs 841 (< 1200 threshold).

tx-agent: jcode
tx-validated: build,test
tx-lane: jcode:session_zebra_1795251286001_f002d14678692202

* style(tui): drop the extra blank line rustfmt flagged after the relocation

tx-agent: jcode
tx-validated: lint
tx-lane: jcode:session_zebra_1795251286001_f002d14678692202

* chore(guardrails): record the terminal-mode relocation in the swallowed-error baseline

Six `let _ =` sites moved from tui/mod.rs and tui/app/commands.rs into
tui/terminal_modes.rs, so the per-file baseline needs the new path.
Counts are moved, not added: mod.rs 1 -> 0, commands.rs 14 -> 9, new file 6.
The recorded total is left at 3846, which is >= the measured 3837, so no
budget is loosened.

tx-agent: jcode
tx-validated: lint
tx-lane: jcode:session_zebra_1795251286001_f002d14678692202
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.

fix(tui): SIGINT/SIGTERM exit leaves kitty keyboard protocol and TUI modes enabled

2 participants