Skip to content

feat: Per-user persistent memory for public link agents (multi-tenant) #147

Closed

Description

@vybe

Summary

Enable agents served via public links to maintain persistent, per-user memory across sessions. When a verified user sends a message, the platform retrieves their memory from the database, injects it into the agent's context at runtime, and after each conversation the system summarizes what was learned and writes it back. One agent, many users — each with their own private memory.

Motivation

Today, public chat sessions replay the last 10 conversation turns as raw context. This breaks for long conversations (context pressure), loses facts from early sessions, and has no memory across separate visits. With per-user memory, a public agent can truly personalize its behavior — remembering a user's name, preferences, goals, and history without any work from the agent author.

Design

Scope

  • Memory is scoped to (agent_name, user_email) — per agent, per user, cross-session
  • Email verification is required — no memory for anonymous sessions
  • Memory is a plain text blob (not vector DB, not structured JSON) — simplest viable start

New Table: public_user_memory

CREATE TABLE IF NOT EXISTS public_user_memory (
    id TEXT PRIMARY KEY,
    agent_name TEXT NOT NULL,
    user_email TEXT NOT NULL,
    memory_text TEXT NOT NULL DEFAULT '',
    message_count INTEGER DEFAULT 0,
    created_at TEXT NOT NULL,
    updated_at TEXT NOT NULL,
    UNIQUE(agent_name, user_email)
);
CREATE INDEX idx_public_user_memory_lookup ON public_user_memory(agent_name, user_email);

No changes to existing public_chat_sessions or public_chat_messages tables.

Request Flow (before agent receives message)

In POST /api/public/chat/{token} (src/backend/routers/public.py), after email verification:

  1. Look up public_user_memory for (agent_name, verified_email)
  2. If found and memory_text is non-empty, prepend a user context block to the system prompt:
## What you know about this user

{memory_text}

---
  1. Pass this as system_prompt into task_execution_service.execute_task() — it gets forwarded via --append-system-prompt, layered on top of the existing platform instructions

The context_prompt (conversation history) continues unchanged as the message parameter.

Memory Update Flow (after agent responds)

After execution completes (both sync and async paths), if identifier_type == "email":

  1. Increment message_count on the public_user_memory row (create it if first message)
  2. Every 5 messages, trigger a background summarization task:
    • Fetch the last 20 messages from public_chat_messages for this session
    • Call a lightweight LLM (e.g., claude-haiku or whatever model is configured) with a summarization prompt:
      You are a memory system. Given this conversation, extract a concise bullet list of facts
      about the user that would be useful to remember for future conversations.
      Be specific: name, preferences, goals, context. Max 300 words.
      Existing memory: {current_memory_text}
      New conversation: {last_20_messages}
      Output: updated memory text only.
      
    • Write the result back to public_user_memory.memory_text

This is fire-and-forget — the user's current request is never blocked by summarization.

No new endpoints required for v1

Memory is managed entirely by the platform. Admin visibility can be added later.

Files to Change

File Change
src/backend/db/schema.py Add public_user_memory table + index
src/backend/db/public_links.py Add get_or_create_user_memory(), update_user_memory(), increment_message_count()
src/backend/database.py Delegation methods for new DB ops
src/backend/routers/public.py Fetch memory + inject into system_prompt; trigger background summarization after response
src/backend/services/platform_prompt_service.py Helper to format user memory block

Acceptance Criteria

  • public_user_memory table created via migration in schema.py
  • First message from a verified user creates a memory row (empty memory_text)
  • Subsequent messages include existing memory_text in system_prompt via --append-system-prompt
  • After every 5th message, background summarization runs and updates memory_text
  • Anonymous sessions are unaffected (no memory, no change to existing flow)
  • No performance impact on the hot path (summarization is fire-and-forget)
  • Summarization failures are logged but do not surface as errors to the user

Out of Scope (follow-up)

  • Admin UI to view/edit/delete user memories
  • Per-link toggle to enable/disable memory
  • Cross-agent memory (memory shared across agents)
  • Vector/semantic memory — text blob is sufficient for v1
  • User-facing memory transparency (showing users what is remembered)

Related

  • src/backend/routers/public.py — public chat handler
  • src/backend/services/platform_prompt_service.py — --append-system-prompt injection
  • docs/memory/feature-flows/public-agent-links.md — feature flow
  • Requirements 15.1 (Public Agent Links), 15.1c (Session Memory spec)

Activity

  1. added
    priority-p1
    type-feature
    on Mar 19, 2026
  2. vybe commented on Mar 19, 2026

    @vybe
    ContributorAuthor

    Implemented MEM-001: Per-user persistent memory for public link agents.

    What was built:

    • New public_user_memory table: plain-text memory blob scoped to (agent_name, user_email)
    • Migration Failure Recovery for Plans #28 for existing databases
    • get_or_create_user_memory(), increment_message_count(), update_user_memory() in PublicLinkOperations
    • format_user_memory_block() helper in platform_prompt_service.py
    • Memory injection into system_prompt for all email-verified sessions (sync + async paths)
    • Fire-and-forget background summarization via _summarize_user_memory() every 5 messages (claude-haiku)
    • Unit tests in tests/test_public_user_memory.py

    All acceptance criteria met. Anonymous sessions unaffected.

  3. added a commit that references this issue on Mar 19, 2026
    1e67bdd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority-p1type-feature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions