Skip to content

#1 Persistent memory for AI coding agents based on real-world benchmarks

fix: parse self-closing graph entities (#494)

Nanamiacb4567bcdb7
diff --git a/src/functions/graph.ts b/src/functions/graph.ts
index b509016a..69fa788e 100644
--- a/src/functions/graph.ts
+++ b/src/functions/graph.ts
@@ -27,12 +27,12 @@ function parseGraphXml(
const now = new Date().toISOString();
const entityRegex =
- /<entity\s+type="([^"]+)"\s+name="([^"]+)"[^>]*>([\s\S]*?)<\/entity>/g;
+ /<entity\s+type="([^"]+)"\s+name="([^"]+)"[^>]*(?:\/>|>([\s\S]*?)<\/entity>)/g;
let match;
while ((match = entityRegex.exec(xml)) !== null) {
const type = match[1] as GraphNode["type"];
const name = match[2];
- const propsBlock = match[3];
+ const propsBlock = match[3] ?? "";
const properties: Record<string, string> = {};
const propRegex = /<property\s+key="([^"]+)">([^<]*)<\/property>/g;
diff --git a/test/graph.test.ts b/test/graph.test.ts
index b4ebae17..ec08ed8c 100644
--- a/test/graph.test.ts
+++ b/test/graph.test.ts
@@ -106,6 +106,32 @@ describe("Graph Functions", () => {
expect(edges[0].type).toBe("uses");
});
+ it("graph-extract accepts self-closing entity tags", async () => {
+ mockProvider.compress.mockResolvedValueOnce(`<entities>
+<entity type="file" name="src/index.ts"/>
+<entity type="function" name="main"><property key="lang">typescript</property></entity>
+</entities>
+<relationships>
+<relationship type="uses" source="src/index.ts" target="main" weight="0.9"/>
+</relationships>`);
+
+ 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<GraphNode>("mem:graph:nodes");
+ expect(nodes.some((n) => n.name === "src/index.ts")).toBe(true);
+ expect(nodes.some((n) => n.name === "main")).toBe(true);
+
+ const edges = await kv.list<GraphEdge>("mem:graph:edges");
+ expect(edges).toHaveLength(1);
+ expect(edges[0].type).toBe("uses");
+ });
+
it("graph-query with search returns matching nodes", async () => {
await sdk.trigger("mem::graph-extract", { observations: [testObs] });