Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions backend/app/cache/multi_tier.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import inspect
import json
import logging
import os
from typing import Any, Optional, Callable
from functools import wraps
from cachetools import TTLCache
Expand Down Expand Up @@ -32,17 +33,18 @@ def __init__(
self,
local_size: int = 1000,
local_ttl: int = 300,
redis_url: str = "redis://redis:6379/1",
redis_url: str | None = None,
):
"""Initialize multi-tier cache.

Args:
local_size: Max items in local cache
local_ttl: TTL for local cache items (seconds)
redis_url: Redis connection URL
redis_url: Redis connection URL. Defaults to HYPERCODE_REDIS_URL
env var, falling back to redis://redis:6379/1.
"""
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")
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

🧩 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)
PY

Repository: welshDog/HyperCode-V2.4

Length of output: 81


🏁 Script executed:

git ls-files backend/app/cache/multi_tier.py

Repository: welshDog/HyperCode-V2.4

Length of output: 99


🏁 Script executed:

cat -n backend/app/cache/multi_tier.py | head -60 | tail -25

Repository: welshDog/HyperCode-V2.4

Length of output: 1116


🏁 Script executed:

rg "HYPERCODE_REDIS_URL|redis_url" -A 2 -B 2

Repository: 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.

Suggested change
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.

self.redis_client = None
self.stats = {
"hits": 0,
Expand Down Expand Up @@ -219,7 +221,7 @@ async def get_cache() -> MultiTierCache:
global _cache_instance

if _cache_instance is None:
_cache_instance = MultiTierCache()
_cache_instance = MultiTierCache() # picks up HYPERCODE_REDIS_URL via __init__
await _cache_instance.connect()

return _cache_instance
Expand Down
Loading