Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

feat(core): add event bus for publishing and subscribing to core events

Steven Enamakelc0fc4607ccdd
diff --git a/crates/openhuman-core/src/core/bus.rs b/crates/openhuman-core/src/core/bus.rs
index 0948d1279c..3cb5370064 100644
--- a/crates/openhuman-core/src/core/bus.rs
+++ b/crates/openhuman-core/src/core/bus.rs
@@ -87,7 +87,9 @@ pub const EVENTS_INTERFACE: &str = "ai.tinyhumans.openhuman.Events";
/// `1.9.0` adds an optional `agent_id` to `ApprovalRequested`,
/// `ApprovalDecided` and `FlowApprovalRequested` (additive).
/// `1.10.0` removes the first-run harness initialization progress events.
-pub const EVENTS_VERSION: Version = Version::new(1, 10, 0);
+/// `1.11.0` removes `FlowScheduleTick`: the cron scheduler fires a flow's
+/// schedule trigger through the schedule runtime's `TriggerSink` instead.
+pub const EVENTS_VERSION: Version = Version::new(1, 11, 0);
/// The bus. Initialised once by [`init`]; safe to touch before that.
pub static BUS: OnceBus<DomainEvent> = OnceBus::new();
diff --git a/crates/openhuman-core/src/core/events.rs b/crates/openhuman-core/src/core/events.rs
index 00868600ed..530de68b57 100644
--- a/crates/openhuman-core/src/core/events.rs
+++ b/crates/openhuman-core/src/core/events.rs
@@ -397,17 +397,6 @@ pub enum DomainEvent {
/// The job name, e.g. `memory_context_refresh`.
job: String,
},
- /// A `flow`-type cron job fired its schedule tick (issue B2,
- /// `my_docs/ohxtf/b2-triggers-trust/01-triggers-and-trust.md` §1).
- /// Published by `cron::scheduler` when a `JobType::Flow` job comes due;
- /// carries only the flow id (the trigger payload for a `schedule` flow is
- /// always empty — `flows::ops::flows_run` seeds `{}`). Consumed by
- /// `flows::bus::FlowTriggerSubscriber`, which loads the flow, checks it is
- /// still enabled with a `schedule` trigger, and spawns a run.
- FlowScheduleTick {
- /// Identifier of the `flows::Flow` to run.
- flow_id: String,
- },
/// Live per-step progress of an in-flight `flows_run` / `flows_resume`
/// (issue G2, live run observation). Published from
/// `flows::observability::FlowRunObserver::on_step_finish` as each
@@ -1372,7 +1361,6 @@ impl DomainEvent {
| Self::CronJobCompleted { .. }
| Self::CronDeliveryRequested { .. }
| Self::ProactiveMessageRequested { .. }
- | Self::FlowScheduleTick { .. }
| Self::CronSystemJobDue { .. }
| Self::FlowRunProgress { .. }
| Self::FlowRunStarted { .. }
@@ -1505,7 +1493,6 @@ impl DomainEvent {
Self::CronJobCompleted { .. } => "CronJobCompleted",
Self::CronDeliveryRequested { .. } => "CronDeliveryRequested",
Self::ProactiveMessageRequested { .. } => "ProactiveMessageRequested",
- Self::FlowScheduleTick { .. } => "FlowScheduleTick",
Self::CronSystemJobDue { .. } => "CronSystemJobDue",
Self::FlowRunProgress { .. } => "FlowRunProgress",
Self::FlowRunStarted { .. } => "FlowRunStarted",
diff --git a/crates/openhuman-core/src/core/events_tests.rs b/crates/openhuman-core/src/core/events_tests.rs
index 87845498e3..1a7a14139c 100644
--- a/crates/openhuman-core/src/core/events_tests.rs
+++ b/crates/openhuman-core/src/core/events_tests.rs
@@ -218,12 +218,6 @@ fn all_variants_have_correct_domain() {
},
"cron",
),
- (
- DomainEvent::FlowScheduleTick {
- flow_id: "flow-1".into(),
- },
- "cron",
- ),
// Workflow
(
DomainEvent::WorkflowLoaded {
diff --git a/crates/openhuman-core/src/cron/scheduler/run_delivery.rs b/crates/openhuman-core/src/cron/scheduler/run_delivery.rs
index c40b1745b7..d6bb3efed7 100644
--- a/crates/openhuman-core/src/cron/scheduler/run_delivery.rs
+++ b/crates/openhuman-core/src/cron/scheduler/run_delivery.rs
@@ -73,11 +73,18 @@ impl RunDelivery for HostDelivery {
pub(super) struct HostTriggers {
binding: Binding,
+ /// Holds the per-flow in-flight dedupe across ticks.
+ #[cfg(feature = "flows")]
+ flows: std::sync::OnceLock<crate::flows::bus::FlowTriggerSubscriber>,
}
impl HostTriggers {
pub(super) fn new(binding: Binding) -> Self {
- Self { binding }
+ Self {
+ binding,
+ #[cfg(feature = "flows")]
+ flows: std::sync::OnceLock::new(),
+ }
}
}
@@ -102,7 +109,14 @@ impl TriggerSink for HostTriggers {
"[cron] flow schedule trigger fired"
);
#[cfg(feature = "flows")]
- crate::flows::bus::dispatch_schedule_tick(&self.binding.config(), flow_id).await;
+ self.flows
+ .get_or_init(|| {
+ crate::flows::bus::FlowTriggerSubscriber::new(std::sync::Arc::new(
+ self.binding.config(),
+ ))
+ })
+ .dispatch_schedule_tick(flow_id)
+ .await;
#[cfg(not(feature = "flows"))]
tracing::warn!(%flow_id, "[cron] built without `flows`; schedule trigger dropped");
Ok(Attempt {
diff --git a/crates/openhuman-core/src/flows/bus/trigger.rs b/crates/openhuman-core/src/flows/bus/trigger.rs
index bd2328ef12..e48a4f1d00 100644
--- a/crates/openhuman-core/src/flows/bus/trigger.rs
+++ b/crates/openhuman-core/src/flows/bus/trigger.rs
@@ -116,10 +116,16 @@ impl FlowTriggerSubscriber {
super::owner::config_for_scope(&self.config)
}
- /// `DomainEvent::FlowScheduleTick` — a `flow`-type cron job fired. Loads
- /// the one named flow, checks it is still enabled with a `schedule`
- /// trigger (it may have been disabled/edited since the job was
- /// registered), and dispatches it with an empty trigger payload.
+ /// A `flow`-type cron job fired (`cron::scheduler`'s trigger sink calls
+ /// this directly). Runs as the agent the flow belongs to
+ /// (`super::owner`), then loads the one named flow, checks it is still
+ /// enabled with a `schedule` trigger (it may have been disabled/edited
+ /// since the job was registered), and dispatches it with an empty trigger
+ /// payload.
+ pub(crate) async fn dispatch_schedule_tick(&self, flow_id: &str) {
+ super::owner::as_owner(&self.config, flow_id, self.handle_schedule_tick(flow_id)).await
+ }
+
async fn handle_schedule_tick(&self, flow_id: &str) {
let config = self.config_for_scope();
let flow = match store::get_flow(&config, flow_id) {
@@ -255,11 +261,6 @@ impl EventHandler<DomainEvent> for FlowTriggerSubscriber {
async fn handle(&self, event: &DomainEvent) {
match event {
- // The flow runs as the agent it belongs to (`super::owner`).
- DomainEvent::FlowScheduleTick { flow_id } => {
- super::owner::as_owner(&self.config, flow_id, self.handle_schedule_tick(flow_id))
- .await
- }
// Every scope's flows may listen for this trigger: match them in
// each, so each run starts as its flow's owner.
DomainEvent::ComposioTriggerReceived {
diff --git a/crates/openhuman-core/src/flows/bus_subscriber_tests.rs b/crates/openhuman-core/src/flows/bus_subscriber_tests.rs
index 6b420053a2..33aa0c542a 100644
--- a/crates/openhuman-core/src/flows/bus_subscriber_tests.rs
+++ b/crates/openhuman-core/src/flows/bus_subscriber_tests.rs
@@ -65,10 +65,7 @@ async fn handle_does_not_panic_on_arbitrary_events() {
job_type: "shell".into(),
})
.await;
- sub.handle(&DomainEvent::FlowScheduleTick {
- flow_id: "missing-flow".into(),
- })
- .await;
+ sub.dispatch_schedule_tick("missing-flow").await;
}
#[test]
@@ -163,10 +160,7 @@ async fn handle_schedule_tick_ignores_disabled_flow() {
// Must not panic and must not spawn a run for a disabled flow — we
// can't directly observe "no run happened" without a full flows_run
// fixture, but this exercises the early-return path without error.
- sub.handle(&DomainEvent::FlowScheduleTick {
- flow_id: "sched-flow".into(),
- })
- .await;
+ sub.dispatch_schedule_tick("sched-flow").await;
}
// ── in-flight dedupe (CodeRabbit finding B) ─────────────────────