Skip to content

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

fix(modules): report cached terminal failures once

Steven Enamakel8d76236ff504
diff --git a/crates/openhuman-core/src/modules/client.rs b/crates/openhuman-core/src/modules/client.rs
index ba3402c1dc..2d537db5aa 100644
--- a/crates/openhuman-core/src/modules/client.rs
+++ b/crates/openhuman-core/src/modules/client.rs
@@ -23,8 +23,8 @@ impl std::fmt::Display for ModuleCallError {
Self::TransportFailed => "transport failed",
Self::ModuleFault => "execution failed",
};
- // Later product reporting recognises this as already reported module
- // unavailability, rather than producing another terminal Sentry event.
+ // Later product reporting recognises this as an already reported
+ // module failure, rather than producing a second terminal event.
write!(formatter, "MODULE_CALL_REPORTED: module {reason}")
}
}
@@ -129,7 +129,13 @@ async fn invoke_proxy<R: DeserializeOwned>(
};
result.map_err(|error| {
let (outcome, reason) = classify(&error);
- failure::report(record, reason);
+ if reason == failure::Reason::ModuleUnavailable {
+ // TinyBus emits this stable error for a module whose terminal
+ // lifecycle state is cached for the rest of the process.
+ failure::report(record, reason);
+ } else {
+ failure::report_invocation(record, reason);
+ }
outcome
})
}
@@ -138,6 +144,10 @@ async fn invoke_proxy<R: DeserializeOwned>(
fn classify(error: &tinybus::Error) -> (ModuleCallError, failure::Reason) {
use tinybus::Error;
match error {
+ Error::ModuleUnavailable { .. } => (
+ ModuleCallError::Unavailable,
+ failure::Reason::ModuleUnavailable,
+ ),
Error::IncompatibleVersion { .. }
| Error::UnknownMethod { .. }
| Error::UnknownInterface { .. }
@@ -146,6 +156,14 @@ fn classify(error: &tinybus::Error) -> (ModuleCallError, failure::Reason) {
ModuleCallError::IncompatibleContract,
failure::Reason::IncompatibleContract,
),
+ Error::MethodFailed { name, .. }
+ if name == "ai.tinyhumans.tinybus.Error.ModuleUnavailable" =>
+ {
+ (
+ ModuleCallError::Unavailable,
+ failure::Reason::ModuleUnavailable,
+ )
+ }
Error::MethodFailed { name, .. }
if matches!(
name.as_str(),
diff --git a/crates/openhuman-core/src/modules/client_tests.rs b/crates/openhuman-core/src/modules/client_tests.rs
index b5f4d08f52..453f1a2535 100644
--- a/crates/openhuman-core/src/modules/client_tests.rs
+++ b/crates/openhuman-core/src/modules/client_tests.rs
@@ -102,6 +102,133 @@ impl BusFixture {
message: "secret-value /home/private-user/payload".to_string(),
})
}
+
+ async fn unavailable(&self) -> tinybus::Result<()> {
+ Err(tinybus::Error::ModuleUnavailable {
+ module: "test.ModuleClient".to_string(),
+ state: "faulted".to_string(),
+ detail: "module stopped accepting calls".to_string(),
+ })
+ }
+}
+
+#[cfg(all(feature = "modules", feature = "crash-reporting"))]
+#[test]
+fn independent_invocation_faults_report_separately_around_a_success() {
+ use tinybus::{broker::Broker, transport::memory::MemoryBus, Connection, ObjectPath};
+ let runtime = tokio::runtime::Builder::new_current_thread()
+ .enable_all()
+ .build()
+ .unwrap();
+ let proxy = runtime.block_on(async {
+ let bus = MemoryBus::new();
+ Broker::new().spawn(bus.clone());
+ let service = Connection::connect(bus.connect().await.unwrap())
+ .await
+ .unwrap();
+ let caller = Connection::connect(bus.connect().await.unwrap())
+ .await
+ .unwrap();
+ service
+ .serve_at(
+ ObjectPath::new("/test/ModuleClient").unwrap(),
+ BusFixture(std::sync::Arc::new(std::sync::atomic::AtomicUsize::new(0))),
+ )
+ .await
+ .unwrap();
+ service.request_name("test.ModuleClient").await.unwrap();
+ let proxy = caller
+ .proxy(
+ "test.ModuleClient",
+ "/test/ModuleClient",
+ "test.ModuleClient",
+ )
+ .unwrap();
+ (proxy, service)
+ });
+ let (proxy, _service) = proxy;
+ let record = registry::find("tinysearch").unwrap();
+ let events = sentry::test::with_captured_events(|| {
+ runtime.block_on(async {
+ assert_eq!(
+ invoke_proxy::<()>(record, &proxy, "Fault", (), false).await,
+ Err(ModuleCallError::ModuleFault)
+ );
+ assert_eq!(
+ invoke_proxy::<u32>(record, &proxy, "Sum", (20, 22), false).await,
+ Ok(42)
+ );
+ assert_eq!(
+ invoke_proxy::<()>(record, &proxy, "Fault", (), false).await,
+ Err(ModuleCallError::ModuleFault)
+ );
+ });
+ });
+
+ assert_eq!(events.len(), 2, "{events:?}");
+ for event in &events {
+ assert_eq!(
+ event.tags.get("reason_code").map(String::as_str),
+ Some("module_fault")
+ );
+ let captured = format!("{event:?}");
+ assert!(!captured.contains("secret-value"));
+ assert!(!captured.contains("/home/private-user"));
+ }
+}
+
+#[cfg(all(feature = "modules", feature = "crash-reporting"))]
+#[test]
+fn cached_native_module_unavailability_is_reported_once() {
+ use tinybus::{broker::Broker, transport::memory::MemoryBus, Connection, ObjectPath};
+ let runtime = tokio::runtime::Builder::new_current_thread()
+ .enable_all()
+ .build()
+ .unwrap();
+ let proxy = runtime.block_on(async {
+ let bus = MemoryBus::new();
+ Broker::new().spawn(bus.clone());
+ let service = Connection::connect(bus.connect().await.unwrap())
+ .await
+ .unwrap();
+ let caller = Connection::connect(bus.connect().await.unwrap())
+ .await
+ .unwrap();
+ service
+ .serve_at(
+ ObjectPath::new("/test/ModuleClient").unwrap(),
+ BusFixture(std::sync::Arc::new(std::sync::atomic::AtomicUsize::new(0))),
+ )
+ .await
+ .unwrap();
+ service.request_name("test.ModuleClient").await.unwrap();
+ let proxy = caller
+ .proxy(
+ "test.ModuleClient",
+ "/test/ModuleClient",
+ "test.ModuleClient",
+ )
+ .unwrap();
+ (proxy, service)
+ });
+ let (proxy, _service) = proxy;
+ let record = registry::find("tinybox").unwrap();
+ let events = sentry::test::with_captured_events(|| {
+ runtime.block_on(async {
+ for _ in 0..2 {
+ assert_eq!(
+ invoke_proxy::<()>(record, &proxy, "Unavailable", (), false).await,
+ Err(ModuleCallError::Unavailable)
+ );
+ }
+ });
+ });
+
+ assert_eq!(events.len(), 1, "{events:?}");
+ assert_eq!(
+ events[0].tags.get("reason_code").map(String::as_str),
+ Some("module_unavailable")
+ );
}
#[cfg(feature = "modules")]
diff --git a/crates/openhuman-core/src/modules/failure.rs b/crates/openhuman-core/src/modules/failure.rs
index 524e273351..bf78481a19 100644
--- a/crates/openhuman-core/src/modules/failure.rs
+++ b/crates/openhuman-core/src/modules/failure.rs
@@ -1,4 +1,4 @@
-//! Sanitized terminal module reports, deduplicated for the process lifetime.
+//! Sanitized terminal module reports with deduplication for cached outcomes.
use std::collections::HashSet;
use std::sync::{Mutex, OnceLock};
@@ -15,6 +15,7 @@ pub(super) enum Reason {
IncompatibleContract,
TransportFailed,
ModuleFault,
+ ModuleUnavailable,
}
impl Reason {
@@ -27,6 +28,7 @@ impl Reason {
Self::IncompatibleContract => "incompatible_contract",
Self::TransportFailed => "transport_failed",
Self::ModuleFault => "module_fault",
+ Self::ModuleUnavailable => "module_unavailable",
}
}
@@ -38,6 +40,7 @@ impl Reason {
Self::IncompatibleContract => "contract",
Self::TransportFailed => "transport",
Self::ModuleFault => "execution",
+ Self::ModuleUnavailable => "execution",
}
}
}
@@ -47,15 +50,21 @@ impl Reason {
/// There are finitely many registry records and reasons, so the process cache
/// is bounded. Concurrent callers cannot duplicate the same terminal report.
pub(super) fn report(record: &'static ModuleRecord, reason: Reason) {
- report_metadata(record.id, record.version, reason);
+ report_metadata(record.id, record.version, reason, true);
+}
+
+/// Report a failure from this invocation. Independent executions can fail
+/// with the same sanitized reason and still represent distinct terminal events.
+pub(super) fn report_invocation(record: &'static ModuleRecord, reason: Reason) {
+ report_metadata(record.id, record.version, reason, false);
}
/// Unknown identifiers never become event metadata or deduplication keys.
pub(super) fn report_unknown_module() {
- report_metadata("unregistered", "unknown", Reason::UnknownModule);
+ report_metadata("unregistered", "unknown", Reason::UnknownModule, true);
}
-fn report_metadata(module: &'static str, version: &'static str, reason: Reason) {
+fn report_metadata(module: &'static str, version: &'static str, reason: Reason, deduplicate: bool) {
static REPORTED: OnceLock<Mutex<HashSet<(&'static str, Reason)>>> = OnceLock::new();
let platform = format!("{}-{}", std::env::consts::OS, std::env::consts::ARCH);
let capture = || {
@@ -78,7 +87,7 @@ fn report_metadata(module: &'static str, version: &'static str, reason: Reason)
let retain_key = sentry::Hub::current().client().is_some();
#[cfg(not(feature = "crash-reporting"))]
let retain_key = true;
- if retain_key {
+ if retain_key && deduplicate {
let first = REPORTED
.get_or_init(|| Mutex::new(HashSet::new()))
.lock()
diff --git a/crates/openhuman-core/src/modules/failure_tests.rs b/crates/openhuman-core/src/modules/failure_tests.rs
index 4162216994..9aa4632f2e 100644
--- a/crates/openhuman-core/src/modules/failure_tests.rs
+++ b/crates/openhuman-core/src/modules/failure_tests.rs
@@ -58,6 +58,7 @@ fn reason_codes_and_stages_are_closed_vocabulary() {
Reason::IncompatibleContract,
Reason::TransportFailed,
Reason::ModuleFault,
+ Reason::ModuleUnavailable,
] {
assert!(reason
.code()
diff --git a/crates/openhuman-core/src/modules/ops.rs b/crates/openhuman-core/src/modules/ops.rs
index 960dfec423..a4f7da77eb 100644
--- a/crates/openhuman-core/src/modules/ops.rs
+++ b/crates/openhuman-core/src/modules/ops.rs
@@ -162,7 +162,13 @@ pub async fn ensure_loaded_within(
let receiver = match resolution::global().claim(id) {
Claim::Done(Resolution::Ready) => return Ok(()),
- Claim::Done(Resolution::Failed(reason)) => return Err(LoadError::Failed(reason)),
+ Claim::Done(Resolution::Failed(reason)) => {
+ // The original resolution may have completed before a Sentry
+ // client was bound. Revisit reporting at the actual cached caller
+ // boundary; the report key suppresses duplicates once captured.
+ report_resolution_failure(id, &reason);
+ return Err(LoadError::Failed(reason));
+ }
Claim::Wait(receiver) => receiver,
Claim::Run { sender, receiver } => {
start_resolution(config.clone(), record, sender).await;
@@ -171,7 +177,10 @@ pub async fn ensure_loaded_within(
};
match resolution::global().wait(id, receiver, within).await {
Waited::Ready => Ok(()),
- Waited::Failed(reason) => Err(LoadError::Failed(reason)),
+ Waited::Failed(reason) => {
+ report_resolution_failure(id, &reason);
+ Err(LoadError::Failed(reason))
+ }
Waited::StillLoading => Err(LoadError::StillLoading),
}
}
@@ -232,10 +241,11 @@ async fn start_resolution(
///
/// A resolution runs once and its failure is cached for every later caller
/// (see the module docs), so this is the single Sentry event a broken install
-/// produces. Those callers re-raise the cached reason, and those re-reports
-/// classify as `ExpectedErrorKind::ModuleUnavailable` and are demoted; this
-/// one goes through [`report_error`] directly so the classifier cannot swallow
-/// it too.
+/// produces. The failed resolution itself reports here, and cached callers
+/// revisit this function in case the original resolution happened before a
+/// Sentry client was bound. Its stable report key suppresses repeats. The
+/// report goes through [`report_error`] directly so the observability
+/// classifier cannot swallow the original terminal event.
///
/// [`report_error`]: crate::core::observability::report_error
pub(super) fn report_resolution_failure(id: &str, reason: &str) {
diff --git a/crates/openhuman-core/src/modules/ops_tests.rs b/crates/openhuman-core/src/modules/ops_tests.rs
index 4ad2f9c283..9dfeae4be9 100644
--- a/crates/openhuman-core/src/modules/ops_tests.rs
+++ b/crates/openhuman-core/src/modules/ops_tests.rs
@@ -276,6 +276,86 @@ async fn a_bounded_wait_with_nothing_cached_and_downloads_off_fails_rather_than_
}
}
+#[cfg(feature = "crash-reporting")]
+#[test]
+fn terminal_resolution_cached_failure_is_reported_when_a_client_becomes_available() {
+ const MODULE: &str = "tinycomputer";
+ let table = tinybus::module::resolution::global();
+ table.forget(MODULE);
+
+ // Model resolution finishing before core/Sentry startup, then publish its
+ // terminal outcome into the same process-wide table read by ensure_loaded.
+ let hub = std::sync::Arc::new(sentry::Hub::new_from_top(sentry::Hub::current()));
+ hub.bind_client(None);
+ sentry::Hub::run(hub, || {
+ ops::report_resolution_failure(MODULE, "private token /home/private-user/module.so");
+ });
+ let sender = table.mark_in_flight(MODULE);
+ table.complete(
+ MODULE,
+ tinybus::module::resolution::Resolution::Failed(
+ "private token /home/private-user/module.so".to_string(),
+ ),
+ sender,
+ );
+
+ let runtime = tokio::runtime::Builder::new_current_thread()
+ .enable_all()
+ .build()
+ .unwrap();
+ let events = sentry::test::with_captured_events(|| {
+ runtime.block_on(async {
+ let config = Config::default();
+ let (first, second) = tokio::join!(
+ ops::ensure_loaded_within(&config, MODULE, None),
+ ops::ensure_loaded_within(&config, MODULE, None),
+ );
+ for result in [first, second] {
+ assert!(matches!(result, Err(ops::LoadError::Failed(_))));
+ }
+ let repeated = ops::ensure_loaded_within(&config, MODULE, None)
+ .await
+ .expect_err("the same cached failure remains terminal");
+ assert!(matches!(repeated, ops::LoadError::Failed(_)));
+ });
+ });
+ table.forget(MODULE);
+ assert_eq!(events.len(), 1, "{events:?}");
+ let captured = format!("{:?}", events[0]);
+ assert!(!captured.contains("private token"));
+ assert!(!captured.contains("/home/private-user"));
+ assert_eq!(
+ events[0].tags.get("reason_code").map(String::as_str),
+ Some("resolution_failed")
+ );
+}
+
+#[cfg(feature = "crash-reporting")]
+#[test]
+fn terminal_resolution_wait_timeout_is_not_reported_as_terminal() {
+ const MODULE: &str = "tinyvoice";
+ let table = tinybus::module::resolution::global();
+ table.forget(MODULE);
+ let _sender = table.mark_in_flight(MODULE);
+ let runtime = tokio::runtime::Builder::new_current_thread()
+ .enable_all()
+ .build()
+ .unwrap();
+ let events = sentry::test::with_captured_events(|| {
+ runtime.block_on(async {
+ let result = ops::ensure_loaded_within(
+ &Config::default(),
+ MODULE,
+ Some(std::time::Duration::ZERO),
+ )
+ .await;
+ assert!(matches!(result, Err(ops::LoadError::StillLoading)));
+ });
+ });
+ table.forget(MODULE);
+ assert!(events.is_empty(), "{events:?}");
+}
+
#[test]
fn every_shipped_registry_entry_names_a_cache_directory() {
// Every shipped registry entry names a directory on every host it claims.