Agent memory: MEMORY.md index in prompt, lazy topic files, memory_write/memory_forget

This commit is contained in:
hermes committed 2026-10-06 12:57:26 +00:00
1 parent 8669c50fb9
commit 9b6eb9bee1
7 files changed
+228 -2

No files matched your search

+73 -1
View File
@@ -57,7 +57,13 @@ fn tail(s: &str, max: usize) -> &str {
pub fn registry() -> Registry {
let mut r = Registry::default();
r.add(Read).add(Write).add(Edit).add(Bash).add(AskUser);
r.add(Read)
.add(Write)
.add(Edit)
.add(Bash)
.add(AskUser)
.add(MemoryWrite)
.add(MemoryForget);
r
}
@@ -377,6 +383,60 @@ impl Tool for Bash {
}
}
fn memory_dir(a: &Value, cx: &ToolCtx) -> PathBuf {
hx_core::memory::dir(
a["scope"].as_str() == Some("project"),
&hx_core::context::hx_home(),
&cx.cwd,
)
}
pub struct MemoryWrite;
#[async_trait]
impl Tool for MemoryWrite {
fn spec(&self) -> ToolSpec {
spec(
"memory_write",
"Save or replace a persistent memory topic (durable facts only, not task progress). `hook` is the one-line index entry shown in every future session; `body` is the full text, read on demand. Appears in the prompt from the next session.",
json!({"type":"object","properties":{
"scope":{"type":"string","enum":["global","project"],"description":"global: about the user/environment; project: this repo"},
"slug":{"type":"string","description":"a-z 0-9 - _"},
"hook":{"type":"string","description":"one line, max 100 chars"},
"body":{"type":"string"}},
"required":["scope","slug","hook","body"]}),
)
}
async fn call(&self, a: Value, cx: &ToolCtx) -> ToolResult {
let g = |k: &str| a[k].as_str().unwrap_or_default();
match hx_core::memory::write(&memory_dir(&a, cx), g("slug"), g("hook"), g("body")) {
Ok(()) => ToolResult::ok(format!("saved memory '{}'", g("slug"))),
Err(e) => ToolResult::err(e),
}
}
}
pub struct MemoryForget;
#[async_trait]
impl Tool for MemoryForget {
fn spec(&self) -> ToolSpec {
spec(
"memory_forget",
"Delete a memory topic and its index line.",
json!({"type":"object","properties":{
"scope":{"type":"string","enum":["global","project"]},
"slug":{"type":"string"}},
"required":["scope","slug"]}),
)
}
async fn call(&self, a: Value, cx: &ToolCtx) -> ToolResult {
let slug = a["slug"].as_str().unwrap_or_default();
match hx_core::memory::forget(&memory_dir(&a, cx), slug) {
Ok(()) => ToolResult::ok(format!("forgot memory '{slug}'")),
Err(e) => ToolResult::err(e),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
@@ -389,6 +449,18 @@ mod tests {
}
}
#[tokio::test]
async fn memory_tools_write_and_forget() {
let t = tempfile::tempdir().unwrap();
let c = cx(t.path());
let a = json!({"scope":"project","slug":"x","hook":"h","body":"b"});
assert!(!MemoryWrite.call(a, &c).await.is_error);
assert!(t.path().join(".hx/memory/topics/x.md").exists());
let f = json!({"scope":"project","slug":"x"});
assert!(!MemoryForget.call(f.clone(), &c).await.is_error);
assert!(MemoryForget.call(f, &c).await.is_error);
}
#[tokio::test]
async fn ask_user_paths() {
let d = tempfile::tempdir().unwrap();