Skip to content

reset: stop replaceSection from deleting user content after the managed section#198

Open
selimdev00 wants to merge 2 commits into
LarsCowe:mainfrom
selimdev00:fix/replace-section-data-loss
Open

reset: stop replaceSection from deleting user content after the managed section#198
selimdev00 wants to merge 2 commits into
LarsCowe:mainfrom
selimdev00:fix/replace-section-data-loss

Conversation

@selimdev00

Copy link
Copy Markdown

Problem

replaceSection bounds the managed section by searching for the next ## heading:

const nextHeadingMatch = afterSection.match(new RegExp(`\\n## (?!${headingTextEscaped})`));
const after = nextHeadingMatch ? afterSection.slice(nextHeadingMatch.index ?? 0) : "";

If no following ## heading exists, after becomes "" - so everything from the marker to EOF is dropped. The managed ## BMAD-METHOD Integration block is appended at the end of the instructions file (CLAUDE.md / AGENTS.md), so any user content placed after it that isn't introduced by a ## heading is silently destroyed when the section is removed during bmalph reset:

  • a trailing top-level # heading and everything under it
  • plain paragraphs after the block

And because reset deletes the file entirely when the result trims to empty, a CLAUDE.md whose only ## block was BMAD could be removed outright along with that trailing content.

Fix

Bound the section by the next heading of the same or higher level (# or ##), skipping past the marker's own heading line first:

const afterMarkerLine = afterSection.slice(afterSection.indexOf("\n"));
const nextHeadingMatch = afterMarkerLine.match(/\n#{1,2} /);
  • A trailing # User Notes now terminates the section and is preserved (was deleted).
  • A following ## Section still terminates it (unchanged).
  • ### and deeper headings remain part of the section body - matching how the shipped BMAD snippet is structured (its internal subheadings are all ###), so the block is still treated as one unit.

Existing replaceSection behavior (replace between headings, remove section, section-at-EOF, marker-not-found) is unchanged; all consumers (reset, mergeInstructionsFile) pass.

Known limitation

Headerless prose placed directly after the managed block (no #/## heading at all) is still absorbed - there's no boundary token to detect it. Fully solving that needs explicit <!-- BMAD:START/END --> delimiters around the section (a format change with migration for existing installs), which I left as a separate follow-up. This PR removes the common and most damaging cases without that migration.

Tests

Added regression cases: trailing # heading preserved on removal; ### subheadings treated as body; marker's own heading not matched as the boundary. Full unit suite passes (1838).

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