Skip to content

build_merge: the #479 shrink guard reads the POST-prune node count, so replace-on-re-extract can destroy any number of nodes silently #2497

Closed

Description

@sortakool

Summary

build_merge's #479 shrink guard compares against the post-prune node
count, so it is structurally unable to see nodes lost to replace-on-re-extract.
A merge can destroy an arbitrary number of existing nodes and the guard stays
silent — including when the graph shrinks overall.

Observed on 0.9.34 (current PyPI release, and our pin).

The mechanism

graphify/build.py, build_merge:

1478  existing_nodes, existing_edges, existing_hyperedges, existing_directed = _loaded
...
1531  if new_sources:
1532      def _kept(item: dict) -> bool: ...
1536      existing_nodes = [n for n in existing_nodes if _kept(n)]   # <-- REBOUND, post-prune
1537      existing_edges  = [e for e in existing_edges  if _kept(e)]
...
1648  if graph_path.exists() and not dedup and not prune_sources:
1650      existing_n = len(existing_nodes)                           # <-- reads the PRUNED list
1651      new_n = G.number_of_nodes()
1652      if new_n < existing_n:
1653          raise ValueError("build_merge would shrink graph from ...")

Line 1536 rebinds existing_nodes to the list with the re-extracted files'
nodes already removed. By line 1650 the very quantity the guard exists to
notice has been subtracted from the left-hand side, so existing_n under-counts
by exactly the number of nodes the merge replaced.

Why it matters more than a one-tier re-extract

The replacement key is source_file. Two unrelated inputs that happen to
produce the same source_file string therefore replace each other — and the
guard cannot report it.

Concretely: chunk A contributes 10 nodes for source_file: "CHANGELOG.md".
Chunk B, from a different project, contributes 1 node for the same string.
Merging B drops A's 10 and adds 1. At line 1650 existing_n is 0 (A's ten were
pruned at 1536), new_n is 1, 1 < 0 is False — no refusal, for a 90% shrink
of that file's contribution.

What we measured

In our own corpus (2026-08-06), a merge printed +796 nodes while the total
rose 681 — 115 existing nodes replaced, 72 of them belonging to an
unrelated project
whose CHANGELOG.md collided on a bare basename. The call
was build_merge([chunk], graph_path=..., prune_sources=None, root=..., directed=False, dedup=False),
so the guard's precondition at 1648 was satisfied and it was active. It did not
fire, and nothing else did: the chunk was schema-valid, the build exited 0.

Subtracting the two printed numbers by hand was the only detector.

Suggested fix

Keep the pre-prune count for the comparison, e.g. capture
existing_n = len(existing_nodes) immediately after _load_existing_graph
(before the 1536 rebind) and compare that at 1650 — or, since replacement is
legitimate, report the replaced count explicitly rather than folding it into a
shrink test:

replaced = pre_prune_count - len(existing_nodes)
if replaced:
    print(f"[graphify] Replaced {replaced} node(s) from {len(new_sources)} re-extracted source file(s).",
          file=sys.stderr)

That mirrors the existing Pruned N node(s) from ... deleted or excluded source file(s). line at ~1620, which does report its own effect. Replacement is the
only mutation of comparable size that currently happens silently.

Not claimed

The mechanism above is read from the installed 0.9.34 source and corroborated by
the production observation; we have not run a minimal standalone repro against
build_merge in isolation.

Activity

  1. added a commit that references this issue on Aug 6, 2026
  2. added a commit that references this issue on Aug 6, 2026
  3. safishamsi commented on Aug 7, 2026

    @safishamsi
    Member

    Fixed in v0.9.35 (graphifyy==0.9.35 on PyPI). The #479 shrink guard now snapshots the on-disk baseline before the replace-on-re-extract rebind and diffs by node identity, refusing any loss from a source that was neither re-extracted nor pruned this run (active even under prune_sources, skipped only under dedup); it also reports how many nodes a re-extract replaced. Thanks for the detailed root-cause.

  4. added a commit that references this issue on Aug 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions