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 <ruv@ruv.net>
This commit is contained in:
Sachin SharmaandrUv authored and GitHub committed 2026-11-17 14:09:43 -05:00
1 parent 34a1eac1ce
commit 795a156dd1
2 files changed
+73 -3

No files matched your search

@@ -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<CliRun> {
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);
});
+8 -3
View File
@@ -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) => {