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

67 lines
2.6 KiB
Python

from pathlib import Path
import hashlib
import json
import shutil
audit=Path(__file__).resolve().parent
changes=[]
for root in (Path('C:/wechat_rpa'),Path('C:/kefu/wechat_rpa')):
path=root/'backend_client.py'
before=path.read_bytes()
text=path.read_text(encoding='utf-8')
old=''' "gateway": {"enabled": False, "url": ""},
'''
new=''' "gateway": {"enabled": False, "url": ""},
# Both load/save use this schema as a whitelist; synced counts must survive it.
"knowledge_summary": {},
'''
assert text.count(old)==1
text=text.replace(old,new)
old='def load_settings() -> dict[str, Any]:\n'
new='''def _normalize_knowledge_summary(value: Any) -> dict[str, Any]:
if not isinstance(value, dict) or not value:
return {}
try:
raw_count = value.get("published", 0)
count = 0 if isinstance(raw_count, bool) else max(0, int(raw_count))
except (TypeError, ValueError, OverflowError):
count = 0
return {
**value,
"published": count,
"enabled": value.get("enabled") is True,
"updated_at": value.get("updated_at") if isinstance(value.get("updated_at"), str) else "",
}
'''+old
assert text.count(old)==1
text=text.replace(old,new)
old=''' settings.update({key: saved[key] for key in settings if key in saved})
'''
new=old+''' settings["knowledge_summary"] = _normalize_knowledge_summary(settings.get("knowledge_summary"))
'''
assert text.count(old)==1
text=text.replace(old,new)
old=''' data.update({key: settings[key] for key in data if key in settings})
'''
new=old+''' data["knowledge_summary"] = _normalize_knowledge_summary(data.get("knowledge_summary"))
'''
assert text.count(old)==1
text=text.replace(old,new)
old=''' settings["knowledge_summary"] = response["knowledge"]
'''
new=''' settings["knowledge_summary"] = _normalize_knowledge_summary(response["knowledge"])
'''
assert text.count(old)==1
text=text.replace(old,new)
backup=root/'backups/recognition-audit-20260916'/path.name
backup.parent.mkdir(parents=True,exist_ok=True)
assert not backup.exists(), str(backup)
backup.write_bytes(before)
path.write_text(text,encoding='utf-8')
shutil.copy2(audit/'test_knowledge_cache_audit.py',root/'test_knowledge_cache_audit.py')
changes.append({'path':str(path),'backup':str(backup),'before_sha256':hashlib.sha256(before).hexdigest(),'after_sha256':hashlib.sha256(path.read_bytes()).hexdigest()})
(audit/'knowledge-cache-changes.json').write_text(json.dumps(changes,indent=2),encoding='utf-8')
print(json.dumps(changes))