Skip to content

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

test(embedding): move cloud embedding tests to tinyhumans

Steven Enamakel13804650a17c
diff --git a/crates/openhuman-core/src/inference/embedding_host/cloud_adapter_tests.rs b/crates/openhuman-core/src/inference/embedding_host/cloud_adapter_tests.rs
index 5ef5276437..c448a46c37 100644
--- a/crates/openhuman-core/src/inference/embedding_host/cloud_adapter_tests.rs
+++ b/crates/openhuman-core/src/inference/embedding_host/cloud_adapter_tests.rs
@@ -1,25 +1,19 @@
use super::*;
-/// Privacy epic S7 (#4441): under LocalOnly, `embed` refuses before touching
-/// the inner cloud transport (so no network / no auth is needed to observe
-/// the block). Uses the thread-scoped privacy override so it never mutates
-/// the process-global policy that sibling tests read.
+/// With no embedding provider registered, the managed embedder keeps its
+/// identity and answers with the `BACKEND_UNAVAILABLE:` sentinel on use.
#[tokio::test]
-async fn embed_blocked_under_local_only() {
- let _mode =
- crate::security::live_policy::test_privacy_scope(crate::config::PrivacyMode::LocalOnly);
- let provider = OpenHumanCloudEmbeddingModel::new(
- Some("http://127.0.0.1:0".into()),
- Some(std::env::temp_dir().join("openhuman_embeddings_localonly_state")),
- false,
+async fn unregistered_managed_embedder_answers_backend_unavailable() {
+ let provider = UnavailableEmbeddingModel::new(
DEFAULT_CLOUD_EMBEDDING_MODEL,
DEFAULT_CLOUD_EMBEDDING_DIMENSIONS,
);
-
+ assert_eq!(provider.name(), "cloud");
+ assert_eq!(provider.dimensions(), DEFAULT_CLOUD_EMBEDDING_DIMENSIONS);
let err = provider.embed(&["hello".to_string()]).await.unwrap_err();
assert!(
err.to_string()
- .contains("Local-only privacy mode is active"),
+ .contains(crate::core::observability::BACKEND_UNAVAILABLE_PREFIX),
"unexpected error: {err}"
);
}
@@ -98,200 +92,3 @@ fn env_workspace_scope_is_the_config_dir_not_the_raw_workspace() {
);
}
-/// A stored TinyHumans API key authenticates cloud embeddings on its own (no
-/// session), sent as `Authorization: Bearer <key>` like managed inference.
-#[tokio::test]
-async fn api_key_alone_authenticates_cloud_embeddings() {
- use axum::{http::HeaderMap, routing::post, Json, Router};
- use std::sync::{Arc, Mutex};
-
- let seen: Arc<Mutex<Option<String>>> = Arc::default();
- let app = Router::new().route(
- "/openai/v1/embeddings",
- post({
- let seen = Arc::clone(&seen);
- move |headers: HeaderMap| async move {
- *seen.lock().unwrap() = headers
- .get("authorization")
- .and_then(|v| v.to_str().ok())
- .map(str::to_owned);
- Json(serde_json::json!({
- "object": "list",
- "data": [{ "object": "embedding", "index": 0, "embedding": [0.5, 0.25] }],
- "model": "m"
- }))
- }
- }),
- );
- let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
- let addr = listener.local_addr().unwrap();
- tokio::spawn(async move { axum::serve(listener, app).await.unwrap() });
-
- let state = tempfile::tempdir().unwrap();
- crate::security::credentials::api_key::store_api_key_in(state.path(), false, "tiny_test_emb")
- .unwrap();
- let provider = OpenHumanCloudEmbeddingModel::new(
- Some(format!("http://{addr}")),
- Some(state.path().to_path_buf()),
- false,
- "m",
- 2,
- );
-
- let vectors = provider
- .embed(&["hello".to_string()])
- .await
- .expect("an API key alone embeds");
- assert_eq!(vectors.len(), 1);
- assert_eq!(
- seen.lock().unwrap().as_deref(),
- Some("Bearer tiny_test_emb")
- );
-}
-
-/// The key is never put on a plaintext non-loopback wire.
-#[tokio::test]
-async fn api_key_is_refused_over_plaintext_non_loopback() {
- let state = tempfile::tempdir().unwrap();
- crate::security::credentials::api_key::store_api_key_in(state.path(), false, "tiny_test_emb")
- .unwrap();
- let provider = OpenHumanCloudEmbeddingModel::new(
- Some("http://embeddings.example.test".into()),
- Some(state.path().to_path_buf()),
- false,
- "m",
- 2,
- );
- let err = provider.embed(&["hello".to_string()]).await.unwrap_err();
- assert!(
- err.to_string()
- .contains("refusing to send the TinyHumans API key"),
- "{err}"
- );
-}
-
-/// Drains `rx` until the cloud-embedding disclosure for `marker` arrives.
-/// Tolerates unrelated events: the bus is process-wide.
-async fn find_embedding_disclosure(
- rx: &mut tinybus::events::EventReceiver<crate::core::events::DomainEvent>,
- marker: &str,
-) -> (Option<String>, Option<String>, Option<String>) {
- loop {
- match rx.recv().await {
- Some(crate::core::events::DomainEvent::ExternalTransferPending {
- descriptor,
- thread_id,
- client_id,
- request_id,
- }) if descriptor.service == marker => return (thread_id, client_id, request_id),
- Some(_) => continue,
- None => panic!("the bus closed before the disclosure arrived"),
- }
- }
-}
-
-fn unreachable_embedder(model: &str) -> OpenHumanCloudEmbeddingModel {
- OpenHumanCloudEmbeddingModel::new(
- Some("http://127.0.0.1:0".into()),
- Some(std::env::temp_dir().join("openhuman_embeddings_routing_state")),
- false,
- model,
- DEFAULT_CLOUD_EMBEDDING_DIMENSIONS,
- )
-}
-
-/// A cloud-embedding call made during a web-chat turn discloses its egress to
-/// that chat. The chat rides the embedder explicitly (built for the turn from
-/// its approval context); it is not read from the running task, which is why
-/// this call, awaited on a plain test task, still routes.
-#[tokio::test]
-async fn a_cloud_embedding_call_in_a_chat_turn_discloses_to_that_chat() {
- crate::core::bus::init().await.expect("bus init");
- let mut rx = crate::core::bus::BUS.get().unwrap().receiver();
- let marker = "embed-routed-to-the-turns-chat";
- let chat = ApprovalChatContext {
- thread_id: "thread-embed".into(),
- client_id: "client-embed".into(),
- request_id: Some("req-embed".into()),
- };
- let embedder = unreachable_embedder(marker).routed_to(Some(chat));
-
- // The transport has nowhere to go; the disclosure precedes it.
- let _ = embedder
- .embed(&["what did I say about invoices?".to_string()])
- .await;
-
- let (thread_id, client_id, request_id) = find_embedding_disclosure(&mut rx, marker).await;
- assert_eq!(thread_id.as_deref(), Some("thread-embed"));
- assert_eq!(client_id.as_deref(), Some("client-embed"));
- assert_eq!(request_id.as_deref(), Some("req-embed"));
-}
-
-/// An embedder built with no chat (background sync, the embedding RPC) still
-/// discloses, unrouted, and a blank or empty batch discloses nothing because
-/// nothing leaves.
-#[tokio::test]
-async fn an_unrouted_embedder_discloses_without_a_chat() {
- crate::core::bus::init().await.expect("bus init");
- let mut rx = crate::core::bus::BUS.get().unwrap().receiver();
- let marker = "embed-without-a-chat";
- let embedder = unreachable_embedder(marker);
-
- let _ = embedder.embed(&[]).await;
- let _ = embedder.embed(&[" ".to_string()]).await;
- let _ = embedder.embed(&["real text".to_string()]).await;
-
- // The first disclosure for this marker is the third call's: the empty and
- // blank batches published nothing.
- let (thread_id, client_id, request_id) = find_embedding_disclosure(&mut rx, marker).await;
- assert_eq!((thread_id, client_id, request_id), (None, None, None));
- assert!(
- tokio::time::timeout(
- std::time::Duration::from_millis(200),
- find_embedding_disclosure(&mut rx, marker)
- )
- .await
- .is_err(),
- "only the batch that leaves is disclosed"
- );
-}
-
-/// `routed_to` re-routes a shared embedder per turn without touching the
-/// original: each turn's disclosure reaches its own chat.
-#[tokio::test]
-async fn routing_is_per_embedder_not_shared() {
- crate::core::bus::init().await.expect("bus init");
- let mut rx = crate::core::bus::BUS.get().unwrap().receiver();
- let marker = "embed-routed-per-turn";
- let base = unreachable_embedder(marker);
- let chat = |thread: &str| ApprovalChatContext {
- thread_id: thread.into(),
- client_id: "c".into(),
- request_id: None,
- };
-
- let _ = base
- .routed_to(Some(chat("thread-a")))
- .embed(&["a".into()])
- .await;
- assert_eq!(
- find_embedding_disclosure(&mut rx, marker)
- .await
- .0
- .as_deref(),
- Some("thread-a")
- );
- let _ = base
- .routed_to(Some(chat("thread-b")))
- .embed(&["b".into()])
- .await;
- assert_eq!(
- find_embedding_disclosure(&mut rx, marker)
- .await
- .0
- .as_deref(),
- Some("thread-b")
- );
- let _ = base.embed(&["c".into()]).await;
- assert_eq!(find_embedding_disclosure(&mut rx, marker).await.0, None);
-}
diff --git a/crates/openhuman-embed/src/host_internals.rs b/crates/openhuman-embed/src/host_internals.rs
index ecef43aa5b..b64c86ffda 100644
--- a/crates/openhuman-embed/src/host_internals.rs
+++ b/crates/openhuman-embed/src/host_internals.rs
@@ -266,7 +266,7 @@ pub mod security {
};
}
pub mod api_key {
- pub use openhuman_core::security::credentials::api_key::get_api_key_in;
+ pub use openhuman_core::security::credentials::api_key::{get_api_key_in, store_api_key_in};
}
}
pub mod approval {
diff --git a/crates/openhuman-tinyhumans/Cargo.toml b/crates/openhuman-tinyhumans/Cargo.toml
index 6d04d252cc..ffafeeee96 100644
--- a/crates/openhuman-tinyhumans/Cargo.toml
+++ b/crates/openhuman-tinyhumans/Cargo.toml
@@ -113,6 +113,7 @@ reqwest = { version = "0.12", default-features = false, features = ["json", "mul
# tower and tracing features and force a separate core build (see below).
axum = { version = "0.8", default-features = false, features = ["http1", "json", "tokio", "query", "ws", "macros"] }
tempfile = "3"
+tinybus = { path = "../../vendor/tinybus/crates/tinybus", default-features = false }
tokio = { version = "1", features = ["full", "test-util"] }
wiremock = "0.6"
tokio-util = { version = "0.7", features = ["rt", "io"] }
diff --git a/crates/openhuman-tinyhumans/src/inference/managed_embedding_tests.rs b/crates/openhuman-tinyhumans/src/inference/managed_embedding_tests.rs
index 67e494f924..21e9d72e99 100644
--- a/crates/openhuman-tinyhumans/src/inference/managed_embedding_tests.rs
+++ b/crates/openhuman-tinyhumans/src/inference/managed_embedding_tests.rs
@@ -78,7 +78,7 @@ async fn api_key_is_refused_over_plaintext_non_loopback() {
/// Drains `rx` until the cloud-embedding disclosure for `marker` arrives.
/// Tolerates unrelated events: the bus is process-wide.
async fn find_embedding_disclosure(
- rx: &mut tinybus_events_placeholder<openhuman_embed::__host::core::events::DomainEvent>,
+ rx: &mut tinybus::events::EventReceiver<openhuman_embed::__host::core::events::DomainEvent>,
marker: &str,
) -> (Option<String>, Option<String>, Option<String>) {
loop {