Claude Code Claude Opus 4.6 commited on
Commit
4fad1b7
·
1 Parent(s): b916a21

Claude Code: Fix health check - use dynamic path for status file, return 'idle' instead of 'unknown'

Browse files

- Change hardcoded /app path to dynamic based on working directory
- Return 'idle' (healthy) instead of 'unknown' (treated as error) when status file not found
- Add status_file path to response for debugging

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

Files changed (1) hide show
  1. error_handlers.py +13 -11
error_handlers.py CHANGED
@@ -56,9 +56,10 @@ async def generic_exception_handler(request: Request, exc: Exception) -> JSONRes
56
  )
57
 
58
 
59
- # Configuration paths
60
- OPENCLAW_DIR = "/app/openclaw/.openclaw"
61
- STATUS_FILE = f"{OPENCLAW_DIR}/agents/cain_status.json"
 
62
 
63
 
64
  def handle_status_file_read() -> Dict[str, Any]:
@@ -67,33 +68,34 @@ def handle_status_file_read() -> Dict[str, Any]:
67
 
68
  Returns:
69
  Status dictionary with current_state, last_updated, and agent fields.
70
-
71
- Raises:
72
- PermissionError: If file exists but cannot be read due to permissions.
73
- json.JSONDecodeError: If file contains invalid JSON.
74
  """
75
  try:
76
  with open(STATUS_FILE, "r") as f:
77
  return json.load(f)
78
  except FileNotFoundError:
 
79
  return {
80
- "current_state": "unknown",
81
  "last_updated": datetime.utcnow().isoformat() + "+00:00",
82
- "agent": "cain"
 
 
83
  }
84
  except PermissionError as e:
85
  return {
86
  "current_state": "error",
87
  "last_updated": datetime.utcnow().isoformat() + "+00:00",
88
  "agent": "cain",
89
- "error": f"Permission denied reading status file: {str(e)}"
 
90
  }
91
  except json.JSONDecodeError as e:
92
  return {
93
  "current_state": "error",
94
  "last_updated": datetime.utcnow().isoformat() + "+00:00",
95
  "agent": "cain",
96
- "error": f"Invalid JSON in status file: {str(e)}"
 
97
  }
98
 
99
 
 
56
  )
57
 
58
 
59
+ # Configuration paths - use dynamic path based on working directory
60
+ _WORKING_DIR = os.environ.get("OPENCLAW_BASE_DIR", os.getcwd())
61
+ _OPENCLAW_RELATIVE = os.environ.get("OPENCLAW_SUBDIR", "openclaw/.openclaw")
62
+ STATUS_FILE = os.path.join(_WORKING_DIR, _OPENCLAW_RELATIVE, "agents", "cain_status.json")
63
 
64
 
65
  def handle_status_file_read() -> Dict[str, Any]:
 
68
 
69
  Returns:
70
  Status dictionary with current_state, last_updated, and agent fields.
 
 
 
 
71
  """
72
  try:
73
  with open(STATUS_FILE, "r") as f:
74
  return json.load(f)
75
  except FileNotFoundError:
76
+ # Status file not found is not an error - return healthy default
77
  return {
78
+ "current_state": "idle",
79
  "last_updated": datetime.utcnow().isoformat() + "+00:00",
80
+ "agent": "cain",
81
+ "status_file": STATUS_FILE,
82
+ "note": "Status file not found - using default"
83
  }
84
  except PermissionError as e:
85
  return {
86
  "current_state": "error",
87
  "last_updated": datetime.utcnow().isoformat() + "+00:00",
88
  "agent": "cain",
89
+ "error": f"Permission denied reading status file: {str(e)}",
90
+ "status_file": STATUS_FILE
91
  }
92
  except json.JSONDecodeError as e:
93
  return {
94
  "current_state": "error",
95
  "last_updated": datetime.utcnow().isoformat() + "+00:00",
96
  "agent": "cain",
97
+ "error": f"Invalid JSON in status file: {str(e)}",
98
+ "status_file": STATUS_FILE
99
  }
100
 
101