"""Actual local allowlist/context and exact source-reference check; no model."""
from pathlib import Path
import json,hashlib
P=Path(__file__).parent;catalog=json.loads((P/'document-catalog.json').read_text());reader=catalog['reader_role'];allowed=[];excluded=[]
for item in catalog['documents']:
 doc=json.loads((P/item['file']).read_text())
 if item['active'] and item['access']==reader:allowed.append(doc)
 else:excluded.append({'file':item['file'],'reason':'archived' if not item['active'] else 'reader_not_permitted'})
context={'nature':'Actually assembled local allowed context; never sent to a model','reader_role':reader,'documents':allowed,'model_request_performed':False}
(P/'allowed-context.json').write_text(json.dumps(context,indent=2)+'\n')
raw=(P/'allowed-context.json').read_text();assert 'WITHHELD_TEST_MARKER_1042' not in raw and 'SYNTHETIC-WITHHELD-STUB' not in raw
assert len(allowed)==1 and allowed[0]['version']==4
paragraphs={x['id']:x['text'] for d in allowed for x in d['paragraphs']};q=json.loads((P/'questions.json').read_text())['questions']
assert 'department manager approval' in paragraphs[q[0]['citation']]
assert 'No individual request status' in paragraphs[q[1]['citation']]
old_context=json.loads((P/'old-version-context.json').read_text())
assert old_context['supplied_document']['version']==3 and not old_context['supplied_document']['active']
assert all(x['id']!='DEMO-EQUIPMENT/v4/section-3/p1' for x in old_context['supplied_document']['paragraphs'])
wrong=json.loads((P/'wrong-answer.json').read_text());assert 'equipment team approval' not in paragraphs[wrong['supplied_citation']]
receipt={'nature':'Actual local source/citation and pre-context access-filter checks, not model or search benchmark','allowed_document':'DEMO-EQUIPMENT/v4','included_paragraph_ids':list(paragraphs),'excluded':excluded,'withheld_marker_absent':True,'withheld_identifier_absent':True,'context_sha256':hashlib.sha256((P/'allowed-context.json').read_bytes()).hexdigest(),'questions':[{'id':'Q1','manual_expected':'Department manager','citation_exists':True,'supporting_text':paragraphs[q[0]['citation']],'result':'supported_reference'},{'id':'Q2','manual_expected':'insufficient_information','personal_status_supplied':False,'result':'information_gap'}],'failure_checks':{'old_only_context':'current_approval_passage_missing; source/version failure','wrong_answer_with_v4_context':'claim unsupported; answer failure'},'semantic_retrieval_performed':False,'model_request_performed':False,'private_data_accessed':False}
(P/'validation-receipt.json').write_text(json.dumps(receipt,indent=2)+'\n');print(json.dumps(receipt,indent=2))
