"""Gradio front end for ChronoSwarm. Deploy notes ------------ * Put the `chronoswarm/` package directory at the root of the Space, next to this file, so `import chronoswarm` resolves without an install step. * The fallback executor is used deliberately (`force_fallback=True`). It is not a degraded mode - `test_fallback_executor_matches_langgraph` asserts identical results - but unlike LangGraph it passes and returns the *same* state dict, so the nodes mutate it in place and this file can poll `state["log"]` from another thread to stream progress. With real LangGraph the outer reference never updates and the log would stay empty until the run finished. * No API key is set, so every LLM-backed agent runs its deterministic shadow. The swarm is fully functional this way; see `chronoswarm/llm.py`. * Spaces storage is ephemeral. Runs go to a temp directory and are handed back as a zip; nothing is expected to survive a restart. """ from __future__ import annotations import html import os import shutil import socket import sys import tempfile import threading import traceback from pathlib import Path ROOT = Path(__file__).resolve().parent PACKAGE_ROOT = ROOT / "chronoswarm" if PACKAGE_ROOT.exists(): sys.path.insert(0, str(PACKAGE_ROOT)) import gradio as gr from chronoswarm.dynamics import PROCESS_LIST from chronoswarm.emit import emit_run from chronoswarm.gallery import write_gallery from chronoswarm.graph import build_graph, initial_state from chronoswarm.schema import SwarmConfig, record_summary # Free CPU Spaces are 2 vCPU. These ceilings keep a run inside a couple of # minutes; the defaults keep it under one. MAX_GENERATIONS = 14 MAX_POPULATION = 44 POLL_SECONDS = 0.4 def embed_gallery(path: str) -> str: """Inline the gallery in an iframe via `srcdoc`. gr.HTML does not reliably execute inline