Skip to content

fix(csharp): keep names that differ only by _ or case apart, in ids and in member-call keys (#4415) - #4416

Closed
JensD-git wants to merge 1 commit into
Graphify-Labs:v8from
JensD-git:fix-csharp-underscore-collision
Closed

JensD-git wants to merge 1 commit into
Graphify-Labs:v8from
JensD-git:fix-csharp-underscore-collision

Conversation

@JensD-git

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes #4415.

Two C# defects with one root: ids.py:make_id strips leading/trailing underscores and casefolds, while C# is
case-sensitive and spells private members _Name.

1. Declarations in one file collapsed (graphify/extractors/engine.py). Action() / _Action(), Size /
_Size, Load() / load() in one type, and Pair / _Pair in one namespace, minted one id; add_node kept the
first declaration and dropped the second, and the dropped method's calls were credited to the survivor. This PR
applies the rule of #3302 (Python, v0.9.55) to C#:

  • A new pre-scan, _csharp_pre_scan_name_collisions, groups the declared names of a file that would share an id:
    types (class, struct, interface, record, enum), and per type its methods, properties and enum members. Types are
    keyed exactly as the walk mints them (namespace, arity, [Bug]: C#: a generic and a non-generic type of the same name share one node #4249); members under the type's final id, so the members
    of a salted type are grouped under the salted id. It enters only namespace and type bodies and #if wrappers,
    so its recursion depth is the nesting depth of declarations.
  • The existing _python_underscore_salted_nid is renamed to _underscore_salted_nid and used for both languages,
    unchanged: when exactly one name of a group has no leading underscore, it keeps the plain id; every other name
    gets sha1(name)[:6] appended. A type, method, property or enum member without a colliding sibling keeps its
    id exactly, and the result does not depend on declaration order.
  • In-file type references go through the same rule (ensure_named_node and the base_list lookup), so
    class Z : _Pair in the same file reaches _Pair, not Pair.
  • The arity fold of [Bug]: C#: a generic and a non-generic type of the same name share one node #4249 moves into a helper, _csharp_type_name_for_id, shared by the walk and the pre-scan.

2. The member-call resolver ignored _ and case (graphify/extract.py). _resolve_csharp_member_calls
keyed names with [^a-zA-Z0-9] removed and lowercased, so this.Put() bound to _Put() on a nearer base class,
and type names such as Token / TOKEN or Spec / _Spec were one key. The key now removes only
non-word characters (the label decoration of .Put()), keeping _ and case.

Two existing tests asserted the old first-wins result for a case-only pair (enum Odd { Value, value }, properties
Count / count); they now assert one node and one edge per member.

Type of change

  • Bug fix
  • New feature
  • Documentation
  • Tests or CI
  • Refactor
  • Security fix

Verification & Invariants

Invariant: in a C# file, two declarations with different names never share a node id because of _ or case;
a declaration with no such sibling keeps the id it had before. A C# member call binds only to a method whose name
equals the called name.

Persisted state: node ids change only for C# declarations that have a colliding sibling in the same file
(salted, as for Python since #3302). No node metadata or cache format change. I did not bump _AST_CACHE_SCHEMA:
the AST cache is namespaced by package version, so a release re-extracts anyway; say if you want the bump in this PR.

Determinism: the salt is a function of the declared name; the group a name falls in does not depend on the
order of declarations.

Effect on a real corpus. A proprietary C# corpus of eight graphs, each built twice from the same source tree:
graphifyy 0.9.89 from PyPI, and the same files with this change. Compared is graphify's own output (graphify extract), no post-processing. Unit, unless stated otherwise: a pair of (source file, label) of both ends of an edge,
deduplicated, per relation.

  • C# nodes: none lost in any graph; 224 new (189 methods, 33 properties, 2 types), in all eight graphs. 197 differ
    from their former survivor by an underscore, 27 only by case.
  • calls edges whose target name is not called on the edge's source line, while a name that differs only by a
    leading or trailing underscore is (source line read for every edge): 319 -> 0, in four of the eight graphs.
  • Lost pairs, all classified: calls 1029, references 115, method 2, contains 1.
    • 784 (669 calls, 115 references): the same edge, now starting at the restored sibling, i.e. the edge comes
      from the body of _X(), which 0.9.89 had merged into X(). 5 sampled by hand: in each, the call line is
      below the declaration of _X().
    • 340 (337 calls, 2 method, 1 contains): the same edge, now ending at the sibling with the same name up to
      _ / case (the method actually called, or the restored declaration).
    • 23 calls, each checked against its source line. 22 were wrong in 0.9.89 and are gone: the target's name
      differs from the called name only by underscores (e.g. x.Run() bound to _Run(), this.Re_Do() bound to
      __ReDo()), or a bare call such as _Hide() bound to another class's method of that name because the
      caller's own _Hide() had been dropped, or the receiver was typed through a collapsed type key (a parameter
      of type TOKEN typed as the class Token; a field of type Spec left untyped because Spec and _Spec were
      one key, after which the field's own name matched an unrelated type). 1 is a call between two methods that
      call each other: with _X() restored they are one node pair, and graph.json keeps one edge per pair.
  • New pairs: calls +1344, references +282, method +189, defines +35, dispatches_to +2, contains +1.
  • Not from this change: 8 .sql file nodes appear only in the branch build (my branch environment has the optional
    SQL grammar from uv sync --all-extras, the PyPI install does not), and one HTML file whose output also varies
    between two builds of the same version.

Limitations

  • Fields are still not nodes (C#: properties are not nodes, so nothing in the graph can point at a member #3006); this PR does not change that.
  • The Swift, Python, TypeScript, C++ and Objective-C member-call resolvers define the same _key(). I changed only
    the C# one.
  • _csharp_receiver_type_name accepts only type names that start with an upper-case letter, so a receiver of type
    _Pair is never typed. Not changed here.
  • Nested type ids omit the enclosing type (as before); the pre-scan follows that, so Host.Item and _Item nested
    in another type of the same file form one group and are salted apart. Two nested types with the SAME name under
    different outers still share an id, as before. PR fix(extract): qualify nested class node IDs with parent class scope #4287 (open, in no release) prefixes nested type ids with the
    enclosing type; if it lands, the pre-scan's key for nested types has to follow (_visit passes the namespace
    only). If the two keys ever diverge, a nested type's collision is only missed (no salt), never salted wrongly.
  • PR Fix: same-file node-id collisions silently drop definitions (#3302) #3322 (open, in no release) adds a generic census for same-file collisions in the shared engine. If it lands,
    the C# pre-scan here overlaps with it for methods and types; it does not cover C# properties, enum members or the
    resolver key.

How was this tested?

$ uv run pytest tests/test_csharp_underscore_collisions.py tests/test_csharp_enum_members.py tests/test_csharp_member_nodes.py tests/test_python_underscore_resolution.py -q
  47 passed

$ uv run pytest tests/test_csharp_underscore_collisions.py tests/test_csharp_enum_members.py tests/test_csharp_member_nodes.py -q   # with graphify/ from v8
  18 failed, 20 passed  (16 of the 17 new tests, and the 2 changed ones; the overload test passes on v8 by design)

$ uv run pytest tests                                      # this branch and v8 (e8eb76e), same machine
  this branch: 7 failed, 7860 passed, 73 skipped
  v8:          7 failed, 7844 passed, 73 skipped
  The same 7 fail on both (Windows-specific):
    test_language_override_lifecycle.py::test_watch_relative_roots_receive_config_events_without_rebasing_source_paths[src]
    test_non_regular_files.py::test_char_device_is_rejected
    test_resolve_memoization.py::test_absolute_path_lookup_skips_getcwd
    test_skillgen_input_path_injection.py::test_step1_still_resolves_a_legitimate_path (3 parameters)
    test_source_key_memoization.py::test_absolute_path_lookup_skips_getcwd

$ uv run --isolated --no-project --python 3.10 --with-editable . --with pytest python -m pytest tests/test_csharp_underscore_collisions.py tests/test_csharp_enum_members.py tests/test_csharp_member_nodes.py tests/test_python_underscore_resolution.py -q
  47 passed

$ uv run ruff check .
  All checks passed!

$ uv run pyright
  964 errors on v8 and on this branch; no new findings

$ git diff --check
  clean

The repro from #4415, run with this branch:

Host   1 node(s) [['.Action()', '.Load()', '._Action()', '.load()', 'Size', '_Size']]
Pair   1 node(s) [['.Go()']]
_Pair  1 node(s) [['.Stop()']]
calls: Host._Action() -> Host.Action()
calls: Record.Save() -> BaseStore.Put()

Graphify-specific checklist

  • I updated generated skill artifacts (uv run python -m tools.skillgen --bless) when changing their source fragments. (No skill fragments changed.)
  • I confirmed that AST/structural extraction remains deterministic (no ambient state dependencies like ENV variables).
  • I reviewed changes for security implications (no unsafe interpolation into shell/Python).
  • I confirmed no API keys or local-only graph data are included.
  • (If applicable) I disclosed AI authorship in my commit messages.

Generated with Claude Code

… and in member-call keys (Graphify-Labs#4415)

make_id strips leading/trailing underscores and casefolds, while C# is
case-sensitive and spells private members `_Name`. In one file,
`Action()` / `_Action()`, `Size` / `_Size`, `Load()` / `load()` and the
types `Pair` / `_Pair` minted one id; add_node kept the first declaration
and dropped the second, and the dropped method's calls were credited to
the survivor.

Apply the rule of Graphify-Labs#3302 to C#: a pre-scan groups the declared names of a
file (types; per type its methods, properties and enum members) that
would share an id, and _underscore_salted_nid (renamed from the Python
helper, unchanged) keeps the plain id on the one name without a leading
underscore and salts the rest. In-file type references and base lists go
through the same rule. A name without a colliding sibling keeps its id.

The C# member-call resolver keyed names by lowercased alphanumerics, so
`this.Put()` bound to `_Put()` on a nearer base, and `Token` / `TOKEN`
were one type key. The key now keeps `_` and case.

Fixes Graphify-Labs#4415.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@JensD-git
JensD-git requested a review from safishamsi as a code owner October 13, 2026 13:14
@github-actions github-actions Bot added the contributor Pull request from a returning community contributor label Oct 13, 2026
@github-actions

Copy link
Copy Markdown

Thanks for the pull request, @JensD-git. A maintainer will review it soon.

Want to talk it through while it is in review? Come join us on our Discord server. For longer-form discussion there is also GitHub Discussions.

A couple of things that speed up review: make sure the test suite passes on Python 3.10 and 3.13, and that the change keeps extraction deterministic.

@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.

No issues found by static checks — no coupling regressions or blocking issues in the code graph. That is not the same as safe to merge: see what was not checked.

Not checked: tests were not run; formal verification proved 0 of 3 changed function(s) (3 not verified).

Formal verification. PR-changed functions: 0/3 verified (0 proven, 0 may-equivalent, 0 distinguished) · 3 not verified (2 vacuous, 1 unsupported).

Not verified on this run: \_resolve\_csharp\_member\_calls (vacuous: never exercised), \_csharp\_extra\_walk (vacuous: never exercised), \_extract\_generic (unsupported).


Graphify review — findings

Fixes C# declarations whose names collapse to one node id (Put/_Put, Load/load, Pair/_Pair). A new _csharp_pre_scan_name_collisions pass finds these groups per file, and the shared _underscore_salted_nid (renamed from the Python-only helper) now salts types, methods, properties, enum members and generic base references apart instead of dropping the second declaration. Member-call resolution in _resolve_csharp_member_calls now keys on case and _, so this.Put() no longer binds to _Put.

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

Analysis details — impact, health, verification

Impact & health

Graphify review

Impact — 3370 functions depend on the 701 functions this change touches.

Health — this change adds coupling hotspots:

  • worse: extract() — 947 callers, 57 callees
  • worse: _extract_generic() — 20 callers, 41 callees
  • worse: walk() — 1 callers, 73 callees
  • worse: _csharp_extra_walk() — 2 callers, 6 callees

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

Test selection

Test selection

190 of 413 test file(s) selected (46%) via static blast radius.

  • tests/test_affected_overrides.py — impact
  • tests/test_astro_extraction.py — impact
  • tests/test_astro_import_ids.py — impact
  • tests/test_blade_extractor.py — impact
  • tests/test_build.py — impact
  • tests/test_builtin_global_type_refs.py — impact
  • tests/test_c_include_resolution.py — impact
  • tests/test_cache.py — impact
  • tests/test_case_sensitive_resolution.py — impact
  • tests/test_cjs_module_extension.py — impact
  • tests/test_cobol_extractor.py — impact
  • tests/test_cpp_method_declarations.py — impact
  • tests/test_cpp_nested_and_cli.py — impact
  • tests/test_cpp_objc_cross_file_calls.py — impact
  • tests/test_cpp_template_type_params.py — impact
  • tests/test_cross_extension_reexport_self_cycle.py — impact
  • tests/test_cross_language_call_resolution.py — impact
  • tests/test_cross_repo_external_call_guards.py — impact
  • tests/test_cross_repo_member_calls.py — impact
  • tests/test_csharp_call_site_generic_args.py — impact
  • tests/test_csharp_enum_members.py — impact, changed-test
  • tests/test_csharp_field_generic_args.py — impact
  • tests/test_csharp_generic_callsites.py — impact
  • tests/test_csharp_interface_dispatch.py — impact
  • tests/test_csharp_member_calls.py — impact
  • tests/test_csharp_member_chain_calls.py — impact
  • tests/test_csharp_member_nodes.py — impact, changed-test
  • tests/test_csharp_object_creation.py — impact
  • tests/test_csharp_partial_classes.py — impact
  • tests/test_csharp_tuple_type_refs.py — impact
  • tests/test_csharp_type_resolution.py — impact
  • tests/test_csharp_underscore_collisions.py — impact, changed-test
  • tests/test_csharp_var_call_return_type.py — impact
  • tests/test_csharp_verbatim_interpolation.py — impact
  • tests/test_definition_file_portability.py — impact
  • tests/test_detect.py — impact
  • tests/test_dotnet.py — impact
  • tests/test_duplicate_annotation_edges.py — impact
  • tests/test_ejs_extractor.py — impact
  • tests/test_elixir_defdelegate.py — impact
  • tests/test_elixir_import_resolution.py — impact
  • tests/test_elixir_keyword_def_calls.py — impact
  • tests/test_elixir_qualified_calls.py — impact
  • tests/test_elixir_unqualified_call_scope.py — impact
  • tests/test_erlang_extractor.py — impact
  • tests/test_extract.py — impact
  • tests/test_extract_cache_location.py — impact
  • tests/test_extract_path_memo.py — impact
  • tests/test_extract_php_closures.py — impact
  • tests/test_file_label_disambiguation.py — impact
  • … and 140 more

Selection is safe under the controlled-regression assumption; always-run tests + a periodic full run are the backstops. Advisory — it never changes the check verdict.

Formal verification

Could not verify: Could not verify \_resolve\_csharp\_member\_calls.

The verifier did not have enough to check \_resolve\_csharp\_member\_calls, 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: no capturable inputs from the test suite; property tier: non-vacuity: domain too small (only 1 distinct inputs exercised, need 3) — 'no divergence' would be near-vacuous

Could not verify: Could not verify \_csharp\_extra\_walk.

The verifier did not have enough to check \_csharp\_extra\_walk, 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: no capturable inputs from the test suite; property tier: not verifiable: all 264 sampled inputs raised on both versions — the function never executed, so 'no divergence' would be vacuous (mostly AttributeError — names the real obstacle, not a sampling gap)

Could not verify: Could not verify \_extract\_generic.

The verifier did not have enough to check \_extract\_generic, 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: no capturable inputs from the test suite; property tier: parameter `config` is annotated `LanguageConfig` — outside the synthesizable primitive/collection set

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

@@ -3999,7 +4101,8 @@ def _csharp_namespace_name(node, source: bytes) -> str:
def _csharp_extra_walk(node, source: bytes, file_nid: str, stem: str, str_path: str,

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 — _csharp_extra_walk()

fans out to 6 callees (efferent coupling).

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

safishamsi added a commit that referenced this pull request Oct 13, 2026
C# nameless / `new X()` / `foreach` receiver resolution (#4422, #4428,
#4423) and `_`/case-distinct node ids (#4416) change AST extraction
output, so warm caches must re-extract.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@safishamsi

Copy link
Copy Markdown
Member

Shipped in v0.9.90, just released to PyPI as graphifyy 0.9.90. Landed via an authorship-preserving cherry-pick (your commit keeps your name and counts on your contributor graph), which is why GitHub marks this closed rather than merged. Thanks @JensD-git!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

contributor Pull request from a returning community contributor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: C#: names that differ only by a leading underscore or by case collapse (Action() / _Action(), Pair / _Pair), and this.Put() binds to _Put()

2 participants