Skip to content

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

Keep the quote after a fallback summary in the summary budget

Elevatormusiccb11c5147b4c
diff --git a/README.md b/README.md
index ceb7148..29c1640 100644
--- a/README.md
+++ b/README.md
@@ -201,7 +201,7 @@ The plugin was faster than both other engines in 10 of 10 cases. hermes-lcm and
- A provider or API key switch forgets the capture of the session: the next compaction uses the fallback summary (`no_capture`) until a main request on the new configuration completes.
- A captured row with a field that Hermes does not send for the stored row (other than the prompt caching marker `cache_control`) refuses the warm path (`source_transform_unsupported`).
- The handoff must start with `## Goal`: text before the first heading is refused (`gate:heading_preamble`).
-- The verbatim tail keeps the newest rows that fit in `tail_tokens`, and in the free room after the system rows and tool schemas of a usable capture, the reply reserve, and a summary: a large system prompt can make a short history compact. It stops at the first older row that does not fit, and that row goes into the summary. When the newest unit alone is larger than the tail or the free room, its largest sent payloads keep only their start and end: message text, tool call arguments (kept as a JSON object), and reasoning text. A large image or other media part is replaced by its attachment mark: a web URL or a file name stays (at most 200 characters), a data URL does not. If the cuts are not enough, the payloads are dropped, largest first; only the row structure stays. Signed `reasoning_details` stay as they are. The fallback summary gets the cut middles, and the fixed summary quotes them (at most 8,000 characters), also the cut middle of a long latest user message that goes in front of the tail. A fallback summary above its 4,096-token reserve (a dense CJK summary, for example) is not used: the fixed summary quotes the parts that the final tail cuts. Each quote makes the fixed summary larger and the tail smaller; after 10 rounds, the tail is cut at the size that the summary quotes, also when that is a little above the free room. The sizes come from the fields that Hermes sends: row metadata does not count; reasoning counts only on a route that needs it back (DeepSeek, Kimi, MiMo thinking; a capture of the route shows it, else a stored row with `reasoning_content`); `reasoning_details` counts only on a route that replays it, without the private native-assistant entries; a tool call counts only its id, type, name, and arguments, and a usable thought signature counts only for a Gemini or Gemma model. The fixed summary keeps bounded quotes of the earlier summary, the focus topic, the memory context, and the cut middles; together with the tool inventory (the most used names, and one count for the others) they stay in one budget (4,096 tokens, and at most the free room). When the fallback summary is used and the latest user request in front of the tail must be cut, the cut middle goes after the summary as a bounded quote: the fallback transcript had only the start and end of a long row. A fallback summary that does not fit in the free room is not used: the fixed summary takes its place. A model or session switch at any time during the compaction discards its result (reason `route_changed`). A change of the provider or the key forgets the capture, also from an empty provider and key. A warm summary that does not fit in the free room (a dense reply under a low threshold, for example) is not used: the fallback summary runs (reason `summary_too_large`). A retry middleware cannot send the warm request a second time, also after a failed send. Reasoning is cut only when a usable capture of the route shows that the route sends it: other reasoning (of an earlier route after a model switch, for example) does not go to the fallback model or into the fixed summary. The latest user request in front of the tail gets the room that stays after the tail is cut. Hermes notifications, task lists, and recovery nudges are not user requests only when the whole text matches the Hermes template. The copied user messages get the room that stays after the tail is cut. Without a usable capture (after a restart, after a model change, or when a middleware changed the captured rows), the request overhead is unknown: the summary has no copied user messages, the tail takes at most half of the free room, the check before a request compacts when the history estimate reaches half of the window less the reply reserve, and the reply reserve is a quarter of the window (at most 65,536 and at least 4,096 tokens). The same reserve applies when the captured request had no positive `max_tokens` or `max_completion_tokens`: the provider default is not known. The host token count is not used for the overhead: it does not use the plugin tokenizer.
+- The verbatim tail keeps the newest rows that fit in `tail_tokens`, and in the free room after the system rows and tool schemas of a usable capture, the reply reserve, and a summary: a large system prompt can make a short history compact. It stops at the first older row that does not fit, and that row goes into the summary. When the newest unit alone is larger than the tail or the free room, its largest sent payloads keep only their start and end: message text, tool call arguments (kept as a JSON object), and reasoning text. A large image or other media part is replaced by its attachment mark: a web URL or a file name stays (at most 200 characters), a data URL does not. If the cuts are not enough, the payloads are dropped, largest first; only the row structure stays. Signed `reasoning_details` stay as they are. The fallback summary gets the cut middles, and the fixed summary quotes them (at most 8,000 characters), also the cut middle of a long latest user message that goes in front of the tail. A fallback summary above its 4,096-token reserve (a dense CJK summary, for example) is not used: the fixed summary quotes the parts that the final tail cuts. Each quote makes the fixed summary larger and the tail smaller; after 10 rounds, the tail is cut at the size that the summary quotes, also when that is a little above the free room. The sizes come from the fields that Hermes sends: row metadata does not count; reasoning counts only on a route that needs it back (DeepSeek, Kimi, MiMo thinking; a capture of the route shows it, else a stored row with `reasoning_content`); `reasoning_details` counts only on a route that replays it, without the private native-assistant entries; a tool call counts only its id, type, name, and arguments, and a usable thought signature counts only for a Gemini or Gemma model. The fixed summary keeps bounded quotes of the earlier summary, the focus topic, the memory context, and the cut middles; together with the tool inventory (the most used names, and one count for the others) they stay in one budget (4,096 tokens, and at most the free room). When the fallback summary is used and the latest user request in front of the tail must be cut, the cut middle goes after the summary as a bounded quote, in what the summary budget has left: the fallback transcript had only the start and end of a long row. A fallback summary that does not fit in the free room is not used: the fixed summary takes its place. A model or session switch at any time during the compaction discards its result (reason `route_changed`). A change of the provider or the key forgets the capture, also from an empty provider and key. A warm summary that does not fit in the free room (a dense reply under a low threshold, for example) is not used: the fallback summary runs (reason `summary_too_large`). A retry middleware cannot send the warm request a second time, also after a failed send. Reasoning is cut only when a usable capture of the route shows that the route sends it: other reasoning (of an earlier route after a model switch, for example) does not go to the fallback model or into the fixed summary. The latest user request in front of the tail gets the room that stays after the tail is cut. Hermes notifications, task lists, and recovery nudges are not user requests only when the whole text matches the Hermes template. The copied user messages get the room that stays after the tail is cut. Without a usable capture (after a restart, after a model change, or when a middleware changed the captured rows), the request overhead is unknown: the summary has no copied user messages, the tail takes at most half of the free room, the check before a request compacts when the history estimate reaches half of the window less the reply reserve, and the reply reserve is a quarter of the window (at most 65,536 and at least 4,096 tokens). The same reserve applies when the captured request had no positive `max_tokens` or `max_completion_tokens`: the provider default is not known. The host token count is not used for the overhead: it does not use the plugin tokenizer.
- The warm request is not streamed and not retried. Its time limit is 120 seconds. The fallback request also has a 120-second limit.
- A request with `extra_headers` or `extra_query`, `n` above 1, a forced `tool_choice`, `response_format`, or audio settings uses the fallback (`settings_unsupported`).
- After `/compress`, Hermes builds the system prompt again from its configuration. A system message that a host gave in code is not kept. This is Hermes behavior, and it is the same for the built-in compressor.
diff --git a/evidence/plugin-integration.json b/evidence/plugin-integration.json
index b7fcc2d..fb8cf59 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-06T17:53:15Z",
+ "started_utc": "2026-10-06T18:07:55Z",
"hermes": {
"commit": "45871e100feceb89769536c88e5e6e265226a409",
"clean": true
@@ -10,7 +10,7 @@
"plugin_files_sha256": {
"warm_compaction/__init__.py": "11e038f05813627b2d1d8b82b3b775ecfe41015746c8e90ed6d452cfdaec65a6",
"warm_compaction/capture.py": "387a5bda2c97530c6e9114e810ac062199e3731c12a582d75598d2bdb95e8c57",
- "warm_compaction/engine.py": "3e57c909033e7771eab5ca2a56d9632f0148c9bf1534df5ba04e0721d8fd8fce",
+ "warm_compaction/engine.py": "381dde42592b0e0f5a37c1a18ac9d19053eb0efda0901b984a3923f9bcfec14f",
"warm_compaction/fallback.py": "da48630977e42366090a203ca80937dfd83c617720b4dcba36ef2bf1fff44976",
"warm_compaction/handoff.py": "9afea62ec2f8ef46cbeefd70b1071bb6c7111fa6fd1cd073e1fb2c7eae30f3c2",
"warm_compaction/layout.py": "88a8b50e59c2f9468e238aea6a6f3dedccd62175c303e5df1a11de43b6d94bd4",
@@ -76,11 +76,11 @@
"threshold_tokens": 100000,
"context_length": 200000
},
- "compress_seconds": 0.106,
+ "compress_seconds": 0.121,
"warm_last": {
"path": "warm",
"reason": "accepted",
- "elapsed_s": 0.025,
+ "elapsed_s": 0.032,
"prompt_tokens": 33892,
"cached_tokens": 16946
},
@@ -189,7 +189,7 @@
"install",
"run"
],
- "process_seconds": 56.235
+ "process_seconds": 63.904
},
{
"scenario": "manual_fallback",
@@ -248,7 +248,7 @@
"threshold_tokens": 100000,
"context_length": 200000
},
- "compress_seconds": 0.254,
+ "compress_seconds": 0.279,
"warm_last": {
"path": "fallback",
"reason": "provider_error",
@@ -361,7 +361,7 @@
"install",
"run"
],
- "process_seconds": 35.008
+ "process_seconds": 40.224
},
{
"scenario": "manual_fixed",
@@ -419,11 +419,11 @@
"threshold_tokens": 100000,
"context_length": 200000
},
- "compress_seconds": 0.229,
+ "compress_seconds": 0.298,
"warm_last": {
"path": "fixed",
"reason": "provider_error",
- "elapsed_s": 0.11,
+ "elapsed_s": 0.141,
"prompt_tokens": null,
"cached_tokens": null
},
@@ -532,7 +532,7 @@
"install",
"run"
],
- "process_seconds": 32.142
+ "process_seconds": 36.662
},
{
"scenario": "auto_tool_loop",
@@ -620,14 +620,14 @@
"warm_last_1": {
"path": "warm",
"reason": "accepted",
- "elapsed_s": 0.016,
+ "elapsed_s": 0.031,
"prompt_tokens": 35209,
"cached_tokens": 17604
},
"warm_last_2": {
"path": "warm",
"reason": "accepted",
- "elapsed_s": 0.0,
+ "elapsed_s": 0.015,
"prompt_tokens": 19504,
"cached_tokens": 9752
},
@@ -737,7 +737,7 @@
"install",
"run"
],
- "process_seconds": 33.21
+ "process_seconds": 39.658
},
{
"scenario": "rollback",
@@ -787,11 +787,11 @@
"threshold_tokens": 100000,
"context_length": 200000
},
- "compress_seconds": 0.168,
+ "compress_seconds": 0.191,
"warm_last": {
"path": "warm",
"reason": "accepted",
- "elapsed_s": 0.016,
+ "elapsed_s": 0.032,
"prompt_tokens": 33882,
"cached_tokens": 16941
},
@@ -973,10 +973,10 @@
"run",
"rollback"
],
- "process_seconds": 38.681
+ "process_seconds": 45.41
}
],
- "finished_utc": "2026-10-06T17:56:31Z",
+ "finished_utc": "2026-10-06T18:11:41Z",
"passed": true,
"run_folder": "outside repository"
}
diff --git a/tests/test_wc_engine.py b/tests/test_wc_engine.py
index 5b96d3e..296a797 100644
--- a/tests/test_wc_engine.py
+++ b/tests/test_wc_engine.py
@@ -444,6 +444,22 @@ class EngineTest(unittest.TestCase):
self.assertNotIn("MID-REQ", prepended["content"])
self.assertTrue(any("MID-REQ" in str(row.get("content")) for row in new if row.get("_compressed_summary")))
+ def test_the_quote_after_a_fallback_summary_stays_in_the_summary_budget(self):
+ # A fallback summary near the reserve: the quote of the cut request takes only what is left, so the tail
+ # cap does not go below the cap that the fallback transcript had.
+ from warm_compaction.engine import SUMMARY_RESERVE
+ from warm_compaction.rows import estimate_tokens
+ self.llm = FakeLlm(text=HEADINGS_TEXT.replace("Finish the test task.", "word " * 3_000) + "\n" + END_LINE)
+ rows = [*old_turns(4), user("BIG start " + "q" * 3_000 + " MID-REQ " + "q" * 37_000 + " big end"),
+ assistant("", [("c1", "read", "{}")]), tool("c1", "r1"), assistant("done")]
+ engine = self.make(threshold=0.95, tail_tokens=2_000, warm=False)
+ engine.update_model(model=ROUTE[0], context_length=64_000, base_url=ROUTE[1], api_key="k",
+ provider="custom", api_mode=ROUTE[2])
+ new = engine.compress(rows)
+ self.assertEqual(engine.warm_last["path"], "fallback")
+ summary = next(row["content"] for row in new if row.get("_compressed_summary") and row["role"] == "assistant")
+ self.assertLessEqual(estimate_tokens(summary), SUMMARY_RESERVE + 100)
+
def test_an_unusable_capture_does_not_set_the_reasoning_rule(self):
# The capture is of this route, but its rows are not the stored rows: it does not show what the next
# request replays. The stored rows with reasoning_content do.
diff --git a/warm_compaction/engine.py b/warm_compaction/engine.py
index 615345c..3b1ad76 100644
--- a/warm_compaction/engine.py
+++ b/warm_compaction/engine.py
@@ -304,9 +304,13 @@ class WarmCompactionEngine(ContextEngine):
prepend = layout.fit_user_row(prepend, allowed)
elif record["path"] == "fallback":
# The fallback transcript had only the start and end of a long row: the middle that this cut
- # removes goes after the summary as a quote. The room keeps space for that quote.
- prepend = layout.fit_user_row(prepend, allowed - fallback.CUT_QUOTE_CHARS // 4, cut)
- block = fallback.cut_quote(cut, fallback.CUT_QUOTE_CHARS // 4)
+ # removes goes after the summary as a quote. The quote takes only what the summary budget has
+ # left (the tail cap stays at least the cap of the fallback transcript); the room keeps space
+ # for it.
+ left = max(0, min(fallback.CUT_QUOTE_CHARS // 4,
+ self._fixed_budget(overhead, reserve) - estimate_tokens(summary) - 4))
+ prepend = layout.fit_user_row(prepend, allowed - left, cut)
+ block = fallback.cut_quote(cut, left)
if block:
summary = summary.rstrip() + "\n\n" + block
else: