feat: per-request toolkit isolation for concurrent multi-user requests#7377
Open
Mustafa-Esoofally wants to merge 1 commit intofeat/google-auth-db-storagefrom
Open
feat: per-request toolkit isolation for concurrent multi-user requests#7377Mustafa-Esoofally wants to merge 1 commit intofeat/google-auth-db-storagefrom
Mustafa-Esoofally wants to merge 1 commit intofeat/google-auth-db-storagefrom
Conversation
0f8d56e to
1fe64da
Compare
Add clone-based isolation so each Slack/Telegram/WhatsApp request gets fresh toolkit state (creds, service, caches) while sharing heavy resources (DB, GoogleAuth, MCP connections) by reference. Changes: - Toolkit: add clone() with _clone_reset_attrs for per-user state reset - Function: add clone_for() to rebind entrypoints to cloned toolkit - deep_copy_field: share GoogleAuth by reference (like MCP tools), fall back to clone() when deepcopy fails (Google httplib2 clients) - Interface routers: call entity.deep_copy() before arun() so each request runs on an isolated agent copy - AgentOS: wire toolkit._db at init time (_initialize_agents) so the OAuth callback closure has DB access before any request arrives - Cookbook: Slack Gmail agent with DB-backed OAuth flow Depends on: feat/google-auth-db-storage
1fe64da to
5eaf013
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Add clone-based isolation so each interface request (Slack, Telegram, WhatsApp) gets fresh toolkit state while sharing heavy resources by reference.
Depends on: #7376
Problem
When multiple users message a Slack bot concurrently, they share the same agent instance. Google toolkits hold mutable state (
creds,service,_label_cache) that can leak between users — User A's Gmail token could be used for User B's request.Solution
Each request clones the agent via
entity.deep_copy()before callingarun(). Per-user state is reset; shared resources (DB, GoogleAuth, MCP connections) are kept by reference.Init-time DB wiring
_initialize_agents()wirestoolkit._db = agent.dbat app startup (not runtime). This ensures the OAuth callback closure has DB access before any request arrives — noapp.state.dbneeded.Files changed (10 files, +382 -21)
toolkit.py(clone + _clone_reset_attrs),function.py(clone_for)agent/_utils.py,team/_utils.py(share GoogleAuth by ref)slack/router.py,telegram/router.py,whatsapp/router.pyos/app.py(init-time _db binding)slack/gmail_agent.pytest_toolkit_isolation.pyType of change
Checklist
test_toolkit_isolation.py)Duplicate and AI-Generated PR Check
Additional Notes
deep_copy()