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(agents): merge async registry updates without reviving retired agents

Rudy Celeklib5185ccc844f
diff --git a/v3/@claude-flow/cli/__tests__/agent-registry-concurrency-r50.test.ts b/v3/@claude-flow/cli/__tests__/agent-registry-concurrency-r50.test.ts
new file mode 100644
index 0000000000..abcf8d9f65
--- /dev/null
+++ b/v3/@claude-flow/cli/__tests__/agent-registry-concurrency-r50.test.ts
@@ -0,0 +1,82 @@
+import { beforeEach, afterEach, expect, it, vi } from 'vitest';
+import { mkdtempSync, readFileSync, writeFileSync, rmSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+const state = vi.hoisted(() => ({ cwd: '', route: vi.fn(), branch: vi.fn(), failNextWrite: false }));
+vi.mock('node:fs', async original => { const fs = await original<typeof import('node:fs')>(); return { ...fs, writeFileSync: (...args: any[]) => { if (state.failNextWrite) { state.failNextWrite=false; throw new Error('fixture write failure'); } return (fs.writeFileSync as any)(...args); } }; });
+vi.mock('../src/mcp-tools/types.js', () => ({ getProjectCwd: () => state.cwd }));
+vi.mock('../src/ruvector/graph-backend.js', () => ({ addNode: vi.fn() }));
+vi.mock('../src/ruvector/model-router.js', () => ({ recordModelOutcome: vi.fn(), recordModelOutcomeByModelId: vi.fn() }));
+vi.mock('../src/mcp-tools/swarm-tools.js', () => ({ pheromoneAgentEligibility: () => ({ eligible: true }), loadSwarmStore: () => ({ swarms: {} }), saveSwarmStore: vi.fn() }));
+vi.mock('../src/ruvector/enhanced-model-router.js', () => ({ getEnhancedModelRouter: () => ({route:state.route}) }));
+vi.mock('../src/ruvector/task-embedder.js', () => ({ embedTaskWithCache: async () => undefined }));
+vi.mock('../src/services/swarm-memory-branches.js', () => ({ SwarmMemoryBranches: class { branchForAgent = state.branch; } }));
+import { agentTools } from '../src/mcp-tools/agent-tools.js';
+const call = (name: string, input: object) => agentTools.find(t => t.name === name)!.handler(input) as Promise<any>;
+const spawn = (agentId: string) => call('agent_spawn', { agentType:'coder',agentId,model:'sonnet' });
+const agents = () => JSON.parse(readFileSync(join(state.cwd,'.claude-flow/agents/store.json'),'utf8')).agents;
+const deferred = <T = Response>() => { let resolve!: (r: T) => void; const promise = new Promise<T>(r => resolve=r); return {promise,resolve}; };
+const response = (ok=true) => new Response(JSON.stringify(ok ? {id:'msg',model:'test',content:[{type:'text',text:'done'}],usage:{input_tokens:1,output_tokens:1}} : {error:'rejected'}), {status:ok?200:400});
+beforeEach(() => { state.cwd=mkdtempSync(join(tmpdir(),'ruflo-registry-'));vi.stubEnv('ANTHROPIC_API_KEY','test-fixture');vi.stubEnv('CLAUDE_FLOW_ROUTER_TRAJECTORY','0');vi.stubEnv('CLAUDE_FLOW_RUN_TRANSCRIPTS','0'); });
+afterEach(() => { vi.useRealTimers();state.failNextWrite=false;vi.unstubAllGlobals();vi.unstubAllEnvs();rmSync(state.cwd,{recursive:true,force:true}); });
+it('retains simultaneous registrations after model-selection awaits', async () => {
+ const routing=deferred<any>();state.route.mockReturnValueOnce(routing.promise);
+ const first=call('agent_spawn',{agentType:'coder',agentId:'first',task:'route me'});
+ await vi.waitFor(()=>expect(state.route).toHaveBeenCalled());
+ await spawn('second');routing.resolve({model:'sonnet',tier:2});await first;
+ expect(Object.keys(agents()).sort()).toEqual(['first','second']);
+});
+it('preserves sibling registrations and updated agent config after execution completes', async () => {
+ await spawn('first');const pending=deferred();vi.stubGlobal('fetch',vi.fn(()=>pending.promise));
+ const execution=call('agent_execute',{agentId:'first',prompt:'test'});
+ await call('agent_update',{agentId:'first',config:{instructions:'new configuration'}});
+ await spawn('second');pending.resolve(response());await execution;
+ expect(Object.keys(agents()).sort()).toEqual(['first','second']);
+ expect(agents().first.config.instructions).toBe('new configuration');
+ expect(agents().first.lastResult.output).toBe('done');
+});
+it.each([true,false])('never resurrects an agent terminated during execution (provider success: %s)', async ok => {
+ await spawn('first');const pending=deferred();vi.stubGlobal('fetch',vi.fn(()=>pending.promise));
+ const execution=call('agent_execute',{agentId:'first',prompt:'test'});
+ await call('agent_terminate',{agentId:'first'});pending.resolve(response(ok));await execution;
+ expect(agents().first.status).toBe('terminated');
+});
+it('keeps one agent busy until its last overlapping request settles and retains both counts', async () => {
+ await spawn('first');const a=deferred(),b=deferred();vi.stubGlobal('fetch',vi.fn().mockReturnValueOnce(a.promise).mockReturnValueOnce(b.promise));
+ const first=call('agent_execute',{agentId:'first',prompt:'one'});const second=call('agent_execute',{agentId:'first',prompt:'two'});
+ a.resolve(response());await first;expect(agents().first.status).toBe('busy');expect(agents().first.taskCount).toBe(2);
+ b.resolve(response());await second;expect(agents().first.status).toBe('idle');expect(agents().first.taskCount).toBe(2);
+});
+it('does not reinsert an agent removed while provider work is pending', async () => {
+ await spawn('first');const pending=deferred();vi.stubGlobal('fetch',vi.fn(()=>pending.promise));
+ const execution=call('agent_execute',{agentId:'first',prompt:'test'});
+ const path=join(state.cwd,'.claude-flow/agents/store.json');writeFileSync(path,JSON.stringify({agents:{},version:'3.0.0'}));
+ pending.resolve(response());await execution;expect(agents()).toEqual({});
+});
+
+it('merges delayed COW metadata without discarding a sibling or newer config', async () => {
+ const branch=deferred<any>();state.branch.mockReturnValueOnce(branch.promise);
+ const first=call('agent_spawn',{agentType:'coder',agentId:'first',model:'sonnet',memoryBase:'fixture.rvf'});
+ await vi.waitFor(()=>expect(state.branch).toHaveBeenCalled());
+ await call('agent_update',{agentId:'first',config:{instructions:'new'}});await spawn('second');
+ branch.resolve({branchPath:'fixture-branch.rvf',basePath:'fixture.rvf'});await first;
+ expect(Object.keys(agents()).sort()).toEqual(['first','second']);
+ expect(agents().first.config.instructions).toBe('new');
+ expect(agents().first.memoryBranch).toBe('fixture-branch.rvf');
+});
+
+it('does not attach old results to a replacement with the same agent ID', async () => {
+ vi.useFakeTimers();vi.setSystemTime(new Date('2026-01-01T00:00:00Z'));
+ await spawn('first');const old=agents().first.createdAt;const pending=deferred();vi.stubGlobal('fetch',vi.fn(()=>pending.promise));
+ const execution=call('agent_execute',{agentId:'first',prompt:'old incarnation'});
+ writeFileSync(join(state.cwd,'.claude-flow/agents/store.json'),JSON.stringify({agents:{},version:'3.0.0'}));
+ vi.setSystemTime(new Date('2026-01-01T00:00:01Z'));await spawn('first');
+ pending.resolve(response());await execution;
+ expect(agents().first.createdAt).not.toBe(old);expect(agents().first.lastResult).toBeUndefined();expect(agents().first.taskCount).toBe(0);
+});
+it('releases execution accounting even if completion persistence throws', async () => {
+ await spawn('first');const pending=deferred();const fetch=vi.fn().mockReturnValueOnce(pending.promise).mockResolvedValue(response());vi.stubGlobal('fetch',fetch);
+ const execution=call('agent_execute',{agentId:'first',prompt:'failing write'});
+ state.failNextWrite=true;pending.resolve(response());await expect(execution).rejects.toThrow('fixture write failure');
+ await call('agent_execute',{agentId:'first',prompt:'next call'});expect(agents().first.status).toBe('idle');
+});
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..b0f3170646 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
@@ -552,6 +552,9 @@ export interface AgentExecuteResult {
fallbackHistory?: Array<{ modelId: string; error: string }>;
}
+// Tracks overlapping calls in this process; persisted state remains authoritative.
+const activeExecutions = new Map<string, number>();
+
export async function executeAgentTask(input: AgentExecuteInput): Promise<AgentExecuteResult> {
const store = loadAgentStore();
const agent = store.agents[input.agentId];
@@ -598,10 +601,14 @@ export async function executeAgentTask(input: AgentExecuteInput): Promise<AgentE
`Agent ID: ${input.agentId}. Domain: ${agent.domain ?? 'general'}. ` +
`Respond directly and stay focused on the task. If you need information you don't have, state that explicitly.`;
+ const initialModelId = agent.modelId;
agent.status = 'busy';
agent.taskCount = (agent.taskCount || 0) + 1;
saveAgentStore(store);
+ const executionKey = JSON.stringify([getAgentPath(), input.agentId, agent.createdAt]);
+ activeExecutions.set(executionKey, (activeExecutions.get(executionKey) || 0) + 1);
+ try {
const startedAt = Date.now();
// #2042 — delegate to callAnthropicMessages so the v3 provider router
@@ -667,7 +674,6 @@ export async function executeAgentTask(input: AgentExecuteInput): Promise<AgentE
}
}
- agent.status = 'idle';
// ADR-149 — close the bandit feedback loop. `recordModelOutcome` updates
// the Beta(α,β) prior for the agent's tier so the Thompson sampler learns
@@ -756,6 +762,19 @@ export async function executeAgentTask(input: AgentExecuteInput): Promise<AgentE
// Silent — bandit feedback must never block routing.
}
+ const remaining = (activeExecutions.get(executionKey) || 1) - 1;
+ const persistCompletion = (out?: AgentExecuteResult): void => {
+ // Awaited provider/feedback work may have overlapped registration, updates,
+ // termination or deletion. Never replace that newer registry snapshot.
+ const latest = loadAgentStore();
+ const current = latest.agents[input.agentId];
+ if (!current || current.createdAt !== agent.createdAt) return;
+ if (current.status === 'busy') current.status = remaining > 0 ? 'busy' : 'idle';
+ if (agent.modelId !== initialModelId && current.modelId === initialModelId) current.modelId = agent.modelId;
+ if (out) current.lastResult = out as unknown as Record<string, unknown>;
+ saveAgentStore(latest);
+ };
+
if (result.success) {
const out: AgentExecuteResult = {
success: true,
@@ -768,12 +787,11 @@ export async function executeAgentTask(input: AgentExecuteInput): Promise<AgentE
durationMs: result.durationMs ?? Date.now() - startedAt,
...(fallbackHistory.length > 0 ? { fallbackHistory } : {}),
};
- agent.lastResult = out as unknown as Record<string, unknown>;
- saveAgentStore(store);
+ persistCompletion(out);
return out;
}
- saveAgentStore(store);
+ persistCompletion();
// No-provider-configured error → surface the same actionable message
// the router built, with a #2042-aware remediation pointer.
const noProvider = (result.error || '').includes('No LLM provider configured');
@@ -790,5 +808,9 @@ export async function executeAgentTask(input: AgentExecuteInput): Promise<AgentE
'Or set RUFLO_PROVIDER=openrouter|ollama to force a specific provider.',
}),
};
+ } finally {
+ const pending = (activeExecutions.get(executionKey) || 1) - 1;
+ if (pending > 0) activeExecutions.set(executionKey, pending);
+ else activeExecutions.delete(executionKey);
+ }
}
-
diff --git a/v3/@claude-flow/cli/src/mcp-tools/agent-tools.ts b/v3/@claude-flow/cli/src/mcp-tools/agent-tools.ts
index 63e5538cf1..4f2728946c 100644
--- a/v3/@claude-flow/cli/src/mcp-tools/agent-tools.ts
+++ b/v3/@claude-flow/cli/src/mcp-tools/agent-tools.ts
@@ -318,7 +318,6 @@ export const agentTools: MCPTool[] = [
return { success: false, error: `Input validation failed: ${validation.errors.join('; ')}` };
}
- const store = loadAgentStore();
const agentId = (input.agentId as string) || `agent-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
const agentType = input.agentType as string;
const config = (input.config as Record<string, unknown>) || {};
@@ -367,6 +366,7 @@ export const agentTools: MCPTool[] = [
...(routingResult.openrouterModel ? { openrouterModel: routingResult.openrouterModel } : {}),
};
+ const store = loadAgentStore();
store.agents[agentId] = agent;
saveAgentStore(store);
@@ -422,8 +422,13 @@ export const agentTools: MCPTool[] = [
memoryBranch = br.branchPath;
agent.memoryBranch = br.branchPath;
agent.memoryBase = br.basePath;
- store.agents[agentId] = agent;
- saveAgentStore(store);
+ const latest = loadAgentStore();
+ const current = latest.agents[agentId];
+ if (current && current.status !== 'terminated') {
+ current.memoryBranch = br.branchPath;
+ current.memoryBase = br.basePath;
+ saveAgentStore(latest);
+ }
}
// else: degraded (agenticow missing / kill-switched) — agent stands
// without an isolated branch; callers see no memoryBranch field.