feat(os): OS operator kernels in CUBE + thin native effector (Steps 1 & 2)

- cubecode/src/cb.rs: Behavior descriptor bitflag (PURE/IO_HEAVY/HOT_PATH)
  encoded into HeaderFlags bits 13..15; C_OS_KERNEL=210 / C_OS_EFFECT=211
  coordinate bands; round-trip unit test.
- fix: HEADER_FLAG_BEHAVIOR mask was 0x7000 (bits 12-14) which grabbed the
  ENCRYPTED bit (12) and dropped HOT_PATH (bit 15). Corrected to 0xE000.
- store_code_cell/put_code_cell/header_for_code gain optional descriptor arg;
  all 12 prior call sites pass None (total change).
- CLI: prog K=<flag> tokens, kernel verb (links+descriptors), tick verb lays
  down the OS kernel call-graph (cfg->decide->summarize->tick), native-apply
  verb = thin effector (runs kernel in VM, reads computed result, emits effect).
- integration test os_kernels_live_in_cube_with_call_graph_and_descriptors.

Proven green via ./check quick (fmt+clippy -D warnings+tests).
This commit is contained in:
CUBELinux-2
2026-08-13 15:57:10 -04:00
parent f6bd8bd01f
commit 1539ecd1bb
8 changed files with 649 additions and 45 deletions
+126
View File
@@ -0,0 +1,126 @@
//! CUBELinux-2 cubebook / kernel-convention constants.
//!
//! These are the conventions the *OS* layer uses to store its computational
//! behavior in CUBE. Per the PDF (Package 4, and the "behavior descriptors"
//! discussion at PDF §524–525), functions/operators that live in CUBE are:
//! * addressed in a reserved `C` axis band (here `c210..=c219`),
//! * tagged with a `kind` (`fn`/`kernel`/`layer`/`checkpoint`/`variant`), and
//! * annotated with *behavior descriptor* flags (pure / I/O heavy /
//! allocates / touches-net / hot-path) carried in the record header's
//! out-of-band flag bits (tag 12, see cubestore encode/decode).
//!
//! This is deliberately NOT a store-IO model: the cubevm never reads or writes
//! OS state records directly. The OS's *decisions/computation* live as CUBE
//! kernels; the native OS layer is a thin effector that reads a kernel's
//! computed result and applies the effect (writes the record, runs the
//! command). See `cubesys/src/commands.rs` `tick` + `native-apply`.
/// First `C` axis value reserved for OS operator kernels (call-graph roots and
/// leaves). The OS keeps its behavioral substrate here, separate from the
/// `c200` operational-snapshot data band and the `c001/c002` doc examples.
pub const C_OS_KERNEL: u8 = 210;
/// `C` axis band for OS *data* records the native layer writes after a kernel
/// decides an effect (kept distinct from the compute-kernel band above).
pub const C_OS_EFFECT: u8 = 211;
/// Header-flag bits 13..=15 are reserved for the behavior-descriptor field.
/// (Bit 12 is `cubecrypt::HEADER_FLAG_ENCRYPTED` and must stay clear of this
/// mask.) `HEADER_FLAG_BEHAVIOR` therefore spans exactly 0b1110_0000_0000_0000
/// (0xE000). `refresh_flags()` preserves spare bits 8..=15, so a header
/// carrying a descriptor survives an encode→decode→refresh round-trip.
pub const HEADER_FLAG_BEHAVIOR: u16 = 0b1110_0000_0000_0000; // bits 13,14,15
/// Bit position of the behavior-descriptor field within the raw flag word.
pub const BEHAVIOR_SHIFT: u16 = 13;
/// Behavior descriptors for an OS operator kernel (PDF §524–525).
///
/// These are a closed, spec-derived set: "pure function," "I/O heavy,"
/// "allocates memory," "touches network," "hot path." We map them onto three
/// available out-of-band bits (13..=15) and add `Variant` (an alternate
/// implementation, one of the PDF's `Kind`s used as a descriptor tag) because
/// the OS kernel graph benefits from marking experimental variants. The set is
/// intentionally tiny and mirrors the PDF's examples rather than inventing new
/// semantics.
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
pub struct Behavior(pub u8);
impl Behavior {
/// Pure function: no side effects, deterministic on inputs.
pub const PURE: u8 = 1 << 0;
/// I/O heavy: performs significant store/device I/O.
pub const IO_HEAVY: u8 = 1 << 1;
/// Hot path: executed frequently; a candidate for optimization/variant swap.
pub const HOT_PATH: u8 = 1 << 2;
/// Build from a raw 3-bit value (already shifted into bit 13..=15 space).
pub fn from_raw(bits: u8) -> Self {
Behavior(bits & 0b111)
}
/// Encode into the out-of-band header flag bits (bits 13..=15).
pub fn to_flags(self) -> u16 {
((self.0 & 0b111) as u16) << BEHAVIOR_SHIFT
}
/// Decode from a raw 16-bit flag word (reads bits 13..=15).
pub fn from_flags(flags: u16) -> Self {
Behavior(((flags & HEADER_FLAG_BEHAVIOR) >> BEHAVIOR_SHIFT) as u8)
}
/// True if any descriptor bit is set.
pub fn is_any(self) -> bool {
self.0 != 0
}
/// Human-readable descriptor tags (used by `ls`/`stat` and the effector).
pub fn tags(self) -> Vec<&'static str> {
let mut out = Vec::new();
if self.0 & Self::PURE != 0 {
out.push("pure");
}
if self.0 & Self::IO_HEAVY != 0 {
out.push("io");
}
if self.0 & Self::HOT_PATH != 0 {
out.push("hot");
}
out
}
}
#[cfg(test)]
mod tests {
use super::*;
use cubecoords::{CubeHeader, Czyx};
use cubestore::{CubeStore, HashBackend};
#[test]
fn behavior_round_trips_through_store() {
// PURE | HOT_PATH must survive the real store round-trip (what
// native-apply reads back). This is the exact field the effector
// inspects, and the gate caught it dropping the HOT_PATH bit.
let b = Behavior(Behavior::PURE | Behavior::HOT_PATH);
let raw = b.to_flags();
assert_eq!(raw, 0x2000 | 0x8000, "to_flags wrong: {raw:#x}");
let mut store = CubeStore::new(HashBackend::new());
let coord = Czyx::new(210, 1, 1, 3);
let mut h = CubeHeader::new();
h.doc_type = Some("kernel".into());
h.title = Some("summarize-procs".into());
h.flags.0 |= raw;
h.refresh_flags();
store.put_record(coord, &h, &[1, 2, 3]);
let (read_h, _) = store.get_record(&coord).expect("record present");
let back = Behavior::from_flags(read_h.flags.bits());
assert_eq!(
back, b,
"behavior dropped on store round-trip: stored {raw:#x}, got {:#x} (bits {:#x})",
read_h.flags.bits(),
read_h.flags.bits()
);
}
}
+3
View File
@@ -35,10 +35,13 @@
#![forbid(unsafe_code)]
#![warn(missing_docs)]
pub mod cb;
pub mod cell;
pub mod opcode;
pub mod vm;
pub use cb::{Behavior, C_OS_EFFECT, C_OS_KERNEL, HEADER_FLAG_BEHAVIOR};
pub use cell::{CodeCell, Kind};
pub use opcode::{decode, encode, CodeError, Op};
pub use vm::{Fault, RunResult, Vm, SYS_DEGREE, SYS_LINKED_EXISTS, SYS_NOP, SYS_TRACE};