mirror of
https://github.com/ruvnet/ruflo.git
synced 2026-12-04 06:21:59 +09:00
Merge PR #4816
This commit is contained in:
commit
1aa99f258b
2 files changed
+117
-12
No files matched your search
@@ -0,0 +1,89 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
import { WorkerDaemon } from '../../src/services/worker-daemon.js';
|
||||
import * as secureFs from '../../src/fs-secure.js';
|
||||
|
||||
type DaemonInternals = {
|
||||
headlessInitPromise: Promise<void>;
|
||||
log(level: 'info' | 'warn' | 'error', message: string): void;
|
||||
runMapWorker(): Promise<unknown>;
|
||||
runAuditWorkerLocal(): Promise<unknown>;
|
||||
};
|
||||
|
||||
let workspace: string;
|
||||
let daemon: DaemonInternals;
|
||||
let logFile: string;
|
||||
const events = ['SIGTERM', 'SIGINT', 'SIGHUP', 'exit', 'uncaughtException', 'unhandledRejection'] as const;
|
||||
let originalListeners: Map<string, ReturnType<typeof process.listeners>>;
|
||||
|
||||
beforeEach(async () => {
|
||||
originalListeners = new Map(events.map(event => [event, process.listeners(event)]));
|
||||
workspace = mkdtempSync(join(tmpdir(), 'daemon-persistence-'));
|
||||
daemon = new WorkerDaemon(workspace, { aiWorkersEnabled: false }) as unknown as DaemonInternals;
|
||||
await daemon.headlessInitPromise;
|
||||
logFile = join(workspace, '.claude-flow', 'logs', 'daemon.log');
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
for (const event of events) {
|
||||
for (const listener of process.listeners(event)) {
|
||||
if (!originalListeners.get(event)!.includes(listener)) process.removeListener(event, listener);
|
||||
}
|
||||
}
|
||||
rmSync(workspace, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
describe('worker daemon persistence', () => {
|
||||
it('appends complete records while the log is below its limit', () => {
|
||||
writeFileSync(logFile, 'earlier\n');
|
||||
daemon.log('info', 'later');
|
||||
expect(readFileSync(logFile, 'utf8')).toMatch(/^earlier\n\[.+\] \[INFO\] later\n$/);
|
||||
expect(existsSync(logFile + '.1')).toBe(false);
|
||||
});
|
||||
|
||||
it('rotates at 5 MiB and retains only three previous logs', () => {
|
||||
const previous = Buffer.alloc(5 * 1024 * 1024, 'a');
|
||||
writeFileSync(logFile, previous);
|
||||
writeFileSync(logFile + '.1', 'older');
|
||||
writeFileSync(logFile + '.2', 'oldest retained');
|
||||
writeFileSync(logFile + '.3', 'discarded');
|
||||
daemon.log('warn', 'new generation');
|
||||
expect(readFileSync(logFile, 'utf8')).toMatch(/\[WARN\] new generation\n$/);
|
||||
expect(readFileSync(logFile + '.1').equals(previous)).toBe(true);
|
||||
expect(readFileSync(logFile + '.2', 'utf8')).toBe('older');
|
||||
expect(readFileSync(logFile + '.3', 'utf8')).toBe('oldest retained');
|
||||
expect(existsSync(logFile + '.4')).toBe(false);
|
||||
});
|
||||
|
||||
it('rotates before a multibyte record would cross the limit', () => {
|
||||
writeFileSync(logFile, Buffer.alloc(5 * 1024 * 1024 - 100, 'a'));
|
||||
daemon.log('info', '界'.repeat(30));
|
||||
expect(existsSync(logFile + '.1')).toBe(true);
|
||||
expect(readFileSync(logFile, 'utf8')).toContain('界'.repeat(30));
|
||||
});
|
||||
|
||||
it.each([
|
||||
['runMapWorker', 'codebase-map.json'],
|
||||
['runAuditWorkerLocal', 'security-audit.json'],
|
||||
] as const)('%s replaces metric JSON through the atomic writer', async (method, filename) => {
|
||||
const write = vi.spyOn(secureFs, 'writeFileAtomic');
|
||||
const result = await daemon[method]();
|
||||
const file = join(workspace, '.claude-flow', 'metrics', filename);
|
||||
expect(write).toHaveBeenCalledWith(file, expect.any(Buffer));
|
||||
expect(JSON.parse(readFileSync(file, 'utf8'))).toEqual(result);
|
||||
});
|
||||
|
||||
it('keeps the previous metric report when the atomic writer fails', async () => {
|
||||
await daemon.runMapWorker();
|
||||
const file = join(workspace, '.claude-flow', 'metrics', 'codebase-map.json');
|
||||
const previous = readFileSync(file, 'utf8');
|
||||
vi.spyOn(secureFs, 'writeFileAtomic').mockImplementation(() => {
|
||||
throw new Error('disk full');
|
||||
});
|
||||
await expect(daemon.runMapWorker()).rejects.toThrow('disk full');
|
||||
expect(readFileSync(file, 'utf8')).toBe(previous);
|
||||
});
|
||||
});
|
||||
@@ -13,7 +13,7 @@
|
||||
|
||||
import { ConfigFileManager } from './config-file-manager.js';
|
||||
import { EventEmitter } from 'events';
|
||||
import { existsSync, mkdirSync, writeFileSync, readFileSync, appendFileSync, unlinkSync, renameSync } from 'fs';
|
||||
import { existsSync, mkdirSync, writeFileSync, readFileSync, appendFileSync, unlinkSync, renameSync, statSync } from 'fs';
|
||||
import { cpus, homedir } from 'os';
|
||||
import { join } from 'path';
|
||||
import { createRequire } from 'node:module';
|
||||
@@ -45,6 +45,8 @@ import { getRepoSupervisorRegistry, type SupervisorRecord } from './repo-supervi
|
||||
// Daemon config is read synchronously from the constructor. In an ESM build,
|
||||
// bare `require` is undefined, so use a module-relative require for YAML.
|
||||
const requireYaml = createRequire(import.meta.url);
|
||||
const DAEMON_LOG_MAX_BYTES = 5 * 1024 * 1024;
|
||||
const DAEMON_LOG_BACKUPS = 3;
|
||||
|
||||
// Worker types matching hooks-tools.ts
|
||||
export type WorkerType =
|
||||
@@ -1745,7 +1747,7 @@ export class WorkerDaemon extends EventEmitter {
|
||||
rawOutputLength: typeof result.output === 'string' ? result.output.length : 0,
|
||||
};
|
||||
|
||||
writeFileSync(metricsFile, JSON.stringify(persisted, null, 2));
|
||||
writeFileAtomic(metricsFile, Buffer.from(JSON.stringify(persisted, null, 2)));
|
||||
}
|
||||
|
||||
// Worker implementations
|
||||
@@ -1771,7 +1773,7 @@ export class WorkerDaemon extends EventEmitter {
|
||||
scannedAt: Date.now(),
|
||||
};
|
||||
|
||||
writeFileSync(metricsFile, JSON.stringify(map, null, 2));
|
||||
writeFileAtomic(metricsFile, Buffer.from(JSON.stringify(map, null, 2)));
|
||||
return map;
|
||||
}
|
||||
|
||||
@@ -1807,7 +1809,7 @@ export class WorkerDaemon extends EventEmitter {
|
||||
note: 'Local fallback checks file presence only; it does not scan source contents.',
|
||||
};
|
||||
|
||||
writeFileSync(auditFile, JSON.stringify(audit, null, 2));
|
||||
writeFileAtomic(auditFile, Buffer.from(JSON.stringify(audit, null, 2)));
|
||||
return audit;
|
||||
}
|
||||
|
||||
@@ -1835,7 +1837,7 @@ export class WorkerDaemon extends EventEmitter {
|
||||
note: 'Install Claude Code CLI for AI-powered optimization suggestions',
|
||||
};
|
||||
|
||||
writeFileSync(optimizeFile, JSON.stringify(perf, null, 2));
|
||||
writeFileAtomic(optimizeFile, Buffer.from(JSON.stringify(perf, null, 2)));
|
||||
return perf;
|
||||
}
|
||||
|
||||
@@ -1880,7 +1882,7 @@ export class WorkerDaemon extends EventEmitter {
|
||||
memoryCleaned: 0,
|
||||
duplicatesRemoved: 0,
|
||||
};
|
||||
writeFileSync(consolidateFile, JSON.stringify(disabledResult, null, 2));
|
||||
writeFileAtomic(consolidateFile, Buffer.from(JSON.stringify(disabledResult, null, 2)));
|
||||
return disabledResult;
|
||||
}
|
||||
|
||||
@@ -1907,7 +1909,7 @@ export class WorkerDaemon extends EventEmitter {
|
||||
memoryCleaned: 0,
|
||||
duplicatesRemoved: 0,
|
||||
};
|
||||
writeFileSync(consolidateFile, JSON.stringify(errorResult, null, 2));
|
||||
writeFileAtomic(consolidateFile, Buffer.from(JSON.stringify(errorResult, null, 2)));
|
||||
return errorResult;
|
||||
}
|
||||
|
||||
@@ -1953,7 +1955,7 @@ export class WorkerDaemon extends EventEmitter {
|
||||
})),
|
||||
};
|
||||
|
||||
writeFileSync(consolidateFile, JSON.stringify(result, null, 2));
|
||||
writeFileAtomic(consolidateFile, Buffer.from(JSON.stringify(result, null, 2)));
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -1996,7 +1998,7 @@ export class WorkerDaemon extends EventEmitter {
|
||||
result = { timestamp: new Date().toISOString(), backedUp: false, error: message };
|
||||
}
|
||||
|
||||
writeFileSync(backupFile, JSON.stringify(result, null, 2));
|
||||
writeFileAtomic(backupFile, Buffer.from(JSON.stringify(result, null, 2)));
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -2034,7 +2036,7 @@ export class WorkerDaemon extends EventEmitter {
|
||||
result = { timestamp: new Date().toISOString(), ran: false, error: message };
|
||||
}
|
||||
|
||||
writeFileSync(harnessFile, JSON.stringify(result, null, 2));
|
||||
writeFileAtomic(harnessFile, Buffer.from(JSON.stringify(result, null, 2)));
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -2059,7 +2061,7 @@ export class WorkerDaemon extends EventEmitter {
|
||||
note: 'Install Claude Code CLI for AI-powered test gap analysis',
|
||||
};
|
||||
|
||||
writeFileSync(testGapsFile, JSON.stringify(result, null, 2));
|
||||
writeFileAtomic(testGapsFile, Buffer.from(JSON.stringify(result, null, 2)));
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -2149,7 +2151,7 @@ export class WorkerDaemon extends EventEmitter {
|
||||
},
|
||||
};
|
||||
|
||||
writeFileSync(benchmarkFile, JSON.stringify(result, null, 2));
|
||||
writeFileAtomic(benchmarkFile, Buffer.from(JSON.stringify(result, null, 2)));
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -2263,6 +2265,20 @@ export class WorkerDaemon extends EventEmitter {
|
||||
// Also write to log file
|
||||
try {
|
||||
const logFile = join(this.config.logDir, 'daemon.log');
|
||||
// Keep three generations. Rotate before the next complete record
|
||||
// crosses 5 MiB; a single oversized record is preserved intact.
|
||||
if (existsSync(logFile)) {
|
||||
const size = statSync(logFile).size;
|
||||
if (size > 0 && size + Buffer.byteLength(logMessage + '\n') > DAEMON_LOG_MAX_BYTES) {
|
||||
const oldest = `${logFile}.${DAEMON_LOG_BACKUPS}`;
|
||||
if (existsSync(oldest)) unlinkSync(oldest);
|
||||
for (let generation = DAEMON_LOG_BACKUPS - 1; generation >= 1; generation--) {
|
||||
const previous = `${logFile}.${generation}`;
|
||||
if (existsSync(previous)) renameSync(previous, `${logFile}.${generation + 1}`);
|
||||
}
|
||||
renameSync(logFile, `${logFile}.1`);
|
||||
}
|
||||
}
|
||||
appendFileSync(logFile, logMessage + '\n');
|
||||
} catch {
|
||||
// Ignore log write errors
|
||||
|
||||
Reference in new issue
Block a user