🌊 The original agent harness. Deploy intelligent multi-player swarms, coordinate autonomous workflows, and build conversational AI systems. Features adaptive memory, self-learning intelligence, federation, vector RAG integration, and native Claude Code / Codex / Hermes and many more Integrated
TypeScript agentic-aiagentic-frameworkagentic-workflowagentsai-agentsai-assistantai-skillsautonomous-agentsclaudecodexdsh-pluginharnessmcp-servermulti-agentmulti-agent-systemsnpmskillsswarmswarm-intelligencetypescript
fix(memory): make the #3693 graph-writer regression test hermetic against vitest's mock-queue race
ruvbc7edd51fbca
diff --git a/v3/@claude-flow/cli/__tests__/graph-writer-shared-sqlite-3693.test.ts b/v3/@claude-flow/cli/__tests__/graph-writer-shared-sqlite-3693.test.tsindex b719ee9fbf..2a18a1de09 100644--- a/v3/@claude-flow/cli/__tests__/graph-writer-shared-sqlite-3693.test.ts+++ b/v3/@claude-flow/cli/__tests__/graph-writer-shared-sqlite-3693.test.ts@@ -5,8 +5,19 @@** Deterministic: real native modules (a second copy of the built binary is* placed in a temp "agentdb" tree), fake timers drive the idle close.+ *+ * Mock hermeticity (the flake this file used to have): vitest queues every+ * vi.doMock/vi.doUnmock and applies the whole queue with+ * `Promise.all(pending.map(async (m) => { await resolvePath(...); apply(m) }))`+ * on the next import, so two queued entries for ONE path are applied in+ * resolve-completion order, not call order. The old `afterEach(doUnmock)` ++ * next-test `doMock` pair therefore occasionally applied the unmock LAST, the+ * writer silently loaded the real shared-sqlite (a different native copy than+ * the holder's) and the sidecars were deleted. Never queue two entries for the+ * same path: setSharedSqliteMock() drains the queue between them, and the+ * `adversarial scheduler` below makes the bad order happen on every run.*/-import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from 'vitest';+import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';import { createRequire } from 'node:module';import { cpSync, existsSync, mkdirSync, mkdtempSync, rmSync, statSync, symlinkSync, writeFileSync } from 'node:fs';import { dirname, join } from 'node:path';@@ -34,7 +45,44 @@ beforeAll(() => {} catch { native = false; }});-afterEach(() => { vi.useRealTimers(); vi.resetModules(); vi.doUnmock('../src/memory/shared-sqlite.js'); });+const SHARED_SQLITE = '../src/memory/shared-sqlite.js';++/**+ * Install (factory) or clear (no factory) the shared-sqlite mock without ever+ * leaving two queued entries for the same path: the import below forces vitest+ * to apply the unmock completely before the mock is queued.+ */+async function setSharedSqliteMock(factory?: () => unknown) {+ vi.resetModules();+ vi.doUnmock(SHARED_SQLITE);+ await import(SHARED_SQLITE);+ vi.resetModules();+ if (factory) vi.doMock(SHARED_SQLITE, factory as never);+}++// Adversarial scheduler: inside one mock-queue drain, the FIRST queued entry+// resolves LAST. That is the order that used to lose the mock intermittently;+// here it is forced, so a doUnmock queued before a doMock can never pass.+const mocker: any = (globalThis as any).__vitest_mocker__;+const adversary = typeof mocker?.resolveMocks === 'function' && typeof mocker?.resolvePath === 'function';+const realResolveMocks = adversary ? mocker.resolveMocks : null;+const realResolvePath = adversary ? mocker.resolvePath : null;+let drainCalls = 0;+const realSetTimeout = globalThis.setTimeout; // fake timers replace the global; the scheduler must not+beforeEach(() => {+ if (!adversary) return;+ mocker.resolveMocks = function (this: unknown, ...a: unknown[]) { drainCalls = 0; return realResolveMocks.apply(this, a); };+ mocker.resolvePath = async function (this: unknown, ...a: unknown[]) {+ if (drainCalls++ === 0) await new Promise((r) => realSetTimeout(r, 40));+ return realResolvePath.apply(this, a);+ };+});++afterEach(async () => {+ vi.useRealTimers();+ await setSharedSqliteMock(); // clear any mock, drained, so the next test starts from the real module+ if (adversary) { mocker.resolveMocks = realResolveMocks; mocker.resolvePath = realResolvePath; }+});const ino = (f: string) => (existsSync(f) ? statSync(f).ino : null);@@ -47,16 +95,16 @@ async function scenario(useShared: boolean) {const before = [ino(dbPath + '-wal'), ino(dbPath + '-shm')];process.env.CLAUDE_FLOW_GRAPH_EDGE_IDLE_MS = '50';- if (useShared) {- vi.doMock('../src/memory/shared-sqlite.js', () => ({- loadBetterSqlite3: async () => HolderCtor,- resolveAgentdbBetterSqlite3: () => HolderCtor,- }));- }+ await setSharedSqliteMock(useShared ? () => ({+ loadBetterSqlite3: async () => HolderCtor,+ resolveAgentdbBetterSqlite3: () => HolderCtor,+ }) : undefined);vi.useFakeTimers();const gw = await import('../src/memory/graph-edge-writer.js');const db = await gw.getBridgeDb(dbPath);expect(db).not.toBeNull();+ // The writer must really be on the copy this scenario intends (never a silent fallback).+ expect(db instanceof HolderCtor).toBe(useShared);db.prepare('insert into memory_entries values(2)').run();vi.advanceTimersByTime(60); // idle release fires: checkpoint(TRUNCATE) + closevi.useRealTimers();@@ -70,6 +118,23 @@ async function scenario(useShared: boolean) {afterAll(() => { try { rmSync(root, { recursive: true, force: true }); } catch { /* */ } });describe('#3693 graph writer idle close vs live AgentDB handle', () => {+ it.skipIf(!adversary)('mock harness: the adversarial scheduler really inverts a naive doUnmock -> doMock pair', async () => {+ // Pins the vitest behaviour the helper exists for. If vitest ever applies+ // queued mocks in call order this fails: delete the scheduler + helper then.+ vi.resetModules();+ vi.doUnmock(SHARED_SQLITE);+ vi.doMock(SHARED_SQLITE, () => ({ loadBetterSqlite3: async () => 'mocked' }) as never);+ const m: any = await import(SHARED_SQLITE);+ expect(await m.loadBetterSqlite3()).not.toBe('mocked'); // mock lost: the real module was served+ });++ it.skipIf(!adversary)('mock harness: setSharedSqliteMock survives the same adversarial order', async () => {+ const marker = function Marker() {};+ await setSharedSqliteMock(() => ({ loadBetterSqlite3: async () => marker }));+ const m: any = await import(SHARED_SQLITE);+ expect(await m.loadBetterSqlite3()).toBe(marker);+ });+it('helper resolves the constructor from the agentdb dependency owner', async () => {if (!native) return;const { resolveAgentdbBetterSqlite3 } = await import('../src/memory/shared-sqlite.js');