Skip to content

bash: clean_env option — run task commands in the image's environment#151

Merged
Meirtz merged 1 commit into
masterfrom
feat/bash-clean-env
Jul 4, 2026
Merged

bash: clean_env option — run task commands in the image's environment#151
Meirtz merged 1 commit into
masterfrom
feat/bash-clean-env

Conversation

@Meirtz

@Meirtz Meirtz commented Jul 4, 2026

Copy link
Copy Markdown
Collaborator

Why

Agentix sandboxes overlay the /nix runtime bundle onto a task image, and the worker environment is deliberately bundle-polluted: PATH and seven other path vars get /nix/runtime prefixes so the runtime's own tools resolve. agentix.bash.run inherits that env verbatim — so a task command like python -m pytest on a SWE-bench image resolves the bundle venv's python (no task deps) instead of the image's conda/toolchain, and native binaries can load /nix libs via LD_LIBRARY_PATH. The agentix-dataset-swe plugin already hand-rolls this subtraction (get_env_without_agentix()) before running the official harness; this makes it a first-class, complete contract.

What

  • The worker spawn records what it strips. Interpreter-hostile vars (PYTHONPATH, LD_PRELOAD, NIX_*, …) were removed with no trace, making the image's originals unrecoverable. They now land under AGENTIX_SAVED_<NAME>, mirroring the existing AGENTIX_ADDED_* convention for prepends. On nested spawns the freshly-observed value overwrites an inherited snapshot (newest observation of the image env wins).
  • get_env_without_agentix() restores them (a live value beats its pre-strip snapshot) in addition to subtracting recorded path additions.
  • bash.run / run_stream gain clean_env=False — opt-in per call. The default (worker env) is correct for runtime tools (the claude CLI, bundled rg live under /nix); clean_env=True is correct for task commands. In clean mode the shell itself is the image's bash too (restored vars like LD_PRELOAD target it, not the Nix bash), with the bundled bash only as a last resort.

Tests

New coverage for the save/restore round-trip, the nested-spawn overwrite rule, clean_env env construction, and image-bash resolution. Env-sensitive tests clear ambient strippable vars so they assert the production rule, not the runner's environment.

Reviewed by a second co-author (Codex) + an adversarial multi-agent pass; confirmed findings folded in (stale-snapshot overwrite, image bash in clean mode, test isolation).

🤖 Generated with Claude Code

The worker environment is deliberately bundle-polluted (PATH and seven
other path vars get /nix/runtime prefixes so the runtime's own tools
resolve), and bash.run inherits it verbatim — so a task command like
`python -m pytest` in a task image resolves the bundle venv's python
instead of the image's toolchain, and native binaries can load /nix libs
via LD_LIBRARY_PATH. The swebench plugin already had to hand-roll the
subtraction before running the official harness.

Three pieces make the subtraction a first-class, complete contract:

- The worker spawn now RECORDS what it strips: interpreter-hostile vars
  (PYTHONPATH, LD_PRELOAD, ...) were removed with no trace, making the
  image's originals unrecoverable; they land under AGENTIX_SAVED_<NAME>,
  mirroring the AGENTIX_ADDED_* convention for prepends. On nested
  spawns the freshly-observed value overwrites an inherited snapshot —
  the newest observation of the image env is authoritative.
- get_env_without_agentix() restores AGENTIX_SAVED_* vars (a live value
  wins over its pre-strip snapshot) in addition to subtracting recorded
  path additions.
- bash.run/run_stream gain clean_env=False: opt-in per call, because the
  default (worker env) is correct for runtime tools — the claude CLI and
  bundled rg live under /nix — while clean_env=True is correct for task
  commands. In clean mode the shell itself is also the image's bash
  (restored vars like LD_PRELOAD target it, not the Nix bash), with the
  bundled bash only as a last resort. Caller env overrides win last
  either way.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@Meirtz Meirtz merged commit 2000b3e into master Jul 4, 2026
5 checks passed
@Meirtz Meirtz deleted the feat/bash-clean-env branch July 4, 2026 01:09
Meirtz added a commit that referenced this pull request Jul 4, 2026
Follow-up to #151 per maintainer review: a boolean flag on run/run_stream
adds cognitive cost to a tool that should read like subprocess.run, and
the mechanism to pick an environment already exists — you pass env=. So
instead of a mode flag:

- bash.run/run_stream env= now follows subprocess.run exactly: omit it
  to inherit the worker's environment (right for bundle tools — the
  claude CLI, bundled rg), pass a dict to REPLACE it wholesale. No merge,
  no flag. (No production caller passed a partial env expecting a merge —
  only tests, updated to the replace idiom.)
- image_env() is the extracted helper the swebench scorer hand-rolled:
  get_env_without_agentix() plus BASH_ENV -> ~/.bashrc (derived from the
  RESULT env's HOME, so a stripped-and-restored or overridden HOME picks
  the right rc file). Canonical name: agentix.bash.image_env, re-exported
  next to run(); implemented in agentix.runtime.shared.env because that
  module owns the ADDED/SAVED contract and a cross-plugin import would
  not resolve statically in the pkgutil namespace layout. swebench's
  score.py now calls it instead of its private copy.
- The shell binary follows the env's PATH like everything else the
  command resolves: the default worker env lists the bundled bash first
  (behavior unchanged for bundle calls), while an image_env() PATH
  selects the image's own bash — its restored LD_PRELOAD targets that
  binary, not the Nix one.

image_env() reads the calling process's live environment, so it is only
meaningful inside the sandbox; the docs show the host-side recipe
(env = await c.remote(image_env)) and warn that evaluating it host-side
would ship the host's environment into the container.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Meirtz added a commit that referenced this pull request Jul 4, 2026
…ag) (#154)

Follow-up to #151 per maintainer review: a boolean flag on run/run_stream
adds cognitive cost to a tool that should read like subprocess.run, and
the mechanism to pick an environment already exists — you pass env=. So
instead of a mode flag:

- bash.run/run_stream env= now follows subprocess.run exactly: omit it
  to inherit the worker's environment (right for bundle tools — the
  claude CLI, bundled rg), pass a dict to REPLACE it wholesale. No merge,
  no flag. (No production caller passed a partial env expecting a merge —
  only tests, updated to the replace idiom.)
- image_env() is the extracted helper the swebench scorer hand-rolled:
  get_env_without_agentix() plus BASH_ENV -> ~/.bashrc (derived from the
  RESULT env's HOME, so a stripped-and-restored or overridden HOME picks
  the right rc file). Canonical name: agentix.bash.image_env, re-exported
  next to run(); implemented in agentix.runtime.shared.env because that
  module owns the ADDED/SAVED contract and a cross-plugin import would
  not resolve statically in the pkgutil namespace layout. swebench's
  score.py now calls it instead of its private copy.
- The shell binary follows the env's PATH like everything else the
  command resolves: the default worker env lists the bundled bash first
  (behavior unchanged for bundle calls), while an image_env() PATH
  selects the image's own bash — its restored LD_PRELOAD targets that
  binary, not the Nix one.

image_env() reads the calling process's live environment, so it is only
meaningful inside the sandbox; the docs show the host-side recipe
(env = await c.remote(image_env)) and warn that evaluating it host-side
would ship the host's environment into the container.

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
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.

1 participant