CCR retrieval is unavailable on OpenAI chat-completions streaming, and toggles the tools cache in mixed-mode sessions
Follow-up to #3810, which fixed when headroom_retrieve enters the tools array. This is the remaining gap on one path.
The path
/v1/chat/completions with stream: true. _should_inject_openai_chat_ccr_tool returns False there (openai.py:1239), which skips the whole CCR block at openai.py:4180 — including sticky replay.
Responses streaming is not affected; it already has buffered_ccr_response.
Two consequences
1. Retrieval is silently unavailable. Compression still runs, so content carries Retrieve more: hash=… markers, but no tool is injected. The model is told to retrieve and has no way to. That is deliberate — injecting would hand OpenAI-compatible clients an unresolvable tool call — but it means CCR is lossy on this path, which is the one thing CCR's contract says it is not.
2. Mixed-mode sessions toggle the tools array. openai_session_id is computed from the messages (openai.py:3770), not the stream flag, so one conversation shares a session across both modes:
| turn | mode | tool present | effect |
|---|---|---|---|
| 1 | non-stream, compresses | yes (inject_first_time) |
tracker records golden bytes |
| 2 | stream | no — block skipped | tools array changed → prefix bust |
| 3 | non-stream | yes (inject_sticky_replay) |
changed again → bust |
tools is the head of the provider cache key, so each flip invalidates the whole prefix. Unlike #3810's once-per-session cost, this recurs.
Why the obvious fix is wrong
StreamingCCRHandler.process_stream (headroom/ccr/response_handler.py:646) already implements exactly this: buffer the SSE, run the retrieval continuation, re-emit as provider-correct SSE — including _openai_response_to_chunks for chat frames. It has tests (tests/test_ccr_response_handler_extra.py) and zero callers; it has been dead since v0.2.2 (d724f140).
Do not wire it. It re-emits from a generator feeding a StreamingResponse, which commits 200 text/event-stream before the outcome is known — the regression #2997 fixed. The current pattern flips stream:false upstream and resynthesizes, which is what lets buffered_ccr_asgi_call preserve the real HTTP status and run a heartbeat (#2465, #3079).
Proposed implementation
Mirror the Responses path. Three of the four pieces already exist and are tested:
buffered_ccr_asgi_call(buffered_ccr_response.py:216) — already provider-shared, takesOPENAI_ERROR_FORMAT.- the non-streaming chat CCR continuation — already works.
_openai_response_to_chunks(response_handler.py:925) +data: [DONE].
What has to be built:
_should_buffer_openai_chat_stream_ccr(...), mirroring_should_buffer_openai_responses_stream_ccr(openai.py:1218), including the OpenCode Zen exclusion (#3656) and ChatGPT-auth exclusion.- Invert the gate ordering. The Responses gate requires
headroom_retrieveto already be intools; on chat it never is, so the gate could never fire. Buffered mode must be decided before injection, and_should_inject_openai_chat_ccr_toolmust then permit injection when buffered mode is on. - Restructure the chat stream/non-stream fork (
openai.py:5015) so both modes run through one_buffered_ccr_operation()-style closure. This is the load-bearing part: usage recording, prefix-tracker updates, the outcome provider and transforms/savings attribution all currently live in_stream_responseand the non-streaming branch. A buffered path that bypasses them would under-report usage and mis-attribute savings — the failure class of #3615/#3699. Do not duplicate the accounting; share it. - Config flag, default off. This is the most incident-prone path in the repo. Land the capability opt-in; flip the default separately, with measurements.
Tests worth pinning
- A buffered chat-stream turn emits valid
chat.completion.chunkframes ending in[DONE], withfinish_reason: tool_callspreserved when the reconstructed message has tool calls (response_handler.py:912already guards this). - Status fidelity: a 429/5xx resolving inside the grace window reaches the client with its real status, not a 200 with no frames (#2997).
- Liveness: a slow turn past the grace window gets a first byte before any client idle watchdog (#2465/#3079).
- Usage and savings are recorded identically to the non-buffered path — same request, both modes, compare the recorded numbers.
- The tools array is byte-stable across a session that mixes streaming and non-streaming turns (the toggle above).
- Flag off ⇒ byte-identical behaviour to today.
Reachability
The toggle needs one conversation to mix modes — a stream retried as non-stream, or a UI that streams interactive turns and uses non-streaming for a regenerate/tool loop. Less common than #3810. Consequence 1, though, applies to every OpenAI-compatible client that streams chat completions, which is a large surface.
Source: headroomlabs-ai/headroom