EventLoop

Struct EventLoop 

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

The main event loop orchestrator.

Implementations§

Source§

impl EventLoop

Source

pub fn new(config: RalphConfig) -> Self

Creates a new event loop from configuration.

Source

pub fn state(&self) -> &LoopState

Returns the current loop state.

Source

pub fn config(&self) -> &RalphConfig

Returns the configuration.

Source

pub fn registry(&self) -> &HatRegistry

Returns the hat registry.

Source

pub fn get_hat_backend(&self, hat_id: &HatId) -> Option<&HatBackend>

Gets the backend configuration for a hat.

If the hat has a backend configured, returns that. Otherwise, returns None (caller should use global backend).

Source

pub fn add_observer<F>(&mut self, observer: F)
where F: Fn(&Event) + Send + 'static,

Adds an observer that receives all published events.

Multiple observers can be added (e.g., session recorder + TUI). Each observer is called before events are routed to subscribers.

Source

pub fn set_observer<F>(&mut self, observer: F)
where F: Fn(&Event) + Send + 'static,

👎Deprecated since 2.0.0: Use add_observer instead

Sets a single observer, clearing any existing observers.

Prefer add_observer when multiple observers are needed.

Source

pub fn check_termination(&self) -> Option<TerminationReason>

Checks if any termination condition is met.

Source

pub fn initialize(&mut self, prompt_content: &str)

Initializes the loop by publishing the start event.

Source

pub fn initialize_resume(&mut self, prompt_content: &str)

Initializes the loop for resume mode by publishing task.resume.

Per spec: “User can run ralph resume to restart reading existing scratchpad.” The planner should read the existing scratchpad rather than doing fresh gap analysis.

Source

pub fn next_hat(&self) -> Option<&HatId>

Gets the next hat to execute (if any have pending events).

Per “Hatless Ralph” architecture: When custom hats are defined, Ralph is always the executor. Custom hats define topology (pub/sub contracts) that Ralph uses for coordination context, but Ralph handles all iterations.

  • Solo mode (no custom hats): Returns “ralph” if Ralph has pending events
  • Multi-hat mode (custom hats defined): Always returns “ralph” if ANY hat has pending events
Source

pub fn has_pending_events(&self) -> bool

Checks if any hats have pending events.

Use this after process_output to detect if the LLM failed to publish an event. If false after processing, the loop will terminate on the next iteration.

Source

pub fn get_hat_publishes(&self, hat_id: &HatId) -> Vec<String>

Gets the topics a hat is allowed to publish.

Used to build retry prompts when the LLM forgets to publish an event.

Source

pub fn inject_fallback_event(&mut self) -> bool

Injects a fallback event to recover from a stalled loop.

When no hats have pending events (agent failed to publish), this method injects a task.resume event which Ralph will handle to attempt recovery.

Returns true if a fallback event was injected, false if recovery is not possible.

Source

pub fn build_prompt(&mut self, hat_id: &HatId) -> Option<String>

Builds the prompt for a hat’s execution.

Per “Hatless Ralph” architecture:

  • Solo mode: Ralph handles everything with his own prompt
  • Multi-hat mode: Ralph is the sole executor, custom hats define topology only

When in multi-hat mode, this method collects ALL pending events across all hats and builds Ralph’s prompt with that context. The ## HATS section in Ralph’s prompt documents the topology for coordination awareness.

Source

pub fn build_ralph_prompt(&self, prompt_content: &str) -> String

Builds the Ralph prompt (coordination mode).

Source

pub fn get_active_hat_id(&self) -> HatId

Returns the primary active hat ID for display purposes. Returns the first active hat, or “ralph” if no specific hat is active.

Source

pub fn record_event_count(&mut self) -> usize

Records the current event count before hat execution.

Call this before executing a hat, then use check_default_publishes after execution to inject a fallback event if needed.

Source

pub fn check_default_publishes(&mut self, hat_id: &HatId, _events_before: usize)

Checks if hat wrote any events, and injects default if configured.

Call this after hat execution with the count from record_event_count. If no new events were written AND the hat has default_publishes configured, this will inject the default event automatically.

Source

pub fn process_output( &mut self, hat_id: &HatId, output: &str, success: bool, ) -> Option<TerminationReason>

Processes output from a hat execution.

Returns the termination reason if the loop should stop.

Source

pub fn add_cost(&mut self, cost: f64)

Adds cost to the cumulative total.

Source

pub fn process_events_from_jsonl(&mut self) -> Result<bool>

Processes events from JSONL and routes orphaned events to Ralph.

Also handles backpressure for malformed JSONL lines by:

  1. Emitting event.malformed system events for each parse failure
  2. Tracking consecutive failures for termination check
  3. Resetting counter when valid events are parsed

Returns true if Ralph should be invoked to handle orphaned events.

Source

pub fn check_ralph_completion(&self, output: &str) -> bool

Checks if output contains LOOP_COMPLETE from Ralph.

Only Ralph can trigger loop completion. Hat outputs are ignored.

Source

pub fn publish_terminate_event(&mut self, reason: &TerminationReason) -> Event

Publishes the loop.terminate system event to observers.

Per spec: “Published by the orchestrator (not agents) when the loop exits.” This is an observer-only event—hats cannot trigger on it.

Returns the event for logging purposes.

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