🌊 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(cli): drain memory teardown instead of forcing process exit (#4334)
ordinary being134911452765
diff --git a/ruflo/bin/ruflo.js b/ruflo/bin/ruflo.jsindex ab712ff5f9..8191ad276d 100755--- a/ruflo/bin/ruflo.js+++ b/ruflo/bin/ruflo.js@@ -69,11 +69,23 @@ if (isMCPMode) {description: 'Ruflo - AI Agent Orchestration Platform',});cli.run()- .then(() => {- // #1641/#1653: Exit cleanly after one-shot commands.- // HNSW VectorDb, sql.js WASM, and ONNX worker threads keep the- // event loop alive after the command handler returns.- process.exit(0);+ .then(async () => {+ // #1641/#1653: HNSW VectorDb, sql.js WASM, and ONNX worker threads can+ // keep the event loop alive after the command handler returns. Close the+ // memory ControllerRegistry before relying on the event loop to drain.+ //+ // #3226: process.exit(0) here raced that teardown and intermittently+ // aborted on Windows with a libuv UV_HANDLE_CLOSING assertion after a+ // successful command. Set the exit status and let the loop drain.+ if (process.exitCode === undefined) process.exitCode = 0;+ try {+ const { shutdownBridge } = await import(+ toImportURL(join(cliBase, 'dist', 'src', 'memory', 'memory-bridge.js'))+ );+ if (typeof shutdownBridge === 'function') await shutdownBridge();+ } catch {+ // Best-effort cleanup; never turn a successful command into a failure.+ }}).catch((error) => {console.error('Fatal error:', error.message);diff --git a/v3/@claude-flow/cli/__tests__/memory-list-teardown-3226.test.ts b/v3/@claude-flow/cli/__tests__/memory-list-teardown-3226.test.tsnew file mode 100644index 0000000000..78d37f848d--- /dev/null+++ b/v3/@claude-flow/cli/__tests__/memory-list-teardown-3226.test.ts@@ -0,0 +1,121 @@+/**+ * #3226: `memory list` printed the correct rows and then intermittently+ * aborted on Windows with a libuv UV_HANDLE_CLOSING assertion. The bin+ * wrapper called process.exit(0) immediately after the command resolved,+ * racing the memory ControllerRegistry/sql.js teardown.+ *+ * This is a bin-level regression test: it spawns the built CLI (and the+ * `ruflo` wrapper) repeatedly against a small sql.js store and requires every+ * run to exit 0 with complete output. The store is built directly with+ * sql.js so the test does not depend on a native AgentDB install or download+ * an embedding model.+ */+import { describe, expect, it } from 'vitest';+import { existsSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';+import { tmpdir } from 'node:os';+import { join } from 'node:path';+import { spawnSync } from 'node:child_process';+import { fileURLToPath } from 'node:url';+import { MEMORY_SCHEMA_V3 } from '../src/memory/memory-initializer.js';++const CLI_BIN = fileURLToPath(new URL('../bin/cli.js', import.meta.url));+const CLI_DIST = fileURLToPath(new URL('../dist/src/index.js', import.meta.url));+const RUFLO_BIN = fileURLToPath(new URL('../../../../ruflo/bin/ruflo.js', import.meta.url));+const CLI_BUILT = existsSync(CLI_DIST);+const RUFLO_BUILT = CLI_BUILT && existsSync(RUFLO_BIN);++async function makeStore(): Promise<{ root: string; dbPath: string; cleanup: () => void }> {+ const root = mkdtempSync(join(tmpdir(), 'ruflo-list-teardown-'));+ const dbPath = join(root, 'memory.db');+ const initSqlJs = (await import('sql.js')).default;+ const SQL = await initSqlJs();+ const db = new SQL.Database();+ try {+ db.run(MEMORY_SCHEMA_V3);+ const now = Date.now();+ for (const [i, key] of ['alpha', 'beta', 'gamma'].entries()) {+ db.run(+ `INSERT INTO memory_entries+ (id, key, namespace, content, access_count, created_at, updated_at, status)+ VALUES (?, ?, 'default', ?, 0, ?, ?, 'active')`,+ [`id-${i}`, key, `value for ${key}`, now, now],+ );+ }+ const bytes = db.export();+ writeFileSync(dbPath, Buffer.from(bytes));+ } finally {+ db.close();+ }+ return { root, dbPath, cleanup: () => rmSync(root, { recursive: true, force: true }) };+}++function runMemory(bin: string, cwd: string, args: string[]) {+ return spawnSync(process.execPath, [bin, ...args], {+ cwd,+ encoding: 'utf8',+ timeout: 20_000,+ env: {+ ...process.env,+ // Exercise the sql.js path that the issue reproduces on.+ CLAUDE_FLOW_DISABLE_BRIDGE: '1',+ NO_COLOR: '1',+ FORCE_COLOR: '0',+ },+ });+}++function runList(bin: string, cwd: string, dbPath: string) {+ return runMemory(bin, cwd, ['memory', 'list', '--path', dbPath]);+}++function expectStableListRuns(bin: string, cwd: string, dbPath: string, runs = 15) {+ for (let i = 1; i <= runs; i++) {+ const result = runList(bin, cwd, dbPath);+ const output = `${result.stdout ?? ''}${result.stderr ?? ''}`;++ expect(result.error, `run ${i} failed to spawn: ${result.error?.message}`).toBeUndefined();+ expect(result.signal, `run ${i} was killed: ${output}`).toBeNull();+ expect(result.status, `run ${i} exited ${result.status}:\n${output}`).toBe(0);+ expect(output, `run ${i} omitted the result summary`).toContain('Showing 3 of 3 entries');+ expect(output, `run ${i} hit the libuv teardown assertion`).not.toMatch(+ /UV_HANDLE_CLOSING|Assertion failed|libuv/,+ );+ }+}++describe.skipIf(!CLI_BUILT)('#3226 memory list teardown', () => {+ it('the built CLI exits cleanly on every run', async () => {+ const store = await makeStore();+ try {+ expectStableListRuns(CLI_BIN, store.root, store.dbPath);+ } finally {+ store.cleanup();+ }+ }, 120_000);++ it.skipIf(!RUFLO_BUILT)('the ruflo wrapper exits cleanly on every run', async () => {+ const store = await makeStore();+ try {+ expectStableListRuns(RUFLO_BIN, store.root, store.dbPath);+ } finally {+ store.cleanup();+ }+ }, 120_000);++ it('a one-shot memory write still terminates without the forced exit', async () => {+ const store = await makeStore();+ try {+ const result = runMemory(CLI_BIN, store.root, [+ 'memory', 'store', '-k', 'delta', '--value', 'delta value',+ '--no-embedding', '--path', store.dbPath,+ ]);+ const output = `${result.stdout ?? ''}${result.stderr ?? ''}`;+ expect(result.error, `store failed to spawn: ${result.error?.message}`).toBeUndefined();+ expect(result.status, `store exited ${result.status}:\n${output}`).toBe(0);+ expect(output).toContain('Data stored successfully');+ } finally {+ store.cleanup();+ }+ }, 30_000);+});+diff --git a/v3/@claude-flow/cli/bin/cli.js b/v3/@claude-flow/cli/bin/cli.jsindex 70cb291bb9..6be3c5cf4b 100755--- a/v3/@claude-flow/cli/bin/cli.js+++ b/v3/@claude-flow/cli/bin/cli.js@@ -369,11 +369,23 @@ if (isHarness) {const { CLI } = await import('../dist/src/index.js');const cli = new CLI();cli.run()- .then(() => {- // #1552: Exit cleanly after one-shot commands.- // Long-running commands (daemon foreground, mcp, status --watch) never resolve,- // so this only fires for normal CLI commands.- process.exit(0);+ .then(async () => {+ // #1552: one-shot commands can leave the memory ControllerRegistry's+ // database handles and timers alive. Close them before relying on the+ // event loop to drain.+ //+ // #3226: calling process.exit(0) here used to race that teardown and+ // intermittently abort on Windows with a libuv UV_HANDLE_CLOSING+ // assertion *after* the command had printed a successful result. Set+ // the exit status and let the loop drain instead.+ if (process.exitCode === undefined) process.exitCode = 0;+ try {+ const { shutdownBridge } = await import('../dist/src/memory/memory-bridge.js');+ if (typeof shutdownBridge === 'function') await shutdownBridge();+ } catch {+ // Cleanup is best-effort: it must never turn a successful command into+ // a failure. Commands that do not use memory simply have nothing to do.+ }}).catch((error) => {console.error('Fatal error:', error.message);