Skip to content

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

Give the handoff its own reply limit, and treat an unmeasured overhead as unknown

Elevatormusic8c58f0b031d6
diff --git a/README.md b/README.md
index 7171240..0b3d35a 100644
--- a/README.md
+++ b/README.md
@@ -112,7 +112,7 @@ plugins:
threshold: 0.5
```
-The warm request sends the whole earlier request again, plus the new rows and the reply reserve. Thus it must fit in the context window. At the default threshold, about half of the window stays free for it. If the request does not fit, the plugin uses the fallback summary (refusal code `capacity`). The plugin uses the prompt token count that the server reported for the earlier request, and estimates only the new rows.
+The warm request sends the whole earlier request again, plus the new rows and the reply reserve. The reply limit of the warm request is the `max_tokens` or `max_completion_tokens` value of the earlier request, kept between 2,048 and 8,192 tokens. Thus it must fit in the context window. At the default threshold, about half of the window stays free for it. If the request does not fit, the plugin uses the fallback summary (refusal code `capacity`). The plugin uses the prompt token count that the server reported for the earlier request, and estimates only the new rows.
## Fallback model and keys
diff --git a/evidence/plugin-integration.json b/evidence/plugin-integration.json
index c7c512a..56ab30a 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-06T10:34:19Z",
+ "started_utc": "2026-10-06T10:45:47Z",
"hermes": {
"commit": "45871e100feceb89769536c88e5e6e265226a409",
"clean": true
@@ -10,20 +10,20 @@
"plugin_files_sha256": {
"warm_compaction/__init__.py": "11e038f05813627b2d1d8b82b3b775ecfe41015746c8e90ed6d452cfdaec65a6",
"warm_compaction/capture.py": "387a5bda2c97530c6e9114e810ac062199e3731c12a582d75598d2bdb95e8c57",
- "warm_compaction/engine.py": "e0778987d455048b8aa31e36dfd9308b89d54ae756fb66ec944271863fb5af0c",
+ "warm_compaction/engine.py": "db2a26b90325105cac5ccae248a831faa8c979c474e826cb30675cc2ab3c93d9",
"warm_compaction/fallback.py": "fe6bfbd18359b84a9ebc0519b73a865fa5736a62f87e06f83535968b49317972",
"warm_compaction/handoff.py": "3ce85d76fce4afbefec0d9c2e91b100e09ac677a28e50ea749f4d25533d68c7e",
"warm_compaction/layout.py": "5e730211647eed0beec1e6819dfc008b0855a0866dc920bb0cf8d6d8ab2dfa92",
"warm_compaction/plugin.yaml": "d67560fd4625ec80d9e26f3e6d712763e77474f68d469c7e48f09fd3506ccc71",
"warm_compaction/rows.py": "cc2a8a3d0561559f24fc40881453441d938ea9d7803e1df81a107a5e05e5f00a",
- "warm_compaction/warm.py": "1ceb52115da6186dccd9b6000224940a119b788858721bb2c144cb5e3ff20f18"
+ "warm_compaction/warm.py": "1a8c12dabf17a1de36ba5f891f0c58230b7752aca3dee6aea10f7d41d864147e"
},
"privacy": "metadata only: names, counts, codes, booleans, and times; no message text",
"scenarios": [
{
"scenario": "enable_manual_warm",
"python": "3.14.7",
- "status": "passed",
+ "status": "failed",
"checks": {
"listed_in_engine_picker": true,
"engine_selected": true,
@@ -37,7 +37,7 @@
"warm_new_rows": true,
"warm_instruction_last": true,
"warm_not_streamed": true,
- "warm_settings_kept": true,
+ "warm_settings_kept": false,
"handoff_in_summary": true,
"no_fallback_request": true,
"history_saved": true,
@@ -75,11 +75,11 @@
"threshold_tokens": 100000,
"context_length": 200000
},
- "compress_seconds": 0.101,
+ "compress_seconds": 0.1,
"warm_last": {
"path": "warm",
"reason": "accepted",
- "elapsed_s": 0.023,
+ "elapsed_s": 0.022,
"prompt_tokens": 33883,
"cached_tokens": 16941
},
@@ -87,10 +87,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"
@@ -99,6 +100,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"
@@ -186,7 +188,7 @@
"install",
"run"
],
- "process_seconds": 56.287
+ "process_seconds": 54.278
},
{
"scenario": "manual_fallback",
@@ -245,11 +247,11 @@
"threshold_tokens": 100000,
"context_length": 200000
},
- "compress_seconds": 0.239,
+ "compress_seconds": 0.325,
"warm_last": {
"path": "fallback",
"reason": "provider_error",
- "elapsed_s": 0.11,
+ "elapsed_s": 0.141,
"prompt_tokens": null,
"cached_tokens": null
},
@@ -358,7 +360,7 @@
"install",
"run"
],
- "process_seconds": 35.614
+ "process_seconds": 33.415
},
{
"scenario": "manual_fixed",
@@ -416,11 +418,11 @@
"threshold_tokens": 100000,
"context_length": 200000
},
- "compress_seconds": 0.291,
+ "compress_seconds": 0.236,
"warm_last": {
"path": "fixed",
"reason": "provider_error",
- "elapsed_s": 0.141,
+ "elapsed_s": 0.094,
"prompt_tokens": null,
"cached_tokens": null
},
@@ -529,12 +531,12 @@
"install",
"run"
],
- "process_seconds": 32.777
+ "process_seconds": 34.263
},
{
"scenario": "auto_tool_loop",
"python": "3.11.14",
- "status": "passed",
+ "status": "failed",
"checks": {
"listed_in_engine_picker": true,
"engine_selected": true,
@@ -546,7 +548,7 @@
"auto1_new_rows": true,
"auto1_instruction_last": true,
"auto1_not_streamed": true,
- "auto1_settings_kept": true,
+ "auto1_settings_kept": false,
"auto1_tool_rows_sent": true,
"first_summary_rows": true,
"wait_flag_cleared": true,
@@ -557,7 +559,7 @@
"auto2_new_rows": true,
"auto2_instruction_last": true,
"auto2_not_streamed": true,
- "auto2_settings_kept": true,
+ "auto2_settings_kept": false,
"auto2_tool_rows_sent": true,
"no_fallback_request": true,
"plugin_made_no_blocked_call": true
@@ -615,14 +617,14 @@
"warm_last_1": {
"path": "warm",
"reason": "accepted",
- "elapsed_s": 0.016,
+ "elapsed_s": 0.0,
"prompt_tokens": 35201,
"cached_tokens": 17600
},
"warm_last_2": {
"path": "warm",
"reason": "accepted",
- "elapsed_s": 0.016,
+ "elapsed_s": 0.0,
"prompt_tokens": 18427,
"cached_tokens": 9213
},
@@ -732,7 +734,7 @@
"install",
"run"
],
- "process_seconds": 34.717
+ "process_seconds": 34.129
},
{
"scenario": "rollback",
@@ -782,7 +784,7 @@
"threshold_tokens": 100000,
"context_length": 200000
},
- "compress_seconds": 0.149,
+ "compress_seconds": 0.182,
"warm_last": {
"path": "warm",
"reason": "accepted",
@@ -968,10 +970,10 @@
"run",
"rollback"
],
- "process_seconds": 39.397
+ "process_seconds": 46.293
}
],
- "finished_utc": "2026-10-06T10:37:38Z",
- "passed": true,
+ "finished_utc": "2026-10-06T10:49:09Z",
+ "passed": false,
"run_folder": "outside repository"
}
diff --git a/tests/test_wc_engine.py b/tests/test_wc_engine.py
index cc1f0d7..6818b0b 100644
--- a/tests/test_wc_engine.py
+++ b/tests/test_wc_engine.py
@@ -357,7 +357,9 @@ class EngineTest(unittest.TestCase):
self.assertIsNone(request_overhead(None))
self.assertIsNone(request_overhead({"body": None}, rows, None))
self.assertEqual(request_overhead(None, rows, estimate_tokens(rows) + 45_000), 45_000)
- self.assertEqual(request_overhead(None, rows, 10), 0)
+ # An estimate that takes all of the measured count is not a measurement of the overhead.
+ self.assertIsNone(request_overhead(None, rows, 10))
+ self.assertIsNone(request_overhead(None, rows, estimate_tokens(rows)))
def test_without_a_capture_the_copies_leave_room_for_the_system_prompt_and_tools(self):
# After a restart there is no capture. The system prompt and the tool schemas still take their space.
@@ -366,8 +368,8 @@ class EngineTest(unittest.TestCase):
engine = self.make(threshold=0.95, tail_tokens=2_000)
engine.update_model(model=ROUTE[0], context_length=64_000, base_url=ROUTE[1], api_key="k",
provider="custom", api_mode=ROUTE[2])
- for current, copied in ((estimate_tokens(rows), True), (estimate_tokens(rows) + 60_000, False),
- (None, False)):
+ for current, copied in ((estimate_tokens(rows) + 1_000, True), (estimate_tokens(rows) + 60_000, False),
+ (estimate_tokens(rows) // 2, False), (None, False)):
with self.subTest(current=current):
new = engine.compress(rows, current_tokens=current)
summary = next(row["content"] for row in new if "## Copied user messages" in str(row["content"]))
diff --git a/tests/test_wc_warm.py b/tests/test_wc_warm.py
index 4e9b80e..17a182d 100644
--- a/tests/test_wc_warm.py
+++ b/tests/test_wc_warm.py
@@ -7,7 +7,7 @@ from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from warm_compaction.rows import estimate_tokens
from warm_compaction.warm import (
- DEFAULT_RESERVE, SAFETY, WarmRefusal, build_request, check_settings, fits, send, split_history, urllib_post,
+ DEFAULT_RESERVE, HANDOFF_MAX_TOKENS, HANDOFF_MIN_TOKENS, SAFETY, WarmRefusal, build_request, check_settings, fits, send, split_history, urllib_post,
wire_row,
)
from wc_fixtures import ROUTE, assistant, capture_for, tool, user
@@ -187,6 +187,24 @@ class BuildRequestTest(unittest.TestCase):
self.assertNotIn("stop", body)
self.assertEqual(body["temperature"], 0.2)
+ def test_the_handoff_has_its_own_reply_limit(self):
+ # The reply limit of the main request is for another task: a small one cuts the handoff, a large one
+ # reserves space that the handoff does not need.
+ cases = (({"max_tokens": 128}, {"max_tokens": HANDOFF_MIN_TOKENS}),
+ ({"max_tokens": 3_000}, {"max_tokens": 3_000}),
+ ({"max_tokens": 60_000}, {"max_tokens": HANDOFF_MAX_TOKENS}),
+ ({"max_completion_tokens": 500}, {"max_completion_tokens": HANDOFF_MIN_TOKENS}),
+ ({}, {}))
+ for extra, expected in cases:
+ with self.subTest(extra=extra):
+ body = self.build(capture_for(self.rows, self.reply, body_extra=extra))
+ self.assertEqual({key: body[key] for key in ("max_tokens", "max_completion_tokens") if key in body},
+ expected)
+
+ def test_a_large_main_reply_limit_does_not_refuse_a_handoff_that_fits(self):
+ capture = capture_for(self.rows, self.reply, body_extra={"max_tokens": 60_000})
+ self.build(capture, context_length=20_000)
+
def test_sends_every_trailing_user_row(self):
# The tail can keep only the newest of several user rows. The handoff must see the older ones too.
self.messages = [*self.messages, user("u4 " + "x" * 5_000)]
diff --git a/warm_compaction/engine.py b/warm_compaction/engine.py
index 7427f0a..4dff304 100644
--- a/warm_compaction/engine.py
+++ b/warm_compaction/engine.py
@@ -85,7 +85,10 @@ def request_overhead(capture: dict[str, Any] | None, messages: list | None = Non
if not isinstance(body, dict) or not isinstance(body.get("messages"), list):
if messages is None or type(current_tokens) is not int or current_tokens <= 0:
return None
- return max(0, current_tokens - estimate_tokens(messages))
+ # The estimate and the server count do not use the same tokenizer. An estimate that takes all of the
+ # count (compressible text, for example) says nothing about the overhead.
+ overhead = current_tokens - estimate_tokens(messages)
+ return overhead if overhead > 0 else None
count = max(len(body["messages"]) - len(capture.get("digests") or ()), 0)
return estimate_tokens({"messages": body["messages"][:count], "tools": body.get("tools")})
diff --git a/warm_compaction/warm.py b/warm_compaction/warm.py
index 5407fb9..94bdf30 100644
--- a/warm_compaction/warm.py
+++ b/warm_compaction/warm.py
@@ -17,6 +17,10 @@ from .rows import (
)
DEFAULT_RESERVE = 4096
+# The reply limit of the handoff. The instruction asks for at most 600 words; the upper limit leaves room for a
+# thinking model that counts its reasoning in the same limit.
+HANDOFF_MIN_TOKENS = 2048
+HANDOFF_MAX_TOKENS = 8192
TIMEOUT_S = 120.0
SAFETY = 1.1
SYSTEM_ROLES = ("system", "developer")
@@ -309,6 +313,12 @@ def build_request(capture: dict[str, Any], messages: list, route: tuple, context
request.pop("stream_options", None)
# A stop sequence of the main request could cut the handoff after the five headings.
request.pop("stop", None)
+ # The reply limit of the main request is for another task: a small one cuts the handoff, a large one reserves
+ # space that the handoff does not need. Keep the field that the route uses.
+ for key in ("max_tokens", "max_completion_tokens"):
+ value = body.get(key)
+ if type(value) is int and value > 0:
+ request[key] = min(max(value, HANDOFF_MIN_TOKENS), HANDOFF_MAX_TOKENS)
if not fits(request, context_length, capture.get("prompt_tokens"), len(body["messages"])):
raise WarmRefusal("capacity")
return request