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(hooks): build-agents writes under the project, validates outputDir, drops fake stats (#4562)

Aaditya5ae2d6527b24
diff --git a/v3/@claude-flow/cli/__tests__/hooks-build-agents-4558.test.ts b/v3/@claude-flow/cli/__tests__/hooks-build-agents-4558.test.ts
new file mode 100644
index 0000000000..1e35a9e52b
--- /dev/null
+++ b/v3/@claude-flow/cli/__tests__/hooks-build-agents-4558.test.ts
@@ -0,0 +1,64 @@
+/**
+ * #4558: hooks_build-agents
+ * - resolved outputDir against the process cwd instead of the project
+ * (getProjectCwd), so a default call overwrote ./agents wherever it ran;
+ * - never validated outputDir (a `..` path was accepted and written to);
+ * - reported patternsApplied = agents * 3 although no pretrain data is read.
+ */
+import { afterEach, beforeEach, describe, expect, it } from 'vitest';
+import { existsSync, mkdtempSync, readdirSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+
+const originalCwd = process.cwd();
+const originalProjectCwd = process.env.CLAUDE_FLOW_CWD;
+let project: string;
+let elsewhere: string;
+
+async function build(input: Record<string, unknown> = {}) {
+ const { hooksTools } = await import('../src/mcp-tools/hooks-tools.js');
+ return hooksTools.find((t) => t.name === 'hooks_build-agents')!.handler(input) as Promise<Record<string, any>>;
+}
+
+beforeEach(() => {
+ project = mkdtempSync(join(tmpdir(), 'ruflo-4558-project-'));
+ elsewhere = mkdtempSync(join(tmpdir(), 'ruflo-4558-cwd-'));
+ process.env.CLAUDE_FLOW_CWD = project;
+ // Never let a default call write into this package's tracked agents/ directory.
+ process.chdir(elsewhere);
+});
+afterEach(() => {
+ process.chdir(originalCwd);
+ if (originalProjectCwd === undefined) delete process.env.CLAUDE_FLOW_CWD;
+ else process.env.CLAUDE_FLOW_CWD = originalProjectCwd;
+});
+
+describe('hooks_build-agents (#4558)', () => {
+ it('writes the default ./agents under the project, not the process cwd', async () => {
+ const r = await build();
+ // Baseline: outputDir is <process cwd>/agents and the YAML files land there.
+ expect(r.outputDir).toBe(join(project, 'agents'));
+ expect(readdirSync(join(project, 'agents')).sort()).toEqual(['architect.yaml', 'coder.yaml', 'reviewer.yaml', 'security-architect.yaml', 'tester.yaml']);
+ expect(existsSync(join(elsewhere, 'agents'))).toBe(false);
+ });
+
+ it('rejects a traversal outputDir without writing anything', async () => {
+ const target = `${project}/out/../escaped`; // raw string: path.join would normalise the .. away
+ const r = await build({ outputDir: target });
+ expect(r).toMatchObject({ success: false, error: expect.stringMatching(/traversal/) });
+ expect(existsSync(join(project, 'escaped'))).toBe(false);
+ });
+
+ it('does not claim pretrain patterns it never read', async () => {
+ const r = await build({ persist: false });
+ expect(r.stats).toMatchObject({ configsGenerated: 5, patternsApplied: 0 });
+ expect(r._note).toMatch(/templates/);
+ });
+
+ it('still honours an explicit absolute outputDir (control)', async () => {
+ const out = join(project, 'custom');
+ const r = await build({ outputDir: out, format: 'json', focus: 'security' });
+ expect(r.outputDir).toBe(out);
+ expect(readdirSync(out).sort()).toEqual(['reviewer.json', 'security-architect.json']);
+ });
+});
diff --git a/v3/@claude-flow/cli/src/commands/hooks.ts b/v3/@claude-flow/cli/src/commands/hooks.ts
index 3984ab1c87..92fd4a534a 100644
--- a/v3/@claude-flow/cli/src/commands/hooks.ts
+++ b/v3/@claude-flow/cli/src/commands/hooks.ts
@@ -1204,6 +1204,14 @@ const buildAgentsCommand: Command = {
includePretrained: true,
});
+ // #4558: the tool now rejects an invalid outputDir with { success: false, error }.
+ const failed = result as { success?: boolean; error?: string };
+ if (failed.success === false) {
+ spinner.fail('Agent config generation failed');
+ output.printError(`Build agents error: ${failed.error}`);
+ return { success: false, exitCode: 1 };
+ }
+
spinner.succeed(`Generated ${result.agents.length} agent configs`);
if (ctx.flags.format === 'json') {
diff --git a/v3/@claude-flow/cli/src/mcp-tools/hooks-tools.ts b/v3/@claude-flow/cli/src/mcp-tools/hooks-tools.ts
index 80f85734c9..1aeb6ec5f7 100644
--- a/v3/@claude-flow/cli/src/mcp-tools/hooks-tools.ts
+++ b/v3/@claude-flow/cli/src/mcp-tools/hooks-tools.ts
@@ -2425,7 +2425,13 @@ export const hooksBuildAgents: MCPTool = {
},
},
handler: async (params: Record<string, unknown>) => {
- const outputDir = resolve((params.outputDir as string) || './agents');
+ // #4558: outputDir was never validated and resolved against the process
+ // cwd, so a default call from another directory overwrote files there.
+ if (params.outputDir !== undefined) {
+ const v = validatePath(params.outputDir, 'outputDir');
+ if (!v.valid) return { success: false, error: v.error };
+ }
+ const outputDir = resolve(getProjectCwd(), (params.outputDir as string) || './agents');
const focus = (params.focus as string) || 'all';
const format = (params.format as string) || 'yaml';
const persist = params.persist !== false; // Default to true
@@ -2475,9 +2481,11 @@ export const hooksBuildAgents: MCPTool = {
agents: filteredAgents,
stats: {
configsGenerated: filteredAgents.length,
- patternsApplied: filteredAgents.length * 3,
+ // #4558: was agents * 3; no pretrain data is read, the configs are templates.
+ patternsApplied: 0,
optimizationsIncluded: filteredAgents.reduce((acc, a) => acc + a.optimizations.length, 0),
},
+ _note: 'Configs are built-in templates; pretrain data is not read.',
};
},
};