diff --git a/src/agent_env/providers/sandbox_providers/modal_sandbox.py b/src/agent_env/providers/sandbox_providers/modal_sandbox.py index 43c9907de..4b369c432 100644 --- a/src/agent_env/providers/sandbox_providers/modal_sandbox.py +++ b/src/agent_env/providers/sandbox_providers/modal_sandbox.py @@ -303,10 +303,14 @@ def __init__( gpu: Optional[str] = None, ecr_pull_secret_name: Optional[str] = None, environment_name: Optional[str] = None, + idle_timeout: Optional[int] = None, ): + """``idle_timeout`` (``[sandbox.providers.modal.config]``) ends each sandbox once it has been idle that many + seconds, with no connection open and no command running; unset, a sandbox lasts until its ``timeout``.""" self._app_name = _resolve_app_base_name(app_name) self._ecr_pull_secret_name = ecr_pull_secret_name self._environment_name = environment_name + self._idle_timeout = idle_timeout self._client: Optional[modal.Client] = None # GPU sandboxes use the V1 backend, which supports Docker in gVisor but not i6pn. self._gpu = gpu @@ -415,7 +419,7 @@ async def create_container( logger.info( f"Creating Modal sandbox from image {image_name} " f"(app={app_name}, tags={app_tags}, sandbox_tags={sandbox_tags}, port={port}, " - f"cpu={cpu}, memory={memory}MB, " + f"cpu={cpu}, memory={memory}MB, idle_timeout={self._idle_timeout}, " f"gpu={self._gpu}, i6pn={i6pn}, region={region}, expose_externally={expose_externally})" ) client = await self._get_client() @@ -429,7 +433,7 @@ async def create_container( # Private networking requires the experimental factory; other calls use the public API. create_kwargs = dict( - app=app, cpu=cpu, memory=memory, timeout=timeout, + app=app, cpu=cpu, memory=memory, timeout=timeout, idle_timeout=self._idle_timeout, env=env or {}, client=client, **port_kwargs, tags=sandbox_tags or None, readiness_probe=modal.Probe.with_exec("true"), diff --git a/tst/unit/providers/sandbox_providers/modal_idle_timeout_wiring_test.py b/tst/unit/providers/sandbox_providers/modal_idle_timeout_wiring_test.py new file mode 100644 index 000000000..c6232eb8d --- /dev/null +++ b/tst/unit/providers/sandbox_providers/modal_idle_timeout_wiring_test.py @@ -0,0 +1,42 @@ +"""The configured ``idle_timeout`` must reach Modal's create call for a container sandbox. + +A sandbox created without one is ended by Scale's Modal budget enforcement, which ends sandboxes that have no idle +timeout, so losing the pass-through would kill agent containers mid-run. +""" + +from unittest.mock import AsyncMock, MagicMock, patch + +import pytest + +from agent_env.providers.sandbox_providers.modal_sandbox import ModalSandboxProvider + + +class _Stop(Exception): + """Raised from the patched create so the test stops once the kwargs are captured.""" + + +async def _create_kwargs(provider: ModalSandboxProvider) -> dict: + captured: dict = {} + + async def fake_create(*args, **kwargs): + captured.update(kwargs) + raise _Stop + + create = MagicMock() + create.aio = fake_create + provider._get_app = AsyncMock(return_value=MagicMock()) + provider._get_client = AsyncMock(return_value=MagicMock()) + with patch("modal.Sandbox.create", create), pytest.raises(Exception): + await provider.create_container(image_name="public.ecr.aws/x/y:1", port=8000, env={}) + return captured + + +@pytest.mark.asyncio +async def test_modal_container_sends_the_configured_idle_timeout(): + assert (await _create_kwargs(ModalSandboxProvider(idle_timeout=1800)))["idle_timeout"] == 1800 + + +@pytest.mark.asyncio +async def test_modal_container_sends_no_idle_timeout_when_unset(): + """Unset keeps Modal's default: the sandbox lasts until its timeout.""" + assert (await _create_kwargs(ModalSandboxProvider()))["idle_timeout"] is None