"""Background Phase 10 operation controller.""" from __future__ import annotations import threading from pathlib import Path from typing import Callable from ..config_loader import load_config from ..phase10_cli import generate_protocol,run_pilot_report,run_presentation,run_quality from ..phase11_cli import run_diagnostics from ..phase12_cli import assets,final_report,progress,snapshot,workflow class OperationsController: def __init__(self)->None:self._thread:threading.Thread|None=None;self._finished:Callable[[list[Path]],None]|None=None;self._error:Callable[[str],None]|None=None def run(self,operation:str,config_path:Path,logs:Path,analysis:Path,output:Path)->None: if self._thread and self._thread.is_alive():raise RuntimeError("An operation is already running") self._thread=threading.Thread(target=self._worker,args=(operation,config_path,logs,analysis,output),daemon=True,name="phase10-worker");self._thread.start() def _worker(self,operation:str,config_path:Path,logs:Path,analysis:Path,output:Path)->None: try: c=load_config("config/default.yaml",config_path) if operation=="main-progress":paths=progress(c,logs,analysis,output) elif operation=="snapshot":paths=snapshot(c,config_path,output) elif operation=="final-report":paths=final_report(c,analysis,output) elif operation=="presentation-assets":paths=assets(c,analysis,output) elif operation=="final-analysis": result=workflow(c,logs,analysis,output);paths=[Path(x) for x in result.output_files]+[output/"final_workflow_result.json"] elif operation=="diagnostics":paths=run_diagnostics(c,analysis,logs,output) elif operation=="quality":paths=run_quality(c,logs,analysis,output) elif operation=="pilot":paths=[run_pilot_report(c,analysis,output/"pilot_report.md",logs)] elif operation=="presentation":paths=[run_presentation(c,analysis,output/"midterm_summary.md",logs)] else:paths=generate_protocol(c,output/"experiment_protocol.md",output/"experiment_protocol.yaml") if self._finished:self._finished(paths) except Exception as exc: if self._error:self._error(str(exc)) def set_finished_callback(self,callback:Callable[[list[Path]],None])->None:self._finished=callback def set_error_callback(self,callback:Callable[[str],None])->None:self._error=callback