Skip to content

bug: per-user memory (MEM-001) not injected for channel sessions (WhatsApp/Telegram/Slack) #895

Closed

Description

@vybe

Summary

Two distinct mechanisms write user-memory to the same single text blob on public_user_memory.memory_text, and they clobber each other:

  1. Agent-deliberate memory — write_user_memory MCP tool (security: Agent writes public-link user PII to shared agent memory, leaking it to all other users #888). The agent decides what's worth remembering and calls the tool. Written under the agent's own model.
  2. Conversation auto-summary — _summarize_user_memory (original MEM-001). Every 5 messages on the web public-chat path, Haiku is called to summarize recent conversation. Output is written via db.update_public_user_memory(...) as a full overwrite of the column.

Both write to the same column with no separation. Even with {existing_memory} passed into the summarizer prompt, the 300-word cap and the difference in models mean deliberate facts can get re-summarized away or dropped.

On top of that, the read path is only wired on web — channel sessions (WhatsApp/Telegram/Slack) never inject memory back into the agent's system prompt on subsequent invocations.

Component

Backend — MEM-001 storage + injection (web public chat + channel adapter pipeline)

Priority

P2 — feature impaired; cross-channel continuity (a stated goal of MEM-001 + #311) is broken on three channels, and the storage model has a conflict between the deliberate-write and auto-summary mechanisms.

What is observed

Bug 1 — Storage conflict

write_user_memory and _summarize_user_memory both call db.update_public_user_memory(agent_name, user_email, memory_text), which overwrites the entire column. The summarizer prompt at routers/public.py:963-974 passes {existing_memory} so Haiku has a chance to preserve deliberate writes — but with a 300-word cap, a different model, and no "do not summarize" tag, agent-deliberate facts can be re-interpreted, truncated, or dropped on the next 5-message cycle.

This is post-hoc — write_user_memory was added in #888 on top of an existing summarization-only design, and the two were never reconciled.

Bug 2 — Channel sessions don't inject memory

On a recent deployment (commit 96b6214e):

  1. A verified WhatsApp user sends a message; the agent calls mcp__trinity__write_user_memory to persist facts.
  2. The write succeeds — a row appears in public_user_memory keyed on (agent_name, verified_email).
  3. On the next message from the same verified email through the same channel, the agent reports no ## What you know about this user block in its context.
  4. The execution's stored message field contains only the channel context prompt — no memory block. Per Trinity's architecture, the memory block is delivered via --append-system-prompt (the system_prompt= kwarg on execute_task), not inside the user message.
  5. message_count on the memory row stays at 0 after several channel exchanges.

Where (code observations)

Memory IS injected on the web public-chat path:

  • src/backend/routers/public.py:563-568 (sync) and :946-954 (async background):
    • Imports format_user_memory_block from services.platform_prompt_service
    • Calls db.get_or_create_public_user_memory(agent_name, verified_email)
    • Passes the formatted block as system_prompt=memory_system_prompt to task_execution_service.execute_task(...)
  • Increments db.increment_public_user_memory_count(...) and fire-and-forgets _summarize_user_memory(...) every 5 messages.

Memory is NOT injected on the channel adapter path:

  • src/backend/adapters/message_router.py:_handle_message_inner:
    • Does not import format_user_memory_block.
    • Does not call get_or_create_public_user_memory or increment_public_user_memory_count.
    • The execute_task(...) call (~line 509) omits system_prompt= entirely.

Cross-check via grep:

$ grep -rn 'format_user_memory_block\|get_or_create_public_user_memory' src/backend/
src/backend/database.py:1278: def get_or_create_public_user_memory(...)
src/backend/routers/public.py:35: from services.platform_prompt_service import format_user_memory_block
src/backend/routers/public.py:566: user_memory = db.get_or_create_public_user_memory(...)
src/backend/routers/public.py:568: memory_system_prompt = format_user_memory_block(...)
src/backend/routers/public.py:990: memory_record = db.get_or_create_public_user_memory(...)
src/backend/routers/public_memory.py:86: db.get_or_create_public_user_memory(...)
src/backend/services/platform_prompt_service.py:142: def format_user_memory_block(...)

No hits in adapters/message_router.py or any channel transport.

Reproduction steps (channel injection bug)

  1. Configure a public-link agent and verify a user email through a non-web channel (WhatsApp, Telegram, or Slack).
  2. From the verified user, send a message that triggers the agent to call mcp__trinity__write_user_memory.
  3. Verify via DB: a row exists in public_user_memory for (agent_name, verified_email) with non-empty memory_text.
  4. Send a follow-up message from the same verified user through the same channel.
  5. Inspect the new execution's system prompt — the memory block is absent.
  6. Compare against the web public-link path against the same agent: the block is present.

Proposed Fix

Split storage into two named keys inside the existing memory_text TEXT column so the two mechanisms coexist without clobbering each other. No schema migration — same column, structured contents.

Storage shape

public_user_memory.memory_text becomes a small JSON blob:

{
  "agent_notes": "<written only by write_user_memory>",
  "conversation_summary": "<written only by _summarize_user_memory>"
}

Each mechanism reads → updates only its own key → writes back. No cross-write.

No legacy support needed — the feature has no production users. Existing rows can be wiped or treated as empty on first read.

Write paths

  • write_user_memory (in routers/public_memory.py) — writes agent_notes only. Reads existing JSON, replaces the agent_notes key, writes back.
  • _summarize_user_memory (existing summarizer) — writes conversation_summary only. Reads existing JSON, replaces the conversation_summary key, writes back. The "existing memory" passed into the summarization prompt should be the existing conversation_summary value, NOT the agent_notes (so the summarizer can't reinterpret deliberate writes).

Read path

  • format_user_memory_block(memory_record) in services/platform_prompt_service.py formats BOTH sections into one system-prompt block when present:

    ## What you know about this user
    
    ### Agent notes
    <agent_notes content>
    
    ### Conversation summary
    <conversation_summary content>
    
    ---
    

    Sections are omitted when empty.

Channel injection

  • In adapters/message_router.py:_handle_message_inner, mirror the web pattern, gated on verified_email and not is_group:
    • Before execute_task: fetch memory row, format the block (both sections), pass as system_prompt=.
    • After adapter.send_response: increment count; if count % 5 == 0, fire-and-forget the summarizer.
  • is_group is excluded because in any_verified group mode verified_email represents the user who unlocked the group, not the current speaker — injecting their memory into group replies would leak PII.

Touch points

  • src/backend/db/public_links.py — new helpers: read returns a dict {agent_notes, conversation_summary}; write helpers update only the targeted key.
  • src/backend/services/platform_prompt_service.py — format_user_memory_block rewritten to accept the dict and emit a multi-section block; extract summarize_user_memory_background here from routers/public.py.
  • src/backend/routers/public.py — import the extracted summarizer; the summarizer now writes only to conversation_summary.
  • src/backend/routers/public_memory.py — write_user_memory writes only to agent_notes.
  • src/backend/adapters/message_router.py — inject + increment + summarize on the channel path, gated on verified_email and not is_group.
  • tests/ — split storage, channel injection, group-chat exclusion, anonymous exclusion, summarizer doesn't touch agent_notes.

Out of scope (follow-ups)

  • Summarizer cost ceiling / per-(agent, user) cooldown — pre-existing concern with MEM-001 generally, applies to both web and channels.
  • Atomic count % 5 == 0 check — current UPDATE … SET count = count + 1 then SELECT is racy; same on web today.
  • Memory-text safety filter — the system-prompt block is rendered raw, leaving a prompt-injection surface from agent-curated content. Pre-existing, not specific to this fix.

Related

Activity

  1. vybe commented on May 18, 2026

    @vybe
    ContributorAuthor

    /claim

  2. added
    status-in-progress
    on May 18, 2026
  3. github-actions commented on May 18, 2026

    @github-actions

    Claimed by @vybe. Label status-in-progress added.

    Next steps:

    1. Create branch: feature/895-<short-slug>
    2. Implement the feature
    3. Open a PR with Fixes #895
  4. vybe commented on May 18, 2026

    @vybe
    ContributorAuthor

    /claim

  5. github-actions commented on May 18, 2026

    @github-actions

    @vybe This issue is already assigned to vybe. Use /unclaim first if you want to take over.

  6. added
    status-in-dev
    and removed
    status-in-progress
    on May 18, 2026
  7. vybe commented on May 18, 2026

    @vybe
    ContributorAuthor

    Fixed by #896 (merged 9c20dc7). Storage split + channel injection landed; manual Slack/TG/WA DM smoke verification still recommended post-deploy.

  8. added a commit that references this issue on Jun 23, 2026
  9. removed
    status-in-dev
    on Aug 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

priority-p2type-bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions