Skip to content

Sandbox runs a granted venv's python by its own name, keeping the venv - #98

Merged
haizhongzheng merged 2 commits into
mainfrom
fix/sandbox-venv-python
Nov 10, 2026
Merged

haizhongzheng merged 2 commits into
mainfrom
fix/sandbox-venv-python

Conversation

@haizhongzheng

@haizhongzheng haizhongzheng commented Nov 10, 2026 •

Copy link
Copy Markdown
Member

Change

Fixes #95.

Problem. Sandbox.wrap runs a command by its real path (hermit/sandbox.py, after shutil.which), with one exception for hermit's own interpreter. A venv's bin/python is a link to its base interpreter, so a venv's python was run as the base interpreter. That interpreter finds no pyvenv.cfg beside the path it started from, so it ran without the venv, and the venv's packages failed to import. Reproduced on main for a venv made from hermit's interpreter and one made from /usr/bin/python3, granted either for the sandbox or for one call: each was run as anaconda3/bin/python or /usr/bin/python3.9, and import <module in the venv> raised ModuleNotFoundError.

Behavior now.

  • A virtual environment's interpreter (a pyvenv.cfg beside it or one directory up) runs by its own name when the environment exists inside under that name. Concretely:
    • the venv lies beneath an explicit grant: the sandbox's or the call's readable or writable, or interpreters;
    • it is reachable from that grant (_reachable). The check follows the links below the outermost granting place, with /usr counted as shown whole, and each link's target must be shown too;
    • it does not lie beneath a layout's root (the learner's /w);
    • its interpreter's links reach its installation through no linked directory other than the system's (/bin, /lib).
  • The interpreter's link hops outside the venv are made again inside as links, not bound as copies, so a shared Python keeps its $ORIGIN.
  • Every other venv interpreter runs by its real path, as before. That covers an ungranted one, one beneath /usr that is not granted, one beneath /w, one reached through a link to a place nothing shows, and one made through uv's version alias. None of them can fail to start where it started before.
  • The trust rule is unchanged. Nothing is bound from a pyvenv.cfg or from a directory's contents. A venv granted for one call still names no installation to show, and a test asserts it.
  • Also fixed on the way: bind() skipped every name beginning with /usr, including /usr2. A grant there is now bound.

Interfaces. hermit/sandbox.py: new helpers _venv_root, _granted_venv, _reachable, _link_hops and _beneath, plus one branch in Sandbox.wrap; bind()'s /usr test is boundary-aware. No signature changes.

  • hermit/adapter/remote.py passes a benchmark venv (HA_ADAPTER_PYTHON) as a trusted interpreters entry. That interpreter now runs as the venv, as the comment there intends.
  • ASPIRE's workaround (examples/aspire_robosuite/utils/agent/sim.py, confine) still works and is kept. Its comment, which said the sandbox runs a venv's python by its real path, is updated.

Docs.

  • docs/guides/sandbox.md (Programs: when a granted venv's interpreter runs by its own name, and the exceptions).
  • docs/_includes/sandbox-build.html (the diagram's programs row).
  • CHANGELOG.md under Fixed.

Validation

  • python -m pytest --changed --base origin/main on 422c1ef: every group (the sandbox is shared code), 1263 passed, 98 skipped, 0 failed. The environment had no mini-swe-agent, and the integration tests ran without --integration, so both were among the skipped.
  • tests/core/test_sandbox.py now has 20 venv-related cases:
    • end to end under bubblewrap: a venv made from this interpreter, one made from /usr/bin/python3, and one whose bin/python reaches the interpreter through another link. Each imports its own package inside;
    • command inspection (bubblewrap faked) for every exception above, the trust rule, link hops made as links, and the /usr2 bind.
    • Each fails on the code it protects: the first three on origin/main, the rest with their fix reverted in a throwaway copy.
  • The launcher, python -m hermit.sandbox '{"network": "isolated", "readable": ["<venv>"]}' -- <venv>/bin/python -c 'import marker95; ...', gives ModuleNotFoundError on main and the venv with this change.
  • bash docs/build.sh (Sphinx, -W): passes, no warnings.
  • ruff check: only the two E741s that are already on main (tests/core/test_sandbox.py:289-290).
  • Not verified:
    • macOS: no bubblewrap there, so the sandbox runs unconfined and this change does not apply.
    • A shared-libpython Python reached through an external link (the $ORIGIN case): checked by reading the code and by inspecting the generated command, since this machine's Python is static.
    • A uv standalone build: not installed here, and such venvs take the fallback anyway.

Review

  • Tests and documentation cover the affected behavior, where applicable. See Validation and Docs.
  • Commands, defaults, and public interfaces are consistent. No signature changes.
  • Shared logs and examples contain no credentials or private data.
  • Documentation-only: not applicable (code change).
  • Core changes. hermit/sandbox.py, shared by every sandbox: one branch in how wrap names the program it runs. Only a granted venv's interpreter is affected.
  • Implementation correctness and failures.
    • An ungranted venv behaves as before.
    • hermit's own interpreter behaves as before (it runs by the same name either way).
    • A venv bound under another name (binds) is not counted as granted, so it runs as before.
  • Real execution paths and tests. Sandbox.wrap end to end under bubblewrap, and the python -m hermit.sandbox launcher.
  • Documentation. docs/guides/sandbox.md, CHANGELOG.md.
  • This description. Read back against the final diff.
  • Independent review (post-PR audit). Dual review: in each round this session and a separate read-only Codex (gpt-6.1-sol, high effort) reviewed the PR independently. It ran 10 rounds, the limit, plus an 11th at the user's request, which found nothing.
    • 20 findings were confirmed and fixed, each with a test. Almost all were cases where running by name would have chosen a name absent inside: layouts, link spellings, nested grants, /usr, a root grant, /usr2, and a bound copy breaking $ORIGIN.
    • One finding was a hole in an intermediate fix: binding a linked installation's lib directories could show undeclared host directories. That fix was removed in favour of the fallback.
    • Left: a caller who lays a named bind over a venv it also grants (minor).
  • Test checklist.
    • The tests are in tests/core/test_sandbox.py, the group of hermit/sandbox.py.
    • Their names state the behavior.
    • The end-to-end test skips without bubblewrap and writes only to tmp_path.
    • Runtime: under 1 s for the three cases.
  • Sandbox checklist.
    • Sandbox changes are core changes: this changes hermit/sandbox.py. The change was agreed in Sandbox.wrap runs a venv's python by its real path, which drops the venv #95, filed by the core team; see Core changes above.
    • It changes what a sandbox runs, not what it shows. No new path is bound: a granted venv was already shown, and its base installation is shown by program_paths as before.
    • Reuse the framework's sandbox: not applicable, since no method or dataset starts a new process.

🤖 Generated with Claude Code

Sandbox.wrap ran a command by its real path. A venv's bin/python is a link to
its base interpreter, so the base ran without the venv, and the venv's
packages failed to import.

A venv's interpreter (a pyvenv.cfg beside it or one directory up) whose
environment lies in a granted place (the sandbox's or the call's readable or
writable, or interpreters) now runs by its own name. An ungranted one runs
by its real path, as before. No pyvenv.cfg home is read for this: the base
installation is shown by following the interpreter's links, as for any
program.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Review fixes for #98. Running a granted venv's interpreter by its own name
must not start a program that is not there inside, where the base
interpreter ran before. Such a venv now keeps the old behaviour:

- one beneath a layout's root (/w), whatever grants it;
- one not reachable from its grant: the walk follows the links below the
  outermost granting place (and /usr, shown whole), and each target must be
  shown too. This covers a .venv linked out of the grant, links through
  folders nothing shows, nested grants, and a bin/ that is a link;
- one named by a spelling no grant shows;
- one whose interpreter's links reach its installation through a linked
  directory other than the system's (uv's version alias).

An ungranted venv beneath /usr also runs as before. The interpreter's link
hops outside the venv are made again inside as links, not bound as copies,
so a shared Python still finds libpython by $ORIGIN. A root grant works,
and bind()'s /usr exclusion no longer drops /usr2. Nothing is bound from a
pyvenv.cfg or from a directory's contents; an earlier attempt that bound a
linked installation's lib directories was removed.

ASPIRE's workaround comment, the sandbox guide, its diagram and the
changelog say what the code does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@haizhongzheng
haizhongzheng merged commit 886c89d into main Nov 10, 2026
1 check passed
@haizhongzheng
haizhongzheng deleted the fix/sandbox-venv-python branch November 10, 2026 20:59
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.

Sandbox.wrap runs a venv's python by its real path, which drops the venv

1 participant