Skip to content

Make Codex and Claude Code subscriptions go further: a local model router that gives routine agent work to economical models and keeps your strongest model for the hard parts.

fix(gateway): preserve nonstream usage in completion receipts (#113)

澄潭cf0a0ef0a95f
diff --git a/crates/gateway/README.md b/crates/gateway/README.md
index ab0fe797..9e0bc50d 100644
--- a/crates/gateway/README.md
+++ b/crates/gateway/README.md
@@ -253,3 +253,10 @@ as streaming failures, for both native and converted protocols. Reported usage
survives fallback; partial response content is not delivered. Attempt/deadline
limits and fixed-source authority still apply, and unknown completion remains
separate from an explicit failure.
+
+Successful converted nonstream responses retain their reported usage in the
+provider completion facts before releasing the decoded response. Completion
+publishes that usage before request termination, so its receipt does not depend
+on canonical capture scheduling. Nonstream capture still records delivered
+content but leaves usage to provider completion; converted streaming capture
+continues publishing accepted usage events. Missing usage remains unknown.
diff --git a/crates/gateway/src/observation/provider/capture/tracker.rs b/crates/gateway/src/observation/provider/capture/tracker.rs
index f4867fde..6ac58098 100644
--- a/crates/gateway/src/observation/provider/capture/tracker.rs
+++ b/crates/gateway/src/observation/provider/capture/tracker.rs
@@ -20,6 +20,10 @@ const NATIVE_CAPTURE_PENDING_LIMIT: usize = 1024 * 1024;
#[path = "memory_tests.rs"]
mod memory_tests;
+#[cfg(test)]
+#[path = "usage_tests.rs"]
+mod usage_tests;
+
pub(super) struct CanonicalResponseTracker {
budget: StreamBudget,
profile: Arc<CandidateProtocolProfile>,
@@ -303,10 +307,12 @@ impl CanonicalResponseTracker {
.transpose()
.map_err(|_| "canonical_response_render_failed")?
.unwrap_or_default();
- // Same-protocol Native delivery records usage from the main
- // projector in provider completion. The strict decoder here is a
- // capture-only sidecar and must not create a second usage fact.
+ // Native delivery and converted nonstream responses retain usage
+ // in the main provider's completion facts. This capture sidecar
+ // must not report it again, including for Delta pricing semantics.
+ // Converted streams still publish their accepted usage events.
if !self.native_output
+ && self.streaming
&& let ModelEvent::UsageUpdated { usage } = &event.event
{
self.pending_delivery.merge_usage(usage);
diff --git a/crates/gateway/src/observation/provider/capture/usage_tests.rs b/crates/gateway/src/observation/provider/capture/usage_tests.rs
new file mode 100644
index 00000000..ae2d75fe
--- /dev/null
+++ b/crates/gateway/src/observation/provider/capture/usage_tests.rs
@@ -0,0 +1,85 @@
+//! Canonical capture preserves delivered content without duplicating the
+//! nonstream usage retained by the production response driver.
+use super::*;
+use crate::server::core_runtime::profiles::fixed_reasoning;
+use serde_json::json;
+
+fn capture(ingress: IngressProtocol, streaming: bool) -> CanonicalResponseTracker {
+ let profile = Arc::new(CandidateProtocolProfile::exact_portable_path(
+ ingress,
+ IngressProtocol::ChatCompletions,
+ "physical",
+ fixed_reasoning("fixed"),
+ ));
+ let mut capture = CanonicalResponseTracker::new(
+ profile,
+ ToolIdProjection::new(ingress),
+ None,
+ streaming,
+ "alias".into(),
+ );
+ capture.observe(CapturedNativeEvent::Head(200)).unwrap();
+ capture
+}
+
+fn accepted_delivery(capture: &mut CanonicalResponseTracker) -> CanonicalFrameDelivery {
+ let wire: Vec<_> = capture
+ .pending
+ .iter()
+ .flat_map(|unit| unit.bytes.iter().copied())
+ .collect();
+ assert!(!wire.is_empty());
+ capture.correlate_accepted_output(&wire, true).unwrap()
+}
+
+#[test]
+fn converted_nonstream_capture_leaves_usage_to_provider_completion() {
+ for ingress in [IngressProtocol::Responses, IngressProtocol::Messages] {
+ let mut capture = capture(ingress, false);
+ let body = json!({
+ "id": "usage", "object": "chat.completion", "created": 1, "model": "physical",
+ "choices": [{"index": 0, "message": {"role": "assistant", "content": "ok"},
+ "finish_reason": "stop"}],
+ "usage": {"prompt_tokens": 32, "completion_tokens": 45, "total_tokens": 77}
+ });
+ capture
+ .observe(CapturedNativeEvent::Body(
+ serde_json::to_vec(&body).unwrap(),
+ ))
+ .unwrap();
+ capture.observe(CapturedNativeEvent::EndStream).unwrap();
+ let delivery = accepted_delivery(&mut capture);
+ assert!(
+ !delivery.events.is_empty(),
+ "delivered content must still be captured"
+ );
+ assert!(
+ delivery.usage.is_none(),
+ "nonstream usage has one producer even when capture is processed first"
+ );
+ }
+}
+
+#[test]
+fn converted_stream_capture_keeps_delivered_usage() {
+ for ingress in [IngressProtocol::Responses, IngressProtocol::Messages] {
+ let mut capture = capture(ingress, true);
+ let event = json!({
+ "id": "usage", "object": "chat.completion.chunk", "created": 1, "model": "physical",
+ "choices": [{"index": 0, "delta": {"role": "assistant", "content": "ok"},
+ "finish_reason": "stop"}],
+ "usage": {"prompt_tokens": 32, "completion_tokens": 45, "total_tokens": 77}
+ });
+ capture
+ .observe(CapturedNativeEvent::Body(
+ format!("data: {event}\n\ndata: [DONE]\n\n").into_bytes(),
+ ))
+ .unwrap();
+ capture.observe(CapturedNativeEvent::EndStream).unwrap();
+ let delivery = accepted_delivery(&mut capture);
+ assert!(!delivery.events.is_empty());
+ let usage = delivery.usage.unwrap();
+ assert_eq!(usage.input_tokens, Some(32));
+ assert_eq!(usage.output_tokens, Some(45));
+ }
+}
diff --git a/crates/gateway/src/runtime/driver/response.rs b/crates/gateway/src/runtime/driver/response.rs
index 381f6636..6cc27123 100644
--- a/crates/gateway/src/runtime/driver/response.rs
+++ b/crates/gateway/src/runtime/driver/response.rs
@@ -306,9 +306,16 @@ pub(super) fn classify_precommit(
}
};
push_prefix_bytes(state, bytes)?;
+ // The nonstream decoder has already been consumed. Preserve
+ // its known usage for completion before RequestFinished;
+ // canonical capture remains independent of this result.
+ let mut facts = semantic_response_facts(status);
+ if !decoded.response.usage.is_empty() {
+ facts.usage = Some(model_usage_fact(&decoded.response.usage));
+ }
Ok(PrecommitClassification::classified(
ClassifiedAttemptResult {
- facts: semantic_response_facts(status),
+ facts,
readiness: take_readiness(state, rendered_status, content_type, true)?,
},
))
@@ -1135,3 +1142,7 @@ mod terminal_tests;
#[cfg(test)]
#[path = "response_budget_tests.rs"]
mod tests;
+
+#[cfg(test)]
+#[path = "response_usage_tests.rs"]
+mod usage_tests;
diff --git a/crates/gateway/src/runtime/driver/response_usage_tests.rs b/crates/gateway/src/runtime/driver/response_usage_tests.rs
new file mode 100644
index 00000000..57da79b4
--- /dev/null
+++ b/crates/gateway/src/runtime/driver/response_usage_tests.rs
@@ -0,0 +1,236 @@
+//! Preserve known nonstream usage before the decoder is consumed, without
+//! depending on the independent canonical capture worker.
+use super::*;
+use std::time::{Duration, Instant};
+
+use crate::ports::{ExecutionScope, InMemoryRuntimeStateStore, RuntimeStateKey};
+use crate::runtime::state::{OwnedAttemptStatePermits, acquire_target_permit};
+use crate::server::core_runtime::profiles::{ClientProtocolProfile, fixed_reasoning};
+use hiroute_gateway_core::core::execution_plan::{
+ CaPolicy, PoolEpoch, TransportReuseClassId, TransportScheme, TransportTarget,
+};
+use hiroute_gateway_core::runtime::attempt::{AttemptTransportFacts, CommitFence};
+use hiroute_gateway_core::runtime::body::{BodyPlan, BudgetTree};
+use hiroute_gateway_core::runtime::driver::{
+ AttemptCleanupOutcome, AttemptCommitFacts, AttemptDownstreamOutcome, AttemptStreamOutcome,
+ AttemptTerminationReason,
+};
+use serde_json::{Value, json};
+
+fn chat_response(with_usage: bool) -> Value {
+ let mut body = json!({
+ "id": "nonstream-usage", "object": "chat.completion", "created": 1,
+ "model": "physical",
+ "choices": [{"index": 0, "message": {"role": "assistant", "content": "ok"},
+ "finish_reason": "stop"}]
+ });
+ if with_usage {
+ body["usage"] = json!({
+ "prompt_tokens": 32, "completion_tokens": 45, "total_tokens": 77,
+ "prompt_tokens_details": {"cached_tokens": 0},
+ "completion_tokens_details": {"reasoning_tokens": 36}
+ });
+ }
+ body
+}
+
+async fn attempt_state(ingress: IngressProtocol) -> ProductionAttemptState {
+ let profile = CandidateProtocolProfile::exact_portable_path(
+ ingress,
+ IngressProtocol::ChatCompletions,
+ "physical",
+ fixed_reasoning("fixed"),
+ );
+ let native_output = ingress == IngressProtocol::ChatCompletions;
+ let budget = BudgetTree::new(4 * 1024 * 1024, 4 * 1024 * 1024)
+ .unwrap()
+ .stream(4 * 1024 * 1024)
+ .unwrap();
+ let store = InMemoryRuntimeStateStore::default();
+ let key = RuntimeStateKey::binding("nonstream-usage");
+ let scope = ExecutionScope::new(
+ Instant::now() + Duration::from_secs(1),
+ tokio_util::sync::CancellationToken::new(),
+ );
+ let permit = acquire_target_permit(&store, &key, &scope)
+ .await
+ .unwrap()
+ .unwrap();
+ let decoder = (!native_output).then(|| {
+ adapters::NativeResponseDecoder::new_for_attempt(&profile, 200, false, None, budget.clone())
+ .unwrap()
+ });
+ let projector = native_output.then(|| {
+ Box::new(
+ adapters::NativeResponseProjector::new_for_attempt(
+ &profile,
+ false,
+ "alias".into(),
+ None,
+ budget.clone(),
+ )
+ .unwrap(),
+ )
+ });
+ let plan = BodyPlan::PassThrough {
+ max_chunk_bytes: 64 * 1024,
+ };
+ let prefix =
+ ChargedBodyQueue::new(&budget, MemoryRole::ResponsePrefix, &plan, 256 * 1024, 64).unwrap();
+ ProductionAttemptState {
+ managed_cpa: false,
+ #[cfg(all(unix, debug_assertions))]
+ capture: None,
+ response_status: Some(StatusCode::OK),
+ resolved_target: TransportTarget {
+ scheme: TransportScheme::Http,
+ authority: "127.0.0.1:1".into(),
+ addresses: Arc::from(["127.0.0.1:1".parse().unwrap()]),
+ sni: None,
+ ca: CaPolicy::System,
+ alpn: Arc::from([]),
+ connect_timeout: Duration::from_secs(1),
+ transport_read_buffer_bytes: 16 * 1024,
+ h2_stream_window_bytes: 65_535,
+ h2_connection_window_bytes: 65_535,
+ h2_max_concurrent_streams: 1,
+ reuse_class: TransportReuseClassId(1),
+ pool_epoch: PoolEpoch(1),
+ connection_fingerprint: [0; 32],
+ },
+ permits: OwnedAttemptStatePermits::without_credential(key, permit),
+ classified_failure: None,
+ client_profile: ClientProtocolProfile::for_candidate(&profile).unwrap(),
+ profile,
+ served_model_alias: "alias".into(),
+ streaming: false,
+ native_output,
+ omit_thinking: false,
+ chat_tool_projection: None,
+ chat_tool_projection_budget: None,
+ decoder,
+ renderer: None,
+ projector,
+ prefix: Some(prefix),
+ prefix_terminal_chunks: None,
+ budget,
+ semantic_seen: false,
+ semantic_terminal: None,
+ retry_after: None,
+ runtime_state_authority: Default::default(),
+ }
+}
+
+async fn completed_facts(
+ ingress: IngressProtocol,
+ with_usage: bool,
+) -> ProviderClassificationFacts {
+ let mut state = attempt_state(ingress).await;
+ let bytes = serde_json::to_vec(&chat_response(with_usage)).unwrap();
+ let body_bytes = bytes.len() as u64;
+ let input =
+ ChargedBytes::copy_from_opaque(&state.budget, MemoryRole::ResponsePrefix, &bytes).unwrap();
+ assert!(
+ classify_precommit(&mut state, PrecommitEvent::Body(input))
+ .unwrap()
+ .classified
+ .is_none()
+ );
+ let classified = classify_precommit(&mut state, PrecommitEvent::EndStream)
+ .unwrap()
+ .classified
+ .unwrap();
+ assert_eq!(
+ state.semantic_terminal,
+ Some(SemanticTerminalOutcome::Complete)
+ );
+ if !state.native_output {
+ assert!(state.decoder.is_none());
+ assert!(classified.readiness.decoder.is_none());
+ assert_eq!(
+ classified.facts.usage.is_some(),
+ with_usage,
+ "converted nonstream usage must survive consuming the decoder"
+ );
+ }
+ let now = Instant::now();
+ let completion = ProviderAttemptCompletion {
+ disposition: Disposition::Accept,
+ commits: AttemptCommitFacts {
+ upstream_request: CommitFence::WriteConfirmed,
+ downstream_headers: CommitFence::WriteConfirmed,
+ downstream_semantic: CommitFence::WriteConfirmed,
+ },
+ stream: AttemptStreamOutcome::CompletedEos,
+ downstream: AttemptDownstreamOutcome::Completed,
+ cleanup: AttemptCleanupOutcome::Completed,
+ transport: AttemptTransportFacts {
+ started_at: now,
+ upstream_protocol: None,
+ connect_elapsed: None,
+ request_write_elapsed: None,
+ upstream_ttfb: None,
+ last_upstream_progress_at: None,
+ local_read_suppressed: Duration::ZERO,
+ upstream_body_bytes: body_bytes,
+ timeout: None,
+ },
+ ended_at: now,
+ termination_reason: AttemptTerminationReason::AcceptedEos,
+ };
+ let facts = finalize_attempt_facts(
+ &state,
+ Some(&classified.readiness),
+ Some(&classified.facts),
+ &completion,
+ );
+ assert_eq!(
+ facts.model_event.as_ref().unwrap().as_str(),
+ "response_complete"
+ );
+ facts
+}
+
+#[tokio::test]
+async fn converted_nonstream_success_preserves_usage_after_decoder_consumption() {
+ for ingress in [IngressProtocol::Responses, IngressProtocol::Messages] {
+ let facts = completed_facts(ingress, true).await;
+ assert!(facts.error_class.is_none(), "{ingress:?}: {facts:?}");
+ let usage = facts.usage.unwrap();
+ assert_eq!(usage.input, UsageDimension::reported(32));
+ assert_eq!(usage.output, UsageDimension::reported(45));
+ assert_eq!(usage.cache_read, UsageDimension::reported(0));
+ assert_eq!(usage.reasoning, UsageDimension::reported(36));
+ assert_eq!(usage.cache_write, UsageDimension::unknown());
+ assert_eq!(usage.billable, UsageDimension::unknown());
+ }
+}
+
+#[tokio::test]
+async fn native_nonstream_success_keeps_the_same_reported_usage() {
+ let facts = completed_facts(IngressProtocol::ChatCompletions, true).await;
+ assert!(facts.error_class.is_none(), "{facts:?}");
+ let usage = facts.usage.unwrap();
+ assert_eq!(usage.input, UsageDimension::reported(32));
+ assert_eq!(usage.output, UsageDimension::reported(45));
+ assert_eq!(usage.cache_read, UsageDimension::reported(0));
+ assert_eq!(usage.reasoning, UsageDimension::reported(36));
+ assert_eq!(usage.cache_write, UsageDimension::unknown());
+ assert_eq!(usage.billable, UsageDimension::unknown());
+}
+
+#[tokio::test]
+async fn native_nonstream_success_without_reported_usage_does_not_invent_zeroes() {
+ let facts = completed_facts(IngressProtocol::ChatCompletions, false).await;
+ assert!(facts.error_class.is_none(), "{facts:?}");
+ assert!(facts.usage.is_none());
+}
+
+#[tokio::test]
+async fn converted_nonstream_without_required_usage_remains_a_protocol_failure() {
+ for ingress in [IngressProtocol::Responses, IngressProtocol::Messages] {
+ let facts = completed_facts(ingress, false).await;
+ assert!(facts.error_class.is_some(), "{ingress:?}: {facts:?}");
+ assert!(facts.usage.is_none());
+ }
+}
diff --git a/docs/code-map/architecture.md b/docs/code-map/architecture.md
index 36d5b642..4425f016 100644
--- a/docs/code-map/architecture.md
+++ b/docs/code-map/architecture.md
@@ -96,6 +96,7 @@ Scan → Prepare → Save → restart → native use, including stale-source rej
| Lifecycle acceptance of empty response end-stream frames | [Transport acceptance capture](../../crates/gateway/src/observation/content.rs), [Product lifecycle validation](../../crates/domain/src/observation/lifecycle.rs), [bounded receiver](../../crates/observation/src/writer/lifecycle.rs) | Successful zero-byte end-stream writes are real operational events with a zero byte count; identity remains strict. [Receiver regressions](../../crates/observation/src/writer/lifecycle_tests.rs) preserve continuous ACK through RequestFinished |
| Capture completeness | [Content completeness](../../crates/observation/src/content/completeness.rs), [session query](../../crates/observation/src/query_v2/sessions.rs) | Aggregate every required request/direction; a finished stream or readable preview does not prove the session is complete |
| Streaming memory | [Capture](../../crates/gateway/src/observation/provider/capture.rs), [pending delivery](../../crates/gateway/src/observation/provider/capture/tracker.rs) | Decoder, projector and delivery tracker share the stream budget; queue capacity is not capture evidence |
+| Reported token usage | [Response driver](../../crates/gateway/src/runtime/driver/response.rs), [completion facts](../../crates/gateway/src/observation/request/completion.rs), [capture tracker](../../crates/gateway/src/observation/provider/capture/tracker.rs) | Nonstream completion retains known usage before request termination; capture records content without duplicating it. Follow the [Gateway guide](../../crates/gateway/README.md#response-failure-evidence) for streaming ownership. |
| Upstream fault attribution | [Wire projection](../../crates/gateway/src/observation/provider/wire_diagnostic.rs), [attempt association](../../crates/gateway/src/observation/request/diagnostic.rs), [decision calls](../../crates/gateway/src/core_runtime/classification/diagnostic.rs) | Actual bounded model/control fields, negotiated HTTP protocol, closed error codes, hashed request IDs and separate client headers/body commit facts must preserve the real outcome without prompts or provider error text |
| Source disabled before an upstream attempt | [Exact permission gate](../../crates/local-storage/src/secrets/runtime.rs), [product preparation reply](../../crates/gateway/src/runtime/driver/selection/preparation.rs), [Gateway error guide](../../crates/gateway/README.md#candidate-preparation-errors) | Preserve the closed lease denial in the request fact and local JSON response; a frozen route does not grant current source/Key permission, and rejected preparation remains zero attempts |
| Inspect CPA authentication recovery | [Private response metadata](../../crates/gateway/src/observation/provider/cpa_execution.rs), [pinned CPA adapter](../../vendor/cpa/README.md) | Logical attempts and actual HTTP sends differ; only verified CPA targets can supply recovery counters. Missing evidence remains unknown and does not create token usage. |
diff --git a/tools/e2e-harness/tests/p0_gateway_runtime/nonstream_terminal.rs b/tools/e2e-harness/tests/p0_gateway_runtime/nonstream_terminal.rs
index 15d3a5bd..3590c398 100644
--- a/tools/e2e-harness/tests/p0_gateway_runtime/nonstream_terminal.rs
+++ b/tools/e2e-harness/tests/p0_gateway_runtime/nonstream_terminal.rs
@@ -4,6 +4,86 @@ const RESPONSES_INCOMPLETE: &[u8] = br#"{"id":"partial-response","model":"runtim
const CHAT_INCOMPLETE: &[u8] = br#"{"id":"partial-chat","object":"chat.completion","created":1,"model":"runtime-native","choices":[{"index":0,"message":{"role":"assistant","content":"PRIVATE_PARTIAL_BODY"},"finish_reason":"length"}],"usage":{"prompt_tokens":7,"completion_tokens":2,"total_tokens":9}}"#;
const MESSAGES_INCOMPLETE: &[u8] = br#"{"id":"partial-message","type":"message","role":"assistant","model":"runtime-native","content":[{"type":"text","text":"PRIVATE_PARTIAL_BODY"}],"stop_reason":"max_tokens","stop_sequence":null,"usage":{"input_tokens":7,"output_tokens":2}}"#;
+#[test]
+fn successful_nonstream_usage_is_recorded_before_request_finish_without_a_decision_stage() {
+ for (protocol, body) in [
+ ("responses", br#"{"id":"usage","model":"runtime-native","status":"completed","output":[{"type":"message","role":"assistant","content":[{"type":"output_text","text":"ok"}]}],"usage":{"input_tokens":32,"output_tokens":45,"total_tokens":77,"input_tokens_details":{"cached_tokens":0},"output_tokens_details":{"reasoning_tokens":36}}}"#.as_slice()),
+ ("chat_completions", br#"{"id":"usage","object":"chat.completion","created":1,"model":"runtime-native","choices":[{"index":0,"message":{"role":"assistant","content":"ok"},"finish_reason":"stop"}],"usage":{"prompt_tokens":32,"completion_tokens":45,"total_tokens":77,"prompt_tokens_details":{"cached_tokens":0},"completion_tokens_details":{"reasoning_tokens":36}}}"#.as_slice()),
+ ("messages", br#"{"id":"usage","type":"message","role":"assistant","model":"runtime-native","content":[{"type":"text","text":"ok"}],"stop_reason":"end_turn","stop_sequence":null,"usage":{"input_tokens":32,"output_tokens":45,"cache_read_input_tokens":0}}"#.as_slice()),
+ ] {
+ serde_json::from_slice::<serde_json::Value>(body)
+ .expect("valid nonstream provider JSON fixture");
+ let provider = NativeProvider::start(vec![
+ ProviderReply::Complete { status: 200, error_kind: None, body },
+ ProviderReply::Complete { status: 200, error_kind: None, body },
+ ]);
+ let fixture = RuntimeFixture::launch_with_observation_and_publication_candidates(
+ &[&provider], 1, ObservationFaults::healthy(),
+ Some(&[PublicationCandidate {
+ provider_index: 0, upstream_protocol: protocol, statically_enabled: true,
+ }]),
+ );
+ let response = fixture.request_body(br#"{"model":"runtime-model","input":"hello","stream":false}"#);
+ let facts = wait_execution_facts(&fixture, 1);
+ assert_eq!(provider.calls(), 1);
+ let requests = provider.requests();
+ let offset = requests[0].windows(4).position(|part| part == b"\r\n\r\n").unwrap() + 4;
+ let upstream: serde_json::Value = serde_json::from_slice(&requests[0][offset..]).unwrap();
+ let observed: Vec<_> = facts.iter().map(|row| &row["fact"]).collect();
+ assert_eq!(upstream["stream"], false, "{protocol}: {observed:?}");
+ assert_eq!(response.status, 200, "{protocol}: {observed:?}");
+ let document: serde_json::Value = serde_json::from_slice(&response.body).unwrap();
+ assert_eq!(document["status"], "completed");
+ assert_eq!(document["usage"]["input_tokens"], 32);
+ assert_eq!(document["usage"]["output_tokens"], 45);
+
+ let terminal = facts.iter().find(|row| row["fact"]["kind"] == "request_finished").unwrap();
+ assert_eq!(terminal["fact"]["outcome"], "accepted");
+ assert_eq!(terminal["fact"]["facts_completeness"], "complete");
+ let request_id = terminal["correlation"]["request_id"].as_str().unwrap();
+ // The content terminal proves this capture worker has enqueued any
+ // usage. Content and execution have independent persistence workers.
+ let path = fixture.observation_root.as_ref().unwrap().join("conversation-content.jsonl");
+ let deadline = Instant::now() + Duration::from_secs(5);
+ loop {
+ let complete = std::fs::read_to_string(&path).ok().is_some_and(|text| {
+ text.lines().filter(|line| !line.is_empty()).any(|line| {
+ let row: serde_json::Value = serde_json::from_str(line).unwrap();
+ row["correlation"]["request_id"] == request_id
+ && row["direction"] == "response_delivered" && row["phase"] == "finish"
+ })
+ });
+ if complete { break; }
+ assert!(Instant::now() < deadline, "response capture did not complete: {protocol}");
+ std::thread::sleep(Duration::from_millis(10));
+ }
+ // A subsequent execution terminal on the same FIFO producer proves
+ // the first capture's enqueued facts have also been persisted.
+ let marker = fixture.request_body(br#"{"model":"runtime-model","input":"usage flush marker","stream":false}"#);
+ assert_eq!(marker.status, 200, "{protocol}");
+ assert_eq!(provider.calls(), 2);
+ let facts = wait_execution_facts(&fixture, 2);
+ let usage: Vec<_> = facts.iter().filter(|row|
+ row["correlation"]["request_id"] == request_id
+ && row["fact"]["kind"] == "usage_and_cache"
+ ).collect();
+ assert_eq!(usage.len(), 1, "{protocol}: {facts:?}");
+ let usage = usage[0];
+ assert_eq!(usage["fact"]["source"], "provider_completion");
+ assert_eq!(usage["fact"]["ordinal"], 1);
+ assert_eq!(usage["correlation"]["request_id"], request_id);
+ assert!(usage["sequence"].as_u64().unwrap() < terminal["sequence"].as_u64().unwrap());
+ for (field, expected) in [("input_tokens", 32), ("output_tokens", 45), ("cache_read_tokens", 0)] {
+ assert_eq!(usage["fact"][field], expected, "{protocol}: {field}");
+ }
+ assert_eq!(usage["fact"]["reasoning_tokens"], if protocol == "messages" { serde_json::Value::Null } else { serde_json::json!(36) });
+ assert!(usage["fact"]["cache_write_tokens"].is_null());
+ assert!(usage["fact"]["billable_tokens"].is_null());
+ assert_eq!(usage["fact"]["input_provenance"], "reported");
+ assert!(!facts.iter().any(|row| row["fact"]["kind"] == "agent_turn_finished"));
+ }
+}
+
#[test]
fn known_nonstream_incomplete_relays_before_body_and_retains_usage_within_budget() {
for (protocol, body) in [