Skip to content

Commit 7bfd59c

Browse files
SrijanSrivsafishamsi
authored andcommitted
fix(update): refuse a saved semantic id when the total grows
Compare saved semantic ids with the new graph instead of the node total. A sourceless stub cannot fill a missing id, and a code rebuild is not a deletion. A legacy id that build_from_json rewrites to its canonical stem is the same node, so cluster-only still writes.
1 parent 3050270 commit 7bfd59c

3 files changed

Lines changed: 96 additions & 1 deletion

File tree

‎graphify/build.py‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,53 @@ def _backfill_origin(item: dict) -> None:
6666
item.pop("_origin", None)
6767
return
6868
item.setdefault("_origin", "ast" if _is_ast_tier(item) else "semantic")
69+
def _lost_saved_semantic_nodes(
70+
saved_nodes: list,
71+
new_nodes: list,
72+
deleted_sources: set[str] | None = None,
73+
) -> list[dict]:
74+
"""Saved semantic nodes whose ids are absent from the new graph.
75+
76+
A node created in this run is not in ``saved_nodes``, so a sourceless stub
77+
cannot stand in for a missing id. ``deleted_sources`` is the files the user
78+
removed. A code rebuild is not a deletion: do not pass the rebuilt set.
79+
"""
80+
deleted = {
81+
norm
82+
for source in (deleted_sources or ())
83+
if (norm := _norm_source_file(source))
84+
}
85+
new_ids = {node.get("id") for node in new_nodes if isinstance(node, dict)}
86+
# cluster-only and build_from_json rewrite a legacy semantic id to its
87+
# canonical stem, then fold a bare doc id into its `_doc` twin. That id
88+
# is the same node. A missing rationale id has no such successor.
89+
stem_remap = _semantic_id_remap(saved_nodes, None)
90+
rewritten = [
91+
{**node, "id": stem_remap[node["id"]]}
92+
if isinstance(node, dict) and node.get("id") in stem_remap
93+
else node
94+
for node in saved_nodes
95+
]
96+
doc_remap = _doc_twin_remap(rewritten)
97+
98+
def _still_there(node_id: str) -> bool:
99+
if node_id in new_ids:
100+
return True
101+
renamed = doc_remap.get(stem_remap.get(node_id, node_id), stem_remap.get(node_id, node_id))
102+
return renamed in new_ids
103+
104+
lost: list[dict] = []
105+
for node in saved_nodes:
106+
if not isinstance(node, dict) or _is_ast_tier(node):
107+
continue
108+
node_id = node.get("id")
109+
if isinstance(node_id, str) and _still_there(node_id):
110+
continue
111+
source = _norm_source_file(node.get("source_file"))
112+
if source and source in deleted:
113+
continue
114+
lost.append(node)
115+
return lost
69116

70117

71118
# Relations that say only "these two symbols appear together", with no claim about

‎graphify/export.py‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,10 +310,14 @@ def to_json(G: nx.Graph, communities: dict[int, list[str]], output_path: str, *,
310310
# Empty/whitespace existing file (e.g. a freshly touched path):
311311
# no nodes to lose, so any new graph is a growth — proceed.
312312
existing_n = 0
313+
existing_nodes = []
313314
else:
314315
try:
315316
existing_data = json.loads(raw)
316-
existing_n = len(existing_data.get("nodes", []))
317+
existing_nodes = existing_data.get("nodes", [])
318+
if not isinstance(existing_nodes, list):
319+
existing_nodes = []
320+
existing_n = len(existing_nodes)
317321
except Exception as exc:
318322
# Non-empty but unparseable existing graph (corrupt or a
319323
# mid-write): we cannot verify the new graph is not a silent
@@ -330,6 +334,21 @@ def to_json(G: nx.Graph, communities: dict[int, list[str]], output_path: str, *,
330334
)
331335
return False
332336
new_n = G.number_of_nodes()
337+
from graphify.build import _lost_saved_semantic_nodes
338+
new_node_list = [
339+
{"id": node_id, **{key: value for key, value in attrs.items() if key != "id"}}
340+
for node_id, attrs in G.nodes(data=True)
341+
]
342+
missing = _lost_saved_semantic_nodes(existing_nodes, new_node_list, set())
343+
if missing:
344+
import sys as _sys
345+
ids = ", ".join(str(node.get("id")) for node in missing)
346+
print(
347+
"[graphify] WARNING: Refusing to overwrite — saved semantic "
348+
f"node(s) are missing from the new graph: {ids}.",
349+
file=_sys.stderr,
350+
)
351+
return False
333352
if new_n < existing_n:
334353
import sys as _sys
335354
print(

‎graphify/watch.py‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1433,6 +1433,7 @@ def _check_shrink(
14331433
had_explicit_deletions: bool = False,
14341434
rebuilt_sources: "set[str] | None" = None,
14351435
failed_sources: "set[str] | None" = None,
1436+
deleted_sources: "set[str] | None" = None,
14361437
) -> bool:
14371438
"""Return True (ok to proceed) or False (shrink refused).
14381439
@@ -1454,6 +1455,12 @@ def _check_shrink(
14541455
``--force`` (#1116 left stale nodes write-blocked even though build dropped them).
14551456
Files in ``failed_sources`` never account for lost nodes: extraction did not
14561457
complete, so their disappearance is the silent shrink this guard protects.
1458+
1459+
A larger total is not enough when a saved semantic id is gone (#2229).
1460+
``deleted_sources`` is the files the user removed. A semantic node on one of
1461+
those files may go. A semantic node on a file that is still present may not.
1462+
The rebuilt set is not that list: an update re-extracts code and must not
1463+
treat a rationale node on that file as a deleted symbol.
14571464
"""
14581465
if force or not existing_data:
14591466
return True
@@ -1469,6 +1476,26 @@ def _check_shrink(
14691476
return True
14701477
existing_nodes = existing_data.get("nodes", [])
14711478
new_nodes = new_data.get("nodes", [])
1479+
from graphify.build import _lost_saved_semantic_nodes
1480+
missing = _lost_saved_semantic_nodes(existing_nodes, new_nodes, deleted_sources)
1481+
if missing:
1482+
if tmp is not None:
1483+
tmp.unlink(missing_ok=True)
1484+
ids = ", ".join(str(node.get("id")) for node in missing)
1485+
print(
1486+
"[graphify] WARNING: Refusing to overwrite — saved semantic "
1487+
f"node(s) are missing from the new graph: {ids}.",
1488+
file=sys.stderr,
1489+
)
1490+
if len(new_nodes) < len(existing_nodes):
1491+
print(
1492+
f"[graphify] WARNING: new graph has {len(new_nodes)} nodes but existing "
1493+
f"graph.json has {len(existing_nodes)}. Refusing to overwrite — you may be "
1494+
f"missing chunk files from a previous session. "
1495+
f"Pass --force to override.",
1496+
file=sys.stderr,
1497+
)
1498+
return False
14721499
if len(new_nodes) >= len(existing_nodes):
14731500
return True
14741501
if rebuilt_sources is not None:
@@ -2233,6 +2260,7 @@ def _failed(f: str) -> bool:
22332260
had_explicit_deletions=bool(deleted_paths),
22342261
rebuilt_sources=rebuilt_sources,
22352262
failed_sources=failed_sources,
2263+
deleted_sources=deleted_paths,
22362264
):
22372265
return False
22382266
from graphify.export import backup_if_protected as _backup
@@ -2449,6 +2477,7 @@ def _failed(f: str) -> bool:
24492477
had_explicit_deletions=bool(deleted_paths),
24502478
rebuilt_sources=rebuilt_sources,
24512479
failed_sources=failed_sources,
2480+
deleted_sources=deleted_paths,
24522481
):
24532482
return False
24542483
from graphify.exporters.html import _HTML_STALE_MARKER

0 commit comments

Comments
 (0)