Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.
fix: async sandbox db writes (#2351)
cthomas658594c4c891
diff --git a/letta/server/rest_api/routers/v1/sandbox_configs.py b/letta/server/rest_api/routers/v1/sandbox_configs.pyindex 505e08a3d..00681ea24 100644--- a/letta/server/rest_api/routers/v1/sandbox_configs.py+++ b/letta/server/rest_api/routers/v1/sandbox_configs.py@@ -22,36 +22,36 @@ logger = get_logger(__name__)@router.post("/", response_model=PydanticSandboxConfig)-def create_sandbox_config(+async def create_sandbox_config(config_create: SandboxConfigCreate,server: SyncServer = Depends(get_letta_server),actor_id: str = Depends(get_user_id),):- actor = server.user_manager.get_user_or_default(user_id=actor_id)+ actor = await server.user_manager.get_actor_or_default_async(actor_id=actor_id)- return server.sandbox_config_manager.create_or_update_sandbox_config(config_create, actor)+ return await server.sandbox_config_manager.create_or_update_sandbox_config_async(config_create, actor)@router.post("/e2b/default", response_model=PydanticSandboxConfig)-def create_default_e2b_sandbox_config(+async def create_default_e2b_sandbox_config(server: SyncServer = Depends(get_letta_server),actor_id: str = Depends(get_user_id),):- actor = server.user_manager.get_user_or_default(user_id=actor_id)- return server.sandbox_config_manager.get_or_create_default_sandbox_config(sandbox_type=SandboxType.E2B, actor=actor)+ actor = await server.user_manager.get_actor_or_default_async(actor_id=actor_id)+ return await server.sandbox_config_manager.get_or_create_default_sandbox_config_async(sandbox_type=SandboxType.E2B, actor=actor)@router.post("/local/default", response_model=PydanticSandboxConfig)-def create_default_local_sandbox_config(+async def create_default_local_sandbox_config(server: SyncServer = Depends(get_letta_server),actor_id: str = Depends(get_user_id),):- actor = server.user_manager.get_user_or_default(user_id=actor_id)- return server.sandbox_config_manager.get_or_create_default_sandbox_config(sandbox_type=SandboxType.LOCAL, actor=actor)+ actor = await server.user_manager.get_actor_or_default_async(actor_id=actor_id)+ return await server.sandbox_config_manager.get_or_create_default_sandbox_config_async(sandbox_type=SandboxType.LOCAL, actor=actor)@router.post("/local", response_model=PydanticSandboxConfig)-def create_custom_local_sandbox_config(+async def create_custom_local_sandbox_config(local_sandbox_config: LocalSandboxConfig,server: SyncServer = Depends(get_letta_server),actor_id: str = Depends(get_user_id),@@ -67,26 +67,26 @@ def create_custom_local_sandbox_config()# Retrieve the user (actor)- actor = server.user_manager.get_user_or_default(user_id=actor_id)+ actor = await server.user_manager.get_actor_or_default_async(actor_id=actor_id)# Wrap the LocalSandboxConfig into a SandboxConfigCreatesandbox_config_create = SandboxConfigCreate(config=local_sandbox_config)# Use the manager to create or update the sandbox config- sandbox_config = server.sandbox_config_manager.create_or_update_sandbox_config(sandbox_config_create, actor=actor)+ sandbox_config = await server.sandbox_config_manager.create_or_update_sandbox_config_async(sandbox_config_create, actor=actor)return sandbox_config@router.patch("/{sandbox_config_id}", response_model=PydanticSandboxConfig)-def update_sandbox_config(+async def update_sandbox_config(sandbox_config_id: str,config_update: SandboxConfigUpdate,server: SyncServer = Depends(get_letta_server),actor_id: str = Depends(get_user_id),):- actor = server.user_manager.get_user_or_default(user_id=actor_id)- return server.sandbox_config_manager.update_sandbox_config(sandbox_config_id, config_update, actor)+ actor = await server.user_manager.get_actor_or_default_async(actor_id=actor_id)+ return await server.sandbox_config_manager.update_sandbox_config_async(sandbox_config_id, config_update, actor)@router.delete("/{sandbox_config_id}", status_code=204)@@ -112,7 +112,7 @@ async def list_sandbox_configs(@router.post("/local/recreate-venv", response_model=PydanticSandboxConfig)-def force_recreate_local_sandbox_venv(+async def force_recreate_local_sandbox_venv(server: SyncServer = Depends(get_letta_server),actor_id: str = Depends(get_user_id),):@@ -120,10 +120,10 @@ def force_recreate_local_sandbox_venv(Forcefully recreate the virtual environment for the local sandbox.Deletes and recreates the venv, then reinstalls required dependencies."""- actor = server.user_manager.get_user_or_default(user_id=actor_id)+ actor = await server.user_manager.get_actor_or_default_async(actor_id=actor_id)# Retrieve the local sandbox config- sbx_config = server.sandbox_config_manager.get_or_create_default_sandbox_config(sandbox_type=SandboxType.LOCAL, actor=actor)+ sbx_config = await server.sandbox_config_manager.get_or_create_default_sandbox_config_async(sandbox_type=SandboxType.LOCAL, actor=actor)local_configs = sbx_config.get_local_config()sandbox_dir = os.path.expanduser(local_configs.sandbox_dir) # Expand tildediff --git a/letta/services/sandbox_config_manager.py b/letta/services/sandbox_config_manager.pyindex 84883b9cf..6e7a43bcd 100644--- a/letta/services/sandbox_config_manager.py+++ b/letta/services/sandbox_config_manager.py@@ -73,6 +73,60 @@ class SandboxConfigManager:db_sandbox.create(session, actor=actor)return db_sandbox.to_pydantic()+ @enforce_types+ @trace_method+ async def get_or_create_default_sandbox_config_async(self, sandbox_type: SandboxType, actor: PydanticUser) -> PydanticSandboxConfig:+ sandbox_config = await self.get_sandbox_config_by_type_async(sandbox_type, actor=actor)+ if not sandbox_config:+ logger.debug(f"Creating new sandbox config of type {sandbox_type}, none found for organization {actor.organization_id}.")++ # TODO: Add more sandbox types later+ if sandbox_type == SandboxType.E2B:+ default_config = {} # Empty+ else:+ # TODO: May want to move this to environment variables v.s. persisting in database+ default_local_sandbox_path = LETTA_TOOL_EXECUTION_DIR+ default_config = LocalSandboxConfig(sandbox_dir=default_local_sandbox_path).model_dump(exclude_none=True)++ sandbox_config = await self.create_or_update_sandbox_config_async(SandboxConfigCreate(config=default_config), actor=actor)+ return sandbox_config++ @enforce_types+ @trace_method+ async def create_or_update_sandbox_config_async(+ self, sandbox_config_create: SandboxConfigCreate, actor: PydanticUser+ ) -> PydanticSandboxConfig:+ """Create or update a sandbox configuration based on the PydanticSandboxConfig schema."""+ config = sandbox_config_create.config+ sandbox_type = config.type+ sandbox_config = PydanticSandboxConfig(+ type=sandbox_type, config=config.model_dump(exclude_none=True), organization_id=actor.organization_id+ )++ # Attempt to retrieve the existing sandbox configuration by type within the organization+ db_sandbox = await self.get_sandbox_config_by_type_async(sandbox_config.type, actor=actor)+ if db_sandbox:+ # Prepare the update data, excluding fields that should not be reset+ update_data = sandbox_config.model_dump(exclude_unset=True, exclude_none=True)+ update_data = {key: value for key, value in update_data.items() if getattr(db_sandbox, key) != value}++ # If there are changes, update the sandbox configuration+ if update_data:+ db_sandbox = await self.update_sandbox_config_async(db_sandbox.id, SandboxConfigUpdate(**update_data), actor)+ else:+ printd(+ f"`create_or_update_sandbox_config` was called with user_id={actor.id}, organization_id={actor.organization_id}, "+ f"type={sandbox_config.type}, but found existing configuration with nothing to update."+ )++ return db_sandbox+ else:+ # If the sandbox configuration doesn't exist, create a new one+ async with db_registry.async_session() as session:+ db_sandbox = SandboxConfigModel(**sandbox_config.model_dump(exclude_none=True))+ await db_sandbox.create_async(session, actor=actor)+ return db_sandbox.to_pydantic()+@enforce_types@trace_methoddef update_sandbox_config(@@ -101,6 +155,34 @@ class SandboxConfigManager:)return sandbox.to_pydantic()+ @enforce_types+ @trace_method+ async def update_sandbox_config_async(+ self, sandbox_config_id: str, sandbox_update: SandboxConfigUpdate, actor: PydanticUser+ ) -> PydanticSandboxConfig:+ """Update an existing sandbox configuration."""+ async with db_registry.async_session() as session:+ sandbox = await SandboxConfigModel.read_async(db_session=session, identifier=sandbox_config_id, actor=actor)+ # We need to check that the sandbox_update provided is the same type as the original sandbox+ if sandbox.type != sandbox_update.config.type:+ raise ValueError(+ f"Mismatched type for sandbox config update: tried to update sandbox_config of type {sandbox.type} with config of type {sandbox_update.config.type}"+ )++ update_data = sandbox_update.model_dump(exclude_unset=True, exclude_none=True)+ update_data = {key: value for key, value in update_data.items() if getattr(sandbox, key) != value}++ if update_data:+ for key, value in update_data.items():+ setattr(sandbox, key, value)+ await sandbox.update_async(db_session=session, actor=actor)+ else:+ printd(+ f"`update_sandbox_config` called with user_id={actor.id}, organization_id={actor.organization_id}, "+ f"name={sandbox.type}, but nothing to update."+ )+ return sandbox.to_pydantic()+@enforce_types@trace_methoddef delete_sandbox_config(self, sandbox_config_id: str, actor: PydanticUser) -> PydanticSandboxConfig:@@ -175,6 +257,26 @@ class SandboxConfigManager:except NoResultFound:return None+ @enforce_types+ @trace_method+ async def get_sandbox_config_by_type_async(+ self, type: SandboxType, actor: Optional[PydanticUser] = None+ ) -> Optional[PydanticSandboxConfig]:+ """Retrieve a sandbox config by its type."""+ async with db_registry.async_session() as session:+ try:+ sandboxes = await SandboxConfigModel.list_async(+ db_session=session,+ type=type,+ organization_id=actor.organization_id,+ limit=1,+ )+ if sandboxes:+ return sandboxes[0].to_pydantic()+ return None+ except NoResultFound:+ return None+@enforce_types@trace_methoddef create_sandbox_env_var(@@ -301,6 +403,17 @@ class SandboxConfigManager:result[env_var.key] = env_var.valuereturn result+ @enforce_types+ @trace_method+ async def get_sandbox_env_vars_as_dict_async(+ self, sandbox_config_id: str, actor: PydanticUser, after: Optional[str] = None, limit: Optional[int] = 50+ ) -> Dict[str, str]:+ env_vars = await self.list_sandbox_env_vars_async(sandbox_config_id, actor, after, limit)+ result = {}+ for env_var in env_vars:+ result[env_var.key] = env_var.value+ return result+@enforce_types@trace_methoddef get_sandbox_env_var_by_key_and_sandbox_config_id(diff --git a/letta/services/tool_sandbox/local_sandbox.py b/letta/services/tool_sandbox/local_sandbox.pyindex a17815961..276409516 100644--- a/letta/services/tool_sandbox/local_sandbox.py+++ b/letta/services/tool_sandbox/local_sandbox.py@@ -60,14 +60,16 @@ class AsyncToolSandboxLocal(AsyncToolSandboxBase):additional_env_vars: Optional[Dict],) -> ToolExecutionResult:"""- Unified asynchronougit pus method to run the tool in a local sandbox environment,+ Unified asynchronous method to run the tool in a local sandbox environment,always via subprocess for multi-core parallelism."""# Get sandbox configurationif self.provided_sandbox_config:sbx_config = self.provided_sandbox_configelse:- sbx_config = self.sandbox_config_manager.get_or_create_default_sandbox_config(sandbox_type=SandboxType.LOCAL, actor=self.user)+ sbx_config = await self.sandbox_config_manager.get_or_create_default_sandbox_config_async(+ sandbox_type=SandboxType.LOCAL, actor=self.user+ )local_configs = sbx_config.get_local_config()use_venv = local_configs.use_venv@@ -76,7 +78,9 @@ class AsyncToolSandboxLocal(AsyncToolSandboxBase):if self.provided_sandbox_env_vars:env.update(self.provided_sandbox_env_vars)else:- env_vars = self.sandbox_config_manager.get_sandbox_env_vars_as_dict(sandbox_config_id=sbx_config.id, actor=self.user, limit=100)+ env_vars = await self.sandbox_config_manager.get_sandbox_env_vars_as_dict_async(+ sandbox_config_id=sbx_config.id, actor=self.user, limit=100+ )env.update(env_vars)if agent_state:diff --git a/tests/integration_test_async_tool_sandbox.py b/tests/integration_test_async_tool_sandbox.pyindex eba7b84fb..e6d54207a 100644--- a/tests/integration_test_async_tool_sandbox.py+++ b/tests/integration_test_async_tool_sandbox.py@@ -1,3 +1,4 @@+import asyncioimport secretsimport stringimport uuid@@ -276,12 +277,20 @@ def core_memory_tools(test_user):yield tools[email protected](scope="session")+def event_loop(request):+ """Create an instance of the default event loop for each test case."""+ loop = asyncio.get_event_loop_policy().new_event_loop()+ yield loop+ loop.close()++# Local sandbox tests@pytest.mark.asyncio@pytest.mark.local_sandbox-async def test_local_sandbox_default(disable_e2b_api_key, add_integers_tool, test_user):+async def test_local_sandbox_default(disable_e2b_api_key, add_integers_tool, test_user, event_loop):args = {"x": 10, "y": 5}# Mock and assert correct pathway was invoked@@ -298,7 +307,7 @@ async def test_local_sandbox_default(disable_e2b_api_key, add_integers_tool, tes@pytest.mark.asyncio@pytest.mark.local_sandbox-async def test_local_sandbox_stateful_tool(disable_e2b_api_key, clear_core_memory_tool, test_user, agent_state):+async def test_local_sandbox_stateful_tool(disable_e2b_api_key, clear_core_memory_tool, test_user, agent_state, event_loop):args = {}sandbox = AsyncToolSandboxLocal(clear_core_memory_tool.name, args, user=test_user)result = await sandbox.run(agent_state=agent_state)@@ -310,7 +319,7 @@ async def test_local_sandbox_stateful_tool(disable_e2b_api_key, clear_core_memor@pytest.mark.asyncio@pytest.mark.local_sandbox-async def test_local_sandbox_with_list_rv(disable_e2b_api_key, list_tool, test_user):+async def test_local_sandbox_with_list_rv(disable_e2b_api_key, list_tool, test_user, event_loop):sandbox = AsyncToolSandboxLocal(list_tool.name, {}, user=test_user)result = await sandbox.run()assert len(result.func_return) == 5@@ -318,7 +327,7 @@ async def test_local_sandbox_with_list_rv(disable_e2b_api_key, list_tool, test_u@pytest.mark.asyncio@pytest.mark.local_sandbox-async def test_local_sandbox_env(disable_e2b_api_key, get_env_tool, test_user):+async def test_local_sandbox_env(disable_e2b_api_key, get_env_tool, test_user, event_loop):manager = SandboxConfigManager()sandbox_dir = str(Path(__file__).parent / "test_tool_sandbox")config_create = SandboxConfigCreate(config=LocalSandboxConfig(sandbox_dir=sandbox_dir).model_dump())@@ -337,7 +346,7 @@ async def test_local_sandbox_env(disable_e2b_api_key, get_env_tool, test_user):@pytest.mark.asyncio@pytest.mark.local_sandbox-async def test_local_sandbox_per_agent_env(disable_e2b_api_key, get_env_tool, agent_state, test_user):+async def test_local_sandbox_per_agent_env(disable_e2b_api_key, get_env_tool, agent_state, test_user, event_loop):manager = SandboxConfigManager()key = "secret_word"sandbox_dir = str(Path(__file__).parent / "test_tool_sandbox")@@ -359,7 +368,7 @@ async def test_local_sandbox_per_agent_env(disable_e2b_api_key, get_env_tool, ag@pytest.mark.asyncio@pytest.mark.local_sandboxasync def test_local_sandbox_external_codebase_with_venv(- disable_e2b_api_key, custom_test_sandbox_config, external_codebase_tool, test_user+ disable_e2b_api_key, custom_test_sandbox_config, external_codebase_tool, test_user, event_loop):args = {"percentage": 10}sandbox = AsyncToolSandboxLocal(external_codebase_tool.name, args, user=test_user)@@ -371,7 +380,7 @@ async def test_local_sandbox_external_codebase_with_venv(@pytest.mark.asyncio@pytest.mark.local_sandboxasync def test_local_sandbox_with_venv_and_warnings_does_not_error(- disable_e2b_api_key, custom_test_sandbox_config, get_warning_tool, test_user+ disable_e2b_api_key, custom_test_sandbox_config, get_warning_tool, test_user, event_loop):sandbox = AsyncToolSandboxLocal(get_warning_tool.name, {}, user=test_user)result = await sandbox.run()@@ -380,7 +389,7 @@ async def test_local_sandbox_with_venv_and_warnings_does_not_error(@pytest.mark.asyncio@pytest.mark.e2b_sandbox-async def test_local_sandbox_with_venv_errors(disable_e2b_api_key, custom_test_sandbox_config, always_err_tool, test_user):+async def test_local_sandbox_with_venv_errors(disable_e2b_api_key, custom_test_sandbox_config, always_err_tool, test_user, event_loop):sandbox = AsyncToolSandboxLocal(always_err_tool.name, {}, user=test_user)result = await sandbox.run()assert len(result.stdout) != 0@@ -391,7 +400,7 @@ async def test_local_sandbox_with_venv_errors(disable_e2b_api_key, custom_test_s@pytest.mark.asyncio@pytest.mark.e2b_sandbox-async def test_local_sandbox_with_venv_pip_installs_basic(disable_e2b_api_key, cowsay_tool, test_user):+async def test_local_sandbox_with_venv_pip_installs_basic(disable_e2b_api_key, cowsay_tool, test_user, event_loop):manager = SandboxConfigManager()config_create = SandboxConfigCreate(config=LocalSandboxConfig(use_venv=True, pip_requirements=[PipRequirement(name="cowsay")]).model_dump()@@ -411,7 +420,7 @@ async def test_local_sandbox_with_venv_pip_installs_basic(disable_e2b_api_key, c@pytest.mark.asyncio@pytest.mark.e2b_sandbox-async def test_local_sandbox_with_venv_pip_installs_with_update(disable_e2b_api_key, cowsay_tool, test_user):+async def test_local_sandbox_with_venv_pip_installs_with_update(disable_e2b_api_key, cowsay_tool, test_user, event_loop):manager = SandboxConfigManager()config_create = SandboxConfigCreate(config=LocalSandboxConfig(use_venv=True).model_dump())config = manager.create_or_update_sandbox_config(config_create, test_user)@@ -442,7 +451,7 @@ async def test_local_sandbox_with_venv_pip_installs_with_update(disable_e2b_api_@pytest.mark.asyncio@pytest.mark.e2b_sandbox-async def test_e2b_sandbox_default(check_e2b_key_is_set, add_integers_tool, test_user):+async def test_e2b_sandbox_default(check_e2b_key_is_set, add_integers_tool, test_user, event_loop):args = {"x": 10, "y": 5}# Mock and assert correct pathway was invoked@@ -459,7 +468,7 @@ async def test_e2b_sandbox_default(check_e2b_key_is_set, add_integers_tool, test@pytest.mark.asyncio@pytest.mark.e2b_sandbox-async def test_e2b_sandbox_pip_installs(check_e2b_key_is_set, cowsay_tool, test_user):+async def test_e2b_sandbox_pip_installs(check_e2b_key_is_set, cowsay_tool, test_user, event_loop):manager = SandboxConfigManager()config_create = SandboxConfigCreate(config=E2BSandboxConfig(pip_requirements=["cowsay"]).model_dump())config = manager.create_or_update_sandbox_config(config_create, test_user)@@ -479,7 +488,7 @@ async def test_e2b_sandbox_pip_installs(check_e2b_key_is_set, cowsay_tool, test_@pytest.mark.asyncio@pytest.mark.e2b_sandbox-async def test_e2b_sandbox_stateful_tool(check_e2b_key_is_set, clear_core_memory_tool, test_user, agent_state):+async def test_e2b_sandbox_stateful_tool(check_e2b_key_is_set, clear_core_memory_tool, test_user, agent_state, event_loop):sandbox = AsyncToolSandboxE2B(clear_core_memory_tool.name, {}, user=test_user)result = await sandbox.run(agent_state=agent_state)assert result.agent_state.memory.get_block("human").value == ""@@ -489,7 +498,7 @@ async def test_e2b_sandbox_stateful_tool(check_e2b_key_is_set, clear_core_memory@pytest.mark.asyncio@pytest.mark.e2b_sandbox-async def test_e2b_sandbox_inject_env_var_existing_sandbox(check_e2b_key_is_set, get_env_tool, test_user):+async def test_e2b_sandbox_inject_env_var_existing_sandbox(check_e2b_key_is_set, get_env_tool, test_user, event_loop):manager = SandboxConfigManager()config_create = SandboxConfigCreate(config=E2BSandboxConfig().model_dump())config = manager.create_or_update_sandbox_config(config_create, test_user)@@ -513,7 +522,7 @@ async def test_e2b_sandbox_inject_env_var_existing_sandbox(check_e2b_key_is_set,@pytest.mark.asyncio@pytest.mark.e2b_sandbox-async def test_e2b_sandbox_per_agent_env(check_e2b_key_is_set, get_env_tool, agent_state, test_user):+async def test_e2b_sandbox_per_agent_env(check_e2b_key_is_set, get_env_tool, agent_state, test_user, event_loop):manager = SandboxConfigManager()key = "secret_word"wrong_val = "".join(secrets.choice(string.ascii_letters + string.digits) for _ in range(20))@@ -537,7 +546,7 @@ async def test_e2b_sandbox_per_agent_env(check_e2b_key_is_set, get_env_tool, age@pytest.mark.asyncio@pytest.mark.e2b_sandbox-async def test_e2b_sandbox_with_list_rv(check_e2b_key_is_set, list_tool, test_user):+async def test_e2b_sandbox_with_list_rv(check_e2b_key_is_set, list_tool, test_user, event_loop):sandbox = AsyncToolSandboxE2B(list_tool.name, {}, user=test_user)result = await sandbox.run()assert len(result.func_return) == 5