feat(box): add box.enabled toggle and gate consumers on availability

Make the Box sandbox runtime optional. When ``box.enabled`` is false in
config (or when an enabled Box fails to connect), every dependent feature
degrades to the same disabled-state UX rather than crashing or silently
falling back to less safe code paths.

Backend:

- config.yaml: new top-level ``box.enabled: true`` flag (default true)
- BoxService:
  - Read box.enabled on construction
  - initialize() short-circuits when disabled — no remote WS connect, no
    stdio subprocess fork
  - _on_runtime_disconnect is a no-op when disabled (no reconnect loop
    on a deliberately-off service)
  - get_status() now exposes ``enabled`` so the frontend can tell
    "disabled in config" from "configured but failed"
- MCP stdio loader (mcp_stdio.uses_box_stdio): requires box_service to
  be available, not just installed
- MCP _init_stdio_python_server: when ap.box_service exists but is
  unavailable, refuse the stdio server with an actionable error instead
  of silently falling through to host-stdio (which bypasses the sandbox
  the operator asked for). Setups without ap.box_service installed at
  all keep the legacy host-stdio fallback for pre-Box dev mode
- SkillService._require_box_for_write: refuses create/update/install/
  write_skill_file when ap.box_service is installed but unavailable.
  Distinguishes disabled vs failed in the error message so the UI can
  surface the right hint. Legacy setups (no ap.box_service) keep the
  local fallback path — that distinction is what keeps the existing
  local-skills tests valid

Tests:
- Box disabled-state behavior (4 cases)
- Skill write refusal in disabled & failed states (7 cases)
- MCP stdio runtime info policy updated to match new refuse-when-down
  behavior

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Junyan Qin
2026-05-20 17:07:53 +08:00
parent 99328cf4c0
commit ec2d21fe63
8 changed files with 261 additions and 4 deletions

View File

@@ -152,8 +152,10 @@ def make_app(
profile: str = 'default',
host_root: str = '',
workspace_quota_mb: int | None = None,
enabled: bool = True,
):
box_config = {
'enabled': enabled,
'backend': 'local',
'runtime': {'endpoint': ''},
'local': {
@@ -1225,6 +1227,65 @@ class TestBoxHostMountModeNone:
)
class TestBoxDisabledByConfig:
"""``box.enabled = false`` must keep the BoxService usable as a status
surface but skip every connection attempt and report unavailable."""
@pytest.mark.asyncio
async def test_initialize_skips_connector_when_disabled(self):
logger = Mock()
app = make_app(logger, enabled=False)
client = Mock(spec=BoxRuntimeClient)
client.initialize = AsyncMock()
service = BoxService(app, client=client)
await service.initialize()
# The client must not be touched; we did not even open a connection.
client.initialize.assert_not_awaited()
assert service.enabled is False
assert service.available is False
# The reason is captured so the dashboard / UI can show it.
assert 'disabled' in service._connector_error.lower()
@pytest.mark.asyncio
async def test_get_status_reports_disabled(self):
logger = Mock()
service = BoxService(make_app(logger, enabled=False), client=Mock(spec=BoxRuntimeClient))
await service.initialize()
status = await service.get_status()
assert status['available'] is False
assert status['enabled'] is False
assert 'disabled' in status['connector_error'].lower()
@pytest.mark.asyncio
async def test_get_status_distinguishes_enabled_but_unavailable(self):
logger = Mock()
client = Mock(spec=BoxRuntimeClient)
client.initialize = AsyncMock(side_effect=RuntimeError('docker daemon not running'))
service = BoxService(make_app(logger, enabled=True), client=client)
await service.initialize()
status = await service.get_status()
assert status['available'] is False
assert status['enabled'] is True
assert 'docker daemon' in status['connector_error']
@pytest.mark.asyncio
async def test_disconnect_callback_is_no_op_when_disabled(self):
logger = Mock()
service = BoxService(make_app(logger, enabled=False), client=Mock(spec=BoxRuntimeClient))
# Should be safe to fire; must not flip reconnect state on a disabled
# service. If it tried to schedule a reconnect, the test would hang.
await service._on_runtime_disconnect(connector=Mock())
assert service._reconnecting is False
class TestBuildSkillExtraMounts:
"""Robustness of skill mount construction against a stale skill cache.