-
Notifications
You must be signed in to change notification settings - Fork 0
feat(temporal): refuse other clocks as system time #128
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
8 commits
Select commit
Hold shift + click to select a range
0595f43
feat(temporal): refuse other clocks as system time
seonghobae 13eb288
Merge remote-tracking branch 'origin/main' into review/pr128-current
seonghobae 2c0ded6
test: derive crate contract from workspace manifest
seonghobae bbe7c6c
fix: use usize for system clock match counts
seonghobae 4bcc260
chore: keep codegraph index local
seonghobae 872137c
Merge current main into system clock gate
seonghobae 30ff215
test: measure system clock recovery against mixed truth
seonghobae 0b63ef7
Merge remote-tracking branch 'origin/main' into agent/system-clock-id…
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
Binary file not shown.
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 = "system_clock" | ||
| description = "System time cannot be replaced by event, assertion, document, available, or cutoff time." | ||
| 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,145 @@ | ||
| //! Clock-family identity for system stamps. | ||
|
|
||
| use crate::SystemClockError; | ||
|
|
||
| /// Closed vocabulary of clocks that must not be confused with system time. | ||
| #[derive(Clone, Copy, Debug, Eq, PartialEq)] | ||
| pub enum ClockFamily { | ||
| /// Event/valid time. | ||
| EventTime, | ||
| /// Assertion time. | ||
| AssertionTime, | ||
| /// Document creation or revision time. | ||
| DocumentTime, | ||
| /// Availability time. | ||
| AvailableTime, | ||
| /// Knowledge-cutoff time. | ||
| CutoffTime, | ||
| /// System/record time. | ||
| SystemTime, | ||
| } | ||
|
|
||
| /// Return whether a stamp is on the system clock. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// This function is infallible for the closed vocabulary and exists to keep | ||
| /// the public comparison surface explicit. | ||
| #[allow(clippy::unnecessary_wraps)] | ||
| pub fn stamp_is_system(family: ClockFamily) -> Result<bool, SystemClockError> { | ||
| Ok(matches!(family, ClockFamily::SystemTime)) | ||
| } | ||
|
|
||
| /// Refuse to treat event time as system time. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Always returns [`SystemClockError::EventTimeIsNotSystemTime`]. | ||
| pub fn refuse_event_time_as_system() -> Result<(), SystemClockError> { | ||
| Err(SystemClockError::EventTimeIsNotSystemTime) | ||
| } | ||
|
|
||
| /// Refuse to treat assertion time as system time. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Always returns [`SystemClockError::AssertionTimeIsNotSystemTime`]. | ||
| pub fn refuse_assertion_time_as_system() -> Result<(), SystemClockError> { | ||
| Err(SystemClockError::AssertionTimeIsNotSystemTime) | ||
| } | ||
|
|
||
| /// Refuse to treat document time as system time. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Always returns [`SystemClockError::DocumentTimeIsNotSystemTime`]. | ||
| pub fn refuse_document_time_as_system() -> Result<(), SystemClockError> { | ||
| Err(SystemClockError::DocumentTimeIsNotSystemTime) | ||
| } | ||
|
|
||
| /// Refuse to treat availability time as system time. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Always returns [`SystemClockError::AvailableTimeIsNotSystemTime`]. | ||
| pub fn refuse_available_time_as_system() -> Result<(), SystemClockError> { | ||
| Err(SystemClockError::AvailableTimeIsNotSystemTime) | ||
| } | ||
|
|
||
| /// Refuse to treat knowledge-cutoff time as system time. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Always returns [`SystemClockError::CutoffTimeIsNotSystemTime`]. | ||
| pub fn refuse_cutoff_time_as_system() -> Result<(), SystemClockError> { | ||
| Err(SystemClockError::CutoffTimeIsNotSystemTime) | ||
| } | ||
|
seonghobae marked this conversation as resolved.
|
||
|
|
||
| /// Fraction of recovered system-clock flags that match known truth. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Returns [`SystemClockError::InvalidSystemPayload`] when either slice is | ||
| /// empty or the lengths differ. | ||
| pub fn identity_recovery_rate(truth: &[bool], decided: &[bool]) -> Result<f64, SystemClockError> { | ||
| if truth.is_empty() || truth.len() != decided.len() { | ||
| return Err(SystemClockError::InvalidSystemPayload); | ||
| } | ||
| let mut matches = 0_usize; | ||
| for (truth_flag, decided_flag) in truth.iter().zip(decided) { | ||
| if truth_flag == decided_flag { | ||
| matches += 1; | ||
| } | ||
| } | ||
| Ok(matches as f64 / truth.len() as f64) | ||
|
seonghobae marked this conversation as resolved.
seonghobae marked this conversation as resolved.
|
||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::{ | ||
| ClockFamily, identity_recovery_rate, refuse_assertion_time_as_system, | ||
| refuse_available_time_as_system, refuse_cutoff_time_as_system, | ||
| refuse_document_time_as_system, refuse_event_time_as_system, stamp_is_system, | ||
| }; | ||
| use crate::SystemClockError; | ||
|
|
||
| #[test] | ||
| fn local_branches_cover_families_and_payloads() { | ||
| assert!(stamp_is_system(ClockFamily::SystemTime).expect("system")); | ||
| assert!(!stamp_is_system(ClockFamily::EventTime).expect("event")); | ||
| assert!(!stamp_is_system(ClockFamily::AssertionTime).expect("assertion")); | ||
| assert!(!stamp_is_system(ClockFamily::DocumentTime).expect("document")); | ||
| assert!(!stamp_is_system(ClockFamily::AvailableTime).expect("available")); | ||
| assert!(!stamp_is_system(ClockFamily::CutoffTime).expect("cutoff")); | ||
| assert_eq!( | ||
| refuse_event_time_as_system(), | ||
| Err(SystemClockError::EventTimeIsNotSystemTime) | ||
| ); | ||
| assert_eq!( | ||
| refuse_assertion_time_as_system(), | ||
| Err(SystemClockError::AssertionTimeIsNotSystemTime) | ||
| ); | ||
| assert_eq!( | ||
| refuse_document_time_as_system(), | ||
| Err(SystemClockError::DocumentTimeIsNotSystemTime) | ||
| ); | ||
| assert_eq!( | ||
| refuse_available_time_as_system(), | ||
| Err(SystemClockError::AvailableTimeIsNotSystemTime) | ||
| ); | ||
| assert_eq!( | ||
| refuse_cutoff_time_as_system(), | ||
| Err(SystemClockError::CutoffTimeIsNotSystemTime) | ||
| ); | ||
| let matched = identity_recovery_rate(&[true], &[true]).expect("rate"); | ||
| assert!((matched - 1.0).abs() < f64::EPSILON); | ||
| assert_eq!( | ||
| identity_recovery_rate(&[], &[]), | ||
| Err(SystemClockError::InvalidSystemPayload) | ||
| ); | ||
| assert_eq!( | ||
| identity_recovery_rate(&[true], &[]), | ||
| Err(SystemClockError::InvalidSystemPayload) | ||
| ); | ||
| } | ||
| } | ||
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,74 @@ | ||
| //! Fail-closed system-clock errors. | ||
|
|
||
| use std::fmt; | ||
|
|
||
| /// A fail-closed system-clock error. | ||
| #[derive(Clone, Copy, Debug, Eq, PartialEq)] | ||
| #[non_exhaustive] | ||
| pub enum SystemClockError { | ||
| /// Event time was treated as system time. | ||
| EventTimeIsNotSystemTime, | ||
| /// Assertion time was treated as system time. | ||
| AssertionTimeIsNotSystemTime, | ||
| /// Document time was treated as system time. | ||
| DocumentTimeIsNotSystemTime, | ||
| /// Availability time was treated as system time. | ||
| AvailableTimeIsNotSystemTime, | ||
| /// Knowledge-cutoff time was treated as system time. | ||
| CutoffTimeIsNotSystemTime, | ||
| /// A recovery slice was empty or length-mismatched. | ||
| InvalidSystemPayload, | ||
| } | ||
|
|
||
| impl fmt::Display for SystemClockError { | ||
| fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| let message = match self { | ||
| Self::EventTimeIsNotSystemTime => "event time is not system time", | ||
| Self::AssertionTimeIsNotSystemTime => "assertion time is not system time", | ||
| Self::DocumentTimeIsNotSystemTime => "document time is not system time", | ||
| Self::AvailableTimeIsNotSystemTime => "availability time is not system time", | ||
| Self::CutoffTimeIsNotSystemTime => "knowledge cutoff is not system time", | ||
| Self::InvalidSystemPayload => "invalid system-clock payload", | ||
| }; | ||
| formatter.write_str(message) | ||
| } | ||
| } | ||
|
|
||
| impl std::error::Error for SystemClockError {} | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::SystemClockError; | ||
|
|
||
| #[test] | ||
| fn error_messages_are_stable() { | ||
| for (error, message) in [ | ||
| ( | ||
| SystemClockError::EventTimeIsNotSystemTime, | ||
| "event time is not system time", | ||
| ), | ||
| ( | ||
| SystemClockError::AssertionTimeIsNotSystemTime, | ||
| "assertion time is not system time", | ||
| ), | ||
| ( | ||
| SystemClockError::DocumentTimeIsNotSystemTime, | ||
| "document time is not system time", | ||
| ), | ||
| ( | ||
| SystemClockError::AvailableTimeIsNotSystemTime, | ||
| "availability time is not system time", | ||
| ), | ||
| ( | ||
| SystemClockError::CutoffTimeIsNotSystemTime, | ||
| "knowledge cutoff is not system time", | ||
| ), | ||
| ( | ||
| SystemClockError::InvalidSystemPayload, | ||
| "invalid system-clock 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,29 @@ | ||
| #![forbid(unsafe_code)] | ||
| #![deny(missing_docs)] | ||
| #![allow(clippy::cast_precision_loss)] | ||
| //! System time cannot be replaced by the other TEPP clocks. | ||
| //! | ||
| //! System/record time is when TEPP recorded a change. Event, assertion, | ||
| //! document, available, and cutoff times are not substitutes (ADR 0002). | ||
|
|
||
| mod clock; | ||
| mod error; | ||
|
|
||
| /// Closed vocabulary of clocks that must not be confused with system time. | ||
| pub use clock::ClockFamily; | ||
| /// Fraction of recovered system-clock flags that match known truth. | ||
| pub use clock::identity_recovery_rate; | ||
| /// Refuse to treat assertion time as system time. | ||
| pub use clock::refuse_assertion_time_as_system; | ||
| /// Refuse to treat availability time as system time. | ||
| pub use clock::refuse_available_time_as_system; | ||
| /// Refuse to treat knowledge-cutoff time as system time. | ||
| pub use clock::refuse_cutoff_time_as_system; | ||
| /// Refuse to treat document time as system time. | ||
| pub use clock::refuse_document_time_as_system; | ||
| /// Refuse to treat event time as system time. | ||
| pub use clock::refuse_event_time_as_system; | ||
| /// Return whether a stamp is on the system clock. | ||
| pub use clock::stamp_is_system; | ||
| /// Fail-closed system-clock errors. | ||
| pub use error::SystemClockError; |
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 `system_clock` package identity. | ||
|
|
||
| #[test] | ||
| fn package_identity_is_stable() { | ||
| let observed = std::hint::black_box(env!("CARGO_PKG_NAME")); | ||
| assert_eq!(observed, "system_clock"); | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.