SPEC-022: Strategy analysis¶
- Status: Implemented
- Milestone: v0.14 "Behaviour and strategy"
- Owners: lead maintainer
1. Purpose¶
Show how well an agent plays, move by move, not only the final result: how often it picks the best move, how much each mistake costs, where its blunders are, how it opens, and whether it learns over a series of games. A world that can judge its own positions provides an analyst, and AgentPlay does the rest from the logs.
2. Terms¶
Keywords MUST, SHOULD and MAY follow RFC 2119.
- Analyst: a pure function, shipped with a world, that values each legal action in a given
observation.
- Move value: the analyst's value of an action, from the acting agent's point of view.
- Regret: the best move value minus the chosen move's value (0 for a best move).
- Blunder: a move whose regret is at least the world's blunder_threshold.
- Exact and approximate analysts: an exact analyst gives values with perfect play (a solved
game such as tic-tac-toe). An approximate analyst searches a fixed number of moves ahead and
estimates the rest (chess).
3. Requirements¶
Analysts¶
- STR-001 A world MAY provide an analyst: a class with
values(observation, legal_actions) -> {action: value}, deterministic and without side effects, named by the world'sanalystattribute or registered under the entry-point groupagentplaykit.analystswith the world's name and version (<name>@<version>). Because logs do not store the list of legal actions, an analyst MUST also providelegal(observation), the legal actions the observation shows. It MAY providecells(observation, action), the grid cells a move touches, for the viewer (STR-011). The move an agent played is its accepted action, as the world read it (info.normalised.read_aswhen present). - STR-002 Analysts MUST be used only by evaluators reading logs, never during a run, and MUST NOT receive the running world (EVL-003): they get the logged observation and legal actions.
- STR-003 A world with an analyst MUST state in
describe(), asanalysis, itsblunder_threshold, what a value means (values, for example "1 win, 0 draw, -1 loss with perfect play"), and whether the analyst isexactorapproximate. It MAY state abest_margin(default 0): a move whose regret is at most the margin counts as a best move. The run log'srun_start.worldMUST carryanalysis, so reports can label results. - STR-004 Tic-tac-toe MUST ship an analyst using minimax values, with a blunder being a move that turns a won or drawn position into a worse result with perfect play.
Chess¶
- STR-013 Chess (SPEC-024) MUST ship an approximate analyst, written in AgentPlay with the
chess world's own rules engine, so it needs no other package and no chess program
(ADR-0013). It reads the logged
fenandlegal_moves. For each legal move it MUST: - search a fixed number of half-moves ahead,
depth(default 2: the move and the reply); - then follow captures and promotions only, up to 4 in a row, so a piece left to be taken is seen;
- value the position reached by material (pawn 1, knight 3, bishop 3, rook 5, queen 9) plus small bonuses for central and advanced pieces, at most 0.5 a side;
- value checkmate as ±100 less the half-moves needed to reach it, and stalemate, insufficient material and the fifty-move rule as 0.
Values are in pawns, from the moving player's point of view, rounded to 0.1.
- STR-014 The chess analyst MUST be deterministic: the same position and depth always give
the same values on any machine. It MUST limit its work by depth and the capture limit, never
by time. Moves MUST be searched in a fixed order. Repetition MUST be judged only from what the
position shows, because the observation does not hold the whole game.
- STR-015 Chess's describe() MUST state blunder_threshold 3 (a minor piece or more),
best_margin 0.3, and that the analyst is approximate, with its depth. Reports and the
terminal MUST mark chess strategy results as approximate and give the depth, for example
"approximate, depth 2". The strategy evaluator MUST accept depth (1 to 4) for chess. The
chess guide MUST explain what the analyst can and cannot see: it finds hanging pieces and
mate in one, but not deep plans.
The strategy evaluator¶
- STR-005 The framework MUST ship an evaluator
strategythat, for every move in a log of a world with an analyst, computes the move value, the best value and the regret, and returns per agent: accuracy (share of best moves), mean regret, blunders per move, and each blunder with itsseq, the move played and the best moves. - STR-006 The evaluator MUST cache analyst results by observation, and MUST skip (and say so)
moves whose analysis takes longer than
max_ms_per_move(default 2000).
Across games¶
- STR-007 Reports MUST show each competitor's opening: the distribution of its first move per world and role, with counts.
- STR-008 Reports MUST show learning for series of games between the same competitors
(tournaments,
--runs, experiments): the result in the first third of games against the last third, in game order, with intervals, and say "no clear change" when the intervals overlap. - STR-009 A world MAY declare the best response to an opponent with a known weakness (a scripted bot that favours one move); reports MUST then show how often the agent played it, by game third, as a measure of exploiting the weakness.
Reports and viewer¶
- STR-010 Reports MUST add a strategy section per world with an analyst: accuracy, mean regret and blunder rate per competitor with 95% intervals, and its five worst blunders linked to their replay turns.
- STR-011 For grid views (ENV-019), the viewer MUST mark blunders on the turn list and, on a blunder's turn, highlight the best moves on the board.
- STR-012 The docs MUST show how to write an analyst for a custom world, with the
guess-the-number example (
examples/guess) as the worked example: the best guess splits the numbers still possible in half.
4. Acceptance criteria¶
| Requirement | Test ID | Notes |
|---|---|---|
| STR-001..004 | STR-001..STR-004 |
analyst lookup; purity check; tic-tac-toe values against known positions |
| STR-005..006 | STR-005, STR-006 |
minimax scores accuracy 1 and regret 0; random play gives known blunders; cache and timeout |
| STR-007..009 | STR-007..STR-009 |
openings; a scripted agent that improves after game 5; exploiting a biased rock-paper-scissors bot |
| STR-010..012 | STR-010..STR-012 |
report section; viewer marks in a browser test; docs example runs |
| STR-013..015 | STR-013..STR-015 |
finds mate in one and a hanging queen; values for set positions; same values twice; no time limit; within max_ms_per_move at depth 2 on busy positions; describe() fields; "approximate" label |
5. Examples¶
class TicTacToeAnalyst:
def values(self, observation, legal_actions):
board, me = observation["board"], observation["you"]
return {a: minimax_value(play(board, a, me), me) for a in legal_actions}
strategy: tic-tac-toe (60 runs) accuracy mean regret blunders/move
minimax 1.00 0.00 0.00
qwen2.5-3b 0.71 0.18 0.09
llama3.2-3b 0.58 0.31 0.16
The chess analyst after 1. e4 e5 2. Bc4 Nc6 3. Qf3, with black to move (depth 2):
Nf6 0.0 best: blocks the mate on f7
Qe7 -0.1 best: within best_margin 0.3
a6 -98.0 blunder: Qxf7# follows (mate two half-moves away)
strategy: chess (20 runs, approximate, depth 2) accuracy mean regret blunders/move
qwen2.5-3b 0.44 2.10 0.12
llama3.2-3b 0.39 2.65 0.15
6. Open questions¶
- A scripted chess player built on the same search, as a stronger opponent than
scripted/random(an open question in SPEC-024). It would be an agent, not an analyst, so STR-002 still holds. - Deeper chess analysis, for example depth 4 run once and stored next to the log.
- Strategy summaries in words, produced by a judge from the analyst's numbers.
7. Change log¶
- 2026-12-01: Initial draft.
- 2026-12-02: Moved to v0.14, after the chess world (SPEC-024) in v0.13.
- 2026-12-03: Added the chess analyst (STR-013 to STR-015),
best_marginand exact or approximate analysts (STR-003), at the lead maintainer's request. - 2026-12-03: Accepted by the lead maintainer. On building: STR-001 adds
legal(observation)and the optionalcells, because logs do not store the legal actions; STR-003 puts the fields underanalysisand in the log; STR-012 uses the guess example, because the treasure hunt's observation does not say where the treasure is, so no analyst can judge a move from it. - 2026-12-03: Implemented in v0.14.0; every requirement has a test.
Changes to other specs on acceptance¶
- SPEC-001, new ENV-0xx: the optional
analyst,blunder_thresholdand best responses. - SPEC-006: the entry-point group
agentplaykit.analysts. - SPEC-010, SPEC-012 and SPEC-013: the
strategyevaluator, viewer marks and report sections. - SPEC-024: chess's
describe()gainsblunder_threshold,best_marginand the analyst (STR-015).