Skip to content

Freeze ?2026 committed snapshots and cursor state at BSU #5

Closed

Description

@tony

Summary

Mode ?2026 synchronized output in src/libvt/terminal.py (#2) exhibits two coupled snapshot defects: Terminal.committed depends on feed() chunk splits and whether committed was previously read, and cursor/mode mutations inside an active ?2026h hold leak into committed before CSI ? 2026 l.

Reproduction

  1. Compare whole-chunk versus split-chunk B + CSI ? 2026 h + C after reading committed at A:
$ uv run python -c 'from libvt import Terminal; a = Terminal(rows=1, cols=6); a.feed(b"A"); _ = a.committed; a.feed(b"B\x1b[?2026hC"); b = Terminal(rows=1, cols=6); b.feed(b"A"); _ = b.committed; b.feed(b"B"); b.feed(b"\x1b[?2026hC"); print("whole:", a.committed.lines, "split:", b.committed.lines)'
  1. Move the cursor and hide DECTCEM (?25l) inside ?2026h without writing printable text:
$ uv run python -c 'from libvt import Terminal; vt = Terminal(rows=4, cols=8); vt.feed(b"A"); _ = vt.committed; vt.feed(b"\x1b[?2026h\x1b[2;3H\x1b[?25l\x1b[6n\x1b[?2026$p"); print("committed:", vt.committed.cursor, "live:", vt.cursor, "replies:", vt.drain_replies())'

Expected

  • Step 1 prints whole: ('AB',) split: ('AB',) across whole-buffer, split-buffer, byte-by-byte, and uninspected runs, and updates committed.lines to ('ABC',) on CSI ? 2026 l.
  • Step 2 keeps committed.cursor at 0-based (0, 1) with visible=True until CSI ? 2026 l, while updating vt.cursor to (1, 2) (visible=False) and queuing live replies b'\x1b[2;3R\x1b[?2026;1$y' immediately.

Actual

  • Step 1 prints whole: ('A',) split: ('AB',) because self._generation increments only at the end of feed(), causing self.snapshot() inside _ensure_sync_snapshot() to hit self._cached_snapshot.generation == self._generation and return the stale 'A' snapshot.
  • Step 2 prints committed: Cursor(row=1, col=2, visible=False, ...) because _set_cursor() and _apply_mode() do not call _before_mutate(), deferring snapshot capture until vt.committed is read after the cursor and visibility have already mutated.
Environment
  • libvt: #2 (feat/v2-architecture, 0.1.0a0)
  • Python: 3.15.0 (unicodedata 17.0.0)
  • Reference tmux: 3.8a (tmux -f /dev/null)
  • OS: Linux x86_64
Affected code in src/libvt/terminal.py (#2)
def _ensure_sync_snapshot(self) -> None:
    if self._sync_hold_pending and self._held_snapshot is None:
        self._held_snapshot = self.snapshot()
        self._sync_hold_pending = False

Proposal

Invariants

The invariants below are load-bearing; everything under Intent is open.

  • At any byte offset where mode ?2026 is reset, vt.committed == vt.snapshot().
  • At the exact transition ?2026l -> ?2026h, committed latches the presentation state (cells, styles, active screen, cursor coordinates/visibility/shape/blink, title, working directory, prompt zones, program status, and visual modes) produced by all bytes strictly preceding CSI ? 2026 h, independent of feed() chunking or observer reads.
  • Repeated CSI ? 2026 h while mode ?2026 is already active is a no-op and never overwrites the latched frame; unfreezing occurs on CSI ? 2026 l, DECSTR, RIS, Terminal.resize(), or an explicit sync-timeout expiration call (never a wall-clock timer inside feed()).
  • Protocol queries (CPR, DECXCPR, DECRQM, DA1/DA2, XTVERSION, DECRQSS, CSI ? u, CSI ? 996 n, OSC 10/11 ?) continue to reply immediately from live parser and screen state.

Intent (non-binding)

  • Capture lightweight scalar frame metadata plus shallow row references (or per-row sync_epoch copy-on-write stamps on _Row) at ?2026l -> ?2026h so cursor-only holds clone zero rows while keeping committed O(1) when ?2026 is inactive.
  • Advance _generation (or invalidate _cached_snapshot = None) whenever terminal state mutates rather than only once per feed() call.

Not doing

  • Adding wall-clock asyncio timers inside sans-I/O Terminal.feed().

References

Activity

  1. added
    bug
    on Nov 22, 2026
  2. tony commented on Nov 23, 2026

    @tony
    MemberAuthor

    Resolved in #2 (0c7ee41).

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions