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(swarm): scaleAgents targets total, executeTask catches throws (#1872) (#2000)

rUvc3ab5ef24084
diff --git a/v3/__tests__/integration/swarm-integration.test.ts b/v3/__tests__/integration/swarm-integration.test.ts
index ca01a26a74..5bab3cb4f3 100644
--- a/v3/__tests__/integration/swarm-integration.test.ts
+++ b/v3/__tests__/integration/swarm-integration.test.ts
@@ -122,10 +122,10 @@ describe('Swarm Integration Tests', () => {
expect(stateAfter.topology).toBe('hierarchical');
});
- // SKIP #1872 β€” real bug: SwarmCoordinator doesn't catch errors from
- // agent.executeTask; mocked rejection propagates instead of returning
- // {status:'failed', error:...} as the test (and contract) expects.
- it.skip('should handle agent failures gracefully', async () => {
+ // ruflo#1872 β€” fixed: SwarmCoordinator.executeTask now wraps the
+ // agent call in try/catch so a thrown error becomes a structured
+ // {status:'failed', error} result.
+ it('should handle agent failures gracefully', async () => {
const agent = await coordinator.spawnAgent({
id: 'fragile-agent',
type: 'coder',
@@ -194,10 +194,10 @@ describe('Swarm Integration Tests', () => {
expect(connections.every(c => c.type === 'peer')).toBe(true);
});
- // SKIP #1872 β€” real bug: scaleAgents({count: N}) accumulates rather
- // than targets N total. Scale-down path also broken (1 β†’ 4 β†’ 6 instead
- // of 1 β†’ 4 β†’ 2).
- it.skip('should handle dynamic agent scaling', async () => {
+ // ruflo#1872 β€” fixed: scaleAgents({count:N}) now interprets count as
+ // the TARGET TOTAL of that agent type (spawning or terminating to
+ // reach it) rather than as a delta.
+ it('should handle dynamic agent scaling', async () => {
await coordinator.spawnAgent({ id: 'base-agent', type: 'coder', capabilities: ['code'] });
const initialCount = (await coordinator.listAgents()).length;
diff --git a/v3/src/coordination/application/SwarmCoordinator.ts b/v3/src/coordination/application/SwarmCoordinator.ts
index f92169703c..4e82c4d50d 100644
--- a/v3/src/coordination/application/SwarmCoordinator.ts
+++ b/v3/src/coordination/application/SwarmCoordinator.ts
@@ -202,7 +202,24 @@ export class SwarmCoordinator {
}
const startTime = Date.now();
- const result = await agent.executeTask(task);
+ // ruflo#1872 β€” `agent.executeTask` can throw (mock rejection in
+ // tests, real broker/network errors in production). Previously the
+ // throw propagated up to the caller, defeating the
+ // "TaskResult{status:'failed', error}" contract callers rely on
+ // for graceful degradation. Wrap in try/catch so a crashed agent
+ // produces a structured failure result instead of an unhandled
+ // promise rejection.
+ let result: TaskResult;
+ try {
+ result = await agent.executeTask(task);
+ } catch (err) {
+ result = {
+ taskId: task.id,
+ status: 'failed',
+ error: err instanceof Error ? err.message : String(err),
+ agentId,
+ };
+ }
const duration = Date.now() - startTime;
// Update metrics
@@ -317,25 +334,28 @@ export class SwarmCoordinator {
* Scale agents
*/
async scaleAgents(config: { type: string; count: number }): Promise<void> {
+ // ruflo#1872 β€” `count` is the TARGET TOTAL of this agent type, not
+ // a delta. Previously `count` was added to the existing count, so
+ // calling `scaleAgents({count:3})` then `scaleAgents({count:2})`
+ // ended up at 1+3+2=6 instead of the intuitive 2.
const existingOfType = Array.from(this.agents.values()).filter(
a => a.type === config.type
);
-
const currentCount = existingOfType.length;
- const targetCount = currentCount + config.count;
+ const targetCount = Math.max(0, Math.floor(config.count));
- if (config.count > 0) {
+ if (targetCount > currentCount) {
// Scale up
for (let i = currentCount; i < targetCount; i++) {
await this.spawnAgent({
id: `${config.type}-${Date.now()}-${i}`,
type: config.type,
- capabilities: this.getDefaultCapabilities(config.type)
+ capabilities: this.getDefaultCapabilities(config.type),
});
}
- } else if (config.count < 0) {
- // Scale down
- const toRemove = existingOfType.slice(0, Math.abs(config.count));
+ } else if (targetCount < currentCount) {
+ // Scale down β€” terminate the oldest first so deterministic.
+ const toRemove = existingOfType.slice(0, currentCount - targetCount);
for (const agent of toRemove) {
await this.terminateAgent(agent.id);
}