From 2d32d9d4a2ba6264920949498a4c28d7c8b0a4e8 Mon Sep 17 00:00:00 2001 From: Rudy Celekli Date: Sat, 10 Oct 2026 12:21:44 -0400 Subject: [PATCH 1/2] fix(cli-core): commit owned JSON memory snapshots atomically Signed-off-by: Rudy Celekli --- tests/json-memory-transactions.test.mjs | 16 ++++++++++++++++ .../cli-core/src/memory/json-backend.ts | 12 +++++++----- 2 files changed, 23 insertions(+), 5 deletions(-) create mode 100644 tests/json-memory-transactions.test.mjs 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 { From bca0329e17f6611566c78c46ae0287f15dacce31 Mon Sep 17 00:00:00 2001 From: Rudy Celekli Date: Sat, 10 Oct 2026 15:34:59 -0400 Subject: [PATCH 2/2] test: run focused regressions with the repository Vitest runner Signed-off-by: Rudy Celekli --- tests/json-memory-transactions.test.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/json-memory-transactions.test.mjs b/tests/json-memory-transactions.test.mjs index 7f07b0bb08..09824a1d94 100644 --- a/tests/json-memory-transactions.test.mjs +++ b/tests/json-memory-transactions.test.mjs @@ -1,4 +1,4 @@ -import test from 'node:test'; +import { test } from 'vitest'; import assert from 'node:assert/strict'; import { mkdtempSync, rmSync, readFileSync } from 'node:fs'; import { join } from 'node:path';