Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(edit_page): receipt diff lists removed lines before added lines
garrytanbaeffff0d6cb
diff --git a/src/core/skillpack/diff-text.ts b/src/core/skillpack/diff-text.tsindex f94befe344..2316273a73 100644--- a/src/core/skillpack/diff-text.ts+++ b/src/core/skillpack/diff-text.ts@@ -189,6 +189,9 @@ interface DiffOp {/*** Line-level LCS-driven diff. Classic O(N*M) dynamic programming.+ * The backward walk takes additions first on ties, so once reversed every+ * change block lists its removed lines before its added lines, as `diff -u`+ * and `git diff` do.*/function diffLines(a: string[], b: string[]): DiffOp[] {const n = a.length;@@ -214,12 +217,12 @@ function diffLines(a: string[], b: string[]): DiffOp[] {ops.push({ kind: 'equal', line: a[i - 1] });i -= 1;j -= 1;- } else if (lcs[i - 1][j] >= lcs[i][j - 1]) {- ops.push({ kind: 'del', line: a[i - 1] });- i -= 1;- } else {+ } else if (lcs[i][j - 1] >= lcs[i - 1][j]) {ops.push({ kind: 'add', line: b[j - 1] });j -= 1;+ } else {+ ops.push({ kind: 'del', line: a[i - 1] });+ i -= 1;}}while (i > 0) {diff --git a/test/edit-page.test.ts b/test/edit-page.test.tsindex 51d021661e..c61721d12b 100644--- a/test/edit-page.test.ts+++ b/test/edit-page.test.ts@@ -18,7 +18,7 @@ import { disposePersistenceConsumer } from '../src/core/persistence/service.ts';import { _resetWriteThroughCacheForTest } from '../src/core/write-through.ts';import { renderFactsTable } from '../src/core/facts-fence.ts';import { renderTakesFence } from '../src/core/takes-fence.ts';-import { editableView } from '../src/core/persistence/page-edit.ts';+import { editDiff, editableView } from '../src/core/persistence/page-edit.ts';import { STARTER_OPS } from '../src/mcp/surface.ts';import { serializePageToMarkdown } from '../src/core/markdown.ts';import type { Page } from '../src/core/types.ts';@@ -100,6 +100,15 @@ describe('edit_page schema and catalog', () => {});});+describe('edit_page receipt diff', () => {+ bunTest('each change block lists removed lines before added lines', () => {+ const { diff } = editDiff('notes/order', 'Keep.\nOld one.\nOld two.\nKeep too.\nOld three.\n', 'Keep.\nNew one.\nNew two.\nKeep too.\nNew three.\n');+ expect(diff.split('\n').filter(l => /^[-+ ]/.test(l) && !/^(---|\+\+\+) /.test(l))).toEqual([+ ' Keep.', '-Old one.', '-Old two.', '+New one.', '+New two.', ' Keep too.', '-Old three.', '+New three.',+ ]);+ });+});+describe('edit_page matching', () => {test('a unique match publishes one new revision with a caller-view diff and write-through', async () => {await seed('notes/a', 'First line.\n\nSecond line.\n');@@ -111,6 +120,7 @@ describe('edit_page matching', () => {expect(result.body.revision).not.toBe(before.revision);expect(result.body.diff).toContain('-Second line.');expect(result.body.diff).toContain('+Second line, edited.');+ expect(result.body.diff.indexOf('-Second line.')).toBeLessThan(result.body.diff.indexOf('+Second line, edited.'));expect((await read('notes/a')).content).toBe(before.content.replace('Second line.', 'Second line, edited.'));expect(readFileSync(join(fixture, 'brain', 'notes/a.md'), 'utf8')).toContain('Second line, edited.');});