markup: Settle keeps the current revision's bytes (level-up D13)
Merge3 records where each settled block came from (theirs or mine, and its index there) and keeps the three bodies, so Settle can emit theirs' blocks and the seams between theirs' neighbours as they are, mine's blocks with their own source and seams, and Join only where the sides meet. The result is parsed again and falls back to Join if it would not give the settled blocks. Hunk.Sources gives a conflict's three sides as exact bytes, for the autosave reply's hunks. Base build note 23 revised.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
4 files changed,
+384
−61
docs/superpowers/specs/2026-11-12-mapa-design.md+10 −3internal/mapa/markup/merge.go+76 −58internal/mapa/markup/settle.go+172 −0internal/mapa/markup/settle_test.go+126 −0
diff --git a/docs/superpowers/specs/2026-11-12-mapa-design.md b/docs/superpowers/specs/2026-11-12-mapa-design.md| index ee978a9..4af1dbc 100644 |
| --- a/docs/superpowers/specs/2026-11-12-mapa-design.md |
| +++ b/docs/superpowers/specs/2026-11-12-mapa-design.md |
| @@ -892,9 +892,16 @@ this list together. |
| 23. **The block merge refines diff3.** Where both sides kept the same |
| number of blocks in a changed stretch (edits in place), each block is |
| resolved on its own, so Ana's edit to one block and Bo's to the next |
| - do not conflict for being neighbours. A merged body is re-joined with |
| - one blank line between blocks (a tight list stays tight), so a merged |
| - save can tidy blank lines. A save that needs no merge keeps every byte. |
| + do not conflict for being neighbours. A save that needs no merge |
| + keeps every byte. *Revised by the level-up (§6.4, D13):* a merged |
| + body keeps the current revision's bytes too. Its blocks, and the |
| + bytes between two of its blocks that are still neighbours, are |
| + emitted as they are; blocks taken from the saver's text bring their |
| + own source and seams; only a seam where the two sides meet is |
| + re-joined with one blank line (a tight list stays tight). If the |
| + result would not parse back to the settled blocks, the whole body is |
| + re-joined that way instead. Resolve's chosen text settles the same |
| + way. |
| 24. **A conflict offers Yours, Theirs, or Both (yours first)** for each |
| block both people changed. With no merge base, it is Yours or Theirs |
| whole. Choosing only what is saved now discards the draft and commits |
diff --git a/internal/mapa/markup/merge.go b/internal/mapa/markup/merge.go| index e7a7d0e..b3f4d4a 100644 |
| --- a/internal/mapa/markup/merge.go |
| +++ b/internal/mapa/markup/merge.go |
| @@ -8,31 +8,59 @@ package markup |
| // Hunk is one stretch of the merged body: settled blocks, or a conflict |
| // with both sides and what they started from. |
| type Hunk struct { |
| - Blocks []Block |
| - Conflict bool |
| - Base []Block |
| - Mine []Block |
| - Theirs []Block |
| + Blocks []Block |
| + // From and At place a settled hunk's blocks: the side they were |
| + // taken from, and the index of the first of them in that side. |
| + From Side |
| + At int |
| + // Conflict marks a hunk both sides changed. Base, Mine and Theirs |
| + // are its three sides, starting at BaseAt, MineAt and TheirAt. |
| + Conflict bool |
| + Base, Mine, Theirs []Block |
| + BaseAt, MineAt, TheirAt int |
| + // texts are the three bodies the hunk came from, shared by every |
| + // hunk of one merge, so Settle can keep their bytes (level-up §6.4). |
| + texts *sides |
| } |
| +// Side is which body a settled block was taken from. |
| +type Side int |
| + |
| +// The two sides a block can come from. A block both sides kept is |
| +// theirs: the current revision's bytes are the anchor. |
| +const ( |
| + FromTheirs Side = iota + 1 |
| + FromMine |
| +) |
| + |
| +// sides is one merge's three bodies, normalised, and their blocks. |
| +type sides struct { |
| + base, mine, theirs string |
| + baseBlocks, mineBlocks, theirsBlocks []Block |
| +} |
| + |
| +// cursor is a position in each of the three bodies' blocks. |
| +type cursor struct{ base, mine, theirs int } |
| + |
| // Merge3 merges mine and theirs, both edited from base. clean reports |
| // that no hunk is a conflict. |
| func Merge3(base, mine, theirs string) (hunks []Hunk, clean bool) { |
| - baseBlocks, mineBlocks, theirBlocks := Parse(base), Parse(mine), |
| - Parse(theirs) |
| - toMine := align(baseBlocks, mineBlocks) |
| - toTheirs := align(baseBlocks, theirBlocks) |
| + texts := &sides{base: normalise(base), mine: normalise(mine), |
| + theirs: normalise(theirs)} |
| + texts.baseBlocks, texts.mineBlocks, texts.theirsBlocks = |
| + Parse(texts.base), Parse(texts.mine), Parse(texts.theirs) |
| + toMine := align(texts.baseBlocks, texts.mineBlocks) |
| + toTheirs := align(texts.baseBlocks, texts.theirsBlocks) |
| clean = true |
| - at, mineAt, theirAt := 0, 0, 0 |
| + at := cursor{} |
| for { |
| - stable := nextStable(at, toMine, toTheirs) |
| - end, mineEnd, theirEnd := len(baseBlocks), len(mineBlocks), |
| - len(theirBlocks) |
| + stable := nextStable(at.base, toMine, toTheirs) |
| + end := cursor{len(texts.baseBlocks), len(texts.mineBlocks), |
| + len(texts.theirsBlocks)} |
| if stable >= 0 { |
| - end, mineEnd, theirEnd = stable, toMine[stable], toTheirs[stable] |
| + end = cursor{stable, toMine[stable], toTheirs[stable]} |
| } |
| - for _, hunk := range resolveStretch(baseBlocks[at:end], |
| - mineBlocks[mineAt:mineEnd], theirBlocks[theirAt:theirEnd]) { |
| + for _, hunk := range texts.resolveStretch(at, end) { |
| if hunk.Conflict || len(hunk.Blocks) > 0 { |
| hunks = append(hunks, hunk) |
| clean = clean && !hunk.Conflict |
| @@ -41,8 +69,8 @@ func Merge3(base, mine, theirs string) (hunks []Hunk, clean bool) { |
| if stable < 0 { |
| return hunks, clean |
| } |
| - hunks = append(hunks, Hunk{Blocks: baseBlocks[stable : stable+1]}) |
| - at, mineAt, theirAt = stable+1, toMine[stable]+1, toTheirs[stable]+1 |
| + hunks = append(hunks, texts.settled(FromTheirs, toTheirs[stable], 1)) |
| + at = cursor{stable + 1, toMine[stable] + 1, toTheirs[stable] + 1} |
| } |
| } |
| @@ -61,13 +89,17 @@ func nextStable(from int, toMine, toTheirs []int) int { |
| // hold the same number of blocks, each was edited in place, so each |
| // position is resolved on its own: Ana's edit to one block and Bo's to |
| // the next do not conflict just because they are neighbours. |
| -func resolveStretch(base, mine, theirs []Block) []Hunk { |
| - if len(base) < 2 || len(mine) != len(base) || len(theirs) != len(base) { |
| - return []Hunk{resolve(base, mine, theirs)} |
| +func (texts *sides) resolveStretch(at, end cursor) []Hunk { |
| + count := end.base - at.base |
| + if count < 2 || end.mine-at.mine != count || |
| + end.theirs-at.theirs != count { |
| + return []Hunk{texts.resolve(at, end)} |
| } |
| - hunks := make([]Hunk, len(base)) |
| - for at := range base { |
| - hunks[at] = resolve(base[at:at+1], mine[at:at+1], theirs[at:at+1]) |
| + hunks := make([]Hunk, count) |
| + for step := range count { |
| + from := cursor{at.base + step, at.mine + step, at.theirs + step} |
| + hunks[step] = texts.resolve(from, |
| + cursor{from.base + 1, from.mine + 1, from.theirs + 1}) |
| } |
| return hunks |
| } |
| @@ -75,14 +107,28 @@ func resolveStretch(base, mine, theirs []Block) []Hunk { |
| // resolve is diff3's rule for one unstable stretch: a side that did not |
| // change it yields to the side that did; two identical changes are one; |
| // anything else is a conflict. |
| -func resolve(base, mine, theirs []Block) Hunk { |
| +func (texts *sides) resolve(at, end cursor) Hunk { |
| + base := texts.baseBlocks[at.base:end.base] |
| + mine := texts.mineBlocks[at.mine:end.mine] |
| + theirs := texts.theirsBlocks[at.theirs:end.theirs] |
| switch { |
| - case sameBlocks(mine, base): |
| - return Hunk{Blocks: theirs} |
| - case sameBlocks(theirs, base), sameBlocks(mine, theirs): |
| - return Hunk{Blocks: mine} |
| + case sameBlocks(mine, base), sameBlocks(mine, theirs): |
| + return texts.settled(FromTheirs, at.theirs, len(theirs)) |
| + case sameBlocks(theirs, base): |
| + return texts.settled(FromMine, at.mine, len(mine)) |
| } |
| - return Hunk{Conflict: true, Base: base, Mine: mine, Theirs: theirs} |
| + return Hunk{Conflict: true, Base: base, Mine: mine, Theirs: theirs, |
| + BaseAt: at.base, MineAt: at.mine, TheirAt: at.theirs, texts: texts} |
| +} |
| + |
| +// settled is a hunk of count blocks taken from one side, from at. |
| +func (texts *sides) settled(from Side, at, count int) Hunk { |
| + blocks := texts.theirsBlocks |
| + if from == FromMine { |
| + blocks = texts.mineBlocks |
| + } |
| + return Hunk{Blocks: blocks[at : at+count], From: from, At: at, |
| + texts: texts} |
| } |
| func sameBlocks(one, other []Block) bool { |
| @@ -142,31 +188,3 @@ const ( |
| KeepTheirs |
| KeepBoth |
| ) |
| - |
| -// Settle joins the hunks into a body, taking each conflict's choice in |
| -// order. It reports false if any conflict has no valid choice. |
| -func Settle(hunks []Hunk, choices []Choice) (string, bool) { |
| - var blocks []Block |
| - next := 0 |
| - for _, hunk := range hunks { |
| - if !hunk.Conflict { |
| - blocks = append(blocks, hunk.Blocks...) |
| - continue |
| - } |
| - if next >= len(choices) { |
| - return "", false |
| - } |
| - switch choices[next] { |
| - case KeepMine: |
| - blocks = append(blocks, hunk.Mine...) |
| - case KeepTheirs: |
| - blocks = append(blocks, hunk.Theirs...) |
| - case KeepBoth: |
| - blocks = append(append(blocks, hunk.Mine...), hunk.Theirs...) |
| - default: |
| - return "", false |
| - } |
| - next++ |
| - } |
| - return Join(blocks), next == len(choices) |
| -} |
diff --git a/internal/mapa/markup/settle.go b/internal/mapa/markup/settle.go| new file mode 100644 |
| index 0000000..4311267 |
| --- /dev/null |
| +++ b/internal/mapa/markup/settle.go |
| @@ -0,0 +1,172 @@ |
| +package markup |
| + |
| +import "strings" |
| + |
| +// Settling a merge keeps bytes (level-up §6.4, D13). The current |
| +// revision ("theirs") is the anchor: its blocks, and the bytes between |
| +// two of its blocks that are still neighbours, are emitted as they are. |
| +// A block taken from mine brings its own source, and the bytes between |
| +// two of mine's neighbours come with them. Only a seam where the two |
| +// sides meet follows Join's rule. The result is parsed again: if it does |
| +// not give the settled blocks, Settle falls back to Join, which is |
| +// correct but tidies spacing. |
| + |
| +// placed is one settled block and its place in the side it came from. |
| +type placed struct { |
| + block Block |
| + from Side |
| + at int |
| +} |
| + |
| +// Settle joins the hunks into a body, taking each conflict's choice in |
| +// order. It reports false if any conflict has no valid choice. |
| +func Settle(hunks []Hunk, choices []Choice) (string, bool) { |
| + var pieces []placed |
| + var texts *sides |
| + next := 0 |
| + for _, hunk := range hunks { |
| + texts = firstSides(texts, hunk.texts) |
| + if !hunk.Conflict { |
| + pieces = appendPieces(pieces, hunk.Blocks, hunk.From, hunk.At) |
| + continue |
| + } |
| + if next >= len(choices) { |
| + return "", false |
| + } |
| + chosen, ok := hunk.chosen(pieces, choices[next]) |
| + if !ok { |
| + return "", false |
| + } |
| + pieces = chosen |
| + next++ |
| + } |
| + return texts.join(pieces), next == len(choices) |
| +} |
| + |
| +func firstSides(found, candidate *sides) *sides { |
| + if found != nil { |
| + return found |
| + } |
| + return candidate |
| +} |
| + |
| +// chosen is pieces with a conflict's chosen side appended. |
| +func (hunk Hunk) chosen(pieces []placed, choice Choice) ([]placed, bool) { |
| + switch choice { |
| + case KeepMine: |
| + return appendPieces(pieces, hunk.Mine, FromMine, hunk.MineAt), true |
| + case KeepTheirs: |
| + return appendPieces(pieces, hunk.Theirs, FromTheirs, |
| + hunk.TheirAt), true |
| + case KeepBoth: |
| + pieces = appendPieces(pieces, hunk.Mine, FromMine, hunk.MineAt) |
| + return appendPieces(pieces, hunk.Theirs, FromTheirs, |
| + hunk.TheirAt), true |
| + } |
| + return pieces, false |
| +} |
| + |
| +func appendPieces(pieces []placed, blocks []Block, from Side, |
| + at int) []placed { |
| + for step, block := range blocks { |
| + pieces = append(pieces, placed{block: block, from: from, |
| + at: at + step}) |
| + } |
| + return pieces |
| +} |
| + |
| +// join is the settled body: the sides' own bytes where they hold, and |
| +// Join where the result would not parse back to the settled blocks. |
| +func (texts *sides) join(pieces []placed) string { |
| + blocks := make([]Block, len(pieces)) |
| + for at, settled := range pieces { |
| + blocks[at] = settled.block |
| + } |
| + if texts == nil || len(pieces) == 0 { |
| + return Join(blocks) |
| + } |
| + kept := texts.keep(pieces) |
| + if sameStructure(Parse(kept), blocks) { |
| + return kept |
| + } |
| + return Join(blocks) |
| +} |
| + |
| +// keep writes the pieces with their sides' bytes between neighbours. |
| +func (texts *sides) keep(pieces []placed) string { |
| + var out strings.Builder |
| + if first := pieces[0]; first.at == 0 { |
| + out.WriteString(texts.text(first.from)[:first.block.Start]) |
| + } |
| + for at, settled := range pieces { |
| + if at > 0 { |
| + out.WriteString(texts.seam(pieces[at-1], settled)) |
| + } |
| + out.WriteString(settled.block.Source) |
| + } |
| + return out.String() |
| +} |
| + |
| +// seam is the bytes between two settled blocks: their side's own, when |
| +// they were neighbours there, and Join's rule anywhere else. |
| +func (texts *sides) seam(before, after placed) string { |
| + if before.from == after.from && after.at == before.at+1 { |
| + blocks := texts.blocks(before.from) |
| + start, end := blocks[before.at].End, blocks[after.at].Start |
| + return texts.text(before.from)[start:end] |
| + } |
| + if before.block.Kind == Item && after.block.Kind == Item { |
| + return "\n" |
| + } |
| + return "\n\n" |
| +} |
| + |
| +func (texts *sides) text(from Side) string { |
| + if from == FromMine { |
| + return texts.mine |
| + } |
| + return texts.theirs |
| +} |
| + |
| +func (texts *sides) blocks(from Side) []Block { |
| + if from == FromMine { |
| + return texts.mineBlocks |
| + } |
| + return texts.theirsBlocks |
| +} |
| + |
| +// sameStructure reports whether parsed is blocks: the same number, and |
| +// each of the same kind, level and source. |
| +func sameStructure(parsed, blocks []Block) bool { |
| + if len(parsed) != len(blocks) { |
| + return false |
| + } |
| + for at := range parsed { |
| + one, other := parsed[at], blocks[at] |
| + if one.Kind != other.Kind || one.Level != other.Level || |
| + one.Ordered != other.Ordered || one.Source != other.Source { |
| + return false |
| + } |
| + } |
| + return true |
| +} |
| + |
| +// Sources is a conflict's three sides as Markdown, each the exact bytes |
| +// of its body from the hunk's first block to its last ("" for a side |
| +// with no blocks). The editor's inline choice shows them (§6.5). |
| +func (hunk Hunk) Sources() (base, mine, theirs string) { |
| + if hunk.texts == nil { |
| + return Join(hunk.Base), Join(hunk.Mine), Join(hunk.Theirs) |
| + } |
| + texts := hunk.texts |
| + return span(texts.base, hunk.Base), span(texts.mine, hunk.Mine), |
| + span(texts.theirs, hunk.Theirs) |
| +} |
| + |
| +// span is the bytes of text that blocks, a run of its own, cover. |
| +func span(text string, blocks []Block) string { |
| + if len(blocks) == 0 { |
| + return "" |
| + } |
| + return text[blocks[0].Start:blocks[len(blocks)-1].End] |
| +} |
diff --git a/internal/mapa/markup/settle_test.go b/internal/mapa/markup/settle_test.go| new file mode 100644 |
| index 0000000..6390cc8 |
| --- /dev/null |
| +++ b/internal/mapa/markup/settle_test.go |
| @@ -0,0 +1,126 @@ |
| +package markup |
| + |
| +import ( |
| + "strings" |
| + "testing" |
| +) |
| + |
| +// Level-up §6.4, D13: a merged body changes bytes only in the blocks |
| +// mine changed and at their seams. Everything else is theirs, byte for |
| +// byte. |
| + |
| +const spacedBase = "# Plan\n\nOne.\n\nTwo.\n\nThree.\n\nFour." |
| + |
| +func mergedClean(t *testing.T, base, mine, theirs string) string { |
| + t.Helper() |
| + hunks, clean := Merge3(base, mine, theirs) |
| + if !clean { |
| + t.Fatalf("a conflict merging\n%q\n%q", mine, theirs) |
| + } |
| + body, ok := Settle(hunks, nil) |
| + if !ok { |
| + t.Fatal("a clean merge did not settle") |
| + } |
| + return body |
| +} |
| + |
| +func TestSettleKeepsTheirSpacingOutsideMineEdits(t *testing.T) { |
| + theirs := "\n# Plan\n\n\n\nOne.\n\n \nTwo.\n\nThree, theirs." + |
| + "\n\n\t\n\nFour." |
| + mine := "# Plan\n\nOne.\n\nTwo.\n\nThree.\n\nFour, mine." |
| + got := mergedClean(t, spacedBase, mine, theirs) |
| + want := "\n# Plan\n\n\n\nOne.\n\n \nTwo.\n\nThree, theirs." + |
| + "\n\nFour, mine." |
| + if got != want { |
| + t.Fatalf("got %q\nwant %q", got, want) |
| + } |
| +} |
| + |
| +func TestSettleKeepsTheirBytesWhenMineChangedNothing(t *testing.T) { |
| + theirs := "# Plan\n\n\n\nOne, theirs.\n\nTwo.\n\nThree.\n\n\nFour." |
| + if got := mergedClean(t, spacedBase, spacedBase, theirs); got != theirs { |
| + t.Fatalf("got %q", got) |
| + } |
| +} |
| + |
| +func TestSettleKeepsMineOwnSeamsBetweenMineBlocks(t *testing.T) { |
| + mine := "# Plan\n\nOne.\n\nNew A.\n\n\n\nNew B.\n\nTwo.\n\nThree." + |
| + "\n\nFour." |
| + theirs := "# Plan\n\n\nOne.\n\nTwo.\n\nThree.\n\n\n\nFour, theirs." |
| + got := mergedClean(t, spacedBase, mine, theirs) |
| + want := "# Plan\n\n\nOne.\n\nNew A.\n\n\n\nNew B.\n\nTwo.\n\nThree." + |
| + "\n\n\n\nFour, theirs." |
| + if got != want { |
| + t.Fatalf("got %q\nwant %q", got, want) |
| + } |
| +} |
| + |
| +func TestSettleKeepsATightListTight(t *testing.T) { |
| + base := "Intro.\n\n- a\n- b\n- c" |
| + mine := "Intro.\n\n- a\n- b, mine\n- c" |
| + theirs := "Intro, theirs.\n\n\n- a\n- b\n- c" |
| + got := mergedClean(t, base, mine, theirs) |
| + if got != "Intro, theirs.\n\n\n- a\n- b, mine\n- c" { |
| + t.Fatalf("got %q", got) |
| + } |
| +} |
| + |
| +// Only the bytes inside the blocks mine changed, and the seams beside |
| +// them, differ from theirs. |
| +func TestSettleChangesOnlyMineBlocksAndTheirSeams(t *testing.T) { |
| + theirs := "# Plan\n\n\n\nOne.\n\n \nTwo.\n\nThree.\n\n\n\nFour." |
| + mine := "# Plan\n\nOne.\n\nTwo, mine.\n\nThree.\n\nFour." |
| + got := mergedClean(t, spacedBase, mine, theirs) |
| + head, tail := "# Plan\n\n\n\nOne.", "Three.\n\n\n\nFour." |
| + if !strings.HasPrefix(got, head) || !strings.HasSuffix(got, tail) || |
| + !strings.Contains(got, "Two, mine.") { |
| + t.Fatalf("got %q", got) |
| + } |
| +} |
| + |
| +// The structure check: a result that would not parse back to the |
| +// settled blocks falls back to Join. |
| +func TestSettleFallsBackToJoinWhenTheBytesWouldNotParse(t *testing.T) { |
| + base := "A.\n\nB." |
| + mine := "A.\n\n```\nmine code" |
| + theirs := "A.\n\nB, theirs." |
| + hunks, clean := Merge3(base, mine, theirs) |
| + if clean { |
| + t.Fatal("two edits to one block merged silently") |
| + } |
| + got, ok := Settle(hunks, []Choice{KeepBoth}) |
| + if !ok { |
| + t.Fatal("a chosen conflict did not settle") |
| + } |
| + want := Join([]Block{{Kind: Paragraph, Source: "A."}, |
| + {Kind: Code, Source: "```\nmine code"}, |
| + {Kind: Paragraph, Source: "B, theirs."}}) |
| + if got != want { |
| + t.Fatalf("got %q, want %q", got, want) |
| + } |
| + placedBlocks := Parse(mine) |
| + if sameStructure(Parse(got), append(placedBlocks, |
| + Block{Kind: Paragraph, Source: "B, theirs."})) { |
| + t.Fatal("the vacuous case: the joined body parses as settled") |
| + } |
| +} |
| + |
| +func TestAConflictsSourcesAreItsExactBytes(t *testing.T) { |
| + base := "One.\n\nTwo.\n\nThree." |
| + mine := "One.\n\nTwo,\nmine. \n\nThree." |
| + theirs := "One.\n\nTwo, theirs.\n\nThree." |
| + hunks, _ := Merge3(base, mine, theirs) |
| + for _, hunk := range hunks { |
| + if !hunk.Conflict { |
| + continue |
| + } |
| + was, ours, others := hunk.Sources() |
| + if was != "Two." || ours != "Two,\nmine. " || |
| + others != "Two, theirs." || hunk.MineAt != 1 { |
| + t.Fatalf("sources %q %q %q at %d", was, ours, others, |
| + hunk.MineAt) |
| + } |
| + return |
| + } |
| + t.Fatal("no conflict") |
| +} |