Files
aigc-home/scripts/verify-hel730.py
T

119 lines
6.7 KiB
Python

"""Reproduce HEL-730 on an isolated database and HTTP fake, never on a user server.
Run from repo root: PYTHONPATH=calliope-backend/src:calliope-backend python3 scripts/verify-hel730.py
Optional: PLAYWRIGHT_MODULE=/absolute/path/to/playwright/index.mjs.
"""
from __future__ import annotations
import asyncio
import hashlib
import json
import os
from pathlib import Path
import socket
import subprocess
import threading
import time
import uuid
import httpx
import uvicorn
from PIL import Image
from calliope.config import settings
import calliope.config as config_module
from calliope.db import migrate_db
from calliope.main import create_app
from tests.test_studio_generation import fake_comfy, PNG_1PX
ROOT = Path(__file__).resolve().parents[1]
OUT = ROOT.parent / 'hel730-evidence'
OUT.mkdir(exist_ok=True)
DATA = OUT / ('isolated-data-'+uuid.uuid4().hex[:8])
DATA.mkdir(exist_ok=True)
settings.data_dir = DATA
settings.assets_dir = DATA / 'assets'
settings.dry_run = False
settings.queue_poll_interval_sec = .5
settings.queue_poll_timeout_sec = 10
settings.llm_profiles = []
settings.llm_active_id = None
settings.comfyui_endpoints = []
settings.comfyui_active_id = None
config_module.CONFIG_FILE = DATA / 'isolated-config.json'
# Configuration and queue data are local to this evidence run.
asyncio.run(migrate_db(settings.db_path))
fake_generator = fake_comfy.__wrapped__()
fake = next(fake_generator)
sock = socket.socket()
sock.bind(('127.0.0.1', 0))
port = sock.getsockname()[1]
server = uvicorn.Server(uvicorn.Config(create_app(ROOT/'calliope-web'/'build'), log_level='warning'))
thread = threading.Thread(target=lambda: server.run(sockets=[sock]), daemon=True)
thread.start()
try:
with httpx.Client(base_url=f'http://127.0.0.1:{port}', timeout=30) as client:
for _ in range(200):
if server.started: break
time.sleep(.05)
assert server.started
def request(method, url, **kwargs):
r = client.request(method, url, **kwargs)
assert r.is_success, (url, r.status_code, r.text)
return r.json()
endpoint = request('POST','/api/comfyui/endpoints',json={'name':'HEL-730 本机隔离假服务','base_url':f'http://127.0.0.1:{fake.server_address[1]}','auth_type':'none'})['endpoints'][0]
scan = request('POST','/api/workflows/import-scan',json={'root':str(ROOT/'example_ComfyUI_workflows'),'dry_run':True})
(OUT/'scan.json').write_text(json.dumps(scan,ensure_ascii=False,indent=2))
assert request('GET','/api/workflows?view=meta') == []
candidates = [f for f in scan['files'] if f.get('importable') and f.get('suggested',{}).get('usage') == 'text2image' and 'image' in f.get('suggested',{}).get('media_out',[])]
assert candidates, 'No convertible UI-format workflow in the real directory'
# Prefer text-only inputs so the fake run does not need synthetic user reference media.
candidates.sort(key=lambda f: (bool(f.get('suggested',{}).get('media_in')),f.get('source_path','')))
chosen = candidates[0]
path = ROOT/'example_ComfyUI_workflows'/chosen['source_path']
graph = json.loads(path.read_text())
convert = request('POST','/api/workflows/convert',json={'files':[{'source_path':chosen['source_path'],'workflow_json':graph}],'include_workflows':True})
(OUT/'convert.json').write_text(json.dumps(convert,ensure_ascii=False,indent=2))
imported = request('POST','/api/workflows/import-batch',json={'files':[{'source_path':chosen['source_path'],'workflow_json':graph}]})
(OUT/'import.json').write_text(json.dumps(imported,ensure_ascii=False,indent=2))
rows = request('GET','/api/workflows')
assert len(rows) == 1
wf = rows[0]
wid = wf['id']
outputs = wf.get('output_map') or [{'nodeId':o['nodeId'],'kind':o.get('kind') or 'image'} for o in wf['output_schema'] if o.get('kind') in ('image','video')]
assert outputs, wf
wf = request('PATCH',f'/api/workflows/{wid}',json={'usage':wf.get('usage') or 'text2image','usage_source':'user','output_map':outputs,'output_map_source':'user','endpoint_id':endpoint['id'],'is_enabled':True})
fake.forced_output_node = outputs[0]['nodeId']
fake.outputs_key = 'videos' if outputs[0]['kind']=='video' else 'images'
fake.out_name = 'hel730-isolated.png'
fake.object_info = {n['class_type']:{'input':{'required':{},'optional':{}},'output':[]} for n in wf['workflow_json'].values() if isinstance(n,dict) and n.get('class_type')}
check = request('POST',f'/api/workflows/{wid}/check',json={'endpoint_id':endpoint['id'],'refresh':True})
values = {i['nodeId']:i.get('defaultValue','HEL-730 fake-service verification') for i in wf['input_schema'] if i['kind'] in ('text','textarea','number','select')}
result = request('POST',f'/api/workflows/{wid}/test',json={'input_values':values})
jobid = result['job']['id']
deadline = time.monotonic()+30
while True:
job = request('GET',f'/api/jobs/{jobid}')
if job['status'] not in ('pending','running'): break
assert time.monotonic()<deadline, job
time.sleep(.1)
assert job['status']=='done', job
assert job['origin']=='workflow_test'
assert job['output_paths']
output = Path(job['output_paths'][0])
assert output.read_bytes()==PNG_1PX
assert '_workflow_test' in str(output)
history = request('GET','/api/playground/jobs')
assert all(j['id']!=jobid for j in history)
meta = request('GET','/api/workflows?view=meta')
assert meta[0]['is_enabled'] and meta[0]['usage'] and not meta[0]['needs_mapping']
summary = {'source_path':chosen['source_path'],'source_sha256':hashlib.sha256(path.read_bytes()).hexdigest(),'ui_format_files':scan['total'],'directory_json_files':len(list((ROOT/'example_ComfyUI_workflows').rglob('*.json'))),'imported':len(meta),'workflow_id':wid,'mapping':outputs,'check':check,'test_job_id':jobid,'test_status':job['status'],'output_sha256':hashlib.sha256(output.read_bytes()).hexdigest(),'output_isolated':True,'excluded_from_creation_history':True,'fake_prompt_requests':len(fake.prompts),'service_url':f'http://127.0.0.1:{port}'}
(OUT/'closed-loop.json').write_text(json.dumps(summary,ensure_ascii=False,indent=2))
# This server only offers a fake 1px response. The UI screenshot must not pretend it is artwork.
env = {**os.environ,'HEL730_URL':f'http://127.0.0.1:{port}','HEL730_OUT':str(OUT),'HEL730_WORKFLOW':str(wid)}
subprocess.run(['node',str(ROOT/'scripts'/'verify-hel730.mjs')],env=env,check=True)
print(json.dumps(summary,ensure_ascii=False,indent=2))
finally:
server.should_exit = True
thread.join(timeout=10)
sock.close()
fake_generator.close()