Skip to content

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

Refuse a capture that a later execution middleware can change, and keep the handoff instruction

Elevatormusic0e879b92b7df
diff --git a/README.md b/README.md
index 4651d76..4537919 100644
--- a/README.md
+++ b/README.md
@@ -138,7 +138,7 @@ warm_compaction: path=warm reason=accepted elapsed_s=10.656 prompt_tokens=108021
```
- `path` is `warm`, `fallback`, or `fixed`.
-- `reason` is `accepted` or the refusal code of the warm request: `disabled`, `no_capture`, `api_mode_unsupported`, `route_changed`, `settings_unsupported`, `source_transform_unsupported`, `history_changed`, `capacity`, `cancelled`, `middleware_unavailable`, `middleware_refused`, `middleware_rewrite`, `middleware_repeated`, `middleware_changed_reply`, `provider_error`, `timeout`, `incomplete_response`, or `gate:<reason>`. The warm request goes through the Hermes `llm_request` and `llm_execution` middleware, as a main request does.
+- `reason` is `accepted` or the refusal code of the warm request: `disabled`, `no_capture`, `api_mode_unsupported`, `route_changed`, `settings_unsupported`, `source_transform_unsupported`, `history_changed`, `capacity`, `cancelled`, `middleware_unavailable`, `middleware_refused`, `middleware_rewrite`, `middleware_repeated`, `middleware_after_capture`, `middleware_changed_reply`, `provider_error`, `timeout`, `incomplete_response`, or `gate:<reason>`. The warm request goes through the Hermes `llm_request` and `llm_execution` middleware, as a main request does.
- `cached_tokens` is `None` when the server does not report it. Then the cache reuse is unknown.
The engine status (`get_status()`) has the same values in `warm_last`. The log never contains message text, request bodies, or keys.
diff --git a/evidence/plugin-integration.json b/evidence/plugin-integration.json
index e7f4257..e3a24e3 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-06T09:31:05Z",
+ "started_utc": "2026-10-06T09:47:38Z",
"hermes": {
"commit": "45871e100feceb89769536c88e5e6e265226a409",
"clean": true
@@ -9,14 +9,14 @@
"script_sha256": "85397c1e4879f7cf21d3b2619baecf72fb67eb27e69376f428d0426ee69ab3d1",
"plugin_files_sha256": {
"warm_compaction/__init__.py": "11e038f05813627b2d1d8b82b3b775ecfe41015746c8e90ed6d452cfdaec65a6",
- "warm_compaction/capture.py": "9d6414ec2dc552a01d7ba0ab84009ee30a363d57b9a061c2b5d9bc699c0efb75",
- "warm_compaction/engine.py": "b1bfd993e7e33f4760a9a49c28987bf877d5c4a507a2b1520aeaf33e1f62f088",
+ "warm_compaction/capture.py": "9f195ff31821b1dfc486373b074b799f4bc8750b07d10b53073374f760b2f2eb",
+ "warm_compaction/engine.py": "dfa9d6fb060d4d7665f0467630086ecfd076742e097258415c47bb5084134888",
"warm_compaction/fallback.py": "b5ccd56550381fc8033351156cbf2dcf0a7e667f8f9bbf0d15bb2101f3abb97e",
"warm_compaction/handoff.py": "a88b580a909ea832b53d6e2be58fa213ce621bd486df7a43d0b835ac9ccb8563",
"warm_compaction/layout.py": "84673bd98710416599b3718071dcc24e0c230e4845cdbffb47fd743b2a21f423",
"warm_compaction/plugin.yaml": "d67560fd4625ec80d9e26f3e6d712763e77474f68d469c7e48f09fd3506ccc71",
"warm_compaction/rows.py": "cc2a8a3d0561559f24fc40881453441d938ea9d7803e1df81a107a5e05e5f00a",
- "warm_compaction/warm.py": "f765fb622ac9b6558eafc9b14179abffbd862a9fa42e542a2908ec2a862e8019"
+ "warm_compaction/warm.py": "b429103b13301e2eb82fb9c7bae422d28be5049114e27d71c5586762856b8c03"
},
"privacy": "metadata only: names, counts, codes, booleans, and times; no message text",
"scenarios": [
@@ -75,11 +75,11 @@
"threshold_tokens": 100000,
"context_length": 200000
},
- "compress_seconds": 0.167,
+ "compress_seconds": 0.094,
"warm_last": {
"path": "warm",
"reason": "accepted",
- "elapsed_s": 0.022,
+ "elapsed_s": 0.018,
"prompt_tokens": 33883,
"cached_tokens": 16941
},
@@ -87,7 +87,8 @@
"final_session_id": "wc-enable_manual_warm",
"continuation_keeps_custom_system_text": false,
"fence_denied": {
- "child_process_blocked": 4
+ "child_process_blocked": 4,
+ "network_host_blocked": 1
},
"fence_blocked_programs": [
"git",
@@ -95,11 +96,14 @@
"python",
"python3"
],
- "fence_blocked_hosts": [],
+ "fence_blocked_hosts": [
+ "hermes-assets.nousresearch.com"
+ ],
"fence_blocked_callers": [
- "child_process_blocked from hermes_cli/source_check.py:_git_run < hermes_cli/source_check.py:_branch_tip < hermes_cli/source_check.py:_check_branch",
+ "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"
+ "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"
],
"fence_socketpairs": 1,
"install": {
@@ -184,7 +188,7 @@
"install",
"run"
],
- "process_seconds": 65.674
+ "process_seconds": 54.245
},
{
"scenario": "manual_fallback",
@@ -243,11 +247,11 @@
"threshold_tokens": 100000,
"context_length": 200000
},
- "compress_seconds": 0.332,
+ "compress_seconds": 0.217,
"warm_last": {
"path": "fallback",
"reason": "provider_error",
- "elapsed_s": 0.203,
+ "elapsed_s": 0.094,
"prompt_tokens": null,
"cached_tokens": null
},
@@ -356,7 +360,7 @@
"install",
"run"
],
- "process_seconds": 33.65
+ "process_seconds": 32.083
},
{
"scenario": "manual_fixed",
@@ -414,11 +418,11 @@
"threshold_tokens": 100000,
"context_length": 200000
},
- "compress_seconds": 0.227,
+ "compress_seconds": 0.235,
"warm_last": {
"path": "fixed",
"reason": "provider_error",
- "elapsed_s": 0.109,
+ "elapsed_s": 0.11,
"prompt_tokens": null,
"cached_tokens": null
},
@@ -527,7 +531,7 @@
"install",
"run"
],
- "process_seconds": 29.985
+ "process_seconds": 29.805
},
{
"scenario": "auto_tool_loop",
@@ -613,14 +617,14 @@
"warm_last_1": {
"path": "warm",
"reason": "accepted",
- "elapsed_s": 0.0,
+ "elapsed_s": 0.016,
"prompt_tokens": 35201,
"cached_tokens": 17600
},
"warm_last_2": {
"path": "warm",
"reason": "accepted",
- "elapsed_s": 0.016,
+ "elapsed_s": 0.0,
"prompt_tokens": 18206,
"cached_tokens": 9103
},
@@ -730,7 +734,7 @@
"install",
"run"
],
- "process_seconds": 33.244
+ "process_seconds": 33.575
},
{
"scenario": "rollback",
@@ -780,11 +784,11 @@
"threshold_tokens": 100000,
"context_length": 200000
},
- "compress_seconds": 0.15,
+ "compress_seconds": 0.257,
"warm_last": {
"path": "warm",
"reason": "accepted",
- "elapsed_s": 0.015,
+ "elapsed_s": 0.016,
"prompt_tokens": 33873,
"cached_tokens": 16936
},
@@ -792,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": [
@@ -943,7 +947,8 @@
"stored": 30
},
"fence_denied": {
- "child_process_blocked": 5
+ "child_process_blocked": 5,
+ "network_host_blocked": 1
},
"fence_blocked_programs": [
"git",
@@ -951,7 +956,9 @@
"python",
"python3"
],
- "fence_blocked_hosts": []
+ "fence_blocked_hosts": [
+ "hermes-assets.nousresearch.com"
+ ]
},
"exit": [
0,
@@ -963,10 +970,10 @@
"run",
"rollback"
],
- "process_seconds": 38.427
+ "process_seconds": 37.135
}
],
- "finished_utc": "2026-10-06T09:34:26Z",
+ "finished_utc": "2026-10-06T09:50:45Z",
"passed": true,
"run_folder": "outside repository"
}
diff --git a/tests/test_wc_capture.py b/tests/test_wc_capture.py
index 3a6e5e0..9a27c9a 100644
--- a/tests/test_wc_capture.py
+++ b/tests/test_wc_capture.py
@@ -78,6 +78,23 @@ class CaptureStoreTest(unittest.TestCase):
self.assertEqual(self.store.latest("s1")["reply"]["tool_calls"],
[["c1", "read"], ["c1_d2", "ls"], ["c1_d3", "cat"]])
+ def test_no_body_when_an_execution_middleware_runs_after_the_capture(self):
+ # A later middleware can change the request after the capture saw it. The capture cannot see that change.
+ import wc_hermes_stub
+ wc_hermes_stub.install(self)
+
+ def other(request=None, next_call=None, **context):
+ return next_call()
+ for chain, kept in (([self.store.on_llm_execution, other], False), ([other, self.store.on_llm_execution], True)):
+ wc_hermes_stub.PLUGINS._delivery_manager = lambda chain=chain: SimpleNamespace(
+ _middleware={"llm_execution": chain})
+ self.addCleanup(lambda: wc_hermes_stub.PLUGINS.__dict__.pop("_delivery_manager", None))
+ with self.subTest(kept=kept):
+ self.run_request()
+ capture = self.store.latest("s1")
+ self.assertEqual(capture["body"] is not None, kept)
+ self.assertEqual(capture.get("refusal"), None if kept else "middleware_after_capture")
+
def test_keeps_tool_call_ids_and_names(self):
self.run_request(finish="tool_calls", message=reply_object("", [("c1", "read")]))
self.assertEqual(self.store.latest("s1")["reply"]["tool_calls"], [["c1", "read"]])
diff --git a/tests/test_wc_engine.py b/tests/test_wc_engine.py
index d9d21ed..f7316f8 100644
--- a/tests/test_wc_engine.py
+++ b/tests/test_wc_engine.py
@@ -235,6 +235,18 @@ class EngineTest(unittest.TestCase):
self.assertEqual((self.engine.warm_last["path"], self.engine.warm_last["reason"]), ("fallback", "capacity"))
self.assertEqual(self.post.calls, [])
+ def test_a_request_middleware_that_removes_the_instruction_stops_the_warm_request(self):
+ def drop(request=None, **context):
+ return {"request": {**request, "messages": request["messages"][:-1]}}
+ wc_hermes_stub.REQUEST_MIDDLEWARE.append(drop)
+ rows = old_turns()
+ reply = assistant("final")
+ self.seed(rows, reply)
+ self.engine.compress([*rows, reply])
+ self.assertEqual((self.engine.warm_last["path"], self.engine.warm_last["reason"]),
+ ("fallback", "middleware_rewrite"))
+ self.assertEqual(self.post.calls, [])
+
def test_a_request_middleware_that_changes_the_captured_part_stops_the_warm_request(self):
# The captured body already went through the request middleware. A second pass would apply it twice.
def prepend(request=None, **context):
diff --git a/tests/test_wc_warm.py b/tests/test_wc_warm.py
index c9f2629..ed3f4ee 100644
--- a/tests/test_wc_warm.py
+++ b/tests/test_wc_warm.py
@@ -325,6 +325,12 @@ class BuildRequestTest(unittest.TestCase):
self.build(capture)
self.assertEqual(caught.exception.code, "source_transform_unsupported")
+ def test_a_capture_without_a_body_gives_its_refusal(self):
+ capture = dict(capture_for(self.rows, self.reply), body=None, refusal="middleware_after_capture")
+ with self.assertRaises(WarmRefusal) as caught:
+ self.build(capture)
+ self.assertEqual(caught.exception.code, "middleware_after_capture")
+
def test_refuses_when_the_window_is_too_small(self):
with self.assertRaises(WarmRefusal) as caught:
self.build(capture_for(self.rows, self.reply), context_length=4_096)
diff --git a/warm_compaction/capture.py b/warm_compaction/capture.py
index 6504f6b..59f16b1 100644
--- a/warm_compaction/capture.py
+++ b/warm_compaction/capture.py
@@ -74,7 +74,8 @@ class CaptureStore:
digests = [row_digest(row) for row in conversation_history]
except Exception:
return None
- entry = {"session_id": str(session_id), "route": (model, base_url, api_mode), "digests": digests, "body": None}
+ entry = {"session_id": str(session_id), "route": (model, base_url, api_mode), "digests": digests, "body": None,
+ "refusal": None}
with self._lock:
self._open[str(api_request_id)] = entry
self._open.move_to_end(str(api_request_id))
@@ -96,12 +97,32 @@ class CaptureStore:
entry = self._open.get(str(api_request_id or ""))
if entry is None or entry["route"][2] != "chat_completions":
return
+ refusal = None
try:
body = final_body(request)
except UnsupportedRequest:
body = None
+ if body is not None and self._runs_before_other_middleware():
+ # A later middleware can change the request after this capture saw it. The warm request would
+ # then send a body that the provider never received.
+ body, refusal = None, "middleware_after_capture"
with self._lock:
entry["body"] = body
+ entry["refusal"] = refusal
+
+ def _runs_before_other_middleware(self) -> bool:
+ """True when another llm_execution middleware runs after this capture. The chain order is in the
+ Hermes plugin manager; when it cannot be read, the capture is kept."""
+ try:
+ from hermes_cli.plugins import _delivery_manager
+ chain = list(_delivery_manager()._middleware.get("llm_execution", []))
+ except Exception:
+ return False
+ own = CaptureStore.on_llm_execution
+ for index, callback in enumerate(chain):
+ if getattr(callback, "__self__", None) is self and getattr(callback, "__func__", None) is own:
+ return index < len(chain) - 1
+ return False
def on_post_api_request(self, api_request_id: Any = None, session_id: Any = None, finish_reason: Any = None,
assistant_message: Any = None, usage: Any = None, **_: Any) -> None:
diff --git a/warm_compaction/engine.py b/warm_compaction/engine.py
index 2a6bc48..29ce04a 100644
--- a/warm_compaction/engine.py
+++ b/warm_compaction/engine.py
@@ -256,7 +256,9 @@ class WarmCompactionEngine(ContextEngine):
# The captured part went through the request middleware already. A middleware that changes it again (for
# example, adds a system row) would apply twice and change the cached prefix. It can change the new rows.
if ({k: v for k, v in changed.items() if k != "messages"} != {k: v for k, v in body.items() if k != "messages"}
- or changed["messages"][:captured] != body["messages"][:captured]):
+ or changed["messages"][:captured] != body["messages"][:captured]
+ # The host instruction must stay the last row: without it, the reply is not a handoff.
+ or changed["messages"][-1:] != body["messages"][-1:]):
raise warm.WarmRefusal("middleware_rewrite")
body = changed
# A request middleware can add text to the new rows. Check the size again before the request is sent.
diff --git a/warm_compaction/warm.py b/warm_compaction/warm.py
index 571f3c1..5882670 100644
--- a/warm_compaction/warm.py
+++ b/warm_compaction/warm.py
@@ -282,7 +282,7 @@ def build_request(capture: dict[str, Any], messages: list, route: tuple, context
raise WarmRefusal("route_changed")
body = capture.get("body")
if body is None:
- raise WarmRefusal("settings_unsupported")
+ raise WarmRefusal(capture.get("refusal") or "settings_unsupported")
check_settings(body)
new_rows, trailing = split_history(capture, messages)
check_source(body, messages[: len(capture["digests"])])