Implementation:0xK3vin MegaMemory MergeEngine merge
| Knowledge Sources | |
|---|---|
| Domains | Knowledge_Graph, Merge_Algorithm, SQLite |
| Last Updated | 2026-10-27 12:00 GMT |
Overview
Concrete engine for merging two divergent knowledge graph databases into a fresh output file provided by the MegaMemory library.
Description
The MergeEngine (src/merge.ts) performs a deterministic two-way merge of two knowledge.db SQLite files. It opens the left and right DBs read-only and writes to a fresh output DB. The algorithm runs in two passes:
- Pass 1 (nodes): For each canonical ID (stripping any existing ::left/::right suffixes), it compares the left and right node versions. Identical nodes are kept as-is (left's copy). Conflict nodes are inserted both with suffixed IDs (id::left, id::right), a shared merge_group UUID, needs_merge = 1, and a source_branch label. Pre-existing unresolved conflicts are carried forward.
- Pass 2 (edges): Edges from non-conflicting nodes are deferred and inserted with remapped target IDs (using an idRemapping table keyed by "side:originalId"). Edges from conflicting nodes are marked with the conflict's merge_group if the edge sets differ between sides. INSERT OR IGNORE prevents duplicate edges.
Helper functions: stripMergeSuffix (remove ::left/::right), hasMergeSuffix, nodesAreIdentical (deep content comparison ignoring timestamps/embeddings), edgeSetsAreIdentical (set comparison by content key).
Usage
Import this engine when you need to:
- Merge branch knowledge graphs: combine two independently-grown .megamemory/knowledge.db files into one output
- Programmatic merge with custom labels: pass leftLabel/rightLabel to name branches for conflict display
- Integrate merge into other tooling: call MergeEngine.merge directly instead of the megamemory merge CLI
Code Reference
Source Location
- Repository: 0xK3vin_MegaMemory
- File: src/merge.ts
- Key functions:
- stripMergeSuffix: L27-31
- hasMergeSuffix: L36-38
- nodesAreIdentical: L43-61
- edgeSetsAreIdentical: L73-82
- MergeEngine.merge: L90-111
- MergeEngine.performMerge: L113-404 (two-pass algorithm)
- insertNodeAsIs: L406-426
- insertConflictNode: L428-455
- deferEdgesForNode: L460-488
- deferEdgesClean: L493-549
Signature
export const MERGE_SUFFIX_LEFT = "::left";
export const MERGE_SUFFIX_RIGHT = "::right";
export interface MergeOptions {
leftLabel?: string; // default "left"
rightLabel?: string; // default "right"
}
export function stripMergeSuffix(id: string): string;
export function hasMergeSuffix(id: string): boolean;
export function nodesAreIdentical(left: NodeRow, right: NodeRow): boolean;
export function edgeSetsAreIdentical(left: EdgeRow[], right: EdgeRow[]): boolean;
export class MergeEngine {
merge(
leftPath: string,
rightPath: string,
outputPath: string,
options?: MergeOptions
): MergeResult;
// MergeResult = { clean, conceptConflicts, edgeConflicts, removedClean, mergeGroups: string[] }
}
Import
import { MergeEngine, stripMergeSuffix, nodesAreIdentical, edgeSetsAreIdentical } from "./merge.js";
import type { MergeOptions, MergeResult } from "./merge.js";
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| leftPath | string | Yes | Path to left knowledge.db |
| rightPath | string | Yes | Path to right knowledge.db |
| outputPath | string | Yes | Path for the merged output DB (created fresh) |
| leftLabel | string | No | Source label for left conflicts; default "left" |
| rightLabel | string | No | Source label for right conflicts; default "right" |
Outputs
| Name | Type | Description |
|---|---|---|
| merge() returns | MergeResult | { clean, conceptConflicts, edgeConflicts, removedClean, mergeGroups[] } |
| clean | number | Identical or one-sided nodes kept without conflict |
| conceptConflicts | number | Node IDs that differ between left and right |
| edgeConflicts | number | Nodes whose outgoing edge sets differ between sides |
| removedClean | number | One-sided nodes that are already soft-deleted |
| mergeGroups | string[] | UUIDs assigned to each conflict group for later resolution |
Usage Examples
Merge Two Branch DBs
import { MergeEngine } from "./merge.js";
const engine = new MergeEngine();
const result = engine.merge(
"main.db",
"feature-branch.db",
"merged.db",
{ leftLabel: "main", rightLabel: "feature" }
);
console.log(`Clean: ${result.clean}, Conflicts: ${result.conceptConflicts + result.edgeConflicts}`);
if (result.mergeGroups.length > 0) {
console.log(`Resolve with: megamemory conflicts, then megamemory resolve <group> --keep <left|right|both>`);
}
Programmatic Conflict Inspection
import { MergeEngine } from "./merge.js";
const engine = new MergeEngine();
const result = engine.merge("left.db", "right.db", "out.db");
for (const groupId of result.mergeGroups) {
// Use KnowledgeDB.getNodesByMergeGroup(groupId) to inspect each conflict's versions
const versions = db.getNodesByMergeGroup(groupId);
console.log(`Conflict on "${stripMergeSuffix(versions[0].id)}": ${versions.length} versions`);
}
Related Pages
Implements Principle
- Principle:0xK3vin_MegaMemory_Knowledge_Graph_Merge
- Principle:0xK3vin_MegaMemory_Merge_Conflict_Resolution