Skip to content

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

@Nsuccess

Description

@Nsuccess

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",  # deduction | induction | summarization | analogy
    source_memory_ids=["mem_1", "mem_2", "mem_3"],
    confidence_score=0.85,
    expires_at=datetime(2026, 2, 1)  # Optional TTL for time-sensitive conclusions
)
Self-Consistency Verification - Run inference N times with different seeds:

Consistent resultsreinforce confidence
Inconsistent resultsweaken confidence
Prevents hallucination from becoming "memory"
Example: Agent Self-Improvement
# Agent asks: "Who should I contact about backend performance?"

trace = await reasoner.reason(ReasoningQuery(
    goal="Who should I contact about backend performance?",
    constraints=ReasoningConstraints(
        entity_types=["Person"],
        relationships=["expert_in", "worked_on"]
    )
))

# System:
# 1. Retrieves memories about people, skills, projects
# 2. Traverses graph: Person → skills → projects → outcomes
# 3. Scores by tool success rates (who actually delivered?)
# 4. Infers: "John has 90% success rate on DB tasks, worked on backend"
# 5. Writes DerivedMemory: "John is the best backend contact"

# Next time agent needs backend help → instant answer, no re-reasoning
Why This Matters
This is the difference between:

Memory as storage - "Here's what I remember"
Memory as cognition - "Here's what I concluded from what I remember"
Agents with MRE don't just recall - they learn, infer, and build knowledge over time.

Dependencies
Builds on Knowledge Graph (#249) for relationship traversal
Builds on Tool Memory (#247) for success rate scoring
Works standalone with reduced capability if dependencies not present
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 covering:

Query DSL validation
Reasoning loop execution
Derived memory creation and deduplication
Confidence reinforcement/weakening
Expiration and pruning
Graph traversal integration
Tool stats integration

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions