Skip to content

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

feat(cron): track job owner for completion notifications

Steven Enamakel403445daa292
diff --git a/crates/openhuman-core/src/cron/completion_owner.rs b/crates/openhuman-core/src/cron/completion_owner.rs
new file mode 100644
index 0000000000..b4417432aa
--- /dev/null
+++ b/crates/openhuman-core/src/cron/completion_owner.rs
@@ -0,0 +1,49 @@
+//! Which agent a just-completed cron job belonged to.
+//!
+//! `CronJobCompleted` names only the job, and a one-shot job
+//! (`delete_after_run`) is already gone from the store by the time a
+//! subscriber sees the event, so its owner cannot be looked up there. The
+//! scheduler publishes the event inside the job's own task, which carries the
+//! agent's context, and notes the owner here first; the notification bridge
+//! takes it back out (`desktop::notifications`).
+//!
+//! In-process only — the bus delivers locally — and bounded: if no subscriber
+//! drains it (the bridge is not registered), it is cleared rather than grown.
+
+use std::collections::HashMap;
+use std::sync::{LazyLock, Mutex};
+
+/// Notes kept before the map is cleared outright.
+const CAPACITY: usize = 1024;
+
+/// Job id → the agent it ran as.
+static OWNERS: LazyLock<Mutex<HashMap<String, String>>> = LazyLock::new(Default::default);
+
+/// Notes that `job_id` just completed as the current context's agent, when
+/// it ran as one. Call just before publishing `CronJobCompleted`.
+pub fn note(job_id: &str) {
+ let Some(agent) = crate::core::runtime::CoreContext::current()
+ .and_then(|context| context.session_agent().map(str::to_string))
+ else {
+ return;
+ };
+ let mut owners = OWNERS
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner);
+ if owners.len() >= CAPACITY {
+ owners.clear();
+ }
+ owners.insert(job_id.to_string(), agent);
+}
+
+/// The agent `job_id` completed as, if one was noted; removes the note.
+pub fn take(job_id: &str) -> Option<String> {
+ OWNERS
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner)
+ .remove(job_id)
+}
+
+#[cfg(test)]
+#[path = "completion_owner_tests.rs"]
+mod tests;
diff --git a/crates/openhuman-core/src/cron/completion_owner_tests.rs b/crates/openhuman-core/src/cron/completion_owner_tests.rs
new file mode 100644
index 0000000000..bc7d01b38a
--- /dev/null
+++ b/crates/openhuman-core/src/cron/completion_owner_tests.rs
@@ -0,0 +1,26 @@
+use super::*;
+use crate::core::runtime::{ContextOverlay, CoreContext, DomainSet};
+
+#[tokio::test]
+async fn a_job_completed_as_an_agent_is_noted_once() {
+ let agent = CoreContext::for_test(DomainSet::full(), None).derive_with(
+ ContextOverlay::new(
+ crate::config::Config::default(),
+ DomainSet::full(),
+ Default::default(),
+ )
+ .session_agent("completion-owner-agent"),
+ );
+ CoreContext::scope(agent, async { note("completion-owner-job") }).await;
+ assert_eq!(
+ take("completion-owner-job").as_deref(),
+ Some("completion-owner-agent")
+ );
+ assert_eq!(take("completion-owner-job"), None, "taken once");
+}
+
+#[test]
+fn a_job_completed_without_an_agent_is_not_noted() {
+ note("completion-owner-local");
+ assert_eq!(take("completion-owner-local"), None);
+}
diff --git a/crates/openhuman-core/src/cron/mod.rs b/crates/openhuman-core/src/cron/mod.rs
index a84a6f84e4..aff93a72f0 100644
--- a/crates/openhuman-core/src/cron/mod.rs
+++ b/crates/openhuman-core/src/cron/mod.rs
@@ -11,6 +11,7 @@ pub mod scheduler_gate;
pub mod bus;
pub mod channel_bridge;
+pub mod completion_owner;
pub mod job_builder;
pub mod ops;
pub mod origin;
diff --git a/crates/openhuman-core/src/cron/scheduler.rs b/crates/openhuman-core/src/cron/scheduler.rs
index 8ef1cba300..5c64479da1 100644
--- a/crates/openhuman-core/src/cron/scheduler.rs
+++ b/crates/openhuman-core/src/cron/scheduler.rs
@@ -263,6 +263,9 @@ pub(super) async fn execute_and_persist_job(
)
.await;
+ // The job ran as its agent (when it had one); a one-shot job is already
+ // deleted, so subscribers learn the owner here, not from the store.
+ crate::cron::completion_owner::note(&job.id);
BUS.publish(DomainEvent::CronJobCompleted {
job_id: job.id.clone(),
success,
diff --git a/crates/openhuman-core/src/desktop/notifications/bus.rs b/crates/openhuman-core/src/desktop/notifications/bus.rs
index 4fc100db80..c81d7c82c2 100644
--- a/crates/openhuman-core/src/desktop/notifications/bus.rs
+++ b/crates/openhuman-core/src/desktop/notifications/bus.rs
@@ -477,8 +477,15 @@ async fn event_owner(config: &crate::config::Config, event: &DomainEvent) -> Opt
let DomainEvent::CronJobCompleted { job_id, .. } = event else {
return None;
};
+ // Noted by the scheduler as the job completed — the only record of a
+ // one-shot job, which is deleted before the event is published.
+ if let Some(agent) = crate::cron::completion_owner::take(job_id) {
+ return Some(agent);
+ }
// No backend: every record is `local`.
crate::storage::installed()?;
+ // Found in `local` (`Some(None)`) and found nowhere (`None`) both mean
+ // the notification is stored in `local`.
crate::storage::agents::find_owner("notification owner", || async {
crate::cron::get_job(config, job_id).is_ok()
})