Commit graph

2630 commits

Author SHA1 Message Date
Garm
290238f398 fix(traffic-learner): raise min-evidence default and make it configurable
The traffic learner was emitting one-shot error_recovery patterns that
contradicted each other and bloated MEMORY.md with low-signal noise. Two
issues drove this:

1. The shutdown flush bypassed the evidence gate: the in-memory
   _min_evidence was set to 2, but on stop() the gate dropped to 1, so
   every singleton pattern got persisted at session end. This is the
   opposite of how evidence thresholding should work — singletons are
   the least trustworthy patterns, not the most.

2. The default min_evidence of 2 is too low to filter noise from the
   matchers, which pair up failed/successful tool calls within a small
   sliding window without a strong semantic check that the calls are
   actually related.

Changes:
- Raise default min_evidence from 2 to 5 in TrafficLearner.
- Remove the shutdown-relaxation in flush_to_files; require
  self._min_evidence at all times, including on stop().
- Add traffic_learning_min_evidence to ProxyConfig (default 5).
- Add --min-evidence CLI flag with HEADROOM_MIN_EVIDENCE envvar so
  users and embedded clients (desktop apps, plugins) can tune the
  threshold without source changes.
- Thread the config value through HeadroomProxy into TrafficLearner.
- Tests: cover default propagation and custom value flow.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-30 17:44:22 +09:00
Tejas Chopra
0397104358
Merge pull request #325 from chopratejas/rust-stage-3g-pr1-pipeline-traits
feat(rust): pipeline traits + orchestrator skeleton (Phase 3g PR1)
2026-04-29 23:52:42 -07:00
chopratejas
caf8fefdb3 feat(rust): pipeline traits + orchestrator skeleton (Phase 3g PR1)
Introduces `CompressionPipeline` — the formal lossless-then-lossy
orchestrator described in issue #315. Before this PR each compressor
carried its own ad-hoc decision tree (SmartCrusher's 3c.2 refactor
made it explicit for one transform; the rest still decide privately).
This PR replaces that scaffolding with two traits, one orchestrator,
and one real impl per trait.

# Surface

* `LosslessTransform` — `name()`, `applies_to()`, `apply(content)`.
  Preserves all information; orchestrator runs these first and stops
  early if the cumulative savings hit `lossless_target_ratio`.
* `LossyTransform` — same shape plus `apply(content, ctx)` taking a
  `CompressionContext` (query + token budget) and a `confidence()`
  calibration score for telemetry.
* `TransformResult { output, bytes_saved, structure_preserved,
  reversible_via }` — common return shape.
* `TransformError { InvalidInput, Skipped, Internal }` — orchestrator
  treats all three as skip-this-transform; never panics.
* `CompressionPipeline` + `CompressionPipelineBuilder` — sequential
  dispatch keyed on `ContentType`. Acceptance gate is
  `min_savings_ratio` (default 5%); lossless stop gate is
  `lossless_target_ratio` (default 50% of original). Per-step
  `name()` reporting feeds the strategy-stats JSONB nest from 3e.0.

# Concrete impls (one real impl per trait — no speculative extraction)

* `JsonMinifier` (lossless) — `serde_json::Value` round-trip.
  Pretty-printed JSON shrinks 25-35%; already-compact returns
  `bytes_saved == 0` and the orchestrator rejects it.
* `LineImportanceFilter` (lossy) — consumes the existing
  `signals::LineImportanceDetector` trait. Walks `str::lines()`,
  scores each, drops below threshold, anchor-preserves first/last,
  collapses gaps into `[... N lines omitted ...]` markers.

# No regex

By project convention, nothing in this module uses the `regex` crate.
JsonMinifier is pure `serde_json`. LineImportanceFilter walks lines
and consumes the signals trait (aho-corasick + ASCII word-boundary
post-filter, also no regex).

# Touches outside the new module

* `ContentType` gains `Hash` derive so the orchestrator can key
  transforms by content type. Trivially safe — the enum is already
  `Eq + PartialEq`.
* `transforms/mod.rs` re-exports the new public surface.

# Test plan (all green)

* 41 unit tests across the four submodules:
  - 9 trait + error-handling tests
  - 9 JsonMinifier tests (pretty/compact/empty/malformed/Unicode/
    deeply-nested/structure-preserved/reversible-via/applies-to)
  - 11 LineImportanceFilter tests (drop/keep/anchor windows/single-
    line/empty/gap-counting/confidence/structure-preserved/Unicode/
    overlapping anchors/applies-to narrowing)
  - 12 orchestrator tests (empty pipeline/no-applicable/lossless-
    runs/rejection-below-min-ratio/error-recovery/lossless-target-
    stop/lossy-runs/lossy-compounds/structure-preserved-flag/
    is-acceptable-zero-cases/min-savings-ratio/builder-dispatch/
    builder-order)
* `make ci-precheck` clean
* `cargo fmt --all` + `cargo clippy` happy

# Out of scope (lands in later PRs)

* PR2: wrap existing structural transforms (Diff/Log/Search/Tag) in
  trait shape. Begins parallel-execution candidate via subagents.
* PR3: SmartCrusher refactor to use the orchestrator + retire Python
  glue.
* PR4: `ProseFieldCompressor` (parser/model boundary primitive,
  blocked on labeled corpus).
* PR5: migrate text/search/log compressors and delete Python
  ContentRouter strategy dispatch.
2026-04-29 23:42:56 -07:00
chopratejas
c9aaba3f5b feat(rust): port tag_protector to Rust + 5 bug fixes (Phase 3e.4)
`headroom/transforms/tag_protector.py` was a regex-driven scan-and-
replace loop that ran on every kompress call from ContentRouter
(`content_router.py:1089`). The Python implementation had five real
bugs we now fix in the port — the most consequential being a
`str.replace(.., .., 1)` first-occurrence-replace bug that silently
collapsed two identical custom-tag blocks in the same input to a
single placeholder + a stray duplicate of the second block.

# Bug fixes (each pinned by a `fixed_in_3e4` test)

* **#1: O(n²) on nested custom tags.** Python's `while changed` loop
  restarted a full regex scan after every replacement. Rust walks
  once in linear time on input length.
* **#2: First-occurrence replace bug.** `result.replace(orig, ph, 1)`
  replaces the FIRST textual match, not the matched offset. Two
  identical custom-tag blocks collapsed to one placeholder + a stray
  duplicate of the second block. The Rust walker stitches output by
  offset so distinct blocks always get distinct placeholders.
* **#3: Silent 50-iteration cap.** Python had a hard `max_iterations
  = 50` safety limit that quietly truncated tag protection on deeply
  nested input. The Rust walker is bounded by input length only.
* **#4: Self-closing pass duplicate-replace risk.** Python ran a
  second loop with the same `replace_first` bug for self-closers.
  Rust handles self-closers in the same single pass.
* **#5: Placeholder collision.** If the input contained a literal
  `{{HEADROOM_TAG_…}}` substring, Python silently let the collision
  break restoration. Rust salts the prefix and reports it in stats.

# Architecture

Two-phase walker:
* Phase 1 (`identify_spans`): linear scan over input bytes, hand-
  rolled tag-open / tag-close lexer (no regex). Maintains a stack of
  open custom tags; on a matching close, collapses the inner span
  into a single `Span { start, end, Block }`. Self-closing custom
  tags become `Span { ..., SelfClosing }` immediately. Marker-only
  mode (`compress_tagged_content=true`) emits Open/CloseMarker spans
  instead. Orphan opens stay un-protected (matches Python behavior).
  Orphan closes are emitted verbatim and counted in stats.
* Phase 2 (`emit_output`): walks `text` once, splicing placeholders
  for span ranges and copying everything else verbatim. Offset-based,
  never `str.replace`.

PyO3 surface: `protect_tags`, `restore_tags`, `is_html_tag`,
`known_html_tag_names`. The Python shim retires the regex internals
and re-exports `KNOWN_HTML_TAGS` (rebuilt from the Rust list) +
`_is_html_tag` for backwards compat with `content_router.py` and the
existing test surface.

# Test plan

* 25 Rust unit tests including 4 `fixed_in_3e4_*` bug-fix tests
* 27 Python tests (23 existing + 4 new `fixed_in_3e4` parity tests)
* 5 integration tests in `test_tag_protection_integration.py` pass
* `make ci-precheck` clean
2026-04-29 23:18:09 -07:00
Tejas Chopra
84bd992d10
Merge pull request #322 from chopratejas/retire-query-echo
chore(transforms): retire query_echo (already disabled in production)
2026-04-29 22:53:32 -07:00
chopratejas
eac5204b9d chore(transforms): retire query_echo (already disabled in production)
`headroom/transforms/query_echo.py` was already disabled in all three
proxy handlers (`anthropic.py`, `openai.py`, `gemini.py`) — each
carried the same comment: 'disabled — hurts prefix caching in long
conversations. The echo changes every turn, invalidating the cached
prefix.' That call was right: the echo's per-turn variability would
bust the Anthropic/OpenAI/Gemini prompt cache and cost more in TTFT
than the recall benefit ever paid back. The module was orphaned but
still living in the tree, with its own 70-test file pinning the
disabled behavior.

# Removed
* `headroom/transforms/query_echo.py` (123 LOC)
* `tests/test_query_echo.py` (whole file)
* The three 'disabled — hurts prefix caching' comment blocks in the
  proxy handlers (the rationale lives in this commit message and the
  PR description; no need to leave dead-code breadcrumbs in the hot
  path).

# Kept
* `headroom.utils.extract_user_query` is a different function with
  the same name and is still used elsewhere — untouched.

# Test plan
* `make ci-precheck` clean
* No remaining references to `query_echo`/`QueryEcho`/`Query Echo`/
  `inject_query_echo` in the tree.
2026-04-29 21:30:44 -07:00
Tejas Chopra
c6b7f1836f
Merge pull request #321 from chopratejas/retire-text-compressor
chore(transforms): retire dead text_compressor module (Phase 3e.3)
2026-04-29 21:29:18 -07:00
chopratejas
0161cdb386 chore(transforms): retire dead text_compressor module (Phase 3e.3)
`headroom/transforms/text_compressor.py` was a regex-line-sampling
fallback that nothing in the runtime called. ContentRouter routes
`CompressionStrategy.TEXT` straight to the Kompress ML compressor at
`content_router.py:1046` — the comment there literally says 'Prefer
Kompress ML compressor for text'. The Python file was orphaned but
still imported by its own test class, making it look live in the 3e
queue.

Drops the 3e.3 port from the queue: there's nothing to port.

# Removed
* `headroom/transforms/text_compressor.py` (255 LOC, unused)
* `tests/test_text_compressors.py::TestTextCompressor` (3 tests)
* `text_compressor` mention in `error_detection.py` shim docstring
* `text_compressor` mention in `test_signals_keyword_parity.py` docstring
* `TextCompressor` mention in `bench_latency.py` scenario comment

# Kept (defensive)
The legacy marker regex in `ccr/tool_injection.py:213` stays — it
parses an even older TextCompressor output format (pre-2026), is
purely defensive, and removal buys nothing. Test references to that
format in `test_ccr_tool_injection.py` document the regex contract
and stay too.

# Test plan
* `make ci-precheck` clean
* `tests/test_text_compressors.py` 19 passes (was 22, dropped 3)
2026-04-29 21:14:36 -07:00
Tejas Chopra
2228d1b019
Merge pull request #320 from chopratejas/rust-stage-3e-5-log-compressor
feat(rust): port log_compressor + bug fixes (Phase 3e.5)
2026-04-29 21:02:37 -07:00
chopratejas
45720301e5 feat(rust): port log_compressor to Rust + bug fixes (Phase 3e.5)
Ports `headroom.transforms.log_compressor` to Rust. The biggest-by-
impact remaining compressor port: build/test logs are where the
10-50x compression wins live.

* Stack-trace state machine: per-flavor dispatcher (Python Traceback,
  JS, Java, Rust error, Go); each flavor has its own termination
  rule. Python terminated on any blank line, dropping mid-trace
  lines from chained-exception traces.
* Conservative dedupe: preserves message prefix (everything before
  first `:` or `=`); only trailing region is tokenised. Python's
  blanket normalisation collapsed segfaults at different addresses.
* Loud CCR failures: `tracing::warn!` + `logger.warning` instead of
  bare `except: pass`.
* `LogLevel::FAIL` documented as cosmetic-equivalent to ERROR.

Same shape as search_compressor port. Rust `LogCompressor`
orchestrates format detect -> classify -> score -> select ->
format -> CCR. Inline static-table format detector (YAGNI),
aho-corasick level classifier with word-boundary post-filter
(`signals::keyword_detector` technique), hand-rolled per-flavor
stack-trace state machine. `signals::LineImportanceDetector` NOT
consumed -- log levels are structural, not prose-style importance.

`headroom.transforms.log_compressor` becomes a thin shim:
`compress()` delegates to Rust end-to-end; internal helpers
preserved for the existing 50-test surface. Two existing tests
updated for new dedupe semantics + new compress orchestration.

* 17 Rust unit tests
* 50 Python tests pass
* `make ci-precheck` clean
2026-04-29 20:47:49 -07:00
Tejas Chopra
d6b9ccdb4e
Merge pull request #319 from chopratejas/rust-stage-3e-2-search-compressor
feat(rust): re-land search_compressor port (Phase 3e.2 redux)
2026-04-29 19:33:07 -07:00
chopratejas
f78d24d988 test(langchain): seed random per-test to fix flaky first/last anchor eval
The 12 langchain integration evals generate fixture data via
`random.choice`/`random.randint` without seeding. SmartCrusher's
anchor selection consumes the same global random state, so a handful
of unseeded inputs (~1% of seed values) skip the first/last anchor
preservation and the eval flakes — surfaced on PR #319 CI even though
this PR doesn't touch SmartCrusher.

Confirmed pre-existing: identical 5/500 seed failures on `main`
@ `cf3877d`. The fix is the smallest one that doesn't paper over the
underlying selector behavior — seed `random` per-test via an autouse
fixture so dataset generation is reproducible.
2026-04-29 19:20:34 -07:00
chopratejas
1de32b1437 style: ruff format tests/test_transforms_search_compressor.py 2026-04-29 18:56:31 -07:00
chopratejas
4d799d5264 feat(rust): port search_compressor to Rust + signals trait consumer (Phase 3e.2)
Ports `headroom.transforms.search_compressor` to Rust as the first
consumer of the `signals::LineImportanceDetector` trait shipped in
Phase 3e.1.

The Python regex registry (`_GREP_PATTERN`/`_RG_CONTEXT_PATTERN`)
silently misparsed two real-world inputs. The hand-rolled Rust parser
fixes both:

* **Windows paths.** `^([^:]+):(\d+):(.*)$` captured only the drive
  letter from `C:\Users\foo\bar.py:42:line`, then the `\d+` group
  failed on `\`. Result: every Windows-formatted line was silently
  dropped from `file_matches`. The Rust parser detects the drive
  prefix and starts the line-number scan after the drive colon.
* **Filenames with `-`.** `_RG_CONTEXT_PATTERN`'s `[^:-]+` excluded
  dashes from the path, so legitimate names like
  `pre-commit-config.yaml-42-line` parsed wrong. The Rust parser
  anchors on the *line-number marker* (`<sep>\d+<sep>`), so paths
  can contain dashes freely.

Two further hardening changes:

* CCR storage failures are loud (Python silently swallowed them).
* Per-file dedup is `O(n log n)` via `BTreeSet<(line_no, content_hash)>`
  (Python used linear `match not in file_selected`, worst-case
  quadratic for big files).

The Rust `SearchCompressor` owns a `Box<dyn LineImportanceDetector>`
defaulting to `KeywordDetector`. Priority scoring routes through the
trait instead of a hardcoded regex list, so a future BGE classifier
head (per the trait extension docs) can take over without touching
the compressor.

Sidecar `SearchCompressorStats` captures lines unparsed, files
dropped by `max_files`, matches dropped by per-file vs global caps,
and the CCR skip reason -- diagnostics Python never emitted.

`headroom.transforms.search_compressor` is now a thin shim that
delegates `compress()` to Rust end-to-end (so the parser bug fixes
land in production), and keeps the legacy `_parse_search_results`
helper routed through the same Rust parser. The other internal
helpers (`_score_matches`, `_select_matches`, `_format_output`)
stay Python -- they're heavily covered by existing direct-call tests
and Rust scoring is byte-equivalent.

The 4 public dataclasses are unchanged. Tests that monkeypatched the
old internal `_store_in_ccr` helper are updated to exercise the new
`_persist_to_python_ccr` boundary instead.

* 16 Rust unit tests (parser, scoring, selection, CCR round-trip) +
  3 explicit `fixed_in_3e2` markers for the bug-fix lines
* 53 Python tests (existing suite intact; 2 updated for new shape)
* `make ci-precheck` clean

Stacks on PR #317 (signals trait module).
2026-04-29 17:08:36 -07:00
Tejas Chopra
cf3877de38
Merge pull request #317 from chopratejas/rust-stage-3e-1-signals
feat(rust): signals trait module + KeywordDetector (Phase 3e.1)
2026-04-29 17:02:22 -07:00
chopratejas
c39d9fe13b test: align test_error_detection with Phase 3e.1 bug fixes
This test pinned the pre-3e.1 behavior on three lines that the new
KeywordDetector intentionally changes:

1. `'token'` was asserted to be in SECURITY_KEYWORDS. It was dropped
   from the security set in 3e.1 because it false-positived on every
   LLM-token reference in our own product. Updated to assert the new
   set (`security|password|auth|secret`) and explicitly that `token`
   is gone.
2. SECURITY_PATTERN was tested via "rotate the auth token" (matched
   via the now-removed `token` keyword). Now tested via "rotate the
   auth header" (matches via `auth`, which is the real security
   signal) plus a negative assertion that LLM-metric strings no
   longer fire.
3. ERROR_PATTERN test added an assertion that "Connection timeout"
   now flags as an error (3e.1 fixed the keyword/regex drift).

PRIORITY_PATTERNS_TEXT indices were also corrected: the Rust-supplied
markdown_prefixes table is ordered `# `, `## `, `### `, `#### `, `**`,
`> ` (six prefixes) so the bold/blockquote assertions live at indices
6 and 7, not 3 and 4. New `# ` and `## ` checks pin the lower indices.

Each diverging assertion carries a `fixed_in_3e1` comment so the
audit trail stays clear.
2026-04-29 16:10:08 -07:00
Tejas Chopra
d15afdfde5
Merge pull request #316 from chopratejas/rust-stage-3d-pr4-unidiff-detector
Rust stage 3d pr4 unidiff detector
2026-04-29 15:57:06 -07:00
chopratejas
12c2665531 feat(rust): signals trait module + KeywordDetector (Phase 3e.1)
Establish `crates/headroom-core/src/signals/` as a top-level module
holding cross-cutting detection traits. Phase 3e.1 ports
`error_detection.py` to a `LineImportanceDetector` trait + a
`Tiered<T>` combinator + a single concrete `KeywordDetector` impl
backed by aho-corasick. Three traits at three granularities are
sketched (line / blob / item); only line-importance is implemented
today.

Two bug fixes from the Python source bake into both the Rust impl
and the Python regex shim:

1. `ERROR_KEYWORDS` listed `timeout|abort|denied|rejected` but
   `ERROR_PATTERN` regex omitted them. Lines like `"Connection
   timeout"` were silently neutral despite the keyword being canonical.
   Both surfaces now flag them.
2. `SECURITY_KEYWORDS` carried `token`, which false-positived on
   every reference to LLM tokens (`input_tokens`, `tokens_saved`, ...)
   in our own product. Dropped from the security set.

The Python `error_detection.py` shim now reflects keyword data out of
Rust via `keyword_registry_snapshot()` and recompiles the legacy
`re.Pattern` objects on the fly. Existing callers (text_compressor,
search_compressor, intelligent_context) continue to import the same
names with no source changes; caller migration to the trait API
happens in their own port PRs.

The trait architecture is the seam where a future ML detector slots
in without touching `KeywordDetector` or any caller. The canonical
extension is documented in `signals/README.md` as a classifier head
on the existing `bge-small-en-v1.5` embedder loaded by
`relevance::EmbeddingScorer` -- 384-dim -> 4-class softmax,
~1.5 KB head, ~1 ms inference, no extra model file. Two alternatives
(distilled tinyBERT in ONNX, logistic regression on lexical
features) are kept open in case BGE-head underfits.

Per the no-silent-fallbacks rule: only `KeywordDetector` lands as a
concrete impl. No NoOp, no MockDetector, no stub-ML -- those will
arrive with their real implementations.

Phase 3g (Compression Pipeline Formalization, issue #315) is queued
as the cross-cutting follow-up that will make lossless-then-lossy-
then-CCR ordering an explicit, observable architecture rather than
implicit per-compressor logic. Trait shapes there will reuse the
signals primitive landed in this PR.
2026-04-29 15:55:13 -07:00
Tejas Chopra
d11063eb33
Merge pull request #314 from chopratejas/rust-stage-3e-0-strategy-stats
feat(telemetry): surface per-strategy compression counters
2026-04-29 15:00:03 -07:00
Tejas Chopra
a5560f0425
Merge pull request #312 from chopratejas/rust-stage-3d-pr5-router-rewire
chore(rust): wire detection chain into ContentRouter (Stage 3d PR5)
2026-04-29 14:59:50 -07:00
chopratejas
e3554767e6 feat(telemetry): surface per-strategy compression counters
The PrometheusMetrics `compressions_by_strategy` and
`tokens_saved_by_strategy` counters (PR #302) were tracked in process
but never exported, because the Prometheus->Supabase pipeline treats
each metric name as a separate column. The data clock for the
code_compressor port-vs-retire decision needed actual production
visibility, not just CI assertions.

Add both dicts to the `/stats` endpoint and nest them under
`pipeline_timing._strategies` in the beacon Supabase payload. The
existing `pipeline_timing` column is JSONB and absorbs the nested
shape -- zero schema change.

`_build_pipeline_timing` is extracted from the inline payload-builder
so the projection has its own unit tests, including a guard that the
`_strategies` sub-key is only emitted when at least one counter is
non-empty.

The Prometheus scrape still does NOT carry these metrics; the existing
`test_prometheus_export_does_not_leak_per_strategy_metrics` guard
remains in place.
2026-04-29 14:14:09 -07:00
chopratejas
5c60abcf81 chore(rust): wire detection chain into ContentRouter (Stage 3d PR5)
Replaces the dispatch-path detection with the locked Stage-3d chain:

    Tier 1: magika_detect()       (PR3)
    Tier 2: unidiff::is_diff()    (PR4)
    Tier 3: PlainText fall-through

The regex `content_detector` is no longer on the production path —
it stays in the tree as a comparison oracle (and for any direct
caller); a future PR retires it entirely.

What lands:
- `crates/headroom-core/src/transforms/detection.rs`: new `detect()`
  function that chains the two tiers. Tier-1 errors log at WARN
  level and continue to Tier 2 (the chain's *next* tier IS the
  legitimate fallback for magika failure; treating tier-1 error as
  hard-fail would block all detection on transient ONNX issues).
- 12 unit tests covering: empty, JSON, source code, HTML, standard
  git diff, naked hunk (Tier 2 catch), prose, grep search results
  (locked-design behavior change), build log, YAML, Rust source,
  determinism across repeated calls.
- PyO3 binding `detect_content_type` now calls the chain. Synthesizes
  the legacy `DetectionResult` shape (confidence=1.0, empty metadata)
  since the chain doesn't surface a probabilistic score and no
  production caller reads metadata from the binding today.
- Python `headroom/transforms/content_router.py`: `_detect_content`
  now delegates to `headroom._core.detect_content_type`. The Python-
  side `_get_magika_detector` + regex fallback is retired (single
  detection surface; no parallel paths). Test for the helper rewritten
  to monkeypatch the Rust binding instead of the old Python paths.

Behavior changes (per locked design):
- `SearchResults` and `BuildOutput` ContentTypes route to PlainText
  (or SourceCode if magika happens to label it code-like) rather
  than to specialized strategies. No regex tier on the Rust side,
  per `project_rust_content_detection_arch.md`. If proxy benchmarks
  show real loss on grep/build outputs, we add focused detectors
  later — not preemptively.

Stacked on PR4 (unidiff). When PR4 squash-merges, this PR rebases
trivially against main.

Tests:
- 12 new Rust unit tests in `transforms::detection::tests`
- 43 Python content_router tests (was 42; old monkeypatch test
  rewritten in place, not duplicated)
- `make ci-precheck` green
2026-04-29 10:11:52 -07:00
Tejas Chopra
9b3b9c2a9b
Merge pull request #309 from chopratejas/rust-stage-3d-pr4-unidiff-detector
chore(rust): port unidiff Tier 2 diff detector (Stage 3d PR4)
2026-04-29 09:32:43 -07:00
chopratejas
19fc49ac39 chore(rust): port unidiff Tier 2 diff detector (Stage 3d PR4)
Adds the second tier of the Stage-3d ContentRouter detection arch.
Magika (PR3) is a probabilistic ML classifier — short, prose-prefixed,
or "looks like code because the lines are code" diffs can slip past
it into PlainText. PR4 catches those by running the [`unidiff`]
parser as a deterministic oracle: anything that parses to ≥1
PatchedFile with ≥1 hunk is a diff.

What lands:
- `crates/headroom-core/src/transforms/unidiff_detector.rs`:
  - `is_diff(content) -> bool`: predicate.
  - `detect_diff(content) -> Option<ContentType>`: typed wrapper for
    the router (PR5) to chain after Magika.
  - Empty input shortcuts to false without invoking the parser.
  - "Found zero hunks" is treated as **not** a diff — `unidiff::PatchSet
    ::parse` returns Ok(()) on plain text (just finds zero files);
    we explicitly require non-empty patch + non-empty hunk to avoid
    silently routing prose through the diff compressor.
- 14 unit tests: standard git diff, naked hunk without git header,
  multi-file, added/removed-only files, JSON/HTML/YAML/source/prose
  negatives, "almost looks like a diff" prose with @@/--- in passing,
  truncated-diff canary.

Known gaps (deliberately punted to PR5+):
- Combined-merge headers (`@@@ ... @@@`) — `unidiff`'s hunk regex is
  for plain `@@`. Rare in proxy traffic; PR5 router can fall back
  to the regex content_detector if needed.
- Pathological CRLF-stripped inputs — `input.lines()` strips `\r`
  only when paired with `\n`. Acceptable.

What does NOT land here (per PR scope):
- No PyO3 surface — module-only.
- No router rewiring — the existing regex `content_detector` still
  drives `ContentRouter`. PR5 chains magika → unidiff → PlainText.

The `unidiff` crate brings `regex` (already in tree) and `encoding_rs`
(default features) — small dep impact.

`make ci-precheck` green.
2026-04-28 23:19:09 -07:00
Tejas Chopra
02723fd0eb
Merge pull request #308 from chopratejas/rust-stage-3d-pr3-magika-detector
chore(rust): port Magika detection (Stage 3d PR3 — Tier 1)
2026-04-28 22:50:27 -07:00
Tejas Chopra
6801820795
Merge pull request #304 from SwiftWing21/fix/file-logging-app-lifespan
fix(proxy): defer file logging install to create_app()
2026-04-28 22:50:03 -07:00
Tejas Chopra
1812695638
Merge pull request #303 from SwiftWing21/diag/compression-timeout-296
diag(proxy): plumb request_id and exception type into compression failure logs
2026-04-28 22:49:53 -07:00
Tejas Chopra
5d1cc84357
Merge pull request #280 from eggrollofchaos/codex/fix-image-model-release-clean
fix: release image router models after compression
2026-04-28 22:49:37 -07:00
Wei Alexander Xin
cf60882949 fix: release image router models after compression 2026-04-29 01:45:27 -04:00
chopratejas
d34658b22e chore(rust): port Magika detection (Stage 3d PR3 — Tier 1)
Adds Google's `magika` ONNX-backed content classifier as the first
tier of the new Stage-3d ContentRouter detection arch (`magika` →
`unidiff-rs` → `PlainText` fall-through; no regex tier on the Rust
side).

What lands:
- New module `crates/headroom-core/src/transforms/magika_detector.rs`:
  - `magika_detect(content: &str) -> Result<ContentType, _>`
  - `OnceLock<Mutex<Result<Session, _>>>` singleton: model loads
    once per process; init failure is recorded once and cheaply
    replayed (no retry — rust-side `feedback_no_silent_fallbacks`).
  - `map_magika_label(&str) -> ContentType`: explicit match arms
    against magika's 200+ labels, mapped onto Headroom's existing
    `ContentType` enum so the dispatch (PR5) stays enum-stable.
    Unmapped labels passthrough to `PlainText` rather than misroute.
- 16 unit tests: empty fast-path, JSON / Python / Rust / JS /
  diff / markdown / plain prose / HTML / YAML / shell / SQL,
  singleton-reuse smoke, default-passthrough for unmapped labels,
  pure-table-lookup sanity.

What does NOT land here (per PR scope):
- No PyO3 surface yet — PR3 is detector-only.
- No router rewiring — the existing regex `content_detector` still
  drives `ContentRouter` until PR5 flips the dispatch.
- No `unidiff-rs` Tier-2 — that's PR4.

The `magika` crate brings `ndarray` + `ort` (already in our dep
tree via `fastembed`); adding it shares the ONNX Runtime singleton
rather than pulling a second ML stack.

`make ci-precheck` green.
2026-04-28 22:36:28 -07:00
SwiftWing21
7f03e5a1c2 style(tests): apply ruff format to compression diagnostics test
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-28 21:31:23 -07:00
Tejas Chopra
5dcecdcd35
Merge pull request #306 from chopratejas/rust-stage-3d-audit-finalize
fix(smart_crusher): re-land orphaned audit close-out — CCR knob + scorer fail-loud
2026-04-28 21:00:32 -07:00
SwiftWing21
036e424c4c test(proxy): patch logger.warning directly in compression diagnostics test
The previous _ListHandler approach attached a handler to the
headroom.proxy logger and worked locally on Python 3.14, but failed in
CI on Python 3.10-3.13 — the warning record never reached the handler.
Root cause is unclear (possibly cross-test logger state), but the
handler-attachment path is brittle for a single-warning assertion.

Replace it with unittest.mock.patch.object on the handler module's
logger.warning. This is invariant to logging hierarchy, propagation
flags, and per-test logger mutations — we directly observe the call
that the production code makes.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-28 21:00:04 -07:00
SwiftWing21
e64fdfaf93 chore(proxy): plumb request_id and exception type into compression failure logs
Issue #296 reports compression appearing to complete successfully then
being discarded after a 30s timeout. The diagnostic gap that makes the
report hard to root-cause:

1. The pipeline's "Pipeline complete: ..." and "Pipeline: freezing
   first ..." log lines have no request_id, so under concurrent load
   the reporter cannot tell whether the success log is from the
   timing-out request or a sibling request.
2. The handler's catch-all "Optimization failed: {e}" passes only
   str(e), which is empty for asyncio.TimeoutError — the report shows
   "Optimization failed:" with nothing after the colon.

This change is observability-only:

- pipeline.apply now reads request_id from kwargs and prefixes its two
  INFO log lines with [request_id] when present.
- The four anthropic_pipeline.apply call sites in the Anthropic handler
  (3 in handle_anthropic_messages, 1 in handle_anthropic_batch_create)
  pass request_id through.
- The catch-all warning becomes
  "[{request_id}] Optimization failed: {type(e).__name__}: {e}" so
  TimeoutError is distinguishable from real exceptions in bug reports.

No behavior change. Adds two tests covering both diagnostics.

This is intentionally not a fix for #296 — the underlying timeout still
needs reproduction at 367k+ token transcripts. With these diagnostics in
place, the next bug report will be able to confirm whether the failing
request actually reached the pipeline.

Refs #296

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-28 20:59:50 -07:00
chopratejas
ca2cb450c5 fix(smart_crusher): drop pre-fix CCR override hack — use dataclass defaults
The shim previously set `CCRConfig(enabled=True, inject_retrieval_marker=False)`
when no `ccr_config` was passed. That override was a no-op-intent
hack from the era where the Rust port silently ignored the flag —
"set it to False, doesn't matter, marker fires anyway".

Now that the flag is honored end-to-end, the override actively
suppresses markers + store writes for every direct caller that
doesn't pass a config. This broke `test_smart_crusher_ccr_roundtrip`
(CI of PR #306), and would silently kneecap any other caller
relying on the documented dataclass defaults.

Fix: fall through to `CCRConfig()` so the dataclass defaults
(`enabled=True, inject_retrieval_marker=True`) flow through. Markers
fire by default — same end-state as pre-PR #306 behavior, but now
honored by the Rust gate instead of silently ignored.

Verified by re-running the previously-failing tests in
`tests/test_transforms/test_smart_crusher_ccr_roundtrip.py` and the
audit / observability / eval suites. All 48 pass.
2026-04-28 18:50:54 -07:00
chopratejas
3f8de4e117 fix(smart_crusher): re-land orphaned audit close-out — CCR knob + scorer fail-loud
Re-lands two audit fixes that were marked "merged" on GitHub but never
reached main: squash-merging the parent stack changed its commit SHA,
which silently dropped the contents of the stacked PRs (#301, #305).
Single PR this time — no stacking risk.

What lands:

1. **`enable_ccr_marker` Rust gate** — new field on `SmartCrusherConfig`
   (default `true`). `crush_array` checks it before emitting the
   `<<ccr:HASH>>` marker text and the CCR store write. PyO3 surface +
   parity-fixture tolerance updated; recorded fixtures predate the
   field and inherit the `true` default.

2. **Python shim collapses both flags to the gate** — both
   `ccr_config.enabled=False` and `ccr_config.inject_retrieval_marker
   =False` now flip the Rust gate off. Storing a payload nothing in
   the prompt can reference is pointless, and storing under
   `enabled=False` would be a surprise side effect the user
   explicitly opted out of.

3. **Custom `scorer` / `relevance_config` fails loud** — replaces the
   prior WARNING-and-drop. Silently dropping a user-supplied scorer
   is a textbook silent fallback. `NotImplementedError` instead.
   Verified zero production callers pass these args; full plumbing
   arrives with Stage-3c.2's relevance-crate Python bridge.

Tests:
- 2 new Rust unit tests in `crusher.rs::tests`
- 6 new Python tests in `test_smart_crusher_toin_attachment.py`
  (3 CCR marker-knob behaviors + 3 scorer fail-loud)
- Removed `test_ccr_inject_marker_false_logs_warning` (the WARNING is
  gone now that the flag is honored)
- `make ci-precheck` green; eval suite + observability tests run
  twice consecutively to verify no TOIN file pollution leaks into the
  regular+coverage double-run on Python 3.11

RUST_DEV.md audit table reflects both gaps closed.
2026-04-28 18:37:52 -07:00
Tejas Chopra
1ee1bb295b
Merge pull request #302 from chopratejas/rust-stage-3d-compression-metrics
chore(proxy): per-strategy compression observability
2026-04-28 15:41:55 -07:00
chopratejas
2a6ab38177 test(observability): isolate SmartCrusher.apply tests from global TOIN file
The new SmartCrusher.apply() tests added in this PR feed the global
TOIN learning store (default path `~/.headroom/toin.json`). On Python
3.11, CI runs the suite twice (regular + coverage); a pattern written
in the first pass changes which rows the lossy sampler keeps in the
second pass and breaks
`tests/test_integrations/langchain/test_evals.py::TestRelevancePreservation::test_first_last_items_always_preserved`.

Fix: an `isolated_toin` fixture that points `HEADROOM_TOIN_PATH` at a
tempdir for the test and resets the singleton on enter and exit. Same
isolation pattern PR #300 already uses for `test_smart_crusher_toin_attachment.py`.
2026-04-28 15:33:07 -07:00
SwiftWing21
3d2d894a33 fix(proxy): defer file logging install to create_app()
Move _setup_file_logging() from module import to create_app() so
importing headroom.proxy.server in tests or library contexts no longer
silently attaches a RotatingFileHandler to the user's live proxy.log.

This was discovered via PR #303: running the test suite produced
"Optimization failed: TimeoutError" entries in ~/.headroom/logs/proxy.log
because TestClient instantiations from test_proxy_anthropic_compression_diagnostics.py
inherited the module-level handler.

Add a regression test that imports the server module in a subprocess
and asserts no RotatingFileHandler is attached to the headroom logger.

Refs #303 (review thread).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-28 15:28:33 -07:00
Tejas Chopra
a1f168369d
Merge pull request #279 from Kayzo/feature/production-multi-worker-proxy-startup
feat(proxy): support production multi-worker proxy startup + CLI/env tuning
2026-04-28 15:18:21 -07:00
Tejas Chopra
33825c3542
Merge pull request #300 from chopratejas/rust-stage-3d-audit-toin-ccr
fix(smart_crusher): re-attach TOIN learning loop + audit known regressions
2026-04-28 14:44:48 -07:00
Kayzo
0264e03d33 fix(testing): stabilize 3.12 suite and fingerprints 2026-04-28 21:35:32 +00:00
chopratejas
cf04c8ac78 test(smart_crusher): make CCR-warning test resilient to logging pollution
`caplog` flakes under the full suite — earlier tests can leave
`logging.disable()` or root-handler state that suppresses records on
the named logger before pytest's caplog filter sees them. The test
passed in isolation but failed deterministically once the larger
suite preceded it (CI run 25070639932 across all four Python
versions, ~4500 tests in).

Fix: monkeypatch `headroom.transforms.smart_crusher.logger.warning`
directly. Bypasses every level/disable/handler concern; we only
assert the constructor *called* `logger.warning` with the expected
flag name in the message — which is exactly the contract this test
guards.
2026-04-28 13:49:16 -07:00
chopratejas
cf97995834 chore(proxy): per-strategy compression observability
Adds a `CompressionObserver` Protocol (`headroom.transforms.observability`)
and wires `ContentRouter` and `SmartCrusher` to call it once per real
compression event. `PrometheusMetrics` implements the protocol and
accumulates per-strategy counters (`compressions_by_strategy`,
`tokens_saved_by_strategy`).

Why: the TOIN→SmartCrusher silent disconnect was invisible for three
weeks because no signal distinguished by strategy. With per-strategy
counters in place, the next regression of that shape fails the test
suite the day it lands instead of waiting on a manual audit.

Counters live as in-process state only — deliberately NOT exported via
the Prometheus scrape or OTel surface. The metric→Supabase pipeline
treats each metric name as a column, and we cannot add new columns.
CI-level observability via `tests/test_compression_observability.py` is
sufficient to catch silent regressions; production export waits on a
non-column-adding pipeline.
2026-04-28 13:11:48 -07:00
Tejas Chopra
9d5f15bdeb
Merge pull request #298 from SwiftWing21/fix/ccr-tool-injection-frozen-guard
fix(proxy): guard CCR tool injection against frozen prefix to preserve cache
2026-04-28 11:29:22 -07:00
chopratejas
049ca9cab2 fix(smart_crusher): re-attach TOIN learning loop + audit known regressions
The Stage 3c.1b retirement of the Python SmartCrusher silently disconnected
three subsystems. The audit on 2026-04-28 caught them; this commit fixes
what's fixable today and labels the rest visibly.

## TOIN learning loop — fixed

Before: `ContentRouter._record_to_toin` skipped SmartCrusher on the
assumption SmartCrusher recorded its own TOIN events. The retired Python
class did. The Rust port doesn't know about TOIN. Net result: the
highest-traffic compression strategy stopped fueling the learning loop,
silently.

Fix: shim's `crush()` and `_smart_crush_content()` now call
`toin.record_compression()` after a real compression. Filtered on
`strategy != "passthrough"` because the Rust port flips
`was_modified=True` from JSON whitespace re-canonicalization. Best
effort: TOIN failures are logged at debug level and never break
compression.

Token estimates use `len(json) // 4` (the rule the retired Python used)
because the router doesn't pass a tokenizer down to this layer and
re-tokenizing here would dominate the recording cost.

7 tests in `tests/test_smart_crusher_toin_attachment.py`:
- crush() records on real compression, doesn't on passthrough
- structurally-similar inputs land on the same pattern
- _smart_crush_content() records (legacy apply() path)
- TOIN errors don't break compression
- non-JSON input doesn't record
- inject_retrieval_marker=False emits a WARNING

## CCR marker emission knob — labelled, not yet fixed

`ccr_config.inject_retrieval_marker=False` is not honored — the Rust
port emits `<<ccr:HASH N_rows_offloaded>>` markers in `dropped_summary`
unconditionally. Today the production default has the flag True so no
one is hitting the gap, but the silent-disconnect was a real
regression. Shim now logs a WARNING when callers pass `False` so the
mismatch is visible. Fix needs a Rust-side gate; tracked in
`RUST_DEV.md`.

## Custom relevance scorer — labelled, not yet fixed

`relevance_config` and `scorer` constructor args are accepted for
source compatibility but the Rust default `HybridScorer` always
runs. Shim was logging this at debug; bumped to WARNING. Tracked.

## RUST_DEV.md

New "Known regressions in retired-Python components" section with a
table per retired component. The intent is that this section gets
updated whenever a regression closes (or a new one is found), so the
duplicate-codebase tax stays visible instead of decaying into folklore.
2026-04-28 11:28:25 -07:00
Tejas Chopra
035fa02c19
Merge pull request #295 from chopratejas/rust-stage-3d-pr1-content-detector
chore(rust): port ContentDetector to Rust + parity + PyO3 bridge
2026-04-28 11:27:39 -07:00
SwiftWing21
429ae0095b fix(proxy): guard CCR tool injection against frozen prefix to preserve cache
The Anthropic handler's CCR injector path applied a frozen_message_count
guard to system instruction injection but not to tool injection. When
Kompress fired for the first time in a session, the tools array was
mutated unconditionally, invalidating Anthropic's prefix cache and
dropping cache_read_input_tokens to zero on calls where ~48K tokens
were previously being cached.

Mirror the existing inject_system_instructions guard for inject_tool:
when frozen_message_count > 0, defer tool injection so the warm prefix
stays intact.

Adds test_ccr_tool_injection_disabled_when_prefix_frozen as a direct
companion to the existing test_ccr_system_instruction_injection_
disabled_when_prefix_frozen.

Fixes #294

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-28 10:01:11 -07:00
Tejas Chopra
f81b88b6ac
Merge pull request #293 from chopratejas/rust-perf-tier1-gil-dashmap-single-serialize
perf(rust): tier-1 multi-worker wins — GIL release, sharded CCR store, single-serialize CCR write
2026-04-27 23:57:58 -07:00
chopratejas
625290cfbc chore(rust): port ContentDetector to Rust + parity harness + PyO3 bridge
Faithful port of `headroom/transforms/content_detector.py` into
`headroom-core`. Same regex patterns, dispatch order, confidence
formulas, and line-count caps; lockstep with Python via 21 recorded
parity fixtures (every dispatch branch exercised).

- crates/headroom-core/src/transforms/content_detector.rs: regex-only
  detector (no ML); ContentType/DetectionResult mirror Python's enum +
  dataclass surface; metadata uses serde_json::Map for clean PyO3
  bridging.
- Tie-break in code detection: track scores in first-match insertion
  order (matches Python dict iteration semantics on `max()` ties).
- TypeScript second pattern is start-anchored — Python's
  `pattern.match(line)` is start-anchored, but the regex crate's
  `is_match` is unanchored, so the literal `:` prefix is required for
  parity.
- crates/headroom-parity: ContentDetectorComparator + universal f64
  normalization in `compare_fixture` (serde_json's lossy parse vs
  full-precision serialize creates a 1-ULP asymmetry that broke
  comparison; round-trip the actual through to_string/from_str).
- crates/headroom-py: detect_content_type/is_json_array_of_dicts and
  PyDetectionResult exposed via PyO3 with GIL released during scan.
- tests/parity/recorder.py: new `_wrap_function` for free-function
  recording; content_detector hook + 21 varied inputs covering JSON
  arrays, diffs, HTML, search, build/log, six languages, and fallbacks.

Sets up PR2 (ContentRouter scaffold) to call into Rust ContentDetector
in-process via the bridge.
2026-04-27 23:57:39 -07:00