From 79407eb95161ff0208e09742571896c39f548073 Mon Sep 17 00:00:00 2001 From: repo-worker-code Date: Thu, 29 Oct 2026 04:16:42 -0500 Subject: [PATCH 1/4] =?UTF-8?q?groups:=20build=20weak=20removal=20?= =?UTF-8?q?=E2=80=94=20concurrent=20grants=20and=20device=20grants=20stand?= =?UTF-8?q?=20(#161)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit policy.removal was recorded and displayed but never read by the voiding track, so a group configured removal=weak silently got strong semantics. - delegation rs void(): skip concurrent-voiding when the group's removal is weak (design 6.3: only the authority check applies). - fold.rs void_device(): the same branch for device grants. - the authority check judged activity and role at the head membership, so a grant concurrent with the removal was refused NotActive before it could stand. Under weak remove both are read at the event's causal position (is_active_for/access_for, Membership::access_at_position_shared). Strong remove keeps reading the head state, byte-identical to today. Tests contrast weak vs strong for the delegation void path (design 6.4's week-late case) and the device-grant void path, and check that an event causally after the removal stays invalid under weak. --- pkgs/id/src/groups/delegation.rs | 14 ++-- pkgs/id/src/groups/fold.rs | 77 +++++++++++++++++--- pkgs/id/src/groups/policy.rs | 5 +- pkgs/id/src/groups/tests.rs | 117 ++++++++++++++++++++++++++++++- pkgs/id/src/membership/layer.rs | 18 +++++ 5 files changed, 211 insertions(+), 20 deletions(-) diff --git a/pkgs/id/src/groups/delegation.rs b/pkgs/id/src/groups/delegation.rs index 45076382d8..2773b7007d 100644 --- a/pkgs/id/src/groups/delegation.rs +++ b/pkgs/id/src/groups/delegation.rs @@ -8,8 +8,8 @@ //! //! Each link attenuates: a grantor passes on no more than it holds. A link carries authority at a //! position when its grantor was valid where it was made, the grant is not voided by a concurrent -//! removal of its grantor, no veto in the position's past cuts it, and the group's mode lets it -//! stand there (design §7.3). +//! removal of its grantor (strong remove only; weak remove stands it, design §6.3), no veto in the +//! position's past cuts it, and the group's mode lets it stand there (design §7.3). //! //! The mode decides a removed member's grants. Under `keep_prior` they stand. Under `cascade` they //! stand only while the grantor still holds the scope. Under `transfer` the removal's successor @@ -377,10 +377,14 @@ impl Engine { } /// Whether a settled removal of the grantor is concurrent with the grant: neither is in the - /// other's causal past. Strong remove voids such a grant (design §6.2 and §7.3). The removals - /// are read from the events, not from the round's state: a concurrent removal may be judged - /// after the grant in canonical order. + /// other's causal past, and the group voids such a grant. Strong remove voids it (design §6.2 + /// and §7.3). Weak remove stands it, and only the authority check decides later events + /// (design §6.3). The removals are read from the events, not from the round's state: a + /// concurrent removal may be judged after the grant in canonical order. fn void(&self, by_id: &BTreeMap, grant: &Grant) -> bool { + if !self.strong_removal(grant.group) { + return false; + } let past_grant = causal_past(by_id, &[grant.id]); self.settled .iter() diff --git a/pkgs/id/src/groups/fold.rs b/pkgs/id/src/groups/fold.rs index 8f8afd92dd..fdaae7776d 100644 --- a/pkgs/id/src/groups/fold.rs +++ b/pkgs/id/src/groups/fold.rs @@ -35,7 +35,7 @@ use crate::rejection::Reason; use super::delegation::{Grant, Removal, removed_member}; use super::friendship::{Pair, friendship_id}; -use super::policy::{OnMemberRemoved, Policy, Template}; +use super::policy::{OnMemberRemoved, Policy, Removal as RemovalMode, Template}; use super::{Body, Create, Delegate, DeviceOp, Event, Key, MemberOp, Role}; /// Why an event had no effect. A refused event changes no state. It is still decided, so the @@ -278,9 +278,9 @@ impl Projection { } /// Whether the grant with this event ID is in force at the head of the events (design §7.3). - /// A concurrent removal of its grantor voids it, a veto cuts it, and the group's mode decides - /// the rest. The chain above the grant is not checked here. Use the outcomes of the actions - /// it was meant to allow for that. + /// A concurrent removal of its grantor voids it under strong remove; weak remove stands it. A + /// veto cuts it, and the group's mode decides the rest. The chain above the grant is not + /// checked here. Use the outcomes of the actions it was meant to allow for that. pub fn grant_in_force(&self, id: &Hash) -> bool { self.in_force.contains(id) } @@ -995,6 +995,46 @@ impl Engine { self.listed(group, key) && (record.friendship.is_none() || record.present.contains(key)) } + /// Whether `key` is an active member of `group` at the causal position `after`: listed there, + /// and present there for a friendship. Weak remove judges an event's authority at its own + /// position, so an event concurrent with a removal keeps the standing its author had there + /// (design §6.3). This is the slow path: it does not use the membership layer's frontier memo. + fn is_active_at(&self, group: &Hash, record: &Record, key: &Key, after: &[Hash]) -> bool { + let listed = self + .membership + .access_at_position_shared(after, *group, *key) + .ok() + .flatten() + .is_some(); + listed && (record.friendship.is_none() || record.present.contains(key)) + } + + /// Whether `key` is active in `group` for an event at the causal position `after`. Strong remove + /// reads the current state, which is what it has always done; weak remove reads the position + /// (design §6.3). + fn is_active_for(&self, group: &Hash, record: &Record, key: &Key, after: &[Hash]) -> bool { + if self.strong_removal(*group) { + self.is_active(group, record, key) + } else { + self.is_active_at(group, record, key, after) + } + } + + /// The member's role in `group` at the causal position `after`, or `None` when it holds none + /// there. Strong remove reads the current state; weak remove reads the position, so an event + /// concurrent with a removal keeps its author's role there (design §6.3). + fn access_for(&self, group: &Hash, key: &Key, after: &[Hash]) -> Option { + if self.strong_removal(*group) { + return self.access(group, key); + } + self.membership + .access_at_position_shared(after, *group, *key) + .ok() + .flatten() + .map(|access| access_role(&access)) + .or_else(|| self.access(group, key)) + } + /// Resolves whether `actor` is an active authorized device for an account in `group` at `after`. /// Returns `Some((account, authorized_scope))` if so. pub(super) fn authorized_device_account( @@ -1054,23 +1094,25 @@ impl Engine { event: &Event, ) -> Option<(Key, Role)> { let record = self.groups.get(&group)?; - if self.is_active(&group, record, &event.author) { + if self.is_active_for(&group, record, &event.author, &event.after) { let role = if record.admins.contains(&event.author) { Role::Admin } else { - self.access(&group, &event.author).unwrap_or(Role::Pull) + self.access_for(&group, &event.author, &event.after) + .unwrap_or(Role::Pull) }; Some((event.author, role)) } else if let Some((account, device_scope)) = self.authorized_device_account(by_id, group, event.author, &event.after) { - if !self.is_active(&group, record, &account) { + if !self.is_active_for(&group, record, &account, &event.after) { return None; } let account_role = if record.admins.contains(&account) { Role::Admin } else { - self.access(&group, &account).unwrap_or(Role::Pull) + self.access_for(&group, &account, &event.after) + .unwrap_or(Role::Pull) }; Some((account, account_role.min(device_scope))) } else { @@ -1078,8 +1120,23 @@ impl Engine { } } - /// Whether a settled removal of the authorizing account is concurrent with the device grant. + /// Whether the group voids the events that are concurrent with a removal of their author: strong + /// remove does (design §6.2). Weak remove stands them, and only the authority check applies + /// (design §6.3). A group with no record yet, or with `removal = strong` (the default), voids + /// them. The record read is the group's current one, as with its delegation mode. + pub(super) fn strong_removal(&self, group: Hash) -> bool { + self.groups + .get(&group) + .is_none_or(|record| record.policy.removal == RemovalMode::Strong) + } + + /// Whether a settled removal of the authorizing account is concurrent with the device grant, and + /// the group voids such a grant. Strong remove voids it (design §6.2). Under weak remove only + /// the authority check applies, so a concurrent device grant stands (design §6.3). fn void_device(&self, by_id: &BTreeMap, grant: &DeviceGrant) -> bool { + if !self.strong_removal(grant.group) { + return false; + } let past_grant = causal_past(by_id, &[grant.id]); self.settled .iter() @@ -1108,7 +1165,7 @@ impl Engine { Ok(record) => record, Err(refusal) => return Ok(Err(refusal)), }; - if !self.is_active(&group, record, &event.author) { + if !self.is_active_for(&group, record, &event.author, &event.after) { return Ok(Err(Refusal::NotActive)); } let is_admin = record.admins.contains(&event.author); diff --git a/pkgs/id/src/groups/policy.rs b/pkgs/id/src/groups/policy.rs index 79506f926b..2ec3693b24 100644 --- a/pkgs/id/src/groups/policy.rs +++ b/pkgs/id/src/groups/policy.rs @@ -31,8 +31,9 @@ pub enum OnMemberRemoved { Dissolve, } -/// How a removal takes effect (design §6). Strong is the default. The rules belong to the voiding -/// track; this module only records the setting. +/// How a removal takes effect (design §6). Strong is the default. The voiding track reads this +/// setting: strong remove voids the events concurrent with a removal, weak remove stands them and +/// applies only the authority check (design §6.2 and §6.3). #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] pub enum Removal { /// Strong remove, the default. diff --git a/pkgs/id/src/groups/tests.rs b/pkgs/id/src/groups/tests.rs index fc8d517c6d..c445883e36 100644 --- a/pkgs/id/src/groups/tests.rs +++ b/pkgs/id/src/groups/tests.rs @@ -8,10 +8,10 @@ use std::collections::{BTreeMap, BTreeSet}; use p2panda_core::Hash; use proptest::prelude::*; -use super::policy::{Delegation, Join, OnMemberRemoved, Policy, RoleRules, Template}; +use super::policy::{Delegation, Join, OnMemberRemoved, Policy, Removal, RoleRules, Template}; use super::{ - Body, Create, Delegate, Event, Key, MemberOp, Outcome, Pair, Projection, Refusal, Role, fold, - friendship_id, + Body, Create, Delegate, DeviceOp, Event, Key, MemberOp, Outcome, Pair, Projection, Refusal, Role, + fold, friendship_id, }; use crate::rejection::Reason; @@ -797,6 +797,117 @@ fn test_a_group_without_a_delegation_setting_cascades() { ); } +fn with_removal(removal: Removal, mode: Option) -> Policy { + Policy { + removal, + delegation: mode, + ..Policy::default() + } +} + +fn device_add( + name: &str, + by: u8, + group: Hash, + device: u8, + scope: Role, + after_names: &[&str], +) -> Event { + event( + name, + by, + after_names, + Some(group), + Body::Device(DeviceOp::Add { + device: key(device), + scope, + }), + ) +} + +/// Design §6.4's week-late case, adapted to weak remove (§6.3): Joe (2) grants Write to Dan (3) +/// without having seen his own removal. `keep_prior` makes the grant a fact about the past, so the +/// removal's strength alone decides whether the concurrent grant stands. +fn late_grant_events(removal: Removal) -> Vec { + let g = id("g"); + vec![ + custom("g", 1, &[], with_removal(removal, Some(Delegation::KeepPrior))), + add("a0", 1, g, 2, Role::Write, &["g"]), + add("a1", 1, g, 3, Role::Write, &["a0"]), + delegate("d0", 1, g, 2, Role::Write, true, &["a1"]), + delegate("d1", 2, g, 3, Role::Write, true, &["d0"]), + remove("r0", 1, g, 2, &["a0"]), + ] +} + +#[test] +fn test_a_concurrent_grant_stands_under_weak_remove() { + let weak = project(&late_grant_events(Removal::Weak)); + assert_eq!(outcome(&weak, "d1"), Outcome::Applied); + assert!( + weak.grant_in_force(&id("d1")), + "weak remove stands a grant concurrent with it" + ); + assert!(role_of(&weak, id("g"), 2).is_none(), "2 is removed"); +} + +#[test] +fn test_a_concurrent_grant_is_void_under_strong_remove() { + let strong = project(&late_grant_events(Removal::Strong)); + assert!( + !strong.grant_in_force(&id("d1")), + "strong remove voids a grant concurrent with it, whether the fold refuses it at the head or \ + applies it and voids the link" + ); +} + +#[test] +fn test_weak_remove_still_invalidates_events_after_the_removal() { + let g = id("g"); + let mut events = late_grant_events(Removal::Weak); + events.push(delegate("d2", 2, g, 4, Role::Read, false, &["r0"])); + let weak = project(&events); + assert_eq!(outcome(&weak, "r0"), Outcome::Applied); + assert_eq!( + outcome(&weak, "d2"), + Outcome::Refused(Refusal::NotActive), + "an event by the removed member in the removal's causal future is invalid" + ); +} + +/// Joe (2) authorizes device 50 without having seen his own removal, and is then re-added. The +/// device grant is concurrent with the removal, so only the removal's strength decides it. +fn late_device_events(removal: Removal) -> Vec { + let g = id("g"); + vec![ + custom("g", 1, &[], with_removal(removal, None)), + add("a0", 1, g, 2, Role::Write, &["g"]), + device_add("dv0", 2, g, 50, Role::Write, &["a0"]), + remove("r0", 1, g, 2, &["a0"]), + add("a1", 1, g, 2, Role::Write, &["r0"]), + ] +} + +#[test] +fn test_a_concurrent_device_grant_stands_under_weak_remove() { + let g = id("g"); + let weak = project(&late_device_events(Removal::Weak)); + assert!( + weak.active_devices(&g, &key(2)).contains(&key(50)), + "weak remove stands a device grant concurrent with it" + ); +} + +#[test] +fn test_a_concurrent_device_grant_is_void_under_strong_remove() { + let g = id("g"); + let strong = project(&late_device_events(Removal::Strong)); + assert!( + strong.active_devices(&g, &key(2)).is_empty(), + "strong remove voids a device grant concurrent with it" + ); +} + #[test] fn test_keep_prior_lets_a_grant_made_before_the_removal_stand() { let g = id("g"); diff --git a/pkgs/id/src/membership/layer.rs b/pkgs/id/src/membership/layer.rs index 93413372ec..e681303930 100644 --- a/pkgs/id/src/membership/layer.rs +++ b/pkgs/id/src/membership/layer.rs @@ -363,6 +363,24 @@ impl Membership { Ok(self.access_at(&deps, AuthId::Group(group), AuthId::Key(member))) } + /// The same as [`Membership::access_at_position`], borrowing the layer immutably. A caller + /// that holds the state can ask about a position this way; it copies the frontier index rather + /// than memoising into it, so prefer the mutable method on a hot path. + /// + /// # Errors + /// + /// Returns [`MembershipError`] when an `after` hash is not indexed. + pub fn access_at_position_shared( + &self, + after: &[Hash], + group: Hash, + member: [u8; 32], + ) -> Result>, MembershipError> { + let mut index = self.index.clone(); + let deps: HashSet = index.frontier(after, group)?.into_iter().collect(); + Ok(self.access_at(&deps, AuthId::Group(group), AuthId::Key(member))) + } + /// The accounts that hold manage access in `group` at the causal position that `after` names. /// Empty when the group does not exist there. Delegation takes these as its roots (design /// §7.2). From 2da794e37a0088d585cf3954e08bf2b9775d796e Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Thu, 29 Oct 2026 08:44:42 -0500 Subject: [PATCH 2/4] =?UTF-8?q?groups:=20weak=20removal=20=E2=80=94=20caus?= =?UTF-8?q?al=20device=20resolution=20and=20Admin=20status=20(#161)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Codex review on PR 2204: authorized_device_account gated on head membership, so a device-authored event concurrent with its account's removal was refused NotActive; and device() read Admin status from the current record, so an Admin-scoped device grant concurrent with the Admin's removal was refused Attenuated. Add listed_for/admin_for position-aware helpers (admin replayed from settled events in the causal past in canonical order), use them in authorized_device_account, effective_author, and device(). Two regression tests cover both findings. Also rustfmt the PR files (tests.rs diffs in the fmt checks); meta_client.rs fmt diff and the clippy-lint cache failure are pre-existing/infra and untouched. --- pkgs/id/src/groups/fold.rs | 84 +++++++++++++++++++++++++++++++++++-- pkgs/id/src/groups/tests.rs | 61 +++++++++++++++++++++++++-- 2 files changed, 138 insertions(+), 7 deletions(-) diff --git a/pkgs/id/src/groups/fold.rs b/pkgs/id/src/groups/fold.rs index fdaae7776d..267ee334ed 100644 --- a/pkgs/id/src/groups/fold.rs +++ b/pkgs/id/src/groups/fold.rs @@ -1035,6 +1035,82 @@ impl Engine { .or_else(|| self.access(group, key)) } + /// Whether `key` is listed in `group` for an event at the causal position `after`. Strong + /// remove reads the current membership; weak remove reads the position, so a device grant + /// whose account is removed concurrently still resolves (design §6.3). + fn listed_for(&self, group: &Hash, key: &Key, after: &[Hash]) -> bool { + if self.strong_removal(*group) { + return self.listed(group, key); + } + self.membership + .access_at_position_shared(after, *group, *key) + .ok() + .flatten() + .is_some() + } + + /// Whether `key` is an Admin of `group` for an event at the causal position `after`. Strong + /// remove reads the current record; weak remove also honours the Admin status the key held + /// at the position, replayed from the settled events in the event's causal past in canonical + /// order — a removal concurrent with the event has already struck the key from the current + /// record's admins, and the membership layer cannot recover the status because it records + /// Admin as Manage (design §6.3). + fn admin_for( + &self, + by_id: &BTreeMap, + group: &Hash, + key: &Key, + after: &[Hash], + ) -> bool { + let record = self.groups.get(group); + if self.strong_removal(*group) { + return record.is_some_and(|record| record.admins.contains(key)); + } + if record.is_some_and(|record| record.admins.contains(key)) { + return true; + } + let past = causal_past(by_id, after); + let Ok(order) = canonical_order(by_id) else { + return false; + }; + let mut admin: Option = None; + for event in order { + if !past.contains(&event.id) || !self.settled.contains(&event.id) { + continue; + } + match &event.body { + Body::Create(create) if event.id == *group => { + admin = Some(if create.admins_on_create { + event.author == *key + || create + .members + .first() + .is_some_and(|(member, _)| member == key) + } else { + event.author == *key + || create + .members + .iter() + .any(|(member, role)| member == key && *role == Role::Admin) + }); + } + Body::Member(MemberOp::Add { member, role }) + | Body::Member(MemberOp::SetRole { member, role }) + if member == key && event.group == Some(*group) => + { + admin = Some(*role == Role::Admin); + } + Body::Member(MemberOp::Remove { member, .. }) + if member == key && event.group == Some(*group) => + { + admin = Some(false); + } + _ => {} + } + } + admin.unwrap_or(false) + } + /// Resolves whether `actor` is an active authorized device for an account in `group` at `after`. /// Returns `Some((account, authorized_scope))` if so. pub(super) fn authorized_device_account( @@ -1075,7 +1151,7 @@ impl Engine { if account_removed { continue; } - if !self.listed(&group, &grant.account) { + if !self.listed_for(&group, &grant.account, after) { continue; } if self.void_device(by_id, grant) { @@ -1095,7 +1171,7 @@ impl Engine { ) -> Option<(Key, Role)> { let record = self.groups.get(&group)?; if self.is_active_for(&group, record, &event.author, &event.after) { - let role = if record.admins.contains(&event.author) { + let role = if self.admin_for(by_id, &group, &event.author, &event.after) { Role::Admin } else { self.access_for(&group, &event.author, &event.after) @@ -1108,7 +1184,7 @@ impl Engine { if !self.is_active_for(&group, record, &account, &event.after) { return None; } - let account_role = if record.admins.contains(&account) { + let account_role = if self.admin_for(by_id, &group, &account, &event.after) { Role::Admin } else { self.access_for(&group, &account, &event.after) @@ -1168,7 +1244,7 @@ impl Engine { if !self.is_active_for(&group, record, &event.author, &event.after) { return Ok(Err(Refusal::NotActive)); } - let is_admin = record.admins.contains(&event.author); + let is_admin = self.admin_for(by_id, &group, &event.author, &event.after); match op { DeviceOp::Add { device, scope } => { if device == event.author { diff --git a/pkgs/id/src/groups/tests.rs b/pkgs/id/src/groups/tests.rs index c445883e36..a0f4eb98d5 100644 --- a/pkgs/id/src/groups/tests.rs +++ b/pkgs/id/src/groups/tests.rs @@ -10,8 +10,8 @@ use proptest::prelude::*; use super::policy::{Delegation, Join, OnMemberRemoved, Policy, Removal, RoleRules, Template}; use super::{ - Body, Create, Delegate, DeviceOp, Event, Key, MemberOp, Outcome, Pair, Projection, Refusal, Role, - fold, friendship_id, + Body, Create, Delegate, DeviceOp, Event, Key, MemberOp, Outcome, Pair, Projection, Refusal, + Role, fold, friendship_id, }; use crate::rejection::Reason; @@ -831,7 +831,12 @@ fn device_add( fn late_grant_events(removal: Removal) -> Vec { let g = id("g"); vec![ - custom("g", 1, &[], with_removal(removal, Some(Delegation::KeepPrior))), + custom( + "g", + 1, + &[], + with_removal(removal, Some(Delegation::KeepPrior)), + ), add("a0", 1, g, 2, Role::Write, &["g"]), add("a1", 1, g, 3, Role::Write, &["a0"]), delegate("d0", 1, g, 2, Role::Write, true, &["a1"]), @@ -908,6 +913,56 @@ fn test_a_concurrent_device_grant_is_void_under_strong_remove() { ); } +/// A device-authored grant is judged through the device's account: Joe (2) authorizes device +/// 50, the device grants Write to Dan (3), and Joe's removal is concurrent with both. Under +/// weak remove the account's standing at the grant's position resolves the device, so the +/// grant stands (design 6.3); the device-resolution gate must not read head membership. +#[test] +fn test_a_device_authored_grant_stands_under_weak_remove() { + let g = id("g"); + let events = vec![ + custom( + "g", + 1, + &[], + with_removal(Removal::Weak, Some(Delegation::KeepPrior)), + ), + add("a0", 1, g, 2, Role::Write, &["g"]), + add("a1", 1, g, 3, Role::Write, &["a0"]), + delegate("d0", 1, g, 2, Role::Write, true, &["a1"]), + device_add("dv0", 2, g, 50, Role::Write, &["d0"]), + remove("r0", 1, g, 2, &["a0"]), + delegate("dv1", 50, g, 3, Role::Write, false, &["dv0", "d0"]), + ]; + let weak = project(&events); + assert_eq!(outcome(&weak, "dv1"), Outcome::Applied); + assert!( + weak.grant_in_force(&id("dv1")), + "weak remove stands a device-authored grant concurrent with the account's removal" + ); +} + +/// An Admin authorizes an Admin-scoped device without having seen their own removal. Under +/// weak remove the Admin status at the grant's position is what counts, so the device grant +/// stands (design 6.3); the current record has already struck the author from its admins. +#[test] +fn test_an_admin_scoped_device_grant_stands_under_weak_remove() { + let g = id("g"); + let events = vec![ + custom("g", 1, &[], with_removal(Removal::Weak, None)), + add("a0", 1, g, 2, Role::Admin, &["g"]), + device_add("dv0", 2, g, 50, Role::Admin, &["a0"]), + remove("r0", 1, g, 2, &["a0"]), + add("a1", 1, g, 2, Role::Write, &["r0"]), + ]; + let weak = project(&events); + assert_eq!(outcome(&weak, "dv0"), Outcome::Applied); + assert!( + weak.active_devices(&g, &key(2)).contains(&key(50)), + "weak remove stands an Admin-scoped device grant concurrent with the Admin's removal" + ); +} + #[test] fn test_keep_prior_lets_a_grant_made_before_the_removal_stand() { let g = id("g"); From 6859a37e774c6eee2e93096fe5b7c6ca22d1c350 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Thu, 29 Oct 2026 11:03:38 -0500 Subject: [PATCH 3/4] test(groups): weak-removal regression tests with the removal sorting first (#161) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The existing weak-removal device tests used event names whose hashes sort the device events before the removal (dv0/dv1 < r0), so canonical order judged them before the removal landed. These variants name the removal s0 — the smallest hash in the set — so the removal is applied first, and cover: a device-authored delegation standing, an Admin-scoped device grant standing, the same grant Applied without a re-add (the removed account still has no active devices at the head, per the documented cascade), and a device-authored grant with a wider removal past. All pass against the causal-position helpers (listed_for / admin_for / is_active_for) — they pin the behaviour the Codex review asked about on PR #2204. --- pkgs/id/src/groups/tests.rs | 91 +++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) diff --git a/pkgs/id/src/groups/tests.rs b/pkgs/id/src/groups/tests.rs index a0f4eb98d5..0b58ac7122 100644 --- a/pkgs/id/src/groups/tests.rs +++ b/pkgs/id/src/groups/tests.rs @@ -1686,3 +1686,94 @@ fn test_a_grant_by_a_member_who_was_re_added_stands_in_every_mode() { ); } } + +/// Removal-sorts-first variant of the device-authored grant case: the removal's ID +/// (s0) is smaller than the device grant's (dv1), so canonical order applies the +/// removal BEFORE the grant is judged. Under weak remove the grant still stands: +/// its authority is judged at its own causal position (design §6.3). +#[test] +fn test_a_device_authored_grant_stands_under_weak_remove_when_the_removal_sorts_first() { + let g = id("g"); + let events = vec![ + custom( + "g", + 1, + &[], + with_removal(Removal::Weak, Some(Delegation::KeepPrior)), + ), + add("a0", 1, g, 2, Role::Write, &["g"]), + add("a1", 1, g, 3, Role::Write, &["a0"]), + delegate("d0", 1, g, 2, Role::Write, true, &["a1"]), + device_add("dv0", 2, g, 50, Role::Write, &["d0"]), + remove("s0", 1, g, 2, &["a0"]), + delegate("dv1", 50, g, 3, Role::Write, false, &["dv0", "d0"]), + ]; + let weak = project(&events); + assert_eq!(outcome(&weak, "dv1"), Outcome::Applied); + assert!( + weak.grant_in_force(&id("dv1")), + "weak remove stands a device-authored grant even when the removal sorts first" + ); +} + +/// Removal-sorts-first variant of the Admin-scoped device grant case: the +/// removal (s0) is applied before the device grant (dv0) in canonical order. +/// Under weak remove the author's Admin status at the grant's causal position +/// is what counts, so the grant stands (design §6.3). +#[test] +fn test_an_admin_scoped_device_grant_stands_under_weak_remove_when_the_removal_sorts_first() { + let g = id("g"); + let events = vec![ + custom("g", 1, &[], with_removal(Removal::Weak, None)), + add("a0", 1, g, 2, Role::Admin, &["g"]), + device_add("dv0", 2, g, 50, Role::Admin, &["a0"]), + remove("s0", 1, g, 2, &["a0"]), + add("a1", 1, g, 2, Role::Write, &["s0"]), + ]; + let weak = project(&events); + assert_eq!(outcome(&weak, "dv0"), Outcome::Applied); + assert!( + weak.active_devices(&g, &key(2)).contains(&key(50)), + "weak remove stands an Admin-scoped device grant even when the removal sorts first" + ); +} + +/// The Admin-scoped device grant above without a re-add: the grant is judged +/// Applied at its causal position (design §6.3). The account still ends +/// removed, so it has no active devices at the head — that is the documented +/// head-view cascade, not a refusal of the grant. +#[test] +fn test_an_admin_scoped_device_grant_is_applied_under_weak_remove_without_a_readd() { + let g = id("g"); + let events = vec![ + custom("g", 1, &[], with_removal(Removal::Weak, None)), + add("a0", 1, g, 2, Role::Admin, &["g"]), + device_add("dv0", 2, g, 50, Role::Admin, &["a0"]), + remove("s0", 1, g, 2, &["a0"]), + ]; + let weak = project(&events); + assert_eq!(outcome(&weak, "dv0"), Outcome::Applied); + assert!( + weak.active_devices(&g, &key(2)).is_empty(), + "a removed account has no active devices at the head" + ); +} + +/// The device-authored delegation case with the removal authored after both +/// member adds (a wider causal past, still concurrent with the delegation). +#[test] +fn test_a_device_authored_grant_stands_under_weak_remove_with_a_wide_removal_past() { + let g = id("g"); + let events = vec![ + custom("g", 1, &[], with_removal(Removal::Weak, Some(Delegation::KeepPrior))), + add("a0", 1, g, 2, Role::Write, &["g"]), + add("a1", 1, g, 3, Role::Write, &["a0"]), + delegate("d0", 1, g, 2, Role::Write, true, &["a1"]), + device_add("dv0", 2, g, 50, Role::Write, &["d0"]), + remove("s0", 1, g, 2, &["a0", "a1"]), + delegate("dv1", 50, g, 3, Role::Write, false, &["dv0", "d0"]), + ]; + let weak = project(&events); + assert_eq!(outcome(&weak, "dv1"), Outcome::Applied); + assert!(weak.grant_in_force(&id("dv1"))); +} From baf1f0f7c33c5779bc26e17641c88e51ccdaf9a6 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Thu, 29 Oct 2026 14:45:22 -0500 Subject: [PATCH 4/4] groups: seed causal Admin replay from group creation (#161) admin_for replayed Admin status only from settled events, but the group creation event is never a candidate and so never settled - its Body::Create branch was unreachable, and an Admin assigned at creation whose removal sorted first lost Admin status at the causal position, refusing a concurrent Admin-scoped device grant as Attenuated. Exempt the creation event from the settled filter in the replay. Regression: test_an_admin_scoped_device_grant_stands_under_weak_remove_for_a_creation_admin (failed before: Refused(Attenuated); passes after). Codex P2 finding on PR 2204, queue task 2941. --- pkgs/id/src/groups/fold.rs | 9 ++++++++- pkgs/id/src/groups/tests.rs | 27 +++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/pkgs/id/src/groups/fold.rs b/pkgs/id/src/groups/fold.rs index 267ee334ed..1212abbdfe 100644 --- a/pkgs/id/src/groups/fold.rs +++ b/pkgs/id/src/groups/fold.rs @@ -1075,7 +1075,14 @@ impl Engine { }; let mut admin: Option = None; for event in order { - if !past.contains(&event.id) || !self.settled.contains(&event.id) { + if !past.contains(&event.id) { + continue; + } + // The group's creation is never in the settled set (it is not a candidate + // event), but it seeds the replay: an Admin assigned at creation has no + // MemberOp::Add to recover the status from. + let is_creation = event.id == *group && matches!(event.body, Body::Create(_)); + if !is_creation && !self.settled.contains(&event.id) { continue; } match &event.body { diff --git a/pkgs/id/src/groups/tests.rs b/pkgs/id/src/groups/tests.rs index 0b58ac7122..3fdd604e86 100644 --- a/pkgs/id/src/groups/tests.rs +++ b/pkgs/id/src/groups/tests.rs @@ -1738,6 +1738,33 @@ fn test_an_admin_scoped_device_grant_stands_under_weak_remove_when_the_removal_s ); } +/// Creation-assigned Admin variant of the removal-sorts-first case: the Admin +/// was named in the group Create (no MemberOp::Add ever names them), so the +/// causal Admin replay in admin_for must seed from the creation event itself. +/// The removal (s0) sorts before the device grant (dv0); under weak remove the +/// grant stands (design 6.3). +#[test] +fn test_an_admin_scoped_device_grant_stands_under_weak_remove_for_a_creation_admin() { + let g = id("g"); + let events = vec![ + custom( + "g", + 1, + &[(2, Role::Admin)], + with_removal(Removal::Weak, None), + ), + device_add("dv0", 2, g, 50, Role::Admin, &["g"]), + remove("s0", 1, g, 2, &["g"]), + add("a1", 1, g, 2, Role::Write, &["s0"]), + ]; + let weak = project(&events); + assert_eq!(outcome(&weak, "dv0"), Outcome::Applied); + assert!( + weak.active_devices(&g, &key(2)).contains(&key(50)), + "weak remove stands an Admin-scoped device grant for an Admin assigned at creation" + ); +} + /// The Admin-scoped device grant above without a re-add: the grant is judged /// Applied at its causal position (design §6.3). The account still ends /// removed, so it has no active devices at the head — that is the documented