fix(ai-formats): preserve Claude raw blocks, reasoning tokens, and test stack safety

This commit is contained in:
zhefox
2026-06-17 03:45:23 +08:00
parent 628a3a0d8d
commit 6ab08f4014
6 changed files with 229 additions and 27 deletions
@@ -271,7 +271,7 @@ mod tests {
use super::{
append_local_failover_policy_to_value, local_failover_policy_from_report_context,
LocalFailoverPolicy, LocalFailoverRegexRule,
local_failover_policy_from_transport, LocalFailoverPolicy, LocalFailoverRegexRule,
};
use crate::provider_transport::snapshot::{
GatewayProviderTransportEndpoint, GatewayProviderTransportKey,
+30 -2
View File
@@ -32,6 +32,27 @@ fn hash_api_key(value: &str) -> String {
format!("{:x}", hasher.finalize())
}
fn run_async_test_on_large_stack<F>(name: &'static str, future: F)
where
F: std::future::Future<Output = ()> + Send + 'static,
{
let handle = std::thread::Builder::new()
.name(name.to_string())
.stack_size(16 * 1024 * 1024)
.spawn(move || {
tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.expect("tokio runtime should build")
.block_on(future);
})
.expect("large-stack audit test thread should spawn");
if let Err(payload) = handle.join() {
std::panic::resume_unwind(payload);
}
}
fn sample_local_openai_auth_snapshot(api_key_id: &str, user_id: &str) -> StoredAuthApiKeySnapshot {
StoredAuthApiKeySnapshot::new(
user_id.to_string(),
@@ -158,8 +179,15 @@ fn sample_local_openai_key() -> StoredProviderCatalogKey {
.expect("key transport should build")
}
#[tokio::test]
async fn gateway_exposes_request_id_header_for_local_execution_response() {
#[test]
fn gateway_exposes_request_id_header_for_local_execution_response() {
run_async_test_on_large_stack(
"gateway_exposes_request_id_header_for_local_execution_response",
gateway_exposes_request_id_header_for_local_execution_response_impl(),
);
}
async fn gateway_exposes_request_id_header_for_local_execution_response_impl() {
let auth_repository = Arc::new(InMemoryAuthApiKeySnapshotRepository::seed(vec![(
Some(hash_api_key("sk-client-openai-audit-bundle")),
sample_local_openai_auth_snapshot("api-key-1", "user-1"),
+36 -8
View File
@@ -40,8 +40,15 @@ where
}
}
#[tokio::test]
async fn gateway_records_usage_for_execution_runtime_sync_when_runtime_enabled() {
#[test]
fn gateway_records_usage_for_execution_runtime_sync_when_runtime_enabled() {
run_async_test_on_large_stack(
"gateway_records_usage_for_execution_runtime_sync_when_runtime_enabled",
gateway_records_usage_for_execution_runtime_sync_when_runtime_enabled_impl(),
);
}
async fn gateway_records_usage_for_execution_runtime_sync_when_runtime_enabled_impl() {
let usage_repository = Arc::new(InMemoryUsageReadRepository::default());
let request_candidate_repository = Arc::new(InMemoryRequestCandidateRepository::default());
@@ -312,8 +319,15 @@ async fn gateway_records_pending_usage_before_execution_runtime_sync_result_arri
upstream_handle.abort();
}
#[tokio::test]
async fn gateway_keeps_pending_sync_usage_lightweight_for_large_request_body() {
#[test]
fn gateway_keeps_pending_sync_usage_lightweight_for_large_request_body() {
run_async_test_on_large_stack(
"gateway_keeps_pending_sync_usage_lightweight_for_large_request_body",
gateway_keeps_pending_sync_usage_lightweight_for_large_request_body_impl(),
);
}
async fn gateway_keeps_pending_sync_usage_lightweight_for_large_request_body_impl() {
let usage_repository = Arc::new(InMemoryUsageReadRepository::default());
let request_candidate_repository = Arc::new(InMemoryRequestCandidateRepository::default());
let execution_request_started = Arc::new(tokio::sync::Notify::new());
@@ -576,8 +590,15 @@ async fn gateway_records_usage_for_execution_runtime_stream_when_runtime_enabled
upstream_handle.abort();
}
#[tokio::test]
async fn gateway_records_pending_usage_before_execution_runtime_stream_headers_arrive() {
#[test]
fn gateway_records_pending_usage_before_execution_runtime_stream_headers_arrive() {
run_async_test_on_large_stack(
"gateway_records_pending_usage_before_execution_runtime_stream_headers_arrive",
gateway_records_pending_usage_before_execution_runtime_stream_headers_arrive_impl(),
);
}
async fn gateway_records_pending_usage_before_execution_runtime_stream_headers_arrive_impl() {
let usage_repository = Arc::new(InMemoryUsageReadRepository::default());
let request_candidate_repository = Arc::new(InMemoryRequestCandidateRepository::default());
let execution_request_started = Arc::new(tokio::sync::Notify::new());
@@ -725,8 +746,15 @@ async fn gateway_records_pending_usage_before_execution_runtime_stream_headers_a
upstream_handle.abort();
}
#[tokio::test]
async fn gateway_keeps_pending_stream_usage_lightweight_for_large_request_body() {
#[test]
fn gateway_keeps_pending_stream_usage_lightweight_for_large_request_body() {
run_async_test_on_large_stack(
"gateway_keeps_pending_stream_usage_lightweight_for_large_request_body",
gateway_keeps_pending_stream_usage_lightweight_for_large_request_body_impl(),
);
}
async fn gateway_keeps_pending_stream_usage_lightweight_for_large_request_body_impl() {
let usage_repository = Arc::new(InMemoryUsageReadRepository::default());
let request_candidate_repository = Arc::new(InMemoryRequestCandidateRepository::default());
let execution_request_started = Arc::new(tokio::sync::Notify::new());
+48 -11
View File
@@ -31,7 +31,7 @@ where
{
let handle = std::thread::Builder::new()
.name(name.to_string())
.stack_size(8 * 1024 * 1024)
.stack_size(16 * 1024 * 1024)
.spawn(move || {
tokio::runtime::Builder::new_current_thread()
.enable_all()
@@ -421,8 +421,15 @@ async fn gateway_truncates_deep_request_echo_for_local_openai_chat_sync_usage_im
upstream_handle.abort();
}
#[tokio::test]
async fn gateway_applies_system_max_request_body_size_to_local_openai_chat_sync_usage() {
#[test]
fn gateway_applies_system_max_request_body_size_to_local_openai_chat_sync_usage() {
run_async_test_on_large_stack(
"gateway_applies_system_max_request_body_size_to_local_openai_chat_sync_usage",
gateway_applies_system_max_request_body_size_to_local_openai_chat_sync_usage_impl(),
);
}
async fn gateway_applies_system_max_request_body_size_to_local_openai_chat_sync_usage_impl() {
let usage_repository = Arc::new(InMemoryUsageReadRepository::default());
let request_candidate_repository = Arc::new(InMemoryRequestCandidateRepository::default());
@@ -711,8 +718,16 @@ async fn gateway_strips_request_and_response_bodies_when_request_record_level_is
upstream_handle.abort();
}
#[tokio::test]
async fn gateway_records_failed_usage_when_all_local_openai_chat_candidates_exhaust_after_retryable_sync_failure(
#[test]
fn gateway_records_failed_usage_when_all_local_openai_chat_candidates_exhaust_after_retryable_sync_failure(
) {
run_async_test_on_large_stack(
"gateway_records_failed_usage_when_all_local_openai_chat_candidates_exhaust_after_retryable_sync_failure",
gateway_records_failed_usage_when_all_local_openai_chat_candidates_exhaust_after_retryable_sync_failure_impl(),
);
}
async fn gateway_records_failed_usage_when_all_local_openai_chat_candidates_exhaust_after_retryable_sync_failure_impl(
) {
let usage_repository = Arc::new(InMemoryUsageReadRepository::default());
let request_candidate_repository = Arc::new(InMemoryRequestCandidateRepository::default());
@@ -854,8 +869,15 @@ async fn gateway_records_failed_usage_when_all_local_openai_chat_candidates_exha
assert_eq!(stored_candidates[0].status_code, Some(503));
}
#[tokio::test]
async fn gateway_records_failed_usage_when_sync_runtime_transport_is_unavailable_without_plan_fallback(
#[test]
fn gateway_records_failed_usage_when_sync_runtime_transport_is_unavailable_without_plan_fallback() {
run_async_test_on_large_stack(
"gateway_records_failed_usage_when_sync_runtime_transport_is_unavailable_without_plan_fallback",
gateway_records_failed_usage_when_sync_runtime_transport_is_unavailable_without_plan_fallback_impl(),
);
}
async fn gateway_records_failed_usage_when_sync_runtime_transport_is_unavailable_without_plan_fallback_impl(
) {
let usage_repository = Arc::new(InMemoryUsageReadRepository::default());
let request_candidate_repository = Arc::new(InMemoryRequestCandidateRepository::default());
@@ -1166,8 +1188,16 @@ async fn gateway_records_failed_usage_for_claude_runtime_miss_without_execution_
execution_runtime_handle.abort();
}
#[tokio::test]
async fn gateway_handles_local_openai_chat_stream_report_with_local_reporting_when_usage_runtime_enabled(
#[test]
fn gateway_handles_local_openai_chat_stream_report_with_local_reporting_when_usage_runtime_enabled()
{
run_async_test_on_large_stack(
"gateway_handles_local_openai_chat_stream_report_with_local_reporting_when_usage_runtime_enabled",
gateway_handles_local_openai_chat_stream_report_with_local_reporting_when_usage_runtime_enabled_impl(),
);
}
async fn gateway_handles_local_openai_chat_stream_report_with_local_reporting_when_usage_runtime_enabled_impl(
) {
let usage_repository = Arc::new(InMemoryUsageReadRepository::default());
let request_candidate_repository = Arc::new(InMemoryRequestCandidateRepository::default());
@@ -1333,8 +1363,15 @@ async fn gateway_handles_local_openai_chat_stream_report_with_local_reporting_wh
upstream_handle.abort();
}
#[tokio::test]
async fn gateway_preserves_stream_usage_when_max_response_body_size_truncates_capture() {
#[test]
fn gateway_preserves_stream_usage_when_max_response_body_size_truncates_capture() {
run_async_test_on_large_stack(
"gateway_preserves_stream_usage_when_max_response_body_size_truncates_capture",
gateway_preserves_stream_usage_when_max_response_body_size_truncates_capture_impl(),
);
}
async fn gateway_preserves_stream_usage_when_max_response_body_size_truncates_capture_impl() {
let usage_repository = Arc::new(InMemoryUsageReadRepository::default());
let request_candidate_repository = Arc::new(InMemoryRequestCandidateRepository::default());