mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-25 12:11:49 +08:00
feat/https-compose-caddy
1
Commits
| Author | SHA1 | Message | Date | |
|---|---|---|---|---|
|
|
fef99cc5b4 |
feat(compress): add per-message compression diagnostics (#3058)
## Description Adds `CompressConfig.diagnostics` (bool, default `False`) and a companion `HEADROOM_DIAGNOSTICS=1` environment variable that enable per-message decision logging on every call to `compress()`. When active, `CompressResult.diagnostics` is a list of `MessageDecision` objects — one per input message — each describing: | Field | Description | |---|---| | `message_index` | Zero-based position in the original messages list | | `role` | `"user"` / `"assistant"` / `"tool"` / `"system"` / … | | `tokens_before` | Token count before the ContentRouter acted on this message | | `tokens_after` | Token count after (equals `tokens_before` for protected/passthrough) | | `action` | What was done: `"compressed:kompress:0.72"`, `"protected:user_message"`, `"passthrough:frozen"`, `"passthrough:small"`, etc. | Closes #2855. ## 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 - **`headroom/config.py`** — add `MessageDecision` dataclass; add `message_decisions: list[MessageDecision]` field to `TransformResult` - **`headroom/compress.py`** — add `CompressConfig.diagnostics`; add `CompressResult.diagnostics`; derive `collect_diagnostics` from config + env var; pass to pipeline; collect from `TransformResult` - **`headroom/transforms/pipeline.py`** — accumulate `message_decisions` from each transform result - **`headroom/transforms/content_router.py`** — instrument every decision branch in `apply()` with `_diag[i] = "action:..."` strings when `collect_diagnostics=True`; build `MessageDecision` list after Pass 3 (zero overhead when flag is off) - **`headroom/__init__.py`** — export `MessageDecision` in `__all__` and lazy-import map - **`tests/test_compress_diagnostics.py`** — 14 tests covering the full `compress() <-> pipeline` contract via a mock pipeline ## Testing - [x] Unit tests pass (`pytest`) - [x] Linting passes (`ruff check .`) - [ ] Type checking passes (`mypy headroom`) - [x] New tests added for new functionality - [ ] Manual testing performed ### Test Output ```text $ python -m pytest tests/test_compress_diagnostics.py -v ========================= test session starts ========================== collected 14 items tests/test_compress_diagnostics.py::test_message_decision_dataclass PASSED tests/test_compress_diagnostics.py::test_message_decision_importable_from_headroom PASSED tests/test_compress_diagnostics.py::test_transform_result_has_message_decisions_field PASSED tests/test_compress_diagnostics.py::test_transform_result_message_decisions_populated PASSED tests/test_compress_diagnostics.py::test_compress_config_diagnostics_defaults_false PASSED tests/test_compress_diagnostics.py::test_compress_config_diagnostics_can_be_set PASSED tests/test_compress_diagnostics.py::test_compress_result_diagnostics_none_by_default PASSED tests/test_compress_diagnostics.py::test_compress_passes_collect_diagnostics_when_config_true PASSED tests/test_compress_diagnostics.py::test_compress_result_diagnostics_list_when_config_true PASSED tests/test_compress_diagnostics.py::test_compress_result_diagnostics_contains_decisions PASSED tests/test_compress_diagnostics.py::test_env_var_enables_diagnostics PASSED tests/test_compress_diagnostics.py::test_env_var_absent_keeps_diagnostics_none PASSED tests/test_compress_diagnostics.py::test_env_var_overrides_config_false PASSED tests/test_compress_diagnostics.py::test_no_diagnostics_when_optimize_false PASSED ========================== 14 passed in 0.42s ========================== $ ruff check . All checks passed! ``` ## Real Behavior Proof - Environment: Python 3.12, headroom dev install - Exact command / steps: `python -m pytest tests/test_compress_diagnostics.py -v` - Observed result: 14 tests pass; diagnostics list is populated when `CompressConfig(diagnostics=True)` or `HEADROOM_DIAGNOSTICS=1` - Not tested: ContentRouter integration tests (require compiled `headroom._core` extension — run in CI) ## Runtime Rollout Safety - Rollout-managed feature(s): None — diagnostics are opt-in via `CompressConfig.diagnostics=True` or `HEADROOM_DIAGNOSTICS=1` - Minimum rollout channel: N/A (additive, opt-in only) - Stable/default behavior changed: No — `CompressConfig.diagnostics` defaults to `False`; `CompressResult.diagnostics` is `None` unless explicitly requested - Kill switch / disable path: Simply omit `diagnostics=True` / unset `HEADROOM_DIAGNOSTICS` - Unsafe override required: No - Qualification impact: None - Rollback path: Revert this PR; no schema or data migration involved ## 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 - [ ] 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 - [x] New and existing unit tests pass locally with my changes - [x] I did **not** edit `CHANGELOG.md` — it is generated by release-please from my Conventional Commit PR title (a CI guard enforces this) ## Screenshots (if applicable) N/A — no UI changes. ## Additional Notes The `collect_diagnostics` flag incurs zero overhead in the hot path when not set: the `_diag` dict in `ContentRouter.apply()` is never allocated and no `MessageDecision` objects are constructed. Signed-off-by: Radha Krishnan P <gingeekrishna@gmail.com> --------- Signed-off-by: Radhakrishnan Pachyappan <radhakrishnan.p@op.tech> Signed-off-by: Radhakrishnan P <gingeekrishna@gmail.com> |