Files
roboco/tests/unit/runtime/test_sandbox_env.py
T
8f6dde9a50 feat(sandbox): pluggable per-engine registry (postgres/redis/mongo) (#324)
* feat(sandbox): pluggable per-engine registry (postgres/redis/mongo)

Replaces the hardcoded postgres+redis branches in the provisioner and the
env emitter with a registry of SandboxEngine specs (image, run args,
readiness probe, connection, ROBOCO_TEST_* env) in a pure low module
(roboco/models/sandbox.py). VALID_SANDBOX_SERVICES is derived from the
registry — single source of truth — and the provisioner + orchestrator
iterate it, so adding an engine is one class + one registry line, not
another branch. Adds a mongo:8-alpine engine (ROBOCO_TEST_MONGO_*) as the
third service alongside postgres/redis.

Also fixes the cold-pull loop that stranded v0.19.0 board agents with
empty error strings: docker run pulled inline under a 20s deadline, so a
NAS cold pull was killed, cancelled, and re-pulled from scratch forever.
_ensure_image now inspects + pulls (300s) before run; provisioning errors
log type+message so a bare TimeoutError no longer shows as "".

Panel edit-project dialog: postgres/redis toggles -> a Set<string>
multi-select driven by a SANDBOX_SERVICES catalog, so new engines appear
in the UI by adding to the catalog.

Tests: engine parity (allowlist==registry, unique slugs/images, no None
leak in env, SandboxInfo aggregates every engine), mongo provision + env
injection, plus the existing postgres/redis provision/env/spawn/janitor
suite updated to the registry shape. 821 unit / 5 skip green; ruff + mypy
(360 files) clean.

* docs(sandbox): reflect pluggable engine registry + mongo across docs

CHANGELOG (0.19.0): Added entry for the pluggable sandbox engine registry
(postgres/redis/mongo) + Fixed entry for the cold-pull loop/empty-error
strand that boarded v0.19.0 board agents.

docs/map (9 files): sandbox subsystem blurbs, SandboxProvisioner rows,
_maybe_provision_sandbox/_append_sandbox_env rows, feature-flag rows, the
migration-057 row + v0.17.0 delta, and the models.md VALID_SANDBOX_SERVICES
note — all retitled to DB/Redis/Mongo via the engine registry
(roboco/models/sandbox.py), with the one-class-one-line extension story and
the _ensure_image cold-pull fix. Production-network (roboco_data) lines left
as postgres+redis — mongo is sandbox-only, not a prod service.

docs/rag (3 files): sandbox-db.md rewritten around the registry (engine list,
generic _provision_engine, image pre-pull, ROBOCO_TEST_DB_*/REDIS_*/MONGO_*
incl. MONGO_AUTH_DB=admin, single emit_env); config-reference sandbox flag
row + subsection retitled; db-network-isolation framing broadened to
postgres/redis/mongo. preconditions-and-rejections left untouched (its hit
was an unrelated gateway see-also link).

* test(e2e): harden umbrella close terminal reads with bounded wait-for-state

The MegaTask umbrella close test flaked once on CI (ceo-approve returned
200 but the re-fetch saw awaiting_pm_review) then passed on re-run. The
production path is deterministic: complete -> main_pm_complete ->
submit_pm_review -> escalate_to_ceo -> ceo_approve -> commit, all on one
session, all awaited; the fire-and-forget completion hooks are isolated
(own session, best-effort, never touch task.status or the request session).
20 local runs could not reproduce it.

The one real surface is the read pattern: the e2e stack commits on the
uvicorn thread's loop and reads via a separate loop (run_db -> asyncio.run
with a fresh engine), so a terminal single point-read can race a
still-draining completion hook on a contended runner. Replace the two
terminal point-reads with a bounded wait_for_status poll. Strictly better
than a one-shot read: absorbs the transient, and a genuine state bug still
surfaces via the timeout branch asserting against the last-read state.

---------

Co-authored-by: Renn F <rennf93@users.noreply.github.com>
2026-07-07 13:59:53 +02:00

192 lines
6.2 KiB
Python

"""Sandbox env injection: `_append_sandbox_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.
"""
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.models.sandbox import SandboxConnection, SandboxInfo
from roboco.runtime.orchestrator import AgentOrchestrator
def _config(sandbox_info: SandboxInfo | None = None) -> OrchestratorAgentConfig:
return OrchestratorAgentConfig(
agent_id="dev-1",
blueprint_path=Path(),
mcp_config_path=Path("/tmp/mcp.json"),
sandbox_info=sandbox_info,
)
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"
),
}
)
cmd: list[str] = []
AgentOrchestrator._append_sandbox_env(cmd, _config(info))
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
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",
)
}
)
cmd: list[str] = []
AgentOrchestrator._append_sandbox_env(cmd, _config(info))
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 == []
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_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_sandbox_env_when_sandbox_active(
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))
assert calls == ["sandbox"]
@pytest.mark.asyncio
async def test_spawn_container_uses_legacy_gate_env_without_sandbox(
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_spares_fresh_sandbox(
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)."""
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))
remove.assert_awaited_once_with("roboco-agent-dev-1", teardown_sandbox=False)