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
+11 -3
View File
@@ -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)
+8 -3
View File
@@ -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: