Skip to main content

bcx_model/
event.rs

1use bcx_core::{CapabilityRef, EventId, PolicyEpoch, ValidationError};
2
3/// Relationship between a BCX event and one of its parents.
4#[derive(Clone, Copy, Debug, Eq, PartialEq)]
5pub enum RelationshipKind {
6    /// The parent directly caused this event.
7    CausedBy,
8    /// The event was delegated by the parent.
9    DelegatedFrom,
10    /// The event retries the parent.
11    RetryOf,
12    /// The event was scheduled by the parent.
13    ScheduledBy,
14    /// The event was derived from the parent.
15    DerivedFrom,
16    /// The event joins several parent branches.
17    JoinedFrom,
18}
19
20/// Observable cause class for an operation.
21#[derive(Clone, Copy, Debug, Eq, PartialEq)]
22pub enum CauseKind {
23    /// An external network request entered the local trust boundary.
24    ExternalRequest,
25    /// A runtime or client attested an explicit user action.
26    ExplicitUserAction,
27    /// Application code initiated the operation.
28    ApplicationAction,
29    /// A service call was delegated by another participant.
30    DelegatedServiceCall,
31    /// A timer or schedule initiated the operation.
32    Timer,
33    /// A queue message initiated the operation.
34    QueueMessage,
35    /// The operation is a retry.
36    Retry,
37    /// An administrator initiated the operation.
38    Administrator,
39    /// An autonomous agent initiated the operation.
40    AutonomousAgent,
41}
42
43/// High-level operation action.
44#[derive(Clone, Copy, Debug, Eq, PartialEq)]
45pub enum OperationAction {
46    /// Read data without modifying authoritative state.
47    Read,
48    /// Create a new object or state transition.
49    Create,
50    /// Update existing state.
51    Update,
52    /// Delete or tombstone existing state.
53    Delete,
54    /// Derive an output from one or more inputs.
55    Derive,
56    /// Execute a component or tool.
57    Execute,
58    /// Transfer data or authority across a boundary.
59    Transfer,
60    /// Subscribe to future updates.
61    Subscribe,
62    /// Publish an event.
63    Publish,
64}
65
66/// Admission decision produced before execution.
67#[derive(Clone, Copy, Debug, Eq, PartialEq)]
68pub enum AdmissionResult {
69    /// The operation may continue as requested.
70    Allow,
71    /// The operation is denied.
72    Deny,
73    /// The operation may continue only with a narrower scope.
74    Narrow,
75    /// The operation requires stronger approval.
76    RequireApproval,
77    /// The operation is quarantined for later review.
78    Quarantine,
79}
80
81/// Execution result recorded after an operation attempt.
82#[derive(Clone, Copy, Debug, Eq, PartialEq)]
83pub enum EffectResult {
84    /// The operation completed.
85    Completed,
86    /// The operation partially completed.
87    Partial,
88    /// The executor rejected the operation.
89    Rejected,
90    /// Execution failed.
91    Failed,
92    /// Execution was cancelled.
93    Cancelled,
94    /// Execution timed out.
95    TimedOut,
96}
97
98/// Compact event capsule for causal parentage.
99#[derive(Clone, Copy, Debug, Eq, PartialEq)]
100pub struct CauseCapsule<'a> {
101    /// Local event identifier.
102    pub event_id: EventId,
103    /// Parent event identifiers.
104    pub parents: &'a [EventId],
105    /// Relationship used for every parent in this compact capsule.
106    pub relationship: RelationshipKind,
107    /// Observable cause class.
108    pub cause_kind: CauseKind,
109    /// Requested action.
110    pub action: OperationAction,
111    /// Optional authority reference.
112    pub authority: Option<CapabilityRef>,
113    /// Optional policy epoch reference.
114    pub policy_epoch: Option<PolicyEpoch>,
115}
116
117impl CauseCapsule<'_> {
118    /// Validates bounded capsule shape.
119    pub const fn validate(&self, maximum_parents: usize) -> Result<(), ValidationError> {
120        if self.parents.len() > maximum_parents {
121            Err(ValidationError::TooLarge)
122        } else {
123            Ok(())
124        }
125    }
126}