Replace polling with file, scene-tree, process-handle and bridge events #360

Merged
nelsong6 merged 3 commits from claude/spirelens-polling-patterns-138645 into main 2026-10-18 22:05:24 -07:00
nelsong6 commented 2026-10-18 18:07:59 -07:00 (Migrated from github.com)

Important

Merge this only after romaine-life/spire-lens-mcp#20. The host harness here waits for the bridge's bridge-ready.json, and only the bridge from that PR writes it. If this merges first, env-prep's probe-bridge-ready aborts with bridge_not_ready on every run.

Replaces each polling loop in SpireLens with an event-driven wait.

Commits

  1. Core play-now watcher. ReplayPlayNowWatcher drops its 1 s Godot Timer. It now checks play-now.json when:

    • a FileSystemWatcher reports a change (marshalled to the main thread with CallDeferred, bursts merged into one check);
    • an NMainMenu becomes ready (the scene tree's node_added signal, no Harmony);
    • a replay ends;
    • Core initializes.

    Two narrow timed fallbacks remain where no event exists:

    • a file still open by its writer is retried every 0.25 s, at most 20 times, because Windows has no "file closed" event;
    • a file that doesn't parse gets one 3 s deadline before it is discarded.

    A generation counter drops callbacks queued by an earlier Core after a hot reload.

  2. Run page play status. The page no longer asks GET /api/play every 1.5 s. It opens GET /api/play/events, a server-sent event stream. serve blocks on a Windows directory change notification (FindFirstChangeNotification) for the playlists directory and pushes {"pending": ...} when the request appears, changes or is consumed. The 10 s "not picked up" warning is a single timer, not a check.

  3. Host harness.

    • WaitSts2Ready and ProbeBridge wait on a directory change notification for the bridge's ready file. It is written once NGame.GameStartupComplete finishes and must name a live STS2 pid. They then confirm once over HTTP.
    • StopSts2 opens the processes' handles before taskkill and blocks on WaitForMultipleObjects.
    • In prepare_scenario.py, the "three stable menu reads", the fixed sleep(3.0) and the 0.5 s state loops are replaced by the bridge's wait_for_change. "Settled" now means startup actually finished, rather than a guess at when.

Docs updated: docs/replay-player.md, docs/run-database.md, docs/sts2-runtime-primer.md, docs/glimmung-workflow.md.

Deployed / not verified

  • Core is built, deployed and hot reloaded (Core.Initialize complete, HOT RELOAD DONE, 452 classes patched, 0 failed/skipped). spirelens-db is reinstalled.
  • Core also builds against the checked-in stubs (-p:Sts2DataDir=/nonexistent). go build and go vet pass for Windows and Linux.
  • No tests were run and nothing was checked live. That includes the play-now flow, the SSE stream and a Glimmung run.
> [!IMPORTANT] > **Merge this only after romaine-life/spire-lens-mcp#20.** The host harness here waits for the bridge's `bridge-ready.json`, and only the bridge from that PR writes it. If this merges first, env-prep's `probe-bridge-ready` aborts with `bridge_not_ready` on every run. Replaces each polling loop in SpireLens with an event-driven wait. ## Commits 1. **Core play-now watcher.** `ReplayPlayNowWatcher` drops its 1 s Godot `Timer`. It now checks `play-now.json` when: - a `FileSystemWatcher` reports a change (marshalled to the main thread with `CallDeferred`, bursts merged into one check); - an `NMainMenu` becomes `ready` (the scene tree's `node_added` signal, no Harmony); - a replay ends; - Core initializes. Two narrow timed fallbacks remain where no event exists: - a file still open by its writer is retried every 0.25 s, at most 20 times, because Windows has no "file closed" event; - a file that doesn't parse gets one 3 s deadline before it is discarded. A generation counter drops callbacks queued by an earlier Core after a hot reload. 2. **Run page play status.** The page no longer asks `GET /api/play` every 1.5 s. It opens `GET /api/play/events`, a server-sent event stream. `serve` blocks on a Windows directory change notification (`FindFirstChangeNotification`) for the playlists directory and pushes `{"pending": ...}` when the request appears, changes or is consumed. The 10 s "not picked up" warning is a single timer, not a check. 3. **Host harness.** - `WaitSts2Ready` and `ProbeBridge` wait on a directory change notification for the bridge's ready file. It is written once `NGame.GameStartupComplete` finishes and must name a live STS2 pid. They then confirm once over HTTP. - `StopSts2` opens the processes' handles before `taskkill` and blocks on `WaitForMultipleObjects`. - In `prepare_scenario.py`, the "three stable menu reads", the fixed `sleep(3.0)` and the 0.5 s state loops are replaced by the bridge's `wait_for_change`. "Settled" now means startup actually finished, rather than a guess at when. Docs updated: `docs/replay-player.md`, `docs/run-database.md`, `docs/sts2-runtime-primer.md`, `docs/glimmung-workflow.md`. ## Deployed / not verified - Core is built, deployed and hot reloaded (`Core.Initialize complete`, `HOT RELOAD DONE`, `452 classes patched, 0 failed/skipped`). `spirelens-db` is reinstalled. - Core also builds against the checked-in stubs (`-p:Sts2DataDir=/nonexistent`). `go build` and `go vet` pass for Windows and Linux. - **No tests were run and nothing was checked live.** That includes the play-now flow, the SSE stream and a Glimmung run.
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
romaine-life/spirelens!360
No description provided.