Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(#6712 PR 4b): the pass confirms dirty-tree renames (identityHead) and vetoes takeovers and held renames whose explicit id differs (#6729)
garrytan0c5fa5902444
diff --git a/src/core/persistence/sync-discovery.ts b/src/core/persistence/sync-discovery.tsindex 7c1a3de846..b06ec21adc 100644--- a/src/core/persistence/sync-discovery.ts+++ b/src/core/persistence/sync-discovery.ts@@ -624,7 +624,7 @@ function identityDowngradesOf(manifests: Array<SyncManifest | null>, eligible: (* so it must not when both carry an explicit frontmatter id and the ids differ: they are two different documents that* happen to map to one slug. The sync is refused before any write, naming both.*/-function assertTakeoverIdentity(discovery: SyncDiscovery, winner: SyncEntry, holder: { slug: string; fm_id: string | null }): void {+export function assertTakeoverIdentity(discovery: SyncDiscovery, winner: SyncEntry, holder: { slug: string; fm_id: string | null }): void {const incoming = holder.fm_id ? incomingId(discovery, winner) : null;if (!explicitIdVeto(incoming, holder.fm_id)) return;throw opError('page_identity_changed', 'A file would take over a page whose frontmatter id differs from its own.',@@ -633,7 +633,7 @@ function assertTakeoverIdentity(discovery: SyncDiscovery, winner: SyncEntry, hol{ detail: 'rename_identity_conflict' });}/** #6729: the frontmatter id of the file an entry imports (null when it has none or cannot be read). */-function incomingId(discovery: SyncDiscovery, entry: SyncEntry): string | null {+export function incomingId(discovery: SyncDiscovery, entry: SyncEntry): string | null {try { return frontmatterIdentity(readSyncContent(discovery, entry)).id; } catch { return null; }}/** #6517: several live pages (the soft-delete exit is named) or several tombstones record one imported origin. */diff --git a/src/core/persistence/sync-pass-run.ts b/src/core/persistence/sync-pass-run.tsindex 250a2cbdf4..595a93ad10 100644--- a/src/core/persistence/sync-pass-run.ts+++ b/src/core/persistence/sync-pass-run.ts@@ -15,8 +15,9 @@*/import { randomUUID } from 'node:crypto';import { opError } from '../ops/contract.ts';-import { confirmedSyncDelta } from '../sync-rename-identity.ts';-import type { DowngradedRename } from '../sync.ts';+import { confirmedSyncDelta, confirmRenameIdentities, explicitIdVeto } from '../sync-rename-identity.ts';+import { buildDetachedWorkingTreeManifest } from '../sync-delta.ts';+import type { DowngradedRename, SyncManifest } from '../sync.ts';import type { BrainEngine } from '../engine.ts';import type { SyncOpts, SyncResult } from '../../commands/sync.ts';import type { Cursor, RetryWait } from './sync-run.ts';@@ -24,7 +25,7 @@ import { getWriteRequestById } from './journal.ts';import type { WriteRequest } from './model.ts';import { managedSyncWriteDiagnostic } from './sync-write-diagnostic.ts';import { recordedStop, replaysUnchangedRefusal } from './sync-stop-class.ts';-import { adoptLegacyPage, syncExcludePatterns, syncGit, type LegacyIdentity, type ManagedSyncContext, type SyncDiscovery, type SyncEntry, type SyncSlugCollision } from './sync-discovery.ts';+import { adoptLegacyPage, assertTakeoverIdentity, incomingId, syncExcludePatterns, syncGit, type LegacyIdentity, type ManagedSyncContext, type SyncDiscovery, type SyncEntry, type SyncSlugCollision } from './sync-discovery.ts';import { assertSyncDispatchActive, type SyncAuthority, type SyncProcessingOptions } from './sync-authority.ts';import type { PassStamp, SyncCursorOptions, SyncIntent } from './sync-prepare.ts';import { OPEN_AT_STAMP_CAP, passFingerprint, readPassRow, SYNC_ORIGIN_DELETED_OP, writePassRow, type ManagedSyncPassValue, type PassEpisode,@@ -148,20 +149,21 @@ async function batchEntries(engine: BrainEngine, level: ShadowLevel, deletes: Sh// that folds to it; several, or one the importer cannot write, keep the slug and the freeze holds legacy_slug_conflict.const legacySlugs = new Map<string, Pick<SyncEntry, 'legacySlugs' | 'legacyIdVeto'>>();const adopting = fresh.filter(entry => slugOf.get(entry.path) && !isCodeFilePath(entryPaths(level, entry.path).sourcePath));+ const discovery = { root: level.root, gitRoot: level.gitRoot, target: level.head, sourceId: level.sourceId } as SyncDiscovery;+ const named = (entry: ShadowPath): SyncEntry => ({ ...entryPaths(level, entry.path), action: 'import', working: working(entry) });if (adopting.length) {const identities = await engine.executeRaw<LegacyIdentity>("SELECT id, slug, source_path, deleted_at, frontmatter->>'id' AS fm_id FROM pages WHERE source_id=$1 AND (source_path IS NULL OR slug=ANY($2::text[]))",[level.sourceId, [...new Set(adopting.map(entry => slugOf.get(entry.path)!))]]);const bySlug = new Map(identities.map(page => [page.slug, page]));- const discovery = { root: level.root, gitRoot: level.gitRoot, target: level.head, sourceId: level.sourceId } as SyncDiscovery;for (const entry of adopting) {- const named: SyncEntry = { ...entryPaths(level, entry.path), action: 'import', working: working(entry) };- slugOf.set(entry.path, await adoptLegacyPage(engine, discovery, identities, bySlug.get(slugOf.get(entry.path)!), slugOf.get(entry.path)!, named));- if (named.legacySlugs) legacySlugs.set(entry.path, { legacySlugs: named.legacySlugs, ...(named.legacyIdVeto ? { legacyIdVeto: true as const } : {}) });+ const adopted = named(entry);+ slugOf.set(entry.path, await adoptLegacyPage(engine, discovery, identities, bySlug.get(slugOf.get(entry.path)!), slugOf.get(entry.path)!, adopted));+ if (adopted.legacySlugs) legacySlugs.set(entry.path, { legacySlugs: adopted.legacySlugs, ...(adopted.legacyIdVeto ? { legacyIdVeto: true as const } : {}) });}}const slugs = [...new Set([...slugOf.values()].filter(Boolean))];- const holders = new Map((slugs.length ? await engine.executeRaw<{ id: number | string; slug: string; source_path: string | null; revision: string; deleted: boolean }>(- 'SELECT id, slug, source_path, knowledge_revision::text AS revision, deleted_at IS NOT NULL AS deleted FROM pages WHERE source_id=$1 AND slug=ANY($2::text[])', [level.sourceId, slugs]) : [])+ const holders = new Map((slugs.length ? await engine.executeRaw<{ id: number | string; slug: string; source_path: string | null; revision: string; deleted: boolean; fm_id: string | null }>(+ "SELECT id, slug, source_path, knowledge_revision::text AS revision, deleted_at IS NOT NULL AS deleted, frontmatter->>'id' AS fm_id FROM pages WHERE source_id=$1 AND slug=ANY($2::text[])", [level.sourceId, slugs]) : []).map(page => [page.slug, page]));const gitPathOfOrigin = (origin: string) => level.slugMode === 'source-root' && level.scope ? `${level.scope}/${syncOriginPath(origin)}` : syncOriginPath(origin);// #6887: a both-moved import imports when the page still holds what its recorded disk blob imports (sync-screen.ts driftBaselineContent).@@ -179,15 +181,24 @@ async function batchEntries(engine: BrainEngine, level: ShadowLevel, deletes: Shclaims.set(slug, [...claims.get(slug) ?? [], entry]);}const renamed = new Set<string>();- let renames: Promise<Map<string, string> | null> | null = null;+ let renames: Promise<Map<string, { from: string; identityHead?: string }> | null> | null = null;const downgrades: DowngradedRename[] = [];+ // #6729, as discovery confirms them: the commit range's renames, then (working mode) the dirty tree's, whose target side+ // is read from disk and whose kept renames are bound to the bytes the verdict read (identityHead, checked at import).const gitRenames = () => renames ??= (async () => {- if (level.mode !== 'committed' || !level.baselineCommit || level.baselineCommit === level.head || (!deleteEntries.size && !level.heldRenames.length)) return null;- const delta = await confirmedSyncDelta(engine, level.gitRoot, level.baselineCommit, level.head, { sourceId: level.sourceId, sourcePathOf: path => entryPaths(level, path).sourcePath });- if (delta.status !== 'ok') return null;- // #6729: the inexact renames whose identity was not kept are reported (and recorded) as discovery reported them.- downgrades.push(...(delta.manifest.downgradedRenames ?? []).map(pair => ({ ...pair, from: entryPaths(level, pair.from).sourcePath, to: entryPaths(level, pair.to).sourcePath })));- return new Map(delta.manifest.renamed.map(pair => [pair.to, pair.from]));+ if (!deleteEntries.size && !level.heldRenames.length) return null;+ const confirm = { sourceId: level.sourceId, sourcePathOf: (path: string) => entryPaths(level, path).sourcePath };+ const manifests: SyncManifest[] = [];+ if (level.baselineCommit && level.baselineCommit !== level.head) {+ const delta = await confirmedSyncDelta(engine, level.gitRoot, level.baselineCommit, level.head, confirm);+ if (delta.status === 'ok') manifests.push(delta.manifest);+ else if (level.mode === 'committed') return null;+ }+ if (level.mode === 'working') manifests.push(await confirmRenameIdentities(engine, level.gitRoot, 'HEAD', null, buildDetachedWorkingTreeManifest(level.gitRoot), confirm));+ if (!manifests.length) return null;+ // The inexact renames whose identity was not kept are reported (and recorded) as discovery reported them.+ downgrades.push(...manifests.flatMap(manifest => manifest.downgradedRenames ?? []).map(pair => ({ ...pair, from: entryPaths(level, pair.from).sourcePath, to: entryPaths(level, pair.to).sourcePath })));+ return new Map(manifests.flatMap(manifest => manifest.renamed.map(pair => [pair.to, { from: pair.from, ...(pair.identityHead ? { identityHead: pair.identityHead } : {}) }] as const)));})();for (const [slug, claimants] of claims) {const holder = holders.get(slug);@@ -200,6 +211,8 @@ async function batchEntries(engine: BrainEngine, level: ShadowLevel, deletes: Shconst 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) {+ // #6729: a file whose frontmatter id differs from the page's is another document; it inherits nothing (refused before any write).+ assertTakeoverIdentity(discovery, named(winner), 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).@@ -212,10 +225,11 @@ async function batchEntries(engine: BrainEngine, level: ShadowLevel, deletes: Sh// #5988: a held rename destination still owes its page the move: re-bound while the origin page is unchanged, else held rename_held.// A held destination renamed again (a Git rename of the held path) carries the held identity on.const heldAt = (path: string | undefined) => path === undefined ? undefined : level.heldRenames.find(row => row.path === path)?.rename;- const held = heldAt(winner.path) ?? (level.heldRenames.length ? heldAt((await gitRenames())?.get(winner.path)) : undefined);- const origin = held ? (await engine.executeRaw<{ source_path: string | null; revision: string; deleted: boolean }>(- 'SELECT source_path, knowledge_revision::text AS revision, deleted_at IS NOT NULL AS deleted FROM pages WHERE source_id=$1 AND id=$2', [level.sourceId, held.pageId]))[0] : undefined;- if (held && origin && !origin.deleted && origin.source_path !== null && syncOriginPath(origin.source_path) === syncOriginPath(held.sourcePath)) {+ const held = heldAt(winner.path) ?? (level.heldRenames.length ? heldAt((await gitRenames())?.get(winner.path)?.from) : undefined);+ const origin = held ? (await engine.executeRaw<{ source_path: string | null; revision: string; deleted: boolean; fm_id: string | null }>(+ "SELECT source_path, knowledge_revision::text AS revision, deleted_at IS NOT NULL AS deleted, frontmatter->>'id' AS fm_id FROM pages WHERE source_id=$1 AND id=$2", [level.sourceId, held.pageId]))[0] : undefined;+ if (held && origin && !origin.deleted && origin.source_path !== null && syncOriginPath(origin.source_path) === syncOriginPath(held.sourcePath)+ && !explicitIdVeto(incomingId(discovery, named(winner)), origin.fm_id)) {entries.push({ ...base, pageId: null, revision: null, ...(origin.revision === held.revision ? { renameFrom: held } : { renameHeld: held }) });continue;}@@ -223,11 +237,12 @@ async function batchEntries(engine: BrainEngine, level: ShadowLevel, deletes: Shconst twins = fresh.filter(other => other.state.level === winner.state.level);// #6729: a Git rename with an edit keeps its page when Git pairs it and the identity is confirmed, as discovery's delta does.const inexact = moved.length === 1 && twins.length === 1 ? null : (await gitRenames())?.get(winner.path);- const gitMoved = inexact && deleteEntries.has(inexact) && !renamed.has(inexact) ? leaving.get(inexact) : undefined;+ const gitMoved = inexact && deleteEntries.has(inexact.from) && !renamed.has(inexact.from) ? leaving.get(inexact.from) : undefined;if (gitMoved?.pageId != null || (moved.length === 1 && twins.length === 1)) {const gone = gitMoved ?? moved[0]!;const origin = entryPaths(level, gone.path).sourcePath;- entries.push({ ...base, pageId: null, revision: null, renameFrom: { sourcePath: origin, slug: gone.slug!, pageId: gone.pageId!, revision: gone.revision! } });+ entries.push({ ...base, pageId: null, revision: null, renameFrom: { sourcePath: origin, slug: gone.slug!, pageId: gone.pageId!, revision: gone.revision! },+ ...(gitMoved && inexact?.identityHead && base.working ? { identityHead: inexact.identityHead } : {}) });deleteEntries.delete(gone.path); renamed.add(gone.path);continue;}@@ -625,7 +640,10 @@ export async function runSyncPass(engine: BrainEngine, input: SyncPassInput): Prawait finishPassApply(engine, sourceId, applied);return result(configured && context.source.last_commit === null ? 'first_sync' : 'synced', configured ? undefined : 'selection_scoped');}- const built = await batchEntries(engine, level, deletes, imports);+ const built = await batchEntries(engine, level, deletes, imports).catch(async (error: unknown) => {+ if ((error as { code?: string }).code === 'page_identity_changed') await save('blocked', 'page_identity_changed');+ throw error;+ });collisions.push(...built.collisions);identityDowngrades.push(...built.downgrades.filter(pair => !identityDowngrades.some(seen => seen.from === pair.from && seen.to === pair.to)));for (const entry of [...built.skipped, ...built.collisions.flatMap(c => c.skipped.map(sourcePath => imports.find(i => entryPaths(level, i.path).sourcePath === sourcePath)!))].filter(Boolean)) {diff --git a/test/sync-inexact-rename-6729.test.ts b/test/sync-inexact-rename-6729.test.tsindex f1b20dafe7..315acb3b32 100644--- a/test/sync-inexact-rename-6729.test.ts+++ b/test/sync-inexact-rename-6729.test.ts@@ -259,8 +259,17 @@ for (const kind of testBackends()) describe(`inexact Git rename identity (#6729,expect((await pageRow(s.id, 'notes/spec-2'))!.id).not.toBe(spec.id);expect([(await pageRow(s.id, 'people/alice'))!.id, (await pageRow(s.id, 'people/bob'))!.id]).not.toContain(a.id);expect([(await pageRow(s.id, 'people/alice'))!.id, (await pageRow(s.id, 'people/bob'))!.id]).not.toContain(b.id);+ // Design v3.2 §2.3 row 6 (delete_drift): in managed sync the retitle moved the old page in the database since its+ // import, so its file's deletion has no proof and is held file_drift (side deleted); the page is never soft-deleted,+ // so the downgraded pair has no old page to record.expect(result.identityDowngrades?.map(entry => [entry.from, entry.reason]).sort()).toEqual([- ['notes/spec.md', 'no_identity_signal'], ['people/a.md', 'title_not_unique'], ['people/b.md', 'title_not_unique']]);+ ...(mode === 'classic' ? [['notes/spec.md', 'no_identity_signal']] : []), ['people/a.md', 'title_not_unique'], ['people/b.md', 'title_not_unique']]);+ if (mode === 'managed') {+ const { readGitSourceHolds } = await import('../src/core/persistence/sync-holds.ts');+ const holds = (await readGitSourceHolds(engine, { sourceIds: [s.id] }))[0]?.holds ?? [];+ expect(holds.map(hold => [hold.path, hold.code, hold.meta.side])).toEqual([['notes/spec.md', 'file_drift', 'deleted']]);+ expect(await pageRow(s.id, 'notes/spec')).toMatchObject({ id: spec.id, deleted_at: null });+ }git(s.root, 'mv', 'notes/draft.md', 'notes/draft-2.md'); write(s.root, 'notes/draft-2.md', h1(`Draft ${mode} h1 6729`, 'Edited.\n'));git(s.root, 'add', '-A');expect(git(s.root, 'diff', '--cached', '--name-status', '-M')).toMatch(/^R0\d\d\tnotes\/draft.md\tnotes\/draft-2.md$/);