pub struct Router {
pub config: RouterConfig,
pub available_agents: Vec<String>,
pub weights: AgentWeights,
/* private fields */
}Expand description
The agent router.
Fields§
§config: RouterConfigRouter configuration
available_agents: Vec<String>Available agents discovered at runtime
weights: AgentWeightsPer-agent rate limit weights (used when weighted_round_robin is enabled)
Implementations§
Source§impl Router
impl Router
pub fn available_agent_count(&self) -> usize
Sourcepub fn review_agents_all_cooled(&self) -> bool
pub fn review_agents_all_cooled(&self) -> bool
Agents are installed but every review agent is cooled down or degraded
pub fn has_routable_review_agent(&self) -> bool
pub fn model_for_complexity( &self, agent: &str, complexity: &str, task_id: &str, ) -> Option<String>
Sourcepub fn new(config: RouterConfig) -> Self
pub fn new(config: RouterConfig) -> Self
Create a new router with the given configuration.
Sourcepub fn from_config() -> Self
pub fn from_config() -> Self
Create a router with default configuration loaded from files.
Sourcepub async fn reload_async(&mut self)
pub async fn reload_async(&mut self)
Async reload that uses async pool expansion to avoid blocking the runtime when discovering opencode free models.
Sourcepub async fn refresh_health(&self, store: &Arc<TaskStore>)
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.
Sourcepub fn is_agent_available(&self, agent: &str) -> bool
pub fn is_agent_available(&self, agent: &str) -> bool
Check if an agent is available.
Sourcepub fn healthy_agent_count(&self, complexity: &str) -> usize
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.
Sourcepub fn is_degraded(&self, threshold: usize) -> bool
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.
pub fn earliest_cooldown_until(&self, complexity: Option<&str>) -> Option<i64>
Sourcepub fn next_round_robin_agent(
&mut self,
exclude: &[&str],
complexity: &str,
) -> Option<String>
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).
Sourcepub async fn route(
&mut self,
task: &ExternalTask,
store: &Arc<TaskStore>,
repo: &str,
) -> Result<RouteResult>
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):
- Check for
agent:*label — use that agent directly - If weighted_round_robin enabled, select by capacity-weighted probability
- If round_robin mode, cycle through agents (stateful)
- Call LLM classifier for intelligent routing
- After max_route_attempts LLM failures, fall back to round-robin
Sourcepub fn record_rate_limit(&mut self, agent: &str)
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.
Sourcepub fn record_success(&mut self, agent: &str)
pub fn record_success(&mut self, agent: &str)
Record a successful task completion, restoring agent weight.
Sourcepub fn tick_weight_recovery(&mut self)
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.
Sourcepub async fn store_route_result(
&self,
task_id: &str,
result: &RouteResult,
store: &Arc<TaskStore>,
repo: &str,
) -> Result<()>
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.
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for Router
impl !UnwindSafe for Router
impl Forget for Router
impl Freeze for Routerwhere
*const u8 is !null: Freeze,
PhantomData<u8>: Freeze,
PhantomData<(String, NamedProfile)>: Freeze,
PhantomData<String>: Freeze,
PhantomData<(String, HashMap<String, Vec<String>>)>: Freeze,
PhantomData<(String, f64)>: Freeze,
PhantomData<(String, i64)>: Freeze,
PhantomData<(String, RateLimitState)>: Freeze,
*const ArcInner<LlmRouter> is !null: Freeze,
PhantomData<ArcInner<LlmRouter>>: Freeze,
*const ArcInner<ClientRef> is !null: Freeze,
PhantomData<ArcInner<ClientRef>>: Freeze,
PhantomData<(String, String)>: Freeze,
impl Move for Router
impl Send for Router
impl Sync for Router
impl Unpin for Router
impl UnsafeUnpin for Routerwhere
*const u8 is !null: UnsafeUnpin,
PhantomData<u8>: UnsafeUnpin,
PhantomData<(String, NamedProfile)>: UnsafeUnpin,
PhantomData<String>: UnsafeUnpin,
PhantomData<(String, HashMap<String, Vec<String>>)>: UnsafeUnpin,
PhantomData<(String, f64)>: UnsafeUnpin,
PhantomData<(String, i64)>: UnsafeUnpin,
PhantomData<(String, RateLimitState)>: UnsafeUnpin,
*const ArcInner<LlmRouter> is !null: UnsafeUnpin,
PhantomData<ArcInner<LlmRouter>>: UnsafeUnpin,
*const ArcInner<ClientRef> is !null: UnsafeUnpin,
PhantomData<ArcInner<ClientRef>>: UnsafeUnpin,
PhantomData<(String, String)>: UnsafeUnpin,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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