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(shared): align rate admission and retry reset times

Rudy Celeklicca0ad8da673
diff --git a/tests/shared-rate-window-retry.test.mjs b/tests/shared-rate-window-retry.test.mjs
new file mode 100644
index 0000000000..20ed7ec28b
--- /dev/null
+++ b/tests/shared-rate-window-retry.test.mjs
@@ -0,0 +1,11 @@
+import test from 'node:test';
+import assert from 'node:assert/strict';
+import { SlidingWindowRateLimiter, TokenBucketRateLimiter } from '../v3/@claude-flow/shared/src/resilience/rate-limiter.ts';
+test('sliding-window admission expires a request at its advertised reset time', () => {
+ const original = Date.now; let now = 1000; Date.now = () => now; const l = new SlidingWindowRateLimiter({ maxRequests: 1, windowMs: 10000 });
+ try { assert.equal(l.consume().allowed, true); now = 11000; assert.equal(l.status().allowed, true); assert.equal(l.consume().allowed, true); } finally { l.destroy(); Date.now = original; }
+});
+test('token-bucket retry delay counts only the remaining refill time', () => {
+ const original = Date.now; let now = 1000; Date.now = () => now; const l = new TokenBucketRateLimiter({ maxRequests: 1, windowMs: 10000 });
+ try { l.consume(); now = 10000; for (const r of [l.check(), l.consume()]) { assert.equal(r.allowed, false); assert.equal(r.retryAfter, 1000); assert.equal(r.resetAt.getTime(), now + r.retryAfter); } now = 11000; assert.equal(l.consume().allowed, true); } finally { l.destroy(); Date.now = original; }
+});
diff --git a/v3/@claude-flow/shared/src/resilience/rate-limiter.ts b/v3/@claude-flow/shared/src/resilience/rate-limiter.ts
index 82996e0c50..2da7aa4295 100644
--- a/v3/@claude-flow/shared/src/resilience/rate-limiter.ts
+++ b/v3/@claude-flow/shared/src/resilience/rate-limiter.ts
@@ -194,7 +194,7 @@ export class SlidingWindowRateLimiter implements RateLimiter {
if (!entries) return;
const cutoff = Date.now() - this.options.windowMs;
- const filtered = entries.filter((e) => e.timestamp >= cutoff);
+ const filtered = entries.filter((e) => e.timestamp > cutoff);
if (filtered.length === 0) {
this.requests.delete(key);
@@ -274,7 +274,7 @@ export class TokenBucketRateLimiter implements RateLimiter {
allowed: bucket.tokens >= 1,
remaining: Math.floor(bucket.tokens),
resetAt: new Date(bucket.lastRefill + this.options.windowMs),
- retryAfter: bucket.tokens >= 1 ? 0 : this.options.windowMs,
+ retryAfter: bucket.tokens >= 1 ? 0 : Math.max(0, bucket.lastRefill + this.options.windowMs - Date.now()),
total: this.options.maxRequests,
used: this.options.maxRequests - Math.floor(bucket.tokens),
};
@@ -292,7 +292,7 @@ export class TokenBucketRateLimiter implements RateLimiter {
allowed: false,
remaining: 0,
resetAt: new Date(bucket.lastRefill + this.options.windowMs),
- retryAfter: this.options.windowMs,
+ retryAfter: Math.max(0, bucket.lastRefill + this.options.windowMs - Date.now()),
total: this.options.maxRequests,
used: this.options.maxRequests,
};