fix(install): leave a hand-written # graphify section in a global file

Every `graphify uninstall` now visits the user's global CLAUDE.md and
CODEBUDDY.md, also for users who never installed the skill there, so a
section they wrote under a `# graphify` heading must not go with
graphify's registration. In those files only a section carrying the
registration bullet graphify has always written (`- **graphify** (`...`)`
with the `/graphify` trigger, in every version since the first) is
removed; a hand-written one stays, and a file holding both is left as is
with a hint rather than guessed at. The project-scoped removal keeps its
existing behavior.

Also: a BOM no longer hides a block on the file's first line and is
written back as found, and a file without the heading is never decoded,
so an unrelated non-UTF-8 file draws no "skipped" warning.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit a7bd4e61e4)
This commit is contained in:
madhusudanpatnaik authored and safishamsi committed 2026-11-03 22:20:47 +00:00
1 parent edc74052ee
commit 67cf4967bf
2 files changed
+153 -4

No files matched your search

+61 -4
View File
@@ -438,9 +438,39 @@ def _project_scope_root(path: Path, project_dir: Path) -> Path:
except ValueError:
return path
return project_dir / rel.parts[0] if rel.parts else path
def _remove_skill_registration(target: Path, prefix: str) -> None:
def _is_graphify_skill_registration(section: "list[str]") -> bool:
"""Whether a `# graphify` section is the registration graphify writes.
Every version of the registration has had the bullet naming the graphify
skill and the `/graphify` trigger, which a hand-written section has no
reason to repeat.
"""
return any(
line.strip().startswith("- **graphify** (`") and "Trigger: `/graphify`" in line
for line in section
)
def _skill_registration_sections(content: str) -> "list[list[str]]":
"""The body of each exact `# graphify` section, bounded as
_remove_marker_section bounds it: up to the next H1."""
lines = content.split("\n")
sections = []
for start, line in enumerate(lines):
if line.strip() == _SKILL_REGISTRATION_MARKER:
end = next(
(j for j in range(start + 1, len(lines)) if lines[j].startswith("# ")),
len(lines),
)
sections.append(lines[start + 1:end])
return sections
def _remove_skill_registration(target: Path, prefix: str, *, only_graphify_written: bool = False) -> None:
"""Remove the skill registration _register_always_on_block wrote into *target*.
*only_graphify_written* is for the user's global CLAUDE.md / CODEBUDDY.md,
which every `graphify uninstall` visits, also for users who never installed
the skill there. Only a `# graphify` section carrying graphify's
registration bullet goes; a hand-written `# graphify` section stays, and a
file holding both is left as is, with a hint, rather than guessed at.
Degrades instead of raising, as the install side does (#3474): a CLAUDE.md
that nix/home-manager links into a read-only store must not abort an
uninstall halfway, with later platforms not yet cleaned.
@@ -448,7 +478,30 @@ def _remove_skill_registration(target: Path, prefix: str) -> None:
try:
if not target.exists():
return
# A file without the heading is never decoded or rewritten, so an
# unrelated non-UTF-8 file draws no warning.
if _SKILL_REGISTRATION_MARKER.encode("utf-8") not in target.read_bytes():
return
content, newline = _read_keep_newline(target)
# A BOM would hide a heading on the first line; it is written back as found.
bom = "" if content.startswith("") else ""
content = content[len(bom):]
if only_graphify_written:
written = [_is_graphify_skill_registration(s) for s in _skill_registration_sections(content)]
if not any(written):
return
if not all(written):
print(
f"{prefix}left as is: {_shown_path(target)} also has a "
f"`{_SKILL_REGISTRATION_MARKER}` section graphify did not write",
file=sys.stderr,
)
print(
f" hint: remove graphify's own `{_SKILL_REGISTRATION_MARKER}` block "
"(the one naming the graphify skill) by hand",
file=sys.stderr,
)
return
# Match the exact H1 `# graphify` registration heading, never a substring of a
# user's `## graphify`/`### graphify` (#2062). Section runs to the next H1.
# _SKILL_REGISTRATION_MARKER is the single source of truth for this heading,
@@ -461,7 +514,7 @@ def _remove_skill_registration(target: Path, prefix: str) -> None:
if cleaned or target.is_symlink():
# The file's own line ending is restored, so the rest of its content is
# never re-translated on write (#3668/#4360).
_write_keep_newline(target, cleaned + "\n", newline)
_write_keep_newline(target, bom + cleaned + "\n", newline)
print(f"{prefix}graphify skill registration removed from {_shown_path(target)}")
else:
target.unlink()
@@ -2427,7 +2480,9 @@ def claude_uninstall(project_dir: Path | None = None, *, project: bool = False,
_remove_skill_file("claude", project=False)
# Without the skill, the global registration names a file that no longer
# exists, and Claude Code reads it in every session.
_remove_skill_registration(_claude_global_registration()[0], " CLAUDE.md -> ")
_remove_skill_registration(
_claude_global_registration()[0], " CLAUDE.md -> ", only_graphify_written=True
)
md_targets = [
project_dir / "CLAUDE.md",
@@ -2551,7 +2606,9 @@ def codebuddy_uninstall(project_dir: Path | None = None, *, project: bool = Fals
if remove_user_skill:
_remove_skill_file("codebuddy", project=False)
# Before the project-file checks below, which return early.
_remove_skill_registration(_codebuddy_global_registration(), " CODEBUDDY.md -> ")
_remove_skill_registration(
_codebuddy_global_registration(), " CODEBUDDY.md -> ", only_graphify_written=True
)
target = project_dir / "CODEBUDDY.md"
if not target.exists():
@@ -230,3 +230,95 @@ def test_uninstall_resolves_the_file_install_wrote(project, tmp_path, monkeypatc
assert _registered(md)
assert f"(`{skill_ref}`)" in md.read_text(encoding="utf-8")
assert install_mod._codebuddy_global_registration() == _codebuddy_md()
# The global files belong to the user, and every `graphify uninstall` visits
# them, also for users who never installed the skill there.
HAND_WRITTEN = {
"notes only": "# graphify\nAlways run graphify update after edits.\n",
"notes, then another H1": "# graphify\nmy rule\n\n# Other\nkeep\n",
"a bullet naming graphify": "# graphify\n- **graphify** (`~/tools/graphify`) is my graph CLI\n",
}
@pytest.mark.parametrize("notes", sorted(HAND_WRITTEN))
@pytest.mark.parametrize("which", ["CLAUDE.md", "CODEBUDDY.md"])
def test_a_hand_written_graphify_section_in_a_global_file_survives(project, notes, which):
md = _claude_md() if which == "CLAUDE.md" else _codebuddy_md()
md.parent.mkdir(parents=True)
md.write_text(HAND_WRITTEN[notes], encoding="utf-8")
install(platform="codex")
uninstall_all(project)
assert md.read_text(encoding="utf-8") == HAND_WRITTEN[notes]
def test_a_global_file_with_both_kinds_of_section_is_left_with_a_hint(project, capsys):
_claude_md().parent.mkdir(parents=True)
_claude_md().write_text("# notes\n", encoding="utf-8")
install(platform="claude")
mixed = _claude_md().read_text(encoding="utf-8") + "\n# graphify\nmy own notes\n"
_claude_md().write_text(mixed, encoding="utf-8")
uninstall_all(project)
assert _claude_md().read_text(encoding="utf-8") == mixed
err = capsys.readouterr().err
assert "did not write" in err and "by hand" in err, err
# What earlier versions appended: an em dash until the project-scope rework,
# a hyphen after it, both with a leading blank line and an older second line.
OLDER_BLOCKS = {
"em dash": (
"\n# graphify\n"
"- **graphify** (`~/.claude/skills/graphify/SKILL.md`) "
"— any input to knowledge graph. Trigger: `/graphify`\n"
"When the user types `/graphify`, invoke the Skill tool "
"with `skill: \"graphify\"` before doing anything else.\n"
),
"hyphen": (
"\n# graphify\n"
"- **graphify** (`~/.claude/skills/graphify/SKILL.md`) "
"- any input to knowledge graph. Trigger: `/graphify`\n"
"When the user types `/graphify`, invoke the Skill tool "
"with `skill: \"graphify\"` before doing anything else.\n"
),
}
@pytest.mark.parametrize("older", sorted(OLDER_BLOCKS))
def test_a_block_an_older_version_wrote_is_removed(project, older):
_claude_md().parent.mkdir(parents=True)
_claude_md().write_text(USER_NOTES + OLDER_BLOCKS[older], encoding="utf-8")
uninstall_all(project)
assert _claude_md().read_text(encoding="utf-8") == USER_NOTES
def test_a_bom_neither_hides_the_block_nor_is_lost(project):
bom = ""
_claude_md().parent.mkdir(parents=True)
_claude_md().write_text(bom + install_mod._skill_registration(), encoding="utf-8")
uninstall_all(project)
assert not _claude_md().exists()
original = (bom + USER_NOTES).encode("utf-8")
_claude_md().write_bytes(original)
install(platform="claude")
uninstall_all(project)
assert _claude_md().read_bytes() == original
def test_an_unrelated_non_utf8_global_file_is_left_alone_quietly(project, capsys):
raw = b"# notes\n\xff\xfe not utf-8\n"
_claude_md().parent.mkdir(parents=True)
_claude_md().write_bytes(raw)
uninstall_all(project)
assert _claude_md().read_bytes() == raw
assert "skipped" not in capsys.readouterr().err