Skip to content

graphify update + export wiki silently wipes wiki/ when .graphify_analysis.json is missing (data loss) #746

Closed

Description

@boo-code

Summary

Running graphify update . followed by graphify export wiki on a project that does not have a .graphify_analysis.json sidecar deletes every existing wiki article and writes only god-node summaries (~10 files), regardless of how many community articles previously existed.

In our project this caused a silent loss of 2,519 wiki articles when an automated pre-push hook ran the two commands in sequence. Detection happened only after the regression had already merged.

Tested on graphifyy==0.7.5 (Python package), Linux x86_64, default branch v7.

Reproduction

Starting from a project that has a populated graphify-out/ (e.g. produced by an earlier graphify extract . run with thousands of wiki articles):

# Pre-condition
ls graphify-out/wiki/*.md | wc -l        # → 2519 (or any large count)
test -f graphify-out/.graphify_analysis.json && echo "sidecar present" || echo "sidecar MISSING"
# Make the sidecar 'missing' (simulating a clean checkout / a project that
# only ran `update`, never `extract`):
rm -f graphify-out/.graphify_analysis.json

# Reproduction
graphify update .
graphify export wiki

# Post-condition
ls graphify-out/wiki/*.md | wc -l        # → 11   ← every community article gone

Same outcome if the sidecar never existed at all (e.g. project initialized via graphify update only, without ever running the LLM-driven extract).

Root cause

Three independent design choices combine into a destructive interaction:

  1. graphify update (graphify/watch.py::_rebuild_code) writes graph.json, GRAPH_REPORT.md, and .graphify_root — but never writes .graphify_analysis.json.

  2. graphify cluster-only (graphify/__main__.py:1496-1551) re-clusters the graph but also never writes .graphify_analysis.json. Only graphify extract (__main__.py:2154-2188) writes that sidecar, and extract requires LLM API access (expensive; not appropriate for a pre-push hook or watch loop).

  3. graphify export wiki (__main__.py:1800-1808) reads communities from .graphify_analysis.json and silently falls back to {} if the file is absent:

    communities: dict[int, list[str]] = {}
    if analysis_path.exists():
        _an = json.loads(analysis_path.read_text(encoding="utf-8"))
        communities = {int(k): v for k, v in _an.get("communities", {}).items()}
  4. The destructive wiki-cleanup runs before the (now empty) community-iteration loop. From graphify/wiki.py::to_wiki (lines 199-206 in 0.7.5):

    # Clear stale .md files from previous runs to prevent orphan accumulation.
    # ...to_wiki() owns wiki/ entirely (always writes the full set),
    # it can safely clear .md files at the start of each call.
    for old_article in out.glob("*.md"):
        old_article.unlink()

The cleanup is correct iff to_wiki() actually writes the full set. With an empty communities dict the loop produces no community articles — only the god-node summaries. The cleanup-then-write pattern collapses the entire wiki/ directory.

Impact

  • Silent data loss. No warning, no non-zero exit code. The post-export wiki/ looks superficially valid (a few .md files exist), so automation cannot detect the regression by checking for empty output.
  • Unsafe in CI / pre-push / watch automation. Any workflow that pairs update (or cluster-only) with export wiki will produce this regression on every run after a clean checkout.
  • Encourages workarounds. Downstream projects either pin themselves to running the expensive extract on every push (LLM cost), or build out-of-tree helpers to fill the gap.

In our case (a private monorepo) we caught this only post-merge, when the affected PR shipped 2,519 file deletions to main. We've since:

  • Restored the wiki/ directory from a pre-regression commit.
  • Built a local helper that imports graphify.cluster + graphify.analyze from the installed package and recomputes the sidecar without LLM calls (Louvain clustering is deterministic), then injects the sidecar between update and export wiki.
  • Added a "wiki article count drop > 50%" safety guard in our pre-push hook to abort + roll back if any future regression in this class slips through.

These fixes work, but the underlying contract should be repaired upstream so other graphify users don't hit the same trap.

Suggested fix (one of)

Pick the option that fits the project's design intent best:

  1. Make update derive the sidecar. After writing graph.json, if .graphify_analysis.json is absent or older than graph.json, recompute communities/cohesion/gods/surprises locally (no LLM) and write the sidecar — mirroring the analysis-write block at __main__.py:2154-2176. This keeps update as the single command users run between extract baselines.

  2. Make export wiki fail fast when the sidecar is missing. Replace the silent if analysis_path.exists() fallback with an explicit error (non-zero exit + "refusing to export wiki: .graphify_analysis.json missing or empty; run graphify extract .orgraphify recluster . first"). At minimum this prevents data loss; users opt back in to the destructive cleanup only when the precondition is met.

  3. Make to_wiki()'s cleanup safe when the community set is empty. Skip the for old_article in out.glob("*.md"): old_article.unlink() step when communities is empty (or below a heuristic threshold). The cleanup invariant in the comment ("to_wiki() owns wiki/ entirely (always writes the full set)") is only true when the full set is non-empty.

Option 1 is preferred — it closes the semantic gap rather than just papering over the symptom, and is consistent with how downstream consumers expect update to refresh derived artifacts.

Environment

  • graphifyy==0.7.5 (installed via uv tool install graphifyy)
  • Python 3.13.0 (graphifyy venv interpreter)
  • Linux x86_64
  • Default repo branch: v7

Happy to test a fix against the same reproducer if it helps.

Activity

  1. safishamsi commented on May 6, 2026

    @safishamsi
    Member

    Fixed in v0.7.8: two-layer guard added. (1) graphify export wiki now fails fast with exit 1 and a clear error message if .graphify_analysis.json is missing. (2) to_wiki() raises ValueError before the cleanup loop when communities is empty — wiki/ is never wiped without a full community set.

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