Files
skills/orchestrator/kvb_orchestrator/skill_scripts.py
T
Kevin Veen-BirkenbachandClaude Opus 5.5 f356083a24 feat(orchestrator): hub requirements 066 to 082 and 084
Radio footer, rules page, schedules editor (System timers and Claude
tasks through signed runner jobs), account usage bar, usage explorer
with a streamed usage.jsonl, queue Controls tab and override, running
tasks on the board, stale task prune with the generic dial, greyed card
stamps, commit-tree diffs from the runner preview, VS Code path links,
expired asks and decisions that stay as EXPIRED cards (asks keyed by a
call digest; an async PostToolUse hook closes those answered in the
terminal), the Match refresh hourglass, attachments by paste, drop and
paperclip in every text field (page CSP gains img-src blob: for the
thumbnails) and the limits board with profiles and a passkey to loosen.
Fixes: one red accept on a red-gate commit and no release while a risk
is open, the send bar footer segments share one height, the mic button
survives a redraw, new Match cards queue before the release card.

E2E specs were still running at commit time (operator order); their
failures follow as separate fixes.

🎯 Fix: 88 %
💥 Break: 8 %
💸 Tokens: –
⏱️ Velocity: –
🛡️ Security: 100 %
🏆 Score: 81
🚦 Gate: ⛔ fix 88 % < 90, break 8 % > 5 (committed on the operator's word)

Created-with: https://s.veen.world/skills
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-27 19:05:38 +00:00

120 lines
4.1 KiB
Python

from __future__ import annotations
import functools
import importlib.util
import os
import re
import subprocess
import sys
from dataclasses import dataclass
from pathlib import Path
from types import ModuleType
from typing import Protocol
SCRIPT_TIMEOUT = 60
OUTPUT_LIMIT = 4000
FIRST_LINE_LIMIT = 200
ELIDED = "..."
SAFE_PATH = "/usr/local/bin:/usr/bin:/bin"
CONFIG_ENV = "AGENTS_COUNCIL_CONFIG"
RADIO = "kvb-org-radio/scripts/radio.py"
RASCI = "kvb-org-rasci/scripts/rasci.py"
NIGHT = "kvb-org-suspend/scripts/night.py"
OPERATOR_SCRIPT = "kvb-org-operator/scripts/operator.py"
ESTIMATE = "kvb-org-queue-estimate/scripts/estimate.py"
def bounded(stdout: str) -> str:
"""Returns:
stdout when it fits OUTPUT_LIMIT, else its first line, whole or not
at all once over FIRST_LINE_LIMIT, ELIDED and the last OUTPUT_LIMIT
characters of the rest: the first line a caller parses, like the ts
radio.py prints, outlives a long output.
"""
if len(stdout) <= OUTPUT_LIMIT:
return stdout
first, _, rest = stdout.partition("\n")
return "\n".join([first if len(first) <= FIRST_LINE_LIMIT else "", ELIDED, rest[-OUTPUT_LIMIT:]])
def script_env(home: Path, council: Path) -> dict[str, str]:
"""Returns:
The whole environment of a skill script: PATH, HOME, the council
and, when the hub has one, CONFIG_ENV, the read-only mount of the
queue config compose.yml passes; nothing else of the hub's.
"""
config = {CONFIG_ENV: os.environ[CONFIG_ENV]} if CONFIG_ENV in os.environ else {}
return {"PATH": SAFE_PATH, "HOME": str(home), "AGENTS_COUNCIL_ROOT": str(council), "PYTHONDONTWRITEBYTECODE": "1", **config}
def script_module(script: Path) -> ModuleType | None:
"""Returns:
The installed script as a module, loaded again after an install
changed it, None when it is missing or does not load.
"""
try:
changed = script.stat().st_mtime_ns
except OSError:
return None
return loaded(script, changed)
def task_pattern(skills: Path) -> re.Pattern[str] | None:
"""Args:
skills: the installed skills directory, the hub's --skills mount.
Returns:
TASK of the installed kvb-org-rasci rasci.py, the one rule for
register task ids; None when the script is missing, does not load or
holds no compiled pattern.
"""
found = getattr(script_module(skills / RASCI), "TASK", None)
return found if isinstance(found, re.Pattern) else None
@functools.cache
def loaded(script: Path, changed: int) -> ModuleType | None:
spec = importlib.util.spec_from_file_location(f"kvb_orchestrator_{script.stem}_{changed}", script)
if spec is None:
return None
module = importlib.util.module_from_spec(spec)
try:
spec.loader.exec_module(module)
except (OSError, ImportError, RuntimeError, SyntaxError):
return None
return module
class Runner(Protocol):
skills: Path
def run(self, script: str, args: list[str]) -> dict: ...
@dataclass(frozen=True)
class Scripts:
skills: Path
council: Path
home: Path
def run(self, script: str, args: list[str]) -> dict:
command = " ".join([Path(script).name, *args])
env = script_env(self.home, self.council)
try:
done = subprocess.run(
[sys.executable, str(self.skills / script), *args],
capture_output=True,
encoding="utf-8",
errors="replace",
timeout=SCRIPT_TIMEOUT,
env=env,
stdin=subprocess.DEVNULL,
check=False,
)
except subprocess.TimeoutExpired as error:
partial = error.stdout.decode("utf-8", errors="replace") if isinstance(error.stdout, bytes) else error.stdout or ""
return {"command": command, "code": -1, "stdout": bounded(partial), "stderr": str(error)}
except OSError as error:
return {"command": command, "code": -1, "stdout": "", "stderr": str(error)}
return {"command": command, "code": done.returncode, "stdout": bounded(done.stdout), "stderr": done.stderr[-OUTPUT_LIMIT:]}