Skip to content

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

fix(composio): a saved scope pref is not undone by a deferred legacy migration

M3gA-Minde37385845546
diff --git a/crates/openhuman-core/src/integrations/composio/ops/scope_prefs.rs b/crates/openhuman-core/src/integrations/composio/ops/scope_prefs.rs
index 6d01c18461..0bf0881a1a 100644
--- a/crates/openhuman-core/src/integrations/composio/ops/scope_prefs.rs
+++ b/crates/openhuman-core/src/integrations/composio/ops/scope_prefs.rs
@@ -53,10 +53,25 @@ fn migrated() -> &'static Mutex<HashSet<PathBuf>> {
SET.get_or_init(|| Mutex::new(HashSet::new()))
}
+/// Held across a migration pass and across `save`'s write plus its legacy-row
+/// drop, so a migration that read the file before a save cannot push its stale
+/// row over the value the save just stored.
+// ponytail: one process-wide lock; per-workspace if contention ever shows.
+static LEGACY: tokio::sync::Mutex<()> = tokio::sync::Mutex::const_new(());
+
+/// Test seam: make the migration of one toolkit's row fail, as a module error
+/// would, so a deferred (partial) migration can be driven deterministically.
+#[cfg(test)]
+thread_local! {
+ static FAIL_MIGRATING: std::cell::RefCell<Option<String>> =
+ const { std::cell::RefCell::new(None) };
+}
+
/// Move rows still in the legacy workspace file into the module, then empty
/// the file. A failure leaves the file untouched and is retried on the next
/// call, so no stored preference is lost to a transient module error.
async fn migrate_legacy(ctx: &crate::core::runtime::DispatchCtx, config: &Config) {
+ let _legacy = LEGACY.lock().await;
let workspace = config.workspace_dir.clone();
if migrated()
.lock()
@@ -74,6 +89,11 @@ async fn migrate_legacy(ctx: &crate::core::runtime::DispatchCtx, config: &Config
}
};
for (toolkit, pref) in &rows {
+ #[cfg(test)]
+ if FAIL_MIGRATING.with(|fail| fail.borrow().as_deref() == Some(toolkit.as_str())) {
+ tracing::warn!(%toolkit, "{LOG} legacy pref migration deferred (test seam)");
+ return;
+ }
let request = ComposioSetUserScopesRequest {
toolkit: toolkit.clone(),
scopes: to_wire(*pref),
@@ -185,12 +205,32 @@ pub(crate) async fn save(
return Err("user_scopes: toolkit must not be empty".to_string());
}
migrate_legacy(ctx, config).await;
+ let _legacy = LEGACY.lock().await;
let request = ComposioSetUserScopesRequest {
toolkit: key.clone(),
scopes: to_wire(pref),
};
connectors::call::<_, ComposioUserScopesResponse>(config, methods::SET_USER_SCOPES, request)
.await?;
+ // A migration still deferred would push this toolkit's legacy row again
+ // on the next read and undo the choice just stored. Drop the row only
+ // after the write succeeded: dropping it first and then failing the write
+ // would fall back to the permissive default.
+ let pending = !migrated()
+ .lock()
+ .unwrap_or_else(PoisonError::into_inner)
+ .contains(&config.workspace_dir);
+ if pending {
+ let row = key.clone();
+ file_store::update(
+ ctx,
+ config,
+ USER_SCOPES_FILE,
+ move |prefs: &mut LegacyScopeFile| ((), prefs.remove(&row).is_some()),
+ )
+ .await
+ .map_err(|error| format!("user_scopes: legacy row not cleared: {error}"))?;
+ }
tracing::info!(
toolkit = %key,
read = pref.read,
diff --git a/crates/openhuman-core/src/integrations/composio/ops/scope_prefs_tests.rs b/crates/openhuman-core/src/integrations/composio/ops/scope_prefs_tests.rs
index 4e0a5b31d6..630a0ff852 100644
--- a/crates/openhuman-core/src/integrations/composio/ops/scope_prefs_tests.rs
+++ b/crates/openhuman-core/src/integrations/composio/ops/scope_prefs_tests.rs
@@ -100,3 +100,55 @@ async fn a_workspace_with_no_legacy_file_is_marked_migrated() {
migrate_legacy(&crate::core::runtime::DispatchCtx::ambient(), &config).await;
assert!(migrated().lock().unwrap().contains(&config.workspace_dir));
}
+
+/// A save made while the legacy migration is still deferred (another row keeps
+/// failing) must not be undone by the next migration pass re-pushing the
+/// toolkit's stale legacy row.
+#[tokio::test]
+async fn a_saved_pref_survives_a_deferred_legacy_migration() {
+ use crate::integrations::composio::module_client::module_guard;
+ let _serialised = module_guard().await;
+ let ctx = crate::core::runtime::DispatchCtx::ambient();
+ let tmp = tempfile::tempdir().unwrap();
+ let mut config = config_in(&tmp);
+ config.workspace_dir = tmp.path().join("workspace");
+ config.config_path = tmp.path().join("config.toml");
+ file_store::save(
+ &ctx,
+ &config,
+ USER_SCOPES_FILE,
+ &serde_json::json!({
+ "gmail": {"read": true, "write": true, "admin": true},
+ "slack": {"read": true, "write": true, "admin": false},
+ }),
+ )
+ .await
+ .unwrap();
+ FAIL_MIGRATING.with(|fail| *fail.borrow_mut() = Some("slack".into()));
+
+ let revoked = UserScopePref {
+ read: true,
+ write: true,
+ admin: false,
+ };
+ let saved = save(&ctx, &config, "gmail", revoked).await;
+ let pref = load_or_default(&ctx, &config, "gmail").await;
+ let left: LegacyScopeFile = file_store::load(&ctx, &config, USER_SCOPES_FILE)
+ .await
+ .unwrap();
+
+ FAIL_MIGRATING.with(|fail| *fail.borrow_mut() = None);
+ let _ = save(&ctx, &config, "gmail", UserScopePref::default()).await;
+ let _ = save(&ctx, &config, "slack", UserScopePref::default()).await;
+
+ saved.expect("the module stores the row");
+ assert_eq!(pref, revoked, "a revoked admin scope came back");
+ assert!(
+ !left.contains_key("gmail"),
+ "the saved toolkit's legacy row stays"
+ );
+ assert!(
+ left.contains_key("slack"),
+ "an unmigrated row is kept for retry"
+ );
+}