Repository navigation
fix(client): restore host kitty keyboard state on detach - #2347
farhan-helmy wants to merge 2 commits into
Conversation
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
📝 WalkthroughWalkthroughTerminal 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. ChangesTerminal keyboard enhancement
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
Possibly related PRs
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
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
📒 Files selected for processing (2)
src/client/mod.rssrc/terminal_modes.rs
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 SummaryThe PR corrects host Kitty keyboard synchronization for iTerm2 and makes client terminal teardown idempotent while preserving push/pop pairing.
Confidence Score: 5/5The 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.
|
| 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
Reviews (1): Last reviewed commit: "fix(client): restore the terminal once d..." | Re-trigger Greptile
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/client/mod.rs (1)
2328-2336: 📐 Maintainability & Code Quality | 🔵 TrivialVerify the repository-standard check.
The PR summary lists
cargo test,cargo fmt --check, andcargo clippy --all-targets. The repository rule requiresjust checkby default. Runjust check, or document that the narrower commands are accepted and explain the known master-only failure.As per coding guidelines, use
justrecipes 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
📒 Files selected for processing (1)
src/client/mod.rs
|
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. |
Detaching from herdr in iTerm2 leaves the terminal emitting CSI-u key events as text, so the shell after
herdris unusable untilreset. Two things go wrong on teardown:The host key reporting sequence never applies in iTerm2.
set_host_kitty_keyboard_report_allwroteESC [ = {flags} uwith 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 asESC [ = {flags} ; 1 u.The keyboard enhancement stack is popped without a matching push. Direct attach goes through
setup_terminal_with_capabilitieswithenable_client_protocolsfalse and never pushes, butrestore_terminal_statepopped 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.TerminalGuardnow records whether setup pushed, and only pops when it did.Verified on Linux with
cargo test,cargo fmt --checkandcargo clippy --all-targets. The suite is green apart fromgenerated_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