from pathlib import Path import copy,hashlib,importlib.util,json,subprocess,tempfile BASE=Path(__file__).resolve().parent spec=importlib.util.spec_from_file_location('net',BASE/'first_network.py');net=importlib.util.module_from_spec(spec);spec.loader.exec_module(net) m=dict(w1=[[.2,-.1],[-.3,.4],[.1,.3],[.2,.1]],b1=[.01]*4,w2=[.1,-.2,.3,.2],b2=.04);rows=[dict(x=[.2,.8],y=1),dict(x=[.9,.1],y=0)];g=net.gradients(m,rows);eps=1e-6;errors=[] paths=[('b2',)]+[(key,j) for key in ['b1','w2'] for j in range(4)]+[('w1',j,k) for j in range(4) for k in range(2)] def get(o,path): for key in path:o=o[key] return o def put(o,path,v): for key in path[:-1]:o=o[key] o[path[-1]]=v for path in paths: a,b=copy.deepcopy(m),copy.deepcopy(m);put(a,path,get(m,path)+eps);put(b,path,get(m,path)-eps) numeric=(net.evaluate(a,rows)['loss']-net.evaluate(b,rows)['loss'])/(2*eps);errors.append(abs(numeric-get(g,path))) assert max(errors)<1e-7 host=Path('/var/lib/dpkg/status');before=hashlib.sha256(host.read_bytes()).hexdigest() logs=[] def cmd(args): if args[0]=='dpkg' and any(a.startswith('--root=') for a in args): own_root=next(a.split('=',1)[1] for a in args if a.startswith('--root='));args=[args[0],'--log='+own_root+'/study-dpkg.log']+args[1:] r=subprocess.run(args,capture_output=True,text=True);logs.append(dict(args=args,returncode=r.returncode,stdout=r.stdout,stderr=r.stderr));return r with tempfile.TemporaryDirectory(prefix='sm97-package-') as temp: d=Path(temp);pkg=d/'package';root=d/'root';root.mkdir();(pkg/'DEBIAN').mkdir(parents=True);(pkg/'usr/bin').mkdir(parents=True) (pkg/'DEBIAN/control').write_text('Package: edu-first-app\nVersion: 1.0\nSection: misc\nPriority: optional\nArchitecture: all\nMaintainer: Study Example \nDescription: Isolated own learning program\n') app=pkg/'usr/bin/edu-first-app';app.write_text('#!/bin/sh\nprintf "study app ready\\n"\n');app.chmod(0o755) deb=(BASE/'edu-first-app_1.0_all.deb').resolve();assert cmd(['dpkg-deb','--build',str(pkg),str(deb)]).returncode==0 assert cmd(['dpkg','--root='+str(root),'--install',str(deb)]).returncode==0 installed=root/'usr/bin/edu-first-app';assert installed.exists();r=cmd([str(installed)]);assert r.stdout.strip()=='study app ready' query=['dpkg-query','--admindir='+str(root/'var/lib/dpkg'),'-W','-f=${Status}','edu-first-app'];assert cmd(query).stdout=='install ok installed' assert cmd(['dpkg','--root='+str(root),'--remove','edu-first-app']).returncode==0 assert not installed.exists();q=cmd(query);assert q.returncode!=0 or q.stdout!='install ok installed' assert cmd(['dpkg','--root='+str(root),'--audit']).returncode==0 assert hashlib.sha256(host.read_bytes()).hexdigest()==before v=cmd(['dpkg','--version']).stdout.splitlines()[0] report=dict(gradient_parameters=len(paths),max_gradient_error=max(errors),network_runs=1,dpkg_version=v,os='Ubuntu24.04.3',host_package_database_unchanged=True,host_status_sha256=before,own_package='edu-first-app1.0',package_scripts=False,isolated_root=True,program_executed=True,removed_file_absent=True,logs=logs) (BASE/'native_verification.json').write_text(json.dumps(report,ensure_ascii=False,indent=2));print({k:v for k,v in report.items() if k!='logs'})