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 <[email protected]>
… 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 <[email protected]>
@patkraw
patkraw force-pushed the feat/4574-child-sandboxes/patkraw branch from 9b4334a to 5845890 Compare November 24, 2026 03:18

@pimlock pimlock left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Design-level pass on the parts I'm confident about so far; more to follow on the policy sync model (copy timing, revocation while the parent is stopped) and on the per-root opt-in.


if let Some(enabled) = file
.as_ref()
.and_then(|f| f.openshell.gateway.experimental_enable_child_sandboxes)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider requiring config push when child sandboxes are enabled.

The sync re-pushes every child after copying, but publish_sandbox returns early under config_delivery_mode = "poll", so children pick up the parent's change only on their next poll, up to one interval after the parent loaded it. Since polling goes away in 0.2.0 (#4498), it seems reasonable to require config_delivery_mode = "push" when experimental_enable_child_sandboxes is on: refuse to start otherwise, and serve an unadmitted config to a child whose supervisor does not support push.

If you do, please make sure a failed sync is still retried. Today that retry comes from the periodic poll rebuild; I have not checked whether push sessions get an equivalent. Doing the copy inside the config build when the gate finds the child behind would make the republish itself the retry.

Low priority, not blocking.

Comment thread proto/openshell.proto
SandboxWorkloadTemplateProvenance created_from_workload_template = 20;
// Read-only parent of a child sandbox, set by the gateway at creation.
// Absent on root sandboxes. Never changes.
SandboxParent parent = 21;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

experimental_enable_child_sandboxes gates the behavior, but the contract ships unmarked: Sandbox.parent, Sandbox.created_by, CreateSandboxRequest.parent, ListSandboxesRequest.parent and SandboxParent land in openshell.v1 with no EXPERIMENTAL note, and so do sandbox create/list --parent and the max_concurrent_children gateway key.

After the next release these are frozen, while the RFC still leaves depth > 1 and policy narrowing open. Could you mark the fields and the message EXPERIMENTAL (as done at openshell.proto:2845), label or hide --parent, and either prefix the key (experimental_max_concurrent_children) or nest both keys under an experimental table?

Comment thread proto/openshell.proto
// Read-only identity of who created this sandbox, set by the gateway: the
// user who created it, or `sandbox:<id>` when a sandbox created it over
// MCP. Empty on sandboxes created before this field existed.
string created_by = 22;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

created_by holds either an OIDC subject or sandbox:<id>, and the authz code (check_creator_has_access, acting_for in mcp.rs) reads it as a subject. SDK consumers have to parse a prefix that a user subject could also carry, and it stores a raw id where parent uses {name, id}.

Consider a typed creator, for example oneof creator { string user_subject = 1; SandboxParent sandbox = 2; }, or keep this as the user subject and record the creating sandbox separately.

description: &'static str,
}

const TOOLS: &[Tool] = &[

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The MCP tools become a third API surface with no versioned contract:

  • users and sandboxes get the same tools/list, and role-only arguments are marked only in prose ("users only");
  • results (timed_out, already_existed, deleting) have no outputSchema;
  • there is no stability statement, and the SDK exposes the replies as raw bytes.

Could you emit the tool schema per caller role, declare outputSchema, and document the tools (and post_mcp in the SDK) as experimental?

Child sandboxes are experimental. A gateway operator enables them, and the MCP tools, with `experimental_enable_child_sandboxes`. Refer to [Gateway Configuration](/how-it-works/gateways/configuration#child-sandboxes).
</Note>

## Roots and Children

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please document the upgrade and rollback constraint here.

The parent link lives only in field 21 of the stored Sandbox. A gateway that predates it (a rollback, or an old replica during a rolling update) drops the unknown field when it rewrites the record, for example on a policy-status report. From then on the child is a root on every gateway version, with its policy and providers no longer tied to the parent. Users should delete child sandboxes before rolling back to a version without child support or running mixed gateway versions.

.store
.supersede_older_policies(sandbox_id, i64::from(newest_loaded))
.await;
// A root now enforces a new policy: copy it into its children.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[should] Gateways with the feature off still pay for it. Every policy load on a root (policy.rs:5562), and every failed load under fail-closed, starts a child sync. That sync takes the parent's lock and decodes every sandbox in the workspace. Deleting a root counts children the same way, and startup scans every workspace. None of these paths checks the flag.

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)

2 participants