Skip to content

fix(graph): tolerate reordered XML attrs on entity + relationship (closes #635)#685

Merged
rohitg00 merged 1 commit into
mainfrom
fix/635-codex-xml-attr-order
May 27, 2026
Merged

fix(graph): tolerate reordered XML attrs on entity + relationship (closes #635)#685
rohitg00 merged 1 commit into
mainfrom
fix/635-codex-xml-attr-order

Conversation

@rohitg00
Copy link
Copy Markdown
Owner

@rohitg00 rohitg00 commented May 27, 2026

Closes #635.

Bug

parseGraphXml in src/functions/graph.ts hard-coded attribute order on both tags:

  • <entity type="..." name="..."> — type must come first
  • <relationship type="..." source="..." target="..." weight="..."/> — strict left-to-right

Codex CLI's LLM commonly emits name before type, or source/target before type on relationships. Any deviation made the regex miss the tag entirely, silently dropping the node/edge. End-user symptom: a full graph-extract run completes with nodesAdded: 0.

Fix

Two-pass extraction with a tiny parseAttrs() helper that reads any key="value" pair in any order:

  • entitySelfClose (<entity ... />) + entityWithBody (<entity ...>...</entity>) patterns separate the two tag shapes. Preserves the fix: parse self-closing graph entities #494 fix where greedy attr-matching swallowed the trailing / and merged adjacent entities.
  • Relationship matcher reads source / target / type / weight from parsed attrs. Falls back to 0.5 when weight is missing or non-finite; requires type + source + target present to emit the edge (silent drop on incomplete tags, as before).

Tests

New case in test/graph.test.ts feeds the Codex-style reordered shape (<entity name=... type=...> + <relationship source=... target=... type=... weight=...>) and asserts node count + edge type + weight round-trip. All 7 graph tests pass.

Summary by CodeRabbit

  • Bug Fixes
    • Improved graph parser robustness to tolerate attribute reordering variations
    • Enhanced relationship validation with stricter node reference verification
    • Added weight normalization with intelligent defaults and clamping
    • Refined entity parsing to support multiple XML tag formats

Review Change Stack

…oses #635)

Previous parser hard-coded attribute order: type-before-name on <entity>, type/source/target/weight on <relationship>. Codex CLI's LLM frequently emits attrs in a different order (name first, source/target before type), and any deviation caused the whole tag to silently drop — graph extraction returned 0 nodes for an entire run.

Replaced both regexes with an order-independent two-pass approach:
- Tiny parseAttrs() reads any key="value" pair in any order
- entitySelfClose + entityWithBody patterns separate the two tag shapes (preserving the #494 fix where greedy attr-matching swallowed trailing / and merged adjacent entities)
- Relationship matcher now reads source/target/type/weight from parsed attrs, falls back to weight=0.5 on missing or non-finite, requires type+source+target present to emit the edge

Regression test: <entity name=... type=...> + <relationship source=... target=... type=...> shape that previously dropped to zero nodes/edges. 7/7 graph tests pass.
@vercel
Copy link
Copy Markdown

vercel Bot commented May 27, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
agentmemory Ready Ready Preview, Comment May 27, 2026 5:11pm

Request Review

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 27, 2026

📝 Walkthrough

Walkthrough

The graph XML parser is refactored to tolerate reordered attributes by extracting key-value pairs dynamically rather than assuming fixed attribute order. Entity and relationship parsing now use separate regex patterns with helper functions to build node records and validate relationship weights with defaults and clamping. A new test verifies the parser handles attribute order flexibility.

Changes

Graph XML Parser Robustness

Layer / File(s) Summary
Attribute parsing utility and entity parsing refactor
src/functions/graph.ts
Introduces parseAttrs helper to extract arbitrary key="value" attribute pairs. Refactors entity parsing into two regex patterns for self-closing and body tags, using addEntity helper to collect <property> children into a properties map.
Relationship parsing with weight validation and edge emission
src/functions/graph.ts
Refactors relationship parsing to extract attributes with parseAttrs, validate required source/target fields, apply weight defaults and clamp to [0, 1], and emit edges only when both nodes exist.
Test coverage for attribute order tolerance
test/graph.test.ts
Adds test case mocking reordered entity/relationship attributes, verifying node/edge counts, correct node types, and proper edge weight values.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

  • rohitg00/agentmemory#494: Refactors parseGraphXml to use attribute/property parsing strategy with self-closing tag handling and relationship edge emission in src/functions/graph.ts, directly overlapping with the main PR's XML parser robustness improvements.
  • rohitg00/agentmemory#588: Modifies src/functions/graph.ts parseGraphXml parsing logic for XML entities and relationships around self-closing tag handling and attribute matching, related to the main PR's attribute order tolerance refactor.

Poem

🐰 Attributes dance in any order now,
No fixed pattern dictates the vow.
Weights are clamped and nodes aligned,
XML flexibility redefined!
A parser strong, both wise and kind. 🌟

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.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 PR title accurately reflects the main change: fixing the graph parser to tolerate reordered XML attributes on entity and relationship tags, which directly addresses the changeset's core refactoring.
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 docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/635-codex-xml-attr-order

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

ESLint skipped: no ESLint configuration detected in root package.json. To enable, add eslint to devDependencies.


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.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
src/functions/graph.ts (1)

18-23: ⚡ Quick win

Remove implementation-descriptive comments in src parser block.

These comments explain what the code does; please replace with clearer naming/tests and keep comments only for non-obvious intent.

As per coding guidelines "Avoid code comments explaining WHAT — use clear naming instead".

Also applies to: 45-49

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/functions/graph.ts` around lines 18 - 23, The block comment in
src/functions/graph.ts that describes the parser's implementation (how it parses
key="value" pairs and why it was changed) should be removed; instead, rename the
parser utilities (e.g., parseAttributes, parseEntity, parseRelationship) and
their parameters to be self-explanatory and add unit tests that demonstrate
expected behavior (attribute order independence and handling of missing fields)
so the intent is captured by names/tests rather than an implementation-comment;
retain only brief comments for non-obvious intent or edge-case rationale near
the relevant functions.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@src/functions/graph.ts`:
- Around line 18-23: The block comment in src/functions/graph.ts that describes
the parser's implementation (how it parses key="value" pairs and why it was
changed) should be removed; instead, rename the parser utilities (e.g.,
parseAttributes, parseEntity, parseRelationship) and their parameters to be
self-explanatory and add unit tests that demonstrate expected behavior
(attribute order independence and handling of missing fields) so the intent is
captured by names/tests rather than an implementation-comment; retain only brief
comments for non-obvious intent or edge-case rationale near the relevant
functions.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: ea4dc7d8-95f5-4f9d-a229-947d37d38e30

📥 Commits

Reviewing files that changed from the base of the PR and between 4dd1dde and c985ecf.

📒 Files selected for processing (2)
  • src/functions/graph.ts
  • test/graph.test.ts

@rohitg00 rohitg00 merged commit 8d6a02f into main May 27, 2026
7 checks passed
@rohitg00 rohitg00 deleted the fix/635-codex-xml-attr-order branch May 27, 2026 17:31
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