Skip to content

fix(ids): casefold before the non-word filter so normalize_id is idempotent - #2616

Closed
rajarshidattapy wants to merge 2 commits into
Graphify-Labs:v8from
rajarshidattapy:fix/normalize-id-idempotency
Closed

rajarshidattapy wants to merge 2 commits into
Graphify-Labs:v8from
rajarshidattapy:fix/normalize-id-idempotency

Conversation

@rajarshidattapy

Copy link
Copy Markdown
Contributor

Fixes #2614.

Problem

normalize_id() casefolded after the [^\w]+ filter:

s = unicodedata.normalize("NFKC", s)
s = re.sub(r"[^\w]+", "_", s, flags=re.UNICODE)
s = re.sub(r"_+", "_", s)
return s.strip("_").casefold()      # <-- last

Casefolding can expand a character into a base letter plus a combining mark:
"İ".casefold() is "i" + U+0307. With the filter already spent, that mark
survived into the id — so normalize_id broke both of its contracts:

  • Not idempotent, though the docstring promises it: İstanbul → i̇stanbul → i_stanbul.
  • Emitted non-\w characters, though cache.py documents that "normalize_id drops every
    non-word character".

Turkish identifiers (İslemYap, İçerik) hit this on every run, and U+0307 is invisible in
most terminals, so the resulting ids look correct.

The concrete damage is the ghost-node class ids.py was created to prevent. build.py
re-normalizes ids that make_id() already produced, and at build.py:1057 compares
_normalize_id(nid).startswith(new_stem) where new_stem comes from make_id() and is not
re-normalized. For a Turkish symbol the two sides disagreed (i_slemyap vs i̇slemyap), so
_semantic_id_remap's re-key silently no-opped.

Fix

Move casefold ahead of the filter, and NFKC again to recompose what it expanded:

s = unicodedata.normalize("NFKC", s)
s = unicodedata.normalize("NFKC", s.casefold())
s = re.sub(r"[^\w]+", "_", s, flags=re.UNICODE)
s = re.sub(r"_+", "_", s)
return s.strip("_")

Blast radius (measured, not assumed)

Scanned all 1.1M codepoints, and diffed ids across the 369-file graphify tree:

ASCII codepoints that change 0 — no churn for ordinary code
Codepoints where old emitted non-\w 27 → 0
Codepoints where old was non-idempotent 1 (U+0130) → 0
Codepoints whose id changes at all 29
Real tokens in the graphify tree whose id changes 0 (the 14 hits are the Turkish/Greek fixtures added by this PR)

The 29 affected codepoints are U+0130, Greek ypogegrammeni, and precomposed
Latin/Greek letters whose casefold expands. For those, the new output is the composed
NFKC form — which is what the module docstring already specified.

No migration needed — and I verified rather than assumed

The issue expected this to need #1504/#1917-style legacy-form handling. It does not, because
the new output is exactly the old recipe's fixed point: normalize_id(old_id) == new_id.
build.py:1008 keys its reconciliation map on _normalize_id(nid), so old-form and new-form
ids already collapse to the same key.

Tested end to end rather than argued: built a Turkish corpus with the unfixed code
(islem_i̇slemyap), applied the fix, added a symbol, ran graphify update:

islem
islem_caller
islem_i_kinci      <- new symbol
islem_i_slemyap    <- re-keyed from islem_i̇slemyap
main
main_main
dangling edges: 0
GHOST PAIR PRESENT: False

So no legacy-form code was added.

Tests

A property test for exactly this already existed and did not catch it.
test_property_normalize_id_idempotent with @given(st.text()) has been in the suite since
the contract file was written — a uniform draw essentially never produces U+0130 out of ~1.1M
codepoints. Fixing only the implementation would leave that gap open, so this PR also
strengthens the guard:

  • deterministic Turkish/case-expanding entries in CONTRACT_CASES
  • a new CASE_EXPANDING_CHARS set covering each distinct failure shape (expansion,
    recomposition, combining marks, ẞ → ss)
  • the missing \w-only postcondition, asserted directly
  • hypothesis strategies biased toward case-expanding letters and combining marks, so the
    class is covered by construction rather than by luck

New tests produce 18 failures on the unfixed code, 0 with the fix.

One assertion hypothesis rejected, worth knowing

I first asserted normalize_id(s.upper()) == normalize_id(s.lower()). Hypothesis
immediately found ı (U+0131 dotless i): 'ı'.upper() is 'I', which casefolds to 'i'.
str.upper() is locale-independent and lossy, so that property is false regardless of this
fix. Replaced with caseless equivalence via casefold, which is the invariant the graph
actually relies on. Noted in the test docstring so nobody "fixes" it back.

Verification

  • Issue repro: all four cases now idempotent
  • tests/test_id_normalization_contract.py: 71 passed
  • Full suite: 41 failures, identical to the pre-existing baseline on this platform, zero
    new, +37 tests
graphify/ids.py                         |  30 ++++++--
tests/test_id_normalization_contract.py | 117 +++++++++++++++++++++++++++++

@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. 1 change(s) tested, no difference found (not proven).


Graphify review — findings

This pull request reverts the changelog entries for version 0.9.39, moving 0.9.38 back to "unreleased" status. It removes the fuzzy dedup content-word/stopword comparison helpers (_content_token_swap, _same_word_variant, _STOPWORDS) and their call sites in deduplicate_entities and _llm_tiebreak, along with the file-source-based dedupe logic in the JS dynamic import rescue path. The changes appear to roll back several features/fixes that had been slated for the 0.9.39 release, touching dedup, extract, and watch modules plus their corresponding tests.

No blocking issues surfaced.

Analysis details — impact, health, verification

Impact & health

Graphify review

Impact — 3268 functions depend on the 1518 functions this change touches.

Health — grade A; 10 existing hotspot(s) in the area this change touches (pre-existing, not introduced here):

  • extract() — 400 callers, 39 callees (high)
  • _rebuild_code() — 93 callers, 51 callees (high)
  • build_from_json() — 151 callers, 17 callees (high)
  • deduplicate_entities() — 53 callers, 19 callees (high)
  • build_merge() — 41 callers, 14 callees (high)
  • save_semantic_cache() — 50 callers, 9 callees (high)
  • _extract_generic() — 18 callers, 22 callees (high)
  • extract_bash() — 34 callers, 10 callees (high)
  • …and 2 more

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

Formal verification

Could not verify: Could not verify \_llm\_tiebreak.

The verifier did not have enough to check \_llm\_tiebreak, 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 105 sampled inputs raised on both versions — the function never executed, so 'no divergence' would be vacuous (mostly TypeError — names the real obstacle, not a sampling gap)

Could not verify: Could not verify deduplicate\_entities.

The verifier did not have enough to check deduplicate\_entities, 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 7 sampled inputs raised on both versions — the function never executed, so 'no divergence' would be vacuous (mostly TypeError — names the real obstacle, not a sampling gap)

Could not verify: Could not verify \_rescue\_js\_dynamic\_imports.

The verifier did not have enough to check \_rescue\_js\_dynamic\_imports, 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

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

The verifier did not have enough to check \_resolve\_objc\_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: non-vacuity: domain too small (only 1 distinct inputs exercised, need 3) — 'no divergence' would be near-vacuous

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: parameter `path` is annotated `Path` — outside the synthesizable primitive/collection set

Could not verify: Could not verify extract\_objc.

The verifier did not have enough to check extract\_objc, 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

Could not verify: Could not verify \_merge\_decl\_def\_classes.

The verifier did not have enough to check \_merge\_decl\_def\_classes, 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: the input domain has 49 values but only 7 distinct were tested — a small finite domain must be EXHAUSTED, not sampled (an untested input could invert the result)

No difference found (not proven): No behavior difference found in normalize\_id (not a proof).

The verifier ran both versions of normalize\_id 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.

Could not verify: Could not verify watch.

The verifier did not have enough to check watch, 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 `watch_path` is annotated `Path` — outside the synthesizable primitive/collection set

@safishamsi

Copy link
Copy Markdown
Member

Shipped in v0.9.40 (graphifyy==0.9.40 on PyPI). Casefold-before-filter for #2614. Credited in the release notes. Thanks @rajarshidattapy!

@safishamsi safishamsi closed this Aug 11, 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.

ids.normalize_id() is not idempotent for U+0130 (Turkish İ), breaking its documented invariant and emitting non-\w characters into node IDs

2 participants