data-gen-ebd/rollout/dashboard.py
amos 6166b7624f Add the read-only live dashboard for the task catalog and rollout progress
rollout.dashboard serves catalog counts, attempts and complete trajectories, and the public
prompt and template of each task. catalog.controllers()/complete() are split out so the
dashboard can reuse receipts it has already read.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 17:45:28 +08:00

587 lines
36 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""Serve a live, read-only dashboard of the task bank and rollout attempts. Task pages expose only the public
prompt and the starting workspace (template/); graders, reference fixtures and specs never leave the host."""
import argparse
import base64
import collections
import datetime
import hmac
import http.server
import json
import os
from pathlib import Path
import re
import stat
import subprocess
import sys
import threading
import time
import urllib.parse
from . import catalog, core
# ponytail: a controller without a receipt counts as running while it changed this recently; a crashed one shows as
# running until then. Upgrade path: have pi_run write a heartbeat file.
ACTIVE_SECONDS = 2 * 3600
TZ = datetime.timezone(datetime.timedelta(hours=8))
MAX_SHOWN_BYTES = 512 * 1024
TASK_ID = re.compile(r"[A-Za-z0-9][A-Za-z0-9_.-]{0,127}")
def route(source):
if source.startswith("bos-qiao/task_factory"):
return "Bos 导入"
if source.startswith("bos-qiao/"):
return "Bos 改写"
if source.startswith("bos-seed/"):
return "Bos 种子"
if source.startswith("hwe/"):
return "HWE 改写"
return "原创"
def added_dates(repo):
"""Task directory -> date its task.json first entered Git; uncommitted tasks are absent."""
try:
out = subprocess.run(["git", "-C", str(repo), "log", "--diff-filter=A", "--date=short", "--format=@%ad",
"--name-only", "--", "tasks/*/task.json"], capture_output=True, text=True,
timeout=60).stdout
except (OSError, subprocess.SubprocessError):
return {}
dates, day = {}, None
for line in out.splitlines():
if line.startswith("@"):
day = line[1:]
elif line.strip():
dates[Path(line).parent.name] = day # Log runs newest first, so the first add wins.
return dates
def _mtime(path):
try:
return os.stat(path).st_mtime
except OSError:
return 0.0
def _row(path, receipt, now):
row = dict(name=path.name, batch=path.parent.name)
if receipt is not None:
row.update(task_id=receipt.get("task_id"), model=receipt.get("model_id", "glm-5.3"),
status=receipt.get("status"), complete=catalog.complete(receipt),
time=_mtime(path / "receipt.json"))
return row
events = sorted(path.glob("calibration-*/events.jsonl")) + sorted(path.glob("run-*/events.jsonl"))
first = []
for name in events:
try:
with open(name, encoding="utf-8") as stream:
first.append(json.loads(stream.readline()))
except (OSError, ValueError):
pass
active = max([_mtime(path), _mtime(path / "broker.jsonl")] + [_mtime(name) for name in events])
row.update(task_id=next((event.get("task_id") for event in first if event.get("task_id")), None), model=None,
complete=False, time=active, started=min((event.get("time", active) for event in first), default=None),
phase="Agent" if any(path.glob("run-*")) else "校准",
status="RUNNING" if now - active < ACTIVE_SECONDS else "NO_RECEIPT")
return row
def scan(roots, now):
"""(controller, receipt) pairs and one display row per controller under these roots."""
pairs = list(catalog.controllers(roots))
return pairs, [_row(path, receipt, now) for path, receipt in pairs]
def _day(seconds):
return datetime.datetime.fromtimestamp(seconds, TZ).strftime("%Y-%m-%d")
def summarize(bank, attempts, added, cold_done):
rows = []
for task in bank["tasks"]:
rows.append(dict(id=task["id"], status=task["status"], difficulty=task["difficulty"],
reviewed=task["difficulty_source"] == "reviewed", family=task["family"],
language=task["language"], source=task["source"], route=route(task["source"]),
version=task["version"], fixtures=task["fixtures"], results=task["results"],
reason=task.get("reason"), added=added.get(task["id"])))
ran = {row["id"] for row in rows if any(sum(r.values()) for r in row["results"].values())}
families = collections.defaultdict(collections.Counter)
for row in rows:
counts = families[row["family"]]
counts["total"] += 1
counts["usable"] += row["status"] == "USABLE"
counts["ran"] += row["id"] in ran
counts["hard"] += row["status"] == "USABLE" and row["difficulty"] == "HARD"
models = collections.defaultdict(collections.Counter)
daily = collections.defaultdict(collections.Counter)
for item in attempts:
if item["status"] == "RUNNING":
kind = "running"
elif item["complete"]:
kind = item["status"].lower()
else:
kind = "excluded"
if item["model"]: # Unknown until the receipt is written.
models[item["model"]][kind] += 1
if kind != "running" and item["time"]:
daily[_day(item["time"])]["complete" if item["complete"] else "excluded"] += 1
for row in rows:
if row["added"]:
daily[row["added"]]["tasks"] += 1
finished = sorted((item for item in attempts if item["status"] != "RUNNING"), key=lambda item: -item["time"])
count = lambda key, items: dict(collections.Counter(item[key] for item in items))
complete = [item for item in attempts if item["complete"]]
return dict(
image_id=bank["image_id"], cold_done=cold_done,
tasks=dict(total=len(rows), committed=sum(bool(row["added"]) for row in rows), status=count("status", rows),
usable_difficulty=count("difficulty", [row for row in rows if row["status"] == "USABLE"]),
routes=count("route", rows), languages=count("language", rows), ran=len(ran),
ran_ever=len({item["task_id"] for item in complete}),
families={name: dict(counts) for name, counts in sorted(families.items())}),
attempts=dict(total=len(attempts), complete=len(complete),
passed=sum(item["status"] == "PASS" for item in complete),
failed=sum(item["status"] == "FAIL" for item in complete),
running=sum(item["status"] == "RUNNING" for item in attempts),
excluded=sum(not item["complete"] and item["status"] != "RUNNING" for item in attempts),
models={name: dict(counts) for name, counts in sorted(models.items())}),
daily=[dict(day=day, **counts) for day, counts in sorted(daily.items())],
running=sorted((item for item in attempts if item["status"] == "RUNNING"),
key=lambda item: item.get("started") or 0),
recent=finished[:40], rows=rows)
class State:
def __init__(self):
self.lock = threading.Lock()
self.snapshot, self.error, self.cold, self.cold_at = None, None, None, None
self.cold_calibrated = {}
def _merge(*groups):
"""Hot and cold copies of one controller collapse into one; the copy with a receipt wins."""
merged = {}
for pairs, rows in groups:
for pair, row in zip(pairs, rows):
if pair[1] is not None or row["name"] not in merged:
merged[row["name"]] = pair, row
return [pair for pair, _ in merged.values()], [row for _, row in merged.values()]
def save_cold(path, at, cold, calibrated):
"""Keep only what the counts read, so a restart need not wait hours for the archive disk."""
pairs = [(str(controller), None if receipt is None else dict(
{key: receipt.get(key) for key in ("task_id", "task_digest", "model_id", "status", "cleanup", "agent_exit")},
pi_evidence=dict(status=(receipt.get("pi_evidence") or {}).get("status")))) for controller, receipt in cold[0]]
temporary = path.with_name(path.name + ".tmp")
temporary.write_text(json.dumps(dict(at=at, pairs=pairs, rows=cold[1], calibrated=calibrated)), encoding="utf-8")
os.replace(temporary, path)
def load_cold(path):
saved = json.loads(path.read_text(encoding="utf-8"))
return (saved["at"], ([(Path(controller), receipt) for controller, receipt in saved["pairs"]], saved["rows"]),
saved.get("calibrated", {}))
def refresh_cold(args, state):
if args.cold_cache and args.cold_cache.is_file():
try:
at, cold, calibrated = load_cold(args.cold_cache)
with state.lock:
state.cold, state.cold_at, state.cold_calibrated = cold, at, calibrated
except (OSError, ValueError, KeyError):
pass # A bad cache only costs one slow scan.
while True:
try:
calibrated = catalog.calibrations(args.cold_calibrations, args.image_id)
with state.lock:
state.cold_calibrated = calibrated
cold = scan(args.cold_runs, time.time())
at = time.time()
with state.lock:
state.cold, state.cold_at = cold, at
if args.cold_cache:
save_cold(args.cold_cache, at, cold, calibrated)
except Exception as error: # Keep the last scan; the page shows why it is stale.
with state.lock:
state.error = "冷盘扫描失败:%s: %s" % (type(error).__name__, error)
time.sleep(args.cold_refresh)
def refresh(args, state):
"""One pass over the task bank and the hot runs, merged with the latest cold scan."""
holds, reviews = (args.repo / "docs" / name for name in ("task-holds.json", "task-reviews.json"))
now = time.time()
with state.lock:
cold, cold_at, calibrated = state.cold, state.cold_at, state.cold_calibrated
pairs, rows = _merge(scan(args.runs, now), cold or ([], []))
bank = catalog.build(args.repo / "tasks", args.calibrations, [], holds if holds.is_file() else None,
args.image_id, reviews if reviews.is_file() else None, found=pairs, calibrated=calibrated)
snapshot = summarize(bank, rows, added_dates(args.repo), cold is not None or not args.cold_runs)
snapshot.update(scanned_at=now, cold_at=cold_at, refresh=args.refresh)
with state.lock:
state.snapshot, state.error = snapshot, None
def refresh_hot(args, state):
while True:
try:
refresh(args, state)
except Exception as error: # A task caught mid-edit fails to load; serve the last good snapshot meanwhile.
with state.lock:
state.error = "%s: %s" % (type(error).__name__, error)
time.sleep(args.refresh)
def public_files(task_root):
"""Relative paths and sizes of the starting workspace an Agent sees."""
template = task_root / "template"
files = []
for folder, dirs, names in os.walk(template):
dirs.sort()
for name in sorted(names):
path = Path(folder, name)
info = os.lstat(path)
if stat.S_ISREG(info.st_mode):
files.append(dict(path=path.relative_to(template).as_posix(), size=info.st_size))
return files
class Handler(http.server.BaseHTTPRequestHandler):
server_version = "embedded-rollout-dashboard"
def log_message(self, *args):
pass # No per-request log on the shared disk.
def _send(self, code, body, kind="application/json; charset=utf-8"):
data = body if isinstance(body, bytes) else json.dumps(body, ensure_ascii=False).encode("utf-8")
self.send_response(code)
self.send_header("Content-Type", kind)
self.send_header("Content-Length", str(len(data)))
self.send_header("Cache-Control", "no-store")
self.send_header("X-Content-Type-Options", "nosniff")
self.send_header("Content-Security-Policy", "default-src 'self'; style-src 'unsafe-inline'; "
"script-src 'unsafe-inline'")
if code == 401:
self.send_header("WWW-Authenticate", 'Basic realm="embedded-rollout", charset="UTF-8"')
self.end_headers()
self.wfile.write(data)
def _authorized(self):
header = self.headers.get("Authorization", "")
if not header.startswith("Basic "):
return False
try:
supplied = base64.b64decode(header[6:], validate=True).decode("utf-8").partition(":")[2]
except ValueError:
return False
return hmac.compare_digest(supplied.encode("utf-8"), self.server.password.encode("utf-8"))
def _task(self, task_id):
with self.server.state.lock:
snapshot = self.server.state.snapshot
rows = {row["id"]: row for row in (snapshot or {}).get("rows", [])}
if not TASK_ID.fullmatch(task_id) or task_id not in rows:
return None, None
return rows[task_id], self.server.repo / "tasks" / task_id
def do_GET(self):
if self.server.password and not self._authorized():
return self._send(401, dict(error="需要登录"))
path = urllib.parse.unquote(urllib.parse.urlsplit(self.path).path)
try:
if path == "/":
return self._send(200, PAGE.encode("utf-8"), "text/html; charset=utf-8")
if path == "/api/summary":
with self.server.state.lock:
body = dict(self.server.state.snapshot or {}, error=self.server.state.error)
return self._send(200, body)
if path.startswith("/api/task/"):
row, root = self._task(path[len("/api/task/"):])
if row is None:
return self._send(404, dict(error="没有这道题"))
manifest = json.loads(core._read(root, "task.json"))
return self._send(200, dict(row=row, files=public_files(root),
prompt=core._read(root, "prompt.md").decode("utf-8", "replace"),
manifest={key: manifest.get(key) for key in (
"id", "version", "family", "source", "candidate_files",
"timeout_seconds")}))
if path.startswith("/api/file/"):
task_id, _, name = path[len("/api/file/"):].partition("/")
row, root = self._task(task_id)
if row is None or name not in {item["path"] for item in public_files(root)}:
return self._send(404, dict(error="只能查看题面和初始工作区里的文件"))
data = core._read(root / "template", name, MAX_SHOWN_BYTES)
return self._send(200, dict(path=name, binary=b"\0" in data,
text=None if b"\0" in data else data.decode("utf-8", "replace")))
except (OSError, ValueError) as error:
return self._send(500, dict(error=str(error)))
return self._send(404, dict(error="not found"))
def server(args, state, password):
httpd = http.server.ThreadingHTTPServer((args.host, args.port), Handler)
httpd.state, httpd.repo, httpd.password = state, args.repo, password
return httpd
def serve(args):
password = None
if args.password_file:
secret = core._path(args.password_file)
info = os.stat(secret)
if not stat.S_ISREG(info.st_mode) or info.st_mode & 0o077:
raise ValueError("password file must be a regular file readable only by its owner (0600)")
password = core._read(secret.parent, secret.name).decode("utf-8").strip()
if not password:
raise ValueError("password file is empty")
state = State()
httpd = server(args, state, password)
for target in (refresh_hot,) + ((refresh_cold,) if args.cold_runs or args.cold_calibrations else ()):
threading.Thread(target=target, args=(args, state), daemon=True).start()
print("dashboard on http://%s:%d/" % (args.host, args.port), flush=True)
httpd.serve_forever()
def main(argv=None):
parser = argparse.ArgumentParser(description="Serve the live task-bank and rollout dashboard")
parser.add_argument("--repo", type=Path, default=Path("."), help="checkout holding tasks/ and docs/")
parser.add_argument("--calibrations", type=Path, nargs="+", required=True)
parser.add_argument("--runs", type=Path, nargs="*", default=[], help="hot run roots, rescanned every --refresh")
parser.add_argument("--cold-runs", type=Path, nargs="*", default=[], help="slow archive roots")
parser.add_argument("--cold-calibrations", type=Path, nargs="*", default=[],
help="calibration roots on the slow archive disk, read with --cold-runs")
parser.add_argument("--image-id", required=True)
parser.add_argument("--host", default="127.0.0.1")
parser.add_argument("--port", type=int, default=8091)
parser.add_argument("--password-file", type=Path, help="0600 file; viewers log in with any user name")
parser.add_argument("--refresh", type=int, default=60)
parser.add_argument("--cold-refresh", type=int, default=3600)
parser.add_argument("--cold-cache", type=Path, help="local JSON file that keeps the last archive scan")
args = parser.parse_args(argv)
args.repo = core._path(args.repo)
try:
serve(args)
except (OSError, ValueError) as error:
print("dashboard rejected: " + str(error), file=sys.stderr)
return 2
return 0
PAGE = r"""<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Embedded Rollout 看板</title>
<style>
:root{--bg:#f5f6f8;--panel:#fff;--ink:#1b2230;--muted:#667085;--line:#e4e7ec;--accent:#2f6fdb;--pass:#1f9d55;
--fail:#d64545;--warn:#c98a12;--run:#7c4ddb;--chip:#eef1f6;--code:#f7f8fa;color-scheme:light}
@media (prefers-color-scheme:dark){:root{--bg:#0f1218;--panel:#171b23;--ink:#e6e9ef;--muted:#98a2b3;--line:#2a303c;
--accent:#6b9cff;--pass:#3ccf7f;--fail:#ff6b6b;--warn:#f0b43c;--run:#a98bff;--chip:#222836;--code:#12161d;color-scheme:dark}}
*{box-sizing:border-box}body{margin:0;background:var(--bg);color:var(--ink);font:14px/1.5 -apple-system,BlinkMacSystemFont,
"Segoe UI","PingFang SC","Microsoft YaHei",sans-serif}
header{display:flex;flex-wrap:wrap;gap:8px 16px;align-items:baseline;padding:16px 24px;border-bottom:1px solid var(--line);
background:var(--panel)}header h1{font-size:18px;margin:0}header .meta{color:var(--muted);font-size:12px}
main{max-width:1280px;margin:0 auto;padding:16px}a{color:var(--accent);text-decoration:none;cursor:pointer}
.grid{display:grid;gap:12px}.kpis{grid-template-columns:repeat(auto-fit,minmax(150px,1fr))}
.two{grid-template-columns:repeat(auto-fit,minmax(340px,1fr))}
.card{background:var(--panel);border:1px solid var(--line);border-radius:10px;padding:14px 16px;min-width:0}
.kpi .n{font-size:28px;font-weight:650;font-variant-numeric:tabular-nums}.kpi .l{color:var(--muted);font-size:12px}
.kpi .s{color:var(--muted);font-size:12px;margin-top:2px}h2{font-size:14px;margin:0 0 10px}
section{margin-top:12px}.bar{display:grid;grid-template-columns:minmax(90px,170px) 1fr 44px;gap:8px;align-items:center;
margin:5px 0;font-size:13px}.bar .t{height:10px;background:var(--chip);border-radius:5px;overflow:hidden;display:flex}
.bar .t i{display:block;height:100%}.bar .v{text-align:right;font-variant-numeric:tabular-nums;color:var(--muted)}
.name{overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
table{width:100%;border-collapse:collapse;font-size:13px}th,td{text-align:left;padding:6px 8px;border-bottom:1px solid var(--line);
white-space:nowrap}th{color:var(--muted);font-weight:500;position:sticky;top:0;background:var(--panel)}
td.src{max-width:260px;overflow:hidden;text-overflow:ellipsis}.scroll{overflow:auto;max-height:640px}
.chip{display:inline-block;padding:0 7px;border-radius:9px;font-size:12px;background:var(--chip)}
.USABLE,.PASS,.ROUTINE{color:var(--pass)}.FAIL,.HARD,.BLOCKED{color:var(--fail)}.HOLD,.MEDIUM,.PARTIAL{color:var(--warn)}
.RUNNING{color:var(--run)}.filters{display:flex;flex-wrap:wrap;gap:8px;margin-bottom:10px}
input,select{font:inherit;color:inherit;background:var(--bg);border:1px solid var(--line);border-radius:6px;padding:5px 8px}
.days{display:flex;gap:3px;align-items:flex-end;height:120px;overflow-x:auto;padding-top:6px}
.day{flex:1 0 22px;display:flex;flex-direction:column;justify-content:flex-end;align-items:center;height:100%;font-size:10px;
color:var(--muted)}.day i{display:block;width:100%;border-radius:3px 3px 0 0}.legend{font-size:12px;color:var(--muted)}
.legend b{display:inline-block;width:10px;height:10px;border-radius:2px;margin:0 4px 0 10px;vertical-align:-1px}
.err{background:color-mix(in srgb,var(--fail) 12%,var(--panel));border-color:var(--fail)}
.detail{display:grid;grid-template-columns:260px 1fr;gap:12px}@media (max-width:820px){.detail{grid-template-columns:1fr}}
.files a{display:block;padding:3px 6px;border-radius:5px;font-family:ui-monospace,Menlo,Consolas,monospace;font-size:12px;
overflow:hidden;text-overflow:ellipsis;white-space:nowrap;color:var(--ink)}.files a.on{background:var(--chip);color:var(--accent)}
pre{margin:0;background:var(--code);border:1px solid var(--line);border-radius:8px;padding:12px;overflow:auto;
font:12px/1.55 ui-monospace,Menlo,Consolas,monospace;max-height:70vh}pre.prompt{white-space:pre-wrap;max-height:none}
.code{counter-reset:ln}.code span{display:block}.code span::before{counter-increment:ln;content:counter(ln);display:inline-block;
width:3.2em;margin-right:12px;text-align:right;color:var(--muted);user-select:none}
.kv{display:grid;grid-template-columns:auto 1fr;gap:4px 12px;font-size:13px}.kv span:nth-child(odd){color:var(--muted)}
.muted{color:var(--muted)}
</style>
</head>
<body>
<header><h1>Embedded Rollout 看板</h1><span class="meta" id="meta">加载中…</span></header>
<main id="app"></main>
<script>
"use strict";
const $app = document.getElementById("app"), $meta = document.getElementById("meta");
const COLORS = {USABLE:"var(--pass)",HOLD:"var(--warn)",BLOCKED:"var(--fail)",UNCALIBRATED:"var(--muted)",
HARD:"var(--fail)",MEDIUM:"var(--warn)",PARTIAL:"var(--warn)",ROUTINE:"var(--pass)",UNMEASURED:"var(--muted)"};
const LABEL = {USABLE:"可用",HOLD:"暂缓",BLOCKED:"判据待重写",UNCALIBRATED:"未校准",HARD:"难",MEDIUM:"中",
PARTIAL:"样本不足",ROUTINE:"常规",UNMEASURED:"未测",RUNNING:"运行中",PASS:"PASS",FAIL:"FAIL"};
let data = null, view = {q:"", status:"", difficulty:"", family:""}, openTask = null;
function h(tag, attrs, ...kids) {
const el = document.createElement(tag);
for (const [k, v] of Object.entries(attrs || {})) {
if (k === "on") for (const [e, f] of Object.entries(v)) el.addEventListener(e, f);
else if (k === "style") el.style.cssText = v; else if (v !== null && v !== undefined) el.setAttribute(k, v);
}
for (const kid of kids.flat()) if (kid !== null && kid !== undefined && kid !== false)
el.append(kid instanceof Node ? kid : document.createTextNode(String(kid)));
return el;
}
const fmt = s => s ? new Date(s * 1000).toLocaleString("zh-CN", {hour12:false}) : "—";
const ago = s => { const m = Math.round((Date.now() / 1000 - s) / 60); return m < 60 ? m + " 分钟前" : Math.round(m / 6) / 10 + " 小时前"; };
const ORDER = ["USABLE", "HOLD", "UNCALIBRATED", "BLOCKED"], LEVELS = ["HARD", "MEDIUM", "PARTIAL", "ROUTINE", "UNMEASURED"];
const rank = r => ORDER.indexOf(r.status) * 10 + LEVELS.indexOf(r.difficulty);
const pf = r => r ? (r.PASS || 0) + "/" + (r.FAIL || 0) : "0/0";
const kpi = (n, l, s) => h("div", {class:"card kpi"}, h("div", {class:"n"}, n), h("div", {class:"l"}, l), s ? h("div", {class:"s"}, s) : null);
function bars(title, counts, order) {
const keys = order ? order.filter(k => counts[k]) : Object.keys(counts).sort((a, b) => counts[b] - counts[a]);
const max = Math.max(1, ...keys.map(k => counts[k]));
return h("div", {class:"card"}, h("h2", {}, title), keys.map(k => h("div", {class:"bar"},
h("span", {class:"name", title:k}, LABEL[k] ? LABEL[k] + " " + k : k),
h("span", {class:"t"}, h("i", {style:"width:" + 100 * counts[k] / max + "%;background:" + (COLORS[k] || "var(--accent)")})),
h("span", {class:"v"}, counts[k]))));
}
function families(f) {
const names = Object.keys(f).sort((a, b) => f[b].total - f[a].total), max = Math.max(1, ...names.map(n => f[n].total));
return h("div", {class:"card"}, h("h2", {}, "任务族(总数 / 可用 / 已跑通 / 难题)"),
h("div", {class:"legend"}, h("b", {style:"background:var(--accent)"}), "已跑通", h("b", {style:"background:var(--pass);opacity:.45"}),
"可用未跑", h("b", {style:"background:var(--muted);opacity:.35"}), "不可用"),
names.map(n => { const c = f[n], ran = c.ran || 0, usable = Math.max(0, (c.usable || 0) - ran);
return h("div", {class:"bar"}, h("a", {class:"name", title:n, on:{click:() => { view.family = n; render(); location.hash = "tasks"; }}}, n),
h("span", {class:"t", style:"width:" + 100 * c.total / max + "%"},
h("i", {style:"width:" + 100 * ran / c.total + "%;background:var(--accent)"}),
h("i", {style:"width:" + 100 * usable / c.total + "%;background:var(--pass);opacity:.45"}),
h("i", {style:"flex:1;background:var(--muted);opacity:.35"})),
h("span", {class:"v"}, c.total + "·" + (c.usable || 0) + "·" + ran + "·" + (c.hard || 0))); }));
}
function daily(days) {
const last = days.slice(-30), max = Math.max(1, ...last.map(d => (d.complete || 0) + (d.excluded || 0)));
const maxT = Math.max(1, ...last.map(d => d.tasks || 0));
const col = (d, parts, m) => h("div", {class:"day", title:d.day}, parts.map(([v, c]) => v ? h("i", {style:"height:" + 96 * v / m + "px;background:" + c}) : null), d.day.slice(5));
return h("div", {class:"grid two"},
h("div", {class:"card"}, h("h2", {}, "每日结束的尝试"), h("div", {class:"legend"}, h("b", {style:"background:var(--accent)"}), "完整轨迹",
h("b", {style:"background:var(--muted);opacity:.5"}), "排除(环境故障等)"),
h("div", {class:"days"}, last.map(d => col(d, [[d.excluded, "color-mix(in srgb,var(--muted) 50%,transparent)"], [d.complete, "var(--accent)"]], max)))),
h("div", {class:"card"}, h("h2", {}, "每日新入库的题目(按首次提交日期)"),
h("div", {class:"days"}, last.map(d => col(d, [[d.tasks, "var(--pass)"]], maxT)))));
}
function attemptsTable(title, items, running) {
return h("div", {class:"card"}, h("h2", {}, title + "(" + items.length + ")"), items.length ? h("div", {class:"scroll", style:"max-height:320px"},
h("table", {}, h("thead", {}, h("tr", {}, ["任务", "模型", running ? "阶段" : "结果", "批次", running ? "开始 / 最近活动" : "结束时间"].map(t => h("th", {}, t)))),
h("tbody", {}, items.map(a => h("tr", {}, h("td", {}, a.task_id ? h("a", {on:{click:() => show(a.task_id)}}, a.task_id) : "—"),
h("td", {}, a.model || "—"),
h("td", {}, running ? h("span", {class:"RUNNING"}, a.phase) : h("span", {class:a.complete ? a.status : ""}, a.complete ? a.status : "排除 · " + a.status)),
h("td", {class:"src", title:a.batch}, a.batch),
h("td", {}, running ? fmt(a.started) + " / " + ago(a.time) : fmt(a.time))))))) : h("div", {class:"muted"}, "暂无"));
}
function taskTable() {
const rows = data.rows.filter(r => (!view.q || (r.id + " " + r.source).toLowerCase().includes(view.q.toLowerCase())) &&
(!view.status || r.status === view.status) && (!view.difficulty || r.difficulty === view.difficulty) && (!view.family || r.family === view.family))
.sort((x, y) => rank(x) - rank(y) || x.id.localeCompare(y.id));
const sel = (key, values) => h("select", {on:{change:e => { view[key] = e.target.value; render(); }}},
h("option", {value:""}, {status:"全部状态", difficulty:"全部难度", family:"全部任务族"}[key]),
values.map(v => { const o = h("option", {value:v}, LABEL[v] ? LABEL[v] + " " + v : v); if (view[key] === v) o.selected = true; return o; }));
const q = h("input", {placeholder:"搜索任务 ID / 来源", value:view.q, on:{input:e => { view.q = e.target.value; renderTable(); }}});
const box = h("div", {class:"card", id:"tasks"}, h("h2", {}, "全部题目(" + rows.length + " / " + data.rows.length + ")· 点击查看题面与代码"),
h("div", {class:"filters"}, q, sel("status", ["USABLE", "HOLD", "BLOCKED", "UNCALIBRATED"]),
sel("difficulty", ["HARD", "MEDIUM", "PARTIAL", "ROUTINE", "UNMEASURED"]), sel("family", [...new Set(data.rows.map(r => r.family))].sort())),
h("div", {class:"scroll"}, h("table", {}, h("thead", {}, h("tr", {}, ["任务", "状态", "难度", "Qwen P/F", "GLM P/F", "任务族", "语言", "来源路线", "入库", "来源"].map(t => h("th", {}, t)))),
h("tbody", {}, rows.map(r => h("tr", {}, h("td", {}, h("a", {on:{click:() => show(r.id)}}, r.id)),
h("td", {class:r.status}, LABEL[r.status] || r.status), h("td", {class:r.difficulty}, (LABEL[r.difficulty] || r.difficulty) + (r.reviewed ? "(评审)" : "")),
h("td", {}, pf(r.results["qwen3.8-27b-nvfp4"])), h("td", {}, pf(r.results["glm-5.3"])), h("td", {}, r.family), h("td", {}, r.language),
h("td", {}, r.route), h("td", {}, r.added || h("span", {class:"HOLD"}, "未提交")), h("td", {class:"src", title:r.source}, r.source)))))));
return box;
}
function renderTable() { const old = document.getElementById("tasks"), pos = old && old.querySelector("input").selectionStart;
const fresh = taskTable(); old.replaceWith(fresh); const i = fresh.querySelector("input"); i.focus(); if (pos !== null) i.setSelectionRange(pos, pos); }
function render() {
if (openTask) return;
if (!data || !data.tasks) { $app.replaceChildren(h("div", {class:"card"}, data && data.error ? "数据生成失败:" + data.error : "首次扫描中,请稍候…")); return; }
const t = data.tasks, a = data.attempts, st = t.status, typing = document.activeElement && document.activeElement.tagName === "INPUT";
$meta.textContent = "数据更新于 " + fmt(data.scanned_at) + " · 每 " + data.refresh + " 秒刷新 · 冷盘历史" +
(data.cold_at ? "扫描于 " + fmt(data.cold_at) : data.cold_done ? "未配置" : "扫描中(历史尝试暂未计入)") + " · 镜像 " + data.image_id.slice(7, 19);
$app.replaceChildren(...[
data.error ? h("div", {class:"card err"}, "最近一次刷新失败,以下为上一份数据:" + data.error) : null,
h("div", {class:"grid kpis"},
kpi(t.total, "已生成题目", t.committed + " 道已提交," + (t.total - t.committed) + " 道制作中"),
kpi(st.USABLE || 0, "可用题", "暂缓 " + (st.HOLD || 0) + " · 判据待重写 " + (st.BLOCKED || 0) + (st.UNCALIBRATED ? " · 未校准 " + st.UNCALIBRATED : "")),
kpi(t.ran, "已跑通的题", "当前版本有完整真实轨迹;历史上 " + t.ran_ever + " 道"),
kpi(t.usable_difficulty.HARD || 0, "难题(可用)", "Qwen 通过率 ≤1/3 且失败经复核"),
kpi(a.total, "已跑尝试", "完整轨迹 " + a.complete + " · 排除 " + a.excluded),
kpi(a.passed + " / " + a.failed, "完整轨迹 PASS / FAIL", "通过率 " + (a.complete ? Math.round(100 * a.passed / a.complete) : 0) + "%"),
kpi(a.running, "正在运行", a.running ? "见下方运行中列表" : "当前空闲")),
h("section", {class:"grid two"}, bars("题目状态", st, ["USABLE", "HOLD", "BLOCKED", "UNCALIBRATED"]),
bars("可用题难度", t.usable_difficulty, ["HARD", "MEDIUM", "PARTIAL", "ROUTINE", "UNMEASURED"])),
h("section", {class:"grid two"}, families(t.families), h("div", {class:"grid"}, bars("来源路线", t.routes), bars("语言", t.languages),
h("div", {class:"card"}, h("h2", {}, "按模型"), h("table", {}, h("thead", {}, h("tr", {}, ["模型", "PASS", "FAIL", "排除"].map(x => h("th", {}, x)))),
h("tbody", {}, Object.entries(a.models).map(([m, c]) => h("tr", {}, h("td", {}, m), h("td", {class:"PASS"}, c.pass || 0), h("td", {class:"FAIL"}, c.fail || 0),
h("td", {}, c.excluded || 0)))))))),
h("section", {}, daily(data.daily)),
h("section", {class:"grid two"}, attemptsTable("运行中", data.running, true), attemptsTable("最近结束", data.recent, false)),
h("section", {}, taskTable())].filter(Boolean));
if (typing) document.querySelector("#tasks input").focus();
}
async function api(path) { const r = await fetch(path, {cache:"no-store"}); const body = await r.json(); if (!r.ok) throw new Error(body.error || r.status); return body; }
async function show(id) { location.hash = "task/" + encodeURIComponent(id); }
async function renderTask(id) {
openTask = id; window.scrollTo(0, 0);
$app.replaceChildren(h("div", {class:"card"}, "加载 " + id + " …"));
let d; try { d = await api("/api/task/" + encodeURIComponent(id)); } catch (e) { $app.replaceChildren(h("div", {class:"card err"}, String(e))); return; }
const r = d.row, m = d.manifest, code = h("div", {}, h("div", {class:"muted"}, "选择左侧文件查看代码")), list = h("div", {class:"files"});
const open = async (f, link) => { list.querySelectorAll("a").forEach(x => x.classList.remove("on")); link.classList.add("on");
code.replaceChildren(h("div", {class:"muted"}, "加载 " + f.path + " …"));
try { const c = await api("/api/file/" + encodeURIComponent(id) + "/" + f.path.split("/").map(encodeURIComponent).join("/"));
code.replaceChildren(h("h2", {}, c.path, m.candidate_files.includes(c.path) ? h("span", {class:"chip", style:"margin-left:8px"}, "候选文件(Agent 需要修改)") : null),
c.binary ? h("div", {class:"muted"}, "二进制文件,不显示") : h("pre", {class:"code"}, c.text.replace(/\n$/, "").split("\n").map(l => h("span", {}, l || " "))));
} catch (e) { code.replaceChildren(h("div", {class:"err card"}, String(e))); } };
d.files.forEach(f => { const a = h("a", {title:f.path + " · " + f.size + " B"}, f.path); a.addEventListener("click", () => open(f, a)); list.append(a); });
$app.replaceChildren(h("p", {}, h("a", {on:{click:() => { location.hash = ""; }}}, "← 返回看板")),
h("div", {class:"grid two"}, h("div", {class:"card"}, h("h2", {}, r.id + " · v" + r.version), h("div", {class:"kv"},
h("span", {}, "状态"), h("span", {class:r.status}, (LABEL[r.status] || r.status) + (r.reason ? ":" + r.reason : "")),
h("span", {}, "难度"), h("span", {class:r.difficulty}, (LABEL[r.difficulty] || r.difficulty) + (r.reviewed ? "(人工评审)" : "")),
h("span", {}, "Qwen P/F"), h("span", {}, pf(r.results["qwen3.8-27b-nvfp4"])), h("span", {}, "GLM P/F"), h("span", {}, pf(r.results["glm-5.3"])),
h("span", {}, "任务族"), h("span", {}, r.family), h("span", {}, "语言"), h("span", {}, r.language),
h("span", {}, "来源"), h("span", {}, r.route + " · " + r.source), h("span", {}, "入库"), h("span", {}, r.added || "未提交"),
h("span", {}, "候选文件"), h("span", {}, m.candidate_files.join("、")), h("span", {}, "校准样例类"), h("span", {}, r.fixtures),
h("span", {}, "评分期限"), h("span", {}, m.timeout_seconds + " 秒"))),
h("div", {class:"card"}, h("h2", {}, "说明"), h("div", {class:"muted"},
"这里只展示 Agent 能看到的公开题面(prompt.md)和初始工作区(template/)。正式判据、参考解与对照样例只留在评分侧,不对外展示。"))),
h("section", {class:"card"}, h("h2", {}, "题面 prompt.md"), h("pre", {class:"prompt"}, d.prompt)),
h("section", {class:"card"}, h("h2", {}, "初始工作区 template/(" + d.files.length + " 个文件)"), h("div", {class:"detail"}, list, code)));
const first = d.files.find(f => m.candidate_files.includes(f.path)) || d.files[0];
if (first) open(first, [...list.children].find(a => a.textContent === first.path));
}
function route() { const m = location.hash.match(/^#task\/(.+)$/); if (m) renderTask(decodeURIComponent(m[1])); else { openTask = null; render(); } }
async function poll() { try { data = await api("/api/summary"); } catch (e) { data = Object.assign(data || {}, {error:String(e)}); } render(); }
window.addEventListener("hashchange", route);
poll().then(route); setInterval(poll, 30000);
</script>
</body>
</html>
"""
if __name__ == "__main__":
sys.exit(main())