Memory: no cap, BM25 search, automatic extraction #238

Merged
hermes merged 2 commits from memory-search into main 2026-11-30 14:39:30 +00:00
Collaborator
  • Remove 50-entry cap (ADR 0013 superseded by 0035): prompt index = most recently used hooks, 2 KB per scope, +N more.
  • New memory_search tool (BM25 over slug/hook/body, bumps recency).
  • Prompt lists concrete write triggers; default scope project.
  • One extra model call on compaction and on exit extracts max 5 facts, saved automatically.

Tests: unit test for cap removal/search/extraction parse; all.sh green except tests/schema_e2e.sh, which also fails on main (JSON control char).
Embeddings: follow-up PR.

- Remove 50-entry cap (ADR 0013 superseded by 0035): prompt index = most recently used hooks, 2 KB per scope, `+N more`. - New `memory_search` tool (BM25 over slug/hook/body, bumps recency). - Prompt lists concrete write triggers; default scope project. - One extra model call on compaction and on exit extracts max 5 facts, saved automatically. Tests: unit test for cap removal/search/extraction parse; all.sh green except tests/schema_e2e.sh, which also fails on main (JSON control char). Embeddings: follow-up PR.
hermes added 1 commit 2026-11-30 12:46:18 +00:00

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
🧪 PR contains tests
🔒 Security concerns

Prompt injection to persistent memory:
extract_memory_now and compaction extraction write model-generated facts from the transcript (which may include untrusted tool output or file contents) to memory without user confirmation. These are injected into the system prompt of all later sessions, so malicious content can persist and influence future behavior. Add content limits, and consider showing or confirming saved entries, or marking auto-extracted entries as untrusted.

⚡ Recommended focus areas for review

Blocking Exit Call

extract_memory_now() runs after ratatui::restore() and awaits a full model call on exit with no timeout or cancellation. If the network is slow or down, or the user hits Ctrl-C, the terminal appears hung. Also, agent.lock().await may block if a turn is still running. Consider a timeout and making the extraction skippable. The extraction also silently discards failures, so users get no signal when memory was not saved.

eprintln!("[saving memory…]");
let saved = agent.lock().await.extract_memory_now().await;
if !saved.is_empty() {
    eprintln!("[memory saved: {}]", saved.join(", "));
}
Unreviewed Auto-Writes

Model-extracted entries are written automatically from transcript content, including tool output and file contents. Prompt-injected text in the transcript could persist as durable memory that is loaded into every future system prompt. Slug validation is tested (../bad rejected), but body/hook content is not sanitized or size-limited. Verify limits on body size and hook length, and consider surfacing what was saved for easy review or memory_forget.

let r = r#"sure: [{"scope":"project","slug":"ex","hook":"h","body":"b"},{"slug":"../bad","hook":"h","body":"b"}]"#;
assert_eq!(apply_extracted(r, &home, &cwd), ["project/ex"]);
assert!(apply_extracted("no json", &home, &cwd).is_empty());
Schema Behavior Change

memory_forget no longer requires scope. Verify the call implementation has a defined default (project) when scope is missing, and that it matches the default used by memory_write and memory_search. Otherwise a forget may hit the wrong scope, or fail, with no indication.

"required":["slug"]}),
## PR Reviewer Guide 🔍 Here are some key observations to aid the review process: <table> <tr><td>⏱️&nbsp;<strong>Estimated effort to review</strong>: 3 🔵🔵🔵⚪⚪</td></tr> <tr><td>🧪&nbsp;<strong>PR contains tests</strong></td></tr> <tr><td>🔒&nbsp;<strong>Security concerns</strong><br><br> <strong>Prompt injection to persistent memory:</strong><br> `extract_memory_now` and compaction extraction write model-generated facts from the transcript (which may include untrusted tool output or file contents) to memory without user confirmation. These are injected into the system prompt of all later sessions, so malicious content can persist and influence future behavior. Add content limits, and consider showing or confirming saved entries, or marking auto-extracted entries as untrusted.</td></tr> <tr><td>⚡&nbsp;<strong>Recommended focus areas for review</strong><br><br> <details><summary><a href='https://git.jokresner.de/johannes/hx/src/branch/memory-search/crates/hx-tui/src/lib.rs#L715-L719'><strong>Blocking Exit Call</strong></a> `extract_memory_now()` runs after `ratatui::restore()` and awaits a full model call on exit with no timeout or cancellation. If the network is slow or down, or the user hits Ctrl-C, the terminal appears hung. Also, `agent.lock().await` may block if a turn is still running. Consider a timeout and making the extraction skippable. The extraction also silently discards failures, so users get no signal when memory was not saved. </summary> ```rust eprintln!("[saving memory…]"); let saved = agent.lock().await.extract_memory_now().await; if !saved.is_empty() { eprintln!("[memory saved: {}]", saved.join(", ")); } ``` </details> <details><summary><a href='https://git.jokresner.de/johannes/hx/src/branch/memory-search/crates/hx-core/src/memory.rs#L309-L311'><strong>Unreviewed Auto-Writes</strong></a> Model-extracted entries are written automatically from transcript content, including tool output and file contents. Prompt-injected text in the transcript could persist as durable memory that is loaded into every future system prompt. Slug validation is tested (`../bad` rejected), but body/hook content is not sanitized or size-limited. Verify limits on body size and hook length, and consider surfacing what was saved for easy review or `memory_forget`. </summary> ```rust let r = r#"sure: [{"scope":"project","slug":"ex","hook":"h","body":"b"},{"slug":"../bad","hook":"h","body":"b"}]"#; assert_eq!(apply_extracted(r, &home, &cwd), ["project/ex"]); assert!(apply_extracted("no json", &home, &cwd).is_empty()); ``` </details> <details><summary><a href='https://git.jokresner.de/johannes/hx/src/branch/memory-search/crates/hx-tools/src/lib.rs#L573-L573'><strong>Schema Behavior Change</strong></a> `memory_forget` no longer requires `scope`. Verify the `call` implementation has a defined default (project) when `scope` is missing, and that it matches the default used by `memory_write` and `memory_search`. Otherwise a forget may hit the wrong scope, or fail, with no indication. </summary> ```rust "required":["slug"]}), ``` </details> </td></tr> </table>

PR Code Suggestions ✨

Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Impact
General
Add timeout to exit-time memory extraction

The exit-time extraction is a network model call with no timeout. The terminal is
already restored, so a slow or hung provider makes quitting appear frozen. Wrap the
call in tokio::time::timeout so exit always completes. On timeout, skip saving.

crates/hx-tui/src/lib.rs [715-717]

 eprintln!("[saving memory…]");
-let saved = agent.lock().await.extract_memory_now().await;
+let saved = tokio::time::timeout(std::time::Duration::from_secs(20), async {
+    agent.lock().await.extract_memory_now().await
+})
+.await
+.unwrap_or_default();
 if !saved.is_empty() {
Suggestion importance[1-10]: 6

__

Why: Valid: extract_memory_now() is a network model call on exit with no timeout, so a hung provider freezes quitting. The improved code is correct, since unwrap_or_default() on the timeout Result<Vec<String>, Elapsed> yields an empty vec. Impact is moderate because it only affects the exit path and the timeout value is arbitrary.

Low
## PR Code Suggestions ✨ <!-- 9d8b7df --> Explore these optional code suggestions: <table><thead><tr><td><strong>Category</strong></td><td align=left><strong>Suggestion&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </strong></td><td align=center><strong>Impact</strong></td></tr><tbody><tr><td rowspan=1>General</td> <td> <details><summary>Add timeout to exit-time memory extraction</summary> ___ **The exit-time extraction is a network model call with no timeout. The terminal is <br>already restored, so a slow or hung provider makes quitting appear frozen. Wrap the <br>call in <code>tokio::time::timeout</code> so exit always completes. On timeout, skip saving.** [crates/hx-tui/src/lib.rs [715-717]](https://git.jokresner.de/johannes/hx/src/branch/memory-search/crates/hx-tui/src/lib.rs#L715-L717) ```diff eprintln!("[saving memory…]"); -let saved = agent.lock().await.extract_memory_now().await; +let saved = tokio::time::timeout(std::time::Duration::from_secs(20), async { + agent.lock().await.extract_memory_now().await +}) +.await +.unwrap_or_default(); if !saved.is_empty() { ``` <details><summary>Suggestion importance[1-10]: 6</summary> __ Why: Valid: `extract_memory_now()` is a network model call on exit with no timeout, so a hung provider freezes quitting. The improved code is correct, since `unwrap_or_default()` on the timeout `Result<Vec<String>, Elapsed>` yields an empty vec. Impact is moderate because it only affects the exit path and the timeout value is arbitrary. </details></details></td><td align=center>Low </td></tr></tr></tbody></table>
hermes added 1 commit 2026-11-30 12:56:26 +00:00
hermes merged commit 76253d237a into main 2026-11-30 14:39:30 +00:00
Sign in to join this conversation.
No Reviewers
2 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: johannes/hx#238