Merge branch 'pr-4020' into batch/rudycelekli-fixes

This commit is contained in:
r committed 2026-10-11 10:45:18 -04:00
commit 2940c91fcd
2 files changed
+23 -5

No files matched your search

+16
View File
@@ -0,0 +1,16 @@
import { test } from 'vitest';
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');
}));
@@ -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 {