From 65114cb4771ff0e38d944d8f5c9f7f41f8b881c7 Mon Sep 17 00:00:00 2001 From: Jeroen Schweitzer Date: Wed, 10 Dec 2025 02:10:31 +0100 Subject: [PATCH] fix(library-desk): fix idempotency in create_entity_mentions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix create_entity_mentions to correctly count only newly created relationships, not all relationships processed by MERGE. **Problem**: count(r) was returning ALL relationships touched by MERGE (both created and matched), breaking idempotency tests. **Solution**: Use temporary flag 'just_created' set only ON CREATE, filter to those relationships, count them, then remove the flag. This ensures the count only includes new relationships. Now properly returns: - N on first run (N new relationships created) - 0 on subsequent runs (no new relationships, all already exist) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- services/library-desk/src/services/graph_service.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/services/library-desk/src/services/graph_service.py b/services/library-desk/src/services/graph_service.py index c32798e..2288901 100644 --- a/services/library-desk/src/services/graph_service.py +++ b/services/library-desk/src/services/graph_service.py @@ -1158,11 +1158,16 @@ Feel free to expand it with more details! // Create MENTIONS relationship if it doesn't exist MERGE (d)-[r:MENTIONS]->(e) - ON CREATE SET r.created_at = datetime(), + ON CREATE SET r.just_created = true, + r.created_at = datetime(), r.mention_count = 1 ON MATCH SET r.updated_at = datetime(), r.mention_count = COALESCE(r.mention_count, 0) + 1 + // Count only newly created relationships + WITH r + WHERE r.just_created = true + REMOVE r.just_created RETURN count(r) as relationships_created """