On this page

ADR-0296: First-Class Virtualized Platform Capabilities (test_caps)

Context

Tenon enforces capability-based authority: effectful functions accept explicit capability handles (cli: Cli, fs: Fs, clock: Clock, env: Env, stdin: Stdin, stdout: Stdout, stderr: Stderr, net: Net, process: Process) rather than reaching into ambient OS globals. While this guarantees static auditability via --report=authority, testing effectful code previously required real temporary directories, real OS processes, wall-clock delays, and loopback TCP sockets — making tests slow, flaky, and unable to deterministically inject faults such as ENOSPC, path denials, or subprocess timeouts.

Decision

We introduce deterministic, zero-syscall in-memory virtualization for all CLI platform capabilities across both the native C runtime (plugins/cli/native/) and the reference interpreter (crates/tenon-compiler/src/interp/), paired with an ergonomic library plugin plugins/test_caps/.

1. Unified Virtual Platform State (trt_vcap / VirtualCaps)

Both backends implement identical in-memory state (trt_vcap_state in plugins/cli/native/trt_cli.h and VirtualCaps in crates/tenon-compiler/src/interp/virtual_caps.rs), gated by a single active flag (default false, imposing a single predicted branch and zero allocations on production paths):

2. Zero-Subprocess Interpreter Dispatch

In tenon interp (crates/tenon-compiler/src/interp/eval.rs and mod.rs), extern symbols prefixed with trt_vcap_ are intercepted directly in-process and dispatched to Machine::eval_vcap. Programs importing test_caps never compile or spawn an out-of-process ffi-host in the interpreter, achieving sub-millisecond deterministic execution.

3. Ergonomic plugins/test_caps/ Surface

plugins/test_caps/ exports VirtualCli, VirtualFs, VirtualClock, VirtualProcess, VirtualNet, VirtualListener, VirtualConn, and fault-injection helpers (fail_after_writes, advance_clock_ms, deny_path, capture_stdout_text, capture_stderr_text) backed by native/tz_test_caps.h.

4. Authority Soundness

Production main(cli: Cli) binaries cannot construct Cli / Fs / Process / Net / Clock / Env values (E0810 remains unchanged), and test_caps requires explicit capabilities = ["ffi"] opt-in in tenon.toml. When test_caps is not linked, trt_vcap.active is always 0 and --report=authority remains 100% sound.

Consequences