fix(reply): done-by-user guard keeps tool calls, other people's lines and started tools safe

- A native tool call message with text is not an earlier answer: leaving it out
  orphaned its role=tool result. Only plain assistant answers are left out.
- The guard is off once any tool run started in the reply, whatever it returned:
  an MCP call with an empty result was not in invoked_tools_history, so a second
  try could run it again.
- HistoryContext.answers marks the lines that are Jarvis's answers to the asker
  (to_id is the asker); only those leave the history, not someone else saying the
  same words.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
javis-bot
2026-11-18 20:55:29 +09:00
parent 4d83543acf
commit 3e4974f3bd
9 changed files with 99 additions and 41 deletions

View File

@@ -45,23 +45,21 @@ def _yes_no(cfg, system: str, user_content: str, what: str, timeout_sec: float)
return result
def _line_text(line: str) -> str:
"""The text of a history line ``[date time] name: text``."""
head, sep, rest = line.partition("] ")
if not sep or not head.startswith("["):
return line
return rest.partition(": ")[2] or rest
def _is_answer(m: dict) -> bool:
"""A plain answer of Jarvis: not a native tool call message (its tool results answer it)."""
return (m.get("role") == "assistant" and not m.get("tool_calls") and isinstance(m.get("content"), str)
and bool(m["content"].strip()))
def earlier_answers(recent_messages: Iterable[dict], history: Any) -> list[str]:
"""Jarvis's earlier answers in the short-term dialogue memory, and the history lines (recent and
matches) with the same text, as given, so a second try can leave exactly these out."""
answers = [m["content"] for m in recent_messages or []
if m.get("role") == "assistant" and isinstance(m.get("content"), str) and m["content"].strip()]
said = {a.strip() for a in answers}
lines = [x for x in [*(getattr(history, "recent", None) or []), *(getattr(history, "matches", None) or [])]
if isinstance(x, str) and _line_text(x).strip() in said]
return answers + lines if answers else []
"""Jarvis's earlier plain answers in the short-term dialogue memory, and its answer lines in the
history context (``HistoryContext.answers``, the lines that are its answers to the asker), as
given, so a second try can leave exactly these out. Empty without an answer in the memory."""
answers = [m["content"] for m in recent_messages or [] if _is_answer(m)]
if not answers:
return []
shown = {*(getattr(history, "recent", None) or []), *(getattr(history, "matches", None) or [])}
return answers + [x for x in getattr(history, "answers", None) or [] if x in shown]
def superseded(cfg, text: str, reply: str, timeout_sec: float = 5.0) -> bool: