Skip to content

feat(rune-kit): sandboxed display math for latex and math fences - #1294

Merged
Hona merged 4 commits into
devfrom
ws3/markdown-math
Nov 21, 2026
Merged

Hona merged 4 commits into
devfrom
ws3/markdown-math

Conversation

@Hona

@Hona Hona commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

Display math for closed ```latex and ```math fences in Markdown (anomalyco/opencode#64380), behind the opt-in rune-kit feature math. RaTeX runs in a sandboxed WebAssembly module, never in the app's own address space.

How it works

  • Module: ratex-math.wasm is built from tools/ratex-math-wasm/ (guest source, pinned toolchain and lock file) by scripts/build-math-wasm.sh, which writes crates/rune-kit/assets/math/ratex-math.wasm.deflate, the KaTeX font pack and src/math/wasm_hash.rs. scripts/build-math-wasm.sh --check rebuilds from source and compares byte for byte (passes at this head).
  • Freshness check: no existing mechanism fit (the precompiled shaders need a Mac and Xcode), so crates/rune-kit/build.rs checks, when math is on, that the committed module, font pack and guest sources match the SHA-256 digests in wasm_hash.rs. A guest edit without a rebuild fails the build.
  • Native: wasmi 1.1 on one lazy background thread (rune-math). Web (wasm32): the browser's WebAssembly in a terminable Web Worker; no wasmi in the web bundle.
  • Rasterising: the host decodes the guest's RM01 display list (every byte treated as hostile) and draws it with skrifa + tiny-skia.

Host-enforced limits

Limit Value
Input 16 KiB
Fuel (native) 40,000,000 instructions
Linear memory 16 MiB (StoreLimits on native, --max-memory in the module for the browser)
Wall-clock deadline 250 ms (native check, worker.terminate() on web)
Output 1 MiB wire, 4,096 items, 16,384 path commands, coordinates within ±16,384 em
Canvas 4,096 px per side, 1 Mi px total, 2,048 dashes per rule
Queue 64 jobs
Cache 128 entries, 8 MiB of bitmaps (LRU); failures are cached and never retried

Any trap, fuel or memory exhaustion, deadline or wire error drops the guest instance and shows the fence as highlighted code.

Cost

  • Startup and hot path: nothing runs until the first closed math fence settles; a document with no latex/math fence keeps its whole-document memo (may_have_math_fence).
  • Size (FAT-LTO, Windows): wasmi host +960,000 B with opt-level = "z" on the wasmi*/wasmparser packages (saves ~404 KiB against opt-level = 3); module 809,586 B raw / 298,353 B deflated; KaTeX fonts 513,664 B raw / 267,712 B deflated.
  • Find: rendered math is skipped by Markdown::find; a fence that fell back to code stays searchable.

Notices

THIRD_PARTY_NOTICES.md (new) plus crates/rune-kit/assets/math/{OFL.txt,FONT_NOTICE.txt}. For rune-desktop/THIRD_PARTY_NOTICES.md: KaTeX fonts (SIL OFL 1.1), RaTeX 0.1.16 (MIT), unicode-normalization 0.1.26 (MIT OR Apache-2.0, Unicode-3.0), wasmi 1.1.0 (MIT OR Apache-2.0).

Proof

  • Checks at this head (farm route): native with and without math, wasm32 with math: clean, no rune-kit warnings.
  • Hostile-input fuzz (16 hostile TeX inputs incl. all READER-4 cases, 200,000 mutated RM01 payloads, 4,000 mutated TeX inputs, 50-formula async Markdown + Find): PROOF-FARM run requested at ws3/proof-1294-1139; result goes here.

…ences

Add an optional `math` feature to `rune-kit` that renders closed
```` ```latex ```` and ```` ```math ```` Markdown code fences as KaTeX-compatible
display math (`anomalyco/opencode#64380`) in pure Rust with zero JS or
WASM runtime:

- Uses `ratex-parser` + `ratex-layout` + `ratex-types` + `ratex-font` +
  `ratex-katex-fonts` (`0.1.16`, `default-features = false`, MIT +
  SIL OFL 1.1) and rasterises `DisplayList` directly via the workspace's
  existing `skrifa 0.44` + `tiny-skia 0.11` into `rune::Pixels`
  (no duplicate `skrifa`/`tiny-skia`, no `ratex-svg`/`fontdb`/`ttf-parser`).
- Bounded per-thread LRU pixel cache (128 entries / 8 MiB cap, caching
  both valid equations and parse/cap failures so streaming tokens below
  a settled fence pay zero reparses).
- Hard defense-in-depth caps: 16 KiB raw source, pre-parse nesting depth
  32, RaTeX parser depth/expansion/layout validation limits, 4,096
  display items, 4096x1024 pt logical bounds, and 4M pixel area cap.
- Open streaming fences, empty fences, parse/layout errors, and inputs
  exceeding caps fall back seamlessly to highlighted code blocks.
- Adds `MarkdownStyle::math_scale(f32)` (default 2.0) for crisp 1:1
  physical-pixel rendering at any display scale factor.
@calleum

calleum commented Nov 20, 2026

Copy link
Copy Markdown

would be nice to have it inline as well e.g. calleum@ea1ba64#diff-455c1483da6eec83a5f7a9e50612e5991db96bbc593462da95a558aa35b4dca8

Hona added 3 commits November 21, 2026 11:12
…I thread

ratex-math.wasm (built reproducibly from tools/ratex-math-wasm by scripts/build-math-wasm.sh,
checked by crates/rune-kit/build.rs) runs in wasmi on a lazy background worker (native) or
browser WebAssembly in a terminable Web Worker (wasm32), with host-enforced fuel, 16 MiB memory,
250 ms deadline, output and canvas caps, and a negative cache.
# Conflicts:
#	crates/rune-kit/src/markdown/render/blocks.rs
@Hona Hona changed the title feat(rune-kit): pure-Rust display math rendering for latex and math fences feat(rune-kit): sandboxed display math for latex and math fences Nov 21, 2026
@Hona
Hona merged commit 16418bb into dev Nov 21, 2026
1 check passed
@Hona

Hona commented Nov 21, 2026

Copy link
Copy Markdown
Member Author

Farm-built --profile perf fuzz & proof passed (branch ws3/proof-1294-1139, proof_math_fuzz 200000 4000):

  • Stage 1: 8/8 valid formulas (2.7–9.5 ms warm) + 16/16 hostile TeX inputs (all READER-4 cases) trapped/contained under 40M fuel + 16 MiB linear memory, max 166.0 ms, clean recovery after each.
  • Stage 2: 200,000 mutated RM01 wire payloads in 4.16 s (12,840 accepted, 187,160 rejected, max iter 2,641.0 µs, 0 panics, 0 truncated wires accepted).
  • Stage 3: 4,000 mutated TeX inputs in 27.75 s (76 ok, 3,924 err/trapped, max iter 89.12 ms, 0 panics).
  • Stage 4: 50 valid + 1 invalid async Markdown + Find exclusion: wakes=51 gen=51 img_els=50/50 find_rendered_matches=0 find_fallback_matches=1.
    ALL FUZZ & PROOF STAGES PASSED! — safe to enable rune-kit/math in rune-desktop.

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.

2 participants