szn

Bug 500 — tty.Term.setAttributes sixel-bit SGR desync (latent)

500. Non-SGR marker bit leaks into the SGR diff

Severity: LOW (latent — Term has no production callers today; would be MEDIUM if wired up) Status: 🟢 FIXED Files: src/tty/tty.zig:145-172

Problem

Attr (grid.zig) is a packed u16 with 11 real SGR flags plus a sixel marker bit that is not a terminal attribute. The SGR emitter deliberately excludes it (attrFields = all[0 .. all.len - 2]), but the change-diff uses the raw bitcast including the sixel bit:

const changed = @as(u16, @bitCast(attrs)) ^ @as(u16, @bitCast(self.attrs));

Trace old = {} → new = {sixel} (reachable in principle: screen.zig ORs attr.sixel onto blank cells under sixel images): changed ≠ 0 → no early return; new ≠ 0 → skips the reset branch; old == 0 → skips the ESC[m + fg/bg-null branch; the field loop emits nothing → a bare ESC[m goes to the terminal (full SGR reset, colors cleared) without nulling self.fg/self.bg. The terminal’s colors are reset but Term still believes fg/bg are set → subsequent same-color sets become no-ops → wrong colors until something forces a change.

Currently latent: render.zig imports only tty.Error; Term itself is instantiated only in tests. But this is clearly intended as the client terminal emitter, so the desync will go live the moment it’s wired up.

Proposed Fix

Mask the sixel bit out of the diff before comparing (and out of the stored self.attrs, or store the masked value consistently):

const SIXEL_BIT: u16 = 1 << 11; // keep in sync with Attr layout, or comptime-derive
const changed = (@as(u16, @bitCast(attrs)) ^ @as(u16, @bitCast(self.attrs))) & ~SIXEL_BIT;

Also see #17’s note: attrFields/attrCodes are coupled to Attr declaration order via all[0 .. all.len - 2] — build them from explicit name→code pairs instead.

Resolution