0213. Process limits as an attenuation
- Status: Accepted
- Date: 2026-11-09
- Plan: M14.2 (
plans/harness2/PLAN.md,PLAN.md); consumed by M14.12, M14.13, M14.20 - Review: spec drafted and reviewed 2026-11-09; owner decisions recorded below
Context
Tool and check processes must not hang or flood their caller.
The native pump already has a combined stdout/stderr cap and a
deadline (hrt_proc_run_impl,
plugins/cli/native/hrt_proc.h). On breach it kills,
reaps and returns EFBIG or ETIMEDOUT, never partial success. The
interpreter has the same pair
(crates/harness/src/interp_process.rs). Ordinary
run, stream, sandboxed runs and
run_input on both platforms pass
SIZE_MAX, 0. cli.call_plugin is the
exception: it already uses manifest
max_output/timeout_ms and the
payload/group path.
The two backends disagree today in four ways:
- Native creates a process group and kills it only for
run_input.runandstreamkill just the direct child. The interpreter'srun_inputalways kills the group. Its unsandboxedrunandstreamare unbounded, and the spawn path differs by OS: Linux usesCommand, while macOS uses the direct-execsubprocessbackend required by ADR-0087. Sandboxed interpreterrunis also unbounded today. - EFBIG is 27 on Linux and macOS, but ETIMEDOUT is 110 on Linux
and 60 on macOS. Under the shared lowering both fall into
Io("spawn <cmd>: os error N"). That text is OS-dependent, and "spawn" is wrong because the spawn succeeded. - An app-scope lease can also fail with ETIMEDOUT before spawn. The interpreter reports the same failure as EINVAL, because its lease error has no raw errno and dispatch defaults to 22.
Process.sandboxcopies the parent's fields only when the parent was already sandboxed. Policy deduplication hashes and compares an explicit field list.
The limits here are separate from three existing features.
Process plugins use manifest
max_output/timeout_ms (ADR-0099).
Platform [policy] uses fuel, heap, output and
deadline_ms for the program itself (ADR-0079,
ADR-0098). harness test uses its own per-case
supervision (ADR-0172).
Decision
API. Add
Process.limit(max_output: i64, timeout_ms: i64) -> Processto thecliandagentplatforms. It is an attenuation likesandbox(ADR-0095): it constructs policy, launches nothing, and is classifiedauthority.narrow. Both arguments must be ≥ 1. A literal below 1 is a new diagnostic E0818, "process limit must be positive", taken from the reserved M10 capability block (help: "use a limit of at least 1, e.g.limit(1048576, 10000)"). A run-time value below 1 does not trap. If the parent is not already denied, the handle is denied with EINVAL, and launches from it returnIo("spawn <cmd>: os error 22")without spawning. If the parent is already denied,limitpreserves that denial, so the launch error follows the preserved code (for example a sandboxDenied(...), not necessarilyIo22). This matches pluginlimit < 0and sandbox widening, which also yields a denied handle.Composition. The limit fields stack per field:
new = old unset ? n : min(old, n), the same formula asPar.limit. 0 in a stored field means unset, which is unambiguous because user values must be ≥ 1. Limits can be tightened but never loosened, and no API removes them.sandboxcopies the parent's limit fields unconditionally, andlimitcopies the parent's sandbox, net, root, read-only and denied fields. Both orders therefore give the same effective policy for ordinary valid values. The capability hash and equality include the new fields.Denied-policy precedence. The first denial always wins (fail closed).
limitpreserves an existing parent denial.sandboxwidening never replaces an existing denial: if the parent is already denied, a later widening failure (code 10008) is not written over it. Scope order-equivalence oflimitandsandboxincludes this rule.Scope. The limits apply to
run,run_inputand, oncli,call_plugin, where the effective bound is the per-field minimum of the manifest and the handle.streamon a limited handle fails with EINVAL before flushing and before spawning. Inherited descriptors cannot be measured, and grouping an interactive child would make it a background job on the terminal.Process group and breach. Every limited launch, sandboxed or not, is spawned in its own process group (
setpgid/posix_spawnattr_setpgroup/process_group(0)) on Linux and macOS. Underharness test, the group is the app-scope lease (ADR-0172); otherwise the PGID equals the child pid. A zombie holds that PGID until the direct child is reaped. Exception: the macOS limited unsandboxed interpreter path uses the ADR-0087subprocessbackend (Exec::setpgid(), i.e.setpgid(0, 0)), so its PGID equals the child pid even underharness test.subprocesscannot join an existing lease PGID withoutposix_spawnporunsafepre_exec; either would undo ADR-0087 or the no-unsafe rule. Joining the lease through a safe direct-exec helper is a follow-up. Sandboxed macOS limited runs, Linux limited runs, and native C still take the lease. Breach kill of that child and its descendants still works (killpgon the child pid plusJob::kill).On cap or deadline breach the runtime sends SIGKILL to the whole process group (
killpg) with no SIGTERM grace, then also signals the direct child (kill(pid, SIGKILL)/Child::kill/Job::kill) so a child that left the group cannot make the blocking reap hang. It then reaps only the direct child. Grandchildren are killed via the process group and reaped by init; the runtime does not reap them. The plan wording is therefore "the whole process group is killed; the direct child is reaped", not "grandchildren are reaped".Always
killpgbefore anywait/try_wait/waitpidthat could reap the direct child. Neverkillpgafter the child has been reaped: that would close the interpreter's PGID-reuse window. Ignore ESRCH fromkillpg. Any other kill error is returned asIoafter the direct child has been reaped. Safe Rust has nokillpg; the interpreter keeps using/bin/kill -KILL -- -<pgid>as the equivalent, but must invoke it before any reap.The cap counts stdout and stderr bytes the parent reads; exactly
max_outputbytes succeed; stdin input is not counted. No partial output is returned. A descendant that callssetsid,setpgidordaemonescapes the kill (supervision, not containment; ADR-0172). On success, descendants that closed their stdio keep running.Deadline.
timeout_msis monotonic elapsed time starting at successful spawn. It covers output pumping and waiting for exit. Spawn setup (argument conversion, the app-scope lease, sandbox handshake, plugin preparation) does not count against it. Post-kill cleanup and reaping also do not count against it. After SIGKILL the runtime blocks inwaitpid/waituntil the direct child is reaped. That wait can only hang if the child is in uninterruptible sleep, the same as before these limits; the runtime never abandons a direct child it owns.Errors. The kind stays
CliError.Io; no new type or variant is added. A breach is detected where it happens and passed as a private code at the language boundary only: 10011 for the output cap, 10012 for the deadline. The raw EFBIG/ETIMEDOUT values thathrt_proc_run_inputandinterp_process::run_inputreturn to their existing callers do not change. The shared lowering adds two fixed arms forrun/run_input:Io("output limit exceeded: <cmd>")andIo("timed out: <cmd>"). These are byte-identical across backends and operating systems. Emitting them requires explicit lowering categories, not only new table rows: codes 10011/10012 must keepargs[0], apply to non-plugin calls withoutProcessSandbox, and be gated onProcessLimit.call_pluginkeeps its existing mapping. Do not use the wording "deadline exceeded" (that is the program deadline trap, ADR-0098).Unsandboxed
denied. After this change, every launch path checkscap.deniedfirst, including unsandboxedrunandstream. Today onlyrun_inputdoes.
Alternatives considered
- Extra
run/run_inputparameters. Every call site changes, and a callee given aProcesscould drop the bound. As an attenuation, the limit travels with the authority. - Keep the existing fallback
Io("spawn <cmd>: os error N"). Goldens would need per-OS output for ETIMEDOUT. The text says "spawn" after a successful spawn, and it cannot separate a deadline from a lease timeout. - Map the raw errno in lowering. ETIMEDOUT's value depends on the target, so the IR constants would become target-specific.
- Trap on non-positive dynamic values (the
Par.limitbehaviour). This needs a newTrapKindin the app-test trap wire table, and it kills the whole app over a bad config value. The plan keeps traps for our own bugs. - Zero means unlimited. This conflicts with min-stacking and with plugin manifests, which require positive limits. Positive values let zero stay the internal "unset" marker.
- SIGTERM, a grace period, then SIGKILL. This adds a second timer and lets a child stretch the bound. The native and interpreter pumps already send SIGKILL directly.
PR_SET_CHILD_SUBREAPERor cgroups to reap descendants ourselves. The first is Linux-only and changes orphan handling for the whole process; the second is Linux-only and privileged. Group kill works on both supported OSes. Init reaps orphaned grandchildren.- Apply the deadline to
streamwithout a group. Descendants would survive a breach. - Measure the deadline from public method entry. Existing helpers do not interrupt every setup wait (lease, sandbox handshake, plugin preparation). Starting at successful spawn matches what can actually be enforced without threading remaining time through those phases.
Consequences
cli, agent and both backends gain one
method, two platform.toml entries (sorted after
call_plugin and before run), and one IR
op with arity 3, all i64 with the receiver last, as
ProcessSandbox does. They also gain E0818, two
private error codes, and edits to LANGUAGE.md and
diagnostics.md. The plugin DOCS.md files must stay
within 1,500 tokens.
Limited run stops using the handle-free
unsandboxed path. Unlimited handles behave exactly as before.
Unsandboxed run paths must now honour cap.denied.
Limited children run in their own process group, which is
already true for run_input. Interpreter limited runs
must keep macOS on the ADR-0087 direct-exec path; they must not
switch that OS to Command::spawn. A child that reads
/dev/tty stops until the deadline.
Breach detection on the cap is deterministic. Deadline outcomes near the bound are not (ADR-0098). Tests assert bounded termination and use wide margins. The grandchild test asserts the descendant was killed, not that the runtime reaped it.
M14.12, M14.13 and M14.20 can then bound tools, receipts and hidden checks. M14.3 still owns macOS containment.