Skip to content

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

Count warm failures after host commit and retain gateway notices

Elevatormusicaf548e21da4e
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 4bcaf7f..9437006 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,8 +6,8 @@ All notable changes are listed here. The format follows [Keep a Changelog](https
### Added
-- A WARNING for each compaction that does not use the warm path, with its reason (Hermes copies it to `errors.log`).
-- After 3 compactions in a row without the warm path, one notice with a hint for each distinct reason, in the logs and in the status of the next automatic compaction. Later failures update the pending notice and its fixed-summary count until Hermes shows it.
+- A WARNING for each saved compaction that Hermes confirms without the warm path, with its reason (Hermes copies it to `errors.log`).
+- After 3 confirmed compactions in a row without the warm path, one notice with a hint for each distinct reason, in the logs and at the next automatic compaction when engine status is enabled. The notice has no routine progress text. Later confirmed failures update the pending notice and its fixed-summary count until Hermes shows it.
### Security
diff --git a/README.md b/README.md
index 2640790..74c2d5a 100644
--- a/README.md
+++ b/README.md
@@ -163,7 +163,7 @@ The warm request uses the key that Hermes gives to the engine. The fallback requ
## Check the result
-Hermes writes one line for each compaction to `logs/agent.log` in the Hermes home folder:
+Hermes writes one metadata line for each compaction attempt to `logs/agent.log` in the Hermes home folder:
```text
warm_compaction: path=warm reason=accepted elapsed_s=10.656 prompt_tokens=108021 cached_tokens=107968
@@ -173,7 +173,7 @@ warm_compaction: path=warm reason=accepted elapsed_s=10.656 prompt_tokens=108021
- `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_order_unknown`, `headers_unknown`, `tls_unknown`, `tls_unverified`, `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.
+The engine status (`get_status()`) has the same values for the latest attempt in `warm_last`. The log never contains message text, request bodies, or keys.
Manual `/compress` can report `No changes from compression` when all messages fit in the recent-history tail. Then the plugin has no older history to summarize. The request estimate also includes system instructions and tool definitions, which the plugin does not compress. Use `/context` to see the token counts for each part. This unchanged result does not count as a warm-path failure.
@@ -181,8 +181,8 @@ Manual `/compress` can report `No changes from compression` when all messages fi
When the warm summary cannot be used, the plugin tries the fallback summary. If that summary cannot be used, it uses the fixed summary. The plugin reports these results:
-- Each compaction without the warm path also writes a WARNING with its reason, for example `Warm compaction skipped (provider_error); used the fallback summary`. Hermes copies warnings to `logs/errors.log`.
-- After 3 compactions in a row without the warm path, the plugin writes one WARNING with a hint for each distinct reason. Hermes shows one notice with the status of the next automatic compaction, also when the compaction status is turned off. Later failures update the pending notice until Hermes shows it. For example:
+- Each saved compaction that Hermes confirms without the warm path writes a WARNING with its reason, for example `Warm compaction skipped (provider_error); used the fallback summary`. Hermes copies warnings to `logs/errors.log`.
+- After 3 confirmed compactions in a row without the warm path, the plugin writes one WARNING with a hint for each distinct reason. When automatic engine status is enabled, Hermes shows the notice at the next automatic compaction. The notice has no routine progress text, so a gateway filter for that text does not discard it. Later confirmed failures update the pending notice until Hermes shows it. For example:
```text
⚠ Warm compaction unavailable: the last 3 compactions did not use the warm summary (provider_error: the server refused the warm request (a provider error, or a gateway that needs a cookie)). Compaction continues with the fallback summary. Details: the warm_compaction lines in logs/agent.log.
@@ -190,7 +190,9 @@ When the warm summary cannot be used, the plugin tries the fallback summary. If
- When the fallback summary could not be used, the notice says how many times, and that those compactions used the fixed summary (no model, less detail).
- A warm summary can fail the handoff checks even when the server reports cached tokens. The notice describes the summary result. It does not prove a cache miss, a speed change, or that all details were kept.
-- A warm compaction or a session reset ends the streak, and the notice can show again after the next 3 failures. A cancelled attempt does not count, and `warm: false` is not a failure.
+- A confirmed warm compaction or a session reset ends the streak, and the notice can show again after the next 3 failures. A cancelled or rejected attempt does not count or clear the streak, and `warm: false` is not a failure.
+- When the engine's automatic status is disabled, warnings stay in the logs. The engine keeps a pending notice until status is enabled or a confirmed warm compaction or session reset clears it.
+- The streak belongs to one engine instance. It is not saved across a restart or shared with separate gateway manual-command engines. A host that sends no successful compaction notification has attempt metadata only.
- Manual `/compress` has no status line from the engine: after manual compactions only, the notice is in the logs.
## Results
diff --git a/evidence/plugin-integration.json b/evidence/plugin-integration.json
index 7d367ab..34b2232 100644
--- a/evidence/plugin-integration.json
+++ b/evidence/plugin-integration.json
@@ -1,16 +1,16 @@
{
"check": "standalone warm_compaction plugin on unpatched Hermes Agent",
- "started_utc": "2026-10-07T03:11:26Z",
+ "started_utc": "2026-10-07T03:43:35Z",
"hermes": {
"commit": "45871e100feceb89769536c88e5e6e265226a409",
"clean": true
},
"python": "3.11.14",
- "script_sha256": "33947de293d0167c457f2c8b967c57e4c5064a262357de4129376519b6e2f77d",
+ "script_sha256": "a6fa3470f1184720284dbdec1a5e513cb0b42b93dc4a52d4c0a56d0c2d1973ca",
"plugin_files_sha256": {
"warm_compaction/__init__.py": "760484ace80b9da77d7397828f69178905c9ac95df7b8114956bde727bd98aef",
"warm_compaction/capture.py": "b7ba65676334da63dacc5cebdd2399f46f85e35545be22fccae9443cebcf7ce0",
- "warm_compaction/engine.py": "74eb8790e7e77f0213eb7bd28256cbd6d602d30f9edc02b5737391b825b1a9f0",
+ "warm_compaction/engine.py": "56c44d835f5149a9d39ee8235f5cbc8fc778e0b3e0791198103af5edae5c6646",
"warm_compaction/fallback.py": "dc2b7fbfdee292601e52549d23d9b7224d40da1d8c06f553f3d0b8e10a23534d",
"warm_compaction/handoff.py": "fcab51def8fbcc2b1928957930c9a5c58a4ad202c949fd9f870a77f3b30276ae",
"warm_compaction/layout.py": "b49314c5236268247c337d9fae939135c97b47384300dcdfd00b0da0c040db93",
@@ -29,6 +29,9 @@
"engine_selected": true,
"seed_turn": true,
"capture_keeps_reported_prompt_tokens": true,
+ "failure_streak_waits_for_manual_commit": true,
+ "failure_streak_after_manual_commit": true,
+ "manual_commit_notification_one_time": true,
"path_and_reason": true,
"removed_rows": true,
"summary_rows": true,
@@ -76,11 +79,11 @@
"threshold_tokens": 100000,
"context_length": 200000
},
- "compress_seconds": 0.117,
+ "compress_seconds": 0.143,
"warm_last": {
"path": "warm",
"reason": "accepted",
- "elapsed_s": 0.035,
+ "elapsed_s": 0.042,
"prompt_tokens": 33855,
"cached_tokens": 16927
},
@@ -189,7 +192,7 @@
"install",
"run"
],
- "process_seconds": 67.007
+ "process_seconds": 73.363
},
{
"scenario": "manual_fallback",
@@ -200,6 +203,9 @@
"engine_selected": true,
"seed_turn": true,
"capture_keeps_reported_prompt_tokens": true,
+ "failure_streak_waits_for_manual_commit": true,
+ "failure_streak_after_manual_commit": true,
+ "manual_commit_notification_one_time": true,
"path_and_reason": true,
"removed_rows": true,
"summary_rows": true,
@@ -248,11 +254,11 @@
"threshold_tokens": 100000,
"context_length": 200000
},
- "compress_seconds": 0.326,
+ "compress_seconds": 0.301,
"warm_last": {
"path": "fallback",
"reason": "provider_error",
- "elapsed_s": 0.157,
+ "elapsed_s": 0.14,
"prompt_tokens": null,
"cached_tokens": null
},
@@ -260,7 +266,7 @@
"final_session_id": "wc-manual_fallback",
"continuation_keeps_custom_system_text": false,
"fence_denied": {
- "child_process_blocked": 5,
+ "child_process_blocked": 6,
"network_host_blocked": 1
},
"fence_blocked_programs": [
@@ -361,7 +367,7 @@
"install",
"run"
],
- "process_seconds": 43.322
+ "process_seconds": 40.173
},
{
"scenario": "manual_fixed",
@@ -372,6 +378,9 @@
"engine_selected": true,
"seed_turn": true,
"capture_keeps_reported_prompt_tokens": true,
+ "failure_streak_waits_for_manual_commit": true,
+ "failure_streak_after_manual_commit": true,
+ "manual_commit_notification_one_time": true,
"path_and_reason": true,
"removed_rows": true,
"summary_rows": true,
@@ -532,7 +541,7 @@
"install",
"run"
],
- "process_seconds": 38.463
+ "process_seconds": 41.571
},
{
"scenario": "auto_tool_loop",
@@ -620,7 +629,7 @@
"warm_last_1": {
"path": "warm",
"reason": "accepted",
- "elapsed_s": 0.015,
+ "elapsed_s": 0.032,
"prompt_tokens": 34449,
"cached_tokens": 17224
},
@@ -735,7 +744,7 @@
"install",
"run"
],
- "process_seconds": 39.776
+ "process_seconds": 43.307
},
{
"scenario": "auto_warm_failures",
@@ -744,12 +753,18 @@
"checks": {
"listed_in_engine_picker": true,
"engine_selected": true,
+ "gateway_routine_progress_disabled": true,
"turn1_final": true,
"turn2_final": true,
"turn3_final": true,
+ "disabled_host_status_is_silent": true,
+ "disabled_host_status_keeps_notice": true,
"turn4_final": true,
"three_fallbacks_then_warm": true,
"one_notice": true,
+ "one_notice_survives_gateway_filter": true,
+ "gateway_keeps_the_notice_text": true,
+ "notice_has_no_routine_progress": true,
"notice_before_the_fourth_compaction": true,
"notice_names_the_reason": true,
"notice_describes_the_summary_result": true,
@@ -757,6 +772,22 @@
"each_refusal_in_agent_log": true,
"each_refusal_in_errors_log": true,
"notice_in_errors_log": true,
+ "fresh_engine_counter_in_host_snapshot": true,
+ "boundary_probe_has_real_fallback_candidate": true,
+ "first_attempt_advances_compression_count": true,
+ "staged_failure_does_not_count": true,
+ "discarded_host_boundary_is_silent": true,
+ "discarded_failure_does_not_count": true,
+ "discarded_host_boundary_cannot_be_replayed": true,
+ "host_rollback_restores_compression_count": true,
+ "restored_candidate_cannot_count_at_stale_boundary": true,
+ "later_failure_waits_for_commit": true,
+ "later_committed_failure_counts_once": true,
+ "committed_host_boundary_cannot_be_replayed": true,
+ "replayed_host_boundary_does_not_count": true,
+ "rollback_keeps_prior_committed_streak": true,
+ "rollback_after_commit_restores_count": true,
+ "retry_after_rollback_counts_once": true,
"plugin_made_no_blocked_call": true
},
"requests": [
@@ -891,9 +922,12 @@
]
],
"notices": [
- "🗜️ Compacting context — summarizing earlier conversation so I can continue...\n⚠ Warm compaction unavailable: the last 3 compactions did not use the warm summary (provider_error: the server refused the warm request (a provider error, or a gateway that needs a cookie)). Compaction continues with the fallback summary. Details: the warm_compaction lines in logs/agent.log."
+ "⚠ Warm compaction unavailable: the last 3 compactions did not use the warm summary (provider_error: the server refused the warm request (a provider error, or a gateway that needs a cookie)). Compaction continues with the fallback summary. Details: the warm_compaction lines in logs/agent.log."
],
"status_count": 13,
+ "gateway_notices": [
+ "⚠ Warm compaction unavailable: the last 3 compactions did not use the warm summary (provider_error: the server refused the warm request (a provider error, or a gateway that needs a cookie)). Compaction continues with the fallback summary. Details: the warm_compaction lines in logs/agent.log."
+ ],
"log_counts": {
"agent_refusals": 3,
"errors_refusals": 3,
@@ -1002,7 +1036,7 @@
"install",
"run"
],
- "process_seconds": 37.811
+ "process_seconds": 45.512
},
{
"scenario": "rollback",
@@ -1013,6 +1047,9 @@
"engine_selected": true,
"seed_turn": true,
"capture_keeps_reported_prompt_tokens": true,
+ "failure_streak_waits_for_manual_commit": true,
+ "failure_streak_after_manual_commit": true,
+ "manual_commit_notification_one_time": true,
"path_and_reason": true,
"removed_rows": true,
"summary_rows": true,
@@ -1052,7 +1089,7 @@
"threshold_tokens": 100000,
"context_length": 200000
},
- "compress_seconds": 0.162,
+ "compress_seconds": 0.194,
"warm_last": {
"path": "warm",
"reason": "accepted",
@@ -1064,7 +1101,7 @@
"final_session_id": "wc-rollback",
"continuation_keeps_custom_system_text": false,
"fence_denied": {
- "child_process_blocked": 6,
+ "child_process_blocked": 5,
"network_host_blocked": 1
},
"fence_blocked_programs": [
@@ -1235,10 +1272,10 @@
"run",
"rollback"
],
- "process_seconds": 45.801
+ "process_seconds": 52.328
}
],
- "finished_utc": "2026-10-07T03:15:58Z",
+ "finished_utc": "2026-10-07T03:48:32Z",
"passed": true,
"run_folder": "outside repository"
}
diff --git a/scripts/check_plugin_hermes.py b/scripts/check_plugin_hermes.py
index a4b8705..6b9d3c1 100644
--- a/scripts/check_plugin_hermes.py
+++ b/scripts/check_plugin_hermes.py
@@ -498,7 +498,8 @@ def run_phase(spec, session_dir):
try:
# A Hermes process selects its dependency set at start, before any third-party import.
import hermes_bootstrap # noqa: F401
- merge_config(home / "config.yaml", {"model": {"base_url": server.base_url}})
+ merge_config(home / "config.yaml", {"model": {"base_url": server.base_url},
+ "compression": {"progress_notices": False}})
import platform
# Fill the platform cache before the fence: some Windows Pythons start "cmd /c ver" one time.
platform.uname()
@@ -723,8 +724,13 @@ def run_manual(agent, engine, db, history, server, result, checks, scenario, com
checks["capture_keeps_reported_prompt_tokens"] = capture.get("prompt_tokens") == len(
json.dumps(seed_request["body"]["messages"])) // 4
begin = time.perf_counter()
+ failures_before = engine._warm_failures
removed, _usage = compress(session, "")
+ checks["failure_streak_waits_for_manual_commit"] = engine._warm_failures == failures_before
finalize(agent, committed=True)
+ expected_failures = failures_before + (scenario in ("manual_fallback", "manual_fixed"))
+ checks["failure_streak_after_manual_commit"] = engine._warm_failures == expected_failures
+ checks["manual_commit_notification_one_time"] = finalize(agent, committed=True) is False
result["compress_seconds"] = round(time.perf_counter() - begin, 3)
warm_last = dict(engine.warm_last or {})
result["warm_last"] = warm_last
@@ -787,6 +793,10 @@ def run_auto(agent, engine, history, server, result, checks):
def run_auto_failures(agent, engine, history, server, result, checks, statuses, home):
"""Repeated warm refusals: each one is a WARNING in agent.log and errors.log, and after the third one the
next automatic compaction status shows one notice to the user. A warm compaction ends the streak."""
+ from agent.context_engine import automatic_compaction_status_message
+ from gateway.run import _gateway_compression_progress_notices_enabled, _prepare_gateway_status_message
+
+ checks["gateway_routine_progress_disabled"] = not _gateway_compression_progress_notices_enabled()
questions = (TOOL_QUESTION, TOOL_AGAIN_QUESTION)
outcomes, notice_turn = [], None
messages = history
@@ -797,6 +807,17 @@ def run_auto_failures(agent, engine, history, server, result, checks, statuses,
checks[f"turn{turn + 1}_final"] = reply.get("final_response") == f"Done {turn + 1}."
last = dict(engine.warm_last or {})
outcomes.append([last.get("path"), last.get("reason")])
+ if turn == 2:
+ # The host guard runs before the engine formatter. It must keep the notice for a later status.
+ pending = engine._warm_notice
+ engine.emit_automatic_compaction_status = False
+ try:
+ hidden = automatic_compaction_status_message(
+ engine, phase="compress", default_message="Compacting context")
+ checks["disabled_host_status_is_silent"] = hidden is None
+ checks["disabled_host_status_keeps_notice"] = bool(pending) and engine._warm_notice == pending
+ finally:
+ engine.emit_automatic_compaction_status = True
if any(NOTICE_MARK in message for _kind, message in statuses[before:]):
notice_turn = turn + 1
result["outcomes"] = outcomes
@@ -806,6 +827,12 @@ def run_auto_failures(agent, engine, history, server, result, checks, statuses,
result["status_count"] = len(statuses)
checks["three_fallbacks_then_warm"] = outcomes == [["fallback", "provider_error"]] * 3 + [["warm", "accepted"]]
checks["one_notice"] = len(notices) == 1
+ delivered = [_prepare_gateway_status_message("telegram", kind, message)
+ for kind, message in statuses if NOTICE_MARK in message]
+ result["gateway_notices"] = [message for message in delivered if message]
+ checks["one_notice_survives_gateway_filter"] = len(result["gateway_notices"]) == 1
+ checks["gateway_keeps_the_notice_text"] = bool(notices) and result["gateway_notices"] == notices
+ checks["notice_has_no_routine_progress"] = bool(notices) and notices[0].startswith("\u26a0 Warm compaction")
checks["notice_before_the_fourth_compaction"] = notice_turn == 4
checks["notice_names_the_reason"] = bool(notices) and (
"provider_error: the server refused the warm request" in notices[0])
@@ -823,6 +850,71 @@ def run_auto_failures(agent, engine, history, server, result, checks, statuses,
checks["each_refusal_in_agent_log"] = agent_log.count(refusal) == 3
checks["each_refusal_in_errors_log"] = errors_log.count(refusal) == 3
checks["notice_in_errors_log"] = errors_log.count("Warm compaction failed 3 times in a row") == 1
+ check_host_commit_boundaries(engine, checks)
+
+
+def check_host_commit_boundaries(engine, checks):
+ """Run staged, discarded, and committed synthetic summaries through the real host boundary helpers."""
+ from types import SimpleNamespace
+ from agent.conversation_compression import (
+ _queue_context_engine_compression_notification, _restore_compressor_attempt_state,
+ _snapshot_compressor_attempt_state, finalize_context_engine_compression_notification)
+
+ llm = SimpleNamespace(complete=lambda *_args, **_kwargs: SimpleNamespace(
+ text=FALLBACK_SUMMARY, usage=SimpleNamespace(input_tokens=50)))
+ probe = type(engine)(llm=llm)
+ probe.on_session_start("wc-boundary-0", platform="cli")
+ probe.update_model(model=MODEL, context_length=CONTEXT_LENGTH, base_url=PLACEHOLDER_URL,
+ api_key="no-key-required", provider="custom", api_mode="chat_completions")
+ host = SimpleNamespace(context_compressor=probe, platform="cli")
+ history = synthetic_history()
+
+ def queue(next_session):
+ _queue_context_engine_compression_notification(
+ host, new_session_id=next_session, old_session_id=probe._wc_session_id)
+
+ snapshot = _snapshot_compressor_attempt_state(probe)
+ checks["fresh_engine_counter_in_host_snapshot"] = snapshot.get("compression_count") == 0
+ staged = probe.compress(history)
+ checks["boundary_probe_has_real_fallback_candidate"] = (
+ staged is not history and probe.warm_last["path"] == "fallback")
+ checks["first_attempt_advances_compression_count"] = probe.compression_count == 1
+ checks["staged_failure_does_not_count"] = probe._warm_failures == 0
+ queue("wc-boundary-discarded")
+ checks["discarded_host_boundary_is_silent"] = not finalize_context_engine_compression_notification(
+ host, committed=False)
+ checks["discarded_failure_does_not_count"] = probe._warm_failures == 0
+ checks["discarded_host_boundary_cannot_be_replayed"] = not finalize_context_engine_compression_notification(
+ host, committed=True)
+ _restore_compressor_attempt_state(probe, snapshot, durable_cooldown_authoritative=False)
+ checks["host_rollback_restores_compression_count"] = probe.compression_count == snapshot["compression_count"]
+ queue("wc-boundary-stale")
+ finalize_context_engine_compression_notification(host, committed=True)
+ checks["restored_candidate_cannot_count_at_stale_boundary"] = probe._warm_failures == 0
+
+ probe.compress(history)
+ checks["later_failure_waits_for_commit"] = probe._warm_failures == 0
+ queue("wc-boundary-committed")
+ finalize_context_engine_compression_notification(host, committed=True)
+ checks["later_committed_failure_counts_once"] = probe._warm_failures == 1
+ checks["committed_host_boundary_cannot_be_replayed"] = not finalize_context_engine_compression_notification(
+ host, committed=True)
+ checks["replayed_host_boundary_does_not_count"] = probe._warm_failures == 1
+
+ # A rollback after an earlier commit must keep that committed streak and reject the new candidate.
+ snapshot = _snapshot_compressor_attempt_state(probe)
+ probe.compress(history)
+ queue("wc-boundary-rollback-after-commit")
+ finalize_context_engine_compression_notification(host, committed=False)
+ _restore_compressor_attempt_state(probe, snapshot, durable_cooldown_authoritative=False)
+ queue("wc-boundary-stale-after-commit")
+ finalize_context_engine_compression_notification(host, committed=True)
+ checks["rollback_keeps_prior_committed_streak"] = probe._warm_failures == 1
+ checks["rollback_after_commit_restores_count"] = probe.compression_count == snapshot["compression_count"]
+ probe.compress(history)
+ queue("wc-boundary-retry")
+ finalize_context_engine_compression_notification(host, committed=True)
+ checks["retry_after_rollback_counts_once"] = probe._warm_failures == 2
# ---------------------------------------------------------------------------------------------------------
diff --git a/tests/test_wc_engine.py b/tests/test_wc_engine.py
index 317833d..6c03fee 100644
--- a/tests/test_wc_engine.py
+++ b/tests/test_wc_engine.py
@@ -1,6 +1,7 @@
"""Tests for the warm_compaction context engine with stand-in Hermes modules."""
import json
+import threading
import unittest
from types import SimpleNamespace
@@ -77,6 +78,17 @@ class EngineTest(unittest.TestCase):
finish_reason="tool_calls" if reply.get("tool_calls") else "stop",
assistant_message=reply_object(reply))
+ def commit(self, engine, new_session=None, **context):
+ source = engine._wc_session_id
+ engine.on_session_start(new_session or source, boundary_reason="compression", old_session_id=source,
+ **context)
+
+ def compress_committed(self, engine, history):
+ result = engine.compress(history)
+ if result is not history:
+ self.commit(engine)
+ return result
+
def test_threshold_comes_from_the_setting(self):
self.assertEqual(self.engine.threshold_tokens, 100_000)
self.assertFalse(self.engine.should_compress(99_999))
@@ -592,7 +604,7 @@ class EngineTest(unittest.TestCase):
history = [*old_turns(), assistant("done")]
with self.assertLogs("warm_compaction.engine", level="WARNING") as logs:
for _ in range(3):
- engine.compress(history)
+ self.compress_committed(engine, history)
self.assertEqual((engine.warm_last["path"], engine.warm_last["reason"]), ("fallback", "no_capture"))
self.assertEqual(sum("Warm compaction skipped (no_capture); used the fallback summary" in line
for line in logs.output), 3)
@@ -600,9 +612,9 @@ class EngineTest(unittest.TestCase):
# Hermes log style: no module prefix in the text (the log format shows the logger name), no emoji.
self.assertFalse(any(line.split(":", 2)[2].startswith(("warm_compaction:", "\u26a0")) for line in logs.output))
message = engine.get_automatic_compaction_status_message(phase="compress", default_message="Compacting")
- self.assertTrue(message.startswith("Compacting\n"))
# Hermes warning style on screen: the warning sign, a subject, what continues, and where to look.
- notice = message.split("\n", 1)[1]
+ notice = message
+ self.assertNotIn("Compacting", message)
self.assertTrue(notice.startswith("\u26a0 Warm compaction unavailable: the last 3 compactions did not "
"use the warm summary (no_capture:"))
self.assertIn("no main-model request completed", notice)
@@ -619,9 +631,9 @@ class EngineTest(unittest.TestCase):
history = [*old_turns(), assistant("done")]
with self.assertLogs("warm_compaction.engine", level="WARNING") as logs:
for _ in range(3):
- engine.compress(history)
+ self.compress_committed(engine, history)
self.llm.error = RuntimeError("down")
- engine.compress(history)
+ self.compress_committed(engine, history)
self.assertEqual(engine.warm_last["path"], "fixed")
notice = engine.get_automatic_compaction_status_message(phase="compress", default_message="Compacting")
self.assertIn("the last 4 compactions", notice)
@@ -635,16 +647,17 @@ class EngineTest(unittest.TestCase):
rows, reply = old_turns(), assistant("final")
with self.assertLogs("warm_compaction.engine", level="WARNING") as logs:
for _ in range(2):
- engine.compress([*rows, reply])
+ self.compress_committed(engine, [*rows, reply])
self.seed(rows, reply)
engine._post = fake_post(content="no headings")
- engine.compress([*rows, reply])
+ self.compress_committed(engine, [*rows, reply])
def refused(*args, **kwargs):
raise RuntimeError("synthetic server refusal")
engine._post = refused
- engine.compress([*rows, reply])
+ self.seed(rows, reply)
+ self.compress_committed(engine, [*rows, reply])
notice = engine.get_automatic_compaction_status_message(phase="compress", default_message="Compacting")
causes = (
"no_capture: no main-model request completed",
@@ -663,12 +676,12 @@ class EngineTest(unittest.TestCase):
history = [*old_turns(), assistant("done")]
with self.assertLogs("warm_compaction.engine", level="WARNING") as logs:
for _ in range(3):
- engine.compress(history)
+ self.compress_committed(engine, history)
self.assertIn("Warm compaction unavailable", engine.get_automatic_compaction_status_message(
phase="compress", default_message="Compacting"))
self.llm.error = RuntimeError("down")
for _ in range(2):
- engine.compress(history)
+ self.compress_committed(engine, history)
self.assertEqual(engine.get_automatic_compaction_status_message(
phase="compress", default_message="Compacting"), "Compacting")
self.assertEqual(sum("Warm compaction skipped" in line for line in logs.output), 5)
@@ -680,7 +693,8 @@ class EngineTest(unittest.TestCase):
rows, reply = old_turns(), assistant("final")
self.seed(rows, reply)
for _ in range(3):
- engine.compress([*rows, reply])
+ self.seed(rows, reply)
+ self.compress_committed(engine, [*rows, reply])
self.assertEqual((engine.warm_last["path"], engine.warm_last["reason"],
engine.warm_last["cached_tokens"]), ("fallback", "gate:heading_missing", 900))
notice = engine.get_automatic_compaction_status_message(phase="compress", default_message="Compacting")
@@ -693,29 +707,29 @@ class EngineTest(unittest.TestCase):
engine = self.make()
rows, reply = old_turns(), assistant("final")
for _ in range(3):
- engine.compress([*rows, reply])
+ self.compress_committed(engine, [*rows, reply])
self.seed(rows, reply)
- engine.compress([*rows, reply])
+ self.compress_committed(engine, [*rows, reply])
self.assertEqual(engine.warm_last["path"], "warm")
self.assertEqual((engine._warm_failures, engine._warm_fixed, engine._warm_failure_reasons), (0, 0, []))
self.assertEqual(engine.get_automatic_compaction_status_message(phase="compress",
default_message="Compacting"), "Compacting")
self.store.forget(session_id="s1")
for _ in range(2):
- engine.compress([*rows, reply])
+ self.compress_committed(engine, [*rows, reply])
self.assertEqual(engine.get_automatic_compaction_status_message(phase="compress",
default_message="Compacting"), "Compacting")
def test_a_cancelled_attempt_does_not_end_the_failure_streak(self):
engine = self.make()
history = [*old_turns(), assistant("done")]
- engine.compress(history)
+ self.compress_committed(engine, history)
engine._compression_cancelled_check = lambda: True
- engine.compress(history)
+ self.compress_committed(engine, history)
self.assertEqual(engine.warm_last["path"], "cancelled")
engine._compression_cancelled_check = lambda: False
for _ in range(2):
- engine.compress(history)
+ self.compress_committed(engine, history)
self.assertIn("Warm compaction unavailable",
engine.get_automatic_compaction_status_message(phase="compress", default_message="Compacting"))
@@ -724,19 +738,25 @@ class EngineTest(unittest.TestCase):
history = [*old_turns(), assistant("done")]
with self.assertNoLogs("warm_compaction.engine", level="WARNING"):
for _ in range(4):
- engine.compress(history)
+ self.compress_committed(engine, history)
self.assertEqual(engine.get_automatic_compaction_status_message(phase="compress",
default_message="Compacting"), "Compacting")
- def test_the_notice_shows_when_the_host_status_is_turned_off(self):
- # A user who turned the compaction status off still gets the notice: it is a warning, not progress.
+ def test_a_disabled_formatter_keeps_the_pending_notice(self):
engine = self.make()
history = [*old_turns(), assistant("done")]
for _ in range(3):
- engine.compress(history)
+ self.compress_committed(engine, history)
+ pending = engine._warm_notice
engine.emit_automatic_compaction_status = False
- message = engine.get_automatic_compaction_status_message(phase="compress", default_message="Compacting")
- self.assertTrue(message.startswith("\u26a0 Warm compaction unavailable: the last 3 compactions"))
+ self.assertIsNone(engine.get_automatic_compaction_status_message(
+ phase="compress", default_message="Compacting"))
+ self.assertEqual(engine._warm_notice, pending)
+ engine.emit_automatic_compaction_status = True
+ self.assertEqual(engine.get_automatic_compaction_status_message(
+ phase="compress", default_message="Compacting"), pending)
+ self.assertEqual(engine.get_automatic_compaction_status_message(
+ phase="compress", default_message="Compacting"), "Compacting")
def test_the_notice_tells_when_the_fallback_also_failed(self):
# The fixed summary has no model request: the notice must not say that the fallback summary works.
@@ -745,7 +765,7 @@ class EngineTest(unittest.TestCase):
history = [*old_turns(), assistant("done")]
with self.assertLogs("warm_compaction.engine", level="WARNING") as logs:
for _ in range(3):
- engine.compress(history)
+ self.compress_committed(engine, history)
self.assertEqual(engine.warm_last["path"], "fixed")
notice = engine.get_automatic_compaction_status_message(phase="compress", default_message="Compacting")
self.assertNotIn("continues with the slower fallback summary", notice)
@@ -759,16 +779,131 @@ class EngineTest(unittest.TestCase):
engine = self.make()
history = [*old_turns(), assistant("done")]
for _ in range(3):
- engine.compress(history)
+ self.compress_committed(engine, history)
engine.on_session_reset()
self.assertEqual((engine._warm_failures, engine._warm_fixed, engine._warm_failure_reasons), (0, 0, []))
self.assertEqual(engine.get_automatic_compaction_status_message(phase="compress",
default_message="Compacting"), "Compacting")
for _ in range(2):
- engine.compress(history)
+ self.compress_committed(engine, history)
self.assertEqual(engine.get_automatic_compaction_status_message(phase="compress",
default_message="Compacting"), "Compacting")
+ def test_a_result_is_not_counted_before_the_commit_boundary(self):
+ engine = self.make()
+ engine.compress([*old_turns(), assistant("done")])
+ self.assertEqual((engine._warm_failures, engine._warm_fixed), (0, 0))
+ self.assertIsNone(engine._warm_notice)
+ self.commit(engine)
+ self.assertEqual(engine._warm_failures, 1)
+
+ def test_a_rejected_warm_success_keeps_the_prior_streak(self):
+ engine = self.make()
+ rows, reply = old_turns(), assistant("done")
+ for _ in range(3):
+ self.compress_committed(engine, [*rows, reply])
+ pending = engine._warm_notice
+ self.seed(rows, reply)
+ engine.compress([*rows, reply])
+ self.assertEqual(engine.warm_last["path"], "warm")
+ self.assertEqual(engine._warm_failures, 3)
+ self.assertEqual(engine._warm_notice, pending)
+ # A pre-commit cancellation restores the public counter in Hermes.
+ engine.compression_count -= 1
+ self.commit(engine)
+ self.assertEqual(engine._warm_failures, 3)
+ self.assertEqual(engine._warm_notice, pending)
+
+ def test_in_place_and_rotation_boundaries_consume_one_result(self):
+ for target in ("s1", "s2"):
+ with self.subTest(target=target):
+ engine = self.make()
+ engine.compress([*old_turns(), assistant("done")])
+ engine.on_session_start(target, boundary_reason="compression", old_session_id="s1")
+ self.assertEqual(engine._warm_failures, 1)
+ engine.on_session_start(target, boundary_reason="compression", old_session_id="s1")
+ self.assertEqual(engine._warm_failures, 1)
+
+ def test_reset_new_session_and_noop_drop_an_uncommitted_result(self):
+ for action in ("reset", "session", "noop"):
+ with self.subTest(action=action):
+ engine = self.make()
+ engine.compress([*old_turns(), assistant("done")])
+ if action == "reset":
+ engine.on_session_reset()
+ elif action == "session":
+ engine.on_session_start("s2")
+ else:
+ short = [user("short")]
+ self.assertIs(engine.compress(short), short)
+ engine.on_session_start("s1", boundary_reason="compression", old_session_id="s1")
+ self.assertEqual(engine._warm_failures, 0)
+
+ def test_adoption_wrong_session_and_wrong_count_drop_the_result(self):
+ for refusal in ("adoption", "session", "count"):
+ with self.subTest(refusal=refusal):
+ engine = self.make()
+ engine.compress([*old_turns(), assistant("done")])
+ count = engine.compression_count
+ context = {"session_db": object()} if refusal == "adoption" else {}
+ old = "other" if refusal == "session" else "s1"
+ if refusal == "count":
+ engine.compression_count -= 1
+ engine.on_session_start("s1", boundary_reason="compression", old_session_id=old, **context)
+ self.assertEqual(engine._warm_failures, 0)
+ engine.compression_count = count
+ self.commit(engine)
+ self.assertEqual(engine._warm_failures, 0)
+
+ def test_a_new_attempt_replaces_a_discarded_result(self):
+ engine = self.make()
+ history = [*old_turns(), assistant("done")]
+ engine.compress(history)
+ engine.compress(history)
+ self.assertEqual(engine._warm_failures, 0)
+ self.commit(engine)
+ self.assertEqual(engine._warm_failures, 1)
+
+ def test_a_late_worker_cannot_change_the_current_candidate_or_counter(self):
+ entered, release = threading.Event(), threading.Event()
+ results, errors = [], []
+ engine = self.make()
+ history = [*old_turns(), assistant("done")]
+ complete = self.llm.complete
+
+ def delayed_complete(*args, **kwargs):
+ if threading.current_thread().name == "old-compaction":
+ entered.set()
+ if not release.wait(5):
+ raise TimeoutError("test worker was not released")
+ return complete(*args, **kwargs)
+
+ self.llm.complete = delayed_complete
+
+ def old_attempt():
+ try:
+ results.append(engine.compress(history))
+ except BaseException as error:
+ errors.append(error)
+
+ worker = threading.Thread(target=old_attempt, name="old-compaction", daemon=True)
+ worker.start()
+ try:
+ self.assertTrue(entered.wait(5), "old worker did not enter the fallback")
+ engine.compress(history)
+ self.assertEqual(engine.compression_count, 1)
+ finally:
+ release.set()
+ worker.join(5)
+ self.assertFalse(worker.is_alive())
+ self.assertEqual(errors, [])
+ self.assertEqual(len(results), 1)
+ self.assertTrue(results[0] is history, "old worker returned a candidate")
+ self.assertEqual(engine.compression_count, 1)
+ self.assertEqual(engine._warm_failures, 0)
+ self.commit(engine)
+ self.assertEqual(engine._warm_failures, 1)
+
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 2d40b11..9fd5aa6 100644
--- a/warm_compaction/engine.py
+++ b/warm_compaction/engine.py
@@ -4,6 +4,7 @@ from __future__ import annotations
import copy
import logging
+import threading
import time
from typing import Any
from collections.abc import Callable
@@ -157,8 +158,12 @@ class WarmCompactionEngine(ContextEngine):
self._clock = clock
self.threshold_percent = float(self._settings["threshold"])
self.last_real_prompt_tokens = 0
+ self.compression_count = 0
self.warm_last: dict[str, Any] | None = None
self._wc_session_id = ""
+ self._wc_result_lock = threading.Lock()
+ self._wc_attempt_serial = 0
+ self._pending_warm_result: tuple[str, int, dict[str, Any]] | None = None
self._wc_route: tuple = (None, None, None)
self._wc_api_key: Any = ""
self._wc_provider: str = ""
@@ -181,9 +186,21 @@ class WarmCompactionEngine(ContextEngine):
return clone
def on_session_start(self, session_id: str, **kwargs: Any) -> None:
- if kwargs.get("boundary_reason") == "compression" and kwargs.get("old_session_id"):
- self._store.forget(session_id=kwargs["old_session_id"])
- self._wc_session_id = str(session_id or "")
+ with self._wc_result_lock:
+ pending, self._pending_warm_result = self._pending_warm_result, None
+ self._wc_attempt_serial += 1
+ if kwargs.get("boundary_reason") == "compression":
+ old_session = str(kwargs.get("old_session_id") or "")
+ # Hermes also calls this hook when it adopts another process's child. That call gives session_db.
+ if (pending is not None and "session_db" not in kwargs
+ and pending[0] == old_session == self._wc_session_id
+ and pending[1] == self.compression_count):
+ self._note_warm_result(pending[2])
+ if old_session:
+ self._store.forget(session_id=old_session)
+ elif str(session_id or "") != self._wc_session_id:
+ self._clear_warm_failures()
+ self._wc_session_id = str(session_id or "")
# The hooks do not get the key: the store asks the engine for the stamp of the key now.
self._store.set_stamp(self._wc_session_id, self._key_stamp)
@@ -244,10 +261,13 @@ class WarmCompactionEngine(ContextEngine):
return start > 0
def on_session_reset(self) -> None:
- super().on_session_reset()
- self.warm_last = None
- # The failure streak and its notice are of the old session.
- self._clear_warm_failures()
+ with self._wc_result_lock:
+ super().on_session_reset()
+ self._wc_attempt_serial += 1
+ self._pending_warm_result = None
+ self.warm_last = None
+ # The failure streak and its notice are of the old session.
+ self._clear_warm_failures()
# 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
@@ -264,7 +284,10 @@ class WarmCompactionEngine(ContextEngine):
# The route, key, and session of this attempt, before anything reads them. Hermes can switch them while
# the attempt still runs (it runs on a pooled thread and can outlive a host timeout); the warm request goes
# only to this route, and the result is used only when they did not change.
- attempt = self._attempt()
+ with self._wc_result_lock:
+ self._wc_attempt_serial += 1
+ self._pending_warm_result = None
+ attempt = self._attempt()
prefixes = self._prefixes()
policy = self._policy(messages)
capture = self._store.latest(attempt[3])
@@ -390,13 +413,16 @@ class WarmCompactionEngine(ContextEngine):
end_marker=hermes_value("agent.context_compressor", "_SUMMARY_END_MARKER", HERMES_END_MARKER),
marker=hermes_value("agent.context_compressor", "_DB_PERSISTED_MARKER", HERMES_DB_MARKER),
copy_tokens=copy_tokens, tail_tokens=tail_tokens, policy=policy)
- # Again before the result is used: a switch or a cancel while the new history was built also stops it.
- if self._cancelled() or self._attempt() != attempt:
- record.update(path="cancelled", reason="cancelled" if self._cancelled() else "route_changed")
+ # A stale worker must not change the counter or replace a newer candidate. No model call holds this lock.
+ with self._wc_result_lock:
+ if self._cancelled() or self._attempt() != attempt:
+ record.update(path="cancelled", reason="cancelled" if self._cancelled() else "route_changed")
+ self._finish(record, started)
+ return messages
+ self.compression_count += 1
self._finish(record, started)
- return messages
- self.compression_count += 1
- self._finish(record, started)
+ # The host can still reject this candidate. Only its successful boundary updates the failure streak.
+ self._pending_warm_result = (attempt[3], self.compression_count, dict(record))
return new
def _policy(self, messages: list) -> SendPolicy:
@@ -435,10 +461,10 @@ class WarmCompactionEngine(ContextEngine):
request_reserve(budget, int(self.context_length or 0)))
def _attempt(self) -> tuple:
- """(route, api key, provider, session id, window, threshold) now. The result of an attempt is sized for
- the window and threshold: a change of them also stops it."""
+ """Route, key, provider, session, window, threshold, and local attempt id now.
+ A change stops the attempt before it can publish a candidate."""
return (tuple(self._wc_route), self._wc_api_key, self._wc_provider, self._wc_session_id,
- int(self.context_length or 0), int(self.threshold_tokens or 0))
+ int(self.context_length or 0), int(self.threshold_tokens or 0), self._wc_attempt_serial)
def _warm_summary(self, messages: list, capture: dict[str, Any] | None, focus_topic: str | None, memory: str,
prefixes: tuple[str, ...], record: dict[str, Any], attempt: tuple) -> str | None:
@@ -650,7 +676,6 @@ class WarmCompactionEngine(ContextEngine):
logger.info("warm_compaction: path=%s reason=%s elapsed_s=%s prompt_tokens=%s cached_tokens=%s",
record["path"], record["reason"], record["elapsed_s"], record["prompt_tokens"],
record["cached_tokens"])
- self._note_warm_result(record)
def _clear_warm_failures(self) -> None:
self._warm_failures, self._warm_fixed = 0, 0
@@ -699,11 +724,11 @@ class WarmCompactionEngine(ContextEngine):
def get_automatic_compaction_status_message(self, *, phase: str, default_message: str,
**context: Any) -> str | None:
- """The Hermes status for an automatic compaction, with the warm failure notice one time after a streak.
- The notice shows also when the user turned the compaction status off: it is a warning, not progress."""
+ """Return one pending warning when Hermes permits an automatic compaction status."""
message = super().get_automatic_compaction_status_message(phase=phase, default_message=default_message,
**context)
- notice, self._warm_notice = self._warm_notice, None
- if notice is None:
- return message
- return f"{message}\n{notice}" if message else notice
+ if message is None:
+ return None
+ with self._wc_result_lock:
+ notice, self._warm_notice = self._warm_notice, None
+ return notice if notice is not None else message