On this page

0277. Per-agent trap domains

Context

Every trap (LANGUAGE.md "Runtime traps": overflow, bounds, fuel exhausted, deadline exceeded, io error, file closed, out of memory) ends in trt_trap (runtime/tenon_rt.h): flush stdout, print trap: <kind> at <file>:<line>:<col>, exit(101). The interpreter propagates Stop::Trap(kind, pos) to run_prepared* and the driver maps it to 101. One bad agent step, TUI widget or procman job therefore kills every agent, the UI and the daemon. The C runtime already has half the mechanism: under TN_PAR a thread-local trt_worker_trap (jmp_buf + kind/site) lets trt_par_execute (runtime/trt_par.h) catch a worker trap, drop the worker arenas and re-raise the lowest-index trap after join (ADR-0102). Values are immutable (0007/0036), there is no global mutable state, and agent steps are pure Agent.step(self, event) -> Transition<Self> calls (0010/0107), so a trapped call has no partially updated value to repair; only arenas and in-flight effects need cleanup.

Decision

  1. Kernel mechanism. A trap domain is a thread-local stack of trt_trap_context (generalising trt_worker_trap). trt_trap longjmps to the innermost domain; with none open it keeps today's print-and-exit-101. The interpreter mirrors it: a domain frame catches Stop::Trap and restores heap marks. Both backends return the same value: struct Trapped { kind: str, file: str, line: i64, col: i64 }, kind using the exact spellings already in LANGUAGE.md.
  2. No new syntax (0006, 0009). Domains open only through platform methods (policy in plugins, 0004). The cli platform adds a Traps capability (cli.traps, ceiling traps): traps.contain(job) -> result<J, Trapped> for J: Contained, where interface Contained { fn run(self) -> Self } (a job is a value holding its input and, after run, its output). The agent platform's runner wraps each Agent.step in a domain and turns a trap into a per-task Trapped outcome. Surviving a trap is an authority: a library cannot swallow traps unless main handed it Traps (0050/0095). After ADR-0275 (D3) a contain(f, x) form over function values may replace Contained.
  3. Entry and unwind (C). setjmp lives only in one runtime helper, trt_domain_call(fn, env, out), shaped like trt_par_execute; generated function bodies never contain setjmp, so no generated local needs volatile and -O2 stays correct. The helper's own state is set before setjmp and not modified after it. On entry it records trt_mark of every scratch colour and of trt_persistent; on a trap it trt_resets to those marks, so the transition arena (0107) and anything promoted inside is discarded. Nothing allocated inside a trapped domain is reachable afterwards (its result is dropped).
  4. Effects in flight. The domain owns a resource ledger: File/Listener/Conn descriptors, capability slots and child process groups created inside it are closed or killed-and-reaped on unwind. Effects already performed stay done: bytes written to stdout (buffered or flushed), files written, requests sent. The trap does not flush or truncate stdout; a partial line may remain, and platforms print their own marker. Locks held by the runtime (trt_par_io_lock) are released as trt_par_execute does today.
  5. Nesting, workers, budgets. Domains nest; a trap goes to the innermost one. A data- parallel worker trap (0100/0102) is re-raised at join in the caller's thread and so lands in the caller's innermost domain. Fuel and deadline stay process ceilings (0017/0074): a domain may carry a tighter budget (traps.limit(fuel, deadline_ms) -> Traps, attenuation only); exhausting the domain budget traps into that domain, exhausting the process budget still ends the process once the domain returns.
  6. Not catchable. Native crashes in FFI code (0152), including SIGSEGV in a shim; in the interpreter a dead ffi-host child stays fatal, matching C. Stack overflow is out of scope (sigaltstack + guard-page recovery is not cheap or portable to get right; a separate ADR may add it). The SIGBUS mmap path (0055) never traps, so it is unaffected; no signal handler ever longjmps into a domain. cli.exit is not a trap and still exits.
  7. Journals and replay. The runner journals a trapped transition as a result record (kind + site). Replay re-executes deterministic kinds and requires the same kind and site (a mismatch is a replay divergence); deadline exceeded and io error are replayed from the record. Retry, restart and supervision are plugin policy: the agent runner defaults to no retry (deterministic traps repeat); procman and the TUI shell choose their own.

UX: one failing agent or widget degrades to a visible error, not a dead app or daemon. DX: a structured Trapped with the same site as today's message; no exceptions to learn. AX: no new grammar; one capability method, one result type, explicit authority.

Prior art

Alternatives considered

Consequences

Traps become values at a few platform-chosen boundaries; elsewhere behaviour is unchanged. LANGUAGE.md: +2 lines (~70 tokens) under "Runtime traps"; trim by moving trap kind list details to diagnostics.md. No new compile-time codes are needed; misuse of Traps outside a cli program is the existing capability error. JSON trap events gain contained: true. Frozen files: listed below; Maintainer approves them before slices 2-4.

Frozen files that would change

Implementation slices

  1. Runtime domain stack + trt_domain_call + arena mark/reset; port trt_par_execute onto it; unit tests on generated C text. Files: runtime/tenon_rt.h, trt_par.h, trt_mem.h, crates/tenon/src/cgen_par.rs. Frozen: no. Agent: Claude Opus.
  2. Traps capability, Contained, contain/limit in both backends; interp domain frame. Files: plugins/cli/*, crates/tenon/src/{lower_platform,cgen}.rs, interp.rs, interp/{heap,platform,trap_report}.rs, LANGUAGE.md. Frozen: yes. Agent: Claude Opus.
  3. Resource ledger for files, sockets, processes. Files: plugins/cli/native/trt_{cli,io, net,proc}.h, interp_{fs,net,process}.rs. Frozen: yes. Agent: Claude Sonnet.
  4. Agent runner per-step domain, Trapped outcome, journal + replay. Files: plugins/agent/{run,journal,recover}.tn, DOCS.md, native/trt_agent.h. Frozen: yes. Agent: Claude Opus.
  5. Policies: procman job restart on Trapped, TUI shell widget placeholder. Files: plugins/procman_core/*, plugins/tui_shell/*. Frozen: no. Agent: Claude Sonnet.

Open questions (resolved)

Resolved by maintainer on 2026-11-22: every question takes its stated default.

  1. Trapped.kind: stable str spellings or a closed TrapKind enum? Default: str now, enum when generated codecs need it.
  2. User-facing Traps.contain in slice 2, or runner-only until D3 function values land? Default: ship contain (procman and TUI need it).
  3. Agent runner retry on Trapped? Default: none; report to the parent as ChildDone.
  4. Stack overflow recovery: separate ADR later, or never? Default: separate ADR, deferred.