Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(sync): scope the #6729 verdict in managed takeovers to renames and held renames; one exported explicit-id veto (#6729)

garrytan9e10c690955a
diff --git a/src/core/persistence/sync-discovery.ts b/src/core/persistence/sync-discovery.ts
index 64988dd158..4b194279ad 100644
--- a/src/core/persistence/sync-discovery.ts
+++ b/src/core/persistence/sync-discovery.ts
@@ -9,7 +9,7 @@ import { managedForbiddenFlag, managedSyncFlagRefusal } from './managed-sync-fla
import type { Action } from '../agent-output.ts';
import type { RegistryCode } from '../error-registry.ts';
import { buildDetachedWorkingTreeManifest, computeSyncDelta } from '../sync-delta.ts';
-import { confirmedSyncDelta, confirmRenameIdentities, frontmatterIdentity } from '../sync-rename-identity.ts';
+import { confirmedSyncDelta, confirmRenameIdentities, explicitIdVeto, frontmatterIdentity } from '../sync-rename-identity.ts';
import { isSyncable, isCodeFilePath, isImageFilePath, matchesAnyGlob, resolveSlugForPath, type DowngradedRename, type SyncManifest } from '../sync.ts';
import { resolveSlugRootMode } from '../sync-anchor.ts';
import { isWriteTargetContained } from '../path-confine.ts';
@@ -547,7 +547,7 @@ export async function discoverManagedSync(engine: BrainEngine, opts: SyncOpts, c
// #5988: a held rename destination (or a held one renamed again) still owes its page the move; the deletion of a held path stays to clear its hold.
const held = holdByPath.get(winner.path)?.meta.rename_from ?? (deletion ? holdByPath.get(deletion.path)?.meta.rename_from : undefined);
const origin = held ? identities.find(page => page.id === held.pageId) : undefined;
- if (held && origin && !deleted.has(origin.id) && origin.source_path != null && sameSyncOrigin(origin.source_path, held.sourcePath, originScope, origin.slug)) {
+ if (held && origin && !deleted.has(origin.id) && origin.source_path != null && sameSyncOrigin(origin.source_path, held.sourcePath, originScope, origin.slug) && !explicitIdVeto(incomingId(discovered, winner), origin.fm_id)) {
if (origin.knowledge_revision === held.revision) winner.renameFrom = held;
else winner.renameHeld = held;
}
@@ -577,14 +577,17 @@ function identityDowngradesOf(manifests: Array<SyncManifest | null>, eligible: (
* 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 {
- if (!holder.fm_id) return;
- const incoming = frontmatterIdentity(readSyncContent(discovery, winner)).id;
- if (!incoming || incoming === holder.fm_id) return;
+ 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.',
`${winner.sourcePath} in ${discovery.sourceId} maps to page ${holder.slug}, whose file is gone, but its frontmatter id '${incoming}' differs from the page's id '${holder.fm_id}', so it is a different document and must not inherit that page's facts, links and history; nothing was written. `
+ `Give the file the page's id if it is the same document, or rename it so its slug differs, commit, then run gbrain sync --no-pull --source ${discovery.sourceId}.`,
{ 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 {
+ 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. */
function ambiguousOriginRefusal(sourceId: string, sourcePath: string, named: Array<{ slug: string }>, ambiguous: 'several_live' | 'several_tombstones'): OperationError {
return opError('page_identity_changed', 'Several pages claim the same imported origin.',
diff --git a/src/core/sync-rename-identity.ts b/src/core/sync-rename-identity.ts
index 558787f7af..a44d172cc3 100644
--- a/src/core/sync-rename-identity.ts
+++ b/src/core/sync-rename-identity.ts
@@ -107,6 +107,14 @@ function workingTreeHead(root: string, path: string): Buffer | null {
} catch { return null; }
}
+/**
+ * The explicit-id veto every identity takeover consults (#6729): a file whose frontmatter id differs from the id of the
+ * page it would inherit is a different document. Exported for the managed freeze loop's takeover branches.
+ */
+export function explicitIdVeto(incomingId: string | null, holderId: string | null): boolean {
+ return !!incomingId && !!holderId && incomingId !== holderId;
+}
+
/** A live page that shares an identity value with a renamed file; `source_path` and `slug` tell whether it belongs to a path of this range. */
export interface LiveIdentityHolder { slug: string; source_path: string | null; fm_id: string | null; title: string | null }
@@ -162,8 +170,12 @@ export function decideRenameIdentities(manifest: SyncManifest, inputs: RenameIde
for (const rename of inexact) {
const before = inputs.base(rename.from), after = inputs.target(rename.to);
const downgrade = (reason: RenameDowngradeReason) => downgraded.push({ from: rename.from, to: rename.to, similarity: rename.similarity!, reason });
+ // A respelled path (case or separators) that keeps its slug is the same-slug takeover managed discovery already
+ // performs: it keeps the page unless an explicit id conflicts.
+ const sameSlug = resolveSlugForPath(sourcePathOf(rename.from)) === resolveSlugForPath(sourcePathOf(rename.to));
+ if (sameSlug && !explicitIdVeto(after.id, before.id)) { confirmed.push(rename); continue; }
if (before.id && after.id) {
- if (before.id !== after.id) downgrade('id_conflict');
+ if (explicitIdVeto(after.id, before.id)) downgrade('id_conflict');
else if (uniqueId(before.id)) confirmed.push(rename);
else downgrade('id_shared');
continue;
diff --git a/test/sync-inexact-rename-6729.test.ts b/test/sync-inexact-rename-6729.test.ts
index 1bafcc5057..3a8bbd9776 100644
--- a/test/sync-inexact-rename-6729.test.ts
+++ b/test/sync-inexact-rename-6729.test.ts
@@ -232,6 +232,41 @@ for (const kind of testBackends()) describe(`inexact Git rename identity (#6729,
expect(elapsed).toBeLessThan(20_000);
}, 300_000);
+ test('a case-respelling move plus edit keeps its page in classic and managed sync; in managed sync a conflicting explicit id does not inherit', () => withEnv(env, async () => {
+ for (const mode of ['classic', 'managed'] as const) {
+ const s = await (mode === 'classic' ? classic : managed)({ 'notes/Foo Bar.md': doc('title: First title'), 'notes/Id Doc.md': doc('id: id-doc-one-6729\ntitle: Id doc') });
+ const foo = (await pageRow(s.id, 'notes/foo-bar'))!, idDoc = (await pageRow(s.id, 'notes/id-doc'))!;
+ git(s.root, 'mv', 'notes/Foo Bar.md', 'notes/foo-bar.md'); write(s.root, 'notes/foo-bar.md', doc('title: A new title', 'An edit.\n'));
+ commit(s.root, 'respell and edit');
+ expect(diffStatus(s.root)).toEqual([expect.stringMatching(/^R0\d\d\tnotes\/Foo Bar.md\tnotes\/foo-bar.md$/)]);
+ const result = await s.sync();
+ expect(result.status).toBe('synced');
+ expect((await pageRow(s.id, 'notes/foo-bar'))).toMatchObject({ id: foo.id, deleted_at: null });
+ expect(result.identityDowngrades ?? []).toEqual([]);
+ if (mode === 'classic') continue;
+ // Managed discovery's same-slug takeover: only a conflicting explicit id vetoes it, before anything is written.
+ git(s.root, 'mv', 'notes/Id Doc.md', 'notes/id-doc.md'); write(s.root, 'notes/id-doc.md', doc('id: id-doc-two-6729\ntitle: Id doc', 'Another document.\n'));
+ commit(s.root, 'respell with a different id');
+ await expect(s.sync()).rejects.toMatchObject({ code: 'page_identity_changed', detail: 'rename_identity_conflict' });
+ expect((await pageRow(s.id, 'notes/id-doc'))).toMatchObject({ id: idDoc.id, deleted_at: null });
+ }
+ }), 240_000);
+
+ test('managed: a held rename whose fixed file carries a different explicit id does not inherit the held page', () => withEnv(env, async () => {
+ const s = await managed({ 'notes/x.md': doc('id: held-6729-one\ntitle: Held X\nslug: notes/x') });
+ const x = (await pageRow(s.id, 'notes/x'))!;
+ git(s.root, 'mv', 'notes/x.md', 'notes/y.md'); commit(s.root, 'exact move onto a stale slug: line');
+ expect(diffStatus(s.root)).toEqual(['R100\tnotes/x.md\tnotes/y.md']);
+ const held = await s.sync();
+ expect(held.held?.[0]).toMatchObject({ path: 'notes/y.md' });
+ write(s.root, 'notes/y.md', doc('id: held-6729-two\ntitle: Another document'));
+ commit(s.root, 'replace the held file with a different document');
+ await s.sync();
+ const y = (await pageRow(s.id, 'notes/y'))!;
+ expect(y.id).not.toBe(x.id);
+ expect((await pageRow(s.id, 'notes/x'))!.id).toBe(x.id);
+ }), 180_000);
+
test('managed: crossed inexact pairs with one shared title never swap identities; a unique title keeps one', () => withEnv(env, async () => {
const s = await managed({
'people/a-x-com.md': doc('type: person\ntitle: Colleague\naliases: [Ali]', 'Works on payments.\n'),