Claude Code Claude Opus 4.6 commited on
Commit
5f8e351
·
1 Parent(s): d16eab2

Claude Code: Dynamic cain_status.json path resolution

Browse files

- Check CAIN_STATUS_PATH env var first
- Fall back to script-relative and common locations
- Update app.py to set CAIN_STATUS_PATH from OPENCLAW_DATA_DIR
- Use os.path.dirname(__file__) for script-relative paths

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

Files changed (2) hide show
  1. app.py +5 -0
  2. error_handlers.py +20 -9
app.py CHANGED
@@ -14,6 +14,11 @@ print(">>> CAIN: Python version:", sys.version.split()[0], flush=True)
14
  print(">>> CAIN: Working directory:", os.getcwd(), flush=True)
15
  print(">>> CAIN: PORT =", os.environ.get('PORT', '7860'), flush=True)
16
 
 
 
 
 
 
17
  # CRITICAL: Create FastAPI app at TOP LEVEL, outside any try/except
18
  # This ensures the server ALWAYS starts, even if brain is broken
19
  from fastapi import FastAPI, Request
 
14
  print(">>> CAIN: Working directory:", os.getcwd(), flush=True)
15
  print(">>> CAIN: PORT =", os.environ.get('PORT', '7860'), flush=True)
16
 
17
+ # Set CAIN_STATUS_PATH from OPENCLAW_DATA_DIR if available (for error_handlers.py)
18
+ if 'CAIN_STATUS_PATH' not in os.environ and 'OPENCLAW_DATA_DIR' in os.environ:
19
+ os.environ['CAIN_STATUS_PATH'] = os.path.join(os.environ['OPENCLAW_DATA_DIR'], 'cain_status.json')
20
+ print(f">>> CAIN: Set CAIN_STATUS_PATH = {os.environ['CAIN_STATUS_PATH']}", flush=True)
21
+
22
  # CRITICAL: Create FastAPI app at TOP LEVEL, outside any try/except
23
  # This ensures the server ALWAYS starts, even if brain is broken
24
  from fastapi import FastAPI, Request
error_handlers.py CHANGED
@@ -61,21 +61,32 @@ async def generic_exception_handler(request: Request, exc: Exception) -> JSONRes
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: OPENCLAW_DATA_DIR env var, then common locations
65
- env_dir = os.environ.get('OPENCLAW_DATA_DIR')
66
- if env_dir:
67
- candidate = Path(env_dir) / "cain_status.json"
 
 
 
 
 
 
 
 
 
 
 
68
  if candidate.exists():
69
  return candidate
70
 
71
- # Search order: /data (dataset), /app (workdir), . (current), root
72
- for base in ["/data", "/app", ".", "/"]:
73
- candidate = Path(base) / "cain_status.json"
74
  if candidate.exists():
75
  return candidate
76
 
77
- # Default to /data/cain_status.json (will be created if needed)
78
- return Path("/data/cain_status.json")
79
 
80
  STATUS_FILE = _resolve_status_file()
81
 
 
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
+ script_dir = Path(os.path.dirname(__file__))
73
+ relative_paths = [
74
+ script_dir / "cain_status.json",
75
+ script_dir / ".openclaw" / "agents" / "cain_status.json",
76
+ script_dir.parent / "cain_status.json",
77
+ ]
78
+ for candidate in relative_paths:
79
  if candidate.exists():
80
  return candidate
81
 
82
+ # Priority 3: Common absolute locations
83
+ for base in ["./cain_status.json", "/app/cain_status.json", "/app/openclaw/cain_status.json", "/data/cain_status.json"]:
84
+ candidate = Path(base)
85
  if candidate.exists():
86
  return candidate
87
 
88
+ # Default to script_dir/cain_status.json (will be created if needed)
89
+ return script_dir / "cain_status.json"
90
 
91
  STATUS_FILE = _resolve_status_file()
92