"""Measure empty owned CAD without changing product or existing acceptance data.""" import faulthandler import json from pathlib import Path import sys import time import traceback out = Path(__file__).resolve().parent repo = out.parents[1] sys.path.insert(0, str(repo / 'src')) if len(sys.argv) > 1: action, evidence, pid = sys.argv[1:] trace_path = Path(str(evidence) + '.' + action + '.trace.jsonl') with trace_path.open('w', encoding='utf-8', buffering=1) as log, Path(str(trace_path) + '.fault.txt').open('w') as fault: def record(stage, **details): log.write(json.dumps(dict(ns=time.time_ns(), stage=stage, **details)) + '\n') record('helper-entry') faulthandler.dump_traceback_later(5, repeat=True, file=fault) from dx_productivity.trial_task_center.isolated_cad import native_action record('helper-imported') def trace(frame, event, arg): if event == 'line' and (frame.f_code.co_name in {'_native_action', 'verify_application', 'project_file_path'}): record(frame.f_code.co_name, line=frame.f_lineno) return trace sys.settrace(trace) try: result = native_action(action, Path(evidence), int(pid)) finally: sys.settrace(None) faulthandler.cancel_dump_traceback_later() record('helper-result', result=result) raise SystemExit(result) from dx_productivity.trial_task_center import isolated_cad root = out / 'empty-cad-diagnostic' assert not root.exists() isolated_cad._helper_command = lambda action, evidence, pid: (sys.executable, '-X', 'utf8', str(Path(__file__).resolve()), action, str(evidence), str(pid)) session = isolated_cad.IsolatedCadSession(root) original_wait = session._wait_process # Diagnostic only: observe the eventual exit, never count this as product acceptance. session._wait_process = lambda process, timeout, phase: original_wait(process, 120, phase) record = dict(status='running', started_ns=time.time_ns(), diagnostic_wait_seconds=120, project_opened=False) try: with session: record['startup_ns'] = time.time_ns() print('empty CAD attached', flush=True) session.checkpoint(root / 'checkpoint.json') record['checkpoint_ns'] = time.time_ns() print('empty CAD checkpoint complete', flush=True) record.update(status='completed', finished_ns=time.time_ns(), cad_record=session.record) except BaseException: record.update(status='failed_preserved', failure=traceback.format_exc(), finished_ns=time.time_ns(), cad_record=session.record) raise finally: (out / 'empty-cad-diagnostic.json').write_text(json.dumps(record, ensure_ascii=False, indent=2), encoding='utf-8')