fix: use HYPERCODE_REDIS_URL env var in MultiTierCache default (#196)#196
fix: use HYPERCODE_REDIS_URL env var in MultiTierCache default (#196)#196welshDog wants to merge 1 commit into
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