The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
test(tokenjuice): cover scoped legacy savings import in rpc tests
Steven Enamakel871dbf3ccd2d
diff --git a/crates/openhuman-core/src/inference/tokenjuice/rpc_scope_tests.rs b/crates/openhuman-core/src/inference/tokenjuice/rpc_scope_tests.rsindex c2e5a013ea..3466d71e76 100644--- a/crates/openhuman-core/src/inference/tokenjuice/rpc_scope_tests.rs+++ b/crates/openhuman-core/src/inference/tokenjuice/rpc_scope_tests.rs@@ -3,6 +3,7 @@ use std::sync::{Arc, Mutex};use crate::config::Config;use crate::core::runtime::{ContextOverlay, CoreContext, DispatchCtx, DomainSet};+use crate::storage::StorageBackend;use tinybus::{broker::Broker, transport::memory::MemoryBus, Connection, ObjectPath};#[derive(Default)]@@ -156,35 +157,67 @@ fn profile_config(workspace: &std::path::Path, threshold: usize) -> Config {config}-struct WorkspaceEnvGuard(Option<std::ffi::OsString>);+struct StorageGuard(Option<Arc<dyn StorageBackend>>);-impl WorkspaceEnvGuard {- fn set(workspace: &std::path::Path) -> Self {- let previous = std::env::var_os("OPENHUMAN_WORKSPACE");- std::env::set_var("OPENHUMAN_WORKSPACE", workspace);- Self(previous)+impl StorageGuard {+ fn install_memory() -> Self {+ Self(crate::storage::install(Arc::new(+ crate::storage::MemoryStorage::new(),+ )))}}-impl Drop for WorkspaceEnvGuard {+impl Drop for StorageGuard {fn drop(&mut self) {+ crate::storage::clear();if let Some(previous) = self.0.take() {- std::env::set_var("OPENHUMAN_WORKSPACE", previous);- } else {- std::env::remove_var("OPENHUMAN_WORKSPACE");+ crate::storage::install(previous);}}}+fn savings_marker_id(workspace: &std::path::Path) -> String {+ use sha2::Digest as _;+ let path = workspace.join("state").join("tokenjuice_savings.json");+ let digest = sha2::Sha256::digest(path.as_os_str().as_encoded_bytes());+ format!("savings@{}", &hex::encode(digest)[..16])+}++async fn savings_imported(workspace: &std::path::Path, profile: Option<&str>) -> bool {+ let backend = crate::storage::installed().expect("the fixture storage backend is installed");+ let scope = crate::storage::scope_for_module(profile, "tinyjuice");+ let scoped = backend.for_scope(&scope).unwrap();+ crate::storage::import_state::host_import_done(+ &scoped,+ "tinyjuice_host_import",+ &savings_marker_id(workspace),+ )+ .await+ .unwrap()+}++const SAVINGS_SNAPSHOT: &str = r#"{+ "total":{"events":1,"originalTokens":12,"compactedTokens":6,"tokensSaved":6,"costSavedUsd":0.0},+ "byModel":{},+ "byCompressor":{}+}"#;+#[tokio::test]async fn tokenjuice_rpcs_use_their_derived_config_for_module_calls_and_install() {let _env_guard = crate::config::TEST_ENV_LOCK.lock().await;let operator = tempfile::tempdir().unwrap();let operator_workspace = operator.path().join("workspace");std::fs::create_dir_all(&operator_workspace).unwrap();- let _workspace_env = WorkspaceEnvGuard::set(&operator_workspace);+ let _workspace_env =+ crate::config::test_env::EnvVarGuard::workspace_unlocked(&operator_workspace);+ let _storage = StorageGuard::install_memory();let workspaces = [tempfile::tempdir().unwrap(), tempfile::tempdir().unwrap()];+ for workspace in &workspaces {+ let state = workspace.path().join("state");+ std::fs::create_dir_all(&state).unwrap();+ std::fs::write(state.join("tokenjuice_savings.json"), SAVINGS_SNAPSHOT).unwrap();+ }let configs = [profile_config(workspaces[0].path(), 711),profile_config(workspaces[1].path(), 922),@@ -263,5 +296,14 @@ async fn tokenjuice_rpcs_use_their_derived_config_for_module_calls_and_install()}),"Install must carry workspace and option values from derived config {index}");+ let profile = ["juice-profile-a", "juice-profile-b"][index];+ assert!(+ savings_imported(&config.workspace_dir, Some(profile)).await,+ "legacy savings import must complete in the caller's profile scope"+ );+ assert!(+ !savings_imported(&config.workspace_dir, None).await,+ "legacy savings import must not write into the operator's module scope"+ );}}