headroom/tests/test_transforms/test_smart_crusher_bugs.py
Abhay Singh 3bb02f8f75
fix(transforms/smart_crusher): don't crash on a tool call with a null function (#2232)
## Description

A tool call whose `function` field is explicitly `null` crashes
SmartCrusher's per-request context extraction.

`_extract_context_from_messages` (called at the top of `apply()`) walks
recent assistant tool calls:

```python
for tc in msg.get("tool_calls", []):
    if isinstance(tc, dict):
        func = tc.get("function", {})
        args = func.get("arguments", "")
```

`dict.get("function", {})` only substitutes `{}` when the key is
**missing**. When the key is present but `null` — `{"id": "1", "type":
"function", "function": null}`, which clients emit for a partial or
streamed tool call — `func` is `None`, and `None.get("arguments")`
raises `AttributeError`. That propagates out of
`_extract_context_from_messages` and crashes `apply()` for the entire
request, so the request either errors or has to fail open to
uncompressed with a logged traceback.

The sibling `_build_tool_name_index` in the same file already guards
this exact shape with `(tc.get("function") or {})` — this call site just
wasn't updated to match.

## Fix

Use the same null-safe form:

```python
func = tc.get("function") or {}
```

`None` (and any other falsy value) now collapses to `{}`, the null tool
call contributes no context, and extraction continues to the next call.

Closes #

## Type of Change

- [x] Bug fix (non-breaking change that fixes an issue)
- [ ] New feature (non-breaking change that adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
- [ ] Documentation update
- [ ] Performance improvement
- [ ] Code refactoring (no functional changes)

## Changes Made

- `headroom/transforms/smart_crusher.py`: `tc.get("function", {})` →
`tc.get("function") or {}` in `_extract_context_from_messages`.
- `tests/test_transforms/test_smart_crusher_bugs.py`: new test asserting
a `{"function": null}` tool call doesn't crash extraction and later
calls are still read.
- `CHANGELOG.md`: Bug Fixes entry.

## Testing

- [ ] Unit tests pass (`pytest`)
- [x] Linting passes (`ruff check .`)
- [x] Type checking passes (`mypy headroom`)
- [x] New tests added for new functionality
- [ ] Manual testing performed

### Test Output

```text
$ uvx ruff@0.15.17 check headroom/transforms/smart_crusher.py tests/test_transforms/test_smart_crusher_bugs.py
All checks passed!
$ uvx mypy@1.20.2 --ignore-missing-imports headroom/transforms/smart_crusher.py
Success: no issues found in 1 source file
```

## Real Behavior Proof

- Environment: Windows 11, Python 3.12, `uvx ruff@0.15.17` / `uvx
mypy@1.20.2`. A full `pytest` OOM-kills this box (ML stack import), so I
reproduced the extraction loop with a dependency-free script and left
the full pytest to CI.
- Exact command / steps: ran an assistant message with tool calls
`[{"function": null}, {"function": {"arguments": "keep-me"}}]` through
the OLD `get("function", {})` loop and the NEW `get("function") or {}`
loop.
- Observed result: OLD raises `AttributeError` on the null function; NEW
skips it and returns `"keep-me"` from the following call.
- Not tested: a live proxy request carrying a null-function tool call;
full local `pytest` deferred to CI (OOM).

## Review Readiness

- [x] I have performed a self-review
- [x] This PR is ready for human review

## Checklist

- [x] My code follows the project's style guidelines
- [x] I have performed a self-review of my code
- [x] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [x] I have added tests that prove my fix is effective or that my
feature works
- [ ] New and existing unit tests pass locally with my changes
- [x] I have updated the CHANGELOG.md if applicable

## Additional Notes

The "unit tests pass locally" box is unchecked because the full suite
imports the ML stack, which I can't run here. The new test uses the
existing `_make_crusher` helper in
`tests/test_transforms/test_smart_crusher_bugs.py`, so it runs under the
normal CI pytest job; behaviour is additionally verified by the
standalone proof above.

---------

Co-authored-by: JerrettDavis <mxjerrett@gmail.com>
2026-08-11 23:39:37 -05:00

194 lines
7.7 KiB
Python

"""Regression tests for SmartCrusher bugs.
Bug 1: _crush_number_array mixes types (string summary + numbers),
violating the schema-preserving guarantee.
Bug 2: _current_field_semantics is shared instance state, creating
a race condition when crushing concurrently.
"""
from __future__ import annotations
import json
from concurrent.futures import ThreadPoolExecutor, as_completed
from headroom import SmartCrusherConfig
from headroom.transforms.smart_crusher import SmartCrusher
# ---------------------------------------------------------------------------
# Fixtures
# ---------------------------------------------------------------------------
def _make_crusher(max_items: int = 10, min_items: int = 3) -> SmartCrusher:
"""Build a SmartCrusher with deterministic small-K config for tests."""
config = SmartCrusherConfig(
enabled=True,
min_items_to_analyze=min_items,
min_tokens_to_crush=0,
max_items_after_crush=max_items,
variance_threshold=2.0,
)
return SmartCrusher(config=config)
# Bug #1 (number array schema preservation) — invariant pinned by the
# Rust port (`crates/headroom-core/src/transforms/smart_crusher/crushers.rs::
# crush_number_array` + its unit tests) and the parity fixtures
# (`tests/parity/fixtures/smart_crusher/number_array_40_changepoint*`).
# The Python `_crush_number_array` helper that the previous tests
# probed was removed when the Python implementation was retired in
# Stage 3c.1b.
# ---------------------------------------------------------------------------
# Bug 2: Race condition on _current_field_semantics
# ---------------------------------------------------------------------------
class TestFieldSemanticsThreadSafety:
"""_current_field_semantics must not leak between concurrent crushes.
Previously it was stored as instance state (self._current_field_semantics)
which created a race condition when the same SmartCrusher instance
was used from multiple threads.
"""
def test_concurrent_crushes_no_cross_contamination(self) -> None:
"""Two concurrent crushes must not share field_semantics state."""
crusher = _make_crusher(max_items=5)
# Two different array payloads
payload_a = json.dumps([{"name": f"item_{i}", "value": i} for i in range(20)])
payload_b = json.dumps([{"key": f"k_{i}", "score": i * 0.1} for i in range(20)])
results: dict[str, str] = {}
errors: list[Exception] = []
def crush_task(label: str, content: str) -> None:
try:
result, modified, info = crusher._smart_crush_content(content)
results[label] = result
except Exception as e:
errors.append(e)
with ThreadPoolExecutor(max_workers=4) as executor:
futures = []
# Run many concurrent crushes to increase race probability
for i in range(20):
futures.append(executor.submit(crush_task, f"a_{i}", payload_a))
futures.append(executor.submit(crush_task, f"b_{i}", payload_b))
for f in as_completed(futures):
f.result() # Re-raise exceptions
assert not errors, f"Concurrent crushes raised errors: {errors}"
# After all crushes, thread-local state must be clean
tl = getattr(crusher, "_thread_local", None)
if tl is not None:
semantics = getattr(tl, "field_semantics", None)
assert semantics is None, f"field_semantics leaked in thread-local: {semantics}"
# ---------------------------------------------------------------------------
# Issue 7: Recursion depth limit
# ---------------------------------------------------------------------------
class TestRecursionDepthLimit:
"""_process_value must not crash on deeply nested JSON."""
def test_deeply_nested_json_does_not_crash(self) -> None:
"""Nesting deeper than _MAX_PROCESS_DEPTH should return value unchanged."""
crusher = _make_crusher()
# Build a 100-level nested structure
nested: dict = {"leaf": "value"}
for _i in range(100):
nested = {"level": nested}
content = json.dumps(nested)
result, was_modified, info = crusher._smart_crush_content(content)
# Should not raise RecursionError
parsed = json.loads(result)
# The deep structure should be preserved (returned as-is past depth limit)
assert isinstance(parsed, dict)
def test_deeply_nested_list_does_not_crash(self) -> None:
"""Deeply nested lists should also be handled safely."""
crusher = _make_crusher()
nested: list = ["leaf"]
for _i in range(100):
nested = [nested]
content = json.dumps(nested)
result, was_modified, info = crusher._smart_crush_content(content)
parsed = json.loads(result)
assert isinstance(parsed, list)
class TestLosslessOnlyMode:
"""`lossless_only` produces marker-free, byte-recoverable output.
Strict mode: lossless tabular compaction still applies, but any path
that would need a CCR marker (lossy row-drop OR opaque-blob offload)
leaves the content uncompacted instead — so the result is always
marker-free and decodes back to the original input without loss.
"""
def _droppable_rows(self) -> list[dict]:
return [{"path": "a.py", "line": i, "content": "x" * 300} for i in range(50)]
def test_lossless_only_is_marker_free_and_byte_recoverable(self) -> None:
rows = self._droppable_rows()
config = SmartCrusherConfig(
min_items_to_analyze=3,
min_tokens_to_crush=0,
lossless_min_savings_ratio=0.99, # force the would-be-lossy path
lossless_only=True,
)
out = SmartCrusher(config=config).crush(json.dumps(rows))
assert "<<ccr:" not in out.compressed
assert json.loads(out.compressed) == rows
def test_crush_kwarg_overrides_configured_mode(self) -> None:
# Configured non-strict, but the per-call kwarg forces strict mode
# for this call: marker-free and fully recoverable.
rows = self._droppable_rows()
config = SmartCrusherConfig(
min_items_to_analyze=3,
min_tokens_to_crush=0,
lossless_min_savings_ratio=0.99,
)
crusher = SmartCrusher(config=config)
out = crusher.crush(json.dumps(rows), lossless_only=True)
assert "<<ccr:" not in out.compressed
assert json.loads(out.compressed) == rows
def test_extract_context_survives_null_function_tool_call() -> None:
# A tool_call with an explicit {"function": null} must not crash context
# extraction: `dict.get("function", {})` returns None for a present-but-null
# key, and `.get` on None raises AttributeError inside apply().
crusher = _make_crusher()
messages = [
{
"role": "assistant",
"tool_calls": [
{"id": "1", "type": "function", "function": None},
{"id": "2", "type": "function", "function": {"arguments": "keep-me"}},
],
},
]
ctx = crusher._extract_context_from_messages(messages)
assert "keep-me" in ctx
# Stage 3c.1 lockstep bug-fix tests previously lived here; they probed
# Python helpers (`_percentile_linear`, `_detect_sequential_pattern`,
# `_detect_rare_status_values`, `_compute_k_split`) that were removed
# along with the Python implementation in Stage 3c.1b. The Rust port
# pins the same invariants — see the `bug1_*` / `bug2_*` / `bug3_*` /
# `bug4_*` tests in `crates/headroom-core/src/transforms/smart_crusher/`
# (notably `crushers.rs` and `analyzer.rs`). Parity fixtures
# (`tests/parity/fixtures/smart_crusher/`) byte-compare the post-fix
# behavior across the language boundary.