Spaces:
Sleeping
Sleeping
Claude Code Claude Code commited on
Commit ·
420f732
1
Parent(s): 8756346
Claude Code: Add defensive .env initialization to brain_minimal.py
Browse files- Add try/except block for .env file initialization at startup
- Fallback to copying .env.example if .env is missing
- Create empty .env as last resort to prevent crash
- Add warning log when fallback mode is active
- Prevents FileNotFoundError on subprocess startup
Co-Authored-By: Claude Code <noreply@anthropic.com>
8 unmodified lines910111213146 unmodified lines2122232425262 unmodified lines2930313233348 unmodified lines- Graceful shutdown on SIGTERM- Heartbeat file for health monitoring- Persistent polling loop"""import osimport sys6 unmodified linesfrom datetime import datetimefrom threading import Thread, Event# Configure logginglogging.basicConfig(level=logging.INFO,2 unmodified lines)logger = logging.getLogger(__name__)# Global shutdown flag_shutdown_event = Event()8 unmodified lines91011121314156 unmodified lines222324252627282930313233343536373839404142434445464748495051525354555657585960612 unmodified lines646566676869707172738 unmodified lines- Graceful shutdown on SIGTERM- Heartbeat file for health monitoring- Persistent polling loop- Defensive .env initialization to prevent startup crashes"""import osimport sys6 unmodified linesfrom datetime import datetimefrom threading import Thread, Event# CRITICAL: Defensive .env initialization before any other imports# This prevents FileNotFoundError if brain_minimal.py runs before entrypoint.sh_fallback_mode = Falsetry:# Ensure /app is in path for utils importif "/app" not in sys.path:sys.path.insert(0, "/app")# Try to import and run env_inittry:from utils.env_init import init_envinit_env()except ImportError:# If utils.env_init is not available, create .env directlyenv_file = Path("/app/.env")if not env_file.exists():env_example = Path("/app/.env.example")if env_example.exists():import shutilshutil.copy(env_example, env_file)logging.basicConfig(level=logging.INFO)logging.warning("[BRAIN_MINIMAL] .env not found - copied from .env.example (fallback mode)")_fallback_mode = Trueelse:env_file.touch()logging.basicConfig(level=logging.INFO)logging.warning("[BRAIN_MINIMAL] .env.example not found - created empty .env (fallback mode)")_fallback_mode = Trueexcept Exception as e:# Don't crash on .env init errors - log warning and continuelogging.basicConfig(level=logging.INFO)logging.warning(f"[BRAIN_MINIMAL] .env initialization failed (using safe defaults): {e}")_fallback_mode = True# Configure logginglogging.basicConfig(level=logging.INFO,2 unmodified lines)logger = logging.getLogger(__name__)# Log if fallback mode is activeif _fallback_mode:logger.warning("[BRAIN_MINIMAL] FALLBACK MODE ACTIVE - .env handling may be incomplete")# Global shutdown flag_shutdown_event = Event()
brain_minimal.py+39