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
This commit is contained in:
ai-chess
2026-05-14 23:34:03 +03:00
parent de850a9a05
commit 33d6af764a
4 changed files with 31 additions and 6 deletions
+5
View File
@@ -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 -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" 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
echo "[ai-chess] open: http://127.0.0.1:7833/" echo "[ai-chess] open: http://127.0.0.1:7833/"
echo "[ai-chess] attach: tmux attach -t chess" echo "[ai-chess] attach: tmux attach -t chess"
echo "[ai-chess] first prompt fires in ~12s (after agents boot)"
echo echo
tmux attach -t chess tmux attach -t chess
+11 -3
View File
@@ -120,11 +120,19 @@ def build_app(state: state_mod.AppState) -> FastAPI:
@app.post("/api/new-game") @app.post("/api/new-game")
async def new_game_post() -> dict[str, Any]: async def new_game_post() -> dict[str, Any]:
await start_new_game(state) 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] 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}") @app.get("/api/agent-binding/{side}")
async def agent_binding(side: str) -> dict[str, Any]: async def agent_binding(side: str) -> dict[str, Any]:
s = Side.from_str(side) s = Side.from_str(side)
+7 -2
View File
@@ -4,7 +4,7 @@ import asyncio
import random import random
from typing import Any from typing import Any
from ulid import ULID import ulid
from ai_chess.chess_game import ChessGame from ai_chess.chess_game import ChessGame
from ai_chess.persistence import save_game 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.bindings = make_bindings(side_to_agent)
state.first_move_sent = {Side.WHITE: False, Side.BLACK: False} state.first_move_sent = {Side.WHITE: False, Side.BLACK: False}
state.nudge_count = {Side.WHITE: 0, Side.BLACK: 0} state.nudge_count = {Side.WHITE: 0, Side.BLACK: 0}
gid = str(ULID()) gid = ulid.new().str
state.game = ChessGame( state.game = ChessGame(
game_id=gid, game_id=gid,
agents={s.value: b.to_dict() for s, b in state.bindings.items()}, 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] return self.pane_for[side.value]
async def _submit(self, side: Side, text: str) -> None: async def _submit(self, side: Side, text: str) -> None:
# 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) 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, async def _send_first(self, side: Side, *, opponent_san: str | None,
opponent_comment: str | None) -> None: opponent_comment: str | None) -> None:
+7
View File
@@ -2,12 +2,19 @@ import asyncio
import pytest import pytest
import ai_chess.coordinator as coord_mod
from ai_chess.chess_game import ChessGame from ai_chess.chess_game import ChessGame
from ai_chess.coordinator import Coordinator from ai_chess.coordinator import Coordinator
from ai_chess.sides import Side from ai_chess.sides import Side
from ai_chess.state import AppState, AgentBinding 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: class FakeTmux:
def __init__(self): def __init__(self):
self.calls: list[tuple] = [] self.calls: list[tuple] = []