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: repair Bailian requests and upstream fallback (#64)

澄潭23deefe8a381
diff --git a/apps/desktop/src/features/decision-services/presentation.ts b/apps/desktop/src/features/decision-services/presentation.ts
index 9fcee72a..3d7e0326 100644
--- a/apps/desktop/src/features/decision-services/presentation.ts
+++ b/apps/desktop/src/features/decision-services/presentation.ts
@@ -44,7 +44,10 @@ export function decisionFailureHelp(code: string, language: 'zh' | 'en'): string
const t = (zh: string, en: string) => language === 'zh' ? zh : en;
switch (code) {
case 'CLASSIFIER_TIMEOUT': return t('连接超时。请检查网络和接入点,或在高级设置中适当增加超时。', 'The connection timed out. Check the network and endpoint, or increase the timeout in advanced settings.');
- case 'CLASSIFIER_INPUT_REJECTED': return t('服务拒绝了请求。请核对 API Key 权限、模型名称和完整接入点。', 'The service rejected the request. Check API key permissions, the model name and the complete endpoint.');
+ case 'CLASSIFIER_AUTH_REJECTED': return t('认证被拒绝。请核对 API Key 及其工作空间权限。', 'Authentication was rejected. Check the API key and its workspace permissions.');
+ case 'CLASSIFIER_RATE_LIMITED': return t('服务请求过于频繁,请稍后重试或核对配额。', 'The service rate limit was reached. Retry later or check your quota.');
+ case 'CLASSIFIER_ENDPOINT_REJECTED': return t('服务不接受当前工作空间接入点,请核对完整地址和工作空间配置。', 'The service rejected this workspace endpoint. Check the full address and workspace configuration.');
+ case 'CLASSIFIER_INPUT_REJECTED': return t('服务拒绝了请求。请核对模型名称、完整接入点及协议兼容性;这不一定是 API Key 问题。', 'The service rejected the request. Check the model, full endpoint and protocol compatibility; this does not necessarily indicate an API key problem.');
case 'CLASSIFIER_OUTPUT_INVALID': return t('响应不符合决策协议。请检查接入点及模型是否支持决策接口;自定义扩展需返回允许的分支。', 'The response does not match the decision protocol. Check decision API support for the endpoint and model; an extension must return an allowed branch.');
case 'CLASSIFIER_CONFIG_INVALID':
case 'DECISION_SERVICE_INVALID': return t('连接配置无效。请核对完整地址、认证和超时设置。', 'The connection configuration is invalid. Check the complete URL, authentication and timeout.');
diff --git a/apps/desktop/tests/decision-presentation.test.mjs b/apps/desktop/tests/decision-presentation.test.mjs
index ed52de5f..d035c62e 100644
--- a/apps/desktop/tests/decision-presentation.test.mjs
+++ b/apps/desktop/tests/decision-presentation.test.mjs
@@ -1,6 +1,6 @@
import { test } from 'node:test';
import assert from 'node:assert/strict';
-import { initialAuthMode, protectedInput, sameEndpointOrigin, testKey, decisionReferences } from '../src/features/decision-services/presentation.ts';
+import { decisionFailureHelp, initialAuthMode, protectedInput, sameEndpointOrigin, testKey, decisionReferences } from '../src/features/decision-services/presentation.ts';
import { protocolExamples, protocolCurl } from '../src/features/decision-services/protocol-examples.ts';
import { defaultJudgment, judgmentIssue } from '../src/features/decision-services/types.ts';
import { readFileSync } from 'node:fs';
@@ -54,3 +54,11 @@ test('protocol examples separate the new task decision from its prior assessment
assert.deepEqual(request, protocolExamples[key].request);
}
});
+
+test('decision diagnostics distinguish confirmed causes without exposing arbitrary service text', () => {
+ assert.match(decisionFailureHelp('CLASSIFIER_AUTH_REJECTED', 'en'), /Authentication was rejected/);
+ assert.match(decisionFailureHelp('CLASSIFIER_ENDPOINT_REJECTED', 'en'), /workspace endpoint/);
+ assert.match(decisionFailureHelp('CLASSIFIER_RATE_LIMITED', 'en'), /rate limit/);
+ assert.match(decisionFailureHelp('CLASSIFIER_INPUT_REJECTED', 'zh'), /协议兼容性/);
+ assert.doesNotMatch(decisionFailureHelp('private upstream details', 'en'), /private upstream details/);
+});
diff --git a/apps/desktop/tests/v3/browser/product-shell-scenarios.mjs b/apps/desktop/tests/v3/browser/product-shell-scenarios.mjs
index 641358d0..ca3ddeaa 100644
--- a/apps/desktop/tests/v3/browser/product-shell-scenarios.mjs
+++ b/apps/desktop/tests/v3/browser/product-shell-scenarios.mjs
@@ -301,7 +301,7 @@ const scenarios = [
assert(saved.secret === 'synthetic-test-key', 'Built-in auth was incorrectly prefixed by UI');
const tested = calls('test_classifier_decision').at(-1).payload.input.classifier.service;
assert(tested.revision === 1 && tested.connection.auth_header.value_secret_ref === 'protected/r1', 'Test used the unsaved editor or old credential');
- assert(text().includes('核对 API Key 权限') && !text().includes('CLASSIFIER_INPUT_REJECTED'), 'Failure is not actionable or exposes raw code by default');
+ assert(text().includes('协议兼容性') && !text().includes('CLASSIFIER_INPUT_REJECTED'), 'Failure is not actionable or exposes raw code by default');
await click('修改连接'); await click('更换');
setInput(input('credential'), 'replacement-key');
form().querySelector('summary').click(); await pause(30);
diff --git a/contracts/compatibility-support.v1.json b/contracts/compatibility-support.v1.json
index b24088be..a104b89d 100644
--- a/contracts/compatibility-support.v1.json
+++ b/contracts/compatibility-support.v1.json
@@ -1232,6 +1232,33 @@
"crates/daemon/src/control/runtime/delegation_tasks.rs",
"crates/daemon/src/control/runtime_early_cancel_tests.rs"
]
+ },
+ {
+ "contract": "hiroute.candidate-protocol-profile/v1",
+ "field": "0.2.0 toggle high / Boolean controls and Messages budget-only profiles",
+ "mode": "recovery_read_only",
+ "owner": "gateway/adapters/request/reasoning",
+ "reason": "Supported release d625e653 projected enabled toggles as Responses high or native Boolean fields and manual Messages thinking without a budget. Normalize protocol controls at serialization without rewriting stored profiles or their digests. Explicit discrete effort retains its value. Frozen regression input is copied from the released producer.",
+ "removal_condition": "Retire only when supported retained publications no longer include these released profiles or an explicit identity-preserving migration replaces the reader.",
+ "allowed_paths": [
+ "crates/domain/src/routing/gateway_execution.rs",
+ "crates/gateway/src/profiles/reasoning.rs",
+ "crates/gateway/src/adapters/request/reasoning.rs",
+ "crates/gateway/src/adapters/native_reasoning_tests.rs"
+ ]
+ },
+ {
+ "contract": "hiroute.diagnostic-event/v1",
+ "mode": "recovery_read_only",
+ "owner": "runtime-diagnostics",
+ "reason": "Read retained V1 diagnostic records without the newer optional model, reasoning, HTTP protocol, failure, model terminal result and commit facts. Missing fields remain unknown. Transport completion is separate from unknown model completion evidence; current producers emit observed facts and never rewrite historical logs.",
+ "removal_condition": "Remove when retained V1 log support no longer includes records produced before the Bailian wire diagnostic extension.",
+ "allowed_paths": [
+ "crates/diagnostics/src/event/model.rs",
+ "crates/diagnostics/src/record.rs",
+ "crates/diagnostics/src/runtime.rs"
+ ],
+ "field": "optional model, reasoning, HTTP protocol, provider error and model terminal result, target and commit facts in retained V1 logs"
}
]
}
diff --git a/crates/cpa-bridge/src/borrowed_codex/tests.rs b/crates/cpa-bridge/src/borrowed_codex/tests.rs
index 9fd61add..f544a1e6 100644
--- a/crates/cpa-bridge/src/borrowed_codex/tests.rs
+++ b/crates/cpa-bridge/src/borrowed_codex/tests.rs
@@ -1,3 +1,27 @@
+pub(crate) fn isolated_lock_test(test_name: &str) -> bool {
+ // Other concurrently forked tests may temporarily retain a CLOEXEC descriptor.
+ // Keep the production open-file-description lifetime and isolate its assertion.
+ const CHILD: &str = "HIROUTE_ISOLATED_LOCK_TEST";
+ if std::env::var(CHILD).as_deref() == Ok(test_name) {
+ return false;
+ }
+ let output = std::process::Command::new(std::env::current_exe().unwrap())
+ .args(["--exact", test_name])
+ .env(CHILD, test_name)
+ .output()
+ .unwrap();
+ let stdout = String::from_utf8_lossy(&output.stdout);
+ assert!(
+ output.status.success()
+ && stdout
+ .lines()
+ .any(|line| line == format!("test {test_name} ... ok")),
+ "isolated lease test must execute its exact case: {stdout} {}",
+ String::from_utf8_lossy(&output.stderr)
+ );
+ true
+}
+
use serde_json::{Value, json};
use super::*;
@@ -46,6 +70,12 @@ fn flat_value(auth_dir: &Path) -> Value {
#[cfg(unix)]
#[test]
fn selected_engine_version_is_carried_to_cpa_and_refreshed_on_reacquire() {
+ if isolated_lock_test(
+ "borrowed_codex::tests::selected_engine_version_is_carried_to_cpa_and_refreshed_on_reacquire",
+ ) {
+ return;
+ }
+
use std::os::unix::fs::PermissionsExt;
let (temp, auth_dir, source) = setup();
let executable = temp.path().join("selected-codex");
@@ -247,26 +277,9 @@ fn access_rotation_advances_generation_but_account_replacement_is_rejected() {
#[test]
fn canonical_source_and_auth_directory_are_exclusive_across_instances() {
- // Closing one descriptor cannot release copies inherited by concurrently forked
- // process tests. Keep this exact lifetime assertion in its own test process.
- const CASE: &str =
- "borrowed_codex::tests::canonical_source_and_auth_directory_are_exclusive_across_instances";
- const CHILD: &str = "HIROUTE_ISOLATED_LOCK_TEST";
- if std::env::var(CHILD).as_deref() != Ok(CASE) {
- let output = std::process::Command::new(std::env::current_exe().unwrap())
- .args(["--exact", CASE])
- .env(CHILD, CASE)
- .output()
- .unwrap();
- let stdout = String::from_utf8_lossy(&output.stdout);
- assert!(
- output.status.success()
- && stdout
- .lines()
- .any(|line| line == format!("test {CASE} ... ok")),
- "isolated lease test must execute its exact case: {stdout} {}",
- String::from_utf8_lossy(&output.stderr)
- );
+ if isolated_lock_test(
+ "borrowed_codex::tests::canonical_source_and_auth_directory_are_exclusive_across_instances",
+ ) {
return;
}
let (temp, auth_dir, source) = setup();
diff --git a/crates/daemon/src/control/runtime/candidate_execution_tests.rs b/crates/daemon/src/control/runtime/candidate_execution_tests.rs
index 1be546fe..caeedfdf 100644
--- a/crates/daemon/src/control/runtime/candidate_execution_tests.rs
+++ b/crates/daemon/src/control/runtime/candidate_execution_tests.rs
@@ -622,7 +622,20 @@ fn unmarked_native_controls_never_infer_adaptive_from_model_name() {
UpstreamProtocol::Responses,
UpstreamProtocol::ChatCompletions,
] {
- let profiles = reasoning_profiles(&native, protocol).unwrap();
+ let profiles = reasoning_profiles(&native, protocol);
+ if protocol == UpstreamProtocol::Responses
+ && matches!(
+ native.capability,
+ NativeReasoningCapabilityV1::Budget { .. }
+ )
+ {
+ assert!(
+ profiles.is_none(),
+ "Responses has no standard budget control"
+ );
+ continue;
+ }
+ let profiles = profiles.unwrap();
assert!(
!serde_json::to_string(&profiles)
.unwrap()
@@ -635,7 +648,7 @@ fn unmarked_native_controls_never_infer_adaptive_from_model_name() {
}
GatewayNativeReasoningRenderV1::ExactFields { fields, .. }
| GatewayNativeReasoningRenderV1::ExactBudget { fields, .. } => {
- assert_eq!(fields.len(), 1)
+ assert!(!fields.is_empty())
}
}
}
@@ -662,7 +675,7 @@ fn portable_thinking_toggle_renders_the_selected_native_protocol() {
(
UpstreamProtocol::Responses,
vec!["reasoning", "effort"],
- GatewayNativeReasoningValueV1::String("high".into()),
+ GatewayNativeReasoningValueV1::String("low".into()),
GatewayNativeReasoningValueV1::String("none".into()),
),
(
@@ -677,7 +690,25 @@ fn portable_thinking_toggle_renders_the_selected_native_protocol() {
let GatewayNativeReasoningRenderV1::ExactFields { fields, .. } = &profile.render else {
panic!("toggle must render an explicit wire field");
};
- assert_eq!(fields.len(), 1);
+ let enabled = profile.profile_id == "enabled";
+ assert_eq!(
+ fields.len(),
+ if protocol == UpstreamProtocol::Messages && enabled {
+ 2
+ } else {
+ 1
+ }
+ );
+ if protocol == UpstreamProtocol::Messages && enabled {
+ assert_eq!(
+ fields[1],
+ field(
+ "thinking.budget_tokens",
+ GatewayNativeReasoningValueV1::U64(1024),
+ protocol
+ )
+ );
+ }
assert_eq!(fields[0].path, path);
assert_eq!(fields[0].value, expected);
}
@@ -703,7 +734,7 @@ fn deepseek_toggle_uses_each_native_protocols_documented_control() {
(
UpstreamProtocol::Responses,
"reasoning.effort",
- "high",
+ "low",
"none",
),
(
@@ -726,7 +757,25 @@ fn deepseek_toggle_uses_each_native_protocols_documented_control() {
protocol
)
);
- assert_eq!(fields.len(), 1);
+ let enabled = profile.profile_id == "enabled";
+ assert_eq!(
+ fields.len(),
+ if protocol == UpstreamProtocol::Messages && enabled {
+ 2
+ } else {
+ 1
+ }
+ );
+ if protocol == UpstreamProtocol::Messages && enabled {
+ assert_eq!(
+ fields[1],
+ field(
+ "thinking.budget_tokens",
+ GatewayNativeReasoningValueV1::U64(1024),
+ protocol
+ )
+ );
+ }
}
}
}
diff --git a/crates/daemon/src/control/runtime/candidate_protocol_profiles.rs b/crates/daemon/src/control/runtime/candidate_protocol_profiles.rs
index e895354a..b07a79d2 100644
--- a/crates/daemon/src/control/runtime/candidate_protocol_profiles.rs
+++ b/crates/daemon/src/control/runtime/candidate_protocol_profiles.rs
@@ -193,56 +193,7 @@ pub(super) fn reasoning_profiles(
GatewayReasoningControlKindV1::Toggle,
GatewayNativeReasoningRenderV1::ExactFields {
protocol,
- fields: if parameter == "deepseek_thinking" {
- match protocol {
- UpstreamProtocol::Responses => vec![field(
- "reasoning.effort",
- GatewayNativeReasoningValueV1::String(
- if enabled { "high" } else { "none" }.into(),
- ),
- protocol,
- )],
- UpstreamProtocol::ChatCompletions | UpstreamProtocol::Messages => {
- vec![field(
- "thinking.type",
- GatewayNativeReasoningValueV1::String(
- if enabled { "enabled" } else { "disabled" }.into(),
- ),
- protocol,
- )]
- }
- }
- } else if parameter == "enable_thinking" {
- match protocol {
- UpstreamProtocol::ChatCompletions => vec![field(
- "enable_thinking",
- GatewayNativeReasoningValueV1::Bool(enabled),
- protocol,
- )],
- UpstreamProtocol::Responses => vec![field(
- "reasoning.effort",
- GatewayNativeReasoningValueV1::String(
- if enabled { "high" } else { "none" }.into(),
- ),
- protocol,
- )],
- UpstreamProtocol::Messages => {
- vec![field(
- "thinking.type",
- GatewayNativeReasoningValueV1::String(
- if enabled { "enabled" } else { "disabled" }.into(),
- ),
- protocol,
- )]
- }
- }
- } else {
- vec![field(
- parameter,
- GatewayNativeReasoningValueV1::Bool(enabled),
- protocol,
- )]
- },
+ fields: toggle_fields(parameter, enabled, protocol),
},
)
})
@@ -306,6 +257,9 @@ pub(super) fn reasoning_profiles(
maximum_tokens,
step_tokens,
} => {
+ if protocol == UpstreamProtocol::Responses {
+ return None;
+ }
let count = u64::from((maximum_tokens - minimum_tokens) / step_tokens) + 1;
if count > MAX_MATERIALIZED_BUDGET_PROFILES {
return None;
@@ -319,11 +273,21 @@ pub(super) fn reasoning_profiles(
GatewayReasoningControlKindV1::Budget,
GatewayNativeReasoningRenderV1::ExactBudget {
protocol,
- fields: vec![field(
- parameter,
- GatewayNativeReasoningValueV1::U64(value),
- protocol,
- )],
+ fields: {
+ let mut fields = vec![field(
+ parameter,
+ GatewayNativeReasoningValueV1::U64(value),
+ protocol,
+ )];
+ if protocol == UpstreamProtocol::Messages {
+ fields.push(field(
+ "thinking.type",
+ GatewayNativeReasoningValueV1::String("enabled".into()),
+ protocol,
+ ));
+ }
+ fields
+ },
budget_path: parameter_path(parameter, protocol),
selected_tokens: value,
min_tokens: u64::from(*minimum_tokens),
@@ -409,3 +373,46 @@ pub(super) fn field(
pub(super) fn parameter_path(parameter: &str, protocol: UpstreamProtocol) -> Vec<String> {
GatewayNativeReasoningFieldAssignmentV1::parameter_path(parameter, protocol)
}
+
+fn toggle_fields(
+ parameter: &str,
+ enabled: bool,
+ protocol: UpstreamProtocol,
+) -> Vec<GatewayNativeReasoningFieldAssignmentV1> {
+ match protocol {
+ UpstreamProtocol::Responses => vec![field(
+ "reasoning.effort",
+ GatewayNativeReasoningValueV1::String(if enabled { "low" } else { "none" }.into()),
+ protocol,
+ )],
+ UpstreamProtocol::Messages => {
+ let mut fields = vec![field(
+ "thinking.type",
+ GatewayNativeReasoningValueV1::String(
+ if enabled { "enabled" } else { "disabled" }.into(),
+ ),
+ protocol,
+ )];
+ if enabled {
+ fields.push(field(
+ "thinking.budget_tokens",
+ GatewayNativeReasoningValueV1::U64(1024),
+ protocol,
+ ));
+ }
+ fields
+ }
+ UpstreamProtocol::ChatCompletions if parameter == "deepseek_thinking" => vec![field(
+ "thinking.type",
+ GatewayNativeReasoningValueV1::String(
+ if enabled { "enabled" } else { "disabled" }.into(),
+ ),
+ protocol,
+ )],
+ UpstreamProtocol::ChatCompletions => vec![field(
+ parameter,
+ GatewayNativeReasoningValueV1::Bool(enabled),
+ protocol,
+ )],
+ }
+}
diff --git a/crates/daemon/tests/support/usage_query_gateway_product.py b/crates/daemon/tests/support/usage_query_gateway_product.py
index 499d88db..253a8170 100644
--- a/crates/daemon/tests/support/usage_query_gateway_product.py
+++ b/crates/daemon/tests/support/usage_query_gateway_product.py
@@ -85,7 +85,7 @@ def response_body(protocol, streaming):
'model': MODEL, 'usage': provider_usage(protocol, streaming)}
if protocol == 'responses':
return dict(common, status='completed', output=[{
- 'type': 'message', 'role': 'assistant', 'content': [{
+ 'type': 'message', 'id': 'message', 'role': 'assistant', 'content': [{
'type': 'output_text', 'text': 'usage-ok'}]}])
return dict(common, type='message', role='assistant', content=[{
'type': 'text', 'text': 'usage-ok'}], stop_reason='end_turn',
diff --git a/crates/diagnostics/src/event/mod.rs b/crates/diagnostics/src/event/mod.rs
index 7bad2924..1614d60a 100644
--- a/crates/diagnostics/src/event/mod.rs
+++ b/crates/diagnostics/src/event/mod.rs
@@ -144,6 +144,11 @@ impl DiagnosticEvent {
_ => Warn,
},
DiagnosticEvent::AttemptEnd(end) => match end.outcome {
+ AttemptOutcome::Completed
+ if end.provider_result == Some(WireModelResult::Unknown) =>
+ {
+ Warn
+ }
AttemptOutcome::Completed => Info,
_ => Warn,
},
@@ -151,7 +156,11 @@ impl DiagnosticEvent {
| DiagnosticEvent::RequestCancel(_)
| DiagnosticEvent::RequestTimeout(_) => Warn,
DiagnosticEvent::SemanticCommit(_) => Info,
- DiagnosticEvent::ModelStage(_) | DiagnosticEvent::UpstreamWire(_) => Debug,
+ DiagnosticEvent::ModelStage(_) => Debug,
+ DiagnosticEvent::UpstreamWire(wire) => match wire.phase {
+ UpstreamWirePhase::Failure => Warn,
+ _ => Debug,
+ },
DiagnosticEvent::ReasoningCleanup(_) => Warn,
DiagnosticEvent::ResponseFailure(_) => Warn,
DiagnosticEvent::ContentCaptureEnd(end) => match end.outcome {
diff --git a/crates/diagnostics/src/event/model.rs b/crates/diagnostics/src/event/model.rs
index 3b85e059..d7662284 100644
--- a/crates/diagnostics/src/event/model.rs
+++ b/crates/diagnostics/src/event/model.rs
@@ -46,6 +46,12 @@ pub struct AttemptBegin {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub attempt_token: Option<CorrelationToken>,
pub attempt_index: u64,
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub binding_token: Option<CorrelationToken>,
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub native_model: Option<NativeModelId>,
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub upstream_protocol: Option<IngressProtocol>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
@@ -59,6 +65,20 @@ pub struct AttemptEnd {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub http_status: Option<u16>,
pub reasoning_fields_removed: u64,
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub http_protocol: Option<WireHttpProtocol>,
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub provider_error: Option<WireProviderError>,
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub provider_result: Option<WireModelResult>,
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub commits: Option<AttemptWireCommits>,
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub native_model: Option<NativeModelId>,
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub request_reasoning: Option<WireRequestReasoning>,
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub upstream_request_token: Option<CorrelationToken>,
}
/// Counts only; never records history, signatures or provider error bodies.
@@ -149,6 +169,12 @@ pub struct Fallback {
pub request_token: Option<CorrelationToken>,
pub from_attempt_index: u64,
pub reason: FallbackReason,
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub next_attempt_index: Option<u64>,
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub next_attempt_token: Option<CorrelationToken>,
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub next_binding_token: Option<CorrelationToken>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
@@ -159,6 +185,10 @@ pub enum FallbackReason {
ConnectorUnavailable,
RateLimited,
UpstreamFailure,
+ AuthenticationRejected,
+ InputRejected,
+ Timeout,
+ InvalidOutput,
OtherStableReason,
}
@@ -232,8 +262,8 @@ pub enum ExclusionReason {
Other,
}
-/// Wire-shape facts and closed request controls only: never header values,
-/// URLs, model names or response bodies.
+/// Wire-shape facts and bounded model/control fields: never raw header values,
+/// URLs, prompts, provider messages or response bodies.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct UpstreamWire {
@@ -246,6 +276,20 @@ pub struct UpstreamWire {
pub http_status: Option<u16>,
pub content_type: WireContentType,
pub request_reasoning: Option<WireRequestReasoning>,
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub request_kind: Option<WireRequestKind>,
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub attempt_token: Option<CorrelationToken>,
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub attempt_index: Option<u64>,
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub native_model: Option<NativeModelId>,
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub upstream_request_token: Option<CorrelationToken>,
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub http_protocol: Option<WireHttpProtocol>,
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub provider_error: Option<WireProviderError>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
@@ -254,6 +298,14 @@ pub struct WireRequestReasoning {
pub responses_effort: Option<WireReasoningEffort>,
pub chat_effort: Option<WireReasoningEffort>,
pub messages_effort: Option<WireReasoningEffort>,
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub messages_thinking: Option<WireThinkingType>,
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub messages_budget_tokens: Option<u64>,
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub enable_thinking: Option<bool>,
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub thinking_enabled: Option<bool>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
@@ -275,6 +327,108 @@ pub enum WireReasoningEffort {
pub enum UpstreamWirePhase {
Request,
Response,
+ Failure,
+ Completed,
+}
+
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
+#[serde(rename_all = "snake_case")]
+pub enum WireRequestKind {
+ ModelInference,
+ DecisionService,
+}
+
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
+#[serde(rename_all = "snake_case")]
+pub enum WireHttpProtocol {
+ Http1,
+ Http2,
+}
+
+/// The observed model terminal is independent of successful HTTP transport.
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
+#[serde(rename_all = "snake_case")]
+pub enum WireModelResult {
+ Complete,
+ Failed,
+ Incomplete,
+ Unknown,
+}
+
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
+#[serde(rename_all = "snake_case")]
+pub enum WireThinkingType {
+ Enabled,
+ Disabled,
+ Adaptive,
+ Other,
+}
+
+/// Closed attribution only. Provider error text and arbitrary codes never enter logs.
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
+#[serde(rename_all = "snake_case")]
+pub enum WireProviderError {
+ ThinkingBudgetRejected,
+ AuthenticationRejected,
+ RateLimited,
+ EndpointRejected,
+ InputRejected,
+ UpstreamUnavailable,
+ InvalidOutput,
+ Timeout,
+ Cancelled,
+ Unknown,
+}
+
+#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
+#[serde(deny_unknown_fields)]
+pub struct AttemptWireCommits {
+ pub upstream_request: WireCommitState,
+ pub downstream_headers: WireCommitState,
+ /// Any response body byte, including a failure event, closes transparent replay.
+ pub downstream_body: WireCommitState,
+}
+
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
+#[serde(rename_all = "snake_case")]
+pub enum WireCommitState {
+ Clear,
+ Committed,
+ Poisoned,
+}
+
+/// Only a native identifier from the selected encoder may construct this field.
+#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
+#[serde(transparent)]
+pub struct NativeModelId(String);
+
+impl NativeModelId {
+ pub fn new(value: &str) -> Option<Self> {
+ (!value.is_empty()
+ && value.len() <= 256
+ && !value.contains("://")
+ && value.bytes().all(|byte| {
+ byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_' | b'.' | b':' | b'/')
+ }))
+ .then(|| Self(value.to_owned()))
+ }
+}
+
+impl<'de> Deserialize<'de> for NativeModelId {
+ fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
+ struct Identifier;
+ impl<'de> serde::de::Visitor<'de> for Identifier {
+ type Value = NativeModelId;
+ fn expecting(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ f.write_str("a bounded native model identifier")
+ }
+ fn visit_str<E: serde::de::Error>(self, value: &str) -> Result<Self::Value, E> {
+ NativeModelId::new(value)
+ .ok_or_else(|| E::custom("invalid native model identifier"))
+ }
+ }
+ deserializer.deserialize_str(Identifier)
+ }
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
@@ -286,3 +440,44 @@ pub enum WireContentType {
Other,
Missing,
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn historical_wire_and_attempt_records_keep_missing_facts_unknown() {
+ let old_wire = serde_json::json!({"request_token":null,"phase":"response",
+ "bearer_present":false,"api_key_present":false,"authorization_bytes":0,
+ "body_bytes":null,"http_status":400,"content_type":"json","request_reasoning":null});
+ let wire: UpstreamWire = serde_json::from_value(old_wire).unwrap();
+ assert!(wire.native_model.is_none() && wire.provider_error.is_none());
+ assert!(wire.attempt_index.is_none() && wire.http_protocol.is_none());
+ let old_attempt = serde_json::json!({"attempt_token":null,"outcome":"failed",
+ "commit":"unknown","elapsed_ms":1,"http_status":400,"reasoning_fields_removed":0});
+ let attempt: AttemptEnd = serde_json::from_value(old_attempt).unwrap();
+ assert!(attempt.commits.is_none() && attempt.http_protocol.is_none());
+ let mut encoded = serde_json::to_value(wire).unwrap();
+ encoded["provider_error"] = "provider-secret-marker".into();
+ assert!(serde_json::from_value::<UpstreamWire>(encoded).is_err());
+ }
+
+ #[test]
+ fn native_model_field_rejects_content_urls_and_oversized_identifiers() {
+ for value in [
+ "",
+ "prompt with spaces",
+ "https://credential.invalid",
+ "model\nsecret",
+ &"x".repeat(257),
+ ] {
+ assert!(NativeModelId::new(value).is_none());
+ assert!(serde_json::from_value::<NativeModelId>(serde_json::json!(value)).is_err());
+ }
+ assert_eq!(
+ serde_json::to_value(NativeModelId::new("provider/qwen3.6-flash:revision").unwrap())
+ .unwrap(),
+ "provider/qwen3.6-flash:revision"
+ );
+ }
+}
diff --git a/crates/domain/src/routing/gateway_execution.rs b/crates/domain/src/routing/gateway_execution.rs
index b5f0e65b..56a0c1ae 100644
--- a/crates/domain/src/routing/gateway_execution.rs
+++ b/crates/domain/src/routing/gateway_execution.rs
@@ -372,10 +372,12 @@ impl GatewayNativeReasoningFieldAssignmentV1 {
match &self.value {
GatewayNativeReasoningValueV1::Bool(value) => *value == enabled,
GatewayNativeReasoningValueV1::String(_) => match protocol {
- UpstreamProtocol::Responses => string_value(
- ["reasoning", "effort"],
- if enabled { "high" } else { "none" },
- ),
+ UpstreamProtocol::Responses => {
+ string_value(["reasoning", "effort"], if enabled { "low" } else { "none" })
+ // Released 0.2.0 stored toggle-on as high. Its immutable
+ // profile remains readable; current rendering uses low.
+ || (enabled && string_value(["reasoning", "effort"], "high"))
+ }
UpstreamProtocol::Messages | UpstreamProtocol::ChatCompletions => string_value(
["thinking", "type"],
if enabled { "enabled" } else { "disabled" },
@@ -727,19 +729,21 @@ fn toggle_field_matches(
enabled: bool,
protocol: UpstreamProtocol,
) -> bool {
- match (protocol, parameter) {
- (UpstreamProtocol::Responses, "enable_thinking" | "deepseek_thinking")
- | (UpstreamProtocol::Messages, "enable_thinking" | "deepseek_thinking")
- | (UpstreamProtocol::ChatCompletions, "deepseek_thinking") => {
- matches!(field.value, GatewayNativeReasoningValueV1::String(_))
- && field.matches_toggle_wire(protocol, enabled)
+ let native_bool = field.path
+ == GatewayNativeReasoningFieldAssignmentV1::parameter_path(parameter, protocol)
+ && matches!(field.value, GatewayNativeReasoningValueV1::Bool(_))
+ && field.matches_toggle_wire(protocol, enabled);
+ match protocol {
+ UpstreamProtocol::Responses | UpstreamProtocol::Messages => {
+ (matches!(field.value, GatewayNativeReasoningValueV1::String(_))
+ && field.matches_toggle_wire(protocol, enabled))
+ || native_bool
}
- _ => {
- field.path
- == GatewayNativeReasoningFieldAssignmentV1::parameter_path(parameter, protocol)
- && matches!(field.value, GatewayNativeReasoningValueV1::Bool(_))
+ UpstreamProtocol::ChatCompletions if parameter == "deepseek_thinking" => {
+ matches!(field.value, GatewayNativeReasoningValueV1::String(_))
&& field.matches_toggle_wire(protocol, enabled)
}
+ _ => native_bool,
}
}
diff --git a/crates/gateway-core/src/runtime/attempt/exchange.rs b/crates/gateway-core/src/runtime/attempt/exchange.rs
index 4c4264e8..834efdfa 100644
--- a/crates/gateway-core/src/runtime/attempt/exchange.rs
+++ b/crates/gateway-core/src/runtime/attempt/exchange.rs
@@ -74,6 +74,7 @@ pub struct AttemptExchange<T: AttemptTransport> {
semantic_upstream_calls: usize,
connection_sub_attempts: usize,
connected: bool,
+ negotiated_protocol: Option<HttpProtocol>,
request_framing_reconciled: bool,
pending_request_write: Option<Bytes>,
writer_state: WriterState,
diff --git a/crates/gateway-core/src/runtime/attempt/exchange/constructor.rs b/crates/gateway-core/src/runtime/attempt/exchange/constructor.rs
index bbb6d614..6f335262 100644
--- a/crates/gateway-core/src/runtime/attempt/exchange/constructor.rs
+++ b/crates/gateway-core/src/runtime/attempt/exchange/constructor.rs
@@ -277,6 +277,7 @@ impl<T: AttemptTransport> AttemptExchange<T> {
semantic_upstream_calls: 0,
connection_sub_attempts: 0,
connected: false,
+ negotiated_protocol: None,
request_framing_reconciled: false,
pending_request_write: None,
writer_state: WriterState::NotStarted,
@@ -350,6 +351,7 @@ impl<T: AttemptTransport> AttemptExchange<T> {
.saturating_sub(self.transport_suppression_observed);
AttemptTransportFacts {
started_at: self.attempt_started_at,
+ upstream_protocol: self.negotiated_protocol,
connect_elapsed: self.connect_elapsed,
request_write_elapsed: self.request_write_elapsed,
upstream_ttfb: self
diff --git a/crates/gateway-core/src/runtime/attempt/exchange/io.rs b/crates/gateway-core/src/runtime/attempt/exchange/io.rs
index 49547bbe..4a0d5db0 100644
--- a/crates/gateway-core/src/runtime/attempt/exchange/io.rs
+++ b/crates/gateway-core/src/runtime/attempt/exchange/io.rs
@@ -59,6 +59,8 @@ impl<T: AttemptTransport> AttemptExchange<T> {
match connect {
Ok(()) => {
self.connect_elapsed = Some(self.attempt_started_at.elapsed());
+ self.negotiated_protocol =
+ self.transport.as_ref().map(AttemptTransport::protocol);
self.connected = true;
return Ok(());
}
diff --git a/crates/gateway-core/src/runtime/attempt/transport.rs b/crates/gateway-core/src/runtime/attempt/transport.rs
index b9ab299a..74cbeb18 100644
--- a/crates/gateway-core/src/runtime/attempt/transport.rs
+++ b/crates/gateway-core/src/runtime/attempt/transport.rs
@@ -21,6 +21,8 @@ pub enum AttemptTimeoutKind {
#[derive(Clone, Debug)]
pub struct AttemptTransportFacts {
pub started_at: Instant,
+ /// Captured only after a successful connection; retained after cleanup.
+ pub upstream_protocol: Option<HttpProtocol>,
pub connect_elapsed: Option<Duration>,
pub request_write_elapsed: Option<Duration>,
/// Measured at the transport receipt boundary, never at downstream write.
diff --git a/crates/gateway-core/src/runtime/driver/decision.rs b/crates/gateway-core/src/runtime/driver/decision.rs
index 49ef4e41..03c84a70 100644
--- a/crates/gateway-core/src/runtime/driver/decision.rs
+++ b/crates/gateway-core/src/runtime/driver/decision.rs
@@ -763,6 +763,7 @@ pub(super) fn preexchange_transport_facts(
) -> AttemptTransportFacts {
AttemptTransportFacts {
started_at: selected.budget.issued_at,
+ upstream_protocol: None,
connect_elapsed: None,
request_write_elapsed: None,
upstream_ttfb: None,
diff --git a/crates/gateway-core/src/transport/pingora/client/basic.rs b/crates/gateway-core/src/transport/pingora/client/basic.rs
index ca82629d..f51bfff3 100644
--- a/crates/gateway-core/src/transport/pingora/client/basic.rs
+++ b/crates/gateway-core/src/transport/pingora/client/basic.rs
@@ -90,7 +90,13 @@ impl PingoraClientSession {
}
ClientSession::H2(mut session) => {
session
- .write_request_header(Box::new(build_request_header(head)?), false)
+ .write_request_header(
+ Box::new(request::build_h2_request_header(
+ head,
+ connected.peer.is_tls(),
+ )?),
+ false,
+ )
.map_err(|error| AttemptError::Transport(error.to_string().into()))?;
let writer = session.take_request_body_writer().ok_or_else(|| {
AttemptError::Transport("Pingora H2 request writer is unavailable".into())
diff --git a/crates/gateway-core/src/transport/pingora/client/request.rs b/crates/gateway-core/src/transport/pingora/client/request.rs
index 84b182d6..f1a84fea 100644
--- a/crates/gateway-core/src/transport/pingora/client/request.rs
+++ b/crates/gateway-core/src/transport/pingora/client/request.rs
@@ -17,6 +17,26 @@ pub(super) fn build_request_header(
Ok(request)
}
+pub(super) fn build_h2_request_header(
+ head: &PreparedRequestHead,
+ tls: bool,
+) -> Result<RequestHeader, AttemptError> {
+ let mut request = build_request_header(head)?;
+ let authority = request.remove_header(&http::header::HOST).ok_or_else(|| {
+ AttemptError::Transport("HTTP/2 request requires an authorized authority".into())
+ })?;
+ let uri = http::Uri::builder()
+ .scheme(if tls { "https" } else { "http" })
+ .authority(authority.as_bytes())
+ .path_and_query(head.path_and_query.as_ref())
+ .build()
+ .map_err(|_| AttemptError::Transport("invalid HTTP/2 request authority".into()))?;
+ // Pingora otherwise derives :authority from Host while retaining Host on
+ // the wire. Some upstream gateways reject that duplicate authority.
+ request.set_uri(uri);
+ Ok(request)
+}
+
pub(super) async fn run_upstream_reader(
mut session: ClientSession,
sender: mpsc::Sender<Result<TransportPrecommitReceipt, AttemptError>>,
diff --git a/crates/gateway-core/src/transport/pingora/client/tests.rs b/crates/gateway-core/src/transport/pingora/client/tests.rs
index 6b3873dd..19d82467 100644
--- a/crates/gateway-core/src/transport/pingora/client/tests.rs
+++ b/crates/gateway-core/src/transport/pingora/client/tests.rs
@@ -10,6 +10,102 @@ use tokio::io::{AsyncReadExt, AsyncWriteExt};
struct ActiveTask(Arc<AtomicUsize>);
+#[test]
+fn h2_authority_preserves_port_path_and_auth_without_duplicate_host() {
+ for authority in ["provider.test:8443", "[::1]:8080"] {
+ let head = PreparedRequestHead {
+ method: http::Method::POST,
+ path_and_query: "/compatible-mode/v1/systemone?test=1".into(),
+ headers: http::HeaderMap::from_iter([
+ (http::header::HOST, authority.parse().unwrap()),
+ (
+ http::header::AUTHORIZATION,
+ "Bearer test-only".parse().unwrap(),
+ ),
+ ]),
+ };
+ let h1 = build_request_header(&head).unwrap();
+ assert_eq!(h1.headers[http::header::HOST], authority);
+ for tls in [false, true] {
+ let h2 = request::build_h2_request_header(&head, tls).unwrap();
+ assert_eq!(h2.uri.authority().unwrap().as_str(), authority);
+ assert_eq!(
+ h2.uri.scheme_str(),
+ Some(if tls { "https" } else { "http" })
+ );
+ assert_eq!(
+ h2.uri.path_and_query().unwrap().as_str(),
+ head.path_and_query.as_ref()
+ );
+ assert!(!h2.headers.contains_key(http::header::HOST));
+ assert_eq!(h2.headers[http::header::AUTHORIZATION], "Bearer test-only");
+ }
+ }
+ let head = PreparedRequestHead {
+ method: http::Method::POST,
+ path_and_query: "/".into(),
+ headers: http::HeaderMap::new(),
+ };
+ assert!(request::build_h2_request_header(&head, true).is_err());
+}
+
+#[tokio::test]
+async fn real_h2_upstream_receives_one_authority_from_production_client() {
+ let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
+ let address = listener.local_addr().unwrap();
+ let server = tokio::spawn(async move {
+ let (socket, _) = listener.accept().await.unwrap();
+ let mut connection = h2::server::handshake(socket).await.unwrap();
+ let (request, mut respond) = connection.accept().await.unwrap().unwrap();
+ assert_eq!(
+ request.uri().authority().unwrap().as_str(),
+ "provider.test:8443"
+ );
+ assert_eq!(
+ request.uri().path_and_query().unwrap().as_str(),
+ "/systemone?test=1"
+ );
+ assert!(!request.headers().contains_key(http::header::HOST));
+ respond
+ .send_response(http::Response::new(()), true)
+ .unwrap();
+ while connection.accept().await.is_some() {}
+ });
+ let mut target = plain_target(address, 87);
+ target.alpn = Arc::from([Arc::from("h2")]);
+ target = target.with_derived_connection_fingerprint();
+ let mut client =
+ PingoraClientSession::new(Arc::new(PingoraConnectorRegistry::default()), [0; 32]);
+ tokio::time::timeout(Duration::from_secs(3), async {
+ client.connect(&target, address).await.unwrap();
+ assert_eq!(client.protocol(), HttpProtocol::Http2);
+ client
+ .write_request_head(&PreparedRequestHead {
+ method: http::Method::POST,
+ path_and_query: "/systemone?test=1".into(),
+ headers: http::HeaderMap::from_iter([(
+ http::header::HOST,
+ "provider.test:8443".parse().unwrap(),
+ )]),
+ })
+ .await
+ .unwrap();
+ client.finish_request_body().await.unwrap();
+ assert!(matches!(
+ client.read_response_head().await.unwrap(),
+ TransportPrecommitEvent::ResponseHead {
+ status: http::StatusCode::OK,
+ ..
+ }
+ ));
+ client.cancel_reset().await;
+ })
+ .await
+ .unwrap();
+ server.abort();
+ let _ = server.await;
+}
+
impl Drop for ActiveTask {
fn drop(&mut self) {
self.0.fetch_sub(1, Ordering::AcqRel);
diff --git a/crates/gateway-core/tests/routing_contracts.rs b/crates/gateway-core/tests/routing_contracts.rs
index c3ea2068..fa16e119 100644
--- a/crates/gateway-core/tests/routing_contracts.rs
+++ b/crates/gateway-core/tests/routing_contracts.rs
@@ -305,6 +305,7 @@ impl SelectionPublicationPort<Arc<str>> for ContractSelection {
fn transport_facts() -> AttemptTransportFacts {
AttemptTransportFacts {
started_at: Instant::now(),
+ upstream_protocol: None,
connect_elapsed: Some(Duration::from_millis(1)),
request_write_elapsed: Some(Duration::from_millis(2)),
upstream_ttfb: Some(Duration::from_millis(3)),
diff --git a/crates/gateway/README.md b/crates/gateway/README.md
index 9fe00a41..3c282ccc 100644
--- a/crates/gateway/README.md
+++ b/crates/gateway/README.md
@@ -9,6 +9,12 @@ branch decision, plan and freeze candidates, then admit execution into
`hiroute-gateway-core`. The core owns attempts, response commit, cancellation and
resource cleanup; the Gateway does not run a second execution loop.
+The standalone `hirouted` entry can attach the same typed diagnostics using
+`--diagnostics-root <private-root>` and an optional temporary
+`--diagnostic-level-override`. These diagnostics do not enable session content
+capture or execution-fact storage. At Info, failed attempts retain the safe
+actual model and reasoning controls, HTTP protocol/status and body commit state.
+
Messages thinking computation is projected in
[request/native.rs](src/adapters/request/native.rs); request-local `thinking.display`
is resolved by [ingress.rs](src/adapters/ingress.rs) before Replay externalization.
@@ -85,3 +91,24 @@ Use the repository [test planner](../../scripts/test-plan.py) and
[configured validation runner](../../scripts/validation.py). An isolated Jev or
diagnostic success does not establish product routing success. Source-text
assertions and private helper names are not substitutes for these behaviors.
+
+## Native controls and completed responses
+
+[Reasoning serialization](src/adapters/request/reasoning.rs) owns protocol-level
+switch semantics and normalizes supported released projections without rewriting
+frozen publication identities. The daemon produces current profiles; explicit
+effort, manual budget and adaptive controls remain distinct. Shared Pingora
+transport assigns HTTP/2 authority once for model and classifier calls.
+
+[Response classification](src/runtime/driver/response.rs) classifies upstream
+failures for bounded relay within the frozen plan before the first downstream body.
+Streaming headers alone leave relay open; a later failed model terminal stays a failure in attempt,
+request and Agent turn observations even when its native bytes were delivered.
+The decision diagnostic uses closed error codes; provider text is not a public
+error message or a substitute for evidence of credential failure.
+
+Known incomplete nonstream JSON responses follow the same prebody relay boundary
+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.
diff --git a/crates/gateway/src/adapters/native_reasoning_tests.rs b/crates/gateway/src/adapters/native_reasoning_tests.rs
index 983d32de..8bc3fd33 100644
--- a/crates/gateway/src/adapters/native_reasoning_tests.rs
+++ b/crates/gateway/src/adapters/native_reasoning_tests.rs
@@ -1,7 +1,8 @@
//! Exact catalog controls replace incoming controls before native request serialization.
use super::*;
+use crate::server::core_runtime::model_ir::RequestedReasoningDisposition;
use crate::server::core_runtime::profiles::{
- CandidateProtocolProfile, NativeReasoningFieldAssignment, NativeReasoningRender,
+ CandidateProtocolProfile, CriticalFact, NativeReasoningFieldAssignment, NativeReasoningRender,
NativeReasoningValue, ReasoningControlKind, fixed_reasoning,
};
use crate::server::request_plan::IngressProtocol;
@@ -83,6 +84,244 @@ fn messages_adaptive_effort_overwrites_caller_disabled_thinking_and_effort() {
assert_eq!(native.body["output_config"], json!({"effort":"high"}));
}
+fn project_reasoning(
+ protocol: IngressProtocol,
+ reasoning: crate::server::core_runtime::profiles::ReasoningProfileCapability,
+ max_tokens: u64,
+) -> Result<PreparedNativeRequest, ProtocolAdapterError> {
+ project_reasoning_with_cap(protocol, reasoning, max_tokens, max_tokens)
+}
+
+fn project_reasoning_with_cap(
+ protocol: IngressProtocol,
+ reasoning: crate::server::core_runtime::profiles::ReasoningProfileCapability,
+ max_tokens: u64,
+ configured_cap: u64,
+) -> Result<PreparedNativeRequest, ProtocolAdapterError> {
+ let body = match protocol {
+ IngressProtocol::Responses => {
+ json!({"model":"alias", "input":"hello", "reasoning":{"effort":"high", "summary":"auto"}})
+ }
+ IngressProtocol::Messages => {
+ json!({"model":"alias", "max_tokens":max_tokens, "messages":[{"role":"user","content":"hello"}], "thinking":{"type":"enabled","budget_tokens":4096}})
+ }
+ IngressProtocol::ChatCompletions => {
+ json!({"model":"alias", "messages":[{"role":"user","content":"hello"}]})
+ }
+ };
+ let request = decode_ingress_request(protocol, &body).unwrap();
+ let mut profile =
+ CandidateProtocolProfile::exact_portable_path(protocol, protocol, "physical", reasoning);
+ profile.capability.context.max_output_tokens = CriticalFact::Exact(configured_cap);
+ profile.capability.context.max_total_tokens =
+ CriticalFact::Exact(Some(1024 * 1024 + configured_cap));
+ project_candidate_request(&request, &profile)
+}
+
+#[test]
+fn released_toggle_profiles_render_standard_controls_without_changing_frozen_bytes() {
+ // Frozen output of the 0.2.0 producer d625e653 (enable_thinking toggle).
+ // Do not build this input through the current daemon projection.
+ let released = r#"{"profile_id":"enabled","control_kind":"toggle","render":{"kind":"exact_fields","protocol":"responses","fields":[{"path":["reasoning","effort"],"value":{"kind":"string","value":"high"}}]},"accounting":"within_output_cap","additional_reservation_tokens":0}"#;
+ let enabled: crate::server::core_runtime::profiles::ReasoningProfileCapability =
+ serde_json::from_str(released).unwrap();
+ let before = serde_json::to_value(&enabled).unwrap();
+ let rendered = project_reasoning(IngressProtocol::Responses, enabled.clone(), 2048).unwrap();
+ assert_eq!(rendered.body["reasoning"]["effort"], "low");
+ assert_eq!(rendered.body["reasoning"]["summary"], "auto");
+ assert_eq!(serde_json::to_value(&enabled).unwrap(), before);
+ let mut explicit = enabled.clone();
+ explicit.profile_id = "high".into();
+ explicit.control_kind = ReasoningControlKind::Discrete;
+ assert_eq!(
+ project_reasoning(IngressProtocol::Responses, explicit, 2048)
+ .unwrap()
+ .body["reasoning"]["effort"],
+ "high"
+ );
+ for on in [false, true] {
+ for protocol in [IngressProtocol::Responses, IngressProtocol::Messages] {
+ let mut profile = enabled.clone();
+ profile.profile_id = if on { "enabled" } else { "disabled" }.into();
+ profile.render = NativeReasoningRender::ExactFields {
+ protocol,
+ fields: vec![NativeReasoningFieldAssignment {
+ path: if protocol == IngressProtocol::Messages {
+ vec!["thinking".into(), "type".into()]
+ } else {
+ vec!["reasoning".into(), "effort".into()]
+ },
+ value: NativeReasoningValue::String(
+ match (protocol, on) {
+ (IngressProtocol::Messages, true) => "enabled",
+ (IngressProtocol::Messages, false) => "disabled",
+ (_, true) => "low",
+ (_, false) => "none",
+ }
+ .into(),
+ ),
+ }],
+ };
+ let rendered = project_reasoning(protocol, profile.clone(), 2048).unwrap();
+ if protocol == IngressProtocol::Messages {
+ assert_eq!(
+ rendered.body["thinking"],
+ if on {
+ json!({"type":"enabled", "budget_tokens":1024})
+ } else {
+ json!({"type":"disabled"})
+ }
+ );
+ if on {
+ assert!(project_reasoning(protocol, profile, 1024).is_err());
+ }
+ } else {
+ assert_eq!(
+ rendered.body["reasoning"]["effort"],
+ if on { "low" } else { "none" }
+ );
+ }
+ }
+ }
+}
+
+#[test]
+fn messages_explicit_budget_is_preserved_and_checked_against_output_cap() {
+ for budget in [512, 1024, 2048, 4096] {
+ let mut profile = fixed_reasoning(format!("budget-{budget}"));
+ profile.control_kind = ReasoningControlKind::Budget;
+ profile.render = NativeReasoningRender::ExactBudget {
+ protocol: IngressProtocol::Messages,
+ fields: vec![NativeReasoningFieldAssignment {
+ path: vec!["thinking".into(), "budget_tokens".into()],
+ value: NativeReasoningValue::U64(budget),
+ }],
+ budget_path: vec!["thinking".into(), "budget_tokens".into()],
+ selected_tokens: budget,
+ min_tokens: 512,
+ max_tokens: 4096,
+ step_tokens: 512,
+ };
+ let rendered = project_reasoning(IngressProtocol::Messages, profile, 4096);
+ if (1024..4096).contains(&budget) {
+ assert_eq!(
+ rendered.unwrap().body["thinking"],
+ json!({"type":"enabled", "budget_tokens":budget})
+ );
+ } else {
+ assert!(rendered.is_err());
+ }
+ }
+}
+
+#[test]
+fn messages_manual_toggle_checks_the_effective_candidate_output_cap() {
+ let mut reasoning = fixed_reasoning("enabled");
+ reasoning.control_kind = ReasoningControlKind::Toggle;
+ reasoning.render = NativeReasoningRender::ExactFields {
+ protocol: IngressProtocol::Messages,
+ fields: vec![NativeReasoningFieldAssignment {
+ path: vec!["thinking".into(), "type".into()],
+ value: NativeReasoningValue::String("enabled".into()),
+ }],
+ };
+ assert!(
+ project_reasoning_with_cap(IngressProtocol::Messages, reasoning.clone(), 4096, 1024,)
+ .is_err()
+ );
+ let rendered =
+ project_reasoning_with_cap(IngressProtocol::Messages, reasoning, 4096, 2048).unwrap();
+ assert_eq!(rendered.body["max_tokens"], 2048);
+ assert_eq!(rendered.body["thinking"]["budget_tokens"], 1024);
+}
+
+#[test]
+fn legacy_custom_boolean_profile_keeps_its_exact_assignment() {
+ for protocol in [IngressProtocol::Responses, IngressProtocol::Messages] {
+ let mut reasoning = fixed_reasoning("custom-boolean-name");
+ reasoning.control_kind = ReasoningControlKind::Toggle;
+ reasoning.render = NativeReasoningRender::ExactFields {
+ protocol,
+ fields: vec![NativeReasoningFieldAssignment {
+ path: vec!["custom_toggle".into()],
+ value: NativeReasoningValue::Bool(true),
+ }],
+ };
+ let rendered = project_reasoning(protocol, reasoning, 4096).unwrap();
+ assert_eq!(rendered.body["custom_toggle"], true);
+ assert!(rendered.body.get("thinking").is_none());
+ if protocol == IngressProtocol::Responses {
+ assert_eq!(rendered.body["reasoning"], json!({"summary":"auto"}));
+ }
+ }
+}
+
+#[test]
+fn plan_reasoning_removes_legacy_switch_aliases_without_changing_fixed_native_controls() {
+ for protocol in [IngressProtocol::Responses, IngressProtocol::Messages] {
+ let body = match protocol {
+ IngressProtocol::Responses => json!({
+ "model":"alias", "input":"hello", "enable_thinking":false,
+ "thinking":{"enabled":false}, "reasoning":{"effort":"high","summary":"auto"}
+ }),
+ IngressProtocol::Messages => json!({
+ "model":"alias", "max_tokens":4096,
+ "messages":[{"role":"user","content":"hello"}],
+ "enable_thinking":false, "thinking":{"enabled":false}
+ }),
+ IngressProtocol::ChatCompletions => unreachable!(),
+ };
+ let mut request = decode_ingress_request(protocol, &body).unwrap();
+ let mut reasoning = fixed_reasoning("enabled");
+ reasoning.control_kind = ReasoningControlKind::Toggle;
+ reasoning.render = NativeReasoningRender::ExactFields {
+ protocol,
+ fields: vec![NativeReasoningFieldAssignment {
+ path: if protocol == IngressProtocol::Responses {
+ vec!["reasoning".into(), "effort".into()]
+ } else {
+ vec!["thinking".into(), "type".into()]
+ },
+ value: NativeReasoningValue::String(
+ if protocol == IngressProtocol::Responses {
+ "low"
+ } else {
+ "enabled"
+ }
+ .into(),
+ ),
+ }],
+ };
+ let mut profile = CandidateProtocolProfile::exact_portable_path(
+ protocol, protocol, "physical", reasoning,
+ );
+ profile.capability.context.max_output_tokens = CriticalFact::Exact(4096);
+ let rendered = project_candidate_request(&request, &profile).unwrap();
+ assert!(rendered.body.get("enable_thinking").is_none());
+ assert!(rendered.body["thinking"].get("enabled").is_none());
+ if protocol == IngressProtocol::Responses {
+ assert!(rendered.body.get("thinking").is_none());
+ assert_eq!(
+ rendered.body["reasoning"],
+ json!({"effort":"low","summary":"auto"})
+ );
+ } else {
+ assert_eq!(
+ rendered.body["thinking"],
+ json!({"type":"enabled","budget_tokens":1024})
+ );
+ }
+ request.requested_reasoning.disposition =
+ RequestedReasoningDisposition::AppliedToFixedBinding;
+ let fixed = project_candidate_request(&request, &profile).unwrap();
+ assert_eq!(fixed.body["enable_thinking"], body["enable_thinking"]);
+ assert_eq!(fixed.body["thinking"], body["thinking"]);
+ if protocol == IngressProtocol::Responses {
+ assert_eq!(fixed.body["reasoning"], body["reasoning"]);
+ }
+ }
+}
+
fn display_profile(mode: &str) -> CandidateProtocolProfile {
let mut reasoning = fixed_reasoning(mode);
reasoning.control_kind = ReasoningControlKind::Discrete;
@@ -93,19 +332,22 @@ fn display_profile(mode: &str) -> CandidateProtocolProfile {
if mode == "enabled" {
fields.push(NativeReasoningFieldAssignment {
path: vec!["thinking".into(), "budget_tokens".into()],
- value: NativeReasoningValue::U64(128),
+ value: NativeReasoningValue::U64(1024),
});
}
reasoning.render = NativeReasoningRender::ExactFields {
protocol: IngressProtocol::Messages,
fields,
};
- CandidateProtocolProfile::exact_portable_path(
+ let mut profile = CandidateProtocolProfile::exact_portable_path(
IngressProtocol::Messages,
IngressProtocol::Messages,
"physical",
reasoning,
- )
+ );
+ // Manual thinking must fit strictly below the effective output limit.
+ profile.capability.context.max_output_tokens = CriticalFact::Exact(2048);
+ profile
}
#[test]
@@ -117,7 +359,7 @@ fn thinking_display_plan_modes_use_only_plan_budget_and_preserve_format() {
("adaptive", json!({"type":"adaptive","display":"omitted"})),
(
"enabled",
- json!({"type":"enabled","budget_tokens":128,"display":"omitted"}),
+ json!({"type":"enabled","budget_tokens":1024,"display":"omitted"}),
),
] {
let native = project_candidate_request(&request, &display_profile(mode)).unwrap();
diff --git a/crates/gateway/src/adapters/request.rs b/crates/gateway/src/adapters/request.rs
index 158568a9..d53e7705 100644
--- a/crates/gateway/src/adapters/request.rs
+++ b/crates/gateway/src/adapters/request.rs
@@ -12,6 +12,8 @@ use super::{ChatToolProjection, ProtocolAdapterError};
mod native;
mod reader;
+mod reasoning;
+use reasoning::render_reasoning;
mod template;
mod tools;
pub use reader::sequential_attempt_body;
@@ -934,87 +936,6 @@ fn validate_message_shapes(
Ok(())
}
-fn render_reasoning(
- body: &mut Map<String, Value>,
- reasoning: &ReasoningProfileCapability,
- protocol: IngressProtocol,
-) -> Result<(), ProtocolAdapterError> {
- match &reasoning.render {
- NativeReasoningRender::NoControlParameter => {}
- NativeReasoningRender::ExactFields {
- protocol: owner,
- fields,
- }
- | NativeReasoningRender::ExactBudget {
- protocol: owner,
- fields,
- ..
- } if *owner == protocol => {
- for field in fields {
- insert_exact_field(body, &field.path, native_reasoning_value(&field.value))?;
- }
- }
- NativeReasoningRender::ExactFields { .. } | NativeReasoningRender::ExactBudget { .. } => {
- return Err(ProtocolAdapterError::ClientUnrepresentable(
- "reasoning render belongs to another upstream protocol".into(),
- ));
- }
- }
- Ok(())
-}
-
-fn native_reasoning_value(value: &NativeReasoningValue) -> Value {
- match value {
- NativeReasoningValue::Bool(value) => Value::Bool(*value),
- NativeReasoningValue::String(value) => Value::String(value.clone()),
- NativeReasoningValue::U64(value) => Value::from(*value),
- }
-}
-
-fn insert_exact_field(
- body: &mut Map<String, Value>,
- path: &[String],
- value: Value,
-) -> Result<(), ProtocolAdapterError> {
- let Some((root, tail)) = path.split_first() else {
- return Err(ProtocolAdapterError::ClientUnrepresentable(
- "reasoning field path is empty".into(),
- ));
- };
- if tail.is_empty() {
- if body.insert(root.clone(), value).is_some() {
- return Err(ProtocolAdapterError::ClientUnrepresentable(
- "reasoning field collides with native request".into(),
- ));
- }
- return Ok(());
- }
- let root_value = body
- .entry(root.clone())
- .or_insert_with(|| Value::Object(Map::new()));
- let mut object = root_value.as_object_mut().ok_or_else(|| {
- ProtocolAdapterError::ClientUnrepresentable(
- "reasoning path collides with a non-object native field".into(),
- )
- })?;
- for part in &tail[..tail.len() - 1] {
- let child = object
- .entry(part.clone())
- .or_insert_with(|| Value::Object(Map::new()));
- object = child.as_object_mut().ok_or_else(|| {
- ProtocolAdapterError::ClientUnrepresentable(
- "reasoning path collides with a non-object native field".into(),
- )
- })?;
- }
- if object.insert(tail[tail.len() - 1].clone(), value).is_some() {
- return Err(ProtocolAdapterError::ClientUnrepresentable(
- "reasoning field is assigned more than once".into(),
- ));
- }
- Ok(())
-}
-
fn candidate_max_output(
request: &ModelRequestIRV1,
profile: &CandidateProtocolProfile,
diff --git a/crates/gateway/src/adapters/request/native.rs b/crates/gateway/src/adapters/request/native.rs
index f2a5edd9..e01f7e67 100644
--- a/crates/gateway/src/adapters/request/native.rs
+++ b/crates/gateway/src/adapters/request/native.rs
@@ -71,6 +71,12 @@ pub(super) fn project(
{
// Plan controls own effort/budget, not unrelated native siblings such as
// output formatting. Start each attempt from the immutable original tree.
+ if request.ingress_protocol != IngressProtocol::ChatCompletions {
+ object.remove("enable_thinking");
+ if let Some(thinking) = object.get_mut("thinking").and_then(Value::as_object_mut) {
+ thinking.remove("enabled");
+ }
+ }
for (root, fields) in [
("reasoning", &["effort"][..]),
("thinking", &["type", "budget_tokens"][..]),
@@ -81,17 +87,6 @@ pub(super) fn project(
native.remove(*field);
}
}
- if let Some(configured) = canonical.get(root).and_then(Value::as_object) {
- let native = object.entry(root).or_insert_with(|| json!({}));
- let native = native
- .as_object_mut()
- .ok_or(ModelIrError::InvalidField("reasoning control"))?;
- for field in fields {
- if let Some(value) = configured.get(*field) {
- native.insert((*field).into(), value.clone());
- }
- }
- }
if object
.get(root)
.and_then(Value::as_object)
diff --git a/crates/gateway/src/adapters/request/reasoning.rs b/crates/gateway/src/adapters/request/reasoning.rs
new file mode 100644
index 00000000..8248c0d8
--- /dev/null
+++ b/crates/gateway/src/adapters/request/reasoning.rs
@@ -0,0 +1,172 @@
+//! Protocol-owned reasoning controls and supported released-profile normalization.
+use super::*;
+use crate::server::core_runtime::profiles::ReasoningControlKind;
+
+pub(super) fn render_reasoning(
+ body: &mut Map<String, Value>,
+ reasoning: &ReasoningProfileCapability,
+ protocol: IngressProtocol,
+) -> Result<(), ProtocolAdapterError> {
+ if reasoning
+ .render
+ .protocol()
+ .is_some_and(|owner| owner != protocol)
+ {
+ return Err(invalid(
+ "reasoning render belongs to another upstream protocol",
+ ));
+ }
+ if protocol == IngressProtocol::Responses
+ && matches!(reasoning.render, NativeReasoningRender::ExactBudget { .. })
+ {
+ return Err(invalid("Responses has no standard reasoning token budget"));
+ }
+ // Normalize the released toggle projection at the wire boundary. The stored
+ // profile and its digest remain unchanged; explicitly selected effort does too.
+ if reasoning.control_kind == ReasoningControlKind::Toggle
+ && matches!(reasoning.profile_id.as_str(), "enabled" | "disabled")
+ && protocol != IngressProtocol::ChatCompletions
+ {
+ let enabled = reasoning.profile_id == "enabled";
+ match protocol {
+ IngressProtocol::Responses => {
+ insert_exact_field(
+ body,
+ &["reasoning".into(), "effort".into()],
+ json!(if enabled { "low" } else { "none" }),
+ )?;
+ }
+ IngressProtocol::Messages => {
+ insert_exact_field(
+ body,
+ &["thinking".into(), "type".into()],
+ json!(if enabled { "enabled" } else { "disabled" }),
+ )?;
+ if enabled {
+ insert_exact_field(
+ body,
+ &["thinking".into(), "budget_tokens".into()],
+ json!(1024),
+ )?;
+ }
+ }
+ IngressProtocol::ChatCompletions => unreachable!(),
+ }
+ return complete_messages_thinking(body, protocol);
+ }
+ match &reasoning.render {
+ NativeReasoningRender::NoControlParameter => {}
+ NativeReasoningRender::ExactFields {
+ protocol: owner,
+ fields,
+ }
+ | NativeReasoningRender::ExactBudget {
+ protocol: owner,
+ fields,
+ ..
+ } if *owner == protocol => {
+ for field in fields {
+ insert_exact_field(body, &field.path, native_reasoning_value(&field.value))?;
+ }
+ }
+ NativeReasoningRender::ExactFields { .. } | NativeReasoningRender::ExactBudget { .. } => {
+ return Err(ProtocolAdapterError::ClientUnrepresentable(
+ "reasoning render belongs to another upstream protocol".into(),
+ ));
+ }
+ }
+ complete_messages_thinking(body, protocol)
+}
+
+fn native_reasoning_value(value: &NativeReasoningValue) -> Value {
+ match value {
+ NativeReasoningValue::Bool(value) => Value::Bool(*value),
+ NativeReasoningValue::String(value) => Value::String(value.clone()),
+ NativeReasoningValue::U64(value) => Value::from(*value),
+ }
+}
+
+fn insert_exact_field(
+ body: &mut Map<String, Value>,
+ path: &[String],
+ value: Value,
+) -> Result<(), ProtocolAdapterError> {
+ let Some((root, tail)) = path.split_first() else {
+ return Err(ProtocolAdapterError::ClientUnrepresentable(
+ "reasoning field path is empty".into(),
+ ));
+ };
+ if tail.is_empty() {
+ if body.insert(root.clone(), value).is_some() {
+ return Err(ProtocolAdapterError::ClientUnrepresentable(
+ "reasoning field collides with native request".into(),
+ ));
+ }
+ return Ok(());
+ }
+ let root_value = body
+ .entry(root.clone())
+ .or_insert_with(|| Value::Object(Map::new()));
+ let mut object = root_value.as_object_mut().ok_or_else(|| {
+ ProtocolAdapterError::ClientUnrepresentable(
+ "reasoning path collides with a non-object native field".into(),
+ )
+ })?;
+ for part in &tail[..tail.len() - 1] {
+ let child = object
+ .entry(part.clone())
+ .or_insert_with(|| Value::Object(Map::new()));
+ object = child.as_object_mut().ok_or_else(|| {
+ ProtocolAdapterError::ClientUnrepresentable(
+ "reasoning path collides with a non-object native field".into(),
+ )
+ })?;
+ }
+ if object.insert(tail[tail.len() - 1].clone(), value).is_some() {
+ return Err(ProtocolAdapterError::ClientUnrepresentable(
+ "reasoning field is assigned more than once".into(),
+ ));
+ }
+ Ok(())
+}
+
+fn invalid(message: &str) -> ProtocolAdapterError {
+ ProtocolAdapterError::ClientUnrepresentable(message.into())
+}
+
+fn complete_messages_thinking(
+ body: &mut Map<String, Value>,
+ protocol: IngressProtocol,
+) -> Result<(), ProtocolAdapterError> {
+ if protocol != IngressProtocol::Messages {
+ return Ok(());
+ }
+ let max_tokens = body.get("max_tokens").and_then(Value::as_u64);
+ let Some(thinking) = body.get_mut("thinking").and_then(Value::as_object_mut) else {
+ return Ok(());
+ };
+ // Older budget profiles stored only budget_tokens. Adaptive is explicit and
+ // must never acquire a manual budget from this normalization.
+ if thinking.contains_key("budget_tokens") && !thinking.contains_key("type") {
+ thinking.insert("type".into(), json!("enabled"));
+ }
+ match thinking.get("type").and_then(Value::as_str) {
+ Some("enabled") => {
+ let budget = thinking
+ .entry("budget_tokens")
+ .or_insert(json!(1024))
+ .as_u64()
+ .ok_or_else(|| invalid("Messages thinking budget must be an integer"))?;
+ if budget < 1024 || max_tokens.is_none_or(|limit| budget >= limit) {
+ return Err(invalid(
+ "Messages thinking budget must be at least 1024 and below max_tokens",
+ ));
+ }
+ }
+ Some("disabled") => {
+ thinking.remove("budget_tokens");
+ }
+ _ => {}
+ }
+ Ok(())
+}
diff --git a/crates/gateway/src/adapters/response/native_passthrough_evidence.rs b/crates/gateway/src/adapters/response/native_passthrough_evidence.rs
index 701f4e4f..df0e38a0 100644
--- a/crates/gateway/src/adapters/response/native_passthrough_evidence.rs
+++ b/crates/gateway/src/adapters/response/native_passthrough_evidence.rs
@@ -359,6 +359,17 @@ fn delta_kind(event: &str) -> Option<(&'static str, &'static str, Option<&'stati
fn final_text<'a>(item: &'a Map<String, Value>, position: usize, kind: &str) -> Option<&'a str> {
match kind {
+ // Some native streams report reasoning_text deltas and retain the
+ // identical final text as a summary. Only absent/null content permits
+ // this equivalent evidence; explicit content always remains authoritative.
+ "reasoning_text" if item.get("content").is_none_or(Value::is_null) => item
+ .get("summary")?
+ .as_array()?
+ .get(position)?
+ .as_object()
+ .filter(|part| part.get("type").and_then(Value::as_str) == Some("summary_text"))?
+ .get("text")?
+ .as_str(),
"output_text" | "refusal" | "reasoning_text" => item
.get("content")?
.as_array()?
diff --git a/crates/gateway/src/adapters/response/native_passthrough_evidence_tests.rs b/crates/gateway/src/adapters/response/native_passthrough_evidence_tests.rs
index 05956379..abc54144 100644
--- a/crates/gateway/src/adapters/response/native_passthrough_evidence_tests.rs
+++ b/crates/gateway/src/adapters/response/native_passthrough_evidence_tests.rs
@@ -60,6 +60,112 @@ fn responses_deltas_are_compared_to_the_final_snapshot_without_buffering_text()
}
}
+#[test]
+fn reasoning_text_can_match_an_identical_summary_without_masking_conflicts() {
+ let reasoning = json!({"type":"reasoning","id":"thought","status":null,
+ "content":null,"summary":[{"type":"summary_text","text":"first-second"}]});
+ let mut absent_content = reasoning.clone();
+ absent_content.as_object_mut().unwrap().remove("content");
+ let mut wrong_text = reasoning.clone();
+ wrong_text["summary"][0]["text"] = json!("replacement");
+ let mut wrong_identity = reasoning.clone();
+ wrong_identity["id"] = json!("another-thought");
+ let mut wrong_position = reasoning.clone();
+ wrong_position["summary"] = json!([
+ {"type":"summary_text","text":"other"},
+ {"type":"summary_text","text":"first-second"}
+ ]);
+ let mut wrong_type = reasoning.clone();
+ wrong_type["summary"][0]["type"] = json!("future_summary");
+ let mut explicit_content = reasoning.clone();
+ explicit_content["content"] = json!([{"type":"reasoning_text","text":"conflict"}]);
+ let mut empty_content = reasoning.clone();
+ empty_content["content"] = json!([]);
+ let mut missing_summary = reasoning.clone();
+ missing_summary.as_object_mut().unwrap().remove("summary");
+ for (item, done_text, expected) in [
+ (
+ reasoning.clone(),
+ "first-second",
+ NativeTerminalOutcome::Complete,
+ ),
+ (
+ absent_content,
+ "first-second",
+ NativeTerminalOutcome::Complete,
+ ),
+ (wrong_text, "first-second", NativeTerminalOutcome::Unknown),
+ (
+ wrong_identity,
+ "first-second",
+ NativeTerminalOutcome::Unknown,
+ ),
+ (
+ wrong_position,
+ "first-second",
+ NativeTerminalOutcome::Unknown,
+ ),
+ (wrong_type, "first-second", NativeTerminalOutcome::Unknown),
+ (
+ explicit_content,
+ "first-second",
+ NativeTerminalOutcome::Unknown,
+ ),
+ (
+ empty_content,
+ "first-second",
+ NativeTerminalOutcome::Unknown,
+ ),
+ (
+ missing_summary,
+ "first-second",
+ NativeTerminalOutcome::Unknown,
+ ),
+ (
+ reasoning,
+ "conflicting-done",
+ NativeTerminalOutcome::Unknown,
+ ),
+ ] {
+ let mut stream = projector(IngressProtocol::Responses, true);
+ let mut events = Vec::new();
+ for delta in ["first-", "second"] {
+ events.extend(sse(
+ Some("response.reasoning_text.delta"),
+ &json!({
+ "type":"response.reasoning_text.delta","item_id":"thought",
+ "output_index":0,"content_index":0,"delta":delta
+ }),
+ ));
+ }
+ events.extend(sse(
+ Some("response.reasoning_text.done"),
+ &json!({
+ "type":"response.reasoning_text.done","item_id":"thought",
+ "output_index":0,"content_index":0,"text":done_text
+ }),
+ ));
+ events.extend(sse(
+ Some("response.output_item.done"),
+ &json!({
+ "type":"response.output_item.done","output_index":0,"item":item
+ }),
+ ));
+ events.extend(sse(
+ Some("response.completed"),
+ &json!({
+ "type":"response.completed","response":{
+ "id":"response","model":"physical","status":"completed","output":[item]
+ }
+ }),
+ ));
+ let units = stream.feed(&events, true).unwrap();
+ assert_eq!(units.last().unwrap().terminal, Some(expected), "{item}");
+ assert_eq!(unit_json(&units[3])["item"], item);
+ assert_eq!(unit_json(&units[4])["response"]["output"][0], item);
+ }
+}
+
#[test]
fn responses_delta_after_done_keeps_wire_but_withholds_complete() {
let mut stream = projector(IngressProtocol::Responses, true);
diff --git a/crates/gateway/src/attempt_outcome/mod.rs b/crates/gateway/src/attempt_outcome/mod.rs
index f0ea727a..2a21c2df 100644
--- a/crates/gateway/src/attempt_outcome/mod.rs
+++ b/crates/gateway/src/attempt_outcome/mod.rs
@@ -2,7 +2,7 @@
//!
//! HTTP status alone is sufficient only where the protocol contract is
//! unambiguous. In particular, a 429 must carry the Connector-classified
-//! quota or binding-overload scope; an unclassified 429 fails closed.
+//! quota or binding-overload scope; an unclassified 429 can relay without guessing a quarantine scope.
use std::time::Duration;
@@ -110,25 +110,11 @@ impl AttemptFailure {
}
/// Whether a failure may select another frozen target while the
- /// downstream semantic commit fence is still clear.
+ /// downstream response body commit fence is still clear.
pub fn is_precommit_relayable(&self) -> bool {
- match self.class {
- AttemptFailureClass::Credential
- | AttemptFailureClass::Quota
- | AttemptFailureClass::BindingOverload
- | AttemptFailureClass::Protocol
- | AttemptFailureClass::Transient
- | AttemptFailureClass::Timeout(_)
- | AttemptFailureClass::Disconnect => true,
- AttemptFailureClass::PreOutputStream(class) => !matches!(
- class,
- PreOutputStreamClass::PermanentClient | PreOutputStreamClass::Unclassified
- ),
- AttemptFailureClass::ReasoningHistory
- | AttemptFailureClass::PermanentClient
- | AttemptFailureClass::PostCommit
- | AttemptFailureClass::Unclassified => false,
- }
+ // Cause classification controls quarantine and key rotation, not whether
+ // another already-authorized candidate can answer before body delivery.
+ !matches!(self.class, AttemptFailureClass::PostCommit)
}
/// Credential and quota failures may stay on the same frozen binding and
@@ -313,7 +299,7 @@ mod tests {
}
#[test]
- fn fallback_never_guesses_an_unclassified_429() {
+ fn unclassified_429_can_relay_without_guessing_failure_scope() {
let failure = classify_failure(
&RawAttemptFailure::Http {
status: 429,
@@ -323,11 +309,12 @@ mod tests {
ConnectorErrorProfile::exact(),
);
assert_eq!(failure.class, AttemptFailureClass::Unclassified);
- assert!(!failure.is_precommit_relayable());
+ assert!(failure.is_precommit_relayable());
+ assert_eq!(failure.state_scope(), None);
}
#[test]
- fn permanent_client_statuses_are_never_success_or_fallback() {
+ fn upstream_client_errors_can_relay_without_quarantining() {
for status in [400, 404, 422] {
let failure = classify_failure(
&RawAttemptFailure::Http {
@@ -338,12 +325,13 @@ mod tests {
ConnectorErrorProfile::exact(),
);
assert_eq!(failure.class, AttemptFailureClass::PermanentClient);
- assert!(!failure.is_precommit_relayable());
+ assert!(failure.is_precommit_relayable());
+ assert_eq!(failure.state_scope(), None);
}
}
#[test]
- fn ambiguous_client_statuses_fail_closed_while_typed_protocol_can_relay() {
+ fn ambiguous_and_typed_client_errors_can_relay() {
let protocol = classify_failure(
&RawAttemptFailure::Http {
status: 404,
@@ -364,7 +352,8 @@ mod tests {
ConnectorErrorProfile::exact(),
);
assert_eq!(ambiguous.class, AttemptFailureClass::Unclassified);
- assert!(!ambiguous.is_precommit_relayable());
+ assert!(ambiguous.is_precommit_relayable());
+ assert_eq!(ambiguous.state_scope(), None);
}
#[test]
diff --git a/crates/gateway/src/core_runtime.rs b/crates/gateway/src/core_runtime.rs
index 0b30e32a..db468918 100644
--- a/crates/gateway/src/core_runtime.rs
+++ b/crates/gateway/src/core_runtime.rs
@@ -1248,11 +1248,16 @@ impl ProductionGatewayRuntime {
drop(core_session);
let runtime_state_authority_failed =
!response_started && runtime_state_authority.failed();
- let observation_outcome = classify_request_observation_outcome(
- request_observation.has_accepted_attempt(),
- result.is_ok(),
- request_observation.accepted_attempt_cancelled(),
- );
+ let model_failed = result.is_ok() && request_observation.accepted_attempt_failed();
+ let observation_outcome = if model_failed {
+ "failed"
+ } else {
+ classify_request_observation_outcome(
+ request_observation.has_accepted_attempt(),
+ result.is_ok(),
+ request_observation.accepted_attempt_cancelled(),
+ )
+ };
if let Some(mut guard) = agent_turn_guard.take() {
request_observation.finish_agent_turn_output().await;
let executions = request_observation
@@ -1277,6 +1282,10 @@ impl ProductionGatewayRuntime {
})
.unwrap_or_default();
let status = match (request_observation.has_accepted_attempt(), result.is_ok()) {
+ (true, true) if model_failed => AgentTurnStatus::Failed,
+ (true, true) if request_observation.accepted_attempt_unknown() => {
+ AgentTurnStatus::Unknown
+ }
(true, true) => AgentTurnStatus::Completed,
(true, false) => AgentTurnStatus::Interrupted,
(false, _) => AgentTurnStatus::Failed,
diff --git a/crates/gateway/src/core_runtime/classification.rs b/crates/gateway/src/core_runtime/classification.rs
index b4812ae6..91934532 100644
--- a/crates/gateway/src/core_runtime/classification.rs
+++ b/crates/gateway/src/core_runtime/classification.rs
@@ -15,6 +15,12 @@ use http::header::{CONTENT_LENGTH, CONTENT_TYPE, HOST};
use http::{HeaderMap, HeaderValue, Method, StatusCode};
use tokio_util::sync::CancellationToken;
+#[path = "classification/diagnostic.rs"]
+mod diagnostic;
+#[cfg(test)]
+use diagnostic::classifier_status_failure;
+use diagnostic::{CallDiagnostic, classifier_response_failure, diagnostic_request};
+
#[path = "classification/group_policy.rs"]
mod group_policy;
#[path = "classification/protocol.rs"]
@@ -25,10 +31,7 @@ pub(super) use group_policy::{apply as apply_group_policy, execution_position};
use super::ProductionGatewayRuntime;
use super::adapters::{PreparedReplayTemplate, sequential_replay_body};
-use super::model_ir::{
- CanonicalMessage, ContentPart, MODEL_REQUEST_IR_SCHEMA, MessageRole, ModelRequestIRV1,
- RequestedReasoningControl, ToolChoice,
-};
+use super::model_ir::{ContentPart, MessageRole, ModelRequestIRV1};
use super::profiles::{
BranchDecisionV1, ClassifierFallbackReasonV1, CompiledClassifierKindV1,
CompiledComplexityStrategyV1, ComplexityDecisionSourceV1, ComplexityReasonCodeV1, ComplexityV1,
@@ -40,7 +43,7 @@ use crate::ports::{ExecutionScope, HeaderSecretLeaseRequest};
use crate::replay::{ReplayError, ReplayStore};
use crate::runtime::TargetResolver;
use crate::server::request_plan::{
- ClassifierAuthenticationAuthorityV1, IngressProtocol, RestBranchClassifierAuthorityV1,
+ ClassifierAuthenticationAuthorityV1, RestBranchClassifierAuthorityV1,
};
use protocol::{
ClassifierAssessment, ClassifierResponse, classifier_request_template,
@@ -65,6 +68,9 @@ pub enum ClassifierDiagnosticError {
Timeout,
Unavailable,
RejectedInput,
+ AuthenticationRejected,
+ RateLimited,
+ EndpointRejected,
InvalidOutput,
Cancelled,
Resource,
@@ -78,6 +84,9 @@ impl ClassifierDiagnosticError {
Self::Timeout => "CLASSIFIER_TIMEOUT",
Self::Unavailable => "CLASSIFIER_UNAVAILABLE",
Self::RejectedInput => "CLASSIFIER_INPUT_REJECTED",
+ Self::AuthenticationRejected => "CLASSIFIER_AUTH_REJECTED",
+ Self::RateLimited => "CLASSIFIER_RATE_LIMITED",
+ Self::EndpointRejected => "CLASSIFIER_ENDPOINT_REJECTED",
Self::InvalidOutput => "CLASSIFIER_OUTPUT_INVALID",
Self::Cancelled => "CLASSIFIER_TEST_CANCELLED",
Self::Resource => "CLASSIFIER_TEST_RESOURCE_UNAVAILABLE",
@@ -114,6 +123,7 @@ struct PreparedDecisionRequest {
}
pub(super) struct ClassificationOutcome {
+ diagnostic_failure: Option<ClassifierDiagnosticError>,
pub(super) decision: BranchDecisionV1,
pub(super) facts: SanitizedStructuralFactsV1,
pub(super) assessment: Option<BoundAssessment>,
@@ -131,6 +141,9 @@ enum CallFailure {
Timeout,
Unavailable,
RejectedInput,
+ AuthenticationRejected,
+ RateLimited,
+ EndpointRejected,
InvalidOutput,
}
@@ -235,6 +248,9 @@ impl ProductionGatewayRuntime {
ClassificationError::Integrity => ClassifierDiagnosticError::Integrity,
ClassificationError::Resource => ClassifierDiagnosticError::Resource,
})?;
+ if let Some(failure) = result.diagnostic_failure {
+ return Err(failure);
+ }
if result.decision.fallback_used {
return Err(match result.decision.fallback_reason {
Some(ClassifierFallbackReasonV1::Timeout) => ClassifierDiagnosticError::Timeout,
@@ -288,6 +304,7 @@ impl ProductionGatewayRuntime {
|| strategy.classifier_kind == CompiledClassifierKindV1::LocalRules
{
return Ok(ClassificationOutcome {
+ diagnostic_failure: None,
decision: local_decision,
facts,
assessment: None,
@@ -359,6 +376,7 @@ impl ProductionGatewayRuntime {
.map(|target| bind_assessment(target, assessment))
});
Ok(ClassificationOutcome {
+ diagnostic_failure: None,
decision: BranchDecisionV1 {
policy: None,
competence_trigger: None,
@@ -397,11 +415,16 @@ impl ProductionGatewayRuntime {
fallback.classification_duration_micros = Some(classification_duration_micros);
fallback.fallback_reason = Some(match failure {
CallFailure::Timeout => ClassifierFallbackReasonV1::Timeout,
- CallFailure::Unavailable => ClassifierFallbackReasonV1::Unavailable,
- CallFailure::RejectedInput => ClassifierFallbackReasonV1::RejectedInput,
+ CallFailure::Unavailable
+ | CallFailure::AuthenticationRejected
+ | CallFailure::RateLimited => ClassifierFallbackReasonV1::Unavailable,
+ CallFailure::RejectedInput | CallFailure::EndpointRejected => {
+ ClassifierFallbackReasonV1::RejectedInput
+ }
CallFailure::InvalidOutput => ClassifierFallbackReasonV1::InvalidOutput,
});
Ok(ClassificationOutcome {
+ diagnostic_failure: Some(failure.diagnostic()),
decision: fallback,
facts,
assessment: None,
@@ -423,6 +446,7 @@ impl ProductionGatewayRuntime {
cancellation: CancellationToken,
) -> Result<Result<ClassifierResponse, CallFailure>, ClassificationError> {
let template = &prepared.template;
+ let mut diagnostic = CallDiagnostic::new(self.observation.diagnostic_context());
let scope = ExecutionScope::new(call_deadline, cancellation.clone());
ensure_call_active(call_deadline, overall_deadline, &cancellation)?;
@@ -474,6 +498,7 @@ impl ProductionGatewayRuntime {
}
}
+ diagnostic.prepared(&headers, &template.bytes);
let resolver = TargetResolver::new();
let target = match resolver
.resolve(classifier.transport_target.clone(), &scope)
@@ -548,10 +573,19 @@ impl ProductionGatewayRuntime {
.budget()
.reserve(MemoryRole::ResponsePrefix, CLASSIFIER_RESPONSE_BYTES)
.map_err(|_| ClassificationError::Resource)?;
- let exchange_result = collect_response(&mut exchange, call_deadline).await;
+ let exchange_result = collect_response(&mut exchange, call_deadline, &mut diagnostic).await;
+ diagnostic.protocol(exchange.transport_facts().upstream_protocol);
let (status, body) = match exchange_result {
Ok(response) => response,
Err(CollectResponseError::Attempt(error)) => {
+ diagnostic.failure(match error {
+ AttemptError::DeadlineExceeded
+ | AttemptError::ConnectTimeout
+ | AttemptError::FirstByteTimeout
+ | AttemptError::StreamIdleTimeout
+ | AttemptError::RequestWriteTimeout => CallFailure::Timeout,
+ _ => CallFailure::Unavailable,
+ });
let _ = exchange
.finish_or_abort_bounded(classifier_cleanup_timeout(call_deadline))
.await;
@@ -565,6 +599,7 @@ impl ProductionGatewayRuntime {
);
}
Err(CollectResponseError::InvalidOutput) => {
+ diagnostic.failure(CallFailure::InvalidOutput);
let _ = exchange
.finish_or_abort_bounded(classifier_cleanup_timeout(call_deadline))
.await;
@@ -625,48 +660,16 @@ impl ProductionGatewayRuntime {
}
if status == StatusCode::OK {
- return Ok(match &prepared.bindings {
+ let result = match &prepared.bindings {
Some(bindings) => system_one::parse_response(&body, bindings),
None => parse_classifier_response(&body, &classifier.decision, prepared.has_target),
- });
+ };
+ diagnostic.finish(result.as_ref().err().copied());
+ return Ok(result);
}
- Ok(Err(classifier_status_failure(status)))
- }
-}
-
-fn diagnostic_request() -> ModelRequestIRV1 {
- ModelRequestIRV1 {
- native_body: None,
- native_only: false,
- schema_version: MODEL_REQUEST_IR_SCHEMA.into(),
- ingress_protocol: IngressProtocol::Responses,
- responses_options: None,
- responses_item_ids: Default::default(),
- responses_item_statuses: Default::default(),
- served_model_id: "hiroute-classifier-diagnostic".into(),
- stream: false,
- instructions: Vec::new(),
- messages: vec![CanonicalMessage {
- role: MessageRole::User,
- content: vec![ContentPart::Text {
- text: CLASSIFIER_DIAGNOSTIC_LATEST_USER.into(),
- }],
- name: None,
- }],
- tools: Vec::new(),
- tool_namespaces: Vec::new(),
- responses_tool_order: Vec::new(),
- web_search: None,
- responses_search_history: Default::default(),
- responses_annotations: Default::default(),
- responses_message_phases: Default::default(),
- responses_internal_chat_message_metadata: Default::default(),
- responses_reasoning_history: Default::default(),
- tool_choice: ToolChoice::None,
- parallel_tool_calls: false,
- requested_reasoning: RequestedReasoningControl::absent(),
- requested_max_output_tokens: None,
- provider_state: Vec::new(),
+ let failure = classifier_response_failure(status, &body);
+ diagnostic.finish(Some(failure));
+ Ok(Err(failure))
}
}
@@ -691,6 +694,7 @@ fn bind_assessment(target: AssessmentTarget, assessment: ClassifierAssessment) -
async fn collect_response<T: hiroute_gateway_core::runtime::attempt::AttemptTransport>(
exchange: &mut AttemptExchange<T>,
deadline: Instant,
+ diagnostic: &mut CallDiagnostic,
) -> Result<(StatusCode, Vec<u8>), CollectResponseError> {
let mut status = None;
let mut body = Vec::new();
@@ -700,8 +704,9 @@ async fn collect_response<T: hiroute_gateway_core::runtime::attempt::AttemptTran
.drive_writer_once()
.await
.map_err(CollectResponseError::Attempt)?;
+ diagnostic.protocol(exchange.transport_facts().upstream_protocol);
while let Some(event) = exchange.next_precommit_event() {
- consume_response_event(event, &mut status, &mut body, &mut ended)?;
+ consume_response_event(event, &mut status, &mut body, &mut ended, diagnostic)?;
}
}
while !ended {
@@ -712,7 +717,7 @@ async fn collect_response<T: hiroute_gateway_core::runtime::attempt::AttemptTran
else {
return Err(CollectResponseError::InvalidOutput);
};
- consume_response_event(event, &mut status, &mut body, &mut ended)?;
+ consume_response_event(event, &mut status, &mut body, &mut ended, diagnostic)?;
}
let status = status.ok_or(CollectResponseError::InvalidOutput)?;
Ok((status, body))
@@ -723,12 +728,14 @@ fn consume_response_event(
status: &mut Option<StatusCode>,
body: &mut Vec<u8>,
ended: &mut bool,
+ diagnostic: &mut CallDiagnostic,
) -> Result<(), CollectResponseError> {
if *ended {
return Err(CollectResponseError::InvalidOutput);
}
match event {
PrecommitEvent::ResponseHead(head) if status.is_none() => {
+ diagnostic.response(head.headers(), head.status());
*status = Some(head.status());
}
PrecommitEvent::ResponseHead(_) => {
@@ -760,21 +767,6 @@ fn map_preparation_error(error: AttemptError) -> ClassificationError {
}
}
-fn classifier_status_failure(status: StatusCode) -> CallFailure {
- if status == StatusCode::UNAUTHORIZED
- || status == StatusCode::FORBIDDEN
- || status == StatusCode::TOO_MANY_REQUESTS
- || status.as_u16() == 529
- || status.is_server_error()
- {
- CallFailure::Unavailable
- } else if status.is_client_error() {
- CallFailure::RejectedInput
- } else {
- CallFailure::InvalidOutput
- }
-}
-
fn map_call_error(
error: AttemptError,
call_deadline: Instant,
@@ -951,9 +943,6 @@ mod tests {
);
}
for status in [
- StatusCode::UNAUTHORIZED,
- StatusCode::FORBIDDEN,
- StatusCode::TOO_MANY_REQUESTS,
StatusCode::INTERNAL_SERVER_ERROR,
StatusCode::from_u16(529).unwrap(),
] {
@@ -967,6 +956,29 @@ mod tests {
classify_attempt_failure(&AttemptError::Body(BodyError::BodyLimitExceeded)),
CallFailure::InvalidOutput
);
+ for status in [StatusCode::UNAUTHORIZED, StatusCode::FORBIDDEN] {
+ assert_eq!(
+ classifier_response_failure(status, b"private token"),
+ CallFailure::AuthenticationRejected
+ );
+ }
+ assert_eq!(
+ classifier_status_failure(StatusCode::TOO_MANY_REQUESTS),
+ CallFailure::RateLimited
+ );
+ assert_eq!(
+ classifier_response_failure(
+ StatusCode::BAD_REQUEST,
+ br#"{"error":{"message":"Workspace endpoint is invalid."}}"#
+ ),
+ CallFailure::EndpointRejected
+ );
+ let failure = classifier_response_failure(
+ StatusCode::BAD_REQUEST,
+ br#"{"error":{"message":"private token details"}}"#,
+ );
+ assert_eq!(failure, CallFailure::RejectedInput);
+ assert_eq!(failure.diagnostic().code(), "CLASSIFIER_INPUT_REJECTED");
}
#[tokio::test(flavor = "current_thread")]
@@ -1038,9 +1050,35 @@ mod tests {
address.port()
));
std::fs::create_dir_all(&root).unwrap();
+ #[cfg(unix)]
+ {
+ use std::os::unix::fs::PermissionsExt;
+ std::fs::set_permissions(&root, std::fs::Permissions::from_mode(0o700)).unwrap();
+ }
let publications =
Arc::new(GatewayPublicationInstaller::open(root.join("publication.json")).unwrap());
- let runtime = ProductionGatewayRuntime::compose(ProductionPorts::fail_closed(publications));
+ let diagnostics = hiroute_diagnostics::runtime::DiagnosticRuntime::start(
+ hiroute_diagnostics::runtime::RuntimeConfig {
+ root: root.join("diagnostics"),
+ role: hiroute_diagnostics::event::ProcessRole::Daemon,
+ component: hiroute_diagnostics::record::Component::Gateway,
+ parent_session_id: None,
+ level_override: Some(hiroute_diagnostics::DiagnosticLevel::Debug),
+ },
+ );
+ assert!(
+ diagnostics.owns_role(),
+ "diagnostic fixture must own its writer"
+ );
+ let observation = Arc::new(
+ super::super::observation::GatewayObservation::from_environment()
+ .with_diagnostics(diagnostics.port()),
+ );
+ let runtime = ProductionGatewayRuntime::compose_with_planner_and_observation(
+ ProductionPorts::fail_closed(publications),
+ Arc::new(super::super::PublicationPlannerInputAuthority),
+ observation,
+ );
let mode = hiroute_domain::ComplexityClassifierModeV1::DecisionService {
service: Box::new(hiroute_domain::DecisionServiceV1 {
id: "decision-fixture".into(),
@@ -1057,6 +1095,15 @@ mod tests {
let outcome = runtime.test_classifier_decision(&mode).await.unwrap();
assert_eq!(outcome.branch_id, "local");
server.join().unwrap();
+ diagnostics.shutdown();
+ let log = std::fs::read_to_string(root.join("diagnostics/daemon/current.jsonl")).unwrap();
+ assert!(
+ log.contains("\"request_kind\":\"decision_service\""),
+ "{log}"
+ );
+ assert!(log.contains("\"http_protocol\":\"http1\""), "{log}");
+ assert!(log.contains("\"phase\":\"completed\""), "{log}");
+ assert!(!log.contains(CLASSIFIER_DIAGNOSTIC_LATEST_USER), "{log}");
std::fs::remove_dir_all(root).unwrap();
}
}
diff --git a/crates/gateway/src/core_runtime/classification/diagnostic.rs b/crates/gateway/src/core_runtime/classification/diagnostic.rs
new file mode 100644
index 00000000..5428334c
--- /dev/null
+++ b/crates/gateway/src/core_runtime/classification/diagnostic.rs
@@ -0,0 +1,247 @@
+//! Safe diagnostics for the decision service's real transport path.
+
+use std::sync::atomic::{AtomicU64, Ordering};
+
+use hiroute_diagnostics::{
+ context::DiagnosticContext,
+ correlation::CorrelationDomain,
+ event::{
+ DiagnosticEvent, UpstreamWire, UpstreamWirePhase, WireHttpProtocol, WireProviderError,
+ WireRequestKind,
+ },
+};
+use hiroute_gateway_core::transport::HttpProtocol;
+use http::{HeaderMap, StatusCode};
+
+use super::{CLASSIFIER_DIAGNOSTIC_LATEST_USER, CallFailure, ClassifierDiagnosticError};
+use crate::server::core_runtime::model_ir::{
+ CanonicalMessage, ContentPart, MODEL_REQUEST_IR_SCHEMA, MessageRole, ModelRequestIRV1,
+ RequestedReasoningControl, ToolChoice,
+};
+use crate::server::core_runtime::observation::wire_diagnostic;
+use crate::server::request_plan::IngressProtocol;
+
+static NEXT_CALL: AtomicU64 = AtomicU64::new(1);
+
+pub(super) struct CallDiagnostic {
+ context: DiagnosticContext,
+ final_event: UpstreamWire,
+ finished: bool,
+}
+
+impl CallDiagnostic {
+ pub(super) fn new(context: DiagnosticContext) -> Self {
+ let mut final_event = wire_diagnostic::response(&HeaderMap::new(), 0, &context);
+ final_event.http_status = None;
+ final_event.request_kind = Some(WireRequestKind::DecisionService);
+ final_event.request_token = context.token(
+ CorrelationDomain::Decision,
+ &NEXT_CALL.fetch_add(1, Ordering::Relaxed).to_string(),
+ );
+ final_event.phase = UpstreamWirePhase::Failure;
+ final_event.provider_error = Some(WireProviderError::Unknown);
+ Self {
+ context,
+ final_event,
+ finished: false,
+ }
+ }
+
+ pub(super) fn prepared(&mut self, headers: &HeaderMap, template: &[u8]) {
+ if self.context.handle().level().is_none() {
+ return;
+ }
+ let mut event = wire_diagnostic::request(headers, template);
+ event.request_token = self.final_event.request_token;
+ event.request_kind = Some(WireRequestKind::DecisionService);
+ self.final_event.native_model = event.native_model.clone();
+ self.final_event.request_reasoning = event.request_reasoning.clone();
+ self.context.emit(DiagnosticEvent::UpstreamWire(event));
+ }
+
+ pub(super) fn protocol(&mut self, protocol: Option<HttpProtocol>) {
+ self.final_event.http_protocol = protocol.map(|protocol| match protocol {
+ HttpProtocol::Http1 => WireHttpProtocol::Http1,
+ HttpProtocol::Http2 => WireHttpProtocol::Http2,
+ });
+ }
+
+ pub(super) fn response(&mut self, headers: &HeaderMap, status: StatusCode) {
+ let mut event = wire_diagnostic::response(headers, status.as_u16(), &self.context);
+ event.request_kind = Some(WireRequestKind::DecisionService);
+ event.request_token = self.final_event.request_token;
+ event.http_protocol = self.final_event.http_protocol;
+ self.final_event.http_status = event.http_status;
+ self.final_event.upstream_request_token = event.upstream_request_token;
+ self.final_event.provider_error = event.provider_error.or(Some(WireProviderError::Unknown));
+ self.context.emit(DiagnosticEvent::UpstreamWire(event));
+ }
+
+ pub(super) fn failure(&mut self, failure: CallFailure) {
+ self.final_event.provider_error = Some(match failure {
+ CallFailure::Timeout => WireProviderError::Timeout,
+ CallFailure::Unavailable => WireProviderError::UpstreamUnavailable,
+ CallFailure::RejectedInput => WireProviderError::InputRejected,
+ CallFailure::AuthenticationRejected => WireProviderError::AuthenticationRejected,
+ CallFailure::RateLimited => WireProviderError::RateLimited,
+ CallFailure::EndpointRejected => WireProviderError::EndpointRejected,
+ CallFailure::InvalidOutput => WireProviderError::InvalidOutput,
+ });
+ }
+
+ pub(super) fn finish(&mut self, failure: Option<CallFailure>) {
+ match failure {
+ Some(failure) => self.failure(failure),
+ None => {
+ self.final_event.phase = UpstreamWirePhase::Completed;
+ self.final_event.provider_error = None;
+ }
+ }
+ self.context
+ .emit(DiagnosticEvent::UpstreamWire(self.final_event.clone()));
+ self.finished = true;
+ }
+}
+
+impl Drop for CallDiagnostic {
+ fn drop(&mut self) {
+ if !self.finished {
+ self.context
+ .emit(DiagnosticEvent::UpstreamWire(self.final_event.clone()));
+ }
+ }
+}
+
+pub(super) fn classifier_response_failure(status: StatusCode, body: &[u8]) -> CallFailure {
+ if status == StatusCode::BAD_REQUEST
+ && let Ok(value) = serde_json::from_slice::<serde_json::Value>(body)
+ && value
+ .get("error")
+ .unwrap_or(&value)
+ .get("message")
+ .and_then(serde_json::Value::as_str)
+ == Some("Workspace endpoint is invalid.")
+ {
+ return CallFailure::EndpointRejected;
+ }
+ classifier_status_failure(status)
+}
+
+pub(super) fn classifier_status_failure(status: StatusCode) -> CallFailure {
+ if matches!(status, StatusCode::UNAUTHORIZED | StatusCode::FORBIDDEN) {
+ return CallFailure::AuthenticationRejected;
+ }
+ if status == StatusCode::TOO_MANY_REQUESTS {
+ return CallFailure::RateLimited;
+ }
+ if status.as_u16() == 529 || status.is_server_error() {
+ CallFailure::Unavailable
+ } else if status.is_client_error() {
+ CallFailure::RejectedInput
+ } else {
+ CallFailure::InvalidOutput
+ }
+}
+
+pub(super) fn diagnostic_request() -> ModelRequestIRV1 {
+ ModelRequestIRV1 {
+ native_body: None,
+ native_only: false,
+ schema_version: MODEL_REQUEST_IR_SCHEMA.into(),
+ ingress_protocol: IngressProtocol::Responses,
+ responses_options: None,
+ responses_item_ids: Default::default(),
+ responses_item_statuses: Default::default(),
+ served_model_id: "hiroute-classifier-diagnostic".into(),
+ stream: false,
+ instructions: Vec::new(),
+ messages: vec![CanonicalMessage {
+ role: MessageRole::User,
+ content: vec![ContentPart::Text {
+ text: CLASSIFIER_DIAGNOSTIC_LATEST_USER.into(),
+ }],
+ name: None,
+ }],
+ tools: Vec::new(),
+ tool_namespaces: Vec::new(),
+ responses_tool_order: Vec::new(),
+ web_search: None,
+ responses_search_history: Default::default(),
+ responses_annotations: Default::default(),
+ responses_message_phases: Default::default(),
+ responses_internal_chat_message_metadata: Default::default(),
+ responses_reasoning_history: Default::default(),
+ tool_choice: ToolChoice::None,
+ parallel_tool_calls: false,
+ requested_reasoning: RequestedReasoningControl::absent(),
+ requested_max_output_tokens: None,
+ provider_state: Vec::new(),
+ }
+}
+
+impl CallFailure {
+ pub(super) fn diagnostic(self) -> ClassifierDiagnosticError {
+ match self {
+ Self::Timeout => ClassifierDiagnosticError::Timeout,
+ Self::Unavailable => ClassifierDiagnosticError::Unavailable,
+ Self::RejectedInput => ClassifierDiagnosticError::RejectedInput,
+ Self::InvalidOutput => ClassifierDiagnosticError::InvalidOutput,
+ Self::AuthenticationRejected => ClassifierDiagnosticError::AuthenticationRejected,
+ Self::RateLimited => ClassifierDiagnosticError::RateLimited,
+ Self::EndpointRejected => ClassifierDiagnosticError::EndpointRejected,
+ }
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn info_decision_failures_keep_closed_authentication_and_endpoint_causes() {
+ let root = std::env::temp_dir().join(format!(
+ "hiroute-info-decision-{}-{}",
+ std::process::id(),
+ crate::server::core_runtime::observation::unix_nanos()
+ ));
+ let runtime = hiroute_diagnostics::runtime::DiagnosticRuntime::start(
+ hiroute_diagnostics::runtime::RuntimeConfig {
+ root: root.clone(),
+ role: hiroute_diagnostics::event::ProcessRole::Daemon,
+ component: hiroute_diagnostics::record::Component::Gateway,
+ parent_session_id: None,
+ level_override: Some(hiroute_diagnostics::DiagnosticLevel::Info),
+ },
+ );
+ for (status, failure) in [
+ (
+ StatusCode::UNAUTHORIZED,
+ CallFailure::AuthenticationRejected,
+ ),
+ (StatusCode::BAD_REQUEST, CallFailure::EndpointRejected),
+ ] {
+ let mut diagnostic = CallDiagnostic::new(runtime.port().handle().context());
+ diagnostic.prepared(
+ &HeaderMap::new(),
+ br#"{"model":"decision-model-preview","input":"private-decision-prompt"}"#,
+ );
+ diagnostic.protocol(Some(HttpProtocol::Http2));
+ diagnostic.response(&HeaderMap::new(), status);
+ diagnostic.finish(Some(failure));
+ }
+ runtime.shutdown();
+ let log = std::fs::read_to_string(root.join("daemon/current.jsonl")).unwrap();
+ for expected in [
+ "authentication_rejected",
+ "endpoint_rejected",
+ "decision-model-preview",
+ "http2",
+ "decision_service",
+ ] {
+ assert!(log.contains(expected), "{expected}: {log}");
+ }
+ assert!(!log.contains("private-decision-prompt"), "{log}");
+ assert!(!log.contains("\"phase\":\"request\""), "{log}");
+ std::fs::remove_dir_all(root).unwrap();
+ }
+}
diff --git a/crates/gateway/src/core_runtime/classification/group_policy_tests.rs b/crates/gateway/src/core_runtime/classification/group_policy_tests.rs
index 63a798d0..0f16cd73 100644
--- a/crates/gateway/src/core_runtime/classification/group_policy_tests.rs
+++ b/crates/gateway/src/core_runtime/classification/group_policy_tests.rs
@@ -25,6 +25,7 @@ fn outcome(
};
let policy = branches[0].execution_policy();
ClassificationOutcome {
+ diagnostic_failure: None,
decision,
facts: input.classification_facts.clone().unwrap(),
assessment: score.map(|score| BoundAssessment {
diff --git a/crates/gateway/src/main.rs b/crates/gateway/src/main.rs
index 6228a57b..824cabaf 100644
--- a/crates/gateway/src/main.rs
+++ b/crates/gateway/src/main.rs
@@ -4,6 +4,12 @@ use std::net::SocketAddr;
use std::path::PathBuf;
use clap::Parser;
+use hiroute_diagnostics::{
+ DiagnosticLevel,
+ event::ProcessRole,
+ record::Component,
+ runtime::{DiagnosticRuntime, RuntimeConfig},
+};
#[cfg(feature = "e2e-test-control")]
use hiroute_gateway::server::test_control::E2eControlOptions;
use hiroute_gateway::server::{GatewayLaunchIdentity, GatewayLauncher, GatewayLauncherError};
@@ -32,6 +38,12 @@ struct Cli {
/// Optional sealed static Planner/profile input for production requests.
#[arg(long, conflicts_with = "fixture")]
planner: Option<PathBuf>,
+ /// Private local diagnostic root for this standalone process.
+ #[arg(long)]
+ diagnostics_root: Option<PathBuf>,
+ /// Temporary diagnostic level; never changes the saved setting.
+ #[arg(long, requires = "diagnostics_root")]
+ diagnostic_level_override: Option<DiagnosticLevel>,
#[cfg(feature = "e2e-test-control")]
/// Explicit, sealed real-process E2E control endpoint. Hidden so normal
/// production discovery cannot mistake this for a supported operator API.
@@ -57,8 +69,20 @@ fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
if cli.role != "gateway" {
return Err(GatewayLauncherError::UnsupportedRole(cli.role).into());
}
+ let diagnostics = cli.diagnostics_root.as_ref().map(|root| {
+ DiagnosticRuntime::start(RuntimeConfig {
+ root: root.clone(),
+ role: ProcessRole::Daemon,
+ component: Component::Gateway,
+ parent_session_id: None,
+ level_override: cli.diagnostic_level_override,
+ })
+ });
+ if let Some(runtime) = &diagnostics {
+ hiroute_diagnostics::panic::install_panic_hook(runtime.handle().clone());
+ }
let launch_identity = GatewayLaunchIdentity::from_environment()?;
- match cli.fixture {
+ let launcher = match cli.fixture {
Some(fixture) => {
GatewayLauncher::from_fixture_with_identity(cli.listen, &fixture, launch_identity)?
}
@@ -91,8 +115,12 @@ fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
)?
}
}
- }
- .serve()?;
+ };
+ let launcher = match &diagnostics {
+ Some(runtime) => launcher.with_diagnostics(runtime.port()),
+ None => launcher,
+ };
+ launcher.serve()?;
Ok(())
}
diff --git a/crates/gateway/src/observation.rs b/crates/gateway/src/observation.rs
index 3afebb46..1d1c27ad 100644
--- a/crates/gateway/src/observation.rs
+++ b/crates/gateway/src/observation.rs
@@ -21,6 +21,7 @@ mod pricing;
mod producer;
#[path = "observation/provider.rs"]
mod provider;
+pub(super) use provider::wire_diagnostic;
#[path = "observation/request.rs"]
mod request;
#[path = "observation/schema.rs"]
@@ -242,6 +243,13 @@ fn reliable_observation_session_id(
}
impl GatewayObservation {
+ pub(super) fn diagnostic_context(&self) -> hiroute_diagnostics::context::DiagnosticContext {
+ self.diagnostics
+ .lock()
+ .unwrap_or_else(|poisoned| poisoned.into_inner())
+ .handle()
+ .context()
+ }
pub fn with_price_source(mut self, source: Arc<dyn RequestPriceSource>) -> Self {
self.price_source = Some(source);
self
@@ -252,11 +260,15 @@ impl GatewayObservation {
/// are produced, and channel loss/NACK projection stays inert until a port
/// exists.
pub fn with_diagnostics(self, diagnostics: DiagnosticsPort) -> Self {
+ self.set_diagnostics(diagnostics);
+ self
+ }
+
+ pub(crate) fn set_diagnostics(&self, diagnostics: DiagnosticsPort) {
*self
.diagnostics
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner()) = diagnostics;
- self
}
pub fn from_environment() -> Self {
let environment = EnvironmentObservation::load();
diff --git a/crates/gateway/src/observation/content.rs b/crates/gateway/src/observation/content.rs
index 6f269bcc..e2966fb7 100644
--- a/crates/gateway/src/observation/content.rs
+++ b/crates/gateway/src/observation/content.rs
@@ -443,6 +443,11 @@ impl AcceptedResponseCapture {
/// body frame. The caller retains ownership of this sequencing rule.
pub fn accepted_frame(&mut self, body: &Bytes, end_stream: bool) {
if !self.request.is_enabled() {
+ if self.request.tracks_attempts() && (!body.is_empty() || end_stream) {
+ // Diagnostic acceptance has no content identity or capture.
+ // Business channels remain disabled by RequestObservation.
+ let _ = self.request.accept_current("", body.len());
+ }
return;
}
if !body.is_empty() || end_stream {
diff --git a/crates/gateway/src/observation/provider.rs b/crates/gateway/src/observation/provider.rs
index cd63c5bc..ced83905 100644
--- a/crates/gateway/src/observation/provider.rs
+++ b/crates/gateway/src/observation/provider.rs
@@ -29,7 +29,7 @@ use super::{RequestObservation, active_request};
#[path = "provider/capture.rs"]
mod capture;
#[path = "provider/wire_diagnostic.rs"]
-pub(super) mod wire_diagnostic;
+pub(in crate::server::core_runtime) mod wire_diagnostic;
pub(super) use capture::{CanonicalCaptureHandle, CanonicalCaptureProducer};
@@ -107,7 +107,7 @@ impl ProviderRuntimePort for ObservedProductionProvider {
.starts_with("credential/none/")
.then(|| context.credential_ref().as_str().to_owned());
let (request, inner) = self.inner.materialize_attempt(logical, context).await?;
- let observation = active_request().filter(RequestObservation::is_enabled);
+ let observation = active_request().filter(RequestObservation::tracks_attempts);
if let (Some(observation), Some(credential_ref)) = (&observation, no_credential_ref) {
observation.no_credential_materialized(&stable_binding_id, &credential_ref);
}
@@ -149,10 +149,7 @@ impl ProviderRuntimePort for ObservedProductionProvider {
if let (Some(observation), PrecommitEvent::ResponseHead(head)) =
(&state.observation, &event)
{
- observation.wire_diagnostic(wire_diagnostic::response(
- head.headers(),
- head.status().as_u16(),
- ));
+ observation.response_head_diagnostic(head.headers(), head.status().as_u16());
}
let prepared = state
.tracker
diff --git a/crates/gateway/src/observation/provider/wire_diagnostic.rs b/crates/gateway/src/observation/provider/wire_diagnostic.rs
index 034b8585..7b70c163 100644
--- a/crates/gateway/src/observation/provider/wire_diagnostic.rs
+++ b/crates/gateway/src/observation/provider/wire_diagnostic.rs
@@ -1,6 +1,8 @@
use hiroute_diagnostics::event::{
- UpstreamWire, UpstreamWirePhase, WireContentType, WireReasoningEffort, WireRequestReasoning,
+ NativeModelId, UpstreamWire, UpstreamWirePhase, WireContentType, WireProviderError,
+ WireReasoningEffort, WireRequestKind, WireRequestReasoning, WireThinkingType,
};
+use hiroute_diagnostics::{context::DiagnosticContext, correlation::CorrelationDomain};
use http::HeaderMap;
use serde::Deserialize;
use serde::de::{IgnoredAny, MapAccess, SeqAccess, Visitor};
@@ -34,10 +36,17 @@ fn shape(headers: &HeaderMap, phase: UpstreamWirePhase, status: Option<u16>) ->
http_status: status,
content_type,
request_reasoning: None,
+ request_kind: Some(WireRequestKind::ModelInference),
+ attempt_token: None,
+ attempt_index: None,
+ native_model: None,
+ upstream_request_token: None,
+ http_protocol: None,
+ provider_error: status.and_then(status_error),
}
}
-pub(in crate::server::core_runtime::observation) fn request(
+pub(in crate::server::core_runtime) fn request(
headers: &HeaderMap,
serialized_template: &[u8],
) -> UpstreamWire {
@@ -51,26 +60,80 @@ pub(in crate::server::core_runtime::observation) fn request(
}
// Read only controls from the exact encoder template. Serde skips other
// fields without allocating their strings or expanding Replay references.
- event.request_reasoning = serde_json::from_slice::<Controls>(serialized_template)
- .ok()
- .map(|body| WireRequestReasoning {
+ if let Ok(body) = serde_json::from_slice::<Controls>(serialized_template) {
+ event.native_model = body.model;
+ event.request_reasoning = Some(WireRequestReasoning {
responses_effort: body.reasoning.and_then(|value| value.effort.0),
chat_effort: body.reasoning_effort.0,
messages_effort: body.output_config.and_then(|value| value.effort.0),
+ messages_thinking: body.thinking.as_ref().and_then(|value| value.kind.0),
+ messages_budget_tokens: body.thinking.as_ref().and_then(|value| value.budget_tokens),
+ enable_thinking: body.enable_thinking,
+ thinking_enabled: body.thinking.as_ref().and_then(|value| value.enabled),
});
+ }
+ event
+}
+
+pub(in crate::server::core_runtime) fn response(
+ headers: &HeaderMap,
+ status: u16,
+ context: &DiagnosticContext,
+) -> UpstreamWire {
+ let mut event = shape(headers, UpstreamWirePhase::Response, Some(status));
+ // Values never leave this boundary. Different provider header spellings map
+ // to the same correlation domain, with a bounded input to the HMAC.
+ event.upstream_request_token = ["x-request-id", "x-dashscope-request-id", "request-id"]
+ .iter()
+ .find_map(|name| headers.get(*name))
+ .and_then(|value| value.to_str().ok())
+ .filter(|value| !value.is_empty() && value.len() <= 1024)
+ .and_then(|value| context.token(CorrelationDomain::ExternalRequest, value));
event
}
-pub(super) fn response(headers: &HeaderMap, status: u16) -> UpstreamWire {
- shape(headers, UpstreamWirePhase::Response, Some(status))
+pub(in crate::server::core_runtime) fn status_error(status: u16) -> Option<WireProviderError> {
+ match status {
+ 401 | 403 => Some(WireProviderError::AuthenticationRejected),
+ 429 => Some(WireProviderError::RateLimited),
+ 400..=499 => Some(WireProviderError::InputRejected),
+ 500..=599 => Some(WireProviderError::UpstreamUnavailable),
+ _ => None,
+ }
}
#[derive(Deserialize)]
struct Controls {
+ model: Option<NativeModelId>,
reasoning: Option<EffortObject>,
#[serde(default)]
reasoning_effort: Effort,
output_config: Option<EffortObject>,
+ thinking: Option<Thinking>,
+ enable_thinking: Option<bool>,
+}
+
+#[derive(Deserialize)]
+struct Thinking {
+ #[serde(default, rename = "type")]
+ kind: ThinkingType,
+ budget_tokens: Option<u64>,
+ enabled: Option<bool>,
+}
+
+#[derive(Default)]
+struct ThinkingType(Option<WireThinkingType>);
+
+impl<'de> Deserialize<'de> for ThinkingType {
+ fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
+ let value = String::deserialize(deserializer)?;
+ Ok(Self(Some(match value.as_str() {
+ "enabled" => WireThinkingType::Enabled,
+ "disabled" => WireThinkingType::Disabled,
+ "adaptive" => WireThinkingType::Adaptive,
+ _ => WireThinkingType::Other,
+ })))
+ }
}
#[derive(Deserialize)]
@@ -155,16 +218,24 @@ mod tests {
"content-type",
"application/json; charset=utf-8".parse().unwrap(),
);
- assert_eq!(response(&headers, 403).content_type, WireContentType::Json);
- assert_eq!(response(&headers, 403).http_status, Some(403));
- assert!(response(&headers, 403).request_reasoning.is_none());
+ let context = hiroute_diagnostics::context::DiagnosticHandle::default().context();
+ assert_eq!(
+ response(&headers, 403, &context).content_type,
+ WireContentType::Json
+ );
+ assert_eq!(response(&headers, 403, &context).http_status, Some(403));
+ assert!(
+ response(&headers, 403, &context)
+ .request_reasoning
+ .is_none()
+ );
}
#[test]
fn wire_reasoning_controls_are_closed_and_distinguish_missing_from_unavailable() {
let headers = HeaderMap::new();
let encoded = serde_json::to_vec(&serde_json::json!({
- "model":"secret-model", "input":"secret-prompt".repeat(20_000),
+ "model":"native-model", "input":"secret-prompt".repeat(20_000),
"reasoning":{"effort":"medium", "summary":"secret-summary"},
"reasoning_effort":"secret-control", "output_config":{"effort":null}
}))
@@ -174,6 +245,7 @@ mod tests {
assert_eq!(value["request_reasoning"]["responses_effort"], "medium");
assert_eq!(value["request_reasoning"]["chat_effort"], "other");
assert_eq!(value["request_reasoning"]["messages_effort"], "other");
+ assert_eq!(value["native_model"], "native-model");
assert!(!serde_json::to_string(&event).unwrap().contains("secret"));
let empty = request(&headers, br#"{}"#).request_reasoning.unwrap();
assert_eq!(empty.responses_effort, None);
@@ -237,4 +309,21 @@ mod tests {
);
}
}
+
+ #[test]
+ fn messages_controls_record_exact_type_budget_and_switches_without_other_fields() {
+ let event = request(&HeaderMap::new(), br#"{"model":"qwen3.6-flash","thinking":{"type":"enabled","budget_tokens":1024,"enabled":true,"secret":"private"},"enable_thinking":false,"messages":[{"content":"secret-prompt"}]}"#);
+ let value = serde_json::to_value(event).unwrap();
+ assert_eq!(value["native_model"], "qwen3.6-flash");
+ assert_eq!(value["request_reasoning"]["messages_thinking"], "enabled");
+ assert_eq!(value["request_reasoning"]["messages_budget_tokens"], 1024);
+ assert_eq!(value["request_reasoning"]["thinking_enabled"], true);
+ assert_eq!(value["request_reasoning"]["enable_thinking"], false);
+ assert!(!value.to_string().contains("private") && !value.to_string().contains("secret"));
+ let disabled = request(&HeaderMap::new(), br#"{"thinking":{"type":"disabled"}}"#);
+ assert_eq!(
+ disabled.request_reasoning.unwrap().messages_budget_tokens,
+ None
+ );
+ }
}
diff --git a/crates/gateway/src/observation/request.rs b/crates/gateway/src/observation/request.rs
index 05f418bb..8fb0a3b7 100644
--- a/crates/gateway/src/observation/request.rs
+++ b/crates/gateway/src/observation/request.rs
@@ -1,3 +1,6 @@
+#[path = "request/diagnostic.rs"]
+mod diagnostic;
+
use std::collections::BTreeMap;
use std::sync::{Arc, Mutex, OnceLock};
use std::time::{Duration, Instant};
@@ -105,9 +108,14 @@ pub(super) struct RequestObservationState {
pub(super) accepted_wire_usage_recorded: bool,
pub(super) accepted_attempt_finished: bool,
pub(super) accepted_attempt_cancelled: bool,
+ pub(super) accepted_attempt_failed: bool,
+ pub(super) accepted_attempt_unknown: bool,
pub(super) response_capture: Option<CanonicalCaptureHandle>,
pub(super) tool_id_projection: Option<ToolIdProjection>,
pub(super) response_part_ordinal: u32,
+ pub(super) provider_error: Option<hiroute_diagnostics::event::WireProviderError>,
+ pub(super) prepared_wire: Option<hiroute_diagnostics::event::UpstreamWire>,
+ pub(super) response_wire: Option<hiroute_diagnostics::event::UpstreamWire>,
}
/// Aggregate counters for one content capture cycle. Only sizes and counts are kept, so a
@@ -229,36 +237,12 @@ impl RequestObservation {
self.inner.context.emit(event);
}
- pub(super) fn wire_diagnostic(&self, mut event: hiroute_diagnostics::event::UpstreamWire) {
- event.request_token = self.inner.context.token(
- CorrelationDomain::ModelRequest,
- &self.inner.metadata.request_id,
- );
- self.emit_diagnostic(DiagnosticEvent::UpstreamWire(event));
- }
-
fn attempt_token(&self, attempt_id: &str) -> Option<CorrelationToken> {
self.inner
.context
.token(CorrelationDomain::Attempt, attempt_id)
}
- pub(crate) fn prepared_request_diagnostic(
- &self,
- headers: &http::HeaderMap,
- serialized_template: &[u8],
- ) {
- if self.is_enabled()
- && self.inner.context.handle().level()
- == Some(hiroute_diagnostics::DiagnosticLevel::Debug)
- {
- self.wire_diagnostic(super::provider::wire_diagnostic::request(
- headers,
- serialized_template,
- ));
- }
- }
-
pub(crate) fn response_failure(
&self,
stage: hiroute_diagnostics::event::ResponseFailureStage,
@@ -412,6 +396,10 @@ impl RequestObservation {
self.inner.enabled || self.inner.agent_turn_output.get().is_some()
}
+ pub(in crate::server::core_runtime::observation) fn tracks_attempts(&self) -> bool {
+ self.is_enabled() || self.inner.context.handle().level().is_some()
+ }
+
pub(crate) fn bind_agent_turn_output(
&self,
store: Arc<crate::agent_turn_history::AgentTurnHistoryStore>,
@@ -468,7 +456,7 @@ impl RequestObservation {
/// observation-only identity; generation zero records that no credential
/// authority or credential runtime state participated.
pub(super) fn no_credential_materialized(&self, stable_binding_id: &str, credential_ref: &str) {
- if !self.is_enabled() || !credential_ref.starts_with("credential/none/") {
+ if !self.tracks_attempts() || !credential_ref.starts_with("credential/none/") {
return;
}
self.start_attempt(stable_binding_id, credential_ref, credential_ref, 0);
@@ -480,7 +468,7 @@ impl RequestObservation {
result: Option<&RuntimeStateEntry>,
outcome: &str,
) {
- if !self.is_enabled() {
+ if !self.tracks_attempts() {
return;
}
let fields = state_key_fields(key);
@@ -617,7 +605,7 @@ impl RequestObservation {
frame_id: &str,
byte_count: usize,
) -> Option<AttemptObservation> {
- if !self.is_enabled() {
+ if !self.tracks_attempts() {
return None;
}
let attempt = {
@@ -662,7 +650,7 @@ impl RequestObservation {
}
pub fn finish(&self, outcome: &str) {
- if !self.is_enabled() {
+ if !self.tracks_attempts() {
return;
}
let terminal_attempt = {
@@ -731,16 +719,26 @@ impl RequestObservation {
generation: u64,
) {
let mut state = self.lock_state();
+ // Native endpoint state uses an alias of the logical binding. Repeated
+ // credential reads must retain the same staged attempt and relay link.
+ let logical_binding_id = state
+ .candidates
+ .get(stable_binding_id)
+ .map_or(stable_binding_id, |candidate| {
+ candidate.stable_binding_id.as_str()
+ });
if state.current_attempt.is_some()
|| state.accepted_attempt.is_some()
|| state.pending_attempt.as_ref().is_some_and(|pending| {
- pending.stable_binding_id == stable_binding_id && pending.key_id == key_id
+ pending.stable_binding_id == logical_binding_id && pending.key_id == key_id
})
{
return;
}
state.published_disposition = None;
state.accepted_wire_usage_recorded = false;
+ state.provider_error = None;
+ state.response_wire = None;
let candidate =
state
.candidates
@@ -815,6 +813,19 @@ impl RequestObservation {
request_token: self.inner.request_token,
attempt_token: self.attempt_token(&attempt.attempt_id),
attempt_index: u64::from(attempt.ordinal),
+ binding_token: self
+ .inner
+ .context
+ .token(CorrelationDomain::Binding, &attempt.stable_binding_id),
+ native_model: hiroute_diagnostics::event::NativeModelId::new(&attempt.request_model),
+ upstream_protocol: Some(match attempt.upstream_protocol.as_str() {
+ "responses" => hiroute_diagnostics::event::IngressProtocol::OpenAiResponses,
+ "chat_completions" | "chat" => {
+ hiroute_diagnostics::event::IngressProtocol::OpenAiChat
+ }
+ "messages" => hiroute_diagnostics::event::IngressProtocol::AnthropicMessages,
+ _ => hiroute_diagnostics::event::IngressProtocol::Unknown,
+ }),
}));
if attempt.previous_attempt_id.is_some() {
// Product attempts of one request are ordinal-sequential, so the attempt that
@@ -823,6 +834,12 @@ impl RequestObservation {
request_token: self.inner.request_token,
from_attempt_index: u64::from(attempt.ordinal.saturating_sub(1)),
reason: fallback_reason(&attempt.start_reason),
+ next_attempt_index: Some(u64::from(attempt.ordinal)),
+ next_attempt_token: self.attempt_token(&attempt.attempt_id),
+ next_binding_token: self
+ .inner
+ .context
+ .token(CorrelationDomain::Binding, &attempt.stable_binding_id),
}));
}
}
@@ -859,6 +876,14 @@ impl RequestObservation {
self.lock_state().accepted_attempt.is_some()
}
+ pub fn accepted_attempt_failed(&self) -> bool {
+ self.lock_state().accepted_attempt_failed
+ }
+
+ pub fn accepted_attempt_unknown(&self) -> bool {
+ self.lock_state().accepted_attempt_unknown
+ }
+
pub fn accepted_attempt_cancelled(&self) -> bool {
self.lock_state().accepted_attempt_cancelled
}
@@ -1063,11 +1088,24 @@ fn fallback_reason(start_reason: &str) -> FallbackReason {
return FallbackReason::OtherStableReason;
};
match class {
- "rate_limited" | "rate_limit_exceeded" | "too_many_requests" => FallbackReason::RateLimited,
+ "rate_limited"
+ | "rate_limit_exceeded"
+ | "too_many_requests"
+ | "quota"
+ | "binding_overload" => FallbackReason::RateLimited,
+ "credential" => FallbackReason::AuthenticationRejected,
+ "permanent_client" | "reasoning_history" => FallbackReason::InputRejected,
+ "timeout" => FallbackReason::Timeout,
+ "protocol" => FallbackReason::InvalidOutput,
"connector_unavailable" | "connector_not_ready" => FallbackReason::ConnectorUnavailable,
- "upstream_unavailable" | "upstream_error" | "provider_error" | "upstream_rejected" => {
- FallbackReason::UpstreamFailure
- }
+ "upstream_unavailable"
+ | "upstream_error"
+ | "provider_error"
+ | "upstream_rejected"
+ | "transient"
+ | "disconnect"
+ | "preoutput_stream"
+ | "unclassified" => FallbackReason::UpstreamFailure,
"candidate_excluded" => FallbackReason::CandidateExcluded,
_ => FallbackReason::OtherStableReason,
}
diff --git a/crates/gateway/src/observation/request/completion.rs b/crates/gateway/src/observation/request/completion.rs
index 4b825b99..eab33381 100644
--- a/crates/gateway/src/observation/request/completion.rs
+++ b/crates/gateway/src/observation/request/completion.rs
@@ -9,7 +9,8 @@ use hiroute_gateway_core::runtime::driver::{
};
use hiroute_diagnostics::event::{
- AttemptEnd, DiagnosticEvent, ModelStageKind, RequestCancel, RequestTimeout,
+ AttemptEnd, AttemptWireCommits, DiagnosticEvent, ModelStageKind, RequestCancel, RequestTimeout,
+ WireCommitState, WireHttpProtocol, WireModelResult, WireProviderError,
};
use super::{
@@ -67,7 +68,7 @@ impl RequestObservation {
&self,
disposition: &PublishedDisposition,
) {
- if !self.is_enabled() {
+ if !self.tracks_attempts() {
return;
}
{
@@ -86,7 +87,7 @@ impl RequestObservation {
failure: &AttemptFailureFacts,
disposition: Disposition,
) {
- if !self.is_enabled() {
+ if !self.tracks_attempts() {
return;
}
let error_class = failure
@@ -108,7 +109,7 @@ impl RequestObservation {
observation: &CompletedAttemptObservation,
stable_binding_id: Option<&str>,
) {
- if !self.is_enabled() {
+ if !self.tracks_attempts() {
return;
}
let promoted = stable_binding_id.and_then(|stable_binding_id| {
@@ -165,6 +166,14 @@ impl RequestObservation {
self.usage_fact(attempt, usage);
}
+ let model_failed = provider
+ .and_then(|facts| facts.model_event.as_ref())
+ .is_some_and(|event| {
+ matches!(event.as_str(), "response_failed" | "response_incomplete")
+ });
+ let model_unknown = provider
+ .and_then(|facts| facts.model_event.as_ref())
+ .is_some_and(|event| event.as_str() == "response_unknown");
if observation.disposition == Disposition::Accept && accepted {
let attempt = {
let mut state = self.lock_state();
@@ -173,6 +182,8 @@ impl RequestObservation {
None
} else {
state.accepted_attempt_finished = true;
+ state.accepted_attempt_failed = model_failed;
+ state.accepted_attempt_unknown = model_unknown;
state.accepted_attempt_cancelled =
observation.downstream == AttemptDownstreamOutcome::Cancelled;
state.accepted_attempt.clone()
@@ -180,6 +191,9 @@ impl RequestObservation {
};
if let Some(attempt) = attempt {
let (outcome, error_class, retryable) = match observation.downstream {
+ AttemptDownstreamOutcome::Completed if model_failed => {
+ ("rejected", Some("provider_rejected"), Some(false))
+ }
AttemptDownstreamOutcome::Completed => ("accepted", None, None),
AttemptDownstreamOutcome::Failed => (
"postcommit_transport_failed",
@@ -210,7 +224,13 @@ impl RequestObservation {
let mut state = self.lock_state();
state.published_disposition = None;
let attempt = state.current_attempt.take();
- if observation.disposition == Disposition::Continue
+ // A group's final failure can be Terminate even when the frozen request
+ // plan continues into another group. Cache the last real attempt here;
+ // a fallback event is emitted only if another real attempt starts.
+ if matches!(
+ observation.disposition,
+ Disposition::Continue | Disposition::Terminate
+ ) && observation.commits.downstream_semantic.is_clear()
&& let Some(attempt) = &attempt
{
state.previous_attempt_id = Some(attempt.attempt_id.clone());
@@ -449,7 +469,36 @@ impl RequestObservation {
});
let timed_out = transport.and_then(|facts| facts.timeout).is_some();
let commit = commit_state(observation);
+ let (prepared_wire, provider_error, upstream_request_token) = {
+ let mut state = self.lock_state();
+ (
+ state.prepared_wire.take(),
+ state.provider_error,
+ state
+ .response_wire
+ .as_ref()
+ .and_then(|wire| wire.upstream_request_token),
+ )
+ };
if let Some(outcome) = attempt_outcome(outcome, timed_out) {
+ let provider_result = provider
+ .and_then(|facts| facts.model_event.as_ref())
+ .and_then(|event| match event.as_str() {
+ "response_complete" => Some(WireModelResult::Complete),
+ "response_failed" => Some(WireModelResult::Failed),
+ "response_incomplete" => Some(WireModelResult::Incomplete),
+ "response_unknown" => Some(WireModelResult::Unknown),
+ _ => None,
+ });
+ let provider_error = provider_error.or(match provider_result {
+ Some(WireModelResult::Incomplete) => Some(WireProviderError::InvalidOutput),
+ Some(WireModelResult::Failed) => Some(WireProviderError::Unknown),
+ _ => None,
+ });
+ let retain_controls = outcome != hiroute_diagnostics::event::AttemptOutcome::Completed
+ || provider_result == Some(WireModelResult::Unknown)
+ || self.inner.context.handle().level()
+ == Some(hiroute_diagnostics::DiagnosticLevel::Debug);
self.emit_diagnostic(DiagnosticEvent::AttemptEnd(AttemptEnd {
reasoning_fields_removed: attempt.reasoning_fields_removed,
attempt_token: self.attempt_token(&attempt.attempt_id),
@@ -459,6 +508,32 @@ impl RequestObservation {
http_status: provider
.and_then(|facts| facts.http_status)
.map(|status| status.as_u16()),
+ http_protocol: transport.and_then(|facts| facts.upstream_protocol).map(
+ |protocol| match protocol {
+ hiroute_gateway_core::transport::HttpProtocol::Http1 => {
+ WireHttpProtocol::Http1
+ }
+ hiroute_gateway_core::transport::HttpProtocol::Http2 => {
+ WireHttpProtocol::Http2
+ }
+ },
+ ),
+ provider_error,
+ provider_result,
+ native_model: prepared_wire
+ .as_ref()
+ .filter(|_| retain_controls)
+ .and_then(|wire| wire.native_model.clone()),
+ request_reasoning: prepared_wire
+ .as_ref()
+ .filter(|_| retain_controls)
+ .and_then(|wire| wire.request_reasoning.clone()),
+ upstream_request_token,
+ commits: observation.map(|value| AttemptWireCommits {
+ upstream_request: diagnostic_fence(value.commits.upstream_request),
+ downstream_headers: diagnostic_fence(value.commits.downstream_headers),
+ downstream_body: diagnostic_fence(value.commits.downstream_semantic),
+ }),
}));
}
if let Some(facts) = transport {
@@ -494,6 +569,14 @@ impl RequestObservation {
}
}
+fn diagnostic_fence(fence: CommitFence) -> WireCommitState {
+ match fence {
+ CommitFence::Clear => WireCommitState::Clear,
+ CommitFence::WriteConfirmed => WireCommitState::Committed,
+ CommitFence::WriteStartedMayHaveCommitted => WireCommitState::Poisoned,
+ }
+}
+
fn provider_facts(
observation: &CompletedAttemptObservation,
) -> Option<&hiroute_gateway_core::runtime::driver::ProviderClassificationFacts> {
diff --git a/crates/gateway/src/observation/request/diagnostic.rs b/crates/gateway/src/observation/request/diagnostic.rs
new file mode 100644
index 00000000..e2f3e634
--- /dev/null
+++ b/crates/gateway/src/observation/request/diagnostic.rs
@@ -0,0 +1,87 @@
+//! Typed safe facts from the actual provider request and response boundaries.
+
+use super::RequestObservation;
+use crate::server::core_runtime::observation::wire_diagnostic;
+use hiroute_diagnostics::{correlation::CorrelationDomain, event::DiagnosticEvent};
+
+impl RequestObservation {
+ pub(in crate::server::core_runtime::observation) fn wire_diagnostic(
+ &self,
+ mut event: hiroute_diagnostics::event::UpstreamWire,
+ ) {
+ event.request_token = self.inner.context.token(
+ CorrelationDomain::ModelRequest,
+ &self.inner.metadata.request_id,
+ );
+ let state = self.lock_state();
+ let attempt = state
+ .accepted_attempt
+ .as_ref()
+ .or(state.current_attempt.as_ref())
+ .or(state.pending_attempt.as_ref());
+ event.attempt_index = Some(u64::from(
+ attempt
+ .map(|a| a.ordinal)
+ .filter(|ordinal| *ordinal != 0)
+ .unwrap_or(state.next_attempt_ordinal),
+ ));
+ event.attempt_token = attempt
+ .filter(|a| !a.attempt_id.is_empty())
+ .and_then(|a| self.attempt_token(&a.attempt_id));
+ drop(state);
+ self.emit_diagnostic(DiagnosticEvent::UpstreamWire(event));
+ }
+
+ pub(in crate::server::core_runtime::observation) fn response_head_diagnostic(
+ &self,
+ headers: &http::HeaderMap,
+ status: u16,
+ ) {
+ let event = wire_diagnostic::response(headers, status, &self.inner.context);
+ if let Some(error) = event.provider_error {
+ self.lock_state().provider_error = Some(error);
+ }
+ self.lock_state().response_wire = Some(event.clone());
+ self.wire_diagnostic(event);
+ }
+
+ pub(crate) fn provider_failure_diagnostic(
+ &self,
+ http_status: Option<u16>,
+ error: hiroute_diagnostics::event::WireProviderError,
+ ) {
+ use hiroute_diagnostics::event::UpstreamWirePhase;
+ self.lock_state().provider_error = Some(error);
+ let mut event = wire_diagnostic::response(
+ &http::HeaderMap::new(),
+ http_status.unwrap_or(0),
+ &self.inner.context,
+ );
+ event.phase = UpstreamWirePhase::Failure;
+ event.http_status = http_status;
+ event.provider_error = Some(error);
+ let state = self.lock_state();
+ if let Some(prepared) = &state.prepared_wire {
+ event.native_model = prepared.native_model.clone();
+ event.request_reasoning = prepared.request_reasoning.clone();
+ }
+ event.upstream_request_token = state
+ .response_wire
+ .as_ref()
+ .and_then(|wire| wire.upstream_request_token);
+ drop(state);
+ self.wire_diagnostic(event);
+ }
+
+ pub(crate) fn prepared_request_diagnostic(
+ &self,
+ headers: &http::HeaderMap,
+ serialized_template: &[u8],
+ ) {
+ if self.inner.context.handle().level().is_some() {
+ let event = wire_diagnostic::request(headers, serialized_template);
+ self.lock_state().prepared_wire = Some(event.clone());
+ self.wire_diagnostic(event);
+ }
+ }
+}
diff --git a/crates/gateway/src/observation/request/planner.rs b/crates/gateway/src/observation/request/planner.rs
index fdfbb942..e95add88 100644
--- a/crates/gateway/src/observation/request/planner.rs
+++ b/crates/gateway/src/observation/request/planner.rs
@@ -11,7 +11,7 @@ use super::{CandidateObservation, RequestObservation};
impl RequestObservation {
pub fn record_planner(&self, input: &PlannerInputV1, output: &PlannerOutputV1) {
- if !self.is_enabled() {
+ if !self.tracks_attempts() {
return;
}
let plan_id = match &output.identity {
diff --git a/crates/gateway/src/observation/tests.rs b/crates/gateway/src/observation/tests.rs
index d3669346..eaeeb35e 100644
--- a/crates/gateway/src/observation/tests.rs
+++ b/crates/gateway/src/observation/tests.rs
@@ -2,7 +2,7 @@ use serde_json::{Value, json};
use hiroute_diagnostics::runtime::DiagnosticsPort;
-use crate::ports::{ProbeLeaseOutcome, RuntimeStateKey};
+use crate::ports::{ProbeLeaseOutcome, RuntimeStateEntry, RuntimeStateKey};
use hiroute_gateway_core::runtime::attempt::{
AttemptGeneration, AttemptId, Disposition, PublishedDisposition, RequestId,
};
@@ -134,6 +134,14 @@ fn request(policy: OtelContentPolicy) -> RequestObservation {
fn request_with_diagnostics(
policy: OtelContentPolicy,
diagnostics: DiagnosticsPort,
+) -> RequestObservation {
+ request_with_capture(policy, diagnostics, true)
+}
+
+fn request_with_capture(
+ policy: OtelContentPolicy,
+ diagnostics: DiagnosticsPort,
+ enabled: bool,
) -> RequestObservation {
let gateway = GatewayObservation::with_sinks_and_policy(
true,
@@ -143,7 +151,7 @@ fn request_with_diagnostics(
);
RequestObservation::new(
super::request::RequestObservationCapture {
- enabled: true,
+ enabled,
content: true,
},
gateway.key,
@@ -175,6 +183,70 @@ fn request_with_diagnostics(
)
}
+#[test]
+fn info_failure_keeps_actual_reasoning_and_safe_provider_attribution_without_content_capture() {
+ use hiroute_diagnostics::{
+ DiagnosticLevel,
+ event::{ProcessRole, WireProviderError},
+ record::Component,
+ runtime::{DiagnosticRuntime, RuntimeConfig},
+ };
+ for enabled in [true, false] {
+ let root = std::env::temp_dir().join(format!(
+ "hiroute-info-wire-{}-{enabled}-{}",
+ std::process::id(),
+ super::unix_nanos()
+ ));
+ let runtime = DiagnosticRuntime::start(RuntimeConfig {
+ root: root.clone(),
+ role: ProcessRole::Daemon,
+ component: Component::Gateway,
+ parent_session_id: None,
+ level_override: Some(DiagnosticLevel::Info),
+ });
+ let request = request_with_capture(OtelContentPolicy::Disabled, runtime.port(), enabled);
+ assert_eq!(request.captures_content(), enabled);
+ assert_eq!(request.is_enabled(), enabled);
+ request.no_credential_materialized("binding:test", "credential/none/test");
+ request.prepared_request_diagnostic(&http::HeaderMap::new(), br#"{"model":"qwen3.6-flash","reasoning":{"effort":"high"},"thinking":{"type":"enabled","budget_tokens":81920},"input":"private-prompt-marker"}"#);
+ let mut headers = http::HeaderMap::new();
+ headers.insert("x-request-id", "private-provider-id".parse().unwrap());
+ request.response_head_diagnostic(&headers, 400);
+ request.provider_failure_diagnostic(Some(400), WireProviderError::ThinkingBudgetRejected);
+ request.disposition_published(&PublishedDisposition {
+ request_id: RequestId(1),
+ attempt_id: AttemptId(1),
+ generation: AttemptGeneration(1),
+ disposition: Disposition::Continue,
+ });
+ request.finish("failed");
+ runtime.shutdown();
+ let log = std::fs::read_to_string(root.join("daemon/current.jsonl")).unwrap();
+ assert!(
+ log.contains("thinking_budget_rejected") && log.contains("qwen3.6-flash"),
+ "{log}"
+ );
+ assert!(
+ log.contains("\"responses_effort\":\"high\"")
+ && log.contains("\"messages_budget_tokens\":81920"),
+ "{log}"
+ );
+ assert!(
+ log.contains("upstream_request_token") && log.contains("\"outcome\":\"failed\""),
+ "{log}"
+ );
+ assert!(
+ !log.contains("private-prompt-marker") && !log.contains("private-provider-id"),
+ "{log}"
+ );
+ assert!(
+ !log.contains("\"phase\":\"request\"") && !log.contains("\"phase\":\"response\""),
+ "{log}"
+ );
+ std::fs::remove_dir_all(root).unwrap();
+ }
+}
+
/// Drives the real observation branches that production runs: candidate staging, the
/// published accept, the first accepted frame and the terminal request, and asserts that
/// the diagnostic sink receives typed correlated events instead of raw identities.
@@ -212,6 +284,16 @@ fn request_lifecycle_records_typed_diagnostics_without_raw_identities() {
);
request.record_plan_stage(std::time::Duration::from_millis(4));
request.no_credential_materialized("binding:test", "credential/none/source-local-test");
+ let mut wire_headers = http::HeaderMap::new();
+ wire_headers.insert(
+ "x-request-id",
+ "provider-secret-request-id".parse().unwrap(),
+ );
+ request.prepared_request_diagnostic(
+ &wire_headers,
+ br#"{"model":"native-model","reasoning":{"effort":"low"},"input":"secret-prompt-marker"}"#,
+ );
+ request.response_head_diagnostic(&wire_headers, 200);
use hiroute_diagnostics::event::ReasoningCleanupReason;
request.reasoning_cleanup(ReasoningCleanupReason::ContextBreakRetry, 2, Some(3));
request.reasoning_cleanup(ReasoningCleanupReason::SuccessfulPrefixReuse, 0, Some(3));
@@ -257,6 +339,7 @@ fn request_lifecycle_records_typed_diagnostics_without_raw_identities() {
"request_end",
"model_stage",
"response_failure",
+ "upstream_wire",
] {
assert!(
log.contains(&format!("\"{kind}\":")),
@@ -264,6 +347,12 @@ fn request_lifecycle_records_typed_diagnostics_without_raw_identities() {
);
}
assert!(log.contains("\"attempt_index\":1"), "{log}");
+ assert!(log.contains("\"native_model\":\"native-model\""), "{log}");
+ assert!(log.contains("\"responses_effort\":\"low\""), "{log}");
+ assert!(
+ !log.contains("provider-secret-request-id") && !log.contains("secret-prompt-marker"),
+ "{log}"
+ );
assert!(log.contains("\"outcome\":\"completed\""), "{log}");
assert!(log.contains("\"stage\":\"parse\""), "{log}");
assert!(log.contains("\"state\":\"semantic_committed\""), "{log}");
@@ -375,6 +464,7 @@ fn accepted_client_cancellation_finishes_the_request_as_cancelled() {
failure: None,
transport: AttemptTransportFacts {
started_at: now,
+ upstream_protocol: Some(hiroute_gateway_core::transport::HttpProtocol::Http1),
connect_elapsed: None,
request_write_elapsed: None,
upstream_ttfb: None,
@@ -405,6 +495,8 @@ fn accepted_client_cancellation_finishes_the_request_as_cancelled() {
.lines()
.map(|line| serde_json::from_str::<Value>(line).unwrap())
.collect::<Vec<_>>();
+ assert!(log.contains("\"http_protocol\":\"http1\""), "{log}");
+ assert!(log.contains("\"downstream_body\":\"committed\""), "{log}");
for kind in ["attempt_end", "request_end"] {
assert!(
events
@@ -1114,6 +1206,86 @@ fn mapper_and_envelope_versions_are_explicit() {
}
}
+#[test]
+fn native_alias_repeated_reads_preserve_the_staged_relay_identity() {
+ let request = request(OtelContentPolicy::Disabled);
+ let digest = hiroute_domain::CanonicalDigest::of_bytes(b"native-profile");
+ let native_alias =
+ crate::runtime::native_endpoint_state_key("binding:test", digest.as_str()).unwrap();
+ let candidate = super::request::CandidateObservation {
+ candidate_id: "candidate:test".into(),
+ stable_binding_id: "binding:test".into(),
+ declared_order: 0,
+ profile_digest: digest.to_string(),
+ provider_name: "provider:test".into(),
+ request_model: "native:test".into(),
+ upstream_protocol: "responses".into(),
+ model_configuration_id: "model:test".into(),
+ adapter_revision: "adapter:test@1".into(),
+ effective_cost_micros: None,
+ cost_class: "unknown".into(),
+ protocol_profile: None,
+ streaming: true,
+ };
+ {
+ let mut state = request.lock_state();
+ state
+ .candidates
+ .insert(native_alias.clone(), candidate.clone());
+ state
+ .candidates
+ .insert("binding:test".into(), candidate.clone());
+ state.candidates.insert(
+ "binding:other".into(),
+ super::request::CandidateObservation {
+ stable_binding_id: "binding:other".into(),
+ ..candidate
+ },
+ );
+ state.previous_attempt_id = Some("attempt:previous".into());
+ state.next_attempt_reason = Some("protocol".into());
+ }
+ let key = RuntimeStateKey::credential(native_alias.as_str(), "credential:test", "key:test", 4);
+ let active = RuntimeStateEntry::default();
+ request.runtime_state_read(&key, Some(&active), "active");
+ request.runtime_state_read(&key, Some(&active), "active");
+ // A logical-key read is the same candidate as its native state alias.
+ request.runtime_state_read(
+ &RuntimeStateKey::credential("binding:test", "credential:test", "key:test", 4),
+ Some(&active),
+ "active",
+ );
+ {
+ let state = request.lock_state();
+ let pending = state.pending_attempt.as_ref().unwrap();
+ assert_eq!(pending.stable_binding_id, "binding:test");
+ assert_eq!(
+ pending.previous_attempt_id.as_deref(),
+ Some("attempt:previous")
+ );
+ assert_eq!(pending.start_reason, "protocol");
+ assert_eq!(pending.credential_ref, "credential:test");
+ assert_eq!(pending.key_id, "key:test");
+ assert_eq!(pending.credential_generation, 4);
+ assert_eq!(state.next_attempt_ordinal, 1);
+ }
+ // Sharing a credential does not collapse a genuinely different binding.
+ request.runtime_state_read(
+ &RuntimeStateKey::credential("binding:other", "credential:test", "key:test", 4),
+ Some(&active),
+ "active",
+ );
+ assert_eq!(
+ request
+ .lock_state()
+ .pending_attempt
+ .as_ref()
+ .unwrap()
+ .stable_binding_id,
+ "binding:other"
+ );
+}
+
#[test]
fn acquired_probe_stages_and_authoritative_publication_starts_the_attempt() {
let request = request(OtelContentPolicy::Disabled);
diff --git a/crates/gateway/src/runtime/driver/response.rs b/crates/gateway/src/runtime/driver/response.rs
index 696dfb20..3bdfa70f 100644
--- a/crates/gateway/src/runtime/driver/response.rs
+++ b/crates/gateway/src/runtime/driver/response.rs
@@ -1,3 +1,6 @@
+#[path = "response_prefix.rs"]
+mod prefix;
+
use super::response_diagnostics;
use hiroute_diagnostics::event::{
ResponseFailureReason as FailureReason, ResponseFailureStage as FailureStage,
@@ -107,28 +110,15 @@ pub(super) fn classify_precommit(
.map_err(|_| Arc::from(MATERIALIZATION_PROTOCOL_FAILED))?,
);
}
- if state.streaming && status.is_success() {
- // HTTP acceptance, not the arrival of a recognized semantic event,
- // closes the transparent retry window. Body parsing stays bounded
- // and executes only on the accepted response path.
- return Ok(PrecommitClassification::classified(
- ClassifiedAttemptResult {
- facts: ProviderClassificationFacts {
- http_status: Some(status),
- readiness: label("response_headers"),
- retryability: RetryabilityFact::NonRetryable,
- ..ProviderClassificationFacts::default()
- },
- readiness: take_readiness(state, status, "text/event-stream", false)?,
- },
- ));
- }
Ok(PrecommitClassification::pending())
}
PrecommitEvent::Body(bytes) => {
let status = state
.response_status
.ok_or_else(|| Arc::from("provider body arrived before final response head"))?;
+ if state.streaming && status.is_success() {
+ return prefix::classify_stream_prefix(state, bytes.bytes(), false);
+ }
if state.native_output && status.is_success() {
let projected = state
.projector
@@ -170,6 +160,9 @@ pub(super) fn classify_precommit(
let status = state
.response_status
.ok_or_else(|| Arc::from("provider ended before final response head"))?;
+ if state.streaming && status.is_success() {
+ return prefix::classify_stream_prefix(state, &[], true);
+ }
if state.native_output && status.is_success() {
let units = match state
.projector
@@ -198,6 +191,13 @@ pub(super) fn classify_precommit(
if let Some(error) = failure {
return classify_model_error(state, error, status);
}
+ if known_terminal_failure(state.semantic_terminal) {
+ return classify_state_failure(
+ state,
+ RawAttemptFailure::Protocol,
+ StatusCode::BAD_GATEWAY,
+ );
+ }
if !state.semantic_seen || state.semantic_terminal.is_none() {
return classify_state_failure(
state,
@@ -240,8 +240,24 @@ pub(super) fn classify_precommit(
}
};
state.semantic_terminal = semantic_terminal_for_response(&decoded.response);
- if let Some(error) = decoded.response.error.clone() {
- return classify_model_error(state, error, status);
+ if known_terminal_failure(state.semantic_terminal) {
+ let mut classified = if let Some(error) = decoded.response.error.clone() {
+ classify_model_error(state, error, status)?
+ } else {
+ classify_state_failure(
+ state,
+ RawAttemptFailure::Protocol,
+ StatusCode::BAD_GATEWAY,
+ )?
+ };
+ // Nonstream decoding consumes the decoder before rendering.
+ // Preserve its reported usage even when no client body is built.
+ if !decoded.response.usage.is_empty()
+ && let Some(result) = classified.classified.as_mut()
+ {
+ result.facts.usage = Some(model_usage_fact(&decoded.response.usage));
+ }
+ return Ok(classified);
}
if !decoded
.events
@@ -326,6 +342,16 @@ pub(super) fn finalize_attempt_facts(
.and_then(|readiness| readiness.semantic_terminal)
.or(state.semantic_terminal)
{
+ // A prebody failure keeps the classification that authorized relay.
+ // Only a failure discovered after acceptance closes retryability here.
+ if matches!(
+ outcome,
+ SemanticTerminalOutcome::Failed | SemanticTerminalOutcome::Incomplete
+ ) && state.classified_failure.is_none()
+ {
+ facts.error_class = Some(label("provider_rejected"));
+ facts.retryability = RetryabilityFact::NonRetryable;
+ }
facts.model_event = Some(label(match outcome {
SemanticTerminalOutcome::Complete => "response_complete",
SemanticTerminalOutcome::Incomplete => "response_incomplete",
@@ -611,6 +637,16 @@ fn sanitized_provider_kind(
return None;
}
let code = error.code.as_deref()?.to_ascii_lowercase();
+ // Preserve a precise cause without guessing one for generic InvalidParameter.
+ // Relay eligibility is independent of this diagnostic classification.
+ if code == "invalidparameter"
+ && error
+ .message
+ .as_deref()
+ .is_some_and(reasoning_budget_rejected)
+ {
+ return Some(ProviderFailureKind::Protocol);
+ }
if code == "invalid_encrypted_content" {
return Some(ProviderFailureKind::ReasoningHistory);
}
@@ -653,6 +689,20 @@ fn sanitized_provider_kind(
}
}
+fn reasoning_budget_rejected(message: &str) -> bool {
+ const PREFIX: &str =
+ "The thinking_budget parameter must be a positive integer and not greater than ";
+ message
+ .strip_prefix("<400> InternalError.Algo.InvalidParameter: ")
+ .unwrap_or(message)
+ .strip_prefix(PREFIX)
+ .is_some_and(|limit| {
+ !limit.is_empty()
+ && limit.len() <= 10
+ && limit.bytes().all(|byte| byte.is_ascii_digit())
+ })
+}
+
fn sealed_connector_semantics(profile: &CandidateProtocolProfile) -> bool {
profile.schema_version == "hiroute.candidate-protocol-profile/v1"
&& profile.capability.schema_version == "hiroute.candidate-capability/v1"
@@ -673,6 +723,7 @@ fn classify_model_error(
error: ModelError,
response_status: StatusCode,
) -> Result<PrecommitClassification<ProductionReadiness, ProductionDecodedSse>, Arc<str>> {
+ response_diagnostics::model_error(&error, Some(response_status.as_u16()));
let kind = sanitized_provider_kind(&state.profile, &error);
let status = error.status.unwrap_or(response_status.as_u16());
let raw = RawAttemptFailure::Http {
@@ -769,7 +820,8 @@ fn project_native_chunk_readiness(
if terminal {
return Err(Arc::from("native stream emitted bytes after terminal"));
}
- if unit.failure.is_some() {
+ if let Some(error) = &unit.failure {
+ response_diagnostics::model_error(error, Some(readiness.response_status.as_u16()));
response_diagnostics::note(
FailureStage::ProviderStream,
FailureReason::ProviderRejected,
@@ -852,7 +904,8 @@ fn decode_stream_chunk_readiness(
loop {
for event in decoder.take_events() {
observe_semantic_terminal(&mut readiness.semantic_terminal, &event.event);
- if matches!(event.event, ModelEvent::ResponseFailed { .. }) {
+ if let ModelEvent::ResponseFailed { error } = &event.event {
+ response_diagnostics::model_error(error, Some(readiness.response_status.as_u16()));
response_diagnostics::note(
FailureStage::ProviderStream,
FailureReason::ProviderRejected,
@@ -906,6 +959,13 @@ fn finish_native_stream_on_terminal(
Ok(())
}
+fn known_terminal_failure(terminal: Option<SemanticTerminalOutcome>) -> bool {
+ matches!(
+ terminal,
+ Some(SemanticTerminalOutcome::Failed | SemanticTerminalOutcome::Incomplete)
+ )
+}
+
fn semantic_terminal_for_response(response: &ModelResponseIRV1) -> Option<SemanticTerminalOutcome> {
if response.error.is_some() {
Some(SemanticTerminalOutcome::Failed)
diff --git a/crates/gateway/src/runtime/driver/response_diagnostics.rs b/crates/gateway/src/runtime/driver/response_diagnostics.rs
index eaecf4d2..583223dc 100644
--- a/crates/gateway/src/runtime/driver/response_diagnostics.rs
+++ b/crates/gateway/src/runtime/driver/response_diagnostics.rs
@@ -1,5 +1,10 @@
-use crate::server::core_runtime::{adapters::ProtocolAdapterError, model_ir::ModelIrError};
-use hiroute_diagnostics::event::{ResponseFailureReason as Reason, ResponseFailureStage as Stage};
+use crate::server::core_runtime::{
+ adapters::ProtocolAdapterError,
+ model_ir::{ModelError, ModelIrError},
+};
+use hiroute_diagnostics::event::{
+ ResponseFailureReason as Reason, ResponseFailureStage as Stage, WireProviderError,
+};
use std::sync::Arc;
pub(super) fn note(stage: Stage, reason: Reason) {
@@ -8,6 +13,56 @@ pub(super) fn note(stage: Stage, reason: Reason) {
}
}
+pub(super) fn model_error(error: &ModelError, response_status: Option<u16>) {
+ if let Some(observation) = crate::server::core_runtime::observation::active_request() {
+ observation.provider_failure_diagnostic(
+ error.status.or(response_status),
+ model_error_code(error, response_status),
+ );
+ }
+}
+
+fn model_error_code(error: &ModelError, response_status: Option<u16>) -> WireProviderError {
+ if let Some(message) = error.message.as_deref() {
+ const PREFIX: &str =
+ "The thinking_budget parameter must be a positive integer and not greater than ";
+ let message = message
+ .strip_prefix("<400> InternalError.Algo.InvalidParameter: ")
+ .unwrap_or(message);
+ if message.strip_prefix(PREFIX).is_some_and(|limit| {
+ !limit.is_empty()
+ && limit.len() <= 10
+ && limit.bytes().all(|byte| byte.is_ascii_digit())
+ }) {
+ return WireProviderError::ThinkingBudgetRejected;
+ }
+ if message == "Workspace endpoint is invalid." {
+ return WireProviderError::EndpointRejected;
+ }
+ }
+ match error.code.as_deref() {
+ Some(
+ "invalid_api_key" | "authentication_error" | "permission_denied" | "permission_error",
+ ) => WireProviderError::AuthenticationRejected,
+ Some(
+ "rate_limit_exceeded" | "insufficient_quota" | "rate_limit_error" | "overloaded_error",
+ ) => WireProviderError::RateLimited,
+ Some("server_error" | "internal_error" | "api_error") => {
+ WireProviderError::UpstreamUnavailable
+ }
+ Some("invalid_request_error" | "invalid_request" | "not_found_error") => {
+ WireProviderError::InputRejected
+ }
+ _ => match error.status.or(response_status) {
+ Some(401 | 403) => WireProviderError::AuthenticationRejected,
+ Some(429) => WireProviderError::RateLimited,
+ Some(400..=499) => WireProviderError::InputRejected,
+ Some(500..=599) => WireProviderError::UpstreamUnavailable,
+ _ => WireProviderError::Unknown,
+ },
+ }
+}
+
pub(super) fn adapter(stage: Stage, error: ProtocolAdapterError) -> Arc<str> {
note(stage, category(&error));
Arc::from("provider response processing failed")
@@ -34,6 +89,31 @@ fn category(error: &ProtocolAdapterError) -> Reason {
mod tests {
use super::*;
+ #[test]
+ fn model_errors_keep_safe_attribution_without_provider_text() {
+ let known = ModelError { status: Some(400), code: Some("InvalidParameter".into()), message: Some("<400> InternalError.Algo.InvalidParameter: The thinking_budget parameter must be a positive integer and not greater than 81920".into()), retryable: None };
+ assert_eq!(
+ model_error_code(&known, Some(200)),
+ WireProviderError::ThinkingBudgetRejected
+ );
+ let mut unknown = known;
+ unknown.message = Some("provider-secret-marker thinking_budget arbitrary detail".into());
+ assert_eq!(
+ model_error_code(&unknown, Some(200)),
+ WireProviderError::InputRejected
+ );
+ for (status, expected) in [
+ (401, WireProviderError::AuthenticationRejected),
+ (429, WireProviderError::RateLimited),
+ (503, WireProviderError::UpstreamUnavailable),
+ ] {
+ unknown.status = Some(status);
+ let actual = model_error_code(&unknown, Some(200));
+ assert_eq!(actual, expected);
+ assert!(!serde_json::to_string(&actual).unwrap().contains("secret"));
+ }
+ }
+
#[test]
fn diagnostic_categories_distinguish_local_limits_and_protocol_failures_without_payloads() {
for (error, expected) in [
diff --git a/crates/gateway/src/runtime/driver/response_prefix.rs b/crates/gateway/src/runtime/driver/response_prefix.rs
new file mode 100644
index 00000000..3d059642
--- /dev/null
+++ b/crates/gateway/src/runtime/driver/response_prefix.rs
@@ -0,0 +1,140 @@
+//! Hold upstream acceptance only until the first bounded, deliverable body unit.
+//! Headers are not evidence that an upstream can produce a usable response.
+use super::*;
+
+pub(super) fn classify_stream_prefix(
+ state: &mut ProductionAttemptState,
+ bytes: &[u8],
+ end_stream: bool,
+) -> Result<PrecommitClassification<ProductionReadiness, ProductionDecodedSse>, Arc<str>> {
+ let status = state.response_status.expect("stream prefix follows a head");
+ let failure = if state.native_output {
+ project_prefix(state, bytes, end_stream)
+ } else {
+ decode_prefix(state, bytes, end_stream)
+ };
+ match failure {
+ Ok(Some(error)) => return classify_model_error(state, error, status),
+ Err(_) => {
+ return classify_state_failure(
+ state,
+ RawAttemptFailure::Protocol,
+ StatusCode::BAD_GATEWAY,
+ );
+ }
+ Ok(None) => {}
+ }
+ if known_terminal_failure(state.semantic_terminal) {
+ return classify_state_failure(state, RawAttemptFailure::Protocol, StatusCode::BAD_GATEWAY);
+ }
+ let empty = state.prefix.as_ref().is_none_or(|prefix| prefix.is_empty());
+ if end_stream && empty {
+ return classify_state_failure(state, RawAttemptFailure::Protocol, StatusCode::BAD_GATEWAY);
+ }
+ // Native Responses certifies the terminal after its optional native tail.
+ // Do not publish acceptance while that terminal still cannot be delivered.
+ if empty
+ || state
+ .projector
+ .as_ref()
+ .is_some_and(|projector| projector.awaiting_responses_eof())
+ {
+ return Ok(PrecommitClassification::pending());
+ }
+ Ok(PrecommitClassification::classified(
+ ClassifiedAttemptResult {
+ facts: ProviderClassificationFacts {
+ http_status: Some(status),
+ readiness: label("response_body_ready"),
+ retryability: RetryabilityFact::NonRetryable,
+ ..ProviderClassificationFacts::default()
+ },
+ readiness: take_readiness(state, status, "text/event-stream", false)?,
+ },
+ ))
+}
+
+fn project_prefix(
+ state: &mut ProductionAttemptState,
+ bytes: &[u8],
+ end_stream: bool,
+) -> Result<Option<ModelError>, Arc<str>> {
+ let units = state
+ .projector
+ .as_mut()
+ .ok_or_else(|| Arc::from("native stream projector is unavailable"))?
+ .feed(bytes, end_stream)
+ .map_err(|error| response_diagnostics::adapter(FailureStage::NativeProjection, error))?;
+ for unit in units {
+ state.semantic_seen |= unit.semantic;
+ if let Some(outcome) = unit.terminal {
+ state.semantic_terminal = Some(native_terminal(outcome));
+ }
+ if let Some(error) = unit.failure {
+ state.semantic_terminal = Some(SemanticTerminalOutcome::Failed);
+ return Ok(Some(error));
+ }
+ push_native_prefix_unit(state, unit.bytes, unit.terminal.is_some())?;
+ }
+ Ok(None)
+}
+
+fn decode_prefix(
+ state: &mut ProductionAttemptState,
+ bytes: &[u8],
+ end_stream: bool,
+) -> Result<Option<ModelError>, Arc<str>> {
+ let decoder = state
+ .decoder
+ .as_mut()
+ .ok_or_else(|| Arc::from("native stream decoder is unavailable"))?;
+ let renderer = state
+ .renderer
+ .as_mut()
+ .ok_or_else(|| Arc::from("client stream renderer is unavailable"))?;
+ let mut status = decoder
+ .feed(bytes, end_stream)
+ .map_err(|error| response_diagnostics::adapter(FailureStage::Decode, error))?;
+ let mut output = Vec::new();
+ loop {
+ for event in decoder.take_events() {
+ state.semantic_seen |= event.is_semantic_output();
+ observe_semantic_terminal(&mut state.semantic_terminal, &event.event);
+ // Render even a failed unit into the private prefix to retain usage
+ // already reported in this chunk. Nothing is delivered until accept.
+ for rendered in renderer
+ .push(&event)
+ .map_err(|error| response_diagnostics::adapter(FailureStage::Render, error))?
+ {
+ output.extend_from_slice(
+ &rendered
+ .wire_bytes()
+ .map_err(|_| Arc::from("stream prefix serialization failed"))?,
+ );
+ }
+ if let ModelEvent::ResponseFailed { error } = event.event {
+ return Ok(Some(error));
+ }
+ }
+ note_response_reasoning_loss(renderer.take_reasoning_loss());
+ if status != adapters::ResponseDecodeStatus::NeedDrain {
+ break;
+ }
+ status = decoder
+ .resume()
+ .map_err(|_| Arc::from("stream prefix drain failed"))?;
+ }
+ let terminal = status == adapters::ResponseDecodeStatus::Terminal;
+ if terminal && !end_stream {
+ finish_native_stream_on_terminal(&mut state.decoder)?;
+ } else if end_stream {
+ state
+ .decoder
+ .take()
+ .expect("prefix decoder is present")
+ .finish()
+ .map_err(|_| Arc::from("stream prefix ended without terminal"))?;
+ }
+ push_native_prefix_unit(state, output, terminal || end_stream)?;
+ Ok(None)
+}
diff --git a/crates/gateway/src/runtime/driver/selection.rs b/crates/gateway/src/runtime/driver/selection.rs
index dd53d19b..d49772b6 100644
--- a/crates/gateway/src/runtime/driver/selection.rs
+++ b/crates/gateway/src/runtime/driver/selection.rs
@@ -497,6 +497,7 @@ mod tests {
}),
transport: AttemptTransportFacts {
started_at: now,
+ upstream_protocol: None,
connect_elapsed: None,
request_write_elapsed: None,
upstream_ttfb: None,
@@ -516,6 +517,7 @@ mod tests {
let realtime_facts = RealtimeRoutingFacts::default();
let transport = AttemptTransportFacts {
started_at: now,
+ upstream_protocol: None,
connect_elapsed: None,
request_write_elapsed: None,
upstream_ttfb: None,
@@ -608,6 +610,7 @@ mod tests {
&rejected,
&AttemptTransportFacts {
started_at: now,
+ upstream_protocol: None,
connect_elapsed: None,
request_write_elapsed: None,
upstream_ttfb: None,
@@ -632,6 +635,7 @@ mod tests {
&rejected,
&AttemptTransportFacts {
started_at: now,
+ upstream_protocol: None,
connect_elapsed: None,
request_write_elapsed: None,
upstream_ttfb: None,
@@ -664,6 +668,7 @@ mod tests {
};
let transport = AttemptTransportFacts {
started_at: now,
+ upstream_protocol: None,
connect_elapsed: None,
request_write_elapsed: None,
upstream_ttfb: None,
@@ -770,6 +775,7 @@ mod tests {
}),
transport: AttemptTransportFacts {
started_at: now,
+ upstream_protocol: None,
connect_elapsed: None,
request_write_elapsed: None,
upstream_ttfb: None,
@@ -1041,6 +1047,7 @@ mod tests {
failure: None,
transport: AttemptTransportFacts {
started_at: now,
+ upstream_protocol: None,
connect_elapsed: None,
request_write_elapsed: None,
upstream_ttfb: None,
diff --git a/crates/gateway/src/server.rs b/crates/gateway/src/server.rs
index 62ff6cca..7c778688 100644
--- a/crates/gateway/src/server.rs
+++ b/crates/gateway/src/server.rs
@@ -408,6 +408,17 @@ impl GatewayLauncher {
})
}
+ /// Attach safe local diagnostics before the standalone production listener starts.
+ pub fn with_diagnostics(
+ self,
+ diagnostics: hiroute_diagnostics::runtime::DiagnosticsPort,
+ ) -> Self {
+ if let GatewayMode::Production { runtime, .. } = &self.mode {
+ runtime.observation().set_diagnostics(diagnostics);
+ }
+ self
+ }
+
/// Starts the Pingora listener. The readiness probe and every product
/// request traverse this same `GatewayHttpApp`.
pub fn serve(self) -> Result<(), GatewayLauncherError> {
diff --git a/docs/code-map/architecture.md b/docs/code-map/architecture.md
index c166119d..5cd40a2f 100644
--- a/docs/code-map/architecture.md
+++ b/docs/code-map/architecture.md
@@ -38,7 +38,7 @@ The name must fit any runtime root that supports `hiroute/control.sock`, includi
| Display, prepare and save the selected source | [Safe Desktop projection](../../apps/desktop/src-tauri/src/bridge/model_connection_web.rs), [daemon preparation](../../crates/daemon/src/control/runtime/model_connections/pi_discovered.rs) | Provider/model identity remains visible; privileged scan material stays private; source changes invalidate the prepared candidate |
| Make a saved source usable by a plan | [Capability qualification](../../crates/application/src/compute_management/compilation.rs), [candidate materialization](../../crates/daemon/src/control/runtime/candidate_execution.rs), [compiler](../../crates/application/src/compiler) | Native-observed facts retain source-local authority and budgets; display/save alone does not prove route eligibility |
| Configure models and collaboration independently | [Shared settings transaction](../../crates/application/src/agent_connection/settings), [typed native facts](../../crates/daemon/src/control/runtime/settings_facts), [native provider guide](../../crates/integrations/src/agents/additional_native/README.md) | Native defaults, model grants, installed Skills and conditional restore have distinct owners |
-| Project native reasoning settings | [Wire types](../../crates/domain/src/routing/gateway_execution.rs), [daemon renderer](../../crates/daemon/src/control/runtime/candidate_protocol_profiles.rs), [Gateway reader](../../crates/gateway/src/profiles/reasoning.rs) | Keep one field/path/toggle definition; [materializer → compiler → Gateway tests](../../crates/daemon/src/control/runtime/candidate_execution_tests.rs) cover accepted aliases |
+| Project native reasoning settings | [Wire types](../../crates/domain/src/routing/gateway_execution.rs), [daemon renderer](../../crates/daemon/src/control/runtime/candidate_protocol_profiles.rs), [Gateway reader](../../crates/gateway/src/profiles/reasoning.rs) and [wire normalization](../../crates/gateway/src/adapters/request/reasoning.rs) | Keep one field/path/toggle definition; [materializer → compiler → Gateway tests](../../crates/daemon/src/control/runtime/candidate_execution_tests.rs) cover accepted aliases |
Native configuration precedence belongs to each reader. For example, Pi provider
`baseUrl` overrides inherited catalog addresses, while provider `api` defaults apply
@@ -69,6 +69,7 @@ Scan → Prepare → Save → restart → native use, including stale-source rej
| --- | --- | --- |
| 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 |
+| 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 |
| Search and gaps | [Query](../../crates/observation/src/query_v2/search.rs), [index worker](../../crates/observation/src/text_index/worker.rs), [status](../../crates/observation/src/query/status.rs) | Bounded scan windows, wraparound and explicit gaps distinguish incomplete search from an empty result |
| Business relations and visibility | [Relation owner](../../crates/observation/src/query_v2/relation.rs), [managed content](../../crates/observation/src/managed_text/README.md) | `query_v2` also writes relations; every content read checks current authorization and visibility |
| Native tool-history continuation | [Responses ingress](../../crates/gateway/src/adapters/responses_ingress.rs), [real continuation journey](../../tools/e2e-harness/tests/p0_gateway_protocol/continuation.rs) | Preserve model-generated empty tool names in native history without relaxing declarations/call IDs; unsafe conversion, ContextHold and reasoning cleanup stay disabled for that history |
diff --git a/docs/code-map/decision-foundation.md b/docs/code-map/decision-foundation.md
index ebfcde20..d91b6101 100644
--- a/docs/code-map/decision-foundation.md
+++ b/docs/code-map/decision-foundation.md
@@ -17,7 +17,7 @@ with no current runtime or product entry.
| Create a connection while editing a route | [DesktopApp](../../apps/desktop/src/product/DesktopApp.tsx), [selector](../../apps/desktop/src/features/decision-services/DecisionSelector.tsx) | Preserve route edits and select the exact saved revision without implicit publication |
| Configure categories, degree and competence | [Plan editor](../../apps/desktop/src/plan-editor.tsx), [branch editor](../../apps/desktop/src/features/decision-services/BranchRoutingEditor.tsx), [judgment controls](../../apps/desktop/src/features/decision-services/JudgmentSettings.tsx), [domain](../../crates/domain/src/routing/decision.rs) | Existing model picker, collapsed advanced settings, complete overrides and publish validation |
| Freeze connection, definition and rubric | [Authoring snapshot](../../crates/daemon/src/control/runtime/plan_content_snapshot.rs), [Application compiler](../../crates/application/src/compiler), [Gateway compiler](../../crates/gateway/src/publication/compiler.rs) | [Decision publication contracts](../../crates/gateway/src/publication/tests/decision_contracts.rs), [real publication/restart](../../crates/daemon/tests/publication_process.rs) |
-| Judge the current task and preceding actual stage | [Call lifetime](../../crates/gateway/src/core_runtime/classification.rs), [System One adapter](../../crates/gateway/src/core_runtime/classification/system_one.rs), [custom codec](../../crates/gateway/src/core_runtime/classification/protocol.rs) | [Provider mapping](../../crates/gateway/src/core_runtime/classification/system_one_tests.rs), [protocol/replay tests](../../crates/gateway/src/core_runtime/classification/protocol_tests.rs) |
+| Judge the current task and preceding actual stage | [Call lifetime](../../crates/gateway/src/core_runtime/classification.rs), [safe transport diagnostics](../../crates/gateway/src/core_runtime/classification/diagnostic.rs), [System One adapter](../../crates/gateway/src/core_runtime/classification/system_one.rs), [custom codec](../../crates/gateway/src/core_runtime/classification/protocol.rs) | [Provider mapping](../../crates/gateway/src/core_runtime/classification/system_one_tests.rs), [protocol/replay tests](../../crates/gateway/src/core_runtime/classification/protocol_tests.rs) |
| Select a group and execute candidates | [Group policy](../../crates/gateway/src/core_runtime/classification/group_policy.rs), [planner](../../crates/gateway/src/planner.rs), [history](../../crates/gateway/src/agent_turn_history/store.rs) | [Threshold/score isolation](../../crates/gateway/src/core_runtime/classification/group_policy_tests.rs), [group boundaries](../../crates/gateway/src/planning/branch_tests.rs), [multi-turn listener journey](../../tools/e2e-harness/tests/p0_gateway_runtime/decision_branches.rs) |
| Inspect actual execution and competence | [Quality query](../../crates/observation/src/query_v2/plan_quality.rs), [PlanQuality](../../apps/desktop/src/features/PlanQuality.tsx), [stage detail](../../apps/desktop/src/features/PlanQualityStage.tsx) | [Stage summaries](../../crates/observation/src/store/tests/plan_quality/summary.rs), [UI identities](../../apps/desktop/tests/plan-quality-state.test.mjs) |
diff --git a/tools/e2e-harness/tests/p0_gateway_context_hold.rs b/tools/e2e-harness/tests/p0_gateway_context_hold.rs
index b3cef8ec..3592bfa2 100644
--- a/tools/e2e-harness/tests/p0_gateway_context_hold.rs
+++ b/tools/e2e-harness/tests/p0_gateway_context_hold.rs
@@ -271,6 +271,12 @@ fn real_hirouted_holds_only_inside_each_classified_branch() {
"runtime.context_hold.responses_search_append"
]
);
+ // This scenario exercises a failure after output delivery. A coalesced
+ // delta+failure received before any downstream body is now relayable.
+ let failure_start = RESPONSES_FAILED_AFTER_SEMANTIC
+ .windows(b"event: response.failed".len())
+ .position(|part| part == b"event: response.failed")
+ .unwrap();
let simple = NativeProvider::start(vec![
complete(),
complete(),
@@ -286,9 +292,13 @@ fn real_hirouted_holds_only_inside_each_classified_branch() {
error_kind: None,
body: RESPONSES_INCOMPLETE,
},
- ProviderReply::StreamComplete {
+ ProviderReply::StreamDrip {
status: 200,
- body: RESPONSES_FAILED_AFTER_SEMANTIC,
+ chunks: vec![
+ RESPONSES_FAILED_AFTER_SEMANTIC[..failure_start].to_vec(),
+ RESPONSES_FAILED_AFTER_SEMANTIC[failure_start..].to_vec(),
+ ],
+ interval: std::time::Duration::from_secs(1),
},
]);
let complex = NativeProvider::start(vec![
@@ -298,7 +308,9 @@ fn real_hirouted_holds_only_inside_each_classified_branch() {
complete(),
complete(),
]);
- let fixture = RuntimeFixture::launch_classified(&[&simple, &complex], 2);
+ // Isolate success-only context holds from prebody fallback: known incomplete
+ // JSON must fail without delivery when the sole attempt is exhausted.
+ let fixture = RuntimeFixture::launch_classified(&[&simple, &complex], 1);
assert_eq!(
send(
@@ -449,10 +461,8 @@ fn real_hirouted_holds_only_inside_each_classified_branch() {
"incomplete-origin",
vec![message("user", "rename the partial readme")],
);
- assert_eq!(incomplete.status, 200);
- let incomplete_body: Value = serde_json::from_slice(&incomplete.body).unwrap();
- assert_eq!(incomplete_body["status"], "incomplete");
- assert_eq!(incomplete_body["output"][0]["status"], "incomplete");
+ assert_eq!(incomplete.status, 502);
+ assert!(!String::from_utf8_lossy(&incomplete.body).contains("partial"));
assert_eq!((simple.calls(), complex.calls()), (6, 3));
assert_eq!(
@@ -461,7 +471,6 @@ fn real_hirouted_holds_only_inside_each_classified_branch() {
"incomplete-origin",
vec![
message("user", "rename the partial readme"),
- message("assistant", "partial"),
message("user", "complex-route finish the architecture"),
],
)
@@ -558,7 +567,7 @@ fn real_hirouted_holds_only_inside_each_classified_branch() {
];
let chat_fixture = RuntimeFixture::launch_classified_with_publication_candidates(
&[&chat_simple, &chat_complex],
- 2,
+ 1,
&chat_candidates,
);
@@ -567,17 +576,14 @@ fn real_hirouted_holds_only_inside_each_classified_branch() {
"chat-length-origin",
vec![message("user", "rename until the token limit")],
);
- assert_eq!(length.status, 200);
- let length_body: Value = serde_json::from_slice(&length.body).unwrap();
- assert_eq!(length_body["status"], "incomplete");
- assert_eq!(length_body["output"][0]["status"], "incomplete");
+ assert_eq!(length.status, 502);
+ assert!(!String::from_utf8_lossy(&length.body).contains("partial"));
assert_eq!(
send(
&chat_fixture,
"chat-length-origin",
vec![
message("user", "rename until the token limit"),
- message("assistant", "partial"),
message("user", "complex-route continue after length"),
],
)
@@ -595,17 +601,14 @@ fn real_hirouted_holds_only_inside_each_classified_branch() {
"chat-refusal-origin",
vec![message("user", "rename content that will be refused")],
);
- assert_eq!(refusal.status, 200);
- let refusal_body: Value = serde_json::from_slice(&refusal.body).unwrap();
- assert_eq!(refusal_body["status"], "incomplete");
- assert_eq!(refusal_body["output"][0]["status"], "incomplete");
+ assert_eq!(refusal.status, 502);
+ assert!(!String::from_utf8_lossy(&refusal.body).contains("cannot comply"));
assert_eq!(
send(
&chat_fixture,
"chat-refusal-origin",
vec![
message("user", "rename content that will be refused"),
- message("assistant", "cannot comply"),
message("user", "complex-route recover after refusal"),
],
)
diff --git a/tools/e2e-harness/tests/p0_gateway_observation.rs b/tools/e2e-harness/tests/p0_gateway_observation.rs
index 6cdcb395..f12c1806 100644
--- a/tools/e2e-harness/tests/p0_gateway_observation.rs
+++ b/tools/e2e-harness/tests/p0_gateway_observation.rs
@@ -17,6 +17,324 @@ const ACCEPTED_STREAM_CREATED: &[u8] = b"event: response.created\ndata: {\"type\
const ACCEPTED_STREAM_TEXT: &[u8] = b"event: response.output_text.delta\ndata: {\"type\":\"response.output_text.delta\",\"item_id\":\"observation-message\",\"output_index\":0,\"content_index\":0,\"delta\":\"accepted-stream-22008\"}\n\n";
const ACCEPTED_STREAM_COMPLETED: &[u8] = b"event: response.completed\ndata: {\"type\":\"response.completed\",\"response\":{\"id\":\"observation-stream\",\"model\":\"native-observed\",\"status\":\"completed\",\"output\":[{\"type\":\"message\",\"id\":\"observation-message\",\"status\":\"completed\",\"role\":\"assistant\",\"content\":[{\"type\":\"output_text\",\"text\":\"accepted-stream-22008\",\"annotations\":[]}]}],\"usage\":{\"input_tokens\":13,\"output_tokens\":5,\"total_tokens\":18,\"input_tokens_details\":{\"cached_tokens\":4},\"output_tokens_details\":{\"reasoning_tokens\":3}}}}\n\n";
+#[test]
+fn production_info_cross_group_relay_links_only_actual_attempts() {
+ for relay_allowed in [true, false] {
+ let rejected = NativeProvider::start(vec![ProviderReply::StreamComplete {
+ status: 200,
+ body: br#"event: response.failed
+data: {"type":"response.failed","response":{"id":"cross-group-failure","model":"native-observed","status":"failed","output":[],"error":{"code":"InvalidParameter","message":"<400> InternalError.Algo.InvalidParameter: The thinking_budget parameter must be a positive integer and not greater than 81920"},"usage":{"input_tokens":7,"output_tokens":2}}}
+
+"#,
+ }]);
+ let accepted = NativeProvider::start(vec![ProviderReply::StreamComplete {
+ status: 200,
+ body: br#"event: response.completed
+data: {"type":"response.completed","response":{"id":"cross-group-success","model":"native-observed","status":"completed","output":[{"type":"message","id":"cross-group-message","status":"completed","role":"assistant","content":[{"type":"output_text","text":"accepted-cross-group-response","annotations":[]}]}],"usage":{"input_tokens":11,"output_tokens":7}}}
+
+"#,
+ }]);
+ let fixture = RuntimeFixture::launch_classified_with_info_diagnostics(
+ &[&rejected, &accepted],
+ if relay_allowed { 2 } else { 1 },
+ );
+ assert!(fixture.observation_root.is_none());
+ let response = fixture.request_body(
+ br#"{"model":"runtime-model","input":"private-cross-group-input","stream":true}"#,
+ );
+ assert_eq!(response.status, if relay_allowed { 200 } else { 502 });
+ assert_eq!(rejected.calls(), 1);
+ assert_eq!(accepted.calls(), usize::from(relay_allowed));
+ let path = fixture
+ .diagnostics_root
+ .as_ref()
+ .unwrap()
+ .join("daemon/current.jsonl");
+ let (log, records) =
+ wait_diagnostic_terminal(&path, if relay_allowed { "completed" } else { "failed" });
+ let attempts = records
+ .iter()
+ .filter_map(|record| record.pointer("/event/attempt_begin"))
+ .collect::<Vec<_>>();
+ let ended = records
+ .iter()
+ .filter_map(|record| record.pointer("/event/attempt_end"))
+ .collect::<Vec<_>>();
+ let relays = records
+ .iter()
+ .filter_map(|record| record.pointer("/event/fallback"))
+ .collect::<Vec<_>>();
+ assert_eq!(attempts.len(), if relay_allowed { 2 } else { 1 }, "{log}");
+ assert_eq!(ended.len(), attempts.len(), "{log}");
+ assert_eq!(ended[0]["native_model"], "runtime-native-model-1", "{log}");
+ assert_eq!(
+ ended[0]["request_reasoning"]["responses_effort"], "low",
+ "{log}"
+ );
+ assert_eq!(ended[0]["http_status"], 200, "{log}");
+ assert_eq!(ended[0]["provider_result"], "failed", "{log}");
+ assert_eq!(
+ ended[0]["provider_error"], "thinking_budget_rejected",
+ "{log}"
+ );
+ assert_eq!(ended[0]["commits"]["downstream_body"], "clear", "{log}");
+ assert_eq!(relays.len(), usize::from(relay_allowed), "{log}");
+ if relay_allowed {
+ let relay = relays[0];
+ assert_eq!(relay["reason"], "invalid_output", "{log}");
+ assert_eq!(
+ relay["from_attempt_index"], attempts[0]["attempt_index"],
+ "{log}"
+ );
+ assert_eq!(
+ relay["next_attempt_index"], attempts[1]["attempt_index"],
+ "{log}"
+ );
+ assert_eq!(
+ relay["request_token"], attempts[0]["request_token"],
+ "{log}"
+ );
+ assert_eq!(
+ relay["request_token"], attempts[1]["request_token"],
+ "{log}"
+ );
+ assert_eq!(
+ relay["next_attempt_token"], attempts[1]["attempt_token"],
+ "{log}"
+ );
+ assert_eq!(
+ relay["next_binding_token"], attempts[1]["binding_token"],
+ "{log}"
+ );
+ assert_ne!(
+ attempts[0]["binding_token"], attempts[1]["binding_token"],
+ "{log}"
+ );
+ assert_eq!(ended[1]["provider_result"], "complete", "{log}");
+ assert_eq!(ended[1]["outcome"], "completed", "{log}");
+ }
+ for private in [
+ "private-cross-group-input",
+ "provider-secret-1",
+ "<400> InternalError",
+ "accepted-cross-group-response",
+ ] {
+ assert!(
+ !log.contains(private),
+ "private content reached Info diagnostics"
+ );
+ }
+ }
+}
+
+#[test]
+fn production_info_failure_logs_actual_wire_and_body_relay_without_content_capture() {
+ let rejected = NativeProvider::start(vec![ProviderReply::CompleteWithRequestId {
+ status: 400,
+ request_id: "private-upstream-request-id",
+ body: br#"{"error":{"message":"<400> InternalError.Algo.InvalidParameter: The thinking_budget parameter must be a positive integer and not greater than 81920","type":"invalid_request_error"}}"#,
+ }]);
+ let accepted = NativeProvider::start(vec![ProviderReply::Complete {
+ status: 200,
+ error_kind: None,
+ body: br#"{"id":"info-ok","model":"native-observed","status":"completed","output":[{"type":"message","id":"info-message","status":"completed","role":"assistant","content":[{"type":"output_text","text":"accepted-info-response","annotations":[]}]}],"usage":{"input_tokens":11,"output_tokens":7,"total_tokens":18}}"#,
+ }]);
+ let fixture = RuntimeFixture::launch_with_info_diagnostics(&[&rejected, &accepted], 2);
+ assert!(fixture.observation_root.is_none());
+ let response = fixture.request_body(
+ br#"{"model":"runtime-model","input":"private-input-marker","stream":false}"#,
+ );
+ assert_eq!(response.status, 200);
+ assert_eq!((rejected.calls(), accepted.calls()), (1, 1));
+ assert!(String::from_utf8_lossy(&response.body).contains("accepted-info-response"));
+ let path = fixture
+ .diagnostics_root
+ .as_ref()
+ .unwrap()
+ .join("daemon/current.jsonl");
+ let (log, records) = wait_diagnostic_terminal(&path, "completed");
+ assert!(
+ records.iter().any(|record| {
+ record
+ .pointer("/event/level_applied/level")
+ .and_then(Value::as_str)
+ == Some("info")
+ }),
+ "{log}"
+ );
+ let failed = records
+ .iter()
+ .find_map(|record| {
+ let event = record.pointer("/event/attempt_end")?;
+ (event["outcome"] == "failed").then_some(event)
+ })
+ .expect("failed attempt must remain visible at Info");
+ assert_eq!(failed["native_model"], "runtime-native-model-1", "{log}");
+ assert_eq!(
+ failed["request_reasoning"]["responses_effort"], "low",
+ "{log}"
+ );
+ assert_eq!(failed["http_status"], 400, "{log}");
+ assert_eq!(failed["http_protocol"], "http1", "{log}");
+ assert_eq!(
+ failed["provider_error"], "thinking_budget_rejected",
+ "{log}"
+ );
+ assert_eq!(failed["provider_result"], "failed", "{log}");
+ assert!(failed["upstream_request_token"].as_str().is_some(), "{log}");
+ assert_eq!(failed["commits"]["downstream_headers"], "clear", "{log}");
+ assert_eq!(failed["commits"]["downstream_body"], "clear", "{log}");
+ let next = records
+ .iter()
+ .find_map(|record| record.pointer("/event/fallback"))
+ .expect("fallback must identify its next target");
+ assert_eq!(next["next_attempt_index"], 2, "{log}");
+ assert!(next["next_binding_token"].as_str().is_some(), "{log}");
+ assert!(
+ records.iter().any(|record| {
+ record.pointer("/event/attempt_end").is_some_and(|event| {
+ event["outcome"] == "completed"
+ && event["provider_error"].is_null()
+ && event["provider_result"] == "complete"
+ })
+ }),
+ "the successful fallback must not inherit the failed provider cause: {log}"
+ );
+ for private in [
+ "private-input-marker",
+ "private-upstream-request-id",
+ "provider-secret-1",
+ "<400> InternalError",
+ "accepted-info-response",
+ ] {
+ assert!(
+ !log.contains(private),
+ "private content reached Info diagnostics"
+ );
+ }
+ assert!(
+ !records.iter().any(|record| {
+ record
+ .pointer("/event/upstream_wire/phase")
+ .and_then(Value::as_str)
+ == Some("request")
+ }),
+ "Info failures must not enable successful request detail: {log}"
+ );
+}
+
+#[test]
+fn production_info_unknown_model_terminal_keeps_uncertainty_separate_from_failure() {
+ // Native forward compatibility preserves this future status, while its
+ // semantic result remains unknown despite HTTP 200.
+ let provider = NativeProvider::start(vec![ProviderReply::Complete {
+ status: 200,
+ error_kind: None,
+ body: br#"{"id":"unknown-info","model":"native-observed","status":"provider_future_status","output":[{"type":"message","id":"unknown-info-message","status":"completed","role":"assistant","content":[{"type":"output_text","text":"private-unknown-response","annotations":[]}]}],"usage":{"input_tokens":11,"output_tokens":7,"total_tokens":18}}"#,
+ }]);
+ let fixture = RuntimeFixture::launch_with_info_diagnostics(&[&provider], 1);
+ let response = fixture.request_body(
+ br#"{"model":"runtime-model","input":"private-unknown-input","stream":false}"#,
+ );
+ assert_eq!(response.status, 200);
+ assert_eq!(provider.calls(), 1);
+ let path = fixture
+ .diagnostics_root
+ .as_ref()
+ .unwrap()
+ .join("daemon/current.jsonl");
+ let (log, records) = wait_diagnostic_terminal(&path, "completed");
+ let terminal = records
+ .iter()
+ .find(|record| record.pointer("/event/attempt_end").is_some())
+ .expect("uncertain model result must have an attempt terminal");
+ assert_eq!(terminal["level"], "warn", "{log}");
+ let attempt = &terminal["event"]["attempt_end"];
+ assert_eq!(attempt["outcome"], "completed", "{log}");
+ assert_eq!(attempt["http_status"], 200, "{log}");
+ assert_eq!(attempt["provider_result"], "unknown", "{log}");
+ assert!(attempt["provider_error"].is_null(), "{log}");
+ assert_eq!(attempt["commits"]["downstream_body"], "committed", "{log}");
+ assert_eq!(attempt["native_model"], "runtime-native-model-1", "{log}");
+ assert_eq!(
+ attempt["request_reasoning"]["responses_effort"], "low",
+ "{log}"
+ );
+ assert!(!log.contains("private-unknown-input") && !log.contains("private-unknown-response"));
+ assert!(fixture.observation_root.is_none());
+}
+
+#[test]
+fn production_unknown_completion_retains_transport_usage_and_an_unknown_agent_turn() {
+ let provider = NativeProvider::start(vec![ProviderReply::Complete {
+ status: 200,
+ error_kind: None,
+ body: br#"{"id":"unknown-turn","model":"native-observed","status":"provider_future_status","output":[{"type":"message","id":"unknown-turn-message","status":"completed","role":"assistant","content":[{"type":"output_text","text":"unknown-turn-response","annotations":[]}]}],"usage":{"input_tokens":11,"output_tokens":7,"total_tokens":18}}"#,
+ }]);
+ let fallback = NativeProvider::start(vec![ProviderReply::Complete {
+ status: 200,
+ error_kind: None,
+ body: ACCEPTED_BODY,
+ }]);
+ let fixture = RuntimeFixture::launch_classified_with_observation(
+ &[&provider, &fallback],
+ 1,
+ ObservationFaults::healthy(),
+ );
+ let response = fixture
+ .request_body(br#"{"model":"runtime-model","input":"unknown-turn-input","stream":false}"#);
+ assert_eq!(response.status, 200);
+ assert_eq!(provider.calls(), 1);
+ assert_eq!(fallback.calls(), 0);
+ let facts = wait_complete_accepted_request(fixture.observation_root.as_deref().unwrap());
+ for (kind, field, expected) in [
+ ("request_finished", "outcome", "accepted"),
+ ("attempt_finished", "outcome", "accepted"),
+ ("agent_turn_finished", "status", "unknown"),
+ ] {
+ assert!(
+ facts
+ .iter()
+ .any(|row| { row["fact"]["kind"] == kind && row["fact"][field] == expected }),
+ "{kind} must retain its transport or semantic result: {facts:?}"
+ );
+ }
+ assert!(facts.iter().any(|row| {
+ row["fact"]["kind"] == "attempt_finished"
+ && row["fact"]["provider_model_event"] == "response_unknown"
+ && row["fact"]["error_class"].is_null()
+ }));
+ assert!(facts.iter().any(|row| {
+ row["fact"]["kind"] == "usage_and_cache"
+ && row["fact"]["input_tokens"] == 11
+ && row["fact"]["output_tokens"] == 7
+ }));
+}
+
+fn wait_diagnostic_terminal(path: &Path, outcome: &str) -> (String, Vec<Value>) {
+ let deadline = Instant::now() + Duration::from_secs(5);
+ loop {
+ let log = fs::read_to_string(path).unwrap_or_default();
+ let records = log
+ .lines()
+ .map(|line| serde_json::from_str::<Value>(line).unwrap())
+ .collect::<Vec<_>>();
+ if records.iter().any(|record| {
+ record
+ .pointer("/event/request_end/outcome")
+ .and_then(Value::as_str)
+ == Some(outcome)
+ }) {
+ return (log, records);
+ }
+ assert!(
+ Instant::now() < deadline,
+ "missing {outcome} request terminal: {log}"
+ );
+ thread::sleep(Duration::from_millis(10));
+ }
+}
+
#[test]
fn real_hirouted_emits_request_route_attempt_commit_usage_and_accepted_only_content() {
hiroute_e2e::p0_execution_receipt!(
@@ -403,7 +721,7 @@ fn real_native_client_completion_before_http_close_keeps_response_and_observatio
let reasoning = serde_json::json!({"type":"reasoning","id":"thought","status":null,
"summary":[{"type":"summary_text","text":"fixture thought"}],"content":null});
let reasoning_stream = [
- serde_json::json!({"type":"response.reasoning_text.delta","output_index":0,"item_id":"thought","content_index":0,"delta":"fixture thought"}),
+ serde_json::json!({"type":"response.reasoning_summary_text.delta","output_index":0,"item_id":"thought","summary_index":0,"delta":"fixture thought"}),
serde_json::json!({"type":"response.output_item.done","output_index":0,"item":reasoning}),
].into_iter().flat_map(|event| format!("data:{event}\n\n").into_bytes()).collect::<Vec<_>>();
let text = String::from_utf8(ACCEPTED_STREAM_TEXT.to_vec())
diff --git a/tools/e2e-harness/tests/p0_gateway_protocol/continuation.rs b/tools/e2e-harness/tests/p0_gateway_protocol/continuation.rs
index 24b10730..269d049d 100644
--- a/tools/e2e-harness/tests/p0_gateway_protocol/continuation.rs
+++ b/tools/e2e-harness/tests/p0_gateway_protocol/continuation.rs
@@ -266,6 +266,8 @@ fn production_native_tool_history_survives_restart_with_request_authentication()
"Use the same legal namespace shape",
),
);
+ // This rejection fixture deliberately authorizes only one candidate and
+ // one attempt. Namespace forwarding is independent of upstream relay.
assert_eq!(
rejected.status,
400,
@@ -640,24 +642,15 @@ fn snapshot(
agent_plan_revision: 56,
protocols: vec![IngressProtocol::Responses],
overall_timeout_ms: 10_000,
- max_attempts: 2,
+ max_attempts: 1,
routing: None,
- candidates: vec![
- candidate(
- 4,
- "rejecting-target",
- "rejecting-credential",
- rejecting_provider,
- IngressProtocol::Responses,
- ),
- candidate(
- 5,
- "rejecting-forbidden-fallback",
- "forbidden-credential",
- forbidden_provider,
- IngressProtocol::Responses,
- ),
- ],
+ candidates: vec![candidate(
+ 4,
+ "rejecting-target",
+ "rejecting-credential",
+ rejecting_provider,
+ IngressProtocol::Responses,
+ )],
},
AliasPlanV1 {
served_model_id: "other-plan".into(),
diff --git a/tools/e2e-harness/tests/p0_gateway_protocol/reviewer.rs b/tools/e2e-harness/tests/p0_gateway_protocol/reviewer.rs
index 4a901421..3f4d7fe2 100644
--- a/tools/e2e-harness/tests/p0_gateway_protocol/reviewer.rs
+++ b/tools/e2e-harness/tests/p0_gateway_protocol/reviewer.rs
@@ -7,9 +7,10 @@ use hiroute_gateway::server::core_runtime::model_ir::{
ResponseBlockKind,
};
use hiroute_gateway::server::core_runtime::profiles::{
- CandidateProtocolProfile, ClientProtocolProfile, Fidelity, NativeReasoningFieldAssignment,
- NativeReasoningRender, NativeReasoningValue, ReasoningAccounting, ReasoningControlKind,
- ReasoningProfileCapability, StreamingRefusalSemantics, fixed_reasoning,
+ CandidateProtocolProfile, ClientProtocolProfile, CriticalFact, Fidelity,
+ NativeReasoningFieldAssignment, NativeReasoningRender, NativeReasoningValue,
+ ReasoningAccounting, ReasoningControlKind, ReasoningProfileCapability,
+ StreamingRefusalSemantics, fixed_reasoning,
};
use hiroute_gateway::server::request_plan::IngressProtocol;
use serde_json::{Value, json};
@@ -246,7 +247,7 @@ fn protocol_reviewer_reasoning_catalog_renders_fixed_toggle_discrete_and_budget_
let messages_request = decode_ingress_request(
IngressProtocol::Messages,
- &json!({"model":"agent/research","max_tokens":1,"messages":[{"role":"user","content":"hello"}]}),
+ &json!({"model":"agent/research","max_tokens":2048,"messages":[{"role":"user","content":"hello"}]}),
)
.unwrap();
let budget = ReasoningProfileCapability {
@@ -267,18 +268,36 @@ fn protocol_reviewer_reasoning_catalog_renders_fixed_toggle_discrete_and_budget_
accounting: ReasoningAccounting::WithinOutputCap,
additional_reservation_tokens: 0,
};
- let budget_profile = CandidateProtocolProfile::exact_portable_path(
+ let mut budget_profile = CandidateProtocolProfile::exact_portable_path(
IngressProtocol::Messages,
IngressProtocol::Messages,
"budget-model",
budget,
);
+ budget_profile.capability.context.max_output_tokens = CriticalFact::Exact(4096);
+ assert_eq!(
+ project_candidate_request(&messages_request, &budget_profile)
+ .unwrap()
+ .body["max_tokens"],
+ 2048
+ );
assert_eq!(
project_candidate_request(&messages_request, &budget_profile)
.unwrap()
.body["thinking"],
json!({"type":"enabled","budget_tokens":1024})
);
+ let capped_request = decode_ingress_request(
+ IngressProtocol::Messages,
+ &json!({"model":"agent/research","max_tokens":1024,"messages":[{"role":"user","content":"hello"}]}),
+ )
+ .unwrap();
+ assert_eq!(
+ project_candidate_request(&capped_request, &budget_profile)
+ .unwrap_err()
+ .code(),
+ "CLIENT_PROTOCOL_UNREPRESENTABLE"
+ );
let mut invalid_budget = budget_profile;
if let NativeReasoningRender::ExactBudget {
selected_tokens, ..
diff --git a/tools/e2e-harness/tests/p0_gateway_runtime.rs b/tools/e2e-harness/tests/p0_gateway_runtime.rs
index 461dbf2e..6fffbf60 100644
--- a/tools/e2e-harness/tests/p0_gateway_runtime.rs
+++ b/tools/e2e-harness/tests/p0_gateway_runtime.rs
@@ -41,6 +41,9 @@ mod reasoning;
#[path = "p0_gateway_runtime/accepted_history.rs"]
mod accepted_history;
+#[path = "p0_gateway_runtime/nonstream_terminal.rs"]
+mod nonstream_terminal;
+
#[test]
fn fixed_requests_retry_only_same_source_keys_and_never_enter_an_allowed_plan() {
for (status, error) in [(401, AUTH_ERROR), (403, AUTH_ERROR), (429, QUOTA_ERROR)] {
@@ -271,37 +274,52 @@ fn malformed_success_and_typed_nonstream_error_fall_back_before_commit() {
#[test]
fn forged_generic_error_header_cannot_change_connector_classification() {
for (status, forged_kind) in [(429, "quota"), (422, "protocol")] {
- let forged = NativeProvider::start(vec![ProviderReply::Complete {
- status,
- error_kind: Some(forged_kind),
- body: br#"{"error":{"type":"invented_provider_signal","message":"stop"}}"#,
- }]);
- let forbidden = NativeProvider::start(vec![ProviderReply::Complete {
+ let forged = NativeProvider::start(vec![
+ ProviderReply::Complete {
+ status,
+ error_kind: Some(forged_kind),
+ body: br#"{"error":{"type":"invented_provider_signal","message":"stop"}}"#,
+ },
+ ProviderReply::Complete {
+ status: 200,
+ error_kind: None,
+ body: RESPONSES_OK,
+ },
+ ]);
+ let fallback = NativeProvider::start(vec![ProviderReply::Complete {
status: 200,
error_kind: None,
body: RESPONSES_OK,
}]);
- let fixture = RuntimeFixture::launch(&[&forged, &forbidden], 2);
+ let fixture = RuntimeFixture::launch(&[&forged, &fallback], 2);
let response = fixture.request();
assert_eq!(
response.status,
- status,
- "forged_calls={} forbidden_calls={} body={}",
+ 200,
+ "forged_calls={} fallback_calls={} body={}",
forged.calls(),
- forbidden.calls(),
+ fallback.calls(),
String::from_utf8_lossy(&response.body)
);
assert_eq!(forged.calls(), 1);
- assert_eq!(forbidden.calls(), 0);
+ assert_eq!(fallback.calls(), 1);
assert!(!String::from_utf8_lossy(&response.body).contains("invented_provider_signal"));
+ let next = fixture
+ .request_body(br#"{"model":"runtime-model","input":"new request","stream":false}"#);
+ assert_eq!(next.status, 200);
+ assert_eq!(
+ forged.calls(),
+ 2,
+ "forged kind cannot quarantine an unclassified failure"
+ );
+ assert_eq!(fallback.calls(), 1);
}
}
#[test]
-fn successful_sse_headers_commit_even_without_semantic_output_and_never_fallback() {
+fn control_only_successful_streams_preserve_native_output() {
for first_body in [
- b"event: response.failed\ndata: {\"type\":\"response.failed\",\"response\":{\"id\":\"failed-stream\",\"model\":\"runtime-native\",\"status\":\"failed\",\"output\":[],\"error\":{\"type\":\"server_error\",\"message\":\"retry\"},\"usage\":{\"input_tokens\":7,\"output_tokens\":2}}}\n\n".as_slice(),
b"event: response.created\ndata: {\"type\":\"response.created\",\"response\":{\"id\":\"empty-stream\",\"model\":\"runtime-native\"}}\n\nevent: response.completed\ndata: {\"type\":\"response.completed\",\"response\":{\"id\":\"empty-stream\",\"model\":\"runtime-native\",\"status\":\"completed\",\"output\":[],\"usage\":{\"input_tokens\":1,\"output_tokens\":0,\"total_tokens\":1}}}\n\n".as_slice(),
b": keepalive\n\nevent: response.created\ndata: {\"type\":\"response.created\",\"response\":{\"id\":\"control-only\",\"model\":\"runtime-native\"}}\n\nevent: response.completed\ndata: {\"type\":\"response.completed\",\"response\":{\"id\":\"control-only\",\"model\":\"runtime-native\",\"status\":\"completed\",\"output\":[]}}\n\n".as_slice(),
] {
@@ -322,24 +340,173 @@ fn successful_sse_headers_commit_even_without_semantic_output_and_never_fallback
assert_eq!(first.calls(), 1);
assert_eq!(second.calls(), 0);
assert!(!String::from_utf8_lossy(&response.body).contains("response.output_text.delta"));
- assert!(String::from_utf8_lossy(&response.body).contains(
- if first_body.starts_with(b"event: response.failed") {
- "response.failed"
- } else {
- "response.completed"
- }
- ));
- if first_body.starts_with(b"event: response.failed") {
- let facts = wait_execution_facts(&fixture, 1);
- assert!(facts.iter().any(|row|
- row.pointer("/fact/kind").and_then(|v| v.as_str()) == Some("usage_and_cache")
- && row.pointer("/fact/input_tokens").and_then(|v| v.as_u64()) == Some(7)
- && row.pointer("/fact/output_tokens").and_then(|v| v.as_u64()) == Some(2)
- ), "failed stream must retain reported usage: {facts:?}");
+ assert!(String::from_utf8_lossy(&response.body).contains("response.completed"));
+ }
+}
+
+#[test]
+fn streaming_failures_before_body_relay_even_after_success_headers() {
+ for (case, reply) in [
+ ProviderReply::StreamComplete {
+ status: 200,
+ body: FAILED_STREAM,
+ },
+ ProviderReply::StreamThenClose {
+ body: b"",
+ declared_length: 100,
+ },
+ ProviderReply::StreamComplete {
+ status: 200,
+ body: b"event: response.output_text.delta\ndata: {invalid}\n\n",
+ },
+ ]
+ .into_iter()
+ .enumerate()
+ {
+ let first = NativeProvider::start(vec![reply]);
+ let fallback = NativeProvider::start(vec![ProviderReply::StreamComplete {
+ status: 200,
+ body: RESPONSES_STREAM_OK,
+ }]);
+ let fixture = RuntimeFixture::launch_with_observation(
+ &[&first, &fallback],
+ 2,
+ ObservationFaults::healthy(),
+ );
+ let response =
+ fixture.request_body(br#"{"model":"runtime-model","input":"hello","stream":true}"#);
+ assert_eq!(response.status, 200);
+ assert_eq!(first.calls(), 1);
+ assert_eq!(fallback.calls(), 1);
+ let body = String::from_utf8_lossy(&response.body);
+ assert!(body.contains("response.completed"), "{body}");
+ assert!(
+ !body.contains("failed-stream"),
+ "failed prefix must not leak: {body}"
+ );
+ let facts = wait_execution_facts(&fixture, 1);
+ assert!(
+ facts
+ .iter()
+ .any(|row| row.pointer("/fact/kind").and_then(|v| v.as_str())
+ == Some("attempt_finished")
+ && row.pointer("/fact/disposition").and_then(|v| v.as_str())
+ == Some("continue")
+ && row.pointer("/fact/retryable").and_then(|v| v.as_bool()) == Some(true)),
+ "retry diagnostics must retain the decision that caused fallback: {facts:?}"
+ );
+ if case == 0 {
+ assert!(
+ facts
+ .iter()
+ .any(|row| row.pointer("/fact/kind").and_then(|v| v.as_str())
+ == Some("usage_and_cache")
+ && row.pointer("/fact/input_tokens").and_then(|v| v.as_u64()) == Some(7)
+ && row.pointer("/fact/output_tokens").and_then(|v| v.as_u64()) == Some(2)),
+ "failed attempt usage must survive fallback: {facts:?}"
+ );
}
+ assert!(
+ facts
+ .iter()
+ .any(|row| row.pointer("/fact/kind").and_then(|v| v.as_str())
+ == Some("request_finished")
+ && row.pointer("/fact/outcome").and_then(|v| v.as_str()) == Some("accepted")),
+ "fallback must complete the request: {facts:?}"
+ );
}
}
+const FAILED_STREAM: &[u8] = b"event: response.failed\ndata: {\"type\":\"response.failed\",\"response\":{\"id\":\"failed-stream\",\"model\":\"runtime-native\",\"status\":\"failed\",\"output\":[],\"error\":{\"type\":\"server_error\",\"message\":\"retry\"},\"usage\":{\"input_tokens\":7,\"output_tokens\":2}}}\n\n";
+
+#[test]
+fn streaming_failure_after_delivered_body_stays_failed_and_retains_usage() {
+ use std::io::Read;
+ let early = b"event: response.created\ndata: {\"type\":\"response.created\",\"response\":{\"id\":\"failed-stream\",\"model\":\"runtime-native\"}}\n\n";
+ let first = NativeProvider::start(vec![ProviderReply::StreamDrip {
+ status: 200,
+ chunks: vec![early.to_vec(), FAILED_STREAM.to_vec()],
+ interval: Duration::from_secs(1),
+ }]);
+ let fallback = NativeProvider::start(vec![ProviderReply::StreamComplete {
+ status: 200,
+ body: RESPONSES_STREAM_OK,
+ }]);
+ let fixture = RuntimeFixture::launch_classified_with_observation(
+ &[&first, &fallback],
+ 2,
+ ObservationFaults::healthy(),
+ );
+ let mut stream = open_request(
+ fixture.address,
+ "POST",
+ "/v1/responses",
+ &[("X-HiRoute-Token", "runtime-token")],
+ br#"{"model":"runtime-model","input":"hello","stream":true}"#,
+ );
+ stream
+ .set_read_timeout(Some(Duration::from_millis(800)))
+ .unwrap();
+ let mut received = Vec::new();
+ let mut chunk = [0u8; 4096];
+ // Native model identifiers are rewritten to the served alias. Match the
+ // delivered event, not the provider's pre-projection JSON bytes.
+ let first_body_marker = b"event: response.created\n";
+ while !received
+ .windows(first_body_marker.len())
+ .any(|part| part == first_body_marker)
+ {
+ let count = stream
+ .read(&mut chunk)
+ .expect("first body must be delivered before failure");
+ assert!(count > 0);
+ received.extend_from_slice(&chunk[..count]);
+ }
+ stream
+ .set_read_timeout(Some(Duration::from_secs(5)))
+ .unwrap();
+ stream.read_to_end(&mut received).unwrap();
+ assert!(String::from_utf8_lossy(&received).contains("response.failed"));
+ assert_eq!(first.calls(), 1);
+ assert_eq!(
+ fallback.calls(),
+ 0,
+ "body delivery closes transparent relay"
+ );
+ let facts = wait_execution_facts(&fixture, 1);
+ assert!(
+ facts
+ .iter()
+ .any(|row| row.pointer("/fact/kind").and_then(|v| v.as_str())
+ == Some("agent_turn_finished")
+ && row.pointer("/fact/status").and_then(|v| v.as_str()) == Some("failed")),
+ "a delivered model failure must not complete the Agent turn: {facts:?}"
+ );
+ for (kind, outcome) in [
+ ("request_finished", "failed"),
+ ("attempt_finished", "rejected"),
+ ] {
+ assert!(
+ facts
+ .iter()
+ .any(
+ |row| row.pointer("/fact/kind").and_then(|v| v.as_str()) == Some(kind)
+ && row.pointer("/fact/outcome").and_then(|v| v.as_str()) == Some(outcome)
+ ),
+ "transport completion must retain model failure: {facts:?}"
+ );
+ }
+ assert!(
+ facts
+ .iter()
+ .any(|row| row.pointer("/fact/kind").and_then(|v| v.as_str())
+ == Some("usage_and_cache")
+ && row.pointer("/fact/input_tokens").and_then(|v| v.as_u64()) == Some(7)
+ && row.pointer("/fact/output_tokens").and_then(|v| v.as_u64()) == Some(2)),
+ "failed stream must retain reported usage: {facts:?}"
+ );
+}
+
#[test]
fn successful_stream_forwards_unknown_events_before_any_semantic_output() {
use std::io::Read;
@@ -613,7 +780,7 @@ fn real_listener_distinguishes_quota_key_scope_from_overload_binding_scope() {
}
#[test]
-fn real_listener_typed_400_and_422_preserve_protocol_and_permanent_boundaries() {
+fn real_listener_all_prebody_400_and_422_relay_without_quarantining() {
struct Case {
name: &'static str,
status: u16,
@@ -624,6 +791,22 @@ fn real_listener_typed_400_and_422_preserve_protocol_and_permanent_boundaries()
}
for case in [
+ Case {
+ name: "reasoning-budget-400",
+ status: 400,
+ body: br#"{"error":{"code":"InvalidParameter","message":"<400> InternalError.Algo.InvalidParameter: The thinking_budget parameter must be a positive integer and not greater than 131072"}}"#,
+ second_primary_reply: ProviderReply::Complete { status: 200, error_kind: None, body: RESPONSES_OK },
+ expected_downstream: 200,
+ expected_fallback_calls: 1,
+ },
+ Case {
+ name: "other-invalid-parameter-400",
+ status: 400,
+ body: br#"{"error":{"code":"InvalidParameter","message":"private invalid tool schema"}}"#,
+ second_primary_reply: ProviderReply::Complete { status: 200, error_kind: None, body: RESPONSES_OK },
+ expected_downstream: 200,
+ expected_fallback_calls: 1,
+ },
Case {
name: "protocol-400",
status: 400,
@@ -641,12 +824,12 @@ fn real_listener_typed_400_and_422_preserve_protocol_and_permanent_boundaries()
status: 422,
body: PERMANENT_ERROR,
second_primary_reply: ProviderReply::Complete {
- status: 422,
+ status: 200,
error_kind: None,
- body: PERMANENT_ERROR,
+ body: RESPONSES_OK,
},
- expected_downstream: 422,
- expected_fallback_calls: 0,
+ expected_downstream: 200,
+ expected_fallback_calls: 1,
},
] {
let primary = NativeProvider::start(vec![
@@ -2081,7 +2264,8 @@ fn typed_failure_matrix_keeps_quota_binding_and_permanent_failures_distinct() {
assert_eq!(overload.class, AttemptFailureClass::BindingOverload);
assert_eq!(overload.state_scope(), Some(FailureStateScope::Binding));
assert_eq!(permanent.class, AttemptFailureClass::PermanentClient);
- assert!(!permanent.is_precommit_relayable());
+ assert!(permanent.is_precommit_relayable());
+ assert_eq!(permanent.state_scope(), None);
}
fn assert_fault_control_ack(response: &WireResponse, operation: &str, remaining: u64) {
diff --git a/tools/e2e-harness/tests/p0_gateway_runtime/nonstream_terminal.rs b/tools/e2e-harness/tests/p0_gateway_runtime/nonstream_terminal.rs
new file mode 100644
index 00000000..15d3a5bd
--- /dev/null
+++ b/tools/e2e-harness/tests/p0_gateway_runtime/nonstream_terminal.rs
@@ -0,0 +1,80 @@
+use super::*;
+
+const RESPONSES_INCOMPLETE: &[u8] = br#"{"id":"partial-response","model":"runtime-native","status":"incomplete","incomplete_details":{"reason":"max_output_tokens"},"output":[{"type":"message","id":"partial-message","role":"assistant","status":"incomplete","content":[{"type":"output_text","text":"PRIVATE_PARTIAL_BODY"}]}],"usage":{"input_tokens":7,"output_tokens":2,"total_tokens":9}}"#;
+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 known_nonstream_incomplete_relays_before_body_and_retains_usage_within_budget() {
+ for (protocol, body) in [
+ ("responses", RESPONSES_INCOMPLETE),
+ ("chat_completions", CHAT_INCOMPLETE),
+ ("messages", MESSAGES_INCOMPLETE),
+ ] {
+ for max_attempts in [1, 2] {
+ let first = NativeProvider::start(vec![ProviderReply::Complete {
+ status: 200,
+ error_kind: None,
+ body,
+ }]);
+ let second = NativeProvider::start(vec![ProviderReply::Complete {
+ status: 200,
+ error_kind: None,
+ body: FALLBACK_OK,
+ }]);
+ let candidates = [
+ PublicationCandidate {
+ provider_index: 0,
+ upstream_protocol: protocol,
+ statically_enabled: true,
+ },
+ PublicationCandidate {
+ provider_index: 1,
+ upstream_protocol: "responses",
+ statically_enabled: true,
+ },
+ ];
+ let fixture = RuntimeFixture::launch_with_observation_and_publication_candidates(
+ &[&first, &second],
+ max_attempts,
+ ObservationFaults::healthy(),
+ Some(&candidates),
+ );
+ let response = fixture
+ .request_body(br#"{"model":"runtime-model","input":"hello","stream":false}"#);
+ let label = format!("{protocol}, max_attempts={max_attempts}");
+ let wire = String::from_utf8_lossy(&response.body);
+ assert_eq!(
+ response.status,
+ if max_attempts == 2 { 200 } else { 502 },
+ "{label}: {wire}"
+ );
+ assert_eq!(first.calls(), 1, "{label}");
+ assert_eq!(second.calls(), usize::from(max_attempts == 2), "{label}");
+ assert!(!wire.contains("PRIVATE_PARTIAL_BODY"), "{label}: {wire}");
+ if max_attempts == 2 {
+ assert!(wire.contains("fallback"), "{label}: {wire}");
+ }
+ let facts = wait_execution_facts(&fixture, 1);
+ assert!(
+ facts
+ .iter()
+ .any(|row| row.pointer("/fact/kind").and_then(|v| v.as_str())
+ == Some("attempt_finished")
+ && row.pointer("/fact/retryable").and_then(|v| v.as_bool()) == Some(true)
+ && row.pointer("/fact/disposition").and_then(|v| v.as_str())
+ == Some("continue")),
+ "prebody failure must remain relayable: {label}: {facts:?}"
+ );
+ assert!(
+ facts
+ .iter()
+ .any(|row| row.pointer("/fact/kind").and_then(|v| v.as_str())
+ == Some("usage_and_cache")
+ && row.pointer("/fact/input_tokens").and_then(|v| v.as_u64()) == Some(7)
+ && row.pointer("/fact/output_tokens").and_then(|v| v.as_u64()) == Some(2)),
+ "incomplete attempt usage must survive: {label}: {facts:?}"
+ );
+ }
+ }
+}
diff --git a/tools/e2e-harness/tests/runtime_support/mod.rs b/tools/e2e-harness/tests/runtime_support/mod.rs
index 694c6d9a..1ba79d7d 100644
--- a/tools/e2e-harness/tests/runtime_support/mod.rs
+++ b/tools/e2e-harness/tests/runtime_support/mod.rs
@@ -44,6 +44,11 @@ pub enum ProviderReply {
retry_after_secs: u64,
body: &'static [u8],
},
+ CompleteWithRequestId {
+ status: u16,
+ request_id: &'static str,
+ body: &'static [u8],
+ },
DelayedComplete {
duration: Duration,
status: u16,
@@ -391,6 +396,19 @@ fn write_provider_reply(stream: &mut TestTlsStream, reply: ProviderReply) -> std
},
);
}
+ ProviderReply::CompleteWithRequestId {
+ status,
+ request_id,
+ body,
+ } => {
+ write!(
+ stream,
+ "HTTP/1.1 {status} Provider Response\r\nContent-Type: application/json\r\nX-Request-Id: {request_id}\r\nContent-Length: {}\r\nConnection: close\r\n\r\n",
+ body.len()
+ )?;
+ stream.write_all(body)?;
+ stream.flush()?;
+ }
ProviderReply::EarlyComplete { status, body } => {
write!(
stream,
@@ -466,6 +484,7 @@ pub struct RuntimeFixture {
pub address: SocketAddr,
pub replay_root: PathBuf,
pub observation_root: Option<PathBuf>,
+ pub diagnostics_root: Option<PathBuf>,
runtime_state_control: Option<(SocketAddr, String)>,
_serial: std::sync::MutexGuard<'static, ()>,
_directory: tempfile::TempDir,
@@ -479,6 +498,7 @@ struct RuntimeLaunchOptions {
replay_root_mode: ReplayRootMode,
orphan_ttl_ms: u64,
observation: Option<ObservationFaults>,
+ diagnostic_level: Option<&'static str>,
test_control: bool,
classified_route: bool,
rest_classifier: bool,
@@ -527,6 +547,7 @@ impl Default for RuntimeLaunchOptions {
replay_root_mode: ReplayRootMode::Normal,
orphan_ttl_ms: 24 * 60 * 60 * 1_000,
observation: None,
+ diagnostic_level: None,
test_control: false,
classified_route: false,
rest_classifier: false,
@@ -690,6 +711,61 @@ impl RuntimeFixture {
)
}
+ pub fn launch_classified_with_observation(
+ providers: &[&NativeProvider],
+ max_attempts: u32,
+ faults: ObservationFaults,
+ ) -> Self {
+ assert_eq!(providers.len(), 2);
+ Self::launch_configured(
+ providers,
+ max_attempts,
+ None,
+ None,
+ None,
+ RuntimeLaunchOptions {
+ classified_route: true,
+ observation: Some(faults),
+ ..RuntimeLaunchOptions::default()
+ },
+ )
+ }
+
+ pub fn launch_with_info_diagnostics(providers: &[&NativeProvider], max_attempts: u32) -> Self {
+ Self::launch_configured(
+ providers,
+ max_attempts,
+ None,
+ None,
+ None,
+ RuntimeLaunchOptions {
+ diagnostic_level: Some("info"),
+ reasoning_choices: true,
+ ..RuntimeLaunchOptions::default()
+ },
+ )
+ }
+
+ pub fn launch_classified_with_info_diagnostics(
+ providers: &[&NativeProvider],
+ max_attempts: u32,
+ ) -> Self {
+ assert_eq!(providers.len(), 2);
+ Self::launch_configured(
+ providers,
+ max_attempts,
+ None,
+ None,
+ None,
+ RuntimeLaunchOptions {
+ classified_route: true,
+ diagnostic_level: Some("info"),
+ reasoning_choices: true,
+ ..RuntimeLaunchOptions::default()
+ },
+ )
+ }
+
pub fn launch_rest_classified(providers: &[&NativeProvider], max_attempts: u32) -> Self {
assert_eq!(
providers.len(),
@@ -1112,6 +1188,9 @@ impl RuntimeFixture {
let observation_root = options
.observation
.map(|_| directory.path().join("observation"));
+ let diagnostics_root = options
+ .diagnostic_level
+ .map(|_| directory.path().join("diagnostics"));
if let Some(root) = &observation_root {
std::fs::create_dir(root).unwrap();
}
@@ -1197,6 +1276,7 @@ impl RuntimeFixture {
address,
replay_root,
observation_root,
+ diagnostics_root,
runtime_state_control: control.map(|(control, _)| control),
_serial: serial,
}
@@ -1339,6 +1419,12 @@ impl Hirouted {
.arg(publication)
.arg("--credentials")
.arg(credentials);
+ if let Some(level) = options.diagnostic_level {
+ command
+ .arg("--diagnostics-root")
+ .arg(directory.join("diagnostics"))
+ .args(["--diagnostic-level-override", level]);
+ }
if let Some(attempt_timeout_ms) = options.attempt_timeout_ms {
command.env(E2E_ATTEMPT_TIMEOUT_MS_ENV, attempt_timeout_ms.to_string());
}