From 5a630f608409025e5e7f297af6701d54b5c36654 Mon Sep 17 00:00:00 2001 From: Joel Teply Date: Mon, 16 Nov 2026 16:41:44 -0600 Subject: [PATCH 1/2] Contract slice 1, the core edge: bindings, typed ids and a validator checked byte for byte against the engine's (CONTINUUM-ENGINE 4.11) Serves every mind's switch, act and learning step: the seam they cross gets one definition, generated for both sides, instead of JSON each side guesses at. Plan line 3b (one live engine); slice 1 of the accepted design (#6092). - core/llama/contract_build.rs: binds the fork's tools/server/server-continuum.h (layout tests on: fixed-size POD frames), compiles the engine's own validator, and derives the schema identity exactly as the fork's CMake does (first 16 bytes of the header's SHA-256). llama::contract exposes the frames, ctm_validate and SCHEMA_ID. - continuum-engine inference::engine_contract: EngineInstanceId / ActId / LayerId as distinct types over the UUID's 16 bytes (ActId/LayerId mint UUIDv7; workspace uuid gains v7); validate() mirrors ctm_validate (magic, version, schema, kind, exact size, zero reserved, tags via checked conversion, buffer bounds) reading fields at the bound structs' own offsets. - tests.rs: both edges must give the same verdict on the same bytes for every case, and the two compiled schema identities must be equal. - Submodule pinned to fork fable/contract-s1 (c1b4abceb, llama.cpp#141): draft until that lands on the shipping line. No behavior change; nothing calls it yet (slices 2-6 replace the JSON call paths). Not built locally (the M5 is serving); CI is the first compile. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo --- Cargo.toml | 2 +- .../src/inference/engine_contract.rs | 12 ++ .../src/inference/engine_contract/ids.rs | 56 +++++ .../src/inference/engine_contract/tests.rs | 94 ++++++++ .../src/inference/engine_contract/validate.rs | 203 ++++++++++++++++++ core/continuum-engine/src/inference/mod.rs | 1 + core/llama/build.rs | 3 + core/llama/contract_build.rs | 51 +++++ core/llama/src/lib.rs | 10 + core/vendor/llama.cpp | 2 +- 10 files changed, 432 insertions(+), 2 deletions(-) create mode 100644 core/continuum-engine/src/inference/engine_contract.rs create mode 100644 core/continuum-engine/src/inference/engine_contract/ids.rs create mode 100644 core/continuum-engine/src/inference/engine_contract/tests.rs create mode 100644 core/continuum-engine/src/inference/engine_contract/validate.rs create mode 100644 core/llama/contract_build.rs diff --git a/Cargo.toml b/Cargo.toml index 2825115680..b85c7a3cb5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -309,7 +309,7 @@ tokio-stream = "0.1" # Timing and UUIDs (for JTAG protocol) chrono = "0.4" -uuid = { version = "1.6", features = ["v4", "v5", "serde"] } +uuid = { version = "1.6", features = ["v4", "v5", "v7", "serde"] } # Safetensors for model/adapter weights safetensors = "0.7" diff --git a/core/continuum-engine/src/inference/engine_contract.rs b/core/continuum-engine/src/inference/engine_contract.rs new file mode 100644 index 0000000000..0b73631d25 --- /dev/null +++ b/core/continuum-engine/src/inference/engine_contract.rs @@ -0,0 +1,12 @@ +//! The core's edge of the core<->engine contract (CONTINUUM-ENGINE 4.11; the definition is the fork's +//! `server-continuum.h`, bound in `llama::contract`). Ids are their native 16 bytes in distinct types; a frame's +//! kind and tags are validated here exactly as the engine validates them (`validate`), and the cross-language test +//! (`tests.rs`) runs both validators over the same bytes so the two edges cannot drift. + +mod ids; +mod validate; +#[cfg(test)] +mod tests; + +pub use ids::{ActId, EngineInstanceId, LayerId}; +pub use validate::{validate, FrameError, FrameKind}; diff --git a/core/continuum-engine/src/inference/engine_contract/ids.rs b/core/continuum-engine/src/inference/engine_contract/ids.rs new file mode 100644 index 0000000000..611c832af9 --- /dev/null +++ b/core/continuum-engine/src/inference/engine_contract/ids.rs @@ -0,0 +1,56 @@ +//! The contract's ids: UUIDs in their 16 bytes, one type per kind, compared by exact equality only +//! (CONTINUUM-ENGINE 4.11.2). UUIDv7 time order is for reading logs, never for deciding freshness. + +use llama::contract::ctm_id; +use uuid::Uuid; + +macro_rules! wire_id { + ($(#[$doc:meta])* $name:ident) => { + $(#[$doc])* + #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)] + pub struct $name(Uuid); + + impl $name { + pub fn from_uuid(id: Uuid) -> Self { + Self(id) + } + pub fn uuid(&self) -> Uuid { + self.0 + } + /// The frame field's form: the UUID's 16 bytes in RFC 9562 order. + pub fn to_wire(self) -> ctm_id { + ctm_id { bytes: *self.0.as_bytes() } + } + pub fn from_wire(id: &ctm_id) -> Self { + Self(Uuid::from_bytes(id.bytes)) + } + } + }; +} + +wire_id!( + /// The engine process's identity, minted by the engine at start and adopted at attach. + EngineInstanceId +); +wire_id!( + /// One act (a turn): minted once per act by the core; a retry keeps it; a learn pin is its act. + ActId +); +wire_id!( + /// One LoRA layer of a mind's genome. + LayerId +); + +impl ActId { + /// A new act's id (UUIDv7). + pub fn mint() -> Self { + Self(Uuid::now_v7()) + } +} + +impl LayerId { + /// A new layer's id (UUIDv7). + pub fn mint() -> Self { + Self(Uuid::now_v7()) + } +} diff --git a/core/continuum-engine/src/inference/engine_contract/tests.rs b/core/continuum-engine/src/inference/engine_contract/tests.rs new file mode 100644 index 0000000000..5512a98ca3 --- /dev/null +++ b/core/continuum-engine/src/inference/engine_contract/tests.rs @@ -0,0 +1,94 @@ +//! what this catches: the two edges of the core<->engine contract drifting. Every case runs the Rust validator and +//! the engine's own C validator (`ctm_validate`, linked from the fork) over the SAME bytes and requires the same +//! verdict, and the schema identity both sides compiled in to be equal (CONTINUUM-ENGINE 4.11.3). + +use super::*; +use llama::contract as c; + +fn bytes_of(frame: &T) -> &[u8] { + // test: a bound POD frame viewed as its bytes, exactly what crosses the wire + unsafe { std::slice::from_raw_parts((frame as *const T).cast::(), std::mem::size_of::()) } +} + +fn engine_verdict(bytes: &[u8]) -> i32 { + // test: the fork's validator over the same bytes + unsafe { c::ctm_validate(bytes.as_ptr().cast(), bytes.len()) } +} + +/// Rust and C agree on `bytes`; returns the shared verdict as the engine's code. +fn agree(bytes: &[u8]) -> i32 { + let rust = validate(bytes).map_or_else(FrameError::code, |_| c::CTM_VALID as i32); + let engine = engine_verdict(bytes); + assert_eq!(rust, engine, "the Rust and engine edges disagree on the same bytes"); + rust +} + +fn header(kind: u32, size: usize) -> c::ctm_frame_header { + c::ctm_frame_header { + magic: c::CTM_MAGIC, + schema_version: c::CTM_SCHEMA_VERSION as u16, + kind: kind as u16, + size: size as u32, + reserved: 0, + schema: c::ctm_id { bytes: c::SCHEMA_ID }, + } +} + +#[test] +fn both_edges_compiled_in_the_same_schema() { + // test: reading the linked static + let engine = unsafe { c::ctm_schema_id.bytes }; + assert_eq!(engine, c::SCHEMA_ID, "core and engine were built from different contract headers"); +} + +#[test] +fn both_edges_give_every_frame_the_same_verdict() { + let mut p = c::ctm_prefetch { header: header(c::CTM_KIND_PREFETCH, std::mem::size_of::()), ..Default::default() }; + assert_eq!(agree(bytes_of(&p)), c::CTM_VALID as i32); + assert_eq!(agree(&bytes_of(&p)[..31]), c::CTM_INVALID_SHORT as i32); + p.header.magic ^= 1; + assert_eq!(agree(bytes_of(&p)), c::CTM_INVALID_MAGIC as i32); + p.header.magic ^= 1; + p.header.schema_version = 2; + assert_eq!(agree(bytes_of(&p)), c::CTM_INVALID_VERSION as i32); + p.header.schema_version = c::CTM_SCHEMA_VERSION as u16; + p.header.schema.bytes[0] ^= 1; + assert_eq!(agree(bytes_of(&p)), c::CTM_INVALID_SCHEMA as i32); + p.header.schema.bytes[0] ^= 1; + p.header.kind = 999; + assert_eq!(agree(bytes_of(&p)), c::CTM_INVALID_KIND as i32); + p.header.kind = c::CTM_KIND_PREFETCH as u16; + p.header.size += 8; + assert_eq!(agree(bytes_of(&p)), c::CTM_INVALID_SIZE as i32); + p.header.size -= 8; + p.header.reserved = 1; + assert_eq!(agree(bytes_of(&p)), c::CTM_INVALID_RESERVED as i32); + + let mut a = c::ctm_act_submit { header: header(c::CTM_KIND_ACT_SUBMIT, std::mem::size_of::()), ..Default::default() }; + a.mode = c::CTM_ACT_GENERATE as u16; + a.flags = c::CTM_ACT_LEARN_PIN; + a.act = ActId::mint().to_wire(); + assert_eq!(agree(bytes_of(&a)), c::CTM_VALID as i32); + a.mode = 7; + assert_eq!(agree(bytes_of(&a)), c::CTM_INVALID_TAG as i32, "an out-of-range enum is refused, never cast"); + a.mode = c::CTM_ACT_LEARN as u16; + a.flags = 1 << 9; + assert_eq!(agree(bytes_of(&a)), c::CTM_INVALID_TAG as i32, "an unknown flag bit"); + a.flags = 0; + a.prompt.offset = u64::MAX; + a.prompt.length = 2; + assert_eq!(agree(bytes_of(&a)), c::CTM_INVALID_BUFFER_BOUNDS as i32, "offset + length overflows"); + + let mut l = c::ctm_learn_duty { header: header(c::CTM_KIND_LEARN_DUTY, std::mem::size_of::()), ..Default::default() }; + l.mode = c::CTM_LEARN_DREAM as u16; + l.share_ppm = 1_000_001; + assert_eq!(agree(bytes_of(&l)), c::CTM_INVALID_TAG as i32, "a share past the whole"); +} + +#[test] +fn an_id_crosses_as_its_own_sixteen_bytes() { + let act = ActId::mint(); + assert_eq!(act.to_wire().bytes, *act.uuid().as_bytes()); + assert_eq!(ActId::from_wire(&act.to_wire()), act); + assert_eq!(act.uuid().get_version_num(), 7); +} diff --git a/core/continuum-engine/src/inference/engine_contract/validate.rs b/core/continuum-engine/src/inference/engine_contract/validate.rs new file mode 100644 index 0000000000..3e511355a2 --- /dev/null +++ b/core/continuum-engine/src/inference/engine_contract/validate.rs @@ -0,0 +1,203 @@ +//! The Rust edge's validation of a frame, the same rules as the engine's `ctm_validate` (server-continuum.cpp): +//! magic, schema version, schema identity, a known kind, the kind's exact size, zero reserved bytes, every tag in +//! range and every buffer's `offset + length` without overflow. Fields are read at the bound structs' own offsets +//! (`offset_of!`), little-endian, from bytes that need not be aligned; nothing is cast into an enum. + +use llama::contract as c; +use std::mem::{offset_of, size_of}; + +/// Why a frame was refused; each the engine's `ctm_validation` value of the same name. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum FrameError { + Short, + Magic, + Version, + Schema, + Kind, + Size, + Reserved, + Tag, + BufferBounds, +} + +impl FrameError { + /// The engine's code for the same refusal (the cross-language test compares them). + pub fn code(self) -> i32 { + (match self { + Self::Short => c::CTM_INVALID_SHORT, + Self::Magic => c::CTM_INVALID_MAGIC, + Self::Version => c::CTM_INVALID_VERSION, + Self::Schema => c::CTM_INVALID_SCHEMA, + Self::Kind => c::CTM_INVALID_KIND, + Self::Size => c::CTM_INVALID_SIZE, + Self::Reserved => c::CTM_INVALID_RESERVED, + Self::Tag => c::CTM_INVALID_TAG, + Self::BufferBounds => c::CTM_INVALID_BUFFER_BOUNDS, + }) as i32 + } +} + +/// A frame's kind, converted from its raw tag; an unknown tag is `FrameError::Kind`. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum FrameKind { + Attach, + EngineDescriptor, + ActSubmit, + ActDone, + Cancel, + Prefetch, + Placed, + PinRelease, + Released, + GenomeSet, + LearnDuty, + Refused, +} + +impl FrameKind { + fn from_tag(tag: u16) -> Option { + Some(match u32::from(tag) { + c::CTM_KIND_ATTACH => Self::Attach, + c::CTM_KIND_ENGINE_DESCRIPTOR => Self::EngineDescriptor, + c::CTM_KIND_ACT_SUBMIT => Self::ActSubmit, + c::CTM_KIND_ACT_DONE => Self::ActDone, + c::CTM_KIND_CANCEL => Self::Cancel, + c::CTM_KIND_PREFETCH => Self::Prefetch, + c::CTM_KIND_PLACED => Self::Placed, + c::CTM_KIND_PIN_RELEASE => Self::PinRelease, + c::CTM_KIND_RELEASED => Self::Released, + c::CTM_KIND_GENOME_SET => Self::GenomeSet, + c::CTM_KIND_LEARN_DUTY => Self::LearnDuty, + c::CTM_KIND_REFUSED => Self::Refused, + _ => return None, + }) + } + + /// The kind's fixed size, the same as the engine's `ctm_frame_size`. + pub fn size(self) -> usize { + match self { + Self::Attach => size_of::(), + Self::EngineDescriptor => size_of::(), + Self::ActSubmit => size_of::(), + Self::ActDone => size_of::(), + Self::Cancel => size_of::(), + Self::Prefetch => size_of::(), + Self::Placed => size_of::(), + Self::PinRelease => size_of::(), + Self::Released => size_of::(), + Self::GenomeSet => size_of::(), + Self::LearnDuty => size_of::(), + Self::Refused => size_of::(), + } + } +} + +struct Frame<'a>(&'a [u8]); + +impl Frame<'_> { + fn u16(&self, at: usize) -> u16 { + u16::from_le_bytes([self.0[at], self.0[at + 1]]) + } + fn u32(&self, at: usize) -> u32 { + let mut b = [0u8; 4]; + b.copy_from_slice(&self.0[at..at + 4]); + u32::from_le_bytes(b) + } + fn u64(&self, at: usize) -> u64 { + let mut b = [0u8; 8]; + b.copy_from_slice(&self.0[at..at + 8]); + u64::from_le_bytes(b) + } + /// `offset + length` of the buffer_ref at `at` does not overflow. + fn buffer_ok(&self, at: usize) -> bool { + let offset = self.u64(at + offset_of!(c::ctm_buffer_ref, offset)); + let length = self.u64(at + offset_of!(c::ctm_buffer_ref, length)); + offset.checked_add(length).is_some() + } +} + +fn in_range(v: u32, lo: u32, count: u32) -> bool { + (lo..count).contains(&v) +} + +/// Validate `bytes` as one frame of this build's schema; its kind when valid. +pub fn validate(bytes: &[u8]) -> Result { + if bytes.len() < size_of::() { + return Err(FrameError::Short); + } + let f = Frame(bytes); + if f.u32(offset_of!(c::ctm_frame_header, magic)) != c::CTM_MAGIC { + return Err(FrameError::Magic); + } + if u32::from(f.u16(offset_of!(c::ctm_frame_header, schema_version))) != c::CTM_SCHEMA_VERSION { + return Err(FrameError::Version); + } + let schema_at = offset_of!(c::ctm_frame_header, schema); + if bytes[schema_at..schema_at + 16] != c::SCHEMA_ID { + return Err(FrameError::Schema); + } + let kind = FrameKind::from_tag(f.u16(offset_of!(c::ctm_frame_header, kind))).ok_or(FrameError::Kind)?; + if usize::try_from(f.u32(offset_of!(c::ctm_frame_header, size))).ok() != Some(kind.size()) { + return Err(FrameError::Size); + } + if bytes.len() < kind.size() { + return Err(FrameError::Short); + } + if f.u32(offset_of!(c::ctm_frame_header, reserved)) != 0 { + return Err(FrameError::Reserved); + } + check_tags(kind, &f).map(|()| kind) +} + +fn check_tags(kind: FrameKind, f: &Frame<'_>) -> Result<(), FrameError> { + let tag = |ok: bool| if ok { Ok(()) } else { Err(FrameError::Tag) }; + let zero = |ok: bool| if ok { Ok(()) } else { Err(FrameError::Reserved) }; + let bounds = |ok: bool| if ok { Ok(()) } else { Err(FrameError::BufferBounds) }; + match kind { + FrameKind::EngineDescriptor => { + zero(f.u16(offset_of!(c::ctm_engine_descriptor, reserved0)) == 0)?; + bounds(f.buffer_ok(offset_of!(c::ctm_engine_descriptor, resources))) + } + FrameKind::ActSubmit => { + zero(f.u16(offset_of!(c::ctm_act_submit, reserved0)) == 0)?; + tag(in_range(u32::from(f.u16(offset_of!(c::ctm_act_submit, mode))), 1, c::CTM_ACT_MODE_COUNT))?; + tag(f.u32(offset_of!(c::ctm_act_submit, flags)) & !c::CTM_ACT_FLAGS_ALL == 0)?; + for at in [ + offset_of!(c::ctm_act_submit, prompt), + offset_of!(c::ctm_act_submit, labels), + offset_of!(c::ctm_act_submit, view), + offset_of!(c::ctm_act_submit, sampling), + ] { + bounds(f.buffer_ok(at))?; + } + Ok(()) + } + FrameKind::ActDone => { + tag(in_range(u32::from(f.u16(offset_of!(c::ctm_act_done, outcome))), 1, c::CTM_OUTCOME_COUNT))?; + tag(in_range(u32::from(f.u16(offset_of!(c::ctm_act_done, reason))), 0, c::CTM_REASON_COUNT))?; + tag(in_range(u32::from(f.u16(offset_of!(c::ctm_act_done, mind_from))), 0, c::CTM_MIND_FROM_COUNT))?; + tag(u32::from(f.u16(offset_of!(c::ctm_act_done, flags))) & !c::CTM_DONE_FLAGS_ALL == 0)?; + bounds(f.buffer_ok(offset_of!(c::ctm_act_done, observations))) + } + FrameKind::Placed => { + zero(f.u16(offset_of!(c::ctm_placed, reserved0)) == 0 && f.u32(offset_of!(c::ctm_placed, reserved1)) == 0)?; + tag(in_range(u32::from(f.u16(offset_of!(c::ctm_placed, place))), 1, c::CTM_PLACE_COUNT)) + } + FrameKind::Released => { + zero(f.u16(offset_of!(c::ctm_released, reserved0)) == 0 && f.u32(offset_of!(c::ctm_released, reserved1)) == 0)?; + tag(in_range(u32::from(f.u16(offset_of!(c::ctm_released, outcome))), 1, c::CTM_PIN_COUNT)) + } + FrameKind::GenomeSet => bounds(f.buffer_ok(offset_of!(c::ctm_genome_set, layers))), + FrameKind::LearnDuty => { + zero(f.u16(offset_of!(c::ctm_learn_duty, reserved0)) == 0)?; + tag(in_range(u32::from(f.u16(offset_of!(c::ctm_learn_duty, mode))), 1, c::CTM_LEARN_MODE_COUNT))?; + tag(f.u32(offset_of!(c::ctm_learn_duty, share_ppm)) <= 1_000_000) + } + FrameKind::Refused => { + zero(f.u32(offset_of!(c::ctm_refused, reserved0)) == 0)?; + tag(in_range(u32::from(f.u16(offset_of!(c::ctm_refused, refused_kind))), 0, c::CTM_KIND_KIND_COUNT))?; + tag(in_range(u32::from(f.u16(offset_of!(c::ctm_refused, reason))), 0, c::CTM_REASON_COUNT)) + } + FrameKind::Attach | FrameKind::Cancel | FrameKind::Prefetch | FrameKind::PinRelease => Ok(()), + } +} diff --git a/core/continuum-engine/src/inference/mod.rs b/core/continuum-engine/src/inference/mod.rs index 50d05004e3..5d303cf49b 100644 --- a/core/continuum-engine/src/inference/mod.rs +++ b/core/continuum-engine/src/inference/mod.rs @@ -23,6 +23,7 @@ pub mod backends; pub mod batching_probe; pub mod child_log; pub mod embed_lane_memory; +pub mod engine_contract; pub mod engine_client; pub use engine_client::engine_client; pub mod engine_stall; diff --git a/core/llama/build.rs b/core/llama/build.rs index 8f63cf3bb1..3df64aceee 100644 --- a/core/llama/build.rs +++ b/core/llama/build.rs @@ -14,6 +14,7 @@ use std::path::PathBuf; mod cpu_target; mod msvc_cache; +mod contract_build; #[path = "../continuum-cli-lifecycle/src/cargo_cache_custody.rs"] mod cargo_cache_custody; @@ -447,6 +448,8 @@ fn main() { bindings .write_to_file(&out_path) .expect("Failed to write bindings"); + // the core<->engine contract (CONTINUUM-ENGINE 4.11): its bindings, schema identity and the engine's validator + contract_build::build(&submodule, &PathBuf::from(env::var("OUT_DIR").expect("OUT_DIR is set when cargo runs this build script"))); // These children supply static link inputs only; Metal's library is embedded // above. System runtime libraries remain outside this output. The generation // is also recorded in Cargo's captured producer output for legacy rollback. diff --git a/core/llama/contract_build.rs b/core/llama/contract_build.rs new file mode 100644 index 0000000000..6dd393cc24 --- /dev/null +++ b/core/llama/contract_build.rs @@ -0,0 +1,51 @@ +// The core<->engine contract (CONTINUUM-ENGINE 4.11): the fork's `tools/server/server-continuum.h` is the one +// definition of every frame. This step binds it with bindgen (layout tests ON: these are fixed-size POD frames, +// unlike the opaque pointer-only structs the llama bindings bind), compiles the engine's own validator so the core +// can check its Rust edge against it byte for byte, and derives the schema identity exactly as the fork's CMake +// does: the first 16 bytes of the header's SHA-256. + +use sha2::{Digest, Sha256}; +use std::path::Path; + +pub fn build(submodule: &Path, out_dir: &Path) { + let server = submodule.join("tools").join("server"); + let header = server.join("server-continuum.h"); + println!("cargo:rerun-if-changed={}", header.display()); + println!("cargo:rerun-if-changed={}", server.join("server-continuum.cpp").display()); + + let bytes = std::fs::read(&header).unwrap_or_else(|e| panic!("the contract header {}: {e}", header.display())); + let digest = Sha256::digest(&bytes); + let id: Vec = digest[..16].iter().map(|b| format!("0x{b:02x}")).collect(); + + let schema_c = out_dir.join("server-continuum-schema.c"); + std::fs::write( + &schema_c, + format!("#include \"server-continuum.h\"\nconst ctm_id ctm_schema_id = {{ {{ {} }} }};\n", id.join(", ")), + ) + .expect("write the contract's schema identity"); + std::fs::write(out_dir.join("contract_schema_id.rs"), format!("[{}]", id.join(", "))) + .expect("write the contract's schema identity for Rust"); + + cc::Build::new() + .cpp(true) + .std("c++17") + .flag_if_supported("/EHsc") + .include(&server) + .file(server.join("server-continuum.cpp")) + .compile("continuum_contract_cpp"); + cc::Build::new().include(&server).file(&schema_c).compile("continuum_contract_schema"); + + bindgen::Builder::default() + .header(header.to_str().expect("the contract header's path is UTF-8")) + .allowlist_type("ctm_.*") + .allowlist_function("ctm_.*") + .allowlist_var("CTM_.*") + .allowlist_var("ctm_schema_id") + .derive_default(true) + .derive_eq(true) + .layout_tests(true) + .generate() + .expect("bind the contract header") + .write_to_file(out_dir.join("contract.rs")) + .expect("write the contract bindings"); +} diff --git a/core/llama/src/lib.rs b/core/llama/src/lib.rs index 25f016cd56..874518d15e 100644 --- a/core/llama/src/lib.rs +++ b/core/llama/src/lib.rs @@ -65,6 +65,16 @@ pub mod sys { include!(concat!(env!("OUT_DIR"), "/bindings.rs")); } +/// The core<->engine contract (CONTINUUM-ENGINE 4.11), generated from the fork's `server-continuum.h`: the raw +/// frames, the engine's own validator and the schema identity. Safe types and the Rust edge's validation live in +/// `continuum-engine` (`inference::engine_contract`), which checks itself against this validator byte for byte. +pub mod contract { + #![allow(non_camel_case_types, non_snake_case, non_upper_case_globals, dead_code)] + include!(concat!(env!("OUT_DIR"), "/contract.rs")); + /// The schema identity this core was built with: the first 16 bytes of the header's SHA-256. + pub const SCHEMA_ID: [u8; 16] = include!(concat!(env!("OUT_DIR"), "/contract_schema_id.rs")); +} + mod mtmd; mod safe; mod decode_boundary; diff --git a/core/vendor/llama.cpp b/core/vendor/llama.cpp index f39cf25fbd..c1b4abcebb 160000 --- a/core/vendor/llama.cpp +++ b/core/vendor/llama.cpp @@ -1 +1 @@ -Subproject commit f39cf25fbd3d95c69302fdf38d43dae2002b5f3e +Subproject commit c1b4abcebb7980fdf23f48f7210cf79ebfe29b7d From 44a55981f5c0a911cc1b66ca10f3ef997018b7a8 Mon Sep 17 00:00:00 2001 From: Joel Teply Date: Mon, 16 Nov 2026 16:46:44 -0600 Subject: [PATCH 2/2] Contract slice 1: record the moved pin in LLAMA_CPP_PIN (fork fable/contract-s1, draft until llama.cpp#141 lands); in_range takes a typed Range; the short-frame case slices by the header's size, not a literal Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo --- .../src/inference/engine_contract/tests.rs | 3 ++- .../src/inference/engine_contract/validate.rs | 22 +++++++++---------- core/vendor/LLAMA_CPP_PIN | 2 +- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/core/continuum-engine/src/inference/engine_contract/tests.rs b/core/continuum-engine/src/inference/engine_contract/tests.rs index 5512a98ca3..e8e86a9080 100644 --- a/core/continuum-engine/src/inference/engine_contract/tests.rs +++ b/core/continuum-engine/src/inference/engine_contract/tests.rs @@ -45,7 +45,8 @@ fn both_edges_compiled_in_the_same_schema() { fn both_edges_give_every_frame_the_same_verdict() { let mut p = c::ctm_prefetch { header: header(c::CTM_KIND_PREFETCH, std::mem::size_of::()), ..Default::default() }; assert_eq!(agree(bytes_of(&p)), c::CTM_VALID as i32); - assert_eq!(agree(&bytes_of(&p)[..31]), c::CTM_INVALID_SHORT as i32); + let one_short = std::mem::size_of::() - 1; + assert_eq!(agree(&bytes_of(&p)[..one_short]), c::CTM_INVALID_SHORT as i32); p.header.magic ^= 1; assert_eq!(agree(bytes_of(&p)), c::CTM_INVALID_MAGIC as i32); p.header.magic ^= 1; diff --git a/core/continuum-engine/src/inference/engine_contract/validate.rs b/core/continuum-engine/src/inference/engine_contract/validate.rs index 3e511355a2..c6cd68eba0 100644 --- a/core/continuum-engine/src/inference/engine_contract/validate.rs +++ b/core/continuum-engine/src/inference/engine_contract/validate.rs @@ -116,8 +116,8 @@ impl Frame<'_> { } } -fn in_range(v: u32, lo: u32, count: u32) -> bool { - (lo..count).contains(&v) +fn in_range(v: u32, valid: std::ops::Range) -> bool { + valid.contains(&v) } /// Validate `bytes` as one frame of this build's schema; its kind when valid. @@ -160,7 +160,7 @@ fn check_tags(kind: FrameKind, f: &Frame<'_>) -> Result<(), FrameError> { } FrameKind::ActSubmit => { zero(f.u16(offset_of!(c::ctm_act_submit, reserved0)) == 0)?; - tag(in_range(u32::from(f.u16(offset_of!(c::ctm_act_submit, mode))), 1, c::CTM_ACT_MODE_COUNT))?; + tag(in_range(u32::from(f.u16(offset_of!(c::ctm_act_submit, mode))), 1..c::CTM_ACT_MODE_COUNT))?; tag(f.u32(offset_of!(c::ctm_act_submit, flags)) & !c::CTM_ACT_FLAGS_ALL == 0)?; for at in [ offset_of!(c::ctm_act_submit, prompt), @@ -173,30 +173,30 @@ fn check_tags(kind: FrameKind, f: &Frame<'_>) -> Result<(), FrameError> { Ok(()) } FrameKind::ActDone => { - tag(in_range(u32::from(f.u16(offset_of!(c::ctm_act_done, outcome))), 1, c::CTM_OUTCOME_COUNT))?; - tag(in_range(u32::from(f.u16(offset_of!(c::ctm_act_done, reason))), 0, c::CTM_REASON_COUNT))?; - tag(in_range(u32::from(f.u16(offset_of!(c::ctm_act_done, mind_from))), 0, c::CTM_MIND_FROM_COUNT))?; + tag(in_range(u32::from(f.u16(offset_of!(c::ctm_act_done, outcome))), 1..c::CTM_OUTCOME_COUNT))?; + tag(in_range(u32::from(f.u16(offset_of!(c::ctm_act_done, reason))), 0..c::CTM_REASON_COUNT))?; + tag(in_range(u32::from(f.u16(offset_of!(c::ctm_act_done, mind_from))), 0..c::CTM_MIND_FROM_COUNT))?; tag(u32::from(f.u16(offset_of!(c::ctm_act_done, flags))) & !c::CTM_DONE_FLAGS_ALL == 0)?; bounds(f.buffer_ok(offset_of!(c::ctm_act_done, observations))) } FrameKind::Placed => { zero(f.u16(offset_of!(c::ctm_placed, reserved0)) == 0 && f.u32(offset_of!(c::ctm_placed, reserved1)) == 0)?; - tag(in_range(u32::from(f.u16(offset_of!(c::ctm_placed, place))), 1, c::CTM_PLACE_COUNT)) + tag(in_range(u32::from(f.u16(offset_of!(c::ctm_placed, place))), 1..c::CTM_PLACE_COUNT)) } FrameKind::Released => { zero(f.u16(offset_of!(c::ctm_released, reserved0)) == 0 && f.u32(offset_of!(c::ctm_released, reserved1)) == 0)?; - tag(in_range(u32::from(f.u16(offset_of!(c::ctm_released, outcome))), 1, c::CTM_PIN_COUNT)) + tag(in_range(u32::from(f.u16(offset_of!(c::ctm_released, outcome))), 1..c::CTM_PIN_COUNT)) } FrameKind::GenomeSet => bounds(f.buffer_ok(offset_of!(c::ctm_genome_set, layers))), FrameKind::LearnDuty => { zero(f.u16(offset_of!(c::ctm_learn_duty, reserved0)) == 0)?; - tag(in_range(u32::from(f.u16(offset_of!(c::ctm_learn_duty, mode))), 1, c::CTM_LEARN_MODE_COUNT))?; + tag(in_range(u32::from(f.u16(offset_of!(c::ctm_learn_duty, mode))), 1..c::CTM_LEARN_MODE_COUNT))?; tag(f.u32(offset_of!(c::ctm_learn_duty, share_ppm)) <= 1_000_000) } FrameKind::Refused => { zero(f.u32(offset_of!(c::ctm_refused, reserved0)) == 0)?; - tag(in_range(u32::from(f.u16(offset_of!(c::ctm_refused, refused_kind))), 0, c::CTM_KIND_KIND_COUNT))?; - tag(in_range(u32::from(f.u16(offset_of!(c::ctm_refused, reason))), 0, c::CTM_REASON_COUNT)) + tag(in_range(u32::from(f.u16(offset_of!(c::ctm_refused, refused_kind))), 0..c::CTM_KIND_KIND_COUNT))?; + tag(in_range(u32::from(f.u16(offset_of!(c::ctm_refused, reason))), 0..c::CTM_REASON_COUNT)) } FrameKind::Attach | FrameKind::Cancel | FrameKind::Prefetch | FrameKind::PinRelease => Ok(()), } diff --git a/core/vendor/LLAMA_CPP_PIN b/core/vendor/LLAMA_CPP_PIN index 5f0a504f5f..11eb22099a 100644 --- a/core/vendor/LLAMA_CPP_PIN +++ b/core/vendor/LLAMA_CPP_PIN @@ -1 +1 @@ -f39cf25fbd3d95c69302fdf38d43dae2002b5f3e +c1b4abcebb7980fdf23f48f7210cf79ebfe29b7d