Skip to content

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

Trigger preflight compaction conservatively without a usable capture

Elevatormusic2af8270753d8
diff --git a/README.md b/README.md
index 86f38a0..d7df897 100644
--- a/README.md
+++ b/README.md
@@ -199,7 +199,7 @@ The plugin was faster than both other engines in 10 of 10 cases. hermes-lcm and
- The warm request uses its own HTTP connection. It sends the route URL (with its query, for example an Azure `api-version`), the route headers, and the TLS settings, but not the cookies of the Hermes client. A gateway that needs a cookie from an earlier response refuses the warm request, and the fallback summary runs (`provider_error`).
- A model or session switch, or a host timeout, during the attempt stops the warm request before it is sent (`route_changed` or `cancelled`).
- 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`. 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 a short text. 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). 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, and the reply reserve is a quarter of the window (at most 65,536 and at least 4,096 tokens). 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`. 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 a short text. 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). 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 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 2328f51..3737e9f 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-06T13:55:14Z",
+ "started_utc": "2026-10-06T14:08:25Z",
"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": "d48df1522b0155236fb1018373608ff179aa606e5a30157c5f1f759b8b7c4054",
+ "warm_compaction/engine.py": "44a21ca2c779d0e404dc70feaf57fcaa28d36ff5b741cfe5094f056f9704e961",
"warm_compaction/fallback.py": "f4cf08186f38fc9a46f47af2bedf22584b6ede4f11de1c3abd0fd598aec2f8a4",
"warm_compaction/handoff.py": "9afea62ec2f8ef46cbeefd70b1071bb6c7111fa6fd1cd073e1fb2c7eae30f3c2",
"warm_compaction/layout.py": "2ade107c1b6fc4f4d1bc35e0118c17184d8fe241484aad4b36d03800acb965c7",
@@ -76,11 +76,11 @@
"threshold_tokens": 100000,
"context_length": 200000
},
- "compress_seconds": 0.106,
+ "compress_seconds": 0.108,
"warm_last": {
"path": "warm",
"reason": "accepted",
- "elapsed_s": 0.023,
+ "elapsed_s": 0.03,
"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": 57.073
+ "process_seconds": 53.422
},
{
"scenario": "manual_fallback",
@@ -246,11 +248,11 @@
"threshold_tokens": 100000,
"context_length": 200000
},
- "compress_seconds": 0.228,
+ "compress_seconds": 0.231,
"warm_last": {
"path": "fallback",
"reason": "provider_error",
- "elapsed_s": 0.11,
+ "elapsed_s": 0.094,
"prompt_tokens": null,
"cached_tokens": null
},
@@ -359,7 +361,7 @@
"install",
"run"
],
- "process_seconds": 34.653
+ "process_seconds": 40.886
},
{
"scenario": "manual_fixed",
@@ -417,11 +419,11 @@
"threshold_tokens": 100000,
"context_length": 200000
},
- "compress_seconds": 0.276,
+ "compress_seconds": 0.221,
"warm_last": {
"path": "fixed",
"reason": "provider_error",
- "elapsed_s": 0.14,
+ "elapsed_s": 0.11,
"prompt_tokens": null,
"cached_tokens": null
},
@@ -530,7 +532,7 @@
"install",
"run"
],
- "process_seconds": 31.374
+ "process_seconds": 29.409
},
{
"scenario": "auto_tool_loop",
@@ -625,7 +627,7 @@
"warm_last_2": {
"path": "warm",
"reason": "accepted",
- "elapsed_s": 0.016,
+ "elapsed_s": 0.0,
"prompt_tokens": 18436,
"cached_tokens": 9218
},
@@ -735,7 +737,7 @@
"install",
"run"
],
- "process_seconds": 36.624
+ "process_seconds": 34.311
},
{
"scenario": "rollback",
@@ -785,11 +787,11 @@
"threshold_tokens": 100000,
"context_length": 200000
},
- "compress_seconds": 0.159,
+ "compress_seconds": 0.142,
"warm_last": {
"path": "warm",
"reason": "accepted",
- "elapsed_s": 0.016,
+ "elapsed_s": 0.015,
"prompt_tokens": 33882,
"cached_tokens": 16941
},
@@ -948,7 +950,7 @@
"stored": 30
},
"fence_denied": {
- "child_process_blocked": 6,
+ "child_process_blocked": 5,
"network_host_blocked": 1
},
"fence_blocked_programs": [
@@ -971,10 +973,10 @@
"run",
"rollback"
],
- "process_seconds": 41.056
+ "process_seconds": 37.639
}
],
- "finished_utc": "2026-10-06T13:58:35Z",
+ "finished_utc": "2026-10-06T14:11:40Z",
"passed": true,
"run_folder": "outside repository"
}
diff --git a/tests/test_wc_engine.py b/tests/test_wc_engine.py
index 0024d9e..8006848 100644
--- a/tests/test_wc_engine.py
+++ b/tests/test_wc_engine.py
@@ -255,6 +255,24 @@ class EngineTest(unittest.TestCase):
self.assertTrue(engine.should_compress_preflight(messages))
self.assertFalse(self.make(threshold=0.95).should_compress_preflight([user("hi")]))
+ def test_preflight_without_a_usable_capture_keeps_half_of_the_room_for_the_overhead(self):
+ # After a restart the system prompt and tool schemas are unknown: as in compress, half of the room
+ # (the window less the unknown reply reserve) is for them.
+ from warm_compaction.engine import request_reserve
+ from warm_compaction.rows import estimate_tokens
+ engine = self.make(threshold=0.95)
+ engine.update_model(model=ROUTE[0], context_length=64_000, base_url=ROUTE[1], api_key="k",
+ provider="custom", api_mode=ROUTE[2])
+ half = (64_000 - request_reserve(None, 64_000)) // 2
+ rows = old_turns(26)
+ self.assertGreaterEqual(estimate_tokens(rows), half)
+ self.assertLess(estimate_tokens(rows), engine.threshold_tokens)
+ self.assertTrue(engine.should_compress_preflight(rows))
+ self.assertFalse(engine.should_compress_preflight(old_turns(4)))
+ # A usable capture with a small overhead: the threshold applies.
+ self.seed(rows[:-1], rows[-1])
+ self.assertFalse(engine.should_compress_preflight([*rows, user("next")]))
+
def test_a_second_send_is_refused_before_it_sends(self):
def twice(request=None, next_call=None, **context):
first = next_call()
diff --git a/warm_compaction/engine.py b/warm_compaction/engine.py
index 8459045..0aec91e 100644
--- a/warm_compaction/engine.py
+++ b/warm_compaction/engine.py
@@ -177,16 +177,20 @@ class WarmCompactionEngine(ContextEngine):
def should_compress_preflight(self, messages: list) -> bool:
"""The history estimate, with the system rows and tool schemas of a usable capture, reaches the threshold;
- or with the reply reserve it does not fit in the window."""
+ or with the reply reserve it does not fit in the window. Without a usable capture (after a restart, for
+ example) the overhead is unknown: as in compress, half of the room (the window less the reply reserve) is
+ for the system rows and the tool schemas."""
if self.threshold_tokens <= 0:
return False
tokens = estimate_tokens(messages)
+ window = int(self.context_length or 0)
budget = self._budget_capture(self._store.latest(self._wc_session_id), messages)
if budget is not None:
tokens += request_overhead(budget, messages) or 0
- window = int(self.context_length or 0)
if window > 0 and tokens + request_reserve(budget, window) > window:
return True
+ elif window > 0 and tokens >= (window - request_reserve(None, window)) // 2:
+ return True
return tokens >= self.threshold_tokens
def has_content_to_compress(self, messages: list) -> bool: