Skip to content

fix(memory): separate context from extraction target in extractCandidates#8

Merged
lIang70 merged 1 commit into
mainfrom
fix/memory-extract-context-separation
Apr 15, 2026
Merged

fix(memory): separate context from extraction target in extractCandidates#8
lIang70 merged 1 commit into
mainfrom
fix/memory-extract-context-separation

Conversation

@lIang70

@lIang70 lIang70 commented Apr 15, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Previously, extractCandidates replaced Messages with ContextMessages when the latter was non-empty, causing the LLM to extract memories from the entire context window (including already-processed old messages), leading to duplicate extractions and wasted tokens.
  • Now, ContextMessages is delivered as a system message with a "Do NOT extract" instruction, while Messages remains the sole extraction target in the user message. This leverages the LLM's native role-based message separation — a much stronger signal than in-text markers.
  • Context text is independently truncated to 30% of MaxConversationRunes to prevent it from crowding out the extraction target.
  • When ContextMessages is nil, behavior is entirely unchanged (backward compatible).

Test plan

  • Updated TestExtractWithContextMessages to verify the two-message structure (system + user), system message contains context content and restriction, user message contains only new messages.
  • Added TestExtractWithoutContextMessages to verify backward-compatible single user message when no context is provided.
  • All 120+ existing memory package tests pass.

Made with Cursor

…ates

Previously, when ContextMessages was non-empty, it completely replaced
Messages for extraction. This caused the LLM to extract memories from
the entire context window — including old messages that were likely
already processed — leading to duplicate extractions and wasted tokens.

Now ContextMessages is passed as a system message (reference-only
context with a "Do NOT extract" instruction), while Messages remains
the sole extraction target in the user message. This leverages the
LLM's native role-based separation, which is a much stronger signal
than in-text markers.

When ContextMessages is nil, behavior is entirely unchanged (single
user message, backward compatible).

Context is independently truncated to 30% of MaxConversationRunes to
prevent it from crowding out the extraction target.

Made-with: Cursor
@lIang70
lIang70 merged commit 41651c3 into main Apr 15, 2026
8 checks passed
@lIang70
lIang70 deleted the fix/memory-extract-context-separation branch April 15, 2026 17:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant