#!/usr/bin/env python3
"""Check ComfyUI API queue/history for errors."""
import paramiko, json
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('192.168.178.150', username='fabian', key_filename=r'C:\Users\fabia\.ssh\id_ed25519')

def run(cmd, timeout=30):
    _, stdout, stderr = ssh.exec_command(cmd, timeout=timeout)
    return stdout.read().decode(), stderr.read().decode()

# Queue status
out, _ = run("curl -s http://localhost:8188/queue")
print("=== QUEUE ===")
try:
    q = json.loads(out)
    print(f"Running: {len(q.get('queue_running', []))}")
    print(f"Pending: {len(q.get('queue_pending', []))}")
except:
    print(out[:500])

# History
out, _ = run("curl -s http://localhost:8188/history")
print("\n=== HISTORY ===")
try:
    h = json.loads(out)
    for pid, info in h.items():
        print(f"\nPrompt ID: {pid}")
        status = info.get('status', {})
        print(f"  Status: {status}")
        outputs = info.get('outputs', {})
        for nid, nout in outputs.items():
            print(f"  Node {nid}: {list(nout.keys()) if isinstance(nout, dict) else nout}")
        if not outputs:
            print("  NO OUTPUTS")
except:
    print(out[:2000])

# Check stderr output (nohup might redirect differently)  
out, _ = run("cat /home/fabian/comfyui_err.log 2>/dev/null || echo 'no err log'")
print(f"\n=== STDERR LOG ===\n{out[:2000]}")

# Check full nohup output
out, _ = run("wc -l /home/fabian/comfyui.log 2>/dev/null")
print(f"\n=== LOG LINES: {out.strip()}")

# Check if there are processes actively computing
out, _ = run("bash -c 'top -bn1 | head -20'")
print(f"\n=== TOP ===\n{out}")

ssh.close()
