mirror of
https://github.com/hanzoai/python-sdk.git
synced 2026-11-23 04:31:13 +00:00
Hanzo CI/CD / test (linux-amd64) (push) Skipped
Hanzo CI/CD / test (linux-arm64) (push) Skipped
Hanzo CI/CD / test-advisory (push) Skipped
Hanzo CI/CD / test-amd64 (push) Successful in 3m9s
Hanzo CI/CD / cicd (push) Failing after 3m33s
Hanzo CI/CD / verify (push) Skipped
CI/CD / cicd (push) Failing after 7m0s
* feat(hanzo-research): unified autonomous research protocol, adaptive testing & WAL cloud sync
* fix(hanzo-research): enforce exact paired McNemar in verify_progressive_screen & add exhaustive small-sample tests
* feat(hanzo-research): autonomous controller, cloud leases, HIP-1403, Kai#22 SPRT power & Cloud#938 GPU jobs
* feat(research): add native hanzoai/tasks scheduling, evo/dgx durable dispatch & SSE wait
* fix(hanzo-research): support JSON argv command arrays and stamp allow_when_busy on task dispatch
* fix(research): hydrate header-only /v1/research/evidence rows via GET /evidence/{id} in CloudLeaseManager
* feat(research): promising_inconclusive continuation, paired trace guard, and wait_gpu_job resilience
941 lines
42 KiB
Python
941 lines
42 KiB
Python
"""An in-process stand-in for api.hanzo.ai's `/v1/research` and `/v1/benchmark`.
|
|
|
|
It answers as hanzo-inc/cloud `apps/research` (research.go, record.go, store.go) and
|
|
`apps/benchmark` (benchmark.go, claims.go, presets.go, history.go) do at the rules a client
|
|
can observe: the batch keys and their bounds, required fields, versions appended by
|
|
content and read back canonical (the latest that is not retracted), completion derived,
|
|
writes forced private, grants on every version, the public `?org=` read only to a caller
|
|
with no org, RFC 7807 refusals, the acting org chosen from the token's signed `orgs`, and
|
|
claims on the leaderboard only from org admin. JWTs are read, not verified.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import base64
|
|
import hashlib
|
|
import threading
|
|
import urllib.parse
|
|
from typing import Any
|
|
from http.server import ThreadingHTTPServer, BaseHTTPRequestHandler
|
|
|
|
from hanzo_research.wire import key
|
|
|
|
CATALOG = [
|
|
{
|
|
"id": "gpqa_diamond",
|
|
"title": "GPQA-Diamond",
|
|
"axis": "science-reasoning",
|
|
"items": 198,
|
|
"native": True,
|
|
"source": "hendrydong/gpqa_diamond_mc",
|
|
},
|
|
{
|
|
"id": "livecodebench",
|
|
"title": "LiveCodeBench (v6)",
|
|
"axis": "competitive-coding",
|
|
"native": True,
|
|
"source": "livecodebench/code_generation_lite test6",
|
|
},
|
|
]
|
|
KEYS = {"benchmarks": "benchmarks", "runs": "runs", "studies": "studies", "papers": "papers"}
|
|
NEEDS = {"benchmarks": ("id",), "runs": ("id", "benchmark"), "studies": ("id",), "papers": ("id",)}
|
|
DERIVED = ("project", "canonical", "completion", "key", "visibility", "trainable", "publishable")
|
|
GAPS = (
|
|
"embedder",
|
|
"facts_digest",
|
|
"k",
|
|
"prompt",
|
|
"prompt_digest",
|
|
"store_digest",
|
|
"reader",
|
|
"temperature",
|
|
"max_tokens",
|
|
"dataset_digest",
|
|
"commit",
|
|
"questions",
|
|
"answered",
|
|
"when",
|
|
"ended",
|
|
"by",
|
|
"split",
|
|
"system",
|
|
"version",
|
|
)
|
|
|
|
|
|
def jwt(claims: dict[str, Any]) -> str:
|
|
"""An unsigned JWT carrying `claims` (the fake reads, never verifies)."""
|
|
enc = lambda d: base64.urlsafe_b64encode(json.dumps(d).encode()).decode().rstrip("=") # noqa: E731
|
|
return f"{enc({'alg': 'none'})}.{enc(claims)}.sig"
|
|
|
|
|
|
def completion(r: dict[str, Any]) -> str:
|
|
q, a = r.get("questions"), r.get("answered")
|
|
if q is None or a is None:
|
|
return "unknown"
|
|
if a > q:
|
|
return "inconsistent"
|
|
if a < q:
|
|
return "partial"
|
|
return "complete" if (r.get("ended") or "").strip() else "unknown"
|
|
|
|
|
|
class Problem(Exception):
|
|
def __init__(self, status: int, detail: str):
|
|
super().__init__(detail)
|
|
self.status, self.detail = status, detail
|
|
|
|
|
|
class Fake:
|
|
"""The server's state and its handlers; `requests` records every request it answered."""
|
|
|
|
def __init__(self) -> None:
|
|
self.versions: dict[tuple[str, str], list[dict[str, Any]]] = {} # (org, kind) -> versions
|
|
self.experiments: dict[str, list[dict[str, Any]]] = {}
|
|
self.attempts: dict[str, list[dict[str, Any]]] = {}
|
|
self.artifacts: dict[str, dict[str, dict[str, Any]]] = {}
|
|
self.evidences: dict[tuple[str, str], list[dict[str, Any]]] = {}
|
|
self.idempotency: dict[tuple[str, str], dict[str, Any]] = {}
|
|
self.workflow_jobs: dict[tuple[str, str], dict[str, Any]] = {}
|
|
self.workflow_namespaces: dict[str, dict[str, Any]] = {}
|
|
self.workflow_schedules: dict[tuple[str, str], dict[str, Any]] = {}
|
|
self.fleet_workers: list[dict[str, Any]] = []
|
|
self.agent_targets: list[dict[str, Any]] = []
|
|
self.fleet_presence: dict[str, dict[str, Any]] = {}
|
|
self.busy_defer_queues: set[str] = set()
|
|
self.instant_complete_jobs: set[str] = set()
|
|
self.sse_hang_sec: float = 0.0
|
|
self.sse_streams_opened = 0
|
|
self.stage_handler: Any = None
|
|
self.claims: list[dict[str, Any]] = []
|
|
self.attempt_rows: list[dict[str, Any]] = []
|
|
self.requests: list[dict[str, Any]] = []
|
|
self.unavailable = 0 # answer the next N requests 503
|
|
self.expired: set[str] = set() # bearer tokens answered 401
|
|
self.project = "" # file every write under this project instead of the caller's
|
|
self.seq = 0
|
|
self.lock = threading.Lock()
|
|
|
|
# ── identity ────────────────────────────────────────────────────────────────
|
|
|
|
def principal(self, headers: Any) -> dict[str, Any] | None:
|
|
auth = headers.get("Authorization") or ""
|
|
if not auth.startswith("Bearer "):
|
|
return None
|
|
parts = auth[7:].split(".")
|
|
if len(parts) != 3:
|
|
return None
|
|
c = json.loads(base64.urlsafe_b64decode(parts[1] + "=" * (-len(parts[1]) % 4)))
|
|
# the server's rule (auth_identity.go homeOrg, Acts): a person's home is the first of
|
|
# its signed orgs and it may switch among them, any org for a SuperAdmin (home admin);
|
|
# a machine (no orgs, or type application) is its owner and never switches
|
|
orgs = [m["org"] for m in c.get("orgs") or []]
|
|
machine = c.get("type") == "application" or not orgs
|
|
home = c.get("owner", "") if machine else orgs[0]
|
|
sudo = home == "admin" and not machine
|
|
want = headers.get("X-Org-Id") or ""
|
|
org = want if want and not machine and (sudo or (want != "admin" and want in orgs)) else home
|
|
return {
|
|
"org": org,
|
|
"home": home,
|
|
"super": sudo,
|
|
"user": c.get("sub", ""),
|
|
"project": c.get("project") or "default",
|
|
}
|
|
|
|
# ── research: records ───────────────────────────────────────────────────────
|
|
|
|
def canonical(self, org: str, kind: str) -> list[dict[str, Any]]:
|
|
latest: dict[tuple[str, str], dict[str, Any]] = {}
|
|
for v in self.versions.get((org, kind), []):
|
|
latest[(v["project"], v["id"])] = v
|
|
out = []
|
|
for v in latest.values():
|
|
if (v.get("revision") or "") == "retracted":
|
|
continue
|
|
r = dict(v)
|
|
r.pop("_hash", None)
|
|
r.pop("_seq", None)
|
|
r["canonical"] = True
|
|
if kind == "runs":
|
|
r["completion"] = completion(r)
|
|
r["key"] = key(r)
|
|
out.append(r)
|
|
return sorted(out, key=lambda r: (r["project"], r["id"]))
|
|
|
|
def put(self, who: dict[str, Any], kind: str, body: dict[str, Any]) -> dict[str, Any]:
|
|
rows = body.get(KEYS[kind]) or []
|
|
if not rows:
|
|
raise Problem(400, "nothing to record")
|
|
if len(rows) > 20000:
|
|
raise Problem(413, "batch too large; split the upload")
|
|
for r in rows:
|
|
if any(not r.get(f) for f in NEEDS[kind]):
|
|
raise Problem(422, {"runs": "run needs an id and a benchmark"}.get(kind, f"{kind[:-1]} needs an id"))
|
|
added = 0
|
|
store = self.versions.setdefault((who["org"], kind), [])
|
|
for r in rows:
|
|
rec = {k: v for k, v in r.items() if k not in DERIVED}
|
|
rec["revision"] = (
|
|
(rec.get("revision") or "").strip().lower()
|
|
if (rec.get("revision") or "").strip().lower() in ("corrected", "retracted")
|
|
else "original"
|
|
)
|
|
h = hashlib.sha256(json.dumps(rec, sort_keys=True).encode()).hexdigest()
|
|
if any(v["_hash"] == h and v["id"] == r["id"] and v["project"] == who["project"] for v in store):
|
|
continue
|
|
self.seq += 1
|
|
store.append(
|
|
{
|
|
**rec,
|
|
"project": who["project"],
|
|
"visibility": "private",
|
|
"trainable": False,
|
|
"publishable": False,
|
|
"_hash": h,
|
|
"_seq": self.seq,
|
|
}
|
|
)
|
|
added += 1
|
|
return {
|
|
"project": self.project or who["project"],
|
|
"recorded": added,
|
|
"canonical": len(self.canonical(who["org"], kind)),
|
|
}
|
|
|
|
def runs(self, who: dict[str, Any] | None, q: dict[str, str]) -> dict[str, Any]:
|
|
if who is None:
|
|
if not q.get("org"):
|
|
raise Problem(403, "a validated principal is required")
|
|
rows = [r for r in self.canonical(q["org"], "runs") if r["visibility"] == "public"]
|
|
if not rows:
|
|
raise Problem(404, "Not Found")
|
|
else:
|
|
rows = self.canonical(who["org"], "runs")
|
|
if q.get("completion") and q["completion"] not in ("complete", "partial", "inconsistent", "unknown"):
|
|
raise Problem(422, "completion must be complete, partial, inconsistent, or unknown")
|
|
for f in ("project", "id", "key", "benchmark", "system", "study", "completion"):
|
|
if q.get(f):
|
|
rows = [r for r in rows if r.get(f) == q[f]]
|
|
if q.get("since"):
|
|
rows = [r for r in rows if (r.get("when") or "") >= q["since"]]
|
|
if q.get("until"):
|
|
rows = [r for r in rows if r.get("when") and r["when"] <= q["until"]]
|
|
census = {
|
|
k: sum(1 for r in rows if r["completion"] == k) for k in ("complete", "partial", "inconsistent", "unknown")
|
|
}
|
|
gaps = [{"field": f, "runs": n} for f in GAPS if (n := sum(1 for r in rows if r.get(f) in (None, "")))]
|
|
return {"data": rows, "total": len(rows), "completion": census, "gaps": gaps}
|
|
|
|
def compare(self, who: dict[str, Any], q: dict[str, str]) -> dict[str, Any]:
|
|
if not q.get("a") or not q.get("b"):
|
|
raise Problem(400, "compare needs two run ids, a and b")
|
|
rows = {
|
|
r["id"]: r
|
|
for r in self.canonical(who["org"], "runs")
|
|
if r["project"] == (q.get("project") or who["project"])
|
|
}
|
|
for k in ("a", "b"):
|
|
if q[k] not in rows:
|
|
raise Problem(404, f'no canonical run "{q[k]}"')
|
|
a, b = rows[q["a"]], rows[q["b"]]
|
|
blocks = []
|
|
if a.get("benchmark") != b.get("benchmark"):
|
|
blocks.append(f'different benchmarks: "{a.get("benchmark")}" and "{b.get("benchmark")}"')
|
|
for r in (a, b):
|
|
if r["completion"] != "complete":
|
|
blocks.append(f'run "{r["id"]}" is not established as complete (completion {r["completion"]})')
|
|
config = [
|
|
{"field": f, "a": str(a.get(f) or ""), "b": str(b.get(f) or "")}
|
|
for f in ("system", "version", "commit", "dataset_digest")
|
|
if (a.get(f) or "") != (b.get(f) or "")
|
|
]
|
|
index = {(m["category"], m["metric"]): m for m in b.get("measures") or []}
|
|
deltas = []
|
|
for m in a.get("measures") or []:
|
|
n = index.get((m["category"], m["metric"]))
|
|
if n:
|
|
overlap = (
|
|
None
|
|
if None in (m.get("lo"), m.get("hi"), n.get("lo"), n.get("hi"))
|
|
else max(m["lo"], n["lo"]) <= min(m["hi"], n["hi"])
|
|
)
|
|
deltas.append(
|
|
{
|
|
"category": m["category"],
|
|
"metric": m["metric"],
|
|
"a": m["value"],
|
|
"b": n["value"],
|
|
"change": n["value"] - m["value"],
|
|
"overlap": overlap,
|
|
}
|
|
)
|
|
return {
|
|
"a": a,
|
|
"b": b,
|
|
"comparable": not blocks,
|
|
"blocks": blocks or None,
|
|
"config": config or None,
|
|
"deltas": deltas or None,
|
|
}
|
|
|
|
def grant(self, who: dict[str, Any], body: dict[str, Any]) -> dict[str, Any]:
|
|
if not body.get("id") and not body.get("sha256"):
|
|
raise Problem(400, "grant needs a stable id or an artifact sha256")
|
|
vis = body.get("visibility")
|
|
if vis is not None and vis not in ("private", "org", "public"):
|
|
raise Problem(422, "visibility must be private, org, or public")
|
|
project = body.get("project") or who["project"]
|
|
n = 0
|
|
if body.get("sha256"):
|
|
if vis is None:
|
|
raise Problem(400, "artifact grant needs a visibility")
|
|
a = self.artifacts.get(who["org"], {}).get(body["sha256"])
|
|
if a and a["project"] == project:
|
|
a["visibility"], n = vis, 1
|
|
else:
|
|
for v in self.versions.get((who["org"], "runs"), []):
|
|
if v["id"] == body["id"] and v["project"] == project:
|
|
for k in ("visibility", "trainable", "publishable"):
|
|
if body.get(k) is not None:
|
|
v[k] = body[k]
|
|
n += 1
|
|
if n == 0:
|
|
raise Problem(404, "no records for that target")
|
|
return {"updated": n}
|
|
|
|
# ── research: experiments and artifacts ─────────────────────────────────────
|
|
|
|
def ingest(self, who: dict[str, Any], body: dict[str, Any]) -> dict[str, Any]:
|
|
exps, atts = body.get("experiments") or [], body.get("attempts") or []
|
|
if not exps and not atts:
|
|
raise Problem(400, "ingest needs at least one experiment or attempt")
|
|
for e in exps:
|
|
if not (e.get("id") and e.get("kind") and e.get("subject")):
|
|
raise Problem(422, "experiment needs id, kind, and subject")
|
|
for a in atts:
|
|
if not (a.get("benchmark") and a.get("item") and a.get("model")):
|
|
raise Problem(422, "attempt needs benchmark, item, and model")
|
|
ex = self.experiments.setdefault(who["org"], [])
|
|
at = self.attempts.setdefault(who["org"], [])
|
|
e_added = a_added = 0
|
|
for e in exps:
|
|
if {**e, "project": who["project"]} not in ex:
|
|
ex.append({**e, "project": who["project"]})
|
|
e_added += 1
|
|
for a in atts:
|
|
if a not in at:
|
|
at.append(a)
|
|
a_added += 1
|
|
return {
|
|
"project": who["project"],
|
|
"experiments_ingested": e_added,
|
|
"attempts_ingested": a_added,
|
|
"canonical_experiments": len({e["id"] for e in ex}),
|
|
"experiments_retained": len(ex),
|
|
"canonical_attempts": len(at),
|
|
"attempts_retained": len(at),
|
|
"rolled_up": True,
|
|
}
|
|
|
|
def put_artifact(self, who: dict[str, Any], a: dict[str, Any]) -> dict[str, Any]:
|
|
if a.get("kind") not in ("snapshot", "report"):
|
|
raise Problem(422, "kind must be snapshot or report")
|
|
raw = base64.b64decode(a.get("content") or "")
|
|
if not raw:
|
|
raise Problem(422, "content must be non-empty base64")
|
|
h = hashlib.sha256(raw).hexdigest()
|
|
if a.get("sha256") and a["sha256"].lower() != h:
|
|
raise Problem(422, "sha256 does not match the content bytes")
|
|
store = self.artifacts.setdefault(who["org"], {})
|
|
created = h not in store
|
|
rec = {
|
|
k: a.get(k)
|
|
for k in (
|
|
"kind",
|
|
"run_id",
|
|
"git_sha",
|
|
"git_branch",
|
|
"git_dirty",
|
|
"lib_versions",
|
|
"ts",
|
|
"artifact_id",
|
|
"content_sha256",
|
|
"media_type",
|
|
"canonicalization",
|
|
"compression",
|
|
"uncompressed_size",
|
|
"tags",
|
|
)
|
|
}
|
|
rec["received_at"] = a.get("received_at") or 1700000999
|
|
store.setdefault(
|
|
h,
|
|
{
|
|
**rec,
|
|
"sha256": h,
|
|
"ref": "sha256:" + h,
|
|
"project": who["project"],
|
|
"visibility": "private",
|
|
"retention_class": "raw-artifact",
|
|
"_bytes": raw,
|
|
},
|
|
)
|
|
return {"sha256": h, "ref": "sha256:" + h, "created": created, "rolled_up": True}
|
|
|
|
# ── the arena ───────────────────────────────────────────────────────────────
|
|
|
|
def put_claims(self, who: dict[str, Any] | None, body: dict[str, Any]) -> dict[str, Any]:
|
|
if who is None:
|
|
raise Problem(401, "sign in")
|
|
if who["org"] == "admin" and not who["super"]:
|
|
raise Problem(403, "SuperAdmin required")
|
|
vis = body.get("visibility") or "private"
|
|
if vis not in ("private", "public"):
|
|
raise Problem(400, 'visibility is "private" or "public"')
|
|
out = {"recorded": 0, "rejected": [], "org": who["org"], "visibility": vis}
|
|
known = {c["id"] for c in CATALOG}
|
|
for i, c in enumerate(body.get("data") or []):
|
|
if not c.get("benchmark") or not c.get("model"):
|
|
out["rejected"].append(f"row {i}: needs a benchmark and a model")
|
|
elif c["benchmark"] not in known:
|
|
out["rejected"].append(f"row {i}: unknown benchmark")
|
|
elif not str(c.get("source") or "").strip():
|
|
out["rejected"].append(f"row {i}: a claim needs its source")
|
|
elif not 0 <= float(c.get("score", -1)) <= 100:
|
|
out["rejected"].append(f"row {i}: a score is a percentage from 0 to 100")
|
|
else:
|
|
self.claims.append(
|
|
{
|
|
**c,
|
|
"org": who["org"],
|
|
"visibility": vis,
|
|
"origin": "stored",
|
|
"at": "2026-10-15T00:00:00Z",
|
|
"by": who["user"],
|
|
}
|
|
)
|
|
out["recorded"] += 1
|
|
if not out["rejected"]:
|
|
del out["rejected"]
|
|
return out
|
|
|
|
def leaderboard(self, q: dict[str, str]) -> dict[str, Any]:
|
|
bench = q.get("benchmark") or "gpqa_diamond"
|
|
rows = []
|
|
for c in self.claims:
|
|
if c["benchmark"] == bench and c["org"] == "admin" and c["visibility"] == "public":
|
|
rows.append(
|
|
{
|
|
"model": c["model"],
|
|
"measured": None,
|
|
"n": 0,
|
|
"published": c["score"],
|
|
"gap": None,
|
|
"protocol": c["protocol"],
|
|
"claims": 1,
|
|
"mean": c["score"],
|
|
}
|
|
)
|
|
return {"benchmark": bench, "rows": rows}
|
|
|
|
# ── dispatch ────────────────────────────────────────────────────────────────
|
|
|
|
def handle(self, method: str, url: str, headers: Any, raw: bytes) -> tuple[int, str, bytes]:
|
|
parts = urllib.parse.urlsplit(url)
|
|
path = parts.path
|
|
q = {k: v[-1] for k, v in urllib.parse.parse_qs(parts.query).items()}
|
|
body = json.loads(raw) if raw else None
|
|
with self.lock:
|
|
self.requests.append(
|
|
{"method": method, "path": path, "query": q, "headers": dict(headers.items()), "body": body}
|
|
)
|
|
if self.unavailable:
|
|
self.unavailable -= 1
|
|
return 503, "application/json", b'{"detail":"research store failover in progress; retry","status":503}'
|
|
if (headers.get("Authorization") or "").removeprefix("Bearer ") in self.expired:
|
|
return 401, "application/json", b'{"detail":"token expired","status":401}'
|
|
if path == "/redirect":
|
|
return 302, "text/plain", b""
|
|
who = self.principal(headers)
|
|
try:
|
|
out = self.route(method, path, q, body, who, headers)
|
|
except Problem as p:
|
|
doc = {"detail": p.detail, "status": p.status, "title": "x", "type": "about:blank"}
|
|
return p.status, "application/json", json.dumps(doc).encode()
|
|
if isinstance(out, bytes):
|
|
return 200, "application/octet-stream", out
|
|
return out[0], "application/json", json.dumps(out[1]).encode()
|
|
|
|
def route(
|
|
self, method: str, path: str, q: dict[str, str], body: Any, who: dict[str, Any] | None, headers: Any = None
|
|
) -> Any:
|
|
ok = lambda x: (200, x) # noqa: E731
|
|
if path == "/v1/research/runs" and method == "GET":
|
|
return ok(self.runs(who, q))
|
|
if path.startswith("/v1/benchmark/"):
|
|
return self.arena(method, path, q, body, who)
|
|
if who is None:
|
|
raise Problem(403, "a validated principal is required")
|
|
if path == "/v1/compute/fleet/workers" and method == "GET":
|
|
return ok({"workers": list(self.fleet_workers)})
|
|
if path == "/v1/agent/targets" and method == "GET":
|
|
return ok({"targets": list(self.agent_targets)})
|
|
if path == "/v1/workflow/namespaces" and method == "POST":
|
|
b = body or {}
|
|
ns_name = str((b.get("namespaceInfo") or {}).get("name") or b.get("name") or "default")
|
|
rec = dict(b, name=ns_name)
|
|
self.workflow_namespaces[ns_name] = rec
|
|
return ok(rec)
|
|
if path.startswith("/v1/workflow/namespaces/"):
|
|
rest = path.removeprefix("/v1/workflow/namespaces/")
|
|
segs = [s for s in rest.split("/") if s]
|
|
if len(segs) == 4 and segs[0] == "fleet" and segs[1] == "activities" and method == "GET":
|
|
pres = self.fleet_presence.get(segs[2])
|
|
if not pres:
|
|
raise Problem(404, "presence activity not found")
|
|
return ok(pres)
|
|
if len(segs) == 2 and segs[1] == "activities" and method == "POST":
|
|
b = body or {}
|
|
ns = segs[0]
|
|
jid = str(b.get("activityId") or b.get("id") or "job-1")
|
|
rid = str(b.get("runId") or jid)
|
|
act_type = (
|
|
(b.get("activityType") or {}).get("name")
|
|
if isinstance(b.get("activityType"), dict)
|
|
else None
|
|
) or str(b.get("type") or "research.bench")
|
|
queue = str(b.get("taskQueue") or "gpu-jobs")
|
|
self.workflow_jobs[(ns, jid)] = {
|
|
"id": jid,
|
|
"activityId": jid,
|
|
"runId": rid,
|
|
"type": act_type,
|
|
"taskQueue": queue,
|
|
"input": b.get("input") or {},
|
|
"retryPolicy": b.get("retryPolicy"),
|
|
"heartbeatTimeout": b.get("heartbeatTimeout"),
|
|
"startToCloseTimeout": b.get("startToCloseTimeout"),
|
|
"requestId": b.get("requestId"),
|
|
"polls": 0,
|
|
"deferCount": 0,
|
|
"lastBusyReason": "",
|
|
"notBefore": "",
|
|
}
|
|
return ok(
|
|
{
|
|
"id": jid,
|
|
"activityId": jid,
|
|
"runId": rid,
|
|
"execution": {"workflowId": jid, "runId": rid},
|
|
"type": act_type,
|
|
"taskQueue": queue,
|
|
"input": b.get("input") or {},
|
|
"status": "ACTIVITY_TASK_STATE_SCHEDULED",
|
|
"state": "ACTIVITY_TASK_STATE_SCHEDULED",
|
|
}
|
|
)
|
|
if len(segs) == 5 and segs[1] == "activities" and segs[4] == "release" and method == "POST":
|
|
ns, jid = segs[0], segs[2]
|
|
job = self.workflow_jobs.get((ns, jid))
|
|
if not job:
|
|
raise Problem(404, "activity not found")
|
|
b = body or {}
|
|
job["deferCount"] = int(job.get("deferCount") or 0) + 1
|
|
job["lastBusyReason"] = str(b.get("reason") or "busy")
|
|
job["notBefore"] = "2026-11-23T00:15:00Z"
|
|
return ok(
|
|
{
|
|
"id": jid,
|
|
"execution": {"workflowId": jid, "runId": job["runId"]},
|
|
"taskQueue": job["taskQueue"],
|
|
"status": "ACTIVITY_TASK_STATE_SCHEDULED",
|
|
"state": "ACTIVITY_TASK_STATE_SCHEDULED",
|
|
"deferCount": job["deferCount"],
|
|
"lastBusyReason": job["lastBusyReason"],
|
|
"notBefore": job["notBefore"],
|
|
}
|
|
)
|
|
if len(segs) == 4 and segs[1] == "activities" and method == "GET":
|
|
ns, jid = segs[0], segs[2]
|
|
job = self.workflow_jobs.get((ns, jid))
|
|
if not job:
|
|
raise Problem(404, "activity not found")
|
|
job["polls"] += 1
|
|
p_idx = job["polls"]
|
|
if job.get("sse_completed") or jid in self.instant_complete_jobs:
|
|
state = "ACTIVITY_TASK_STATE_COMPLETED"
|
|
elif job["taskQueue"] in self.busy_defer_queues:
|
|
# Simulate STARTED -> SCHEDULED (busy deferral) -> STARTED -> COMPLETED
|
|
if p_idx == 1:
|
|
state = "ACTIVITY_TASK_STATE_STARTED"
|
|
elif p_idx == 2:
|
|
state = "ACTIVITY_TASK_STATE_SCHEDULED"
|
|
job["deferCount"] = max(1, int(job.get("deferCount") or 0))
|
|
job["lastBusyReason"] = job.get("lastBusyReason") or "active_dev_session"
|
|
elif p_idx == 3:
|
|
state = "ACTIVITY_TASK_STATE_STARTED"
|
|
else:
|
|
state = "ACTIVITY_TASK_STATE_COMPLETED"
|
|
else:
|
|
state = (
|
|
"ACTIVITY_TASK_STATE_COMPLETED"
|
|
if p_idx >= 2
|
|
else "ACTIVITY_TASK_STATE_STARTED"
|
|
)
|
|
out_val = (
|
|
self.stage_handler(job["input"])
|
|
if (self.stage_handler is not None and state == "ACTIVITY_TASK_STATE_COMPLETED")
|
|
else {"tok_per_sec": 182.4}
|
|
)
|
|
return ok(
|
|
{
|
|
"id": jid,
|
|
"execution": {"workflowId": jid, "runId": job["runId"]},
|
|
"type": job["type"],
|
|
"taskQueue": job["taskQueue"],
|
|
"status": state,
|
|
"state": state,
|
|
"deferCount": job.get("deferCount", 0),
|
|
"lastBusyReason": job.get("lastBusyReason", ""),
|
|
"result": out_val,
|
|
"output": out_val,
|
|
}
|
|
)
|
|
if len(segs) == 2 and segs[1] == "schedules" and method == "POST":
|
|
b = dict(body or {})
|
|
ns = segs[0]
|
|
sid = str(b.get("scheduleId") or "")
|
|
if not sid:
|
|
raise Problem(400, "scheduleId required")
|
|
b["namespace"] = ns
|
|
b.setdefault("state", {"paused": False, "note": ""})
|
|
b.setdefault("info", {"actionCount": 0, "createTime": "2026-11-23T00:00:00Z"})
|
|
self.workflow_schedules[(ns, sid)] = b
|
|
return ok(b)
|
|
if len(segs) == 2 and segs[1] == "schedules" and method == "GET":
|
|
ns = segs[0]
|
|
rows = [v for (s_ns, _), v in sorted(self.workflow_schedules.items()) if s_ns == ns]
|
|
return ok({"schedules": rows})
|
|
if len(segs) == 3 and segs[1] == "schedules" and method == "GET":
|
|
ns, sid = segs[0], segs[2]
|
|
sch = self.workflow_schedules.get((ns, sid))
|
|
if not sch:
|
|
raise Problem(404, "schedule not found")
|
|
return ok(sch)
|
|
if len(segs) == 3 and segs[1] == "schedules" and method == "DELETE":
|
|
ns, sid = segs[0], segs[2]
|
|
if (ns, sid) not in self.workflow_schedules:
|
|
raise Problem(404, "schedule not found")
|
|
del self.workflow_schedules[(ns, sid)]
|
|
return ok({"status": "deleted"})
|
|
if len(segs) == 4 and segs[1] == "schedules" and segs[3] == "trigger" and method == "POST":
|
|
ns, sid = segs[0], segs[2]
|
|
sch = self.workflow_schedules.get((ns, sid))
|
|
if not sch:
|
|
raise Problem(404, "schedule not found")
|
|
sch["info"]["actionCount"] = int(sch["info"].get("actionCount") or 0) + 1
|
|
req_id = str((body or {}).get("requestId") or "r1")
|
|
return ok(
|
|
{
|
|
"status": "triggered",
|
|
"execution": {"workflowId": f"{sid}-{req_id}", "runId": req_id},
|
|
}
|
|
)
|
|
if len(segs) == 4 and segs[1] == "schedules" and segs[3] == "pause" and method == "POST":
|
|
ns, sid = segs[0], segs[2]
|
|
sch = self.workflow_schedules.get((ns, sid))
|
|
if not sch:
|
|
raise Problem(404, "schedule not found")
|
|
sch["state"] = {"paused": True, "note": str((body or {}).get("note") or "")}
|
|
return ok({"status": "paused"})
|
|
if len(segs) == 4 and segs[1] == "schedules" and segs[3] == "unpause" and method == "POST":
|
|
ns, sid = segs[0], segs[2]
|
|
sch = self.workflow_schedules.get((ns, sid))
|
|
if not sch:
|
|
raise Problem(404, "schedule not found")
|
|
sch["state"] = {"paused": False, "note": ""}
|
|
return ok({"status": "running"})
|
|
raise Problem(404, "unknown workflow route")
|
|
name = path.removeprefix("/v1/research/")
|
|
if name == "evidence" and method == "POST":
|
|
b = body or {}
|
|
if not b.get("uri") or b.get("content") is None:
|
|
raise Problem(422, "evidence needs uri and content")
|
|
idem = (headers.get("Idempotency-Key") if headers else None) or ""
|
|
if idem and (who["org"], idem) in self.idempotency:
|
|
return ok(self.idempotency[(who["org"], idem)])
|
|
chain = self.evidences.setdefault((who["org"], b["uri"]), [])
|
|
if chain and chain[0].get("hash") and chain[0]["hash"] == b.get("hash"):
|
|
if idem:
|
|
self.idempotency[(who["org"], idem)] = chain[0]
|
|
return ok(chain[0])
|
|
ver = (chain[0]["version"] + 1) if chain else 1
|
|
rec = {
|
|
"id": f"ev-{who['org']}-{ver}",
|
|
"uri": b["uri"],
|
|
"hash": b.get("hash") or "",
|
|
"kind": b.get("kind") or "element",
|
|
"source": b.get("source") or {"system": "hanzo"},
|
|
"links": b.get("links") or [],
|
|
"content": b["content"],
|
|
"time": "2026-11-22T00:00:00Z",
|
|
"version": ver,
|
|
"by": who["user"] or who["org"],
|
|
}
|
|
chain.insert(0, rec)
|
|
if idem:
|
|
self.idempotency[(who["org"], idem)] = rec
|
|
return ok(rec)
|
|
if name == "evidence" and method == "GET":
|
|
uri = q.get("uri") or ""
|
|
rows = list(self.evidences.get((who["org"], uri), []))
|
|
return ok({"data": rows, "total": len(rows)})
|
|
if method == "POST" and name in KEYS:
|
|
return ok(self.put(who, name, body or {}))
|
|
if method == "GET" and name in ("benchmarks", "studies", "papers"):
|
|
rows = self.canonical(who["org"], name)
|
|
for f in ("id", "project", "study"):
|
|
if q.get(f):
|
|
rows = [r for r in rows if r.get(f) == q[f]]
|
|
if q.get("cites"):
|
|
rows = [r for r in rows if q["cites"] in (r.get("cites") or [])]
|
|
return ok({"data": rows, "total": len(rows)})
|
|
if name == "compare":
|
|
return ok(self.compare(who, q))
|
|
if name == "grants":
|
|
return ok(self.grant(who, body or {}))
|
|
if name == "experiments/versions" and method == "GET":
|
|
exp_id = q.get("id") or ""
|
|
rows = [
|
|
e
|
|
for e in self.experiments.get(who["org"], [])
|
|
if e.get("id") == exp_id and (not q.get("project") or e["project"] == q["project"])
|
|
]
|
|
return ok({"project": q.get("project") or who["project"], "id": exp_id, "versions": rows})
|
|
if name == "experiments" and method == "POST":
|
|
return ok(self.ingest(who, body or {}))
|
|
if name == "experiments":
|
|
all_rows = [
|
|
e for e in self.experiments.get(who["org"], []) if not q.get("project") or e["project"] == q["project"]
|
|
]
|
|
if q.get("include_retracted") == "true":
|
|
rows = all_rows
|
|
else:
|
|
latest_by_id: dict[str, dict[str, Any]] = {}
|
|
for e in all_rows:
|
|
latest_by_id[e["id"]] = e
|
|
rows = [
|
|
e
|
|
for e in latest_by_id.values()
|
|
if (e.get("revision") or "").lower() != "retracted"
|
|
and (e.get("status") or "").lower() != "retracted"
|
|
]
|
|
return ok({"data": rows, "total": len(rows)})
|
|
if name == "totals":
|
|
ex = self.experiments.get(who["org"], [])
|
|
return ok(
|
|
{
|
|
"projects": 1,
|
|
"experiments": len({e["id"] for e in ex}),
|
|
"experiments_retained": len(ex),
|
|
"attempts": len(self.attempts.get(who["org"], [])),
|
|
"by_kind": None,
|
|
}
|
|
)
|
|
if name == "projects":
|
|
return ok(
|
|
{"data": [{"project": "default", "experiments": len(self.experiments.get(who["org"], []))}], "total": 1}
|
|
)
|
|
if name == "artifacts" and method == "POST":
|
|
return ok(self.put_artifact(who, body or {}))
|
|
if name == "artifacts":
|
|
rows = [
|
|
{k: v for k, v in a.items() if k != "_bytes"}
|
|
for a in self.artifacts.get(who["org"], {}).values()
|
|
if not q.get("run") or a["run_id"] == q["run"]
|
|
]
|
|
return ok({"data": rows, "total": len(rows)})
|
|
if name.startswith("artifacts/"):
|
|
a = self.artifacts.get(who["org"], {}).get(name.removeprefix("artifacts/"))
|
|
if not a:
|
|
raise Problem(404, "artifact not found")
|
|
return a["_bytes"]
|
|
raise Problem(404, "Not Found")
|
|
|
|
def arena(self, method: str, path: str, q: dict[str, str], body: Any, who: dict[str, Any] | None) -> Any:
|
|
name = path.removeprefix("/v1/benchmark/")
|
|
if name == "catalog":
|
|
return 200, {"data": CATALOG, "total": len(CATALOG)}
|
|
if name == "leaderboard":
|
|
return 200, self.leaderboard(q)
|
|
if name == "compare":
|
|
if not q.get("a") or not q.get("b"):
|
|
raise Problem(400, "compare needs ?a= and ?b= model ids")
|
|
return 200, {
|
|
"benchmark": q.get("benchmark") or "gpqa_diamond",
|
|
"a": q["a"],
|
|
"b": q["b"],
|
|
"n_common": 0,
|
|
"a_correct": 0,
|
|
"b_correct": 0,
|
|
"rescue_a_over_b": 0,
|
|
"rescue_b_over_a": 0,
|
|
"net_a_minus_b": 0,
|
|
"mcnemar_p": 1,
|
|
}
|
|
if name == "history":
|
|
return 200, {"benchmark": q.get("benchmark") or "gpqa_diamond", "data": [], "total": 0}
|
|
if name == "claims" and method == "POST":
|
|
return 200, self.put_claims(who, body or {})
|
|
if name == "claims":
|
|
rows = [c for c in self.claims if c["visibility"] == "public" or (who and c["org"] == who["org"])]
|
|
for f in ("benchmark", "model", "provider", "source", "protocol", "org"):
|
|
if q.get(f):
|
|
rows = [c for c in rows if c.get(f) == q[f]]
|
|
return 200, {"data": rows, "total": len(rows)}
|
|
if name == "presets" and method == "POST":
|
|
if who is None:
|
|
raise Problem(401, "sign in")
|
|
p = dict(body or {}, owner=who["org"])
|
|
if not p.get("name") or not p.get("arms"):
|
|
raise Problem(400, "preset needs a name and at least one arm")
|
|
p["rank"] = p.get("rank") or p["arms"]
|
|
p["panel"] = max(1, int(p.get("panel") or 1))
|
|
return 202, {
|
|
"status": "accepted",
|
|
"served_as": "enso-" + p["name"],
|
|
"preset": p,
|
|
"by": who["user"],
|
|
"note": "n",
|
|
}
|
|
if name == "presets":
|
|
return 200, {"data": [{"name": "ultra", "owner": "hanzo", "arms": ["a"], "rank": ["a"], "panel": 3}]}
|
|
if name == "runs" and method == "POST":
|
|
if who is None:
|
|
raise Problem(401, "sign in")
|
|
if not (body or {}).get("model"):
|
|
raise Problem(400, "run needs a model id")
|
|
native = {c["id"]: c["native"] for c in CATALOG}
|
|
data = []
|
|
for b in body.get("benchmarks") or []:
|
|
if b not in native:
|
|
raise Problem(422, f"unknown benchmarks: {b}")
|
|
held = [a for a in self.attempt_rows if a["benchmark"] == b and a["model"] == body["model"]]
|
|
if held:
|
|
n, ok = len(held), sum(1 for a in held if a["correct"])
|
|
data.append(
|
|
{
|
|
"benchmark": b,
|
|
"latest": {"run": held[-1]["run"], "score": 100 * ok / n, "n": n},
|
|
"note": "measured",
|
|
}
|
|
)
|
|
else:
|
|
data.append(
|
|
{
|
|
"benchmark": b,
|
|
"latest": None,
|
|
"harness": "hanzoai/enso-bench" if native[b] else "",
|
|
"note": "nothing was queued",
|
|
}
|
|
)
|
|
return 200, {"org": who["org"], "by": who["user"], "model": body["model"], "data": data}
|
|
raise Problem(404, "Not Found")
|
|
|
|
|
|
class Server:
|
|
"""The fake on a loopback port: `with Server() as s: Research(base=s.url, api_key=...)`."""
|
|
|
|
def __init__(self) -> None:
|
|
self.fake = Fake()
|
|
fake = self.fake
|
|
|
|
class Handler(BaseHTTPRequestHandler):
|
|
def log_message(self, *a: Any) -> None:
|
|
pass
|
|
|
|
def answer(self) -> None:
|
|
if self.command == "GET" and self.path.split("?", 1)[0] == "/v1/workflow/events":
|
|
raw_tok = (self.headers.get("Authorization") or "").removeprefix("Bearer ")
|
|
if raw_tok in fake.expired:
|
|
self.send_response(401)
|
|
self.end_headers()
|
|
return
|
|
who = fake.principal(self.headers)
|
|
if who is None:
|
|
self.send_response(403)
|
|
self.end_headers()
|
|
return
|
|
fake.sse_streams_opened += 1
|
|
self.send_response(200)
|
|
self.send_header("Content-Type", "text/event-stream")
|
|
self.send_header("Cache-Control", "no-cache")
|
|
self.end_headers()
|
|
self.wfile.write(b": hanzo-tasks event stream\n\n")
|
|
self.wfile.flush()
|
|
if fake.sse_hang_sec > 0:
|
|
import time as _t
|
|
|
|
_t.sleep(fake.sse_hang_sec)
|
|
return
|
|
with fake.lock:
|
|
pending = list(fake.workflow_jobs.items())
|
|
for (ns, jid), job in pending:
|
|
rid = job.get("runId") or jid
|
|
if job["taskQueue"] in fake.busy_defer_queues:
|
|
job["deferCount"] = max(1, int(job.get("deferCount") or 0))
|
|
job["lastBusyReason"] = job.get("lastBusyReason") or "active_dev_session"
|
|
defer_ev = {
|
|
"kind": "activity.deferred",
|
|
"namespace": ns,
|
|
"workflow_id": jid,
|
|
"run_id": rid,
|
|
"data": {
|
|
"deferCount": job["deferCount"],
|
|
"lastBusyReason": job["lastBusyReason"],
|
|
},
|
|
}
|
|
self.wfile.write(
|
|
f"event: activity.deferred\ndata: {json.dumps(defer_ev)}\n\n".encode()
|
|
)
|
|
job["sse_completed"] = True
|
|
comp_ev = {
|
|
"kind": "activity.completed",
|
|
"namespace": ns,
|
|
"workflow_id": jid,
|
|
"run_id": rid,
|
|
"data": {"status": "ACTIVITY_TASK_STATE_COMPLETED"},
|
|
}
|
|
self.wfile.write(
|
|
f"event: activity.completed\ndata: {json.dumps(comp_ev)}\n\n".encode()
|
|
)
|
|
self.wfile.flush()
|
|
return
|
|
n = int(self.headers.get("Content-Length") or 0)
|
|
status, ctype, out = fake.handle(
|
|
self.command, self.path, self.headers, self.rfile.read(n) if n else b""
|
|
)
|
|
self.send_response(status)
|
|
if status == 302:
|
|
self.send_header("Location", "https://example.com/elsewhere")
|
|
self.send_header("Content-Type", ctype)
|
|
self.send_header("Content-Length", str(len(out)))
|
|
self.end_headers()
|
|
self.wfile.write(out)
|
|
|
|
do_GET = do_POST = do_DELETE = answer
|
|
|
|
self.httpd = ThreadingHTTPServer(("127.0.0.1", 0), Handler)
|
|
self.url = f"http://127.0.0.1:{self.httpd.server_address[1]}"
|
|
self.thread = threading.Thread(target=self.httpd.serve_forever, args=(0.01,), daemon=True)
|
|
|
|
def __enter__(self) -> Server:
|
|
self.thread.start()
|
|
return self
|
|
|
|
def __exit__(self, *a: Any) -> None:
|
|
self.httpd.shutdown()
|
|
self.httpd.server_close()
|