KeiSeiKit-1.0/_primitives/_rust/kei-model-router/src/select_kernel.rs
Parfii-bot 8302261e1f feat(kei-model-router): registry-driven, three-layer DNA
Removes hardcoded Claude-only Model enum. Pricing constants now read
from _blocks/registries/models.toml at startup; provider/model lookup
goes through a typed Registry returned by registry.rs.

New API surface:
  - Registry::load(dir) → (providers, models, profiles)
  - pick(profile_id, &Registry) → Result<(provider_id, model_id)>
  - cost_micro_cents(model_id, in, out, &Registry) → Option<u64>
  - next_model(model_id, &Registry) → Option<&Model> (ascending cost,
    same provider, skip deprecated)

Files:
  - registry_types.rs      new   107 LOC  (Provider/Model/Profile structs)
  - registry.rs            new   152 LOC  (TOML load + lookups)
  - pricing.rs             rew   127 LOC  (registry-backed, no constants)
  - escalate.rs            rew   181 LOC  (registry-backed ladder + skip deprecated)
  - select.rs              rew   131 LOC
  - select_kernel.rs       new    74 LOC  (Constructor-Pattern split)
  - select_posterior.rs    new   178 LOC  (Constructor-Pattern split)
  - posterior.rs           rew   197 LOC
  - calibrate.rs           rew   175 LOC
  - lib.rs                 rew    53 LOC
  - main.rs                rew   163 LOC  (CLI updated to new API)
  - Cargo.toml             dep   added toml 0.8

Verification (orchestrator-side, RULE 0.13 §Verify-before-commit):
  - cargo check                 → clean
  - cargo test --release        → 58 passed / 0 failed / 0 ignored
  - LOC limit (Constructor)     → max 197 / limit 200
  - largest fn cmd_select       → ~27 LOC / limit 30

DNA-INDEX.md regenerated by kei-registry hook (primitive count
144 → 150 reflects the 6 new/split modules).

=== STATUS-TRUTH MARKER ===
shipped: functional
stubs: 0
cargo-check: PASS
behaviour-verified: yes
follow-up-required:
  - select.rs `estimated_cost` still embeds inline cost constants
    mirroring models.toml; if non-Anthropic providers need dynamic
    pricing in select-time estimation, thread Registry through.
  - External callers of old `cost_micro_cents(Model, ...)` signature
    will break — intentional, no external callers in this workspace.
2026-05-13 21:23:53 +08:00

74 lines
2.3 KiB
Rust

//! Kernel-smoothed posterior fallback for the empirical selector.
//!
//! When a task-class has no direct ledger entries, borrows posterior mass
//! from neighbouring task-classes weighted by DNA similarity.
//!
//! Constructor Pattern: SQL cube — separated from select.rs to keep both files <200 LOC.
use crate::kernel::{self, KernelWeights};
use crate::posterior::Posterior;
use crate::pricing::Model;
use rusqlite::{Connection, Result as SqlResult};
const QUERY: &str = "SELECT task_class_dna,
SUM(CASE WHEN outcome = 'functional'
AND COALESCE(escalation_depth, 0) = 0
THEN 1 ELSE 0 END) AS np,
SUM(CASE WHEN outcome IS NOT NULL
AND NOT (outcome = 'functional'
AND COALESCE(escalation_depth, 0) = 0)
THEN 1 ELSE 0 END) AS nm
FROM agents
WHERE task_class_dna IS NOT NULL
AND task_class_dna != ?1
AND model = ?2
GROUP BY task_class_dna";
/// Weighted-sum posterior borrowing from neighbour task-classes.
///
/// Returns a Beta posterior with `alpha`/`beta` inflated by kernel similarity.
/// Starts from a uniform prior (alpha=1, beta=1) and accumulates evidence.
pub fn smooth(
conn: &Connection,
target_task_class: &str,
model: Model,
weights: KernelWeights,
) -> SqlResult<Posterior> {
let mut stmt = conn.prepare(QUERY)?;
let rows = stmt.query_map(
rusqlite::params![target_task_class, model.slug()],
|r| {
Ok((
r.get::<_, String>(0)?,
r.get::<_, Option<i64>>(1)?.unwrap_or(0),
r.get::<_, Option<i64>>(2)?.unwrap_or(0),
))
},
)?;
accumulate_weighted(rows, target_task_class, weights)
}
fn accumulate_weighted(
rows: impl Iterator<Item = rusqlite::Result<(String, i64, i64)>>,
target: &str,
weights: KernelWeights,
) -> SqlResult<Posterior> {
let mut alpha = 1.0_f64;
let mut beta = 1.0_f64;
let mut n = 0_u32;
for row in rows {
let (other_tc, np, nm) = row?;
let sim = kernel::similarity(target, &other_tc, weights);
if sim <= 0.0 {
continue;
}
alpha += sim * np as f64;
beta += sim * nm as f64;
n = n.saturating_add((np + nm) as u32);
}
Ok(Posterior { alpha, beta, n })
}