mirror of
https://github.com/rennf93/roboco.git
synced 2026-08-03 07:23:24 +02:00
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:
@@ -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
|
||||
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user