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(events): preserve read-only RVF history during tail recovery

Rudy Celeklie13418db8a96
diff --git a/tests/rvf-truncated-tail-recovery.test.mjs b/tests/rvf-truncated-tail-recovery.test.mjs
index 42fc47502d..36b2b5b4b1 100644
--- a/tests/rvf-truncated-tail-recovery.test.mjs
+++ b/tests/rvf-truncated-tail-recovery.test.mjs
@@ -1,6 +1,8 @@
-import { test } from 'vitest';
+import * as fs from 'node:fs';
+vi.mock('node:fs', async importOriginal => { const actual = await importOriginal(); return { ...actual, truncateSync: vi.fn(actual.truncateSync) }; });
+import { test, vi } from 'vitest';
import assert from 'node:assert/strict';
-import { mkdtempSync, appendFileSync, rmSync } from 'node:fs';
+import { mkdtempSync, appendFileSync, rmSync, readFileSync, chmodSync } 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';
@@ -14,3 +16,37 @@ for(const tail of [Buffer.from([0,0]),Buffer.from([0,0,0,100,123])]) test(`new e
assert.deepEqual((await log.getEvents('a')).map(e=>e.id),['1','2']);
}finally{await log.close();rmSync(dir,{recursive:true,force:true});}
});
+
+for (const code of ['EACCES', 'EROFS', 'EPERM']) test(`reads an unrepaired ${code} log without permitting appends`, async () => {
+ const dir = mkdtempSync(join(tmpdir(), 'rvf-readonly-tail-')); const path = join(dir, 'events.rvf');
+ let log = new RvfEventLog({ logPath: path });
+ const event = id => ({ id, aggregateId: 'a', aggregateType: 'Task', type: 'Created', timestamp: Number(id), version: 0, payload: {} });
+ try {
+ await log.initialize(); await log.append(event('1')); await log.close(); appendFileSync(path, Buffer.from([0, 0]));
+ const original = readFileSync(path);
+ vi.mocked(fs.truncateSync).mockImplementationOnce(() => { throw Object.assign(new Error('read-only fixture'), { code }); });
+ log = new RvfEventLog({ logPath: path });
+ await log.initialize();
+ assert.deepEqual((await log.getEvents('a')).map(e => e.id), ['1']);
+ await assert.rejects(log.append(event('2')), /read-only.*unrepaired/i);
+ assert.deepEqual(readFileSync(path), original);
+ assert.deepEqual((await log.getEvents('a')).map(e => e.id), ['1']);
+ } finally { await log.close(); vi.mocked(fs.truncateSync).mockReset(); const actual = await vi.importActual('node:fs'); vi.mocked(fs.truncateSync).mockImplementation(actual.truncateSync); rmSync(dir, { recursive: true, force: true }); }
+});
+
+test('native read-only damaged log retains readable history and refuses new events', async () => {
+ const dir = mkdtempSync(join(tmpdir(), 'rvf-native-readonly-')); const path = join(dir, 'events.rvf');
+ let log = new RvfEventLog({ logPath: path });
+ const event = id => ({ id, aggregateId: 'a', aggregateType: 'Task', type: 'Created', timestamp: Number(id), version: 0, payload: {} });
+ try {
+ await log.initialize(); await log.append(event('1')); await log.close(); appendFileSync(path, Buffer.from([0, 0]));
+ const original = readFileSync(path); chmodSync(path, 0o400);
+ const actual = await vi.importActual('node:fs');
+ assert.throws(() => actual.truncateSync(path, original.length - 2), error => error.code === 'EACCES' || error.code === 'EPERM');
+ log = new RvfEventLog({ logPath: path }); await log.initialize();
+ assert.deepEqual((await log.getEvents('a')).map(e => e.id), ['1']);
+ await assert.rejects(log.append(event('2')), /read-only.*unrepaired/i);
+ assert.deepEqual(readFileSync(path), original);
+ assert.deepEqual((await log.getEvents('a')).map(e => e.id), ['1']);
+ } finally { await log.close(); chmodSync(path, 0o600); 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.ts
index 90206dd403..73ff56fa55 100644
--- a/v3/@claude-flow/shared/src/events/rvf-event-log.ts
+++ b/v3/@claude-flow/shared/src/events/rvf-event-log.ts
@@ -64,6 +64,9 @@ export class RvfEventLog extends EventEmitter {
private config: Required<RvfEventLogConfig>;
private initialized = false;
+ /** Files whose incomplete tails could not be repaired because they are read-only. */
+ private readOnlyFiles = new Set<string>();
+
/**
* All events kept in insertion order.
* Rebuilt from the file on initialize().
@@ -146,6 +149,7 @@ export class RvfEventLog extends EventEmitter {
this.aggregateIndex.clear();
this.aggregateVersions.clear();
this.snapshots.clear();
+ this.readOnlyFiles.clear();
this.initialized = false;
this.emit('shutdown');
@@ -383,12 +387,25 @@ export class RvfEventLog extends EventEmitter {
}
if (recoverableTail && offset < buf.length) {
- truncateSync(filePath, offset);
+ try {
+ truncateSync(filePath, offset);
+ } catch (error) {
+ const code = (error as NodeJS.ErrnoException).code;
+ if (code !== 'EACCES' && code !== 'EROFS' && code !== 'EPERM') throw error;
+ // Preserve readable history but never append behind an unreadable tail.
+ this.readOnlyFiles.add(filePath);
+ if (this.config.verbose) {
+ console.warn(`[RvfEventLog] Read-only log has an unrepaired tail: ${filePath}`);
+ }
+ }
}
}
/** Append a single record to an RVF file. */
private appendRecord(filePath: string, record: unknown): void {
+ if (this.readOnlyFiles.has(filePath)) {
+ throw new Error(`[RvfEventLog] Read-only log has an unrepaired tail; append refused: ${filePath}`);
+ }
const json = JSON.stringify(record);
const payload = Buffer.from(json, 'utf8');
const lengthBuf = Buffer.allocUnsafe(LENGTH_PREFIX_BYTES);