# browser ephemeral vm

- Status: `experiment`
- Track: [browser containment & sandbox boundaries](https://aifoc.us/labs/browser-containment/)
- Layer: 1. Execution Containment & Browser Security
- Updated: 1 December 2026
- Live demo: https://browser-ephemeral-vm.kinlan-test.workers.dev
- Source: https://github.com/PaulKinlan/browser-ephemeral-vm

## Inspired by

- [wat the heck: why ship javascript when nobody reads the code?](https://aifoc.us/wat-the-heck/)
- [the browser is the sandbox](https://aifoc.us/the-browser-is-the-sandbox/)

## Hypothesis

- **Claim:** Fuel-metered `.wasm` linear memory paired with binary OPFS checkpoints provides sub-millisecond deterministic halt and byte-exact rollback for untrusted compute scripts inside a standard browser tab.
- **Confirms it:** Runaway loops trap via `WebAssembly.RuntimeError` at the exact fuel limit, and rolling back to a pre-run OPFS checkpoint leaves zero residual mutated bytes in the hex diff.
- **Falsifies or bounds it:** Checkpoint serialization to OPFS exceeds acceptable turn latency on multi-megabyte linear memories or infinite host-import callbacks stall before fuel check increments.

> **Why this exists:** When an application runs generated code or untrusted scripts inside the browser, two things go wrong: an infinite loop can lock up the tab forever, and killing a crashed script leaves half-mutated data behind. This lab tests how to run code inside a strict step budget ("fuel") and roll memory cleanly back to a known snapshot instead of restarting a full server container.


## What this lab tests

[Browser Ephemeral VM](https://browser-ephemeral-vm.kinlan-test.workers.dev) ([GitHub](https://github.com/PaulKinlan/browser-ephemeral-vm)) runs a fuel-metered linear-memory `.wasm` binary inside a Dedicated Worker backed by [OPFS](https://developer.mozilla.org/en-US/docs/Web/API/File_System_API/Origin_private_file_system) binary checkpoints (`vm-ckpt-*.bin`), aborting deterministically via `WebAssembly.RuntimeError` on step-budget exhaustion and computing exact byte-level hex diffs and rollbacks.


## Open questions to iterate on

- How large can WASM linear memory checkpoints grow before incremental page hashing beats full `.bin` snapshots in OPFS?
- How should capability-gated host imports expose DOM or OPFS slices without leaking host heap state?

## Iteration log

- **2026-12-01:** Shipped fuel-metered `.wasm` Dedicated Worker kernel, OPFS checkpoint snapshots, hex diff view, and top-of-page setup code.
- **2026-12-01:** Moved the fuel-metered Wasm VM workbench above the setup blueprint for 700px embeds, added CSP-safe DOM syntax highlighting for WAT/hex-diff/wire views, and tightened Worker CSP with `no-cache` static asset routing (15 passing checks).

## Primitives tested

- `WebAssembly.Memory`
- `Fuel Metering`
- `OPFS Snapshots`
- `Dedicated Worker`

## Connected labs in other tracks

- [live .wat-to-.wasm compiler & equivalence gate](https://aifoc.us/labs/local-compute/zero-js-wat-web/) — Compiles raw `.wat` source that could run inside this metered VM.
- [slice capability mesh](https://aifoc.us/labs/browser-containment/slice-capability-mesh/) — Brokers scoped OPFS handles that ephemeral Wasm tasks read.

---
Canonical: https://aifoc.us/labs/browser-containment/browser-ephemeral-vm/
