Skip to content

Commit 77e9afd

Browse files
committed
feat(machine): make a running machine's own files immutable in run jails
During `agent6 machine run`, the machine's own `.asm.toml` and its `scripts/` bundle (when under the jail-mounted cwd) are marked read-only in every jail the run spawns — tool-state jails via a new `LiveWorld.protect_paths`, and agent `run_command` jails via a new `ToolDispatcher(extra_protect_paths=...)` threaded through the agent subprocess. Layered alongside the existing protect_git / protect_agent6 read-only carve-outs. So a tool or agent state (even one with run_command and network) cannot rewrite its own machine logic, add an `allow_network` flag, or alter an audited bundle script — mid-run or for a future run. The stored run copy under `.agent6/machines/` was already covered by protect_agent6. Security review note: defense-in-depth for the supervisor egress model — the machine artifact the operator reviewed stays fixed for the duration of the run. Files outside cwd aren't in the child's view, so only in-cwd machine files are (and need to be) protected. No jail/Rust changes — reuses extra_protect_paths.
1 parent 7364910 commit 77e9afd

7 files changed

Lines changed: 91 additions & 6 deletions

File tree

‎SECURITY.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,10 @@ refused rather than silently under-confined. Every surface fails closed:
316316
machine cannot smuggle a path that reads or executes outside its own
317317
directory. Scripts are drafted at authoring time and reviewed/committed
318318
by the operator, never fetched or generated from untrusted model output
319-
at run time.
319+
at run time. And during a run, the machine's own `.asm.toml` + `scripts/`
320+
are made **read-only in every jail** (alongside `.git`/`.agent6`), so a
321+
tool or agent state cannot rewrite its own logic, add an `allow_network`
322+
flag, or alter an audited script mid-run or for a future run.
320323

321324
## Prompt-injection resilience
322325

‎STATE_MACHINES.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,9 @@ mounted cwd at run time, so keep the bundle at (or under) the directory you
347347
run `agent6` from. `machine check` validates the bundle: every entry under
348348
`scripts/` must resolve *inside* the bundle (symlinks that escape via
349349
`..`/absolute are rejected) and every static `scripts/...` command
350-
reference must exist and stay inside the bundle.
350+
reference must exist and stay inside the bundle. During a run the bundle (the
351+
`.asm.toml` + `scripts/`) is **read-only in every jail**, so a tool or agent
352+
cannot rewrite its own machine logic or audited scripts mid-run.
351353

352354
#### `wait`
353355

‎src/agent6/cli/machine_agent.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ def _run_one(req: dict[str, Any]) -> dict[str, Any]:
107107
graph_client=None,
108108
run_root_node_id=None,
109109
mcp_manager=None,
110+
extra_protect_paths=tuple(Path(p) for p in req.get("protect_paths", [])),
110111
)
111112
wf = Workflow(
112113
root=root,

‎src/agent6/cli/machine_cmds.py‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,11 @@ def _cmd_machine_graph(path: Path, *, fmt: str) -> int:
231231

232232

233233
def _build_machine_agent_runner(
234-
overlay: dict[str, Any], cwd: Path, profile: SandboxProfile, transcript_dir: Path
234+
overlay: dict[str, Any],
235+
cwd: Path,
236+
profile: SandboxProfile,
237+
transcript_dir: Path,
238+
protect_paths: tuple[Path, ...] = (),
235239
) -> Callable[[AgentRequest], AgentExecResult]:
236240
"""Build the runner an `agent` state uses to drive a confined agent6 loop.
237241
@@ -252,6 +256,7 @@ def run_agent(request: AgentRequest) -> AgentExecResult:
252256
"overlay": overlay,
253257
"profile": profile,
254258
"transcript_dir": str(transcript_dir),
259+
"protect_paths": [str(p) for p in protect_paths],
255260
"request": {
256261
"model": request.model,
257262
"prompt": request.prompt,
@@ -303,6 +308,19 @@ def run_agent(request: AgentRequest) -> AgentExecResult:
303308
return run_agent
304309

305310

311+
def _machine_protect_paths(machine_path: Path, cwd: Path) -> tuple[Path, ...]:
312+
"""The machine's own ``.asm.toml`` + ``scripts/`` bundle, to mark read-only
313+
in run jails. Only paths under the jail-mounted cwd are enforceable (a path
314+
outside cwd isn't in the child's view, so it can't edit it anyway)."""
315+
cwd_r = cwd.resolve()
316+
out: list[Path] = []
317+
for p in (machine_path, machine_path.parent / "scripts"):
318+
rp = p.resolve()
319+
if rp.exists() and _is_inside(rp, cwd_r):
320+
out.append(rp)
321+
return tuple(out)
322+
323+
306324
def _machine_network_refusal(
307325
cfg: Config, profile: SandboxProfile, tool_states: list[ToolState], has_network_tool: bool
308326
) -> str | None:
@@ -351,6 +369,9 @@ def _cmd_machine_run(path: Path, *, exit_on_wait: bool = False) -> int: # noqa:
351369
agent_runner: Callable[[AgentRequest], AgentExecResult] | None = None
352370
# Default profile for confinement-free machines: resolve from the host.
353371
profile: SandboxProfile = detect().detected_profile
372+
# The running machine's own file + scripts bundle are read-only in every
373+
# run jail, so a tool/agent can't rewrite its own logic or audited scripts.
374+
protect_paths = _machine_protect_paths(path, cwd)
354375
# Load the effective config when an `agent` state needs it, or when there
355376
# are any tool states (we need sandbox.tool_network/profile to gate egress).
356377
if has_agent_state or tool_states:
@@ -379,7 +400,7 @@ def _cmd_machine_run(path: Path, *, exit_on_wait: bool = False) -> int: # noqa:
379400
# The engine is a host-netns supervisor; each agent state confines
380401
# itself in its own subprocess per sandbox.agent_network.
381402
agent_runner = _build_machine_agent_runner(
382-
spec.config, cwd, profile, root / "agent_transcripts"
403+
spec.config, cwd, profile, root / "agent_transcripts", protect_paths
383404
)
384405
_warn_if_unsandboxed(profile)
385406
root = _machines_dir(cwd) / spec.machine
@@ -394,6 +415,7 @@ def _cmd_machine_run(path: Path, *, exit_on_wait: bool = False) -> int: # noqa:
394415
journal=journal,
395416
agent_runner=agent_runner,
396417
profile=profile,
418+
protect_paths=protect_paths,
397419
)
398420
result = drive(spec, journal, world, live=True, exit_on_wait=exit_on_wait)
399421
except (JournalError, EngineError) as exc:

‎src/agent6/machine/engine.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,10 @@ class LiveWorld:
165165
agent_runner: Callable[[AgentRequest], AgentExecResult] | None = None
166166
poll_interval_s: float = 0.5
167167
profile: SandboxProfile = "strict"
168+
# Paths made read-only in every tool jail — the running machine's own
169+
# `.asm.toml` + `scripts/` bundle, so a tool can't rewrite its own machine
170+
# logic or audited scripts mid-run (set by the CLI).
171+
protect_paths: tuple[Path, ...] = ()
168172

169173
def run_tool(
170174
self, argv: tuple[str, ...], timeout_s: float, *, allow_network: bool = False
@@ -181,6 +185,7 @@ def run_tool(
181185
profile=self.profile,
182186
env=env,
183187
allow_network=allow_network,
188+
extra_protect_paths=self.protect_paths,
184189
timeout_s=float(timeout_s),
185190
)
186191
try:

‎src/agent6/tools/dispatch.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,10 +245,15 @@ def __init__(
245245
graph_client: object | None = None,
246246
run_root_node_id: str | None = None,
247247
mcp_manager: MCPManager | None = None,
248+
extra_protect_paths: tuple[Path, ...] = (),
248249
) -> None:
249250
self._root = root.resolve()
250251
self._config = config
251252
self._sandbox_profile: SandboxProfile = sandbox_profile
253+
# Extra read-only paths layered into every run_command jail on top of
254+
# protect_git/protect_agent6 (e.g. a running machine's own .asm.toml +
255+
# scripts bundle, so an agent state can't rewrite them mid-run).
256+
self._extra_protect_paths = extra_protect_paths
252257
self._approver: _Approver = approver or _default_approver
253258
self._events = events
254259
# Optional GraphClient + root-task id for the DAG-as-tool
@@ -864,6 +869,7 @@ def _run_argv_in_jail(
864869
if self._config.sandbox.protect_agent6:
865870
protect_paths.append((self._root / "agent6.toml").resolve())
866871
protect_paths.append((self._root / self._agent6_dir_name).resolve())
872+
protect_paths.extend(self._extra_protect_paths)
867873
# caller-provided timeout overrides the JailPolicy default
868874
# (600s). Used by verify_command + metric_command for fast failure
869875
# detection on pathological edits.

‎tests/unit/test_machine_tool_egress.py‎

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# SPDX-License-Identifier: Apache-2.0
22
# Copyright 2026 Eric Lesiuta
3-
"""Phase 4: opt-in networked tool states + machine script-bundle validation."""
3+
"""Machine tool egress: per-state allow_network, bundle validation, and the
4+
running machine's files made read-only in run jails (immutability)."""
45

56
from __future__ import annotations
67

@@ -10,7 +11,10 @@
1011

1112
import pytest
1213

13-
from agent6.cli.machine_cmds import _validate_bundle # pyright: ignore[reportPrivateUsage]
14+
from agent6.cli.machine_cmds import (
15+
_machine_protect_paths, # pyright: ignore[reportPrivateUsage]
16+
_validate_bundle, # pyright: ignore[reportPrivateUsage]
17+
)
1418
from agent6.machine import MachineJournal, ToolState, drive, load_machine
1519
from agent6.machine.engine import LiveWorld, ToolExecResult
1620

@@ -149,6 +153,48 @@ def test_liveworld_non_network_tool_is_isolated(
149153
assert seen[-1].allow_network is False
150154

151155

156+
def test_liveworld_passes_protect_paths_to_jail(
157+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
158+
) -> None:
159+
seen = _patch_jail(monkeypatch)
160+
guarded = (tmp_path / "m.asm.toml", tmp_path / "scripts")
161+
world = LiveWorld(
162+
cwd=tmp_path,
163+
journal=MachineJournal(tmp_path / "i"),
164+
profile="strict",
165+
protect_paths=guarded,
166+
)
167+
world.run_tool(("true",), 5.0)
168+
assert seen[-1].extra_protect_paths == guarded
169+
170+
171+
# --- machine-file immutability (_machine_protect_paths) --------------------
172+
173+
174+
def test_protect_paths_include_machine_file_and_scripts(tmp_path: Path) -> None:
175+
f = _write(tmp_path, NET_MACHINE)
176+
(tmp_path / "scripts").mkdir()
177+
got = _machine_protect_paths(f, tmp_path)
178+
assert f.resolve() in got
179+
assert (tmp_path / "scripts").resolve() in got
180+
181+
182+
def test_protect_paths_skip_missing_scripts(tmp_path: Path) -> None:
183+
f = _write(tmp_path, NET_MACHINE) # no scripts/ dir
184+
got = _machine_protect_paths(f, tmp_path)
185+
assert got == (f.resolve(),)
186+
187+
188+
def test_protect_paths_exclude_machine_outside_cwd(tmp_path: Path) -> None:
189+
# A machine file outside the jail-mounted cwd isn't in the child's view, so
190+
# it isn't (and can't be) protected.
191+
outside = tmp_path.parent / "outside.asm.toml"
192+
outside.write_text(NET_MACHINE, encoding="utf-8")
193+
sub = tmp_path / "repo"
194+
sub.mkdir()
195+
assert _machine_protect_paths(outside, sub) == ()
196+
197+
152198
# --- bundle / script-path validation ---------------------------------------
153199

154200

0 commit comments

Comments
 (0)