Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

refresh: complete by ancestry once members synced to the target or a descendant (#6712 wave)

garrytan556d4e862001
diff --git a/src/core/persistence/worktree-refresh.ts b/src/core/persistence/worktree-refresh.ts
index 5d25eaef98..107b0be5f2 100644
--- a/src/core/persistence/worktree-refresh.ts
+++ b/src/core/persistence/worktree-refresh.ts
@@ -313,15 +313,19 @@ async function bookkeeping(engine: BrainEngine, row: WorktreeRefreshRow): Promis
});
}
-/** Steps 6-7: the managed `--no-pull` sync per member, then completion when every member is at target_head. */
-async function syncMembers(engine: BrainEngine, git: Git, row: WorktreeRefreshRow, root: string): Promise<{ synced: RefreshMemberSync[]; blocked: RefreshSyncBlocked[] }> {
+/** #6712: a member has synced the refresh once its `last_commit` is the target or a descendant of it (a commit landed on top of the target). */
+async function reachedTarget(git: Git, root: string, row: WorktreeRefreshRow, commit: string | null): Promise<boolean> {
+ return commit === row.target_head || (!!commit && commit !== row.old_head && await isAncestor(git, root, row.target_head, commit));
+}
+
+/** Steps 6-7: the managed `--no-pull` sync per member, then completion when every member reached target_head (or a descendant). */
+async function syncMembers(engine: BrainEngine, git: Git, row: WorktreeRefreshRow, root: string): Promise<{ synced: RefreshMemberSync[]; blocked: RefreshSyncBlocked[]; atTarget: boolean }> {
const { performManagedSync } = await import('./sync-run.ts');
const synced: RefreshMemberSync[] = [], blocked: RefreshSyncBlocked[] = [];
for (const sourceId of row.source_ids) {
let status = 'up_to_date';
for (let pass = 0; pass < MAX_MEMBER_SYNC_PASSES; pass++) {
- const commit = await lastCommit(engine, sourceId);
- if (commit === row.target_head || (commit && commit !== row.old_head && await isAncestor(git, root, row.target_head, commit))) break;
+ if (await reachedTarget(git, root, row, await lastCommit(engine, sourceId))) break;
let result: SyncResult;
try { result = await performManagedSync(engine, { sourceId, noPull: true }); }
catch (error) {
@@ -342,18 +346,19 @@ async function syncMembers(engine: BrainEngine, git: Git, row: WorktreeRefreshRo
await recordUpstreamObservation(engine, sourceId);
synced.push({ source_id: sourceId, status, last_commit: await lastCommit(engine, sourceId) });
}
- const atTarget = synced.every(member => member.last_commit === row.target_head);
+ let atTarget = true;
+ for (const member of synced) if (!await reachedTarget(git, root, row, member.last_commit)) { atTarget = false; break; }
if (atTarget || blocked.length) await engine.transaction(tx => transition(tx, row, ['syncing'], 'completed',
{ synced, ...(blocked.length ? { sync_blocked: blocked } : {}) }));
- return { synced, blocked };
+ return { synced, blocked, atTarget };
}
async function lastCommit(engine: BrainEngine, sourceId: string): Promise<string | null> {
const [row] = await engine.executeRaw<{ last_commit: string | null }>('SELECT last_commit FROM sources WHERE id=$1', [sourceId]);
return row?.last_commit ?? null;
}
-function finished(row: WorktreeRefreshRow, sync: { synced: RefreshMemberSync[]; blocked: RefreshSyncBlocked[] }, preserved = row.preserved_uncommitted): WorktreeRefreshResult {
- const status = sync.blocked.length ? 'sync_blocked' : sync.synced.every(member => member.last_commit === row.target_head) ? 'completed' : 'syncing';
+function finished(row: WorktreeRefreshRow, sync: { synced: RefreshMemberSync[]; blocked: RefreshSyncBlocked[]; atTarget: boolean }, preserved = row.preserved_uncommitted): WorktreeRefreshResult {
+ const status = sync.blocked.length ? 'sync_blocked' : sync.atTarget ? 'completed' : 'syncing';
return { status, refresh_id: row.id, worktree_id: row.worktree_id, source_ids: row.source_ids, old_head: row.old_head, target_head: row.target_head,
upstream_ref: row.upstream_ref, preserved_uncommitted: preserved, synced: sync.synced,
...(sync.blocked.length ? { sync_blocked: sync.blocked, next: sync.blocked.map(item => item.resume).join(' ; ') } : {}),
diff --git a/test/persistence-worktree-refresh.test.ts b/test/persistence-worktree-refresh.test.ts
index 808048102c..73cccaa1f9 100644
--- a/test/persistence-worktree-refresh.test.ts
+++ b/test/persistence-worktree-refresh.test.ts
@@ -41,7 +41,7 @@ import { _resetCliExitVerdictForTests, currentExitCode } from '../src/core/cli-f
import { isolatedPersistencePostgres } from './helpers/persistence-postgres.ts';
import { testBackends } from './helpers/test-backends.ts';
import { withEnv } from './helpers/with-env.ts';
-import { git, page, withRefreshFixture, type RefreshFixture } from './helpers/worktree-refresh-fixture.ts';
+import { commitFile, git, page, withRefreshFixture, type RefreshFixture } from './helpers/worktree-refresh-fixture.ts';
const backends = testBackends();
const engines: BrainEngine[] = [];
@@ -442,3 +442,24 @@ test('admission completes a syncing refresh whose members all reached the target
expect((await f.put(f.alpha, 'notes/caught-up', page('Caught up', 'admitted'))).state ?? 'committed').toBe('committed');
expect((await f.refreshRows()).map(r => r.state)).toEqual(['completed']);
}), 120_000);
+
+test('#6712 wave: a refresh completes once every member synced to a descendant of the target, and --resume completes it the same way', () => each(async f => {
+ const target = f.push('alpha/two.md', page('Alpha two', 'Upstream.'));
+ let descendant = '';
+ // A commit lands on top of the fast-forwarded target before the member syncs run, so each member syncs past the target.
+ const result = await refreshWorktree(f.engine, f.alpha, { hooks: { boundary: point => {
+ if (point === 'merged') descendant = commitFile(f.root, 'alpha/three.md', page('Alpha three', 'Committed on top of the target.'));
+ } } });
+ expect(git(f.root, 'merge-base', '--is-ancestor', target, descendant) === '' && descendant !== target).toBe(true);
+ expect(await f.lastCommit(f.alpha)).toBe(descendant);
+ expect(await f.lastCommit(f.beta)).toBe(descendant);
+ expect(result).toMatchObject({ status: 'completed', target_head: target });
+ expect(result.next).toBeUndefined();
+ expect((await f.refreshRows()).map(r => r.state)).toEqual(['completed']);
+ expect((await f.put(f.alpha, 'notes/after-descendant', page('After', 'admitted'))).state ?? 'committed').toBe('committed');
+ // A refresh left in syncing (its process died) whose members already synced past the target completes on --resume.
+ const [row] = await f.refreshRows();
+ await f.engine.executeRaw(`UPDATE persistence_worktree_refreshes SET state='syncing',completed_at=NULL WHERE id=$1::uuid`, [row.id]);
+ expect(await refreshWorktree(f.engine, f.alpha, { resume: true })).toMatchObject({ status: 'completed', refresh_id: row.id });
+ expect((await f.refreshRows()).map(r => r.state)).toEqual(['completed']);
+}), 180_000);