On this page

0213. Process limits as an attenuation

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:

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

  1. API. Add Process.limit(max_output: i64, timeout_ms: i64) -> Process to the cli and agent platforms. It is an attenuation like sandbox (ADR-0095): it constructs policy, launches nothing, and is classified authority.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 return Io("spawn <cmd>: os error 22") without spawning. If the parent is already denied, limit preserves that denial, so the launch error follows the preserved code (for example a sandbox Denied(...), not necessarily Io 22). This matches plugin limit < 0 and sandbox widening, which also yields a denied handle.

  2. Composition. The limit fields stack per field: new = old unset ? n : min(old, n), the same formula as Par.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. sandbox copies the parent's limit fields unconditionally, and limit copies 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.

  3. Denied-policy precedence. The first denial always wins (fail closed). limit preserves an existing parent denial. sandbox widening 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 of limit and sandbox includes this rule.

  4. Scope. The limits apply to run, run_input and, on cli, call_plugin, where the effective bound is the per-field minimum of the manifest and the handle. stream on 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.

  5. 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. Under harness 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-0087 subprocess backend (Exec::setpgid(), i.e. setpgid(0, 0)), so its PGID equals the child pid even under harness test. subprocess cannot join an existing lease PGID without posix_spawnp or unsafe pre_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 (killpg on the child pid plus Job::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 killpg before any wait / try_wait / waitpid that could reap the direct child. Never killpg after the child has been reaped: that would close the interpreter's PGID-reuse window. Ignore ESRCH from killpg. Any other kill error is returned as Io after the direct child has been reaped. Safe Rust has no killpg; 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_output bytes succeed; stdin input is not counted. No partial output is returned. A descendant that calls setsid, setpgid or daemon escapes the kill (supervision, not containment; ADR-0172). On success, descendants that closed their stdio keep running.

  6. Deadline. timeout_ms is 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 in waitpid/wait until 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.

  7. 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 that hrt_proc_run_input and interp_process::run_input return to their existing callers do not change. The shared lowering adds two fixed arms for run/run_input: Io("output limit exceeded: <cmd>") and Io("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 keep args[0], apply to non-plugin calls without ProcessSandbox, and be gated on ProcessLimit. call_plugin keeps its existing mapping. Do not use the wording "deadline exceeded" (that is the program deadline trap, ADR-0098).

  8. Unsandboxed denied. After this change, every launch path checks cap.denied first, including unsandboxed run and stream. Today only run_input does.

Alternatives considered

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.