Spaces:
Sleeping
Sleeping
Claude Code Claude Opus 4.6 commited on
Commit ·
e3aa6b2
1
Parent(s): 4805801
Claude Code: Fix 'Error: unknown' status display
Browse files- Add cleanup of stale 'unknown' error strings on app startup
- Make /health endpoint defensive - only return error if real error exists
- Cleanup runs on module import before FastAPI starts
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
app.py
CHANGED
|
@@ -1,9 +1,11 @@
|
|
| 1 |
import os
|
| 2 |
import sys
|
|
|
|
| 3 |
import logging
|
| 4 |
import traceback
|
| 5 |
import subprocess
|
| 6 |
import signal
|
|
|
|
| 7 |
from fastapi import FastAPI, Request
|
| 8 |
from starlette.middleware.base import BaseHTTPMiddleware
|
| 9 |
from starlette.staticfiles import StaticFiles
|
|
@@ -19,6 +21,51 @@ from contextlib import asynccontextmanager
|
|
| 19 |
# Ensure logs directory exists
|
| 20 |
os.makedirs("logs", exist_ok=True)
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
# Configure logging
|
| 23 |
logging.basicConfig(
|
| 24 |
level=logging.INFO,
|
|
@@ -182,7 +229,9 @@ def health_check():
|
|
| 182 |
"timestamp": datetime.utcnow().isoformat() + "+00:00",
|
| 183 |
}
|
| 184 |
|
| 185 |
-
if error
|
|
|
|
|
|
|
| 186 |
response["error"] = error
|
| 187 |
|
| 188 |
return JSONResponse(response, status_code=200)
|
|
|
|
| 1 |
import os
|
| 2 |
import sys
|
| 3 |
+
import json
|
| 4 |
import logging
|
| 5 |
import traceback
|
| 6 |
import subprocess
|
| 7 |
import signal
|
| 8 |
+
from pathlib import Path
|
| 9 |
from fastapi import FastAPI, Request
|
| 10 |
from starlette.middleware.base import BaseHTTPMiddleware
|
| 11 |
from starlette.staticfiles import StaticFiles
|
|
|
|
| 21 |
# Ensure logs directory exists
|
| 22 |
os.makedirs("logs", exist_ok=True)
|
| 23 |
|
| 24 |
+
|
| 25 |
+
def _clean_stale_unknown_errors():
|
| 26 |
+
"""
|
| 27 |
+
CRITICAL FIX: Clean stale 'unknown' error strings from ALL possible cain_status.json locations.
|
| 28 |
+
This prevents the 'Error: unknown' display issue where error field contains literal 'unknown' string.
|
| 29 |
+
Must run BEFORE any health checks can hit /health endpoint.
|
| 30 |
+
"""
|
| 31 |
+
logger = logging.getLogger(__name__)
|
| 32 |
+
cleaned_count = 0
|
| 33 |
+
|
| 34 |
+
# All possible locations where cain_status.json might exist
|
| 35 |
+
_all_possible_status_paths = [
|
| 36 |
+
Path("/data/cain_status.json"), # Primary (OPENCLAW_DATA_DIR)
|
| 37 |
+
Path("/app/openclaw/.openclaw/agents/cain_status.json"), # Nested structure
|
| 38 |
+
Path("/app/.openclaw/agents/cain_status.json"), # Legacy flat structure
|
| 39 |
+
Path("/app/cain_status.json"), # App root
|
| 40 |
+
Path("/app/data/cain_status.json"), # App data subdirectory
|
| 41 |
+
Path("/app/memory/cain_status.json"), # Memory directory
|
| 42 |
+
Path("/data/memory/cain_status.json"), # Data memory directory
|
| 43 |
+
]
|
| 44 |
+
|
| 45 |
+
for sp in _all_possible_status_paths:
|
| 46 |
+
try:
|
| 47 |
+
if sp.exists():
|
| 48 |
+
with open(sp, 'r') as f:
|
| 49 |
+
data = json.load(f)
|
| 50 |
+
error = data.get('error')
|
| 51 |
+
# Clean if error is a "null" string (unknown, none, null, or empty)
|
| 52 |
+
if isinstance(error, str) and error.strip().lower() in ('unknown', 'none', 'null', ''):
|
| 53 |
+
data['error'] = None
|
| 54 |
+
data['_cleaned_at'] = 'app_startup_cleanup'
|
| 55 |
+
with open(sp, 'w') as f:
|
| 56 |
+
json.dump(data, f, indent=2)
|
| 57 |
+
cleaned_count += 1
|
| 58 |
+
logger.info(f"[CLEANUP] Cleaned stale '{error}' from {sp}")
|
| 59 |
+
except Exception as e:
|
| 60 |
+
logger.debug(f"[CLEANUP] Could not clean {sp}: {e}")
|
| 61 |
+
|
| 62 |
+
if cleaned_count > 0:
|
| 63 |
+
logger.info(f"[CLEANUP] Cleaned {cleaned_count} status file(s) with stale 'unknown' errors")
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
# Run cleanup IMMEDIATELY on module import (before FastAPI starts)
|
| 67 |
+
_clean_stale_unknown_errors()
|
| 68 |
+
|
| 69 |
# Configure logging
|
| 70 |
logging.basicConfig(
|
| 71 |
level=logging.INFO,
|
|
|
|
| 229 |
"timestamp": datetime.utcnow().isoformat() + "+00:00",
|
| 230 |
}
|
| 231 |
|
| 232 |
+
# CRITICAL: Only include error field if there's a REAL error (non-empty string)
|
| 233 |
+
# This prevents returning "error": "unknown" or similar stale values
|
| 234 |
+
if error and isinstance(error, str) and error.strip() and error.strip().lower() not in ("unknown", "none", "null"):
|
| 235 |
response["error"] = error
|
| 236 |
|
| 237 |
return JSONResponse(response, status_code=200)
|