Skip to content

refactor(server): separate Responses generation from the HTTP connection - #2914

Merged
inureyes merged 3 commits into
mainfrom
refactor/issue-2896-responses-run-sink
Nov 10, 2026
Merged

inureyes merged 3 commits into
mainfrom
refactor/issue-2896-responses-run-sink

Conversation

@inureyes

@inureyes inureyes commented Nov 10, 2026 •

Copy link
Copy Markdown
Member

Summary

Part of #2903. Moves the streamed /v1/responses generation out of stream_create_response so a run no longer depends on the SSE channel, which background mode (#2898) needs. Refactor only: foreground behavior is unchanged.

Changes

  • New src/server/routes/responses_run.rs:
    • ResponseEventSink trait, with exactly the five methods the issue specifies (emit, on_started, record_terminal, finish_registration, generation_finished).
    • prepare_response_run(...) -> Result<PreparedRun, ErrorResponse>: the former render, options, cache-context and queue-reservation prelude. PreparedRun owns everything the blocking task needs; its fields are pub(crate), and PreparedRun::arm_admission(cancelled) arms the AdmissionWaiter on its options.
    • drive_response_run(run, cancelled, sink): the former spawn_blocking closure body, moved intact. Every send_event call became a sink emit, the two finish_in_flight calls became finish_registration, the persist_response call and the early-cancel store insert became record_terminal, and after_generation() became generation_finished(). on_started fires once on the first token or prefill-progress callback.
    • SseResponseSink: wraps ResponseStreamSender, registers the in-flight cancel token on construction, persists through persist_response, retires the registration through finish_in_flight, and switches to the after_generation sender in generation_finished.
  • done_items and streamed_output() (feat: Stream tool-call arguments incrementally on Chat and Responses #2396) moved from ResponseStreamSender into the driver, so the final object's output does not depend on which sink ran.
  • send_compaction_events takes &dyn ResponseEventSink.
  • stream_create_response is now argument handling, prepare_response_run, sink construction, spawn_blocking(drive_response_run), the admission wait, and sse_response.
  • persist_response is pub(crate) so feat: Create and poll background Responses #2898 can append conversations at terminal; count_reasoning_tokens is pub(super).

Notes for sibling sub-issues

The trait doc on ResponseEventSink states the terminal contract #2898 builds on:

  • A finished run (including a cancel during generation) calls record_terminal with the final response, then emits response.completed or response.incomplete.
  • A cancel acknowledged before the first token calls record_terminal with a cancelled response whose output is empty, before response.completed, where the store insert used to be. That turn produced no output, so nothing goes to a conversation: SseResponseSink::record_terminal stores it without the conversation append, keyed on cancelled status plus empty output (every generated response carries at least one output item).
  • A failed generation emits response.error without record_terminal, as before. A sink that drops events (the background sink) must inspect the Error event in emit to learn the code and message.
  • finish_registration runs once in every case, before the outcome is decided.
  • prepare_response_run keeps the can_accept_request 503 and the queue-reservation error mapping, so its ErrorResponse carries the same status codes as before.

Tests

  • New src/server/routes/responses_run_tests.rs on the scripted provider:
    • drive_response_run_delivers_events_to_a_non_sse_sink: a recording sink receives the same event names and sequence numbers as the SSE stream for the same token script.
    • record_terminal_runs_once_before_the_terminal_event: one record_terminal, after generation_finished and finish_registration, before response.completed, which is last.
    • on_started_fires_once_across_several_tokens.
    • failed_generation_reports_only_an_error_event and cancel_before_the_first_token_records_an_empty_cancelled_response pin the two non-finished terminal paths.
  • Existing tests pass unmodified (responses_cancel_route_tests.rs, tool_call_stream_route_tests.rs, stream_route_tests.rs, reasoning_echo_*_tests.rs, responses_auto_compact_tests.rs are untouched).

Verification

  • cargo fmt --all -- --check, cargo clippy -p mlxcel --lib --tests --profile test-fast --features cuda -- -D warnings, scripts/ci/check_env_var_docs.py, scripts/insert_apache_header.py --check: clean.
  • cargo test --lib --profile test-fast --features cuda with -- --test-threads=1: server::routes 552 passed (1 ignored), server::streaming_responses 4 passed, server::reasoning_echo 30 passed.
  • Real checkpoint qwen3-0.6b-4bit with mlxcel-server on GB10: a streamed request ends with response.completed, contiguous sequence numbers 0..66, and is retrievable by id; a non-streamed request returns completed; POST /v1/responses/{id}/cancel mid-stream returns cancelled, the stream ends with response.completed carrying status: cancelled, GET and a repeat cancel both return cancelled; a client disconnect mid-stream logs Sequence ... cancelled (client disconnected) and the server stays healthy.

Closes #2896

Move the streamed /v1/responses generation out of stream_create_response into a new routes/responses_run.rs, so a run can be driven without an SSE channel. prepare_response_run renders the prompt, builds the options and cache context, and reserves the queue slot into a PreparedRun; drive_response_run runs the former spawn_blocking body unchanged and delivers every event through the new ResponseEventSink trait.

SseResponseSink keeps the foreground behavior: it wraps ResponseStreamSender, persists the terminal response, retires the in-flight cancel registration, and switches to the after-generation sender once generation ends. The record of completed output items moves from the sender into the driver, so the final object does not depend on which sink ran. send_compaction_events now takes a &dyn ResponseEventSink.

New unit tests drive a run into a recording sink on the scripted provider and check that it receives the same event names and sequence numbers as the SSE stream, that record_terminal runs once before the terminal event, and that on_started fires once.
@inureyes inureyes added type:refactor Code restructuring without changing functionality priority:medium Medium priority status:review Under review labels Nov 10, 2026
State on ResponseEventSink which terminal outcome calls which hook: record_terminal runs for a finished or cancelled run, a cancel acknowledged before the first token records an empty cancelled response that no conversation receives, and a failed generation is reported only as an emitted response.error. SseResponseSink now keys the no-append case on both the cancelled status and the empty output.

Add driver tests for the error path and for the early-cancel path, which had no non-SSE coverage.
@inureyes inureyes added status:done Completed and removed status:review Under review labels Nov 10, 2026
@inureyes
inureyes marked this pull request as ready for review November 10, 2026 01:13
@inureyes
inureyes merged commit c987b32 into main Nov 10, 2026
31 checks passed
@inureyes
inureyes deleted the refactor/issue-2896-responses-run-sink branch November 10, 2026 01:13
@inureyes inureyes added this to the 0.9 milestone Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

priority:medium Medium priority status:done Completed type:refactor Code restructuring without changing functionality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

refactor: Separate Responses generation from the HTTP connection

1 participant