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
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 fn llm_router_handle(&self) -> LlmRouterHandle
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).
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.
Auto Trait Implementations§
impl !RefUnwindSafe for Router
impl !UnwindSafe for Router
impl Freeze for Router
impl Send for Router
impl Sync for Router
impl Unpin for Router
impl UnsafeUnpin for Router
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> 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