Skip to content

feat(wasm): drive a Tauri screenshot task through the async kernal API #14

Closed

Description

@zackees

Context

This is the first end-to-end application proof for the threaded, streamed Rust sketch runtime in #13. Build and test a kernal-api-wasm example in which a Rust sketch compiled to one Wasm module drives a native Tauri webview exclusively through asynchronous kernal-api calls.

The example executable accepts a URL and an output path:

kernal-api-wasm-tauri --url <http-or-https-url> --output <screenshot.png>

It must launch a Tauri window at that URL, wait for the requested top-level page to finish loading, await five seconds, capture the visible webview viewport as PNG, atomically write the PNG to the exact output path granted by the CLI, close the window, and exit successfully.

native CLI / Tauri host
  | grants URL + exact output destination
  v
one Rust Wasm sketch
  | await webview.open(...)
  | await webview.loaded(...)
  | await clock.sleep(5 s)
  | await webview.capture_png(...)
  | await snapshot.write_to(...)
  v
private kernal-api host adapters
  |-- Windows: WebView2
  |-- macOS: WKWebView
  `-- Linux: WebKitGTK

This is deliberately not Tauri compiled into Wasm. Tauri, its event loop, native webview handles, image encoders, and filesystem implementation remain native and private. The sketch contains the orchestration/business logic and can perform host effects only through the generated, versioned kernel ABI.

Feasibility findings

The vertical slice is feasible, with one owned adapter layer:

  • Tauri 2.11.5 has WebviewWindowBuilder::on_page_load and distinguishes Started from Finished, so the host can complete an async guest future after the requested top-level navigation finishes.
  • Tauri's with_webview runs on the main thread and supplies the platform webview handle. Tauri explicitly recommends pinning at least its minor version when this escape hatch is used; pin the exact Tauri version selected by the implementation.
  • Tauri/Wry do not yet expose a portable Rust screenshot operation; Wry's cross-platform screenshot request remains open. kernal-api therefore owns a private platform adapter rather than exposing Wry/Tauri types or waiting on that API.
  • Windows WebView2 exposes asynchronous ICoreWebView2::CapturePreview, including PNG output.
  • macOS WKWebView exposes asynchronous takeSnapshot.
  • Linux WebKitGTK exposes asynchronous webkit_web_view_get_snapshot, and Tauri's PlatformWebview::inner() provides the WebKitGTK handle on Linux.
  • Tauri's Linux CI guidance uses WebKitGTK 4.1 and xvfb for a virtual display. See the official prerequisites and CI guide.

Implementation

Example layout and dependency boundary

  • Add a feature-gated native example package under examples/wasm-tauri-screenshot/ and a small Rust sketch package beneath it. The exact package split is private implementation detail; do not add another public facade crate solely for the example.
  • The native example depends on kernal-api in the application-to-facade direction. kernal-api may privately own the Wasm runtime and Tauri adapters; it must never depend on the example.
  • Compile the guest through Soldr on Rust 1.95.0 to the same validated threaded target selected by feat(wasm): prove a streamed kernal-api Rust sketch runtime with zccache and extension2 cores #13 (currently wasm32-wasip1-threads) and load exactly that produced module. Do not substitute a native implementation in tests.
  • Keep Wasmtime, fp-bindgen/generated support, Tauri, Wry, WebView2, WebKitGTK, Objective-C, Tokio, and OS backend types private.
  • Gate the engine and webview implementation behind explicit heavyweight features. default = [] must remain a useful async process/host HAL and must not resolve the Tauri or Wasm engine dependency trees.
  • Pin Tauri exactly. If screenshot access requires direct platform crates, pin versions compatible with that Tauri release so a minor update cannot silently change native handle types.

Async guest contract

Expose facade-owned semantic operations equivalent to the following; final naming may follow the generated guest API conventions established by #13:

async fn run(task: ScreenshotTask) -> Result<(), ScreenshotTaskError>;
async fn open_webview(request: OpenWebview) -> Result<WebviewHandle, WebviewError>;
async fn wait_until_loaded(view: WebviewHandle) -> Result<LoadedPage, WebviewError>;
async fn sleep(duration: Duration) -> Result<(), ClockError>;
async fn capture_visible_png(view: WebviewHandle) -> Result<SnapshotHandle, WebviewError>;
async fn write_snapshot(snapshot: SnapshotHandle, output: OutputFile) -> Result<(), IoError>;
async fn close_webview(view: WebviewHandle) -> Result<(), WebviewError>;
  • These are asynchronous Wasm imports/futures. Each operation must suspend and resume the guest without blocking the Tauri event loop or constructing another Tokio/kernel runtime.
  • Create, navigate, capture, and close on Tauri's event-loop/main thread. Native completion callbacks resolve the corresponding guest future.
  • Start the five-second delay only after the matching top-level PageLoadEvent::Finished; use the kernel monotonic clock, not std::thread::sleep in the guest or UI thread.
  • Bound page load and total execution with explicit timeouts. A timeout, trap, cancellation, or window close must wake the guest with a typed error, close the webview, revoke handles, and leave no live kernel resources.
  • Use generation-safe, instance-scoped handles. Reject stale and cross-instance handles deterministically.

Screenshot and output ownership

  • Capture only the visible page viewport, excluding OS window chrome, at the native device scale. A fixed logical viewport in the fixture makes the proof comparable without requiring byte-identical rendering across engines.
  • The guest receives an opaque SnapshotHandle, not Vec<u8> and not a platform image. Do not serialize the PNG as one fp-bindgen value or copy it wholesale through Wasm linear memory.
  • Enforce configured maximum pixel dimensions and encoded bytes. Back the snapshot by a quota-accounted host blob/resource, then write/copy it through the kernel's bounded async I/O path. The native platform API may materialize its result internally, but it must fail at the configured bound and cannot create an unaccounted allocation.
  • Parse and authorize the output path in the native CLI. Grant the guest one exact OutputFile capability rather than ambient filesystem access or the path string itself.
  • Write a sibling temporary file, flush/close it, then atomically replace the requested output on success. Remove the temporary file after any failure; never leave a partial PNG at the final path.

Untrusted remote content

  • Accept only http and https URLs. Permit loopback HTTP for the offline test fixture; reject file:, data:, custom schemes, and malformed URLs before creating a window.
  • Treat the loaded page as untrusted. Assign it no Tauri commands, plugins, capability files, IPC initialization, filesystem access, or bridge to the Wasm sketch. The page is rendered and captured only.
  • Reject popup/new-window requests and navigations to disallowed schemes. Redirects remain subject to the same URL policy.

Deterministic test fixture

Add an offline integration test that:

  1. Starts a loopback HTTP fixture with deterministic HTML, a fixed viewport-oriented color pattern, and a page-load completion marker.
  2. Builds/uses the actual Rust Wasm sketch and launches the native Tauri runner as a child process so the GUI event loop owns its required thread.
  3. Supplies the fixture URL and a temporary .png output path.
  4. Records host-side timestamps for the matching load-finished event and capture request.
  5. Decodes the result with the existing Rust PNG tooling and checks format, nonzero dimensions, and tolerant color-region assertions rather than a cross-engine pixel-perfect hash.
  6. Confirms the process exits, the window closes, the Wasm instance tears down, and all kernel handles/resources return to zero.

The test must initially fail because no async webview/snapshot ABI exists, then pass through the real generated ABI. Do not satisfy it with a mock runtime, JavaScript screenshot library, WebDriver screenshot, OS desktop capture, direct Tauri call from the fixture/test, or a native copy of the sketch logic.

Build and CI

  • Document one Soldr command sequence that builds the guest module, builds the feature-gated native runner, and runs the focused integration test. Every Rust toolchain invocation in scripts and CI goes through soldr.
  • Add a focused CI job on the repository's supported Linux, macOS, and Windows architecture matrix. Install Tauri's documented platform prerequisites; run Linux under xvfb with WebKitGTK 4.1.
  • Keep the ordinary default = [] and each-feature lanes independent of GUI setup. Only the focused Tauri/Wasm job installs or compiles heavyweight webview/runtime facilities.
  • Upload the produced PNG and structured timing/resource log on failure for diagnosis.

Acceptance criteria

  • RED: a checked-in focused test demonstrates that a Wasm sketch cannot currently open, await, capture, and close a Tauri webview through kernal-api.
  • RED: document that Tauri/Wry lacks one portable screenshot call and identify the three native async APIs used by the private adapter.
  • GREEN: Rust 1.95.0/Soldr compiles the example sketch into one validated Wasm module using only the versioned kernel ABI plus the owned restricted-stdlib imports approved by feat(wasm): prove a streamed kernal-api Rust sketch runtime with zccache and extension2 cores #13.
  • GREEN: import validation rejects ambient WASI and any direct Tauri, filesystem, network, clock, or platform import before instantiation.
  • GREEN: kernal-api-wasm-tauri --url <fixture> --output <path> launches the real Tauri webview, waits for the requested top-level load, and requests capture no earlier than 5.0 seconds afterward.
  • GREEN: the command atomically writes a decodable PNG of the visible webview viewport to the exact CLI output path, closes the app, and exits zero.
  • GREEN: instrumentation proves every guest step crossed the generated async kernel ABI and suspended/resumed without blocking Tauri's event loop or creating another kernel runtime.
  • GREEN: PNG bytes never cross the ABI as one serialized value; snapshot storage is opaque, quota-accounted, bounded, and written through the kernel's async resource/I/O path.
  • GREEN: invalid schemes, disallowed redirects, load timeout, capture failure, unwritable output, guest trap, and cancellation return typed errors without a partial final file, leaked window, task, thread, or resource.
  • GREEN: the remote page has no usable Tauri IPC/plugin/capability surface and cannot access the guest or output capability.
  • GREEN: the deterministic offline integration test passes with real native webviews on the supported Linux, macOS, and Windows x86-64/ARM64 CI matrix; Linux runs under xvfb/WebKitGTK 4.1.
  • GREEN: soldr cargo test --locked --no-default-features still passes without resolving or compiling Tauri, Wry, Wasmtime, fp-bindgen, or platform screenshot dependencies.
  • Focused lint, format, build, and test commands are documented and pass; the failure artifact includes the screenshot and timing/resource trace.

Decisions

Related

Activity

  1. added
    enhancement
    on Aug 27, 2026
  2. zackees commented on Sep 16, 2026

    @zackees
    CollaboratorAuthor

    Implemented; closing with the evidence, checked against the artifacts rather than the issue's own status.

    kernal-api — examples/wasm-tauri-screenshot/runner.rs implements precisely the CLI this issue specifies: usage: kernal-api-wasm-tauri --url <http(s)-url> --output <png> [--module <built-wasm>] [--worker <native-worker>], with --url and --output required and the output path granted rather than assumed. tests/wasm_tauri_screenshot.rs carries the capture and containment proofs.

    The screenshot-cli-guest-build and tauri-webview-smoke lanes run it.

    These four are a dependency chain (#15 → #26 → #32, with #14 as the end-to-end proof), and the chain is present end to end, which is why they close together rather than one at a time.

    🤖 Generated with Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions