Skip to content

Unify node-ID normalization into a single source of truth (#1) - #1378

Closed
danielnguyenfinhub wants to merge 4 commits into
Graphify-Labs:v8from
danielnguyenfinhub:v8
Closed

danielnguyenfinhub wants to merge 4 commits into
Graphify-Labs:v8from
danielnguyenfinhub:v8

Conversation

@danielnguyenfinhub

Copy link
Copy Markdown
Contributor

extract._make_id and build._normalize_id each carried a copy of the same NFKC -> \w -> underscore-collapse -> casefold recipe, kept in sync only by mirrored docstrings. That duplication is the root of the recurring ID-drift bug class (#811, #550, #1033, #1104): the AST extractor, the LLM subagents, and the graph builder must all produce identical node IDs or a single entity splits into disconnected ghost nodes.

Move the recipe into a new graphify/ids.py (normalize_id + make_id). extract and build now both delegate to it, so they can no longer diverge. Behavior is unchanged — both copies were already identical.

Add tests/test_id_normalization_contract.py: parametrized regression cases for the cited issues, plus hypothesis property tests asserting _make_id(s) == _normalize_id(s) and idempotency. The shared-implementation guard fails if a future change re-forks the normalization.

Claude-Session: https://claude.ai/code/session_01UZZai31gG49fxtUf3onjN3

danielnguyenfinhub and others added 4 commits June 18, 2026 14:26
extract._make_id and build._normalize_id each carried a copy of the same
NFKC -> \w -> underscore-collapse -> casefold recipe, kept in sync only by
mirrored docstrings. That duplication is the root of the recurring ID-drift
bug class (Graphify-Labs#811, Graphify-Labs#550, Graphify-Labs#1033, Graphify-Labs#1104): the AST extractor, the LLM subagents,
and the graph builder must all produce identical node IDs or a single entity
splits into disconnected ghost nodes.

Move the recipe into a new graphify/ids.py (normalize_id + make_id). extract
and build now both delegate to it, so they can no longer diverge. Behavior is
unchanged — both copies were already identical.

Add tests/test_id_normalization_contract.py: parametrized regression cases for
the cited issues, plus hypothesis property tests asserting
_make_id(s) == _normalize_id(s) and idempotency. The shared-implementation
guard fails if a future change re-forks the normalization.


Claude-Session: https://claude.ai/code/session_01UZZai31gG49fxtUf3onjN3

Co-authored-by: Claude <[email protected]>
…#2)

The extraction-spec.md shown to semantic subagents documents node-ID
construction with worked examples
(`src/auth/session.py` + `ValidateToken` -> `auth_session_validatetoken`).
Those examples are the LLM's ground truth and must reproduce exactly what the
AST extractor emits, or the two producers generate different IDs for the same
symbol and the graph splits it into disconnected ghost nodes — the same bug
class (Graphify-Labs#811/Graphify-Labs#550/Graphify-Labs#1033/Graphify-Labs#1104) the new graphify/ids.py unification addressed on
the code side.

The prose is hand-maintained, so it can silently drift from the code. This adds
tests/test_extraction_spec_ids.py, which parses every `path` + `entity` -> `id`
example straight out of the 14 shipped specs (the shared skillgen fragment plus
each per-host copy) and asserts extract._make_id(_file_stem(...), ...)
reproduces it. It fails if a spec example is edited to a wrong value or if the
ID functions change so the documented examples no longer hold, and also locks
the spec's cautionary filename-only / full-path anti-examples to the code.

Test-only; no runtime behavior change.


Claude-Session: https://claude.ai/code/session_01UZZai31gG49fxtUf3onjN3

Co-authored-by: Claude <[email protected]>
…pose (#3)

* Guard the LLM-facing node-ID spec against drift from the ID functions

The extraction-spec.md shown to semantic subagents documents node-ID
construction with worked examples
(`src/auth/session.py` + `ValidateToken` -> `auth_session_validatetoken`).
Those examples are the LLM's ground truth and must reproduce exactly what the
AST extractor emits, or the two producers generate different IDs for the same
symbol and the graph splits it into disconnected ghost nodes — the same bug
class (Graphify-Labs#811/Graphify-Labs#550/Graphify-Labs#1033/Graphify-Labs#1104) the new graphify/ids.py unification addressed on
the code side.

The prose is hand-maintained, so it can silently drift from the code. This adds
tests/test_extraction_spec_ids.py, which parses every `path` + `entity` -> `id`
example straight out of the 14 shipped specs (the shared skillgen fragment plus
each per-host copy) and asserts extract._make_id(_file_stem(...), ...)
reproduces it. It fails if a spec example is edited to a wrong value or if the
ID functions change so the documented examples no longer hold, and also locks
the spec's cautionary filename-only / full-path anti-examples to the code.

Test-only; no runtime behavior change.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Claude-Session: https://claude.ai/code/session_01UZZai31gG49fxtUf3onjN3

* fix(merge-graphs): normalise MultiGraph inputs to Graph before nx.compose

When mixing an AST-only extraction (no multigraph header in graph.json,
so networkx defaults to MultiGraph) with a full LLM extraction that
explicitly sets multigraph=false (Graph), nx.compose raises:
  NetworkXError: All graphs must be graphs or multigraphs.

Converts any MultiGraph to a simple Graph with nx.Graph(g) before the
compose loop so merge-graphs works regardless of the source type.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Claude-Session: https://claude.ai/code/session_01UZZai31gG49fxtUf3onjN3

---------

Co-authored-by: Claude <[email protected]>
safishamsi added a commit that referenced this pull request Jun 18, 2026
extract._make_id and build._normalize_id were copy-pasted forks of the same
NFKC/casefold recipe kept in sync by mirrored docstrings -- the root of the
recurring ID-drift ghost-node bug class (#811/#550/#1033/#1104). Move the recipe
to graphify/ids.py and have all four producers delegate to it: extract, build,
and (completing the migration) mcp_ingest and symbol_resolution, whose
"avoid an import cycle" copies are moot now that ids.py is dependency-free. The
contract test asserts all four resolve to the shared recipe, with hypothesis
property tests for make_id == normalize_id and idempotency.

Co-Authored-By: danielnguyenfinhub <[email protected]>
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@safishamsi

Copy link
Copy Markdown
Member

Merged to v8 in 8883991 — thanks @danielnguyenfinhub, excellent refactor. I independently verified the safety claim: a differential test (42 single-string inputs + 11 multi-arg forms + a brute force over all strings ≤ length 4) confirmed the new graphify.ids reproduces both old recipes exactly — zero divergence, no ghost-node regression. (Minor note: the two old recipes weren't byte-identical — extract's had an extra per-part strip('_.') — but they're behaviorally equivalent on the single-string contract, and make_id preserves the multi-arg join.) On merge I completed the migration: mcp_ingest._make_id and symbol_resolution._bash_make_id were two more live copies of the exact recipe (the latter's 'avoid an import cycle' reason is moot now that ids.py is dependency-free), so they now delegate too, and the contract guard asserts all four producers resolve to the shared recipe. Hypothesis property tests run (not skipped). Full suite green (2218 passed).

@safishamsi safishamsi closed this Jun 18, 2026
PerryLin-0420 pushed a commit to PerryLin-0420/graphify-decouple that referenced this pull request Aug 13, 2026
…abs#1378)

extract._make_id and build._normalize_id were copy-pasted forks of the same
NFKC/casefold recipe kept in sync by mirrored docstrings -- the root of the
recurring ID-drift ghost-node bug class (Graphify-Labs#811/Graphify-Labs#550/Graphify-Labs#1033/Graphify-Labs#1104). Move the recipe
to graphify/ids.py and have all four producers delegate to it: extract, build,
and (completing the migration) mcp_ingest and symbol_resolution, whose
"avoid an import cycle" copies are moot now that ids.py is dependency-free. The
contract test asserts all four resolve to the shared recipe, with hypothesis
property tests for make_id == normalize_id and idempotency.

Co-Authored-By: danielnguyenfinhub <[email protected]>
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
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.

2 participants