gateway: parse v7 rcvMsgContent shape and profile display name

Inbound DMs were silently dropped: simplex-chat v7 tags received text as
{"type": "rcvMsgContent", "msgContent": {"type": "text", ...}}, but
_parse_chat_item required a flat {"type": "text", "text": ...}. Every
message returned None (no gateway_message/gateway_sent/denied ever fired) so
pepper got no reply.

- parse content.msgContent.text, tolerating the DB/Aeson constructor-keyed
  nesting as well
- resolve the peer name from profile.displayName first; v7's contact
  displayName is null and localDisplayName is collision-suffixed (pepper_1)
- update the synthetic fixture to the real wire shape and add regression tests
- set PYTHONUNBUFFERED=1 in the rendered gateway unit so journal logging is live
This commit is contained in:
Denton Social committed 2026-09-26 19:51:52 -05:00
1 parent 5f05c5c749
commit b348f19486
3 files changed
+84 -5

No files matched your search

+1
View File
@@ -7,6 +7,7 @@ Wants=network-online.target semif-simplex.service
Type=simple
WorkingDirectory=@REPO@
Environment=HF_HOME=@REPO@/.runtime/hf
Environment=PYTHONUNBUFFERED=1
ExecStart=@REPO@/.runtime/venv/bin/python -m semif_agent.cli gateway
Restart=always
RestartSec=5
+22 -3
View File
@@ -161,14 +161,33 @@ class SimplexAdapter(GatewayAdapter):
if direction and "Snd" in direction:
return None # own echo
content = chat_item.get("content") or {}
if content.get("type") != "text":
# simplex-chat v7 tags the content union by constructor: a received text
# message is {"type": "rcvMsgContent", "msgContent": {"type": "text",
# "text": ...}}. The daemon's DB/Aeson encoding nests under the
# constructor key instead ({"rcvMsgContent": {"msgContent": ...}}), so
# accept both shapes.
if content.get("type") == "rcvMsgContent":
msg_content = content.get("msgContent") or {}
elif isinstance(content.get("rcvMsgContent"), dict):
msg_content = content["rcvMsgContent"].get("msgContent") or {}
else:
return None
text = str(content.get("text", "")).strip()
if msg_content.get("type") != "text":
return None
text = str(msg_content.get("text", "")).strip()
if not text:
return None
contact = chat_info.get("contact") or {}
contact_id = str(contact.get("contactId") or chat_info.get("chatId") or "")
display_name = contact.get("displayName") or contact.get("localDisplayName")
profile = contact.get("profile") or {}
# v7's Contact has `displayName: null`; the real peer name lives in
# `profile.displayName`, and `localDisplayName` is auto-suffixed on
# collisions (e.g. a second contact becomes `pepper_1`).
display_name = (
profile.get("displayName")
or contact.get("displayName")
or contact.get("localDisplayName")
)
if not contact_id:
return None
if not self.is_authorized(contact_id, display_name):
+61 -2
View File
@@ -66,15 +66,26 @@ def test_check_requirements_needs_url():
# ---- adapter: parsing ----
def direct_item(text="hello", contact_id="4", display="alice", direction="directRcv", kind="text"):
# 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`.
return {
"chatInfo": {
"type": "direct",
"chatId": contact_id,
"contact": {"contactId": contact_id, "displayName": display},
"contact": {
"contactId": contact_id,
"localDisplayName": display,
"displayName": None,
"profile": {"displayName": display},
},
},
"chatItem": {
"chatDir": {"type": direction},
"content": {"type": kind, "text": text},
"content": {
"type": "rcvMsgContent",
"msgContent": {"type": kind, "text": text},
},
},
}
@@ -103,6 +114,54 @@ def test_parse_filters_unauthorized_contact():
assert adapter._parse_chat_item(direct_item(contact_id="4", display="alice")) is None
def test_parse_real_v7_payload_with_suffixed_local_name():
# Captured live from simplex-chat v7 (`/_get chat @3`): the text is nested
# under content.msgContent; Contact.displayName is null, the peer name is in
# profile.displayName, and a collision-suffixed localDisplayName must not
# defeat the allowlist.
adapter = SimplexAdapter({"allowed_users": ["pepper"]})
item = {
"chatInfo": {
"type": "direct",
"chatId": 6,
"contact": {
"contactId": 6,
"localDisplayName": "pepper_1",
"displayName": None,
"profile": {"displayName": "pepper"},
},
},
"chatItem": {
"chatDir": {"type": "directRcv"},
"content": {
"type": "rcvMsgContent",
"msgContent": {
"type": "text",
"text": "what is the next thing on my calendar?",
},
},
},
}
message = adapter._parse_chat_item(item)
assert message is not None
assert message.text == "what is the next thing on my calendar?"
assert message.contact_id == "6"
assert message.display_name == "pepper"
def test_parse_accepts_aeson_nested_content_shape():
# The daemon's DB/Aeson encoding nests under the constructor key instead of
# tagging with `type`; the parser tolerates both.
adapter = SimplexAdapter({"allow_all_users": True})
item = direct_item()
item["chatItem"]["content"] = {
"rcvMsgContent": {"msgContent": {"type": "text", "text": "hi"}}
}
message = adapter._parse_chat_item(item)
assert message is not None
assert message.text == "hi"
# ---- adapter: send command ----
def test_send_command_is_structured_not_shortcut():