Skip to content

fix(wiki): keep the article when a community is relabelled only by case (#4508) - #4798

Closed
dupeisen wants to merge 1 commit into
Graphify-Labs:v8from
dupeisen:fix/4508-wiki-case-only-relabel
Closed

dupeisen wants to merge 1 commit into
Graphify-Labs:v8from
dupeisen:fix/4508-wiki-case-only-relabel

Conversation

@dupeisen

Copy link
Copy Markdown
Contributor

What this fixes

to_wiki writes each article through write_text_atomic_if_changed, then sweeps the *.md files the run did not produce. On a case-preserving, case-insensitive filesystem (APFS, NTFS) the write for Groups.md replaces the contents of the existing groups.md entry without renaming it. The sweep then compared names case-sensitively:

if old_article.name not in produced:   # "groups.md" not in {"Groups.md"}
    old_article.unlink()               # ...deletes the file this run just wrote

So a community relabelled from groups to Groups loses its article entirely, and index.md keeps a link to a Groups.md that no longer exists.

Repro (on v8 @ dac0634, Windows NTFS)

G = nx.Graph()
G.add_node("a", label="parse()", source_file="src/a.py", file_type="code")
G.add_node("b", label="tokenize()", source_file="src/b.py", file_type="code")
G.add_edge("a", "b", relation="calls")

out = pathlib.Path(tempfile.mkdtemp()) / "wiki"
to_wiki(G, {0: ["a", "b"]}, out, community_labels={0: "groups"})
print(sorted(p.name for p in out.glob("*.md")))     # ['groups.md', 'index.md']

to_wiki(G, {0: ["a", "b"]}, out, community_labels={0: "Groups"})
print(sorted(p.name for p in out.glob("*.md")))     # ['index.md']   <- article gone

index.md still links to Groups.md, which does not exist.

After the change

第一次导出:   ['groups.md', 'index.md']
大小写改名后: ['Groups.md', 'index.md']
index 里的链接: [('Groups', 'Groups.md')]
失效链接: 无

Implementation

Comparisons fold case, and a case-only difference is renamed through a temporary name:

produced_by_fold = {name.casefold(): name for name in produced}
for old_article in out.glob("*.md"):
    wanted = produced_by_fold.get(old_article.name.casefold())
    if wanted is None:
        old_article.unlink()
    elif old_article.name != wanted:
        temp = out / f".{wanted}.case-tmp"
        old_article.rename(temp)
        temp.rename(out / wanted)

The two-step rename is not defensive noise: on a case-insensitive filesystem groups.md and Groups.md are the same file, so Path.rename with the direct target is a no-op that leaves the old case on disk. Going through a distinct temporary name is what actually changes the directory entry.

_unique_slug in the same function already folds case when it dedups colliding labels, with a comment naming APFS/NTFS explicitly — so the sweep was the one remaining case-sensitive comparison, which is what the issue suggested.

Verification

New tests/test_wiki_case_only_relabel.py (4 tests):

  • the article survives the case-only relabel;
  • the new case reaches the directory entry;
  • index.md has no dangling link;
  • a genuinely stale article from an older run is still swept.

The first three fail on unmodified v8 (3 failed, 1 passed). The fourth passes both ways — that is deliberate, it is the guard against over-correcting the sweep into "never delete anything".

$ uv run pytest tests/test_wiki_case_only_relabel.py -q
4 passed

$ uv run pytest tests/test_wiki.py -q
20 failed, 15 passed     # identical to unmodified v8

$ uv run ruff check graphify/wiki.py tests/test_wiki_case_only_relabel.py
All checks passed!

$ uv run pyright graphify/wiki.py
1 error   # pre-existing at wiki.py:292, identical on unmodified v8

Note on the test_wiki.py numbers: those 20 failures are pre-existing on this Windows checkout and are all UnicodeDecodeError from test code calling read_text() without an encoding (see test_index_links_every_article_written reading index.md). I confirmed the same 20 failed, 15 passed on unmodified v8, so this change neither adds nor hides any of them. I did not touch them — that is a separate concern and not what this issue reports.

Limitations

  • Only the sweep's comparison changed. A community relabelled to a name that collides with an unrelated existing article still goes through _unique_slug's dedup, which is unchanged.
  • The temporary name is .Groups.md.case-tmp; if a run is killed between the two renames the temp file survives and the next sweep deletes it, since it is not in produced. Leaving it rather than guessing at recovery seemed right for a file the next run regenerates anyway.

Fixes #4508


Disclosure: I used an AI coding assistant for this change, as the contributing guide asks. I reproduced the bug before and after on a clean checkout, and confirmed the new assertions fail without the fix.

`to_wiki` writes each article through `write_text_atomic_if_changed`, then
sweeps the `*.md` files the run did not produce. On a case-preserving,
case-insensitive filesystem (APFS, NTFS) the write for `Groups.md` replaces the
contents of the existing `groups.md` entry without renaming it. The sweep then
compared names case-sensitively, saw `"groups.md" not in {"Groups.md"}` and
unlinked the file the same run had just written, so `index.md` linked to a
`Groups.md` that no longer existed.

Comparisons now fold case, and a case-only difference is renamed through a
temporary name so the new case reaches disk. The two-step rename is required
because a case-insensitive filesystem treats source and destination as one
file. `_unique_slug` already folds case when it dedups colliding labels; this
was the remaining case-sensitive comparison.

Tests: tests/test_wiki_case_only_relabel.py (4 tests) covers the article
surviving, the new case landing on disk, no dangling link in `index.md`, and a
genuinely stale article still being swept. The first three fail on unmodified
v8; the fourth passes both ways, which is the guard against over-correcting the
sweep.

Fixes Graphify-Labs#4508

Co-Authored-By: Claude <[email protected]>
@dupeisen
dupeisen requested a review from safishamsi as a code owner October 26, 2026 19:36
@github-actions

Copy link
Copy Markdown

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

@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 not verified).

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

Not verified on this run: to\_wiki (vacuous: never exercised).


Graphify review — findings

Fixes the orphan sweep in to_wiki so a community relabelled only by letter case keeps its article, instead of having the freshly written file deleted and index.md left pointing at nothing (#4508). The sweep now compares filenames case-folded, and any on-disk entry whose folded name matches a produced article but differs in case is renamed to the new spelling through a temporary .{name}.case-tmp file, since a case-insensitive filesystem treats a direct rename as a no-op. That rename runs on every folded match with a different case, so it also fires where the old-case file is a separate entry from the one just written, and a failure between the two steps leaves the temp file behind. Names with no folded match are still unlinked as before, and the new tests cover the surviving article, the new case on disk, resolvable index.md links, and genuine stale-file removal.

Worth a look

  • On case-sensitive filesystems, the case-only rename overwrites the fresh article with stale content — graphify/wiki.py:461 · Escalate · high
    • NOT verified (no proof, no reproducing execution) — needs human review
Analysis details — impact, health, verification

Impact & health

Graphify review

Impact — 322 functions depend on the 28 functions this change touches.

Health — this change adds coupling hotspots:

  • worse: to_wiki() — 53 callers, 8 callees

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

Test selection

Test selection

6 of 466 test file(s) selected (1%) via static blast radius.

  • tests/test_export_idempotent_writes.py — impact
  • tests/test_export_path_length.py — impact
  • tests/test_wiki.py — impact
  • tests/test_wiki_case_only_relabel.py — impact, changed-test
  • tests/test_wiki_link_filename_parity.py — impact
  • tests/test_wiki_truncation_indicator.py — impact

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

Could not verify: Could not verify to\_wiki.

The verifier did not have enough to check to\_wiki, 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: no capturable inputs from the test suite; property tier: not verifiable: all 225 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)

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

safishamsi added a commit that referenced this pull request Oct 27, 2026
… rename over the fresh file

Follow-up to #4798. Its case-only-relabel sweep ran the two-step rename
unconditionally whenever the on-disk name differed from the produced name
by case. That is right on a case-INSENSITIVE filesystem (APFS, NTFS)
where the two are the same inode, but on a case-SENSITIVE filesystem
(ext4, most Linux CI and production hosts) the run already wrote the
correct content to a distinct file, so renaming the stale lower-case twin
over it silently restored the old body: no missing file, no dangling
link, just wrong content. #4508 never existed on case-sensitive hosts, so
the PR turned a non-bug into a silent corruption there.

Guard the rename with target.exists() and not target.samefile(old): a
distinct inode is a stale twin and is unlinked; the same inode takes the
rename. Adds a content-level regression test (the PR's existence/link
tests pass on both filesystems even with the bug, so they could not catch
this); it fails on a case-sensitive host without the guard.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@safishamsi

Copy link
Copy Markdown
Member

Landed in v0.9.97, now on PyPI (pip install graphifyy==0.9.97). A community relabelled only by case keeps its article, with a case-sensitive-filesystem guard. Thanks for the fix, @dupeisen.

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.

export wiki: a community relabelled only by case loses its article on case-insensitive filesystems (orphan sweep compares names case-sensitively)

2 participants