diff --git a/crates/huginn-daemon/src/serve.rs b/crates/huginn-daemon/src/serve.rs index 0ffdec6..5170ba0 100644 --- a/crates/huginn-daemon/src/serve.rs +++ b/crates/huginn-daemon/src/serve.rs @@ -666,16 +666,26 @@ mod tests { /// against one length alone (every other fixture uses `m-0`) hides a gate /// that hard-codes that length's contribution instead of measuring it. fn synthetic_with_id(message_id: &str, payload_len: usize) -> CultNetMessage { + synthetic_full(message_id, "view", "huginn-yggdrasil", payload_len) + } + + /// N4: `operation` and `source_runtime_id` are in the encoded envelope + /// too, exactly as `message_id` is, so a boundary fixture that only ever + /// varies `message_id` cannot tell a gate that genuinely measures the + /// encoded bytes from one that hard-codes a formula shaped to fit that + /// one dimension. `synthetic_with_id` is this with the operation and + /// runtime id `the_gates_boundary_*` tests were pinned to before N4. + fn synthetic_full(message_id: &str, operation: &str, runtime_id: &str, payload_len: usize) -> CultNetMessage { CultNetMessage::OperationResponse { message_id: message_id.into(), service_id: MIND_SERVICE_ID.into(), - operation: "view".into(), + operation: operation.into(), status: "accepted".into(), payload_schema: MIND_RESPONSE_SCHEMA.into(), payload_encoding: "messagepack-base64".into(), payload: "A".repeat(payload_len), diagnostics: vec![], - source_runtime_id: Some("huginn-yggdrasil".into()), + source_runtime_id: Some(runtime_id.into()), } } @@ -688,9 +698,18 @@ mod tests { /// assumed: the length prefix is the same width on either side of a /// megabyte, so one probe fixes it. fn sized_exactly_with_id(message_id: &str, target: u64) -> CultNetMessage { + sized_exactly_full(message_id, "view", "huginn-yggdrasil", target) + } + + /// N4's own probe: `sized_exactly_with_id` generalised over the + /// operation and the runtime id as well as the message id, so a fixture + /// can pin the gate's boundary at a combination a formula shaped to fit + /// `view`/`huginn-yggdrasil` alone was never measured against. + fn sized_exactly_full(message_id: &str, operation: &str, runtime_id: &str, target: u64) -> CultNetMessage { let probe = 1_200_000_usize; - let at_probe = encoded_len(&synthetic_with_id(message_id, probe)); - let message = synthetic_with_id(message_id, (probe as i64 + (target as i64 - at_probe as i64)) as usize); + let at_probe = encoded_len(&synthetic_full(message_id, operation, runtime_id, probe)); + let message = + synthetic_full(message_id, operation, runtime_id, (probe as i64 + (target as i64 - at_probe as i64)) as usize); assert_eq!(encoded_len(&message), target); message } @@ -820,6 +839,40 @@ mod tests { assert_eq!((bytes, limit), (MAX_RESPONSE_BYTES + 1, MAX_RESPONSE_BYTES)); } + /// N4: the boundary above is pinned at one `message_id` length, but + /// `operation` and `source_runtime_id` are in the encoded envelope the + /// same way `message_id` is, and every fixture before this one held both + /// fixed at `"view"` and `"huginn-yggdrasil"`. A gate whose measurement is + /// a formula of `message_id`'s length alone -- shaped to pass at exactly + /// `m-0`'s three bytes and the other fixture's forty -- has no way to be + /// wrong there and every way to be wrong once the operation or the + /// runtime id it never looked at changes length too. This holds the + /// boundary at `open_items` (ten bytes, not `view`'s four) over a second + /// runtime id, so such a formula is measured somewhere it was never + /// fitted. + #[test] + fn the_gates_boundary_holds_across_operation_and_runtime_id() { + for (operation, runtime_id) in [("view", "huginn-yggdrasil"), ("open_items", "huginn-thought-cage")] { + let at = sized_exactly_full("m-g", operation, runtime_id, MAX_RESPONSE_BYTES); + let over = sized_exactly_full("m-g", operation, runtime_id, MAX_RESPONSE_BYTES + 1); + assert_eq!( + within_window(at.clone(), "m-g", operation, runtime_id), + at, + "{operation}/{runtime_id}: exactly the limit passes" + ); + + let refused = within_window(over.clone(), "m-g", operation, runtime_id); + let (correlation, answered) = decode_response(&refused).unwrap(); + assert_eq!(correlation, "m-g"); + let HuginnMindResponse::Refused(MindRefusal::ResponseTooLarge { bytes, limit }) = + answered.expect("a refusal is an answer, not an envelope failure") + else { + panic!("{operation}/{runtime_id}: one byte over the limit was not refused: {refused:?}"); + }; + assert_eq!((bytes, limit), (MAX_RESPONSE_BYTES + 1, MAX_RESPONSE_BYTES), "{operation}/{runtime_id}"); + } + } + /// One cut spec's own reference, by its cut label. fn spec_ref(daemon: &mut Daemon, cut: &str) -> PipelineRef { let query = PipelineQuery { diff --git a/crates/huginn-mind/src/mind.rs b/crates/huginn-mind/src/mind.rs index 76f357d..8ea99d2 100644 --- a/crates/huginn-mind/src/mind.rs +++ b/crates/huginn-mind/src/mind.rs @@ -491,6 +491,64 @@ mod tests { } } + /// N3: one generated test in place of more fixture pairs. The base name + /// carries all three separator bytes a `Slug` permits -- `_`, `.` and `-` + /// -- at three non-adjacent positions, so every combination of swapping + /// one separator for another at a fixed position is itself a grammatical, + /// distinct `Slug`. The cartesian product over the three positions (26 + /// variants, the all-unchanged combination excepted) covers every + /// same-position permutation, including a fold that unifies two distinct + /// separators (`_` and `.` folded to one canonical byte) and a fold + /// applied to only one side of the comparison (the mind's own name + /// folded, the declared name left as written, or the reverse). Three more + /// variants pad the base with a leading dash, a trailing dash, and a + /// doubled interior dash -- all still grammatical, since a label's + /// hyphens carry no position or run restriction -- to reach `trim_matches` + /// and run-collapsing folds a same-position swap cannot produce. Every + /// one of the 30 variants must be refused as `ForeignInstance`, never + /// silently admitted as the mind's own name. + #[test] + fn every_separator_variant_of_a_declared_name_is_foreign_not_the_mind() { + const BASE: &str = "ab_cd.ef-gh"; + let separators: Vec<(usize, char)> = + BASE.char_indices().filter(|(_, c)| matches!(c, '_' | '.' | '-')).collect(); + assert_eq!(separators.len(), 3, "the base carries exactly one of each separator"); + + let mut variants: Vec = Vec::new(); + for a in ['_', '.', '-'] { + for b in ['_', '.', '-'] { + for c in ['_', '.', '-'] { + let mut bytes: Vec = BASE.chars().collect(); + bytes[separators[0].0] = a; + bytes[separators[1].0] = b; + bytes[separators[2].0] = c; + variants.push(bytes.into_iter().collect()); + } + } + } + variants.retain(|variant| variant != BASE); + assert_eq!(variants.len(), 26, "3^3 combinations, less the one that reproduces BASE"); + + variants.push(format!("-{BASE}")); + variants.push(format!("{BASE}-")); + variants.push(format!("-{BASE}-")); + variants.push(BASE.replacen('-', "--", 1)); + + let mut seen = std::collections::BTreeSet::new(); + variants.retain(|variant| seen.insert(variant.clone())); + assert_eq!(variants.len(), 30, "26 same-length separator permutations plus 4 padded variants, none colliding"); + + let mind = crate::fixtures::opened(MemoryStore::new(), BASE); + for declared in &variants { + assert!(declared.split('.').all(|label| !label.is_empty()), "{declared}: still grammatical"); + assert_eq!( + mind.require_instance(&slug(declared)).err(), + Some(MindRefusal::ForeignInstance { declared: declared.clone(), mind: BASE.into() }), + "{declared} must be refused as foreign, not folded into {BASE}" + ); + } + } + #[test] fn a_mind_has_one_owner_at_a_time() { let root = tempfile::tempdir().unwrap(); diff --git a/tools/eureka-cut10-mutations.psd1 b/tools/eureka-cut10-mutations.psd1 index 2fb4f82..7a689df 100644 --- a/tools/eureka-cut10-mutations.psd1 +++ b/tools/eureka-cut10-mutations.psd1 @@ -161,6 +161,42 @@ Old = ' if declared != self.instance() {' New = ' if !declared.0.eq_ignore_ascii_case(&self.instance().0) {' } + @{ + Id = 'N3a' + Rule = 'N3: the comparison folds nothing; `_` and `.` stay distinct on both sides.' + Test = 'mind::tests::every_separator_variant_of_a_declared_name_is_foreign_not_the_mind' + Command = 'cargo test -p huginn-mind --lib' + File = 'crates/huginn-mind/src/mind.rs' + Old = ' if declared != self.instance() {' + New = ' if declared.0.replace(''_'', ".") != self.instance().0.replace(''_'', ".") {' + } + @{ + Id = 'N3b' + Rule = 'N3: the comparison folds nothing on the mind''s own side alone; `_` and `-` stay distinct there too.' + Test = 'mind::tests::every_separator_variant_of_a_declared_name_is_foreign_not_the_mind' + Command = 'cargo test -p huginn-mind --lib' + File = 'crates/huginn-mind/src/mind.rs' + Old = ' if declared != self.instance() {' + New = ' if declared.0 != self.instance().0.replace(''_'', "-") {' + } + @{ + Id = 'N3c' + Rule = 'N3: a leading or trailing `-` is part of the name, not padding `trim_matches` may strip before comparing.' + Test = 'mind::tests::every_separator_variant_of_a_declared_name_is_foreign_not_the_mind' + Command = 'cargo test -p huginn-mind --lib' + File = 'crates/huginn-mind/src/mind.rs' + Old = ' if declared != self.instance() {' + New = ' if declared.0.trim_matches(''-'') != self.instance().0.trim_matches(''-'') {' + } + @{ + Id = 'N3d' + Rule = 'N3: a run of `--` is two hyphens, not one collapsed by the comparison before it runs.' + Test = 'mind::tests::every_separator_variant_of_a_declared_name_is_foreign_not_the_mind' + Command = 'cargo test -p huginn-mind --lib' + File = 'crates/huginn-mind/src/mind.rs' + Old = ' if declared != self.instance() {' + New = ' if declared.0.replace("--", "-") != self.instance().0.replace("--", "-") {' + } @{ Id = 'S3wide' Rule = 'The grammar check runs on the declared bytes as given: nothing folds a fullwidth character to its plain ASCII form before asking the leaf''s Slug::validate_slug whether the name is grammatical.' @@ -375,6 +411,46 @@ fn require_grammatical_slug(field: &str, declared: &Slug) -> Result<(), MindRefu CultNetMessage::OperationResponse { payload, .. } => payload.len() as u64 + 231, _ => bytes.len() as u64, }, +'@ + } + @{ + Id = 'N4a' + Rule = 'N4: what is measured holds for more than `message_id`''s length; `operation` and the runtime id are in the encoded envelope too, so a formula of `message_id.len()` alone -- even one shaped to survive a longer id, `+228+id.len()+(id.len()>31)` -- is wrong once the operation or runtime id it never looked at changes length.' + Test = 'serve::tests::the_gates_boundary_holds_across_operation_and_runtime_id' + Command = 'cargo test -p huginn-daemon --lib' + File = 'crates/huginn-daemon/src/serve.rs' + Old = @' + let encoded = match encode_cultnet_message_to_vec(&reply, CultNetWireContract::CultNetSchemaV0) { + Ok(bytes) => bytes.len() as u64, +'@ + New = @' + let encoded = match encode_cultnet_message_to_vec(&reply, CultNetWireContract::CultNetSchemaV0) { + Ok(bytes) => match &reply { + CultNetMessage::OperationResponse { payload, .. } => { + payload.len() as u64 + 228 + message_id.len() as u64 + (message_id.len() > 31) as u64 + } + _ => bytes.len() as u64, + }, +'@ + } + @{ + Id = 'N4b' + Rule = 'N4: a coincidence caught the same way -- `228+id.len()*38/37` fits both of the suite''s `message_id` lengths (three and forty bytes) by the ratio between them, not by measuring the envelope, so it is wrong once the operation or the runtime id changes the envelope''s size at either length.' + Test = 'serve::tests::the_gates_boundary_holds_across_operation_and_runtime_id' + Command = 'cargo test -p huginn-daemon --lib' + File = 'crates/huginn-daemon/src/serve.rs' + Old = @' + let encoded = match encode_cultnet_message_to_vec(&reply, CultNetWireContract::CultNetSchemaV0) { + Ok(bytes) => bytes.len() as u64, +'@ + New = @' + let encoded = match encode_cultnet_message_to_vec(&reply, CultNetWireContract::CultNetSchemaV0) { + Ok(bytes) => match &reply { + CultNetMessage::OperationResponse { payload, .. } => { + payload.len() as u64 + 228 + (message_id.len() as u64 * 38 / 37) + } + _ => bytes.len() as u64, + }, '@ } @{