Skip to content

fix(extract): keep the checkout path out of ObjC category ids - #4642

Closed
madhusudanpatnaik wants to merge 2 commits into
Graphify-Labs:v8from
madhusudanpatnaik:fix/objc-category-portable-ids
Closed

madhusudanpatnaik wants to merge 2 commits into
Graphify-Labs:v8from
madhusudanpatnaik:fix/objc-category-portable-ids

Conversation

@madhusudanpatnaik

Copy link
Copy Markdown
Contributor

What does this PR do?

An Objective-C category's node ids carried the absolute checkout path. A category (@interface Foo (Cat) in Foo+Cat.h) keys its class node and its methods off the base stem Foo, so they fold into Foo.h's class (#1556). extract() turns an id minted from an absolute input path into the repo-relative form only when the id starts with its own file's stem prefix (…/Foo+Cat). These ids start with the base stem's prefix (…/Foo), so they stayed absolute:

/home/me/proj/Lib/NSString+Extra.h   →  NSString node  home_me_proj_lib_nsstring_nsstring
/home/me/proj/Lib/Foo+Cat.h (Foo.h in the corpus)  →  -bar method  home_me_proj_lib_foo_foo_bar

Effects on v8:

  • graph.json carries the checkout path, including usernames, which graph.json edge endpoints still leak absolute-path-derived file ids #502 guards against for every other id.
  • Two checkouts of the same repo produce different graphs, and absolute and relative input disagree.
  • An incremental rebuild in a moved or copied checkout adds a duplicate node for every category class, because the stored ids name the old location.
  • With SPM-style include/ symlinks (SDWebImage), each category node is extracted twice and the leaked copy can't collapse with its symlink twin the way every other symbol does. That leaves a duplicate class node, and on SDWebImage one false reference: the CIImage type in @property CIImage *CIImage bound to the duplicate's -CIImage getter.

The fix (graphify/extract.py, id-remap registration): for a file whose name contains + and whose stem is a category stem (_objc_category_base_stem, the same rule the ObjC extractor uses), also register the base stem's prefix, in its input and resolved forms, after the file's own prefixes. The prefix pass is source_file-gated, so this only touches nodes from that category file. Because the rewrite also lands in sym_remap, the call records and ObjC field tables follow. No cache-schema bump is needed: per-file extraction is unchanged, and the remap runs on cached results as well. A graphify update over a graph v8 built produces the fresh result.

Type of change

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

Verification & Invariants

Invariant (#502): a node id never depends on where the repo is checked out; absolute and relative input give the same graph.

  • Real repos (v8 at 0.9.93 → this branch; each repo extracted from two checkout locations plus once with relative input):

    Repo Ids with the checkout path Two checkouts identical Absolute = relative input Edges compared by label and file
    SDWebImage/SDWebImage 334 → 0 no → yes no → yes identical
    AFNetworking/AFNetworking 51 → 0 no → yes no → yes identical
    onevcat/Kingfisher (Nocilla test deps) 17 → 0 no → yes no → yes identical

    The only change is that exactly the leaked ids are renamed (334 / 51 / 17). With SDWebImage's include/ symlinks in place, the extract() graph keeps the same node and edge counts and the same edges by label. 326 edge keys differ only in which symlink twin the source node is credited to. Through the CLI, SDWebImage's graph has 125 fewer nodes: each is a duplicate pair on v8 (a leaked-id copy beside the clean copy reached through the symlink). The one distinct edge v8 has and this branch doesn't is the false NSImage → -CIImage reference above.

  • Path variations: a checkout path with spaces and non-ASCII characters, a category file at the repo root, a nested directory, and a .mm implementation all give clean ids. NSArray+Many+More.h, which isn't a category stem by the existing rule, is left exactly as on v8.

  • CLI (graphify update, Kingfisher and SDWebImage):

    • Updating over a graph v8 built equals a fresh build.
    • Adding a category file and running update again equals a fresh build.
    • The hook-style incremental rebuild goes from 17–18 differing nodes (Kingfisher) and 334–335 (SDWebImage) on v8 down to one node each. Ids and edges are identical; what remains is listed under limitations.
  • Determinism: identical graph under a different PYTHONHASHSEED, parallel extraction, a warm cache, and a cache filled by v8 first (SDWebImage, AFNetworking, Kingfisher).

  • Performance: the extra registration only runs for a file whose name contains +. Isolated, it costs 0.125 ms per extract() on grpc-go (1,089 files, none with +) and 2.6 ms on SDWebImage (58 category files). Full extract(), cold cache, 3 interleaved rounds, v8 vs this branch: SDWebImage 2.64/2.81/2.68 s vs 2.80/3.06/2.62 s, AFNetworking 1.28/1.38/1.16 s vs 1.30/1.12/1.27 s, Kingfisher 4.59/4.33/4.28 s vs 4.92/4.39/4.42 s, grpc-go 40.78/38.06/39.04 s vs 42.01/39.72/39.93 s; all within run-to-run noise, peak RSS identical.

  • Read the CONTRIBUTING.md guide.

  • Reproduced the issue and identified the invariant.

  • Made the smallest fix necessary.

  • Added a regression test (if bug fix) or isolated boundary test.

  • Kept the PR description synchronized with the final implementation.

  • Documented any limitations / unsupported cases explicitly.

Limitations:

  • Hook path, shared category node: when two categories of the same outside class sit in one directory (NSData+Nocilla.h, NSData+Matcheable.h), they share one node by design (iOS Objective-C/Swift mixed-language: many expected edges missing (self-imports, .h↔.m linkage, cross-language calls, symbol fragmentation) #1556). A fresh build credits it to the first file, while the hook-style rebuild credits it to the re-extracted file. Ids and edges match; only that node's source_file/line differs. On v8 the hook path added a duplicate node there instead.
  • Hook path, cross-directory category: a category added in one directory (Core/UIView+ZZ.h) isn't folded into a same-named class declared in another directory (include/…/UIView+WebCache.h), because the decl/def merge only sees the re-extracted file. v8 behaves the same.

How was this tested?

  • New tests/test_objc_category_portable_ids.py (4 tests): no id or edge endpoint carries the checkout path; a category class and methods get repo-relative ids (lib_nsstring_nsstring, lib_foo_foo_bar beside lib_foo_foo_base) with their method edges; two checkouts give the same graph; relative input gives the same ids as absolute input. All 4 fail on v8 (0.9.93) and pass here. The existing ObjC tests (88 with the new ones) pass.
uv run pytest tests/test_objc_category_portable_ids.py tests/test_objc*.py tests/test_cpp_objc_cross_file_calls.py -q   # 88 passed
# Full suite: per Python, `uv sync --all-extras --frozen`, then `uv run pytest tests/ -q --tb=short`.
#   3.10: 8467 | 3.12: 8466 | 3.13: 8466 | 3.14: 8466 passed; only the 7 DNS-dependent test_security tests fail (no network here; same on v8)
#   merged onto v8 with #4578–#4581, #4620 and my variadic, Swift, perf and Dart branches
#   (only the one-line `_AST_CACHE_SCHEMA` conflicts) — 3.10: 8541 | 3.13: 8540 passed
#   the watch tests (test_language_override_lifecycle.py, test_pr10_watch_overrides.py) can fail
#   intermittently on v8 as well: a test-harness race, fixed by #4620; a re-run clears it
uv run python -m tools.skillgen --check / --audit-coverage / --schema-singleton / --monolith-roundtrip / --always-on-roundtrip   # all OK
uv run python -m tools.skillgen --bless   # no changes
uv run ruff check .                       # v0.15.14, as pinned in .pre-commit-config.yaml
uv run pyright                            # 643 errors on v8 (0.9.93) and on this branch; none new
uv run bandit -r graphify -ll             # 12 findings, identical to v8
GRAPHIFY_TEST_CLI=.venv/bin/graphify bun test tests/pi.test.ts   # 20/20
uv run graphify --help && uv run graphify install   # OK, in a throwaway HOME

Graphify-specific checklist

  • I updated generated skill artifacts (uv run python -m tools.skillgen --bless) when changing their source fragments. (No source fragments touched; --check passes.)
  • 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.

An ObjC category (`@interface Foo (Cat)` in `Foo+Cat.h`) keys its class
node and methods off the base stem `Foo`, so they fold into `Foo.h`'s
class (Graphify-Labs#1556). extract() turns an id minted from an absolute input path
into the repo-relative form only when it starts with its own file's stem
prefix (`.../Foo+Cat`), so these ids kept the absolute form. A category
on a class outside the corpus (`NSString (Extra)`) got a node id with
the checkout path in it, and so did the methods of a category on an
in-corpus class. graph.json then carried the path (Graphify-Labs#502), two checkouts
of one repo gave different graphs, and an incremental rebuild in a moved
or copied checkout added duplicate nodes.

The remap now also registers a category file's base-stem prefix, after
the file's own prefixes. On SDWebImage, AFNetworking and Kingfisher,
334 / 51 / 17 ids carried the checkout path; now 0, two checkouts give
identical graphs, and absolute and relative input agree. Every edge,
compared by label and file, is unchanged. Through the CLI, SDWebImage's
graph drops 125 duplicate category nodes (a leaked copy beside the clean
copy reached through its include/ symlink) and one false reference.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions

Copy link
Copy Markdown

Thanks for the pull request, @madhusudanpatnaik. 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.

@github-actions github-actions Bot added the contributor Pull request from a returning community contributor label Oct 21, 2026

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

Worth a look — the grounded gate found no coupling regressions or blocking issues, but 1 advisory finding(s) below merit a look before merge.

Not checked: tests were not run; formal verification proved 0 of 1 changed function(s) (1 sampled, not proven).

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


Graphify review — findings

Fixes ObjC category node ids leaking the checkout path into the graph. Ids for a category's base class and its methods (e.g. NSString from NSString+Extra.h) now get rewritten from the absolute-path form to the repo-relative form, so the same repo checked out in two places yields the same graph. extract registers the base-stem prefix alongside the file's own prefixes, and the rewrite hinges on how that base stem is derived from a path that can itself contain +. New tests cover checkout-path leakage, category ids on in-corpus and external classes, two checkouts, and relative versus absolute input paths.

Worth a look

  • Base-stem prefix may be mis-derived when a directory in the path contains '+' — graphify/extract.py:9819 · Escalate · medium
    • NOT verified (no proof, no reproducing execution) — needs human review
Analysis details — impact, health, verification

Impact & health

Graphify review

Impact — 3159 functions depend on the 333 functions this change touches.

Health — this change adds coupling hotspots:

  • worse: extract() — 989 callers, 60 callees

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

Test selection

Test selection

203 of 437 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_delegate_declarations.py — impact
  • tests/test_csharp_enum_members.py — impact
  • tests/test_csharp_field_generic_args.py — impact
  • tests/test_csharp_foreach_loop_variable.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
  • 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
  • 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_ext_tagged_id_clash.py — impact
  • tests/test_extract.py — impact
  • tests/test_extract_cache_location.py — impact
  • … and 153 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

No difference found (not proven): No behavior difference found in extract (not a proof).

The verifier ran both versions of extract on many inputs and saw identical behavior every time. Strong evidence the change is safe, but evidence, not a proof.

Guarantee: Empirical: differential testing (both versions run on many generated inputs). A divergence on an untested input remains possible, so this is 'no counterexample found', not 'proven equivalent'.

Note: An input the sampler did not try could still differ.

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

No code change. test (3.13) failed in
test_watch_root_config_lifecycle_rebuilds_and_refreshes_event_mapping[create],
the test-harness race fixed by Graphify-Labs#4620, which fails the same way on v8.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@madhusudanpatnaik

Copy link
Copy Markdown
Contributor Author

I pushed an empty commit (f7cbc34) to re-run CI. The earlier test (3.13) failure was the flaky watch test test_watch_root_config_lifecycle_rebuilds_and_refreshes_event_mapping[create], the test-harness race that #4620 fixes, not this change. All checks pass now.

@safishamsi

Copy link
Copy Markdown
Member

Landed in 0.9.94, cherry-picked to preserve your authorship. Objective-c category node ids stay repo-relative instead of embedding the checkout path. Thanks @madhusudanpatnaik. Live on PyPI: https://pypi.org/project/graphifyy/0.9.94/

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.

2 participants