agentmemory icon indicating copy to clipboard operation
agentmemory copied to clipboard

fix: parse self-closing graph entities

Open Rex57 opened this issue 6 months ago β€’ 4 comments

Summary

  • Accept self-closing graph <entity .../> tags in parseGraphXml.
  • Keep existing explicit closing-tag entity parsing and property parsing behavior.
  • Add regression coverage for a self-closing entity that participates in a relationship.

Fixes #492.

Verification

  • npx tsdown passed in isolated fork clone.
  • npm run build reached tsdown build completion but failed afterward on Windows shell-only copy fallback (true is not recognized / shell syntax). No live AgentMemory service was started.
  • Tests not run locally per local environment constraint; full npm test intentionally skipped to avoid any accidental interaction with the live AgentMemory setup.

Summary by CodeRabbit

  • Bug Fixes

    • Improved XML entity parsing to support both self-closing and standard tag formats.
  • Tests

    • Added test case for entity parsing with self-closing XML tags to ensure robust format handling.

Review Change Stack

Rex57 avatar May 18 '26 08:05 Rex57

@Rex57 is attempting to deploy a commit to the rohitg00's projects Team on Vercel.

A member of the Team first needs to authorize it.

vercel[bot] avatar May 18 '26 08:05 vercel[bot]

πŸ“ Walkthrough

Walkthrough

Updated the graph extraction entity parser to accept both self-closing <entity ... /> and standard <entity>...</entity> tag formats by making the properties block optional in the regex. Added a test case verifying self-closing entities parse correctly and produce expected node and edge counts.

Changes

Self-closing Entity Tag Support

Layer / File(s) Summary
Entity regex self-closing tag support and test
src/functions/graph.ts, test/graph.test.ts
parseGraphXml's entityRegex now treats the inner properties content group as optional using ?, allowing empty propsBlock when tags are self-closing. A new test mocks self-closing file entities and verifies extraction produces correct node and edge counts.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

🐰 A rabbit's tale of tags made whole,
Self-closing entities find their role,
With optionals in regex bright,
The graph now parses day and night!

πŸš₯ Pre-merge checks | βœ… 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
βœ… Passed checks (4 passed)
Check name Status Explanation
Title check βœ… Passed The title accurately describes the main change: updating the parseGraphXml function to handle self-closing graph entity tags in addition to standard closing-tag forms.
Linked Issues check βœ… Passed The pull request fully addresses issue #492 by modifying parseGraphXml's regex to support self-closing entity tags and adding test coverage for this scenario.
Out of Scope Changes check βœ… Passed All changes are scoped to the stated objective: updating entity parsing logic and adding regression test coverage for self-closing entities.
Description Check βœ… Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
πŸ§ͺ Generate unit tests (beta)
  • [ ] Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❀️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

coderabbitai[bot] avatar May 18 '26 08:05 coderabbitai[bot]

Caught a regression on local test merge. The new self-closing test case fails because the regex still has a greedy quantifier:

// shipped in this PR:
/<entity\s+type="([^"]+)"\s+name="([^"]+)"[^>]*(?:\/>|>([\s\S]*?)<\/entity>)/g
//                                            ^^^^^ greedy β€” eats across the next entity

Traced standalone with the test payload (<entity ... />\n<entity ... ><property/></entity>): match 1 captures the first entity's name correctly, then [^>]* greedily consumes the / of /> and tries the alternation. The first alternative \/> fails (no / left to match), so it falls through to the second alternative >(...)<\/entity> β€” which then matches >\n<entity ...><property/></entity>, swallowing entity #2 entirely. Result: 1 match instead of 2.

One-char fix: lazy quantifier instead of greedy.

- /<entity\s+type="([^"]+)"\s+name="([^"]+)"[^>]*(?:\/>|>([\s\S]*?)<\/entity>)/g
+ /<entity\s+type="([^"]+)"\s+name="([^"]+)"[^>]*?(?:\/>|>([\s\S]*?)<\/entity>)/g

Verified with the same node trace: lazy returns 2 matches.

Push that and the test suite should go green. Thanks for the catch + the regression test β€” that test is exactly the kind of payload that exposes the bug.

rohitg00 avatar May 19 '26 12:05 rohitg00

I hit this same parser failure while investigating #559 / graph rebuild behavior on a local v0.9.20-style store. This PR is the right fix direction, and I think the real provider output makes the case stronger.

A live graph extraction response that source parseGraphXml would parse as 0 nodes / 0 edges was not empty. It looked like this, sanitized:

<entities>
  <entity type="agent" name="Agent"/>
  <entity type="system" name="Memory System"/>
  <entity type="component" name="MCP Server Agent Memory"/>
  <entity type="concept" name="Chinese Input"/>
  <entity type="concept" name="English Memories"/>
  <entity type="tool" name="Glob"/>
  <entity type="directory" name="<local-project-dir>"/>
  <entity type="file_type" name="Markdown files"/>
</entities>
<relationships>
  <relationship type="uses" source="Agent" target="Glob" weight="0.9"/>
  <relationship type="searches_in" source="Glob" target="<local-project-dir>" weight="0.9"/>
  <relationship type="searches_for" source="Glob" target="Markdown files" weight="0.9"/>
</relationships>

So self-closing entity support is not just a synthetic edge case: it is enough to turn real non-empty provider output into 0/0 today.

One follow-up this PR may or may not want to include: the same real response also contains out-of-schema entity/relationship types (agent, system, component, directory, file_type, searches_in, searches_for, etc.). Even after the self-closing regex fix, those need either normalization to the supported graph enums or explicit rejection/fallback, otherwise graph extraction may stay brittle. Happy to split that into a separate PR if you'd rather keep #494 narrowly scoped to self-closing tags.

This also cross-links to #559, because parser loss can present as an empty graph even when the provider returned usable graph-shaped XML.

jonathanzhan1975 avatar May 20 '26 13:05 jonathanzhan1975

Thanks @Rex57 β€” regex change is minimal (alternation between self-closing and explicit-close) and propsBlock ?? "" guard handles the missing capture group. Tests cover the new path. Merged. Closes #492; partial fix for #559.

rohitg00 avatar May 20 '26 20:05 rohitg00