88 lines
2.7 KiB
Python
88 lines
2.7 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""Патч гейтов iishko под модели DeepAI Code (idempotent, с бэкапами и ast-check).
|
||
|
||
python3 /opt/dpcode/patch_gates.py --apply
|
||
"""
|
||
import ast
|
||
import os
|
||
import re
|
||
import shutil
|
||
import sys
|
||
import time
|
||
|
||
MODELS = [
|
||
"glm-5.3-flash", "deepseek-v4.1-flash", "mimo-2.6-flash", "qwen-3.8-flash",
|
||
"minimax-m3.1-flash", "muse-ember", "solar-mini-4", "gpt-6-luna",
|
||
"mimo-2.6-pro", "glyph-cluster", "deepai-cyber",
|
||
"claude-opus-5-5", "claude-sonnet-5-5", "claude-fable-5-2", "gpt-6.1-sol",
|
||
"gpt-6.1-terra", "gpt-6-astra", "gemini-4-argon", "gemini-4-hydrogen",
|
||
"gemini-4-carbon", "glm-5.3", "kimi-k3", "kimi-k3.1-pro", "minimax-m3",
|
||
"muse-spark-1.3", "muse-wonder", "qwen-3.8-max-prime",
|
||
]
|
||
MARK = "# [DPA 1114]"
|
||
SSL = "/root/sso_limits.py"
|
||
TG = "/root/toolgate.py"
|
||
apply = "--apply" in sys.argv
|
||
|
||
|
||
def backup(path):
|
||
ts = time.strftime("%Y%m%dT%H%M%S")
|
||
dst = f"{path}.bak-dpa-{ts}"
|
||
shutil.copy2(path, dst)
|
||
return dst
|
||
|
||
|
||
def patch_sso():
|
||
src = open(SSL, encoding="utf-8").read()
|
||
if MARK in src:
|
||
print("sso_limits: already patched")
|
||
return True
|
||
lines = src.splitlines()
|
||
# точка вставки: после последней строки MODELS += ... (до исполняемого блока apply)
|
||
idx = 0
|
||
for i, ln in enumerate(lines):
|
||
if re.match(r"^\s*MODELS\s*(\+=|=)", ln):
|
||
idx = i + 1
|
||
ins = "MODELS += [m for m in %r if m not in MODELS] %s" % (MODELS, MARK)
|
||
lines.insert(idx, ins)
|
||
out = "\n".join(lines) + "\n"
|
||
try:
|
||
ast.parse(out)
|
||
except SyntaxError as e:
|
||
print("sso_limits: SYNTAX ERROR, не применяю:", e)
|
||
return False
|
||
b = backup(SSL)
|
||
open(SSL, "w", encoding="utf-8").write(out)
|
||
print("sso_limits patched at line", idx, "backup", b)
|
||
return True
|
||
|
||
|
||
def patch_toolgate():
|
||
src = open(TG, encoding="utf-8").read()
|
||
if MARK in src:
|
||
print("toolgate: already patched")
|
||
return True
|
||
m = re.search(r"^ALLOW\s*=\s*\[", src, re.M)
|
||
if not m:
|
||
print("toolgate: ALLOW literal не найден")
|
||
return False
|
||
ins = "\n" + ",\n".join(' "%s"' % x for x in MODELS) + ", " + MARK + "\n"
|
||
pos = m.end()
|
||
out = src[:pos] + ins + src[pos:]
|
||
try:
|
||
ast.parse(out)
|
||
except SyntaxError as e:
|
||
print("toolgate: SYNTAX ERROR, не применяю:", e)
|
||
return False
|
||
b = backup(TG)
|
||
open(TG, "w", encoding="utf-8").write(out)
|
||
print("toolgate patched, backup", b)
|
||
return True
|
||
|
||
|
||
if not apply:
|
||
print("dry-run: добавил бы", len(MODELS), "моделей в", SSL, "и", TG)
|
||
sys.exit(0)
|
||
ok1 = patch_sso()
|
||
ok2 = patch_toolgate()
|
||
print("RESULT", "sso=", ok1, "toolgate=", ok2) |