diff --git a/.crumb b/.crumb index c851afc..0eee901 100644 --- a/.crumb +++ b/.crumb @@ -13,27 +13,27 @@ "generated": { "children": [ { - "digest": "568edf1ba4cc3e991aefc7d761a9e782c79fa20d9a5fb49d9f1da59661e1a668", - "last_commit": "8a9c8e2", + "digest": "70cc942a40485af2b5600def403490456457d47b942439f4a8a895440321c5a4", + "last_commit": "638b9a3", "name": "crates", "purpose": "Directory crates. Purpose not yet described by a human." }, { "digest": "73532b950b93ca0ace42e0f199e6af467de919c40a31a8ba3489bba4f1e932b5", - "last_commit": "b3f8bd4", + "last_commit": "6f2a421", "name": "specs", "purpose": "Directory specs. Purpose not yet described by a human." } ], - "children_root": "sha256:c650904c99898bb27d1eb448c83ff2d50c81df7290ed2b43d908434079f1ded6", - "compiled_at": "2026-10-18T17:39:12Z", + "children_root": "sha256:604d3d44c1a2fdec6842d2a39159654ac493eb8811234750128560ed3b443b93", + "compiled_at": "2026-10-18T18:12:09Z", "compiler": "crumb-compile/1", - "digest": "sha256:71ee540e6eed2670441ef803479e7bb8d1591226f6d199a4e1f99dcef4b0cab3", + "digest": "sha256:92deb32d92cba7a430d1fce8eb0cf1c2110ce6823fe90349006fe57e66c8c381", "evidence_files": 1, "evidence_root": "sha256:87c36bf8ed7be61c064552ebf141834707770b75d070ea49328a3e06d510afba", - "files": 503, - "generated_at_commit": "b3f8bd4948610368d87195fd39f935b759e0a479", - "source_tree": "sha256:c8c74cbe1334576d1f9b4d1cf4901769e87e79a95f0d6cb485389059292c101f" + "files": 506, + "generated_at_commit": "638b9a34f8f3b8ad0efb91085138de1677a81ba6", + "source_tree": "sha256:efba5ea71920b1877ceeaacd65c3a053a394d7049362f8389750af2e646ecdb6" }, "owner_lane": "", "seed": { diff --git a/CHANGELOG.md b/CHANGELOG.md index 8dd86ab..a9de8f6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,10 @@ VERSIONING.md. ### Specifications +- `crates/aien-pi-spark-task` (C4, aien-protocols-mv#31): Rust reader and + writer for the PST1 fixed header, no `unsafe`, no JSON, fixed bounds. Its + test walks `specs/pi-spark-task/vectors/expected.txt` and requires the same + verdict line as the C checker and byte-identical re-encoding. - `specs/pi-spark-task`: PST1 Draft v0.1 (C3, aien-protocols-mv#30): a 4-byte `PST1` magic now opens the header (`reserved1` shrank to 1 byte and `reserved2` was removed, so the header stays 224 bytes; fields after `type` diff --git a/CONSUMERS.md b/CONSUMERS.md index 841d91f..a387828 100644 --- a/CONSUMERS.md +++ b/CONSUMERS.md @@ -10,6 +10,7 @@ carries a newer tag with migration notes, all consumers target `v0.1.0`. | `spark-inquisitor` | protocol-types, evaluation-protocol, probe | v0.1.0 | | `spark-rsi` | protocol-types, evaluation-protocol, probe | v0.1.0 | | `aien-sovereign-core` (`spark-harvester`, `spark-mail-rs`) | protocol-types, provenance, probe | v0.1.0 | +| `aien-sovereign-core` (Spark side of the Pi link, lane D, aien-sovereign-core-mv#585; planned, not yet a dependency) | pi-spark-task (`crates/aien-pi-spark-task`, PST1 Draft v0.1) | v0.1.0 | Consumption is by path dependency today. A consumer moves to a newer tag by recording the new version in this table in the same change that adopts it. diff --git a/Cargo.lock b/Cargo.lock index fb61e61..f86df5d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -87,6 +87,13 @@ dependencies = [ "uuid", ] +[[package]] +name = "aien-pi-spark-task" +version = "0.1.0" +dependencies = [ + "sha2 0.10.9", +] + [[package]] name = "aien-probe" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 0ae7089..0a47c3f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,7 @@ members = [ "crates/aien-evaluation-protocol", "crates/aien-provenance", "crates/aien-probe", + "crates/aien-pi-spark-task", ] [workspace.package] diff --git a/crates/.crumb b/crates/.crumb index 2dcd18d..56105f0 100644 --- a/crates/.crumb +++ b/crates/.crumb @@ -80,9 +80,15 @@ "name": "aien-inference-protocol", "purpose": "Directory crates/aien-inference-protocol. Purpose not yet described by a human." }, + { + "digest": "0df1292fb1ef449ec3362cbb5a0407da3b0b77322a22f615da94679ae7537073", + "last_commit": "638b9a3", + "name": "aien-pi-spark-task", + "purpose": "PST1 Pi-Spark task message: Rust header reader and writer that must match the C checker byte for" + }, { "digest": "8882b1057235e20f8b0af49350d3e4a1cdc1a4c6259170fcbc66cc905cc1a7a8", - "last_commit": "f250aa2", + "last_commit": "8a9c8e2", "name": "aien-probe", "purpose": "Directory crates/aien-probe. Purpose not yet described by a human." }, @@ -99,15 +105,15 @@ "purpose": "Directory crates/aien-provenance. Purpose not yet described by a human." } ], - "children_root": "sha256:b552586103a38f8880dcc92d669e9391a5e924eec6578a237b5d842bfe046f05", - "compiled_at": "2026-10-15T11:35:44Z", + "children_root": "sha256:6699c654ab08e8211dd36c643fd8e93a127064e3170e089d1fa5edf6dc4a5b3b", + "compiled_at": "2026-10-18T18:12:09Z", "compiler": "crumb-compile/1", - "digest": "sha256:568edf1ba4cc3e991aefc7d761a9e782c79fa20d9a5fb49d9f1da59661e1a668", + "digest": "sha256:70cc942a40485af2b5600def403490456457d47b942439f4a8a895440321c5a4", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 29, - "generated_at_commit": "f250aa2e0f8c3da108da0c2b2d9f80ddf67ae7a1", - "source_tree": "sha256:325f848278a692d37df4599578dbe32f7d3a87d50398c9aac9ac83d84a0344ca" + "files": 32, + "generated_at_commit": "638b9a34f8f3b8ad0efb91085138de1677a81ba6", + "source_tree": "sha256:8511d71d37a5e5d698cb1438ff4bddbd8a1a2aeb53293ce14522bc4042549add" }, "owner_lane": "", "seed": { diff --git a/crates/aien-pi-spark-task/.crumb b/crates/aien-pi-spark-task/.crumb new file mode 100644 index 0000000..7fea290 --- /dev/null +++ b/crates/aien-pi-spark-task/.crumb @@ -0,0 +1,33 @@ +{ + "extensions": { + "generated": { + "children": [ + { + "digest": "f83bdac08bbdbc95005bc353d20b4767b320910dd05417eebf6110770d16bae0", + "last_commit": "638b9a3", + "name": "src", + "purpose": "PST1 header decode, encode and verdict line" + }, + { + "digest": "2b9cefa448aa8152cbe12b63ebea0b757a54219f2ff0120187cc85ba04a456da", + "last_commit": "638b9a3", + "name": "tests", + "purpose": "Parity test: walks specs/pi-spark-task/vectors/expected.txt against the C checker's verdicts" + } + ], + "children_root": "sha256:579bcd4dc6f8c60cab33b5eac75e5d0bf20857d3ae278a74566f3eb3ea1fb12d", + "compiled_at": "2026-10-18T18:12:09Z", + "compiler": "crumb-compile/1", + "digest": "sha256:0df1292fb1ef449ec3362cbb5a0407da3b0b77322a22f615da94679ae7537073", + "evidence_files": 0, + "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", + "files": 3, + "generated_at_commit": "638b9a34f8f3b8ad0efb91085138de1677a81ba6", + "source_tree": "sha256:df08077a3d6d5875b6883196d612341b9c451d5c92214d2c9346b1a7f0d7cf98" + } + }, + "layer": "crates/aien-pi-spark-task", + "name": "crates/aien-pi-spark-task", + "purpose": "PST1 Pi-Spark task message: Rust header reader and writer that must match the C checker byte for byte", + "schema_version": "1.0.0" +} diff --git a/crates/aien-pi-spark-task/Cargo.toml b/crates/aien-pi-spark-task/Cargo.toml new file mode 100644 index 0000000..9be3176 --- /dev/null +++ b/crates/aien-pi-spark-task/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "aien-pi-spark-task" +version = "0.1.0" +edition = "2021" +license = "AGPL-3.0-or-later" +description = "PST1 Pi-Spark task message: fixed-size header reader and writer (specs/pi-spark-task)" + +[dependencies] +sha2 = { workspace = true } diff --git a/crates/aien-pi-spark-task/src/.crumb b/crates/aien-pi-spark-task/src/.crumb new file mode 100644 index 0000000..748c6e0 --- /dev/null +++ b/crates/aien-pi-spark-task/src/.crumb @@ -0,0 +1,20 @@ +{ + "extensions": { + "generated": { + "children": [], + "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", + "compiled_at": "2026-10-18T18:12:09Z", + "compiler": "crumb-compile/1", + "digest": "sha256:f83bdac08bbdbc95005bc353d20b4767b320910dd05417eebf6110770d16bae0", + "evidence_files": 0, + "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", + "files": 1, + "generated_at_commit": "638b9a34f8f3b8ad0efb91085138de1677a81ba6", + "source_tree": "sha256:3dd498e3f7edf7c039dfb904c9c35acb471ce17aa7928d61e3817bd94703570c" + } + }, + "layer": "crates/aien-pi-spark-task/src", + "name": "crates/aien-pi-spark-task/src", + "purpose": "PST1 header decode, encode and verdict line", + "schema_version": "1.0.0" +} diff --git a/crates/aien-pi-spark-task/src/lib.rs b/crates/aien-pi-spark-task/src/lib.rs new file mode 100644 index 0000000..e07e861 --- /dev/null +++ b/crates/aien-pi-spark-task/src/lib.rs @@ -0,0 +1,338 @@ +//! PST1, the Pi-Spark task message: reader and writer for the fixed header. +//! +//! Spec: `specs/pi-spark-task/SPEC.md` (Draft v0.1, contract 0.1.0, wire +//! version 1). The C reference checker is `specs/pi-spark-task/tools/pst_verify.c`; +//! this crate must give the same verdict on the same bytes (the test in +//! `tests/vectors.rs` walks the shared vectors). +//! +//! Bounds are fixed: a message is 224 header bytes plus at most 800 payload +//! bytes. Decoding borrows the payload from the input and allocates nothing. +//! There is no JSON anywhere in the decode path. +//! +//! First cut (aien-protocols-mv#31): the header only. Per-type payload +//! bodies, the side of the sender and the session rules are not read here. +#![forbid(unsafe_code)] + +use sha2::{Digest, Sha256}; + +/// Bytes in the fixed header. +pub const HEADER_LEN: usize = 224; +/// Largest payload. +pub const MAX_PAYLOAD: usize = 800; +/// Largest message (header plus payload). +pub const MAX_MESSAGE: usize = HEADER_LEN + MAX_PAYLOAD; +/// The four bytes that open every message. +pub const MAGIC: [u8; 4] = *b"PST1"; +/// The only wire version. +pub const VERSION: u8 = 1; +/// Highest message type (section 5). +pub const MAX_TYPE: u8 = 14; +/// The `request` type. +pub const TYPE_REQUEST: u8 = 1; + +const O_MAGIC: usize = 0; +const O_VERSION: usize = 4; +const O_TYPE: usize = 5; +const O_RES0: usize = 6; +const O_CAUSE: usize = 8; +const O_ORIGIN: usize = 40; +const O_OBJECTIVE: usize = 72; +const O_SUBJECT: usize = 104; +const O_PARENT: usize = 136; +const O_TASK: usize = 152; +const O_IDEM: usize = 168; +const O_ORDINAL: usize = 184; +const O_EFFECT: usize = 188; +const O_RES1: usize = 189; +const O_PLEN: usize = 190; +const O_DIGEST: usize = 192; + +/// A refusal code from SPEC.md section 7. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Refusal { + BadMagic, + BadVersion, + Downgrade, + BadType, + WrongSide, + BadLength, + TrailingBytes, + ReservedNonzero, + NoCause, + NoTask, + NoObjective, + DigestMismatch, + BadEffectClass, + SubjectMismatch, +} + +impl Refusal { + /// The code name exactly as SPEC.md section 7 and the C checker print it. + pub fn code(self) -> &'static str { + match self { + Refusal::BadMagic => "PST_BAD_MAGIC", + Refusal::BadVersion => "PST_BAD_VERSION", + Refusal::Downgrade => "PST_DOWNGRADE", + Refusal::BadType => "PST_BAD_TYPE", + Refusal::WrongSide => "PST_WRONG_SIDE", + Refusal::BadLength => "PST_BAD_LENGTH", + Refusal::TrailingBytes => "PST_TRAILING_BYTES", + Refusal::ReservedNonzero => "PST_RESERVED_NONZERO", + Refusal::NoCause => "PST_NO_CAUSE", + Refusal::NoTask => "PST_NO_TASK", + Refusal::NoObjective => "PST_NO_OBJECTIVE", + Refusal::DigestMismatch => "PST_DIGEST_MISMATCH", + Refusal::BadEffectClass => "PST_BAD_EFFECT_CLASS", + Refusal::SubjectMismatch => "PST_SUBJECT_MISMATCH", + } + } +} + +/// The decoded fixed header (SPEC.md section 4). Reserved bytes and the +/// magic are not stored: they have exactly one allowed value. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Header { + pub version: u8, + pub msg_type: u8, + pub cause_id: [u8; 32], + pub origin_run_id: [u8; 32], + pub objective_id: [u8; 32], + pub subject_id: [u8; 32], + pub parent_task_id: [u8; 16], + pub task_id: [u8; 16], + pub idempotence_key: [u8; 16], + pub ordinal: u32, + pub effect_class: u8, + pub payload_len: u16, + pub payload_digest: [u8; 32], +} + +/// One accepted message: the header and the payload it borrows. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Message<'a> { + pub header: Header, + pub payload: &'a [u8], +} + +/// Why `encode` could not write a message. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum EncodeError { + /// The payload is longer than 800 bytes. + PayloadTooLong, + /// `payload_len` in the header differs from the payload given. + LengthMismatch, + /// The output buffer is too small. + BufferTooSmall, +} + +fn arr(b: &[u8], at: usize) -> [u8; N] { + let mut a = [0u8; N]; + a.copy_from_slice(&b[at..at + N]); + a +} + +fn all_zero(b: &[u8]) -> bool { + b.iter().all(|&x| x == 0) +} + +/// Decode and check one message in the order of SPEC.md section 4.1, the +/// same order as the C checker, so the same bytes give the same code. +pub fn decode(b: &[u8]) -> Result, Refusal> { + // 1. length bounds + if b.len() < HEADER_LEN || b.len() > MAX_MESSAGE { + return Err(Refusal::BadLength); + } + // 2. magic + if b[O_MAGIC..O_MAGIC + 4] != MAGIC { + return Err(Refusal::BadMagic); + } + // 3. version + if b[O_VERSION] < VERSION { + return Err(Refusal::Downgrade); + } + if b[O_VERSION] > VERSION { + return Err(Refusal::BadVersion); + } + // 4. type + if b[O_TYPE] == 0 || b[O_TYPE] > MAX_TYPE { + return Err(Refusal::BadType); + } + // 5. reserved + if !all_zero(&b[O_RES0..O_RES0 + 2]) || b[O_RES1] != 0 { + return Err(Refusal::ReservedNonzero); + } + // 6, 7. payload length against the bytes present + let payload_len = u16::from_be_bytes(arr(b, O_PLEN)); + let body = &b[HEADER_LEN..]; + if body.len() < usize::from(payload_len) { + return Err(Refusal::BadLength); + } + if body.len() > usize::from(payload_len) { + return Err(Refusal::TrailingBytes); + } + // 8..11. field rules + if b[O_EFFECT] > 2 { + return Err(Refusal::BadEffectClass); + } + if all_zero(&b[O_CAUSE..O_CAUSE + 32]) { + return Err(Refusal::NoCause); + } + if all_zero(&b[O_TASK..O_TASK + 16]) || all_zero(&b[O_IDEM..O_IDEM + 16]) { + return Err(Refusal::NoTask); + } + if b[O_TYPE] == TYPE_REQUEST && all_zero(&b[O_OBJECTIVE..O_OBJECTIVE + 32]) { + return Err(Refusal::NoObjective); + } + // 12. payload digest + let digest: [u8; 32] = Sha256::digest(body).into(); + if digest[..] != b[O_DIGEST..O_DIGEST + 32] { + return Err(Refusal::DigestMismatch); + } + Ok(Message { + header: Header { + version: b[O_VERSION], + msg_type: b[O_TYPE], + cause_id: arr(b, O_CAUSE), + origin_run_id: arr(b, O_ORIGIN), + objective_id: arr(b, O_OBJECTIVE), + subject_id: arr(b, O_SUBJECT), + parent_task_id: arr(b, O_PARENT), + task_id: arr(b, O_TASK), + idempotence_key: arr(b, O_IDEM), + ordinal: u32::from_be_bytes(arr(b, O_ORDINAL)), + effect_class: b[O_EFFECT], + payload_len, + payload_digest: arr(b, O_DIGEST), + }, + payload: body, + }) +} + +/// Write one message into `out` and return the byte count. Fields are written +/// exactly as given; the digest is not recomputed (use [`payload_digest`]). +pub fn encode(h: &Header, payload: &[u8], out: &mut [u8]) -> Result { + if payload.len() > MAX_PAYLOAD { + return Err(EncodeError::PayloadTooLong); + } + if usize::from(h.payload_len) != payload.len() { + return Err(EncodeError::LengthMismatch); + } + let n = HEADER_LEN + payload.len(); + if out.len() < n { + return Err(EncodeError::BufferTooSmall); + } + let o = &mut out[..n]; + o[..HEADER_LEN].fill(0); + o[O_MAGIC..O_MAGIC + 4].copy_from_slice(&MAGIC); + o[O_VERSION] = h.version; + o[O_TYPE] = h.msg_type; + o[O_CAUSE..O_CAUSE + 32].copy_from_slice(&h.cause_id); + o[O_ORIGIN..O_ORIGIN + 32].copy_from_slice(&h.origin_run_id); + o[O_OBJECTIVE..O_OBJECTIVE + 32].copy_from_slice(&h.objective_id); + o[O_SUBJECT..O_SUBJECT + 32].copy_from_slice(&h.subject_id); + o[O_PARENT..O_PARENT + 16].copy_from_slice(&h.parent_task_id); + o[O_TASK..O_TASK + 16].copy_from_slice(&h.task_id); + o[O_IDEM..O_IDEM + 16].copy_from_slice(&h.idempotence_key); + o[O_ORDINAL..O_ORDINAL + 4].copy_from_slice(&h.ordinal.to_be_bytes()); + o[O_EFFECT] = h.effect_class; + o[O_PLEN..O_PLEN + 2].copy_from_slice(&h.payload_len.to_be_bytes()); + o[O_DIGEST..O_DIGEST + 32].copy_from_slice(&h.payload_digest); + o[HEADER_LEN..].copy_from_slice(payload); + Ok(n) +} + +/// SHA-256 of a payload, for filling `Header::payload_digest`. +pub fn payload_digest(payload: &[u8]) -> [u8; 32] { + Sha256::digest(payload).into() +} + +fn hex(out: &mut String, bytes: &[u8]) { + const HX: &[u8; 16] = b"0123456789abcdef"; + for &x in bytes { + out.push(char::from(HX[usize::from(x >> 4)])); + out.push(char::from(HX[usize::from(x & 15)])); + } +} + +/// The verdict line in the exact format of `pst_verify FILE` and +/// `vectors/expected.txt`. +pub fn verdict_line(result: &Result, Refusal>) -> String { + let m = match result { + Err(r) => return format!("refuse {}", r.code()), + Ok(m) => &m.header, + }; + let mut s = String::with_capacity(512); + s.push_str(&format!( + "ok type={} version={} cause=", + m.msg_type, m.version + )); + hex(&mut s, &m.cause_id); + s.push_str(" origin="); + hex(&mut s, &m.origin_run_id); + s.push_str(" objective="); + hex(&mut s, &m.objective_id); + s.push_str(" subject="); + hex(&mut s, &m.subject_id); + s.push_str(" parent="); + hex(&mut s, &m.parent_task_id); + s.push_str(" task="); + hex(&mut s, &m.task_id); + s.push_str(" idem="); + hex(&mut s, &m.idempotence_key); + s.push_str(&format!( + " ordinal={} effect={} len={} digest=", + m.ordinal, m.effect_class, m.payload_len + )); + hex(&mut s, &m.payload_digest); + s +} + +#[cfg(test)] +mod tests { + use super::*; + + fn sample() -> (Header, &'static [u8]) { + let payload: &'static [u8] = b"hello"; + let h = Header { + version: VERSION, + msg_type: TYPE_REQUEST, + cause_id: [1; 32], + origin_run_id: [2; 32], + objective_id: [3; 32], + subject_id: [4; 32], + parent_task_id: [0; 16], + task_id: [5; 16], + idempotence_key: [6; 16], + ordinal: 0, + effect_class: 1, + payload_len: 5, + payload_digest: payload_digest(payload), + }; + (h, payload) + } + + #[test] + fn encode_then_decode_round_trips() { + let (h, p) = sample(); + let mut out = [0u8; MAX_MESSAGE]; + let n = encode(&h, p, &mut out).unwrap(); + let m = decode(&out[..n]).unwrap(); + assert_eq!(m.header, h); + assert_eq!(m.payload, p); + } + + #[test] + fn encode_refuses_what_it_cannot_write() { + let (mut h, p) = sample(); + let mut small = [0u8; HEADER_LEN]; + assert_eq!(encode(&h, p, &mut small), Err(EncodeError::BufferTooSmall)); + h.payload_len = 4; + let mut out = [0u8; MAX_MESSAGE]; + assert_eq!(encode(&h, p, &mut out), Err(EncodeError::LengthMismatch)); + let long = [0u8; MAX_PAYLOAD + 1]; + assert_eq!( + encode(&h, &long, &mut [0u8; 2048]), + Err(EncodeError::PayloadTooLong) + ); + } +} diff --git a/crates/aien-pi-spark-task/tests/.crumb b/crates/aien-pi-spark-task/tests/.crumb new file mode 100644 index 0000000..e935bfd --- /dev/null +++ b/crates/aien-pi-spark-task/tests/.crumb @@ -0,0 +1,20 @@ +{ + "extensions": { + "generated": { + "children": [], + "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", + "compiled_at": "2026-10-18T18:12:09Z", + "compiler": "crumb-compile/1", + "digest": "sha256:2b9cefa448aa8152cbe12b63ebea0b757a54219f2ff0120187cc85ba04a456da", + "evidence_files": 0, + "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", + "files": 1, + "generated_at_commit": "638b9a34f8f3b8ad0efb91085138de1677a81ba6", + "source_tree": "sha256:9f24358336a1bbbad41b3aa078e9a3b92f9c9d57f1cc574b50b648c37414a9a3" + } + }, + "layer": "crates/aien-pi-spark-task/tests", + "name": "crates/aien-pi-spark-task/tests", + "purpose": "Parity test: walks specs/pi-spark-task/vectors/expected.txt against the C checker's verdicts", + "schema_version": "1.0.0" +} diff --git a/crates/aien-pi-spark-task/tests/vectors.rs b/crates/aien-pi-spark-task/tests/vectors.rs new file mode 100644 index 0000000..e134c6c --- /dev/null +++ b/crates/aien-pi-spark-task/tests/vectors.rs @@ -0,0 +1,58 @@ +//! Walks `specs/pi-spark-task/vectors/expected.txt` and requires the Rust +//! reader to print the same verdict line as the C reference checker +//! (`tools/pst_verify.c`) for every file, and to write every accepted message +//! back out as the identical bytes. + +use std::path::PathBuf; + +use aien_pi_spark_task::{decode, encode, verdict_line, MAX_MESSAGE}; + +fn vectors_dir() -> PathBuf { + PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../specs/pi-spark-task/vectors") +} + +#[test] +fn every_expected_line_matches_the_c_checker() { + let dir = vectors_dir(); + let list = std::fs::read_to_string(dir.join("expected.txt")).expect("read expected.txt"); + let mut checked = 0usize; + let mut accepted = 0usize; + let mut failures = Vec::new(); + for line in list.lines() { + if line.is_empty() || line.starts_with('#') { + continue; + } + let (name, want) = line + .split_once(' ') + .expect("line has a file name and a verdict"); + let bytes = std::fs::read(dir.join(name)).expect("read vector"); + let result = decode(&bytes); + let got = verdict_line(&result); + if got != want { + failures.push(format!("{name}\n expected: {want}\n got: {got}")); + } + if let Ok(msg) = &result { + let mut out = [0u8; MAX_MESSAGE]; + let n = encode(&msg.header, msg.payload, &mut out).expect("re-encode accepted message"); + if out[..n] != bytes[..] { + failures.push(format!("{name}: round trip gave different bytes")); + } + accepted += 1; + } + checked += 1; + } + assert!( + failures.is_empty(), + "{} of {checked} vectors differ:\n{}", + failures.len(), + failures.join("\n") + ); + assert!( + checked >= 23, + "expected at least 23 vectors, found {checked}" + ); + assert!( + accepted >= 4, + "expected at least 4 accepted vectors, found {accepted}" + ); +}