Spaces:
Build error
Build error
Commit ·
b59d6a5
1
Parent(s): f3043c1
test: add integration tests for token_headroom mode
Browse filesCo-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
tests/test_token_headroom_mode.py
ADDED
|
@@ -0,0 +1,307 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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ated multi-turn conversations,
|
| 4 |
+
verifying the critical invariants: no message injection, correct frozen counts,
|
| 5 |
+
proper handling of both Anthropic and OpenAI formats, and correct behavior
|
| 6 |
+
when Claude Code drops messages.
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
import copy
|
| 10 |
+
|
| 11 |
+
from headroom.cache.compression_cache import CompressionCache
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def _make_user_msg(text: str) -> dict:
|
| 15 |
+
return {"role": "user", "content": text}
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def _make_assistant_msg(text: str) -> dict:
|
| 19 |
+
return {"role": "assistant", "content": text}
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def _make_tool_use_msg(tool_id: str, name: str) -> dict:
|
| 23 |
+
return {
|
| 24 |
+
"role": "assistant",
|
| 25 |
+
"content": [{"type": "tool_use", "id": tool_id, "name": name, "input": {}}],
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def _make_tool_result_msg(tool_id: str, content: str) -> dict:
|
| 30 |
+
"""Anthropic-format tool result."""
|
| 31 |
+
return {
|
| 32 |
+
"role": "user",
|
| 33 |
+
"content": [{"type": "tool_result", "tool_use_id": tool_id, "content": content}],
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def _make_openai_tool_msg(tool_call_id: str, content: str) -> dict:
|
| 38 |
+
"""OpenAI-format tool result."""
|
| 39 |
+
return {"role": "tool", "tool_call_id": tool_call_id, "content": content}
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def _large_code_content(n: int = 200) -> str:
|
| 43 |
+
"""Generate realistic Python code content."""
|
| 44 |
+
parts = ["import os\nimport sys\nfrom typing import List, Dict\n\n"]
|
| 45 |
+
for i in range(n // 10):
|
| 46 |
+
parts.append(
|
| 47 |
+
f"def function_{i}(arg: str) -> str:\n"
|
| 48 |
+
f' """Docstring for function {i}."""\n'
|
| 49 |
+
f" result = arg.strip()\n"
|
| 50 |
+
f" for j in range({i}):\n"
|
| 51 |
+
f" result += str(j)\n"
|
| 52 |
+
f" return result\n\n"
|
| 53 |
+
)
|
| 54 |
+
return "".join(parts)
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
class TestMultiTurnCompression:
|
| 58 |
+
"""Simulate multi-turn conversations to verify compression cascade."""
|
| 59 |
+
|
| 60 |
+
def test_first_turn_nothing_cached(self):
|
| 61 |
+
"""On first turn, no cache hits, frozen count is minimal."""
|
| 62 |
+
cache = CompressionCache()
|
| 63 |
+
messages = [
|
| 64 |
+
_make_user_msg("Read file.py"),
|
| 65 |
+
_make_tool_use_msg("t1", "Read"),
|
| 66 |
+
_make_tool_result_msg("t1", _large_code_content(100)),
|
| 67 |
+
]
|
| 68 |
+
frozen = cache.compute_frozen_count(messages)
|
| 69 |
+
# user (stable) + tool_use (stable) + tool_result (miss) → 2
|
| 70 |
+
assert frozen == 2
|
| 71 |
+
|
| 72 |
+
def test_second_turn_cache_hits(self):
|
| 73 |
+
"""After caching, same content gets cache hits."""
|
| 74 |
+
cache = CompressionCache()
|
| 75 |
+
code = _large_code_content(100)
|
| 76 |
+
compressed = "# compressed version"
|
| 77 |
+
|
| 78 |
+
# Simulate first turn: pipeline compressed the code
|
| 79 |
+
h = CompressionCache.content_hash(code)
|
| 80 |
+
cache.store_compressed(h, compressed, tokens_saved=500)
|
| 81 |
+
|
| 82 |
+
# Second turn: same messages
|
| 83 |
+
messages = [
|
| 84 |
+
_make_user_msg("Read file.py"),
|
| 85 |
+
_make_tool_use_msg("t1", "Read"),
|
| 86 |
+
_make_tool_result_msg("t1", code),
|
| 87 |
+
_make_user_msg("now edit it"),
|
| 88 |
+
]
|
| 89 |
+
frozen = cache.compute_frozen_count(messages)
|
| 90 |
+
# All 4 messages stable (user, tool_use, tool_result cached, user)
|
| 91 |
+
assert frozen == 4
|
| 92 |
+
|
| 93 |
+
# apply_cached should swap the content
|
| 94 |
+
result = cache.apply_cached(messages)
|
| 95 |
+
tool_result = result[2]["content"][0]
|
| 96 |
+
assert tool_result["content"] == compressed
|
| 97 |
+
|
| 98 |
+
def test_multi_turn_waterfall(self):
|
| 99 |
+
"""Messages age out progressively across turns."""
|
| 100 |
+
cache = CompressionCache()
|
| 101 |
+
|
| 102 |
+
# Build conversation with 3 read results
|
| 103 |
+
code_a = "code A " * 200
|
| 104 |
+
code_b = "code B " * 200
|
| 105 |
+
code_c = "code C " * 200
|
| 106 |
+
|
| 107 |
+
messages = [
|
| 108 |
+
_make_user_msg("read A"),
|
| 109 |
+
_make_tool_result_msg("t1", code_a),
|
| 110 |
+
_make_user_msg("read B"),
|
| 111 |
+
_make_tool_result_msg("t2", code_b),
|
| 112 |
+
_make_user_msg("read C"),
|
| 113 |
+
_make_tool_result_msg("t3", code_c),
|
| 114 |
+
]
|
| 115 |
+
|
| 116 |
+
# Turn 1: nothing cached
|
| 117 |
+
frozen = cache.compute_frozen_count(messages)
|
| 118 |
+
assert frozen == 1 # only first user msg
|
| 119 |
+
|
| 120 |
+
# Simulate pipeline compressing A and B (not C — in protection window)
|
| 121 |
+
cache.store_compressed(CompressionCache.content_hash(code_a), "ca", tokens_saved=100)
|
| 122 |
+
cache.store_compressed(CompressionCache.content_hash(code_b), "cb", tokens_saved=100)
|
| 123 |
+
|
| 124 |
+
# Turn 2: A and B cached, C still uncached
|
| 125 |
+
frozen = cache.compute_frozen_count(messages)
|
| 126 |
+
# user(stable) + tool_result_A(cached) + user(stable) + tool_result_B(cached) + user(stable) + tool_result_C(miss)
|
| 127 |
+
assert frozen == 5
|
| 128 |
+
|
| 129 |
+
# Now cache C too
|
| 130 |
+
cache.store_compressed(CompressionCache.content_hash(code_c), "cc", tokens_saved=100)
|
| 131 |
+
|
| 132 |
+
# Turn 3: all cached
|
| 133 |
+
frozen = cache.compute_frozen_count(messages)
|
| 134 |
+
assert frozen == 6 # all stable
|
| 135 |
+
|
| 136 |
+
|
| 137 |
+
class TestNoMessageInjection:
|
| 138 |
+
"""Critical invariant: proxy never adds messages."""
|
| 139 |
+
|
| 140 |
+
def test_output_length_equals_input(self):
|
| 141 |
+
cache = CompressionCache()
|
| 142 |
+
messages = [
|
| 143 |
+
_make_user_msg("hello"),
|
| 144 |
+
_make_tool_result_msg("t1", _large_code_content(50)),
|
| 145 |
+
_make_user_msg("bye"),
|
| 146 |
+
]
|
| 147 |
+
result = cache.apply_cached(messages)
|
| 148 |
+
assert len(result) == len(messages)
|
| 149 |
+
|
| 150 |
+
def test_orphan_cache_entries_not_injected(self):
|
| 151 |
+
"""Cache entries with no matching message are NOT injected."""
|
| 152 |
+
cache = CompressionCache()
|
| 153 |
+
cache.store_compressed("orphan_hash_1", "orphan content 1", tokens_saved=100)
|
| 154 |
+
cache.store_compressed("orphan_hash_2", "orphan content 2", tokens_saved=200)
|
| 155 |
+
|
| 156 |
+
messages = [_make_user_msg("hello")]
|
| 157 |
+
result = cache.apply_cached(messages)
|
| 158 |
+
assert len(result) == 1
|
| 159 |
+
assert result[0]["content"] == "hello"
|
| 160 |
+
|
| 161 |
+
def test_input_not_mutated(self):
|
| 162 |
+
"""apply_cached must NOT mutate the input list or messages."""
|
| 163 |
+
cache = CompressionCache()
|
| 164 |
+
code = "original code content"
|
| 165 |
+
h = CompressionCache.content_hash(code)
|
| 166 |
+
cache.store_compressed(h, "compressed", tokens_saved=50)
|
| 167 |
+
|
| 168 |
+
messages = [
|
| 169 |
+
_make_tool_result_msg("t1", code),
|
| 170 |
+
]
|
| 171 |
+
original = copy.deepcopy(messages)
|
| 172 |
+
cache.apply_cached(messages)
|
| 173 |
+
assert messages == original
|
| 174 |
+
|
| 175 |
+
|
| 176 |
+
class TestClaudeCodeDropsMessages:
|
| 177 |
+
"""When Claude Code drops messages via its own context management."""
|
| 178 |
+
|
| 179 |
+
def test_dropped_messages_not_readded(self):
|
| 180 |
+
cache = CompressionCache()
|
| 181 |
+
content_a = "content A " * 100
|
| 182 |
+
content_b = "content B " * 100
|
| 183 |
+
cache.store_compressed(CompressionCache.content_hash(content_a), "ca", tokens_saved=100)
|
| 184 |
+
cache.store_compressed(CompressionCache.content_hash(content_b), "cb", tokens_saved=100)
|
| 185 |
+
|
| 186 |
+
# CC dropped the message with content_b
|
| 187 |
+
messages = [
|
| 188 |
+
_make_user_msg("hello"),
|
| 189 |
+
_make_tool_result_msg("t1", content_a),
|
| 190 |
+
_make_user_msg("continue"),
|
| 191 |
+
]
|
| 192 |
+
result = cache.apply_cached(messages)
|
| 193 |
+
assert len(result) == 3 # NOT 4
|
| 194 |
+
|
| 195 |
+
def test_frozen_count_breaks_at_gap(self):
|
| 196 |
+
"""Dropped cached message creates a gap that stops frozen count."""
|
| 197 |
+
cache = CompressionCache()
|
| 198 |
+
content_a = "content A " * 100
|
| 199 |
+
content_c = "content C " * 100
|
| 200 |
+
cache.store_compressed(CompressionCache.content_hash(content_a), "ca", tokens_saved=100)
|
| 201 |
+
cache.store_compressed(CompressionCache.content_hash(content_c), "cc", tokens_saved=100)
|
| 202 |
+
|
| 203 |
+
# CC dropped content_b, content_c is still here but preceded by uncached gap
|
| 204 |
+
messages = [
|
| 205 |
+
_make_tool_result_msg("t1", content_a),
|
| 206 |
+
_make_tool_result_msg("t2", "UNCACHED content_b replacement"),
|
| 207 |
+
_make_tool_result_msg("t3", content_c),
|
| 208 |
+
]
|
| 209 |
+
frozen = cache.compute_frozen_count(messages)
|
| 210 |
+
# t1 (cached, stable), t2 (NOT cached, stop)
|
| 211 |
+
assert frozen == 1
|
| 212 |
+
|
| 213 |
+
|
| 214 |
+
class TestOpenAIFormat:
|
| 215 |
+
"""Verify OpenAI-format tool messages work correctly."""
|
| 216 |
+
|
| 217 |
+
def test_openai_tool_result_cached(self):
|
| 218 |
+
cache = CompressionCache()
|
| 219 |
+
content = "large openai output " * 100
|
| 220 |
+
h = CompressionCache.content_hash(content)
|
| 221 |
+
cache.store_compressed(h, "compressed openai output", tokens_saved=300)
|
| 222 |
+
|
| 223 |
+
messages = [
|
| 224 |
+
_make_user_msg("run command"),
|
| 225 |
+
{
|
| 226 |
+
"role": "assistant",
|
| 227 |
+
"content": None,
|
| 228 |
+
"tool_calls": [
|
| 229 |
+
{
|
| 230 |
+
"id": "tc1",
|
| 231 |
+
"type": "function",
|
| 232 |
+
"function": {"name": "bash", "arguments": "{}"},
|
| 233 |
+
}
|
| 234 |
+
],
|
| 235 |
+
},
|
| 236 |
+
_make_openai_tool_msg("tc1", content),
|
| 237 |
+
]
|
| 238 |
+
|
| 239 |
+
result = cache.apply_cached(messages)
|
| 240 |
+
assert len(result) == 3
|
| 241 |
+
assert result[2]["content"] == "compressed openai output"
|
| 242 |
+
|
| 243 |
+
def test_openai_frozen_count(self):
|
| 244 |
+
cache = CompressionCache()
|
| 245 |
+
content = "openai tool output " * 100
|
| 246 |
+
h = CompressionCache.content_hash(content)
|
| 247 |
+
cache.store_compressed(h, "compressed", tokens_saved=200)
|
| 248 |
+
|
| 249 |
+
messages = [
|
| 250 |
+
_make_user_msg("hello"),
|
| 251 |
+
_make_openai_tool_msg("tc1", content),
|
| 252 |
+
_make_openai_tool_msg("tc2", "uncached content"),
|
| 253 |
+
]
|
| 254 |
+
frozen = cache.compute_frozen_count(messages)
|
| 255 |
+
# user (stable), tool tc1 (cached), tool tc2 (miss)
|
| 256 |
+
assert frozen == 2
|
| 257 |
+
|
| 258 |
+
|
| 259 |
+
class TestUpdateFromResult:
|
| 260 |
+
"""Verify update_from_result correctly caches compression results."""
|
| 261 |
+
|
| 262 |
+
def test_caches_compressed_anthropic(self):
|
| 263 |
+
cache = CompressionCache()
|
| 264 |
+
original_content = "long original " * 100
|
| 265 |
+
compressed_content = "short compressed"
|
| 266 |
+
|
| 267 |
+
originals = [
|
| 268 |
+
_make_user_msg("hello"),
|
| 269 |
+
_make_tool_result_msg("t1", original_content),
|
| 270 |
+
]
|
| 271 |
+
compressed = [
|
| 272 |
+
_make_user_msg("hello"),
|
| 273 |
+
_make_tool_result_msg("t1", compressed_content),
|
| 274 |
+
]
|
| 275 |
+
|
| 276 |
+
cache.update_from_result(originals, compressed)
|
| 277 |
+
|
| 278 |
+
h = CompressionCache.content_hash(original_content)
|
| 279 |
+
assert cache.get_compressed(h) == compressed_content
|
| 280 |
+
|
| 281 |
+
def test_caches_compressed_openai(self):
|
| 282 |
+
cache = CompressionCache()
|
| 283 |
+
original_content = "long openai output " * 100
|
| 284 |
+
compressed_content = "short compressed"
|
| 285 |
+
|
| 286 |
+
originals = [_make_openai_tool_msg("tc1", original_content)]
|
| 287 |
+
compressed = [_make_openai_tool_msg("tc1", compressed_content)]
|
| 288 |
+
|
| 289 |
+
cache.update_from_result(originals, compressed)
|
| 290 |
+
|
| 291 |
+
h = CompressionCache.content_hash(original_content)
|
| 292 |
+
assert cache.get_compressed(h) == compressed_content
|
| 293 |
+
|
| 294 |
+
def test_length_mismatch_no_crash(self):
|
| 295 |
+
"""If pipeline somehow changes message count, don't crash."""
|
| 296 |
+
cache = CompressionCache()
|
| 297 |
+
originals = [_make_user_msg("a"), _make_user_msg("b")]
|
| 298 |
+
compressed = [_make_user_msg("a")] # shorter
|
| 299 |
+
# Should not raise, just log warning
|
| 300 |
+
cache.update_from_result(originals, compressed)
|
| 301 |
+
assert cache.get_stats()["entries"] == 0
|
| 302 |
+
|
| 303 |
+
def test_unchanged_content_not_cached(self):
|
| 304 |
+
cache = CompressionCache()
|
| 305 |
+
msg = _make_user_msg("same content")
|
| 306 |
+
cache.update_from_result([msg], [msg])
|
| 307 |
+
assert cache.get_stats()["entries"] == 0
|