Back to .md Directory

003 - Phase 1: Basic Retrieval and Activation

Implements BFS activation spreading, cosine similarity search, and bridge discovery for a cognitive memory retrieval subsystem.

May 2, 2026
0 downloads
0 views
rag eval
View source

What this file does

Implements BFS activation spreading, cosine similarity search, and bridge discovery for a cognitive memory retrieval subsystem.

When to use it

  • Building a retrieval layer for a hierarchical memory system
  • Implementing activation spreading with threshold-based filtering
  • Adding serendipitous bridge discovery to memory retrieval
  • Integrating multiple retrieval strategies into a coordinator

Assumes this stack

Pythontorchnumpy

003 - Phase 1: Basic Retrieval and Activation

Overview

Phase 1 Step 003 implements the foundational retrieval and activation subsystem for the cognitive memory architecture. This phase builds upon the encoding and storage systems by creating basic activation spreading mechanisms and similarity-based retrieval that will serve as the foundation for more sophisticated cognitive processing in later phases.

Status

  • Started: 2025-06-17
  • Current Phase: Testing and Quality Improvement
  • Completion: 90%
  • Expected Completion: 2025-06-17

Objectives

  • Implement basic activation spreading engine with BFS traversal
  • Create similarity-based memory retrieval using cosine similarity
  • Build simple bridge discovery mechanism using distance inversion
  • Develop context-driven activation with threshold-based filtering
  • Integrate retrieval with hierarchical memory storage
  • [⚠️] Comprehensive test coverage for retrieval subsystem (In Progress)

Implementation Progress

Step 3A: Basic Activation Engine

Status: ✅ Complete Priority: High

Tasks

  • Create BasicActivationEngine in retrieval/basic_activation.py
  • Implement BFS traversal through memory connections
  • Add threshold-based activation filtering
  • Implement hierarchical activation (L0→L1→L2)
  • Add activation strength computation

Step 3B: Similarity-Based Search

Status: ✅ Complete Priority: High

Tasks

  • Implement SimilaritySearch in retrieval/similarity_search.py
  • Add cosine similarity computation for cognitive vectors
  • Implement k-nearest neighbor search across hierarchy levels
  • Add recency bias for recent memory preference
  • Implement search result ranking and filtering

Step 3C: Simple Bridge Discovery

Status: ✅ Complete Priority: Medium

Tasks

  • Create SimpleBridgeDiscovery in retrieval/bridge_discovery.py
  • Implement distance inversion algorithm from tech spec
  • Add novelty score computation (inverse similarity)
  • Add connection potential scoring
  • Implement bridge ranking and selection

Step 3D: Context-Driven Retrieval

Status: ✅ Complete Priority: Medium

Tasks

  • Implement ContextualRetrieval coordinator
  • Add context vector generation from current state
  • Integrate activation, similarity search, and bridge discovery
  • Implement memory categorization (core/peripheral/bridge)
  • Add retrieval result aggregation and ranking

Technical Architecture

Retrieval Component Structure

cognitive_memory/retrieval/
├── __init__.py
├── basic_activation.py      # BFS activation spreading
├── similarity_search.py     # Cosine similarity retrieval
├── bridge_discovery.py      # Simple distance inversion
└── contextual_retrieval.py  # High-level retrieval coordination

Key Algorithms

Activation Spreading (BFS)

  • Start with high-similarity memories at L0 (concepts)
  • Spread activation through connection graph using breadth-first search
  • Apply activation threshold to limit computational overhead
  • Track activation strength for result ranking

Similarity Search

  • Compute cosine similarity between query and memory vectors
  • Search across all hierarchy levels (L0, L1, L2)
  • Apply recency bias to prefer recent memories
  • Return ranked results with similarity scores

Bridge Discovery

  • Identify memories with low direct similarity to query
  • Compute connection potential through activated memories
  • Calculate bridge score: (novelty * 0.6) + (connection_potential * 0.4)
  • Return top-k bridge memories for serendipitous connections

Integration Architecture

graph TB
    subgraph "Retrieval Subsystem"
        CR[Contextual Retrieval]
        BAE[Basic Activation Engine]
        SS[Similarity Search]
        SBD[Simple Bridge Discovery]
    end

    subgraph "Storage Layer"
        HMS[Hierarchical Memory Storage]
        CG[Connection Graph]
    end

    CR --> BAE
    CR --> SS
    CR --> SBD

    BAE --> HMS
    BAE --> CG
    SS --> HMS
    SBD --> HMS

Key Design Decisions

  • Simple algorithms: Focus on correctness over sophistication for Phase 1
  • Threshold-based activation: Computationally efficient activation spreading
  • Hierarchical search: Leverage L0→L1→L2 structure for efficient retrieval
  • Interface compliance: All retrievers implement abstract interfaces
  • Modular design: Each retrieval method can be tested independently

Dependencies

  • torch (tensor operations for similarity computation)
  • collections.deque (BFS queue implementation)
  • numpy (numerical operations for scoring)
  • typing (type hints for interfaces)
  • Core storage and memory components from previous steps

Integration Points

  • With Storage: Retrieval accesses memories through storage interfaces
  • With Encoding: Uses cognitive vectors from encoding system
  • With Core: Implements retrieval interfaces defined in core/interfaces.py
  • Future Phases: Foundation for advanced activation and bridge discovery

Algorithm Specifications

Basic Activation Spreading

def activate_memories(context_vector, threshold=0.7, max_activations=50):
    # Phase 1: Find high-similarity L0 concepts
    l0_matches = storage.search_level(0, context_vector, threshold)

    # Phase 2: BFS through connection graph
    activated = set()
    queue = deque(l0_matches)

    while queue and len(activated) < max_activations:
        current = queue.popleft()
        if current.id not in activated:
            activated.add(current.id)
            connected = connection_graph.get_connections(current.id, threshold)
            queue.extend(connected)

    return ActivationResult(activated, strengths)

Simple Bridge Discovery

def discover_bridges(query_context, activated_memories, k=5):
    candidates = get_non_activated_memories(activated_memories)
    bridge_scores = []

    for candidate in candidates:
        # Novelty = inverse similarity to query
        novelty = 1.0 - cosine_similarity(query_context, candidate.vector)

        # Connection potential to activated memories
        connection_potential = max(
            cosine_similarity(candidate.vector, activated.vector)
            for activated in activated_memories
        )

        bridge_score = (novelty * 0.6) + (connection_potential * 0.4)
        bridge_scores.append((candidate, bridge_score))

    return sorted(bridge_scores, key=lambda x: x[1], reverse=True)[:k]

Testing Strategy & Progress

Unit Testing Status

  • BasicActivationEngine: ✅ Complete (23/23 tests passing)

    • BFS traversal, threshold filtering, memory categorization
    • Error handling, cosine similarity, configuration management
    • Interface compliance and performance characteristics
  • SimilaritySearch: ✅ Complete (33/33 tests passing)

    • Cosine similarity computation, recency bias calculation
    • Weight normalization, combined scoring, multi-level search
    • Configuration management and error handling
  • SimpleBridgeDiscovery: ⚠️ In Progress (29/40 tests passing)

    • 11 failing tests due to hierarchy_level vs level attribute mismatch
    • Core algorithm functionality working correctly
    • Need to fix bridge explanation generation and candidate filtering
  • ContextualRetrieval: ⏸️ Pending

    • Integration coordinator tests not yet implemented
    • Depends on completion of component tests

Testing Improvements Made

  • Fixed Implementation Issues: Weight normalization, proper score tracking
  • Enhanced API Design: Added missing methods, improved error handling
  • Architecture Consistency: Standardized on hierarchy_level attribute
  • Better Test Design: Improved mocking, controlled test conditions

Success Criteria

  • All retrieval interfaces implemented and tested
  • BFS activation spreading functional and efficient
  • Similarity search returns relevant results with proper ranking
  • Bridge discovery identifies meaningful connections
  • Integration with storage layer complete and tested
  • [⚠️] 85%+ test coverage for retrieval components (BasicActivation: ✅, SimilaritySearch: ✅, BridgeDiscovery: ⚠️)
  • [⚠️] All quality gates pass (ruff, mypy, pytest) - In Progress

Risks & Mitigation

  • Risk: BFS may be too slow for large memory graphs
    • Mitigation: Implement strict activation limits and early termination
  • Risk: Simple bridge discovery may produce low-quality connections
    • Mitigation: Focus on correctness, plan enhancement for Phase 2
  • Risk: Threshold tuning may be challenging
    • Mitigation: Start with conservative values, make configurable

Future Enhancements (Phase 2+)

  • Advanced activation spreading with learned weights
  • Attention-based bridge discovery mechanisms
  • Dynamic threshold adjustment based on context
  • Multi-modal retrieval combining different similarity metrics

Resources

  • Technical specification: architecture-technical-specification.md
  • High-level architecture: architecture-highlevel.md
  • Previous phases: 001_phase1_foundation.md, 002_phase1_storage.md
  • BFS algorithm reference: Graph traversal algorithms
  • Cosine similarity: Vector similarity metrics

Change Log

  • 2025-06-17: Step 003 progress document created
  • 2025-06-17: Basic retrieval and activation scope defined
  • 2025-06-17: Algorithm specifications and architecture planned
  • 2025-06-17: Implementation completed for all core retrieval components
    • ✅ BasicActivationEngine with BFS traversal and threshold filtering
    • ✅ SimilaritySearch with cosine similarity and recency bias
    • ✅ SimpleBridgeDiscovery with distance inversion algorithm
    • ✅ ContextualRetrieval coordinator with memory categorization
    • ✅ Public API and factory functions in retrieval module
  • 2025-06-17: Core implementation 85% complete, remaining: comprehensive testing
  • 2025-06-17: Testing phase initiated - critical architectural improvements discovered:
    • SimilaritySearch Enhanced: Fixed weight normalization, added combined_score tracking, improved API
    • Test Coverage: BasicActivationEngine (✅ 23/23 tests passing), SimilaritySearch (✅ 33/33 tests passing)
    • Code Quality: Fixed interface mismatches, improved separation of concerns, better error handling
    • Architecture Fix: Resolved CognitiveMemory attribute compatibility (hierarchy_level vs level)
  • 2025-06-17: Implementation status upgraded to 90% - remaining: BridgeDiscovery test fixes and integration tests

What's inside

8 sections: overview, objectives, implementation progress, technical architecture, algorithm specs, testing strategy, success criteria, risks

Change this for your project

  • Replace cognitive_memory/retrieval/ with your own package path
  • Replace lcbcFoo/heimdall-mcp-server with your repository name
  • Replace architecture-technical-specification.md with your spec file name

Where it goes

Reference documentation for a retrieval pipeline. Keep with the ingestion or retrieval code it describes.

Worth borrowing

  • Separating retrieval into independent engines (activation, similarity, bridge) that a coordinator combines
  • Using threshold-based BFS to limit computational cost in large graphs
  • Scoring bridge candidates with a weighted formula of novelty and connection potential

Related Documents