szn

Bug 406 — Sixel accounting cluster

406. Sixel accounting cluster: ref_inc counts unplaced markers, marker overwrite skips the decrement, eviction hook lost after alt/clone swaps, eraseDisplay uses hardcoded 20px rows and a never-shifted img.row, and >2^21 ids defeat the reference check

Severity: MEDIUM Status: ✅ RESOLVED

Site Defect
:376 vs :403-404 The marker loop breaks when grid_x >= width but ref_inc = cell_rows * cell_cols counts those cells anyway → isImageReferenced never reaches 0; the slot can only be reclaimed by the forced path at :317-332 which evicts referenced images, leaving markers that name a vanished id.
:378-382 Marker cells are written directly onto whatever cell is there — overwriting an existing sixel marker without decrementMainGridRef (every erase path at :736/:747/:791 does decrement). Second inflation route.
:527 setupGridEviction is wired only in resize. useAltScreen swaps in a fresh Grid.init (hook null); Grid.clone (grid.zig:147-154) does not carry on_line_evict/ctx; restoreSavedGrid reinstalls a hook-less clone — history trims on those grids silently leak references.
:994-1002 eraseDisplay overlap test uses hardcoded (px_height + 19) / 20 while placement uses cell_px_height (:352), and compares against img.row, which is set once at :343 and never shifted by shiftSixelAnchors (which updates anchor_row only) — after any scroll, keep/free decisions use stale geometry: visible images are freed (markers dangle) or dead ones persist.
:291 vs :431 Reference check passes the full img.id but the comparison masks (img.id & 0x1FFFFF); after 2^21 images every check reports “not referenced” and slots are clobbered under live markers.

Fix: count only placed markers; decrement before overwrite; install the eviction hook on every grid swap; compute erase overlap from anchor_row with the current cell_px_height; mask both sides of the id comparison.