fix: group chat history shared across users — bot no longer loses context

History was keyed by (chat_id, user_id). In group chats, when Jordan
asked about Solomon buyback and Cory followed up, the bot couldn't see
Jordan's exchange. Now maintains chat-level history (chat_id, 0) that
captures all exchanges with usernames. Group context visible to all
follow-up responses.

Pentagon-Agent: Epimetheus <3D35839A-7722-4740-B93D-51157F7D5E70>
This commit is contained in:
m3taversal 2026-03-24 14:51:03 +00:00
parent d33ddd9f3d
commit 60c92d5c19

View file

@ -268,9 +268,20 @@ def _compress_history(history: list[dict]) -> str:
def _format_conversation_history(chat_id: int, user_id: int) -> str:
"""Format conversation history with compressed context summary (Ganymede: Option C+A)."""
key = (chat_id, user_id)
history = conversation_history.get(key, [])
"""Format conversation history with compressed context summary (Ganymede: Option C+A).
In group chats, merges user-specific history with chat-level history
so the bot sees exchanges from other users in the same chat.
"""
user_key = (chat_id, user_id)
chat_key = (chat_id, 0) # chat-level history (all users)
# Merge: chat-level history gives full group context
chat_history = conversation_history.get(chat_key, [])
user_history = conversation_history.get(user_key, [])
# Use chat-level if available (group chats), otherwise user-level (DMs)
history = chat_history if chat_history else user_history
if not history:
return "(No prior conversation)"
@ -280,8 +291,9 @@ def _format_conversation_history(chat_id: int, user_id: int) -> str:
# Full exchange log for reference
for exchange in history:
who = exchange.get("username", "User")
if exchange.get("user"):
lines.append(f"User: {exchange['user']}")
lines.append(f"@{who}: {exchange['user']}")
if exchange.get("bot"):
lines.append(f"Rio: {exchange['bot']}")
lines.append("")
@ -675,12 +687,21 @@ IMPORTANT: Two special tags you can append at the end of your response (after yo
# Update conversation state: reset window, store history (Ganymede+Rhea)
if user:
username = user.username or "anonymous"
key = (msg.chat_id, user.id)
unanswered_count[key] = 0 # reset — conversation alive
entry = {"user": text[:500], "bot": response[:500], "username": username}
# Per-user history
history = conversation_history.setdefault(key, [])
history.append({"user": text[:500], "bot": response[:500]})
history.append(entry)
if len(history) > MAX_HISTORY:
history.pop(0)
# Chat-level history (group context — all users visible)
chat_key = (msg.chat_id, 0)
chat_history = conversation_history.setdefault(chat_key, [])
chat_history.append(entry)
if len(chat_history) > MAX_HISTORY:
chat_history.pop(0)
# Record rate limit
if user: