Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(sync): skip a pinned import that trails the page's own working-tree bytes

Howard Park026391c5a89e
diff --git a/src/core/persistence/sync-prepare.ts b/src/core/persistence/sync-prepare.ts
index f457c1db0a..a07c80f98d 100644
--- a/src/core/persistence/sync-prepare.ts
+++ b/src/core/persistence/sync-prepare.ts
@@ -15,7 +15,7 @@ import { prepareCanonicalProjections } from './canonical-projections.ts';
import { digest, sha256 } from './digest.ts';
import { preserveProtectedTakes } from './protected-takes.ts';
import { getWorktreeBinding } from './ownership.ts';
-import { assertConfiguredSyncRoot, assertSyncEntryOrigin, syncGit, syncRawHash, type SyncRename } from './sync-discovery.ts';
+import { assertConfiguredSyncRoot, assertSyncEntryOrigin, readSyncFile, syncGit, syncRawHash, type SyncRename } from './sync-discovery.ts';
import { assertSyncPageOrigin, sameSyncOrigin, syncOriginPath, syncOriginScope, type SyncOriginScope } from './sync-origin.ts';
import { assertManagedSyncActive, validateSyncAuthority, type SyncAuthority, type SyncProcessingOptions } from './sync-authority.ts';
import type { PreparedContentImport } from './prepared-import.ts';
@@ -200,7 +200,17 @@ export async function prepareManagedSyncMutation(engine: BrainEngine, row: Write
throw new OperationError('invalid_params', frontmatterSlugConflictMessage(p.sourcePath, parsedInput.slug, expectedSlug));
}
if (!p.companyApproval && base && !p.lineEndingOnly && p.rawHash !== sha256(p.content) && !sameCanonicalImport(base, parsedInput)) {
- throw new OperationError('source_changed', 'Newer working-tree bytes and the current page disagree with this pinned Git import.');
+ // The pinned commit can simply trail the coordinator: a committed page write reaches the
+ // working tree before its Git effect lands. When the newer working-tree bytes are the
+ // current page itself, there is nothing to import and nothing to protect; the commit that
+ // carries them is imported as a no-op by a later run.
+ const working = renamed ? null : readSyncFile(root, p.path);
+ if (!working || sha256(working) !== p.rawHash
+ || !sameCanonicalImport(base, parseMarkdown(working.toString('utf8'), row.slug, { activePack }))) {
+ throw new OperationError('source_changed', 'Newer working-tree bytes and the current page disagree with this pinned Git import.');
+ }
+ return { observedRevision: snapshot?.revision ?? null, noop: true, contentUnchanged: true, validate,
+ apply: async () => ({ status: 'skipped', slug: row.slug, source_id: row.source_id, chunks: 0, noop: true, imported_file: true }) };
}
let importContent = p.content;
if (row.authority.remote) {
diff --git a/test/persistence-managed-sync.test.ts b/test/persistence-managed-sync.test.ts
index 0850aa7717..2968365775 100644
--- a/test/persistence-managed-sync.test.ts
+++ b/test/persistence-managed-sync.test.ts
@@ -81,6 +81,33 @@ test('imports files without rewriting bytes and checkpoints only committed page
}
}),120_000);
+test('a pinned import that trails the current page and its working-tree bytes is skipped, not a conflict', async () => withEnv({ GBRAIN_HOME: home }, async () => {
+ for (const engine of engines) {
+ const current = '---\ntitle: Example note\n---\nThe current observation, already in the database.\n';
+ const pinned = '---\ntitle: Example note\n---\nAn older observation that Git still carries.\n';
+ const f = await fixture(engine, { 'notes/example.md': current });
+ expect((await performManagedSync(engine, { sourceId: f.id, noPull: true })).status).toBe('first_sync');
+ const path = join(f.root, 'notes/example.md');
+ // The state between a coordinated page write and its queued Git effect: the newest commit
+ // holds other bytes than the page, and the working tree already carries the page's own bytes.
+ writeFileSync(path, pinned); const head = commit(f.root, 'commit that trails the page'); writeFileSync(path, current);
+ const result = await performManagedSync(engine, { sourceId: f.id, noPull: true });
+ expect(result.status).not.toBe('blocked_by_failures'); expect(result.managedWrite).toBeUndefined(); expect(result.toCommit).toBe(head);
+ expect((await engine.getPage('notes/example', { sourceId: f.id }))?.compiled_truth).toContain('current observation');
+ expect(readFileSync(path, 'utf8')).toBe(current);
+ expect((await engine.executeRaw<{ last_commit: string }>('SELECT last_commit FROM sources WHERE id=$1', [f.id]))[0].last_commit).toBe(head);
+ expect(loadSyncFailures().filter(row => row.source_id === f.id)).toHaveLength(0);
+ // Working-tree bytes that are neither the pinned commit nor the page are still protected.
+ const foreign = '---\ntitle: Example note\n---\nA local edit that nothing imported.\n';
+ writeFileSync(path, pinned.replace('older', 'still older')); commit(f.root, 'another trailing commit'); writeFileSync(path, foreign);
+ const blocked = await performManagedSync(engine, { sourceId: f.id, noPull: true });
+ expect(blocked).toMatchObject({ status: 'blocked_by_failures', managedWrite: { write_error: 'source_changed', reason: 'pinned_git_worktree_conflict' } });
+ expect(readFileSync(path, 'utf8')).toBe(foreign);
+ expect((await engine.getPage('notes/example', { sourceId: f.id }))?.compiled_truth).toContain('current observation');
+ rmSync(syncFailuresPath(), { force: true });
+ }
+}),120_000);
+
test('interrupted cursor resumes its pinned target before a newer HEAD and never advances early', async () => withEnv({ GBRAIN_HOME: home }, async () => {
for (const engine of engines) {
const f = await fixture(engine, {'a.md':'First stable observation about engineering.\n','b.md':'Original second observation about engineering.\n'});