Files
skills/orchestrator/kvb_orchestrator/ledger.py
T
Kevin Veen-BirkenbachandClaude Opus 5.5 6f940b4415 feat(orchestrator): rename kvb-hub to Agent Orchestrator with the phase 2 surface
Surface (requirement 001 phase 2): navigation Stream (Inbox, Regeln) / Agents / Artifacts with keys 1-3 and hash routes (#graph and #tree redirect), header with title, command palette and settings only, server-side badges (unsnoozed inbox, beats within 900 s, open jobs), a Mayday banner with "aufheben", four stream chips with agent:/art:/seit: search, Filter and time menus, the inbox in main, an agents toolbar (Funk an alle with Newsletter, Broadcast, Pan-Pan, Security, Mayday; Priorität; Tag/Nacht), Post or Call in the agent panel, Ambiente in the settings and read-aloud over the seven classes with Mayday locked.

Rename (decision 9): hub/ is orchestrator/, the package kvb_orchestrator, the unit kvb-orchestrator-runner.service, scripts/orchestrator.sh, environment KVB_ORCHESTRATOR_*, make targets orchestrator-*, the MCP server, image and compose project kvb-orchestrator, the guard header X-Kvb-Orchestrator and the message marker [kvb-orchestrator msg <id>]. install runs migrate first: it stops the old container, removes its image and code, moves state, approvals and pinned keys, swaps the MCP registration, the PermissionRequest hook and the runner unit, and changes nothing on a second run (scratch-HOME test with stub docker, claude and systemctl). Saved views fall back to the old localStorage key once.

MEDUSA acceptances carried to the renamed paths: 82 rows whose lines are unchanged (reason prefixed "renamed from <old path>, line unchanged"), and 7 rows whose text changed only in the product name, the package name, a temp prefix or a new class argument, each upheld by one kvb-think-criticize skeptic (approval.py:60, messaging.py:53, server.py:56, tests/hub_support.py:64, passkey.py:205, page-agent.js:75, language.py:46).

Cost 101 %: the longer name adds 15 tokens of skill text; the operator approved it with the commit.

🎯 Fix: 92 %
💥 Break: 4 %
💸 Cost: 101 %
⏱️ Time: –
🛡️ Security: 100 %
💰 Value: ?
🏆 Score: 87
🚦 Gate: ⚠️ cost 101 % > 100: ask the operator before the commit

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-06 22:20:06 +00:00

124 lines
4.5 KiB
Python

from __future__ import annotations
import json
import secrets
import threading
from pathlib import Path
from .records import read_json, stamp, write_atomic
class Ledger:
def __init__(self, file: Path) -> None:
self.file = file
self.changed = threading.Condition()
self.items: dict[str, dict] = {}
if file.is_file():
self.items = json.loads(file.read_text(encoding="utf-8"))
def fresh(self) -> str:
with self.changed:
item_id = secrets.token_hex(8)
while item_id in self.items:
item_id = secrets.token_hex(8)
return item_id
def write(self, items: dict[str, dict]) -> None:
write_atomic(self.file, json.dumps(items, indent=1, sort_keys=True).encode("utf-8"))
self.items = items
def insert(self, item_id: str, fields: dict) -> dict:
with self.changed:
record = {**fields, "id": item_id, "created": stamp()}
self.write({**self.items, item_id: record})
return dict(record)
def add(self, fields: dict) -> dict:
with self.changed:
return self.insert(self.fresh(), fields)
def get(self, item_id: str) -> dict | None:
with self.changed:
record = self.items.get(item_id)
return None if record is None else dict(record)
def listed(self) -> list[dict]:
with self.changed:
records = [dict(record) for record in self.items.values()]
return sorted(records, key=lambda record: (record["created"], record["id"]))
def pending(self) -> list[dict]:
return [record for record in self.listed() if record.get("status") == "pending"]
def transition(self, item_id: str, before: str, changes: dict) -> tuple[dict | None, bool]:
with self.changed:
record = self.items.get(item_id)
if record is None:
return None, False
if record.get("status") != before:
return dict(record), False
updated = {**record, **changes}
self.write({**self.items, item_id: updated})
self.changed.notify_all()
return dict(updated), True
def decide(self, item_id: str, changes: dict) -> tuple[dict | None, bool]:
return self.transition(item_id, "pending", {**changes, "decided": stamp()})
def change(self, item_id: str, changes: dict) -> dict | None:
with self.changed:
record = self.items.get(item_id)
if record is None:
return None
updated = {**record, **changes}
self.write({**self.items, item_id: updated})
return dict(updated)
def remove(self, item_id: str) -> bool:
with self.changed:
if item_id not in self.items:
return False
self.write({key: value for key, value in self.items.items() if key != item_id})
return True
def wait(self, item_id: str, timeout: float) -> dict | None:
with self.changed:
if item_id not in self.items:
return None
self.changed.wait_for(lambda: self.items[item_id].get("status") != "pending", timeout)
return dict(self.items[item_id])
class Reviews(Ledger):
def __init__(self, state: Path) -> None:
super().__init__(state / "reviews.json")
self.pages = state / "pages"
self.pages.mkdir(exist_ok=True)
def stored(self, review_id: str, suffix: str) -> Path:
return self.pages / f"{review_id}{suffix}"
def submit(self, fields: dict[str, str], page: bytes, files: list[dict]) -> dict:
with self.changed:
review_id = self.fresh()
write_atomic(self.stored(review_id, ".html"), page)
write_atomic(self.stored(review_id, ".json"), json.dumps({"files": files}).encode("utf-8"))
return self.insert(review_id, {**fields, "files": len(files), "status": "pending", "decided": None})
class Marks:
def __init__(self, file: Path) -> None:
self.file = file
self.lock = threading.Lock()
data = read_json(file)
self.items: dict[str, dict] = data if isinstance(data, dict) else {}
def put(self, key: str, mark: dict) -> None:
with self.lock:
self.items = {**self.items, key: mark}
write_atomic(self.file, json.dumps(self.items, sort_keys=True).encode("utf-8"))
def get(self, key: str) -> dict:
with self.lock:
return dict(self.items.get(key, {"until": 0.0, "pinned": False}))