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
6 changes: 5 additions & 1 deletion src/agent_env/env/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,9 @@ class DeployedGatewayEnv(DeployedSandboxEnv):
# Each child env on a sandbox of its own, which the gateway fronts (a multi env's computers): its own deploy record,
# by its environment name.
child_envs: dict[str, dict] = field(default_factory=dict)
# Where the gateway's stack lives when it is one slot of a VM other runs share (StackLayout.to_record); None for
# the default stack, which has the VM to itself.
stack_layout: Optional[dict] = None

@classmethod
def _fields_from(cls, data: dict) -> dict:
Expand All @@ -252,6 +255,7 @@ def _fields_from(cls, data: dict) -> dict:
"vnc_url": data.get("vnc_url"),
"env_state_instance_ids": data.get("env_state_instance_ids") or [],
"child_envs": data.get("child_envs") or {},
"stack_layout": data.get("stack_layout"),
}


Expand Down Expand Up @@ -584,4 +588,4 @@ def _record_class(data: dict) -> type[DeployedEnv]:

# A record carrying any of these is a gateway's. Not gateway_mode: an older kernel writes its default into any record it round-trips.
_GATEWAY_ONLY_FIELDS = ("gateway_url", "db_web_url", "db_mcp_url", "website_frontend_urls", "vnc_url", "env_state_instance_ids",
"child_envs")
"child_envs", "stack_layout")
7 changes: 7 additions & 0 deletions src/agent_env/env/envs/_deployment.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from agent_env.env.env import DeployedSandboxEnv, gateway_url_of
from agent_env.env.gateway.constants import DATA_PLANE_LOAD_TIMEOUT_S
from agent_env.env.store import register_env_instance
from agent_env.providers.env_providers.stack_layout import DEFAULT_STACK_LAYOUT, StackLayout
from agent_env.store.base import ObjectNotFoundError
from agent_env.utils.paths import validate_file_name

Expand Down Expand Up @@ -322,6 +323,12 @@ def as_builtin(provider: Optional[EnvironmentProvider]) -> Optional[_SandboxEnvi
return provider if isinstance(provider, _SandboxEnvironmentProvider) else None


def stack_layout_of(provider: Optional[EnvironmentProvider]) -> StackLayout:
"""Where a built-in provider's stack lives in its sandbox; the default for any other provider, or none."""
builtin = as_builtin(provider)
return builtin.stack_layout if builtin is not None else DEFAULT_STACK_LAYOUT


def provider_options(env: Env, provider: EnvironmentProvider, **options) -> dict:
"""The options provider takes: every one for a deploy(**options), else those it names, one it doesn't being dropped at deploy()'s
default; raises for a deploy refused (see ``deploy_refusal``)."""
Expand Down
5 changes: 2 additions & 3 deletions src/agent_env/env/envs/mcp_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from agent_env.env.envs._deployment import (
as_builtin, builtin_provider_for, close_deployed, close_replaced, deploy_refusal, deploy_through_provider, host_staging_refusal,
data_plane_headers, file_parts, legacy_load_file, load_by_signed_url, plugin_provider_like_a_builtin, provider_or_class,
reset_server_data,
reset_server_data, stack_layout_of,
staged_paths,
)
from agent_env.env.gateway import GatewayMode
Expand Down Expand Up @@ -260,8 +260,7 @@ async def _copy_artifact_into_container(self, file_artifact, path: str) -> str:
# Stage on the disk-backed app dir, not /tmp (tmpfs/RAM): a large
# artifact (github ~8GB) OOM-kills the copy under concurrent loads.
# Random suffix avoids collisions between concurrent loads.
from agent_env.providers.env_providers.constants import GATEWAY_APP_DIR
stage_dir = f"{GATEWAY_APP_DIR}/_artifact_staging"
stage_dir = stack_layout_of(self._env_provider).artifact_staging_dir
vm_temp_path = f"{stage_dir}/{self.environment_name}-{uuid.uuid4().hex[:8]}-{posixpath.basename(path)}"
await self._sandbox.exec_script(f"mkdir -p {shlex.quote(stage_dir)}")
await self._sandbox.load_object_file(file_artifact.object_url, vm_temp_path)
Expand Down
30 changes: 16 additions & 14 deletions src/agent_env/env/envs/multi_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def _snapshot_after_load_default() -> bool:
from agent_env.env.gateway import GatewayMode
from agent_env.env.envs._deployment import (
as_builtin, builtin_provider_for, close_deployed, close_replaced, deploy_refusal, deploy_through_provider, host_staging_refusal,
plugin_provider_like_a_builtin, provider_or_class, reset_server_data,
plugin_provider_like_a_builtin, provider_or_class, reset_server_data, stack_layout_of,
)
from agent_env.env import store as env_store
from agent_env.env.envs import _multi_computers
Expand All @@ -52,7 +52,7 @@ def _snapshot_after_load_default() -> bool:
from agent_env.env.envs.mcp_server import MCPServerEnv
from agent_env.env.envs.website import WebsiteEnv
from agent_env.env.envs.service_db import DATABASE_SERVICE_NAME
from agent_env.providers.env_providers.constants import DOCKER_COMPOSE_PATH
from agent_env.providers.env_providers.stack_layout import DEFAULT_STACK_LAYOUT
from agent_env.providers.env_state.local_postgres import INIT_SCRIPT_MOUNT_PATH
from agent_env.store.base import NotFoundError
from agent_env.store.ids import derive_id
Expand Down Expand Up @@ -117,14 +117,14 @@ def browser_problems(envs: list[Env]) -> list[str]:
_SERVICE_DB_IMAGE = re.compile(rf"^( {re.escape(DATABASE_SERVICE_NAME)}:\n image: ).*$", re.MULTILINE)


def snapshot_compose(compose: str, db_image: str) -> str:
"""``compose``, the file a deploy wrote, with its servicedb started from ``db_image`` and without the schema init
script, whose work a snapshot already holds. Every other service stays as the deploy wrote it."""
def snapshot_compose(compose: str, db_image: str, *, compose_file: str = DEFAULT_STACK_LAYOUT.compose_file) -> str:
"""``compose``, the file a deploy wrote at ``compose_file``, with its servicedb started from ``db_image`` and without
the schema init script, whose work a snapshot already holds. Every other service stays as the deploy wrote it."""
if problem := image_reference_problem(db_image):
raise ValueError(problem)
swapped, count = _SERVICE_DB_IMAGE.subn(lambda match: match.group(1) + db_image, compose)
if count != 1:
raise RuntimeError(f"{DOCKER_COMPOSE_PATH} names a {DATABASE_SERVICE_NAME} image {count} times, not once, so "
raise RuntimeError(f"{compose_file} names a {DATABASE_SERVICE_NAME} image {count} times, not once, so "
"the snapshot can't replace it")
return swapped.replace(f" volumes:\n - ./init-schemas.sql:{INIT_SCRIPT_MOUNT_PATH}\n", "")

Expand Down Expand Up @@ -711,7 +711,7 @@ async def _resumable_environments(self, environment_universe_artifact, total: in
))
if not recorded:
return set()
if not await _check_changelog_empty(self._sandbox):
if not await _check_changelog_empty(self._sandbox, layout=stack_layout_of(self._env_provider)):
logger.info(
"Not resuming: the changelog is non-empty, so this env's data has been "
"modified since it was loaded and every service must be re-seeded"
Expand Down Expand Up @@ -880,14 +880,14 @@ async def _load_from_snapshot(self, snapshot) -> None:
from agent_env.env.envs.service_db import DB_USER
from agent_env.env.gateway import AGENT_ENV_GATEWAY_MCP_PORT
from agent_env.providers import EnvironmentGatewayProvider
from agent_env.providers.env_providers.constants import GATEWAY_APP_DIR
from agent_env.providers.env_state import LocalPostgresStateProvider
from agent_env.config import get_config, service_db_env_id

if self._sandbox is None:
raise RuntimeError("Environment not deployed - call deploy() first")
if self._sandbox.mode == SANDBOX_MODE_CONTAINER:
raise NotImplementedError("Snapshot loading not supported on container mode")
layout = stack_layout_of(self._env_provider)
db_image = Artifact.get(snapshot.db_image_artifact_id, snapshot.db_image_artifact_version)
if not isinstance(db_image, DockerImageArtifact):
raise RuntimeError(f"Snapshot artifact {snapshot.db_image_artifact_id} is not a DockerImageArtifact")
Expand All @@ -896,27 +896,29 @@ async def _load_from_snapshot(self, snapshot) -> None:

logger.info("Pointing docker-compose.yml's servicedb at the snapshot image...")
# The write adds back the newline cat returns, so the file doesn't grow a blank line each reload.
deployed = (await self._sandbox.exec_script(f"cat {DOCKER_COMPOSE_PATH}")).removesuffix("\n")
compose = snapshot_compose(deployed, db_image.image_name)
deployed = (await self._sandbox.exec_script(f"cat {layout.compose_file}")).removesuffix("\n")
compose = snapshot_compose(deployed, db_image.image_name, compose_file=layout.compose_file)
gw = EnvironmentGatewayProvider()
gw._sandbox = self._sandbox
gw._stack_layout = layout
# The state store is the deploy's local Postgres; the changelog triggers below run through it.
gw._state_provider = LocalPostgresStateProvider(service_db_config=Env.get(service_db_env_id(get_config())).to_config())
gw._state_instance = LocalPostgresStateProvider.default_instance()
await gw._write_file(self._sandbox, DOCKER_COMPOSE_PATH, compose)
await gw._write_file(self._sandbox, layout.compose_file, compose)

# Remove old servicedb container and its anonymous volume, then start fresh from snapshot image
logger.info("Recreating servicedb container with snapshot image...")
await self._sandbox.exec_script(
f"cd {GATEWAY_APP_DIR} && docker compose rm -sf -v {DATABASE_SERVICE_NAME} && docker compose up -d {DATABASE_SERVICE_NAME} 2>&1"
f"cd {layout.app_dir} && {layout.compose_command_from_app_dir} rm -sf -v {DATABASE_SERVICE_NAME} && "
f"{layout.compose_command_from_app_dir} up -d {DATABASE_SERVICE_NAME} 2>&1"
)

# Wait for servicedb to be healthy
logger.info("Waiting for servicedb to be healthy...")
for i in range(60):
try:
await self._sandbox.exec_script(
f"docker compose -f {DOCKER_COMPOSE_PATH} exec -T {DATABASE_SERVICE_NAME} pg_isready -U {DB_USER}",
f"{layout.compose_command} exec -T {DATABASE_SERVICE_NAME} pg_isready -U {DB_USER}",
)
logger.info(f"servicedb healthy after {i + 1}s")
break
Expand All @@ -938,7 +940,7 @@ async def _load_from_snapshot(self, snapshot) -> None:
if name != DATABASE_SERVICE_NAME)))
logger.info(f"Recreating services: {svc_list}")
await gw._compose_up(self._sandbox, f"--force-recreate {svc_list}")
await gw._wait_for_gateway(self._sandbox, self._sandbox.host_port(AGENT_ENV_GATEWAY_MCP_PORT))
await gw._wait_for_gateway(self._sandbox, self._sandbox.host_port(layout.host_port(AGENT_ENV_GATEWAY_MCP_PORT)))

# Install changelog triggers for each service
for name in environment_names:
Expand Down
11 changes: 6 additions & 5 deletions src/agent_env/env/envs/website.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
from agent_env.env.envs._deployment import (
as_builtin, builtin_provider_for, close_deployed, close_replaced, deploy_refusal, deploy_through_provider, host_staging_refusal,
data_plane_headers, file_parts, legacy_load_file, load_by_signed_url, plugin_provider_like_a_builtin, provider_or_class,
reset_server_data,
reset_server_data, stack_layout_of,
staged_paths,
)
from agent_env.env.gateway import GatewayMode
Expand Down Expand Up @@ -340,14 +340,14 @@ async def load_file_artifact_universe(self, file_artifact_universe: "Any", desti
return await super().load_file_artifact_universe(file_artifact_universe, destination_path)

async def _copy_artifact_into_container(self, file_artifact, path: str) -> str:
from agent_env.providers.env_providers.constants import AGENT_ENV_WEBSITE_BACKEND_SUFFIX, DOCKER_COMPOSE_PATH
from agent_env.providers.env_providers.constants import AGENT_ENV_WEBSITE_BACKEND_SUFFIX
validate_file_path(path)
backend_service = f"{self.environment_name}-{AGENT_ENV_WEBSITE_BACKEND_SUFFIX}"
# Random suffix so concurrent loads on a shared sandbox (parallel /load-universe,
# MCP+website sharing a service_name, repeated loads) don't race on a shared path.
vm_path = f"/tmp/_artifact_{self.environment_name}-{uuid.uuid4().hex[:8]}-{posixpath.basename(path)}"
container_path = f"/tmp/data/{path}"
compose = f"docker compose -f {shlex.quote(DOCKER_COMPOSE_PATH)}"
compose = stack_layout_of(self._env_provider).compose_command

await self._sandbox.load_object_file(file_artifact.object_url, vm_path)
await self._sandbox.exec_script(
Expand All @@ -358,11 +358,12 @@ async def _copy_artifact_into_container(self, file_artifact, path: str) -> str:

async def _remove_from_container(self, path: str) -> None:
"""Remove what a load staged at ``/tmp/data/<path>``, best-effort: a load that finished stands either way."""
from agent_env.providers.env_providers.constants import AGENT_ENV_WEBSITE_BACKEND_SUFFIX, DOCKER_COMPOSE_PATH
from agent_env.providers.env_providers.constants import AGENT_ENV_WEBSITE_BACKEND_SUFFIX
backend_service = f"{self.environment_name}-{AGENT_ENV_WEBSITE_BACKEND_SUFFIX}"
container_path = f"/tmp/data/{path}"
compose = stack_layout_of(self._env_provider).compose_command
try:
await self._sandbox.exec_script(f"docker compose -f {shlex.quote(DOCKER_COMPOSE_PATH)} exec -T "
await self._sandbox.exec_script(f"{compose} exec -T "
f"{shlex.quote(backend_service)} rm -rf {shlex.quote(container_path)}")
except Exception as e: # noqa: BLE001 -- cleanup, not the load
logger.warning(f"[{self.environment_name}] could not remove {container_path} after a load (ignored): {e}")
Expand Down
Loading
Loading