fix(merge): preserve repeated H2 headings instead of dropping all but the last - #7
Open
JeePeeTee wants to merge 1 commit into
Open
fix(merge): preserve repeated H2 headings instead of dropping all but the last#7JeePeeTee wants to merge 1 commit into
JeePeeTee wants to merge 1 commit into
Conversation
… the last blocksByKey builds a map keyed on the normalized heading, so a MEMORY.md that repeats an H2 -- or has two that differ only in case, since keys are lowercased -- collapsed them into one map entry. Merge then emits one block per key, so every section but the last was silently discarded. This fires even when all three inputs are byte-identical: merging a file with two "## Notes" sections against itself loses the first one's body. It reaches users through both callers, the claude-memmerge driver and Reconcile. Parse now disambiguates repeated headings by occurrence, so the Nth "## Notes" in ours matches the Nth in theirs. The first occurrence keeps its plain key, so cross-document matching is unchanged and "## Deploy" on one machine still matches "## deploy" on another. The separator is a NUL byte, which cannot occur in markdown, so a disambiguated key can never collide with a real heading's; keys are internal to the merge and never appear in the output. Four tests cover it: distinct keys at parse time, both sections surviving an identical-input merge, case-variant headings in one file, and edits to different occurrences merging cleanly rather than one erasing the other.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
blocksByKeybuilds a map keyed on the normalized heading, so aMEMORY.mdthat repeats an H2 — or has two that differ only in case, since keys are lowercased — collapses them into a single map entry.Mergethen emits one block per key, so every section but the last is silently discarded.This fires even when all three inputs are byte-identical. Merging a file that has two
## Notessections against itself loses the first one's body. It reaches users through both callers: theclaude-memmergedriver andReconcile.Solution
Parsedisambiguates repeated headings by occurrence, so the Nth## Notesin ours matches the Nth in theirs. The first occurrence keeps its plain key, so cross-document matching is unchanged and## Deployon one machine still matches## deployon another.The separator is a NUL byte, which cannot occur in markdown, so a disambiguated key can never collide with a real heading's. Keys are internal to the merge algorithm and never appear in the output — only
Block.Headingis written.Verification
go build ./... && go vet ./... && gofmt -l . && go test ./...all clean.Four new tests, each confirmed to fail when the fix is reverted:
got 1 in "# T\n\n## Notes\nsecond\n"before the fix)Found while reviewing the codebase; happy to adjust the approach if you'd prefer duplicates concatenated rather than kept separate.