Skip to main content

Crate cortex_router

Crate cortex_router 

Source
Expand description

§cortex-router

A fail-closed, model-free routing policy: given a task and its context, it decides between deterministic tooling, repository analysis, a bounded local model, or the upstream (strong) agent.

Model self-reported confidence is deliberately absent from the API. Routing is a function of inspectable inputs only: task text, evidence state, schema validity, token budgets, mutation authority, and local capability availability.

§Why

“Should a small local model handle this?” is a safety question, not an optimization question. This crate answers it with rules you can read, test, and audit:

  • Fail-closed guards. Missing or contradictory evidence, a schema failure, or a budget overrun always escalates upstream.
  • Mutation is never local. Anything likely to mutate code or state is reserved for the upstream agent.
  • High-risk classes are named. Security, authentication, concurrency, migration, release, deployment, and publication escalate by class.
  • Plans never exceed the caller’s bound. The returned ContextPlan reports at most the max_input_tokens the request declared.
use cortex_router::{route, EvidenceStatus, ExecutionTarget, RoutingRequest};

let mut request = RoutingRequest::new("Deploy the service to production");
request.evidence = EvidenceStatus::Verified;
let decision = route(&request);
assert_eq!(decision.target, ExecutionTarget::Upstream);
assert!(!decision.advisory_only);

let mut summary = RoutingRequest::new("Summarize the supplied evidence");
summary.evidence = EvidenceStatus::Verified;
assert_eq!(route(&summary).target, ExecutionTarget::Ollama);

§Scope and limits

Classification is lexical and currently tuned for English task text, which is a deliberate trade: it is fast, deterministic, and inspectable, with no model in the loop. Treat classify as a conservative first filter — it escalates when uncertain — and layer richer signals above it if you need them.

§License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.

Structs§

Classification
ContextPlan
LocalAvailability
RoutingDecision
RoutingRequest
TokenBudget

Enums§

ContextStrategy
EvidenceStatus
ExecutionTarget
ModelTier
MutationStatus
RiskLevel
RoutingReason
TaskClass

Constants§

UPSTREAM_EVIDENCE_TOKENS
Default evidence ceiling for upstream work when the caller allows more.

Functions§

classification_for_tier
Turn a model-chosen tier into a Classification, retaining lexical mutation cues and preferring a more specific lexical class when the tier matches.
classify
Classify work using stable lexical rules, without calling a model.
detector_disagreement
True when two or more deterministic lexicon families fire on the same task.
fold_text
Compatibility-normalize, case-fold, and collapse whitespace.
fold_words
Fold, then keep letters/digits and turn everything else into spaces so phrase matching can use word boundaries.
mixed_script
True when the task mixes Latin letters with another script.
parse_model_tier
Parse a closed-set label into a ModelTier.
policy_tier
Map a task class to the model tier the routing policy grants it.
route
Select a target through deterministic, fail-closed routing rules.
route_with_classification
Same policy as route, with a precomputed classification.
tier_rank
Rank tiers so under-calls are detectable without floating point.