Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(sync): a pass ends on its folded stamp when its own Git commit lands before its effect row records it

garrytanaf06914ae38a
diff --git a/src/core/persistence/sync-pass-fold.ts b/src/core/persistence/sync-pass-fold.ts
index e61b0dcbcd..777c4f2909 100644
--- a/src/core/persistence/sync-pass-fold.ts
+++ b/src/core/persistence/sync-pass-fold.ts
@@ -86,9 +86,10 @@ export function foldPassCheckpoint<T extends PreparedMutation>(prepared: T, inpu
/**
* The round after a folded writeback stamp, when HEAD left the stamp's target (durability on: the import's Git effect
* committed meanwhile): whether `from..head` changes exactly the files of the stamp's writebacks (`converged.writeback`),
- * each to the bytes its import wrote (the Git effect's `expected_hash`), with every one of those effects committed. Then
- * the level at HEAD is the one the stamp proved plus the pass's own bytes, and the pass ends on its stamp; anything else
- * reads the level in full.
+ * each to the bytes its import wrote (the Git effect's `expected_hash`), with every one of those effects committed or
+ * still running (its commit lands before its row records it, so HEAD can show the commit first). Then the level at HEAD
+ * is the one the stamp proved plus the pass's own bytes, and the pass ends on its stamp; anything else reads the level
+ * in full.
*/
export async function ownWritebackCommit(engine: Pick<BrainEngine, 'executeRaw'>, input: { gitRoot: string; localPath: string; from: string; writeIds: readonly string[] }): Promise<boolean> {
if (!input.writeIds.length) return false;
@@ -98,7 +99,7 @@ export async function ownWritebackCommit(engine: Pick<BrainEngine, 'executeRaw'>
if (!head || changed === null) return false;
const effects = await engine.executeRaw<{ path: string | null; hash: string | null; state: string }>(`SELECT data->>'relative_path' AS path, data->>'expected_hash' AS hash, state
FROM persistence_effects WHERE request_id = ANY($1::uuid[]) AND kind='git'`, [input.writeIds]);
- if (!effects.length || effects.some(effect => effect.state !== 'committed' || !effect.path || !effect.hash)) return false;
+ if (!effects.length || effects.some(effect => !['committed', 'running'].includes(effect.state) || !effect.path || !effect.hash)) return false;
const root = realpathSync(input.localPath);
const want = new Map(effects.map(effect => [relative(input.gitRoot, join(root, effect.path!)).split(sep).join('/'), effect.hash!]));
// `:<mode> <mode> <oid> <oid> <status>\0<path>\0` per changed path: each must be a file of `want`, now a blob.
diff --git a/test/persistence-sync-fence-holds.test.ts b/test/persistence-sync-fence-holds.test.ts
index 4cfc60f370..fcc1f066b1 100644
--- a/test/persistence-sync-fence-holds.test.ts
+++ b/test/persistence-sync-fence-holds.test.ts
@@ -355,6 +355,28 @@ test('a manifest with one fixable fence, one unfixable fence and clean files: th
expect((await s.sync()).status).toBe('up_to_date');
}), 180_000);
+test('forced probe: the Git effect commits the normalized file before it records the commit, while the next round starts; the pass still ends on its stamp', () => each(async engine => {
+ const s = await source(engine, { 'notes/a.md': note('A'), 'people/fixable.md': fixable() }, {}, true);
+ const head = git(s.root, 'rev-parse', 'HEAD');
+ // The effect's commit is on disk but its row is not yet committed (`effect:git:mid`) when the pass's next round starts.
+ const committed = Promise.withResolvers<void>(), done = Promise.withResolvers<void>();
+ const { installFaultHook } = await import('../src/core/persistence/fault-points.ts');
+ installFaultHook(async (point, detail) => {
+ if (point === 'effect:git:mid' && detail.sourceId === s.id) { committed.resolve(); await Promise.race([done.promise, Bun.sleep(20_000)]); }
+ if (point === 'sync:round_start' && detail.sourceId === s.id && (detail.round ?? 1) > 1) await Promise.race([committed.promise, Bun.sleep(20_000)]);
+ });
+ let result: SyncResult;
+ try { result = await s.sync(); } finally { done.resolve(); installFaultHook(undefined); }
+ expect(result).toMatchObject({ status: 'first_sync', added: 2 });
+ expect(git(s.root, 'log', '-1', '--format=%s')).toBe('gbrain: persist canonical memory update');
+ expect(await s.lastCommit()).toBe(head);
+ // As without the race: the next sync reads gbrain's own commit as a no-op; the one after is up to date.
+ const again = await s.sync();
+ expect(again).toMatchObject({ added: 0, modified: 0 });
+ expect(again.fences_normalized).toBeUndefined();
+ expect((await s.sync()).status).toBe('up_to_date');
+}), 180_000);
+
test('dry run lists the file it would normalize beside the hold and leaves every file byte-identical', () => each(async engine => {
const s = await source(engine, { 'people/fixable.md': fixable(), 'people/malformed.md': MALFORMED, 'notes/ok.md': note('Ok') });
const before = readFileSync(join(s.root, 'people/fixable.md'), 'utf8');