diff --git a/src/agent_env/providers/sandbox_providers/sandbox.py b/src/agent_env/providers/sandbox_providers/sandbox.py index addb4c409..0986f795c 100644 --- a/src/agent_env/providers/sandbox_providers/sandbox.py +++ b/src/agent_env/providers/sandbox_providers/sandbox.py @@ -599,12 +599,13 @@ async def _write_bytes_to_vm_path(self, data: bytes, vm_path: str) -> None: if len(encoded) <= self._WFT_CHUNK_BYTES: await self.exec_script(f"base64 -d <<'ENDB64' > {shlex.quote(vm_path)}\n{encoded}\nENDB64") return - # Too big for one heredoc arg: append the (shell-safe) base64 in bounded chunks. + # Too big for one heredoc arg: append the (shell-safe) base64 in bounded chunks, then decode it from stdin, + # since macOS's base64, a local sandbox's host's there, takes no file argument. vm_b64 = f"{vm_path}.b64" await self.exec_script(f": > {shlex.quote(vm_b64)}") for i in range(0, len(encoded), self._WFT_CHUNK_BYTES): await self.exec_script(f"printf '%s' {shlex.quote(encoded[i:i + self._WFT_CHUNK_BYTES])} >> {shlex.quote(vm_b64)}") - await self.exec_script(f"base64 -d {shlex.quote(vm_b64)} > {shlex.quote(vm_path)} && rm -f {shlex.quote(vm_b64)}") + await self.exec_script(f"base64 -d < {shlex.quote(vm_b64)} > {shlex.quote(vm_path)} && rm -f {shlex.quote(vm_b64)}") async def write_host_file(self, data: bytes, vm_path: str) -> None: """Write bytes to vm_path on the VM host itself, not into the agent container.""" diff --git a/tst/unit/providers/sandbox_providers/vm_sandbox_test.py b/tst/unit/providers/sandbox_providers/vm_sandbox_test.py index a81777801..cc149110a 100644 --- a/tst/unit/providers/sandbox_providers/vm_sandbox_test.py +++ b/tst/unit/providers/sandbox_providers/vm_sandbox_test.py @@ -2,6 +2,7 @@ import hashlib import io import re +import subprocess import threading import time from concurrent.futures import ThreadPoolExecutor @@ -525,7 +526,7 @@ async def test_write_file_from_text_large_chunks_reconstruct_exactly(): # The reconstructed base64 must equal the original, byte-for-byte. assert _b64_from_chunk_scripts(sandbox.scripts, b64_path) == expected_b64 # And it's decoded into the real path before the docker cp. - assert any(f"base64 -d {b64_path} > " in s for s in sandbox.scripts) + assert any(f"base64 -d < {b64_path} > " in s for s in sandbox.scripts) class _CleanupFailingSandbox(VmSandbox): @@ -759,6 +760,22 @@ async def test_a_load_bounds_how_many_signs_run_at_once(): assert 1 < store.most <= sandbox_module._CONCURRENT_SIGNS +@pytest.mark.asyncio +async def test_a_file_written_on_the_host_in_chunks_decodes_with_this_machines_own_base64(tmp_path): + """The scripts a chunked write runs, run here: a local sandbox's host is this machine, whose base64 may be macOS's, + which takes no file argument.""" + sandbox = _ScriptRecorder() + data = bytes(range(256)) * 1024 + target = tmp_path / "input.bin" + await sandbox.write_host_file(data, str(target)) + + for script in sandbox.scripts: + ran = subprocess.run(["bash", "-c", script], capture_output=True, text=True) + assert ran.returncode == 0, (script[:80], ran.stderr) + + assert target.read_bytes() == data and not (tmp_path / "input.bin.b64").exists() + + @pytest.mark.asyncio async def test_write_host_file_writes_on_the_host_in_bounded_chunks(): import base64 @@ -771,7 +788,7 @@ async def test_write_host_file_writes_on_the_host_in_bounded_chunks(): assert not any("docker" in s for s in sandbox.scripts) b64_path = "/tmp/agentenv_run_code/input.json.b64" assert _b64_from_chunk_scripts(sandbox.scripts, b64_path) == base64.b64encode(data).decode() - assert sandbox.scripts[-1] == f"base64 -d {b64_path} > /tmp/agentenv_run_code/input.json && rm -f {b64_path}" + assert sandbox.scripts[-1] == f"base64 -d < {b64_path} > /tmp/agentenv_run_code/input.json && rm -f {b64_path}" class _ArgsRecorder(VmSandbox):