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(providers): honor explicit execution provider selection

Rudy Celekli4c2c830635a9
diff --git a/v3/@claude-flow/cli/__tests__/agent-provider-selection.test.ts b/v3/@claude-flow/cli/__tests__/agent-provider-selection.test.ts
new file mode 100644
index 0000000000..b4e1f78d12
--- /dev/null
+++ b/v3/@claude-flow/cli/__tests__/agent-provider-selection.test.ts
@@ -0,0 +1,68 @@
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+import { mkdtempSync, mkdirSync, rmSync, writeFileSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+import { callAnthropicMessages, executeAgentTask } from '../src/mcp-tools/agent-execute-core.js';
+
+vi.mock('../src/ruvector/model-router.js', () => ({
+ recordModelOutcome: vi.fn(), recordModelOutcomeByModelId: vi.fn(),
+}));
+
+describe('explicit execution provider boundaries', () => {
+ let dir: string;
+ let fetchMock: ReturnType<typeof vi.fn>;
+ beforeEach(() => {
+ dir = mkdtempSync(join(tmpdir(), 'ruflo-provider-selection-'));
+ for (const key of ['ANTHROPIC_API_KEY', 'OPENROUTER_API_KEY', 'OLLAMA_API_KEY', 'RUFLO_PROVIDER', 'OLLAMA_BASE_URL', 'OPENROUTER_BASE_URL']) vi.stubEnv(key, '');
+ vi.stubEnv('CLAUDE_FLOW_CWD', dir);
+ fetchMock = vi.fn().mockResolvedValue({ ok: true, json: async () => ({
+ id: 'response', model: 'test-model', content: [{type: 'text', text: 'done'}],
+ choices: [{message: {content: 'done'}, finish_reason: 'stop'}],
+ usage: {input_tokens: 1, output_tokens: 1}, stop_reason: 'end_turn',
+ }) });
+ vi.stubGlobal('fetch', fetchMock);
+ });
+ afterEach(() => { vi.unstubAllEnvs(); vi.unstubAllGlobals(); rmSync(dir, {recursive: true, force: true}); });
+
+ it.each(['argument', 'environment'])('honors explicit Ollama from %s when an OpenRouter key also exists', async source => {
+ vi.stubEnv('OPENROUTER_API_KEY', 'openrouter-test');
+ vi.stubEnv('OLLAMA_API_KEY', 'ollama-test');
+ if (source === 'environment') vi.stubEnv('RUFLO_PROVIDER', 'ollama');
+ const result = await callAnthropicMessages({prompt: 'test', ...(source === 'argument' ? {provider: 'ollama' as const} : {})});
+ expect(result.success).toBe(true);
+ expect(fetchMock.mock.calls[0][0]).toBe('https://ollama.com/v1/chat/completions');
+ });
+
+ it.each(['anthropic', 'ollama', 'openrouter'] as const)('fails closed when explicit %s lacks credentials despite another configured provider', async provider => {
+ vi.stubEnv(provider === 'anthropic' ? 'OPENROUTER_API_KEY' : 'ANTHROPIC_API_KEY', 'other-provider-test');
+ const result = await callAnthropicMessages({prompt: 'test', provider});
+ expect(result.success).toBe(false);
+ expect(result.error?.toLowerCase()).toContain(provider);
+ expect(fetchMock).not.toHaveBeenCalled();
+ });
+
+ it('keeps key-based inference when no provider was selected', async () => {
+ vi.stubEnv('OPENROUTER_API_KEY', 'openrouter-test');
+ expect((await callAnthropicMessages({prompt: 'test'})).success).toBe(true);
+ expect(fetchMock.mock.calls[0][0]).toBe('https://openrouter.ai/api/v1/chat/completions');
+ });
+
+ it('lets a per-agent provider override the environment default', async () => {
+ vi.stubEnv('ANTHROPIC_API_KEY', 'anthropic-test');
+ vi.stubEnv('OLLAMA_API_KEY', 'ollama-test');
+ vi.stubEnv('RUFLO_PROVIDER', 'ollama');
+ expect((await callAnthropicMessages({prompt: 'test', provider: 'anthropic'})).success).toBe(true);
+ expect(fetchMock.mock.calls[0][0]).toBe('https://api.anthropic.com/v1/messages');
+ });
+
+ it('honors the stored agent provider through actual agent execution', async () => {
+ vi.stubEnv('OPENROUTER_API_KEY', 'openrouter-test');
+ vi.stubEnv('OLLAMA_API_KEY', 'ollama-test');
+ const agents = join(dir, '.claude-flow', 'agents'); mkdirSync(agents, {recursive: true});
+ writeFileSync(join(agents, 'store.json'), JSON.stringify({version: '3.0.0', agents: {
+ selected: {agentId: 'selected', agentType: 'coder', status: 'idle', health: 100, taskCount: 0, config: {}, createdAt: new Date().toISOString(), provider: 'ollama', model: 'sonnet'},
+ }}));
+ expect((await executeAgentTask({agentId: 'selected', prompt: 'test'})).success).toBe(true);
+ expect(fetchMock.mock.calls[0][0]).toBe('https://ollama.com/v1/chat/completions');
+ });
+});
diff --git a/v3/@claude-flow/cli/src/mcp-tools/agent-execute-core.ts b/v3/@claude-flow/cli/src/mcp-tools/agent-execute-core.ts
index 4debea7890..9bbd90a4f8 100644
--- a/v3/@claude-flow/cli/src/mcp-tools/agent-execute-core.ts
+++ b/v3/@claude-flow/cli/src/mcp-tools/agent-execute-core.ts
@@ -184,7 +184,7 @@ export async function callAnthropicMessages(input: AnthropicCallInput): Promise<
// branch above).
const openrouterKey = process.env.OPENROUTER_API_KEY;
const useOpenRouter =
- explicitProvider === 'openrouter' || (!anthropicKey && !!openrouterKey);
+ explicitProvider === 'openrouter' || (!explicitProvider && !anthropicKey && !!openrouterKey);
// #2962 — only consult the persisted config when a candidate is actually
// relevant (explicit choice, or no env key found anywhere), so a normal
// ANTHROPIC_API_KEY-only setup never pays a config-file read.
@@ -195,7 +195,7 @@ export async function callAnthropicMessages(input: AnthropicCallInput): Promise<
const persistedOpenRouter =
explicitProvider === 'openrouter' && !openrouterKey ? getPersistedProviderConfig('openrouter') : undefined;
const useOllama =
- explicitProvider === 'ollama' || (!anthropicKey && !openrouterKey && (!!ollamaKey || !!persistedOllama));
+ explicitProvider === 'ollama' || (!explicitProvider && !anthropicKey && !openrouterKey && (!!ollamaKey || !!persistedOllama));
if (useOpenRouter) {
const apiKey = openrouterKey || persistedOpenRouter?.apiKey;
@@ -212,6 +212,7 @@ export async function callAnthropicMessages(input: AnthropicCallInput): Promise<
defaultModel: process.env.OPENROUTER_DEFAULT_MODEL || persistedOpenRouter?.model || 'anthropic/claude-sonnet-4-6',
});
}
+ return { success: false, error: 'OpenRouter was selected but no API key is configured.' };
}
if (useOllama) {
// #2962 — retire the undocumented OLLAMA_API_KEY=local sentinel
@@ -230,6 +231,7 @@ export async function callAnthropicMessages(input: AnthropicCallInput): Promise<
model: input.model || persistedOllama?.model,
});
}
+ return { success: false, error: 'Ollama was selected but no API key or self-hosted endpoint is configured.' };
}
if (!anthropicKey) {
return {
@@ -791,4 +793,3 @@ export async function executeAgentTask(input: AgentExecuteInput): Promise<AgentE
}),
};
}
-