Replace polling with game-state events, a startup ready file and process-handle waits #20

Merged
nelsong6 merged 2 commits from claude/event-driven-waits into main 2026-10-18 22:05:11 -07:00
nelsong6 commented 2026-10-18 18:07:41 -07:00 (Migrated from github.com)

Removes the polling that bridge clients depend on. The bridge now tells them when something happened.

What changes

  • Game-state waits. A new POST {"action":"wait_for_change","since_version":N,"timeout_ms":M} holds the request until the state version passes N and the game has settled, then returns the state. "Settled" means no game action running or queued, no turn hand-off, no screen transition, no run load in progress. A pending player choice also counts as settled. The version goes up on run, room, act, combat, action-queue, action-executor, overlay-stack, map-screen and hand-mode events. As a catch-all for UI without a model event (event pages, reward buttons), it also goes up on the scene tree's node_added/node_removed. Every get_game_state response now carries state_version, settled and unsettled_reason; markdown responses put them on the last line.
  • Startup readiness. GET / now also reports ready, pid, state_version and ready_file. ready turns true once NGame.GameStartupComplete finishes: after the logo, the main menu load and the startup error checks. At that point the bridge writes %APPDATA%\SlayTheSpire2\SpireLensMcpBridge\bridge-ready.json (pid, port, version) and deletes any stale copy when it starts. "status": "ok" keeps its old meaning: the listener is up.
  • MCP server (mcp/server.py):
    • New tool wait_for_game_state_change.
    • _wait_for_state is now built on wait_for_change.
    • start_sts2 waits on a directory change notification for the ready file instead of probing every 3 s.
    • stop_sts2 waits on the game's process handles instead of checking every 1 s.
    • bridge_health adds a bridge_starting status.
  • replace_deck with persist now answers once the save has been written, instead of telling the caller to keep calling validate_current_run_save. The HTTP thread waits on the save task; the main thread never blocks.
  • AGENTS.md drops the "call get_game_state twice" advice and points agents to wait_for_game_state_change.

Compatibility

This is additive: old clients see the same fields plus new ones. The romaine-life/spirelens PR that switches the host harness to the ready file has to merge after this one. Glimmung's env-prep rebuilds the bridge from this repo's main, so once this is merged every run uses the new bridge.

Not verified

  • No tests were run, and nothing was tried live. The running game still has the old bridge DLL loaded, because a bridge mod can't hot reload, so the new code runs only after STS2 restarts.
  • Built locally with dotnet build -c Release. server.py passes py_compile.
  • The "settled" rules come from reading the decompiled ActionExecutor / ActionQueueSet / CombatManager code. If a state never settles, wait_for_change returns "status": "timeout" with unsettled_reason rather than hanging.
Removes the polling that bridge clients depend on. The bridge now tells them when something happened. ## What changes - **Game-state waits.** A new `POST {"action":"wait_for_change","since_version":N,"timeout_ms":M}` holds the request until the state version passes N *and* the game has settled, then returns the state. "Settled" means no game action running or queued, no turn hand-off, no screen transition, no run load in progress. A pending player choice also counts as settled. The version goes up on run, room, act, combat, action-queue, action-executor, overlay-stack, map-screen and hand-mode events. As a catch-all for UI without a model event (event pages, reward buttons), it also goes up on the scene tree's `node_added`/`node_removed`. Every `get_game_state` response now carries `state_version`, `settled` and `unsettled_reason`; markdown responses put them on the last line. - **Startup readiness.** `GET /` now also reports `ready`, `pid`, `state_version` and `ready_file`. `ready` turns true once `NGame.GameStartupComplete` finishes: after the logo, the main menu load and the startup error checks. At that point the bridge writes `%APPDATA%\SlayTheSpire2\SpireLensMcpBridge\bridge-ready.json` (pid, port, version) and deletes any stale copy when it starts. `"status": "ok"` keeps its old meaning: the listener is up. - **MCP server (`mcp/server.py`):** - New tool `wait_for_game_state_change`. - `_wait_for_state` is now built on `wait_for_change`. - `start_sts2` waits on a directory change notification for the ready file instead of probing every 3 s. - `stop_sts2` waits on the game's process handles instead of checking every 1 s. - `bridge_health` adds a `bridge_starting` status. - **`replace_deck` with `persist`** now answers once the save has been written, instead of telling the caller to keep calling `validate_current_run_save`. The HTTP thread waits on the save task; the main thread never blocks. - **`AGENTS.md`** drops the "call `get_game_state` twice" advice and points agents to `wait_for_game_state_change`. ## Compatibility This is additive: old clients see the same fields plus new ones. The romaine-life/spirelens PR that switches the host harness to the ready file has to merge **after** this one. Glimmung's env-prep rebuilds the bridge from this repo's `main`, so once this is merged every run uses the new bridge. ## Not verified - No tests were run, and nothing was tried live. The running game still has the old bridge DLL loaded, because a bridge mod can't hot reload, so the new code runs only after STS2 restarts. - Built locally with `dotnet build -c Release`. `server.py` passes `py_compile`. - The "settled" rules come from reading the decompiled `ActionExecutor` / `ActionQueueSet` / `CombatManager` code. If a state never settles, `wait_for_change` returns `"status": "timeout"` with `unsettled_reason` rather than hanging.
Sign in to join this conversation.
No description provided.