Skip to content

feat: graphics substrate (registry, tier engine, cell integration) - #141

Draft
natemoo-re wants to merge 3 commits into
spec/graphicsfrom
feat/graphics-substrate
Draft

natemoo-re wants to merge 3 commits into
spec/graphicsfrom
feat/graphics-substrate

Conversation

@natemoo-re

Copy link
Copy Markdown
Member

Implements the Graphics Specification engine in C (stacked on the spec PR; the public TypeScript surface + test suite land in the img PR after this).

What lands here

Memory (§5). The engine gains a second, dimension-independent carve beside the per-dimension clayterm carve: [gfx: entry table (256) | pixel pool (4 MiB default) | placements tables (64×2 double-buffered)]. Because the region never moves or re-initializes, the registry survives resize untouched (§10.5) — and this is exactly the property §4.5 calls out for the future zero-copy canvas surface views. setImage/removeImage MUST NOT grow linear memory (INV-I8); capacity failures are closed RangeError codes, compaction is the only reclamation.

Registry surface (§7.2). Two-phase native calls: image_begin validates and reserves (returning the absolute address the host writes pixels at — video's same-size-per-frame path allocates nothing), image_commit bumps the data version. image_remove emits the protocol's data-freeing d=I deletion block into the output buffer and reconciles covered cells to their blank state (INV-I6) so the next diff emits nothing redundant.

Tier engine (§9). Pre-layout resolution (variant, omission, liveness, evidence mirror, line-mode cap) expands the void img op into a Clay element: the alt tier is a wrapped, vertically-clipped text child (so alt's fit sizing and wrap come from the layout engine's own text machinery); pixel tiers attach the op's wire address as customData. The render-command walk then applies the four footprint-preserving demotions (clip containment §8.5, placement-id collision §9.2.1, pixel-overlap §10.3, table capacity §5.4) — kitty→ascii only, same painted footprint, no re-measurement — then records the placement or paints the art.

Frame phases (§9.2.6). Coverage pass (pixel coverage asserts blanks over the back buffer, later element wins shared cells) → repair precheck (a placement whose covered cells will receive writes re-places; runs before any dirty-marking) → scoped stale deletions (lowercase d=i,i=…,p=…, data retained) → delete-before-re-transmit chunked transmission (4096-char base64 chunks, m=1/m=0, q=2 on every block) → cell writes (the ordinary diff) → placements after, with the §7.6 cursor-restore carve-out. Steady state is byte-silent.

Ascii art (§9.3). Per-cell circle sampling (6 internal + 10 external, geometry in the source and mirrored in the generator), directional-then-global contrast with power-of-two exponents (γd=4, γg=2 — exact repeated squaring, no libm, bit-identical everywhere), 95-character nearest-match against shape vectors generated at build time from the public-domain font8x8 bitmap font (deno task gen-shape, golden-lockable).

Capability mirror. graphics_capability(ct, kitty) — the renderer's private capability state (terminfo-spec §4.2); the true→false transition emits the §11.2 denial bytes immediately (TINV-5). Wired to the update() capability fold in the img PR.

Verification

  • make builds clean; the full existing suite passes (27 tests / 328 steps — post-feat: term.update() folds CapabilityEvents into runtime capabilities #133 main, this branch is rebased onto it).
  • Engine exercised end to end against the real wasm (scratch smoke, not committed): registry two-phase set → ascii art cells with sampled colors → capability grant → exact kitty transmission/placement blocks → byte-silent steady state → d=I removal bytes.
  • Spec touch (pre-approved by the spec PR's Appendix A): current-surface notes A5/A6 — renderer-spec §12.1/§12.2/§12.3 now describe the img opcode, props, and the three image error types that exist with this PR.
  • No changeset: no public TypeScript API yet; the img PR carries it.

@bombshell-cooper

bombshell-cooper Bot commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

No changeset needed

This pull request is classified as no_release_impact: @bomb.sh/tty

Comment thread .changeset/cooper-approved-132.md Resolved
@github-actions

github-actions Bot commented Oct 9, 2026 •

Copy link
Copy Markdown

Size Increased — +12.5 KB

156.2 KB unpacked

@pkg-pr-new

pkg-pr-new Bot commented Oct 9, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@bomb.sh/tty@141

commit: df1f984

@natemoo-re
natemoo-re force-pushed the feat/graphics-substrate branch from 8813820 to 30aa8fa Compare October 9, 2026 20:17
@natemoo-re
natemoo-re force-pushed the feat/graphics-substrate branch from d7ab323 to 3d0eef2 Compare December 4, 2026 03:06
The pixel-surface registry, tier engine (kitty, shape-aware ascii,
alt), placements record, and cell-buffer integration of the Graphics
Specification, ported onto the retained frame pair.

- Graphics state lives in its own dimension-independent carve at the
  heap base; clayterm_init takes it, resize leaves it in place.
- OP_IMG decodes in execute_current_runlist like any element: it is
  recorded for ElementInfo, so its replay range covers the packed op
  and re-resolves registry liveness and tier each render.
- The declared bg accepts symbolic color words and resolves against the
  inherited color; alt text inherits the foreground like text().
- Image error text is composed into per-frame storage alongside the
  frame's errors, so it stays readable while that frame is previous.
- Graphics bytes are emitted inside present_cups' synchronized-update
  wrap: queued and stale deletions plus transmissions before the cell
  diff, placements after it and before the caret step.
- Cleanup never produces bytes outside render(). Removal, kitty-graphics
  denial, and resize enqueue wire ids in the pending-deletion queue;
  the next cursor-mode render drains it first. Overflow collapses to
  one delete-all with transmission resets and deletion damage.
- The coverage pass runs after the color-tier check, so a tier change's
  complete redraw re-places live placements without re-transmitting.
- input.c ignores APC frames carrying an a= action key, so relayed
  quiet commands never surface as kitty-graphics capability events.
The output buffer only carried the per-cell text budget, and buf_put
drops writes that do not fit, so a large transmission reached the
terminal as a truncated APC stream: a 200x200 image on a 40x12 Term
emitted 59 KB of a ~213 KB encoding with no error.

- The output capacity adds graphics_output_bytes(pool) per Graphics
  Specification §5.6, fixed at init from the pool size and kept across
  resize. clayterm_size takes the pool size so the host carves for it.
- Buffer records an overflow flag that the next render clears. reduce()
  reports CLAYTERM_REDUCE_OUTPUT instead of returning partial bytes,
  and the host throws a RangeError. The sizing makes this unreachable;
  graphics retained state is not rolled back on that path.
@natemoo-re
natemoo-re force-pushed the feat/graphics-substrate branch from 3d0eef2 to beb3bca Compare December 4, 2026 03:09
Two bugs that left the img demo's panels black (both first found on
spike/img-demo, c6ab8ee and a42e2df, and never carried over):

- Continuation chunks began their keys with a comma (_G,m=1,q=2;...).
  Strict parsers such as ghostty's reject the empty key and drop the
  whole transmission; q=2 hides the failure, so every placement then
  points at an image the terminal never stored. Continuations now
  emit only m and q, per §9.2.2.
- Clay emits an element's background rect after its custom command, so
  a declared bg painted over the ascii art drawn in the custom handler.
  Pixel-tier img elements now mark their rect to be skipped, and the
  handler paints the background itself before the art or placement.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant