cvpfus Codex commited on
Commit
8ed88b6
·
1 Parent(s): f158a05

Add reader brain health diagnostics

Browse files

Co-authored-by: Codex <codex@openai.com>

Files changed (2) hide show
  1. app.py +16 -2
  2. scripts/verify.py +36 -0
app.py CHANGED
@@ -647,6 +647,7 @@ def _runtime_status_core() -> dict[str, Any]:
647
  llama_start = time.perf_counter()
648
  llama_status: dict[str, Any]
649
  if not LLAMA_CPP_BASE_URL:
 
650
  llama_status = {
651
  "available": False,
652
  "status": "fallback-ready",
@@ -657,15 +658,25 @@ def _runtime_status_core() -> dict[str, Any]:
657
  "elapsed_ms": _elapsed_ms(llama_start),
658
  }
659
  else:
 
660
  try:
 
 
 
 
 
661
  request = urllib.request.Request(
662
- f"{LLAMA_CPP_BASE_URL}/models",
663
  headers=_llama_cpp_headers(),
664
  method="GET",
665
  )
666
  with urllib.request.urlopen(request, timeout=1.5) as response:
667
  payload = json.loads(response.read().decode("utf-8"))
668
  model_ids = [item.get("id", "") for item in payload.get("data", []) if isinstance(item, dict)]
 
 
 
 
669
  llama_status = {
670
  "available": True,
671
  "status": "online",
@@ -675,13 +686,16 @@ def _runtime_status_core() -> dict[str, Any]:
675
  "elapsed_ms": _elapsed_ms(llama_start),
676
  }
677
  except (OSError, urllib.error.URLError, TimeoutError, ValueError, json.JSONDecodeError) as exc:
 
 
678
  llama_status = {
679
  "available": False,
680
  "status": "fallback-ready",
681
  "base_url": LLAMA_CPP_BASE_URL,
682
  "model": LLAMA_CPP_MODEL,
683
  "fallback": "rule-based local narration",
684
- "warning": exc.__class__.__name__,
 
685
  "elapsed_ms": _elapsed_ms(llama_start),
686
  }
687
 
 
647
  llama_start = time.perf_counter()
648
  llama_status: dict[str, Any]
649
  if not LLAMA_CPP_BASE_URL:
650
+ _runtime_log("llama.cpp reader brain endpoint is not configured; using narration fallback")
651
  llama_status = {
652
  "available": False,
653
  "status": "fallback-ready",
 
658
  "elapsed_ms": _elapsed_ms(llama_start),
659
  }
660
  else:
661
+ models_url = f"{LLAMA_CPP_BASE_URL}/models"
662
  try:
663
+ _runtime_log(
664
+ "llama.cpp reader brain health check "
665
+ f"url={models_url} token={'configured' if LLAMA_CPP_TOKEN else 'not-set'} "
666
+ "timeout=1.5s"
667
+ )
668
  request = urllib.request.Request(
669
+ models_url,
670
  headers=_llama_cpp_headers(),
671
  method="GET",
672
  )
673
  with urllib.request.urlopen(request, timeout=1.5) as response:
674
  payload = json.loads(response.read().decode("utf-8"))
675
  model_ids = [item.get("id", "") for item in payload.get("data", []) if isinstance(item, dict)]
676
+ _runtime_log(
677
+ "llama.cpp reader brain health response "
678
+ f"models={model_ids[:6]} expected={LLAMA_CPP_MODEL}"
679
+ )
680
  llama_status = {
681
  "available": True,
682
  "status": "online",
 
686
  "elapsed_ms": _elapsed_ms(llama_start),
687
  }
688
  except (OSError, urllib.error.URLError, TimeoutError, ValueError, json.JSONDecodeError) as exc:
689
+ detail = _http_exception_detail(exc)
690
+ _runtime_log(f"llama.cpp reader brain health failed url={models_url} error={detail}")
691
  llama_status = {
692
  "available": False,
693
  "status": "fallback-ready",
694
  "base_url": LLAMA_CPP_BASE_URL,
695
  "model": LLAMA_CPP_MODEL,
696
  "fallback": "rule-based local narration",
697
+ "health_url": models_url,
698
+ "warning": detail,
699
  "elapsed_ms": _elapsed_ms(llama_start),
700
  }
701
 
scripts/verify.py CHANGED
@@ -579,6 +579,42 @@ def verify_modal_klein_integration() -> None:
579
  "Runtime setup should show reader token as configured without exposing the value",
580
  )
581
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
582
  # Test: runtime status includes Modal Klein path
583
  with patch.object(app, "KLEIN_MODAL_ENDPOINT", ""):
584
  status = app._runtime_status_core()
 
579
  "Runtime setup should show reader token as configured without exposing the value",
580
  )
581
 
582
+ # Test: reader-brain runtime status exposes actionable health details without leaking tokens
583
+ with patch.object(app, "LLAMA_CPP_BASE_URL", "https://reader.example/v1"), patch.object(app, "LLAMA_CPP_TOKEN", "reader-secret"):
584
+ http_error = urllib.error.HTTPError(
585
+ "https://reader.example/v1/models",
586
+ 401,
587
+ "Unauthorized",
588
+ {},
589
+ None,
590
+ )
591
+ http_error.fp = MagicMock()
592
+ http_error.fp.read.return_value = b'{"detail":"Unauthorized"}'
593
+ with patch("urllib.request.urlopen", side_effect=http_error) as urlopen_mock:
594
+ status = app._runtime_status_core()
595
+ reader_status = status["reader_brain"]
596
+ health_request = urlopen_mock.call_args.args[0]
597
+ assert_true(
598
+ health_request.headers.get("Authorization") == "Bearer reader-secret",
599
+ "Reader-brain health checks should send Bearer token when configured",
600
+ )
601
+ assert_true(
602
+ "HTTPError status=401" in reader_status["warning"],
603
+ "Reader-brain runtime warning should include HTTP status details",
604
+ )
605
+ assert_true(
606
+ "Unauthorized" in reader_status["warning"],
607
+ "Reader-brain runtime warning should include response body details",
608
+ )
609
+ assert_true(
610
+ "reader-secret" not in json.dumps(reader_status),
611
+ "Reader-brain runtime status should not expose token values",
612
+ )
613
+ assert_true(
614
+ reader_status["health_url"] == "https://reader.example/v1/models",
615
+ "Reader-brain runtime status should expose the health URL",
616
+ )
617
+
618
  # Test: runtime status includes Modal Klein path
619
  with patch.object(app, "KLEIN_MODAL_ENDPOINT", ""):
620
  status = app._runtime_status_core()