"""Pure presentation of service-verified statistics, never model table values.""" from __future__ import annotations import copy import re def _table_ranges(lines): ranges=[];index=0 while index]*>.*?','',remaining,flags=re.I|re.S) if ranges or count: diagnostics.setdefault('modelTablesReplacedByVerifiedData',[]).append({'block':index,'tables':len(ranges)+count}) if not any(line.strip() and not re.match(r'^\s*#{1,6}\s',line) for line in remaining.splitlines()):continue block['text']=remaining.strip() kept.append(block) result['blocks']=kept return result def _native_table_markdown(markdown, refs): lines=markdown.splitlines();ranges=_table_ranges(lines) if not ranges:return None table_lines={i for start,end in ranges for i in range(start,end)} # Existing service captions retain period, ordering and displayed TopN. # Anchored prose is redundant with the model's independent explanation. shown=[line for i,line in enumerate(lines) if i in table_lines or not any('['+ref+']' in line for ref in refs)] return '\n'.join(shown).strip() def native_sections(sources, facts): result=[] for source in sources: refs=source['evidenceRefs'];text=_native_table_markdown(source['markdown'],refs) if not text:continue ids=[fact['id'] for fact in facts if fact.get('evidenceRef') in refs] if ids:result.append({'markdown':text,'factIds':ids,'evidenceRefs':list(refs),'scope':copy.deepcopy(source.get('scope') or {})}) return result def history_sections(snapshot, facts, renderer): result=[];seen=set() for index,section in enumerate(snapshot.get('sections') or []): text=renderer(snapshot,section) if not text or text in seen:continue ids=[fact['id'] for fact in facts if (fact.get('scope') or {}).get('sectionIndex')==index] if not ids:continue seen.add(text);result.append({'markdown':text,'factIds':ids, 'evidenceRefs':list(dict.fromkeys(fact['evidenceRef'] for fact in facts if fact['id'] in ids)), 'scope':copy.deepcopy({k:section[k] for k in ('period','filters','stage','dimension') if k in section})}) return result def attach_verified_sections(blocks, sections, facts, summary_selection, diagnostics): """Append exact data only to accepted explanations that reference its source.""" result=copy.deepcopy(blocks);known={fact['id']:fact for fact in facts};used=set() selected=({summary_selection['leadIndex'],*summary_selection['evidenceIndexes']} if summary_selection else set()) for block in result: if block.get('index') not in selected:continue narrative_ids=set(block.get('factIds') or []);chosen=[] for index,section in enumerate(sections): if index in used or not narrative_ids.intersection(section['factIds']):continue used.add(index);chosen.append(section) if not chosen:continue block['text']+='\n\n'+'\n\n'.join(section['markdown'] for section in chosen) table_ids=list(dict.fromkeys(key for section in chosen for key in section['factIds'] if key in known)) block['factIds']=list(dict.fromkeys([*block['factIds'],*table_ids])) block['evidenceRefs']=list(dict.fromkeys([*block['evidenceRefs'],*(known[key]['evidenceRef'] for key in table_ids)])) diagnostics['verifiedDataPresentation']={'displayedSections':len(used),'availableSections':len(sections),'fullDataRetained':True} return result