Files
ai-chess/scripts/pane-agent.sh
T
ai-chess 51e63a8c7a feat(docker): bake claude+codex CLI + auth into image, non-root user
Dockerfile additions:
- Node 20 (NodeSource repo) + @anthropic-ai/claude-code@2.1.141 via npm.
- codex 0.130.0 musl tarball from github releases, dropped into
  /usr/local/bin/codex.
- jq + xz-utils + gnupg apt packages.
- Symlink /usr/games/stockfish → /usr/local/bin/stockfish (Debian
  puts it under /usr/games which isn't on the default PATH).
- Non-root user 'chess' (uid 1000) because claude-code refuses to run
  with --dangerously-skip-permissions as root; HOME=/home/chess.
- Pre-seeded /home/chess/.claude/settings.json + /home/chess/.claude.json:
  - hasCompletedOnboarding=true / lastOnboardingVersion='2.1.141'
  - projects['/app'].hasTrustDialogAccepted=true (mirrors the user's
    claude-trust-current-dir helper, but baked since cwd is always /app)
  - permissions.defaultMode='bypassPermissions'
  - skipDangerousModePermissionPrompt=true
  Without these, every fresh tmux pane hangs on the theme picker, then
  the workspace-trust dialog, never reaching its first MCP call.

docker-compose.yml:
- ANTHROPIC_BASE_URL + ANTHROPIC_AUTH_TOKEN env (loaded from /opt/chessai/.env).
- ./secrets/codex mounted read-only at /home/chess/.codex so the user's
  cliproxyapi-flavored codex config + auth.json travel with the deploy
  but stay outside the image layers.

deploy/codex/config.toml:
- Minimal codex config (cliproxyapi provider, gpt-5.5 xhigh fast,
  chess MCP url placeholder overridden per-side by pane-agent.sh).

scripts/pane-agent.sh:
- Use CLAUDE_BIN / CODEX_BIN from PATH (mac vs linux dynamics);
  the inline -c mcp_servers.chess.url override removes the need for
  'codex mcp add/remove' that polluted ~/.codex/config.toml on Mac runs.

Verified end-to-end on chess.danosito.com: AI-vs-AI game starts,
claude (white) inside the container makes the first move with a
toxic russian comment, MCP HTTP roundtrip works.
2026-05-15 02:05:18 +03:00

99 lines
2.8 KiB
Bash
Executable File

#!/usr/bin/env bash
# scripts/pane-agent.sh <white|black>
# Launched in a tmux pane; talks to backend on 127.0.0.1:7833 to figure out
# which agent (claude|codex) plays this side, writes per-side config files
# under ./config/, then exec's the agent TUI.
set -euo pipefail
side="${1:?usage: pane-agent.sh <white|black>}"
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
CFG="$ROOT/config"
mkdir -p "$CFG"
# wait until backend ready
for _ in $(seq 1 120); do
if curl -fsS "http://127.0.0.1:7833/api/state" >/dev/null 2>&1; then break; fi
sleep 0.5
done
# poll agent binding (start.sh POSTs /api/new-game before creating panes)
agent=""
for _ in $(seq 1 120); do
agent="$(curl -fsS "http://127.0.0.1:7833/api/agent-binding/${side}" 2>/dev/null \
| python3 -c 'import sys,json;
try: print(json.load(sys.stdin).get("agent",""))
except Exception: print("")' 2>/dev/null || true)"
if [ -n "$agent" ]; then break; fi
sleep 0.5
done
if [ -z "$agent" ]; then
echo "[pane-agent] no agent binding for $side after 60s — dropping to shell"
exec bash
fi
case "$side" in
white) claude_theme='light' ; codex_theme='Catppuccin Latte' ;;
black) claude_theme='dark' ; codex_theme='Catppuccin Mocha' ;;
*) echo "bad side: $side"; exec bash ;;
esac
echo "[pane-agent] side=$side agent=$agent claude_theme=$claude_theme codex_theme=\"$codex_theme\""
if [ "$agent" = "human" ]; then
cat <<EOF
[pane-agent] $side is controlled by a human — playing through the browser.
Nothing to run here. You can leave this pane attached or detach.
EOF
exec sleep infinity
fi
cd "$ROOT"
: "${CLAUDE_BIN:=$(command -v claude || echo /usr/local/bin/claude)}"
: "${CODEX_BIN:=$(command -v codex || echo /usr/local/bin/codex)}"
case "$agent" in
claude)
cat > "$CFG/claude-settings.${side}.json" <<EOF
{
"theme": "${claude_theme}",
"permissions": { "allow": ["*"], "defaultMode": "bypassPermissions" }
}
EOF
cat > "$CFG/claude-mcp.${side}.json" <<EOF
{
"mcpServers": {
"chess": {
"type": "http",
"url": "http://127.0.0.1:7833/mcp/${side}"
}
}
}
EOF
exec "$CLAUDE_BIN" \
--model opus \
--effort max \
--mcp-config "$CFG/claude-mcp.${side}.json" \
--settings "$CFG/claude-settings.${side}.json" \
--strict-mcp-config \
--dangerously-skip-permissions
;;
codex)
# Override the global config's chess MCP url (each side talks to its own
# path). The user's persistent ~/.codex/config.toml does NOT get
# rewritten — we just pass an inline override.
exec "$CODEX_BIN" \
-c 'model="gpt-5.5"' \
-c 'model_reasoning_effort="xhigh"' \
-c 'service_tier="fast"' \
-c "tui.theme=\"${codex_theme}\"" \
-c "mcp_servers.chess.url=\"http://127.0.0.1:7833/mcp/${side}\"" \
--dangerously-bypass-approvals-and-sandbox
;;
*)
echo "unknown agent: $agent"; exec bash ;;
esac