Files
headroom/tests/fixtures/anthropic/claude_code_auto_mode_request.json
Devanshi Vyas 12c157966f fix(proxy): preserve Claude Code auto-mode protocol (#3784)
## Description

Fix Claude Code server-side auto-mode compatibility when requests pass
through
Headroom’s native Anthropic Messages endpoint.

Previously, auto-mode capability headers, safeguard payloads, query
parameters,
unknown response fields, or opaque SSE frames could be rewritten or lost
while
proxying and handling CCR continuations. This could trigger Claude
Code’s
client-side classifier fallback and its associated warning.

This change adds a request-local Anthropic wire envelope, preserves
client-owned
capability data and opaque response attachments, prevents classifier
payloads
from entering Headroom-owned observations, and documents that
schema-translating
Bedrock, Vertex, and LiteLLM routes do not currently support the
server-side
auto-mode protocol.


Closes #

## Type of Change

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

## Changes Made

- Preserve native Anthropic request paths and raw query parameters.
- Preserve client-owned `anthropic-version`, `anthropic-beta`, and
safeguard data.
- Avoid sticky-beta, memory, or turn-hook rewrites on classifier-bearing
turns.
- Preserve unknown Anthropic response fields across CCR continuations.
- Preserve opaque SSE frames through request-local parse/render
envelopes.
- Reject unknown or malformed deltas during strict buffered
reconstruction.
- Redact safeguard payloads from diagnostics and pipeline observations.
- Consolidate Anthropic SSE parsing and rendering into the wire module.
- Document auto-mode limitations for Bedrock, Vertex, and LiteLLM
translation paths.
- Add request, response, streaming, CCR, malformed-byte, and passthrough
regression tests.

## Testing

<!-- Check what you actually ran, then paste the real command output
below. -->

- [ ] 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 -p no:cacheprovider \
    tests/test_anthropic_auto_mode_passthrough.py \
    tests/test_anthropic_buffered_sse.py \
    tests/test_ccr_response_handler_extra.py \
    tests/test_ccr_buffered_stream_signed_thinking.py \
    tests/test_proxy/test_anthropic_streaming_ccr_retrieve.py \
    tests/test_buffered_ccr_salvage.py \
    tests/test_buffered_ccr_accept_header.py \
    tests/test_proxy/test_anthropic_copilot_upstream_auth.py \
    tests/test_proxy_streaming_request_logger.py \
    tests/test_proxy_streaming_resilience.py -q

125 passed, 1 warning in 13.70s

$ ruff check --no-cache .
All checks passed!

$ ruff format --check --no-cache .
1559 files already formatted

$ python -m mypy \
    headroom/proxy/anthropic_wire.py \
    headroom/ccr/response_handler.py \
    headroom/proxy/handlers/anthropic.py \
    headroom/proxy/handlers/streaming.py

Success: no issues found in 4 source files

$ npx --no-install commitlint \
    --from origin/main --to HEAD \
    --config .commitlintrc.json

Exited successfully.
```

## Real Behavior Proof

- Environment: Pending. Record OS, Python version, Claude Code version,
provider/model, and Headroom revision.
- Exact command / steps: Pending. Start Headroom with `headroom proxy
--port 8787`, then run Claude Code with
`ANTHROPIC_BASE_URL=http://127.0.0.1:8787` and exercise server-side auto
mode.
- Observed result: Pending. Attach redacted output or logs showing the
request completed through Headroom and whether Claude Code used its
client-side fallback.
- Not tested: Live Claude Code auto mode against Anthropic; Bedrock,
Vertex, and LiteLLM translation routes.

## Runtime Rollout Safety

- Rollout-managed feature(s): None.
- Minimum rollout channel: N/A.
- Stable/default behavior changed: Native Anthropic safeguard-capable
requests.
- Kill switch / disable path: Set `CLAUDE_CODE_AUTO_MODE_SERVER=0` to
use Claude Code’s client-side fallback.
- Unsafe override required: No.
- Qualification impact: Live qualification is pending. Translation
routes remain documented as unsupported for server-side auto mode.
- Rollback path: Revert this PR or deploy the previous Headroom
revision.

## 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 where needed
- [x] I have made corresponding documentation changes
- [x] No new warnings were observed in focused tests
- [x] I have added tests for the fix
- [ ] Full existing and new unit test suite passes locally
- [x] I did **not** edit `CHANGELOG.md`

## Screenshots (if applicable)

N/A. This change affects proxy wire behavior.
2026-09-25 09:53:27 -07:00

34 lines
606 B
JSON

{
"model": "claude-test-20260924",
"max_tokens": 128,
"stream": true,
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "Run the proposed action."
}
]
}
],
"tools": [
{
"name": "run_action",
"description": "Run a sanitized test action.",
"input_schema": {
"type": "object",
"properties": {
"target": {"type": "string"}
},
"required": ["target"]
}
}
],
"safeguards": {
"mode": "auto",
"tool_use_ids": ["toolu_auto_001"]
}
}