1use std::{fmt, time::Duration};
5
6use serde::{Deserialize, Serialize};
7use stateknot_core::{
8 AgentAdmission, ArtifactRef, Checkpoint, CheckpointHead, CheckpointId, CompiledGraph,
9 DeliveryFence, Digest, DurableTimer, DurableTimerRecord, DurableWait, FencingEpoch,
10 InterruptRecord, InterruptRequest, JournalEvent, JournalExpectation, JournalHead,
11 JournalPayload, ModelInvocation, NodeAttempt, OutboxAttempt, OutboxAttemptCompletion,
12 OutboxAttemptStart, OutboxDelivery, OutboxDestinationRef, PendingNodeResult,
13 PendingNodeResultHead, QuarantineId, RunFence, RunId, RunLease, RunLifecycle, RunRevision,
14 RunTransition, SchedulerReservationId, SchedulerShardId, Superstep, TenantId, Timestamp,
15 ToolInvocation,
16};
17
18use crate::StoreError;
19
20const SCHEDULER_FAIRNESS_POLICY_DIGEST_DOMAIN: &[u8] = b"stateknot.scheduler-fairness-policy.v1\0";
21
22#[derive(Clone, Eq, PartialEq)]
28pub struct ArtifactStorageLocator {
29 storage_namespace: Box<str>,
30 object_key: Box<str>,
31 object_version: Option<Box<str>>,
32 object_etag: Option<Box<str>>,
33}
34
35impl ArtifactStorageLocator {
36 pub const MAX_STORAGE_NAMESPACE_BYTES: usize = 128;
38 pub const MAX_OBJECT_KEY_BYTES: usize = 1024;
40 pub const MAX_OBJECT_VERSION_BYTES: usize = 1024;
42 pub const MAX_OBJECT_ETAG_BYTES: usize = 1024;
44
45 pub fn new(
57 storage_namespace: impl Into<String>,
58 object_key: impl Into<String>,
59 object_version: Option<String>,
60 object_etag: Option<String>,
61 ) -> Result<Self, StoreError> {
62 let storage_namespace = storage_namespace.into();
63 let object_key = object_key.into();
64 validate_storage_namespace(&storage_namespace)?;
65 validate_object_key(&object_key)?;
66 let object_version =
67 validate_opaque_locator_value(object_version, Self::MAX_OBJECT_VERSION_BYTES)?;
68 let object_etag = validate_opaque_locator_value(object_etag, Self::MAX_OBJECT_ETAG_BYTES)?;
69 Ok(Self {
70 storage_namespace: storage_namespace.into_boxed_str(),
71 object_key: object_key.into_boxed_str(),
72 object_version,
73 object_etag,
74 })
75 }
76
77 #[must_use]
79 pub fn storage_namespace(&self) -> &str {
80 &self.storage_namespace
81 }
82
83 #[must_use]
85 pub fn object_key(&self) -> &str {
86 &self.object_key
87 }
88
89 #[must_use]
91 pub fn object_version(&self) -> Option<&str> {
92 self.object_version.as_deref()
93 }
94
95 #[must_use]
97 pub fn object_etag(&self) -> Option<&str> {
98 self.object_etag.as_deref()
99 }
100}
101
102impl fmt::Debug for ArtifactStorageLocator {
103 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
104 formatter
105 .debug_struct("ArtifactStorageLocator")
106 .field("coordinates", &"<redacted>")
107 .field("has_object_version", &self.object_version.is_some())
108 .field("has_object_etag", &self.object_etag.is_some())
109 .finish_non_exhaustive()
110 }
111}
112
113#[derive(Clone, Eq, PartialEq)]
115pub struct ArtifactRegistration {
116 registration_key: Digest,
117 artifact: ArtifactRef,
118 locator: ArtifactStorageLocator,
119}
120
121impl ArtifactRegistration {
122 #[must_use]
124 pub const fn new(
125 registration_key: Digest,
126 artifact: ArtifactRef,
127 locator: ArtifactStorageLocator,
128 ) -> Self {
129 Self {
130 registration_key,
131 artifact,
132 locator,
133 }
134 }
135
136 #[must_use]
138 pub const fn registration_key(&self) -> Digest {
139 self.registration_key
140 }
141
142 #[must_use]
144 pub const fn artifact(&self) -> &ArtifactRef {
145 &self.artifact
146 }
147
148 #[must_use]
150 pub const fn locator(&self) -> &ArtifactStorageLocator {
151 &self.locator
152 }
153}
154
155impl fmt::Debug for ArtifactRegistration {
156 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
157 formatter
158 .debug_struct("ArtifactRegistration")
159 .field("identity", self.artifact.identity())
160 .field("locator", &self.locator)
161 .finish_non_exhaustive()
162 }
163}
164
165#[derive(Clone, Eq, PartialEq)]
167pub struct StoredArtifact {
168 pub(crate) registration: ArtifactRegistration,
169 pub(crate) registered_at: Timestamp,
170}
171
172impl StoredArtifact {
173 #[must_use]
179 pub const fn new(registration: ArtifactRegistration, registered_at: Timestamp) -> Self {
180 Self {
181 registration,
182 registered_at,
183 }
184 }
185
186 #[must_use]
188 pub const fn registration(&self) -> &ArtifactRegistration {
189 &self.registration
190 }
191
192 #[must_use]
194 pub const fn artifact(&self) -> &ArtifactRef {
195 self.registration.artifact()
196 }
197
198 #[must_use]
200 pub const fn locator(&self) -> &ArtifactStorageLocator {
201 self.registration.locator()
202 }
203
204 #[must_use]
206 pub const fn registered_at(&self) -> Timestamp {
207 self.registered_at
208 }
209}
210
211impl fmt::Debug for StoredArtifact {
212 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
213 formatter
214 .debug_struct("StoredArtifact")
215 .field("identity", self.artifact().identity())
216 .field("registered_at", &self.registered_at)
217 .field("locator", self.locator())
218 .finish_non_exhaustive()
219 }
220}
221
222#[derive(Clone, Debug, Eq, PartialEq)]
224#[non_exhaustive]
225pub enum ArtifactRegistrationOutcome {
226 Registered(StoredArtifact),
228 Idempotent(StoredArtifact),
230}
231
232impl ArtifactRegistrationOutcome {
233 #[must_use]
235 pub const fn stored(&self) -> &StoredArtifact {
236 match self {
237 Self::Registered(stored) | Self::Idempotent(stored) => stored,
238 }
239 }
240}
241
242fn validate_storage_namespace(value: &str) -> Result<(), StoreError> {
243 if value.is_empty()
244 || value.len() > ArtifactStorageLocator::MAX_STORAGE_NAMESPACE_BYTES
245 || value == "."
246 || value == ".."
247 || !value.as_bytes()[0].is_ascii_alphanumeric()
248 || value
249 .bytes()
250 .any(|byte| !byte.is_ascii_alphanumeric() && !matches!(byte, b'.' | b'_' | b':' | b'-'))
251 {
252 return Err(StoreError::InvalidArtifactStorageLocator);
253 }
254 Ok(())
255}
256
257fn validate_object_key(value: &str) -> Result<(), StoreError> {
258 if value.is_empty()
259 || value.len() > ArtifactStorageLocator::MAX_OBJECT_KEY_BYTES
260 || value.starts_with('/')
261 || value.ends_with('/')
262 || value.chars().any(|character| {
263 character == '\\'
264 || character.is_control()
265 || matches!(character, '\u{FDD0}'..='\u{FDEF}')
266 || (character as u32 & 0xFFFE) == 0xFFFE
267 })
268 || value
269 .split('/')
270 .any(|segment| segment.is_empty() || matches!(segment, "." | ".."))
271 {
272 return Err(StoreError::InvalidArtifactStorageLocator);
273 }
274 Ok(())
275}
276
277fn validate_opaque_locator_value(
278 value: Option<String>,
279 maximum_bytes: usize,
280) -> Result<Option<Box<str>>, StoreError> {
281 value
282 .map(|value| {
283 if value.is_empty()
284 || value.len() > maximum_bytes
285 || !value
286 .bytes()
287 .all(|byte| byte.is_ascii_graphic() || byte == b' ')
288 {
289 return Err(StoreError::InvalidArtifactStorageLocator);
290 }
291 Ok(value.into_boxed_str())
292 })
293 .transpose()
294}
295
296#[derive(Clone, Debug, Eq, PartialEq)]
303pub struct SchedulerFairnessPolicyRegistration {
304 shard_id: SchedulerShardId,
305 policy_digest: Digest,
306 policy_bytes: Box<[u8]>,
307 cycle_length: u16,
308}
309
310impl SchedulerFairnessPolicyRegistration {
311 pub const MAX_POLICY_BYTES: usize = 262_144;
313 pub const MAX_CYCLE_LENGTH: u16 = 4096;
315
316 pub fn new(
322 shard_id: SchedulerShardId,
323 policy_bytes: impl Into<Vec<u8>>,
324 cycle_length: u16,
325 ) -> Result<Self, StoreError> {
326 let policy_bytes = policy_bytes.into();
327 if policy_bytes.is_empty()
328 || policy_bytes.len() > Self::MAX_POLICY_BYTES
329 || cycle_length == 0
330 || cycle_length > Self::MAX_CYCLE_LENGTH
331 {
332 return Err(StoreError::InvalidSchedulerFairnessPolicy);
333 }
334 let mut preimage =
335 Vec::with_capacity(SCHEDULER_FAIRNESS_POLICY_DIGEST_DOMAIN.len() + policy_bytes.len());
336 preimage.extend_from_slice(SCHEDULER_FAIRNESS_POLICY_DIGEST_DOMAIN);
337 preimage.extend_from_slice(&policy_bytes);
338 Ok(Self {
339 shard_id,
340 policy_digest: Digest::sha256(preimage),
341 policy_bytes: policy_bytes.into_boxed_slice(),
342 cycle_length,
343 })
344 }
345
346 #[must_use]
348 pub const fn shard_id(&self) -> &SchedulerShardId {
349 &self.shard_id
350 }
351
352 #[must_use]
354 pub const fn policy_digest(&self) -> Digest {
355 self.policy_digest
356 }
357
358 #[must_use]
360 pub const fn policy_bytes(&self) -> &[u8] {
361 &self.policy_bytes
362 }
363
364 #[must_use]
366 pub const fn cycle_length(&self) -> u16 {
367 self.cycle_length
368 }
369}
370
371#[derive(Clone, Debug, Eq, PartialEq)]
373pub struct StoredSchedulerFairnessPolicy {
374 pub(crate) registration: SchedulerFairnessPolicyRegistration,
375 pub(crate) registered_at: Timestamp,
376}
377
378impl StoredSchedulerFairnessPolicy {
379 #[must_use]
381 pub const fn registration(&self) -> &SchedulerFairnessPolicyRegistration {
382 &self.registration
383 }
384
385 #[must_use]
387 pub const fn registered_at(&self) -> Timestamp {
388 self.registered_at
389 }
390}
391
392#[derive(Clone, Debug, Eq, PartialEq)]
394#[non_exhaustive]
395pub enum SchedulerFairnessPolicyRegistrationOutcome {
396 Registered(StoredSchedulerFairnessPolicy),
398 Idempotent(StoredSchedulerFairnessPolicy),
400}
401
402impl SchedulerFairnessPolicyRegistrationOutcome {
403 #[must_use]
405 pub const fn policy(&self) -> &StoredSchedulerFairnessPolicy {
406 match self {
407 Self::Registered(policy) | Self::Idempotent(policy) => policy,
408 }
409 }
410}
411
412#[derive(Clone, Debug, Eq, PartialEq)]
414pub struct SchedulerFairnessReservation {
415 pub(crate) shard_id: SchedulerShardId,
416 pub(crate) reservation_id: SchedulerReservationId,
417 pub(crate) policy_digest: Digest,
418 pub(crate) sequence: u64,
419 pub(crate) slot: u16,
420 pub(crate) reserved_at: Timestamp,
421}
422
423impl SchedulerFairnessReservation {
424 #[must_use]
426 pub const fn shard_id(&self) -> &SchedulerShardId {
427 &self.shard_id
428 }
429
430 #[must_use]
432 pub const fn reservation_id(&self) -> SchedulerReservationId {
433 self.reservation_id
434 }
435
436 #[must_use]
438 pub const fn policy_digest(&self) -> Digest {
439 self.policy_digest
440 }
441
442 #[must_use]
444 pub const fn sequence(&self) -> u64 {
445 self.sequence
446 }
447
448 #[must_use]
450 pub const fn slot(&self) -> u16 {
451 self.slot
452 }
453
454 #[must_use]
456 pub const fn reserved_at(&self) -> Timestamp {
457 self.reserved_at
458 }
459}
460
461#[derive(Clone, Copy, Debug, Eq, PartialEq)]
468pub struct SchedulerFairnessRetentionPolicy {
469 retain_for: Duration,
470 batch_size: u16,
471}
472
473impl SchedulerFairnessRetentionPolicy {
474 pub const MIN_RETAIN_FOR: Duration = Duration::from_secs(60 * 60);
476 pub const MAX_RETAIN_FOR: Duration = Duration::from_secs(366 * 24 * 60 * 60);
478 pub const MAX_BATCH_SIZE: u16 = 10_000;
480
481 pub const fn new(retain_for: Duration, batch_size: u16) -> Result<Self, StoreError> {
487 if retain_for.as_nanos() < Self::MIN_RETAIN_FOR.as_nanos()
488 || retain_for.as_nanos() > Self::MAX_RETAIN_FOR.as_nanos()
489 || batch_size == 0
490 || batch_size > Self::MAX_BATCH_SIZE
491 {
492 return Err(StoreError::InvalidSchedulerFairnessRetention);
493 }
494 Ok(Self {
495 retain_for,
496 batch_size,
497 })
498 }
499
500 #[must_use]
502 pub const fn retain_for(self) -> Duration {
503 self.retain_for
504 }
505
506 #[must_use]
508 pub const fn batch_size(self) -> u16 {
509 self.batch_size
510 }
511}
512
513#[derive(Clone, Copy, Debug, Eq, PartialEq)]
515pub struct SchedulerFairnessRetentionReport {
516 pub(crate) observed_at: Timestamp,
517 pub(crate) cutoff: Timestamp,
518 pub(crate) deleted: u16,
519}
520
521impl SchedulerFairnessRetentionReport {
522 #[must_use]
524 pub const fn observed_at(self) -> Timestamp {
525 self.observed_at
526 }
527
528 #[must_use]
530 pub const fn cutoff(self) -> Timestamp {
531 self.cutoff
532 }
533
534 #[must_use]
536 pub const fn deleted(self) -> u16 {
537 self.deleted
538 }
539}
540
541#[derive(Clone, Debug, Eq, PartialEq)]
543pub struct StoredGraphDefinition {
544 pub(crate) tenant_id: TenantId,
545 pub(crate) graph: CompiledGraph,
546 pub(crate) registered_at: Timestamp,
547}
548
549impl StoredGraphDefinition {
550 #[must_use]
552 pub const fn tenant_id(&self) -> &TenantId {
553 &self.tenant_id
554 }
555
556 #[must_use]
558 pub const fn graph(&self) -> &CompiledGraph {
559 &self.graph
560 }
561
562 #[must_use]
564 pub const fn registered_at(&self) -> Timestamp {
565 self.registered_at
566 }
567}
568
569#[derive(Clone, Debug, Eq, PartialEq)]
571#[non_exhaustive]
572pub enum GraphDefinitionRegistrationOutcome {
573 Registered(StoredGraphDefinition),
575 Idempotent(StoredGraphDefinition),
577}
578
579impl GraphDefinitionRegistrationOutcome {
580 #[must_use]
582 pub const fn definition(&self) -> &StoredGraphDefinition {
583 match self {
584 Self::Registered(definition) | Self::Idempotent(definition) => definition,
585 }
586 }
587}
588
589#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
595#[serde(rename_all = "snake_case")]
596#[non_exhaustive]
597pub enum RunQuarantineCause {
598 IntegrityFailure,
600 UnsupportedSchema,
602 MissingArtifact,
604 CrossTenantReference,
606 ProjectionMismatch,
608 FencingEpochExhausted,
610 OperatorPolicy,
612}
613
614impl RunQuarantineCause {
615 #[must_use]
617 pub const fn as_str(self) -> &'static str {
618 match self {
619 Self::IntegrityFailure => "integrity_failure",
620 Self::UnsupportedSchema => "unsupported_schema",
621 Self::MissingArtifact => "missing_artifact",
622 Self::CrossTenantReference => "cross_tenant_reference",
623 Self::ProjectionMismatch => "projection_mismatch",
624 Self::FencingEpochExhausted => "fencing_epoch_exhausted",
625 Self::OperatorPolicy => "operator_policy",
626 }
627 }
628}
629
630#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
632pub struct RunQuarantineComponent(Box<str>);
633
634impl RunQuarantineComponent {
635 pub const MAX_LEN: usize = 128;
637
638 pub fn new(value: impl Into<String>) -> Result<Self, StoreError> {
648 let value = value.into();
649 if value.is_empty()
650 || value.len() > Self::MAX_LEN
651 || value.bytes().any(|byte| {
652 !byte.is_ascii_lowercase()
653 && !byte.is_ascii_digit()
654 && !matches!(byte, b'.' | b'_' | b':' | b'-')
655 })
656 {
657 return Err(StoreError::InvalidRunQuarantineComponent);
658 }
659 Ok(Self(value.into_boxed_str()))
660 }
661
662 pub fn from_corrupt_store_error(error: &StoreError) -> Result<Self, StoreError> {
671 let record = error
672 .corrupt_record()
673 .ok_or(StoreError::InvalidRunQuarantineRequest)?;
674 let mut component = String::with_capacity("store.".len() + record.len());
675 component.push_str("store.");
676 let mut previous_separator = false;
677 for byte in record.bytes() {
678 let normalized = if byte.is_ascii_alphanumeric() {
679 byte.to_ascii_lowercase()
680 } else if matches!(byte, b'.' | b':' | b'-') {
681 byte
682 } else {
683 b'_'
684 };
685 if normalized == b'_' {
686 if previous_separator {
687 continue;
688 }
689 previous_separator = true;
690 } else {
691 previous_separator = false;
692 }
693 component.push(char::from(normalized));
694 }
695 while component.ends_with('_') {
696 component.pop();
697 }
698 Self::new(component)
699 }
700
701 #[must_use]
703 pub fn as_str(&self) -> &str {
704 &self.0
705 }
706}
707
708#[derive(Clone, Debug, Eq, PartialEq)]
710pub struct RunQuarantineRequest {
711 pub(crate) tenant_id: TenantId,
712 pub(crate) run_id: RunId,
713 pub(crate) quarantine_id: QuarantineId,
714 pub(crate) expectation: JournalExpectation,
715 pub(crate) cause: RunQuarantineCause,
716 pub(crate) component: RunQuarantineComponent,
717 pub(crate) evidence_digest: Digest,
718 pub(crate) expected_fence: Option<RunFence>,
719}
720
721impl RunQuarantineRequest {
722 pub fn new(
732 tenant_id: TenantId,
733 run_id: RunId,
734 quarantine_id: QuarantineId,
735 expectation: JournalExpectation,
736 cause: RunQuarantineCause,
737 component: RunQuarantineComponent,
738 evidence_digest: Digest,
739 ) -> Result<Self, StoreError> {
740 if expectation
741 .head()
742 .is_some_and(|head| head.tenant_id() != &tenant_id || head.run_id() != run_id)
743 {
744 return Err(StoreError::InvalidRunQuarantineRequest);
745 }
746 Ok(Self {
747 tenant_id,
748 run_id,
749 quarantine_id,
750 expectation,
751 cause,
752 component,
753 evidence_digest,
754 expected_fence: None,
755 })
756 }
757
758 pub fn with_expected_fence(mut self, fence: RunFence) -> Result<Self, StoreError> {
769 if fence.tenant_id() != &self.tenant_id || fence.run_id() != self.run_id {
770 return Err(StoreError::InvalidRunQuarantineRequest);
771 }
772 if self
773 .expected_fence
774 .as_ref()
775 .is_some_and(|expected| expected != &fence)
776 {
777 return Err(StoreError::InvalidRunQuarantineRequest);
778 }
779 self.expected_fence = Some(fence);
780 Ok(self)
781 }
782
783 #[must_use]
785 pub const fn tenant_id(&self) -> &TenantId {
786 &self.tenant_id
787 }
788
789 #[must_use]
791 pub const fn run_id(&self) -> RunId {
792 self.run_id
793 }
794
795 #[must_use]
797 pub const fn quarantine_id(&self) -> QuarantineId {
798 self.quarantine_id
799 }
800
801 #[must_use]
803 pub const fn expectation(&self) -> &JournalExpectation {
804 &self.expectation
805 }
806
807 #[must_use]
809 pub const fn cause(&self) -> RunQuarantineCause {
810 self.cause
811 }
812
813 #[must_use]
815 pub const fn component(&self) -> &RunQuarantineComponent {
816 &self.component
817 }
818
819 #[must_use]
821 pub const fn evidence_digest(&self) -> Digest {
822 self.evidence_digest
823 }
824
825 #[must_use]
827 pub const fn expected_fence(&self) -> Option<&RunFence> {
828 self.expected_fence.as_ref()
829 }
830}
831
832#[derive(Clone, Debug, Eq, PartialEq)]
838pub struct CorruptionQuarantineContext {
839 pub(crate) tenant_id: TenantId,
840 pub(crate) run_id: RunId,
841 pub(crate) quarantine_id: QuarantineId,
842 pub(crate) expectation: JournalExpectation,
843 pub(crate) evidence_digest: Digest,
844 pub(crate) expected_fence: Option<RunFence>,
845}
846
847impl CorruptionQuarantineContext {
848 pub fn new(
855 tenant_id: TenantId,
856 run_id: RunId,
857 quarantine_id: QuarantineId,
858 expectation: JournalExpectation,
859 evidence_digest: Digest,
860 ) -> Result<Self, StoreError> {
861 if expectation
862 .head()
863 .is_some_and(|head| head.tenant_id() != &tenant_id || head.run_id() != run_id)
864 {
865 return Err(StoreError::InvalidRunQuarantineRequest);
866 }
867 Ok(Self {
868 tenant_id,
869 run_id,
870 quarantine_id,
871 expectation,
872 evidence_digest,
873 expected_fence: None,
874 })
875 }
876
877 pub fn with_expected_fence(mut self, fence: RunFence) -> Result<Self, StoreError> {
884 if fence.tenant_id() != &self.tenant_id || fence.run_id() != self.run_id {
885 return Err(StoreError::InvalidClaimedRunRecoveryContext);
886 }
887 if self
888 .expected_fence
889 .as_ref()
890 .is_some_and(|expected| expected != &fence)
891 {
892 return Err(StoreError::InvalidClaimedRunRecoveryContext);
893 }
894 self.expected_fence = Some(fence);
895 Ok(self)
896 }
897
898 #[must_use]
900 pub const fn tenant_id(&self) -> &TenantId {
901 &self.tenant_id
902 }
903
904 #[must_use]
906 pub const fn run_id(&self) -> RunId {
907 self.run_id
908 }
909
910 #[must_use]
912 pub const fn quarantine_id(&self) -> QuarantineId {
913 self.quarantine_id
914 }
915
916 #[must_use]
918 pub const fn expectation(&self) -> &JournalExpectation {
919 &self.expectation
920 }
921
922 #[must_use]
924 pub const fn evidence_digest(&self) -> Digest {
925 self.evidence_digest
926 }
927
928 #[must_use]
930 pub const fn expected_fence(&self) -> Option<&RunFence> {
931 self.expected_fence.as_ref()
932 }
933
934 pub(crate) fn into_request(
935 self,
936 component: RunQuarantineComponent,
937 ) -> Result<RunQuarantineRequest, StoreError> {
938 let request = RunQuarantineRequest::new(
939 self.tenant_id,
940 self.run_id,
941 self.quarantine_id,
942 self.expectation,
943 RunQuarantineCause::IntegrityFailure,
944 component,
945 self.evidence_digest,
946 )?;
947 match self.expected_fence {
948 Some(fence) => request.with_expected_fence(fence),
949 None => Ok(request),
950 }
951 }
952}
953
954#[derive(Clone, Debug, Eq, PartialEq)]
956pub struct RunQuarantine {
957 pub(crate) request: RunQuarantineRequest,
958 pub(crate) quarantined_at: Timestamp,
959 pub(crate) digest: Digest,
960}
961
962impl RunQuarantine {
963 #[must_use]
965 pub const fn request(&self) -> &RunQuarantineRequest {
966 &self.request
967 }
968
969 #[must_use]
971 pub const fn quarantined_at(&self) -> Timestamp {
972 self.quarantined_at
973 }
974
975 #[must_use]
977 pub const fn digest(&self) -> Digest {
978 self.digest
979 }
980}
981
982#[derive(Clone, Debug, Eq, PartialEq)]
984#[non_exhaustive]
985pub enum RunQuarantineCommitOutcome {
986 Committed(RunQuarantine),
988 Idempotent(RunQuarantine),
990}
991
992impl RunQuarantineCommitOutcome {
993 #[must_use]
995 pub const fn quarantine(&self) -> &RunQuarantine {
996 match self {
997 Self::Committed(quarantine) | Self::Idempotent(quarantine) => quarantine,
998 }
999 }
1000}
1001
1002#[derive(Clone, Debug)]
1004#[non_exhaustive]
1005pub enum AdmissionOutcome {
1006 Committed(RunLifecycle),
1008 Idempotent(RunLifecycle),
1010}
1011
1012impl AdmissionOutcome {
1013 #[must_use]
1015 pub const fn lifecycle(&self) -> &RunLifecycle {
1016 match self {
1017 Self::Committed(lifecycle) | Self::Idempotent(lifecycle) => lifecycle,
1018 }
1019 }
1020}
1021
1022#[derive(Clone, Debug)]
1028pub struct StoredAgentAdmission {
1029 pub(crate) admission: AgentAdmission,
1030 pub(crate) run: StoredRun,
1031 pub(crate) event: JournalEvent,
1032 pub(crate) checkpoint: Checkpoint,
1033}
1034
1035impl StoredAgentAdmission {
1036 #[must_use]
1038 pub const fn admission(&self) -> &AgentAdmission {
1039 &self.admission
1040 }
1041
1042 #[must_use]
1044 pub const fn run(&self) -> &StoredRun {
1045 &self.run
1046 }
1047
1048 #[must_use]
1050 pub const fn event(&self) -> &JournalEvent {
1051 &self.event
1052 }
1053
1054 #[must_use]
1056 pub const fn checkpoint(&self) -> &Checkpoint {
1057 &self.checkpoint
1058 }
1059}
1060
1061#[derive(Clone, Debug)]
1063#[non_exhaustive]
1064pub enum AgentAdmissionCommitOutcome {
1065 Committed(StoredAgentAdmission),
1067 Idempotent(StoredAgentAdmission),
1069}
1070
1071impl AgentAdmissionCommitOutcome {
1072 #[must_use]
1074 pub const fn stored(&self) -> &StoredAgentAdmission {
1075 match self {
1076 Self::Committed(stored) | Self::Idempotent(stored) => stored,
1077 }
1078 }
1079}
1080
1081#[derive(Clone, Debug)]
1083pub struct StoredAgentSubmission {
1084 pub(crate) key_digest: Digest,
1085 pub(crate) submission_digest: Digest,
1086 pub(crate) admission: StoredAgentAdmission,
1087 pub(crate) created_at: Timestamp,
1088}
1089
1090impl StoredAgentSubmission {
1091 #[must_use]
1093 pub const fn key_digest(&self) -> Digest {
1094 self.key_digest
1095 }
1096
1097 #[must_use]
1099 pub const fn submission_digest(&self) -> Digest {
1100 self.submission_digest
1101 }
1102
1103 #[must_use]
1105 pub const fn admission(&self) -> &StoredAgentAdmission {
1106 &self.admission
1107 }
1108
1109 #[must_use]
1111 pub const fn created_at(&self) -> Timestamp {
1112 self.created_at
1113 }
1114}
1115
1116#[derive(Clone, Debug)]
1118#[non_exhaustive]
1119pub enum AgentSubmissionCommitOutcome {
1120 Committed(StoredAgentSubmission),
1122 Idempotent(StoredAgentSubmission),
1124}
1125
1126impl AgentSubmissionCommitOutcome {
1127 #[must_use]
1129 pub const fn stored(&self) -> &StoredAgentSubmission {
1130 match self {
1131 Self::Committed(stored) | Self::Idempotent(stored) => stored,
1132 }
1133 }
1134}
1135
1136#[derive(Clone, Debug)]
1138#[non_exhaustive]
1139pub enum AppendOutcome {
1140 Committed(JournalEvent),
1142 Idempotent(JournalEvent),
1144}
1145
1146#[derive(Clone, Debug)]
1149#[non_exhaustive]
1150pub enum CheckpointCommitOutcome {
1151 Committed {
1153 event: JournalEvent,
1155 checkpoint: Checkpoint,
1157 },
1158 Idempotent {
1160 event: JournalEvent,
1162 checkpoint: Checkpoint,
1164 },
1165}
1166
1167impl CheckpointCommitOutcome {
1168 #[must_use]
1170 pub const fn event(&self) -> &JournalEvent {
1171 match self {
1172 Self::Committed { event, .. } | Self::Idempotent { event, .. } => event,
1173 }
1174 }
1175
1176 #[must_use]
1178 pub const fn checkpoint(&self) -> &Checkpoint {
1179 match self {
1180 Self::Committed { checkpoint, .. } | Self::Idempotent { checkpoint, .. } => checkpoint,
1181 }
1182 }
1183}
1184
1185#[derive(Clone, Debug)]
1188#[non_exhaustive]
1189pub enum WaitCheckpointCommitOutcome {
1190 Committed {
1192 event: JournalEvent,
1194 checkpoint: Checkpoint,
1196 waits: Vec<DurableWait>,
1198 },
1199 Idempotent {
1201 event: JournalEvent,
1203 checkpoint: Checkpoint,
1205 waits: Vec<DurableWait>,
1207 },
1208}
1209
1210impl WaitCheckpointCommitOutcome {
1211 #[must_use]
1213 pub const fn event(&self) -> &JournalEvent {
1214 match self {
1215 Self::Committed { event, .. } | Self::Idempotent { event, .. } => event,
1216 }
1217 }
1218
1219 #[must_use]
1221 pub const fn checkpoint(&self) -> &Checkpoint {
1222 match self {
1223 Self::Committed { checkpoint, .. } | Self::Idempotent { checkpoint, .. } => checkpoint,
1224 }
1225 }
1226
1227 #[must_use]
1229 pub fn waits(&self) -> &[DurableWait] {
1230 match self {
1231 Self::Committed { waits, .. } | Self::Idempotent { waits, .. } => waits,
1232 }
1233 }
1234}
1235
1236#[derive(Clone, Debug)]
1238#[non_exhaustive]
1239pub enum InterruptResolutionCommitOutcome {
1240 Committed {
1242 event: JournalEvent,
1244 record: InterruptRecord,
1246 },
1247 Idempotent {
1249 event: JournalEvent,
1251 record: InterruptRecord,
1253 },
1254}
1255
1256impl InterruptResolutionCommitOutcome {
1257 #[must_use]
1259 pub const fn event(&self) -> &JournalEvent {
1260 match self {
1261 Self::Committed { event, .. } | Self::Idempotent { event, .. } => event,
1262 }
1263 }
1264
1265 #[must_use]
1267 pub const fn record(&self) -> &InterruptRecord {
1268 match self {
1269 Self::Committed { record, .. } | Self::Idempotent { record, .. } => record,
1270 }
1271 }
1272}
1273
1274#[derive(Clone, Debug)]
1276#[non_exhaustive]
1277pub enum TimerFiringCommitOutcome {
1278 Committed {
1280 event: JournalEvent,
1282 record: DurableTimerRecord,
1284 },
1285 Idempotent {
1287 event: JournalEvent,
1289 record: DurableTimerRecord,
1291 },
1292}
1293
1294impl TimerFiringCommitOutcome {
1295 #[must_use]
1297 pub const fn event(&self) -> &JournalEvent {
1298 match self {
1299 Self::Committed { event, .. } | Self::Idempotent { event, .. } => event,
1300 }
1301 }
1302
1303 #[must_use]
1305 pub const fn record(&self) -> &DurableTimerRecord {
1306 match self {
1307 Self::Committed { record, .. } | Self::Idempotent { record, .. } => record,
1308 }
1309 }
1310}
1311
1312#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, Serialize)]
1314#[serde(rename_all = "snake_case")]
1315#[non_exhaustive]
1316pub enum WaitAbandonmentReason {
1317 RunCancellation,
1319 RunFailure,
1321}
1322
1323#[derive(Clone, Debug, Eq, PartialEq)]
1325pub struct WaitAbandonment {
1326 pub(crate) wait: DurableWait,
1327 pub(crate) reason: WaitAbandonmentReason,
1328 pub(crate) journal: JournalHead,
1329 pub(crate) digest: Digest,
1330}
1331
1332impl WaitAbandonment {
1333 #[must_use]
1335 pub const fn wait(&self) -> &DurableWait {
1336 &self.wait
1337 }
1338
1339 #[must_use]
1341 pub const fn reason(&self) -> WaitAbandonmentReason {
1342 self.reason
1343 }
1344
1345 #[must_use]
1347 pub const fn journal(&self) -> &JournalHead {
1348 &self.journal
1349 }
1350
1351 #[must_use]
1353 pub const fn digest(&self) -> Digest {
1354 self.digest
1355 }
1356}
1357
1358#[derive(Clone, Debug)]
1361#[non_exhaustive]
1362pub enum WaitAbandonmentCommitOutcome {
1363 Committed {
1365 event: JournalEvent,
1367 abandonments: Vec<WaitAbandonment>,
1369 },
1370 Idempotent {
1372 event: JournalEvent,
1374 abandonments: Vec<WaitAbandonment>,
1376 },
1377}
1378
1379impl WaitAbandonmentCommitOutcome {
1380 #[must_use]
1382 pub const fn event(&self) -> &JournalEvent {
1383 match self {
1384 Self::Committed { event, .. } | Self::Idempotent { event, .. } => event,
1385 }
1386 }
1387
1388 #[must_use]
1390 pub fn abandonments(&self) -> &[WaitAbandonment] {
1391 match self {
1392 Self::Committed { abandonments, .. } | Self::Idempotent { abandonments, .. } => {
1393 abandonments
1394 }
1395 }
1396 }
1397}
1398
1399#[derive(Clone, Debug)]
1402#[non_exhaustive]
1403pub enum BarrierCommitOutcome {
1404 Committed {
1407 event: JournalEvent,
1409 checkpoint: Checkpoint,
1411 },
1412 Idempotent {
1414 event: JournalEvent,
1416 checkpoint: Checkpoint,
1418 },
1419}
1420
1421impl BarrierCommitOutcome {
1422 #[must_use]
1424 pub const fn event(&self) -> &JournalEvent {
1425 match self {
1426 Self::Committed { event, .. } | Self::Idempotent { event, .. } => event,
1427 }
1428 }
1429
1430 #[must_use]
1432 pub const fn checkpoint(&self) -> &Checkpoint {
1433 match self {
1434 Self::Committed { checkpoint, .. } | Self::Idempotent { checkpoint, .. } => checkpoint,
1435 }
1436 }
1437}
1438
1439#[derive(Clone, Debug)]
1441#[non_exhaustive]
1442pub enum ToolInvocationCommitOutcome {
1443 Committed {
1445 event: JournalEvent,
1447 invocation: ToolInvocation,
1449 },
1450 Idempotent {
1452 event: JournalEvent,
1454 invocation: ToolInvocation,
1456 },
1457}
1458
1459impl ToolInvocationCommitOutcome {
1460 #[must_use]
1462 pub const fn event(&self) -> &JournalEvent {
1463 match self {
1464 Self::Committed { event, .. } | Self::Idempotent { event, .. } => event,
1465 }
1466 }
1467
1468 #[must_use]
1470 pub const fn invocation(&self) -> &ToolInvocation {
1471 match self {
1472 Self::Committed { invocation, .. } | Self::Idempotent { invocation, .. } => invocation,
1473 }
1474 }
1475}
1476
1477#[derive(Clone, Debug)]
1479#[non_exhaustive]
1480pub enum ModelInvocationCommitOutcome {
1481 Committed {
1483 event: JournalEvent,
1485 invocation: ModelInvocation,
1487 },
1488 Idempotent {
1490 event: JournalEvent,
1492 invocation: ModelInvocation,
1494 },
1495}
1496
1497impl ModelInvocationCommitOutcome {
1498 #[must_use]
1500 pub const fn event(&self) -> &JournalEvent {
1501 match self {
1502 Self::Committed { event, .. } | Self::Idempotent { event, .. } => event,
1503 }
1504 }
1505
1506 #[must_use]
1508 pub const fn invocation(&self) -> &ModelInvocation {
1509 match self {
1510 Self::Committed { invocation, .. } | Self::Idempotent { invocation, .. } => invocation,
1511 }
1512 }
1513}
1514
1515#[derive(Clone, Debug)]
1521#[non_exhaustive]
1522pub enum PendingNodeResultCommitOutcome {
1523 Committed {
1525 event: JournalEvent,
1527 result: PendingNodeResult,
1529 },
1530 Idempotent {
1532 event: JournalEvent,
1534 result: PendingNodeResult,
1536 },
1537}
1538
1539impl PendingNodeResultCommitOutcome {
1540 #[must_use]
1542 pub const fn event(&self) -> &JournalEvent {
1543 match self {
1544 Self::Committed { event, .. } | Self::Idempotent { event, .. } => event,
1545 }
1546 }
1547
1548 #[must_use]
1550 pub const fn result(&self) -> &PendingNodeResult {
1551 match self {
1552 Self::Committed { result, .. } | Self::Idempotent { result, .. } => result,
1553 }
1554 }
1555}
1556
1557#[derive(Clone, Debug)]
1559#[non_exhaustive]
1560pub enum NodeAttemptCommitOutcome {
1561 Committed {
1563 event: JournalEvent,
1565 attempt: NodeAttempt,
1567 },
1568 Idempotent {
1570 event: JournalEvent,
1572 attempt: NodeAttempt,
1574 },
1575}
1576
1577impl NodeAttemptCommitOutcome {
1578 #[must_use]
1580 pub const fn event(&self) -> &JournalEvent {
1581 match self {
1582 Self::Committed { event, .. } | Self::Idempotent { event, .. } => event,
1583 }
1584 }
1585
1586 #[must_use]
1588 pub const fn attempt(&self) -> &NodeAttempt {
1589 match self {
1590 Self::Committed { attempt, .. } | Self::Idempotent { attempt, .. } => attempt,
1591 }
1592 }
1593}
1594
1595#[derive(Clone, Debug, Eq, PartialEq)]
1597pub struct StoredOutboxDestination {
1598 pub(crate) destination: OutboxDestinationRef,
1599 pub(crate) config: JournalPayload,
1600 pub(crate) created_at: Timestamp,
1601}
1602
1603impl StoredOutboxDestination {
1604 #[must_use]
1606 pub const fn destination(&self) -> &OutboxDestinationRef {
1607 &self.destination
1608 }
1609
1610 #[must_use]
1612 pub const fn config(&self) -> &JournalPayload {
1613 &self.config
1614 }
1615
1616 #[must_use]
1618 pub const fn created_at(&self) -> Timestamp {
1619 self.created_at
1620 }
1621}
1622
1623#[derive(Clone, Debug, Eq, PartialEq)]
1625#[non_exhaustive]
1626pub enum OutboxDestinationRegistrationOutcome {
1627 Registered(StoredOutboxDestination),
1629 Idempotent(StoredOutboxDestination),
1631}
1632
1633impl OutboxDestinationRegistrationOutcome {
1634 #[must_use]
1636 pub const fn destination(&self) -> &StoredOutboxDestination {
1637 match self {
1638 Self::Registered(destination) | Self::Idempotent(destination) => destination,
1639 }
1640 }
1641}
1642
1643#[derive(Clone, Debug)]
1645#[non_exhaustive]
1646pub enum OutboxEnqueueOutcome {
1647 Committed {
1649 event: JournalEvent,
1651 deliveries: Vec<OutboxDelivery>,
1653 },
1654 Idempotent {
1656 event: JournalEvent,
1658 deliveries: Vec<OutboxDelivery>,
1660 },
1661}
1662
1663impl OutboxEnqueueOutcome {
1664 #[must_use]
1666 pub const fn event(&self) -> &JournalEvent {
1667 match self {
1668 Self::Committed { event, .. } | Self::Idempotent { event, .. } => event,
1669 }
1670 }
1671
1672 #[must_use]
1674 pub fn deliveries(&self) -> &[OutboxDelivery] {
1675 match self {
1676 Self::Committed { deliveries, .. } | Self::Idempotent { deliveries, .. } => deliveries,
1677 }
1678 }
1679}
1680
1681#[derive(Clone, Debug)]
1683pub struct OutboxClaim {
1684 pub(crate) destination: StoredOutboxDestination,
1685 pub(crate) delivery: OutboxDelivery,
1686 pub(crate) start: OutboxAttemptStart,
1687}
1688
1689impl OutboxClaim {
1690 #[must_use]
1692 pub const fn destination(&self) -> &StoredOutboxDestination {
1693 &self.destination
1694 }
1695
1696 #[must_use]
1698 pub const fn delivery(&self) -> &OutboxDelivery {
1699 &self.delivery
1700 }
1701
1702 #[must_use]
1704 pub const fn start(&self) -> &OutboxAttemptStart {
1705 &self.start
1706 }
1707
1708 #[must_use]
1710 pub const fn fence(&self) -> &DeliveryFence {
1711 self.start.fence()
1712 }
1713}
1714
1715#[derive(Clone, Debug)]
1717#[non_exhaustive]
1718pub enum OutboxClaimOutcome {
1719 Claimed(OutboxClaim),
1721 Idempotent(OutboxClaim),
1723 NoWork,
1725}
1726
1727impl OutboxClaimOutcome {
1728 #[must_use]
1730 pub const fn claim(&self) -> Option<&OutboxClaim> {
1731 match self {
1732 Self::Claimed(claim) | Self::Idempotent(claim) => Some(claim),
1733 Self::NoWork => None,
1734 }
1735 }
1736}
1737
1738#[derive(Clone, Debug)]
1740#[non_exhaustive]
1741pub enum OutboxCompletionOutcome {
1742 Committed {
1744 attempt: OutboxAttempt,
1746 },
1747 Idempotent {
1749 attempt: OutboxAttempt,
1751 },
1752}
1753
1754impl OutboxCompletionOutcome {
1755 #[must_use]
1757 pub const fn attempt(&self) -> &OutboxAttempt {
1758 match self {
1759 Self::Committed { attempt } | Self::Idempotent { attempt } => attempt,
1760 }
1761 }
1762
1763 #[must_use]
1765 pub const fn completion(&self) -> Option<&OutboxAttemptCompletion> {
1766 self.attempt().completion()
1767 }
1768}
1769
1770#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
1772pub struct OutboxAttemptHistoryPageSize(u8);
1773
1774impl OutboxAttemptHistoryPageSize {
1775 pub const MAX: u8 = 16;
1777
1778 pub const fn new(value: u8) -> Result<Self, StoreError> {
1784 if value == 0 || value > Self::MAX {
1785 return Err(StoreError::InvalidOutboxAttemptPageSize);
1786 }
1787 Ok(Self(value))
1788 }
1789
1790 #[must_use]
1792 pub const fn get(self) -> u8 {
1793 self.0
1794 }
1795}
1796
1797#[derive(Clone, Debug)]
1799pub struct OutboxAttemptHistoryPage {
1800 pub(crate) records: Vec<OutboxAttempt>,
1801 pub(crate) has_more: bool,
1802}
1803
1804impl OutboxAttemptHistoryPage {
1805 #[must_use]
1807 pub fn records(&self) -> &[OutboxAttempt] {
1808 &self.records
1809 }
1810
1811 #[must_use]
1813 pub const fn has_more(&self) -> bool {
1814 self.has_more
1815 }
1816
1817 #[must_use]
1819 pub fn next_cursor(&self) -> Option<OutboxAttempt> {
1820 self.records.last().cloned()
1821 }
1822}
1823
1824#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
1826pub struct NodeAttemptHistoryPageSize(u8);
1827
1828impl NodeAttemptHistoryPageSize {
1829 pub const MAX: u8 = 2;
1835
1836 pub const fn new(value: u8) -> Result<Self, StoreError> {
1843 if value == 0 || value > Self::MAX {
1844 return Err(StoreError::InvalidNodeAttemptPageSize);
1845 }
1846 Ok(Self(value))
1847 }
1848
1849 #[must_use]
1851 pub const fn get(self) -> u8 {
1852 self.0
1853 }
1854}
1855
1856#[derive(Clone, Debug)]
1858pub struct NodeAttemptHistoryPage {
1859 pub(crate) records: Vec<NodeAttempt>,
1860 pub(crate) has_more: bool,
1861}
1862
1863impl NodeAttemptHistoryPage {
1864 #[must_use]
1866 pub fn records(&self) -> &[NodeAttempt] {
1867 &self.records
1868 }
1869
1870 #[must_use]
1872 pub const fn has_more(&self) -> bool {
1873 self.has_more
1874 }
1875
1876 #[must_use]
1878 pub fn next_cursor(&self) -> Option<NodeAttempt> {
1879 self.records.last().cloned()
1880 }
1881}
1882
1883#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
1885pub struct PendingNodeResultPageSize(u8);
1886
1887impl PendingNodeResultPageSize {
1888 pub const MAX: u8 = 2;
1895
1896 pub const fn new(value: u8) -> Result<Self, StoreError> {
1903 if value == 0 || value > Self::MAX {
1904 return Err(StoreError::InvalidPendingNodeResultPageSize);
1905 }
1906 Ok(Self(value))
1907 }
1908
1909 #[must_use]
1911 pub const fn get(self) -> u8 {
1912 self.0
1913 }
1914}
1915
1916#[derive(Clone, Debug, Eq, PartialEq)]
1923pub struct PendingNodeResultPageCursor {
1924 pub(crate) base_checkpoint: CheckpointHead,
1925 pub(crate) snapshot_journal_head: JournalHead,
1926 pub(crate) after: PendingNodeResultHead,
1927}
1928
1929impl PendingNodeResultPageCursor {
1930 #[must_use]
1932 pub const fn base_checkpoint(&self) -> &CheckpointHead {
1933 &self.base_checkpoint
1934 }
1935
1936 #[must_use]
1938 pub const fn snapshot_journal_head(&self) -> &JournalHead {
1939 &self.snapshot_journal_head
1940 }
1941
1942 #[must_use]
1944 pub const fn after(&self) -> &PendingNodeResultHead {
1945 &self.after
1946 }
1947}
1948
1949#[derive(Clone, Debug)]
1951pub struct PendingNodeResultPage {
1952 pub(crate) base_checkpoint: CheckpointHead,
1953 pub(crate) snapshot_journal_head: JournalHead,
1954 pub(crate) records: Vec<PendingNodeResult>,
1955 pub(crate) has_more: bool,
1956}
1957
1958impl PendingNodeResultPage {
1959 #[must_use]
1961 pub const fn base_checkpoint(&self) -> &CheckpointHead {
1962 &self.base_checkpoint
1963 }
1964
1965 #[must_use]
1967 pub const fn snapshot_journal_head(&self) -> &JournalHead {
1968 &self.snapshot_journal_head
1969 }
1970
1971 #[must_use]
1973 pub fn records(&self) -> &[PendingNodeResult] {
1974 &self.records
1975 }
1976
1977 #[must_use]
1980 pub fn into_records(self) -> Vec<PendingNodeResult> {
1981 self.records
1982 }
1983
1984 #[must_use]
1986 pub const fn has_more(&self) -> bool {
1987 self.has_more
1988 }
1989
1990 #[must_use]
1992 pub fn next_cursor(&self) -> Option<PendingNodeResultPageCursor> {
1993 self.records
1994 .last()
1995 .map(|result| PendingNodeResultPageCursor {
1996 base_checkpoint: self.base_checkpoint.clone(),
1997 snapshot_journal_head: self.snapshot_journal_head.clone(),
1998 after: result.head(),
1999 })
2000 }
2001}
2002
2003#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
2005pub struct ModelInvocationHistoryPageSize(u8);
2006
2007impl ModelInvocationHistoryPageSize {
2008 pub const MAX: u8 = 1;
2013
2014 pub const fn new(value: u8) -> Result<Self, StoreError> {
2020 if value != Self::MAX {
2021 return Err(StoreError::InvalidModelInvocationPageSize);
2022 }
2023 Ok(Self(value))
2024 }
2025
2026 #[must_use]
2028 pub const fn get(self) -> u8 {
2029 self.0
2030 }
2031}
2032
2033#[derive(Clone, Debug)]
2035pub struct ModelInvocationHistoryPage {
2036 pub(crate) records: Vec<ModelInvocation>,
2037 pub(crate) has_more: bool,
2038}
2039
2040impl ModelInvocationHistoryPage {
2041 #[must_use]
2043 pub fn records(&self) -> &[ModelInvocation] {
2044 &self.records
2045 }
2046
2047 #[must_use]
2049 pub const fn has_more(&self) -> bool {
2050 self.has_more
2051 }
2052
2053 #[must_use]
2055 pub fn next_cursor(&self) -> Option<ModelInvocation> {
2056 self.records.last().cloned()
2057 }
2058}
2059
2060#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
2062pub struct ToolInvocationHistoryPageSize(u8);
2063
2064impl ToolInvocationHistoryPageSize {
2065 pub const MAX: u8 = 2;
2070
2071 pub const fn new(value: u8) -> Result<Self, StoreError> {
2078 if value == 0 || value > Self::MAX {
2079 return Err(StoreError::InvalidToolInvocationPageSize);
2080 }
2081 Ok(Self(value))
2082 }
2083
2084 #[must_use]
2086 pub const fn get(self) -> u8 {
2087 self.0
2088 }
2089}
2090
2091#[derive(Clone, Debug)]
2093pub struct ToolInvocationHistoryPage {
2094 pub(crate) records: Vec<ToolInvocation>,
2095 pub(crate) has_more: bool,
2096}
2097
2098impl ToolInvocationHistoryPage {
2099 #[must_use]
2101 pub fn records(&self) -> &[ToolInvocation] {
2102 &self.records
2103 }
2104
2105 #[must_use]
2107 pub const fn has_more(&self) -> bool {
2108 self.has_more
2109 }
2110
2111 #[must_use]
2113 pub fn next_cursor(&self) -> Option<ToolInvocation> {
2114 self.records.last().cloned()
2115 }
2116}
2117
2118impl AppendOutcome {
2119 #[must_use]
2121 pub const fn event(&self) -> &JournalEvent {
2122 match self {
2123 Self::Committed(event) | Self::Idempotent(event) => event,
2124 }
2125 }
2126}
2127
2128#[allow(clippy::large_enum_variant)]
2130#[derive(Clone, Debug)]
2131#[non_exhaustive]
2132pub enum RunProjection {
2133 Unchanged,
2135 Transition {
2137 expected_revision: RunRevision,
2139 transition: RunTransition,
2141 },
2142}
2143
2144#[derive(Clone, Debug, Eq, PartialEq)]
2148pub struct CheckpointPointer {
2149 pub(crate) checkpoint_id: CheckpointId,
2150 pub(crate) superstep: Superstep,
2151 pub(crate) digest: Digest,
2152}
2153
2154impl CheckpointPointer {
2155 #[must_use]
2157 pub const fn checkpoint_id(&self) -> CheckpointId {
2158 self.checkpoint_id
2159 }
2160
2161 #[must_use]
2163 pub const fn superstep(&self) -> Superstep {
2164 self.superstep
2165 }
2166
2167 #[must_use]
2169 pub const fn digest(&self) -> Digest {
2170 self.digest
2171 }
2172}
2173
2174#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
2176pub struct CheckpointLineagePageSize(u8);
2177
2178impl CheckpointLineagePageSize {
2179 pub const MAX: u8 = 8;
2185
2186 pub const fn new(value: u8) -> Result<Self, StoreError> {
2193 if value == 0 || value > Self::MAX {
2194 return Err(StoreError::InvalidCheckpointPageSize);
2195 }
2196 Ok(Self(value))
2197 }
2198
2199 #[must_use]
2201 pub const fn get(self) -> u8 {
2202 self.0
2203 }
2204}
2205
2206#[derive(Clone, Debug, Eq, PartialEq)]
2208pub struct CheckpointLineagePage {
2209 pub(crate) checkpoints: Vec<Checkpoint>,
2210 pub(crate) next_cursor: Option<CheckpointHead>,
2211}
2212
2213impl CheckpointLineagePage {
2214 #[must_use]
2216 pub fn checkpoints(&self) -> &[Checkpoint] {
2217 &self.checkpoints
2218 }
2219
2220 #[must_use]
2222 pub const fn has_more(&self) -> bool {
2223 self.next_cursor.is_some()
2224 }
2225
2226 #[must_use]
2228 pub fn next_cursor(&self) -> Option<CheckpointHead> {
2229 self.next_cursor.clone()
2230 }
2231}
2232
2233#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
2240pub struct GraphReplayLimits {
2241 maximum_barrier_result_bytes: usize,
2242}
2243
2244impl GraphReplayLimits {
2245 pub const HARD_MAXIMUM_BARRIER_RESULT_BYTES: usize = 512 * 1024 * 1024;
2247
2248 pub const fn new(maximum_barrier_result_bytes: usize) -> Result<Self, StoreError> {
2255 if maximum_barrier_result_bytes == 0
2256 || maximum_barrier_result_bytes > Self::HARD_MAXIMUM_BARRIER_RESULT_BYTES
2257 {
2258 return Err(StoreError::InvalidGraphReplayLimits);
2259 }
2260 Ok(Self {
2261 maximum_barrier_result_bytes,
2262 })
2263 }
2264
2265 #[must_use]
2267 pub const fn maximum_barrier_result_bytes(self) -> usize {
2268 self.maximum_barrier_result_bytes
2269 }
2270}
2271
2272impl Default for GraphReplayLimits {
2273 fn default() -> Self {
2274 Self {
2275 maximum_barrier_result_bytes: 64 * 1024 * 1024,
2276 }
2277 }
2278}
2279
2280#[derive(Clone, Copy, Debug, Eq, PartialEq)]
2282pub struct GraphReplayReport {
2283 pub(crate) checkpoints_validated: u64,
2284 pub(crate) barriers_replayed: u64,
2285 pub(crate) results_replayed: u64,
2286 pub(crate) maximum_barrier_result_bytes: usize,
2287}
2288
2289impl GraphReplayReport {
2290 #[must_use]
2292 pub const fn checkpoints_validated(self) -> u64 {
2293 self.checkpoints_validated
2294 }
2295
2296 #[must_use]
2298 pub const fn barriers_replayed(self) -> u64 {
2299 self.barriers_replayed
2300 }
2301
2302 #[must_use]
2304 pub const fn results_replayed(self) -> u64 {
2305 self.results_replayed
2306 }
2307
2308 #[must_use]
2310 pub const fn maximum_barrier_result_bytes(self) -> usize {
2311 self.maximum_barrier_result_bytes
2312 }
2313}
2314
2315impl RunProjection {
2316 #[must_use]
2318 pub const fn unchanged() -> Self {
2319 Self::Unchanged
2320 }
2321
2322 #[must_use]
2324 pub const fn transition(expected_revision: RunRevision, transition: RunTransition) -> Self {
2325 Self::Transition {
2326 expected_revision,
2327 transition,
2328 }
2329 }
2330}
2331
2332#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
2334pub struct WaitDiscoveryPageSize(u8);
2335
2336impl WaitDiscoveryPageSize {
2337 pub const MAX: u8 = 16;
2339
2340 pub const fn new(value: u8) -> Result<Self, StoreError> {
2346 if value == 0 || value > Self::MAX {
2347 return Err(StoreError::InvalidWaitDiscoveryPageSize);
2348 }
2349 Ok(Self(value))
2350 }
2351
2352 #[must_use]
2354 pub const fn get(self) -> u8 {
2355 self.0
2356 }
2357}
2358
2359#[derive(Clone, Debug, Eq, PartialEq)]
2361pub struct DueTimerPageCursor {
2362 pub(crate) tenant_id: stateknot_core::TenantId,
2363 pub(crate) snapshot_at: Timestamp,
2364 pub(crate) due_at: Timestamp,
2365 pub(crate) run_id: stateknot_core::RunId,
2366 pub(crate) timer_id: stateknot_core::TimerId,
2367}
2368
2369#[derive(Clone, Debug)]
2371pub struct DueTimerPage {
2372 pub(crate) tenant_id: stateknot_core::TenantId,
2373 pub(crate) snapshot_at: Timestamp,
2374 pub(crate) records: Vec<DurableTimer>,
2375 pub(crate) has_more: bool,
2376}
2377
2378impl DueTimerPage {
2379 #[must_use]
2381 pub const fn snapshot_at(&self) -> Timestamp {
2382 self.snapshot_at
2383 }
2384
2385 #[must_use]
2387 pub fn records(&self) -> &[DurableTimer] {
2388 &self.records
2389 }
2390
2391 #[must_use]
2393 pub const fn has_more(&self) -> bool {
2394 self.has_more
2395 }
2396
2397 #[must_use]
2399 pub fn next_cursor(&self) -> Option<DueTimerPageCursor> {
2400 self.records.last().map(|timer| DueTimerPageCursor {
2401 tenant_id: self.tenant_id.clone(),
2402 snapshot_at: self.snapshot_at,
2403 due_at: timer.marker().due_at(),
2404 run_id: timer.intent().run_id(),
2405 timer_id: timer.marker().timer_id(),
2406 })
2407 }
2408}
2409
2410#[derive(Clone, Debug, Eq, PartialEq)]
2412pub struct ExpiredInterruptPageCursor {
2413 pub(crate) tenant_id: stateknot_core::TenantId,
2414 pub(crate) snapshot_at: Timestamp,
2415 pub(crate) expires_at: Timestamp,
2416 pub(crate) run_id: stateknot_core::RunId,
2417 pub(crate) interrupt_id: stateknot_core::InterruptId,
2418}
2419
2420#[derive(Clone, Debug)]
2422pub struct ExpiredInterruptPage {
2423 pub(crate) tenant_id: stateknot_core::TenantId,
2424 pub(crate) snapshot_at: Timestamp,
2425 pub(crate) records: Vec<InterruptRequest>,
2426 pub(crate) has_more: bool,
2427}
2428
2429impl ExpiredInterruptPage {
2430 #[must_use]
2432 pub const fn snapshot_at(&self) -> Timestamp {
2433 self.snapshot_at
2434 }
2435
2436 #[must_use]
2438 pub fn records(&self) -> &[InterruptRequest] {
2439 &self.records
2440 }
2441
2442 #[must_use]
2444 pub const fn has_more(&self) -> bool {
2445 self.has_more
2446 }
2447
2448 #[must_use]
2450 pub fn next_cursor(&self) -> Option<ExpiredInterruptPageCursor> {
2451 let request = self.records.last()?;
2452 Some(ExpiredInterruptPageCursor {
2453 tenant_id: self.tenant_id.clone(),
2454 snapshot_at: self.snapshot_at,
2455 expires_at: request.marker().expires_at()?,
2456 run_id: request.intent().run_id(),
2457 interrupt_id: request.marker().interrupt_id(),
2458 })
2459 }
2460}
2461
2462#[derive(Clone, Debug)]
2464pub struct StoredRun {
2465 pub(crate) lifecycle: RunLifecycle,
2466 pub(crate) journal_head: Option<JournalHead>,
2467 pub(crate) lease: Option<RunLease>,
2468 pub(crate) last_fencing_epoch: Option<FencingEpoch>,
2469 pub(crate) checkpoint: Option<CheckpointPointer>,
2470 pub(crate) scheduler_ready_at: Option<stateknot_core::Timestamp>,
2471 pub(crate) scheduler_not_before: Option<stateknot_core::Timestamp>,
2472 pub(crate) wait_set_digest: Option<Digest>,
2473 pub(crate) unresolved_wait_count: u8,
2474 pub(crate) next_timer_due_at: Option<Timestamp>,
2475 pub(crate) next_interrupt_expiry_at: Option<Timestamp>,
2476 pub(crate) quarantined: bool,
2477}
2478
2479impl StoredRun {
2480 #[must_use]
2482 pub const fn lifecycle(&self) -> &RunLifecycle {
2483 &self.lifecycle
2484 }
2485
2486 #[must_use]
2488 pub const fn journal_head(&self) -> Option<&JournalHead> {
2489 self.journal_head.as_ref()
2490 }
2491
2492 #[must_use]
2494 pub const fn lease(&self) -> Option<&RunLease> {
2495 self.lease.as_ref()
2496 }
2497
2498 #[must_use]
2500 pub const fn last_fencing_epoch(&self) -> Option<FencingEpoch> {
2501 self.last_fencing_epoch
2502 }
2503
2504 #[must_use]
2507 pub const fn checkpoint(&self) -> Option<&CheckpointPointer> {
2508 self.checkpoint.as_ref()
2509 }
2510
2511 #[must_use]
2516 pub const fn scheduler_ready_at(&self) -> Option<stateknot_core::Timestamp> {
2517 self.scheduler_ready_at
2518 }
2519
2520 #[must_use]
2526 pub const fn scheduler_not_before(&self) -> Option<stateknot_core::Timestamp> {
2527 self.scheduler_not_before
2528 }
2529
2530 #[must_use]
2532 pub const fn wait_set_digest(&self) -> Option<Digest> {
2533 self.wait_set_digest
2534 }
2535
2536 #[must_use]
2538 pub const fn unresolved_wait_count(&self) -> u8 {
2539 self.unresolved_wait_count
2540 }
2541
2542 #[must_use]
2544 pub const fn next_timer_due_at(&self) -> Option<Timestamp> {
2545 self.next_timer_due_at
2546 }
2547
2548 #[must_use]
2550 pub const fn next_interrupt_expiry_at(&self) -> Option<Timestamp> {
2551 self.next_interrupt_expiry_at
2552 }
2553
2554 #[must_use]
2556 pub const fn is_quarantined(&self) -> bool {
2557 self.quarantined
2558 }
2559}
2560
2561#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
2563pub struct RunnableRunPageSize(u8);
2564
2565impl RunnableRunPageSize {
2566 pub const MAX: u8 = 16;
2571
2572 pub const fn new(value: u8) -> Result<Self, StoreError> {
2579 if value == 0 || value > Self::MAX {
2580 return Err(StoreError::InvalidRunnableRunPageSize);
2581 }
2582 Ok(Self(value))
2583 }
2584
2585 #[must_use]
2587 pub const fn get(self) -> u8 {
2588 self.0
2589 }
2590}
2591
2592#[derive(Clone, Debug)]
2594pub struct RunnableRunCandidate {
2595 pub(crate) run: StoredRun,
2596 pub(crate) ready_at: stateknot_core::Timestamp,
2597 pub(crate) available_at: stateknot_core::Timestamp,
2598}
2599
2600impl RunnableRunCandidate {
2601 #[must_use]
2603 pub const fn run(&self) -> &StoredRun {
2604 &self.run
2605 }
2606
2607 #[must_use]
2609 pub const fn ready_at(&self) -> stateknot_core::Timestamp {
2610 self.ready_at
2611 }
2612
2613 #[must_use]
2615 pub const fn available_at(&self) -> stateknot_core::Timestamp {
2616 self.available_at
2617 }
2618}
2619
2620#[derive(Clone, Debug, Eq, PartialEq)]
2622pub struct RunnableRunPageCursor {
2623 pub(crate) tenant_id: stateknot_core::TenantId,
2624 pub(crate) snapshot_at: stateknot_core::Timestamp,
2625 pub(crate) available_at: stateknot_core::Timestamp,
2626 pub(crate) run_id: stateknot_core::RunId,
2627}
2628
2629#[derive(Clone, Debug)]
2631pub struct RunnableRunPage {
2632 pub(crate) tenant_id: stateknot_core::TenantId,
2633 pub(crate) snapshot_at: stateknot_core::Timestamp,
2634 pub(crate) records: Vec<RunnableRunCandidate>,
2635 pub(crate) has_more: bool,
2636}
2637
2638impl RunnableRunPage {
2639 #[must_use]
2641 pub const fn snapshot_at(&self) -> stateknot_core::Timestamp {
2642 self.snapshot_at
2643 }
2644
2645 #[must_use]
2647 pub fn records(&self) -> &[RunnableRunCandidate] {
2648 &self.records
2649 }
2650
2651 #[must_use]
2653 pub const fn has_more(&self) -> bool {
2654 self.has_more
2655 }
2656
2657 #[must_use]
2659 pub fn next_cursor(&self) -> Option<RunnableRunPageCursor> {
2660 self.records.last().map(|candidate| RunnableRunPageCursor {
2661 tenant_id: self.tenant_id.clone(),
2662 snapshot_at: self.snapshot_at,
2663 available_at: candidate.available_at,
2664 run_id: candidate.run.lifecycle().provenance().run_id(),
2665 })
2666 }
2667}
2668
2669#[derive(Clone, Debug, Eq, PartialEq)]
2671#[non_exhaustive]
2672pub enum LeaseRenewalOutcome {
2673 Renewed(RunLease),
2675 Idempotent(RunLease),
2677}
2678
2679#[derive(Clone, Debug, Eq, PartialEq)]
2686pub struct LiveLeaseObservation {
2687 pub(crate) lease: RunLease,
2688 pub(crate) observed_at: Timestamp,
2689}
2690
2691impl LiveLeaseObservation {
2692 #[must_use]
2694 pub const fn lease(&self) -> &RunLease {
2695 &self.lease
2696 }
2697
2698 #[must_use]
2700 pub const fn observed_at(&self) -> Timestamp {
2701 self.observed_at
2702 }
2703
2704 #[must_use]
2706 pub fn into_parts(self) -> (RunLease, Timestamp) {
2707 (self.lease, self.observed_at)
2708 }
2709}
2710
2711#[derive(Clone, Debug, Eq, PartialEq)]
2713#[non_exhaustive]
2714pub enum LeaseClaimOutcome {
2715 Claimed(RunLease),
2717 Idempotent(RunLease),
2719}
2720
2721impl LeaseClaimOutcome {
2722 #[must_use]
2724 pub const fn lease(&self) -> &RunLease {
2725 match self {
2726 Self::Claimed(lease) | Self::Idempotent(lease) => lease,
2727 }
2728 }
2729}
2730
2731impl LeaseRenewalOutcome {
2732 #[must_use]
2734 pub const fn lease(&self) -> &RunLease {
2735 match self {
2736 Self::Renewed(lease) | Self::Idempotent(lease) => lease,
2737 }
2738 }
2739}
2740
2741#[derive(Clone, Copy, Debug, Eq, PartialEq)]
2743#[non_exhaustive]
2744pub enum LeaseReleaseOutcome {
2745 Released,
2747 Idempotent,
2749}
2750
2751#[derive(Clone, Copy, Debug, Eq, PartialEq)]
2753#[non_exhaustive]
2754pub enum DelayedRetryScheduleOutcome {
2755 Scheduled {
2757 not_before: Timestamp,
2759 },
2760 Idempotent {
2762 not_before: Timestamp,
2764 },
2765 Due {
2770 not_before: Timestamp,
2772 },
2773}
2774
2775impl DelayedRetryScheduleOutcome {
2776 #[must_use]
2778 pub const fn not_before(self) -> Timestamp {
2779 match self {
2780 Self::Scheduled { not_before }
2781 | Self::Idempotent { not_before }
2782 | Self::Due { not_before } => not_before,
2783 }
2784 }
2785}
2786
2787#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
2789pub struct JournalPageSize(u16);
2790
2791impl JournalPageSize {
2792 pub const MAX: u16 = 1_000;
2794
2795 pub const fn new(value: u16) -> Result<Self, StoreError> {
2801 if value == 0 || value > Self::MAX {
2802 return Err(StoreError::InvalidPageSize);
2803 }
2804 Ok(Self(value))
2805 }
2806
2807 #[must_use]
2809 pub const fn get(self) -> u16 {
2810 self.0
2811 }
2812}
2813
2814#[derive(Clone, Debug, Eq, PartialEq)]
2816pub struct JournalPage {
2817 pub(crate) events: Vec<JournalEvent>,
2818 pub(crate) has_more: bool,
2819}
2820
2821impl JournalPage {
2822 #[must_use]
2824 pub fn events(&self) -> &[JournalEvent] {
2825 &self.events
2826 }
2827
2828 #[must_use]
2830 pub const fn has_more(&self) -> bool {
2831 self.has_more
2832 }
2833
2834 #[must_use]
2836 pub fn next_cursor(&self) -> Option<JournalHead> {
2837 self.events.last().map(JournalEvent::head)
2838 }
2839}