From 21d9072190f391c3bb3f82b0e0d9efaeae5d69ae Mon Sep 17 00:00:00 2001 From: "w.antar" Date: Tue, 16 Jun 2026 09:25:36 +0100 Subject: [PATCH] fix(tests): update recent history truncation to use token limits --- tests/agent/test_context_prompt_cache.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/tests/agent/test_context_prompt_cache.py b/tests/agent/test_context_prompt_cache.py index ac3a83bf..13f160ae 100644 --- a/tests/agent/test_context_prompt_cache.py +++ b/tests/agent/test_context_prompt_cache.py @@ -222,18 +222,23 @@ def test_recent_history_capped_at_max(tmp_path) -> None: assert f"entry-{builder._MAX_RECENT_HISTORY + 19}" in prompt -def test_recent_history_truncated_at_max_chars(tmp_path) -> None: - """Recent History section must be truncated at _MAX_HISTORY_CHARS.""" +def test_recent_history_truncated_at_max_tokens(tmp_path) -> None: + """Recent History section must be truncated to _MAX_HISTORY_TOKENS.""" + import tiktoken + workspace = _make_workspace(tmp_path) builder = ContextBuilder(workspace) - big_entry = "x" * (builder._MAX_HISTORY_CHARS + 5_000) + big_entry = "word " * (builder._MAX_HISTORY_TOKENS + 5_000) builder.memory.append_history(big_entry) prompt = builder.build_system_prompt() history_section = prompt.split("# Recent History\n\n", 1) assert len(history_section) == 2 - assert len(history_section[1]) < builder._MAX_HISTORY_CHARS + 200 + + enc = tiktoken.get_encoding("cl100k_base") + # Small margin for the truncation suffix appended after the token slice. + assert len(enc.encode(history_section[1])) <= builder._MAX_HISTORY_TOKENS + 50 def test_no_recent_history_when_dream_has_processed_all(tmp_path) -> None: