Skip to content

fix(client): restore host kitty keyboard state on detach - #2347

Closed
farhan-helmy wants to merge 2 commits into
herdrdev:masterfrom
farhan-helmy:fix/host-kitty-keyboard-restore
Closed

farhan-helmy wants to merge 2 commits into
herdrdev:masterfrom
farhan-helmy:fix/host-kitty-keyboard-restore

Conversation

@farhan-helmy

Copy link
Copy Markdown

Detaching from herdr in iTerm2 leaves the terminal emitting CSI-u key events as text, so the shell after herdr is unusable until reset. Two things go wrong on teardown:

The host key reporting sequence never applies in iTerm2. set_host_kitty_keyboard_report_all wrote ESC [ = {flags} u with the mode parameter omitted. Mode 1 is the protocol default, but iTerm2 matches the parameter against 1, 2 and 3 with no default arm, so the bare form is a no-op there and the host never follows the focused pane's key reporting. Now written as ESC [ = {flags} ; 1 u.

The keyboard enhancement stack is popped without a matching push. Direct attach goes through setup_terminal_with_capabilities with enable_client_protocols false and never pushes, but restore_terminal_state popped unconditionally. iTerm2 keeps an independent stack per screen buffer and falls back to a shared field when the current stack is empty, so the unbalanced pop leaves enhanced key reporting on after herdr exits. TerminalGuard now records whether setup pushed, and only pops when it did.

Verified on Linux with cargo test, cargo fmt --check and cargo clippy --all-targets. The suite is green apart from generated_workspace_ids_are_short_base32_handles, which fails identically on unmodified master on this machine.

User-facing behavior change, so it may need a public-doc note — happy to add one if you want it.

🤖 Generated with Claude Code

Write the mode parameter on the host key reporting sequence. Mode 1 is
the protocol default when omitted, but iTerm2 matches the parameter
against 1, 2, and 3 with no default arm, so the bare form is a no-op
there and the host never follows the focused pane's key reporting.

Only pop a keyboard enhancement stack entry when setup pushed one.
Direct attach goes through setup_terminal_with_capabilities with
enable_client_protocols false and never pushes, while the teardown path
popped unconditionally. iTerm2 keeps an independent stack per screen
buffer and falls back to a shared field when the current stack is empty,
so an unbalanced pop leaves enhanced key reporting enabled after herdr
exits and the shell echoes CSI-u key events as text.

refs herdrdev#2262
@coderabbitai

coderabbitai Bot commented Aug 5, 2026 •

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

Terminal setup now tracks keyboard enhancement state and restores it conditionally during normal and panic cleanup. A shared atomic claim prevents duplicate restoration. Kitty keyboard enhancement sequences now specify protocol mode 1.

Changes

Terminal keyboard enhancement

Layer / File(s) Summary
Explicit Kitty protocol mode
src/terminal_modes.rs
Kitty keyboard enhancement sequences now include explicit mode 1. The test expectation was updated.
Conditional terminal cleanup
src/client/mod.rs
TerminalGuard tracks whether enhancement flags were enabled. Normal and panic cleanup share an atomic restoration claim and pop the flags only when setup enabled them. A regression test verifies single-claim behavior.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant TerminalGuard
  participant PanicHook
  participant RestoreTerminal
  TerminalGuard->>RestoreTerminal: drop with restoration latch
  PanicHook->>RestoreTerminal: panic cleanup with restoration latch
  RestoreTerminal->>RestoreTerminal: atomically claim teardown
  RestoreTerminal->>RestoreTerminal: conditionally pop keyboard flags
Loading

Possibly related PRs

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the terminal keyboard state restoration fix when the client detaches.
Description check ✅ Passed The description directly explains the iTerm2 failure, the Kitty protocol fix, guarded restoration, and verification results.
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.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

@kangal-bot kangal-bot added the ai-review Trigger automated AI reviews for pull requests admitted by the PR gate label Aug 5, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2


ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: d44b6e22-24b1-499e-9c7e-d8604a3159ae

📥 Commits

Reviewing files that changed from the base of the PR and between 15442a2 and beadbd4.

📒 Files selected for processing (2)
  • src/client/mod.rs
  • src/terminal_modes.rs

Comment thread src/client/mod.rs Resolved
Comment thread src/client/mod.rs Resolved
The panic hook and TerminalGuard::drop both call restore_terminal_state.
Unwinding runs the hook first and then drops the guard, so teardown ran
twice and the second pop_keyboard_enhancement_flags took a stack entry
herdr never pushed, leaving the host on whatever the outer entry set.

Share one latch between the hook and the guard so the first caller wins.
The latch lives on the guard rather than in a process-wide static so a
second client session in the same process still restores its own state.

refs herdrdev#2262
@greptile-apps

greptile-apps Bot commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

The PR corrects host Kitty keyboard synchronization for iTerm2 and makes client terminal teardown idempotent while preserving push/pop pairing.

  • Emits the explicit Kitty keyboard mode parameter when updating the host’s current enhancement flags.
  • Records whether client setup pushed keyboard enhancement flags and pops only for matching setup paths.
  • Shares a one-shot restoration latch between the panic hook and TerminalGuard::Drop.

Confidence Score: 5/5

The PR appears safe to merge, with terminal restoration now correctly paired and protected against duplicate teardown.

The changed setup and teardown paths use the same capability state to pair keyboard enhancement pushes and pops, while the shared latch prevents the panic hook and guard drop from restoring the terminal twice.

Important Files Changed

Filename Overview
src/client/mod.rs Tracks keyboard-stack ownership and coordinates panic/drop teardown through a shared idempotency latch; no changed-code defect identified.
src/terminal_modes.rs Adds the explicit Kitty mode-1 parameter required for iTerm2 while retaining the intended complete flag replacement.

Sequence Diagram

sequenceDiagram
    participant Setup as Client setup
    participant Guard as TerminalGuard
    participant Host as Host terminal
    participant Panic as Panic hook
    Setup->>Host: Push keyboard enhancement flags (full client only)
    Setup->>Guard: Record push state and shared restore latch
    alt Normal detach
        Guard->>Guard: Claim restore
        Guard->>Host: Pop only if setup pushed
        Guard->>Host: Restore terminal modes
    else Panic
        Panic->>Guard: Claim shared restore
        Panic->>Host: Pop only if setup pushed
        Panic->>Host: Restore terminal modes
        Guard->>Guard: Drop observes claimed restore and skips duplicate teardown
    end
Loading

Reviews (1): Last reviewed commit: "fix(client): restore the terminal once d..." | Re-trigger Greptile

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
src/client/mod.rs (1)

2328-2336: 📐 Maintainability & Code Quality | 🔵 Trivial

Verify the repository-standard check.

The PR summary lists cargo test, cargo fmt --check, and cargo clippy --all-targets. The repository rule requires just check by default. Run just check, or document that the narrower commands are accepted and explain the known master-only failure.

As per coding guidelines, use just recipes by default and explain any bypass of a failing check.

Source: Coding guidelines


ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: f7cff0f9-12f5-465c-86e1-e955f50d9713

📥 Commits

Reviewing files that changed from the base of the PR and between beadbd4 and b93ce6e.

📒 Files selected for processing (1)
  • src/client/mod.rs

@ogulcancelik

Copy link
Copy Markdown
Collaborator

thanks for the pr. i’m closing this because it doesn’t fix the reported issue.

iterm2 3.6.11 already defaults the omitted kitty set mode to 1, so adding ;1 does not change its behavior. the actual bug is that this iterm2 version clears the keyboard reporting stack when handling set, leaving herdr with nothing to pop on detach.

the direct-attach and panic cleanup changes address separate issues and should not be bundled into this fix.

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

Labels

ai-review Trigger automated AI reviews for pull requests admitted by the PR gate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants