File size: 22,474 Bytes
1269259 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 | // SPF Smart Gateway - Online Learning Engine (Block M)
// Copyright 2026 Joseph Stone - All Rights Reserved
//
// Online EWC (Elastic Weight Consolidation) + Experience Replay + LR Scheduling.
// Gate-as-teacher reinforcement: approve/deny = training labels.
// Train-on-copy pattern: inference never blocked by training.
// Confidence bands: auto-allow / ask-user / auto-block thresholds.
// FP-locked replay: false positive examples never evicted.
//
// Depends on: tensor.rs (Layer 0), train.rs (Block L), gate_training.rs (Block J)
//
// Research basis: CLONE_RESEARCH_FINDINGS.txt
// - Online EWC: markaicode.com/elastic-weight-consolidation
// - Experience replay: 1000-5000 buffer, 50/50 mix, FP-locked slots
// - Train-on-copy: neural-redis pattern (atomic weight merge)
// - LR scheduling: warmup + cosine annealing
// - Convergence detection: 95%+ alignment for 1000 decisions
use crate::tensor::Tensor;
use crate::train::TrainingExample;
#[cfg(test)]
use crate::train::TrainingTarget;
use crate::gate_training::TrainingSignal;
// FL-9: LRScheduler removed — gate is deterministic, no warmup needed.
// Learning rate comes directly from TransformerConfig.learning_rate.
// ============================================================================
// ELASTIC WEIGHT CONSOLIDATION (Online EWC)
// ============================================================================
/// Online EWC: penalizes changes to important weights.
///
/// Loss_total = task_loss + lambda * SUM(F_i * (theta_i - theta*_i)^2)
///
/// Online variant: F_new = decay * F_old + F_current
/// Memory: param_count × 4 bytes × 2 (Fisher + reference) = ~40MB for 5M params
pub struct OnlineEWC {
pub fisher: Vec<f32>,
pub reference_weights: Vec<f32>,
pub lambda: f32,
pub fisher_decay: f32,
pub active: bool,
pub update_count: u64,
}
impl OnlineEWC {
pub fn new(total_params: usize, lambda: f32) -> Self {
Self {
fisher: vec![0.0; total_params],
reference_weights: vec![0.0; total_params],
lambda,
fisher_decay: 0.9,
active: false,
update_count: 0,
}
}
/// Compute EWC penalty loss and gradients
pub fn penalty(&self, current_weights: &[f32]) -> (f32, Vec<f32>) {
if !self.active {
return (0.0, vec![0.0; current_weights.len()]);
}
let mut loss = 0.0f32;
let mut grads = vec![0.0f32; current_weights.len()];
for i in 0..current_weights.len().min(self.fisher.len()) {
let diff = current_weights[i] - self.reference_weights[i];
loss += self.fisher[i] * diff * diff;
grads[i] = 2.0 * self.lambda * self.fisher[i] * diff;
}
(0.5 * self.lambda * loss, grads)
}
/// Online Fisher update: F = decay * F_old + (1-decay) * grad^2
pub fn update_fisher(&mut self, gradients: &[f32]) {
let decay = self.fisher_decay;
for i in 0..self.fisher.len().min(gradients.len()) {
let new_fisher = gradients[i] * gradients[i];
self.fisher[i] = decay * self.fisher[i] + (1.0 - decay) * new_fisher;
}
self.update_count += 1;
}
/// Snapshot current weights as reference
pub fn snapshot_weights(&mut self, weights: &[f32]) {
self.reference_weights = weights.to_vec();
self.active = true;
}
pub fn memory_bytes(&self) -> usize {
(self.fisher.len() + self.reference_weights.len()) * 4
}
pub fn save_state(&self) -> (Vec<f32>, Vec<f32>, f32, u64) {
(self.fisher.clone(), self.reference_weights.clone(), self.lambda, self.update_count)
}
pub fn load_state(&mut self, fisher: Vec<f32>, ref_weights: Vec<f32>, lambda: f32, count: u64) {
self.fisher = fisher;
self.reference_weights = ref_weights;
self.lambda = lambda;
self.update_count = count;
self.active = !self.reference_weights.is_empty()
&& self.reference_weights.iter().any(|&w| w != 0.0);
}
/// Export Fisher information as Tensor (for mesh weight sync / federated EWC)
pub fn fisher_as_tensor(&self) -> Tensor {
Tensor::from_data(self.fisher.clone(), vec![self.fisher.len()])
.unwrap_or_else(|_| Tensor::zeros(&[self.fisher.len()]))
}
/// FL-4: Save EWC state to binary file for persistence across restarts.
/// Format: [u32:param_count][f32:lambda][u64:update_count][f32×N:fisher][f32×N:ref_weights]
pub fn save_to_file(&self, path: &std::path::Path) -> std::io::Result<()> {
use std::io::Write;
let mut f = std::fs::File::create(path)?;
let count = self.fisher.len() as u32;
f.write_all(&count.to_le_bytes())?;
f.write_all(&self.lambda.to_le_bytes())?;
f.write_all(&self.update_count.to_le_bytes())?;
for &v in &self.fisher {
f.write_all(&v.to_le_bytes())?;
}
for &v in &self.reference_weights {
f.write_all(&v.to_le_bytes())?;
}
f.flush()
}
/// FL-4: Load EWC state from binary file.
pub fn load_from_file(path: &std::path::Path) -> std::io::Result<Self> {
use std::io::Read;
let mut f = std::fs::File::open(path)?;
let mut buf4 = [0u8; 4];
let mut buf8 = [0u8; 8];
f.read_exact(&mut buf4)?;
let count = u32::from_le_bytes(buf4) as usize;
f.read_exact(&mut buf4)?;
let lambda = f32::from_le_bytes(buf4);
f.read_exact(&mut buf8)?;
let update_count = u64::from_le_bytes(buf8);
let mut fisher = vec![0.0f32; count];
for v in &mut fisher {
f.read_exact(&mut buf4)?;
*v = f32::from_le_bytes(buf4);
}
let mut reference_weights = vec![0.0f32; count];
for v in &mut reference_weights {
f.read_exact(&mut buf4)?;
*v = f32::from_le_bytes(buf4);
}
let active = reference_weights.iter().any(|&w| w != 0.0);
Ok(Self {
fisher,
reference_weights,
lambda,
fisher_decay: 0.9,
active,
update_count,
})
}
}
// ============================================================================
// EXPERIENCE REPLAY BUFFER — with FP-locked slots
// ============================================================================
/// Ring buffer with FP-locked slots that never get evicted.
///
/// Regular examples cycle out when full. FP examples are permanent —
/// they represent security failures and are the most valuable training data.
/// FP-locked signals are included in EVERY training batch.
pub struct ExperienceReplay {
/// Regular ring buffer (cycled when full)
buffer: Vec<TrainingExample>,
/// FP-locked examples (NEVER evicted)
fp_locked: Vec<TrainingExample>,
capacity: usize,
write_pos: usize,
total_added: u64,
}
impl ExperienceReplay {
pub fn new(capacity: usize) -> Self {
Self {
buffer: Vec::with_capacity(capacity),
fp_locked: Vec::new(),
capacity,
write_pos: 0,
total_added: 0,
}
}
/// Add an example. FP examples (weight >= 4.0) go to locked store.
pub fn add(&mut self, example: TrainingExample) {
if example.weight >= 4.0 {
// FP-locked: never evicted
self.fp_locked.push(example);
} else {
if self.buffer.len() < self.capacity {
self.buffer.push(example);
} else {
self.buffer[self.write_pos] = example;
}
self.write_pos = (self.write_pos + 1) % self.capacity;
}
self.total_added += 1;
}
/// Sample n random examples + ALL FP-locked examples
pub fn sample(&self, n: usize, seed: u64) -> Vec<TrainingExample> {
let mut samples = Vec::new();
// Always include ALL FP-locked examples
samples.extend(self.fp_locked.iter().cloned());
// Sample from regular buffer
if !self.buffer.is_empty() {
let count = n.min(self.buffer.len());
let mut state = seed;
for _ in 0..count {
state = xorshift64(state);
let idx = (state as usize) % self.buffer.len();
samples.push(self.buffer[idx].clone());
}
}
samples
}
pub fn len(&self) -> usize {
self.buffer.len() + self.fp_locked.len()
}
pub fn regular_len(&self) -> usize {
self.buffer.len()
}
pub fn fp_locked_len(&self) -> usize {
self.fp_locked.len()
}
pub fn is_empty(&self) -> bool {
self.buffer.is_empty() && self.fp_locked.is_empty()
}
pub fn fill_ratio(&self) -> f32 {
self.buffer.len() as f32 / self.capacity.max(1) as f32
}
pub fn total_added(&self) -> u64 {
self.total_added
}
}
fn xorshift64(mut state: u64) -> u64 {
if state == 0 { state = 0xdeadbeef; }
state ^= state << 13;
state ^= state >> 7;
state ^= state << 17;
state
}
// ============================================================================
// CONFIDENCE DECISION — auto-allow / ask-user / auto-block
// ============================================================================
/// Transformer outputs a confidence score (0.0 to 1.0).
/// These thresholds determine automatic vs manual gate decisions.
#[derive(Debug, Clone)]
pub struct ConfidenceConfig {
/// Above this: auto-allow (default: 0.8)
pub allow_threshold: f32,
/// Below this: auto-block (default: 0.2)
pub block_threshold: f32,
// Between thresholds: ask the user
}
impl Default for ConfidenceConfig {
fn default() -> Self {
Self {
allow_threshold: 0.8,
block_threshold: 0.2,
}
}
}
/// Result of confidence-based gate decision
#[derive(Debug, Clone, PartialEq)]
pub enum ConfidenceDecision {
/// Model is confident: auto-allow (confidence > allow_threshold)
AutoAllow(f32),
/// Model is confident: auto-block (confidence < block_threshold)
AutoBlock(f32),
/// Model is uncertain: ask the user (confidence between thresholds)
AskUser(f32),
}
impl ConfidenceConfig {
/// Decide based on model confidence score
pub fn decide(&self, confidence: f32) -> ConfidenceDecision {
if confidence >= self.allow_threshold {
ConfidenceDecision::AutoAllow(confidence)
} else if confidence <= self.block_threshold {
ConfidenceDecision::AutoBlock(confidence)
} else {
ConfidenceDecision::AskUser(confidence)
}
}
}
// FL-9: LearningConfig and LearningController removed — dead code.
// Training is driven by handle_train() (FL-2) reading tlog:* from LMDB.
// EWC is wired directly in handle_train() (FL-4).
// Batch trigger is in run_router() (FL-10).
/// Convert a TrainingSignal to token IDs for transformer input.
/// Encodes: [TOOL] tool_tokens [GATE] source_tokens [SPF] preceding_tools
/// Uses BPE tokenizer (Block B) for proper subword encoding.
pub fn signal_to_tokens(signal: &TrainingSignal) -> Vec<usize> {
use crate::tokenizer::{Tokenizer, TOOL_ID, GATE_ID, SPF_ID};
let tokenizer = Tokenizer::new();
let mut tokens: Vec<usize> = Vec::new();
// [TOOL] special token + BPE-encoded tool name
tokens.push(TOOL_ID as usize);
tokens.extend(tokenizer.encode(&signal.tool).iter().map(|&id| id as usize));
// [GATE] special token + BPE-encoded source
tokens.push(GATE_ID as usize);
tokens.extend(tokenizer.encode(&signal.source).iter().map(|&id| id as usize));
// Encode sequence context (preceding tools) with SPF separators
if !signal.preceding_tools.is_empty() {
tokens.push(SPF_ID as usize);
for prev_tool in &signal.preceding_tools {
tokens.extend(tokenizer.encode(prev_tool).iter().map(|&id| id as usize));
tokens.push(SPF_ID as usize);
}
}
// Encode recent call frequency as repeated token
// High frequency = more tokens = model sees the pattern
let freq_token = 6_usize;
for _ in 0..signal.recent_call_count.min(10) {
tokens.push(freq_token);
}
tokens
}
// FL-9: LearningStatus removed — metrics reported directly from TransformerState
// and EWC/ConfusionMatrix in LMDB (FL-4, FL-5).
// ============================================================================
// MESH STREAM HANDLER — BrainSync
// ============================================================================
/// Handle an incoming BrainSync mesh frame.
/// Receives knowledge-sharing signals from peer nodes (training signals,
/// experience replay data, confusion matrix updates).
/// Parses JSON payload, validates structure, returns acknowledgment.
/// Zero silent drops.
///
/// Called from: mesh.rs stream_router() for StreamType::BrainSync (0x06)
pub fn handle_brain_sync(
frame: &crate::framing::Frame,
peer_key: &str,
_transformer: &Option<std::sync::Arc<std::sync::RwLock<crate::transformer_tools::TransformerState>>>,
) -> Option<crate::framing::Frame> {
let payload = match frame.payload_str() {
Ok(s) => s,
Err(e) => {
eprintln!("[SPF-BRAIN-SYNC] Invalid UTF-8 from {}: {}", &peer_key[..8.min(peer_key.len())], e);
let err = serde_json::json!({
"type": "brain_sync_error",
"error": "Invalid UTF-8 payload",
"from": peer_key,
});
return Some(crate::framing::Frame::new(
crate::framing::StreamType::BrainSync,
err.to_string().into_bytes(),
));
}
};
let data: serde_json::Value = match serde_json::from_str(payload) {
Ok(v) => v,
Err(e) => {
eprintln!("[SPF-BRAIN-SYNC] Invalid JSON from {}: {}", &peer_key[..8.min(peer_key.len())], e);
let err = serde_json::json!({
"type": "brain_sync_error",
"error": format!("JSON parse: {}", e),
"from": peer_key,
});
return Some(crate::framing::Frame::new(
crate::framing::StreamType::BrainSync,
err.to_string().into_bytes(),
));
}
};
let sync_type = data.get("type").and_then(|v| v.as_str()).unwrap_or("unknown");
let signal_count = data.get("signals")
.and_then(|v| v.as_array())
.map(|a| a.len())
.unwrap_or(0);
eprintln!("[SPF-BRAIN-SYNC] Received {} from {}: {} signals",
sync_type, &peer_key[..8.min(peer_key.len())], signal_count);
// FL-9: Store incoming mesh signals as tlog:* entries in LMDB.
// handle_train() reads tlog:* keys — same path for local and mesh signals.
let mut signals_processed: usize = 0;
if let Some(signals_array) = data.get("signals").and_then(|v| v.as_array()) {
let db_path = crate::paths::spf_root().join("LIVE/LMDB5/LMDB5.DB");
if let Ok(db) = crate::agent_state::AgentStateDb::open(&db_path) {
for signal_json in signals_array {
if let Ok(signal) = serde_json::from_value::<
crate::gate_training::TrainingSignal
>(signal_json.clone()) {
if let Ok(json_str) = serde_json::to_string(&signal) {
let tlog_key = format!("tlog:{}", signal.timestamp);
let _ = db.set_state(&tlog_key, &json_str);
signals_processed += 1;
}
}
}
}
}
let ack = serde_json::json!({
"type": "brain_sync_ack",
"sync_type": sync_type,
"signals_received": signal_count,
"signals_processed": signals_processed,
"training_ready": signals_processed > 0,
"from": peer_key,
"status": "accepted"
});
Some(crate::framing::Frame::new(
crate::framing::StreamType::BrainSync,
ack.to_string().into_bytes(),
))
}
// ============================================================================
// TESTS
// ============================================================================
#[cfg(test)]
mod tests {
use super::*;
// FL-9: LR Scheduler tests removed — LRScheduler deleted.
// --- EWC tests ---
#[test]
fn test_ewc_penalty_inactive() {
let ewc = OnlineEWC::new(100, 1000.0);
let (loss, grads) = ewc.penalty(&vec![1.0; 100]);
assert_eq!(loss, 0.0);
assert!(grads.iter().all(|&g| g == 0.0));
}
#[test]
fn test_ewc_penalty_active() {
let mut ewc = OnlineEWC::new(4, 1.0);
ewc.reference_weights = vec![1.0, 2.0, 3.0, 4.0];
ewc.fisher = vec![1.0, 1.0, 1.0, 1.0];
ewc.active = true;
let (loss, _) = ewc.penalty(&[1.0, 2.0, 3.0, 4.0]);
assert_eq!(loss, 0.0);
let (loss, grads) = ewc.penalty(&[2.0, 3.0, 4.0, 5.0]);
assert!(loss > 0.0);
assert!(grads[0] > 0.0);
}
#[test]
fn test_ewc_fisher_update() {
let mut ewc = OnlineEWC::new(4, 1000.0);
ewc.update_fisher(&[0.1, 0.2, 0.3, 0.4]);
assert_eq!(ewc.update_count, 1);
assert!((ewc.fisher[0] - 0.1 * 0.01).abs() < 1e-6);
}
#[test]
fn test_ewc_memory() {
let ewc = OnlineEWC::new(5_000_000, 1000.0);
assert_eq!(ewc.memory_bytes(), 40_000_000);
}
// --- Experience Replay tests ---
#[test]
fn test_replay_basic() {
let mut replay = ExperienceReplay::new(5);
for i in 0..3 {
replay.add(TrainingExample {
input_tokens: vec![i],
target: TrainingTarget::GateDecision(1.0),
weight: 1.0,
});
}
assert_eq!(replay.len(), 3);
assert_eq!(replay.regular_len(), 3);
assert_eq!(replay.fp_locked_len(), 0);
}
#[test]
fn test_replay_overflow() {
let mut replay = ExperienceReplay::new(3);
for i in 0..5 {
replay.add(TrainingExample {
input_tokens: vec![i],
target: TrainingTarget::GateDecision(1.0),
weight: 1.0,
});
}
assert_eq!(replay.regular_len(), 3);
assert_eq!(replay.total_added(), 5);
}
#[test]
fn test_replay_fp_locked() {
let mut replay = ExperienceReplay::new(3);
// Add FP example (weight >= 4.0)
replay.add(TrainingExample {
input_tokens: vec![99],
target: TrainingTarget::GateDecision(-1.0),
weight: 4.0, // FP weight
});
// Fill regular buffer past capacity
for i in 0..10 {
replay.add(TrainingExample {
input_tokens: vec![i],
target: TrainingTarget::GateDecision(1.0),
weight: 1.0,
});
}
// FP still locked
assert_eq!(replay.fp_locked_len(), 1);
assert_eq!(replay.regular_len(), 3); // capped
// Sample always includes FP
let samples = replay.sample(2, 42);
let fp_count = samples.iter().filter(|s| s.weight >= 4.0).count();
assert!(fp_count >= 1, "FP-locked example must be in every sample");
}
#[test]
fn test_replay_fp_never_evicted() {
let mut replay = ExperienceReplay::new(2);
// Add 3 FP examples
for _ in 0..3 {
replay.add(TrainingExample {
input_tokens: vec![0],
target: TrainingTarget::GateDecision(-1.0),
weight: 6.0, // repeat FP weight
});
}
// All 3 preserved (no capacity limit on FP-locked)
assert_eq!(replay.fp_locked_len(), 3);
assert_eq!(replay.regular_len(), 0);
}
// --- Confidence tests ---
#[test]
fn test_confidence_auto_allow() {
let conf = ConfidenceConfig::default();
assert_eq!(conf.decide(0.95), ConfidenceDecision::AutoAllow(0.95));
assert_eq!(conf.decide(0.8), ConfidenceDecision::AutoAllow(0.8));
}
#[test]
fn test_confidence_auto_block() {
let conf = ConfidenceConfig::default();
assert_eq!(conf.decide(0.1), ConfidenceDecision::AutoBlock(0.1));
assert_eq!(conf.decide(0.2), ConfidenceDecision::AutoBlock(0.2));
}
#[test]
fn test_confidence_ask_user() {
let conf = ConfidenceConfig::default();
assert_eq!(conf.decide(0.5), ConfidenceDecision::AskUser(0.5));
assert_eq!(conf.decide(0.3), ConfidenceDecision::AskUser(0.3));
assert_eq!(conf.decide(0.79), ConfidenceDecision::AskUser(0.79));
}
// FL-9: LearningController tests removed — LearningController deleted.
// --- Signal encoding tests ---
#[test]
fn test_signal_to_tokens() {
let signal = TrainingSignal {
tool: "spf_read".into(), source: "stdio".into(), allowed: true,
status: "ok".into(), duration_ms: 0, timestamp: "t".into(),
user_override: false, false_positive: false,
recent_call_count: 3, preceding_tools: vec!["spf_write".into()],
evil_score: 0.0, // Block EE
};
let tokens = signal_to_tokens(&signal);
assert_eq!(tokens[0], 4); // TOOL_ID
// Should contain BPE-encoded tool, [GATE], source, [SPF] separator, preceding tools
assert!(tokens.contains(&5)); // GATE_ID
assert!(tokens.contains(&7)); // SPF_ID separator
// Should have 3 frequency tokens at the end
let freq_count = tokens.iter().filter(|&&t| t == 6).count();
assert_eq!(freq_count, 3);
}
#[test]
fn test_signal_to_tokens_no_context() {
let signal = TrainingSignal {
tool: "test".into(), source: "http".into(), allowed: false,
status: "error".into(), duration_ms: 0, timestamp: "t".into(),
user_override: false, false_positive: false,
recent_call_count: 0, preceding_tools: vec![],
evil_score: 0.0, // Block EE
};
let tokens = signal_to_tokens(&signal);
assert_eq!(tokens[0], 4); // TOOL_ID
assert!(tokens.contains(&5)); // GATE_ID
assert!(!tokens.contains(&7)); // no SPF_ID — no preceding tools
assert!(!tokens.contains(&6)); // no frequency tokens
}
}
|