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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import time
|
| 2 |
+
import uuid
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
from core.config import SimConfig
|
| 6 |
+
from core.sim import init_world, tick, svg_render, raycast_pov
|
| 7 |
+
from core.kpi import compute_kpis
|
| 8 |
+
from core.export import export_run
|
| 9 |
+
|
| 10 |
+
TITLE = "ZEN Orchestrator Sandbox — Business-Grade Agent Orchestra Simulator"
|
| 11 |
+
|
| 12 |
+
def new_run_id() -> str:
|
| 13 |
+
# Stable + readable enough for exports
|
| 14 |
+
return f"{int(time.time())}_{uuid.uuid4().hex[:8]}"
|
| 15 |
+
|
| 16 |
+
def status_text(w):
|
| 17 |
+
k = compute_kpis(w)
|
| 18 |
+
lines = [
|
| 19 |
+
f"run_id={w.run_id} | seed={w.seed}",
|
| 20 |
+
f"day={k['day']} tick={k['tick']} outage={k['outage_active']}",
|
| 21 |
+
f"spend=${k['spend_usd']} | llm_calls={k['llm_calls']} | tool_calls={k['tool_calls']}",
|
| 22 |
+
f"done={k['done']} overdue={k['overdue']} sla_rate={k['sla_rate']} cost/done=${k['cost_per_done']}",
|
| 23 |
+
]
|
| 24 |
+
if w.ledger.alerts:
|
| 25 |
+
lines.append(f"ALERT: {w.ledger.alerts[-1]}")
|
| 26 |
+
return "\n".join(lines)
|
| 27 |
+
|
| 28 |
+
def ledger_text(w):
|
| 29 |
+
# CFO-friendly snapshot
|
| 30 |
+
top_agents = sorted(w.ledger.spend_by_agent.items(), key=lambda kv: -kv[1])[:6]
|
| 31 |
+
top_models = sorted(w.ledger.spend_by_model.items(), key=lambda kv: -kv[1])[:6]
|
| 32 |
+
lines = []
|
| 33 |
+
lines.append("Ledger Snapshot")
|
| 34 |
+
lines.append(f"Spend USD: {w.ledger.spend_usd:.6f}")
|
| 35 |
+
lines.append(f"Tokens prompt: {w.ledger.tokens_prompt} | completion: {w.ledger.tokens_completion} | cached: {w.ledger.tokens_cached_prompt}")
|
| 36 |
+
lines.append(f"Latency total ms: {w.ledger.latency_ms_total} | LLM calls: {w.ledger.llm_calls} | Tool calls: {w.ledger.tool_calls}")
|
| 37 |
+
lines.append("\nSpend by agent:")
|
| 38 |
+
for a, v in top_agents:
|
| 39 |
+
lines.append(f"- {a}: ${v:.6f}")
|
| 40 |
+
lines.append("\nSpend by model tier:")
|
| 41 |
+
for m, v in top_models:
|
| 42 |
+
lines.append(f"- {m}: ${v:.6f}")
|
| 43 |
+
return "\n".join(lines)
|
| 44 |
+
|
| 45 |
+
def ui_events_text(w):
|
| 46 |
+
return "\n".join(w.ui_events[-20:])
|
| 47 |
+
|
| 48 |
+
def rebuild(w):
|
| 49 |
+
arena = svg_render(w)
|
| 50 |
+
pov = raycast_pov(w, w.pov)
|
| 51 |
+
status = status_text(w)
|
| 52 |
+
tasks_table = w.tasks.to_compact_table(w.step, limit=14)
|
| 53 |
+
ledger = ledger_text(w)
|
| 54 |
+
events = ui_events_text(w)
|
| 55 |
+
return arena, pov, status, tasks_table, ledger, events
|
| 56 |
+
|
| 57 |
+
def apply_settings(seed, minutes_per_tick, budget_soft, budget_hard, initial_tasks, new_tasks_per_day, rework_base):
|
| 58 |
+
cfg = SimConfig(
|
| 59 |
+
minutes_per_tick=int(minutes_per_tick),
|
| 60 |
+
budget_usd_soft=float(budget_soft),
|
| 61 |
+
budget_usd_hard=float(budget_hard),
|
| 62 |
+
initial_tasks=int(initial_tasks),
|
| 63 |
+
new_tasks_per_day=int(new_tasks_per_day),
|
| 64 |
+
rework_probability_base=float(rework_base),
|
| 65 |
+
)
|
| 66 |
+
rid = new_run_id()
|
| 67 |
+
w = init_world(seed=int(seed), run_id=rid, config=cfg)
|
| 68 |
+
return (*rebuild(w), w)
|
| 69 |
+
|
| 70 |
+
def run_n_ticks(w, n):
|
| 71 |
+
n = max(1, int(n))
|
| 72 |
+
for _ in range(n):
|
| 73 |
+
if w.done:
|
| 74 |
+
break
|
| 75 |
+
tick(w)
|
| 76 |
+
return (*rebuild(w), w)
|
| 77 |
+
|
| 78 |
+
def toggle_overlay(w, v):
|
| 79 |
+
w.overlay = bool(v)
|
| 80 |
+
return (*rebuild(w), w)
|
| 81 |
+
|
| 82 |
+
def toggle_auto_camera(w, v):
|
| 83 |
+
w.auto_camera = bool(v)
|
| 84 |
+
w.ui_events.append(f"t={w.step}: auto_camera={w.auto_camera}")
|
| 85 |
+
return (*rebuild(w), w)
|
| 86 |
+
|
| 87 |
+
def swap_pov(w):
|
| 88 |
+
names = list(w.agents.keys())
|
| 89 |
+
i = names.index(w.pov)
|
| 90 |
+
w.pov = names[(i + 1) % len(names)]
|
| 91 |
+
w.ui_events.append(f"t={w.step}: POV -> {w.pov}")
|
| 92 |
+
return (*rebuild(w), w)
|
| 93 |
+
|
| 94 |
+
def export_clicked(w):
|
| 95 |
+
data, fname = export_run(w)
|
| 96 |
+
return gr.File(value=(fname, data))
|
| 97 |
+
|
| 98 |
+
with gr.Blocks(title=TITLE) as demo:
|
| 99 |
+
gr.Markdown(
|
| 100 |
+
f"## {TITLE}\n"
|
| 101 |
+
"This is the **business spine**: event-sourcing + ledger accounting + time dilation + task SLA model.\n"
|
| 102 |
+
"Run weeks/months/years by changing **minutes per tick** and using **Run N ticks** or Autoplay."
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
w_state = gr.State(None)
|
| 106 |
+
autoplay_on = gr.State(False)
|
| 107 |
+
|
| 108 |
+
with gr.Row():
|
| 109 |
+
arena = gr.HTML(label="Arena (Animated SVG)")
|
| 110 |
+
with gr.Column():
|
| 111 |
+
pov_img = gr.Image(label="Agent POV (Body-cam Mode)", type="numpy", width=560, height=315)
|
| 112 |
+
status = gr.Textbox(label="Status + KPIs", lines=5)
|
| 113 |
+
tasks_box = gr.Textbox(label="Task Queue (top slice)", lines=12)
|
| 114 |
+
|
| 115 |
+
with gr.Row():
|
| 116 |
+
ledger_box = gr.Textbox(label="Ledger (cost/tokens/latency)", lines=12)
|
| 117 |
+
events_box = gr.Textbox(label="Run Log (UI)", lines=12)
|
| 118 |
+
|
| 119 |
+
with gr.Accordion("Scenario Controls (Business)", open=True):
|
| 120 |
+
with gr.Row():
|
| 121 |
+
seed = gr.Number(value=1337, precision=0, label="Seed (determinism)")
|
| 122 |
+
minutes_per_tick = gr.Slider(5, 240, value=30, step=5, label="Minutes per tick (time dilation)")
|
| 123 |
+
with gr.Row():
|
| 124 |
+
budget_soft = gr.Number(value=5.00, precision=2, label="Budget soft cap (USD)")
|
| 125 |
+
budget_hard = gr.Number(value=10.00, precision=2, label="Budget hard cap (USD)")
|
| 126 |
+
with gr.Row():
|
| 127 |
+
initial_tasks = gr.Slider(0, 200, value=24, step=1, label="Initial backlog size")
|
| 128 |
+
new_tasks_per_day = gr.Slider(0, 50, value=6, step=1, label="New tasks per simulated day")
|
| 129 |
+
rework_base = gr.Slider(0.0, 0.5, value=0.08, step=0.01, label="Base rework probability")
|
| 130 |
+
|
| 131 |
+
with gr.Row():
|
| 132 |
+
btn_apply = gr.Button("Apply / New Run")
|
| 133 |
+
btn_run = gr.Button("Run N ticks")
|
| 134 |
+
run_n = gr.Number(value=120, precision=0, label="N")
|
| 135 |
+
|
| 136 |
+
with gr.Accordion("Playback + Camera", open=True):
|
| 137 |
+
with gr.Row():
|
| 138 |
+
overlay = gr.Checkbox(value=True, label="POV Reticle Overlay")
|
| 139 |
+
auto_camera = gr.Checkbox(value=True, label="Auto Camera Cuts")
|
| 140 |
+
btn_swap_pov = gr.Button("Swap POV Agent")
|
| 141 |
+
with gr.Row():
|
| 142 |
+
autoplay_speed = gr.Slider(0.05, 0.8, value=0.18, step=0.01, label="Autoplay tick interval (sec)")
|
| 143 |
+
btn_play = gr.Button("▶ Start Autoplay")
|
| 144 |
+
btn_pause = gr.Button("⏸ Stop Autoplay")
|
| 145 |
+
|
| 146 |
+
with gr.Accordion("Exports", open=True):
|
| 147 |
+
btn_export = gr.Button("Export run (events.jsonl + metrics.csv + summary.json)")
|
| 148 |
+
export_file = gr.File(label="Download export ZIP")
|
| 149 |
+
|
| 150 |
+
timer = gr.Timer(value=0.18, active=False)
|
| 151 |
+
|
| 152 |
+
# Init on load
|
| 153 |
+
def on_load():
|
| 154 |
+
cfg = SimConfig()
|
| 155 |
+
w = init_world(seed=1337, run_id=new_run_id(), config=cfg)
|
| 156 |
+
return (*rebuild(w), w)
|
| 157 |
+
|
| 158 |
+
demo.load(on_load, inputs=[], outputs=[arena, pov_img, status, tasks_box, ledger_box, events_box, w_state], queue=True)
|
| 159 |
+
|
| 160 |
+
btn_apply.click(
|
| 161 |
+
apply_settings,
|
| 162 |
+
inputs=[seed, minutes_per_tick, budget_soft, budget_hard, initial_tasks, new_tasks_per_day, rework_base],
|
| 163 |
+
outputs=[arena, pov_img, status, tasks_box, ledger_box, events_box, w_state],
|
| 164 |
+
queue=True,
|
| 165 |
+
)
|
| 166 |
+
|
| 167 |
+
btn_run.click(
|
| 168 |
+
run_n_ticks,
|
| 169 |
+
inputs=[w_state, run_n],
|
| 170 |
+
outputs=[arena, pov_img, status, tasks_box, ledger_box, events_box, w_state],
|
| 171 |
+
queue=True,
|
| 172 |
+
)
|
| 173 |
+
|
| 174 |
+
overlay.change(
|
| 175 |
+
toggle_overlay,
|
| 176 |
+
inputs=[w_state, overlay],
|
| 177 |
+
outputs=[arena, pov_img, status, tasks_box, ledger_box, events_box, w_state],
|
| 178 |
+
queue=True,
|
| 179 |
+
)
|
| 180 |
+
|
| 181 |
+
auto_camera.change(
|
| 182 |
+
toggle_auto_camera,
|
| 183 |
+
inputs=[w_state, auto_camera],
|
| 184 |
+
outputs=[arena, pov_img, status, tasks_box, ledger_box, events_box, w_state],
|
| 185 |
+
queue=True,
|
| 186 |
+
)
|
| 187 |
+
|
| 188 |
+
btn_swap_pov.click(
|
| 189 |
+
swap_pov,
|
| 190 |
+
inputs=[w_state],
|
| 191 |
+
outputs=[arena, pov_img, status, tasks_box, ledger_box, events_box, w_state],
|
| 192 |
+
queue=True,
|
| 193 |
+
)
|
| 194 |
+
|
| 195 |
+
# Autoplay
|
| 196 |
+
def autoplay_start(interval: float):
|
| 197 |
+
return gr.update(value=float(interval), active=True), True
|
| 198 |
+
|
| 199 |
+
def autoplay_stop():
|
| 200 |
+
return gr.update(active=False), False
|
| 201 |
+
|
| 202 |
+
btn_play.click(autoplay_start, inputs=[autoplay_speed], outputs=[timer, autoplay_on], queue=True)
|
| 203 |
+
btn_pause.click(autoplay_stop, inputs=[], outputs=[timer, autoplay_on], queue=True)
|
| 204 |
+
|
| 205 |
+
def autoplay_tick(w, is_on: bool):
|
| 206 |
+
if not is_on:
|
| 207 |
+
return (*rebuild(w), w, is_on, gr.update())
|
| 208 |
+
if not w.done:
|
| 209 |
+
tick(w)
|
| 210 |
+
return (*rebuild(w), w, is_on, gr.update())
|
| 211 |
+
|
| 212 |
+
timer.tick(
|
| 213 |
+
autoplay_tick,
|
| 214 |
+
inputs=[w_state, autoplay_on],
|
| 215 |
+
outputs=[arena, pov_img, status, tasks_box, ledger_box, events_box, w_state, autoplay_on, timer],
|
| 216 |
+
queue=True,
|
| 217 |
+
)
|
| 218 |
+
|
| 219 |
+
btn_export.click(
|
| 220 |
+
export_clicked,
|
| 221 |
+
inputs=[w_state],
|
| 222 |
+
outputs=[export_file],
|
| 223 |
+
queue=True,
|
| 224 |
+
)
|
| 225 |
+
|
| 226 |
+
demo.queue().launch(ssr_mode=False)
|