Files
skills/orchestrator/kvb_orchestrator/calibration.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

93 lines
3.4 KiB
Python

from __future__ import annotations
import datetime
import json
import math
import threading
from pathlib import Path
from .fields import decode
from .records import read_json, write_atomic
from .settings import number_in, section
CALIBRATION = "account-calibration.json"
SHARE = (0.0, 100.0)
def last_reset(reset: dict, now: float) -> float:
"""Args:
reset: {day, time}: day 0 for Sunday to 6 for Saturday, time HH:MM,
both in the hub's local time.
now: epoch seconds.
Returns:
The epoch seconds of the latest such moment at or before now; days
step by the calendar, so a DST change keeps the wall clock time, as
Resources.lastMoment counts in the page.
"""
hours, minutes = map(int, reset["time"].split(":"))
at = datetime.datetime.fromtimestamp(now).replace(hour=hours, minute=minutes, second=0, microsecond=0)
at -= datetime.timedelta(days=(at.isoweekday() % 7 - reset["day"]) % 7)
if at.timestamp() > now:
at -= datetime.timedelta(days=7)
return at.timestamp()
def valid_share(body: bytes) -> float:
"""Args:
body: {share}: the percent of the limit the provider shows.
Returns:
The share; another key, a missing one or a share outside 0 to 100
raises ToolError.
"""
low, high = SHARE
return number_in(section(decode(body), "body", ("share",))["share"], "share", low, high)
def well_formed(entry: object) -> bool:
if not isinstance(entry, dict) or set(entry) != {"share", "at"}:
return False
share, at = entry["share"], entry["at"]
numbers = all(isinstance(value, int | float) and not isinstance(value, bool) and math.isfinite(value) for value in (share, at))
return numbers and SHARE[0] <= share <= SHARE[1]
class Calibrations:
def __init__(self, file: Path) -> None:
self.file = file
self.lock = threading.Lock()
data = read_json(file)
self.items: dict[str, dict] = {key: value for key, value in data.items() if well_formed(value)} if isinstance(data, dict) else {}
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 put(self, account_id: str, share: float, now: float) -> dict:
"""Returns:
{share, at}: the stored calibration, at the epoch second it was
set; tokens of the hours from at on count on top of it.
"""
entry = {"share": share, "at": int(now)}
with self.lock:
self.write({**self.items, account_id: entry})
return dict(entry)
def current(self, accounts: list[dict], now: float) -> dict[str, dict]:
"""Args:
accounts: the stored accounts, each with its id and reset.
now: epoch seconds.
Returns:
Each account's calibration that is not older than its last
reset; a calibration of an account that is gone or reset since
is dropped from the file.
"""
resets = {account["id"]: last_reset(account["reset"], now) for account in accounts}
with self.lock:
fresh = {key: entry for key, entry in self.items.items() if key in resets and entry["at"] >= resets[key]}
if fresh != self.items:
self.write(fresh)
return {key: dict(entry) for key, entry in fresh.items()}