ADR-0296:
First-Class Virtualized Platform Capabilities
(test_caps)
- Status: Accepted
- Date: 2026-11-28
- Related: ADR-0152 (Out-of-process FFI),
ADR-0269 (In-language
testblocks), ADR-0284 (Platform ergonomics & multi-root sandbox)
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):
- Clock: monotonic
clock_msinitialized to0, mutated only viaset_ms/advance_clock_ms(delta). - Args & Env: in-memory argument vector and
environment map replacing
argc/argvandenviron. - Stdin / Stdout / Stderr: in-memory byte
buffers. Virtual stdin pre-populates reader slot 0
(
eof = true,fd = -1), so all streaming readers (read_all,read_text,read_some,lines,byte_lines,byte_lines_max,chunks) work without syscalls. Stdout and stderr writes/prints append to capture buffers (capture_stdout_text()/capture_stderr_text()). - Fs: in-memory tree of
File,Dir, andSymlinkentries with exact attenuation parity:Fs.restrict(root)canonicalizes against the virtual tree and rejects..escapes with errno10001(CliError.Denied("outside root: ...")).Fs.read_only()rejects writes/mkdir/append/truncate/sync with errno10002(CliError.Denied("read-only filesystem: ...")).Fs.no_follow()rejects symlink traversal/reads with errno10013(CliError.Denied("no-follow filesystem: ...")) and omits symlinks fromlist/walk.- Fault injection:
deny_path(path)(errno13/CliError.Denied) andfail_after_writes(n, CliError.Io("ENOSPC"))(errno28, classified inlower_platform/mod.rsasCliError.Io("ENOSPC: ...")).
- Process: scripted/canned
(cmd, args) -> (status, stdout, stderr, os_error)registry with wildcard arg matching,max_outputenforcement (10011), and deterministic timeout simulation (10012->CliError.Io("timed out: ...")). - Net: in-memory loopback listeners + paired
bidirectional connections AND stubbed
Net.connect(addr)fixtures with write capture, enforcingNet.none()andNet.restrict(hosts)grants identically.
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
- Effectful functions taking
cli: Cli,fs: Fs,process: Process, ornet: Netcan be tested deterministically in< 1 mswith zero syscalls on both C andinterp. lower_platform/mod.rsmaps errno28(ENOSPC) toCliError.Io("ENOSPC: ...")on both backends, andlower_platform/process.rsalways classifies process limit/timeout errors (10011,10012) even in helpers that receive an already-attenuated or virtualizedProcesshandle.- Tests that activate virtual mode must call
deactivate_virtual()before returning from atestblock so thetenon testharness printsok <name>to real stdout.