Skip to main content

appcore_core/
runtime.rs

1// =============================================================================
2//        #######
3//     ###       ###     F: runtime.rs
4//    ##   ## ##   ##    P: AppCore-Runtime
5//         ## ##
6//                       C: 2026/05/29 20:47:35 by dnettoRaw
7//    ##   ## ##   ##    U: 2026/07/24 16:07:49 by dnettoRaw
8//      ###########      S: 1.0.1-rc.8
9// =============================================================================
10
11//! Minimal immutable runtime instance built from runtime builder contracts.
12
13use crate::audit::AuditLog;
14use crate::bus::CommandBus;
15use crate::command::CommandRegistry;
16use crate::context::RuntimeContext;
17use crate::decision::{DecisionEngine, DecisionOutcome, DecisionRegistry};
18use crate::envelope::CommandEnvelope;
19use crate::error::RuntimeResult;
20use crate::event::EventRegistry;
21use crate::event_bus::EventBus;
22use crate::handler::CommandResult;
23use crate::identity::RuntimeIdentity;
24use crate::lifecycle::RuntimeLifecycle;
25use crate::state::StateRegistry;
26use appcore_contracts::ApplicationManifestV1;
27
28/// Built runtime instance with manifest and registries.
29#[derive(Debug)]
30pub struct RuntimeInstance {
31    pub(crate) application_manifest: ApplicationManifestV1,
32    pub(crate) identity: RuntimeIdentity,
33    pub(crate) command_registry: CommandRegistry,
34    pub(crate) event_registry: EventRegistry,
35    pub(crate) state_registry: StateRegistry,
36    pub(crate) decision_registry: DecisionRegistry,
37    pub(crate) decision_engine: DecisionEngine,
38    pub(crate) command_bus: CommandBus,
39    pub(crate) event_bus: EventBus,
40    pub(crate) audit_log: AuditLog,
41    pub(crate) lifecycle: RuntimeLifecycle,
42}
43
44impl RuntimeInstance {
45    /// Returns the application-owned V1 manifest.
46    pub fn application_manifest(&self) -> &ApplicationManifestV1 {
47        &self.application_manifest
48    }
49
50    /// Returns Runtime/application identity.
51    pub fn identity(&self) -> &RuntimeIdentity {
52        &self.identity
53    }
54
55    /// Returns declared commands.
56    pub fn commands(&self) -> &CommandRegistry {
57        &self.command_registry
58    }
59
60    /// Returns declared events.
61    pub fn events(&self) -> &EventRegistry {
62        &self.event_registry
63    }
64
65    /// Returns declared states.
66    pub fn states(&self) -> &StateRegistry {
67        &self.state_registry
68    }
69
70    /// Returns declared decision nodes.
71    pub fn decisions(&self) -> &DecisionRegistry {
72        &self.decision_registry
73    }
74
75    /// Returns the command bus.
76    pub fn command_bus(&self) -> &CommandBus {
77        &self.command_bus
78    }
79
80    /// Returns the process lifecycle.
81    pub fn lifecycle(&self) -> &RuntimeLifecycle {
82        &self.lifecycle
83    }
84
85    /// Returns recently emitted events.
86    pub fn event_bus(&self) -> &EventBus {
87        &self.event_bus
88    }
89
90    /// Returns the process-local audit log.
91    pub fn audit_log(&self) -> &AuditLog {
92        &self.audit_log
93    }
94
95    /// Evaluates policy and dispatches one command.
96    pub fn dispatch_command(
97        &self,
98        command: &CommandEnvelope,
99        context: &dyn RuntimeContext,
100    ) -> RuntimeResult<CommandResult> {
101        match self.decision_engine.evaluate(command, context)? {
102            DecisionOutcome::Allow => self.command_bus.dispatch(command, context),
103            DecisionOutcome::Deny(message) => Ok(CommandResult::rejected(message)),
104            DecisionOutcome::Defer(message) => Ok(CommandResult::rejected(message)),
105        }
106    }
107
108    /// Verifies compatibility with another Runtime identity.
109    pub fn ensure_compatible(&self, other: &RuntimeIdentity) -> RuntimeResult<()> {
110        self.identity().ensure_compatible(other)
111    }
112}