Skip to content

fix(mcp): report duplicate kg_add reuse - #2286

Open
MichaelWave369 wants to merge 1 commit into
MemPalace:developfrom
MichaelWave369:fix/kg-add-duplicate-reason
Open

fix(mcp): report duplicate kg_add reuse#2286
MichaelWave369 wants to merge 1 commit into
MemPalace:developfrom
MichaelWave369:fix/kg-add-duplicate-reason

Conversation

@MichaelWave369

Copy link
Copy Markdown

Closes #2268.

tool_kg_add previously returned the same success shape for a fresh insert and an existing open triple reused by KnowledgeGraph.add_triple(). That meant a caller could not tell whether this call created the fact or reused an existing row whose provenance remained unchanged.

This patch keeps deduplication and storage semantics untouched and makes the MCP response truthful:

  • fresh insert: preserves the existing response shape, with no reason
  • reused open triple: adds "reason": "already_exists"
  • duplicate detection follows the KG's existing entity/predicate normalization
  • invalidated historical triples do not count as duplicates; a later re-add is reported as fresh

This deliberately does not modify KnowledgeGraph.add_triple(), so it is orthogonal to #1139's NULL-metadata backfill work.

Regression coverage pins three cases:

  1. a duplicate with different provenance reports already_exists, returns the same triple id, and leaves the original stored provenance intact;
  2. equivalent entity spellings that normalize to the same KG identity still report reuse;
  3. re-adding a relationship after the prior triple was invalidated is treated as a fresh insert.

Validation against current develop (639c69a):

  • python -m pytest -q tests/test_kg_add_duplicate_reason.py
  • python -m pytest -q tests/test_mcp_server.py -k 'kg_add or kg_query'
  • ruff check mempalace/mcp_server.py tests/test_kg_add_duplicate_reason.py
  • ruff format --check mempalace/mcp_server.py tests/test_kg_add_duplicate_reason.py
  • python -m compileall -q mempalace/mcp_server.py tests/test_kg_add_duplicate_reason.py

All passed locally in the fork's GitHub Actions validation run.

@igorls igorls added bug Something isn't working area/mcp MCP server and tools area/kg Knowledge graph labels Aug 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/kg Knowledge graph area/mcp MCP server and tools bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

add_triple discards provenance on a duplicate fact and kg_add reports it as a fresh insert

2 participants