Skip to content

Commit c00bf70

Browse files
committed
fix: add memory leak protection for steer rate limit map
Add size-based eviction (max 1024 entries) and periodic stale sweep (every 60s) to prevent unbounded growth of the steerRateLimit Map.
1 parent 100dd79 commit c00bf70

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

‎src/agents/subagent-control.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,19 @@ const STEER_ABORT_SETTLE_TIMEOUT_MS = 5_000;
4141
const SUBAGENT_REPLY_HISTORY_LIMIT = 50;
4242

4343
const steerRateLimit = new Map<string, number>();
44+
const STEER_RATE_LIMIT_MAX = 1024;
45+
const STEER_RATE_LIMIT_SWEEP_INTERVAL_MS = 60_000;
46+
47+
function cleanSteerRateLimitStale(now: number): void {
48+
const staleThreshold = now - STEER_RATE_LIMIT_MS * 10;
49+
for (const [key, ts] of steerRateLimit) {
50+
if (ts < staleThreshold) {
51+
steerRateLimit.delete(key);
52+
}
53+
}
54+
}
55+
56+
setInterval(() => cleanSteerRateLimitStale(Date.now()), STEER_RATE_LIMIT_SWEEP_INTERVAL_MS).unref();
4457

4558
type GatewayCaller = typeof callGateway;
4659
type UpdateSessionStore = typeof updateSessionStore;
@@ -514,6 +527,11 @@ export async function steerControlledSubagentRun(params: {
514527
};
515528
}
516529
steerRateLimit.set(rateKey, now);
530+
while (steerRateLimit.size > STEER_RATE_LIMIT_MAX) {
531+
const first = steerRateLimit.keys().next();
532+
if (first.done) {break;}
533+
steerRateLimit.delete(first.value);
534+
}
517535
}
518536

519537
markSubagentRunForSteerRestart(params.entry.runId);

0 commit comments

Comments
 (0)