fix(poker): guarantee hand logging — history was conditioning it away
Logging a stated hand was unreliable and got worse mid-session: same model, same
hand, clean history logged 4/4 but the real session's history logged 0/4. Root
cause: memory.recent() rebuilt past turns as "hand -> narration" with the tool
calls stripped (they live in tool_events), so the model's own context became
few-shot examples training it, mid-conversation, to STOP calling tools. Even a
maximal "LOG FIRST, no exceptions" prompt scored 0/5 — it's structural, not wording.
Two-part fix (both, per the system-of-record frame):
- A (guarantee): chat._ensure_hand_logged — on a HAND turn that's Brian's OWN hand,
if the model didn't log it, force record_hand (tool_choice). Guarded to hero hands
(looks_like_hero_hand) so an observed hand is never force-logged as his. Adds
tool_choice passthrough to llm.chat_call; surfaces msg_type on TurnContext.
- B (heal forward): mind._history_with_tools makes each assistant turn's tool calls
visible in reconstructed history ("record_hand -> Hand #62"), so the demonstrated
pattern stops being "hand -> narrate". Recovers natural logging as logs accumulate.
Verified: force guard returns record_hand on the polluted context; full respond_stream
logs Hand #63 end-to-end on a clean session. 7 guard tests; suite 219 green.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
+47
-2
@@ -10,7 +10,7 @@ deliberate) and hands back a ready message list + the active mode. Then:
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from lyra import config, llm, logbus, memory, mind, modes, summary
|
||||
from lyra import config, llm, logbus, memory, mind, modes, poker_prompts, summary
|
||||
from lyra import tools as toolkit
|
||||
from lyra.llm import Backend
|
||||
|
||||
@@ -78,6 +78,43 @@ def _mind_loop(messages, backend: Backend, model: str | None, tool_specs,
|
||||
return reply, tools_run
|
||||
|
||||
|
||||
_FORCE_LOG = (
|
||||
"You have not logged Brian's hand yet — and a hand must ALWAYS be recorded, no exceptions. "
|
||||
"Call record_hand now: pass his ENTIRE hand description as one `shorthand` string."
|
||||
)
|
||||
|
||||
|
||||
def _ensure_hand_logged(messages, user_msg: str, msg_type: str | None, tools_run: list,
|
||||
backend: Backend, model: str | None, ctx: dict, session_id: str) -> list:
|
||||
"""Guarantee the ledger. If this turn was Brian's OWN hand and the model didn't log it,
|
||||
force the record_hand call — the log can't be left to the model's discretion, because
|
||||
mid-session the history few-shot-conditions it to skip logging (see mind._history_with_tools;
|
||||
even a maximal 'LOG FIRST' prompt scored 0/5 under a polluted history). Guarded to hero
|
||||
hands so an observed hand is never force-logged as his. Returns forced tool names."""
|
||||
if msg_type != "HAND" or backend not in config.load().tool_backends:
|
||||
return []
|
||||
if any(t in ("record_hand", "log_hand") for t in tools_run):
|
||||
return []
|
||||
if not poker_prompts.looks_like_hero_hand(user_msg):
|
||||
return []
|
||||
try:
|
||||
_, tcs = llm.chat_call(
|
||||
messages + [{"role": "system", "content": _FORCE_LOG}],
|
||||
backend=backend, model=model, tools=toolkit.specs(["record_hand"]),
|
||||
tool_choice={"type": "function", "function": {"name": "record_hand"}},
|
||||
)
|
||||
except Exception as exc:
|
||||
logbus.log("error", "forced hand-log failed", session=session_id, error=str(exc)[:160])
|
||||
return []
|
||||
forced = []
|
||||
for tc in (tcs or []):
|
||||
result = toolkit.dispatch(tc["name"], tc["arguments"], ctx)
|
||||
memory.add_tool_event(session_id, tc["name"], tc["arguments"], result)
|
||||
logbus.log("info", "forced hand log", session=session_id, tool=tc["name"], result=result[:80])
|
||||
forced.append(tc["name"])
|
||||
return forced
|
||||
|
||||
|
||||
def _voice_pass(messages, draft: str, backend: Backend, model: str | None) -> str:
|
||||
"""Mouth: re-render the mind's draft in her voice. Falls back to the draft on failure."""
|
||||
try:
|
||||
@@ -104,7 +141,8 @@ def respond(session_id: str, user_msg: str, backend: Backend = "cloud",
|
||||
# Persist the user turn before the tool loop so its timestamp precedes any
|
||||
# tool events fired mid-turn (keeps the transcript export in true order).
|
||||
memory.remember(session_id, "user", user_msg)
|
||||
reply, _ = _mind_loop(messages, backend, model, tool_specs, ctx, session_id)
|
||||
reply, tools_run = _mind_loop(messages, backend, model, tool_specs, ctx, session_id)
|
||||
_ensure_hand_logged(messages, user_msg, turn.msg_type, tools_run, backend, model, ctx, session_id)
|
||||
mouth = _mouth_target(cfg, backend, model)
|
||||
if mouth and reply:
|
||||
reply = _voice_pass(messages, reply, *mouth)
|
||||
@@ -139,6 +177,7 @@ def respond_stream(session_id: str, user_msg: str, backend: Backend = "cloud",
|
||||
if mouth is None:
|
||||
# No separate voice: stream the mind directly (the original path, unchanged).
|
||||
parts: list[str] = []
|
||||
tools_run: list[str] = []
|
||||
for _ in range(MAX_TOOL_ROUNDS):
|
||||
assistant_msg = None
|
||||
tool_calls = None
|
||||
@@ -161,7 +200,11 @@ def respond_stream(session_id: str, user_msg: str, backend: Backend = "cloud",
|
||||
logbus.log("info", "tool call", session=session_id, tool=tc["name"], result=result[:80])
|
||||
messages.append({"role": "tool", "tool_call_id": tc["id"], "content": result})
|
||||
_maybe_switch_mode(session_id, tc["name"])
|
||||
tools_run.append(tc["name"])
|
||||
yield ("tool", tc["name"])
|
||||
for name in _ensure_hand_logged(messages, user_msg, turn.msg_type, tools_run,
|
||||
backend, model, ctx, session_id):
|
||||
yield ("tool", name)
|
||||
reply = "".join(parts)
|
||||
if not reply:
|
||||
reply = _TANGLED
|
||||
@@ -169,6 +212,8 @@ def respond_stream(session_id: str, user_msg: str, backend: Backend = "cloud",
|
||||
else:
|
||||
# Mind decides + runs tools (non-streamed); mouth re-voices, streamed.
|
||||
draft, tools_run = _mind_loop(messages, backend, model, tool_specs, ctx, session_id)
|
||||
tools_run += _ensure_hand_logged(messages, user_msg, turn.msg_type, tools_run,
|
||||
backend, model, ctx, session_id)
|
||||
for name in tools_run:
|
||||
yield ("tool", name)
|
||||
parts = []
|
||||
|
||||
Reference in New Issue
Block a user