Hermes Agent context engine: the main model writes the compaction handoff on its cached prompt prefix
Send the route headers, normalize the finish reason, and require an end line in the fallback
Elevatormusicaac5598cb9ca
diff --git a/README.md b/README.mdindex 0b3d35a..f2b5c17 100644--- a/README.md+++ b/README.md@@ -112,11 +112,11 @@ plugins:threshold: 0.5```-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.+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. The warm request sends the default headers of the Hermes client for the route: the provider headers, `model.default_headers`, and `providers.<name>.extra_headers`. 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-The fallback summary uses the auxiliary task `warm_compaction`. Set `auxiliary.warm_compaction.provider` and `auxiliary.warm_compaction.model` to use a different model. The default `auto` uses the main model route. The fallback request is at most about 11,000 tokens: a transcript of about 8,000 tokens, the instruction, and a 2,048-token reply. The focus topic and the memory context are cut to about 1,000 tokens and take their size from the transcript. A fallback model needs a context window of at least 16,000 tokens. Hermes accepts main models with 64,000 tokens or more, so the default route has enough space.+The fallback summary uses the auxiliary task `warm_compaction`. Set `auxiliary.warm_compaction.provider` and `auxiliary.warm_compaction.model` to use a different model. The default `auto` uses the main model route. The fallback reply must end with the line `[END OF SUMMARY]`: the auxiliary route reports no finish reason, so a reply without that line counts as cut off. The fallback request is at most about 11,000 tokens: a transcript of about 8,000 tokens, the instruction, and a 2,048-token reply. The focus topic and the memory context are cut to about 1,000 tokens and take their size from the transcript. A fallback model needs a context window of at least 16,000 tokens. Hermes accepts main models with 64,000 tokens or more, so the default route has enough space.For a custom endpoint that needs a key, put the key name in the model settings:@@ -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_after_capture`, `middleware_order_unknown`, `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_order_unknown`, `headers_unknown`, `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.jsonindex 7ac0749..a420301 100644--- a/evidence/plugin-integration.json+++ b/evidence/plugin-integration.json@@ -1,22 +1,22 @@{"check": "standalone warm_compaction plugin on unpatched Hermes Agent",- "started_utc": "2026-10-06T10:50:00Z",+ "started_utc": "2026-10-06T11:09:43Z","hermes": {"commit": "45871e100feceb89769536c88e5e6e265226a409","clean": true},"python": "3.11.14",- "script_sha256": "142269a2bf8baae84ec933a126f4c835091588ad7aae425b64b788c4e4a52ef2",+ "script_sha256": "74bb090a6af32a6e0994d3189c3ea9d551328a88e634501b1eaa09135aed3c2c","plugin_files_sha256": {"warm_compaction/__init__.py": "11e038f05813627b2d1d8b82b3b775ecfe41015746c8e90ed6d452cfdaec65a6","warm_compaction/capture.py": "387a5bda2c97530c6e9114e810ac062199e3731c12a582d75598d2bdb95e8c57",- "warm_compaction/engine.py": "db2a26b90325105cac5ccae248a831faa8c979c474e826cb30675cc2ab3c93d9",- "warm_compaction/fallback.py": "fe6bfbd18359b84a9ebc0519b73a865fa5736a62f87e06f83535968b49317972",+ "warm_compaction/engine.py": "f33e5f76683e379e164e71d8a68fcdb750811463f5fdb4b73bc71c8077b32263",+ "warm_compaction/fallback.py": "45dffdab8983292f4d6b7e5b7f60b7578078c8b1abb436c0e47acef3b2f77960","warm_compaction/handoff.py": "3ce85d76fce4afbefec0d9c2e91b100e09ac677a28e50ea749f4d25533d68c7e","warm_compaction/layout.py": "5e730211647eed0beec1e6819dfc008b0855a0866dc920bb0cf8d6d8ab2dfa92","warm_compaction/plugin.yaml": "d67560fd4625ec80d9e26f3e6d712763e77474f68d469c7e48f09fd3506ccc71","warm_compaction/rows.py": "cc2a8a3d0561559f24fc40881453441d938ea9d7803e1df81a107a5e05e5f00a",- "warm_compaction/warm.py": "1a8c12dabf17a1de36ba5f891f0c58230b7752aca3dee6aea10f7d41d864147e"+ "warm_compaction/warm.py": "c651392ce7f01937ebe284bb9d44eacf11c877961b405834b2fe1829a02f673e"},"privacy": "metadata only: names, counts, codes, booleans, and times; no message text","scenarios": [@@ -76,11 +76,11 @@"threshold_tokens": 100000,"context_length": 200000},- "compress_seconds": 0.129,+ "compress_seconds": 0.114,"warm_last": {"path": "warm","reason": "accepted",- "elapsed_s": 0.03,+ "elapsed_s": 0.023,"prompt_tokens": 33883,"cached_tokens": 16941},@@ -187,7 +187,7 @@"install","run"],- "process_seconds": 63.941+ "process_seconds": 58.218},{"scenario": "manual_fallback",@@ -246,7 +246,7 @@"threshold_tokens": 100000,"context_length": 200000},- "compress_seconds": 0.242,+ "compress_seconds": 0.214,"warm_last": {"path": "fallback","reason": "provider_error",@@ -258,7 +258,7 @@"final_session_id": "wc-manual_fallback","continuation_keeps_custom_system_text": false,"fence_denied": {- "child_process_blocked": 5,+ "child_process_blocked": 4,"network_host_blocked": 1},"fence_blocked_programs": [@@ -359,7 +359,7 @@"install","run"],- "process_seconds": 40.203+ "process_seconds": 35.991},{"scenario": "manual_fixed",@@ -417,11 +417,11 @@"threshold_tokens": 100000,"context_length": 200000},- "compress_seconds": 0.229,+ "compress_seconds": 0.269,"warm_last": {"path": "fixed","reason": "provider_error",- "elapsed_s": 0.093,+ "elapsed_s": 0.141,"prompt_tokens": null,"cached_tokens": null},@@ -530,7 +530,7 @@"install","run"],- "process_seconds": 31.621+ "process_seconds": 32.246},{"scenario": "auto_tool_loop",@@ -618,14 +618,14 @@"warm_last_1": {"path": "warm","reason": "accepted",- "elapsed_s": 0.016,+ "elapsed_s": 0.031,"prompt_tokens": 35201,"cached_tokens": 17600},"warm_last_2": {"path": "warm","reason": "accepted",- "elapsed_s": 0.0,+ "elapsed_s": 0.015,"prompt_tokens": 18427,"cached_tokens": 9213},@@ -735,7 +735,7 @@"install","run"],- "process_seconds": 41.669+ "process_seconds": 34.006},{"scenario": "rollback",@@ -785,7 +785,7 @@"threshold_tokens": 100000,"context_length": 200000},- "compress_seconds": 0.178,+ "compress_seconds": 0.184,"warm_last": {"path": "warm","reason": "accepted",@@ -948,7 +948,8 @@"stored": 30},"fence_denied": {- "child_process_blocked": 4+ "child_process_blocked": 5,+ "network_host_blocked": 1},"fence_blocked_programs": ["git",@@ -956,7 +957,9 @@"python","python3"],- "fence_blocked_hosts": []+ "fence_blocked_hosts": [+ "hermes-assets.nousresearch.com"+ ]},"exit": [0,@@ -968,10 +971,10 @@"run","rollback"],- "process_seconds": 46.63+ "process_seconds": 44.422}],- "finished_utc": "2026-10-06T10:53:44Z",+ "finished_utc": "2026-10-06T11:13:08Z","passed": true,"run_folder": "outside repository"}diff --git a/scripts/check_plugin_hermes.py b/scripts/check_plugin_hermes.pyindex 4249c50..7907d83 100644--- a/scripts/check_plugin_hermes.py+++ b/scripts/check_plugin_hermes.py@@ -46,7 +46,8 @@ HANDOFF = ("## Next step\nReport the code word BLUE-7.")HANDOFF_GOAL = "Finish the synthetic test task."-FALLBACK_SUMMARY = HANDOFF.replace("Finish the synthetic test task.", "Finish the synthetic test task (fallback).")+FALLBACK_SUMMARY = HANDOFF.replace("Finish the synthetic test task.", "Finish the synthetic test task (fallback).") + (+ "\n[END OF SUMMARY]")TOOL_PLUGIN = {"plugin.yaml": ("manifest_version: 2\nname: wc_test_tools\nversion: 0.0.1\n"diff --git a/tests/test_wc_engine.py b/tests/test_wc_engine.pyindex 6818b0b..6983807 100644--- a/tests/test_wc_engine.py+++ b/tests/test_wc_engine.py@@ -5,11 +5,13 @@ import unittestfrom types import SimpleNamespaceimport wc_hermes_stub+from warm_compaction.fallback import END_LINEfrom wc_fixtures import HEADINGS_TEXT, ROUTE, SYSTEM, assistant, tool, user, wireclass FakeLlm:- def __init__(self, text=HEADINGS_TEXT.replace("Finish the test task.", "Fallback summary."), error=None):+ def __init__(self, text=HEADINGS_TEXT.replace("Finish the test task.", "Fallback summary.") + "\n" + END_LINE,+ error=None):self.text, self.error, self.calls = text, error, []def complete(self, messages, **kwargs):@@ -236,6 +238,42 @@ 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_the_warm_request_has_the_default_headers_of_the_hermes_client(self):+ # Provider headers (attribution, a WAF User-Agent, credentials) go with every request of the route.+ wc_hermes_stub.HOST_HEADERS.update({"X-Title": "Hermes Agent"})+ wc_hermes_stub.USER_HEADERS.update({"User-Agent": "allowed-agent"})+ wc_hermes_stub.CUSTOM_HEADERS.update({"X-Gateway-Key": "synthetic-header-secret"})+ rows = old_turns()+ reply = assistant("final")+ self.seed(rows, reply)+ self.engine.compress([*rows, reply])+ self.assertEqual(self.engine.warm_last["path"], "warm")+ headers = self.post.calls[0]["headers"]+ self.assertEqual((headers["X-Title"], headers["User-Agent"], headers["X-Gateway-Key"], headers["Authorization"]),+ ("Hermes Agent", "allowed-agent", "synthetic-header-secret", "Bearer k"))+ self.assertNotIn("synthetic-header-secret", json.dumps(self.engine.warm_last))+ # Without a host factory, the provider profile gives the headers.+ wc_hermes_stub.HOST_HEADERS.clear()+ wc_hermes_stub.PROFILE_HEADERS.update({"User-Agent": "profile-agent"})+ wc_hermes_stub.USER_HEADERS.clear()+ self.seed(rows, reply)+ self.engine.compress([*rows, reply])+ self.assertEqual(self.post.calls[1]["headers"]["User-Agent"], "profile-agent")++ def test_an_unreadable_header_source_stops_the_warm_request(self):+ def broken(base_url):+ raise AttributeError("changed")+ self.addCleanup(setattr, wc_hermes_stub.AGENT_INIT, "_host_default_headers_factory",+ wc_hermes_stub.AGENT_INIT._host_default_headers_factory)+ wc_hermes_stub.AGENT_INIT._host_default_headers_factory = broken+ 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", "headers_unknown"))+ 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]}}diff --git a/tests/test_wc_fallback.py b/tests/test_wc_fallback.pyindex 39e8d16..a22b269 100644--- a/tests/test_wc_fallback.py+++ b/tests/test_wc_fallback.py@@ -4,7 +4,7 @@ import unittestfrom types import SimpleNamespacefrom warm_compaction.fallback import (- FALLBACK_INSTRUCTION, MAX_TOKENS, MIDDLE_MARK, TASK, TOOL_CHARS, TRANSCRIPT_CHARS, TRANSCRIPT_TOKENS,+ END_LINE, FALLBACK_INSTRUCTION, MAX_TOKENS, MIDDLE_MARK, TASK, TOOL_CHARS, TRANSCRIPT_CHARS, TRANSCRIPT_TOKENS,fixed_summary, llm_summary, render_row, transcript,)from warm_compaction.rows import estimate_tokens@@ -18,7 +18,7 @@ SUMMARY = "## Goal\nG\n## User instructions\n- none\n## Current state\n- [OPEN]class FakeLlm:- def __init__(self, text=SUMMARY, error=None, output_tokens=None):+ def __init__(self, text=SUMMARY + "\n" + END_LINE, error=None, output_tokens=None):self.text, self.error, self.calls, self.output_tokens = text, error, [], output_tokensdef complete(self, messages, **kwargs):@@ -169,10 +169,20 @@ class LlmSummaryTest(unittest.TestCase):self.assertLessEqual(estimate_tokens(extra) + estimate_tokens(messages[1]["content"]),TRANSCRIPT_TOKENS + 16)+ def test_a_reply_without_the_end_line_is_refused(self):+ # ctx.llm reports no finish reason. A content filter or a provider limit can stop a reply below+ # MAX_TOKENS; the five headings can already be there. Only the end line shows a complete reply.+ self.assertTrue(FALLBACK_INSTRUCTION.rstrip().endswith(END_LINE))+ for text in (SUMMARY, SUMMARY + "\n" + END_LINE + "\nmore text", END_LINE + "\n" + SUMMARY):+ with self.subTest(text=text[-20:]), self.assertLogs("warm_compaction.fallback", level="WARNING"):+ self.assertEqual(llm_summary(FakeLlm(text=text, output_tokens=300), [user("hi")], PREFIXES),+ (None, None))+ text = "<think>plan</think>\n" + SUMMARY + "\n\n " + END_LINE + " \n"+ self.assertEqual(llm_summary(FakeLlm(text=text), [user("hi")], PREFIXES), (SUMMARY, 321))+def test_a_reply_that_reached_the_token_limit_is_refused(self):# ctx.llm reports no finish reason. A reply at the max_tokens limit can be cut off.- long_summary = SUMMARY + "\n" + "- fact\n" * 1_000- for llm in (FakeLlm(output_tokens=MAX_TOKENS), FakeLlm(text=long_summary)):+ for llm in (FakeLlm(output_tokens=MAX_TOKENS),):with self.subTest(), self.assertLogs("warm_compaction.fallback", level="WARNING"):self.assertEqual(llm_summary(llm, [user("hi")], PREFIXES), (None, None))self.assertEqual(llm_summary(FakeLlm(output_tokens=300), [user("hi")], PREFIXES), (SUMMARY, 321))diff --git a/tests/test_wc_warm.py b/tests/test_wc_warm.pyindex 17a182d..342b0c2 100644--- a/tests/test_wc_warm.py+++ b/tests/test_wc_warm.py@@ -403,6 +403,21 @@ def fake_post(status, payload, calls):class SendTest(unittest.TestCase):+ def test_the_finish_reason_is_normalized(self):+ # Some OpenAI-compatible servers send STOP or MAX_TOKENS. The gate needs the contract value.+ for raw, expected in (("STOP", "stop"), ("MAX_TOKENS", "length"), ("end", "stop"), ("stop", "stop")):+ payload = {"choices": [{"message": {"content": "text"}, "finish_reason": raw}], "usage": {}}+ with self.subTest(raw=raw):+ reply = send({"messages": []}, "http://h/v1", "k", post=fake_post(200, payload, []))+ self.assertEqual(reply["finish_reason"], expected)++ def test_extra_headers_are_sent(self):+ calls = []+ payload = {"choices": [{"message": {"content": "text"}, "finish_reason": "stop"}], "usage": {}}+ send({"messages": []}, "http://h/v1", "k", post=fake_post(200, payload, calls),+ extra_headers={"X-Title": "Hermes Agent"})+ self.assertEqual((calls[0][2]["X-Title"], calls[0][2]["Authorization"]), ("Hermes Agent", "Bearer k"))+def test_reads_the_reply_and_the_usage(self):calls = []payload = {"choices": [{"message": {"content": "text"}, "finish_reason": "stop"}],diff --git a/tests/wc_hermes_stub.py b/tests/wc_hermes_stub.pyindex 789e340..0527171 100644--- a/tests/wc_hermes_stub.py+++ b/tests/wc_hermes_stub.py@@ -97,6 +97,20 @@ CONTEXT_COMPRESSOR = _module(resolve_model_threshold=resolve_model_threshold)AGENT.context_engine = CONTEXT_ENGINEAGENT.context_compressor = CONTEXT_COMPRESSOR+# The sources of the default headers of the Hermes OpenAI client, in the order of agent.agent_init: a host+# factory or the provider profile, then model.default_headers, then providers.<name>.extra_headers.+HOST_HEADERS: dict = {}+PROFILE_HEADERS: dict = {}+USER_HEADERS: dict = {}+CUSTOM_HEADERS: dict = {}+AGENT_INIT = _module("agent.agent_init", _host_default_headers_factory=lambda base_url: (+ (lambda api_key, base: dict(HOST_HEADERS)) if HOST_HEADERS else None))+AUXILIARY_CLIENT = _module("agent.auxiliary_client", _apply_user_default_headers=lambda headers: (+ {**(headers or {}), **USER_HEADERS} if USER_HEADERS else headers))+PROVIDERS = _module("providers", get_provider_profile=lambda name: (+ SimpleNamespace(default_headers=dict(PROFILE_HEADERS)) if PROFILE_HEADERS else None))+AGENT.agent_init = AGENT_INIT+AGENT.auxiliary_client = AUXILIARY_CLIENTHERMES_CLI = _module("hermes_cli")PLUGINS = _module("hermes_cli.plugins", VALID_HOOKS={"pre_api_request", "post_api_request", "on_session_start", "on_session_end", "on_session_finalize",@@ -145,11 +159,16 @@ MIDDLEWARE = _module("hermes_cli.middleware", VALID_MIDDLEWARE={"tool_request", "tool_execution", "llm_request", "llm_execution"},run_llm_execution_middleware=run_llm_execution_middleware,apply_llm_request_middleware=apply_llm_request_middleware)+CONFIG_PROVIDERS = _module("hermes_cli.config_providers",+ get_custom_provider_extra_headers=lambda base_url, *args, **kwargs: dict(CUSTOM_HEADERS))+HERMES_CLI.config_providers = CONFIG_PROVIDERSHERMES_CLI.plugins = PLUGINSHERMES_CLI.middleware = MIDDLEWAREMODULES = {"agent": AGENT, "agent.context_engine": CONTEXT_ENGINE, "agent.context_compressor": CONTEXT_COMPRESSOR,"hermes_cli": HERMES_CLI, "hermes_cli.plugins": PLUGINS, "hermes_cli.middleware": MIDDLEWARE,+ "agent.agent_init": AGENT_INIT, "agent.auxiliary_client": AUXILIARY_CLIENT, "providers": PROVIDERS,+ "hermes_cli.config_providers": CONFIG_PROVIDERS,}@@ -161,3 +180,5 @@ def install(test_case):test_case.addCleanup(EXECUTION_MIDDLEWARE.clear)test_case.addCleanup(REQUEST_MIDDLEWARE.clear)test_case.addCleanup(CAPTURE_CHAIN.clear)+ for headers in (HOST_HEADERS, PROFILE_HEADERS, USER_HEADERS, CUSTOM_HEADERS):+ test_case.addCleanup(headers.clear)diff --git a/warm_compaction/engine.py b/warm_compaction/engine.pyindex 4dff304..be70e75 100644--- a/warm_compaction/engine.py+++ b/warm_compaction/engine.py@@ -125,6 +125,7 @@ class WarmCompactionEngine(ContextEngine):self._wc_session_id = ""self._wc_route: tuple = (None, None, None)self._wc_api_key: Any = ""+ self._wc_provider: str = ""@propertydef name(self) -> str:@@ -146,6 +147,7 @@ class WarmCompactionEngine(ContextEngine):api_mode=api_mode)self._wc_route = (model, base_url, api_mode)self._wc_api_key = api_key+ self._wc_provider = providerdef update_from_response(self, usage: dict[str, Any]) -> None:usage = usage or {}@@ -255,6 +257,8 @@ class WarmCompactionEngine(ContextEngine):from hermes_cli.middleware import apply_llm_request_middleware, run_llm_execution_middlewareexcept Exception as error:raise warm.WarmRefusal("middleware_unavailable") from error+ # Before any middleware sees the request: a route without its headers does not send it.+ headers = warm.route_headers(self._wc_api_key, self._wc_route[1], self._wc_provider)context = {"purpose": NAME, "api_request_id": None, "session_id": self._wc_session_id,"model": self._wc_route[0], "base_url": self._wc_route[1], "api_mode": self._wc_route[2]}try:@@ -289,7 +293,7 @@ class WarmCompactionEngine(ContextEngine):# attempt stops even when the middleware catches this refusal.repeated.append(True)raise warm.WarmRefusal("middleware_repeated")- result = warm.send(base, self._wc_route[1], self._wc_api_key, post=self._post)+ result = warm.send(base, self._wc_route[1], self._wc_api_key, post=self._post, extra_headers=headers)sent.append((result, dict(result)))return resulttry:diff --git a/warm_compaction/fallback.py b/warm_compaction/fallback.pyindex 2959577..20f3840 100644--- a/warm_compaction/fallback.py+++ b/warm_compaction/fallback.py@@ -38,6 +38,8 @@ ROW_TOKENS = ROW_CHARS // 4EXTRAS_CHARS = 4_000EXTRAS_TOKENS = EXTRAS_CHARS // 4MARK_TOKENS = 8+# The last line of a complete fallback reply. ctx.llm reports no finish reason.+END_LINE = "[END OF SUMMARY]"FALLBACK_INSTRUCTION = """\Write a handoff summary of the conversation transcript in the next message. The host program will replace the \@@ -69,6 +71,9 @@ Identifiers, names, values, and results that the next step needs.## Next stepThe next action that the user asked for and its exact target. Describe it. Do not do it.++End the summary with this line:+[END OF SUMMARY]"""@@ -165,14 +170,17 @@ def transcript(messages: list, prefixes: Iterable[str], reserve_chars: int = 0,return "\n\n".join([*head, *recent])-def _finish_reason(result: Any, text: str) -> str:- """Return "length" for a reply that can be cut off, else "stop". ctx.llm reports no finish reason. A reply- at the max_tokens limit can be cut off; the instruction asks for about 1,000 tokens. Without an output count,- a reply with an estimate above three quarters of the limit counts as cut off."""+def _complete_reply(result: Any, raw: str) -> tuple[str, str]:+ """Return (finish reason, text without the end line). ctx.llm reports no finish reason, and an output count+ below the limit does not show a complete reply: a content filter or a provider limit can stop it. Only a+ reply that ends with END_LINE, below the max_tokens limit, counts as complete ("stop")."""output = getattr(getattr(result, "usage", None), "output_tokens", None)- if isinstance(output, int) and not isinstance(output, bool) and output > 0:- return "length" if output >= MAX_TOKENS else "stop"- return "length" if estimate_tokens(text) >= MAX_TOKENS * 3 // 4 else "stop"+ lines = strip_think(raw).rstrip().splitlines()+ if not lines or lines[-1].strip() != END_LINE:+ return "length", raw+ if isinstance(output, int) and not isinstance(output, bool) and output >= MAX_TOKENS:+ return "length", raw+ return "stop", "\n".join(lines[:-1])def llm_summary(llm: Any, messages: list, prefixes: Iterable[str], *, focus_topic: str | None = None,@@ -196,7 +204,8 @@ def llm_summary(llm: Any, messages: list, prefixes: Iterable[str], *, focus_topi# The same checks as the warm reply: the five headings, the byte limit, and no summary markers. A reply# without them (cut off, or an answer to the conversation) must not replace the history.raw = str(getattr(result, "text", "") or "")- text, reason = gate({"content": raw, "finish_reason": _finish_reason(result, raw)}, prefixes)+ finish, body = _complete_reply(result, raw)+ text, reason = gate({"content": body, "finish_reason": finish}, prefixes)if text is None:logger.warning("warm_compaction: the fallback summary was refused (%s)", reason)return None, Nonediff --git a/warm_compaction/warm.py b/warm_compaction/warm.pyindex 94bdf30..4702b1c 100644--- a/warm_compaction/warm.py+++ b/warm_compaction/warm.py@@ -360,19 +360,59 @@ def _api_key_text(api_key: Any) -> str:return api_key if isinstance(api_key, str) else ""+_FINISH_ALIASES = {"max_tokens": "length", "end": "stop", "function_call": "tool_calls"}+++def _normalize_finish_reason(raw: Any) -> Any:+ """The rule of Hermes 45871e10 (agent.message_sanitization.normalize_finish_reason): the lowercase OpenAI+ value, with the aliases of other providers. Hermes applies it in its transport; this request does not use it."""+ if not isinstance(raw, str) or not raw:+ return raw+ lowered = raw.lower()+ return _FINISH_ALIASES.get(lowered, lowered)+++def route_headers(api_key: Any, base_url: Any, provider: Any) -> dict[str, str]:+ """Return the default headers that the Hermes OpenAI client sends on this route, in the order of Hermes+ 45871e10 (agent.agent_init): the host headers or the provider profile headers, then model.default_headers,+ then providers.<name>.extra_headers. Provider headers go with every request (attribution, a User-Agent for a+ WAF, gateway credentials). Raise WarmRefusal("headers_unknown") when a source cannot be read: a request+ without them can be refused or go to another cache. Per-request headers (session affinity) are in the+ captured request and refused there. The values can be credentials: never log them."""+ url = str(base_url or "")+ try:+ from agent.agent_init import _host_default_headers_factory+ from agent.auxiliary_client import _apply_user_default_headers+ from hermes_cli.config_providers import get_custom_provider_extra_headers+ factory = _host_default_headers_factory(url)+ if factory is not None:+ headers = dict(factory(_api_key_text(api_key), url) or {})+ else:+ from providers import get_provider_profile+ profile = get_provider_profile(provider) if provider else None+ headers = dict(getattr(profile, "default_headers", None) or {})+ headers = dict(_apply_user_default_headers(headers) or {})+ headers.update(get_custom_provider_extra_headers(url) or {})+ except Exception as error:+ raise WarmRefusal("headers_unknown") from error+ return {str(key): str(value) for key, value in headers.items()}++def _optional_int(value: Any) -> int | None:return value if isinstance(value, int) and not isinstance(value, bool) else Nonedef send(body: dict[str, Any], base_url: str, api_key: Any, timeout_s: float = TIMEOUT_S,- post: Post | None = None) -> dict[str, Any]:- """Send the warm request one time. Return the reply fields and the usage, or raise WarmRefusal."""+ post: Post | None = None, extra_headers: dict[str, str] | None = None) -> dict[str, Any]:+ """Send the warm request one time. Return the reply fields and the usage, or raise WarmRefusal.+ extra_headers are the client default headers of the route (route_headers); they win, as in the SDK."""if not base_url:raise WarmRefusal("provider_error")headers = {"Content-Type": "application/json", "Accept": "application/json"}key = _api_key_text(api_key)if key:headers["Authorization"] = f"Bearer {key}"+ headers.update(extra_headers or {})data = json.dumps(body).encode("utf-8")started = time.monotonic()try:@@ -392,7 +432,8 @@ def send(body: dict[str, Any], base_url: str, api_key: Any, timeout_s: float = Tdetails = usage.get("prompt_tokens_details") or {}return {"content": message.get("content"),- "finish_reason": choice.get("finish_reason"),+ "finish_reason": hermes_value("agent.message_sanitization", "normalize_finish_reason",+ _normalize_finish_reason)(choice.get("finish_reason")),"tool_calls": bool(message.get("tool_calls")),"refusal": bool(message.get("refusal")),"prompt_tokens": _optional_int(usage.get("prompt_tokens")),