diff --git a/src/functions/graph.ts b/src/functions/graph.ts index 0dfbad0c6f..87076d9b09 100644 --- a/src/functions/graph.ts +++ b/src/functions/graph.ts @@ -296,6 +296,70 @@ 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 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", + "key", + "value", +]); + +function lastXmlBlock(xml: string, tag: string): string | null { + 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 { + 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 +371,43 @@ 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 entityPayload = entitiesBlock !== null ? entitiesBlock : xml; + const relationshipPayload = + relationshipsBlock !== null ? 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 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 = {}; 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; + const value = propMatch[2].slice(0, MAX_PROPERTY_VALUE_LENGTH); + const candidate = { ...properties, [key]: value }; + if (Buffer.byteLength(JSON.stringify(candidate)) > MAX_PROPERTY_BYTES) { + break; + } + properties[key] = value; } nodes.push({ id: generateId("gn"), @@ -332,19 +420,24 @@ function parseGraphXml( }; let match; - while ((match = entitySelfClose.exec(xml)) !== null) { + while ((match = entitySelfClose.exec(entityPayload)) !== null) { addEntity(match[1]); } - while ((match = entityWithBody.exec(xml)) !== null) { + while ((match = entityWithBody.exec(entityPayload)) !== null) { addEntity(match[1], match[2]); } const relRegex = /]*?)\/>/g; - while ((match = relRegex.exec(xml)) !== null) { + while ((match = relRegex.exec(relationshipPayload)) !== null) { const attrs = parseAttrs(match[1]); - const type = attrs["type"] as GraphEdge["type"] | undefined; - const sourceName = attrs["source"]; - const targetName = attrs["target"]; + 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; 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..9d6ff75e89 100644 --- a/test/graph.test.ts +++ b/test/graph.test.ts @@ -157,6 +157,181 @@ 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("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("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("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] });