simplex: mark the reported message read so next_message advances

The bridge only read the daemon's unread state (/_get chats, /_get chat);
nothing wrote the read state, so a persistent-unread message was re-reported
on every call and simplex.next_message never advanced.

v7.0.3 does expose /_read chat @<id> and /_read chat items @<id> <ids>
(the earlier "v7 has no mark-read command" note was wrong). Added:

- SimplexDaemon.mark_read / request_mark_read plus read_command /
  parse_read_response; parse_direct_text_item now carries item_id.
- SimpleX bridge POST /read {"contact","item_ids"} marks exactly the listed
  items read on the bridge's own daemon, reporting ok/error honestly.
- The live inbox entry keeps item_id so /inbox/next hands back the
  consumable id.
- simplex.next_message consumes exactly the item it reported (a failed
  consume is surfaced as an honest note, never a fabricated read failure).

Precise per-item reading leaves the rest of the chat unread.
This commit is contained in:
gabby committed 2026-10-09 11:16:21 +00:00
1 parent 4e3e3f767c
commit fc0beb5798
8 files changed
+429 -34

No files matched your search

+91 -2
View File
@@ -38,6 +38,8 @@ class FakeDaemon:
history_error=None,
send_response=None,
send_error=None,
read_response=None,
read_error=None,
):
self.link = link
self.error = error
@@ -49,9 +51,12 @@ class FakeDaemon:
self.history_error = history_error
self.send_response = send_response
self.send_error = send_error
self.read_response = read_response
self.read_error = read_error
self.refresh_count = 0
self.chat_calls = []
self.history_calls = []
self.read_calls = []
self.sent = []
self.closed = False
self.on_message = None
@@ -105,6 +110,14 @@ class FakeDaemon:
raise self.history_error
return list(self.history_list)
def request_mark_read(self, chat_id, item_ids=None, timeout=20.0):
self.read_calls.append((str(chat_id), list(item_ids or [])))
if self.read_error is not None:
raise self.read_error
if self.read_response is not None:
return self.read_response
return {"ok": True, "type": "itemsReadForChat", "error": ""}
def close(self):
self.closed = True
@@ -116,8 +129,13 @@ def start_bridge(daemon=None, **config):
return bridge, daemon, f"http://127.0.0.1:{port}"
def inbound(text, contact_id="4", display_name="Alice"):
return {"text": text, "contact_id": contact_id, "display_name": display_name}
def inbound(text, contact_id="4", display_name="Alice", item_id=""):
return {
"text": text,
"contact_id": contact_id,
"display_name": display_name,
"item_id": item_id,
}
def get(url, token=None):
@@ -722,3 +740,74 @@ def test_describe_bridges_renders_service_auth_and_endpoints():
assert "/unread" in text
assert "/history" in text
assert "never speak" in text
def test_read_marks_given_items_only():
daemon = FakeDaemon()
bridge, _, base = start_bridge(daemon=daemon)
try:
result = post(f"{base}/read", {"contact": "4", "item_ids": ["8", "9"]})
assert result == {
"ok": True,
"contact_id": "4",
"item_ids": ["8", "9"],
"status": "read",
}
assert daemon.read_calls == [("4", ["8", "9"])], "only the listed items"
finally:
bridge.stop()
def test_read_validates_body():
bridge, daemon, base = start_bridge()
try:
payloads = (
{"item_ids": ["8"]},
{"contact": "4"},
{"contact": "4", "item_ids": []},
{"contact": " ", "item_ids": ["8"]},
)
for payload in payloads:
with pytest.raises(urllib.error.HTTPError) as exc:
post(f"{base}/read", payload)
assert exc.value.code == 400
assert daemon.read_calls == []
finally:
bridge.stop()
def test_read_without_connection_is_unavailable():
bridge, _, base = start_bridge(
daemon=FakeDaemon(read_error=RuntimeError("not connected"))
)
try:
result = post(f"{base}/read", {"contact": "4", "item_ids": ["8"]})
assert result["ok"] is False
assert result["status"] == "unavailable"
assert "not connected" in result["error"]
finally:
bridge.stop()
def test_read_reports_a_rejected_read():
bridge, _, base = start_bridge(
daemon=FakeDaemon(read_response={"ok": False, "error": "agent error"})
)
try:
result = post(f"{base}/read", {"contact": "4", "item_ids": ["8"]})
assert result["ok"] is False
assert result["status"] == "rejected"
assert result["error"] == "agent error"
finally:
bridge.stop()
def test_inbox_carries_item_id_for_consumption():
bridge, _, base = start_bridge()
try:
bridge._on_message(inbound("hi", contact_id="4", item_id="33"))
popped = get(f"{base}/inbox/next")["message"]
assert popped["item_id"] == "33"
finally:
bridge.stop()
+63 -1
View File
@@ -19,13 +19,15 @@ from semif_agent.simplex_ws import (
parse_chat,
parse_chat_item,
parse_direct_text_item,
parse_read_response,
read_command,
send_text_command,
)
# ---- parsing ----
def direct_item(text="hello", contact_id="4", display="alice", direction="directRcv", kind="text"):
def direct_item(text="hello", contact_id="4", display="alice", direction="directRcv", kind="text", item_id=7):
# v7 wire shape: the Contact's `displayName` is null and the peer name lives
# in `profile.displayName`; `localDisplayName` is auto-suffixed on collision.
# Received text is tagged `rcvMsgContent` with the message under `msgContent`.
@@ -41,6 +43,7 @@ def direct_item(text="hello", contact_id="4", display="alice", direction="direct
},
},
"chatItem": {
"meta": {"itemId": item_id},
"chatDir": {"type": direction},
"content": {
"type": "rcvMsgContent",
@@ -56,6 +59,7 @@ def test_parse_direct_text():
assert message["text"] == "hello"
assert message["contact_id"] == "4"
assert message["display_name"] == "alice"
assert message["item_id"] == "7"
def test_parse_filters_echo_group_and_non_text():
@@ -658,3 +662,61 @@ def test_request_chat_history_without_connection_raises():
daemon = SimplexDaemon("ws://x")
with pytest.raises(RuntimeError):
daemon.request_chat_history("4", timeout=1)
# ---- daemon: read marking ----
def test_read_command_targets_specific_items():
assert read_command("7", ["8", "9"]) == "/_read chat items @7 8,9"
def test_read_command_without_items_reads_the_whole_chat():
assert read_command("@7") == "/_read chat @7"
def test_read_command_ignores_blank_item_ids():
assert read_command("7", [" ", ""]) == "/_read chat @7"
def test_parse_read_response_ok_and_error():
assert parse_read_response({"type": "itemsReadForChat"})["ok"] is True
rejected = parse_read_response(
{"type": "chatCmdError", "chatError": {"type": "error"}}
)
assert rejected["ok"] is False
assert rejected["error"]
assert parse_read_response({})["ok"] is False
def test_mark_read_sends_item_command_and_parses_ok():
daemon = SimplexDaemon("ws://x")
async def scenario():
daemon._loop = asyncio.get_running_loop()
ws = FakeWS(daemon, [{"type": "itemsReadForChat"}])
daemon._ws = ws
return await daemon.mark_read("7", ["11", "12"], timeout=5), ws
result, ws = asyncio.run(scenario())
assert result["ok"] is True
assert [m["cmd"] for m in ws.sent] == ["/_read chat items @7 11,12"]
def test_mark_read_reports_a_rejected_command():
daemon = SimplexDaemon("ws://x")
async def scenario():
daemon._loop = asyncio.get_running_loop()
ws = FakeWS(
daemon, [{"type": "chatCmdError", "chatError": {"type": "error"}}]
)
daemon._ws = ws
return await daemon.mark_read("7", ["11"], timeout=5)
assert asyncio.run(scenario())["ok"] is False
def test_request_mark_read_without_connection_raises():
daemon = SimplexDaemon("ws://x")
with pytest.raises(RuntimeError):
daemon.request_mark_read("7", ["1"], timeout=1)