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): preserve urgent messages on queue overflow (#4235)

Rudy Mizrahi Celekli3bc6cfa6e532
diff --git a/v3/@claude-flow/swarm/__tests__/message-bus.overflow-priority.test.ts b/v3/@claude-flow/swarm/__tests__/message-bus.overflow-priority.test.ts
new file mode 100644
index 0000000000..eb23f2be42
--- /dev/null
+++ b/v3/@claude-flow/swarm/__tests__/message-bus.overflow-priority.test.ts
@@ -0,0 +1,43 @@
+import { afterEach, describe, expect, it } from 'vitest';
+import { createMessageBus, type MessageBus } from '../src/message-bus.js';
+import type { Message } from '../src/types.js';
+
+let bus: MessageBus;
+afterEach(async () => { await bus.shutdown(); });
+
+async function enqueue(priorities: Message['priority'][], capacity: number): Promise<string[]> {
+ bus = createMessageBus({ maxQueueSize: capacity });
+ for (const [index, priority] of priorities.entries()) {
+ await bus.send({
+ from: 'sender', to: 'target', type: 'direct', priority,
+ payload: String(index), requiresAck: false, ttlMs: 60_000,
+ });
+ }
+ expect(bus.getQueueDepth()).toBe(capacity);
+ return bus.getMessages('target').map((message) => message.payload as string);
+}
+
+describe('MessageBus queue overflow priority', () => {
+ it('retains urgent messages when a full queue also contains high priority', async () => {
+ expect(await enqueue(['urgent', 'high', 'urgent'], 2)).toEqual(['0', '2']);
+ });
+
+ it('still discards low before normal, high, or urgent', async () => {
+ expect(await enqueue(['urgent', 'normal', 'low', 'high', 'urgent'], 4))
+ .toEqual(['0', '4', '3', '1']);
+ });
+
+ it('still discards normal before high or urgent', async () => {
+ expect(await enqueue(['normal', 'high', 'urgent', 'urgent'], 3))
+ .toEqual(['2', '3', '1']);
+ });
+
+ it('drops the oldest of the lowest available priority and preserves FIFO', async () => {
+ expect(await enqueue(['high', 'urgent', 'high', 'urgent', 'urgent'], 4))
+ .toEqual(['1', '3', '4', '2']);
+ });
+
+ it('continues to bound an all-urgent queue by evicting its oldest entry', async () => {
+ expect(await enqueue(['urgent', 'urgent', 'urgent'], 2)).toEqual(['1', '2']);
+ });
+});
diff --git a/v3/@claude-flow/swarm/src/message-bus.ts b/v3/@claude-flow/swarm/src/message-bus.ts
index fd2bc1fc1b..fa7ed36975 100644
--- a/v3/@claude-flow/swarm/src/message-bus.ts
+++ b/v3/@claude-flow/swarm/src/message-bus.ts
@@ -185,8 +185,8 @@ class PriorityMessageQueue {
return queue.popFront();
}
}
- // Fall back to any queue
- for (const priority of PRIORITY_ORDER) {
+ // Only high/urgent remain; preserve urgent before high on overflow too.
+ for (const priority of ['high', 'urgent'] as Priority[]) {
const queue = this.queues.get(priority)!;
if (queue.length > 0) {
this.totalCount--;