Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(sync): legacy full sync holds a rename whose old page changed instead of importing a second page (#5988)

garrytan1e67819ff523
diff --git a/src/commands/import.ts b/src/commands/import.ts
index 342310f1d8..9df53b4215 100644
--- a/src/commands/import.ts
+++ b/src/commands/import.ts
@@ -220,6 +220,8 @@ export async function runImport(
strategy?: SyncStrategy;
sourceId?: string;
managedBookmark?: boolean;
+ /** #5988: paths the caller already held this run; they are skipped without importing (not failures). */
+ heldPaths?: ReadonlySet<string> | readonly string[];
/** #5988: each file's outcome (a throw arrives as `{ status: 'error', error }`); `'held'` = the caller held it, not a failure. */
onFileResult?: (path: string, filePath: string, result: Pick<ImportResult, 'status' | 'error' | 'refusal' | 'frontmatter_recovery'>) => Promise<'held' | undefined>;
/**
@@ -667,6 +669,7 @@ export async function runImport(
// never plans against the empty tables it started with (O-CEO-17).
const analyzeEvery = await importAnalyzeEveryPages(engine);
+ const heldPaths = new Set(opts.heldPaths ?? []);
async function processFile(eng: BrainEngine, filePath: string) {
if (signal?.aborted) return;
const relativePath = singleFile ? basename(filePath) : relative(dir, filePath);
@@ -675,6 +678,10 @@ export async function runImport(
// relative (matching the incremental path's git-diff paths). The
// checkpoint (`completed`) stays dir-relative.
const importRelPath = opts.slugRoot ? relative(opts.slugRoot, filePath) : relative(importRoot, filePath);
+ if (heldPaths.has(importRelPath)) {
+ skipped++; completed.add(relativePath); processed++; tickProgress();
+ return;
+ }
// v0.31.2 (D5): per-file slow-path log. Fires only when a single
// file takes >5s. The user's hang surfaces as one file taking
// forever — without this, the agent can't see which file.
diff --git a/src/commands/sync/full.ts b/src/commands/sync/full.ts
index 61e874173e..2d6bab8534 100644
--- a/src/commands/sync/full.ts
+++ b/src/commands/sync/full.ts
@@ -114,6 +114,8 @@ export async function performFullSync(
// issue #1939: performFullSync owns the failure ledger + bookmark via the
// shared gate below; don't let runImport double-record or write its own.
managedBookmark: true,
+ // #5988: a held rename destination whose old page changed is held, never imported as a new page.
+ heldPaths: holds?.heldPaths,
// #5988: content refusals are held, not failed (never gating the bookmark).
onFileResult: async (path, filePath, fileResult) => {
noteScreenedImport(holds, path, fileResult);
diff --git a/src/commands/sync/holds.ts b/src/commands/sync/holds.ts
index 7ececf3f66..1c1ded38b8 100644
--- a/src/commands/sync/holds.ts
+++ b/src/commands/sync/holds.ts
@@ -300,20 +300,35 @@ export async function holdRenameDestination(engine: BrainEngine, holds: LegacyHo
if (!pending) return false;
const current = await currentPage(engine, holds.sourceId, { id: pending.pageId });
if (!current || (current.slug === pending.slug && current.revision === pending.revision)) return false;
- await recordLegacyHold(engine, holds, { path: input.to, upstreamVersion: screen.upstreamVersion, renameFrom: pending, refusal: { code: 'rename_held',
- reason: 'rename_source_changed', message: `The page ${input.to} was renamed from changed after the rename, so moving it onto the new file would overwrite those changes.` } });
+ await holdChangedRename(engine, holds, input.to, pending, screen.upstreamVersion);
return true;
}
-/** Full sync: a held rename destination that now screens clean moves its page (same id) before the walk imports it. */
+/** Holds a rename destination whose old page changed after the rename was recorded: moving it would overwrite those changes. */
+async function holdChangedRename(engine: BrainEngine, holds: LegacyHolds, to: string, renameFrom: SyncRename, upstreamVersion: string | null): Promise<void> {
+ await recordLegacyHold(engine, holds, { path: to, upstreamVersion, renameFrom, refusal: { code: 'rename_held', reason: 'rename_source_changed',
+ message: `${to} is a rename of page ${renameFrom.slug}, which changed after the rename was recorded, so the page was not moved onto the new file.` } });
+}
+
+/**
+ * Full sync: a held rename destination that now screens clean moves its page
+ * (same id) before the walk imports it. One whose old page changed since is
+ * held as `rename_held` (the walk skips it), never imported as a second page.
+ */
export async function moveHeldRenames(engine: BrainEngine, holds: LegacyHolds | null, root: string): Promise<void> {
if (!holds?.active) return;
for (const record of holds.existing.values()) {
const origin = record.meta.rename_from;
const filePath = join(root, record.path);
- if (!origin || !existsSync(filePath) || screenLegacyFile(filePath, record.path).refusal) continue;
+ if (!origin || !existsSync(filePath)) continue;
+ const screen = screenLegacyFile(filePath, record.path);
+ if (screen.refusal) continue;
const current = await currentPage(engine, holds.sourceId, { id: origin.pageId });
- if (!current || current.slug !== origin.slug || current.revision !== origin.revision) continue;
+ if (!current) continue;
+ if (current.slug !== origin.slug || current.revision !== origin.revision) {
+ await holdChangedRename(engine, holds, record.path, origin, screen.upstreamVersion);
+ continue;
+ }
try {
if (await engine.updateSlug(origin.slug, resolveSlugForPath(record.path), { sourceId: holds.sourceId }) === 0) continue;
await engine.executeRaw('UPDATE pages SET source_path=$1 WHERE id=$2 AND source_id=$3', [record.path, origin.pageId, holds.sourceId]);
diff --git a/src/core/persistence/sync-holds.ts b/src/core/persistence/sync-holds.ts
index 50474caa38..b13d05c1ba 100644
--- a/src/core/persistence/sync-holds.ts
+++ b/src/core/persistence/sync-holds.ts
@@ -220,7 +220,7 @@ export function gitHoldFix(record: Pick<GitHoldRecord, 'source_id' | 'path' | 'c
user_message: `gbrain refuses ${record.path}, whose exact bytes imported under an earlier version. This is a gbrain bug: report it with the gbrain version, the file and the code, then upgrade or pin the last good version.`,
why: 'The same bytes imported before, so only gbrain changed; sync holds the file under sync.parser_regression=hold.' };
case 'rename_held':
- return repair(true, `The page ${record.path} was renamed from changed after the rename; the preview proposes re-binding the rename to the current page for approval.`);
+ return repair(true, `The page that ${record.path} renames changed after the rename was recorded, so it was not moved; the preview proposes re-binding the rename to the current page for approval.`);
case 'frontmatter_slug_conflict':
return repair(true, `The frontmatter slug of ${record.path} names another page; the preview proposes removing that line for approval.`);
default:
diff --git a/test/sync-legacy-holds.test.ts b/test/sync-legacy-holds.test.ts
index c5e24d216d..efc85232a7 100644
--- a/test/sync-legacy-holds.test.ts
+++ b/test/sync-legacy-holds.test.ts
@@ -208,6 +208,31 @@ test('a held rename carries its origin across a chained rename, and an edited ol
expect(await page(id, 'notes/y')).toBeUndefined();
}), 120_000);
+test('a full sync holds a renamed file whose old page changed instead of importing it as a new page', async () => eachEngine(async () => {
+ const body = Array.from({ length: 30 }, (_, i) => `Line ${i} of a synthetic note that keeps the rename detectable.`).join('\n');
+ const { id, root } = await source({ 'notes/x.md': `---\ntitle: X\n---\n${body}\n` });
+ await sync(id, root);
+ const x = await page(id, 'notes/x');
+ git(root, 'mv', 'notes/x.md', 'notes/y.md');
+ write(root, { 'notes/y.md': `---\ntitle: alice-example first line\nalice-example second line\n---\n${body}\n` }, 'rename onto a broken file');
+ await sync(id, root);
+ await engine.executeRaw('UPDATE pages SET knowledge_revision=gen_random_uuid() WHERE id=$1', [x.id]);
+ write(root, { 'notes/y.md': `---\ntitle: Y\n---\n${body}\n` }, 'fix y after the old page changed');
+
+ const full = await sync(id, root, { full: true });
+ expect(full.held).toMatchObject([{ path: 'notes/y.md', code: 'rename_held', reason: 'rename_source_changed', stale: true }]);
+ expect(await page(id, 'notes/y')).toBeUndefined();
+ expect(await page(id, 'notes/x')).toMatchObject({ id: x.id, source_path: 'notes/x.md', deleted_at: null });
+ const [hold] = await holds(id);
+ expect(hold).toMatchObject({ path: 'notes/y.md', code: 'rename_held', page_id: x.id, meta: { rename_from: { sourcePath: 'notes/x.md', pageId: x.id } } });
+ expect(hold.message).not.toContain('alice-example');
+
+ // Unchanged on the next full walk: still held, still no second page.
+ await sync(id, root, { full: true });
+ expect(await page(id, 'notes/y')).toBeUndefined();
+ expect((await holds(id)).map(h => [h.path, h.code])).toEqual([['notes/y.md', 'rename_held']]);
+}), 120_000);
+
test('a retry request or an older reader version re-screens a held file the delta did not touch', () => eachEngine(async () => {
const { id, root } = await source({ 'notes/a.md': good('Alpha'), 'notes/broken.md': BROKEN });
await sync(id, root);