Skip to content

fix(graph): resolve relationship endpoints case-insensitively, accept body-form tags#957

Open
shgew wants to merge 1 commit into
rohitg00:mainfrom
shgew:fix/graph-relationship-resolution
Open

fix(graph): resolve relationship endpoints case-insensitively, accept body-form tags#957
shgew wants to merge 1 commit into
rohitg00:mainfrom
shgew:fix/graph-relationship-resolution

Conversation

@shgew

@shgew shgew commented Jun 20, 2026

Copy link
Copy Markdown

What

Two fixes inside parseGraphXml:

  1. Normalize endpoint names (trim + lowercase) via a name->node map so edges whose source/target differ only in case still resolve.
  2. Parse both self-closing <relationship .../> AND body-form <relationship>...</relationship> elements. The original regex only matched self-closing.

Adds a referential-integrity rule plus a few-shot to the extraction prompt so models emit endpoints that reference declared entities.

Why

Relations scored ~1/5 for every locally-hosted model in benchmarks even when entity extraction worked. Root cause was this parser plus prompt combination, not model capability: a perfect model could not satisfy both the case-sensitive name match and the self-closing-only XML expectation.

Tests

  • test/graph-prompt.test.ts (new file, 1 case): prompt includes the referential-integrity rule + few-shot.
  • test/graph.test.ts: parseGraphXml relation resolution describe block (3 cases): case-insensitive resolution, body-form acceptance, drops relationships whose endpoint is not a declared entity.
  • Targeted suite: 30/30 pass (full graph.test.ts + graph-prompt.test.ts).

Compatibility

No env changes. Strict superset of previous parsing - any valid pre-fix XML still resolves to the same graph.

Summary by CodeRabbit

  • Bug Fixes

    • Graph relationship matching now operates case-insensitively and automatically trims whitespace for improved resolution accuracy.
    • Graph extraction now supports both self-closing and body-form XML relationship tag formats.
  • Tests

    • Added validation tests for relationship endpoint resolution and extraction prompt rules.

… body-form tags

Two fixes inside parseGraphXml:

1. Normalize endpoint names (trim + lowercase) via a name->node map
   so edges whose source/target differ only in case still resolve.

2. Parse both self-closing <relationship .../> AND body-form
   <relationship>...</relationship> elements. The original regex
   only matched self-closing.

Adds a referential-integrity rule plus a few-shot to the extraction
prompt so models emit endpoints that reference declared entities.

Relations scored ~1/5 for every locally-hosted model in benchmarks
even when entity extraction worked. Root cause was this parser plus
prompt combination, not model capability: a perfect model could not
satisfy both the case-sensitive name match and the
self-closing-only XML expectation.

Tests: test/graph-prompt.test.ts (+13 lines), test/graph.test.ts
(+79 lines).

Signed-off-by: Hleb Shauchenka <me@marleb.org>
@vercel

vercel Bot commented Jun 20, 2026

Copy link
Copy Markdown

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

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Jun 20, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 63929aae-5561-481c-a18d-2a9b6c15f1f0

📥 Commits

Reviewing files that changed from the base of the PR and between f6f9e3c and 28b68d9.

📒 Files selected for processing (4)
  • src/functions/graph.ts
  • src/prompts/graph-extraction.ts
  • test/graph-prompt.test.ts
  • test/graph.test.ts

📝 Walkthrough

Walkthrough

parseGraphXml in src/functions/graph.ts is updated to build a normalized (trimmed, lowercased) node-name index and a shared addRelationship helper, and to run separate regex passes for both self-closing and body-form <relationship> tags. The GRAPH_EXTRACTION_SYSTEM prompt is extended with endpoint-referencing rules and a concrete XML example. New tests cover both changes.

Changes

Graph Relationship Resolution

Layer / File(s) Summary
parseGraphXml normalized resolution and body-form tag support
src/functions/graph.ts, test/graph.test.ts
Builds a trim().toLowerCase() node-name lookup map and a shared addRelationship helper that resolves source/target via that map. Adds a second regex pass for <relationship ...>...</relationship> body-form tags alongside the existing self-closing pass. New test suite covers case-insensitive/trimmed matching, body-form acceptance, and silent dropping of relationships with undeclared endpoints.
Extraction prompt endpoint constraints and XML example
src/prompts/graph-extraction.ts, test/graph-prompt.test.ts
GRAPH_EXTRACTION_SYSTEM gains rules for empty-output when no entities are found, a constraint that source/target must reference only declared entity names, and a full XML few-shot example with weight formatting. Prompt tests assert the "declared" rule and "Example" snippet are present.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

  • rohitg00/agentmemory#494: Modifies parseGraphXml to improve entity/node parsing, directly related to the relationship resolution work in this PR.
  • rohitg00/agentmemory#685: Also modifies parseGraphXml relationship parsing to add more flexible attribute handling and tag variant support, the same function and concern as this PR.

Poem

🐇 Hop, hop, a name once trimmed,
No case shall block the edge's path!
Body-form tags, no longer dimmed,
The graph is spared the parser's wrath.
With prompts that show a clear example,
My knowledge graph is simply ample! ✨

🚥 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
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly and accurately summarizes the two main fixes: case-insensitive relationship endpoint resolution and support for body-form XML tags. It is concise, specific, and clearly communicates the primary changes.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant