PhillNet_Recall / scripts /stress_evidence.py
ayjays132's picture
Upload 121 files
3b3b2bd verified
Raw History Blame Contribute Delete
5.61 kB
"""Independent integer-graph oracle and numerical contract checks (synthetic)."""
from pathlib import Path
import sys,json,random,time,hashlib,platform,statistics,math,tempfile
ROOT=Path(__file__).resolve().parents[1];sys.path.insert(0,str(ROOT))
from phillnet_recall import RecallMemory,RecallPolicy
from phillnet_recall.evidence import EvidencePolicy
def main():
rng=random.Random(800714);policy=RecallPolicy();passed=0;raw=[]
# Independent oracle uses integer adjacency and exact set joins, not library paths.
for trial in range(512):
n=12;edges=[]
for a in range(n):
for b in range(a+1,n):
if rng.random()<.14:edges.append((a,rng.choice(['p','q','r']),b))
start=rng.randrange(n);steps=[rng.choice(['p','q','r']) for _ in range(rng.randint(1,4))]
current={start};partial=False
for label in steps:
nxt=set()
for node in current:
children={b for a,p,b in edges if a==node and p==label}
if not children:partial=True
nxt.update(children)
current=nxt
if not current:break
expected_status='unknown' if not current else 'incomplete' if partial else 'supported'
expected_values=sorted(f'N{v}' for v in current) if expected_status=='supported' else []
with RecallMemory(policy=policy,clock=lambda:100.) as m:
for a,p,b in edges:m.remember(f'N{a} {p} N{b}',source='synthetic',entity=f'N{a}',predicate=p,value=f'N{b}',cardinality='many')
r=m.follow(f'N{start}',steps)
good=r['status']==expected_status and sorted(r['answer_values'])==expected_values
passed+=good;raw.append({'trial':trial,'status':r['status'],'expected_status':expected_status,'expected':expected_values,'got':r['answer_values'],'passed':good})
ep=EvidencePolicy();ref=EvidencePolicy(native=False);num=0;maxdiff=0.
for ident in (31000,31001):
rows=[[rng.random() for _ in range(20)] for _ in range(2048)]
rows+=[[0.]*20,[1.]*20]+[[float(i==j) for i in range(20)] for j in range(20)]
aa=ep.bank.score_many([ident]*len(rows),rows);bb=ref.bank.score_many([ident]*len(rows),rows)
num+=sum(a==b for a,b in zip(aa,bb));maxdiff=max(maxdiff,max(abs(a-b) for a,b in zip(aa,bb)))
# Tamper a correctly hashed certificate and ensure recomputation defeats it.
rejects=0
with RecallMemory(policy=policy,clock=lambda:100.) as m:
m.remember('X p Y',source='test',entity='X',predicate='p',value='Y')
c=m.follow('X',['p'])['certificate']
from phillnet_recall.core import canonical_json
for i in range(128):
cp=json.loads(json.dumps(c));cp['answer_values']=[f'forged{i}']
cp['sha256']=hashlib.sha256(canonical_json({k:v for k,v in cp.items() if k!='sha256'}).encode()).hexdigest()
rejects+=not m.verify_evidence(cp)['valid']
# A real 10,000-record snapshot workload. Latency includes all scope scanning.
with RecallMemory(policy=policy,clock=lambda:100.) as m:
for i in range(100):
m.remember(f'X{i} p Y{i}',source='stress',entity=f'X{i}',predicate='p',value=f'Y{i}')
m.remember(f'Y{i} q Z{i}',source='stress',entity=f'Y{i}',predicate='q',value=f'Z{i}')
for i in range(9800):m.remember(f'Unrelated{i} status waiting',source='stress',entity=f'Unrelated{i}',predicate='status',value='waiting')
times=[];scale_pass=0
for i in range(40):
t=time.perf_counter_ns();r=m.follow(f'X{i}',['p','q']);times.append((time.perf_counter_ns()-t)/1e6)
scale_pass+=r['answer_values']==[f'Z{i}']
def size(obj,seen):
if id(obj) in seen:return 0
seen.add(id(obj));n=sys.getsizeof(obj)
if isinstance(obj,dict):return n+sum(size(k,seen)+size(v,seen) for k,v in obj.items())
if isinstance(obj,(list,tuple,set,frozenset)):return n+sum(size(v,seen) for v in obj)
return n
cache_bytes=size([m._evidence_cache,m._evidence_graph_cache],set())
overflow=m.follow('X1',['p','q'],max_scan=9999)['status']
report={'construction':'synthetic same-author randomized property checks; distinct from public or human evaluations',
'graph_oracle':{'trials':512,'passed':passed,'rows':raw},
'numerics':{'probes':4140,'exact_matches':num,'max_abs_difference':maxdiff,'native_available':bool(ep.bank._lib)},
'forged_rehashed_certificates':{'n':128,'rejected':rejects},
'scale':{'records':10000,'queries':40,'correct':scale_pass,'median_ms':statistics.median(times),'p95_ms':sorted(times)[math.ceil(.95*len(times))-1],'raw_ms':times,'cold_first_ms':times[0],'warm_median_ms':statistics.median(times[1:]),'warm_p95_ms':sorted(times[1:])[math.ceil(.95*(len(times)-1))-1],'estimated_python_cache_bytes':cache_bytes,'scan_bound_control':overflow,'algorithm':'first read builds a bounded complete scope graph; warm reads use its adjacency indexes; any write or time boundary invalidates it','limitation':'same connection, no mutations between the timed queries; cache bytes are a Python deep-size estimate, not total RSS'},
'environment':{'python':sys.version,'platform':platform.platform()}}
(ROOT/'reports/evidence_stress.json').write_text(json.dumps(report,indent=2)+'\n')
print(json.dumps({k:v for k,v in report.items() if k not in ('graph_oracle',)},indent=2));print('oracle',passed,'/512')
if passed!=512 or rejects!=128 or num!=4140 or scale_pass!=40:raise SystemExit(1)
if __name__=='__main__':main()