use std::{
io,
path::{Path, PathBuf},
};
use anyhow::Context as _;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use crate::{
datatype::Value,
tool::{ToolDesc, ToolDescBuilder, ToolFunc},
};
const FILE_NAME: &str = "MEMORY.md";
const LOCK_NAME: &str = ".MEMORY.md.lock";
const SEPARATOR: &str = "\n\n";
const STAMP_LEN: usize = "[YYYY-MM-DD] ".len();
const DEFAULT_LIMIT: usize = 3_000;
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct Memory {
dir: PathBuf,
#[serde(default = "default_limit")]
limit: usize,
}
fn default_limit() -> usize {
DEFAULT_LIMIT
}
impl Memory {
pub fn new(dir: impl Into<PathBuf>) -> Self {
Self {
dir: dir.into(),
limit: DEFAULT_LIMIT,
}
}
pub fn with_limit(mut self, chars: usize) -> Self {
self.limit = chars;
self
}
pub fn dir(&self) -> &Path {
&self.dir
}
pub fn limit(&self) -> usize {
self.limit
}
pub(super) fn prepared(&self) -> anyhow::Result<Self> {
let made = std::path::absolute(&self.dir).and_then(|dir| {
std::fs::create_dir_all(&dir)?;
Ok(dir)
});
let dir =
made.with_context(|| format!("making the memory folder {}", self.dir.display()))?;
Ok(Self {
dir,
limit: self.limit,
})
}
fn file(&self) -> PathBuf {
self.dir.join(FILE_NAME)
}
fn read(&self) -> io::Result<Vec<String>> {
match std::fs::read_to_string(self.file()) {
Ok(text) => Ok(parse_entries(&text)),
Err(e) if e.kind() == io::ErrorKind::NotFound => Ok(Vec::new()),
Err(e) => Err(e),
}
}
fn edit(&self, ops: &[Op]) -> Value {
let io_error = |e: io::Error| {
crate::to_value!({
"error": format!("the memory's MEMORY.md could not be read or written: {e}"),
"phase": "io",
})
};
let lock_path = self.dir.join(LOCK_NAME);
let lock = match std::fs::OpenOptions::new()
.create(true)
.truncate(false)
.write(true)
.open(&lock_path)
{
Ok(lock) => lock,
Err(e) => return io_error(e),
};
if let Err(e) = lock.lock() {
log::warn!(
"could not lock {}: {e}; editing the memory without waiting for other edits",
lock_path.display()
);
}
let current = match self.read() {
Ok(current) => current,
Err(e) => return io_error(e),
};
let entries = match edited(¤t, ops, self.limit, &today()) {
Ok(entries) => entries,
Err(reason) => {
return crate::to_value!({
"error": format!("{reason}. Nothing was changed."),
"phase": "validation",
"memory": shown(¤t, self.limit),
});
}
};
if entries != current
&& let Err(e) = self.write(&entries)
{
return io_error(e);
}
crate::to_value!({
"result": "saved",
"memory": shown(&entries, self.limit),
"note": "Your system prompt still shows the memory from the start of this conversation; this shows up from the next one.",
})
}
fn write(&self, entries: &[String]) -> io::Result<()> {
let mut text = entries.join(SEPARATOR);
if !text.is_empty() {
text.push('\n');
}
let mut temp = tempfile::NamedTempFile::new_in(&self.dir)?;
io::Write::write_all(&mut temp, text.as_bytes())?;
temp.as_file().sync_all()?;
temp.persist(self.file()).map_err(|e| e.error)?;
Ok(())
}
}
impl From<PathBuf> for Memory {
fn from(dir: PathBuf) -> Self {
Self::new(dir)
}
}
impl From<&Path> for Memory {
fn from(dir: &Path) -> Self {
Self::new(dir)
}
}
impl From<String> for Memory {
fn from(dir: String) -> Self {
Self::new(dir)
}
}
impl From<&str> for Memory {
fn from(dir: &str) -> Self {
Self::new(dir)
}
}
pub(super) fn render_memory(memory: &Memory) -> anyhow::Result<String> {
let entries = memory
.read()
.with_context(|| format!("reading {}", memory.file().display()))?;
Ok(format!(
"# Memory\n\n\
Your memory of this user, kept with the `memory` tool across conversations, each \
entry led by the date it was last written. It was loaded when this conversation \
started; your changes show up next time.\n\n\
[{}/{} chars]\n{}",
used(&entries),
memory.limit,
if entries.is_empty() {
"(empty)".to_string()
} else {
entries.join(SEPARATOR)
}
))
}
pub(super) fn memory_tool(memory: &Memory) -> (ToolDesc, ToolFunc) {
let memory = memory.clone();
let func = crate::tool_func!(async |args: Value| -> Value with [memory = memory.clone()] {
let ops = match ops(&args) {
Ok(ops) => ops,
Err(error) => return crate::to_value!({ "error": error, "phase": "validation" }),
};
match tokio::task::spawn_blocking(move || memory.edit(&ops)).await {
Ok(out) => out,
Err(e) => crate::to_value!({ "error": e.to_string(), "phase": "io" }),
}
});
(memory_tool_desc(), func)
}
fn memory_tool_desc() -> ToolDesc {
ToolDescBuilder::new("memory")
.description(concat!(
"Keep short notes that are put in your system prompt at the start of every future ",
"conversation: what you learn about the user that would still help in a later ",
"conversation (who they are, how they like to be helped, their circumstances and ",
"plans) and your own notes (agreements, what to avoid, lessons learned). A fact the ",
"user mentions in passing counts, even inside a request about something else. Leave ",
"out what only this conversation needs: the request itself, details and progress. ",
"Each entry is one fact: a new fact is a new entry, even about the same ",
"user. When a fact changes, replace that fact's entry instead of adding another, and ",
"never rewrite an entry to fit another fact into it. Each entry starts with the date ",
"it was last written, as [YYYY-MM-DD], which is added for you: write only the fact, ",
"and use the dates to tell what may be out of date. ",
"The memory has a size limit; when it is full, make room in the same call: shorten ",
"entries first, and remove one only when shortening is not enough, then tell the user ",
"what you removed. The changes in one call are made in order, all or none. ",
"Your system prompt keeps the memory as it was when this conversation started; ",
"changes show up from the next one.",
))
.parameters(crate::to_value!({
"type": "object",
"properties": {
"operations": {
"type": "array",
"description": "The changes to make, in order; one change is a list of one.",
"items": {
"type": "object",
"properties": {
"action": {
"type": "string",
"enum": ["add", "replace", "remove"],
"description": "'add' a new fact as its own entry; 'replace' the entry containing old_text with content, when its fact changed; 'remove' the entry containing old_text."
},
"content": {
"type": "string",
"description": "One fact, without a date: the entry to add, or the whole new text of the replaced entry."
},
"old_text": {
"type": "string",
"description": "A short piece of the entry to replace or remove, long enough that only that entry contains it."
}
},
"required": ["action"]
}
}
},
"required": ["operations"]
}))
.build()
}
enum Op {
Add { content: String },
Replace { old_text: String, content: String },
Remove { old_text: String },
}
fn ops(args: &Value) -> Result<Vec<Op>, String> {
let ops = match args.pointer("/operations") {
Some(Value::Array(ops)) if !ops.is_empty() => ops,
_ => return Err("operations must be a non-empty array of changes".into()),
};
ops.iter().map(op).collect()
}
fn op(value: &Value) -> Result<Op, String> {
let field = |name: &str| value.pointer(&format!("/{name}")).and_then(|v| v.as_str());
let required = |name: &'static str| {
field(name)
.map(str::to_string)
.ok_or(format!("missing parameter: {name}"))
};
Ok(match field("action") {
Some("add") => Op::Add {
content: required("content")?,
},
Some("replace") => Op::Replace {
old_text: required("old_text")?,
content: required("content")?,
},
Some("remove") => Op::Remove {
old_text: required("old_text")?,
},
other => {
return Err(format!(
"action must be add, replace or remove, not {other:?}"
));
}
})
}
fn shown(entries: &[String], limit: usize) -> Value {
crate::to_value!({
"usage": format!("{}/{limit} chars", used(entries)),
"entries": Value::array(entries.to_vec()),
})
}
pub(super) fn today() -> String {
chrono::Local::now().format("%Y-%m-%d").to_string()
}
fn used(entries: &[String]) -> usize {
entries.join(SEPARATOR).chars().count()
}
fn edited(
current: &[String],
ops: &[Op],
limit: usize,
today: &str,
) -> Result<Vec<String>, String> {
let mut entries = current.to_vec();
for op in ops {
match op {
Op::Add { content } => entries.push(entry(content, today)?),
Op::Replace { old_text, content } => {
let content = entry(content, today)?;
let i = find(&entries, old_text)?;
entries[i] = content;
}
Op::Remove { old_text } => {
let i = find(&entries, old_text)?;
entries.remove(i);
}
}
}
let size = used(&entries);
if size > limit && size > used(current) {
return Err(format!(
"the memory would be {size} characters, over its limit of {limit}; shorten \
entries in the same call, or remove one if that is not enough"
));
}
Ok(entries)
}
fn parse_entries(text: &str) -> Vec<String> {
let mut entries = vec![String::new()];
for line in text.lines() {
let current = entries.last_mut().expect("never empty");
if line.trim().is_empty() {
entries.push(String::new());
} else {
if !current.is_empty() {
current.push('\n');
}
current.push_str(line);
}
}
entries
.into_iter()
.map(|e| e.trim().to_string())
.filter(|e| !e.is_empty())
.collect()
}
fn entry(content: &str, today: &str) -> Result<String, String> {
let content = content
.lines()
.filter(|line| !line.trim().is_empty())
.collect::<Vec<_>>()
.join("\n");
let content = body(content.trim()).trim();
if content.is_empty() {
return Err("an entry cannot be empty".into());
}
Ok(format!("[{today}] {content}"))
}
fn body(entry: &str) -> &str {
let b = entry.as_bytes();
let stamped = b.len() >= STAMP_LEN
&& b[0] == b'['
&& b[11] == b']'
&& b[12] == b' '
&& b[5] == b'-'
&& b[8] == b'-'
&& [1, 2, 3, 4, 6, 7, 9, 10]
.iter()
.all(|&i| b[i].is_ascii_digit());
if stamped { &entry[STAMP_LEN..] } else { entry }
}
fn find(entries: &[String], old_text: &str) -> Result<usize, String> {
let needle = squash(body(old_text.trim()));
if needle.is_empty() {
return Err("old_text cannot be empty".into());
}
let bodies: Vec<String> = entries.iter().map(|e| squash(body(e))).collect();
if let Some(i) = bodies.iter().position(|b| *b == needle) {
return Ok(i);
}
let hits: Vec<usize> = (0..bodies.len())
.filter(|&i| bodies[i].contains(&needle))
.collect();
match hits[..] {
[i] => Ok(i),
[] => Err(format!("no entry contains {old_text:?}")),
_ => Err(format!(
"{} entries contain {old_text:?}; give more of the entry, or all of it, so that \
only one matches",
hits.len()
)),
}
}
fn squash(text: &str) -> String {
text.split_whitespace().collect::<Vec<_>>().join(" ")
}
#[cfg(test)]
mod tests {
use futures::StreamExt as _;
use super::*;
use crate::{message::Message, to_value};
fn add(content: &str) -> Op {
Op::Add {
content: content.into(),
}
}
fn replace(old_text: &str, content: &str) -> Op {
Op::Replace {
old_text: old_text.into(),
content: content.into(),
}
}
fn remove(old_text: &str) -> Op {
Op::Remove {
old_text: old_text.into(),
}
}
const DAY: &str = "2026-11-17";
fn dated(text: &str) -> String {
format!("[{DAY}] {text}")
}
fn entries(texts: &[&str]) -> Vec<String> {
texts.iter().map(|s| s.to_string()).collect()
}
fn apply(start: &[String], ops: &[Op]) -> Result<Vec<String>, String> {
edited(start, ops, DEFAULT_LIMIT, DAY)
}
fn fresh() -> Memory {
Memory::new(tempfile::tempdir().unwrap().keep()).with_limit(100)
}
fn value(msg: &Message) -> Value {
msg.contents[0].as_value().unwrap().clone()
}
async fn call(func: &ToolFunc, args: Value) -> Value {
let output = func
.call_pure(args, "1")
.expect("a pure tool")
.next()
.await
.unwrap();
value(&output.message)
}
fn phase(out: &Value) -> Option<&str> {
out.pointer("/phase").and_then(|v| v.as_str())
}
fn saved(out: &Value) -> bool {
out.pointer("/result").and_then(|v| v.as_str()) == Some("saved")
}
#[test]
fn test_entries_round_trip_through_the_file_format() {
let texts = entries(&["Name is Minsu", "Two\nlines"]);
let file = texts.join(SEPARATOR) + "\n";
assert_eq!(parse_entries(&file), texts);
assert_eq!(parse_entries("a\n \nb\n\n\n\nc\n"), ["a", "b", "c"]);
assert!(parse_entries("").is_empty());
}
#[test]
fn test_a_memory_reads_from_a_spec_with_its_default_limit() {
let memory: Memory = serde_json::from_value(serde_json::json!({ "dir": "/m" })).unwrap();
assert_eq!(memory, Memory::new("/m"));
let memory: Memory =
serde_json::from_value(serde_json::json!({ "dir": "/m", "limit": 500 })).unwrap();
assert_eq!(memory.limit(), 500);
assert!(
serde_json::from_value::<Memory>(serde_json::json!({ "dir": "/m", "limit": -1 }))
.is_err()
);
}
#[test]
fn test_edits_add_replace_and_remove_by_old_text() {
let start = entries(&["Name is Minsu", "Drinks tea"]);
assert!(apply(&start, &[replace("drinks TEA", "x")]).is_err());
let after = apply(
&start,
&[
replace("Drinks\n tea", "Drinks only oat-milk lattes"),
remove("Minsu"),
add("Prefers short answers\n\n \nin Korean"),
],
)
.unwrap();
assert_eq!(
after,
[
dated("Drinks only oat-milk lattes"),
dated("Prefers short answers\nin Korean")
]
);
}
#[test]
fn test_entries_are_led_by_the_day_they_were_written() {
let start = entries(&["Lives in Seoul"]);
let after = apply(
&start,
&[
replace("Seoul", "[2026-11-10] Lives in Busan"),
add("Married in (2027-03)"),
],
)
.unwrap();
assert_eq!(
after,
[dated("Lives in Busan"), dated("Married in (2027-03)")]
);
assert_eq!(body(&dated("Drinks tea")), "Drinks tea");
assert_eq!(body("[2026-1-10] Drinks tea"), "[2026-1-10] Drinks tea");
assert_eq!(body("Drinks tea"), "Drinks tea");
}
#[test]
fn test_an_edit_that_cannot_be_made_changes_nothing() {
let start = entries(&["Lives in Seoul", "Works in Seoul"]);
let e = apply(&start, &[add("new"), remove("Seoul")]).unwrap_err();
assert!(e.contains("2 entries"), "{e}");
for bad in [add(" "), add("\n \n"), remove(" "), remove("Busan")] {
assert!(apply(&start, &[bad]).is_err());
}
}
#[test]
fn test_old_text_is_matched_on_the_entries_without_their_dates() {
let start = entries(&[
&dated("Lives in Seoul, Gangnam"),
&dated("Likes tea"),
&dated("Likes tea with milk"),
]);
assert_eq!(
apply(
&start,
&[replace("[2026-11-10] Lives in Seoul", "Lives in Busan")]
)
.unwrap()[0],
dated("Lives in Busan")
);
assert!(apply(&start, &[remove("[2026-11-10] Lives in Daegu")]).is_err());
assert!(apply(&start, &[remove(DAY)]).is_err());
assert!(apply(&start, &[remove("tea")]).is_err());
assert_eq!(
apply(&start, &[replace(" Likes tea", "Likes coffee")]).unwrap()[1..],
[dated("Likes coffee"), dated("Likes tea with milk")]
);
assert_eq!(
apply(&start, &[remove(&dated("Likes tea"))]).unwrap()[1..],
[dated("Likes tea with milk")]
);
let twice = entries(&["Drinks tea", "Drinks tea"]);
assert_eq!(
apply(&twice, &[remove("Drinks tea")]).unwrap(),
["Drinks tea"]
);
}
#[test]
fn test_the_limit_applies_to_the_result_and_only_to_growth() {
let start = entries(&[&"a".repeat(95)]);
let e = edited(&start, &[add("bbbbb")], 100, DAY).unwrap_err();
assert!(e.contains("over its limit of 100"), "{e}");
assert_eq!(
edited(&start, &[remove("aaa"), add("bbbbb")], 100, DAY).unwrap(),
[dated("bbbbb")]
);
let over = entries(&[&"a".repeat(80), &"b".repeat(80)]);
assert!(edited(&over, &[replace("aaa", "a")], 100, DAY).is_ok());
assert!(edited(&over, &[add("c")], 100, DAY).is_err());
}
#[test]
fn test_render_shows_the_entries_and_how_full_the_file_is() {
let memory = fresh().with_limit(500);
assert!(
render_memory(&memory)
.unwrap()
.ends_with("[0/500 chars]\n(empty)")
);
std::fs::write(memory.file(), "Name is Minsu\n\nDrinks tea\n").unwrap();
let text = render_memory(&memory).unwrap();
assert!(text.starts_with("# Memory\n"), "{text}");
assert!(text.contains("`memory` tool"), "{text}");
assert!(
text.ends_with("[25/500 chars]\nName is Minsu\n\nDrinks tea"),
"{text}"
);
}
#[test]
fn test_prepared_makes_the_folders_but_not_the_file() {
let dir = tempfile::tempdir().unwrap();
let folder = dir.path().join("agent1/agent2");
let memory = Memory::new(&folder).prepared().unwrap();
assert!(folder.is_dir());
assert_eq!(memory.file(), folder.join("MEMORY.md"));
assert!(!memory.file().exists());
assert!(memory.dir().is_absolute());
let blocked = dir.path().join("a-file");
std::fs::write(&blocked, "").unwrap();
assert!(Memory::new(&blocked).prepared().is_err());
}
#[tokio::test]
async fn test_memory_tool_edits_the_file() {
let memory = fresh();
let (desc, func) = memory_tool(&memory);
assert_eq!(desc.name, "memory");
assert!(!memory.file().exists());
let out = call(
&func,
to_value!({ "operations": [{ "action": "add", "content": "- Name is Minsu" }] }),
)
.await;
assert!(saved(&out), "{out:?}");
assert_eq!(
out.pointer("/memory/usage").unwrap().as_str(),
Some("28/100 chars")
);
let out = call(
&func,
to_value!({ "operations": [
{ "action": "replace", "old_text": "Minsu", "content": "Name is Minsu Kim" },
{ "action": "add", "content": "Answer briefly" }
] }),
)
.await;
assert!(saved(&out), "{out:?}");
let day = today();
assert_eq!(
std::fs::read_to_string(memory.file()).unwrap(),
format!("[{day}] Name is Minsu Kim\n\n[{day}] Answer briefly\n")
);
}
#[tokio::test]
async fn test_memory_tool_refusal_shows_the_entries() {
let memory = fresh();
let (_, func) = memory_tool(&memory);
call(
&func,
to_value!({ "operations": [{ "action": "add", "content": "Drinks tea" }] }),
)
.await;
let tea = format!("[{}] Drinks tea", today());
for op in [
to_value!({ "action": "remove", "old_text": "coffee" }),
to_value!({ "action": "add", "content": "x".repeat(100) }),
] {
let out = call(&func, to_value!({ "operations": [op] })).await;
assert_eq!(phase(&out), Some("validation"), "{out:?}");
assert_eq!(
out.pointer("/memory/entries/0").unwrap().as_str(),
Some(tea.as_str())
);
}
for args in [
to_value!({}),
to_value!({ "action": "add", "content": "x" }),
to_value!({ "operations": [] }),
to_value!({ "operations": "[]" }),
to_value!({ "operations": [{ "action": "add" }] }),
to_value!({ "operations": [{ "action": "append", "content": "x" }] }),
] {
let out = call(&func, args.clone()).await;
assert_eq!(phase(&out), Some("validation"), "{args:?}");
}
assert_eq!(memory.read().unwrap(), [tea]);
}
#[test]
fn test_edits_at_once_all_land() {
let memory = fresh().with_limit(DEFAULT_LIMIT);
let threads: Vec<_> = (0..8)
.map(|i| {
let memory = memory.clone();
std::thread::spawn(move || memory.edit(&[add(&format!("Fact {i}"))]))
})
.collect();
for t in threads {
assert!(saved(&t.join().unwrap()));
}
let mut got: Vec<String> = memory
.read()
.unwrap()
.iter()
.map(|e| body(e).to_string())
.collect();
got.sort();
assert_eq!(got, (0..8).map(|i| format!("Fact {i}")).collect::<Vec<_>>());
}
}