Commit Graph
5 Commits
Author SHA1 Message Date
Nandana Dileep d8ff0fe9d2 fix: [BUG] Volatile SQLite metrics stores lose their schema before the first save (#4633)
<!-- oss-harness:v2 -->
Fixes #4262.

Focused Python fix for: [BUG] Volatile SQLite metrics stores lose their
schema before the first save

Changed implementation: headroom/storage/sqlite.py.
Regression tests: tests/test_storage/test_sqlite.py.

Verification (working directory `.`): `.venv/bin/python -m pytest -q -x
-p no:cacheprovider -- /workspace/tests/test_storage/test_sqlite.py`
- Patched code: 35 tests, zero failures/errors.
- Original code with the same regression tests: 1 failures, zero
collection/setup errors.
- Base commit: `12dca26aa11d99b3af6c6d68cdf96ff90ef415aa`.

AI-assisted contribution: OpenCode using Autoloops wrote this patch. The
automated harness independently ran the tests listed above. No human
review is claimed.

<!-- devin-review-badge-begin -->

---

<a href="https://app.devin.ai/review/headroomlabs-ai/headroom/pull/4633"
target="_blank"><picture><source media="(prefers-color-scheme: dark)"
srcset="https://static.devin.ai/assets/gh-devin-review-dark.svg?v=4"><img
src="https://static.devin.ai/assets/gh-devin-review-light.svg?v=4"
alt="Devin Review"></picture></a>
<!-- devin-review-badge-end -->

### Maintainer real behavior proof

Reviewed `55a6c8e3807c3d59da81a457514c82d4a3b48032` against main
`12dca26aa11d99b3af6c6d68cdf96ff90ef415aa` in isolated Windows
worktrees, Python 3.12.13. Current main is an ancestor; no branch update
was needed.

Executed `.venv/Scripts/python.exe -m pytest
tests/test_storage/test_sqlite.py -q -k 'not test_creates_db_file'
--disable-warnings`: **34 passed**, including disk migration/concurrency
and volatile-store regressions. Ruff and `git diff --check` passed. The
excluded test fails before exercising storage because its fixture holds
the temporary file open on Windows; the identical WinError 32 was
reproduced independently on current main.

Also ran a direct API smoke script, separately loading `git show
upstream/main:headroom/storage/sqlite.py` and `git show
HEAD:headroom/storage/sqlite.py`. For each of
`SQLiteStorage(':memory:')` and `SQLiteStorage('')`, it saved one
`RequestMetrics` row, checked equality with `get`, `count == 1`, one
result from `query`, `get_summary_stats()['total_requests'] == 1`, then
closed and verified a recreated store has zero rows. **Main: first save
raises `OperationalError('no such table: requests')` for both paths. PR:
all operations pass for both paths.**

Limits: this is a direct local storage API exercise, not a running proxy
or production-load qualification. Hosted CI's four test shards and
applicable checks passed on the exact reviewed head. No release or merge
performed.
2026-11-04 12:28:05 -06:00
b4c9bdf100 fix: [BUG] Saved request metrics lose their cache optimization fields (#4584)
<!-- oss-harness:v2 -->
Fixes #4261.

Focused Python fix for: [BUG] Saved request metrics lose their cache
optimization fields

Changed implementation: headroom/storage/jsonl.py,
headroom/storage/sqlite.py.
Regression tests: tests/test_storage/test_jsonl.py,
tests/test_storage/test_sqlite.py, tests/test_storage_backends.py.

Verification (working directory `.`): `.venv/bin/python -m pytest -q -x
-p no:cacheprovider -- /workspace/tests/test_storage/test_jsonl.py
/workspace/tests/test_storage/test_sqlite.py
/workspace/tests/test_storage_backends.py`
- Patched code: 57 tests, zero failures/errors.
- Original code with the same regression tests: 1 failures, zero
collection/setup errors.
- Base commit: `1fe8549f9ca77a2bc1280e4319980e0b9b31f5b9`.

AI-assisted contribution: OpenCode using Autoloops wrote this patch. The
automated harness independently ran the tests listed above. No human
review is claimed.

<!-- devin-review-badge-begin -->

---

<a href="https://app.devin.ai/review/headroomlabs-ai/headroom/pull/4584"
target="_blank"><picture><source media="(prefers-color-scheme: dark)"
srcset="https://static.devin.ai/assets/gh-devin-review-dark.svg?v=4"><img
src="https://static.devin.ai/assets/gh-devin-review-light.svg?v=4"
alt="Devin Review"></picture></a>
<!-- devin-review-badge-end -->

## Maintainer verification: Real behavior proof

Tested the current synced head
`0ad8c55662fb905cb3464fe82a58aec1335a637b` on Windows with CPython 3.12.
A standalone script (outside pytest, no mocks/provider calls)
instantiated `RequestMetrics` with all seven cache-optimization fields,
saved it through each real `SQLiteStorage` and `JSONLStorage`, closed
the store, opened a new instance against the same on-disk file, and
retrieved the request by ID. Exact command:
`G:/git/headroom/.venv/Scripts/python.exe
C:/Users/jd/AppData/Local/Temp/headroom-4584-durable-proof.py` from the
isolated branch checkout.

Both backends returned `cache_optimizer_used='anthropic'`,
`cache_optimizer_strategy='explicit'`, `cacheable_tokens=1200`,
`breakpoints_inserted=2`, `estimated_cache_hit=True`,
`estimated_savings_percent=81.5`, and `semantic_cache_hit=True` after
reopening. All seven values matched the saved input. This checks real
local disk persistence; live provider/proxy traffic, other operating
systems, and multiprocess migration were not part of this standalone
observation. The targeted storage tests separately cover
legacy/concurrent migration: 57 passed; the excluded Windows temp-file
unlink test fails identically on main.

---------

Co-authored-by: nandanadileep <116407139+nandanadileep@users.noreply.github.com>
Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: Nandana Dileep <nandanadileep@users.noreply.github.com>
Co-authored-by: JD Davis <mxjerrett@gmail.com>
2026-11-04 08:41:23 -06:00
chopratejas 967b0db439 fix: B1 — retire ICM, RollingWindow, scoring, relevance + dependents
Phase B step 1 of the live-zone-only realignment. Removes ~10K LOC of
"drop messages from history" machinery that became unreachable after
PR-A1 made `/v1/messages` a passthrough on the proxy. Live-zone-only
compression (PR-B2..B7) operates on content blocks within messages;
message-list mutation no longer happens in the pipeline.

Python deletes:
- headroom/transforms/intelligent_context.py (1077 LOC)
- headroom/transforms/rolling_window.py (395 LOC)
- headroom/transforms/progressive_summarizer.py (508 LOC)
- headroom/transforms/scoring.py (459 LOC)
- headroom/transforms/tool_crusher.py (338 LOC)
- 5 corresponding tests/test_transforms/* and tests/test_proxy_intelligent_context.py

Rust deletes:
- crates/headroom-core/src/context/* (manager, config, workspace,
  candidate, ccr_drop, strategy/, mod) + safety.rs replaced
- crates/headroom-core/src/scoring/* (mod, score, scorer, traits, weights)
- MessageScorerComparator from crates/headroom-parity (PR #338/#343
  becomes deletable; sunk cost stays sunk)
- 13 message_scorer fixtures + record_message_scorer.py

Rust adds (move + rewrite):
- crates/headroom-core/src/transforms/safety.rs — `tool_pair_indices`
  preserves the OpenAI/Anthropic tool_use ↔ tool_result pairing rule
  the live-zone dispatcher (PR-B2) needs. No IcmConfig dependency.

Surface refactors:
- HeadroomConfig: drop `tool_crusher`, `rolling_window`,
  `intelligent_context` fields; hoist `output_buffer_tokens` to top
  level (used by client.py).
- ProxyConfig: drop `intelligent_context*` fields.
- `headroom wrap` proxy server: retire IntelligentContextManager
  and RollingWindow imports + branch; pipeline is CacheAligner →
  ContentRouter (smart_routing) or CacheAligner → SmartCrusher
  (legacy).
- CLI: drop `--no-intelligent-context`, `--no-intelligent-scoring`,
  `--no-compress-first` flags.
- LangChain memory integration: rename `_apply_rolling_window` →
  `_apply_compression`, drop RollingWindowConfig dep. Threshold is
  now advisory — B6 will rework the contract.
- TransformPipeline.create_pipeline now takes only cache_aligner_config.
- headroom/__init__.py + headroom/transforms/__init__.py: strip
  exports of deleted symbols.

Bug fixes uncovered by full pytest sweep:
- providers/copilot/wrap.py: `environ or os.environ` collapsed
  empty-dict to falsy → callers passing `environ={}` accidentally
  pulled from os.environ. Use `environ if environ is not None else
  os.environ`.

Test correctness fixes:
- _DummyAnthropicHandler._retry_request gains **_kwargs to match
  the real handler signature post-A8.
- test_ws_http_fallback extracts JSON from `content=` (post-A3
  byte-faithful) rather than the obsolete `json=` kwarg.
- test_ccr_response_handler_extra fixture joins SSE events with
  `\n\n` per spec (post-A8 byte-buffer parser requirement).
- test_proxy_responses_phase_preservation: capture via direct
  handler attached to the named logger, so the assertion is
  order-independent (proxy `_setup_file_logging` flips
  `headroom.propagate=False` once any earlier test triggers it).
- conftest.py autouse fixture resets `headroom.propagate=True`
  before each test as a defensive measure for the same pollution.
- test_wrap_copilot_translated_backend_still_requires_byok:
  monkeypatch.delenv every provider key so the BYOK error
  actually fires.
- test_native_installers: skip when system bash < 4.3 (macOS ships 3.2).
- TestGeminiEmbedContent / TestGeminiBatchEmbedContents:
  pytest.mark.skip — proxy currently has no :embedContent route;
  feature gap, not regression.

Acceptance:
- cargo build --workspace + cargo clippy + cargo fmt --check: green.
- cargo test --workspace --exclude headroom-py: 777 passed.
- pytest: 4892 passed, 240 skipped, 0 failed.
- git grep returns only intentional comments referencing the deletion.

Per-PR-B1 plan: REALIGNMENT/04-phase-B-live-zone.md.
2026-05-02 12:23:17 -07:00
chopratejas e4a41faa33 Fix all ruff lint and format errors for CI
- Fix E402: Move module-level imports to top of file
- Fix F401: Add noqa for availability check imports
- Fix F402: Rename loop variables shadowing imports
- Fix E722: Replace bare except with except Exception
- Fix B904: Add exception chaining (from e)
- Fix F811: Remove duplicate imports
- Fix B027: Add noqa for empty close() method
- Fix E741: Rename ambiguous variable l -> label
- Fix I001: Import sorting issues
- Apply ruff format to all 106 files

All 902 tests pass.
2026-01-10 15:33:44 -08:00
chopratejas 9c7d4512d6 Initial commit: Headroom SDK - LLM context optimization toolkit
A comprehensive SDK for optimizing LLM context windows, reducing token
usage while preserving critical information for AI agents.

Core Features:
- SmartCrusher: Statistical compression of tool outputs (70-85% reduction)
- CacheAligner: Prefix optimization for prompt cache hits
- RollingWindow: Intelligent context window management
- BM25/Hybrid relevance scoring for smart item selection

Integrations:
- OpenAI and Anthropic provider support
- LangChain integration (ChatModel, Callbacks, Runnable)
- MCP (Model Context Protocol) integration for tool compression

Test Coverage:
- 372 tests passing across all modules
- 35 performance benchmarks
- Real-world agent evaluations with 88% token savings

Key Components:
- headroom/transforms/: Core compression transforms
- headroom/providers/: OpenAI and Anthropic support
- headroom/integrations/: LangChain and MCP integrations
- headroom/relevance/: BM25 and hybrid scoring
- headroom/pricing/: Model pricing registry
- benchmarks/: Performance benchmark suite
- examples/: Usage examples and demos
2026-01-06 23:16:58 -08:00