headroom/headroom/proxy
JD Davis e92c253977
refactor(proxy): extract ccr session tracker (#2003)
## Description

Extracts the sticky CCR session tracker from `headroom.proxy.helpers`
into a focused state module. `helpers.SessionCcrTracker` remains as an
env-aware compatibility wrapper so existing CCR tool injection and
singleton call sites keep the same API.

Closes #

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

- Added `headroom.proxy.ccr_session_tracker.SessionCcrTracker` as the
pure bounded LRU CCR state holder.
- Replaced the in-helper CCR tracker implementation with a small
env-aware wrapper.
- Added direct tracker tests for unknown sessions, monotonic done state,
first-write golden bytes, provider isolation, LRU eviction, reset, and
input validation.

## 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
python -m pytest tests/test_ccr_session_tracker.py tests/test_ccr_tool_always_on.py tests/test_corrupt_golden_bytes_recovery.py tests/test_issue_728_empty_tools_injection.py
35 passed in 0.69s

python -m ruff check .
All checks passed!

python -m ruff format --check .
1069 files already formatted

python -m mypy headroom --ignore-missing-imports
Success: no issues found in 410 source files

gitleaks protect --staged --no-banner --redact
no leaks found
```

## Real Behavior Proof

- Environment: Windows, Python 3.13.13
- Exact command / steps: Ran direct CCR tracker tests, CCR always-on
tests, corrupt golden byte recovery tests, empty tools injection
regression tests, full ruff, format check, mypy, and staged gitleaks
scan.
- Observed result: Existing sticky CCR tool behavior and recovery
behavior remain green while the CCR session state domain is directly
covered.
- Not tested: Full repository pytest suite locally; CI covers the
broader matrix.

## 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
- [ ] I have updated the CHANGELOG.md if applicable

## Screenshots (if applicable)

N/A

## Additional Notes

Documentation and changelog updates are not applicable for this internal
refactor. The default-branch Dependabot alerts reported during push are
pre-existing and unrelated to this PR.
2026-07-11 10:17:53 -05:00
..
handlers fix(proxy): preserve chatgpt responses streaming (#2012) 2026-07-11 00:10:02 -05:00
interceptors fix(windows): pin UTF-8 encoding on text-mode subprocess calls (#1311) 2026-06-23 12:52:49 -05:00
__init__.py fix(proxy): lazy-import server to avoid fastapi crash (#442) 2026-06-10 12:44:23 -05:00
audit.py feat(proxy): pilot hardening — inbound auth, security headers, audit log, air-gap switch (#1537) 2026-06-28 12:09:00 -07:00
auth_mode.py refactor(proxy): isolate auth classification policy (#1945) 2026-07-10 19:27:08 -05:00
auth_policy.py refactor(proxy): isolate auth classification policy (#1945) 2026-07-10 19:27:08 -05:00
background_compression.py perf(compression): take large cold-start contexts off the synchronous kompress path (#1171) (#1298) 2026-06-23 10:48:06 -05:00
body_forwarding.py Extract Python forwarder mode policy (#1987) 2026-07-11 00:06:37 -05:00
cc_switch_reconciler.py feat(proxy): cc-switch reconciler — keep Headroom in the request path alongside cc-switch (#1030) 2026-06-17 08:41:16 -05:00
ccr_session_tracker.py refactor(proxy): extract ccr session tracker (#2003) 2026-07-11 10:17:53 -05:00
compression_decision.py fix(proxy): surface CompressionDecision.passthrough_reason in tags 2026-05-15 14:40:00 -07:00
cost.py fix(proxy): keep OpenAI tool observations mutable in cache mode (#1884) 2026-07-09 07:51:01 -07:00
debug_introspection.py fix(ci): restore repro harness test in dev installs 2026-04-20 22:12:01 +07:00
extensions.py fix(proxy): make third-party extensions opt-in 2026-04-25 12:48:23 -07:00
forwarded_headers.py refactor(proxy): isolate forwarded header policy (#1942) 2026-07-10 17:41:38 -05:00
forwarded_policy.py refactor(proxy): isolate forwarded header policy (#1942) 2026-07-10 17:41:38 -05:00
helpers.py refactor(proxy): extract ccr session tracker (#2003) 2026-07-11 10:17:53 -05:00
image_compression_decision.py refactor(proxy): MemoryRanker + ImageCompressionDecision + branch-aware version-sync 2026-05-19 12:13:09 -05:00
loopback_guard.py fix(security): block DNS-rebinding on /debug/* and /stats/reset via Host-header allowlist (#605) 2026-06-11 12:58:33 -05:00
memory_decision.py refactor(memory): isolate injection decision policy (#1952) 2026-07-10 23:45:44 -05:00
memory_decision_policy.py refactor(memory): isolate injection decision policy (#1952) 2026-07-10 23:45:44 -05:00
memory_handler.py feat(memory): add opt-in Apple-GPU (MPS) embedding runtime (#766) 2026-06-11 12:59:20 -05:00
memory_injection.py fix(proxy): MemoryDecision contract + 3 bypass bugs + drop 500-char query cap 2026-05-19 11:13:52 -05:00
memory_query.py refactor(memory): isolate query construction policy (#1950) 2026-07-10 23:44:27 -05:00
memory_query_policy.py refactor(memory): isolate query construction policy (#1950) 2026-07-10 23:44:27 -05:00
memory_rank_policy.py refactor(proxy): isolate memory rank policy (#1960) 2026-07-10 23:51:51 -05:00
memory_ranker.py refactor(proxy): isolate memory rank policy (#1960) 2026-07-10 23:51:51 -05:00
memory_tool_adapter.py Add multi-provider memory system with auto-detection 2026-02-01 14:42:50 -08:00
models.py feat(content-router): lossless-first dispatch, cross-turn dedup, and A7 lossy-after-fold (#1818) 2026-07-06 08:32:06 -07:00
modes.py refactor(proxy): isolate proxy mode policy (#1965) 2026-07-11 00:00:03 -05:00
outcome.py fix(proxy): count exhausted upstream 5xx as failed across all providers (#1571) 2026-07-09 17:07:08 -05:00
output_effort_policy.py refactor(proxy): isolate output effort policy (#1961) 2026-07-10 23:53:00 -05:00
output_savings.py refactor(output): isolate savings policy (#1947) 2026-07-10 23:42:11 -05:00
output_savings_policy.py refactor(output): isolate savings policy (#1947) 2026-07-10 23:42:11 -05:00
output_shaper.py refactor(proxy): isolate output effort policy (#1961) 2026-07-10 23:53:00 -05:00
output_steering.py refactor(output): isolate verbosity steering (#1940) 2026-07-10 17:39:39 -05:00
probe_recorder.py feat: probe-based retention scoring of recorded compression events (#862) 2026-06-11 13:02:36 -05:00
project_context.py refactor(proxy): isolate project attribution policy (#1957) 2026-07-10 23:50:27 -05:00
project_policy.py refactor(proxy): isolate project attribution policy (#1957) 2026-07-10 23:50:27 -05:00
prometheus_metrics.py fix(proxy): keep OpenAI tool observations mutable in cache mode (#1884) 2026-07-09 07:51:01 -07:00
proxy_mode_policy.py refactor(proxy): isolate proxy mode policy (#1965) 2026-07-11 00:00:03 -05:00
python_forwarder_mode_policy.py Extract Python forwarder mode policy (#1987) 2026-07-11 00:06:37 -05:00
rate_limit_policy.py refactor(proxy): isolate rate limit policy (#1954) 2026-07-10 23:47:33 -05:00
rate_limiter.py refactor(proxy): isolate rate limit policy (#1954) 2026-07-10 23:47:33 -05:00
request_limit_policy.py Extract request limit policy (#1982) 2026-07-10 23:55:11 -05:00
request_log_redaction_policy.py Extract request log redaction policy (#1968) 2026-07-10 06:43:04 -07:00
request_logger.py Extract request log redaction policy (#1968) 2026-07-10 06:43:04 -07:00
runtime_env.py feat(proxy): hot-reload live env knobs so a reused proxy picks them up without a restart (#1090) 2026-06-18 09:50:50 -07:00
savings_tracker.py fix(proxy): cache_savings_usd silently zeroes when litellm is unavailable (#2005) 2026-07-10 16:12:32 -07:00
semantic_cache.py refactor(cache): isolate semantic key policy (#1953) 2026-07-10 23:49:27 -05:00
semantic_cache_key.py refactor(cache): isolate semantic key policy (#1953) 2026-07-10 23:49:27 -05:00
server.py fix(kompress): surface model-not-ready state via logs and health endpoint (#2034) 2026-07-11 10:12:08 -05:00
ssl_context.py fix(tls): add HEADROOM_TLS_STRICT=0 toggle for corporate SSL inspection (#1308) (#1341) 2026-06-24 09:51:30 -05:00
stage_timer.py fix(ci): restore repro harness test in dev installs 2026-04-20 22:12:01 +07:00
tool_injection_config.py refactor(proxy): extract tool injection config (#2010) 2026-07-11 00:03:15 -05:00
tool_schema_savings_policy.py Extract tool schema savings policy (#1971) 2026-07-11 10:10:21 -05:00
turn_hooks.py feat(proxy): add turn-hook extension point for buffered model turns (#1891) 2026-07-08 17:38:45 -07:00
verbosity_controller.py feat: output-token reduction — verbosity shaper, per-user learning, counterfactual savings (#965) 2026-06-16 21:06:43 -07:00
warmup.py feat(proxy): share cold-start warmup across pipelines + lock memory init 2026-04-20 22:01:41 +07:00
ws_session_registry.py fix: log Codex ws cancellations safely 2026-05-10 09:06:07 -07:00