Skip to content

fix(vterm): replay DEC private modes on attach so resume restores scroll and arrows#28

Merged
aksOps merged 1 commit into
mainfrom
fix/vterm-replay-private-modes
Jun 13, 2026
Merged

fix(vterm): replay DEC private modes on attach so resume restores scroll and arrows#28
aksOps merged 1 commit into
mainfrom
fix/vterm-replay-private-modes

Conversation

@aksOps

@aksOps aksOps commented Jun 13, 2026

Copy link
Copy Markdown
Contributor

Summary

Reported against v0.2.1: on a session's first launch mouse scroll and arrow keys work, but after detach + re-attach (or resume) they go dead.

Same bug family as #24 — which replayed SGR colors so re-attach wasn't black-and-white. This is the modes version.

Root cause

The vterm captured screen content but discarded the agent's input-affecting DEC private modes:

  • setPrivateMode treated mouse tracking (?1000/1002/1003/1006), bracketed paste (?2004), and cursor visibility (?25) as "no effect on captured text", and never tracked application cursor keys (DECCKM ?1) or keypad mode at all.
  • Redraw() — what the host sends on every attach — emitted only SGR + content + cursor position.

On first attach the agent's mode-setup sequences flow live through the PTY to the already-attached client, so everything works. On detach the attach client resets those modes (mouse off, DECSTR resets DECCKM, ?2004l). On re-attach the host sends only Redraw(), leaving the client in default mode while the still-running agent expects application-cursor (SS3) arrows and mouse reporting — so arrows are ignored and the wheel does nothing (v0.2.1's ?1007 change removed the wheel→arrow side effect that had masked it).

This also covers literal resume (ResumeBackground spawns a fresh host+agent): if you resume-then-attach, the agent emits its modes into a clientless vterm and the attach replay would otherwise drop them.

Fix

Mirror #24's per-cell SGR tracking, for modes: the vterm now tracks the input/cursor-affecting private modes in a privModes map[int]bool + appKeypad bool (DECCKM ?1, mouse ?1000/1002/1003 + focus ?1004 + encodings ?1005/1006/1015, bracketed paste ?2004, cursor visibility ?25, application keypad ESC=). Redraw() replays the ones that differ from their power-on default (only ?25 defaults on → emitted as ?25l when hidden), before the grid content, so a re-attaching/resuming client lands in the agent's expected state. reset() (RIS) clears them. Alt-screen (?1049, owned by the attach client) and SGR replay are unchanged; Capture stays plain text.

Out of scope (noted): autowrap ?7, kitty keyboard flags, and charset — none cause the reported symptoms.

Testing

  • 10 new vterm unit tests: each replayed mode, default suppression (no redundant ?25h), toggle-off, RIS reset, modes set on the alt screen, and mode-before-content ordering — all written red-first
  • End-to-end TestReattachReplaysAgentPrivateModes: spawns a live host whose agent enables ?1h ?1000h ?1006h, attaches, detaches, re-attaches, and asserts the modes are replayed (the regression the test suite previously lacked)
  • Full suite: 407 tests / 20 packages, 100 under -race
  • vet, golangci-lint, gofmt, gosec (0 issues), govulncheck all clean

🤖 Generated with Claude Code

…oll and arrows

Reported against v0.2.1: on a session's first launch scroll and arrow keys
work, but after detach+re-attach (or resume) they go dead.

Root cause, same family as #24 (which replayed SGR colors): the vterm
captured screen content but discarded the agent's input-affecting DEC
private modes. setPrivateMode treated mouse tracking, bracketed paste, and
cursor visibility as "no effect on captured text" and never tracked
application cursor keys (DECCKM ?1) or keypad mode at all; Redraw emitted
only SGR + content + cursor position. On first attach the agent's mode
setup flowed live through the PTY to the attached client, so it worked. On
detach the attach client resets those modes; on re-attach the host sends
only Redraw(), leaving the client in default mode while the still-running
agent expects application-cursor (SS3) arrows and mouse reporting -- so
arrows are ignored and the wheel does nothing (v0.2.1's ?1007 change
removed the wheel->arrow fallback that had masked it).

The vterm now tracks the input/cursor-affecting modes (DECCKM ?1, mouse
?1000/1002/1003 + focus ?1004 + encodings ?1005/1006/1015, bracketed paste
?2004, cursor visibility ?25, application keypad ESC=) and Redraw replays
the ones that differ from their power-on default, before the grid content,
so a re-attaching or resuming client lands in the agent's expected state.
Capture stays plain text; alt-screen and SGR replay are unchanged.

Adds unit coverage for each replayed mode (incl. default suppression,
toggle-off, RIS reset, alt-screen, ordering) and an end-to-end
TestReattachReplaysAgentPrivateModes that detaches and re-attaches to a
live host and asserts the modes come back.
@sonarqubecloud

Copy link
Copy Markdown

@aksOps aksOps merged commit 0aa8a5c into main Jun 13, 2026
13 checks passed
@aksOps aksOps deleted the fix/vterm-replay-private-modes branch June 13, 2026 12:06
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