headroom/tests/test_no_ccr_lossy.py
Tejas Chopra 60af15f96f
feat(content-router): lossless-first dispatch, cross-turn dedup, and A7 lossy-after-fold (#1818)
## Description

<!-- Briefly explain the change and why it is needed. -->

Closes #

## Type of Change

- [ ] 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

- 

## Testing

<!-- Check what you actually ran, then paste the real command output
below. -->

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

### Test Output

```text
# Paste relevant command output or artifact links here
```

## Real Behavior Proof

- Environment:
- Exact command / steps:
- Observed result:
- Not tested:

## Review Readiness

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

## Checklist

- [ ] My code follows the project's style guidelines
- [ ] I have performed a self-review of my code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [ ] My changes generate no new warnings
- [ ] 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
- [ ] I have updated the CHANGELOG.md if applicable

## Screenshots (if applicable)

Add screenshots to help explain your changes.

## Additional Notes

<!-- Mention any N/A checklist items, tradeoffs, follow-ups, or
maintainer context. -->

---------

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-06 08:32:06 -07:00

56 lines
1.8 KiB
Python

"""No-CCR lossy: with markers OFF, unmarked lossy output is accepted (not skipped).
The reversibility guard skips lossy-unmarked tool output to keep it recoverable —
but only makes sense when retrieval markers are ON. In no-CCR mode
(ccr_inject_marker=False) recovery is deliberately disabled, so the unmarked lossy
result IS the intended output. This tests both directions without needing the
ModernBERT model (self.compress is mocked to a lossy result).
"""
from types import SimpleNamespace
from headroom.transforms.content_router import (
CompressionStrategy,
ContentRouter,
ContentRouterConfig,
)
def _run(marker_on):
orig = "some_identifier = compute_value(x)\n" * 60 # long, lossy-eligible
fake = SimpleNamespace(
compressed="<<lossy summary, marker-free>>",
compression_ratio=0.3, # < min_ratio → enters accept branch
strategy_used=CompressionStrategy.KOMPRESS, # lossy + unmarked
strategy_chain=["kompress"],
)
r = ContentRouter(ContentRouterConfig(ccr_inject_marker=marker_on))
r.compress = lambda content, context=None, bias=1.0: fake # type: ignore
tr, rc = [], {}
out, was = r._compress_block_content(
orig,
hash((orig, marker_on)),
"",
1.0,
1.0,
None,
tr,
rc,
[],
"tool_result",
"tool",
enforce_reversibility=True,
)
return was, rc
def test_markers_on_skips_unmarked_lossy():
was, rc = _run(marker_on=True)
assert was is False # guard skips: unrecoverable
assert rc.get("lossy_unrecoverable_skipped", 0) == 1
def test_no_ccr_mode_accepts_unmarked_lossy():
was, rc = _run(marker_on=False)
assert was is True # accepted: no-CCR mode wants unmarked lossy
assert rc.get("lossy_unrecoverable_skipped", 0) == 0