"""Inspect or stop only the recorded, command-verified campaign/API process.""" import argparse import json import os from pathlib import Path import signal ROOT = Path(__file__).resolve().parents[1] def command(pid): try: return Path(f'/proc/{pid}/cmdline').read_bytes().split(b'\0')[:-1] except FileNotFoundError: return [] def main(): parser=argparse.ArgumentParser(description=__doc__) parser.add_argument('--campaign',help='Defaults to ~/ai/opensysone/runs/LAST_CAMPAIGN') parser.add_argument('--stop',action='store_true',help='TERM the verified supervisor, or its API if training is complete') args=parser.parse_args() campaign=Path(args.campaign) if args.campaign else Path((Path.home()/'ai/opensysone/runs/LAST_CAMPAIGN').read_text().strip()) campaign=campaign.resolve() state=json.loads((campaign/'state.json').read_text()) plan=json.loads((campaign/'plan.json').read_text()) supervisor=state.get('supervisor_pid') supervisor_args=command(supervisor) if supervisor else [] ours=(str(ROOT/'scripts/launch_24h.py').encode() in supervisor_args and str(campaign).encode() in supervisor_args) api=state.get('api_pid') api_args=command(api) if api else [] api_ours=(api_args==[part.encode() for part in state.get('api_command',[])]) if api_args else False child=state.get('child_pid') child_args=command(child) if child else [] child_ours=(child_args==[part.encode() for part in state.get('child_command',[])]) if child_args else False if args.stop: if ours: os.kill(supervisor,signal.SIGTERM) print('Stop requested; supervisor will checkpoint training and stop subsequent stages.') elif child_ours: os.kill(child,signal.SIGTERM) print('Stop requested for the recorded orphan child; training checkpoints at its next boundary.') elif api_ours: os.kill(api,signal.SIGTERM) print('Stop requested for the recorded API process.') else: raise RuntimeError('No recorded, command-verified process remains to stop') print(json.dumps({'plan':plan,'state':state,'supervisor_running':ours,'child_running':child_ours,'api_running':api_ours},indent=2)) if __name__=='__main__': main()