Skip to content

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

fix(mcp-ui): scope the widget document cache by tenant

oxoxDevbf91b5061b4e
diff --git a/crates/openhuman-core/src/mcp/ui/cache.rs b/crates/openhuman-core/src/mcp/ui/cache.rs
index 31d09377ff..a70b1948a2 100644
--- a/crates/openhuman-core/src/mcp/ui/cache.rs
+++ b/crates/openhuman-core/src/mcp/ui/cache.rs
@@ -35,6 +35,15 @@ struct Store {
reads: HashMap<(String, String), Timed<UiResource>>,
}
+fn scoped(id: &str) -> String {
+ let tenant = crate::core::runtime::tenant::current_tenant_or_isolated("mcp_ui::cache");
+ scoped_for(&tenant, id)
+}
+
+fn scoped_for(tenant: &crate::core::runtime::Tenant, id: &str) -> String {
+ crate::core::runtime::tenant_key(tenant, id)
+}
+
static STORE: LazyLock<Mutex<Store>> = LazyLock::new(|| Mutex::new(Store::default()));
fn evict<K: Clone + Eq + std::hash::Hash, T>(map: &mut HashMap<K, Timed<T>>, ttl: Duration) {
@@ -57,7 +66,7 @@ pub fn put_inline(entry: InlineEntry) -> String {
let mut store = STORE.lock();
evict(&mut store.inline, INLINE_TTL);
store.inline.insert(
- id.clone(),
+ scoped(&id),
Timed {
at: Instant::now(),
value: entry,
@@ -73,7 +82,7 @@ pub fn get_inline(id: &str) -> Option<InlineEntry> {
let store = STORE.lock();
store
.inline
- .get(id)
+ .get(&scoped(id))
.filter(|entry| entry.at.elapsed() < INLINE_TTL)
.map(|entry| entry.value.clone())
}
@@ -84,7 +93,7 @@ pub fn get_read(server_id: &str, uri: &str) -> Option<UiResource> {
let store = STORE.lock();
store
.reads
- .get(&(server_id.to_string(), uri.to_string()))
+ .get(&(scoped(server_id), uri.to_string()))
.filter(|entry| entry.at.elapsed() < READ_TTL)
.map(|entry| entry.value.clone())
}
@@ -94,7 +103,7 @@ pub fn put_read(server_id: &str, uri: &str, resource: UiResource) {
let mut store = STORE.lock();
evict(&mut store.reads, READ_TTL);
store.reads.insert(
- (server_id.to_string(), uri.to_string()),
+ (scoped(server_id), uri.to_string()),
Timed {
at: Instant::now(),
value: resource,
diff --git a/crates/openhuman-core/src/mcp/ui/cache_tests.rs b/crates/openhuman-core/src/mcp/ui/cache_tests.rs
index c7fe60762d..a614e97d66 100644
--- a/crates/openhuman-core/src/mcp/ui/cache_tests.rs
+++ b/crates/openhuman-core/src/mcp/ui/cache_tests.rs
@@ -63,3 +63,21 @@ fn eviction_drops_expired_entries() {
evict(&mut map, Duration::from_secs(1));
assert!(map.is_empty());
}
+
+#[test]
+fn entries_are_not_shared_across_tenants() {
+ use crate::core::runtime::Tenant;
+ let tenant = |profile: Option<&str>| Tenant {
+ profile: profile.map(str::to_string),
+ agent: None,
+ };
+ assert_ne!(
+ scoped_for(&tenant(Some("alice")), "doc"),
+ scoped_for(&tenant(Some("bob")), "doc")
+ );
+ assert_ne!(
+ scoped_for(&tenant(Some("alice")), "doc"),
+ scoped_for(&tenant(None), "doc")
+ );
+ assert_eq!(scoped_for(&tenant(None), "doc"), "doc");
+}
diff --git a/scripts/ci/process-statics.json b/scripts/ci/process-statics.json
index fe51488c70..0b92d53cba 100644
--- a/scripts/ci/process-statics.json
+++ b/scripts/ci/process-statics.json
@@ -1 +1,12 @@
-[]
+[
+ {
+ "file": "crates/openhuman-core/src/mcp/ui/cache.rs",
+ "scope": "",
+ "name": "STORE",
+ "type": "LazyLock<Mutex<Store>>",
+ "count": 1,
+ "category": "B",
+ "reason": "Every entry key is prefixed with the calling tenant's profile and agent scope through tenant_key, and a SaaS call with no tenant scope gets an isolated key that never matches.",
+ "verification": "mcp/ui/cache_tests.rs: entries_are_not_shared_across_tenants"
+ }
+]