🌊 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
TypeScript agentic-aiagentic-frameworkagentic-workflowagentsai-agentsai-assistantai-skillsautonomous-agentsclaudecodexdsh-pluginharnessmcp-servermulti-agentmulti-agent-systemsnpmskillsswarmswarm-intelligencetypescript
fix(events): isolate RVF persisted records from caller mutations
Rudy Celekli7b7eaafb7640
diff --git a/tests/rvf-record-snapshots.test.mjs b/tests/rvf-record-snapshots.test.mjsnew file mode 100644index 0000000000..f44a935d5a--- /dev/null+++ b/tests/rvf-record-snapshots.test.mjs@@ -0,0 +1,31 @@+import { test } from 'node:test';+import assert from 'node:assert/strict';+import { mkdtempSync, rmSync } from 'node:fs';+import { tmpdir } from 'node:os';+import { join } from 'node:path';+import { RvfEventLog } from '../v3/@claude-flow/shared/src/events/rvf-event-log.ts';+for (const mode of ['input', 'reader', 'listener']) test(`events own their persisted payload across ${mode} mutations`, async () => {+ const dir=mkdtempSync(join(tmpdir(),'rvf-owned-')); const log=new RvfEventLog({logPath:join(dir,'events.rvf')});+ try {+ await log.initialize();+ const event={id:'one',aggregateId:'a',aggregateType:'Task',type:'Created',timestamp:1,version:0,payload:{label:'original', omitted:()=> 'not serialized'}};+ if(mode==='listener') log.on('event:appended',e=>e.payload.label='changed');+ await log.append(event);+ if(mode==='input') event.payload.label='changed';+ if(mode==='reader') (await log.getAllEvents())[0].payload.label='changed';+ assert.equal((await log.getEvents('a'))[0].payload.label,'original');+ (await log.getEvents('a'))[0].payload.label='other';+ assert.equal((await log.getAllEvents())[0].payload.label,'original');+ } finally {await log.close();rmSync(dir,{recursive:true,force:true});}+});+for(const mode of ['input','reader','listener']) test(`snapshots own persisted state across ${mode} mutations`,async()=>{+ const dir=mkdtempSync(join(tmpdir(),'rvf-snapshot-'));const log=new RvfEventLog({logPath:join(dir,'events.rvf')});+ try {+ await log.initialize();const snapshot={aggregateId:'a',aggregateType:'Task',version:1,timestamp:1,state:{label:'original'}};+ if(mode==='listener') log.on('snapshot:saved',s=>s.state.label='changed');+ await log.saveSnapshot(snapshot);+ if(mode==='input') snapshot.state.label='changed';+ if(mode==='reader') (await log.getSnapshot('a')).state.label='changed';+ assert.equal((await log.getSnapshot('a')).state.label,'original');+ }finally{await log.close();rmSync(dir,{recursive:true,force:true});}+});diff --git a/v3/@claude-flow/shared/src/events/rvf-event-log.ts b/v3/@claude-flow/shared/src/events/rvf-event-log.tsindex 2ca3b708e2..f8077dd677 100644--- a/v3/@claude-flow/shared/src/events/rvf-event-log.ts+++ b/v3/@claude-flow/shared/src/events/rvf-event-log.ts@@ -172,12 +172,12 @@ export class RvfEventLog extends EventEmitter {event.version = nextVersion;// Persist to disk first (crash-safe ordering)- this.appendRecord(this.config.logPath, event);+ const persisted = this.appendRecord(this.config.logPath, event);// Update in-memory state- this.indexEvent(event);+ this.indexEvent(persisted);- this.emit('event:appended', event);+ this.emit('event:appended', structuredClone(persisted));if (nextVersion % this.config.snapshotThreshold === 0) {this.emit('snapshot:recommended', {@@ -191,10 +191,10 @@ export class RvfEventLog extends EventEmitter {async saveSnapshot(snapshot: EventSnapshot): Promise<void> {this.ensureInitialized();- this.appendRecord(this.snapshotPath, snapshot);- this.snapshots.set(snapshot.aggregateId, snapshot);+ const persisted = this.appendRecord(this.snapshotPath, snapshot);+ this.snapshots.set(persisted.aggregateId, persisted);- this.emit('snapshot:saved', snapshot);+ this.emit('snapshot:saved', structuredClone(persisted));}// ===========================================================================@@ -208,7 +208,7 @@ export class RvfEventLog extends EventEmitter {const indices = this.aggregateIndex.get(aggregateId);if (!indices || indices.length === 0) return [];- let result = indices.map((i) => this.events[i]);+ let result = indices.map((i) => structuredClone(this.events[i]));if (fromVersion !== undefined) {result = result.filter((e) => e.version >= fromVersion);@@ -224,10 +224,10 @@ export class RvfEventLog extends EventEmitter {this.ensureInitialized();if (!filter) {- return [...this.events].sort((a, b) => a.timestamp - b.timestamp);+ return structuredClone(this.events).sort((a, b) => a.timestamp - b.timestamp);}- let result: DomainEvent[] = [...this.events];+ let result: DomainEvent[] = structuredClone(this.events);// Aggregate ID filterif (filter.aggregateIds && filter.aggregateIds.length > 0) {@@ -277,7 +277,7 @@ export class RvfEventLog extends EventEmitter {/** Get latest snapshot for an aggregate. */async getSnapshot(aggregateId: string): Promise<EventSnapshot | null> {this.ensureInitialized();- return this.snapshots.get(aggregateId) ?? null;+ return structuredClone(this.snapshots.get(aggregateId) ?? null);}/** Return event store statistics. */@@ -381,13 +381,15 @@ export class RvfEventLog extends EventEmitter {}/** Append a single record to an RVF file. */- private appendRecord(filePath: string, record: unknown): void {+ private appendRecord<T>(filePath: string, record: T): T {const json = JSON.stringify(record);+ const persisted = JSON.parse(json) as T;const payload = Buffer.from(json, 'utf8');const lengthBuf = Buffer.allocUnsafe(LENGTH_PREFIX_BYTES);lengthBuf.writeUInt32BE(payload.length, 0);appendFileSync(filePath, Buffer.concat([lengthBuf, payload]));+ return persisted;}/** Add an event to the in-memory indexes. */