Skip to content

feat(box): send python3 and each alias's folder to the shell - #417

Merged
hanaduy merged 4 commits into
mainfrom
feature/python-alias-through-shell
Nov 14, 2026
Merged

hanaduy merged 4 commits into
mainfrom
feature/python-alias-through-shell

Conversation

@hanaduy

@hanaduy hanaduy commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Description

Today a python3 that a program starts (a #!/usr/bin/env python3 line, subprocess.run(["python3", ...])) takes a different path from a python3 typed in the Shell. The bin/python3 alias reads the script itself, with the agent's own access, sends the source straight to Monty with no shell:exec decision, and waits under its own 35-second limit, while Monty allows 120 seconds. Every alias also starts its command in the workspace, not in the folder it was run from. Measured on main (f476373, macOS): after cd sub, zsh -c 'cat here.txt' read the workspace's here.txt and exited 0, and a file the command wrote landed at the workspace top.

This follows the review discussion on #389: the python3 alias stops deciding which interpreter runs and becomes a pipe to the Shell, so the Shell is the one place that decides what python3 means, and every alias tells the Shell which folder it was started in. It doesn't add any way to run real Python; on this branch the Shell's python3 is still Monty.

What changes

  • Protocol 8 → 9, one new client frame, run. It carries a command and the caller's folder. The command is text for zsh, bash and sh, or a name plus an argument list for python3 and python. call and every other frame are unchanged.
  • Every alias sends its folder: PWD when it names . (same device and inode), otherwise getcwd. If the alias can't tell, it exits 125 and sends nothing. It never falls back to the workspace.
  • python3 and python aliases open a Shell Program and send their name and arguments unchanged. They parse no option and read no file. The trusted process single-quotes every word, with the same quoting xargs and find -exec already use, so $(...), backticks and newlines stay literal. The Shell's own python3 then runs Monty, exactly as a typed python3 does.
  • The folder is checked before the command runs. Program::enter first refuses a folder no host path backs (see below). Then it calls Mediated::change_dir, the fs:read change_dir decision and reachable-paths check a typed cd gets, and checks the resolved folder again. The workspace, where the Program already starts, takes no decision, as today. A refused folder, a missing one, or a file runs nothing and exits 126 with strands-box: cannot start in <folder>: <reason>. Entering the folder counts against the call's deadline, and a call with no time left after entering reports a timeout (125) without running the command.
  • The alias's 35-second Python wait is gone. limits.shell.timeout applies (default 30 s). Monty's own 120 s limit is unchanged, and it's what stops a script that never hands back control.
  • The direct Python mode is refused at protocol 9. open with mode python followed by call used to run Monty with no shell:exec decision. No version-9 client uses it, and any program in the box can connect to box.sock, so leaving it would let the agent skip the decision the docs now say gates python3. The broker refuses it with a reason naming protocol 9. The serving code is unreachable but not deleted yet, because deleting it also touches a shared timeout used by fetch.
  • A folder no host path backs is refused. The Shell keeps an in-memory /, /tmp, /home, /usr/bin and /bin. On main a call from /, or from Linux's private /tmp, runs in the workspace like every other call. Without this check, entering the caller's folder would put the Shell in that in-memory folder, where the command exits 0 and its files go nowhere. Program::enter refuses a folder no host path backs, both as the alias spelled it and after change_dir resolves it (which catches a .. that climbs out of the workspace), with 126. A typed cd /tmp inside a command is unchanged.

Visible behaviour changes (each needs approval)

  • The python3 alias now raises a shell:exec decision. A policy with only fs:* permits refuses it with 126.
  • The Shell reads a script file under an fs:read decision, from the caller's folder, instead of the alias reading it with the agent's own access.
  • bash -c, zsh -c, sh -c, python3 and python start in the caller's folder.
  • Through the Python alias, a bare python3 or python3 - exits 2 (the Shell's refusal) instead of 125, and --version and --help work.
  • The broker protocol loses a mode: open with mode python is refused at version 9.
  • A call from a folder no host path backs (/, Linux's private /tmp) exits 126. On main it ran in the workspace.

Not in this PR

Related Issues

Part of #371. Follows the alias proposal in the review of #389. #397 has merged; 52e51a0 merges main into this branch and resolves the conflicts in the alias, host.rs, program.rs, crates/box/AGENTS.md and docs/user/shell.md. #444 is stacked on this branch. This PR also overlaps #414 in host.rs, program.rs (a new Program::enter next to use_shell, using shell.proc and kernel().change_dir) and docs/design/decisions.md, so whichever lands second rebases.

Type of Change

Other: a new broker frame and a change to how the python3 alias routes (frozen surfaces).

Testing

Run on macOS for every commit (logs not attached):

  • cargo fmt --all and cargo fmt -p strands-box --check: pass.
  • cargo clippy -p strands-box --all-targets --all-features --locked -- -D warnings with CI's allow list: 0 warnings.
  • cargo test -p strands-box --bin strands-box-sock-alias --locked: 45 pass.
  • cargo test -p strands-box --lib --all-features --locked: 948 pass, 1 ignored on afff460 (two tests of the removed Python mode were replaced by one that checks it's refused, and one deadline test was added).
  • cargo test -p strands-box --all-features --locked --no-run: every target compiles.
  • Replacing the change_dir call with a plain assignment fails 2 of the new broker tests, and dropping the quoting fails 4 (measured on afff460; the host-path check now refuses some folders before change_dir runs).

CI on the first commit (834d539): macOS and Linux unit tests passed, including the in-box tests; lint, site manifest and CodeQL passed.

CI on the merge with main (52e51a0): macOS and Linux unit tests, lint, site manifest and CodeQL pass.

New tests:

  • Alias: name and arguments forwarded unchanged (no args, -, -m pytest, -c, --version, arguments after a script, $(rm x), backticks, newlines, quotes, spaces, the empty string); non-UTF-8 argument refused; folder taken from PWD only when it names .; unknown folder refused; every submission sends the folder.
  • Protocol: run round-trips in both command shapes; malformed run frames refused; version is 9; version 8 refused.
  • Broker, with a real Shell and policy but no box: every word stays literal (11 hostile arguments through printf; a file survives $(rm x)); a run starts in the caller's folder; the Python alias reaches the Shell's python3 and reads a script that exists only in the caller's folder; bare python3 and python3 - exit 2; the Python alias needs shell:exec; a policy forbidding every change_dir still runs in the workspace but refuses sub and writes nothing; a folder outside reach, missing, a file, relative, or 200 KiB long is refused and runs nothing; /, /tmp, a made-up absolute path, and a .. that climbs out of the workspace are refused and run nothing; open with mode python is refused and runs nothing.
  • In a box (crates/box/tests/box_shell.rs), compiled here, run only by CI: an alias run from a subfolder runs there (pwd, cat, python3 show.py); a refused folder runs nothing; the Python alias needs shell:exec.

Not run locally: anything that starts a box (box_shell.rs and the other end-to-end suites, test-integ), Linux, and the Mermaid blocks in the changed design pages.

Docs updated in the same commit: docs/user/monty.md, docs/user/shell.md, docs/user/config.md, docs/design/monty.md, docs/design/shell.md, docs/design/binaries.md, docs/design/architecture.md, crates/box/AGENTS.md, the protocol.rs module docs, and one entry in docs/design/decisions.md (#python-in-the-shell-is-monty, reworded to say both ways reach the Shell's python3).

  • I ran the relevant suites (just check, or cargo test --workspace --all-features)
  • If I touched Rust, I ran cargo fmt --all and cargo clippy (scoped to strands-box, see above)

Checklist

  • I have read the CONTRIBUTING document
  • I have reviewed and understand every line of code in this PR, including any generated by AI tools, and I can explain why it works
  • My change is focused and reasonably small; I have split unrelated work into separate PRs
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • My changes generate no new warnings

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

## Problem

A `python3` or `python` that a program starts skips the Shell. The alias
reads the script with the agent's own access, sends the source straight
to Monty with no `shell:exec` decision, and waits under its own 35-second
limit, while Monty allows 120 seconds and the Shell's deadline is
configurable. The same script typed in the Shell takes a different route
with different decisions and limits.

Every alias also starts its command in the workspace, not in the folder
it was run from. Measured on main: from a subfolder,
`zsh -c 'cat here.txt'` read the workspace's `here.txt` and exited 0. A
read silently gets the wrong file, and a write lands in the wrong folder.

## Solution

The broker protocol moves to version 9, with one new client frame, `run`.
It carries a command and the caller's folder. The command is text for
`zsh`, `bash` and `sh`, or a name and an argument list for `python3` and
`python`. `call` and every other frame are unchanged; an alias built for
version 8 is refused by version.

- Every alias sends the folder it was run from: `PWD` when it names `.`,
  otherwise `getcwd`. If the alias can't tell, it exits 125 and sends
  nothing, so a command never falls back to the workspace.
- The `python3` and `python` aliases open a Shell Program and send their
  name and arguments unchanged. They parse no option and read no file.
  The trusted process single-quotes every word, the quoting `xargs` and
  `find -exec` already use, so `$(...)`, backticks and newlines stay
  literal. The Shell's own `python3` then runs Monty, exactly as a typed
  `python3` does, and refuses a bare `python3` or `python3 -` with
  status 2.
- The alias's 35-second Python wait is gone; `limits.shell.timeout`
  applies, and a `Signal` can end the call.
- Before the command runs, `Program::enter` moves the Shell into the
  folder through `Mediated::change_dir`, the `fs:read` `change_dir`
  decision and reachable paths check a typed `cd` gets. The folder the
  Program already starts in, the workspace, takes no decision, so a
  file-scoped policy still runs `zsh -c` from there. A refused folder, a
  missing one, or a file runs nothing and exits 126 with
  `strands-box: cannot start in <folder>: <reason>`.

Interfaces that move, which need a maintainer's approval (the broker
protocol and the aliases are frozen surfaces):

- The `python3` alias now raises a `shell:exec` decision. A policy with
  only `fs:*` permits refuses it with 126.
- The Shell reads a script file under `fs:read`, from the caller's
  folder, not with the agent's own access.
- `bash -c`, `zsh -c`, `sh -c`, `python3` and `python` start in the
  caller's folder, and a folder other than the workspace raises an
  `fs:read` `change_dir` decision.
- Through the Python alias, a refused form exits 2 instead of 125, and
  `--version` and `--help` work.

Monty still roots a relative path at the workspace. Making it follow the
Shell's folder changes the vendored Shell's script hook, which the move
to upstream Strands Shell replaces, so it is a follow-up. The direct
`Interpreter::Python` route stays in the daemon for one release; no alias
opens it.

Docs: `docs/user/monty.md`, `docs/user/shell.md`, `docs/user/config.md`,
`docs/design/monty.md`, `docs/design/shell.md`,
`docs/design/binaries.md`, `docs/design/architecture.md`, the
`python-in-the-shell-is-monty` entry in `docs/design/decisions.md`, and
`crates/box/AGENTS.md`.

## Tests

Ran on macOS: `cargo fmt -p strands-box --check`, clippy with the CI
allow list and `-D warnings`, the alias tests (45 pass), the library
tests (948 pass, 1 ignored), and every test target with `--no-run`.
`Cargo.lock` is unchanged.

- Alias: `a_python_alias_forwards_its_name_and_arguments_unchanged`
  (no arguments, `-`, `-m`, `$(rm x)`, backticks, newlines, quotes,
  spaces, the empty string), `a_python_alias_refuses_a_non_utf8_argument`,
  `the_working_directory_comes_from_pwd_only_when_pwd_names_it`,
  `an_unknown_working_directory_is_refused`,
  `every_submission_sends_the_callers_directory`. The `-c`, `-lc`,
  `-c -l` and `SCRIPT` tests still pass.
- Protocol: `a_run_frame_round_trips_in_both_command_shapes`,
  `a_malformed_run_frame_is_refused`,
  `the_protocol_version_that_carries_run_is_nine`.
- Broker: `a_command_as_words_keeps_every_argument_literal`,
  `a_run_starts_in_the_callers_directory`,
  `a_python_run_reaches_the_shells_python_and_reads_the_callers_script`,
  `a_python_run_with_no_script_is_refused_by_the_shell`,
  `a_python_run_needs_a_shell_exec_permit`,
  `only_a_folder_other_than_the_programs_own_meets_the_change_dir_decision`,
  `a_folder_the_box_cannot_enter_is_refused_and_runs_nothing`,
  `a_python_program_refuses_a_run`, `the_version_before_run_is_refused`.
  Replacing the `change_dir` call with a plain assignment, and dropping
  the quoting, each fails four of these.
- In a box (`box_shell.rs`), compiled here and run only by CI:
  `an_alias_run_from_a_subfolder_runs_in_that_subfolder`,
  `an_alias_in_a_folder_the_policy_refuses_runs_nothing`,
  `the_python_alias_needs_a_shell_exec_permit`.

Not run: anything that starts a box (`box_shell.rs` and the other
end-to-end suites, `test-integ`), and Linux. This Mac can't start a box
inside its own sandbox. The Mermaid blocks in the changed design pages
were not compiled here.
## Problem

Two review findings on the previous commit.

- The broker still served `open` in Python mode. A `call` on that
  Program ran Monty with no `shell:exec` decision. No version 9 alias
  opens it, but any process in the agent's sandbox can connect to
  `run/box.sock`, so a policy that forbids `shell:exec` on `python3`
  did not stop a client the workload wrote itself.
- A caller's folder that no host path backs was entered. The reachable
  paths check passes a name outside the host set, and the Shell's
  in-memory tree holds `/`, `/tmp`, `/home`, `/bin` and `/usr/bin`. So
  `printf RAN` run from `/` or `/tmp` ran in memory and exited 0, and a
  write landed nowhere on the host. A `..` spelling that starts inside
  the workspace reached the same place.

Smaller findings: entering the folder ran outside the call's deadline,
and the docs left out the input limit on `python3 -c`, a script that
never pauses, a folder name that isn't valid UTF-8, and the agent's home
in one troubleshooting row. The previous commit message also said a
`Signal` can end a Python call through the alias; the alias sends no
`Signal`.

## Solution

- The broker refuses `open` in Python mode with a `transport_error`
  denial that says the mode was removed in protocol 9. The Shell is now
  the only way to Monty. The old serving code stays, unreachable:
  deleting it also retires `REQUEST_TIMEOUT` and the `fetch.rs` timeout
  ordering check, which is a separate change.
- `Program::enter` refuses a folder `Reach::names_a_host_path` doesn't
  back, both as the alias spelled it and as the Shell resolved it after
  `change_dir`, and restores the old folder. The refusal is the usual
  126 with `strands-box: cannot start in <folder>: outside this box's
  home and every declared bind`.
- Entering the folder counts against the call's deadline, and the
  command gets what is left of it.
- Docs: `docs/user/monty.md` (input limit, never-pausing script,
  non-UTF-8 folder, the troubleshooting row), `docs/user/shell.md`,
  `docs/design/shell.md`, `docs/design/monty.md`,
  `docs/design/binaries.md`, the `python-in-the-shell-is-monty` entry,
  `crates/box/AGENTS.md`, and the protocol and Python module docs.

## Tests

Ran on macOS: fmt check, clippy with the CI allow list and
`-D warnings`, the alias tests, the library tests, and every test target
with `--no-run`. `Cargo.lock` is unchanged.

- `an_open_in_python_mode_is_refused_and_runs_nothing` replaces
  `a_python_program_refuses_input_and_signal_by_name` and
  `a_python_program_refuses_a_run`: the open is refused by name, a
  `call` for that id finds nothing open and writes nothing, and the
  connection still serves a Shell Program. With the old open restored,
  it fails.
- `a_folder_the_box_cannot_enter_is_refused_and_runs_nothing` gains `/`,
  `/tmp`, a made-up absolute path, and a `..` spelling from the
  workspace into `/tmp`. Without both checks, `/` prints `RAN`. Without
  the check after `change_dir`, the `..` row prints `RAN`.

Not run here: the in-box suites and Linux.
… docs

## Problem

Three small findings from the review of the previous commit.

- `tokio::time::timeout` polls the folder entry before its timer. A
  synchronous lookup that overran the call's deadline still returned
  `Ok`, and the command then started with no time left, so it could run
  before the timeout was reported.
- `docs/design/architecture.md` said the broker hands Python scripts to
  Monty, and the `monty-is-per-request-and-buffers-its-output` entry in
  `docs/design/decisions.md` described an outer limit on direct Python
  calls. The previous commit removed that route.
- `an_open_in_python_mode_is_refused_and_runs_nothing` waited on an
  unbounded read, so a regression would fail only at the 300-second idle
  timeout.

## Solution

- After the folder entry, the broker reports a timeout and exits 125
  without running the command if no time is left.
- `architecture.md` now says the Shell handles the Python aliases and
  runs scripts in Monty. The decisions entry drops the outer limit and
  the "separate five-minute allowance" sentence, and says Python runs
  through the Shell.
- The refusal test bounds each read to 5 seconds.

## Tests

Ran on macOS: fmt check, clippy with the CI allow list and
`-D warnings`, the alias tests, the library tests, and every test target
with `--no-run`. `Cargo.lock` is unchanged.

- `a_run_whose_folder_entry_passes_the_deadline_runs_nothing` runs a
  `run` into a subfolder 20 times with a zero deadline and checks that
  each reports the timeout, exits 125, and writes nothing. With the old
  remaining-time line restored, it fails on the first attempt.
- On this code, replacing `change_dir` with a plain assignment fails 2
  broker tests, and removing the quoting fails 4.

Not run here: the in-box suites and Linux.
crates/box/src/bin/strands-box-sock-alias.rs: kept the run frame, the caller's folder and python3 words; submission now sends Open and Run, and forward attaches main's stdin frames (each Input, then InputEof, or InputEof alone) after Run for every alias; forwards_stdin is true for Text (shell aliases) and false for Words (python3/python); main's stdin tests now send Run.
crates/box/src/run/broker/host.rs: imported both Command and main's MAX_CHUNK_BYTES and MAX_ENCODED_CHUNK_BYTES; main's Input handling is per Program, so it serves a Run as it serves a Call.
crates/box/src/run/broker/program.rs: kept no_host_folder for Program::enter and dropped poisoned(), which main's stdin buffer no longer calls.
crates/box/AGENTS.md: kept main's stdin notes with Call changed to Run, said python3/python send InputEof right after Run and forward no stdin, and kept the StdinEof note for every alias.
docs/user/shell.md: kept the caller's folder rule and main's credential route placeholder, and said the python3 and python aliases forward no standard input.
PROTOCOL_VERSION stays 9: main is still at 8, and the Input frames it uses existed before.
Comment thread crates/box/src/run/broker/host.rs Resolved
@github-actions

Copy link
Copy Markdown

Assessment: Comment

Review of head 52e51a0. There were no earlier review comments. On the code side, I found no case where the new behaviour gives a wrong result. The interface changes this PR lists (protocol 9, Open { mode: python } refused, the python3 alias needing shell:exec, aliases starting in the caller's folder) still need a maintainer's approval under the root AGENTS.md rule that strands-box is interface-frozen. That approval, not the code, is what blocks the merge.

Cases and rules checked that held
  • Starting folder: I checked how Program::enter handles these cases:
    • the workspace, which gets no decision, the same as main;
    • a subfolder reached through a stale PWD or through getcwd;
    • /, Linux's private /tmp, and /usr/bin (all refused 126);
    • /ws/../etc (passes the spelled check, refused after it resolves);
    • a symlink that leads outside reach (refused by the reach check);
    • a TMPDIR outside the policy, which is refused, as the docs say.
  • Word quoting: command_text keeps $(…), backticks, quotes, newlines, *, NAME=value and the empty string literal.
  • Python mode refusal: no client in the repo opens Interpreter::Python (only tests do), and a test pins the refusal.
  • Deadline: time spent entering the folder is subtracted, and a call with no time left runs nothing.
  • Premises: each alias still finds the socket from its own path, and argv[0] still chooses the route before it connects. The directory never chooses the socket. A workload-written run gets no authority that cd <dir> && cmd in a call doesn't already have. PROTOCOL_VERSION is bumped, and the reference pages ship in the same commit.
  • Tests run: the 50 matching broker::host and broker::program unit tests pass locally (Linux x86_64). I did not run the in-box suite.

One structural follow-up is inline: the Python-mode path can be removed from host.rs now. The folder checks are well tested, including the proof that removing change_dir or the quoting breaks the tests.

@abhisheksp abhisheksp 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.

Approving. Making the Shell the one place that picks the interpreter is the right shape, and closing the direct Monty mode removes an undecided route. I found no new reach: the caller's folder is untrusted input that only chooses where the command starts, and it goes through the same fs:read change_dir decision and host-path check as a typed cd.

This approval also covers the frozen-surface changes: PROTOCOL_VERSION 8 to 9 with the new run frame, open in python mode refused, the python3 alias needing shell:exec, and every alias starting in the caller's folder.

Two comments, neither blocking:

  1. macOS exit 125. An alias can now exit 125 where main ran the command, when PWD is stale or getcwd is refused. Please say in docs/user/shell.md what the user sees and how to recover, or fall back to the workspace in that case if you think it is safe.
  2. Dead Monty serving code. The direct Monty mode's serving code is still present and kept unreachable by one deny line. Please remove it here or in a follow-up, so the refusal does not rest on a single line.

@hanaduy
hanaduy merged commit 174fe80 into main Nov 14, 2026
37 of 39 checks passed
@hanaduy
hanaduy deleted the feature/python-alias-through-shell branch November 14, 2026 00:15
hanaduy added a commit that referenced this pull request Nov 16, 2026
Review asked that Box not know about Python installs: with Monty off, Python is an ordinary tool
the operator declares and grants, and Box special-cases `python3` only to run Monty. So this
restores the branch to #417's state and drops what the earlier commits on it added:

- install detection and protection (`run/python_install.rs`, the refused writes into the install);
- the grants Box added for a Python tool, including the libraries and the certificates;
- `[tool.python]` answering both names, the 126 refusal when no Python tool is declared, the
  write-only stdin and its stderr line, and the read-only install line;
- the Shell hooks those needed in the vendored Shell, which `main` has since replaced (#414).

The `monty = false` switch comes back on top of `main` in a later commit. The containment fix for
a link chain through a linked folder moves to its own pull request, since it affects any tool.
hanaduy added a commit that referenced this pull request Nov 16, 2026
After the revert, this branch holds only #417, which main has as 174fe80, so the merge takes
main's tree. That includes #414, which replaced the vendored Shell.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants