fix(gateway): persist transcript changes in /retry, /undo and fix /reset

/retry and /undo set session_entry.conversation_history which does not
exist on SessionEntry. The truncated history was never written to disk,
so the next message reload picked up the full unmodified transcript.

Added SessionStore.rewrite_transcript() that persists changes to both
the JSONL file and SQLite database, and updated both commands to use it.

/reset accessed self.session_store._sessions which does not exist on
SessionStore (the correct attribute is _entries). Also replaced the
hand-coded session key with _generate_session_key() to fix WhatsApp DM
sessions using the wrong key format.

Closes #210
This commit is contained in:
Farukest 2026-03-01 01:12:58 +03:00
parent 6366177118
commit b7f8a17c24
No known key found for this signature in database
GPG key ID: 73E2756B3FFF5241
3 changed files with 95 additions and 5 deletions

View file

@ -901,13 +901,12 @@ class GatewayRunner:
source = event.source
# Get existing session key
session_key = f"agent:main:{source.platform.value}:" + \
(f"dm" if source.chat_type == "dm" else f"{source.chat_type}:{source.chat_id}")
session_key = self.session_store._generate_session_key(source)
# Memory flush before reset: load the old transcript and let a
# temporary agent save memories before the session is wiped.
try:
old_entry = self.session_store._sessions.get(session_key)
old_entry = self.session_store._entries.get(session_key)
if old_entry:
old_history = self.session_store.load_transcript(old_entry.session_id)
if old_history:
@ -1135,7 +1134,7 @@ class GatewayRunner:
# Truncate history to before the last user message
truncated = history[:last_user_idx]
session_entry.conversation_history = truncated
self.session_store.rewrite_transcript(session_entry.session_id, truncated)
# Re-send by creating a fake text event with the old message
retry_event = MessageEvent(
@ -1167,7 +1166,7 @@ class GatewayRunner:
removed_msg = history[last_user_idx].get("content", "")
removed_count = len(history) - last_user_idx
session_entry.conversation_history = history[:last_user_idx]
self.session_store.rewrite_transcript(session_entry.session_id, history[:last_user_idx])
preview = removed_msg[:40] + "..." if len(removed_msg) > 40 else removed_msg
return f"↩️ Undid {removed_count} message(s).\nRemoved: \"{preview}\""