The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(state): a compaction's pruned tool stub no longer paints beside its archived original
Tekniume1601d408a6f
diff --git a/hermes_state_compaction_display.py b/hermes_state_compaction_display.pynew file mode 100644index 00000000000..06163bdafea--- /dev/null+++ b/hermes_state_compaction_display.py@@ -0,0 +1,43 @@+"""Display visibility of the rewritten copies an in-place compaction inserts.++In-place compaction archives the summarized rows (``active=0, compacted=1``). They stay+display-visible on purpose, so the transcript keeps the full history. The compressor also carries+some messages forward in a rewritten form: a tool result pruned or demoted to a one-line stub keeps+its ``message_uid`` and timestamp but not its body. That copy's display key differs from the archived+original's, so both painted and the user saw the same tool result twice (once full, once as the+stub). The stub exists for the model's context only, so it is written ``model_only`` when its full+original stays visible. The archived body is the one the transcript shows.+"""++from __future__ import annotations++from typing import Any++from agent.context_compressor import MODEL_ONLY_DISPLAY_METADATA_KEY+from hermes_state_common import _sql_json_extract++_ORIGINAL_VISIBLE_SQL = f"COALESCE({_sql_json_extract('o.display_metadata', '$.' + MODEL_ONLY_DISPLAY_METADATA_KEY)}, 0) = 0"+++def _next_message_id(conn: Any) -> int:+ """The id the next inserted row receives (rows are never deleted below MAX(id) inside the commit)."""+ return int(conn.execute("SELECT COALESCE(MAX(id), 0) + 1 FROM messages").fetchone()[0])+++def hide_rewritten_tool_copies(db: Any, conn: Any, session_id: str, first_new_id: int) -> None:+ """Mark the tool rows inserted at or after *first_new_id* model-only when an archived row with the+ same ``message_uid`` but a different body is still display-visible. A copy whose original was+ rewound (``compacted=0``) stays visible, because it is then the only copy the transcript has."""+ rows = conn.execute(+ "SELECT c.id, c.display_metadata FROM messages c WHERE c.session_id = ? AND c.id >= ? "+ "AND c.active = 1 AND c.role = 'tool' AND c.message_uid IS NOT NULL AND EXISTS ("+ "SELECT 1 FROM messages o WHERE o.session_id = c.session_id AND o.message_uid = c.message_uid "+ f"AND o.id < c.id AND o.active = 0 AND o.compacted = 1 AND o.content IS NOT c.content AND {_ORIGINAL_VISIBLE_SQL})",+ (session_id, int(first_new_id))).fetchall()+ updates = []+ for row in rows:+ meta = db._decode_display_metadata(row["display_metadata"]) or {}+ if not meta.get(MODEL_ONLY_DISPLAY_METADATA_KEY):+ updates.append((db._encode_display_metadata({**meta, MODEL_ONLY_DISPLAY_METADATA_KEY: True}), row["id"]))+ if updates:+ conn.executemany("UPDATE messages SET display_metadata = ? WHERE id = ?", updates)diff --git a/hermes_state_messages.py b/hermes_state_messages.pyindex 614e3262dd7..2c58be86e6f 100644--- a/hermes_state_messages.py+++ b/hermes_state_messages.py@@ -22,6 +22,7 @@ from hermes_cli.timefmt import coerce_epochfrom hermes_state_common import (_COMPRESSION_LOCK_ROW_SQL, _ENDED_ROW_SQL, _RESET_END_REASONS, _RESET_END_REASONS_SQL, _ended_by_compression,_json_or, _legacy_reset_child_sql, _placeholders, _sql_json_extract)+from hermes_state_compaction_display import _next_message_id, hide_rewritten_tool_copiesfrom hermes_state_detached_results import SessionDetachedResultsMixinfrom hermes_state_todo import SessionTodoResultMixinfrom hermes_state_identity import (@@ -881,7 +882,9 @@ class SessionMessagesMixin(SessionDetachedResultsMixin, SessionTodoResultMixin):# durable row's timestamp so display dedupe still collapses generations. Donors are active-only;# the rewound/archived originals above never donate, so this is a no-op for a clean compaction.self._carry_parent_timestamps(conn, session_id, compacted_messages)+ first_new_id = _next_message_id(conn)inserted, tool_calls_total = self._insert_message_rows(conn, session_id, compacted_messages)+ hide_rewritten_tool_copies(self, conn, session_id, first_new_id)if unseen:_ids, unseen_tool_calls = self._tail_rows_after_watermark(conn,@@ -1004,7 +1007,9 @@ class SessionMessagesMixin(SessionDetachedResultsMixin, SessionTodoResultMixin):# durable row's timestamp so display dedupe still collapses generations. Donors are active-only;# the rewound/archived originals above never donate, so this is a no-op for a clean compaction.self._carry_parent_timestamps(conn, session_id, compacted_messages)+ first_new_id = _next_message_id(conn)inserted, tool_calls_total = self._insert_message_rows(conn, session_id, compacted_messages)+ hide_rewritten_tool_copies(self, conn, session_id, first_new_id)if tail_ids:self._clone_message_rows(conn, tail_ids)inserted += len(tail_ids)diff --git a/tests/hermes_state/test_compaction_rewritten_tool_copy_display.py b/tests/hermes_state/test_compaction_rewritten_tool_copy_display.pynew file mode 100644index 00000000000..f9387299109--- /dev/null+++ b/tests/hermes_state/test_compaction_rewritten_tool_copy_display.py@@ -0,0 +1,56 @@+"""In-place compaction carries some tool results forward as pruned stubs (same ``message_uid``, short+body). When the full original is archived it stays display-visible, so the stub must not paint as a+second copy of the same tool result (#132939, #135655). When the original was rewound instead (the+verbatim carried tail), the stub is the only copy and stays visible."""+import json++import pytest++from hermes_state import SessionDB+++def db(tmp_path):+ instance = SessionDB(tmp_path / "state.db")+ yield instance+ instance.close()+++def _seed(db, sid):+ db.create_session(sid, source="test")+ db.append_messages_batch(sid, [+ {"role": "user", "content": "read it", "timestamp": 100.0},+ {"role": "assistant", "content": "", "timestamp": 101.0,+ "tool_calls": [{"id": "call-1", "type": "function", "function": {"name": "read_file", "arguments": "{}"}}]},+ {"role": "tool", "content": "FULL " * 1000, "tool_call_id": "call-1", "tool_name": "read_file",+ "timestamp": 102.0},+ {"role": "assistant", "content": "done reading", "timestamp": 103.0},+ ])+ history = db.get_messages_as_conversation(sid, include_row_ids=True)+ history[2]["content"] = "[read_file] pruned stub"+ return history+++def _painted_tool_bodies(db, sid):+ _model, display = db.get_resume_conversations(sid)+ paged = db.get_messages(sid, include_compacted=True)+ return ([m.get("content") for m in display if m["role"] == "tool"],+ [m.get("content") for m in paged if m["role"] == "tool"])+++def test_pruned_copy_of_an_archived_tool_result_paints_once(db):+ sid = "archived-original"+ history = _seed(db, sid)+ db.archive_and_compact(sid, [{"role": "user", "content": "summary", "timestamp": 150.0}, *history])+ for bodies in _painted_tool_bodies(db, sid):+ assert bodies == ["FULL " * 1000]+ model_tool = [m for m in db.get_messages_as_conversation(sid) if m["role"] == "tool"]+ assert [m["content"] for m in model_tool] == ["[read_file] pruned stub"]+++def test_pruned_copy_of_a_rewound_tail_original_stays_visible(db):+ sid = "rewound-original"+ history = _seed(db, sid)+ db.archive_and_compact(sid, history, tail_count=len(history))+ for bodies in _painted_tool_bodies(db, sid):+ assert bodies == ["[read_file] pruned stub"], json.dumps(bodies)[:200]