Skip to content

CubeLoop — a Pythonic, async-native agent harness core designed for high performance, readability, and production-grade persistence.

test(compaction): cover no-usage fallback in real_context_estimate

xf gonge0908d158c6b
diff --git a/tests/middleware/compaction/test_tokens.py b/tests/middleware/compaction/test_tokens.py
index 7dd12ab..a6200d3 100644
--- a/tests/middleware/compaction/test_tokens.py
+++ b/tests/middleware/compaction/test_tokens.py
@@ -150,3 +150,16 @@ def test_real_estimate_only_zero_usage_falls_back_to_approx() -> None:
AssistantMessage(content=[], usage=Usage()), # zero-sum, not an anchor
]
assert real_context_estimate(messages) == approx_tokens(messages) == 100
+
+
+def test_real_estimate_falls_back_when_no_usage_reported() -> None:
+ # A provider that never emits usage leaves every assistant with usage=None.
+ # The estimate must degrade to the plain char estimate — not crash, not
+ # anchor on a missing measurement, not undercount.
+ messages = [
+ UserMessage(content=[TextContent(text="x" * 400)]), # 200 tokens
+ AssistantMessage(content=[TextContent(text="x" * 200)]), # 100, usage=None
+ UserMessage(content=[TextContent(text="x" * 200)]), # 100 tokens
+ AssistantMessage(content=[TextContent(text="x" * 100)]), # 50, usage=None
+ ]
+ assert real_context_estimate(messages) == approx_tokens(messages) == 450