Files
kefu/deploy/recognition-audit-20260916/fix_gateway_guard.py
T
2026-09-21 10:34:06 +08:00

48 lines
2.6 KiB
Python

from pathlib import Path
import hashlib
import json
import shutil
directory = Path('C:/kefu/deploy/recognition-audit-20260916')
manifest = []
for root in (Path('C:/kefu/wechat_rpa'), Path('C:/wechat_rpa')):
path = root / 'model_gateway.py'
raw = path.read_bytes()
text = raw.decode('utf-8').replace('\r\n', '\n')
old = ' def plan(self) -> tuple[list[Outlet], Outlet | None, str, int]:'
assert old in text
text = text.replace(old, ' def plan(self, *, purpose: str = "chat") -> tuple[list[Outlet], Outlet | None, str, int]:', 1)
marker = ' judge = pick(roles.get("judge_id") or "")'
assert marker in text
text = text.replace(marker, ''' if purpose == "guard":
# Internal screenshot classification uses the configured vision role.
# Older installations without that role keep their primary outlet;
# customer-answer fanout and reply scoring cannot judge UI state.
vision = pick(roles.get("vision_id") or "")
return ([vision] if vision is not None else answers[:1], None,
"shadow", int(roles.get("version") or 0))
''' + marker, 1)
old = ''' body = await request.json()
answers, judge, mode, version = catalog.plan()'''
assert old in text
text = text.replace(old, ''' body = await request.json()
purpose = str(body.get("purpose") or "chat")
answers, judge, mode, version = catalog.plan(purpose=purpose)''', 1)
start = text.index(' verdict = await run_judge(', text.index(' results = await asyncio.gather('))
end = text.index('\n\n chosen = usable[0]', start)
old = text[start:end]
text = text[:start] + ''' if purpose == "guard":
verdict = dict(_EMPTY_VERDICT)
else:
''' + '\n'.join(' ' + line for line in old.split('\n')) + text[end:]
backup = root / 'backups/recognition-audit-20260916/ai-context/model_gateway-before-guard-routing.py'
if backup.exists():
raise RuntimeError(f'Refuse to overwrite {backup}')
shutil.copy2(path, backup)
newline = '\r\n' if b'\r\n' in raw else '\n'
path.write_bytes(text.replace('\n', newline).encode('utf-8'))
shutil.copy2(directory / 'test_gateway_guard_audit.py', root / 'test_gateway_guard_audit.py')
manifest.append({'path': str(path), 'backup': str(backup), 'before_sha256': hashlib.sha256(raw).hexdigest(), 'after_sha256': hashlib.sha256(path.read_bytes()).hexdigest()})
(directory / 'gateway-guard-manifest.json').write_text(json.dumps(manifest, ensure_ascii=False, indent=2), encoding='utf-8')
print('Guard vision-role routing updated in both projects.')