"""Printable inspection evidence; presentation only, no model or storage writes.""" import html import math from urllib.parse import urlsplit from .reviews import parse_review def esc(value): return html.escape(str(value),quote=True) def number(value): return f'{value:.5f}' if isinstance(value,(int,float)) and math.isfinite(value) else '—' def image_url(value): value=str(value or '') parsed=urlsplit(value) # Preserve scoped capability/signed URLs without allowing executable schemes. return esc(value) if (value.startswith('/api/') and not parsed.netloc) or parsed.scheme=='https' else '' STYLE=''' :root{color-scheme:light;--ink:#202744;--muted:#697797;--line:#e0e4f2;--purple:#6554ed} *{box-sizing:border-box}body{margin:0;background:#f5f6fc;color:var(--ink);font:15px/1.6 system-ui,sans-serif} main{max-width:1120px;margin:36px auto;padding:0 24px}header{background:linear-gradient(115deg,#202442,#454893);color:white;padding:32px;border-radius:20px} .eyebrow{font-size:11px;letter-spacing:.16em;text-transform:uppercase;font-weight:700}.header-row,.section-heading{display:flex;justify-content:space-between;align-items:center;gap:20px} h1{font-size:32px;line-height:1.2;margin:12px 0}header p{color:#e0e2ff;margin:8px 0}h2{font-size:20px;margin:0 0 18px}h3{font-size:15px} button{background:white;color:#343663;border:0;border-radius:10px;padding:12px 18px;font:inherit;font-weight:650;cursor:pointer;white-space:nowrap} .card{background:white;border:1px solid var(--line);border-radius:18px;padding:26px;margin:20px 0}.summary{display:grid;grid-template-columns:1.4fr 1fr 1fr 1fr;gap:20px}.metric small,.muted,figcaption{color:var(--muted)}.metric strong{display:block;font-size:25px;line-height:1.4;font-variant-numeric:tabular-nums} .badge{display:inline-block;padding:5px 12px;border-radius:30px;background:#f0ecff;color:#5b45bb;font-weight:700}.good{background:#e7f8f2;color:#08785b}.anomalous{background:#fff0e5;color:#a44814}.uncertain{background:#fff6d6;color:#826300} .note{background:#f6f4ff;border:1px solid #e1dcff;border-radius:10px;padding:14px 18px}.images{display:grid;grid-template-columns:1fr 1fr;gap:22px}figure{margin:0;min-width:0}img{display:block;width:100%;height:300px;object-fit:contain;background:#f5f6fc;border-radius:12px}figcaption{font-size:13px;margin-top:8px} table{width:100%;border-collapse:collapse;table-layout:fixed}th,td{text-align:left;border-bottom:1px solid var(--line);padding:11px 12px;vertical-align:top;overflow-wrap:anywhere}th{width:30%;font-size:13px;color:var(--muted);font-weight:600}.section-no{font-size:12px;color:var(--purple);letter-spacing:.1em} article{border-left:3px solid #d7d0ff;padding:2px 18px;margin:18px 0}article p{white-space:pre-wrap;overflow-wrap:anywhere}footer{color:var(--muted);font-size:12px;padding:8px 2px 30px}.empty{color:var(--muted)} @media(max-width:700px){main{padding:0 14px;margin:14px auto}header,.card{padding:20px}.header-row{align-items:flex-start;flex-direction:column}.summary,.images{grid-template-columns:1fr 1fr}h1{font-size:26px}img{height:220px}} @media(max-width:440px){.images{grid-template-columns:1fr}.summary{gap:18px}.metric strong{font-size:20px}th{width:38%}} @media print{@page{margin:14mm}body{background:white;font-size:11px}main{margin:0;padding:0;max-width:none}header{background:white;color:var(--ink);border-bottom:3px solid var(--purple);border-radius:0;padding:0 0 18px}header p{color:var(--muted)}button{display:none}.card{border-radius:0;padding:16px 0;border:0;border-bottom:1px solid var(--line)}.summary,.images,article,tr{break-inside:avoid}img{height:240px}h2,h3{break-after:avoid}.note{background:white}} ''' def render_report(record,reviews,notes,messages,local=False): fields=('id','created_at','category','batch','filename','verdict','score','threshold', 'uncertainty_margin','model_id','profile','model_sha256','inference_ms', 'total_model_ms','localization','scope','storage','sync_version','alert_acknowledged') rows=''.join(f'{esc(key.replace("_"," ").capitalize())}{esc(record[key])}' for key in fields if key in record) review_html=''.join(f'
{esc(r["status"])} · {esc(r["decision"])}

{esc(r["reviewer"])} · {esc(r["created_at"])}

{esc(r["note"])}

' for r in reviews) review_html+=''.join(f'
Review note

{esc(n["reviewer"])} · {esc(n["created_at"])}

{esc(n["body"])}

' for n in notes if not parse_review(n['body'])) review_html=review_html or '

No human review decision has been recorded. A model verdict is not human acceptance.

' evidence=''.join(f'
{"Question" if m.get("role")=="user" else "Evidence Assistant"}

{esc(m.get("answer",m.get("question","")))}

' for m in messages) or '

No saved conversation yet. Ask the Evidence Assistant a question for this inspection, then refresh this report.

' sources={s['chunk_id']:s for m in messages for s in m.get('evidence',[])} source_html=''.join(f'

{esc(key)} · {esc(s["title"])}

{esc(s["text"])}

{esc(s["url"])} · {esc(s["scope"])}

' for key,s in sources.items()) or '

No document passages saved. Measurement-only answers use the saved inspection directly.

' verdict=record.get('verdict','Uncertain');tone={'Good':'good','Anomalous':'anomalous','Uncertain':'uncertain'}.get(verdict,'uncertain') images=''.join(f'
{alt}
{caption}
' for kind,alt,caption in ( ('image','Original inspected image','Original image · retained inspection input'), ('heatmap','Model anomaly heatmap','Heatmap · feature distance from normal reference patches, not a defect diagnosis'))) storage=record.get('storage','local_device') storage_copy={'supabase_personal':'Private account · Supabase','supabase':'Shared Team · Supabase','browser_session':'Temporary guest workspace · not durable Personal cloud storage','local_account':'Private local mirror · check synchronization for cloud persistence'}.get(storage,'Local device · not automatically cloud-saved') if local and storage=='browser_session':storage_copy='Guest inspection · local device only, outside a cloud account' def section(index,title,content): return f'

{title}

{index}
{content}
' summary=f'
Model verdict
{esc(verdict)}
Anomaly score{number(record.get("score"))}
Decision threshold{number(record.get("threshold"))}
Review margin±{number(record.get("uncertainty_margin"))}
' return f'''DefectRAG · Inspection {esc(record.get('id','')[:8])}
DefectRAG · Inspection intelligence

Inspection evidence report

{esc(record.get('category','').replace('_',' ').title())} · {esc(record.get('created_at',''))}

{esc(storage_copy)}

{summary}

Anomaly scores are feature distances, not accuracy, confidence or defect probability. Localization highlights unusual appearance; it does not establish a defect type or cause. Human review remains necessary.

{section('01','Image and localization','
'+images+'
')} {section('02','Inspection measurements',''+rows+'
')} {section('03','Human review',review_html)}{section('04','Evidence conversation',evidence)}{section('05','Retrieved documentation',source_html)}
'''