Spaces:
Sleeping
Sleeping
Claude Code Claude Opus 4.6 commited on
Commit ·
dc05a69
1
Parent(s): d326b84
Claude Code: fix import path in error_handlers and simplify health/startup
Browse files- Fix: corrected brain_minimal import path in error_handlers.py
- Simplify: health check now always returns 200 OK without brain initialization
- Simplify: startup event removed blocking imports for faster startup
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- app.py +2 -64
- error_handlers.py +1 -1
app.py
CHANGED
|
@@ -63,23 +63,6 @@ async def startup_event():
|
|
| 63 |
logger.info(">>> STARTUP: startup_event triggered")
|
| 64 |
logger.info(f">>> STARTUP: PORT={os.environ.get('PORT', '7860')}")
|
| 65 |
logger.info(f">>> STARTUP: Working directory={os.getcwd()}")
|
| 66 |
-
logger.info(f">>> STARTUP: Python version={sys.version}")
|
| 67 |
-
logger.info(">>> STARTUP: Checking openclaw package...")
|
| 68 |
-
|
| 69 |
-
# Check if openclaw package is accessible
|
| 70 |
-
try:
|
| 71 |
-
import openclaw
|
| 72 |
-
logger.info(f">>> STARTUP: openclaw package found at: {openclaw.__file__}")
|
| 73 |
-
except ImportError as e:
|
| 74 |
-
logger.error(f">>> STARTUP: openclaw package NOT found: {e}")
|
| 75 |
-
|
| 76 |
-
# Check brain_minimal
|
| 77 |
-
try:
|
| 78 |
-
from openclaw.openclaw.agents.brain_minimal import BrainMinimal
|
| 79 |
-
logger.info(">>> STARTUP: brain_minimal module imported successfully")
|
| 80 |
-
except ImportError as e:
|
| 81 |
-
logger.warning(f">>> STARTUP: brain_minimal import failed: {e}")
|
| 82 |
-
|
| 83 |
logger.info(">>> STARTUP: Initialization complete, ready to serve requests")
|
| 84 |
print("Cain is ready")
|
| 85 |
|
|
@@ -105,59 +88,14 @@ async def root():
|
|
| 105 |
@app.get("/health")
|
| 106 |
async def health():
|
| 107 |
"""
|
| 108 |
-
Health check endpoint
|
| 109 |
-
|
| 110 |
-
Checks:
|
| 111 |
-
1. Basic API availability
|
| 112 |
-
2. Persistence files (JSON-based storage)
|
| 113 |
-
3. Brain import (if available)
|
| 114 |
|
| 115 |
-
|
| 116 |
-
Returns 503 with error details if critical failures occur.
|
| 117 |
"""
|
| 118 |
-
checks = {}
|
| 119 |
-
is_healthy = True
|
| 120 |
-
|
| 121 |
-
# 1. Check persistence files (graceful - don't crash if files don't exist)
|
| 122 |
-
try:
|
| 123 |
-
cain_status_path = "/app/openclaw/.openclaw/agents/cain_status.json"
|
| 124 |
-
if os.path.exists(cain_status_path):
|
| 125 |
-
checks["persistence"] = "ok"
|
| 126 |
-
else:
|
| 127 |
-
checks["persistence"] = "no data files yet"
|
| 128 |
-
except Exception as e:
|
| 129 |
-
checks["persistence"] = f"warning: {type(e).__name__}"
|
| 130 |
-
# Persistence issues are warnings, not critical failures
|
| 131 |
-
|
| 132 |
-
# 2. Check brain import (graceful - don't crash if import fails)
|
| 133 |
-
try:
|
| 134 |
-
from openclaw.openclaw.agents.brain_minimal import BrainMinimal
|
| 135 |
-
brain = BrainMinimal(agent_name="cain", legacy_mode=True)
|
| 136 |
-
checks["brain"] = "ok"
|
| 137 |
-
except ImportError:
|
| 138 |
-
checks["brain"] = "not available (optional)"
|
| 139 |
-
except Exception as e:
|
| 140 |
-
checks["brain"] = f"warning: {type(e).__name__}"
|
| 141 |
-
# Brain issues are warnings, not critical failures for health endpoint
|
| 142 |
-
|
| 143 |
-
# 3. Check frontend assets (graceful)
|
| 144 |
-
try:
|
| 145 |
-
index_path = "/app/static/index.html"
|
| 146 |
-
fallback_path = "/app/index.html"
|
| 147 |
-
if os.path.exists(index_path) or os.path.exists(fallback_path):
|
| 148 |
-
checks["frontend"] = "ok"
|
| 149 |
-
else:
|
| 150 |
-
checks["frontend"] = "not found"
|
| 151 |
-
except Exception as e:
|
| 152 |
-
checks["frontend"] = f"warning: {type(e).__name__}"
|
| 153 |
-
|
| 154 |
-
# Return 200 OK with status: ok for health endpoint
|
| 155 |
-
# This endpoint should never crash - it's for uptime monitoring
|
| 156 |
return JSONResponse(
|
| 157 |
status_code=http_status.HTTP_200_OK,
|
| 158 |
content={
|
| 159 |
"status": "ok",
|
| 160 |
-
"checks": checks,
|
| 161 |
"uptime_seconds": round(time.time() - START_TIME, 2),
|
| 162 |
"timestamp": time.time()
|
| 163 |
}
|
|
|
|
| 63 |
logger.info(">>> STARTUP: startup_event triggered")
|
| 64 |
logger.info(f">>> STARTUP: PORT={os.environ.get('PORT', '7860')}")
|
| 65 |
logger.info(f">>> STARTUP: Working directory={os.getcwd()}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
logger.info(">>> STARTUP: Initialization complete, ready to serve requests")
|
| 67 |
print("Cain is ready")
|
| 68 |
|
|
|
|
| 88 |
@app.get("/health")
|
| 89 |
async def health():
|
| 90 |
"""
|
| 91 |
+
Health check endpoint - always returns 200 OK.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
+
This endpoint is used for uptime monitoring and should never fail.
|
|
|
|
| 94 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
return JSONResponse(
|
| 96 |
status_code=http_status.HTTP_200_OK,
|
| 97 |
content={
|
| 98 |
"status": "ok",
|
|
|
|
| 99 |
"uptime_seconds": round(time.time() - START_TIME, 2),
|
| 100 |
"timestamp": time.time()
|
| 101 |
}
|
error_handlers.py
CHANGED
|
@@ -112,7 +112,7 @@ def handle_brain_response(message: str) -> str:
|
|
| 112 |
AttributeError: If required brain methods are missing.
|
| 113 |
"""
|
| 114 |
try:
|
| 115 |
-
from agents.brain_minimal import BrainMinimal
|
| 116 |
|
| 117 |
brain = BrainMinimal(agent_name="cain", legacy_mode=True)
|
| 118 |
result = brain._conversation_process(message)
|
|
|
|
| 112 |
AttributeError: If required brain methods are missing.
|
| 113 |
"""
|
| 114 |
try:
|
| 115 |
+
from openclaw.openclaw.agents.brain_minimal import BrainMinimal
|
| 116 |
|
| 117 |
brain = BrainMinimal(agent_name="cain", legacy_mode=True)
|
| 118 |
result = brain._conversation_process(message)
|