Keep world-state emit reliable across turns

The AI would sometimes stop emitting the `state` delta block once it missed
a turn. Two compounding causes: the emit rule sat only in the system block
(far from where the model generates), and the block was stripped before
storage — so every replayed history turn looked blockless, biasing the model
by imitation to stop emitting too.

- EMIT_REMINDER: a one-line reminder appended last in the prompt (strongest
  recency slot), gated on has_ws and counted against the token budget.
- render_delta_block + _history_text: re-attach each past AI turn's own delta
  block in replayed history (reconstructed from the stored snapshot delta), so
  the model always sees its emit format. Action.text stays clean, so UI,
  embeddings, and card/NPC trigger-matching are unaffected. History budgeting
  counts the augmented text so it can't overflow.

Undo/retry untouched (read the separate world_state_before column). 46 tests.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UWVyFKvqJGjfbXdibLgkMe
This commit is contained in:
parththakkar106
2026-07-25 12:03:41 +05:30
co-authored by Claude Opus 4.8
parent 8fc6d4d2e0
commit 970d71a5b6
4 changed files with 69 additions and 3 deletions
+17
View File
@@ -243,3 +243,20 @@ def test_band_label():
assert w.band_label(d, 10) == "very weak"
assert w.band_label(d, 55) == "minor damage"
assert w.band_label(d, 100) == "full health" # inclusive top edge
def test_render_delta_block_roundtrips_through_extract():
# A rendered block re-injected into history must parse back to the same delta,
# so the model sees valid examples of its own emit format.
delta = {"player.hp": -15, "milestones.escaped": True}
block = w.render_delta_block(delta)
assert block.startswith("```state")
clean, parsed = w.extract_delta(f"You flee into the night.\n{block}")
assert clean == "You flee into the night."
assert parsed == delta
def test_render_delta_block_empty_is_blank():
# No change this turn -> nothing appended to history.
assert w.render_delta_block({}) == ""
assert w.render_delta_block(None) == ""