Skip to content

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

test(cron): cover idempotency keys and origin gating

Steven Enamakel8f46c6c3aafd
diff --git a/crates/openhuman-core/src/cron/channel_bridge.rs b/crates/openhuman-core/src/cron/channel_bridge.rs
index ac4040117c..a2aa3ffd3b 100644
--- a/crates/openhuman-core/src/cron/channel_bridge.rs
+++ b/crates/openhuman-core/src/cron/channel_bridge.rs
@@ -117,8 +117,7 @@ pub async fn send_to_channel(
// Run-specific key: a retry after an ambiguous send must not duplicate the
// reminder, while identical reminders from different runs must not collide
// (the content-derived default key would reuse one key for both).
- let mut message =
- SendMessage::new(text, reply_target).in_thread(thread_id.map(str::to_string));
+ let mut message = SendMessage::new(text, reply_target).in_thread(thread_id.map(str::to_string));
message.idempotency_key = Some(idempotency_key.to_string());
ch.send(&message)
.await
diff --git a/crates/openhuman-core/src/cron/channel_bridge_tests.rs b/crates/openhuman-core/src/cron/channel_bridge_tests.rs
index 8a91e7af62..c697d29ce4 100644
--- a/crates/openhuman-core/src/cron/channel_bridge_tests.rs
+++ b/crates/openhuman-core/src/cron/channel_bridge_tests.rs
@@ -16,7 +16,11 @@ impl Channel for MockChannel {
}
async fn send(&self, message: &SendMessage) -> anyhow::Result<()> {
self.sends.fetch_add(1, Ordering::SeqCst);
- *self.last_target.lock().unwrap() = message.recipient.clone();
+ *self.last_target.lock().unwrap() = format!(
+ "{}|{}",
+ message.recipient,
+ message.idempotency_key.clone().unwrap_or_default()
+ );
Ok(())
}
async fn listen(&self, _tx: mpsc::Sender<ChannelMessage>) -> anyhow::Result<()> {
@@ -60,8 +64,16 @@ async fn bridge_reads_appends_trims_and_sends() {
assert_eq!(got.last().unwrap().1, "again");
assert_eq!(got[0].1, "ok");
- send_to_channel("Telegram", "42", None, "hi").await.unwrap();
+ send_to_channel("Telegram", "42", None, "hi", "cron:j:r1")
+ .await
+ .unwrap();
assert_eq!(sends.load(Ordering::SeqCst), 1);
- assert_eq!(*last_target.lock().unwrap(), "42");
- assert!(send_to_channel("slack", "42", None, "hi").await.is_err());
+ assert_eq!(
+ *last_target.lock().unwrap(),
+ "42|cron:j:r1",
+ "the send carries the run-specific idempotency key"
+ );
+ assert!(send_to_channel("slack", "42", None, "hi", "k")
+ .await
+ .is_err());
}
diff --git a/crates/openhuman-core/src/cron/origin_tests.rs b/crates/openhuman-core/src/cron/origin_tests.rs
index 9b4985bbcd..f79b059370 100644
--- a/crates/openhuman-core/src/cron/origin_tests.rs
+++ b/crates/openhuman-core/src/cron/origin_tests.rs
@@ -243,3 +243,26 @@ async fn tools_report_external_effect_from_the_live_turn() {
assert!(add_shell, "shell job stays gated");
assert!(!via_cron, "the collapsed `cron` tool agrees");
}
+
+#[test]
+fn announce_to_own_chat_keeps_the_gate_unless_the_origin_is_retained() {
+ // `create_agent_job` drops the origin for a non-`current` session target
+ // with an announce delivery, so that job would run as TrustedAutomation.
+ for target in ["isolated", "main", "session:x"] {
+ let args = json!({
+ "prompt": "p",
+ "session_target": target,
+ "delivery": {"mode": "announce", "channel": "telegram", "to": "42"}
+ });
+ assert!(
+ !is_self_scoped_agent_job(&args, Some(&ch())),
+ "{target} must stay gated"
+ );
+ }
+ let current = json!({
+ "prompt": "p",
+ "session_target": "current",
+ "delivery": {"mode": "announce", "channel": "telegram", "to": "42"}
+ });
+ assert!(is_self_scoped_agent_job(&current, Some(&ch())));
+}
diff --git a/crates/openhuman-core/src/cron/scheduler/origin_context_tests.rs b/crates/openhuman-core/src/cron/scheduler/origin_context_tests.rs
index 9f7a4afa54..a75d50acd0 100644
--- a/crates/openhuman-core/src/cron/scheduler/origin_context_tests.rs
+++ b/crates/openhuman-core/src/cron/scheduler/origin_context_tests.rs
@@ -30,11 +30,7 @@ fn tail_is_bounded_by_characters_and_keeps_the_newest() {
m("assistant", &long),
];
let tail = bounded_tail(&msgs, 10, 1400);
- assert!(
- tail.chars().count() <= 1400 + 1,
- "len {}",
- tail.chars().count()
- );
+ assert!(tail.chars().count() <= 1400, "len {}", tail.chars().count());
assert!(tail.ends_with(&"x".repeat(100)));
assert!(tail.lines().last().unwrap().starts_with("assistant: "));
assert!(!tail.contains("old one"));
diff --git a/crates/openhuman-core/src/cron/scheduler/origin_delivery.rs b/crates/openhuman-core/src/cron/scheduler/origin_delivery.rs
index 29b87fddfc..1995f2133f 100644
--- a/crates/openhuman-core/src/cron/scheduler/origin_delivery.rs
+++ b/crates/openhuman-core/src/cron/scheduler/origin_delivery.rs
@@ -164,7 +164,7 @@ pub(crate) async fn deliver_to_origin(
&origin_request_id(&job.id, run_id),
)
.await
- .map_err(|e| anyhow!(e))?;
+ .map_err(|e| anyhow!(e))?;
channel_bridge::append_assistant_message(history_key, &text);
tracing::debug!(
job_id = %job.id,