Skip to content

fix(terminal): reset modifyOtherKeys and focus after each session - #718

Merged
blooop merged 4 commits into
mainfrom
fix/reset-keyboard-modes
Oct 31, 2026
Merged

blooop merged 4 commits into
mainfrom
fix/reset-keyboard-modes

Conversation

@blooop

@blooop blooop commented Oct 31, 2026 •

Copy link
Copy Markdown
Owner

After a session that ends without its own cleanup, Ctrl-C at your shell cancels the line again. Before this change, Ctrl-C typed ;5;99~ into the line, and the next Enter ran that line. This PR also releases 0.64.2.

In short

Claude Code switches on two terminal modes in the container:

  1. xterm modifyOtherKeys. While it is on, Ctrl-C sends the text ESC[27;5;99~, not the byte that sends SIGINT.
  2. Focus reports. While they are on, the terminal sends ESC[I and ESC[O when the pane gets or loses focus.

When the session ends through ssh -t and the agent's own reset does not reach the terminal, both modes stay on. Bash then gets text where it expects keys. Readline takes ESC[27 and types ;5;99~ into the line. On 2026-10-31 the line became dla <url>;5;99~;5;99~;5;99~;5;99~exit. Bash ran dla, then tried to run 5 and 99~.

dl already writes a restore (a fixed list of escape sequences that turn off modes a session can leave on) after every session. That list turned off the kitty keyboard protocol, but it did not turn off modifyOtherKeys or focus reports. Now the list also writes ESC[>4m and ESC[?1004l. ESC[>4m resets modifyOtherKeys to the terminal's own configured value, so a user who sets XTerm*modifyOtherKeys: 1 keeps that value. Neither sequence is a push, so both change nothing on a terminal that is already at its defaults.

Evidence

The pty test in rust/dl/tests/terminal.rs runs the built dl on a pty. A fake devpod session switches on the kitty keyboard protocol, modifyOtherKeys and focus reports, then exits with status 255. These are the bytes that reached the terminal after the session output, on main and on this branch:

- \r\n ESC[?1047l ESC[<u                      ESC[?2004l ESC[?1000l … ESC[?7h ESC[0m
+ \r\n ESC[?1047l ESC[<u ESC[>4m ESC[?1004l ESC[?2004l ESC[?1000l … ESC[?7h ESC[0m

On main, nothing turns off modifyOtherKeys or focus reports, so the terminal keeps both modes. On this branch, the restore turns off both. Source: the test with a temporary eprintln! of the bytes it read, run in a worktree of main and on this branch.

full bytes
main:   \r\n\u{1b}[?1047l\u{1b}[<u\u{1b}[?2004l\u{1b}[?1000l\u{1b}[?1002l\u{1b}[?1003l\u{1b}[?1005l\u{1b}[?1006l\u{1b}[?1015l\u{1b}[?25h\u{1b}[?7h\u{1b}[0m
branch: \r\n\u{1b}[?1047l\u{1b}[<u\u{1b}[>4m\u{1b}[?1004l\u{1b}[?2004l\u{1b}[?1000l\u{1b}[?1002l\u{1b}[?1003l\u{1b}[?1005l\u{1b}[?1006l\u{1b}[?1015l\u{1b}[?25h\u{1b}[?7h\u{1b}[0m

I did not test this in a real terminal emulator. The machine I used has no display.

What this changes

  • rust/devlaunch-runner/src/terminal.rs: RESTORE also writes ESC[>4m (modifyOtherKeys reset) and ESC[?1004l (focus reports off). The doc comment says why. A new unit test pins both sequences.
  • rust/dl/tests/terminal.rs: the fake session also switches on modifyOtherKeys and focus reports. The test fails if dl does not turn off both after the session.
  • docs/cli.md: the page on a dead session now names modifyOtherKeys, focus reports, the ;5;99~ symptom and printf '\033[>4m\033[?1004l' as the manual fix.
  • Release 0.64.2: the version in rust/Cargo.toml, rust/Cargo.lock and README.md, and a CHANGELOG.md entry.

Where this fits

 session ends (cleanly or not)
   terminal::restore()
     ESC[?1047l   alternate screen off
     ESC[<u       kitty keyboard protocol: pop one entry
+    ESC[>4m      modifyOtherKeys reset
+    ESC[?1004l   focus reports off
     ESC[?2004l   bracketed paste off
     …            mouse modes off, cursor on, autowrap on, attributes reset

How I checked it

The pty test failed before the fix and passes after it:

fake devpod session prints: kitty push, modifyOtherKeys on, focus on, "REMOTE-PUSHED"; exits 255
run `dl <ws> -- true` on a pty
assert the bytes after "REMOTE-PUSHED" contain ESC[>4m and ESC[?1004l

With the terminal.rs fix removed, a_session_that_dies_holding_the_terminal_gives_it_back fails. With the fix, it passes.

A self-review changed ESC[>4;0m to ESC[>4m in c92616a3. The first form forced the mode to 0 and lost a user's own setting. After that change, I ran the pty test again and read the bytes above.

commands I ran
cd rust
pixi run cargo test --locked --workspace           # 39 test result lines, all ok
pixi run cargo clippy --locked --all-targets -- -D warnings   # clean
pixi run cargo fmt --check                         # clean
pixi run cargo build --locked --release -p dl -p dla
cd ..
pixi run pytest -q test/test_readme_cli_doc.py test/test_citations_resolve.py test/test_docs_prose.py   # 305 passed
prek run --from-ref origin/main --to-ref HEAD      # passed

I did not run pixi run test-e2e.

Merge danger

Door: one-way. The code change is a plain revert. But the merge publishes 0.64.2 to the conda channel and PyPI, and a published version stays published.

Blast radius: small. If a terminal does not ignore ESC[>4m or ESC[?1004l, it prints those bytes once after each session. Every xterm-compatible terminal ignores a sequence it does not know.

Not covered

A host claude session that dl did not start can also leave these modes on. dl cannot fix that case, because dl does not run the session. A PROMPT_COMMAND in the shell is the fix for that case.

🤖 Generated with Claude Code

Summary by Sourcery

Restore terminal input and focus behavior after sessions end unexpectedly, and release version 0.64.2.

Bug Fixes:

  • Ensure terminal cleanup disables xterm modifyOtherKeys and focus reporting after sessions that exit without performing their own cleanup, restoring reliable Ctrl-C and focus behavior at the shell.

Tests:

  • Extend terminal tests to verify both stranded modes are reset after an interrupted session.

Chores:

  • Bump the project version and release metadata to 0.64.2.

…sion

Claude Code switches on xterm modifyOtherKeys and focus reports in the
container. When the session ended without its own reset reaching the
terminal, both stayed on: Ctrl-C reached bash as ESC[27;5;99~, readline
typed ";5;99~" into the line, and the next Enter ran it.

The restore dl writes after every session now also writes ESC[>4;0m and
ESC[?1004l. Both are sets, so they change nothing on a terminal that is
already in the default state.

Release 0.64.2.
CSI > 4 m resets the resource to its initial value, so a user's modifyOtherKeys: 1 survives a session.
@sourcery-ai

sourcery-ai Bot commented Oct 31, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

The PR fixes terminal state leakage after sessions that exit without cleanup by adding non-destructive modifyOtherKeys and focus-report resets to the unconditional restore sequence, adds unit and pty regression coverage, and releases the changes as version 0.64.2.

Sequence diagram for terminal restoration after an unclean session

sequenceDiagram
    participant Shell
    participant dl
    participant Session
    participant Terminal

    dl->>Session: start session
    Session->>Terminal: enable modifyOtherKeys and focus reports
    Session--xdl: exit without cleanup
    dl->>Terminal: RESTORE
    Note over Terminal: ESC[>4m resets modifyOtherKeys
    Note over Terminal: ESC[?1004l disables focus reports
    Terminal-->>Shell: Ctrl-C remains an interrupt key
Loading

File-Level Changes

Change Details Files
Extend unconditional terminal cleanup to restore xterm keyboard and focus modes after sessions exit.
  • Add modifyOtherKeys reset while preserving the terminal's configured default.
  • Disable focus reporting in the restore sequence.
  • Document the affected escape sequences and failure mode.
  • Add unit coverage for both restore sequences.
rust/devlaunch-runner/src/terminal.rs
Strengthen the pseudo-terminal integration test to verify cleanup after an unclean session.
  • Make the fake session enable kitty keyboard, modifyOtherKeys, and focus reporting.
  • Assert that both corresponding off sequences arrive after session output.
rust/dl/tests/terminal.rs
Release the project as version 0.64.2 and update user-facing release metadata.
  • Bump the workspace and lockfile versions.
  • Update the changelog, README version output, and Conda badge.
  • Document the terminal Ctrl-C/focus-reporting fix.
rust/Cargo.toml
rust/Cargo.lock
CHANGELOG.md
README.md

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@codecov

codecov Bot commented Oct 31, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.79%. Comparing base (e3be446) to head (de56c9e).

Additional details and impacted files
Flag Coverage Δ
python 42.98% <ø> (ø)
rust 94.98% <100.00%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
shipped code (rust) 94.98% <100.00%> (+0.01%) ⬆️
harness and tooling (python) 42.98% <ø> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@blooop
blooop marked this pull request as ready for review October 31, 2026 11:34
…cribed

Review follow-ups on #718: docs/cli.md and the terminal.rs module doc now
name the two modes, the ;5;99~ symptom and the manual printf. The module
doc says ESC[>4m is a reset, not an off. The CHANGELOG entry links #718.

@blooop blooop left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

This was generated by AI during review.

Reviewed head de56c9ee against merge base e3be4469 (git diff e3be4469...HEAD). The Standards axis ran in a fresh context that did not see the code written. Its findings at 1b48e541 are below, with what de56c9ee did about each one.

Standards

  1. The release is in the same commit as the fix (25ad70f8), and the fixup commits c92616a3 and 1b48e541 stay in the merge history. Earlier releases are a separate release: X.Y.Z commit (931b730f, #716). Non-blocking. Not changed: the split needs a force-push for a cosmetic gain.
  2. The CHANGELOG entry has no PR link, and 0.64.1's entry has one. Non-blocking. Fixed in de56c9ee.
  3. docs/cli.md describes only the kitty case: the 9;133u symptom and printf '\033[<u'. The ;5;99~ symptom and its manual fix are missing. The module doc in terminal.rs lists the same four modes. Non-blocking. Fixed in de56c9ee: both now name modifyOtherKeys and focus reports, and the doc names the symptom and printf '\033[>4m\033[?1004l'.
  4. The claim "every sequence is a no-op when it is not needed" overstates ESC[>4m. It resets the mode to the configured value, so it also undoes a value that the user's own shell set. Non-blocking. Fixed in de56c9ee: the module doc and docs/cli.md say it is a reset, not an off.
  5. Fowler smells: the two sequences appear in RESTORE, in the unit test and in the pty test constants. RESTORE is pub(crate), so the integration test cannot reuse it. Nit, acceptable. No other smells.
  6. PR body: a reviewer gets the why and the change from the body alone, and it matches the head (files, test name, byte order). The reported line was quoted in two forms. Nit. Fixed in de56c9ee.

Spec

No spec available. The PR closes no issue, and no commit names one.

Verdict

Approve. No blocking findings.

@sourcery-ai sourcery-ai 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.

Hey - I've found 3 issues

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="rust/devlaunch-runner/src/terminal.rs" line_range="93-94" />
<code_context>
 pub(crate) const RESTORE: &str = concat!(
     "\x1b[?1047l", // alternate screen off
     "\x1b[<u",     // kitty keyboard protocol: pop one stack entry
+    "\x1b[>4m",    // xterm modifyOtherKeys reset
+    "\x1b[?1004l", // focus reports off
     "\x1b[?2004l", // bracketed paste off
     "\x1b[?1000l", // mouse: normal tracking off
</code_context>
<issue_to_address>
**Terminal modes remain enabled**

When signal-driven shutdown restores the terminal before the foreground child stops, and the child writes mode-setting sequences afterward, `interrupt::drain` does not restore the terminal again after the child exits, and `cleanup_and_exit` uses `_exit`, leaving those modes enabled when `dl` exits.

Restore the terminal again after the foreground child has stopped and before exiting.
</issue_to_address>

### Comment 2
<location path="rust/devlaunch-runner/src/terminal.rs" line_range="93-94" />
<code_context>
 pub(crate) const RESTORE: &str = concat!(
     "\x1b[?1047l", // alternate screen off
     "\x1b[<u",     // kitty keyboard protocol: pop one stack entry
+    "\x1b[>4m",    // xterm modifyOtherKeys reset
+    "\x1b[?1004l", // focus reports off
     "\x1b[?2004l", // bracketed paste off
     "\x1b[?1000l", // mouse: normal tracking off
</code_context>
<issue_to_address>
**Parent terminal modes disappear**

When `dl` runs inside a terminal application that enabled focus reports or a non-resource `modifyOtherKeys` mode, `terminal::restore` unconditionally writes the focus-off and `modifyOtherKeys` reset sequences instead of preserving the modes active before `dl`, so the enclosing application loses its expected focus or modified-key events.

Preserve and restore the caller's prior focus-report and `modifyOtherKeys` settings instead of unconditionally resetting them.
</issue_to_address>

### Comment 3
<location path="CHANGELOG.md" line_range="14" />
<code_context>
+
+### Fixed
+
+- **Ctrl-C works at the shell again after a session that ends without cleaning up.**
+  Claude Code switches on xterm `modifyOtherKeys` and focus reports in the container.
+  When the session ended without its own reset reaching the terminal, both stayed on:
</code_context>
<issue_to_address>
**Configured keyboard mode still breaks Ctrl-C**

When the terminal's initial `modifyOtherKeys` value is 1, the release note promises Ctrl-C works again, but the restore leaves `modifyOtherKeys` enabled, so bash still receives `ESC[27;5;99~` instead of SIGINT.

Qualify the release note to state that Ctrl-C is restored when the terminal's configured initial mode is off.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment on lines +93 to +94
"\x1b[>4m", // xterm modifyOtherKeys reset
"\x1b[?1004l", // focus reports off

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Medium · Terminal modes remain enabled

When signal-driven shutdown restores the terminal before the foreground child stops, and the child writes mode-setting sequences afterward, interrupt::drain does not restore the terminal again after the child exits, and cleanup_and_exit uses _exit, leaving those modes enabled when dl exits.

Restore the terminal again after the foreground child has stopped and before exiting.

Prompt for AI agents
In `rust/devlaunch-runner/src/terminal.rs` at lines 93-94:

**Terminal modes remain enabled**

When signal-driven shutdown restores the terminal before the foreground child stops, and the child writes mode-setting sequences afterward, `interrupt::drain` does not restore the terminal again after the child exits, and `cleanup_and_exit` uses `_exit`, leaving those modes enabled when `dl` exits.

Restore the terminal again after the foreground child has stopped and before exiting.

Comment on lines +93 to +94
"\x1b[>4m", // xterm modifyOtherKeys reset
"\x1b[?1004l", // focus reports off

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Medium · Parent terminal modes disappear

When dl runs inside a terminal application that enabled focus reports or a non-resource modifyOtherKeys mode, terminal::restore unconditionally writes the focus-off and modifyOtherKeys reset sequences instead of preserving the modes active before dl, so the enclosing application loses its expected focus or modified-key events.

Preserve and restore the caller's prior focus-report and modifyOtherKeys settings instead of unconditionally resetting them.

Prompt for AI agents
In `rust/devlaunch-runner/src/terminal.rs` at lines 93-94:

**Parent terminal modes disappear**

When `dl` runs inside a terminal application that enabled focus reports or a non-resource `modifyOtherKeys` mode, `terminal::restore` unconditionally writes the focus-off and `modifyOtherKeys` reset sequences instead of preserving the modes active before `dl`, so the enclosing application loses its expected focus or modified-key events.

Preserve and restore the caller's prior focus-report and `modifyOtherKeys` settings instead of unconditionally resetting them.

Comment thread CHANGELOG.md

### Fixed

- **Ctrl-C works at the shell again after a session that ends without cleaning up.**

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Medium · Configured keyboard mode still breaks Ctrl-C

When the terminal's initial modifyOtherKeys value is 1, the release note promises Ctrl-C works again, but the restore leaves modifyOtherKeys enabled, so bash still receives ESC[27;5;99~ instead of SIGINT.

Qualify the release note to state that Ctrl-C is restored when the terminal's configured initial mode is off.

Prompt for AI agents
In `CHANGELOG.md` at line 14:

**Configured keyboard mode still breaks Ctrl-C**

When the terminal's initial `modifyOtherKeys` value is 1, the release note promises Ctrl-C works again, but the restore leaves `modifyOtherKeys` enabled, so bash still receives `ESC[27;5;99~` instead of SIGINT.

Qualify the release note to state that Ctrl-C is restored when the terminal's configured initial mode is off.

@blooop
blooop merged commit 86081fa into main Oct 31, 2026
15 checks passed
@blooop
blooop deleted the fix/reset-keyboard-modes branch October 31, 2026 11:46
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.

1 participant