Files
weval-l99/wevia-antiregression.py
2026-04-13 12:43:21 +02:00

149 lines
6.9 KiB
Python

import os, json, subprocess, time, shutil
LOG = '/var/log/wevia-antiregression.log'
STATUS = '/var/www/html/api/wevia-antiregression-status.json'
fixes = []
issues = []
def lg(m):
with open(LOG,'a') as f: f.write(time.strftime('%H:%M ')+m+chr(10))
def fx(t): fixes.append(t); lg('[FIX] '+t)
# ═══ 1. HOMEPAGE REDIRECT CHECK ═══
# Homepage MUST return 200 with >10KB and NO redirect
try:
r = subprocess.run(['curl','-sk','-L','--max-redirs','3','https://weval-consulting.com/',
'-o','/dev/null','-w','%{http_code}|%{size_download}|%{url_effective}|%{num_redirects}',
'--max-time','10'], capture_output=True, text=True, timeout=15)
parts = r.stdout.strip().split('|')
code,size,url,redirs = int(parts[0]),int(parts[1]),parts[2],int(parts[3])
if code != 200 or size < 5000:
issues.append(f'Homepage: HTTP {code}, {size} bytes (expected 200, >5KB)')
if 'command-center' in url or 'login' in url:
issues.append(f'Homepage REDIRECT to {url}!')
# AUTO-FIX: remove ak_redirect from JS
f = '/var/www/html/weval-faq-fix.js'
os.system('chattr -i '+f)
c = open(f).read()
if 'ak_redirect' in c and 'REMOVED' not in c:
lines = c.split(chr(10))
new = []; skip=False
for L in lines:
if 'Fix 9' in L and 'redirect' in L.lower(): skip=True; new.append('// Fix 9: REMOVED by anti-regression'); continue
if skip and L.strip()=='})();': skip=False; continue
if skip: continue
new.append(L)
open(f,'w').write(chr(10).join(new))
fx('Removed ak_redirect JS from faq-fix.js')
os.system('chattr +i '+f)
if redirs > 0 and 'weval-consulting.com' in url:
pass # OK if final URL is still homepage
except Exception as e:
issues.append(f'Homepage check error: {e}')
# ═══ 2. CHATBOT POST HEALTH ═══
try:
r = subprocess.run(['curl','-sk','-X','POST','https://127.0.0.1/api/weval-ia-fast.php',
'-H','Host: weval-consulting.com','-H','Content-Type: application/json',
'-d',json.dumps({'message':'health check'}),
'--max-time','15'], capture_output=True, text=True, timeout=20)
d = json.loads(r.stdout)
if 'maintenance' in d.get('response','').lower():
issues.append('Chatbot POST returning maintenance (crash)')
# AUTO-FIX: remove duplicate function
fast = open('/var/www/html/api/weval-ia-fast.php').read()
wire = '/var/www/weval/wevia-ia/cognitive-wire.php'
if os.path.exists(wire) and 'function wevia_mirofish_insights' in fast and 'function wevia_mirofish_insights' in open(wire).read():
os.system('chattr -i /var/www/html/api/weval-ia-fast.php')
lines = fast.split(chr(10)); new=[]; skip=False; depth=0
for L in lines:
if not skip and 'function wevia_mirofish_insights($msg)' in L:
skip=True; depth=0; new.append('// mirofish: auto-fixed by anti-regression'); continue
if skip:
depth += L.count('{') - L.count('}')
if depth <= 0 and '}' in L: skip=False
continue
new.append(L)
open('/var/www/html/api/weval-ia-fast.php','w').write(chr(10).join(new))
# Update GOLD
gold = '/opt/wevads/vault/gold-4avr-brain/weval-ia-fast-FINAL.GOLD'
if os.path.exists(os.path.dirname(gold)):
shutil.copy('/var/www/html/api/weval-ia-fast.php', gold)
os.system('chattr +i /var/www/html/api/weval-ia-fast.php')
fx('Removed duplicate mirofish function + updated GOLD')
except Exception as e:
issues.append(f'Chatbot check error: {e}')
# ═══ 3. AUTH REDIRECT LOOP CHECK ═══
try:
js = open('/var/www/html/weval-faq-fix.js').read()
if 'window.location.replace' in js and 'ak_redirect' in js and 'REMOVED' not in js:
issues.append('ak_redirect JS still present (redirect loop risk)')
os.system('chattr -i /var/www/html/weval-faq-fix.js')
lines = js.split(chr(10)); new=[]; skip=False
for L in lines:
if 'Fix 9' in L and 'redirect' in L.lower(): skip=True; new.append('// Fix 9: REMOVED by anti-regression'); continue
if skip and L.strip()=='})();': skip=False; continue
if skip: continue
new.append(L)
open('/var/www/html/weval-faq-fix.js','w').write(chr(10).join(new))
os.system('chattr +i /var/www/html/weval-faq-fix.js')
fx('Removed ak_redirect JS redirect loop')
except Exception as e:
issues.append(f'JS check error: {e}')
# ═══ 4. NGINX SYNTAX CHECK ═══
try:
r = subprocess.run(['nginx','-t'], capture_output=True, text=True, timeout=5)
if 'successful' not in r.stderr:
issues.append(f'Nginx config error: {r.stderr[:50]}')
except: pass
# ═══ 5. PHP SYNTAX CHECK (critical files) ═══
for php in ['/var/www/html/api/weval-ia-fast.php','/var/www/html/api/wevia-action-engine.php',
'/var/www/html/api/wevia-autonomy-controller.php','/var/www/html/api/wevia-auth-agent.php']:
try:
r = subprocess.run(['php','-l',php], capture_output=True, text=True, timeout=5)
if 'No syntax errors' not in r.stdout:
issues.append(f'PHP syntax error: {os.path.basename(php)}')
except: pass
# ═══ 6. DOMAIN CHECK ═══
for d in [
'weval-consulting.com','wevads.weval-consulting.com','monitor.weval-consulting.com',
'ethica.weval-consulting.com','auth.weval-consulting.com','paperclip.weval-consulting.com',
'mirofish.weval-consulting.com','crm.weval-consulting.com','code.weval-consulting.com',
'deerflow.weval-consulting.com','mm.weval-consulting.com','n8n.weval-consulting.com',
'analytics.weval-consulting.com','consent.wevup.app','ethica.wevup.app']:
try:
r = subprocess.run(['curl','-sk','-o','/dev/null','-w','%{http_code}',f'https://{d}/',
'--max-time','5'], capture_output=True, text=True, timeout=8)
if r.stdout not in ['200','302','301']:
issues.append(f'Domain {d}: HTTP {r.stdout}')
except: pass
# ═══ SAVE ═══
status = {
'timestamp': time.strftime('%Y-%m-%d %H:%M:%S'),
'healthy': len(issues) == 0,
'issues': issues,
'fixes': fixes,
'issues_count': len(issues),
'fixes_count': len(fixes)
}
open(STATUS,'w').write(json.dumps(status, indent=2))
# Email on issues
if issues:
body = 'WEVIA Anti-Regression - ' + time.strftime('%Y-%m-%d %H:%M') + chr(10)*2
for i in issues: body += '- ' + i + chr(10)
if fixes:
body += chr(10) + 'Auto-fixes applied:' + chr(10)
for f in fixes: body += ' + ' + f + chr(10)
os.system(f'echo "{body}" | mail -s "[WEVIA REGRESSION] {len(issues)} issues" ymahboub@weval-consulting.com 2>/dev/null')
lg(f'AntiReg: {len(issues)} issues, {len(fixes)} fixes')
print(f'AntiReg: {len(issues)} issues | {len(fixes)} fixes | {"HEALTHY" if not issues else "ISSUES"}')