2026-07-08 16:40:02 +02:00
|
|
|
"""Sandbox marker env: `_append_sandbox_marker_env` + the `_spawn_container` branch.
|
2026-07-03 19:24:00 +02:00
|
|
|
|
2026-07-08 16:40:02 +02:00
|
|
|
An opted-in spawn injects a cheap `ROBOCO_SANDBOX_SERVICES_AVAILABLE` marker
|
|
|
|
|
(never prod creds — actual provisioning is on-demand via `request_sandbox`)
|
|
|
|
|
and MUST NOT also run the legacy `_append_gate_env` prod-creds injection —
|
|
|
|
|
the marker replaces, never coexists with, prod creds.
|
2026-07-03 19:24:00 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
import asyncio
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
from unittest.mock import AsyncMock
|
|
|
|
|
|
|
|
|
|
import pytest
|
2026-07-07 13:59:53 +02:00
|
|
|
from roboco.models.runtime import OrchestratorAgentConfig
|
2026-07-03 19:24:00 +02:00
|
|
|
from roboco.runtime.orchestrator import AgentOrchestrator
|
|
|
|
|
|
|
|
|
|
|
2026-07-08 16:40:02 +02:00
|
|
|
def _config(
|
|
|
|
|
sandbox_available_services: list[str] | None = None,
|
|
|
|
|
) -> OrchestratorAgentConfig:
|
2026-07-03 19:24:00 +02:00
|
|
|
return OrchestratorAgentConfig(
|
|
|
|
|
agent_id="dev-1",
|
|
|
|
|
blueprint_path=Path(),
|
|
|
|
|
mcp_config_path=Path("/tmp/mcp.json"),
|
2026-07-08 16:40:02 +02:00
|
|
|
sandbox_available_services=sandbox_available_services or [],
|
2026-07-03 19:24:00 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2026-07-08 16:40:02 +02:00
|
|
|
def test_append_sandbox_marker_env_lists_services() -> None:
|
2026-07-03 19:24:00 +02:00
|
|
|
cmd: list[str] = []
|
2026-07-08 16:40:02 +02:00
|
|
|
AgentOrchestrator._append_sandbox_marker_env(cmd, ["postgres", "redis"])
|
2026-07-03 19:24:00 +02:00
|
|
|
|
2026-07-08 16:40:02 +02:00
|
|
|
assert "ROBOCO_SANDBOX_SERVICES_AVAILABLE=postgres,redis" in cmd
|
2026-07-03 19:24:00 +02:00
|
|
|
|
|
|
|
|
|
2026-07-08 16:40:02 +02:00
|
|
|
def test_append_sandbox_marker_env_single_service() -> None:
|
2026-07-03 19:24:00 +02:00
|
|
|
cmd: list[str] = []
|
2026-07-08 16:40:02 +02:00
|
|
|
AgentOrchestrator._append_sandbox_marker_env(cmd, ["mongo"])
|
2026-07-03 19:24:00 +02:00
|
|
|
|
2026-07-08 16:40:02 +02:00
|
|
|
assert "ROBOCO_SANDBOX_SERVICES_AVAILABLE=mongo" in cmd
|
2026-07-03 19:24:00 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def _fake_proc() -> AsyncMock:
|
|
|
|
|
proc = AsyncMock()
|
|
|
|
|
proc.communicate = AsyncMock(return_value=(b"", b""))
|
|
|
|
|
proc.returncode = 0
|
|
|
|
|
return proc
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _stub_spawn_container_collaborators(
|
|
|
|
|
monkeypatch: pytest.MonkeyPatch, orch: AgentOrchestrator, calls: list[str]
|
|
|
|
|
) -> None:
|
|
|
|
|
monkeypatch.setattr(orch, "_provider_for", lambda *_a: None)
|
|
|
|
|
monkeypatch.setattr(orch, "_remove_container", AsyncMock(return_value=None))
|
|
|
|
|
monkeypatch.setattr(orch, "_resolve_host_paths", lambda *_a: {})
|
|
|
|
|
monkeypatch.setattr(
|
|
|
|
|
AgentOrchestrator,
|
|
|
|
|
"_build_mount_args",
|
|
|
|
|
staticmethod(lambda *_a: []),
|
|
|
|
|
)
|
|
|
|
|
monkeypatch.setattr(orch, "_append_agent_auth_env", lambda *_a: None)
|
|
|
|
|
monkeypatch.setattr(orch, "_append_git_context_env", lambda *_a: None)
|
|
|
|
|
monkeypatch.setattr(orch, "_append_gate_env", lambda *_a: calls.append("gate"))
|
|
|
|
|
monkeypatch.setattr(
|
|
|
|
|
orch,
|
2026-07-08 16:40:02 +02:00
|
|
|
"_append_sandbox_marker_env",
|
2026-07-03 19:24:00 +02:00
|
|
|
lambda *_a: calls.append("sandbox"),
|
|
|
|
|
)
|
|
|
|
|
monkeypatch.setattr(orch, "_append_image_and_claude_args", lambda *_a: None)
|
|
|
|
|
monkeypatch.setattr(
|
|
|
|
|
asyncio, "create_subprocess_exec", AsyncMock(return_value=_fake_proc())
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
2026-07-08 16:40:02 +02:00
|
|
|
async def test_spawn_container_uses_marker_env_when_opted_in(
|
2026-07-03 19:24:00 +02:00
|
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
|
|
|
) -> None:
|
|
|
|
|
orch = AgentOrchestrator.__new__(AgentOrchestrator)
|
|
|
|
|
calls: list[str] = []
|
|
|
|
|
_stub_spawn_container_collaborators(monkeypatch, orch, calls)
|
|
|
|
|
|
2026-07-08 16:40:02 +02:00
|
|
|
await orch._spawn_container(_config(["postgres"]))
|
2026-07-03 19:24:00 +02:00
|
|
|
|
|
|
|
|
assert calls == ["sandbox"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
2026-07-08 16:40:02 +02:00
|
|
|
async def test_spawn_container_uses_legacy_gate_env_when_not_opted_in(
|
2026-07-03 19:24:00 +02:00
|
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
|
|
|
) -> None:
|
|
|
|
|
orch = AgentOrchestrator.__new__(AgentOrchestrator)
|
|
|
|
|
calls: list[str] = []
|
|
|
|
|
_stub_spawn_container_collaborators(monkeypatch, orch, calls)
|
|
|
|
|
|
|
|
|
|
await orch._spawn_container(_config(None))
|
|
|
|
|
|
|
|
|
|
assert calls == ["gate"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
2026-07-08 16:40:02 +02:00
|
|
|
async def test_spawn_container_stale_clear_runs_with_teardown_sandbox_false(
|
2026-07-03 19:24:00 +02:00
|
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
|
|
|
) -> None:
|
2026-07-08 16:40:02 +02:00
|
|
|
"""The pre-spawn stale-clear is vestigial now (nothing is provisioned
|
|
|
|
|
before spawn) but still passes teardown_sandbox=False — it must not
|
|
|
|
|
tear down a sandbox the agent requested moments ago via the verb."""
|
2026-07-03 19:24:00 +02:00
|
|
|
orch = AgentOrchestrator.__new__(AgentOrchestrator)
|
|
|
|
|
calls: list[str] = []
|
|
|
|
|
_stub_spawn_container_collaborators(monkeypatch, orch, calls)
|
|
|
|
|
remove = AsyncMock(return_value=None)
|
|
|
|
|
monkeypatch.setattr(orch, "_remove_container", remove)
|
|
|
|
|
|
2026-07-08 16:40:02 +02:00
|
|
|
await orch._spawn_container(_config(["postgres"]))
|
2026-07-03 19:24:00 +02:00
|
|
|
|
2026-07-08 16:01:01 +02:00
|
|
|
remove.assert_awaited_once_with(
|
|
|
|
|
"roboco-agent-dev-1",
|
|
|
|
|
teardown_sandbox=False,
|
|
|
|
|
stop_reason="pre_spawn_stale_clear",
|
|
|
|
|
)
|