Skip to content

Your lightweight open-source personal agent.

fix(tests): update recent history truncation to use token limits

w.antar21d9072190f3
diff --git a/tests/agent/test_context_prompt_cache.py b/tests/agent/test_context_prompt_cache.py
index ac3a83bf4..13f160aee 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: