agentleFS
lauren-frameworklauren-framework/lauren-ai/skills/agent-session-isolation/SKILL.md

agent-session-isolation

Skill0 starsChanged 6 months agoMITView on GitHub

Implements per-tenant and per-user session isolation for agent conversations using namespaced conversation IDs. Use when building multi-tenant applications where different users or tenants must have fully isolated conversation histories that cannot bleed into each other.

The file

We split this skill into the rules it gives an agent

1 rule. About 0 are common, 0 are distinctive, and 1 is very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈0

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 1

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    When you only need user isolation (not tenant isolation), use the user ID directly as the conversation_id:

    line 72

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Use codemap find "SymbolName" to locate any symbol before reading — it gives exact file + line range and is faster than grep across the whole repo.

Agent Session Isolation per User / Tenant

Overview

TenantIsolatedAgentRunner wraps AgentRunnerBase and prefixes every conversation_id with "{tenant_id}:{user_id}". Each tenant gets its own InMemoryConversationStore so histories cannot cross tenant boundaries.


Implementation

# isolation/tenant_runner.py
from __future__ import annotations
from lauren_ai import agent, ShortTermMemory
from lauren_ai._memory._stores import InMemoryConversationStore

class TenantIsolatedAgentRunner:
    """Wraps AgentRunner to namespace conversation IDs per tenant."""

    def __init__(self, runner, agent_instance):
        self._runner = runner
        self._agent = agent_instance
        self._stores: dict[str, InMemoryConversationStore] = {}

    def _get_store(self, tenant_id: str) -> InMemoryConversationStore:
        if tenant_id not in self._stores:
            self._stores[tenant_id] = InMemoryConversationStore()
        return self._stores[tenant_id]

    async def run(self, tenant_id: str, user_id: str, prompt: str):
        conversation_id = f"{tenant_id}:{user_id}"
        store = self._get_store(tenant_id)
        return await self._runner.run(
            self._agent,
            prompt,
            conversation_id=conversation_id,
            conversation_store=store,
        )

    def get_conversation_count(self, tenant_id: str) -> int:
        store = self._stores.get(tenant_id)
        if store is None:
            return 0
        return len(store)

Why this is safe

  • conversation_id = f"{tenant_id}:{user_id}" ensures that user123 in tenant_a and user123 in tenant_b have different keys.
  • Each tenant has its own InMemoryConversationStore dict — there is no shared mutable state.
  • Switching to a Redis-backed store in production only requires replacing InMemoryConversationStore with a RedisConversationStore.

Per-user isolation without multi-tenancy

When you only need user isolation (not tenant isolation), use the user ID directly as the conversation_id:

response = await runner.run(
    agent_instance,
    prompt,
    conversation_id=user_id,
    conversation_store=shared_store,
)

Each user's messages are stored under their own key in the same store.


Attaching to a Lauren controller

@controller("/chat")
class ChatController:
    def __init__(self, runner: AgentRunner) -> None:
        self._isolated = TenantIsolatedAgentRunner(runner, MyAgent())

    @post("/message")
    async def message(self, body: Json[ChatRequest], ctx: ExecutionContext) -> dict:
        tenant_id = ctx.request.state.tenant_id
        user_id   = ctx.request.state.user_id
        response = await self._isolated.run(tenant_id, user_id, body.text)
        return {"reply": response.content}

Reference files

FileContents
src/lauren_ai/_memory/_stores.pyInMemoryConversationStore
src/lauren_ai/_agents/_runner.pyrun(conversation_id=, conversation_store=)

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

Reviews can't be read right now.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.