Skip to content

Graphs that teach > graphs that impress. Turn any code into an interactive knowledge graph you can explore, search, and ask questions about. Works with Claude Code, Codex, Cursor, Copilot, Gemini CLI, and more.

fix: coerce edge weights to float before comparison in merge scripts

Lum1104660f76678315
diff --git a/understand-anything-plugin/skills/understand/merge-batch-graphs.py b/understand-anything-plugin/skills/understand/merge-batch-graphs.py
index 1896f50d..54a1b5c8 100644
--- a/understand-anything-plugin/skills/understand/merge-batch-graphs.py
+++ b/understand-anything-plugin/skills/understand/merge-batch-graphs.py
@@ -69,6 +69,14 @@ COMPLEXITY_MAP: dict[str, str] = {
VALID_COMPLEXITY = {"simple", "moderate", "complex"}
+def _num(v: Any) -> float:
+ """Coerce a value to float for safe comparison (handles string weights)."""
+ try:
+ return float(v)
+ except (TypeError, ValueError):
+ return 0.0
+
+
# ── Batch loading ─────────────────────────────────────────────────────────
def load_batch(path: Path) -> dict[str, Any] | None:
@@ -284,7 +292,7 @@ def merge_and_normalize(batches: list[dict[str, Any]]) -> tuple[dict[str, Any],
key = (src, tgt, etype)
existing = edges_by_key.get(key)
- if existing is None or edge.get("weight", 0) > existing.get("weight", 0):
+ if existing is None or _num(edge.get("weight", 0)) > _num(existing.get("weight", 0)):
edges_by_key[key] = edge
# ── Build report ─────────────────────────────────────────────────
diff --git a/understand-anything-plugin/skills/understand/merge-subdomain-graphs.py b/understand-anything-plugin/skills/understand/merge-subdomain-graphs.py
index 7e6de82a..4b82ef0e 100644
--- a/understand-anything-plugin/skills/understand/merge-subdomain-graphs.py
+++ b/understand-anything-plugin/skills/understand/merge-subdomain-graphs.py
@@ -25,6 +25,14 @@ from pathlib import Path
from typing import Any
+def _num(v: Any) -> float:
+ """Coerce a value to float for safe comparison (handles string weights)."""
+ try:
+ return float(v)
+ except (TypeError, ValueError):
+ return 0.0
+
+
def load_graph(path: Path) -> dict[str, Any] | None:
"""Load and minimally validate a knowledge graph JSON file."""
try:
@@ -77,7 +85,7 @@ def merge_graphs(graphs: list[dict[str, Any]]) -> tuple[dict[str, Any], list[str
edges_by_key[key] = edge
else:
edge_dedup_count += 1
- if edge.get("weight", 0) > existing.get("weight", 0):
+ if _num(edge.get("weight", 0)) > _num(existing.get("weight", 0)):
edges_by_key[key] = edge
# Drop edges referencing missing nodes