Skip to content

Conversation

@Nsuccess
Copy link

@Nsuccess Nsuccess commented Jan 16, 2026

Closes #250

What this PR does

Adds a Memory Reasoning Engine (MRE) that transforms MemU from a retrieval system into a reasoning system.

The core insight: Memory should not be static. Memory should produce new memory.

Current State vs Enhancement

Capability Current MemU With MRE
Memory access Retrieve by similarity Reason over relationships
Output Stored memories Stored + derived knowledge
Agent behavior Reactive (answer when asked) Reflective (infer before asked)

The Reasoning Loop

RETRIEVE → TRAVERSE → FILTER → SCORE → INFER → WRITE BACK
  1. RETRIEVE - Get candidate memories matching query constraints
  2. TRAVERSE - Walk knowledge graph relationships (requires feat: Add Knowledge Graph with entity extraction and graph traversal #249)
  3. FILTER - Apply type, time range, confidence thresholds
  4. SCORE - Rank by relevance + tool success rates (requires feat: Add Tool Memory type with specialized metadata and statistics #247)
  5. INFER - LLM-assisted conclusion generation
  6. WRITE BACK - Store derived memories with provenance

Key Components

ReasoningQuery DSL - Structured queries with constraints:

query = ReasoningQuery(
    goal="Who can help with database optimization?",
    constraints=ReasoningConstraints(
        entity_types=["Person"],
        memory_types=["knowledge", "tool"],
        relationships=["works_on", "expert_in"],
        time_range_days=30,
        require_tool_success=True
    ),
    reasoning_depth=2
)

DerivedMemory - Machine-generated knowledge with full provenance:

DerivedMemory(
    content="John is the best database expert",
    inference_type="deduction",
    source_memory_ids=["mem_1", "mem_2", "mem_3"],
    confidence_score=0.85,
    expires_at=datetime(2026, 2, 1)
)

Self-Consistency Verification - Run inference N times with different seeds:

  • Consistent results → reinforce confidence
  • Inconsistent results → weaken confidence
  • Prevents hallucination from becoming "memory"

Files Added

File Purpose
src/memu/reasoning/__init__.py Module exports
src/memu/reasoning/query_dsl.py ReasoningQuery, ReasoningConstraints, ReasoningTrace
src/memu/reasoning/derived_memory.py DerivedMemory, DerivedMemoryStore
src/memu/reasoning/memory_reasoner.py Core MemoryReasoner class
src/memu/reasoning/prompts.py LLM prompts for inference
tests/test_memory_reasoner.py 29 unit tests

Tests

All 29 tests passing.

Nsuccess and others added 7 commits January 16, 2026 11:23
Closes NevaMind-AI#243

- Add ToolCallResult model for tracking tool execution history
- Add 'tool' to MemoryType literal
- Add when_to_use field for retrieval hints
- Add metadata field for type-specific data
- Add tool_calls field for tool execution history
- Add get_tool_statistics() method for success_rate, avg_time_cost, avg_score
- Add tool.py prompt for tool memory extraction
- Update repository interfaces and implementations (inmemory, postgres)
- Add 14 unit tests for Tool Memory feature
Closes NevaMind-AI#248

- Add KnowledgeGraph class using NetworkX for entity-relationship storage
- Add entity extraction LLM prompt for extracting entities and relationships
- Add KnowledgeGraphMixin for service integration
- Add entity deduplication with fuzzy matching
- Add graph traversal with get_subgraph_context()
- Add 26 unit tests for Knowledge Graph functionality
- Add networkx>=3.0 dependency
- Add ReasoningQuery DSL with constraints (entity_types, memory_types, relationships, time_range)
- Implement reasoning loop: RETRIEVE -> TRAVERSE -> FILTER -> SCORE -> INFER -> WRITE BACK
- Add DerivedMemory type with source tracking, confidence scores, expiration
- Add self-consistency verification (reinforce/weaken based on repeated inference)
- Integrate with Knowledge Graph for relationship traversal
- Integrate with Tool Memory for success rate scoring
- Add 29 unit tests covering all components
@sairin1202
Copy link
Contributor

Same with PR#249, the knowledge graph approach conflicts with our current system architecture, so we’re unable to accept it. Appreciate your understanding.

@sairin1202 sairin1202 closed this Jan 21, 2026
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.

[2026NewYearChallenge] feat: Add Memory Reasoning Engine with derived knowledge inference

3 participants