mirror of
https://github.com/ruvnet/ruflo.git
synced 2026-11-23 21:59:14 +09:00
Merge PR #4031 (RVF tail recovery) with oversized-tail and permission-safe fixes into release/shared-3.0.4
This commit is contained in:
commit
c444a600ee
3 files changed
+201
-3
No files matched your search
@@ -0,0 +1,98 @@
|
||||
import { test } from 'vitest';
|
||||
import assert from 'node:assert/strict';
|
||||
import { mkdtempSync, appendFileSync, rmSync, readFileSync, statSync } 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';
|
||||
|
||||
const event = id => ({ id, aggregateId: 'a', aggregateType: 'Task', type: 'Created', timestamp: Number(id), version: 0, payload: {} });
|
||||
const ids = async log => (await log.getEvents('a')).map(e => e.id);
|
||||
|
||||
async function withLog(fn, config = {}) {
|
||||
const dir = mkdtempSync(join(tmpdir(), 'rvf-matrix-'));
|
||||
const path = join(dir, 'events.rvf');
|
||||
try { await fn(path, () => new RvfEventLog({ logPath: path, ...config })); } finally { rmSync(dir, { recursive: true, force: true }); }
|
||||
}
|
||||
|
||||
// Repairable tails: every shape that is a torn write must lose nothing but the torn bytes, across repeated reopen→append cycles.
|
||||
const repairable = {
|
||||
'one byte of a length prefix': Buffer.from([0]),
|
||||
'a partial length prefix': Buffer.from([0, 0]),
|
||||
'three bytes of a length prefix': Buffer.from([0, 0, 0]),
|
||||
'a length prefix with no payload': Buffer.from([0, 0, 0, 100]),
|
||||
'a partial payload': Buffer.from([0, 0, 0, 100, 123]),
|
||||
};
|
||||
for (const [name, tail] of Object.entries(repairable)) {
|
||||
test(`keeps every append across two reopen cycles after ${name}`, () => withLog(async (path, open) => {
|
||||
let log = open();
|
||||
await log.initialize(); await log.append(event('1')); await log.close();
|
||||
appendFileSync(path, tail);
|
||||
for (const id of ['2', '3']) { log = open(); await log.initialize(); await log.append(event(id)); await log.close(); }
|
||||
log = open(); await log.initialize();
|
||||
assert.deepEqual(await ids(log), ['1', '2', '3']);
|
||||
await log.close();
|
||||
}));
|
||||
}
|
||||
|
||||
test('an oversized length prefix is never accepted silently: append rejects and the bytes are kept', () => withLog(async (path, open) => {
|
||||
let log = open();
|
||||
await log.initialize(); await log.append(event('1')); await log.close();
|
||||
appendFileSync(path, Buffer.from('garbage')); // prefix decodes to ~1.7 GB, over the limit
|
||||
const before = readFileSync(path);
|
||||
log = open(); await log.initialize();
|
||||
assert.deepEqual(await ids(log), ['1']);
|
||||
await assert.rejects(log.append(event('2')), /unrepaired tail.*safety limit/i);
|
||||
assert.deepEqual(readFileSync(path), before, 'file untouched');
|
||||
assert.deepEqual(await ids(log), ['1'], 'rejected event is not indexed');
|
||||
await log.close();
|
||||
}));
|
||||
|
||||
test('repairOversizedTail truncates at the bad prefix so later appends are readable', () => withLog(async (path, open) => {
|
||||
let log = open();
|
||||
await log.initialize(); await log.append(event('1')); await log.close();
|
||||
const good = statSync(path).size;
|
||||
appendFileSync(path, Buffer.from('garbage'));
|
||||
for (const id of ['2', '3']) { log = open(); await log.initialize(); await log.append(event(id)); await log.close(); }
|
||||
log = open(); await log.initialize();
|
||||
assert.deepEqual(await ids(log), ['1', '2', '3']);
|
||||
assert.ok(statSync(path).size > good);
|
||||
await log.close();
|
||||
}, { repairOversizedTail: true }));
|
||||
|
||||
test('a complete record with a non-JSON payload and a zero-filled tail stay readable and appendable', () => withLog(async (path, open) => {
|
||||
let log = open();
|
||||
await log.initialize(); await log.append(event('1')); await log.close();
|
||||
appendFileSync(path, Buffer.from([0, 0, 0, 3, 0x61, 0x62, 0x63])); // complete record, not JSON: skipped on replay
|
||||
log = open(); await log.initialize(); await log.append(event('2')); await log.close();
|
||||
log = open(); await log.initialize();
|
||||
assert.deepEqual(await ids(log), ['1', '2']);
|
||||
await log.close();
|
||||
}));
|
||||
|
||||
test('a damaged snapshot file tail does not lose later snapshots', () => withLog(async (path, open) => {
|
||||
let log = open();
|
||||
await log.initialize(); await log.append(event('1'));
|
||||
await log.saveSnapshot({ aggregateId: 'a', version: 1, state: { n: 1 }, timestamp: 1 });
|
||||
await log.close();
|
||||
appendFileSync(path.replace(/\.rvf$/, '.snap.rvf'), Buffer.from([0, 0]));
|
||||
log = open(); await log.initialize();
|
||||
await log.saveSnapshot({ aggregateId: 'a', version: 2, state: { n: 2 }, timestamp: 2 });
|
||||
await log.close();
|
||||
log = open(); await log.initialize();
|
||||
assert.equal((await log.getSnapshot('a')).version, 2);
|
||||
await log.close();
|
||||
}));
|
||||
|
||||
test('a record larger than the replay limit is refused at append instead of being written unreadable', () => withLog(async (path, open) => {
|
||||
const log = open();
|
||||
await log.initialize(); await log.append(event('1'));
|
||||
const before = readFileSync(path);
|
||||
const huge = { ...event('2'), payload: { blob: 'x'.repeat(101 * 1024 * 1024) } };
|
||||
await assert.rejects(log.append(huge), /exceeds the \d+ byte limit/);
|
||||
assert.deepEqual(readFileSync(path), before);
|
||||
await log.append(event('3'));
|
||||
await log.close();
|
||||
const again = open(); await again.initialize();
|
||||
assert.deepEqual(await ids(again), ['1', '3']);
|
||||
await again.close();
|
||||
}));
|
||||
@@ -0,0 +1,52 @@
|
||||
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, 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';
|
||||
for(const tail of [Buffer.from([0,0]),Buffer.from([0,0,0,100,123])]) test(`new events survive reopening after ${tail.length}-byte incomplete tail`,async()=>{
|
||||
const dir=mkdtempSync(join(tmpdir(),'rvf-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,tail);
|
||||
log=new RvfEventLog({logPath:path});await log.initialize();await log.append(event('2'));await log.close();
|
||||
log=new RvfEventLog({logPath:path});await log.initialize();
|
||||
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 }); }
|
||||
});
|
||||
@@ -16,7 +16,7 @@
|
||||
*/
|
||||
|
||||
import { EventEmitter } from 'node:events';
|
||||
import { existsSync, mkdirSync, readFileSync, writeFileSync, appendFileSync, renameSync } from 'node:fs';
|
||||
import { existsSync, mkdirSync, readFileSync, writeFileSync, appendFileSync, renameSync, truncateSync } from 'node:fs';
|
||||
import { dirname } from 'node:path';
|
||||
import type { DomainEvent } from './domain-events.js';
|
||||
|
||||
@@ -39,12 +39,19 @@ export interface RvfEventLogConfig {
|
||||
verbose?: boolean;
|
||||
/** Maximum events before snapshot recommendation */
|
||||
snapshotThreshold?: number;
|
||||
/**
|
||||
* When replay stops at a length prefix larger than the safety limit, truncate the file at that
|
||||
* prefix (discarding it and everything after it) instead of refusing further appends. Default
|
||||
* false: the bytes are kept and append() rejects explicitly until the file is repaired.
|
||||
*/
|
||||
repairOversizedTail?: boolean;
|
||||
}
|
||||
|
||||
const DEFAULT_CONFIG: Required<RvfEventLogConfig> = {
|
||||
logPath: 'events.rvf',
|
||||
verbose: false,
|
||||
snapshotThreshold: 100,
|
||||
repairOversizedTail: false,
|
||||
};
|
||||
|
||||
// =============================================================================
|
||||
@@ -55,6 +62,8 @@ const DEFAULT_CONFIG: Required<RvfEventLogConfig> = {
|
||||
const MAGIC = Buffer.from('RVFL');
|
||||
const MAGIC_LENGTH = 4;
|
||||
const LENGTH_PREFIX_BYTES = 4;
|
||||
/** Largest record replayFile() will read; appendRecord() refuses anything bigger so no accepted append is unreadable. */
|
||||
const MAX_PAYLOAD_SIZE = 100 * 1024 * 1024; // 100MB safety limit
|
||||
|
||||
// =============================================================================
|
||||
// RvfEventLog Implementation
|
||||
@@ -64,6 +73,10 @@ 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. */
|
||||
/** Files that hold bytes we could not repair; appends to them are refused (value: why). */
|
||||
private readOnlyFiles = new Map<string, string>();
|
||||
|
||||
/**
|
||||
* All events kept in insertion order.
|
||||
* Rebuilt from the file on initialize().
|
||||
@@ -153,6 +166,7 @@ export class RvfEventLog extends EventEmitter {
|
||||
this.aggregateIndex.clear();
|
||||
this.aggregateVersions.clear();
|
||||
this.snapshots.clear();
|
||||
this.readOnlyFiles.clear();
|
||||
this.initialized = false;
|
||||
|
||||
this.emit('shutdown');
|
||||
@@ -349,13 +363,14 @@ export class RvfEventLog extends EventEmitter {
|
||||
}
|
||||
|
||||
let offset = MAGIC_LENGTH;
|
||||
let oversizedAt = -1;
|
||||
|
||||
const MAX_PAYLOAD_SIZE = 100 * 1024 * 1024; // 100MB safety limit
|
||||
while (offset + LENGTH_PREFIX_BYTES <= buf.length) {
|
||||
const payloadLength = buf.readUInt32BE(offset);
|
||||
offset += LENGTH_PREFIX_BYTES;
|
||||
|
||||
if (payloadLength > MAX_PAYLOAD_SIZE) {
|
||||
oversizedAt = offset - LENGTH_PREFIX_BYTES;
|
||||
if (this.config.verbose) {
|
||||
console.warn(`[RvfEventLog] Payload size ${payloadLength} exceeds safety limit`);
|
||||
}
|
||||
@@ -363,13 +378,14 @@ export class RvfEventLog extends EventEmitter {
|
||||
}
|
||||
|
||||
if (offset + payloadLength > buf.length) {
|
||||
// Truncated record — stop reading (crash recovery).
|
||||
// Remove only the incomplete record before accepting future appends.
|
||||
if (this.config.verbose) {
|
||||
console.warn(
|
||||
`[RvfEventLog] Truncated record at offset ${offset - LENGTH_PREFIX_BYTES} — ` +
|
||||
`expected ${payloadLength} bytes, have ${buf.length - offset}`
|
||||
);
|
||||
}
|
||||
offset -= LENGTH_PREFIX_BYTES;
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -385,13 +401,45 @@ export class RvfEventLog extends EventEmitter {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// An oversized length prefix is not a torn write: keep the bytes unless the caller opted in to repair.
|
||||
let keepUntil = offset;
|
||||
if (oversizedAt >= 0) {
|
||||
if (!this.config.repairOversizedTail) {
|
||||
this.readOnlyFiles.set(filePath, 'a record length prefix exceeds the safety limit');
|
||||
return;
|
||||
}
|
||||
keepUntil = oversizedAt;
|
||||
}
|
||||
|
||||
if (keepUntil < buf.length) {
|
||||
try {
|
||||
truncateSync(filePath, keepUntil);
|
||||
} 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.set(filePath, 'the file is not writable, so its damaged tail could not be repaired');
|
||||
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<T>(filePath: string, record: T): T {
|
||||
const refused = this.readOnlyFiles.get(filePath);
|
||||
if (refused !== undefined) {
|
||||
throw new Error(`[RvfEventLog] Read-only until repaired: unrepaired tail (${refused}); append refused: ${filePath}`);
|
||||
}
|
||||
const json = JSON.stringify(record);
|
||||
const persisted = JSON.parse(json) as T;
|
||||
const payload = Buffer.from(json, 'utf8');
|
||||
if (payload.length > MAX_PAYLOAD_SIZE) {
|
||||
// replayFile() refuses records this large, so writing one would be an append that can never be read back.
|
||||
throw new Error(`[RvfEventLog] Record of ${payload.length} bytes exceeds the ${MAX_PAYLOAD_SIZE} byte limit; append refused`);
|
||||
}
|
||||
const lengthBuf = Buffer.allocUnsafe(LENGTH_PREFIX_BYTES);
|
||||
lengthBuf.writeUInt32BE(payload.length, 0);
|
||||
|
||||
|
||||
Reference in new issue
Block a user