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>
This commit is contained in:
Renzo F
2026-07-08 16:40:02 +02:00
committed by GitHub
co-authored by Renn F
parent 9e4025b822
commit 47d78f50ee
30 changed files with 1824 additions and 246 deletions
+24 -97
View File
@@ -1,8 +1,9 @@
"""Sandbox env injection: `_append_sandbox_env` + the `_spawn_container` branch.
"""Sandbox marker env: `_append_sandbox_marker_env` + the `_spawn_container` branch.
A sandbox-active spawn must inject `ROBOCO_TEST_DB_*` / `ROBOCO_TEST_REDIS_*`
pointed at the sandbox and MUST NOT also run the legacy `_append_gate_env`
prod-creds injection — sandbox replaces, never coexists with, prod creds.
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
@@ -13,93 +14,32 @@ from unittest.mock import AsyncMock
import pytest
from roboco.models.runtime import OrchestratorAgentConfig
from roboco.models.sandbox import SandboxConnection, SandboxInfo
from roboco.runtime.orchestrator import AgentOrchestrator
def _config(sandbox_info: SandboxInfo | None = None) -> OrchestratorAgentConfig:
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_info=sandbox_info,
sandbox_available_services=sandbox_available_services or [],
)
def test_append_sandbox_env_injects_postgres_and_redis() -> None:
info = SandboxInfo(
services={
"postgres": SandboxConnection(
host="roboco-sandbox-pg-dev-1",
port=5432,
password="pgpw",
user="sandbox",
database="sandbox",
),
"redis": SandboxConnection(
host="roboco-sandbox-redis-dev-1", port=6379, password="rdpw"
),
}
)
def test_append_sandbox_marker_env_lists_services() -> None:
cmd: list[str] = []
AgentOrchestrator._append_sandbox_env(cmd, _config(info))
AgentOrchestrator._append_sandbox_marker_env(cmd, ["postgres", "redis"])
assert "ROBOCO_TEST_DB_HOST=roboco-sandbox-pg-dev-1" in cmd
assert "ROBOCO_TEST_DB_PORT=5432" in cmd
assert "ROBOCO_TEST_DB_USER=sandbox" in cmd
assert "ROBOCO_TEST_DB_PASSWORD=pgpw" in cmd
assert "ROBOCO_TEST_DB_ADMIN_DB=sandbox" in cmd
assert "ROBOCO_TEST_REDIS_HOST=roboco-sandbox-redis-dev-1" in cmd
assert "ROBOCO_TEST_REDIS_PORT=6379" in cmd
assert "ROBOCO_TEST_REDIS_PASSWORD=rdpw" in cmd
assert "ROBOCO_SANDBOX_SERVICES_AVAILABLE=postgres,redis" in cmd
def test_append_sandbox_env_postgres_only_omits_redis_vars() -> None:
info = SandboxInfo(
services={
"postgres": SandboxConnection(
host="roboco-sandbox-pg-dev-1",
port=5432,
password="pgpw",
user="sandbox",
database="sandbox",
)
}
)
def test_append_sandbox_marker_env_single_service() -> None:
cmd: list[str] = []
AgentOrchestrator._append_sandbox_env(cmd, _config(info))
AgentOrchestrator._append_sandbox_marker_env(cmd, ["mongo"])
assert "ROBOCO_TEST_DB_HOST=roboco-sandbox-pg-dev-1" in cmd
assert not any(v.startswith("ROBOCO_TEST_REDIS_") for v in cmd)
def test_append_sandbox_env_injects_mongo() -> None:
info = SandboxInfo(
services={
"mongo": SandboxConnection(
host="roboco-sandbox-mongo-dev-1",
port=27017,
password="mpw",
user="sandbox",
database="admin",
)
}
)
cmd: list[str] = []
AgentOrchestrator._append_sandbox_env(cmd, _config(info))
assert "ROBOCO_TEST_MONGO_HOST=roboco-sandbox-mongo-dev-1" in cmd
assert "ROBOCO_TEST_MONGO_PORT=27017" in cmd
assert "ROBOCO_TEST_MONGO_USER=sandbox" in cmd
assert "ROBOCO_TEST_MONGO_PASSWORD=mpw" in cmd
assert "ROBOCO_TEST_MONGO_AUTH_DB=admin" in cmd
assert not any(v.startswith("ROBOCO_TEST_DB_") for v in cmd)
def test_append_sandbox_env_noop_without_sandbox_info() -> None:
cmd: list[str] = []
AgentOrchestrator._append_sandbox_env(cmd, _config(None))
assert cmd == []
assert "ROBOCO_SANDBOX_SERVICES_AVAILABLE=mongo" in cmd
def _fake_proc() -> AsyncMock:
@@ -125,7 +65,7 @@ def _stub_spawn_container_collaborators(
monkeypatch.setattr(orch, "_append_gate_env", lambda *_a: calls.append("gate"))
monkeypatch.setattr(
orch,
"_append_sandbox_env",
"_append_sandbox_marker_env",
lambda *_a: calls.append("sandbox"),
)
monkeypatch.setattr(orch, "_append_image_and_claude_args", lambda *_a: None)
@@ -135,27 +75,20 @@ def _stub_spawn_container_collaborators(
@pytest.mark.asyncio
async def test_spawn_container_uses_sandbox_env_when_sandbox_active(
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)
info = SandboxInfo(
services={
"postgres": SandboxConnection(
host="h", port=5432, password="pw", user="sandbox", database="sandbox"
)
}
)
await orch._spawn_container(_config(info))
await orch._spawn_container(_config(["postgres"]))
assert calls == ["sandbox"]
@pytest.mark.asyncio
async def test_spawn_container_uses_legacy_gate_env_without_sandbox(
async def test_spawn_container_uses_legacy_gate_env_when_not_opted_in(
monkeypatch: pytest.MonkeyPatch,
) -> None:
orch = AgentOrchestrator.__new__(AgentOrchestrator)
@@ -168,25 +101,19 @@ async def test_spawn_container_uses_legacy_gate_env_without_sandbox(
@pytest.mark.asyncio
async def test_spawn_container_stale_clear_spares_fresh_sandbox(
async def test_spawn_container_stale_clear_runs_with_teardown_sandbox_false(
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""The pre-spawn stale-clear must not tear down the sandbox that was
just provisioned for this very spawn (teardown_sandbox=False)."""
"""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)
info = SandboxInfo(
services={
"postgres": SandboxConnection(
host="h", port=5432, password="pw", user="sandbox", database="sandbox"
)
}
)
await orch._spawn_container(_config(info))
await orch._spawn_container(_config(["postgres"]))
remove.assert_awaited_once_with(
"roboco-agent-dev-1",