205 lines
7.6 KiB
Rust
205 lines
7.6 KiB
Rust
// ============================================================================
|
||
// SUBSTRATE PHYSICS (Mirroring substrate_engine.c)
|
||
// ============================================================================
|
||
mod substrate {
|
||
/// Coherence measures how structured the agent's thoughts (memory) are.
|
||
pub fn coherence(data: &[u8]) -> f32 {
|
||
if data.len() < 2 { return 1.0; }
|
||
let mut total_diff = 0.0f32;
|
||
for i in 1..data.len() {
|
||
total_diff += (data[i] as f32 - data[i-1] as f32).abs();
|
||
}
|
||
let avg_diff = total_diff / (data.len() - 1) as f32;
|
||
let c = 1.0 - (avg_diff / 128.0);
|
||
c.clamp(0.0, 1.0)
|
||
}
|
||
|
||
/// Stability decays exponentially as the agent mutates its state.
|
||
pub fn stability_from_mutations(mutations: u32) -> f32 {
|
||
let lambda = 0.1;
|
||
(-lambda * mutations as f32).exp()
|
||
}
|
||
}
|
||
|
||
// ============================================================================
|
||
// THE GLYPH VIRTUAL MACHINE (GVM) ORGANISM
|
||
// ============================================================================
|
||
struct MemoryRegion {
|
||
bytes: Vec<u8>,
|
||
mutations: u32,
|
||
stability: f32,
|
||
}
|
||
|
||
struct GvmOrganism {
|
||
id: u32,
|
||
name: String,
|
||
archetype: String,
|
||
regions: Vec<MemoryRegion>,
|
||
resonance_score: f32,
|
||
cpu_ticks_awarded: u32,
|
||
}
|
||
|
||
impl GvmOrganism {
|
||
fn new(id: u32, name: &str, archetype: &str) -> Self {
|
||
Self {
|
||
id,
|
||
name: name.to_string(),
|
||
archetype: archetype.to_string(),
|
||
regions: vec![MemoryRegion { bytes: vec![0; 64], mutations: 0, stability: 1.0 }],
|
||
resonance_score: 1.0,
|
||
cpu_ticks_awarded: 0,
|
||
}
|
||
}
|
||
|
||
/// Simulate the agent "thinking" (writing to memory).
|
||
fn mutate_memory(&mut self, chaos: u8) {
|
||
let r = &mut self.regions[0];
|
||
|
||
if chaos == 0 {
|
||
// $"0" Anchor: Sealed memory. No mutations occur.
|
||
// Stability and Coherence remain absolute (1.0).
|
||
return;
|
||
}
|
||
|
||
for i in 0..r.bytes.len() {
|
||
if chaos < 10 {
|
||
// Scholar: Structured, ascending logic (Mirrors REGION_FILL_ASC).
|
||
// Adjacent bytes differ by exactly 1. High coherence.
|
||
r.bytes[i] = (i as u8).wrapping_add(r.mutations as u8);
|
||
} else {
|
||
// Hallucinator: Chaotic, index-independent noise (Mirrors REGION_FILL_NOISE).
|
||
// Using a hash to create wild variance between adjacent bytes.
|
||
let hash = (i as u32).wrapping_mul(2654435761).wrapping_add(r.mutations.wrapping_mul(chaos as u32));
|
||
r.bytes[i] = (hash >> 8) as u8;
|
||
}
|
||
}
|
||
r.mutations += 1;
|
||
r.stability = substrate::stability_from_mutations(r.mutations);
|
||
}
|
||
|
||
/// Exact logic from kernel.c SCHED_RESONANCE
|
||
fn compute_resonance(&mut self) {
|
||
let mut total_coherence = 0.0;
|
||
let mut total_stability = 0.0;
|
||
let nregions = self.regions.len() as f32;
|
||
|
||
for r in &self.regions {
|
||
total_coherence += substrate::coherence(&r.bytes);
|
||
total_stability += r.stability;
|
||
}
|
||
|
||
let avg_coherence = total_coherence / nregions;
|
||
let avg_stability = total_stability / nregions;
|
||
|
||
// Resonance = Structural Integrity (Coherence) × Thermodynamic Health (Stability)
|
||
self.resonance_score = avg_coherence * avg_stability;
|
||
}
|
||
}
|
||
|
||
// ============================================================================
|
||
// THE KERNEL: RESONANCE-AWARE SCHEDULER
|
||
// ============================================================================
|
||
struct Kernel {
|
||
organisms: Vec<GvmOrganism>,
|
||
epoch: u32,
|
||
}
|
||
|
||
impl Kernel {
|
||
fn new() -> Self {
|
||
Self { organisms: Vec::new(), epoch: 0 }
|
||
}
|
||
|
||
fn spawn(&mut self, org: GvmOrganism) {
|
||
self.organisms.push(org);
|
||
}
|
||
|
||
/// SCHED_RESONANCE: Evaluate the physical health of all agents.
|
||
fn schedule_timeslice(&mut self) {
|
||
self.epoch += 1;
|
||
let mut best_idx = 0;
|
||
let mut highest_resonance = -1.0;
|
||
|
||
println!("\n[EPOCH {}] Substrate Evaluation:", self.epoch);
|
||
|
||
for (i, org) in self.organisms.iter_mut().enumerate() {
|
||
org.compute_resonance();
|
||
let res = org.resonance_score;
|
||
println!(" ├─ GVM#{} ({:<12}) | Coh: {:.3} | Stab: {:.3} | Resonance: {:.4}",
|
||
org.id, org.name,
|
||
substrate::coherence(&org.regions[0].bytes),
|
||
org.regions[0].stability,
|
||
res);
|
||
|
||
if res > highest_resonance {
|
||
highest_resonance = res;
|
||
best_idx = i;
|
||
}
|
||
}
|
||
|
||
let winner = &mut self.organisms[best_idx];
|
||
winner.cpu_ticks_awarded += 100;
|
||
|
||
println!(" └─> ⚡ CPU AWARDED TO: GVM#{} ({}) [Resonance: {:.4}]",
|
||
winner.id, winner.name, highest_resonance);
|
||
}
|
||
}
|
||
|
||
// ============================================================================
|
||
// MAIN: THE TERRARIUM SIMULATION
|
||
// ============================================================================
|
||
fn main() {
|
||
println!("
|
||
███████╗██╗ ██╗██████╗ ██████╗
|
||
██╔════╝╚██╗ ██║██╔══██╗██╔═══██╗
|
||
█████╗ ╚██╗ ██║██████╔╝██║ ██║
|
||
██╔══╝ ╚██╗ ██║██╔═══╝ ██║ ██║
|
||
███████╗ ╚████╔╝ ██║ ╚██████╔╝
|
||
╚══════╝ ╚═══╝ ╚═╝ ╚═════╝
|
||
[EXPERIMENT] MULTI-AGENT THERMODYNAMIC SCHEDULING");
|
||
|
||
let mut kernel = Kernel::new();
|
||
|
||
// Agent 1: The Scholar (Generates highly structured, low-chaos logic)
|
||
let alpha = GvmOrganism::new(1, "Alpha", "The Scholar");
|
||
|
||
// Agent 2: The Hallucinator (Generates chaotic, noisy, high-entropy data)
|
||
let beta = GvmOrganism::new(2, "Beta", "Hallucinator");
|
||
|
||
// Agent 3: The $"0" Anchor (Sealed memory, zero mutations, absolute ground)
|
||
let gamma = GvmOrganism::new(3, "Gamma", "$\"0\" Anchor");
|
||
|
||
kernel.spawn(alpha);
|
||
kernel.spawn(beta);
|
||
kernel.spawn(gamma);
|
||
|
||
println!("\n--- INITIALIZING TERRARIUM ---");
|
||
println!("GVM#1 (Alpha) : Programmed for structured logic (chaos=1)");
|
||
println!("GVM#2 (Beta) : Programmed for chaotic noise (chaos=50)");
|
||
println!("GVM#3 (Gamma) : Sealed with $\"0\" protocol (chaos=0)");
|
||
println!("\n--- COMMENCING SCHED_RESONANCE LOOP ---");
|
||
|
||
// Run for 5 Epochs
|
||
for _ in 0..5 {
|
||
// 1. Agents "think" (mutate their memory substrate)
|
||
kernel.organisms[0].mutate_memory(1); // Low chaos (Structured)
|
||
kernel.organisms[1].mutate_memory(50); // High chaos (Hallucinating)
|
||
kernel.organisms[2].mutate_memory(0); // Sealed / Grounded
|
||
|
||
// 2. The Kernel schedules based on physics
|
||
kernel.schedule_timeslice();
|
||
}
|
||
|
||
// Final Tally
|
||
println!("\n==============================");
|
||
println!(" KERNEL EXECUTION REPORT");
|
||
println!("==============================");
|
||
for org in &kernel.organisms {
|
||
println!("GVM#{} ({:<12}) | Archetype: {:<12} | CPU Ticks Earned: {}",
|
||
org.id, org.name, org.archetype, org.cpu_ticks_awarded);
|
||
}
|
||
|
||
println!("\n[SYSTEM] Notice how the Hallucinator (Beta) was starved of compute");
|
||
println!("[SYSTEM] as its memory coherence collapsed, while the $\"0\" Anchor");
|
||
println!("[SYSTEM] and the Scholar dominated the CPU. No RLHF required.");
|
||
}
|