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

@ -567,6 +567,37 @@ class SessionStore:
with open(transcript_path, "a") as f:
f.write(json.dumps(message, ensure_ascii=False) + "\n")
def rewrite_transcript(self, session_id: str, messages: List[Dict[str, Any]]) -> None:
"""Replace a session's transcript with the given messages."""
# Rewrite SQLite
if self._db:
try:
self._db._conn.execute(
"DELETE FROM messages WHERE session_id = ?", (session_id,)
)
self._db._conn.execute(
"UPDATE sessions SET message_count = 0, tool_call_count = 0 WHERE id = ?",
(session_id,),
)
self._db._conn.commit()
for msg in messages:
self._db.append_message(
session_id=session_id,
role=msg.get("role", "unknown"),
content=msg.get("content"),
tool_name=msg.get("tool_name"),
tool_calls=msg.get("tool_calls"),
tool_call_id=msg.get("tool_call_id"),
)
except Exception as e:
logger.debug("Session DB rewrite failed: %s", e)
# Rewrite legacy JSONL
transcript_path = self.get_transcript_path(session_id)
with open(transcript_path, "w") as f:
for msg in messages:
f.write(json.dumps(msg, ensure_ascii=False) + "\n")
def load_transcript(self, session_id: str) -> List[Dict[str, Any]]:
"""Load all messages from a session's transcript."""
# Try SQLite first