fix(orchestrator): keep installs off a running hub row and fail orphaned jobs

The runner records the hub row it runs in runner-job.json of the hub
state directory (new --state flag, set by the runner unit) and drops it
when the row ends. orchestrator.sh idle exits 1 with the row's task and
job id while that file names a live pid; install, stop, runner and the
home install.sh check it first, because stopping the runner would kill
the row. A starting runner posts /api/runner/restart, and the hub fails
every job an earlier runner left running, closing its row like a failed
run with the reason that the runner restarted.

🎯 Fix: 92 %
💥 Break: 4 %
💸 Tokens: 100 %
⏱️ Velocity: –
🛡️ Security: 100 %
🏆 Score: 88
🚦 Gate: ✅ 90/5, tokens and velocity <= 100, security 100 %

Created-with: https://s.veen.world/skills
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Kevin Veen-BirkenbachandClaude Opus 5.5 committed 2026-11-27 10:44:15 +00:00
1 parent 0f8cb32198
commit 30862411f3
15 files changed
+411 -56

No files matched your search

+5 -2
View File
@@ -29,6 +29,7 @@ BOARD = re.compile(r"/api/board/([a-z-]{1,32})")
UPLOADS = "/api/uploads"
MATCH_ROUTE = "/api/match"
PREVIEWS_ROUTE = "/api/runner/previews"
RESTART_ROUTE = "/api/runner/restart"
MATCH_POSTS = {"/api/match/stale": "match_stale", "/api/match/snapshot": "match_snapshot"}
HOOK_POSTS = {"/api/asks": "ask", "/api/decisions": "question"}
POSTS = (*HOOK_POSTS, UPLOADS, "/api/messages", *MATCH_POSTS)
@@ -283,7 +284,9 @@ class Handler(Exchange):
def runner(self, path: str, body: bytes) -> None:
hub = self.server.hub
route = RUNNER_ROUTE.fullmatch(path)
if route is None:
if path == RESTART_ROUTE:
self.answer(hub.restart_runner, HTTPStatus.OK)
elif route is None:
self.answer(lambda: hub.record_preview(body), HTTPStatus.OK)
elif route.group(2) == "claim":
self.answer(lambda: hub.claim(route.group(1)), HTTPStatus.OK)
@@ -303,7 +306,7 @@ class Handler(Exchange):
return lambda body: self.answer(lambda: receive(body), HTTPStatus.OK if path in MATCH_POSTS else HTTPStatus.CREATED)
self.send_text(HTTPStatus.FORBIDDEN, "forbidden: local POST with the X-Kvb-Orchestrator header and no foreign origin only")
return None
if RUNNER_ROUTE.fullmatch(path) or path == PREVIEWS_ROUTE:
if RUNNER_ROUTE.fullmatch(path) or path in (PREVIEWS_ROUTE, RESTART_ROUTE):
if self.client():
return lambda body: self.runner(path, body)
self.send_text(HTTPStatus.FORBIDDEN, "forbidden: the runner calls with the X-Kvb-Orchestrator header and no origin only")
+37 -6
View File
@@ -15,6 +15,7 @@ TASK_JOB = "task"
GATE_JOB = "gate"
CHANGES = (TASK_JOB, GATE_JOB)
UNRUNNABLE = "The hub runner needs a command and an absolute directory, and this row lacks one; run it with operator.py run in a terminal."
RESTARTED = "the runner restarted while this job ran; run the row again"
def runner_job(job: dict) -> dict:
@@ -148,11 +149,39 @@ class Jobs:
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, checked is True)
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}})
return self.ended(job, checked is True)
def restart_runner(self) -> dict:
"""Returns:
{"failed": ids}: every running job, which the restart of the
runner killed, turned failed with RESTARTED as its output, code
None and finished now, its row closed like a failed run.
"""
failed = []
for job in self.jobs.listed():
record, changed = self.jobs.transition(job["id"], "running", {"status": "failed", "code": None, "output": RESTARTED, "finished": time.time()})
if changed:
self.ended(record, False)
failed.append(job["id"])
return {"failed": failed}
def ended(self, job: dict, checked: bool) -> dict:
"""Args:
job: a job that just left running, with its status, code and
output.
checked: as in close_row().
Returns:
{"id", "status", "code", "runs", "told"} after its row closed
(close_row()), the run went to the log and, for a task change,
its batch was told (tell_applied()).
"""
status, code = job["status"], job["code"]
runs = self.close_row(job, status, code, checked)
tail = "\n".join(job["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}
return {"id": job["id"], "status": status, "code": code, "runs": runs, "told": told}
def close_row(self, job: dict, status: str, code: int | None, checked: bool) -> list[dict]:
"""Args:
@@ -165,14 +194,16 @@ class Jobs:
job on its row: `ran` on exit 0, with --check-failed unless
checked, so the row stays open; `failed` with where the output
stays, the job's console, never the output itself, on any other
exit; none for a refused job or a task or gate change.
exit and, why RESTARTED, on a failed job without code, which a
runner restart killed; 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}"
why = RESTARTED if code is None else f"killed by signal {-code}" if code < 0 else f"exit {code}"
where = f"the console of hub job {job['id']} under the row in Waits on Operator"
return [self.scripts.run(OPERATOR_SCRIPT, ["failed", *shown, "--why", why, f"--where={where}"])]
return [self.scripts.run(OPERATOR_SCRIPT, ["ran", *shown] if checked else ["ran", *shown, "--check-failed"])]