headroom/tests/test_acceptance.py
chopratejas c765c53bf8 feat(rust): retire python smart_crusher, ship rust-only via pyo3
Stage 3c.1b step 2 + cleanup. The python `SmartCrusher` (3669 lines)
is replaced by a thin pyo3-backed shim (~290 lines) that delegates
every byte to `headroom._core.SmartCrusher` (built from
`crates/headroom-py`, landed in the previous commit). There is no
python implementation and no env-var fallback — the wheel is a hard
import.

Why now: parity was already proven across 17 fixtures + the python-
side bridge test (1+17 in `test_smart_crusher_rust_parity.py`).
Keeping a shadow python impl behind a flag is a permanent maintenance
cost with no operational benefit. Stage 3c.1b deletes ~3380 lines of
python parser/scorer/analyzer/orchestrator code; the rust crate has
its own coverage (388 unit tests + property tests in headroom-core).

Surface preserved (drop-in for every production caller):
- `headroom.transforms.smart_crusher.SmartCrusher` — same class name,
  same `__init__(config, relevance_config, scorer, ccr_config)`
  signature (the latter three are accepted for source-compat and
  silently dropped — rust port keeps those subsystems disabled in
  Stage 3c.1, they re-attach in Stage 3c.2).
- `SmartCrusherConfig` and `CrushResult` dataclasses kept as python
  dataclasses (callers use `asdict()` / dataclass matching on them).
- `crush(content, query, bias)`, `_smart_crush_content(content, ...)`,
  `apply(messages, tokenizer, **kwargs)`, and
  `_extract_context_from_messages(messages)` all preserved.
- `smart_crush_tool_output(content, config, ccr_config)` thin wrapper.

The transform-protocol `apply()` orchestration stays python (message
walking, digest-marker insertion, token counting); only the per-
message compression call delegates to rust.

Removed:
- Python parser / planner / scorer / analyzer / classifier (~3380 lines).
- Internal helpers `_classify_array`, `_detect_sequential_pattern`,
  `_detect_rare_status_values`, `_detect_items_by_learned_semantics`,
  `_percentile_linear`, `_compute_k_split`, `_crush_number_array`,
  `_process_value`, etc. — rust crate has parallel coverage.
- `SmartAnalyzer`, `ArrayType`, `CompressionStrategy`,
  `extract_query_anchors` — internals; not used by any production
  caller (only tests probed them).

Tests deleted (probed deleted internals — same precedent as Stage 3b):
- `tests/test_transforms/test_smart_crusher.py` (40 tests)
- `tests/test_transforms/test_universal_json_crush.py` (45)
- `tests/test_transforms/test_anchor_selector.py` (49)
- `tests/test_toin_field_learning.py` (21)
- `tests/test_crushability.py` (20)

Tests trimmed (removed methods/classes that probe deferred subsystems
— scorer injection, CCR marker injection, TOIN feedback recording —
all of which re-attach in Stage 3c.2):
- `tests/test_transforms/test_smart_crusher_bugs.py`:
  TestNumberArraySchemaPreservation, TestStage3c1BugFixes.
- `tests/test_relevance.py`: 2 scorer-injection tests.
- `tests/test_ccr.py`: TestSmartCrusherCCRIntegration class +
  test_custom_marker_template.
- `tests/test_toin_integration.py`: TestTOINIntegration +
  TestStoreToTOINHash classes.
- `tests/test_critical_fixes.py`: TestSmartCrusherTOINIntegration +
  test_full_feedback_loop.
- `tests/test_acceptance.py::TestQueryAnchorExtraction`: dropped the
  `extract_query_anchors` probe; kept the end-to-end "Alice
  preserved" assertion.

Bug fixes from Stage 3c.1 (#1 percentile linear interp, #2 zero-
padded sequential, #3 rare-status pareto, #4 k-split overshoot) are
pinned by the rust crate and the parity fixtures
(`tests/parity/fixtures/smart_crusher/`).

Tests:
- 517 passed in the smart_crusher-adjacent file set
  (test_transforms/, test_relevance*, test_ccr, test_toin_integration,
  test_quality_retention, test_acceptance, test_critical_fixes).
- 18 in `test_smart_crusher_rust_parity.py` (1 sanity + 17 fixtures).
- 388 rust unit tests still green.

One stale-error-message regex in `test_relevance_extra.py` updated
from "requires sentence-transformers" → "requires fastembed".
2026-04-27 00:52:21 -07:00

592 lines
22 KiB
Python

"""
Acceptance tests for Headroom SDK.
These are the 4 required acceptance tests from the spec:
1. Date Trap Test
2. Tool Orphan Test
3. Streaming Test
4. Safety Test (malformed JSON)
"""
import pytest
from headroom import OpenAIProvider, Tokenizer
from headroom.transforms import CacheAligner, RollingWindow
from headroom.transforms.tool_crusher import crush_tool_output
# Create a shared provider for tests
_provider = OpenAIProvider()
def get_tokenizer(model: str = "gpt-4o") -> Tokenizer:
"""Get a tokenizer for tests using OpenAI provider."""
token_counter = _provider.get_token_counter(model)
return Tokenizer(token_counter, model)
class TestDateTrap:
"""Test that system prompt dates are relocated and prefix hash is stable."""
def test_date_extraction_from_system_prompt(self):
"""Dates should be extracted from system prompt."""
messages_day1 = [
{"role": "system", "content": "You are helpful. Current Date: 2024-01-15"},
{"role": "user", "content": "Hello"},
]
aligner = CacheAligner()
tokenizer = get_tokenizer()
result = aligner.apply(messages_day1, tokenizer)
# Date should be moved out of main system content
system_content = result.messages[0]["content"]
# The date should be after the dynamic separator (---), not in the static prefix
# Split on the separator marker "---" to get static content
static_content = system_content.split("---")[0]
assert "Current Date: 2024-01-15" not in static_content
def test_stable_prefix_hash_across_days(self):
"""Prefix hash should be stable despite different dates."""
messages_day1 = [
{"role": "system", "content": "You are a helpful assistant. Current Date: 2024-01-15"},
{"role": "user", "content": "Hello"},
]
messages_day2 = [
{"role": "system", "content": "You are a helpful assistant. Current Date: 2024-01-16"},
{"role": "user", "content": "Hello"},
]
aligner = CacheAligner()
tokenizer = get_tokenizer()
result1 = aligner.apply(messages_day1, tokenizer)
result2 = aligner.apply(messages_day2, tokenizer)
# Extract hashes from markers
hash1 = None
hash2 = None
for marker in result1.markers_inserted:
if marker.startswith("stable_prefix_hash:"):
hash1 = marker.split(":", 1)[1]
for marker in result2.markers_inserted:
if marker.startswith("stable_prefix_hash:"):
hash2 = marker.split(":", 1)[1]
# Stable hash despite different dates
assert hash1 is not None
assert hash2 is not None
assert hash1 == hash2
def test_various_date_formats(self):
"""Various date formats should be detected."""
test_cases = [
"Current Date: 2024-01-15",
"Today is Monday, January 15",
"Today's date: 2024-01-15",
"2024-01-15T10:30:00",
]
aligner = CacheAligner()
tokenizer = get_tokenizer()
for date_str in test_cases:
messages = [
{"role": "system", "content": f"You are helpful. {date_str}. Be concise."},
{"role": "user", "content": "Hello"},
]
result = aligner.apply(messages, tokenizer)
# Transform should be applied (date detected)
# Either transforms_applied has cache_align or the date is moved
system_content = result.messages[0]["content"]
# Date should be separated from main instructions
assert "[Context:" in system_content or "cache_align" in result.transforms_applied
def test_cache_metrics_returned(self):
"""CachePrefixMetrics should be returned with all fields."""
messages = [
{"role": "system", "content": "You are helpful. Current Date: 2024-01-15"},
{"role": "user", "content": "Hello"},
]
aligner = CacheAligner()
tokenizer = get_tokenizer()
result = aligner.apply(messages, tokenizer)
# Cache metrics should be populated
assert result.cache_metrics is not None
assert result.cache_metrics.stable_prefix_bytes > 0
assert result.cache_metrics.stable_prefix_tokens_est > 0
assert len(result.cache_metrics.stable_prefix_hash) == 16
# First request: no previous hash, prefix_changed should be False
assert result.cache_metrics.prefix_changed is False
assert result.cache_metrics.previous_hash is None
def test_cache_metrics_tracks_changes(self):
"""Cache metrics should track prefix changes across requests."""
aligner = CacheAligner()
tokenizer = get_tokenizer()
# First request with one system prompt
messages1 = [
{"role": "system", "content": "You are helpful. Current Date: 2024-01-15"},
{"role": "user", "content": "Hello"},
]
result1 = aligner.apply(messages1, tokenizer)
# Second request with same static content (different date)
messages2 = [
{"role": "system", "content": "You are helpful. Current Date: 2024-01-16"},
{"role": "user", "content": "Hello"},
]
result2 = aligner.apply(messages2, tokenizer)
# Same static prefix → prefix_changed should be False
assert result2.cache_metrics is not None
assert result2.cache_metrics.prefix_changed is False
assert result2.cache_metrics.previous_hash == result1.cache_metrics.stable_prefix_hash
# Third request with DIFFERENT static content
messages3 = [
{"role": "system", "content": "You are VERY helpful. Current Date: 2024-01-17"},
{"role": "user", "content": "Hello"},
]
result3 = aligner.apply(messages3, tokenizer)
# Different static prefix → prefix_changed should be True
assert result3.cache_metrics is not None
assert result3.cache_metrics.prefix_changed is True
assert result3.cache_metrics.stable_prefix_hash != result2.cache_metrics.stable_prefix_hash
class TestToolOrphan:
"""Test that dropping tool_call also drops its tool response."""
def test_tool_unit_atomicity(self):
"""Tool calls and their responses must be dropped together."""
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{
"role": "assistant",
"content": None,
"tool_calls": [
{
"id": "call_1",
"type": "function",
"function": {"name": "search", "arguments": '{"query": "test"}'},
}
],
},
{"role": "tool", "tool_call_id": "call_1", "content": '{"results": ["a", "b", "c"]}'},
{"role": "assistant", "content": "Based on the search, I found 3 results."},
{"role": "user", "content": "Thanks!"},
]
window = RollingWindow()
tokenizer = get_tokenizer()
# Force a very small token limit to trigger dropping
result = window.apply(
messages,
tokenizer,
model_limit=200, # Very small limit
output_buffer=50,
)
# Extract tool_call IDs and tool response IDs from result
tool_call_ids: set[str] = set()
tool_response_ids: set[str] = set()
for msg in result.messages:
if msg.get("tool_calls"):
for tc in msg["tool_calls"]:
tool_call_ids.add(tc.get("id", ""))
if msg.get("role") == "tool":
tool_response_ids.add(msg.get("tool_call_id", ""))
# Every tool response must have a matching tool call
# (no orphaned tool responses)
assert tool_response_ids <= tool_call_ids, (
f"Orphaned tool responses detected! "
f"Tool calls: {tool_call_ids}, Tool responses: {tool_response_ids}"
)
def test_multiple_tool_calls_atomicity(self):
"""Multiple tool calls in one message are handled atomically."""
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{
"role": "assistant",
"content": None,
"tool_calls": [
{
"id": "call_1",
"type": "function",
"function": {"name": "search", "arguments": '{"q": "a"}'},
},
{
"id": "call_2",
"type": "function",
"function": {"name": "search", "arguments": '{"q": "b"}'},
},
],
},
{"role": "tool", "tool_call_id": "call_1", "content": '{"result": "a"}'},
{"role": "tool", "tool_call_id": "call_2", "content": '{"result": "b"}'},
{"role": "assistant", "content": "Found results for both queries."},
{"role": "user", "content": "Great!"},
]
window = RollingWindow()
tokenizer = get_tokenizer()
result = window.apply(
messages,
tokenizer,
model_limit=300,
output_buffer=50,
)
# Verify atomicity
tool_call_ids: set[str] = set()
tool_response_ids: set[str] = set()
for msg in result.messages:
if msg.get("tool_calls"):
for tc in msg["tool_calls"]:
tool_call_ids.add(tc.get("id", ""))
if msg.get("role") == "tool":
tool_response_ids.add(msg.get("tool_call_id", ""))
assert tool_response_ids <= tool_call_ids
def test_many_tool_calls_all_or_nothing(self):
"""MCP-style: one assistant message with MANY tool calls must be atomic."""
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Search everything."},
{
"role": "assistant",
"content": None,
"tool_calls": [
{
"id": "call_1",
"type": "function",
"function": {"name": "search_web", "arguments": '{"q": "a"}'},
},
{
"id": "call_2",
"type": "function",
"function": {"name": "search_files", "arguments": '{"q": "b"}'},
},
{
"id": "call_3",
"type": "function",
"function": {"name": "search_db", "arguments": '{"q": "c"}'},
},
{
"id": "call_4",
"type": "function",
"function": {"name": "search_api", "arguments": '{"q": "d"}'},
},
],
},
{"role": "tool", "tool_call_id": "call_1", "content": '{"results": ["web_result"]}'},
{"role": "tool", "tool_call_id": "call_2", "content": '{"results": ["file_result"]}'},
{"role": "tool", "tool_call_id": "call_3", "content": '{"results": ["db_result"]}'},
{"role": "tool", "tool_call_id": "call_4", "content": '{"results": ["api_result"]}'},
{"role": "assistant", "content": "I found results from all 4 sources."},
{"role": "user", "content": "Thanks!"},
]
window = RollingWindow()
tokenizer = get_tokenizer()
# Force a tight limit to potentially drop the tool unit
result = window.apply(
messages,
tokenizer,
model_limit=400, # Tight limit
output_buffer=50,
)
# Extract tool_call_ids and tool_response_ids
tool_call_ids: set[str] = set()
tool_response_ids: set[str] = set()
for msg in result.messages:
if msg.get("tool_calls"):
for tc in msg["tool_calls"]:
tool_call_ids.add(tc.get("id", ""))
if msg.get("role") == "tool":
tool_response_ids.add(msg.get("tool_call_id", ""))
# KEY ASSERTION: Either ALL 4 tool responses are present, or NONE are
# This verifies the all-or-nothing atomicity
if tool_response_ids:
# If any are present, the assistant must have all the matching tool_calls
assert tool_response_ids <= tool_call_ids
# And the counts should match (all 4 kept together)
assert len(tool_response_ids) == len(tool_call_ids)
else:
# If none are present, the assistant message with tool_calls should be gone too
assert len(tool_call_ids) == 0
class TestStreaming:
"""Test that streaming works correctly."""
def test_stream_passthrough(self):
"""Streaming should pass through chunks correctly."""
# This test requires a mock client since we can't call real APIs
# We'll test the wrapper behavior
class MockChunk:
def __init__(self, content: str):
self.choices = [
type("Choice", (), {"delta": type("Delta", (), {"content": content})()})
]
class MockStream:
def __init__(self):
self.chunks = [MockChunk("Hello"), MockChunk(" "), MockChunk("World")]
self.index = 0
def __iter__(self):
return self
def __next__(self):
if self.index >= len(self.chunks):
raise StopIteration
chunk = self.chunks[self.index]
self.index += 1
return chunk
# The stream wrapper should yield all chunks
stream = MockStream()
chunks = list(stream)
assert len(chunks) == 3
assert all(hasattr(c, "choices") for c in chunks)
def test_stream_metrics_saved(self):
"""Metrics should be saved when stream completes."""
# This would require integration test with mock client
# For unit test, we verify the wrapper generator works
pass
class TestSafetyMalformedJSON:
"""Test that malformed JSON is NOT modified (safety first)."""
def test_malformed_json_unchanged(self):
"""Malformed JSON in tool output should not be modified."""
malformed = '{"key": "value", invalid}'
result, modified = crush_tool_output(malformed)
assert result == malformed, "Malformed JSON should be unchanged"
assert modified is False, "Should report as not modified"
def test_truncated_json_unchanged(self):
"""Truncated JSON should not be modified."""
truncated = '{"key": "value", "nested": {"inner": '
result, modified = crush_tool_output(truncated)
assert result == truncated
assert modified is False
def test_plain_text_unchanged(self):
"""Plain text (non-JSON) should not be modified."""
plain_text = "This is just plain text, not JSON at all."
result, modified = crush_tool_output(plain_text)
assert result == plain_text
assert modified is False
def test_valid_json_can_be_modified(self):
"""Valid JSON should be processed (but may or may not change)."""
valid_json = '{"key": "value"}'
result, modified = crush_tool_output(valid_json)
# Valid JSON is processed - result should still be valid JSON
import json
parsed = json.loads(result)
assert "key" in parsed
def test_large_json_is_crushed(self):
"""Large valid JSON should be crushed."""
import json
# Create large JSON with long array
large_data = {
"results": [{"id": i, "name": f"Item {i}" * 50} for i in range(100)],
"metadata": {"total": 100},
}
large_json = json.dumps(large_data)
result, modified = crush_tool_output(large_json)
if modified:
parsed = json.loads(result)
# Should have truncated array
assert len(parsed["results"]) < 100
class TestQueryAnchorExtraction:
"""Test that query anchors preserve needle records during crushing."""
def test_preserves_needle_by_name(self):
"""If user asks for 'Alice', item with Alice should be preserved."""
import json
from headroom.transforms.smart_crusher import SmartCrusher, SmartCrusherConfig
# User is searching for 'Alice'
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Find the user named 'Alice' in the system."},
{
"role": "assistant",
"content": None,
"tool_calls": [
{
"id": "call_1",
"type": "function",
"function": {"name": "find_users", "arguments": '{"name": "Alice"}'},
}
],
},
{
"role": "tool",
"tool_call_id": "call_1",
"content": json.dumps(
[{"id": i, "name": f"User{i}", "score": 0.1} for i in range(50)]
+ [{"id": 42, "name": "Alice", "score": 0.1}]
), # Alice is at the END, not in first/last K
},
]
# End-to-end behavior: the relevance scorer (HybridScorer in
# the Rust port — BM25 + embedding) should pick up "Alice"
# from the user message and preserve the matching tool item
# even though it sits at index 50.
config = SmartCrusherConfig(
enabled=True,
min_items_to_analyze=5,
min_tokens_to_crush=100,
max_items_after_crush=10,
)
crusher = SmartCrusher(config)
tokenizer = get_tokenizer()
result = crusher.apply(messages, tokenizer)
tool_msg = next(m for m in result.messages if m.get("role") == "tool")
crushed_content = tool_msg["content"]
assert "Alice" in crushed_content
def test_preserves_needle_by_uuid(self):
"""If user asks for a UUID, item with that UUID should be preserved."""
import json
from headroom.transforms.smart_crusher import SmartCrusher, SmartCrusherConfig
target_uuid = "550e8400-e29b-41d4-a716-446655440000"
messages = [
{"role": "system", "content": "You are helpful."},
{"role": "user", "content": f"Get details for request {target_uuid}"},
{
"role": "assistant",
"content": None,
"tool_calls": [
{
"id": "call_1",
"type": "function",
"function": {"name": "get_requests", "arguments": "{}"},
}
],
},
{
"role": "tool",
"tool_call_id": "call_1",
"content": json.dumps(
[{"request_id": f"other-{i}", "status": "ok"} for i in range(50)]
+ [{"request_id": target_uuid, "status": "ok"}]
), # Target at end
},
]
config = SmartCrusherConfig(
enabled=True,
min_items_to_analyze=5,
min_tokens_to_crush=100,
max_items_after_crush=10,
)
crusher = SmartCrusher(config)
tokenizer = get_tokenizer()
result = crusher.apply(messages, tokenizer)
tool_msg = next(m for m in result.messages if m.get("role") == "tool")
crushed_content = tool_msg["content"]
assert target_uuid in crushed_content
class TestTransformIntegration:
"""Integration tests for transform pipeline."""
def test_pipeline_preserves_message_order(self):
"""Transform pipeline should preserve message order."""
from headroom.transforms import TransformPipeline
messages = [
{"role": "system", "content": "You are helpful."},
{"role": "user", "content": "Hello"},
{"role": "assistant", "content": "Hi there!"},
{"role": "user", "content": "How are you?"},
]
pipeline = TransformPipeline(provider=_provider)
result = pipeline.apply(messages, "gpt-4o", model_limit=128000)
# Order should be preserved
roles = [m["role"] for m in result.messages]
assert roles[0] == "system"
assert "user" in roles
assert "assistant" in roles
def test_pipeline_never_removes_user_content(self):
"""User message content should never be removed."""
from headroom.transforms import TransformPipeline
user_content = "This is my important question that should never be modified!"
messages = [
{"role": "system", "content": "You are helpful."},
{"role": "user", "content": user_content},
]
pipeline = TransformPipeline(provider=_provider)
result = pipeline.apply(messages, "gpt-4o", model_limit=128000)
# Find user message
user_messages = [m for m in result.messages if m.get("role") == "user"]
assert len(user_messages) >= 1
# Original user content should be preserved somewhere
all_content = " ".join(m.get("content", "") for m in result.messages)
assert user_content in all_content
if __name__ == "__main__":
pytest.main([__file__, "-v"])