Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

test: stop the member sync's consumer before the refresh releases the polled write (refresh-barrier-gst1623 item 2 flake)

garrytanaf30306da647
diff --git a/src/core/persistence/worktree-refresh.ts b/src/core/persistence/worktree-refresh.ts
index b5ecd37202..7903c8bb5e 100644
--- a/src/core/persistence/worktree-refresh.ts
+++ b/src/core/persistence/worktree-refresh.ts
@@ -56,7 +56,7 @@ export interface WorktreeRefreshRow {
topology_generation: string | number; state: WorktreeRefreshState; old_head: string; target_head: string; upstream_ref: string;
preserved_uncommitted: string[]; outcome: Record<string, unknown>; created_at: string | Date; updated_at: string | Date; completed_at: string | Date | null;
}
-export type RefreshBoundary = 'drained' | 'fenced' | 'merged' | 'syncing';
+export type RefreshBoundary = 'drained' | 'fenced' | 'merged' | 'syncing' | 'synced';
export interface RefreshHooks { boundary?: (point: RefreshBoundary, row: WorktreeRefreshRow) => Promise<void> | void }
export interface RefreshOptions {
dryRun?: boolean; resume?: boolean; abandon?: boolean;
@@ -597,7 +597,7 @@ async function reachedTarget(git: Git, root: string, row: WorktreeRefreshRow, co
}
/** 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, budgetMs: number): Promise<{ synced: RefreshMemberSync[]; blocked: RefreshSyncBlocked[]; atTarget: boolean }> {
+async function syncMembers(engine: BrainEngine, git: Git, row: WorktreeRefreshRow, root: string, budgetMs: number, hooks?: RefreshHooks): Promise<{ synced: RefreshMemberSync[]; blocked: RefreshSyncBlocked[]; atTarget: boolean }> {
const sync = await boundedMemberSync(budgetMs);
const synced: RefreshMemberSync[] = [], blocked: RefreshSyncBlocked[] = [];
for (const [index, sourceId] of row.source_ids.entries()) {
@@ -627,6 +627,7 @@ async function syncMembers(engine: BrainEngine, git: Git, row: WorktreeRefreshRo
// #8072: a deferred member completes the refresh too: the checkout is at the target and the member's next sync resumes from
// its last_commit, while a row left syncing would refuse every other write to the worktree until then.
const deferred = synced.filter(member => member.status === 'deferred').map(member => member.source_id);
+ await hooks?.boundary?.('synced', row);
if (atTarget || blocked.length || deferred.length) await engine.transaction(tx => transition(tx, row, ['syncing'], 'completed',
{ synced, ...(blocked.length ? { sync_blocked: blocked } : {}), ...(deferred.length ? { sync_deferred: deferred } : {}) }));
return { synced, blocked, atTarget };
@@ -837,7 +838,7 @@ async function refreshOnce(engine: BrainEngine, sourceId: string, opts: RefreshO
await opts.hooks?.boundary?.('drained', row);
const merged = await fenceAndMerge(engine, git, sourceId, plan, row, opts.hooks);
await opts.hooks?.boundary?.('syncing', merged);
- const sync = await syncMembers(engine, git, merged, plan.root, syncBudgetMs);
+ const sync = await syncMembers(engine, git, merged, plan.root, syncBudgetMs, opts.hooks);
const result = finished(await readRefresh(engine, row.id), sync, plan.preserved);
return plan.published ? { ...result, published: plan.published } : result;
} finally { await refreshLock.release(); }
diff --git a/test/refresh-barrier-gst1623.test.ts b/test/refresh-barrier-gst1623.test.ts
index e0cdbbd341..7de2e4c070 100644
--- a/test/refresh-barrier-gst1623.test.ts
+++ b/test/refresh-barrier-gst1623.test.ts
@@ -29,7 +29,7 @@ import { _resetRefreshBarrierCacheForTests, deferredReplayRefusal, expireDeferre
import { submitPageMutation } from '../src/core/persistence/page-mutations.ts';
import { prepareFileTarget } from '../src/core/persistence/page-prepare.ts';
import { performManagedSync } from '../src/core/persistence/sync-run.ts';
-import { refreshWorktree, resumeWorktreeRefreshes } from '../src/core/persistence/worktree-refresh.ts';
+import { refreshWorktree, resumeWorktreeRefreshes, type RefreshBoundary } from '../src/core/persistence/worktree-refresh.ts';
import { _transientDelayForTests } from '../src/core/persistence/sync-drain.ts';
import { deleteSourceRow } from '../src/core/source-delete.ts';
import { OperationError } from '../src/core/ops/contract.ts';
@@ -100,12 +100,13 @@ const ownerHost = async (f: RefreshFixture) => (await f.engine.executeRaw<{ owne
/** Drives this process's consumer on `row` for up to `ms`: until it settles, or (a write held again) until the wait ends. */
const drive = async (f: RefreshFixture, row: WriteRequest, ms = 5_000) => waitForWrite(f.engine, (await getWriteRequestById(f.engine, row.id))!, { engine: f.engine.kind } as never, ms);
-/** Runs `fn` while a real refresh of the checkout is paused at its drained boundary; the refresh changes alpha/two.md by default. */
-async function duringRefresh(f: RefreshFixture, fn: () => Promise<void>, change: [string, string] = ['alpha/two.md', page('Alpha two', 'An upstream observation.')]) {
+/** Runs `fn` while a real refresh of the checkout is paused at its drained boundary; the refresh changes alpha/two.md by default. `at` runs at every later boundary. */
+async function duringRefresh(f: RefreshFixture, fn: () => Promise<void>, change: [string, string] = ['alpha/two.md', page('Alpha two', 'An upstream observation.')],
+ at?: (point: RefreshBoundary) => Promise<void>) {
f.push(...change);
let release!: () => void; const gate = new Promise<void>(resolve => { release = resolve; });
let reached!: () => void; const drained = new Promise<void>(resolve => { reached = resolve; });
- const refresh = refreshWorktree(f.engine, f.alpha, { hooks: { boundary: async point => { if (point === 'drained') { reached(); await gate; } } } });
+ const refresh = refreshWorktree(f.engine, f.alpha, { hooks: { boundary: async point => { if (point === 'drained') { reached(); await gate; } else await at?.(point); } } });
await drained;
try { await fn(); } catch (error) { release(); await refresh.catch(() => undefined); throw error; }
release();
@@ -476,6 +477,8 @@ test('item 2 (confirmation): polling a receipt never publishes a released deferr
const refresh = await duringRefresh(f, async () => {
deferred = await requestRow(f, (await call(f, 'put_page', { slug: 'notes/polled', content: page('Polled', 'deferred') })).body.request_id);
await disposePersistenceConsumer(f.engine);
+ }, undefined, async point => {
+ if (point === 'synced') await disposePersistenceConsumer(f.engine);
});
expect((await refresh).status).toBe('completed');
for (let i = 0; i < 3; i++) await call(f, 'get_write_request', { request_id: deferred.request_id });