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 import argparse,importlib.util s=importlib.util.spec_from_file_location('profile_trace',Path(__file__).parent/'tools/landscape-hdmi-stall-trace.py') trace=importlib.util.module_from_spec(s);s.loader.exec_module(trace) root=Path(__file__).parent out=Path('/run/k230-source3-render-profile-20261021');out.mkdir(mode=0o700) 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.write_text(restore.read_text().replace('rm -f ', 'if [ -f /run/k230-source3-render-profile-20261021/traced/trace-state.json ] && [ ! -f /run/k230-source3-render-profile-20261021/traced/trace-cleanup.json ]; then '+sys.executable+' '+str(root/'tools/landscape-hdmi-stall-trace.py')+' --cleanup /run/k230-source3-render-profile-20261021/traced/trace-state.json || true; fi\nrm -f ',1)) restore.chmod(0o700) unit='k230-source3-render-profile-20261021' subprocess.run(['systemd-run','--quiet','--unit='+unit,'--on-active=480s',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()