chronaxis.fast.cli
源码基线:8904c2f744951857659abd40aaa3535d57faf53c
1"""fast单会话及独立批量命令的JSON响应;静态命令不读取报文文件。"""23import json4import sys56from .can_io import _prepare7from .config import compile_config8from .run import run_prepared9from ..planning.checks import PlanValidationError101112def execute(args):13 """参数为 CLI 已解析的配置路径和动作。成功返回 0,配置或执行失败返回非零。"""14 try:15 if args.fast_command == 'batch':16 return _batch(args)17 from .session import effective_spec, _worker_process_limit18 spec = effective_spec(compile_config(args.config,_with_database=True), getattr(args,'process_count',None))19 database = spec.pop('_database')20 if args.fast_command != 'run':21 _prepare(spec,database,_programs_checked=True)22 except (ValueError, OSError, TypeError) as error:23 details = {'valid':False,'errors':[item.to_dict() for item in error.diagnostics]24 if isinstance(error,PlanValidationError) else [{'message':str(error)}]}25 if getattr(error,'blocked_checks',()):26 details['blocked_checks'] = error.blocked_checks27 print(json.dumps(details,ensure_ascii=False),file=sys.stderr)28 return 229 if args.fast_command == 'run':30 try:31 path = run_prepared(spec,args.output,engine='base_hpc_v1',database=database,_programs_checked=True)32 except (ValueError,OSError,RuntimeError,ArithmeticError) as error:33 print(json.dumps({'errors':[{'message':str(error)}]},ensure_ascii=False),file=sys.stderr)34 return 135 response = {'manifest_path':str(path)}36 elif args.fast_command == 'explain':37 response = dict(engine='base_hpc_v1', file_policy='single_continuous_session',38 worker_process_limit=_worker_process_limit(spec['process_count'], len(spec['inputs'])),39 error_policy='fail_session', evaluation='current_message_before_direction',40 validity='only_current_message', **spec)41 else:42 response = dict(valid=True,engine='base_hpc_v1')43 if args.fast_command != 'run':44 # fast内联时间归并不创建工作进程,不能套用普通读取的time_order判断。45 from ..process_capabilities import managed_worker_warnings46 hints = managed_worker_warnings(47 _worker_process_limit(spec['process_count'], len(spec['inputs'])) > 0, '本fast计划')48 if hints:49 response['warnings'] = hints50 print(json.dumps(response,ensure_ascii=False,allow_nan=False))51 return 0525354def _batch(args):55 """执行一份独立文件清单。CLI 保留实际报告位置,失败和取消返回非零。"""56 from .batch import run_batch57 try:58 path = run_batch(args.config,args.output,total=args.total,process_count=getattr(args,'process_count',None))59 except BaseException as exc:60 report = getattr(exc,'batch_report',None)61 if report is None:62 raise63 try:64 print(json.dumps({'status':report.status,'report_path':str(report.report_path),65 'error':str(exc)},ensure_ascii=False))66 finally:67 report.close()68 return 169 data = json.loads(path.read_text(encoding='utf-8'))70 print(json.dumps({'status':data['status'],'report_path':str(path)},ensure_ascii=False))71 return 0 if data['status']=='completed' else 130 if data['status']=='cancelled' else 172