#!/usr/bin/env python3 """Patch iishko gates for minara-pool models (line-based, no escape games).""" import json, py_compile, shutil, time import os import sys MODELS_PATH = os.environ.get("MINARA_MODELS_JSON", "minara_models.json") GATE_SSO = os.environ.get("MINARA_GATE_SSO", "/root/sso_limits.py") GATE_LEGACY = os.environ.get("MINARA_GATE_LEGACY", "/opt/grok-bot/iishko/legacy.py") GATE_TOOL = os.environ.get("MINARA_GATE_TOOL", "/root/toolgate.py") def main(): MODELS = json.load(open(MODELS_PATH, encoding="utf-8")) ts = time.strftime("%Y%m%d-%H%M%S") TAG = " # [MINARA 1106]" line_sso = "MODELS += [m for m in " + repr(MODELS) + " if m not in MODELS]" + TAG line_rank = " models += [m for m in " + repr(MODELS) + " if m not in models]" + TAG line_tool = "ALLOW += [m for m in " + repr(MODELS) + " if m not in ALLOW]" + TAG def insert_after(path, anchor, newline): lines = open(path, encoding="utf-8").read().split("\n") if any("MINARA 1106" in l for l in lines): return "already" idx = [i for i, l in enumerate(lines) if anchor in l] if not idx: return "anchor-missing" shutil.copy2(path, path + ".bak-minara-" + ts) out = lines[:idx[0] + 1] + [newline] + lines[idx[0] + 1:] open(path, "w", encoding="utf-8").write("\n".join(out)) py_compile.compile(path, doraise=True) return "patched" def insert_before(path, anchor, newline): lines = open(path, encoding="utf-8").read().split("\n") if any("MINARA 1106" in l for l in lines): return "already" idx = [i for i, l in enumerate(lines) if anchor in l] if not idx: return "anchor-missing" shutil.copy2(path, path + ".bak-minara-" + ts) out = lines[:idx[0]] + [newline] + lines[idx[0]:] open(path, "w", encoding="utf-8").write("\n".join(out)) py_compile.compile(path, doraise=True) return "patched" def append_line(path, newline): lines = open(path, encoding="utf-8").read().split("\n") if any("MINARA 1106" in l for l in lines): return "already" shutil.copy2(path, path + ".bak-minara-" + ts) out = lines + [newline, ""] open(path, "w", encoding="utf-8").write("\n".join(out)) py_compile.compile(path, doraise=True) return "patched" print("sso_limits:", insert_before(GATE_SSO, "parts = [x.strip()", line_sso)) print("legacy:", insert_after(GATE_LEGACY, "models += IMG_MODELS + IMG_ALWAYS + DISC_MODELS + WUS_MODELS", line_rank)) print("toolgate:", append_line(GATE_TOOL, line_tool)) for p in (GATE_SSO, GATE_LEGACY, GATE_TOOL): n = sum(1 for l in open(p, encoding="utf-8") if "MINARA 1106" in l) print(p, "markers:", n) if __name__ == "__main__": main()