Search⌘ K
AI Features

Server-Side Tenant Isolation, Versioning, and Memory Rollback

Explore how server-side sessions create secure team and user namespaces for AI agent memory. Learn to prevent cross-tenant vector recall leaks, preserve version history, roll back incorrect facts, expire unverified entries, and test isolation guarantees.

We'll cover the following...

In the last lesson, we built a provenance gate that decides whether a source may write to memory at all. Our memory still has two gaps. It’s one set of files for everyone, and the assistant now serves more than one team. And a write that passes the gate is saved in place, so if a trusted source sends a wrong value, the old value is gone.

In this lesson, we’ll give each team and each engineer their own namespace: a storage location chosen by the server from the login session. We’ll reproduce a cross-team leak in vector recall and close it. Then we’ll keep every version of every fact, let unverified entries expire, and roll a bad fact back to a good version.

Two teams, one memory file

Two teams now share the assistant: checkout and payments. Each team tells the assistant where its escalations go. Both statements are user turns, so the provenance gate would pass both. Let’s send them through reconcile() from lesson 5.5.

collision.py · Python 3.14.0
import shutil
from memory_store import MEMORY_DIR, view
from sessions import reconcile
TEAM_STATEMENTS = [("checkout", "escalation channel", "#checkout-oncall", "Escalate checkout incidents in #checkout-oncall"),
("payments", "escalation channel", "#payments-oncall", "Our escalations go to #payments-oncall")]
if __name__ == "__main__":
shutil.rmtree(MEMORY_DIR, ignore_errors=True)
for team, subject, value, statement in TEAM_STATEMENTS: # both are user turns, so both pass the gate
print(f"{team:<8} | {reconcile('facts', subject, value, statement, False)}")
print("facts.json:", view("facts"))

In the code above:

  • Lines 5–6: We write one statement from each team. Both use the same subject, “escalation channel.”

  • Lines 9–11: We clear memory and reconcile both statements.

  • Line 12: We print the memory file.

The second team’s statement overwrites the first:

checkout | ADD
payments | UPDATE
facts.json: {'escalation_channel': '#payments-oncall'}
Payments’ statement replaced checkout’s, so the next checkout session escalates in the wrong channel

reconcile() did what it was built for. It saw a new value for an existing key and chose UPDATE. The issue is that both teams write to the same file. The next time a checkout engineer asks where to escalate, the assistant names the payments channel.

Give each team a namespace

The fix is to build the storage path from who is asking. Team facts go in a folder for the team, and personal rules, such as an engineer’s preferred answer format, go in a folder for that engineer.

The team and user must come from the server’s login session. If the path used a team name sent in the request, anyone could write to another team’s memory by sending that team’s name. This is the same lesson as the source tag in lesson 7.1: our code sets the fields that decide access, and the request never does.

The server builds every memory path from the login session, and a team name in the request is ignored
The server builds every memory path from the login session, and a team name in the request is ignored

Let’s build the namespace and a ...

tenant_store.py · Python 3.14.0
import os
import re
ROOT = "tenant_memories"
SCOPE = {"facts": "team", "rules": "user"} # team facts are shared by a team; rules belong to one engineer
SESSIONS = {"tok-1": {"team": "checkout", "user": "eng-042"}, # filled in by the login service
"tok-2": {"team": "checkout", "user": "eng-007"},
"tok-3": {"team": "payments", "user": "eng-113"}}
SAFE_ID = re.compile(r"[a-z0-9-]+")
def session_for(token):
return SESSIONS[token] # an unknown token raises KeyError, so the request stops here
def namespace_path(session, name):
team, user = session["team"], session["user"]
if not (SAFE_ID.fullmatch(team) and SAFE_ID.fullmatch(user)):
raise ValueError(f"unsafe id in session: {session}")
parts = [ROOT, team] + (["users", user] if SCOPE[name] == "user" else [])
return os.path.join(*parts, f"{name}.json")
...