#3401: fix(memory-lancedb): improve autoCapture with turn-by-turn processing
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
#15896: fix(memory-lancedb): capture even with injected recall context
by aelaguiz · 2026-02-14
88.7%
#14576: Fix/memory loss bugs
by ENCHIGO · 2026-02-12
77.9%
#8795: feat(memory): add Redis-backed long-term memory plugin
by tf-gmail · 2026-02-04
76.9%
#16634: feat(memory-lancedb): make auto-capture min length configurable
by ciberponk · 2026-02-14
76.0%
#8919: Pr/memory flush improvements
by shortbus · 2026-02-04
75.1%
#9026: fix(session-memory): sanitize content to prevent binary data in mem...
by Flamrru · 2026-02-04
74.9%
#14243: fix: fire session-memory hook on auto-resets + topic-aware memory p...
by TheDude135 · 2026-02-11
74.3%
#9085: fix: improve stability for terminated responses and telegram retries
by vladdick88 · 2026-02-04
74.2%
#19341: Mind Memory Fork
by juliopx · 2026-02-17
74.2%
#23639: fix(agents): stop re-resizing session history images on every turn ...
by yinghaosang · 2026-02-22
73.6%