Skip to content

Fix: Preserve user content when generating rules#6

Open
MS-Jahan wants to merge 1 commit into
Ogro-Projukti:mainfrom
MS-Jahan:fix/rules-append-mode
Open

Fix: Preserve user content when generating rules#6
MS-Jahan wants to merge 1 commit into
Ogro-Projukti:mainfrom
MS-Jahan:fix/rules-append-mode

Conversation

@MS-Jahan

Copy link
Copy Markdown

Fix: Preserve user content when generating rules

Problem

The codegenome rules command was destructively overwriting existing files, causing loss of user customizations:

  • .windsurfrules: Users' Windsurf IDE rules replaced completely
  • AGENTS.md: Important agent instructions lost
  • .github/copilot-instructions.md: Custom Copilot settings overwritten
  • .cursor/rules/codegenome-knowledge-graph.mdc: Cursor rules reset

This made the rules command unsafe to run more than once and prevented users from adding their own customizations alongside CodeGenome instructions.

Solution

Modified write_rule() in src/codegenome/rules.py to use section markers for non-destructive content management:

<!-- BEGIN CODEGENOME MCP INTEGRATION -->
[CodeGenome-managed content here]
<!-- END CODEGENOME MCP INTEGRATION -->

Behavior

  1. New files: Creates file with CodeGenome content wrapped in markers
  2. Existing files without section: Appends CodeGenome section with markers (preserves all existing content)
  3. Existing files with section: Replaces only the CodeGenome section (preserves user content before and after)

Example

Before (destructive):

# My Custom Rules
User content here

Running codegenome rules would replace everything with CodeGenome content.

After (non-destructive):

# My Custom Rules
User content here

<!-- BEGIN CODEGENOME MCP INTEGRATION -->
# CodeGenome Knowledge Graph (MCP)
[CodeGenome instructions]
<!-- END CODEGENOME MCP INTEGRATION -->

User content preserved! Running the command again only updates the CodeGenome section.

Benefits

Safe to run multiple times - updates don't destroy customizations
Users can add custom rules - above or below CodeGenome section
Backwards compatible - works with existing files
Automatic backup - creates .backup if file is unreadable

Testing

Added comprehensive test suite in tests/test_rules_append.py:

  • ✓ New file creation with markers
  • ✓ Appending to existing files (preserves user content)
  • ✓ Replacing existing CodeGenome section
  • ✓ Multiple updates to same file
  • ✓ Whitespace preservation

All tests pass ✓

Files Changed

  • src/codegenome/rules.py - Modified write_rule() function (42 lines added)
  • tests/test_rules_append.py - New test suite (134 lines)

Migration

No migration needed. Existing generated files will work as-is. Next time users run codegenome rules, it will append the section with markers instead of overwriting.

Problem:
- The rules generation function (generate_rules) was overwriting existing
  files completely, destroying user customizations in .windsurfrules,
  AGENTS.md, copilot-instructions.md, and other rule files.

Solution:
- Modified write_rule() to use section markers for content management
- Markers: <!-- BEGIN CODEGENOME MCP INTEGRATION --> and <!-- END -->
- New behavior:
  * New files: Create with markers wrapping CodeGenome content
  * Existing files without section: Append CodeGenome section with markers
  * Existing files with section: Replace only the CodeGenome section,
    preserving all user content before and after

Benefits:
- Users can add custom rules above/below CodeGenome section
- CodeGenome updates won't destroy customizations
- Non-destructive updates - safe to run multiple times
- Backup created if file is unreadable

Tests:
- Added test_rules_append.py with comprehensive test coverage
- Manual verification confirms all scenarios work correctly
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.

1 participant