fix: game service api block when a game is running
This commit is contained in:
+106
-31
@@ -2,10 +2,11 @@ from __future__ import annotations
|
||||
|
||||
import json
|
||||
import sys
|
||||
import time
|
||||
from abc import ABC, abstractmethod
|
||||
from random import Random
|
||||
from typing import IO, Any
|
||||
from urllib.error import URLError
|
||||
from urllib.error import HTTPError, URLError
|
||||
from urllib.request import Request, urlopen
|
||||
|
||||
from texas_holdem.human_io import clear_screen, prompt_action, render_observation
|
||||
@@ -54,6 +55,27 @@ class CallingStationAgent(PokerAgent):
|
||||
return PlayerAction("fold")
|
||||
|
||||
|
||||
def normalise_http_agent_endpoint(raw: str) -> str:
|
||||
"""Return the canonical base URL for an HTTP agent endpoint."""
|
||||
url = raw.rstrip("/")
|
||||
if url.endswith("/act"):
|
||||
url = url[: -len("/act")]
|
||||
if url.endswith("/game"):
|
||||
url = url[: -len("/game")]
|
||||
return url
|
||||
|
||||
|
||||
def http_agent_endpoint_from_spec(spec: dict[str, Any]) -> str | None:
|
||||
"""Extract the canonical HTTP endpoint from an agent spec, if present."""
|
||||
agent_type = str(spec.get("type", "calling")).lower()
|
||||
if agent_type != "http":
|
||||
return None
|
||||
endpoint = spec.get("endpoint")
|
||||
if not endpoint:
|
||||
raise ValueError("http agent requires an endpoint")
|
||||
return normalise_http_agent_endpoint(str(endpoint))
|
||||
|
||||
|
||||
class HttpAgent(PokerAgent):
|
||||
"""Remote agent that talks to a base URL exposing ``/act`` and ``/game``.
|
||||
|
||||
@@ -66,28 +88,36 @@ class HttpAgent(PokerAgent):
|
||||
ACT_PATH = "/act"
|
||||
GAME_PATH = "/game"
|
||||
|
||||
def __init__(self, endpoint: str, timeout_seconds: float = 10.0) -> None:
|
||||
self.base_url = self._normalise_base_url(endpoint)
|
||||
def __init__(
|
||||
self,
|
||||
endpoint: str,
|
||||
timeout_seconds: float = 10.0,
|
||||
player_id: str | None = None,
|
||||
game_update_timeout_seconds: float | None = None,
|
||||
retries: int = 2,
|
||||
retry_backoff_seconds: float = 0.25,
|
||||
) -> None:
|
||||
self.base_url = normalise_http_agent_endpoint(endpoint)
|
||||
self.timeout_seconds = timeout_seconds
|
||||
|
||||
@staticmethod
|
||||
def _normalise_base_url(raw: str) -> str:
|
||||
"""Strip a trailing slash so URL joins do not produce double slashes.
|
||||
|
||||
Centralising this also tolerates the legacy "endpoint already points
|
||||
at /act" mistake by chopping off a redundant ``/act`` suffix.
|
||||
"""
|
||||
url = raw.rstrip("/")
|
||||
if url.endswith("/act"):
|
||||
url = url[: -len("/act")]
|
||||
return url
|
||||
self.player_id = player_id
|
||||
self.game_update_timeout_seconds = (
|
||||
float(game_update_timeout_seconds)
|
||||
if game_update_timeout_seconds is not None
|
||||
else min(timeout_seconds, 3.0)
|
||||
)
|
||||
self.retries = max(0, retries)
|
||||
self.retry_backoff_seconds = max(0.0, retry_backoff_seconds)
|
||||
|
||||
def _url(self, path: str) -> str:
|
||||
"""Compose a full URL by joining the base with a path component."""
|
||||
return f"{self.base_url}{path}"
|
||||
|
||||
def decide(self, observation: Observation) -> PlayerAction:
|
||||
payload = self._post_json(self.ACT_PATH, observation.to_dict())
|
||||
payload = self._post_json(
|
||||
self.ACT_PATH,
|
||||
observation.to_dict(),
|
||||
timeout_seconds=self.timeout_seconds,
|
||||
)
|
||||
if not isinstance(payload, dict):
|
||||
raise RuntimeError("agent endpoint must return a JSON object")
|
||||
return PlayerAction.from_dict(payload)
|
||||
@@ -100,30 +130,54 @@ class HttpAgent(PokerAgent):
|
||||
only by way of the raised exception bubbling to the engine guard.
|
||||
"""
|
||||
try:
|
||||
self._post_json(self.GAME_PATH, game_state)
|
||||
self._post_json(
|
||||
self.GAME_PATH,
|
||||
game_state,
|
||||
timeout_seconds=self.game_update_timeout_seconds,
|
||||
)
|
||||
except RuntimeError:
|
||||
# ``/game`` is informational; treat any HTTP error as a benign
|
||||
# drop rather than reraising and aborting the hand loop.
|
||||
return None
|
||||
|
||||
def _post_json(self, path: str, payload: dict[str, Any]) -> Any:
|
||||
def _post_json(
|
||||
self,
|
||||
path: str,
|
||||
payload: dict[str, Any],
|
||||
timeout_seconds: float,
|
||||
) -> Any:
|
||||
"""POST ``payload`` as JSON to ``base_url + path`` and return parsed body.
|
||||
|
||||
Extracted as a tiny helper so ``decide`` and ``on_game_update`` share
|
||||
identical transport semantics (timeout, error wrapping, content-type).
|
||||
"""
|
||||
body = json.dumps(payload).encode("utf-8")
|
||||
request = Request(
|
||||
self._url(path),
|
||||
data=body,
|
||||
headers={"Content-Type": "application/json"},
|
||||
method="POST",
|
||||
)
|
||||
try:
|
||||
with urlopen(request, timeout=self.timeout_seconds) as response:
|
||||
raw = response.read().decode("utf-8")
|
||||
except (OSError, URLError) as exc:
|
||||
raise RuntimeError(f"agent endpoint failed: {self._url(path)}") from exc
|
||||
last_error: BaseException | None = None
|
||||
raw = ""
|
||||
for attempt in range(self.retries + 1):
|
||||
request = Request(
|
||||
self._url(path),
|
||||
data=body,
|
||||
headers=self._headers(),
|
||||
method="POST",
|
||||
)
|
||||
try:
|
||||
with urlopen(request, timeout=timeout_seconds) as response:
|
||||
raw = response.read().decode("utf-8")
|
||||
break
|
||||
except HTTPError as exc:
|
||||
detail = exc.read().decode("utf-8", errors="replace")
|
||||
last_error = RuntimeError(
|
||||
f"agent endpoint failed with HTTP {exc.code}: "
|
||||
f"{self._url(path)} {detail}"
|
||||
)
|
||||
except (OSError, URLError) as exc:
|
||||
last_error = exc
|
||||
if attempt < self.retries and self.retry_backoff_seconds > 0:
|
||||
time.sleep(self.retry_backoff_seconds * (2**attempt))
|
||||
else:
|
||||
raise RuntimeError(f"agent endpoint failed: {self._url(path)}") from last_error
|
||||
|
||||
if not raw:
|
||||
return None
|
||||
try:
|
||||
@@ -133,6 +187,12 @@ class HttpAgent(PokerAgent):
|
||||
f"agent endpoint returned invalid JSON: {self._url(path)}"
|
||||
) from exc
|
||||
|
||||
def _headers(self) -> dict[str, str]:
|
||||
headers = {"Content-Type": "application/json", "Connection": "close"}
|
||||
if self.player_id:
|
||||
headers["X-Player-Id"] = self.player_id
|
||||
return headers
|
||||
|
||||
|
||||
class HumanAgent(PokerAgent):
|
||||
"""Interactive CLI agent for debugging and manual play.
|
||||
@@ -189,7 +249,11 @@ class HumanAgent(PokerAgent):
|
||||
return line.rstrip("\n")
|
||||
|
||||
|
||||
def build_agent(spec: dict[str, Any], rng: Random | None = None) -> PokerAgent:
|
||||
def build_agent(
|
||||
spec: dict[str, Any],
|
||||
rng: Random | None = None,
|
||||
player_id: str | None = None,
|
||||
) -> PokerAgent:
|
||||
agent_type = str(spec.get("type", "calling")).lower()
|
||||
if agent_type == "random":
|
||||
return RandomAgent(rng)
|
||||
@@ -199,7 +263,18 @@ def build_agent(spec: dict[str, Any], rng: Random | None = None) -> PokerAgent:
|
||||
endpoint = spec.get("endpoint")
|
||||
if not endpoint:
|
||||
raise ValueError("http agent requires an endpoint")
|
||||
return HttpAgent(str(endpoint), float(spec.get("timeout_seconds", 10.0)))
|
||||
return HttpAgent(
|
||||
str(endpoint),
|
||||
timeout_seconds=float(spec.get("timeout_seconds", 10.0)),
|
||||
player_id=player_id,
|
||||
game_update_timeout_seconds=(
|
||||
float(spec["game_update_timeout_seconds"])
|
||||
if "game_update_timeout_seconds" in spec
|
||||
else None
|
||||
),
|
||||
retries=int(spec.get("retries", 2)),
|
||||
retry_backoff_seconds=float(spec.get("retry_backoff_seconds", 0.25)),
|
||||
)
|
||||
if agent_type in {"human", "cli", "interactive"}:
|
||||
return HumanAgent()
|
||||
raise ValueError(f"unknown agent type: {agent_type}")
|
||||
|
||||
Reference in New Issue
Block a user