Skip to content

Search

src/retrospeedlab/search/ finds the fastest button sequence from power-on to the end of a game, proves that it replays and relays the run to the stream. Nothing in it is specific to one game: each game's games/speedrun/<slug>.py supplies the RAM map, the states, the rewards and the actions.

Level by level

The run is split at the transitions of the game's state machine (menu, each stage, the ending). Each level is searched on its own, by workers that each drive their own console process:

Role What it does
Scouts Go-Explore, frontier search and Monte Carlo tree search toward the level's exit
Backtrackers Search again from earlier checkpoints of the route, where a faster line may start
Beam scouts Beam search over the action table, scored by the game's progress measure
Vision scouts Optional: ask a vision model about the screen when the RAM alone does not help
Polishers Shorten segments that already reach the exit

They are guided by RAM novelty, a learned move table, a route map of where the hero has been, the lab's hints and, when enabled, the PPO policy. Explored cells are kept in archives on disk, so a new turn picks up where the last one stopped.

Verified from power-on

  • After every new segment the whole movie replays from power-on; only a run that replays is kept.
  • A stored record is replayed again at every start.
  • A new minor or patch version of the cartridge replays every stored piece and cuts the route at the first one that breaks. A new major version starts the game fresh.

The PPO policy

When speedrun.policy is on, a Stable-Baselines3 PPO agent trains on the GPU beside the search. Its Gymnasium environment, PolicyEnv in search/policy/env.py, starts from the level the search is stuck on, observes the RAM and picks from the game's action table. It is rewarded for new RAM states and for leaving the level, and loses for dying. Exits it finds go back to the search, and its weights guide the scouts. Run your own AI shows it in use.

Events

The search publishes to Kafka when events.kafka is set:

Topic Content
runs.attempts Attempts and their outcome
runs.steps Search progress per level
runs.records New verified records
runs.turn_minutes What each worker kind did each minute of a turn
policy.rounds PPO training rounds and their metrics
lab.events Lab sessions starting, judging and finishing

The lab

When a game stalls, the runner starts a Claude Code session on the game's search package. The lab leaves what it learned in two files next to the configuration:

  • hints.json: button presses that get past a hard spot. The search refuses to start without it; write [] when there are none.
  • knowledge.yaml: mechanics, the player, enemies, power-ups, levels and RAM addresses, shown on the website. A missing file is empty knowledge; a malformed one stops the run.

Every key of knowledge.yaml is required, and sprite names one of the cartridge's SPRITES or is null:

mechanics:
  - title: Thrust
    text: 'Holding A burns fuel, and an empty tank drops the courier.'
player:
  name: Courier
  text: The comet courier.
  sprite: courier
enemies:
  - name: Drone
    text: Flies at the courier.
    sprite: drone
    levels: [1, 2]
powerups:
  - name: Fuel
    text: Fills the tank.
    sprite: null
    effect: More fuel
levels:
  - name: Level 1
    goal: Deliver the parcel
    route: Over the ridge
    hazards: [rocks]
ram:
  - address: 0x0010
    name: fuel
    meaning: Fuel left
    verified: true

The lab never edits them in place: python -m retrospeedlab.search.lab.save <draft> <file> loads a draft with the search's own schema and refuses what the search could not load.

Configuration

config.example.yaml at the repository root lists every key, and every key is required. The search keeps its state under paths.cache.

Key Meaning
paths.collection The games collection
training.game The slug to search
events.kafka Kafka bootstrap servers, or null for no events
ui.enable Relay the live run to the backend (needs UPLOAD_API_KEY)
video.encoder libx264 or h264_nvenc
speedrun.scouts … speedrun.polishers Workers of each role
speedrun.policy Train the PPO policy beside the search
speedrun.segment_seconds Search time of one round on a segment, doubled each round

Commands

uv run python -m retrospeedlab.runner search comet_courier "$PWD/config.yaml"
uv run python -m retrospeedlab.search.speedrun.run comet_courier "$PWD/config.yaml" working/cache/tas/levels.msgpack

The first searches one game until stopped; the second checks that every stored part of a route still replays.