Merge branch 'feature/radio-receipts-inline' into unify/skills-20261112-night
radio.py conflict resolved by the unify side plus the prepared resolution (seen() on top of the feedback-channel radio.py); receipts.py and its tests adapted to the unify helpers. Merged tree: test_hub_receipts 15, test_radio_seen 8, test_radio 45 passed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
38d475ebd9
6 files changed
+645
-22
No files matched your search
@@ -1,7 +1,5 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import functools
|
||||
import importlib.util
|
||||
import sqlite3
|
||||
import time
|
||||
from contextlib import closing
|
||||
@@ -12,6 +10,7 @@ from .errors import ToolError
|
||||
from .fields import decode, expect
|
||||
from .jobs import GATE_JOB
|
||||
from .signed import signing
|
||||
from .skill_scripts import script_module
|
||||
|
||||
GATES_SCRIPT = "kvb-check-gate/scripts/gates.py"
|
||||
GATES_DB = "gates.db"
|
||||
@@ -35,24 +34,7 @@ def gates_module(script: Path) -> ModuleType | None:
|
||||
loaded again after an install changed it, None when it is missing or
|
||||
does not load.
|
||||
"""
|
||||
try:
|
||||
changed = script.stat().st_mtime_ns
|
||||
except OSError:
|
||||
return None
|
||||
return loaded(script, changed)
|
||||
|
||||
|
||||
@functools.cache
|
||||
def loaded(script: Path, changed: int) -> ModuleType | None:
|
||||
spec = importlib.util.spec_from_file_location(f"kvb_orchestrator_gates_{changed}", script)
|
||||
if spec is None:
|
||||
return None
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
try:
|
||||
spec.loader.exec_module(module)
|
||||
except (OSError, ImportError, RuntimeError, SyntaxError):
|
||||
return None
|
||||
return module
|
||||
return script_module(script)
|
||||
|
||||
|
||||
def branch_gates(gates: ModuleType, db: sqlite3.Connection, repo: str, branch: str) -> dict:
|
||||
|
||||
@@ -0,0 +1,187 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import functools
|
||||
import inspect
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
from types import ModuleType
|
||||
|
||||
from .council import RECEIPT
|
||||
from .names import PSEUDO, Directory, Names
|
||||
from .records import epoch, read_regular
|
||||
from .skill_scripts import RADIO, script_module
|
||||
|
||||
SEEN = "radio-seen"
|
||||
PROMPT = "news-seen"
|
||||
SNAPSHOT_SOURCE = "session"
|
||||
PROMPT_BYTES = 64
|
||||
STATE_BYTES = 4096
|
||||
CACHED = 4096
|
||||
SEEN_PARAMETERS = ("row", "name", "sid", "prompt", "state", "since")
|
||||
|
||||
Marks = tuple[float | None, dict[str, float]]
|
||||
Stamp = tuple[int, int, int]
|
||||
|
||||
|
||||
def radio_module(skills: Path) -> ModuleType | None:
|
||||
"""Args:
|
||||
skills: the installed skills directory, the hub's --skills mount.
|
||||
|
||||
Returns:
|
||||
Its kvb-org-radio radio.py as a module, the same file the hooks run;
|
||||
None when it is missing, does not load or its seen() does not take
|
||||
SEEN_PARAMETERS, as after an install of other skills than the hub's.
|
||||
"""
|
||||
module = script_module(skills / RADIO)
|
||||
seen = getattr(module, "seen", None)
|
||||
try:
|
||||
parameters = tuple(inspect.signature(seen).parameters) if callable(seen) else ()
|
||||
except (TypeError, ValueError):
|
||||
return None
|
||||
return module if parameters == SEEN_PARAMETERS else None
|
||||
|
||||
|
||||
def moment(value: object, now: float) -> float | None:
|
||||
found = epoch(value)
|
||||
return found if found is not None and found <= now else None
|
||||
|
||||
|
||||
def stamp(path: Path) -> Stamp | None:
|
||||
try:
|
||||
found = os.lstat(path)
|
||||
except OSError:
|
||||
return None
|
||||
return found.st_ino, found.st_mtime_ns, found.st_size
|
||||
|
||||
|
||||
@functools.lru_cache(maxsize=CACHED)
|
||||
def parsed_prompt(path: Path, version: Stamp) -> float | None:
|
||||
try:
|
||||
return float(read_regular(path, PROMPT_BYTES))
|
||||
except (OSError, ValueError):
|
||||
return None
|
||||
|
||||
|
||||
@functools.lru_cache(maxsize=CACHED)
|
||||
def parsed_state(path: Path, version: Stamp) -> object:
|
||||
try:
|
||||
return json.loads(read_regular(path, STATE_BYTES))
|
||||
except (OSError, ValueError, RecursionError):
|
||||
return None
|
||||
|
||||
|
||||
def prompt_of(path: Path, now: float) -> float | None:
|
||||
version = stamp(path)
|
||||
return None if version is None else moment(parsed_prompt(path, version), now)
|
||||
|
||||
|
||||
def state_of(path: Path, now: float) -> dict[str, float]:
|
||||
version = stamp(path)
|
||||
data = None if version is None else parsed_state(path, version)
|
||||
if not isinstance(data, dict):
|
||||
return {}
|
||||
return {key: found for key, value in data.items() if (found := moment(value, now)) is not None}
|
||||
|
||||
|
||||
def read_marks(council: Path, now: float) -> dict[str, Marks]:
|
||||
"""Args:
|
||||
council: the council directory with radio-seen/<sid>.json and the
|
||||
older news-seen/<sid>.
|
||||
now: the moment to judge at.
|
||||
|
||||
Returns:
|
||||
Per session id with a mark file its prompt mark and its radio-seen
|
||||
marks; a missing, malformed, unreadable, non-regular, oversized or
|
||||
future-dated mark is left out, a prompt mark then None. A file is
|
||||
parsed again only after its inode, mtime or size changed.
|
||||
"""
|
||||
sids = {path.stem for path in (council / SEEN).glob("*.json")} | {path.name for path in (council / PROMPT).glob("*")}
|
||||
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:
|
||||
"""Args:
|
||||
store: the hub's own name file for receipts, apart from the claim and
|
||||
title history, which any agent can feed through a claim file.
|
||||
sessions: read_snapshot(), written on the host from Claude Code's
|
||||
sessions; each record's name is the one radio.py's name_of()
|
||||
reads, kept as is, so rows and receipts match it unnormalized.
|
||||
now: the moment of the sighting.
|
||||
|
||||
Returns:
|
||||
The store's history from snapshot sightings alone, the names of
|
||||
ended sessions included.
|
||||
"""
|
||||
store.observe([(sid, record["name"], None, SNAPSHOT_SOURCE) for sid, record in sessions.items() if isinstance(record.get("name"), str) and record["name"]], now)
|
||||
return Directory([entry for entry in store.listed() if entry["source"] == SNAPSHOT_SOURCE])
|
||||
|
||||
|
||||
def first_known(directory: Directory, sid: str, first: dict[str, float]) -> float:
|
||||
if sid not in first:
|
||||
first[sid] = directory.timeline(sid)[0]["since"]
|
||||
return first[sid]
|
||||
|
||||
|
||||
def owners(directory: Directory, sender: str, ts: float, sessions: set[str], first: dict[str, float]) -> list[str]:
|
||||
held = [sid for sid in sorted(directory.aliases(sender)) if not sid.startswith(PSEUDO) and directory.at(sid, ts) == sender]
|
||||
strict = [sid for sid in held if first_known(directory, sid, first) <= ts]
|
||||
return strict or held or ([sender] if sender in sessions else [])
|
||||
|
||||
|
||||
def acknowledged(radio: ModuleType, rows: list[dict], sessions: set[str], directory: Directory) -> dict[float, dict[str, float]]:
|
||||
found: dict[float, dict[str, float]] = {}
|
||||
first: dict[str, float] = {}
|
||||
for row in rows:
|
||||
call, sender, ts = epoch(row.get("call")), row.get("from"), epoch(row["ts"])
|
||||
if radio.kind(row) != RECEIPT or call is None or not isinstance(sender, str):
|
||||
continue
|
||||
for owner in owners(directory, sender, ts, sessions, first):
|
||||
found.setdefault(call, {}).setdefault(owner, ts)
|
||||
return found
|
||||
|
||||
|
||||
def reader(radio: ModuleType, row: dict, sid: str, name: str, marks: Marks, acked: dict[str, float]) -> dict | None:
|
||||
prompt, state = marks
|
||||
if not radio.seen(row, name, sid, radio.NEVER if prompt is None else prompt, state, radio.NEVER):
|
||||
return None
|
||||
return {"session": sid, "name": name, "read": acked.get(sid)}
|
||||
|
||||
|
||||
def order(entry: dict) -> tuple:
|
||||
return entry["read"] is None, entry["read"] or 0.0, entry["name"], entry["session"]
|
||||
|
||||
|
||||
def readers_and_acks(radio: ModuleType, news: list[dict], window: tuple[float, float], marks: dict[str, Marks], directory: Directory) -> dict[float, dict]:
|
||||
"""Args:
|
||||
radio: radio_module(); its seen() decides whether a session read a
|
||||
row, with the marks alone and no delivery window.
|
||||
news: every news.jsonl row; a receipt counts for its call even when
|
||||
it lies outside the window.
|
||||
window: (begin, end); each row inside it but a receipt gets an entry.
|
||||
marks: read_marks() of the sessions.
|
||||
directory: session_directory(); a session goes by the name it held
|
||||
at the row's time, after it ended too, and by its id without one,
|
||||
as radio.py's name_of(); a receipt belongs to the
|
||||
sessions known to hold its sender's name by its time, else to
|
||||
those whose first known name it is.
|
||||
|
||||
Returns:
|
||||
Per row ts {"readers": [{"session", "name", "read"}], "acks": [session
|
||||
ids]}: acks the sessions with a receipt whose call is that ts; read
|
||||
the first such receipt's ts, None for a reader without one, as the
|
||||
seen marks keep no delivery time.
|
||||
"""
|
||||
begin, end = window
|
||||
rows = [row for row in news if epoch(row.get("ts")) is not None]
|
||||
acks = acknowledged(radio, rows, set(marks), directory)
|
||||
reach = {sid: max(radio.NEVER if prompt is None else prompt, radio.NEVER, *state.values()) for sid, (prompt, state) in marks.items()}
|
||||
found = {}
|
||||
for row in rows:
|
||||
ts = epoch(row["ts"])
|
||||
if radio.kind(row) == RECEIPT or not begin <= ts <= end:
|
||||
continue
|
||||
acked = acks.get(ts, {})
|
||||
readers = [entry for sid, held in marks.items() if ts <= reach[sid] and (entry := reader(radio, row, sid, directory.at(sid, ts) or sid, held, acked))]
|
||||
found[ts] = {"readers": sorted(readers, key=order), "acks": sorted(acked)}
|
||||
return found
|
||||
@@ -1,9 +1,12 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import functools
|
||||
import importlib.util
|
||||
import subprocess
|
||||
import sys
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
from types import ModuleType
|
||||
from typing import Protocol
|
||||
|
||||
SCRIPT_TIMEOUT = 60
|
||||
@@ -16,6 +19,31 @@ OPERATOR_SCRIPT = "kvb-org-operator/scripts/operator.py"
|
||||
ESTIMATE = "kvb-org-queue-estimate/scripts/estimate.py"
|
||||
|
||||
|
||||
def script_module(script: Path) -> ModuleType | None:
|
||||
"""Returns:
|
||||
The installed script as a module, loaded again after an install
|
||||
changed it, None when it is missing or does not load.
|
||||
"""
|
||||
try:
|
||||
changed = script.stat().st_mtime_ns
|
||||
except OSError:
|
||||
return None
|
||||
return loaded(script, changed)
|
||||
|
||||
|
||||
@functools.cache
|
||||
def loaded(script: Path, changed: int) -> ModuleType | None:
|
||||
spec = importlib.util.spec_from_file_location(f"kvb_orchestrator_{script.stem}_{changed}", script)
|
||||
if spec is None:
|
||||
return None
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
try:
|
||||
spec.loader.exec_module(module)
|
||||
except (OSError, ImportError, RuntimeError, SyntaxError):
|
||||
return None
|
||||
return module
|
||||
|
||||
|
||||
class Runner(Protocol):
|
||||
skills: Path
|
||||
|
||||
|
||||
@@ -436,9 +436,28 @@ def cmd_assist(args: argparse.Namespace) -> int:
|
||||
return 0
|
||||
|
||||
|
||||
def seen(row: dict, name: str, sid: str, prompt: float, state: dict, since: float) -> bool:
|
||||
"""Args:
|
||||
row: a news.jsonl row with a numeric ts.
|
||||
name: the session's name, as name_of() gives it.
|
||||
sid: the session's id; a row pinned to it in to_sessions is for it
|
||||
under any name.
|
||||
prompt: the session's prompt mark, as prompt_mark() gives it.
|
||||
state: the session's radio-seen state; the mark of the row's channel
|
||||
holds the ts of the newest row the session took on it.
|
||||
since: rows at or before it count as taken; the hooks pass the start
|
||||
of WINDOW, a reader of the marks alone NEVER.
|
||||
|
||||
Returns:
|
||||
Whether the session has taken the row: it is for the session and its
|
||||
ts is not after the prompt mark, since or the mark of its channel.
|
||||
"""
|
||||
return for_me(row, name, sid) and not row["ts"] > max(prompt, since, mark(state, channel(row)))
|
||||
|
||||
|
||||
def pending(sid: str, name: str, state: dict, family: str) -> list[dict]:
|
||||
floor = max(prompt_mark(sid), time.time() - WINDOW, mark(state, family))
|
||||
return [row for row in items() if channel(row) == family and row["ts"] > floor and for_me(row, name, sid)]
|
||||
prompt, since = prompt_mark(sid), time.time() - WINDOW
|
||||
return [row for row in items() if channel(row) == family and for_me(row, name, sid) and not seen(row, name, sid, prompt, state, since)]
|
||||
|
||||
|
||||
def take(sid: str, family: str) -> list[dict]:
|
||||
|
||||
@@ -0,0 +1,277 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import os
|
||||
import tempfile
|
||||
import types
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from unittest import mock
|
||||
|
||||
import hub_support # noqa: F401
|
||||
from kvb_orchestrator import receipts
|
||||
from kvb_orchestrator.names import Directory, Names
|
||||
|
||||
REPO_ROOT = Path(__file__).resolve().parents[1]
|
||||
NOW = 1_800_000_000.0
|
||||
T = NOW - 1000
|
||||
EARLY = T - 100
|
||||
WINDOW = (T, T + 100)
|
||||
|
||||
|
||||
def message(cls: str, ts: float, to: list[str], sender: str = "One") -> dict:
|
||||
return {"ts": ts, "from": sender, "to": to, "text": cls, "class": cls, "v": 2}
|
||||
|
||||
|
||||
def receipt(reader: str, ts: float, call: object) -> dict:
|
||||
return {"ts": ts, "from": reader, "to": ["One"], "text": "c", "class": "receipt", "v": 2, "call": call}
|
||||
|
||||
|
||||
def directory(*held: tuple[str, str, float]) -> Directory:
|
||||
return Directory([{"session": sid, "name": name, "first_seen": since, "source": "claim"} for sid, name, since in held])
|
||||
|
||||
|
||||
def who(entry: dict) -> list[tuple[str, float | None]]:
|
||||
return [(reader["session"], reader["read"]) for reader in entry["readers"]]
|
||||
|
||||
|
||||
NAMED = directory(("s1", "One", EARLY), ("s2", "Two", EARLY), ("s3", "Three", EARLY))
|
||||
|
||||
|
||||
class TestReceipts(unittest.TestCase):
|
||||
def setUp(self):
|
||||
tmp = tempfile.TemporaryDirectory()
|
||||
self.addCleanup(tmp.cleanup)
|
||||
self.base = Path(tmp.name)
|
||||
self.council = self.base / "council"
|
||||
(self.council / receipts.SEEN).mkdir(parents=True)
|
||||
(self.council / receipts.PROMPT).mkdir()
|
||||
with mock.patch.dict(os.environ, {"AGENTS_COUNCIL_ROOT": str(self.council)}):
|
||||
self.radio = receipts.radio_module(REPO_ROOT / "skills")
|
||||
self.assertIsNotNone(self.radio)
|
||||
|
||||
def state(self, sid: str, text: str) -> None:
|
||||
(self.council / receipts.SEEN / f"{sid}.json").write_text(text, encoding="utf-8")
|
||||
|
||||
def prompt(self, sid: str, text: str) -> None:
|
||||
(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]:
|
||||
return receipts.readers_and_acks(self.radio, news, window, receipts.read_marks(self.council, NOW), names)
|
||||
|
||||
def test_every_class_gets_its_readers_by_channel_and_its_acks_from_receipts(self):
|
||||
news = [
|
||||
message("post", T + 1, ["Two"]),
|
||||
message("call", T + 2, ["Two", "Three"]),
|
||||
message("newsletter", T + 3, []),
|
||||
message("broadcast", T + 4, []),
|
||||
message("mayday", T + 5, []),
|
||||
message("pan-pan", T + 6, []),
|
||||
message("security", T + 7, []),
|
||||
receipt("Two", T + 8, T + 2),
|
||||
receipt("Three", T + 500, T + 2),
|
||||
]
|
||||
self.state("s1", json.dumps({"radio": T + 600, "mail": T + 600}))
|
||||
self.state("s2", json.dumps({"radio": T + 7, "mail": T + 3, "mtime": T + 900}))
|
||||
self.state("s3", json.dumps({"radio": T + 600}))
|
||||
self.prompt("s4", str(T + 4))
|
||||
found = self.inline(news, NAMED)
|
||||
self.assertEqual(sorted(found), [T + n for n in range(1, 8)])
|
||||
self.assertEqual({ts: (who(entry), entry["acks"]) for ts, entry in found.items()}, {
|
||||
T + 1: ([("s2", None)], []),
|
||||
T + 2: ([("s2", T + 8), ("s3", T + 500)], ["s2", "s3"]),
|
||||
T + 3: ([("s2", None), ("s4", None)], []),
|
||||
T + 4: ([("s3", None), ("s2", None), ("s4", None)], []),
|
||||
T + 5: ([("s3", None), ("s2", None)], []),
|
||||
T + 6: ([("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}])
|
||||
|
||||
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, [])]
|
||||
self.state("s1", json.dumps({"radio": T + 50}))
|
||||
self.state("s2", json.dumps({"radio": T + 50}))
|
||||
found = self.inline(news, directory(("s1", "One", EARLY), ("s2", "Two", EARLY)))
|
||||
self.assertEqual((who(found[T + 10]), found[T + 10]["acks"]), ([("s2", T + 11)], ["s2"]))
|
||||
self.assertEqual((who(found[T + 20]), found[T + 20]["acks"]), ([("s2", None)], []))
|
||||
|
||||
def test_a_renamed_name_goes_to_the_session_holding_it_at_the_time(self):
|
||||
news = [
|
||||
message("call", T + 10, ["Two"]),
|
||||
receipt("Two", T + 11, T + 10),
|
||||
message("call", T + 60, ["Two"]),
|
||||
receipt("Two", T + 61, T + 60),
|
||||
]
|
||||
self.state("s2", json.dumps({"radio": T + 70}))
|
||||
self.state("s5", json.dumps({"radio": T + 70}))
|
||||
found = self.inline(news, directory(("s2", "Two", EARLY), ("s2", "Deux", T + 50), ("s5", "Two", T + 55)))
|
||||
self.assertEqual((who(found[T + 10]), found[T + 10]["acks"]), ([("s2", T + 11), ("s5", None)], ["s2"]))
|
||||
self.assertEqual((who(found[T + 60]), found[T + 60]["acks"]), ([("s5", T + 61)], ["s5"]))
|
||||
self.assertEqual([reader["name"] for reader in found[T + 10]["readers"]], ["Two", "Two"])
|
||||
|
||||
def test_receipt_names_come_from_the_host_snapshot_alone_and_outlive_the_session(self):
|
||||
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"}]
|
||||
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)
|
||||
ended = receipts.session_directory(Names(file), {}, T + 50)
|
||||
for names in (live, ended):
|
||||
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()))
|
||||
news = [message("call", T + 10, ["Two"]), receipt("Two", T + 11, T + 10)]
|
||||
self.state("s2", json.dumps({"radio": T + 20}))
|
||||
self.state("sA", json.dumps({"radio": T + 20}))
|
||||
found = self.inline(news, ended)
|
||||
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):
|
||||
names = receipts.session_directory(Names(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)]
|
||||
self.state("s2", json.dumps({"radio": T + 30}))
|
||||
found = self.inline(news, names)
|
||||
self.assertEqual((who(found[T + 10]), found[T + 10]["acks"]), ([("s2", T + 11)], ["s2"]))
|
||||
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):
|
||||
news = [message("mayday", T + 10, []), message("call", T + 20, ["Two"]), receipt("Two", T + 21, T + 20)]
|
||||
self.state("s2", json.dumps({"radio": T + 90}))
|
||||
found = self.inline(news, NAMED)
|
||||
self.assertEqual((who(found[T + 10]), who(found[T + 20])), ([("s2", None)], [("s2", T + 21)]))
|
||||
|
||||
def test_a_missing_malformed_unreadable_oversized_or_future_mark_counts_as_not_read(self):
|
||||
news = [message("broadcast", T + 4, [])]
|
||||
outside = self.base / "outside.json"
|
||||
outside.write_text(json.dumps({"radio": NOW}), encoding="utf-8")
|
||||
for sid, text in (
|
||||
("text", json.dumps({"radio": str(NOW)})),
|
||||
("flag", json.dumps({"radio": True})),
|
||||
("nan", '{"radio": NaN}'),
|
||||
("inf", '{"radio": Infinity}'),
|
||||
("huge", json.dumps({"radio": 1e300})),
|
||||
("negative", json.dumps({"radio": -5})),
|
||||
("future", json.dumps({"radio": NOW + 60})),
|
||||
("list", json.dumps([NOW])),
|
||||
("broken", '{"radio": '),
|
||||
("empty", ""),
|
||||
("deep", "[" * receipts.STATE_BYTES),
|
||||
("oversized", json.dumps({"radio": NOW}) + " " * receipts.STATE_BYTES),
|
||||
("good", json.dumps({"radio": NOW}) + " " * (receipts.STATE_BYTES - 30)),
|
||||
):
|
||||
self.state(sid, text)
|
||||
(self.council / receipts.SEEN / "link.json").symlink_to(outside)
|
||||
(self.council / receipts.SEEN / "folder.json").mkdir()
|
||||
os.mkfifo(self.council / receipts.SEEN / "fifo.json")
|
||||
for sid, text in (
|
||||
("p-text", "soon"),
|
||||
("p-future", str(NOW + 60)),
|
||||
("p-nan", "nan"),
|
||||
("p-large", " " * receipts.PROMPT_BYTES + str(NOW)),
|
||||
("p-good", f" {NOW}\n"),
|
||||
):
|
||||
self.prompt(sid, text)
|
||||
(self.council / receipts.PROMPT / "p-link").symlink_to(self.council / receipts.PROMPT / "p-good")
|
||||
marks = receipts.read_marks(self.council, NOW)
|
||||
bad = sorted(set(marks) - {"good", "p-good"})
|
||||
self.assertEqual(bad, sorted([
|
||||
"text", "flag", "nan", "inf", "huge", "negative", "future", "list", "broken", "empty", "deep", "oversized",
|
||||
"link", "folder", "fifo", "p-text", "p-future", "p-nan", "p-large", "p-link",
|
||||
]))
|
||||
for sid in bad:
|
||||
with self.subTest(sid=sid):
|
||||
self.assertEqual(marks[sid], (None, {}))
|
||||
self.assertEqual((marks["good"], marks["p-good"]), ((None, {"radio": NOW}), (NOW, {})))
|
||||
found = receipts.readers_and_acks(self.radio, news, WINDOW, marks, directory())
|
||||
self.assertEqual(who(found[T + 4]), [("good", None), ("p-good", None)])
|
||||
|
||||
def test_marks_are_parsed_again_only_after_the_file_changed(self):
|
||||
self.state("s2", json.dumps({"radio": T}))
|
||||
with mock.patch.object(receipts, "read_regular", wraps=receipts.read_regular) as reads:
|
||||
first = receipts.read_marks(self.council, NOW)
|
||||
again = receipts.read_marks(self.council, NOW)
|
||||
fresh = self.council / receipts.SEEN / "s2.json.new"
|
||||
fresh.write_text(json.dumps({"radio": T + 5}), encoding="utf-8")
|
||||
os.replace(fresh, self.council / receipts.SEEN / "s2.json")
|
||||
changed = receipts.read_marks(self.council, NOW)
|
||||
self.assertEqual([marks["s2"] for marks in (first, again, changed)], [(None, {"radio": T}), (None, {"radio": T}), (None, {"radio": T + 5})])
|
||||
self.assertEqual(reads.call_count, 2)
|
||||
self.assertEqual(receipts.read_marks(self.council, T + 1), {"s2": (None, {})})
|
||||
|
||||
def test_a_receipt_acks_only_the_call_whose_time_it_carries(self):
|
||||
first, second = T + 10, T + 20
|
||||
news = [
|
||||
message("call", first, ["Two"]),
|
||||
message("call", second, ["Two"]),
|
||||
receipt("Two", T + 11, first),
|
||||
receipt("Two", T + 12, first),
|
||||
receipt("Two", T + 21, second + 1e-6),
|
||||
receipt("Two", T + 22, str(second)),
|
||||
receipt("Two", T + 23, True),
|
||||
receipt("s3", T + 24, second),
|
||||
receipt("Nobody", T + 25, second),
|
||||
{**receipt("Two", T + 26, second), "from": ["Two"]},
|
||||
]
|
||||
self.state("s2", json.dumps({"radio": T + 30}))
|
||||
self.state("s3", json.dumps({"radio": T + 10}))
|
||||
found = self.inline(news, directory(("s2", "Two", EARLY)))
|
||||
self.assertEqual((who(found[first]), found[first]["acks"]), ([("s2", T + 11)], ["s2"]))
|
||||
self.assertEqual((who(found[second]), found[second]["acks"]), ([("s2", None)], ["s3"]))
|
||||
|
||||
def test_a_receipt_outside_the_window_still_acks_its_call_and_no_receipt_row_is_served(self):
|
||||
call = T + 10
|
||||
news = [message("call", call, ["Two"]), receipt("Two", T + 400, call), message("call", T + 200, ["Two"])]
|
||||
self.state("s2", json.dumps({"radio": T + 300}))
|
||||
found = self.inline(news, NAMED, (T, T + 15))
|
||||
self.assertEqual(list(found), [call])
|
||||
self.assertEqual((who(found[call]), found[call]["acks"]), ([("s2", T + 400)], ["s2"]))
|
||||
self.assertEqual(list(self.inline(news, NAMED, (T + 300, T + 500))), [])
|
||||
|
||||
def test_the_hub_decides_by_radio_seen_without_a_delivery_window(self):
|
||||
calls = []
|
||||
|
||||
def seen(*args):
|
||||
calls.append(args)
|
||||
return False
|
||||
|
||||
spy = types.SimpleNamespace(**{**vars(self.radio), "seen": seen})
|
||||
news = [message("broadcast", T + 4, [])]
|
||||
self.state("s2", json.dumps({"radio": NOW}))
|
||||
found = receipts.readers_and_acks(spy, news, WINDOW, receipts.read_marks(self.council, NOW), NAMED)
|
||||
self.assertEqual(found, {T + 4: {"readers": [], "acks": []}})
|
||||
self.assertEqual(calls, [(news[0], "Two", "s2", self.radio.NEVER, {"radio": NOW}, self.radio.NEVER)])
|
||||
|
||||
def test_a_row_pinned_to_a_session_reaches_it_under_any_name(self):
|
||||
news = [{**message("call", T + 10, ["Old"]), "to_sessions": ["s2"]}]
|
||||
self.state("s2", json.dumps({"radio": T + 20}))
|
||||
self.assertEqual(who(self.inline(news, NAMED)[T + 10]), [("s2", None)])
|
||||
|
||||
def test_rows_without_a_usable_time_and_sessions_without_a_name(self):
|
||||
news = [{**message("broadcast", T + 4, []), "ts": "x"}, {**message("broadcast", T + 5, []), "ts": True}, message("call", T + 6, ["s9"])]
|
||||
self.state("s9", json.dumps({"radio": NOW}))
|
||||
found = self.inline(news, directory())
|
||||
self.assertEqual(list(found), [T + 6])
|
||||
self.assertEqual(found[T + 6]["readers"], [{"session": "s9", "name": "s9", "read": None}])
|
||||
|
||||
def test_read_marks_without_mark_folders_is_empty(self):
|
||||
self.assertEqual(receipts.read_marks(self.base / "missing", NOW), {})
|
||||
|
||||
def test_the_radio_module_needs_an_installed_radio_whose_seen_takes_the_hubs_arguments(self):
|
||||
self.assertIsNone(receipts.radio_module(self.base / "none"))
|
||||
fake = self.base / "skills" / "kvb-org-radio" / "scripts"
|
||||
fake.mkdir(parents=True)
|
||||
for version, text, loads in (
|
||||
(2, "SEEN = 1\n", False),
|
||||
(3, "def seen(\n", False),
|
||||
(4, "def seen(row, name, prompt, state, since):\n return True\n", False),
|
||||
(5, "def seen(row, name, sid, state, prompt, since):\n return True\n", False),
|
||||
(6, "def seen(row, name, sid, prompt, state, since):\n return True\n", True),
|
||||
):
|
||||
(fake / "radio.py").write_text(text, encoding="utf-8")
|
||||
os.utime(fake / "radio.py", ns=(1, version))
|
||||
with self.subTest(text=text):
|
||||
self.assertEqual(receipts.radio_module(self.base / "skills") is not None, loads)
|
||||
self.assertEqual(tuple(receipts.inspect.signature(self.radio.seen).parameters), receipts.SEEN_PARAMETERS)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,130 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import importlib.util
|
||||
import json
|
||||
import math
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import time
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from unittest import mock
|
||||
|
||||
REPO_ROOT = Path(__file__).resolve().parents[1]
|
||||
RADIO = REPO_ROOT / "skills" / "kvb-org-radio" / "scripts" / "radio.py"
|
||||
CHANNELS = {
|
||||
"mayday": "radio", "pan-pan": "radio", "security": "radio", "call": "radio", "broadcast": "radio",
|
||||
"post": "mail", "newsletter": "mail", "receipt": "mail",
|
||||
}
|
||||
ADDRESSED = ("call", "post", "receipt")
|
||||
NEVER = 0.0
|
||||
|
||||
|
||||
def load(root: Path):
|
||||
spec = importlib.util.spec_from_file_location("kvb_radio_seen_test", RADIO)
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
with mock.patch.dict(os.environ, {"AGENTS_COUNCIL_ROOT": str(root)}):
|
||||
spec.loader.exec_module(module)
|
||||
return module
|
||||
|
||||
|
||||
def row(cls: str, ts: float, to: list[str]) -> dict:
|
||||
return {"ts": ts, "from": "One", "to": to, "text": cls, "class": cls, "v": 2}
|
||||
|
||||
|
||||
class TestSeen(unittest.TestCase):
|
||||
def setUp(self):
|
||||
tmp = tempfile.TemporaryDirectory()
|
||||
self.addCleanup(tmp.cleanup)
|
||||
self.base = Path(tmp.name)
|
||||
self.radio = load(self.base / "council")
|
||||
|
||||
def seen(self, entry: dict, name: str, prompt: float, state: dict, since: float) -> bool:
|
||||
return self.radio.seen(entry, name, "s2", prompt, state, since)
|
||||
|
||||
def test_a_row_pinned_to_the_session_is_seen_under_any_name(self):
|
||||
pinned = {**row("call", 100.0, ["Old"]), "to_sessions": ["s2"]}
|
||||
self.assertTrue(self.seen(pinned, "Two", NEVER, {"radio": 100.0}, NEVER))
|
||||
self.assertFalse(self.radio.seen(pinned, "Two", "s3", NEVER, {"radio": 100.0}, NEVER))
|
||||
|
||||
def test_each_class_is_seen_by_the_mark_of_its_channel_alone(self):
|
||||
for cls, own in CHANNELS.items():
|
||||
other = "mail" if own == "radio" else "radio"
|
||||
entry = row(cls, 100.0, ["Two"] if cls in ADDRESSED else [])
|
||||
with self.subTest(cls=cls):
|
||||
self.assertTrue(self.seen(entry, "Two", NEVER, {own: 100.0}, NEVER))
|
||||
self.assertTrue(self.seen(entry, "Two", NEVER, {own: 150.0}, NEVER))
|
||||
self.assertFalse(self.seen(entry, "Two", NEVER, {own: 99.5}, NEVER))
|
||||
self.assertFalse(self.seen(entry, "Two", NEVER, {other: 500.0, "mtime": 500.0}, NEVER))
|
||||
self.assertFalse(self.seen(entry, "Two", NEVER, {}, NEVER))
|
||||
|
||||
def test_the_prompt_mark_and_since_cover_every_channel(self):
|
||||
for cls in CHANNELS:
|
||||
entry = row(cls, 100.0, ["Two"] if cls in ADDRESSED else [])
|
||||
with self.subTest(cls=cls):
|
||||
self.assertTrue(self.seen(entry, "Two", 100.0, {}, NEVER))
|
||||
self.assertTrue(self.seen(entry, "Two", NEVER, {}, 100.0))
|
||||
self.assertFalse(self.seen(entry, "Two", 99.5, {}, 99.5))
|
||||
|
||||
def test_a_row_the_session_sent_or_one_for_others_is_never_seen(self):
|
||||
marks = {"radio": 500.0, "mail": 500.0}
|
||||
self.assertFalse(self.seen(row("broadcast", 100.0, []), "One", 500.0, marks, 500.0))
|
||||
self.assertFalse(self.seen(row("call", 100.0, ["Three"]), "Two", 500.0, marks, 500.0))
|
||||
self.assertFalse(self.seen(row("post", 100.0, ["Three"]), "Two", 500.0, marks, 500.0))
|
||||
self.assertTrue(self.seen(row("call", 100.0, ["Three", "Two"]), "Two", NEVER, marks, NEVER))
|
||||
|
||||
def test_rows_without_the_version_mark_read_their_old_channel(self):
|
||||
wake = {"ts": 100.0, "from": "One", "to": ["Two"], "class": "direct", "wake": True}
|
||||
mail = {"ts": 100.0, "from": "One", "to": ["Two"], "text": "old direct"}
|
||||
board = {"ts": 100.0, "from": "One", "to": [], "text": "old board", "class": "broadcast"}
|
||||
self.assertTrue(self.seen(wake, "Two", NEVER, {"radio": 100.0}, NEVER))
|
||||
self.assertFalse(self.seen(wake, "Two", NEVER, {"mail": 100.0}, NEVER))
|
||||
self.assertTrue(self.seen(mail, "Two", NEVER, {"mail": 100.0}, NEVER))
|
||||
self.assertFalse(self.seen(mail, "Two", NEVER, {"radio": 100.0}, NEVER))
|
||||
self.assertTrue(self.seen(board, "Two", NEVER, {"mail": 100.0}, NEVER))
|
||||
|
||||
def test_a_mark_that_is_no_number_counts_as_never(self):
|
||||
for value in ("500", None, [500], {"x": 500}):
|
||||
with self.subTest(value=value):
|
||||
self.assertFalse(self.seen(row("broadcast", 100.0, []), "Two", NEVER, {"radio": value}, NEVER))
|
||||
|
||||
def test_a_nan_prompt_mark_leaves_nothing_to_take_as_before(self):
|
||||
for cls, own in CHANNELS.items():
|
||||
entry = row(cls, 100.0, ["Two"] if cls in ADDRESSED else [])
|
||||
with self.subTest(cls=cls):
|
||||
self.assertTrue(self.seen(entry, "Two", math.nan, {}, NEVER))
|
||||
self.assertFalse(self.seen(entry, "Two", NEVER, {own: math.nan}, NEVER))
|
||||
|
||||
|
||||
class TestPendingLeavesWhatSeenTook(unittest.TestCase):
|
||||
def test_show_lists_the_rows_after_every_mark_and_inside_the_window(self):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
base = Path(tmp)
|
||||
home, root = base / "home", base / "council"
|
||||
(home / ".claude" / "sessions").mkdir(parents=True)
|
||||
(home / ".claude" / "sessions" / "s2.json").write_text(json.dumps({"sessionId": "s2", "name": "Two"}), encoding="utf-8")
|
||||
(root / "radio-seen").mkdir(parents=True)
|
||||
(root / "news-seen").mkdir()
|
||||
now = time.time()
|
||||
rows = [
|
||||
{**row("newsletter", now - 90000, []), "text": "expired"},
|
||||
{**row("newsletter", now - 46, []), "text": "under the prompt mark"},
|
||||
{**row("security", now - 44, []), "text": "under the radio mark"},
|
||||
{**row("post", now - 70, ["Two"]), "text": "under the mail mark"},
|
||||
{**row("call", now - 20, ["Three"]), "text": "for three"},
|
||||
{**row("broadcast", now - 30, []), "text": "after the radio mark"},
|
||||
{**row("newsletter", now - 35, []), "text": "after the mail mark"},
|
||||
]
|
||||
(root / "news.jsonl").write_text("".join(json.dumps(entry) + "\n" for entry in rows), encoding="utf-8")
|
||||
(root / "news-seen" / "s2").write_text(str(now - 45), encoding="utf-8")
|
||||
(root / "radio-seen" / "s2.json").write_text(json.dumps({"radio": now - 40, "mail": now - 60}), encoding="utf-8")
|
||||
env = {"PATH": "/usr/bin:/bin", "HOME": str(home), "AGENTS_COUNCIL_ROOT": str(root), "AGENTS_COUNCIL_CONFIG": str(base / "config.json"), "CLAUDE_CODE_SESSION_ID": "s2"}
|
||||
shown = subprocess.run([sys.executable, str(RADIO), "show"], capture_output=True, text=True, env=env, check=False, timeout=30)
|
||||
self.assertEqual(shown.returncode, 0, shown.stderr)
|
||||
self.assertEqual([line.split(": ", 1)[1] for line in shown.stdout.splitlines()], ["after the radio mark", "after the mail mark"])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user