Skip to content

fix(install): preserve line endings of CLAUDE.md, AGENTS.md and siblings (#4360) - #4372

Closed
nothariharan wants to merge 1 commit into
Graphify-Labs:v8from
nothariharan:fix/preserve-line-endings
Closed

nothariharan wants to merge 1 commit into
Graphify-Labs:v8from
nothariharan:fix/preserve-line-endings

Conversation

@nothariharan

Copy link
Copy Markdown
Contributor

What this fixes

Closes #4360.

#3741 stopped two writers from translating line endings on write (newline=""), but every install/uninstall path in the issue still reads through Path.read_text, which opens in universal-newline mode and folds \r\n to \n before graphify's block is merged. The later write then re-emitted the platform default, so a read-modify-write normalised the whole file:

  • On Windows, an LF file gained CRLF on every line.
  • On Linux/macOS, a CRLF file lost its \r.

For CLAUDE.md / AGENTS.md, which are shared and reviewed, that buries the real one-block change in an every-line diff and can trip line-ending checks.

Confirmed real on v8 (0.9.87)

The issue's minimal repro, run end-to-end through the real CLI:

$ printf 'a\nb\n' > CLAUDE.md        # LF file
$ graphify claude install
$ file CLAUDE.md                      # -> CRLF line terminators   (bug)
$ graphify uninstall
$ file CLAUDE.md                      # -> still CRLF              (bug)

Function-level check on Windows showed CLAUDE.md, AGENTS.md, GEMINI.md, CODEBUDDY.md and .github/copilot-instructions.md all flipping LF → CRLF after install, and staying CRLF after uninstall.

The fix

One shared read/write pair, used by every listed writer so the read and write sides can no longer drift apart:

  • _read_keep_newline(path) -> (text, newline) — reads bytes (bypassing universal-newline translation), detects the file's dominant ending (\r\n or \n), and returns bare-\n text so the existing _replace_or_append_section / _remove_marker_section helpers keep working unchanged.
  • _write_keep_newline(path, text, newline) — converts bare \n to the detected ending and writes with newline="", so nothing is translated on the way out. A fresh file (no prior convention) is written LF, matching what the other writers already emit.

Writers updated

Path Install Uninstall
CLAUDE.md claude_install _strip_graphify_md_section
AGENTS.md _agents_install _agents_uninstall
GEMINI.md gemini_install* gemini_uninstall
CODEBUDDY.md codebuddy_install codebuddy_uninstall
.github/copilot-instructions.md vscode_install (new-file branch too) vscode_uninstall
# graphify H1 registration _register_always_on_block* _remove_claude_skill_registration

* the two #3741 paths are included: they had the write side fixed but still read through read_text, so a CRLF file was still normalised to LF on any host. Fixing the read side closes that gap.

As a small simplification, the installers now reuse the already-read content for the "already configured (no change)" comparison instead of re-reading the file.

Tests

New tests/test_install_line_endings.py:

  • A round-trip test per writer (claude, agents, gemini, codebuddy, vscode, always-on registration, _strip_graphify_md_section): the file is seeded with an LF body and again with a CRLF body; after install only the graphify block changed and every line keeps the original ending; after uninstall the bytes equal the original exactly.
  • A fresh-file test asserting LF (not platform CRLF).
  • Direct unit tests for _read_keep_newline / _write_keep_newline.

The bug is observable in the file bytes on any host OS (LF input fails on Windows, CRLF input fails on Linux), so the tests are not platform-gated.

Verification:

  • New tests pass: 17 passed.
  • Pre-fix, the same suite fails 11 cases — confirming the tests pin the bug.
  • Existing install/uninstall suites unaffected: 253 passed, 3 skipped; the wider 5-file run has byte-identical pass/fail sets before and after the change.
  • ruff check clean.

Scope

Only the read-modify-write writers listed in the issue (plus the #3741 read side). Graphify-owned rule files that aren't shared/reviewed (kiro, antigravity, cursor, devin) and the JSON settings writers are unchanged.

…ngs (Graphify-Labs#4360)

Graphify-Labs#3741 stopped the always-on and Gemini writers from translating line endings
on write (newline=""), but every one of these paths still READ through
Path.read_text, which opens in universal-newline mode and folds CRLF to LF
before the block is merged. A later write then re-emitted the platform
default, so a read-modify-write normalised the WHOLE file: on Windows an LF
file gained CRLF on every line, on Linux a CRLF file lost its CR.

Install and uninstall now go through one shared read/write pair
(_read_keep_newline / _write_keep_newline): the file's own dominant ending is
detected on read, the section helpers keep working on bare-LF text, and the
ending is restored on write with newline="". A file with no prior convention
is written LF, matching the other writers. This covers claude_install,
_agents_install, codebuddy_install, gemini_install/_uninstall,
vscode_install/_uninstall, _strip_graphify_md_section, _agents_uninstall,
codebuddy_uninstall and _remove_claude_skill_registration, plus the two
Graphify-Labs#3741 paths so the read side can no longer drift from the write side.

Adds a round-trip test per writer: an LF file and a CRLF file in, same
endings out, and install-then-uninstall returns the exact original bytes.
@github-actions

Copy link
Copy Markdown

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

@github-actions github-actions Bot added the contributor Pull request from a returning community contributor label Oct 12, 2026

@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; no formal proof of the changed code.


Graphify review — findings

Preserves each instruction file's own line endings when graphify installs or uninstalls its section in CLAUDE.md, GEMINI.md, AGENTS.md and copilot-instructions.md, so the rest of the file is never re-encoded. _read_keep_newline reads raw bytes, records whether the file uses CRLF or LF, and hands the section helpers bare-\n text. _write_keep_newline restores that ending with no further translation, so LF files no longer become CRLF on Windows and CRLF files no longer flatten to LF elsewhere; new files are written as plain LF.

Worth a look

  • _read_keep_newline treats any CRLF as the file convention instead of the dominant ending — graphify/install.py:801 · Escalate · medium
    • agreed by 2 of 2 members but NOT verified (no proof, no reproducing execution) — consensus is not a verdict; needs human review
Analysis details — impact, health, verification

Impact & health

Graphify review

Impact — 594 functions depend on the 182 functions this change touches.

Health — this change adds coupling hotspots:

  • worse: codebuddy_install() — 22 callers, 7 callees
  • worse: claude_install() — 20 callers, 6 callees
  • worse: gemini_install() — 12 callers, 9 callees
  • worse: codebuddy_uninstall() — 15 callers, 6 callees
  • worse: claude_uninstall() — 18 callers, 4 callees
  • worse: _agents_install() — 7 callers, 8 callees
  • worse: gemini_uninstall() — 9 callers, 6 callees
  • worse: vscode_install() — 5 callers, 10 callees
  • …and 4 more — each is listed as a finding

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

Test selection

Test selection

40 of 391 test file(s) selected (10%) via static blast radius.

  • tests/test_affected_cli.py — impact
  • tests/test_agents_platform.py — impact
  • tests/test_atomic_version_stamp.py — impact
  • tests/test_claude_md.py — impact
  • tests/test_codebuddy.py — impact
  • tests/test_dedup_shrink_refuses_force_write.py — impact
  • tests/test_detect.py — impact
  • tests/test_devin.py — impact
  • tests/test_explain_cli.py — impact
  • tests/test_explain_same_file_tie.py — impact
  • tests/test_extract_cli.py — impact
  • tests/test_gemini_hook.py — impact
  • tests/test_global_add_tag_inference.py — impact
  • tests/test_god_nodes_cli.py — impact
  • tests/test_hollow_chunks_arm_shrink_guard.py — impact
  • tests/test_home_sandbox.py — impact
  • tests/test_hook_strict.py — impact
  • tests/test_incomplete_build_guard.py — impact
  • tests/test_install.py — impact
  • tests/test_install_line_endings.py — impact, changed-test
  • tests/test_install_references.py — impact
  • tests/test_install_strings.py — impact
  • tests/test_install_version_warning.py — impact
  • tests/test_lookup_exit_codes.py — impact
  • tests/test_merge_chunks_validation.py — impact
  • tests/test_multigraph_diagnostics.py — impact
  • tests/test_no_dedup_flag.py — impact
  • tests/test_opencode_plugin_refresh.py — impact
  • tests/test_path_cli.py — impact
  • tests/test_query_cli.py — impact
  • tests/test_query_induced_edges.py — impact
  • tests/test_read_hook.py — impact
  • tests/test_replace_or_append_section.py — impact
  • tests/test_search_hook.py — impact
  • tests/test_skill_auto_refresh.py — impact
  • tests/test_skill_version_warning.py — impact
  • tests/test_truncation_full_answer.py — impact
  • tests/test_uninstall_purge_safety.py — impact
  • tests/test_uninstall_scope.py — impact
  • tests/test_unverified_semantic_shrink.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.

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

@safishamsi

Copy link
Copy Markdown
Member

Shipped in v0.9.88, just released to PyPI as graphifyy 0.9.88. Landed via an authorship-preserving cherry-pick (your commit keeps your name and counts on your contributor graph), which is why GitHub marks this closed rather than merged. Thanks @nothariharan!

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

Labels

contributor Pull request from a returning community contributor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

install/uninstall still rewrite line endings of CLAUDE.md, AGENTS.md and related files (remaining paths after #3741)

2 participants