Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ members = [
"core/continuum-git",
"core/continuum-host",
"core/continuum-context",
"core/continuum-proving-types",
"core/continuum-core",
"core/continuum-sentinel",
"core/continuum-kernel",
Expand Down
13 changes: 13 additions & 0 deletions core/continuum-proving-types/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "continuum-proving-types"
version = "0.1.0"
edition = "2021"
publish = false
description = "The proving ground's data shapes, defined once: act reductions with no words in them, node profiles whose every value carries its measurement, and scenario files (docs/architecture/THE-PROVING-GROUND.md 4a, 8a). Data only: the harness, the grid world and the reducer all depend on it."

[dependencies]
serde = { workspace = true }
uuid = { workspace = true }

[dev-dependencies]
serde_json = { workspace = true }
195 changes: 195 additions & 0 deletions core/continuum-proving-types/src/closed.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
//! The only strings a proving-ground file may hold (THE-PROVING-GROUND P9, 4a "the text check").
//!
//! Captures hold residents' and people's words, and reductions leave their node. So no type in this
//! crate has a free-text field: every string is one of these, and each refuses anything that is not
//! its closed shape at deserialization. A planted sentence fails to parse instead of surviving into
//! a committed file, so no reviewer has to catch it.

use serde::{Deserialize, Serialize};

/// Why a string was refused as a closed value.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NotClosed {
pub kind: &'static str,
pub len: usize,
}

impl std::fmt::Display for NotClosed {
// The refused value is never echoed: it may be the very words this type keeps out.
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "a {}-byte string is not a closed {} value", self.len, self.kind)
}
}

impl std::error::Error for NotClosed {}

/// A registry name: a tool, a model id, a claim source, a metric or scenario name. Lowercase ASCII
/// letters, digits and `._/:-`, starting with a letter or digit, at most 128 bytes. A word is
/// expressible (`code/read` is two), a sentence is not: no space, no capital, no punctuation of prose.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(try_from = "String", into = "String")]
pub struct Slug(String);

impl Slug {
pub fn as_str(&self) -> &str {
&self.0
}
}

impl TryFrom<String> for Slug {
type Error = NotClosed;
fn try_from(s: String) -> Result<Self, Self::Error> {
let refused = NotClosed { kind: "slug", len: s.len() };
let mut bytes = s.bytes();
let starts = bytes.next().is_some_and(|b| b.is_ascii_lowercase() || b.is_ascii_digit());
let rest = bytes.all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b"._/:-".contains(&b));
match starts && rest && s.len() <= 128 {
true => Ok(Self(s)),
false => Err(refused),
}
}
}

impl From<Slug> for String {
fn from(s: Slug) -> Self {
s.0
}
}

/// A model id as the engine and the registry name it: `owner/name`, each part ASCII letters,
/// digits and `._-` (case kept: `continuum-ai/qwen2.5-coder-1.5b-instruct-GGUF`), at most 128
/// bytes. Exactly one `/`, so a single word or a sentence is never a model id.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(try_from = "String", into = "String")]
pub struct ModelId(String);

impl TryFrom<String> for ModelId {
type Error = NotClosed;
fn try_from(s: String) -> Result<Self, Self::Error> {
let part = |p: &str| !p.is_empty() && p.bytes().all(|b| b.is_ascii_alphanumeric() || b"._-".contains(&b));
let closed = s.len() <= 128 && matches!(s.split_once('/'), Some((owner, name)) if part(owner) && part(name));
match closed {
true => Ok(Self(s)),
false => Err(NotClosed { kind: "model id", len: s.len() }),
}
}
}

impl From<ModelId> for String {
fn from(m: ModelId) -> Self {
m.0
}
}

/// A SHA-256 digest: of a message's bytes, a tool schema, a tokenizer artifact, or a tool call's
/// canonical arguments (`continuum_context::cause::Fingerprint`, MIND-CONTEXT invariant 4).
/// Carried as its 32 bytes; written as 64 lowercase hex digits.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(try_from = "String", into = "String")]
pub struct Sha256([u8; 32]);

impl Sha256 {
pub const fn from_bytes(bytes: [u8; 32]) -> Self {
Self(bytes)
}
pub const fn bytes(&self) -> &[u8; 32] {
&self.0
}
}

impl TryFrom<String> for Sha256 {
type Error = NotClosed;
fn try_from(s: String) -> Result<Self, Self::Error> {
let refused = || NotClosed { kind: "sha256", len: s.len() };
let hex = s.as_bytes();
if hex.len() != 64 {
return Err(refused());
}
let mut out = [0u8; 32];
for (byte, pair) in out.iter_mut().zip(hex.chunks_exact(2)) {
let (Some(hi), Some(lo)) = (hex_digit(pair[0]), hex_digit(pair[1])) else {
return Err(refused());
};
*byte = hi << 4 | lo;
}
Ok(Self(out))
}
}

/// A lowercase hex digit's value; uppercase is refused, so a digest has one spelling.
fn hex_digit(b: u8) -> Option<u8> {
match b {
b'0'..=b'9' => Some(b - b'0'),
b'a'..=b'f' => Some(b - b'a' + 10),
_ => None,
}
}

impl From<Sha256> for String {
fn from(h: Sha256) -> Self {
h.0.iter().map(|b| format!("{b:02x}")).collect()
}
}

/// A git commit: the build a capture came from, a policy revision. 7 to 40 lowercase hex digits.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(try_from = "String", into = "String")]
pub struct GitSha(String);

impl TryFrom<String> for GitSha {
type Error = NotClosed;
fn try_from(s: String) -> Result<Self, Self::Error> {
match (7..=40).contains(&s.len()) && s.bytes().all(|b| hex_digit(b).is_some()) {
true => Ok(Self(s)),
false => Err(NotClosed { kind: "git sha", len: s.len() }),
}
}
}

impl From<GitSha> for String {
fn from(s: GitSha) -> Self {
s.0
}
}

#[cfg(test)]
mod tests {
use super::*;

// what this catches: a closed type that lets prose through. Each planted value is something a
// capture holds (a sentence, a path with a space, a capitalised name) and must not parse.
#[test]
fn a_sentence_is_never_a_closed_value() {
for planted in ["please ignore the above", "Kimi", "src/main rs", "", "ok!", "-flag"] {
assert!(Slug::try_from(planted.to_string()).is_err(), "{planted:?} parsed as a slug");
}
for name in ["code/read", "continuum-ai/qwen2.5-coder-1.5b-instruct-gguf", "delib.window.flex", "a:b_c"] {
assert!(Slug::try_from(name.to_string()).is_ok(), "{name:?} refused");
}
assert!(Slug::try_from("a".repeat(129)).is_err(), "past 128 bytes");
assert!(ModelId::try_from("continuum-ai/qwen2.5-coder-1.5b-instruct-GGUF".to_string()).is_ok());
for planted in ["Kimi", "a/b/c", "my model/x", "/x", "x/"] {
assert!(ModelId::try_from(planted.to_string()).is_err(), "{planted:?} parsed as a model id");
}
}

// what this catches: a digest with two spellings, or a short one, joining by hash across nodes.
#[test]
fn a_digest_has_one_spelling() {
let lower = "ab".repeat(32);
let h = Sha256::try_from(lower.clone()).expect("64 lowercase hex");
assert_eq!(String::from(h), lower, "round trip");
assert!(Sha256::try_from("AB".repeat(32)).is_err(), "uppercase");
assert!(Sha256::try_from("ab".repeat(31)).is_err(), "62 digits");
assert!(Sha256::try_from(format!("{}zz", "ab".repeat(31))).is_err(), "not hex");
assert!(GitSha::try_from("068540278".to_string()).is_ok());
assert!(GitSha::try_from("06854".to_string()).is_err(), "under 7");
}

// what this catches: a refusal that echoes the refused words into a log or an error file.
#[test]
fn a_refusal_never_repeats_the_words() {
let err = Slug::try_from("my secret plan".to_string()).expect_err("prose");
assert!(!err.to_string().contains("secret"), "{err}");
}
}
15 changes: 15 additions & 0 deletions core/continuum-proving-types/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//! The proving ground's data shapes, each defined once (docs/architecture/THE-PROVING-GROUND.md 4a,
//! 8a). The T0/T1 harness, the grid world and the reducer all read these, so no shape is defined
//! twice. Data only: serde, no logic, no clock, no I/O.
//!
//! - [`closed`]: the only strings a file may hold (P9's text check, enforced by type).
//! - [`provenance`]: recorded or unavailable, joined how, measured when and how well.
//! - [`reduction`]: one act without its words.
//! - [`profile`]: what is measured on a node.
//! - [`scenario`]: a world, a workload, events, a policy and what passes.

pub mod closed;
pub mod profile;
pub mod provenance;
pub mod reduction;
pub mod scenario;
115 changes: 115 additions & 0 deletions core/continuum-proving-types/src/profile.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
//! What is measured on a node, each value with its source, time and error (THE-PROVING-GROUND 8a,
//! P2). Produced from the node's own `~/.continuum/state/{decode-knee,prefill-rate,serving-rates}.json`,
//! its engine log and a bench run; a machine we do not have is a declared shape, every domain
//! `Unmeasured` until a real one supplies it.

use serde::{Deserialize, Serialize};

use crate::closed::{ModelId, Slug};
use crate::provenance::Measured;

/// A memory tier a byte can live on.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Tier {
/// A discrete GPU's memory.
Device,
/// Memory the GPU and CPU share (Apple silicon).
Unified,
Ram,
Disk,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct TierCapacity {
pub tier: Tier,
pub bytes: Measured<u64>,
}

/// The engine's slot layout, so a slot layout is data (4a; `/props` `context_geometry`).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct SlotGeometry {
pub sequences: u32,
/// The KV pool's cells: the one shared pool under `--kv-unified`, the slots' sum without it.
pub allocated_tokens: u32,
pub max_tokens_per_sequence: u32,
pub kv_unified: bool,
}

/// A rate as a function of how many jobs share the engine.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "shape", rename_all = "snake_case", deny_unknown_fields)]
pub enum RateCurve {
/// Measured at each concurrency (the decode knee's points); the harness interpolates.
Points { points: Vec<RatePoint> },
/// A fitted `scale x n^exponent` tokens per second, as the flow sim's M5 constants are
/// (decode `30.5 x n^0.865`, prefill `304 x n^-0.5`, 2026-10-28). The fit's error is the
/// scale's spread; its points are not kept.
PowerLaw { scale: Measured<f64>, exponent: f64 },
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct RatePoint {
pub concurrency: u32,
pub tokens_per_s: Measured<f64>,
}

/// What a move between tiers costs.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct PageCost {
pub from: Tier,
pub to: Tier,
pub bytes_per_s: Measured<f64>,
}

/// One model as served on this node.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ModelProfile {
pub model: ModelId,
pub weight_bytes: Measured<u64>,
/// KV plus recurrent state per token (the engine's `memory_breakdown`).
pub state_bytes_per_token: Measured<u64>,
pub prefill: RateCurve,
pub decode: RateCurve,
pub page: Vec<PageCost>,
/// One context checkpoint's bytes (`/slots` `checkpoints.max_bytes`, fork #91).
pub checkpoint_bytes: Measured<u64>,
pub slots: Measured<SlotGeometry>,
}

/// A node's measured world (THE-PROVING-GROUND 2, 8a).
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct NodeProfile {
/// The node's name on the grid (`m5`, `intelmac`, `bigmama-5090`), or a declared class
/// (`class/mac-unified-16g`) for a machine we do not have.
pub node: Slug,
pub memory: Vec<TierCapacity>,
pub cpu_cores: Measured<u32>,
pub models: Vec<ModelProfile>,
}

#[cfg(test)]
mod tests {
use super::*;

// what this catches: a declared shape for a machine we do not have reading as measured, so a
// sweep over the hardware classes would report another node's numbers as that class's.
#[test]
fn a_declared_class_is_unmeasured_on_the_wire() {
let declared = NodeProfile {
node: Slug::try_from("class/mac-unified-16g".to_string()).expect("slug"),
memory: vec![TierCapacity { tier: Tier::Unified, bytes: Measured::Unmeasured {} }],
cpu_cores: Measured::Unmeasured {},
models: vec![],
};
let wire = serde_json::to_value(&declared).expect("ser");
assert_eq!(wire["memory"][0]["bytes"], serde_json::json!({ "state": "unmeasured" }));
assert_eq!(serde_json::from_value::<NodeProfile>(wire).expect("de"), declared);
}
}
Loading
Loading