The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.
TypeScript agentai-chatai-studioall-in-onecoding-agentdesktop-appfluximage-generationlinuxllmlocal-ailocal-llmopen-sourceprivacyself-hostedstable-diffusionuncensoreduncensored-aivideo-generationwindows
fix(code): the merge stops resurrecting work the user deleted
David0663d305923d
diff --git a/src/lib/__tests__/three-way-merge.test.ts b/src/lib/__tests__/three-way-merge.test.tsindex 2e150125..54f00774 100644--- a/src/lib/__tests__/three-way-merge.test.ts+++ b/src/lib/__tests__/three-way-merge.test.ts@@ -97,3 +97,90 @@ describe('mergeThreeWay', () => {expect(res.ok).toBe(false)})})++// The merge was silently restoring deleted work. Found by the review of B4+// (2026-08-14) and reproduced against the real function before the fix:+//+// base console.log("debug") + a function+// disk the user deleted the debug line in their editor+// staged the model added an import at the top+// result ok:true, and the debug line was BACK+//+// Cause: collides() answered the conflict question, and the group builder used+// the same answer to decide membership. An insertion at exactly the first line+// of a replaced block does not conflict, so it was not grouped; it opened its+// own group whose span lay behind the write cursor, the cursor moved backwards,+// and the tail loop re-emitted base lines the other side had deleted. On a+// larger block the whole replaced block came back alongside its replacement.+describe('an insertion at the first line of a changed block', () => {+ const base = 'console.log("debug")\nexport function go() {\n return 1\n}\n'++ it('does not bring back the line the user deleted on disk', () => {+ const disk = 'export function go() {\n return 1\n}\n'+ const staged = 'import { go2 } from "./go2"\n' + base+ const r = mergeThreeWay(base, disk, staged)++ expect(r.ok).toBe(true)+ if (!r.ok) return+ expect(r.content).not.toContain('console.log("debug")')+ expect(r.content).toBe('import { go2 } from "./go2"\nexport function go() {\n return 1\n}\n')+ })++ it('lands above the block, not below it', () => {+ const disk = 'console.log("kept")\nexport function go() {\n return 1\n}\n'+ const staged = 'import { go2 } from "./go2"\n' + base+ const r = mergeThreeWay(base, disk, staged)++ expect(r.ok).toBe(true)+ if (!r.ok) return+ expect(r.content.split('\n')[0]).toBe('import { go2 } from "./go2"')+ expect(r.content).toContain('console.log("kept")')+ })++ it('does not duplicate a replaced block', () => {+ const lines = Array.from({ length: 40 }, (_, i) => `L${i}`)+ const base40 = lines.join('\n') + '\n'+ const d = [...lines]; d.splice(10, 5, 'DISK-A', 'DISK-B', 'DISK-C', 'DISK-D', 'DISK-E')+ const s = [...lines]; s.splice(10, 0, 'STAGED-1', 'STAGED-2')+ const r = mergeThreeWay(base40, d.join('\n') + '\n', s.join('\n') + '\n')++ expect(r.ok).toBe(true)+ if (!r.ok) return+ expect(r.content).toContain('DISK-A')+ expect(r.content).toContain('STAGED-1')+ expect(r.content).not.toMatch(/^L10$/m)+ expect(r.content.trimEnd().split('\n')).toHaveLength(42)+ })+})++// The property that matters on the apply path: whatever we write must still+// contain the user's disk state. Stripping the lines the staged side added has+// to give back exactly what was on disk. 6000 random pairs, the same shape the+// review fuzzed with.+describe('a merged write never resurrects disk content', () => {+ it('holds across random disk edits and staged insertions', () => {+ let rng = 1234567+ const rand = (n: number) => { rng = (rng * 1103515245 + 12345) & 0x7fffffff; return rng % n }+ let checked = 0++ for (let iter = 0; iter < 6000; iter++) {+ const len = 4 + rand(12)+ const baseArr = Array.from({ length: len }, (_, i) => `line${i}`)+ const at = rand(len)+ const take = rand(Math.min(4, len - at)) + 1+ const diskArr = [...baseArr]+ diskArr.splice(at, take, ...Array.from({ length: rand(3) }, (_, k) => `DISK${iter}_${k}`))+ const insAt = rand(len + 1)+ const marker = `STAGED${iter}`+ const stagedArr = [...baseArr]+ stagedArr.splice(insAt, 0, marker)++ const r = mergeThreeWay(baseArr.join('\n'), diskArr.join('\n'), stagedArr.join('\n'))+ if (!r.ok) continue+ checked+++ const withoutInsert = r.content.split('\n').filter((l) => l !== marker).join('\n')+ expect(withoutInsert, `iter ${iter}`).toBe(diskArr.join('\n'))+ }+ expect(checked).toBeGreaterThan(3000)+ })+})diff --git a/src/lib/three-way-merge.ts b/src/lib/three-way-merge.tsindex dad15033..ef68ec11 100644--- a/src/lib/three-way-merge.ts+++ b/src/lib/three-way-merge.ts@@ -83,6 +83,25 @@ function collides(a: Region, b: Region): boolean {return a.start < b.end && b.start < a.end}+/**+ * Does this region have to be EMITTED inside the group being built?+ *+ * A separate question from whether it conflicts, and conflating the two is what+ * broke the merge (review 2026-08-14). An insertion at exactly the first line+ * of a block the other side replaced does not conflict, so it was left out of+ * the group and opened its own one further down the loop. Its span then lay+ * BEHIND the write cursor: the cursor moved backwards, and the tail loop+ * re-emitted base lines the other side had deleted. The user's deleted line+ * came back, ok:true, with no conflict and no undo.+ *+ * Grouping is therefore inclusive at the edges: anything whose span touches the+ * group joins it, and the group decides afterwards whether the two sides+ * actually fight.+ */+function touches(a: Region, start: number, end: number): boolean {+ return a.start <= end && a.end >= start+}+/** The result of applying one side's regions across [start, end). */function rewrite(base: string[], regions: Region[], start: number, end: number): string[] {const out: string[] = []@@ -136,13 +155,13 @@ export function mergeThreeWay(base: string, ours: string, theirs: string): Merge// span, so a chain of edits over the same lines is judged as one unit.for (let grew = true; grew; ) {grew = false- while (oi < ourRegions.length && collides(ourRegions[oi], { start, end, lines: [] })) {+ while (oi < ourRegions.length && touches(ourRegions[oi], start, end)) {end = Math.max(end, ourRegions[oi].end)mine.push(ourRegions[oi])oi++grew = true}- while (ti < theirRegions.length && collides(theirRegions[ti], { start, end, lines: [] })) {+ while (ti < theirRegions.length && touches(theirRegions[ti], start, end)) {end = Math.max(end, theirRegions[ti].end)yours.push(theirRegions[ti])ti++@@ -156,6 +175,16 @@ export function mergeThreeWay(base: string, ours: string, theirs: string): Mergeconst only = mine.length === 0 ? yours : minefor (const line of rewrite(baseLines, only, start, end)) out.push(line)if (mine.length > 0) mergedRegions+++ } else if (!mine.some((m) => yours.some((y) => collides(m, y)))) {+ // Both sides edited inside this span but never the same base lines: an+ // insertion sitting at the first line of a replaced block is the common+ // one. Take both, in base order, insertion first where the anchor is+ // shared, so the inserted lines land above the block and not below it.+ const all = [...mine, ...yours].sort(+ (x, y) => x.start - y.start || (x.end - x.start) - (y.end - y.start),+ )+ for (const line of rewrite(baseLines, all, start, end)) out.push(line)+ mergedRegions++} else {const a = rewrite(baseLines, mine, start, end)const b = rewrite(baseLines, yours, start, end)@@ -166,7 +195,9 @@ export function mergeThreeWay(base: string, ours: string, theirs: string): Mergeconflicts++}}- cursor = end+ // Never backwards. A group that ends before the cursor would let the tail+ // loop below re-emit base lines that were already resolved.+ cursor = Math.max(cursor, end)}if (conflicts > 0) return { ok: false, conflicts }