Merge branch 'feature/radio-receipts-hub' into unify/skills-20261112-night

# Conflicts:
#	orchestrator/kvb_orchestrator/hub.py
#	orchestrator/server.py
#	orchestrator/templates/page-stream.js
#	skills/kvb-org-radio/scripts/radio.py
This commit is contained in:
Kevin Veen-Birkenbach committed 2026-11-13 17:56:36 +00:00
commit 5b65fb5284
25 files changed
+1433 -88

No files matched your search

+8
View File
@@ -79,6 +79,14 @@ def radio_rows(rows: list[dict], begin: float, end: float) -> list[dict]:
return sorted(found, key=lambda row: row["ts"])[-RADIO_LIMIT:]
def message_rows(rows: list[dict], begin: float, end: float) -> list[dict]:
"""Returns:
radio_rows() without the receipts, which only feed the acks of their
call.
"""
return [row for row in radio_rows(rows, begin, end) if row["class"] != RECEIPT]
def office_kind(office: str) -> tuple[str, str, str]:
"""Args:
office: an office of one or two segments.
+3 -4
View File
@@ -3,12 +3,12 @@ from __future__ import annotations
import time
from .asking import ASK_LIFETIME, ask_state
from .council import HUMAN, read_claims
from .council import read_claims
from .errors import ToolError
from .messaging import DIRECT, EVERYONE, TO_ALL
from .register import REGISTER, adder, operator_rows, read_register
from .signed import operator_signed, signing
from .skill_scripts import RADIO, RASCI
from .skill_scripts import RASCI
DENIABLE = ("ask", "operator", "rule")
DENY_KEYS = ("channel", "reason", "to")
@@ -125,6 +125,5 @@ class Denying:
where = f"{channel} to {address}" if direct else f"{channel} to all"
fields = {"to": note["to"], "address": address, "text": text, "upload": None, "voice": None, "voice_sha256": "", "class": channel}
record = self.messages.add({**fields, "denial": item["signed"], "proof": proof, "created_ts": time.time()})
targets = ["--to", address] if direct else []
run = self.scripts.run(RADIO, [*DENY_CHANNELS[channel], *targets, "--session", HUMAN, f"[kvb-orchestrator msg {record['id']}] {' '.join(text.split())}"])
run = self.radioed(record, DENY_CHANNELS[channel])
return where if run["code"] == 0 else f"{where} failed ({run['code']})"
+31 -1
View File
@@ -1,5 +1,6 @@
from __future__ import annotations
import functools
import threading
import time
from dataclasses import dataclass
@@ -29,6 +30,15 @@ from .messaging import Messaging
from .names import Names
from .offices import Offices
from .presence import SNAPSHOT
from .receipts import (
NAMES,
READS,
SIGHTING,
ReceiptNames,
ReceiptReads,
sight,
sighting_pass,
)
from .redirects import Redirects
from .requesting import Requesting
from .resources import RESOURCES, SAMPLES
@@ -43,7 +53,7 @@ from .tasking import Tasking
from .timeline import Timeline
from .transcripts import Transcripts
from .tree import TREE_HEARTBEAT, Tree
from .view import View
from .view import RADIO_WINDOW, View
from .viewstore import ViewStore
from .voicenotes import VoiceNotes
@@ -91,6 +101,8 @@ class Hub(Mcp, Reviewing, Asking, Messaging, Choosing, Approving, Denying, Reque
log: Log
batches: Pending
names: Names
receipt_names: ReceiptNames
receipt_reads: ReceiptReads
watch: Watch
tree: Tree
agent_sessions: Path
@@ -119,6 +131,22 @@ class Hub(Mcp, Reviewing, Asking, Messaging, Choosing, Approving, Denying, Reque
return f"{signing.origin_of(self.public_port)}/"
def sighting(hub: Hub, stop: threading.Event) -> threading.Thread:
"""Args:
hub: the hub whose receipt stores record the host snapshot's names
and the first sighting of each read row.
stop: ends the thread once set.
Returns:
The started daemon thread that runs sighting_pass() every SIGHTING
seconds, whether or not a page builds the state.
"""
step = functools.partial(sighting_pass, hub.scripts.skills, hub.council, hub.receipt_names, hub.receipt_reads, hub.agent_sessions, RADIO_WINDOW)
thread = threading.Thread(target=sight, args=(step, hub.receipt_reads, stop, SIGHTING), name="receipt names", daemon=True)
thread.start()
return thread
def open_hub(council: Path, pages: Path, public_port: int, state: Path, host_state: Path, transcripts: Path, scripts: Runner, templates: Path, places: Places, model: Model, engines: Engines, approvers: Path, redirects: Redirects) -> Hub:
uploads = state / "uploads"
uploads.mkdir(exist_ok=True)
@@ -144,6 +172,8 @@ def open_hub(council: Path, pages: Path, public_port: int, state: Path, host_sta
log=Log(state / "log.jsonl"),
batches=Pending(),
names=Names(state / "names.json"),
receipt_names=ReceiptNames(state / NAMES),
receipt_reads=ReceiptReads(state / READS),
settings=settings,
redirects=redirects,
watch=Watch(),
+54 -5
View File
@@ -1,13 +1,14 @@
from __future__ import annotations
import contextlib
import re
import secrets
import time
from .council import HUMAN, agent, ordered, read_claims
from .council import HUMAN, NEWS, agent, ordered, read_claims
from .errors import Conflict, Missing, ToolError
from .fields import NAME_LIMIT, ORDER_NAME, decode, expect, field, message_text, single
from .records import write_atomic
from .records import epoch, read_jsonl, write_atomic
from .register import REGISTER, agent_order, read_register
from .signed import message_signed, signing
from .skill_scripts import ESTIMATE, NIGHT, RADIO, RASCI
@@ -22,6 +23,7 @@ UNSAFE = re.compile(r"[^A-Za-z0-9._-]+")
DIRECT = {"post": ["post"], "call": ["send", "call"]}
TO_ALL = {"newsletter": ["post"], **{cls: ["send", cls] for cls in ("broadcast", "pan-pan", "security", "mayday")}}
EVERYONE = "*"
STORED = re.compile(r"ts: ([0-9]+(?:\.[0-9]+)?(?:e[+-]?[0-9]+)?)")
DENIED = "Denied by the operator."
MESSAGE_KEYS = ("to", "text", "upload", "class", "voice")
SIGNED_BOARD = ("clear-mayday", *NIGHTS, "estimates")
@@ -57,6 +59,20 @@ def chosen(payload: dict, choices: dict[str, list[str]]) -> list[str]:
return choices[cls]
def stored_ts(stdout: str) -> float | None:
"""Returns:
The ts of the STORED line radio.py prints first, right after it
stored its row; None when the first line is none or holds a ts
epoch() refuses. No later line, an address or a cut tail, counts.
"""
match = STORED.fullmatch(stdout.partition("\n")[0])
return epoch(float(match[1])) if match else None
def relay_targets(record: dict) -> list[str]:
return [] if EVERYONE in (record["to"], record["address"]) else [record["address"]]
def message_row(message: dict) -> dict:
upload = message["upload"]
return {
@@ -112,7 +128,7 @@ class Messaging:
}
def relayed(self, record: dict) -> str:
upload, voice = record["upload"], record["voice"]
upload, voice = record.get("upload"), record.get("voice")
notes = ("" if upload is None else f" file: {upload['path']}") + ("" if voice is None else f" voice: {self.voice_notes.host_path(voice)}")
return f"{' '.join(record['text'].split())}{notes}"
@@ -182,9 +198,42 @@ class Messaging:
fields = {"to": target, "address": address, "text": item["text"], "upload": upload, "voice": item["voice"] or None, "voice_sha256": item["voice_sha256"], "class": item["class"]}
return self.messages.add({**fields, "proof": proof, "created_ts": time.time()})
def relay_text(self, record: dict) -> str:
return f"[kvb-orchestrator msg {record['id']}] {self.relayed(record)}"
def relay_ts(self, record: dict, news: list[dict], stored: float | None) -> float | None:
"""Args:
record: a message of the hub's ledger radioed() just relayed.
news: the news.jsonl rows after the run.
stored: stored_ts() of the run's stdout.
Returns:
stored when a row of that ts was sent by HUMAN to
relay_targets(), with the relay text and no to_sessions; else
None.
"""
text = " ".join(self.relay_text(record).split())
found = stored is not None and any(
epoch(row.get("ts")) == stored and row.get("from") == HUMAN and row.get("to") == relay_targets(record)
and "to_sessions" not in row and isinstance(row.get("text"), str) and " ".join(row["text"].split()) == text
for row in news
)
return stored if found else None
def radioed(self, record: dict, command: list[str]) -> dict:
targets = [] if record["to"] == EVERYONE else ["--to", record["address"]]
return self.scripts.run(RADIO, [*command, *targets, "--session", HUMAN, f"[kvb-orchestrator msg {record['id']}] {self.relayed(record)}"])
"""Returns:
The radio.py run; whatever its exit code, timeout included, the
record keeps the ts radio.py printed for its row as relay_ts once
relay_ts() confirms it, the one row whose readers and acks the
message shows.
"""
targets = [part for address in relay_targets(record) for part in ("--to", address)]
run = self.scripts.run(RADIO, [*command, *targets, "--session", HUMAN, self.relay_text(record)])
relay = self.relay_ts(record, read_jsonl(self.council / NEWS), stored_ts(run["stdout"]))
if relay is not None:
with contextlib.suppress(OSError):
self.messages.change(record["id"], {"relay_ts": relay})
return run
def relay(self, payload: dict, choices: dict[str, list[str]]) -> dict:
with self.voice_notes.held(payload["voice"]):
+23 -6
View File
@@ -81,17 +81,34 @@ class Directory:
return found
def valid_entry(entry: object) -> bool:
if not isinstance(entry, dict) or set(entry) != {"session", "name", "first_seen", "source"}:
return False
texts = all(isinstance(entry[key], str) and entry[key] for key in ("session", "name", "source"))
moment = entry["first_seen"]
return texts and isinstance(moment, int | float) and not isinstance(moment, bool) and math.isfinite(moment)
class Names:
def __init__(self, file: Path) -> None:
"""Args:
file: the store; an entry or an observed name of the wrong shape
or type is left out.
"""
self.file = file
self.lock = threading.Lock()
self.titled = -math.inf
data = read_json(file)
data = data if isinstance(data, dict) else {}
entries, observed = data.get("entries"), data.get("observed")
keys = {"session", "name", "first_seen", "source"}
self.entries = [entry for entry in entries if isinstance(entry, dict) and set(entry) == keys] if isinstance(entries, list) else []
self.observed: dict[str, str] = observed if isinstance(observed, dict) else {}
self.data = data if isinstance(data, dict) else {}
entries, observed = self.data.get("entries"), self.data.get("observed")
self.entries = [entry for entry in entries if valid_entry(entry)] if isinstance(entries, list) else []
self.observed: dict[str, str] = {key: name for key, name in observed.items() if isinstance(name, str)} if isinstance(observed, dict) else {}
def payload(self) -> dict:
return {"entries": self.entries, "observed": self.observed}
def save(self) -> None:
write_atomic(self.file, json.dumps(self.payload(), sort_keys=True).encode("utf-8"))
def observe(self, sightings: list[tuple[str, str, float | None, str]], now: float) -> None:
"""Args:
@@ -120,7 +137,7 @@ class Names:
first = seen if seen is not None and seen > floor else max(now, floor)
self.entries.append({"session": session, "name": name, "first_seen": first, "source": source})
if changed:
write_atomic(self.file, json.dumps({"entries": self.entries, "observed": self.observed}, sort_keys=True).encode("utf-8"))
self.save()
def listed(self) -> list[dict]:
with self.lock:
+11 -4
View File
@@ -12,25 +12,32 @@ SNAPSHOT = "agent-sessions.json"
SNAPSHOT_STALE = 5 * 60
def read_snapshot(path: Path, now: float) -> dict[str, dict]:
def fresh_snapshot(path: Path, now: float) -> dict[str, dict] | None:
"""Args:
path: the snapshot client/sessions.py writes on the host.
now: the moment to judge at.
Returns:
Its session records by session id; empty when it is missing,
Its session records by session id; None when it is missing,
malformed, written more than SNAPSHOT_STALE ago or dated in the
future.
"""
data = read_json(path)
if not isinstance(data, dict) or not isinstance(data.get("sessions"), dict):
return {}
return None
written = data.get("written")
if isinstance(written, bool) or not isinstance(written, int | float) or not 0 <= now - written <= SNAPSHOT_STALE:
return {}
return None
return {sid: record for sid, record in data["sessions"].items() if isinstance(record, dict)}
def read_snapshot(path: Path, now: float) -> dict[str, dict]:
"""Returns:
fresh_snapshot(), empty in place of None.
"""
return fresh_snapshot(path, now) or {}
def unanswered(news: list[dict], now: float) -> list[tuple[str, float]]:
"""Returns:
(callee, call time) of every call older than UNANSWERED without a
+251 -16
View File
@@ -3,21 +3,38 @@ from __future__ import annotations
import functools
import inspect
import json
import math
import os
import sys
import threading
import time
from collections import Counter
from collections.abc import Callable
from pathlib import Path
from types import ModuleType
from .council import RECEIPT
from .names import PSEUDO, Directory, Names
from .records import epoch, read_regular
from .council import NEWS, RECEIPT
from .names import PSEUDO, SESSION_ID, Directory, Names
from .presence import fresh_snapshot, read_snapshot
from .records import epoch, read_json, read_jsonl, read_regular, write_atomic
from .skill_scripts import RADIO, script_module
SEEN = "radio-seen"
PROMPT = "news-seen"
NAMES = "receipt-names.json"
READS = "receipt-reads.json"
READ_ENTRIES = 32768
LOG_CHARS = 300
SNAPSHOT_SOURCE = "session"
PROMPT_BYTES = 64
STATE_BYTES = 4096
CACHED = 4096
SIGHTING = 15.0
LOOKED_AGE = 2 * SIGHTING
KEPT = 7 * 86400
ENTRIES = 4096
STEP = 3600.0
NAME_CHARS = 200
SEEN_PARAMETERS = ("row", "name", "sid", "prompt", "state", "since")
Marks = tuple[float | None, dict[str, float]]
@@ -100,20 +117,158 @@ def read_marks(council: Path, now: float) -> dict[str, Marks]:
return {sid: (prompt_of(council / PROMPT / sid, now), state_of(council / SEEN / f"{sid}.json", now)) for sid in sorted(sids)}
def session_directory(store: Names, sessions: dict[str, dict], now: float) -> Directory:
def fits(sid: str, name: object) -> bool:
return bool(SESSION_ID.fullmatch(sid)) and isinstance(name, str) and 0 < len(name) <= NAME_CHARS
class ReceiptNames(Names):
def __init__(self, file: Path) -> None:
"""Args:
file: the hub's receipt name store; beside the Names history it
keeps each session's last sighting under sighted. An entry or
observed name whose session id is not of SESSION_ID or whose
name is not 1 to NAME_CHARS characters, and a malformed
sighting are left out.
"""
super().__init__(file)
self.entries = [entry for entry in self.entries if fits(entry["session"], entry["name"])]
self.observed = {key: name for key, name in self.observed.items() if fits(key.partition("\x00")[0], name)}
sighted = self.data.get("sighted")
self.sighted: dict[str, float] = {sid: when for sid, value in sighted.items() if SESSION_ID.fullmatch(sid) and (when := epoch(value)) is not None} if isinstance(sighted, dict) else {}
def payload(self) -> dict:
return {**super().payload(), "sighted": self.sighted}
def keep(self, live: list[str], now: float) -> None:
"""Args:
live: the sessions of the snapshot, sighted at now; a sighting is
stored at most every STEP seconds, and a session without one
counts as sighted now.
now: the moment of the sighting; a sighting after it counts as at
it, a session not sighted for KEPT seconds loses its entries,
and past ENTRIES entries the sessions sighted longest ago lose
theirs first.
"""
seen = set(live)
with self.lock:
future = {sid for sid, last in self.sighted.items() if last > now}
changed = bool(future)
self.sighted.update(dict.fromkeys(future, now))
sessions = {entry["session"] for entry in self.entries}
for sid in sorted(sessions | seen):
last = self.sighted.get(sid)
if last is None or (sid in seen and now - last >= STEP):
self.sighted[sid] = now
changed = True
gone = {sid for sid, last in self.sighted.items() if now - last > KEPT or sid not in sessions}
counts = Counter(entry["session"] for entry in self.entries if entry["session"] not in gone)
total = sum(counts.values())
for sid in sorted(counts, key=lambda sid: (self.sighted[sid], sid)):
if total <= ENTRIES:
break
gone.add(sid)
total -= counts[sid]
if gone:
self.entries = [entry for entry in self.entries if entry["session"] not in gone]
self.observed = {key: name for key, name in self.observed.items() if key.partition("\x00")[0] not in gone}
self.sighted = {sid: last for sid, last in self.sighted.items() if sid not in gone}
changed = True
if changed:
self.save()
def sightings(sessions: dict[str, dict]) -> list[tuple[str, str, None, str]]:
return [(sid, record["name"], None, SNAPSHOT_SOURCE) for sid, record in sessions.items() if fits(sid, record.get("name"))]
def stored_read(entry: object) -> tuple[tuple[str, float], float | None] | None:
if not isinstance(entry, list) or len(entry) != 3 or not isinstance(entry[0], str) or not SESSION_ID.fullmatch(entry[0]):
return None
ts, when = epoch(entry[1]), epoch(entry[2])
if ts is None or (when is None and entry[2] is not None) or (when is not None and when < ts):
return None
return (entry[0], ts), when
def clamped(times: dict[tuple[str, float], float | None], now: float) -> dict[tuple[str, float], float | None]:
return {key: when if when is None or when <= now else now for key, when in times.items()}
class ReceiptReads:
def __init__(self, file: Path) -> None:
"""Args:
file: the hub's read time store, {"reads": [[session id, row ts,
first sighting or null]]}; an entry of another shape, a
session id not of SESSION_ID, a time that is no epoch or a
sighting before its row is left out.
"""
self.file = file
self.lock = threading.Lock()
self.looked: float | None = None
self.floor = -math.inf
data = read_json(file)
reads = data.get("reads") if isinstance(data, dict) else None
self.times: dict[tuple[str, float], float | None] = dict(found for entry in reads if (found := stored_read(entry))) if isinstance(reads, list) else {}
def listed(self, now: float) -> dict[tuple[str, float], float | None]:
"""Returns:
Per (session id, row ts) its first sighting, one after now as
now.
"""
with self.lock:
return clamped(self.times, now)
def blind(self) -> None:
with self.lock:
self.looked = None
def see(self, covered: set[tuple[str, float]], now: float, begin: float, fresh: bool) -> None:
"""Args:
covered: (session id, row ts) of each row a mark of the session
covers now.
now: the moment of the sighting pass; a pair not stored yet is
stored with it, with None unless the previous pass of this
process read a fresh snapshot at most LOOKED_AGE seconds
before, since it was read at an unknown time before the hub
looked, as across a frozen hub; a stored sighting after it is
stored as it.
begin: the start of the radio window; pairs of older rows go, and
past READ_ENTRIES those of the oldest rows, which then never
come back with a later time.
fresh: whether this pass read a fresh session snapshot.
"""
with self.lock:
when = now if self.looked is not None and 0 <= now - self.looked <= LOOKED_AGE else None
self.floor = max(self.floor, begin)
times = {key: value for key, value in clamped(self.times, now).items() if key[1] >= self.floor}
times.update({key: when for key in covered if key[1] >= self.floor and key not in times})
if len(times) > READ_ENTRIES:
self.floor = math.nextafter(sorted(key[1] for key in times)[-READ_ENTRIES - 1], math.inf)
times = {key: value for key, value in times.items() if key[1] >= self.floor}
self.looked = now if fresh else None
if times != self.times:
self.times = times
write_atomic(self.file, json.dumps({"reads": [[sid, ts, read] for (sid, ts), read in sorted(times.items())]}).encode("utf-8"))
def session_directory(store: ReceiptNames, sessions: dict[str, dict], now: float) -> Directory:
"""Args:
store: the hub's own name file for receipts, apart from the claim and
title history, which any agent can feed through a claim file.
sessions: read_snapshot(), written on the host from Claude Code's
sessions; each record's name is the one radio.py's name_of()
reads, kept as is, so rows and receipts match it unnormalized.
reads, kept as is, so rows and receipts match it unnormalized; a
record without a session id of SESSION_ID or a name of 1 to
NAME_CHARS characters is left out.
now: the moment of the sighting.
Returns:
The store's history from snapshot sightings alone, the names of
ended sessions included.
ended sessions included until ReceiptNames.keep() drops them.
"""
store.observe([(sid, record["name"], None, SNAPSHOT_SOURCE) for sid, record in sessions.items() if isinstance(record.get("name"), str) and record["name"]], now)
found = sightings(sessions)
store.observe(found, now)
store.keep([sid for sid, *_ in found], now)
return Directory([entry for entry in store.listed() if entry["source"] == SNAPSHOT_SOURCE])
@@ -141,18 +296,21 @@ def acknowledged(radio: ModuleType, rows: list[dict], sessions: set[str], direct
return found
def reader(radio: ModuleType, row: dict, sid: str, name: str, marks: Marks, acked: dict[str, float]) -> dict | None:
def reader(radio: ModuleType, row: dict, sid: str, name: str, marks: Marks, acked: dict[str, float], sighted: dict[tuple[str, float], float | None]) -> dict | None:
prompt, state = marks
if not radio.seen(row, name, sid, radio.NEVER if prompt is None else prompt, state, radio.NEVER):
return None
return {"session": sid, "name": name, "read": acked.get(sid)}
if sid in acked:
return {"session": sid, "name": name, "read": acked[sid], "approximate": False}
when = sighted.get((sid, epoch(row["ts"])))
return {"session": sid, "name": name, "read": when, "approximate": when is not None}
def order(entry: dict) -> tuple:
return entry["read"] is None, entry["read"] or 0.0, entry["name"], entry["session"]
def readers_and_acks(radio: ModuleType, news: list[dict], window: tuple[float, float], marks: dict[str, Marks], directory: Directory) -> dict[float, dict]:
def readers_and_acks(radio: ModuleType, news: list[dict], window: tuple[float, float], marks: dict[str, Marks], directory: Directory, sighted: dict[tuple[str, float], float | None]) -> dict[float, dict]:
"""Args:
radio: radio_module(); its seen() decides whether a session read a
row, with the marks alone and no delivery window.
@@ -165,23 +323,100 @@ def readers_and_acks(radio: ModuleType, news: list[dict], window: tuple[float, f
as radio.py's name_of(); a receipt belongs to the
sessions known to hold its sender's name by its time, else to
those whose first known name it is.
sighted: ReceiptReads.listed(), per (session id, row ts) the first
sighting pass that found a mark covering the row.
Returns:
Per row ts {"readers": [{"session", "name", "read"}], "acks": [session
ids]}: acks the sessions with a receipt whose call is that ts; read
the first such receipt's ts, None for a reader without one, as the
seen marks keep no delivery time.
Per row ts {"readers": [{"session", "name", "read", "approximate"}],
"acks": [session ids]}: acks the sessions with a receipt whose call is
that ts; read the first such receipt's ts, else its sighting with
approximate true, else None, never a mark, which holds a send time.
A ts more than one row carries gets no entry: radio.py never stores
two rows at one ts, so a row appended with a genuine ts can neither
replace nor lend its readers and acks.
"""
begin, end = window
rows = [row for row in news if epoch(row.get("ts")) is not None]
shared = {ts for ts, count in Counter(epoch(row["ts"]) for row in rows).items() if count > 1}
acks = acknowledged(radio, rows, set(marks), directory)
reach = {sid: max(radio.NEVER if prompt is None else prompt, radio.NEVER, *state.values()) for sid, (prompt, state) in marks.items()}
found = {}
for row in rows:
ts = epoch(row["ts"])
if radio.kind(row) == RECEIPT or not begin <= ts <= end:
if radio.kind(row) == RECEIPT or not begin <= ts <= end or ts in shared:
continue
acked = acks.get(ts, {})
readers = [entry for sid, held in marks.items() if ts <= reach[sid] and (entry := reader(radio, row, sid, directory.at(sid, ts) or sid, held, acked))]
readers = [entry for sid, held in marks.items() if ts <= reach[sid] and (entry := reader(radio, row, sid, directory.at(sid, ts) or sid, held, acked, sighted))]
found[ts] = {"readers": sorted(readers, key=order), "acks": sorted(acked)}
return found
def inline(skills: Path, council: Path, store: ReceiptNames, reads: ReceiptReads, snapshot: Path, news: list[dict], window: tuple[float, float], now: float) -> dict[float, dict]:
"""Args:
skills: the installed skills directory, for radio_module().
council: the council directory holding the seen marks.
store: the hub's receipt name store, NAMES in its state directory.
reads: the hub's read time store, READS in its state directory.
snapshot: agent-sessions.json, the host's session snapshot.
news: every news.jsonl row.
window: (begin, end) of the rows the hub serves.
now: the moment to judge at.
Returns:
readers_and_acks() of the rows; empty while radio.py is missing or
its seen() does not take SEEN_PARAMETERS.
"""
radio = radio_module(skills)
if radio is None:
return {}
directory = session_directory(store, read_snapshot(snapshot, now), now)
return readers_and_acks(radio, news, window, read_marks(council, now), directory, reads.listed(now))
def read_of(inlined: dict[float, dict], ts: float | None) -> dict:
return inlined.get(ts, {"readers": [], "acks": []})
def sighting_pass(skills: Path, council: Path, store: ReceiptNames, reads: ReceiptReads, snapshot: Path, window: float, now: float) -> None:
"""Args:
skills: the installed skills directory, for radio_module().
council: the council directory with news.jsonl and the seen marks.
store: the hub's receipt name store; the pass records the snapshot's
names in it, so a session that left before the next state build
keeps its name.
reads: the hub's read time store; the pass records each row of the
last window seconds a mark newly covers.
snapshot: agent-sessions.json, which the host rewrites every 15
seconds; a pass without a fresh one or without radio_module()
counts as one that did not look.
window: seconds of rows the pass looks at, the hub's radio window.
now: the moment of the pass.
"""
sessions = fresh_snapshot(snapshot, now)
directory = session_directory(store, sessions or {}, now)
radio = radio_module(skills)
if radio is None:
reads.see(set(), now, now - window, False)
return
found = readers_and_acks(radio, read_jsonl(council / NEWS), (now - window, now), read_marks(council, now), directory, {})
reads.see({(entry["session"], ts) for ts, row in found.items() for entry in row["readers"]}, now, now - window, sessions is not None)
def sight(step: Callable[[float], None], reads: ReceiptReads, stop: threading.Event, every: float) -> None:
"""Args:
step: one sighting pass, called with its moment; a pass that fails
for any reason but SystemExit or KeyboardInterrupt logs one line
to stderr and the next one runs.
reads: the read time store the passes feed; a failed pass counts as
one that did not look.
stop: ends the loop once set.
every: seconds between two passes.
"""
while True:
try:
step(time.time())
except Exception as error: # noqa: BLE001
reads.blind()
print(f"receipts: sighting pass failed: {error!r}"[:LOG_CHARS], file=sys.stderr, flush=True)
if stop.wait(every):
return
+20 -2
View File
@@ -11,6 +11,8 @@ 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"
@@ -19,6 +21,19 @@ 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
@@ -70,6 +85,9 @@ class Scripts:
stdin=subprocess.DEVNULL,
check=False,
)
except (OSError, subprocess.TimeoutExpired) as error:
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": done.stdout[-OUTPUT_LIMIT:], "stderr": done.stderr[-OUTPUT_LIMIT:]}
return {"command": command, "code": done.returncode, "stdout": bounded(done.stdout), "stderr": done.stderr[-OUTPUT_LIMIT:]}
+6 -3
View File
@@ -3,12 +3,13 @@ from __future__ import annotations
import time
from .asking import ask_state
from .council import MAYDAY_ACTIVE, NEWS, radio_rows, read_claims, sessions
from .council import MAYDAY_ACTIVE, NEWS, message_rows, read_claims, sessions
from .crons import cron_event
from .errors import Missing, ToolError
from .events import event, event_filters, wanted
from .fields import instant, single, whole
from .names import Directory
from .receipts import read_of
from .records import iso_epoch, read_jsonl
from .register import REGISTER, operator_rows, read_register
from .signed import signing
@@ -51,12 +52,14 @@ class Timeline:
def timeline(self, begin: float, end: float, now: float, tasks: dict[str, dict], claims: list[dict], radio: list[dict], directory: Directory) -> list[dict]:
rows = []
for index, row in enumerate(radio_rows(radio, begin, end)):
inlined = self.inlined(radio, (begin, end), now)
for index, row in enumerate(message_rows(radio, begin, end)):
alarm = row["class"] in ("mayday", "pan-pan") and row["ts"] >= end - MAYDAY_ACTIVE
title = f"{row['from'] or '?'} → {', '.join(row['to']) or 'all'}"
source = directory.key(row["from"], row["ts"])
who = {"agent": directory.at(source, row["ts"]) or row["from"], "now": directory.now(source), "session": source}
ref = {"to": row["to"], "class": row["class"], "targets": [directory.key(name, row["ts"]) for name in row["to"]]}
read = read_of(inlined, row["ts"])
ref = {"to": row["to"], "class": row["class"], "targets": [directory.key(name, row["ts"]) for name in row["to"]], **read}
rows.append(event({"id": f"radio:{row['ts']}:{index}", "ts": row["ts"], "kind": "radio", "part": row["class"], **who, "title": title, "text": row["text"], "waiting": alarm, "ref": ref}))
for review in self.reviews.listed():
created = iso_epoch(review["created"]) or now
+17 -4
View File
@@ -7,9 +7,9 @@ from .council import (
MAYDAY_ACTIVE,
NEWS,
grants,
message_rows,
night_mode,
night_parts,
radio_rows,
read_claims,
sessions,
silence_of,
@@ -24,6 +24,7 @@ from .messaging import message_row
from .orbits import TRACK_STEPS, track
from .phrasing import joined, phrase_parts
from .presence import presence, read_snapshot
from .receipts import inline, read_of
from .records import epoch, iso_epoch, read_jsonl
from .register import (
AGENT_ORDER,
@@ -76,6 +77,16 @@ class View:
self.tree.beat()
return self.state(begin, end, now, TRACK_STEPS if sampled else 0)
def inlined(self, news: list[dict], window: tuple[float, float], now: float) -> dict[float, dict]:
"""Returns:
inline() of the rows; empty when it fails, so the state builds
without readers.
"""
try:
return inline(self.scripts.skills, self.council, self.receipt_names, self.receipt_reads, self.agent_sessions, news, window, now)
except Exception: # noqa: BLE001
return {}
def first(self, news: list[dict], history: list[dict], claims: list[dict], now: float) -> float:
moments = [epoch(row.get("ts")) for row in news] + [epoch(row.get("start")) for row in history]
moments += [iso_epoch(claim["since"]) for claim in claims] + [self.transcripts.first()]
@@ -90,13 +101,15 @@ class View:
names = sessions(claims)
directory = self.directory(claims, news, now)
radio = []
for row in radio_rows(news, begin, end):
inlined = self.inlined(news, (begin, end), now)
for row in message_rows(news, begin, end):
source = directory.key(row["from"], row["ts"])
targets = [directory.key(name, row["ts"]) for name in row["to"]]
called = [directory.at(key, row["ts"]) or name for key, name in zip(targets, row["to"], strict=True)]
word, rest = phrase_parts(row["class"], directory.at(source, row["ts"]) or row["from"], called, row["text"])
spoken = {"speech": joined((word, rest)), "word": word, "rest": rest, "voice": self.speech.voice_of(source)}
radio.append({**row, "source": source, "targets": targets, "now": directory.now(source), **spoken})
read = read_of(inlined, row["ts"])
radio.append({**row, "source": source, "targets": targets, "now": directory.now(source), **spoken, **read})
grant_rows = [{**grant, "target": directory.key(grant["to"], grant["ts"])} for grant in grants(history, (begin, end), claims, tasks)]
self.watch.see(tasks, now)
jobs = [{**{key: value for key, value in job.items() if key != "proof"}, "output": job["output"][-OUTPUT_LIMIT:]} for job in self.jobs.listed()[-JOBS_SHOWN:]]
@@ -132,7 +145,7 @@ class View:
"reviews": [row for row in map(review_row, self.reviews.pending()) if row["created_ts"] <= end],
"asks": [ask_row(ask, names) for ask in self.asks.pending() if ask["expires"] > now and ask["created_ts"] <= end],
"decisions": [decision_row(decision) for decision in self.decisions.pending() if decision["created_ts"] <= end],
"messages": [message_row(message) for message in self.messages.listed() if begin <= message["created_ts"] <= end],
"messages": [{**message_row(message), **read_of(inlined, epoch(message.get("relay_ts")))} for message in self.messages.listed() if begin <= message["created_ts"] <= end],
"operator": operator_rows(tasks),
"register": register_rows(tasks),
"tasks": rows,
+3 -1
View File
@@ -68,6 +68,7 @@ from __future__ import annotations
import argparse
import ssl
import threading
from pathlib import Path
from kvb_orchestrator import NAME
@@ -75,7 +76,7 @@ from kvb_orchestrator.engines import Engines
from kvb_orchestrator.errors import ToolError
from kvb_orchestrator.exchange import HubServer
from kvb_orchestrator.handler import Handler
from kvb_orchestrator.hub import Places, open_hub
from kvb_orchestrator.hub import Places, open_hub, sighting
from kvb_orchestrator.keys import key_from
from kvb_orchestrator.language import Model
from kvb_orchestrator.redirects import TIMEOUT, Redirects, dial
@@ -122,6 +123,7 @@ def main() -> int:
parser.error(str(error))
server = HubServer((args.bind, args.listen_port), Handler)
server.hub = hub
sighting(hub, threading.Event())
print(f"{NAME}: listening on {args.bind}:{args.listen_port}, public port {args.public_port}", flush=True)
server.serve_forever()
return 0
+7
View File
@@ -281,6 +281,11 @@ input[type="datetime-local"] { padding: 3px 6px; border: 1px solid var(--line-st
.custom { display: inline-flex; flex-wrap: wrap; align-items: center; gap: 6px; }
.avatar { display: inline-grid; place-items: center; flex: none; width: 26px; height: 26px; overflow: hidden; border-radius: 50%; background: var(--sunken); border: 1px solid var(--line); }
.avatar.big { width: 52px; height: 52px; }
.readers { display: flex; flex-wrap: wrap; align-items: center; gap: 4px; margin-top: 6px; }
.reader { position: relative; display: inline-flex; }
.reader .avatar { width: 22px; height: 22px; }
.reader .ack { position: absolute; right: -4px; bottom: -3px; display: inline-grid; place-items: center; width: 13px; height: 13px; border-radius: 50%; background: var(--ok); color: #fff; font-size: 9px; box-shadow: 0 0 0 1.5px var(--panel); }
.readers-more { padding: 0 7px; border-radius: 999px; background: var(--neutral-soft); color: var(--muted); font-size: var(--fs-xs); font-weight: 650; line-height: 20px; cursor: default; }
.avatar svg { display: block; width: 100%; height: 100%; }
.linkish { padding: 0; border: 0; background: none; color: inherit; font-weight: 650; text-align: left; cursor: pointer; overflow-wrap: anywhere; }
.linkish:hover { color: var(--accent); text-decoration: underline; }
@@ -470,6 +475,8 @@ pre.notes { margin: 0; padding: 8px 10px; background: var(--sunken); border-radi
{% include "radioset.js" %}
{% include "receipts.js" %}
{% include "bubbles.js" %}
{% include "diffs.js" %}
+3 -3
View File
@@ -1,7 +1,7 @@
function history(key) {
const known = (people()[key] || {names: [nameOf(key)]}).names;
const radio = state.radio.filter((row) => row.source === key || row.targets.includes(key)).map((row) => ({ts: row.ts, mine: row.from === HUMAN, who: row.from, text: row.text}));
const sent = state.messages.filter((message) => known.includes(message.to) || message.to === "*").map((message) => ({ts: message.created_ts, mine: true, who: HUMAN, text: message.upload ? `${message.text}\n📎 ${message.upload}` : message.text, id: message.id, voice: message.voice}));
const radio = state.radio.filter((row) => row.source === key || row.targets.includes(key)).map((row) => ({ts: row.ts, mine: row.from === HUMAN, who: row.from, text: row.text, readers: row.readers, acks: row.acks}));
const sent = state.messages.filter((message) => known.includes(message.to) || message.to === "*").map((message) => ({ts: message.created_ts, mine: true, who: HUMAN, text: message.upload ? `${message.text}\n📎 ${message.upload}` : message.text, id: message.id, voice: message.voice, readers: message.readers, acks: message.acks}));
const hubIds = new Set(sent.map((entry) => entry.id));
const relayed = radio.filter((entry) => !(entry.who === HUMAN && [...hubIds].some((id) => entry.text.includes(`[kvb-orchestrator msg ${id}]`))));
return [...relayed, ...sent].sort((a, b) => a.ts - b.ts).slice(-80);
@@ -31,7 +31,7 @@
h("span", {class: "muted"}, `${dash(claim.runtime)} · since ${dash(claim.since)}`)))) : h("p", {class: "muted"}, "No claims."));
const rows = history(ui.agent);
agentDom.history.replaceChildren(rows.length
? h("div", {}, rows.map((row) => h("div", {class: `bubble ${row.mine ? "mine" : "theirs"}`}, h("span", {class: "muted"}, `${row.who} · `, when(row.ts)), row.text, voiceReplay(row.voice))))
? h("div", {}, rows.map((row) => h("div", {class: `bubble ${row.mine ? "mine" : "theirs"}`}, h("span", {class: "muted"}, `${row.who} · `, when(row.ts)), row.text, voiceReplay(row.voice), readerRow(row))))
: h("p", {class: "muted"}, "No history in the chosen time range."));
placePopup();
}
+8
View File
@@ -95,6 +95,14 @@
const nameOf = (key) => (people()[key] || {now: String(key).replace(/^name:/, "")}).now;
const agentKey = (who) => (people()[who] ? who : (Object.entries(people()).find(([, entry]) => entry.now === who || entry.names.includes(who)) || [who])[0]);
const chairKey = () => (state.graph.nodes.find((node) => node.kind === "chair") || {id: HUMAN}).id;
const readLabel = (reader) => Receipts.label(reader, (ts) => absolute.format(stampOf(ts)));
const readerRow = (row) => {
const {shown, hidden} = Receipts.strip(row);
const avatar = (reader) => h("span", {class: "reader", "data-session": reader.session, "data-acked": String(reader.acked), role: "img", ...named(readLabel(reader))},
face(people()[reader.session] ? reader.session : reader.name, false), reader.acked ? h("span", {class: "ack", "aria-hidden": "true"}, icon("bi-check2-all")) : null);
const more = hidden.length ? h("span", {class: "readers-more", role: "img", ...named(`${hidden.length} more readers`, [...shown, ...hidden].map(readLabel).join("\n"))}, `+${hidden.length}`) : null;
return shown.length ? h("div", {class: "readers", role: "group", "aria-label": `Read by ${shown.length + hidden.length}`}, shown.map(avatar), more) : null;
};
const stamp = (life) => [life.born ? `🐣 ${life.born}` : "", life.died ? `🪦 ${life.died}` : ""].filter(Boolean).join(" · ");
function toast(title, lines, good) {
+3 -2
View File
@@ -216,13 +216,14 @@
h("span", {class: "radio-route"}, `${row.from} → ${targetsOf(row)}`),
h("span", {class: "radio-line", title: first}, first)),
h("pre", {class: "radio-full"}, row.text),
voiceReplay(message ? message.voice : null)));
voiceReplay(message ? message.voice : null)),
readerRow(row));
}
function drawLog() {
const dom = radioDom.log;
const entry = radioSet.log;
const sig = JSON.stringify([state.radio.length, state.radio.length ? state.radio[state.radio.length - 1].ts : 0, state.messages.length, entry, Math.floor(Date.now() / 60000)]);
const sig = JSON.stringify([state.radio.length, state.radio.length ? state.radio[state.radio.length - 1].ts : 0, state.messages.length, Receipts.signature(state.radio), Receipts.signature(state.messages), entry, Math.floor(Date.now() / 60000)]);
if (sig === dom.sig) {
return;
}
+2 -1
View File
@@ -133,12 +133,13 @@
const alarm = entry.kind === "radio" && (entry.part === "mayday" || entry.part === "pan-pan");
const renamed = entry.now && entry.now !== entry.agent ? h("span", {title: `now: ${entry.now}`}, person(entry.agent)) : person(entry.agent);
const who = entry.agent ? [face(entry.agent, false), renamed] : entry.session ? h("strong", {title: entry.session}, entry.session.slice(0, 8)) : null;
const headline = entry.kind === "transcript" || entry.kind === "ask" || entry.kind === "operator" ? null : h("span", {class: "headline"}, entry.title);
const headline = entry.kind === "transcript" || entry.kind === "ask" || entry.kind === "operator" ? null : h("span", {class: "headline"}, Receipts.headline(entry));
return h("li", {class: `entry k-${entry.kind}${entry.waiting ? " waiting" : ""}${alarm ? " alarm" : ""}`, "data-ref": item ? keyOf(item) : null, "data-item": `message:${entry.ts}/${entry.id}`, "data-to": item && item.to ? item.to : null},
h("div", {class: "msg-meta"}, when(entry.ts), kindPill(entry), extras(entry)),
h("div", {},
h("div", {class: "route", "data-star-slot": ""}, who, entry.sub ? h("button", {type: "button", class: "linkish muted", on: {click: () => focusSub(entry.session, entry.sub)}}, `› Subagent ${entry.sub.slice(0, 12)}`) : null, entry.kind === "ask" ? h("strong", {}, entry.title) : null, headline),
body(entry),
entry.kind === "radio" ? readerRow(entry.ref) : null,
item ? diffBlocks(item) : null,
item ? feedbackList(keyOf(item)) : null,
item ? h("div", {class: "line-actions"}, actionsFor(item)) : null));
+1 -1
View File
@@ -103,7 +103,7 @@ const RadioSet = (() => {
}
function sentRows(messages, human) {
return messages.map((message) => ({ts: message.created_ts, from: human, to: message.to === "*" ? [] : [message.to], class: message.class || "post", text: message.text, voice: message.voice, sent: true}));
return messages.map((message) => ({ts: message.created_ts, from: human, to: message.to === "*" ? [] : [message.to], class: message.class || "post", text: message.text, voice: message.voice, sent: true, readers: message.readers, acks: message.acks}));
}
function tabRows(rows, tab, human, messages) {
+50
View File
@@ -0,0 +1,50 @@
const Receipts = (() => {
"use strict";
const SHOWN = 8;
/**
* @param {object} row a radio row of the state or the ref of a radio entry of the Stream, with readers and acks.
* @returns {object} shown and hidden: every reader with acked true where its receipt exists, the first SHOWN as avatars and the rest behind "+N".
*/
function strip(row) {
const acks = new Set(Array.isArray(row.acks) ? row.acks : []);
const readers = (Array.isArray(row.readers) ? row.readers : []).map((reader) => ({...reader, acked: acks.has(reader.session)}));
return {shown: readers.slice(0, SHOWN), hidden: readers.slice(SHOWN)};
}
/**
* @param {object} reader an entry of strip().
* @param {function(number): string} format renders a read time.
* @returns {string} the reader's name, its read time where the hub knows one, prefixed "about" where only a sighting gives it, and whether it acknowledged.
*/
function label(reader, format) {
const known = reader.read !== null && reader.read !== undefined;
const read = known ? `read ${reader.approximate ? "about " : ""}${format(reader.read)}` : "read";
return [reader.name, read, reader.acked ? "acknowledged" : ""].filter(Boolean).join(" · ");
}
/**
* @param {object} entry a Stream entry.
* @returns {string} its headline; a radio entry whose sender the route already shows names only the recipients.
*/
function headline(entry) {
if (entry.kind !== "radio" || !entry.agent) {
return entry.title;
}
return `→ ${entry.ref.to.length ? entry.ref.to.join(", ") : "all"}`;
}
/**
* @param {Array<object>} rows radio rows of the state.
* @returns {string} changes whenever a reader, its read time or an ack of any row does, so a log that redraws only on change shows them on the next poll.
*/
function signature(rows) {
const read = (reader) => `${reader.session}@${reader.read ?? ""}${reader.approximate ? "~" : ""}`;
return rows.map((row) => `${(row.readers || []).map(read).join(",")}/${(row.acks || []).join(",")}`).join("|");
}
return {SHOWN, strip, label, headline, signature};
})();
if (typeof module === "object" && module.exports) {
module.exports = Receipts;
}