Skip to content

_pick_winner can discard a real declaration in favour of a source-less stub #3775

Closed

Description

@BloodyBeard

Summary

In graphify/dedup.py, _pick_winner() chooses which of several colliding node records survives, ranking them by _content_richness(). That richness score explicitly ignores source_file and source_location (they're listed in _RICHNESS_IGNORED_KEYS) — which are precisely the fields that distinguish a real, located declaration from a placeholder.

The result: a node with no source_file at all can out-score and replace the genuine declaration it collides with, purely because it happens to carry one or two extra bookkeeping keys (e.g. external, type, _origin).

Reproduction

Run against an unmodified dedup.py:

from graphify.dedup import _content_richness, _pick_winner

real = {
    "id": "docs_guide_setup", "label": "Setup", "file_type": "concept",
    "source_file": "docs/guide.md", "source_location": "L12",
    "attributes": {"kind": "section"},
}
stub = {
    "id": "setup", "label": "Setup", "file_type": "concept",
    "source_file": "", "type": "external", "external": True,
    "_origin": "semantic",
}

print(_content_richness(real))      # 2
print(_content_richness(stub))      # 3
print(_pick_winner([real, stub]))   # -> the stub wins

Observed: _pick_winner returns the stub (id: "setup"), discarding the record that has an actual file and line.

Expected: the record with a real source_file should win. Provenance is a stronger signal of canonicity than incidental field count.

Scope (checked, not assumed)

This does not affect code graphs. Both passes of deduplicate_entities() skip file_type == "code" nodes (if _is_code(node): continue), so _pick_winner is never reached for code symbols. The exposure is non-code node types — concept, document, paper, image — i.e. the semantic/document extraction path.

I mention this because I initially suspected this bug explained a missing function in a Rust graph, and that turned out to be wrong: the function was never missing, and the cause was elsewhere entirely. The scoring flaw above is real and reproducible on its own merits, but I don't want to overstate its blast radius.

Suggested fix

Add a provenance tiebreak ahead of richness, so a node with a real source location always beats one without:

def _score(n: dict) -> tuple[int, int, int, int]:
    has_suffix = bool(_CHUNK_SUFFIX.search(n["id"]))
    no_source = 0 if n.get("source_file") else 1
    return (1 if has_suffix else 0, no_source, -_content_richness(n), len(n["id"]))

(Original: return (1 if has_suffix else 0, -_content_richness(n), len(n["id"])))

This preserves the existing ordering in every case where both records have — or both lack — provenance, and only changes the outcome in the ambiguous case the bug concerns.

Environment

  • graphify 0.9.65 (PyPI package graphifyy)
  • Python 3.14, Windows 10

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