feat(grok): convert interactive intake/secretary to the grok CLI; delete opencode

Move the last Grok runtime off opencode onto xAI's official `grok` CLI, for full
parity with the Claude path. The intake/secretary chat now runs per-turn headless
`grok -p` invocations that resume one session id (proven live: context carries
across runs), with streaming-json deltas mapped to the existing panel StreamChunk
kinds — the IntakeDriver loop, message source, relay, and idle reaper are reused
unchanged; only the SessionFactory differs (GrokCliSession replaces the
opencode-serve session).

- GrokCliSession + a pure, unit-tested streaming-json -> StreamChunk assembler
  (thought coalesced to one block, text streamed live, end captures the session
  id for -r, fenced-draft fallback, clear errors incl. rate-limit).
- intake propose_draft and secretary read_company_state/read_task/submit_directive
  are now FastMCP servers (roboco-intake / roboco-secretary) wired into
  ~/.grok/config.toml, launched via `uv run --directory /app` to resolve the
  installed package. The secretary tools reuse the shared backend helpers.
- Orchestrator: interactive spawn mounts the subscription auth + per-agent usage
  dir (no metered xAI key, no permission env — grok flags carry per-role perms);
  usage/cost now read a captured usage.json (drop the opencode.db reader, the
  _opencode_db_path/_grok_usage_from_opencode methods, and the cost-cap's
  opencode read). hosts["opencode"] -> hosts["grok_usage"]; OPENCODE_DATA_DIR ->
  GROK_USAGE_DATA_DIR.
- Fix one-shot usage capture: `-s` does not pin the session id (grok generates
  its own), so the entrypoint now reads the real id back from the JSON run log
  and the reader uses it; usage is captured per-turn on the interactive path.
- Delete the opencode layer: opencode_config/opencode_usage/opencode_session, the
  docker/grok/*.js plugins, the old one-shot entrypoint, and their tests.
- Compose (all three files), .env.example, and stale comments updated to the
  grok-CLI runtime; add the SuperGrok auth mount + grok-usage dir.

Gate green: ruff, mypy (296 files), xenon, tests. NAS build/verify pending.
This commit is contained in:
Renn F
2026-06-19 04:42:25 +02:00
parent 499f6fc509
commit a88045aacf
40 changed files with 1307 additions and 2200 deletions
@@ -0,0 +1,95 @@
"""grok_cli_session — the pure streaming-json → StreamChunk mapper.
The subprocess runner (``GrokCliSession``) needs the live grok binary, so it is
not gate-covered; the turn-mapping logic lives in the pure ``_StreamAssembler``
and is fully exercised here by feeding it parsed events.
"""
from __future__ import annotations
import json
from roboco.agent_sdk.grok_cli_session import (
_classify_failure,
_parse_event,
_StreamAssembler,
)
def _kinds(chunks: list) -> list[str]:
return [c.kind for c in chunks]
def test_thought_deltas_coalesce_into_one_thinking_block() -> None:
a = _StreamAssembler()
out: list = []
for piece in ("Let", " me", " think"):
out += a.feed({"type": "thought", "data": piece})
# Nothing emitted until the answer starts (reasoning shown as one block).
assert out == []
out += a.feed({"type": "text", "data": "Hello"})
assert _kinds(out) == ["thinking", "text"]
assert out[0].text == "Let me think"
assert out[1].text == "Hello"
def test_text_deltas_stream_live() -> None:
a = _StreamAssembler()
out: list = []
for piece in ("a", "b", "c"):
out += a.feed({"type": "text", "data": piece})
assert _kinds(out) == ["text", "text", "text"]
assert "".join(c.text for c in out) == "abc"
def test_end_captures_session_id_and_emits_turn_end() -> None:
a = _StreamAssembler()
a.feed({"type": "text", "data": "hi"})
out = a.feed({"type": "end", "sessionId": "sid-9", "stopReason": "EndTurn"})
assert _kinds(out) == ["turn_end"]
assert a.session_id == "sid-9"
assert a.saw_end is True
assert out[-1].data["session_id"] == "sid-9"
def test_end_flushes_pending_thinking_before_turn_end() -> None:
a = _StreamAssembler()
a.feed({"type": "thought", "data": "reasoning only"})
out = a.feed({"type": "end", "sessionId": "s", "stopReason": "EndTurn"})
assert _kinds(out) == ["thinking", "turn_end"]
def test_fenced_draft_is_surfaced_as_a_draft_chunk() -> None:
a = _StreamAssembler()
draft = {"title": "Build X", "objective": "do it"}
a.feed({"type": "text", "data": "Here:\n```roboco-draft\n"})
a.feed({"type": "text", "data": json.dumps(draft)})
a.feed({"type": "text", "data": "\n```\n"})
out = a.feed({"type": "end", "sessionId": "s", "stopReason": "EndTurn"})
assert "draft" in _kinds(out)
draft_chunk = next(c for c in out if c.kind == "draft")
assert draft_chunk.data["title"] == "Build X"
def test_unknown_event_types_are_ignored() -> None:
a = _StreamAssembler()
assert a.feed({"type": "tool", "name": "whatever"}) == []
assert a.feed({"type": "", "data": "x"}) == []
def test_parse_event_is_tolerant() -> None:
assert _parse_event('{"type":"text","data":"x"}') == {"type": "text", "data": "x"}
assert _parse_event("not json") is None
assert _parse_event("[1,2,3]") is None # not a dict
def test_classify_failure_detects_rate_limit() -> None:
msg = _classify_failure(1, "xAI error: 429 too many requests")
assert "rate-limited" in msg.lower()
def test_classify_failure_generic_uses_last_stderr_line() -> None:
msg = _classify_failure(2, "warming up\nboom: the model exploded")
assert "boom: the model exploded" in msg
# With no stderr, the exit code is surfaced.
assert "exit code 2" in _classify_failure(2, "")
@@ -1,130 +0,0 @@
"""normalize_opencode_message maps an opencode message reply to panel chunks.
The OpencodeServeSession transport (subprocess + HTTP) is exercised live against
a real `opencode serve`; the deterministic message→chunk mapping, the
turn-level error surfacing, and session-id extraction are covered here.
"""
from __future__ import annotations
from typing import TYPE_CHECKING
import pytest
from roboco.agent_sdk.opencode_session import (
OpencodeServeSession,
_extract_session_id,
_message_error,
normalize_opencode_message,
)
if TYPE_CHECKING:
from roboco.agent_sdk.intake_driver import StreamChunk
def _kinds(chunks: list[StreamChunk]) -> list[str]:
return [c.kind for c in chunks]
def test_text_part_emits_text_then_turn_end() -> None:
chunks = normalize_opencode_message({"parts": [{"type": "text", "text": "Hi"}]})
assert _kinds(chunks) == ["text", "turn_end"]
assert chunks[0].text == "Hi"
def test_reasoning_part_maps_to_thinking() -> None:
chunks = normalize_opencode_message({"parts": [{"type": "reasoning", "text": "x"}]})
assert chunks[0].kind == "thinking"
assert chunks[0].text == "x"
def test_tool_part_maps_to_tool_use() -> None:
chunks = normalize_opencode_message(
{"parts": [{"type": "tool", "tool": "read", "input": {"path": "x"}}]}
)
tool = next(c for c in chunks if c.kind == "tool_use")
assert tool.tool == "read"
assert tool.data == {"input": {"path": "x"}}
def test_fenced_draft_in_text_becomes_draft_chunk() -> None:
fenced = '```roboco-draft\n{"title": "Add login"}\n```'
chunks = normalize_opencode_message({"parts": [{"type": "text", "text": fenced}]})
draft = next(c for c in chunks if c.kind == "draft")
assert draft.data["title"] == "Add login"
@pytest.mark.asyncio
async def test_send_on_dead_serve_yields_clear_error(
monkeypatch: pytest.MonkeyPatch,
) -> None:
# A crashed `opencode serve` must surface a clear error + end the turn, not
# hang the chat with opaque connection errors while the container zombies.
sess = OpencodeServeSession()
monkeypatch.setattr(sess, "_session_id", "ses-1")
monkeypatch.setattr(sess, "_client", object()) # unused: dead-proc guard wins
monkeypatch.setattr(sess, "_proc", type("P", (), {"returncode": 1})())
chunks = [c async for c in sess.send("hi")]
assert [c.kind for c in chunks] == ["error", "turn_end"]
assert "exited" in chunks[0].text
def test_propose_draft_tool_part_becomes_draft_chunk() -> None:
# The intake-tools.js propose_draft tool call (its input nested under
# `draft`) is intercepted into a draft chunk — NOT rendered as a tool_use —
# so the panel shows the draft card. This is the primary Grok-intake path.
chunks = normalize_opencode_message(
{
"parts": [
{
"type": "tool",
"tool": "propose_draft",
"input": {"draft": {"title": "Add login", "team": "backend"}},
}
]
}
)
assert "tool_use" not in _kinds(chunks)
draft = next(c for c in chunks if c.kind == "draft")
assert draft.data["title"] == "Add login"
assert draft.data["team"] == "backend"
def test_unknown_part_skipped_but_turn_still_ends() -> None:
chunks = normalize_opencode_message({"parts": [{"type": "mystery", "x": 1}]})
assert _kinds(chunks) == ["turn_end"]
def test_empty_message_yields_only_turn_end() -> None:
assert _kinds(normalize_opencode_message({"parts": []})) == ["turn_end"]
def test_turn_level_error_is_surfaced_not_blank() -> None:
# A model failure lands in info.error with parts=[]; it must NOT render blank.
msg = {
"info": {
"role": "assistant",
"error": {
"name": "APIError",
"data": {"message": "Incorrect API key provided"},
},
},
"parts": [],
}
chunks = normalize_opencode_message(msg)
assert _kinds(chunks) == ["error", "turn_end"]
assert "Incorrect API key" in chunks[0].text
def test_message_error_extraction() -> None:
assert _message_error({"info": {"error": {"data": {"message": "boom"}}}}) == "boom"
assert _message_error({"info": {"error": {"name": "APIError"}}}) == "APIError"
assert _message_error({"info": {}}) is None
assert _message_error({"parts": []}) is None
def test_extract_session_id_is_tolerant() -> None:
assert _extract_session_id({"id": "s1"}) == "s1"
assert _extract_session_id({"sessionID": "s2"}) == "s2"
assert _extract_session_id({"info": {"id": "s3"}}) == "s3"
assert _extract_session_id({}) is None
assert _extract_session_id("nope") is None
@@ -10,6 +10,8 @@ from roboco.llm.providers import grok_cli_usage as gu
if TYPE_CHECKING:
from pathlib import Path
import pytest
def _write_updates(path: Path, totals: list[int]) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
@@ -79,7 +81,9 @@ def test_usage_and_cost_prices_total_at_output_rate() -> None:
assert abs(cost - 2.00) < 1e-6 # noqa: PLR2004
def test_main_writes_usage_file(tmp_path: Path, monkeypatch) -> None: # type: ignore[no-untyped-def]
def test_main_writes_usage_file(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
home = tmp_path / ".grok"
cwd = "/ws/be-dev-1"
sid = "sid-1"
@@ -89,6 +93,7 @@ def test_main_writes_usage_file(tmp_path: Path, monkeypatch) -> None: # type: i
monkeypatch.setattr(gu, "USAGE_OUT_PATH", out)
monkeypatch.setenv("GROK_HOME", str(home))
monkeypatch.setenv("ROBOCO_GROK_RUN_CWD", cwd)
monkeypatch.delenv("ROBOCO_GROK_RUN_LOG", raising=False)
monkeypatch.setenv("ROBOCO_AGENT_SESSION_ID", sid)
monkeypatch.setenv("ROBOCO_AGENT_MODEL", "grok-build")
assert gu.main() == 0
@@ -96,3 +101,76 @@ def test_main_writes_usage_file(tmp_path: Path, monkeypatch) -> None: # type: i
assert data["total_tokens"] == 1234 # noqa: PLR2004
assert data["model"] == "grok-build"
assert data["cost_usd"] > 0.0
def test_capture_session_usage_writes_running_total(tmp_path: Path) -> None:
home = tmp_path / ".grok"
cwd = "/ws/intake-1"
sid = "sid-x"
target = home / "sessions" / "%2Fws%2Fintake-1" / sid
_write_updates(target / "updates.jsonl", [100, 900, 500])
out = tmp_path / "usage.json"
tokens = gu.capture_session_usage(
cwd=cwd, session_id=sid, model="grok-build", out_path=out, grok_home=home
)
assert tokens == 900 # noqa: PLR2004 — the running max is the chat total
data = json.loads(out.read_text())
assert data["total_tokens"] == 900 # noqa: PLR2004
assert data["cost_usd"] > 0.0
def test_capture_session_usage_zero_when_session_absent(tmp_path: Path) -> None:
out = tmp_path / "usage.json"
tokens = gu.capture_session_usage(
cwd="/ws/x",
session_id="missing",
model="grok-build",
out_path=out,
grok_home=tmp_path / ".grok",
)
assert tokens == 0
# A zero session still writes a usage file (a real zero-cost run).
assert json.loads(out.read_text())["total_tokens"] == 0
def test_session_id_from_run_log_reads_the_real_id(tmp_path: Path) -> None:
log = tmp_path / "run.json"
log.write_text(
json.dumps({"text": "ok", "sessionId": "019edd9d-real", "stopReason": "End"}),
encoding="utf-8",
)
assert gu.session_id_from_run_log(log) == "019edd9d-real"
def test_session_id_from_run_log_none_for_bad_log(tmp_path: Path) -> None:
assert gu.session_id_from_run_log(tmp_path / "absent.json") is None
bad = tmp_path / "bad.json"
bad.write_text("not json", encoding="utf-8")
assert gu.session_id_from_run_log(bad) is None
idless = tmp_path / "idless.json"
idless.write_text(json.dumps({"text": "ok"}), encoding="utf-8")
assert gu.session_id_from_run_log(idless) is None
def test_main_prefers_run_log_session_id(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
# grok ignores a requested id, so the real id comes from the run log — it must
# win over the ROBOCO_AGENT_SESSION_ID fallback (which points at no store).
home = tmp_path / ".grok"
cwd = "/ws/be-dev-1"
real_sid = "real-sid"
_write_updates(
home / "sessions" / "%2Fws%2Fbe-dev-1" / real_sid / "updates.jsonl", [777]
)
run_log = tmp_path / "run.json"
run_log.write_text(json.dumps({"sessionId": real_sid}), encoding="utf-8")
out = tmp_path / "usage.json"
monkeypatch.setattr(gu, "USAGE_OUT_PATH", out)
monkeypatch.setenv("GROK_HOME", str(home))
monkeypatch.setenv("ROBOCO_GROK_RUN_CWD", cwd)
monkeypatch.setenv("ROBOCO_GROK_RUN_LOG", str(run_log))
monkeypatch.setenv("ROBOCO_AGENT_SESSION_ID", "ignored-fallback")
monkeypatch.setenv("ROBOCO_AGENT_MODEL", "grok-build")
assert gu.main() == 0
assert json.loads(out.read_text())["total_tokens"] == 777 # noqa: PLR2004
-143
View File
@@ -1,143 +0,0 @@
"""Tests for the Grok opencode.json generator (RoboCo MCP -> opencode config)."""
from __future__ import annotations
from roboco.llm.providers.opencode_config import (
OpencodeGuards,
build_opencode_config,
translate_mcp_servers,
)
_MODEL = "grok-build-0.1"
_MCP = {
"mcpServers": {
"roboco-flow": {
"command": "uv",
"args": ["run", "--no-sync", "python", "-m", "roboco.mcp.flow_server"],
"env": {
"ROBOCO_AGENT_ID": "uuid-1",
"UV_PROJECT_ENVIRONMENT": "/app/.venv",
},
},
"roboco-do": {
"command": "uv",
"args": ["run", "--no-sync", "python", "-m", "roboco.mcp.do_server"],
"env": {"ROBOCO_AGENT_ID": "uuid-1"},
},
}
}
def test_translate_mcp_servers_shape() -> None:
out = translate_mcp_servers(_MCP)
flow = out["roboco-flow"]
assert flow["type"] == "local"
assert flow["enabled"] is True
# command + args collapse into a single command array (opencode shape).
assert flow["command"] == [
"uv",
"run",
"--no-sync",
"python",
"-m",
"roboco.mcp.flow_server",
]
# env -> environment (opencode key).
assert flow["environment"]["ROBOCO_AGENT_ID"] == "uuid-1"
assert "env" not in flow
assert set(out) == {"roboco-flow", "roboco-do"}
def test_translate_mcp_servers_empty() -> None:
assert translate_mcp_servers({}) == {}
assert translate_mcp_servers({"mcpServers": {}}) == {}
def test_translate_mcp_servers_omits_environment_when_no_env() -> None:
out = translate_mcp_servers(
{"mcpServers": {"x": {"command": "uv", "args": ["run"]}}}
)
assert "environment" not in out["x"]
assert out["x"]["command"] == ["uv", "run"]
def test_build_opencode_config_emits_no_provider_block() -> None:
cfg = build_opencode_config(
_MCP,
_MODEL,
instruction_paths=["/app/system-prompt.md"],
)
# CRITICAL: NO provider block. ANY provider.xai block breaks plugin-tool
# registration on opencode 1.17.8 (verified live). The built-in xai provider
# drives the model; the key reaches it via the XAI_API_KEY env var.
assert "provider" not in cfg
# Top-level model selector is "<provider>/<model>".
assert cfg["model"] == "xai/grok-build-0.1"
# Gateway servers carried through.
assert "roboco-flow" in cfg["mcp"]
assert cfg["instructions"] == ["/app/system-prompt.md"]
def test_build_opencode_config_has_no_plugin_array() -> None:
# opencode 1.17.8 ignores config `plugin:`-array absolute paths for
# registration; plugins live in the auto-discovery dir, baked into the images.
cfg = build_opencode_config(_MCP, _MODEL, instruction_paths=[])
assert "plugin" not in cfg
def test_build_opencode_config_edit_permission_is_tunable() -> None:
# Read-only roles (qa / pr_reviewer / auditor / PMs / board) get edit=deny so
# a Grok agent can't write code on a role that must never touch the tree.
cfg = build_opencode_config(
{},
_MODEL,
instruction_paths=[],
guards=OpencodeGuards(edit_permission="deny"),
)
assert cfg["permission"]["edit"] == "deny"
def test_build_opencode_config_bash_permission_is_tunable() -> None:
cfg = build_opencode_config(
{},
_MODEL,
instruction_paths=[],
guards=OpencodeGuards(bash_permission="deny"),
)
assert cfg["permission"]["bash"] == "deny"
assert cfg["permission"]["edit"] == "allow"
def test_build_opencode_config_allows_external_directory_by_default() -> None:
# opencode auto-denies an "ask" external-dir read in headless mode (the
# pr-reviewer couldn't read a diff it wrote to /tmp); default "allow".
cfg = build_opencode_config(_MCP, _MODEL, instruction_paths=[])
assert cfg["permission"]["external_directory"] == "allow"
def test_build_opencode_config_external_directory_is_tunable() -> None:
cfg = build_opencode_config(
{},
_MODEL,
instruction_paths=[],
guards=OpencodeGuards(external_directory_permission="deny"),
)
assert cfg["permission"]["external_directory"] == "deny"
def test_build_opencode_config_disables_subagent_task_tool_by_default() -> None:
# The subagent `task` tool must be hard-disabled: a RoboCo role never uses
# opencode-internal subagents, and one spawned on grok-build-0.1 hung the run.
cfg = build_opencode_config(_MCP, _MODEL, instruction_paths=[])
assert cfg["tools"] == {"task": False}
def test_build_opencode_config_subagents_can_be_re_enabled() -> None:
cfg = build_opencode_config(
_MCP,
_MODEL,
instruction_paths=[],
guards=OpencodeGuards(disable_subagents=False),
)
assert "tools" not in cfg
-134
View File
@@ -1,134 +0,0 @@
"""Tests for opencode usage capture (reading the opencode SQLite session table).
The fixture DB mirrors the real opencode v1.x ``session`` table columns observed
from a local run (cost + tokens_input/output/reasoning/cache_read/cache_write).
"""
from __future__ import annotations
import sqlite3
from typing import TYPE_CHECKING
from roboco.llm.providers.opencode_usage import (
cost_for_session,
read_session_usage,
)
if TYPE_CHECKING:
from pathlib import Path
_M = 1_000_000
_TOL = 1e-4
_ZERO_COST = 0.0
# Single-session fixture: input, output, reasoning, cache_read, cache_write.
_IN, _OUT, _REASON, _CREAD, _CWRITE = 100, 50, 10, 20, 5
# Second session for the summation test.
_S2_IN, _S2_OUT, _S2_CREAD = 200, 70, 10
# grok-build-0.1: 1M input ($1.00) + 1M output ($2.00) = $3.00.
_GROK_COST_1M_1M = 3.00
# A REAL grok-build-0.1 session row observed from a live opencode run. Our
# pricing must reproduce opencode's own stored `cost` (= xAI authoritative).
_REAL_IN, _REAL_OUT, _REAL_REASON, _REAL_CREAD = 6120, 1, 226, 1856
_REAL_COST = 0.0069452
def _make_db(
path: Path, rows: list[tuple[str, int, int, int, int, int, float]]
) -> None:
con = sqlite3.connect(path)
con.execute(
"""
CREATE TABLE session (
id text PRIMARY KEY,
tokens_input integer DEFAULT 0 NOT NULL,
tokens_output integer DEFAULT 0 NOT NULL,
tokens_reasoning integer DEFAULT 0 NOT NULL,
tokens_cache_read integer DEFAULT 0 NOT NULL,
tokens_cache_write integer DEFAULT 0 NOT NULL,
cost real DEFAULT 0 NOT NULL
)
"""
)
con.executemany(
"INSERT INTO session "
"(id, tokens_input, tokens_output, tokens_reasoning, "
"tokens_cache_read, tokens_cache_write, cost) "
"VALUES (?, ?, ?, ?, ?, ?, ?)",
rows,
)
con.commit()
con.close()
def test_read_missing_db_returns_none(tmp_path: Path) -> None:
assert read_session_usage(tmp_path / "nope.db") is None
def test_read_single_session(tmp_path: Path) -> None:
db = tmp_path / "opencode.db"
# (id, input, output, reasoning, cache_read, cache_write, cost)
_make_db(db, [("s1", _IN, _OUT, _REASON, _CREAD, _CWRITE, 0.0007)])
usage = read_session_usage(db, session_id="s1")
assert usage is not None
assert usage.tokens_input == _IN
assert usage.tokens_output == _OUT
assert usage.tokens_cache_read == _CREAD
assert usage.tokens_cache_write == _CWRITE
assert usage.tokens_reasoning == _REASON
def test_read_sums_all_sessions_when_no_id(tmp_path: Path) -> None:
db = tmp_path / "opencode.db"
_make_db(
db,
[
("s1", _IN, _OUT, 0, 0, 0, 0.0),
("s2", _S2_IN, _S2_OUT, 0, _S2_CREAD, 0, 0.0),
],
)
usage = read_session_usage(db)
assert usage is not None
assert usage.tokens_input == _IN + _S2_IN
assert usage.tokens_output == _OUT + _S2_OUT
assert usage.tokens_cache_read == _S2_CREAD
def test_read_empty_table_returns_none(tmp_path: Path) -> None:
db = tmp_path / "opencode.db"
_make_db(db, [])
assert read_session_usage(db) is None
def test_cost_for_session_uses_roboco_pricing(tmp_path: Path) -> None:
db = tmp_path / "opencode.db"
# 1M input + 1M output for grok-build-0.1 → our $3.00, not opencode's 99.0.
_make_db(db, [("s1", _M, _M, 0, 0, 0, 99.0)])
usage, cost = cost_for_session("grok-build-0.1", db, session_id="s1")
assert usage is not None
assert abs(cost - _GROK_COST_1M_1M) < _TOL
def test_cost_for_session_missing_db(tmp_path: Path) -> None:
usage, cost = cost_for_session("grok-build-0.1", tmp_path / "nope.db")
assert usage is None
assert cost == _ZERO_COST
def test_cost_reproduces_opencode_authoritative_cost(tmp_path: Path) -> None:
"""Real observed row: our pricing must match opencode's stored USD cost.
Proves the column semantics (non-cached input disjoint from cache_read;
reasoning separate, billed at output rate).
"""
db = tmp_path / "opencode.db"
# (id, input, output, reasoning, cache_read, cache_write, cost)
_make_db(
db,
[("real", _REAL_IN, _REAL_OUT, _REAL_REASON, _REAL_CREAD, 0, _REAL_COST)],
)
usage, cost = cost_for_session("grok-build-0.1", db, session_id="real")
assert usage is not None
assert abs(cost - _REAL_COST) < _TOL
assert abs(cost - usage.opencode_cost) < _TOL
+8 -11
View File
@@ -29,14 +29,10 @@ from roboco.models.runtime import OrchestratorAgentConfig
@pytest.fixture(autouse=True)
def _isolate_grok_auth(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> Path:
def _isolate_grok_auth(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path:
"""Point GROK_AUTH_HOST_PATH at a fresh tmp dir so tests never mount the real
~/.grok. Tests that exercise the auth mount create ``auth.json`` themselves."""
monkeypatch.setattr(
"roboco.llm.providers.grok.GROK_AUTH_HOST_PATH", str(tmp_path)
)
monkeypatch.setattr("roboco.llm.providers.grok.GROK_AUTH_HOST_PATH", str(tmp_path))
return tmp_path
@@ -81,7 +77,7 @@ class _FakeHost:
async def _remove_container(self, container_name: str) -> None:
self.removed.append(container_name)
def _ensure_opencode_data_dir(self, agent_id: str) -> None:
def _ensure_grok_usage_dir(self, agent_id: str) -> None:
self.data_dirs_ensured.append(agent_id)
def _resolve_host_paths(
@@ -92,7 +88,7 @@ class _FakeHost:
if config.mcp_config_path
else None,
"settings": str(agent_settings_path) if agent_settings_path else None,
"opencode": f"/host/data/opencode/{config.agent_id}",
"grok_usage": f"/host/data/grok-usage/{config.agent_id}",
}
def _build_mount_args(
@@ -218,11 +214,12 @@ async def test_grok_spawn_wires_gateway_env_and_image_last() -> None:
assert "ROBOCO_MCP_CONFIG=/app/mcp-config.json" in cmd
assert "ROBOCO_AGENT_ID=be-dev-1" in cmd # renderer computes per-role flags
assert "ROBOCO_AGENT_MODEL=grok-build" in cmd
# Fixed session id so usage capture can locate the run's session store.
assert "ROBOCO_AGENT_SESSION_ID=sess-1" in cmd
# No session id is injected: grok ignores a requested id, so the entrypoint
# reads the real one back from the run log for usage capture.
assert not any(c.startswith("ROBOCO_AGENT_SESSION_ID=") for c in cmd)
# Usage capture: per-agent data dir mounted + the entrypoint's usage file.
assert host.data_dirs_ensured == ["be-dev-1"]
assert "/host/data/opencode/be-dev-1:/home/agent/.grok-usage" in cmd
assert "/host/data/grok-usage/be-dev-1:/home/agent/.grok-usage" in cmd
assert "ROBOCO_GROK_USAGE_FILE=/home/agent/.grok-usage/usage.json" in cmd
# Identity wiring from the shared host helpers is present.
assert "ROBOCO_AGENT_TOKEN=hmac-be-dev-1" in cmd
@@ -0,0 +1,92 @@
"""roboco-intake MCP server — propose_draft delivers the draft to the relay."""
from __future__ import annotations
from typing import Any
import httpx
import pytest
from roboco.mcp import intake_server
def _client(handler: Any) -> httpx.AsyncClient:
return httpx.AsyncClient(transport=httpx.MockTransport(handler))
@pytest.mark.asyncio
async def test_post_draft_posts_to_the_relay(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv("ROBOCO_API_URL", "http://orch:8000")
seen: dict[str, Any] = {}
def handler(request: httpx.Request) -> httpx.Response:
seen["url"] = str(request.url)
seen["json"] = __import__("json").loads(request.content)
return httpx.Response(200, json={"ok": True})
async with _client(handler) as client:
result = await intake_server.post_draft(
"sess-1", {"title": "Build X"}, client=client
)
assert result == {"ok": True}
assert seen["url"] == "http://orch:8000/api/prompter/live/sess-1/events"
assert seen["json"]["kind"] == "draft"
assert seen["json"]["tool"] == "propose_draft"
assert seen["json"]["data"] == {"title": "Build X"}
@pytest.mark.asyncio
async def test_post_draft_reports_http_error() -> None:
def handler(_request: httpx.Request) -> httpx.Response:
return httpx.Response(503)
async with _client(handler) as client:
result = await intake_server.post_draft("s", {}, client=client)
assert result == {"error": "http_503"}
@pytest.mark.asyncio
async def test_post_draft_reports_request_failure() -> None:
def handler(_request: httpx.Request) -> httpx.Response:
raise httpx.ConnectError("boom")
async with _client(handler) as client:
result = await intake_server.post_draft("s", {}, client=client)
assert result["error"] == "request_failed"
assert "boom" in result["detail"]
@pytest.mark.asyncio
async def test_propose_draft_requires_a_live_session(
monkeypatch: pytest.MonkeyPatch,
) -> None:
monkeypatch.delenv("ROBOCO_PROMPTER_SESSION_ID", raising=False)
msg = await intake_server.propose_draft({"title": "X"})
assert "No live session id" in msg
@pytest.mark.asyncio
async def test_propose_draft_acks_on_success(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv("ROBOCO_PROMPTER_SESSION_ID", "sess-1")
async def _ok(_sid: str, _draft: dict[str, Any]) -> dict[str, Any]:
return {"ok": True}
monkeypatch.setattr(intake_server, "post_draft", _ok)
msg = await intake_server.propose_draft({"title": "X"})
assert "Draft submitted" in msg
@pytest.mark.asyncio
async def test_propose_draft_reports_relay_failure(
monkeypatch: pytest.MonkeyPatch,
) -> None:
monkeypatch.setenv("ROBOCO_PROMPTER_SESSION_ID", "sess-1")
async def _fail(_sid: str, _draft: dict[str, Any]) -> dict[str, Any]:
return {"error": "http_503"}
monkeypatch.setattr(intake_server, "post_draft", _fail)
msg = await intake_server.propose_draft({"title": "X"})
assert "Could not submit the draft" in msg
assert "http_503" in msg
@@ -0,0 +1,75 @@
"""roboco-secretary MCP server — tools wrap the shared backend helpers as JSON.
The backend-calling logic (``secretary_driver._do_*``) is covered by the secretary
driver tests; here we only assert the MCP wrappers forward the right args and
return the backend result as a JSON string the model reads back.
"""
from __future__ import annotations
import json
from typing import Any
import pytest
from roboco.mcp import secretary_server
@pytest.mark.asyncio
async def test_read_company_state_returns_json(
monkeypatch: pytest.MonkeyPatch,
) -> None:
async def _state() -> dict[str, Any]:
return {"charter": "ship it", "tasks": {"pending": 3}}
monkeypatch.setattr(secretary_server, "_do_read_state", _state)
out = await secretary_server.read_company_state()
assert json.loads(out) == {"charter": "ship it", "tasks": {"pending": 3}}
@pytest.mark.asyncio
async def test_read_task_forwards_the_id(monkeypatch: pytest.MonkeyPatch) -> None:
seen: dict[str, Any] = {}
async def _task(task_id: str) -> dict[str, Any]:
seen["id"] = task_id
return {"id": task_id, "title": "T"}
monkeypatch.setattr(secretary_server, "_do_read_task", _task)
out = await secretary_server.read_task("task-9")
assert seen["id"] == "task-9"
assert json.loads(out)["title"] == "T"
@pytest.mark.asyncio
async def test_submit_directive_forwards_kind_and_payload(
monkeypatch: pytest.MonkeyPatch,
) -> None:
seen: dict[str, Any] = {}
async def _submit(kind: str, payload: dict[str, Any]) -> dict[str, Any]:
seen["kind"] = kind
seen["payload"] = payload
return {"queued": True}
monkeypatch.setattr(secretary_server, "_do_submit_directive", _submit)
out = await secretary_server.submit_directive(
"relay_message", {"channel": "announcements", "text": "hi"}
)
assert seen["kind"] == "relay_message"
assert seen["payload"] == {"channel": "announcements", "text": "hi"}
assert json.loads(out) == {"queued": True}
@pytest.mark.asyncio
async def test_submit_directive_tolerates_missing_payload(
monkeypatch: pytest.MonkeyPatch,
) -> None:
seen: dict[str, Any] = {}
async def _submit(_kind: str, payload: dict[str, Any]) -> dict[str, Any]:
seen["payload"] = payload
return {"ok": True}
monkeypatch.setattr(secretary_server, "_do_submit_directive", _submit)
await secretary_server.submit_directive("announce", None)
assert seen["payload"] == {}
+30 -32
View File
@@ -1,10 +1,11 @@
"""GROK cost budget kill-switch: kill a live container over the cost ceiling.
opencode exposes no usage hook to a plugin, so the budget kill-switch lives in
the orchestrator: it reads each live GROK container's cumulative opencode cost
and kills + evicts it past ROBOCO_GROK_MAX_COST_USD (also catching runaway-loop
token burn). The cost computation itself is covered in opencode_usage tests; here
cost_for_session is stubbed so the kill DECISION is exercised deterministically.
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.
"""
from __future__ import annotations
@@ -15,22 +16,32 @@ import pytest
from roboco.models.runtime import AgentInstance
from roboco.runtime.orchestrator import AgentOrchestrator, AgentState
_COST_FN = "roboco.llm.providers.opencode_usage.cost_for_session"
def _grok_instance(provider_type: str = "grok") -> AgentInstance:
cfg = type("C", (), {"provider_type": provider_type, "model": "grok-build-0.1"})()
cfg = type("C", (), {"provider_type": provider_type, "model": "grok-build"})()
return AgentInstance(agent_id="be-dev-1", state=AgentState.ACTIVE, config=cfg)
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
@pytest.mark.asyncio
async def test_cost_over_cap_kills_and_evicts(monkeypatch: pytest.MonkeyPatch) -> None:
orch = AgentOrchestrator.__new__(AgentOrchestrator)
orch._grok_max_cost_usd = 5.0
orch._instances = {"be-dev-1": _grok_instance()}
remove_mock = AsyncMock()
monkeypatch.setattr(orch, "_remove_container", remove_mock)
monkeypatch.setattr(_COST_FN, lambda *_a, **_k: (None, 7.5))
orch, remove_mock = _orch(monkeypatch, cap=5.0, cost=7.5)
await orch._enforce_grok_cost_budget()
@@ -40,12 +51,7 @@ async def test_cost_over_cap_kills_and_evicts(monkeypatch: pytest.MonkeyPatch) -
@pytest.mark.asyncio
async def test_cost_under_cap_spares(monkeypatch: pytest.MonkeyPatch) -> None:
orch = AgentOrchestrator.__new__(AgentOrchestrator)
orch._grok_max_cost_usd = 5.0
orch._instances = {"be-dev-1": _grok_instance()}
remove_mock = AsyncMock()
monkeypatch.setattr(orch, "_remove_container", remove_mock)
monkeypatch.setattr(_COST_FN, lambda *_a, **_k: (None, 1.0))
orch, remove_mock = _orch(monkeypatch, cap=5.0, cost=1.0)
await orch._enforce_grok_cost_budget()
@@ -55,12 +61,7 @@ async def test_cost_under_cap_spares(monkeypatch: pytest.MonkeyPatch) -> None:
@pytest.mark.asyncio
async def test_cap_zero_disables_the_sweep(monkeypatch: pytest.MonkeyPatch) -> None:
orch = AgentOrchestrator.__new__(AgentOrchestrator)
orch._grok_max_cost_usd = 0.0
orch._instances = {"be-dev-1": _grok_instance()}
remove_mock = AsyncMock()
monkeypatch.setattr(orch, "_remove_container", remove_mock)
monkeypatch.setattr(_COST_FN, lambda *_a, **_k: (None, 999.0))
orch, remove_mock = _orch(monkeypatch, cap=0.0, cost=999.0)
await orch._enforce_grok_cost_budget()
@@ -70,12 +71,9 @@ async def test_cap_zero_disables_the_sweep(monkeypatch: pytest.MonkeyPatch) -> N
@pytest.mark.asyncio
async def test_non_grok_container_is_ignored(monkeypatch: pytest.MonkeyPatch) -> None:
orch = AgentOrchestrator.__new__(AgentOrchestrator)
orch._grok_max_cost_usd = 5.0
orch._instances = {"be-dev-1": _grok_instance(provider_type="anthropic")}
remove_mock = AsyncMock()
monkeypatch.setattr(orch, "_remove_container", remove_mock)
monkeypatch.setattr(_COST_FN, lambda *_a, **_k: (None, 999.0))
orch, remove_mock = _orch(
monkeypatch, cap=5.0, cost=999.0, provider_type="anthropic"
)
await orch._enforce_grok_cost_budget()
+45 -67
View File
@@ -1,13 +1,15 @@
"""GROK agents capture token usage/cost from their opencode SQLite store.
"""GROK agents capture token usage/cost from their captured ``usage.json``.
A Grok agent runs opencode no SDK /usage/status server and no Claude
transcript so finalize must read opencode.db (mounted into the orchestrator)
instead. Reasoning folds into output (it bills at the output rate).
A Grok agent runs the grok CLI no SDK /usage/status server and no Claude
transcript so finalize reads the ``usage.json`` the entrypoint / interactive
driver wrote to the per-agent data dir (mounted into the orchestrator). grok
reports a single cumulative total with no input/output split, so it folds into
output (it bills at the output rate).
"""
from __future__ import annotations
import sqlite3
import json
from typing import TYPE_CHECKING
import pytest
@@ -18,82 +20,58 @@ if TYPE_CHECKING:
from pathlib import Path
def _make_db(path: Path, cols: dict[str, float]) -> None:
con = sqlite3.connect(path)
con.execute(
"CREATE TABLE session (id TEXT, tokens_input INT, tokens_output INT, "
"tokens_cache_read INT, tokens_cache_write INT, tokens_reasoning INT, "
"cost REAL)"
)
con.execute(
"INSERT INTO session (id, tokens_input, tokens_output, tokens_cache_read, "
"tokens_cache_write, tokens_reasoning, cost) VALUES (?,?,?,?,?,?,?)",
(
"s1",
cols["tokens_input"],
cols["tokens_output"],
cols["tokens_cache_read"],
cols["tokens_cache_write"],
cols["tokens_reasoning"],
cols["cost"],
def _write_usage(path: Path, total_tokens: int, cost_usd: float) -> None:
path.write_text(
json.dumps(
{"model": "grok-build", "total_tokens": total_tokens, "cost_usd": cost_usd}
),
encoding="utf-8",
)
con.commit()
con.close()
def test_grok_usage_folds_reasoning_into_output(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
db = tmp_path / "opencode.db"
_make_db(
db,
{
"tokens_input": 100,
"tokens_output": 50,
"tokens_reasoning": 30,
"tokens_cache_read": 10,
"tokens_cache_write": 5,
"cost": 0.02,
},
)
orch = AgentOrchestrator.__new__(AgentOrchestrator)
monkeypatch.setattr(orch, "_opencode_db_path", lambda _aid: str(db))
# reasoning (30) folded into output (50) → 80; bills at the output rate.
assert orch._grok_usage_from_opencode("be-dev-1") == (100, 80, 10, 5)
def test_grok_usage_zero_when_store_missing(
def test_grok_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, "_opencode_db_path", lambda _aid: str(tmp_path / "absent.db")
orch, "_grok_usage_json", lambda _aid: json.loads(usage.read_text())
)
assert orch._grok_usage_from_opencode("be-dev-1") == (0, 0, 0, 0)
# The whole total folds into output (no input/output split from the CLI).
assert orch._grok_usage_tokens("be-dev-1") == (0, 180, 0, 0)
def test_grok_usage_zero_when_store_missing(monkeypatch: pytest.MonkeyPatch) -> None:
orch = AgentOrchestrator.__new__(AgentOrchestrator)
monkeypatch.setattr(orch, "_grok_usage_json", lambda _aid: None)
assert orch._grok_usage_tokens("be-dev-1") == (0, 0, 0, 0)
def test_grok_cost_read_from_usage_json(monkeypatch: pytest.MonkeyPatch) -> None:
captured_cost = 3.25
orch = AgentOrchestrator.__new__(AgentOrchestrator)
monkeypatch.setattr(
orch,
"_grok_usage_json",
lambda _aid: {"cost_usd": captured_cost, "total_tokens": 9},
)
assert orch._grok_cost_usd("be-dev-1") == captured_cost
monkeypatch.setattr(orch, "_grok_usage_json", lambda _aid: None)
assert orch._grok_cost_usd("be-dev-1") == 0.0
@pytest.mark.asyncio
async def test_resolve_final_usage_routes_grok_to_opencode(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
async def test_resolve_final_usage_routes_grok_to_usage_json(
monkeypatch: pytest.MonkeyPatch,
) -> None:
db = tmp_path / "opencode.db"
_make_db(
db,
{
"tokens_input": 7,
"tokens_output": 3,
"tokens_reasoning": 2,
"tokens_cache_read": 0,
"tokens_cache_write": 0,
"cost": 0.01,
},
)
orch = AgentOrchestrator.__new__(AgentOrchestrator)
monkeypatch.setattr(orch, "_opencode_db_path", lambda _aid: str(db))
monkeypatch.setattr(
orch, "_grok_usage_json", lambda _aid: {"total_tokens": 12, "cost_usd": 0.01}
)
cfg = type("C", (), {"provider_type": "grok"})()
orch._instances = {"be-dev-1": AgentInstance(agent_id="be-dev-1", config=cfg)}
# No SDK fetch / transcript read for GROK — usage comes from opencode.db.
assert await orch._resolve_final_token_usage("be-dev-1") == (7, 5, 0, 0)
# No SDK fetch / transcript read for GROK — usage comes from usage.json.
assert await orch._resolve_final_token_usage("be-dev-1") == (0, 12, 0, 0)
@@ -1,12 +1,16 @@
"""Interactive intake/secretary builders fork a GROK route onto opencode.
"""Interactive intake/secretary builders fork a GROK route onto the grok CLI.
A GROK route swaps the Claude SDK-driver image for the opencode-serve image and
the ANTHROPIC_* env for XAI_* + the opencode store mount; every other
provider keeps the Claude path's ANTHROPIC_* behaviour.
A GROK route swaps the Claude SDK-driver image for the grok-CLI prompter/secretary
image and the ANTHROPIC_* env for the subscription auth mount + the per-agent
usage mount (no metered xAI key, no permission env the driver computes the grok
permission flags). Every other provider keeps the Claude path's ANTHROPIC_*.
"""
from __future__ import annotations
from typing import TYPE_CHECKING
from roboco.llm.providers import grok as grok_provider
from roboco.runtime.orchestrator import (
GROK_PROMPTER_IMAGE,
GROK_SECRETARY_IMAGE,
@@ -15,20 +19,21 @@ from roboco.runtime.orchestrator import (
_SecretaryRunSpec,
)
if TYPE_CHECKING:
from pathlib import Path
import pytest
_HOSTS: dict[str, str | None] = {
"claude": "/h/.claude",
"prompt": "/h/p.md",
"workspaces": "/h/ws",
"opencode": "/h/oc/intake-1",
"grok_usage": "/h/gu/intake-1",
}
def _intake_spec(
provider_type: str,
*,
base_url: str | None,
token: str | None,
grok_variant: str | None = None,
provider_type: str, *, base_url: str | None, token: str | None
) -> _IntakeRunSpec:
return _IntakeRunSpec(
container_name="roboco-agent-intake-1",
@@ -38,57 +43,46 @@ def _intake_spec(
hosts=_HOSTS,
session_id="sess-1",
cwd="/data/workspace",
cli_model="grok-build-0.1",
cli_model="grok-build",
api_url="http://roboco-orchestrator:8000",
provider_base_url=base_url,
provider_auth_token=token,
provider_type=provider_type,
model="grok-build-0.1",
grok_variant=grok_variant,
model="grok-build",
)
def test_intake_grok_uses_xai_env_and_opencode_mount() -> None:
cmd = AgentOrchestrator._build_intake_run_cmd(
_intake_spec(
"grok",
base_url="https://api.x.ai/v1",
token="xai-key",
grok_variant="minimal",
)
)
assert "XAI_API_KEY=xai-key" in cmd
assert "XAI_BASE_URL=https://api.x.ai/v1" in cmd
assert "ROBOCO_AGENT_MODEL=grok-build-0.1" in cmd
assert "ROBOCO_SYSTEM_PROMPT=/app/system-prompt.md" in cmd
assert "/h/oc/intake-1:/home/agent/.local/share/opencode" in cmd
# Per-role reasoning effort reaches the container for the serve driver.
assert "ROBOCO_GROK_VARIANT=minimal" in cmd
assert cmd[-1] == GROK_PROMPTER_IMAGE
# The xAI endpoint is never mislabelled as Anthropic.
assert not any(c.startswith("ANTHROPIC_") for c in cmd)
# Intake is read-only (no code edits, no shell) but reads sibling product
# repos OUTSIDE its cwd, so it keeps external-directory reads.
assert "ROBOCO_GROK_EDIT_PERMISSION=deny" in cmd
assert "ROBOCO_GROK_BASH_PERMISSION=deny" in cmd
assert "ROBOCO_GROK_EXTERNAL_DIR_PERMISSION=allow" in cmd
def test_intake_anthropic_omits_grok_permission_env() -> None:
# The opencode permission env is a GROK-only contract; the Claude path never
# sets it (it gates tools via the SDK can_use_tool allowlist instead).
cmd = AgentOrchestrator._build_intake_run_cmd(
_intake_spec("anthropic", base_url="https://api.anthropic.com", token="sk-ant")
)
assert not any(c.startswith("ROBOCO_GROK_EDIT_PERMISSION=") for c in cmd)
assert not any(c.startswith("ROBOCO_GROK_BASH_PERMISSION=") for c in cmd)
def test_intake_grok_omits_variant_when_unset() -> None:
def test_intake_grok_uses_grok_cli_usage_mount_and_env() -> None:
cmd = AgentOrchestrator._build_intake_run_cmd(
_intake_spec("grok", base_url="https://api.x.ai/v1", token="xai-key")
)
assert not any(c.startswith("ROBOCO_GROK_VARIANT=") for c in cmd)
# The per-agent usage dir is mounted so finalize reads usage.json back.
assert "/h/gu/intake-1:/home/agent/.grok-usage" in cmd
assert "ROBOCO_AGENT_MODEL=grok-build" in cmd
assert "ROBOCO_GROK_USAGE_FILE=/home/agent/.grok-usage/usage.json" in cmd
assert cmd[-1] == GROK_PROMPTER_IMAGE
# No metered xAI key, no Anthropic mislabelling, no stale opencode contract.
assert not any(c.startswith("XAI_") for c in cmd)
assert not any(c.startswith("ANTHROPIC_") for c in cmd)
assert not any(c.startswith("ROBOCO_GROK_VARIANT") for c in cmd)
assert not any(c.startswith("ROBOCO_GROK_EDIT_PERMISSION") for c in cmd)
assert "/home/agent/.local/share/opencode" not in " ".join(cmd)
def test_intake_grok_mounts_subscription_auth_when_present(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
# The auth mount is .exists()-guarded; point the host dir at a tmp ~/.grok
# holding an auth.json so the mount is emitted.
grok_dir = tmp_path / ".grok"
grok_dir.mkdir()
(grok_dir / "auth.json").write_text("{}", encoding="utf-8")
monkeypatch.setattr(grok_provider, "GROK_AUTH_HOST_PATH", str(grok_dir))
cmd = AgentOrchestrator._build_intake_run_cmd(
_intake_spec("grok", base_url="https://api.x.ai/v1", token="xai-key")
)
assert f"{grok_dir / 'auth.json'}:/home/agent/.grok/auth.json:ro" in cmd
def test_intake_anthropic_keeps_anthropic_env() -> None:
@@ -98,34 +92,35 @@ def test_intake_anthropic_keeps_anthropic_env() -> None:
assert "ANTHROPIC_BASE_URL=https://api.anthropic.com" in cmd
assert "ANTHROPIC_AUTH_TOKEN=sk-ant" in cmd
assert not any(c.startswith("XAI_") for c in cmd)
assert not any(c.startswith("ROBOCO_GROK_USAGE_FILE") for c in cmd)
assert cmd[-1] == "roboco-agent-prompter"
def test_secretary_grok_uses_openai_env_and_grok_image() -> None:
def test_secretary_grok_uses_grok_cli_env_and_keeps_hmac() -> None:
spec = _SecretaryRunSpec(
container_name="roboco-agent-secretary-1",
image=GROK_SECRETARY_IMAGE,
hosts={"claude": "/h/.claude", "prompt": "/h/p.md", "opencode": "/h/oc/sec-1"},
hosts={
"claude": "/h/.claude",
"prompt": "/h/p.md",
"grok_usage": "/h/gu/sec-1",
},
session_id="sess-2",
cwd="/app",
cli_model="grok-build-0.1",
cli_model="grok-build",
api_url="http://roboco-orchestrator:8000",
agent_uuid="uuid-sec",
agent_token="hmac-secretary",
provider_base_url="https://api.x.ai/v1",
provider_auth_token="xai-key",
provider_type="grok",
model="grok-build-0.1",
model="grok-build",
)
cmd = AgentOrchestrator._build_secretary_run_cmd(spec)
assert "XAI_API_KEY=xai-key" in cmd
assert "/h/oc/sec-1:/home/agent/.local/share/opencode" in cmd
assert "/h/gu/sec-1:/home/agent/.grok-usage" in cmd
assert "ROBOCO_AGENT_MODEL=grok-build" in cmd
# The HMAC identity the directive tools authenticate with survives.
assert "ROBOCO_AGENT_TOKEN=hmac-secretary" in cmd
assert cmd[-1] == GROK_SECRETARY_IMAGE
assert not any(c.startswith("XAI_") for c in cmd)
assert not any(c.startswith("ANTHROPIC_") for c in cmd)
# The Secretary is read-only and reads only /app + the API, so edit/bash
# are denied and it gets NO external-directory reads (unlike intake).
assert "ROBOCO_GROK_EDIT_PERMISSION=deny" in cmd
assert "ROBOCO_GROK_BASH_PERMISSION=deny" in cmd
assert "ROBOCO_GROK_EXTERNAL_DIR_PERMISSION=deny" in cmd
@@ -153,7 +153,7 @@ async def test_reaper_kills_and_releases_wedged_grok_container(
) -> None:
"""A GROK container idle past the kill TTL is killed, evicted, and released.
Unlike a Claude agent, a wedged opencode container is ACTIVE yet fires no
Unlike a Claude agent, a wedged grok container is ACTIVE yet fires no
verb, so the live-instance skip would shield it forever. Past the longer
grok-idle TTL the watchdog removes the container and drops it from
`_instances`, so the same reap pass then unclaims the task.