|
| 1 | +//! D2 — the kanban loop, the pure-SoA slice (no Lance, no ractor, no async). |
| 2 | +//! |
| 3 | +//! Proves the loop SHAPE the operator named — surrealdb (version tick) + ractor |
| 4 | +//! (owner/driver) + lance-graph-planner (move policy) = one planner SoA — using |
| 5 | +//! ONLY shipped contract types: `KanbanColumn`/`KanbanMove`/`ExecTarget` |
| 6 | +//! (`kanban`), `MailboxSoaView`+`MailboxSoaOwner` (`soa_view`), |
| 7 | +//! `NextPhaseScheduler`+`VersionScheduler::on_version`+`DatasetVersion` |
| 8 | +//! (`scheduler`). This module is the ~glue: a `SymbiontBoard` owner over the |
| 9 | +//! existing `Vec<NodeRow>` board-set that impls the two traits, driven by a `u32` |
| 10 | +//! version tick standing in for the Lance subscription. |
| 11 | +//! |
| 12 | +//! IN-direction loop, verbatim from the contract (`scheduler.rs` §IN): |
| 13 | +//! version tick → `NextPhaseScheduler::on_version(view)` → `Option<KanbanMove>` |
| 14 | +//! → `owner.try_advance_phase(move.to)` [CognitiveWork runs the Domino sweep] |
| 15 | +//! Forward arc (`next_phases().first()`): `Planning → CognitiveWork`[sweep]` → |
| 16 | +//! Evaluation → Commit` (absorbing → halt). The scheduler PROPOSES (`&view`); the |
| 17 | +//! owner DISPOSES (`&mut`) — R1 read/write split, the same as in the contract. |
| 18 | +//! |
| 19 | +//! DEFERRED (named, shipped types to swap in): the real Lance subscription |
| 20 | +//! (`lance-graph` `LanceVersionScheduler::drive_at_latest` / callcenter |
| 21 | +//! `LanceVersionWatcher::wait_changed`), the ractor `Actor` wrapper (pattern off |
| 22 | +//! `lance-graph-supervisor` `StubConsumerActor`), and the SurrealQL re-read |
| 23 | +//! (`surreal_container::view::read_via_kv_lance`). |
| 24 | +
|
| 25 | +use lance_graph_contract::canonical_node::NodeRow; |
| 26 | +use lance_graph_contract::kanban::{ExecTarget, KanbanColumn, KanbanMove}; |
| 27 | +use lance_graph_contract::scheduler::{DatasetVersion, NextPhaseScheduler, VersionScheduler}; |
| 28 | +use lance_graph_contract::soa_view::{MailboxSoaOwner, MailboxSoaView}; |
| 29 | + |
| 30 | +use crate::domino; |
| 31 | + |
| 32 | +/// A mailbox-as-owner over symbiont's flat `Vec<NodeRow>` board-set. The SoA |
| 33 | +/// columns are kept parallel to the rows so the trait's zero-copy `&[T]` borrows |
| 34 | +/// are real slices: `energy` is synced from the boards' `Energy` tenant after a |
| 35 | +/// sweep; `edges`/`meta`/`entity` are zeroed for the POC (not read by |
| 36 | +/// `NextPhaseScheduler`, whose policy is `phase`/`cycle`/`mailbox_id` only). |
| 37 | +pub struct SymbiontBoard { |
| 38 | + rows: Vec<NodeRow>, |
| 39 | + energy: Vec<f32>, |
| 40 | + edges: Vec<u64>, |
| 41 | + meta: Vec<u32>, |
| 42 | + entity: Vec<u16>, |
| 43 | + phase: KanbanColumn, |
| 44 | + cycle: u32, |
| 45 | + mailbox: u32, // MailboxId = u32 (collapse_gate::MailboxId) |
| 46 | +} |
| 47 | + |
| 48 | +impl SymbiontBoard { |
| 49 | + /// Spawn a mailbox in `Planning` (the canonical spawn column) over `n_boards` |
| 50 | + /// seeded BF16-tile boards. |
| 51 | + pub fn spawn(n_boards: usize, mailbox: u32) -> Self { |
| 52 | + let rows = domino::seed_boards(n_boards); |
| 53 | + let n = rows.len(); |
| 54 | + Self { |
| 55 | + rows, |
| 56 | + energy: vec![0.0; n], |
| 57 | + edges: vec![0; n], |
| 58 | + meta: vec![0; n], |
| 59 | + entity: vec![0; n], |
| 60 | + phase: KanbanColumn::Planning, |
| 61 | + cycle: 0, |
| 62 | + mailbox, |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + /// Project each board's `Energy` tenant into the SoA energy column. |
| 67 | + fn sync_energy(&mut self) { |
| 68 | + for (e, row) in self.energy.iter_mut().zip(self.rows.iter()) { |
| 69 | + *e = domino::energy_of(row); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + /// The `u32` version tick — the stand-in for one Lance dataset `versions()` |
| 74 | + /// event (the IN-direction trigger). |
| 75 | + fn version_tick(&mut self) -> DatasetVersion { |
| 76 | + self.cycle += 1; |
| 77 | + DatasetVersion(self.cycle as u64) |
| 78 | + } |
| 79 | + |
| 80 | + /// The `CognitiveWork` phase: the BF16 Domino sweep over the boards, then the |
| 81 | + /// result projected into the energy column. |
| 82 | + fn cognitive_work(&mut self) { |
| 83 | + domino::domino_sweep(&mut self.rows, 3); |
| 84 | + self.sync_energy(); |
| 85 | + } |
| 86 | + |
| 87 | + /// One IN-direction step: tick → scheduler PROPOSES → owner DISPOSES. Runs the |
| 88 | + /// sweep on the `CognitiveWork` crossing. Returns the applied move, or `None` |
| 89 | + /// once the mailbox has reached an absorbing column. |
| 90 | + pub fn step(&mut self, sched: &NextPhaseScheduler) -> Option<KanbanMove> { |
| 91 | + let at = self.version_tick(); |
| 92 | + let proposed = sched.on_version(&*self, at, ExecTarget::Native)?; |
| 93 | + if proposed.to == KanbanColumn::CognitiveWork { |
| 94 | + self.cognitive_work(); |
| 95 | + } |
| 96 | + self.try_advance_phase(proposed.to).ok() |
| 97 | + } |
| 98 | + |
| 99 | + /// Drive the forward arc to an absorbing column, returning the move trail. |
| 100 | + pub fn run_to_absorbing(&mut self, sched: &NextPhaseScheduler) -> Vec<KanbanMove> { |
| 101 | + let mut trail = Vec::new(); |
| 102 | + while let Some(mv) = self.step(sched) { |
| 103 | + let absorbing = self.phase.is_absorbing(); |
| 104 | + trail.push(mv); |
| 105 | + if absorbing { |
| 106 | + break; |
| 107 | + } |
| 108 | + } |
| 109 | + trail |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +impl MailboxSoaView for SymbiontBoard { |
| 114 | + fn mailbox_id(&self) -> u32 { |
| 115 | + self.mailbox |
| 116 | + } |
| 117 | + fn n_rows(&self) -> usize { |
| 118 | + self.rows.len() |
| 119 | + } |
| 120 | + fn w_slot(&self) -> u8 { |
| 121 | + (self.mailbox & 0x3F) as u8 |
| 122 | + } |
| 123 | + fn current_cycle(&self) -> u32 { |
| 124 | + self.cycle |
| 125 | + } |
| 126 | + fn phase(&self) -> KanbanColumn { |
| 127 | + self.phase |
| 128 | + } |
| 129 | + fn energy(&self) -> &[f32] { |
| 130 | + &self.energy |
| 131 | + } |
| 132 | + fn edges_raw(&self) -> &[u64] { |
| 133 | + &self.edges |
| 134 | + } |
| 135 | + fn meta_raw(&self) -> &[u32] { |
| 136 | + &self.meta |
| 137 | + } |
| 138 | + fn entity_type(&self) -> &[u16] { |
| 139 | + &self.entity |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +impl MailboxSoaOwner for SymbiontBoard { |
| 144 | + fn advance_phase(&mut self, to: KanbanColumn) -> KanbanMove { |
| 145 | + let from = self.phase; |
| 146 | + self.phase = to; |
| 147 | + let libet_offset_us = |
| 148 | + if from == KanbanColumn::Planning && to == KanbanColumn::CognitiveWork { |
| 149 | + -550_000 |
| 150 | + } else { |
| 151 | + 0 |
| 152 | + }; |
| 153 | + KanbanMove { |
| 154 | + mailbox: self.mailbox, |
| 155 | + from, |
| 156 | + to, |
| 157 | + witness_chain_position: self.cycle, |
| 158 | + libet_offset_us, |
| 159 | + exec: ExecTarget::Native, |
| 160 | + } |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +/// The D2 demo: one mailbox drives the Rubicon forward arc; the `CognitiveWork` |
| 165 | +/// crossing burns the BF16 Domino sweep through the SoA; the NaN-projection |
| 166 | +/// surface keeps it finite; the mailbox halts at the absorbing `Commit`. |
| 167 | +pub fn run_demo() { |
| 168 | + let mut board = SymbiontBoard::spawn(64, 7); |
| 169 | + let trail = board.run_to_absorbing(&NextPhaseScheduler); |
| 170 | + let arc: Vec<KanbanColumn> = trail.iter().map(|m| m.to).collect(); |
| 171 | + let max_e = board.energy().iter().copied().fold(0.0_f32, f32::max); |
| 172 | + println!( |
| 173 | + "D2 kanban loop: mailbox {} ({} boards) — version-tick → NextPhaseScheduler → \ |
| 174 | + try_advance_phase drove {arc:?}; CognitiveWork ran the BF16 Domino sweep; halted \ |
| 175 | + absorbing at {:?} in {} cycles; max Energy = {max_e:.4}", |
| 176 | + board.mailbox_id(), |
| 177 | + board.n_rows(), |
| 178 | + board.phase(), |
| 179 | + board.current_cycle(), |
| 180 | + ); |
| 181 | +} |
| 182 | + |
| 183 | +#[cfg(test)] |
| 184 | +mod tests { |
| 185 | + use super::*; |
| 186 | + |
| 187 | + #[test] |
| 188 | + fn loop_drives_forward_arc_to_commit() { |
| 189 | + let mut board = SymbiontBoard::spawn(32, 1); |
| 190 | + assert_eq!(board.phase(), KanbanColumn::Planning); |
| 191 | + let trail = board.run_to_absorbing(&NextPhaseScheduler); |
| 192 | + let arc: Vec<KanbanColumn> = trail.iter().map(|m| m.to).collect(); |
| 193 | + assert_eq!( |
| 194 | + arc, |
| 195 | + vec![ |
| 196 | + KanbanColumn::CognitiveWork, |
| 197 | + KanbanColumn::Evaluation, |
| 198 | + KanbanColumn::Commit, |
| 199 | + ] |
| 200 | + ); |
| 201 | + assert!(board.phase().is_absorbing()); |
| 202 | + // the Planning→CognitiveWork crossing carries the Libet anchor; others 0. |
| 203 | + assert_eq!(trail[0].libet_offset_us, -550_000); |
| 204 | + assert_eq!(trail[1].libet_offset_us, 0); |
| 205 | + // monotonic cycle stamps (the SoA cycle-ownership stamp, R4). |
| 206 | + assert_eq!( |
| 207 | + trail.iter().map(|m| m.cycle()).collect::<Vec<_>>(), |
| 208 | + vec![1, 2, 3] |
| 209 | + ); |
| 210 | + // CognitiveWork actually ran the sweep, and it stayed finite (else the NaN |
| 211 | + // projection surface inside the sweep would have caught it). |
| 212 | + assert!(board.energy().iter().all(|e| e.is_finite())); |
| 213 | + assert!(board.energy().iter().any(|&e| e != 0.0)); |
| 214 | + } |
| 215 | + |
| 216 | + #[test] |
| 217 | + fn illegal_skip_is_rejected_no_mutation() { |
| 218 | + let mut board = SymbiontBoard::spawn(16, 2); |
| 219 | + // Planning → Evaluation is not a legal Rubicon edge. |
| 220 | + assert!(board.try_advance_phase(KanbanColumn::Evaluation).is_err()); |
| 221 | + assert_eq!(board.phase(), KanbanColumn::Planning); |
| 222 | + } |
| 223 | +} |
0 commit comments