From 27e5cd503e7760f14ecad7b4c1e8bd722c4ecaaa Mon Sep 17 00:00:00 2001 From: rUv Date: Wed, 11 Nov 2026 18:06:37 -0500 Subject: [PATCH 01/10] feat: add bounded Rust Safetensors WSP checkpoint experiments --- .github/workflows/tensor-wsp.yml | 25 ++++ crates/wsp-tensor/.gitignore | 4 + crates/wsp-tensor/Cargo.toml | 20 +++ crates/wsp-tensor/README.md | 123 +++++++++++++++++ crates/wsp-tensor/scripts/benchmark.py | 24 ++++ crates/wsp-tensor/scripts/e2e.py | 38 +++++ crates/wsp-tensor/scripts/pan2_compare.py | 64 +++++++++ crates/wsp-tensor/src/lib.rs | 161 ++++++++++++++++++++++ crates/wsp-tensor/src/main.rs | 13 ++ crates/wsp-tensor/tests/checkpoints.rs | 58 ++++++++ 10 files changed, 530 insertions(+) create mode 100644 .github/workflows/tensor-wsp.yml create mode 100644 crates/wsp-tensor/.gitignore create mode 100644 crates/wsp-tensor/Cargo.toml create mode 100644 crates/wsp-tensor/README.md create mode 100644 crates/wsp-tensor/scripts/benchmark.py create mode 100644 crates/wsp-tensor/scripts/e2e.py create mode 100644 crates/wsp-tensor/scripts/pan2_compare.py create mode 100644 crates/wsp-tensor/src/lib.rs create mode 100644 crates/wsp-tensor/src/main.rs create mode 100644 crates/wsp-tensor/tests/checkpoints.rs diff --git a/.github/workflows/tensor-wsp.yml b/.github/workflows/tensor-wsp.yml new file mode 100644 index 00000000..77c9f4b5 --- /dev/null +++ b/.github/workflows/tensor-wsp.yml @@ -0,0 +1,25 @@ +name: Tensor WSP checkpoint validation +on: + pull_request: + paths: ['crates/wsp-tensor/**', '.github/workflows/tensor-wsp.yml'] + push: + branches: [main] + paths: ['crates/wsp-tensor/**', '.github/workflows/tensor-wsp.yml'] +permissions: + contents: read +jobs: + checkpoint-validation: + runs-on: ubuntu-latest + defaults: + run: + working-directory: crates/wsp-tensor + steps: + - uses: actions/checkout@v4 + - uses: dtolnay/rust-toolchain@stable + with: + components: rustfmt, clippy + - run: cargo fmt --check + - run: cargo clippy --locked --all-targets -- -D warnings + - run: cargo test --locked + - run: cargo build --release --locked + - run: python3 scripts/e2e.py diff --git a/crates/wsp-tensor/.gitignore b/crates/wsp-tensor/.gitignore new file mode 100644 index 00000000..dae5e698 --- /dev/null +++ b/crates/wsp-tensor/.gitignore @@ -0,0 +1,4 @@ +/target/ +__pycache__/ +*.safetensors +*.log diff --git a/crates/wsp-tensor/Cargo.toml b/crates/wsp-tensor/Cargo.toml new file mode 100644 index 00000000..4ba7089a --- /dev/null +++ b/crates/wsp-tensor/Cargo.toml @@ -0,0 +1,20 @@ +[package] +name = "ruv-fann-wsp-tensor" +version = "0.1.0" +edition = "2021" +license = "MIT OR Apache-2.0" +publish = false +description = "Bounded, streaming Safetensors checkpoint edits for WSP experiments" + +[workspace] + +[dependencies] +serde = { version = "1", features = ["derive"] } +serde_json = "1" +sha2 = "0.10" +half = "2" +tempfile = "3" + +[dev-dependencies] +safetensors = "0.4.5" + diff --git a/crates/wsp-tensor/README.md b/crates/wsp-tensor/README.md new file mode 100644 index 00000000..6e312318 --- /dev/null +++ b/crates/wsp-tensor/README.md @@ -0,0 +1,123 @@ +# Rust WSP tensor checkpoint experiments + +Standalone, unpublished backend for bounded direct edits to Safetensors shards. +This extends the WSP workflow concept. It does not replace or import the small +dense model schema in `ruv-fann-wsp`, and does not implement PAN2 inference. + +## Build and verify + +```sh +cargo test --locked +cargo build --release --locked +python3 scripts/e2e.py +python3 scripts/benchmark.py +``` + +The integration tests use the official Rust Safetensors reader on the generated +candidates. Python independently generates and decodes a BF16 fixture and drives +the compiled CLI through inspect, edit, rejection and byte-exact rollback. + +## Inspect, edit, rollback + +```sh +target/release/ruv-fann-wsp-tensor inspect original.safetensors +target/release/ruv-fann-wsp-tensor apply original.safetensors candidate.safetensors patch.json > receipt.json +target/release/ruv-fann-wsp-tensor rollback candidate.safetensors restored.safetensors receipt.json +cmp original.safetensors restored.safetensors +``` + +Patch format, using the exact shard SHA256 from inspect: + +```json +{ + "schema": "ruv-fann.tensor-patch/1", + "base_sha256": "REPLACE_WITH_EXACT_SHARD_SHA256", + "edits": [{"tensor": "REPLACE_WITH_EXACT_TENSOR_NAME", "index": 0, "value": 0.75}] +} +``` + +Indices are flattened row-major tensor positions. Supported edits: F32, F16, +BF16. Other supported integer and F64 tensors remain byte-identical. No topology, +tensor shape, tensor name or metadata changes. At most 128 unique replacements; +each finite replacement must have absolute value at most 1,000,000 and actual +encoded change at most 1. A change that rounds to the same bytes rejects. +These are resource and mutation bounds, not evidence of behavioral safety. + +Every operation writes a private temporary copy in the destination directory. +It validates the hash and structure of that copy before editing it. Persistence +uses an atomic no-clobber operation; original files and existing outputs are +never overwritten. Rollback verifies candidate hash, edited bytes and restored +baseline hash. A receipt records `behavioral_validation: not_run`. + +Keep stdout receipts with their checkpoints; if output capture fails, the +candidate may exist without a receipt. The original is still intact. Hashes +provide content integrity, not authorization or trusted evaluation signatures. + +## PAN2 paired inference + +Use a separately provisioned Linux CUDA executor with the official PAN2 +environment and dependencies already installed. No GPU is provisioned by this +project. The script runs the official launcher in its `pan` Conda environment. +It requires a clean upstream checkout pinned to +`0fb907b6e16a82ead6d691d1de2eef6299c71e9a`. + +Prepare complete native-format transformer directories for baseline and +candidate. Convert the released Diffusers weights with upstream's +`scripts/convert_checkpoint.py` first. Apply the Rust patch to a selected native +Safetensors shard. Keep configuration and all other shards identical. Check +the index file if the checkpoint layout uses one; this CLI edits one shard and +does not manage shard indexes or full checkpoint directories. + +```sh +python3 scripts/pan2_compare.py \ + --repo /models/PAN2 \ + --baseline /models/base/transformer \ + --candidate /models/candidate/transformer \ + --prompts /evaluation/prompts.json \ + --output /evaluation/pan2-comparison +``` + +The harness records checkpoint and output hashes, prompt hash, fixed seed, +upstream revision and timings. It uses fresh output directories, checks inputs +again after inference, and terminates the process group on timeout or error. +This is a paired generation harness, not a validated physics evaluator. +Prompt-referenced images and external text encoder/VAE caches must be separately +pinned and kept immutable by the operator. Those dependencies are not hashed by +this initial harness. Review upstream licensing for each component. + +No automatic promotion is implemented. Score action adherence and physical +consistency against real observations on an unseen scenario set before choosing +to deploy a candidate. Generating plausible video does not prove correctness. + +## Performance and deployment + +No full-shard allocation or mmap is used. Header length is capped at 16 MiB; +JSON expansion is additional memory. Payload hashing uses a 1 MiB buffer. Copy +uses the operating system's optimized streaming copy where available. Cost is +linear in shard size and requires an additional shard's worth of disk space. +Use local NVMe for candidate creation; keep the large model off Workers. + +Cloudflare R2 stores small validation receipts in a private bucket. No public +endpoint, model upload or remote execution API is required. Use your existing +authenticated Cloudflare tooling to upload reports; no credentials are included +here. Production checkpoint transfer should use resumable multipart upload and +verify SHA256 after download. This implementation does not automate that upload. + +## Threat model and limits + +Untrusted patch JSON and malformed checkpoint headers are validated. JSON input +is limited to 1 MiB, header to 16 MiB, rank to 32, dimensions checked for overflow, +and offsets checked for contiguous coverage. Checkpoints are local trusted-user +files, not a public file upload service. Use filesystem permissions and a job +sandbox before exposing this CLI to other tenants. Filesystem failures, process +termination and unavailable disk space can interrupt a run; the source remains +untouched. Receipts are unsigned. Dependency advisory scanning is a separate +release check. This crate is `publish = false` until review and GPU validation. + +Acceptance: candidates load with the official reader; unchanged tensors retain +their bytes; invalid and stale edits reject; rollback restores the entire original +file hash. Full PAN2 quality and speed improvements remain unverified. + +Sources: [PAN2](https://github.com/ifm-ai/PAN2), +[WSP](https://github.com/ruvnet/ruv-FANN/tree/main/packages/wsp), +[Cloudflare R2](https://developers.cloudflare.com/r2/objects/upload-objects/). diff --git a/crates/wsp-tensor/scripts/benchmark.py b/crates/wsp-tensor/scripts/benchmark.py new file mode 100644 index 00000000..b37fcf47 --- /dev/null +++ b/crates/wsp-tensor/scripts/benchmark.py @@ -0,0 +1,24 @@ +"""Physical I/O checkpoint benchmark, not neural inference throughput.""" +import hashlib, json, pathlib, resource, struct, subprocess, tempfile, time +ROOT = pathlib.Path(__file__).resolve().parents[1] +BIN = ROOT / "target/release/ruv-fann-wsp-tensor" +rows = [] +for mib in (16, 128): + with tempfile.TemporaryDirectory() as directory: + d = pathlib.Path(directory) + base, out = d / "base", d / "candidate" + size = mib * 1024 * 1024 + h = json.dumps({"weight":{"dtype":"BF16","shape":[2],"data_offsets":[0,4]}, "padding":{"dtype":"U8","shape":[size],"data_offsets":[4,size+4]}}).encode() + h += b" " * (-len(h)%8) + with base.open("wb") as f: + f.write(struct.pack(" = std::result::Result>; +const HEADER_LIMIT: u64 = 16 * 1024 * 1024; +const BUFFER: usize = 1024 * 1024; + +#[derive(Debug, Deserialize, Serialize)] +#[serde(deny_unknown_fields)] +pub struct Edit { pub tensor: String, pub index: u64, pub value: f32 } +#[derive(Debug, Deserialize, Serialize)] +#[serde(deny_unknown_fields)] +pub struct Patch { pub schema: String, pub base_sha256: String, pub edits: Vec } +#[derive(Debug, Deserialize, Serialize)] +#[serde(deny_unknown_fields)] +pub struct Undo { pub offset: u64, pub before: Vec, pub after: Vec } +#[derive(Debug, Deserialize, Serialize)] +#[serde(deny_unknown_fields)] +pub struct Receipt { + pub schema: String, pub base_sha256: String, pub candidate_sha256: String, + pub edits: Vec, pub behavioral_validation: String, +} +#[derive(Debug, Deserialize, Serialize)] +pub struct Tensor { pub dtype: String, pub shape: Vec, pub data_offsets: [u64; 2] } +#[derive(Debug, Serialize)] +pub struct Inspection { pub data_start: u64, pub bytes: u64, pub tensors: BTreeMap } + +fn fail(s: &str) -> Result { Err(s.into()) } +fn width(s: &str) -> Result { + match s { "BOOL"|"U8"|"I8"=>Ok(1), "F16"|"BF16"|"U16"|"I16"=>Ok(2), + "F32"|"U32"|"I32"=>Ok(4), "F64"|"U64"|"I64"=>Ok(8), _=>fail("unsupported dtype") } +} +pub fn inspect(f: &mut File) -> Result { + f.rewind()?; + let bytes=f.metadata()?.len(); + let mut len=[0;8]; f.read_exact(&mut len)?; + let n=u64::from_le_bytes(len); + if n==0 || n>HEADER_LIMIT || n%8!=0 || n.checked_add(8).filter(|v| *v<=bytes).is_none() { return fail("invalid header length"); } + let mut raw=vec![0;n as usize]; f.read_exact(&mut raw)?; + // Streaming validation avoids mapping an entire multi-GB shard into memory. + let object: UniqueHeader=serde_json::from_slice(&raw)?; + let mut tensors=BTreeMap::new(); + for (k,v) in object.0 { + if k=="__metadata__" {let _:BTreeMap=serde_json::from_value(v)?;continue;} + tensors.insert(k,serde_json::from_value::(v)?); + } + let mut ordered:Vec<_>=tensors.values().collect(); + ordered.sort_by_key(|t|(t.data_offsets[0],t.data_offsets[1])); + let mut cursor=0; + for t in ordered { + if t.shape.len()>32 {return fail("tensor rank exceeds 32");} + let count=t.shape.iter().try_fold(1u64,|a,b|a.checked_mul(*b)).ok_or("shape overflow")?; + let size=count.checked_mul(width(&t.dtype)?).ok_or("tensor size overflow")?; + if t.data_offsets[0]!=cursor || t.data_offsets[1].checked_sub(cursor)!=Some(size) {return fail("invalid tensor offsets or shape");} + cursor=t.data_offsets[1]; + } + if cursor!=bytes-n-8 {return fail("truncated or trailing tensor data");} + Ok(Inspection{data_start:n+8,bytes,tensors}) +} +struct UniqueHeader(BTreeMap); +impl<'de> Deserialize<'de> for UniqueHeader { + fn deserialize>(d:D)->std::result::Result{ + struct V; + impl<'de> serde::de::Visitor<'de> for V { + type Value=UniqueHeader; + fn expecting(&self,f:&mut std::fmt::Formatter)->std::fmt::Result{f.write_str("unique tensor names")} + fn visit_map>(self,mut m:M)->std::result::Result{ + let mut out=BTreeMap::new(); + while let Some((k,v))=m.next_entry::()? { + if out.insert(k,v).is_some(){return Err(serde::de::Error::custom("duplicate tensor name"));} + } + Ok(UniqueHeader(out)) + } + } + d.deserialize_map(V) + } +} +fn hash(f: &mut File) -> Result { + f.rewind()?; + let mut h=Sha256::new(); let mut buf=vec![0;BUFFER]; + loop { let n=f.read(&mut buf)?; if n==0 {break;} h.update(&buf[..n]); } + Ok(format!("{:x}",h.finalize())) +} +pub fn fingerprint(path: &Path) -> Result { hash(&mut File::open(path)?) } +fn decode(dtype: &str,b: &[u8]) -> Result { + Ok(match dtype { "F32"=>f32::from_le_bytes(b.try_into()?), + "F16"=>f16::from_le_bytes(b.try_into()?).to_f32(), "BF16"=>bf16::from_le_bytes(b.try_into()?).to_f32(), _=>return fail("editable dtypes: F32 F16 BF16") }) +} +fn encode(dtype: &str,v: f32) -> Result> { + let b=match dtype { "F32"=>v.to_le_bytes().to_vec(), "F16"=>f16::from_f32(v).to_le_bytes().to_vec(), + "BF16"=>bf16::from_f32(v).to_le_bytes().to_vec(), _=>return fail("unsupported edit dtype") }; + if !decode(dtype,&b)?.is_finite() {return fail("nonfinite encoded value");} Ok(b) +} +fn copy_snapshot(source: &Path,dest: &Path) -> Result<(tempfile::NamedTempFile,String)> { + let parent=dest.parent().filter(|p| !p.as_os_str().is_empty()).unwrap_or(Path::new(".")); + if dest.exists() {return fail("destination already exists");} + let mut temp=tempfile::NamedTempFile::new_in(parent)?; + let mut f=File::open(source)?; + let mut h=Sha256::new();let mut buf=vec![0;BUFFER]; + loop {let n=f.read(&mut buf)?;if n==0 {break;}h.update(&buf[..n]);temp.write_all(&buf[..n])?;} + Ok((temp,format!("{:x}",h.finalize()))) +} +fn commit(mut temp: tempfile::NamedTempFile,dest: &Path) -> Result<()> { + temp.flush()?;temp.as_file().sync_all()?; + temp.persist_noclobber(dest).map_err(|e| e.error)?; + #[cfg(unix)] File::open(dest.parent().filter(|p| !p.as_os_str().is_empty()).unwrap_or(Path::new(".")))?.sync_all()?; + Ok(()) +} +pub fn apply(source: &Path,dest: &Path,p: &Patch) -> Result { + if p.schema!="ruv-fann.tensor-patch/1" || p.edits.is_empty() || p.edits.len()>128 {return fail("invalid patch schema or edit count");} + // Work only on a private copy. Hash and validate the same bytes that we edit. + let (mut temp,base)=copy_snapshot(source,dest)?; + let f=temp.as_file_mut(); + if base!=p.base_sha256 {return fail("baseline hash mismatch");} + let info=inspect(f)?; let mut seen=BTreeSet::new();let mut edits=Vec::new(); + for e in &p.edits { + if !seen.insert((&e.tensor,e.index)) {return fail("duplicate edit");} + if !e.value.is_finite() || e.value.abs()>1_000_000.0 {return fail("invalid replacement");} + let t=info.tensors.get(&e.tensor).ok_or("unknown tensor")?; + let w=width(&t.dtype)?; + let count=(t.data_offsets[1]-t.data_offsets[0])/w; + if e.index>=count {return fail("index out of range");} + let offset=info.data_start+t.data_offsets[0]+e.index*w; + f.seek(SeekFrom::Start(offset))?; let mut before=vec![0;w as usize]; f.read_exact(&mut before)?; + let old=decode(&t.dtype,&before)?; + let after=encode(&t.dtype,e.value)?; let actual=decode(&t.dtype,&after)?; + if !old.is_finite() || (actual-old).abs()>1.0 {return fail("nonfinite baseline or delta exceeds 1");} + if after==before {return fail("edit is a no-op after dtype rounding");} + edits.push(Undo{offset,before,after}); + } + for e in &edits { f.seek(SeekFrom::Start(e.offset))?;f.write_all(&e.after)?; } + inspect(f)?; + let candidate=hash(f)?; + commit(temp,dest)?; + Ok(Receipt{schema:"ruv-fann.tensor-receipt/1".into(),base_sha256:base,candidate_sha256:candidate, + edits,behavioral_validation:"not_run".into()}) +} +pub fn rollback(source: &Path,dest: &Path,r: &Receipt) -> Result<()> { + if r.schema!="ruv-fann.tensor-receipt/1" || r.edits.is_empty() || r.edits.len()>128 {return fail("invalid receipt");} + let (mut temp,candidate_hash)=copy_snapshot(source,dest)?;let f=temp.as_file_mut(); + if candidate_hash!=r.candidate_sha256 {return fail("candidate hash mismatch");} + let info=inspect(f)?;let mut ranges=BTreeSet::new(); + for e in &r.edits { + let n=e.before.len(); + if ![2,4].contains(&n) || n!=e.after.len() || e.offset(path: &Path) -> Result { + let mut bytes=Vec::new(); File::open(path)?.take(1024*1024+1).read_to_end(&mut bytes)?; + if bytes.len()>1024*1024 {return fail("JSON exceeds 1 MiB");} + Ok(serde_json::from_slice(&bytes)?) +} diff --git a/crates/wsp-tensor/src/main.rs b/crates/wsp-tensor/src/main.rs new file mode 100644 index 00000000..56326b3c --- /dev/null +++ b/crates/wsp-tensor/src/main.rs @@ -0,0 +1,13 @@ +use ruv_fann_wsp_tensor::{apply,fingerprint,inspect,read_json,rollback,Result}; +use std::{fs::File,path::Path}; +fn run()->Result<()> { + let a:Vec=std::env::args().collect(); + let result=match a.get(1).map(String::as_str) { + Some("inspect") if a.len()==3=>serde_json::json!({"checkpoint":inspect(&mut File::open(&a[2])?)?,"sha256":fingerprint(Path::new(&a[2]))?}), + Some("apply") if a.len()==5=>serde_json::to_value(apply(Path::new(&a[2]),Path::new(&a[3]),&read_json(Path::new(&a[4]))?)?)?, + Some("rollback") if a.len()==5=>{rollback(Path::new(&a[2]),Path::new(&a[3]),&read_json(Path::new(&a[4]))?)?;serde_json::json!({"restored":fingerprint(Path::new(&a[3]))?})}, + _=>return Err("usage: ruv-fann-wsp-tensor inspect FILE | apply BASE OUTPUT PATCH.json | rollback CANDIDATE OUTPUT RECEIPT.json".into()) + }; + println!("{}",serde_json::to_string_pretty(&result)?);Ok(()) +} +fn main(){if let Err(e)=run(){eprintln!("{e}");std::process::exit(1);}} diff --git a/crates/wsp-tensor/tests/checkpoints.rs b/crates/wsp-tensor/tests/checkpoints.rs new file mode 100644 index 00000000..d5bcfd02 --- /dev/null +++ b/crates/wsp-tensor/tests/checkpoints.rs @@ -0,0 +1,58 @@ +use ruv_fann_wsp_tensor::*; +use std::{fs::{self,File},path::Path}; +fn fixture(path:&Path,dtype:&str) { + let bytes=match dtype {"F32"=>[0.25f32,0.5].into_iter().flat_map(f32::to_le_bytes).collect::>(), + "BF16"=>[0.25f32,0.5].into_iter().flat_map(|v|half::bf16::from_f32(v).to_le_bytes()).collect(), + _=>[0.25f32,0.5].into_iter().flat_map(|v|half::f16::from_f32(v).to_le_bytes()).collect()}; + write_raw(path,&format!(r#"{{"weight":{{"dtype":"{dtype}","shape":[2],"data_offsets":[0,{}]}}}}"#,bytes.len()),&bytes); +} +fn write_raw(path:&Path,header:&str,data:&[u8]) { + let mut header=header.as_bytes().to_vec();while header.len() & 7 != 0 {header.push(b' ');} + let mut bytes=(header.len() as u64).to_le_bytes().to_vec();bytes.extend(header);bytes.extend(data);fs::write(path,bytes).unwrap(); +} +fn patch(path:&Path)->Patch {Patch{schema:"ruv-fann.tensor-patch/1".into(),base_sha256:fingerprint(path).unwrap(),edits:vec![Edit{tensor:"weight".into(),index:0,value:0.75}]}} +#[test] fn all_precisions_roundtrip_and_official_reader() { + for dtype in ["F32","BF16","F16"] { + let d=tempfile::tempdir().unwrap();let b=d.path().join("base");let c=d.path().join("candidate");let restored=d.path().join("restored"); + fixture(&b,dtype);let before=fs::read(&b).unwrap();let r=apply(&b,&c,&patch(&b)).unwrap(); + let bytes=fs::read(&c).unwrap();let parsed=safetensors::SafeTensors::deserialize(&bytes).unwrap();assert_eq!(parsed.tensor("weight").unwrap().shape(),&[2]); + assert_ne!(r.base_sha256,r.candidate_sha256);assert_eq!(r.behavioral_validation,"not_run"); + rollback(&c,&restored,&r).unwrap();assert_eq!(fs::read(restored).unwrap(),before);assert_eq!(fs::read(&b).unwrap(),before); + } +} +#[test] fn reject_patch_abuse_without_outputs() { + for case in 0..10 { + let d=tempfile::tempdir().unwrap();let b=d.path().join("b");let out=d.path().join("out");fixture(&b,"F32");let mut p=patch(&b); + match case {0=>p.base_sha256="0".repeat(64),1=>p.edits[0].tensor="missing".into(),2=>p.edits[0].index=u64::MAX, + 3=>p.edits[0].value=f32::NAN,4=>p.edits[0].value=2.0,5=>p.edits.push(Edit{tensor:"weight".into(),index:0,value:0.4}), + 6=>p.edits.clear(),7=>p.edits[0].value=0.25,8=>p.schema="bad".into(),_=>p.edits=(0..129).map(|_|Edit{tensor:"weight".into(),index:0,value:0.5}).collect()} + assert!(apply(&b,&out,&p).is_err(),"case {case}");assert!(!out.exists()); + assert_eq!(fs::read_dir(d.path()).unwrap().count(),1); + } +} +#[test] fn reject_malformed_headers() { + for h in [r#"{"w":{"dtype":"F32","shape":[2],"data_offsets":[0,4]}}"#, + r#"{"w":{"dtype":"F32","shape":[1],"data_offsets":[4,8]}}"#, + r#"{"w":{"dtype":"F32","shape":[18446744073709551615,2],"data_offsets":[0,4]}}"#, + r#"{"w":{"dtype":"F32","shape":[1],"data_offsets":[0,4]},"w":{"dtype":"F32","shape":[1],"data_offsets":[0,4]}}"#, + r#"{"w":{"dtype":"UNKNOWN","shape":[1],"data_offsets":[0,4]}}"#] { + let d=tempfile::tempdir().unwrap();let b=d.path().join("b");write_raw(&b,h,&[0;4]);assert!(inspect(&mut File::open(b).unwrap()).is_err()); + } +} +#[test] fn refuse_overwrite_and_forged_rollback() { + let d=tempfile::tempdir().unwrap();let b=d.path().join("b");let c=d.path().join("c");let o=d.path().join("o");fixture(&b,"BF16"); + assert!(apply(&b,&b,&patch(&b)).is_err());let mut r=apply(&b,&c,&patch(&b)).unwrap(); + assert!(apply(&b,&c,&patch(&b)).is_err());r.edits[0].before[0]^=1; + assert!(rollback(&c,&o,&r).is_err());assert!(!o.exists()); + r.candidate_sha256="f".repeat(64);assert!(rollback(&c,&o,&r).is_err()); +} +#[test] fn dtype_rounding_is_not_an_edit() { + let d=tempfile::tempdir().unwrap();let b=d.path().join("b");fixture(&b,"BF16");let mut p=patch(&b);p.edits[0].value=f32::from_bits(0.25f32.to_bits()+1); + assert!(apply(&b,&d.path().join("c"),&p).is_err()); +} +#[test] fn preserve_untouched_tensor_and_metadata() { + let d=tempfile::tempdir().unwrap();let b=d.path().join("b");let c=d.path().join("c"); + write_raw(&b,r#"{"__metadata__":{"origin":"fixture"},"weight":{"dtype":"F32","shape":[1],"data_offsets":[0,4]},"other":{"dtype":"F32","shape":[1],"data_offsets":[4,8]}}"#,&[0.25f32,42.0].into_iter().flat_map(f32::to_le_bytes).collect::>()); + apply(&b,&c,&patch(&b)).unwrap();let old=fs::read(b).unwrap();let new=fs::read(c).unwrap(); + assert_eq!(&old[..old.len()-8],&new[..new.len()-8]);assert_eq!(&old[old.len()-4..],&new[new.len()-4..]); +} From 812695581339bfae5b7e615d0866fa855b2f26c1 Mon Sep 17 00:00:00 2001 From: rUv Date: Wed, 11 Nov 2026 18:08:26 -0500 Subject: [PATCH 02/10] test: validate checkpoint round trips and optimize streaming baseline hashing --- crates/wsp-tensor/Cargo.lock | 360 ++++++++++++++++ crates/wsp-tensor/README.md | 3 +- crates/wsp-tensor/scripts/benchmark.py | 4 +- crates/wsp-tensor/scripts/pan2_compare.py | 14 +- crates/wsp-tensor/src/lib.rs | 386 +++++++++++++----- crates/wsp-tensor/src/main.rs | 18 +- crates/wsp-tensor/tests/checkpoints.rs | 200 ++++++--- .../validation/benchmark-report.json | 17 + crates/wsp-tensor/validation/e2e-report.json | 11 + 9 files changed, 847 insertions(+), 166 deletions(-) create mode 100644 crates/wsp-tensor/Cargo.lock create mode 100644 crates/wsp-tensor/validation/benchmark-report.json create mode 100644 crates/wsp-tensor/validation/e2e-report.json diff --git a/crates/wsp-tensor/Cargo.lock b/crates/wsp-tensor/Cargo.lock new file mode 100644 index 00000000..78e8634b --- /dev/null +++ b/crates/wsp-tensor/Cargo.lock @@ -0,0 +1,360 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "bitflags" +version = "2.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ded4057c258ba199e2d26386d3af3780957ecaee6c4ef4041c6b4b8b97c0b06" + +[[package]] +name = "block-buffer" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" +dependencies = [ + "generic-array", +] + +[[package]] +name = "cfg-if" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e7648175b45a9a48536d676f68d918270699102aa8dab5496df06904c914600" + +[[package]] +name = "cpufeatures" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59ed5838eebb26a2bb2e58f6d5b5316989ae9d08bab10e0e6d103e656d1b0280" +dependencies = [ + "libc", +] + +[[package]] +name = "crunchy" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "460fbee9c2c2f33933d720630a6a0bac33ba7053db5344fac858d4b8952d77d5" + +[[package]] +name = "crypto-common" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78c8292055d1c1df0cce5d180393dc8cce0abec0a7102adb6c7b1eef6016d60a" +dependencies = [ + "generic-array", + "typenum", +] + +[[package]] +name = "digest" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" +dependencies = [ + "block-buffer", + "crypto-common", +] + +[[package]] +name = "errno" +version = "0.3.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" +dependencies = [ + "libc", + "windows-sys", +] + +[[package]] +name = "fastrand" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be" + +[[package]] +name = "generic-array" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" +dependencies = [ + "typenum", + "version_check", +] + +[[package]] +name = "getrandom" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "899def5c37c4fd7b2664648c28120ecec138e4d395b459e5ca34f9cce2dd77fd" +dependencies = [ + "cfg-if", + "libc", + "r-efi", + "wasip2", +] + +[[package]] +name = "half" +version = "2.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ea2d84b969582b4b1864a92dc5d27cd2b77b622a8d79306834f1be5ba20d84b" +dependencies = [ + "cfg-if", + "crunchy", + "zerocopy", +] + +[[package]] +name = "itoa" +version = "1.0.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3bd1574e617a9a6c1f8f8181bd7e841e1f20e4277607ef28945765188d6284d4" + +[[package]] +name = "libc" +version = "0.2.190" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce5d3ddc6d3fa000eb1536d85e147bfe31aacaba692ed6a876f95cb7c855be78" + +[[package]] +name = "linux-raw-sys" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df1d3c3b53da64cf5760482273a98e575c651a67eec7f77df96b5b642de8f039" + +[[package]] +name = "memchr" +version = "2.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98" + +[[package]] +name = "once_cell" +version = "1.21.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" + +[[package]] +name = "proc-macro2" +version = "1.0.107" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.47" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "r-efi" +version = "5.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f" + +[[package]] +name = "rustix" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd15f8a2c5551a84d56efdc1cd049089e409ac19a3072d5037a17fd70719ff3e" +dependencies = [ + "bitflags", + "errno", + "libc", + "linux-raw-sys", + "windows-sys", +] + +[[package]] +name = "ruv-fann-wsp-tensor" +version = "0.1.0" +dependencies = [ + "half", + "safetensors", + "serde", + "serde_json", + "sha2", + "tempfile", +] + +[[package]] +name = "safetensors" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44560c11236a6130a46ce36c836a62936dc81ebf8c36a37947423571be0e55b6" +dependencies = [ + "serde", + "serde_json", +] + +[[package]] +name = "serde" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4148590afebada386688f18773da617792bf2ef03ffc1e4cbd2b1d45b023e0ba" +dependencies = [ + "serde_core", + "serde_derive", +] + +[[package]] +name = "serde_core" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67dca2c9c51e58a4791a4b1ed58308b39c64224d349a935ab5039aa360942a48" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7a5d71263a5a7d47b41f6b3f06ba276f10cc18b0931f1799f710578e2309348" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.9", +] + +[[package]] +name = "serde_json" +version = "1.0.154" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7e9cc8b1b85264074fbcc02a88680c4096b1e47df8f739dceb03bf482f04bd6" +dependencies = [ + "itoa", + "memchr", + "serde", + "serde_core", + "zmij", +] + +[[package]] +name = "sha2" +version = "0.10.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7507d819769d01a365ab707794a4084392c824f54a7a6a7862f8c3d0892b283" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + +[[package]] +name = "syn" +version = "2.0.119" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "872831b642d1a07999a962a351ed35b955ea2cfc8f3862091e2a240a84f17297" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "syn" +version = "3.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d78c8dee4c7bf0e14673097256fed6142ce9d3b85a408189d07482442145823b" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "tempfile" +version = "3.23.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2d31c77bdf42a745371d260a26ca7163f1e0924b64afa0b688e61b5a9fa02f16" +dependencies = [ + "fastrand", + "getrandom", + "once_cell", + "rustix", + "windows-sys", +] + +[[package]] +name = "typenum" +version = "1.20.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6f5e870be6c3b371b77fe0ee0bafb859fa4964b4404c27de1d380043c4dda20" + +[[package]] +name = "unicode-ident" +version = "1.0.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2c754d6c33795a1c324727428e5a7dedb5b06195f9890bdbcba760d3e246563" + +[[package]] +name = "version_check" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" + +[[package]] +name = "wasip2" +version = "1.0.4+wasi-0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b67efb37e106e55ce722a510d6b5f9c17f083e5fc79afc2badeb12cc313d9487" +dependencies = [ + "wit-bindgen", +] + +[[package]] +name = "windows-link" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" + +[[package]] +name = "windows-sys" +version = "0.61.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" +dependencies = [ + "windows-link", +] + +[[package]] +name = "wit-bindgen" +version = "0.57.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ebf944e87a7c253233ad6766e082e3cd714b5d03812acc24c318f549614536e" + +[[package]] +name = "zerocopy" +version = "0.8.63" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5fe1f8f1b06191a00962174c61aa5005e0bb391a6d80d07e24d115c01a92ed8" +dependencies = [ + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.8.63" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "863ad3ac83293fb4d740aedbfdc9240dd8d1a50c1099acd76ce80ce7c7230c7f" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "zmij" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29666d0abbfad1e3dc4dcf6144730dd3a3ab225bbbdac83319345b1b44ccfc1b" diff --git a/crates/wsp-tensor/README.md b/crates/wsp-tensor/README.md index 6e312318..bc9365db 100644 --- a/crates/wsp-tensor/README.md +++ b/crates/wsp-tensor/README.md @@ -73,6 +73,7 @@ python3 scripts/pan2_compare.py \ --repo /models/PAN2 \ --baseline /models/base/transformer \ --candidate /models/candidate/transformer \ + --receipt /evaluation/receipt.json \ --prompts /evaluation/prompts.json \ --output /evaluation/pan2-comparison ``` @@ -93,7 +94,7 @@ to deploy a candidate. Generating plausible video does not prove correctness. No full-shard allocation or mmap is used. Header length is capped at 16 MiB; JSON expansion is additional memory. Payload hashing uses a 1 MiB buffer. Copy -uses the operating system's optimized streaming copy where available. Cost is +hashes the baseline in the same streaming pass to avoid rereading the shard. Cost is linear in shard size and requires an additional shard's worth of disk space. Use local NVMe for candidate creation; keep the large model off Workers. diff --git a/crates/wsp-tensor/scripts/benchmark.py b/crates/wsp-tensor/scripts/benchmark.py index b37fcf47..d5cec8c2 100644 --- a/crates/wsp-tensor/scripts/benchmark.py +++ b/crates/wsp-tensor/scripts/benchmark.py @@ -18,7 +18,7 @@ p = d / "patch.json" p.write_text(json.dumps({"schema":"ruv-fann.tensor-patch/1","base_sha256":digest,"edits":[{"tensor":"weight","index":0,"value":0.75}]})) start = time.perf_counter() - subprocess.run(["/usr/bin/time","-f","%M","-o",str(d/"rss"),str(BIN),"apply",str(base),str(out),str(p)],check=True,stdout=subprocess.DEVNULL) + subprocess.run([str(BIN),"apply",str(base),str(out),str(p)],check=True,stdout=subprocess.DEVNULL) elapsed = time.perf_counter()-start - rows.append({"payload_mib":mib,"seconds":round(elapsed,4),"effective_mib_per_sec":round(mib/elapsed,2),"child_peak_rss_kib":int((d/"rss").read_text().strip())}) + rows.append({"payload_mib":mib,"seconds":round(elapsed,4),"effective_mib_per_sec":round(mib/elapsed,2),"child_peak_rss_kib_including_spawn_overhead":resource.getrusage(resource.RUSAGE_CHILDREN).ru_maxrss}) print(json.dumps({"scope":"synthetic Safetensors file I/O; warm OS cache; single sample per size", "rows":rows},indent=2)) diff --git a/crates/wsp-tensor/scripts/pan2_compare.py b/crates/wsp-tensor/scripts/pan2_compare.py index 946ecb9e..62ba2e2a 100644 --- a/crates/wsp-tensor/scripts/pan2_compare.py +++ b/crates/wsp-tensor/scripts/pan2_compare.py @@ -16,12 +16,12 @@ def inventory(p): def main(): a = argparse.ArgumentParser() - for name in ("repo", "baseline", "candidate", "prompts", "output"): + for name in ("repo", "baseline", "candidate", "prompts", "output", "receipt"): a.add_argument("--"+name, required=True, type=pathlib.Path) a.add_argument("--seed",type=int,default=123) a.add_argument("--timeout",type=int,default=7200) args = a.parse_args() - for name in ("repo","baseline","candidate","prompts","output"): + for name in ("repo","baseline","candidate","prompts","output","receipt"): setattr(args,name,getattr(args,name).resolve()) if not shutil.which("nvidia-smi"): raise RuntimeError("CUDA executor unavailable: nvidia-smi missing") subprocess.run(["nvidia-smi"],check=True,stdout=subprocess.DEVNULL) @@ -31,7 +31,13 @@ def main(): raise RuntimeError("PAN2 checkout must be clean") if args.timeout<1 or args.baseline==args.candidate: raise RuntimeError("invalid timeout or identical checkpoint directories") before = {"baseline":inventory(args.baseline),"candidate":inventory(args.candidate)} - if before["baseline"]==before["candidate"]: raise RuntimeError("candidate has no weight differences") + if before["baseline"].keys()!=before["candidate"].keys(): raise RuntimeError("shard sets differ") + changed = [k for k in before["baseline"] if before["baseline"][k]!=before["candidate"][k]] + if len(changed)!=1: raise RuntimeError("exactly one edited shard is required") + receipt = json.loads(args.receipt.read_text()) + key = changed[0] + if receipt.get("schema")!="ruv-fann.tensor-receipt/1" or receipt.get("base_sha256")!=before["baseline"][key] or receipt.get("candidate_sha256")!=before["candidate"][key]: + raise RuntimeError("receipt does not match checkpoint pair") prompts_hash = digest(args.prompts) args.output.mkdir(parents=True,exist_ok=False) prompts = args.output / "prompts.json" @@ -57,7 +63,7 @@ def main(): runs[label] = {"seconds":time.perf_counter()-start,"videos":{str(p.relative_to(args.output/label)):digest(p) for p in videos}} after = {"baseline":inventory(args.baseline),"candidate":inventory(args.candidate)} if before!=after or digest(prompts)!=prompts_hash: raise RuntimeError("inputs changed during evaluation") - result = {"schema":"ruv-fann.pan2-paired-inference/1","pan2_commit":head,"seed":args.seed,"prompts_sha256":prompts_hash,"checkpoints":before,"runs":runs,"behavioral_validation":"requires independent scoring","promotion":"not_performed"} + result = {"schema":"ruv-fann.pan2-paired-inference/1","pan2_commit":head,"seed":args.seed,"prompts_sha256":prompts_hash,"receipt_sha256":digest(args.receipt),"checkpoints":before,"runs":runs,"behavioral_validation":"requires independent scoring","promotion":"not_performed"} (args.output/"comparison.json").write_text(json.dumps(result,indent=2)) print(json.dumps(result,indent=2)) diff --git a/crates/wsp-tensor/src/lib.rs b/crates/wsp-tensor/src/lib.rs index b5063575..abee5fed 100644 --- a/crates/wsp-tensor/src/lib.rs +++ b/crates/wsp-tensor/src/lib.rs @@ -2,7 +2,12 @@ use half::{bf16, f16}; use serde::{Deserialize, Serialize}; use sha2::{Digest, Sha256}; -use std::{collections::{BTreeMap, BTreeSet}, fs::File, io::{Read, Seek, SeekFrom, Write}, path::Path}; +use std::{ + collections::{BTreeMap, BTreeSet}, + fs::File, + io::{Read, Seek, SeekFrom, Write}, + path::Path, +}; pub type Result = std::result::Result>; const HEADER_LIMIT: u64 = 16 * 1024 * 1024; @@ -10,67 +15,131 @@ const BUFFER: usize = 1024 * 1024; #[derive(Debug, Deserialize, Serialize)] #[serde(deny_unknown_fields)] -pub struct Edit { pub tensor: String, pub index: u64, pub value: f32 } +pub struct Edit { + pub tensor: String, + pub index: u64, + pub value: f32, +} #[derive(Debug, Deserialize, Serialize)] #[serde(deny_unknown_fields)] -pub struct Patch { pub schema: String, pub base_sha256: String, pub edits: Vec } +pub struct Patch { + pub schema: String, + pub base_sha256: String, + pub edits: Vec, +} #[derive(Debug, Deserialize, Serialize)] #[serde(deny_unknown_fields)] -pub struct Undo { pub offset: u64, pub before: Vec, pub after: Vec } +pub struct Undo { + pub offset: u64, + pub before: Vec, + pub after: Vec, +} #[derive(Debug, Deserialize, Serialize)] #[serde(deny_unknown_fields)] pub struct Receipt { - pub schema: String, pub base_sha256: String, pub candidate_sha256: String, - pub edits: Vec, pub behavioral_validation: String, + pub schema: String, + pub base_sha256: String, + pub candidate_sha256: String, + pub edits: Vec, + pub behavioral_validation: String, } #[derive(Debug, Deserialize, Serialize)] -pub struct Tensor { pub dtype: String, pub shape: Vec, pub data_offsets: [u64; 2] } +pub struct Tensor { + pub dtype: String, + pub shape: Vec, + pub data_offsets: [u64; 2], +} #[derive(Debug, Serialize)] -pub struct Inspection { pub data_start: u64, pub bytes: u64, pub tensors: BTreeMap } +pub struct Inspection { + pub data_start: u64, + pub bytes: u64, + pub tensors: BTreeMap, +} -fn fail(s: &str) -> Result { Err(s.into()) } +fn fail(s: &str) -> Result { + Err(s.into()) +} fn width(s: &str) -> Result { - match s { "BOOL"|"U8"|"I8"=>Ok(1), "F16"|"BF16"|"U16"|"I16"=>Ok(2), - "F32"|"U32"|"I32"=>Ok(4), "F64"|"U64"|"I64"=>Ok(8), _=>fail("unsupported dtype") } + match s { + "BOOL" | "U8" | "I8" => Ok(1), + "F16" | "BF16" | "U16" | "I16" => Ok(2), + "F32" | "U32" | "I32" => Ok(4), + "F64" | "U64" | "I64" => Ok(8), + _ => fail("unsupported dtype"), + } } pub fn inspect(f: &mut File) -> Result { f.rewind()?; - let bytes=f.metadata()?.len(); - let mut len=[0;8]; f.read_exact(&mut len)?; - let n=u64::from_le_bytes(len); - if n==0 || n>HEADER_LIMIT || n%8!=0 || n.checked_add(8).filter(|v| *v<=bytes).is_none() { return fail("invalid header length"); } - let mut raw=vec![0;n as usize]; f.read_exact(&mut raw)?; + let bytes = f.metadata()?.len(); + let mut len = [0; 8]; + f.read_exact(&mut len)?; + let n = u64::from_le_bytes(len); + if n == 0 + || n > HEADER_LIMIT + || n % 8 != 0 + || n.checked_add(8).filter(|v| *v <= bytes).is_none() + { + return fail("invalid header length"); + } + let mut raw = vec![0; n as usize]; + f.read_exact(&mut raw)?; // Streaming validation avoids mapping an entire multi-GB shard into memory. - let object: UniqueHeader=serde_json::from_slice(&raw)?; - let mut tensors=BTreeMap::new(); - for (k,v) in object.0 { - if k=="__metadata__" {let _:BTreeMap=serde_json::from_value(v)?;continue;} - tensors.insert(k,serde_json::from_value::(v)?); - } - let mut ordered:Vec<_>=tensors.values().collect(); - ordered.sort_by_key(|t|(t.data_offsets[0],t.data_offsets[1])); - let mut cursor=0; + let object: UniqueHeader = serde_json::from_slice(&raw)?; + let mut tensors = BTreeMap::new(); + for (k, v) in object.0 { + if k == "__metadata__" { + let _: BTreeMap = serde_json::from_value(v)?; + continue; + } + tensors.insert(k, serde_json::from_value::(v)?); + } + let mut ordered: Vec<_> = tensors.values().collect(); + ordered.sort_by_key(|t| (t.data_offsets[0], t.data_offsets[1])); + let mut cursor = 0; for t in ordered { - if t.shape.len()>32 {return fail("tensor rank exceeds 32");} - let count=t.shape.iter().try_fold(1u64,|a,b|a.checked_mul(*b)).ok_or("shape overflow")?; - let size=count.checked_mul(width(&t.dtype)?).ok_or("tensor size overflow")?; - if t.data_offsets[0]!=cursor || t.data_offsets[1].checked_sub(cursor)!=Some(size) {return fail("invalid tensor offsets or shape");} - cursor=t.data_offsets[1]; + if t.shape.len() > 32 { + return fail("tensor rank exceeds 32"); + } + let count = t + .shape + .iter() + .try_fold(1u64, |a, b| a.checked_mul(*b)) + .ok_or("shape overflow")?; + let size = count + .checked_mul(width(&t.dtype)?) + .ok_or("tensor size overflow")?; + if t.data_offsets[0] != cursor || t.data_offsets[1].checked_sub(cursor) != Some(size) { + return fail("invalid tensor offsets or shape"); + } + cursor = t.data_offsets[1]; + } + if cursor != bytes - n - 8 { + return fail("truncated or trailing tensor data"); } - if cursor!=bytes-n-8 {return fail("truncated or trailing tensor data");} - Ok(Inspection{data_start:n+8,bytes,tensors}) + Ok(Inspection { + data_start: n + 8, + bytes, + tensors, + }) } -struct UniqueHeader(BTreeMap); +struct UniqueHeader(BTreeMap); impl<'de> Deserialize<'de> for UniqueHeader { - fn deserialize>(d:D)->std::result::Result{ + fn deserialize>(d: D) -> std::result::Result { struct V; impl<'de> serde::de::Visitor<'de> for V { - type Value=UniqueHeader; - fn expecting(&self,f:&mut std::fmt::Formatter)->std::fmt::Result{f.write_str("unique tensor names")} - fn visit_map>(self,mut m:M)->std::result::Result{ - let mut out=BTreeMap::new(); - while let Some((k,v))=m.next_entry::()? { - if out.insert(k,v).is_some(){return Err(serde::de::Error::custom("duplicate tensor name"));} + type Value = UniqueHeader; + fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + f.write_str("unique tensor names") + } + fn visit_map>( + self, + mut m: M, + ) -> std::result::Result { + let mut out = BTreeMap::new(); + while let Some((k, v)) = m.next_entry::()? { + if out.insert(k, v).is_some() { + return Err(serde::de::Error::custom("duplicate tensor name")); + } } Ok(UniqueHeader(out)) } @@ -80,82 +149,185 @@ impl<'de> Deserialize<'de> for UniqueHeader { } fn hash(f: &mut File) -> Result { f.rewind()?; - let mut h=Sha256::new(); let mut buf=vec![0;BUFFER]; - loop { let n=f.read(&mut buf)?; if n==0 {break;} h.update(&buf[..n]); } - Ok(format!("{:x}",h.finalize())) -} -pub fn fingerprint(path: &Path) -> Result { hash(&mut File::open(path)?) } -fn decode(dtype: &str,b: &[u8]) -> Result { - Ok(match dtype { "F32"=>f32::from_le_bytes(b.try_into()?), - "F16"=>f16::from_le_bytes(b.try_into()?).to_f32(), "BF16"=>bf16::from_le_bytes(b.try_into()?).to_f32(), _=>return fail("editable dtypes: F32 F16 BF16") }) -} -fn encode(dtype: &str,v: f32) -> Result> { - let b=match dtype { "F32"=>v.to_le_bytes().to_vec(), "F16"=>f16::from_f32(v).to_le_bytes().to_vec(), - "BF16"=>bf16::from_f32(v).to_le_bytes().to_vec(), _=>return fail("unsupported edit dtype") }; - if !decode(dtype,&b)?.is_finite() {return fail("nonfinite encoded value");} Ok(b) -} -fn copy_snapshot(source: &Path,dest: &Path) -> Result<(tempfile::NamedTempFile,String)> { - let parent=dest.parent().filter(|p| !p.as_os_str().is_empty()).unwrap_or(Path::new(".")); - if dest.exists() {return fail("destination already exists");} - let mut temp=tempfile::NamedTempFile::new_in(parent)?; - let mut f=File::open(source)?; - let mut h=Sha256::new();let mut buf=vec![0;BUFFER]; - loop {let n=f.read(&mut buf)?;if n==0 {break;}h.update(&buf[..n]);temp.write_all(&buf[..n])?;} - Ok((temp,format!("{:x}",h.finalize()))) -} -fn commit(mut temp: tempfile::NamedTempFile,dest: &Path) -> Result<()> { - temp.flush()?;temp.as_file().sync_all()?; + let mut h = Sha256::new(); + let mut buf = vec![0; BUFFER]; + loop { + let n = f.read(&mut buf)?; + if n == 0 { + break; + } + h.update(&buf[..n]); + } + Ok(format!("{:x}", h.finalize())) +} +pub fn fingerprint(path: &Path) -> Result { + hash(&mut File::open(path)?) +} +fn decode(dtype: &str, b: &[u8]) -> Result { + Ok(match dtype { + "F32" => f32::from_le_bytes(b.try_into()?), + "F16" => f16::from_le_bytes(b.try_into()?).to_f32(), + "BF16" => bf16::from_le_bytes(b.try_into()?).to_f32(), + _ => return fail("editable dtypes: F32 F16 BF16"), + }) +} +fn encode(dtype: &str, v: f32) -> Result> { + let b = match dtype { + "F32" => v.to_le_bytes().to_vec(), + "F16" => f16::from_f32(v).to_le_bytes().to_vec(), + "BF16" => bf16::from_f32(v).to_le_bytes().to_vec(), + _ => return fail("unsupported edit dtype"), + }; + if !decode(dtype, &b)?.is_finite() { + return fail("nonfinite encoded value"); + } + Ok(b) +} +fn copy_snapshot(source: &Path, dest: &Path) -> Result<(tempfile::NamedTempFile, String)> { + let parent = dest + .parent() + .filter(|p| !p.as_os_str().is_empty()) + .unwrap_or(Path::new(".")); + if dest.exists() { + return fail("destination already exists"); + } + let mut temp = tempfile::NamedTempFile::new_in(parent)?; + let mut f = File::open(source)?; + let mut h = Sha256::new(); + let mut buf = vec![0; BUFFER]; + loop { + let n = f.read(&mut buf)?; + if n == 0 { + break; + } + h.update(&buf[..n]); + temp.write_all(&buf[..n])?; + } + Ok((temp, format!("{:x}", h.finalize()))) +} +fn commit(mut temp: tempfile::NamedTempFile, dest: &Path) -> Result<()> { + temp.flush()?; + temp.as_file().sync_all()?; temp.persist_noclobber(dest).map_err(|e| e.error)?; - #[cfg(unix)] File::open(dest.parent().filter(|p| !p.as_os_str().is_empty()).unwrap_or(Path::new(".")))?.sync_all()?; + #[cfg(unix)] + File::open( + dest.parent() + .filter(|p| !p.as_os_str().is_empty()) + .unwrap_or(Path::new(".")), + )? + .sync_all()?; Ok(()) } -pub fn apply(source: &Path,dest: &Path,p: &Patch) -> Result { - if p.schema!="ruv-fann.tensor-patch/1" || p.edits.is_empty() || p.edits.len()>128 {return fail("invalid patch schema or edit count");} +pub fn apply(source: &Path, dest: &Path, p: &Patch) -> Result { + if p.schema != "ruv-fann.tensor-patch/1" || p.edits.is_empty() || p.edits.len() > 128 { + return fail("invalid patch schema or edit count"); + } // Work only on a private copy. Hash and validate the same bytes that we edit. - let (mut temp,base)=copy_snapshot(source,dest)?; - let f=temp.as_file_mut(); - if base!=p.base_sha256 {return fail("baseline hash mismatch");} - let info=inspect(f)?; let mut seen=BTreeSet::new();let mut edits=Vec::new(); + let (mut temp, base) = copy_snapshot(source, dest)?; + let f = temp.as_file_mut(); + if base != p.base_sha256 { + return fail("baseline hash mismatch"); + } + let info = inspect(f)?; + let mut seen = BTreeSet::new(); + let mut edits = Vec::new(); for e in &p.edits { - if !seen.insert((&e.tensor,e.index)) {return fail("duplicate edit");} - if !e.value.is_finite() || e.value.abs()>1_000_000.0 {return fail("invalid replacement");} - let t=info.tensors.get(&e.tensor).ok_or("unknown tensor")?; - let w=width(&t.dtype)?; - let count=(t.data_offsets[1]-t.data_offsets[0])/w; - if e.index>=count {return fail("index out of range");} - let offset=info.data_start+t.data_offsets[0]+e.index*w; - f.seek(SeekFrom::Start(offset))?; let mut before=vec![0;w as usize]; f.read_exact(&mut before)?; - let old=decode(&t.dtype,&before)?; - let after=encode(&t.dtype,e.value)?; let actual=decode(&t.dtype,&after)?; - if !old.is_finite() || (actual-old).abs()>1.0 {return fail("nonfinite baseline or delta exceeds 1");} - if after==before {return fail("edit is a no-op after dtype rounding");} - edits.push(Undo{offset,before,after}); - } - for e in &edits { f.seek(SeekFrom::Start(e.offset))?;f.write_all(&e.after)?; } + if !seen.insert((&e.tensor, e.index)) { + return fail("duplicate edit"); + } + if !e.value.is_finite() || e.value.abs() > 1_000_000.0 { + return fail("invalid replacement"); + } + let t = info.tensors.get(&e.tensor).ok_or("unknown tensor")?; + let w = width(&t.dtype)?; + let count = (t.data_offsets[1] - t.data_offsets[0]) / w; + if e.index >= count { + return fail("index out of range"); + } + let offset = info.data_start + t.data_offsets[0] + e.index * w; + f.seek(SeekFrom::Start(offset))?; + let mut before = vec![0; w as usize]; + f.read_exact(&mut before)?; + let old = decode(&t.dtype, &before)?; + let after = encode(&t.dtype, e.value)?; + let actual = decode(&t.dtype, &after)?; + if !old.is_finite() || (actual - old).abs() > 1.0 { + return fail("nonfinite baseline or delta exceeds 1"); + } + if after == before { + return fail("edit is a no-op after dtype rounding"); + } + edits.push(Undo { + offset, + before, + after, + }); + } + for e in &edits { + f.seek(SeekFrom::Start(e.offset))?; + f.write_all(&e.after)?; + } inspect(f)?; - let candidate=hash(f)?; - commit(temp,dest)?; - Ok(Receipt{schema:"ruv-fann.tensor-receipt/1".into(),base_sha256:base,candidate_sha256:candidate, - edits,behavioral_validation:"not_run".into()}) -} -pub fn rollback(source: &Path,dest: &Path,r: &Receipt) -> Result<()> { - if r.schema!="ruv-fann.tensor-receipt/1" || r.edits.is_empty() || r.edits.len()>128 {return fail("invalid receipt");} - let (mut temp,candidate_hash)=copy_snapshot(source,dest)?;let f=temp.as_file_mut(); - if candidate_hash!=r.candidate_sha256 {return fail("candidate hash mismatch");} - let info=inspect(f)?;let mut ranges=BTreeSet::new(); + let candidate = hash(f)?; + commit(temp, dest)?; + Ok(Receipt { + schema: "ruv-fann.tensor-receipt/1".into(), + base_sha256: base, + candidate_sha256: candidate, + edits, + behavioral_validation: "not_run".into(), + }) +} +pub fn rollback(source: &Path, dest: &Path, r: &Receipt) -> Result<()> { + if r.schema != "ruv-fann.tensor-receipt/1" || r.edits.is_empty() || r.edits.len() > 128 { + return fail("invalid receipt"); + } + let (mut temp, candidate_hash) = copy_snapshot(source, dest)?; + let f = temp.as_file_mut(); + if candidate_hash != r.candidate_sha256 { + return fail("candidate hash mismatch"); + } + let info = inspect(f)?; + let mut ranges = BTreeSet::new(); for e in &r.edits { - let n=e.before.len(); - if ![2,4].contains(&n) || n!=e.after.len() || e.offset(path: &Path) -> Result { - let mut bytes=Vec::new(); File::open(path)?.take(1024*1024+1).read_to_end(&mut bytes)?; - if bytes.len()>1024*1024 {return fail("JSON exceeds 1 MiB");} + let mut bytes = Vec::new(); + File::open(path)? + .take(1024 * 1024 + 1) + .read_to_end(&mut bytes)?; + if bytes.len() > 1024 * 1024 { + return fail("JSON exceeds 1 MiB"); + } Ok(serde_json::from_slice(&bytes)?) } diff --git a/crates/wsp-tensor/src/main.rs b/crates/wsp-tensor/src/main.rs index 56326b3c..7ea06d81 100644 --- a/crates/wsp-tensor/src/main.rs +++ b/crates/wsp-tensor/src/main.rs @@ -1,13 +1,19 @@ -use ruv_fann_wsp_tensor::{apply,fingerprint,inspect,read_json,rollback,Result}; -use std::{fs::File,path::Path}; -fn run()->Result<()> { - let a:Vec=std::env::args().collect(); +use ruv_fann_wsp_tensor::{apply, fingerprint, inspect, read_json, rollback, Result}; +use std::{fs::File, path::Path}; +fn run() -> Result<()> { + let a: Vec = std::env::args().collect(); let result=match a.get(1).map(String::as_str) { Some("inspect") if a.len()==3=>serde_json::json!({"checkpoint":inspect(&mut File::open(&a[2])?)?,"sha256":fingerprint(Path::new(&a[2]))?}), Some("apply") if a.len()==5=>serde_json::to_value(apply(Path::new(&a[2]),Path::new(&a[3]),&read_json(Path::new(&a[4]))?)?)?, Some("rollback") if a.len()==5=>{rollback(Path::new(&a[2]),Path::new(&a[3]),&read_json(Path::new(&a[4]))?)?;serde_json::json!({"restored":fingerprint(Path::new(&a[3]))?})}, _=>return Err("usage: ruv-fann-wsp-tensor inspect FILE | apply BASE OUTPUT PATCH.json | rollback CANDIDATE OUTPUT RECEIPT.json".into()) }; - println!("{}",serde_json::to_string_pretty(&result)?);Ok(()) + println!("{}", serde_json::to_string_pretty(&result)?); + Ok(()) +} +fn main() { + if let Err(e) = run() { + eprintln!("{e}"); + std::process::exit(1); + } } -fn main(){if let Err(e)=run(){eprintln!("{e}");std::process::exit(1);}} diff --git a/crates/wsp-tensor/tests/checkpoints.rs b/crates/wsp-tensor/tests/checkpoints.rs index d5bcfd02..be461116 100644 --- a/crates/wsp-tensor/tests/checkpoints.rs +++ b/crates/wsp-tensor/tests/checkpoints.rs @@ -1,58 +1,166 @@ use ruv_fann_wsp_tensor::*; -use std::{fs::{self,File},path::Path}; -fn fixture(path:&Path,dtype:&str) { - let bytes=match dtype {"F32"=>[0.25f32,0.5].into_iter().flat_map(f32::to_le_bytes).collect::>(), - "BF16"=>[0.25f32,0.5].into_iter().flat_map(|v|half::bf16::from_f32(v).to_le_bytes()).collect(), - _=>[0.25f32,0.5].into_iter().flat_map(|v|half::f16::from_f32(v).to_le_bytes()).collect()}; - write_raw(path,&format!(r#"{{"weight":{{"dtype":"{dtype}","shape":[2],"data_offsets":[0,{}]}}}}"#,bytes.len()),&bytes); -} -fn write_raw(path:&Path,header:&str,data:&[u8]) { - let mut header=header.as_bytes().to_vec();while header.len() & 7 != 0 {header.push(b' ');} - let mut bytes=(header.len() as u64).to_le_bytes().to_vec();bytes.extend(header);bytes.extend(data);fs::write(path,bytes).unwrap(); -} -fn patch(path:&Path)->Patch {Patch{schema:"ruv-fann.tensor-patch/1".into(),base_sha256:fingerprint(path).unwrap(),edits:vec![Edit{tensor:"weight".into(),index:0,value:0.75}]}} -#[test] fn all_precisions_roundtrip_and_official_reader() { - for dtype in ["F32","BF16","F16"] { - let d=tempfile::tempdir().unwrap();let b=d.path().join("base");let c=d.path().join("candidate");let restored=d.path().join("restored"); - fixture(&b,dtype);let before=fs::read(&b).unwrap();let r=apply(&b,&c,&patch(&b)).unwrap(); - let bytes=fs::read(&c).unwrap();let parsed=safetensors::SafeTensors::deserialize(&bytes).unwrap();assert_eq!(parsed.tensor("weight").unwrap().shape(),&[2]); - assert_ne!(r.base_sha256,r.candidate_sha256);assert_eq!(r.behavioral_validation,"not_run"); - rollback(&c,&restored,&r).unwrap();assert_eq!(fs::read(restored).unwrap(),before);assert_eq!(fs::read(&b).unwrap(),before); +use std::{ + fs::{self, File}, + path::Path, +}; +fn fixture(path: &Path, dtype: &str) { + let bytes = match dtype { + "F32" => [0.25f32, 0.5] + .into_iter() + .flat_map(f32::to_le_bytes) + .collect::>(), + "BF16" => [0.25f32, 0.5] + .into_iter() + .flat_map(|v| half::bf16::from_f32(v).to_le_bytes()) + .collect(), + _ => [0.25f32, 0.5] + .into_iter() + .flat_map(|v| half::f16::from_f32(v).to_le_bytes()) + .collect(), + }; + write_raw( + path, + &format!( + r#"{{"weight":{{"dtype":"{dtype}","shape":[2],"data_offsets":[0,{}]}}}}"#, + bytes.len() + ), + &bytes, + ); +} +fn write_raw(path: &Path, header: &str, data: &[u8]) { + let mut header = header.as_bytes().to_vec(); + while header.len() & 7 != 0 { + header.push(b' '); + } + let mut bytes = (header.len() as u64).to_le_bytes().to_vec(); + bytes.extend(header); + bytes.extend(data); + fs::write(path, bytes).unwrap(); +} +fn patch(path: &Path) -> Patch { + Patch { + schema: "ruv-fann.tensor-patch/1".into(), + base_sha256: fingerprint(path).unwrap(), + edits: vec![Edit { + tensor: "weight".into(), + index: 0, + value: 0.75, + }], } } -#[test] fn reject_patch_abuse_without_outputs() { +#[test] +fn all_precisions_roundtrip_and_official_reader() { + for dtype in ["F32", "BF16", "F16"] { + let d = tempfile::tempdir().unwrap(); + let b = d.path().join("base"); + let c = d.path().join("candidate"); + let restored = d.path().join("restored"); + fixture(&b, dtype); + let before = fs::read(&b).unwrap(); + let r = apply(&b, &c, &patch(&b)).unwrap(); + let bytes = fs::read(&c).unwrap(); + let parsed = safetensors::SafeTensors::deserialize(&bytes).unwrap(); + assert_eq!(parsed.tensor("weight").unwrap().shape(), &[2]); + assert_ne!(r.base_sha256, r.candidate_sha256); + assert_eq!(r.behavioral_validation, "not_run"); + rollback(&c, &restored, &r).unwrap(); + assert_eq!(fs::read(restored).unwrap(), before); + assert_eq!(fs::read(&b).unwrap(), before); + } +} +#[test] +fn reject_patch_abuse_without_outputs() { for case in 0..10 { - let d=tempfile::tempdir().unwrap();let b=d.path().join("b");let out=d.path().join("out");fixture(&b,"F32");let mut p=patch(&b); - match case {0=>p.base_sha256="0".repeat(64),1=>p.edits[0].tensor="missing".into(),2=>p.edits[0].index=u64::MAX, - 3=>p.edits[0].value=f32::NAN,4=>p.edits[0].value=2.0,5=>p.edits.push(Edit{tensor:"weight".into(),index:0,value:0.4}), - 6=>p.edits.clear(),7=>p.edits[0].value=0.25,8=>p.schema="bad".into(),_=>p.edits=(0..129).map(|_|Edit{tensor:"weight".into(),index:0,value:0.5}).collect()} - assert!(apply(&b,&out,&p).is_err(),"case {case}");assert!(!out.exists()); - assert_eq!(fs::read_dir(d.path()).unwrap().count(),1); + let d = tempfile::tempdir().unwrap(); + let b = d.path().join("b"); + let out = d.path().join("out"); + fixture(&b, "F32"); + let mut p = patch(&b); + match case { + 0 => p.base_sha256 = "0".repeat(64), + 1 => p.edits[0].tensor = "missing".into(), + 2 => p.edits[0].index = u64::MAX, + 3 => p.edits[0].value = f32::NAN, + 4 => p.edits[0].value = 2.0, + 5 => p.edits.push(Edit { + tensor: "weight".into(), + index: 0, + value: 0.4, + }), + 6 => p.edits.clear(), + 7 => p.edits[0].value = 0.25, + 8 => p.schema = "bad".into(), + _ => { + p.edits = (0..129) + .map(|_| Edit { + tensor: "weight".into(), + index: 0, + value: 0.5, + }) + .collect() + } + } + assert!(apply(&b, &out, &p).is_err(), "case {case}"); + assert!(!out.exists()); + assert_eq!(fs::read_dir(d.path()).unwrap().count(), 1); } } -#[test] fn reject_malformed_headers() { - for h in [r#"{"w":{"dtype":"F32","shape":[2],"data_offsets":[0,4]}}"#, +#[test] +fn reject_malformed_headers() { + for h in [ + r#"{"w":{"dtype":"F32","shape":[2],"data_offsets":[0,4]}}"#, r#"{"w":{"dtype":"F32","shape":[1],"data_offsets":[4,8]}}"#, r#"{"w":{"dtype":"F32","shape":[18446744073709551615,2],"data_offsets":[0,4]}}"#, r#"{"w":{"dtype":"F32","shape":[1],"data_offsets":[0,4]},"w":{"dtype":"F32","shape":[1],"data_offsets":[0,4]}}"#, - r#"{"w":{"dtype":"UNKNOWN","shape":[1],"data_offsets":[0,4]}}"#] { - let d=tempfile::tempdir().unwrap();let b=d.path().join("b");write_raw(&b,h,&[0;4]);assert!(inspect(&mut File::open(b).unwrap()).is_err()); + r#"{"w":{"dtype":"UNKNOWN","shape":[1],"data_offsets":[0,4]}}"#, + ] { + let d = tempfile::tempdir().unwrap(); + let b = d.path().join("b"); + write_raw(&b, h, &[0; 4]); + assert!(inspect(&mut File::open(b).unwrap()).is_err()); } } -#[test] fn refuse_overwrite_and_forged_rollback() { - let d=tempfile::tempdir().unwrap();let b=d.path().join("b");let c=d.path().join("c");let o=d.path().join("o");fixture(&b,"BF16"); - assert!(apply(&b,&b,&patch(&b)).is_err());let mut r=apply(&b,&c,&patch(&b)).unwrap(); - assert!(apply(&b,&c,&patch(&b)).is_err());r.edits[0].before[0]^=1; - assert!(rollback(&c,&o,&r).is_err());assert!(!o.exists()); - r.candidate_sha256="f".repeat(64);assert!(rollback(&c,&o,&r).is_err()); -} -#[test] fn dtype_rounding_is_not_an_edit() { - let d=tempfile::tempdir().unwrap();let b=d.path().join("b");fixture(&b,"BF16");let mut p=patch(&b);p.edits[0].value=f32::from_bits(0.25f32.to_bits()+1); - assert!(apply(&b,&d.path().join("c"),&p).is_err()); -} -#[test] fn preserve_untouched_tensor_and_metadata() { - let d=tempfile::tempdir().unwrap();let b=d.path().join("b");let c=d.path().join("c"); - write_raw(&b,r#"{"__metadata__":{"origin":"fixture"},"weight":{"dtype":"F32","shape":[1],"data_offsets":[0,4]},"other":{"dtype":"F32","shape":[1],"data_offsets":[4,8]}}"#,&[0.25f32,42.0].into_iter().flat_map(f32::to_le_bytes).collect::>()); - apply(&b,&c,&patch(&b)).unwrap();let old=fs::read(b).unwrap();let new=fs::read(c).unwrap(); - assert_eq!(&old[..old.len()-8],&new[..new.len()-8]);assert_eq!(&old[old.len()-4..],&new[new.len()-4..]); +#[test] +fn refuse_overwrite_and_forged_rollback() { + let d = tempfile::tempdir().unwrap(); + let b = d.path().join("b"); + let c = d.path().join("c"); + let o = d.path().join("o"); + fixture(&b, "BF16"); + assert!(apply(&b, &b, &patch(&b)).is_err()); + let mut r = apply(&b, &c, &patch(&b)).unwrap(); + assert!(apply(&b, &c, &patch(&b)).is_err()); + r.edits[0].before[0] ^= 1; + assert!(rollback(&c, &o, &r).is_err()); + assert!(!o.exists()); + r.candidate_sha256 = "f".repeat(64); + assert!(rollback(&c, &o, &r).is_err()); +} +#[test] +fn dtype_rounding_is_not_an_edit() { + let d = tempfile::tempdir().unwrap(); + let b = d.path().join("b"); + fixture(&b, "BF16"); + let mut p = patch(&b); + p.edits[0].value = f32::from_bits(0.25f32.to_bits() + 1); + assert!(apply(&b, &d.path().join("c"), &p).is_err()); +} +#[test] +fn preserve_untouched_tensor_and_metadata() { + let d = tempfile::tempdir().unwrap(); + let b = d.path().join("b"); + let c = d.path().join("c"); + write_raw( + &b, + r#"{"__metadata__":{"origin":"fixture"},"weight":{"dtype":"F32","shape":[1],"data_offsets":[0,4]},"other":{"dtype":"F32","shape":[1],"data_offsets":[4,8]}}"#, + &[0.25f32, 42.0] + .into_iter() + .flat_map(f32::to_le_bytes) + .collect::>(), + ); + apply(&b, &c, &patch(&b)).unwrap(); + let old = fs::read(b).unwrap(); + let new = fs::read(c).unwrap(); + assert_eq!(&old[..old.len() - 8], &new[..new.len() - 8]); + assert_eq!(&old[old.len() - 4..], &new[new.len() - 4..]); } diff --git a/crates/wsp-tensor/validation/benchmark-report.json b/crates/wsp-tensor/validation/benchmark-report.json new file mode 100644 index 00000000..7fa90c53 --- /dev/null +++ b/crates/wsp-tensor/validation/benchmark-report.json @@ -0,0 +1,17 @@ +{ + "scope": "synthetic Safetensors file I/O; warm OS cache; single sample per size", + "rows": [ + { + "payload_mib": 16, + "seconds": 0.0513, + "effective_mib_per_sec": 311.85, + "child_peak_rss_kib_including_spawn_overhead": 17360 + }, + { + "payload_mib": 128, + "seconds": 0.9299, + "effective_mib_per_sec": 137.65, + "child_peak_rss_kib_including_spawn_overhead": 17360 + } + ] +} diff --git a/crates/wsp-tensor/validation/e2e-report.json b/crates/wsp-tensor/validation/e2e-report.json new file mode 100644 index 00000000..c44ab578 --- /dev/null +++ b/crates/wsp-tensor/validation/e2e-report.json @@ -0,0 +1,11 @@ +{ + "schema": "ruv-fann.tensor-e2e/1", + "fixture": "independent BF16 Safetensors", + "roundtrip": "passed", + "overwrite_rejected": true, + "base_sha256": "d3b70606f71adf9ee5c2a251f26192a33e7e4a8071479be8e686cc797a2eb53b", + "candidate_sha256": "3627b08adb9cd1a21428410d9997b0b569b4d0666e5f9bd38eca1bf28e122c72", + "elapsed_ms": 5.249, + "pan2_inference": "not_run", + "behavioral_improvement": "not_established" +} From 1c62829302da66318be4ff8ca0795539588de4a7 Mon Sep 17 00:00:00 2001 From: ruv Date: Wed, 11 Nov 2026 18:36:47 -0500 Subject: [PATCH 03/10] fix: patch standalone tensor rustix advisory --- crates/wsp-tensor/Cargo.lock | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/wsp-tensor/Cargo.lock b/crates/wsp-tensor/Cargo.lock index 78e8634b..978185be 100644 --- a/crates/wsp-tensor/Cargo.lock +++ b/crates/wsp-tensor/Cargo.lock @@ -121,9 +121,9 @@ checksum = "ce5d3ddc6d3fa000eb1536d85e147bfe31aacaba692ed6a876f95cb7c855be78" [[package]] name = "linux-raw-sys" -version = "0.11.0" +version = "0.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "df1d3c3b53da64cf5760482273a98e575c651a67eec7f77df96b5b642de8f039" +checksum = "32a66949e030da00e8c7d4434b251670a91556f4144941d37452769c25d58a53" [[package]] name = "memchr" @@ -163,9 +163,9 @@ checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f" [[package]] name = "rustix" -version = "1.1.2" +version = "1.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cd15f8a2c5551a84d56efdc1cd049089e409ac19a3072d5037a17fd70719ff3e" +checksum = "e5e76742e5294153d4dde1207dbec5bdd81eef6fabdc0d2118babadabfd858aa" dependencies = [ "bitflags", "errno", From f9c146e5dec06fb5845ae600514712f8553ad6fd Mon Sep 17 00:00:00 2001 From: ruv Date: Wed, 11 Nov 2026 18:43:25 -0500 Subject: [PATCH 04/10] Harden paired PAN2 evidence isolation and independent CLI validation --- crates/wsp-tensor/scripts/benchmark.py | 100 +++-- crates/wsp-tensor/scripts/e2e.py | 72 +++- crates/wsp-tensor/scripts/pan2_compare.py | 400 +++++++++++++++--- crates/wsp-tensor/scripts/test_cli_scripts.py | 78 ++++ .../wsp-tensor/scripts/test_pan2_compare.py | 275 ++++++++++++ 5 files changed, 825 insertions(+), 100 deletions(-) create mode 100644 crates/wsp-tensor/scripts/test_cli_scripts.py create mode 100644 crates/wsp-tensor/scripts/test_pan2_compare.py diff --git a/crates/wsp-tensor/scripts/benchmark.py b/crates/wsp-tensor/scripts/benchmark.py index d5cec8c2..df5c730b 100644 --- a/crates/wsp-tensor/scripts/benchmark.py +++ b/crates/wsp-tensor/scripts/benchmark.py @@ -1,24 +1,76 @@ -"""Physical I/O checkpoint benchmark, not neural inference throughput.""" -import hashlib, json, pathlib, resource, struct, subprocess, tempfile, time -ROOT = pathlib.Path(__file__).resolve().parents[1] -BIN = ROOT / "target/release/ruv-fann-wsp-tensor" -rows = [] -for mib in (16, 128): - with tempfile.TemporaryDirectory() as directory: - d = pathlib.Path(directory) - base, out = d / "base", d / "candidate" - size = mib * 1024 * 1024 - h = json.dumps({"weight":{"dtype":"BF16","shape":[2],"data_offsets":[0,4]}, "padding":{"dtype":"U8","shape":[size],"data_offsets":[4,size+4]}}).encode() - h += b" " * (-len(h)%8) - with base.open("wb") as f: - f.write(struct.pack(" MAX_JSON: + raise RuntimeError("JSON input exceeds 1 MiB") + return json.loads(data), data + + +def read_json(path): + return json_snapshot(path)[0] + + +def inventory(root, *, cache=False): + """Hash all files, including config/index files; never read devices/FIFOs. + + HF snapshots legitimately link into their own blobs directory. Those links + are recorded and must stay inside this explicitly selected cache root. + """ + if not root.is_dir(): + raise RuntimeError(f"missing directory: {root}") + result = {} + for path in sorted(root.rglob("*")): + mode = path.lstat().st_mode + name = str(path.relative_to(root)) + if stat.S_ISLNK(mode): + target = path.resolve(strict=True) + if not cache or not target.is_relative_to(root) or not target.is_file(): + raise RuntimeError(f"unsafe symlink: {path}") + result[name] = {"sha256": digest(target), "link": os.readlink(path)} + elif stat.S_ISREG(mode): + result[name] = {"sha256": digest(path)} + elif not stat.S_ISDIR(mode): + raise RuntimeError(f"special file is not an evaluation input: {path}") + if not result: + raise RuntimeError(f"empty input directory: {root}") + return result + + +def model_cache(root): + # HF_HOME also commonly contains authentication tokens. Require a dedicated + # offline data-only cache; never open or emit hashes of credential files. + if not root.is_dir() or any(path.name not in {"hub", "assets"} or not path.is_dir() + or path.is_symlink() for path in root.iterdir()): + raise RuntimeError("--hf-home requires data-only hub/assets directories; no credentials") + return inventory(root, cache=True) + + +def checkpoint(root): + # Pinned generate.py uses /transformer unless basename=transformer. + # Restrict this harness to one unambiguous native Safetensors layout. Its + # fallback loader otherwise accepts .pt/.bin or DCP after a shard load fails. + if root.name != "transformer": + raise RuntimeError("native checkpoint directory must be named transformer") + files = inventory(root) + shards = [name for name in files if name.endswith(".safetensors")] + if not shards or any("/" in name for name in shards): + raise RuntimeError("requires top-level Safetensors shards") + if any(pathlib.Path(name).suffix in {".pt", ".bin"} or pathlib.Path(name).name in + {".metadata", "metadata", "_metadata", ".metadata.tmp"} for name in files): + raise RuntimeError("alternate checkpoint loader formats are not allowed") + if (root.parent / "model_state.pt").exists(): + raise RuntimeError("parent model_state.pt could bypass the selected shards") + return files + + +def verify_receipt(receipt, baseline, candidate, base_hash, candidate_hash): + if (not isinstance(receipt, dict) or receipt.get("schema") != "ruv-fann.tensor-receipt/1" + or receipt.get("base_sha256") != base_hash + or receipt.get("candidate_sha256") != candidate_hash): + raise RuntimeError("receipt does not match checkpoint pair") + edits = receipt.get("edits") + if not isinstance(edits, list) or not 1 <= len(edits) <= 128: + raise RuntimeError("receipt requires 1..128 undo edits") + if baseline.stat().st_size != candidate.stat().st_size: + raise RuntimeError("edited shard size changed") + undo = [] + for edit in edits: + if not isinstance(edit, dict): + raise RuntimeError("invalid undo edit") + offset, before, after = (edit.get(k) for k in ("offset", "before", "after")) + if (type(offset) is not int or offset < 8 or not isinstance(before, list) + or not isinstance(after, list) or len(before) not in (2, 4) + or len(before) != len(after) or before == after + or any(type(x) is not int or not 0 <= x <= 255 for x in before + after)): + raise RuntimeError("invalid undo edit") + undo.append((offset, bytes(before), bytes(after))) + undo.sort() + restored = hashlib.sha256() + with candidate.open("rb") as stream: + header = stream.read(8) + if len(header) != 8: + raise RuntimeError("truncated shard") + data_start = 8 + int.from_bytes(header, "little") + stream.seek(0) + position = 0 + for offset, before, after in undo: + if offset < max(position, data_start) or offset + len(after) > candidate.stat().st_size: + raise RuntimeError("overlapping or out-of-payload undo edit") + remaining = offset - position + while remaining: + chunk = stream.read(min(1024 * 1024, remaining)) + if not chunk: + raise RuntimeError("truncated shard") + restored.update(chunk) + remaining -= len(chunk) + if stream.read(len(after)) != after: + raise RuntimeError("receipt edited bytes do not match candidate") + restored.update(before) + position = offset + len(after) + while chunk := stream.read(1024 * 1024): + restored.update(chunk) + if restored.hexdigest() != base_hash: + raise RuntimeError("receipt undo does not restore baseline") -def digest(p): - with p.open("rb") as f: return hashlib.file_digest(f, "sha256").hexdigest() - -def inventory(p): - paths = sorted(p.rglob("*.safetensors")) - if not paths: raise RuntimeError("checkpoint requires Safetensors shards") - return {str(f.relative_to(p)): digest(f) for f in paths} - -def main(): - a = argparse.ArgumentParser() - for name in ("repo", "baseline", "candidate", "prompts", "output", "receipt"): - a.add_argument("--"+name, required=True, type=pathlib.Path) - a.add_argument("--seed",type=int,default=123) - a.add_argument("--timeout",type=int,default=7200) - args = a.parse_args() - for name in ("repo","baseline","candidate","prompts","output","receipt"): - setattr(args,name,getattr(args,name).resolve()) - if not shutil.which("nvidia-smi"): raise RuntimeError("CUDA executor unavailable: nvidia-smi missing") - subprocess.run(["nvidia-smi"],check=True,stdout=subprocess.DEVNULL) - head = subprocess.check_output(["git","-C",str(args.repo),"rev-parse","HEAD"],text=True).strip() - if head != PIN: raise RuntimeError("PAN2 checkout does not match pinned revision") - if subprocess.check_output(["git","-C",str(args.repo),"status","--porcelain"],text=True).strip(): + +def prompt_images(prompts, repo): + """Resolve aliases as pinned inference.inputs does (relative to repo cwd).""" + if isinstance(prompts, dict): + keys = [key for key in ("entries", "prompts", "data") if key in prompts] + if len(keys) != 1: + raise RuntimeError("prompt wrapper requires one entry list") + prompts = prompts[keys[0]] + if not isinstance(prompts, list) or not prompts: + raise RuntimeError("prompts require a nonempty entry list") + images = {} + for entry in prompts: + if not isinstance(entry, dict): + raise RuntimeError("prompt entries must be objects") + for key in ("image_path", "image", "ref_image"): + if entry.get(key): + if not isinstance(entry[key], str): + raise RuntimeError("image reference must be a local path") + path = pathlib.Path(entry[key]) + if not path.is_absolute(): + path = repo / path + regular(path) + images[str(path.resolve(strict=True))] = digest(path) + return images + + +def git(repo, *args): + return subprocess.check_output(["git", "-C", str(repo), *args], text=True, timeout=30).strip() + + +def source_flags(repo): + # git status/diff trust assume-unchanged and skip-worktree bits. A modified + # launcher with either bit is not proof of the pinned executable checkout. + flags = git(repo, "ls-files", "-v", "-z") + if any(entry and not entry.startswith("H ") for entry in flags.split("\0")): + raise RuntimeError("PAN2 index must not hide files with assume-unchanged/skip-worktree") + return flags + + +def run_process(command, *, cwd, env, log, timeout): + process = subprocess.Popen(command, cwd=cwd, env=env, stdout=log, + stderr=subprocess.STDOUT, start_new_session=True) + try: + code = process.wait(timeout=timeout) + if code: + raise RuntimeError(f"inference failed: exit {code}") + finally: + # Even a successful parent may leave workers alive. Kill the whole + # session before hashing inputs or starting the other checkpoint. + try: + os.killpg(process.pid, signal.SIGKILL) + except ProcessLookupError: + pass + process.wait() + + +def environment(args, label): + env = os.environ.copy() + for key in list(env): + if (key in {"BASH_ENV", "ENV", "SHELLOPTS", "BASHOPTS", "CDPATH", "GLOBIGNORE", + "PYTHONPATH", "PYTHONHOME", "LD_PRELOAD", "LD_LIBRARY_PATH", + "HF_TOKEN", "HUGGING_FACE_HUB_TOKEN", "HF_TOKEN_PATH", "HF_STORED_TOKENS_PATH"} + or key.startswith("BASH_FUNC_")): + env.pop(key) + env.update({"PAN_ROOT": str(args.repo), "SEED": str(args.seed), "NUM_GPUS": "1", + "NUM_STEPS": "50", "FLOW_SHIFT": "9", "GUIDANCE_SCALE": "2.5", + "VIDEO_HEIGHT": "704", "VIDEO_WIDTH": "1248", "FPS": "24", + "VIDEO_LENGTH": "241", "TASK": "auto", "EXTRA_ARGS": "", + "CONDA_ENV": "pan", "TRAINING_CONFIG": "config/sft_29B_720p.yaml", + "NEGATIVE_PROMPT_FILE": "inference/prompts/negative_prompt.json", + "HF_HOME": str(args.hf_home), "HF_HUB_CACHE": str(args.hf_home / "hub"), + "HUGGINGFACE_HUB_CACHE": str(args.hf_home / "hub"), + "TRANSFORMERS_CACHE": str(args.hf_home / "hub"), + "HF_ASSETS_CACHE": str(args.hf_home / "assets"), + "HF_HUB_OFFLINE": "1", "TRANSFORMERS_OFFLINE": "1", + "HF_DATASETS_OFFLINE": "1", "PYTHONDONTWRITEBYTECODE": "1"}) + for key in ("TRITON_CACHE_DIR", "TORCHINDUCTOR_CACHE_DIR", + "FLASH_ATTENTION_CUTE_DSL_CACHE_DIR", "QUACK_CACHE_DIR"): + env[key] = str(args.output / (label + "-cache") / key.lower()) + return env + + +def main(argv=None): + parser = argparse.ArgumentParser(description=__doc__) + for name in ("repo", "baseline", "candidate", "prompts", "output", "receipt", "hf-home"): + parser.add_argument("--" + name, required=True, type=pathlib.Path) + parser.add_argument("--seed", type=int, default=123) + parser.add_argument("--timeout", type=int, default=7200) + args = parser.parse_args(argv) + for name in ("repo", "baseline", "candidate", "prompts", "output", "receipt", "hf_home"): + path = getattr(args, name).absolute() + if path.is_symlink(): + raise RuntimeError("input/output root must not be a symlink") + setattr(args, name, path.resolve()) + if args.timeout < 1: + raise RuntimeError("timeout must be positive") + roots = [args.repo, args.baseline, args.candidate, args.hf_home, args.output] + for i, root in enumerate(roots): + if any(root.is_relative_to(other) or other.is_relative_to(root) for other in roots[:i]): + raise RuntimeError("repository, checkpoints, cache and output must be disjoint") + if any(path.is_relative_to(args.output) for path in (args.prompts, args.receipt)): + raise RuntimeError("output must not contain inputs") + regular(args.prompts) + regular(args.receipt) + flags = source_flags(args.repo) + head = git(args.repo, "rev-parse", "HEAD") + if head != PIN: + raise RuntimeError("PAN2 checkout does not match pinned revision") + if git(args.repo, "status", "--porcelain", "--untracked-files=all"): raise RuntimeError("PAN2 checkout must be clean") - if args.timeout<1 or args.baseline==args.candidate: raise RuntimeError("invalid timeout or identical checkpoint directories") - before = {"baseline":inventory(args.baseline),"candidate":inventory(args.candidate)} - if before["baseline"].keys()!=before["candidate"].keys(): raise RuntimeError("shard sets differ") - changed = [k for k in before["baseline"] if before["baseline"][k]!=before["candidate"][k]] - if len(changed)!=1: raise RuntimeError("exactly one edited shard is required") - receipt = json.loads(args.receipt.read_text()) + # Ignored Python source can shadow a tracked module despite clean git status. + if git(args.repo, "ls-files", "--others", "--ignored", "--exclude-standard", "--", "*.py", "*.so", "*.sh"): + raise RuntimeError("ignored executable source in PAN2 checkout") + before = {"baseline": checkpoint(args.baseline), "candidate": checkpoint(args.candidate)} + if before["baseline"].keys() != before["candidate"].keys(): + raise RuntimeError("checkpoint file sets differ") + changed = [key for key in before["baseline"] if before["baseline"][key] != before["candidate"][key]] + if len(changed) != 1 or not changed[0].endswith(".safetensors"): + raise RuntimeError("exactly one edited shard and otherwise identical files required") key = changed[0] - if receipt.get("schema")!="ruv-fann.tensor-receipt/1" or receipt.get("base_sha256")!=before["baseline"][key] or receipt.get("candidate_sha256")!=before["candidate"][key]: - raise RuntimeError("receipt does not match checkpoint pair") - prompts_hash = digest(args.prompts) - args.output.mkdir(parents=True,exist_ok=False) + receipt, receipt_bytes = json_snapshot(args.receipt) + receipt_hash = hashlib.sha256(receipt_bytes).hexdigest() + verify_receipt(receipt, args.baseline / key, args.candidate / key, + before["baseline"][key]["sha256"], before["candidate"][key]["sha256"]) + prompt_data, prompt_bytes = json_snapshot(args.prompts) + prompts_hash = hashlib.sha256(prompt_bytes).hexdigest() + images = prompt_images(prompt_data, args.repo) + cache = model_cache(args.hf_home) + source = git(args.repo, "ls-files", "-s") + if not shutil.which("nvidia-smi"): + raise RuntimeError("CUDA executor unavailable: nvidia-smi missing") + gpu = subprocess.check_output(["nvidia-smi"], text=True, timeout=30) + args.output.mkdir(parents=True, exist_ok=False) prompts = args.output / "prompts.json" - shutil.copyfile(args.prompts,prompts) - # Fixed sampler; remove inherited shell EXTRA_ARGS and sampler overrides. - env = os.environ.copy() - env.update({"PAN_ROOT":str(args.repo),"SEED":str(args.seed),"NUM_GPUS":"1","NUM_STEPS":"50","FLOW_SHIFT":"9","GUIDANCE_SCALE":"2.5","VIDEO_HEIGHT":"704","VIDEO_WIDTH":"1248","FPS":"24","VIDEO_LENGTH":"241","TASK":"auto","EXTRA_ARGS":"","TRAINING_CONFIG":"config/sft_29B_720p.yaml","NEGATIVE_PROMPT_FILE":"inference/prompts/negative_prompt.json","HF_HUB_OFFLINE":"1"}) + prompts.write_bytes(prompt_bytes) + (args.output / "receipt.json").write_bytes(receipt_bytes) + + def unchanged(): + if (before != {"baseline": checkpoint(args.baseline), "candidate": checkpoint(args.candidate)} + or digest(prompts) != prompts_hash or digest(args.prompts) != prompts_hash + or digest(args.receipt) != receipt_hash + or digest(args.output / "receipt.json") != receipt_hash + or images != {path: digest(regular(pathlib.Path(path))) for path in images} + or cache != model_cache(args.hf_home) + or head != git(args.repo, "rev-parse", "HEAD") + or source != git(args.repo, "ls-files", "-s") + or flags != source_flags(args.repo) + or git(args.repo, "diff", "HEAD", "--") + or git(args.repo, "status", "--porcelain", "--untracked-files=all") + or git(args.repo, "ls-files", "--others", "--ignored", "--exclude-standard", "--", "*.py", "*.so", "*.sh")): + raise RuntimeError("inputs changed during evaluation") + runs = {} - for label in ("baseline","candidate"): + for label in ("baseline", "candidate"): + unchanged() start = time.perf_counter() - with (args.output / (label+".log")).open("w") as log: - process = subprocess.Popen(["bash",str(args.repo/"inference/inference.sh"),str(getattr(args,label)),str(args.output/label),str(prompts)],cwd=args.repo,env=env,stdout=log,stderr=subprocess.STDOUT,start_new_session=True) - try: - code = process.wait(timeout=args.timeout) - if code: raise RuntimeError(f"{label} inference failed: exit {code}") - except BaseException: - try: os.killpg(process.pid,signal.SIGKILL) - except ProcessLookupError: pass - process.wait() - raise - videos = sorted((args.output/label).rglob("*.mp4")) - if not videos: raise RuntimeError("inference produced no videos") - runs[label] = {"seconds":time.perf_counter()-start,"videos":{str(p.relative_to(args.output/label)):digest(p) for p in videos}} - after = {"baseline":inventory(args.baseline),"candidate":inventory(args.candidate)} - if before!=after or digest(prompts)!=prompts_hash: raise RuntimeError("inputs changed during evaluation") - result = {"schema":"ruv-fann.pan2-paired-inference/1","pan2_commit":head,"seed":args.seed,"prompts_sha256":prompts_hash,"receipt_sha256":digest(args.receipt),"checkpoints":before,"runs":runs,"behavioral_validation":"requires independent scoring","promotion":"not_performed"} - (args.output/"comparison.json").write_text(json.dumps(result,indent=2)) - print(json.dumps(result,indent=2)) - -if __name__ == "__main__": main() + with (args.output / (label + ".log")).open("x") as log: + run_process(["bash", "--noprofile", "--norc", str(args.repo / "inference/inference.sh"), + str(getattr(args, label)), str(args.output / label), str(prompts)], + cwd=args.repo, env=environment(args, label), log=log, timeout=args.timeout) + elapsed = time.perf_counter() - start + unchanged() + outputs = inventory(args.output / label) + videos = {name: value["sha256"] for name, value in outputs.items() if name.endswith(".mp4")} + if not videos: + raise RuntimeError("inference produced no videos") + if runs and videos.keys() != runs["baseline"]["videos"].keys(): + raise RuntimeError("baseline/candidate video sets differ") + # Upstream saves the actual parsed arguments beside each video. Require + # them so a stale/misdirected worker cannot qualify the other checkpoint. + for name in videos: + video = args.output / label / name + if video.stat().st_size == 0: + raise RuntimeError("empty video output") + settings = read_json(video.with_name(video.stem + "_config.json")) + actual = settings.get("arguments", {}) + expected = {"checkpoint_path": str(getattr(args, label)), "prompt_json": str(prompts), + "seed": args.seed, "num_inference_steps": 50, "flow_shift": 9, + "guidance_scale": 2.5, "video_height": 704, "video_width": 1248, + "fps": 24, "dtype": "bf16"} + if not isinstance(actual, dict) or any(actual.get(k) != v for k, v in expected.items()): + raise RuntimeError("output settings do not match selected checkpoint/sampler") + runs[label] = {"seconds": elapsed, "videos": videos, "files": outputs} + result = {"schema": "ruv-fann.pan2-paired-inference/1", "pan2_commit": head, + "seed": args.seed, "prompts_sha256": prompts_hash, "receipt_sha256": receipt_hash, + "checkpoints": before, "reference_images": images, "hf_home_files": cache, + "gpu_inventory": gpu, "runs": runs, + "harness_sha256": digest(pathlib.Path(__file__).resolve()), + "host": platform.platform(), "harness_python": sys.version, + "runtime_dependencies": "operator-provisioned pan Conda environment; not certified by this receipt", + "timing_scope": "single cold per-run kernel cache; includes launcher/model loading; no speedup claim", + "behavioral_validation": "requires independent scoring", "promotion": "not_performed"} + (args.output / "comparison.json").write_text(json.dumps(result, indent=2)) + print(json.dumps(result, indent=2)) + return result + + +if __name__ == "__main__": + main() diff --git a/crates/wsp-tensor/scripts/test_cli_scripts.py b/crates/wsp-tensor/scripts/test_cli_scripts.py new file mode 100644 index 00000000..276d7c88 --- /dev/null +++ b/crates/wsp-tensor/scripts/test_cli_scripts.py @@ -0,0 +1,78 @@ +"""Release-oracle regressions; fake CLI tests are explicitly not native proof.""" +import contextlib +import importlib +import io +import os +import pathlib +import subprocess +import sys +import tempfile +import unittest +from unittest import mock + +import benchmark +import e2e + + +class CliScripts(unittest.TestCase): + def test_explicit_binary_override_selects_exact_file(self): + with tempfile.TemporaryDirectory() as directory: + executable = pathlib.Path(directory) / 'installed-cli' + executable.write_bytes(b'fixture binary path only') + with mock.patch.dict(os.environ, {'WSP_TENSOR_BIN': str(executable)}): + self.assertEqual(e2e.binary(), executable.resolve()) + + def test_target_directory_override_resolves_standalone_binary(self): + with tempfile.TemporaryDirectory() as directory: + executable = pathlib.Path(directory) / 'release/ruv-fann-wsp-tensor' + executable.parent.mkdir() + executable.write_bytes(b'fixture binary path only') + with mock.patch.dict(os.environ, {'CARGO_TARGET_DIR': directory}): + os.environ.pop('WSP_TENSOR_BIN', None) + self.assertEqual(e2e.binary(), executable) + + def test_optimized_python_rejects_wrong_native_candidate_bytes(self): + # Fake external CLI reports correct hashes of a deliberately unchanged + # candidate. The independent BF16 oracle must still fail under -O. + with tempfile.TemporaryDirectory() as directory: + fake = pathlib.Path(directory) / 'fake-cli' + fake.write_text('''#!/usr/bin/env python3 +import hashlib,json,pathlib,sys +command,source=sys.argv[1:3];data=pathlib.Path(source).read_bytes();sha=hashlib.sha256(data).hexdigest() +if command=='inspect': print(json.dumps({'sha256':sha})) +elif command=='apply': + pathlib.Path(sys.argv[3]).write_bytes(data) + print(json.dumps({'schema':'ruv-fann.tensor-receipt/1','base_sha256':sha,'candidate_sha256':sha,'behavioral_validation':'not_run'})) +else: raise RuntimeError('oracle should have rejected before rollback') +''') + fake.chmod(0o700) + env = dict(os.environ, WSP_TENSOR_BIN=str(fake)) + result = subprocess.run([sys.executable, '-O', str(pathlib.Path(e2e.__file__))], + env=env, capture_output=True, text=True, timeout=20) + self.assertNotEqual(result.returncode, 0) + self.assertIn('candidate differs outside expected BF16 edit', result.stderr) + + def test_cli_process_has_timeout(self): + with tempfile.TemporaryDirectory() as directory: + fake = pathlib.Path(directory) / 'fixture' + fake.write_bytes(b'path fixture') + with mock.patch.dict(os.environ, {'WSP_TENSOR_BIN': str(fake)}), \ + mock.patch.object(e2e.subprocess, 'run', side_effect=subprocess.TimeoutExpired('fixture', 60)) as run: + with self.assertRaises(subprocess.TimeoutExpired): + e2e.run('inspect', '/fixture') + self.assertEqual(run.call_args.kwargs['timeout'], 60) + + def test_import_benchmark_does_not_start_benchmark(self): + with mock.patch.object(benchmark.subprocess, 'run', side_effect=RuntimeError('import spawned CLI')): + importlib.reload(benchmark) + + def test_benchmark_rejects_unbounded_work_before_loading_cli(self): + with mock.patch.object(benchmark, 'binary', side_effect=RuntimeError('must validate first')), \ + contextlib.redirect_stderr(io.StringIO()): + for argv in (['--repeats', '0'], ['--repeats', '101'], ['--sizes-mib', '0'], ['--timeout', '0']): + with self.assertRaises(SystemExit): + benchmark.main(argv) + + +if __name__ == '__main__': + unittest.main() diff --git a/crates/wsp-tensor/scripts/test_pan2_compare.py b/crates/wsp-tensor/scripts/test_pan2_compare.py new file mode 100644 index 00000000..68887242 --- /dev/null +++ b/crates/wsp-tensor/scripts/test_pan2_compare.py @@ -0,0 +1,275 @@ +"""CPU-only harness regressions. The mock launcher does NOT run PAN2/CUDA.""" +import contextlib +import hashlib +import io +import json +import os +import pathlib +import signal +import struct +import subprocess +import tempfile +import time +import unittest +from unittest import mock + +import pan2_compare as pan + + +class PairedHarness(unittest.TestCase): + def setUp(self): + self.temp = tempfile.TemporaryDirectory() + self.addCleanup(self.temp.cleanup) + self.root = pathlib.Path(self.temp.name) + self.repo = self.root / 'repo' + self.repo.mkdir() + self.base = self.root / 'baseline/transformer' + self.candidate = self.root / 'candidate/transformer' + self.cache = self.root / 'hf-home' + for path in (self.base, self.candidate, self.cache): + path.mkdir(parents=True) + header = json.dumps({'weight': {'dtype': 'BF16', 'shape': [2], 'data_offsets': [0, 4]}}).encode() + header += b' ' * (-len(header) % 8) + data = struct.pack(' "$1"', 'fixture', str(marker)], + cwd=self.root, env=os.environ.copy(), log=log, timeout=5) + pid = int(marker.read_text()) + for _ in range(100): + proc = pathlib.Path(f'/proc/{pid}/stat') + try: + state = proc.read_text().split()[2] + except (FileNotFoundError, ProcessLookupError): + break + if state == 'Z': + break + time.sleep(0.01) + else: + self.fail('worker survived successful launcher cleanup') + + def test_symlink_video_cannot_hash_an_external_file(self): + def tamper(kw): + video = self.root / 'result/baseline/clip.mp4' + video.unlink() + video.symlink_to(self.receipt) + with self.assertRaisesRegex(RuntimeError, 'symlink'): + self.invoke(tamper) + + def test_prompt_file_swap_after_parsing_rejected_before_launch(self): + snapshot = pan.json_snapshot + def swap(path): + result = snapshot(path) + if path == self.prompts: + self.prompts.write_text('[{"type":"t2v","prompt":"swapped"}]') + return result + with mock.patch.object(pan, 'json_snapshot', side_effect=swap), \ + self.assertRaisesRegex(RuntimeError, 'inputs changed'): + self.invoke() + self.assertFalse((self.root / 'result/baseline').exists()) + + def test_timeout_kills_worker_process_group(self): + marker = self.root / 'worker-pid' + script = self.root / 'timeout.sh' + script.write_text('sleep 60 &\necho $! > "$1"\nwait\n') + with (self.root / 'timeout.log').open('w') as log: + with self.assertRaises(subprocess.TimeoutExpired): + pan.run_process(['bash', str(script), str(marker)], cwd=self.root, + env=os.environ.copy(), log=log, timeout=0.2) + pid = int(marker.read_text()) + for _ in range(100): + proc = pathlib.Path(f'/proc/{pid}/stat') + try: + state = proc.read_text().split()[2] + except (FileNotFoundError, ProcessLookupError): + break + if state == 'Z': + break + time.sleep(0.01) + else: + self.fail('worker survived timeout cleanup') + + +if __name__ == '__main__': + unittest.main() From 9afba045bf1c083ff10a46fe49e4405242fe98d4 Mon Sep 17 00:00:00 2001 From: ruv Date: Wed, 11 Nov 2026 18:44:31 -0500 Subject: [PATCH 05/10] Harden WSP tensor inspection and rollback --- crates/wsp-tensor/src/lib.rs | 91 +++++++++++++++++++++----- crates/wsp-tensor/src/main.rs | 38 ++++++++--- crates/wsp-tensor/tests/checkpoints.rs | 76 ++++++++++++++++++++- 3 files changed, 180 insertions(+), 25 deletions(-) diff --git a/crates/wsp-tensor/src/lib.rs b/crates/wsp-tensor/src/lib.rs index abee5fed..c2651a23 100644 --- a/crates/wsp-tensor/src/lib.rs +++ b/crates/wsp-tensor/src/lib.rs @@ -44,6 +44,7 @@ pub struct Receipt { pub behavioral_validation: String, } #[derive(Debug, Deserialize, Serialize)] +#[serde(deny_unknown_fields)] pub struct Tensor { pub dtype: String, pub shape: Vec, @@ -85,14 +86,7 @@ pub fn inspect(f: &mut File) -> Result { f.read_exact(&mut raw)?; // Streaming validation avoids mapping an entire multi-GB shard into memory. let object: UniqueHeader = serde_json::from_slice(&raw)?; - let mut tensors = BTreeMap::new(); - for (k, v) in object.0 { - if k == "__metadata__" { - let _: BTreeMap = serde_json::from_value(v)?; - continue; - } - tensors.insert(k, serde_json::from_value::(v)?); - } + let tensors = object.0; let mut ordered: Vec<_> = tensors.values().collect(); ordered.sort_by_key(|t| (t.data_offsets[0], t.data_offsets[1])); let mut cursor = 0; @@ -122,22 +116,32 @@ pub fn inspect(f: &mut File) -> Result { tensors, }) } -struct UniqueHeader(BTreeMap); +struct UniqueHeader(BTreeMap); impl<'de> Deserialize<'de> for UniqueHeader { fn deserialize>(d: D) -> std::result::Result { struct V; impl<'de> serde::de::Visitor<'de> for V { type Value = UniqueHeader; fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - f.write_str("unique tensor names") + f.write_str("unique tensor names and metadata") } fn visit_map>( self, mut m: M, ) -> std::result::Result { let mut out = BTreeMap::new(); - while let Some((k, v)) = m.next_entry::()? { - if out.insert(k, v).is_some() { + let mut metadata_seen = false; + while let Some(k) = m.next_key::()? { + if k == "__metadata__" { + if metadata_seen { + return Err(serde::de::Error::custom("duplicate __metadata__")); + } + metadata_seen = true; + let _: UniqueMetadata = m.next_value()?; + continue; + } + let tensor = m.next_value::()?; + if out.insert(k, tensor).is_some() { return Err(serde::de::Error::custom("duplicate tensor name")); } } @@ -147,6 +151,32 @@ impl<'de> Deserialize<'de> for UniqueHeader { d.deserialize_map(V) } } +struct UniqueMetadata; +impl<'de> Deserialize<'de> for UniqueMetadata { + fn deserialize>(d: D) -> std::result::Result { + struct V; + impl<'de> serde::de::Visitor<'de> for V { + type Value = UniqueMetadata; + fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + f.write_str("unique string metadata keys") + } + fn visit_map>( + self, + mut m: M, + ) -> std::result::Result { + let mut seen = BTreeSet::new(); + while let Some(key) = m.next_key::()? { + if !seen.insert(key) { + return Err(serde::de::Error::custom("duplicate metadata key")); + } + let _: String = m.next_value()?; + } + Ok(UniqueMetadata) + } + } + d.deserialize_map(V) + } +} fn hash(f: &mut File) -> Result { f.rewind()?; let mut h = Sha256::new(); @@ -163,6 +193,9 @@ fn hash(f: &mut File) -> Result { pub fn fingerprint(path: &Path) -> Result { hash(&mut File::open(path)?) } +pub fn fingerprint_file(file: &mut File) -> Result { + hash(file) +} fn decode(dtype: &str, b: &[u8]) -> Result { Ok(match dtype { "F32" => f32::from_le_bytes(b.try_into()?), @@ -278,8 +311,12 @@ pub fn apply(source: &Path, dest: &Path, p: &Patch) -> Result { behavioral_validation: "not_run".into(), }) } -pub fn rollback(source: &Path, dest: &Path, r: &Receipt) -> Result<()> { - if r.schema != "ruv-fann.tensor-receipt/1" || r.edits.is_empty() || r.edits.len() > 128 { +pub fn rollback(source: &Path, dest: &Path, r: &Receipt) -> Result { + if r.schema != "ruv-fann.tensor-receipt/1" + || r.behavioral_validation != "not_run" + || r.edits.is_empty() + || r.edits.len() > 128 + { return fail("invalid receipt"); } let (mut temp, candidate_hash) = copy_snapshot(source, dest)?; @@ -301,6 +338,26 @@ pub fn rollback(source: &Path, dest: &Path, r: &Receipt) -> Result<()> { { return fail("invalid undo range"); } + if e.before == e.after { + return fail("no-op undo"); + } + let dtype = info.tensors.values().find_map(|t| { + let element_width = width(&t.dtype).ok()?; + let start = info.data_start.checked_add(t.data_offsets[0])?; + let end = info.data_start.checked_add(t.data_offsets[1])?; + (matches!(t.dtype.as_str(), "F32" | "F16" | "BF16") + && n as u64 == element_width + && e.offset >= start + && e.offset.checked_add(n as u64)? <= end + && (e.offset - start) % element_width == 0) + .then_some(t.dtype.as_str()) + }); + let dtype = dtype.ok_or("undo is not an editable tensor element")?; + let before = decode(dtype, &e.before)?; + let after = decode(dtype, &e.after)?; + if !before.is_finite() || !after.is_finite() || (after - before).abs() > 1.0 { + return fail("invalid undo values"); + } for i in e.offset..e.offset + n as u64 { if !ranges.insert(i) { return fail("overlapping undo"); @@ -315,11 +372,13 @@ pub fn rollback(source: &Path, dest: &Path, r: &Receipt) -> Result<()> { f.seek(SeekFrom::Start(e.offset))?; f.write_all(&e.before)?; } - if hash(f)? != r.base_sha256 { + let restored = hash(f)?; + if restored != r.base_sha256 { return fail("restored hash mismatch"); } inspect(f)?; - commit(temp, dest) + commit(temp, dest)?; + Ok(restored) } pub fn read_json(path: &Path) -> Result { let mut bytes = Vec::new(); diff --git a/crates/wsp-tensor/src/main.rs b/crates/wsp-tensor/src/main.rs index 7ea06d81..a6ec13a8 100644 --- a/crates/wsp-tensor/src/main.rs +++ b/crates/wsp-tensor/src/main.rs @@ -1,16 +1,38 @@ -use ruv_fann_wsp_tensor::{apply, fingerprint, inspect, read_json, rollback, Result}; -use std::{fs::File, path::Path}; +use ruv_fann_wsp_tensor::{apply, fingerprint_file, inspect, read_json, rollback, Result}; +use std::{ffi::OsString, fs::File, path::Path}; + fn run() -> Result<()> { - let a: Vec = std::env::args().collect(); - let result=match a.get(1).map(String::as_str) { - Some("inspect") if a.len()==3=>serde_json::json!({"checkpoint":inspect(&mut File::open(&a[2])?)?,"sha256":fingerprint(Path::new(&a[2]))?}), - Some("apply") if a.len()==5=>serde_json::to_value(apply(Path::new(&a[2]),Path::new(&a[3]),&read_json(Path::new(&a[4]))?)?)?, - Some("rollback") if a.len()==5=>{rollback(Path::new(&a[2]),Path::new(&a[3]),&read_json(Path::new(&a[4]))?)?;serde_json::json!({"restored":fingerprint(Path::new(&a[3]))?})}, - _=>return Err("usage: ruv-fann-wsp-tensor inspect FILE | apply BASE OUTPUT PATCH.json | rollback CANDIDATE OUTPUT RECEIPT.json".into()) + let args: Vec = std::env::args_os().skip(1).collect(); + let result = match args.first().and_then(|arg| arg.to_str()) { + Some("inspect") if args.len() == 2 => { + // Inspect and fingerprint the same open file. Reopening the path after + // inspection could report a hash for a replacement file. + let mut file = File::open(Path::new(&args[1]))?; + let checkpoint = inspect(&mut file)?; + let sha256 = fingerprint_file(&mut file)?; + serde_json::json!({"checkpoint": checkpoint, "sha256": sha256}) + } + Some("apply") if args.len() == 4 => serde_json::to_value(apply( + Path::new(&args[1]), + Path::new(&args[2]), + &read_json(Path::new(&args[3]))?, + )?)?, + Some("rollback") if args.len() == 4 => { + let restored = rollback( + Path::new(&args[1]), + Path::new(&args[2]), + &read_json(Path::new(&args[3]))?, + )?; + serde_json::json!({"restored": restored}) + } + _ => { + return Err("usage: ruv-fann-wsp-tensor inspect FILE | apply BASE OUTPUT PATCH.json | rollback CANDIDATE OUTPUT RECEIPT.json".into()); + } }; println!("{}", serde_json::to_string_pretty(&result)?); Ok(()) } + fn main() { if let Err(e) = run() { eprintln!("{e}"); diff --git a/crates/wsp-tensor/tests/checkpoints.rs b/crates/wsp-tensor/tests/checkpoints.rs index be461116..38df2322 100644 --- a/crates/wsp-tensor/tests/checkpoints.rs +++ b/crates/wsp-tensor/tests/checkpoints.rs @@ -1,6 +1,7 @@ use ruv_fann_wsp_tensor::*; use std::{ fs::{self, File}, + io::Write, path::Path, }; fn fixture(path: &Path, dtype: &str) { @@ -112,12 +113,20 @@ fn reject_malformed_headers() { r#"{"w":{"dtype":"F32","shape":[1],"data_offsets":[4,8]}}"#, r#"{"w":{"dtype":"F32","shape":[18446744073709551615,2],"data_offsets":[0,4]}}"#, r#"{"w":{"dtype":"F32","shape":[1],"data_offsets":[0,4]},"w":{"dtype":"F32","shape":[1],"data_offsets":[0,4]}}"#, + r#"{"w":{"dtype":"F64","dtype":"F32","shape":[1],"data_offsets":[0,4]}}"#, + r#"{"w":{"dtype":"F32","shape":[1],"data_offsets":[0,4],"extra":true}}"#, + r#"{"__metadata__":{"origin":"a","origin":"b"},"w":{"dtype":"F32","shape":[1],"data_offsets":[0,4]}}"#, + r#"{"__metadata__":{"origin":"a"},"__metadata__":{"origin":"b"},"w":{"dtype":"F32","shape":[1],"data_offsets":[0,4]}}"#, + r#"{"w":{"dtype":"F32","shape":[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],"data_offsets":[0,4]}}"#, r#"{"w":{"dtype":"UNKNOWN","shape":[1],"data_offsets":[0,4]}}"#, ] { let d = tempfile::tempdir().unwrap(); let b = d.path().join("b"); write_raw(&b, h, &[0; 4]); - assert!(inspect(&mut File::open(b).unwrap()).is_err()); + assert!( + inspect(&mut File::open(b).unwrap()).is_err(), + "accepted malformed header: {h}" + ); } } #[test] @@ -130,12 +139,77 @@ fn refuse_overwrite_and_forged_rollback() { assert!(apply(&b, &b, &patch(&b)).is_err()); let mut r = apply(&b, &c, &patch(&b)).unwrap(); assert!(apply(&b, &c, &patch(&b)).is_err()); + r.behavioral_validation = "passed".into(); + assert!(rollback(&c, &o, &r).is_err()); + assert!(!o.exists()); + r.behavioral_validation = "not_run".into(); + r.edits[0].offset += 1; + assert!(rollback(&c, &o, &r).is_err()); + assert!(!o.exists()); + r.edits[0].offset -= 1; r.edits[0].before[0] ^= 1; assert!(rollback(&c, &o, &r).is_err()); assert!(!o.exists()); r.candidate_sha256 = "f".repeat(64); assert!(rollback(&c, &o, &r).is_err()); } + +#[cfg(unix)] +#[test] +fn inspect_cli_accepts_non_utf8_paths() { + use std::os::unix::ffi::OsStrExt; + use std::process::Command; + + let d = tempfile::tempdir().unwrap(); + let path = d + .path() + .join(std::ffi::OsStr::from_bytes(b"tensor-\xff.safetensors")); + fixture(&path, "F32"); + let result = Command::new(env!("CARGO_BIN_EXE_ruv-fann-wsp-tensor")) + .arg("inspect") + .arg(&path) + .output() + .unwrap(); + assert!( + result.status.success(), + "{}", + String::from_utf8_lossy(&result.stderr) + ); + let output: serde_json::Value = serde_json::from_slice(&result.stdout).unwrap(); + assert_eq!(output["checkpoint"]["tensors"]["weight"]["dtype"], "F32"); + assert_eq!(output["sha256"], fingerprint(&path).unwrap()); +} + +#[test] +fn file_handle_hash_is_stable_across_path_replacement() { + let d = tempfile::tempdir().unwrap(); + let path = d.path().join("tensor.safetensors"); + let moved = d.path().join("original.safetensors"); + fixture(&path, "F32"); + let original_hash = fingerprint(&path).unwrap(); + let mut file = File::open(&path).unwrap(); + inspect(&mut file).unwrap(); + + fs::rename(&path, &moved).unwrap(); + fixture(&path, "BF16"); + assert_ne!(fingerprint(&path).unwrap(), original_hash); + assert_eq!(fingerprint_file(&mut file).unwrap(), original_hash); +} + +#[test] +fn declared_header_and_json_limits_reject_before_unbounded_reads() { + let d = tempfile::tempdir().unwrap(); + let oversized_header = d.path().join("oversized.safetensors"); + let mut f = File::create(&oversized_header).unwrap(); + let declared = 16 * 1024 * 1024 + 8; + f.write_all(&(declared as u64).to_le_bytes()).unwrap(); + f.set_len(declared as u64 + 8).unwrap(); + assert!(inspect(&mut File::open(oversized_header).unwrap()).is_err()); + + let oversized_json = d.path().join("oversized.json"); + fs::write(&oversized_json, vec![b' '; 1024 * 1024 + 1]).unwrap(); + assert!(read_json::(&oversized_json).is_err()); +} #[test] fn dtype_rounding_is_not_an_edit() { let d = tempfile::tempdir().unwrap(); From 640b16ee8192cdaa4d7bb8bcec207c39357ca4bc Mon Sep 17 00:00:00 2001 From: ruv Date: Wed, 11 Nov 2026 18:44:13 -0500 Subject: [PATCH 06/10] Reject paired runs that omit the same prompt outputs --- crates/wsp-tensor/scripts/pan2_compare.py | 7 +++++-- crates/wsp-tensor/scripts/test_pan2_compare.py | 6 ++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/crates/wsp-tensor/scripts/pan2_compare.py b/crates/wsp-tensor/scripts/pan2_compare.py index 2659a318..884b95e6 100644 --- a/crates/wsp-tensor/scripts/pan2_compare.py +++ b/crates/wsp-tensor/scripts/pan2_compare.py @@ -274,6 +274,9 @@ def main(argv=None): prompt_data, prompt_bytes = json_snapshot(args.prompts) prompts_hash = hashlib.sha256(prompt_bytes).hexdigest() images = prompt_images(prompt_data, args.repo) + entries = (prompt_data if isinstance(prompt_data, list) else + next(prompt_data[key] for key in ("entries", "prompts", "data") if key in prompt_data)) + prompt_count = len(entries) cache = model_cache(args.hf_home) source = git(args.repo, "ls-files", "-s") if not shutil.which("nvidia-smi"): @@ -311,8 +314,8 @@ def unchanged(): unchanged() outputs = inventory(args.output / label) videos = {name: value["sha256"] for name, value in outputs.items() if name.endswith(".mp4")} - if not videos: - raise RuntimeError("inference produced no videos") + if len(videos) != prompt_count: + raise RuntimeError("inference must produce one video per prompt entry") if runs and videos.keys() != runs["baseline"]["videos"].keys(): raise RuntimeError("baseline/candidate video sets differ") # Upstream saves the actual parsed arguments beside each video. Require diff --git a/crates/wsp-tensor/scripts/test_pan2_compare.py b/crates/wsp-tensor/scripts/test_pan2_compare.py index 68887242..da28585e 100644 --- a/crates/wsp-tensor/scripts/test_pan2_compare.py +++ b/crates/wsp-tensor/scripts/test_pan2_compare.py @@ -204,6 +204,12 @@ def test_environment_removes_shell_injection_and_separates_kernel_cache(self): self.assertNotEqual(baseline['TRITON_CACHE_DIR'], candidate['TRITON_CACHE_DIR']) self.assertEqual(baseline['HF_HUB_OFFLINE'], '1') + def test_matching_but_incomplete_output_sets_cannot_qualify(self): + self.prompts.write_text('[{"prompt":"first"},{"prompt":"second"}]') + with self.assertRaisesRegex(RuntimeError, 'one video per prompt'): + self.invoke() + self.assertFalse((self.root / 'result/candidate').exists()) + def test_wrong_output_checkpoint_settings_rejected(self): def tamper(kw): out = self.root / 'result/baseline/clip_config.json' From 7bb38aaa4d268ba48f3654bd9906a73c282f5390 Mon Sep 17 00:00:00 2001 From: ruv Date: Wed, 11 Nov 2026 18:44:35 -0500 Subject: [PATCH 07/10] Resolve Windows CLI suffix without altering explicit binary overrides --- crates/wsp-tensor/scripts/e2e.py | 3 ++- crates/wsp-tensor/scripts/test_cli_scripts.py | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/crates/wsp-tensor/scripts/e2e.py b/crates/wsp-tensor/scripts/e2e.py index 61da166a..b237e6db 100644 --- a/crates/wsp-tensor/scripts/e2e.py +++ b/crates/wsp-tensor/scripts/e2e.py @@ -22,7 +22,8 @@ def binary(): target = pathlib.Path(os.environ.get("CARGO_TARGET_DIR", "target")) if not target.is_absolute(): target = ROOT / target - return (target / "release/ruv-fann-wsp-tensor").resolve(strict=True) + name = "ruv-fann-wsp-tensor.exe" if os.name == "nt" else "ruv-fann-wsp-tensor" + return (target / "release" / name).resolve(strict=True) def check(condition, message): diff --git a/crates/wsp-tensor/scripts/test_cli_scripts.py b/crates/wsp-tensor/scripts/test_cli_scripts.py index 276d7c88..ff4fc2f0 100644 --- a/crates/wsp-tensor/scripts/test_cli_scripts.py +++ b/crates/wsp-tensor/scripts/test_cli_scripts.py @@ -7,6 +7,7 @@ import subprocess import sys import tempfile +import types import unittest from unittest import mock @@ -31,6 +32,19 @@ def test_target_directory_override_resolves_standalone_binary(self): os.environ.pop('WSP_TENSOR_BIN', None) self.assertEqual(e2e.binary(), executable) + def test_windows_target_directory_resolves_exe_without_changing_explicit_path(self): + with tempfile.TemporaryDirectory() as directory: + executable = pathlib.Path(directory) / 'release/ruv-fann-wsp-tensor.exe' + executable.parent.mkdir() + executable.write_bytes(b'Windows path selection fixture only') + explicit = pathlib.Path(directory) / 'explicit-installed-name' + explicit.write_bytes(b'explicit selection fixture only') + windows = types.SimpleNamespace(name='nt', environ={'CARGO_TARGET_DIR': directory}) + with mock.patch.object(e2e, 'os', windows): + self.assertEqual(e2e.binary(), executable) + windows.environ['WSP_TENSOR_BIN'] = str(explicit) + self.assertEqual(e2e.binary(), explicit) + def test_optimized_python_rejects_wrong_native_candidate_bytes(self): # Fake external CLI reports correct hashes of a deliberately unchanged # candidate. The independent BF16 oracle must still fail under -O. From 604c5a112ecf5c9f74d84d20cb4b4f5ef376482c Mon Sep 17 00:00:00 2001 From: ruv Date: Wed, 11 Nov 2026 18:45:57 -0500 Subject: [PATCH 08/10] Qualify and publish source-bound tensor checkpoint archives through protected release --- .../workflows/protected-tensor-release.yml | 66 +++ .github/workflows/tensor-wsp.yml | 75 ++- README.md | 13 + crates/wsp-tensor/Cargo.toml | 10 +- crates/wsp-tensor/LICENSE-APACHE | 202 ++++++++ crates/wsp-tensor/LICENSE-MIT | 21 + crates/wsp-tensor/README.md | 28 +- crates/wsp-tensor/src/lib.rs | 6 +- crates/wsp-tensor/tests/checkpoints.rs | 29 ++ scripts/tensor-release.py | 454 ++++++++++++++++++ scripts/tests/test_tensor_release.py | 157 ++++++ 11 files changed, 1042 insertions(+), 19 deletions(-) create mode 100644 .github/workflows/protected-tensor-release.yml create mode 100644 crates/wsp-tensor/LICENSE-APACHE create mode 100644 crates/wsp-tensor/LICENSE-MIT create mode 100644 scripts/tensor-release.py create mode 100644 scripts/tests/test_tensor_release.py diff --git a/.github/workflows/protected-tensor-release.yml b/.github/workflows/protected-tensor-release.yml new file mode 100644 index 00000000..d2e62d7a --- /dev/null +++ b/.github/workflows/protected-tensor-release.yml @@ -0,0 +1,66 @@ +name: protected-tensor-release +on: + workflow_dispatch: + inputs: + expected_sha: + description: Exact unchanged main SHA qualified on release/** + required: true + type: string +permissions: + contents: read + issues: read + actions: read +concurrency: + group: protected-registry-release + cancel-in-progress: false +jobs: + publish: + runs-on: ubuntu-24.04 + timeout-minutes: 45 + permissions: + contents: write + issues: read + actions: read + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 + with: + persist-credentials: false + - uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 + with: + python-version: '3.12' + - uses: dtolnay/rust-toolchain@b725eda525362e7cf34af9a7243cac1e3f01a7ec + with: + toolchain: '1.99.1' + - name: Revalidate exact source and download the sealed pre-main candidate + env: + GH_TOKEN: ${{ github.token }} + EXPECTED_SHA: ${{ inputs.expected_sha }} + run: python3 scripts/tensor-release.py download --expected-sha "$EXPECTED_SHA" --directory "$RUNNER_TEMP/tensor-publication" + - name: Publish only the qualified immutable Cargo archive + env: + GH_TOKEN: ${{ github.token }} + EXPECTED_SHA: ${{ inputs.expected_sha }} + CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }} + run: python3 scripts/tensor-release.py publish --expected-sha "$EXPECTED_SHA" --directory "$RUNNER_TEMP/tensor-publication" + - name: Verify canonical bytes and fresh public Cargo and CLI consumers without registry credentials + env: + GH_TOKEN: ${{ github.token }} + EXPECTED_SHA: ${{ inputs.expected_sha }} + run: python3 scripts/tensor-release.py verify-public --expected-sha "$EXPECTED_SHA" --directory "$RUNNER_TEMP/tensor-publication" + - name: Create exact-source release and verify every downloaded asset + env: + GH_TOKEN: ${{ github.token }} + EXPECTED_SHA: ${{ inputs.expected_sha }} + run: python3 scripts/tensor-release.py github-release --expected-sha "$EXPECTED_SHA" --directory "$RUNNER_TEMP/tensor-publication" + - uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 + if: always() + with: + name: tensor-publication-${{ github.sha }} + path: | + ${{ runner.temp }}/tensor-publication/*.json + ${{ runner.temp }}/tensor-publication/*.crate + ${{ runner.temp }}/tensor-publication/fresh/*.log + ${{ runner.temp }}/tensor-publication/release-assets/* + ${{ runner.temp }}/tensor-publication/candidate/*.json + if-no-files-found: error + retention-days: 90 diff --git a/.github/workflows/tensor-wsp.yml b/.github/workflows/tensor-wsp.yml index 77c9f4b5..13b64b23 100644 --- a/.github/workflows/tensor-wsp.yml +++ b/.github/workflows/tensor-wsp.yml @@ -1,25 +1,78 @@ name: Tensor WSP checkpoint validation on: pull_request: - paths: ['crates/wsp-tensor/**', '.github/workflows/tensor-wsp.yml'] + paths: ['crates/wsp-tensor/**', 'scripts/tensor-release.py', 'scripts/tests/test_tensor_release.py', '.github/workflows/tensor-wsp.yml', '.github/workflows/protected-tensor-release.yml'] push: - branches: [main] - paths: ['crates/wsp-tensor/**', '.github/workflows/tensor-wsp.yml'] + branches: [main, 'release/**'] + paths: ['crates/wsp-tensor/**', 'scripts/tensor-release.py', 'scripts/tests/test_tensor_release.py', '.github/workflows/tensor-wsp.yml', '.github/workflows/protected-tensor-release.yml'] + workflow_dispatch: {} permissions: contents: read +concurrency: + group: tensor-wsp-${{ github.ref }} + cancel-in-progress: true jobs: - checkpoint-validation: - runs-on: ubuntu-latest + qualify: + runs-on: ubuntu-24.04 + timeout-minutes: 30 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 + with: + persist-credentials: false + - uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 + with: + python-version: '3.12' + - uses: dtolnay/rust-toolchain@b725eda525362e7cf34af9a7243cac1e3f01a7ec + with: + toolchain: '1.99.1' + components: clippy, rustfmt + - name: Install pinned MSRV and advisory tool + run: | + rustup toolchain install 1.81.0 --profile minimal + cargo install cargo-audit --version '=0.22.2' --locked + - name: Exercise release rejection guards + run: python3 -m unittest discover -s scripts/tests -p 'test_tensor_release.py' -v + - name: Seal source-bound archive after native, packed and independent CLI qualification + env: + EXPECTED_SHA: ${{ github.sha }} + run: python3 scripts/tensor-release.py seal --expected-sha "$EXPECTED_SHA" --directory "$RUNNER_TEMP/tensor-seal" + - uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 + with: + name: tensor-candidate-${{ github.sha }} + path: | + ${{ runner.temp }}/tensor-seal/*.crate + ${{ runner.temp }}/tensor-seal/*.json + ${{ runner.temp }}/tensor-seal/*.log + if-no-files-found: error + retention-days: 90 + - name: Preserve failing qualification logs + if: failure() + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 + with: + name: tensor-failure-${{ github.sha }} + path: ${{ runner.temp }}/tensor-seal/*.log + if-no-files-found: ignore + portability: + strategy: + fail-fast: false + matrix: + os: [windows-latest, macos-latest] + runs-on: ${{ matrix.os }} + timeout-minutes: 20 defaults: run: working-directory: crates/wsp-tensor + shell: bash steps: - - uses: actions/checkout@v4 - - uses: dtolnay/rust-toolchain@stable + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 + with: + persist-credentials: false + - uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 + with: + python-version: '3.12' + - uses: dtolnay/rust-toolchain@b725eda525362e7cf34af9a7243cac1e3f01a7ec with: - components: rustfmt, clippy - - run: cargo fmt --check - - run: cargo clippy --locked --all-targets -- -D warnings - - run: cargo test --locked + toolchain: '1.99.1' + - run: cargo test --locked --test checkpoints -- --test-threads=1 - run: cargo build --release --locked - run: python3 scripts/e2e.py diff --git a/README.md b/README.md index d7775fe6..8daedbd2 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,19 @@ dependency pins. Examples below retain the corresponding runtime generations. Each family has a defined scope. Forecasting currently implements a univariate RNN and simple baselines; LSTM/GRU forecasting architectures return unsupported errors. Agent assignment records scheduling decisions; external task execution depends on the integration you supply. CUDA translation emits source and does not execute a GPU kernel. The native OpenCV bridge calls an installed C++ library, while the portable packages implement a smaller set of operations. +## Edit Safetensors checkpoints in Rust + +[`ruv-fann-wsp-tensor`](crates/wsp-tensor/README.md) provides a standalone Rust +library and CLI for bounded F32, F16 and BF16 checkpoint edits. It streams a private +copy, checks the exact baseline hash, applies at most 128 replacements, and restores +byte-identical originals from hash-bound rollback receipts. Existing files are never +overwritten. Install with `cargo install ruv-fann-wsp-tensor --version 0.1.0 --locked`. + +This is an offline checkpoint utility. The accompanying paired PAN2 experiment +harness records input/output provenance; GPU generation and model quality gains +have not been validated. See its guide for limits, independent evaluation and +source/packed/public package verification. + ## Weight-space programming in Node.js and browsers A neural network's weights are the numbers that control how inputs become diff --git a/crates/wsp-tensor/Cargo.toml b/crates/wsp-tensor/Cargo.toml index 4ba7089a..e140c56a 100644 --- a/crates/wsp-tensor/Cargo.toml +++ b/crates/wsp-tensor/Cargo.toml @@ -3,8 +3,16 @@ name = "ruv-fann-wsp-tensor" version = "0.1.0" edition = "2021" license = "MIT OR Apache-2.0" -publish = false +publish = ["crates-io"] description = "Bounded, streaming Safetensors checkpoint edits for WSP experiments" +rust-version = "1.81" +repository = "https://github.com/ruvnet/ruv-FANN" +homepage = "https://ruvnet.github.io/ruv-FANN/" +documentation = "https://docs.rs/ruv-fann-wsp-tensor" +readme = "README.md" +keywords = ["safetensors", "checkpoint", "model-editing", "weight-space", "machine-learning"] +categories = ["science", "command-line-utilities"] +include = ["src/**", "tests/**", "scripts/**", "Cargo.toml", "Cargo.lock", "README.md", "LICENSE-MIT", "LICENSE-APACHE"] [workspace] diff --git a/crates/wsp-tensor/LICENSE-APACHE b/crates/wsp-tensor/LICENSE-APACHE new file mode 100644 index 00000000..0060a69b --- /dev/null +++ b/crates/wsp-tensor/LICENSE-APACHE @@ -0,0 +1,202 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (which shall not include the combination of separate works, which + are aggregated in a volume of a storage or distribution medium, + without themselves constituting additional works under the License). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based upon (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and derivative works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control + systems, and issue tracking systems that are managed by, or on behalf + of, the Licensor for the purpose of discussing and improving the Work, + but excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to use, reproduce, modify, merge, publish, + distribute, sublicense, and/or sell copies of the Work, and to + permit persons to whom the Work is furnished to do so, subject to + the following conditions: + + The above copyright notice and this permission notice shall be + included in all copies or substantial portions of the Work. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, trademark, patent, + attribution and other notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright notice and license terms for Your + uses, additions or Derivative Works; provided that You use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Support. Unless required by applicable law or + agreed to in writing, Licensor provides the Work "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same page as the copyright notice for easier identification within + third-party archives. + + Copyright 2024 rUv Contributors + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. \ No newline at end of file diff --git a/crates/wsp-tensor/LICENSE-MIT b/crates/wsp-tensor/LICENSE-MIT new file mode 100644 index 00000000..2dd524ac --- /dev/null +++ b/crates/wsp-tensor/LICENSE-MIT @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2025 rUv + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/crates/wsp-tensor/README.md b/crates/wsp-tensor/README.md index bc9365db..147bfec9 100644 --- a/crates/wsp-tensor/README.md +++ b/crates/wsp-tensor/README.md @@ -1,11 +1,20 @@ # Rust WSP tensor checkpoint experiments -Standalone, unpublished backend for bounded direct edits to Safetensors shards. +Standalone Rust library and CLI for bounded direct edits to Safetensors shards. This extends the WSP workflow concept. It does not replace or import the small dense model schema in `ruv-fann-wsp`, and does not implement PAN2 inference. ## Build and verify +Install the CLI from crates.io with its reviewed dependency lock: + +```sh +cargo install ruv-fann-wsp-tensor --version 0.1.0 --locked +``` + +For the library, add `ruv-fann-wsp-tensor = "0.1.0"` to your Cargo dependencies. +The host MSRV is Rust 1.81. Source builds from this directory: + ```sh cargo test --locked cargo build --release --locked @@ -75,6 +84,7 @@ python3 scripts/pan2_compare.py \ --candidate /models/candidate/transformer \ --receipt /evaluation/receipt.json \ --prompts /evaluation/prompts.json \ + --hf-home /models/offline-huggingface \ --output /evaluation/pan2-comparison ``` @@ -82,9 +92,12 @@ The harness records checkpoint and output hashes, prompt hash, fixed seed, upstream revision and timings. It uses fresh output directories, checks inputs again after inference, and terminates the process group on timeout or error. This is a paired generation harness, not a validated physics evaluator. -Prompt-referenced images and external text encoder/VAE caches must be separately -pinned and kept immutable by the operator. Those dependencies are not hashed by -this initial harness. Review upstream licensing for each component. +The harness hashes prompt images, all checkpoint files, the receipt and a dedicated +offline Hugging Face data directory. That directory may contain only `hub/` and/or +`assets/`; credential files are rejected. Kernel caches are fresh for each run. +Provision these model components separately and review their upstream licenses. +CPU tests use an explicitly mocked launcher; they do not establish PAN2 inference +or generation quality on a GPU. No automatic promotion is implemented. Score action adherence and physical consistency against real observations on an unseen scenario set before choosing @@ -112,8 +125,11 @@ and offsets checked for contiguous coverage. Checkpoints are local trusted-user files, not a public file upload service. Use filesystem permissions and a job sandbox before exposing this CLI to other tenants. Filesystem failures, process termination and unavailable disk space can interrupt a run; the source remains -untouched. Receipts are unsigned. Dependency advisory scanning is a separate -release check. This crate is `publish = false` until review and GPU validation. +untouched. Receipts are unsigned. The release workflow audits the standalone dependency lock with warnings denied. +Its source-bound archive is tested before publication, then downloaded from the +public registry and exercised through fresh library and installed CLI consumers. +GPU quality evaluation remains a separate experiment, not a capability of this +checkpoint file utility. Acceptance: candidates load with the official reader; unchanged tensors retain their bytes; invalid and stale edits reject; rollback restores the entire original diff --git a/crates/wsp-tensor/src/lib.rs b/crates/wsp-tensor/src/lib.rs index c2651a23..b06db850 100644 --- a/crates/wsp-tensor/src/lib.rs +++ b/crates/wsp-tensor/src/lib.rs @@ -355,7 +355,11 @@ pub fn rollback(source: &Path, dest: &Path, r: &Receipt) -> Result { let dtype = dtype.ok_or("undo is not an editable tensor element")?; let before = decode(dtype, &e.before)?; let after = decode(dtype, &e.after)?; - if !before.is_finite() || !after.is_finite() || (after - before).abs() > 1.0 { + if !before.is_finite() + || !after.is_finite() + || after.abs() > 1_000_000.0 + || (after - before).abs() > 1.0 + { return fail("invalid undo values"); } for i in e.offset..e.offset + n as u64 { diff --git a/crates/wsp-tensor/tests/checkpoints.rs b/crates/wsp-tensor/tests/checkpoints.rs index 38df2322..4eb5ff25 100644 --- a/crates/wsp-tensor/tests/checkpoints.rs +++ b/crates/wsp-tensor/tests/checkpoints.rs @@ -238,3 +238,32 @@ fn preserve_untouched_tensor_and_metadata() { assert_eq!(&old[..old.len() - 8], &new[..new.len() - 8]); assert_eq!(&old[old.len() - 4..], &new[new.len() - 4..]); } + +#[test] +fn forged_receipt_cannot_exceed_apply_replacement_bound() { + let d = tempfile::tempdir().unwrap(); + let base = d.path().join("base"); + let candidate = d.path().join("candidate"); + let restored = d.path().join("restored"); + let header = r#"{"weight":{"dtype":"F32","shape":[1],"data_offsets":[0,4]}}"#; + let before = 1_000_000.5f32.to_le_bytes(); + let after = 1_000_001.0f32.to_le_bytes(); + write_raw(&base, header, &before); + write_raw(&candidate, header, &after); + let offset = inspect(&mut File::open(&candidate).unwrap()) + .unwrap() + .data_start; + let forged = Receipt { + schema: "ruv-fann.tensor-receipt/1".into(), + base_sha256: fingerprint(&base).unwrap(), + candidate_sha256: fingerprint(&candidate).unwrap(), + edits: vec![Undo { + offset, + before: before.to_vec(), + after: after.to_vec(), + }], + behavioral_validation: "not_run".into(), + }; + assert!(rollback(&candidate, &restored, &forged).is_err()); + assert!(!restored.exists()); +} diff --git a/scripts/tensor-release.py b/scripts/tensor-release.py new file mode 100644 index 00000000..e003d742 --- /dev/null +++ b/scripts/tensor-release.py @@ -0,0 +1,454 @@ +#!/usr/bin/env python3 +"""Source-bound tensor crate qualification and protected exact-archive publication.""" +import argparse +import hashlib +import importlib.util +import json +import os +from pathlib import Path +import re +import shutil +import subprocess +import tarfile +import time +import tomllib +import urllib.request +import zipfile + +ROOT = Path(__file__).resolve().parents[1] +PACKAGE = ROOT / 'crates/wsp-tensor' +REPO = 'ruvnet/ruv-FANN' +NAME, VERSION = 'ruv-fann-wsp-tensor', '0.1.0' +ARCHIVE = f'{NAME}-{VERSION}.crate' +TAG = f'{NAME}-v{VERSION}' +WORKFLOW = '.github/workflows/tensor-wsp.yml' + +def load(name, file): + spec = importlib.util.spec_from_file_location(name, ROOT / 'scripts' / file) + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + return module + +registry = load('tensor_registry', 'publish-candidates.py') +archive_tools = load('tensor_archive', 'verify-release.py') +ci_tools = load('tensor_ci', 'select-rust-candidate.py') +sha = archive_tools.digest + +def gh(endpoint): + return ci_tools.gh(endpoint) + +def identity(source): + if not re.fullmatch('[a-f0-9]{40}', source): + raise ValueError('Full source SHA required') + if subprocess.check_output(['git', 'rev-parse', 'HEAD'], cwd=ROOT, text=True).strip() != source: + raise ValueError('Checkout SHA mismatch') + if subprocess.check_output(['git', 'status', '--porcelain'], cwd=ROOT): + raise ValueError('Clean immutable checkout required') + +def protected(source): + identity(source) + keys = ('GITHUB_ACTIONS', 'GITHUB_WORKFLOW', 'GITHUB_REF', 'GITHUB_SHA') + if tuple(os.environ.get(k) for k in keys) != ('true', 'protected-tensor-release', 'refs/heads/main', source): + raise ValueError('Only protected tensor workflow may publish') + if gh(f'repos/{REPO}/git/ref/heads/main')['object']['sha'] != source: + raise ValueError('Main moved') + if gh(f'repos/{REPO}/issues?state=open&labels=release-blocker&per_page=1'): + raise ValueError('Open release blockers') + +def command_plan(base): + packed = base / 'extracted' / f'{NAME}-{VERSION}' + target = base / 'build' + return [ + ('fmt', ['cargo', 'fmt', '--check'], PACKAGE, None), + ('clippy', ['cargo', 'clippy', '--locked', '--all-targets', '--', '-D', 'warnings'], PACKAGE, None), + ('source-tests', ['cargo', 'test', '--locked', '--test', 'checkpoints', '--', '--test-threads=1'], PACKAGE, 'rust'), + ('msrv', ['cargo', '+1.81.0', 'check', '--locked', '--all-targets'], PACKAGE, None), + ('audit', ['cargo', 'audit', '--file', 'Cargo.lock', '--deny', 'warnings'], PACKAGE, None), + ('python-tests', ['python3', '-m', 'unittest', 'discover', '-s', 'scripts', '-p', 'test_*.py', '-v'], PACKAGE, 'python'), + ('package', ['cargo', 'package', '--locked', '--no-verify', '--target-dir', str(base / 'package')], PACKAGE, None), + ('packed-tests', ['cargo', 'test', '--locked', '--test', 'checkpoints', '--', '--test-threads=1'], packed, 'rust'), + ('packed-build', ['cargo', 'build', '--release', '--locked'], packed, None), + ('packed-cli', ['python3', 'scripts/e2e.py'], packed, 'cli'), + ] + +def evidence(output, kind): + if kind == 'rust': + summaries = re.findall(r'^test result: ok\. (\d+) passed; (\d+) failed; (\d+) ignored; (\d+) measured; (\d+) filtered out;', output, re.M) + if len(summaries) != 1 or int(summaries[0][0]) < 10 or any(map(int, summaries[0][1:])): + raise ValueError('Nonzero complete tensor integration suite required') + return {'testsPassed': int(summaries[0][0])} + if kind == 'python': + rows = re.findall(r'^Ran (\d+) tests? in ', output, re.M) + if len(rows) != 1 or int(rows[0]) == 0 or not re.search(r'^OK\s*$', output, re.M): + raise ValueError('Nonzero successful Python tests with no skips required') + return {'testsPassed': int(rows[0])} + if kind == 'cli': + data = json.loads(output) + if (data.get('schema') != 'ruv-fann.tensor-e2e/1' or data.get('roundtrip') != 'passed' + or data.get('overwrite_rejected') is not True or data.get('pan2_inference') != 'not_run'): + raise ValueError('Actual independent CLI roundtrip missing') + return data + return {} + +def portable(value, base): + text = str(value) + for prefix, alias in ((base, '${CANDIDATE}'), (ROOT, '${SOURCE}')): + if text == str(prefix) or text.startswith(str(prefix) + '/'): + return alias + text[len(str(prefix)):] + return text + +def execute(role, argv, cwd, kind, base, commands): + env = dict(os.environ, CARGO_TARGET_DIR=str(base / 'build'), CARGO_BUILD_JOBS='2', CARGO_INCREMENTAL='0', + WSP_TENSOR_BIN=str(base / 'build/release' / NAME)) + result = subprocess.run(argv, cwd=cwd, env=env, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True) + log = base / f'{role}.log' + log.write_text(result.stdout) + if result.returncode: + print(result.stdout[-12000:], flush=True) + raise ValueError(f'{role} failed ({result.returncode})') + commands.append({'role': role, 'argv': [portable(x, base) for x in argv], 'cwd': portable(cwd, base), + 'log': log.name, 'logSha256': sha(log), 'exitCode': result.returncode, + 'evidence': evidence(result.stdout, kind)}) + print(f'{role}: passed', flush=True) + +def normalized_manifest(actual): + source = tomllib.loads((PACKAGE / 'Cargo.toml').read_text()) + expected = {key: value for key, value in source.items() if key != 'workspace'} + expected['package'] = dict(source['package'], build=False, autolib=False, autobins=False, + autoexamples=False, autotests=False, autobenches=False) + for section in ('dependencies', 'dev-dependencies', 'build-dependencies'): + if section in expected: + expected[section] = {key: {'version': value} if isinstance(value, str) else value + for key, value in expected[section].items()} + expected['lib'] = {'name': NAME.replace('-', '_'), 'path': 'src/lib.rs'} + expected['bin'] = [{'name': NAME, 'path': 'src/main.rs'}] + expected['test'] = [{'name': path.stem, 'path': 'tests/' + path.name} + for path in sorted((PACKAGE / 'tests').glob('*.rs'))] + if actual != expected: + raise ValueError('Normalized Cargo manifest differs from reviewed package semantics') + + +def archive_binding(archive, source): + info = archive_tools.archive_info(archive) + if (info['name'], info['version']) != (NAME, VERSION): + raise ValueError('Package generation mismatch') + with tarfile.open(archive) as stream: + entries = stream.getmembers() + if len({m.name for m in entries}) != len(entries): + raise ValueError('Duplicate Cargo archive member') + members = {m.name: m for m in entries if m.isfile()} + prefix = f'{NAME}-{VERSION}/' + normalized_manifest(tomllib.loads(stream.extractfile(prefix + 'Cargo.toml').read().decode())) + vcs = json.load(stream.extractfile(prefix + '.cargo_vcs_info.json')) + if vcs['git'].get('sha1') != source or vcs['git'].get('dirty', False) or vcs.get('path_in_vcs') != 'crates/wsp-tensor': + raise ValueError('Archive VCS identity mismatch') + for name, member in members.items(): + relative = name.removeprefix(prefix) + if relative in ('Cargo.toml', '.cargo_vcs_info.json'): + continue + original = 'Cargo.toml' if relative == 'Cargo.toml.orig' else relative + path = PACKAGE / original + if not path.is_file() or path.is_symlink() or stream.extractfile(member).read() != path.read_bytes(): + raise ValueError('Archive/source payload mismatch: ' + relative) + tracked = subprocess.check_output(['git', 'ls-files', '--', 'crates/wsp-tensor'], cwd=ROOT, text=True).splitlines() + expected = {p.removeprefix('crates/wsp-tensor/') for p in tracked + if p.startswith(('crates/wsp-tensor/src/', 'crates/wsp-tensor/tests/', 'crates/wsp-tensor/scripts/')) + or p.rsplit('/', 1)[-1] in ('Cargo.lock', 'README.md', 'LICENSE-MIT', 'LICENSE-APACHE')} + actual = {name.removeprefix(prefix) for name in members} + if not expected.issubset(actual): + raise ValueError('Tracked package source absent from archive') + return info + +def validate(base, source): + identity(source) + receipt = json.loads((base / 'candidate.json').read_text()) + if receipt.get('schema') != 'ruv-fann.tensor-candidate/1' or receipt.get('sourceSha') != source: + raise ValueError('Candidate source/schema mismatch') + if receipt.get('name') != NAME or receipt.get('version') != VERSION or receipt.get('archive') != ARCHIVE: + raise ValueError('Candidate package mismatch') + archive = base / ARCHIVE + archive_binding(archive, source) + if sha(archive) != receipt['archiveSha256']: + raise ValueError('Archive seal mismatch') + commands = receipt['commands'] + plan = command_plan(base) + if len(commands) != len(plan): + raise ValueError('Incomplete command proof') + for row, (role, argv, cwd, kind) in zip(commands, plan): + if type(row.get('exitCode')) is not int: + raise ValueError('Command exit must be an integer') + if (row.get('role'), row.get('argv'), row.get('cwd'), row.get('exitCode'), row.get('log')) != ( + role, [portable(x, base) for x in argv], portable(cwd, base), 0, f'{role}.log'): + raise ValueError('Command proof mismatch') + log = base / row['log'] + if sha(log) != row['logSha256'] or evidence(log.read_text(), kind) != row['evidence']: + raise ValueError('Log evidence mismatch') + return receipt + +def seal(base, source): + identity(source) + base.mkdir(parents=True, exist_ok=False) + commands = [] + for role, argv, cwd, kind in command_plan(base): + execute(role, argv, cwd, kind, base, commands) + if role == 'package': + shutil.copyfile(base / 'package/package' / ARCHIVE, base / ARCHIVE) + archive_binding(base / ARCHIVE, source) + archive_tools.extract_root(base / ARCHIVE, base / 'extracted') + identity(source) + receipt = {'schema': 'ruv-fann.tensor-candidate/1', 'sourceSha': source, 'name': NAME, 'version': VERSION, + 'archive': ARCHIVE, 'archiveSha256': sha(base / ARCHIVE), 'commands': commands, + 'pan2GpuInference': 'not_run', 'behavioralImprovement': 'not_established'} + (base / 'candidate.json').write_text(json.dumps(receipt, indent=2) + '\n') + validate(base, source) + +def validate_ci(run, jobs, artifacts, source): + if (run.get('head_sha') != source or run.get('event') != 'push' or not run.get('head_branch', '').startswith('release/') + or run.get('path') != WORKFLOW or run.get('status') != 'completed' or run.get('conclusion') != 'success'): + raise ValueError('Successful exact-source release-branch tensor workflow required') + names = {j.get('name') for j in jobs} + if not {'qualify', 'portability (windows-latest)', 'portability (macos-latest)'}.issubset(names) or any( + j.get('status') != 'completed' or j.get('conclusion') != 'success' for j in jobs): + raise ValueError('Missing/pending/failed tensor qualification job') + matches = [a for a in artifacts if a.get('name') == f'tensor-candidate-{source}' and not a.get('expired')] + if len(matches) != 1 or not re.fullmatch('sha256:[a-f0-9]{64}', matches[0].get('digest', '')): + raise ValueError('Unique digest-bound candidate required') + return matches[0] + +def download(base, source): + protected(source) + runs = ci_tools.pages(f'repos/{REPO}/actions/workflows/tensor-wsp.yml/runs?head_sha={source}&event=push', 'workflow_runs') + runs = [r for r in runs if r.get('head_branch', '').startswith('release/') and r.get('conclusion') == 'success'] + if not runs: + raise ValueError('No successful tensor preflight') + run = max(runs, key=lambda r: r['id']) + jobs = ci_tools.pages(f'repos/{REPO}/actions/runs/{run["id"]}/jobs?filter=latest', 'jobs') + artifacts = ci_tools.pages(f'repos/{REPO}/actions/runs/{run["id"]}/artifacts', 'artifacts') + item = validate_ci(run, jobs, artifacts, source) + # Preserve the repository-wide pre-main regression gate as well. + core_runs = ci_tools.pages(f'repos/{REPO}/actions/workflows/ci.yml/runs?head_sha={source}&event=push', 'workflow_runs') + core_runs = [r for r in core_runs if r.get('head_branch', '').startswith('release/') and r.get('conclusion') == 'success'] + if not core_runs: + raise ValueError('No full exact-source repository CI') + core = max(core_runs, key=lambda r: r['id']) + core_receipt = ci_tools.validate_run(core, + ci_tools.pages(f'repos/{REPO}/actions/runs/{core["id"]}/jobs?filter=latest', 'jobs'), + ci_tools.pages(f'repos/{REPO}/actions/runs/{core["id"]}/artifacts', 'artifacts'), source) + base.mkdir(parents=True, exist_ok=False) + path = base / 'candidate.zip' + with path.open('xb') as stream: + subprocess.run(['gh', 'api', f'repos/{REPO}/actions/artifacts/{item["id"]}/zip?release_probe={time.time_ns()}'], stdout=stream, check=True) + if 'sha256:' + sha(path) != item['digest']: + raise ValueError('GitHub ZIP seal mismatch') + with zipfile.ZipFile(path) as stream: + names = stream.namelist() + if len(names) != len(set(names)) or any(Path(n).is_absolute() or '..' in Path(n).parts or '\\' in n for n in names): + raise ValueError('Unsafe candidate ZIP') + stream.extractall(base / 'candidate') + validate(base / 'candidate', source) + (base / 'ci.json').write_text(json.dumps({'sourceSha': source, 'run': run, 'jobs': jobs, 'artifact': item, 'repositoryCi': core_receipt}, indent=2)) + +def validated_inputs(base, source): + protected(source) + ci = json.loads((base / 'ci.json').read_text()) + if ci.get('sourceSha') != source: + raise ValueError('Downloaded CI source mismatch') + run_id = ci['run']['id'] + run = gh(f'repos/{REPO}/actions/runs/{run_id}') + item = validate_ci(run, + ci_tools.pages(f'repos/{REPO}/actions/runs/{run_id}/jobs?filter=latest', 'jobs'), + ci_tools.pages(f'repos/{REPO}/actions/runs/{run_id}/artifacts', 'artifacts'), source) + if item['id'] != ci['artifact']['id'] or item['digest'] != ci['artifact']['digest']: + raise ValueError('Downloaded artifact differs from accepted CI') + archive = base / 'candidate.zip' + if 'sha256:' + sha(archive) != item['digest']: + raise ValueError('Original GitHub ZIP seal changed') + expected = {ARCHIVE, 'candidate.json', *[role + '.log' for role, *_ in command_plan(base / 'candidate')]} + with zipfile.ZipFile(archive) as stream: + if set(stream.namelist()) != expected or len(stream.namelist()) != len(expected): + raise ValueError('Unexpected candidate ZIP inventory') + for name in expected: + path = base / 'candidate' / name + if path.is_symlink() or path.read_bytes() != stream.read(name): + raise ValueError('Candidate bytes changed after accepted CI download') + core_id = ci['repositoryCi']['runId'] + ci_tools.validate_run(gh(f'repos/{REPO}/actions/runs/{core_id}'), + ci_tools.pages(f'repos/{REPO}/actions/runs/{core_id}/jobs?filter=latest', 'jobs'), + ci_tools.pages(f'repos/{REPO}/actions/runs/{core_id}/artifacts', 'artifacts'), source) + return validate(base / 'candidate', source) + + +def publish(base, source): + receipt = validated_inputs(base, source) + archive = base / 'candidate' / ARCHIVE + digest = receipt['archiveSha256'] + public = registry.downloaded(NAME, VERSION) + if public is not None and registry.sha(public) != digest: + raise ValueError('Immutable version already has different bytes') + if public is None: + registry.confirm_version_absent(NAME, VERSION) + token = os.environ.get('CARGO_REGISTRY_TOKEN') + if not token: + raise ValueError('Missing Cargo token') + status = registry.publish(archive, registry.metadata(archive), digest, token) + if status not in ('already-verified', 'published-and-byte-verified'): + raise ValueError('Exact byte publication required') + (base / 'publication.json').write_text(json.dumps({'sourceSha': source, 'archiveSha256': digest, 'name': NAME, 'version': VERSION, 'status': status}, indent=2)) + +def verify_public(base, source): + receipt = validated_inputs(base, source) + public = registry.downloaded(NAME, VERSION) + if public is None or registry.sha(public) != receipt['archiveSha256']: + raise ValueError('Canonical public archive mismatch') + (base / 'public.crate').write_bytes(public) + fresh = base / 'fresh' + fresh.mkdir() + packed = archive_tools.extract_root(base / 'public.crate', fresh / 'extracted') + commands = [] + for role, argv, kind in [ + ('public-tests', ['cargo', 'test', '--locked', '--test', 'checkpoints', '--', '--test-threads=1'], 'rust'), + ('public-build', ['cargo', 'build', '--release', '--locked'], None), + ('public-cli', ['python3', 'scripts/e2e.py'], 'cli')]: + execute(role, argv, packed, kind, fresh, commands) + # Resolve as a real public registry consumer in a fresh Cargo home, then verify its lock checksum. + consumer = fresh / 'consumer' + consumer.mkdir() + (consumer / 'src').mkdir() + (consumer / 'Cargo.toml').write_text(f'[package]\nname="tensor-public-consumer"\nversion="0.0.0"\nedition="2021"\n[dependencies]\n{NAME}="={VERSION}"\n') + fixture = fresh / 'registry-fixture.safetensors' + import struct + header = json.dumps({'w': {'dtype': 'F32', 'shape': [1], 'data_offsets': [0, 4]}}).encode() + header += b' ' * (-len(header) % 8) + fixture.write_bytes(struct.pack('=std::env::args().collect();let base=Path::new(&args[1]);let candidate=base.with_extension("candidate");let restored=base.with_extension("restored"); +let original=fs::read(base).unwrap();let p=Patch{schema:"ruv-fann.tensor-patch/1".into(),base_sha256:fingerprint(base).unwrap(),edits:vec![Edit{tensor:"w".into(),index:0,value:0.75}]}; +let receipt=apply(base,&candidate,&p).unwrap();let bytes=fs::read(&candidate).unwrap();assert_eq!(&bytes[bytes.len()-4..],&0.75f32.to_le_bytes()); +assert_eq!(fs::read(base).unwrap(),original);assert!(apply(base,&candidate,&p).is_err());rollback(&candidate,&restored,&receipt).unwrap();assert_eq!(fs::read(restored).unwrap(),original); +println!("Public tensor API consumer passed");}''') + env = dict(os.environ, CARGO_HOME=str(fresh / 'cargo-home'), CARGO_TARGET_DIR=str(fresh / 'consumer-target')) + result = subprocess.run(['cargo', 'run', '--manifest-path', str(consumer / 'Cargo.toml'), '--', str(fixture)], cwd=consumer, env=env, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True) + (fresh / 'registry-consumer.log').write_text(result.stdout) + if result.returncode or result.stdout.splitlines().count('Public tensor API consumer passed') != 1: + raise ValueError('Fresh registry consumer failed') + import tomllib + rows = tomllib.loads((consumer / 'Cargo.lock').read_text())['package'] + rows = [r for r in rows if r['name'] == NAME] + if len(rows) != 1 or rows[0].get('checksum') != receipt['archiveSha256'] or rows[0].get('source') != 'registry+https://github.com/rust-lang/crates.io-index': + raise ValueError('Fresh registry checksum/source mismatch') + installed = fresh / 'installed' + result = subprocess.run(['cargo', 'install', '--locked', '--root', str(installed), '--version', '=' + VERSION, NAME], + cwd=fresh, env=env, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True) + (fresh / 'registry-install.log').write_text(result.stdout) + if result.returncode: + raise ValueError('Fresh registry CLI install failed') + result = subprocess.run(['python3', str(packed / 'scripts/e2e.py')], env=dict(env, WSP_TENSOR_BIN=str(installed / 'bin' / NAME)), + stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True) + (fresh / 'registry-cli.log').write_text(result.stdout) + if result.returncode: + raise ValueError('Fresh installed registry CLI failed') + installed_evidence = evidence(result.stdout, 'cli') + (base / 'public-consumers.json').write_text(json.dumps({'sourceSha': source, 'archiveSha256': receipt['archiveSha256'], 'commands': commands, + 'registryConsumer': {'exitCode': 0, 'logSha256': sha(fresh / 'registry-consumer.log'), 'checksum': rows[0]['checksum']}, + 'installedCli': {'exitCode': 0, 'installLogSha256': sha(fresh / 'registry-install.log'), 'logSha256': sha(fresh / 'registry-cli.log'), 'evidence': installed_evidence}}, indent=2)) + +def validate_public(base, source): + receipt = validated_inputs(base, source) + digest = receipt['archiveSha256'] + published = json.loads((base / 'publication.json').read_text()) + proof = json.loads((base / 'public-consumers.json').read_text()) + if (published.get('sourceSha') != source or published.get('archiveSha256') != digest + or published.get('name') != NAME or published.get('version') != VERSION + or published.get('status') not in ('already-verified', 'published-and-byte-verified') + or proof.get('sourceSha') != source or proof.get('archiveSha256') != digest): + raise ValueError('Publication proof mismatch') + if sha(base / 'public.crate') != digest: + raise ValueError('Public archive changed') + fresh = base / 'fresh' + roles = [('public-tests', 'rust'), ('public-build', None), ('public-cli', 'cli')] + if len(proof['commands']) != len(roles): + raise ValueError('Incomplete public qualification') + for row, (role, kind) in zip(proof['commands'], roles): + log = fresh / (role + '.log') + if (row.get('role') != role or type(row.get('exitCode')) is not int or row['exitCode'] != 0 + or row.get('log') != log.name or sha(log) != row['logSha256'] + or evidence(log.read_text(), kind) != row['evidence']): + raise ValueError('Public command proof mismatch') + registry_proof = proof['registryConsumer'] + if (type(registry_proof.get('exitCode')) is not int or registry_proof['exitCode'] != 0 + or registry_proof.get('checksum') != digest + or sha(fresh / 'registry-consumer.log') != registry_proof['logSha256'] + or (fresh / 'registry-consumer.log').read_text().splitlines().count('Public tensor API consumer passed') != 1): + raise ValueError('Public registry consumer proof mismatch') + cli = proof['installedCli'] + if (type(cli.get('exitCode')) is not int or cli['exitCode'] != 0 + or sha(fresh / 'registry-install.log') != cli['installLogSha256'] + or sha(fresh / 'registry-cli.log') != cli['logSha256'] + or evidence((fresh / 'registry-cli.log').read_text(), 'cli') != cli['evidence']): + raise ValueError('Public installed CLI proof mismatch') + return receipt + + +def github_release(base, source): + receipt = validate_public(base, source) + public = registry.downloaded(NAME, VERSION) + if public is None or registry.sha(public) != receipt['archiveSha256']: + raise ValueError('Canonical bytes changed before GitHub release') + assets = base / 'release-assets' + assets.mkdir() + shutil.copyfile(base / 'public.crate', assets / ARCHIVE) + for name in ('ci.json', 'publication.json', 'public-consumers.json'): + shutil.copyfile(base / name, assets / name) + for path in (base / 'candidate').glob('*.log'): + shutil.copyfile(path, assets / ('candidate-' + path.name)) + shutil.copyfile(base / 'candidate/candidate.json', assets / 'candidate.json') + for path in (base / 'fresh').glob('*.log'): + shutil.copyfile(path, assets / path.name) + manifest = {p.name: sha(p) for p in assets.iterdir()} + (assets / 'SHA256SUMS').write_text(''.join(f'{digest} {name}\n' for name, digest in sorted(manifest.items()))) + manifest['SHA256SUMS'] = sha(assets / 'SHA256SUMS') + release_tools = load('tensor_github', 'create-rust-github-release.py') + release_tools.TAG = TAG + existing = release_tools.existing_release() + ref = subprocess.run(['gh', 'api', f'repos/{REPO}/git/ref/tags/{TAG}'], capture_output=True, text=True) + if ref.returncode: + if '404' not in ref.stderr: + raise ValueError('Could not establish immutable tag absence') + if existing: + raise ValueError('Release exists without the exact tag') + subprocess.run(['gh', 'api', '--method', 'POST', f'repos/{REPO}/git/refs', '-f', f'ref=refs/tags/{TAG}', '-f', f'sha={source}'], check=True, stdout=subprocess.DEVNULL) + if release_tools.tag_source() != source: + raise ValueError('Existing tag identifies another source') + if not existing: + notes = base / 'release-notes.md' + notes.write_text(f"Bounded F32/F16/BF16 Safetensors checkpoint editing and byte-exact rollback.\n\nSource: {source}\nArchive SHA-256: {receipt['archiveSha256']}\n\nQualified source and packed integration tests, cross-platform CLI roundtrips, Rust 1.81, audit with warnings denied, and fresh public registry consumers.\n\nPAN2 GPU inference has not run; no behavioral improvement or automatic promotion is claimed.\n") + subprocess.run(['gh', 'release', 'create', TAG, '--repo', REPO, '--target', source, '--draft', + '--title', f'{NAME} {VERSION}', '--notes-file', str(notes), *map(str, sorted(assets.iterdir()))], check=True) + def verify(folder): + folder.mkdir() + subprocess.run(['gh', 'release', 'download', TAG, '--repo', REPO, '--dir', str(folder)], check=True) + if {p.name: sha(p) for p in folder.iterdir()} != manifest or release_tools.tag_source() != source: + raise ValueError('GitHub release bytes or source differ') + verify(base / 'github-before') + protected(source) + if not existing or existing.get('draft') is True: + release_id = release_tools.existing_release()['id'] + subprocess.run(['gh', 'api', '--method', 'PATCH', f'repos/{REPO}/releases/{release_id}', '-F', 'draft=false'], check=True, stdout=subprocess.DEVNULL) + verify(base / 'github-after') + metadata = gh(f'repos/{REPO}/releases/tags/{TAG}') + if metadata.get('draft') is not False: + raise ValueError('Release remains draft') + (base / 'github-release.json').write_text(json.dumps({'sourceSha': source, 'tag': TAG, 'url': metadata['html_url'], 'assets': manifest, 'verified': True}, indent=2)) + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('mode', choices=('seal', 'validate', 'download', 'publish', 'verify-public', 'github-release')) + parser.add_argument('--expected-sha', required=True) + parser.add_argument('--directory', required=True, type=Path) + args = parser.parse_args() + globals()[{'verify-public': 'verify_public', 'github-release': 'github_release'}.get(args.mode, args.mode)](args.directory.resolve(), args.expected_sha) + +if __name__ == '__main__': + main() diff --git a/scripts/tests/test_tensor_release.py b/scripts/tests/test_tensor_release.py new file mode 100644 index 00000000..1c41e1ff --- /dev/null +++ b/scripts/tests/test_tensor_release.py @@ -0,0 +1,157 @@ +"""Reject altered source semantics, untrusted CI and rewritten candidate evidence.""" +import copy +import importlib.util +import io +import json +import os +from pathlib import Path +import tarfile +import tempfile +import tomllib +import unittest +from unittest.mock import patch +import zipfile + +ROOT = Path(__file__).resolve().parents[2] +spec = importlib.util.spec_from_file_location('tensor_release_tested', ROOT / 'scripts/tensor-release.py') +release = importlib.util.module_from_spec(spec) +spec.loader.exec_module(release) +SHA = 'a' * 40 + + +def normalized(): + data = tomllib.loads((release.PACKAGE / 'Cargo.toml').read_text()) + data.pop('workspace') + data['package'].update(build=False, autolib=False, autobins=False, autoexamples=False, autotests=False, autobenches=False) + data['lib'] = {'name': release.NAME.replace('-', '_'), 'path': 'src/lib.rs'} + data['bin'] = [{'name': release.NAME, 'path': 'src/main.rs'}] + data['test'] = [{'name': p.stem, 'path': 'tests/' + p.name} for p in sorted((release.PACKAGE / 'tests').glob('*.rs'))] + for key in ('dependencies', 'dev-dependencies'): + data[key] = {name: {'version': v} if isinstance(v, str) else v for name, v in data[key].items()} + return data + + +class TensorReleaseTests(unittest.TestCase): + def test_normalized_dependency_target_build_feature_and_extra_fields_reject(self): + release.normalized_manifest(normalized()) + mutations = [ + lambda m: m['dependencies']['half'].update(version='999.0.0'), + lambda m: m['package'].update(build='scripts/exfiltrate.rs'), + lambda m: m['lib'].update(path='src/main.rs'), + lambda m: m.update(features={'default': ['half/serde']}), + lambda m: m['bin'][0].update(path='../outside.rs'), + ] + for mutate in mutations: + data = normalized() + mutate(data) + with self.assertRaisesRegex(ValueError, 'Normalized'): + release.normalized_manifest(data) + + def test_real_archive_source_and_normalized_manifest_binding(self): + # Derive the normalized Cargo fixture with Cargo's existing TOML emitter, + # independently mutate only that file, and preserve original/vcs payloads. + with tempfile.TemporaryDirectory() as directory: + archive = Path(directory) / release.ARCHIVE + files = {} + for path in release.PACKAGE.rglob('*'): + relative = path.relative_to(release.PACKAGE) + if not path.is_file() or any(p in ('target', '__pycache__', 'validation') for p in relative.parts): + continue + if path.suffix == '.log' or path.name == '.gitignore': + continue + files[str(relative)] = path.read_bytes() + files['Cargo.toml.orig'] = files.pop('Cargo.toml') + files['.cargo_vcs_info.json'] = json.dumps({'git': {'sha1': SHA}, 'path_in_vcs': 'crates/wsp-tensor'}).encode() + def write(data, duplicate=False): + files['Cargo.toml'] = release.archive_tools.dump_toml(data).encode() + with tarfile.open(archive, 'w:gz') as stream: + for name, content in files.items(): + info = tarfile.TarInfo(f'{release.NAME}-{release.VERSION}/' + name) + info.size = len(content) + stream.addfile(info, io.BytesIO(content)) + if duplicate: + stream.addfile(info, io.BytesIO(content)) + write(normalized()) + release.archive_binding(archive, SHA) + data = normalized(); data['dependencies']['half']['version'] = '999.0.0' + write(data) + with self.assertRaisesRegex(ValueError, 'Normalized'): + release.archive_binding(archive, SHA) + write(normalized(), duplicate=True) + with self.assertRaisesRegex(ValueError, 'Duplicate'): + release.archive_binding(archive, SHA) + write(normalized()) + with self.assertRaisesRegex(ValueError, 'VCS'): + release.archive_binding(archive, 'b' * 40) + + def test_complete_nonzero_rust_and_python_results_required(self): + good = 'test result: ok. 10 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.0s\n' + self.assertEqual(release.evidence(good, 'rust'), {'testsPassed': 10}) + for output in (good.replace('10 passed', '0 passed'), good.replace('0 ignored', '1 ignored'), + good.replace('0 filtered', '5 filtered'), good + good, ''): + with self.assertRaises(ValueError): release.evidence(output, 'rust') + release.evidence('Ran 3 tests in 0.01s\n\nOK\n', 'python') + for output in ('Ran 0 tests in 0s\nOK\n', 'Ran 1 test in 0s\nOK (skipped=1)\n', 'Ran 3 tests in 0s\nFAILED\n'): + with self.assertRaises(ValueError): release.evidence(output, 'python') + + def ci_fixture(self): + run = dict(id=12, head_sha=SHA, event='push', head_branch='release/tensor', path=release.WORKFLOW, status='completed', conclusion='success') + jobs = [dict(name=n, status='completed', conclusion='success') for n in + ('qualify', 'portability (windows-latest)', 'portability (macos-latest)')] + artifact = dict(id=42, name='tensor-candidate-' + SHA, expired=False, digest='sha256:' + 'd' * 64) + return run, jobs, [artifact] + + def test_missing_pending_skipped_stale_and_duplicate_ci_reject(self): + run, jobs, artifacts = self.ci_fixture() + release.validate_ci(run, jobs, artifacts, SHA) + for key, value in [('head_sha', 'b' * 40), ('event', 'pull_request'), ('head_branch', 'main'), + ('path', '.github/workflows/ci.yml'), ('status', 'queued'), ('conclusion', 'failure')]: + changed = dict(run); changed[key] = value + with self.assertRaises(ValueError): release.validate_ci(changed, jobs, artifacts, SHA) + for changed in (jobs[:-1], [dict(jobs[0], status='queued')] + jobs[1:], [dict(jobs[0], conclusion='skipped')] + jobs[1:]): + with self.assertRaises(ValueError): release.validate_ci(run, changed, artifacts, SHA) + for changed in ([], artifacts * 2, [dict(artifacts[0], expired=True)], [dict(artifacts[0], digest='none')]): + with self.assertRaises(ValueError): release.validate_ci(run, jobs, changed, SHA) + + def test_protected_identity_environment_and_main_lease(self): + environment = dict(GITHUB_ACTIONS='true', GITHUB_WORKFLOW='protected-tensor-release', GITHUB_REF='refs/heads/main', GITHUB_SHA=SHA) + def api(endpoint): + return {'object': {'sha': SHA}} if '/git/ref/' in endpoint else [] + with patch.object(release, 'identity'), patch.object(release, 'gh', side_effect=api), patch.dict(os.environ, environment, clear=True): + release.protected(SHA) + for key in environment: + with patch.dict(os.environ, {key: 'invalid'}): + with self.assertRaises(ValueError): release.protected(SHA) + with patch.object(release, 'identity'), patch.dict(os.environ, environment, clear=True), patch.object(release, 'gh', return_value={'object': {'sha': 'b'*40}}): + with self.assertRaisesRegex(ValueError, 'Main moved'): release.protected(SHA) + + def test_rewritten_downloaded_candidate_rejects_even_with_rewritten_local_hashes(self): + with tempfile.TemporaryDirectory() as directory: + base = Path(directory); candidate = base / 'candidate'; candidate.mkdir() + names = {release.ARCHIVE, 'candidate.json', *[role + '.log' for role, *_ in release.command_plan(candidate)]} + for name in names: (candidate / name).write_bytes(b'original') + with zipfile.ZipFile(base / 'candidate.zip', 'w') as stream: + for name in names: stream.write(candidate / name, name) + run, jobs, artifacts = self.ci_fixture() + artifacts[0]['digest'] = 'sha256:' + release.sha(base / 'candidate.zip') + (base / 'ci.json').write_text(json.dumps({'sourceSha': SHA, 'run': run, 'artifact': artifacts[0], 'repositoryCi': {'runId': 99}})) + def pages(endpoint, field): return jobs if field == 'jobs' else artifacts + with patch.object(release, 'protected'), patch.object(release, 'gh', return_value=run), patch.object(release.ci_tools, 'pages', side_effect=pages), patch.object(release.ci_tools, 'validate_run'), patch.object(release, 'validate', return_value={'valid': True}): + self.assertEqual(release.validated_inputs(base, SHA), {'valid': True}) + (candidate / 'candidate.json').write_text('{"archiveSha256":"forged with new log hashes"}') + with self.assertRaisesRegex(ValueError, 'bytes changed'): + release.validated_inputs(base, SHA) + (base / 'candidate.zip').write_bytes(b'forged zip') + with self.assertRaisesRegex(ValueError, 'ZIP seal changed'): + release.validated_inputs(base, SHA) + + def test_cli_requires_actual_roundtrip_and_no_inference_claim(self): + data = {'schema': 'ruv-fann.tensor-e2e/1', 'roundtrip': 'passed', 'overwrite_rejected': True, 'pan2_inference': 'not_run'} + release.evidence(json.dumps(data), 'cli') + for key, value in [('roundtrip', 'skipped'), ('overwrite_rejected', False), ('pan2_inference', 'passed')]: + changed = dict(data); changed[key] = value + with self.assertRaises(ValueError): release.evidence(json.dumps(changed), 'cli') + + +if __name__ == '__main__': + unittest.main() From fe2083e3f3dbf016a8ce81d4c160f0bc01fcae97 Mon Sep 17 00:00:00 2001 From: ruv Date: Wed, 11 Nov 2026 18:46:38 -0500 Subject: [PATCH 09/10] Exclude Python bytecode from exact tensor archive inventory --- crates/wsp-tensor/Cargo.toml | 2 +- scripts/tensor-release.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/wsp-tensor/Cargo.toml b/crates/wsp-tensor/Cargo.toml index e140c56a..b36e93f4 100644 --- a/crates/wsp-tensor/Cargo.toml +++ b/crates/wsp-tensor/Cargo.toml @@ -12,7 +12,7 @@ documentation = "https://docs.rs/ruv-fann-wsp-tensor" readme = "README.md" keywords = ["safetensors", "checkpoint", "model-editing", "weight-space", "machine-learning"] categories = ["science", "command-line-utilities"] -include = ["src/**", "tests/**", "scripts/**", "Cargo.toml", "Cargo.lock", "README.md", "LICENSE-MIT", "LICENSE-APACHE"] +include = ["src/*.rs", "tests/*.rs", "scripts/*.py", "Cargo.toml", "Cargo.lock", "README.md", "LICENSE-MIT", "LICENSE-APACHE"] [workspace] diff --git a/scripts/tensor-release.py b/scripts/tensor-release.py index e003d742..1f501269 100644 --- a/scripts/tensor-release.py +++ b/scripts/tensor-release.py @@ -99,7 +99,7 @@ def portable(value, base): def execute(role, argv, cwd, kind, base, commands): env = dict(os.environ, CARGO_TARGET_DIR=str(base / 'build'), CARGO_BUILD_JOBS='2', CARGO_INCREMENTAL='0', - WSP_TENSOR_BIN=str(base / 'build/release' / NAME)) + WSP_TENSOR_BIN=str(base / 'build/release' / NAME), PYTHONDONTWRITEBYTECODE='1') result = subprocess.run(argv, cwd=cwd, env=env, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True) log = base / f'{role}.log' log.write_text(result.stdout) From fc575c8c6cdbd18cca92fb07ddfdcd87c60d34a8 Mon Sep 17 00:00:00 2001 From: ruv Date: Wed, 11 Nov 2026 19:07:22 -0500 Subject: [PATCH 10/10] Exercise real Darwin Unicode paths while retaining Linux non-UTF8 coverage --- crates/wsp-tensor/tests/checkpoints.rs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/crates/wsp-tensor/tests/checkpoints.rs b/crates/wsp-tensor/tests/checkpoints.rs index 4eb5ff25..a6af0fff 100644 --- a/crates/wsp-tensor/tests/checkpoints.rs +++ b/crates/wsp-tensor/tests/checkpoints.rs @@ -156,14 +156,19 @@ fn refuse_overwrite_and_forged_rollback() { #[cfg(unix)] #[test] -fn inspect_cli_accepts_non_utf8_paths() { +fn inspect_cli_accepts_native_unix_paths() { use std::os::unix::ffi::OsStrExt; use std::process::Command; let d = tempfile::tempdir().unwrap(); - let path = d - .path() - .join(std::ffi::OsStr::from_bytes(b"tensor-\xff.safetensors")); + // Linux permits arbitrary non-NUL filename bytes. Darwin's filesystem + // rejects invalid UTF-8 before the CLI runs, so exercise a real Unicode + // path there instead of treating Linux filesystem behavior as universal. + #[cfg(not(target_os = "macos"))] + let name = std::ffi::OsStr::from_bytes(b"tensor-\xff.safetensors"); + #[cfg(target_os = "macos")] + let name = std::ffi::OsStr::from_bytes("tensor-e\u{301}-\u{1f9e0}.safetensors".as_bytes()); + let path = d.path().join(name); fixture(&path, "F32"); let result = Command::new(env!("CARGO_BIN_EXE_ruv-fann-wsp-tensor")) .arg("inspect")