Skip to content

feat: add experimental child sandboxes for sub-agents (RFC 0021) - #5641

Draft
patkraw wants to merge 2 commits into
ai-openshell:mainfrom
patkraw:feat/4574-child-sandboxes/patkraw
Draft

patkraw wants to merge 2 commits into
ai-openshell:mainfrom
patkraw:feat/4574-child-sandboxes/patkraw

Conversation

@patkraw

@patkraw patkraw commented Nov 17, 2026 •

Copy link
Copy Markdown

Summary

Adds child sandboxes: a root sandbox can have depth-1 child sandboxes that use its policy and a subset of its providers, so an agent can run sub-agents in their own sandboxes. This is the implementation of RFC 0021 "Sandboxed Subagents", opened as a draft so the team can try the feature while the RFC is in review. It is off unless the operator sets experimental_enable_child_sandboxes.

Size note: GitHub overstates the removed lines. crates/openshell-server/src/grpc/sandbox.rs really has about 5,240 added and 98 removed (git diff --diff-algorithm=patience), and more than half of the added lines in this PR are tests.

Related Issue

Closes #4574

The RFC pull request (#4721) also references #4574. If maintainers prefer a separate implementation issue, I will re-link this PR.

Changes

  • Gateway settings and data model: experimental_enable_child_sandboxes (default false) and max_concurrent_children (default 10) in gateway.toml; read-only Sandbox.parent and Sandbox.created_by; CreateSandboxRequest.parent. Records without a parent read as roots, so there is no migration.
  • Creating a child: only a root can be a parent, the request carries no policy, providers must be the parent's, and each parent has a child limit. The parent is locked for the checks and the insert, so concurrent creates cannot exceed the limit, also across replicas on PostgreSQL. Deleting a parent that has children is refused.
  • Policy and provider sync: a child gets a copy of the policy its parent enforces, and the gateway copies each new parent version into its children. Every configuration build for a child checks the parent's live record and refuses the child while the parent is missing, blocked, or ahead of the child's copy, so a failed sync fails closed. A provider the parent loses is removed from its children. A child's own policy writes and proposals are refused, and children always fail closed.
  • CLI: sandbox create --parent <root> and sandbox list --parent <root>; sandbox list shows a PARENT column when a page holds a child.
  • MCP for agents: the supervisor serves http://openshell.local/mcp inside each sandbox and forwards it to the gateway's new /mcp endpoint with the sandbox's session token; the agent holds no credential. Five tools (create_sandbox, list, exec, delete, logs) each run as the existing RPC through the normal gRPC stack, so authentication, authorization, rate limits and interceptors apply. A sandbox caller reaches only these methods, only through /mcp, and only for its own children.
  • Long work: exec runs in the foreground for at most 45 seconds. Its description tells the caller to start longer work detached in the sandbox and check on it with later exec calls. The gateway keeps no job state.
  • MCP for users: /mcp accepts user callers, and openshell mcp serve is a stdio bridge that reuses the CLI login and connection.
  • Templates for agents: an agent can create a child from a workload template labelled openshell.ai/agent-usable=true; it cannot name an image.
  • Audit: each MCP tool call is one OCSF API Activity event with the caller, the user it acts for, the outcome and the main arguments.
  • Docs: docs/how-it-works/sandboxes/child-sandboxes.mdx, the gateway configuration reference, the templates page and the CLI skill.
  • Dev tooling: OPENSHELL_EXPERIMENTAL_CHILD_SANDBOXES=1 mise run gateway:docker turns the feature on in the dev gateway.

How to try it

  1. Check out docs/4574-child-sandboxes-examples/patkraw from patkraw/OpenShell. It is this PR's first commit plus tutorials and runnable scenarios, which are not part of this PR.
  2. Follow examples/child-sandboxes/tutorials, starting with 00-setup.md. Seven short tutorials cover the CLI, MCP from inside a root, background jobs, templates, policy and provider sync, and MCP from the host. The tutorials branch still has the earlier wait tool; its background-jobs tutorial does not match this PR until it is updated.
  3. On a gateway other than the dev gateway, set experimental_enable_child_sandboxes = true under [openshell.gateway] and restart it.
  4. On a gateway with an admin role configured, the user who creates a root must be a workspace member for its agent to create children: openshell workspace member add --workspace <ws> --subject <user> --role user.

Design notes: long-running work

This PR ships the "plain" approach: no wait tool and no background argument. The caller starts long work detached with setsid, writes output and the exit code to files, and reads them with later exec calls. An earlier version of this PR had gateway-managed jobs ("managed"); the second commit removes them, about 900 lines, so the gateway keeps no job state and runs no job script in a child.

Two experiments with lead agents driving child sandboxes compared the approaches. Both reports are on the tutorials branch.

  • First experiment (63 runs, three models, three tasks): managed completed 18 of 18 runs on the hard tasks, plain 15 of 18, and a variant that returns a process id ("pid") 16 of 18. The plain failures were about waiting: one model ended its turn while the work was still running, and another polled in a loop of up to 183 tool calls.
  • Second experiment (342 runs, five models, in Claude Code and Pi): Claude Haiku 5.5 and DeepSeek V4.1 Flash completed all 84 main runs with every approach. The approaches differed in effort: a median of 14.5 tool calls for managed and 17 for plain with the longer tool text, and 11 for plain with the short text this PR ships.

The exec description is the short text from the experiments. docs/how-it-works/sandboxes/child-sandboxes.mdx has the full steps, which weaker models may need in their instructions. A skill with those steps is a possible follow-up.

To do before merge

  • Test child sandboxes with Cedar policies. Cedar support landed on main for development builds after this PR was opened. A child gets a copy of its parent's policy, and that path has not been run with a parent that uses a Cedar policy.

Known limits

  • One level, parent's exact policy. A child cannot have children or a narrower policy, so a parent must hold every permission its children need.
  • Revocation is not instant. A parent policy change reaches a child within its next configuration rebuild, at most about 30 seconds; a removed provider was measured at about 15 seconds.
  • A stopped parent cannot revoke. The sync and the delivery check follow the policy the parent has loaded, so a policy change on a stopped parent reaches its running children only when it starts again.
  • Children on a different image. A child keeps its parent's filesystem paths, program paths and process user, so a template's image must share the parent image's layout. Missing paths only remove access.
  • Timed-out commands keep running. A foreground exec that reaches its timeout is reported as timed out but keeps running until the child is deleted (existing ExecSandbox behaviour, ExecSandbox processes keep running after execution_timeout and after the client disconnects #5042). Detached work depends on a process outliving its exec; a future cleanup of exec'd processes needs a detach option first.
  • Long work is the caller's job. The gateway does not track detached commands: nothing stops a second long command in the same child, and a detached command that is killed leaves no exit code, so the caller must check with ps.
  • Workspace scans. A parent's children are found by listing its workspace: on child create, on delete of a root, and after each root policy load, including on gateways with the feature off. An index by parent is future work.
  • Creator check. A root creates children only while its creator is a workspace member; a platform admin by role must also be a member. Gateways with no admin role configured skip the check.
  • Orphans are stopped by name after an id check, which leaves a small window if the name is reused.
  • The door uses the sandbox's session token, not a token limited to /mcp.
  • Tested on Docker with one gateway. Not yet exercised on Kubernetes, Podman, VM drivers or several replicas. On Kubernetes, a child created without a template does not copy its root's template labels or annotations; whether any isolation depends on those is unverified.

Testing

Run on a macOS arm64 development machine, on both commits rebased onto main at 152c59d31:

  • cargo clippy --workspace --all-targets -- -D warnings and the same for e2e/rust: clean. cargo fmt --all -- --check: clean.
  • mise run e2e:rust:child-sandboxes (Docker gateway): 6 of 6 passed. One test starts a detached command through MCP and collects its result.
  • cargo test for openshell-server, openshell-cli, openshell-sdk, openshell-supervisor-network, openshell-core and openshell-gateway-interceptors: openshell-server passes. The failures are tests that also fail on main on this machine: 4 in openshell-core (e2fsprogs tool checks that time out), 8 in openshell-supervisor-network proxy tests, the openshell-cli sandbox-create tests that wait on helper processes, and three provider_readiness_update_polls_later_targets_* timing tests that are intermittent on main.
  • The seven tutorials were run end to end against a Docker dev gateway on an earlier version of the first commit, before the wait tool was removed.
  • A manual check on a Docker gateway, on that earlier version, confirmed the documented timeout behaviour and that a detached command survives its exec.

Not run: Linux-only tests (Landlock, seccomp), the Podman, Kubernetes and VM lanes, and the PostgreSQL multi-replica tests. CI is expected to cover the first.

  • Checks appropriate to the affected code and behavior pass
  • Unit tests added/updated (if applicable)
  • E2E tests added/updated (if applicable)

Checklist

  • Follows Conventional Commits
  • Commits are signed off (DCO)
  • Architecture docs updated (if applicable)

@copy-pr-bot

copy-pr-bot Bot commented Nov 17, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

A root sandbox can have depth-1 child sandboxes that share its policy and a
subset of its providers. Users create them with sandbox create --parent;
agents in a root manage them through MCP tools at openshell.local/mcp; host
users reach the same tools through openshell mcp serve. Off by default
(experimental_enable_child_sandboxes).

Signed-off-by: patkraw <pkrawczuk@nvidia.com>
… exec

Remove the wait tool, the background argument of exec, and the jobs module
that ran them. The exec tool's description now tells the caller to start long
work detached in the sandbox and check on it with later exec calls. The
gateway keeps no job state and runs no job script in a child.

The e2e test starts a detached command and collects its result. The docs
page describes the steps in full.

Signed-off-by: patkraw <pkrawczuk@nvidia.com>
@patkraw
patkraw force-pushed the feat/4574-child-sandboxes/patkraw branch from 9b4334a to 5845890 Compare November 24, 2026 03:18

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

state:accepted A maintainer decided OpenShell should pursue this issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(gateway): let an agent in a sandbox create child sandboxes within a user-set grant (gateway MCP + API)

1 participant