Commit Graph
2 Commits
Author SHA1 Message Date
Tejas ChopraandClaude Opus 5.5 a20c76c26b refactor(transforms): drop the Python mirror of the Rust log selector (#4096)
## Description

`LogCompressor.compress()` runs entirely in Rust. The class still
carried a Python re-implementation of the line parser, scorer, selector,
deduper, context adder and formatter, plus the pytest short-summary
helpers and a `_store_in_ccr` shim. Production never ran them, and
`_store_in_ccr` still had the #816 bug that the live path fixed (no
`explicit_hash`). All removed names are private.

Refs #4136

## 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
- [x] Code refactoring (no functional changes)

## Changes Made

- Remove the private helpers listed above: about 400 lines.
- Format-detection tests now call `headroom._core.detect_log_format`,
which `compress()` uses.
- Level counts, and the JS and rustc stack-trace shapes, become
`compress()`-level tests. The stack-trace tests run with
`error_context_lines=0`, so only trace detection can keep the frames.
- Two Rust unit tests are added for behaviour no Rust test pinned:
`level_classifier_covers_every_level` (FAIL, DEBUG, TRACE, unknown) and
`score_line_orders_levels_and_boosts`.
- Helper tests whose behaviour Rust already unit-tests are deleted:
dedupe, pytest short-summary recognition and CRLF, the malformed
diagnostic, cap precedence and reservation, omission naming, select
first/last, and the Go/Rust/.NET/Java trace openers.

## Testing

- [x] 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
$ cargo test -p headroom-core --lib log_compressor
test result: ok. 40 passed; 0 failed
$ pytest tests/test_log_compressor.py tests/test_transforms_log_compressor.py tests/test_transforms_stack_traces.py tests/test_ccr_rust_marker_hash_bridge.py tests/test_transforms_content_router.py tests/test_adaptive_sizer.py tests/test_transforms_search_compressor.py tests/test_search_compressor.py
223 passed in 7.52s
$ ruff check / format: All checks passed;  mypy log_compressor.py: Success
```

## Real Behavior Proof

- Environment: macOS, Python 3.12, repo `.venv`, this branch's worktree
with the prebuilt `headroom._core`. Temp `HOME`,
`HEADROOM_WORKSPACE_DIR` and `CODEX_HOME`; nothing touched the real
home.
- Exact command / steps: (1) shared smoke harness: a real proxy from
this checkout against a local fake upstream, with one Anthropic and one
OpenAI request carrying a compressible tool result (`smoke.sh
<worktree>`); (2) a real `LogCompressor.compress()` with
`error_context_lines=0` on 100 filler lines, a JS stack trace, and 100
more filler lines.
- Observed result: The proxy serves both requests (200) with the same
savings as main. The direct check keeps exactly the three trace lines
and reports the level counts from Rust. Output below.
- Not tested: Nothing beyond the listed files.

```text
branch: cleanup/transforms-log-drop-python-mirror @ b82ef1b7
health: 200
POST /v1/messages        -> HTTP200 smoke-ok
POST /v1/chat/completions -> HTTP200 smoke-ok
stats: requests=2 failed=0 tokens_saved=13574 savings_percent=99.82
proxy.log WARNING/ERROR lines ($SMOKE/ws/logs/proxy-51445.log):
   2 headroom.proxy - WARNING - event=proxy_offline_mode air-gap active — every connection Headroom initiates is refused (telemetry, update check, license reporter, model/tokenizer/binary/dataset downloads, remote Kompress, OTLP/Langfuse export, Copilot auth, subscription polling, OpenAI embedders, Headroom Cloud compression). Still allowed on purpose: forwarding your requests to the upstream you configured, operator-configured local endpoints, loopback health probes.
   4 headroom.transforms.kompress_compressor - WARNING - Kompress: artifact prefetch found no usable file for chopratejas/kompress-v2-base
   4 headroom.transforms.kompress_compressor - WARNING - Kompress: artifact prefetch refused for chopratejas/kompress-v2-base (HEADROOM_OFFLINE is set): HEADROOM_OFFLINE is set: refusing outbound network access for HuggingFace download of chopratejas/kompress-v2-base/onnx/kompress-int8-wo.onnx to huggingface.co. Unset HEADROOM_OFFLINE, or turn off the feature that needs this connection.
(smoke dir: $SMOKE)
```

```text
$ python -c "<LogCompressor(error_context_lines=0).compress(100 filler + JS stack trace + 100 filler)>"
LogFormat.GENERIC {'errors': 1, 'fails': 0, 'info': 0, 'selected': 3, 'total': 203, 'warnings': 0}
Error: Connection failed
    at Connection.connect (src/db.js:42:15)
    at async main (src/index.js:10:5)
[200 lines omitted: 1 ERROR]
```

## Runtime Rollout Safety

- Rollout-managed feature(s): None.
- Minimum rollout channel: Not applicable; no rollout-managed behaviour
is touched.
- Stable/default behavior changed: No. Only unused private methods are
removed.
- Kill switch / disable path: Not applicable; nothing that runs changes.
- Unsafe override required: No.
- Qualification impact: None.
- Rollback path: Revert this single commit.

## 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
- [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)

Not applicable.

## Additional Notes

Part of maintainer-requested cleanup #4136 (dead code, duplication,
debuggability), split into small PRs for review.

Documentation changes are not in this PR. They are recorded in the
cleanup docs ledger for the single follow-up docs PR.

`adaptive_sizer.py` is kept on purpose. On main, the SearchCompressor
helpers still import it, and those are removed in a separate PR. Delete
it in a one-file follow-up once both PRs land.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 08:58:41 -07:00
chopratejasandClaude Opus 4.5 d3298368bf fix: improve error handling and add comprehensive test coverage
Bug fixes:
- Replace bare except handlers with specific exception types and logging
  in proxy/server.py (6 instances for CCR, SSE parsing, cost tracking)
- Fix session_id filtering security bug in memory/backends/local.py
  (sessions were not properly isolated in vector search)

New tests (344 total):
- test_ccr_batch_processor.py: 51 tests for batch result processing
- test_compression_store.py: 76 tests for compression cache
- test_log_compressor.py: 47 tests for log format detection/compression
- test_search_compressor.py: 48 tests for grep output compression
- test_integrations/langchain/: 122 tests for LangChain integration
  (agents, memory, retriever, streaming)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-27 16:08:36 -08:00