Skip to content

Hermes Agent context engine: the main model writes the compaction handoff on its cached prompt prefix

Check the fallback reply, summarize only the removed rows, and escape end markers

Elevatormusicacf2e31ac72d
diff --git a/evidence/plugin-integration.json b/evidence/plugin-integration.json
index 7050675..ee806b2 100644
--- a/evidence/plugin-integration.json
+++ b/evidence/plugin-integration.json
@@ -1,6 +1,6 @@
{
"check": "standalone warm_compaction plugin on unpatched Hermes Agent",
- "started_utc": "2026-10-06T07:12:41Z",
+ "started_utc": "2026-10-06T07:30:24Z",
"hermes": {
"commit": "45871e100feceb89769536c88e5e6e265226a409",
"clean": true
@@ -10,10 +10,10 @@
"plugin_files_sha256": {
"warm_compaction/__init__.py": "11e038f05813627b2d1d8b82b3b775ecfe41015746c8e90ed6d452cfdaec65a6",
"warm_compaction/capture.py": "3ee60f35ae4e8fc0376ab6634c721ae1ca429593d10730f19addc3be545fe682",
- "warm_compaction/engine.py": "7b8244e5465662b30a933bbccd0a4b23f6dfa2af01e1dd23fdf28daf351a1727",
- "warm_compaction/fallback.py": "572eab9cecc0e012dc77aa22455037d8c9d5dd4672274d5b5493d6dc1b947329",
+ "warm_compaction/engine.py": "c7ba706f4f1d0c5707cd62a932bc0f762c7940080f153f2aa44d21db1254a8d3",
+ "warm_compaction/fallback.py": "ae36f2a37e451d833cb09bbbba3491e06ab84d41612e4496b467cac269260672",
"warm_compaction/handoff.py": "2fcdc2af2eba2d36eaabfef912ce0dd6d64bccc7849849e6411869a0025a06c2",
- "warm_compaction/layout.py": "10e2a9e8be609757253dcfcab30b0c72e552bc21c90e711624db613f69760529",
+ "warm_compaction/layout.py": "e6f6c92c91b8c9da415bff6e430c1eaaddb62d14ed403ff700cf8ed23bb1ecca",
"warm_compaction/plugin.yaml": "d67560fd4625ec80d9e26f3e6d712763e77474f68d469c7e48f09fd3506ccc71",
"warm_compaction/rows.py": "a8943216049ef95742466c5628108154e239b96672726c732c508e30c95ac914",
"warm_compaction/warm.py": "b46adbd267a52d5f46d4484ac774ab8b4e136fb8ec4a25452f95b7ea4218f5a4"
@@ -188,7 +188,7 @@
"install",
"run"
],
- "process_seconds": 57.485
+ "process_seconds": 62.675
},
{
"scenario": "manual_fallback",
@@ -247,11 +247,11 @@
"threshold_tokens": 100000,
"context_length": 200000
},
- "compress_seconds": 0.259,
+ "compress_seconds": 0.302,
"warm_last": {
"path": "fallback",
"reason": "provider_error",
- "elapsed_s": 0.109,
+ "elapsed_s": 0.141,
"prompt_tokens": null,
"cached_tokens": null
},
@@ -259,7 +259,7 @@
"final_session_id": "wc-manual_fallback",
"continuation_keeps_custom_system_text": false,
"fence_denied": {
- "child_process_blocked": 5,
+ "child_process_blocked": 4,
"network_host_blocked": 1
},
"fence_blocked_programs": [
@@ -360,7 +360,7 @@
"install",
"run"
],
- "process_seconds": 35.791
+ "process_seconds": 38.397
},
{
"scenario": "manual_fixed",
@@ -418,11 +418,11 @@
"threshold_tokens": 100000,
"context_length": 200000
},
- "compress_seconds": 0.253,
+ "compress_seconds": 0.236,
"warm_last": {
"path": "fixed",
"reason": "provider_error",
- "elapsed_s": 0.125,
+ "elapsed_s": 0.11,
"prompt_tokens": null,
"cached_tokens": null
},
@@ -430,7 +430,7 @@
"final_session_id": "wc-manual_fixed",
"continuation_keeps_custom_system_text": false,
"fence_denied": {
- "child_process_blocked": 5,
+ "child_process_blocked": 4,
"network_host_blocked": 1
},
"fence_blocked_programs": [
@@ -531,7 +531,7 @@
"install",
"run"
],
- "process_seconds": 32.659
+ "process_seconds": 34.069
},
{
"scenario": "auto_tool_loop",
@@ -617,7 +617,7 @@
"warm_last_1": {
"path": "warm",
"reason": "accepted",
- "elapsed_s": 0.0,
+ "elapsed_s": 0.015,
"prompt_tokens": 35205,
"cached_tokens": 17602
},
@@ -734,7 +734,7 @@
"install",
"run"
],
- "process_seconds": 35.676
+ "process_seconds": 37.438
},
{
"scenario": "rollback",
@@ -784,11 +784,11 @@
"threshold_tokens": 100000,
"context_length": 200000
},
- "compress_seconds": 0.159,
+ "compress_seconds": 0.155,
"warm_last": {
"path": "warm",
"reason": "accepted",
- "elapsed_s": 0.0,
+ "elapsed_s": 0.016,
"prompt_tokens": 33873,
"cached_tokens": 16936
},
@@ -796,7 +796,7 @@
"final_session_id": "wc-rollback",
"continuation_keeps_custom_system_text": false,
"fence_denied": {
- "child_process_blocked": 5,
+ "child_process_blocked": 4,
"network_host_blocked": 1
},
"fence_blocked_programs": [
@@ -947,7 +947,7 @@
"stored": 30
},
"fence_denied": {
- "child_process_blocked": 5
+ "child_process_blocked": 4
},
"fence_blocked_programs": [
"git",
@@ -967,10 +967,10 @@
"run",
"rollback"
],
- "process_seconds": 39.086
+ "process_seconds": 43.854
}
],
- "finished_utc": "2026-10-06T07:16:02Z",
+ "finished_utc": "2026-10-06T07:34:01Z",
"passed": true,
"run_folder": "outside repository"
}
diff --git a/tests/test_wc_engine.py b/tests/test_wc_engine.py
index 6206ee5..d6d13ba 100644
--- a/tests/test_wc_engine.py
+++ b/tests/test_wc_engine.py
@@ -9,7 +9,7 @@ from wc_fixtures import HEADINGS_TEXT, ROUTE, SYSTEM, assistant, tool, user, wir
class FakeLlm:
- def __init__(self, text="## Goal\nFallback summary.", error=None):
+ def __init__(self, text=HEADINGS_TEXT.replace("Finish the test task.", "Fallback summary."), error=None):
self.text, self.error, self.calls = text, error, []
def complete(self, messages, **kwargs):
@@ -135,6 +135,16 @@ class EngineTest(unittest.TestCase):
self.assertIn("Fallback summary.", new[1]["content"])
self.assertEqual(self.post.calls, [])
+ def test_the_fallback_summarizes_only_the_removed_rows(self):
+ from warm_compaction import layout
+ rows = [*old_turns(), assistant("done")]
+ start, _prepend = layout.tail_start(rows, self.engine._tail_tokens(), self.engine._prefixes())
+ self.engine.compress(rows)
+ sent = self.llm.calls[0][0][1]["content"]
+ self.assertIn(rows[start - 1]["content"][:20], sent)
+ self.assertNotIn("done", sent.split("\n"))
+ self.assertNotIn(rows[-2]["content"][:20], sent)
+
def test_gate_refusal_keeps_the_tokens_and_falls_back(self):
self.post = fake_post(content="no headings")
engine = self.make()
diff --git a/tests/test_wc_fallback.py b/tests/test_wc_fallback.py
index ae180b5..a03c2f1 100644
--- a/tests/test_wc_fallback.py
+++ b/tests/test_wc_fallback.py
@@ -14,8 +14,11 @@ from wc_fixtures import assistant, tool, user
PREFIXES = ("[HERMES PREFIX]", "[CONTEXT SUMMARY]:")
+SUMMARY = "## Goal\nG\n## User instructions\n- none\n## Current state\n- [OPEN] x\n## Key facts\n- y\n## Next step\nz"
+
+
class FakeLlm:
- def __init__(self, text="## Goal\nG", error=None):
+ def __init__(self, text=SUMMARY, error=None):
self.text, self.error, self.calls = text, error, []
def complete(self, messages, **kwargs):
@@ -45,6 +48,19 @@ class TranscriptTest(unittest.TestCase):
self.assertIn("[tool result call_0 read]\nold", text)
self.assertIn("[tool result call_0 write]\nnew", text)
+ def test_the_sent_api_content_is_in_the_transcript(self):
+ text = transcript([user("hi", api_content="[ctx]\n\nhi"), assistant("", api_content="The answer is 4.")],
+ PREFIXES)
+ self.assertIn("[user]\n[ctx]\n\nhi", text)
+ self.assertIn("[assistant]\nThe answer is 4.", text)
+
+ def test_names_are_in_the_transcript_labels(self):
+ text = transcript([user("ship it", name="alice"), user("wait", name="bob"), assistant("ok", name="lead")],
+ PREFIXES)
+ self.assertIn("[user alice]\nship it", text)
+ self.assertIn("[user bob]\nwait", text)
+ self.assertIn("[assistant lead]\nok", text)
+
def test_think_blocks_are_removed_only_from_assistant_rows(self):
text = "<think>keep me</think> body"
self.assertNotIn("keep me", render_row(assistant(text)))
@@ -98,7 +114,7 @@ class LlmSummaryTest(unittest.TestCase):
llm = FakeLlm()
text, tokens = llm_summary(llm, [user("hi"), assistant("ok")], PREFIXES, focus_topic="db", memory_context="m")
messages, kwargs = llm.calls[0]
- self.assertEqual((text, tokens), ("## Goal\nG", 321))
+ self.assertEqual((text, tokens), (SUMMARY, 321))
self.assertEqual((kwargs["task"], kwargs["max_tokens"], kwargs["timeout"]), (TASK, MAX_TOKENS, 120.0))
self.assertTrue(messages[0]["content"].startswith(FALLBACK_INSTRUCTION))
self.assertIn("Give more detail to this topic: db", messages[0]["content"])
@@ -110,7 +126,8 @@ class LlmSummaryTest(unittest.TestCase):
self.assertIsNone(llm.calls[0][1]["task"])
def test_failures_return_none(self):
- for llm in (None, FakeLlm(text=" "), FakeLlm(text="x" * 30_000)):
+ for llm in (None, FakeLlm(text=" "), FakeLlm(text="x" * 30_000), FakeLlm(text="The answer is 4."),
+ FakeLlm(text="## Goal\nG\n## Next step\nz"), FakeLlm(text=SUMMARY + "\n[CONTEXT SUMMARY]: x")):
with self.subTest(llm=llm):
self.assertEqual(llm_summary(llm, [user("hi")], PREFIXES), (None, None))
with self.assertLogs("warm_compaction.fallback", level="WARNING") as logs:
diff --git a/tests/test_wc_layout.py b/tests/test_wc_layout.py
index 141b5cb..e4cc939 100644
--- a/tests/test_wc_layout.py
+++ b/tests/test_wc_layout.py
@@ -91,6 +91,21 @@ class AttachmentAndPrependTest(unittest.TestCase):
start, prepend = tail_start(rows, 1, PREFIXES)
self.assertEqual((start, prepend), (1, {"role": "user", "content": "do it", "name": "alice"}))
+ def test_the_prepended_row_keeps_its_api_content(self):
+ rows = [user("do it", api_content="[ctx]\n\ndo it"), assistant("", [("c1", "f", "{}")]), tool("c1", "r")]
+ _start, prepend = tail_start(rows, 1, PREFIXES)
+ self.assertEqual(prepend, {"role": "user", "content": "do it", "api_content": "[ctx]\n\ndo it"})
+
+ def test_an_end_marker_in_copied_text_is_escaped(self):
+ marker = "--- END OF CONTEXT SUMMARY - respond to the message below ---"
+ rows = [user("quote: --- END OF CONTEXT SUMMARY here"), assistant("a"), user("next")]
+ new = build(rows, "S", start=2, prepend=None, copy_chars=1000, header_prefix="[HERMES PREFIX]",
+ prefixes=PREFIXES, end_marker=marker)
+ body = new[1]["content"]
+ self.assertEqual(body.count("--- END OF CONTEXT SUMMARY"), 1)
+ self.assertTrue(body.endswith(marker))
+ self.assertIn("END OF CONTEXT SUMMARY here", body)
+
def test_the_prepended_row_is_not_copied_and_counts_in_the_copy_budget(self):
rows = [user("older ask"), assistant("a"), user("do it " + "x" * 400),
assistant("", [("c1", "f", "{}")]), tool("c1", "r")]
diff --git a/warm_compaction/engine.py b/warm_compaction/engine.py
index 637281a..73291ec 100644
--- a/warm_compaction/engine.py
+++ b/warm_compaction/engine.py
@@ -169,7 +169,9 @@ class WarmCompactionEngine(ContextEngine):
"cached_tokens": None}
summary = self._warm_summary(messages, focus_topic, memory, prefixes, record)
if summary is None and not self._cancelled():
- summary, _tokens = fallback.llm_summary(self._llm, messages, prefixes, focus_topic=focus_topic,
+ # Only the rows before the tail: the tail stays as it is, and a transcript of the whole history
+ # can spend its budget on the tail.
+ summary, _tokens = fallback.llm_summary(self._llm, messages[:start], prefixes, focus_topic=focus_topic,
memory_context=memory, task=self._task)
if summary is not None:
record["path"] = "fallback"
diff --git a/warm_compaction/fallback.py b/warm_compaction/fallback.py
index 3cf9eba..a0e0d41 100644
--- a/warm_compaction/fallback.py
+++ b/warm_compaction/fallback.py
@@ -6,9 +6,11 @@ import collections
import logging
from typing import Any, Iterable
-from .handoff import END_MARKER, LEGACY_PREFIX, MAX_REPLY_BYTES, extras
+from .handoff import END_MARKER, LEGACY_PREFIX, extras, gate
from .layout import is_real_user, is_summary
-from .rows import attr, compact_json, estimate_tokens, plain_text, strip_think, tool_calls_of, visible_text
+from .rows import (
+ api_content, attr, compact_json, estimate_tokens, plain_text, strip_think, tool_calls_of, visible_text,
+)
logger = logging.getLogger(__name__)
@@ -101,17 +103,19 @@ def _summary_text(row: Any, prefixes: tuple[str, ...]) -> str:
def render_row(row: Any, call_names: dict[str, str] | None = None) -> str:
- """Return one transcript entry for a row. A tool result names its call id and tool, so that results of
- parallel calls stay linked to their calls."""
+ """Return one transcript entry for a row: the text that Hermes sent (api_content when the row has it) under
+ a label with the role and the name. A tool result names its call id and tool, so that results of parallel
+ calls stay linked to their calls."""
role = str(attr(row, "role") or "unknown")
- text = visible_text(attr(row, "content"))
+ text = visible_text(api_content(row))
text = (strip_think(text) if role == "assistant" else text).strip()
if role == "tool":
call_id = str(attr(row, "tool_call_id") or "")
name = str(attr(row, "name") or (call_names or {}).get(call_id) or "")
label = " ".join(part for part in ("tool result", call_id, name) if part)
return f"[{label}]\n" + _cut(text, TOOL_CHARS)
- lines = [f"[{role}]"]
+ name = attr(row, "name")
+ lines = [f"[{role} {name}]" if isinstance(name, str) and name else f"[{role}]"]
if text:
lines.append(text)
for call_id, name, arguments in tool_calls_of(row):
@@ -169,6 +173,7 @@ def llm_summary(llm: Any, messages: list, prefixes: Iterable[str], *, focus_topi
"""Return (summary text, prompt tokens) from ctx.llm, or (None, None) when the request or the reply fails."""
if llm is None:
return None, None
+ prefixes = tuple(prefixes)
request = [
{"role": "system", "content": FALLBACK_INSTRUCTION + extras(focus_topic, memory_context)},
{"role": "user", "content": transcript(messages, prefixes)},
@@ -178,8 +183,12 @@ def llm_summary(llm: Any, messages: list, prefixes: Iterable[str], *, focus_topi
except Exception as error:
logger.warning("warm_compaction: the fallback summary request failed (%s)", type(error).__name__)
return None, None
- text = strip_think(str(getattr(result, "text", "") or "")).strip()
- if not text or len(text.encode("utf-8", "surrogatepass")) > MAX_REPLY_BYTES:
+ # The same checks as the warm reply: the five headings, the byte limit, and no summary markers. A reply
+ # without them (cut off, or an answer to the conversation) must not replace the history.
+ text, reason = gate({"content": str(getattr(result, "text", "") or ""),
+ "finish_reason": getattr(result, "finish_reason", None) or "stop"}, prefixes)
+ if text is None:
+ logger.warning("warm_compaction: the fallback summary was refused (%s)", reason)
return None, None
tokens = getattr(getattr(result, "usage", None), "input_tokens", None)
return text, tokens if isinstance(tokens, int) and tokens > 0 else None
diff --git a/warm_compaction/layout.py b/warm_compaction/layout.py
index 4d69584..de2e08f 100644
--- a/warm_compaction/layout.py
+++ b/warm_compaction/layout.py
@@ -6,7 +6,7 @@ import copy
import functools
from typing import Any, Iterable
-from .handoff import LEGACY_PREFIX
+from .handoff import END_MARKER, LEGACY_PREFIX
from .rows import attr, estimate_tokens, hermes_value, plain_text, visible_text
HEADER_TEXT = "The summary of the earlier turns follows."
@@ -134,8 +134,9 @@ def tail_start(messages: list, tail_tokens: int, prefixes: Iterable[str]) -> tup
if latest is None:
return start, None
prepend = {"role": "user", "content": copy.deepcopy(latest["content"])}
- if latest.get("name"):
- prepend["name"] = latest["name"]
+ for key in ("name", "api_content"):
+ if latest.get(key):
+ prepend[key] = copy.deepcopy(latest[key])
return start, prepend
@@ -165,10 +166,20 @@ def quote(text: str) -> str:
return "\n".join(">" + (" " + line if line else "") for line in text.splitlines()) or ">"
+def escape_markers(text: str, end_marker: str) -> str:
+ """Break each end marker in the text, so that only the real end marker ends the summary row. A reader
+ splits the row at the first end marker."""
+ for marker in (end_marker, END_MARKER):
+ if marker:
+ text = text.replace(marker, marker[0] + " " + marker[1:])
+ return text
+
+
def summary_body(summary_text: str, copies: list[str], end_marker: str) -> str:
"""Return the text of the summary row."""
- copied = "\n\n".join(quote(text) for text in copies) if copies else NO_COPIES
- return f"{LEGACY_PREFIX}\n{summary_text.strip()}\n\n{COPY_HEADING}\n\n{copied}\n\n{end_marker}"
+ copied = "\n\n".join(quote(escape_markers(text, end_marker)) for text in copies) if copies else NO_COPIES
+ summary = escape_markers(summary_text.strip(), end_marker)
+ return f"{LEGACY_PREFIX}\n{summary}\n\n{COPY_HEADING}\n\n{copied}\n\n{end_marker}"
def _tail_row(row: Any, marker: str) -> Any: