Skip to main content

OrcsEngine

Struct OrcsEngine 

Source
pub struct OrcsEngine { /* private fields */ }
Expand description

OrcsEngine - Main runtime for ORCS CLI.

Coordinates parallel execution of ChannelRunners and manages the World (channel hierarchy).

§Parallel Execution Model

Engine operates in parallel mode with:

  • WorldManager for concurrent World access
  • ChannelRunner per channel
  • Event injection via channel handles
  • Snapshot collection via graceful runner shutdown

§Example

use orcs_runtime::{World, ChannelConfig};

// Create World with IO channel
let mut world = World::new();
let io = world.create_channel(ChannelConfig::interactive());

// Inject into engine with IO channel (required)
let engine = OrcsEngine::new(world, io);

// Spawn channel runners with bound components
engine.spawn_runner(io, Box::new(MyComponent::new()));

// Run the engine (parallel execution)
engine.run().await;

§Signal Priority

Signals are broadcast to all Runners. A Veto signal immediately stops the engine without processing further.

Implementations§

Source§

impl OrcsEngine

Source

pub fn new(world: World, io_channel: ChannelId) -> Self

Create new engine with injected World and IO channel.

The World is immediately transferred to a WorldManager which starts processing commands. The IO channel is required for Human input/output.

§Arguments
  • world - The World containing the IO channel
  • io_channel - The IO channel ID (must exist in World)
§Example
let mut world = World::new();
let io = world.create_channel(ChannelConfig::interactive());

let engine = OrcsEngine::new(world, io);
Source

pub fn set_mcp_manager(&mut self, manager: Arc<McpClientManager>)

Sets the shared MCP client manager for all spawned runners.

Must be called before spawning runners to ensure they receive the manager.

Source

pub fn set_hook_registry(&mut self, registry: SharedHookRegistry)

Sets the shared hook registry for all spawned runners.

Must be called before spawning runners to ensure they receive the registry.

Source

pub fn hook_registry(&self) -> Option<&SharedHookRegistry>

Returns a reference to the hook registry, if configured.

Source

pub fn spawn_runner( &mut self, channel_id: ChannelId, component: Box<dyn Component>, ) -> ChannelHandle

Spawn a channel runner for a channel with a bound Component.

Returns the channel handle for event injection.

§Arguments
  • channel_id - The channel to run
  • component - The Component to bind (1:1 relationship)
Source

pub fn spawn_client_runner( &mut self, channel_id: ChannelId, io_port: IOPort, principal: Principal, ) -> (ChannelHandle, OutputSender)

Spawn a channel runner with an EventEmitter injected into the Component.

This enables IO-less (event-only) execution for Components that support the Emitter trait. The Component can emit output via emit_output().

Use this for ChannelRunner-based execution instead of ClientRunner. Spawn a ClientRunner for an IO channel (no component).

ClientRunner is dedicated to Human I/O bridging. It broadcasts UserInput events to all channels and displays Output events from other channels.

§Arguments
  • channel_id - The channel to run
  • io_port - IO port for View communication
  • principal - Principal for signal creation from IO input
§Returns

A tuple of (ChannelHandle, event_tx) where event_tx can be used by other runners to send Output events to this runner for display.

Source

pub async fn spawn_channel( &mut self, parent: ChannelId, config: ChannelConfig, component: Box<dyn Component>, ) -> Option<ChannelId>

Spawn a child channel with configuration and bound Component.

Creates a new channel in World and spawns its runner.

§Arguments
  • parent - Parent channel ID
  • config - Channel configuration
  • component - Component to bind to the new channel (1:1)
Source

pub async fn spawn_channel_with_auth( &mut self, parent: ChannelId, config: ChannelConfig, component: Box<dyn Component>, session: Arc<Session>, checker: Arc<dyn PermissionChecker>, ) -> Option<ChannelId>

Spawn a child channel with authentication/authorization.

Creates a new channel in World and spawns its runner with Session and PermissionChecker configured.

§Authorization Check

Before spawning, checks if the session is allowed to spawn children using the provided checker. Returns None if unauthorized.

§Arguments
  • parent - Parent channel ID
  • config - Channel configuration
  • component - Component to bind to the new channel (1:1)
  • session - Session for permission checking (Arc for shared grants)
  • checker - Permission checker policy
§Returns
  • Some(ChannelId) if spawn succeeded
  • None if parent doesn’t exist or permission denied
Source

pub fn spawn_runner_with_auth( &mut self, channel_id: ChannelId, component: Box<dyn Component>, session: Arc<Session>, checker: Arc<dyn PermissionChecker>, ) -> ChannelHandle

Spawn a runner with Session and PermissionChecker.

The Session and Checker are passed to the ChildContext for command permission checking.

§Arguments
  • channel_id - The channel to run
  • component - The Component to bind
  • session - Session for permission checking
  • checker - Permission checker policy
Source

pub fn spawn_runner_full_auth( &mut self, channel_id: ChannelId, component: Box<dyn Component>, output_tx: Option<OutputSender>, lua_loader: Option<Arc<dyn LuaChildLoader>>, component_loader: Option<Arc<dyn ComponentLoader>>, session: Arc<Session>, checker: Arc<dyn PermissionChecker>, grants: Arc<dyn GrantPolicy>, ) -> ChannelHandle

Spawn a ChannelRunner with all options including auth.

This is the most complete spawn method that supports:

  • Event emission (signal broadcasting)
  • Output routing to IO channel
  • Child spawning via LuaChildLoader
  • Component loading for spawn_runner_from_script
  • Session-based permission checking
§Arguments
  • channel_id - The channel to run
  • component - The Component to bind
  • output_tx - Optional channel for Output event routing
  • lua_loader - Optional loader for spawning Lua children
  • component_loader - Optional loader for spawning Components as runners
  • session - Session for permission checking
  • checker - Permission checker policy
§Returns

A handle for injecting Events into the Channel.

Source

pub fn spawn_runner_full_auth_with_snapshot( &mut self, channel_id: ChannelId, component: Box<dyn Component>, output_tx: Option<OutputSender>, lua_loader: Option<Arc<dyn LuaChildLoader>>, component_loader: Option<Arc<dyn ComponentLoader>>, session: Arc<Session>, checker: Arc<dyn PermissionChecker>, grants: Arc<dyn GrantPolicy>, initial_snapshot: Option<ComponentSnapshot>, component_config: Value, ) -> ChannelHandle

Spawns a ChannelRunner with full auth and an optional initial snapshot.

Same as Self::spawn_runner_full_auth but accepts an optional ComponentSnapshot to restore before init() (session resume).

Source

pub fn world_read(&self) -> &Arc<RwLock<World>>

Returns the read-only World handle for parallel access.

Source

pub fn world_tx(&self) -> &WorldCommandSender

Returns the world command sender.

Source

pub fn inject_event( &self, channel_id: ChannelId, event: Event, ) -> Result<(), EngineError>

Injects an event to a specific channel (targeted delivery).

Unlike signal broadcast, this sends to a single channel only. Used for @component message routing.

§Errors

Returns EngineError::ChannelNotFound if channel doesn’t exist, or EngineError::SendFailed if the buffer is full.

Source

pub fn signal(&self, signal: Signal)

Send signal (from external, e.g., human input)

Broadcasts to all ChannelRunners via signal_tx.

Source

pub fn is_running(&self) -> bool

Check if engine is running

Source

pub fn start(&mut self)

Start the engine (set running flag) and spawn the runner monitor.

Use this when you need to start the engine without entering the run loop, for example in interactive mode where you control the polling yourself.

Source

pub fn stop(&self)

Stop the engine by sending a Veto signal.

This triggers graceful shutdown via the signal broadcast channel. Call shutdown() after this to await runner completion and collect snapshots.

Source

pub async fn shutdown(&mut self)

Awaits all runners, collects snapshots, and cleans up.

Must be called after stop() to complete the shutdown sequence. Snapshots are available via collected_snapshots() after this returns.

Source

pub fn board(&self) -> &SharedBoard

Returns the shared Board handle.

External code (e.g., Lua API registration) can use this to query the Board for recent events.

Source

pub fn io_channel(&self) -> ChannelId

Returns the IO channel ID.

The IO channel is required and always available.

Source

pub async fn run(&mut self)

Main run loop with parallel execution.

This method:

  1. Waits for stop signal (running flag set to false)
  2. Shuts down all runners on exit
§Note

Caller must spawn runners via spawn_runner() before calling run(). Each runner requires a bound Component (1:1 relationship).

§Example
let mut engine = OrcsEngine::new(world);
engine.spawn_runner(io_id, Box::new(MyComponent::new()));
engine.run().await;
Source

pub fn collected_snapshots(&self) -> &HashMap<String, ComponentSnapshot>

Returns a reference to snapshots collected during graceful shutdown.

Populated by shutdown_parallel() when runners complete their shutdown sequence. Keyed by component FQN.

§Usage

Call this after run() completes to retrieve snapshots for session persistence.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more