import os
import sys

lab = r"C:\Users\intel\AppData\Local\OptimusRuntime\Labs\AR74\12_ar88_authority_operational_closure"
candidate_ws = os.path.join(lab, "candidate_workspace")

guard_file = os.path.join(candidate_ws, "app", "remediation", "self_repair", "promotion_guard.py")

with open(guard_file, "r", encoding="utf-8") as f:
    content = f.read()

# Check FORBIDDEN_EXTENSIONS and CRITICAL_FILES
old_forbidden = """FORBIDDEN_EXTENSIONS: Tuple[str, ...] = (
    ".key",
    ".pem",
    ".env",
)"""

new_forbidden = """FORBIDDEN_EXTENSIONS: Tuple[str, ...] = (
    ".key",
    ".pem",
    ".env",
    ".db",
    ".sqlite",
    ".sqlite3",
    ".db-wal",
    ".db-shm",
)"""

old_crit_files = """CRITICAL_FILES: Set[str] = {
    "artifacts/brain_host/brain_host_tls.py",
    "artifacts/brain_host/brain_adapter.py",
    "new_mission_dispatcher.py",
    "artifacts/brain_host/capability_mission_planner.py",
    "artifacts/brain_host/generic_developer_executor.py",
    "tools/start_brain_host.ps1",
}"""

new_crit_files = """CRITICAL_FILES: Set[str] = {
    "artifacts/brain_host/brain_host_tls.py",
    "artifacts/brain_host/brain_adapter.py",
    "new_mission_dispatcher.py",
    "artifacts/brain_host/capability_mission_planner.py",
    "artifacts/brain_host/generic_developer_executor.py",
    "tools/start_brain_host.ps1",
    "continuity.db",
    "continuity.db-wal",
    "continuity.db-shm",
}"""

old_crit_pref = """CRITICAL_PREFIXES: Tuple[str, ...] = (
    "app/remediation/self_repair/",
    "runtime_v1/foundation/",
    "runtime_v1/foundation/providers/",
    "artifacts/brain_host/",
    "config/",
)"""

new_crit_pref = """CRITICAL_PREFIXES: Tuple[str, ...] = (
    "app/remediation/self_repair/",
    "runtime_v1/foundation/",
    "runtime_v1/foundation/providers/",
    "artifacts/brain_host/",
    "config/",
    "teitan_runtime/",
)"""

if old_forbidden in content and old_crit_files in content and old_crit_pref in content:
    content = content.replace(old_forbidden, new_forbidden)
    content = content.replace(old_crit_files, new_crit_files)
    content = content.replace(old_crit_pref, new_crit_pref)
    with open(guard_file, "w", encoding="utf-8") as f:
        f.write(content)
    print("SUCCESS: promotion_guard.py updated with database, continuity, and teitan_runtime protections!")
else:
    print("ERROR: blocks not matched in promotion_guard.py")

