fix(gateway): restore failover and usage diagnostics

This commit is contained in:
elky
2026-07-30 09:12:11 +08:00
parent a04673a90d
commit 1ab4f079c9
4 changed files with 116 additions and 30 deletions
@@ -148,18 +148,17 @@ pub(super) fn build_stream_failure_from_execution_error(
error: &ExecutionError,
) -> StreamFailureReport {
let transport_error = execution_error_is_transport(error);
let status_code = error.upstream_status.unwrap_or_else(|| {
if matches!(
error.kind,
ExecutionErrorKind::ConnectTimeout
| ExecutionErrorKind::FirstByteTimeout
| ExecutionErrorKind::ReadTimeout
) {
504
} else {
502
}
});
let fallback_status_code = if matches!(
error.kind,
ExecutionErrorKind::ConnectTimeout
| ExecutionErrorKind::FirstByteTimeout
| ExecutionErrorKind::ReadTimeout
) {
504
} else {
502
};
let status_code = error.upstream_status.unwrap_or(fallback_status_code);
let error_type = serde_json::to_value(&error.kind)
.ok()
.and_then(|value| value.as_str().map(ToOwned::to_owned))
@@ -639,6 +638,7 @@ pub(super) async fn handle_prefetch_stream_failure(
)
.await;
}
let honor_local_failover = honor_http_failover && retry_scope_out.is_some();
let failure_analysis = record_stream_sync_failure(
state,
plan,
@@ -646,14 +646,14 @@ pub(super) async fn handle_prefetch_stream_failure(
&payload,
candidate_status_code,
None,
if honor_http_failover {
if honor_local_failover {
StreamFailureHandling::HonorLocalFailover
} else {
StreamFailureHandling::Terminal
},
)
.await;
if honor_http_failover
if honor_local_failover
&& matches!(
failure_analysis.decision,
LocalFailoverDecision::RetryNextCandidate
@@ -722,8 +722,8 @@ async fn handle_prefetch_transport_stream_failure(
payload.report_context.as_ref(),
)
.await;
let retrying_next_candidate =
matches!(analysis.decision, LocalFailoverDecision::RetryNextCandidate);
let retrying_next_candidate = retry_scope_out.is_some()
&& matches!(analysis.decision, LocalFailoverDecision::RetryNextCandidate);
if !retrying_next_candidate {
crate::execution_runtime::mark_stream_candidate_watchdog_terminal_started();
let report_context_with_diagnostics =
@@ -1577,6 +1577,8 @@ mod tests {
const TEST_OPENAI_IMAGE_SYNC_PLAN_KIND: &str = "openai_image_sync";
const TEST_STANDARD_TEXT_SYNC_PLAN_KIND: &str = "openai_responses_compact_sync";
const HEARTBEAT_USAGE_POLL_INTERVAL: Duration = Duration::from_millis(10);
const HEARTBEAT_USAGE_SETTLE_TIMEOUT: Duration = Duration::from_secs(30);
struct TestSyncAttemptSource {
attempts: VecDeque<AiSyncAttempt>,
@@ -1733,11 +1735,32 @@ mod tests {
usage_repository: &InMemoryUsageReadRepository,
request_id: &str,
) {
let usage = usage_repository
.find_by_request_id(request_id)
.await
.expect("usage should read")
.expect("terminal usage should be recorded");
let deadline = Instant::now() + HEARTBEAT_USAGE_SETTLE_TIMEOUT;
let usage = loop {
let usage = usage_repository
.find_by_request_id(request_id)
.await
.expect("usage should read");
if usage.as_ref().is_some_and(|usage| {
matches!(usage.status.as_str(), "completed" | "failed" | "cancelled")
}) {
break usage.expect("terminal usage should be recorded");
}
let now = Instant::now();
let last_status = usage.as_ref().map(|usage| usage.status.as_str());
assert!(
now < deadline,
"terminal usage should be recorded within {HEARTBEAT_USAGE_SETTLE_TIMEOUT:?}; \
last status: {}",
last_status.unwrap_or("<missing>")
);
tokio::time::sleep(HEARTBEAT_USAGE_POLL_INTERVAL.min(deadline - now)).await;
};
assert_eq!(
usage.status, "completed",
"heartbeat usage should complete successfully"
);
let request_metadata = usage
.request_metadata
.as_ref()