The host runner computes a merge preview for each operator row that merges or commits and posts it to the runner-only /api/runner/previews, and an ask of git commit ships the staged commit tree; Match cards show what the merge brings in onto HEAD, or why no preview exists. Cards fly off with their verdict stamp unless reduced motion is set, Match gets its own button in Tasks and a Home tile that keeps the live stack, and the 3D graph and the faces fix land with it. 🎯 Fix: 85 % 💥 Break: 10 % 💸 Tokens: – ⏱️ Velocity: – 🛡️ Security: 100 % 🏆 Score: 77 🚦 Gate: ⛔ fix 85 % < 90, break 10 % > 5 Created-with: https://s.veen.world/skills Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
123 lines
6.2 KiB
Python
123 lines
6.2 KiB
Python
from __future__ import annotations
|
|
|
|
import time
|
|
|
|
from .errors import Conflict, Missing, ToolError
|
|
from .fields import bounded, decode, expect, field
|
|
from .register import OPERATOR, REGISTER, operator_rows, read_register, runnable
|
|
from .signed import operator_signed
|
|
from .skill_scripts import OPERATOR_SCRIPT, OUTPUT_LIMIT
|
|
|
|
JOB_OPEN = ("queued", "running")
|
|
RUN_OUTPUT = 64 << 10
|
|
CONSOLE = ("id", "status", "code", "output", "created_ts", "started", "finished")
|
|
TASK_JOB = "task"
|
|
GATE_JOB = "gate"
|
|
CHANGES = (TASK_JOB, GATE_JOB)
|
|
USAGE = 2
|
|
|
|
|
|
def runner_job(job: dict) -> dict:
|
|
"""Returns:
|
|
What the runner claims: an operator command, with head when a Match
|
|
release bound it to a merge preview of that HEAD, or a task or gate
|
|
change with kind and argv; a runner that predates such changes
|
|
verifies them as an operator command and refuses them.
|
|
"""
|
|
change = {"kind": job["kind"], "argv": job["argv"]} if job.get("kind") in CHANGES else {}
|
|
head = {"head": job["head"]} if job.get("head") else {}
|
|
return {**{key: job[key] for key in ("id", "task", "command", "directory", "sha256")}, **change, **head, "proof": job.get("proof")}
|
|
|
|
|
|
def console_of(job: dict | None) -> dict | None:
|
|
return None if job is None else {**{key: job[key] for key in CONSOLE}, "output": job["output"][-OUTPUT_LIMIT:]}
|
|
|
|
|
|
class Jobs:
|
|
def open_jobs(self) -> set[str]:
|
|
return {job["task"] for job in self.jobs.listed() if job["status"] in JOB_OPEN}
|
|
|
|
def consoles(self) -> dict[str, dict]:
|
|
latest: dict[str, dict] = {}
|
|
for job in sorted(self.jobs.listed(), key=lambda job: job["created_ts"]):
|
|
latest[job["task"]] = job
|
|
# Trip-wire: jobs queued before the inbox console carry no dismissed key and must not flood the inbox after an upgrade.
|
|
return {task: job for task, job in latest.items() if job.get("dismissed") is False}
|
|
|
|
def dismiss(self, body: bytes) -> dict:
|
|
payload = decode(body)
|
|
expect(payload, ("id",))
|
|
job_id = field(payload, "id")
|
|
record = self.jobs.get(job_id)
|
|
if record is None:
|
|
raise Missing(job_id)
|
|
if record["status"] in JOB_OPEN:
|
|
raise Conflict({"id": job_id, "status": record["status"]})
|
|
self.jobs.change(job_id, {"dismissed": True})
|
|
return {"id": job_id, "dismissed": True}
|
|
|
|
def queue_job(self, item: dict, proof: dict | None) -> str:
|
|
row = next((row for row in operator_rows(read_register(self.council / REGISTER)) if row["task"] == item["id"]), None)
|
|
if row is None or not runnable(row) or operator_signed(row) != item["signed"]:
|
|
return "refused: the row changed since the grant"
|
|
if item["id"] in self.open_jobs():
|
|
return "already running"
|
|
head = self.previewed_head(row["task"], row["command"]) if item.get("bound") else ""
|
|
job = {"task": row["task"], "command": row["command"], "directory": row["directory"], "sha256": item["sha256"], "status": "queued", "proof": proof, "head": head}
|
|
self.jobs.add({**job, "code": None, "output": "", "created_ts": time.time(), "started": None, "finished": None, "dismissed": False})
|
|
return "handed to the runner"
|
|
|
|
def runner_jobs(self) -> dict:
|
|
return {"jobs": [runner_job(job) for job in self.jobs.listed() if job["status"] == "queued"]}
|
|
|
|
def claim(self, job_id: str) -> dict:
|
|
job, changed = self.jobs.transition(job_id, "queued", {"status": "running", "started": time.time()})
|
|
if job is None:
|
|
raise Missing(job_id)
|
|
if not changed:
|
|
raise Conflict({"id": job_id, "status": job["status"]})
|
|
return runner_job(job)
|
|
|
|
def report(self, job_id: str, body: bytes) -> dict:
|
|
payload = decode(body)
|
|
expect(payload, ("output", "code", "refused"))
|
|
output, code, refused = bounded(payload, "output", RUN_OUTPUT), payload["code"], payload["refused"]
|
|
if not isinstance(refused, bool) or (code is not None and (isinstance(code, bool) or not isinstance(code, int))):
|
|
raise ToolError("code must be a whole number or null and refused true or false")
|
|
record = self.jobs.get(job_id)
|
|
if record is None:
|
|
raise Missing(job_id)
|
|
if record["status"] != "running":
|
|
raise Conflict({"id": job_id, "status": record["status"]})
|
|
if code is None and not refused:
|
|
self.jobs.change(job_id, {"output": output})
|
|
return {"id": job_id, "status": "running", "code": None}
|
|
status = "refused" if refused else "done" if code == 0 else "failed"
|
|
job, changed = self.jobs.transition(job_id, "running", {"status": status, "code": code, "output": output, "finished": time.time()})
|
|
if not changed:
|
|
raise Conflict({"id": job_id, "status": job["status"]})
|
|
runs = self.close_row(job, status, code)
|
|
tail = "\n".join(output.splitlines()[-20:])
|
|
self.log.add({"kind": "run", "title": f"{status}: {job['command'][:200]}", "text": tail, "ref": {"job": job_id, "task": job["task"], "code": code, "status": status}})
|
|
told = self.tell_applied(job) if job.get("kind") == TASK_JOB else []
|
|
return {"id": job_id, "status": status, "code": code, "runs": runs, "told": told}
|
|
|
|
def close_row(self, job: dict, status: str, code: int | None) -> list[dict]:
|
|
"""Returns:
|
|
The operator.py runs that report the end of an operator command
|
|
job on its row: `ran` on exit 0, then an older operator.py's
|
|
`done` when `ran` is its usage error; `failed` on any other exit;
|
|
none for a refused job or a task or gate change.
|
|
"""
|
|
if job.get("kind") in CHANGES or status not in ("done", "failed"):
|
|
return []
|
|
slug = job["task"].removeprefix(OPERATOR)
|
|
shown = [slug, f"--command={job['command']}", f"--dir={job['directory']}"]
|
|
if status == "failed":
|
|
why = f"killed by signal {-code}" if code < 0 else f"exit {code}"
|
|
return [self.scripts.run(OPERATOR_SCRIPT, ["failed", *shown, "--why", why])]
|
|
runs = [self.scripts.run(OPERATOR_SCRIPT, ["ran", *shown])]
|
|
if runs[0]["code"] == USAGE:
|
|
runs.append(self.scripts.run(OPERATOR_SCRIPT, ["done", slug]))
|
|
return runs
|