Files
roboco/tests/unit/agent_sdk/test_grok_interactive_entrypoints.py
T
Renn F a402de61bc fix(grok): address adversarial-review findings across the grok-CLI conversion
A 7-dimension adversarial review (find -> independently refute) surfaced 14 real
issues; fixed each:

Runtime bugs
- GrokCliSession.send drained stdout fully BEFORE stderr — a >64KB stderr burst
  would deadlock the turn forever (spinner never clears). Drain stderr
  concurrently, and add a per-turn watchdog (ROBOCO_GROK_TURN_TIMEOUT_SECONDS,
  default 600s) that kills a wedged process and emits error+turn_end.
- Crash-restarted grok agents launched `grok -p ""` (empty prompt) — Claude gets
  a scan-for-work fallback. Default the prompt in _spawn_container so every
  dedicated provider gets it too.
- _grok_usage_json read /data/grok-usage unconditionally while its writers branch
  compose-vs-local, so a local-mode agent finalized at $0 and the cost-cap was
  inert. Single-source the path in a new _grok_usage_dir helper (read == write).
- GrokCliSession secretary role fell through to "unknown" (get_agent_role returns
  a truthy sentinel, never None), defeating the ROBOCO_AGENT_ROLE fallback.

Parity / hardening
- --deny set was missing `git tag -d` / `git reflog delete` that the Claude
  bash-guard blocks — added them (the "same set" claim is now true).
- Interactive mains now install the bash-guard hook too (defense-in-depth).
- Compose: collapse the GROK_AUTH_DIR / ROBOCO_HOST_GROK_DIR auth-mount pair into
  one canonical var so a partial override can't silently break agent auth.

Docs / comments
- Panel routing card + architecture security doc no longer say Grok runs on the
  deleted opencode runtime; orchestrator comments point at the renamed entrypoint.

Tests
- Cover the interactive _render_grok_config MCP wiring (ModuleNotFound guard +
  secretary HMAC env), the cost-cap kill-failure + interactive relay-close paths,
  the local-mode usage read, the role fallback, the turn timeout, and the new
  git denies. (#13 — a separate grok "Write" tool — investigated: grok's only
  built-in file-mutation tool is search_replace, already removed; no gap.)

Gate green: ruff, mypy, xenon, tests.
2026-06-19 06:09:15 +02:00

72 lines
2.6 KiB
Python

"""Interactive grok entrypoints render the load-bearing MCP wiring into config.toml.
``_render_grok_config`` is the only synchronous, testable part of the interactive
mains (``main()`` needs the live container). It must produce the exact MCP
invocation the branch depends on — ``uv run --directory /app --no-sync`` (the
ModuleNotFound guard), ``UV_PROJECT_ENVIRONMENT=/app/.venv``, and (secretary) the
HMAC identity env the directive tools authenticate with.
"""
from __future__ import annotations
import tomllib
from typing import TYPE_CHECKING
from roboco.agent_sdk import grok_intake_main, grok_secretary_main
if TYPE_CHECKING:
from pathlib import Path
import pytest
def test_intake_render_wires_roboco_intake_mcp(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
cfg = tmp_path / ".grok" / "config.toml"
monkeypatch.setattr(grok_intake_main, "GROK_CONFIG_PATH", cfg)
grok_intake_main._render_grok_config("http://orch:8000", "sess-1")
parsed = tomllib.loads(cfg.read_text())
server = parsed["mcp_servers"]["roboco-intake"]
assert server["command"] == "uv"
# The ModuleNotFound guard: --directory /app + --no-sync, installed module.
assert server["args"] == [
"run",
"--directory",
"/app",
"--no-sync",
"python",
"-m",
"roboco.mcp.intake_server",
]
assert server["env"]["UV_PROJECT_ENVIRONMENT"] == "/app/.venv"
assert server["env"]["ROBOCO_API_URL"] == "http://orch:8000"
assert server["env"]["ROBOCO_PROMPTER_SESSION_ID"] == "sess-1"
def test_secretary_render_wires_mcp_and_hmac_identity(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
cfg = tmp_path / ".grok" / "config.toml"
monkeypatch.setattr(grok_secretary_main, "GROK_CONFIG_PATH", cfg)
monkeypatch.setenv("ROBOCO_AGENT_ID", "uuid-sec")
monkeypatch.setenv("ROBOCO_AGENT_ROLE", "secretary")
monkeypatch.setenv("ROBOCO_AGENT_TOKEN", "hmac-xyz")
grok_secretary_main._render_grok_config("http://orch:8000")
parsed = tomllib.loads(cfg.read_text())
server = parsed["mcp_servers"]["roboco-secretary"]
assert server["args"] == [
"run",
"--directory",
"/app",
"--no-sync",
"python",
"-m",
"roboco.mcp.secretary_server",
]
# The HMAC identity the directive tools authenticate with must flow through.
assert server["env"]["ROBOCO_AGENT_TOKEN"] == "hmac-xyz"
assert server["env"]["ROBOCO_AGENT_ID"] == "uuid-sec"
assert server["env"]["ROBOCO_AGENT_ROLE"] == "secretary"
assert server["env"]["UV_PROJECT_ENVIRONMENT"] == "/app/.venv"