← Back to PRs

#3401: fix(memory-lancedb): improve autoCapture with turn-by-turn processing

by mike-nott open 2026-01-28 15:07 View on GitHub →
extensions: memory-lancedb
## Summary Two fixes for autoCapture reliability in memory-lancedb. ## Problem 1: Injected Memory Context Blocks Capture When autoRecall is enabled, it prepends `<relevant-memories>` to user messages: ``` <relevant-memories> The following memories may be relevant... </relevant-memories> User's actual message here ``` The `shouldCapture()` function was checking for `<relevant-memories>` in the text and skipping it entirely. This meant **user messages were never captured** when both autoRecall and autoCapture were enabled. **Fix:** Add `stripMemoryContext()` helper to remove the injected context before evaluating capture criteria. ## Problem 2: Scanning Full History Instead of Current Turn The agent_end handler was: 1. Extracting ALL messages from conversation history (could be 100+) 2. Filtering through shouldCapture 3. Taking the first 3 that passed In long sessions, recent messages were at the end of the array and never processed. The first 3 messages that matched were often from much earlier in the conversation. **Fix:** Only process the **last user message** and **last assistant message** — the current turn. Previous turns were already captured when they occurred. ## Changes - Add `stripMemoryContext()` helper function - Update `shouldCapture()` to strip context before filtering - Rewrite agent_end handler for turn-by-turn capture - Strip context from text before storing ## Testing Tested in a 250+ message session: - Before: Recent messages (e.g., "I got an espresso machine for Christmas") never captured - After: Each turn captures the current user + assistant exchange Memory DB grew from 1072 → 1082 entries correctly capturing conversation turns. <!-- greptile_comment --> <h2>Greptile Overview</h2> <h3>Greptile Summary</h3> This PR improves `memory-lancedb` autoCapture reliability by (1) stripping the `<relevant-memories>…</relevant-memories>` block injected by autoRecall before deciding whether to capture/store a message, and (2) changing the `agent_end` handler to only consider the current turn (last user + last assistant messages) instead of scanning the entire conversation history. The changes fit into the plugin’s lifecycle-hook pipeline: autoRecall injects context in `before_agent_start`, and autoCapture persists high-signal snippets in `agent_end` after embedding + dedupe against LanceDB. <h3>Confidence Score: 4/5</h3> - This PR looks safe to merge and should improve autoCapture correctness, with minor edge cases to consider. - The change is localized to one plugin file and the core logic (strip injected recall context + process only current turn) matches the described bugs. Main remaining concerns are around message selection in `agent_end` (ensuring the captured assistant text is actually the final user-facing reply) and making `stripMemoryContext` less prone to accidental truncation if tags appear unexpectedly. - extensions/memory-lancedb/index.ts <!-- greptile_other_comments_section --> <sub>(4/5) You can add custom instructions or style guidelines for the agent [here](https://app.greptile.com/review/github)!</sub> <!-- /greptile_comment -->

Most Similar PRs