import json,subprocess,sys,time,re,hashlib from pathlib import Path sys.path.insert(0,str(Path(__file__).parent)) from cadence_audit import identity,ipc root=Path(__file__).parent m=json.loads((root/'descriptor.json').read_text()) before=identity() assert before['system']==m['system'] and before['profile']==m['system'] assert before['sway_sha256']==m['normal_sha256'] assert before['apps']==['k230-terminal'] and before['wallpaper']['state']=='still' assert len(before['outputs'])==1 and before['outputs'][0]['max_render_time']==8 assert before['outputs'][0]['transform']=='270' override=Path('/run/systemd/system/shell.service.d/94-source3-cadence-timing.conf') assert not override.exists() stable=root/'recovery';stable.mkdir(mode=0o700) start=subprocess.check_output(['systemctl','show','shell.service','-p','ExecStart','--value'],text=True) old=re.search(r'path=([^;]+);',start).group(1).strip() argv=re.search(r'argv\[\]=([^;]+);',start).group(1).strip() assert argv==old or argv.startswith(old+' ') args=argv[len(old):];assert re.fullmatch(r'[ /A-Za-z0-9_.+?=:-]*',args) assert re.fullmatch(r'/nix/store/[0-9a-z]{32}-k230-card-shell-hdmi-trial',m['candidate']) assert hashlib.sha256(Path(m['candidate_exe']).read_bytes()).hexdigest()==m['candidate_sha256'] restore=stable/'restore.sh' restore.write_text('#!/bin/sh\nset -eu\nrm -f '+str(override)+'\nsystemctl daemon-reload\nsystemctl restart shell.service shell-ui.service theme-helper.service\n') restore.chmod(0o700) unit='k230-source3-cadence-elapsed-20261021-attempt2' subprocess.run(['systemd-run','--quiet','--unit='+unit,'--on-active=300s',str(restore)],check=True) assert subprocess.run(['systemctl','is-active','--quiet',unit+'.timer']).returncode==0 gc=Path('/nix/var/nix/gcroots/source3-cadence-elapsed-20261021') if gc.exists():assert gc.is_symlink() and str(gc.resolve())==m['candidate'] else:gc.symlink_to(m['candidate']) meta={'source_revision':m['source_revision'],'descriptor':m,'before':before,'started_utc':time.strftime('%Y-%m-%dT%H:%M:%SZ',time.gmtime()),'runs':[],'restore_unit':unit} (stable/'before.private.json').write_text(json.dumps({**meta,'normal_exec_start':start},indent=2)+'\n') def ready(sha,timeout=60): end=time.monotonic()+timeout while time.monotonic()