Skip to content

Make SyncToken opaque per Terminal and guard _Row mutations with write barriers #22

Done

Description

@tony

Summary

SyncToken in src/libvt/terminal.py (#13) exposes 15 public dataclass fields coupled to a process-global _GLOBAL_ROW_GEN counter in src/libvt/_grid.py, while _Row mutates wrapped and cell state across multiple direct attribute writes that rely on manual bump_row_gen() calls. Executing DCH (CSI P) at column 0 left of a DECLRMM (?69h) left margin clears scr._rows[c_row].wrapped = False without bumping content_gen, causing Terminal.changes_since() to return dirty_rows == () while snapshot()[0].wrapped flipped from True to False.

Reproduction

  1. Soft-wrap row 0, enable DECLRMM (?69h) with margins 3..6, move to (0, 0), capture sync_token(view="working"), and feed CSI P:
uv run python -c 'from libvt import Terminal; vt = Terminal(rows=3, cols=6); vt.feed(b"ABCDEFG\x1b[?69h\x1b[3;6s\x1b[1;1H"); tok = vt.sync_token(view="working"); vt.feed(b"\x1b[P"); d = vt.changes_since(tok, view="working"); print("wrapped:", vt.snapshot()[0].wrapped, "dirty_rows:", d.dirty_rows)'

Expected

  • DCH left of DECLRMM either leaves row 0 untouched (including wrapped) or reports row 0 in dirty_rows; a ScreenDelta replica always matches vt.snapshot() across lines, cell styles, and wrapped flags.
  • SyncToken keeps its watermark fields private (repr compact and opaque, supporting == and hashing) and uses a per-Terminal (per-ScreenSet) monotonic row generation counter rather than a process-global integer shared across unrelated Terminal instances.
  • Every _Row mutation routes through structural write barriers on _Row that bump content_gen and invalidate cached _frozen automatically, backed by a debug/test row-fingerprint check.

Actual

wrapped: False dirty_rows: () — incremental consumers miss the wrapped transition and diverge from Snapshot.

Environment

Versions
  • libvt: #2 (0.1.0a0)
  • Python: 3.15.1
  • OS: Linux x86_64

Proposal

Invariants

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

  • Callers treat SyncToken as an opaque value (==, hash, repr); internal watermark layout is private so internal representation upgrades remain non-breaking after 0.1.0.
  • Two independent Terminal instances never contend on or advance a shared module-global row-generation counter, matching WezTerm Surface SequenceNo at ee6f3044d.
  • No mutation to a _Row's characters, packed attributes, sparse extensions, used, text_used, or wrapped flag can occur without advancing that row's content_gen.

Intent (non-binding)

  • Give each Screen / ScreenSet a shared monotonic generation counter cell, make _Row mutable state private behind mutator methods (set_wrapped(), clear_spacer_head(), copy_slice_from(), etc.), and guard DCH wrapped clearing behind left <= c_col <= right.
  • Add a randomized sequence fuzz test verifying ScreenDelta.apply_rows() replica equality (including wrapped and Cell attributes) against Terminal.snapshot() after every operation.

Not doing

  • Exposing internal watermark integers as public SyncToken attributes.

References

Activity

  1. added
    bug
    on Nov 23, 2026
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