Files
roboco/docker/scripts/grok-cli-agent-entrypoint.sh
T
Renn F c139e2d017 feat(grok-cli): wire usage capture into the run (session id + post-run extract)
The provider pins a fixed session id (ROBOCO_AGENT_SESSION_ID, reused from the
agent session id as on the Claude path); the entrypoint passes it to
'grok -p -s <id>' so the run's session store is locatable, then runs the usage
reader post-run (best-effort) to write the captured tokens + cost. The
orchestrator-side finalize that reads that file follows.
2026-06-19 03:32:32 +02:00

80 lines
3.9 KiB
Bash
Executable File

#!/usr/bin/env bash
# Entrypoint for the roboco-agent-grok-cli image (one-shot delivery roles).
#
# Runs an agent on xAI's official `grok` CLI (Grok Build), authenticated by the
# SuperGrok subscription via a mounted ~/.grok/auth.json — the parity analogue of
# the Claude Code path's mounted ~/.claude. The gateway, identity, and workspace
# are mounted by the orchestrator's shared container assembly (the same that
# wires Claude); this entrypoint renders the grok runtime config from that mount
# and runs the CLI headless.
set -euo pipefail
# Render ~/.grok/config.toml (the MCP gateway) + the per-role grok flags. Run
# from /app so `python -m` resolves the INSTALLED roboco package: dev/doc/qa
# agents run at their workspace-clone cwd, whose own roboco/ dir would shadow it
# on the sys.path front (the ModuleNotFound lesson). The render reads
# ROBOCO_MCP_CONFIG + ROBOCO_AGENT_ID and writes the config + an args file.
( cd /app && python -m roboco.llm.providers.grok_cli_config )
GROK_ARGS_FILE="${ROBOCO_GROK_ARGS_FILE:-/tmp/roboco-grok-args}"
mapfile -t GROK_ARGS < "$GROK_ARGS_FILE"
# Prompt-injection guard (parity with the Claude UserPromptSubmit hook): the task
# prompt is DATA, not instructions — refuse a poisoned one before the model sees
# it. Same patterns as docker/scripts/user-prompt-hook.sh; run from /app too.
if ! ( cd /app && python -m roboco.agent_sdk.prompt_guard "${ROBOCO_INITIAL_PROMPT:-}" ); then
echo "Refusing to run: task prompt matched a prompt-injection pattern." >&2
exit 1
fi
# Run the agent. The prompt comes from an env var (never an untrusted argv
# positional). `< /dev/null` keeps the headless run from blocking on stdin. We
# do NOT `exec`: the script regains control to inspect the result + exit code.
# `--cwd` is the agent's workspace (the orchestrator sets the container workdir
# to it, mirroring the Claude path). Per-role flags (tool removal / deny rules /
# effort / turn cap) come from the rendered args file.
RUN_LOG="/tmp/grok-run.json"
ERR_LOG="/tmp/grok-run.err"
WORKSPACE="${ROBOCO_WORKSPACE:-$PWD}"
# A fixed session id (set by the provider) makes the run's session store
# locatable for usage capture below; absent, grok generates its own.
SESSION_ARGS=()
[ -n "${ROBOCO_AGENT_SESSION_ID:-}" ] && SESSION_ARGS=(-s "${ROBOCO_AGENT_SESSION_ID}")
set +e
grok -p "${ROBOCO_INITIAL_PROMPT:-}" \
-m "${ROBOCO_AGENT_MODEL:-grok-build}" \
--cwd "$WORKSPACE" \
--output-format json \
"${SESSION_ARGS[@]}" \
"${GROK_ARGS[@]}" \
< /dev/null > "$RUN_LOG" 2> "$ERR_LOG"
run_rc=$?
set -e
# Surface the run output + any stderr into the agent log.
cat "$RUN_LOG"
[ -s "$ERR_LOG" ] && cat "$ERR_LOG" >&2
# Capture token usage from the grok session store (~/.grok/sessions). The
# orchestrator reads the written usage file back at finalize — the grok analogue
# of the Claude transcript. Best-effort; never fails the run. Run from /app for
# the same module-resolution reason as the render above.
( cd /app && ROBOCO_GROK_RUN_CWD="$WORKSPACE" \
python -m roboco.llm.providers.grok_cli_usage ) || true
# Rate-limit detection (parity with the opencode B4 path): an xAI 429 / quota
# error ends the run without a terminal verb. Detect it from the run output and
# exit 75 (EX_TEMPFAIL) so the orchestrator PARKS the grok provider instead of
# the dispatcher respawning the same task every tick (429 -> exit -> respawn, a
# token loop). A rate-limited task is retried once the limit lifts, not dropped.
if grep -qiE '(\b429\b|rate.?limit|too many requests|quota|insufficient_quota)' \
"$RUN_LOG" "$ERR_LOG" 2>/dev/null; then
echo "[grok] rate-limited — exiting 75 so the orchestrator parks the provider;" \
"the task is retried when the limit lifts." >&2
exit 75
fi
# A graceful exit without a terminal verb is handled server-side by the
# orchestrator (_handle_stopped_container substitutes the still-owned task) —
# the grok-cli runtime needs no in-container SDK server for that.
exit "$run_rc"