The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(buzz): resume watched channels from a durable cursor across restarts (#90464)
Alessandro Bonid36827b0864e
diff --git a/contributors/emails/[email protected] b/contributors/emails/[email protected]new file mode 100644index 00000000000..904681d271e--- /dev/null+++ b/contributors/emails/[email protected]@@ -0,0 +1,2 @@+sandrohub013+# buzz durable channel cursors (#90464)diff --git a/plugins/platforms/buzz/adapter.py b/plugins/platforms/buzz/adapter.pyindex 88c9ca6b83b..2aeda88396c 100644--- a/plugins/platforms/buzz/adapter.py+++ b/plugins/platforms/buzz/adapter.py@@ -224,6 +224,9 @@ def _escape_unresolved_presentation_mention(content: str, error: str) -> Optiona_FETCH_LIMIT = 50# Bound on the per-channel de-dupe set (events, not bytes)._SEEN_CAP = 500+# Where the per-channel cursors survive a restart, relative to HERMES_HOME.+_CURSOR_STATE_SUBDIR = "buzz"+_CURSOR_STATE_FILENAME = "channel-cursors.json"# Re-run DM discovery (``dms list`` plus the channels-list fallback) every# N poll sweeps to pick up conversations opened mid-run._DM_DISCOVERY_EVERY = 5@@ -713,6 +716,9 @@ class BuzzAdapter(BasePlatformAdapter):# event_meta backs NIP-10 reply-parent resolution for require_mention# (thread replies to our own messages count as addressed — #75826).self._channel_state: Dict[str, dict] = {}+ # Cursors read back from disk at connect() and consumed by the first+ # seed of each channel; empty on a first-ever run.+ self._restored_cursors: Dict[str, dict] = {}self._channel_names: Dict[str, str] = {}# channel_id -> raw ``channels list`` entry; drives DM-vs-channel# classification (see _may_reclassify_as_dm).@@ -839,10 +845,14 @@ class BuzzAdapter(BasePlatformAdapter):return False# Seed high-water marks from the newest events so a (re)start never- # replays channel history into the agent.+ # replays channel history into the agent — except where a previous run+ # left a cursor, which is restored instead so the events that landed+ # while we were down still dispatch (#90464).+ self._load_cursors()for channel_id in watch:await self._seed_channel(channel_id, chat_type="group")await self._discover_dms(seed=True)+ self._save_cursors()# Inbound transport: prefer the NIP-42-authenticated WebSocket# subscription (push, near-zero latency); fall back to CLI polling@@ -1522,8 +1532,11 @@ class BuzzAdapter(BasePlatformAdapter):channel_id = subscriptions.get(subscription_id)state = self._channel_state.get(channel_id or "")if channel_id and state is not None:+ before = self._cursor_mark(state)await self._handle_event(channel_id, state, event)self._trim_seen(state)+ if self._cursor_mark(state) != before:+ self._save_cursors()elif message[0] == "CLOSED":detail = message[-1] if len(message) > 2 else "subscription closed"raise ConnectionError(str(detail))@@ -1567,8 +1580,113 @@ class BuzzAdapter(BasePlatformAdapter):"event_meta": OrderedDict(),}+ # ── Durable channel cursors ───────────────────────────────────────────++ @staticmethod+ def _cursor_path() -> Path:+ from hermes_constants import get_hermes_home++ return get_hermes_home() / _CURSOR_STATE_SUBDIR / _CURSOR_STATE_FILENAME++ def _load_cursors(self) -> None:+ """Read back the cursors a previous run persisted.++ A file written by a different identity or against a different relay is+ ignored rather than trusted: channel ids would collide while the event+ stream behind them is a different one. Any read/parse failure leaves+ the cursors empty, which degrades to the old seed-from-history+ behaviour instead of failing the connect.+ """+ self._restored_cursors = {}+ try:+ path = self._cursor_path()+ if not path.exists():+ return+ data = json.loads(path.read_text(encoding="utf-8"))+ except Exception:+ logger.debug("Buzz: could not read channel cursors", exc_info=True)+ return+ if not isinstance(data, dict):+ return+ if (+ data.get("identity") != self._self_pubkey+ or data.get("relay") != self.relay_url+ ):+ return+ channels = data.get("channels")+ if not isinstance(channels, dict):+ return+ for channel_id, entry in channels.items():+ if not isinstance(entry, dict):+ continue+ try:+ last_ts = int(entry.get("last_ts") or 0)+ except (TypeError, ValueError):+ continue+ raw_seen = entry.get("seen")+ seen = (+ [str(event_id) for event_id in raw_seen][-_SEEN_CAP:]+ if isinstance(raw_seen, list)+ else []+ )+ self._restored_cursors[str(channel_id)] = {+ "chat_type": str(entry.get("chat_type") or ""),+ "last_ts": last_ts,+ "seen": seen,+ }++ def _save_cursors(self) -> None:+ """Persist every watched channel's cursor. Never raises."""+ payload = {+ "identity": self._self_pubkey,+ "relay": self.relay_url,+ "channels": {+ channel_id: {+ "chat_type": state.get("chat_type") or "group",+ "last_ts": int(state.get("last_ts") or 0),+ "seen": list(state.get("seen") or ()),+ }+ for channel_id, state in self._channel_state.items()+ },+ }+ try:+ from utils import atomic_json_write++ atomic_json_write(self._cursor_path(), payload, indent=None)+ except Exception:+ logger.debug("Buzz: could not persist channel cursors", exc_info=True)++ @staticmethod+ def _cursor_mark(state: dict) -> tuple:+ """Cheap change detector for one channel's cursor."""+ seen = state.get("seen") or ()+ return (+ int(state.get("last_ts") or 0),+ len(seen),+ next(reversed(seen), None) if seen else None,+ )++ def _restore_channel_state(self, channel_id: str, chat_type: str) -> bool:+ """Install a persisted cursor for *channel_id*; True when one existed.++ Restoring is what closes the restart gap: seeding from current history+ instead would mark everything that arrived while the gateway was down+ as already seen, so the relay's durable copy is never dispatched+ (#90464).+ """+ restored = self._restored_cursors.pop(channel_id, None)+ if restored is None:+ return False+ state = self._new_channel_state(restored["chat_type"] or chat_type)+ state["last_ts"] = restored["last_ts"]+ state["seen"] = OrderedDict((event_id, None) for event_id in restored["seen"])+ self._channel_state[channel_id] = state+ return True+async def _seed_channel(self, channel_id: str, chat_type: str) -> None:"""Initialize a channel's high-water mark from its newest events."""+ if self._restore_channel_state(channel_id, chat_type):+ returnstate = self._new_channel_state(chat_type)self._channel_state[channel_id] = statecode, out, err = await self._run_cli(@@ -1617,7 +1735,7 @@ class BuzzAdapter(BasePlatformAdapter):continueif seed:await self._seed_channel(dm_id, chat_type="dm")- else:+ elif not self._restore_channel_state(dm_id, "dm"):self._channel_state[dm_id] = self._new_channel_state("dm")self._channel_names.setdefault(dm_id, "DM")@@ -1637,7 +1755,7 @@ class BuzzAdapter(BasePlatformAdapter):continueif seed:await self._seed_channel(ch_id, chat_type="dm")- else:+ elif not self._restore_channel_state(ch_id, "dm"):self._channel_state[ch_id] = self._new_channel_state("dm")async def _poll_channel(self, channel_id: str) -> None:@@ -1655,9 +1773,14 @@ class BuzzAdapter(BasePlatformAdapter):"Buzz: poll of channel %s failed — %s", channel_id, _cli_error_message(err, code))return+ before = self._cursor_mark(state)for event in _parse_json_list(out):await self._handle_event(channel_id, state, event)self._trim_seen(state)+ # Persist only when the sweep actually moved the cursor, so an idle+ # channel does not rewrite the file every poll interval.+ if self._cursor_mark(state) != before:+ self._save_cursors()async def _handle_event(self, channel_id: str, state: dict, event: dict) -> None:"""De-dupe, filter, and dispatch a single ``messages get`` event."""diff --git a/tests/gateway/test_buzz_adapter.py b/tests/gateway/test_buzz_adapter.pyindex c4a943b94dc..19be9adf7ac 100644--- a/tests/gateway/test_buzz_adapter.py+++ b/tests/gateway/test_buzz_adapter.py@@ -1985,3 +1985,154 @@ class TestBuzzAdapterEdit:assert await adapter.delete_message(CHANNEL, "") is Falseassert cli.calls == []+# ── Durable channel cursors across restart (#90464) ───────────────────────+++class TestChannelCursorPersistence:+ """A restart must resume from the saved cursor, not reseed from history.++ Seeding marks every event currently in the channel as seen. Anything that+ arrived while the gateway was down is in that history, so an unconditional+ reseed swallows it permanently even though the relay still has it.+ """++ @pytest.fixture+ def adapter(self, tmp_path, monkeypatch):+ monkeypatch.setenv("HERMES_HOME", str(tmp_path))+ a = _make_adapter()+ a._dispatched = []++ async def capture(**kwargs):+ a._dispatched.append(kwargs)++ a._dispatch_message = capture+ a._message_handler = AsyncMock()+ return a++ @staticmethod+ def _cursor_file(tmp_path):+ return tmp_path / "buzz" / "channel-cursors.json"++ async def _seed(self, adapter, *events):+ cli = _ScriptedCli()+ cli.script("messages", "get", list(events))+ adapter._run_cli = cli+ adapter._load_cursors()+ await adapter._seed_channel(CHANNEL, chat_type="group")+ adapter._save_cursors()+ return cli++ @pytest.mark.asyncio+ async def test_seed_writes_a_cursor(self, adapter, tmp_path):+ await self._seed(adapter, _event("e1", created_at=100), _event("e2", created_at=200))++ saved = json.loads(self._cursor_file(tmp_path).read_text(encoding="utf-8"))+ assert saved["identity"] == SELF_PUBKEY+ assert saved["relay"] == "https://test.relay"+ assert saved["channels"][CHANNEL]["last_ts"] == 200+ assert saved["channels"][CHANNEL]["seen"] == ["e1", "e2"]++ @pytest.mark.asyncio+ async def test_restart_resumes_instead_of_reseeding(self, adapter, tmp_path, monkeypatch):+ await self._seed(adapter, _event("e1", content="@Chip first", created_at=100))++ # Restart. The relay now also holds a mention that landed while the+ # gateway was down, and it sits in the same history a reseed reads.+ restarted = _make_adapter()+ restarted._dispatched = []++ async def capture(**kwargs):+ restarted._dispatched.append(kwargs)++ restarted._dispatch_message = capture+ restarted._message_handler = AsyncMock()+ cli = _ScriptedCli()+ cli.script("messages", "get", [+ _event("e1", content="@Chip first", created_at=100),+ _event("e2", content="@Chip sent while you were down", created_at=150),+ ])+ restarted._run_cli = cli+ restarted._load_cursors()+ await restarted._seed_channel(CHANNEL, chat_type="group")++ # Restoring must not spend a CLI call on history it is not going to use.+ assert cli.calls == []+ state = restarted._channel_state[CHANNEL]+ assert set(state["seen"]) == {"e1"}+ assert state["last_ts"] == 100++ # The first poll after the restart delivers the missed mention.+ await restarted._poll_channel(CHANNEL)+ assert [d["message_id"] for d in restarted._dispatched] == ["e2"]++ @pytest.mark.asyncio+ async def test_cursor_survives_only_for_the_same_identity_and_relay(self, adapter, tmp_path, monkeypatch):+ await self._seed(adapter, _event("e1", created_at=100))++ # Same machine, different bot: channel ids would collide but the event+ # stream behind them is another one, so the cursor must be ignored.+ other = _make_adapter()+ other._self_pubkey = OTHER_PUBKEY+ other._load_cursors()+ assert other._restored_cursors == {}++ elsewhere = _make_adapter({"relay_url": "https://other.relay"})+ elsewhere._load_cursors()+ assert elsewhere._restored_cursors == {}++ @pytest.mark.asyncio+ async def test_unreadable_cursor_file_falls_back_to_seeding(self, adapter, tmp_path):+ path = self._cursor_file(tmp_path)+ path.parent.mkdir(parents=True, exist_ok=True)+ path.write_text("{ not json", encoding="utf-8")++ cli = _ScriptedCli()+ cli.script("messages", "get", [_event("e1", created_at=100)])+ adapter._run_cli = cli+ adapter._load_cursors()+ await adapter._seed_channel(CHANNEL, chat_type="group")++ # Degrades to the old behaviour rather than failing the connect.+ assert adapter._restored_cursors == {}+ assert set(adapter._channel_state[CHANNEL]["seen"]) == {"e1"}++ @pytest.mark.asyncio+ async def test_restored_seen_set_stays_bounded(self, adapter, tmp_path):+ cap = _buzz_mod._SEEN_CAP+ path = self._cursor_file(tmp_path)+ path.parent.mkdir(parents=True, exist_ok=True)+ path.write_text(+ json.dumps({+ "identity": SELF_PUBKEY,+ "relay": "https://test.relay",+ "channels": {+ CHANNEL: {+ "chat_type": "group",+ "last_ts": 100,+ "seen": [f"e{i}" for i in range(cap * 2)],+ }+ },+ }),+ encoding="utf-8",+ )+ adapter._load_cursors()+ await adapter._seed_channel(CHANNEL, chat_type="group")++ seen = adapter._channel_state[CHANNEL]["seen"]+ assert len(seen) == cap+ # The newest ids are the ones worth keeping for de-dupe.+ assert f"e{cap * 2 - 1}" in seen+ assert "e0" not in seen++ @pytest.mark.asyncio+ async def test_idle_poll_does_not_rewrite_the_cursor(self, adapter, tmp_path):+ cli = await self._seed(adapter, _event("e1", created_at=100))+ path = self._cursor_file(tmp_path)+ before = path.stat().st_mtime_ns++ cli.responses.clear()+ cli.script("messages", "get", [_event("e1", created_at=100)])+ await adapter._poll_channel(CHANNEL)++ assert path.stat().st_mtime_ns == before+