Skip to content

Implement multi-model embeddings system with two-level embeddability #573

Closed

Description

@malibio

Context and Motivation

NodeSpace needs a semantic search capability to enable Claude Code and other AI agents to find relevant nodes based on meaning, not just keywords. This requires:

  1. Embeddings storage: Vector representations of node content for semantic similarity
  2. Multi-model support: Different embedding models for different use cases (accuracy vs speed)
  3. Intelligent embeddability: Not all nodes should be embedded individually - some contribute to parent embeddings
  4. Efficiency: Embed only when content changes, debounce rapid edits, background processing

This issue implements the backend embeddings system that will later be used by MCP semantic search (Issue #2).


Test Baseline (Session Start)

Recorded on 2025-11-21 at session start:

  • Test Files: 3 failed | 69 passed | 22 skipped (94 total)
  • Tests: 2 failed | 1425 passed | 22 skipped (1449 total)
  • Failures:
    1. index-stability-with-reordering.test.ts - Reordering children test failed
    2. split-pane-isolation.test.ts - Split pane isolation test failed

Note: These failures pre-exist and are not related to embeddings implementation.


Implementation Approach

1. Database Schema Changes

Create embeddings table:

CREATE TABLE embeddings (
    id TEXT PRIMARY KEY DEFAULT (lower(hex(randomblob(16)))),
    node_id TEXT NOT NULL REFERENCES node(id) ON DELETE CASCADE,
    model_name TEXT NOT NULL DEFAULT 'all-MiniLM-L6-v2',
    embedding_vector BLOB NOT NULL, -- F32 vector serialized
    created_at INTEGER NOT NULL DEFAULT (unixepoch()),
    updated_at INTEGER NOT NULL DEFAULT (unixepoch()),
    
    -- Composite unique constraint: one embedding per node per model
    UNIQUE(node_id, model_name)
);

-- Index for lookups
CREATE INDEX idx_embeddings_node_model ON embeddings(node_id, model_name);

Modify node table:

  • Add last_embedded_at column for staleness tracking
  • Create index for finding stale nodes

Why separate table?

  • Support multiple embedding models per node (future-proof)
  • Keep node table clean and focused
  • Easier to rebuild embeddings without touching core data
  • Foreign key cascade handles cleanup automatically

2. Two-Level Embeddability System

Add to NodeBehavior trait:

  • get_embeddable_content() - Returns content if node should be embedded as a root
  • get_parent_contribution() - Returns content this node contributes to parent

Implementations:

  • Text nodes: Always embeddable, always contribute full content
  • Headers: Embeddable if they are roots with children, contribute title
  • Tasks: Never embedded alone, never contribute to parent
  • Dates: Never embedded, never contribute

3. Embeddings Service

Create EmbeddingsService:

  • Generate embeddings using fastembed crate with all-MiniLM-L6-v2 model
  • Store and retrieve embeddings from database
  • Build embeddable content by combining node + children contributions
  • Track staleness and identify nodes needing re-embedding

4. Background Embedding Job

Hourly background task:

  • Find nodes that need re-embedding (stale or never embedded)
  • Process with 30-second debounce per root
  • Store in database with CASCADE cleanup on node deletion

Acceptance Criteria

  • embeddings table created with node_id, model_name, embedding_vector, timestamps
  • embedding_vector column removed from node table (if exists)
  • last_embedded_at column added to node table
  • NodeBehavior trait includes get_embeddable_content() and get_parent_contribution()
  • All node types (text, header, task, date) implement embeddability methods correctly
  • EmbeddingsService can generate embeddings using all-MiniLM-L6-v2
  • EmbeddingsService can store and retrieve embeddings from database
  • should_embed_node() correctly identifies embeddable roots (text nodes, headers with children)
  • build_embeddable_content() combines node + children contributions recursively
  • Staleness detection finds nodes updated since last embedding with 30-second debounce
  • Background job runs hourly and processes all stale embeddings
  • Unit tests for embeddability logic (each node type)
  • Integration tests for embedding generation and storage
  • Integration tests for staleness tracking and background processing

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions