Repository navigation
feat(scoring): commit successful completion with outbox atomically #69
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
Closed
Closed
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
c64035e
test(scoring): require atomic completion outbox persistence
seonghobae c63ccd6
feat(scoring): commit completion and outbox atomically
seonghobae 42acf0f
feat(scoring): export completion outbox transaction
seonghobae 5f0bee7
style(scoring): apply rustfmt to completion composition
seonghobae e0daa19
style(scoring): apply rustfmt to completion tests
seonghobae 52ac36f
style(scoring): terminate completion composition with newline
seonghobae 24c36b2
style(scoring): terminate completion test with newline
seonghobae 0216aca
fix(scoring): compare persisted fence with checked conversion
seonghobae 89dcdf1
style(scoring): rustfmt checked fencing assertion
seonghobae ef1780c
test(scoring): reject misbound completion outbox envelopes
seonghobae 43845df
fix(scoring): bind completion outbox to job and time
seonghobae 9adaf29
style(scoring): apply rustfmt to completion envelope test
seonghobae 3854cef
test(scoring): instantiate completion envelope Display and source
seonghobae c202f6b
merge(main): reconcile persist PR #69 after #85
seonghobae cda680c
test(scoring): prove mixed exact completion and outbox reconciliation
cursoragent 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
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,135 @@ | ||
| //! Atomic `PostgreSQL` composition for scoring completion and integration outbox evidence. | ||
| //! | ||
| //! This module does not calculate psychometric quantities or define an external event schema. | ||
| //! It composes the existing fenced scoring-job completion transition with the existing durable | ||
| //! transactional outbox inside one caller-owned transaction so a successful result transition | ||
| //! cannot commit while its integration evidence is lost. | ||
|
|
||
| use crate::integration::IntegrationEvent; | ||
| use crate::postgres_integration::{enqueue_outbox_event, PersistenceDisposition, PersistenceError}; | ||
| use crate::postgres_scoring_job::{ | ||
| record_successful_scoring_completion, ScoringJobCompletionDisposition, | ||
| ScoringJobPersistenceError, | ||
| }; | ||
| use postgres::Transaction; | ||
| use std::error::Error; | ||
| use std::fmt::{Display, Formatter}; | ||
|
|
||
| const SOURCE_REF: &str = "psychometrics_commons"; | ||
|
|
||
| /// Durable dispositions produced by one atomic scoring-completion/outbox operation. | ||
| #[derive(Clone, Copy, Debug, Eq, PartialEq)] | ||
| pub struct ScoringCompletionOutboxPersistence { | ||
| completion: ScoringJobCompletionDisposition, | ||
| outbox: PersistenceDisposition, | ||
| } | ||
|
|
||
| impl ScoringCompletionOutboxPersistence { | ||
| /// Return whether scoring completion was newly committed or exactly replayed. | ||
| #[must_use] | ||
| pub const fn completion(self) -> ScoringJobCompletionDisposition { | ||
| self.completion | ||
| } | ||
|
|
||
| /// Return whether immutable outbox evidence was newly inserted or exactly replayed. | ||
| #[must_use] | ||
| pub const fn outbox(self) -> PersistenceDisposition { | ||
| self.outbox | ||
| } | ||
| } | ||
|
|
||
| /// Fail-closed error for atomic scoring completion and outbox persistence. | ||
| #[derive(Debug)] | ||
| #[non_exhaustive] | ||
| pub enum ScoringCompletionOutboxError { | ||
| /// The outbox envelope is not bound to this exact scoring-job completion boundary. | ||
| InvalidCompletionEnvelope, | ||
| /// The fenced scoring-job completion transition failed validation or persistence. | ||
| Completion(ScoringJobPersistenceError), | ||
| /// The immutable integration outbox event failed validation or persistence. | ||
| Outbox(PersistenceError), | ||
| } | ||
|
|
||
| impl Display for ScoringCompletionOutboxError { | ||
| fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result { | ||
| formatter.write_str(match self { | ||
| Self::InvalidCompletionEnvelope => { | ||
| "scoring completion outbox must bind the exact job and completion time" | ||
| } | ||
| Self::Completion(_) => "scoring completion persistence failed", | ||
| Self::Outbox(_) => "scoring completion outbox persistence failed", | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| impl Error for ScoringCompletionOutboxError { | ||
| fn source(&self) -> Option<&(dyn Error + 'static)> { | ||
| match self { | ||
| Self::InvalidCompletionEnvelope => None, | ||
| Self::Completion(error) => Some(error), | ||
| Self::Outbox(error) => Some(error), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Persist one fenced successful scoring completion and one immutable outbox event atomically. | ||
| /// | ||
| /// Before any write, the outbox event must be emitted by `psychometrics_commons`, identify the | ||
| /// exact scoring job as its subject, and carry the same server-authoritative completion time. | ||
| /// Event type, tenant, schema version, correlation, causation, payload digest, event identity, | ||
| /// and `scoring_result_ref` stay on the caller's versioned integration contract; this composition | ||
| /// does not add a schema that binds those fields. A new `event_ref` after an already-accepted | ||
| /// completion therefore inserts a second outbox row rather than rewriting historical completion | ||
| /// evidence. | ||
| /// | ||
| /// The caller supplies and owns a `READ COMMITTED` transaction. After envelope validation, the | ||
| /// scoring-job transition runs first and the outbox insert runs in the same transaction. If either | ||
| /// stage fails, callers must roll the transaction back so no newly written completion state can | ||
| /// survive without its outbox evidence. Exact replay remains idempotent at both existing adapters, | ||
| /// and mixed exact dispositions can safely reconcile legacy partial state without mutating | ||
| /// historical evidence. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Returns [`ScoringCompletionOutboxError::InvalidCompletionEnvelope`] before writes when the | ||
| /// integration event is bound to another source, job, or completion time, | ||
| /// [`ScoringCompletionOutboxError::Completion`] when fenced scoring completion fails, or | ||
| /// [`ScoringCompletionOutboxError::Outbox`] when the integration event cannot be persisted. | ||
| #[allow(clippy::too_many_arguments)] | ||
| pub fn record_successful_scoring_completion_with_outbox( | ||
| transaction: &mut Transaction<'_>, | ||
| scoring_job_ref: &str, | ||
| fencing_token: u64, | ||
| scoring_result_ref: &str, | ||
| completed_at_unix_ms: u64, | ||
| completion_event: &IntegrationEvent, | ||
| outbox_max_attempts: usize, | ||
| ) -> Result<ScoringCompletionOutboxPersistence, ScoringCompletionOutboxError> { | ||
| validate_completion_envelope(scoring_job_ref, completed_at_unix_ms, completion_event)?; | ||
| let completion = record_successful_scoring_completion( | ||
| transaction, | ||
| scoring_job_ref, | ||
| fencing_token, | ||
| scoring_result_ref, | ||
| completed_at_unix_ms, | ||
| ) | ||
| .map_err(ScoringCompletionOutboxError::Completion)?; | ||
| let outbox = enqueue_outbox_event(transaction, completion_event, outbox_max_attempts) | ||
| .map_err(ScoringCompletionOutboxError::Outbox)?; | ||
|
|
||
| Ok(ScoringCompletionOutboxPersistence { completion, outbox }) | ||
| } | ||
|
|
||
| fn validate_completion_envelope( | ||
| scoring_job_ref: &str, | ||
| completed_at_unix_ms: u64, | ||
| completion_event: &IntegrationEvent, | ||
| ) -> Result<(), ScoringCompletionOutboxError> { | ||
| if completion_event.source() != SOURCE_REF | ||
| || completion_event.subject_ref() != scoring_job_ref | ||
| || completion_event.occurred_at_unix_ms() != completed_at_unix_ms | ||
| { | ||
| return Err(ScoringCompletionOutboxError::InvalidCompletionEnvelope); | ||
| } | ||
| Ok(()) | ||
| } | ||
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.
The bind set is source + job +
occurred_at/completed_at. LeaveResultSnapshotandscoring_result_refto the worker/schema PR. Keepevent_refstable across retries: a new identity afterDuplicatecompletion inserts a second outbox row rather than reconciling.