Skip to content

Run leaf from Codex's sandbox through a plugin MCP server - #3331

Merged
max-sixty merged 4 commits into
mainfrom
codex-mcp-sandbox
Oct 20, 2026
Merged

max-sixty merged 4 commits into
mainfrom
codex-mcp-sandbox

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

Codex's sandbox refuses what a leaf command needs and leaves no road out to a bridge, so a sandboxed Codex user had to add a rules file before Leaf could serve a page. Leaf's Codex plugin now declares an MCP server, which Codex starts outside the sandbox. Its one tool, run, runs this install's leaf with the words, working directory and stdin it is given, for the Codex thread the call's _meta names. A sandboxed refusal names the tool, and the README's rules file is gone.

What Codex does (measured at codex-cli 0.160.0)

  • Approval. A plugin can declare default_tools_approval_mode = "approve" on its own server and Codex honors it; a user's [plugins."leaf@leaf".mcp_servers.leaf] overrides it either way. With no mode declared, the first call asks the user, offering to allow the tool for the session or always. The manifest declares none: allowing the tool lets the agent run any leaf command, package scripts included, with the user's access, which the README now says, giving approve as the opt-in. Declaring it by default is left as a decision.
  • Start. Codex starts a plugin's MCP servers with every session, eagerly (initialize and tools/list arrive in a session that calls no tool), and never restarts one that exits. So the server is a stdlib program on the system Python (3.9 works): it answers tools/list about 20 ms after starting and holds 17 MB for the session's life, where the MCP SDK's imports alone took 0.3 s and 74 MB.
  • Environment. The server gets a short allowlist of variables plus the manifest's env_vars, which forwards the ones a leaf command reads from its harness or host.
  • Timeout. Codex abandons a call at its tool timeout and cancels nothing, so the server reads tool_timeout_sec from the manifest and ends the call's process group 10 s before, and ends every call as it exits.

Proof

leaf-dev journey codex-queue|codex-app-server --sandbox runs the task in Codex's workspace-write sandbox with the state home outside its writable roots and nothing to approve an escalation, and requires the task to serve through the tool. Both pass every step; in the final codex-queue run all 38 leaf commands went through the tool.

Review guide

  • skills/leaf/scripts/leaf/codex_tool.py and .codex-plugin/plugin.json: the server and its declaration.
  • machine.py: the Codex remedy, keyed on CODEX_SANDBOX and CODEX_SANDBOX_NETWORK_DISABLED.
  • dev/leaf_dev/journey_codex.py, commands/journey.py, eval_codex.py: --sandbox for the Codex journeys, a queue endpoint the MCP route can read, and MCP calls in Codex evidence.
  • worker/codex-config.toml: the website's Codex runs unsandboxed, so it disables the server.
  • TODO.md: a page in the state home is outside the sandbox's writable roots, so the agent's own edits to it ask the user or fail; that choice is filed.

This was written by Claude Code on behalf of max-sixty

max-sixty and others added 4 commits October 20, 2026 05:58
Codex's sandbox refuses what a `leaf` command needs and leaves no road out to
a bridge, so a sandboxed Codex user had to add a rules file before Leaf could
serve a page. Leaf's Codex plugin now declares an MCP server, which Codex
starts outside the sandbox. Its one tool, `run`, runs this install's `leaf`
with the words, working directory and stdin it is given, for the Codex thread
the call's `_meta` names. A sandboxed refusal names the tool, and the README's
rules file is gone.

Measured at codex-cli 0.160.0 with a scratch plugin:

- A plugin can declare `default_tools_approval_mode = "approve"` on its own
  server, and Codex honors it: the same call under `approval_policy = "never"`
  fails as needing approval without it. A user's
  `[plugins."leaf@leaf".mcp_servers.leaf]` overrides it.
- Codex starts a plugin's MCP servers with every session, sending
  `initialize` and `tools/list` in a session that calls no tool. So the server
  is a stdlib program on the system Python, answering `tools/list` in about
  20 ms with 17 MB resident; the MCP SDK's imports alone took 0.3 s and 74 MB.
- Codex starts the server with a short list of variables plus those the
  manifest names, so the manifest forwards the ones a `leaf` command reads
  from its harness, and the thread comes from each call's `_meta`.

`leaf-dev journey codex-queue|codex-app-server --sandbox` runs the task in
Codex's workspace-write sandbox with nothing to approve an escalation, and
requires it to serve through the tool; both journeys pass every step. The
journey's queue wrapper keeps its endpoint in a file, since the MCP server's
`leaf` cannot see the App Server's environment, and Codex evidence now records
MCP tool calls.

TODO.md trades the finished item for the one this leaves: a page in the state
home is outside the sandbox's writable roots, so the agent's own edits to it
are refused or ask the user.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…unning

Review found that a manifest-approved `run` tool lets an agent run any `leaf`
command, package scripts included, outside a sandbox the user chose, with no
question asked. With no mode declared, Codex 0.160.0 asks before the first call
and offers to allow the tool for the session or always (measured over App
Server), so the manifest declares none, the README says what allowing it lets
the agent do, and `default_tools_approval_mode = "approve"` is the opt-in.

Codex abandons a call at its tool timeout and cancels nothing, and never
restarts a server that exits. The manifest now declares `tool_timeout_sec`,
which the server reads to end a call's process group shortly before Codex gives
up, killing it if it ignores SIGTERM; the server ends every running call as it
exits, and answers every request, a failing one with its error. The
cancellation handling Codex never exercised is gone.

A Codex sandbox refusal now also reads `CODEX_SANDBOX_NETWORK_DISABLED`, which
Codex sets off macOS too, and tells the agent to ask the user to allow the tool
if Codex refuses the call. The tool forwards `LEAF_HOST_LAUNCHER` and
`LEAF_PAGE_COMMIT`, and the website, whose Codex runs unsandboxed, disables
the server.

The sandboxed journey now keeps the state home outside the sandbox's writable
roots, as a user's is, and allows the tool as a user who answered "always"
has; `codex-queue --sandbox` passes every step with all 38 `leaf` commands run
through the tool.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
The Worker's config test held the website's Codex config to no plugins table,
which the table disabling Leaf's MCP server there now is; it now holds the
config to exactly that table.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@max-sixty
max-sixty merged commit aa552d6 into main Oct 20, 2026
3 checks passed
@max-sixty
max-sixty deleted the codex-mcp-sandbox branch October 20, 2026 13:55
leaf-agent added a commit that referenced this pull request Nov 22, 2026
This week's duplicate sweep of `dev/`, `build/`, `tests/` and `worker/`
(week 47) moves six duplicated helpers onto one home each and moves
every caller. Behavior stays the same. The touched model tests, the dev
js_facts tests and the Worker's routing, storage and index tests pass. A
script compared the old and new outputs of the consolidated `leaf_dev`
functions and the seeded-log reads across the corpus, and they match.

| Question | Home | Copies removed (introducing PR) |
|---|---|---|
| Which stylesheets do the core and the bundled packages ship, leaving
out vendored files? | new `interact_support.shipped_sheets` | `sheets()`
in `test_interface_fonts.py` (#4804) and `test_theme_tokens.py` (#6025).
Five inline loops in `test_interact_layer.py` (#3138, #3886) |
| Run `tests/js_facts.mjs` | `interact_support.js_facts` (#2666) | its
own subprocess call in `dev/tests/test_js_facts.py` (#5118) |
| Lowercase hex of bytes in the Worker | `routing.hex`, which is
`newSessionId` (#313) renamed, plus a `sha256Hex` in `storage.ts` | the
publication-identity and chunk-digest copies in `storage.ts` (#2652) |
| Is this pid alive? | `leaf.machine.pid_alive` | `_alive` in
`tests/process/test_codex_tool.py` (#3331). `running()` in
`test_interact_layer.py` (#1774) |
| Read an example's seeded log | `read_event_lines(seed_log(source))`
(#4484) | inline parsers in `test_render_pages.py`,
`test_interact_product.py` (#924) and `test_render_gate.py` (#509). The
last one split lines with `splitlines()`, which also breaks at U+2028 |
| Paths of the bundled registries and packages; which registry keys name
elements | `leaf.schema.ASSETS` / `BUNDLED_PACKAGES` /
`DEFAULT_PACKAGE`, `leaf.registry.contract.is_element_name`,
`docs.registries()` | `docs.py`'s own `ASSETS`/`PACKAGES` (#3593) and
its `startswith("lf-")` checks (#2660). `logs usage`'s hand-written
regex and registry list (#5970). `ROOT / "packages" / "default"` in
`stills.py` (#3927) and `test_product_page.py` (#3593) |

`is_element_name` and the two checks it replaces give the same answer
for every key in the bundled registries today. A selector key such as
`lf-options.selected` would have passed the `lf-` prefix test.

<details><summary>Sets found and left alone</summary>

- **Sets an open PR is changing.** Nothing in this diff overlaps another
open PR's hunks. #6732 moves tests out of `test_render_gate.py`,
`test_render_pages.py` and `test_interact_layer.py`, but none of the
tests changed here.
- **Changes to browser tests with nothing run to check them.** The WCAG
contrast computation exists as about 11 copies across browser, journey
and model tests. Other copies: about 9 `EDITOR_ROOTS` shadow-root
keepers, the `/api/interaction` batch predicates, CDP `touch()`
closures, one-line patch templates, reply-holding route handlers, and
the `data-lf-entry` runtime-module import snippet in
`dev/leaf_dev/states.py` and in tests. Each would mean editing browser
tests that this sweep can't run on a shared host. They suit a sweep that
can run those tests.
- **Vendor bundling.** `build/browser/build.mjs` builds signals-core and
Lit with esbuild calls that repeat `build/vendor.mjs`'s `bundle()`.
Merging them changes build options and needs a full `check:browser` byte
comparison, so it was left for a change that runs that.
- **Copies that are near but not the same.** `_build.py`'s `check_links`
repeats `leaf_website.released_path`, but it also needs the matched
page's root and kind. The list of API-key variables an eval drops is
repeated across Python and the Promptfoo configs, as subsets and
supersets of one another. The Codex token-expiry check has different
thresholds, 1 h and 60 s.
- **Too small to be worth it.** Picking a free loopback port
(`eval_user.py`, `interact_support.available_loopback_port`). The XDG
state root in `leaf_dev.state_home` and `leaf.state.state_home`. The
`settled` helper in two `tests/runtime` files.

</details>

Co-authored-by: leaf-agent <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants