Skip to content

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

Size the tail as Hermes sends it, require the whole summary carrier, and reset calibration on a session reset

Elevatormusic6b85661039b1
diff --git a/evidence/plugin-integration.json b/evidence/plugin-integration.json
index 3f57786..20e0946 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-06T14:39:33Z",
+ "started_utc": "2026-10-06T15:00:00Z",
"hermes": {
"commit": "45871e100feceb89769536c88e5e6e265226a409",
"clean": true
@@ -10,12 +10,12 @@
"plugin_files_sha256": {
"warm_compaction/__init__.py": "11e038f05813627b2d1d8b82b3b775ecfe41015746c8e90ed6d452cfdaec65a6",
"warm_compaction/capture.py": "387a5bda2c97530c6e9114e810ac062199e3731c12a582d75598d2bdb95e8c57",
- "warm_compaction/engine.py": "ba58072c5508c987bc4095b9a9c30c6aedffa998b07562c70071fa0845088106",
+ "warm_compaction/engine.py": "1e8c9ec97c6c1de67c8fd8cff3aef284836737063428c813636ebf07e5528633",
"warm_compaction/fallback.py": "f4cf08186f38fc9a46f47af2bedf22584b6ede4f11de1c3abd0fd598aec2f8a4",
"warm_compaction/handoff.py": "9afea62ec2f8ef46cbeefd70b1071bb6c7111fa6fd1cd073e1fb2c7eae30f3c2",
- "warm_compaction/layout.py": "a45b12d53b7fcaf8ca61a57000cf29329d68fc4ea47526c9919ce46e8bb8146b",
+ "warm_compaction/layout.py": "9ef3d55e5275551ea388367b578164a3a0d2c0c1edbd40aba5a1ec38fc25a950",
"warm_compaction/plugin.yaml": "d67560fd4625ec80d9e26f3e6d712763e77474f68d469c7e48f09fd3506ccc71",
- "warm_compaction/rows.py": "d2ca064205e4d2c021240efcf64bb85c70e144c424dabafdbae085a0855f0836",
+ "warm_compaction/rows.py": "e80983b65a0c6406c06d4e64c6cb58c101799ea55dcd759e21ab625925466107",
"warm_compaction/warm.py": "f76aa64c739f8db7e228c25f9f3ee0c7d890c48eea8215f75577a2d6e5f4736b"
},
"privacy": "metadata only: names, counts, codes, booleans, and times; no message text",
@@ -76,11 +76,11 @@
"threshold_tokens": 100000,
"context_length": 200000
},
- "compress_seconds": 0.099,
+ "compress_seconds": 0.104,
"warm_last": {
"path": "warm",
"reason": "accepted",
- "elapsed_s": 0.023,
+ "elapsed_s": 0.028,
"prompt_tokens": 33892,
"cached_tokens": 16946
},
@@ -88,10 +88,11 @@
"final_session_id": "wc-enable_manual_warm",
"continuation_keeps_custom_system_text": false,
"fence_denied": {
- "network_host_blocked": 1,
- "child_process_blocked": 3
+ "child_process_blocked": 4,
+ "network_host_blocked": 1
},
"fence_blocked_programs": [
+ "git",
"pip",
"python",
"python3"
@@ -100,6 +101,7 @@
"hermes-assets.nousresearch.com"
],
"fence_blocked_callers": [
+ "child_process_blocked from hermes_cli/source_check.py:_git_run < hermes_cli/source_check.py:_git_stdout < hermes_cli/source_check.py:_read_checkout",
"child_process_blocked from tools/env_probe.py:_run < tools/env_probe.py:_py_out < tools/env_probe.py:_pip_python_version",
"child_process_blocked from tools/env_probe.py:_run < tools/env_probe.py:_py_out < tools/env_probe.py:_python_version_of",
"network_host_blocked from hermes_cli/release_channels.py:read < hermes_cli/release_channels.py:read_bytes < hermes_cli/release_channels.py:read_record"
@@ -187,7 +189,7 @@
"install",
"run"
],
- "process_seconds": 53.942
+ "process_seconds": 82.1
},
{
"scenario": "manual_fallback",
@@ -246,7 +248,7 @@
"threshold_tokens": 100000,
"context_length": 200000
},
- "compress_seconds": 0.239,
+ "compress_seconds": 0.252,
"warm_last": {
"path": "fallback",
"reason": "provider_error",
@@ -359,7 +361,7 @@
"install",
"run"
],
- "process_seconds": 32.634
+ "process_seconds": 39.555
},
{
"scenario": "manual_fixed",
@@ -417,11 +419,11 @@
"threshold_tokens": 100000,
"context_length": 200000
},
- "compress_seconds": 0.228,
+ "compress_seconds": 0.26,
"warm_last": {
"path": "fixed",
"reason": "provider_error",
- "elapsed_s": 0.109,
+ "elapsed_s": 0.125,
"prompt_tokens": null,
"cached_tokens": null
},
@@ -530,7 +532,7 @@
"install",
"run"
],
- "process_seconds": 30.224
+ "process_seconds": 36.904
},
{
"scenario": "auto_tool_loop",
@@ -618,14 +620,14 @@
"warm_last_1": {
"path": "warm",
"reason": "accepted",
- "elapsed_s": 0.015,
+ "elapsed_s": 0.031,
"prompt_tokens": 35209,
"cached_tokens": 17604
},
"warm_last_2": {
"path": "warm",
"reason": "accepted",
- "elapsed_s": 0.015,
+ "elapsed_s": 0.016,
"prompt_tokens": 18436,
"cached_tokens": 9218
},
@@ -735,7 +737,7 @@
"install",
"run"
],
- "process_seconds": 33.729
+ "process_seconds": 41.908
},
{
"scenario": "rollback",
@@ -785,11 +787,11 @@
"threshold_tokens": 100000,
"context_length": 200000
},
- "compress_seconds": 0.143,
+ "compress_seconds": 0.149,
"warm_last": {
"path": "warm",
"reason": "accepted",
- "elapsed_s": 0.0,
+ "elapsed_s": 0.016,
"prompt_tokens": 33882,
"cached_tokens": 16941
},
@@ -797,7 +799,7 @@
"final_session_id": "wc-rollback",
"continuation_keeps_custom_system_text": false,
"fence_denied": {
- "child_process_blocked": 4,
+ "child_process_blocked": 5,
"network_host_blocked": 1
},
"fence_blocked_programs": [
@@ -948,8 +950,7 @@
"stored": 30
},
"fence_denied": {
- "child_process_blocked": 5,
- "network_host_blocked": 1
+ "child_process_blocked": 5
},
"fence_blocked_programs": [
"git",
@@ -957,9 +958,7 @@
"python",
"python3"
],
- "fence_blocked_hosts": [
- "hermes-assets.nousresearch.com"
- ]
+ "fence_blocked_hosts": []
},
"exit": [
0,
@@ -971,10 +970,10 @@
"run",
"rollback"
],
- "process_seconds": 39.267
+ "process_seconds": 45.121
}
],
- "finished_utc": "2026-10-06T14:42:43Z",
+ "finished_utc": "2026-10-06T15:04:06Z",
"passed": true,
"run_folder": "outside repository"
}
diff --git a/tests/test_wc_engine.py b/tests/test_wc_engine.py
index c0e3954..625654c 100644
--- a/tests/test_wc_engine.py
+++ b/tests/test_wc_engine.py
@@ -704,8 +704,13 @@ class EngineTest(unittest.TestCase):
def test_reset_clears_the_status(self):
self.engine.compress([*old_turns(), assistant("done")])
+ self.engine.update_from_response({"prompt_tokens": 150_000, "completion_tokens": 10})
+ self.engine.awaiting_real_usage_after_compression = True
self.engine.on_session_reset()
self.assertEqual((self.engine.warm_last, self.engine.compression_count), (None, 0))
+ # Hermes uses the real prompt count of the old session as a floor unless the latch is set.
+ self.assertEqual((self.engine.last_real_prompt_tokens,
+ self.engine.awaiting_real_usage_after_compression), (0, False))
self.assertIn("warm_last", self.engine.get_status())
def test_an_unknown_overhead_caps_the_tail(self):
diff --git a/tests/test_wc_layout.py b/tests/test_wc_layout.py
index 3aab847..7edadc6 100644
--- a/tests/test_wc_layout.py
+++ b/tests/test_wc_layout.py
@@ -21,7 +21,7 @@ class PredicateTest(unittest.TestCase):
self.assertTrue(is_real_user(user("hi"), PREFIXES))
self.assertTrue(is_real_user(user("x", display_kind="steer"), PREFIXES))
for row in (user("x", _todo_snapshot_synthetic=True), user("x", _dropped_toolcall_nudge=True),
- user("x", display_kind="hidden"), user(" "), user("[HERMES PREFIX] old"),
+ user("x", display_kind="hidden"), user(" "), user("[HERMES PREFIX]\n\n" + HEADER_TEXT),
user("x", _compressed_summary=True), user("[System: Your previous tool call failed"),
assistant("hi")):
with self.subTest(row=row):
@@ -43,10 +43,19 @@ class PredicateTest(unittest.TestCase):
self.assertTrue(is_real_user(user("Continue now."), PREFIXES))
def test_summary_detection(self):
- self.assertTrue(is_summary(assistant("[CONTEXT SUMMARY]: s"), PREFIXES))
+ # Without the flag (the Hermes session store drops it), only the whole carrier is a summary: a prefix and
+ # the end marker, or the plugin header row.
+ self.assertTrue(is_summary(assistant("[CONTEXT SUMMARY]: s\n\n--- END OF CONTEXT SUMMARY x"), PREFIXES))
+ self.assertTrue(is_summary(user("[HERMES PREFIX]\n\n" + HEADER_TEXT), PREFIXES))
self.assertTrue(is_summary(user("plain", _compressed_summary=True), PREFIXES))
self.assertFalse(is_summary(user("plain"), PREFIXES))
+ def test_a_user_message_that_starts_with_a_prefix_is_real(self):
+ # "Analyze this summary" pasted by the user is the active request, not an earlier summary.
+ row = user("[CONTEXT SUMMARY]: please check this text for errors.")
+ self.assertFalse(is_summary(row, PREFIXES))
+ self.assertTrue(is_real_user(row, PREFIXES))
+
class TailTest(unittest.TestCase):
def test_units_keep_tool_groups_whole(self):
@@ -177,6 +186,14 @@ class BoundTailTest(unittest.TestCase):
self.assertNotIn("api_content", bounded[2])
self.assertIs(bound_tail(rows[:1], 2_000)[0], rows[0])
+ def test_the_tail_is_sized_as_hermes_sends_it(self):
+ # Hermes sends api_content in place of content: a row with both fields costs its sent text only.
+ from warm_compaction.layout import bound_tail
+ text = "t" * 4_000
+ rows = [user("o" * 4_000), assistant("a"), user(text, api_content=text + " [ctx]"), assistant("b")]
+ self.assertEqual(tail_start(rows, 1_500, PREFIXES), (2, None))
+ self.assertIs(bound_tail(rows[2:], 1_500)[0], rows[2])
+
def test_a_large_assistant_row_is_cut_and_keeps_its_tool_calls(self):
from warm_compaction.layout import bound_tail
from warm_compaction.rows import MIDDLE_MARK, estimate_tokens
diff --git a/warm_compaction/engine.py b/warm_compaction/engine.py
index 19771dd..5b6c789 100644
--- a/warm_compaction/engine.py
+++ b/warm_compaction/engine.py
@@ -210,6 +210,9 @@ class WarmCompactionEngine(ContextEngine):
def on_session_reset(self) -> None:
super().on_session_reset()
self.warm_last = None
+ # The calibration of the old session: Hermes uses the real prompt count as a floor unless the latch is set.
+ self.last_real_prompt_tokens = 0
+ self.awaiting_real_usage_after_compression = False
def get_status(self) -> dict[str, Any]:
status = super().get_status()
@@ -415,7 +418,7 @@ class WarmCompactionEngine(ContextEngine):
return None
window = int(self.context_length or 0)
limit = min(threshold, window - reserve) if window > 0 else threshold
- return limit - (overhead + estimate_tokens(tail_rows) + estimate_tokens(summary) + CARRIER_TOKENS)
+ return limit - (overhead + estimate_tokens(sent_rows(tail_rows)) + estimate_tokens(summary) + CARRIER_TOKENS)
def _tail_cap(self, summary_tokens: int, overhead: int | None, reserve: int, prepend_tokens: int = 0) -> int:
"""The tail budget, and at most the free room for the tail after the overhead, the summary, and the
diff --git a/warm_compaction/layout.py b/warm_compaction/layout.py
index 979e4f8..5a3953e 100644
--- a/warm_compaction/layout.py
+++ b/warm_compaction/layout.py
@@ -8,7 +8,7 @@ from typing import Any, Iterable
from .handoff import END_MARKER, LEGACY_PREFIX
from .rows import (api_content, attr, compact_json, cut_bounds, cut_middle, estimate_tokens, hermes_value, plain_text,
- visible_text)
+ sent_tokens, visible_text)
HEADER_TEXT = "The summary of the earlier turns follows."
COPY_HEADING = "## Copied user messages"
@@ -80,11 +80,18 @@ def nudge_texts() -> frozenset[str]:
def is_summary(row: Any, prefixes: Iterable[str]) -> bool:
- """Return True for a summary record: the Hermes summary flag or a known summary prefix."""
+ """Return True for a summary record: the Hermes summary flag, or (the Hermes session store drops the flag) the
+ whole carrier: a known summary prefix with the end marker, or the plugin header row. A user message that only
+ starts with a prefix is a real request."""
if attr(row, "_compressed_summary"):
return True
- text = plain_text(attr(row, "content")).lstrip()
- return any(prefix and text.startswith(prefix) for prefix in prefixes)
+ text = plain_text(attr(row, "content")).strip()
+ for prefix in prefixes:
+ if prefix and text.startswith(prefix):
+ rest = text[len(prefix):].strip()
+ if END_MARKER in rest or rest == HEADER_TEXT:
+ return True
+ return False
def is_real_user(row: Any, prefixes: Iterable[str]) -> bool:
@@ -125,9 +132,10 @@ def tail_start(messages: list, tail_tokens: int, prefixes: Iterable[str]) -> tup
if not spans:
return 0, None
start = spans[-1][0]
- size = sum(estimate_tokens(row) for row in messages[start:])
+ # As Hermes sends the rows: the stored display text of a row with api_content is not in the request.
+ size = sum(sent_tokens(row) for row in messages[start:])
for span_start, span_end in reversed(spans[:-1]):
- cost = sum(estimate_tokens(row) for row in messages[span_start:span_end])
+ cost = sum(sent_tokens(row) for row in messages[span_start:span_end])
if size + cost > tail_tokens:
break
size += cost
@@ -310,7 +318,7 @@ def bound_tail(rows: list, tokens: int, removed: list | None = None) -> list:
limits: dict[tuple, int] = {}
# First the cuts to start and end (at least MIN_COPY_CHARS), then, when they are not enough, the drops.
floor = MIN_COPY_CHARS
- while sum(estimate_tokens(row) for row in rows) > tokens:
+ while sum(sent_tokens(row) for row in rows) > tokens:
cuttable = []
for index, row in enumerate(rows):
for key, value in _tail_parts(row):
@@ -332,7 +340,7 @@ def bound_tail(rows: list, tokens: int, removed: list | None = None) -> list:
limits[(index, key)] = 0
new_value = DROPPED
else:
- target = cost - (sum(estimate_tokens(row) for row in rows) - tokens)
+ target = cost - (sum(sent_tokens(row) for row in rows) - tokens)
limit = max(MIN_COPY_CHARS, min(len(value) - 1, len(value) * max(target, 0) // max(cost, 1)))
# Cut the original text again: the kept start and end are then parts of the original text.
limits[(index, key)] = limit
diff --git a/warm_compaction/rows.py b/warm_compaction/rows.py
index 93f8a33..d0ad452 100644
--- a/warm_compaction/rows.py
+++ b/warm_compaction/rows.py
@@ -113,6 +113,11 @@ def sent_rows(messages: list) -> list:
return out
+def sent_tokens(row: Any) -> int:
+ """Estimated tokens of one row as Hermes sends it (sent_rows)."""
+ return estimate_tokens(sent_rows([row])[0])
+
+
def cut_bounds(text: str, limit: int) -> tuple[int, int]:
"""Return (end of the kept start, start of the kept end) of cut_middle: text[first:second] is the removed
part."""