Claude Code Claude Opus 4.6 commited on
Commit
446b8d3
·
1 Parent(s): 575f47c

Claude Code: Fix path resolution in error_handlers.py - remove hardcoded /app paths

Browse files

- Replace hardcoded /app/* paths with dynamic directory resolution
- Add _get_base_dir() function to determine runtime directory dynamically
- Respect OPENCLAW_DATA_DIR environment variable for Docker environment
- Simplify _resolve_status_file() to trust CAIN_STATUS_PATH env var

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

Files changed (1) hide show
  1. error_handlers.py +30 -17
error_handlers.py CHANGED
@@ -58,37 +58,50 @@ async def generic_exception_handler(request: Request, exc: Exception) -> JSONRes
58
 
59
 
60
  # Configuration path - cain_status.json location
61
- # Dynamically search common locations for cain_status.json
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
  def _resolve_status_file() -> Path:
63
  """Find cain_status.json by searching common locations."""
64
  # Priority 1: CAIN_STATUS_PATH env var (direct path or set by app.py)
 
65
  env_path = os.environ.get('CAIN_STATUS_PATH')
66
  if env_path:
67
- candidate = Path(env_path)
68
- if candidate.exists() or candidate.parent.exists():
69
- return candidate
70
 
71
- # Priority 2: Search relative to this script's location
72
- # Docker WORKDIR is /app, so script_dir will be /app
 
 
 
 
 
73
  script_dir = Path(os.path.dirname(__file__))
74
  relative_paths = [
75
- script_dir / "cain_status.json", # /app/cain_status.json
76
- script_dir / ".openclaw" / "cain_status.json", # /app/.openclaw/cain_status.json
77
- script_dir / ".openclaw" / "agents" / "cain_status.json", # /app/.openclaw/agents/cain_status.json
78
  script_dir.parent / "cain_status.json",
79
  ]
80
  for candidate in relative_paths:
81
  if candidate.exists():
82
  return candidate
83
 
84
- # Priority 3: Common absolute locations
85
- for base in ["./cain_status.json", "/app/cain_status.json", "/app/.openclaw/cain_status.json", "/data/cain_status.json"]:
86
- candidate = Path(base)
87
- if candidate.exists():
88
- return candidate
89
-
90
- # Default to script_dir/cain_status.json (will be created if needed)
91
- return script_dir / "cain_status.json"
92
 
93
  STATUS_FILE = _resolve_status_file()
94
 
 
58
 
59
 
60
  # Configuration path - cain_status.json location
61
+ def _get_base_dir() -> Path:
62
+ """Determine the correct base directory dynamically."""
63
+ # Priority 1: OPENCLAW_DATA_DIR (set by Docker environment)
64
+ data_dir = os.environ.get('OPENCLAW_DATA_DIR')
65
+ if data_dir:
66
+ return Path(data_dir)
67
+
68
+ # Priority 2: /data (standard Docker volume mount)
69
+ data_path = Path("/data")
70
+ if data_path.exists():
71
+ return data_path
72
+
73
+ # Priority 3: Use this script's directory as fallback
74
+ return Path(os.path.dirname(__file__))
75
+
76
+
77
  def _resolve_status_file() -> Path:
78
  """Find cain_status.json by searching common locations."""
79
  # Priority 1: CAIN_STATUS_PATH env var (direct path or set by app.py)
80
+ # Trust this completely if set - don't second-guess it
81
  env_path = os.environ.get('CAIN_STATUS_PATH')
82
  if env_path:
83
+ return Path(env_path)
 
 
84
 
85
+ # Priority 2: Use OPENCLAW_DATA_DIR (set in Docker)
86
+ base_dir = _get_base_dir()
87
+ candidate = base_dir / "cain_status.json"
88
+ if candidate.parent.exists():
89
+ return candidate
90
+
91
+ # Priority 3: Search relative to this script's location
92
  script_dir = Path(os.path.dirname(__file__))
93
  relative_paths = [
94
+ script_dir / "cain_status.json", # Script directory
95
+ script_dir / ".openclaw" / "cain_status.json", # .openclaw subdir
96
+ script_dir / ".openclaw" / "agents" / "cain_status.json", # agents subdir
97
  script_dir.parent / "cain_status.json",
98
  ]
99
  for candidate in relative_paths:
100
  if candidate.exists():
101
  return candidate
102
 
103
+ # Default: use base_dir/cain_status.json (will be created if needed)
104
+ return _get_base_dir() / "cain_status.json"
 
 
 
 
 
 
105
 
106
  STATUS_FILE = _resolve_status_file()
107