The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
feat(memory): add file memory tool write tests
Steven Enamakel445792bb694b
diff --git a/crates/openhuman-core/src/memory/files.rs b/crates/openhuman-core/src/memory/files.rsdeleted file mode 100644index 62ee38845b..0000000000--- a/crates/openhuman-core/src/memory/files.rs+++ /dev/null@@ -1,93 +0,0 @@-//! Owner-only writes of the local memory state files.-//!-//! The files under `<workspace>/memory/` (the job queue, channel threads,-//! source sync state, the layout migration state, the local install identity) hold ids and source-//! settings that are the person's own. `std::fs::write` creates them with-//! the process umask (0644 under the usual 022), readable by every local-//! account; here, on unix, a file is created (and kept) 0600 and its-//! directory 0700, as the config and keyring files already are. Other-//! platforms keep their default ACLs.--use std::fs::File;-use std::io::Write;-use std::path::Path;--/// Owner read/write only.-#[cfg(unix)]-pub const FILE_MODE: u32 = 0o600;--/// Owner read/write/search only.-#[cfg(unix)]-pub const DIR_MODE: u32 = 0o700;--/// Creates `dir` and any missing parent, each new one 0700 on unix, and-/// narrows `dir` itself to 0700 when it already existed.-///-/// # Errors-///-/// When a directory cannot be created or its mode set.-pub fn create_private_dir_all(dir: &Path) -> std::io::Result<()> {- #[cfg(unix)]- {- use std::os::unix::fs::{DirBuilderExt, PermissionsExt};- std::fs::DirBuilder::new()- .recursive(true)- .mode(DIR_MODE)- .create(dir)?;- let mode = std::fs::metadata(dir)?.permissions().mode() & 0o777;- if mode != DIR_MODE {- tracing::debug!(- dir = %dir.display(),- from = format!("{mode:o}"),- "[memory:files] narrowing a state directory to 0700"- );- std::fs::set_permissions(dir, std::fs::Permissions::from_mode(DIR_MODE))?;- }- Ok(())- }- #[cfg(not(unix))]- {- std::fs::create_dir_all(dir)- }-}--/// Creates (or truncates) `path` for writing, 0600 on unix whether it is-/// new or already existed with a wider mode.-///-/// # Errors-///-/// When the file cannot be opened or its mode set.-pub fn create_private(path: &Path) -> std::io::Result<File> {- let mut options = std::fs::OpenOptions::new();- options.write(true).create(true).truncate(true);- #[cfg(unix)]- {- use std::os::unix::fs::{OpenOptionsExt, PermissionsExt};- options.mode(FILE_MODE);- let file = options.open(path)?;- // `mode` applies only to a file this call creates.- file.set_permissions(std::fs::Permissions::from_mode(FILE_MODE))?;- Ok(file)- }- #[cfg(not(unix))]- {- options.open(path)- }-}--/// Writes `bytes` to `path` owner-only ([`create_private`]), creating its-/// directory owner-only ([`create_private_dir_all`]).-///-/// # Errors-///-/// When the directory or file cannot be created or written.-pub fn write_private(path: &Path, bytes: &[u8]) -> std::io::Result<()> {- if let Some(dir) = path.parent() {- create_private_dir_all(dir)?;- }- create_private(path)?.write_all(bytes)-}--#[cfg(test)]-#[path = "files_tests.rs"]-mod tests;diff --git a/crates/openhuman-core/src/memory/files_tests.rs b/crates/openhuman-core/src/memory/files_tests.rsdeleted file mode 100644index b23483d81e..0000000000--- a/crates/openhuman-core/src/memory/files_tests.rs+++ /dev/null@@ -1,87 +0,0 @@-use super::*;--#[cfg(unix)]-use std::os::unix::fs::PermissionsExt;--#[cfg(unix)]-fn mode(path: &Path) -> u32 {- std::fs::metadata(path).unwrap().permissions().mode() & 0o777-}--#[test]-fn write_private_writes_the_bytes() {- let tmp = tempfile::tempdir().unwrap();- let file = tmp.path().join("memory").join("state.json");- write_private(&file, b"{\"a\":1}").unwrap();- write_private(&file, b"{}").unwrap();- assert_eq!(- std::fs::read(&file).unwrap(),- b"{}",- "truncated, not appended"- );-}--#[cfg(unix)]-#[test]-fn a_new_file_is_0600_in_a_0700_directory() {- let tmp = tempfile::tempdir().unwrap();- let dir = tmp.path().join("memory").join("nested");- let file = dir.join("state.json");- write_private(&file, b"{}").unwrap();- assert_eq!(mode(&file), 0o600);- assert_eq!(mode(&dir), 0o700);- assert_eq!(mode(&tmp.path().join("memory")), 0o700);-}--#[cfg(unix)]-#[test]-fn an_existing_world_readable_file_and_directory_are_narrowed() {- let tmp = tempfile::tempdir().unwrap();- let dir = tmp.path().join("memory");- std::fs::create_dir_all(&dir).unwrap();- std::fs::set_permissions(&dir, std::fs::Permissions::from_mode(0o755)).unwrap();- let file = dir.join("jobs.json");- std::fs::write(&file, b"{}").unwrap();- std::fs::set_permissions(&file, std::fs::Permissions::from_mode(0o644)).unwrap();-- write_private(&file, b"{\"pending\":[]}").unwrap();- assert_eq!(mode(&file), 0o600);- assert_eq!(mode(&dir), 0o700);-}--#[cfg(unix)]-#[tokio::test]-async fn every_memory_state_file_is_owner_only() {- use tinymemory_api::{ConsolidateRequest, ItemKind, Namespace, Reach};-- use crate::memory::lifecycle::jobs;- use crate::memory::test_fixtures::config_in;- use tinymemory_tools::BackgroundJob;-- let tmp = tempfile::tempdir().unwrap();- let config = config_in(&tmp);- let workspace = config.workspace_dir.clone();-- crate::memory::channels::record(&workspace, "telegram", "thread-1");- crate::memory::sources::state::update(&workspace, "src-1", |_| {});- crate::memory::layout_migration::state::save(&workspace, &Default::default()).unwrap();- jobs::enqueue(- &config,- &Namespace::ROOT,- vec![BackgroundJob::BuildBeliefs {- request: ConsolidateRequest::new(Reach::exact(Namespace::agent("a")))- .kinds([ItemKind::Conversation]),- }],- )- .await;-- let dir = workspace.join("memory");- assert_eq!(mode(&dir), 0o700);- let mut seen = 0;- for entry in std::fs::read_dir(&dir).unwrap() {- let path = entry.unwrap().path();- assert_eq!(mode(&path), 0o600, "{}", path.display());- seen += 1;- }- assert!(seen >= 4, "every state file was written: {seen}");-}diff --git a/crates/openhuman-core/src/memory/tool_writes_tests.rs b/crates/openhuman-core/src/memory/tool_writes_tests.rsindex c04ec8e9f4..8eb208dd36 100644--- a/crates/openhuman-core/src/memory/tool_writes_tests.rs+++ b/crates/openhuman-core/src/memory/tool_writes_tests.rs@@ -89,7 +89,7 @@ fn persistence_failure_and_invalid_scope_never_acknowledge_a_write() {let args = json!({"action":"learn","text":"Prefers tea"});assert!(enqueue(&config, &args, &facts).is_err());assert!(!path(&config.workspace_dir).exists());- std::fs::write(config.workspace_dir.join("memory"), "blocks directory").unwrap();+ std::fs::write(config.workspace_dir.join("storage"), "blocks the storage directory").unwrap();assert!(enqueue(&config, &args, &self::facts(&config)).is_err());}@@ -105,25 +105,12 @@ fn private_outbox_is_scrubbed_before_persistence_and_has_a_size_limit() {&facts(&config),).unwrap();- let file = path(&config.workspace_dir);- let persisted = std::fs::read_to_string(&file).unwrap();+ let persisted = serde_json::to_string(&read(&config.workspace_dir).unwrap()).unwrap();assert!(!persisted.contains(secret));- #[cfg(unix)]- {- use std::os::unix::fs::PermissionsExt;- assert_eq!(- std::fs::metadata(&file).unwrap().permissions().mode() & 0o777,- 0o600- );- assert_eq!(- std::fs::metadata(file.parent().unwrap())- .unwrap()- .permissions()- .mode()- & 0o777,- 0o700- );- }+ assert!(+ !path(&config.workspace_dir).exists(),+ "the queue is a storage value, not a file"+ );let huge = "x".repeat(MAX_BYTES);assert!(enqueue(&config,@@ -240,7 +227,8 @@ fn corrupt_existing_outbox_is_preserved_and_refuses_new_writes() {let tmp = tempfile::tempdir().unwrap();let config = config_in(&tmp);let file = path(&config.workspace_dir);- super::super::files::write_private(&file, b"corrupt existing state").unwrap();+ std::fs::create_dir_all(file.parent().unwrap()).unwrap();+ std::fs::write(&file, b"corrupt existing state").unwrap();assert!(enqueue(&config,&json!({"action":"learn","text":"Prefers tea"}),