Skip to content

feat(rules): add preserveUnmanagedRuleContent to keep hand edits in root rule files - #4019

Merged
dyoshikawa merged 4 commits into
mainfrom
resolve-issue-4010-preserve-unmanaged-rule-content
Nov 29, 2026
Merged

dyoshikawa merged 4 commits into
mainfrom
resolve-issue-4010-preserve-unmanaged-rule-content

Conversation

@dyoshikawa

@dyoshikawa dyoshikawa commented Nov 29, 2026 •

Copy link
Copy Markdown
Owner

Summary

Adds an opt-in preserveUnmanagedRuleContent key to rulesync.jsonc. When it is on, every root rule file rulesync writes (AGENTS.md, CLAUDE.md, CLAUDE.local.md, ~/.codex/AGENTS.md, ~/.claude/CLAUDE.md, ZCode, Codebuff, ...) gets its generated body fenced between <!-- Generated by Rulesync --> and <!-- End of Rulesync -->, and content before and after the fence is kept on the next generate. Default output is unchanged.

What changed

  • src/features/rules/rule-managed-block.ts: splice and extract helpers for the Markdown fence. Generated frontmatter stays on the first line, ahead of the fence. An existing file's leading frontmatter is replaced only when the generated content has its own.

  • AiFile.composeWrittenContent(): a new hook the write phase uses to compute what goes on disk from the existing content and the generated content. ToolRule overrides it when preservation is on. Because generate --check / --dry-run go through the same writeAiFiles comparison, content outside the fence is never reported as drift.

  • RulesProcessor: new preserveUnmanagedContent option (wired from config in generate). It marks every root ToolRule it emits, which covers local-root files and root mirrors too. Per-rule files in rules directories (.claude/rules/*.md, .cursor/rules/*.mdc, ...) are not fenced.

  • Import (and the source side of convert): with the option on, when a rule file has both markers, only the fenced part goes into .rulesync/rules/. With it off, the whole file is imported. Hand-written content is not copied into the source, so the round trip does not duplicate it.

  • Config: preserveUnmanagedRuleContent (schema, Config, ConfigResolver). Config-file only, like preserveUnownedHooks, which has no CLI flag either.

  • Docs: configuration reference and a new section in the global-mode guide. Regenerated docs-content.ts.

  • Tests: unit tests for the splice/extract helpers, processor tests (fence, kept content, check drift, option off, import), a config-resolver test, and a global-mode e2e case (generate -> hand edit -> --check -> regenerate -> import).

  • Removal: when a root rule file stops being generated (--delete sweep, retireTargets), only the fenced block is removed and the hand-written rest is kept. The file is deleted only if nothing else is left. A root file without a fence is no longer treated as rulesync's and is left in place, including one written before the option was on, so it has to be removed by hand. Only root rule files get this treatment; per-rule files are deleted as before.

  • Marker lines inside Markdown code blocks are ignored. An existing CRLF file keeps CRLF line endings.

  • rulesync convert honors the option.

Design decisions (made autonomously; open to revisit)

  • Option 2 from the issue (fence + keep outside content), made opt-in. A default-on fence would change every user's output and need a major release, so this ships as a new key that defaults to off.
  • Name preserveUnmanagedRuleContent matches the preserveUnownedHooks family. No CLI flag, because the sibling has none and this is a property of the files, not of one run.
  • First run on a marker-less file: the existing content is treated as rulesync's (it is what rulesync wrote before the option was on), so only the fenced body is written. Keeping it would duplicate the generated content outside the fence. The docs say hand edits go outside the fence after the first fenced generate. A start marker without an end marker also counts as "no fence".
  • Import reads only the fenced part. This differs from the issue's suggestion of importing everything. Importing the whole file would put the hand-written content into the source, and the next generate would write it again inside the fence. Documented.
  • Scope: root rule files only (including local-root files and root mirrors), in both project and global mode.
  • The splice logic in src/lib/gitignore.ts is tied to gitignore entries and its legacy-block heuristics, so it was not shared. Only the marker wording was reused.

Skipped

  • No CLI flag (see above).
  • Frontmatter that rulesync wrote earlier but the new body no longer has is left as is when the generated content has no frontmatter, because it cannot be told apart from hand-written frontmatter.

Closes #4010

🤖 Generated with Claude Code

https://claude.ai/code/session_01JdNNKCxxrKAMDANMGDEGP4

dyoshikawa and others added 4 commits November 29, 2026 15:50
…oot rule files

With the opt-in preserveUnmanagedRuleContent config key, rulesync wraps the
generated body of every root rule file between <!-- Generated by Rulesync -->
and <!-- End of Rulesync --> and keeps content outside the fence on
generate. generate --check compares the spliced result, and import reads
only the fenced part. A file without markers is treated as rulesync-owned.

Closes #4010

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01JdNNKCxxrKAMDANMGDEGP4
- Ignore marker lines inside Markdown code blocks so a body quoting the
  markers stays stable across runs
- Strip to the fenced part on import only when the option is on
- On the --delete sweep and retireTargets, remove only the fenced block of a
  root rule file and keep the hand-written rest
- Pass the option to rulesync convert
- Keep CRLF line endings of an existing file when splicing

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01JdNNKCxxrKAMDANMGDEGP4
Under preserveUnmanagedRuleContent, a root rule file without a fence is no
longer rulesync's, so the --delete sweep and retireTargets keep it instead
of deleting the hand-written rest on the next run. Only root rule files are
fenced on removal; per-rule files are deleted as before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01JdNNKCxxrKAMDANMGDEGP4
…eserve-unmanaged-rule-content

# Conflicts:
#	docs/guide/configuration.md
#	src/config/config-resolver.ts
#	src/config/config.ts
#	src/generated/docs-content.ts
#	src/lib/generate.test.ts
@dyoshikawa
dyoshikawa merged commit fcfb10f into main Nov 29, 2026
10 checks passed
@dyoshikawa
dyoshikawa deleted the resolve-issue-4010-preserve-unmanaged-rule-content branch November 29, 2026 07:20
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.

Generate in global mode discards hand-written content in rule files (no managed block)

1 participant