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): retain provider deadlines through response consumption

Rudy Celekli45a5d55e2c72
diff --git a/v3/@claude-flow/cli/__tests__/agent-provider-timeout.test.ts b/v3/@claude-flow/cli/__tests__/agent-provider-timeout.test.ts
new file mode 100644
index 0000000000..4ca8a47e33
--- /dev/null
+++ b/v3/@claude-flow/cli/__tests__/agent-provider-timeout.test.ts
@@ -0,0 +1,58 @@
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+import { createServer, type Server } from 'node:http';
+import { mkdtempSync, rmSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+import { callAnthropicMessages } from '../src/mcp-tools/agent-execute-core.js';
+
+const providers = ['anthropic', 'openrouter', 'ollama'] as const;
+describe('provider request lifetime', () => {
+ let dir: string;
+ let server: Server | undefined;
+ let releaseBody: ReturnType<typeof setTimeout> | undefined;
+ beforeEach(() => {
+ dir = mkdtempSync(join(tmpdir(), 'ruflo-provider-deadline-'));
+ vi.stubEnv('CLAUDE_FLOW_CWD', dir);
+ for (const key of ['ANTHROPIC_API_KEY', 'ANTHROPIC_AUTH_TOKEN', 'ANTHROPIC_BASE_URL', 'OPENROUTER_API_KEY', 'OLLAMA_API_KEY', 'RUFLO_PROVIDER', 'OLLAMA_BASE_URL', 'OPENROUTER_BASE_URL']) vi.stubEnv(key, '');
+ });
+ afterEach(async () => {
+ if (releaseBody) clearTimeout(releaseBody);
+ releaseBody = undefined;
+ if (server) { server.closeAllConnections(); await new Promise<void>(resolve => server!.close(() => resolve())); }
+ server = undefined;
+ vi.useRealTimers(); vi.unstubAllEnvs(); vi.unstubAllGlobals();
+ rmSync(dir, {recursive: true, force: true});
+ });
+
+ it.each(providers)('keeps the %s deadline active after headers and cancels a stalled body', async provider => {
+ vi.stubEnv(`${provider.toUpperCase()}_API_KEY`, 'test-credential');
+ let cancelled = false;
+ server = createServer((_req, res) => {
+ res.on('close', () => { cancelled = !res.writableEnded; });
+ res.writeHead(200, {'content-type':'application/json'}); res.flushHeaders();
+ // Bounded even on the broken implementation: no hanging test/server.
+ releaseBody = setTimeout(() => res.end(JSON.stringify({
+ id:'late', model:'test-model', content:[{type:'text',text:'late result'}],
+ choices:[{message:{content:'late result'},finish_reason:'stop'}],
+ usage:{input_tokens:1,output_tokens:1}, stop_reason:'end_turn',
+ })), 400);
+ });
+ await new Promise<void>(resolve => server!.listen(0, '127.0.0.1', resolve));
+ const base = `http://127.0.0.1:${(server.address() as {port:number}).port}`;
+ const localFetch = globalThis.fetch;
+ // Exercise real HTTP body cancellation, while preventing provider API spend.
+ vi.stubGlobal('fetch', (url: string, init: RequestInit) => localFetch(base + new URL(url).pathname, init));
+ const result = await callAnthropicMessages({prompt:'test', provider, timeoutMs:80});
+ expect(result.success).toBe(false);
+ expect(result.error).toMatch(/abort|timeout/i);
+ await vi.waitFor(() => expect(cancelled).toBe(true), {timeout:150});
+ });
+
+ it.each(providers)('clears the %s timer when fetch rejects', async provider => {
+ vi.stubEnv(`${provider.toUpperCase()}_API_KEY`, 'test-credential');
+ vi.useFakeTimers();
+ vi.stubGlobal('fetch', vi.fn().mockRejectedValue(new Error('connection refused')));
+ expect((await callAnthropicMessages({prompt:'test',provider,timeoutMs:10000})).success).toBe(false);
+ expect(vi.getTimerCount()).toBe(0);
+ });
+});
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..588f8d2dd7 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
@@ -240,9 +240,9 @@ export async function callAnthropicMessages(input: AnthropicCallInput): Promise<
}
const model = input.model || DEFAULT_ANTHROPIC_MODEL;
const startedAt = Date.now();
+ const controller = new AbortController();
+ const timer = setTimeout(() => controller.abort(), input.timeoutMs || 60000);
try {
- const controller = new AbortController();
- const timer = setTimeout(() => controller.abort(), input.timeoutMs || 60000);
const res = await fetch('https://api.anthropic.com/v1/messages', {
method: 'POST',
headers: {
@@ -271,7 +271,6 @@ export async function callAnthropicMessages(input: AnthropicCallInput): Promise<
}),
signal: controller.signal,
});
- clearTimeout(timer);
if (!res.ok) {
const errText = await res.text().catch(() => '<unreadable error body>');
return { success: false, model, error: `Anthropic API error ${res.status}: ${errText.slice(0, 400)}` };
@@ -307,6 +306,8 @@ export async function callAnthropicMessages(input: AnthropicCallInput): Promise<
error: err instanceof Error ? err.message : String(err),
durationMs: Date.now() - startedAt,
};
+ } finally {
+ clearTimeout(timer);
}
}
@@ -340,9 +341,9 @@ async function callOllamaCompat(
// (the daemon binds to 11434 with no auth by default), but Ollama Cloud
// does. Send the bearer when the key is non-empty AND looks cloud-shaped.
const sendAuth = !!input.apiKey && input.apiKey !== 'local';
+ const controller = new AbortController();
+ const timer = setTimeout(() => controller.abort(), input.timeoutMs || 60000);
try {
- const controller = new AbortController();
- const timer = setTimeout(() => controller.abort(), input.timeoutMs || 60000);
const res = await fetch(url, {
method: 'POST',
headers: {
@@ -362,7 +363,6 @@ async function callOllamaCompat(
}),
signal: controller.signal,
});
- clearTimeout(timer);
if (!res.ok) {
const errText = await res.text().catch(() => '<unreadable error body>');
return { success: false, model, error: `Ollama API error ${res.status} at ${url}: ${errText.slice(0, 400)}` };
@@ -402,6 +402,8 @@ async function callOllamaCompat(
error: err instanceof Error ? err.message : String(err),
durationMs: Date.now() - startedAt,
};
+ } finally {
+ clearTimeout(timer);
}
}
@@ -426,9 +428,9 @@ async function callOpenAICompat(
const startedAt = Date.now();
const base = input.baseUrl.replace(/\/+$/, '');
const url = `${base}/v1/chat/completions`;
+ const controller = new AbortController();
+ const timer = setTimeout(() => controller.abort(), input.timeoutMs || 60000);
try {
- const controller = new AbortController();
- const timer = setTimeout(() => controller.abort(), input.timeoutMs || 60000);
const messages: Array<{ role: string; content: string }> = [];
if (input.systemPrompt) messages.push({ role: 'system', content: input.systemPrompt });
messages.push({ role: 'user', content: input.prompt });
@@ -450,7 +452,6 @@ async function callOpenAICompat(
}),
signal: controller.signal,
});
- clearTimeout(timer);
if (!res.ok) {
const errText = await res.text().catch(() => '<unreadable error body>');
return { success: false, model, error: `${input.providerLabel} API error ${res.status}: ${errText.slice(0, 400)}` };
@@ -483,6 +484,8 @@ async function callOpenAICompat(
error: err instanceof Error ? err.message : String(err),
durationMs: Date.now() - startedAt,
};
+ } finally {
+ clearTimeout(timer);
}
}