Repository navigation
feat(membership): refuse customer-competitor overlap #129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| [package] | ||
| name = "role_contradiction" | ||
| description = "Customer and competitor cannot occupy the same group at once." | ||
| version.workspace = true | ||
| edition.workspace = true | ||
| rust-version.workspace = true | ||
| license.workspace = true | ||
| authors.workspace = true | ||
| repository.workspace = true | ||
| homepage.workspace = true | ||
| readme.workspace = true | ||
| keywords.workspace = true | ||
| categories.workspace = true | ||
| publish = false | ||
|
|
||
| [lints] | ||
| workspace = true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| //! Fail-closed role-contradiction errors. | ||
|
|
||
| use std::fmt; | ||
|
|
||
| /// A fail-closed role-contradiction error. | ||
| #[derive(Clone, Copy, Debug, Eq, PartialEq)] | ||
| #[non_exhaustive] | ||
| pub enum RoleContradictionError { | ||
| /// Customer and competitor were assigned in the same group. | ||
| CustomerCompetitorOverlap, | ||
| /// A contextual role was treated as a permanent entity class. | ||
| RoleIsNotEntityClass, | ||
| /// A recovery slice was empty or length-mismatched. | ||
| InvalidRolePayload, | ||
| } | ||
|
|
||
| impl fmt::Display for RoleContradictionError { | ||
| fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| let message = match self { | ||
| Self::CustomerCompetitorOverlap => { | ||
| "customer and competitor cannot occupy the same group" | ||
| } | ||
| Self::RoleIsNotEntityClass => "contextual role is not an entity class", | ||
| Self::InvalidRolePayload => "invalid role-contradiction payload", | ||
| }; | ||
| formatter.write_str(message) | ||
| } | ||
| } | ||
|
|
||
| impl std::error::Error for RoleContradictionError {} | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::RoleContradictionError; | ||
|
|
||
| #[test] | ||
| fn error_messages_are_stable() { | ||
| for (error, message) in [ | ||
| ( | ||
| RoleContradictionError::CustomerCompetitorOverlap, | ||
| "customer and competitor cannot occupy the same group", | ||
| ), | ||
| ( | ||
| RoleContradictionError::RoleIsNotEntityClass, | ||
| "contextual role is not an entity class", | ||
| ), | ||
| ( | ||
| RoleContradictionError::InvalidRolePayload, | ||
| "invalid role-contradiction payload", | ||
| ), | ||
| ] { | ||
| assert_eq!(error.to_string(), message); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| #![forbid(unsafe_code)] | ||
| #![deny(missing_docs)] | ||
| #![allow(clippy::cast_precision_loss)] | ||
| //! Customer and competitor cannot occupy the same group at once. | ||
| //! | ||
| //! Customer, partner, and competitor are time-varying roles, not permanent | ||
| //! entity classes. A customer/competitor pair in one group fails closed | ||
| //! (ADR 0003). | ||
|
|
||
| mod error; | ||
| mod role; | ||
|
|
||
| /// Fail-closed role-contradiction errors. | ||
| pub use error::RoleContradictionError; | ||
| /// Fraction of recovered contextual roles that match known truth. | ||
| pub use role::identity_recovery_rate; | ||
| /// Refuse a contradictory customer/competitor pair in one group. | ||
| pub use role::refuse_contradictory_roles; | ||
| /// Refuse to treat a contextual role as a permanent entity class. | ||
| pub use role::refuse_role_as_entity_class; | ||
| /// Return whether two roles contradict in the same group. | ||
| pub use role::roles_contradict; | ||
| /// Closed vocabulary of commercial roles that can change over time. | ||
| pub use role::ContextualRole; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,177 @@ | ||
| //! Contextual commercial roles that are not permanent entity classes. | ||
|
|
||
| use crate::RoleContradictionError; | ||
|
|
||
| /// Closed vocabulary of commercial roles that can change over time. | ||
| #[derive(Clone, Copy, Debug, Eq, PartialEq)] | ||
| pub enum ContextualRole { | ||
| /// Customer role in one commercial context. | ||
| Customer, | ||
| /// Partner role in one commercial context. | ||
| Partner, | ||
| /// Competitor role in one commercial context. | ||
| Competitor, | ||
| } | ||
|
|
||
| impl ContextualRole { | ||
| /// Return the stable wire role name. | ||
| #[must_use] | ||
| pub const fn wire_name(self) -> &'static str { | ||
| match self { | ||
| Self::Customer => "customer", | ||
| Self::Partner => "partner", | ||
| Self::Competitor => "competitor", | ||
| } | ||
| } | ||
|
|
||
| /// Parse a stable wire role name. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Returns [`RoleContradictionError::InvalidRolePayload`] for unrecognized | ||
| /// names. | ||
| pub fn from_wire_name(name: &str) -> Result<Self, RoleContradictionError> { | ||
| match name { | ||
| "customer" => Ok(Self::Customer), | ||
| "partner" => Ok(Self::Partner), | ||
| "competitor" => Ok(Self::Competitor), | ||
| _ => Err(RoleContradictionError::InvalidRolePayload), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Return whether two roles contradict in the same group. | ||
| /// | ||
| /// Customer and competitor cannot occupy the same group. Partner may coexist | ||
| /// with either role (coopetition or a customer-partner) without rewriting | ||
| /// entity identity. | ||
| #[must_use] | ||
| pub const fn roles_contradict(left: ContextualRole, right: ContextualRole) -> bool { | ||
| matches!( | ||
| (left, right), | ||
| (ContextualRole::Customer, ContextualRole::Competitor) | ||
| | (ContextualRole::Competitor, ContextualRole::Customer) | ||
| ) | ||
| } | ||
|
|
||
| /// Refuse a contradictory customer/competitor pair in one group. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Returns [`RoleContradictionError::CustomerCompetitorOverlap`] when the pair | ||
| /// is customer and competitor. Compatible pairs succeed. | ||
| pub fn refuse_contradictory_roles( | ||
| left: ContextualRole, | ||
| right: ContextualRole, | ||
| ) -> Result<(), RoleContradictionError> { | ||
| if roles_contradict(left, right) { | ||
| return Err(RoleContradictionError::CustomerCompetitorOverlap); | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
||
| /// Refuse to treat a contextual role as a permanent entity class. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Always returns [`RoleContradictionError::RoleIsNotEntityClass`]. | ||
| pub fn refuse_role_as_entity_class(_role: ContextualRole) -> Result<(), RoleContradictionError> { | ||
| Err(RoleContradictionError::RoleIsNotEntityClass) | ||
| } | ||
|
|
||
| /// Fraction of recovered contextual roles that match known truth. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Returns [`RoleContradictionError::InvalidRolePayload`] when either slice is | ||
| /// empty or the lengths differ. | ||
| pub fn identity_recovery_rate( | ||
| truth: &[ContextualRole], | ||
| decided: &[ContextualRole], | ||
| ) -> Result<f64, RoleContradictionError> { | ||
| if truth.is_empty() || truth.len() != decided.len() { | ||
| return Err(RoleContradictionError::InvalidRolePayload); | ||
| } | ||
| let mut matches = 0_u32; | ||
| for (truth_role, decided_role) in truth.iter().zip(decided) { | ||
| if truth_role == decided_role { | ||
| matches += 1; | ||
| } | ||
| } | ||
| Ok(f64::from(matches) / truth.len() as f64) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::{ | ||
| identity_recovery_rate, refuse_contradictory_roles, refuse_role_as_entity_class, | ||
| roles_contradict, ContextualRole, | ||
| }; | ||
| use crate::RoleContradictionError; | ||
|
|
||
| #[test] | ||
| fn local_branches_cover_pairs_payloads_and_wire_names() { | ||
| assert!(roles_contradict( | ||
| ContextualRole::Customer, | ||
| ContextualRole::Competitor | ||
| )); | ||
| assert!(roles_contradict( | ||
| ContextualRole::Competitor, | ||
| ContextualRole::Customer | ||
| )); | ||
| assert!(!roles_contradict( | ||
| ContextualRole::Customer, | ||
| ContextualRole::Partner | ||
| )); | ||
| assert!(!roles_contradict( | ||
| ContextualRole::Partner, | ||
| ContextualRole::Competitor | ||
| )); | ||
| assert!(!roles_contradict( | ||
| ContextualRole::Customer, | ||
| ContextualRole::Customer | ||
| )); | ||
| assert_eq!( | ||
| refuse_contradictory_roles(ContextualRole::Customer, ContextualRole::Competitor), | ||
| Err(RoleContradictionError::CustomerCompetitorOverlap) | ||
| ); | ||
| assert_eq!( | ||
| refuse_contradictory_roles(ContextualRole::Competitor, ContextualRole::Customer), | ||
| Err(RoleContradictionError::CustomerCompetitorOverlap) | ||
| ); | ||
| refuse_contradictory_roles(ContextualRole::Customer, ContextualRole::Partner) | ||
| .expect("compatible"); | ||
| refuse_contradictory_roles(ContextualRole::Partner, ContextualRole::Competitor) | ||
| .expect("coopetition"); | ||
| for role in [ | ||
| ContextualRole::Customer, | ||
| ContextualRole::Partner, | ||
| ContextualRole::Competitor, | ||
| ] { | ||
| assert_eq!( | ||
| ContextualRole::from_wire_name(role.wire_name()).expect("round-trip"), | ||
| role | ||
| ); | ||
| assert_eq!( | ||
| refuse_role_as_entity_class(role), | ||
| Err(RoleContradictionError::RoleIsNotEntityClass) | ||
| ); | ||
| } | ||
| assert_eq!( | ||
| ContextualRole::from_wire_name("organization"), | ||
| Err(RoleContradictionError::InvalidRolePayload) | ||
| ); | ||
| let matched = | ||
| identity_recovery_rate(&[ContextualRole::Customer], &[ContextualRole::Customer]) | ||
| .expect("rate"); | ||
| assert!((matched - 1.0).abs() < f64::EPSILON); | ||
| assert_eq!( | ||
| identity_recovery_rate(&[], &[]), | ||
| Err(RoleContradictionError::InvalidRolePayload) | ||
| ); | ||
| assert_eq!( | ||
| identity_recovery_rate(&[ContextualRole::Customer], &[]), | ||
| Err(RoleContradictionError::InvalidRolePayload) | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| //! Integration contract for the `role_contradiction` package identity. | ||
|
|
||
| #[test] | ||
| fn package_identity_is_stable() { | ||
| let observed = std::hint::black_box(env!("CARGO_PKG_NAME")); | ||
| assert_eq!(observed, "role_contradiction"); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟠 Major | 🏗️ Heavy lift
역할 할당의 그룹 및 시간 컨텍스트를 모델에 포함하십시오. 현재 public API와 테스트는 role label만 사용합니다. 따라서 “same group at once” 제약을 정확히 적용하거나 검증할 수 없습니다.
crates/role_contradiction/src/role.rs#L63-L101: 그룹, 엔티티, 유효 기간을 포함하는 역할 할당을 받아 동일 그룹의 기간 중첩만 거부하고 recovery를 식별자로 정렬하십시오.crates/role_contradiction/tests/role_contradiction_contract.rs#L59-L103: 그룹 분리, 기간 비중첩, 시간 순서, 부분 recovery를 포함하는 synthetic truth 계약 테스트를 추가하십시오.📍 Affects 2 files
crates/role_contradiction/src/role.rs#L63-L101(this comment)crates/role_contradiction/tests/role_contradiction_contract.rs#L59-L103🤖 Prompt for AI Agents
Source: Coding guidelines