rohitsar567 Claude Opus 4.7 (1M context) commited on
Commit
59cf54e
Β·
1 Parent(s): e204c0e

fix(upload-extract): use _catalogue_scorecard primary path for status

Browse files

Previous fix mirrored only the fallback path of /api/policies/{id}/scorecard
(build_scorecard(extracted, insurer_reviews)). The endpoint's PRIMARY path
is _catalogue_scorecard(policy_id, profile), which builds off the
marketplace catalogue (heuristic record.json + curated overlay + dedup +
insurer reviews) β€” that's what produces the 52.2%/grade-C the user sees.
The bare build_scorecard path reads 17.4% because it doesn't see the
heuristic merge.

Same resolution order as the live scorecard endpoint:
1. _catalogue_scorecard(pid, None) ← primary
2. build_scorecard(extracted, insurer_reviews) ← fallback

This makes status.completeness_pct + status.overall_grade equal to the
scorecard endpoint's response BY CONSTRUCTION for every upload that
becomes a marketplace card (which is every successful upload).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

Files changed (1) hide show
  1. backend/uploaded_docs.py +30 -18
backend/uploaded_docs.py CHANGED
@@ -1147,27 +1147,39 @@ async def extract_one_for_upload(
1147
 
1148
  # Resolve the freshly-graded card so the status can report the
1149
  # actual completeness + grade the CHAT CARD will show. Mirror
1150
- # the /api/policies/{id}/scorecard endpoint's resolution path
1151
- # byte-for-byte: load the same EXTRACTED_DIR/<pid>.json we just
1152
- # wrote AND pass the same insurer_reviews. Without the reviews
1153
- # arg the Claim Experience sub-score (and downstream completeness
1154
- # accounting) diverges from what the card endpoint serves β€” that
1155
- # was the 2026-05-27 "17.4% status vs 52.2% card" false-regression
1156
- # the operator saw on the proof upload.
 
1157
  _final_completeness = None
1158
  _final_grade = None
1159
  try:
1160
- from backend.scorecard import build_scorecard as _bs
1161
- _doc_for_sc = json.loads(out_json.read_text())
1162
- _ir = None
1163
- if insurer_slug:
1164
- _rp = _settings.DATA_DIR / "reviews" / f"{insurer_slug}.json"
1165
- if _rp.exists():
1166
- try:
1167
- _ir = json.loads(_rp.read_text())
1168
- except Exception:
1169
- _ir = None
1170
- _sc = _bs(_doc_for_sc, insurer_reviews=_ir, profile=None)
 
 
 
 
 
 
 
 
 
 
 
1171
  if _sc is not None:
1172
  _final_completeness = float(_sc.data_completeness_pct)
1173
  _final_grade = _sc.overall_grade
 
1147
 
1148
  # Resolve the freshly-graded card so the status can report the
1149
  # actual completeness + grade the CHAT CARD will show. Mirror
1150
+ # the /api/policies/{id}/scorecard endpoint's resolution order
1151
+ # byte-for-byte: PRIMARY path is the marketplace catalogue
1152
+ # scorecard (_catalogue_scorecard) which folds in the heuristic
1153
+ # record.json + curated overlay + insurer reviews + product
1154
+ # dedup β€” that's what produces the 52.2%/grade-C the user sees
1155
+ # on the inline card. The fallback path (build_scorecard on the
1156
+ # bare extracted JSON) reads 17.4% because it doesn't see the
1157
+ # heuristic merge or reviews-driven sub-scores.
1158
  _final_completeness = None
1159
  _final_grade = None
1160
  try:
1161
+ # PRIMARY β€” same call /api/policies/{id}/scorecard makes first.
1162
+ # Bust the marketplace grade cache first (we invalidated _MG_CACHE
1163
+ # above, but _catalogue_indices builds off the latest record.json
1164
+ # + extracted JSON on each call so this becomes a fresh build).
1165
+ import backend.main as _bm2
1166
+ _sc = _bm2._catalogue_scorecard(policy_id, None)
1167
+ if _sc is None:
1168
+ # FALLBACK β€” same path the endpoint falls through to for
1169
+ # non-catalogued ids. For user uploads this is just defensive;
1170
+ # the upload IS a marketplace card by design (record.json is
1171
+ # persisted under UPLOADED_DOCS_DIR/<pid>/).
1172
+ from backend.scorecard import build_scorecard as _bs
1173
+ _doc_for_sc = json.loads(out_json.read_text())
1174
+ _ir = None
1175
+ if insurer_slug:
1176
+ _rp = _settings.DATA_DIR / "reviews" / f"{insurer_slug}.json"
1177
+ if _rp.exists():
1178
+ try:
1179
+ _ir = json.loads(_rp.read_text())
1180
+ except Exception:
1181
+ _ir = None
1182
+ _sc = _bs(_doc_for_sc, insurer_reviews=_ir, profile=None)
1183
  if _sc is not None:
1184
  _final_completeness = float(_sc.data_completeness_pct)
1185
  _final_grade = _sc.overall_grade