Skip to content

W8: export a view as PNG, SVG or its files - #601

Merged
neuromechanist merged 22 commits into
feature/issue-586-epic-pipeline-progressfrom
feature/issue-588-w8-export
Nov 26, 2026
Merged

neuromechanist merged 22 commits into
feature/issue-586-epic-pipeline-progressfrom
feature/issue-588-w8-export

Conversation

@neuromechanist

@neuromechanist neuromechanist commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Refs #588, nemarOrg/nemar-cli#2324

Summary

Phase W8 of the live quality views (#588, epic #586, nemarOrg/nemar-cli#2324; nemar-cli ADR 0137 decision 9, website ADR 0035 decision 7): exports of a drawn view, made only on request. This head also merges the epic branch with W7 (#599) and W9 (#598).

What a visitor gets

  • Export toggle: each view with files to hand over has an Export this view toggle (a native details). That covers stored views and computed views (Activations, the Signal window). Its code loads only when the toggle first opens.
  • Image (PNG) at 2, 3 or 4 times, in the light print palette even when the page is dark:
    • the caption, the drawing and every scalp map the view shows;
    • a heatmap's row and column labels and its legend's stored texts;
    • printed lines naming the view.
    • Text chunks name the view's digest (nemar.view-digest.v1), its kind, kind version and permalink, plus the color maps' notice when a map or heatmap is shown.
  • Activations (owner's ruling): the drawn window as PNG, always the window on show.
    • It carries nemar:window-start-s and nemar:window-length-s beside the digest, kind and permalink.
    • It prints the tab's notes under the traces and writes them as a nemar:notes chunk, as ADR 0137 decision 9 requires: computed in the browser, the archive caption when present, the display-only 1 Hz high-pass, the store digest's state (not_checked today), the transform's caption, and the line on shaded spans.
    • The notes come from the same sentences extras/activity.ts prints, registered with the window (shownActivity); the DOM is not read.
  • Drawing (SVG) for a line chart (the spectrum) or a histogram only, as ADR 0137 says. It has a <title>, a <desc> and a <metadata id="nemar-view"> holding the same fields as JSON, with the same lines printed under it.
  • Each file of the view, or a computed view's input files (the activations' weights, components and bad segments), saved as the exact bytes NEMAR recorded:
    • The bytes come from the file source the view was drawn with, preloadedSource(recordFiles(...)).
    • They are checked against size and SHA-256 again and never re-encoded. A mismatch is said and nothing is saved.
    • Every file is asked for through that source as the controls open. A file the view never read itself (the Signal window's tables) is then held, so Save makes no request.
  • Permalink: it names the canonical site, https://nemar.org, from MARKETING_BASE_URL, the site's configured public address (lead ruling, C14). Only local development, a loopback host, writes the page's own origin.
  • Accessibility: a status line (role="status") says what was saved or why not. Buttons use aria-disabled and a guard against a second click while busy, never disabled, so the keyboard's focus stays put.

How

  • export/png.ts:
    • CRC-32 is written here; no dependency is added.
    • tEXt chunks go right after IHDR; text that Latin-1 cannot hold goes in an uncompressed iTXt chunk.
    • The browser's chunks are copied byte for byte.
    • pngChunks and pngTexts read a file back.
  • export/meta.ts: one field list for both formats, plus extra fields for a computed view. wrapText breaks lines at spaces and breaks an over-long word (a digest, a permalink) where it must.
  • export/palette.ts: the light palette, held equal to tokens.css :root by a test.
  • export/svg.ts:
    • It writes the renderer's own tree (linesSvg, histogramSvg) without the cursor, with an inline <style>, through XMLSerializer.
    • The caption and printed lines are broken to the SVG's width; the kind and the digest each get a line of their own.
  • export/raster.ts: paints the picture on a canvas.
    • Lines and histograms are painted from the SVG tree, element by element. The Content Security Policy allows no blob: image.
    • Renderer modules are imported on demand; they are already loaded.
  • export/maps-part.ts and export/heatmap-part.ts: chunks of their own, loaded as the controls open.
    • Every drawn map is painted with contoursOf lines, so maps never scrolled into view are exported with their lines too.
    • A heatmap is the page's cell canvas, with its labels and its legend.
  • Activations: the window is drawn again in 2D (activityPainter in activations.ts, the viewer's renderFrame). The page's WebGL canvas has preserveDrawingBuffer: false, so its pixels are not read back. extras/activity.ts registers the window on show (shownActivity).
  • export/download.ts: verifiedBytes, and saveBytes through a revoked object URL.
  • export/controls.ts: the controls chunk. As it opens, it starts loading the images chunk (picture.ts) and the parts the view needs, so a click makes no request.
  • export-hook.ts: the toggle. It is offered when downloadsOf(view) is not empty.

Correction: the first version of this body said W7's tab would get the toggle automatically. It did not: W7's view declares its files as inputs, and its window is WebGL. Both are wired here explicitly.

Overlap with W7 and W9

  • panel.ts:
    • drawView returns { built, files } on its success paths;
    • the scalp-map registry keeps each map's built;
    • one attachExport(...) call is made after a view draws.
  • activations.ts: shownActivity, activityWindow, activityHeight and activityPainter added at its end.
  • extras/activity.ts: registers the window on show and uses activityHeight.
  • Merge conflicts were in two files, and both sides were kept in each:
    • e2e/quality-panel.spec.ts: both test blocks;
    • ADR 0035: W7's addendum, then W8's.

Sizes (merged tree, real site build, gzip 9; all gates pass)

Measure Size Budget
Export controls chunk, on its own 2.6 KB
Images chunk (PNG and SVG), on its own 4.2 KB
Maps part / heatmap part, on its own 1.0 KB / 0.9 KB
Lazy root controls with its static imports 13.9 KB 30 KB
Lazy root picture with its static imports 10.3 KB 30 KB
Lazy root sensor-field-view 23.6 KB 30 KB
First screen, Spectrum / Components 27.8 / 27.4 KB 30 KB
First screen, Spectrum / Components, no idle preload 29.2 / 29.2 KB 30 KB
First screen, Activations / without idle preload 25.2 / 26.6 KB 30 KB
Worker with its lazy chunks (unchanged by W8) 15.6 KB 16 KB
A tab with a view's export open (printed, not gated: ADR 0035 addendum 2026-11-26) 31.6 to 36.0 KB
  • The size check fails a build where any of the four export chunks loads with a first screen.
  • W8 adds nothing to the worker.

Tests

  • test/quality-views-export.test.ts (Vitest, on the record and files captured from the real route; HTTP fixtures only):
    • PNG chunks: CRC-32 check values; every CRC verifies and the original chunks are kept byte for byte; iTXt for text past Latin-1; keyword rules.
    • Fields: digest, kind, permalink and notice present (the notice only with a map); extra fields written and printed.
    • SVG: <metadata>, title, kind and digest on their own lines, every printed line's width within the SVG on the 480 px histogram with a long caption.
    • Heatmap labels and legend: minmax and absmax, fractional room.
    • Permalink origin: canonical on staging, preview and production; the page's origin on four local forms.
    • Light palette equal to the tokens.
    • Files: byte-identical, refused with the reason on a mismatch.
    • No request: every fetch throws during an export.
    • Activations: the window's start, length and height.
  • e2e/quality-panel.spec.ts (Playwright, real browser, real panel, worker and serving-copy reader), export tests:
    • Lazy code: loads only when a toggle opens.
    • PNG: at 2× and 4×, CRCs verify, fields present, light even when the page is dark, no request (data or code).
    • SVG: parses as SVG; no color-map claim; lines fit the width as measured by the browser (getComputedTextLength).
    • Files: byte-identical, with no request.
    • Keyboard: labelled controls in Tab order. Focus stays on a button while it saves, and a second click is ignored.
    • A generic nemar.lines view is exported too.
    • 18 component maps: all exported with their contour lines, including maps never traced by the worker.
    • Heatmap: prints its labels and legend texts (minmax and absmax).
    • Activations: the window as PNG, light, with its start and length, and no request (data, code or serving-copy samples).
      • Its notes are present: the digest state, the 1 Hz filter and the shaded spans, both in the nemar:notes chunk and painted on the canvas.
      • After Later window, the export saves the new window.
      • The three input files are byte-identical.
    • Signal window: its files are asked for once as the controls open; Save then makes no request and the bytes are identical.

Mutation testing (the script is kept outside the repo):

  • First round: 61 mutations, 59 killed. The 2 survivors are equivalent: Object.hasOwn changed to in for an inherited name, and the hook's first toggle always being the opening.
  • Review fixes: 22 mutations, 22 killed (one needed an added test for fractional room).
  • Round 2 (notes and file prefetch): 6 mutations, 6 killed. They cover notes not painted, no nemar:notes chunk, notes not handed to the picture, captions dropped, notes printed twice, and Signal files fetched only at the click.
  • Activations export: 9 mutations, 9 killed:
    • window start in samples;
    • window length a sample short;
    • height not the page's;
    • export saving the first window;
    • window fields not written;
    • no image offered;
    • window not painted;
    • no toggle for computed views;
    • figure not found.

Gates (merged tree)

  • bun run typecheck: 0 errors
  • bun run lint
  • Targeted Vitest: 26 quality files, 1,802 passed (the export suite now has 26 tests)
  • astro build and check-quality-views-size.mjs --require-build: pass
  • bun run test:quality-views-perf (gate, median of 5): pass
  • e2e: quality-panel, quality-views-perf, viewer-scalp-map, viewer-quality-marks, admin-quality: 40 of 40
  • Full bun run test once: 269 files passed, 8 skipped; 8,719 tests passed. The drift tests are skipped locally; they are expected red in CI.

Deviations

  • Raw files carry no metadata. They are the exact bytes, so they cannot hold it. The controls print the permalink beside them, and the images carry everything.
  • The Activations picture is drawn again in 2D, not read from the page's WebGL canvas, whose drawing buffer is not kept.
  • The Signal window now offers its input files (no image), because the toggle is offered whenever downloadsOf(view) is not empty.
  • e2e shell styles: the Activations e2e cases add the panel's scoped plot style to the shell page. Without it, the shell's unstyled canvases spill over the toggle; the real page has the style.

Agentic AI tools were used.

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Deploying nemar-website with  Cloudflare Pages  Cloudflare Pages

Latest commit: b163c20
Status: ✅  Deploy successful!
Preview URL: https://1ce4ed78.nemar-website.pages.dev
Branch Preview URL: https://feature-issue-588-w8-export.nemar-website.pages.dev

View logs

@neuromechanist
neuromechanist merged commit e132e32 into feature/issue-586-epic-pipeline-progress Nov 26, 2026
3 of 4 checks passed
@neuromechanist
neuromechanist deleted the feature/issue-588-w8-export branch November 26, 2026 15:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant