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(cli): fail consensus commands when the hive rejects the operation

Rudy Celekli9e4ee395c181
diff --git a/v3/@claude-flow/cli/__tests__/hive-mind-cli-consensus-errors.test.ts b/v3/@claude-flow/cli/__tests__/hive-mind-cli-consensus-errors.test.ts
new file mode 100644
index 0000000000..0caf3404fc
--- /dev/null
+++ b/v3/@claude-flow/cli/__tests__/hive-mind-cli-consensus-errors.test.ts
@@ -0,0 +1,100 @@
+import { beforeEach, afterEach, describe, expect, it, vi } from 'vitest';
+import { mkdtempSync, readFileSync, rmSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+
+const printed = vi.hoisted(() => ({ printJson: vi.fn(), printSuccess: vi.fn(), printError: vi.fn() }));
+vi.mock('../src/output.js', () => ({ output: printed }));
+vi.mock('../src/prompt.js', () => ({ select: vi.fn(), confirm: vi.fn(), input: vi.fn() }));
+vi.mock('../src/mcp-client.js', async () => {
+ const { hiveMindTools } = await import('../src/mcp-tools/hive-mind-tools.js');
+ return {
+ MCPClientError: class extends Error {},
+ callMCPTool: async (name: string, input: Record<string, unknown>) => {
+ const tool = hiveMindTools.find(tool => tool.name === name);
+ if (!tool) throw new Error(`Unexpected tool: ${name}`);
+ return tool.handler(input);
+ },
+ };
+});
+import { hiveMindCommand } from '../src/commands/hive-mind.js';
+import { hiveMindTools } from '../src/mcp-tools/hive-mind-tools.js';
+import type { CommandContext } from '../src/types.js';
+
+// Only the MCP dispatch seam and output presentation are replaced. The real
+// command action calls the real hive handler, which reads/writes disposable files.
+function tool(name: string) { return hiveMindTools.find(tool => tool.name === name)!; }
+const consensus = hiveMindCommand.subcommands!.find(command => command.name === 'consensus')!;
+describe('CLI consensus outcomes reflect actual persisted hive state', () => {
+ let dir: string;
+ let previous: string | undefined;
+ let token: string;
+ let proposalId: string;
+ function bytes() { return readFileSync(join(dir, '.claude-flow/hive-mind/state.json'), 'utf8'); }
+ function run(flags: Record<string, unknown>) {
+ return consensus.action!({ args: [], flags, raw: [], cwd: dir } as unknown as CommandContext);
+ }
+ beforeEach(async () => {
+ dir = mkdtempSync(join(tmpdir(), 'ruflo-cli-consensus-'));
+ previous = process.env.CLAUDE_FLOW_CWD;
+ process.env.CLAUDE_FLOW_CWD = dir;
+ vi.clearAllMocks();
+ const init = await tool('hive-mind_init').handler({ consensus: 'quorum' }) as Record<string, unknown>;
+ token = init.hiveToken as string;
+ for (const agentId of ['worker-1', 'worker-2', 'worker-3']) {
+ await tool('hive-mind_join').handler({ agentId, hiveToken: token });
+ }
+ const proposal = await tool('hive-mind_consensus').handler({ action: 'propose', type: 'design', value: 'use-jwt' }) as Record<string, unknown>;
+ proposalId = proposal.proposalId as string;
+ });
+ afterEach(() => {
+ if (previous === undefined) delete process.env.CLAUDE_FLOW_CWD;
+ else process.env.CLAUDE_FLOW_CWD = previous;
+ rmSync(dir, { recursive: true, force: true });
+ });
+ it('JSON vote denial returns failure and leaves real state byte-identical', async () => {
+ const before = bytes();
+ const result = await run({ action: 'vote', proposalId, voterId: 'outsider', vote: 'yes', format: 'json' });
+ expect(result.success).toBe(false);
+ expect(result.exitCode).toBe(1);
+ expect(printed.printJson).toHaveBeenCalledWith(expect.objectContaining({ error: expect.stringContaining('not a registered') }));
+ expect(bytes()).toBe(before);
+ });
+ it('human vote denial prints its reason without a false success message', async () => {
+ const before = bytes();
+ const result = await run({ action: 'vote', proposalId, voterId: 'outsider', vote: 'yes' });
+ expect(result.success).toBe(false);
+ expect(printed.printSuccess).not.toHaveBeenCalled();
+ expect(printed.printError).toHaveBeenCalledWith(expect.stringContaining('not a registered'));
+ expect(bytes()).toBe(before);
+ });
+ it('missing proposal is a failed command, not a vote receipt', async () => {
+ const before = bytes();
+ const result = await run({ action: 'vote', proposalId: 'missing', voterId: 'worker-1', vote: 'yes' });
+ expect(result.success).toBe(false);
+ expect(printed.printSuccess).not.toHaveBeenCalled();
+ expect(bytes()).toBe(before);
+ });
+ it('duplicate votes fail while preserving the already recorded vote', async () => {
+ await run({ action: 'vote', proposalId, voterId: 'worker-1', vote: 'yes', format: 'json' });
+ const before = bytes();
+ const result = await run({ action: 'vote', proposalId, voterId: 'worker-1', vote: 'yes', format: 'json' });
+ expect(result.success).toBe(false);
+ expect(result.exitCode).toBe(1);
+ expect(bytes()).toBe(before);
+ });
+ it('accepted worker vote remains successful and actually persists', async () => {
+ const result = await run({ action: 'vote', proposalId, voterId: 'worker-1', vote: 'yes' });
+ expect(result.success).toBe(true);
+ const state = JSON.parse(bytes());
+ expect(state.consensus.pending[0].votes['worker-1']).toBe(true);
+ expect(printed.printSuccess).toHaveBeenCalledWith('Vote recorded (For: 1, Against: 0)');
+ });
+ it('successful JSON status remains a successful read without a write', async () => {
+ const before = bytes();
+ const result = await run({ action: 'status', proposalId, format: 'json' });
+ expect(result.success).toBe(true);
+ expect(printed.printJson).toHaveBeenCalled();
+ expect(bytes()).toBe(before);
+ });
+});
diff --git a/v3/@claude-flow/cli/src/commands/hive-mind.ts b/v3/@claude-flow/cli/src/commands/hive-mind.ts
index 674f02f8bc..a9e0bce41e 100644
--- a/v3/@claude-flow/cli/src/commands/hive-mind.ts
+++ b/v3/@claude-flow/cli/src/commands/hive-mind.ts
@@ -1207,6 +1207,11 @@ const consensusCommand: Command = {
const action = ctx.flags.action as string || 'list';
try {
const result = await callMCPTool<Record<string, unknown>>('hive-mind_consensus', { action, proposalId: ctx.flags.proposalId, type: ctx.flags.type, value: ctx.flags.value, vote: ctx.flags.vote === 'yes', voterId: ctx.flags.voterId, hiveToken: getHiveTokenForCli() });
+ if (result.error) {
+ if (ctx.flags.format === 'json') output.printJson(result);
+ else output.printError(`Consensus error: ${String(result.error)}`);
+ return { success: false, exitCode: 1, data: result };
+ }
if (ctx.flags.format === 'json') { output.printJson(result); return { success: true, data: result }; }
if (action === 'list') {
output.writeln(output.bold('\nPending Proposals'));