-
Notifications
You must be signed in to change notification settings - Fork 98
feat: Entity Identity Synthesis — first-class Entity nodes with semantic memory #124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jescalan
wants to merge
7
commits into
verygoodplugins:main
Choose a base branch
from
jescalan:feat/entity-identities
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
fcfb458
feat: entity identity synthesis — first-class Entity nodes with seman…
jescalan 94f08a0
fix: address PR review comments
jescalan f16d959
fix: self-merge guard, missing import, test assertion
jescalan 2a7e643
style: run black + isort on all PR-changed files
jescalan bf2fea3
fix: address third round of review comments
jescalan d42b837
fix: docstring threshold, defensive ref count parse, retry failed ide…
jescalan 814a9e8
Merge branch 'main' into feat/entity-identities
jack-arturo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,3 +41,4 @@ node_modules/ | |
| /benchmarks/snapshots/ | ||
| /benchmarks/results/ | ||
| benchmarks/baselines/locomo_baseline.json | ||
| data/ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,218 @@ | ||
| """Entity API endpoints for AutoMem. | ||
|
|
||
| Provides CRUD and query operations for first-class Entity nodes. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import json | ||
| import logging | ||
| from typing import Any, Callable, Dict, List, Optional | ||
|
|
||
| from flask import Blueprint, abort, jsonify, request | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def _parse_aliases(raw: Any) -> List[str]: | ||
| """Safely parse aliases from a graph result (may be list or JSON string).""" | ||
| if not raw: | ||
| return [] | ||
| if isinstance(raw, list): | ||
| return raw | ||
| if isinstance(raw, str): | ||
| try: | ||
| parsed = json.loads(raw) | ||
| return parsed if isinstance(parsed, list) else [] | ||
| except Exception: | ||
| return [] | ||
| return [] | ||
|
|
||
|
|
||
| def _serialize_entity_row(row: list) -> Dict[str, Any]: | ||
| """Convert a result row into a serialisable entity dict. | ||
|
|
||
| Expected column order (12 columns): | ||
| 0: e.id, 1: e.slug, 2: e.category, 3: e.name, 4: e.aliases, | ||
| 5: e.identity, 6: e.identity_version, 7: e.identity_updated_at, | ||
| 8: e.identity_source_count, 9: ref_count, | ||
| 10: e.created_at, 11: e.last_referenced_at | ||
| """ | ||
| return { | ||
| "id": row[0], | ||
| "slug": row[1], | ||
| "category": row[2], | ||
| "name": row[3], | ||
| "aliases": _parse_aliases(row[4]), | ||
| "identity": row[5], | ||
| "identity_version": int(row[6] or 0), | ||
| "identity_updated_at": row[7], | ||
| "identity_source_count": int(row[8] or 0), | ||
| "reference_count": int(row[9] or 0), | ||
| "created_at": row[10] if len(row) > 10 else None, | ||
| "last_referenced_at": row[11] if len(row) > 11 else None, | ||
| } | ||
|
|
||
|
|
||
| def create_entity_blueprint( | ||
| get_memory_graph: Callable[[], Any], | ||
| logger: Any, | ||
| ) -> Blueprint: | ||
| """Create the entity API blueprint.""" | ||
|
|
||
| bp = Blueprint("entity", __name__) | ||
|
jescalan marked this conversation as resolved.
|
||
|
|
||
| @bp.route("/entities", methods=["GET"]) | ||
| def list_entities() -> Any: | ||
| graph = get_memory_graph() | ||
| if graph is None: | ||
| abort(503, description="FalkorDB is unavailable") | ||
|
|
||
| category = request.args.get("category") | ||
| limit = min(int(request.args.get("limit", 100)), 500) | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
|
|
||
| if category: | ||
| result = graph.query( | ||
| """ | ||
| MATCH (e:Entity) | ||
| WHERE e.merged_into IS NULL AND e.category = $category | ||
| OPTIONAL MATCH (e)-[ref:REFERENCED_IN]->() | ||
| WITH e, count(ref) as ref_count | ||
| RETURN e.id, e.slug, e.category, e.name, e.aliases, | ||
| e.identity, e.identity_version, e.identity_updated_at, | ||
| e.identity_source_count, ref_count, | ||
| e.created_at, e.last_referenced_at | ||
| ORDER BY ref_count DESC | ||
| LIMIT $limit | ||
| """, | ||
| {"category": category, "limit": limit}, | ||
| ) | ||
| else: | ||
| result = graph.query( | ||
| """ | ||
| MATCH (e:Entity) | ||
| WHERE e.merged_into IS NULL | ||
| OPTIONAL MATCH (e)-[ref:REFERENCED_IN]->() | ||
| WITH e, count(ref) as ref_count | ||
| RETURN e.id, e.slug, e.category, e.name, e.aliases, | ||
| e.identity, e.identity_version, e.identity_updated_at, | ||
| e.identity_source_count, ref_count, | ||
| e.created_at, e.last_referenced_at | ||
| ORDER BY ref_count DESC | ||
| LIMIT $limit | ||
| """, | ||
| {"limit": limit}, | ||
| ) | ||
|
|
||
| entities = [] | ||
| for row in getattr(result, "result_set", []) or []: | ||
| entities.append(_serialize_entity_row(row)) | ||
|
|
||
| return jsonify({"status": "success", "entities": entities, "count": len(entities)}) | ||
|
|
||
| @bp.route("/entity/<slug>", methods=["GET"]) | ||
| def get_entity(slug: str) -> Any: | ||
| graph = get_memory_graph() | ||
| if graph is None: | ||
| abort(503, description="FalkorDB is unavailable") | ||
|
|
||
| # Try direct slug match first, then alias lookup | ||
| result = graph.query( | ||
| """ | ||
| MATCH (e:Entity) | ||
| WHERE e.merged_into IS NULL AND (e.slug = $slug OR $slug IN e.aliases) | ||
| OPTIONAL MATCH (e)-[ref:REFERENCED_IN]->() | ||
| WITH e, count(ref) as ref_count | ||
| RETURN e.id, e.slug, e.category, e.name, e.aliases, | ||
| e.identity, e.identity_version, e.identity_updated_at, | ||
| e.identity_source_count, ref_count, | ||
| e.created_at, e.last_referenced_at | ||
| LIMIT 1 | ||
| """, | ||
| {"slug": slug}, | ||
| ) | ||
|
|
||
| rows = getattr(result, "result_set", []) or [] | ||
| if not rows: | ||
| abort(404, description=f"Entity '{slug}' not found") | ||
|
|
||
| entity = _serialize_entity_row(rows[0]) | ||
| return jsonify({"status": "success", "entity": entity}) | ||
|
|
||
| @bp.route("/entities/merge-candidates", methods=["GET"]) | ||
| def merge_candidates() -> Any: | ||
| graph = get_memory_graph() | ||
| if graph is None: | ||
| abort(503, description="FalkorDB is unavailable") | ||
|
|
||
| try: | ||
| from automem.consolidation.entity_dedup import find_merge_candidates | ||
|
|
||
| auto_merge, review = find_merge_candidates(graph) | ||
| auto_merge_ids = {(c.entity_a_id, c.entity_b_id) for c in auto_merge} | ||
| candidates = [] | ||
| for c in auto_merge + review: | ||
| candidates.append({ | ||
| "entity_a": c.entity_a_id, | ||
| "entity_b": c.entity_b_id, | ||
| "canonical": c.canonical_id, | ||
| "alias": c.alias_id, | ||
| "confidence": c.confidence, | ||
| "reason": c.reason, | ||
| "auto_merge": (c.entity_a_id, c.entity_b_id) in auto_merge_ids, | ||
| }) | ||
| return jsonify({"status": "success", "candidates": candidates, "count": len(candidates)}) | ||
| except Exception as exc: | ||
| logger.exception("Failed to find merge candidates") | ||
| return jsonify({"error": "Failed to find merge candidates", "details": str(exc)}), 500 | ||
|
|
||
| @bp.route("/entity/<slug>/merge", methods=["POST"]) | ||
| def merge_entity(slug: str) -> Any: | ||
| graph = get_memory_graph() | ||
| if graph is None: | ||
| abort(503, description="FalkorDB is unavailable") | ||
|
|
||
| data = request.get_json(silent=True) or {} | ||
| target_slug = data.get("merge_into") | ||
| if not target_slug: | ||
| abort(400, description="'merge_into' slug is required") | ||
|
|
||
| # Resolve both entities | ||
| alias_result = graph.query( | ||
| "MATCH (e:Entity) WHERE e.slug = $slug AND e.merged_into IS NULL RETURN e.id LIMIT 1", | ||
| {"slug": slug}, | ||
| ) | ||
| canonical_result = graph.query( | ||
| "MATCH (e:Entity) WHERE e.slug = $slug AND e.merged_into IS NULL RETURN e.id LIMIT 1", | ||
| {"slug": target_slug}, | ||
| ) | ||
|
|
||
| alias_rows = getattr(alias_result, "result_set", []) or [] | ||
| canonical_rows = getattr(canonical_result, "result_set", []) or [] | ||
|
|
||
| if not alias_rows: | ||
| abort(404, description=f"Entity '{slug}' not found") | ||
| if not canonical_rows: | ||
| abort(404, description=f"Target entity '{target_slug}' not found") | ||
|
|
||
| alias_id = alias_rows[0][0] | ||
| canonical_id = canonical_rows[0][0] | ||
|
|
||
| try: | ||
| from automem.consolidation.entity_dedup import merge_entities | ||
|
|
||
| merge_result = merge_entities(graph, canonical_id, alias_id) | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| return jsonify({ | ||
| "status": "success", | ||
| "merge": { | ||
| "canonical": merge_result.canonical_id, | ||
| "alias": merge_result.alias_id, | ||
| "alias_slug": merge_result.alias_slug, | ||
| "edges_moved": merge_result.edges_moved, | ||
| }, | ||
| }) | ||
| except Exception as exc: | ||
| logger.exception("Merge failed") | ||
| return jsonify({"error": "Merge failed", "details": str(exc)}), 500 | ||
|
|
||
| return bp | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.