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): isolate cached configuration by project directory

Rudy Celekli03447c689715
diff --git a/v3/@claude-flow/cli/__tests__/config-project-isolation.test.ts b/v3/@claude-flow/cli/__tests__/config-project-isolation.test.ts
new file mode 100644
index 0000000000..28d0dea893
--- /dev/null
+++ b/v3/@claude-flow/cli/__tests__/config-project-isolation.test.ts
@@ -0,0 +1,104 @@
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
+import { join } from 'node:path';
+import { tmpdir } from 'node:os';
+import { ConfigFileManager } from '../src/services/config-file-manager.js';
+
+let root: string;
+let a: string;
+let b: string;
+const file = (cwd: string) => join(cwd, 'claude-flow.config.json');
+const read = (cwd: string) => JSON.parse(readFileSync(file(cwd), 'utf8'));
+beforeEach(() => {
+ root = mkdtempSync(join(tmpdir(), 'config-project-isolation-'));
+ a = join(root, 'a');
+ b = join(root, 'b');
+ for (const [cwd, name] of [[a, 'a'], [b, 'b']]) {
+ mkdirSync(cwd);
+ writeFileSync(file(cwd), JSON.stringify({ project: name, custom: { keep: name } }));
+ }
+ vi.stubEnv('CLAUDE_FLOW_CONFIG', '');
+});
+afterEach(() => {
+ vi.unstubAllEnvs();
+ rmSync(root, { recursive: true, force: true });
+});
+
+describe('configuration project isolation', () => {
+ it('switches reads to the requested project, including switching back', () => {
+ const manager = new ConfigFileManager();
+ expect(manager.get(a, 'project')).toBe('a');
+ expect(manager.get(b, 'project')).toBe('b');
+ expect(manager.getConfigPath()).toBe(file(b));
+ expect(manager.get(a, 'project')).toBe('a');
+ expect(manager.getConfigPath()).toBe(file(a));
+ });
+
+ it('updates project B without changing cached project A', () => {
+ const manager = new ConfigFileManager();
+ const before = readFileSync(file(a), 'utf8');
+ manager.load(a);
+ manager.set(b, 'custom.enabled', true);
+ expect(readFileSync(file(a), 'utf8')).toBe(before);
+ expect(read(b)).toEqual({ project: 'b', custom: { keep: 'b', enabled: true } });
+ });
+
+ it('does not carry existing values into a missing project configuration', () => {
+ rmSync(file(b));
+ const manager = new ConfigFileManager();
+ manager.load(a);
+ manager.set(b, 'custom.enabled', true);
+ expect(read(b)).toEqual({ custom: { enabled: true } });
+ expect(read(a).project).toBe('a');
+ });
+
+ it('exports the requested project even after another was cached', () => {
+ const manager = new ConfigFileManager();
+ manager.load(a);
+ manager.exportTo(b, 'export.json');
+ expect(JSON.parse(readFileSync(join(b, 'export.json'), 'utf8')).project).toBe('b');
+ });
+
+ it.each(['reset', 'import'] as const)('discovers the current hidden file for %s without loading it first', (operation) => {
+ rmSync(file(b));
+ mkdirSync(join(b, '.claude-flow'));
+ const hidden = join(b, '.claude-flow', 'config.json');
+ writeFileSync(hidden, '{"project":"b"}');
+ writeFileSync(join(b, 'input.json'), '{"project":"imported"}');
+ const manager = new ConfigFileManager();
+ if (operation === 'reset') expect(manager.reset(b)).toBe(hidden);
+ else manager.importFrom(b, 'input.json');
+ expect(manager.getConfigPath()).toBe(hidden);
+ expect(existsSync(file(b))).toBe(false);
+ expect(JSON.parse(readFileSync(hidden, 'utf8'))).toEqual(operation === 'reset' ? manager.getDefaults() : { project: 'imported' });
+ });
+
+ it.each(['reset', 'import'] as const)('targets project B for %s after project A was loaded', (operation) => {
+ const before = readFileSync(file(a), 'utf8');
+ writeFileSync(join(b, 'input.json'), '{"project":"imported"}');
+ const manager = new ConfigFileManager();
+ manager.load(a);
+ if (operation === 'reset') expect(manager.reset(b)).toBe(file(b));
+ else manager.importFrom(b, 'input.json');
+ expect(readFileSync(file(a), 'utf8')).toBe(before);
+ expect(read(b)).toEqual(operation === 'reset' ? manager.getDefaults() : { project: 'imported' });
+ });
+
+ it('preserves malformed project B rather than writing through the project A cache', () => {
+ const before = readFileSync(file(a), 'utf8');
+ writeFileSync(file(b), '{"incomplete":');
+ const manager = new ConfigFileManager();
+ manager.load(a);
+ expect(() => manager.set(b, 'custom.enabled', true)).toThrow(/Failed to load config/);
+ expect(readFileSync(file(a), 'utf8')).toBe(before);
+ expect(readFileSync(file(b), 'utf8')).toBe('{"incomplete":');
+ expect(existsSync(file(b) + '.tmp')).toBe(false);
+ });
+
+ it('binds newly created configuration to its project', () => {
+ const manager = new ConfigFileManager();
+ manager.create(a, { project: 'created' }, true);
+ expect(manager.get(b, 'project')).toBe('b');
+ expect(manager.get(a, 'project')).toBe('created');
+ });
+});
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 2d8d66f3fa..a93ff89c99 100644
--- a/v3/@claude-flow/cli/src/services/config-file-manager.ts
+++ b/v3/@claude-flow/cli/src/services/config-file-manager.ts
@@ -58,6 +58,7 @@ const DEFAULT_CONFIG: Record<string, unknown> = {
};
export class ConfigFileManager {
+ private configCwd: string | null = null;
private configPath: string | null = null;
private config: Record<string, unknown> | null = null;
@@ -79,6 +80,7 @@ export class ConfigFileManager {
/** Load config from file, returns null if not found */
load(cwd: string): Record<string, unknown> | null {
+ this.selectProject(cwd);
this.configPath = this.findConfig(cwd);
if (!this.configPath) {
this.config = null;
@@ -107,6 +109,7 @@ export class ConfigFileManager {
/** Get the current config, loading if needed */
getConfig(cwd: string): Record<string, unknown> {
+ this.selectProject(cwd);
if (this.config === null) {
this.load(cwd);
}
@@ -125,6 +128,7 @@ export class ConfigFileManager {
if (key.split('.').some(part => part === '__proto__' || part === 'constructor' || part === 'prototype')) {
throw new Error(`Unsafe configuration key: ${key}`);
}
+ this.selectProject(cwd);
// A targeted update must not persist unrelated defaults (notably the
// default memory path, which would relocate an existing memory store).
const config = this.config ?? this.load(cwd) ?? {};
@@ -147,6 +151,7 @@ export class ConfigFileManager {
}
const config = { ...DEFAULT_CONFIG, ...overrides };
this.writeAtomic(targetPath, config);
+ this.selectProject(cwd);
this.config = config;
this.configPath = targetPath;
return targetPath;
@@ -154,7 +159,8 @@ export class ConfigFileManager {
/** Reset config to defaults */
reset(cwd: string): string {
- const targetPath = this.configPath ?? path.resolve(cwd, CONFIG_FILENAMES[0]);
+ this.selectProject(cwd);
+ const targetPath = this.findConfig(cwd) ?? path.resolve(cwd, CONFIG_FILENAMES[0]);
this.writeAtomic(targetPath, DEFAULT_CONFIG);
this.config = { ...DEFAULT_CONFIG };
this.configPath = targetPath;
@@ -184,7 +190,8 @@ export class ConfigFileManager {
if (typeof imported !== 'object' || imported === null || Array.isArray(imported)) {
throw new Error('Import file must contain a JSON object');
}
- const targetPath = this.configPath ?? path.resolve(cwd, CONFIG_FILENAMES[0]);
+ this.selectProject(cwd);
+ const targetPath = this.findConfig(cwd) ?? path.resolve(cwd, CONFIG_FILENAMES[0]);
this.writeAtomic(targetPath, imported);
this.config = imported;
this.configPath = targetPath;
@@ -200,6 +207,16 @@ export class ConfigFileManager {
return { ...DEFAULT_CONFIG };
}
+ /** Cached values and file paths belong to one resolved project directory. */
+ private selectProject(cwd: string): void {
+ const resolved = path.resolve(cwd);
+ if (this.configCwd !== resolved) {
+ this.configCwd = resolved;
+ this.config = null;
+ this.configPath = null;
+ }
+ }
+
/** Atomic write: write to .tmp then rename */
private writeAtomic(filePath: string, data: Record<string, unknown>): void {
const dir = path.dirname(filePath);