Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(sync): record where each rename side was read; dirty-tree fixtures for working-tree-only ids and unstaged id conflicts (#6729)

garrytan9862e71d3b17
diff --git a/src/core/sync-identity-downgrades.ts b/src/core/sync-identity-downgrades.ts
index f2200eb6dd..48c8522c02 100644
--- a/src/core/sync-identity-downgrades.ts
+++ b/src/core/sync-identity-downgrades.ts
@@ -58,7 +58,7 @@ export async function recordIdentityDowngrades(engine: BrainEngine, sourceId: st
(SELECT count(*) FROM links WHERE from_page_id = $3 OR to_page_id = $3) AS links,
(SELECT count(*) FROM page_aliases WHERE source_id = $1 AND slug = $2) AS aliases`, [sourceId, old.slug, Number(old.id)]);
const recorded = new Date(old.deleted_at);
- const record: IdentityDowngrade = { source_id: sourceId, from: pair.from, to: pair.to, similarity: pair.similarity, reason: pair.reason,
+ const record: IdentityDowngrade = { source_id: sourceId, from: pair.from, to: pair.to, similarity: pair.similarity, reason: pair.reason, ...(pair.read ? { read: pair.read } : {}),
old_slug: old.slug, old_page_id: Number(old.id), new_slug: fresh?.slug ?? null,
facts: Number(counts?.facts ?? 0), links: Number(counts?.links ?? 0), aliases: Number(counts?.aliases ?? 0),
recorded_at: recorded.toISOString(), expires_at: new Date(recorded.getTime() + IDENTITY_DOWNGRADE_WINDOW_HOURS * 3600_000).toISOString() };
diff --git a/src/core/sync-rename-identity.ts b/src/core/sync-rename-identity.ts
index a44d172cc3..c396aa4317 100644
--- a/src/core/sync-rename-identity.ts
+++ b/src/core/sync-rename-identity.ts
@@ -30,7 +30,7 @@ import { JSON_SCHEMA, load as yamlLoad } from 'js-yaml';
import type { BrainEngine } from './engine.ts';
import { spawn } from './spawn.ts';
import { gitChildEnv } from './git-env.ts';
-import { isMarkdownFilePath, resolveSlugForPath, type DowngradedRename, type RenameDowngradeReason, type SyncManifest } from './sync.ts';
+import { isMarkdownFilePath, resolveSlugForPath, type DowngradedRename, type RenameDowngradeReason, type RenameSideSource, type SyncManifest } from './sync.ts';
import { computeSyncDelta, type SyncDeltaResult } from './sync-delta.ts';
const HEAD_BYTES = 64 * 1024;
@@ -126,6 +126,8 @@ export interface RenameIdentityInputs {
live: LiveIdentityHolder[];
/** Maps a Git path to its source-relative form; a page recording either form belongs to the range. */
sourcePathOf?: (gitPath: string) => string;
+ /** Where the target side was read (the base side is always a committed blob); defaults to `commit`. */
+ targetSource?: RenameSideSource;
}
/** Inexact pairs, or an add/delete pool rule (c) can pair; with neither the manifest passes through untouched. */
@@ -169,7 +171,8 @@ export function decideRenameIdentities(manifest: SyncManifest, inputs: RenameIde
const downgraded: DowngradedRename[] = [];
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 });
+ const downgrade = (reason: RenameDowngradeReason) => downgraded.push({ from: rename.from, to: rename.to, similarity: rename.similarity!, reason,
+ read: { from: 'commit', to: inputs.targetSource ?? 'commit' } });
// 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));
@@ -245,7 +248,7 @@ export async function confirmRenameIdentities(engine: Pick<BrainEngine, 'execute
return decideRenameIdentities(manifest, {
base: path => baseIdentity.get(path) ?? NO_IDENTITY,
target: path => targetIdentity.get(path) ?? NO_IDENTITY,
- live, sourcePathOf: opts.sourcePathOf,
+ live, sourcePathOf: opts.sourcePathOf, targetSource: target === null ? 'working_tree' : 'commit',
});
}
diff --git a/src/core/sync.ts b/src/core/sync.ts
index 70e3995d9a..2568b588de 100644
--- a/src/core/sync.ts
+++ b/src/core/sync.ts
@@ -31,7 +31,11 @@ export interface SyncManifest {
/** #6729: why an inexact Git rename did not keep its page identity (see `confirmRenameIdentities`). */
export type RenameDowngradeReason = 'id_conflict' | 'id_shared' | 'id_one_side' | 'title_not_unique' | 'slug_not_unique' | 'no_identity_signal';
-export interface DowngradedRename { from: string; to: string; similarity: number; reason: RenameDowngradeReason }
+/** Where a rename side's frontmatter was read: a committed blob, or the file on disk (the dirty-tree manifest's target side). */
+export type RenameSideSource = 'commit' | 'working_tree';
+export interface DowngradedRename { from: string; to: string; similarity: number; reason: RenameDowngradeReason;
+ /** #6729: the source each side's frontmatter was read from (absent on a pair recorded before this field). */
+ read?: { from: RenameSideSource; to: RenameSideSource } }
export interface RawManifestEntry {
action: 'A' | 'M' | 'D' | 'R';
diff --git a/test/sync-inexact-rename-6729.test.ts b/test/sync-inexact-rename-6729.test.ts
index 307e3ac441..6c52dc0c3b 100644
--- a/test/sync-inexact-rename-6729.test.ts
+++ b/test/sync-inexact-rename-6729.test.ts
@@ -225,6 +225,8 @@ for (const kind of testBackends()) describe(`inexact Git rename identity (#6729,
expect(confirmed.renamed.every(rename => rename.from.replace('notes/n', '') === rename.to.replace('notes/m', ''))).toBe(true);
expect(confirmed.added).toEqual([]);
expect(confirmed.deleted).toEqual([]);
+ // Stated budget: 4,000 frontmatter heads (both sides) through one cat-file stream plus the pure decision in under 20 s on a
+ // 4-vCPU CI machine (about 2.5 s measured locally), so a large reorganization never stalls discovery.
expect(elapsed).toBeLessThan(20_000);
}, 300_000);
@@ -263,6 +265,25 @@ for (const kind of testBackends()) describe(`inexact Git rename identity (#6729,
expect((await pageRow(s.id, 'notes/x'))!.id).toBe(x.id);
}), 180_000);
+ test('dirty tree reads the working-tree side from disk: an id only the working file carries, and an unstaged id conflict on a staged rename, both stay delete plus add (classic and managed)', () => withEnv(env, async () => {
+ for (const mode of ['classic', 'managed'] as const) {
+ const s = await (mode === 'classic' ? classic : managed)({ 'notes/w.md': doc('title: Working one'), 'notes/s.md': doc('id: staged-6729-one\ntitle: Staged one') });
+ const w = (await pageRow(s.id, 'notes/w'))!, st = (await pageRow(s.id, 'notes/s'))!;
+ git(s.root, 'mv', 'notes/w.md', 'notes/w-2.md'); write(s.root, 'notes/w-2.md', doc('id: working-6729\ntitle: Working two', 'Edited.\n'));
+ git(s.root, 'mv', 'notes/s.md', 'notes/s-2.md'); write(s.root, 'notes/s-2.md', doc('id: staged-6729-one\ntitle: Staged two', 'Edited.\n'));
+ git(s.root, 'add', '-A');
+ // The index still pairs s.md -> s-2.md under the same id; only the working file changes it.
+ write(s.root, 'notes/s-2.md', doc('id: staged-6729-two\ntitle: Staged two', 'Edited.\n'));
+ expect(git(s.root, 'diff', '--cached', '--name-status', '-M').split('\n').every(line => /^R0\d\d\t/.test(line))).toBe(true);
+ const result = await s.sync({ workingTree: true });
+ expect(result.status).toBe('synced');
+ expect(result.identityDowngrades?.map(entry => [entry.from, entry.reason, entry.read]).sort()).toEqual([
+ ['notes/s.md', 'id_conflict', { from: 'commit', to: 'working_tree' }], ['notes/w.md', 'id_one_side', { from: 'commit', to: 'working_tree' }]]);
+ expect((await pageRow(s.id, 'notes/w-2'))!.id).not.toBe(w.id);
+ expect((await pageRow(s.id, 'notes/s-2'))!.id).not.toBe(st.id);
+ }
+ }), 240_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'),