fix(AUDIT-M2): prune rate-limit timestamp arrays in record* methods#285
fix(AUDIT-M2): prune rate-limit timestamp arrays in record* methods#285xlabtg merged 3 commits intoxlabtg:mainfrom
Conversation
Adding .gitkeep for PR creation (default mode). This file will be removed when the task is complete. Issue: xlabtg#284
recordToolCall() and recordApiCall() previously pushed timestamps without bounds, growing the arrays indefinitely between checkAction() calls. Fix by filtering out expired entries and slicing to the rate-limit cap on every record call, keeping memory proportional to the configured limit regardless of how many calls occur before the next policy check. Adds two unit tests verifying that 10 000 consecutive calls without a checkAction() leave the arrays no larger than the configured rate-limit cap. Closes xlabtg#284
Solution summaryDone. Here's a summary of what was done: Root cause: Fix (policy-engine.ts:234-251): Each record method now:
This guarantees the arrays stay bounded at all times, not just at check time. Tests added: Two new cases in PR: #285 This summary was automatically extracted from the AI working session output. |
🤖 Solution Draft LogThis log file contains the complete execution trace of the AI solution draft process. 💰 Cost: $0.618400📊 Context and tokens usage:
Total: (38.6K + 1.2M cached) input tokens, 6.7K output tokens, $0.618400 cost 🤖 Models used:
📎 Log file uploaded as Gist (952KB)Now working session is ended, feel free to review and add any feedback on the solution draft. |
🔄 Auto-restart triggered (iteration 1)Reason: Merge conflicts detected Starting new session to address the issues. Auto-restart-until-mergeable mode is active. Will continue until PR becomes mergeable. |
🔄 Auto-restart-until-mergeable Log (iteration 1)This log file contains the complete execution trace of the AI solution draft process. 💰 Cost: $0.193295📊 Context and tokens usage:
Total: (13.5K + 362.2K cached) input tokens, 2.3K output tokens, $0.193295 cost 🤖 Models used:
📎 Log file uploaded as Gist (1370KB)Now working session is ended, feel free to review and add any feedback on the solution draft. |
Summary
Fixes AUDIT-M2:
recordToolCall()andrecordApiCall()inPolicyEnginepushed timestamps into arrays without ever pruning them betweencheckAction()calls, causing unbounded memory growth proportional to call count.Root cause: Timestamp pruning only happened inside
checkAction(). Any code path that calledrecordToolCall()/recordApiCall()without a subsequentcheckAction()would accumulate entries forever.Fix: Both record methods now prune expired entries immediately on every call using the same window as
checkAction()(3 600 000 msfor tool calls,60 000 msfor API calls), and additionally cap the array to the configured rate-limit maximum via.slice(). This keeps memory proportional to the configured cap regardless of call frequency.Changes
src/autonomous/policy-engine.ts— addfilter+sliceinrecordToolCallandrecordApiCallsrc/autonomous/__tests__/policy-engine.test.ts— add two regression tests per acceptance criteria in the issueHow to reproduce the issue
Test plan
toolCallTimestamps stays bounded after many recordToolCall calls without checkAction— asserts array length ≤toolCallsPerHour(100) after 10 000 callsapiCallTimestamps stays bounded after many recordApiCall calls without checkAction— asserts array length ≤apiCallsPerMinute(30) after 10 000 callsFixes #284