Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(sync): #6712 PR 4b a database-only page at a new file slug is guarded as unknown-side drift, never overwritten

garrytan5bdd5ab34138
diff --git a/src/core/persistence/sync-pass-run.ts b/src/core/persistence/sync-pass-run.ts
index 6fb8236056..97a1760e89 100644
--- a/src/core/persistence/sync-pass-run.ts
+++ b/src/core/persistence/sync-pass-run.ts
@@ -164,8 +164,12 @@ async function batchEntries(engine: BrainEngine, level: ShadowLevel, deletes: Sh
const paths = entryPaths(level, winner.path);
const base = { ...paths, action: 'import' as const, working: working(winner), level: winner.state.level, slug, ...legacySlugs.get(winner.path) };
if (holder) {
+ // A live page at the slug with no recorded origin (written straight to the database, or by an older gbrain) attests
+ // neither side: the import is guarded as drift of unknown side, so the freeze imports it only as a proven no-op and
+ // otherwise holds file_drift (as discovery's tree walk guards it, sync-prescreen.ts).
entries.push({ ...base, pageId: Number(holder.id), revision: holder.revision,
- ...(holder.source_path !== null ? { renameFrom: { sourcePath: holder.source_path, slug, pageId: Number(holder.id), revision: holder.revision } } : {}) });
+ ...(holder.source_path !== null ? { renameFrom: { sourcePath: holder.source_path, slug, pageId: Number(holder.id), revision: holder.revision } } : {}),
+ ...(holder.source_path === null && !holder.deleted ? { drift: 'unknown' as const } : {}) });
if (holderPath !== null && deleteEntries.delete(holderPath)) renamed.add(holderPath);
continue;
}
diff --git a/test/sync-pass-live-6712.test.ts b/test/sync-pass-live-6712.test.ts
index 25b5781e52..6c4e36215c 100644
--- a/test/sync-pass-live-6712.test.ts
+++ b/test/sync-pass-live-6712.test.ts
@@ -21,6 +21,7 @@
* held legacy_slug_conflict and forks no second page.
* - a committed edit under a different uncommitted edit (committed mode) is held worktree_dirty, the page and the local
* file untouched, and the next pass settles instead of counting a not_converged streak; sync.holds=fail stops the run.
+ * - a page written straight to the database at a new file's slug is never overwritten by the file: held file_drift (side unknown).
* - probe 54: a pass intent keeps what the #6862 FIFO guard reads: a `managed_sync_` kind, the slug and page, and `renameFrom.slug`.
*
* Each test fails on capy/6712-pr4b (8cda2764b), where a non-company source still runs the cursor engine: the fences do not
@@ -36,7 +37,7 @@ import { SYNC_ADMISSION_KEY } from '../src/core/persistence/sync-admission.ts';
import { performManagedSync } from '../src/core/persistence/sync-run.ts';
import { RECOVERY_VERSION } from '../src/core/markdown.ts';
import { readGitHold, SYNC_IMPORT_PROVENANCE_OP, writeGitHold } from '../src/core/persistence/sync-holds.ts';
-import { preparePageAdmission } from '../src/core/persistence/page-mutations.ts';
+import { preparePageAdmission, submitPageMutation } from '../src/core/persistence/page-mutations.ts';
import { admitWrite } from '../src/core/persistence/journal.ts';
import { createHash, randomUUID } from 'node:crypto';
import { drainNext, runDrain } from '../src/core/persistence/sync-drain.ts';
@@ -287,6 +288,21 @@ describe('#6712 PR 4b live pass fences (pglite)', () => {
expect(readFileSync(join(root, 'notes/a.md'), 'utf8')).toContain('An uncommitted draft of notes/a.');
}, { setup }), 180_000);
+ test('a database-only page at a new file\'s slug is held file_drift and never overwritten', () => managedBrain(async ({ engine, ctx, root }) => {
+ expect((await performManagedSync(engine, OPTS)).status).toBe('first_sync');
+ await engine.setConfig('sync.write_through', 'false');
+ for (const [slug, body] of [['notes/d', 'A database-only page.'], ['notes/e', 'The same body.']] as const)
+ await submitPageMutation(ctx, { operation: 'put_page', params: { slug, request_id: randomUUID(), content: note(slug, body) } });
+ await waitFor(async () => (await engine.executeRaw("SELECT 1 FROM persistence_requests WHERE state<>'committed'")).length === 0, { timeoutMs: 20_000, label: 'the put_pages commit' });
+ writeFileSync(join(root, 'notes/d.md'), note('notes/d', 'A committed file at the same slug.'));
+ writeFileSync(join(root, 'notes/e.md'), note('notes/e', 'The same body.')); commit(root, 'files at database-only slugs');
+ const result = await performManagedSync(engine, OPTS);
+ // Neither page records an origin, so neither side is attested: both are held (an equal body is not proof the file wrote it).
+ expect((result.held ?? []).map(hold => [hold.path, hold.code])).toEqual([['notes/d.md', 'file_drift'], ['notes/e.md', 'file_drift']]);
+ expect((await engine.readPageSnapshot('notes/d', { sourceId: 'default' }))!.page.compiled_truth).toContain('A database-only page.');
+ expect((await engine.executeRaw<{ source_path: string | null }>("SELECT source_path FROM pages WHERE source_id='default' AND slug IN ('notes/d','notes/e') ORDER BY slug")).map(row => row.source_path)).toEqual([null, null]);
+ }, { setup }), 180_000);
+
test('probe 54: a pass intent keeps what the #6862 FIFO guard reads, renameFrom included', () => managedBrain(async ({ engine, root }) => {
expect((await performManagedSync(engine, OPTS)).status).toBe('first_sync');
git(root, 'mv', 'notes/b.md', 'notes/renamed.md'); commit(root, 'rename b');