From 4d944bb2abac1433418959c11aa09420a040c10e Mon Sep 17 00:00:00 2001 From: Sarah Chitty Date: Mon, 23 Nov 2026 19:56:10 -0800 Subject: [PATCH 1/4] fix(sandbox): load docker images in batches under the exec argument limit _load_docker_images ran every image's curl-of-a-signed-URL in one bash -c script. Signed URLs with a session token are ~2.4 KB each, so a MultiEnv with 36 images (14 MCP servers and 11 websites, two images each) made an 86 KB script and Modal's VM sandbox refused it: "Total length of CMD arguments cannot exceed 65536 bytes (ARG_MAX)". The loads now go in scripts of at most 60,000 bytes, each in parallel and the scripts one after another. Co-Authored-By: Claude Opus 5.5 --- .../providers/sandbox_providers/sandbox.py | 25 +++++++++++++- .../sandbox_providers/load_scripts_test.py | 34 +++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 tst/unit/providers/sandbox_providers/load_scripts_test.py diff --git a/src/agent_env/providers/sandbox_providers/sandbox.py b/src/agent_env/providers/sandbox_providers/sandbox.py index b784d21d8..6c970b114 100644 --- a/src/agent_env/providers/sandbox_providers/sandbox.py +++ b/src/agent_env/providers/sandbox_providers/sandbox.py @@ -53,6 +53,28 @@ # sandbox id, so a sandbox that shares its host (the local one) can remove its own when it terminates. SANDBOX_LABEL = "agentenv.sandbox" +# The most bytes one image-load script may hold. A load is a curl of a signed URL (about 2.4 KB with a session token) +# per image, run in parallel in one `bash -c` script, and a provider caps one exec's arguments: Modal's VM sandbox at +# 65536 bytes (ARG_MAX). A MultiEnv with many images (14 MCP servers and 11 websites, two images each) passes that, so +# the loads go in batches under this size, each batch in parallel and the batches one after another. +_MAX_LOAD_SCRIPT_BYTES = 60_000 + + +def _load_scripts(load_commands: list[str], limit: int = _MAX_LOAD_SCRIPT_BYTES) -> list[str]: + """``load_commands`` as `bash -c` scripts that each run their commands in parallel and wait, each at most ``limit`` + bytes. A command too long to fit with any other gets a script of its own.""" + scripts: list[str] = [] + batch: list[str] = [] + for command in load_commands: + if batch and len((" & ".join([*batch, command]) + " & wait").encode()) > limit: + scripts.append(" & ".join(batch) + " & wait") + batch = [] + batch.append(command) + if batch: + scripts.append(" & ".join(batch) + " & wait") + return scripts + + # At most this many image signs in flight per event loop: each holds a worker thread, for up to a # remote signer's whole retry window, and a connection from its session's pool. _CONCURRENT_SIGNS = 8 @@ -520,7 +542,8 @@ async def _load_docker_images(self, artifacts: list, signed_urls: list[str | Non f"(gunzip -c {shlex.quote(tmp_tar)} | docker load && rm -f {shlex.quote(tmp_tar)})" ) logger.info(f" Queued: {artifact.image_name}") - await self.exec_script(" & ".join(load_commands) + " & wait", max_retries=2) + for script in _load_scripts(load_commands): + await self.exec_script(script, max_retries=2) logger.info("Verifying Docker images...") exit_code, stdout, stderr = await self.exec_with_output("sudo", "docker", "images") diff --git a/tst/unit/providers/sandbox_providers/load_scripts_test.py b/tst/unit/providers/sandbox_providers/load_scripts_test.py new file mode 100644 index 000000000..7b3b2c9e9 --- /dev/null +++ b/tst/unit/providers/sandbox_providers/load_scripts_test.py @@ -0,0 +1,34 @@ +"""Image loads go in scripts small enough for a provider's exec argument limit (Modal's VM sandbox: 65536 bytes).""" + +from agent_env.providers.sandbox_providers.sandbox import _MAX_LOAD_SCRIPT_BYTES, _load_scripts + + +def _command(i: int, url_bytes: int = 2400) -> str: + # The shape _load_docker_images writes: a curl of a signed URL, then docker load. + return f"(curl -fsSL 'https://bucket.s3.amazonaws.com/img{i}.tar.gz?{'x' * url_bytes}' -o /tmp/i{i} && docker load)" + + +def test_a_few_images_stay_in_one_script(): + commands = [_command(i) for i in range(3)] + assert _load_scripts(commands) == [" & ".join(commands) + " & wait"] + + +def test_many_images_split_under_the_limit_and_keep_every_command_once_in_order(): + commands = [_command(i) for i in range(36)] # 14 MCP servers and 11 websites (two images each): about 86 KB + scripts = _load_scripts(commands) + assert len(scripts) > 1 + assert all(len(s.encode()) <= _MAX_LOAD_SCRIPT_BYTES for s in scripts) + assert all(s.endswith(" & wait") for s in scripts) + back = [c for s in scripts for c in s.removesuffix(" & wait").split(" & ")] + assert back == commands + + +def test_a_command_longer_than_the_limit_gets_a_script_of_its_own(): + big = _command(0, url_bytes=_MAX_LOAD_SCRIPT_BYTES + 10) + small = [_command(i) for i in range(1, 3)] + scripts = _load_scripts([small[0], big, small[1]]) + assert scripts == [small[0] + " & wait", big + " & wait", small[1] + " & wait"] + + +def test_no_images_no_scripts(): + assert _load_scripts([]) == [] From 49e457731dae05492ee9eca497146453e775fe80 Mon Sep 17 00:00:00 2001 From: Sarah Chitty Date: Tue, 24 Nov 2026 12:07:32 -0800 Subject: [PATCH 2/4] fix(modal_vm): send an exec's argv over stdin past Modal's exec limit Moves the fix from Sandbox (batched image loads) into ModalVmSandbox.exec, per review: an argv past 32 KiB is written NUL-separated to stdin and read back with mapfile, as kosa8 does through a file. Every image load stays parallel, any long exec on modal_vm is covered, and the shared load path is unchanged for other providers. Co-Authored-By: Claude Opus 5.5 --- .../sandbox_providers/modal_vm_sandbox.py | 19 ++++++++- .../providers/sandbox_providers/sandbox.py | 25 +---------- .../sandbox_providers/load_scripts_test.py | 34 --------------- .../modal_vm_sandbox_test.py | 41 +++++++++++++++++++ 4 files changed, 60 insertions(+), 59 deletions(-) delete mode 100644 tst/unit/providers/sandbox_providers/load_scripts_test.py diff --git a/src/agent_env/providers/sandbox_providers/modal_vm_sandbox.py b/src/agent_env/providers/sandbox_providers/modal_vm_sandbox.py index f54d90f1a..32ec5a0c0 100644 --- a/src/agent_env/providers/sandbox_providers/modal_vm_sandbox.py +++ b/src/agent_env/providers/sandbox_providers/modal_vm_sandbox.py @@ -130,6 +130,12 @@ _DISK_IDLE_TIMEOUT_S = 30 * 60 +#: The most bytes of arguments an exec sends as argv; Modal rejects one past ~48 KB. A longer one goes over stdin. +_MAX_EXEC_ARG_BYTES = 32 * 1024 +#: Reads a NUL-separated argv from stdin and runs it, with no input of its own. +_FROM_STDIN = 'mapfile -d "" -t a && exec "${a[@]}" tuple[int, str, str]: diff --git a/src/agent_env/providers/sandbox_providers/sandbox.py b/src/agent_env/providers/sandbox_providers/sandbox.py index 6c970b114..b784d21d8 100644 --- a/src/agent_env/providers/sandbox_providers/sandbox.py +++ b/src/agent_env/providers/sandbox_providers/sandbox.py @@ -53,28 +53,6 @@ # sandbox id, so a sandbox that shares its host (the local one) can remove its own when it terminates. SANDBOX_LABEL = "agentenv.sandbox" -# The most bytes one image-load script may hold. A load is a curl of a signed URL (about 2.4 KB with a session token) -# per image, run in parallel in one `bash -c` script, and a provider caps one exec's arguments: Modal's VM sandbox at -# 65536 bytes (ARG_MAX). A MultiEnv with many images (14 MCP servers and 11 websites, two images each) passes that, so -# the loads go in batches under this size, each batch in parallel and the batches one after another. -_MAX_LOAD_SCRIPT_BYTES = 60_000 - - -def _load_scripts(load_commands: list[str], limit: int = _MAX_LOAD_SCRIPT_BYTES) -> list[str]: - """``load_commands`` as `bash -c` scripts that each run their commands in parallel and wait, each at most ``limit`` - bytes. A command too long to fit with any other gets a script of its own.""" - scripts: list[str] = [] - batch: list[str] = [] - for command in load_commands: - if batch and len((" & ".join([*batch, command]) + " & wait").encode()) > limit: - scripts.append(" & ".join(batch) + " & wait") - batch = [] - batch.append(command) - if batch: - scripts.append(" & ".join(batch) + " & wait") - return scripts - - # At most this many image signs in flight per event loop: each holds a worker thread, for up to a # remote signer's whole retry window, and a connection from its session's pool. _CONCURRENT_SIGNS = 8 @@ -542,8 +520,7 @@ async def _load_docker_images(self, artifacts: list, signed_urls: list[str | Non f"(gunzip -c {shlex.quote(tmp_tar)} | docker load && rm -f {shlex.quote(tmp_tar)})" ) logger.info(f" Queued: {artifact.image_name}") - for script in _load_scripts(load_commands): - await self.exec_script(script, max_retries=2) + await self.exec_script(" & ".join(load_commands) + " & wait", max_retries=2) logger.info("Verifying Docker images...") exit_code, stdout, stderr = await self.exec_with_output("sudo", "docker", "images") diff --git a/tst/unit/providers/sandbox_providers/load_scripts_test.py b/tst/unit/providers/sandbox_providers/load_scripts_test.py deleted file mode 100644 index 7b3b2c9e9..000000000 --- a/tst/unit/providers/sandbox_providers/load_scripts_test.py +++ /dev/null @@ -1,34 +0,0 @@ -"""Image loads go in scripts small enough for a provider's exec argument limit (Modal's VM sandbox: 65536 bytes).""" - -from agent_env.providers.sandbox_providers.sandbox import _MAX_LOAD_SCRIPT_BYTES, _load_scripts - - -def _command(i: int, url_bytes: int = 2400) -> str: - # The shape _load_docker_images writes: a curl of a signed URL, then docker load. - return f"(curl -fsSL 'https://bucket.s3.amazonaws.com/img{i}.tar.gz?{'x' * url_bytes}' -o /tmp/i{i} && docker load)" - - -def test_a_few_images_stay_in_one_script(): - commands = [_command(i) for i in range(3)] - assert _load_scripts(commands) == [" & ".join(commands) + " & wait"] - - -def test_many_images_split_under_the_limit_and_keep_every_command_once_in_order(): - commands = [_command(i) for i in range(36)] # 14 MCP servers and 11 websites (two images each): about 86 KB - scripts = _load_scripts(commands) - assert len(scripts) > 1 - assert all(len(s.encode()) <= _MAX_LOAD_SCRIPT_BYTES for s in scripts) - assert all(s.endswith(" & wait") for s in scripts) - back = [c for s in scripts for c in s.removesuffix(" & wait").split(" & ")] - assert back == commands - - -def test_a_command_longer_than_the_limit_gets_a_script_of_its_own(): - big = _command(0, url_bytes=_MAX_LOAD_SCRIPT_BYTES + 10) - small = [_command(i) for i in range(1, 3)] - scripts = _load_scripts([small[0], big, small[1]]) - assert scripts == [small[0] + " & wait", big + " & wait", small[1] + " & wait"] - - -def test_no_images_no_scripts(): - assert _load_scripts([]) == [] diff --git a/tst/unit/providers/sandbox_providers/modal_vm_sandbox_test.py b/tst/unit/providers/sandbox_providers/modal_vm_sandbox_test.py index 036351aa3..00a600b84 100644 --- a/tst/unit/providers/sandbox_providers/modal_vm_sandbox_test.py +++ b/tst/unit/providers/sandbox_providers/modal_vm_sandbox_test.py @@ -53,6 +53,47 @@ async def fake_exec(*cmd, text=False): assert captured["cmd"] == ("docker", "images") +@pytest.mark.asyncio +async def test_exec_past_modals_arg_limit_sends_the_argv_over_stdin(): + # A parallel load of many signed image URLs runs past Modal's exec limit; the argv goes over stdin instead. + sb = MagicMock() + sb.object_id = "sb-test" + captured: dict = {} + process = MagicMock() + process.stdin.drain.aio = AsyncMock() + + async def fake_exec(*cmd, text=False): + captured["cmd"] = cmd + return process + + sb.exec.aio = fake_exec + script = " & ".join(f"(curl -fsSL 'https://b/img{i}?{'x' * 2400}' | docker load)" for i in range(36)) + " & wait" + await ModalVmSandbox(sb, {}).exec("sudo", "bash", "-c", script) + + assert captured["cmd"] == ("bash", "-c", mvs._FROM_STDIN) + sent = b"".join(call.args[0] for call in process.stdin.write.call_args_list) + assert sent == b"bash\0-c\0" + script.encode() + b"\0" + assert all(len(call.args[0]) <= mvs._MAX_EXEC_ARG_BYTES for call in process.stdin.write.call_args_list) + process.stdin.write_eof.assert_called_once() + + +def _bash_has_mapfile_d() -> bool: + import subprocess + + return subprocess.run(["bash", "-c", 'mapfile -d "" a Date: Tue, 24 Nov 2026 14:41:31 -0800 Subject: [PATCH 3/4] fix(modal_vm): run a long -c script from a file, past Linux's 128 KiB argument limit Addresses Greptile: over stdin the argv dodged Modal's limit, but exec still passed a -c script as one argument, which Linux caps at 128 KiB (MAX_ARG_STRLEN). The wrapper now writes the script to a temp file and evals it from there, removing the file first, so $0 and $@ are kept. Tested with a 200 KiB script. Co-Authored-By: Claude Opus 5.5 --- .../sandbox_providers/modal_vm_sandbox.py | 10 ++++++++-- .../sandbox_providers/modal_vm_sandbox_test.py | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/agent_env/providers/sandbox_providers/modal_vm_sandbox.py b/src/agent_env/providers/sandbox_providers/modal_vm_sandbox.py index 32ec5a0c0..3c4448141 100644 --- a/src/agent_env/providers/sandbox_providers/modal_vm_sandbox.py +++ b/src/agent_env/providers/sandbox_providers/modal_vm_sandbox.py @@ -132,8 +132,14 @@ #: The most bytes of arguments an exec sends as argv; Modal rejects one past ~48 KB. A longer one goes over stdin. _MAX_EXEC_ARG_BYTES = 32 * 1024 -#: Reads a NUL-separated argv from stdin and runs it, with no input of its own. -_FROM_STDIN = 'mapfile -d "" -t a && exec "${a[@]}" "$f" && a[2]="eval \"\$(cat -- $f && rm -f -- $f)\""; fi && ' + 'exec "${a[@]}" Date: Tue, 24 Nov 2026 15:01:55 -0800 Subject: [PATCH 4/4] fix(modal_vm): move only a shell's -c script into a file Addresses Greptile: the wrapper rewrote any -c, so a long python3 -c got the eval as its source. Only bash or sh (by basename) has its -c script run from a file now; another program's arguments go through unchanged. Co-Authored-By: Claude Opus 5.5 --- .../sandbox_providers/modal_vm_sandbox.py | 5 +++-- .../sandbox_providers/modal_vm_sandbox_test.py | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/agent_env/providers/sandbox_providers/modal_vm_sandbox.py b/src/agent_env/providers/sandbox_providers/modal_vm_sandbox.py index 3c4448141..6d804ad6f 100644 --- a/src/agent_env/providers/sandbox_providers/modal_vm_sandbox.py +++ b/src/agent_env/providers/sandbox_providers/modal_vm_sandbox.py @@ -133,10 +133,11 @@ #: The most bytes of arguments an exec sends as argv; Modal rejects one past ~48 KB. A longer one goes over stdin. _MAX_EXEC_ARG_BYTES = 32 * 1024 #: Reads a NUL-separated argv from stdin and runs it, with no input of its own. Linux caps one argument at 128 KiB -#: (MAX_ARG_STRLEN), so a ``-c`` script goes into a file that the shell reads, removes and evals, keeping its $0 and $@. +#: (MAX_ARG_STRLEN), so a shell's ``-c`` script goes into a file that the shell reads, removes and evals, keeping its $0 +#: and $@. Another program's ``-c`` (python3, say) is left as it is. _FROM_STDIN = ( 'mapfile -d "" -t a && ' - 'if [ "${#a[@]}" -ge 3 ] && [ "${a[1]}" = -c ]; then ' + 'if [ "${#a[@]}" -ge 3 ] && [ "${a[1]}" = -c ] && case "${a[0]##*/}" in bash|sh) true;; *) false;; esac; then ' r'f=$(mktemp) && printf %s "${a[2]}" > "$f" && a[2]="eval \"\$(cat -- $f && rm -f -- $f)\""; fi && ' 'exec "${a[@]}"