Closing the Loop: How Clockwork's Built-in AI Agent Uses Kane CLI to Verify the Games It Builds
An AI agent inside a game engine can add objects, attach behaviors and write rules quickly. The agent's own report that it "added coins" doesn't prove the coins work. The proof is a player touching a coin in a real brows
An AI agent inside a game engine can add objects, attach behaviors and write rules quickly. The agent's own report that it "added coins" doesn't prove the coins work. The proof is a player touching a coin in a real browser, the coin disappearing and the score going up by one.
Clockwork is a 2D game engine that runs in the browser. Its built-in agent closes that loop. It edits the project through the same commands the editor uses. It then asks Kane CLI to play the result in a real browser and gets back a typed verdict with the exact values Kane read. This post walks through how that works, from the agent's modes to the Test Mode that makes a real-time game testable, and ends with a recorded run.
Three modes: Ask, Plan and Build
The agent has three modes, defined in shared/protocol.ts. You switch between them at the bottom of the chat.
Ask is read-only. The agent reads the live project and answers questions about it, such as which object is the player, what behaviors it has, and which rule raises the score. In Ask mode the agent never edits the project. It explains what it found and suggests edits you can request in Plan or Build.
Plan drafts a change without applying it. The agent calls preview_commands, and Clockwork shows the change set in the chat and draws a ghost of the changed objects on the canvas. You click Apply or Discard. If the project changes before you decide, the preview is marked stale and can't be applied. Destructive changes, such as deleting objects or scenes, always come back as a preview first.
Build applies the change. Each tool call shows as a card in the chat, so you can see which reads the agent made and what it applied.
The agent reads before it writes
The agent's tools are defined in shared/ai-tools.ts. Before it edits, it reads the live project with get_scene, search_entities, inspect_entity, get_variables, get_game_test_profile and capture_viewport. When it isn't sure of a command's arguments, it calls list_commands, which returns every project command as a one-line signature.
All writes go through apply_commands. One call applies a batch of commands as one atomic transaction: one revision and one undo entry. Every call carries an expectedRevision. If the project has moved on since the agent read it, the batch is rejected rather than applied to a state the agent never saw. Agent edits use the same command registry as the editor, so they appear in the normal undo history. undo_last_agent_change rolls back the agent's last transaction and refuses if you have edited since.
The agent loop lives in src/features/agent/useAgentSession.ts.
Making a real-time game testable
A browser agent can't reliably test a game that runs at 60 frames per second. By the time it reads the score, the player has moved. Clockwork solves this with Test Mode.
In Test Mode the game loads paused and advances only on request. The frame-stepping controller, src/engine/runtime/test-controller.ts, never advances on wall time. The caller asks for an exact number of frames, and the runtime advances exactly that many. Inputs go through the same input path as normal play.
The page shows three panels:
- Status: PAUSED or running, the current FRAME, and the LAST ERROR.
-
Check steps: one button per planned step, labelled with the format from
shared/kane-check.ts:RUN STEP n: <CONTROL> <frames> FRAMES. For example,RUN STEP 1: MOVE RIGHT 45 FRAMES. - Observations: readouts such as PLAYER X, PLAYER Y, COLLECTED, DESTROYED, DAMAGE EVENTS, ACCEPTED INPUTS, every variable and a count per tag.
Each step has a fixed frame count, and the snapshot runs with a fixed seed. A run with the same steps lands on the same frames every time, which makes it reproducible. The readouts are plain page text, so Kane reads them with DOM assertions (--assertion-mode dom) and doesn't have to interpret pixels.
Every game gets a test profile
Kane can only press controls and read values that the game exposes. A game test profile lists them. Thirteen starters have hand-written profiles in src/engine/model/game-test-profiles.ts. Every other game, including Blank projects, imported projects and games the agent builds, gets a profile derived from the project itself (src/engine/model/derived-test-profile.ts). The derived profile relabels the six logical actions as controls (MOVE LEFT, MOVE RIGHT, MOVE UP, MOVE DOWN, JUMP and ACTION). Its readouts come from state the runtime already tracks: status, player position, every variable, tag counts and event counts. Every game therefore has a Test Mode, and the profile only observes the game without changing how it plays.
Two-stage verification
The agent checks a mechanic with verify_with_kane. A check is a title, up to six steps and a list of expectations. For example: hold MOVE RIGHT for 45 frames, then expect score to equal 1 and coin-count to have changed.
Verification runs in two stages:
- Simulator first. Clockwork runs the steps in the engine simulator, using the same inputs through the same runtime, and compares the readouts with the expectations. If an expectation fails here, Kane doesn't run. The agent gets the simulator's values and can adjust the game or the check, for example by adding frames, a jump, or moving the coin.
-
Kane in a real browser. When the simulator agrees, Clockwork publishes a snapshot and Kane CLI runs once against it.
server/kane/runner.tsstartskane-cli run --agentwith--assertion-mode domand reads Kane's NDJSON event stream. Kane clicks each RUN STEP button once, in order, and stores the readouts.
Clockwork then compares Kane's stored values with the expectations itself, in judgeCheck (shared/kane-check.ts). The result is typed, with a status and one outcome per expectation. It streams back into the chat as a verification card through src/features/agent/kane-check-session.ts. A PASSED card means the simulator and the real browser agree on every readout. The agent reads the same typed result, so its reply is based on the values Kane recorded.
Walkthrough: coins in Meadow Sandbox
Meadow Sandbox is a starter built for this kind of experiment. It has the Clockwork Meadows look and feel and no gameplay content: Marlow the player, the parallax meadow, one unbroken floor and three easy ledges. It has no coins, enemies, hazards, checkpoints, goal, variables or HUD, so anything the agent adds is easy to see. Start a fresh Meadow Sandbox project, open the agent in Build mode and send:
Add collectible coins, drawn as simple gold circles, along the player's path, starting along the ground and using the tag coin. When the player touches a coin, it should disappear and the score should increase by one. Display the score in the HUD. After implementing it, verify with Kane that touching a coin removes it and increases the score by one.
Recorded run a20a4b28 asked the agent for the same mechanic: coins tagged coin, +1 score per coin, a SCORE HUD and a Kane check. Here is what the agent did:
-
Build. It applied 6 coins, a
scorevariable and a Score HUD widget throughapply_commands. - Preflight. It ran a headless check of the change.
-
Verify. It called
verify_with_kanewith a check that holds MOVE RIGHT for 45 frames.
The simulator agreed, so Kane ran once in a real browser. The check PASSED at project revision 9, and the simulator and browser agreed on every readout:
| Readout | Before | After |
|---|---|---|
| SCORE | 0 | 1 |
| COIN COUNT | 6 | 5 |
| COLLECTED | 0 | 1 |
| PLAYER X | 240 | 473.67 |
The table shows the whole mechanic. The player moved right, touched one coin, the coin left the scene and the score rose by exactly one.
Reviewed test plans
Some goals need more than one check. Clockwork supports that with a test plan that you review before Kane runs anything.
-
propose_test_plandrafts a bounded plan for the open project. Each phase uses only the controls and readouts in the game's test profile. Clockwork validates the plan against the profile, the project revision and fixed budgets for phases, steps, wall time and frames. Proposing a plan never runs Kane and never edits the project. - You approve it. The plan appears in the AI Testing review panel, and nothing runs until you approve it.
-
run_kane_test_phaseruns exactly one approved phase through Kane in Test Mode. It returns a typed verdict (passed, failed, inconclusive or cancelled) with the actual value for each assertion, the accepted-input count and an evidence bundle. The verdict goes back to the agent, which reports it against your original goal and decides whether another phase is needed.
On Space Shooter, the agent-driven phase b967bbdd passed in 79.7 s. It recorded shots-fired 3, projectiles 3 and 1 accepted input over 45 frames. Two phases approved with the panel's Approve button, 98e21783 and ea1bb3b3, passed in 94.7 s and 78.1 s, each with a typed verdict per criterion and an evidence bundle.
AI Play
Play is for you. AI Play is the second Play button, and it sends Kane in to play the game. The editor saves the project and publishes a snapshot. Kane then plays that snapshot through a visible AI HUD while the panel streams each step.
If the run fails, Clockwork's repair policy looks for a specific content problem: a collectible whose bounds overlap a solid body, where the player can never reach it. The policy moves the collectible above the body with one object.update command, which records one undo entry. Clockwork then publishes a new snapshot and runs Kane again with the same seed.
We tested this with a deliberately broken level: a coin buried inside the ground. Kane caught the broken level and the loop repaired it. The first run (c062a3d4) failed, the repair moved the coin, and the rerun (159ac555) passed. On the Neon Runner demo, the second attempt finished with COINS 3 and STATUS WON. Each repair is a single undo entry, so you can review it and undo it like any other edit.
- Source code: https://github.com/tope-olajide/clockwork
- Demo video: https://youtu.be/ybe9e93r4SU
Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.