Skip to content

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

fix(composio): restore the prior module row when the legacy-row drop fails

M3gA-Mind35c188b3da70
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 0bf0881a1a..57daba4512 100644
--- a/crates/openhuman-core/src/integrations/composio/ops/scope_prefs.rs
+++ b/crates/openhuman-core/src/integrations/composio/ops/scope_prefs.rs
@@ -206,30 +206,64 @@ pub(crate) async fn save(
}
migrate_legacy(ctx, config).await;
let _legacy = LEGACY.lock().await;
+ // A migration still deferred would push this toolkit's legacy row again
+ // on the next read and undo the choice stored below, so that row is
+ // dropped after the write. Drop it 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);
+ // The row the module enforces now, put back if the drop fails, so a save
+ // reported as failed leaves the module as the UI will show it.
+ let prior = if pending {
+ let request = ComposioGetUserScopesRequest {
+ toolkit: key.clone(),
+ };
+ Some(
+ connectors::call::<_, ComposioUserScopesResponse>(
+ config,
+ methods::GET_USER_SCOPES,
+ request,
+ )
+ .await?
+ .scopes,
+ )
+ } else {
+ None
+ };
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 {
+ if let Some(prior) = prior {
let row = key.clone();
- file_store::update(
+ let cleared = 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}"))?;
+ .await;
+ if let Err(error) = cleared {
+ let request = ComposioSetUserScopesRequest {
+ toolkit: key.clone(),
+ scopes: prior,
+ };
+ if let Err(restore) = connectors::call::<_, ComposioUserScopesResponse>(
+ config,
+ methods::SET_USER_SCOPES,
+ request,
+ )
+ .await
+ {
+ tracing::error!(toolkit = %key, %restore, "{LOG} prior pref not restored");
+ }
+ return Err(format!("user_scopes: legacy row not cleared: {error}"));
+ }
}
tracing::info!(
toolkit = %key,
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 630a0ff852..10d434cf18 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
@@ -152,3 +152,59 @@ async fn a_saved_pref_survives_a_deferred_legacy_migration() {
"an unmigrated row is kept for retry"
);
}
+
+/// When the legacy row can't be dropped (an unreadable legacy file), the save
+/// reports failure, so the module must keep the row it enforced before rather
+/// than the one the UI is about to roll back.
+#[tokio::test]
+async fn a_save_whose_legacy_cleanup_fails_leaves_the_prior_row() {
+ 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");
+ let granted = UserScopePref {
+ read: true,
+ write: true,
+ admin: true,
+ };
+ connectors::call::<_, ComposioUserScopesResponse>(
+ &config,
+ methods::SET_USER_SCOPES,
+ ComposioSetUserScopesRequest {
+ toolkit: "gmail".into(),
+ scopes: to_wire(granted),
+ },
+ )
+ .await
+ .unwrap();
+ file_store::save(
+ &ctx,
+ &config,
+ USER_SCOPES_FILE,
+ &serde_json::json!({"gmail": "not a pref"}),
+ )
+ .await
+ .unwrap();
+
+ let saved = save(&ctx, &config, "gmail", UserScopePref::default()).await;
+ let enforced = connectors::call::<_, ComposioUserScopesResponse>(
+ &config,
+ methods::GET_USER_SCOPES,
+ ComposioGetUserScopesRequest {
+ toolkit: "gmail".into(),
+ },
+ )
+ .await
+ .map(|reply| from_wire(reply.scopes));
+
+ file_store::save(&ctx, &config, USER_SCOPES_FILE, &serde_json::json!({}))
+ .await
+ .unwrap();
+ let _ = save(&ctx, &config, "gmail", UserScopePref::default()).await;
+
+ assert!(saved.is_err(), "the save must report the failed cleanup");
+ assert_eq!(enforced.unwrap(), granted, "the module keeps the prior row");
+}