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

+21 -15
View File
@@ -18,7 +18,7 @@ class MessagingInbox:
self._lock = threading.Lock()
self._items: deque[dict] = deque()
self._max = max(1, int(max_size))
self._contacts: dict[str, str] = {}
self._contacts: dict[str, dict] = {}
def record(self, message: dict) -> dict:
contact_id = str(message.get("contact_id") or "")
@@ -31,15 +31,26 @@ class MessagingInbox:
"received_at": time.time(),
}
with self._lock:
if display_name:
self._contacts[contact_id] = display_name
else:
self._contacts.setdefault(contact_id, "")
self._remember(contact_id, message)
self._items.append(entry)
while len(self._items) > self._max:
self._items.popleft()
return entry
def _remember(self, contact_id: str, contact: dict) -> None:
"""Upsert the cached contact for an id (caller holds the lock)."""
current = self._contacts.setdefault(
contact_id, {"id": contact_id, "display_name": ""}
)
name = contact.get("display_name") or ""
if name:
current["display_name"] = name
# Keep the unique local name and connection health when the source knows
# them; never invent them (an inbound item has no connection state).
for key in ("local_name", "connected", "auth_errors"):
if key in contact and contact[key] not in (None, ""):
current[key] = contact[key]
def peek(self) -> list[dict]:
with self._lock:
return list(self._items)
@@ -59,22 +70,17 @@ class MessagingInbox:
"""Upsert contacts learned from the daemon (not from a message).
A non-empty incoming name overwrites a cached one; an empty name never
clobbers a name already learned from an inbound message.
clobbers a name already learned from an inbound message. The unique
local name and connection health ride along when the daemon reports
them.
"""
with self._lock:
for contact in contacts:
contact_id = str(contact.get("id") or "")
if not contact_id:
continue
name = contact.get("display_name") or ""
if name:
self._contacts[contact_id] = name
else:
self._contacts.setdefault(contact_id, "")
self._remember(contact_id, contact)
def contacts(self) -> list[dict]:
with self._lock:
return [
{"id": contact_id, "display_name": name}
for contact_id, name in self._contacts.items()
]
return [dict(contact) for contact in self._contacts.values()]