0297. Sandboxed Hot-Reloadable Script Plugins (M22.7–M22.10 Core)
- Status: Accepted
- Date: 2026-11-28
- Plan: Wave 1, Rank 10 (Idea #28); completes ADR-0280 slices
M22.7–M22.9 and ADR-0099
exec_hashintegration - Relates to: 0280, 0277, 0099
Context
ADR-0280 established the architecture for runtime script
plugins ([plugin] kind = "script"), including
manifest validation (slice 1) and the pure host primitives in
plugins/script_host/. Three runtime pieces remained
to make script plugins usable end-to-end:
- M22.7 (
script_serve.rs): a framed-stdio interpreter serve loop (tenon script-serve) with per-callTrapsdomain isolation so fuel exhaustion, deadline expiry, or runtime traps never crash the host or the plugin serve loop. - M22.8 (
plugin_kit.rs): script conformance validation,exec_hashenforcement vs--dev-pluginslive reload, and reusable host-interface extraction +ScriptPlugindispatcher and typed proxy codegen. - M22.9 (
plugins/script_host/): complete host-side protocol framing, ADR-0099exec_hashpinning,fswatch.coalesce-backed hot-reload watcher, andproc.lib-backed sandboxed session lifecycle with crash recovery and clean restart.
Decision
1.
Framed-Stdio Interpreter Serve Loop
(crates/tenon-compiler/src/interp/script_serve.rs)
- Wire framing: 4-byte little-endian length
prefix followed by UTF-8 JSON payload (
encode_frame,read_frame,write_frame), enforcingmax_frame_bytes(default 4 MiB). Oversized frames emit a deterministic error frame and terminate the session cleanly. - Operations:
{"op":"ping"}->{"ok":true,"status":"ready","exports":[...]}{"op":"init","fn":"<opt>","args":[...]}-> initializes stateful plugin state{"op":"handle","fn":"<opt>","payload":<json>,"fuel":<opt>,"deadline_ms":<opt>}-> statefulhandle(state, msg)dispatch; updates stored state atomically only on non-trapping return, preserving the last good state on any trap (ADR-0280 #5, ADR-0277).{"op":"call","fn":"<name>","args":[...],"fuel":<opt>,"deadline_ms":<opt>}-> direct typed function invocation over Core IR (i64,i32,u8,u32,u64,f64,bool,str,bytes,unit,vec,map,record,union).{"op":"shutdown"}->{"ok":true,"status":"bye"}and clean exit.- Unknown operations or functions return
{"ok":false,"status":"unsupported","error":"..."}without crashing the serve loop (ADR-0280 #6).
- Per-call
Trapsdomain isolation: Every call runs inside an isolatedTrapsdomain (domain_depth,domain_budgetswithfuel+Deadline::new(deadline_ms), and per-callheapcap). Per-request budgets are monotonically attenuated against host defaults (a plugin or request can only lower limits, never raise them). On anyStop::Trap,Stop::BudgetTrap,Stop::Depth, orStop::Exit, all domain resources (files, sockets, workspaces, capability slots) are unwound and a deterministic{"ok":false,"status":"trapped","trap":{...}}frame is written; the loop remains alive for subsequent calls. - Zero-FFI invariant:
serve_plugin_pathandserve_programrefuse any program declaringexternfunctions (E1170).
2.
Script Conformance, Dispatcher/Proxy Codegen & Hash Lock
(crates/tenon/src/plugin_kit.rs)
- Conformance
(
script_conformance):tenon plugin checkvalidates<host>.plugin/<major>API syntax and confirms zeroexterndeclarations across all package.tnsources (E1170). - Interface extraction & codegen:
extract_script_interface(E1171) extracts method signatures from any Tenoninterface;generate_script_dispatcheremits plugin-side dispatch withunsupported:fallback;generate_script_proxyemits a host-sideScriptPluginProxybound toscript_host.protocol. - ADR-0099
exec_hashenforcement:compute_script_tree_hashcomputes the canonicalsha256:<64-hex>digest over sortedpath\nlen\n<bytes>entries (matchingscript_host.lock.exec_hash).verify_exec_hashreturnsHashStatus::Verifiedon match,Err(E1172)on mismatch in production, andHashStatus::ReloadAllowedwhen--dev-pluginsis enabled. - CLI handler:
plugin_kit::script_serve_commandimplementstenon script-serve <plugin-path> [--fuel N] [--deadline-ms N] [--max-frame N] [--heap-bytes N], reporting fatal serve failures withE1174.
3. Complete
plugins/script_host/ Runtime Library
script_host.lock: addsLockDecision,exec_hash,parse_exec_hash,verify_exec_hash.script_host.protocol:Limits,attenuate,ScriptProxy,TrapInfo,CallReply,encode_call,encode_handle,encode_init,encode_ping,encode_shutdown,decode_reply.script_host.reload: integratesfswatch.coalesce(ReloadMode.Prod | DevPlugins,ReloadWatcher,record_tree,on_events,poll_reload). InDevPlugins, debounced file edits triggershould_reload = truewith incrementedgeneration; inProd, hash drift is fail-closed (refused_reason).script_host.session:SessionConfig,resolve_script_bin,build_spawn_argv,build_spawn_env,new_session,verify_start,on_crash,on_reload, plus liveproc.liboperations (start,stop,restart,call_raw) with clean shutdown, crash policy enforcement (Restart->Disable), andTrapped { kind: "plugin exited" }on EOF or timeout.
4. Reserved Diagnostic Codes
E1170: Script plugin conformance failure (invalidapior forbiddenexternFFI).E1171: Invalid host API or missing interface during script dispatcher/proxy extraction.E1172:exec_hashmismatch in production mode (pass--dev-pluginsfor live reload).E1174: Fataltenon script-servesession error.
Consequences
- Hosts can execute untrusted
.tnscript plugins in isolated child processes with deterministic JSON framing, per-call fuel/deadline/heap traps, and zero C-compiler dependency. - Production deployments enforce ADR-0099
exec_hashpinning by default, while--dev-pluginsenables debouncedfswatch-driven hot reload without restarting the host application. - Zero external Cargo crates or
unsafeblocks are introduced.