Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
test(convergence): drain-lease takeover round with queued followers (#6712 PR 1)
garrytandf9498b4970a
diff --git a/scripts/persistence/README.md b/scripts/persistence/README.mdindex ec2bfc8733..856b09ca6d 100644--- a/scripts/persistence/README.md+++ b/scripts/persistence/README.md@@ -563,6 +563,7 @@ The chaos modes, each named in failures and selectable with| (e) mid-run edit | The `sync:after_freeze` hook writes an uncommitted edit to a seeded entry after it was frozen | the run's receipt and `sync status` name `worktree_dirty`; after the editor commits, strict convergence | skipped until PR 1 of #6712: no `worktree_dirty` hold is named, and the run churns through `source_changed` conflicts and cancellations (5 of 20 entries after 150 s) || (f) pooler drop | Every pooled connection drops at the proxy after 20 committed pages of a bulk run (`GBRAIN_TEST_CONVERGENCE_PGBOUNCER_CONTAINER=<name>` restarts that container instead) | no new hold; the drain resumes within the owner-kill stall bound; strict convergence | skipped until PR 1 of #6712: it converges with no hold, but publication stalls 356 s (a publication lease) after the drop || (g) two drainers | `sync --all --no-delegate` beside a serve draining a delegated sync of the same source | strict convergence | runs |+| (lease) drain-lease takeover | A `--no-delegate` leader stalls right after it takes the source's drain lease (`sync:lease_acquired`), two `--no-delegate` followers queue behind it, and the leader is SIGKILLed | a follower reports following, one takes over inside the lease TTL (2 min) and finishes `synced`, no follower ends `blocked`; strict convergence | runs (#6712 PR 1) || (knock-on) | One page's publication throws P0001 once inside a bulk group, then a trigger refuses one page with P0001 for a whole run | no other page of the run is cancelled; strict convergence once the trigger is gone | skipped until PR 1 of #6712: the one-shot throw is retried and cancels nothing, but the refused page cancels the run's other 74 requests, and both plain syncs after the trigger is dropped stop `blocked_by_failures` on the frozen `storage_error` request until `--retry-failed` || (systemic) | A trigger on `pages` raises an unregistered P0001 (one scenario) or 55000 (another) on every page write for one run, then is dropped | the refused run does not report success and its receipt names the SQLSTATE; strict convergence once the trigger is gone | skipped until PR 1 of #6712: the refused run stops `blocked` naming the SQLSTATE, but both plain syncs after the trigger is dropped stay `blocked_by_failures` until `--retry-failed` |diff --git a/test/sync-convergence.serial.test.ts b/test/sync-convergence.serial.test.tsindex 26b7067d91..5efef97d07 100644--- a/test/sync-convergence.serial.test.ts+++ b/test/sync-convergence.serial.test.ts@@ -112,6 +112,7 @@ const MODES = {e: { title: 'a deterministic mid-run edit after freezeEntry', skip: PR1 },f: { title: 'a pooler drop mid-bulk', skip: PR1 },g: { title: 'two concurrent drainers: a --all run beside a serve', skip: null },+ lease: { title: 'the drain-lease holder killed with two followers queued behind it', skip: null },'knock-on': { title: 'one page failure inside a bulk group', skip: PR1 },systemic: { title: 'a systemic database refusal: an unknown P0001 or a 55000 from a trigger on every page write', skip: PR1 },} as const satisfies Record<string, { title: string; skip: string | null }>;@@ -806,7 +807,7 @@ describe('convergence harness calibration (#6650 L0, #6712 PR 0)', () => {});test('modes master fails stay written and name the PR that turns them on', () => {for (const mode of ['e', 'f', 'knock-on', 'systemic'] as const) expect(MODES[mode].skip).toContain('PR 1 of #6712');- for (const mode of ['a', 'b', 'c', 'c-db', 'd', 'g'] as const) expect(MODES[mode].skip).toBeNull();+ for (const mode of ['a', 'b', 'c', 'c-db', 'd', 'g', 'lease'] as const) expect(MODES[mode].skip).toBeNull();});});@@ -1079,6 +1080,43 @@ scenario(['g'], 'a --all run beside a serve draining the same source converges',oracleTests(b, () => ({ bigPage: false }));});+// ---------------------------------------------------------------- (lease): the drain-lease holder dies with followers queued++scenario(['lease'], 'a killed drain-lease holder is taken over by a queued follower and the source converges', () => {+ const b = new Brain('lease-takeover', ['lease']);+ const notes = Math.min(NOTES, 200);+ const followers: Array<{ code: number; envelope: Record<string, unknown> | null; followed: boolean }> = [];+ beforeAll(async () => {+ await b.setup(brain => brain.writeNotes(Array.from({ length: notes }, (_, i) => i), 'Initial', 'fixture'));+ b.writeNotes(Array.from({ length: Math.min(notes, 120) }, (_, i) => i), 'Round one', 'round one');+ const tag = 'leader', release = join(b.home, 'release-leader');+ b.plan = ['leader stalls at sync:lease_acquired #1; two --no-delegate followers queue behind it; the leader is SIGKILLed'];+ const leader = b.sync(['--no-pull', '--no-delegate', '--json'], { point: 'sync:lease_acquired', nth: 1, marker: b.marker(tag), action: 'stall', release });+ await until(() => existsSync(b.marker(tag)), 'the leader to take the drain lease', SYNC_TIMEOUT_MS);+ const queued = [0, 1].map(() => b.sync(['--no-pull', '--no-delegate', '--json', '--timeout', '10m']));+ // Both followers must be following the live leader before it dies (they print the holder once they see it).+ await Bun.sleep(5_000);+ leader.proc.kill('SIGKILL'); b.killedPids.add(leader.proc.pid);+ const killed = await leader.result;+ b.keepLog('leader.log', `${killed.out}\n${killed.err}`);+ b.recordFault(tag, b.plan[0]);+ for (const [i, run] of queued.entries()) {+ const r = await run.result;+ b.keepLog(`follower-${i + 1}.log`, `${r.out}\n${r.err}`);+ followers.push({ code: r.code, envelope: lastJson(r.out), followed: r.err.includes('following it until it finishes') });+ b.note(`follower ${i + 1}: exit ${r.code} in ${(r.ms / 1000).toFixed(1)}s outcome=${String(lastJson(r.out)?.outcome ?? 'none')}`);+ }+ await b.recover();+ }, SCENARIO_TIMEOUT_MS);+ afterAll(() => b.teardown(), 120_000);+ test(as('wedge', 'a follower takes over the dead holder inside the lease TTL; no follower ends blocked'), () => b.verdict([+ ...(followers.some(f => f.followed) ? [] : ['no follower reported following the live leader']),+ ...(followers.some(f => f.code === 0 && f.envelope?.outcome === 'synced') ? [] : [`no follower finished the source: ${JSON.stringify(followers.map(f => [f.code, f.envelope?.outcome]))}`]),+ ...followers.filter(f => f.envelope?.outcome === 'blocked').map(f => `a follower ended blocked: ${JSON.stringify(f.envelope?.next ?? f.envelope).slice(0, 400)}`),+ ]));+ oracleTests(b, () => ({ bigPage: false }));+});+// ---------------------------------------------------------------- (systemic): a refusal on every page writefor (const sqlstate of ['P0001', '55000'] as const) {