Skip to content

armgit / ruflo

Public Imported from GitHub
0

🌊 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

fix(cli-core): commit owned JSON memory snapshots atomically

Rudy Celekli2d32d9d4a2ba
diff --git a/tests/json-memory-transactions.test.mjs b/tests/json-memory-transactions.test.mjs
new file mode 100644
index 0000000000..7f07b0bb08
--- /dev/null
+++ b/tests/json-memory-transactions.test.mjs
@@ -0,0 +1,16 @@
+import test from 'node:test';
+import assert from 'node:assert/strict';
+import { mkdtempSync, rmSync, readFileSync } from 'node:fs';
+import { join } from 'node:path';
+import { tmpdir } from 'node:os';
+import { JsonMemoryBackend } from '../v3/@claude-flow/cli-core/src/memory/json-backend.ts';
+async function fixture(run) { const dir = mkdtempSync(join(tmpdir(), 'json-memory-')); const path = join(dir, 'memory.json'); try { await run(new JsonMemoryBackend({ path }), path); } finally { rmSync(dir, { recursive: true, force: true }); } }
+test('stored values and tags do not alias caller inputs', async () => fixture(async b => {
+ const value = { text: 'original' }, tags = ['original']; await b.store('key', value, { tags }); value.text = 'changed'; tags.push('changed'); const entry = await b.retrieve('key'); assert.deepEqual(entry.value, { text: 'original' }); assert.deepEqual(entry.tags, ['original']);
+}));
+test('query result mutation cannot change persisted memory', async () => fixture(async b => {
+ await b.store('key', { text: 'original' }); const rows = await b.list(); rows[0].value.text = 'changed'; assert.deepEqual((await b.retrieve('key')).value, { text: 'original' });
+}));
+test('serialization failure cannot create a phantom cached entry', async () => fixture(async (b, path) => {
+ await b.store('old', 'preserved'); const before = readFileSync(path, 'utf8'); const circular = {}; circular.self = circular; await assert.rejects(b.store('failed', circular)); assert.equal(readFileSync(path, 'utf8'), before); assert.equal(await b.retrieve('failed'), null); assert.equal((await b.retrieve('old')).value, 'preserved');
+}));
diff --git a/v3/@claude-flow/cli-core/src/memory/json-backend.ts b/v3/@claude-flow/cli-core/src/memory/json-backend.ts
index 50fe873ec2..59ca9dcd07 100644
--- a/v3/@claude-flow/cli-core/src/memory/json-backend.ts
+++ b/v3/@claude-flow/cli-core/src/memory/json-backend.ts
@@ -55,7 +55,7 @@ export class JsonMemoryBackend implements MemoryBackend {
}
private load(): FileShape {
- if (this.cache) return this.cache;
+ if (this.cache) return structuredClone(this.cache);
if (!existsSync(this.path)) {
const fresh: FileShape = {
version: 1,
@@ -65,7 +65,7 @@ export class JsonMemoryBackend implements MemoryBackend {
updatedAt: nowIso(),
};
this.cache = fresh;
- return fresh;
+ return structuredClone(fresh);
}
try {
const raw = readFileSync(this.path, 'utf-8');
@@ -74,7 +74,7 @@ export class JsonMemoryBackend implements MemoryBackend {
throw new Error(`Unsupported memory file version: ${parsed.version}`);
}
this.cache = parsed;
- return parsed;
+ return structuredClone(parsed);
} catch (err) {
throw new Error(`memory: failed to load ${this.path}: ${(err as Error).message}`);
}
@@ -82,14 +82,16 @@ export class JsonMemoryBackend implements MemoryBackend {
private save(state: FileShape): void {
state.updatedAt = nowIso();
- this.cache = state;
const dir = dirname(this.path);
if (!existsSync(dir)) {
mkdirSync(dir, { recursive: true });
}
const tmp = `${this.path}.tmp.${process.pid}.${Date.now()}`;
- writeFileSync(tmp, JSON.stringify(state, null, 2), 'utf-8');
+ const serialized = JSON.stringify(state, null, 2);
+ writeFileSync(tmp, serialized, 'utf-8');
renameSync(tmp, this.path);
+ // Commit only the independently owned representation actually persisted.
+ this.cache = JSON.parse(serialized);
}
private isExpired(entry: MemoryEntry): boolean {