Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix: the stale editor save check trims the same whitespace in SQL as in JavaScript

garrytan2faff78d6fd9
diff --git a/src/core/persistence/local-edit-adoption.ts b/src/core/persistence/local-edit-adoption.ts
index ac0b19bd38..9e9ca65f51 100644
--- a/src/core/persistence/local-edit-adoption.ts
+++ b/src/core/persistence/local-edit-adoption.ts
@@ -112,6 +112,9 @@ async function settled(path: string, quiet: number): Promise<boolean> {
}
}
+/** The whitespace JS `trim` strips; the SQL side trims the same set, so an earlier revision matches whatever its edges hold. */
+const EDGE_WHITESPACE = ' \t\n\v\f\r\u00a0\u1680\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u2028\u2029\u202f\u205f\u3000\ufeff';
+const edgeTrim = (text: string | null | undefined) => (text ?? '').trim();
/** Body lines that are Facts or timeline fence rows (list items and table rows, not a table's separator line). */
function fenceRows(...texts: Array<string | null | undefined>): Set<string> {
return new Set(texts.flatMap(text => (text ?? '').split('\n')).map(line => line.trim()).filter(line => /^(- |\|)/.test(line) && !/^\|[\s:|-]*\|?$/.test(line)));
@@ -124,12 +127,12 @@ function fenceRows(...texts: Array<string | null | undefined>): Set<string> {
async function staleEditorSave(engine: BrainEngine, slug: string, text: string, snapshot: PageSnapshot, activePack: unknown): Promise<boolean> {
assertInlineBody(snapshot.page, 'staleEditorSave');
const file = parseMarkdown(text, slug, { activePack } as never);
- const [compiled, timeline] = [(file.compiled_truth ?? '').trim(), (file.timeline ?? '').trim()];
- if (compiled !== (snapshot.page.compiled_truth ?? '').trim() || timeline !== (snapshot.page.timeline ?? '').trim()) {
+ const [compiled, timeline] = [edgeTrim(file.compiled_truth), edgeTrim(file.timeline)];
+ if (compiled !== edgeTrim(snapshot.page.compiled_truth) || timeline !== edgeTrim(snapshot.page.timeline)) {
const [earlier] = await engine.executeRaw<{ same: boolean }>(`SELECT EXISTS (SELECT 1 FROM (SELECT compiled_truth, timeline FROM page_versions
WHERE page_id=$1 AND compiled_truth IS NOT NULL ORDER BY id DESC LIMIT $2) v
- WHERE md5(btrim(v.compiled_truth, E' \\t\\r\\n'))=md5($3) AND md5(btrim(COALESCE(v.timeline, ''), E' \\t\\r\\n'))=md5($4)) AS same`,
- [snapshot.page.id, STALE_REVISIONS, compiled, timeline]);
+ WHERE md5(btrim(v.compiled_truth, $5))=md5($3) AND md5(btrim(COALESCE(v.timeline, ''), $5))=md5($4)) AS same`,
+ [snapshot.page.id, STALE_REVISIONS, compiled, timeline, EDGE_WHITESPACE]);
if (earlier?.same) return true;
}
const [previous] = await engine.executeRaw<{ compiled_truth: string; timeline: string | null }>(
diff --git a/test/adopt-local-edit.test.ts b/test/adopt-local-edit.test.ts
index 0777f0f747..9812a46104 100644
--- a/test/adopt-local-edit.test.ts
+++ b/test/adopt-local-edit.test.ts
@@ -613,3 +613,13 @@ test('#8755 o: an adoption whose caller names no claim token ends write_claim_lo
expect(await revision(f, 'one')).toBe(before.revision);
}), 180_000);
+test('#8755 p: a stale editor save matches an earlier revision whose edges hold whitespace beyond space, tab and newline', () => each(async f => {
+ const { localEditAdoptionSeams } = await import('../src/core/persistence/local-edit-adoption.ts');
+ let published = 0;
+ localEditAdoptionSeams.beforePublish = () => { published++; };
+ const EARLIER = 'An earlier body a stale editor buffer still held.';
+ await f.engine.executeRaw(`INSERT INTO page_versions (page_id, compiled_truth, timeline, frontmatter)
+ SELECT id, $1, timeline, frontmatter FROM pages WHERE source_id=$2 AND slug='one'`, [`\u00a0\ufeff${EARLIER}\u3000\u2028`, f.alpha]);
+ await expectRefused(f, page('Alpha one', EARLIER), timeline(f), 'add_timeline_entry');
+ expect(published).toBe(0);
+}), 180_000);