Skip to content

The agent that grows with you

chore(state): name the weak claim at both rewind sites; hoist the per-message import; replay through substitute_api_content

Teknium2d8d7e251952
diff --git a/agent/transcript_repair.py b/agent/transcript_repair.py
index 80a2f888fff..9334555a810 100644
--- a/agent/transcript_repair.py
+++ b/agent/transcript_repair.py
@@ -203,6 +203,9 @@ def resolve_and_repair_transcript_batch(
same payload is that message re-handed to the flush — stamped durable and skipped, never re-INSERTed. Returns
only rows that need fresh inserts.
"""
+ # Late import: agent.session_persistence imports this module at load time.
+ from agent.session_persistence import _PERSIST_AFTER_ADMISSION_INTERRUPT
+
inserted_rows: list[dict[str, Any]] = []
durable_uids = _active_uid_dedupe(conn, session_id, messages)
for msg in messages:
@@ -217,7 +220,6 @@ 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 (stored := durable_uids.get(message_uid_or_none(msg) or ""))
diff --git a/hermes_state_messages.py b/hermes_state_messages.py
index 2543495c628..b014f2503e5 100644
--- a/hermes_state_messages.py
+++ b/hermes_state_messages.py
@@ -873,7 +873,7 @@ class SessionMessagesMixin(SessionDetachedResultsMixin, SessionTodoResultMixin):
rewind_ids = list(carried_ids)
if tail_below > 0:
rewind_ids += self._tail_originals(covered_active, tail_below, merged_away)
- # A row claimed by identity alone is archived as compacted history, never rewound out of display.
+ # A weak claim (a rewritten carry naming the row) is archived as compacted history, never rewound out of display.
rewind_ids += [row_id for row_id in unseen if row_id not in weak_claims]
rewind_ids = list(dict.fromkeys(rewind_ids))
if rewind_ids:
@@ -1008,7 +1008,7 @@ class SessionMessagesMixin(SessionDetachedResultsMixin, SessionTodoResultMixin):
"ORDER BY id DESC LIMIT ?",
(session_id, *((int(watermark),) if bound else ()),
int(tail_below) + self._uncounted_merged_rows(compacted_messages[-int(tail_count):]))).fetchall()]
- # A row claimed by identity alone is archived as compacted history, never rewound out of display.
+ # A weak claim (a rewritten carry naming the row) is archived as compacted history, never rewound out of display.
rewind_ids += [row_id for row_id in tail_ids if row_id not in weak_claims]
rewind_ids = list(dict.fromkeys(rewind_ids))
if rewind_ids:
diff --git a/tests/hermes_state/test_compaction_carried_tail_not_cloned.py b/tests/hermes_state/test_compaction_carried_tail_not_cloned.py
index 6db9d04e438..62f0b057cbf 100644
--- a/tests/hermes_state/test_compaction_carried_tail_not_cloned.py
+++ b/tests/hermes_state/test_compaction_carried_tail_not_cloned.py
@@ -207,8 +207,15 @@ def test_a_rewritten_carry_claims_only_what_the_compressor_rewrote(db: SessionDB
def _replayed(db: SessionDB) -> list:
- """What the next provider request replays per live row: the ``api_content`` sidecar, else the content."""
- return [m.get("api_content") or m["content"] for m in db.get_messages_as_conversation(SID)]
+ """The content the next provider request carries per live row, through the turn's own sidecar substitution."""
+ from agent.turn_context import substitute_api_content
+
+ replayed = []
+ for message in db.get_messages_as_conversation(SID):
+ api_message = dict(message)
+ substitute_api_content(api_message)
+ replayed.append(api_message["content"])
+ return replayed
def test_matching_text_never_claims_an_independent_append(db: SessionDB) -> None: