2026-06-18 12:48:40 +02:00
|
|
|
"""GROK cost budget kill-switch: kill a live container over the cost ceiling.
|
|
|
|
|
|
2026-06-19 04:42:25 +02:00
|
|
|
The grok CLI exposes no live usage hook, so the budget kill-switch lives in the
|
|
|
|
|
orchestrator: it reads each live GROK container's captured cost (from its
|
|
|
|
|
usage.json, via ``_grok_cost_usd``) and kills + evicts it past
|
|
|
|
|
ROBOCO_GROK_MAX_COST_USD (also catching runaway-loop token burn). The usage.json
|
|
|
|
|
read is covered in the grok usage tests; here ``_grok_cost_usd`` is stubbed so the
|
|
|
|
|
kill DECISION is exercised deterministically.
|
2026-06-18 12:48:40 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
from unittest.mock import AsyncMock
|
|
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
from roboco.models.runtime import AgentInstance
|
|
|
|
|
from roboco.runtime.orchestrator import AgentOrchestrator, AgentState
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _grok_instance(provider_type: str = "grok") -> AgentInstance:
|
2026-06-19 04:42:25 +02:00
|
|
|
cfg = type("C", (), {"provider_type": provider_type, "model": "grok-build"})()
|
2026-06-18 12:48:40 +02:00
|
|
|
return AgentInstance(agent_id="be-dev-1", state=AgentState.ACTIVE, config=cfg)
|
|
|
|
|
|
|
|
|
|
|
2026-06-19 04:42:25 +02:00
|
|
|
def _orch(
|
|
|
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
|
|
|
*,
|
|
|
|
|
cap: float,
|
|
|
|
|
cost: float,
|
|
|
|
|
provider_type: str = "grok",
|
|
|
|
|
) -> tuple[AgentOrchestrator, AsyncMock]:
|
|
|
|
|
"""A bare orchestrator with the cost reader + container removal stubbed."""
|
|
|
|
|
orch = AgentOrchestrator.__new__(AgentOrchestrator)
|
|
|
|
|
orch._grok_max_cost_usd = cap
|
|
|
|
|
orch._instances = {"be-dev-1": _grok_instance(provider_type)}
|
|
|
|
|
monkeypatch.setattr(orch, "_grok_cost_usd", lambda _agent_id: cost)
|
|
|
|
|
remove_mock = AsyncMock()
|
|
|
|
|
monkeypatch.setattr(orch, "_remove_container", remove_mock)
|
|
|
|
|
return orch, remove_mock
|
|
|
|
|
|
|
|
|
|
|
2026-06-18 12:48:40 +02:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
async def test_cost_over_cap_kills_and_evicts(monkeypatch: pytest.MonkeyPatch) -> None:
|
2026-06-19 04:42:25 +02:00
|
|
|
orch, remove_mock = _orch(monkeypatch, cap=5.0, cost=7.5)
|
2026-06-18 12:48:40 +02:00
|
|
|
|
|
|
|
|
await orch._enforce_grok_cost_budget()
|
|
|
|
|
|
|
|
|
|
remove_mock.assert_awaited_once_with("roboco-agent-be-dev-1")
|
|
|
|
|
assert "be-dev-1" not in orch._instances
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
async def test_cost_under_cap_spares(monkeypatch: pytest.MonkeyPatch) -> None:
|
2026-06-19 04:42:25 +02:00
|
|
|
orch, remove_mock = _orch(monkeypatch, cap=5.0, cost=1.0)
|
2026-06-18 12:48:40 +02:00
|
|
|
|
|
|
|
|
await orch._enforce_grok_cost_budget()
|
|
|
|
|
|
|
|
|
|
remove_mock.assert_not_awaited()
|
|
|
|
|
assert "be-dev-1" in orch._instances
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
async def test_cap_zero_disables_the_sweep(monkeypatch: pytest.MonkeyPatch) -> None:
|
2026-06-19 04:42:25 +02:00
|
|
|
orch, remove_mock = _orch(monkeypatch, cap=0.0, cost=999.0)
|
2026-06-18 12:48:40 +02:00
|
|
|
|
|
|
|
|
await orch._enforce_grok_cost_budget()
|
|
|
|
|
|
|
|
|
|
remove_mock.assert_not_awaited()
|
|
|
|
|
assert "be-dev-1" in orch._instances
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
async def test_non_grok_container_is_ignored(monkeypatch: pytest.MonkeyPatch) -> None:
|
2026-06-19 04:42:25 +02:00
|
|
|
orch, remove_mock = _orch(
|
|
|
|
|
monkeypatch, cap=5.0, cost=999.0, provider_type="anthropic"
|
|
|
|
|
)
|
2026-06-18 12:48:40 +02:00
|
|
|
|
|
|
|
|
await orch._enforce_grok_cost_budget()
|
|
|
|
|
|
|
|
|
|
remove_mock.assert_not_awaited()
|
|
|
|
|
assert "be-dev-1" in orch._instances
|