From 33d6af764a0565706719789749bf594b0f5613fd Mon Sep 17 00:00:00 2001 From: ai-chess Date: Thu, 14 May 2026 23:34:03 +0300 Subject: [PATCH] fix: ulid API + tolerate missing tmux + dispatch-first endpoint - coordinator: use `ulid.new().str` (ulid-py 1.x API), not `ULID()` - coordinator._submit: swallow tmux errors so game logic continues - app: split first-prompt dispatch into POST /api/dispatch-first - start.sh: POST /api/dispatch-first ~12s after tmux create - test_coordinator: isolate GAMES_DIR via tmp_path --- scripts/start.sh | 5 +++++ src/ai_chess/app.py | 14 +++++++++++--- src/ai_chess/coordinator.py | 11 ++++++++--- tests/test_coordinator.py | 7 +++++++ 4 files changed, 31 insertions(+), 6 deletions(-) diff --git a/scripts/start.sh b/scripts/start.sh index decd461..d4e61b6 100755 --- a/scripts/start.sh +++ b/scripts/start.sh @@ -46,8 +46,13 @@ tmux new-session -d -s chess -x 220 -y 60 "$ROOT/scripts/pane-agent.sh white" tmux split-window -h -p 50 -t chess:0.0 "$ROOT/scripts/pane-agent.sh black" tmux split-window -v -p 15 -t chess:0 "tail -F $LOGS/backend.log" +# Give claude/codex ~12s to boot in their panes, then ask coordinator to +# inject the first prompt to the white pane. +( sleep 12 && curl -fsS -X POST http://127.0.0.1:7833/api/dispatch-first >/dev/null ) & + echo echo "[ai-chess] open: http://127.0.0.1:7833/" echo "[ai-chess] attach: tmux attach -t chess" +echo "[ai-chess] first prompt fires in ~12s (after agents boot)" echo tmux attach -t chess diff --git a/src/ai_chess/app.py b/src/ai_chess/app.py index 003aa98..a0240cc 100644 --- a/src/ai_chess/app.py +++ b/src/ai_chess/app.py @@ -120,11 +120,19 @@ def build_app(state: state_mod.AppState) -> FastAPI: @app.post("/api/new-game") async def new_game_post() -> dict[str, Any]: await start_new_game(state) - runtime = getattr(state, "runtime", None) - if runtime and runtime.get("coord"): - await runtime["coord"].dispatch_first_prompt() return state.game.get_state() # type: ignore[union-attr] + @app.post("/api/dispatch-first") + async def dispatch_first() -> dict[str, Any]: + runtime = getattr(state, "runtime", None) + if not runtime or not runtime.get("coord"): + return {"ok": False, "reason": "coordinator_not_running"} + try: + await runtime["coord"].dispatch_first_prompt() + return {"ok": True} + except Exception as e: + return {"ok": False, "reason": "dispatch_failed", "detail": str(e)} + @app.get("/api/agent-binding/{side}") async def agent_binding(side: str) -> dict[str, Any]: s = Side.from_str(side) diff --git a/src/ai_chess/coordinator.py b/src/ai_chess/coordinator.py index 60eac3a..1c5b4b9 100644 --- a/src/ai_chess/coordinator.py +++ b/src/ai_chess/coordinator.py @@ -4,7 +4,7 @@ import asyncio import random from typing import Any -from ulid import ULID +import ulid from ai_chess.chess_game import ChessGame from ai_chess.persistence import save_game @@ -45,7 +45,7 @@ async def start_new_game(state: AppState) -> ChessGame: state.bindings = make_bindings(side_to_agent) state.first_move_sent = {Side.WHITE: False, Side.BLACK: False} state.nudge_count = {Side.WHITE: 0, Side.BLACK: 0} - gid = str(ULID()) + gid = ulid.new().str state.game = ChessGame( game_id=gid, agents={s.value: b.to_dict() for s, b in state.bindings.items()}, @@ -74,7 +74,12 @@ class Coordinator: return self.pane_for[side.value] async def _submit(self, side: Side, text: str) -> None: - await self.tmux.submit(pane=self._pane(side), text=text) + # Tmux failures (missing session, dead pane) should not break the game + # state machine. Log and continue. + try: + await self.tmux.submit(pane=self._pane(side), text=text) + except Exception as e: + print(f"[coordinator] tmux submit to {side.value} failed: {e}", flush=True) async def _send_first(self, side: Side, *, opponent_san: str | None, opponent_comment: str | None) -> None: diff --git a/tests/test_coordinator.py b/tests/test_coordinator.py index 223ae74..ce67b64 100644 --- a/tests/test_coordinator.py +++ b/tests/test_coordinator.py @@ -2,12 +2,19 @@ import asyncio import pytest +import ai_chess.coordinator as coord_mod from ai_chess.chess_game import ChessGame from ai_chess.coordinator import Coordinator from ai_chess.sides import Side from ai_chess.state import AppState, AgentBinding +@pytest.fixture(autouse=True) +def _isolated_games_dir(tmp_path, monkeypatch): + """Redirect save_game writes into a tmp directory so tests don't pollute games/.""" + monkeypatch.setattr(coord_mod, "GAMES_DIR", tmp_path / "games") + + class FakeTmux: def __init__(self): self.calls: list[tuple] = []