Skip to content

Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.

Add reasoning capability forwarding to HeadroomAgnoModel

chopratejas4ac48ed443fa
diff --git a/headroom/integrations/agno/model.py b/headroom/integrations/agno/model.py
index b9ce80e8b..f6a7abb04 100644
--- a/headroom/integrations/agno/model.py
+++ b/headroom/integrations/agno/model.py
@@ -72,6 +72,21 @@ class HeadroomAgnoModel(Model): # type: ignore[misc]
before each API call. Works with OpenAIChat, Claude, Gemini, and
other Agno model types.
+ Important - Reasoning Modes:
+ Claude's extended thinking and Agno's reasoning flow are INCOMPATIBLE.
+ Choose ONE approach:
+
+ 1. Claude Extended Thinking (native):
+ - Set thinking={"type": "enabled", "budget_tokens": N} on Claude model
+ - Do NOT use reasoning=True on Agent
+ - Claude handles reasoning internally with structured thinking blocks
+
+ 2. Agno Reasoning Flow (framework):
+ - Do NOT set thinking config on the model
+ - Use reasoning=True on Agent
+ - Use underlying_model as reasoning_model for proper detection
+ - Agno handles chain-of-thought with text-based <thinking> tags
+
Example:
from agno.agent import Agent
from agno.models.openai import OpenAIChat
@@ -93,8 +108,18 @@ class HeadroomAgnoModel(Model): # type: ignore[misc]
config = HeadroomConfig(default_mode=HeadroomMode.OPTIMIZE)
optimized = HeadroomAgnoModel(wrapped_model=model, headroom_config=config)
+ # Agno reasoning with HeadroomAgnoModel
+ model = OpenAIChat(id="gpt-4o")
+ wrapped = HeadroomAgnoModel(wrapped_model=model)
+ agent = Agent(
+ model=wrapped,
+ reasoning=True,
+ reasoning_model=wrapped.underlying_model, # Use underlying for detection
+ )
+
Attributes:
wrapped_model: The underlying Agno model
+ underlying_model: Same as wrapped_model, for framework introspection
total_tokens_saved: Running total of tokens saved
metrics_history: List of OptimizationMetrics from recent calls
"""
@@ -137,6 +162,10 @@ class HeadroomAgnoModel(Model): # type: ignore[misc]
if self.provider is None and hasattr(self.wrapped_model, "provider"):
self.provider = self.wrapped_model.provider
+ # Forward capability attributes from wrapped model
+ # These are critical for framework introspection (e.g., Agno reasoning detection)
+ self._forward_capability_attributes()
+
# Initialize config
if self.headroom_config is None:
self.headroom_config = HeadroomConfig()
@@ -155,6 +184,43 @@ class HeadroomAgnoModel(Model): # type: ignore[misc]
if hasattr(super(), "__post_init__"):
super().__post_init__()
+ def _forward_capability_attributes(self) -> None:
+ """Forward capability attributes from wrapped model.
+
+ This ensures that framework introspection (like Agno's reasoning detection)
+ can access capabilities like 'thinking', 'reasoning_effort', etc.
+ through the wrapper.
+ """
+ # Reasoning-related attributes
+ capability_attrs = [
+ "thinking", # Claude extended thinking
+ "reasoning_effort", # OpenAI reasoning effort
+ "supports_native_structured_outputs", # Structured output support
+ "supports_json_schema_outputs", # JSON schema support
+ ]
+
+ for attr in capability_attrs:
+ if hasattr(self.wrapped_model, attr):
+ value = getattr(self.wrapped_model, attr)
+ # Use object.__setattr__ to bypass any dataclass restrictions
+ object.__setattr__(self, attr, value)
+
+ def has_extended_thinking_enabled(self) -> bool:
+ """Check if the wrapped model has extended thinking enabled.
+
+ Extended thinking is a Claude-specific feature that uses structured
+ content blocks. It is INCOMPATIBLE with Agno's reasoning flow.
+
+ Returns:
+ True if extended thinking is configured on the wrapped model.
+ """
+ thinking = getattr(self.wrapped_model, "thinking", None)
+ if thinking is None:
+ return False
+ if isinstance(thinking, dict):
+ return thinking.get("type") == "enabled"
+ return bool(thinking)
+
# Forward attribute access to wrapped model for compatibility
def __getattr__(self, name: str) -> Any:
"""Forward attribute access to wrapped model."""
@@ -172,10 +238,33 @@ class HeadroomAgnoModel(Model): # type: ignore[misc]
"id",
"name",
"provider",
+ "underlying_model",
):
raise AttributeError(f"'{type(self).__name__}' has no attribute '{name}'")
return getattr(self.wrapped_model, name)
+ # =========================================================================
+ # Capability property: underlying_model for type introspection
+ # =========================================================================
+
+ @property
+ def underlying_model(self) -> Any:
+ """Return the underlying model for type introspection.
+
+ Frameworks like Agno that need to check model capabilities
+ (e.g., native reasoning detection via __class__.__name__) can use
+ this to access the actual model class.
+
+ Example:
+ wrapped = HeadroomAgnoModel(wrapped_model=Claude(...))
+ actual_class = wrapped.underlying_model.__class__.__name__ # "Claude"
+ """
+ return self.wrapped_model
+
+ # =========================================================================
+ # Pipeline and metrics
+ # =========================================================================
+
@property
def pipeline(self) -> TransformPipeline:
"""Lazily initialize TransformPipeline (thread-safe)."""
@@ -207,21 +296,47 @@ class HeadroomAgnoModel(Model): # type: ignore[misc]
return self._metrics_history.copy()
def _convert_messages_to_openai(self, messages: list[Any]) -> list[dict[str, Any]]:
- """Convert Agno messages to OpenAI format for Headroom."""
+ """Convert Agno messages to OpenAI format for Headroom.
+
+ Preserves extended thinking content blocks (thinking, redacted_thinking)
+ which must be passed through unchanged for Claude's extended thinking API.
+ """
result = []
for msg in messages:
# Handle Agno Message objects
if hasattr(msg, "role") and hasattr(msg, "content"):
entry: dict[str, Any] = {
"role": msg.role,
- "content": msg.content if msg.content is not None else "",
}
+
+ # Handle content - can be string or list of content blocks
+ content = msg.content
+ if content is None:
+ entry["content"] = ""
+ elif isinstance(content, list):
+ # Content blocks (e.g., extended thinking)
+ # Preserve the structure - don't stringify
+ entry["content"] = content
+ else:
+ entry["content"] = content
+
# Handle tool calls
if hasattr(msg, "tool_calls") and msg.tool_calls:
entry["tool_calls"] = msg.tool_calls
# Handle tool call ID for tool responses
if hasattr(msg, "tool_call_id") and msg.tool_call_id:
entry["tool_call_id"] = msg.tool_call_id
+
+ # Preserve reasoning content for extended thinking
+ if hasattr(msg, "reasoning_content") and msg.reasoning_content:
+ entry["reasoning_content"] = msg.reasoning_content
+ if hasattr(msg, "redacted_reasoning_content") and msg.redacted_reasoning_content:
+ entry["redacted_reasoning_content"] = msg.redacted_reasoning_content
+
+ # Preserve provider_data which may contain thinking signatures
+ if hasattr(msg, "provider_data") and msg.provider_data:
+ entry["provider_data"] = msg.provider_data
+
result.append(entry)
# Handle dict format
elif isinstance(msg, dict):
@@ -238,6 +353,9 @@ class HeadroomAgnoModel(Model): # type: ignore[misc]
Agno's base Model methods call _log_messages() which requires
Message objects with a .log() method.
+ Preserves extended thinking fields (reasoning_content, provider_data, etc.)
+ which are critical for Claude's extended thinking API.
+
Args:
messages: List of messages (may be dicts or Message objects)
@@ -253,13 +371,17 @@ class HeadroomAgnoModel(Model): # type: ignore[misc]
try:
result.append(AgnoMessage.from_dict(msg))
except Exception:
- # If from_dict fails, create a simple Message
+ # If from_dict fails, create a Message with all relevant fields
result.append(
AgnoMessage(
role=msg.get("role", "user"),
content=msg.get("content"),
tool_calls=msg.get("tool_calls"),
tool_call_id=msg.get("tool_call_id"),
+ # Extended thinking fields
+ reasoning_content=msg.get("reasoning_content"),
+ redacted_reasoning_content=msg.get("redacted_reasoning_content"),
+ provider_data=msg.get("provider_data"),
)
)
else:
@@ -285,10 +407,37 @@ class HeadroomAgnoModel(Model): # type: ignore[misc]
# Reuse the ensure method which handles the conversion
return self._ensure_message_objects(messages)
+ def _has_thinking_blocks(self, messages: list[dict[str, Any]]) -> bool:
+ """Check if any message contains extended thinking content blocks.
+
+ Extended thinking blocks (thinking, redacted_thinking) must not be
+ modified and require special handling by Claude's API.
+
+ Args:
+ messages: List of messages in dict format
+
+ Returns:
+ True if any message contains thinking blocks
+ """
+ for msg in messages:
+ content = msg.get("content")
+ if isinstance(content, list):
+ for block in content:
+ if isinstance(block, dict):
+ block_type = block.get("type", "")
+ if block_type in ("thinking", "redacted_thinking"):
+ return True
+ # Also check for reasoning_content which indicates thinking was used
+ if msg.get("reasoning_content") or msg.get("redacted_reasoning_content"):
+ return True
+ return False
+
def _optimize_messages(self, messages: list[Any]) -> tuple[list[Any], OptimizationMetrics]:
"""Apply Headroom optimization to messages.
Thread-safe with fallback on pipeline errors.
+ Skips optimization for messages with extended thinking blocks to preserve
+ Claude's thinking content structure.
"""
request_id = str(uuid4())
@@ -312,6 +461,26 @@ class HeadroomAgnoModel(Model): # type: ignore[misc]
# Get model name from wrapped model
model = get_model_name_from_agno(self.wrapped_model)
+ # Skip optimization for messages with extended thinking blocks
+ # Thinking blocks must be passed through unchanged for Claude's API
+ if self._has_thinking_blocks(openai_messages):
+ logger.info("Skipping Headroom optimization: messages contain extended thinking blocks")
+ # Estimate token count (rough approximation)
+ tokens_estimate = sum(len(str(m.get("content", ""))) // 4 for m in openai_messages)
+ metrics = OptimizationMetrics(
+ request_id=request_id,
+ timestamp=datetime.now(timezone.utc),
+ tokens_before=tokens_estimate,
+ tokens_after=tokens_estimate,
+ tokens_saved=0,
+ savings_percent=0,
+ transforms_applied=["skipped:extended_thinking"],
+ model=model,
+ )
+ # Convert back to Agno Message objects
+ result_messages = self._convert_messages_from_openai(openai_messages, messages)
+ return result_messages, metrics
+
# Ensure pipeline is initialized
_ = self.pipeline
diff --git a/headroom/transforms/anchor_selector.py b/headroom/transforms/anchor_selector.py
index 119e3929b..0875eb1e2 100644
--- a/headroom/transforms/anchor_selector.py
+++ b/headroom/transforms/anchor_selector.py
@@ -200,7 +200,9 @@ def _calculate_value_uniqueness(item: dict[str, Any], all_items: list[dict[str,
field_value_counts[key] = Counter()
# Convert to string for hashability
try:
- value_str = json.dumps(value, sort_keys=True) if not isinstance(value, str) else value
+ value_str = (
+ json.dumps(value, sort_keys=True) if not isinstance(value, str) else value
+ )
except (TypeError, ValueError):
value_str = str(value)
field_value_counts[key][value_str] += 1
diff --git a/headroom/transforms/smart_crusher.py b/headroom/transforms/smart_crusher.py
index d54104506..96274fcc6 100644
--- a/headroom/transforms/smart_crusher.py
+++ b/headroom/transforms/smart_crusher.py
@@ -1418,7 +1418,7 @@ class SmartCrusher(Transform):
self._relevance_threshold = rel_cfg.relevance_threshold
# Initialize AnchorSelector for dynamic position-based preservation
- anchor_config = self.config.anchor if hasattr(self.config, 'anchor') else AnchorConfig()
+ anchor_config = self.config.anchor if hasattr(self.config, "anchor") else AnchorConfig()
self._anchor_selector = AnchorSelector(anchor_config)
# NOTE: Error detection now uses structural outlier detection (_detect_structural_outliers)
@@ -1629,9 +1629,7 @@ class SmartCrusher(Transform):
item_hash = hashlib.sha256(content.encode()).hexdigest()[:16]
except (TypeError, ValueError, RecursionError) as e:
# Serialization failed - keep the item (fail-safe)
- logger.debug(
- "Dedup hash failed for item at index %d: %s. Keeping item.", idx, e
- )
+ logger.debug("Dedup hash failed for item at index %d: %s. Keeping item.", idx, e)
# Use index as unique "hash" to ensure item is kept
item_hash = f"__idx_{idx}__"
@@ -1814,9 +1812,7 @@ class SmartCrusher(Transform):
# Instead of returning a sparse result, fill remaining slots with
# diverse items from the array. This maximizes information density.
if len(keep_indices) < effective_max and len(keep_indices) < n:
- keep_indices = self._fill_remaining_slots(
- keep_indices, items, n, effective_max
- )
+ keep_indices = self._fill_remaining_slots(keep_indices, items, n, effective_max)
if len(keep_indices) <= effective_max:
return keep_indices
diff --git a/tests/test_integrations/agno/test_model.py b/tests/test_integrations/agno/test_model.py
index 9ddd3687c..0f0c97ab7 100644
--- a/tests/test_integrations/agno/test_model.py
+++ b/tests/test_integrations/agno/test_model.py
@@ -610,6 +610,203 @@ class TestIntegrationWithRealHeadroom:
assert metrics.tokens_after >= 0
+class TestReasoningCapabilityForwarding:
+ """Tests for reasoning capability forwarding in HeadroomAgnoModel.
+
+ These tests verify that HeadroomAgnoModel properly forwards
+ reasoning-related properties from the wrapped model, enabling
+ framework introspection (e.g., Agno's reasoning detection).
+ """
+
+ def test_underlying_model_property_returns_wrapped_model(self, mock_agno_model):
+ """underlying_model property should return the wrapped model."""
+ from headroom.integrations.agno import HeadroomAgnoModel
+
+ wrapped = HeadroomAgnoModel(wrapped_model=mock_agno_model)
+
+ assert wrapped.underlying_model is mock_agno_model
+
+ def test_underlying_model_class_introspection(self):
+ """underlying_model allows class name introspection for framework detection."""
+ from agno.models.openai import OpenAIChat
+
+ from headroom.integrations.agno import HeadroomAgnoModel
+
+ base_model = OpenAIChat(id="gpt-4o")
+ wrapped = HeadroomAgnoModel(wrapped_model=base_model)
+
+ # Framework detection typically checks __class__.__name__
+ assert wrapped.underlying_model.__class__.__name__ == "OpenAIChat"
+ assert wrapped.__class__.__name__ == "HeadroomAgnoModel"
+
+ def test_thinking_property_forwarded_when_present(self, mock_agno_model):
+ """thinking property is forwarded from wrapped model when present."""
+ from headroom.integrations.agno import HeadroomAgnoModel
+
+ # Set thinking config on mock model
+ mock_agno_model.thinking = {"type": "enabled", "budget_tokens": 5000}
+
+ wrapped = HeadroomAgnoModel(wrapped_model=mock_agno_model)
+
+ assert wrapped.thinking == {"type": "enabled", "budget_tokens": 5000}
+
+ def test_thinking_property_not_present_when_absent(self, mock_agno_model):
+ """thinking property not set when wrapped model doesn't have it."""
+ from headroom.integrations.agno import HeadroomAgnoModel
+
+ # Ensure mock doesn't have thinking attribute
+ if hasattr(mock_agno_model, "thinking"):
+ delattr(mock_agno_model, "thinking")
+
+ wrapped = HeadroomAgnoModel(wrapped_model=mock_agno_model)
+
+ # Should raise AttributeError when accessed
+ assert not hasattr(wrapped, "thinking") or wrapped.thinking is None
+
+ def test_reasoning_effort_property_forwarded(self, mock_agno_model):
+ """reasoning_effort property is forwarded from wrapped model."""
+ from headroom.integrations.agno import HeadroomAgnoModel
+
+ mock_agno_model.reasoning_effort = "high"
+
+ wrapped = HeadroomAgnoModel(wrapped_model=mock_agno_model)
+
+ assert wrapped.reasoning_effort == "high"
+
+ def test_provider_property_forwarded_from_wrapped_model(self, mock_agno_model):
+ """provider property is set from wrapped model during init."""
+ from headroom.integrations.agno import HeadroomAgnoModel
+
+ mock_agno_model.provider = "OpenAI"
+
+ wrapped = HeadroomAgnoModel(wrapped_model=mock_agno_model)
+
+ assert wrapped.provider == "OpenAI"
+
+ def test_name_property_forwarded_from_wrapped_model(self, mock_agno_model):
+ """name property is set from wrapped model during init."""
+ from headroom.integrations.agno import HeadroomAgnoModel
+
+ mock_agno_model.name = "gpt-4o"
+
+ wrapped = HeadroomAgnoModel(wrapped_model=mock_agno_model)
+
+ assert wrapped.name == "gpt-4o"
+
+ def test_has_extended_thinking_enabled_with_dict_config(self, mock_agno_model):
+ """has_extended_thinking_enabled returns True when thinking dict is enabled."""
+ from headroom.integrations.agno import HeadroomAgnoModel
+
+ mock_agno_model.thinking = {"type": "enabled", "budget_tokens": 5000}
+
+ wrapped = HeadroomAgnoModel(wrapped_model=mock_agno_model)
+
+ assert wrapped.has_extended_thinking_enabled() is True
+
+ def test_has_extended_thinking_disabled_with_dict_config(self, mock_agno_model):
+ """has_extended_thinking_enabled returns False when thinking dict is disabled."""
+ from headroom.integrations.agno import HeadroomAgnoModel
+
+ mock_agno_model.thinking = {"type": "disabled"}
+
+ wrapped = HeadroomAgnoModel(wrapped_model=mock_agno_model)
+
+ assert wrapped.has_extended_thinking_enabled() is False
+
+ def test_has_extended_thinking_returns_false_when_none(self, mock_agno_model):
+ """has_extended_thinking_enabled returns False when thinking is None."""
+ from headroom.integrations.agno import HeadroomAgnoModel
+
+ mock_agno_model.thinking = None
+
+ wrapped = HeadroomAgnoModel(wrapped_model=mock_agno_model)
+
+ assert wrapped.has_extended_thinking_enabled() is False
+
+ def test_has_extended_thinking_returns_false_when_missing(self, mock_agno_model):
+ """has_extended_thinking_enabled returns False when thinking attribute missing."""
+ from headroom.integrations.agno import HeadroomAgnoModel
+
+ # Remove thinking attribute if present
+ if hasattr(mock_agno_model, "thinking"):
+ delattr(mock_agno_model, "thinking")
+
+ wrapped = HeadroomAgnoModel(wrapped_model=mock_agno_model)
+
+ assert wrapped.has_extended_thinking_enabled() is False
+
+ def test_has_extended_thinking_with_truthy_value(self, mock_agno_model):
+ """has_extended_thinking_enabled handles non-dict truthy values."""
+ from headroom.integrations.agno import HeadroomAgnoModel
+
+ mock_agno_model.thinking = True
+
+ wrapped = HeadroomAgnoModel(wrapped_model=mock_agno_model)
+
+ assert wrapped.has_extended_thinking_enabled() is True
+
+ def test_has_extended_thinking_with_falsy_value(self, mock_agno_model):
+ """has_extended_thinking_enabled handles non-dict falsy values."""
+ from headroom.integrations.agno import HeadroomAgnoModel
+
+ mock_agno_model.thinking = False
+
+ wrapped = HeadroomAgnoModel(wrapped_model=mock_agno_model)
+
+ assert wrapped.has_extended_thinking_enabled() is False
+
+ def test_supports_native_structured_outputs_forwarded(self, mock_agno_model):
+ """supports_native_structured_outputs property is forwarded."""
+ from headroom.integrations.agno import HeadroomAgnoModel
+
+ mock_agno_model.supports_native_structured_outputs = True
+
+ wrapped = HeadroomAgnoModel(wrapped_model=mock_agno_model)
+
+ assert wrapped.supports_native_structured_outputs is True
+
+ def test_supports_json_schema_outputs_forwarded(self, mock_agno_model):
+ """supports_json_schema_outputs property is forwarded."""
+ from headroom.integrations.agno import HeadroomAgnoModel
+
+ mock_agno_model.supports_json_schema_outputs = True
+
+ wrapped = HeadroomAgnoModel(wrapped_model=mock_agno_model)
+
+ assert wrapped.supports_json_schema_outputs is True
+
+ def test_multiple_capability_properties_forwarded(self, mock_agno_model):
+ """Multiple capability properties are forwarded correctly."""
+ from headroom.integrations.agno import HeadroomAgnoModel
+
+ mock_agno_model.thinking = {"type": "enabled", "budget_tokens": 10000}
+ mock_agno_model.reasoning_effort = "medium"
+ mock_agno_model.supports_native_structured_outputs = True
+ mock_agno_model.supports_json_schema_outputs = False
+ mock_agno_model.provider = "Anthropic"
+
+ wrapped = HeadroomAgnoModel(wrapped_model=mock_agno_model)
+
+ assert wrapped.thinking == {"type": "enabled", "budget_tokens": 10000}
+ assert wrapped.reasoning_effort == "medium"
+ assert wrapped.supports_native_structured_outputs is True
+ assert wrapped.supports_json_schema_outputs is False
+ assert wrapped.provider == "Anthropic"
+
+ def test_underlying_model_with_real_openai_model(self):
+ """Test underlying_model with real Agno OpenAIChat model."""
+ from agno.models.openai import OpenAIChat
+
+ from headroom.integrations.agno import HeadroomAgnoModel
+
+ base_model = OpenAIChat(id="gpt-4o")
+ wrapped = HeadroomAgnoModel(wrapped_model=base_model)
+
+ # Verify underlying_model returns the actual model
+ assert wrapped.underlying_model is base_model
+ assert isinstance(wrapped.underlying_model, OpenAIChat)
+
+
class TestRealAgnoIntegration:
"""REAL integration tests with actual Agno components.
diff --git a/tests/test_transforms/test_anchor_selector.py b/tests/test_transforms/test_anchor_selector.py
index 79cc15f80..aa73c4c70 100644
--- a/tests/test_transforms/test_anchor_selector.py
+++ b/tests/test_transforms/test_anchor_selector.py
@@ -14,20 +14,15 @@ preservation with adaptive, pattern-aware anchor selection.
"""
import json
-from dataclasses import dataclass
-from enum import Enum
-from typing import Any
import pytest
from headroom import OpenAIProvider, SmartCrusherConfig, Tokenizer
from headroom.transforms.smart_crusher import (
- CompressionStrategy,
SmartAnalyzer,
SmartCrusher,
)
-
# =============================================================================
# Test Fixtures
# =============================================================================
@@ -211,9 +206,7 @@ def crush_items(
}
)
- messages.append(
- {"role": "tool", "tool_call_id": "call_1", "content": json.dumps(items)}
- )
+ messages.append({"role": "tool", "tool_call_id": "call_1", "content": json.dumps(items)})
config = SmartCrusherConfig(
enabled=True,
@@ -329,7 +322,7 @@ class TestAdversarialPositions:
crushed = crush_items(items, tokenizer, max_items=15)
# All spikes should be preserved
- preserved_ids = set(item.get("id") for item in crushed)
+ preserved_ids = {item.get("id") for item in crushed}
for pos in spike_positions:
assert pos in preserved_ids, f"Spike at position {pos} should be preserved"
@@ -342,10 +335,7 @@ class TestAdversarialPositions:
# First 10 items are identical
identical_items = [{"id": "same", "value": 0, "type": "duplicate"} for _ in range(10)]
# Rest are unique
- unique_items = [
- {"id": f"unique_{i}", "value": i * 10, "type": "unique"}
- for i in range(90)
- ]
+ unique_items = [{"id": f"unique_{i}", "value": i * 10, "type": "unique"} for i in range(90)]
items = identical_items + unique_items
crushed = crush_items(items, tokenizer, max_items=10)
@@ -365,10 +355,7 @@ class TestAdversarialPositions:
we should preserve variety instead of duplicates.
"""
# First 90 items are unique
- unique_items = [
- {"id": f"unique_{i}", "value": i * 10, "type": "unique"}
- for i in range(90)
- ]
+ unique_items = [{"id": f"unique_{i}", "value": i * 10, "type": "unique"} for i in range(90)]
# Last 10 items are identical
identical_items = [{"id": "same", "value": 100, "type": "duplicate"} for _ in range(10)]
items = unique_items + identical_items
@@ -385,10 +372,7 @@ class TestAdversarialPositions:
When user queries for a specific item that's at position 42,
it should be preserved even though it's not at front/back.
"""
- items = [
- {"id": i, "name": f"item_{i}", "status": "active"}
- for i in range(100)
- ]
+ items = [{"id": i, "name": f"item_{i}", "status": "active"} for i in range(100)]
# Put target item in the middle
items[42]["name"] = "target_special_item"
items[42]["description"] = "This is what the user is looking for"
@@ -432,10 +416,10 @@ class TestSizeAdaptation:
@pytest.mark.parametrize(
"size,max_items,expected_min_anchors",
[
- (20, 10, 3), # Small array: at least 3 anchors (front + back)
+ (20, 10, 3), # Small array: at least 3 anchors (front + back)
(100, 15, 4), # Medium array: at least 4 anchors
(500, 20, 5), # Large array: at least 5 anchors
- (2000, 25, 6), # Very large: at least 6 anchors
+ (2000, 25, 6), # Very large: at least 6 anchors
],
)
def test_anchor_count_scales(self, tokenizer, size, max_items, expected_min_anchors):
@@ -453,9 +437,7 @@ class TestSizeAdaptation:
back_boundary = int(size * 0.9)
anchor_count = sum(
- 1
- for item in crushed
- if item["id"] < front_boundary or item["id"] >= back_boundary
+ 1 for item in crushed if item["id"] < front_boundary or item["id"] >= back_boundary
)
assert anchor_count >= expected_min_anchors, (
@@ -619,9 +601,7 @@ class TestPatternAwareAnchoring:
positions = get_preserved_positions(crushed, 100)
# Should have items from multiple regions
- regions_with_items = sum(
- 1 for region in ["front", "middle", "back"] if positions[region]
- )
+ regions_with_items = sum(1 for region in ["front", "middle", "back"] if positions[region])
assert regions_with_items >= 2, (
f"Generic data should cover multiple regions, got {regions_with_items}"
@@ -654,7 +634,9 @@ class TestQueryAwareAnchoring:
recent_count = sum(1 for id in ids if id > 20)
# Should have multiple recent items due to "latest" keyword
- assert recent_count >= 2, f"Query with 'latest' should preserve recent items, got {recent_count}"
+ assert recent_count >= 2, (
+ f"Query with 'latest' should preserve recent items, got {recent_count}"
+ )
def test_recent_query_shifts_to_back(self, tokenizer):
"""'Recent' in query should preserve more recent items."""
@@ -689,7 +671,9 @@ class TestQueryAwareAnchoring:
early_count = sum(1 for id in ids if id < 10)
# Should have multiple early items due to "first" keyword
- assert early_count >= 2, f"Query with 'first' should preserve early items, got {early_count}"
+ assert early_count >= 2, (
+ f"Query with 'first' should preserve early items, got {early_count}"
+ )
def test_oldest_query_shifts_to_front(self, tokenizer):
"""'Oldest' in query should preserve earlier items."""
@@ -771,9 +755,7 @@ class TestInformationDensity:
# First 6 items are identical
items = [{"id": "dup", "value": 0, "constant": "same"} for _ in range(6)]
# Rest are unique
- items.extend(
- [{"id": f"uniq_{i}", "value": i * 10, "unique": True} for i in range(94)]
- )
+ items.extend([{"id": f"uniq_{i}", "value": i * 10, "unique": True} for i in range(94)])
crushed = crush_items(items, tokenizer, max_items=12)
@@ -811,11 +793,13 @@ class TestInformationDensity:
items = []
# Create items with varying diversity
for i in range(100):
- items.append({
- "id": i,
- "type": "common" if i < 90 else f"rare_type_{i}",
- "value": i,
- })
+ items.append(
+ {
+ "id": i,
+ "type": "common" if i < 90 else f"rare_type_{i}",
+ "value": i,
+ }
+ )
crushed = crush_items(items, tokenizer, max_items=15)
@@ -857,7 +841,7 @@ class TestCoverageMetrics:
crushed = crush_items(items, tokenizer, max_items=12)
- categories = set(item["category"] for item in crushed)
+ categories = {item["category"] for item in crushed}
# Should have at least 2 of 3 categories represented
assert len(categories) >= 2, f"Should cover multiple categories, got {categories}"
@@ -922,8 +906,6 @@ class TestEdgeCases:
def test_empty_array(self, tokenizer):
"""Empty array should return empty result."""
- items: list[dict] = []
-
messages = [
{"role": "system", "content": "You are helpful."},
{"role": "tool", "tool_call_id": "call_1", "content": "[]"},
@@ -998,10 +980,7 @@ class TestEdgeCases:
NOTE: Future AnchorSelector may implement error deduplication or sampling
for cases where all items are errors, but that requires careful design.
"""
- items = [
- {"id": i, "status": "error", "error_code": f"ERR_{i}"}
- for i in range(50)
- ]
+ items = [{"id": i, "status": "error", "error_code": f"ERR_{i}"} for i in range(50)]
crushed = crush_items(items, tokenizer, max_items=15)
@@ -1056,10 +1035,7 @@ class TestEdgeCases:
def test_very_long_string_values(self, tokenizer):
"""Items with very long string values should be handled."""
- items = [
- {"id": i, "data": "x" * 10000 if i == 10 else "short"}
- for i in range(30)
- ]
+ items = [{"id": i, "data": "x" * 10000 if i == 10 else "short"} for i in range(30)]
crushed = crush_items(items, tokenizer, max_items=10)
@@ -1068,6 +1044,7 @@ class TestEdgeCases:
def test_deeply_nested_items(self, tokenizer):
"""Deeply nested items should be handled without stack overflow."""
+
def create_nested(depth: int) -> dict:
if depth == 0:
return {"value": "leaf"}