from pathlib import Path

bh_path = Path(r"C:\Users\intel\AppData\Local\OptimusRuntime\Labs\AR74\12_ar88_authority_operational_closure\candidate_workspace\artifacts\brain_host\brain_host_tls.py")
content = bh_path.read_text(encoding="utf-8")

old_sec = """                                    dispatch_live_shadow_hook(_snap, async_dispatch=True)
                                except Exception as _ex_shadow_hook:
                                    logger.error(f"[BrainHost] Live shadow hook dispatch error: {_ex_shadow_hook}", exc_info=True)"""

new_sec = """                                    logger.info("[BrainHost] Dispatched live shadow hook for task %s, result calling dispatch...", tid)
                                    # Forward direct in-process reference to OPERATIONAL_TASKS so hook updates live dictionary directly
                                    _snap["operational_tasks"] = OPERATIONAL_TASKS
                                    dispatch_live_shadow_hook(_snap, async_dispatch=True)
                                except Exception as _ex_shadow_hook:
                                    logger.error(f"[BrainHost] Live shadow hook dispatch error: {_ex_shadow_hook}", exc_info=True)"""

assert old_sec in content, "old_sec not found"
content = content.replace(old_sec, new_sec, 1)

old_sec_exc = """                                    dispatch_live_shadow_hook(_snap, async_dispatch=True)
                                except Exception as _ex_shadow_hook:
                                    logger.error(f"[BrainHost] Live shadow hook exception dispatch error: {_ex_shadow_hook}", exc_info=True)"""

new_sec_exc = """                                    logger.info("[BrainHost] Dispatched live shadow hook on exception for task %s...", tid)
                                    _snap["operational_tasks"] = OPERATIONAL_TASKS
                                    dispatch_live_shadow_hook(_snap, async_dispatch=True)
                                except Exception as _ex_shadow_hook:
                                    logger.error(f"[BrainHost] Live shadow hook exception dispatch error: {_ex_shadow_hook}", exc_info=True)"""

assert old_sec_exc in content, "old_sec_exc not found"
content = content.replace(old_sec_exc, new_sec_exc, 1)

bh_path.write_text(content, encoding="utf-8")
print("SUCCESS: Explicitly passed operational_tasks to live_shadow_hook in brain_host_tls.py!")

