The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(agent): replay history after a heal whose row recreate failed
kshitijk4poor44cfc5715809
diff --git a/agent/session_persistence.py b/agent/session_persistence.pyindex def61e3d31e..87b6c7ae505 100644--- a/agent/session_persistence.py+++ b/agent/session_persistence.py@@ -350,7 +350,9 @@ def _db_flush_failed(agent, e: Exception, batch_rows: List[Dict[str, Any]], adopagent._parent_session_id = parent_idif not agent._session_db_created:# Row creation failed too (transient store trouble): don't append into a guaranteed- # rollback — keep the batch unmarked so the next flush retries the whole thing.+ # rollback — keep the batch unmarked so the next flush retries the whole thing. That flush+ # recreates the row up front (no FK error, no heal), so it must replay the history prefix itself.+ agent._session_row_replay_pending = agent.session_idlogger.warning("Session DB row for %s is missing and could not be recreated; will retry next flush",getattr(agent, "session_id", None))return None@@ -460,8 +462,10 @@ class SessionPersistenceMixin:try:if not self._session_db_created: # retry row creation if the earlier attempt failed transientlyself._ensure_db_session()- batch_rows, batch_msgs = _db_flush_collect(self, messages, conversation_history, _replay_history)+ replay = _replay_history or getattr(self, "_session_row_replay_pending", None) == self.session_id+ batch_rows, batch_msgs = _db_flush_collect(self, messages, conversation_history, replay)_db_flush_write(self, batch_rows, batch_msgs, messages)+ self._session_row_replay_pending = None# Markers are now the sole truth; reset the one-shot seed so no id() outlives this flush.self._flushed_db_message_ids = set()self._last_flushed_db_idx = len(messages)diff --git a/tests/agent/test_session_row_under_live_agent_persist.py b/tests/agent/test_session_row_under_live_agent_persist.pyindex 35066a8ef00..3b38e3127be 100644--- a/tests/agent/test_session_row_under_live_agent_persist.py+++ b/tests/agent/test_session_row_under_live_agent_persist.py@@ -79,7 +79,8 @@ def test_flush_fails_closed_when_row_cannot_be_recreated(monkeypatch):db = SessionDB(db_path=Path(tmpdir) / "test.db")agent = _make_agent(db, "sess-gone")- agent._flush_messages_to_session_db([{"role": "user", "content": "a"}], [])+ turn1 = [{"role": "user", "content": "a"}]+ agent._flush_messages_to_session_db(turn1, [])assert len(db.get_messages("sess-gone")) == 1assert db.delete_session("sess-gone") is True@@ -89,13 +90,18 @@ def test_flush_fails_closed_when_row_cannot_be_recreated(monkeypatch):monkeypatch.setattr(db, "create_session", _broken_create)- healed = agent._flush_messages_to_session_db(- [{"role": "user", "content": "b"}], []- )+ turn2 = turn1 + [{"role": "user", "content": "b"}]+ healed = agent._flush_messages_to_session_db(turn2, turn1)assert healed is Falseassert agent._session_db_created is Falsemonkeypatch.undo()+ # The next flush recreates the row before any FK error (so no heal runs) and must still+ # replay the history prefix the failed heal left unwritten.+ turn3 = turn2 + [{"role": "assistant", "content": "c"}]+ assert agent._flush_messages_to_session_db(turn3, turn2) is True+ assert [r["content"] for r in db.get_messages("sess-gone")] == ["a", "b", "c"]+# FK failure while the session row still exists (e.g. a sessions-table FK): no heal, and# no replay of the history prefix onto the live transcript.live = _make_agent(db, "sess-fk-live")