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:
commit
5b65fb5284
25 files changed
+1433
-88
No files matched your search
@@ -55,14 +55,14 @@ flowchart TD
|
|||||||
|
|
||||||
### Server
|
### Server
|
||||||
|
|
||||||
- [ ] Each radio row the hub serves carries `readers` (session id, name, read
|
- [x] Each radio row the hub serves carries `readers` (session id, name, read
|
||||||
time) and `acks` (session ids), computed by the function `radio.py`
|
time) and `acks` (session ids), computed by the function `radio.py`
|
||||||
uses for its own seen check; a unit test covers every class.
|
uses for its own seen check; a unit test covers every class.
|
||||||
- [ ] A receipt counts for its call when the call is served, even if the
|
- [x] A receipt counts for its call when the call is served, even if the
|
||||||
receipt row lies outside the served window.
|
receipt row lies outside the served window.
|
||||||
- [ ] A missing, malformed, unreadable or future-dated seen mark counts as
|
- [x] A missing, malformed, unreadable or future-dated seen mark counts as
|
||||||
not read.
|
not read.
|
||||||
- [ ] Receipt rows are absent from the rows served to the Stream, the radio
|
- [x] Receipt rows are absent from the rows served to the Stream, the radio
|
||||||
log and agent pages; count badges count messages without receipts.
|
log and agent pages; count badges count messages without receipts.
|
||||||
|
|
||||||
### Page
|
### Page
|
||||||
@@ -78,9 +78,9 @@ flowchart TD
|
|||||||
|
|
||||||
### Tests
|
### Tests
|
||||||
|
|
||||||
- [ ] Unit: readers and acks per class; malformed marks; receipt to call
|
- [x] Unit: readers and acks per class; malformed marks; receipt to call
|
||||||
matching by call time; the header renders the sender once.
|
matching by call time; the header renders the sender once.
|
||||||
- [ ] Integration: the hub state endpoint over a council fixture (posts,
|
- [x] Integration: the hub state endpoint over a council fixture (posts,
|
||||||
calls, broadcasts, receipts, seen marks) returns readers and acks and
|
calls, broadcasts, receipts, seen marks) returns readers and acks and
|
||||||
no receipt rows.
|
no receipt rows.
|
||||||
- [ ] E2E: Chromium and Firefox at 1920, 1366 and 390: a call with two
|
- [ ] E2E: Chromium and Firefox at 1920, 1366 and 390: a call with two
|
||||||
|
|||||||
@@ -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:]
|
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]:
|
def office_kind(office: str) -> tuple[str, str, str]:
|
||||||
"""Args:
|
"""Args:
|
||||||
office: an office of one or two segments.
|
office: an office of one or two segments.
|
||||||
|
|||||||
@@ -3,12 +3,12 @@ from __future__ import annotations
|
|||||||
import time
|
import time
|
||||||
|
|
||||||
from .asking import ASK_LIFETIME, ask_state
|
from .asking import ASK_LIFETIME, ask_state
|
||||||
from .council import HUMAN, read_claims
|
from .council import read_claims
|
||||||
from .errors import ToolError
|
from .errors import ToolError
|
||||||
from .messaging import DIRECT, EVERYONE, TO_ALL
|
from .messaging import DIRECT, EVERYONE, TO_ALL
|
||||||
from .register import REGISTER, adder, operator_rows, read_register
|
from .register import REGISTER, adder, operator_rows, read_register
|
||||||
from .signed import operator_signed, signing
|
from .signed import operator_signed, signing
|
||||||
from .skill_scripts import RADIO, RASCI
|
from .skill_scripts import RASCI
|
||||||
|
|
||||||
DENIABLE = ("ask", "operator", "rule")
|
DENIABLE = ("ask", "operator", "rule")
|
||||||
DENY_KEYS = ("channel", "reason", "to")
|
DENY_KEYS = ("channel", "reason", "to")
|
||||||
@@ -125,6 +125,5 @@ class Denying:
|
|||||||
where = f"{channel} to {address}" if direct else f"{channel} to all"
|
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}
|
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()})
|
record = self.messages.add({**fields, "denial": item["signed"], "proof": proof, "created_ts": time.time()})
|
||||||
targets = ["--to", address] if direct else []
|
run = self.radioed(record, DENY_CHANNELS[channel])
|
||||||
run = self.scripts.run(RADIO, [*DENY_CHANNELS[channel], *targets, "--session", HUMAN, f"[kvb-orchestrator msg {record['id']}] {' '.join(text.split())}"])
|
|
||||||
return where if run["code"] == 0 else f"{where} failed ({run['code']})"
|
return where if run["code"] == 0 else f"{where} failed ({run['code']})"
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import functools
|
||||||
import threading
|
import threading
|
||||||
import time
|
import time
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
@@ -29,6 +30,15 @@ from .messaging import Messaging
|
|||||||
from .names import Names
|
from .names import Names
|
||||||
from .offices import Offices
|
from .offices import Offices
|
||||||
from .presence import SNAPSHOT
|
from .presence import SNAPSHOT
|
||||||
|
from .receipts import (
|
||||||
|
NAMES,
|
||||||
|
READS,
|
||||||
|
SIGHTING,
|
||||||
|
ReceiptNames,
|
||||||
|
ReceiptReads,
|
||||||
|
sight,
|
||||||
|
sighting_pass,
|
||||||
|
)
|
||||||
from .redirects import Redirects
|
from .redirects import Redirects
|
||||||
from .requesting import Requesting
|
from .requesting import Requesting
|
||||||
from .resources import RESOURCES, SAMPLES
|
from .resources import RESOURCES, SAMPLES
|
||||||
@@ -43,7 +53,7 @@ from .tasking import Tasking
|
|||||||
from .timeline import Timeline
|
from .timeline import Timeline
|
||||||
from .transcripts import Transcripts
|
from .transcripts import Transcripts
|
||||||
from .tree import TREE_HEARTBEAT, Tree
|
from .tree import TREE_HEARTBEAT, Tree
|
||||||
from .view import View
|
from .view import RADIO_WINDOW, View
|
||||||
from .viewstore import ViewStore
|
from .viewstore import ViewStore
|
||||||
from .voicenotes import VoiceNotes
|
from .voicenotes import VoiceNotes
|
||||||
|
|
||||||
@@ -91,6 +101,8 @@ class Hub(Mcp, Reviewing, Asking, Messaging, Choosing, Approving, Denying, Reque
|
|||||||
log: Log
|
log: Log
|
||||||
batches: Pending
|
batches: Pending
|
||||||
names: Names
|
names: Names
|
||||||
|
receipt_names: ReceiptNames
|
||||||
|
receipt_reads: ReceiptReads
|
||||||
watch: Watch
|
watch: Watch
|
||||||
tree: Tree
|
tree: Tree
|
||||||
agent_sessions: Path
|
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)}/"
|
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:
|
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 = state / "uploads"
|
||||||
uploads.mkdir(exist_ok=True)
|
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"),
|
log=Log(state / "log.jsonl"),
|
||||||
batches=Pending(),
|
batches=Pending(),
|
||||||
names=Names(state / "names.json"),
|
names=Names(state / "names.json"),
|
||||||
|
receipt_names=ReceiptNames(state / NAMES),
|
||||||
|
receipt_reads=ReceiptReads(state / READS),
|
||||||
settings=settings,
|
settings=settings,
|
||||||
redirects=redirects,
|
redirects=redirects,
|
||||||
watch=Watch(),
|
watch=Watch(),
|
||||||
|
|||||||
@@ -1,13 +1,14 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import contextlib
|
||||||
import re
|
import re
|
||||||
import secrets
|
import secrets
|
||||||
import time
|
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 .errors import Conflict, Missing, ToolError
|
||||||
from .fields import NAME_LIMIT, ORDER_NAME, decode, expect, field, message_text, single
|
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 .register import REGISTER, agent_order, read_register
|
||||||
from .signed import message_signed, signing
|
from .signed import message_signed, signing
|
||||||
from .skill_scripts import ESTIMATE, NIGHT, RADIO, RASCI
|
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"]}
|
DIRECT = {"post": ["post"], "call": ["send", "call"]}
|
||||||
TO_ALL = {"newsletter": ["post"], **{cls: ["send", cls] for cls in ("broadcast", "pan-pan", "security", "mayday")}}
|
TO_ALL = {"newsletter": ["post"], **{cls: ["send", cls] for cls in ("broadcast", "pan-pan", "security", "mayday")}}
|
||||||
EVERYONE = "*"
|
EVERYONE = "*"
|
||||||
|
STORED = re.compile(r"ts: ([0-9]+(?:\.[0-9]+)?(?:e[+-]?[0-9]+)?)")
|
||||||
DENIED = "Denied by the operator."
|
DENIED = "Denied by the operator."
|
||||||
MESSAGE_KEYS = ("to", "text", "upload", "class", "voice")
|
MESSAGE_KEYS = ("to", "text", "upload", "class", "voice")
|
||||||
SIGNED_BOARD = ("clear-mayday", *NIGHTS, "estimates")
|
SIGNED_BOARD = ("clear-mayday", *NIGHTS, "estimates")
|
||||||
@@ -57,6 +59,20 @@ def chosen(payload: dict, choices: dict[str, list[str]]) -> list[str]:
|
|||||||
return choices[cls]
|
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:
|
def message_row(message: dict) -> dict:
|
||||||
upload = message["upload"]
|
upload = message["upload"]
|
||||||
return {
|
return {
|
||||||
@@ -112,7 +128,7 @@ class Messaging:
|
|||||||
}
|
}
|
||||||
|
|
||||||
def relayed(self, record: dict) -> str:
|
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)}")
|
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}"
|
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"]}
|
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()})
|
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:
|
def radioed(self, record: dict, command: list[str]) -> dict:
|
||||||
targets = [] if record["to"] == EVERYONE else ["--to", record["address"]]
|
"""Returns:
|
||||||
return self.scripts.run(RADIO, [*command, *targets, "--session", HUMAN, f"[kvb-orchestrator msg {record['id']}] {self.relayed(record)}"])
|
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:
|
def relay(self, payload: dict, choices: dict[str, list[str]]) -> dict:
|
||||||
with self.voice_notes.held(payload["voice"]):
|
with self.voice_notes.held(payload["voice"]):
|
||||||
|
|||||||
@@ -81,17 +81,34 @@ class Directory:
|
|||||||
return found
|
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:
|
class Names:
|
||||||
def __init__(self, file: Path) -> None:
|
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.file = file
|
||||||
self.lock = threading.Lock()
|
self.lock = threading.Lock()
|
||||||
self.titled = -math.inf
|
self.titled = -math.inf
|
||||||
data = read_json(file)
|
data = read_json(file)
|
||||||
data = data if isinstance(data, dict) else {}
|
self.data = data if isinstance(data, dict) else {}
|
||||||
entries, observed = data.get("entries"), data.get("observed")
|
entries, observed = self.data.get("entries"), self.data.get("observed")
|
||||||
keys = {"session", "name", "first_seen", "source"}
|
self.entries = [entry for entry in entries if valid_entry(entry)] if isinstance(entries, list) else []
|
||||||
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] = {key: name for key, name in observed.items() if isinstance(name, str)} if isinstance(observed, dict) else {}
|
||||||
self.observed: dict[str, str] = observed 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:
|
def observe(self, sightings: list[tuple[str, str, float | None, str]], now: float) -> None:
|
||||||
"""Args:
|
"""Args:
|
||||||
@@ -120,7 +137,7 @@ class Names:
|
|||||||
first = seen if seen is not None and seen > floor else max(now, floor)
|
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})
|
self.entries.append({"session": session, "name": name, "first_seen": first, "source": source})
|
||||||
if changed:
|
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]:
|
def listed(self) -> list[dict]:
|
||||||
with self.lock:
|
with self.lock:
|
||||||
|
|||||||
@@ -12,25 +12,32 @@ SNAPSHOT = "agent-sessions.json"
|
|||||||
SNAPSHOT_STALE = 5 * 60
|
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:
|
"""Args:
|
||||||
path: the snapshot client/sessions.py writes on the host.
|
path: the snapshot client/sessions.py writes on the host.
|
||||||
now: the moment to judge at.
|
now: the moment to judge at.
|
||||||
|
|
||||||
Returns:
|
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
|
malformed, written more than SNAPSHOT_STALE ago or dated in the
|
||||||
future.
|
future.
|
||||||
"""
|
"""
|
||||||
data = read_json(path)
|
data = read_json(path)
|
||||||
if not isinstance(data, dict) or not isinstance(data.get("sessions"), dict):
|
if not isinstance(data, dict) or not isinstance(data.get("sessions"), dict):
|
||||||
return {}
|
return None
|
||||||
written = data.get("written")
|
written = data.get("written")
|
||||||
if isinstance(written, bool) or not isinstance(written, int | float) or not 0 <= now - written <= SNAPSHOT_STALE:
|
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)}
|
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]]:
|
def unanswered(news: list[dict], now: float) -> list[tuple[str, float]]:
|
||||||
"""Returns:
|
"""Returns:
|
||||||
(callee, call time) of every call older than UNANSWERED without a
|
(callee, call time) of every call older than UNANSWERED without a
|
||||||
|
|||||||
@@ -3,21 +3,38 @@ from __future__ import annotations
|
|||||||
import functools
|
import functools
|
||||||
import inspect
|
import inspect
|
||||||
import json
|
import json
|
||||||
|
import math
|
||||||
import os
|
import os
|
||||||
|
import sys
|
||||||
|
import threading
|
||||||
|
import time
|
||||||
|
from collections import Counter
|
||||||
|
from collections.abc import Callable
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from types import ModuleType
|
from types import ModuleType
|
||||||
|
|
||||||
from .council import RECEIPT
|
from .council import NEWS, RECEIPT
|
||||||
from .names import PSEUDO, Directory, Names
|
from .names import PSEUDO, SESSION_ID, Directory, Names
|
||||||
from .records import epoch, read_regular
|
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
|
from .skill_scripts import RADIO, script_module
|
||||||
|
|
||||||
SEEN = "radio-seen"
|
SEEN = "radio-seen"
|
||||||
PROMPT = "news-seen"
|
PROMPT = "news-seen"
|
||||||
|
NAMES = "receipt-names.json"
|
||||||
|
READS = "receipt-reads.json"
|
||||||
|
READ_ENTRIES = 32768
|
||||||
|
LOG_CHARS = 300
|
||||||
SNAPSHOT_SOURCE = "session"
|
SNAPSHOT_SOURCE = "session"
|
||||||
PROMPT_BYTES = 64
|
PROMPT_BYTES = 64
|
||||||
STATE_BYTES = 4096
|
STATE_BYTES = 4096
|
||||||
CACHED = 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")
|
SEEN_PARAMETERS = ("row", "name", "sid", "prompt", "state", "since")
|
||||||
|
|
||||||
Marks = tuple[float | None, dict[str, float]]
|
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)}
|
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:
|
"""Args:
|
||||||
store: the hub's own name file for receipts, apart from the claim and
|
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.
|
title history, which any agent can feed through a claim file.
|
||||||
sessions: read_snapshot(), written on the host from Claude Code's
|
sessions: read_snapshot(), written on the host from Claude Code's
|
||||||
sessions; each record's name is the one radio.py's name_of()
|
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.
|
now: the moment of the sighting.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
The store's history from snapshot sightings alone, the names of
|
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])
|
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
|
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
|
prompt, state = marks
|
||||||
if not radio.seen(row, name, sid, radio.NEVER if prompt is None else prompt, state, radio.NEVER):
|
if not radio.seen(row, name, sid, radio.NEVER if prompt is None else prompt, state, radio.NEVER):
|
||||||
return None
|
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:
|
def order(entry: dict) -> tuple:
|
||||||
return entry["read"] is None, entry["read"] or 0.0, entry["name"], entry["session"]
|
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:
|
"""Args:
|
||||||
radio: radio_module(); its seen() decides whether a session read a
|
radio: radio_module(); its seen() decides whether a session read a
|
||||||
row, with the marks alone and no delivery window.
|
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
|
as radio.py's name_of(); a receipt belongs to the
|
||||||
sessions known to hold its sender's name by its time, else to
|
sessions known to hold its sender's name by its time, else to
|
||||||
those whose first known name it is.
|
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:
|
Returns:
|
||||||
Per row ts {"readers": [{"session", "name", "read"}], "acks": [session
|
Per row ts {"readers": [{"session", "name", "read", "approximate"}],
|
||||||
ids]}: acks the sessions with a receipt whose call is that ts; read
|
"acks": [session ids]}: acks the sessions with a receipt whose call is
|
||||||
the first such receipt's ts, None for a reader without one, as the
|
that ts; read the first such receipt's ts, else its sighting with
|
||||||
seen marks keep no delivery time.
|
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
|
begin, end = window
|
||||||
rows = [row for row in news if epoch(row.get("ts")) is not None]
|
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)
|
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()}
|
reach = {sid: max(radio.NEVER if prompt is None else prompt, radio.NEVER, *state.values()) for sid, (prompt, state) in marks.items()}
|
||||||
found = {}
|
found = {}
|
||||||
for row in rows:
|
for row in rows:
|
||||||
ts = epoch(row["ts"])
|
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
|
continue
|
||||||
acked = acks.get(ts, {})
|
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)}
|
found[ts] = {"readers": sorted(readers, key=order), "acks": sorted(acked)}
|
||||||
return found
|
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
|
||||||
@@ -11,6 +11,8 @@ from typing import Protocol
|
|||||||
|
|
||||||
SCRIPT_TIMEOUT = 60
|
SCRIPT_TIMEOUT = 60
|
||||||
OUTPUT_LIMIT = 4000
|
OUTPUT_LIMIT = 4000
|
||||||
|
FIRST_LINE_LIMIT = 200
|
||||||
|
ELIDED = "..."
|
||||||
SAFE_PATH = "/usr/local/bin:/usr/bin:/bin"
|
SAFE_PATH = "/usr/local/bin:/usr/bin:/bin"
|
||||||
RADIO = "kvb-org-radio/scripts/radio.py"
|
RADIO = "kvb-org-radio/scripts/radio.py"
|
||||||
RASCI = "kvb-org-rasci/scripts/rasci.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"
|
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:
|
def script_module(script: Path) -> ModuleType | None:
|
||||||
"""Returns:
|
"""Returns:
|
||||||
The installed script as a module, loaded again after an install
|
The installed script as a module, loaded again after an install
|
||||||
@@ -70,6 +85,9 @@ class Scripts:
|
|||||||
stdin=subprocess.DEVNULL,
|
stdin=subprocess.DEVNULL,
|
||||||
check=False,
|
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": -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:]}
|
||||||
@@ -3,12 +3,13 @@ from __future__ import annotations
|
|||||||
import time
|
import time
|
||||||
|
|
||||||
from .asking import ask_state
|
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 .crons import cron_event
|
||||||
from .errors import Missing, ToolError
|
from .errors import Missing, ToolError
|
||||||
from .events import event, event_filters, wanted
|
from .events import event, event_filters, wanted
|
||||||
from .fields import instant, single, whole
|
from .fields import instant, single, whole
|
||||||
from .names import Directory
|
from .names import Directory
|
||||||
|
from .receipts import read_of
|
||||||
from .records import iso_epoch, read_jsonl
|
from .records import iso_epoch, read_jsonl
|
||||||
from .register import REGISTER, operator_rows, read_register
|
from .register import REGISTER, operator_rows, read_register
|
||||||
from .signed import signing
|
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]:
|
def timeline(self, begin: float, end: float, now: float, tasks: dict[str, dict], claims: list[dict], radio: list[dict], directory: Directory) -> list[dict]:
|
||||||
rows = []
|
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
|
alarm = row["class"] in ("mayday", "pan-pan") and row["ts"] >= end - MAYDAY_ACTIVE
|
||||||
title = f"{row['from'] or '?'} → {', '.join(row['to']) or 'all'}"
|
title = f"{row['from'] or '?'} → {', '.join(row['to']) or 'all'}"
|
||||||
source = directory.key(row["from"], row["ts"])
|
source = directory.key(row["from"], row["ts"])
|
||||||
who = {"agent": directory.at(source, row["ts"]) or row["from"], "now": directory.now(source), "session": source}
|
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}))
|
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():
|
for review in self.reviews.listed():
|
||||||
created = iso_epoch(review["created"]) or now
|
created = iso_epoch(review["created"]) or now
|
||||||
|
|||||||
@@ -7,9 +7,9 @@ from .council import (
|
|||||||
MAYDAY_ACTIVE,
|
MAYDAY_ACTIVE,
|
||||||
NEWS,
|
NEWS,
|
||||||
grants,
|
grants,
|
||||||
|
message_rows,
|
||||||
night_mode,
|
night_mode,
|
||||||
night_parts,
|
night_parts,
|
||||||
radio_rows,
|
|
||||||
read_claims,
|
read_claims,
|
||||||
sessions,
|
sessions,
|
||||||
silence_of,
|
silence_of,
|
||||||
@@ -24,6 +24,7 @@ from .messaging import message_row
|
|||||||
from .orbits import TRACK_STEPS, track
|
from .orbits import TRACK_STEPS, track
|
||||||
from .phrasing import joined, phrase_parts
|
from .phrasing import joined, phrase_parts
|
||||||
from .presence import presence, read_snapshot
|
from .presence import presence, read_snapshot
|
||||||
|
from .receipts import inline, read_of
|
||||||
from .records import epoch, iso_epoch, read_jsonl
|
from .records import epoch, iso_epoch, read_jsonl
|
||||||
from .register import (
|
from .register import (
|
||||||
AGENT_ORDER,
|
AGENT_ORDER,
|
||||||
@@ -76,6 +77,16 @@ class View:
|
|||||||
self.tree.beat()
|
self.tree.beat()
|
||||||
return self.state(begin, end, now, TRACK_STEPS if sampled else 0)
|
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:
|
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 = [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()]
|
moments += [iso_epoch(claim["since"]) for claim in claims] + [self.transcripts.first()]
|
||||||
@@ -90,13 +101,15 @@ class View:
|
|||||||
names = sessions(claims)
|
names = sessions(claims)
|
||||||
directory = self.directory(claims, news, now)
|
directory = self.directory(claims, news, now)
|
||||||
radio = []
|
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"])
|
source = directory.key(row["from"], row["ts"])
|
||||||
targets = [directory.key(name, row["ts"]) for name in row["to"]]
|
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)]
|
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"])
|
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)}
|
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)]
|
grant_rows = [{**grant, "target": directory.key(grant["to"], grant["ts"])} for grant in grants(history, (begin, end), claims, tasks)]
|
||||||
self.watch.see(tasks, now)
|
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:]]
|
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],
|
"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],
|
"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],
|
"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),
|
"operator": operator_rows(tasks),
|
||||||
"register": register_rows(tasks),
|
"register": register_rows(tasks),
|
||||||
"tasks": rows,
|
"tasks": rows,
|
||||||
|
|||||||
@@ -68,6 +68,7 @@ from __future__ import annotations
|
|||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import ssl
|
import ssl
|
||||||
|
import threading
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from kvb_orchestrator import NAME
|
from kvb_orchestrator import NAME
|
||||||
@@ -75,7 +76,7 @@ from kvb_orchestrator.engines import Engines
|
|||||||
from kvb_orchestrator.errors import ToolError
|
from kvb_orchestrator.errors import ToolError
|
||||||
from kvb_orchestrator.exchange import HubServer
|
from kvb_orchestrator.exchange import HubServer
|
||||||
from kvb_orchestrator.handler import Handler
|
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.keys import key_from
|
||||||
from kvb_orchestrator.language import Model
|
from kvb_orchestrator.language import Model
|
||||||
from kvb_orchestrator.redirects import TIMEOUT, Redirects, dial
|
from kvb_orchestrator.redirects import TIMEOUT, Redirects, dial
|
||||||
@@ -122,6 +123,7 @@ def main() -> int:
|
|||||||
parser.error(str(error))
|
parser.error(str(error))
|
||||||
server = HubServer((args.bind, args.listen_port), Handler)
|
server = HubServer((args.bind, args.listen_port), Handler)
|
||||||
server.hub = hub
|
server.hub = hub
|
||||||
|
sighting(hub, threading.Event())
|
||||||
print(f"{NAME}: listening on {args.bind}:{args.listen_port}, public port {args.public_port}", flush=True)
|
print(f"{NAME}: listening on {args.bind}:{args.listen_port}, public port {args.public_port}", flush=True)
|
||||||
server.serve_forever()
|
server.serve_forever()
|
||||||
return 0
|
return 0
|
||||||
|
|||||||
@@ -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; }
|
.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 { 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; }
|
.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%; }
|
.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 { 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; }
|
.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 "radioset.js" %}
|
||||||
|
|
||||||
|
{% include "receipts.js" %}
|
||||||
|
|
||||||
{% include "bubbles.js" %}
|
{% include "bubbles.js" %}
|
||||||
|
|
||||||
{% include "diffs.js" %}
|
{% include "diffs.js" %}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
function history(key) {
|
function history(key) {
|
||||||
const known = (people()[key] || {names: [nameOf(key)]}).names;
|
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 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}));
|
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 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}]`))));
|
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);
|
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."));
|
h("span", {class: "muted"}, `${dash(claim.runtime)} · since ${dash(claim.since)}`)))) : h("p", {class: "muted"}, "No claims."));
|
||||||
const rows = history(ui.agent);
|
const rows = history(ui.agent);
|
||||||
agentDom.history.replaceChildren(rows.length
|
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."));
|
: h("p", {class: "muted"}, "No history in the chosen time range."));
|
||||||
placePopup();
|
placePopup();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -95,6 +95,14 @@
|
|||||||
const nameOf = (key) => (people()[key] || {now: String(key).replace(/^name:/, "")}).now;
|
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 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 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(" · ");
|
const stamp = (life) => [life.born ? `🐣 ${life.born}` : "", life.died ? `🪦 ${life.died}` : ""].filter(Boolean).join(" · ");
|
||||||
|
|
||||||
function toast(title, lines, good) {
|
function toast(title, lines, good) {
|
||||||
|
|||||||
@@ -216,13 +216,14 @@
|
|||||||
h("span", {class: "radio-route"}, `${row.from} → ${targetsOf(row)}`),
|
h("span", {class: "radio-route"}, `${row.from} → ${targetsOf(row)}`),
|
||||||
h("span", {class: "radio-line", title: first}, first)),
|
h("span", {class: "radio-line", title: first}, first)),
|
||||||
h("pre", {class: "radio-full"}, row.text),
|
h("pre", {class: "radio-full"}, row.text),
|
||||||
voiceReplay(message ? message.voice : null)));
|
voiceReplay(message ? message.voice : null)),
|
||||||
|
readerRow(row));
|
||||||
}
|
}
|
||||||
|
|
||||||
function drawLog() {
|
function drawLog() {
|
||||||
const dom = radioDom.log;
|
const dom = radioDom.log;
|
||||||
const entry = radioSet.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) {
|
if (sig === dom.sig) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -133,12 +133,13 @@
|
|||||||
const alarm = entry.kind === "radio" && (entry.part === "mayday" || entry.part === "pan-pan");
|
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 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 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},
|
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", {class: "msg-meta"}, when(entry.ts), kindPill(entry), extras(entry)),
|
||||||
h("div", {},
|
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),
|
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),
|
body(entry),
|
||||||
|
entry.kind === "radio" ? readerRow(entry.ref) : null,
|
||||||
item ? diffBlocks(item) : null,
|
item ? diffBlocks(item) : null,
|
||||||
item ? feedbackList(keyOf(item)) : null,
|
item ? feedbackList(keyOf(item)) : null,
|
||||||
item ? h("div", {class: "line-actions"}, actionsFor(item)) : null));
|
item ? h("div", {class: "line-actions"}, actionsFor(item)) : null));
|
||||||
|
|||||||
@@ -103,7 +103,7 @@ const RadioSet = (() => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function sentRows(messages, human) {
|
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) {
|
function tabRows(rows, tab, human, messages) {
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
@@ -21,12 +21,14 @@ Args:
|
|||||||
call needs --to; broadcast, security, pan-pan and mayday go to all
|
call needs --to; broadcast, security, pan-pan and mayday go to all
|
||||||
and take no --to. direct is the old alias: with --wake a call,
|
and take no --to. direct is the old alias: with --wake a call,
|
||||||
without it a post. mayday also sets radio silence
|
without it a post. mayday also sets radio silence
|
||||||
(<root>/radio-silence.json) for two hours. Prints `wake: SendMessage
|
(<root>/radio-silence.json) for two hours. Prints `ts: <ts>`, the
|
||||||
to <name>` for every addressee without a live listener. --sid pins
|
stored row's ts, flushed right after it is stored, then `sent ...`
|
||||||
a call with a single --to to that session as in feedback send.
|
and `wake: SendMessage to <name>` for every addressee without a live
|
||||||
|
listener. --sid pins a call with a single --to to that session as in
|
||||||
|
feedback send.
|
||||||
post "<text>" [--to <name> ...] [--session <id>]: the post, delivered
|
post "<text>" [--to <name> ...] [--session <id>]: the post, delivered
|
||||||
when the addressee's turn ends, never waking it: post with --to,
|
when the addressee's turn ends, never waking it: post with --to,
|
||||||
newsletter without.
|
newsletter without; prints `ts: <ts>` as send does.
|
||||||
clear [--force] [--session <id>]: end radio silence; its sender may,
|
clear [--force] [--session <id>]: end radio silence; its sender may,
|
||||||
anyone else only with --force.
|
anyone else only with --force.
|
||||||
assist [--session <id>]: join the active mayday as a helper, whom the
|
assist [--session <id>]: join the active mayday as a helper, whom the
|
||||||
@@ -108,6 +110,7 @@ ADDRESSED = ("call", "post", "receipt")
|
|||||||
SEND = ("call", "broadcast", "security", "pan-pan", "mayday", "direct")
|
SEND = ("call", "broadcast", "security", "pan-pan", "mayday", "direct")
|
||||||
FEEDBACKS = ("post", "call")
|
FEEDBACKS = ("post", "call")
|
||||||
VERSION = 2
|
VERSION = 2
|
||||||
|
STORED = "ts: "
|
||||||
WRITERS = ("Edit", "Write", "MultiEdit", "NotebookEdit")
|
WRITERS = ("Edit", "Write", "MultiEdit", "NotebookEdit")
|
||||||
SHELL = re.compile(r"[;&|`$()<>\n\r\\\"]")
|
SHELL = re.compile(r"[;&|`$()<>\n\r\\\"]")
|
||||||
SINGLE_QUOTED = re.compile(r"'[^']*'")
|
SINGLE_QUOTED = re.compile(r"'[^']*'")
|
||||||
@@ -379,12 +382,13 @@ def transmit(cls: str, to: list[str], text: str, caller: str | None, sessions: l
|
|||||||
ROOT.mkdir(parents=True, exist_ok=True)
|
ROOT.mkdir(parents=True, exist_ok=True)
|
||||||
with organize("nofollow").locked(ROOT, LOCK, 0o600, None):
|
with organize("nofollow").locked(ROOT, LOCK, 0o600, None):
|
||||||
ts = store([{"from": sender, "to": to, "text": text, "class": cls, "v": VERSION, **({"to_sessions": sessions} if sessions else {})}])
|
ts = store([{"from": sender, "to": to, "text": text, "class": cls, "v": VERSION, **({"to_sessions": sessions} if sessions else {})}])
|
||||||
|
print(f"{STORED}{ts!r}", flush=True)
|
||||||
if cls == "mayday":
|
if cls == "mayday":
|
||||||
write_atomic(
|
write_atomic(
|
||||||
SILENCE,
|
SILENCE,
|
||||||
json.dumps({"since": ts, "until": ts + SILENCE_SECONDS, "by": sender, "text": text}),
|
json.dumps({"since": ts, "until": ts + SILENCE_SECONDS, "by": sender, "text": text}),
|
||||||
)
|
)
|
||||||
print(f"sent {cls} to {', '.join(to) or 'all'}")
|
print(f"sent {cls} to {', '.join(clip(name) for name in to) or 'all'}")
|
||||||
if cls in RADIO:
|
if cls in RADIO:
|
||||||
for name in unreached(cls, to, sid, sessions):
|
for name in unreached(cls, to, sid, sessions):
|
||||||
print(f"wake: SendMessage to {clip(name)}")
|
print(f"wake: SendMessage to {clip(name)}")
|
||||||
|
|||||||
@@ -353,8 +353,9 @@ CALL = """(async () => {@HELPERS@
|
|||||||
field.value = "E2E call ping";
|
field.value = "E2E call ping";
|
||||||
field.dispatchEvent(new Event("input", {bubbles: true}));
|
field.dispatchEvent(new Event("input", {bubbles: true}));
|
||||||
document.querySelector('#popup button[type="submit"]').click();
|
document.querySelector('#popup button[type="submit"]').click();
|
||||||
const entry = await until(() => all("#main .entry").find((row) => row.textContent.includes("Read") && row.textContent.includes("E2E call ping")), 20000);
|
const entry = await until(() => all("#main .entry").find((row) => row.textContent.includes("E2E call ping") && row.querySelector('.reader[data-acked="true"] .ack')), 20000);
|
||||||
return {to: text(someone), receipt: entry ? text(entry) : null, sent: all("#main .entry").filter((row) => row.textContent.includes("E2E call ping")).length};
|
const read = all("#main .entry .pill").filter((pill) => text(pill) === "Read").length;
|
||||||
|
return {to: text(someone), receipt: entry ? entry.querySelector('.reader[data-acked="true"]').title : null, read, sent: all("#main .entry").filter((row) => row.textContent.includes("E2E call ping")).length};
|
||||||
})()""".replace("@HELPERS@", HELPERS)
|
})()""".replace("@HELPERS@", HELPERS)
|
||||||
OPEN_DELETE = """(async () => {@HELPERS@
|
OPEN_DELETE = """(async () => {@HELPERS@
|
||||||
const card = await until(() => all("#main details.repo").find((details) => details.textContent.includes("github.com/me/demo")), 8000);
|
const card = await until(() => all("#main details.repo").find((details) => details.textContent.includes("github.com/me/demo")), 8000);
|
||||||
@@ -1882,6 +1883,8 @@ class TestOrchestratorE2E(HubCase):
|
|||||||
if script.endswith("radio.py") and args[:2] == ["send", "call"]:
|
if script.endswith("radio.py") and args[:2] == ["send", "call"]:
|
||||||
to, text, now = args[args.index("--to") + 1], args[-1], time.time()
|
to, text, now = args[args.index("--to") + 1], args[-1], time.time()
|
||||||
rows = [{"ts": now, "from": "Operator", "to": [to], "text": text, "class": "call", "v": 2}, {"ts": now + 0.5, "from": to, "to": ["Operator"], "text": text, "class": "receipt", "v": 2, "call": now}]
|
rows = [{"ts": now, "from": "Operator", "to": [to], "text": text, "class": "call", "v": 2}, {"ts": now + 0.5, "from": to, "to": ["Operator"], "text": text, "class": "receipt", "v": 2, "call": now}]
|
||||||
|
self.write_council({"radio-seen/e2e-callee.json": {"radio": now + 0.5}})
|
||||||
|
(self.state / "agent-sessions.json").write_text(json.dumps({"written": now, "sessions": {"e2e-callee": {"name": to, "running": True}}}), encoding="utf-8")
|
||||||
with (self.council / "news.jsonl").open("a", encoding="utf-8") as handle:
|
with (self.council / "news.jsonl").open("a", encoding="utf-8") as handle:
|
||||||
handle.write(self.jsonl(rows))
|
handle.write(self.jsonl(rows))
|
||||||
|
|
||||||
@@ -1892,7 +1895,8 @@ class TestOrchestratorE2E(HubCase):
|
|||||||
result = data["results"][0]
|
result = data["results"][0]
|
||||||
self.assertNotIn("error", result)
|
self.assertNotIn("error", result)
|
||||||
self.assertIsNotNone(result["receipt"], result)
|
self.assertIsNotNone(result["receipt"], result)
|
||||||
self.assertIn("Read", result["receipt"])
|
self.assertTrue(" · read " in result["receipt"] and result["receipt"].endswith(" · acknowledged"), result)
|
||||||
|
self.assertEqual(result["read"], 0, result)
|
||||||
|
|
||||||
def test_chromium_shows_who_speaks_while_a_message_is_read(self):
|
def test_chromium_shows_who_speaks_while_a_message_is_read(self):
|
||||||
self.speaks(("e2e-cue-1", "e2e-cue-2"))
|
self.speaks(("e2e-cue-1", "e2e-cue-2"))
|
||||||
|
|||||||
@@ -0,0 +1,188 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import shutil
|
||||||
|
import sys
|
||||||
|
import time
|
||||||
|
import unittest
|
||||||
|
from pathlib import Path
|
||||||
|
from unittest import mock
|
||||||
|
|
||||||
|
HERE = Path(__file__).resolve().parent
|
||||||
|
sys.path[:0] = [str(HERE.parent), str(HERE)]
|
||||||
|
|
||||||
|
import test_orchestrator_e2e as base
|
||||||
|
from hub_case import HubCase
|
||||||
|
from kvb_orchestrator import receipts
|
||||||
|
from kvb_orchestrator.presence import SNAPSHOT
|
||||||
|
from kvb_orchestrator.view import RADIO_WINDOW
|
||||||
|
|
||||||
|
OUT = Path(os.environ.get("E2E_OUT", "/tmp/radio-receipts-shots"))
|
||||||
|
ADVANCE = "e000000000000001"
|
||||||
|
RESET = "e000000000000002"
|
||||||
|
SESSIONS = {"s1": "Skills", "s2": "Stage", "s3": "Docs"}
|
||||||
|
PROBE = """(async () => {@HELPERS@
|
||||||
|
const pills = (root) => all(".pill", root).filter((pill) => text(pill) === "Read").length;
|
||||||
|
const readers = (node) => all(".reader", node).map((reader) => ({session: reader.dataset.session, acked: reader.dataset.acked, badge: Boolean(reader.querySelector(".ack svg")), title: reader.title, label: reader.getAttribute("aria-label")}));
|
||||||
|
const fits = () => document.documentElement.scrollWidth <= innerWidth + 1;
|
||||||
|
const found = {};
|
||||||
|
await fetch("/api/voice/@RESET@", {cache: "no-store"});
|
||||||
|
const radioChip = await until(() => document.querySelector('#main [aria-label="Categories"] button[aria-label="Radio"]'), 8000);
|
||||||
|
radioChip.click();
|
||||||
|
const call = await until(() => all("#main .entry.k-radio").find((entry) => entry.textContent.includes("gate red on main") && entry.querySelector(".reader")), 12000);
|
||||||
|
if (!call) {
|
||||||
|
return {error: text(document.getElementById("main"))};
|
||||||
|
}
|
||||||
|
const route = text(call.querySelector(".route"));
|
||||||
|
found.stream = {route, sender: route.split("Skills").length - 1, readers: readers(call), read: pills(document.getElementById("main")), entries: all("#main .entry.k-radio").length, fits: fits()};
|
||||||
|
call.scrollIntoView({block: "center"});
|
||||||
|
await sleep(200);
|
||||||
|
document.getElementById("radio-open").click();
|
||||||
|
const spot = await until(() => document.getElementById("radio-spot"), 4000);
|
||||||
|
spot.querySelector('.radio-tabs [data-tab="all"]').click();
|
||||||
|
const row = await until(() => all(".radio-row[data-class=call]", spot).find((item) => item.textContent.includes("gate red on main")), 8000);
|
||||||
|
found.log = {
|
||||||
|
route: row ? text(row.querySelector(".radio-route")) : null, readers: readers(row), read: pills(spot), receipts: all(".radio-row[data-class=receipt]", spot).length,
|
||||||
|
all: Number(text(spot.querySelector('.radio-tabs [data-tab="all"] .count'))), rows: all(".radio-row", spot).length,
|
||||||
|
};
|
||||||
|
spot.querySelector('.radio-tabs [data-tab="sent"]').click();
|
||||||
|
const sentRow = await until(() => all('.radio-row[data-dir="sent"]', spot).find((item) => item.textContent.includes("ping")), 8000);
|
||||||
|
found.sent = {readers: readers(sentRow), read: pills(spot), route: sentRow ? text(sentRow.querySelector(".radio-route")) : null};
|
||||||
|
spot.querySelector('.radio-tabs [data-tab="all"]').click();
|
||||||
|
document.getElementById("radio-open").click();
|
||||||
|
await sleep(200);
|
||||||
|
all("#main button.linkish").find((button) => button.title === "Open Skills").click();
|
||||||
|
const bubble = await until(() => all("#popup .bubble").find((item) => item.textContent.includes("gate red on main")), 8000);
|
||||||
|
const mine = await until(() => all("#popup .bubble.mine").find((item) => item.textContent.includes("ping")), 8000);
|
||||||
|
found.agent = {readers: readers(bubble), mine: readers(mine), read: pills(document.getElementById("popup")), bubbles: all("#popup .bubble").length};
|
||||||
|
document.querySelector("#popup").close();
|
||||||
|
await sleep(200);
|
||||||
|
const later = () => all("#main .entry.k-radio").find((entry) => entry.textContent.includes("which gate"));
|
||||||
|
window.dispatchEvent(new Event("focus"));
|
||||||
|
found.before = await until(() => (later() && !readers(later()).length ? [] : null), 20000);
|
||||||
|
const loads = performance.getEntriesByType("navigation").length;
|
||||||
|
await fetch("/api/voice/@ADVANCE@", {cache: "no-store"});
|
||||||
|
window.dispatchEvent(new Event("focus"));
|
||||||
|
const shown = await until(() => readers(later()).length ? readers(later()) : null, 20000);
|
||||||
|
found.after = {readers: shown, reloaded: performance.getEntriesByType("navigation").length !== loads};
|
||||||
|
call.scrollIntoView({block: "center"});
|
||||||
|
await sleep(300);
|
||||||
|
return found;
|
||||||
|
})()"""
|
||||||
|
|
||||||
|
|
||||||
|
@unittest.skipUnless(shutil.which(os.environ.get("CHROMIUM", "chromium")), "no Chromium")
|
||||||
|
class TestRadioReceipts(HubCase):
|
||||||
|
drive = base.TestOrchestratorE2E.drive
|
||||||
|
authenticator = base.TestOrchestratorE2E.authenticator
|
||||||
|
beside = base.TestOrchestratorE2E.beside
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
super().setUp()
|
||||||
|
OUT.mkdir(parents=True, exist_ok=True)
|
||||||
|
now = time.time()
|
||||||
|
self.call = now - 300
|
||||||
|
self.later = now - 100
|
||||||
|
self.claim("s1", "Skills session")
|
||||||
|
hub = self.httpd.hub
|
||||||
|
ping = now - 200
|
||||||
|
sent = hub.messages.add({"to": "Skills", "address": "Skills", "text": "ping", "upload": None, "class": "call", "voice": None, "created_ts": ping, "relay_ts": ping + 0.1})
|
||||||
|
self.write_council({
|
||||||
|
"news.jsonl": self.jsonl([
|
||||||
|
{"ts": self.call, "from": "Skills", "to": ["Stage", "Docs"], "text": "gate red on main", "class": "call", "v": 2},
|
||||||
|
{"ts": self.call + 5, "from": "Stage", "to": ["Skills"], "text": "gate red on main", "class": "receipt", "v": 2, "call": self.call},
|
||||||
|
{"ts": ping + 0.1, "from": "Operator", "to": ["Skills"], "text": f"[kvb-orchestrator msg {sent['id']}] ping", "class": "call", "v": 2},
|
||||||
|
{"ts": ping + 3, "from": "Skills", "to": ["Operator"], "text": "ping", "class": "receipt", "v": 2, "call": ping + 0.1},
|
||||||
|
{"ts": self.later, "from": "Skills", "to": ["Stage"], "text": "which gate", "class": "call", "v": 2},
|
||||||
|
]),
|
||||||
|
})
|
||||||
|
self.mark("s1", ping + 3)
|
||||||
|
self.mark("s2", self.call + 5)
|
||||||
|
self.mark("s3", self.call + 5)
|
||||||
|
self.sessions()
|
||||||
|
self.beside(self.sessions)
|
||||||
|
self.sight()
|
||||||
|
heard = hub.voice_notes.get
|
||||||
|
|
||||||
|
def advance() -> None:
|
||||||
|
self.sight()
|
||||||
|
self.mark("s2", time.time())
|
||||||
|
self.sight()
|
||||||
|
|
||||||
|
cues = {ADVANCE: advance, RESET: lambda: self.mark("s2", self.call + 5)}
|
||||||
|
|
||||||
|
def get(note_id: str) -> dict:
|
||||||
|
if note_id not in cues:
|
||||||
|
return heard(note_id)
|
||||||
|
cues[note_id]()
|
||||||
|
return {"id": note_id, "status": "done", "text": "", "error": "", "seconds": None, "created_ts": None}
|
||||||
|
|
||||||
|
patch = mock.patch.object(hub.voice_notes, "get", get)
|
||||||
|
patch.start()
|
||||||
|
self.addCleanup(patch.stop)
|
||||||
|
|
||||||
|
def sessions(self) -> None:
|
||||||
|
payload = {"written": time.time(), "sessions": {sid: {"name": name, "running": True} for sid, name in SESSIONS.items()}}
|
||||||
|
written = self.state / f".{SNAPSHOT}.e2e"
|
||||||
|
written.write_text(json.dumps(payload), encoding="utf-8")
|
||||||
|
os.replace(written, self.state / SNAPSHOT)
|
||||||
|
|
||||||
|
def sight(self) -> None:
|
||||||
|
hub = self.httpd.hub
|
||||||
|
receipts.sighting_pass(hub.scripts.skills, hub.council, hub.receipt_names, hub.receipt_reads, hub.agent_sessions, RADIO_WINDOW, time.time())
|
||||||
|
|
||||||
|
def mark(self, sid: str, ts: float) -> None:
|
||||||
|
folder = self.council / receipts.SEEN
|
||||||
|
folder.mkdir(parents=True, exist_ok=True)
|
||||||
|
written = folder / f".{sid}.e2e"
|
||||||
|
written.write_text(json.dumps({"radio": ts}), encoding="utf-8")
|
||||||
|
os.replace(written, folder / f"{sid}.json")
|
||||||
|
|
||||||
|
def steps(self, browser: str) -> list[dict]:
|
||||||
|
steps = []
|
||||||
|
for width, height in base.WIDTHS:
|
||||||
|
steps.append({
|
||||||
|
"width": width, "height": height, "url": f"http://localhost:{self.port}/?{width}#stream", "wait": 1200,
|
||||||
|
"probe": PROBE.replace("@HELPERS@", base.HELPERS).replace("@ADVANCE@", ADVANCE).replace("@RESET@", RESET), "shot": str(OUT / f"{browser}-{width}-receipts.png"),
|
||||||
|
})
|
||||||
|
return steps
|
||||||
|
|
||||||
|
def check(self, data: dict, browser: str) -> None:
|
||||||
|
for (width, _), found in zip(base.WIDTHS, data["results"], strict=True):
|
||||||
|
with self.subTest(browser=browser, width=width):
|
||||||
|
self.assertNotIn("error", found, found)
|
||||||
|
stream, log, agent = found["stream"], found["log"], found["agent"]
|
||||||
|
self.assertEqual((stream["sender"], stream["route"]), (1, "Skills→ Stage, Docs"), stream)
|
||||||
|
self.assertEqual([(reader["session"], reader["acked"], reader["badge"]) for reader in stream["readers"]], [("s2", "true", True), ("s3", "false", False)], stream)
|
||||||
|
self.assertTrue(stream["readers"][0]["title"].startswith("Stage · read ") and stream["readers"][0]["title"].endswith(" · acknowledged"), stream)
|
||||||
|
self.assertEqual(stream["readers"][1]["title"], "Docs · read", stream)
|
||||||
|
self.assertEqual((stream["read"], stream["entries"], stream["fits"]), (0, 3, True), stream)
|
||||||
|
self.assertEqual((log["route"], log["read"], log["receipts"], log["all"], log["rows"]), ("Skills → Stage, Docs", 0, 0, 3, 3), log)
|
||||||
|
self.assertEqual([(reader["session"], reader["badge"]) for reader in log["readers"]], [("s2", True), ("s3", False)], log)
|
||||||
|
self.assertEqual((found["sent"]["route"], found["sent"]["read"]), ("Operator → Skills", 0), found["sent"])
|
||||||
|
self.assertEqual([(reader["session"], reader["acked"], reader["badge"]) for reader in found["sent"]["readers"]], [("s1", "true", True)], found["sent"])
|
||||||
|
self.assertEqual([(reader["session"], reader["badge"]) for reader in agent["readers"]], [("s2", True), ("s3", False)], agent)
|
||||||
|
self.assertEqual([(reader["session"], reader["acked"], reader["badge"]) for reader in agent["mine"]], [("s1", "true", True)], agent)
|
||||||
|
self.assertEqual(agent["read"], 0, agent)
|
||||||
|
self.assertEqual(found["before"], [], found)
|
||||||
|
self.assertEqual([reader["session"] for reader in found["after"]["readers"]], ["s2"], found)
|
||||||
|
self.assertTrue(found["after"]["readers"][0]["title"].startswith("Stage · read about "), found)
|
||||||
|
self.assertFalse(found["after"]["reloaded"], found)
|
||||||
|
|
||||||
|
def test_chromium_shows_readers_and_acks_on_messages_and_the_operators_own_call_with_no_read_entry_and_the_sender_once_at_three_widths(self):
|
||||||
|
data = self.drive(self.steps("chromium"), self.authenticator())
|
||||||
|
(OUT / "chromium-radio-receipts.json").write_text(json.dumps(data, indent=1), encoding="utf-8")
|
||||||
|
self.assertEqual(data["errors"], [])
|
||||||
|
self.check(data, "chromium")
|
||||||
|
|
||||||
|
@unittest.skipUnless(shutil.which(os.environ.get("FIREFOX", "firefox")), "no Firefox")
|
||||||
|
def test_firefox_shows_readers_and_acks_on_messages_and_the_operators_own_call_with_no_read_entry_and_the_sender_once_at_three_widths(self):
|
||||||
|
data = self.drive(self.steps("firefox"), None, "bidi.mjs", os.environ.get("FIREFOX", "firefox"))
|
||||||
|
(OUT / "firefox-radio-receipts.json").write_text(json.dumps(data, indent=1), encoding="utf-8")
|
||||||
|
self.assertEqual([error for error in data["errors"] if "/favicon.ico" not in error], [])
|
||||||
|
self.check(data, "firefox")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
unittest.main()
|
||||||
+689
-16
@@ -1,22 +1,107 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import contextlib
|
||||||
|
import dataclasses
|
||||||
|
import io
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
|
import subprocess
|
||||||
import tempfile
|
import tempfile
|
||||||
|
import threading
|
||||||
|
import time
|
||||||
import types
|
import types
|
||||||
import unittest
|
import unittest
|
||||||
|
from collections.abc import Callable
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from unittest import mock
|
from unittest import mock
|
||||||
|
|
||||||
import hub_support # noqa: F401
|
import hub_support # noqa: F401
|
||||||
from kvb_orchestrator import receipts
|
from hub_case import HubCase
|
||||||
from kvb_orchestrator.names import Directory, Names
|
from kvb_orchestrator import (
|
||||||
|
choosing,
|
||||||
|
council,
|
||||||
|
hub,
|
||||||
|
messaging,
|
||||||
|
receipts,
|
||||||
|
records,
|
||||||
|
skill_scripts,
|
||||||
|
)
|
||||||
|
from kvb_orchestrator.names import Directory
|
||||||
|
from kvb_orchestrator.presence import SNAPSHOT, SNAPSHOT_STALE
|
||||||
|
from kvb_orchestrator.view import RADIO_WINDOW
|
||||||
|
|
||||||
REPO_ROOT = Path(__file__).resolve().parents[1]
|
REPO_ROOT = Path(__file__).resolve().parents[1]
|
||||||
|
TEMPLATES = REPO_ROOT / "orchestrator" / "templates"
|
||||||
NOW = 1_800_000_000.0
|
NOW = 1_800_000_000.0
|
||||||
T = NOW - 1000
|
T = NOW - 1000
|
||||||
EARLY = T - 100
|
EARLY = T - 100
|
||||||
WINDOW = (T, T + 100)
|
WINDOW = (T, T + 100)
|
||||||
|
PAGE_PROBE = """
|
||||||
|
const fs = require("fs");
|
||||||
|
const vm = require("vm");
|
||||||
|
const Receipts = require(process.argv[1]);
|
||||||
|
const RadioSet = require(process.argv[2]);
|
||||||
|
const code = ["page-dom.js", "page-stream.js", "page-radioview.js", "page-agent.js"].map((name) => fs.readFileSync(`${process.argv[3]}/${name}`, "utf8")).join("\\n");
|
||||||
|
const input = JSON.parse(process.argv[4]);
|
||||||
|
class Node {
|
||||||
|
constructor(tag) {
|
||||||
|
Object.assign(this, {tag, attrs: {}, children: []});
|
||||||
|
}
|
||||||
|
setAttribute(key, value) {
|
||||||
|
this.attrs[key] = value;
|
||||||
|
}
|
||||||
|
addEventListener() {}
|
||||||
|
append(child) {
|
||||||
|
this.children.push(child);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const text = (node) => (typeof node === "string" ? node : node.children.map(text).join(""));
|
||||||
|
const find = (node, test, out = []) => {
|
||||||
|
if (typeof node !== "string") {
|
||||||
|
if (test(node)) {
|
||||||
|
out.push(node);
|
||||||
|
}
|
||||||
|
node.children.forEach((child) => find(child, test, out));
|
||||||
|
}
|
||||||
|
return out;
|
||||||
|
};
|
||||||
|
const sandbox = {
|
||||||
|
Receipts, RadioSet, Node, NS: "svg", HUMAN: "Operator", DAY: 86400, WEEKDAYS: [],
|
||||||
|
document: {createElement: (tag) => new Node(tag), createElementNS: (ns, tag) => new Node(tag)},
|
||||||
|
relative: {format: () => "ago"}, absolute: {format: (date) => `at ${date.getTime() / 1000}`},
|
||||||
|
Faces: {of: () => ({parts: []}), SIZE: 10}, speech: {current: null}, look: {hidden: new Set()}, PRESENCE: {}, STATUS: {},
|
||||||
|
state: {inbox: [], messages: [], radio: [], people: input.people}, CLASSES: {call: "Call", receipt: "Read"}, PILLS: {call: "info"}, ui: {}, KINDS: [["review", "Reviews"]], KIND_TONES: {},
|
||||||
|
};
|
||||||
|
vm.runInNewContext(`${code}\\nglobalThis.probe = {eventRow, logLine, history, readerRow};`, sandbox);
|
||||||
|
const shape = (node) => ({
|
||||||
|
route: text(find(node, (item) => item.attrs.class === "route")[0] || {children: []}),
|
||||||
|
routes: find(node, (item) => item.attrs.class === "radio-route").map(text),
|
||||||
|
readers: find(node, (item) => item.attrs.class === "reader").map((item) => ({
|
||||||
|
session: item.attrs["data-session"], acked: item.attrs["data-acked"], title: item.attrs.title, label: item.attrs["aria-label"],
|
||||||
|
badges: find(item, (part) => part.attrs.class === "ack").length, face: find(item, (part) => part.attrs["data-face"] !== undefined).map((part) => part.attrs["data-face"]),
|
||||||
|
})),
|
||||||
|
more: find(node, (item) => item.attrs.class === "readers-more").map((item) => ({text: text(item), title: item.attrs.title})),
|
||||||
|
groups: find(node, (item) => item.attrs.class === "readers").map((item) => item.attrs["aria-label"]),
|
||||||
|
});
|
||||||
|
console.log(JSON.stringify({
|
||||||
|
events: input.events.map((entry) => shape(sandbox.probe.eventRow(entry))),
|
||||||
|
logs: input.logs.map((row) => shape(sandbox.probe.logLine(row))),
|
||||||
|
module: {
|
||||||
|
strip: input.strips.map((row) => Receipts.strip(row)),
|
||||||
|
labels: input.labels.map((reader) => Receipts.label(reader, (ts) => `t${ts}`)),
|
||||||
|
headlines: input.events.map((entry) => Receipts.headline(entry)),
|
||||||
|
signatures: input.signatures.map((rows) => Receipts.signature(rows)),
|
||||||
|
},
|
||||||
|
agent: (() => {
|
||||||
|
Object.assign(sandbox.state, input.agent.state);
|
||||||
|
const sent = RadioSet.logRows(sandbox.state.radio, {tab: "sent", chips: {class: "", from: "", to: "", agent: ""}, q: ""}, "Operator", sandbox.state.messages);
|
||||||
|
return {
|
||||||
|
history: sandbox.probe.history(input.agent.key).map((row) => ({who: row.who, text: row.text, ...shape(sandbox.probe.readerRow(row) || new Node("none"))})),
|
||||||
|
sent: sent.map((row) => ({text: row.text, ...shape(sandbox.probe.logLine(row))})),
|
||||||
|
};
|
||||||
|
})(),
|
||||||
|
}));
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
def message(cls: str, ts: float, to: list[str], sender: str = "One") -> dict:
|
def message(cls: str, ts: float, to: list[str], sender: str = "One") -> dict:
|
||||||
@@ -56,8 +141,8 @@ class TestReceipts(unittest.TestCase):
|
|||||||
def prompt(self, sid: str, text: str) -> None:
|
def prompt(self, sid: str, text: str) -> None:
|
||||||
(self.council / receipts.PROMPT / sid).write_text(text, encoding="utf-8")
|
(self.council / receipts.PROMPT / sid).write_text(text, encoding="utf-8")
|
||||||
|
|
||||||
def inline(self, news: list[dict], names: Directory, window: tuple[float, float] = WINDOW) -> dict[float, dict]:
|
def inline(self, news: list[dict], names: Directory, window: tuple[float, float] = WINDOW, sighted: dict | None = None) -> dict[float, dict]:
|
||||||
return receipts.readers_and_acks(self.radio, news, window, receipts.read_marks(self.council, NOW), names)
|
return receipts.readers_and_acks(self.radio, news, window, receipts.read_marks(self.council, NOW), names, sighted or {})
|
||||||
|
|
||||||
def test_every_class_gets_its_readers_by_channel_and_its_acks_from_receipts(self):
|
def test_every_class_gets_its_readers_by_channel_and_its_acks_from_receipts(self):
|
||||||
news = [
|
news = [
|
||||||
@@ -86,7 +171,7 @@ class TestReceipts(unittest.TestCase):
|
|||||||
T + 6: ([("s3", None), ("s2", None)], []),
|
T + 6: ([("s3", None), ("s2", None)], []),
|
||||||
T + 7: ([("s3", None), ("s2", None)], []),
|
T + 7: ([("s3", None), ("s2", None)], []),
|
||||||
})
|
})
|
||||||
self.assertEqual(found[T + 3]["readers"], [{"session": "s2", "name": "Two", "read": None}, {"session": "s4", "name": "s4", "read": None}])
|
self.assertEqual(found[T + 3]["readers"], [{"session": "s2", "name": "Two", "read": None, "approximate": False}, {"session": "s4", "name": "s4", "read": None, "approximate": False}])
|
||||||
|
|
||||||
def test_an_ended_session_keeps_its_read_its_ack_and_its_own_broadcast_by_its_name_history(self):
|
def test_an_ended_session_keeps_its_read_its_ack_and_its_own_broadcast_by_its_name_history(self):
|
||||||
news = [message("call", T + 10, ["Two"]), receipt("Two", T + 11, T + 10), message("broadcast", T + 20, [])]
|
news = [message("call", T + 10, ["Two"]), receipt("Two", T + 11, T + 10), message("broadcast", T + 20, [])]
|
||||||
@@ -114,8 +199,8 @@ class TestReceipts(unittest.TestCase):
|
|||||||
file = self.base / "receipt-names.json"
|
file = self.base / "receipt-names.json"
|
||||||
forged = [{"session": "sA", "name": "Two", "first_seen": EARLY, "source": "claim"}, {"session": "s2", "name": "Ghost", "first_seen": T + 5, "source": "claim"}]
|
forged = [{"session": "sA", "name": "Two", "first_seen": EARLY, "source": "claim"}, {"session": "s2", "name": "Ghost", "first_seen": T + 5, "source": "claim"}]
|
||||||
file.write_text(json.dumps({"entries": forged, "observed": {}}), encoding="utf-8")
|
file.write_text(json.dumps({"entries": forged, "observed": {}}), encoding="utf-8")
|
||||||
live = receipts.session_directory(Names(file), {"s2": {"name": "Two"}, "s3": {"name": {"x": 1}}, "s4": {}}, EARLY)
|
live = receipts.session_directory(receipts.ReceiptNames(file), {"s2": {"name": "Two"}, "s3": {"name": {"x": 1}}, "s4": {}}, EARLY)
|
||||||
ended = receipts.session_directory(Names(file), {}, T + 50)
|
ended = receipts.session_directory(receipts.ReceiptNames(file), {}, T + 50)
|
||||||
for names in (live, ended):
|
for names in (live, ended):
|
||||||
with self.subTest(names=names):
|
with self.subTest(names=names):
|
||||||
self.assertEqual((names.aliases("Two"), names.at("s2", T + 10), names.at("s3", T), names.aliases("Ghost")), ({"s2"}, "Two", "", set()))
|
self.assertEqual((names.aliases("Two"), names.at("s2", T + 10), names.at("s3", T), names.aliases("Ghost")), ({"s2"}, "Two", "", set()))
|
||||||
@@ -126,18 +211,27 @@ class TestReceipts(unittest.TestCase):
|
|||||||
self.assertEqual((who(found[T + 10]), found[T + 10]["acks"]), ([("s2", T + 11)], ["s2"]))
|
self.assertEqual((who(found[T + 10]), found[T + 10]["acks"]), ([("s2", T + 11)], ["s2"]))
|
||||||
|
|
||||||
def test_snapshot_names_match_rows_and_receipts_unnormalized_as_radio_does(self):
|
def test_snapshot_names_match_rows_and_receipts_unnormalized_as_radio_does(self):
|
||||||
names = receipts.session_directory(Names(self.base / "raw.json"), {"s2": {"name": "Two session"}}, EARLY)
|
names = receipts.session_directory(receipts.ReceiptNames(self.base / "raw.json"), {"s2": {"name": "Two session"}}, EARLY)
|
||||||
news = [message("call", T + 10, ["Two session"]), receipt("Two session", T + 11, T + 10), message("call", T + 20, ["Two"]), receipt("Two", T + 21, T + 20)]
|
news = [message("call", T + 10, ["Two session"]), receipt("Two session", T + 11, T + 10), message("call", T + 20, ["Two"]), receipt("Two", T + 21, T + 20)]
|
||||||
self.state("s2", json.dumps({"radio": T + 30}))
|
self.state("s2", json.dumps({"radio": T + 30}))
|
||||||
found = self.inline(news, names)
|
found = self.inline(news, names)
|
||||||
self.assertEqual((who(found[T + 10]), found[T + 10]["acks"]), ([("s2", T + 11)], ["s2"]))
|
self.assertEqual((who(found[T + 10]), found[T + 10]["acks"]), ([("s2", T + 11)], ["s2"]))
|
||||||
self.assertEqual((who(found[T + 20]), found[T + 20]["acks"]), ([], []))
|
self.assertEqual((who(found[T + 20]), found[T + 20]["acks"]), ([], []))
|
||||||
|
|
||||||
def test_a_reader_without_a_receipt_gets_no_read_time_from_a_later_mark(self):
|
def test_a_reader_reads_at_its_receipt_else_about_at_its_first_sighting_and_never_at_a_mark(self):
|
||||||
news = [message("mayday", T + 10, []), message("call", T + 20, ["Two"]), receipt("Two", T + 21, T + 20)]
|
news = [message("mayday", T + 10, []), message("call", T + 20, ["Two"]), receipt("Two", T + 21, T + 20), message("post", T + 30, ["Two"]), message("newsletter", T + 40, [])]
|
||||||
self.state("s2", json.dumps({"radio": T + 90}))
|
self.state("s2", json.dumps({"radio": T + 90, "mail": T + 35}))
|
||||||
found = self.inline(news, NAMED)
|
self.prompt("s2", str(T + 50))
|
||||||
self.assertEqual((who(found[T + 10]), who(found[T + 20])), ([("s2", None)], [("s2", T + 21)]))
|
found = self.inline(news, NAMED, sighted={("s2", T + 10): NOW - 5, ("s2", T + 20): NOW - 1, ("s2", T + 30): None, ("s3", T + 40): NOW})
|
||||||
|
readers = [found[T + n]["readers"] for n in (10, 20, 30, 40)]
|
||||||
|
self.assertEqual([[(entry["read"], entry["approximate"]) for entry in row] for row in readers], [[(NOW - 5, True)], [(T + 21, False)], [(None, False)], [(None, False)]])
|
||||||
|
self.state("s2", json.dumps({"mail": T + 35}))
|
||||||
|
self.prompt("s2", "")
|
||||||
|
self.assertEqual([who(self.inline(news, NAMED)[T + n]) for n in (10, 30, 40)], [[], [("s2", None)], []])
|
||||||
|
reader = receipts.reader(self.radio, message("post", T + 30, ["Two"]), "s2", "Two", (None, {"radio": T + 90}), {}, {("s2", T + 30): NOW})
|
||||||
|
self.assertIsNone(reader)
|
||||||
|
spy = types.SimpleNamespace(**{**vars(self.radio), "seen": lambda *args: True})
|
||||||
|
self.assertEqual(receipts.reader(spy, message("post", T + 30, ["Two"]), "s2", "Two", (T + 99, {"mail": T + 99}), {}, {})["read"], None)
|
||||||
|
|
||||||
def test_a_missing_malformed_unreadable_oversized_or_future_mark_counts_as_not_read(self):
|
def test_a_missing_malformed_unreadable_oversized_or_future_mark_counts_as_not_read(self):
|
||||||
news = [message("broadcast", T + 4, [])]
|
news = [message("broadcast", T + 4, [])]
|
||||||
@@ -181,7 +275,7 @@ class TestReceipts(unittest.TestCase):
|
|||||||
with self.subTest(sid=sid):
|
with self.subTest(sid=sid):
|
||||||
self.assertEqual(marks[sid], (None, {}))
|
self.assertEqual(marks[sid], (None, {}))
|
||||||
self.assertEqual((marks["good"], marks["p-good"]), ((None, {"radio": NOW}), (NOW, {})))
|
self.assertEqual((marks["good"], marks["p-good"]), ((None, {"radio": NOW}), (NOW, {})))
|
||||||
found = receipts.readers_and_acks(self.radio, news, WINDOW, marks, directory())
|
found = receipts.readers_and_acks(self.radio, news, WINDOW, marks, directory(), {})
|
||||||
self.assertEqual(who(found[T + 4]), [("good", None), ("p-good", None)])
|
self.assertEqual(who(found[T + 4]), [("good", None), ("p-good", None)])
|
||||||
|
|
||||||
def test_marks_are_parsed_again_only_after_the_file_changed(self):
|
def test_marks_are_parsed_again_only_after_the_file_changed(self):
|
||||||
@@ -236,7 +330,7 @@ class TestReceipts(unittest.TestCase):
|
|||||||
spy = types.SimpleNamespace(**{**vars(self.radio), "seen": seen})
|
spy = types.SimpleNamespace(**{**vars(self.radio), "seen": seen})
|
||||||
news = [message("broadcast", T + 4, [])]
|
news = [message("broadcast", T + 4, [])]
|
||||||
self.state("s2", json.dumps({"radio": NOW}))
|
self.state("s2", json.dumps({"radio": NOW}))
|
||||||
found = receipts.readers_and_acks(spy, news, WINDOW, receipts.read_marks(self.council, NOW), NAMED)
|
found = receipts.readers_and_acks(spy, news, WINDOW, receipts.read_marks(self.council, NOW), NAMED, {})
|
||||||
self.assertEqual(found, {T + 4: {"readers": [], "acks": []}})
|
self.assertEqual(found, {T + 4: {"readers": [], "acks": []}})
|
||||||
self.assertEqual(calls, [(news[0], "Two", "s2", self.radio.NEVER, {"radio": NOW}, self.radio.NEVER)])
|
self.assertEqual(calls, [(news[0], "Two", "s2", self.radio.NEVER, {"radio": NOW}, self.radio.NEVER)])
|
||||||
|
|
||||||
@@ -250,7 +344,7 @@ class TestReceipts(unittest.TestCase):
|
|||||||
self.state("s9", json.dumps({"radio": NOW}))
|
self.state("s9", json.dumps({"radio": NOW}))
|
||||||
found = self.inline(news, directory())
|
found = self.inline(news, directory())
|
||||||
self.assertEqual(list(found), [T + 6])
|
self.assertEqual(list(found), [T + 6])
|
||||||
self.assertEqual(found[T + 6]["readers"], [{"session": "s9", "name": "s9", "read": None}])
|
self.assertEqual(found[T + 6]["readers"], [{"session": "s9", "name": "s9", "read": None, "approximate": False}])
|
||||||
|
|
||||||
def test_read_marks_without_mark_folders_is_empty(self):
|
def test_read_marks_without_mark_folders_is_empty(self):
|
||||||
self.assertEqual(receipts.read_marks(self.base / "missing", NOW), {})
|
self.assertEqual(receipts.read_marks(self.base / "missing", NOW), {})
|
||||||
@@ -273,5 +367,584 @@ class TestReceipts(unittest.TestCase):
|
|||||||
self.assertEqual(tuple(receipts.inspect.signature(self.radio.seen).parameters), receipts.SEEN_PARAMETERS)
|
self.assertEqual(tuple(receipts.inspect.signature(self.radio.seen).parameters), receipts.SEEN_PARAMETERS)
|
||||||
|
|
||||||
|
|
||||||
|
class TestReceiptNames(unittest.TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
tmp = tempfile.TemporaryDirectory()
|
||||||
|
self.addCleanup(tmp.cleanup)
|
||||||
|
self.file = Path(tmp.name) / receipts.NAMES
|
||||||
|
|
||||||
|
def sessions(self, store: receipts.ReceiptNames) -> set[str]:
|
||||||
|
return {entry["session"] for entry in store.entries}
|
||||||
|
|
||||||
|
def test_a_session_gone_longer_than_kept_loses_its_entries_and_comes_back_when_sighted_again(self):
|
||||||
|
store = receipts.ReceiptNames(self.file)
|
||||||
|
receipts.session_directory(store, {"s1": {"name": "One"}, "s2": {"name": "Two"}}, NOW)
|
||||||
|
receipts.session_directory(store, {"s1": {"name": "One"}}, NOW + receipts.KEPT)
|
||||||
|
self.assertEqual(self.sessions(store), {"s1", "s2"})
|
||||||
|
names = receipts.session_directory(store, {"s1": {"name": "One"}}, NOW + receipts.KEPT + 1)
|
||||||
|
self.assertEqual((self.sessions(store), names.aliases("Two")), ({"s1"}, set()))
|
||||||
|
again = receipts.ReceiptNames(self.file)
|
||||||
|
self.assertEqual((self.sessions(again), set(again.observed), again.sighted), ({"s1"}, {"s1\x00session"}, {"s1": NOW + receipts.KEPT}))
|
||||||
|
receipts.session_directory(again, {"s2": {"name": "Two"}}, NOW + receipts.KEPT + 2)
|
||||||
|
self.assertEqual(self.sessions(again), {"s1", "s2"})
|
||||||
|
|
||||||
|
def test_a_sighting_is_stored_at_most_every_step(self):
|
||||||
|
store = receipts.ReceiptNames(self.file)
|
||||||
|
receipts.session_directory(store, {"s1": {"name": "One"}}, NOW)
|
||||||
|
with mock.patch.object(store, "save", wraps=store.save) as saves:
|
||||||
|
receipts.session_directory(store, {"s1": {"name": "One"}}, NOW + receipts.STEP - 1)
|
||||||
|
self.assertEqual((saves.call_count, store.sighted), (0, {"s1": NOW}))
|
||||||
|
receipts.session_directory(store, {"s1": {"name": "One"}}, NOW + receipts.STEP)
|
||||||
|
self.assertEqual((saves.call_count, store.sighted), (1, {"s1": NOW + receipts.STEP}))
|
||||||
|
|
||||||
|
def test_past_the_entry_cap_the_sessions_sighted_longest_ago_go_first_and_the_file_stays_readable(self):
|
||||||
|
with mock.patch.object(receipts, "ENTRIES", 3):
|
||||||
|
store = receipts.ReceiptNames(self.file)
|
||||||
|
for n in range(5):
|
||||||
|
receipts.session_directory(store, {f"s{n}": {"name": f"N{n}"}}, NOW + n)
|
||||||
|
self.assertEqual(self.sessions(store), {"s2", "s3", "s4"})
|
||||||
|
crowd = {f"{n:0128d}": {"name": "N" * receipts.NAME_CHARS} for n in range(receipts.ENTRIES + 500)}
|
||||||
|
store = receipts.ReceiptNames(self.file)
|
||||||
|
receipts.session_directory(store, {**crowd, "long": {"name": "N" * (receipts.NAME_CHARS + 1)}}, NOW + 10)
|
||||||
|
self.assertEqual(len(store.entries), receipts.ENTRIES)
|
||||||
|
self.assertNotIn("long", self.sessions(store))
|
||||||
|
self.assertLess(self.file.stat().st_size, records.JSON_LIMIT // 2)
|
||||||
|
self.assertEqual(len(receipts.ReceiptNames(self.file).entries), receipts.ENTRIES)
|
||||||
|
|
||||||
|
def test_a_store_with_malformed_values_keeps_only_its_valid_entries(self):
|
||||||
|
good = {"session": "s1", "name": "One", "first_seen": EARLY, "source": "session"}
|
||||||
|
self.file.write_text(json.dumps({
|
||||||
|
"entries": [
|
||||||
|
good, {**good, "session": 7}, {**good, "session": ""}, {**good, "name": ["One"]}, {**good, "first_seen": "soon"},
|
||||||
|
{**good, "first_seen": True}, {**good, "first_seen": float("nan")}, {**good, "source": None}, {**good, "extra": 1}, "junk",
|
||||||
|
{**good, "session": "bad id!"}, {**good, "session": "s" * 129}, {**good, "name": "N" * (receipts.NAME_CHARS + 1)},
|
||||||
|
],
|
||||||
|
"observed": {"s1\x00session": "One", "s2\x00session": ["Two"], "bad id!\x00session": "X", "s4\x00session": "N" * (receipts.NAME_CHARS + 1)},
|
||||||
|
"sighted": {"s1": T + 1e6, "s2": "x", "s3": None, "bad id!": EARLY},
|
||||||
|
}), encoding="utf-8")
|
||||||
|
store = receipts.ReceiptNames(self.file)
|
||||||
|
self.assertEqual((store.entries, store.observed, store.sighted), ([good], {"s1\x00session": "One"}, {"s1": T + 1e6}))
|
||||||
|
names = receipts.session_directory(store, {"s2": {"name": "Two"}, "bad id!": {"name": "X"}, "s3": {"name": 3}}, T)
|
||||||
|
self.assertEqual((names.at("s1", T), names.at("s2", T), names.aliases("X"), names.at("s3", T)), ("One", "Two", set(), ""))
|
||||||
|
self.assertEqual((store.sighted["s1"], receipts.ReceiptNames(self.file).sighted["s1"]), (T, T))
|
||||||
|
self.file.write_text(json.dumps(["not", "a", "store"]), encoding="utf-8")
|
||||||
|
self.assertEqual(receipts.ReceiptNames(self.file).payload(), {"entries": [], "observed": {}, "sighted": {}})
|
||||||
|
|
||||||
|
|
||||||
|
class TestReceiptReads(unittest.TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
tmp = tempfile.TemporaryDirectory()
|
||||||
|
self.addCleanup(tmp.cleanup)
|
||||||
|
self.file = Path(tmp.name) / receipts.READS
|
||||||
|
|
||||||
|
def test_rows_covered_at_the_first_pass_of_a_process_keep_no_time_and_later_ones_their_first_sighting_across_restarts(self):
|
||||||
|
reads = receipts.ReceiptReads(self.file)
|
||||||
|
reads.see({("s2", T + 1)}, NOW, T, True)
|
||||||
|
reads.see({("s2", T + 1), ("s2", T + 2)}, NOW + 15, T, True)
|
||||||
|
reads.see({("s2", T + 1), ("s2", T + 2)}, NOW + 30, T, True)
|
||||||
|
self.assertEqual(reads.listed(NOW + 99), {("s2", T + 1): None, ("s2", T + 2): NOW + 15})
|
||||||
|
again = receipts.ReceiptReads(self.file)
|
||||||
|
self.assertEqual(again.listed(NOW + 99), reads.listed(NOW + 99))
|
||||||
|
again.see({("s2", T + 2), ("s3", T + 2)}, NOW + 45, T + 2, True)
|
||||||
|
again.see({("s4", T + 3), ("s5", T + 1)}, NOW + 60, T + 2, True)
|
||||||
|
self.assertEqual(again.listed(NOW + 99), {("s2", T + 2): NOW + 15, ("s3", T + 2): None, ("s4", T + 3): NOW + 60})
|
||||||
|
self.assertEqual(json.loads(self.file.read_text(encoding="utf-8")), {"reads": [["s2", T + 2, NOW + 15], ["s3", T + 2, None], ["s4", T + 3, NOW + 60]]})
|
||||||
|
|
||||||
|
def test_the_baseline_waits_for_a_pass_with_a_fresh_snapshot_and_comes_back_after_a_stale_one(self):
|
||||||
|
reads = receipts.ReceiptReads(self.file)
|
||||||
|
covered: set[tuple[str, float]] = set()
|
||||||
|
for n, fresh in enumerate((False, False, True, True, False, True, True)):
|
||||||
|
covered.add(("s7", T + n))
|
||||||
|
reads.see(set(covered), NOW + 15 * n, T, fresh)
|
||||||
|
self.assertEqual(reads.listed(NOW + 999), {
|
||||||
|
("s7", T): None, ("s7", T + 1): None, ("s7", T + 2): None, ("s7", T + 3): NOW + 45,
|
||||||
|
("s7", T + 4): NOW + 60, ("s7", T + 5): None, ("s7", T + 6): NOW + 90,
|
||||||
|
})
|
||||||
|
|
||||||
|
def test_a_pass_after_a_gap_longer_than_two_sighting_intervals_stamps_nothing(self):
|
||||||
|
reads = receipts.ReceiptReads(self.file)
|
||||||
|
moments = (NOW, NOW + 2 * receipts.SIGHTING, NOW + 2 * receipts.SIGHTING + 3600, NOW + 3 * receipts.SIGHTING + 3600)
|
||||||
|
for n, now in enumerate(moments):
|
||||||
|
reads.see({("s7", T + k) for k in range(n + 1)}, now, T, True)
|
||||||
|
self.assertEqual(reads.listed(NOW + 9999), {("s7", T): None, ("s7", T + 1): moments[1], ("s7", T + 2): None, ("s7", T + 3): moments[3]})
|
||||||
|
|
||||||
|
def test_past_the_entry_cap_the_oldest_rows_go_whole_and_never_come_back_with_a_later_time(self):
|
||||||
|
pairs = {("a", T + 1), ("b", T + 2), ("c", T + 2), ("d", T + 3), ("e", T + 4)}
|
||||||
|
with mock.patch.object(receipts, "READ_ENTRIES", 3):
|
||||||
|
reads = receipts.ReceiptReads(self.file)
|
||||||
|
reads.see(set(), NOW, T, True)
|
||||||
|
reads.see(pairs, NOW + 15, T, True)
|
||||||
|
reads.see(pairs, NOW + 30, T, True)
|
||||||
|
self.assertEqual(reads.listed(NOW + 99), {("d", T + 3): NOW + 15, ("e", T + 4): NOW + 15})
|
||||||
|
|
||||||
|
def test_a_store_with_malformed_values_keeps_only_its_valid_entries_and_clamps_a_future_sighting(self):
|
||||||
|
self.file.write_text(json.dumps({"reads": [
|
||||||
|
["s1", T, T + 5], ["s1", T + 1, None], ["bad id!", T, T], ["s2", "x", T], ["s3", T, "soon"], ["s4", T, 9e10],
|
||||||
|
["s5", T], ["s6", True, T], ["s7", T, True], [7, T, T], "junk", ["s8", T, T - 1], ["s9", T, T],
|
||||||
|
]}), encoding="utf-8")
|
||||||
|
reads = receipts.ReceiptReads(self.file)
|
||||||
|
self.assertEqual(reads.listed(NOW), {("s1", T): T + 5, ("s1", T + 1): None, ("s4", T): NOW, ("s9", T): T})
|
||||||
|
reads.see(set(), NOW, T, True)
|
||||||
|
self.assertEqual(receipts.ReceiptReads(self.file).listed(9e10), {("s1", T): T + 5, ("s1", T + 1): None, ("s4", T): NOW, ("s9", T): T})
|
||||||
|
self.file.write_text(json.dumps(["not", "a", "store"]), encoding="utf-8")
|
||||||
|
self.assertEqual(receipts.ReceiptReads(self.file).listed(NOW), {})
|
||||||
|
|
||||||
|
|
||||||
|
class TestRadioRun(unittest.TestCase):
|
||||||
|
def test_a_run_killed_at_the_timeout_returns_the_ts_line_it_printed_before(self):
|
||||||
|
tmp = tempfile.TemporaryDirectory()
|
||||||
|
self.addCleanup(tmp.cleanup)
|
||||||
|
base = Path(tmp.name)
|
||||||
|
script = base / "skills" / skill_scripts.RADIO
|
||||||
|
script.parent.mkdir(parents=True)
|
||||||
|
script.write_text("import sys, time\nprint('ts: 1800000000.5', flush=True)\ntime.sleep(30)\n", encoding="utf-8")
|
||||||
|
with mock.patch.object(skill_scripts, "SCRIPT_TIMEOUT", 1):
|
||||||
|
run = skill_scripts.Scripts(base / "skills", base / "council", base).run(skill_scripts.RADIO, ["send", "call"])
|
||||||
|
self.assertEqual((run["code"], run["stdout"], messaging.stored_ts(run["stdout"])), (-1, "ts: 1800000000.5\n", 1800000000.5))
|
||||||
|
|
||||||
|
|
||||||
|
class TestServedReceipts(HubCase):
|
||||||
|
def setUp(self):
|
||||||
|
super().setUp()
|
||||||
|
self.now = time.time()
|
||||||
|
self.t = self.now - 600
|
||||||
|
|
||||||
|
def sessions(self, records: dict[str, str], written: float | None = None) -> None:
|
||||||
|
payload = {"written": time.time() if written is None else written, "sessions": {sid: {"name": name, "running": True} for sid, name in records.items()}}
|
||||||
|
(self.state / SNAPSHOT).write_text(json.dumps(payload), encoding="utf-8")
|
||||||
|
|
||||||
|
def marks(self, marks: dict[str, dict]) -> None:
|
||||||
|
self.write_council({f"{receipts.SEEN}/{sid}.json": mark for sid, mark in marks.items()})
|
||||||
|
|
||||||
|
def fixture(self) -> None:
|
||||||
|
t = self.t
|
||||||
|
self.sessions({"s1": "One", "s2": "Two", "s3": "Three"})
|
||||||
|
self.write_council({"news.jsonl": self.jsonl([
|
||||||
|
message("post", t + 1, ["Two"]),
|
||||||
|
message("call", t + 2, ["Two", "Three"]),
|
||||||
|
message("broadcast", t + 3, []),
|
||||||
|
receipt("Two", t + 4, t + 2),
|
||||||
|
receipt("Three", t + 5, t + 2),
|
||||||
|
])})
|
||||||
|
self.marks({"s2": {"radio": t + 10, "mail": t + 10}, "s3": {"radio": t + 10}})
|
||||||
|
|
||||||
|
def served(self, rows: list[dict]) -> dict[float, tuple]:
|
||||||
|
return {row["ts"]: ([(reader["session"], reader["name"], reader["read"]) for reader in row["readers"]], row["acks"]) for row in rows}
|
||||||
|
|
||||||
|
def test_the_state_carries_readers_and_acks_per_message_and_serves_no_receipt_row(self):
|
||||||
|
self.fixture()
|
||||||
|
t = self.t
|
||||||
|
state = self.snapshot()
|
||||||
|
self.assertEqual([row["class"] for row in state["radio"]], ["post", "call", "broadcast"])
|
||||||
|
self.assertEqual(self.served(state["radio"]), {
|
||||||
|
t + 1: ([("s2", "Two", None)], []),
|
||||||
|
t + 2: ([("s2", "Two", t + 4), ("s3", "Three", t + 5)], ["s2", "s3"]),
|
||||||
|
t + 3: ([("s3", "Three", None), ("s2", "Two", None)], []),
|
||||||
|
})
|
||||||
|
self.assertNotIn(council.RECEIPT, {edge["class"] for edge in state["graph"]["edges"]})
|
||||||
|
self.assertIn("const Receipts = ", self.index())
|
||||||
|
status, _, body = self.get(f"/api/state?from={t!r}&to={t + 3!r}")
|
||||||
|
self.assertEqual(status, 200, body)
|
||||||
|
narrowed = json.loads(body)["radio"]
|
||||||
|
self.assertEqual(self.served(narrowed)[t + 2], ([("s2", "Two", t + 4), ("s3", "Three", t + 5)], ["s2", "s3"]))
|
||||||
|
self.assertEqual(len(narrowed), 3)
|
||||||
|
|
||||||
|
def test_the_stream_carries_readers_and_acks_in_the_ref_and_no_receipt_entry(self):
|
||||||
|
self.fixture()
|
||||||
|
t = self.t
|
||||||
|
radio = [entry for entry in self.events("view=funk")["events"] if entry["kind"] == "radio"]
|
||||||
|
self.assertEqual([entry["part"] for entry in radio], ["post", "call", "broadcast"])
|
||||||
|
self.assertEqual(self.served([entry["ref"] | {"ts": entry["ts"]} for entry in radio]), {
|
||||||
|
t + 1: ([("s2", "Two", None)], []),
|
||||||
|
t + 2: ([("s2", "Two", t + 4), ("s3", "Three", t + 5)], ["s2", "s3"]),
|
||||||
|
t + 3: ([("s3", "Three", None), ("s2", "Two", None)], []),
|
||||||
|
})
|
||||||
|
|
||||||
|
def test_an_advanced_mark_shows_its_reader_on_the_next_state_build(self):
|
||||||
|
self.fixture()
|
||||||
|
t = self.t
|
||||||
|
self.marks({"s2": {"radio": t + 10}})
|
||||||
|
self.assertEqual(self.served(self.snapshot()["radio"])[t + 1], ([], []))
|
||||||
|
self.marks({"s2": {"radio": t + 10, "mail": t + 10}})
|
||||||
|
self.assertEqual(self.served(self.snapshot()["radio"])[t + 1], ([("s2", "Two", None)], []))
|
||||||
|
|
||||||
|
def test_a_session_only_the_sighting_thread_saw_keeps_its_name_after_it_left(self):
|
||||||
|
t = self.t
|
||||||
|
self.sessions({"s9": "Nine"})
|
||||||
|
stop = threading.Event()
|
||||||
|
thread = hub.sighting(self.httpd.hub, stop)
|
||||||
|
self.settle(lambda: any(entry["session"] == "s9" for entry in self.httpd.hub.receipt_names.listed()))
|
||||||
|
stop.set()
|
||||||
|
thread.join(5)
|
||||||
|
self.assertFalse(thread.is_alive())
|
||||||
|
self.assertTrue(thread.daemon)
|
||||||
|
self.sessions({})
|
||||||
|
self.write_council({"news.jsonl": self.jsonl([message("call", t + 1, ["Nine"]), receipt("Nine", t + 2, t + 1)])})
|
||||||
|
self.marks({"s9": {"radio": t + 10}})
|
||||||
|
self.assertEqual(self.served(self.snapshot()["radio"]), {t + 1: ([("s9", "Nine", t + 2)], ["s9"])})
|
||||||
|
self.assertTrue((self.state / receipts.NAMES).is_file())
|
||||||
|
self.assertFalse((self.council / receipts.NAMES).exists())
|
||||||
|
|
||||||
|
def test_a_malformed_receipt_store_does_not_break_the_state(self):
|
||||||
|
self.fixture()
|
||||||
|
damaged = self.state / "damaged.json"
|
||||||
|
bad = [{"session": "s2", "name": "Two", "first_seen": "soon", "source": "session"}, {"session": "s3", "name": {"x": 1}, "first_seen": 1.0, "source": "session"}]
|
||||||
|
damaged.write_text(json.dumps({"entries": bad, "observed": {"s2\x00session": 5}, "sighted": {"s2": "x"}}), encoding="utf-8")
|
||||||
|
self.httpd.hub = dataclasses.replace(self.httpd.hub, receipt_names=receipts.ReceiptNames(damaged))
|
||||||
|
served = self.served(self.snapshot()["radio"])
|
||||||
|
self.assertEqual(served[self.t + 2], ([("s2", "Two", self.t + 4), ("s3", "Three", self.t + 5)], ["s2", "s3"]))
|
||||||
|
self.assertEqual(self.events("view=funk")["events"][1]["ref"]["acks"], ["s2", "s3"])
|
||||||
|
|
||||||
|
def test_the_sighting_thread_logs_one_line_per_failing_pass_and_survives_all_but_a_system_exit(self):
|
||||||
|
real, calls = receipts.session_directory, []
|
||||||
|
|
||||||
|
def flaky(store: receipts.ReceiptNames, sessions: dict[str, dict], now: float) -> Directory:
|
||||||
|
calls.append(now)
|
||||||
|
if len(calls) <= 3:
|
||||||
|
raise (ValueError, TypeError, OSError)[len(calls) - 1]("broken\npass")
|
||||||
|
return real(store, sessions, now)
|
||||||
|
|
||||||
|
self.sessions({"s9": "Nine"})
|
||||||
|
stop = threading.Event()
|
||||||
|
names = self.httpd.hub.receipt_names
|
||||||
|
with mock.patch.object(receipts, "session_directory", flaky), mock.patch.object(hub, "SIGHTING", 0.01), contextlib.redirect_stderr(io.StringIO()) as logged:
|
||||||
|
thread = hub.sighting(self.httpd.hub, stop)
|
||||||
|
self.settle(lambda: any(entry["session"] == "s9" for entry in names.listed()))
|
||||||
|
alive = thread.is_alive()
|
||||||
|
stop.set()
|
||||||
|
thread.join(5)
|
||||||
|
self.assertEqual((alive, len(calls) >= 4, thread.is_alive()), (True, True, False))
|
||||||
|
self.assertEqual(logged.getvalue().splitlines(), [f"receipts: sighting pass failed: {kind}('broken\\npass')" for kind in ("ValueError", "TypeError", "OSError")])
|
||||||
|
with self.assertRaises(SystemExit):
|
||||||
|
receipts.sight(mock.Mock(side_effect=SystemExit(3)), self.httpd.hub.receipt_reads, threading.Event(), 0.01)
|
||||||
|
|
||||||
|
def test_a_failed_pass_counts_as_one_that_did_not_look(self):
|
||||||
|
reads, stop, calls = self.httpd.hub.receipt_reads, threading.Event(), []
|
||||||
|
|
||||||
|
def step(now: float) -> None:
|
||||||
|
calls.append(now)
|
||||||
|
if len(calls) == 2:
|
||||||
|
raise OSError("council unreadable")
|
||||||
|
if len(calls) == 3:
|
||||||
|
stop.set()
|
||||||
|
reads.see({("s2", T + n) for n in range(len(calls))}, NOW + 15 * len(calls), T, True)
|
||||||
|
|
||||||
|
with contextlib.redirect_stderr(io.StringIO()) as logged:
|
||||||
|
receipts.sight(step, reads, stop, 0)
|
||||||
|
self.assertEqual(reads.listed(NOW + 99), {("s2", T): None, ("s2", T + 1): None, ("s2", T + 2): None})
|
||||||
|
self.assertEqual(logged.getvalue().splitlines(), ["receipts: sighting pass failed: OSError('council unreadable')"])
|
||||||
|
|
||||||
|
def test_a_failing_receipt_store_write_leaves_the_state_and_the_stream_without_readers(self):
|
||||||
|
self.fixture()
|
||||||
|
with mock.patch.object(receipts.ReceiptNames, "save", side_effect=OSError(28, "No space left on device")):
|
||||||
|
state = self.snapshot()
|
||||||
|
radio = [entry for entry in self.events("view=funk")["events"] if entry["kind"] == "radio"]
|
||||||
|
self.assertEqual([(row["class"], row["readers"], row["acks"]) for row in state["radio"]], [("post", [], []), ("call", [], []), ("broadcast", [], [])])
|
||||||
|
self.assertEqual([(entry["ref"]["readers"], entry["ref"]["acks"]) for entry in radio], [([], [])] * 3)
|
||||||
|
|
||||||
|
def test_a_reader_shows_about_the_first_sighting_that_found_its_mark_and_none_for_rows_read_before_the_hub_looked(self):
|
||||||
|
self.fixture()
|
||||||
|
t, hub_ = self.t, self.httpd.hub
|
||||||
|
|
||||||
|
def sight(reads: receipts.ReceiptReads, now: float) -> None:
|
||||||
|
receipts.sighting_pass(hub_.scripts.skills, hub_.council, hub_.receipt_names, reads, hub_.agent_sessions, RADIO_WINDOW, now)
|
||||||
|
|
||||||
|
first = time.time() - 30
|
||||||
|
self.sessions({"s1": "One", "s2": "Two", "s3": "Three"}, first - 1)
|
||||||
|
sight(hub_.receipt_reads, first)
|
||||||
|
self.write_council({"news.jsonl": self.jsonl([*records.read_jsonl(self.council / council.NEWS), message("post", t + 20, ["Three"])])})
|
||||||
|
self.marks({"s2": {"radio": t + 10, "mail": t + 10}, "s3": {"radio": t + 10, "mail": t + 20}})
|
||||||
|
sight(hub_.receipt_reads, first + 5)
|
||||||
|
sight(hub_.receipt_reads, first + 10)
|
||||||
|
served = {row["ts"]: [(entry["session"], entry["read"], entry["approximate"]) for entry in row["readers"]] for row in self.snapshot()["radio"]}
|
||||||
|
self.assertEqual(served, {
|
||||||
|
t + 1: [("s2", None, False)],
|
||||||
|
t + 2: [("s2", t + 4, False), ("s3", t + 5, False)],
|
||||||
|
t + 3: [("s3", None, False), ("s2", None, False)],
|
||||||
|
t + 20: [("s3", first + 5, True)],
|
||||||
|
})
|
||||||
|
restarted = receipts.ReceiptReads(self.state / receipts.READS)
|
||||||
|
self.write_council({"news.jsonl": self.jsonl([*records.read_jsonl(self.council / council.NEWS), message("post", t + 30, ["Three"])])})
|
||||||
|
self.marks({"s2": {"radio": t + 10, "mail": t + 10}, "s3": {"radio": t + 10, "mail": t + 30}})
|
||||||
|
sight(restarted, first + 15)
|
||||||
|
self.httpd.hub = dataclasses.replace(hub_, receipt_reads=restarted)
|
||||||
|
served = {row["ts"]: [(entry["session"], entry["read"]) for entry in row["readers"]] for row in self.snapshot()["radio"]}
|
||||||
|
self.assertEqual((served[t + 20], served[t + 30]), ([("s3", first + 5)], [("s3", None)]))
|
||||||
|
|
||||||
|
def ledger(self, created: float, to: str = "Two") -> dict:
|
||||||
|
return self.httpd.hub.messages.add({"to": to, "address": to, "text": "ping me", "upload": None, "voice": None, "voice_sha256": "", "class": "call", "created_ts": created})
|
||||||
|
|
||||||
|
def relay(self, record: dict, ts: float, **changes: object) -> dict:
|
||||||
|
return {"ts": ts, "from": "Operator", "to": [record["address"]], "text": f"[kvb-orchestrator msg {record['id']}] ping me", "class": "call", "v": 2, **changes}
|
||||||
|
|
||||||
|
def sent(self) -> dict[str, tuple]:
|
||||||
|
return {row["id"]: ([reader["session"] for reader in row["readers"]], row["acks"]) for row in self.snapshot()["messages"]}
|
||||||
|
|
||||||
|
def append(self, rows: list[dict]) -> None:
|
||||||
|
self.council.mkdir(parents=True, exist_ok=True)
|
||||||
|
with (self.council / council.NEWS).open("a", encoding="utf-8") as handle:
|
||||||
|
handle.write(self.jsonl(rows))
|
||||||
|
|
||||||
|
def radio_runs(self, stamps: list[float], extra: Callable[[dict], list[dict]] = lambda row: [], code: int = 0, written: bool = True) -> contextlib.AbstractContextManager:
|
||||||
|
def run(script: str, args: list[str]) -> dict:
|
||||||
|
cls = args[1] if args[0] == "send" else ("post" if "--to" in args else "newsletter")
|
||||||
|
stdout = ""
|
||||||
|
if stamps:
|
||||||
|
row = {"ts": stamps.pop(0), "from": "Operator", "to": [args[args.index("--to") + 1]] if "--to" in args else [], "text": args[-1], "class": cls, "v": 2}
|
||||||
|
self.append([row, *extra(row)] if written else extra(row))
|
||||||
|
stdout = f"ts: {row['ts']!r}\nsent {cls}\n"
|
||||||
|
return {"command": script, "code": code, "stdout": stdout, "stderr": "killed"}
|
||||||
|
|
||||||
|
return mock.patch.object(self.scripts, "run", run)
|
||||||
|
|
||||||
|
def test_a_run_that_fails_after_storing_keeps_the_ts_it_printed_and_one_that_printed_none_keeps_nothing(self):
|
||||||
|
t = self.t
|
||||||
|
self.sessions({"s2": "Two"})
|
||||||
|
written, timed_out, refused, forged, missing = (self.ledger(t + 10 * n) for n in range(1, 6))
|
||||||
|
with self.radio_runs([t + 10.5], code=1):
|
||||||
|
self.httpd.hub.radioed(written, ["send", "call"])
|
||||||
|
with self.radio_runs([t + 20.5], code=-1):
|
||||||
|
self.httpd.hub.radioed(timed_out, ["send", "call"])
|
||||||
|
with self.radio_runs([], code=1):
|
||||||
|
self.httpd.hub.radioed(refused, ["send", "call"])
|
||||||
|
|
||||||
|
def held_lock(script: str, args: list[str]) -> dict:
|
||||||
|
self.append([self.relay(forged, t + 40.5), receipt("Two", t + 40.6, t + 40.5)])
|
||||||
|
return {"command": script, "code": -1, "stdout": "", "stderr": "timed out after 60 seconds"}
|
||||||
|
|
||||||
|
with mock.patch.object(self.scripts, "run", held_lock):
|
||||||
|
self.httpd.hub.radioed(forged, ["send", "call"])
|
||||||
|
with self.radio_runs([t + 50.5], written=False):
|
||||||
|
self.httpd.hub.radioed(missing, ["send", "call"])
|
||||||
|
self.append([receipt("Two", t + 11, t + 10.5)])
|
||||||
|
self.marks({"s2": {"radio": t + 60}})
|
||||||
|
records = (written, timed_out, refused, forged, missing)
|
||||||
|
self.assertEqual([self.httpd.hub.messages.get(record["id"]).get("relay_ts") for record in records], [t + 10.5, t + 20.5, None, None, None])
|
||||||
|
self.assertEqual(self.sent(), {
|
||||||
|
written["id"]: (["s2"], ["s2"]), timed_out["id"]: (["s2"], []), refused["id"]: ([], []), forged["id"]: ([], []), missing["id"]: ([], []),
|
||||||
|
})
|
||||||
|
|
||||||
|
def test_the_printed_ts_counts_only_as_the_whole_first_line_with_a_valid_epoch(self):
|
||||||
|
self.assertEqual(
|
||||||
|
[messaging.stored_ts(text) for text in (
|
||||||
|
"ts: 1800000000.25\nsent call to Two\nts: 1800000000.75\n", "ts: 1800000000.25", "sent call to Two\nts: 1800000000.75\n",
|
||||||
|
"...\nts: 1800000000.75\n", "ts: 2e11\n", " ts: 1800000000.25\n", "ts: 1800000000.25 \n", "ts: nan\n", "ts: 1e999\n",
|
||||||
|
)],
|
||||||
|
[1800000000.25, 1800000000.25, None, None, None, None, None, None, None],
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_a_row_appended_with_the_relay_rows_ts_leaves_that_ts_without_readers_or_acks(self):
|
||||||
|
t = self.t
|
||||||
|
self.sessions({"s2": "Two", "s9": "Nine"})
|
||||||
|
record = self.ledger(t + 10)
|
||||||
|
with self.radio_runs([t + 10.5]):
|
||||||
|
self.httpd.hub.radioed(record, ["send", "call"])
|
||||||
|
self.append([receipt("Two", t + 11, t + 10.5)])
|
||||||
|
self.marks({"s2": {"radio": t + 50, "mail": t + 50}, "s9": {"radio": t + 50, "mail": t + 50}})
|
||||||
|
self.assertEqual(self.sent(), {record["id"]: (["s2"], ["s2"])})
|
||||||
|
self.append([message("broadcast", t + 10.5, [], "Nine"), receipt("Nine", t + 12, t + 10.5)])
|
||||||
|
self.assertEqual(self.sent(), {record["id"]: ([], [])})
|
||||||
|
self.assertEqual([(row["readers"], row["acks"]) for row in self.snapshot()["radio"] if row["ts"] == t + 10.5], [([], [])] * 2)
|
||||||
|
|
||||||
|
def test_a_broadcast_waking_a_hundred_sessions_keeps_the_ts_radio_py_printed_first(self):
|
||||||
|
tmp = tempfile.TemporaryDirectory()
|
||||||
|
self.addCleanup(tmp.cleanup)
|
||||||
|
home = Path(tmp.name)
|
||||||
|
sessions = home / ".claude" / "sessions"
|
||||||
|
sessions.mkdir(parents=True)
|
||||||
|
for n in range(100):
|
||||||
|
(sessions / f"s{n}.json").write_text(json.dumps({"sessionId": f"s{n}", "name": f"Agent {n:03d} on the long wake list"}), encoding="utf-8")
|
||||||
|
record = self.ledger(self.t + 10, "*")
|
||||||
|
with mock.patch.object(self.scripts, "run", skill_scripts.Scripts(REPO_ROOT / "skills", self.council, home).run):
|
||||||
|
run = self.httpd.hub.radioed(record, ["send", "broadcast"])
|
||||||
|
lines = run["stdout"].splitlines()
|
||||||
|
self.assertEqual(self.httpd.hub.messages.get(record["id"]).get("relay_ts"), records.read_jsonl(self.council / council.NEWS)[-1]["ts"], run["stderr"])
|
||||||
|
self.assertEqual(
|
||||||
|
(run["code"], lines[1], lines[-1].startswith("wake: SendMessage to Agent "), len(run["stdout"]) <= skill_scripts.OUTPUT_LIMIT + skill_scripts.FIRST_LINE_LIMIT + 8),
|
||||||
|
(0, skill_scripts.ELIDED, True, True),
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_a_message_shows_the_readers_and_acks_of_exactly_the_row_its_relay_run_wrote(self):
|
||||||
|
t = self.t
|
||||||
|
self.sessions({"s2": "Two", "s9": "Nine"})
|
||||||
|
record = self.ledger(t + 10)
|
||||||
|
self.write_council({"news.jsonl": self.jsonl([self.relay(record, t + 10.1), receipt("Two", t + 10.15, t + 10.1)])})
|
||||||
|
with self.radio_runs([t + 10.5]):
|
||||||
|
self.httpd.hub.radioed(record, ["send", "call"])
|
||||||
|
self.assertEqual(self.httpd.hub.messages.get(record["id"])["relay_ts"], t + 10.5)
|
||||||
|
self.append([
|
||||||
|
self.relay(record, t + 10.3), receipt("Two", t + 10.35, t + 10.3),
|
||||||
|
self.relay(record, t + 10.4, to_sessions=["s9"]), receipt("Nine", t + 10.45, t + 10.4),
|
||||||
|
self.relay(record, t + 11), receipt("Two", t + 11.5, t + 11),
|
||||||
|
])
|
||||||
|
self.marks({"s2": {"radio": t + 10.2}, "s9": {"radio": t + 50}})
|
||||||
|
self.assertEqual(self.sent(), {record["id"]: ([], [])})
|
||||||
|
self.append([receipt("Two", t + 12, t + 10.5)])
|
||||||
|
self.marks({"s2": {"radio": t + 50}, "s9": {"radio": t + 50}})
|
||||||
|
self.assertEqual(self.sent(), {record["id"]: (["s2"], ["s2"])})
|
||||||
|
|
||||||
|
def test_a_copy_of_the_relay_row_landing_with_it_lends_the_message_nothing(self):
|
||||||
|
t = self.t
|
||||||
|
self.sessions({"s2": "Two"})
|
||||||
|
record = self.ledger(t + 10)
|
||||||
|
with self.radio_runs([t + 10.5], lambda row: [{**row, "ts": t + 10.4}, receipt("Two", t + 10.7, t + 10.4)]):
|
||||||
|
self.httpd.hub.radioed(record, ["send", "call"])
|
||||||
|
self.marks({"s2": {"radio": t + 50}})
|
||||||
|
self.assertEqual(self.httpd.hub.messages.get(record["id"])["relay_ts"], t + 10.5)
|
||||||
|
self.assertEqual(self.sent(), {record["id"]: (["s2"], [])})
|
||||||
|
|
||||||
|
def test_every_one_of_forty_choice_requests_keeps_its_own_relay_row_however_late_it_lands(self):
|
||||||
|
signer = self.pin()
|
||||||
|
payload = {"class": "post", "to": [f"Agent {n}" for n in range(choosing.RECIPIENT_LIMIT)]}
|
||||||
|
status, _, body = self.post_json("/api/choices/sign", payload, self.guard(self.host))
|
||||||
|
self.assertEqual(status, 200, body)
|
||||||
|
proof = signer.proof(json.loads(body)["payload"], self.origin)
|
||||||
|
base = time.time() - 300
|
||||||
|
stamps = [base + 1.6 * (n + 1) for n in range(choosing.RECIPIENT_LIMIT)]
|
||||||
|
with mock.patch.object(messaging, "time", types.SimpleNamespace(time=lambda: base)), self.radio_runs(list(stamps)):
|
||||||
|
status, _, body = self.post_json("/api/choices", {**payload, "proof": proof}, self.guard(self.host))
|
||||||
|
self.assertEqual(status, 201, body)
|
||||||
|
ids = json.loads(body)["ids"]
|
||||||
|
self.assertEqual([self.httpd.hub.messages.get(record_id)["relay_ts"] for record_id in ids], stamps)
|
||||||
|
self.assertGreater(stamps[-1], base + skill_scripts.SCRIPT_TIMEOUT)
|
||||||
|
self.sessions({"s39": "Agent 39"})
|
||||||
|
self.append([receipt("Agent 39", base + 70, stamps[-1])])
|
||||||
|
self.marks({"s39": {"mail": base + 80}})
|
||||||
|
served = self.sent()
|
||||||
|
self.assertEqual((served[ids[-1]], served[ids[0]]), ((["s39"], ["s39"]), ([], [])))
|
||||||
|
|
||||||
|
def test_a_ledger_record_from_before_voice_notes_serves_without_readers_and_relays_its_text(self):
|
||||||
|
t = self.t
|
||||||
|
self.fixture()
|
||||||
|
old = self.httpd.hub.messages.add({"to": "Two", "address": "Two", "text": "hi there", "upload": None, "created_ts": t + 10})
|
||||||
|
self.append([self.relay(old, t + 10.1, text=f"[kvb-orchestrator msg {old['id']}] hi there"), receipt("Two", t + 10.2, t + 10.1)])
|
||||||
|
status, _, body = self.get("/api/state")
|
||||||
|
self.assertEqual(status, 200, body)
|
||||||
|
served = [row for row in json.loads(body)["messages"] if row["id"] == old["id"]]
|
||||||
|
self.assertEqual([(row["readers"], row["acks"], row["voice"], row["class"]) for row in served], [([], [], None, "")])
|
||||||
|
self.assertEqual(self.httpd.hub.relay_text(old), f"[kvb-orchestrator msg {old['id']}] hi there")
|
||||||
|
|
||||||
|
def test_a_session_born_while_the_snapshot_was_stale_reads_at_no_time_and_later_rows_at_their_sighting(self):
|
||||||
|
t, hub_ = self.t, self.httpd.hub
|
||||||
|
first = time.time() - 60
|
||||||
|
|
||||||
|
def sight(now: float) -> None:
|
||||||
|
receipts.sighting_pass(hub_.scripts.skills, hub_.council, hub_.receipt_names, hub_.receipt_reads, hub_.agent_sessions, RADIO_WINDOW, now)
|
||||||
|
|
||||||
|
self.write_council({"news.jsonl": self.jsonl([message("call", t + 20, ["Seven"])])})
|
||||||
|
self.marks({"s7": {"radio": t + 20}})
|
||||||
|
self.sessions({"s7": "Seven"}, first - SNAPSHOT_STALE - 1)
|
||||||
|
sight(first)
|
||||||
|
self.sessions({"s7": "Seven"}, first + 14)
|
||||||
|
sight(first + 15)
|
||||||
|
self.append([message("call", t + 40, ["Seven"])])
|
||||||
|
self.marks({"s7": {"radio": t + 40}})
|
||||||
|
sight(first + 30)
|
||||||
|
served = {row["ts"]: [(entry["session"], entry["read"]) for entry in row["readers"]] for row in self.snapshot()["radio"]}
|
||||||
|
self.assertEqual(served, {t + 20: [("s7", None)], t + 40: [("s7", first + 30)]})
|
||||||
|
|
||||||
|
def test_without_an_installed_radio_every_row_has_no_reader(self):
|
||||||
|
self.fixture()
|
||||||
|
with mock.patch.object(receipts, "radio_module", return_value=None):
|
||||||
|
state = self.snapshot()
|
||||||
|
self.assertEqual({row["ts"]: (row["readers"], row["acks"]) for row in state["radio"]}, {self.t + n: ([], []) for n in (1, 2, 3)})
|
||||||
|
|
||||||
|
|
||||||
|
def page(payload: dict) -> dict:
|
||||||
|
payload = {"agent": {"key": "none", "state": {}}, **payload}
|
||||||
|
command = ["node", "-e", PAGE_PROBE, str(TEMPLATES / "receipts.js"), str(TEMPLATES / "radioset.js"), str(TEMPLATES), json.dumps(payload)]
|
||||||
|
done = subprocess.run(command, capture_output=True, text=True, timeout=60, check=True)
|
||||||
|
return json.loads(done.stdout)
|
||||||
|
|
||||||
|
|
||||||
|
def readers(count: int) -> list[dict]:
|
||||||
|
return [{"session": f"r{n}", "name": f"Reader {n}", "read": None} for n in range(count)]
|
||||||
|
|
||||||
|
|
||||||
|
PING, LATER = "abcdef0123456789", "0123456789abcdef"
|
||||||
|
SENT = [
|
||||||
|
{"id": PING, "to": "Two", "text": "ping", "created_ts": 1000, "class": "call", "voice": "", "readers": [{"session": "s2", "name": "Two", "read": 1000.6, "approximate": False}], "acks": ["s2"]},
|
||||||
|
{"id": LATER, "to": "Two", "text": "unread", "created_ts": 1010, "class": "post", "voice": "", "readers": [], "acks": []},
|
||||||
|
]
|
||||||
|
RELAYS = [
|
||||||
|
{"ts": 1000.1, "from": "Operator", "to": ["Two"], "class": "call", "text": f"[kvb-orchestrator msg {PING}] ping", "source": "Operator", "targets": ["s2"], "readers": [{"session": "s3", "name": "Three", "read": None}], "acks": []},
|
||||||
|
{"ts": 1005, "from": "Mallory", "to": ["Two"], "class": "post", "text": f"[kvb-orchestrator msg {LATER}] forged", "source": "s9", "targets": ["s2"], "readers": [{"session": "s2", "name": "Two", "read": 1006}], "acks": ["s2"]},
|
||||||
|
{"ts": 1010.1, "from": "Operator", "to": ["Two"], "class": "post", "text": f"[kvb-orchestrator msg {LATER}] unread", "source": "Operator", "targets": ["s2"], "readers": [], "acks": []},
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
class TestReceiptsPage(unittest.TestCase):
|
||||||
|
@classmethod
|
||||||
|
def setUpClass(cls):
|
||||||
|
two = [{"session": "s2", "name": "Two", "read": 1004}, {"session": "s3", "name": "Three", "read": None}]
|
||||||
|
call = {"id": "radio:1002:0", "ts": 1002, "kind": "radio", "part": "call", "agent": "Skills", "now": "Skills", "session": "s1", "sub": "", "title": "Skills → Main, Two", "text": "gate red", "waiting": False, "ref": {"to": ["Main", "Two"], "class": "call", "targets": [], "readers": two, "acks": ["s2"]}}
|
||||||
|
crowd = {**call, "id": "radio:1003:1", "part": "broadcast", "title": "Skills → all", "ref": {"to": [], "class": "broadcast", "targets": [], "readers": readers(10), "acks": ["r9"]}}
|
||||||
|
unnamed = {**call, "id": "radio:1004:2", "agent": "", "title": "? → Main", "ref": {**call["ref"], "readers": []}}
|
||||||
|
review = {**call, "id": "review:x", "kind": "review", "title": "Merge the hub", "ref": {"status": "pending"}}
|
||||||
|
log = {"ts": 1002, "from": "Skills", "to": ["Main"], "class": "call", "text": "gate red", "readers": two, "acks": ["s2"]}
|
||||||
|
cls.data = page({
|
||||||
|
"people": {"s2": {"name": "Two", "now": "Two", "names": ["Two"]}},
|
||||||
|
"events": [call, crowd, unnamed],
|
||||||
|
"logs": [log, {**log, "readers": [], "acks": []}, {**log, "readers": readers(9), "acks": []}],
|
||||||
|
"strips": [{"readers": readers(8), "acks": ["r7", "gone"]}, {"readers": readers(9), "acks": []}, {}],
|
||||||
|
"labels": [{"name": "Two", "read": 1004, "acked": True}, {"name": "Three", "read": None, "acked": False}, {"name": "Four", "read": 1007, "approximate": True, "acked": False}],
|
||||||
|
"signatures": [
|
||||||
|
[{"readers": two, "acks": []}], [{"readers": two, "acks": ["s2"]}], [{"readers": two[:1], "acks": []}], [{}],
|
||||||
|
[{"readers": [{**two[0], "read": 1005}], "acks": []}], [{"readers": [{**two[0], "approximate": True}], "acks": []}],
|
||||||
|
],
|
||||||
|
"agent": {"key": "s2", "state": {"messages": SENT, "radio": RELAYS}},
|
||||||
|
})
|
||||||
|
cls.review = page({"people": {}, "events": [review], "logs": [], "strips": [], "labels": [], "signatures": []})
|
||||||
|
|
||||||
|
def test_the_stream_header_names_the_sender_once_and_the_route_its_recipients(self):
|
||||||
|
call, crowd, unnamed = self.data["events"]
|
||||||
|
self.assertEqual(call["route"], "Skills→ Main, Two")
|
||||||
|
self.assertEqual(call["route"].count("Skills"), 1)
|
||||||
|
self.assertEqual(crowd["route"], "Skills→ all")
|
||||||
|
self.assertEqual(unnamed["route"], "s1? → Main")
|
||||||
|
self.assertEqual(self.data["module"]["headlines"], ["→ Main, Two", "→ all", "? → Main"])
|
||||||
|
self.assertEqual(self.review["events"][0]["route"], "SkillsMerge the hub")
|
||||||
|
|
||||||
|
def test_under_each_message_one_avatar_per_reader_with_the_badge_where_a_receipt_exists(self):
|
||||||
|
call = self.data["events"][0]
|
||||||
|
self.assertEqual([(reader["session"], reader["acked"], reader["badges"]) for reader in call["readers"]], [("s2", "true", 1), ("s3", "false", 0)])
|
||||||
|
self.assertEqual([reader["title"] for reader in call["readers"]], ["Two · read at 1004 · acknowledged", "Three · read"])
|
||||||
|
self.assertEqual([reader["label"] for reader in call["readers"]], [reader["title"] for reader in call["readers"]])
|
||||||
|
self.assertEqual([reader["face"] for reader in call["readers"]], [["s2"], ["Three"]])
|
||||||
|
self.assertEqual((call["groups"], call["more"]), (["Read by 2"], []))
|
||||||
|
log, silent, _ = self.data["logs"]
|
||||||
|
self.assertEqual([(reader["session"], reader["badges"]) for reader in log["readers"]], [("s2", 1), ("s3", 0)])
|
||||||
|
self.assertEqual(log["routes"], ["Skills → Main"])
|
||||||
|
self.assertEqual((silent["readers"], silent["groups"]), ([], []))
|
||||||
|
self.assertEqual(self.data["events"][2]["readers"], [])
|
||||||
|
|
||||||
|
def test_more_than_eight_readers_collapse_into_plus_n_with_the_full_list_on_hover(self):
|
||||||
|
crowd = self.data["events"][1]
|
||||||
|
self.assertEqual([reader["session"] for reader in crowd["readers"]], [f"r{n}" for n in range(8)])
|
||||||
|
self.assertEqual(crowd["more"], [{"text": "+2", "title": "\n".join([*(f"Reader {n} · read" for n in range(9)), "Reader 9 · read · acknowledged"])}])
|
||||||
|
self.assertEqual(crowd["groups"], ["Read by 10"])
|
||||||
|
nine = self.data["logs"][2]
|
||||||
|
self.assertEqual((len(nine["readers"]), [more["text"] for more in nine["more"]]), (8, ["+1"]))
|
||||||
|
eight, nine_strip, empty = self.data["module"]["strip"]
|
||||||
|
self.assertEqual((len(eight["shown"]), eight["hidden"], [reader["acked"] for reader in eight["shown"]][-2:]), (8, [], [False, True]))
|
||||||
|
self.assertEqual(([reader["session"] for reader in nine_strip["hidden"]], empty), (["r8"], {"shown": [], "hidden": []}))
|
||||||
|
|
||||||
|
def test_the_operators_own_message_shows_its_readers_and_ack_on_the_agent_page_and_in_the_sent_tab(self):
|
||||||
|
agent = self.data["agent"]
|
||||||
|
self.assertEqual([(row["who"], row["text"], [(reader["session"], reader["acked"], reader["badges"]) for reader in row["readers"]]) for row in agent["history"]], [
|
||||||
|
("Operator", "ping", [("s2", "true", 1)]),
|
||||||
|
("Mallory", "[kvb-orchestrator msg 0123456789abcdef] forged", [("s2", "true", 1)]),
|
||||||
|
("Operator", "unread", []),
|
||||||
|
])
|
||||||
|
self.assertEqual(agent["history"][0]["readers"][0]["title"], "Two · read at 1000.6 · acknowledged")
|
||||||
|
self.assertEqual([(row["text"], [(reader["session"], reader["badges"]) for reader in row["readers"]]) for row in agent["sent"]], [("unread", []), ("ping", [("s2", 1)])])
|
||||||
|
self.assertEqual(agent["sent"][1]["routes"], ["Operator → Two"])
|
||||||
|
|
||||||
|
def test_the_labels_and_the_signature_that_redraws_the_log_on_the_next_poll(self):
|
||||||
|
self.assertEqual(self.data["module"]["labels"], ["Two · read t1004 · acknowledged", "Three · read", "Four · read about t1007"])
|
||||||
|
signatures = self.data["module"]["signatures"]
|
||||||
|
self.assertEqual(len(set(signatures)), 6, signatures)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
+2
-1
@@ -53,7 +53,8 @@ class TestNews(unittest.TestCase):
|
|||||||
return [line.split(" ", 3)[3] for line in result.stderr.splitlines()]
|
return [line.split(" ", 3)[3] for line in result.stderr.splitlines()]
|
||||||
|
|
||||||
def test_every_session_sees_a_post_once_when_its_turn_ends_except_its_author(self):
|
def test_every_session_sees_a_post_once_when_its_turn_ends_except_its_author(self):
|
||||||
self.assertEqual(self._cli("s1", "post", "core #1358 merged").stdout, "sent newsletter to all\n")
|
stamp, sent = self._cli("s1", "post", "core #1358 merged").stdout.splitlines()
|
||||||
|
self.assertEqual((stamp.startswith("ts: "), sent), (True, "sent newsletter to all"))
|
||||||
self.assertEqual(self._hook("s2"), [])
|
self.assertEqual(self._hook("s2"), [])
|
||||||
self.assertEqual(self._mailbox("s2"), ["One: core #1358 merged"])
|
self.assertEqual(self._mailbox("s2"), ["One: core #1358 merged"])
|
||||||
self.assertEqual(self._mailbox("s2"), [])
|
self.assertEqual(self._mailbox("s2"), [])
|
||||||
|
|||||||
+31
-4
@@ -16,6 +16,7 @@ from pathlib import Path
|
|||||||
REPO_ROOT = Path(__file__).resolve().parents[1]
|
REPO_ROOT = Path(__file__).resolve().parents[1]
|
||||||
RADIO = REPO_ROOT / "skills" / "kvb-org-radio" / "scripts" / "radio.py"
|
RADIO = REPO_ROOT / "skills" / "kvb-org-radio" / "scripts" / "radio.py"
|
||||||
LINE = re.compile(r"^(?:RADIO|MAIL) (CALL|BROADCAST|SECURITY|PAN-PAN|MAYDAY|POST|NEWSLETTER) \d\d:\d\d (.*)$", re.DOTALL)
|
LINE = re.compile(r"^(?:RADIO|MAIL) (CALL|BROADCAST|SECURITY|PAN-PAN|MAYDAY|POST|NEWSLETTER) \d\d:\d\d (.*)$", re.DOTALL)
|
||||||
|
STORED = "ts: "
|
||||||
LISTENER = (
|
LISTENER = (
|
||||||
"import importlib.util, sys\n"
|
"import importlib.util, sys\n"
|
||||||
"spec = importlib.util.spec_from_file_location('radio', sys.argv[1])\n"
|
"spec = importlib.util.spec_from_file_location('radio', sys.argv[1])\n"
|
||||||
@@ -32,6 +33,10 @@ def strip(line: str) -> str:
|
|||||||
return f"{match[1]} {match[2]}" if match else line
|
return f"{match[1]} {match[2]}" if match else line
|
||||||
|
|
||||||
|
|
||||||
|
def said(stdout: str) -> list[str]:
|
||||||
|
return [line for line in stdout.splitlines() if not line.startswith(STORED)]
|
||||||
|
|
||||||
|
|
||||||
class TestRadio(unittest.TestCase):
|
class TestRadio(unittest.TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
tmp = tempfile.TemporaryDirectory()
|
tmp = tempfile.TemporaryDirectory()
|
||||||
@@ -67,7 +72,7 @@ class TestRadio(unittest.TestCase):
|
|||||||
def _send(self, sid: str, *args: str) -> list[str]:
|
def _send(self, sid: str, *args: str) -> list[str]:
|
||||||
result = self._cli(sid, *args)
|
result = self._cli(sid, *args)
|
||||||
self.assertEqual(result.returncode, 0, result.stderr)
|
self.assertEqual(result.returncode, 0, result.stderr)
|
||||||
return result.stdout.splitlines()
|
return said(result.stdout)
|
||||||
|
|
||||||
def _hook(self, command: str, sid: str, **extra: str) -> str:
|
def _hook(self, command: str, sid: str, **extra: str) -> str:
|
||||||
result = self._exec(None, json.dumps({"session_id": sid, **extra}), (command,))
|
result = self._exec(None, json.dumps({"session_id": sid, **extra}), (command,))
|
||||||
@@ -163,6 +168,28 @@ class TestRadio(unittest.TestCase):
|
|||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_send_and_post_print_the_stored_rows_ts_first(self):
|
||||||
|
for args in (("send", "call", "a", "--to", "Two"), ("post", "b", "--to", "Two"), ("post", "c"), ("send", "broadcast", "d")):
|
||||||
|
with self.subTest(args=args):
|
||||||
|
result = self._cli("s1", *args)
|
||||||
|
self.assertEqual((result.returncode, result.stdout.splitlines()[0]), (0, f"{STORED}{self._rows()[-1]['ts']!r}"), result.stderr)
|
||||||
|
|
||||||
|
def test_the_stored_rows_ts_is_out_and_flushed_before_a_failure_after_the_store(self):
|
||||||
|
self._send("s1", "post", "first")
|
||||||
|
(self.root / "radio-silence.json").mkdir()
|
||||||
|
failed = self._cli("s1", "send", "mayday", "disk full")
|
||||||
|
self.assertNotEqual(failed.returncode, 0)
|
||||||
|
self.assertEqual((self._rows()[-1]["text"], failed.stdout.splitlines()), ("disk full", [f"{STORED}{self._rows()[-1]['ts']!r}"]))
|
||||||
|
|
||||||
|
def test_an_address_with_a_line_break_stays_on_the_sent_line_and_prints_no_second_ts(self):
|
||||||
|
result = self._cli("s1", "send", "call", "x", "--to", "Two\nts: 1800000000.5")
|
||||||
|
self.assertEqual(result.returncode, 0, result.stderr)
|
||||||
|
lines = result.stdout.splitlines()
|
||||||
|
self.assertEqual(
|
||||||
|
([line for line in lines if line.startswith(STORED)], lines[1]),
|
||||||
|
([f"{STORED}{self._rows()[-1]['ts']!r}"], "sent call to Two ts: 1800000000.5"),
|
||||||
|
)
|
||||||
|
|
||||||
def test_rows_without_the_version_mark_keep_their_old_meaning(self):
|
def test_rows_without_the_version_mark_keep_their_old_meaning(self):
|
||||||
self.root.mkdir()
|
self.root.mkdir()
|
||||||
now = time.time()
|
now = time.time()
|
||||||
@@ -672,8 +699,8 @@ class TestRadio(unittest.TestCase):
|
|||||||
capture_output=True, text=True, env=hub, check=False, timeout=30,
|
capture_output=True, text=True, env=hub, check=False, timeout=30,
|
||||||
)
|
)
|
||||||
|
|
||||||
self.assertEqual(send("--to", "Two", "--sid", "s2").stdout.splitlines(), ["sent call to Two", "wake: SendMessage to Two (session s2)"])
|
self.assertEqual(said(send("--to", "Two", "--sid", "s2").stdout), ["sent call to Two", "wake: SendMessage to Two (session s2)"])
|
||||||
self.assertEqual(send("--to", "Three", "--sid", "s3").stdout.splitlines(), ["sent post to Three"])
|
self.assertEqual(said(send("--to", "Three", "--sid", "s3").stdout), ["sent post to Three"])
|
||||||
self.assertEqual(send("--to", "Two", "--to", "Three", "--sid", "s2").returncode, 2)
|
self.assertEqual(send("--to", "Two", "--to", "Three", "--sid", "s2").returncode, 2)
|
||||||
self.assertEqual([(row["class"], row["to"], row["to_sessions"]) for row in self._rows()], [("call", ["Two"], ["s2"]), ("post", ["Three"], ["s3"])])
|
self.assertEqual([(row["class"], row["to"], row["to_sessions"]) for row in self._rows()], [("call", ["Two"], ["s2"]), ("post", ["Three"], ["s3"])])
|
||||||
sessions = self.home / ".claude" / "sessions"
|
sessions = self.home / ".claude" / "sessions"
|
||||||
@@ -748,7 +775,7 @@ class TestRadio(unittest.TestCase):
|
|||||||
[sys.executable, str(RADIO), "feedback", "send", "ran x", "--to", "Two", "--sid", "s2", "--session", "Operator"],
|
[sys.executable, str(RADIO), "feedback", "send", "ran x", "--to", "Two", "--sid", "s2", "--session", "Operator"],
|
||||||
capture_output=True, text=True, env={**self._env(None), "HOME": str(hub_home)}, check=False, timeout=30,
|
capture_output=True, text=True, env={**self._env(None), "HOME": str(hub_home)}, check=False, timeout=30,
|
||||||
)
|
)
|
||||||
self.assertEqual(sent.stdout.splitlines(), ["sent call to Two"], sent.stderr)
|
self.assertEqual(said(sent.stdout), ["sent call to Two"], sent.stderr)
|
||||||
self.assertEqual(self._send("s1", "feedback", "send", "ran y", "--to", "Two", "--sid", "s9"), ["sent call to Two"])
|
self.assertEqual(self._send("s1", "feedback", "send", "ran y", "--to", "Two", "--sid", "s9"), ["sent call to Two"])
|
||||||
self.assertEqual(self._send("s1", "feedback", "send", "ran z", "--to", "Nine", "--sid", "s9"), ["sent call to Nine", "wake: SendMessage to Nine (session s9)"])
|
self.assertEqual(self._send("s1", "feedback", "send", "ran z", "--to", "Nine", "--sid", "s9"), ["sent call to Nine", "wake: SendMessage to Nine (session s9)"])
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user