Skip to content

fix(go): keep both halves of a case-only symbol collision (#2779) - #2780

Closed
catpotd wants to merge 1 commit into
Graphify-Labs:v8from
catpotd:fix/go-case-only-symbol-collision
Closed

catpotd wants to merge 1 commit into
Graphify-Labs:v8from
catpotd:fix/go-case-only-symbol-collision

Conversation

@catpotd

@catpotd catpotd commented Aug 16, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #2779 — a Go file declaring both Run and run produced one casefolded
node id, add_node silently dropped the second declaration, and the surviving
call sites bound by bare name to a same-named unexported function in another
package (an edge Go's visibility rules make impossible).

Scope: Go extractor only. The same first-wins add_node pattern exists in the
other extractors; #2774 reports the Rust flavor of the symptom (different
trigger — bare-name collision, not case-only), so I kept this PR to the language
I can verify end-to-end rather than patching 17 extractors blind.

Root cause

ids.py:normalize_id casefolds by design (AST / LLM / builder parity — #811,
#550, #1033, #1104), so the fix cannot be "stop lowercasing".
_disambiguate_colliding_node_ids cannot help either: it skips same-file
groups (len(source_keys) < 2) and its (old_id, source_key) remap key cannot
tell two symbols in the same file apart. So the collision has to be resolved
where both declarations are still visible: inside the extractor, before ids
leave the file.

Approach

A pre-pass over the file's declarations groups function/method names by their
would-be node id. When a group holds two case-variants:

  • the exported member keeps the plain id — only exported symbols are
    reachable across packages, so cross-package edges (and edges already stored in
    graph.json from files an incremental rebuild does not touch) all target the
    exported one; keeping its id stable means adding/removing an unexported
    sibling in an update re-points nothing. Docs likewise reference the exported
    API, so the casefolded id a semantic node produces lands on the symbol it
    describes.
  • the unexported sibling gets a salt (sha1(name)[:6],
    usedforsecurity=False per the existing convention). Calls to it can only
    come from the same package and only resolve once it exists, so salting it
    re-points nothing.
  • when no unique exported member exists (Run / RUN), every member is salted
    — the outcome never depends on declaration order.

With distinct ids, the existing resolver already does the rest: Run -> run
binds within the file as EXTRACTED, and the cross-package phantom disappears.

An earlier draft kept the plain id on the lowercase member instead; it
verified worse — a partial (hook / update) rebuild of just the declaring file
re-pointed stored cross-package edges from Run to run. The exported-member
rule survives that scenario (pinned by the two incremental tests).

Known limit

The reverse refactor — a file that only had unexported run (plain id) gains
Run, then a partial rebuild of only that file — re-points same-package
cross-file edges at the plain id to Run. Stock v8 mis-points the same edge in
that scenario and loses the run node entirely (first-wins is
declaration-order dependent), so this is no worse on any observable, but it is
not fixed either; a full rebuild reconciles it.

Verification

  • 4 new tests in tests/test_go_qualified_resolution.py:
    • both-halves-extracted and no-cross-package-binding fail on unpatched v8
    • exported-id-stability and partial-rebuild-edge-stability pin the
      incremental behavior that ruled out the earlier draft
  • Full suite: uv run --frozen pytest tests/ -q — 4520 passed / 7 skipped /
    0 failed
    (Python 3.12)
  • ruff (committed baseline) and bandit -ll both clean on the touched files
  • Real-corpus A/B (private Go codebase, ~1.4k nodes): the two dropped functions
    reappear, the phantom cross-package edge disappears, 5 body edges reattach to
    their real owner, zero changes outside the declaring file, and every
    pre-existing node id keeps its label — the "impossible edge" audit from Go: case-only sibling symbols (Run / run) collapse to one node id — the unexported half vanishes and its call sites bind to a same-named function in another package #2779
    goes 1 -> 0.

…abs#2779)

Node ids are casefolded (ids.py), so a Go file declaring both `Run` and
`run` produced one id and add_node silently dropped the second
declaration: the unexported half vanished from the graph and its call
sites bound by bare name to a same-named function in another package —
an edge Go's visibility rules forbid.

Detect case-only collisions in a pre-pass over the file's declarations.
The exported member keeps the plain id (cross-package edges and semantic
nodes reference it, so it must stay stable across incremental rebuilds);
the unexported sibling gets a sha1(name) salt; when no unique exported
member exists, every member is salted so the outcome never depends on
declaration order.
@catpotd catpotd closed this Aug 16, 2026
@catpotd
catpotd deleted the fix/go-case-only-symbol-collision branch August 16, 2026 06:37

@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

This pull request modifies the Go extractor (graphify/extractors/go.py) to handle case-only naming collisions between symbols in the same file — for example, an exported Run and an unexported run — which previously produced identical casefolded node IDs. It adds a pre-scan pass (_scan_declarations, _plain_symbol_nid, _receiver_type_of) to detect such collisions and a symbol_nid helper that salts the ID of non-exported members while keeping the exported member's ID stable, applying this to both function and method declarations. The PR also adds a suite of tests in tests/test_go_qualified_resolution.py covering that both case-only siblings are extracted as distinct nodes, that they don't bind to same-named symbols in other packages, that the exported symbol's ID stays stable when a sibling is added, and that incremental/partial rebuilds preserve cross-package edges.

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

Analysis details — impact, health, verification

Impact & health

Graphify review

Impact — 54 functions depend on the 40 functions this change touches.

Health — this change adds coupling hotspots:

  • new: extract_go() — 16 callers, 7 callees
  • new: walk() — 1 callers, 8 callees

Verification — 54 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: 54 function(s) in the blast radius were not formally verified this run

Formal verification

Could not verify: Could not verify extract\_go.

The verifier did not have enough to check extract\_go, 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: parameter `path` is annotated `Path` — outside the synthesizable primitive/collection set

· 1 grounded finding(s) anchored inline below; 1 more finding(s) on lines outside this diff (see the check run).

Comment thread graphify/extractors/go.py
salt = hashlib.sha1(name.encode("utf-8"), usedforsecurity=False).hexdigest()[:6]
return _make_id(plain_nid, salt)

def walk(node) -> None:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Health regression — walk()

fans out to 8 callees (efferent coupling).

Grounded coupling-delta finding (deterministic), not an LLM guess.

@catpotd
catpotd restored the fix/go-case-only-symbol-collision branch August 16, 2026 06:54
@catpotd catpotd reopened this Aug 16, 2026
@safishamsi

Copy link
Copy Markdown
Member

Landed in v0.9.45, just published to PyPI. Cherry-picked onto v8 with your authorship preserved in the commit, so both the change and the credit are in the history. Thanks @catpotd. Closing since it is now released.

@safishamsi safishamsi closed this Aug 16, 2026
safishamsi pushed a commit that referenced this pull request Sep 5, 2026
ids.py:make_id strips leading/trailing underscores from every name
part before normalizing, so _get_connection/get_connection (and more
broadly any name pair differing only by underscore wrapping --
x/_x/__x/__x__ dunder variants all collapse the same way) mint the
SAME node id. add_node's first-wins dedup then silently drops
whichever declaration is walked second: a public method could be
entirely absent from the graph while its private-by-convention
sibling occupied the public name, with no warning and exit 0.

Adds a Python-scoped pre-scan (_python_pre_scan_underscore_collisions)
that groups module-level functions and direct class methods by their
would-be node id, plus a resolver (_python_underscore_salted_nid) that
salts every group member except a unique public (no leading
underscore) one -- mirroring the established convention the Go
extractor already uses for its own case-only collision (#2779/#2780):
when no unique public member exists, every member is salted, so the
outcome never depends on declaration order. The public member's id
stays exactly what it would be with no colliding sibling at all, so
adding/removing a private-by-convention sibling in an incremental
rebuild re-points nothing.

Both hooks are gated on config.ts_module == "tree_sitter_python" and
live inside the shared _extract_generic engine (Python has no
dedicated extractor file, unlike Go/Rust/SQL), following the existing
precedent for per-language pre-scans already there (csharp_interface_names,
swift_protocol_names). Call-site resolution needed no changes: label_to_nid
is rebuilt from the final nodes list after the walk completes, so it
naturally picks up whichever id (salted or not) a method actually got.

9 new tests in tests/test_python_underscore_resolution.py: the exact
repro, id stability across incremental add/remove of a private
sibling, the call edge correctly binding to the salted target, the
module-level (non-method) case, the no-unique-public-member case
(both members salted), a dunder+plain collision, an unaffected
no-collision file, and scope isolation (a collision in one class must
not touch an unrelated same-named-but-non-colliding member in another
class).

Note: tests/ on the current v8 HEAD has 7 pre-existing failures
unrelated to this change (test_cli_export.py, test_cross_repo_shared_types.py,
test_extract_code_only_cli.py, test_merge_graphs_cli.py) -- confirmed via
`git stash` that they fail identically on pristine v8 without this diff.
Full suite otherwise: 5116 passed. ruff and skillgen --check clean.

Fixes #3302.

(cherry picked from commit cb8cceb)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants