Spaces:
Sleeping
fix(upload-extract): hash-cache short-circuit now matches scorecard endpoint
Browse filesThe Tier-2 content-hash short-circuit had the SAME three bugs the main
extraction path used to have:
1. Called build_scorecard(doc, profile=None) without insurer_reviews
β completeness diverged from the card endpoint (17.4 vs 47.8/52.2)
2. Read `.overall_grade` off the Scorecard dataclass (attr is `.grade`)
β grade always None on cache-hit
3. Never set llm_used β operator can't see WHY no LLM ran on cache-hit
All three caused the multi-insurer audit to show
status='complete' llm=None comp=17.4 grade=None for a card the user sees
as 47.8% / grade C.
Fix applies the same resolution as the main path:
- Bust _MG_CACHE FIRST so _catalogue_scorecard rebuilds with new card
- PRIMARY: _bm._catalogue_scorecard(pid, None)
- FALLBACK: build_scorecard(doc, insurer_reviews=ir, profile=None)
- Read sc.grade (not sc.overall_grade)
- Set llm_used="hash-cache" so operator sees the cache hit explicitly
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
- backend/uploaded_docs.py +45 -15
|
@@ -874,22 +874,9 @@ async def extract_one_for_upload(
|
|
| 874 |
_settings_a.EXTRACTED_DIR.mkdir(parents=True, exist_ok=True)
|
| 875 |
dest = _settings_a.EXTRACTED_DIR / f"{policy_id}.json"
|
| 876 |
dest.write_text(cached_path.read_text())
|
| 877 |
-
# Mark status complete with whatever completeness the
|
| 878 |
-
# marketplace scorecard now computes.
|
| 879 |
-
_final_comp = None
|
| 880 |
-
_final_grade = None
|
| 881 |
-
try:
|
| 882 |
-
from rag.schema import HealthPolicy
|
| 883 |
-
from backend.scorecard import build_scorecard as _bs
|
| 884 |
-
_doc = json.loads(dest.read_text())
|
| 885 |
-
_sc = _bs(_doc, profile=None)
|
| 886 |
-
if _sc is not None:
|
| 887 |
-
_final_comp = float(_sc.data_completeness_pct)
|
| 888 |
-
_final_grade = _sc.overall_grade
|
| 889 |
-
except Exception:
|
| 890 |
-
pass
|
| 891 |
# Bust the #40 grade cache so /api/policies/all
|
| 892 |
-
# picks up the new card
|
|
|
|
| 893 |
try:
|
| 894 |
import backend.main as _bm
|
| 895 |
with _bm._MG_LOCK:
|
|
@@ -897,11 +884,54 @@ async def extract_one_for_upload(
|
|
| 897 |
_bm._MG_CACHE["index"] = None
|
| 898 |
except Exception:
|
| 899 |
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 900 |
await _set_extraction_status(
|
| 901 |
policy_id, status="complete",
|
| 902 |
completed_at=_now(),
|
| 903 |
completeness_pct=_final_comp,
|
| 904 |
overall_grade=_final_grade,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 905 |
)
|
| 906 |
return True
|
| 907 |
except Exception as e: # noqa: BLE001 β cache miss is fine, run fresh
|
|
|
|
| 874 |
_settings_a.EXTRACTED_DIR.mkdir(parents=True, exist_ok=True)
|
| 875 |
dest = _settings_a.EXTRACTED_DIR / f"{policy_id}.json"
|
| 876 |
dest.write_text(cached_path.read_text())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 877 |
# Bust the #40 grade cache so /api/policies/all
|
| 878 |
+
# picks up the new card BEFORE we resolve the
|
| 879 |
+
# catalogue scorecard for status reporting.
|
| 880 |
try:
|
| 881 |
import backend.main as _bm
|
| 882 |
with _bm._MG_LOCK:
|
|
|
|
| 884 |
_bm._MG_CACHE["index"] = None
|
| 885 |
except Exception:
|
| 886 |
pass
|
| 887 |
+
# Mark status complete with the EXACT completeness +
|
| 888 |
+
# grade the chat card will render. Mirror the live
|
| 889 |
+
# scorecard endpoint's resolution order (catalogue
|
| 890 |
+
# primary β bare-policy fallback with insurer_reviews).
|
| 891 |
+
# Earlier draft of this branch called
|
| 892 |
+
# build_scorecard(_doc, profile=None) without reviews
|
| 893 |
+
# and read `.overall_grade` instead of `.grade`, so
|
| 894 |
+
# cache-hit uploads always reported compβ17.4 + grade
|
| 895 |
+
# None even when the actual card was 47.8% / grade C
|
| 896 |
+
# (the 2026-05-27 multi-PDF audit caught this).
|
| 897 |
+
_final_comp = None
|
| 898 |
+
_final_grade = None
|
| 899 |
+
try:
|
| 900 |
+
from backend.scorecard import build_scorecard as _bs
|
| 901 |
+
# PRIMARY β catalogue scorecard (matches /api/policies/{id}/scorecard).
|
| 902 |
+
_sc = _bm._catalogue_scorecard(policy_id, None)
|
| 903 |
+
if _sc is None:
|
| 904 |
+
# FALLBACK β bare-policy build_scorecard with reviews.
|
| 905 |
+
_doc = json.loads(dest.read_text())
|
| 906 |
+
_ir = None
|
| 907 |
+
if insurer_slug:
|
| 908 |
+
from backend.config import settings as _settings_b
|
| 909 |
+
_rp = _settings_b.DATA_DIR / "reviews" / f"{insurer_slug}.json"
|
| 910 |
+
if _rp.exists():
|
| 911 |
+
try:
|
| 912 |
+
_ir = json.loads(_rp.read_text())
|
| 913 |
+
except Exception:
|
| 914 |
+
_ir = None
|
| 915 |
+
_sc = _bs(_doc, insurer_reviews=_ir, profile=None)
|
| 916 |
+
if _sc is not None:
|
| 917 |
+
_final_comp = float(_sc.data_completeness_pct)
|
| 918 |
+
_final_grade = _sc.grade # NOT overall_grade
|
| 919 |
+
except Exception as _sc_err: # noqa: BLE001
|
| 920 |
+
_log.warning(
|
| 921 |
+
"[upload-extract] cache-hit status resolve failed for %s: %s",
|
| 922 |
+
policy_id, _sc_err,
|
| 923 |
+
)
|
| 924 |
await _set_extraction_status(
|
| 925 |
policy_id, status="complete",
|
| 926 |
completed_at=_now(),
|
| 927 |
completeness_pct=_final_comp,
|
| 928 |
overall_grade=_final_grade,
|
| 929 |
+
# Provenance β operator sees WHY no LLM ran: the
|
| 930 |
+
# SHA256 of pdf_bytes matched a prior extraction
|
| 931 |
+
# so we reused it. Distinct from the gemini-2.5-
|
| 932 |
+
# flash#N / nim-fallback labels.
|
| 933 |
+
llm_used="hash-cache",
|
| 934 |
+
llm_response_chars=len((dest.read_text() or "")),
|
| 935 |
)
|
| 936 |
return True
|
| 937 |
except Exception as e: # noqa: BLE001 β cache miss is fine, run fresh
|