harden community serialization fallbacks

Use safe JSON serialization fallbacks for deterministic sort keys in clustering and graph canonicalization, and skip invalid community IDs with a stderr warning instead of raising during update rebuilds.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Ahmad Fathallah
2026-05-12 02:37:38 +03:00
co-authored by Cursor
parent 8e4c803f31
commit ef0e6ee681
2 changed files with 15 additions and 3 deletions
+5 -1
View File
@@ -32,7 +32,11 @@ def _partition(G: nx.Graph) -> dict[str, int]:
stable.add_nodes_from(sorted(G.nodes(), key=str))
edge_rows = sorted(
G.edges(data=True),
key=lambda row: (str(row[0]), str(row[1]), json.dumps(row[2], sort_keys=True, ensure_ascii=False)),
key=lambda row: (
str(row[0]),
str(row[1]),
json.dumps(row[2], sort_keys=True, ensure_ascii=False, default=str),
),
)
for src, tgt, attrs in edge_rows:
stable.add_edge(src, tgt, **attrs)
+10 -2
View File
@@ -123,7 +123,15 @@ def _node_community_map(graph_data: dict) -> dict[str, int]:
cid = node.get("community")
if node_id is None or cid is None:
continue
out[str(node_id)] = int(cid)
try:
out[str(node_id)] = int(cid)
except (TypeError, ValueError):
print(
f"[graphify watch] Skipping node with invalid community id: "
f"node_id={node_id!r} community={cid!r}",
file=sys.stderr,
)
continue
return out
@@ -134,7 +142,7 @@ def _canonical_graph_for_compare(graph_data: dict) -> dict:
if key in canonical and isinstance(canonical[key], list):
canonical[key] = sorted(
canonical[key],
key=lambda item: json.dumps(item, sort_keys=True, ensure_ascii=False),
key=lambda item: json.dumps(item, sort_keys=True, ensure_ascii=False, default=str),
)
return canonical