Skip to content

fix(build): ghost-merge nodes whose source_file is the referencing doc, not the referenced file - #3345

Closed
leninherrera94 wants to merge 1 commit into
Graphify-Labs:v8from
leninherrera94:fix/ghost-merge-wrong-source-file
Closed

leninherrera94 wants to merge 1 commit into
Graphify-Labs:v8from
leninherrera94:fix/ghost-merge-wrong-source-file

Conversation

@leninherrera94

Copy link
Copy Markdown
Contributor

Fixes #3344.

Problem

build_from_json()'s ghost-merge pass (the "#1145-extended" logic collapsing a semantic/LLM-extracted duplicate into its AST-canonical twin) keys exclusively on (source_file, label). When a semantic pass reads a document that only mentions a real source file by name in prose — a saved graphify-out/memory/*.md query answer, a runbook narrating "see App.tsx" or "backup.sh does X" — the node it mints gets source_file = the document being read, not the file actually named in the text. That wrong source_file means the key can never match the real AST node's key, so the ghost survives every subsequent rebuild, forever.

Fix

Add a conservative label-only fallback ("Pass 2b") that reuses the already-existing _is_file_node_label() predicate (currently only used for file-node label disambiguation) to match a ghost against every AST file-self node collected in Pass 1, when the exact (source_file, label) key doesn't resolve it. Only remaps when the match is unique across the graph — a label matching 0 or 2+ real files (e.g. two different index.ts files) is left alone, mirroring the conservatism the existing key-based path already applies via _loc_collisions.

No other code needed to change: _ghost_remap already flows into the existing norm_to_id map that both edge endpoints and hyperedge members resolve through.

Testing

  • Added two regression tests in tests/test_build.py:
    • test_ghost_merge_wrong_source_file_resolved_by_label — the fix's core positive case, including the "prose dropped a leading path segment" variant (customer-app/index.ts vs the real apps/customer-app/index.ts).
    • test_ghost_merge_wrong_source_file_ambiguous_basename_left_alone — confirms the fallback stays conservative when a basename is shared by two real files.
  • Full existing suite: uv run pytest -q → 5228 passed, 93 skipped (unchanged from the pre-patch baseline on this checkout — the only failures present with or without this patch are pre-existing and unrelated: test_ollama_retry_cap.py needs the optional openai extra not installed here, and test_skillgen.py's git-history audits need a commit ref my shallow clone doesn't have).
  • ruff check clean on both changed files.
  • Verified against a real ~4600-node project graph (not included in this repo): 10 long-standing ghost nodes removed across two unrelated documents (a memory query file and an ops runbook), confirming this is a general failure mode of the "document mentions a file by name" pattern rather than a narrow edge case. Both hyperedges that had cited the phantoms now correctly cite only real, AST-backed nodes; graph health diagnostic stayed clean before and after.

…c, not the referenced file

The Graphify-Labs#1145-extended ghost-merge (build_from_json) keys exclusively on
(source_file, label). When a semantic pass reads a document that only
*mentions* a real file by name (a saved graphify-out/memory/*.md query
answer, a runbook like "see App.tsx"/"backup.sh does X"), the new node
it mints gets source_file = the document being read, not the file
actually named in the prose. That wrong source_file means the key can
never match the real AST node's, so the ghost survives every rebuild.

Add a conservative label-only fallback pass reusing the existing
_is_file_node_label() predicate (already used for file-node label
disambiguation) against every AST file-self node collected in Pass 1.
Only remaps when the match is unique across the graph — 0 or 2+
candidates (e.g. two different index.ts files) are left alone, same
conservatism the existing (source_file, label) key already applies via
_loc_collisions.

Verified against a real ~4600-node project graph: 10 long-standing
ghost nodes removed across two unrelated documents (a memory query
file and an ops runbook), confirming this is a general failure mode of
the "document mentions a file by name" pattern, not a narrow edge
case. Both hyperedges that had cited the phantoms now correctly cite
only real, AST-backed nodes.

Fixes Graphify-Labs#3344

@graphify-labs graphify-labs Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Graphify reviewed this change.

Looks safe to merge — no coupling regressions and no blocking issues, checked against the code graph (not a self-assessment).

Formal verification. No changes could be formally verified in this run.


Graphify review — findings

Adds a label-only ghost-merge fallback (Pass 2b in build_from_json) that folds semantic nodes which merely mention a file into the real AST file node, covering the case where a self-referential pass over a memory doc/README stamps the document's path as source_file instead of the mentioned file's, so the exact (source_file, label) key can never match. Matching reuses _is_file_node_label, so bare basenames and directory-qualified suffixes ("customer-app/index.ts" vs "apps/customer-app/index.ts") resolve, while a label hitting 0 or 2+ AST files is left untouched to avoid arbitrary merges.

No blocking issues surfaced. 5 lower-confidence candidates did not survive cross-model review.

Analysis details — impact, health, verification

Impact & health

Graphify review

Impact — 1243 functions depend on the 233 functions this change touches.

Health — this change adds coupling hotspots:

  • new: _rebuild_code() — 113 callers, 50 callees
  • new: build_from_json() — 199 callers, 19 callees
  • new: build_merge() — 62 callers, 13 callees
  • new: to_obsidian() — 38 callers, 14 callees
  • new: extract_files_direct() — 17 callers, 20 callees
  • new: to_wiki() — 41 callers, 7 callees
  • new: extract_corpus_parallel() — 26 callers, 11 callees
  • new: _call_claude_cli() — 31 callers, 9 callees
  • …and 38 more — each is listed as a finding

Verification — 1243 functions in the blast radius were not formally verified this run (proofs are advisory here).

Gate & verification

graphify gate

PASS — objectively clean (no health regressions, tests not run — proofs not run this pass (advisory)). Grounded, not self-assessed.

Advisory (not blocking):

  • verification_scope: 839 function(s) in the blast radius were not formally verified this run

Formal verification

Could not verify: Could not verify build\_from\_json.

The verifier did not have enough to check build\_from\_json, so it is saying so rather than guessing. No false assurance is the whole point.

Guarantee: No guarantee either way, this is an honest abstention, not a pass.

Note: Reason: not verifiable: all 6 sampled inputs raised on both versions — the function never executed, so 'no divergence' would be vacuous (mostly NameError — names the real obstacle, not a sampling gap)

· 46 more finding(s) on lines outside this diff (see the check run).

safishamsi added a commit that referenced this pull request Sep 5, 2026
The #3345 ghost-merge fix cited #2963 (an unrelated Rust Self-keyword bug)
in its comments and test docstrings; the issue it actually resolves is #3344.
@safishamsi

Copy link
Copy Markdown
Member

Shipped in v0.9.55 — landed on v8 via cherry-pick with your authorship preserved. Thanks @leninherrera94 — ghost nodes mis-attributed to the referencing doc now merge onto the real file node. Release: https://github.com/Graphify-Labs/graphify/releases/tag/v0.9.55

@safishamsi safishamsi closed this Sep 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Ghost-merge misses semantic nodes whose source_file is the referencing doc, not the referenced file

2 participants