[Feature] Implement intelligent memory management with LLM-driven INSERT/UPDATE/DELETE decisions #35
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Overview
This PR implements intelligent memory management for the
add
method, addressing the feature request in issue #XX. The system now automatically decides whether to INSERT, UPDATE, DELETE, or SKIP memories based on similarity analysis and LLM-driven decision making, rather than simply inserting all new memories.Problem Statement
Previously, the
add
method would insert every extracted memory as a new record, leading to:Solution
Implemented a three-tier intelligent decision system:
1. Vector Similarity Search
When adding new memories, the system searches for similar existing memories using vector embeddings (similarity threshold: 0.7).
2. Rule-Based Decision Logic
For clear-cut cases, decisions are made automatically:
3. LLM-Driven Decisions
For borderline cases (similarity 0.85-0.95), the LLM analyzes both memories and decides:
Example Usage
New Components
MemoryAction.java
: Enum defining possible actions (INSERT/UPDATE/DELETE/SKIP)MemoryDecision.java
: Decision object containing action, content, existing memory reference, and reasoningMemory.add()
: Core logic implementing intelligent decision-makingIntelligentMemoryTest.java
: Comprehensive test suite with 7 test cases covering all decision pathsdocs/INTELLIGENT_MEMORY.md
: Complete documentation with usage examples and best practicesBenefits
✅ Reduces Redundancy: Automatically skips duplicate memories
✅ Maintains Currency: Updates memories with newer/more accurate information
✅ Ensures Coherence: Removes contradictory or obsolete memories
✅ Saves Storage: Only stores valuable, distinct memories
✅ Improves Search: Cleaner memory store leads to better retrieval quality
Testing
Security
✅ CodeQL security scan: 0 vulnerabilities
✅ Safe error handling with failsafe defaults
✅ No sensitive data exposure
Backward Compatibility
✅ 100% backward compatible - no breaking API changes
✅ Existing code continues to work without modifications
✅ New behavior is transparent and automatic
✅ Can be tuned via existing similarity threshold configuration
Performance Characteristics
Documentation
Complete documentation added in
docs/INTELLIGENT_MEMORY.md
covering:Related Issue
Closes #XX - Add方法当前不够智能,希望能够更加智能,内置LLM能从向量数据库里召回与当前摘要出来的fact相似的记忆,由LLM驱动增删改的决策。
Warning
Firewall rules blocked me from connecting to one or more addresses (expand for details)
I tried to connect to the following addresses, but was blocked by firewall rules:
api.anthropic.com
/usr/lib/jvm/temurin-17-jdk-amd64/bin/java -jar /home/REDACTED/work/mem4j/mem4j/mem4j-core/target/surefire/surefirebooter-20251019052041623_3.jar /home/REDACTED/work/mem4j/mem4j/mem4j-core/target/surefire 2025-10-19T05-20-41_553-jvmRun1 surefire-20251019052041623_1tmp surefire_0-20251019052041623_2tmp
(dns block)/usr/lib/jvm/temurin-17-jdk-amd64/bin/java -jar /home/REDACTED/work/mem4j/mem4j/mem4j-core/target/surefire/surefirebooter-20251019052519243_3.jar /home/REDACTED/work/mem4j/mem4j/mem4j-core/target/surefire 2025-10-19T05-25-19_174-jvmRun1 surefire-20251019052519243_1tmp surefire_0-20251019052519243_2tmp
(dns block)checkstyle.org
/opt/hostedtoolcache/CodeQL/2.23.2/x64/codeql/tools/linux64/java/bin/java -jar /opt/hostedtoolcache/CodeQL/2.23.2/x64/codeql/xml/tools/xml-extractor.jar --fileList=/home/REDACTED/work/mem4j/.codeql-scratch/dbs/java/working/files-to-index16251412941706974424.list --sourceArchiveDir=/home/REDACTED/work/mem4j/.codeql-scratch/dbs/java/src --outputDir=/home/REDACTED/work/mem4j/.codeql-scratch/dbs/java/trap/java
(dns block)If you need me to access, download, or install something from one of these locations, you can either:
Original prompt
Fixes #30
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.