Repository navigation
refactor(server): move rate limiter - #383
Conversation
📝 WalkthroughWalkthroughRate-limiting logic is consolidated into a canonical server module that manages Redis-backed or in-memory storage selection, creates a shared slowapi Limiter instance, and defines endpoint rate-limit constants. The UI module becomes a re-export shim for backward compatibility, the HTTP server imports from the server module, and tests validate module equivalence and functionality. ChangesRate-Limit Module Consolidation
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@coding-agent/src/coding_agent/server/rate_limit.py`:
- Around line 17-20: The code treats whitespace-only AGENT_SESSION_REDIS_URL as
a valid backend; fix by normalizing the env var with .strip() before the truthy
check so blank/padded values fall back to memory. Update the assignment of
redis_url (in coding_agent.server.rate_limit.py) to strip whitespace (e.g.,
redis_url = os.environ.get("AGENT_SESSION_REDIS_URL", "").strip()) and then use
the existing if redis_url: return redis_url else return "memory://".
- Around line 32-35: The warning currently logs the raw storage_uri which can
contain credentials; update the logger.warning call in rate_limit.py to never
emit raw URIs by redacting credentials before logging (e.g., parse storage_uri
in the same scope as the existing logger.warning and reconstruct a masked_uri
that removes or replaces username/password, or log only scheme/host), then pass
masked_uri (not storage_uri) into the logger.warning message where
logger.warning("Rate limiter storage unavailable at %s; ...", storage_uri, exc)
is called; ensure the redaction handles both URLs with userinfo and edge cases
so secrets cannot be logged.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: 17e3bfc3-21f8-4010-8ae0-426242f22a3c
📒 Files selected for processing (5)
coding-agent/src/coding_agent/server/http_server.pycoding-agent/src/coding_agent/server/rate_limit.pycoding-agent/src/coding_agent/ui/rate_limit.pycoding-agent/tests/ui/test_http_server.pycoding-agent/tests/ui/test_rate_limit.py
| redis_url = os.environ.get("AGENT_SESSION_REDIS_URL", "") | ||
| if redis_url: | ||
| return redis_url | ||
| return "memory://" |
There was a problem hiding this comment.
Trim AGENT_SESSION_REDIS_URL before backend selection.
Whitespace-only or padded values are currently treated as configured Redis URIs and can trigger avoidable fallback behavior. Normalize with .strip() before the truthy check.
💡 Suggested patch
def _get_storage_uri() -> str:
- redis_url = os.environ.get("AGENT_SESSION_REDIS_URL", "")
+ redis_url = os.environ.get("AGENT_SESSION_REDIS_URL", "").strip()
if redis_url:
return redis_url
return "memory://"📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| redis_url = os.environ.get("AGENT_SESSION_REDIS_URL", "") | |
| if redis_url: | |
| return redis_url | |
| return "memory://" | |
| redis_url = os.environ.get("AGENT_SESSION_REDIS_URL", "").strip() | |
| if redis_url: | |
| return redis_url | |
| return "memory://" |
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@coding-agent/src/coding_agent/server/rate_limit.py` around lines 17 - 20, The
code treats whitespace-only AGENT_SESSION_REDIS_URL as a valid backend; fix by
normalizing the env var with .strip() before the truthy check so blank/padded
values fall back to memory. Update the assignment of redis_url (in
coding_agent.server.rate_limit.py) to strip whitespace (e.g., redis_url =
os.environ.get("AGENT_SESSION_REDIS_URL", "").strip()) and then use the existing
if redis_url: return redis_url else return "memory://".
| logger.warning( | ||
| "Rate limiter storage unavailable at %s; falling back to memory: %s", | ||
| storage_uri, | ||
| exc, |
There was a problem hiding this comment.
Avoid logging raw storage URIs (credentials can leak).
If AGENT_SESSION_REDIS_URL contains credentials, the warning currently writes secrets to logs.
🔐 Suggested patch
logger.warning(
- "Rate limiter storage unavailable at %s; falling back to memory: %s",
- storage_uri,
+ "Rate limiter storage unavailable; falling back to memory: %s",
exc,
)🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@coding-agent/src/coding_agent/server/rate_limit.py` around lines 32 - 35, The
warning currently logs the raw storage_uri which can contain credentials; update
the logger.warning call in rate_limit.py to never emit raw URIs by redacting
credentials before logging (e.g., parse storage_uri in the same scope as the
existing logger.warning and reconstruct a masked_uri that removes or replaces
username/password, or log only scheme/host), then pass masked_uri (not
storage_uri) into the logger.warning message where logger.warning("Rate limiter
storage unavailable at %s; ...", storage_uri, exc) is called; ensure the
redaction handles both URLs with userinfo and edge cases so secrets cannot be
logged.
Summary
Verification
Summary by CodeRabbit
New Features
Chores