Repository navigation
Description
Part of #2903
Context
Line numbers are at origin/main 8d3e16ad. stream_create_response (src/server/routes/responses.rs:475) runs generation in spawn_blocking (:634), writes events through ResponseStreamSender (src/server/streaming_responses.rs:68), and shares its cancel token with client disconnect: send_event flips the token when the channel closes. Background mode needs the same generation with no HTTP connection, so that run must stop depending on the SSE channel.
Scope
Add src/server/routes/responses_run.rs, holding code moved out of stream_create_response. No behavior changes.
async fn prepare_response_run(...) -> Result<PreparedRun, ErrorResponse>: the body of:490-625(render, options, cache context, queue reservation).PreparedRunowns everything the blocking task needs.fn drive_response_run(run: PreparedRun, cancelled: CancellationToken, sink: Arc<dyn ResponseEventSink>): the body of thespawn_blockingclosure, blocking, moved intact.- The sink contract, used by every caller:
pub(crate) trait ResponseEventSink: Send + Sync {
/// Deliver one event. Must not block on a slow reader unless it is the foreground SSE sink.
fn emit(&self, event: &ResponseStreamEvent);
/// First prefill-progress or token callback; fires at most once.
fn on_started(&self);
/// Called once before the terminal event, replacing the direct persist_response call.
fn record_terminal(&self, response: &ResponseObject);
/// Retire cancel registration; true when a cancel was acknowledged (replaces finish_in_flight).
fn finish_registration(&self) -> bool;
/// Generation is over: a reader leaving from here on is not a cancel.
fn generation_finished(&self);
}SseResponseSinkwrapsResponseStreamSenderand the existingpersist_response(:1491) andfinish_in_flight(:957,:1157) calls.generation_finishedcallsafter_generationon the sender.done_itemsandstreamed_output()(feat: Stream tool-call arguments incrementally on Chat and Responses #2396) move from the sender into the driver, because the final object's output must not depend on which sink ran.send_compaction_events(responses_auto_compact.rs:405) takes&dyn ResponseEventSink.
The early-cancel branch at :957-1006 keeps its current order of store insert before the terminal event.
Out of scope
non_stream_create_response stays as is (rejected: folding it in widens the change without helping background mode). No background code, flags, or route changes.
Acceptance criteria
-
stream_create_responseis only argument handling,prepare_response_run, sink construction,spawn_blocking(drive_response_run), the admission wait, andsse_response. - Existing tests pass unmodified: no edits to
responses_cancel_route_tests.rs,tool_call_stream_route_tests.rs,stream_route_tests.rs,reasoning_echo_*_tests.rs, orresponses_auto_compact_tests.rs. - New unit test
drive_response_run_delivers_events_to_a_non_sse_sink: a recording sink on the scripted provider gets the same event names and sequence numbers as the SSE path for one token script. - New unit test:
record_terminalruns exactly once and before the terminal event. - New unit test:
on_startedfires once across several tokens. - A disconnect still cancels a foreground stream (existing coverage stays green).
Dependencies
None. Refactor only: nothing calls the non-SSE path until the create-and-poll sub-issue of this epic lands, which is the permitted integration deferral.
Verification
cargo fmt --all -- --check
cargo clippy --all-targets --features cuda -- -D warnings # metal,accelerate on macOS
cargo test --lib --profile test-fast --features cuda server::routes -- --test-threads=1
Activity
refactor(server): separate Responses generation from the HTTP connect…