2026-05-02 13:23:54 +08:00
|
|
|
use aether_ai_serving::{
|
|
|
|
|
run_ai_attempt_loop, AiAttemptLoopOutcome, AiAttemptLoopPort, AiExecutionAttempt,
|
|
|
|
|
};
|
2026-04-07 02:50:19 +08:00
|
|
|
use aether_data_contracts::repository::candidates::RequestCandidateStatus;
|
2026-06-24 01:47:26 +08:00
|
|
|
use aether_runtime::ConcurrencyPermit;
|
2026-04-11 01:50:24 +08:00
|
|
|
use aether_scheduler_core::{
|
|
|
|
|
parse_request_candidate_report_context, SchedulerRequestCandidateStatusUpdate,
|
|
|
|
|
};
|
2026-05-02 13:23:54 +08:00
|
|
|
use async_trait::async_trait;
|
2026-04-11 01:50:24 +08:00
|
|
|
use axum::body::Body;
|
|
|
|
|
use axum::http::Response;
|
2026-06-24 01:47:26 +08:00
|
|
|
use futures_util::StreamExt;
|
2026-04-11 01:50:24 +08:00
|
|
|
use tokio::time::{timeout, Duration};
|
|
|
|
|
use tracing::{debug, warn, Instrument};
|
2026-04-07 02:50:19 +08:00
|
|
|
|
2026-05-03 20:14:29 +08:00
|
|
|
use crate::ai_serving::LocalExecutionAttemptSource;
|
2026-04-11 01:50:24 +08:00
|
|
|
use crate::clock::current_unix_ms;
|
2026-04-05 20:23:16 +08:00
|
|
|
use crate::control::GatewayControlDecision;
|
2026-04-07 02:50:19 +08:00
|
|
|
use crate::execution_runtime::{execute_execution_runtime_stream, execute_execution_runtime_sync};
|
2026-04-10 17:44:55 +08:00
|
|
|
use crate::executor::{build_local_execution_exhaustion, LocalExecutionRequestOutcome};
|
2026-05-11 00:12:05 +08:00
|
|
|
use crate::handlers::shared::provider_pool::release_admin_provider_pool_key_lease;
|
2026-04-11 01:50:24 +08:00
|
|
|
use crate::log_ids::short_request_id;
|
2026-04-24 13:29:05 +08:00
|
|
|
use crate::orchestration::local_execution_candidate_metadata_from_report_context;
|
2026-05-13 18:25:13 +08:00
|
|
|
use crate::privacy::RedactionExecutionCandidateId;
|
2026-04-11 01:50:24 +08:00
|
|
|
use crate::request_candidate_runtime::{
|
|
|
|
|
record_local_request_candidate_status, RequestCandidateRuntimeWriter,
|
|
|
|
|
};
|
2026-06-24 01:47:26 +08:00
|
|
|
use crate::stage_metrics::observe_gateway_stage_ms;
|
2026-04-05 20:23:16 +08:00
|
|
|
use crate::{AppState, GatewayError};
|
2026-04-03 14:59:58 +08:00
|
|
|
|
2026-05-25 20:09:37 +08:00
|
|
|
const DEFAULT_STREAM_FIRST_BYTE_WATCHDOG_TIMEOUT_MS: u64 = 30_000;
|
2026-04-11 01:50:24 +08:00
|
|
|
|
2026-05-13 18:25:13 +08:00
|
|
|
fn attach_redaction_execution_candidate(response: &mut Response<Body>, candidate_id: Option<&str>) {
|
|
|
|
|
if let Some(candidate_id) = candidate_id
|
|
|
|
|
.map(str::trim)
|
|
|
|
|
.filter(|value| !value.is_empty())
|
|
|
|
|
{
|
|
|
|
|
response
|
|
|
|
|
.extensions_mut()
|
|
|
|
|
.insert(RedactionExecutionCandidateId::new(candidate_id));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-03 14:59:58 +08:00
|
|
|
pub(crate) async fn execute_sync_plan_and_reports<T>(
|
|
|
|
|
state: &AppState,
|
|
|
|
|
parts: &http::request::Parts,
|
|
|
|
|
trace_id: &str,
|
|
|
|
|
decision: &GatewayControlDecision,
|
|
|
|
|
plan_kind: &str,
|
|
|
|
|
plan_and_reports: Vec<T>,
|
2026-04-10 17:44:55 +08:00
|
|
|
) -> Result<LocalExecutionRequestOutcome, GatewayError>
|
2026-04-03 14:59:58 +08:00
|
|
|
where
|
2026-05-02 13:23:54 +08:00
|
|
|
T: AiExecutionAttempt + Send + Sync + 'static,
|
2026-04-03 14:59:58 +08:00
|
|
|
{
|
2026-04-11 01:50:24 +08:00
|
|
|
let candidate_count = plan_and_reports.len();
|
|
|
|
|
let first_provider = plan_and_reports
|
|
|
|
|
.first()
|
2026-05-02 13:23:54 +08:00
|
|
|
.and_then(|item| item.execution_plan().provider_name.as_deref())
|
2026-04-11 01:50:24 +08:00
|
|
|
.unwrap_or("-")
|
|
|
|
|
.to_string();
|
|
|
|
|
let span = tracing::debug_span!(
|
|
|
|
|
"candidates",
|
|
|
|
|
trace_id = %trace_id,
|
|
|
|
|
plan_kind,
|
|
|
|
|
candidate_count,
|
|
|
|
|
);
|
2026-04-03 14:59:58 +08:00
|
|
|
|
2026-04-11 01:50:24 +08:00
|
|
|
async move {
|
|
|
|
|
tracing::debug!(
|
|
|
|
|
event_name = "candidate_loop_started",
|
|
|
|
|
log_type = "event",
|
|
|
|
|
trace_id = %trace_id,
|
|
|
|
|
plan_kind,
|
|
|
|
|
candidate_count,
|
|
|
|
|
first_provider = first_provider.as_str(),
|
|
|
|
|
"candidate loop started"
|
|
|
|
|
);
|
|
|
|
|
|
2026-05-02 13:23:54 +08:00
|
|
|
let port = SyncAttemptLoopPort {
|
|
|
|
|
state,
|
|
|
|
|
parts,
|
|
|
|
|
trace_id,
|
|
|
|
|
decision,
|
|
|
|
|
plan_kind,
|
2026-04-11 01:50:24 +08:00
|
|
|
};
|
2026-05-02 13:23:54 +08:00
|
|
|
match run_ai_attempt_loop(&port, plan_and_reports).await? {
|
|
|
|
|
AiAttemptLoopOutcome::Responded(response) => {
|
|
|
|
|
Ok(LocalExecutionRequestOutcome::responded(response))
|
|
|
|
|
}
|
|
|
|
|
AiAttemptLoopOutcome::Exhausted(exhaustion) => {
|
|
|
|
|
Ok(LocalExecutionRequestOutcome::Exhausted(exhaustion))
|
|
|
|
|
}
|
|
|
|
|
AiAttemptLoopOutcome::NoPath => Ok(LocalExecutionRequestOutcome::NoPath),
|
|
|
|
|
}
|
2026-04-11 01:50:24 +08:00
|
|
|
}
|
|
|
|
|
.instrument(span)
|
|
|
|
|
.await
|
2026-04-03 14:59:58 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-03 20:14:29 +08:00
|
|
|
pub(crate) async fn execute_sync_attempt_source<T, S>(
|
|
|
|
|
state: &AppState,
|
|
|
|
|
parts: &http::request::Parts,
|
|
|
|
|
trace_id: &str,
|
|
|
|
|
decision: &GatewayControlDecision,
|
|
|
|
|
plan_kind: &str,
|
|
|
|
|
mut source: S,
|
|
|
|
|
) -> Result<LocalExecutionRequestOutcome, GatewayError>
|
|
|
|
|
where
|
|
|
|
|
T: AiExecutionAttempt + Send + Sync + 'static,
|
|
|
|
|
S: LocalExecutionAttemptSource<T>,
|
|
|
|
|
{
|
|
|
|
|
let span = tracing::debug_span!("candidates", trace_id = %trace_id, plan_kind);
|
|
|
|
|
|
|
|
|
|
async move {
|
|
|
|
|
tracing::debug!(
|
|
|
|
|
event_name = "candidate_loop_started",
|
|
|
|
|
log_type = "event",
|
|
|
|
|
trace_id = %trace_id,
|
|
|
|
|
plan_kind,
|
|
|
|
|
"dynamic candidate loop started"
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let port = SyncAttemptLoopPort {
|
|
|
|
|
state,
|
|
|
|
|
parts,
|
|
|
|
|
trace_id,
|
|
|
|
|
decision,
|
|
|
|
|
plan_kind,
|
|
|
|
|
};
|
2026-05-22 23:57:38 +08:00
|
|
|
run_dynamic_attempt_loop(
|
|
|
|
|
&port,
|
|
|
|
|
&mut source,
|
|
|
|
|
trace_id,
|
|
|
|
|
plan_kind,
|
|
|
|
|
state
|
|
|
|
|
.frontdoor_runtime_guards
|
|
|
|
|
.local_execution_planning_timeout,
|
|
|
|
|
)
|
|
|
|
|
.await
|
2026-05-03 20:14:29 +08:00
|
|
|
}
|
|
|
|
|
.instrument(span)
|
|
|
|
|
.await
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-02 13:23:54 +08:00
|
|
|
struct SyncAttemptLoopPort<'a> {
|
|
|
|
|
state: &'a AppState,
|
|
|
|
|
parts: &'a http::request::Parts,
|
|
|
|
|
trace_id: &'a str,
|
|
|
|
|
decision: &'a GatewayControlDecision,
|
|
|
|
|
plan_kind: &'a str,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
|
impl<T> AiAttemptLoopPort<T> for SyncAttemptLoopPort<'_>
|
|
|
|
|
where
|
|
|
|
|
T: AiExecutionAttempt + Send + Sync + 'static,
|
|
|
|
|
{
|
|
|
|
|
type Response = Response<Body>;
|
|
|
|
|
type Exhaustion = crate::executor::LocalExecutionExhaustion;
|
|
|
|
|
type Error = GatewayError;
|
|
|
|
|
|
|
|
|
|
async fn execute_attempt(&self, attempt: &T) -> Result<Option<Self::Response>, Self::Error> {
|
2026-06-24 01:47:26 +08:00
|
|
|
prewarm_direct_reqwest_candidate_client(attempt.execution_plan());
|
|
|
|
|
let _permit = acquire_upstream_execution_gate(self.state, self.trace_id).await?;
|
2026-05-13 18:25:13 +08:00
|
|
|
let mut response = execute_execution_runtime_sync(
|
2026-05-02 13:23:54 +08:00
|
|
|
self.state,
|
|
|
|
|
self.parts.uri.path(),
|
|
|
|
|
attempt.execution_plan().clone(),
|
|
|
|
|
self.trace_id,
|
|
|
|
|
self.decision,
|
|
|
|
|
self.plan_kind,
|
|
|
|
|
attempt.report_kind(),
|
|
|
|
|
attempt.report_context(),
|
|
|
|
|
)
|
2026-05-13 18:25:13 +08:00
|
|
|
.await?;
|
|
|
|
|
if let Some(response) = response.as_mut() {
|
|
|
|
|
attach_redaction_execution_candidate(
|
|
|
|
|
response,
|
|
|
|
|
attempt.execution_plan().candidate_id.as_deref(),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
Ok(response)
|
2026-05-02 13:23:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn mark_unused_attempts(&self, attempts: Vec<T>) -> Result<(), Self::Error> {
|
|
|
|
|
mark_unused_local_candidates(self.state, attempts).await;
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn build_exhaustion(
|
|
|
|
|
&self,
|
|
|
|
|
last_plan: aether_contracts::ExecutionPlan,
|
|
|
|
|
last_report_context: Option<serde_json::Value>,
|
|
|
|
|
) -> Result<Self::Exhaustion, Self::Error> {
|
2026-05-09 01:21:26 +08:00
|
|
|
warn!(
|
|
|
|
|
event_name = "candidate_loop_exhausted",
|
|
|
|
|
log_type = "ops",
|
|
|
|
|
trace_id = %self.trace_id,
|
|
|
|
|
plan_kind = self.plan_kind,
|
|
|
|
|
request_id = %short_request_id(last_plan.request_id.as_str()),
|
|
|
|
|
candidate_id = ?last_plan.candidate_id,
|
|
|
|
|
provider_name = last_plan.provider_name.as_deref().unwrap_or("-"),
|
|
|
|
|
endpoint_id = %last_plan.endpoint_id,
|
|
|
|
|
key_id = %last_plan.key_id,
|
|
|
|
|
model_name = last_plan.model_name.as_deref().unwrap_or("-"),
|
|
|
|
|
"candidate loop exhausted local sync candidates"
|
|
|
|
|
);
|
2026-05-02 13:23:54 +08:00
|
|
|
Ok(
|
|
|
|
|
build_local_execution_exhaustion(self.state, &last_plan, last_report_context.as_ref())
|
|
|
|
|
.await,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-03 14:59:58 +08:00
|
|
|
pub(crate) async fn execute_stream_plan_and_reports<T>(
|
|
|
|
|
state: &AppState,
|
|
|
|
|
trace_id: &str,
|
|
|
|
|
decision: &GatewayControlDecision,
|
|
|
|
|
plan_kind: &str,
|
|
|
|
|
plan_and_reports: Vec<T>,
|
2026-04-10 17:44:55 +08:00
|
|
|
) -> Result<LocalExecutionRequestOutcome, GatewayError>
|
2026-04-03 14:59:58 +08:00
|
|
|
where
|
2026-05-02 13:23:54 +08:00
|
|
|
T: AiExecutionAttempt + Send + Sync + 'static,
|
2026-04-03 14:59:58 +08:00
|
|
|
{
|
2026-04-11 01:50:24 +08:00
|
|
|
let candidate_count = plan_and_reports.len();
|
|
|
|
|
let first_provider = plan_and_reports
|
|
|
|
|
.first()
|
2026-05-02 13:23:54 +08:00
|
|
|
.and_then(|item| item.execution_plan().provider_name.as_deref())
|
2026-04-11 01:50:24 +08:00
|
|
|
.unwrap_or("-")
|
|
|
|
|
.to_string();
|
|
|
|
|
let span = tracing::debug_span!(
|
|
|
|
|
"candidates",
|
|
|
|
|
trace_id = %trace_id,
|
|
|
|
|
plan_kind,
|
|
|
|
|
candidate_count,
|
|
|
|
|
);
|
2026-04-03 14:59:58 +08:00
|
|
|
|
2026-04-11 01:50:24 +08:00
|
|
|
async move {
|
|
|
|
|
tracing::debug!(
|
|
|
|
|
event_name = "candidate_loop_started",
|
|
|
|
|
log_type = "event",
|
|
|
|
|
trace_id = %trace_id,
|
|
|
|
|
plan_kind,
|
|
|
|
|
candidate_count,
|
|
|
|
|
first_provider = first_provider.as_str(),
|
|
|
|
|
"candidate loop started"
|
|
|
|
|
);
|
|
|
|
|
|
2026-05-02 13:23:54 +08:00
|
|
|
let port = StreamAttemptLoopPort {
|
|
|
|
|
state,
|
|
|
|
|
trace_id,
|
|
|
|
|
decision,
|
2026-04-11 01:50:24 +08:00
|
|
|
plan_kind,
|
2026-05-02 13:23:54 +08:00
|
|
|
};
|
|
|
|
|
match run_ai_attempt_loop(&port, plan_and_reports).await? {
|
|
|
|
|
AiAttemptLoopOutcome::Responded(response) => {
|
|
|
|
|
Ok(LocalExecutionRequestOutcome::responded(response))
|
|
|
|
|
}
|
|
|
|
|
AiAttemptLoopOutcome::Exhausted(exhaustion) => {
|
|
|
|
|
Ok(LocalExecutionRequestOutcome::Exhausted(exhaustion))
|
|
|
|
|
}
|
|
|
|
|
AiAttemptLoopOutcome::NoPath => Ok(LocalExecutionRequestOutcome::NoPath),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
.instrument(span)
|
|
|
|
|
.await
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-03 20:14:29 +08:00
|
|
|
pub(crate) async fn execute_stream_attempt_source<T, S>(
|
|
|
|
|
state: &AppState,
|
|
|
|
|
trace_id: &str,
|
|
|
|
|
decision: &GatewayControlDecision,
|
|
|
|
|
plan_kind: &str,
|
|
|
|
|
mut source: S,
|
|
|
|
|
) -> Result<LocalExecutionRequestOutcome, GatewayError>
|
|
|
|
|
where
|
|
|
|
|
T: AiExecutionAttempt + Send + Sync + 'static,
|
|
|
|
|
S: LocalExecutionAttemptSource<T>,
|
|
|
|
|
{
|
|
|
|
|
let span = tracing::debug_span!("candidates", trace_id = %trace_id, plan_kind);
|
|
|
|
|
|
|
|
|
|
async move {
|
|
|
|
|
tracing::debug!(
|
|
|
|
|
event_name = "candidate_loop_started",
|
|
|
|
|
log_type = "event",
|
|
|
|
|
trace_id = %trace_id,
|
|
|
|
|
plan_kind,
|
|
|
|
|
"dynamic candidate loop started"
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let port = StreamAttemptLoopPort {
|
|
|
|
|
state,
|
|
|
|
|
trace_id,
|
|
|
|
|
decision,
|
|
|
|
|
plan_kind,
|
|
|
|
|
};
|
2026-05-22 23:57:38 +08:00
|
|
|
run_dynamic_attempt_loop(
|
|
|
|
|
&port,
|
|
|
|
|
&mut source,
|
|
|
|
|
trace_id,
|
|
|
|
|
plan_kind,
|
|
|
|
|
state
|
|
|
|
|
.frontdoor_runtime_guards
|
|
|
|
|
.local_execution_planning_timeout,
|
|
|
|
|
)
|
|
|
|
|
.await
|
2026-05-03 20:14:29 +08:00
|
|
|
}
|
|
|
|
|
.instrument(span)
|
|
|
|
|
.await
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn run_dynamic_attempt_loop<Port, Source, Attempt>(
|
|
|
|
|
port: &Port,
|
|
|
|
|
source: &mut Source,
|
2026-05-22 23:57:38 +08:00
|
|
|
trace_id: &str,
|
|
|
|
|
plan_kind: &str,
|
|
|
|
|
planning_timeout: Duration,
|
2026-05-03 20:14:29 +08:00
|
|
|
) -> Result<LocalExecutionRequestOutcome, GatewayError>
|
|
|
|
|
where
|
|
|
|
|
Port: AiAttemptLoopPort<
|
|
|
|
|
Attempt,
|
|
|
|
|
Response = Response<Body>,
|
|
|
|
|
Exhaustion = crate::executor::LocalExecutionExhaustion,
|
|
|
|
|
Error = GatewayError,
|
|
|
|
|
>,
|
|
|
|
|
Source: LocalExecutionAttemptSource<Attempt>,
|
|
|
|
|
Attempt: AiExecutionAttempt + Send + Sync + 'static,
|
|
|
|
|
{
|
|
|
|
|
let mut last_attempted = None;
|
|
|
|
|
|
2026-06-24 01:47:26 +08:00
|
|
|
loop {
|
|
|
|
|
let next_started_at = std::time::Instant::now();
|
|
|
|
|
let next_attempt =
|
|
|
|
|
next_execution_attempt_with_timeout(source, trace_id, plan_kind, planning_timeout)
|
|
|
|
|
.await?;
|
|
|
|
|
observe_gateway_stage_ms(
|
|
|
|
|
"stream_candidate_next",
|
|
|
|
|
next_started_at.elapsed().as_millis() as u64,
|
|
|
|
|
);
|
|
|
|
|
let Some(attempt) = next_attempt else {
|
|
|
|
|
break;
|
|
|
|
|
};
|
2026-05-03 20:14:29 +08:00
|
|
|
last_attempted = Some((attempt.execution_plan().clone(), attempt.report_context()));
|
2026-06-24 01:47:26 +08:00
|
|
|
let execute_started_at = std::time::Instant::now();
|
|
|
|
|
let response = port.execute_attempt(&attempt).await?;
|
|
|
|
|
observe_gateway_stage_ms(
|
|
|
|
|
"stream_candidate_execute",
|
|
|
|
|
execute_started_at.elapsed().as_millis() as u64,
|
|
|
|
|
);
|
|
|
|
|
if let Some(response) = response {
|
2026-05-03 20:14:29 +08:00
|
|
|
let remaining = source.drain_execution_attempts().await?;
|
2026-06-24 01:47:26 +08:00
|
|
|
let unused_started_at = std::time::Instant::now();
|
2026-05-03 20:14:29 +08:00
|
|
|
port.mark_unused_attempts(remaining).await?;
|
2026-06-24 01:47:26 +08:00
|
|
|
observe_gateway_stage_ms(
|
|
|
|
|
"stream_candidate_unused",
|
|
|
|
|
unused_started_at.elapsed().as_millis() as u64,
|
|
|
|
|
);
|
2026-05-03 20:14:29 +08:00
|
|
|
return Ok(LocalExecutionRequestOutcome::responded(response));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let Some((last_plan, last_report_context)) = last_attempted else {
|
|
|
|
|
return Ok(LocalExecutionRequestOutcome::NoPath);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Ok(LocalExecutionRequestOutcome::Exhausted(
|
|
|
|
|
port.build_exhaustion(last_plan, last_report_context)
|
|
|
|
|
.await?,
|
|
|
|
|
))
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-22 23:57:38 +08:00
|
|
|
async fn next_execution_attempt_with_timeout<Source, Attempt>(
|
|
|
|
|
source: &mut Source,
|
|
|
|
|
trace_id: &str,
|
|
|
|
|
plan_kind: &str,
|
|
|
|
|
planning_timeout: Duration,
|
|
|
|
|
) -> Result<Option<Attempt>, GatewayError>
|
|
|
|
|
where
|
|
|
|
|
Source: LocalExecutionAttemptSource<Attempt>,
|
|
|
|
|
{
|
|
|
|
|
match timeout(planning_timeout, source.next_execution_attempt()).await {
|
|
|
|
|
Ok(result) => result,
|
|
|
|
|
Err(_) => {
|
|
|
|
|
let timeout_ms = planning_timeout.as_millis() as u64;
|
|
|
|
|
warn!(
|
|
|
|
|
event_name = "local_execution_candidate_planning_timeout",
|
|
|
|
|
log_type = "ops",
|
|
|
|
|
trace_id,
|
|
|
|
|
plan_kind,
|
|
|
|
|
timeout_ms,
|
|
|
|
|
phase = "next_execution_attempt",
|
|
|
|
|
"gateway timed out while planning the next local execution candidate"
|
|
|
|
|
);
|
|
|
|
|
Err(GatewayError::LocalExecutionPlanningTimeout {
|
|
|
|
|
trace_id: trace_id.to_string(),
|
|
|
|
|
phase: "next_execution_attempt",
|
|
|
|
|
timeout_ms,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-02 13:23:54 +08:00
|
|
|
struct StreamAttemptLoopPort<'a> {
|
|
|
|
|
state: &'a AppState,
|
|
|
|
|
trace_id: &'a str,
|
|
|
|
|
decision: &'a GatewayControlDecision,
|
|
|
|
|
plan_kind: &'a str,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
|
impl<T> AiAttemptLoopPort<T> for StreamAttemptLoopPort<'_>
|
|
|
|
|
where
|
|
|
|
|
T: AiExecutionAttempt + Send + Sync + 'static,
|
|
|
|
|
{
|
|
|
|
|
type Response = Response<Body>;
|
|
|
|
|
type Exhaustion = crate::executor::LocalExecutionExhaustion;
|
|
|
|
|
type Error = GatewayError;
|
|
|
|
|
|
|
|
|
|
async fn execute_attempt(&self, attempt: &T) -> Result<Option<Self::Response>, Self::Error> {
|
|
|
|
|
let plan = attempt.execution_plan().clone();
|
|
|
|
|
let report_context = attempt.report_context();
|
|
|
|
|
let candidate_index = parse_request_candidate_report_context(report_context.as_ref())
|
|
|
|
|
.and_then(|context| context.candidate_index)
|
|
|
|
|
.map(|value| value.to_string())
|
|
|
|
|
.unwrap_or_else(|| "-".to_string());
|
|
|
|
|
debug!(
|
|
|
|
|
event_name = "candidate_loop_attempt_started",
|
|
|
|
|
log_type = "debug",
|
|
|
|
|
trace_id = %self.trace_id,
|
|
|
|
|
plan_kind = self.plan_kind,
|
2026-04-11 01:50:24 +08:00
|
|
|
request_id = %short_request_id(plan.request_id.as_str()),
|
|
|
|
|
candidate_id = ?plan.candidate_id,
|
|
|
|
|
provider_name = plan.provider_name.as_deref().unwrap_or("-"),
|
|
|
|
|
endpoint_id = %plan.endpoint_id,
|
|
|
|
|
key_id = %plan.key_id,
|
|
|
|
|
model_name = plan.model_name.as_deref().unwrap_or("-"),
|
2026-05-02 13:23:54 +08:00
|
|
|
candidate_index = candidate_index.as_str(),
|
|
|
|
|
"candidate loop attempting stream execution candidate"
|
|
|
|
|
);
|
2026-06-24 01:47:26 +08:00
|
|
|
prewarm_direct_reqwest_candidate_client(&plan);
|
2026-05-02 13:23:54 +08:00
|
|
|
let watchdog_plan = plan.clone();
|
|
|
|
|
let watchdog_report_context = report_context.clone();
|
|
|
|
|
let execution_state = self.state.clone();
|
|
|
|
|
let execution_trace_id = self.trace_id.to_string();
|
|
|
|
|
let execution_plan_kind = self.plan_kind.to_string();
|
|
|
|
|
let execution_decision = self.decision.clone();
|
|
|
|
|
let execution_report_kind = attempt.report_kind();
|
2026-05-13 18:25:13 +08:00
|
|
|
let mut response = execute_stream_candidate_with_watchdog(
|
2026-05-02 13:23:54 +08:00
|
|
|
self.state,
|
|
|
|
|
self.trace_id,
|
|
|
|
|
self.plan_kind,
|
|
|
|
|
&watchdog_plan,
|
|
|
|
|
watchdog_report_context.as_ref(),
|
|
|
|
|
move || async move {
|
|
|
|
|
execute_execution_runtime_stream(
|
|
|
|
|
&execution_state,
|
|
|
|
|
plan,
|
|
|
|
|
execution_trace_id.as_str(),
|
|
|
|
|
&execution_decision,
|
|
|
|
|
execution_plan_kind.as_str(),
|
|
|
|
|
execution_report_kind,
|
|
|
|
|
report_context,
|
|
|
|
|
)
|
|
|
|
|
.await
|
|
|
|
|
},
|
|
|
|
|
)
|
2026-05-13 18:25:13 +08:00
|
|
|
.await?;
|
|
|
|
|
if let Some(response) = response.as_mut() {
|
|
|
|
|
attach_redaction_execution_candidate(response, watchdog_plan.candidate_id.as_deref());
|
|
|
|
|
}
|
|
|
|
|
Ok(response)
|
2026-05-02 13:23:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn mark_unused_attempts(&self, attempts: Vec<T>) -> Result<(), Self::Error> {
|
|
|
|
|
mark_unused_local_candidates(self.state, attempts).await;
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn build_exhaustion(
|
|
|
|
|
&self,
|
|
|
|
|
last_plan: aether_contracts::ExecutionPlan,
|
|
|
|
|
last_report_context: Option<serde_json::Value>,
|
|
|
|
|
) -> Result<Self::Exhaustion, Self::Error> {
|
|
|
|
|
warn!(
|
|
|
|
|
event_name = "candidate_loop_exhausted",
|
|
|
|
|
log_type = "ops",
|
|
|
|
|
trace_id = %self.trace_id,
|
|
|
|
|
plan_kind = self.plan_kind,
|
|
|
|
|
request_id = %short_request_id(last_plan.request_id.as_str()),
|
|
|
|
|
candidate_id = ?last_plan.candidate_id,
|
|
|
|
|
provider_name = last_plan.provider_name.as_deref().unwrap_or("-"),
|
|
|
|
|
endpoint_id = %last_plan.endpoint_id,
|
|
|
|
|
key_id = %last_plan.key_id,
|
|
|
|
|
model_name = last_plan.model_name.as_deref().unwrap_or("-"),
|
2026-04-11 01:50:24 +08:00
|
|
|
"candidate loop exhausted local stream candidates"
|
|
|
|
|
);
|
2026-05-02 13:23:54 +08:00
|
|
|
Ok(
|
|
|
|
|
build_local_execution_exhaustion(self.state, &last_plan, last_report_context.as_ref())
|
|
|
|
|
.await,
|
|
|
|
|
)
|
2026-04-11 01:50:24 +08:00
|
|
|
}
|
2026-04-03 14:59:58 +08:00
|
|
|
}
|
|
|
|
|
|
2026-06-24 01:47:26 +08:00
|
|
|
fn prewarm_direct_reqwest_candidate_client(plan: &aether_contracts::ExecutionPlan) {
|
|
|
|
|
let started_at = std::time::Instant::now();
|
|
|
|
|
crate::execution_runtime::transport::prewarm_direct_reqwest_client_cache_for_plan(plan);
|
|
|
|
|
observe_gateway_stage_ms(
|
|
|
|
|
"direct_reqwest_client_prewarm",
|
|
|
|
|
started_at.elapsed().as_millis() as u64,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-03 14:59:58 +08:00
|
|
|
pub(crate) async fn mark_unused_local_candidates<T>(state: &AppState, remaining: Vec<T>)
|
|
|
|
|
where
|
2026-05-02 13:23:54 +08:00
|
|
|
T: AiExecutionAttempt,
|
2026-04-03 14:59:58 +08:00
|
|
|
{
|
|
|
|
|
for plan_and_report in remaining {
|
2026-04-24 13:29:05 +08:00
|
|
|
let report_context = plan_and_report.report_context();
|
2026-05-11 00:12:05 +08:00
|
|
|
let metadata =
|
|
|
|
|
local_execution_candidate_metadata_from_report_context(report_context.as_ref());
|
|
|
|
|
if let Some(lease) = metadata.pool_key_lease.as_ref() {
|
|
|
|
|
if let Err(err) =
|
|
|
|
|
release_admin_provider_pool_key_lease(state.runtime_state.as_ref(), lease).await
|
|
|
|
|
{
|
|
|
|
|
warn!(
|
|
|
|
|
error = ?err,
|
|
|
|
|
"gateway candidate loop: failed to release unused pool key lease"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if should_skip_unused_persistence_from_metadata(&metadata) {
|
2026-04-24 13:29:05 +08:00
|
|
|
continue;
|
|
|
|
|
}
|
2026-04-03 14:59:58 +08:00
|
|
|
record_local_request_candidate_status(
|
|
|
|
|
state,
|
2026-05-02 13:23:54 +08:00
|
|
|
plan_and_report.execution_plan(),
|
2026-04-24 13:29:05 +08:00
|
|
|
report_context.as_ref(),
|
2026-04-09 13:51:50 +08:00
|
|
|
SchedulerRequestCandidateStatusUpdate {
|
|
|
|
|
status: RequestCandidateStatus::Unused,
|
|
|
|
|
status_code: None,
|
|
|
|
|
error_type: None,
|
|
|
|
|
error_message: None,
|
|
|
|
|
latency_ms: None,
|
2026-04-10 01:46:14 +08:00
|
|
|
started_at_unix_ms: None,
|
|
|
|
|
finished_at_unix_ms: None,
|
2026-04-09 13:51:50 +08:00
|
|
|
},
|
2026-04-07 02:50:19 +08:00
|
|
|
)
|
|
|
|
|
.await;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-24 13:29:05 +08:00
|
|
|
fn should_skip_unused_persistence(report_context: Option<&serde_json::Value>) -> bool {
|
|
|
|
|
let metadata = local_execution_candidate_metadata_from_report_context(report_context);
|
2026-05-11 00:12:05 +08:00
|
|
|
should_skip_unused_persistence_from_metadata(&metadata)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn should_skip_unused_persistence_from_metadata(
|
|
|
|
|
metadata: &crate::orchestration::LocalExecutionCandidateMetadata,
|
|
|
|
|
) -> bool {
|
2026-05-03 20:14:29 +08:00
|
|
|
metadata.candidate_group_id.is_some() && metadata.pool_key_index.is_some()
|
2026-04-24 13:29:05 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-20 15:08:44 +08:00
|
|
|
fn resolve_stream_candidate_watchdog_timeout(
|
|
|
|
|
plan: &aether_contracts::ExecutionPlan,
|
2026-05-25 21:43:13 +08:00
|
|
|
_report_context: Option<&serde_json::Value>,
|
2026-05-20 15:08:44 +08:00
|
|
|
) -> Duration {
|
2026-05-25 21:43:13 +08:00
|
|
|
let timeout_ms = plan
|
|
|
|
|
.timeouts
|
|
|
|
|
.as_ref()
|
|
|
|
|
.and_then(|timeouts| timeouts.first_byte_ms)
|
|
|
|
|
.unwrap_or(DEFAULT_STREAM_FIRST_BYTE_WATCHDOG_TIMEOUT_MS)
|
|
|
|
|
.max(1);
|
2026-04-11 01:50:24 +08:00
|
|
|
Duration::from_millis(timeout_ms)
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-26 15:02:50 +08:00
|
|
|
fn stream_candidate_watchdog_timeout_message() -> &'static str {
|
|
|
|
|
"Stream first byte timeout"
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-11 01:50:24 +08:00
|
|
|
async fn execute_stream_candidate_with_watchdog<Fut>(
|
2026-06-24 01:47:26 +08:00
|
|
|
state: &(impl RequestCandidateRuntimeWriter + UpstreamExecutionGateProvider + ?Sized),
|
2026-04-11 01:50:24 +08:00
|
|
|
trace_id: &str,
|
|
|
|
|
plan_kind: &str,
|
|
|
|
|
plan: &aether_contracts::ExecutionPlan,
|
|
|
|
|
report_context: Option<&serde_json::Value>,
|
|
|
|
|
execute: impl FnOnce() -> Fut,
|
|
|
|
|
) -> Result<Option<Response<Body>>, GatewayError>
|
|
|
|
|
where
|
|
|
|
|
Fut:
|
|
|
|
|
std::future::Future<Output = Result<Option<Response<Body>>, GatewayError>> + Send + 'static,
|
|
|
|
|
{
|
2026-05-20 15:08:44 +08:00
|
|
|
let timeout_duration = resolve_stream_candidate_watchdog_timeout(plan, report_context);
|
2026-04-11 01:50:24 +08:00
|
|
|
let candidate_started_unix_ms = current_unix_ms();
|
2026-06-24 01:47:26 +08:00
|
|
|
let permit = acquire_upstream_execution_gate(state, trace_id).await?;
|
2026-04-11 01:50:24 +08:00
|
|
|
let mut join_handle = tokio::spawn(execute());
|
|
|
|
|
match timeout(timeout_duration, &mut join_handle).await {
|
2026-06-24 01:47:26 +08:00
|
|
|
Ok(Ok(result)) => {
|
|
|
|
|
result.map(|response| maybe_hold_upstream_execution_permit(response, permit))
|
|
|
|
|
}
|
2026-04-11 01:50:24 +08:00
|
|
|
Ok(Err(join_error)) => Err(GatewayError::Internal(format!(
|
|
|
|
|
"local stream candidate task join failed: {join_error}"
|
|
|
|
|
))),
|
|
|
|
|
Err(_) => {
|
|
|
|
|
join_handle.abort();
|
|
|
|
|
let finished_at_unix_ms = current_unix_ms();
|
|
|
|
|
let request_id = short_request_id(plan.request_id.as_str());
|
|
|
|
|
let provider_name = plan.provider_name.as_deref().unwrap_or("-");
|
|
|
|
|
let model_name = plan.model_name.as_deref().unwrap_or("-");
|
|
|
|
|
let candidate_index = parse_request_candidate_report_context(report_context)
|
|
|
|
|
.and_then(|context| context.candidate_index)
|
|
|
|
|
.map(|value| value.to_string())
|
|
|
|
|
.unwrap_or_else(|| "-".to_string());
|
|
|
|
|
let timeout_ms = u64::try_from(timeout_duration.as_millis()).unwrap_or(u64::MAX);
|
|
|
|
|
record_local_request_candidate_status(
|
|
|
|
|
state,
|
|
|
|
|
plan,
|
|
|
|
|
report_context,
|
|
|
|
|
SchedulerRequestCandidateStatusUpdate {
|
|
|
|
|
status: RequestCandidateStatus::Failed,
|
|
|
|
|
status_code: Some(http::StatusCode::GATEWAY_TIMEOUT.as_u16()),
|
|
|
|
|
error_type: Some("local_stream_candidate_watchdog_timeout".to_string()),
|
2026-05-26 15:02:50 +08:00
|
|
|
error_message: Some(stream_candidate_watchdog_timeout_message().to_string()),
|
2026-04-11 01:50:24 +08:00
|
|
|
latency_ms: None,
|
|
|
|
|
started_at_unix_ms: Some(candidate_started_unix_ms),
|
|
|
|
|
finished_at_unix_ms: Some(finished_at_unix_ms),
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
.await;
|
|
|
|
|
warn!(
|
|
|
|
|
event_name = "local_stream_candidate_watchdog_timed_out",
|
|
|
|
|
log_type = "event",
|
|
|
|
|
trace_id = %trace_id,
|
|
|
|
|
plan_kind,
|
|
|
|
|
request_id = %request_id,
|
|
|
|
|
candidate_id = ?plan.candidate_id,
|
|
|
|
|
provider_name,
|
|
|
|
|
endpoint_id = %plan.endpoint_id,
|
|
|
|
|
key_id = %plan.key_id,
|
|
|
|
|
model_name,
|
|
|
|
|
candidate_index = candidate_index.as_str(),
|
|
|
|
|
timeout_ms,
|
|
|
|
|
"gateway local stream candidate watchdog timed out"
|
|
|
|
|
);
|
|
|
|
|
Ok(None)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-24 01:47:26 +08:00
|
|
|
fn maybe_hold_upstream_execution_permit(
|
|
|
|
|
response: Option<Response<Body>>,
|
|
|
|
|
permit: Option<ConcurrencyPermit>,
|
|
|
|
|
) -> Option<Response<Body>> {
|
|
|
|
|
match (response, permit) {
|
|
|
|
|
(Some(response), Some(permit)) => {
|
|
|
|
|
Some(hold_response_upstream_execution_permit(response, permit))
|
|
|
|
|
}
|
|
|
|
|
(response, _) => response,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn hold_response_upstream_execution_permit(
|
|
|
|
|
response: Response<Body>,
|
|
|
|
|
permit: ConcurrencyPermit,
|
|
|
|
|
) -> Response<Body> {
|
|
|
|
|
let (parts, body) = response.into_parts();
|
|
|
|
|
let stream = async_stream::stream! {
|
|
|
|
|
let _permit = permit;
|
|
|
|
|
let mut body_stream = body.into_data_stream();
|
|
|
|
|
while let Some(item) = body_stream.next().await {
|
|
|
|
|
yield item;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
Response::from_parts(parts, Body::from_stream(stream))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
trait UpstreamExecutionGateProvider {
|
|
|
|
|
fn upstream_execution_gate(&self) -> Option<&aether_runtime::ConcurrencyGate>;
|
|
|
|
|
fn upstream_execution_gate_queue_budget(&self) -> Duration;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl UpstreamExecutionGateProvider for AppState {
|
|
|
|
|
fn upstream_execution_gate(&self) -> Option<&aether_runtime::ConcurrencyGate> {
|
|
|
|
|
self.upstream_execution_gate.as_deref()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn upstream_execution_gate_queue_budget(&self) -> Duration {
|
|
|
|
|
self.frontdoor_runtime_guards.internal_gate_queue_budget
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn acquire_upstream_execution_gate(
|
|
|
|
|
state: &(impl UpstreamExecutionGateProvider + ?Sized),
|
|
|
|
|
trace_id: &str,
|
|
|
|
|
) -> Result<Option<ConcurrencyPermit>, GatewayError> {
|
|
|
|
|
let Some(gate) = state.upstream_execution_gate() else {
|
|
|
|
|
return Ok(None);
|
|
|
|
|
};
|
|
|
|
|
let budget = state.upstream_execution_gate_queue_budget();
|
|
|
|
|
match timeout(budget, gate.acquire()).await {
|
|
|
|
|
Ok(Ok(permit)) => Ok(Some(permit)),
|
|
|
|
|
Ok(Err(err)) => Err(GatewayError::Internal(err.to_string())),
|
|
|
|
|
Err(_) => Err(GatewayError::AdmissionTimeout {
|
|
|
|
|
trace_id: trace_id.to_string(),
|
|
|
|
|
gate: "gateway_upstream_execution",
|
|
|
|
|
queue_budget_ms: budget.as_millis() as u64,
|
|
|
|
|
}),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-07 02:50:19 +08:00
|
|
|
pub(crate) async fn mark_unused_local_candidate_items<T, FPlan, FContext>(
|
|
|
|
|
state: &AppState,
|
|
|
|
|
remaining: Vec<T>,
|
|
|
|
|
plan: FPlan,
|
|
|
|
|
report_context: FContext,
|
|
|
|
|
) where
|
|
|
|
|
FPlan: Fn(&T) -> &aether_contracts::ExecutionPlan,
|
|
|
|
|
FContext: Fn(&T) -> Option<&serde_json::Value>,
|
|
|
|
|
{
|
|
|
|
|
for item in remaining {
|
2026-04-24 13:29:05 +08:00
|
|
|
let report_context = report_context(&item);
|
|
|
|
|
if should_skip_unused_persistence(report_context) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2026-04-07 02:50:19 +08:00
|
|
|
record_local_request_candidate_status(
|
|
|
|
|
state,
|
|
|
|
|
plan(&item),
|
2026-04-24 13:29:05 +08:00
|
|
|
report_context,
|
2026-04-09 13:51:50 +08:00
|
|
|
SchedulerRequestCandidateStatusUpdate {
|
|
|
|
|
status: RequestCandidateStatus::Unused,
|
|
|
|
|
status_code: None,
|
|
|
|
|
error_type: None,
|
|
|
|
|
error_message: None,
|
|
|
|
|
latency_ms: None,
|
2026-04-10 01:46:14 +08:00
|
|
|
started_at_unix_ms: None,
|
|
|
|
|
finished_at_unix_ms: None,
|
2026-04-09 13:51:50 +08:00
|
|
|
},
|
2026-04-03 14:59:58 +08:00
|
|
|
)
|
|
|
|
|
.await;
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-04-11 01:50:24 +08:00
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
|
|
use aether_contracts::{ExecutionPlan, ExecutionTimeouts, RequestBody};
|
|
|
|
|
use aether_data_contracts::repository::candidates::{
|
|
|
|
|
RequestCandidateStatus, UpsertRequestCandidateRecord,
|
|
|
|
|
};
|
|
|
|
|
use async_trait::async_trait;
|
|
|
|
|
use serde_json::json;
|
|
|
|
|
use tokio::sync::Mutex;
|
|
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Default)]
|
|
|
|
|
struct TestRequestCandidateWriter {
|
|
|
|
|
records: Mutex<Vec<UpsertRequestCandidateRecord>>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
|
impl RequestCandidateRuntimeWriter for TestRequestCandidateWriter {
|
|
|
|
|
fn has_request_candidate_data_writer(&self) -> bool {
|
|
|
|
|
true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn upsert_request_candidate(
|
|
|
|
|
&self,
|
|
|
|
|
candidate: UpsertRequestCandidateRecord,
|
|
|
|
|
) -> Result<
|
|
|
|
|
Option<aether_data_contracts::repository::candidates::StoredRequestCandidate>,
|
|
|
|
|
GatewayError,
|
|
|
|
|
> {
|
|
|
|
|
self.records.lock().await.push(candidate);
|
|
|
|
|
Ok(None)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-24 01:47:26 +08:00
|
|
|
impl UpstreamExecutionGateProvider for TestRequestCandidateWriter {
|
|
|
|
|
fn upstream_execution_gate(&self) -> Option<&aether_runtime::ConcurrencyGate> {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn upstream_execution_gate_queue_budget(&self) -> Duration {
|
|
|
|
|
Duration::from_millis(250)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-22 23:57:38 +08:00
|
|
|
struct PendingAttemptSource;
|
|
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
|
impl LocalExecutionAttemptSource<()> for PendingAttemptSource {
|
|
|
|
|
async fn next_execution_attempt(&mut self) -> Result<Option<()>, GatewayError> {
|
|
|
|
|
std::future::pending::<()>().await;
|
|
|
|
|
Ok(None)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn drain_execution_attempts(&mut self) -> Result<Vec<()>, GatewayError> {
|
|
|
|
|
Ok(Vec::new())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-11 01:50:24 +08:00
|
|
|
fn test_plan(timeouts: Option<ExecutionTimeouts>) -> ExecutionPlan {
|
|
|
|
|
ExecutionPlan {
|
|
|
|
|
request_id: "req_watchdog".to_string(),
|
|
|
|
|
candidate_id: Some("cand_watchdog".to_string()),
|
|
|
|
|
provider_name: Some("provider".to_string()),
|
|
|
|
|
provider_id: "provider_id".to_string(),
|
|
|
|
|
endpoint_id: "endpoint_id".to_string(),
|
|
|
|
|
key_id: "key_id".to_string(),
|
|
|
|
|
method: "POST".to_string(),
|
|
|
|
|
url: "https://example.com/v1/messages".to_string(),
|
|
|
|
|
headers: Default::default(),
|
|
|
|
|
content_type: Some("application/json".to_string()),
|
|
|
|
|
content_encoding: None,
|
|
|
|
|
body: RequestBody::from_json(json!({"model": "gpt-test"})),
|
|
|
|
|
stream: true,
|
2026-04-29 09:25:19 +08:00
|
|
|
client_api_format: "claude:messages".to_string(),
|
2026-04-11 01:50:24 +08:00
|
|
|
provider_api_format: "openai:chat".to_string(),
|
|
|
|
|
model_name: Some("gpt-test".to_string()),
|
|
|
|
|
proxy: None,
|
2026-05-05 22:21:23 +08:00
|
|
|
transport_profile: None,
|
2026-04-11 01:50:24 +08:00
|
|
|
timeouts,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-22 23:57:38 +08:00
|
|
|
#[tokio::test]
|
|
|
|
|
async fn next_execution_attempt_times_out_instead_of_waiting_forever() {
|
|
|
|
|
let mut source = PendingAttemptSource;
|
|
|
|
|
|
|
|
|
|
let err = next_execution_attempt_with_timeout(
|
|
|
|
|
&mut source,
|
|
|
|
|
"trace-planning-timeout",
|
|
|
|
|
"openai_responses_sync",
|
|
|
|
|
Duration::from_millis(5),
|
|
|
|
|
)
|
|
|
|
|
.await
|
|
|
|
|
.expect_err("pending candidate planning should time out");
|
|
|
|
|
|
|
|
|
|
match err {
|
|
|
|
|
GatewayError::LocalExecutionPlanningTimeout {
|
|
|
|
|
trace_id,
|
|
|
|
|
phase,
|
|
|
|
|
timeout_ms,
|
|
|
|
|
} => {
|
|
|
|
|
assert_eq!(trace_id, "trace-planning-timeout");
|
|
|
|
|
assert_eq!(phase, "next_execution_attempt");
|
|
|
|
|
assert_eq!(timeout_ms, 5);
|
|
|
|
|
}
|
|
|
|
|
other => panic!("unexpected error: {other:?}"),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-11 01:50:24 +08:00
|
|
|
fn test_report_context() -> serde_json::Value {
|
|
|
|
|
json!({
|
|
|
|
|
"request_id": "req_watchdog",
|
|
|
|
|
"candidate_id": "cand_watchdog",
|
|
|
|
|
"candidate_index": 2,
|
|
|
|
|
"retry_index": 0,
|
|
|
|
|
"user_id": "user_1",
|
|
|
|
|
"api_key_id": "api_key_1",
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn stream_candidate_watchdog_prefers_first_byte_timeout() {
|
2026-05-20 15:08:44 +08:00
|
|
|
let report_context = json!({"upstream_is_stream": true});
|
|
|
|
|
let timeout = resolve_stream_candidate_watchdog_timeout(
|
|
|
|
|
&test_plan(Some(ExecutionTimeouts {
|
2026-04-11 01:50:24 +08:00
|
|
|
first_byte_ms: Some(12_345),
|
|
|
|
|
total_ms: Some(90_000),
|
|
|
|
|
..ExecutionTimeouts::default()
|
2026-05-20 15:08:44 +08:00
|
|
|
})),
|
|
|
|
|
Some(&report_context),
|
|
|
|
|
);
|
2026-04-11 01:50:24 +08:00
|
|
|
|
|
|
|
|
assert_eq!(timeout, Duration::from_millis(12_345));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn stream_candidate_watchdog_uses_default_when_timeouts_missing() {
|
2026-05-20 15:08:44 +08:00
|
|
|
let timeout = resolve_stream_candidate_watchdog_timeout(&test_plan(None), None);
|
2026-04-11 01:50:24 +08:00
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
|
timeout,
|
2026-05-25 20:09:37 +08:00
|
|
|
Duration::from_millis(DEFAULT_STREAM_FIRST_BYTE_WATCHDOG_TIMEOUT_MS)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn stream_candidate_watchdog_ignores_total_timeout_for_stream_upstream() {
|
|
|
|
|
let report_context = json!({"upstream_is_stream": true});
|
|
|
|
|
let timeout = resolve_stream_candidate_watchdog_timeout(
|
|
|
|
|
&test_plan(Some(ExecutionTimeouts {
|
|
|
|
|
total_ms: Some(90_000),
|
|
|
|
|
..ExecutionTimeouts::default()
|
|
|
|
|
})),
|
|
|
|
|
Some(&report_context),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
|
timeout,
|
|
|
|
|
Duration::from_millis(DEFAULT_STREAM_FIRST_BYTE_WATCHDOG_TIMEOUT_MS)
|
2026-04-11 01:50:24 +08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-20 15:08:44 +08:00
|
|
|
#[test]
|
2026-05-25 21:43:13 +08:00
|
|
|
fn stream_candidate_watchdog_prefers_first_byte_timeout_when_upstream_non_stream() {
|
2026-05-20 15:08:44 +08:00
|
|
|
let report_context = json!({"upstream_is_stream": false});
|
|
|
|
|
let timeout = resolve_stream_candidate_watchdog_timeout(
|
|
|
|
|
&test_plan(Some(ExecutionTimeouts {
|
2026-05-25 21:43:13 +08:00
|
|
|
first_byte_ms: Some(12_345),
|
2026-05-20 15:08:44 +08:00
|
|
|
total_ms: Some(599_000),
|
|
|
|
|
..ExecutionTimeouts::default()
|
|
|
|
|
})),
|
|
|
|
|
Some(&report_context),
|
|
|
|
|
);
|
|
|
|
|
|
2026-05-25 21:43:13 +08:00
|
|
|
assert_eq!(timeout, Duration::from_millis(12_345));
|
2026-05-20 15:08:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2026-05-25 21:43:13 +08:00
|
|
|
fn stream_candidate_watchdog_ignores_total_timeout_when_upstream_non_stream() {
|
2026-05-20 15:08:44 +08:00
|
|
|
let report_context = json!({"upstream_is_stream": false});
|
|
|
|
|
let timeout = resolve_stream_candidate_watchdog_timeout(
|
|
|
|
|
&test_plan(Some(ExecutionTimeouts {
|
2026-05-25 21:43:13 +08:00
|
|
|
total_ms: Some(599_000),
|
2026-05-20 15:08:44 +08:00
|
|
|
..ExecutionTimeouts::default()
|
|
|
|
|
})),
|
|
|
|
|
Some(&report_context),
|
|
|
|
|
);
|
|
|
|
|
|
2026-05-25 20:09:37 +08:00
|
|
|
assert_eq!(
|
|
|
|
|
timeout,
|
2026-05-25 21:43:13 +08:00
|
|
|
Duration::from_millis(DEFAULT_STREAM_FIRST_BYTE_WATCHDOG_TIMEOUT_MS)
|
2026-05-25 20:09:37 +08:00
|
|
|
);
|
2026-05-20 15:08:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn stream_candidate_watchdog_defaults_to_streaming_when_flag_missing() {
|
|
|
|
|
let report_context = json!({});
|
|
|
|
|
let timeout = resolve_stream_candidate_watchdog_timeout(
|
|
|
|
|
&test_plan(Some(ExecutionTimeouts {
|
|
|
|
|
first_byte_ms: Some(12_345),
|
|
|
|
|
total_ms: Some(90_000),
|
|
|
|
|
..ExecutionTimeouts::default()
|
|
|
|
|
})),
|
|
|
|
|
Some(&report_context),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
assert_eq!(timeout, Duration::from_millis(12_345));
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-24 13:29:05 +08:00
|
|
|
#[test]
|
|
|
|
|
fn unused_persistence_skips_pool_internal_candidates() {
|
|
|
|
|
assert!(should_skip_unused_persistence(Some(&json!({
|
2026-04-27 12:34:03 +08:00
|
|
|
"candidate_group_id": "pool-group",
|
|
|
|
|
"pool_key_index": 0,
|
|
|
|
|
}))));
|
2026-05-03 20:14:29 +08:00
|
|
|
assert!(should_skip_unused_persistence(Some(&json!({
|
|
|
|
|
"candidate_group_id": "pool-group",
|
|
|
|
|
"pool_key_index": 1,
|
|
|
|
|
}))));
|
2026-04-24 13:29:05 +08:00
|
|
|
assert!(!should_skip_unused_persistence(Some(&json!({
|
|
|
|
|
"candidate_group_id": "pool-group",
|
|
|
|
|
}))));
|
|
|
|
|
assert!(!should_skip_unused_persistence(Some(&json!({
|
|
|
|
|
"candidate_index": 1,
|
|
|
|
|
}))));
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-11 01:50:24 +08:00
|
|
|
#[tokio::test]
|
|
|
|
|
async fn stream_candidate_watchdog_marks_failed_candidate_and_continues() {
|
|
|
|
|
let writer = Arc::new(TestRequestCandidateWriter::default());
|
|
|
|
|
let plan = test_plan(Some(ExecutionTimeouts {
|
|
|
|
|
first_byte_ms: Some(25),
|
|
|
|
|
..ExecutionTimeouts::default()
|
|
|
|
|
}));
|
|
|
|
|
let report_context = test_report_context();
|
|
|
|
|
let writer_for_task = writer.clone();
|
|
|
|
|
|
|
|
|
|
let task = tokio::spawn(async move {
|
|
|
|
|
execute_stream_candidate_with_watchdog(
|
|
|
|
|
writer_for_task.as_ref(),
|
|
|
|
|
"trace_watchdog",
|
|
|
|
|
"claude_cli_stream",
|
|
|
|
|
&plan,
|
|
|
|
|
Some(&report_context),
|
|
|
|
|
|| std::future::pending::<Result<Option<Response<Body>>, GatewayError>>(),
|
|
|
|
|
)
|
|
|
|
|
.await
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
tokio::time::sleep(Duration::from_millis(40)).await;
|
|
|
|
|
let result = task.await.expect("watchdog task should join");
|
|
|
|
|
assert!(matches!(result, Ok(None)));
|
|
|
|
|
|
|
|
|
|
let records = writer.records.lock().await;
|
|
|
|
|
assert_eq!(records.len(), 1);
|
|
|
|
|
let record = &records[0];
|
|
|
|
|
assert_eq!(record.status, RequestCandidateStatus::Failed);
|
|
|
|
|
assert_eq!(
|
|
|
|
|
record.status_code,
|
|
|
|
|
Some(http::StatusCode::GATEWAY_TIMEOUT.as_u16())
|
|
|
|
|
);
|
|
|
|
|
assert_eq!(
|
|
|
|
|
record.error_type.as_deref(),
|
|
|
|
|
Some("local_stream_candidate_watchdog_timeout")
|
|
|
|
|
);
|
|
|
|
|
assert!(record
|
|
|
|
|
.error_message
|
|
|
|
|
.as_deref()
|
2026-05-26 15:02:50 +08:00
|
|
|
.is_some_and(|message| message == "Stream first byte timeout"));
|
2026-04-11 01:50:24 +08:00
|
|
|
assert_eq!(record.candidate_index, 2);
|
|
|
|
|
}
|
|
|
|
|
}
|