Skip to content

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

Cut all sent payloads of the newest unit, and budget an unknown overhead and reply limit conservatively

Elevatormusic0a830476300a
diff --git a/README.md b/README.md
index a6cec3c..6136238 100644
--- a/README.md
+++ b/README.md
@@ -196,7 +196,7 @@ The plugin was faster than both other engines in 10 of 10 cases. hermes-lcm and
## Limits
- The warm path needs a captured main-model request in the current Hermes process. After a restart or a resume, the first compaction uses the fallback summary if no main-model request completed before it (`no_capture`).
-- The verbatim tail keeps the newest rows that fit in `tail_tokens`. It stops at the first older row that does not fit, and that row goes into the summary. When the newest unit alone is larger than the tail or the free room, its largest user, assistant, and tool rows keep only their start and end; an assistant row keeps its tool calls. The fallback summary gets the cut middles. With an unknown request overhead (no usable capture and no host token count), the tail takes at most half of the free room. A capture of another route, or one whose rows a middleware changed, is not used for the room.
+- The verbatim tail keeps the newest rows that fit in `tail_tokens`. It stops at the first older row that does not fit, and that row goes into the summary. When the newest unit alone is larger than the tail or the free room, its largest sent payloads keep only their start and end: message text, tool call arguments (kept as a JSON object), and reasoning text. A large image or other media part is replaced by a short text. Signed `reasoning_details` stay as they are. The fallback summary gets the cut middles. Without a usable capture (after a restart, after a model change, or when a middleware changed the captured rows), the request overhead is unknown: the summary has no copied user messages, the tail takes at most half of the free room, and the reply reserve is a quarter of the window (at most 65,536 and at least 4,096 tokens). The host token count is not used for the overhead: it does not use the plugin tokenizer.
- The warm request is not streamed and not retried. Its time limit is 120 seconds. The fallback request also has a 120-second limit.
- A request with `extra_headers` or `extra_query`, `n` above 1, a forced `tool_choice`, `response_format`, or audio settings uses the fallback (`settings_unsupported`).
- After `/compress`, Hermes builds the system prompt again from its configuration. A system message that a host gave in code is not kept. This is Hermes behavior, and it is the same for the built-in compressor.
diff --git a/evidence/plugin-integration.json b/evidence/plugin-integration.json
index 4e4bb73..5923640 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-06T12:35:21Z",
+ "started_utc": "2026-10-06T12:56:31Z",
"hermes": {
"commit": "45871e100feceb89769536c88e5e6e265226a409",
"clean": true
@@ -10,10 +10,10 @@
"plugin_files_sha256": {
"warm_compaction/__init__.py": "11e038f05813627b2d1d8b82b3b775ecfe41015746c8e90ed6d452cfdaec65a6",
"warm_compaction/capture.py": "387a5bda2c97530c6e9114e810ac062199e3731c12a582d75598d2bdb95e8c57",
- "warm_compaction/engine.py": "c70f188a789215e70d1252ea350d1f8433a1f989572b174a316447011a0d71d1",
+ "warm_compaction/engine.py": "d910f67fab4eb1fbfc108216b4bc7f47d583135c537043be73d1f3548b1dbf32",
"warm_compaction/fallback.py": "30ef046f0511491175d99a772afcc2c9a8f3a9920d0c077821a89f19147682f9",
"warm_compaction/handoff.py": "3ce85d76fce4afbefec0d9c2e91b100e09ac677a28e50ea749f4d25533d68c7e",
- "warm_compaction/layout.py": "8e05c88f13c29330e1bd1a3bdb4410cd1c0a8a6ce30fcf8a118bd88a8e0a55e4",
+ "warm_compaction/layout.py": "ba910e9df1ee04bb4f9d5c9ab6dda48e3975634e6b7b05dd930f273d3cea2b74",
"warm_compaction/plugin.yaml": "d67560fd4625ec80d9e26f3e6d712763e77474f68d469c7e48f09fd3506ccc71",
"warm_compaction/rows.py": "689c0619df6a7f8b89ef05ae87ec81b62c812ce34f81bfe557eb653158effac4",
"warm_compaction/warm.py": "081642f19498b379551af33b70ad01dae3902e1e805c360a08be9f74242d320f"
@@ -76,11 +76,11 @@
"threshold_tokens": 100000,
"context_length": 200000
},
- "compress_seconds": 0.098,
+ "compress_seconds": 0.117,
"warm_last": {
"path": "warm",
"reason": "accepted",
- "elapsed_s": 0.025,
+ "elapsed_s": 0.038,
"prompt_tokens": 33883,
"cached_tokens": 16941
},
@@ -88,10 +88,11 @@
"final_session_id": "wc-enable_manual_warm",
"continuation_keeps_custom_system_text": false,
"fence_denied": {
- "network_host_blocked": 1,
- "child_process_blocked": 3
+ "child_process_blocked": 4,
+ "network_host_blocked": 1
},
"fence_blocked_programs": [
+ "git",
"pip",
"python",
"python3"
@@ -100,6 +101,7 @@
"hermes-assets.nousresearch.com"
],
"fence_blocked_callers": [
+ "child_process_blocked from hermes_cli/source_check.py:_git_run < hermes_cli/source_check.py:_git_stdout < hermes_cli/source_check.py:_read_checkout",
"child_process_blocked from tools/env_probe.py:_run < tools/env_probe.py:_py_out < tools/env_probe.py:_pip_python_version",
"child_process_blocked from tools/env_probe.py:_run < tools/env_probe.py:_py_out < tools/env_probe.py:_python_version_of",
"network_host_blocked from hermes_cli/release_channels.py:read < hermes_cli/release_channels.py:read_bytes < hermes_cli/release_channels.py:read_record"
@@ -187,7 +189,7 @@
"install",
"run"
],
- "process_seconds": 53.236
+ "process_seconds": 52.454
},
{
"scenario": "manual_fallback",
@@ -246,7 +248,7 @@
"threshold_tokens": 100000,
"context_length": 200000
},
- "compress_seconds": 0.227,
+ "compress_seconds": 0.22,
"warm_last": {
"path": "fallback",
"reason": "provider_error",
@@ -359,7 +361,7 @@
"install",
"run"
],
- "process_seconds": 32.914
+ "process_seconds": 31.415
},
{
"scenario": "manual_fixed",
@@ -417,11 +419,11 @@
"threshold_tokens": 100000,
"context_length": 200000
},
- "compress_seconds": 0.241,
+ "compress_seconds": 0.246,
"warm_last": {
"path": "fixed",
"reason": "provider_error",
- "elapsed_s": 0.125,
+ "elapsed_s": 0.109,
"prompt_tokens": null,
"cached_tokens": null
},
@@ -530,7 +532,7 @@
"install",
"run"
],
- "process_seconds": 29.487
+ "process_seconds": 29.036
},
{
"scenario": "auto_tool_loop",
@@ -618,14 +620,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.015,
"prompt_tokens": 18427,
"cached_tokens": 9213
},
@@ -735,7 +737,7 @@
"install",
"run"
],
- "process_seconds": 32.249
+ "process_seconds": 32.294
},
{
"scenario": "rollback",
@@ -785,7 +787,7 @@
"threshold_tokens": 100000,
"context_length": 200000
},
- "compress_seconds": 0.163,
+ "compress_seconds": 0.154,
"warm_last": {
"path": "warm",
"reason": "accepted",
@@ -971,10 +973,10 @@
"run",
"rollback"
],
- "process_seconds": 37.327
+ "process_seconds": 39.378
}
],
- "finished_utc": "2026-10-06T12:38:26Z",
+ "finished_utc": "2026-10-06T12:59:36Z",
"passed": true,
"run_folder": "outside repository"
}
diff --git a/tests/test_wc_engine.py b/tests/test_wc_engine.py
index 515fec0..13491a8 100644
--- a/tests/test_wc_engine.py
+++ b/tests/test_wc_engine.py
@@ -381,6 +381,12 @@ class EngineTest(unittest.TestCase):
from warm_compaction.engine import request_reserve
from warm_compaction.warm import DEFAULT_RESERVE
self.assertEqual(request_reserve(None), DEFAULT_RESERVE)
+ # Without a usable capture the reply limit of the next request is unknown: keep a quarter of the window,
+ # at most 65,536 tokens (the Hermes default of a native Gemini route), at least the default.
+ self.assertEqual(request_reserve(None, 200_000), 50_000)
+ self.assertEqual(request_reserve(None, 1_000_000), 65_536)
+ self.assertEqual(request_reserve(None, 8_000), DEFAULT_RESERVE)
+ self.assertEqual(request_reserve({"body": {"messages": [], "max_tokens": 1_000}}, 200_000), 1_000)
self.assertEqual(request_reserve({"body": {"messages": [], "max_tokens": 1_000}}), 1_000)
self.assertEqual(request_reserve({"body": {"messages": [], "max_completion_tokens": 2_000}}), 2_000)
@@ -391,13 +397,10 @@ class EngineTest(unittest.TestCase):
self.seed(rows, assistant("final"))
capture = self.store.latest("s1")
self.assertEqual(request_overhead(capture), estimate_tokens({"messages": [SYSTEM], "tools": None}))
- # Without a captured body: the host's token count less the history estimate, or unknown.
+ # Without a captured body the overhead is unknown: the host's token count and the plugin estimate do not
+ # use the same tokenizer, so their difference is not a measurement.
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)
- # 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)))
+ self.assertIsNone(request_overhead({"body": None}, rows))
def test_request_overhead_counts_text_that_a_middleware_added_to_a_captured_row(self):
from warm_compaction.engine import request_overhead
@@ -433,8 +436,7 @@ class EngineTest(unittest.TestCase):
provider="custom", api_mode=ROUTE[2])
new = engine.compress(rows, current_tokens=estimate_tokens(rows) + 1_000)
self.assertEqual(engine.warm_last["reason"], "route_changed")
- summary = next(row["content"] for row in new if "## Copied user messages" in str(row["content"]))
- self.assertFalse(summary.split("## Copied user messages", 1)[1].strip().startswith("(none)"))
+ self.assertIsNone(engine._budget_capture(self.store.latest("s1"), rows))
def test_a_large_prepended_user_row_is_cut_to_fit(self):
# The newest user message is before the tail (it is larger than the tail) and goes in front of it. It
@@ -471,7 +473,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) + 1_000, True), (estimate_tokens(rows) + 60_000, False),
+ # The host count is not used: a compressible history can have an estimate above it.
+ for current, copied in ((estimate_tokens(rows) + 1_000, False), (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)
@@ -542,18 +545,19 @@ class EngineTest(unittest.TestCase):
def test_an_unknown_overhead_caps_the_tail(self):
# Without a known overhead (no capture and no host count), the tail takes at most half of the free room:
# the other half is for the system rows and the tool schemas.
- from warm_compaction.engine import CARRIER_TOKENS
+ from warm_compaction.engine import CARRIER_TOKENS, request_reserve
from warm_compaction.rows import estimate_tokens
- from warm_compaction.warm import DEFAULT_RESERVE
rows = [*old_turns(4), user("go"), assistant("", [("c1", "read", "{}")]),
tool("c1", "head " + "r" * 200_000 + " tail")]
engine = self.make(threshold=0.95, tail_tokens=9_500, warm=False)
engine.update_model(model=ROUTE[0], context_length=20_000, base_url=ROUTE[1], api_key="k",
provider="custom", api_mode=ROUTE[2])
- limit = min(engine.threshold_tokens, 20_000 - DEFAULT_RESERVE)
- for current, capped in ((estimate_tokens(rows) + 1_000, False), (None, True)):
- with self.subTest(current=current):
- new = engine.compress(list(rows), current_tokens=current)
+ limit = min(engine.threshold_tokens, 20_000 - request_reserve(None, 20_000))
+ for captured, capped in ((False, True), (True, False)):
+ with self.subTest(captured=captured):
+ if captured:
+ self.seed(rows[:-2], rows[-2])
+ new = engine.compress(list(rows), current_tokens=estimate_tokens(rows) + 1_000)
tail = [row for row in new if not row.get("_compressed_summary")]
self.assertEqual(estimate_tokens(tail) <= (limit - CARRIER_TOKENS) // 2, capped)
diff --git a/tests/test_wc_layout.py b/tests/test_wc_layout.py
index c85571f..51331d4 100644
--- a/tests/test_wc_layout.py
+++ b/tests/test_wc_layout.py
@@ -187,6 +187,44 @@ class BoundTailTest(unittest.TestCase):
self.assertTrue(bounded[1]["content"].startswith("start ") and bounded[1]["content"].endswith(" end"))
self.assertIn(MIDDLE_MARK, bounded[1]["content"])
+ def test_large_tool_call_arguments_are_cut_to_valid_json(self):
+ import json
+ from warm_compaction.layout import CUT_NOTE, bound_tail
+ from warm_compaction.rows import estimate_tokens
+ arguments = json.dumps({"path": "a.txt", "text": "start " + "w" * 40_000 + " end"})
+ rows = [user("write"), assistant("", [("c1", "write", arguments)]), tool("c1", "ok")]
+ removed = []
+ bounded = bound_tail(rows, 2_000, removed)
+ self.assertLessEqual(sum(estimate_tokens(row) for row in bounded), 2_000)
+ call = bounded[1]["tool_calls"][0]
+ self.assertEqual((call["id"], call["function"]["name"]), ("c1", "write"))
+ kept = json.loads(call["function"]["arguments"])["truncated_arguments"]
+ self.assertTrue(kept.startswith('{"path": "a.txt"') and kept.endswith(' end"}'))
+ self.assertEqual(rows[1]["tool_calls"][0]["function"]["arguments"], arguments)
+ self.assertEqual(len(removed), 1)
+ self.assertTrue(removed[0]["content"].startswith(CUT_NOTE) and "w" * 100 in removed[0]["content"])
+
+ def test_large_reasoning_text_is_cut(self):
+ from warm_compaction.layout import bound_tail
+ from warm_compaction.rows import MIDDLE_MARK, estimate_tokens
+ rows = [user("think"), assistant("ok", reasoning_content="r-start " + "t" * 40_000 + " r-end")]
+ bounded = bound_tail(rows, 2_000)
+ self.assertLessEqual(sum(estimate_tokens(row) for row in bounded), 2_000)
+ self.assertIn(MIDDLE_MARK, bounded[1]["reasoning_content"])
+ self.assertTrue(bounded[1]["reasoning_content"].endswith(" r-end"))
+
+ def test_list_content_is_cut_and_a_large_image_is_replaced(self):
+ from warm_compaction.layout import bound_tail
+ from warm_compaction.rows import estimate_tokens
+ image = {"type": "image_url", "image_url": {"url": "data:image/png;base64," + "A" * 100_000}}
+ rows = [user([{"type": "text", "text": "look " + "l" * 20_000 + " end"}, image]), assistant("ok")]
+ bounded = bound_tail(rows, 2_000)
+ self.assertLessEqual(sum(estimate_tokens(row) for row in bounded), 2_000)
+ text, note = bounded[0]["content"]
+ self.assertTrue(text["text"].startswith("look ") and text["text"].endswith(" end"))
+ self.assertEqual(note, {"type": "text", "text": "[image_url removed]"})
+ self.assertIs(rows[0]["content"][1], image)
+
def test_the_cut_middles_are_given_back(self):
# The fallback summary gets the parts that the tail cuts: they are not lost.
from warm_compaction.layout import CUT_NOTE, bound_tail
diff --git a/warm_compaction/engine.py b/warm_compaction/engine.py
index 45c3202..caa5f97 100644
--- a/warm_compaction/engine.py
+++ b/warm_compaction/engine.py
@@ -27,6 +27,8 @@ CARRIER_TOKENS = 500
# The summary size that the tail cap keeps free before the fallback summary is known: two times its reply
# limit, because the estimate and the server count do not use the same tokenizer.
SUMMARY_RESERVE = 2 * fallback.MAX_TOKENS
+# The largest reply reserve for an unknown reply limit: the Hermes output reserve of a native Gemini route.
+UNKNOWN_RESERVE_MAX = 65_536
HERMES_END_MARKER = "--- END OF CONTEXT SUMMARY — respond to the message below, not the summary above ---"
HERMES_DB_MARKER = "_db_persisted"
@@ -79,19 +81,14 @@ def sanitize_memory(memory_context: Any) -> str:
return ""
-def request_overhead(capture: dict[str, Any] | None, messages: list | None = None,
- current_tokens: Any = None) -> int | None:
+def request_overhead(capture: dict[str, Any] | None, messages: list | None = None) -> int | None:
"""Estimated tokens of the request parts that are not history rows: the system rows and the tool schemas of
- the captured request. Without a captured request body (for example, after a restart), the host's token
- count of the request less the history estimate. None when neither is known."""
+ the captured request. None without a captured request body (for example, after a restart). The host's token
+ count less the history estimate is not used: the two do not use the same tokenizer, and a compressible
+ history can have an estimate far above the server count, so the difference can be much too small."""
body = (capture or {}).get("body")
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
- # 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
+ return None
count = max(len(body["messages"]) - len(capture.get("digests") or ()), 0)
overhead = estimate_tokens({"messages": body["messages"][:count], "tools": body.get("tools")})
if messages is not None:
@@ -102,9 +99,14 @@ def request_overhead(capture: dict[str, Any] | None, messages: list | None = Non
return overhead
-def request_reserve(capture: dict[str, Any] | None) -> int:
- """The reply reserve of the captured request (warm.reply_reserve), else the default."""
- return warm.reply_reserve((capture or {}).get("body"))
+def request_reserve(capture: dict[str, Any] | None, context_length: int = 0) -> int:
+ """The reply reserve of the captured request (warm.reply_reserve). Without a captured body, the reply limit
+ of the next request is unknown (Hermes does not give it to a context engine): a quarter of the window, at most
+ UNKNOWN_RESERVE_MAX, and at least the default."""
+ body = (capture or {}).get("body")
+ if isinstance(body, dict) and isinstance(body.get("messages"), list):
+ return warm.reply_reserve(body)
+ return max(warm.DEFAULT_RESERVE, min(context_length // 4, UNKNOWN_RESERVE_MAX))
def _int(value: Any) -> int:
@@ -202,8 +204,8 @@ class WarmCompactionEngine(ContextEngine):
# An unknown overhead can be most of the window: then no copies. A capture of another route says nothing
# about the system prompt, the tools, and the reply limit of this one.
budget = self._budget_capture(capture, messages)
- overhead = request_overhead(budget, messages, current_tokens)
- reserve = request_reserve(budget)
+ overhead = request_overhead(budget, messages)
+ reserve = request_reserve(budget, int(self.context_length or 0))
summary = self._warm_summary(messages, capture, focus_topic, memory, prefixes, record)
# The middles that the tail cuts (see layout.bound_tail): the rows before the tail do not have them.
removed: list = []
diff --git a/warm_compaction/layout.py b/warm_compaction/layout.py
index fed641c..b321260 100644
--- a/warm_compaction/layout.py
+++ b/warm_compaction/layout.py
@@ -7,7 +7,8 @@ import functools
from typing import Any, Iterable
from .handoff import END_MARKER, LEGACY_PREFIX
-from .rows import api_content, attr, cut_bounds, cut_middle, estimate_tokens, hermes_value, plain_text, visible_text
+from .rows import (api_content, attr, compact_json, cut_bounds, cut_middle, estimate_tokens, hermes_value, plain_text,
+ visible_text)
HEADER_TEXT = "The summary of the earlier turns follows."
COPY_HEADING = "## Copied user messages"
@@ -224,48 +225,113 @@ def _tail_row(row: Any, marker: str) -> Any:
return clean
+# The sent text fields of an assistant row that a model can make large: its reasoning.
+REASONING_KEYS = ("reasoning_content", "reasoning")
+
+
+def _tail_parts(row: Any) -> list[tuple[tuple, Any]]:
+ """Return the sent payloads of a tail row that a cut can make smaller, as (key, value): the content (text, or
+ each part of a list), the arguments of each tool call, and the reasoning text of an assistant row."""
+ if not isinstance(row, dict):
+ return []
+ role = row.get("role")
+ content = api_content(row) if role in ("user", "assistant") else row.get("content")
+ parts: list[tuple[tuple, Any]] = []
+ if isinstance(content, str):
+ parts.append((("content",), content))
+ elif isinstance(content, list):
+ for index, item in enumerate(content):
+ if isinstance(item, dict) and item.get("type") == "text" and isinstance(item.get("text"), str):
+ parts.append((("content", index), item["text"]))
+ elif isinstance(item, dict):
+ parts.append((("media", index), item))
+ if role == "assistant":
+ for index, call in enumerate(row.get("tool_calls") or ()):
+ function = call.get("function") if isinstance(call, dict) else None
+ if isinstance(function, dict) and function.get("arguments") is not None:
+ arguments = function["arguments"]
+ parts.append((("arguments", index), arguments if isinstance(arguments, str) else compact_json(arguments)))
+ parts.extend(((key,), row[key]) for key in REASONING_KEYS if isinstance(row.get(key), str))
+ return parts
+
+
+def _set_part(row: dict[str, Any], key: tuple, value: Any) -> dict[str, Any]:
+ """Return a copy of the row with one payload replaced. A changed content drops the api_content sidecar: the
+ new content is the sent text."""
+ new = dict(row)
+ if key[0] in ("content", "media"):
+ content = api_content(row) if row.get("role") in ("user", "assistant") else row.get("content")
+ new.pop("api_content", None)
+ if len(key) == 1:
+ new["content"] = value
+ else:
+ items = list(content)
+ items[key[1]] = {**items[key[1]], "text": value} if key[0] == "content" else value
+ new["content"] = items
+ elif key[0] == "arguments":
+ calls = [dict(call) for call in row["tool_calls"]]
+ # A cut JSON text is not JSON: the start and the end go into a JSON object, so that the call stays valid.
+ calls[key[1]] = {**calls[key[1]], "function": {**calls[key[1]]["function"],
+ "arguments": compact_json({"truncated_arguments": value})}}
+ new["tool_calls"] = calls
+ else:
+ new[key[0]] = value
+ return new
+
+
def bound_tail(rows: list, tokens: int, removed: list | None = None) -> list:
"""Return the tail rows in about tokens estimated tokens. The tail keeps whole units, so the newest unit can
- be larger than the tail budget (a large user message, assistant reply, or tool result). Then the largest text
- rows (as Hermes sent them) are cut to their start and end, until the rows fit or no row has more than
- MIN_COPY_CHARS characters. An assistant row keeps its tool calls. Other rows stay as they are.
-
- With a removed list, one row for each cut row is added to it: the removed middle after CUT_NOTE, with the role
- (and the tool call id) of the row. The fallback summary can then keep what the tail cuts."""
+ be larger than the tail budget (a large user message, assistant reply, tool call, or tool result). Then the
+ largest sent payloads (_tail_parts) are cut to their start and end, until the rows fit or no payload has more
+ than MIN_COPY_CHARS characters. A tool call keeps its id and name, and its arguments stay a JSON object. A
+ media part (an image, for example) is replaced by a short text. Other rows and fields stay as they are; signed
+ reasoning_details stay, because a cut breaks the signature.
+
+ With a removed list, one row for each cut payload is added to it: the removed middle after CUT_NOTE, with the
+ role (and the tool call id) of the row. The fallback summary can then keep what the tail cuts."""
rows = list(rows)
- originals: dict[int, str] = {}
- limits: dict[int, int] = {}
-
- def text_of(row: Any) -> Any:
- return api_content(row) if attr(row, "role") in ("user", "assistant") else attr(row, "content")
-
+ originals: dict[tuple, Any] = {}
+ current: dict[tuple, Any] = {}
+ limits: dict[tuple, int] = {}
while sum(estimate_tokens(row) for row in rows) > tokens:
- cuttable = [(estimate_tokens(row), index) for index, row in enumerate(rows)
- if isinstance(row, dict) and attr(row, "role") in ("user", "assistant", "tool")
- and isinstance(text_of(row), str) and len(text_of(row)) > MIN_COPY_CHARS]
+ cuttable = []
+ for index, row in enumerate(rows):
+ for key, value in _tail_parts(row):
+ value = current.get((index, key), value)
+ if (isinstance(value, str) and len(value) > MIN_COPY_CHARS) or (
+ key[0] == "media" and (index, key) not in current
+ and estimate_tokens(value) > MIN_COPY_CHARS // 4):
+ cuttable.append((estimate_tokens(value), index, key, value))
if not cuttable:
break
- cost, index = max(cuttable)
- text = text_of(rows[index])
- target = cost - (sum(estimate_tokens(row) for row in rows) - tokens)
- limit = max(MIN_COPY_CHARS, min(len(text) - 1, len(text) * max(target, 0) // max(cost, 1)))
- # Cut the original text again: the kept start and end are then parts of the original text.
- original = originals.setdefault(index, text)
- limits[index] = limit
- cut = {key: value for key, value in rows[index].items() if key != "api_content"}
- cut["content"] = cut_middle(original, limit)
- rows[index] = cut
+ cost, index, key, value = max(cuttable, key=lambda item: (item[0], -item[1]))
+ originals.setdefault((index, key), value)
+ if key[0] == "media":
+ new_value: Any = {"type": "text", "text": f"[{value.get('type') or 'media'} removed]"}
+ else:
+ target = cost - (sum(estimate_tokens(row) for row in rows) - tokens)
+ limit = max(MIN_COPY_CHARS, min(len(value) - 1, len(value) * max(target, 0) // max(cost, 1)))
+ # Cut the original text again: the kept start and end are then parts of the original text.
+ limits[(index, key)] = limit
+ new_value = cut_middle(originals[(index, key)], limit)
+ current[(index, key)] = new_value
+ rows[index] = _set_part(rows[index], key, new_value)
if removed is not None:
- for index in sorted(originals):
- original = originals[index]
- first, second = cut_bounds(original, limits[index])
- part = {"role": attr(rows[index], "role"), "content": CUT_NOTE + original[first:second]}
+ for index, key in sorted(originals, key=lambda item: (item[0], repr(item[1]))):
+ original = originals[(index, key)]
+ if key[0] == "media":
+ text = f"(a {original.get('type') or 'media'} part was removed)"
+ else:
+ first, second = cut_bounds(original, limits[(index, key)])
+ label = {"arguments": "tool call arguments: ", "reasoning_content": "reasoning: ",
+ "reasoning": "reasoning: "}.get(key[0], "")
+ text = label + original[first:second]
+ part = {"role": attr(rows[index], "role"), "content": CUT_NOTE + text}
if part["role"] == "tool":
part["tool_call_id"] = attr(rows[index], "tool_call_id")
removed.append(part)
return rows
-
def build(messages: list, summary_text: str, *, start: int, prepend: dict[str, Any] | None, copy_chars: int,
header_prefix: str, prefixes: Iterable[str], end_marker: str,
marker: str = "_db_persisted", copy_tokens: int | None = None, tail_tokens: int | None = None) -> list | None: