Files
roboco/tests/unit/runtime/test_sandbox_env.py
47d78f50ee feat(sandbox): on-demand provisioning via request_sandbox verb (#338)
* feat(sandbox): on-demand request_sandbox verb replaces eager provisioning

Sandboxes were provisioned at every agent spawn for opted-in projects,
so every role paid the sidecar spin-up and a provisioning failure
refused the spawn. Provisioning now happens when an agent asks: the
request_sandbox do-verb (dev + QA) reaches the orchestrator through
ContentActionsDeps, ensure_sandbox provisions idempotently with an
in-memory per-agent cache (evicted at teardown and janitor sweep), and
creds return in the envelope payload including ready-to-export
ROBOCO_TEST_* values. Spawn now only injects a marker env naming the
available services plus a briefing line; sandbox failures can no longer
refuse a spawn. Teardown lifecycle unchanged.

* feat(sandbox): harden request_sandbox + Phase 3 wiring proof and docs

Hardening from adversarial review: ensure_sandbox now provisions the
project's full opted-in set on first request (a later superset can
never tear down a live sandbox mid-use), serializes per-agent behind an
asyncio lock (a client timeout-retry no longer races its own in-flight
provision), and verifies container liveness on every cache hit (a dead
sandbox evicts and re-provisions instead of serving dead creds). MCP
client budget 720->1080s for the full-set cold case. Phase 3: e2e smoke
wiring test (manifest grants + guard-chain envelopes over the real
API), sandbox-db/tools/map docs and CLAUDE.md rewritten for on-demand.

* feat(sandbox): release sandboxes when the agent's work ends

CEO directive: sidecars must not dangle once the agent is done. The six
work-ending verbs (i_am_done, unclaim, i_am_idle, pass_review,
fail_review, i_documented) now release the caller's sandbox best-effort
on their success path via release_sandbox (lock + teardown + cache
evict; a no-sandbox agent costs a dict lookup). Container removal and
the janitor remain the backstop; a re-request provisions fresh.

* test(sandbox): monkeypatch the release hook instead of method assignment

mypy method-assign rejected the direct AsyncMock assignments; the prior
static gate ran before this test file landed.

* test(sandbox): guard envelope evidence for mypy in verb tests

* chore(prompts): regenerate verb tables for request_sandbox

* chore: resolve merge with master (breadcrumbs + statement budget)

---------

Co-authored-by: Renn F <rennf93@users.noreply.github.com>
2026-07-08 16:40:02 +02:00

123 lines
4.0 KiB
Python

"""Sandbox marker env: `_append_sandbox_marker_env` + the `_spawn_container` branch.
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.
"""
from __future__ import annotations
import asyncio
from pathlib import Path
from unittest.mock import AsyncMock
import pytest
from roboco.models.runtime import OrchestratorAgentConfig
from roboco.runtime.orchestrator import AgentOrchestrator
def _config(
sandbox_available_services: list[str] | None = None,
) -> OrchestratorAgentConfig:
return OrchestratorAgentConfig(
agent_id="dev-1",
blueprint_path=Path(),
mcp_config_path=Path("/tmp/mcp.json"),
sandbox_available_services=sandbox_available_services or [],
)
def test_append_sandbox_marker_env_lists_services() -> None:
cmd: list[str] = []
AgentOrchestrator._append_sandbox_marker_env(cmd, ["postgres", "redis"])
assert "ROBOCO_SANDBOX_SERVICES_AVAILABLE=postgres,redis" in cmd
def test_append_sandbox_marker_env_single_service() -> None:
cmd: list[str] = []
AgentOrchestrator._append_sandbox_marker_env(cmd, ["mongo"])
assert "ROBOCO_SANDBOX_SERVICES_AVAILABLE=mongo" in cmd
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,
"_append_sandbox_marker_env",
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
async def test_spawn_container_uses_marker_env_when_opted_in(
monkeypatch: pytest.MonkeyPatch,
) -> None:
orch = AgentOrchestrator.__new__(AgentOrchestrator)
calls: list[str] = []
_stub_spawn_container_collaborators(monkeypatch, orch, calls)
await orch._spawn_container(_config(["postgres"]))
assert calls == ["sandbox"]
@pytest.mark.asyncio
async def test_spawn_container_uses_legacy_gate_env_when_not_opted_in(
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
async def test_spawn_container_stale_clear_runs_with_teardown_sandbox_false(
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""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."""
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)
await orch._spawn_container(_config(["postgres"]))
remove.assert_awaited_once_with(
"roboco-agent-dev-1",
teardown_sandbox=False,
stop_reason="pre_spawn_stale_clear",
)