mirror of
https://github.com/safishamsi/graphify.git
synced 2026-09-23 14:05:43 +00:00
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:
co-authored by
Cursor
parent
8e4c803f31
commit
ef0e6ee681
+5
-1
@@ -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
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user