learning-it by example

#247 Enterprise architecture and AI agents 5.6 min

Multi-Tenant AI Agent Platform: Four Leaks and How to Test

I gave every tenant its own database, and one tenant's data still leaked.

1. In the multi-tenant agent platform demo, what leaked even when every tenant had its own database?
2. When the demo asked as the tenant acme, how many of the four mistakes leaked another tenant's notes with filters written by habit, with one scoped layer, and with a database for each tenant?
3. In a scoped data layer for a multi-tenant agent platform, where should the tenant for a read come from?
4. In the scoped layer design, how is the cache key built so that one tenant cannot be served another tenant's cached answer?
5. What weakness does a scoped data layer that binds the tenant for every read still have?

Practice only: nothing is saved and no score counts towards anything. Each answer comes with the reason.

  1. Predict the leaks, then run

    Copy the code folder first. Before you run anything, write down how many of the four mistakes you expect to leak for each of the three designs, and which mistake you expect a database for each tenant to still leak. Then run python compare.py and compare. You should see 4 of 4, 0 of 4 and 1 of 4.

    A hint

    Read the DatabasePerTenant class in tenants.py and look at what it still shares.

  2. Break the scoped layer's cache

    In tenants.py, find cached_list in the ScopedLayer class. Change the line key = ("list", session) to key = "list" so the cache key is only the question. Predict which design results change, then run python compare.py. The one scoped layer line should now show 1 of 4, leaked through the cache. Put the line back afterwards.

    A hint

    Another tenant calls cached_list first in compare.py, and its answer is stored under the key.

  3. Fix the cache in the per-tenant database design

    In the DatabasePerTenant class, change cached_list so that its cache key includes the session, for example ("list", session), in all three places where the key is used. Run python compare.py. The database for each tenant line should now show 0 of 4 mistakes leaked. Put the three lines back afterwards.

    A hint

    The three places are the if test, the line that stores the result, and the return line.

  4. Add a fifth leak scenario

    In compare.py, add one more scenario line after the search index one: yield "another tenant name", leaked(design.search("acme", "initech", "")). Predict which designs leak it, then run python compare.py. You should see 5 of 5, 0 of 5 and 1 of 5.

    A hint

    The scoped layer ignores the tenant argument, and the per-tenant database cannot see other tenants.

These tasks are for you to try on your own machine. Nothing is checked or saved.

What this does not show

Three made-up tenants and four mistakes I chose, in SQLite; no agent or model is run, the session is assumed trusted, the index is a plain query, and the script prints leaks but does not fail a build.

Up next in Enterprise architecture and AI agents

More in Enterprise architecture and AI agents

All Enterprise architecture and AI agents videos

Related videos