Files
skills/orchestrator/kvb_orchestrator/skill_scripts.py
T
Kevin Veen-BirkenbachandClaude Opus 5.5 30b2a32095 feat(orchestrator): radio receipts inline (005 phase B)
Requirement 005: every radio message in the Stream and the radio log shows the avatars of the agents that read it, with read time and an ack badge where a receipt exists; receipt rows no longer appear as entries of their own, and the sender shows once. Read times come from seen marks, stamped only after a fresh presence pass no older than two sighting intervals; a stored time before its row is refused. An operator message links to its relay row through the ts radio.py prints first on stdout right after store(), checked against an exact row (sender Operator, relay text, targets, no to_sessions); a ts shared by two rows shows no readers. radio.py clips printed addresses; the hub keeps the first stdout line whole. Accepted MEDUSA false positives: MM-SCAN-058 at server.py:73, JB-SCAN-641 and JB-SCAN-616 at radio.py:389 (clip, the output sanitizer), FLA-SCAN-055 at radio.py:109. Committed below the 90/5 bar on the operator's word; the receipts e2e runs through the queue.

🎯 Fix: 89 %
💥 Break: 6 %
💸 Cost: –
⏱️ Time: –
🛡️ Security: 100 %
🏆 Score: 84
🚦 Gate: ⛔ fix 89 % < 90, break 6 % > 5

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 17:19:39 +00:00

94 lines
3.2 KiB
Python

from __future__ import annotations
import functools
import importlib.util
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"
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_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)
@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 = {"PATH": SAFE_PATH, "HOME": str(self.home), "AGENTS_COUNCIL_ROOT": str(self.council), "PYTHONDONTWRITEBYTECODE": "1"}
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:]}