Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions src/agent_env/env/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,18 +456,22 @@ async def load_file_artifact_universe(
file_artifact_universe: "Any",
destination_path: Optional[str] = None,
) -> "LoadFileArtifactUniverseResult":
"""Stage every FileArtifact in `file_artifact_universe` onto this env's sandbox VM.
"""Stage every FileArtifact in `file_artifact_universe` onto this env's sandbox VM, or, for a sandbox that is
the env's own container (a lone server's), into that container.

Files land at ``<destination>/<filename>``. Nested relative paths
(e.g. ``subdir/file.txt``) are supported — parent dirs are created
as needed. The sandbox fetches each file directly from the object
as needed. A VM fetches each file directly from the object
store through a signed URL where the store can sign one, so the bytes
don't round-trip through this process.

Default implementation uses ``self._sandbox`` (set by ``deploy()`` /
``from_deployed_env()`` on most env subclasses). Subclasses that
manage several sandboxes, or none, should override.
"""
from agent_env.providers.sandbox_providers.sandbox import stage_files_into_container # providers import this module
from agent_env.providers.sandbox_providers.sandbox_provider import SANDBOX_MODE_CONTAINER

sandbox = getattr(self, "_sandbox", None)
if sandbox is None:
raise RuntimeError(
Expand All @@ -486,6 +490,15 @@ async def load_file_artifact_universe(
)
return LoadFileArtifactUniverseResult(destination_path=destination, files={})

total = len(file_artifacts)
if sandbox.mode == SANDBOX_MODE_CONTAINER:
logger.info(
f"Loading FileArtifactUniverse '{file_artifact_universe.id}' "
f"v{file_artifact_universe.version} ({total} file(s)) into the env's container at {destination}"
)
loaded = await stage_files_into_container(sandbox, file_artifacts, destination)
return LoadFileArtifactUniverseResult(destination_path=destination, files=loaded)

dirs_to_make = {destination}
loaded: dict[str, str] = {}
for filename in file_artifacts:
Expand All @@ -499,7 +512,6 @@ async def load_file_artifact_universe(
mkdir_cmd = " && ".join(f"mkdir -p {shlex.quote(d)}" for d in sorted(dirs_to_make))
await sandbox.exec_script(mkdir_cmd)

total = len(file_artifacts)
logger.info(
f"Loading FileArtifactUniverse '{file_artifact_universe.id}' "
f"v{file_artifact_universe.version} ({total} file(s)) at {destination}"
Expand Down
42 changes: 39 additions & 3 deletions tst/integration/env/envs/server_env_local_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
rebuilds each sandbox from disk, removes the container. A load is timed by the size of the payload it staged, through
the deploy's own handle and a restored one; an environment of several files is staged file by file at their paths, in a
folder of the load's own that an earlier load's file can't be in the way of, and handed to the server's data/add
together, timed by their total size; and the server's state reads as JSON whether ``data/get`` answers with
data or with a file bundle. A loop of one's own lists, declares and calls the server's tools through ``ToolSession``,
together, timed by their total size; the server's state reads as JSON whether ``data/get`` answers with
data or with a file bundle; and a file and a file universe a task loads onto the env land in the server's container. A loop of one's own lists, declares and calls the server's tools through ``ToolSession``,
and plays a task's agent as an episode, around the echo agent. Requires a docker daemon; spins up a throwaway
``registry:2`` and skips if it can't start.
"""
Expand All @@ -26,7 +26,7 @@
from agentenv_protocol import ToolSession, ToolSessionError
from agentenv_protocol import client as protocol_v1

from agent_env.artifact import DockerImageArtifact, EnvironmentArtifact, FileArtifact
from agent_env.artifact import DockerImageArtifact, EnvironmentArtifact, FileArtifact, FileArtifactUniverse
from agent_env.artifact.store import reset_artifact_store
from agent_env.config import configure, get_config, reset_config, set_image_store
from agent_env.env import legacy_protocol
Expand All @@ -38,6 +38,7 @@
from agent_env.task import EpisodeSpec, Task
from agent_env.task_step.context import TaskStepContext
from agent_env.task.teardown import TORN_DOWN_KEY
from agent_env.task_step.task_steps.load_artifact import LoadArtifactTaskStep
from agent_env.task_step.task_steps.reset_env import ResetEnvTaskStep
from agent_env.task_step.task_steps.teardown_sandboxes import TeardownSandboxesTaskStep
from tst.integration.utils.registry import REGISTRY_IMAGE
Expand Down Expand Up @@ -188,6 +189,41 @@ async def test_an_environment_of_several_files_loads_each_from_its_path_in_one_a
assert staged_sizes[-1] == sum(map(len, contents.values()))


@pytest.mark.asyncio
async def test_files_a_task_loads_onto_a_server_env_land_in_its_container(local_stack):
uid = uuid.uuid4().hex[:8]
env = MCPServerEnv.put(id=f"server-items-{uid}", docker_image_artifact=_put_items_image(f"server-items-{uid}"),
environment_name="items", env_provider_type="server")
expected = {"note.txt": "a single file\n", "a.txt": "a universe member\n", "sub/b.txt": "a nested member\n"}

def put_file(name):
return FileArtifact.put_bytes(id=f"server-items-files-{name.replace('/', '-')}-{uid}", description=name,
filename=name.rsplit("/", 1)[-1], content=expected[name].encode(),
content_type="text/plain")

note = put_file("note.txt")
universe = FileArtifactUniverse.put(id=f"server-items-universe-{uid}",
file_artifacts={name: put_file(name) for name in ("a.txt", "sub/b.txt")})
deployed = await env.deploy(sandbox_type="local", ttl_seconds=900)
container = f"agent-{deployed.sandbox_id}"
local_stack.append(container)
context = TaskStepContext(deployed_envs=[deployed])

await LoadArtifactTaskStep(
id="load-docs", version=None, env_id=env.id, destination_path="/tmp/docs",
artifacts=[{"id": artifact.id, "version": artifact.version} for artifact in (note, universe)],
).execute(context)

for name, text in expected.items():
read = _docker("exec", container, "cat", f"/tmp/docs/{name}")
assert (read.returncode, read.stdout) == (0, text), read.stderr
assert [(e["artifact_type"], e["env_id"], e["destination_path"], e["files"])
for e in context.metadata["loaded_file_artifact_universes"]] == [
("file", env.id, "/tmp/docs", {"note.txt": "/tmp/docs/note.txt"}),
("file_artifact_universe", env.id, "/tmp/docs", {"a.txt": "/tmp/docs/a.txt", "sub/b.txt": "/tmp/docs/sub/b.txt"}),
]


@pytest.mark.asyncio
async def test_a_deployed_envs_tools_are_listed_and_called_from_a_loop_of_ones_own(local_stack):
"""A trainer's view: the env's tools listed with their schemas and called, the env's data plane agreeing on the state
Expand Down
43 changes: 40 additions & 3 deletions tst/integration/env/envs/server_env_modal_test.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""A server env on Modal: its one MCP server in a Modal container, with no gateway.

A load is timed by the size of the payload it staged, measured in the server's container through the deploy's own
handle and a restored one; and the server's state reads as JSON whether ``data/get`` answers with data or with a file
bundle. Needs the resolved config to reach Modal; skipped otherwise. The server's image is a build context, which
handle and a restored one; the server's state reads as JSON whether ``data/get`` answers with data or with a file
bundle; and a file and a file universe a task loads onto the env land in the server's container. Needs the resolved config to reach Modal; skipped otherwise. The server's image is a build context, which
Modal builds where it runs.
"""

Expand All @@ -17,13 +17,14 @@
import pytest
from agentenv_protocol import client as protocol_v1

from agent_env.artifact import DockerImageArtifact, EnvironmentArtifact, FileArtifact
from agent_env.artifact import DockerImageArtifact, EnvironmentArtifact, FileArtifact, FileArtifactUniverse
from agent_env.env import legacy_protocol
from agent_env.env.env import Env
from agent_env.env.envs.mcp_server import MCPServerEnv
from agent_env.env.gateway import constants
from agent_env.task import EpisodeSpec, Task
from agent_env.task_step.context import TaskStepContext
from agent_env.task_step.task_steps.load_artifact import LoadArtifactTaskStep
from agent_env.task_step.task_steps.teardown_sandboxes import TeardownSandboxesTaskStep
from tst.util.capabilities import skip_without_remote_sandbox

Expand Down Expand Up @@ -59,6 +60,42 @@ async def test_a_modal_load_is_timed_by_its_payload_and_the_servers_state_reads_
)


@pytest.mark.asyncio
async def test_files_a_task_loads_onto_a_modal_server_env_land_in_its_container():
uid = uuid.uuid4().hex[:8]
env = MCPServerEnv.put(id=f"server-items-files-{uid}", docker_image_artifact=_items_context(uid),
environment_name="items", env_provider_type="server")
expected = {"note.txt": "a single file\n", "a.txt": "a universe member\n", "sub/b.txt": "a nested member\n"}

def put_file(name):
return FileArtifact.put_bytes(id=f"server-items-files-{name.replace('/', '-')}-{uid}", description=name,
filename=name.rsplit("/", 1)[-1], content=expected[name].encode(),
content_type="text/plain")

note = put_file("note.txt")
universe = FileArtifactUniverse.put(id=f"server-items-universe-{uid}",
file_artifacts={name: put_file(name) for name in ("a.txt", "sub/b.txt")})
deployed = await env.deploy(sandbox_type="modal", ttl_seconds=900)
context = TaskStepContext(deployed_envs=[deployed])
try:
await LoadArtifactTaskStep(
id="load-docs", version=None, env_id=env.id,
artifacts=[{"id": artifact.id, "version": artifact.version} for artifact in (note, universe)],
).execute(context)

container = (await Env.from_instance_id(deployed.instance_id))._sandbox
for name, text in expected.items():
assert await container.exec_with_output("cat", f"/tmp/file_artifacts/{name}") == (0, text, "")
assert [(e["artifact_type"], e["env_id"], e["destination_path"], e["files"])
for e in context.metadata["loaded_file_artifact_universes"]] == [
("file", env.id, "/tmp/file_artifacts", {"note.txt": "/tmp/file_artifacts/note.txt"}),
("file_artifact_universe", env.id, "/tmp/file_artifacts",
{"a.txt": "/tmp/file_artifacts/a.txt", "sub/b.txt": "/tmp/file_artifacts/sub/b.txt"}),
]
finally:
await TeardownSandboxesTaskStep(id="teardown", version=None, env_ids=[env.id]).execute(context)


VERIFY_CRATE = """
from agentenv_protocol import client

Expand Down
79 changes: 79 additions & 0 deletions tst/unit/env/load_file_artifact_universe_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
"""An env's default file-universe load: onto its VM host when its sandbox is a VM, into its container when its sandbox is
the server's own container, as a lone server's is on Modal, which has no VM host to stage on."""

from __future__ import annotations

from types import SimpleNamespace
from unittest.mock import AsyncMock, MagicMock

import pytest

from agent_env.env.envs.mcp_server import MCPServerEnv
from agent_env.providers.sandbox_providers.modal_sandbox import ModalSandbox
from agent_env.providers.sandbox_providers.sandbox import VmSandbox

FILES = {"a.txt": "s3://bucket/a.txt", "sub/b.txt": "s3://bucket/b.txt"}


def _universe():
files = {name: SimpleNamespace(object_url=url) for name, url in FILES.items()}
return SimpleNamespace(id="docs", version=3, get_file_artifacts=lambda: files)


def _env(sandbox) -> MCPServerEnv:
env = MCPServerEnv(id="mcp-docs", version=1, docker_image_artifact=MagicMock(), environment_name="docs",
env_provider_type="server")
env._sandbox = sandbox
return env


@pytest.mark.asyncio
async def test_a_lone_servers_container_on_modal_is_written_directly():
sandbox = AsyncMock(spec=ModalSandbox)
sandbox.mode = "container"

result = await _env(sandbox).load_file_artifact_universe(_universe(), "/app/loaded/")

assert sorted(c.args for c in sandbox.exec.await_args_list) == [
("mkdir", "-p", "/app/loaded"), ("mkdir", "-p", "/app/loaded/sub"),
]
assert sorted(c.args for c in sandbox.write_file_from_object.await_args_list) == [
("s3://bucket/a.txt", "/app/loaded/a.txt"), ("s3://bucket/b.txt", "/app/loaded/sub/b.txt"),
]
assert result.destination_path == "/app/loaded"
assert result.files == {"a.txt": "/app/loaded/a.txt", "sub/b.txt": "/app/loaded/sub/b.txt"}


@pytest.mark.asyncio
async def test_a_lone_servers_container_on_a_vm_is_written_through_its_container():
sandbox = AsyncMock(spec=VmSandbox)
sandbox.mode = "container"
sandbox.container_name = "env-local-1"

result = await _env(sandbox).load_file_artifact_universe(_universe())

assert sorted(c.args[0] for c in sandbox.exec_script.await_args_list) == [
"docker exec -u 0 env-local-1 mkdir -p /tmp/file_artifacts",
"docker exec -u 0 env-local-1 mkdir -p /tmp/file_artifacts/sub",
]
assert sorted(c.args for c in sandbox.write_file_from_object.await_args_list) == [
("s3://bucket/a.txt", "/tmp/file_artifacts/a.txt"), ("s3://bucket/b.txt", "/tmp/file_artifacts/sub/b.txt"),
]
sandbox.load_object_file.assert_not_awaited()
assert result.files == {"a.txt": "/tmp/file_artifacts/a.txt", "sub/b.txt": "/tmp/file_artifacts/sub/b.txt"}


@pytest.mark.asyncio
async def test_a_vm_is_loaded_on_its_host():
sandbox = AsyncMock(spec=VmSandbox)
sandbox.mode = "vm"

result = await _env(sandbox).load_file_artifact_universe(_universe(), "/app/loaded")

sandbox.exec_script.assert_awaited_once_with("mkdir -p /app/loaded && mkdir -p /app/loaded/sub")
assert [c.args for c in sandbox.load_object_file.await_args_list] == [
("s3://bucket/a.txt", "/app/loaded/a.txt"), ("s3://bucket/b.txt", "/app/loaded/sub/b.txt"),
]
sandbox.write_file_from_object.assert_not_awaited()
sandbox.exec.assert_not_awaited()
assert result.files == {"a.txt": "/app/loaded/a.txt", "sub/b.txt": "/app/loaded/sub/b.txt"}
2 changes: 2 additions & 0 deletions tst/unit/task_step/test_load_artifact_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
class _Sandbox:
"""Records every command and S3 pull a loader sends to a sandbox."""

mode = "vm"

def scoped_name(self, name):
return name

Expand Down
Loading