Skip to content

The agent that grows with you

fix(api): scope ancestor reads to display surfaces; guard after_id; stamp read-path fixture

Yingliang Zhangcfd5dc387633
diff --git a/gateway/platforms/api_server.py b/gateway/platforms/api_server.py
index daf0bd1bfaa..0ba45c0582a 100644
--- a/gateway/platforms/api_server.py
+++ b/gateway/platforms/api_server.py
@@ -3057,14 +3057,7 @@ class APIServerAdapter(OpenAICompatRoutesMixin, BasePlatformAdapter):
if db is None:
return []
try:
- # Include compression-ancestor messages so the API server returns
- # the full transcript after a compression rotation, not just the
- # child continuation (#51058).
- return await asyncio.to_thread(
- db.get_messages_as_conversation,
- session_id,
- include_ancestors=True,
- )
+ return await asyncio.to_thread(db.get_messages_as_conversation, session_id)
except Exception as exc:
logger.warning("Failed to load session history for %s: %s", session_id, exc)
return []
diff --git a/hermes_state_messages.py b/hermes_state_messages.py
index 96cc9439b04..ff2551a826c 100644
--- a/hermes_state_messages.py
+++ b/hermes_state_messages.py
@@ -1407,6 +1407,8 @@ class SessionMessagesMixin:
raise ValueError("after_id is incompatible with latest/offset paging")
if after_id is not None and include_compacted:
raise ValueError("after_id is incompatible with include_compacted (deduped display reads use offset paging)")
+ if after_id is not None and include_ancestors:
+ raise ValueError("after_id is incompatible with include_ancestors (merged-lineage reads use offset paging)")
active_clause = self._active_clause(include_inactive, include_compacted)
# Ancestor expansion uses the resume lineage (explicit /branch copies stay single-session).
session_ids = self._resume_lineage_ids(session_id) if include_ancestors else [session_id]
diff --git a/tests/hermes_state/test_hermes_state.py b/tests/hermes_state/test_hermes_state.py
index e803af4eeb6..dbf90f206fb 100644
--- a/tests/hermes_state/test_hermes_state.py
+++ b/tests/hermes_state/test_hermes_state.py
@@ -6598,3 +6598,21 @@ class TestGetMessagesAncestors:
assert [m["content"] for m in rows] == ["p1", "c2", "c3"]
rows = db.get_messages("child", include_ancestors=True, limit=3, offset=3)
assert [m["content"] for m in rows] == ["c4", "c5"]
+
+
+class TestGetMessagesAncestorsGuards:
+ """Paging guards for the merged-lineage read (get_messages)."""
+
+ def test_after_id_rejected_with_include_ancestors(self, db):
+ """after_id keyset paging must not silently drop the cursor on a merged
+ lineage read (the multi-segment set is deduped before paging, so the
+ per-row cursor is not meaningful across segments)."""
+ db.create_session("parent", source="tui")
+ db.append_message("parent", role="user", content="p1")
+ db.end_session("parent", "compression")
+ db.create_session("child", source="tui", parent_session_id="parent")
+ db.append_message("child", role="user", content="c1")
+ anchor = db.get_messages("child")[0]
+
+ with pytest.raises(ValueError, match="after_id is incompatible with include_ancestors"):
+ db.get_messages("child", include_ancestors=True, after_id=anchor["id"])
diff --git a/tests/hermes_state/test_session_db_read_path_split.py b/tests/hermes_state/test_session_db_read_path_split.py
index db488a85c0a..c731553bf69 100644
--- a/tests/hermes_state/test_session_db_read_path_split.py
+++ b/tests/hermes_state/test_session_db_read_path_split.py
@@ -159,6 +159,9 @@ def test_session_resume_reads_do_not_take_writer_lock(db):
db.create_session(session_id="parent1", source="cli", model="m")
db.append_message("parent1", role="user", content="parent turn")
db.append_message("parent1", role="assistant", content="parent reply")
+ # A real rotation stamps the parent end_reason='compression' (publish_compression_child);
+ # without the stamp this is an unverified parent link no production writer creates.
+ db.end_session("parent1", "compression")
db.create_session(session_id="child1", source="cli", model="m", parent_session_id="parent1")
db.append_message("child1", role="user", content="child turn")
db.append_message("child1", role="assistant", content="child reply")