[ABI][KN][LD][CC][RT][LAB][SP][NT][TM][GW][Proofs][Docs] M0.5: realign workspace to vetta-infer-* microkernel architecture

Dissolve the M0 crates (core/io/proto/telemetry/hip/cli) into the approved
service layout abi/kn/ld/cc/rt/lab/sp/nt/tm/gw (docs/ARCHITECTURE.md).

- kn: Lean-verified control bus + service registry (bus_lifecycle.lean, 14 thms)
  and SlabRing/UMA broker; rt: PrefixState + hybrid checkpoint pool.
- sp: faithful throughput-aware Adaptive-K port of clover_policy.h
  (cooldowns, probes, throughput guards) with scenario parity tests.
- gw: strata-ctl-compatible wire emitters + serve session FSM
  (serve_session.lean, 10 thms); abi::serve accepts image= (Vision delta closed).
- abi: pure contracts, checked JSON/VTC1/GEN parsers, zero-alloc sampler.
- ld/cc: checked safetensors reader + write-once VTC1 writer / O_RDONLY reader.
- tm: lock-free telemetry; nt: LyraRpc; lab shares the rt executor core.
- Docs: ARCHITECTURE.md decision-complete spec (MoE-only scope, .vbundle,
  .vplan->CompiledPlan, wire contract, migration map); AGENTS.md Lean rule
  + tandem/review workflow.

Verified live: cargo test --workspace -D warnings = 46/46 PASS;
lean proofs = 34 theorems (slab_ring_dma 10, bus_lifecycle 14, serve_session 10),
0 sorryAx, standard axioms only; vetta-infer info on Inkling = 1244 tensors,
18.18 GiB, checked bounds.
This commit is contained in:
adm committed 2026-11-30 00:54:00 +01:00
1 parent 63b1668a92
commit 7b50f1c07c
49 files changed
+3211 -1208

No files matched your search

+7
View File
@@ -0,0 +1,7 @@
[package]
name = "vetta-infer-sp"
version.workspace = true
edition.workspace = true
license.workspace = true
[dependencies]
+378
View File
@@ -0,0 +1,378 @@
//! Throughput-aware Adaptive-K controller — FAITHFUL port of the measured
//! `strata-clover/engine/src/clover/clover_policy.h` `AdaptiveKController`
//! (the policy behind 21.0 tok/s prose / 42.1 tok/s code, 100% exact vs AR).
//!
//! Ported elements (the M0 sketch lacked all of them):
//! - cooldowns: `ar_cooldown` (8), `k8_cooldown` (12), `k16_cooldown` (12);
//! - `just_probed` one-shot sensitivity after an AR->K=4 probe;
//! - per-band throughput guards (EMA ms/tok ratios 1.06 / 1.06 / 1.05);
//! - promotion to K=16 only on a full 8/8 streak x2 AND `k16_cooldown == 0`
//! (the prose trap fix, commit `f2a79ada4`);
//! - EMA smoothing 0.65/0.35 over K buckets {1, 4, 8, 16}.
//!
//! Lossless exactness vs AR for every K is inherited from the verification
//! machine (see `strata-clover/proofs/clover_gdn_ckpt.lean`,
//! `adaptive_spec_exact_vs_ar`); this controller only chooses K.
/// Mirrors `clamp_adaptive_k`.
fn clamp_k(k: usize, k_max: usize) -> usize {
if k_max <= 1 {
return 1;
}
k.max(1).min(k_max)
}
#[derive(Debug, Clone)]
pub struct AdaptiveKController {
k_max: usize,
cur_k: usize,
/// Plain AR (K=1) steps remaining before the next K=4 probe.
ar_cooldown: u32,
/// K=8 steps remaining before re-trying K=16 after throughput demotion.
k16_cooldown: u32,
/// K=4 steps remaining before re-trying K=8 after throughput demotion.
k8_cooldown: u32,
low_streak: u32,
high_streak: u32,
just_probed: bool,
/// Empirical ms/tok EMA for K buckets {1, 4, 8, 16}.
ema_mpt: [f64; 4],
}
impl AdaptiveKController {
pub fn new(k_max: usize) -> Self {
let km = k_max.max(1);
Self {
k_max: km,
cur_k: clamp_k(if km >= 8 { 8 } else if km >= 4 { 4 } else { 1 }, km),
ar_cooldown: 0,
k16_cooldown: 0,
k8_cooldown: 0,
low_streak: 0,
high_streak: 0,
just_probed: false,
ema_mpt: [0.0; 4],
}
}
pub fn reset(&mut self, k_max: usize) {
*self = Self::new(k_max);
}
pub fn k_max(&self) -> usize {
self.k_max
}
pub fn cur_k(&self) -> usize {
self.cur_k
}
fn bucket(k: usize) -> usize {
if k <= 1 {
0
} else if k <= 4 {
1
} else if k <= 8 {
2
} else {
3
}
}
/// Mirrors `next_k(rem_tokens, ctx_room)`.
pub fn next_k(&self, rem_tokens: usize, ctx_room: usize) -> usize {
let cap = self.k_max.min(rem_tokens.min(ctx_room));
if cap <= 1 {
return 1;
}
if self.ar_cooldown > 0 {
return 1;
}
clamp_k(self.cur_k, cap)
}
/// Mirrors `observe(used_k, accepted_tokens, step_ms)` branch by branch.
pub fn observe(&mut self, used_k: usize, accepted_tokens: usize, step_ms: f64) {
if self.k_max <= 1 {
self.cur_k = 1;
return;
}
let acc = accepted_tokens.max(1);
let b = Self::bucket(used_k);
if step_ms > 0.0 {
let mpt = step_ms / acc as f64;
self.ema_mpt[b] = if self.ema_mpt[b] <= 0.0 {
mpt
} else {
0.65 * self.ema_mpt[b] + 0.35 * mpt
};
}
if used_k <= 1 {
if self.ar_cooldown > 0 {
self.ar_cooldown -= 1;
}
if self.ar_cooldown == 0 {
self.cur_k = clamp_k(4, self.k_max);
self.just_probed = true;
self.low_streak = 0;
self.high_streak = 0;
}
return;
}
if used_k <= 4 {
if self.k8_cooldown > 0 {
self.k8_cooldown -= 1;
}
// Throughput guard: K=4 measurably slower than observed AR -> AR cooldown.
if self.ema_mpt[1] > 0.0
&& self.ema_mpt[0] > 0.0
&& self.ema_mpt[1] > self.ema_mpt[0] * 1.06
{
self.low_streak += 1;
self.high_streak = 0;
if self.just_probed || self.low_streak >= 2 {
self.cur_k = 1;
self.ar_cooldown = 8;
self.low_streak = 0;
self.just_probed = false;
return;
}
}
if accepted_tokens >= 3 {
if self.k8_cooldown == 0 {
self.cur_k = clamp_k(8, self.k_max);
self.low_streak = 0;
self.high_streak = 0;
self.just_probed = false;
} else {
self.low_streak = 0;
self.just_probed = false;
}
} else if accepted_tokens <= 2 {
self.low_streak += 1;
self.high_streak = 0;
if self.just_probed || self.low_streak >= 2 {
self.cur_k = 1;
self.ar_cooldown = 8;
self.low_streak = 0;
self.just_probed = false;
}
} else {
self.just_probed = false;
}
return;
}
if used_k <= 8 {
self.just_probed = false;
if self.k16_cooldown > 0 {
self.k16_cooldown -= 1;
}
// Throughput guard: K=8 measurably slower than observed K=4 -> step back.
if self.ema_mpt[2] > 0.0
&& self.ema_mpt[1] > 0.0
&& self.ema_mpt[2] > self.ema_mpt[1] * 1.06
{
self.low_streak += 1;
self.high_streak = 0;
if self.low_streak >= 2 {
self.cur_k = clamp_k(4, self.k_max);
self.k8_cooldown = 12;
self.low_streak = 0;
return;
}
}
// Promote to K=16 only on full 8/8 acceptance streak (prose trap fix).
if accepted_tokens >= 8 {
self.high_streak += 1;
self.low_streak = 0;
if self.high_streak >= 2 && self.k_max >= 16 && self.k16_cooldown == 0 {
self.cur_k = clamp_k(16, self.k_max);
self.high_streak = 0;
}
} else if accepted_tokens <= 3 {
self.low_streak += 1;
self.high_streak = 0;
if self.low_streak >= 2 {
self.cur_k = clamp_k(4, self.k_max);
self.low_streak = 0;
}
} else {
self.high_streak = 0;
self.low_streak = 0;
}
return;
}
// used_k > 8 (e.g. 16): require >= 10 accepted AND competitive ms/tok to stay.
self.just_probed = false;
if self.ema_mpt[3] > 0.0
&& self.ema_mpt[2] > 0.0
&& self.ema_mpt[3] > self.ema_mpt[2] * 1.05
{
self.cur_k = clamp_k(8, self.k_max);
self.k16_cooldown = 12;
self.low_streak = 0;
self.high_streak = 0;
return;
}
if accepted_tokens <= 9 {
self.low_streak += 1;
self.high_streak = 0;
if self.low_streak >= 2 {
self.cur_k = clamp_k(8, self.k_max);
self.k16_cooldown = 12;
self.low_streak = 0;
}
} else {
self.low_streak = 0;
}
}
}
#[cfg(test)]
mod tests {
use super::*;
// Scenario parity tests: expected values derived by hand from the C++
// reference (clover_policy.h), branch by branch.
#[test]
fn initial_state_and_caps() {
let c = AdaptiveKController::new(16);
assert_eq!(c.cur_k(), 8);
assert_eq!(c.next_k(100, 100), 8);
assert_eq!(c.next_k(3, 100), 3); // rem_tokens cap
assert_eq!(c.next_k(100, 1), 1); // ctx_room cap <= 1
let c4 = AdaptiveKController::new(4);
assert_eq!(c4.cur_k(), 4);
let c1 = AdaptiveKController::new(1);
assert_eq!(c1.cur_k(), 1);
assert_eq!(c1.next_k(100, 100), 1);
}
#[test]
fn k_max_one_forces_ar() {
let mut c = AdaptiveKController::new(1);
c.observe(16, 16, 10.0);
assert_eq!(c.cur_k(), 1);
assert_eq!(c.next_k(100, 100), 1);
}
#[test]
fn promotion_requires_two_full_streaks_and_no_cooldown() {
let mut c = AdaptiveKController::new(16);
c.observe(8, 8, 80.0); // high_streak = 1, not yet 16
assert_eq!(c.cur_k(), 8);
c.observe(8, 8, 80.0); // high_streak = 2 -> promote
assert_eq!(c.cur_k(), 16);
assert_eq!(c.next_k(100, 100), 16);
}
#[test]
fn prose_trap_k16_slower_than_k8_demotes_with_cooldown() {
let mut c = AdaptiveKController::new(16);
// Establish K=8 at 10 ms/tok, then promote to 16.
c.observe(8, 8, 80.0);
c.observe(8, 8, 80.0);
assert_eq!(c.cur_k(), 16);
// K=16 step: 16 accepted in 200 ms = 12.5 ms/tok > 10 * 1.05 -> demote.
c.observe(16, 16, 200.0);
assert_eq!(c.cur_k(), 8);
assert_eq!(c.next_k(100, 100), 8);
// During the 12-step cooldown, full 8/8 streaks must NOT re-promote.
for _ in 0..11 {
c.observe(8, 8, 80.0);
assert_eq!(c.cur_k(), 8, "promotion must wait out k16_cooldown");
}
// 12th observe drains the cooldown; the accumulated full-acceptance
// streak (never reset while cooling down) immediately re-promotes.
c.observe(8, 8, 80.0);
assert_eq!(c.cur_k(), 16);
}
#[test]
fn k16_low_acceptance_demotes_after_two_strikes() {
let mut c = AdaptiveKController::new(16);
c.observe(8, 8, 80.0);
c.observe(8, 8, 80.0); // now K=16
c.observe(16, 5, 0.0); // low_streak = 1, stays 16
assert_eq!(c.cur_k(), 16);
c.observe(16, 5, 0.0); // low_streak = 2 -> demote + cooldown
assert_eq!(c.cur_k(), 8);
}
#[test]
fn ar_probe_cycle_and_one_shot_probe_sensitivity() {
let mut c = AdaptiveKController::new(16);
// Two weak K=4 steps -> drop to AR with cooldown 8.
c.observe(4, 1, 0.0);
c.observe(4, 1, 0.0);
assert_eq!(c.cur_k(), 1);
assert_eq!(c.next_k(100, 100), 1); // ar_cooldown forces K=1
// Drain the 8 AR steps; the 8th re-probes K=4 with just_probed.
for _ in 0..8 {
c.observe(1, 1, 0.0);
}
assert_eq!(c.cur_k(), 4);
// A single weak probe step immediately drops back (just_probed).
c.observe(4, 1, 0.0);
assert_eq!(c.cur_k(), 1);
assert_eq!(c.next_k(100, 100), 1);
}
#[test]
fn k4_promotes_to_k8_on_three_accepted() {
let mut c = AdaptiveKController::new(16);
c.observe(4, 3, 0.0);
assert_eq!(c.cur_k(), 8);
}
#[test]
fn k8_throughput_guard_steps_back_to_k4_with_cooldown() {
let mut c = AdaptiveKController::new(16);
// K=4 at 5 ms/tok promotes to K=8.
c.observe(4, 4, 20.0);
assert_eq!(c.cur_k(), 8);
// One weak K=8 step (no timing): low_streak = 1, survives (no demotion yet).
c.observe(8, 3, 0.0);
assert_eq!(c.cur_k(), 8);
// K=8 at 10 ms/tok (> 5 * 1.06): guard sees low_streak 1 -> 2 -> demote + cooldown.
c.observe(8, 8, 80.0);
assert_eq!(c.cur_k(), 4);
// During k8_cooldown, good K=4 acceptance must not re-promote to 8.
for _ in 0..11 {
c.observe(4, 4, 20.0);
assert_eq!(c.cur_k(), 4);
}
c.observe(4, 4, 20.0); // cooldown drained, accepted >= 3 -> promote
assert_eq!(c.cur_k(), 8);
}
#[test]
fn k4_throughput_guard_vs_ar_one_shot_probe() {
let mut c = AdaptiveKController::new(16);
// AR at 5 ms/tok; cooldown == 0 re-probes K=4 with just_probed.
c.observe(1, 1, 5.0);
assert_eq!(c.cur_k(), 4);
// K=4 at 10 ms/tok (> 5 * 1.06): a single weak probe step drops back
// immediately (just_probed), with the 8-step AR cooldown.
c.observe(4, 4, 40.0);
assert_eq!(c.cur_k(), 1);
assert_eq!(c.next_k(100, 100), 1);
}
#[test]
fn reset_restores_initial_state() {
let mut c = AdaptiveKController::new(16);
c.observe(8, 8, 80.0);
c.observe(8, 8, 80.0);
c.observe(16, 5, 0.0);
c.observe(16, 5, 0.0);
c.reset(16);
assert_eq!(c.cur_k(), 8);
assert_eq!(c.next_k(100, 100), 8);
}
}
+8
View File
@@ -0,0 +1,8 @@
//! `vetta-infer-sp`: speculative coordinator (docs/ARCHITECTURE.md §2).
//!
//! Lossless verification (Leviathan, in `abi`), throughput-aware Adaptive-K,
//! local MTP/Block-Diffusion drafting or remote via `nt`. Every policy here is
//! a faithful port of the measured strata reference, with scenario parity
//! tests — never a simplified re-interpretation.
pub mod adaptive_k;