Skip to content

The agent that grows with you

fix(api_server): honour include_compacted on /api/sessions/{id}/messages

Austin Pickett7cadfaa66858
diff --git a/gateway/platforms/api_server.py b/gateway/platforms/api_server.py
index 0ba45c0582a..6195403c905 100644
--- a/gateway/platforms/api_server.py
+++ b/gateway/platforms/api_server.py
@@ -3297,10 +3297,11 @@ class APIServerAdapter(OpenAICompatRoutesMixin, BasePlatformAdapter):
default_page = requested_limit is None
latest_page = order == "latest" or (order is None and default_page)
limit = 500 if default_page else min(requested_limit, 500)
+ include_compacted = _coerce_request_bool(request.query.get("include_compacted"), default=False)
# Compression lineage: return root→tip messages, matching the REST router (#51058).
messages = await asyncio.to_thread(
db.get_messages, resolved_id, limit=limit, offset=offset, latest=latest_page,
- include_ancestors=True)
+ include_compacted=include_compacted, include_ancestors=True)
return web.json_response({
"object": "list", "session_id": resolved_id,
"data": [self._message_response(m) for m in messages],
diff --git a/tests/gateway/test_api_server_compaction_projection.py b/tests/gateway/test_api_server_compaction_projection.py
index b38534d4149..5050f72957d 100644
--- a/tests/gateway/test_api_server_compaction_projection.py
+++ b/tests/gateway/test_api_server_compaction_projection.py
@@ -242,3 +242,61 @@ class TestMessagesEndpointProjection:
rendered = " ".join(str(message.get("content") or "") for message in messages)
assert "PRIOR CONTEXT" not in rendered
assert "CONTEXT COMPACTION" not in rendered
+
+ @pytest.mark.asyncio
+ async def test_messages_endpoint_honours_include_compacted(self, adapter, session_db, tmp_path, monkeypatch):
+ monkeypatch.setenv("HERMES_HOME", str(tmp_path))
+ sid = session_db.create_session("compacted-history", "api_server")
+ for i in range(1, 4):
+ session_db.append_message(sid, "user", f"question {i}")
+ session_db.append_message(sid, "assistant", f"answer {i}")
+ tail = [{"role": "user", "content": "question 3"}, {"role": "assistant", "content": "answer 3"}]
+ session_db.archive_and_compact(
+ sid, [_row("user", STANDALONE_SUMMARY), *tail],
+ watermark=session_db.get_active_message_watermark(sid), tail_count=len(tail))
+
+ async def contents(query: str) -> list:
+ async with TestClient(TestServer(_messages_app(adapter))) as client:
+ response = await client.get(f"/api/sessions/{sid}/messages{query}")
+ assert response.status == 200
+ return [m.get("content") for m in (await response.json())["data"]]
+
+ live = await contents("")
+ assert "question 1" not in live and "answer 3" in live
+ for query in ("?include_compacted=false", "?include_compacted=0"):
+ assert await contents(query) == live
+ full = await contents("?include_compacted=true")
+ for text in ("question 1", "answer 1", "question 2", "answer 2", "question 3", "answer 3"):
+ assert full.count(text) == 1, (text, full)
+ assert full.index("question 1") < full.index("answer 3")
+
+ @pytest.mark.asyncio
+ async def test_include_compacted_keeps_compression_ancestors(self, adapter, session_db, tmp_path, monkeypatch):
+ monkeypatch.setenv("HERMES_HOME", str(tmp_path))
+ session_db.create_session("rotated-parent", "api_server")
+ session_db.append_message("rotated-parent", "user", "parent ask")
+ session_db.append_message("rotated-parent", "assistant", "parent answer")
+ session_db.end_session("rotated-parent", "compression")
+ child = session_db.create_session("rotated-child", "api_server", parent_session_id="rotated-parent")
+ for i in range(1, 3):
+ session_db.append_message(child, "user", f"child question {i}")
+ session_db.append_message(child, "assistant", f"child answer {i}")
+ tail = [{"role": "user", "content": "child question 2"}, {"role": "assistant", "content": "child answer 2"}]
+ session_db.archive_and_compact(
+ child, [_row("user", STANDALONE_SUMMARY), *tail],
+ watermark=session_db.get_active_message_watermark(child), tail_count=len(tail))
+
+ async def contents(query: str) -> list:
+ async with TestClient(TestServer(_messages_app(adapter))) as client:
+ response = await client.get(f"/api/sessions/{child}/messages{query}")
+ assert response.status == 200
+ return [m.get("content") for m in (await response.json())["data"]]
+
+ live = await contents("")
+ assert "parent ask" in live and "child answer 2" in live
+ assert "child question 1" not in live
+ full = await contents("?include_compacted=true")
+ for text in ("parent ask", "parent answer", "child question 1", "child answer 1",
+ "child question 2", "child answer 2"):
+ assert full.count(text) == 1, (text, full)
+ assert full.index("parent ask") < full.index("child question 1") < full.index("child answer 2")
diff --git a/website/docs/user-guide/features/api-server.md b/website/docs/user-guide/features/api-server.md
index 5752296eb32..82bd01f0447 100644
--- a/website/docs/user-guide/features/api-server.md
+++ b/website/docs/user-guide/features/api-server.md
@@ -611,7 +611,7 @@ External UIs can manage Hermes sessions over REST without standing up the dashbo
| `GET` | `/api/sessions/{id}` | Read session metadata |
| `PATCH` | `/api/sessions/{id}` | Update title or `end_reason` |
| `DELETE` | `/api/sessions/{id}` | Delete a session |
-| `GET` | `/api/sessions/{id}/messages` | Message history for a session. `inline_images=false` renders image attachments as `[image]` placeholders instead of inline data URIs — the transcript travels in kilobytes, for clients reading over a network |
+| `GET` | `/api/sessions/{id}/messages` | Message history for a session. `include_compacted=true` also returns the turns a context compaction archived (default: live transcript only). `inline_images=false` renders image attachments as `[image]` placeholders instead of inline data URIs — the transcript travels in kilobytes, for clients reading over a network |
| `POST` | `/api/sessions/{id}/fork` | Branch the session via `SessionDB` lineage (matches CLI `/branch` semantics) |
| `POST` | `/api/sessions/{id}/chat` | Run one synchronous agent turn |
| `POST` | `/api/sessions/{id}/chat/stream` | SSE wrapper over a single turn — emits `assistant.delta`, `assistant.commentary` (mid-turn commentary: `message_id`, `text`, `already_streamed`; never folded into `assistant.completed`), `tool.started`, `tool.completed`, `tool.failed` (a tool that finished with an error), then a terminal `run.completed` / `run.failed` / `run.cancelled` event that matches how the turn ended (see [Terminal run status](../../developer-guide/programmatic-integration.md#terminal-run-status)) |