# gui/pages/execute.py — v6 (Phase 3)
# ─────────────────────────────────────────────────────────────────────────────
# FIXES vs v5:
# 1. Tax calc: reads from orchestration_tax_summary DB table — no stdout parsing
# 2. Live view: rendered ONLY when running/done, never bleeds into other pages
# get_conn() is non-blocking with 2s timeout so startup never hangs
# 3. Timeline: static snapshot, no more full-box rerender flicker
# Uses a stable key so Plotly doesn't recreate on every rerun
# 4. Run History: always at bottom, newest first, expandable beauty cards
# 5. Tab 2 just shows start button — live panel is ABOVE tabs
# 6. _parse_summary kept as fallback only; primary source is DB
# ─────────────────────────────────────────────────────────────────────────────
import math, subprocess, time as _time, re as _re, threading, os, signal
from pathlib import Path
from datetime import datetime
import streamlit as st
import pandas as pd
import plotly.graph_objects as go
from gui.components.session_tree import render_session_tree
from gui.config import PROJECT_ROOT, LIVE_API, WF_COLORS, PL, \
INSIGHTS_RULES, DASHBOARD_CFG, STATUS_COLORS, STATUS_ICONS
from gui.connection import get_conn, api_post, api_get
from gui.db import q, q1
from gui.helpers import fl, _human_energy, _human_water, _human_carbon
try:
import requests as _req
_REQUESTS_OK = True
except ImportError:
_REQUESTS_OK = False
try:
import yaml as _yaml
_YAML_OK = True
except ImportError:
_YAML_OK = False
# ══════════════════════════════════════════════════════════════════════════════
# THREAD-SAFE SHARED STORE
# ══════════════════════════════════════════════════════════════════════════════
import threading as _threading
_STORE_LOCK = _threading.Lock()
_STORE: dict = {
"running": False,
"done": False,
"phase": "idle",
"progress": 0.0,
"log": [],
"metrics": {},
"result_rows": [],
"group_id": "",
"run_record": None,
"sessions": [],
"queue": [],
"saved": [],
"stop": False,
"current_cmd": "",
"timeline_snap": None, # cached Plotly fig — only rebuilt when group_id changes
"timeline_gid": "", # which group_id the snap belongs to
}
def _store_get(key, default=None):
with _STORE_LOCK:
return _STORE.get(key, default)
def _store_set(key, value):
with _STORE_LOCK:
_STORE[key] = value
def _store_append(key, value):
with _STORE_LOCK:
_STORE.setdefault(key, []).append(value)
def _store_log(line):
with _STORE_LOCK:
_STORE["log"].append(line)
if len(_STORE["log"]) > 200:
_STORE["log"] = _STORE["log"][-200:]
# ── Config shortcuts ──────────────────────────────────────────────────────────
_STUCK_MINS = DASHBOARD_CFG.get("stuck_run", {}).get("threshold_minutes", 30)
_KILL_ON_RESET = DASHBOARD_CFG.get("stuck_run", {}).get("kill_process", True)
_QUEUE_FILE = PROJECT_ROOT / DASHBOARD_CFG.get("queue", {}).get(
"persist_file", "config/queue_state.yaml")
_MAX_LOG = DASHBOARD_CFG.get("live", {}).get("max_log_lines", 200)
_AUTO_SWITCH = DASHBOARD_CFG.get("live", {}).get("auto_switch_to_analysis", True)
# ══════════════════════════════════════════════════════════════════════════════
# SESSION STATE
# ══════════════════════════════════════════════════════════════════════════════
def _init_state():
with _STORE_LOCK:
snap = dict(_STORE)
for _k in ("running","done","phase","progress","log","metrics",
"result_rows","group_id","run_record","sessions"):
st.session_state[f"ex_{_k}"] = snap.get(_k)
for _k in ("queue","saved"):
if f"ex_{_k}" not in st.session_state:
st.session_state[f"ex_{_k}"] = snap.get(_k, [])
with _STORE_LOCK:
_STORE[_k] = list(st.session_state[f"ex_{_k}"])
if "ex_thread" not in st.session_state:
st.session_state["ex_thread"] = None
# ══════════════════════════════════════════════════════════════════════════════
# QUEUE PERSISTENCE
# ══════════════════════════════════════════════════════════════════════════════
def _save_queue():
if not _YAML_OK:
return
try:
_QUEUE_FILE.parent.mkdir(parents=True, exist_ok=True)
with open(_QUEUE_FILE, "w") as f:
_yaml.dump({"queue": _store_get("queue", [])}, f)
except Exception:
pass
def _load_queue():
if not _YAML_OK or not _QUEUE_FILE.exists():
return
try:
data = _yaml.safe_load(_QUEUE_FILE.read_text()) or {}
loaded = data.get("queue", [])
with _STORE_LOCK:
if loaded and not _STORE.get("queue"):
_STORE["queue"] = loaded
except Exception:
pass
# ══════════════════════════════════════════════════════════════════════════════
# TASK LOADER
# ══════════════════════════════════════════════════════════════════════════════
def _load_tasks() -> tuple:
if _YAML_OK:
yaml_path = PROJECT_ROOT / "config" / "tasks.yaml"
if yaml_path.exists():
try:
raw = _yaml.safe_load(yaml_path.read_text()) or {}
tasks = raw.get("tasks", [])
ids = [t["id"] for t in tasks if "id" in t]
cats = {t["id"]: t.get("category", "") for t in tasks}
names = {t["id"]: t.get("name", t["id"]) for t in tasks}
if ids:
return ids, cats, names
except Exception as e:
st.warning(f"⚠️ Could not parse config/tasks.yaml: {e}")
try:
df = q("SELECT DISTINCT task_name FROM experiments "
"WHERE task_name IS NOT NULL ORDER BY task_name")
ids = df.task_name.tolist() if not df.empty else []
except Exception:
ids = []
if not ids:
st.error("❌ No tasks found in config/tasks.yaml and DB is empty.")
ids = []
return ids, {i: "" for i in ids}, {i: i for i in ids}
# ══════════════════════════════════════════════════════════════════════════════
# STUCK RUN DETECTOR
# ══════════════════════════════════════════════════════════════════════════════
def _show_stuck_runs():
try:
stuck = q(f"""
SELECT exp_id, task_name, provider, group_id,
started_at, runs_completed, runs_total
FROM experiments
WHERE status = 'running'
AND started_at IS NOT NULL
AND (julianday('now') - julianday(started_at)) * 1440 > {_STUCK_MINS}
ORDER BY exp_id
""")
except Exception:
return
if stuck.empty:
return
for _, row in stuck.iterrows():
try:
started = datetime.fromisoformat(str(row.started_at))
mins = int((_time.time() - started.timestamp()) / 60)
elapsed = f"{mins} min ago"
except Exception:
elapsed = str(row.started_at)
st.markdown(
f"
",
unsafe_allow_html=True,
)
if st.button(f"⚡ Force Reset exp_{row.exp_id}", key=f"reset_{row.exp_id}"):
_force_reset_experiment(int(row.exp_id))
st.success(f"exp_{row.exp_id} marked as error. Refresh to confirm.")
st.rerun()
def _force_reset_experiment(exp_id: int):
import sqlite3
try:
conn = sqlite3.connect(str(PROJECT_ROOT / "data" / "experiments.db"))
conn.execute(
"UPDATE experiments SET status='error', error_message='Force reset by UI' "
"WHERE exp_id = ?", (exp_id,))
conn.commit()
conn.close()
except Exception as e:
st.error(f"DB reset failed: {e}")
return
if _KILL_ON_RESET:
try:
import psutil
for proc in psutil.process_iter(["pid", "cmdline"]):
cmdline = " ".join(proc.info.get("cmdline") or [])
if "run_experiment" in cmdline or "test_harness" in cmdline:
os.kill(proc.info["pid"], signal.SIGTERM)
except Exception:
pass
# ══════════════════════════════════════════════════════════════════════════════
# SVG GAUGES
# ══════════════════════════════════════════════════════════════════════════════
def _gauge_svg(value, vmin, vmax, label, unit, color, warn=None, danger=None):
pct = max(0.0, min(1.0, (value - vmin) / max(vmax - vmin, 1e-9)))
angle = -140 + pct * 280
r = 52; cx, cy = 60, 62
ex = cx + r * math.sin(math.radians(angle))
ey = cy - r * math.cos(math.radians(angle))
large = 1 if pct > 0.5 else 0
bx = cx + r * math.sin(math.radians(-140))
by = cy - r * math.cos(math.radians(-140))
ex0 = cx - r * math.sin(math.radians(-140))
ey0 = cy - r * math.cos(math.radians(-140))
nclr = ("#ef4444" if danger and value >= danger
else "#f59e0b" if warn and value >= warn else color)
return (f"
"
f"
")
def _bar_gauge(value, vmax, label, unit, color):
pct = max(0.0, min(100.0, value / max(vmax, 1e-9) * 100))
return (f"
"
f"
"
f"{label}"
f"{value:.0f} {unit}"
f"
"
f"
")
# ══════════════════════════════════════════════════════════════════════════════
# FIX #3: STABLE GANTT — built once per group_id, cached in _STORE
# No more full-box rerender every 1s. Only rebuilds when gid changes.
# ══════════════════════════════════════════════════════════════════════════════
def _gantt_chart_stable(group_id: str):
"""Render timeline only when group_id changes — prevents blinking."""
if not group_id:
return
# Return cached fig if same group
cached_gid = _store_get("timeline_gid", "")
cached_fig = _store_get("timeline_snap", None)
if cached_gid == group_id and cached_fig is not None:
st.plotly_chart(cached_fig, use_container_width=True,
key=f"gantt_{group_id}")
return
# Build fresh
try:
exps = q(f"""
SELECT exp_id, task_name, provider, status, started_at, completed_at
FROM experiments WHERE group_id = '{group_id}' ORDER BY exp_id
""")
except Exception:
return
if exps is None or exps.empty:
return
bars = []
now = datetime.utcnow()
first_start = None
for _, row in exps.iterrows():
try:
s = datetime.fromisoformat(str(row.started_at)) if row.started_at else None
e = datetime.fromisoformat(str(row.completed_at)) if row.completed_at else None
if s is None:
continue
if first_start is None:
first_start = s
end = e if e else now
dur_s = max((end - s).total_seconds(), 0.5)
offset= (s - first_start).total_seconds()
st_db = str(row.get("status", "")).lower()
clr = {"completed": "#3b82f6", "running": "#22c55e",
"failed": "#ef4444", "error": "#ef4444"}.get(st_db, "#4b5563")
bars.append({
"label": f"exp_{row.exp_id} · {row.provider[:3]} · {str(row.task_name)[:14]}",
"start": offset,
"dur": dur_s,
"color": clr,
"status": st_db,
})
except Exception:
continue
if not bars:
return
fig = go.Figure()
for bar in reversed(bars):
fig.add_trace(go.Bar(
name=bar["label"],
x=[bar["dur"]], y=[bar["label"]],
base=bar["start"],
orientation="h",
marker_color=bar["color"],
marker_opacity=0.85,
showlegend=False,
hovertemplate=(f"{bar['label']} {bar['dur']:.1f}s"
f" {bar['status']}"),
))
_pl_g = {k: v for k, v in PL.items() if k != 'margin'}
fig.update_layout(
**_pl_g,
height=max(80 + len(bars) * 26, 120),
barmode="overlay",
xaxis_title="Elapsed (s)",
margin=dict(l=10, r=10, t=20, b=24),
title=dict(text="⏱ Timeline", font=dict(size=9), x=0),
)
_store_set("timeline_snap", fig)
_store_set("timeline_gid", group_id)
st.plotly_chart(fig, use_container_width=True, key=f"gantt_{group_id}")
# ══════════════════════════════════════════════════════════════════════════════
# BACKGROUND RUN THREAD
# ══════════════════════════════════════════════════════════════════════════════
def _thread_run_local(_first_exp: dict, sid: str):
import sqlite3 as _sl3
def _db1(sql):
try:
con = _sl3.connect(str(PROJECT_ROOT / "data" / "experiments.db"), timeout=3)
row = con.execute(sql).fetchone()
con.close()
return row[0] if row else None
except Exception:
return None
def _refresh_gid():
try:
con = _sl3.connect(str(PROJECT_ROOT / "data" / "experiments.db"), timeout=2)
row = con.execute(
"SELECT group_id FROM experiments ORDER BY exp_id DESC LIMIT 1"
).fetchone()
con.close()
gid = row[0] if row else ""
if gid:
_store_set("group_id", gid)
# Invalidate timeline cache so it rebuilds for new group
if gid != _store_get("timeline_gid", ""):
_store_set("timeline_snap", None)
except Exception:
pass
def _poll_telemetry(rid):
if not _REQUESTS_OK or not rid:
return
m = _store_get("metrics", {}).copy()
try:
er = _req.get(f"http://127.0.0.1:8765/api/runs/{rid}/samples/energy", timeout=2).json()
pw = er.get("power", []) if isinstance(er, dict) else []
if pw:
m["pkg_w"] = float(pw[-1].get("pkg_w", 0))
m["core_w"] = float(pw[-1].get("core_w", 0))
except Exception:
pass
try:
cr = _req.get(f"http://127.0.0.1:8765/api/runs/{rid}/samples/cpu", timeout=2).json()
if isinstance(cr, list) and cr:
m["temp_c"] = float(cr[-1].get("package_temp", 0))
m["util"] = float(cr[-1].get("cpu_util_percent", 0))
m["ipc"] = float(cr[-1].get("ipc", 0))
except Exception:
pass
try:
ir = _req.get(f"http://127.0.0.1:8765/api/runs/{rid}/samples/interrupts", timeout=2).json()
if isinstance(ir, list) and ir:
m["irq"] = float(ir[-1].get("interrupts_per_sec", 0))
except Exception:
pass
_store_set("metrics", m)
def _run_one(exp, exp_sid):
_store_set("run_record", {"sid": exp_sid, "name": exp.get("name","?"), "exp": exp})
_store_set("log", [])
_store_set("progress", 0.0)
_store_set("phase", "starting")
_store_set("metrics", {})
_store_set("result_rows", [])
_store_set("group_id", "")
_store_set("stop", False)
_store_set("current_cmd", " ".join(str(x) for x in exp.get("cmd", [])))
cmd = exp.get("cmd", [])
if not cmd:
_store_log(f"[ERROR] No cmd found in experiment config: {exp}")
return -1
last_rid = _db1("SELECT COALESCE(MAX(run_id),0) FROM runs") or 0
line_ctr = 0
rc = -1
try:
proc = subprocess.Popen(
cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
text=True, cwd=str(PROJECT_ROOT), bufsize=1)
for raw in iter(proc.stdout.readline, ""):
line = raw.rstrip()
if not line:
continue
_store_log(line)
if _store_get("stop", False):
proc.terminate()
_store_log("[STOPPED by user]")
proc.wait()
return -2
lo = line.lower()
if "planning" in lo: _store_set("phase", "planning")
elif "execution" in lo: _store_set("phase", "execution")
elif "synth" in lo: _store_set("phase", "synthesis")
elif "rep " in lo or "pair" in lo: _store_set("phase", "running")
if any(k in lo for k in ["complete", "saved", "✅"]):
_store_set("phase", "complete")
for pat in ["rep ", "pair ", "progress:"]:
if pat in lo and "/" in lo:
try:
seg = lo.split(pat)[-1].split("/")
d = int(seg[0].strip().split()[-1])
t = int(seg[1].split()[0])
_store_set("progress", min(d / t, 1.0))
except Exception:
pass
break
_refresh_gid()
line_ctr += 1
if line_ctr % 5 == 0:
nr = _db1("SELECT COALESCE(MAX(run_id),0) FROM runs") or last_rid
if nr > last_rid:
last_rid = nr
_poll_telemetry(last_rid)
proc.wait()
rc = proc.returncode
except Exception as ex:
_store_log(f"[ERROR] {ex}")
rc = -1
_store_set("phase", "complete" if rc == 0 else "error")
_store_set("progress", 1.0)
_refresh_gid()
# FIX #1: Read tax from DB — no more stdout parsing
gid = _store_get("group_id", "")
rows = _load_tax_from_db(gid) if gid else []
_store_set("result_rows", rows)
_store_append("sessions", {
"sid": exp_sid,
"name": exp.get("name", "?"),
"status": "complete" if rc == 0 else "error",
"log": _store_get("log", []).copy(),
"summary_rows": rows,
"ts": _time.strftime("%H:%M:%S"),
"rc": rc,
"group_id": gid,
})
return rc
_store_set("running", True)
_store_set("done", False)
_run_one(_first_exp, sid)
# Safety cap: never drain more than 50 queued experiments per thread start
_max_drain = 50
_drained = 0
while _drained < _max_drain:
if _store_get("stop", False):
break
with _STORE_LOCK:
q_list = _STORE.get("queue", [])
if not q_list:
break
nxt = q_list.pop(0)
_drained += 1
_run_one(nxt, f"ses_{int(_time.time()*1000)}")
_store_set("running", False)
_store_set("done", True)
# ══════════════════════════════════════════════════════════════════════════════
# FIX #1: TAX FROM DB — agentic_energy / linear_energy, always > 1
# ══════════════════════════════════════════════════════════════════════════════
def _load_tax_from_db(group_id: str) -> list:
"""
Load orchestration tax directly from orchestration_tax_summary.
tax_multiplier = agentic_dynamic_uj / linear_dynamic_uj → always >= 1
Falls back to computing from runs table if summary table is empty.
"""
if not group_id:
return []
try:
df = q(f"""
SELECT
el.provider,
el.task_name AS task,
rl.total_energy_uj / 1e6 AS linear_j,
ra.total_energy_uj / 1e6 AS agentic_j,
-- FIX: agentic / linear, always >= 1
CASE WHEN rl.total_energy_uj > 0
THEN CAST(ra.total_energy_uj AS REAL) / rl.total_energy_uj
ELSE 1.0 END AS tax_x,
'' AS ci
FROM orchestration_tax_summary ots
JOIN runs rl ON ots.linear_run_id = rl.run_id
JOIN runs ra ON ots.agentic_run_id = ra.run_id
JOIN experiments el ON rl.exp_id = el.exp_id
WHERE el.group_id = '{group_id}'
ORDER BY ots.comparison_id
""")
if df is not None and not df.empty:
return df.to_dict("records")
except Exception:
pass
# Fallback: compute from runs directly
try:
df2 = q(f"""
SELECT
e.provider,
e.task_name AS task,
AVG(CASE WHEN r.workflow_type='linear'
THEN r.total_energy_uj/1e6 END) AS linear_j,
AVG(CASE WHEN r.workflow_type='agentic'
THEN r.total_energy_uj/1e6 END) AS agentic_j
FROM runs r
JOIN experiments e ON r.exp_id = e.exp_id
WHERE e.group_id = '{group_id}'
GROUP BY e.provider, e.task_name
HAVING linear_j IS NOT NULL AND agentic_j IS NOT NULL
""")
if df2 is not None and not df2.empty:
df2["tax_x"] = df2["agentic_j"] / df2["linear_j"].clip(lower=1e-9)
df2["ci"] = ""
return df2.to_dict("records")
except Exception:
pass
return []
# ══════════════════════════════════════════════════════════════════════════════
# LIVE VIEW WIDGET
# ══════════════════════════════════════════════════════════════════════════════
def _render_live_view():
_running = _store_get('running', False)
_done = _store_get('done', False)
if not (_running or _done):
return
st.session_state.ex_running = _running
st.session_state.ex_done = _done
rec = _store_get('run_record') or {}
phase = _store_get('phase', 'idle')
prog = _store_get('progress', 0.0)
log = _store_get('log', [])
m = _store_get('metrics', {})
gid = _store_get('group_id', '')
pc = {"starting": "#7090b0", "planning": "#f59e0b",
"execution": "#3b82f6", "synthesis": "#a78bfa",
"running": "#22c55e", "complete": "#22c55e",
"error": "#ef4444", "idle": "#3d5570"}.get(phase, "#7090b0")
hdr_col, stop_col = st.columns([5, 1])
cur_cmd = _store_get('current_cmd', '')
hdr_col.markdown(
f"
",
unsafe_allow_html=True)
if _running:
if stop_col.button('⏹ Stop', type='secondary', use_container_width=True,
key='stop_run_btn'):
_store_set('stop', True)
st.warning('Stop signal sent.')
if cur_cmd:
st.markdown(
f"
", unsafe_allow_html=True)
seen = 0
for _ in range(600):
_time.sleep(1)
try:
r = _req.get(f"{base_url}/api/run/status/{session_id}", timeout=6)
data = r.json()
except Exception as e:
status_ph.warning(f"Poll error: {e}"); continue
status = data.get("status", "?")
log = data.get("log", [])
prog = float(data.get("progress", 0))
prog_ph.progress(min(prog, 1.0))
new = log[seen:]; seen = len(log)
for l in new: lines.append(l)
if lines:
html = "".join(
f"
",
unsafe_allow_html=True)
st.markdown("")
_analytics_card(dict(sess, summary_rows=rows))
else:
st.caption("No tax data available for this run.")
# ══════════════════════════════════════════════════════════════════════════════
# FIX #2: NON-BLOCKING get_conn()
# ══════════════════════════════════════════════════════════════════════════════
def _get_conn_safe() -> dict:
"""get_conn with 2s timeout so startup never hangs."""
import concurrent.futures
with concurrent.futures.ThreadPoolExecutor(max_workers=1) as ex:
fut = ex.submit(get_conn)
try:
return fut.result(timeout=2)
except Exception:
return {}
# ══════════════════════════════════════════════════════════════════════════════
# MAIN RENDER
# ══════════════════════════════════════════════════════════════════════════════
def render(ctx: dict):
_init_state()
_load_queue()
st.title("Execute Run")
# FIX #2: non-blocking mode banner
_conn = _get_conn_safe()
if _conn.get("verified"):
_hclr = "#22c55e" if _conn.get("harness") else "#f59e0b"
_hmsg = ("Harness ready — runs execute on lab machine"
if _conn.get("harness") else "Server reachable but harness not loaded")
st.markdown(
f"
"
f"🟢 LIVE MODE · "
f"{_hmsg} "
f"Tunnel: {_conn.get('url','')}
",
unsafe_allow_html=True)
else:
st.markdown(
"
"
"⚫ LOCAL MODE · "
"Runs execute on this machine.
",
unsafe_allow_html=True)
# Queue banner
# FIX: Only sync session_state.ex_queue → _STORE when NOT running.
# If running, the thread owns _STORE["queue"]; syncing overwrites pops done by thread
# and causes infinite re-queuing.
if 'ex_queue' not in st.session_state:
st.session_state.ex_queue = _store_get('queue', [])
elif not _store_get('running', False):
_store_set('queue', list(st.session_state.ex_queue))
qlen = len(st.session_state.ex_queue)
if qlen > 0:
st.markdown(
f"
"
f"⏳ {qlen} experiment{'s' if qlen > 1 else ''} queued"
f"{' · 🔴 run in progress' if st.session_state.ex_running else ''}"
f"
",
unsafe_allow_html=True)
# Live view is now INSIDE tab 2 only — see TAB 2 block below
st.session_state.ex_running = _store_get('running', False)
st.session_state.ex_done = _store_get('done', False)
all_tasks, _cat_map, _name_map = _load_tasks()
tab1, tab2, tab3, tab4 = st.tabs([
"📋 Create & Queue",
"⚡ Live Execution",
"📊 Session Analysis",
"📈 Run History",
])
# ══════════════════════════════════════════════════════════════════════════
# TAB 1 — CREATE & QUEUE
# ══════════════════════════════════════════════════════════════════════════
with tab1:
_show_stuck_runs()
left, right = st.columns([1, 1])
with left:
st.markdown("#### 🔬 Build Experiment")
exp_name = st.text_input("Name", value="My Experiment", key="ex_name")
exp_mode = st.radio("Mode", ["Single (test_harness)", "Batch (run_experiment)"],
horizontal=True, key="ex_mode")
if not all_tasks:
st.error("No tasks available. Please check config/tasks.yaml.")
return
if "Single" in exp_mode:
task_labels = [f"{tid} ({_cat_map.get(tid,'')})" for tid in all_tasks]
h_task_idx = st.selectbox("Task", range(len(all_tasks)),
format_func=lambda i: task_labels[i],
key="h_task_idx")
h_task = all_tasks[h_task_idx]
h_prov = st.selectbox("Provider", ["cloud", "local"], key="h_prov")
h_reps = st.number_input("Repetitions", 1, 100, 3, key="h_reps")
h_country = st.selectbox("Region",
["US","DE","FR","NO","IN","AU","GB","CN","BR"],
format_func=lambda x: {"US":"🇺🇸 US","DE":"🇩🇪 DE","FR":"🇫🇷 FR",
"NO":"🇳🇴 NO","IN":"🇮🇳 IN","AU":"🇦🇺 AU",
"GB":"🇬🇧 GB","CN":"🇨🇳 CN","BR":"🇧🇷 BR"}.get(x, x),
key="h_country")
h_cd = st.number_input("Cool-down (s)", 0, 120, 5, step=5, key="h_cd")
h_save_db = st.checkbox("--save-db", value=True, key="h_savedb")
h_opt = st.checkbox("--optimizer", value=False, key="h_opt")
h_warmup = st.checkbox("--no-warmup", value=False, key="h_warmup")
h_debug = st.checkbox("--debug", value=False, key="h_debug")
cmd = ["python", "-m", "core.execution.tests.test_harness",
"--task-id", h_task, "--provider", h_prov,
"--repetitions", str(int(h_reps)), "--country", h_country,
"--cool-down", str(int(h_cd))]
if h_save_db: cmd.append("--save-db")
if h_opt: cmd.append("--optimizer")
if h_warmup: cmd.append("--no-warmup")
if h_debug: cmd.append("--debug")
meta = {"name": exp_name, "mode": "single", "task": h_task,
"provider": h_prov, "reps": int(h_reps),
"country": h_country, "cmd": cmd}
else:
_b_all = st.checkbox("All tasks", value=False, key="b_all")
if _b_all:
_sel = all_tasks; st.caption(f"All {len(all_tasks)} tasks selected")
else:
_sel = st.multiselect(
"Tasks", all_tasks,
default=all_tasks[:2] if len(all_tasks) >= 2 else all_tasks,
format_func=lambda t: f"{t} ({_cat_map.get(t,'')})",
key="b_task_multi")
b_prov = st.multiselect("Providers", ["cloud", "local"],
default=["cloud"], key="b_prov")
b_reps = st.number_input("Repetitions", 1, 100, 3, key="b_reps")
b_country = st.selectbox("Region",
["US","DE","FR","NO","IN","AU","GB","CN","BR"],
format_func=lambda x: {"US":"🇺🇸 US","DE":"🇩🇪 DE","FR":"🇫🇷 FR",
"NO":"🇳🇴 NO","IN":"🇮🇳 IN","AU":"🇦🇺 AU",
"GB":"🇬🇧 GB","CN":"🇨🇳 CN","BR":"🇧🇷 BR"}.get(x, x),
key="b_country")
b_cd = st.number_input("Cool-down (s)", 0, 120, 5, step=5, key="b_cd")
b_save_db = st.checkbox("--save-db", value=True, key="b_savedb")
b_opt = st.checkbox("--optimizer", value=False, key="b_opt")
b_warmup = st.checkbox("--no-warmup", value=False, key="b_warmup")
prov_arg = ",".join(b_prov) if b_prov else "cloud"
tasks_arg = ",".join(_sel) if _sel else (all_tasks[0] if all_tasks else "")
cmd = ["python", "-m", "core.execution.tests.run_experiment",
"--tasks", tasks_arg, "--providers", prov_arg,
"--repetitions", str(int(b_reps)), "--country", b_country,
"--cool-down", str(int(b_cd))]
if b_save_db: cmd.append("--save-db")
if b_opt: cmd.append("--optimizer")
if b_warmup: cmd.append("--no-warmup")
meta = {"name": exp_name, "mode": "batch", "tasks": _sel,
"providers": b_prov, "reps": int(b_reps),
"country": b_country, "cmd": cmd}
st.code(" \\\n ".join(cmd), language="bash")
c1, c2, c3 = st.columns(3)
if c1.button("💾 Save", use_container_width=True, key="ex_save"):
st.session_state.ex_saved.append(dict(meta))
st.success(f"Saved **{exp_name}**")
if c2.button("▶ Run Now", type="primary", use_container_width=True,
key="ex_run_now"):
if st.session_state.ex_running:
st.warning("A run is already in progress. Queue it instead.")
else:
st.session_state.ex_queue.insert(0, dict(meta))
_save_queue()
st.success("Queued — go to ⚡ Live Execution"); st.rerun()
if c3.button("➕ Queue", use_container_width=True, key="ex_queue_btn"):
st.session_state.ex_queue.append(dict(meta))
_save_queue()
st.success(f"Queued at position {len(st.session_state.ex_queue)}")
with right:
st.markdown("#### 📁 Saved Experiments")
if not st.session_state.ex_saved:
st.caption("No saved experiments yet.")
else:
for i, exp in enumerate(st.session_state.ex_saved):
ea, eb, ec = st.columns([3, 1, 1])
ea.markdown(
f"
",
unsafe_allow_html=True)
if eb.button("▶", key=f"sv_run_{i}", use_container_width=True):
st.session_state.ex_queue.insert(0, dict(exp))
_save_queue(); st.rerun()
if ec.button("🗑", key=f"sv_del_{i}", use_container_width=True):
st.session_state.ex_saved.pop(i); st.rerun()
if st.button("▶▶ Run All Saved", type="primary",
use_container_width=True, key="run_all"):
for e in st.session_state.ex_saved:
st.session_state.ex_queue.append(dict(e))
_save_queue()
st.success(f"Queued {len(st.session_state.ex_saved)} experiments")
st.rerun()
st.divider()
st.markdown("#### ⏳ Queue")
if not st.session_state.ex_queue:
st.caption("Queue is empty.")
else:
for i, exp in enumerate(st.session_state.ex_queue):
qa, qb = st.columns([4, 1])
qa.markdown(
f"
"
f"#{i+1} — {exp['name']}
",
unsafe_allow_html=True)
if qb.button("✕", key=f"q_del_{i}", use_container_width=True):
st.session_state.ex_queue.pop(i)
_save_queue(); st.rerun()
if st.button("🗑 Clear queue", use_container_width=True, key="clear_q"):
st.session_state.ex_queue.clear()
_save_queue(); st.rerun()
# ══════════════════════════════════════════════════════════════════════════
# TAB 2 — LIVE EXECUTION (start button only)
# ══════════════════════════════════════════════════════════════════════════
with tab2:
conn = _get_conn_safe()
if not st.session_state.ex_running and st.session_state.ex_queue:
next_exp = st.session_state.ex_queue[0]
rem = len(st.session_state.ex_queue) - 1
st.markdown(
f"
"
f"▶ Ready: {next_exp['name']}"
f"{' · '+str(rem)+' more queued' if rem > 0 else ''}"
f"
", unsafe_allow_html=True)
if st.button(f"▶ Start — {next_exp['name']}", type="primary",
use_container_width=True, key="start_next"):
exp = st.session_state.ex_queue.pop(0)
_save_queue()
sid = f"ses_{int(_time.time()*1000)}"
if conn.get("verified"):
payload = {
"task_id": exp.get("task", (exp.get("tasks") or [all_tasks[0] if all_tasks else "gsm8k_basic"])[0]),
"provider": exp.get("provider", (exp.get("providers") or ["cloud"])[0]),
"country_code": exp.get("country", "US"),
"repetitions": exp.get("reps", 3),
"cool_down": 5,
"tasks": exp.get("tasks", [exp.get("task", all_tasks[0] if all_tasks else "gsm8k_basic")]),
"providers": exp.get("providers", [exp.get("provider", "cloud")]),
"token": conn.get("token", ""),
}
resp, err = api_post("/api/run/start", payload)
if err:
st.error(f"Remote start failed: {err}")
else:
rsid = resp.get("session_id", "")
st.success(f"✅ Started — session `{rsid}`")
rc, lines, rows = _run_remote(exp, rsid, conn["url"])
record = {"sid": sid, "name": exp["name"],
"status": "complete" if rc == 0 else "error",
"log": lines, "summary_rows": rows,
"ts": _time.strftime("%H:%M:%S")}
st.session_state.ex_sessions.append(record)
if record["status"] == "complete":
_analytics_card(record)
else:
st.session_state.ex_run_record = {"sid": sid, "name": exp["name"], "exp": exp}
# FIX: sync remaining queue to _STORE ONCE before starting thread
# then clear session queue so rerun loop can't re-add items
remaining = list(st.session_state.ex_queue) # already popped first exp above
_store_set('queue', remaining)
st.session_state.ex_queue.clear()
_save_queue()
t = threading.Thread(
target=_thread_run_local, args=(exp, sid), daemon=True)
t.start()
st.session_state.ex_thread = t
st.rerun()
elif not st.session_state.ex_running and not st.session_state.ex_queue:
if not (st.session_state.ex_running or st.session_state.ex_done):
st.info("Queue is empty. Go to 📋 Create & Queue to add experiments.")
if st.session_state.ex_running or st.session_state.ex_done:
_render_live_view()
# ══════════════════════════════════════════════════════════════════════════
# TAB 3 — SESSION ANALYSIS
# ══════════════════════════════════════════════════════════════════════════
with tab3:
from gui.pages.session_analysis import render_session_analysis
st.markdown("### 📊 Session Analysis")
try:
recent = q("""
SELECT
e.group_id,
COUNT(DISTINCT e.exp_id) AS n_exps,
COUNT(DISTINCT r.run_number ||'_'|| CAST(e.exp_id AS TEXT)) AS n_runs,
MAX(e.created_at) AS latest
FROM experiments e
LEFT JOIN runs r ON r.exp_id = e.exp_id
GROUP BY e.group_id
ORDER BY MAX(e.exp_id) DESC
LIMIT 10
""")
except Exception:
recent = pd.DataFrame()
gid_options = recent.group_id.tolist() if not recent.empty else []
if not gid_options:
st.info("No sessions in DB yet. Run an experiment first.")
else:
sel_gid = st.selectbox(
"Select session",
gid_options,
format_func=lambda g: (
f"{g} ({recent[recent.group_id==g].iloc[0].n_exps:.0f} exps, "
f"{recent[recent.group_id==g].iloc[0].n_runs or 0:.0f} runs)"
if not recent[recent.group_id==g].empty else g
),
key="t3_gid_sel"
)
if sel_gid:
render_session_tree(sel_gid, expanded=False, key_suffix="hist")
render_session_analysis(sel_gid)
# ══════════════════════════════════════════════════════════════════════════
# TAB 4 — RUN HISTORY (FIX #5: always below, beautiful cards)
# ══════════════════════════════════════════════════════════════════════════
with tab4:
sessions = list(st.session_state.ex_sessions or [])
if not sessions:
st.markdown(
"
"
"
📭
"
"
"
"No runs yet
"
"
"
"Completed runs will appear here as expandable cards
"
"
",
unsafe_allow_html=True)
else:
# Newest first
sessions_rev = list(reversed(sessions))
n = len(sessions_rev)
st.markdown(
f"
"
f"
Run History
"
f"
{n} run{'s' if n!=1 else ''}
"
f"
",
unsafe_allow_html=True)
for i, sess in enumerate(sessions_rev):
_history_card(sess, idx=i, expanded=(i == 0))