Repository navigation
Description
Summary
When an agent sends a proactive message to a user via the MCP send_message tool (#321) — e.g. over Telegram — the delivered message is never persisted to the channel session history (public_chat_messages). The next inbound message from that user builds its conversation context from db.build_public_chat_context(session_id, ...), which contains only turns persisted by the channel message router — so the agent has no record of the proactive message it just sent. In the ensuing exchange the agent is unaware of its own outreach: it can repeat itself, contradict the earlier message, or fail to understand the user's reply ("yes, sounds good" — to what?).
Reported against Telegram, but the same gap applies to all proactive delivery channels: none of the _deliver_* helpers in src/backend/services/proactive_message_service.py write history.
Context
- Inbound flow (
src/backend/adapters/message_router.py): step 11 persists BOTH the user turn and the assistant reply viadb.add_public_chat_message(...)with per-speaker attribution (Slack: scope chat session to thread, not channel #903), and the context prompt for each turn is built from that history viadb.build_public_chat_context(session_id, ...). - Proactive flow (
src/backend/services/proactive_message_service.py, feat: proactive agent messaging to channel users (Telegram / Slack / web) #321):_deliver_telegram/_deliver_slack/_deliver_whatsappresolve the recipient and call the channel API directly — noadd_public_chat_messagecall exists anywhere in the service (_deliver_webis unimplemented v1). - The persistence seam already exists for Telegram and WhatsApp: both delivery helpers look up the chat link, and
telegram_chat_links.session_id/whatsapp_chat_links.session_ididentify the recipient's channel session. The Slack path resolves the recipient by email across workspaces and has no chat-link/session mapping today.
Acceptance Criteria
- A successful proactive Telegram delivery appends an assistant-role message to the recipient's channel session history (
sender_label= agent name, mirroring router step 11 attribution semantics, Slack: scope chat session to thread, not channel #903) - The next inbound turn's
build_public_chat_contextincludes the proactive message, so the agent responds aware of what it sent (regression test: proactive send → inbound reply → context contains the proactive message) - Same behavior for WhatsApp (chat link already carries
session_id) - Slack: either persisted the same way (resolving/creating a session for the DM) or explicitly documented as a known limitation in the
send_messagetool description + user docs — no silent inconsistency - Persist only on confirmed successful delivery — a failed send must not write a phantom assistant turn into history
- Recipient with no session yet (NULL
session_idon the chat link) degrades gracefully: message still delivered, no crash; decide and document whether a session is created - MEM-001 interplay: the persisted assistant turn's
sender_emailfollows the_assistant_sender_emailsemantics (Slack: scope chat session to thread, not channel #903) so sender-filtered summarization doesn't fold proactive messages into the wrong user's memory
Technical Notes
- Likely fix point: after each successful
_deliver_*inproactive_message_service.py, calldb.add_public_chat_message(session_id, "assistant", text, sender_email=..., sender_label=agent_name)using the chat link'ssession_id. - Check whether
send_group_message(MCPchannels.ts, fix(telegram): group chat user identity, observation mode, and proactive messaging #349/fix(slack): channel user identity, observation mode, and proactive messaging #350) has the same gap for proactive group/channel sends — if so, fix in the same pass or file a follow-up. - Out of scope: approval notifications (Notify requester on approval of channel access request #951), TTS voice replies (epic Horizontal Agent Scalability #24).
Activity
test(channels): use monkeypatch.delitem for the fresh-import stub (#1600
test(channels): cover the identifier the service actually derives (#1600
test(channels): stop poisoning test_1609 — patch bound names, don't r…
fix(channels): persist proactive messages to channel session history (#…
Merge origin/dev into fix/1649-group-message-history
fix(channels): persist proactive group messages to channel session hi…
Merge origin/dev into fix/1615-remove-password-ssh
Merge dev into feature/157-a2a-inbound-server (registry union)