Commit 6c31db9
authored
feat(content-router): accept any real compression (remove min-savings floor) (headroomlabs-ai#1771)
## Description
The compression acceptance gate rejected any compression that saved less
than ~15% (`min_ratio` interpolated 0.85 at low context pressure → 0.65
under pressure). That floor was a crude proxy for "big enough to justify
busting the prefix cache," but it dropped genuine token savings —
notably lossless code/log folds that shrink <15% (the `ratio_too_high`
rejections).
This makes the gate accept **any real shrink** (`ratio < 1.0`): any
token saved is worth taking. The two guards that actually protect
correctness are untouched:
- **Reversibility gate** — lossy, unmarked tool output still stays
verbatim (accuracy; headroomlabs-ai#1307).
- **Net-cost policy** (`HEADROOM_NET_COST_POLICY=1`, opt-in) — precisely
accounts for the prefix-cache-bust economics (savings × expected-reads
vs one-time suffix re-write) when a session wants that protection.
Lowering the two values back to `0.85`/`0.65` restores the savings
floor.
Closes #
## Type of Change
- [x] New feature (non-breaking change that adds functionality)
- [x] Performance improvement
## Changes Made
- `ContentRouterConfig.min_ratio_relaxed`: `0.85 → 1.0`
- `ContentRouterConfig.min_ratio_aggressive`: `0.65 → 1.0`
- Gate now accepts any `compression_ratio < 1.0` at every context
pressure; reversibility + net-cost guards unchanged.
## Testing
- [x] Unit tests pass (`pytest`)
- [x] Linting passes (`ruff check`)
- [x] Type checking passes (`mypy headroom`)
- [ ] New tests added for new functionality (existing gate-mechanism
tests cover it; they pass explicit `min_ratio` values and are
unaffected)
- [ ] Manual testing performed
### Test Output
```text
# content-router + compression suites (default-config paths):
tests/test_transforms/test_content_router.py ......... 139 passed
# broad compression sweep (-k compress/router/crush/kompress/lossless/ccr/savings/...):
1 failed, 1940 passed, 54 skipped in 181.25s
# the 1 failure = test_lossless_mode::test_router_lossless_search_no_marker_and_recoverable
# — a local fastembed-cache state-leak flake; passes in isolation (1 passed in 3.15s),
# and is in lossless mode which bypasses this gate entirely.
ruff check headroom/transforms/content_router.py -> All checks passed!
mypy headroom/transforms/content_router.py -> Success: no issues found
```
## Real Behavior Proof
- Environment: local worktree, Python 3.12, `PYTHONPATH` pinned to the
branch checkout.
- Exact command / steps: ran the content-router acceptance-gate suites
and a compression-adjacent sweep against the branch; verified the flaky
test passes in isolation.
- Observed result: gate-mechanism tests (explicit `min_ratio`)
unaffected; no default-floor test regressed; blocks that previously
produced `ratio_too_high` at ratios in `[0.85, 1.0)` are now accepted.
- Not tested: no live end-to-end proxy run was performed for this
specific change; the behavioral effect (more `router:*` acceptances,
fewer `ratio_too_high`) is inferred from the gate logic + suite.
## 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
- [ ] I have added tests that prove my fix is effective (existing gate
tests cover the mechanism)
- [x] New and existing unit tests pass locally with my changes
- [ ] I have updated the CHANGELOG.md if applicable (handled at release
time)
## Additional Notes
Deliberate tradeoff (discussed and chosen): without the net-cost policy
enabled, accepting sub-15% wins can be net-negative on prompt-cached
sessions, because compressing a block invalidates the cached suffix (a
one-time re-write, at 1.25× on Anthropic). If that shows up in practice,
enable `HEADROOM_NET_COST_POLICY=1` (the precise economics guard) or
restore a floor by lowering the two `min_ratio_*` values.1 parent f4ecdeb commit 6c31db9
1 file changed
Lines changed: 12 additions & 12 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
919 | 919 | | |
920 | 920 | | |
921 | 921 | | |
922 | | - | |
923 | | - | |
924 | | - | |
925 | | - | |
926 | | - | |
927 | | - | |
928 | | - | |
929 | | - | |
930 | | - | |
931 | | - | |
932 | | - | |
933 | | - | |
| 922 | + | |
| 923 | + | |
| 924 | + | |
| 925 | + | |
| 926 | + | |
| 927 | + | |
| 928 | + | |
| 929 | + | |
| 930 | + | |
| 931 | + | |
| 932 | + | |
| 933 | + | |
934 | 934 | | |
935 | 935 | | |
936 | 936 | | |
| |||
0 commit comments