The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(state): bound the rewind-tail walk at the watermark and rewind concurrent-tail originals too
Tekniumf57802c7ad68
diff --git a/hermes_state.py b/hermes_state.pyindex 341e6dca91f..f57e0937596 100644--- a/hermes_state.py+++ b/hermes_state.py@@ -12053,28 +12053,46 @@ class SessionDB(SessionSearchMixin, SessionSchemaMixin, SessionPortabilityMixin)# Rewind-target ids: the originals of the carried-forward tail# rows (tail_count), captured BEFORE any flag flips. Named apart# from the watermark `tail_ids` below on purpose — the two are- # different sets (#86366).+ # different sets (#86366): rewind targets sit AT/BELOW the+ # watermark (the compressor only saw rows up to it), while+ # `tail_ids` are concurrent appends ABOVE it. Without the bound,+ # a concurrent append would steal a LIMIT slot and leave a real+ # carried-forward original stamped compacted=1.rewind_tail_ids: Optional[list[int]] = Noneif tail_count > 0:- tail_rows = conn.execute(- "SELECT id FROM messages "- "WHERE session_id = ? AND active = 1 ORDER BY id DESC LIMIT ?",- (session_id, int(tail_count)),- ).fetchall()+ if watermark is not None:+ tail_rows = conn.execute(+ "SELECT id FROM messages "+ "WHERE session_id = ? AND active = 1 AND id <= ? "+ "ORDER BY id DESC LIMIT ?",+ (session_id, int(watermark), int(tail_count)),+ ).fetchall()+ else:+ tail_rows = conn.execute(+ "SELECT id FROM messages "+ "WHERE session_id = ? AND active = 1 ORDER BY id DESC LIMIT ?",+ (session_id, int(tail_count)),+ ).fetchall()rewind_tail_ids = [int(row["id"]) for row in tail_rows]- if rewind_tail_ids:- placeholders = ",".join("?" for _ in rewind_tail_ids)+ # The watermark clone below re-inserts `tail_ids` rows byte-exact+ # as live rows — their originals are the SAME superseded-duplicate+ # class as the carried-forward tail (#86366), so they take the+ # rewind flags too instead of double-matching the recall filter.+ rewind_ids = [*(rewind_tail_ids or []), *tail_ids]++ if rewind_ids:+ placeholders = ",".join("?" for _ in rewind_ids)conn.execute("UPDATE messages SET active = 0, compacted = 0 "f"WHERE session_id = ? AND id IN ({placeholders})",- [session_id, *rewind_tail_ids],+ [session_id, *rewind_ids],)conn.execute("UPDATE messages SET active = 0, compacted = 1 ""WHERE session_id = ? AND active = 1 "f"AND id NOT IN ({placeholders})",- [session_id, *rewind_tail_ids],+ [session_id, *rewind_ids],)else:conn.execute(diff --git a/tests/hermes_state/test_86366_watermark_interplay.py b/tests/hermes_state/test_86366_watermark_interplay.pynew file mode 100644index 00000000000..2844fde9921--- /dev/null+++ b/tests/hermes_state/test_86366_watermark_interplay.py@@ -0,0 +1,159 @@+"""Regression tests for #86366 × watermark interplay in archive_and_compact.++Two sibling sites of the superseded-duplicate class:++* carried-forward tail originals (tail_count) — the compressor's protected+ tail rides inside compacted_messages verbatim; originals must take rewind+ flags (active=0, compacted=0), not compacted=1.+* concurrent-tail originals (watermark clone, #75316) — rows appended during+ the summary call are re-inserted byte-exact as live clones; their originals+ are the SAME superseded-duplicate class and must take rewind flags too.++And the interaction bound: with both watermark and tail_count set, the+rewind-target LIMIT walk must not consume concurrent-append rows (above the+watermark) as if they were carried-forward tail.+"""++from __future__ import annotations++import sqlite3+from pathlib import Path++import pytest++from hermes_state import SessionDB+++def db(tmp_path: Path):+ handle = SessionDB(db_path=tmp_path / "state.db")+ try:+ yield handle+ finally:+ handle.close()+++def _flags(db: SessionDB, session_id: str):+ conn = sqlite3.connect(db.db_path)+ conn.row_factory = sqlite3.Row+ try:+ return [+ dict(r)+ for r in conn.execute(+ "SELECT id, active, compacted, content FROM messages "+ "WHERE session_id = ? ORDER BY id",+ (session_id,),+ ).fetchall()+ ]+ finally:+ conn.close()+++class TestWatermarkTailCountInterplay:+ def test_rewind_walk_bounded_at_watermark(self, db: SessionDB) -> None:+ """A concurrent append above the watermark must not steal a rewind+ LIMIT slot from a genuine carried-forward tail original."""+ sid = "s-wm-bound"+ db.create_session(sid, source="cli")+ for i in range(4):+ db.append_message(sid, "user", content=f"seen-{i}")+ watermark = db.get_active_message_watermark(sid)+ # Concurrent append AFTER the compressor snapshotted its input.+ db.append_message(sid, "user", content="concurrent-append")++ db.archive_and_compact(+ sid,+ [+ {"role": "user", "content": "SUMMARY"},+ {"role": "user", "content": "seen-2"},+ {"role": "user", "content": "seen-3"},+ ],+ watermark=watermark,+ tail_count=2,+ )++ rows = _flags(db, sid)+ by_content = {}+ for r in rows:+ by_content.setdefault(r["content"], []).append(r)++ # Carried-forward tail originals: rewind flags (hidden from recall).+ for content in ("seen-2", "seen-3"):+ originals = [+ r for r in by_content[content] if r["active"] == 0+ ]+ assert originals, content+ assert all(r["compacted"] == 0 for r in originals), (+ f"{content} original stamped compacted=1 — recall duplicate"+ )+ live = [r for r in by_content[content] if r["active"] == 1]+ assert len(live) == 1++ # Summarized-away rows keep discoverability.+ for content in ("seen-0", "seen-1"):+ (row,) = by_content[content]+ assert (row["active"], row["compacted"]) == (0, 1)++ def test_concurrent_tail_original_takes_rewind_flags(+ self, db: SessionDB+ ) -> None:+ """The watermark clone's original is a superseded byte-identical+ duplicate — it must not satisfy recall next to its live clone."""+ sid = "s-wm-clone"+ db.create_session(sid, source="cli")+ for i in range(3):+ db.append_message(sid, "user", content=f"old-{i}")+ watermark = db.get_active_message_watermark(sid)+ db.append_message(sid, "user", content="mid-flight zqx-token")++ db.archive_and_compact(+ sid,+ [{"role": "user", "content": "SUMMARY"}],+ watermark=watermark,+ )++ rows = _flags(db, sid)+ copies = [r for r in rows if "zqx-token" in r["content"]]+ assert len(copies) == 2 # archived original + live clone+ original = next(r for r in copies if r["active"] == 0)+ clone = next(r for r in copies if r["active"] == 1)+ assert original["compacted"] == 0, (+ "concurrent-tail original stamped compacted=1 — it would be "+ "recalled alongside its live clone (same class as #86366)"+ )+ assert clone["compacted"] == 0++ # Recall filter surfaces exactly ONE copy.+ hits = [+ r+ for r in db.search_messages("zqx-token")+ if r.get("session_id") == sid+ ]+ assert len(hits) == 1++ def test_recall_stable_across_generations(self, db: SessionDB) -> None:+ """search_messages hit count for a carried-forward message must not+ grow with compaction generations."""+ sid = "s-gen"+ db.create_session(sid, source="cli")+ for i in range(5):+ db.append_message(sid, "user", content=f"turn-{i} gentok-{i}")++ payload = [+ {"role": "user", "content": "SUMMARY"},+ {"role": "user", "content": "turn-3 gentok-3"},+ {"role": "user", "content": "turn-4 gentok-4"},+ ]+ counts = []+ for _ in range(3):+ db.archive_and_compact(sid, list(payload), tail_count=2)+ counts.append(+ len(+ [+ r+ for r in db.search_messages("gentok-3")+ if r.get("session_id") == sid+ ]+ )+ )+ assert counts == [1, 1, 1], counts