Skip to main content

Router

Struct Router 

Source
pub struct Router {
    pub config: RouterConfig,
    pub available_agents: Vec<String>,
    pub weights: AgentWeights,
    /* private fields */
}
Expand description

The agent router.

Fields§

§config: RouterConfig

Router configuration

§available_agents: Vec<String>

Available agents discovered at runtime

§weights: AgentWeights

Per-agent rate limit weights (used when weighted_round_robin is enabled)

Implementations§

Source§

impl Router

Source

pub fn new(config: RouterConfig) -> Self

Create a new router with the given configuration.

Source

pub fn from_config() -> Self

Create a router with default configuration loaded from files.

Source

pub fn llm_router_handle(&self) -> LlmRouterHandle

Return a cloned handle to the LLM router subsystem.

Callers that need to invoke async methods on the underlying LlmRouter should clone this handle before releasing any RwLock read guard on Router, then call the async method after the guard is dropped. This prevents holding the read lock across await points (which starves write-lock waiters due to Tokio’s write-preferring RwLock).

Source

pub async fn reload_async(&mut self)

Async reload that uses async pool expansion to avoid blocking the runtime when discovering opencode free models.

Source

pub async fn refresh_health(&self, store: &Arc<TaskStore>)

Run the pre-emptive health check, refreshing the degraded-agent set.

Queries the rate_limits table for recent events and combines with cooldown state to mark agents as degraded before routing attempts them.

Source

pub fn is_agent_available(&self, agent: &str) -> bool

Check if an agent is available.

Source

pub fn healthy_agent_count(&self, complexity: &str) -> usize

Count of healthy (routable) agents for a given complexity. Returns the number of agents that are available, not in cooldown, and have an available model.

Source

pub fn is_degraded(&self, threshold: usize) -> bool

Check if the system is in degraded mode (fewer than threshold healthy agents). Uses “simple” complexity as the baseline since it has the widest model support.

Source

pub fn earliest_cooldown_until(&self, complexity: Option<&str>) -> Option<i64>

Source

pub fn next_round_robin_agent( &mut self, exclude: &[&str], complexity: &str, ) -> Option<String>

Get the first available agent. Pick next agent via round-robin (for review or other non-task routing). Pick the next review agent, optionally excluding one (e.g. the task’s original agent). Falls back to the first candidate whenever every candidate is excluded (#3645).

Source

pub async fn route( &mut self, task: &ExternalTask, store: &Arc<TaskStore>, repo: &str, ) -> Result<RouteResult>

Route a task to the best agent.

Routing logic (in priority order):

  1. Check for agent:* label — use that agent directly
  2. If weighted_round_robin enabled, select by capacity-weighted probability
  3. If round_robin mode, cycle through agents (stateful)
  4. Call LLM classifier for intelligent routing
  5. After max_route_attempts LLM failures, fall back to round-robin
Source

pub fn record_rate_limit(&mut self, agent: &str)

Record a rate limit event for an agent, reducing its routing weight.

Called by the engine when an agent returns a 429/rate limit error.

Source

pub fn record_success(&mut self, agent: &str)

Record a successful task completion, restoring agent weight.

Source

pub fn tick_weight_recovery(&mut self)

Tick weight recovery for all agents.

Called periodically by the engine to gradually restore weights as rate limit windows expire.

Source

pub async fn store_route_result( &self, task_id: &str, result: &RouteResult, store: &Arc<TaskStore>, repo: &str, ) -> Result<()>

Store routing result in the task store.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more