Skip to content

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

fix(integrations): correct file store path handling for composio

Steven Enamakel6b6ace627741
diff --git a/crates/openhuman-core/src/agent/prompts/mod_tests_subagent_render_tests.rs b/crates/openhuman-core/src/agent/prompts/mod_tests_subagent_render_tests.rs
index 7abe0a8a8b..3a1088cc87 100644
--- a/crates/openhuman-core/src/agent/prompts/mod_tests_subagent_render_tests.rs
+++ b/crates/openhuman-core/src/agent/prompts/mod_tests_subagent_render_tests.rs
@@ -1,33 +1,5 @@
use super::*;
-#[test]
-fn user_memory_section_returns_empty_when_no_summaries() {
- // Empty learned context → section returns empty string and is
- // skipped by the prompt builder, so the cache boundary stays
- // exactly where it was for workspaces with no tree summaries.
- let learned = LearnedContextData::default();
- let prompt_tools: Vec<PromptTool<'_>> = Vec::new();
- let ctx = PromptContext {
- workspace_dir: Path::new("/tmp"),
- model_name: "test-model",
- agent_id: "",
- tools: &prompt_tools,
- workflows: &[],
- dispatcher_instructions: "",
- learned,
- visible_tool_names: &NO_FILTER,
- tool_call_format: ToolCallFormat::PFormat,
- connected_integrations: &[],
- connected_identities_md: String::new(),
- user_identity: None,
- personality_roster: vec![],
- agents_md_global: None,
- agents_md_local: None,
- };
- let rendered = UserMemorySection.build(&ctx).unwrap();
- assert!(rendered.is_empty());
-}
-
#[test]
fn render_subagent_system_prompt_renders_workspace_tail() {
let workspace = std::env::temp_dir().join(format!(
@@ -104,25 +76,13 @@ fn subagent_prompt_defaults_to_json_and_omits_protocol_without_tools() {
#[test]
fn subagent_render_options_invert_definition_flags() {
- // (omit_identity, omit_safety_preamble,
- // omit_profile, omit_memory_md)
- let options = SubagentRenderOptions::from_definition_flags(true, false, false, false);
+ // (omit_identity, omit_safety_preamble)
+ let options = SubagentRenderOptions::from_definition_flags(true, false);
assert!(!options.include_identity);
assert!(options.include_safety_preamble);
- assert!(options.include_profile);
- assert!(options.include_memory_md);
let narrow = SubagentRenderOptions::narrow();
- let default = SubagentRenderOptions::default();
- assert_eq!(narrow.include_identity, default.include_identity);
- assert_eq!(
- narrow.include_safety_preamble,
- default.include_safety_preamble
- );
- assert_eq!(narrow.include_profile, default.include_profile);
- assert_eq!(narrow.include_memory_md, default.include_memory_md);
- // Narrow default = every flag off, including both user files.
- assert!(!narrow.include_profile);
- assert!(!narrow.include_memory_md);
+ assert!(!narrow.include_identity);
+ assert!(!narrow.include_safety_preamble);
}
#[test]
@@ -212,125 +172,30 @@ fn render_with_files(files: &[(&str, &str)], options: SubagentRenderOptions) ->
)
}
-fn only(identity: bool, profile: bool, memory: bool) -> SubagentRenderOptions {
- SubagentRenderOptions {
- include_identity: identity,
- include_safety_preamble: false,
- }
-}
-
-const PROFILE: &str = "# User Profile\nName: Jane Doe\nRole: Data scientist";
-const MEMORY: &str = "# Long-term memory\nUser prefers terse Rust answers.";
-
#[test]
-fn subagent_profile_md_follows_include_profile_flag_independent_of_identity() {
- // include_profile=true injects PROFILE.md even with the identity preamble
- // omitted (SOUL/IDENTITY stay hidden); with identity on, all three appear.
- let soul = [
- ("SOUL.md", "# Soul\nctx"),
- ("IDENTITY.md", "# Identity\nctx"),
- ("PROFILE.md", PROFILE),
+fn subagent_identity_flag_gates_bootstrap_files_and_never_injects_v1_memory_files() {
+ let files = [
+ ("SOUL.md", "# Soul
+ctx"),
+ ("IDENTITY.md", "# Identity
+ctx"),
+ ("PROFILE.md", "# User Profile
+Name: Jane Doe"),
+ ("MEMORY.md", "# Long-term memory
+User prefers terse Rust answers."),
];
- let rendered = render_with_files(&soul, only(false, true, false));
- assert!(rendered.contains("### PROFILE.md"), "{rendered}");
- assert!(rendered.contains("Jane Doe"), "{rendered}");
- assert!(!rendered.contains("## Project Context"), "{rendered}");
- assert!(
- !rendered.contains("### SOUL.md") && !rendered.contains("### IDENTITY.md"),
- "{rendered}"
- );
-
- let rendered = render_with_files(&soul, only(true, true, false));
- for header in [
- "## Project Context",
- "### SOUL.md",
- "### IDENTITY.md",
- "### PROFILE.md",
- ] {
- assert!(rendered.contains(header), "{header}: {rendered}");
- }
-
- // include_profile=false never leaks the file, even when it is on disk.
- let rendered = render_with_files(&soul, SubagentRenderOptions::narrow());
- assert!(!rendered.contains("### PROFILE.md"), "{rendered}");
- assert!(!rendered.contains("ctx"), "{rendered}");
-}
-
-#[test]
-fn render_subagent_system_prompt_silently_skips_missing_profile_md() {
- // Pre-onboarding workspaces have no PROFILE.md: no orphan header, no
- // "[File not found]" placeholder.
- let rendered = render_with_files(&[], only(false, true, false));
- assert!(!rendered.contains("### PROFILE.md"), "{rendered}");
- assert!(
- !rendered.contains("[File not found: PROFILE.md]"),
- "{rendered}"
- );
-}
+ let with_identity =
+ render_with_files(&files, SubagentRenderOptions::from_definition_flags(false, true));
+ assert!(with_identity.contains("### SOUL.md"), "{with_identity}");
+ assert!(with_identity.contains("### IDENTITY.md"), "{with_identity}");
-#[test]
-fn subagent_definition_flags_gate_profile_md() {
- // omit_profile=false opts IN even with omit_identity=true.
- let rendered = render_with_files(
- &[("PROFILE.md", PROFILE)],
- SubagentRenderOptions::from_definition_flags(true, true, false, false),
- );
- assert!(rendered.contains("### PROFILE.md"), "{rendered}");
- assert!(rendered.contains("Jane Doe"), "{rendered}");
-
- // A narrow specialist (every omit_* true) must not see it.
- let rendered = render_with_files(
- &[("PROFILE.md", PROFILE)],
- SubagentRenderOptions::from_definition_flags(true, true, true, true),
- );
- assert!(!rendered.contains("### PROFILE.md"), "{rendered}");
- assert!(!rendered.contains("Jane Doe"), "{rendered}");
-}
-
-#[test]
-fn render_subagent_system_prompt_frames_memory_md_as_background() {
- // GH-4745: sub-agent MEMORY.md must share the background-memory frame so a
- // fresh thread does not read it as prior in-thread conversation.
- let rendered = render_with_files(
- &[(
- "MEMORY.md",
- "# Long-term memory\nReviewed `def f(x)` last week; user prefers terse notes.",
- )],
- only(false, false, true),
- );
- assert!(
- rendered.contains("### MEMORY.md") && rendered.contains("terse notes"),
- "{rendered}"
- );
- let frame_at = rendered
- .find("background — not this conversation")
- .unwrap_or_else(|| panic!("missing background frame: {rendered}"));
- let heading_at = rendered.find("### MEMORY.md").unwrap();
- assert!(
- frame_at < heading_at,
- "frame must precede block: {rendered}"
- );
-}
+ let narrow = render_with_files(&files, SubagentRenderOptions::narrow());
+ assert!(!narrow.contains("## Project Context"), "{narrow}");
-#[test]
-fn render_subagent_system_prompt_omits_memory_framing_when_no_memory_content() {
- // include_memory_md=true but no MEMORY.md on disk: no dangling frame.
- let rendered = render_with_files(&[], only(false, false, true));
- assert!(
- !rendered.contains("background — not this conversation"),
- "{rendered}"
- );
-}
-
-#[test]
-fn subagent_memory_md_follows_include_memory_flag() {
- let rendered = render_with_files(&[("MEMORY.md", MEMORY)], only(false, false, true));
- assert!(rendered.contains("### MEMORY.md"), "{rendered}");
- assert!(rendered.contains("terse Rust answers"), "{rendered}");
-
- let rendered = render_with_files(&[("MEMORY.md", MEMORY)], SubagentRenderOptions::narrow());
- assert!(!rendered.contains("### MEMORY.md"), "{rendered}");
- assert!(!rendered.contains("terse Rust answers"), "{rendered}");
+ for rendered in [&with_identity, &narrow] {
+ assert!(!rendered.contains("PROFILE.md") && !rendered.contains("Jane Doe"));
+ assert!(!rendered.contains("MEMORY.md") && !rendered.contains("terse Rust"));
+ }
}
#[test]
diff --git a/crates/openhuman-core/src/integrations/composio/file_store.rs b/crates/openhuman-core/src/integrations/composio/file_store.rs
new file mode 100644
index 0000000000..aee730b668
--- /dev/null
+++ b/crates/openhuman-core/src/integrations/composio/file_store.rs
@@ -0,0 +1,93 @@
+//! Small JSON files under the workspace for the Composio host state that is
+//! not memory: connected-account identities and per-toolkit scope prefs.
+//!
+//! Each file holds one serde value. A read of a missing file is the default
+//! value; a write goes to a sibling temp file first and is renamed over the
+//! target, so a crash mid-write leaves the previous file intact. Every
+//! read-modify-write runs under [`lock`], one process-wide async mutex, so two
+//! concurrent updates cannot lose each other's change.
+
+use std::path::{Path, PathBuf};
+
+use serde::de::DeserializeOwned;
+use serde::Serialize;
+use tokio::sync::{Mutex, MutexGuard};
+
+use crate::config::Config;
+
+/// Directory under the workspace holding the integration state files.
+const DIR: &str = "integrations";
+
+/// Connected-account identities file name.
+pub(crate) const IDENTITIES_FILE: &str = "composio_identities.json";
+
+/// Per-toolkit agent scope prefs file name.
+pub(crate) const USER_SCOPES_FILE: &str = "composio_user_scopes.json";
+
+/// `<workspace>/integrations/<file>`.
+pub(crate) fn path(config: &Config, file: &str) -> PathBuf {
+ config.workspace_dir.join(DIR).join(file)
+}
+
+/// Serialises read-modify-write cycles over the integration state files.
+pub(crate) async fn lock() -> MutexGuard<'static, ()> {
+ static LOCK: Mutex<()> = Mutex::const_new(());
+ LOCK.lock().await
+}
+
+/// The value stored at `path`, or `T::default()` when the file does not exist.
+///
+/// # Errors
+///
+/// The file exists but cannot be read or does not parse as `T`.
+pub(crate) async fn load<T>(path: &Path) -> Result<T, String>
+where
+ T: DeserializeOwned + Default,
+{
+ let bytes = match tokio::fs::read(path).await {
+ Ok(bytes) => bytes,
+ Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(T::default()),
+ Err(error) => {
+ return Err(format!(
+ "[composio:store] reading {} failed: {error}",
+ path.display()
+ ))
+ }
+ };
+ serde_json::from_slice(&bytes)
+ .map_err(|error| format!("[composio:store] parsing {} failed: {error}", path.display()))
+}
+
+/// Writes `value` to `path` atomically (temp file, then rename).
+///
+/// # Errors
+///
+/// The directory cannot be created, or the write or rename fails.
+pub(crate) async fn save<T>(path: &Path, value: &T) -> Result<(), String>
+where
+ T: Serialize,
+{
+ let bytes = serde_json::to_vec_pretty(value)
+ .map_err(|error| format!("[composio:store] serializing failed: {error}"))?;
+ if let Some(dir) = path.parent() {
+ tokio::fs::create_dir_all(dir).await.map_err(|error| {
+ format!("[composio:store] creating {} failed: {error}", dir.display())
+ })?;
+ }
+ let tmp = path.with_extension("json.tmp");
+ tokio::fs::write(&tmp, &bytes)
+ .await
+ .map_err(|error| format!("[composio:store] writing {} failed: {error}", tmp.display()))?;
+ tokio::fs::rename(&tmp, path).await.map_err(|error| {
+ format!(
+ "[composio:store] replacing {} failed: {error}",
+ path.display()
+ )
+ })?;
+ tracing::debug!(path = %path.display(), bytes = bytes.len(), "[composio:store] saved");
+ Ok(())
+}
+
+#[cfg(test)]
+#[path = "file_store_tests.rs"]
+mod tests;
diff --git a/crates/openhuman-core/src/integrations/composio/file_store_tests.rs b/crates/openhuman-core/src/integrations/composio/file_store_tests.rs
new file mode 100644
index 0000000000..30a6f4e406
--- /dev/null
+++ b/crates/openhuman-core/src/integrations/composio/file_store_tests.rs
@@ -0,0 +1,46 @@
+use super::*;
+use std::collections::BTreeMap;
+
+#[tokio::test]
+async fn missing_file_loads_the_default() {
+ let dir = tempfile::tempdir().unwrap();
+ let loaded: BTreeMap<String, u32> = load(&dir.path().join("absent.json")).await.unwrap();
+ assert!(loaded.is_empty());
+}
+
+#[tokio::test]
+async fn save_then_load_round_trips_and_creates_the_directory() {
+ let dir = tempfile::tempdir().unwrap();
+ let path = dir.path().join("nested").join("state.json");
+ let mut value = BTreeMap::new();
+ value.insert("gmail".to_string(), 3u32);
+ save(&path, &value).await.unwrap();
+ let loaded: BTreeMap<String, u32> = load(&path).await.unwrap();
+ assert_eq!(loaded, value);
+ assert!(!path.with_extension("json.tmp").exists());
+}
+
+#[tokio::test]
+async fn corrupt_file_is_an_error_not_a_default() {
+ let dir = tempfile::tempdir().unwrap();
+ let path = dir.path().join("state.json");
+ std::fs::write(&path, b"{not json").unwrap();
+ let loaded: Result<BTreeMap<String, u32>, String> = load(&path).await;
+ assert!(loaded.is_err());
+}
+
+#[test]
+fn paths_live_under_the_workspace_integrations_dir() {
+ let config = Config {
+ workspace_dir: std::path::PathBuf::from("/ws"),
+ ..Config::default()
+ };
+ assert_eq!(
+ path(&config, IDENTITIES_FILE),
+ std::path::PathBuf::from("/ws/integrations/composio_identities.json")
+ );
+ assert_eq!(
+ path(&config, USER_SCOPES_FILE),
+ std::path::PathBuf::from("/ws/integrations/composio_user_scopes.json")
+ );
+}