simplex: honest forwarding — verify sends, disambiguate contacts, read persistent unread

The SimpleX forwarding path could report a failed send as success and could
route to the wrong peer:

- contact_entry dropped the daemon's unique localDisplayName, so two peers
  sharing a profile name ("pepper" / "pepper_1") were indistinguishable; the
  agent's SemIf choice then picked the stale one.
- The bridge's POST /send enqueued the message fire-and-forget and returned
  ok:true; the daemon later marked it sndError (auth), yet the agent reported
  "sent" and assessed success.
- next_message read only the live buffer, missing messages that arrived before
  the bridge started.

Fixes:
- simplex_ws: contact_entry exposes local_name + connected/auth_errors;
  parse_chat_item surfaces the agentError; parse_send_response normalizes a
  /_send reply; add correlated send()/request_send().
- bridges/simplex: resolve disambiguates by id -> local_name/display_name and
  prefers the healthiest peer; send() confirms the real terminal status
  (sndSent/sndRcvd ok, sndError with reason) instead of a false ok; /unread
  carries cached connection health; catalog documents the new fields/status.
- bridges/inbox: carry local_name/connected/auth_errors through the cache.
- seeds/simplex/next_message: fall back to /unread + /history and prefer the
  healthy conversation.
- tests: updated shapes + new coverage for resolution and send verification.
This commit is contained in:
gabby committed 2026-10-07 09:47:26 +00:00
1 parent 0efbf218c5
commit dff4849947
7 files changed
+639 -61

No files matched your search

+112 -2
View File
@@ -32,6 +32,8 @@ class FakeDaemon:
chats_error=None,
history=None,
history_error=None,
send_response=None,
send_error=None,
):
self.link = link
self.error = error
@@ -41,6 +43,8 @@ class FakeDaemon:
self.chats_error = chats_error
self.history_list = history or []
self.history_error = history_error
self.send_response = send_response
self.send_error = send_error
self.refresh_count = 0
self.chat_calls = []
self.history_calls = []
@@ -58,6 +62,19 @@ class FakeDaemon:
if chat_id is not None:
self.sent.append((chat_id, text))
def request_send(self, chat_id, text, timeout=20.0):
self.sent.append((chat_id, text))
if self.send_error is not None:
raise self.send_error
if self.send_response is not None:
return self.send_response
return {
"type": "newChatItems",
"chatItems": [
{"chatItem": {"meta": {"itemId": 1, "itemStatus": {"type": "sndRcvd"}}}}
],
}
def request_address(self, timeout=20.0):
if self.error is not None:
raise self.error
@@ -186,7 +203,9 @@ def test_send_routes_to_daemon():
bridge, daemon, base = start_bridge()
try:
result = post(f"{base}/send", {"recipient": "7", "text": "on my way"})
assert result == {"ok": True, "contact_id": "7"}
assert result["ok"] is True, result
assert result["contact_id"] == "7", result
assert result["status"] == "sndRcvd", result
assert daemon.sent == [("7", "on my way")]
finally:
bridge.stop()
@@ -203,6 +222,80 @@ def test_send_resolves_known_display_name():
bridge.stop()
def test_send_reports_a_failed_send_not_a_false_success():
# A dead connection: the daemon creates the item then marks it sndError.
# The bridge must surface ok:false with the reason, never a false success.
failed = {
"type": "newChatItems",
"chatItems": [
{
"chatItem": {
"meta": {
"itemId": 12,
"itemStatus": {
"type": "sndError",
"agentError": {"type": "auth"},
},
}
}
}
],
}
daemon = FakeDaemon(send_response=failed)
bridge, _, base = start_bridge(daemon=daemon)
try:
result = post(f"{base}/send", {"recipient": "7", "text": "hey"})
assert result["ok"] is False, result
assert result["status"] == "sndError", result
assert result["error"] == "auth", result
assert result["contact_id"] == "7"
finally:
bridge.stop()
def test_send_verifies_status_via_history():
# The daemon accepts the send (sndNew) and only later resolves it to a
# delivered status; the bridge polls history to confirm.
accepted = {
"type": "newChatItems",
"chatItems": [
{"chatItem": {"meta": {"itemId": 5, "itemStatus": {"type": "sndNew"}}}}
],
}
history = [
{"item_id": "5", "text": "hi", "status": "sndRcvd", "unread": False,
"direction": "directSnd", "sent_at": ""},
]
daemon = FakeDaemon(send_response=accepted, history=history)
bridge, _, base = start_bridge(daemon=daemon)
try:
result = post(f"{base}/send", {"recipient": "7", "text": "hi"})
assert result["ok"] is True, result
assert result["status"] == "sndRcvd", result
assert daemon.history_calls, "the bridge must poll for the real status"
finally:
bridge.stop()
def test_resolve_prefers_a_healthy_duplicate():
# Two peers share the display name "pepper"; the stale one has auth errors.
daemon = FakeDaemon(
contacts=[
{"id": "3", "display_name": "pepper", "local_name": "pepper",
"connected": True, "auth_errors": 2},
{"id": "4", "display_name": "pepper", "local_name": "pepper_1",
"connected": True, "auth_errors": 0},
]
)
bridge, _, _ = start_bridge(daemon=daemon)
try:
assert bridge.resolve("pepper") == "4", "a stale duplicate must lose"
assert bridge.resolve("pepper_1") == "4"
assert bridge.resolve("3") == "3", "an explicit id always wins"
finally:
bridge.stop()
def test_contacts_refreshed_from_daemon():
# A contact the bridge has never received from must still be visible: the
# daemon knows them, so /send can address them.
@@ -222,7 +315,7 @@ def test_send_resolves_daemon_only_contact():
bridge, _, base = start_bridge(daemon=daemon)
try:
result = post(f"{base}/send", {"recipient": "Zed", "text": "hello"})
assert result == {"ok": True, "contact_id": "42"}
assert result["ok"] is True and result["contact_id"] == "42", result
assert daemon.sent == [("42", "hello")]
finally:
bridge.stop()
@@ -370,6 +463,23 @@ def test_unread_returns_daemon_chats():
bridge.stop()
def test_unread_attaches_cached_connection_health():
daemon = FakeDaemon(chats=[chat_summary(contact_id="4", display_name="pepper")])
bridge, _, base = start_bridge(daemon=daemon)
try:
bridge.inbox.merge_contacts(
[
{"id": "4", "display_name": "pepper", "local_name": "pepper_1",
"connected": True, "auth_errors": 0},
]
)
chats = get(f"{base}/unread")["chats"]
assert chats[0]["connected"] is True
assert chats[0]["auth_errors"] == 0
finally:
bridge.stop()
def test_unread_honours_count_query():
daemon = FakeDaemon(chats=[])
bridge, _, base = start_bridge(daemon=daemon)
+51 -3
View File
@@ -141,18 +141,66 @@ def test_contact_link_reads_both_shapes():
def test_contact_entry_reads_v7_shape():
assert contact_entry(
{"contactId": 4, "displayName": None, "profile": {"displayName": "Alice"}}
) == {"id": "4", "display_name": "Alice"}
) == {"id": "4", "display_name": "Alice", "local_name": "Alice"}
assert contact_entry({"contactId": 7, "localDisplayName": "bob_1"}) == {
"id": "7",
"display_name": "bob_1",
"local_name": "bob_1",
}
assert contact_entry({"chatId": 9, "profile": {"displayName": "Carol"}}) == {
"id": "9",
"display_name": "Carol",
"local_name": "Carol",
}
assert contact_entry({"profile": {"displayName": "no id"}}) is None
def test_contact_entry_keeps_unique_local_name_and_health():
# Two peers share the profile name "pepper"; the daemon's local name is
# unique, and the connection health tells a live peer from a stale one.
healthy = contact_entry(
{
"contactId": 4,
"localDisplayName": "pepper_1",
"profile": {"displayName": "pepper"},
"contactStatus": "active",
"activeConn": {"connStatus": {"type": "ready"}, "authErrCounter": 0},
}
)
assert healthy == {
"id": "4",
"display_name": "pepper",
"local_name": "pepper_1",
"connected": True,
"auth_errors": 0,
}
stale = contact_entry(
{
"contactId": 3,
"localDisplayName": "pepper",
"profile": {"displayName": "pepper"},
"contactStatus": "active",
"activeConn": {"connStatus": {"type": "ready"}, "authErrCounter": 2},
}
)
assert stale["local_name"] == "pepper"
assert stale["connected"] is True
assert stale["auth_errors"] == 2
def test_contact_entry_marks_a_non_ready_connection_disconnected():
entry = contact_entry(
{
"contactId": 4,
"profile": {"displayName": "Alice"},
"contactStatus": "active",
"activeConn": {"connStatus": {"type": "connecting"}},
}
)
assert entry["connected"] is False
assert "auth_errors" not in entry
# ---- daemon: accept + round-trips ----
class FakeWS:
@@ -315,8 +363,8 @@ def test_contacts_parses_contacts_list():
result, ws = asyncio.run(scenario())
assert result == [
{"id": "4", "display_name": "Alice"},
{"id": "7", "display_name": "bob_1"},
{"id": "4", "display_name": "Alice", "local_name": "Alice"},
{"id": "7", "display_name": "bob_1", "local_name": "bob_1"},
]
assert [m["cmd"] for m in ws.sent] == ["/user", "/_contacts 3"]