Skip to content

Persistent project knowledge graph for coding agents. MCP server with semantic search, in-process embeddings, and web explorer.

fix: correct embedding type safety and add merge round-trip test

0xk3vinb505e4231918
diff --git a/src/__tests__/merge.test.ts b/src/__tests__/merge.test.ts
index c6d923d..92a2fc0 100644
--- a/src/__tests__/merge.test.ts
+++ b/src/__tests__/merge.test.ts
@@ -240,6 +240,68 @@ describe("MergeEngine", () => {
output.close();
});
+ it("preserves Buffer embeddings through merge round-trip", () => {
+ const left = createTmpDb("left.db");
+ const right = createTmpDb("right.db");
+ const outputPath = path.join(tmpDir, "output.db");
+
+ const leftEmbedding = Buffer.alloc(1536, 0x1a);
+ const rightEmbedding = Buffer.alloc(1536, 0x2b);
+
+ left.db.insertNode({
+ id: "left-emb",
+ name: "Left Embedding",
+ kind: "feature",
+ summary: "Left with embedding",
+ why: null,
+ file_refs: null,
+ parent_id: null,
+ created_by_task: null,
+ embedding: leftEmbedding,
+ });
+ right.db.insertNode({
+ id: "right-emb",
+ name: "Right Embedding",
+ kind: "feature",
+ summary: "Right with embedding",
+ why: null,
+ file_refs: null,
+ parent_id: null,
+ created_by_task: null,
+ embedding: rightEmbedding,
+ });
+
+ left.db.close();
+ right.db.close();
+
+ engine.merge(left.path, right.path, outputPath);
+
+ const output = new KnowledgeDB(outputPath);
+ const outNodes = output.getAllActiveNodesWithEmbeddings();
+ const leftNode = outNodes.find((node) => node.id === "left-emb");
+ const rightNode = outNodes.find((node) => node.id === "right-emb");
+
+ expect(leftNode).toBeDefined();
+ const outputEmbedding = leftNode!.embedding;
+ expect(Buffer.isBuffer(outputEmbedding)).toBe(true);
+ if (!Buffer.isBuffer(outputEmbedding)) {
+ throw new Error("Expected Buffer embedding");
+ }
+ expect(outputEmbedding).toHaveLength(1536);
+ expect(outputEmbedding.equals(leftEmbedding)).toBe(true);
+
+ expect(rightNode).toBeDefined();
+ const rightOutputEmbedding = rightNode!.embedding;
+ expect(Buffer.isBuffer(rightOutputEmbedding)).toBe(true);
+ if (!Buffer.isBuffer(rightOutputEmbedding)) {
+ throw new Error("Expected Buffer embedding");
+ }
+ expect(rightOutputEmbedding).toHaveLength(1536);
+ expect(rightOutputEmbedding.equals(rightEmbedding)).toBe(true);
+
+ output.close();
+ });
+
it("merges edges for non-conflicting nodes", () => {
const left = createTmpDb("left.db");
const right = createTmpDb("right.db");
diff --git a/src/db.ts b/src/db.ts
index 47a4ffe..a6a7820 100644
--- a/src/db.ts
+++ b/src/db.ts
@@ -337,9 +337,13 @@ export class KnowledgeDB {
.all() as NodeRow[];
}
- getAllActiveNodesWithEmbeddings(): Array<
- Pick<NodeRow, "id" | "name" | "kind" | "summary" | "embedding">
- > {
+ getAllActiveNodesWithEmbeddings(): Array<{
+ id: string;
+ name: string;
+ kind: string;
+ summary: string;
+ embedding: Buffer | null;
+ }> {
const rows = this.db
.prepare(
"SELECT id, name, kind, summary, embedding FROM nodes WHERE removed_at IS NULL AND embedding IS NOT NULL"
@@ -349,7 +353,7 @@ export class KnowledgeDB {
return rows.map((row) => ({
...row,
embedding: row.embedding ? Buffer.from(row.embedding) : null
- })) as Array<Pick<NodeRow, "id" | "name" | "kind" | "summary" | "embedding">>;
+ }));
}
nodeExists(id: string): boolean {
diff --git a/src/types.ts b/src/types.ts
index d890d75..3b4dbb8 100644
--- a/src/types.ts
+++ b/src/types.ts
@@ -152,7 +152,7 @@ export interface NodeRow {
updated_at: string;
removed_at: string | null;
removed_reason: string | null;
- embedding: Buffer | null;
+ embedding: Buffer | ArrayBuffer | null;
merge_group: string | null;
needs_merge: number; // SQLite stores boolean as 0/1
source_branch: string | null;