Memory: byte-budgeted index, memory_search (BM25), auto extraction on compaction/exit

This commit is contained in:
hermes committed 2026-11-30 12:46:16 +00:00
1 parent 59bf45f41d
commit 8705b128ea
8 files changed
+311 -39

No files matched your search

+46 -4
View File
@@ -64,6 +64,7 @@ pub fn registry() -> Registry {
.add(ExitPlan)
.add(MemoryWrite)
.add(MemoryForget)
.add(MemorySearch)
.add(skill::Skill);
r
}
@@ -483,7 +484,7 @@ impl Tool for Bash {
fn memory_dir(a: &Value, cx: &ToolCtx) -> PathBuf {
hx_core::memory::dir(
a["scope"].as_str() == Some("project"),
a["scope"].as_str() != Some("global"),
&hx_core::context::hx_home(),
&cx.cwd,
)
@@ -495,13 +496,13 @@ 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.",
"Save or replace a persistent memory topic (durable facts only, not task progress). Default scope is project. `hook` is the one-line index entry; `body` is the full text, found via memory_search or read on demand. Appears in the prompt index from the next session, searchable immediately.",
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"]}),
"required":["slug","hook","body"]}),
)
}
async fn call(&self, a: Value, cx: &ToolCtx) -> ToolResult {
@@ -518,6 +519,47 @@ impl Tool for MemoryWrite {
}
}
pub struct MemorySearch;
#[async_trait]
impl Tool for MemorySearch {
fn spec(&self) -> ToolSpec {
spec(
"memory_search",
"Search persistent memory (all topics, not just those in the prompt index). Use before asking the user about conventions/decisions or assuming them. Searches both scopes unless `scope` is given.",
json!({"type":"object","properties":{
"query":{"type":"string"},
"scope":{"type":"string","enum":["global","project"]}},
"required":["query"]}),
)
}
async fn call(&self, a: Value, cx: &ToolCtx) -> ToolResult {
let q = a["query"].as_str().unwrap_or_default();
let home = hx_core::context::hx_home();
let mut out = String::new();
for (name, project) in [("project", true), ("global", false)] {
if a["scope"].as_str().is_some_and(|s| s != name) {
continue;
}
let d = hx_core::memory::dir(project, &home, &cx.cwd);
for h in hx_core::memory::search(&d, q, 5) {
out.push_str(&format!(
"[{name}] {} – {}\n {}\n file: {}/topics/{}.md\n",
h.slug,
h.hook,
h.snippet,
d.display(),
h.slug
));
}
}
if out.is_empty() {
ToolResult::ok("no matching memory")
} else {
ToolResult::ok(out)
}
}
}
pub struct MemoryForget;
#[async_trait]
impl Tool for MemoryForget {
@@ -528,7 +570,7 @@ impl Tool for MemoryForget {
json!({"type":"object","properties":{
"scope":{"type":"string","enum":["global","project"]},
"slug":{"type":"string"}},
"required":["scope","slug"]}),
"required":["slug"]}),
)
}
async fn call(&self, a: Value, cx: &ToolCtx) -> ToolResult {