use crate::config::{Config, ModelSpec, SecurityConfig, Tier, Trust};
use crate::jev::{questions, Jev};
use crate::state::Sensitivity;
use anyhow::{Context as _, Result};
use globset::{Glob, GlobSet, GlobSetBuilder};
use serde_json::json;
use std::path::Path;
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Price {
pub input: f64,
pub cached: f64,
pub output: f64,
}
const M: f64 = 1_000_000.0;
pub fn frontier_cost(y: f64, z: f64, f: &Price) -> f64 {
(f.output * y + f.input * z) / M
}
pub fn routed_cost(x_small: f64, y: f64, z: f64, back: f64, w: &Price, f: &Price) -> f64 {
(w.input * (x_small + z) + w.output * y + f.input * back) / M
}
#[derive(Clone, Debug)]
pub struct CostRow {
pub model: String,
pub tier: Tier,
pub est: f64,
pub note: String,
}
#[derive(Clone, Debug)]
pub struct RoutePlan {
pub model: String,
pub tier: Tier,
pub est_cost: f64,
pub p: f64,
pub sensitivity: Sensitivity,
pub alternatives: Vec<CostRow>,
pub reason: String,
}
pub struct SensitivityPolicy {
open: GlobSet,
restricted: GlobSet,
custom: GlobSet,
custom_excludes: Vec<String>,
}
fn globs(patterns: &[String]) -> Result<GlobSet> {
let mut b = GlobSetBuilder::new();
for p in patterns {
b.add(Glob::new(p).with_context(|| format!("bad glob {p}"))?);
}
Ok(b.build()?)
}
impl SensitivityPolicy {
pub fn new(cfg: &SecurityConfig) -> Result<Self> {
Ok(Self {
open: globs(&cfg.open)?,
restricted: globs(&cfg.restricted)?,
custom: globs(&cfg.custom)?,
custom_excludes: cfg.custom_excludes.clone(),
})
}
pub fn classify_path(&self, p: &Path) -> Sensitivity {
if self.custom.is_match(p) {
Sensitivity::Custom
} else if self.restricted.is_match(p) {
Sensitivity::Restricted
} else if self.open.is_match(p) {
Sensitivity::Open
} else {
Sensitivity::Standard
}
}
pub fn classify<'a>(&self, paths: impl IntoIterator<Item = &'a Path>) -> Option<Sensitivity> {
paths.into_iter().map(|p| self.classify_path(p)).max()
}
pub fn eligible(&self, m: &ModelSpec, s: Sensitivity) -> bool {
match s {
Sensitivity::Open => true,
Sensitivity::Standard => matches!(m.trust, Trust::FirstParty | Trust::Vetted),
Sensitivity::Restricted => matches!(m.trust, Trust::FirstParty),
Sensitivity::Custom => !self.custom_excludes.iter().any(|v| m.provider.eq_ignore_ascii_case(v) || m.id.contains(v.as_str())),
}
}
}
pub struct Router {
models: Vec<ModelSpec>,
policy: SensitivityPolicy,
frontier: String,
routing: bool,
}
impl Router {
pub fn new(cfg: &Config) -> Result<Self> {
Ok(Self {
models: cfg.models.clone(),
policy: SensitivityPolicy::new(&cfg.security)?,
frontier: cfg.llm.frontier.clone(),
routing: cfg.llm.routing,
})
}
pub fn policy(&self) -> &SensitivityPolicy {
&self.policy
}
pub fn frontier(&self) -> &ModelSpec {
self.models.iter().find(|m| m.id == self.frontier).unwrap_or(&self.models[0])
}
pub fn model(&self, id: &str) -> Option<&ModelSpec> {
self.models.iter().find(|m| m.id == id)
}
pub fn eligible(&self, s: Sensitivity) -> Vec<&ModelSpec> {
self.models.iter().filter(|m| self.policy.eligible(m, s)).collect()
}
pub fn costs(&self, ctx: u32, out: u32, reads: u32, s: Sensitivity) -> Vec<CostRow> {
let f = self.frontier().price();
let back = (out as f64 / 4.0).max(200.0);
self.eligible(s)
.into_iter()
.map(|m| {
let est = if m.tier == Tier::Frontier {
(f.input * ctx as f64) / M + frontier_cost(out as f64, reads as f64, &m.price())
} else {
routed_cost(ctx as f64, out as f64, reads as f64, back, &m.price(), &f)
};
let note = if m.tier == Tier::Frontier { "stays on frontier".to_string() } else { format!("purpose-built ctx {ctx} tok, {back:.0} tok back") };
CostRow { model: m.id.clone(), tier: m.tier, est, note }
})
.collect()
}
pub async fn choose(&self, subtask: &str, ctx: u32, out: u32, reads: u32, s: Sensitivity, jev: &Jev) -> Result<RoutePlan> {
let alternatives = self.costs(ctx, out, reads, s);
let frontier = self.frontier();
let frontier_cost = alternatives.iter().find(|r| r.model == frontier.id).map(|r| r.est).unwrap_or(0.0);
let mut plan = RoutePlan {
model: frontier.id.clone(),
tier: Tier::Frontier,
est_cost: frontier_cost,
p: 1.0,
sensitivity: s,
alternatives: alternatives.clone(),
reason: "routing disabled".into(),
};
if !self.routing {
return Ok(plan);
}
let eligible: Vec<&ModelSpec> = self.eligible(s).into_iter().filter(|m| m.tier != Tier::Cheap).collect();
if eligible.len() <= 1 {
plan.reason = format!("only {} is eligible for {s} data", frontier.id);
return Ok(plan);
}
let costs: serde_json::Map<String, serde_json::Value> = alternatives.iter().map(|r| (r.model.clone(), json!(r.est))).collect();
let (id, q) = questions::route(&eligible);
let state = json!({
"subtask": subtask,
"context_tokens": ctx,
"expected_output_tokens": out,
"expected_read_tokens": reads,
"sensitivity": s.to_string(),
"costs": costs,
});
let answer = jev.ask_one(state, &id, q).await?;
let (choice, p) = answer.as_choice().unwrap_or((frontier.id.as_str(), 0.0));
let chosen = eligible.iter().find(|m| m.id == choice).copied();
match chosen {
Some(m) if p >= 0.5 => {
plan.model = m.id.clone();
plan.tier = m.tier;
plan.est_cost = alternatives.iter().find(|r| r.model == m.id).map(|r| r.est).unwrap_or(0.0);
plan.p = p;
plan.reason = format!("jev chose {} (p={p:.2}); est ${:.4} vs frontier ${:.4}", m.id, plan.est_cost, frontier_cost);
}
Some(m) => {
plan.p = p;
plan.reason = format!("jev leaned {} at p={p:.2}, below 0.5; staying on frontier", m.id);
}
None => {
plan.reason = "jev choice not eligible; staying on frontier".into();
}
}
Ok(plan)
}
}
pub fn cost_table(x: f64, y: f64, z: f64, f: &Price, w: &Price) -> String {
let pure = f.output * y + f.input * z;
let load = w.input * x;
let gen = w.output * y;
let reads = w.input * z;
let reload = f.input * (y + z);
let routed = load + gen + reads + reload;
let x_small = x * 0.1;
let back = y * 0.25;
let smart = w.input * (x_small + z) + w.output * y + f.input * back;
let mut s = String::new();
s.push_str(&format!("session shape: X={x:.2}M context, Y={y:.2}M generated, Z={z:.2}M read\n\n"));
s.push_str(&format!("Path 1 pure frontier\n generate {:>6.2} read {:>6.2}\n total {pure:>6.2}\n\n", f.output * y, f.input * z));
s.push_str(&format!(
"Path 2 frontier → worker → frontier (full transcript)\n worker loads context {load:>6.2}\n worker generates {gen:>6.2}\n worker reads {reads:>6.2}\n frontier reloads {reload:>6.2}\n total {routed:>6.2}\n\n"
));
s.push_str(&format!(
"Path 3 frontier → worker → frontier (jev harness)\n worker gets a purpose-built context of {x_small:.3}M and returns a scored chunk of {back:.3}M\n total {smart:>6.2}\n\n"
));
s.push_str(&format!(
"routing the naive way costs {:.0}% of pure frontier; the harness way costs {:.0}%\n",
routed / pure * 100.0,
smart / pure * 100.0
));
s
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn paper_numbers_reproduce() {
let opus = Price { input: 5.0, cached: 0.5, output: 25.0 };
let sonnet = Price { input: 3.0, cached: 0.3, output: 15.0 };
let (x, y, z) = (0.65, 0.12, 0.23);
let pure = opus.output * y + opus.input * z;
let routed = sonnet.input * x + sonnet.output * y + sonnet.input * z + opus.input * (y + z);
assert!((pure - 4.15).abs() < 0.01);
assert!((routed - 6.19).abs() < 0.01);
}
#[test]
fn sensitivity_orders_and_filters() {
let cfg = SecurityConfig::default();
let p = SensitivityPolicy::new(&cfg).unwrap();
assert_eq!(p.classify_path(Path::new("src/main.rs")), Sensitivity::Standard);
assert_eq!(p.classify_path(Path::new("docs/guide.md")), Sensitivity::Open);
assert_eq!(p.classify_path(Path::new("config/.env.local")), Sensitivity::Restricted);
let paths = [Path::new("docs/a.md"), Path::new(".env")];
assert_eq!(p.classify(paths.iter().copied()), Some(Sensitivity::Restricted));
}
}