Skip to content

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

fix(memory): never lose a connection's recorded roots to a transient read error

M3gA-Mind2c4b89f50a1e
diff --git a/crates/openhuman-core/src/memory/sources/composio.rs b/crates/openhuman-core/src/memory/sources/composio.rs
index b6deec8d9b..4544d7d80c 100644
--- a/crates/openhuman-core/src/memory/sources/composio.rs
+++ b/crates/openhuman-core/src/memory/sources/composio.rs
@@ -191,7 +191,11 @@ pub async fn forget_connection(
};
let recorded = super::roots::of(&config.workspace_dir, connection_id);
let reaches = match toolkit {
- Some(toolkit) => toolkit_reaches(config, toolkit, &recorded)?,
+ // An unreadable roots record cannot bound the search: search all.
+ Some(toolkit) => match &recorded {
+ Some(recorded) => toolkit_reaches(config, toolkit, recorded)?,
+ None => Vec::new(),
+ },
None => {
tracing::warn!(
connection_id = %connection_id,
@@ -234,7 +238,9 @@ pub async fn forget_connection(
.await?
.forgotten;
}
- super::roots::forget(&config.workspace_dir, connection_id, &recorded);
+ if let Some(recorded) = &recorded {
+ super::roots::forget(&config.workspace_dir, connection_id, recorded);
+ }
Ok(forgotten)
}
diff --git a/crates/openhuman-core/src/memory/sources/composio_tests.rs b/crates/openhuman-core/src/memory/sources/composio_tests.rs
index ed2dd3ba00..e1c248d1cd 100644
--- a/crates/openhuman-core/src/memory/sources/composio_tests.rs
+++ b/crates/openhuman-core/src/memory/sources/composio_tests.rs
@@ -258,7 +258,9 @@ async fn forget_connection_reads_every_root_its_items_were_filed_under() {
.unwrap();
}
assert_eq!(
- super::super::roots::of(&config.workspace_dir, "conn-c").len(),
+ super::super::roots::of(&config.workspace_dir, "conn-c")
+ .unwrap()
+ .len(),
2
);
@@ -272,7 +274,9 @@ async fn forget_connection_reads_every_root_its_items_were_filed_under() {
let left = stored(&engine, MetaFilter::default()).await;
assert_eq!(left.len(), 1);
assert!(left[0].meta.tags.contains(&"connection:conn-d".to_string()));
- assert!(super::super::roots::of(&config.workspace_dir, "conn-c").is_empty());
+ assert!(super::super::roots::of(&config.workspace_dir, "conn-c")
+ .unwrap()
+ .is_empty());
}
#[tokio::test]
diff --git a/crates/openhuman-core/src/memory/sources/roots.rs b/crates/openhuman-core/src/memory/sources/roots.rs
index 1397e27116..0d9da66897 100644
--- a/crates/openhuman-core/src/memory/sources/roots.rs
+++ b/crates/openhuman-core/src/memory/sources/roots.rs
@@ -20,22 +20,20 @@ fn path(workspace_dir: &Path) -> PathBuf {
workspace_dir.join("memory").join("connection_roots.json")
}
-/// The file's contents. A file that exists but cannot be read is set aside
-/// (`.corrupt`) with a warning rather than overwritten.
-fn load(workspace_dir: &Path) -> Roots {
+/// The file's contents. A read error is returned and nothing is changed, so
+/// a file that is only unreadable for now is never replaced. A file that
+/// reads but does not parse is set aside (`.corrupt`) with a warning.
+fn load(workspace_dir: &Path) -> std::io::Result<Roots> {
let file = path(workspace_dir);
let text = match std::fs::read_to_string(&file) {
Ok(text) => text,
- Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Roots::new(),
- Err(error) => {
- tracing::warn!(error = %error, "[memory:sources] connection roots unreadable");
- return set_aside(&file);
- }
+ Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(Roots::new()),
+ Err(error) => return Err(error),
};
- serde_json::from_str(&text).unwrap_or_else(|error| {
+ Ok(serde_json::from_str(&text).unwrap_or_else(|error| {
tracing::warn!(error = %error, "[memory:sources] connection roots unparsable");
set_aside(&file)
- })
+ }))
}
fn set_aside(file: &Path) -> Roots {
@@ -54,7 +52,11 @@ fn save(workspace_dir: &Path, all: &Roots) -> std::io::Result<()> {
std::fs::create_dir_all(parent)?;
}
let json = serde_json::to_vec_pretty(all).map_err(std::io::Error::other)?;
- std::fs::write(&temp, json)?;
+ let mut out = std::fs::File::create(&temp)?;
+ std::io::Write::write_all(&mut out, &json)?;
+ // On disk before it replaces the old file, so a power loss cannot
+ // leave a renamed file with unwritten contents.
+ out.sync_all()?;
std::fs::rename(&temp, &file)
}
@@ -71,7 +73,7 @@ fn locked() -> std::sync::MutexGuard<'static, ()> {
/// a root a later forget would not know.
pub fn record(workspace_dir: &Path, connection_id: &str, root: &str) -> std::io::Result<()> {
let _guard = locked();
- let mut all = load(workspace_dir);
+ let mut all = load(workspace_dir)?;
if all
.entry(connection_id.to_string())
.or_default()
@@ -82,20 +84,27 @@ pub fn record(workspace_dir: &Path, connection_id: &str, root: &str) -> std::io:
Ok(())
}
-/// Every root `connection_id` filed items under.
+/// Every root `connection_id` filed items under, or `None` when the record
+/// cannot be read: the caller must then search everywhere.
#[must_use]
-pub fn of(workspace_dir: &Path, connection_id: &str) -> BTreeSet<String> {
+pub fn of(workspace_dir: &Path, connection_id: &str) -> Option<BTreeSet<String>> {
let _guard = locked();
- load(workspace_dir)
- .remove(connection_id)
- .unwrap_or_default()
+ match load(workspace_dir) {
+ Ok(mut all) => Some(all.remove(connection_id).unwrap_or_default()),
+ Err(error) => {
+ tracing::warn!(error = %error, "[memory:sources] connection roots unreadable");
+ None
+ }
+ }
}
/// Forgets `roots` of `connection_id`, once its items under them are gone.
/// A root recorded meanwhile (a sync during the forget) is kept.
pub fn forget(workspace_dir: &Path, connection_id: &str, roots: &BTreeSet<String>) {
let _guard = locked();
- let mut all = load(workspace_dir);
+ let Ok(mut all) = load(workspace_dir) else {
+ return;
+ };
let Some(held) = all.get_mut(connection_id) else {
return;
};
diff --git a/crates/openhuman-core/src/memory/sources/roots_tests.rs b/crates/openhuman-core/src/memory/sources/roots_tests.rs
index 063d4ee88a..9467a81a27 100644
--- a/crates/openhuman-core/src/memory/sources/roots_tests.rs
+++ b/crates/openhuman-core/src/memory/sources/roots_tests.rs
@@ -5,13 +5,16 @@ fn forgetting_keeps_a_root_recorded_meanwhile() {
let tmp = tempfile::tempdir().unwrap();
let dir = tmp.path();
record(dir, "c", "root").unwrap();
- let seen = of(dir, "c");
+ let seen = of(dir, "c").unwrap();
// A sync records another root while the forget runs.
record(dir, "c", "team:new").unwrap();
forget(dir, "c", &seen);
- assert_eq!(of(dir, "c"), BTreeSet::from(["team:new".to_string()]));
- forget(dir, "c", &of(dir, "c"));
- assert!(of(dir, "c").is_empty());
+ assert_eq!(
+ of(dir, "c").unwrap(),
+ BTreeSet::from(["team:new".to_string()])
+ );
+ forget(dir, "c", &of(dir, "c").unwrap());
+ assert!(of(dir, "c").unwrap().is_empty());
}
#[test]
@@ -21,13 +24,13 @@ fn an_unparsable_file_is_set_aside_not_overwritten() {
let file = path(dir);
std::fs::create_dir_all(file.parent().unwrap()).unwrap();
std::fs::write(&file, "{ not json").unwrap();
- assert!(of(dir, "c").is_empty());
+ assert!(of(dir, "c").unwrap().is_empty());
assert_eq!(
std::fs::read_to_string(file.with_extension("json.corrupt")).unwrap(),
"{ not json"
);
record(dir, "c", "root").unwrap();
- assert_eq!(of(dir, "c").len(), 1, "recording resumed");
+ assert_eq!(of(dir, "c").unwrap().len(), 1, "recording resumed");
}
#[test]
@@ -37,3 +40,20 @@ fn a_record_that_cannot_be_written_is_an_error() {
std::fs::write(tmp.path().join("memory"), "").unwrap();
assert!(record(tmp.path(), "c", "root").is_err());
}
+
+#[test]
+fn an_unreadable_file_is_left_alone_and_reported() {
+ let tmp = tempfile::tempdir().unwrap();
+ let dir = tmp.path();
+ record(dir, "c", "root").unwrap();
+ // A directory where the file should be cannot be read as text.
+ let file = path(dir);
+ std::fs::remove_file(&file).unwrap();
+ std::fs::create_dir(&file).unwrap();
+ assert!(of(dir, "c").is_none(), "the caller must search everywhere");
+ assert!(
+ record(dir, "c", "team:new").is_err(),
+ "nothing is filed unrecorded"
+ );
+ assert!(file.is_dir(), "nothing was set aside or overwritten");
+}
diff --git a/tests/memory_v2_e2e.rs b/tests/memory_v2_e2e.rs
index 4d2a960ecf..a80e32dd53 100644
--- a/tests/memory_v2_e2e.rs
+++ b/tests/memory_v2_e2e.rs
@@ -279,6 +279,10 @@ impl Fixture {
return result;
}
tokio::time::sleep(pause).await;
+ // A late wake can still cross it.
+ if std::time::Instant::now() >= deadline {
+ return result;
+ }
}
}