Repository navigation
Description
graphify version: 0.9.4 (pinned local fork tag 0.9.4+colfix1, branch v8)
Root cause
_semantic_id_remap() (graphify/build.py) re-derives a non-AST node's id from its source_file, matching against _old_file_stems() (legacy pre-#1504 id forms) to migrate old short-form ids to the current full-path-stem canonical form.
The function is documented and expected to be idempotent (safe to run on an already-migrated graph), but it is not: for any source_file where the immediate parent directory shares a name with the file's own stem (e.g. cache/cache.py, or memory/memory.db), the freshly computed canonical stem is a compound of the two identical segments (cache_cache, memory_memory). One of the legacy forms checked by _old_file_stems() is the bare file stem alone (cache, memory) — which is a real string prefix of that compound canonical stem.
On the first migration pass this is harmless: the old short id (e.g. cache_get) correctly migrates to the new canonical id (cache_cache_get). But on a second pass over the now-migrated id, the same bare-stem legacy form (cache) is checked again, cache_cache_get.startswith(cache_) is still true (because the canonical stem itself happens to start with that legacy form), so the function treats the already-canonical id as if it still needed migrating and glues on another cache_ prefix — producing cache_cache_cache_get. Every subsequent pass repeats this, growing the prefix without bound.
Concretely reproduced with a ~-prefixed source_file (~/memory/memory.db): _norm_source_file()'s absolute-path guard uses os.path.isabs(), which returns False for a ~-prefixed path (tilde expansion is a shell/os.path.expanduser concern, not something isabs performs), so the path is treated as project-relative rather than being skipped as unresolvable. The resulting stem (memory + memory → memory_memory) then collides with the bare-stem legacy form (memory) exactly as described above. This is the trigger that surfaced the bug in production, but tracing the code shows the underlying defect is general: it reproduces identically for a plain in-repo file with no ~ involved at all, wherever a directory and its file share a name.
In production this was masked, not fixed: a downstream compensating control (a composition-guard that restores from backup on any unexplained node-id disappearance) reset the corrupted ids every scheduled run, so the growing-prefix symptom never accumulated past one cycle's worth. That is a compensating control on our side, not a fix in graphify itself.
Reproduction
from graphify.build import _semantic_id_remap
# Same-named in-repo directory/file pair — no ~ involved
nodes = [{"id": "cache_get", "source_file": "cache/cache.py", "type": "code"}]
r1 = _semantic_id_remap(nodes, ".")
print(r1) # {'cache_get': 'cache_cache_get'} -- correct first-pass migration
migrated_id = r1["cache_get"]
nodes2 = [{"id": migrated_id, "source_file": "cache/cache.py", "type": "code"}]
r2 = _semantic_id_remap(nodes2, ".")
print(r2) # {'cache_cache_get': 'cache_cache_cache_get'} -- BUG: re-migrates an
# already-canonical id, growing the prefix. Should be {} (stable).The ~-path variant that surfaced this in production:
nodes = [{"id": "memory_db", "source_file": "~/memory/memory.db", "type": "concept"}]
r1 = _semantic_id_remap(nodes, ".")
print(r1) # {'memory_db': 'memory_memory_db'}
migrated_id = r1["memory_db"]
nodes2 = [{"id": migrated_id, "source_file": "~/memory/memory.db", "type": "concept"}]
r2 = _semantic_id_remap(nodes2, ".")
print(r2) # {'memory_memory_db': 'memory_memory_memory_db'} -- BUGFix applied (in our fork, 0.9.4+colfix1 branch v8)
Added an "already canonical" short-circuit at the top of the per-node loop in _semantic_id_remap(), mirroring the guard the sibling function graph_has_legacy_ids() already carries a few lines below it (norm == new_stem or norm.startswith(new_stem + "_")) — that function was already protected against this exact class of false-positive; _semantic_id_remap() was missing the equivalent guard:
norm_nid = _normalize_id(nid)
if norm_nid == new_stem or norm_nid.startswith(new_stem + "_"):
continue # already canonical
new_id: str | None = None
for old_stem in _old_file_stems(rel):
...This closes the general re-matching class (verified idempotent for both the same-named-directory case and the ~-path trigger case) without touching the first-pass migration behaviour, _norm_source_file(), or any other caller.
A narrower fix scoped only to the ~-path trigger (expanding ~ before the os.path.isabs() check in _norm_source_file(), and skipping remap if the path still doesn't resolve under root) was considered and rejected as insufficient on its own: it would have left the same-named-directory case — which needs no ~ at all — live and unpatched. The chosen fix addresses both.
Two regression tests added to tests/test_build.py:
test_semantic_rekey_second_pass_is_idempotent_on_tilde_pathtest_semantic_rekey_second_pass_is_idempotent_on_same_named_dir
Full test suite run clean apart from four pre-existing, unrelated failures (environment GEMINI_API_KEY leakage into backend-detection tests, and an openai-client max_retries default assertion) — none touch build.py, ids.py, or _semantic_id_remap/graph_has_legacy_ids, and are reproducible on the pre-fix state too.
Cross-reference
Related prior migration work: #1504 (silent cross-chunk ID collision), #1509 (stem-rule inconsistency between llm.py and the skill's extraction-spec.md). This is a distinct mechanism from both — a non-idempotent legacy-id migration re-match, not a chunking collision or a stem-rule divergence — but touches the same id-canonicalization area.
Happy to open a PR with the fix + tests above against this repo if useful.
Activity
hconcepcion-work commentedon Aug 12, 2026
Independent reproduction on 0.9.6, plus confirmation that the trigger class is broader than same-named dir/file pairs.
Trigger variant: the bare file stem only needs to be a string prefix of the canonical compound stem — no directory has to share the file's name. Repro corpus file:
alice-one/public/app/alice.jpg(vision-extracted image). Canonical stem:alice_one_public_app_alice. Bare-stem legacy form:alice. Every canonical id for this file (alice_one_public_app_alice_<entity>) satisfiesstartswith("alice_")because the top-level directory namealice-onenormalizes toalice_one_…. So the remap stripsalice_and prepends the full stem on every merge pass — same unbounded growth mechanism described above, with zero same-named path segments anywhere.Observed impact: 17 image-derived nodes reached 16 duplicated stem prefixes (one per incremental merge across ~3 weeks of scheduled updates) before clustering surfaced them as an anomalous community. Notably the semantic cache stayed canonical the whole time (per-file replace on re-extraction), so
graph.jsonand the cache silently diverged and dedup could never reconcile them — different ids are different keys.Fix confirmed locally: an already-canonical guard in
_semantic_id_remap()immediately afternorm_nid = _normalize_id(nid):This mirrors the check
graph_has_legacy_ids()already performs. Tested on 0.9.6:alice_one_public_app_alice_active_tasks_panel→ no remapalice_active_tasks_panel→ still migrates correctlyapp_alice_widget→ still migrates toalice_one_public_app_alice_widgetbuild_merge()over the repaired graph → ids stable across repeated passes (idempotent)ayushcodes10 commentedon Sep 14, 2026
This is already fixed on current v8 (well past 0.9.61), via commit 19c496d "fix(build): make _semantic_id_remap idempotent to stop id accretion (#1917)" — which added exactly the guard proposed here.
Ran both reproductions from this issue directly against current code:
Same-named dir/file (this issue's own repro)
Prefix-only trigger (@hconcepcion-work's broader repro,
alice-one/public/app/alice.jpg)Both match the expected/idempotent behavior this issue asks for. Closing this out rather than opening a duplicate PR — let me know if you'd like it reopened for any remaining edge case.
ayushcodes10 commentedon Oct 20, 2026
Thanks for the detailed writeup — I reproduced both repro snippets against current
upstream/v8and this looks already fixed, not still open.Same result for the
~-path variant (~/memory/memory.db): second pass returns{}._semantic_id_remap()on currentv8already has an "already canonical" short-circuit right where you proposed one, citing a different issue number:It was added by
19c496dd("fix(build): make_semantic_id_remapidempotent to stop id accretion (#1917)"), merged 2026-07-15 — about three weeks before this issue was filed against the pinned0.9.4+colfix1fork. Looks like the same bug, independently found and fixed under #1917 before this report landed. Nothing further to do here as far as I can tell — closing this as a duplicate of #1917 seems right, but leaving that call to a maintainer.