From 795a156dd1357cdc0ed9f980787ba9945474701f Mon Sep 17 00:00:00 2001 From: Sachin Sharma Date: Tue, 17 Nov 2026 11:09:43 -0800 Subject: [PATCH] fix(memory): exit non-zero when a refused sql.js save loses writes (#4839) bin/cli.js swallowed every error from shutdownBridge(), so a refused sql.js save (AGENTDB_LOCK_UNRECOVERABLE) exited 0 and the write was lost. Only that error now prints a clear message and sets exit code 1. Refs ruvnet/ruflo#4733 Co-authored-by: rUv --- .../memory-store-refused-save-4733.test.ts | 65 +++++++++++++++++++ v3/@claude-flow/cli/bin/cli.js | 11 +++- 2 files changed, 73 insertions(+), 3 deletions(-) create mode 100644 v3/@claude-flow/cli/__tests__/memory-store-refused-save-4733.test.ts diff --git a/v3/@claude-flow/cli/__tests__/memory-store-refused-save-4733.test.ts b/v3/@claude-flow/cli/__tests__/memory-store-refused-save-4733.test.ts new file mode 100644 index 0000000000..c4f5f25897 --- /dev/null +++ b/v3/@claude-flow/cli/__tests__/memory-store-refused-save-4733.test.ts @@ -0,0 +1,65 @@ +/** + * ruvnet/ruflo#4733 — `memory store` must not report success for a write the + * sql.js fallback could not save. + * + * Runs real CLI processes in parallel against one project directory. Each + * process must either exit 0 with its row retrievable, or exit non-zero with a + * message saying the write was not saved. + */ +import { describe, it, expect } from 'vitest'; +import { execFile } from 'child_process'; +import { mkdtempSync, rmSync } from 'fs'; +import { tmpdir } from 'os'; +import { join, dirname } from 'path'; +import { fileURLToPath } from 'url'; + +const CLI = join(dirname(fileURLToPath(import.meta.url)), '..', 'bin', 'cli.js'); +const PARALLEL_STORES = 8; + +interface CliRun { + code: number; + stdout: string; + stderr: string; +} + +function runCli(args: string[], projectDir: string): Promise { + return new Promise((resolve) => { + execFile( + process.execPath, + [CLI, ...args], + { cwd: projectDir, env: { ...process.env, CLAUDE_FLOW_CWD: projectDir }, timeout: 120_000 }, + (error, stdout, stderr) => { + const code = error && typeof error.code === 'number' ? error.code : error ? 1 : 0; + resolve({ code, stdout: String(stdout), stderr: String(stderr) }); + }, + ); + }); +} + +describe('memory store with a refused sql.js save (#4733)', () => { + it(`never reports success for a row that did not persist (${PARALLEL_STORES} parallel stores)`, async () => { + const projectDir = mkdtempSync(join(tmpdir(), 'ruflo-4733-')); + const namespace = 'refused-save'; + try { + const keys = Array.from({ length: PARALLEL_STORES }, (_, i) => `k${i}`); + const stores = await Promise.all( + keys.map((key) => + runCli(['memory', 'store', '--key', key, '--value', `v-${key}`, '--namespace', namespace], projectDir), + ), + ); + + for (const [i, run] of stores.entries()) { + const key = keys[i]; + if (run.code === 0) { + const retrieved = await runCli(['memory', 'retrieve', '--key', key, '--namespace', namespace], projectDir); + expect(retrieved.stdout, `exit 0 for ${key} but the row is not retrievable`).toContain(`v-${key}`); + } else { + expect(run.code).toBe(1); + expect(run.stderr, `non-zero exit for ${key} must explain the lost write`).toMatch(/not saved/i); + } + } + } finally { + rmSync(projectDir, { recursive: true, force: true }); + } + }, 300_000); +}); diff --git a/v3/@claude-flow/cli/bin/cli.js b/v3/@claude-flow/cli/bin/cli.js index edbc8ed60d..407c220bbb 100755 --- a/v3/@claude-flow/cli/bin/cli.js +++ b/v3/@claude-flow/cli/bin/cli.js @@ -377,9 +377,14 @@ if (isHarness) { 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) { + // Cleanup is best-effort for everything except a failed persist: when + // the sql.js save is refused (#4733), the command's writes never reached + // disk, so it must not exit 0. Other teardown errors stay silent. + if (error?.code === 'AGENTDB_LOCK_UNRECOVERABLE') { + console.error(`Error: ${error.message}`); + process.exitCode = 1; + } } }) .catch((error) => {