Commit graph

1 commit

Author SHA1 Message Date
Tejas Chopra
4f560bccc7
feat(proxy): add --force-kompress-all to route all content through kompress-v2-base (#1613)
## Description

Adds an opt-in flag that routes **all** compressible content through
Kompress (`kompress-v2-base`), bypassing per-type compressor selection
(SmartCrusher / CodeAware / log / diff / html / tabular / search). For
deployments that prefer a single uniform compressor over the per-type
set, at a deliberate cost of per-type structural fidelity.

The mechanism already existed: `ContentRouter` reads a `force_kompress`
runtime kwarg but nothing turned it on. This PR wires it to user-facing
config (CLI + env), defaulting off.

Closes # N/A

## Type of Change

- [ ] Bug fix (non-breaking change that fixes an issue)
- [x] 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

- Add `force_kompress_all` to `ProxyConfig` (`headroom/proxy/models.py`)
and `ContentRouterConfig` (`headroom/transforms/content_router.py`).
- Default the existing `force_kompress` runtime path from config:
`kwargs.get("force_kompress", self.config.force_kompress_all)` — a
per-request kwarg still overrides.
- Expose `--force-kompress-all` CLI flag and
`HEADROOM_FORCE_KOMPRESS_ALL=1` env, mirroring the existing
`--disable-kompress` pattern (both the env factory and the `__main__`
CLI path).
- Add `tests/test_force_kompress_all.py`.

**Safety preserved:** the flag changes *strategy selection only*. The
Read/Glob/Grep exclusion (`excluded_tool_ids`) runs *before* any
compressor, and the tool-output reversibility gate (`#1307`/`#1479`)
runs *after* — neither is reachable from the strategy choice. So tool
ground truth stays verbatim.

## Testing

- [x] Unit tests pass (`pytest`)
- [x] Linting passes (`ruff check`)
- [x] Type checking passes (`mypy`)
- [x] New tests added for new functionality
- [ ] Manual testing performed (see Real Behavior Proof → Not tested)

### Test Output

```text
$ ruff check headroom/proxy/models.py headroom/transforms/content_router.py headroom/proxy/server.py tests/test_force_kompress_all.py
All checks passed!

$ ruff format --check <same files>
4 files already formatted

$ mypy headroom/proxy/models.py headroom/transforms/content_router.py headroom/proxy/server.py tests/test_force_kompress_all.py
Success: no issues found in 4 source files

$ pytest tests/test_force_kompress_all.py tests/test_content_router_exclude_tools.py -q
tests/test_force_kompress_all.py ....                                    [ 44%]
tests/test_content_router_exclude_tools.py .....                         [100%]
============================== 9 passed in 1.31s ===============================
```

## Real Behavior Proof

- **Environment:** macOS (Darwin 25.4.0), Python 3.12.6, project
`.venv`.
- **Exact command / steps:** Constructed
`ContentRouter(ContentRouterConfig(force_kompress_all=True))` and drove
the real `apply()` entry point (see `tests/test_force_kompress_all.py`)
to verify: (1) the config resolves the runtime flag on; (2) an explicit
`force_kompress=False` kwarg overrides it; (3) a `Read` tool_result is
passed through **verbatim** with the flag on (`router:excluded:tool`
marker present). Plus the full ruff/mypy/pytest suite above.
- **Observed result:** 9 tests pass. Read tool output is unchanged
(byte-for-byte) under `force_kompress_all=True`; the per-request kwarg
override works; the existing exclude-tools suite still passes through
`HeadroomProxy` (which now builds
`ContentRouterConfig(force_kompress_all=...)`).
- **Not tested:** Live proxy end-to-end against a real upstream with the
`kompress-v2-base` ONNX model compressing real traffic; aggregate
savings/accuracy deltas on a real workload. The unit tests assert the
**routing decision and the Read/Glob carve-out**, not model output
quality or ratio.

## 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 — N/A
(documented inline via config docstring + `--help`; see Additional
Notes)
- [x] My changes generate no new warnings
- [x] I have added tests that prove my feature works
- [x] New and existing unit tests pass locally with my changes
- [ ] I have updated the CHANGELOG.md if applicable — N/A (Release
Please generates it from the `feat(proxy):` commit)

## Additional Notes

- **Accuracy tradeoff (intentional):** forcing Kompress on all types
trades per-type structural fidelity (and possibly compression ratio,
since SmartCrusher/CodeAware can beat a general model on their native
type) for a single uniform compressor. Off by default; opt-in per
deployment. Correctness is *not* affected — excluded tools and
reversibility-gated tool ground truth are never touched.
- **Docs:** behavior is documented inline (CLI `--help` text +
`ProxyConfig` docstring). Happy to add a README/wiki note if maintainers
want one.
2026-06-30 15:30:22 -07:00