Repository navigation
Conversation
|
A case file has been opened for 691. Future reviews will auto-run 90 minutes after code updates are pushed.
|
Autobot ReviewFull Review · Value Proposition · Case File Recommendation: Ready To MergePR #691 cleanly fixes #448 by carrying each request's finalized admission prefix-reuse count through the existing
Walkthrough
Merge Risk: Low · up to
|
|
Need a rebase |
ef45ca9 to
d99fa77
Compare
|
Rebased onto |
d99fa77 to
00cbbdd
Compare
|
Rebased onto Still overlaps #692/#705 at the Discord: jcartu |
…chunks With stream_options.include_usage and stream_options.continuous_usage_stats enabled, intermediate Chat and Completions SSE chunks previously reported prompt_tokens_details.cached_tokens as 0 even when the scheduler reused a cached prefix, while only the terminal and final usage chunks reported the reused token count. Encoder::usage_value read each choice's cached count from Encoder::usage, which was only populated inside Encoder::finish when GenerationEvent::Finished arrived. A request's reused prompt token count is already finalized when its first generated token is observed: pre-token requeue paths (failed host-restore fallback and late pool-lookup upgrades) occur only before the first generated token, and subsequent preemption, reasoning-budget cuts, or speculative grammar transitions lock the initial count into Carried::reused_prompt_tokens. Carry the finalized reused prompt token count across the existing per-request first_token oneshot channel in helios/src/server/runtime.rs instead of waiting until GenerationEvent::Finished, computing carried.reused_prompt_tokens.unwrap_or(live.reused_prompt_tokens()) lazily via Incoming::observe_token_with only while first_token is still pending so later tokens incur no scheduler lookup. Add Generation::has_pending_first_token, Generation::take_reused_prompt_tokens, and Encoder::report_cached_tokens (helios/src/server/protocol/encode.rs). In helios/src/server/http.rs, record the count returned by Generation::wait_for_first_token before emitting the initial Chat assistant delta chunk, and track only pending representative first choices (`step_by(choices)`) to drain ready counts on GenerationEvent::ObservedToken for Completions while continuous_usage_stats is active. Anthropic Messages and Responses streaming paths remain unchanged: Messages still reports zero cache reads on message_start and the final counts on message_delta. No new GenerationEvent variant is introduced, preserving Prompt::output_event_capacity and ResponseLifecycle invariants. With stream_options.include_usage and stream_options.continuous_usage_stats enabled: - Every Chat streaming chunk (initial assistant delta through intermediate content/observed chunks to final usage) and every generated single-prompt, n=1 Completions chunk reports the finalized prompt_tokens_details.cached_tokens count matching the final usage chunk (and 0 when no prefix is reused), preserved across preemption/readmission. - Multi-choice (n > 1) and multi-prompt Completions streams report the cumulative reused prompt tokens across prompts whose representative first choice has reached its first token, matching the final usage chunk once all representative first choices have started; Completions echo frames emitted before worker admission continue to report 0 cached tokens. Verified with CPU crate tests and TLA+ lifecycle conformance on Linux (no GPU or exported-core live serving run was performed): - cargo test --locked --release --manifest-path helios/Cargo.toml --features runtime-core,server: 1821 unit passed (47 ignored), 62 integration passed (36 ignored), 0 failed; ignored tests require GPU hardware, exported cores, or external model checkpoint/tokenizer snapshots. Includes Encoder unit tests (Chat, Completions, zero reuse, multi-prompt n=2) and HTTP + Worker + RequestScheduler tests covering Chat prefix hit and miss, post-token preemption/readmission, Completions n=2 prefix hit, staggered multi-prompt n=2 readiness, and Messages message_start/message_delta preservation. - cargo test --locked --release --manifest-path flare/Cargo.toml: 246 unit passed (4 ignored: 3 requiring FLARE_LEGACY_FIXTURE, 1 child helper spawned by the interrupted-acquisition test), 8 integration passed, 0 failed. - cargo test --locked --release --manifest-path flare-cli/Cargo.toml: 219 unit passed (3 ignored: FLARE_LEGACY_FIXTURE and explicit multi-host hardware/distribution fixtures), 26 integration passed, 0 failed. - python3 verification/helios-lifecycle/check.py: all 8 TLA+ models and observed-transition conformance checks passed, including ResponseLifecycle.tla (868,192 states generated, 117,991 distinct states, 365,508 checked Rust transitions) and all 12 targeted lifecycle regression tests. Signed-off-by: Joshua Cartu <jcartu@users.noreply.github.com> Design-Signed-Off-By: Antigravity (Gemini 4 Neon); design-review=2e5b
00cbbdd to
111e27c
Compare
Fixes #448
Why
When Chat or Completions streaming is requested with
"stream_options": {"include_usage": true, "continuous_usage_stats": true}, intermediate SSE chunks previously reportedprompt_tokens_details.cached_tokens: 0even when the Helios scheduler reused a cached prefix, while only the terminal and final usage chunks reported the reused token count. Clients cancelling a stream early or inspecting per-chunk cumulative usage observed inconsistent cache counts relative to the documented cumulative usage behavior indocs/api.md.Encoder::usage_valuereads each prompt's cached count from its representative first choice (Encoder::usage.iter().step_by(choices_per_prompt)), which was previously populated only inEncoder::finishupon receivingGenerationEvent::Finished. A request's reused prompt token count is already finalized by the time the worker observes its first generated token: pre-token requeue transitions (failed host-restore fallback inWorker::service_storeand late pool-lookup upgrades inWorker::retry_late_pool_answers) occur only before any token is generated, and any later preemption, reasoning-budget cut, or speculative grammar transition locks the initial count intoCarried::reused_prompt_tokens.What changed
helios/src/server/runtime.rs: Changed the per-requestfirst_tokenoneshot channel onIncomingandGenerationfromResult<()>toResult<u64>, carryingcarried.reused_prompt_tokens.unwrap_or(live.reused_prompt_tokens())when the worker observes the first generated token (including forced close tokens inActive::close_reasoning). AddedIncoming::observe_token_withsoWorker::completeevaluates the scheduler lookup lazily only whilefirst_tokenis still pending, avoiding per-token scheduler lookups on subsequent tokens. StoredGeneration::first_tokenasOption<oneshot::Receiver<Result<u64>>>and addedGeneration::has_pending_first_tokenandGeneration::take_reused_prompt_tokensfor single consumption. NoGenerationEventvariant was added, leavingPrompt::output_event_capacityandResponseLifecycleunchanged.helios/src/server/protocol/encode.rs: AddedEncoder::report_cached_tokens(index, cached)to populateself.usage[index].cachedbeforeEncoder::finish.helios/src/server/http.rs: Recorded the reused prompt token count returned byGeneration::wait_for_first_tokenbeforeencoder.start()emits the initial Chat assistant chunk. For Completions withcontinuous_usage_stats, tracked only pending representative first choices ((0..size).step_by(decoded.request.choices)) and drained readyfirst_tokencounts onGenerationEvent::ObservedToken, eliminating scanning once all representative first choices have started. Anthropic Messages (message_startreports zero cache reads;message_deltareports final counts) and Responses streaming behavior are unchanged.helios/src/server/protocol/tests.rs&helios/src/server/runtime/phase_tests.rs: AddedEncoderunit tests covering Chat, Completions, zero prefix reuse, and multi-promptn=2streams, plus HTTP +Worker+RequestSchedulerstreaming tests covering Chat prefix reuse hit and miss, post-token preemption/readmission, Completionsn=2prefix reuse, staggered multi-promptn=2first-choice readiness, and Messagesmessage_start/message_deltabehavior.Claimed result
With
stream_options.include_usageandstream_options.continuous_usage_statsenabled:n=1Completions streaming chunk reports the finalizedprompt_tokens_details.cached_tokenscount matching the final usage chunk (0when no prefix is reused), and retains that initial count across post-token preemption and readmission.n > 1) and multi-prompt Completions streams report the cumulative reused prompt tokens across prompts whose representative first choice (prompt_index * n) has reached its first generated token, without letting sibling choices overwrite a prompt's admission count, and match the final usage chunk once all representative first choices have started.echo: trueemit echo frames before worker admission, so echo frames precede first-token readiness and continue to report0cached tokens.Evidence
Executed on Linux CPU without GPUs (no GPU or exported-core live serving test was run):
cargo test --locked --release --manifest-path helios/Cargo.toml --features runtime-core,server: 1821 unit tests passed (47 ignored), 62 integration tests passed (36 ignored), 0 failed. Ignored tests require local CUDA GPU hardware, exported cores, or external model checkpoint/tokenizer snapshots. Coverscontinuous_usage_reports_cached_tokens_before_finish,continuous_usage_multi_choice_reports_first_choice_cached_tokens_per_prompt,continuous_usage_streams_report_reused_prompt_tokens_and_preserve_messages,completions_continuous_usage_tracks_staggered_first_choice_readiness, andchat_continuous_usage_preserves_initial_cached_tokens_across_preemption.cargo test --locked --release --manifest-path flare/Cargo.toml: 246 unit tests passed (4 ignored: 3 requiringFLARE_LEGACY_FIXTURE, 1 helper child process spawned by the interrupted-acquisition test), 8 integration tests passed, 0 failed.cargo test --locked --release --manifest-path flare-cli/Cargo.toml: 219 unit tests passed (3 ignored:FLARE_LEGACY_FIXTUREand explicit multi-host hardware/distribution fixtures), 26 integration tests passed, 0 failed.python3 verification/helios-lifecycle/check.py: all 8 TLA+ models and Rust observed-transition conformance checks passed, includingResponseLifecycle.tla(868,192 states generated, 117,991 distinct states, 365,508 checked Rust transitions) and all 12 targeted lifecycle regression tests.Discord: jcartu