The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(sessions): durable-uid dedupe joins the repair transaction — a live rewrite is never dropped
Sharpe42abc9e2ed22
diff --git a/agent/session_persistence.py b/agent/session_persistence.pyindex e35932f66f0..b8a48402d8b 100644--- a/agent/session_persistence.py+++ b/agent/session_persistence.py@@ -308,39 +308,16 @@ def _db_flush_collect(agent, messages: list[dict], conversation_history: Optionabatch_rows: list[dict[str, Any]] = []batch_msgs: list[dict] = []tool_uid_owners: dict = {} # tool_call_uid_from_history memo; the scanned dicts outlive this loop- # Durable-uid dedupe (re-insert guard): a dict rebuilt from durable rows (crash/failed-turn history- # restore, resume, repaired sequence) carries its durable message_uid but neither the persistence- # marker nor _row_id — the identity guards below can't see it, and the flush would INSERT a second- # copy of an already-durable logical message. Probe the candidates once per flush; already-active uid- # = stamp durable and skip (state stays append-only, no rows rewritten).- from agent.message_metadata import message_uid_or_none- candidate_uids = sorted({- uid for msg in messages- if isinstance(msg, dict) and not _is_ephemeral_scaffolding(msg)- and not msg.get(_DB_PERSISTED_MARKER)- and not isinstance(msg.get("_row_id"), int)- and (uid := message_uid_or_none(msg))- })- durable_uids = set()- if candidate_uids and getattr(agent, "_session_db", None):- try:- probe = agent._session_db.active_message_uid_exists(agent.session_id, candidate_uids)- durable_uids = {uid for uid, exists in probe.items() if exists}- except Exception as probe_exc:- # Fail open: a probe failure must never block persistence — the pre-guard behavior.- logger.warning("durable-uid flush probe failed (dedupe skipped, append-only behavior): %s",- probe_exc)+ # Durable-uid re-insert guard lives in resolve_and_repair_transcript_batch (the write transaction):+ # there it shares the repair path's BEGIN IMMEDIATE, sees only the flushed suffix (not the whole+ # transcript), and defers to the CAS rewrite for snapshot-carrying dicts — a live edit on a restored+ # dict still reaches its row. Nothing to probe here.for msg_idx in range(_db_flush_scan_start(agent, messages), len(messages)):msg = messages[msg_idx]# Append-only flush: a mid-turn persist of scaffolding would commit a synthetic turn the end-of-turn# drop cannot un-write. Skip regardless of position.if not isinstance(msg, dict) or _is_ephemeral_scaffolding(msg) or msg.get(_DB_PERSISTED_MARKER):continue- # Durable-uid re-insert guard: this dict carries a uid that is already an ACTIVE durable row- # (rebuilt-from-restore dict without marker/_row_id). Same logical message — stamp and skip.- if message_uid_or_none(msg) in durable_uids and not msg.get(_PERSIST_AFTER_ADMISSION_INTERRUPT):- msg[_DB_PERSISTED_MARKER] = True- continue# Already durable (history copy or caller-seeded): stamp so future flushes skip it.is_history = id(msg) in history_idsif (diff --git a/agent/transcript_repair.py b/agent/transcript_repair.pyindex 3997b10767b..bce62550971 100644--- a/agent/transcript_repair.py+++ b/agent/transcript_repair.py@@ -5,8 +5,9 @@ clone lookup) and sync markers after commit."""from __future__ import annotationsimport hashlib+import loggingimport sqlite3-from typing import Any, Callable, Dict, List, Mapping+from typing import Any, Callable, Dict, List, Mapping, Setfrom agent.context_compressor import _DB_PERSISTED_MARKERfrom agent.message_metadata import (@@ -15,6 +16,8 @@ from hermes_state_common import _id_chunks, _placeholdersfrom hermes_state_identity import _fill_missing_tool_call_uids, _restore_row_identityfrom hermes_state_messages import _MESSAGE_WRITE_COLUMNS+logger = logging.getLogger("agent.transcript_repair")+# Durable payload columns a row-addressed rewrite may change: every INSERT column except row identity,# role, active flag and the ones owned by the display index / timestamp / session linkage. ``message_uid``@@ -107,6 +110,47 @@ def _active_logical_message_row().fetchone()+def _active_uid_dedupe(+ conn: sqlite3.Connection, session_id: str, messages: List[Dict[str, Any]]+) -> Set[str]:+ """ACTIVE durable uids among *messages*' no-snapshot, no-``_row_id`` dicts — the re-insert guard.++ A dict rebuilt from durable rows (crash/failed-turn history restore, session resume, a repaired+ sequence, any wire-crossing handoff) carries its durable ``message_uid`` — identity is restored on+ EVERY projection — but neither the persistence marker (underscore-prefixed, stripped before the+ wire) nor ``_row_id``/``DB_ROW_SNAPSHOT`` (same strip). Dicts that still carry a snapshot are+ excluded: the CAS path below owns them — it rewrites the active row in place, so their live edits+ must reach the row, never be skipped here. Runs INSIDE the caller's write transaction (the same+ guarantee the repair path relies on: the active set cannot change between this read and the+ INSERT). Fail-open: a probe error logs and returns an empty set — pre-guard append behavior.+ """+ candidate_uids = sorted({+ uid+ for msg in messages+ if isinstance(msg, dict)+ and not isinstance(msg.get("_row_id"), int)+ and not isinstance(msg.get(DB_ROW_SNAPSHOT), str)+ and (uid := message_uid_or_none(msg))+ })+ durable: Set[str] = set()+ if not candidate_uids:+ return durable+ try:+ for chunk in _id_chunks(candidate_uids):+ durable.update(+ row["message_uid"]+ for row in conn.execute(+ f"SELECT DISTINCT message_uid FROM messages "+ f"WHERE session_id = ? AND active = 1 AND message_uid IN ({_placeholders(chunk)})",+ (session_id, *chunk),+ ).fetchall()+ )+ except Exception as exc:+ logger.warning("durable-uid re-insert probe failed (dedupe skipped, append-only behavior): %s", exc)+ return set()+ return durable++def resolve_and_repair_transcript_batch(conn: sqlite3.Connection,session_id: str,@@ -122,9 +166,12 @@ def resolve_and_repair_transcript_batch(its physical ``_row_id``, the pair (logical ``message_uid``, stored-row snapshot) recovers the newestactive generation without matching mutable payload. Watermark-compaction clones are matched by theircopied payload identity, not timestamp alone. Legacy blank assistant rows retain the narrow interrupted-- stream content repair. Returns only rows that need fresh inserts.+ stream content repair. A snapshot-less restored dict whose uid is already an ACTIVE durable row is the+ same logical message re-handed to the flush — stamped durable and skipped, never re-INSERTed. Returns+ only rows that need fresh inserts."""inserted_rows: list[dict[str, Any]] = []+ durable_uids = _active_uid_dedupe(conn, session_id, messages)for msg in messages:existing_row_id = msg.get("_row_id") if isinstance(msg, dict) else Nonerole = msg.get("role", "unknown") if isinstance(msg, dict) else "unknown"@@ -137,6 +184,18 @@ def resolve_and_repair_transcript_batch(# has neither proof, even when role/content/timestamp happen to equal an older message exactly.target_row = _active_logical_message_row(conn, session_id, role, message_uid_or_none(msg))if target_row is None:+ from agent.session_persistence import _PERSIST_AFTER_ADMISSION_INTERRUPT+ if (+ isinstance(msg, dict)+ and message_uid_or_none(msg) in durable_uids+ and not msg.get(_PERSIST_AFTER_ADMISSION_INTERRUPT)+ ):+ # Restored logical message, no row address and no CAS version (the wire strip removes+ # every underscore-prefixed field): its uid already names an ACTIVE durable row, so+ # this is the re-insert defect, not a new message. Stamp durable; the row-addressed+ # repair above keeps snapshot-carrying rewrites live.+ msg[_DB_PERSISTED_MARKER] = True+ continueinserted_rows.append(msg)continuediff --git a/tests/agent/test_flush_uid_reinsert_guard.py b/tests/agent/test_flush_uid_reinsert_guard.pyindex 43dbbdd15c3..34c69bcffb5 100644--- a/tests/agent/test_flush_uid_reinsert_guard.py+++ b/tests/agent/test_flush_uid_reinsert_guard.py@@ -12,9 +12,14 @@ later timestamp with display metadata lost — e.g. session 20261007_121708_c6c6held uid 8f651719 as a durable row at 12:37 and again as a fresh insert at 15:18,four hours later, from the same logical assistant message.-The fix consults the durable uid set (SessionDB.active_message_uid_exists) in-``_db_flush_collect``: a candidate whose uid is already an ACTIVE row is stamped-``_DB_PERSISTED_MARKER`` and skipped — append-only state, no rewrites.+The fix consults the durable uid set in ``resolve_and_repair_transcript_batch``+(the append's write transaction): a candidate without a row address or CAS+snapshot whose uid already names an ACTIVE durable row is stamped+``_DB_PERSISTED_MARKER`` and skipped — append-only state, no rewrites. Dicts+that still carry ``DB_ROW_SNAPSHOT`` are excluded (the CAS path rewrites their+row in place — a live edit must reach the row, never be skipped), and the+probe runs inside the ``BEGIN IMMEDIATE`` so the active set cannot change+between the read and the INSERT.This file reproduces the exact signature end-to-end: restore the history from theDB (fresh dicts, uids preserved, no markers), then flush again — the old code@@ -129,21 +134,42 @@ def test_new_messages_after_restore_still_persist(tmp_path):assert len(contents) == 3, f"expected exactly 3 rows, got {len(contents)}: {contents}"-def test_active_message_uid_exists_probe(tmp_path):- """The SessionDB probe: uid -> exists mapping, empty-input, and no-match- behavior."""+def test_restored_snapshot_rewrite_still_lands(tmp_path):+ """The reviewer's blocker (134799 review, 2026-10-07): a restored dict that+ still carries its ``_db_row_snapshot`` is the CAS repair path's client —+ ``_active_logical_message_row`` finds the active row by (session_id, role,+ message_uid) and rewrites it in place. The durable-uid re-insert guard must+ never stamp-and-skip such a dict: a live edit on it would be silently lost.+ """agent, db = _make_agent_with_db(tmp_path)sid = agent.session_id- agent._flush_messages_to_session_db(- [{"role": "user", "content": "probe me", "timestamp": 4000.0}]++ turn_one = [+ {"role": "user", "content": "q", "timestamp": 5000.0},+ {"role": "assistant", "content": "old answer", "timestamp": 5001.0},+ ]+ agent._flush_messages_to_session_db(turn_one)+ assert _row_count(db, sid) == 2++ # restore with repair_alternation=True: fresh dicts stamped snapshot + marker+ restored = db.get_messages_as_conversation(sid, repair_alternation=True)+ assert len(restored) == 2+ assert all(isinstance(m.get("_db_row_snapshot"), str) for m in restored), (+ "repair_alternation=True read-back must stamp the CAS digest for this test to pin the rewrite path"+ )+ # strip ONLY the persistence marker (underscore-prefixed; the wire strip) —+ # the snapshot and uid stay: this dict is still row-addressable by CAS+ for m in restored:+ m.pop(_DB_PERSISTED_MARKER, None)++ # the live edit that must reach the durable row+ restored[-1]["content"] = "new answer"+ agent._flush_messages_to_session_db(restored)++ final = db.get_messages_as_conversation(sid, repair_alternation=True)+ contents = [m.get("content") for m in final]+ assert contents == ["q", "new answer"], (+ "live edit on a snapshot-carrying restored dict was silently dropped: "+ f"final contents {contents}")- live_uid = db._read_one(- "SELECT message_uid FROM messages WHERE session_id = ? LIMIT 1", (sid,)- )["message_uid"]-- probe = db.active_message_uid_exists(sid, [live_uid, "no-such-uid"])- assert probe == {live_uid: True, "no-such-uid": False}- assert db.active_message_uid_exists(sid, []) == {}- assert db.active_message_uid_exists("no-such-session", [live_uid]) == {- live_uid: False- }\ No newline at end of file+ assert _row_count(db, sid) == 2, "the rewrite must not add rows"\ No newline at end of file