diff options
| author | Christophe Besson <cbesson@gmail.com> | 2026-08-16 15:28:53 +0200 |
|---|---|---|
| committer | Christophe Besson <cbesson@gmail.com> | 2026-08-16 15:28:53 +0200 |
| commit | fd770dc6293be67298f582de5800f2ca6fe24a8b (patch) | |
| tree | df8a393741ccf391b5bb4fa490980c632695cbdd /packages/meshbay-node/src/meshbay_node | |
| parent | 0bd3f805ffbd04b40b5150474336a5e5d200e72b (diff) | |
| download | meshbay-fd770dc6293be67298f582de5800f2ca6fe24a8b.tar.gz | |
feat(common): MNP 0.2 — liveness, and chat history read the way it is written
`get_messages` pages forward from the oldest message. That is the right shape
for "what happened since I last looked" and the wrong one for opening a
conversation, and the browser asked it for `since=0, limit=200` — so a group
with more than two hundred messages showed its first two hundred and the
exchange anyone came for was unreachable. Demonstrated on 300 messages: the
newest was simply absent from the answer.
`get_recent` and `get_before` page backwards, cursored on the row id rather than
the timestamp. Nothing makes a `time.time()` float unique, and a cursor on a
value two rows can share eventually skips a message or repeats it.
PING/PONG covers liveness on an already-open channel: a DataChannel whose peer
vanished without closing still reads as connected, and nothing noticed until a
real request hung. It is not a discovery mechanism — opening a connection to
ping costs a full ICE/DTLS handshake, measured at 0.6-7 s across two ISPs — so
presence in the group list comes from the hub's registry instead.
Both additions are backward compatible: an 0.1 peer sends no `before` and is
answered with the newest page, which is what it wanted.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Diffstat (limited to 'packages/meshbay-node/src/meshbay_node')
| -rw-r--r-- | packages/meshbay-node/src/meshbay_node/chat/store.py | 43 | ||||
| -rw-r--r-- | packages/meshbay-node/src/meshbay_node/transport/quic_server.py | 5 |
2 files changed, 48 insertions, 0 deletions
diff --git a/packages/meshbay-node/src/meshbay_node/chat/store.py b/packages/meshbay-node/src/meshbay_node/chat/store.py index a5b2d7d..6f23905 100644 --- a/packages/meshbay-node/src/meshbay_node/chat/store.py +++ b/packages/meshbay-node/src/meshbay_node/chat/store.py @@ -45,6 +45,11 @@ class StoredMessage: sender_name: str = "" +def _row(r) -> StoredMessage: + return StoredMessage(id=r[0], sender_id=r[1], iteration=r[2], payload=r[3], + timestamp=r[4], thread_id=r[5], sender_name=r[6] or "") + + class ChatStore: """Async SQLite chat store for one group.""" @@ -111,6 +116,44 @@ class ChatStore: for r in rows ] + async def get_recent(self, limit: int = 100) -> list[StoredMessage]: + """The newest `limit` messages, oldest first so they render in order. + + `get_messages(since=0, limit=n)` cannot answer this: it pages *forward* + from the oldest, so a client opening a group with 300 messages was shown + the first 200 and never the conversation it came for. Reaching the newest + rows means ordering DESC in SQL and reversing here. + """ + cursor = await self._db.execute( + "SELECT id, sender_id, iteration, payload, timestamp, thread_id, sender_name " + "FROM messages ORDER BY id DESC LIMIT ?", + (limit,), + ) + rows = await cursor.fetchall() + return [_row(r) for r in reversed(rows)] + + async def get_before(self, before_id: int, limit: int = 50) -> list[StoredMessage]: + """The `limit` messages immediately older than `before_id`, oldest first. + + The cursor is the row id, not the timestamp. Nothing makes `timestamp` + unique — it is a float from `time.time()` — and a cursor on a value two + rows can share is one that eventually skips a message or repeats it. + `id` is AUTOINCREMENT: unique, and ordered by insertion. + """ + cursor = await self._db.execute( + "SELECT id, sender_id, iteration, payload, timestamp, thread_id, sender_name " + "FROM messages WHERE id < ? ORDER BY id DESC LIMIT ?", + (before_id, limit), + ) + rows = await cursor.fetchall() + return [_row(r) for r in reversed(rows)] + + async def has_before(self, message_id: int) -> bool: + """Whether anything older than `message_id` exists — drives "load older".""" + cursor = await self._db.execute( + "SELECT 1 FROM messages WHERE id < ? LIMIT 1", (message_id,)) + return await cursor.fetchone() is not None + async def get_thread(self, thread_id: str, limit: int = 100) -> list[StoredMessage]: """Get messages in a thread.""" cursor = await self._db.execute( diff --git a/packages/meshbay-node/src/meshbay_node/transport/quic_server.py b/packages/meshbay-node/src/meshbay_node/transport/quic_server.py index ed3925d..73e668c 100644 --- a/packages/meshbay-node/src/meshbay_node/transport/quic_server.py +++ b/packages/meshbay-node/src/meshbay_node/transport/quic_server.py @@ -203,6 +203,11 @@ class _MNPServerProtocol(QuicConnectionProtocol): self._do_stream_segment_sync(stream_id, msg) elif mtype == MNP.CHAT_MESSAGE: self._do_chat_message_sync(stream_id, msg) + elif mtype == MNP.PING: + # Liveness is transport-agnostic, and a native client over QUIC + # has the same half-open problem a DataChannel does. + self._send(stream_id, {"type": MNP.PONG, "v": MNP_VERSION, + "token": msg.get("token")}) else: log.warning("Unknown MNP message type: %s", mtype) except Exception as e: |