Skip to content

Headless MCP and test runner that play the real core in Node - #101

Open
kafuji wants to merge 11 commits into
mainfrom
claude/headless-mcp
Open

kafuji wants to merge 11 commits into
mainfrom
claude/headless-mcp

Conversation

@kafuji

@kafuji kafuji commented Nov 26, 2026 •

Copy link
Copy Markdown
Owner

What

A headless way to play Polypiled for automated play and tests: an MCP server and a test client. Each game runs the ordinary LocalNetHackRuntime and the WASM core in a Node worker thread. Observations come from the core's structured output and the displayed map, not from the UI. The old browser bridge is removed.

MCP tools (node scripts/mcp/headless-server.mjs):

Tool What it does
new_game Start a game by seed, character, playmode and options, or from a snapshot. Up to 8 games run at once.
list_games List the running games and their setups.
observe Turn, typed prompt, hero, level, status, unread messages, named monsters, what is here, and the map.
act One action, then wait until the game waits for input again. Actions: move, keys, extended, answer, select, text, position, inventory operation, dismiss. Returns turns, messages, the next prompt and status changes.
travel Walk to x, y.
inventory Object ids, letters, known blessed/cursed state, enchantment, charges, worn items, and what is on the floor.
snapshot Save the setup, every action so far and a fingerprint, optionally to a JSON file. new_game replays it into an identical copy.
reveal (debug mode) The true level: terrain with secret doors and corridors, all monsters, objects and traps, plus luck, alignment, prayer timeout, god anger and nutrition.
debug (debug mode) Wish, create a monster, teleport, level-teleport, map the level.
end_game Stop a game and free it.

scripts/headless/game-client.mjs exposes the same operations to Node tests. Design notes are in docs/mcp-headless.md.

Core changes (built and shipped)

  • sys_random_seed() takes globalThis.nh3dHeadless.seed as a weak seed, so reseed_random() keeps games reproducible.
  • parse() queues shim_command_ready (turn, hero position). The shell can then tell a command prompt from any other key the game reads.
  • getpos() is wrapped to queue shim_getpos at start and end, with the cursor's square.
  • The #nh3dinventory request reveal (debug mode only) publishes shim_reveal. It reads level data without the random number generators and takes no time.
  • public/nethack-5.* come from Build game core (WASM) run 45813280145. That run also checked that saves from the previous core load in this one. The glyph catalog is regenerated.

The app ignores the new events. The only runtime additions are three event translations, one menu accessor and the reveal inventory operation.

Removed

  • scripts/mcp/server.mjs and its smoke client and test
  • src/automation
  • The title-screen MCP control and its app controller
  • The engine's getAutomationContext / submitAutomationAction and their observers
  • The worker bridge's automation claims

Notes for review

  • Determinism: the seed goes to the core, and the clock is frozen on an ordinary day (it affects the moon, Friday the 13th and night). The same setup and the same actions give the same game (tested).
  • Snapshots are action logs replayed on the same setup, checked by a fingerprint of what the hero sees (turn, prompt, hero, level, status, map). This is not a save file, so a snapshot made with a different core is refused rather than loaded wrongly.
  • One game per thread: two cores in one thread trip Asyncify's reentrancy check, because they share the shim callback. Each game gets its own worker, which is terminated at the end rather than resumed through the browser shutdown path.
  • Position actions move the cursor from where getpos() last placed it (map_cursor). They do not use @, because farlook treats @ as picking the spot. Autodescribe messages are left out while the cursor moves.
  • Debug mode forces the name wizard, because the injected sysconf allows only that name.
  • Type check: src/headless is checked by tsconfig.headless.json with Node types. check:tsc runs both configs.
  • Dependencies: esbuild and @types/node are used as installed today, through other packages. They are not added as explicit devDependencies.

Testing

  • npm run test:headless (13 tests against the real core, about 4 s):
    • seed replay
    • command and key prompts
    • single and multi-select menus with counts, and inventory operations by object id
    • y/n prompts, farlook and travel
    • snapshot replay, and refusal of a snapshot that does not match
    • debug reveal, wish, create monster, teleport, level teleport and map
    • a death (#quit) through to game_over and the tombstone
    • the MCP server over stdio: several games and snapshot files
  • A stress run: 8 seeded games, up to 1500 random actions each. None of the actions timed out, and both deaths ended cleanly.
  • npx vitest run: 2103 passed, 4 skipped. This includes AgentSession unit tests for the core marks, getpos cursor keys and inventory operations.
  • npm run check:tsc passes.

🤖 Generated with Claude Code

kafuji and others added 6 commits November 26, 2026 20:07
The old MCP bridge polls a browser tab and rebuilds state from the UI.
For automated play and tests, run the ordinary runtime and WASM core in
Node worker threads instead, and observe through the core's structured
output (knowledge frames, inventory JSON, glances) and the displayed map.

- src/headless: Node asset loading, a frozen clock so a seed replays the
  same game, one game per thread, and AgentSession, which turns runtime
  events into typed prompts and acts until the game waits for input again.
- scripts/headless/game-client.mjs bundles the worker with esbuild and
  starts each game in its own worker thread.
- scripts/mcp/headless-server.mjs exposes new_game, observe, act,
  inventory and end_game over stdio.
- LocalNetHackRuntime.isMultiSelectMenuActive() tells pickup-style menus
  apart.
- src/headless is type-checked with Node types in tsconfig.headless.json.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
For headless play and tests:

- sys_random_seed() takes globalThis.nh3dHeadless.seed when the shell sets
  it, as a weak seed so reseed_random() leaves the game reproducible.
- parse() queues "shim_command_ready" (turn, hero x, y) before reading a
  command, so the shell can tell a command prompt from any other key the
  game reads and knows the turn even when the map did not change.
- The #nh3dinventory request "reveal" publishes "shim_reveal" in debug
  mode only: true terrain (secret doors and corridors included), every
  monster, floor object and trap, and luck, alignment, prayer timeout,
  god anger and nutrition. It reads level data directly, without the
  random number generators, and takes no time.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A key the game reads while choosing a position moves the cursor or picks,
but the window port cannot tell such a key request from any other. The
shell needs to know to show a position prompt (teleport, travel, farlook,
targeting) and where the cursor starts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
public/nethack-5.js and public/nethack-5.wasm come from the game-core-wasm
artifact of Build game core (WASM) run 45813280145, which also checked that
saves from the previous core load in this one. The glyph catalog is
regenerated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- The runtime turns the core's shim_command_ready, shim_getpos and
  shim_reveal notifications into command_ready, getpos and reveal events.
  The session uses them to tell a command prompt from a key read inside a
  command ("key"), and to show position prompts with the cursor that
  getpos() last placed.
- Seeds go to the core (0 to 4294967295); the clock stays frozen on an
  ordinary day for the moon and the calendar.
- New actions: position (move the cursor with hjkl/HJKL and pick),
  inventory (whole item actions by letter or object id through
  #nh3dinventory), and reveal. Cursor descriptions are left out of the
  messages while a position action moves the cursor.
- HeadlessGame records every action. A snapshot is the setup, the actions
  and a fingerprint of what the hero sees; starting from one replays it
  and refuses a replay that does not match.
- Debug mode helpers: reveal, wish, create a monster, teleport, level
  teleport, map; and travel in any mode.
- The MCP server runs up to eight games by id, saves and loads snapshots
  as JSON files inside the working directory, and adds list_games,
  travel, snapshot, reveal and debug.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The headless server replaces it for automated play and tests. Removed:
scripts/mcp/server.mjs and its smoke client and test, src/automation, the
title-screen MCP control and its app controller, the engine's automation
surface (getAutomationContext, submitAutomationAction and the observers
behind them) and the worker bridge's automation claims.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kafuji kafuji changed the title Add a headless MCP server that plays the real core in Node Headless MCP and test runner that play the real core in Node Nov 26, 2026
kafuji and others added 3 commits November 26, 2026 21:10
Both sides changed the core. The C sources merge cleanly; the shipped
public/nethack-5.* and glyph catalog are main's for now and are replaced
by a build of the merged sources in the next commit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
public/nethack-5.js and public/nethack-5.wasm come from the game-core-wasm
artifact of Build game core (WASM) run 45817268849, built from main's core
changes and this branch's together; that run also checked that saves from
the previous core load. The glyph catalog is regenerated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
After the last death question the runtime emits game_over_complete and the
core asks for nothing more, so a session waiting for the next input request
timed out and showed no prompt. The session now treats it as the end:
prompt game_over, gameOver.complete with the death message and tombstone,
and further actions fail.

Found by playing eight seeded games for up to 1500 random actions each;
with this, none timed out and both deaths ended cleanly.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
kafuji and others added 2 commits November 27, 2026 06:35
Both sides changed the core again. The C sources merge cleanly; public/
nethack-5.* and the glyph catalog are main's until the merged sources are
built. The floor panel comment is main's, which moved the panel.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
public/nethack-5.js and public/nethack-5.wasm come from the game-core-wasm
artifact of Build game core (WASM) run 45899191432, built from main's core
changes and this branch's together; that run also checked that saves from
the previous core load. The glyph catalog is regenerated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant