Skip to content

wiki export: index links both same-named communities to the first article, orphaning <Name>_2.md #3032

Closed

Description

@Heratiki

Summary

When two distinct communities receive the same label, to_wiki correctly writes two article files (Name.md and Name_2.md), but index.md links both rows to Name.md. Name_2.md is written and then unreachable — nothing in the wiki links to it.

No data is lost; the articles are on disk. The problem is navigational: the index shows two rows with different node counts pointing at one page, and an agent crawling from index.md (the documented entry point) can never reach the second community.

Repro

graphify 0.9.20, Python 3.14, networkx louvain fallback (graspologic not installed).

import networkx as nx, pathlib
from graphify.wiki import to_wiki

G = nx.Graph()
G.add_edges_from([("a1","a2"),("a2","a3"),("b1","b2"),("b2","b3")])
communities = {0: ["a1","a2","a3"], 1: ["b1","b2","b3"]}
labels = {0: "Shared Name", 1: "Shared Name"}   # distinct communities, same label

to_wiki(G, communities, "wiki_repro", community_labels=labels)

print("files:", sorted(p.name for p in pathlib.Path("wiki_repro").glob("*.md")))
for ln in pathlib.Path("wiki_repro/index.md").read_text().splitlines():
    if ln.startswith("- [Shared Name]"): print("   ", ln)

Output:

files: ['Shared_Name.md', 'Shared_Name_2.md', 'index.md']
    - [Shared Name](Shared_Name.md) — 3 nodes
    - [Shared Name](Shared_Name.md) — 3 nodes

Expected: the second row links to Shared_Name_2.md.

Root cause

Two parallel maps are built, and the index uses the wrong one.

wiki.py:305-309 — articles get a genuinely unique slug per community id, but the resolver is keyed by label, so the second community's slug never enters it:

for cid in communities:
    label = labels.get(cid, f"Community {cid}")
    slug = _unique_slug(_safe_filename(label))
    community_slugs[cid] = slug          # unique per cid  -- correct
    resolver.setdefault(label, slug)     # label -> slug, first wins

wiki.py:324 writes each article under community_slugs[cid], which is why both files exist and are correct.

wiki.py:194 builds the index rows through the label-keyed resolver instead:

lines.append(f"- {_md_link(label, resolver)} — {len(nodes)} nodes")

_md_link(label, resolver) looks up by label, so both rows resolve to the first slug.

The setdefault is deliberate (the comment at wiki.py:298-301 notes it preserves filename-assignment order for the case-collision dedup) — the issue is only that the index consumes resolver when it already has the cid in hand.

Suggested fix

_index_md already iterates communities.items() and has cid. Passing community_slugs and linking by cid would resolve it without touching resolver or the dedup ordering:

# _index_md(..., community_slugs)
slug = community_slugs[cid]
lines.append(f"- [{label}]({quote(slug)}.md) — {len(nodes)} nodes")

God-node articles at wiki.py:315-317 use the same resolver.setdefault pattern and look like they'd collide the same way, though I didn't reproduce that path.

Impact in practice

On a 12,398-node / 871-community graph, LLM-generated labels collided on 2 names in one labeling run and 20 in another — same graph, same partition, just naming variance between runs. So the orphan count is not stable, and on an unlucky run a couple dozen communities drop out of navigation entirely.

Activity

  1. Heratiki commented on Aug 24, 2026

    @Heratiki
    Author

    Followed up on the god-node path I flagged as unverified at the end of this issue: it does collide the same way, reproduced and filed as #3033.

    Both are the residual half of #497 — that fix's _unique_slug correctly stopped the file overwrites (files are written, distinct, and correct in both paths); what survived is the index resolving links by label instead of by the slug it already has in hand.

  2. rohit-jsfreaky commented on Aug 25, 2026

    @rohit-jsfreaky
    Contributor

    Root cause is in the index rather than the writer: to_wiki already gives each community its own slug (community_slugs), but _index_md re-derives the link target from the label -> slug resolver, which is built with setdefault — so the first "Shared Name" wins and both rows get Shared_Name.md.

    Fix passes the slugs the articles were actually written under into _index_md and links by those. #3033 is the same defect in the god-node loop, so both are in one PR. Your repro now prints Shared_Name_2.md on the second row.

  3. safishamsi commented on Oct 12, 2026

    @safishamsi
    Member

    Fixed in v0.9.88 (graphifyy 0.9.88 on PyPI) via #3095.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions