From d668980cae3dd55352cd8651cec4c5b35fe917b4 Mon Sep 17 00:00:00 2001 From: repo-worker-code-g Date: Sat, 7 Nov 2026 00:19:27 -0600 Subject: [PATCH] =?UTF-8?q?feat(id/idset):=20exact=20Admin=20role=20at=20t?= =?UTF-8?q?he=20frontier=20=E2=80=94=20Expander=20takes=20an=20admin-at-fr?= =?UTF-8?q?ontier=20source=20(#6798)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The membership layer records Admin as Manage, so expand() clamped every Admin role_min to Manage: an Admin-threshold admission (and exclusion) was broader than the set author wrote. The Expander now takes an AdminsAtPosition source beside Membership::members_at_position (modelled on the groups fold admins_at_position replay): members the source names at the pinned frontier are marked Role::Admin in the snapshot, clamp_admin_thresholds is deleted, and a source with no admin data fails closed (Admin admits nobody) instead of widening to Manage. Tests (writer_set_expansion, 15/15): the pinned clamp test is replaced by a fail-closed baseline; new tests pin an admin admitted while a non-admin manager is not, and a member promoted to Admin only after the frontier not admitted by the earlier version. --- pkgs/id/src/idset/expand.rs | 119 ++++++++----------- pkgs/id/tests/writer_set_expansion.rs | 163 ++++++++++++++++++++++---- 2 files changed, 190 insertions(+), 92 deletions(-) diff --git a/pkgs/id/src/idset/expand.rs b/pkgs/id/src/idset/expand.rs index 380398c6ef..0574c9336f 100644 --- a/pkgs/id/src/idset/expand.rs +++ b/pkgs/id/src/idset/expand.rs @@ -18,15 +18,19 @@ //! - A member's role is their access at that frontier, mapped the way the //! groups fold maps it (`access_role` in `crate::groups::fold`). The //! membership layer records Admin as Manage — Admin is a policy role the -//! groups fold tracks apart from access — so it cannot distinguish an -//! admin from any other manager at a frontier. A `role_min` of -//! [`Role::Admin`] is therefore evaluated at [`Role::Manage`], the -//! highest role the layer can answer for: admins are admitted (they are -//! managers in the layer), and so are non-admin managers. Answering -//! Admin exactly needs the fold's admin-at-frontier state plumbed into -//! expansion, which no caller can supply yet; the limitation is pinned -//! by a named test beside the spec tests (`test_expand_role_min_admin_*` -//! in `tests/writer_set_expansion.rs`). +//! groups fold tracks apart from access — so the layer alone cannot +//! distinguish an admin from any other manager at a frontier. The +//! expander therefore takes an admin-at-frontier source beside the +//! layer ([`AdminsAtPosition`], modelled on the fold's +//! `admins_at_position` replay): members the source names for the +//! pinned frontier are marked [`Role::Admin`] in the snapshot, so a +//! `role_min` of [`Role::Admin`] is answered exactly — an admin is +//! admitted, a non-admin manager is not, and a member promoted to +//! Admin only after the frontier is not an admin for this version. +//! A source with no admin data returns an empty set and Admin +//! thresholds fail closed (admit nobody) rather than widening to +//! Manage. The behaviour is pinned by the `test_expand_role_min_admin_*` +//! tests in `tests/writer_set_expansion.rs`. //! - Resolved groups are cached under the §4.5 key `(group, frontier, R)`, //! where `R` is the caller's resolution-state identity //! ([`SetVersion::resolution`]). Within one resolution state an expansion @@ -96,16 +100,32 @@ type CacheKey = (Hash, Vec, Hash); /// One expander serves one resolution epoch of one store: when the store's /// resolution state moves (a fork proof lands), build a fresh expander /// over the rebuilt layer and name the new `R` in each [`SetVersion`]. +/// The admin-at-frontier source an [`Expander`] consults beside +/// [`Membership::members_at_position`]: given a causal frontier and a +/// group, the author keys that held the Admin policy role in that group +/// at that frontier. The model implementation is the groups fold's +/// `admins_at_position` replay (`crate::groups::fold`), which replays +/// settled group events at a causal position; a caller adapts it by +/// closing over its fold state and event index. The source is fixed for +/// the expander's life, so the §4.5 cache key needs no admin component: +/// one expander serves one resolution epoch, and the source is part of +/// that epoch's state. +pub type AdminsAtPosition<'a> = dyn Fn(&[Hash], Hash) -> BTreeSet + 'a; + pub struct Expander<'a> { membership: &'a mut Membership, + admins_at: &'a AdminsAtPosition<'a>, cache: HashMap>, } impl<'a> Expander<'a> { - /// An expander over `membership` with an empty cache. - pub fn new(membership: &'a mut Membership) -> Self { + /// An expander over `membership` with an empty cache, answering + /// Admin thresholds exactly from the `admins_at` source (see + /// [`AdminsAtPosition`]). + pub fn new(membership: &'a mut Membership, admins_at: &'a AdminsAtPosition<'a>) -> Self { Self { membership, + admins_at, cache: HashMap::new(), } } @@ -168,17 +188,10 @@ impl<'a> Expander<'a> { if admits_public(version.set, retro) && !public_excluded { return Err(ExpandError::PublicEntry); } - // The membership layer's highest distinguishable role is Manage - // (Admin is recorded as Manage there), so an Admin threshold is - // evaluated at Manage for the layer-backed snapshot; see the - // module doc for the limitation and its follow-up. - let effective; - let set = if has_admin_threshold(version.set) { - effective = clamp_admin_thresholds(version.set); - &effective - } else { - version.set - }; + // Admin thresholds are answered exactly: the snapshot built by + // `group_members` marks the admin source's members `Role::Admin` + // (see the module doc), so the set is evaluated as written. + let set = version.set; let mut groups = Vec::new(); for group in referenced_groups(set) { let members = self.group_members(group, version)?; @@ -216,12 +229,22 @@ impl<'a> Expander<'a> { if let Some(members) = self.cache.get(&key) { return Ok(members.clone()); } - let members: BTreeMap = self + let mut members: BTreeMap = self .membership .members_at_position(&version.frontier, group)? .into_iter() .map(|(author, access)| (author, access_role(&access))) .collect(); + // The layer records Admin as Manage; the admin source is the + // only frontier-pinned evidence of the policy role, so its + // members are upgraded in the snapshot. A named author who is + // not a member at this frontier stays absent: admin status + // never creates membership. + for author in (self.admins_at)(&version.frontier, group) { + if let Some(role) = members.get_mut(&author) { + *role = Role::Admin; + } + } self.cache.insert(key, members.clone()); Ok(members) } @@ -253,7 +276,8 @@ fn referenced_groups(set: &WriterSet) -> Vec { /// A member's role as their membership access gives it. Mirrors the groups /// fold's `access_role`: Admin is recorded apart from the access level and -/// shows as Manage here. +/// shows as Manage here; `group_members` upgrades the members the admin +/// source names to `Role::Admin`. fn access_role(access: &Access<()>) -> Role { if access.is_manage() { Role::Manage @@ -265,50 +289,3 @@ fn access_role(access: &Access<()>) -> Role { Role::Pull } } - -/// Whether any group entry in the set (admissions or exclusions) carries -/// an Admin `role_min` threshold. -fn has_admin_threshold(set: &WriterSet) -> bool { - set.entries - .iter() - .chain(set.excludes.0.iter()) - .any(|entry| { - matches!( - entry, - Entry::Group { - role_min: Role::Admin, - .. - } - ) - }) -} - -/// A copy of `set` with every Admin `role_min` lowered to Manage, the -/// highest role the membership layer distinguishes at a frontier. -fn clamp_admin_thresholds(set: &WriterSet) -> WriterSet { - fn clamp(entries: &[Entry]) -> Vec { - entries - .iter() - .map(|entry| match entry { - Entry::Group { - id, - role_min: Role::Admin, - include_history, - } => Entry::Group { - id: *id, - role_min: Role::Manage, - include_history: *include_history, - }, - other => other.clone(), - }) - .collect() - } - WriterSet { - set_id: set.set_id, - version: set.version, - owner: set.owner, - scope: set.scope.clone(), - entries: clamp(&set.entries), - excludes: super::Excludes(clamp(&set.excludes.0)), - } -} diff --git a/pkgs/id/tests/writer_set_expansion.rs b/pkgs/id/tests/writer_set_expansion.rs index d291a94089..e74a2a28b6 100644 --- a/pkgs/id/tests/writer_set_expansion.rs +++ b/pkgs/id/tests/writer_set_expansion.rs @@ -123,8 +123,16 @@ fn key_entry(key: u8, include_history: bool) -> Entry { } } +/// An admin source with no admin data at any frontier: Admin thresholds +/// fail closed (admit nobody) for expanders built on it. Tests that model +/// admins supply their own source keyed on the frontier, the way the +/// groups fold's replay would answer for a real causal position. +fn no_admins(_: &[Hash], _: Hash) -> BTreeSet<[u8; 32]> { + BTreeSet::new() +} + fn expand(membership: &mut Membership, set: &WriterSet, frontier: Vec) -> BTreeSet<[u8; 32]> { - Expander::new(membership) + Expander::new(membership, &no_admins) .expand(&SetVersion { set, frontier, @@ -257,7 +265,7 @@ fn test_expand_retro_subset_and_empty_without_history_entries() { frontier: vec![group()], resolution: hash(80), }; - let mut expander = Expander::new(&mut membership); + let mut expander = Expander::new(&mut membership, &no_admins); let full = expander.expand(&version).unwrap(); let retro = expander.expand_retro(&version).unwrap(); @@ -319,7 +327,7 @@ fn test_expand_fork_invalidated_grant_changes_expansion_and_cache_keys_on_resolu // The grant stands: Dave is in the expansion under R1. let mut membership = base_layer(); add_dave(&mut membership, true); - let mut expander = Expander::new(&mut membership); + let mut expander = Expander::new(&mut membership, &no_admins); let under_r1 = expander .expand(&SetVersion { set: &set, @@ -355,7 +363,7 @@ fn test_expand_fork_invalidated_grant_changes_expansion_and_cache_keys_on_resolu // same frontier no longer expands to Dave. let mut forked = base_layer(); add_dave(&mut forked, false); - let under_r2 = Expander::new(&mut forked) + let under_r2 = Expander::new(&mut forked, &no_admins) .expand(&SetVersion { set: &set, frontier, @@ -438,7 +446,7 @@ fn test_expand_public_entry_refused_not_materialized() { frontier: vec![group()], resolution: hash(80), }; - let mut expander = Expander::new(&mut membership); + let mut expander = Expander::new(&mut membership, &no_admins); assert_eq!(expander.expand(&version), Err(ExpandError::PublicEntry)); assert_eq!(expander.expand_retro(&version).unwrap(), BTreeSet::new()); } @@ -462,7 +470,7 @@ fn test_expand_public_exclusion_admits_nobody_not_refused() { frontier: vec![group()], resolution: hash(80), }; - let mut expander = Expander::new(&mut membership); + let mut expander = Expander::new(&mut membership, &no_admins); assert_eq!(expander.expand(&version).unwrap(), BTreeSet::new()); assert_eq!(expander.expand_retro(&version).unwrap(), BTreeSet::new()); @@ -479,19 +487,132 @@ fn test_expand_public_exclusion_admits_nobody_not_refused() { ); } -/// Codex P2 on PR #6787: the membership layer records Admin as Manage, so -/// an Admin `role_min` is answered at Manage, the layer's top role — -/// admins (managers in the layer) are admitted rather than nobody, and -/// the approximation is pinned here until admin-at-frontier plumbing -/// exists. Bob (Read) and Carol (Write) stay below the threshold. +/// #6798, fail-closed baseline: the membership layer records Admin as +/// Manage and the default test source carries no admin data, so an Admin +/// `role_min` admits nobody — it no longer widens to Manage (the pre-#6798 +/// clamp, pinned by the test this one replaces). Manage itself is still +/// answered exactly by the layer: Alice (Manage) clears a Manage entry, +/// Bob (Read) and Carol (Write) stay below it. +#[test] +fn test_expand_role_min_admin_without_admin_data_admits_nobody() { + let mut membership = base_layer(); + let admin_set = set(vec![group_entry(Role::Admin, false)], vec![]); + assert!(expand(&mut membership, &admin_set, vec![group()]).is_empty()); + let manage_set = set(vec![group_entry(Role::Manage, false)], vec![]); + let expanded = expand(&mut membership, &manage_set, vec![group()]); + assert_eq!(expanded, [author(ALICE)].into_iter().collect()); +} + +/// #6798, exactness: with an admin source naming Alice at the frontier, +/// an Admin `role_min` admits Alice and NOT Erin, a second manager who +/// holds the layer's top access (Manage) but not the Admin policy role. +/// (Before the plumbing, the clamp admitted every manager.) #[test] -fn test_expand_role_min_admin_is_answered_at_manage_the_layers_top_role() { +fn test_expand_role_min_admin_admits_admin_not_non_admin_manager() { let mut membership = base_layer(); + let add_erin = hash(11); + feed_auth( + &mut membership, + add_erin, + vec![group()], + author(ALICE), + Some(group()), + GroupAction::Add { + member: individual(author(ERIN)), + access: Access::manage(), + }, + ); + // The fold-side answer at this frontier: Alice is the group's admin; + // Erin is a manager, never an admin. + let admins = |frontier: &[Hash], _group: Hash| -> BTreeSet<[u8; 32]> { + if frontier == [add_erin] { + [author(ALICE)].into_iter().collect() + } else { + BTreeSet::new() + } + }; + let admin_set = set(vec![group_entry(Role::Admin, false)], vec![]); + let expanded = Expander::new(&mut membership, &admins) + .expand(&SetVersion { + set: &admin_set, + frontier: vec![add_erin], + resolution: hash(80), + }) + .unwrap(); + assert_eq!(expanded, [author(ALICE)].into_iter().collect()); + // Sanity: a Manage threshold at the same frontier admits both + // managers, so Erin's exclusion above is the Admin distinction, not + // a membership miss. + let manage_set = set(vec![group_entry(Role::Manage, false)], vec![]); + let managers = expand(&mut membership, &manage_set, vec![add_erin]); + assert_eq!( + managers, + [author(ALICE), author(ERIN)].into_iter().collect() + ); +} + +/// #6798, frontier pinning for the policy role (the Admin analogue of +/// spec test 6): Erin is promoted to Admin only at the later position +/// (modelled by the source, whose answer changes at the hash(12) +/// frontier — the fold replays exactly this way over settled events). +/// A set version pinned at the earlier frontier must not admit her; the +/// same set judged at the later frontier does. +#[test] +fn test_expand_role_min_admin_promoted_after_frontier_is_not_admitted() { + let mut membership = base_layer(); + let add_erin = hash(11); + feed_auth( + &mut membership, + add_erin, + vec![group()], + author(ALICE), + Some(group()), + GroupAction::Add { + member: individual(author(ERIN)), + access: Access::manage(), + }, + ); + // A later layer event marks the position at which the fold-side + // promotion to Admin has happened. + let later = hash(12); + feed_auth( + &mut membership, + later, + vec![add_erin], + author(ALICE), + Some(group()), + GroupAction::Add { + member: individual(author(FRANK)), + access: Access::read(), + }, + ); + let admins = |frontier: &[Hash], _group: Hash| -> BTreeSet<[u8; 32]> { + if frontier == [later] { + [author(ERIN)].into_iter().collect() + } else { + BTreeSet::new() + } + }; let set = set(vec![group_entry(Role::Admin, false)], vec![]); - let expanded = expand(&mut membership, &set, vec![group()]); - assert!(expanded.contains(&author(ALICE))); - assert!(!expanded.contains(&author(BOB))); - assert!(!expanded.contains(&author(CAROL))); + let pinned = Expander::new(&mut membership, &admins) + .expand(&SetVersion { + set: &set, + frontier: vec![add_erin], + resolution: hash(80), + }) + .unwrap(); + assert!( + pinned.is_empty(), + "promotion after the frontier leaked into the pinned expansion: {pinned:?}" + ); + let at_promotion = Expander::new(&mut membership, &admins) + .expand(&SetVersion { + set: &set, + frontier: vec![later], + resolution: hash(80), + }) + .unwrap(); + assert_eq!(at_promotion, [author(ERIN)].into_iter().collect()); } // --------------------------------------------------------------------------- @@ -555,12 +676,12 @@ proptest! { let mut membership = base_layer(); let plain = set(entries.clone(), excludes.clone()); let shuffled = set(permute(&entries, seed), permute(&excludes, seed + 1)); - let a = Expander::new(&mut membership).expand(&SetVersion { + let a = Expander::new(&mut membership, &no_admins).expand(&SetVersion { set: &plain, frontier: vec![group()], resolution: hash(80), }); - let b = Expander::new(&mut membership).expand(&SetVersion { + let b = Expander::new(&mut membership, &no_admins).expand(&SetVersion { set: &shuffled, frontier: vec![group()], resolution: hash(80), @@ -583,7 +704,7 @@ proptest! { frontier: vec![group()], resolution: hash(80), }; - let mut expander = Expander::new(&mut membership); + let mut expander = Expander::new(&mut membership, &no_admins); if let Ok(full) = expander.expand(&version) { let retro = expander.expand_retro(&version).unwrap(); prop_assert!(retro.is_subset(&full), "retro {retro:?} not a subset of {full:?}"); @@ -606,7 +727,7 @@ proptest! { frontier: vec![group()], resolution: hash(80), }; - let before = Expander::new(&mut membership).expand(&version); + let before = Expander::new(&mut membership, &no_admins).expand(&version); let mut after = vec![group()]; for (i, (who, kind, level)) in ops.iter().enumerate() { @@ -645,7 +766,7 @@ proptest! { after = vec![id]; } - let after_expand = Expander::new(&mut membership).expand(&version); + let after_expand = Expander::new(&mut membership, &no_admins).expand(&version); prop_assert_eq!(before, after_expand); } }