-
Notifications
You must be signed in to change notification settings - Fork 0
feat(relation): refuse a summary as the source identity #139
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
bf035fc
feat(relation): refuse a summary as the source identity
seonghobae 1d6fa0c
Merge remote-tracking branch 'origin/main' into review/pr139-current
seonghobae bb277d9
style: apply workspace rustfmt
seonghobae 7108baa
test: derive crate contract from workspace manifest
seonghobae 43ebbda
Merge current main into summary edge gate
seonghobae 188b913
Merge remote-tracking branch 'origin/main' into agent/summarizes-edge
seonghobae 5893d0e
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 = "summarizes_edge" | ||
| description = "A summary is not a state transition and not the source document." | ||
| 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 summarizes-edge errors. | ||
|
|
||
| use std::fmt; | ||
|
|
||
| /// A fail-closed summarizes-edge error. | ||
| #[derive(Clone, Copy, Debug, Eq, PartialEq)] | ||
| #[non_exhaustive] | ||
| pub enum SummarizesEdgeError { | ||
| /// A summary was treated as a state transition. | ||
| SummaryIsNotTransition, | ||
| /// A summary was treated as the source document identity. | ||
| SummaryIsNotSourceIdentity, | ||
| /// A recovery slice was empty or length-mismatched. | ||
| InvalidEdgePayload, | ||
| } | ||
|
|
||
| impl fmt::Display for SummarizesEdgeError { | ||
| fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| let message = match self { | ||
| Self::SummaryIsNotTransition => "a summary is not a state transition", | ||
| Self::SummaryIsNotSourceIdentity => "a summary is not the source document identity", | ||
| Self::InvalidEdgePayload => "invalid summarizes-edge payload", | ||
| }; | ||
| formatter.write_str(message) | ||
| } | ||
| } | ||
|
|
||
| impl std::error::Error for SummarizesEdgeError {} | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::SummarizesEdgeError; | ||
|
|
||
| #[test] | ||
| fn error_messages_are_stable() { | ||
| for (error, message) in [ | ||
| ( | ||
| SummarizesEdgeError::SummaryIsNotTransition, | ||
| "a summary is not a state transition", | ||
| ), | ||
| ( | ||
| SummarizesEdgeError::SummaryIsNotSourceIdentity, | ||
| "a summary is not the source document identity", | ||
| ), | ||
| ( | ||
| SummarizesEdgeError::InvalidEdgePayload, | ||
| "invalid summarizes-edge 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,130 @@ | ||
| //! Summary provenance versus the summarized source document. | ||
|
|
||
| use crate::SummarizesEdgeError; | ||
|
|
||
| /// Closed vocabulary of summary-related document identities. | ||
| #[derive(Clone, Copy, Debug, Eq, PartialEq)] | ||
| pub enum SummarizesKind { | ||
| /// A summary of an earlier source (provenance; may point backward). | ||
| Summary, | ||
| /// The earlier source document being summarized. | ||
| SourceDocument, | ||
| } | ||
|
|
||
| impl SummarizesKind { | ||
| /// Return the stable wire kind name. | ||
| #[must_use] | ||
| pub const fn wire_name(self) -> &'static str { | ||
| match self { | ||
| Self::Summary => "summarizes", | ||
| Self::SourceDocument => "source_document", | ||
| } | ||
| } | ||
|
|
||
| /// Parse a stable wire kind name. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Returns [`SummarizesEdgeError::InvalidEdgePayload`] for unrecognized | ||
| /// names. | ||
| pub fn from_wire_name(name: &str) -> Result<Self, SummarizesEdgeError> { | ||
| match name { | ||
| "summarizes" => Ok(Self::Summary), | ||
| "source_document" => Ok(Self::SourceDocument), | ||
| _ => Err(SummarizesEdgeError::InvalidEdgePayload), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Refuse to treat a summary as a forward state transition. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Returns [`SummarizesEdgeError::SummaryIsNotTransition`] when `kind` is | ||
| /// [`SummarizesKind::Summary`]. | ||
| pub fn refuse_summary_as_transition(kind: SummarizesKind) -> Result<(), SummarizesEdgeError> { | ||
| match kind { | ||
| SummarizesKind::Summary => Err(SummarizesEdgeError::SummaryIsNotTransition), | ||
| SummarizesKind::SourceDocument => Ok(()), | ||
| } | ||
| } | ||
|
|
||
| /// Refuse to treat a summary as the source document identity. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Returns [`SummarizesEdgeError::SummaryIsNotSourceIdentity`] when `kind` is | ||
| /// [`SummarizesKind::Summary`]. | ||
| pub fn refuse_summary_as_source_identity(kind: SummarizesKind) -> Result<(), SummarizesEdgeError> { | ||
| match kind { | ||
| SummarizesKind::Summary => Err(SummarizesEdgeError::SummaryIsNotSourceIdentity), | ||
| SummarizesKind::SourceDocument => Ok(()), | ||
| } | ||
| } | ||
|
|
||
| /// Fraction of recovered summary kinds that match known truth. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Returns [`SummarizesEdgeError::InvalidEdgePayload`] when either slice is | ||
| /// empty or the lengths differ. | ||
| pub fn identity_recovery_rate( | ||
| truth: &[SummarizesKind], | ||
| decided: &[SummarizesKind], | ||
| ) -> Result<f64, SummarizesEdgeError> { | ||
| if truth.is_empty() || truth.len() != decided.len() { | ||
| return Err(SummarizesEdgeError::InvalidEdgePayload); | ||
| } | ||
| 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::{ | ||
| SummarizesKind, identity_recovery_rate, refuse_summary_as_source_identity, | ||
| refuse_summary_as_transition, | ||
| }; | ||
| use crate::SummarizesEdgeError; | ||
|
|
||
| #[test] | ||
| fn local_branches_cover_kinds_payloads_and_wire_names() { | ||
| assert_eq!( | ||
| refuse_summary_as_transition(SummarizesKind::Summary), | ||
| Err(SummarizesEdgeError::SummaryIsNotTransition) | ||
| ); | ||
| assert_eq!( | ||
| refuse_summary_as_source_identity(SummarizesKind::Summary), | ||
| Err(SummarizesEdgeError::SummaryIsNotSourceIdentity) | ||
| ); | ||
| refuse_summary_as_transition(SummarizesKind::SourceDocument).expect("source"); | ||
| refuse_summary_as_source_identity(SummarizesKind::SourceDocument).expect("source"); | ||
| for kind in [SummarizesKind::Summary, SummarizesKind::SourceDocument] { | ||
| assert_eq!( | ||
| SummarizesKind::from_wire_name(kind.wire_name()).expect("round-trip"), | ||
| kind | ||
| ); | ||
| } | ||
| assert_eq!( | ||
| SummarizesKind::from_wire_name("references"), | ||
| Err(SummarizesEdgeError::InvalidEdgePayload) | ||
| ); | ||
| let matched = | ||
| identity_recovery_rate(&[SummarizesKind::Summary], &[SummarizesKind::Summary]) | ||
| .expect("rate"); | ||
| assert!((matched - 1.0).abs() < f64::EPSILON); | ||
| assert_eq!( | ||
| identity_recovery_rate(&[], &[]), | ||
| Err(SummarizesEdgeError::InvalidEdgePayload) | ||
| ); | ||
| assert_eq!( | ||
| identity_recovery_rate(&[SummarizesKind::Summary], &[]), | ||
| Err(SummarizesEdgeError::InvalidEdgePayload) | ||
| ); | ||
| } | ||
| } | ||
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 summary is not a state transition and not the source document. | ||
| //! | ||
| //! Summary provenance may point to earlier event time. It never becomes an | ||
| //! input-process-outcome edge and never reuses the source identity | ||
| //! (ADR 0003). | ||
|
|
||
| mod error; | ||
| mod kind; | ||
|
|
||
| /// Fail-closed summarizes-edge errors. | ||
| pub use error::SummarizesEdgeError; | ||
| /// Closed vocabulary of summary-related document identities. | ||
| pub use kind::SummarizesKind; | ||
| /// Fraction of recovered summary kinds that match known truth. | ||
| pub use kind::identity_recovery_rate; | ||
| /// Refuse to treat a summary as the source document identity. | ||
| pub use kind::refuse_summary_as_source_identity; | ||
| /// Refuse to treat a summary as a forward state transition. | ||
| pub use kind::refuse_summary_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,7 @@ | ||
| //! Integration contract for the `summarizes_edge` package identity. | ||
|
|
||
| #[test] | ||
| fn package_identity_is_stable() { | ||
| let observed = std::hint::black_box(env!("CARGO_PKG_NAME")); | ||
| assert_eq!(observed, "summarizes_edge"); | ||
| } |
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 summary is not a state transition and not the source document. | ||
|
|
||
| use summarizes_edge::{ | ||
| SummarizesEdgeError, SummarizesKind, identity_recovery_rate, refuse_summary_as_source_identity, | ||
| refuse_summary_as_transition, | ||
| }; | ||
|
|
||
| #[test] | ||
| fn a_summary_cannot_become_a_transition_or_the_source_identity() { | ||
| assert_eq!( | ||
| refuse_summary_as_transition(SummarizesKind::Summary), | ||
| Err(SummarizesEdgeError::SummaryIsNotTransition) | ||
| ); | ||
| assert_eq!( | ||
| refuse_summary_as_source_identity(SummarizesKind::Summary), | ||
| Err(SummarizesEdgeError::SummaryIsNotSourceIdentity) | ||
| ); | ||
| refuse_summary_as_transition(SummarizesKind::SourceDocument).expect("source"); | ||
| refuse_summary_as_source_identity(SummarizesKind::SourceDocument).expect("source"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn recovered_kinds_match_known_truth_better_than_a_source_collapse() { | ||
| let truth = [ | ||
| SummarizesKind::Summary, | ||
| SummarizesKind::SourceDocument, | ||
| SummarizesKind::Summary, | ||
| ]; | ||
| let recovered = truth; | ||
| let collapsed = [ | ||
| SummarizesKind::SourceDocument, | ||
| SummarizesKind::SourceDocument, | ||
| SummarizesKind::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(SummarizesEdgeError::InvalidEdgePayload) | ||
| ); | ||
| assert_eq!( | ||
| identity_recovery_rate(&[SummarizesKind::Summary], &[]), | ||
| Err(SummarizesEdgeError::InvalidEdgePayload) | ||
| ); | ||
| assert_eq!( | ||
| identity_recovery_rate( | ||
| &[SummarizesKind::Summary, SummarizesKind::SourceDocument], | ||
| &[SummarizesKind::Summary] | ||
| ), | ||
| Err(SummarizesEdgeError::InvalidEdgePayload) | ||
| ); | ||
| } |
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.
📝 Info: identity_recovery_rate cast precision is intentionally allowed
identity_recovery_rateat kind.rs caststruth.len() as f64, which would normally tripclippy::cast_precision_lossunder the workspace's denied pedantic lints. This is intentionally permitted via#![allow(clippy::cast_precision_loss)]in lib.rs. The empty/mismatched-length guard at line 75 ensures no division by zero, and match counting is correct.Was this helpful? React with 👍 or 👎 to provide feedback.