feat(providers): Gemini CLI provider — ModelProvider.GEMINI (#660)

* feat(providers): Gemini CLI provider — ModelProvider.GEMINI

Mirrors the grok blueprint with source-verified divergences (all facts
pinned against google-gemini/gemini-cli @ 9681621c): no refresher
daemon — Google's refresh tokens are reusable, so the RO host mount is
COPIED to a writable container-local ~/.gemini and each container
refreshes in-process independently (the write-back crash risk on RO
never triggers); settings.json renders security.auth.selectedType
'oauth-personal', experimental.enableAgents=false (subagent ban),
autoConfigureMemory=false with a bounded heap; tool scoping rides the
tiered TOML Policy Engine (deny-only rules that yolo mode structurally
cannot beat); gemini -p with --output-format stream-json; usage parsed
from the run's own stdout stats — the adversarial pass caught the
parser reading the json-mode nested shape while the entrypoint runs
stream-json's FLAT shape (every real run would have priced $0 forever,
hidden by fixtures sharing the assumption) — now flat-primary with the
nested shape as cited fallback; rate-limit classified from structured
error.type only (model-echo immune), native exit 41 auth passthrough;
per-model pricing for the three GA models; migrations 084 (enum) + 085
(seed) complete the 082-085 finale chain. V1 excludes interactive
intake/secretary. Stack-merge required two behavior-preserving
complexity refactors in the shared park/usage plumbing (a park-pair
loop; a usage-reader dispatch dict).

* fix(providers): route gemini usage read through the containment barrier

Mirrors the codex/grok fix — _gemini_usage_json now delegates to
_read_usage_json_contained, so CodeQL's path-injection alert on the
gemini read is resolved by the same resolve-and-contain guard.

---------

Co-authored-by: Renn F <rennf93@users.noreply.github.com>
This commit is contained in:
Renzo F
2026-07-23 03:53:21 +02:00
committed by GitHub
co-authored by Renn F
parent 13abb2ece0
commit 21d6730400
24 changed files with 2664 additions and 56 deletions
@@ -0,0 +1,253 @@
"""GEMINI quota/auth parking: break the exit -> respawn cost loop.
A one-shot gemini run that hits a quota error is remapped to exit 75 by the
entrypoint wrapper (see gemini_cli_usage.classify_exit_code); a missing/empty
OAuth credential exits 41 (the CLI's own dedicated auth-failure code). Both
park the GEMINI provider instead of crash-retrying, mirroring grok's exit-75 /
exit-78 parks (see test_grok_rate_limit.py) but tracked independently.
"""
from __future__ import annotations
from datetime import UTC, datetime, timedelta
from unittest.mock import AsyncMock
import pytest
from roboco.models.runtime import AgentInstance
from roboco.runtime.orchestrator import (
_GEMINI_AUTH_EXIT_CODE,
_GEMINI_RATE_LIMIT_EXIT_CODE,
_GEMINI_REPARK_BACKOFF_CAP,
AgentOrchestrator,
AgentState,
)
def _gemini_instance(provider_type: str = "gemini") -> AgentInstance:
cfg = type("C", (), {"provider_type": provider_type, "model": "gemini-2.5-pro"})()
inst = AgentInstance(agent_id="be-dev-1", state=AgentState.ACTIVE, config=cfg)
inst.current_task_id = "task-1"
inst.container_id = "cid"
return inst
class _FakeTracker:
def __init__(self) -> None:
self.activated_with: dict[str, object] | None = None
async def activate(
self,
*,
retry_after: float,
affected_agents: list[str],
kind: str = "rate_limited",
) -> None:
self.activated_with = {
"retry_after": retry_after,
"affected_agents": affected_agents,
"kind": kind,
}
class _RecordingTracker:
"""Records every activate() retry_after across multiple re-parks."""
def __init__(self) -> None:
self.retry_afters: list[float] = []
self.kinds: list[str] = []
async def activate(
self, *, retry_after: float, affected_agents: list[str], kind: str
) -> None:
del affected_agents
self.retry_afters.append(retry_after)
self.kinds.append(kind)
def _orch() -> AgentOrchestrator:
orch = AgentOrchestrator.__new__(AgentOrchestrator)
orch._waiting_records = {}
orch._rate_limit_ceo_notified = set()
orch._gemini_last_park_at = None
orch._gemini_repark_count = 0
orch._gemini_rate_limit_retry_after_s = 60.0
orch._gemini_auth_retry_after_s = 60.0
return orch
def test_is_gemini_rate_limit_exit() -> None:
inst = _gemini_instance()
assert AgentOrchestrator._is_gemini_rate_limit_exit(
inst, _GEMINI_RATE_LIMIT_EXIT_CODE
)
assert not AgentOrchestrator._is_gemini_rate_limit_exit(inst, 0)
assert not AgentOrchestrator._is_gemini_rate_limit_exit(inst, 1)
assert not AgentOrchestrator._is_gemini_rate_limit_exit(
_gemini_instance(provider_type="anthropic"), _GEMINI_RATE_LIMIT_EXIT_CODE
)
# Same numeric exit code as grok's own detector, but provider-scoped: a
# grok instance exiting 75 is NOT a gemini rate-limit exit.
assert not AgentOrchestrator._is_gemini_rate_limit_exit(
_gemini_instance(provider_type="grok"), _GEMINI_RATE_LIMIT_EXIT_CODE
)
def test_is_gemini_auth_exit() -> None:
inst = _gemini_instance()
assert AgentOrchestrator._is_gemini_auth_exit(inst, _GEMINI_AUTH_EXIT_CODE)
assert not AgentOrchestrator._is_gemini_auth_exit(inst, 0)
assert not AgentOrchestrator._is_gemini_auth_exit(inst, 1)
assert not AgentOrchestrator._is_gemini_auth_exit(
_gemini_instance(provider_type="anthropic"), _GEMINI_AUTH_EXIT_CODE
)
@pytest.mark.asyncio
async def test_park_gemini_rate_limited_activates_and_offlines(
monkeypatch: pytest.MonkeyPatch,
) -> None:
orch = _orch()
inst = _gemini_instance()
inst.error_count = 2 # pretend prior crashes — parking must NOT count one
tracker = _FakeTracker()
monkeypatch.setattr(orch, "_make_tracker", lambda _p: tracker)
finalize = AsyncMock()
monkeypatch.setattr(orch, "_finalize_spawn_session", finalize)
monkeypatch.setattr(orch, "_persist_waiting_record", AsyncMock())
await orch._park_gemini_rate_limited("be-dev-1", inst)
finalize.assert_awaited_once()
assert inst.state == AgentState.OFFLINE
assert inst.container_id is None
assert inst.error_count == 0 # a quota park is not a crash
assert tracker.activated_with == {
"retry_after": pytest.approx(60.0),
"affected_agents": ["be-dev-1"],
"kind": "rate_limited",
}
@pytest.mark.asyncio
async def test_park_gemini_auth_unavailable_activates_with_auth_missing_kind(
monkeypatch: pytest.MonkeyPatch,
) -> None:
orch = _orch()
inst = _gemini_instance()
inst.error_count = 2
tracker = _FakeTracker()
monkeypatch.setattr(orch, "_make_tracker", lambda _p: tracker)
monkeypatch.setattr(orch, "_finalize_spawn_session", AsyncMock())
monkeypatch.setattr(orch, "_persist_waiting_record", AsyncMock())
await orch._park_gemini_auth_unavailable("be-dev-1", inst)
assert inst.state == AgentState.OFFLINE
assert inst.container_id is None
assert inst.error_count == 0
assert tracker.activated_with == {
"retry_after": pytest.approx(60.0),
"affected_agents": ["be-dev-1"],
"kind": "auth_missing",
}
@pytest.mark.asyncio
async def test_handle_stopped_container_parks_on_gemini_quota_exit(
monkeypatch: pytest.MonkeyPatch,
) -> None:
orch = AgentOrchestrator.__new__(AgentOrchestrator)
inst = _gemini_instance()
park = AsyncMock()
finalize = AsyncMock()
monkeypatch.setattr(orch, "_park_gemini_rate_limited", park)
monkeypatch.setattr(orch, "_finalize_spawn_session", finalize)
await orch._handle_stopped_container("be-dev-1", inst, _GEMINI_RATE_LIMIT_EXIT_CODE)
park.assert_awaited_once_with("be-dev-1", inst)
finalize.assert_not_awaited()
@pytest.mark.asyncio
async def test_handle_stopped_container_parks_on_gemini_auth_exit(
monkeypatch: pytest.MonkeyPatch,
) -> None:
orch = AgentOrchestrator.__new__(AgentOrchestrator)
inst = _gemini_instance()
park = AsyncMock()
finalize = AsyncMock()
monkeypatch.setattr(orch, "_park_gemini_auth_unavailable", park)
monkeypatch.setattr(orch, "_finalize_spawn_session", finalize)
await orch._handle_stopped_container("be-dev-1", inst, _GEMINI_AUTH_EXIT_CODE)
park.assert_awaited_once_with("be-dev-1", inst)
finalize.assert_not_awaited()
# --------------------------------------------------------------------------- #
# Gemini has no real recovery probe either (an OAuth-login daily quota cap has
# no cheap balance-check API) — mirrors grok's repark-backoff tests exactly.
# --------------------------------------------------------------------------- #
def _backoff_orchestrator() -> AgentOrchestrator:
return _orch()
@pytest.mark.asyncio
async def test_gemini_repark_backs_off_within_episode(
monkeypatch: pytest.MonkeyPatch,
) -> None:
orch = _backoff_orchestrator()
tracker = _RecordingTracker()
monkeypatch.setattr(orch, "_make_tracker", lambda _p: tracker)
monkeypatch.setattr(orch, "_finalize_spawn_session", AsyncMock())
monkeypatch.setattr(orch, "_persist_waiting_record", AsyncMock())
inst = _gemini_instance()
await orch._park_gemini_rate_limited("be-dev-1", inst)
await orch._park_gemini_rate_limited("be-dev-1", inst)
await orch._park_gemini_rate_limited("be-dev-1", inst)
assert tracker.retry_afters == [60.0, 120.0, 240.0]
assert tracker.kinds == ["rate_limited", "rate_limited", "rate_limited"]
@pytest.mark.asyncio
async def test_gemini_repark_resets_after_episode_gap(
monkeypatch: pytest.MonkeyPatch,
) -> None:
orch = _backoff_orchestrator()
orch._gemini_repark_count = 3
orch._gemini_last_park_at = datetime.now(UTC) - timedelta(hours=2)
tracker = _RecordingTracker()
monkeypatch.setattr(orch, "_make_tracker", lambda _p: tracker)
monkeypatch.setattr(orch, "_finalize_spawn_session", AsyncMock())
monkeypatch.setattr(orch, "_persist_waiting_record", AsyncMock())
inst = _gemini_instance()
await orch._park_gemini_rate_limited("be-dev-1", inst)
assert tracker.retry_afters == [60.0]
assert orch._gemini_repark_count == 0
@pytest.mark.asyncio
async def test_gemini_repark_backoff_caps(monkeypatch: pytest.MonkeyPatch) -> None:
orch = _backoff_orchestrator()
tracker = _RecordingTracker()
monkeypatch.setattr(orch, "_make_tracker", lambda _p: tracker)
monkeypatch.setattr(orch, "_finalize_spawn_session", AsyncMock())
monkeypatch.setattr(orch, "_persist_waiting_record", AsyncMock())
inst = _gemini_instance()
for _ in range(_GEMINI_REPARK_BACKOFF_CAP + 3):
await orch._park_gemini_rate_limited("be-dev-1", inst)
max_expected = 60.0 * (2**_GEMINI_REPARK_BACKOFF_CAP)
assert all(
r == max_expected for r in tracker.retry_afters[_GEMINI_REPARK_BACKOFF_CAP:]
)
assert max(tracker.retry_afters) == max_expected
@@ -0,0 +1,158 @@
"""GEMINI agents capture token usage/cost from their captured ``usage.json``.
A Gemini agent runs the gemini CLI — no SDK /usage/status server and no
Claude transcript — so finalize reads the ``usage.json`` the entrypoint wrote
to the per-agent data dir (mounted into the orchestrator). Mirrors
test_grok_usage_finalize.py; gemini's usage.json is priced per-model
server-side (gemini_cli_usage.usage_and_cost) but flattens to the SAME
``{model, total_tokens, cost_usd}`` shape, so the read side is identical to
grok's: the whole total folds into output.
"""
from __future__ import annotations
import json
import tempfile
from typing import TYPE_CHECKING
import httpx
import pytest
from roboco.models.runtime import AgentInstance
from roboco.runtime import orchestrator as orch_mod
from roboco.runtime.orchestrator import AgentOrchestrator
if TYPE_CHECKING:
from pathlib import Path
def _write_usage(path: Path, total_tokens: int, cost_usd: float) -> None:
path.write_text(
json.dumps(
{
"model": "gemini-2.5-pro",
"total_tokens": total_tokens,
"cost_usd": cost_usd,
}
),
encoding="utf-8",
)
def test_gemini_usage_folds_total_into_output(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
usage = tmp_path / "usage.json"
_write_usage(usage, total_tokens=180, cost_usd=0.02)
orch = AgentOrchestrator.__new__(AgentOrchestrator)
monkeypatch.setattr(
orch, "_gemini_usage_json", lambda _aid: json.loads(usage.read_text())
)
assert orch._gemini_usage_tokens("be-dev-1") == (0, 180, 0, 0)
def test_gemini_usage_zero_when_store_missing(monkeypatch: pytest.MonkeyPatch) -> None:
orch = AgentOrchestrator.__new__(AgentOrchestrator)
monkeypatch.setattr(orch, "_gemini_usage_json", lambda _aid: None)
assert orch._gemini_usage_tokens("be-dev-1") == (0, 0, 0, 0)
def test_gemini_cost_read_from_usage_json(monkeypatch: pytest.MonkeyPatch) -> None:
captured_cost = 3.25
orch = AgentOrchestrator.__new__(AgentOrchestrator)
monkeypatch.setattr(
orch,
"_gemini_usage_json",
lambda _aid: {"cost_usd": captured_cost, "total_tokens": 9},
)
assert orch._gemini_cost_usd("be-dev-1") == captured_cost
monkeypatch.setattr(orch, "_gemini_usage_json", lambda _aid: None)
assert orch._gemini_cost_usd("be-dev-1") == 0.0
@pytest.mark.asyncio
async def test_resolve_final_usage_routes_gemini_to_usage_json(
monkeypatch: pytest.MonkeyPatch,
) -> None:
orch = AgentOrchestrator.__new__(AgentOrchestrator)
monkeypatch.setattr(
orch, "_gemini_usage_json", lambda _aid: {"total_tokens": 12, "cost_usd": 0.01}
)
cfg = type("C", (), {"provider_type": "gemini"})()
orch._instances = {"be-dev-1": AgentInstance(agent_id="be-dev-1", config=cfg)}
assert await orch._resolve_final_token_usage("be-dev-1") == (0, 12, 0, 0)
@pytest.mark.asyncio
async def test_resolve_final_turns_tools_gemini_has_neither() -> None:
orch = AgentOrchestrator.__new__(AgentOrchestrator)
cfg = type("C", (), {"provider_type": "gemini"})()
orch._instances = {"be-dev-1": AgentInstance(agent_id="be-dev-1", config=cfg)}
assert await orch._resolve_final_turns_tools("be-dev-1") == (0, 0)
@pytest.mark.asyncio
async def test_resolve_active_tokens_routes_gemini_to_usage_json(
monkeypatch: pytest.MonkeyPatch,
) -> None:
orch = AgentOrchestrator.__new__(AgentOrchestrator)
monkeypatch.setattr(
orch, "_gemini_usage_json", lambda _aid: {"total_tokens": 12, "cost_usd": 0.01}
)
cfg = type("C", (), {"provider_type": "gemini"})()
orch._instances = {"be-dev-1": AgentInstance(agent_id="be-dev-1", config=cfg)}
async with httpx.AsyncClient() as client:
assert await orch._resolve_active_tokens(client, "be-dev-1") == (0, 12, 0, 0)
def test_gemini_usage_dir_branches_compose_vs_local(
monkeypatch: pytest.MonkeyPatch,
) -> None:
monkeypatch.setattr(orch_mod, "PROJECT_HOST_PATH", "")
local = AgentOrchestrator._gemini_usage_dir("be-dev-1")
assert "roboco-gemini-usage" in str(local)
assert local.name == "be-dev-1"
monkeypatch.setattr(orch_mod, "PROJECT_HOST_PATH", "/volume1/roboco")
monkeypatch.setattr(orch_mod, "GEMINI_USAGE_DATA_DIR", "/data/gemini-usage")
assert str(AgentOrchestrator._gemini_usage_dir("be-dev-1")) == (
"/data/gemini-usage/be-dev-1"
)
@pytest.mark.parametrize(
"bad",
["..", ".", "../etc", "a/b", "a\\b", "", "be-dev-1/../x", "x\x00y"],
)
def test_gemini_usage_dir_rejects_path_traversal(bad: str) -> None:
with pytest.raises(ValueError, match="unsafe agent id"):
AgentOrchestrator._gemini_usage_dir(bad)
def test_gemini_usage_json_reads_the_real_local_dir(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
# The un-mocked read path must find usage.json in the SAME branched dir the
# writer mounts (mirrors _ensure_gemini_usage_dir's create path).
monkeypatch.setattr(orch_mod, "PROJECT_HOST_PATH", "")
monkeypatch.setattr(tempfile, "gettempdir", lambda: str(tmp_path))
udir = tmp_path / "roboco-gemini-usage" / "be-dev-1"
udir.mkdir(parents=True)
(udir / "usage.json").write_text(
json.dumps({"total_tokens": 55, "cost_usd": 0.1}), encoding="utf-8"
)
orch = AgentOrchestrator.__new__(AgentOrchestrator)
assert orch._gemini_usage_tokens("be-dev-1") == (0, 55, 0, 0)
assert orch._gemini_cost_usd("be-dev-1") == 0.1 # noqa: PLR2004
def test_ensure_gemini_usage_dir_creates_world_writable(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
monkeypatch.setattr(orch_mod, "PROJECT_HOST_PATH", "")
monkeypatch.setattr(tempfile, "gettempdir", lambda: str(tmp_path))
orch = AgentOrchestrator.__new__(AgentOrchestrator)
orch._ensure_gemini_usage_dir("be-dev-1")
target = tmp_path / "roboco-gemini-usage" / "be-dev-1"
assert target.is_dir()
+9 -4
View File
@@ -1,15 +1,16 @@
"""The orchestrator routes only dedicated-backend providers through the registry.
GROK gets the GrokCliProvider; Anthropic / Ollama Cloud / self-hosted (and any
unknown value) return None so ``_spawn_container`` runs its built-in Claude Code
path unchanged. This keeps the GROK addition purely additive.
GROK gets the GrokCliProvider, GEMINI gets the GeminiCliProvider; Anthropic /
Ollama Cloud / self-hosted (and any unknown value) return None so
``_spawn_container`` runs its built-in Claude Code path unchanged. This keeps
the GROK / GEMINI additions purely additive.
"""
from __future__ import annotations
from unittest.mock import patch
from roboco.llm.providers import GrokCliProvider
from roboco.llm.providers import GeminiCliProvider, GrokCliProvider
from roboco.runtime.orchestrator import AgentOrchestrator
@@ -24,6 +25,10 @@ def test_provider_for_grok_returns_grok_provider() -> None:
assert isinstance(_make_orch()._provider_for("grok"), GrokCliProvider)
def test_provider_for_gemini_returns_gemini_provider() -> None:
assert isinstance(_make_orch()._provider_for("gemini"), GeminiCliProvider)
def test_provider_for_anthropic_returns_none() -> None:
assert _make_orch()._provider_for("anthropic") is None