use std::collections::HashMap;
use std::io;
use std::path::{Path, PathBuf};
use rusty_agent_runtime::journal::{ArtifactStore, FileArtifactStore};
use rusty_agent_runtime::memory::MemoryRecord;
use rusty_agent_runtime::record::PayloadRef;
use serde_json::Value;
pub(crate) fn dir(root: &Path) -> PathBuf {
root.join("memory")
}
pub(crate) fn artifacts_dir(root: &Path) -> PathBuf {
root.join("memory_artifacts")
}
pub(crate) fn artifact_store(root: &Path) -> FileArtifactStore {
FileArtifactStore::new(artifacts_dir(root))
}
pub(crate) async fn persist(root: &Path, scoped_id: &str, record: &MemoryRecord) -> io::Result<()> {
let dir = dir(root);
tokio::fs::create_dir_all(&dir).await?;
let bytes = serde_json::to_vec_pretty(record)
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
let path = dir.join(format!("{scoped_id}.json"));
if let Some(parent) = path.parent() {
tokio::fs::create_dir_all(parent).await?;
}
let tmp = dir.join(format!("{scoped_id}.tmp"));
tokio::fs::write(&tmp, bytes).await?;
tokio::fs::rename(&tmp, path).await
}
pub(crate) async fn remove(root: &Path, scoped_id: &str) -> Result<bool, String> {
let path = dir(root).join(format!("{scoped_id}.json"));
match tokio::fs::remove_file(&path).await {
Ok(()) => Ok(true),
Err(e) if e.kind() == io::ErrorKind::NotFound => Ok(false),
Err(e) => Err(format!("remove memory record {}: {e}", path.display())),
}
}
fn collect_json_files(root: &Path, out: &mut Vec<PathBuf>) {
let Ok(entries) = std::fs::read_dir(root) else {
return;
};
for entry in entries.flatten() {
let path = entry.path();
if path.is_dir() {
collect_json_files(&path, out);
} else if path.extension().and_then(|e| e.to_str()) == Some("json") {
out.push(path);
}
}
}
pub(crate) fn load(root: &Path) -> HashMap<String, MemoryRecord> {
let dir = dir(root);
let mut out = HashMap::new();
let mut files = Vec::new();
collect_json_files(&dir, &mut files);
for path in files {
let scoped_id = path
.strip_prefix(&dir)
.ok()
.map(|relative| relative.with_extension(""))
.map(|relative| {
relative
.components()
.map(|component| component.as_os_str().to_string_lossy().into_owned())
.collect::<Vec<_>>()
.join("/")
});
let parsed = std::fs::read_to_string(&path)
.ok()
.and_then(|raw| serde_json::from_str::<MemoryRecord>(&raw).ok());
match (scoped_id, parsed) {
(Some(id), Some(record)) => {
out.insert(id, record);
}
_ => {
tracing::warn!(path = %path.display(), "skipping unreadable memory file")
}
}
}
out
}
pub(crate) async fn spill_content(
artifacts: &(dyn ArtifactStore + '_),
record: &MemoryRecord,
content: &Value,
) -> Result<(), String> {
let PayloadRef::Artifact(reference) = &record.content else {
return Ok(());
};
let bytes = serde_json::to_vec(content)
.map_err(|e| format!("serialize memory content for artifact spill: {e}"))?;
let stored = artifacts
.put(&bytes)
.await
.map_err(|e| format!("persist memory artifact: {e}"))?;
if stored.sha256 != reference.sha256 {
return Err(format!(
"memory artifact address mismatch: the record references {} but the content \
hashes to {} — the content is not the value the address was minted from",
reference.sha256, stored.sha256
));
}
Ok(())
}
pub(crate) async fn resolve_content(
artifacts: &(dyn ArtifactStore + '_),
record: &mut MemoryRecord,
) -> Result<(), String> {
let PayloadRef::Artifact(reference) = &record.content else {
return Ok(());
};
let bytes = artifacts
.get(&reference.sha256)
.await
.map_err(|e| format!("resolve memory artifact {}: {e}", reference.sha256))?;
let value: Value = serde_json::from_slice(&bytes).map_err(|e| {
format!(
"memory artifact {} did not hold a JSON value: {e}",
reference.sha256
)
})?;
record.content = PayloadRef::Inline(value);
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use chrono::Utc;
use rusty_agent_runtime::memory::{
MemoryKind, MemoryProvenance, MemoryScope, ProvenanceAuthor, ScopeAddress, ValidityWindow,
};
use serde_json::json;
fn record(content: Value) -> MemoryRecord {
let provenance = MemoryProvenance {
author: ProvenanceAuthor::Human {
human_id: "amjad".into(),
},
evidence: Default::default(),
written_at: Utc::now(),
};
MemoryRecord::new(
MemoryKind::Fact,
ScopeAddress::new(MemoryScope::User, "user-7"),
provenance,
1.0,
ValidityWindow::starting(Utc::now()),
Utc::now(),
content,
)
.unwrap()
}
#[tokio::test]
async fn records_round_trip_keyed_by_path_with_corrupt_tolerance() {
let root = std::env::temp_dir().join(format!("rusty-memory-test-{}", uuid::Uuid::new_v4()));
let small = record(json!({"timezone": "Asia/Dubai"}));
persist(&root, &small.memory_id, &small).await.unwrap();
let tenant_record = record(json!({"team": "qa"}));
persist(
&root,
&format!("acme/{}", tenant_record.memory_id),
&tenant_record,
)
.await
.unwrap();
std::fs::write(dir(&root).join("broken.json"), b"{nope").unwrap();
let loaded = load(&root);
assert_eq!(loaded.len(), 2, "corrupt files are skipped, not fatal");
assert!(
loaded.contains_key(&small.memory_id),
"default tenant: bare key"
);
let scoped = format!("acme/{}", tenant_record.memory_id);
assert_eq!(
loaded[&scoped].memory_id, tenant_record.memory_id,
"named tenant: the key comes from the path, the record keeps the bare address"
);
let _ = std::fs::remove_dir_all(root);
}
#[tokio::test]
async fn artifact_spill_and_resolve_keep_the_record_self_contained() {
let root = std::env::temp_dir().join(format!("rusty-memory-test-{}", uuid::Uuid::new_v4()));
let big = json!({"blob": "x".repeat(9000)});
let record = record(big.clone());
assert!(matches!(record.content, PayloadRef::Artifact(_)));
let artifacts = artifact_store(&root);
spill_content(&artifacts, &record, &big).await.unwrap();
let mut resolved = record.clone();
resolve_content(&artifacts, &mut resolved).await.unwrap();
assert_eq!(resolved.content, PayloadRef::Inline(big));
assert_eq!(
resolved.memory_id, record.memory_id,
"the address survives resolution"
);
assert!(artifacts_dir(&root).exists());
assert!(!dir(&root).exists(), "no record was persisted here");
let _ = std::fs::remove_dir_all(root);
}
}