Repository navigation
fix(go-policy): check rate-limit threshold before incrementing counter - #2021
Closed
Ken Tannenbaum (finnoybu) wants to merge 1 commit into
Closed
Ken Tannenbaum (finnoybu) wants to merge 1 commit into
Ken Tannenbaum (finnoybu) wants to merge 1 commit into
Conversation
🤖 AI Agent: code-reviewer — View detailsTL;DR: 1 blocker, 0 warnings. The counter increment logic in
Action items: Reorder the counter increment logic in Warnings: No warnings found. |
🤖 AI Agent: docs-sync-checker — Docs SyncDocs Sync
|
🤖 AI Agent: test-generator — `policy.go`
|
🤖 AI Agent: security-scanner — View detailsNo security issues found. |
🤖 AI Agent: breaking-change-detector — API CompatibilityAPI Compatibility
|
PR Review Summary
Verdict: ❌ Changes needed |
|
🟡 Contributor Check: MEDIUM
Automated check by AGT Contributor Check. |
2 tasks
Imran Siddique (imran-siddique)
previously approved these changes
May 11, 2026
Imran Siddique (imran-siddique)
left a comment
Collaborator
There was a problem hiding this comment.
Reviewed. Security fix from finnoybu audit.
Collaborator
|
Merge conflict after sibling PRs landed. Please rebase on main and force-push to resolve. |
checkRateLimit incremented `state.count++` and then compared the new value against rule.MaxCalls. Once the limit was reached, subsequent calls within the same window continued to increment the counter on their way to the RateLimit decision, growing it unboundedly for the remainder of the window. The growth was internally harmless (the counter is reset when the window expires), but it meant the counter no longer reflected the admit/reject state — a test or observability hook reading state.count after a deny would see values larger than MaxCalls. Reorder: compare BEFORE incrementing. Use `>=` so MaxCalls itself is the inclusive bound (3 allowed → counter reaches 3 → next call gets RateLimit without increment). Adds TestRateLimitCounterDoesNotGrowAfterDeny: log 3 allowed calls, 3 denied calls, assert counter stays at 3. Note: This is part 1 of REVIEW.md item #7. Part 2 — keying the rate limiter by (action, agent_id, tenant) so one noisy caller doesn't starve others — ships as a separate PR because it requires threading context through to checkRateLimit and updating callers.
Collaborator
|
Superseded by #2232 which applies this fix (check-before-increment). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
checkRateLimitinagent-governance-golang/packages/agentmesh/policy.go:153-156incremented the counter first and then compared:Once the limit was reached, every subsequent call within the same window continued to increment the counter on its way to returning
RateLimit. The counter grew unboundedly until the window expired.The growth was internally harmless — the next window reset to 1 — but the counter no longer represented the admit/reject state, so any test or observability hook reading
state.countafter a deny would see values larger thanMaxCalls.Change
Reorder to check-then-increment, using
>=soMaxCallsis the inclusive bound:A 3-call limit now goes: counter 0→1→2→3 across three allowed calls; the 4th call sees
count >= 3, returnsRateLimit, leavescountat 3.Tests
New
TestRateLimitCounterDoesNotGrowAfterDeny: log 3 allowed calls then 3 denied calls; assertstate.count == 3after each phase. The existingTestRateLimitingcontinues to pass — its semantics ("admit N, then deny") are unchanged.Note on REVIEW.md item #7
This is part 1 of REVIEW.md item #7. Part 2 — keying the rate limiter by
(action, agent_id, tenant)so one noisy caller doesn't starve all others — ships as a separate PR because it requires threadingcontextthrough tocheckRateLimitand updating the in-tree caller (middleware.go:256currently passesnilcontext).Test plan
Surfaced as part of the AEGIS Initiative review of the agent-governance-toolkit (HIGH severity, Go section). Filed by Ken Tannenbaum (AEGIS Initiative).