graphify icon indicating copy to clipboard operation
graphify copied to clipboard

[Bug]: node-ID rule 'only [a-z0-9_]' still contradicts ids.py for non-ASCII paths in 0.9.89, in both llm.py and extraction-spec.md (#3103 closed without a fix)

Open Stashub opened this issue 1 month ago • 3 comments

Pre-flight checks

  • [x] I have checked the Troubleshooting section in the README

What happened?

#3103 (closed by its author as completed, no linked commit) reported that the node-ID rule in the extraction prompt contradicts graphify/ids.py for non-ASCII paths. On 0.9.89 / v8 the wording is unchanged, and it is in two prompts, not one:

  • graphify/llm.py _EXTRACTION_SYSTEM (used by graphify extract --backend ...): Node ID format: lowercase, only [a-z0-9_], no dots or slashes. plus stem = full repo-relative path with the extension dropped
  • graphify/skills/*/references/extraction-spec.md line 61 (skill subagents): same rule

For a file named in Cyrillic the two halves cannot both be satisfied: the canonical stem from make_id(_file_stem(path)) keeps Cyrillic, e.g. docs/tz/ЛЗ4_обоснование_с_листом_Расчет_Кдог.md -> docs_tz_лз4_обоснование_с_листом_расчет_кдог. The model therefore improvises a different stem per chunk and per run, and _semantic_id_remap does not reconcile any of them.

Expected: every semantic node from one file carries the canonical stem that ids.py derives, regardless of the script the path is written in.

Steps to reproduce

# corpus: Russian-named markdown files, e.g.
#   docs/tz/ЛЗ4_обоснование_с_листом_Расчет_Кдог.md
#   docs/tz/СРЗ КП18-С301   ЛЗ2 (105) - 12.10.2026.md
#   docs/tz/СРЗ КП18-С303   ЛЗ5 (105) - 12.10.2026.md   (large, split across chunks)
graphify extract . --backend claude-cli --force --allow-dedup-shrink

# canonical stems
python -c "
from graphify.ids import make_id
from graphify.extractors.base import _file_stem
from pathlib import Path
for p in ['docs/tz/ЛЗ4_обоснование_с_листом_Расчет_Кдог.md',
          'docs/tz/СРЗ КП18-С301   ЛЗ2 (105) - 12.10.2026.md',
          'docs/tz/СРЗ КП18-С303   ЛЗ5 (105) - 12.10.2026.md']:
    print(make_id(_file_stem(Path(p))))
"

# the remap leaves model-invented stems alone
python -c "
from graphify.build import _semantic_id_remap
sf = 'docs/tz/ЛЗ4_обоснование_с_листом_Расчет_Кдог.md'
print(_semantic_id_remap([
  {'id': 'docs_tz_lz4_obosnovanie_kdog', 'label': 'f', 'file_type': 'document', 'source_file': sf},
  {'id': 'docs_tz_lz4_obosnovanie_kdog_raschet5_fot', 'label': 'x', 'file_type': 'concept', 'source_file': sf},
], None))
"

Error output or graph output

canonical stems (ids.py):
docs_tz_лз4_обоснование_с_листом_расчет_кдог
docs_tz_срз_кп18_с301_лз2_105_12_10_2026
docs_tz_срз_кп18_с303_лз5_105_12_10_2026

node ids per file in graph.json after ONE extract run (claude-cli backend, 0.9.89):
ЛЗ4_обоснование_с_листом_Расчет_Кдог.md  15 nodes: 9  docs_tz_lz4_obosnovanie_kdog_*  (transliterated + abbreviated)
                                                    6  no file stem at all: gnb_directional_drilling, lz4_smeta_11559, ...
СРЗ КП18-С301   ЛЗ2 (105) - 12.10.2026.md 28 nodes: 4  docs_tz_srz_kp18_s301_lz2_*  (transliterated, date dropped)
                                                    24 no file stem: s301_totals, srz_dogovor_446s001s, concept_kp18_s301, ...
СРЗ КП18-С302   ЛЗ4 (105) - 12.10.2026.md 59 nodes: 32 docs_tz_srz_kp18_s302_lz4_105_12_10_2026_*
                                                    27 no file stem: s302_obj_ktp21, s302_contract, concept_kp18_s302, ...
СРЗ КП18-С303   ЛЗ5 (105) - 12.10.2026.md 82 nodes: 73 docs_tz_srz_kp18_s303_lz5_105_12_10_2026_*
                                                    9  docs_tz_срз_кп18_с303_лз5_105_12_10_2026_*  (canonical, another chunk)
ТКП Монтаж МКС ЛЗ-2,4,5 ver.04.md          8 nodes: 8  docs_tz_ткп_монтаж_мкс_лз_2_4_5_ver_04_*  (canonical)

_semantic_id_remap: {}

So the model does not just transliterate: for many nodes it gives up on {stem}_{entity} entirely, which also makes ids like concept_gesn_norms free to collide across files.

Consequences:

  1. The stem is not stable: it differs between chunks of one file in the same run and between runs. A cross-file edge cached for file X that targets a node of file Y dangles as soon as Y is re-extracted under another spelling, and build_from_json drops such an edge silently (checked: an edge to a missing id yields 0 edges, no stub).
  2. Stemless ids (concept_*, s301_*) are not scoped to their file, so two files can mint the same id for different things and fall into the cross-file collision path.
  3. Logic keyed on the canonical stem (the file's own node in _id_prefixes, _doc_twin_remap, _disambiguate_defining_collisions) never recognises these nodes.
  4. A skill-subagent run on the same corpus (prompt from extraction-spec.md) produced canonical Cyrillic stems only because each subagent noticed the conflict and called graphify.ids.make_id itself; following the prompt literally gives the docs______________doc collapse #3103 describes.

Graphify version

0.9.89 (also v8 at e8eb76e7)

Operating System

Linux

Python Version

3.13

Installation Method

uv tool install (recommended)

Additional Environment Details

Backend claude-cli (Claude Code 2.1.296), no provider keys set.

Additional context

Two possible fixes, either removes the drift:

  • Prompt: state the charset as \w with Unicode semantics in both _EXTRACTION_SYSTEM and extraction-spec.md (the fix #3103 suggested), and give a non-ASCII example such as docs/отчёт.md -> docs_отчёт.
  • Code: _semantic_id_remap already computes new_stem from source_file; for a non-AST node whose id does not start with new_stem, it could re-key it to new_stem + the id's entity part, so the stored id stops depending on the model's spelling. The entity part is the hard part, since the model's stem has no fixed length; using the node label for the entity part is one way around that.

Stashub avatar Oct 13 '26 18:10 Stashub

Thanks for opening this issue, @Stashub. A maintainer will take a look soon.

If you would like to discuss it in real time, come say hi on our Discord server. For longer-form questions and ideas there is also GitHub Discussions.

To help us triage, please make sure the report includes what you expected, what actually happened, and the steps (and a small sample) to reproduce it.

github-actions[bot] avatar Oct 13 '26 18:10 github-actions[bot]

I checked this on 0.9.89. The prompt says ids use only [a-z0-9_] (llm.py _EXTRACTION_SYSTEM and extraction-spec.md), but ids.py keeps Cyrillic. The model can't follow both, so it guesses a stem, and the guess changes between chunks and between runs. _semantic_id_remap leaves those ids as they are.

I plan to fix it in two steps:

  1. Reword the prompt to match ids.py: lowercase, Unicode letters and digits kept, everything else becomes _.
  2. Compute each file's stem in code (make_id(_file_stem(path))) and put it in the chunk prompt, so the model copies the stem instead of guessing.

The tests will use paths from this issue, for example docs/tz/ЛЗ4_обоснование_с_листом_Расчет_Кдог.md → docs_tz_лз4_обоснование_с_листом_расчет_кдог.

The model can still ignore the stem it's given. This change states the right id plainly, but it doesn't enforce it.

I'll raise a PR soon and link it here.

Deepjyoti-Sarmah avatar Oct 13 '26 19:10 Deepjyoti-Sarmah

Sent #4799 for this.

Confirmed the contradiction is real and in three places, not the two the report names: llm.py's _EXTRACTION_SYSTEM plus both skillgen fragments (extraction-spec.md and extraction-spec-compact.md), which render into 14 host skills.

make_id("docs/tz/ЛЗ4_обоснование")  ->  docs_tz_лз4_обоснование
make_id("文档/中文名")                ->  文档_中文名

So the prompt's "only [a-z0-9_]" cannot be satisfied by anything the AST extractor produces. I aligned the prompt with the code rather than the other way round — ids.py keeps non-ASCII deliberately, its docstring says so, and changing it would invalidate existing ids for every non-ASCII corpus. The ASCII half of the rule is untouched, so no ASCII project gets different ids.

Two scope notes:

  • I edited the two fragments and regenerated the artifacts with python -m tools.skillgen rather than hand-editing graphify/skills/**, so the diff shows 28 regenerated files that each differ by that single line. --check reports all 134 in sync.
  • This only fixes the instruction. A model that still transliterates will still mismatch, so the _semantic_id_remap part of this issue stays open — if you would rather have both halves in one PR, say so and I will fold it in.

dupeisen avatar Oct 26 '26 19:10 dupeisen