Repository navigation
Description
Summary
Two distinct mechanisms write user-memory to the same single text blob on public_user_memory.memory_text, and they clobber each other:
- Agent-deliberate memory —
write_user_memoryMCP 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. - 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 viadb.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):
- A verified WhatsApp user sends a message; the agent calls
mcp__trinity__write_user_memoryto persist facts. - The write succeeds — a row appears in
public_user_memorykeyed on(agent_name, verified_email). - On the next message from the same verified email through the same channel, the agent reports no
## What you know about this userblock in its context. - The execution's stored
messagefield contains only the channel context prompt — no memory block. Per Trinity's architecture, the memory block is delivered via--append-system-prompt(thesystem_prompt=kwarg onexecute_task), not inside the user message. message_counton the memory row stays at0after 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_blockfromservices.platform_prompt_service - Calls
db.get_or_create_public_user_memory(agent_name, verified_email) - Passes the formatted block as
system_prompt=memory_system_prompttotask_execution_service.execute_task(...)
- Imports
- 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_memoryorincrement_public_user_memory_count. - The
execute_task(...)call (~line 509) omitssystem_prompt=entirely.
- Does not import
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)
- Configure a public-link agent and verify a user email through a non-web channel (WhatsApp, Telegram, or Slack).
- From the verified user, send a message that triggers the agent to call
mcp__trinity__write_user_memory. - Verify via DB: a row exists in
public_user_memoryfor(agent_name, verified_email)with non-emptymemory_text. - Send a follow-up message from the same verified user through the same channel.
- Inspect the new execution's system prompt — the memory block is absent.
- 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(inrouters/public_memory.py) — writesagent_notesonly. Reads existing JSON, replaces theagent_noteskey, writes back._summarize_user_memory(existing summarizer) — writesconversation_summaryonly. Reads existing JSON, replaces theconversation_summarykey, writes back. The "existing memory" passed into the summarization prompt should be the existingconversation_summaryvalue, NOT the agent_notes (so the summarizer can't reinterpret deliberate writes).
Read path
-
format_user_memory_block(memory_record)inservices/platform_prompt_service.pyformats 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 onverified_email and not is_group:- Before
execute_task: fetch memory row, format the block (both sections), pass assystem_prompt=. - After
adapter.send_response: increment count; if count % 5 == 0, fire-and-forget the summarizer.
- Before
is_groupis excluded because inany_verifiedgroup modeverified_emailrepresents 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_blockrewritten to accept the dict and emit a multi-section block; extractsummarize_user_memory_backgroundhere fromrouters/public.py.src/backend/routers/public.py— import the extracted summarizer; the summarizer now writes only toconversation_summary.src/backend/routers/public_memory.py—write_user_memorywrites only toagent_notes.src/backend/adapters/message_router.py— inject + increment + summarize on the channel path, gated onverified_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 == 0check — currentUPDATE … SET count = count + 1then 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
- MEM-001 commit
1e67bdd75f271125cbd81c2b803748624c6b4893— "feat: Per-user persistent memory for public link agents (MEM-001)" - security: Agent writes public-link user PII to shared agent memory, leaking it to all other users #888 — added
write_user_memoryMCP tool - feat: Unified channel access control — verified-email identity, per-agent allow-list, access requests (web / Telegram / Slack) #311 — unified cross-channel access control, verified-email plumbing into channel router
src/backend/routers/public_memory.py—_USER_FACING_TRIGGERSwhitelist already includespublic, slack, telegram, whatsapp, so channel writes were always intended to work.
Activity
vybe commentedon May 18, 2026
/claim
github-actions commentedon May 18, 2026
Claimed by @vybe. Label
status-in-progressadded.Next steps:
feature/895-<short-slug>Fixes #895vybe commentedon May 18, 2026
/claim
github-actions commentedon May 18, 2026
@vybe This issue is already assigned to vybe. Use
/unclaimfirst if you want to take over.fix(mem-001): split storage + channel injection for per-user memory (#…
vybe commentedon May 18, 2026
Fixed by #896 (merged 9c20dc7). Storage split + channel injection landed; manual Slack/TG/WA DM smoke verification still recommended post-deploy.
Release: v0.7.0 (#1319)