From 38036feb08fa6aaba93b76cdd17f436b0e1b4091 Mon Sep 17 00:00:00 2001 From: cbsincool Date: Wed, 28 Oct 2026 13:14:05 +0800 Subject: [PATCH 1/4] fix(graph): ignore reasoning text when parsing extraction XML Reasoning models can return their chain-of-thought when message.content is empty. That text often repeats the prompt's XML example, and parseGraphXml scanned the whole response, so example entities became real graph nodes with paragraph-long names. In a 23k-node store this produced 156 nodes whose names are reasoning text; they also exceeded the retrieval-index name bound and reported partial coverage. Parse only the final / block, falling back to the whole response when the model omits the wrapper, and reject names that are empty, over 512 characters, multiline, contain angle brackets, or are prompt placeholders. Relationships use the same name validation. Bound extracted properties to 32 keys, 128-character keys and 512-character values so the projection stays representable. Related: #1271, #1393, #1495. Signed-off-by: cbsincool --- src/functions/graph.ts | 56 ++++++++++++++++++++++++++++++++++++------ test/graph.test.ts | 52 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+), 7 deletions(-) diff --git a/src/functions/graph.ts b/src/functions/graph.ts index 0dfbad0c6f..5e8c717793 100644 --- a/src/functions/graph.ts +++ b/src/functions/graph.ts @@ -296,6 +296,33 @@ function parseAttrs(raw: string): Record { return attrs; } +const MAX_ENTITY_NAME_LENGTH = 512; +const MAX_ENTITY_PROPERTIES = 32; +const MAX_PROPERTY_KEY_LENGTH = 128; +const MAX_PROPERTY_VALUE_LENGTH = 512; +const PLACEHOLDER_ENTITY_NAMES = new Set([ + "exact name", + "entity name", + "key", + "value", +]); + +function lastXmlBlock(xml: string, tag: string): string { + const re = new RegExp(`<${tag}>([\\s\\S]*?)`, "g"); + let last = ""; + let match; + while ((match = re.exec(xml)) !== null) last = match[1]; + return last; +} + +function validEntityName(raw: string | undefined): string | null { + const name = raw?.trim() ?? ""; + if (!name || name.length > MAX_ENTITY_NAME_LENGTH) return null; + if (/[\r\n<>]/.test(name)) return null; + if (PLACEHOLDER_ENTITY_NAMES.has(name.toLowerCase())) return null; + return name; +} + function parseGraphXml( xml: string, observationIds: string[], @@ -307,19 +334,34 @@ function parseGraphXml( const edges: GraphEdge[] = []; const now = new Date().toISOString(); + // Reasoning models can return their thinking as the response. The thinking + // often repeats the prompt's XML example, so a global scan would turn that + // example into graph nodes. Parse only the final well-formed payload block; + // if the model omitted the wrapper, fall back to the whole response and let + // the name checks below reject reasoning text. + const entitiesBlock = lastXmlBlock(xml, "entities"); + const relationshipsBlock = lastXmlBlock(xml, "relationships"); + const payload = + entitiesBlock || relationshipsBlock + ? `${entitiesBlock}\n${relationshipsBlock}` + : xml; + const entitySelfClose = /]*?)\/>/g; const entityWithBody = /]*[^/])>([\s\S]*?)<\/entity>/g; const addEntity = (rawAttrs: string, propsBlock = ""): void => { const attrs = parseAttrs(rawAttrs); const type = attrs["type"] as GraphNode["type"] | undefined; - const name = attrs["name"]; + const name = validEntityName(attrs["name"]); if (!type || !name) return; const properties: Record = {}; const propRegex = /([^<]*)<\/property>/g; let propMatch; while ((propMatch = propRegex.exec(propsBlock)) !== null) { - properties[propMatch[1]] = propMatch[2]; + if (Object.keys(properties).length >= MAX_ENTITY_PROPERTIES) break; + const key = propMatch[1]; + if (key.length > MAX_PROPERTY_KEY_LENGTH) continue; + properties[key] = propMatch[2].slice(0, MAX_PROPERTY_VALUE_LENGTH); } nodes.push({ id: generateId("gn"), @@ -332,19 +374,19 @@ function parseGraphXml( }; let match; - while ((match = entitySelfClose.exec(xml)) !== null) { + while ((match = entitySelfClose.exec(payload)) !== null) { addEntity(match[1]); } - while ((match = entityWithBody.exec(xml)) !== null) { + while ((match = entityWithBody.exec(payload)) !== null) { addEntity(match[1], match[2]); } const relRegex = /]*?)\/>/g; - while ((match = relRegex.exec(xml)) !== null) { + while ((match = relRegex.exec(payload)) !== null) { const attrs = parseAttrs(match[1]); const type = attrs["type"] as GraphEdge["type"] | undefined; - const sourceName = attrs["source"]; - const targetName = attrs["target"]; + const sourceName = validEntityName(attrs["source"]); + const targetName = validEntityName(attrs["target"]); if (!type || !sourceName || !targetName) continue; const parsedWeight = parseFloat(attrs["weight"] ?? ""); const weight = Number.isFinite(parsedWeight) ? parsedWeight : 0.5; diff --git a/test/graph.test.ts b/test/graph.test.ts index 1556a59023..3e96e89400 100644 --- a/test/graph.test.ts +++ b/test/graph.test.ts @@ -157,6 +157,58 @@ describe("Graph Functions", () => { expect(edges[0].weight).toBeCloseTo(0.9, 5); }); + it("ignores reasoning text before the final entities block", async () => { + mockProvider.compress.mockResolvedValueOnce(`I should output the XML now. +Example: + + + +Final answer: + + + + + + +`); + + const result = (await sdk.trigger("mem::graph-extract", { + observations: [testObs], + })) as { success: boolean; nodesAdded: number; edgesAdded: number }; + + expect(result.success).toBe(true); + expect(result.nodesAdded).toBe(2); + const nodes = await kv.list("mem:graph:nodes"); + expect(nodes.map((n) => n.name).sort()).toEqual(["main", "src/index.ts"]); + expect(nodes.some((n) => n.name === "exact name")).toBe(false); + }); + + it("rejects reasoning-like names and caps oversized properties", async () => { + const longName = "x".repeat(600); + const properties = Array.from( + { length: 35 }, + (_, i) => `${i === 0 ? "y".repeat(600) : "v"}`, + ).join(""); + mockProvider.compress.mockResolvedValueOnce(` + + +${properties} +`); + + const result = (await sdk.trigger("mem::graph-extract", { + observations: [testObs], + })) as { success: boolean; nodesAdded: number }; + + expect(result.success).toBe(true); + expect(result.nodesAdded).toBe(1); + const nodes = await kv.list("mem:graph:nodes"); + expect(nodes).toHaveLength(1); + expect(nodes[0].name).toBe("valid"); + expect(Object.keys(nodes[0].properties ?? {})).toHaveLength(32); + expect((nodes[0].properties ?? {})["p0"]).toHaveLength(512); + }); + it("graph-query with search returns matching nodes", async () => { await sdk.trigger("mem::graph-extract", { observations: [testObs] }); From a4b11739035f42b81f9554c50716a68f349c06fe Mon Sep 17 00:00:00 2001 From: cbsincool Date: Wed, 28 Oct 2026 13:22:09 +0800 Subject: [PATCH 2/4] fix(graph): handle empty and mixed XML wrappers in extraction Return null from lastXmlBlock when the tag is absent, so an empty final block is not mistaken for no wrapper and reasoning examples are not parsed. Parse entities and relationships from separate payloads so bare entity tags survive a wrapped relationships block. Stop adding properties at an 8 KiB property budget so a node cannot exceed the 16 KiB index projection at the per-field caps. Add cases for empty wrappers, mixed wrappers and the byte budget. Signed-off-by: cbsincool --- src/functions/graph.ts | 35 +++++++++++++++----------- test/graph.test.ts | 57 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 14 deletions(-) diff --git a/src/functions/graph.ts b/src/functions/graph.ts index 5e8c717793..4154878e86 100644 --- a/src/functions/graph.ts +++ b/src/functions/graph.ts @@ -300,6 +300,7 @@ const MAX_ENTITY_NAME_LENGTH = 512; const MAX_ENTITY_PROPERTIES = 32; const MAX_PROPERTY_KEY_LENGTH = 128; const MAX_PROPERTY_VALUE_LENGTH = 512; +const MAX_PROPERTY_BYTES = 8 * 1024; const PLACEHOLDER_ENTITY_NAMES = new Set([ "exact name", "entity name", @@ -307,12 +308,13 @@ const PLACEHOLDER_ENTITY_NAMES = new Set([ "value", ]); -function lastXmlBlock(xml: string, tag: string): string { - const re = new RegExp(`<${tag}>([\\s\\S]*?)`, "g"); - let last = ""; - let match; - while ((match = re.exec(xml)) !== null) last = match[1]; - return last; +function lastXmlBlock(xml: string, tag: string): string | null { + const open = `<${tag}>`; + const close = ``; + const start = xml.lastIndexOf(open); + if (start < 0) return null; + const end = xml.indexOf(close, start + open.length); + return end < 0 ? null : xml.slice(start + open.length, end); } function validEntityName(raw: string | undefined): string | null { @@ -341,10 +343,9 @@ function parseGraphXml( // the name checks below reject reasoning text. const entitiesBlock = lastXmlBlock(xml, "entities"); const relationshipsBlock = lastXmlBlock(xml, "relationships"); - const payload = - entitiesBlock || relationshipsBlock - ? `${entitiesBlock}\n${relationshipsBlock}` - : xml; + const entityPayload = entitiesBlock !== null ? entitiesBlock : xml; + const relationshipPayload = + relationshipsBlock !== null ? relationshipsBlock : xml; const entitySelfClose = /]*?)\/>/g; const entityWithBody = /]*[^/])>([\s\S]*?)<\/entity>/g; @@ -355,13 +356,19 @@ function parseGraphXml( const name = validEntityName(attrs["name"]); if (!type || !name) return; const properties: Record = {}; + let propertyBytes = 2; const propRegex = /([^<]*)<\/property>/g; let propMatch; while ((propMatch = propRegex.exec(propsBlock)) !== null) { if (Object.keys(properties).length >= MAX_ENTITY_PROPERTIES) break; const key = propMatch[1]; if (key.length > MAX_PROPERTY_KEY_LENGTH) continue; - properties[key] = propMatch[2].slice(0, MAX_PROPERTY_VALUE_LENGTH); + const value = propMatch[2].slice(0, MAX_PROPERTY_VALUE_LENGTH); + const size = + Buffer.byteLength(key) + Buffer.byteLength(value) + 4; + if (propertyBytes + size > MAX_PROPERTY_BYTES) break; + properties[key] = value; + propertyBytes += size; } nodes.push({ id: generateId("gn"), @@ -374,15 +381,15 @@ function parseGraphXml( }; let match; - while ((match = entitySelfClose.exec(payload)) !== null) { + while ((match = entitySelfClose.exec(entityPayload)) !== null) { addEntity(match[1]); } - while ((match = entityWithBody.exec(payload)) !== null) { + while ((match = entityWithBody.exec(entityPayload)) !== null) { addEntity(match[1], match[2]); } const relRegex = /]*?)\/>/g; - while ((match = relRegex.exec(payload)) !== null) { + while ((match = relRegex.exec(relationshipPayload)) !== null) { const attrs = parseAttrs(match[1]); const type = attrs["type"] as GraphEdge["type"] | undefined; const sourceName = validEntityName(attrs["source"]); diff --git a/test/graph.test.ts b/test/graph.test.ts index 3e96e89400..22f38b927f 100644 --- a/test/graph.test.ts +++ b/test/graph.test.ts @@ -209,6 +209,63 @@ line two"/> expect((nodes[0].properties ?? {})["p0"]).toHaveLength(512); }); + it("does not fall back to reasoning when the final wrappers are empty", async () => { + mockProvider.compress.mockResolvedValueOnce(`Reasoning example: + + +`); + + const result = (await sdk.trigger("mem::graph-extract", { + observations: [testObs], + })) as { success: boolean; nodesAdded: number; edgesAdded: number }; + + expect(result.success).toBe(true); + expect(result.nodesAdded).toBe(0); + expect(result.edgesAdded).toBe(0); + expect(await kv.list("mem:graph:nodes")).toHaveLength(0); + }); + + it("keeps bare entities when only relationships use a wrapper", async () => { + mockProvider.compress.mockResolvedValueOnce(` + + + +`); + + const result = (await sdk.trigger("mem::graph-extract", { + observations: [testObs], + })) as { success: boolean; nodesAdded: number; edgesAdded: number }; + + expect(result.success).toBe(true); + expect(result.nodesAdded).toBe(2); + expect(result.edgesAdded).toBe(1); + const nodes = await kv.list("mem:graph:nodes"); + expect(nodes.map((n) => n.name).sort()).toEqual(["main", "src/index.ts"]); + }); + + it("stops adding properties when the property block exceeds its byte budget", async () => { + const properties = Array.from( + { length: 35 }, + (_, i) => `${"y".repeat(512)}`, + ).join(""); + mockProvider.compress.mockResolvedValueOnce( + `${properties}`, + ); + + const result = (await sdk.trigger("mem::graph-extract", { + observations: [testObs], + })) as { success: boolean; nodesAdded: number }; + + expect(result.success).toBe(true); + expect(result.nodesAdded).toBe(1); + const nodes = await kv.list("mem:graph:nodes"); + const props = nodes[0].properties ?? {}; + expect(Object.keys(props).length).toBeLessThan(32); + expect(Buffer.byteLength(JSON.stringify(props))).toBeLessThanOrEqual( + 8 * 1024, + ); + }); + it("graph-query with search returns matching nodes", async () => { await sdk.trigger("mem::graph-extract", { observations: [testObs] }); From 0413feedbaa6c76320967a2156db460faca9fe13 Mon Sep 17 00:00:00 2001 From: cbsincool Date: Wed, 28 Oct 2026 13:30:54 +0800 Subject: [PATCH 3/4] fix(graph): accept wrapper whitespace and budget escaped properties Match the last opening / tag with optional attributes or whitespace, while keeping the closing-tag lookup. Budget property bytes with JSON.stringify of the candidate set so escaped quotes and backslashes cannot exceed the 8 KiB property budget. Add tests for whitespace opening tags and escaped values. Signed-off-by: cbsincool --- src/functions/graph.ts | 23 ++++++++++++----------- test/graph.test.ts | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 11 deletions(-) diff --git a/src/functions/graph.ts b/src/functions/graph.ts index 4154878e86..4ca9df0302 100644 --- a/src/functions/graph.ts +++ b/src/functions/graph.ts @@ -309,12 +309,14 @@ const PLACEHOLDER_ENTITY_NAMES = new Set([ ]); function lastXmlBlock(xml: string, tag: string): string | null { - const open = `<${tag}>`; - const close = ``; - const start = xml.lastIndexOf(open); - if (start < 0) return null; - const end = xml.indexOf(close, start + open.length); - return end < 0 ? null : xml.slice(start + open.length, end); + const openRe = new RegExp(`<${tag}\\b[^>]*>`, "g"); + let last: RegExpExecArray | null = null; + let match; + while ((match = openRe.exec(xml)) !== null) last = match; + if (!last) return null; + const start = last.index + last[0].length; + const end = xml.indexOf(``, start); + return end < 0 ? null : xml.slice(start, end); } function validEntityName(raw: string | undefined): string | null { @@ -356,7 +358,6 @@ function parseGraphXml( const name = validEntityName(attrs["name"]); if (!type || !name) return; const properties: Record = {}; - let propertyBytes = 2; const propRegex = /([^<]*)<\/property>/g; let propMatch; while ((propMatch = propRegex.exec(propsBlock)) !== null) { @@ -364,11 +365,11 @@ function parseGraphXml( const key = propMatch[1]; if (key.length > MAX_PROPERTY_KEY_LENGTH) continue; const value = propMatch[2].slice(0, MAX_PROPERTY_VALUE_LENGTH); - const size = - Buffer.byteLength(key) + Buffer.byteLength(value) + 4; - if (propertyBytes + size > MAX_PROPERTY_BYTES) break; + const candidate = { ...properties, [key]: value }; + if (Buffer.byteLength(JSON.stringify(candidate)) > MAX_PROPERTY_BYTES) { + break; + } properties[key] = value; - propertyBytes += size; } nodes.push({ id: generateId("gn"), diff --git a/test/graph.test.ts b/test/graph.test.ts index 22f38b927f..09ddc01f94 100644 --- a/test/graph.test.ts +++ b/test/graph.test.ts @@ -266,6 +266,48 @@ line two"/> ); }); + it("accepts whitespace in the final wrapper opening tags", async () => { + mockProvider.compress.mockResolvedValueOnce(` + + + + + +`); + + const result = (await sdk.trigger("mem::graph-extract", { + observations: [testObs], + })) as { success: boolean; nodesAdded: number; edgesAdded: number }; + + expect(result.success).toBe(true); + expect(result.nodesAdded).toBe(2); + expect(result.edgesAdded).toBe(1); + }); + + it("budgets escaped property values by serialized size", async () => { + const value = '\\"'.repeat(300); + const properties = Array.from( + { length: 20 }, + (_, i) => `${value}`, + ).join(""); + mockProvider.compress.mockResolvedValueOnce( + `${properties}`, + ); + + const result = (await sdk.trigger("mem::graph-extract", { + observations: [testObs], + })) as { success: boolean; nodesAdded: number }; + + expect(result.success).toBe(true); + expect(result.nodesAdded).toBe(1); + const nodes = await kv.list("mem:graph:nodes"); + const props = nodes[0].properties ?? {}; + expect(Object.keys(props).length).toBeLessThan(20); + expect(Buffer.byteLength(JSON.stringify(props))).toBeLessThanOrEqual( + 8 * 1024, + ); + }); + it("graph-query with search returns matching nodes", async () => { await sdk.trigger("mem::graph-extract", { observations: [testObs] }); From b457fea55875496fea5b6fe2be02c6c0eddd6587 Mon Sep 17 00:00:00 2001 From: cbsincool Date: Wed, 28 Oct 2026 15:10:08 +0800 Subject: [PATCH 4/4] fix(graph): reject node and edge types outside the schema The parser cast any type attribute, so a model that copied the prompt enum into type produced nodes like type="file|function|concept|...". Validate against the GraphNode and GraphEdge unions and skip invalid entities and relationships. Add a test with an enum-string entity type and an invalid relationship type. Signed-off-by: cbsincool --- src/functions/graph.ts | 47 ++++++++++++++++++++++++++++++++++++++++-- test/graph.test.ts | 24 +++++++++++++++++++++ 2 files changed, 69 insertions(+), 2 deletions(-) diff --git a/src/functions/graph.ts b/src/functions/graph.ts index 4ca9df0302..87076d9b09 100644 --- a/src/functions/graph.ts +++ b/src/functions/graph.ts @@ -301,6 +301,39 @@ const MAX_ENTITY_PROPERTIES = 32; const MAX_PROPERTY_KEY_LENGTH = 128; const MAX_PROPERTY_VALUE_LENGTH = 512; const MAX_PROPERTY_BYTES = 8 * 1024; +const GRAPH_NODE_TYPES = new Set([ + "file", + "function", + "concept", + "error", + "decision", + "pattern", + "library", + "person", + "project", + "preference", + "location", + "organization", + "event", +]); +const GRAPH_EDGE_TYPES = new Set([ + "uses", + "imports", + "modifies", + "causes", + "fixes", + "depends_on", + "related_to", + "works_at", + "prefers", + "blocked_by", + "caused_by", + "optimizes_for", + "rejected", + "avoids", + "located_in", + "succeeded_by", +]); const PLACEHOLDER_ENTITY_NAMES = new Set([ "exact name", "entity name", @@ -354,7 +387,12 @@ function parseGraphXml( const addEntity = (rawAttrs: string, propsBlock = ""): void => { const attrs = parseAttrs(rawAttrs); - const type = attrs["type"] as GraphNode["type"] | undefined; + const rawType = attrs["type"]; + const type = + typeof rawType === "string" && + GRAPH_NODE_TYPES.has(rawType as GraphNode["type"]) + ? (rawType as GraphNode["type"]) + : undefined; const name = validEntityName(attrs["name"]); if (!type || !name) return; const properties: Record = {}; @@ -392,7 +430,12 @@ function parseGraphXml( const relRegex = /]*?)\/>/g; while ((match = relRegex.exec(relationshipPayload)) !== null) { const attrs = parseAttrs(match[1]); - const type = attrs["type"] as GraphEdge["type"] | undefined; + const rawType = attrs["type"]; + const type = + typeof rawType === "string" && + GRAPH_EDGE_TYPES.has(rawType as GraphEdge["type"]) + ? (rawType as GraphEdge["type"]) + : undefined; const sourceName = validEntityName(attrs["source"]); const targetName = validEntityName(attrs["target"]); if (!type || !sourceName || !targetName) continue; diff --git a/test/graph.test.ts b/test/graph.test.ts index 09ddc01f94..9d6ff75e89 100644 --- a/test/graph.test.ts +++ b/test/graph.test.ts @@ -308,6 +308,30 @@ line two"/> ); }); + it("rejects entity and relationship types outside the schema", async () => { + mockProvider.compress.mockResolvedValueOnce(` + + + + + + + +`); + + const result = (await sdk.trigger("mem::graph-extract", { + observations: [testObs], + })) as { success: boolean; nodesAdded: number; edgesAdded: number }; + + expect(result.success).toBe(true); + expect(result.nodesAdded).toBe(2); + expect(result.edgesAdded).toBe(1); + const nodes = await kv.list("mem:graph:nodes"); + expect(nodes.map((n) => n.name).sort()).toEqual(["main", "src/index.ts"]); + const edges = await kv.list("mem:graph:edges"); + expect(edges).toHaveLength(1); + }); + it("graph-query with search returns matching nodes", async () => { await sdk.trigger("mem::graph-extract", { observations: [testObs] });