Claude Code Claude Opus 4.6 commited on
Commit
23b9b9d
·
1 Parent(s): 1e0d510

Improve load_cain_status() to auto-recreate status file

Browse files

- Call ensure_cain_status_file() when file is missing
- Handle JSON decode errors by recreating the file
- Add proper error logging for all failure cases

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

Files changed (1) hide show
  1. app.py +17 -6
app.py CHANGED
@@ -347,13 +347,24 @@ def load_cain_status() -> Dict[str, Any]:
347
  with open(CAIN_STATUS_FILE, 'r') as f:
348
  return json.load(f)
349
  else:
350
- return {
351
- "current_state": "unknown",
352
- "last_updated": datetime.utcnow().isoformat() + "Z",
353
- "agent": "cain",
354
- "error": "Status file not found"
355
- }
 
 
 
 
 
 
 
356
  except Exception as e:
 
 
 
 
357
  return {
358
  "current_state": "error",
359
  "last_updated": datetime.utcnow().isoformat() + "Z",
 
347
  with open(CAIN_STATUS_FILE, 'r') as f:
348
  return json.load(f)
349
  else:
350
+ # Ensure status file exists before returning
351
+ status = ensure_cain_status_file()
352
+ log_startup_warning("load_cain_status", "Status file was missing, recreated it", {
353
+ "path": str(CAIN_STATUS_FILE)
354
+ })
355
+ return status
356
+ except json.JSONDecodeError as e:
357
+ log_startup_error("load_cain_status", f"Invalid JSON in status file: {e}", {
358
+ "path": str(CAIN_STATUS_FILE)
359
+ })
360
+ # Recreate the file with valid data
361
+ status = ensure_cain_status_file()
362
+ return status
363
  except Exception as e:
364
+ log_startup_error("load_cain_status", f"Error loading status: {e}", {
365
+ "path": str(CAIN_STATUS_FILE),
366
+ "error_type": type(e).__name__
367
+ })
368
  return {
369
  "current_state": "error",
370
  "last_updated": datetime.utcnow().isoformat() + "Z",