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

review: restore bounded retry backoff; make perf tests baseline-independent

ruv62cbcb2b6698
diff --git a/v3/@claude-flow/swarm/__tests__/message-bus.event-driven.test.ts b/v3/@claude-flow/swarm/__tests__/message-bus.event-driven.test.ts
index 18bcd5858b..0cfeb8848c 100644
--- a/v3/@claude-flow/swarm/__tests__/message-bus.event-driven.test.ts
+++ b/v3/@claude-flow/swarm/__tests__/message-bus.event-driven.test.ts
@@ -152,4 +152,89 @@ describe('MessageBus - event-driven dispatch (dream-cycle 2026-09-30)', () => {
expect(deliveredEvents).toBe(1);
});
+
+ it('spaces retries with a bounded backoff instead of retrying back to back', async () => {
+ bus = createMessageBus({
+ processingIntervalMs: 5000,
+ retryAttempts: 3,
+ ackTimeoutMs: 1000,
+ });
+ await bus.initialize();
+
+ const invocationTimes: number[] = [];
+ bus.subscribe('agent-bad', () => {
+ invocationTimes.push(performance.now());
+ throw new Error('simulated handler crash');
+ });
+
+ await bus.send({
+ type: 'direct',
+ from: 'agent-good',
+ to: 'agent-bad',
+ payload: {},
+ priority: 'normal',
+ requiresAck: false,
+ ttlMs: 60000,
+ });
+
+ await new Promise((resolve) => setTimeout(resolve, 300));
+
+ // retryAttempts = 3 -> exactly 3 invocations, so backoff is bounded.
+ expect(invocationTimes).toHaveLength(3);
+ // Linear backoff: >=10ms before the 2nd attempt, >=20ms before the 3rd
+ // (1ms tolerance for timer granularity).
+ expect(invocationTimes[1] - invocationTimes[0]).toBeGreaterThanOrEqual(9);
+ expect(invocationTimes[2] - invocationTimes[1]).toBeGreaterThanOrEqual(19);
+ });
+
+ it('shutdown cancels a pending retry', async () => {
+ bus = createMessageBus({
+ processingIntervalMs: 5000,
+ retryAttempts: 3,
+ ackTimeoutMs: 1000,
+ });
+ await bus.initialize();
+
+ let invocations = 0;
+ bus.subscribe('agent-bad', () => {
+ invocations++;
+ throw new Error('simulated handler crash');
+ });
+
+ await bus.send({
+ type: 'direct',
+ from: 'agent-good',
+ to: 'agent-bad',
+ payload: {},
+ priority: 'normal',
+ requiresAck: false,
+ ttlMs: 60000,
+ });
+
+ // Let the first attempt fail (retry now pending), then shut down.
+ await new Promise((resolve) => setTimeout(resolve, 2));
+ expect(invocations).toBe(1);
+ await bus.shutdown();
+ await new Promise((resolve) => setTimeout(resolve, 100));
+
+ expect(invocations).toBe(1);
+ });
+
+ it('orders a same-tick burst by priority (priority reorders within a burst, not across later sends)', async () => {
+ bus = createMessageBus({ processingIntervalMs: 5000, ackTimeoutMs: 1000 });
+ await bus.initialize();
+
+ const order: string[] = [];
+ bus.subscribe('agent-good', (m) => {
+ order.push(m.priority);
+ });
+
+ const base = { type: 'direct' as const, from: 'agent-sender', to: 'agent-good', requiresAck: false, ttlMs: 60000, payload: {} };
+ void bus.send({ ...base, priority: 'low' });
+ void bus.send({ ...base, priority: 'urgent' });
+
+ await new Promise((resolve) => setTimeout(resolve, 50));
+
+ expect(order).toEqual(['urgent', 'low']);
+ });
});
diff --git a/v3/@claude-flow/swarm/__tests__/message-bus.perf.test.ts b/v3/@claude-flow/swarm/__tests__/message-bus.perf.test.ts
index 79b45c8c9e..95b486f6a9 100644
--- a/v3/@claude-flow/swarm/__tests__/message-bus.perf.test.ts
+++ b/v3/@claude-flow/swarm/__tests__/message-bus.perf.test.ts
@@ -1,44 +1,41 @@
import { describe, it, expect, vi } from 'vitest';
import { MessageBus, createMessageBus } from '../src/message-bus.js';
-// Dream Cycle 2026-09-30 (performance) real wall-clock evidence for the
-// MessageBus event-driven dispatch fix. These are sanity/regression bounds,
-// not the baseline-vs-candidate discriminator (that's
-// message-bus.event-driven.test.ts, run stash-isolated against baseline).
-// Numbers are logged so they can be copied into the dream-cycle gist/issue.
+// Dream Cycle 2026-09-30 (performance): regression bounds for the MessageBus
+// event-driven dispatch fix. Every assertion is a call-count bound derived
+// from the configured backstop floor or from the number of messages sent --
+// none depends on wall-clock ratios or on an unmeasured "pre-fix" baseline.
-describe('MessageBus - performance evidence (dream-cycle 2026-09-30)', () => {
- it('idle-window processQueues() wakeups stay near the backstop floor at a production-matching interval', async () => {
+const spyProcessQueues = (bus: MessageBus) =>
+ vi.spyOn(bus as unknown as { processQueues: () => void }, 'processQueues');
+
+describe('MessageBus - dispatch cost bounds (dream-cycle 2026-09-30)', () => {
+ it('idle: processQueues() wakeups are bounded by the >=250ms backstop even when 10ms is configured', async () => {
const bus = createMessageBus({ processingIntervalMs: 10 }); // matches UnifiedSwarmCoordinator/SwarmHub
- const spy = vi.spyOn(bus as unknown as { processQueues: () => void }, 'processQueues');
+ const spy = spyProcessQueues(bus);
await bus.initialize();
- const windowMs = 2000;
+ const windowMs = 1000;
await new Promise((resolve) => setTimeout(resolve, windowMs));
await bus.shutdown();
- const calls = spy.mock.calls.length;
- const naiveBaselineCalls = Math.floor(windowMs / 10); // what the old 10ms poll would produce
- const reduction = 1 - calls / naiveBaselineCalls;
-
- console.log(
- `[perf] idle-window (${windowMs}ms, processingIntervalMs=10): candidate=${calls} calls, ` +
- `pre-fix-poll-equivalent=${naiveBaselineCalls} calls, reduction=${(reduction * 100).toFixed(1)}%`
- );
-
- expect(calls).toBeLessThanOrEqual(Math.ceil(windowMs / 250) + 1);
- expect(reduction).toBeGreaterThan(0.9);
+ // The backstop interval is floored at 250ms, so at most ceil(1000/250)
+ // ticks (+1 for timer jitter) can occur with no traffic.
+ expect(spy.mock.calls.length).toBeLessThanOrEqual(Math.ceil(windowMs / 250) + 1);
});
- it('single-message delivery latency is near-instant (event-driven) at a production-matching interval', async () => {
+ it('sparse traffic: dispatch passes are bounded by messages sent, not by elapsed time', async () => {
const bus = createMessageBus({ processingIntervalMs: 10 });
+ const spy = spyProcessQueues(bus);
await bus.initialize();
- bus.subscribe('agent-good', () => {});
- const samples: number[] = [];
+ let delivered = 0;
+ bus.subscribe('agent-good', () => {
+ delivered++;
+ });
+
const N = 20;
for (let i = 0; i < N; i++) {
- const start = performance.now();
await new Promise<void>((resolve) => {
bus.once('message.delivered', () => resolve());
void bus.send({
@@ -51,23 +48,18 @@ describe('MessageBus - performance evidence (dream-cycle 2026-09-30)', () => {
ttlMs: 60000,
});
});
- samples.push(performance.now() - start);
}
await bus.shutdown();
- const mean = samples.reduce((a, b) => a + b, 0) / N;
- const max = Math.max(...samples);
- console.log(
- `[perf] single-message delivery latency (N=${N}, processingIntervalMs=10): mean=${mean.toFixed(3)}ms max=${max.toFixed(3)}ms`
- );
-
- // Old interval-driven dispatch averaged ~half the 10ms tick (~5ms) with
- // worst case near 10ms; event-driven dispatch should land well under that.
- expect(mean).toBeLessThan(5);
+ expect(delivered).toBe(N);
+ // One event-driven pass per message; one extra per backstop tick that
+ // could land during the (short) run is covered by the slack of N.
+ expect(spy.mock.calls.length).toBeLessThanOrEqual(2 * N);
});
- it('saturated-load throughput does not regress vs. the pre-fix batched-poll baseline', async () => {
+ it('saturated burst: all messages are delivered in about total/10 passes (per-agent batch cap), not one pass per message', async () => {
const bus = createMessageBus({ processingIntervalMs: 10 });
+ const spy = spyProcessQueues(bus);
await bus.initialize();
let delivered = 0;
@@ -76,10 +68,9 @@ describe('MessageBus - performance evidence (dream-cycle 2026-09-30)', () => {
});
const TOTAL = 2000;
- const start = performance.now();
- const sendPromises: Promise<string>[] = [];
+ const sends: Promise<string>[] = [];
for (let i = 0; i < TOTAL; i++) {
- sendPromises.push(
+ sends.push(
bus.send({
type: 'direct',
from: 'agent-sender',
@@ -91,27 +82,19 @@ describe('MessageBus - performance evidence (dream-cycle 2026-09-30)', () => {
})
);
}
- await Promise.all(sendPromises);
+ await Promise.all(sends);
- // Drain: wait for delivery to catch up (event loop needs to process the
- // setImmediate-scheduled processQueues()/deliverMessage() chain).
- const deadline = performance.now() + 10000;
- while (delivered < TOTAL && performance.now() < deadline) {
+ // Bounded drain wait (completion is the assertion, not its duration).
+ const deadline = Date.now() + 10000;
+ while (delivered < TOTAL && Date.now() < deadline) {
await new Promise((resolve) => setTimeout(resolve, 5));
}
- const elapsed = performance.now() - start;
await bus.shutdown();
- const throughput = TOTAL / (elapsed / 1000);
- console.log(
- `[perf] saturated throughput (N=${TOTAL}, processingIntervalMs=10): delivered=${delivered}/${TOTAL} ` +
- `elapsed=${elapsed.toFixed(1)}ms throughput=${throughput.toFixed(0)} msg/s`
- );
-
expect(delivered).toBe(TOTAL);
- // Module header targets 1000+ msgs/sec; regression threshold from the
- // frozen hypothesis (<=5% vs baseline) is checked via the stash-isolated
- // comparison recorded in the PR/issue — this is the standalone sanity floor.
- expect(throughput).toBeGreaterThan(1000);
+ // processQueues() takes <=10 messages per agent per pass, so a full drain
+ // needs ~TOTAL/10 passes; a handful of extra backstop ticks is allowed.
+ expect(spy.mock.calls.length).toBeGreaterThanOrEqual(TOTAL / 10);
+ expect(spy.mock.calls.length).toBeLessThanOrEqual(TOTAL / 10 + 10);
});
});
diff --git a/v3/@claude-flow/swarm/src/message-bus.ts b/v3/@claude-flow/swarm/src/message-bus.ts
index 22d51a4045..fc039c1ee8 100644
--- a/v3/@claude-flow/swarm/src/message-bus.ts
+++ b/v3/@claude-flow/swarm/src/message-bus.ts
@@ -241,6 +241,11 @@ export class MessageBus extends EventEmitter implements IMessageBus {
// only, so a caller-configured processingIntervalMs is floored here
// rather than driving per-tick dispatch directly (dream-cycle 2026-09-30).
private static readonly MIN_BACKSTOP_INTERVAL_MS = 250;
+ // Retries used to be spaced by the poll interval (~10ms in production);
+ // event-driven dispatch would otherwise retry a failing handler back to
+ // back. Linear backoff (attempt * base), bounded by retryAttempts.
+ private static readonly RETRY_BACKOFF_BASE_MS = 10;
+ private retryTimers: Set<NodeJS.Timeout> = new Set();
constructor(config: Partial<MessageBusConfig> = {}) {
super();
@@ -282,6 +287,11 @@ export class MessageBus extends EventEmitter implements IMessageBus {
// schedule work post-shutdown at all.
this.isShutdown = true;
+ for (const timer of this.retryTimers) {
+ clearTimeout(timer);
+ }
+ this.retryTimers.clear();
+
if (this.processingInterval) {
clearInterval(this.processingInterval);
this.processingInterval = undefined;
@@ -570,8 +580,20 @@ export class MessageBus extends EventEmitter implements IMessageBus {
// drains — a failing broadcast subscriber's retries (and eventual
// message.failed) are silently lost. Only the direct-message path
// is bounded by this fix.
- this.addToQueue(message.to, message, entry.attempts);
- this.scheduleProcessing();
+ //
+ // The re-queue itself is delayed (not just the dispatch) so the
+ // backstop interval cannot pick the message up early either.
+ const backoffMs = MessageBus.RETRY_BACKOFF_BASE_MS * entry.attempts;
+ const timer = setTimeout(() => {
+ this.retryTimers.delete(timer);
+ if (this.isShutdown) {
+ return;
+ }
+ this.addToQueue(message.to, message, entry.attempts);
+ this.scheduleProcessing();
+ }, backoffMs);
+ timer.unref?.();
+ this.retryTimers.add(timer);
this.emit('message.retry', {
messageId: message.id,
attempt: entry.attempts