Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Principle:0xK3vin MegaMemory Knowledge Graph Merge

From Leeroopedia
Knowledge Sources
Domains Merge_Algorithm, Distributed_Systems, Version_Control
Last Updated 2026-10-27 12:00 GMT

Overview

A two-way merge algorithm for combining divergent knowledge graph databases that detects content conflicts, preserves both versions with suffixed IDs, and groups them for later resolution.

Description

Knowledge graph merge addresses the problem that SQLite databases are binary and unmergeable by standard text-based VCS tools (git). When two branches each grow their own knowledge.db, a structured merge is needed. The two-way merge algorithm compares every node ID across both databases: identical nodes are kept once; conflicting nodes (same ID, different content) are inserted both with suffixed IDs (id::left, id::right), a shared merge_group UUID, and merge metadata. Edges are deferred and remapped to the correct suffixed target in a second pass.

Problem solved: Without a merge algorithm, teams using per-project knowledge graphs cannot combine work done on different branches. Naive overwrites lose one side's knowledge. A structured merge preserves all information and flags conflicts for explicit resolution.

Context: MegaMemory's MergeEngine (src/merge.ts) implements this as a two-pass algorithm: Pass 1 inserts all nodes (clean or conflict-suffixed), Pass 2 inserts deferred edges with remapped target IDs. Conflicts are grouped by UUID so the AI-assisted resolveConflict MCP tool or the manual megamemory resolve --keep CLI can resolve them. Pre-existing unresolved conflicts are carried forward, allowing incremental merges.

Usage

Use this principle when:

  • You store structured data in a binary format (SQLite) that VCS cannot diff/merge
  • Two parties independently modify the same database and you need to combine their work
  • You want to preserve both versions of a conflict for later human or AI resolution
  • Edge references must be remapped when their target nodes are conflict-suffixed

Theoretical Basis

Given two databases DL and DR, for each canonical node ID i:

merge(i)={keep(nL)if i∈DL∖DRkeep(nR)if i∈DR∖DLkeep(nL)if i∈DL∩DR∧nL≡nRconflict(nL,nR)if i∈DL∩DR∧nL≢nR

Where nL≡nR (nodesAreIdentical) compares content fields (name, kind, summary, why, parent_id, file_refs, removed state) but ignores timestamps and embeddings.

Conflict representation: Both versions are inserted with IDs i::left and i::right, sharing a UUID merge_group. Edges referencing i are remapped by origin side: edges from the left DB point to i::left, edges from the right DB point to i::right.

Two-pass rationale: Edges are deferred because a conflict-suffixed target node may not exist yet when the edge is first encountered. Pass 1 inserts all nodes; Pass 2 inserts edges with remapped to_id.

Pseudo-code:

# Abstract algorithm (NOT real implementation)
# Pass 1: nodes
for id in all_ids:
    left, right = left_db.get(id), right_db.get(id)
    if left and not right:    output.insert(left)
    elif right and not left:  output.insert(right)
    elif identical(left, right): output.insert(left)
    else:  # conflict
        group = uuid()
        output.insert(left,  id=f"{id}::left",  merge_group=group)
        output.insert(right, id=f"{id}::right", merge_group=group)
        defer_edges(left.edges,  side="left",  target=f"{id}::left")
        defer_edges(right.edges, side="right", target=f"{id}::right")

# Pass 2: edges (remapped)
for edge in deferred_edges:
    edge.to_id = remap(edge.to_id, edge.origin_side)
    output.insert_edge(edge)

Related Pages

Implemented By

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment