KeiSeiKit-1.0/_primitives/_rust/kei-model-router/src/complexity.rs
Parfii-bot 187661714f fix(kei-model-router): close 10 audit-blocker findings
Codex CRITICAL + 4 HIGH + 5 MEDIUM/LOW from RULE 0.23 dual-review and
RULE 0.25 multi-critic swarm — all closed.

CRITICAL fix
  - Model::slug() ledger compatibility: posterior.rs + select_kernel.rs
    query `WHERE model = ?2 OR model = ?3`, binding canonical + legacy
    slug pair via new `Model::legacy_slug()`. Production ledger rows
    written under "haiku"/"sonnet"/"opus" remain visible to posterior
    aggregation. Regression test ledger_legacy_slug_counted.

HIGH fixes
  - cmd_select(): no longer early-returns on profile match. Profile's
    default_model_ref now becomes DecisionInput.fallback; select() always
    runs, posterior/kernel evidence wins if present. RULE 0.20 cost
    optimisation restored for all 18 registered agents.
  - Registry pricing SSoT: DecisionInput now carries Option<Arc<Registry>>.
    estimated_cost() tries registry first; hardcoded match is documented
    fallback only. select_posterior.rs no longer duplicates models.toml
    constants.
  - registry.rs portability: include_str!() embeds the three TOMLs at
    compile time. load_embedded() new; disk path tried first via
    KEI_REGISTRIES_DIR, embedded as fallback. `cargo install`d binaries
    now find registries unconditionally. embedded_registry_matches_disk
    test ensures embedded ≡ disk source.
  - next_model() ambiguity: replaced Option<&Model> with EscalationResult
    enum (Next(&Model) / AtTop / NotFound). Callers can distinguish typo
    from ceiling. 5 new tests.

MEDIUM fixes
  - posterior.rs u32 overflow: `(n_plus + n_minus) as u32` →
    `u32::try_from(n_plus.saturating_add(n_minus)).unwrap_or(u32::MAX)`.
    overflow_guard_on_huge_n test with i64::MAX.
  - pick() unknown-model: now returns None when default_model_ref's model
    is absent from registry. Inverted the deprecation guard.
  - HOME unset: disk_registries_dir() returns None on empty HOME and
    falls through to embedded registries. open_ledger() logs warning
    and returns None instead of opening at malformed path.
  - SQLite WAL + busy_timeout: applied to ledger connection in
    open_ledger() — concurrent CLI invocations no longer SQLITE_BUSY.

LOW fixes
  - impl Model consolidation: next_tier() moved to pricing.rs.
    escalate.rs uses current.next_tier() instead of duplicating logic.
  - complexity.rs: removed duplicate "ml-implementer" in HEAVY_ROLES.
  - dna_class.rs: role("") now returns None instead of Some("").

Verification (orchestrator-side, RULE 0.13 §Verify-before-commit):
  - cargo check        → clean
  - cargo test --release → 63 passed / 0 failed (was 58 → +5 new tests
    cover legacy-slug, EscalationResult, overflow, unknown-model, embedded)
  - Constructor Pattern → all files ≤ 200 LOC (max registry.rs 196)
  - Largest fn from_ledger 28 LOC / limit 30

DNA-INDEX.md regenerated by kei-registry hook (cosmetic).

=== STATUS-TRUTH MARKER ===
shipped: functional
stubs: 0
cargo-check: PASS
behaviour-verified: yes
follow-up-required:
  - (none from this commit; next audit pass before merge to main)
2026-05-13 22:09:19 +08:00

194 lines
6.1 KiB
Rust

//! Task-complexity heuristic.
//!
//! Maps (prompt, role) → τ ∈ [0, 1] via additive feature scoring. Pure
//! function, no LLM call. Fast classifier so router itself has near-zero
//! overhead.
//!
//! Calibration: weights are seeded from session observation; the
//! `calibrate` subcommand can re-fit them against ledger outcomes.
//!
//! Design: every signal contributes a clamped weight; total weight
//! divided by maximum-possible-weight gives τ. Returns matched feature
//! list for transparency / debugging.
//!
//! Constructor Pattern: pure-fn cube. No state, no I/O.
#[derive(Debug, Clone, serde::Serialize)]
pub struct ComplexityEstimate {
pub tau: f64,
pub features: Vec<&'static str>,
}
/// Tier mapping for human consumption: τ ∈ [0, 0.30] = lookup,
/// [0.30, 0.70] = multi-step, [0.70, 1.00] = architecture.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Tier {
Lookup,
MultiStep,
Architecture,
}
impl Tier {
pub fn from_tau(tau: f64) -> Self {
if tau < 0.30 {
Self::Lookup
} else if tau < 0.70 {
Self::MultiStep
} else {
Self::Architecture
}
}
}
/// High-complexity signals — bump τ up. Weight 0.20 each.
const HEAVY_KEYWORDS: &[&str] = &[
"architect", "derive", "proof", "theorem", "rewrite",
"redesign", "novel", "math", "spectral", "manifold",
"algorithm", "convergence", "asymptotic",
];
/// Mid-complexity signals — bump τ up. Weight 0.10 each.
const MID_KEYWORDS: &[&str] = &[
"refactor", "implement", "wire", "integrate", "test",
"audit", "review", "merge", "migration", "schema",
"endpoint", "trait", "async",
];
/// Low-complexity signals — bump τ DOWN. Weight 0.10 each (negative).
const LIGHT_KEYWORDS: &[&str] = &[
"list", "find", "where is", "what is", "search",
"grep", "show", "print", "display", "rename",
"format", "lookup",
];
/// Roles known to require architectural reasoning. Add 0.20 to τ if matched.
const HEAVY_ROLES: &[&str] = &[
"physics-deriver", "ml-implementer", "ml-researcher",
"kei-architect", "architect", "kei-critic", "critic",
"code-implementer-rust", "code-implementer",
"infra-implementer-iac",
];
/// Roles known to be read-only / lookup. Subtract 0.20 from τ.
const LIGHT_ROLES: &[&str] = &[
"Explore", "researcher-code", "researcher-web",
"validator-doc", "validator-version", "patent-compliance",
"keimd-expert",
];
const HEAVY_KW_W: f64 = 0.20;
const MID_KW_W: f64 = 0.10;
const LIGHT_KW_W: f64 = 0.10; // subtracted
const HEAVY_ROLE_W: f64 = 0.20;
const LIGHT_ROLE_W: f64 = 0.20; // subtracted
/// Empirical thresholds — prompt length signals.
const SHORT_PROMPT: usize = 100;
const LONG_PROMPT: usize = 800;
pub fn estimate(prompt: &str, role: Option<&str>) -> ComplexityEstimate {
let lower = prompt.to_lowercase();
let mut tau = 0.50; // baseline
let mut features: Vec<&'static str> = Vec::new();
for &kw in HEAVY_KEYWORDS {
if lower.contains(kw) {
tau += HEAVY_KW_W;
features.push("heavy_kw");
break; // count category once
}
}
for &kw in MID_KEYWORDS {
if lower.contains(kw) {
tau += MID_KW_W;
features.push("mid_kw");
break;
}
}
for &kw in LIGHT_KEYWORDS {
if lower.contains(kw) {
tau -= LIGHT_KW_W;
features.push("light_kw");
break;
}
}
if let Some(r) = role {
if HEAVY_ROLES.iter().any(|&h| h == r) {
tau += HEAVY_ROLE_W;
features.push("heavy_role");
}
if LIGHT_ROLES.iter().any(|&l| l == r) {
tau -= LIGHT_ROLE_W;
features.push("light_role");
}
}
let len = prompt.len();
if len < SHORT_PROMPT {
tau -= 0.10;
features.push("short_prompt");
} else if len > LONG_PROMPT {
tau += 0.10;
features.push("long_prompt");
}
let tau = tau.clamp(0.0, 1.0);
ComplexityEstimate { tau, features }
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn explore_lookup_is_low_tau() {
let e = estimate("find files matching pattern", Some("Explore"));
assert!(e.tau < 0.30, "expected lookup tier, got τ={}", e.tau);
assert_eq!(Tier::from_tau(e.tau), Tier::Lookup);
}
#[test]
fn architecture_prompt_is_high_tau() {
let prompt = "Architect a novel state-space derivation for the manifold-tangent \
proof of convergence in our spectral algorithm. The goal is to \
produce a theorem-backed asymptotic bound.";
let e = estimate(prompt, Some("physics-deriver"));
assert!(e.tau >= 0.70, "expected architecture tier, got τ={}", e.tau);
assert_eq!(Tier::from_tau(e.tau), Tier::Architecture);
}
#[test]
fn implementation_with_role_is_mid() {
let prompt = "Implement the kei-skills consumer endpoint with new tests.";
let e = estimate(prompt, Some("code-implementer-rust"));
// mid_kw + heavy_role + short → 0.5 + 0.10 + 0.20 - 0.10 = 0.70 (boundary)
assert!(e.tau >= 0.30, "got {}", e.tau);
}
#[test]
fn empty_prompt_minus_short_bonus_lands_at_baseline_minus_0_10() {
let e = estimate("", None);
assert_eq!(e.tau, 0.40);
assert!(e.features.contains(&"short_prompt"));
}
#[test]
fn clamps_to_unit_interval() {
// pile every signal: heavy_kw+mid_kw+long_prompt+heavy_role
let prompt = "Architect the novel algorithm: refactor implement wire test \
audit review merge migration schema endpoint trait async derive \
proof theorem rewrite redesign math spectral manifold convergence \
asymptotic. ".repeat(20);
let e = estimate(&prompt, Some("physics-deriver"));
assert!(e.tau >= 0.0 && e.tau <= 1.0);
}
#[test]
fn light_signals_subtract() {
let e = estimate("list files in directory", Some("Explore"));
assert!(e.tau < 0.50);
assert!(e.features.contains(&"light_kw"));
assert!(e.features.contains(&"light_role"));
}
}