-
Notifications
You must be signed in to change notification settings - Fork 0
feat(relation): refuse a template copy as the source identity #143
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
7 commits
Select commit
Hold shift + click to select a range
a5fc722
feat(relation): refuse a template copy as the source identity
seonghobae 76c8772
Merge remote-tracking branch 'origin/main' into review/pr143-current
seonghobae 37d8697
fix(copy-identity): apply repository rustfmt
seonghobae fc9dec9
test: derive crate contract from workspace manifest
seonghobae 7d533c6
Merge current main into copy identity gate
seonghobae d1ae8c1
Merge remote-tracking branch 'origin/main' into agent/copy-identity
seonghobae 0355d1a
chore: regenerate Cargo.lock after main merge
seonghobae 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
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
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 = "copy_identity" | ||
| description = "A template copy is not the source document and not a state transition." | ||
| 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,53 @@ | ||
| //! Fail-closed copy-identity errors. | ||
|
|
||
| use std::fmt; | ||
|
|
||
| /// A fail-closed copy-identity error. | ||
| #[derive(Clone, Copy, Debug, Eq, PartialEq)] | ||
| #[non_exhaustive] | ||
| pub enum CopyIdentityError { | ||
| /// A template copy was treated as the source document identity. | ||
| CopyIsNotSourceIdentity, | ||
| /// A template copy was treated as a state transition. | ||
| CopyIsNotTransition, | ||
| /// A recovery slice was empty or length-mismatched. | ||
| InvalidCopyPayload, | ||
| } | ||
|
|
||
| impl fmt::Display for CopyIdentityError { | ||
| fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| let message = match self { | ||
| Self::CopyIsNotSourceIdentity => "a template copy is not the source document identity", | ||
| Self::CopyIsNotTransition => "a template copy is not a state transition", | ||
| Self::InvalidCopyPayload => "invalid copy-identity payload", | ||
| }; | ||
| formatter.write_str(message) | ||
| } | ||
| } | ||
|
|
||
| impl std::error::Error for CopyIdentityError {} | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::CopyIdentityError; | ||
|
|
||
| #[test] | ||
| fn error_messages_are_stable() { | ||
| for (error, message) in [ | ||
| ( | ||
| CopyIdentityError::CopyIsNotSourceIdentity, | ||
| "a template copy is not the source document identity", | ||
| ), | ||
| ( | ||
| CopyIdentityError::CopyIsNotTransition, | ||
| "a template copy is not a state transition", | ||
| ), | ||
| ( | ||
| CopyIdentityError::InvalidCopyPayload, | ||
| "invalid copy-identity 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,127 @@ | ||
| //! Template-copy identity versus the copied source document. | ||
|
|
||
| use crate::CopyIdentityError; | ||
|
|
||
| /// Closed vocabulary of copy-related document identities. | ||
| #[derive(Clone, Copy, Debug, Eq, PartialEq)] | ||
| pub enum CopyKind { | ||
| /// A template or pasted copy of an earlier source. | ||
| TemplateCopy, | ||
| /// The earlier source document being copied. | ||
| SourceDocument, | ||
| } | ||
|
|
||
| impl CopyKind { | ||
| /// Return the stable wire kind name. | ||
| #[must_use] | ||
| pub const fn wire_name(self) -> &'static str { | ||
| match self { | ||
| Self::TemplateCopy => "template_copy_of", | ||
| Self::SourceDocument => "source_document", | ||
| } | ||
| } | ||
|
|
||
| /// Parse a stable wire kind name. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Returns [`CopyIdentityError::InvalidCopyPayload`] for unrecognized names. | ||
| pub fn from_wire_name(name: &str) -> Result<Self, CopyIdentityError> { | ||
| match name { | ||
| "template_copy_of" => Ok(Self::TemplateCopy), | ||
| "source_document" => Ok(Self::SourceDocument), | ||
| _ => Err(CopyIdentityError::InvalidCopyPayload), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Refuse to treat a template copy as the source document identity. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Returns [`CopyIdentityError::CopyIsNotSourceIdentity`] when `kind` is | ||
| /// [`CopyKind::TemplateCopy`]. | ||
| pub fn refuse_copy_as_source_identity(kind: CopyKind) -> Result<(), CopyIdentityError> { | ||
| match kind { | ||
| CopyKind::TemplateCopy => Err(CopyIdentityError::CopyIsNotSourceIdentity), | ||
| CopyKind::SourceDocument => Ok(()), | ||
| } | ||
| } | ||
|
|
||
| /// Refuse to treat a template copy as a forward state transition. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Returns [`CopyIdentityError::CopyIsNotTransition`] when `kind` is | ||
| /// [`CopyKind::TemplateCopy`]. | ||
| pub fn refuse_copy_as_transition(kind: CopyKind) -> Result<(), CopyIdentityError> { | ||
| match kind { | ||
| CopyKind::TemplateCopy => Err(CopyIdentityError::CopyIsNotTransition), | ||
| CopyKind::SourceDocument => Ok(()), | ||
| } | ||
| } | ||
|
|
||
| /// Fraction of recovered copy kinds that match known truth. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Returns [`CopyIdentityError::InvalidCopyPayload`] when either slice is empty | ||
| /// or the lengths differ. | ||
| pub fn identity_recovery_rate( | ||
| truth: &[CopyKind], | ||
| decided: &[CopyKind], | ||
| ) -> Result<f64, CopyIdentityError> { | ||
| if truth.is_empty() || truth.len() != decided.len() { | ||
| return Err(CopyIdentityError::InvalidCopyPayload); | ||
| } | ||
| let mut matches = 0_u32; | ||
| for (truth_kind, decided_kind) in truth.iter().zip(decided) { | ||
| if truth_kind == decided_kind { | ||
| matches += 1; | ||
| } | ||
| } | ||
| Ok(f64::from(matches) / truth.len() as f64) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::{ | ||
| CopyKind, identity_recovery_rate, refuse_copy_as_source_identity, refuse_copy_as_transition, | ||
| }; | ||
| use crate::CopyIdentityError; | ||
|
|
||
| #[test] | ||
| fn local_branches_cover_kinds_payloads_and_wire_names() { | ||
| assert_eq!( | ||
| refuse_copy_as_source_identity(CopyKind::TemplateCopy), | ||
| Err(CopyIdentityError::CopyIsNotSourceIdentity) | ||
| ); | ||
| assert_eq!( | ||
| refuse_copy_as_transition(CopyKind::TemplateCopy), | ||
| Err(CopyIdentityError::CopyIsNotTransition) | ||
| ); | ||
| refuse_copy_as_source_identity(CopyKind::SourceDocument).expect("source"); | ||
| refuse_copy_as_transition(CopyKind::SourceDocument).expect("source"); | ||
| for kind in [CopyKind::TemplateCopy, CopyKind::SourceDocument] { | ||
| assert_eq!( | ||
| CopyKind::from_wire_name(kind.wire_name()).expect("round-trip"), | ||
| kind | ||
| ); | ||
| } | ||
| assert_eq!( | ||
| CopyKind::from_wire_name("summarizes"), | ||
| Err(CopyIdentityError::InvalidCopyPayload) | ||
| ); | ||
| let matched = identity_recovery_rate(&[CopyKind::TemplateCopy], &[CopyKind::TemplateCopy]) | ||
| .expect("rate"); | ||
| assert!((matched - 1.0).abs() < f64::EPSILON); | ||
| assert_eq!( | ||
| identity_recovery_rate(&[], &[]), | ||
| Err(CopyIdentityError::InvalidCopyPayload) | ||
| ); | ||
| assert_eq!( | ||
| identity_recovery_rate(&[CopyKind::TemplateCopy], &[]), | ||
| Err(CopyIdentityError::InvalidCopyPayload) | ||
| ); | ||
| } | ||
| } |
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,22 @@ | ||
| #![forbid(unsafe_code)] | ||
| #![deny(missing_docs)] | ||
| #![allow(clippy::cast_precision_loss)] | ||
| //! A template copy is not the source document and not a state transition. | ||
| //! | ||
| //! Copy variants keep a distinct identity for relation-aware splits. They | ||
| //! never become input-process-outcome edges and never reuse the source | ||
| //! identity (ADR 0003). | ||
|
|
||
| mod error; | ||
| mod kind; | ||
|
|
||
| /// Fail-closed copy-identity errors. | ||
| pub use error::CopyIdentityError; | ||
| /// Closed vocabulary of copy-related document identities. | ||
| pub use kind::CopyKind; | ||
| /// Fraction of recovered copy kinds that match known truth. | ||
| pub use kind::identity_recovery_rate; | ||
| /// Refuse to treat a template copy as the source document identity. | ||
| pub use kind::refuse_copy_as_source_identity; | ||
| /// Refuse to treat a template copy as a forward state transition. | ||
| pub use kind::refuse_copy_as_transition; |
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,67 @@ | ||
| //! A template copy is not the source document and not a state transition. | ||
|
|
||
| use copy_identity::{ | ||
| CopyIdentityError, CopyKind, identity_recovery_rate, refuse_copy_as_source_identity, | ||
| refuse_copy_as_transition, | ||
| }; | ||
|
|
||
| #[test] | ||
| fn a_copy_cannot_become_the_source_identity_or_a_transition() { | ||
| assert_eq!( | ||
| refuse_copy_as_source_identity(CopyKind::TemplateCopy), | ||
| Err(CopyIdentityError::CopyIsNotSourceIdentity) | ||
| ); | ||
| assert_eq!( | ||
| refuse_copy_as_transition(CopyKind::TemplateCopy), | ||
| Err(CopyIdentityError::CopyIsNotTransition) | ||
| ); | ||
| refuse_copy_as_source_identity(CopyKind::SourceDocument).expect("source"); | ||
| refuse_copy_as_transition(CopyKind::SourceDocument).expect("source"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn recovered_kinds_match_known_truth_better_than_a_source_collapse() { | ||
| let truth = [ | ||
| CopyKind::TemplateCopy, | ||
| CopyKind::SourceDocument, | ||
| CopyKind::TemplateCopy, | ||
| ]; | ||
| let recovered = truth; | ||
| let collapsed = [ | ||
| CopyKind::SourceDocument, | ||
| CopyKind::SourceDocument, | ||
| CopyKind::SourceDocument, | ||
| ]; | ||
| let recovered_rate = identity_recovery_rate(&truth, &recovered).expect("recovered"); | ||
| let collapsed_rate = identity_recovery_rate(&truth, &collapsed).expect("collapsed"); | ||
| let expected = { | ||
| let mut matches = 0_u32; | ||
| for (truth_kind, decided_kind) in truth.iter().zip(recovered.iter()) { | ||
| if truth_kind == decided_kind { | ||
| matches += 1; | ||
| } | ||
| } | ||
| f64::from(matches) / f64::from(u32::try_from(truth.len()).expect("len")) | ||
| }; | ||
| assert!((recovered_rate - expected).abs() < f64::EPSILON); | ||
| assert!(recovered_rate > collapsed_rate); | ||
| } | ||
|
|
||
| #[test] | ||
| fn empty_or_mismatched_kind_payloads_fail_closed() { | ||
| assert_eq!( | ||
| identity_recovery_rate(&[], &[]), | ||
| Err(CopyIdentityError::InvalidCopyPayload) | ||
| ); | ||
| assert_eq!( | ||
| identity_recovery_rate(&[CopyKind::TemplateCopy], &[]), | ||
| Err(CopyIdentityError::InvalidCopyPayload) | ||
| ); | ||
| assert_eq!( | ||
| identity_recovery_rate( | ||
| &[CopyKind::TemplateCopy, CopyKind::SourceDocument], | ||
| &[CopyKind::TemplateCopy] | ||
| ), | ||
| Err(CopyIdentityError::InvalidCopyPayload) | ||
| ); | ||
| } |
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 `copy_identity` package identity. | ||
|
|
||
| #[test] | ||
| fn package_identity_is_stable() { | ||
| let observed = std::hint::black_box(env!("CARGO_PKG_NAME")); | ||
| assert_eq!(observed, "copy_identity"); | ||
| } |
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
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
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.
🔍 Unrelated CHANGELOG entry for persistence_postgres migration 0007
The CHANGELOG diff adds two entries: the
copy_identitygate (expected) and apersistence_postgresretention/deletion/legal-hold (migration0007) entry at CHANGELOG.md. The latter is unrelated to this PR's stated scope (copy-versus-source identity) and the author description explicitly disclaims allocating migrations.retention_sql.rsdoes exist in the tree, so this may be a pre-existing/merge-carried entry rather than newly introduced work, but its appearance in this PR's diff is worth confirming to avoid over-claiming capabilities not delivered by this change.Was this helpful? React with 👍 or 👎 to provide feedback.