Skip to main content

switchyard_libsy/core/
algorithm.rs

1// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2// SPDX-License-Identifier: Apache-2.0
3
4//! The [`Algorithm`] trait and its [`Driver`] — the orchestration contract every
5//! algorithm implements, and the offload channel it uses to make model calls and
6//! publish [`Decision`]s.
7
8use std::{
9    collections::{HashMap, HashSet},
10    pin::Pin,
11    sync::Arc,
12    time::{Duration, Instant},
13};
14
15use async_trait::async_trait;
16use futures::{Stream, StreamExt};
17use parking_lot::Mutex;
18use tracing::Instrument;
19
20/// The request/response protocol types come from [`switchyard_protocol`].
21/// [`switchyard_protocol::LlmRequest`] is the normalized request;
22/// [`switchyard_protocol::AggLlmResponse`] is the buffered response;
23/// [`switchyard_protocol::LlmResponseChunk`] is normalized streaming content;
24/// [`switchyard_protocol::LlmResponseStreamEvent`] is its host/algorithm envelope; and
25/// [`switchyard_protocol::LlmResponse`] carries either a live
26/// [`switchyard_protocol::LlmResponseStream`] or the terminal aggregate.
27use switchyard_protocol::{
28    Context, Decision, LlmClientError, Request, Response, RoutedLlmClient, Signals, Usage,
29};
30
31use super::driver::{DriverRequest, DriverStep, TypeErasedDriver};
32use crate::{DriverError, LibsyError, Result, observability};
33
34/// A boxed, `Send` stream of [`Step`]s — the output of
35/// [`Algorithm::run_stream`]. Boxed so the trait method that produces it keeps
36/// `Arc<dyn Algorithm>` object-safe.
37pub type StepStream = Pin<Box<dyn Stream<Item = Result<Step>> + Send>>;
38
39/// One completed model call observed at the algorithm offload boundary.
40#[derive(Clone, Debug)]
41pub struct LlmCallObservation {
42    /// Model selected for the completed call.
43    pub selected_model: String,
44    /// Routing tier attached to the selected model, when present.
45    pub tier: Option<String>,
46    /// Whether this was the routed backend call rather than classifier or judge overhead.
47    pub is_routed: bool,
48    /// Whether the call completed successfully.
49    pub is_success: bool,
50    /// Time spent waiting for the model call to resolve.
51    pub duration: Duration,
52    /// Normalized usage for a buffered successful response.
53    pub usage: Option<Usage>,
54}
55
56/// One request-scoped observation emitted by the algorithm runner.
57#[derive(Clone, Debug)]
58pub enum RunObservation {
59    /// A completed model call.
60    LlmCall(LlmCallObservation),
61    /// Routing time recorded by the `switchyard.routing_overhead_ms` metric.
62    RoutingOverhead(Duration),
63}
64
65/// Request-scoped callback for algorithm-run observations.
66///
67/// The runner invokes this callback inline while resolving observations. Several
68/// runs may invoke the same observer concurrently, so implementations must be
69/// thread-safe, fast, and non-blocking.
70pub type RunObserver = Arc<dyn Fn(RunObservation) + Send + Sync>;
71
72/// A request paired with the routing [`Decision`] that produced it — the offload
73/// payload a host reads (via [`CallLlmRequest::get_routed`]) to serve the call.
74///
75/// The two model identifiers live in separate, unambiguous places: the model to
76/// call is [`decision.selected_model()`](Decision::selected_model), while
77/// `request.llm_request.model` is the *inbound* name the agent asked for (libsy
78/// never overwrites it). A client maps `selected_model()` to the provider model
79/// id it hits.
80#[derive(Clone)]
81pub struct RoutedRequest {
82    /// The request to serve; its `model` is the agent's original name.
83    pub request: Request,
84    /// The routing decision behind this call; `selected_model()` is the model to hit.
85    pub decision: Arc<dyn Decision>,
86    /// The client that serves this call by default, or `None` when the routed target
87    /// had no client. Rides along on the offloaded call so a host driving the stream
88    /// can serve it by default or override it with its own transport.
89    pub default_client: Option<Arc<dyn RoutedLlmClient>>,
90    /// The request's cross-cutting context, carried through the offload so whoever
91    /// serves the call (libsy's own `run`, or a host driving the stream) hands it to
92    /// [`RoutedLlmClient::call`].
93    pub ctx: Context,
94}
95
96/// The host-facing half of an offloaded model call, surfaced inside [`Step::CallLlm`].
97///
98/// Wraps a `DriverRequest` whose payload is a [`RoutedRequest`]. The host reads the
99/// routed request ([`get_routed`](Self::get_routed)) and the decision behind it
100/// ([`get_decision`](Self::get_decision)), performs (or delegates) the model call, and
101/// fulfills it with [`respond`](Self::respond) — unblocking the algorithm's
102/// [`Driver::call_llm`] on the other side.
103pub struct CallLlmRequest {
104    inner: DriverRequest,
105    routed: RoutedRequest,
106}
107
108impl CallLlmRequest {
109    /// Wrap a driver request whose payload is a [`RoutedRequest`]. Caches an owned copy
110    /// so the accessors are plain field reads.
111    fn new(inner: DriverRequest) -> Self {
112        // The payload is always a `RoutedRequest` (set by `Driver::call_llm`); a
113        // mismatch would be a libsy bug, not a runtime condition.
114        let routed = match inner.request::<RoutedRequest>() {
115            Ok(routed) => routed.clone(),
116            Err(_) => unreachable!("CallLlmRequest payload is always a RoutedRequest"),
117        };
118        Self { inner, routed }
119    }
120
121    /// The routed request the host should serve. Its
122    /// [`default_client`](RoutedRequest::default_client) serves the call by default,
123    /// and its `decision.selected_model()` names the model to hit.
124    pub fn get_routed(&self) -> &RoutedRequest {
125        &self.routed
126    }
127
128    /// The model request to perform (the [`Request`] inside the routed request).
129    pub fn get_request(&self) -> &Request {
130        &self.get_routed().request
131    }
132
133    /// The decision that led to this call — its `selected_model()` is the model to hit.
134    pub fn get_decision(&self) -> &dyn Decision {
135        self.get_routed().decision.as_ref()
136    }
137
138    /// Fulfill the promise with the caller's model-call result. Pass `Err(..)` to
139    /// propagate a failed model call back to the algorithm. Consumes the promise: it
140    /// can only be fulfilled once.
141    pub fn respond(self, result: Result<Response>) -> Result<()> {
142        self.inner.respond::<Response>(result)
143    }
144}
145
146/// The offload channel handed to an algorithm's
147/// [`create_run_task`](Algorithm::create_run_task). The algorithm makes model calls
148/// with [`call_llm_target`](Self::call_llm_target) (or [`call_llm`](Self::call_llm)) and
149/// publishes its [`Decision`]s with [`info`](Self::info); each call is offloaded to the
150/// request's [`Step`] stream and awaits the consumer's response. The step channel is
151/// bounded, so the consumer paces the algorithm one step at a time.
152#[derive(Clone)]
153pub struct Driver {
154    driver: TypeErasedDriver,
155    // How long the call that served this run took. We need this to calculate routing overhead.
156    routed_call: Arc<Mutex<Option<Duration>>>,
157    observer: Option<RunObserver>,
158}
159
160impl Driver {
161    /// Build an empty driver with its step channel ready. Created per call by
162    /// [`run_stream`](Algorithm::run_stream).
163    pub(crate) fn new() -> Self {
164        Self::with_observer(None)
165    }
166
167    fn with_observer(observer: Option<RunObserver>) -> Self {
168        Self {
169            driver: TypeErasedDriver::new(),
170            routed_call: Arc::new(Mutex::new(None)),
171            observer,
172        }
173    }
174
175    /// How long the call that served this run took, if one has succeeded.
176    pub(crate) fn routed_call_duration(&self) -> Option<Duration> {
177        *self.routed_call.lock()
178    }
179
180    /// Records routing overhead (how long Switchyard added to the call) once
181    /// per successful run. Called by `observe_run`.
182    pub(crate) fn observe_routing_overhead(&self, duration: Duration) {
183        if let Some(observer) = &self.observer {
184            observer(RunObservation::RoutingOverhead(duration));
185        }
186    }
187
188    /// Offload a model call: publish `routed` as a [`Step::CallLlm`] and await the
189    /// consumer's [`Response`]. The call's context travels inside
190    /// [`routed.ctx`](RoutedRequest::ctx). Errors if the stream is closed or the call failed.
191    /// The await is wrapped in a `libsy.llm_call` span measuring *fulfillment* as
192    /// the algorithm observes it (host queueing/serving included; a streamed
193    /// response resolves when its stream handle arrives); latency, outcome, and
194    /// token usage are recorded when it resolves. The provider call itself gets a
195    /// `libsy.client_call` span when [`Algorithm::run`] serves it.
196    #[tracing::instrument(
197        target = "libsy",
198        name = "libsy.llm_call",
199        skip_all,
200        fields(
201            algorithm = observability::algorithm_label(&routed.ctx),
202            selected_model = routed.decision.selected_model(),
203            openinference.span.kind = "CHAIN",
204            outcome = tracing::field::Empty,
205            error = tracing::field::Empty,
206            input_tokens = tracing::field::Empty,
207            output_tokens = tracing::field::Empty,
208            total_tokens = tracing::field::Empty,
209            reasoning_tokens = tracing::field::Empty,
210        )
211    )]
212    pub async fn call_llm(&self, routed: RoutedRequest) -> Result<Response> {
213        let algorithm = observability::algorithm_label(&routed.ctx).to_string();
214        let selected_model = routed.decision.selected_model().to_string();
215        let tier = routed.decision.routing_tier().map(str::to_string);
216        let is_routed = routed.decision.is_routed_call();
217        let started = Instant::now();
218        let result = self
219            .driver
220            .fulfill_request::<RoutedRequest, Response>(routed.ctx.clone(), routed)
221            .await;
222        let elapsed = started.elapsed();
223        observability::record_llm_call(
224            &algorithm,
225            &selected_model,
226            tier.as_deref(),
227            is_routed,
228            elapsed,
229            &result,
230            &tracing::Span::current(),
231        );
232        if let Some(observer) = &self.observer {
233            observer(RunObservation::LlmCall(LlmCallObservation {
234                selected_model,
235                tier,
236                is_routed,
237                is_success: result.is_ok(),
238                duration: elapsed,
239                usage: result
240                    .as_ref()
241                    .ok()
242                    .and_then(|response| response.llm_response.as_agg())
243                    .map(|response| response.usage.clone()),
244            }));
245        }
246        // Classifier and judge calls are routing overhead.
247        // And don't record time for failed calls.
248        if is_routed && result.is_ok() {
249            *self.routed_call.lock() = Some(elapsed);
250        }
251        result
252    }
253
254    /// Offload a call to `target`: pair `request` with `decision` and the target's
255    /// default client into a [`RoutedRequest`], then publish it (see
256    /// [`call_llm`](Self::call_llm)). The convenience most algorithms use;
257    /// `decision.selected_model()` names the model to hit, and `request`'s
258    /// `model` is left untouched.
259    pub async fn call_llm_target(
260        &self,
261        ctx: Context,
262        target: &LlmTarget,
263        request: Request,
264        decision: Arc<dyn Decision>,
265    ) -> Result<Response> {
266        self.call_llm(RoutedRequest {
267            request,
268            decision,
269            default_client: target.llm_client.clone(),
270            ctx,
271        })
272        .await
273    }
274
275    /// Publish a routing [`Decision`] as a [`Step::Decision`] on the stream.
276    /// Each successfully published decision is counted and logged with its
277    /// reasoning; a decision the stream never accepted is not recorded.
278    pub async fn info(&self, ctx: Context, decision: Arc<dyn Decision>) -> Result<()> {
279        self.driver.info(ctx.clone(), decision.clone()).await?;
280        observability::record_decision(&ctx, decision.as_ref());
281        Ok(())
282    }
283
284    /// Emit the terminal step: [`Step::ReturnToAgent`] on `Ok`, or an `Err` stream
285    /// item on failure. Internal: called once by [`run_stream`](Algorithm::run_stream)
286    /// when the algorithm finishes.
287    pub(crate) async fn finish(&self, ctx: Context, result: Result<Response>) -> Result<()> {
288        match result {
289            Ok(response) => self.driver.done(ctx, response).await,
290            Err(err) => self.driver.fail(ctx, err).await,
291        }
292    }
293
294    /// Transform the raw driver stream into a stream of [`Step`]s. Internal: the
295    /// consumer stream is taken (once) by [`run_stream`](Algorithm::run_stream). A
296    /// payload that does not match the expected type for its step becomes an `Err` item.
297    pub(crate) fn stream(&self) -> impl Stream<Item = Result<Step>> + use<> {
298        self.driver.stream().map(|item| match item? {
299            DriverStep::Request(req) => Ok(Step::CallLlm(Box::new(CallLlmRequest::new(req)))),
300            DriverStep::Info(payload) => payload
301                .downcast::<Arc<dyn Decision>>()
302                .map(|decision| Step::Decision(*decision))
303                .map_err(|_| {
304                    DriverError::TypeMismatch {
305                        expected: "Arc<dyn Decision>",
306                    }
307                    .into()
308                }),
309            DriverStep::Done(payload) => payload
310                .downcast::<Response>()
311                .map(Step::ReturnToAgent)
312                .map_err(|_| {
313                    DriverError::TypeMismatch {
314                        expected: "Response",
315                    }
316                    .into()
317                }),
318        })
319    }
320}
321
322impl Default for Driver {
323    fn default() -> Self {
324        Self::new()
325    }
326}
327
328/// One item in the stream returned by `Driver::stream` / [`Algorithm::run_stream`].
329pub enum Step {
330    /// The algorithm needs this model call performed. The host serves it (optionally
331    /// via [`RoutedRequest::default_client`]) and fulfills it with
332    /// [`CallLlmRequest::respond`]. Boxed: it is by far the largest variant.
333    CallLlm(Box<CallLlmRequest>),
334    /// A routing decision the algorithm made, published via [`Driver::info`] as it
335    /// happens (rather than collected into a trace returned at the end).
336    Decision(Arc<dyn Decision>),
337    /// The algorithm finished with its final response — the last step of a run.
338    ReturnToAgent(Box<Response>),
339}
340
341/// Abort guard
342struct AbortOnDrop(tokio::task::AbortHandle);
343
344impl Drop for AbortOnDrop {
345    fn drop(&mut self) {
346        self.0.abort();
347    }
348}
349
350/// A named routing target: a `semantic_name` an algorithm routes by, and an optional
351/// [`RoutedLlmClient`] to serve its calls. An algorithm hands a target to
352/// [`Driver::call_llm_target`]; the client rides along as
353/// [`RoutedRequest::default_client`] for the stream consumer to serve or override.
354#[derive(Clone)]
355pub struct LlmTarget {
356    /// The routing label an algorithm selects this target by — a logical tier like
357    /// `"strong"`, or the model id when they coincide. Mapping it to a provider model
358    /// id is the client's concern, never the algorithm's.
359    pub semantic_name: String,
360    /// The client that serves this target's calls by default, or `None` (then the
361    /// stream consumer must serve them).
362    pub llm_client: Option<Arc<dyn RoutedLlmClient>>,
363}
364
365/// The set of targets an algorithm may route among. An algorithm is constructed
366/// with one and picks targets by position ([`targets`](Self::targets)) or by name
367/// ([`get_target`](Self::get_target)).
368#[derive(Clone)]
369pub struct LlmTargetSet {
370    targets: Vec<LlmTarget>,
371}
372
373impl LlmTargetSet {
374    /// Build a target set from a list of targets.
375    pub fn new(targets: Vec<LlmTarget>) -> Self {
376        Self { targets }
377    }
378
379    /// All targets in the set — e.g. for an algorithm to select among.
380    pub fn targets(&self) -> &[LlmTarget] {
381        &self.targets
382    }
383
384    /// Look up a target by name; errors if no target has that name.
385    pub fn get_target(&self, name: &str) -> Result<LlmTarget> {
386        self.targets
387            .iter()
388            .find(|t| t.semantic_name == name)
389            .cloned()
390            .ok_or_else(|| LibsyError::TargetNotFound {
391                target: name.to_string(),
392            })
393    }
394
395    /// The named target, or the first one this request is not barred from when it has been
396    /// excluded (see [`Context::exclude_target`]). Errors if every target is excluded.
397    pub fn resolve_target(&self, name: &str, ctx: &Context) -> Result<LlmTarget> {
398        let target = self.get_target(name)?;
399        if !ctx.is_excluded(&target.semantic_name) {
400            return Ok(target);
401        }
402        self.targets
403            .iter()
404            .find(|t| !ctx.is_excluded(&t.semantic_name))
405            .cloned()
406            .ok_or(LibsyError::AllTargetsExcluded)
407    }
408
409    /// The first target's client that can serve `count_tokens` (an Anthropic
410    /// upstream), or `None` when no target has one. Used by an algorithm's
411    /// [`count_tokens_client`](crate::Algorithm::count_tokens_client).
412    pub fn count_tokens_client(&self) -> Option<Arc<dyn RoutedLlmClient>> {
413        self.targets.iter().find_map(|target| {
414            target
415                .llm_client
416                .as_ref()
417                .filter(|client| client.supports_count_tokens())
418                .cloned()
419        })
420    }
421}
422
423/// Bounds process-local overflow history. Dropping a live session's entry costs one
424/// rediscovered overflow, so the victim choice does not need to be exact.
425const MAX_EVICTION_SESSIONS: usize = 1_024;
426
427/// Per-session record of the targets that overflowed their context window.
428///
429/// A conversation only grows, so a target that could not fit one turn will not fit a
430/// later one; remembering it lets the next turn skip a call certain to fail. Requests
431/// without a session id are not tracked — there is nothing to remember them by.
432#[derive(Default)]
433pub(crate) struct SessionEvictions {
434    sessions: Mutex<HashMap<String, HashSet<String>>>,
435}
436
437impl SessionEvictions {
438    /// The targets `session` has already overflowed; empty for an untracked request.
439    fn evicted_in(&self, session: Option<&str>) -> Vec<String> {
440        let Some(session) = session else {
441            return Vec::new();
442        };
443        self.sessions
444            .lock()
445            .get(session)
446            .map(|targets| targets.iter().cloned().collect())
447            .unwrap_or_default()
448    }
449
450    /// Remembers that `target` overflowed in `session`, tracking at most
451    /// [`MAX_EVICTION_SESSIONS`] sessions.
452    fn record(&self, session: Option<&str>, target: &str) {
453        let Some(session) = session else { return };
454        let mut sessions = self.sessions.lock();
455        if sessions.len() >= MAX_EVICTION_SESSIONS
456            && !sessions.contains_key(session)
457            && let Some(oldest) = sessions.keys().next().cloned()
458        {
459            sessions.remove(&oldest);
460        }
461        sessions
462            .entry(session.to_string())
463            .or_default()
464            .insert(target.to_string());
465    }
466}
467
468/// How many of `targets` this request is still allowed to reach.
469fn eligible_targets(targets: &LlmTargetSet, ctx: &Context) -> usize {
470    targets
471        .targets()
472        .iter()
473        .filter(|t| !ctx.is_excluded(&t.semantic_name))
474        .count()
475}
476
477/// Bars the targets `session` has already overflowed from this request, so routing does
478/// not select one that is certain to fail again.
479pub(crate) fn exclude_evicted(
480    ctx: &mut Context,
481    targets: &LlmTargetSet,
482    evictions: &SessionEvictions,
483    session: Option<&str>,
484) {
485    for target in evictions.evicted_in(session) {
486        // Never seed the pool empty: a later turn may be small enough to serve, and the
487        // caller should get the upstream's answer rather than a routing error.
488        if eligible_targets(targets, ctx) <= 1 {
489            break;
490        }
491        ctx.exclude_target(target);
492    }
493}
494
495/// Calls `target`, falling back to the next eligible target in `targets` whenever one
496/// overflows its context window, until a call succeeds or every target has been tried.
497///
498/// Routing is deliberately not re-run: the fallback replaces the target in place, so the
499/// caller's request-side work and retained state still see exactly one turn.
500/// `fallback_decision` builds the [`Decision`] published for a `from -> to` hop, and each
501/// overflow is recorded against `session` so later turns skip that target outright.
502#[allow(clippy::too_many_arguments)]
503pub(crate) async fn call_llm_with_overflow_fallback(
504    mut ctx: Context,
505    driver: &Driver,
506    targets: &LlmTargetSet,
507    mut target: LlmTarget,
508    mut decision: Arc<dyn Decision>,
509    request: Request,
510    session: Option<&str>,
511    evictions: &SessionEvictions,
512    fallback_decision: impl Fn(&LlmTarget, &LlmTarget) -> Arc<dyn Decision>,
513) -> Result<Response> {
514    loop {
515        let result = driver
516            .call_llm_target(ctx.clone(), &target, request.clone(), decision.clone())
517            .await;
518        let Err(error) = result else { return result };
519        let LibsyError::ClientCall {
520            target: failed,
521            source: LlmClientError::ContextWindowExceeded { .. },
522        } = &error
523        else {
524            return Err(error);
525        };
526        // A target already excluded means the pool is spent; surface the client error
527        // so the caller still sees a context overflow rather than an internal failure.
528        if !ctx.exclude_target(failed) {
529            return Err(error);
530        }
531        evictions.record(session, failed);
532        let Ok(next) = targets.resolve_target(&target.semantic_name, &ctx) else {
533            return Err(error);
534        };
535        decision = fallback_decision(&target, &next);
536        target = next;
537        driver.info(ctx.clone(), decision.clone()).await?;
538    }
539}
540
541/// An optimization strategy. Implement [`create_run_task`](Self::create_run_task);
542/// callers drive it with the provided [`run`](Self::run) (serve calls, get the answer)
543/// or [`run_stream`](Self::run_stream) (drive the [`Step`] stream yourself).
544///
545/// Methods take `self: Arc<Self>`: one algorithm (`Arc<dyn Algorithm>`) is shared across
546/// requests and run concurrently, so it owns its thread-safety and any shared state.
547///
548/// # Concurrency
549///
550/// A host may run the same algorithm concurrently for many requests. Implementations
551/// must synchronize their own mutable shared state. Each call to [`run_stream`](Self::run_stream)
552/// creates an independent [`Driver`], so model-call promises and emitted [`Step`]s cannot
553/// cross between runs.
554///
555/// # Observability
556///
557/// [`run_stream`](Self::run_stream) creates a `libsy.run` span, and each offloaded model
558/// call creates a `libsy.llm_call` span. [`run`](Self::run) additionally wraps calls it
559/// serves through a target's default client in `libsy.client_call`. Decisions and failures
560/// are emitted through `tracing`; metrics use the global OpenTelemetry meter provider.
561#[async_trait]
562pub trait Algorithm: Send + Sync + 'static {
563    /// Stable, low-cardinality name identifying this algorithm — the
564    /// `algorithm` attribute on every span, metric, and log line the crate
565    /// emits for its runs.
566    fn name(&self) -> &str;
567
568    /// Run one request to completion: make model calls with [`Driver::call_llm_target`],
569    /// publish [`Decision`]s with [`Driver::info`], and return the final [`Response`].
570    /// The method an algorithm implements; [`run`](Self::run) / [`run_stream`](Self::run_stream)
571    /// drive it. `ctx` carries the request's cross-cutting values (today: the
572    /// algorithm's telemetry label in [`Context::values`]).
573    async fn create_run_task(
574        self: Arc<Self>,
575        ctx: Context,
576        driver: Driver,
577        request: Request,
578    ) -> Result<Response>;
579
580    /// Feed the algorithm agentic-stack events (tool results, budgets, etc.). The
581    /// reference algorithms ignore signals; a stateful algorithm updates its own
582    /// (interior-mutable) state. Takes `self: Arc<Self>` like the other run methods.
583    #[allow(unused_variables)]
584    async fn process_signals(self: Arc<Self>, signals: Signals) -> Result<()> {
585        Ok(())
586    }
587
588    /// The client [`count_tokens`](Self::count_tokens) forwards to: the first of
589    /// this algorithm's targets whose client can count tokens (an Anthropic
590    /// upstream). The default is `None` — an algorithm with no Anthropic target
591    /// does not support token counting.
592    ///
593    /// CAVEAT: this picks the *first* Anthropic target, not a routed one —
594    /// count_tokens is a direct passthrough, so it does not run the routing
595    /// cascade. For a route with several Anthropic tiers "first" is arbitrary;
596    /// choosing which tier count_tokens should reflect is deferred.
597    fn count_tokens_client(&self) -> Option<Arc<dyn RoutedLlmClient>> {
598        None
599    }
600
601    /// Count the tokens `request` would use — a **direct passthrough** to this
602    /// algorithm's Anthropic target (via
603    /// [`count_tokens_client`](Self::count_tokens_client)), **not** a routed
604    /// call. Token counting is a pre-flight estimate with no routing decision,
605    /// so it deliberately bypasses the classifier cascade (which runs only for
606    /// completions via [`run`](Self::run)). Returns the upstream's JSON
607    /// verbatim. Errors when the algorithm has no Anthropic target.
608    async fn count_tokens(&self, request: Request) -> Result<serde_json::Value> {
609        let client = self
610            .count_tokens_client()
611            .ok_or_else(|| LibsyError::AlgorithmError {
612                message: "no target supports count_tokens (needs an Anthropic upstream)"
613                    .to_string(),
614            })?;
615        client
616            .count_tokens(request)
617            .await
618            .map_err(|source| LibsyError::client_call("count_tokens", source))
619    }
620
621    /// Process a request to completion, returning a stream of [`Step`]s.
622    ///
623    /// The consumer must fulfill every [`Step::CallLlm`] before the algorithm can
624    /// continue. The bounded step channel applies backpressure when the consumer is
625    /// not polling. A successful run ends with [`Step::ReturnToAgent`]; a failure is
626    /// emitted as an `Err` item. Dropping the stream aborts the spawned algorithm task.
627    ///
628    /// Every invocation owns a separate [`Driver`]. `observer`, when present, receives
629    /// each completed model call and, after a successful routed run, its routing overhead.
630    fn run_stream(
631        self: Arc<Self>,
632        ctx: Context,
633        request: Request,
634        observer: Option<RunObserver>,
635    ) -> StepStream {
636        // Stamp the algorithm's telemetry label into the request context; the
637        // context rides on every driver call, so its telemetry is attributed.
638        let mut ctx = ctx;
639        ctx.values.insert(
640            observability::ALGORITHM_KEY.to_string(),
641            self.name().to_string(),
642        );
643        let driver = Driver::with_observer(observer);
644        let task_driver = driver.clone();
645        let task_ctx = ctx.clone();
646        let stream = task_driver.stream();
647        // One `libsy.run` span covers the whole algorithm task; the driver's
648        // `libsy.llm_call` spans and decision logs nest inside it via `tracing`'s
649        // contextual parenting.
650        let span = observability::run_span(self.name(), &request);
651        let observed_driver = task_driver.clone();
652        let handle = tokio::spawn(
653            async move {
654                observability::observe_run(
655                    task_ctx.clone(),
656                    observed_driver,
657                    self.create_run_task(task_ctx, task_driver, request),
658                )
659                .await
660            }
661            .instrument(span),
662        );
663        // Dropping the stream aborts the algorithm task when its consumer goes away.
664        let abort_guard = AbortOnDrop(handle.abort_handle());
665
666        let finish_driver = driver.clone();
667        let finish_ctx = ctx;
668        let tail: StepStream = Box::pin(
669            futures::stream::once(async move {
670                let result = match handle.await {
671                    Ok(response) => response,
672                    Err(source) => Err(LibsyError::AlgorithmTask { source }),
673                };
674                finish_driver.finish(finish_ctx, result).await
675            })
676            .filter_map(|finish_result| async move { finish_result.err().map(Err) }),
677        );
678
679        let stream: StepStream = Box::pin(stream);
680        Box::pin(futures::stream::select(stream, tail).map(move |step| {
681            // link abort guard to stream
682            let _keep_alive = &abort_guard;
683            step
684        }))
685    }
686
687    /// Process a request to completion, returning the final [`Response`] and the trace of
688    /// [`Decision`]s the algorithm made along the way.
689    ///
690    async fn run(
691        self: Arc<Self>,
692        ctx: Context,
693        request: Request,
694    ) -> Result<(Vec<Arc<dyn Decision>>, Response)> {
695        self.run_observed(ctx, request, None).await
696    }
697
698    /// Process a request to completion while reporting each model call to `observer`.
699    async fn run_observed(
700        self: Arc<Self>,
701        ctx: Context,
702        request: Request,
703        observer: Option<RunObserver>,
704    ) -> Result<(Vec<Arc<dyn Decision>>, Response)> {
705        // Serve one offloaded call with its target's default client. A failed *model*
706        // call is forwarded to the algorithm via `respond`; this errors only on an
707        // infrastructure failure (no default client, or the promise was dropped).
708        // `serve` makes the one API call libsy itself performs, so it gets its
709        // own `libsy.client_call` span.
710        #[tracing::instrument(
711            target = "libsy",
712            name = "libsy.client_call",
713            skip_all,
714            fields(
715                algorithm = observability::algorithm_label(&call.get_routed().ctx),
716                switchyard.algorithm = observability::algorithm_label(&call.get_routed().ctx),
717                switchyard.routing.tier = tracing::field::Empty,
718                selected_model = call.get_decision().selected_model(),
719                otel.kind = "client",
720                otel.name = %format_args!("chat {}", call.get_decision().selected_model()),
721                openinference.span.kind = "LLM",
722                gen_ai.operation.name = "chat",
723                gen_ai.request.model = call.get_decision().selected_model(),
724                gen_ai.request.stream = tracing::field::Empty,
725                gen_ai.request.temperature = tracing::field::Empty,
726                gen_ai.request.top_p = tracing::field::Empty,
727                gen_ai.request.top_k = tracing::field::Empty,
728                gen_ai.request.max_tokens = tracing::field::Empty,
729                gen_ai.request.reasoning.level = tracing::field::Empty,
730                gen_ai.output.type = tracing::field::Empty,
731                gen_ai.conversation.id = tracing::field::Empty,
732                server.address = tracing::field::Empty,
733                server.port = tracing::field::Empty,
734                gen_ai.response.id = tracing::field::Empty,
735                gen_ai.response.model = tracing::field::Empty,
736                gen_ai.usage.input_tokens = tracing::field::Empty,
737                gen_ai.usage.output_tokens = tracing::field::Empty,
738                gen_ai.usage.cache_read.input_tokens = tracing::field::Empty,
739                gen_ai.usage.cache_creation.input_tokens = tracing::field::Empty,
740                gen_ai.usage.reasoning.output_tokens = tracing::field::Empty,
741                outcome = tracing::field::Empty,
742                otel.status_code = tracing::field::Empty,
743                error.type = tracing::field::Empty,
744                error = tracing::field::Empty,
745            )
746        )]
747        async fn serve(call: CallLlmRequest) -> Result<()> {
748            let span = tracing::Span::current();
749            observability::record_gen_ai_request(&span, &call.get_routed().request.llm_request);
750            if let Some(tier) = call.get_decision().routing_tier() {
751                span.record("switchyard.routing.tier", tier);
752            }
753            if let Some(session_id) = call
754                .get_routed()
755                .request
756                .metadata
757                .as_ref()
758                .and_then(|metadata| metadata.session_id.as_deref())
759            {
760                span.record("gen_ai.conversation.id", session_id);
761            }
762            let routed = call.get_routed().clone();
763            let target = routed.decision.selected_model().to_string();
764            let client =
765                routed
766                    .default_client
767                    .clone()
768                    .ok_or_else(|| LibsyError::MissingClient {
769                        target: target.clone(),
770                    })?;
771            let result = client
772                .call(routed.ctx, routed.request, routed.decision)
773                .await
774                .map_err(|source| LibsyError::client_call(target, source));
775            let result = observability::observe_client_call(result);
776            call.respond(result)
777        }
778
779        let stream = self.run_stream(ctx, request, observer);
780        tokio::pin!(stream);
781
782        let mut trace: Vec<Arc<dyn Decision>> = Vec::new();
783        let mut in_flight = futures::stream::FuturesUnordered::new();
784        let mut final_response: Option<Response> = None;
785
786        loop {
787            tokio::select! {
788                Some(result) = in_flight.next() => match result {
789                    Ok(()) => {}, // CallLlm completed successfully
790                    Err(err) => return Err(err), // CallLlm failed, propagate the error
791                },
792                step = stream.next() => {
793                    match step {
794                        None => break, // stream has ended, no more steps
795                        Some(item) => match item? {
796                            Step::CallLlm(call) => in_flight.push(serve(*call)),
797                            Step::Decision(decision) => trace.push(decision),
798                            Step::ReturnToAgent(response) => {
799                                final_response = Some(*response);
800                                break;
801                            }
802                        }
803                    }
804                },
805            }
806        }
807        final_response
808            .map(|response| (trace, response))
809            .ok_or(LibsyError::MissingFinalResponse)
810    }
811}
812
813#[cfg(test)]
814mod tests {
815    use super::*;
816    use futures::StreamExt;
817    use switchyard_protocol::{
818        LlmResponse, LlmResponseChunk, completion_text, text_request, text_response,
819    };
820
821    #[derive(Debug, thiserror::Error)]
822    #[error("{0}")]
823    struct TestError(&'static str);
824
825    fn test_error(message: &'static str) -> LibsyError {
826        LibsyError::external("test", TestError(message))
827    }
828
829    /// Mock client that echoes back the target name it was called with.
830    struct EchoClient;
831
832    #[async_trait]
833    impl RoutedLlmClient for EchoClient {
834        async fn call(
835            &self,
836            _ctx: Context,
837            _request: Request,
838            decision: Arc<dyn Decision>,
839        ) -> std::result::Result<Response, LlmClientError> {
840            // Echo back the model the algorithm routed to (the decision's selection).
841            Ok(Response {
842                llm_response: LlmResponse::Agg(text_response(
843                    None,
844                    decision.selected_model().to_string(),
845                )),
846                metadata: None,
847            })
848        }
849    }
850
851    /// Trivial decision + algo used only to exercise the orchestrator: calls the
852    /// first target and returns its response with a one-item trace.
853    struct TestDecision {
854        model: String,
855    }
856
857    impl Decision for TestDecision {
858        fn selected_model(&self) -> &str {
859            &self.model
860        }
861        fn reasoning(&self) -> Option<&str> {
862            None
863        }
864        fn as_any(&self) -> &dyn std::any::Any {
865            self
866        }
867    }
868
869    struct TestAlgo {
870        target_set: LlmTargetSet,
871    }
872
873    #[async_trait]
874    impl Algorithm for TestAlgo {
875        fn name(&self) -> &str {
876            "test"
877        }
878
879        async fn create_run_task(
880            self: Arc<Self>,
881            ctx: Context,
882            driver: Driver,
883            request: Request,
884        ) -> Result<Response> {
885            let target = self
886                .target_set
887                .targets()
888                .first()
889                .ok_or(LibsyError::NoTargets)?
890                .clone();
891            let decision: Arc<dyn Decision> = Arc::new(TestDecision {
892                model: target.semantic_name.clone(),
893            });
894            driver.info(ctx.clone(), decision.clone()).await?;
895            driver
896                .call_llm_target(ctx, &target, request, decision)
897                .await
898        }
899    }
900
901    /// Build a shared `TestAlgo` over the given target set.
902    fn orch(target_set: LlmTargetSet) -> Arc<dyn Algorithm> {
903        Arc::new(TestAlgo { target_set })
904    }
905
906    fn request() -> Request {
907        Request {
908            llm_request: text_request(Some("auto".to_string()), "hi".to_string()),
909            raw_request: None,
910            metadata: None,
911        }
912    }
913
914    /// `(name, has_client)` — `has_client: false` builds a target with no default client.
915    fn target_set(names: &[(&str, bool)]) -> LlmTargetSet {
916        let targets = names
917            .iter()
918            .map(|(name, has_client)| LlmTarget {
919                semantic_name: name.to_string(),
920                llm_client: has_client.then(|| Arc::new(EchoClient) as Arc<dyn RoutedLlmClient>),
921            })
922            .collect();
923        LlmTargetSet::new(targets)
924    }
925
926    #[tokio::test]
927    async fn observed_run_reports_one_successful_routed_call() -> Result<()> {
928        let observations = Arc::new(Mutex::new(Vec::new()));
929        let observed = observations.clone();
930        let observer: RunObserver = Arc::new(move |observation| observed.lock().push(observation));
931        let (_, response) = orch(target_set(&[("direct/model", true)]))
932            .run_observed(Context::default(), request(), Some(observer))
933            .await?;
934        assert_eq!(
935            response.llm_response.as_agg().map(completion_text),
936            Some("direct/model".to_string())
937        );
938        let observations = observations.lock();
939        assert_eq!(observations.len(), 2);
940        let RunObservation::LlmCall(observation) = &observations[0] else {
941            return Err(test_error("expected an LLM call observation"));
942        };
943        assert_eq!(observation.selected_model, "direct/model");
944        assert!(observation.is_routed);
945        assert!(observation.is_success);
946        assert!(observation.usage.is_some());
947        assert!(matches!(
948            observations[1],
949            RunObservation::RoutingOverhead(_)
950        ));
951        Ok(())
952    }
953
954    #[test]
955    fn target_lookup_returns_the_missing_target() {
956        let error = target_set(&[]).get_target("missing").err();
957        assert!(matches!(
958            error,
959            Some(LibsyError::TargetNotFound { target }) if target == "missing"
960        ));
961    }
962
963    /// Client that serves a call as a token stream — its `call` returns
964    /// [`LlmResponse::Stream`] replaying `chunks` in order (as `Ok` items).
965    struct StreamingClient {
966        chunks: Vec<LlmResponseChunk>,
967    }
968
969    #[async_trait]
970    impl RoutedLlmClient for StreamingClient {
971        async fn call(
972            &self,
973            _ctx: Context,
974            _request: Request,
975            _decision: Arc<dyn Decision>,
976        ) -> std::result::Result<Response, LlmClientError> {
977            let stream = futures::stream::iter(
978                self.chunks
979                    .clone()
980                    .into_iter()
981                    .map(|chunk| Ok(chunk.into())),
982            )
983            .boxed();
984            Ok(Response {
985                llm_response: LlmResponse::Stream(stream),
986                metadata: None,
987            })
988        }
989    }
990
991    /// Build a single-target algo whose one target streams `chunks`.
992    fn streaming_orch(chunks: Vec<LlmResponseChunk>) -> Arc<dyn Algorithm> {
993        let target = LlmTarget {
994            semantic_name: "stream/model".to_string(),
995            llm_client: Some(Arc::new(StreamingClient { chunks }) as Arc<dyn RoutedLlmClient>),
996        };
997        orch(LlmTargetSet::new(vec![target]))
998    }
999
1000    #[tokio::test]
1001    async fn run_returns_a_streamed_response_the_caller_aggregates() -> Result<()> {
1002        // A streaming client -> its chunks flow through the promise and `ReturnToAgent`,
1003        // and `run` returns the live stream untouched for the caller to fold.
1004        let orch = streaming_orch(vec![
1005            LlmResponseChunk::MessageStart {
1006                id: Some("m1".to_string()),
1007                model: Some("stream/model".to_string()),
1008            },
1009            LlmResponseChunk::TextDelta {
1010                index: 0,
1011                text: "hel".to_string(),
1012            },
1013            LlmResponseChunk::TextDelta {
1014                index: 0,
1015                text: "lo".to_string(),
1016            },
1017            LlmResponseChunk::MessageStop {
1018                reason: Some("stop".to_string()),
1019            },
1020        ]);
1021        let (trace, response) = orch.run(Context::default(), request()).await?;
1022        // `run` handed back the live stream; the caller folds it to a buffered aggregate.
1023        let agg = response
1024            .llm_response
1025            .into_agg()
1026            .await
1027            .map_err(|error| LibsyError::external("aggregating response stream", error))?;
1028        assert_eq!(completion_text(&agg), "hello");
1029        assert_eq!(agg.model.as_deref(), Some("stream/model"));
1030        assert_eq!(trace.len(), 1);
1031        Ok(())
1032    }
1033
1034    #[tokio::test]
1035    async fn aggregating_a_streamed_response_propagates_a_mid_stream_error() -> Result<()> {
1036        // `run` succeeds and returns the stream; the in-band `Error` chunk surfaces only
1037        // when the caller aggregates it.
1038        let orch = streaming_orch(vec![
1039            LlmResponseChunk::TextDelta {
1040                index: 0,
1041                text: "partial".to_string(),
1042            },
1043            LlmResponseChunk::StreamError {
1044                message: "upstream exploded".to_string(),
1045            },
1046        ]);
1047        let (_, response) = orch.run(Context::default(), request()).await?;
1048        match response.llm_response.into_agg().await {
1049            Ok(_) => panic!("expected a mid-stream error, got an aggregate"),
1050            Err(err) => {
1051                assert!(err.to_string().contains("upstream exploded"));
1052                Ok(())
1053            }
1054        }
1055    }
1056
1057    #[tokio::test]
1058    async fn run_offloads_via_promise_then_returns_to_agent() -> Result<()> {
1059        // A client-less target -> its call is offloaded via a promise the
1060        // orchestrator surfaces as a `CallLlm` step for us to fulfill.
1061        let stream = orch(target_set(&[("offload/model", false)])).run_stream(
1062            Context::default(),
1063            request(),
1064            None,
1065        );
1066        tokio::pin!(stream);
1067
1068        let mut saw_call = false;
1069        let mut final_completion = None;
1070        while let Some(step) = stream.next().await {
1071            match step? {
1072                Step::CallLlm(call) => {
1073                    saw_call = true;
1074                    // The decision rode along with the promise.
1075                    assert_eq!(call.get_decision().selected_model(), "offload/model");
1076                    // Fulfilling the promise is the "real" model call the caller makes.
1077                    call.respond(Ok(Response {
1078                        llm_response: LlmResponse::Agg(text_response(
1079                            None,
1080                            "fulfilled".to_string(),
1081                        )),
1082                        metadata: None,
1083                    }))?;
1084                }
1085                Step::Decision(decision) => {
1086                    assert_eq!(decision.selected_model(), "offload/model");
1087                }
1088                Step::ReturnToAgent(response) => {
1089                    final_completion = Some(
1090                        response
1091                            .llm_response
1092                            .as_agg()
1093                            .map(completion_text)
1094                            .unwrap_or_default(),
1095                    );
1096                }
1097            }
1098        }
1099
1100        assert!(saw_call, "expected a CallLlm step before ReturnToAgent");
1101        assert_eq!(
1102            final_completion.ok_or_else(|| test_error("no ReturnToAgent step"))?,
1103            "fulfilled"
1104        );
1105        Ok(())
1106    }
1107
1108    #[tokio::test]
1109    async fn client_backed_target_offloads_with_a_default_client() -> Result<()> {
1110        // Every call now offloads to the stream; a client-backed target rides its
1111        // client along as `default_client` so the consumer can serve it by default.
1112        let stream = orch(target_set(&[("direct/model", true)])).run_stream(
1113            Context::default(),
1114            request(),
1115            None,
1116        );
1117        tokio::pin!(stream);
1118
1119        let mut final_completion = None;
1120        while let Some(step) = stream.next().await {
1121            match step? {
1122                Step::CallLlm(call) => {
1123                    let routed = call.get_routed().clone();
1124                    let client = routed
1125                        .default_client
1126                        .clone()
1127                        .ok_or_else(|| test_error("expected a default client"))?;
1128                    let target = routed.decision.selected_model().to_string();
1129                    let result = client
1130                        .call(routed.ctx, routed.request, routed.decision)
1131                        .await
1132                        .map_err(|error| LibsyError::client_call(target, error));
1133                    call.respond(result)?;
1134                }
1135                Step::Decision(_) => {}
1136                Step::ReturnToAgent(response) => {
1137                    final_completion = Some(
1138                        response
1139                            .llm_response
1140                            .as_agg()
1141                            .map(completion_text)
1142                            .unwrap_or_default(),
1143                    );
1144                }
1145            }
1146        }
1147
1148        // EchoClient echoes the model name back as the completion.
1149        assert_eq!(
1150            final_completion.ok_or_else(|| test_error("no ReturnToAgent"))?,
1151            "direct/model"
1152        );
1153        Ok(())
1154    }
1155
1156    #[tokio::test]
1157    async fn run_returns_the_response_when_all_targets_have_clients() -> Result<()> {
1158        // Every target has a client, so run serves every call via the
1159        // default client and returns the trace + final response.
1160        let (trace, response) = orch(target_set(&[("direct/model", true)]))
1161            .run(Context::default(), request())
1162            .await?;
1163        // TestAlgo calls the first target; EchoClient echoes its name.
1164        assert_eq!(
1165            response
1166                .llm_response
1167                .as_agg()
1168                .map(completion_text)
1169                .unwrap_or_default(),
1170            "direct/model"
1171        );
1172        assert_eq!(trace[0].selected_model(), "direct/model");
1173        Ok(())
1174    }
1175
1176    #[tokio::test]
1177    async fn run_errors_when_a_target_lacks_a_client() -> Result<()> {
1178        // A client-less target has no default client to serve its offloaded call, so
1179        // driving it to completion errors.
1180        let error = orch(target_set(&[("offload/model", false)]))
1181            .run(Context::default(), request())
1182            .await
1183            .err()
1184            .ok_or_else(|| test_error("expected a missing-client error"))?;
1185        assert!(matches!(
1186            error,
1187            LibsyError::MissingClient { target } if target == "offload/model"
1188        ));
1189        Ok(())
1190    }
1191
1192    #[tokio::test(flavor = "multi_thread", worker_threads = 12)]
1193    async fn requests_are_processed_in_parallel() -> Result<()> {
1194        use std::time::Duration;
1195        use tokio::sync::Barrier;
1196
1197        const N: usize = 12;
1198
1199        // A client that blocks until all N concurrent calls have arrived. If
1200        // requests were serialized (one algorithm behind a `Mutex`), only one
1201        // call could be in flight, the barrier would never reach N, and the test
1202        // would time out. It passes only because the shared algorithm is driven
1203        // concurrently across requests.
1204        struct BarrierClient {
1205            barrier: Arc<Barrier>,
1206        }
1207
1208        #[async_trait]
1209        impl RoutedLlmClient for BarrierClient {
1210            async fn call(
1211                &self,
1212                _ctx: Context,
1213                _request: Request,
1214                decision: Arc<dyn Decision>,
1215            ) -> std::result::Result<Response, LlmClientError> {
1216                self.barrier.wait().await;
1217                Ok(Response {
1218                    llm_response: LlmResponse::Agg(text_response(
1219                        None,
1220                        decision.selected_model().to_string(),
1221                    )),
1222                    metadata: None,
1223                })
1224            }
1225        }
1226
1227        let barrier = Arc::new(Barrier::new(N));
1228        let targets = LlmTargetSet::new(vec![LlmTarget {
1229            semantic_name: "m".to_string(),
1230            llm_client: Some(Arc::new(BarrierClient {
1231                barrier: barrier.clone(),
1232            })),
1233        }]);
1234        // One shared algorithm driven by many concurrent requests.
1235        let algo = orch(targets);
1236
1237        let mut handles = Vec::new();
1238        for _ in 0..N {
1239            let algo = algo.clone();
1240            handles.push(tokio::spawn(async move {
1241                algo.run(Context::default(), request())
1242                    .await
1243                    .map(|(_, response)| {
1244                        response
1245                            .llm_response
1246                            .as_agg()
1247                            .map(completion_text)
1248                            .unwrap_or_default()
1249                    })
1250            }));
1251        }
1252
1253        for handle in handles {
1254            // The timeout turns a serialization deadlock into a failure, not a hang.
1255            let completion = tokio::time::timeout(Duration::from_secs(5), handle)
1256                .await
1257                .map_err(|error| LibsyError::external("waiting for test task", error))?
1258                .map_err(|source| LibsyError::AlgorithmTask { source })??;
1259            assert_eq!(completion, "m");
1260        }
1261        Ok(())
1262    }
1263
1264    #[tokio::test]
1265    async fn offload_error_propagates_back_to_the_algorithm() -> Result<()> {
1266        // A client-less target offloads its call; we fulfill the promise with an
1267        // Err, which must flow back through `call_llm_target` into the algorithm and
1268        // out as an error step — not a response.
1269        let stream = orch(target_set(&[("offload/model", false)])).run_stream(
1270            Context::default(),
1271            request(),
1272            None,
1273        );
1274        tokio::pin!(stream);
1275
1276        let mut saw_error = false;
1277        while let Some(step) = stream.next().await {
1278            match step {
1279                Ok(Step::CallLlm(call)) => {
1280                    call.respond(Err(test_error("upstream model call failed")))?;
1281                }
1282                Ok(Step::Decision(_)) => {}
1283                Ok(Step::ReturnToAgent(..)) => {
1284                    return Err(test_error(
1285                        "expected the offload error to propagate, got a response",
1286                    ));
1287                }
1288                Err(err) => {
1289                    // The algorithm's `call_llm_target` saw the error via the promise.
1290                    assert!(err.to_string().contains("upstream model call failed"));
1291                    saw_error = true;
1292                }
1293            }
1294        }
1295
1296        assert!(saw_error, "expected an error step");
1297        Ok(())
1298    }
1299
1300    #[tokio::test]
1301    async fn dropping_the_stream_cancels_the_algorithm_task() -> Result<()> {
1302        use std::sync::atomic::{AtomicBool, Ordering};
1303        use std::time::Duration;
1304        use tokio::sync::mpsc;
1305
1306        // Sets a flag when dropped, so we can observe whether the algorithm task was
1307        // cancelled/dropped.
1308        struct DropGuard(Arc<AtomicBool>);
1309        impl Drop for DropGuard {
1310            fn drop(&mut self) {
1311                self.0.store(true, Ordering::SeqCst);
1312            }
1313        }
1314
1315        struct StuckAlgo {
1316            started: mpsc::UnboundedSender<()>,
1317            dropped: Arc<AtomicBool>,
1318        }
1319
1320        #[async_trait]
1321        impl Algorithm for StuckAlgo {
1322            fn name(&self) -> &str {
1323                "stuck"
1324            }
1325
1326            async fn create_run_task(
1327                self: Arc<Self>,
1328                _ctx: Context,
1329                _driver: Driver,
1330                _request: Request,
1331            ) -> Result<Response> {
1332                let _guard = DropGuard(self.dropped.clone());
1333                let _ = self.started.send(());
1334                // Await forever without ever touching the driver.
1335                std::future::pending::<()>().await;
1336                unreachable!()
1337            }
1338        }
1339
1340        let (started_tx, mut started_rx) = mpsc::unbounded_channel();
1341        let dropped = Arc::new(AtomicBool::new(false));
1342        let algo: Arc<dyn Algorithm> = Arc::new(StuckAlgo {
1343            started: started_tx,
1344            dropped: dropped.clone(),
1345        });
1346
1347        let stream = algo.run_stream(Context::default(), request(), None);
1348        started_rx
1349            .recv()
1350            .await
1351            .ok_or_else(|| test_error("task never started"))?;
1352        drop(stream);
1353        tokio::time::sleep(Duration::from_millis(100)).await;
1354
1355        assert!(
1356            dropped.load(Ordering::SeqCst),
1357            "algorithm task was NOT cancelled after dropping the stream"
1358        );
1359        Ok(())
1360    }
1361
1362    #[tokio::test]
1363    async fn create_run_task_panic_surfaces_as_a_stream_error() -> Result<()> {
1364        // An algorithm whose task panics must surface an `Err` step to the stream
1365        // consumer, not abort the process from an unobserved detached task.
1366        struct Panicky;
1367
1368        #[async_trait]
1369        impl Algorithm for Panicky {
1370            fn name(&self) -> &str {
1371                "panicky"
1372            }
1373
1374            async fn create_run_task(
1375                self: Arc<Self>,
1376                _ctx: Context,
1377                _driver: Driver,
1378                _request: Request,
1379            ) -> Result<Response> {
1380                panic!("boom");
1381            }
1382        }
1383
1384        let algo: Arc<dyn Algorithm> = Arc::new(Panicky);
1385        let stream = algo.run_stream(Context::default(), request(), None);
1386        tokio::pin!(stream);
1387
1388        let mut saw_error = false;
1389        while let Some(step) = stream.next().await {
1390            match step {
1391                Err(err) => {
1392                    assert!(matches!(err, LibsyError::AlgorithmTask { .. }));
1393                    saw_error = true;
1394                }
1395                Ok(_) => return Err(test_error("expected the panic to surface as an error step")),
1396            }
1397        }
1398
1399        assert!(saw_error, "expected an error step from the panicked task");
1400        Ok(())
1401    }
1402
1403    #[tokio::test]
1404    async fn run_returns_an_error_when_the_algorithm_task_panics() -> Result<()> {
1405        // The panic surfaces as an `Err` step inside `run_stream`; `run` propagates it
1406        // via `?`, so the caller gets an `Err` rather than a hang or a silent panic.
1407        struct Panicky;
1408
1409        #[async_trait]
1410        impl Algorithm for Panicky {
1411            fn name(&self) -> &str {
1412                "panicky"
1413            }
1414
1415            async fn create_run_task(
1416                self: Arc<Self>,
1417                _ctx: Context,
1418                _driver: Driver,
1419                _request: Request,
1420            ) -> Result<Response> {
1421                panic!("boom");
1422            }
1423        }
1424
1425        let algo: Arc<dyn Algorithm> = Arc::new(Panicky);
1426        match algo.run(Context::default(), request()).await {
1427            Ok(_) => Err(test_error(
1428                "expected run to surface the algorithm panic as an error",
1429            )),
1430            Err(err) => {
1431                assert!(matches!(err, LibsyError::AlgorithmTask { .. }));
1432                Ok(())
1433            }
1434        }
1435    }
1436
1437    #[tokio::test]
1438    async fn cancelling_run_cancels_the_algorithm_task() -> Result<()> {
1439        use std::sync::atomic::{AtomicBool, Ordering};
1440        use std::time::Duration;
1441        use tokio::sync::mpsc;
1442
1443        // Sets a flag when dropped, so we can observe whether the algorithm task was
1444        // cancelled once the `run` future driving it is dropped.
1445        struct DropGuard(Arc<AtomicBool>);
1446        impl Drop for DropGuard {
1447            fn drop(&mut self) {
1448                self.0.store(true, Ordering::SeqCst);
1449            }
1450        }
1451
1452        struct StuckAlgo {
1453            started: mpsc::UnboundedSender<()>,
1454            dropped: Arc<AtomicBool>,
1455        }
1456
1457        #[async_trait]
1458        impl Algorithm for StuckAlgo {
1459            fn name(&self) -> &str {
1460                "stuck"
1461            }
1462
1463            async fn create_run_task(
1464                self: Arc<Self>,
1465                _ctx: Context,
1466                _driver: Driver,
1467                _request: Request,
1468            ) -> Result<Response> {
1469                let _guard = DropGuard(self.dropped.clone());
1470                let _ = self.started.send(());
1471                // Hang forever without ever touching the driver, so only cancellation
1472                // (not a dropped step channel) can stop this task.
1473                std::future::pending::<()>().await;
1474                unreachable!()
1475            }
1476        }
1477
1478        let (started_tx, mut started_rx) = mpsc::unbounded_channel();
1479        let dropped = Arc::new(AtomicBool::new(false));
1480        let algo: Arc<dyn Algorithm> = Arc::new(StuckAlgo {
1481            started: started_tx,
1482            dropped: dropped.clone(),
1483        });
1484
1485        // Drive `run` on its own task, wait until the algorithm task is up, then cancel
1486        // `run` — dropping its future (and the `run_stream` stream it holds).
1487        let run_task = tokio::spawn(async move { algo.run(Context::default(), request()).await });
1488        started_rx
1489            .recv()
1490            .await
1491            .ok_or_else(|| test_error("task never started"))?;
1492        run_task.abort();
1493        tokio::time::sleep(Duration::from_millis(100)).await;
1494
1495        assert!(
1496            dropped.load(Ordering::SeqCst),
1497            "algorithm task was NOT cancelled after cancelling run"
1498        );
1499        Ok(())
1500    }
1501
1502    // --- first-wins hedging: `run` must not wait on losing speculative calls -------------
1503
1504    /// The loser: signals it has started serving (so the winner can then win with the
1505    /// loser's serve guaranteed in flight), then finishes late (`Some(delay)`) or never
1506    /// (`None`).
1507    struct LoserClient {
1508        started: Arc<tokio::sync::Notify>,
1509        delay: Option<std::time::Duration>,
1510    }
1511
1512    #[async_trait]
1513    impl RoutedLlmClient for LoserClient {
1514        async fn call(
1515            &self,
1516            _ctx: Context,
1517            _request: Request,
1518            decision: Arc<dyn Decision>,
1519        ) -> std::result::Result<Response, LlmClientError> {
1520            self.started.notify_one();
1521            match self.delay {
1522                Some(delay) => tokio::time::sleep(delay).await,
1523                None => std::future::pending::<()>().await,
1524            }
1525            Ok(Response {
1526                llm_response: LlmResponse::Agg(text_response(
1527                    None,
1528                    decision.selected_model().to_string(),
1529                )),
1530                metadata: None,
1531            })
1532        }
1533    }
1534
1535    /// The winner: waits until the loser's serve has started, then echoes immediately, so
1536    /// the loser's serve is guaranteed in flight when the winner wins.
1537    struct GatedEchoClient {
1538        gate: Arc<tokio::sync::Notify>,
1539    }
1540
1541    #[async_trait]
1542    impl RoutedLlmClient for GatedEchoClient {
1543        async fn call(
1544            &self,
1545            _ctx: Context,
1546            _request: Request,
1547            decision: Arc<dyn Decision>,
1548        ) -> std::result::Result<Response, LlmClientError> {
1549            self.gate.notified().await;
1550            Ok(Response {
1551                llm_response: LlmResponse::Agg(text_response(
1552                    None,
1553                    decision.selected_model().to_string(),
1554                )),
1555                metadata: None,
1556            })
1557        }
1558    }
1559
1560    /// Offloads two targets concurrently and returns the first to resolve, dropping the
1561    /// loser's call (first-wins hedging).
1562    struct Hedge {
1563        winner: LlmTarget,
1564        loser: LlmTarget,
1565    }
1566
1567    #[async_trait]
1568    impl Algorithm for Hedge {
1569        fn name(&self) -> &str {
1570            "hedge"
1571        }
1572
1573        async fn create_run_task(
1574            self: Arc<Self>,
1575            ctx: Context,
1576            driver: Driver,
1577            request: Request,
1578        ) -> Result<Response> {
1579            let dec_w: Arc<dyn Decision> = Arc::new(TestDecision {
1580                model: self.winner.semantic_name.clone(),
1581            });
1582            let dec_l: Arc<dyn Decision> = Arc::new(TestDecision {
1583                model: self.loser.semantic_name.clone(),
1584            });
1585            let win = driver.call_llm_target(ctx.clone(), &self.winner, request.clone(), dec_w);
1586            let lose = driver.call_llm_target(ctx, &self.loser, request, dec_l);
1587            // First to resolve wins; `select!` drops the losing future (and its promise).
1588            tokio::select! {
1589                res = win => res,
1590                res = lose => res,
1591            }
1592        }
1593    }
1594
1595    /// Builds a hedging algo whose winner is gated behind the loser starting, and whose
1596    /// loser finishes after `loser_delay` (or never, when `None`).
1597    fn hedge(loser_delay: Option<std::time::Duration>) -> Arc<dyn Algorithm> {
1598        let started = Arc::new(tokio::sync::Notify::new());
1599        let winner = LlmTarget {
1600            semantic_name: "winner".to_string(),
1601            llm_client: Some(Arc::new(GatedEchoClient {
1602                gate: started.clone(),
1603            })),
1604        };
1605        let loser = LlmTarget {
1606            semantic_name: "loser".to_string(),
1607            llm_client: Some(Arc::new(LoserClient {
1608                started,
1609                delay: loser_delay,
1610            })),
1611        };
1612        Arc::new(Hedge { winner, loser })
1613    }
1614
1615    #[tokio::test]
1616    async fn run_returns_the_winner_without_a_late_loser_overwriting_it() -> Result<()> {
1617        // The loser responds 50ms after the winner has already won. `run` must return the
1618        // winner, not the loser's `respond`-to-a-dropped-receiver error.
1619        let (_trace, response) = hedge(Some(std::time::Duration::from_millis(50)))
1620            .run(Context::default(), request())
1621            .await?;
1622        assert_eq!(
1623            response
1624                .llm_response
1625                .as_agg()
1626                .map(completion_text)
1627                .unwrap_or_default(),
1628            "winner"
1629        );
1630        Ok(())
1631    }
1632
1633    #[tokio::test]
1634    async fn run_returns_the_winner_without_hanging_on_a_pending_loser() -> Result<()> {
1635        // The loser never resolves. `run` must return the winner promptly, not hang
1636        // waiting for the in-flight loser.
1637        let run = hedge(None).run(Context::default(), request());
1638        let (_trace, response) = tokio::time::timeout(std::time::Duration::from_secs(1), run)
1639            .await
1640            .map_err(|error| LibsyError::external("waiting for pending loser", error))??;
1641        assert_eq!(
1642            response
1643                .llm_response
1644                .as_agg()
1645                .map(completion_text)
1646                .unwrap_or_default(),
1647            "winner"
1648        );
1649        Ok(())
1650    }
1651
1652    #[tokio::test]
1653    async fn run_surfaces_a_terminal_error_with_many_calls_in_flight() -> Result<()> {
1654        use std::sync::atomic::{AtomicUsize, Ordering};
1655
1656        // A large fan-out (10 matched the old, now-removed concurrency cap). The terminal
1657        // error must still reach the caller with all of these calls pending.
1658        const N: usize = 10;
1659
1660        // Enters each call; once all N are in flight, signals, then pends forever.
1661        struct EnterThenPend {
1662            started: Arc<AtomicUsize>,
1663            all_started: Arc<tokio::sync::Notify>,
1664            n: usize,
1665        }
1666
1667        #[async_trait]
1668        impl RoutedLlmClient for EnterThenPend {
1669            async fn call(
1670                &self,
1671                _ctx: Context,
1672                _request: Request,
1673                _decision: Arc<dyn Decision>,
1674            ) -> std::result::Result<Response, LlmClientError> {
1675                if self.started.fetch_add(1, Ordering::SeqCst) + 1 == self.n {
1676                    self.all_started.notify_one();
1677                }
1678                std::future::pending::<()>().await;
1679                unreachable!()
1680            }
1681        }
1682
1683        // Fans out N calls, then errors as soon as all N are in flight — exercising a
1684        // terminal failure emitted while the offloaded calls are still pending.
1685        struct FanOutThenError {
1686            target: LlmTarget,
1687            all_started: Arc<tokio::sync::Notify>,
1688            n: usize,
1689        }
1690
1691        #[async_trait]
1692        impl Algorithm for FanOutThenError {
1693            fn name(&self) -> &str {
1694                "fan_out_then_error"
1695            }
1696
1697            async fn create_run_task(
1698                self: Arc<Self>,
1699                ctx: Context,
1700                driver: Driver,
1701                request: Request,
1702            ) -> Result<Response> {
1703                let offloads = futures::future::join_all((0..self.n).map(|i| {
1704                    let decision: Arc<dyn Decision> = Arc::new(TestDecision {
1705                        model: format!("m{i}"),
1706                    });
1707                    driver.call_llm_target(ctx.clone(), &self.target, request.clone(), decision)
1708                }));
1709                tokio::select! {
1710                    _ = offloads => Err(test_error("offloads unexpectedly completed")),
1711                    _ = self.all_started.notified() => {
1712                        Err(test_error("terminal error while calls pending"))
1713                    }
1714                }
1715            }
1716        }
1717
1718        let all_started = Arc::new(tokio::sync::Notify::new());
1719        let target = LlmTarget {
1720            semantic_name: "pending".to_string(),
1721            llm_client: Some(Arc::new(EnterThenPend {
1722                started: Arc::new(AtomicUsize::new(0)),
1723                all_started: all_started.clone(),
1724                n: N,
1725            })),
1726        };
1727        let algo: Arc<dyn Algorithm> = Arc::new(FanOutThenError {
1728            target,
1729            all_started,
1730            n: N,
1731        });
1732
1733        // With the cap gone, `run` keeps polling the stream even with N calls in flight, so
1734        // the terminal error surfaces promptly instead of hanging.
1735        let run = algo.run(Context::default(), request());
1736        let result = tokio::time::timeout(std::time::Duration::from_millis(500), run)
1737            .await
1738            .map_err(|error| {
1739                LibsyError::external("waiting for terminal error with full call cap", error)
1740            })?;
1741        match result {
1742            Ok(_) => Err(test_error("expected the terminal error, got a response")),
1743            Err(err) => {
1744                assert!(
1745                    err.to_string()
1746                        .contains("terminal error while calls pending")
1747                );
1748                Ok(())
1749            }
1750        }
1751    }
1752}