diff --git a/epiphany-pipeline/src/lib.rs b/epiphany-pipeline/src/lib.rs index bdddf614..b3193a58 100644 --- a/epiphany-pipeline/src/lib.rs +++ b/epiphany-pipeline/src/lib.rs @@ -612,6 +612,11 @@ fn key_segment(value: &str) -> String { /// root segment, so its local carries none; a reader never consults it. const ROOT_LOCAL: &str = "self"; +/// The bound on a composed local, whole, in UTF-8 bytes. It is the only depth +/// limit on a chain of resolutions, and the depth test derives its expectation +/// from this name rather than restating the number. +const LOCAL_MAX: usize = 64; + /// Composes and validates a local: every part is a `Label`, and the join is /// bounded whole. Both rules live here because this is the only way a local is /// built; `pipeline_key` has no other path to a key string. Parts are a slice, @@ -621,7 +626,7 @@ fn local(field: &str, parts: &[&str]) -> Result { label_text(field, part)?; } let joined = parts.join("."); - if joined.len() > 64 { + if joined.len() > LOCAL_MAX { return Err(format_error(field, &joined)); } Ok(joined) @@ -1268,7 +1273,9 @@ mod tests { /// because the key tests assert strings and never read one back: an /// instance keys to its slug and then fails to parse as an instance id, so /// no `PipelineRef` and no resolution could ever name one. Every kind now - /// reads back with no kind excused, a resolution included. + /// reads back with no kind excused, a resolution included, and a root kind + /// read as the other root kind is refused: the reader checks the kind + /// segment for the roots exactly as for every other kind. #[test] fn keys_read_back_as_ids_of_their_kind() -> Result<()> { for (document, expected) in samples() { @@ -1282,6 +1289,24 @@ mod tests { ); } + let campaign_key = pipeline_key(&campaign_sample())?; + let instance_key = pipeline_key(&PipelineDocument::Instance(instance_sample()))?; + assert_eq!( + pipeline_id("read_back", &instance_key, PipelineKind::Instance), + Ok((INSTANCE, ROOT_LOCAL)), + "an instance key reads back as an instance" + ); + assert_eq!( + pipeline_id("read_back", &instance_key, PipelineKind::Campaign), + Err(format_error("read_back", &instance_key)), + "an instance key is not a campaign id" + ); + assert_eq!( + pipeline_id("read_back", &campaign_key, PipelineKind::Instance), + Err(format_error("read_back", &campaign_key)), + "a campaign key is not an instance id" + ); + // The instance root's own key segment is validated, not merely bounded. // Nothing else stands between a `Slug` and a key: a space would compose // a store key carrying a byte no `Label` may hold. @@ -1472,6 +1497,52 @@ mod tests { assert_ne!(campaign, instance, "a campaign and an instance of one slug are two documents"); } + /// The root is a `Slug`, so a dotted root keys and reads back whole on + /// both sides: the writer's root check and the reader's root check are + /// each `dotted_text`, not `label_text`. A campaign `game.cult` and an + /// instance `ygg.drasil` key, a document under the dotted campaign keys, + /// and each reads back through `pipeline_id` recovering the root exactly. + #[test] + fn dotted_roots_key_and_read_back() { + let PipelineDocument::Campaign(mut campaign) = campaign_sample() else { unreachable!() }; + campaign.slug = slug("game.cult"); + let campaign_key = pipeline_key(&PipelineDocument::Campaign(campaign)).expect("a dotted campaign keys"); + assert_eq!(campaign_key, "game.cult:campaign:self"); + assert_eq!( + pipeline_id("read_back", &campaign_key, PipelineKind::Campaign), + Ok(("game.cult", ROOT_LOCAL)), + "the dotted campaign root reads back whole" + ); + + let mut instance = instance_sample(); + instance.instance = slug("ygg.drasil"); + let instance_key = pipeline_key(&PipelineDocument::Instance(instance)).expect("a dotted instance keys"); + assert_eq!(instance_key, "ygg.drasil:instance:self"); + assert_eq!( + pipeline_id("read_back", &instance_key, PipelineKind::Instance), + Ok(("ygg.drasil", ROOT_LOCAL)), + "the dotted instance root reads back whole" + ); + + let PipelineDocument::Target(mut target) = samples().remove(1).0 else { unreachable!() }; + target.campaign = slug("game.cult"); + let target_key = pipeline_key(&PipelineDocument::Target(target)).expect("a target under a dotted campaign keys"); + assert_eq!(target_key, "game.cult:target:r2"); + assert_eq!( + pipeline_id("read_back", &target_key, PipelineKind::Target), + Ok(("game.cult", "r2")), + "the dotted root and the local both read back" + ); + + let resolution = resolution_of(PipelineKind::Campaign, &campaign_key).expect("a resolution of a dotted campaign keys"); + assert_eq!(resolution, "game.cult:resolution:campaign.self"); + assert_eq!( + pipeline_id("read_back", &resolution, PipelineKind::Resolution), + Ok(("game.cult", "campaign.self")), + "a resolution under a dotted root reads back" + ); + } + /// Defect 2: a resolution's key carries its subject's kind as a literal /// part of the local, so nothing infers it and two subjects of one local /// and different kinds resolve to two keys. Each reads back through the @@ -1503,9 +1574,10 @@ mod tests { /// subject like any other, and the key reads back to the inner key. Depth /// is bounded by the local alone: each nesting prepends `resolution.`, 11 /// bytes, so a chain `n` deep over a depth-one local of `L` bytes composes - /// `11 * (n - 1) + L` bytes against 64. The deepest chain that keys is - /// therefore `(64 - L) / 11 + 1`, and it depends on the subject: `ruling.A` - /// (8 bytes) keys six deep, `question.Q1` (11 bytes) five. + /// `11 * (n - 1) + L` bytes against `LOCAL_MAX`. The deepest chain that + /// keys is therefore `(LOCAL_MAX - L) / 11 + 1`, and it depends on the + /// subject: at 64, `ruling.A` (8 bytes) keys six deep, `question.Q1` (11 + /// bytes) five. #[test] fn a_resolution_of_a_resolution_reads_back() { let inner = resolution_of(PipelineKind::Question, &id("question", "Q1").0).expect("the inner keys"); @@ -1524,13 +1596,13 @@ mod tests { for (subject_kind, subject_id) in [(PipelineKind::Ruling, id("ruling", "A")), (PipelineKind::Question, id("question", "Q1"))] { let mut key = resolution_of(subject_kind, &subject_id.0).expect("the depth-one resolution keys"); let depth_one = key.len() - prefix; - let deepest = (64 - depth_one) / nesting + 1; + let deepest = (LOCAL_MAX - depth_one) / nesting + 1; for depth in 2..=deepest { key = resolution_of(PipelineKind::Resolution, &key) .unwrap_or_else(|error| panic!("{subject_id:?} depth {depth}: {error}")); assert_eq!(key.len() - prefix, nesting * (depth - 1) + depth_one, "{key}: depth {depth} local length"); } - assert!(key.len() - prefix <= 64, "{key}: the deepest chain fits the local"); + assert!(key.len() - prefix <= LOCAL_MAX, "{key}: the deepest chain fits the local"); assert!( matches!( resolution_of(PipelineKind::Resolution, &key), diff --git a/tools/eureka-cut6-mutations.psd1 b/tools/eureka-cut6-mutations.psd1 index aa5e90e5..bc5251e4 100644 --- a/tools/eureka-cut6-mutations.psd1 +++ b/tools/eureka-cut6-mutations.psd1 @@ -15,17 +15,23 @@ # each note below names where the rule is pinned now. # # - M5, "a finding label carries no dot": the finding arm no longer validates -# the label itself; the label goes through `local`, which refuses a dotted -# part as `finding.key`. Cut 6b's F5 accepted that refusal; the rule is -# pinned by `composed_keys_cannot_collide` and Cut 6b's M6. +# the label itself; the label goes through `local` as its last part, which +# refuses a dotted part as `finding.key`. Cut 6b's F5 accepted that +# refusal; the rule is pinned on the composer by Cut 6b's N3 (the last part +# skips the label check) and M6 (the head does), and through the documents +# by `composed_keys_cannot_collide`. # - M12, "a repo inside a key is escaped", and M13, "the repo escape is -# injective": `key_segment` replaced `repo_segment`; the escape and its -# injectivity are pinned by Cut 6b's M6 and the S2 receiver pair. +# injective": `key_segment` replaced `repo_segment`; each is re-anchored on +# it as Cut 6b's N7 (the escape is the identity) and N8 (`/` to a bare `_`, +# `_` unescaped), both killed by `stewardship_key_escapes_the_repo_slash`. # - M14, "an instance's slug is validated where its key is composed": the # instance arm composes through the shared root check; pinned by Cut 6b's # M5 and M1. # - M15, "the id reader knows the same roots the key writer does": every kind -# reads back with no root excused; pinned by Cut 6b's M1. +# reads back with no root excused; pinned by Cut 6b's N6 (the reader's kind +# check excuses the Instance kind), killed by +# `keys_read_back_as_ids_of_their_kind` reading a campaign key as an +# instance, and by Cut 6b's M1. @{ Mutations = @( @{ diff --git a/tools/eureka-cut6b-mutations.psd1 b/tools/eureka-cut6b-mutations.psd1 index d2647ae6..43c1e34b 100644 --- a/tools/eureka-cut6b-mutations.psd1 +++ b/tools/eureka-cut6b-mutations.psd1 @@ -8,8 +8,17 @@ # # M1-M6 are the cut's own rules. S2, S4 and S5 are the mutations Soul's pass # found surviving; each is kept exactly as Soul wrote it, against the test the -# fix batch extended to kill it. The file holds an `é` literal in +# fix batch extended to kill it. N3, N6, N7, N8, N9 and N10 are Soul's second +# pass: N7 and N8 re-anchor Cut 6's M12 and M13 on `key_segment`, N3 is the +# analogue of Cut 6's M5 on `local`, N6 of Cut 6's M15 on the reader, and N9 +# and N10 are the dotted-root mutations that survived until +# `dotted_roots_key_and_read_back`. The file holds an `é` literal in # `bounds_refuse_in_utf8_bytes`, which is why the harness I/O is byte-exact. +# +# Soul's N4 moved the bound in `local` and both 64s in the depth test to 60 +# together and survived: the test restated the literal. `LOCAL_MAX` now names +# the bound, `local` and the depth test both read it, and the mutation is no +# longer expressible as a single-site edit, so it has no entry here. @{ Mutations = @( @{ @@ -38,7 +47,7 @@ Rule = 'A composed local is bounded whole, not only per part.' Test = 'tests::a_composed_local_is_bounded_whole' Old = @' - if joined.len() > 64 { + if joined.len() > LOCAL_MAX { return Err(format_error(field, &joined)); } '@ @@ -97,8 +106,69 @@ Id = 'S5' Rule = 'The whole-local bound is 64 exactly: 65 refuses.' Test = 'tests::a_composed_local_is_bounded_whole' - Old = ' if joined.len() > 64 {' - New = ' if joined.len() > 65 {' + Old = ' if joined.len() > LOCAL_MAX {' + New = ' if joined.len() > LOCAL_MAX + 1 {' + }, + # Soul N3, the analogue of Cut 6's M5 ("a finding label carries no + # dot") on the composer: the join bound stays and the last part skips + # `label_text`. A finding's label is the last part of its local. + @{ + Id = 'N3' + Rule = 'Every local part is a label, the last included (Cut 6 M5 on the composer).' + Test = 'tests::no_local_part_carries_the_separator' + Old = @' + for part in parts { + label_text(field, part)?; + } +'@ + New = @' + for part in &parts[..parts.len() - 1] { + label_text(field, part)?; + } +'@ + }, + # Soul N6, the analogue of Cut 6's M15 on the reader: the kind check + # excuses the Instance kind. A campaign key read as an instance kills it. + @{ + Id = 'N6' + Rule = 'The reader checks the kind segment for a root kind as for every other (Cut 6 M15).' + Test = 'tests::keys_read_back_as_ids_of_their_kind' + Old = ' if name != kind.name() {' + New = ' if name != kind.name() && kind != PipelineKind::Instance {' + }, + # Soul N7, Cut 6's M12 re-anchored on `key_segment`: the escape is + # the identity, so a repo's slash reaches the local and is refused. + @{ + Id = 'N7' + Rule = 'A repo inside a key is escaped (Cut 6 M12).' + Test = 'tests::stewardship_key_escapes_the_repo_slash' + Old = ' value.replace(''_'', "__").replace(''/'', "_-").replace(''.'', "_d")' + New = ' value.to_string()' + }, + # Soul N8, Cut 6's M13 re-anchored on `key_segment`: `/` becomes a + # bare `_` and `_` is not escaped, so two repos claim one key. + @{ + Id = 'N8' + Rule = 'The repo escape is injective (Cut 6 M13).' + Test = 'tests::stewardship_key_escapes_the_repo_slash' + Old = ' value.replace(''_'', "__").replace(''/'', "_-").replace(''.'', "_d")' + New = ' value.replace(''/'', "_").replace(''.'', "_d")' + }, + # Soul N9 and N10: the root is a `Slug`, on the writer and the reader. + # Both survived while no test keyed or read back a dotted root. + @{ + Id = 'N9' + Rule = 'The composer''s root check is a slug check: a dotted root keys.' + Test = 'tests::dotted_roots_key_and_read_back' + Old = ' dotted_text(root_field, root)?;' + New = ' label_text(root_field, root)?;' + }, + @{ + Id = 'N10' + Rule = 'The reader''s root check is a slug check: a dotted root reads back.' + Test = 'tests::dotted_roots_key_and_read_back' + Old = ' dotted_text(field, root)?;' + New = ' label_text(field, root)?;' } ) }