fix: use HYPERCODE_REDIS_URL env var in MultiTierCache default (#196)#196
fix: use HYPERCODE_REDIS_URL env var in MultiTierCache default (#196)#196
Conversation
The __init__ default redis_url was hardcoded to redis://redis:6379/1
with no auth, causing NOAUTH Authentication required on Railway where
Redis requires a password.
Fix: default to None and resolve via os.getenv("HYPERCODE_REDIS_URL")
with the old URL as last-resort fallback — same pattern as main.py.
Also fixes get_cache() call site to pass the env var explicitly.
📝 WalkthroughWalkthroughThe Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 OpenGrep (1.20.0)backend/app/cache/multi_tier.py┌──────────────┐ �[32m✔�[39m �[1mOpengrep OSS�[0m �[1m Loading rules from local config...�[0m 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. Review rate limit: 0/1 reviews remaining, refill in 60 minutes.Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@backend/app/cache/multi_tier.py`:
- Line 47: self.redis_url is set using os.getenv which returns an empty string
if HYPERCODE_REDIS_URL is present but empty, causing silent fallback to local
cache; change the assignment in the constructor to treat empty/whitespace values
as unset by checking truthiness/stripped content. Concretely, compute candidate
= redis_url if redis_url and redis_url.strip() else
os.getenv("HYPERCODE_REDIS_URL") and then set self.redis_url = candidate.strip()
if candidate and candidate.strip() else "redis://redis:6379/1" so that both an
empty env var and an empty argument correctly fall back to the default.
🪄 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: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: bc326467-7326-4219-9371-282781a6e424
📒 Files selected for processing (1)
backend/app/cache/multi_tier.py
| """ | ||
| self.local_cache = TTLCache(maxsize=local_size, ttl=local_ttl) | ||
| self.redis_url = redis_url | ||
| self.redis_url = redis_url or os.getenv("HYPERCODE_REDIS_URL", "redis://redis:6379/1") |
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
# Verify current expression behaviour when env var is empty.
python - <<'PY'
import os
os.environ["HYPERCODE_REDIS_URL"] = ""
redis_url = None
resolved = redis_url or os.getenv("HYPERCODE_REDIS_URL", "redis://redis:6379/1")
print("resolved =", repr(resolved))
# Expected with current code: '' (shows fallback is skipped)
PYRepository: welshDog/HyperCode-V2.4
Length of output: 81
🏁 Script executed:
git ls-files backend/app/cache/multi_tier.pyRepository: welshDog/HyperCode-V2.4
Length of output: 99
🏁 Script executed:
cat -n backend/app/cache/multi_tier.py | head -60 | tail -25Repository: welshDog/HyperCode-V2.4
Length of output: 1116
🏁 Script executed:
rg "HYPERCODE_REDIS_URL|redis_url" -A 2 -B 2Repository: welshDog/HyperCode-V2.4
Length of output: 50381
Empty HYPERCODE_REDIS_URL bypasses fallback to default Redis URL.
When the environment variable is present but empty or whitespace-only, os.getenv() returns the empty value before the default is ever evaluated. This causes self.redis_url to be set to an empty string instead of falling back to redis://redis:6379/1, silently disabling Redis connectivity and reverting to local-cache-only mode.
Suggested fix
- self.redis_url = redis_url or os.getenv("HYPERCODE_REDIS_URL", "redis://redis:6379/1")
+ configured_url = (redis_url or os.getenv("HYPERCODE_REDIS_URL") or "").strip()
+ self.redis_url = configured_url or "redis://redis:6379/1"📝 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.
| self.redis_url = redis_url or os.getenv("HYPERCODE_REDIS_URL", "redis://redis:6379/1") | |
| configured_url = (redis_url or os.getenv("HYPERCODE_REDIS_URL") or "").strip() | |
| self.redis_url = configured_url or "redis://redis:6379/1" |
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@backend/app/cache/multi_tier.py` at line 47, self.redis_url is set using
os.getenv which returns an empty string if HYPERCODE_REDIS_URL is present but
empty, causing silent fallback to local cache; change the assignment in the
constructor to treat empty/whitespace values as unset by checking
truthiness/stripped content. Concretely, compute candidate = redis_url if
redis_url and redis_url.strip() else os.getenv("HYPERCODE_REDIS_URL") and then
set self.redis_url = candidate.strip() if candidate and candidate.strip() else
"redis://redis:6379/1" so that both an empty env var and an empty argument
correctly fall back to the default.
🐛 Problem
MultiTierCache.__init__had a hardcoded default:On Railway, Redis requires a password (from
HYPERCODE_REDIS_URL). The hardcoded URL had no password → server returnedNOAUTH Authentication required→ app fell back to local-cache-only mode.Meanwhile
main.py(metrics Redis client) was already usingsettings.HYPERCODE_REDIS_URLcorrectly — so you had two Redis clients using two different configs.✅ Fix
Changed
redis_urldefault toNoneand resolve via env var — same pattern as the working client:📋 Impact
Redis connection failed: Authentication requiredwarning on Railwayredis://redis:6379/1still works as a last-resort local fallback🧪 Expected log change after deploy
Before:
⚠️ Redis connection failed: Authentication required.. Using local cache only.After:
✅ Redis cache connectedSummary by CodeRabbit