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(config): preserve files on parse and read failures

Rudy Celeklia8b04d2939e5
diff --git a/v3/@claude-flow/cli/__tests__/config-malformed-preservation.test.ts b/v3/@claude-flow/cli/__tests__/config-malformed-preservation.test.ts
new file mode 100644
index 0000000000..2643dfd608
--- /dev/null
+++ b/v3/@claude-flow/cli/__tests__/config-malformed-preservation.test.ts
@@ -0,0 +1,55 @@
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+import * as fs from 'fs';
+import { join } from 'node:path';
+import { tmpdir } from 'node:os';
+import { ConfigFileManager } from '../src/services/config-file-manager.js';
+
+vi.mock('fs', async (importOriginal) => ({
+ ...await importOriginal<typeof import('fs')>(),
+ readFileSync: vi.fn((await importOriginal<typeof import('fs')>()).readFileSync),
+}));
+let cwd: string;
+let file: string;
+beforeEach(() => {
+ cwd = fs.mkdtempSync(join(tmpdir(), 'config-preserve-'));
+ file = join(cwd, 'claude-flow.config.json');
+ vi.stubEnv('CLAUDE_FLOW_CONFIG', '');
+});
+afterEach(() => {
+ vi.restoreAllMocks();
+ vi.unstubAllEnvs();
+ fs.rmSync(cwd, { recursive: true, force: true });
+});
+
+describe('config writes preserve unreadable or invalid input', () => {
+ it.each(['{"memory":', 'null', '[1,2]', '42', '"config"'])('rejects %s without replacing the file', (contents) => {
+ fs.writeFileSync(file, contents);
+ expect(() => new ConfigFileManager().set(cwd, 'swarm.maxAgents', 3)).toThrow(/config/i);
+ expect(fs.readFileSync(file, 'utf8')).toBe(contents);
+ expect(fs.existsSync(file + '.tmp')).toBe(false);
+ });
+
+ it('propagates permission failures instead of returning writable defaults', () => {
+ fs.writeFileSync(file, '{"private":"keep"}');
+ vi.mocked(fs.readFileSync).mockImplementationOnce(() => {
+ throw Object.assign(new Error('permission denied'), { code: 'EACCES' });
+ });
+ expect(() => new ConfigFileManager().set(cwd, 'swarm.maxAgents', 3)).toThrow(/permission denied/);
+ expect(fs.readFileSync(file, 'utf8')).toBe('{"private":"keep"}');
+ });
+
+ it('treats a file removed between discovery and read as missing', () => {
+ const manager = new ConfigFileManager();
+ vi.spyOn(manager, 'findConfig').mockReturnValue(file);
+ expect(manager.load(cwd)).toBeNull();
+ expect(manager.getConfigPath()).toBeNull();
+ });
+
+ it('still creates a missing config and preserves unrelated fields in a valid one', () => {
+ new ConfigFileManager().set(cwd, 'swarm.maxAgents', 3);
+ expect(JSON.parse(fs.readFileSync(file, 'utf8')).swarm.maxAgents).toBe(3);
+ fs.writeFileSync(file, '{"custom":{"enabled":false},"swarm":{"maxAgents":8}}');
+ new ConfigFileManager().set(cwd, 'swarm.maxAgents', 4);
+ expect(JSON.parse(fs.readFileSync(file, 'utf8'))).toEqual({ custom: { enabled: false }, swarm: { maxAgents: 4 } });
+ });
+});
diff --git a/v3/@claude-flow/cli/src/services/config-file-manager.ts b/v3/@claude-flow/cli/src/services/config-file-manager.ts
index d789c4f6e8..a8aa6f8f95 100644
--- a/v3/@claude-flow/cli/src/services/config-file-manager.ts
+++ b/v3/@claude-flow/cli/src/services/config-file-manager.ts
@@ -86,11 +86,22 @@ export class ConfigFileManager {
}
try {
const content = fs.readFileSync(this.configPath, 'utf-8');
- this.config = JSON.parse(content);
+ const parsed: unknown = JSON.parse(content);
+ if (typeof parsed !== 'object' || parsed === null || Array.isArray(parsed)) {
+ throw new Error('Config file must contain a JSON object');
+ }
+ this.config = parsed as Record<string, unknown>;
return this.config;
- } catch {
+ } catch (error) {
this.config = null;
- return null;
+ if ((error as NodeJS.ErrnoException).code === 'ENOENT') {
+ this.configPath = null;
+ return null;
+ }
+ // A parse/read failure is not a missing config. In particular, set()
+ // must never replace a recoverable file with fallback defaults.
+ const detail = error instanceof Error ? error.message : String(error);
+ throw new Error(`Failed to load config ${this.configPath}: ${detail}`);
}
}