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:
co-authored by
Claude Opus 4.8
parent
8fc6d4d2e0
commit
970d71a5b6
@@ -2,6 +2,7 @@
|
||||
module validates and clamps them against a scenario's stat_schema."""
|
||||
|
||||
from .engine import (
|
||||
EMIT_REMINDER,
|
||||
EMIT_RULE,
|
||||
apply_delta,
|
||||
apply_override,
|
||||
@@ -11,11 +12,13 @@ from .engine import (
|
||||
instantiate,
|
||||
npc_name,
|
||||
npc_triggers,
|
||||
render_delta_block,
|
||||
render_reference,
|
||||
render_state_section,
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
"EMIT_REMINDER",
|
||||
"EMIT_RULE",
|
||||
"apply_delta",
|
||||
"apply_override",
|
||||
@@ -25,6 +28,7 @@ __all__ = [
|
||||
"instantiate",
|
||||
"npc_name",
|
||||
"npc_triggers",
|
||||
"render_delta_block",
|
||||
"render_reference",
|
||||
"render_state_section",
|
||||
]
|
||||
|
||||
@@ -45,6 +45,22 @@ EMIT_RULE = (
|
||||
'"player.outfit": "torn traveling cloak"}\n```'
|
||||
)
|
||||
|
||||
# Short terminal reminder placed at the very end of the prompt (strongest
|
||||
# recency position) so the emit rule is fresh right where the model generates.
|
||||
EMIT_REMINDER = (
|
||||
"[Reminder: end your reply with a ```state block of the changes this turn "
|
||||
"(deltas only), or omit it if truly nothing changed.]"
|
||||
)
|
||||
|
||||
|
||||
def render_delta_block(delta: dict) -> str:
|
||||
"""Render a stored delta back into the fenced `state` block the AI emitted,
|
||||
for re-injecting past turns into context so the model imitates the format.
|
||||
Empty delta -> empty string (the turn legitimately changed nothing)."""
|
||||
if not isinstance(delta, dict) or not delta:
|
||||
return ""
|
||||
return "```state\n" + json.dumps(delta, ensure_ascii=False) + "\n```"
|
||||
|
||||
# ```state { ... } ``` (also tolerates ```json or an unlabelled fence); DOTALL.
|
||||
_FENCE_RE = re.compile(r"```(?:state|json)?\s*(\{.*?\})\s*```", re.DOTALL | re.IGNORECASE)
|
||||
# Fallback: a bare JSON object hugging the end of the text.
|
||||
|
||||
Reference in New Issue
Block a user