Heuristic:0xK3vin MegaMemory Embedding Regeneration On Update
| Knowledge Sources | |
|---|---|
| Domains | Embeddings, Semantic_Search, Optimization |
| Last Updated | 2026-10-27 14:00 GMT |
Overview
Regenerate a concept's embedding only when its `name` or `summary` changes, skipping the costly embedding computation for metadata-only updates.
Description
MegaMemory stores a 384-dimensional float32 embedding for each concept node, used for semantic search via the `understand` tool. The embedding is generated from the text `"{kind}: {name} — {summary}"` using the local ONNX model. Embedding generation is the most expensive operation in the create/update path (~tens of milliseconds per call on CPU).
The `updateConcept` handler checks whether `name` or `summary` were included in the changes. If only `why`, `kind`, or `file_refs` changed, the existing embedding is preserved — these fields don't affect the embedding text. This avoids unnecessary model invocations and keeps updates fast.
On `createConcept`, the embedding is always generated (new node). On `resolveConflict`, the embedding is regenerated using the resolved content.
Usage
Apply this heuristic when:
- Updating concepts that only need metadata changes (`why`, `file_refs`, `kind`)
- You want to avoid the latency of embedding model inference
- Designing an update API — only re-embed when searchable content changes
Do not skip regeneration if `name` or `summary` changed — stale embeddings cause semantic search to miss the concept.
The Insight (Rule of Thumb)
- Action: In `updateConcept`, check if `changes.name` or `changes.summary` are present. Only call `embed()` if either is defined.
- Value: Embedding text = `"{kind}: {name} — {summary}"`. If neither `name` nor `summary` changed, pass `embedding: undefined` to `updateNode` (which skips the DB column update).
- Trade-off: Changing `kind` alone does NOT regenerate the embedding, even though `kind` is part of the embedding text. This is a minor staleness trade-off — `kind` is a short categorical label with low semantic impact.
- Create path: Always embed on `createConcept` (new node has no prior embedding).
Reasoning
The embedding model (`Xenova/all-MiniLM-L6-v2`, quantized ONNX) runs on CPU. While local and fast for a single call (~20-50ms), it becomes noticeable when updating many concepts in a session. Since most `update_concept` calls change only `why` or `file_refs` (adding rationale or file references), skipping the embedding saves the model invocation.
The `embeddingText` function composes `"{kind}: {name} — {summary}"`. Technically, changing `kind` changes the embedding text, but `kind` is a short label (one of: feature, module, pattern, config, decision, component) that contributes little to semantic similarity. The trade-off: faster updates vs. slightly stale embeddings when `kind` changes — acceptable since `kind` changes rarely.
Code Evidence
Conditional embedding regeneration in `updateConcept` from `src/tools.ts:193-223`:
export async function updateConcept(
db: KnowledgeDB,
input: UpdateConceptInput
): Promise<{ message: string }> {
const existing = db.getNode(input.id);
if (!existing) {
throw new Error(`Concept "${input.id}" not found.`);
}
// If summary or name changed, regenerate embedding
let embedding: Buffer | undefined;
if (input.changes.summary !== undefined || input.changes.name !== undefined) {
const name = input.changes.name ?? existing.name;
const kind = input.changes.kind ?? existing.kind;
const summary = input.changes.summary ?? existing.summary;
const text = embeddingText(name, kind, summary);
embedding = await embed(text);
}
const updated = db.updateNode(input.id, {
...input.changes,
embedding,
});
Embedding text composition from `src/embeddings.ts:53-59`:
export function embeddingText(
name: string,
kind: string,
summary: string
): string {
return `${kind}: ${name} — ${summary}`;
}
Embedding always generated on create from `src/tools.ts:167-170`:
// Generate embedding
const text = embeddingText(input.name, input.kind, input.summary);
const embedding = await embed(text);
Embedding regenerated on conflict resolution from `src/tools.ts:369-370`:
const text = embeddingText(base.name, base.kind, input.resolved.summary);
const newEmbedding = await embed(text);