Skip to content

armgit / letta

Public Imported from GitHub
0

Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.

feat: asyncify test_manager.py [LET-4494] (#4890)

Sarah Wooders53786ee102b1
diff --git a/.github/workflows/core-unit-sqlite-test.yaml b/.github/workflows/core-unit-sqlite-test.yaml
index d78da6f07..128feb83f 100644
--- a/.github/workflows/core-unit-sqlite-test.yaml
+++ b/.github/workflows/core-unit-sqlite-test.yaml
@@ -46,7 +46,7 @@ jobs:
{"test_suite": "test_letta_agent_batch.py"},
{"test_suite": "test_providers.py"},
{"test_suite": "test_sources.py"},
- {"test_suite": "test_managers.py"},
+ {"test_suite": "managers/"},
{"test_suite": "sdk/"},
{"test_suite": "mcp_tests/"},
{"test_suite": "test_timezone_formatting.py"},
diff --git a/.github/workflows/core-unit-test.yml b/.github/workflows/core-unit-test.yml
index 1c30d1930..81c75b8ef 100644
--- a/.github/workflows/core-unit-test.yml
+++ b/.github/workflows/core-unit-test.yml
@@ -36,7 +36,7 @@ jobs:
"include": [
{"test_suite": "test_client.py"},
{"test_suite": "test_sdk_client.py"},
- {"test_suite": "test_managers.py"},
+ {"test_suite": "managers/"},
{"test_suite": "test_tool_schema_parsing.py"},
{"test_suite": "test_tool_rule_solver.py"},
{"test_suite": "test_memory.py"},
diff --git a/letta/server/rest_api/routers/v1/runs.py b/letta/server/rest_api/routers/v1/runs.py
index a2a2464da..8e7f9d439 100644
--- a/letta/server/rest_api/routers/v1/runs.py
+++ b/letta/server/rest_api/routers/v1/runs.py
@@ -201,12 +201,7 @@ def retrieve_run_usage(
Get usage statistics for a run.
"""
actor = server.user_manager.get_user_or_default(user_id=headers.actor_id)
-
- try:
- usage = server.job_manager.get_job_usage(job_id=run_id, actor=actor)
- return usage
- except NoResultFound:
- raise HTTPException(status_code=404, detail=f"Run '{run_id}' not found")
+ raise Exception("Not implemented")
@router.get(
diff --git a/letta/server/server.py b/letta/server/server.py
index 4f30704df..dbb2dd59d 100644
--- a/letta/server/server.py
+++ b/letta/server/server.py
@@ -115,65 +115,7 @@ config = LettaConfig.load()
logger = get_logger(__name__)
-class Server(object):
- """Abstract server class that supports multi-agent multi-user"""
-
- @abstractmethod
- def list_agents(self, user_id: str) -> dict:
- """List all available agents to a user"""
- raise NotImplementedError
-
- @abstractmethod
- def get_agent_memory(self, user_id: str, agent_id: str) -> dict:
- """Return the memory of an agent (core memory + non-core statistics)"""
- raise NotImplementedError
-
- @abstractmethod
- def get_server_config(self, user_id: str) -> dict:
- """Return the base config"""
- raise NotImplementedError
-
- @abstractmethod
- def update_agent_core_memory(self, user_id: str, agent_id: str, label: str, actor: User) -> Memory:
- """Update the agents core memory block, return the new state"""
- raise NotImplementedError
-
- @abstractmethod
- def create_agent(
- self,
- request: CreateAgent,
- actor: User,
- # interface
- interface: Union[AgentInterface, None] = None,
- ) -> AgentState:
- """Create a new agent using a config"""
- raise NotImplementedError
-
- @abstractmethod
- def user_message(self, user_id: str, agent_id: str, message: str) -> None:
- """Process a message from the user, internally calls step"""
- raise NotImplementedError
-
- @abstractmethod
- def system_message(self, user_id: str, agent_id: str, message: str) -> None:
- """Process a message from the system, internally calls step"""
- raise NotImplementedError
-
- @abstractmethod
- def send_messages(self, user_id: str, agent_id: str, input_messages: List[MessageCreate]) -> None:
- """Send a list of messages to the agent"""
- raise NotImplementedError
-
- @abstractmethod
- def run_command(self, user_id: str, agent_id: str, command: str) -> Union[str, None]:
- """Run a command on the agent, e.g. /memory
-
- May return a string with a message generated by the command
- """
- raise NotImplementedError
-
-
-class SyncServer(Server):
+class SyncServer(object):
"""Simple single-threaded / blocking server process"""
def __init__(
diff --git a/letta/services/block_manager.py b/letta/services/block_manager.py
index 3717d1cdb..bf757bb41 100644
--- a/letta/services/block_manager.py
+++ b/letta/services/block_manager.py
@@ -3,7 +3,7 @@ from datetime import datetime
from typing import Dict, List, Optional
from sqlalchemy import and_, delete, func, or_, select
-from sqlalchemy.orm import Session
+from sqlalchemy.ext.asyncio import AsyncSession
from letta.log import get_logger
from letta.orm.agent import Agent as AgentModel
@@ -446,7 +446,7 @@ class BlockManager:
# Block History Functions
@enforce_types
- def _move_block_to_sequence(self, session: Session, block: BlockModel, target_seq: int, actor: PydanticUser) -> BlockModel:
+ async def _move_block_to_sequence(self, session: AsyncSession, block: BlockModel, target_seq: int, actor: PydanticUser) -> BlockModel:
"""
Internal helper that moves the 'block' to the specified 'target_seq' within BlockHistory.
1) Find the BlockHistory row at sequence_number=target_seq
@@ -459,14 +459,12 @@ class BlockManager:
if not block.id:
raise ValueError("Block is missing an ID. Cannot move sequence.")
- target_entry = (
- session.query(BlockHistory)
- .filter(
- BlockHistory.block_id == block.id,
- BlockHistory.sequence_number == target_seq,
- )
- .one_or_none()
+ stmt = select(BlockHistory).filter(
+ BlockHistory.block_id == block.id,
+ BlockHistory.sequence_number == target_seq,
)
+ result = await session.execute(stmt)
+ target_entry = result.scalar_one_or_none()
if not target_entry:
raise NoResultFound(f"No BlockHistory row found for block_id={block.id} at sequence={target_seq}")
@@ -480,7 +478,7 @@ class BlockManager:
# Update in DB (optimistic locking).
# We'll do a flush now; the caller does final commit.
- updated_block = block.update(db_session=session, actor=actor, no_commit=True)
+ updated_block = await block.update_async(db_session=session, actor=actor, no_commit=True)
return updated_block
@enforce_types
@@ -527,3 +525,201 @@ class BlockManager:
pass
return None
+
+ @enforce_types
+ @trace_method
+ async def checkpoint_block_async(
+ self,
+ block_id: str,
+ actor: PydanticUser,
+ agent_id: Optional[str] = None,
+ use_preloaded_block: Optional[BlockModel] = None, # For concurrency tests
+ ) -> PydanticBlock:
+ """
+ Create a new checkpoint for the given Block by copying its
+ current state into BlockHistory, using SQLAlchemy's built-in
+ version_id_col for concurrency checks.
+
+ - If the block was undone to an earlier checkpoint, we remove
+ any "future" checkpoints beyond the current state to keep a
+ strictly linear history.
+ - A single commit at the end ensures atomicity.
+ """
+ async with db_registry.async_session() as session:
+ # 1) Load the Block
+ if use_preloaded_block is not None:
+ block = await session.merge(use_preloaded_block)
+ else:
+ block = await BlockModel.read_async(db_session=session, identifier=block_id, actor=actor)
+
+ # 2) Identify the block's current checkpoint (if any)
+ current_entry = None
+ if block.current_history_entry_id:
+ current_entry = await session.get(BlockHistory, block.current_history_entry_id)
+
+ # The current sequence, or 0 if no checkpoints exist
+ current_seq = current_entry.sequence_number if current_entry else 0
+
+ # 3) Truncate any future checkpoints
+ # If we are at seq=2, but there's a seq=3 or higher from a prior "redo chain",
+ # remove those, so we maintain a strictly linear undo/redo stack.
+ stmt = select(BlockHistory).filter(BlockHistory.block_id == block.id, BlockHistory.sequence_number > current_seq)
+ result = await session.execute(stmt)
+ for entry in result.scalars():
+ session.delete(entry)
+
+ # Flush the deletes to ensure they're executed before we create a new entry
+ await session.flush()
+
+ # 4) Determine the next sequence number
+ next_seq = current_seq + 1
+
+ # 5) Create a new BlockHistory row reflecting the block's current state
+ history_entry = BlockHistory(
+ organization_id=actor.organization_id,
+ block_id=block.id,
+ sequence_number=next_seq,
+ description=block.description,
+ label=block.label,
+ value=block.value,
+ limit=block.limit,
+ metadata_=block.metadata_,
+ actor_type=ActorType.LETTA_AGENT if agent_id else ActorType.LETTA_USER,
+ actor_id=agent_id if agent_id else actor.id,
+ )
+ await history_entry.create_async(session, actor=actor, no_commit=True)
+
+ # 6) Update the block’s pointer to the new checkpoint
+ block.current_history_entry_id = history_entry.id
+
+ # 7) Flush changes, then commit once
+ block = await block.update_async(db_session=session, actor=actor, no_commit=True)
+ await session.commit()
+
+ return block.to_pydantic()
+
+ @enforce_types
+ async def _move_block_to_sequence(self, session: AsyncSession, block: BlockModel, target_seq: int, actor: PydanticUser) -> BlockModel:
+ """
+ Internal helper that moves the 'block' to the specified 'target_seq' within BlockHistory.
+ 1) Find the BlockHistory row at sequence_number=target_seq
+ 2) Copy fields into the block
+ 3) Update and flush (no_commit=True) - the caller is responsible for final commit
+
+ Raises:
+ NoResultFound: if no BlockHistory row for (block_id, target_seq)
+ """
+ if not block.id:
+ raise ValueError("Block is missing an ID. Cannot move sequence.")
+
+ stmt = select(BlockHistory).filter(
+ BlockHistory.block_id == block.id,
+ BlockHistory.sequence_number == target_seq,
+ )
+ result = await session.execute(stmt)
+ target_entry = result.scalar_one_or_none()
+ if not target_entry:
+ raise NoResultFound(f"No BlockHistory row found for block_id={block.id} at sequence={target_seq}")
+
+ # Copy fields from target_entry to block
+ block.description = target_entry.description # type: ignore
+ block.label = target_entry.label # type: ignore
+ block.value = target_entry.value # type: ignore
+ block.limit = target_entry.limit # type: ignore
+ block.metadata_ = target_entry.metadata_ # type: ignore
+ block.current_history_entry_id = target_entry.id # type: ignore
+
+ # Update in DB (optimistic locking).
+ # We'll do a flush now; the caller does final commit.
+ updated_block = await block.update_async(db_session=session, actor=actor, no_commit=True)
+ return updated_block
+
+ @enforce_types
+ @trace_method
+ async def undo_checkpoint_block(
+ self, block_id: str, actor: PydanticUser, use_preloaded_block: Optional[BlockModel] = None
+ ) -> PydanticBlock:
+ """
+ Move the block to the immediately previous checkpoint in BlockHistory.
+ If older sequences have been pruned, we jump to the largest sequence
+ number that is still < current_seq.
+ """
+ async with db_registry.async_session() as session:
+ # 1) Load the current block
+ block = (
+ await session.merge(use_preloaded_block)
+ if use_preloaded_block
+ else await BlockModel.read_async(db_session=session, identifier=block_id, actor=actor)
+ )
+
+ if not block.current_history_entry_id:
+ raise ValueError(f"Block {block_id} has no history entry - cannot undo.")
+
+ current_entry = await session.get(BlockHistory, block.current_history_entry_id)
+ if not current_entry:
+ raise NoResultFound(f"BlockHistory row not found for id={block.current_history_entry_id}")
+
+ current_seq = current_entry.sequence_number
+
+ # 2) Find the largest sequence < current_seq
+ stmt = (
+ select(BlockHistory)
+ .filter(BlockHistory.block_id == block.id, BlockHistory.sequence_number < current_seq)
+ .order_by(BlockHistory.sequence_number.desc())
+ .limit(1)
+ )
+ result = await session.execute(stmt)
+ previous_entry = result.scalar_one_or_none()
+ if not previous_entry:
+ # No earlier checkpoint available
+ raise ValueError(f"Block {block_id} is already at the earliest checkpoint (seq={current_seq}). Cannot undo further.")
+
+ # 3) Move to that sequence
+ block = await self._move_block_to_sequence(session, block, previous_entry.sequence_number, actor)
+
+ # 4) Commit
+ await session.commit()
+ return block.to_pydantic()
+
+ @enforce_types
+ @trace_method
+ async def redo_checkpoint_block(
+ self, block_id: str, actor: PydanticUser, use_preloaded_block: Optional[BlockModel] = None
+ ) -> PydanticBlock:
+ """
+ Move the block to the next checkpoint if it exists.
+ If some middle checkpoints have been pruned, we jump to the smallest
+ sequence > current_seq that remains.
+ """
+ async with db_registry.async_session() as session:
+ block = (
+ await session.merge(use_preloaded_block)
+ if use_preloaded_block
+ else await BlockModel.read_async(db_session=session, identifier=block_id, actor=actor)
+ )
+
+ if not block.current_history_entry_id:
+ raise ValueError(f"Block {block_id} has no history entry - cannot redo.")
+
+ current_entry = await session.get(BlockHistory, block.current_history_entry_id)
+ if not current_entry:
+ raise NoResultFound(f"BlockHistory row not found for id={block.current_history_entry_id}")
+
+ current_seq = current_entry.sequence_number
+
+ # Find the smallest sequence that is > current_seq
+ stmt = (
+ select(BlockHistory)
+ .filter(BlockHistory.block_id == block.id, BlockHistory.sequence_number > current_seq)
+ .order_by(BlockHistory.sequence_number.asc())
+ .limit(1)
+ )
+ result = await session.execute(stmt)
+ next_entry = result.scalar_one_or_none()
+ if not next_entry:
+ raise ValueError(f"Block {block_id} is at the highest checkpoint (seq={current_seq}). Cannot redo further.")
+
+ block = await self._move_block_to_sequence(session, block, next_entry.sequence_number, actor)
+
+ await session.commit()
+ return block.to_pydantic()
diff --git a/letta/services/message_manager.py b/letta/services/message_manager.py
index 6afd8e686..db93f08d5 100644
--- a/letta/services/message_manager.py
+++ b/letta/services/message_manager.py
@@ -504,45 +504,6 @@ class MessageManager:
# raise error if message type got modified
raise ValueError(f"Message type got modified: {letta_message_update.message_type}")
- @enforce_types
- @trace_method
- def update_message_by_letta_message(
- self, message_id: str, letta_message_update: LettaMessageUpdateUnion, actor: PydanticUser
- ) -> PydanticMessage:
- """
- Updated the underlying messages table giving an update specified to the user-facing LettaMessage
- """
- message = self.get_message_by_id(message_id=message_id, actor=actor)
- if letta_message_update.message_type == "assistant_message":
- # modify the tool call for send_message
- # TODO: fix this if we add parallel tool calls
- # TODO: note this only works if the AssistantMessage is generated by the standard send_message
- assert message.tool_calls[0].function.name == "send_message", (
- f"Expected the first tool call to be send_message, but got {message.tool_calls[0].function.name}"
- )
- original_args = json.loads(message.tool_calls[0].function.arguments)
- original_args["message"] = letta_message_update.content # override the assistant message
- update_tool_call = message.tool_calls[0].__deepcopy__()
- update_tool_call.function.arguments = json.dumps(original_args)
-
- update_message = MessageUpdate(tool_calls=[update_tool_call])
- elif letta_message_update.message_type == "reasoning_message":
- update_message = MessageUpdate(content=letta_message_update.reasoning)
- elif letta_message_update.message_type == "user_message" or letta_message_update.message_type == "system_message":
- update_message = MessageUpdate(content=letta_message_update.content)
- else:
- raise ValueError(f"Unsupported message type for modification: {letta_message_update.message_type}")
-
- message = self.update_message_by_id(message_id=message_id, message_update=update_message, actor=actor)
-
- # convert back to LettaMessage
- for letta_msg in message.to_letta_messages(use_assistant_message=True):
- if letta_msg.message_type == letta_message_update.message_type:
- return letta_msg
-
- # raise error if message type got modified
- raise ValueError(f"Message type got modified: {letta_message_update.message_type}")
-
@enforce_types
@trace_method
async def update_message_by_id_async(
diff --git a/letta/services/organization_manager.py b/letta/services/organization_manager.py
index 993a3eb14..b5d3829e3 100644
--- a/letta/services/organization_manager.py
+++ b/letta/services/organization_manager.py
@@ -44,13 +44,6 @@ class OrganizationManager:
await org.create_async(session)
return org.to_pydantic()
- @enforce_types
- @trace_method
- def create_default_organization(self) -> PydanticOrganization:
- """Create the default organization."""
- pydantic_org = PydanticOrganization(name=DEFAULT_ORG_NAME, id=DEFAULT_ORG_ID)
- return self.create_organization(pydantic_org)
-
@enforce_types
@trace_method
async def create_default_organization_async(self) -> PydanticOrganization:
diff --git a/letta/services/tool_manager.py b/letta/services/tool_manager.py
index a573a9b1f..7e4803321 100644
--- a/letta/services/tool_manager.py
+++ b/letta/services/tool_manager.py
@@ -41,36 +41,6 @@ logger = get_logger(__name__)
class ToolManager:
"""Manager class to handle business logic related to Tools."""
- # TODO: Refactor this across the codebase to use CreateTool instead of passing in a Tool object
- @enforce_types
- @trace_method
- def create_or_update_tool(self, pydantic_tool: PydanticTool, actor: PydanticUser, bypass_name_check: bool = False) -> PydanticTool:
- """Create a new tool based on the ToolCreate schema."""
- tool_id = self.get_tool_id_by_name(tool_name=pydantic_tool.name, actor=actor)
- if tool_id:
- # Put to dict and remove fields that should not be reset
- update_data = pydantic_tool.model_dump(exclude_unset=True, exclude_none=True)
-
- # If there's anything to update
- if update_data:
- # In case we want to update the tool type
- # Useful if we are shuffling around base tools
- updated_tool_type = None
- if "tool_type" in update_data:
- updated_tool_type = update_data.get("tool_type")
- tool = self.update_tool_by_id(
- tool_id, ToolUpdate(**update_data), actor, updated_tool_type=updated_tool_type, bypass_name_check=bypass_name_check
- )
- else:
- printd(
- f"`create_or_update_tool` was called with user_id={actor.id}, organization_id={actor.organization_id}, name={pydantic_tool.name}, but found existing tool with nothing to update."
- )
- tool = self.get_tool_by_id(tool_id, actor=actor)
- else:
- tool = self.create_tool(pydantic_tool, actor=actor)
-
- return tool
-
@enforce_types
@trace_method
async def create_or_update_tool_async(
@@ -110,12 +80,11 @@ class ToolManager:
pass
@enforce_types
- @trace_method
- def create_or_update_mcp_tool(
+ async def create_mcp_tool_async(
self, tool_create: ToolCreate, mcp_server_name: str, mcp_server_id: str, actor: PydanticUser
) -> PydanticTool:
metadata = {MCP_TOOL_TAG_NAME_PREFIX: {"server_name": mcp_server_name, "server_id": mcp_server_id}}
- return self.create_or_update_tool(
+ return await self.create_or_update_tool_async(
PydanticTool(
tool_type=ToolType.EXTERNAL_MCP, name=tool_create.json_schema["name"], metadata_=metadata, **tool_create.model_dump()
),
@@ -123,7 +92,8 @@ class ToolManager:
)
@enforce_types
- async def create_mcp_tool_async(
+ @trace_method
+ async def create_or_update_mcp_tool_async(
self, tool_create: ToolCreate, mcp_server_name: str, mcp_server_id: str, actor: PydanticUser
) -> PydanticTool:
metadata = {MCP_TOOL_TAG_NAME_PREFIX: {"server_name": mcp_server_name, "server_id": mcp_server_id}}
@@ -134,13 +104,6 @@ class ToolManager:
actor,
)
- @enforce_types
- @trace_method
- def create_or_update_composio_tool(self, tool_create: ToolCreate, actor: PydanticUser) -> PydanticTool:
- return self.create_or_update_tool(
- PydanticTool(tool_type=ToolType.EXTERNAL_COMPOSIO, name=tool_create.json_schema["name"], **tool_create.model_dump()), actor
- )
-
@enforce_types
@trace_method
async def create_or_update_composio_tool_async(self, tool_create: ToolCreate, actor: PydanticUser) -> PydanticTool:
@@ -551,6 +514,8 @@ class ToolManager:
# original tool may no have a JSON schema at all for legacy reasons
# in this case, fallback to dangerous schema generation
if new_schema is None:
+ # Get source_type from update_data if present, otherwise use current tool's source_type
+ source_type = update_data.get("source_type", current_tool.source_type)
if source_type == "typescript":
from letta.functions.typescript_parser import derive_typescript_json_schema
@@ -602,103 +567,6 @@ class ToolManager:
except NoResultFound:
raise ValueError(f"Tool with id {tool_id} not found.")
- @enforce_types
- @trace_method
- def upsert_base_tools(self, actor: PydanticUser) -> List[PydanticTool]:
- """
- Initialize or update all built-in Letta tools for a user.
-
- This method scans predefined modules to discover and register all base tools
- that ship with Letta. Tools are categorized by type (core, memory, multi-agent, etc.)
- and tagged appropriately for filtering.
-
- Args:
- actor: The user to create/update tools for
-
- Returns:
- List of all base tools that were created or updated
-
- Tool Categories Created:
- - LETTA_CORE: Basic conversation tools (send_message)
- - LETTA_MEMORY_CORE: Memory management (core_memory_append/replace)
- - LETTA_MULTI_AGENT_CORE: Multi-agent communication tools
- - LETTA_SLEEPTIME_CORE: Sleeptime agent tools
- - LETTA_VOICE_SLEEPTIME_CORE: Voice agent specific tools
- - LETTA_BUILTIN: Additional built-in utilities
- - LETTA_FILES_CORE: File handling tools
-
- Side Effects:
- - Creates or updates tools in database
- - Tools are marked with appropriate type and tags
- - Existing custom tools with same names are NOT overwritten
-
- Note:
- This is typically called during user initialization or system upgrade
- to ensure all base tools are available. Custom tools take precedence
- over base tools with the same name.
- """
- functions_to_schema = {}
-
- for module_name in LETTA_TOOL_MODULE_NAMES:
- try:
- module = importlib.import_module(module_name)
- except Exception as e:
- # Handle other general exceptions
- raise e
-
- try:
- # Load the function set
- functions_to_schema.update(load_function_set(module))
- except ValueError as e:
- err = f"Error loading function set '{module_name}': {e}"
- warnings.warn(err)
-
- # create tool in db
- tools = []
- for name, schema in functions_to_schema.items():
- if name in LETTA_TOOL_SET:
- if name in BASE_TOOLS:
- tool_type = ToolType.LETTA_CORE
- tags = [tool_type.value]
- elif name in BASE_MEMORY_TOOLS:
- tool_type = ToolType.LETTA_MEMORY_CORE
- tags = [tool_type.value]
- elif name in calculate_multi_agent_tools():
- tool_type = ToolType.LETTA_MULTI_AGENT_CORE
- tags = [tool_type.value]
- elif name in BASE_SLEEPTIME_TOOLS:
- tool_type = ToolType.LETTA_SLEEPTIME_CORE
- tags = [tool_type.value]
- elif name in BASE_VOICE_SLEEPTIME_TOOLS or name in BASE_VOICE_SLEEPTIME_CHAT_TOOLS:
- tool_type = ToolType.LETTA_VOICE_SLEEPTIME_CORE
- tags = [tool_type.value]
- elif name in BUILTIN_TOOLS:
- tool_type = ToolType.LETTA_BUILTIN
- tags = [tool_type.value]
- elif name in FILES_TOOLS:
- tool_type = ToolType.LETTA_FILES_CORE
- tags = [tool_type.value]
- else:
- logger.warning(f"Tool name {name} is not in any known base tool set, skipping")
- continue
-
- # create to tool
- tools.append(
- self.create_or_update_tool(
- PydanticTool(
- name=name,
- tags=tags,
- source_type="python",
- tool_type=tool_type,
- return_char_limit=BASE_FUNCTION_RETURN_CHAR_LIMIT,
- ),
- actor=actor,
- )
- )
-
- # TODO: Delete any base tools that are stale
- return tools
-
@enforce_types
@trace_method
async def upsert_base_tools_async(
diff --git a/tests/managers/conftest.py b/tests/managers/conftest.py
new file mode 100644
index 000000000..36821ee19
--- /dev/null
+++ b/tests/managers/conftest.py
@@ -0,0 +1,780 @@
+"""
+Shared fixtures for all manager tests.
+
+This conftest.py makes fixtures available to all test files in the tests/managers/ directory.
+"""
+
+import os
+import time
+import uuid
+from typing import Tuple
+
+import pytest
+from anthropic.types.beta import BetaMessage
+from anthropic.types.beta.messages import BetaMessageBatchIndividualResponse, BetaMessageBatchSucceededResult
+from sqlalchemy import text
+
+from letta.config import LettaConfig
+from letta.functions.functions import derive_openai_json_schema, parse_source_code
+from letta.functions.mcp_client.types import MCPTool
+from letta.helpers import ToolRulesSolver
+from letta.orm import Base
+from letta.schemas.agent import CreateAgent
+from letta.schemas.block import Block as PydanticBlock, CreateBlock
+from letta.schemas.embedding_config import EmbeddingConfig
+from letta.schemas.enums import JobStatus, MessageRole
+from letta.schemas.environment_variables import SandboxEnvironmentVariableCreate, SandboxEnvironmentVariableUpdate
+from letta.schemas.file import FileMetadata as PydanticFileMetadata
+from letta.schemas.job import BatchJob, Job as PydanticJob
+from letta.schemas.letta_message_content import TextContent
+from letta.schemas.llm_batch_job import AgentStepState
+from letta.schemas.llm_config import LLMConfig
+from letta.schemas.message import Message as PydanticMessage, MessageCreate
+from letta.schemas.organization import Organization
+from letta.schemas.passage import Passage as PydanticPassage
+from letta.schemas.run import Run as PydanticRun
+from letta.schemas.sandbox_config import E2BSandboxConfig, SandboxConfigCreate
+from letta.schemas.source import Source as PydanticSource
+from letta.schemas.tool import Tool as PydanticTool, ToolCreate
+from letta.schemas.tool_rule import InitToolRule
+from letta.schemas.user import User as PydanticUser
+from letta.server.db import db_registry
+from letta.server.server import SyncServer
+from letta.services.block_manager import BlockManager
+
+# Constants
+DEFAULT_EMBEDDING_CONFIG = EmbeddingConfig.default_config(provider="openai")
+CREATE_DELAY_SQLITE = 1
+USING_SQLITE = not bool(os.getenv("LETTA_PG_URI"))
+
+
+# ======================================================================================================================
+# Database and Server Fixtures
+# ======================================================================================================================
+
+
+async def async_session():
+ """Provide an async database session."""
+ async with db_registry.async_session() as session:
+ yield session
+
+
[email protected](autouse=True)
+async def _clear_tables(async_session):
+ """Clear all tables before each test (except block_history)."""
+ # Temporarily disable foreign key constraints for SQLite only
+ engine_name = async_session.bind.dialect.name
+ if engine_name == "sqlite":
+ await async_session.execute(text("PRAGMA foreign_keys = OFF"))
+
+ for table in reversed(Base.metadata.sorted_tables): # Reverse to avoid FK issues
+ # If this is the block_history table, skip it
+ if table.name == "block_history":
+ continue
+ await async_session.execute(table.delete()) # Truncate table
+ await async_session.commit()
+
+ # Re-enable foreign key constraints for SQLite only
+ if engine_name == "sqlite":
+ await async_session.execute(text("PRAGMA foreign_keys = ON"))
+
+
[email protected](scope="module")
+def server():
+ """Create a server instance for the test module."""
+ config = LettaConfig.load()
+ config.save()
+ server = SyncServer(init_with_default_org_and_user=False)
+ return server
+
+
+# ======================================================================================================================
+# Organization and User Fixtures
+# ======================================================================================================================
+
+
+async def default_organization(server: SyncServer):
+ """Create and return the default organization."""
+ org = await server.organization_manager.create_default_organization_async()
+ yield org
+
+
+async def other_organization(server: SyncServer):
+ """Create and return another organization."""
+ org = await server.organization_manager.create_organization_async(pydantic_org=Organization(name="letta"))
+ yield org
+
+
+async def default_user(server: SyncServer, default_organization):
+ """Create and return the default user within the default organization."""
+ user = await server.user_manager.create_default_actor_async(org_id=default_organization.id)
+ yield user
+
+
+async def other_user(server: SyncServer, default_organization):
+ """Create and return another user within the default organization."""
+ user = await server.user_manager.create_actor_async(PydanticUser(name="other", organization_id=default_organization.id))
+ yield user
+
+
+async def other_user_different_org(server: SyncServer, other_organization):
+ """Create and return a user in a different organization."""
+ user = await server.user_manager.create_actor_async(PydanticUser(name="other", organization_id=other_organization.id))
+ yield user
+
+
+# ======================================================================================================================
+# Source and File Fixtures
+# ======================================================================================================================
+
+
+async def default_source(server: SyncServer, default_user):
+ """Create and return the default source."""
+ source_pydantic = PydanticSource(
+ name="Test Source",
+ description="This is a test source.",
+ metadata={"type": "test"},
+ embedding_config=DEFAULT_EMBEDDING_CONFIG,
+ )
+ source = await server.source_manager.create_source(source=source_pydantic, actor=default_user)
+ yield source
+
+
+async def other_source(server: SyncServer, default_user):
+ """Create and return another source."""
+ source_pydantic = PydanticSource(
+ name="Another Test Source",
+ description="This is yet another test source.",
+ metadata={"type": "another_test"},
+ embedding_config=DEFAULT_EMBEDDING_CONFIG,
+ )
+ source = await server.source_manager.create_source(source=source_pydantic, actor=default_user)
+ yield source
+
+
+async def default_file(server: SyncServer, default_source, default_user, default_organization):
+ """Create and return the default file."""
+ file = await server.file_manager.create_file(
+ PydanticFileMetadata(file_name="test_file", organization_id=default_organization.id, source_id=default_source.id),
+ actor=default_user,
+ )
+ yield file
+
+
+async def another_file(server: SyncServer, default_source, default_user, default_organization):
+ """Create and return another file."""
+ pf = PydanticFileMetadata(
+ file_name="another_file",
+ organization_id=default_organization.id,
+ source_id=default_source.id,
+ )
+ file = await server.file_manager.create_file(pf, actor=default_user)
+ yield file
+
+
+# ======================================================================================================================
+# Tool Fixtures
+# ======================================================================================================================
+
+
+async def print_tool(server: SyncServer, default_user, default_organization):
+ """Create and return a print tool."""
+
+ def print_tool(message: str):
+ """
+ Args:
+ message (str): The message to print.
+
+ Returns:
+ str: The message that was printed.
+ """
+ print(message)
+ return message
+
+ # Set up tool details
+ source_code = parse_source_code(print_tool)
+ source_type = "python"
+ description = "test_description"
+ tags = ["test"]
+ metadata = {"a": "b"}
+
+ tool = PydanticTool(description=description, tags=tags, source_code=source_code, source_type=source_type, metadata_=metadata)
+ derived_json_schema = derive_openai_json_schema(source_code=tool.source_code, name=tool.name)
+
+ derived_name = derived_json_schema["name"]
+ tool.json_schema = derived_json_schema
+ tool.name = derived_name
+
+ tool = await server.tool_manager.create_or_update_tool_async(tool, actor=default_user)
+ yield tool
+
+
+async def bash_tool(server: SyncServer, default_user, default_organization):
+ """Create and return a bash tool with requires_approval."""
+
+ def bash_tool(operation: str):
+ """
+ Args:
+ operation (str): The bash operation to execute.
+
+ Returns:
+ str: The result of the executed operation.
+ """
+ print("scary bash operation")
+ return "success"
+
+ # Set up tool details
+ source_code = parse_source_code(bash_tool)
+ source_type = "python"
+ description = "test_description"
+ tags = ["test"]
+ metadata = {"a": "b"}
+
+ tool = PydanticTool(description=description, tags=tags, source_code=source_code, source_type=source_type, metadata_=metadata)
+ derived_json_schema = derive_openai_json_schema(source_code=tool.source_code, name=tool.name)
+
+ derived_name = derived_json_schema["name"]
+ tool.json_schema = derived_json_schema
+ tool.name = derived_name
+ tool.default_requires_approval = True
+
+ tool = await server.tool_manager.create_or_update_tool_async(tool, actor=default_user)
+ yield tool
+
+
+async def other_tool(server: SyncServer, default_user, default_organization):
+ """Create and return another tool."""
+
+ def print_other_tool(message: str):
+ """
+ Args:
+ message (str): The message to print.
+
+ Returns:
+ str: The message that was printed.
+ """
+ print(message)
+ return message
+
+ # Set up tool details
+ source_code = parse_source_code(print_other_tool)
+ source_type = "python"
+ description = "other_tool_description"
+ tags = ["test"]
+
+ tool = PydanticTool(description=description, tags=tags, source_code=source_code, source_type=source_type)
+ derived_json_schema = derive_openai_json_schema(source_code=tool.source_code, name=tool.name)
+
+ derived_name = derived_json_schema["name"]
+ tool.json_schema = derived_json_schema
+ tool.name = derived_name
+
+ tool = await server.tool_manager.create_or_update_tool_async(tool, actor=default_user)
+ yield tool
+
+
+# ======================================================================================================================
+# Block Fixtures
+# ======================================================================================================================
+
+
+async def default_block(server: SyncServer, default_user):
+ """Create and return a default block."""
+ block_manager = BlockManager()
+ block_data = PydanticBlock(
+ label="default_label",
+ value="Default Block Content",
+ description="A default test block",
+ limit=1000,
+ metadata={"type": "test"},
+ )
+ block = await block_manager.create_or_update_block_async(block_data, actor=default_user)
+ yield block
+
+
+async def other_block(server: SyncServer, default_user):
+ """Create and return another block."""
+ block_manager = BlockManager()
+ block_data = PydanticBlock(
+ label="other_label",
+ value="Other Block Content",
+ description="Another test block",
+ limit=500,
+ metadata={"type": "test"},
+ )
+ block = await block_manager.create_or_update_block_async(block_data, actor=default_user)
+ yield block
+
+
+# ======================================================================================================================
+# Agent Fixtures
+# ======================================================================================================================
+
+
+async def sarah_agent(server: SyncServer, default_user, default_organization):
+ """Create and return a sample agent named 'sarah_agent'."""
+ agent_state = await server.agent_manager.create_agent_async(
+ agent_create=CreateAgent(
+ name="sarah_agent",
+ memory_blocks=[],
+ llm_config=LLMConfig.default_config("gpt-4o-mini"),
+ embedding_config=EmbeddingConfig.default_config(provider="openai"),
+ include_base_tools=False,
+ ),
+ actor=default_user,
+ )
+ yield agent_state
+
+
+async def charles_agent(server: SyncServer, default_user, default_organization):
+ """Create and return a sample agent named 'charles_agent'."""
+ agent_state = await server.agent_manager.create_agent_async(
+ agent_create=CreateAgent(
+ name="charles_agent",
+ memory_blocks=[CreateBlock(label="human", value="Charles"), CreateBlock(label="persona", value="I am a helpful assistant")],
+ llm_config=LLMConfig.default_config("gpt-4o-mini"),
+ embedding_config=EmbeddingConfig.default_config(provider="openai"),
+ include_base_tools=False,
+ ),
+ actor=default_user,
+ )
+ yield agent_state
+
+
+async def comprehensive_test_agent_fixture(server: SyncServer, default_user, print_tool, default_source, default_block) -> Tuple:
+ """Create a comprehensive test agent with all features."""
+ memory_blocks = [CreateBlock(label="human", value="BananaBoy"), CreateBlock(label="persona", value="I am a helpful assistant")]
+ create_agent_request = CreateAgent(
+ system="test system",
+ memory_blocks=memory_blocks,
+ llm_config=LLMConfig.default_config("gpt-4o-mini"),
+ embedding_config=EmbeddingConfig.default_config(provider="openai"),
+ block_ids=[default_block.id],
+ tool_ids=[print_tool.id],
+ source_ids=[default_source.id],
+ tags=["a", "b"],
+ description="test_description",
+ metadata={"test_key": "test_value"},
+ tool_rules=[InitToolRule(tool_name=print_tool.name)],
+ initial_message_sequence=[MessageCreate(role=MessageRole.user, content="hello world")],
+ tool_exec_environment_variables={"test_env_var_key_a": "test_env_var_value_a", "test_env_var_key_b": "test_env_var_value_b"},
+ message_buffer_autoclear=True,
+ include_base_tools=False,
+ )
+ created_agent = await server.agent_manager.create_agent_async(
+ create_agent_request,
+ actor=default_user,
+ )
+
+ yield created_agent, create_agent_request
+
+
+# ======================================================================================================================
+# Archive and Passage Fixtures
+# ======================================================================================================================
+
+
+async def default_archive(server: SyncServer, default_user):
+ """Create and return a default archive."""
+ archive = await server.archive_manager.create_archive_async("test", actor=default_user)
+ yield archive
+
+
+async def agent_passage_fixture(server: SyncServer, default_user, sarah_agent):
+ """Create an agent passage."""
+ # Get or create default archive for the agent
+ archive = await server.archive_manager.get_or_create_default_archive_for_agent_async(
+ agent_id=sarah_agent.id, agent_name=sarah_agent.name, actor=default_user
+ )
+
+ passage = await server.passage_manager.create_agent_passage_async(
+ PydanticPassage(
+ text="Hello, I am an agent passage",
+ archive_id=archive.id,
+ organization_id=default_user.organization_id,
+ embedding=[0.1],
+ embedding_config=DEFAULT_EMBEDDING_CONFIG,
+ metadata={"type": "test"},
+ ),
+ actor=default_user,
+ )
+ yield passage
+
+
+async def source_passage_fixture(server: SyncServer, default_user, default_file, default_source):
+ """Create a source passage."""
+ passage = await server.passage_manager.create_source_passage_async(
+ PydanticPassage(
+ text="Hello, I am a source passage",
+ source_id=default_source.id,
+ file_id=default_file.id,
+ organization_id=default_user.organization_id,
+ embedding=[0.1],
+ embedding_config=DEFAULT_EMBEDDING_CONFIG,
+ metadata={"type": "test"},
+ ),
+ file_metadata=default_file,
+ actor=default_user,
+ )
+ yield passage
+
+
+# ======================================================================================================================
+# Message Fixtures
+# ======================================================================================================================
+
+
+async def hello_world_message_fixture(server: SyncServer, default_user, sarah_agent):
+ """Create a hello world message."""
+ message = PydanticMessage(
+ agent_id=sarah_agent.id,
+ role="user",
+ content=[TextContent(text="Hello, world!")],
+ )
+
+ msg = await server.message_manager.create_many_messages_async([message], actor=default_user)
+ yield msg[0]
+
+
+# ======================================================================================================================
+# Sandbox Fixtures
+# ======================================================================================================================
+
+
+async def sandbox_config_fixture(server: SyncServer, default_user):
+ """Create a sandbox configuration."""
+ sandbox_config_create = SandboxConfigCreate(
+ config=E2BSandboxConfig(),
+ )
+ created_config = await server.sandbox_config_manager.create_or_update_sandbox_config_async(sandbox_config_create, actor=default_user)
+ yield created_config
+
+
+async def sandbox_env_var_fixture(server: SyncServer, sandbox_config_fixture, default_user):
+ """Create a sandbox environment variable."""
+ env_var_create = SandboxEnvironmentVariableCreate(
+ key="SAMPLE_VAR",
+ value="sample_value",
+ description="A sample environment variable for testing.",
+ )
+ created_env_var = await server.sandbox_config_manager.create_sandbox_env_var_async(
+ env_var_create, sandbox_config_id=sandbox_config_fixture.id, actor=default_user
+ )
+ yield created_env_var
+
+
+# ======================================================================================================================
+# File Attachment Fixtures
+# ======================================================================================================================
+
+
+async def file_attachment(server: SyncServer, default_user, sarah_agent, default_file):
+ """Create a file attachment to an agent."""
+ assoc, closed_files = await server.file_agent_manager.attach_file(
+ agent_id=sarah_agent.id,
+ file_id=default_file.id,
+ file_name=default_file.file_name,
+ source_id=default_file.source_id,
+ actor=default_user,
+ visible_content="initial",
+ max_files_open=sarah_agent.max_files_open,
+ )
+ yield assoc
+
+
+# ======================================================================================================================
+# Job Fixtures
+# ======================================================================================================================
+
+
+async def default_job(server: SyncServer, default_user):
+ """Create and return a default job."""
+ job_pydantic = PydanticJob(
+ user_id=default_user.id,
+ status=JobStatus.pending,
+ )
+ job = await server.job_manager.create_job_async(pydantic_job=job_pydantic, actor=default_user)
+ yield job
+
+
+async def default_run(server: SyncServer, default_user):
+ """Create and return a default run."""
+ run_pydantic = PydanticRun(
+ user_id=default_user.id,
+ status=JobStatus.pending,
+ )
+ run = await server.job_manager.create_job_async(pydantic_job=run_pydantic, actor=default_user)
+ yield run
+
+
+async def letta_batch_job(server: SyncServer, default_user):
+ """Create a batch job."""
+ return await server.job_manager.create_job_async(BatchJob(user_id=default_user.id), actor=default_user)
+
+
+# ======================================================================================================================
+# MCP Tool Fixtures
+# ======================================================================================================================
+
+
+async def mcp_tool(server: SyncServer, default_user):
+ """Create an MCP tool."""
+ mcp_tool = MCPTool(
+ name="weather_lookup",
+ description="Fetches the current weather for a given location.",
+ inputSchema={
+ "type": "object",
+ "properties": {
+ "location": {"type": "string", "description": "The name of the city or location."},
+ "units": {
+ "type": "string",
+ "enum": ["metric", "imperial"],
+ "description": "The unit system for temperature (metric or imperial).",
+ },
+ },
+ "required": ["location"],
+ },
+ )
+ mcp_server_name = "test"
+ mcp_server_id = "test-server-id" # Mock server ID for testing
+ tool_create = ToolCreate.from_mcp(mcp_server_name=mcp_server_name, mcp_tool=mcp_tool)
+ tool = await server.tool_manager.create_or_update_mcp_tool_async(
+ tool_create=tool_create, mcp_server_name=mcp_server_name, mcp_server_id=mcp_server_id, actor=default_user
+ )
+ yield tool
+
+
+# ======================================================================================================================
+# Test Data Creation Fixtures
+# ======================================================================================================================
+
+
+async def create_test_passages(server: SyncServer, default_file, default_user, sarah_agent, default_source):
+ """Helper function to create test passages for all tests."""
+ # Get or create default archive for the agent
+ archive = await server.archive_manager.get_or_create_default_archive_for_agent(
+ agent_id=sarah_agent.id, agent_name=sarah_agent.name, actor=default_user
+ )
+
+ # Create agent passages
+ passages = []
+ for i in range(5):
+ passage = await server.passage_manager.create_agent_passage(
+ PydanticPassage(
+ text=f"Agent passage {i}",
+ archive_id=archive.id,
+ organization_id=default_user.organization_id,
+ embedding=[0.1],
+ embedding_config=DEFAULT_EMBEDDING_CONFIG,
+ metadata={"type": "test"},
+ ),
+ actor=default_user,
+ )
+ passages.append(passage)
+ if USING_SQLITE:
+ time.sleep(CREATE_DELAY_SQLITE)
+
+ # Create source passages
+ for i in range(5):
+ passage = await server.passage_manager.create_source_passage(
+ PydanticPassage(
+ text=f"Source passage {i}",
+ source_id=default_source.id,
+ file_id=default_file.id,
+ organization_id=default_user.organization_id,
+ embedding=[0.1],
+ embedding_config=DEFAULT_EMBEDDING_CONFIG,
+ metadata={"type": "test"},
+ ),
+ file_metadata=default_file,
+ actor=default_user,
+ )
+ passages.append(passage)
+ if USING_SQLITE:
+ time.sleep(CREATE_DELAY_SQLITE)
+
+ return passages
+
+
+async def agent_passages_setup(server: SyncServer, default_archive, default_source, default_file, default_user, sarah_agent):
+ """Setup fixture for agent passages tests."""
+ agent_id = sarah_agent.id
+ actor = default_user
+
+ await server.agent_manager.attach_source_async(agent_id=agent_id, source_id=default_source.id, actor=actor)
+
+ # Create some source passages
+ source_passages = []
+ for i in range(3):
+ passage = await server.passage_manager.create_source_passage_async(
+ PydanticPassage(
+ organization_id=actor.organization_id,
+ source_id=default_source.id,
+ file_id=default_file.id,
+ text=f"Source passage {i}",
+ embedding=[0.1], # Default OpenAI embedding size
+ embedding_config=DEFAULT_EMBEDDING_CONFIG,
+ ),
+ file_metadata=default_file,
+ actor=actor,
+ )
+ source_passages.append(passage)
+
+ # attach archive
+ await server.archive_manager.attach_agent_to_archive_async(
+ agent_id=agent_id, archive_id=default_archive.id, is_owner=True, actor=default_user
+ )
+
+ # Create some agent passages
+ agent_passages = []
+ for i in range(2):
+ passage = await server.passage_manager.create_agent_passage_async(
+ PydanticPassage(
+ organization_id=actor.organization_id,
+ archive_id=default_archive.id,
+ text=f"Agent passage {i}",
+ embedding=[0.1], # Default OpenAI embedding size
+ embedding_config=DEFAULT_EMBEDDING_CONFIG,
+ ),
+ actor=actor,
+ )
+ agent_passages.append(passage)
+
+ yield agent_passages, source_passages
+
+ # Cleanup
+ await server.source_manager.delete_source(default_source.id, actor=actor)
+
+
+async def agent_with_tags(server: SyncServer, default_user):
+ """Create agents with specific tags."""
+ agent1 = await server.agent_manager.create_agent_async(
+ agent_create=CreateAgent(
+ name="agent1",
+ tags=["primary_agent", "benefit_1"],
+ llm_config=LLMConfig.default_config("gpt-4o-mini"),
+ embedding_config=EmbeddingConfig.default_config(provider="openai"),
+ memory_blocks=[],
+ include_base_tools=False,
+ ),
+ actor=default_user,
+ )
+
+ agent2 = await server.agent_manager.create_agent_async(
+ agent_create=CreateAgent(
+ name="agent2",
+ tags=["primary_agent", "benefit_2"],
+ llm_config=LLMConfig.default_config("gpt-4o-mini"),
+ embedding_config=EmbeddingConfig.default_config(provider="openai"),
+ memory_blocks=[],
+ include_base_tools=False,
+ ),
+ actor=default_user,
+ )
+
+ agent3 = await server.agent_manager.create_agent_async(
+ agent_create=CreateAgent(
+ name="agent3",
+ tags=["primary_agent", "benefit_1", "benefit_2"],
+ llm_config=LLMConfig.default_config("gpt-4o-mini"),
+ embedding_config=EmbeddingConfig.default_config(provider="openai"),
+ memory_blocks=[],
+ include_base_tools=False,
+ ),
+ actor=default_user,
+ )
+
+ return [agent1, agent2, agent3]
+
+
+# ======================================================================================================================
+# LLM and Step State Fixtures
+# ======================================================================================================================
+
+
+def dummy_llm_config() -> LLMConfig:
+ """Create a dummy LLM config."""
+ return LLMConfig.default_config("gpt-4o-mini")
+
+
+def dummy_tool_rules_solver() -> ToolRulesSolver:
+ """Create a dummy tool rules solver."""
+ return ToolRulesSolver(tool_rules=[InitToolRule(tool_name="send_message")])
+
+
+def dummy_step_state(dummy_tool_rules_solver: ToolRulesSolver) -> AgentStepState:
+ """Create a dummy step state."""
+ return AgentStepState(step_number=1, tool_rules_solver=dummy_tool_rules_solver)
+
+
+def dummy_successful_response() -> BetaMessageBatchIndividualResponse:
+ """Create a dummy successful Anthropic message batch response."""
+ return BetaMessageBatchIndividualResponse(
+ custom_id="my-second-request",
+ result=BetaMessageBatchSucceededResult(
+ type="succeeded",
+ message=BetaMessage(
+ id="msg_abc123",
+ role="assistant",
+ type="message",
+ model="claude-3-5-sonnet-20240620",
+ content=[{"type": "text", "text": "hi!"}],
+ usage={"input_tokens": 5, "output_tokens": 7},
+ stop_reason="end_turn",
+ ),
+ ),
+ )
+
+
+# ======================================================================================================================
+# Environment Setup Fixtures
+# ======================================================================================================================
+
+
[email protected](params=[None, "PRODUCTION"])
+def set_letta_environment(request, monkeypatch):
+ """Parametrized fixture to test with different environment settings."""
+ from letta.settings import settings
+
+ # Patch the settings.environment attribute
+ original = settings.environment
+ monkeypatch.setattr(settings, "environment", request.param)
+ yield request.param
+ # Restore original environment
+ monkeypatch.setattr(settings, "environment", original)
diff --git a/tests/managers/data/test_embeddings.json b/tests/managers/data/test_embeddings.json
new file mode 100644
index 000000000..4c190b534
--- /dev/null
+++ b/tests/managers/data/test_embeddings.json
@@ -0,0 +1,7 @@
+{
+ "I like red": [0.005645134020596743, -0.05368466302752495, 0.00702847121283412, 0.02441101148724556, 0.035100437700748444, -0.004485366400331259, 0.021015547215938568, 0.03585498407483101, -0.02661876194179058, -0.026171622797846794, -0.0242712814360857, -0.0063926950097084045, -0.054495103657245636, 0.04476982355117798, 0.061202194541692734, 0.01205180212855339, -0.024299226701259613, 0.0033343317918479443, -0.026199569925665855, 0.030014226213097572, 0.026520950719714165, -0.009571575559675694, 0.024159496650099754, 0.04510517790913582, -0.00016036319721024483, 0.019632210955023766, 0.02410360425710678, 0.023670436814427376, 0.0481792613863945, -0.014518054202198982, 0.013602816499769688, -0.03149537369608879, -0.051141560077667236, -0.04697757586836815, -0.006504479795694351, -0.024802258238196373, -0.010389002040028572, 0.018318738788366318, 0.003143948270007968, -0.011416025459766388, 0.019115205854177475, 0.03962772339582443, 0.06349378079175949, -0.020666219294071198, -0.004890586249530315, -0.025892160832881927, -0.03266911581158638, -0.025151586160063744, -0.017759814858436584, 0.029678871855139732, -0.028225669637322426, -0.022091476246714592, 0.0005519376136362553, 0.0790877640247345, -0.03437383472919464, 0.007964668795466423, -0.012666618451476097, 0.045244909822940826, 0.024732394143939018, -0.0259480532258749, 0.01986975409090519, -0.004314195830374956, -0.02040073089301586, 0.017815707251429558, -0.005554308649152517, 0.011213415302336216, -0.03565936163067818, -0.028155803680419922, 0.06919480860233307, 0.002068019239231944, -0.025179533287882805, 0.0016619260422885418, -0.03524016588926315, -0.004838187247514725, -0.0041919308714568615, -0.019101232290267944, -0.009934877045452595, 0.009403898380696774, -0.014657785184681416, 0.03839808702468872, -0.0327250100672245, 0.013365273363888264, -0.009550616145133972, -0.06097862496972084, -0.051169503480196, -0.06394091993570328, 0.024648554623126984, -0.03252938389778137, 0.0018444496672600508, -0.0035194754600524902, -0.014182699844241142, 0.01464381255209446, -0.011304240673780441, -0.03082466684281826, -0.03904085233807564, 0.034262049943208694, -0.05505402758717537, -0.008467700332403183, -0.013148690573871136, 0.021420767530798912, -0.00475784158334136, -0.010361055843532085, -0.02826758846640587, 0.029092000797390938, -0.010088580660521984, -0.027373310178518295, 0.07601368427276611, 0.0010200365213677287, -0.00014562593423761427, -0.0642203837633133, -0.021141305565834045, -0.007468624040484428, -0.00944581814110279, 0.023949898779392242, -0.016432369127869606, -0.025207478553056717, -0.011828232556581497, -0.022608481347560883, -0.021630363538861275, -0.04510517790913582, 0.009543629363179207, 0.020917735993862152, 0.014546000398695469, -0.018304765224456787, -0.07254835218191147, 0.0004942985251545906, 0.0005842503742314875, -0.0783611610531807, 0.025053774937987328, -0.005687053315341473, 0.005840757396072149, -0.028407320380210876, -0.013791453093290329, -0.018765877932310104, -0.015356441028416157, 0.029036108404397964, -0.003042643191292882, -0.06690321862697601, -0.04694962874054909, -0.025151586160063744, -0.01651620864868164, -0.016138935461640358, 0.03537989780306816, -0.04829104617238045, 0.04345635324716568, -0.0033028924372047186, -0.024355119094252586, -0.015719741582870483, 0.03767148777842522, 0.00986501108855009, 0.0031037756707519293, 0.032249923795461655, -0.04664221778512001, -0.004736882168799639, -0.004792774561792612, -0.03454151004552841, 0.038481928408145905, -0.06774160265922546, -0.004953465424478054, 0.016041122376918793, 0.01720089092850685, 0.00012019052519463003, -0.05030317232012749, -0.03515632823109627, 0.01262469869107008, 0.03565936163067818, -0.07707563787698746, -0.0016086535761132836, -0.0063822148367762566, 0.04697757586836815, 0.03152332082390785, 0.01753624528646469, 0.030237795785069466, 0.005051277112215757, -0.010570652782917023, 0.026157649233937263, -0.013008959591388702, 0.04633481055498123, 0.03350750356912613, 0.03219402953982353, 0.0018252365989610553, 0.046530432999134064, -0.013407193124294281, 0.009557602927088737, -0.030684934929013252, -0.0069201793521642685, -0.002207750454545021, -0.0034024508204311132, -0.050414957106113434, -0.012086734175682068, 0.019967565312981606, 0.09999153017997742, -0.009054571390151978, -0.007671233732253313, 0.017103077843785286, -0.011381092481315136, 0.026534924283623695, 0.041835471987724304, -0.06382913887500763, -0.01650223508477211, 0.03468124195933342, 0.0532095767557621, 0.0390687957406044, -0.04530080035328865, -0.02055443450808525, -0.033088307827711105, -0.02965092658996582, 0.016558127477765083, 0.03266911581158638, -0.021141305565834045, 0.08115578442811966, -0.01985578052699566, 0.022762184962630272, 0.03987923637032509, 0.008516606874763966, -0.01666991226375103, 0.02410360425710678, 0.014406269416213036, 0.016795670613646507, 0.026479031890630722, -0.01472765114158392, -0.03465329855680466, 0.06427627801895142, -0.008754149079322815, -0.051448967307806015, 0.06751803308725357, 0.05608803778886795, -0.03515632823109627, 0.00019060185877606273, 0.048766132444143295, 0.0007183048292063177, 0.018905609846115112, -0.018290791660547256, -0.008432768285274506, -0.0012951319804415107, -0.016614019870758057, 0.005914116278290749, -0.005145595408976078, 0.021295009180903435, -0.03387080132961273, -0.007636301219463348, 0.0040661729872226715, 0.030209850519895554, 0.01903136633336544, 0.012994986027479172, 0.001365870819427073, -0.018584227189421654, 0.024033738300204277, -0.05622776970267296, 0.019115205854177475, -0.0003881902957800776, 0.021336929872632027, -0.02559872530400753, -0.027387283742427826, 0.0023719342425465584, 0.052762437611818314, 0.00943184457719326, -0.021490633487701416, -0.045552317053079605, -0.02709384821355343, -0.031215913593769073, -0.026534924283623695, 0.005414577666670084, -0.013246501795947552, -0.014322430826723576, -0.0002469309256412089, 0.02809991128742695, -0.005051277112215757, 0.024983908981084824, 0.023307137191295624, 0.0026513964403420687, -0.04191930964589119, 0.034932758659124374, 0.03568730503320694, 0.016432369127869606, 0.001386830466799438, -0.008803055621683598, 0.03398258611559868, -0.008551538921892643, 0.021965717896819115, -0.016977321356534958, -0.015104925259947777, 0.010423935018479824, 0.01026324462145567, 0.014699704945087433, -0.020498542115092278, -0.006092273164540529, -0.003674926236271858, 0.03918058052659035, 0.002303815446794033, -0.036274176090955734, -0.05541732907295227, -0.020805951207876205, -0.029064055532217026, 0.008069467730820179, 0.012226465158164501, -0.007094843313097954, -0.015216710045933723, -0.005313272587954998, 0.0016776457196101546, 0.03923647478222847, 0.02273423969745636, -0.009334033355116844, -0.006885246839374304, 0.019310828298330307, -0.028868431225419044, 0.04560821130871773, 0.009634454734623432, -0.029427355155348778, -0.013078824616968632, -0.03317214921116829, 0.013253488577902317, 0.04862640053033829, -0.005456496961414814, -0.016739778220653534, -0.0036050607450306416, -0.054299481213092804, 0.04298126697540283, -0.020652247592806816, -0.046558380126953125, -0.015035059303045273, -0.0005868703592568636, 0.05527759715914726, 0.007475610356777906, 0.02156049944460392, -0.050610579550266266, 0.03817451745271683, 0.01566384918987751, 0.055640898644924164, -0.021784069016575813, -0.014022009447216988, 0.013281434774398804, 0.02730344422161579, 0.016809644177556038, 0.019632210955023766, -0.08171471208333969, -0.030209850519895554, -0.013120744377374649, -0.005735958926379681, 0.024383066222071648, 0.00459016440436244, 0.0033046388998627663, 0.01094792690128088, -0.00961349532008171, -0.018765877932310104, -0.004307209048420191, 0.02494199015200138, 0.001338797970674932, -0.02965092658996582, 0.020987601950764656, -0.05846346542239189, 0.014839435927569866, 0.01548219844698906, 0.010395988821983337, -0.002883699256926775, -0.021420767530798912, 0.022664373740553856, -0.014587920159101486, -0.01632058434188366, 0.021434741094708443, -0.032585278153419495, 0.0009938370203599334, 0.016907455399632454, 0.025738457217812538, 0.013994063250720501, -0.059693098068237305, -0.01263867225497961, 0.03415026515722275, 0.010081593878567219, 0.0038565765134990215, -0.009375952184200287, 0.0474526584148407, -0.008279063738882542, 0.03389874845743179, 0.018626147881150246, 0.0632702112197876, 0.01904533989727497, 0.05522170662879944, 0.04096914082765579, -0.01854230836033821, 0.02224518172442913, 0.08372683823108673, 0.05323752388358116, -0.030070118606090546, 0.002312548691406846, -0.038621656596660614, 0.010605585761368275, -0.034597404301166534, 0.018416550010442734, 0.011143550276756287, 0.035771146416664124, 0.016027148813009262, 0.021295009180903435, 0.036944884806871414, 0.02238491177558899, -0.009816105477511883, -0.03233376145362854, 0.02793223410844803, -0.03537989780306816, -0.015104925259947777, -0.027708664536476135, -0.031076181679964066, -0.046726059168577194, 0.014755597338080406, 0.033311877399683, 0.033759016543626785, -0.010437908582389355, 0.023349056020379066, 0.01103176549077034, -0.039348259568214417, 0.007692193612456322, -0.04499339312314987, 0.04898970201611519, 0.051141560077667236, 0.0024348131846636534, 0.00012226465332787484, -0.0035526615101844072, -0.0312718041241169, 0.004611124284565449, 0.0414721705019474, 0.04661427438259125, 0.0029815109446644783, -0.035771146416664124, -0.00627043005079031, 0.038957010954618454, 0.04259001836180687, 0.019604263827204704, 0.0222312081605196, 0.02509569376707077, -0.0675739273428917, 0.0016069068806245923, 0.02323727123439312, 0.04091324657201767, 0.022315045818686485, 0.02626943401992321, -0.011877138167619705, -0.034597404301166534, -0.02325124479830265, 0.006148165557533503, 0.017857626080513, -0.01028420403599739, -0.00459016440436244, 0.004237343557178974, -0.011395066045224667, 0.0008082566782832146, 0.05435537174344063, 0.04041021689772606, -0.02625546231865883, -0.015538090839982033, 0.034765083342790604, 0.01398008968681097, -0.0007445044466294348, -0.06192879378795624, 0.007049430627375841, -0.025375155732035637, 0.043707866221666336, -0.0008466827566735446, -0.027708664536476135, -0.014434215612709522, -0.04566410183906555, 0.01816503517329693, 0.013267462141811848, 0.016208799555897713, 0.04884997010231018, 0.020778004080057144, -0.01970207504928112, -0.0632702112197876, 0.006647704169154167, 0.00978117249906063, -0.04113681614398956, 0.00744766416028142, 0.02372632920742035, 0.021308982744812965, 0.02459266223013401, 0.020093323662877083, -0.030908504500985146, 0.01430845819413662, -0.00359807419590652, -0.014406269416213036, -0.03266911581158638, 0.0062075513415038586, 0.02425730787217617, 0.04837488383054733, -0.008824015036225319, -0.03744791820645332, -0.00927814096212387, -0.0005340345669537783, 0.02928762510418892, -0.03959977626800537, 0.021336929872632027, 0.03655363991856575, 0.028924323618412018, -0.028491158038377762, -0.016935400664806366, 0.013302394188940525, 0.025389129295945168, -0.010738329961895943, -0.008991692215204239, 0.014217632822692394, -0.05477456748485565, 0.010759289376437664, -0.02156049944460392, -0.01028420403599739, -0.006672156974673271, 0.018416550010442734, 0.00041133322520181537, -0.007887816987931728, -0.0003449609794188291, 0.014965194277465343, 0.02793223410844803, -0.018290791660547256, 0.03616239130496979, 0.016977321356534958, -0.03423410281538963, -0.04175163432955742, -0.0311600212007761, 0.03317214921116829, 0.020819924771785736, -0.021281037479639053, 0.020945681259036064, 0.003400704124942422, 0.03202635422348976, -0.020009484142065048, 0.01918507181107998, 0.02713576704263687, -0.04714525118470192, -0.006256456952542067, -0.03772737830877304, -0.01904533989727497, -0.042506180703639984, 0.02241285890340805, 0.009858024306595325, 0.0008711356786079705, -0.013400206342339516, -0.013365273363888264, 0.022482722997665405, -0.02561269886791706, 0.03286473825573921, 0.014259552583098412, -0.03322803974151611, 0.01632058434188366, -0.012324277311563492, 0.01766200363636017, 0.020847870036959648, -0.04172368720173836, 0.024355119094252586, -0.04122065380215645, -0.03188662230968475, 0.039515938609838486, 0.05863114073872566, 0.0033972107339650393, -0.003350051585584879, -0.020135242491960526, 0.0003742171684280038, -0.016208799555897713, -0.001093395403586328, 0.011499864049255848, -0.0013580110389739275, -0.013260475359857082, 0.0014689224772155285, 0.0030950424261391163, -0.0390687957406044, -0.019324801862239838, 0.016893481835722923, 0.020121268928050995, 0.03244554623961449, 0.005016344133764505, -0.020819924771785736, -0.019660156220197678, -0.005732465535402298, -0.016292639076709747, 0.005687053315341473, 0.024816231802105904, -0.06628840416669846, 0.018612174317240715, -0.003316865535452962, 0.04954862594604492, -0.0015536344144493341, 0.028421292081475258, 0.013253488577902317, -0.01971604861319065, -0.03621828556060791, -0.0012960053281858563, -0.01750829815864563, -0.0038950026500970125, -0.02965092658996582, 0.008628391660749912, -0.04381965100765228, 0.02864486165344715, -0.0033692645374685526, -0.026576843112707138, -0.01110163051635027, 0.0210295207798481, 0.032613225281238556, -0.03621828556060791, 0.0007641540723852813, -0.021518578752875328, 0.02040073089301586, 0.01203782856464386, -0.0036679396871477365, -0.013959130272269249, -0.05399207025766373, -0.02052648924291134, -0.03096439689397812, 0.01917109824717045, 0.07523118704557419, -0.021392822265625, -0.01439229678362608, -0.0024994388222694397, -0.016446342691779137, 0.011087657883763313, -0.01852833479642868, -0.0045866710133850574, -0.01636250503361225, 7.281296711880714e-05, -0.020931709557771683, -0.00951568316668272, -0.03817451745271683, 0.0031544282101094723, -0.044881608337163925, -0.014161740429699421, 0.030349580571055412, -0.001858422765508294, -0.0023439880460500717, 0.0007584775448776782, 0.021798040717840195, 0.012953067198395729, -0.0017562444554641843, 0.03823041170835495, -0.026534924283623695, 0.06276717782020569, 0.010465854778885841, -0.0005995334940962493, -0.048430778086185455, -0.02136487513780594, -0.012184546329081059, -0.013882278464734554, -0.03873344138264656, -0.0066511970944702625, 0.009103477001190186, 0.01280634943395853, -0.002097712131217122, -0.030377527698874474, 0.028071964159607887, 0.05170048400759697, -0.016572101041674614, -0.030936451628804207, -0.005536842159926891, 0.005963021889328957, 0.0028435266576707363, 0.0010960153304040432, -0.023013701662421227, 0.0004329478833824396, -0.023153431713581085, -0.03621828556060791, 0.023544680327177048, 0.005086209625005722, -0.020778004080057144, -0.018807796761393547, 0.012226465158164501, 0.0018444496672600508, -0.029595032334327698, -0.0024959456641227007, -0.005194501020014286, -0.018961502239108086, -0.008607431314885616, -0.026493003591895103, 0.01872395910322666, 0.01736856810748577, -0.009990769438445568, -0.04868229106068611, 0.007503556553274393, 0.02340494841337204, -0.040494054555892944, 0.02006537653505802, -0.015607956796884537, 0.028393346816301346, -0.014951220713555813, -0.012666618451476097, -0.005868703592568636, 0.02948324754834175, -0.02895227074623108, -0.022329019382596016, -0.04952067881822586, 0.008754149079322815, 0.009466777555644512, -0.016124961897730827, 0.02287396974861622, -0.0007759439176879823, -0.024676501750946045, -0.031048236414790154, 0.011066697537899017, 0.032249923795461655, 0.0010479827178642154, 0.011751379817724228, 0.022482722997665405, 0.014420242980122566, -0.03912468999624252, 0.004824214149266481, -0.06271129101514816, 0.023656465113162994, 0.0042268638499081135, -0.0022426832001656294, 0.0011746140662580729, 0.031718943268060684, 0.04172368720173836, -0.050079602748155594, -0.005184021312743425, 0.014853409491479397, -0.0027946208138018847, 0.008991692215204239, -0.0222312081605196, -0.01838860474526882, 0.05796043202280998, 0.011597676202654839, 0.013672681525349617, -0.004073159769177437, -0.025179533287882805, 0.012254411354660988, -0.0017728374805301428, 0.0034391300287097692, 0.0043526217341423035, -0.019967565312981606, 0.014189686626195908, -0.009068544022738934, 0.013120744377374649, -0.014336404390633106, 0.0005270480178296566, -0.0005772638251073658, -0.010409962385892868, 0.012687577866017818, 0.05502608045935631, 0.007929735817015171, -0.010493800975382328, 0.008230158127844334, -0.0011693740962073207, -0.024159496650099754, 0.0003825137100648135, 0.022678347304463387, 0.0015580010367557406, -0.0201911348849535, -0.03624622896313667, -0.004195424262434244, -0.030740827322006226, 0.00842578150331974, -0.005061756819486618, 0.03417821228504181, 0.012561820447444916, -0.05678669363260269, 0.0034496099688112736, -0.01834668405354023, 0.010710383765399456, -0.03401053324341774, 0.008817028254270554, -0.016572101041674614, -0.008740176446735859, -0.0026618761476129293, -0.04057789221405983, 0.016977321356534958, -0.0027457147371023893, 0.005540335550904274, 0.03333982452750206, 0.010305163450539112, -0.029371462762355804, -0.02308356761932373, 0.017270755022764206, 0.005970008671283722, -0.023963872343301773, 0.01103875134140253, 0.01601317711174488, 0.02171420305967331, -0.02543104812502861, -0.001763231004588306, -0.037978895008563995, 0.015342467464506626, 0.01289018802344799, -0.0041011059656739235, 0.02037278562784195, -0.018765877932310104, -0.00311425537802279, 0.005187514703720808, -0.007011004723608494, 0.011066697537899017, -0.02895227074623108, -0.027820449322462082, 0.010039675049483776, -0.0011003819527104497, -0.010570652782917023, -0.020666219294071198, -0.06058737635612488, 0.032054297626018524, -0.06673554331064224, -0.011912071146070957, -0.0064416006207466125, -0.004897572565823793, 0.008984705433249474, -0.0033884774893522263, -0.010724357329308987, -0.01922699064016342, 0.02928762510418892, 0.0017047186847776175, 0.012953067198395729, 0.0009877237025648355, 0.02793223410844803, 0.01801132969558239, -0.012247425504028797, -0.009082517586648464, 0.00035937075153924525, 0.021965717896819115, -0.0004213764041196555, 0.00943184457719326, 0.0319984070956707, 0.01531452126801014, 0.02709384821355343, 0.016544153913855553, -0.022804105654358864, 0.020079350098967552, 0.03185867518186569, -0.027345363050699234, 0.027415229007601738, 0.012869228608906269, 0.020107295364141464, 0.02224518172442913, -0.021267063915729523, -0.006193578243255615, 0.003320358693599701, 0.0027963672764599323, 0.0065359193831682205, 0.039515938609838486, 0.012079748325049877, 0.03549168258905411, 0.0006418894627131522, -0.014091874472796917, -0.01838860474526882, -0.04130449518561363, 0.03809067979454994, 0.01700526662170887, -0.009585549123585224, 0.0007388278609141707, -0.027317417785525322, 0.025403102859854698, -0.0016322331503033638, -0.048263099044561386, 0.006025901064276695, 0.03524016588926315, 0.02811388485133648, -0.0009222248336300254, 0.01447613537311554, 0.022818077355623245, 0.023782221600413322, 0.015621929429471493, -0.022175315767526627, 0.021085413172841072, -0.035435792058706284, 0.010836142115294933, -0.018584227189421654, 0.011793299578130245, -0.0035020089708268642, -0.006004941184073687, 0.012994986027479172, -0.012953067198395729, 0.04155600816011429, 0.028672808781266212, 0.004736882168799639, -0.01632058434188366, 0.014755597338080406, -0.016264691948890686, 0.02410360425710678, 0.01917109824717045, -0.02494199015200138, 0.019967565312981606, -0.025682564824819565, -0.012547846883535385, -0.021155279129743576, -0.0009030118235386908, -0.01389625109732151, -0.007643287535756826, 0.06265539675951004, -0.023880034685134888, -0.0007851137779653072, 0.00711230980232358, 0.020288946107029915, -0.0032557330559939146, 0.012778403237462044, -0.014713677577674389, -0.0349886529147625, -0.019967565312981606, -0.020274972543120384, -0.001649699523113668, 0.03568730503320694, 0.026506977155804634, 0.002441799733787775, -0.012331264093518257, 0.011849191971123219, 0.005299299489706755, 0.03962772339582443, -0.0012872721999883652, 0.03333982452750206, -0.03188662230968475, -0.02107143960893154, -0.0016566860722377896, -0.019324801862239838, -0.024522796273231506, -0.005159568507224321, -0.020945681259036064, 0.007845897227525711, -0.0070040179416537285, -0.011485891416668892, -0.015747686848044395, -0.0007589141605421901, 0.018654093146324158, -0.0198976993560791, 0.029734764248132706, -0.010563666000962257, -0.027513040229678154, 0.02255258895456791, -0.0215744711458683, -0.016250720247626305, -0.0020086336880922318, -0.00106370251160115, -0.011339173652231693, -0.01988372579216957, 0.00359807419590652, 0.02087581716477871, -0.005383138079196215, 0.009830078110098839, -0.011814258992671967, 0.023013701662421227, 0.01599920354783535, 0.015007113106548786, 0.053097791969776154, -0.03621828556060791, -0.042366448789834976, -0.0010060634231194854, 0.019157124683260918, -0.013421165756881237, -0.008586471900343895, 0.004307209048420191, -0.00412905216217041, -0.0005060883704572916, -0.037140510976314545, -0.03378696367144585, -0.011828232556581497, 0.025836268439888954, -0.002668862696737051, 0.033759016543626785, 0.010004742071032524, -0.013022932223975658, -0.017801733687520027, 0.019115205854177475, 0.026856305077672005, -0.009320059791207314, 0.009082517586648464, -0.04027048498392105, -0.013707614503800869, 0.004719415679574013, 0.009005664847791195, 0.03669336810708046, 0.01068243756890297, -0.01506300549954176, -0.02931557036936283, 0.010060634464025497, -0.0018601694609969854, 0.03389874845743179, -0.043540190905332565, -0.03437383472919464, -0.005068743135780096, 0.005005864426493645, -0.010158446617424488, -0.021812014281749725, -0.0006480026640929282, 0.003985827788710594, 0.013868304900825024, 0.047061413526535034, 0.03278090059757233, 0.004359608516097069, 0.03149537369608879, -0.0037273254711180925, 0.017745841294527054, -0.019911672919988632, 0.017480352893471718, 0.027568932622671127, -0.0035159820690751076, 0.026227515190839767, -0.011311227455735207, -0.01061955839395523, 0.02945530228316784, -0.01247099507600069, -0.04298126697540283, 0.010626545175909996, -0.02291589044034481, 0.029874496161937714, 0.018612174317240715, -0.013756520114839077, 0.03976745158433914, -0.006948125548660755, 0.021504607051610947, 0.026339299976825714, -0.019450560212135315, 0.009571575559675694, -0.010912993922829628, 0.008914840407669544, 0.004027747083455324, 0.01817900687456131, -0.02119719795882702, -0.028183748945593834, -0.007342865690588951, -0.011751379817724228, -0.011947003193199635, -0.020344838500022888, -0.01053571980446577, 0.007734112907201052, 0.005550815258175135, 0.013386232778429985, -0.00698655191808939, 0.04395938292145729, 0.0259480532258749, 0.0017309181857854128, -0.006546399090439081, -0.02055443450808525, 0.003842603415250778, 0.008984705433249474, -0.019310828298330307, 0.011485891416668892, 0.029874496161937714, -0.004069666378200054, 0.02188188023865223, 0.015049032866954803, 0.013553909957408905, -0.03487686812877655, -0.018807796761393547, 0.0006707089487463236, 0.005966515280306339, -0.01003268826752901, -0.002199017209932208, 0.035463735461235046, -0.006714076269418001, -0.03266911581158638, 0.007810964714735746, -0.03434588760137558, -0.0251096673309803, -0.04197520390152931, -0.0029745243955403566, 0.024019764736294746, -0.008726202882826328, 0.00048818529467098415, -0.004649550188332796, -0.03744791820645332, 0.00195972784422338, 0.01143698487430811, 0.02843526564538479, -0.019296856597065926, 0.016739778220653534, -0.04594356566667557, 0.06209647282958031, -0.03912468999624252, 0.010298177599906921, -0.005603214725852013, 0.006120219361037016, -0.031215913593769073, -0.03867755085229874, 0.0028190736193209887, -0.04762033745646477, -0.005355191882699728, -0.0008615291444584727, 0.026688627898693085, 0.006172618363052607, -0.020666219294071198, -0.012163586914539337, -0.0037692447658628225, -0.005204981192946434, -0.005267859902232885, 0.03099234402179718, -0.00041133322520181537, 0.008069467730820179, 0.016138935461640358, 0.024704447016119957, -0.0303216353058815, 0.013470071367919445, -0.029231732711195946, 0.07316316664218903, -0.028225669637322426, 0.04988398030400276, -0.03381491079926491, -0.09786761552095413, 0.008146319538354874, 0.012911147437989712, 0.005425057373940945, -0.0016095268074423075, -0.02174214832484722, 0.02578037604689598, -0.0026758492458611727, 0.0069376458413898945, 0.02239888533949852, -0.014378323219716549, 0.01489532832056284, 0.005271353293210268, 0.04845872148871422, 0.03230581432580948, -0.00463208369910717, 0.03015395812690258, 0.02172817662358284, 0.02726152539253235, 0.01682361587882042, 0.014077901840209961, 0.006570851895958185, -0.00951568316668272, 0.03705666959285736, -0.03297652304172516, 0.014420242980122566, -0.0003934302076231688, 0.0026968088932335377, -0.00389150925911963, 0.028868431225419044, 0.0006462560268118978, 0.031187966465950012, 0.0006645957473665476, -0.004177957773208618, 0.0011763606453314424, 0.006193578243255615, 0.017410486936569214, -0.009131423197686672, -0.006259950343519449, 0.0015702275559306145, 0.0044958461076021194, 0.01715897023677826, 0.028896378353238106, -0.03674926236271858, 0.02023305371403694, -0.01971604861319065, 0.03320009261369705, 0.0008837988134473562, -0.0068293544463813305, 0.010249271057546139, 0.010088580660521984, 0.017452405765652657, -0.0035020089708268642, 0.007091349922120571, -0.007412731647491455, -0.004069666378200054, -0.005355191882699728, -0.016124961897730827, 0.009543629363179207, 0.028002100065350533, -0.03297652304172516, -0.04982808604836464, 0.027010008692741394, 0.11681514233350754, -0.0040172673761844635, -0.007817951031029224, 0.004488859325647354, -0.00918032880872488, -0.008272076956927776, -0.0036504731979221106, 0.01950645260512829, -0.004597151186317205, 0.01388926524668932, 0.0010479827178642154, -0.005114155821502209, 0.020344838500022888, 0.005613694433122873, 0.01749432645738125, -0.010458867996931076, -0.024341145530343056, 0.026548895984888077, 0.022440804168581963, -0.018765877932310104, -0.009823091328144073, -0.006899219937622547, -0.025151586160063744, 0.004625097382813692, -0.0016104001551866531, -0.021979691460728645, 0.04208698868751526, -0.00816029217094183, 0.011464931070804596, 0.006172618363052607, 0.01770392246544361, -0.007915763184428215, 0.03233376145362854, -0.002375427633523941, -0.030181903392076492, -0.022846024483442307, -0.018584227189421654, -0.012519900687038898, 0.0227062925696373, 0.004401527810841799, -0.013330340385437012, -0.00018034037202596664, -0.028519105166196823, 0.011360133066773415, 0.042506180703639984, 0.02322329767048359, 0.01597125642001629, -0.0045028324238955975, 0.022650400176644325, 0.05069442093372345, -0.0019737009424716234, -0.0013580110389739275, -0.03565936163067818, -0.013358286581933498, 0.03347955644130707, 0.007203134708106518, 0.018053250387310982, 0.00740574486553669, -0.017382541671395302, -0.008439754135906696, -0.008684284053742886, 0.010165432468056679, -0.0010951419826596975, 0.012904160656034946, -0.008279063738882542, 0.009452804923057556, 0.018137088045477867, -0.032417599111795425, 0.006120219361037016, -0.01094792690128088, 0.008027547970414162, 0.0030251769348978996, -0.00038731697713956237, -0.0029605512972921133, -0.0022374431136995554, 0.022468751296401024, -0.0018968487856909633, -0.03859371319413185, -0.0037692447658628225, -0.010563666000962257, 0.016027148813009262, -0.026059838011860847, -0.023195352405309677, -0.0026304367929697037, -0.024900071322917938, 0.0005912369233556092, -0.0013047385727986693, 0.007101830095052719, 0.037978895008563995, -0.030405472964048386, 0.06528233736753464, 0.005215460900217295, 0.014378323219716549, -0.027163714170455933, 0.010312150232493877, 0.002871472854167223, -0.031048236414790154, 0.019841806963086128, -0.017815707251429558, -0.01556603703647852, 0.03062904253602028, -0.017927492037415504, -0.010172419250011444, -0.0009510443778708577, -0.016055095940828323, 0.02443895861506462, -0.003547421656548977, 0.021141305565834045, -0.013246501795947552, -0.03674926236271858, -0.005442523863166571, -0.016474289819598198, 0.021923799067735672, 0.007943709380924702, -0.02526337094604969, -0.006885246839374304, 0.006822367664426565, -0.028044018894433975, -0.03582703694701195, 0.017256783321499825, 0.02122514508664608, 0.07942312210798264, 0.028016071766614914, -0.012023855932056904, 0.022454777732491493, 0.005917609203606844, 0.003870549611747265, 0.0299583338201046, 0.010242285206913948, -0.00459016440436244, 0.015887418761849403, -0.0073987580835819244, 0.0360785536468029, -0.010004742071032524, -0.010242285206913948, -0.027736609801650047, -0.010731343179941177, -0.02239888533949852, 0.015607956796884537, -0.011653568595647812, 0.04778801277279854, 0.02090376242995262, -0.00681887473911047, -0.04272975027561188, -0.016055095940828323, 0.05038700997829437, 0.024019764736294746, 0.005798838101327419, -0.025738457217812538, -0.03347955644130707, 0.014797516167163849, 0.024983908981084824, -0.04194725677371025, -0.032585278153419495, 0.0010951419826596975, 0.0010322630405426025, 0.004115079063922167, 0.0015169550897553563, 0.0043456354178488255, -0.014546000398695469, -0.014937248080968857, 0.008824015036225319, 0.02207750268280506, -0.023963872343301773, 0.014629838988184929, -0.010444894433021545, 0.0011929536703974009, -0.00918032880872488, -0.022594507783651352, -0.034122318029403687, -0.004272276535630226, 0.01087107416242361, -0.02559872530400753, -0.02360057272017002, -0.02512364089488983, 0.03633007034659386, 0.005645134020596743, -0.007552462629973888, 0.004998877644538879, 0.01838860474526882, -0.031914569437503815, 0.011220402084290981, 0.017731867730617523, 0.0018566761864349246, 0.010326123796403408, 0.011129576712846756, -0.030070118606090546, -0.01601317711174488, 0.007650274317711592, -0.03280884772539139, -0.02830950729548931, 0.038621656596660614, -0.020093323662877083, 0.009823091328144073, -0.05354493111371994, 0.028588969260454178, -0.0010471094865351915, 0.019157124683260918, -0.007601368241012096, -0.016460316255688667, -0.04736882075667381, -0.0017012254102155566, 0.03356339409947395, 0.031215913593769073, -0.01685156300663948, -0.004890586249530315, 0.020959654822945595, 0.025724483653903008, 0.005355191882699728, -0.025500914081931114, -0.026688627898693085, 0.041360385715961456, 0.0007589141605421901, 0.008272076956927776, 0.04060583934187889, 0.006514959502965212, -0.005232927389442921, 0.0022601494565606117, -0.006654690485447645, 0.008824015036225319, 0.009962823241949081, -0.009746239520609379, -0.02680041268467903, -0.00723806768655777, -0.01868204027414322, 0.013546924106776714, -0.009068544022738934, -0.027722638100385666, -0.00016483895888086408, 0.014091874472796917, -0.006899219937622547, 0.011632608249783516, -0.030880559235811234, -0.025165559723973274, 0.0073987580835819244, 0.020931709557771683, -0.0239079799503088, 0.009005664847791195, -0.04476982355117798, 0.006319336127489805, -0.02696808986365795, -0.000489931961055845, -0.02357262559235096, 0.019436586648225784, -0.028547050431370735, 0.0481792613863945, -0.012617712840437889, -0.009215261787176132, 0.008719217032194138, 0.0190593134611845, -0.01636250503361225, -0.01289018802344799, -0.013106770813465118, -0.0021763108670711517, -0.00992789026349783, -0.025682564824819565, -0.014105848036706448, -0.0009947102516889572, 0.0003154864825773984, 0.008914840407669544, 0.010996832512319088, 0.015202736482024193, 0.023866061121225357, -0.02320932410657406, 0.015454252250492573, -0.016558127477765083, -0.0038530833553522825, 0.03096439689397812, 0.01650223508477211, 0.027219606563448906, -0.024648554623126984, 0.0024103603791445494, -0.040382269769907, -0.008551538921892643, -0.006319336127489805, -0.004663523286581039, 0.02070813998579979, -0.019129179418087006, 0.015188763849437237, 0.013085811398923397, 0.019995510578155518, 0.02090376242995262, -0.05304190143942833, 0.036609530448913574, -0.03244554623961449, -0.01541233342140913, -0.022692320868372917, 0.03373107314109802, -0.034094370901584625, -0.008824015036225319, -0.00479976087808609, -0.013630762696266174, 0.010752303525805473, 0.018123114481568336, 0.024313200265169144, 0.031215913593769073, -0.009375952184200287, -0.06332610547542572, -0.03266911581158638, 0.014175713062286377, 0.012820322066545486, -0.03959977626800537, 0.05164458975195885, -0.019087258726358414, 0.018123114481568336, 0.0390687957406044, -0.0012139133177697659, 0.054495103657245636, -0.01255483366549015, 0.013525963760912418, 0.014462161809206009, -0.0010104300454258919, -0.024299226701259613, -0.013001972809433937, 0.019590290263295174, 0.0002868852752726525, 0.03926442191004753, 0.012016869150102139, 0.008146319538354874, 0.015915364027023315, 0.006616264581680298, -0.0003947401710320264, -0.020652247592806816, -0.002052299678325653, -0.008313996717333794, 0.0025797842536121607, -0.014685731381177902, 0.03456945717334747, 0.054662782698869705, -0.03638596087694168, 0.014783543534576893, -0.010486814193427563, 0.012820322066545486, 0.00934102013707161, -0.007496570236980915, -0.0033255985472351313, -0.03940415009856224, 0.028561023995280266, -0.022692320868372917, -0.03688899427652359, -0.019576318562030792, 0.013952143490314484, 0.04334456846117973, 0.026199569925665855, 0.01439229678362608, 0.0282815620303154, 0.01339321956038475, 0.009452804923057556, -0.024704447016119957, 0.012491954490542412, 0.00025173419271595776, -0.020302919670939445, 0.01884971745312214, 0.048402830958366394, 0.0227062925696373, 0.0006069566588848829, 0.04803952947258949, -0.03247349336743355, -0.004932505544275045, -0.019757969304919243, 0.04820720851421356, -0.0267864391207695, 0.03247349336743355, 0.015593983232975006, 0.005928089376538992, -0.006598798092454672, -0.02204955741763115, 0.027582906186580658, 0.01870998553931713, -0.022342992946505547, -0.02626943401992321, -0.008712230250239372, 0.030209850519895554, -0.02675849385559559, -0.008621404878795147, -0.019660156220197678, 0.01956234499812126, 0.011772340163588524, -0.015021086670458317, -0.03300447016954422, 0.013651722110807896, -0.01715897023677826, 0.03532400727272034, 0.0349886529147625, 0.026674654334783554, 0.007629314437508583, 0.007776032201945782, -0.008167278952896595, 0.0218679066747427]
+,
+ "random text": [-0.005331182852387428, 0.005700631532818079, 0.004485026467591524, 0.001735017984174192, -0.03731033578515053, -0.07442998886108398, -0.017590519040822983, 0.005108719225972891, -0.023104440420866013, -0.0058992598205804825, 0.005883369594812393, 0.010304834693670273, -0.02029186487197876, 0.031875863671302795, 0.030763547867536545, 0.0010279013076797128, -0.021960342302918434, -0.029412874951958656, -0.053264155983924866, 0.02555154077708721, 0.01746339723467827, 0.02714056707918644, -0.01828969083726406, -0.002059775171801448, -0.005037213210016489, 0.012656593695282936, 0.017812984064221382, 0.01147276908159256, 0.044460952281951904, -0.025503870099782944, 0.0008297695894725621, -0.044905878603458405, 0.029031507670879364, -0.028538910672068596, 0.008358277380466461, -0.00484653003513813, 0.023867173120379448, 0.007603490259498358, 0.03400516137480736, -0.0056370701640844345, -0.005966793280094862, -0.03546706587076187, 0.044651634991168976, 0.045795734971761703, 0.008739643730223179, 0.01981515623629093, -0.02869781292974949, -0.0033746943809092045, 0.027950970456004143, 0.05653754994273186, -0.0216425359249115, 0.03775526210665703, 0.0002552373334765434, 0.06623061001300812, -0.011870025657117367, -0.0015522799221798778, -0.029730679467320442, 0.04865598306059837, -0.015254651196300983, -0.017097922042012215, 0.02381950244307518, -0.014142332598567009, -0.013260423205792904, 0.02270718477666378, 0.03267037868499756, 0.020991036668419838, -0.039789214730262756, 0.02758549526333809, 0.015302321873605251, 0.05205649882555008, 0.005859534256160259, 0.005795972887426615, -0.0493551529943943, 0.008660192601382732, -0.029492326080799103, -0.0002967009786516428, -0.003698458429425955, -0.017352165654301643, 0.001850222353823483, 0.02578989416360855, 0.032289013266563416, 0.02981013059616089, -0.033846259117126465, -0.016041219234466553, -0.0669933408498764, -0.01411055214703083, -0.09286268800497055, -0.008676082827150822, -0.002548400778323412, -0.014706437475979328, -0.03753279894590378, 0.004616121295839548, -0.050817057490348816, 0.0027370976749807596, -0.016923129558563232, -0.00837416760623455, 0.00029297670698724687, 0.03301996365189552, 0.03240024298429489, -0.024645796045660973, 0.008739643730223179, -0.04916447028517723, -0.006582540925592184, 0.02095925621688366, -0.011782629415392876, 0.011663451790809631, -0.01652587205171585, -0.04439739137887955, -0.027649056166410446, 0.022182805463671684, -0.06534075736999512, -0.016366969794034958, 0.0046200938522815704, 0.010122097097337246, 0.003676609368994832, -0.022452939301729202, 0.04836995527148247, 0.004095715004950762, 0.0064077479764819145, -0.029428765177726746, -0.006157476454973221, 0.0056569334119558334, 0.021912671625614166, -0.033623792231082916, -0.01477794349193573, -0.025678662583231926, -0.013506722636520863, -0.024089636281132698, -0.0062607633881270885, 0.017812984064221382, 0.043285071849823, -0.018353251740336418, -0.006503089796751738, -0.014611096121370792, -0.05507564917206764, 0.01334782037883997, -0.0024768945295363665, 0.005553646478801966, -0.048624202609062195, 0.028840824961662292, 0.0051166643388569355, 0.002816548803821206, -0.017383946105837822, -0.01833736151456833, -0.004361876752227545, -0.03042985126376152, -0.024550454691052437, -0.0038891416043043137, 0.015175200067460537, 0.014356851577758789, -0.04649490490555763, 0.03769170120358467, -0.03886758163571358, 0.02693399414420128, 0.020037619397044182, -0.025233736261725426, 0.03613445535302162, 0.008914437144994736, -0.03823196887969971, 0.0014628972858190536, -0.018210239708423615, -0.022151025012135506, -0.009899633005261421, -0.002923808293417096, -0.011885915882885456, -0.002727166283875704, 0.048211053013801575, 0.0007642222917638719, -0.0714426189661026, 0.02113404870033264, -0.0056370701640844345, -0.06085970252752304, -0.04894200712442398, -0.003315105801448226, -0.03220956027507782, 0.028078092262148857, 0.009883742779493332, -0.026091810315847397, -0.019529132172465324, 0.05065815523266792, 0.04096509516239166, 0.06397419422864914, 0.0280145313590765, -0.003813662799075246, -0.009399089962244034, -0.055679477751255035, 0.03136737644672394, -0.0167483352124691, -0.0019684061408042908, 0.009661279618740082, -0.012894947081804276, 0.020895693451166153, 0.034736111760139465, -0.0020677202846854925, -0.011528384871780872, -0.024995381012558937, 0.061876680701971054, 0.007206233683973551, -0.006137613672763109, 0.021277060732245445, 0.036070894449949265, 0.04052016884088516, -0.022119244560599327, -0.012291117571294308, -0.06724758446216583, -0.055234551429748535, -0.034036941826343536, 0.016478201374411583, -0.006455418653786182, 0.009033613838255405, 0.10366807132959366, -0.05717316269874573, -0.02715645730495453, -0.02822110429406166, -0.04662202671170235, -0.04214097559452057, 0.05361374467611313, 0.062067363411188126, 0.003255517454817891, -0.010336615145206451, -0.02756960503757, 0.05164334923028946, 0.04252234101295471, 0.025646882131695747, -0.004492971580475569, 0.015929987654089928, 0.056346867233514786, -0.0030429852195084095, -0.00699568772688508, 0.04271302372217178, 0.060764361172914505, 0.00837416760623455, 0.005482140462845564, -0.019529132172465324, 0.05205649882555008, 0.024598125368356705, -0.0012116325087845325, 0.014595205895602703, -0.009327583946287632, 0.030032595619559288, -0.01673244498670101, -0.016200121492147446, 0.03514925763010979, 0.00748828612267971, 0.022198695689439774, -0.029285753145813942, -0.005664878524839878, -0.022389378398656845, 0.03292462229728699, -0.0088190957903862, 0.021753769367933273, -0.005462277680635452, -0.023024989292025566, -0.009367309510707855, 0.01900475285947323, -0.03775526210665703, -0.006848702672868967, 0.03565774857997894, 0.026790982112288475, -0.03524459898471832, -0.03613445535302162, -0.053709086030721664, -0.06305255740880966, -0.003289284184575081, -0.01035250537097454, -0.038899362087249756, 0.023485807701945305, -0.01916365511715412, 0.03368735685944557, -0.049958981573581696, -0.02803042158484459, -0.011734958738088608, 0.0027510016225278378, 0.024598125368356705, -0.017034361138939857, 0.03505391627550125, 0.003039012663066387, -0.007543901912868023, -0.009430870413780212, 0.03470433130860329, -0.02404196560382843, 0.02203979343175888, -0.022993208840489388, -0.019338449463248253, -0.031859975308179855, -0.0027966860216110945, 0.03645225986838341, 0.005553646478801966, 0.04538258910179138, -0.006999660283327103, -0.0016754295211285353, -0.023978404700756073, 0.04303082823753357, -0.02515428513288498, -0.003992428071796894, 0.020339535549283028, 0.02582167461514473, 0.02362881973385811, -0.017415726557374, 0.0021094323601573706, 0.05227896198630333, -0.010296889580786228, 0.012187831103801727, 0.01607299968600273, -0.01236262358725071, 0.04077441245317459, -0.008028554730117321, -0.004902145825326443, 0.025630991905927658, -0.01719326339662075, -0.009645389392971992, 0.04811571165919304, -0.011544275097548962, 0.007528011687099934, -0.03861333802342415, -0.021229390054941177, 0.009907578118145466, 0.05850794538855553, 0.04109221696853638, -0.07404862344264984, 0.06839168816804886, -0.03654760122299194, 0.07042563706636429, -0.018226129934191704, -0.005641043186187744, -0.0006142579368315637, 0.05752274766564369, 0.028824934735894203, 0.049990762025117874, -0.010845103301107883, -0.018655167892575264, -0.03683362528681755, 0.032114218920469284, 0.008278826251626015, 0.07290451973676682, -0.05803123489022255, -0.0040023596957325935, 0.022786635905504227, 0.03314708545804024, -0.00846950989216566, -0.03416406363248825, 0.013474942184984684, 0.013316038995981216, -0.04503300040960312, -0.03197120502591133, -0.04433383047580719, 0.005815835669636726, -0.00883498601615429, -0.001370535115711391, 0.025519760325551033, -0.007524039130657911, 0.024884149432182312, 0.005366935860365629, -0.06457802653312683, 0.003805717686191201, -0.017320385202765465, 0.02558332122862339, 0.024852368980646133, 0.010217438451945782, 0.020784461870789528, -0.0022822387982159853, 0.03365557640790939, 0.008898546919226646, -0.009454705752432346, 0.03902648389339447, 0.010694146156311035, -0.040202364325523376, 0.0066818553023040295, 0.01357028353959322, -0.03263859823346138, -0.038263749331235886, 0.01606505550444126, -0.016430530697107315, 0.01657354272902012, -0.026123590767383575, 0.007762392982840538, -0.004131468012928963, 0.026806872338056564, 0.0388358011841774, 0.043380413204431534, -0.013093575835227966, 0.015826700255274773, -0.02953999675810337, -0.027712617069482803, 0.043984245508909225, 0.021499523892998695, 0.014714382588863373, -0.007285685278475285, -0.048814885318279266, -0.023120330646634102, 0.000954408838879317, 0.005811863113194704, -0.02181733027100563, 0.016621213406324387, -0.02273896522819996, 0.005466250237077475, -9.923654943122528e-06, 0.017018470913171768, -0.03181230276823044, 0.0018720715306699276, 0.03753279894590378, 0.018146678805351257, -0.045541491359472275, 0.022993208840489388, -0.022897867485880852, 0.034259404987096786, -0.034958574920892715, 0.03505391627550125, 0.06959934532642365, 0.027188237756490707, 0.047066956758499146, -0.028364118188619614, 0.03282928094267845, 0.019767485558986664, -0.006304461508989334, 0.012966454029083252, 0.07837077230215073, 0.0028900413308292627, -0.019910497590899467, 0.05065815523266792, -0.026997555047273636, -0.022770745679736137, -0.03236846253275871, -0.00969306007027626, -0.007814036682248116, 0.05056281387805939, 0.04827461391687393, -0.019243106245994568, -0.010908665135502815, -0.028348227962851524, -0.001671456964686513, -0.035212818533182144, -0.011385372839868069, 0.0032753802370280027, 0.03832731023430824, 0.00045833474723622203, -0.01722504384815693, 0.03505391627550125, -0.02645728550851345, -0.04052016884088516, 0.0012156050652265549, 0.03279750049114227, -0.06673909723758698, 0.00020235255942679942, -0.00939114484935999, 0.015953822061419487, 0.003432296449318528, 0.019703924655914307, -0.010280999355018139, -0.056346867233514786, -0.0016913197468966246, -0.016589432954788208, -0.00010266598837915808, -0.03654760122299194, 0.039820995181798935, -0.02626660279929638, 0.024550454691052437, 0.01411055214703083, -0.010344560258090496, 0.01564396359026432, 0.0027251800056546926, -0.0176381915807724, -0.023072659969329834, 0.006896373815834522, 0.01937022991478443, -0.06200380250811577, -0.019783375784754753, 0.036515820771455765, 0.03896292299032211, -0.009653334505856037, -0.017097922042012215, 0.007857734337449074, 0.011854135431349277, 0.012410294264554977, -0.009851962327957153, -0.015246706083416939, 0.025758113712072372, -0.05072171613574028, -0.004802831448614597, 0.019306669011712074, 0.016716554760932922, -0.0037620195653289557, 0.014190004207193851, -0.01058291457593441, -0.020148852840065956, 0.08510824292898178, 0.01355439331382513, 0.016923129558563232, 0.02424854040145874, 0.0005834705661982298, -0.02006939984858036, 0.033210646361112595, 0.03220956027507782, -0.020784461870789528, 0.00414338568225503, -0.026647968217730522, 0.003185997484251857, -0.02202390320599079, -0.010574969463050365, -0.055711258202791214, -0.006737471092492342, 0.004481053911149502, -0.024534564465284348, 0.019735705107450485, 0.03565774857997894, 0.028109872713685036, -0.011321811936795712, 0.005418579094111919, 0.007194316014647484, 0.048179272562265396, 0.018559826537966728, -0.014277400448918343, -0.0021710069850087166, -0.008556906133890152, -0.011361537501215935, -0.0003848422784358263, -0.031160803511738777, -0.004119550343602896, -0.02315211109817028, -0.05866684764623642, 0.038899362087249756, 0.03546706587076187, 0.015079858712852001, -0.015024242922663689, 0.01420589443296194, -0.04185495153069496, -0.03238435462117195, -0.06019231304526329, -0.018226129934191704, 0.0011560166021808982, 0.011989202350378036, 0.0014648835640400648, 0.06457802653312683, 0.023326903581619263, 0.0043539316393435, -0.03588021174073219, -0.006403775420039892, 0.0029396985191851854, 0.0343865267932415, 0.008111978881061077, 0.013784802518785, 0.03235257416963577, 0.03565774857997894, -0.02137240208685398, -0.04744832217693329, 0.03689718618988991, 0.028999727219343185, -0.011870025657117367, 0.005712549202144146, -0.017606409266591072, 0.008660192601382732, -0.005915150046348572, -0.0018720715306699276, -0.021245280280709267, 0.02753782458603382, -0.0014251578832045197, -0.0062965163961052895, 0.02691810391843319, 0.03950319066643715, 0.011377427726984024, 0.009542101994156837, -0.002341827377676964, 0.005132554564625025, 0.005494058132171631, -0.011798519641160965, 0.003779896069318056, -0.02760138548910618, 0.05930245667695999, 0.03556240722537041, -0.03699253126978874, 0.013776857405900955, 0.014134387485682964, 0.004643929190933704, -0.04341219365596771, -0.005593372043222189, -0.005168307572603226, 0.020021729171276093, -0.004071879666298628, -0.02402607537806034, 0.006455418653786182, -0.010956335812807083, 0.03336954861879349, 0.026377834379673004, 0.025440309196710587, -0.03390982002019882, 0.0370560921728611, 0.011417153291404247, -0.02359703928232193, -0.006963907275348902, 0.011758794076740742, 0.03708787262439728, 0.053677305579185486, -0.004020236432552338, 0.02974656969308853, -0.0021054598037153482, 0.02534496784210205, -0.0113377021625638, -0.03565774857997894, -0.030986011028289795, -0.005144472233951092, 0.026981664821505547, -0.0003495857527013868, 0.004123522900044918, -0.012195776216685772, 0.028284665197134018, 0.012426184490323067, -0.0034640771336853504, -0.021944452077150345, 0.01454753428697586, -0.04697161540389061, -0.0019783375319093466, 0.010988116264343262, 0.0047194077633321285, -0.005823780782520771, -0.024200869724154472, 0.014277400448918343, -0.023088550195097923, -0.013546448200941086, 0.009867852553725243, 0.022866087034344673, -0.03845443204045296, 0.010670310817658901, 0.04169604554772377, -0.01266453880816698, -0.04614531993865967, 0.013093575835227966, -0.02029186487197876, -0.022405268624424934, 0.017511067911982536, -0.012871111743152142, 0.005696658976376057, 0.0007825954235158861, -0.011901806108653545, -0.02931753359735012, 0.0007955062319524586, -0.017082031816244125, 0.02582167461514473, 0.005557619035243988, 0.00031209466396830976, -0.022897867485880852, 0.025758113712072372, 0.003495857585221529, 0.0031144912354648113, -0.0419502928853035, 0.05704604089260101, -0.028793154284358025, 0.0026298384182155132, 0.010416066274046898, 0.00883498601615429, -0.03565774857997894, -0.006038299296051264, -0.005418579094111919, 0.001958474749699235, -0.022611843422055244, -0.009891687892377377, -0.02758549526333809, -0.014984517358243465, -0.01739983633160591, -0.004941871389746666, -0.0280145313590765, 0.006495144683867693, -0.006169394124299288, 0.008564851246774197, -0.01965625397861004, -0.037818823009729385, 0.05030857026576996, -0.027172347530722618, -0.023469917476177216, -0.014078771695494652, 0.024407442659139633, 0.015516840852797031, 0.027315359562635422, -0.003483939915895462, -0.0036825682036578655, 0.020863912999629974, 0.012807550840079784, -0.004711462650448084, -0.048624202609062195, 0.010193603113293648, -0.0001008659164654091, 0.02580578438937664, -0.0040261950343847275, -0.02272307500243187, -0.016414640471339226, -0.026155371218919754, 0.04185495153069496, 0.025058943778276443, 0.017288604751229286, -0.04188673198223114, -0.026409614831209183, -0.043571099638938904, -0.021944452077150345, 0.04039304703474045, 0.030159717425704002, 0.005355018191039562, -0.013864253647625446, 0.00991552323102951, 0.022119244560599327, -0.025630991905927658, -0.010773597285151482, 0.007476367987692356, 0.010503463447093964, -0.01388808898627758, 0.012926728464663029, 0.020180633291602135, -0.053454842418432236, 0.027490153908729553, 0.005275567062199116, 0.024995381012558937, 0.003994414582848549, 0.003920922055840492, -0.005335155408829451, -0.01678011566400528, -0.007806091103702784, -0.015731358900666237, 0.020164743065834045, 0.016827788203954697, -0.026568517088890076, -0.014245619997382164, 0.0024749082513153553, 0.01125825010240078, 0.022452939301729202, 0.017924215644598007, -0.02315211109817028, -0.022627733647823334, -0.038263749331235886, 0.01412644237279892, -0.04592285677790642, -0.019052423536777496, 0.04871954396367073, 0.011687287129461765, 0.012338788248598576, 0.045319028198719025, -0.004671737086027861, -0.007158563006669283, -0.0012096462305635214, -6.697497155983001e-05, -0.0016744363820180297, 0.002308060647919774, -0.014356851577758789, -0.004643929190933704, -0.010082371532917023, 0.013498777523636818, 0.031446829438209534, -0.006312406621873379, 0.02892027609050274, 0.03486323356628418, -0.00828677136451006, -0.0066937729716300964, -0.0013099535135552287, -0.054821401834487915, 0.01533410232514143, -0.01568368822336197, -0.01894119195640087, -0.012569197453558445, -0.03413228318095207, 0.029381094500422478, -0.0013953635934740305, 0.005482140462845564, 0.0816759467124939, -0.000350330607034266, 0.031192583963274956, 0.0010080385254696012, 0.03772348165512085, -0.02006939984858036, -0.023867173120379448, -0.008084170520305634, 0.0054900855757296085, -0.028793154284358025, 0.011822354979813099, 0.003992428071796894, -0.012529471889138222, -0.017574628815054893, 0.01046373788267374, 0.02075268141925335, -0.006062135100364685, -0.03524459898471832, 0.032543256878852844, 0.006983770057559013, -0.005013377405703068, -0.017066141590476036, 0.012370568700134754, -0.0244551133364439, 0.022182805463671684, -0.005084883887320757, -0.026330163702368736, 0.02841178886592388, 0.0049657067283988, -0.0013268368784338236, -0.005942957941442728, 0.0027589467354118824, -0.04392068460583687, 0.038263749331235886, -0.02981013059616089, -0.0056569334119558334, 0.015500950627028942, 0.0032177779357880354, -0.06985358893871307, 0.0251225046813488, 0.004667764529585838, 0.018210239708423615, 0.0044413283467292786, 0.016398750245571136, 0.0366111621260643, 0.020863912999629974, -0.005243786610662937, -0.023724161088466644, 0.04296726733446121, -0.01746339723467827, -0.03460898995399475, -0.01246591005474329, 0.018798179924488068, -0.016621213406324387, 0.00639980286359787, -0.02736303023993969, -0.031033681705594063, -0.016160396859049797, -0.031891755759716034, -0.005331182852387428, 0.021515414118766785, -0.017304494976997375, -0.020800352096557617, 0.002729152562096715, -0.0026854542084038258, 0.018671058118343353, -0.0023279234301298857, -0.003223736770451069, 0.053264155983924866, 0.0052278959192335606, 0.02205568365752697, -0.025217846035957336, 0.012171940878033638, -0.05269210785627365, -0.02536085806787014, -0.015151364728808403, 0.019258996471762657, -0.011234414763748646, -0.007603490259498358, -0.04878310486674309, -0.010654420591890812, 0.03257503733038902, -0.022103354334831238, 0.0004958258359692991, -0.000931070011574775, -0.012751935049891472, -0.040202364325523376, -0.028173433616757393, -0.009701005183160305, 0.001088482909835875, 0.042236316949129105, -0.02714056707918644, -0.00748828612267971, 0.05644220858812332, -0.045986417680978775, -0.01214016042649746, -0.00556159159168601, -0.011409208178520203, 0.0001017969916574657, -0.00825499091297388, 0.0029377122409641743, 0.006491172127425671, 0.01376891229301691, -0.03292462229728699, -0.007579654920846224, 0.01922721602022648, 0.037373896688222885, -0.01851215586066246, -0.012148105539381504, 0.014539589174091816, -0.01585053652524948, 0.0273471400141716, 0.008628412149846554, -0.04703517630696297, 0.03359201177954674, 0.0167483352124691, 0.012426184490323067, -0.043285071849823, 0.03600733354687691, -0.0005129575147293508, -0.013745076954364777, 0.004671737086027861, -0.003448186907917261, -0.004175166133791208, 0.023692380636930466, -0.007126782555133104, -0.04191851243376732, 0.0044770813547074795, 0.05977916345000267, -0.004131468012928963, -0.008755533955991268, -0.020848022773861885, -0.012275227345526218, -0.030986011028289795, -0.02532907761633396, 0.00776636553928256, -0.0024391552433371544, -0.03632513806223869, 0.02142007276415825, -0.03622979670763016, -0.04271302372217178, -0.032988183200359344, -0.010487573221325874, 0.03109724260866642, -0.03416406363248825, 0.03731033578515053, 0.01724093407392502, -0.054122231900691986, 0.029873691499233246, -0.002027001464739442, 0.007035413291305304, 0.016223957762122154, 0.009033613838255405, 0.030255058780312538, 0.029079178348183632, 0.00388318276964128, -0.0019684061408042908, -0.03200298920273781, -0.011004006490111351, 0.0016555666225031018, 0.014372741803526878, -0.021324731409549713, -0.0415053628385067, -0.010408121161162853, -0.02578989416360855, 0.016907239332795143, 0.018003666773438454, -0.005378853529691696, 0.021992122754454613, -0.0018710782751441002, -0.006181311793625355, 0.009732785634696484, -0.0036547603085637093, -0.028602471575140953, 0.0012593033025041223, 0.062321607023477554, -0.01635107956826687, 0.03460898995399475, -0.0020399123895913363, 0.0077186948619782925, 0.0167483352124691, -0.011552220210433006, 0.00803252775222063, -0.023962514474987984, 0.021277060732245445, 0.02092747576534748, -0.011949476785957813, -0.00180553097743541, 0.015485060401260853, -0.01943379081785679, -0.011989202350378036, -0.012386458925902843, -0.004421465564519167, 0.030302729457616806, -0.0223099272698164, 0.023120330646634102, -0.03486323356628418, 0.05666467547416687, -0.0020935419015586376, 0.044905878603458405, 0.034291185438632965, -0.002790727186948061, 0.04166426509618759, 0.008612521924078465, -0.04121933877468109, 0.013109466060996056, -0.0401705801486969, 0.010948390699923038, -0.0013864253414794803, 0.02092747576534748, 0.018257910385727882, 0.00046727299923077226, 0.0023398410994559526, 0.016382860019803047, 0.0062965163961052895, -0.021245280280709267, 0.026568517088890076, 0.02888849563896656, 0.011814409866929054, 0.022850196808576584, -0.0352763831615448, 0.029619447886943817, 0.016192177310585976, 0.009955248795449734, -0.024757027626037598, 0.03282928094267845, -0.021531304344534874, 0.053486622869968414, -0.0015165269142016768, -0.0016208067536354065, 0.012442074716091156, -0.005025295540690422, -0.0068924012593925, -0.024550454691052437, 0.01357028353959322, 0.0061296685598790646, -0.026552626863121986, -0.02114993892610073, -0.04233165830373764, 0.004151330795139074, 0.01962447352707386, 0.0040679071098566055, 0.011075512506067753, 0.03556240722537041, -0.02737892046570778, 0.00040495340363122523, 0.007182398345321417, 0.016494091600179672, 0.012457964941859245, 0.01014593243598938, -0.028761373832821846, -0.01765408180654049, 0.029222192242741585, -0.010574969463050365, -0.04627244174480438, 0.028602471575140953, -0.032527364790439606, -0.00012097703438485041, -0.003289284184575081, -0.05011788755655289, 0.000416126218624413, -0.0020081319380551577, -0.0021551167592406273, 0.021563084796071053, -0.056982479989528656, 0.05297813192009926, -0.03333776816725731, 0.006979797501116991, 0.003912976942956448, -0.01897297240793705, 0.014158223755657673, 0.0034243513364344835, 0.03106546215713024, 0.01214016042649746, 0.006951989606022835, -0.0007299588760361075, 0.02165842615067959, -0.01346699707210064, -0.01565190777182579, -0.012171940878033638, -0.023692380636930466, -0.010090316645801067, 0.004175166133791208, -0.009971139021217823, -0.008008692413568497, -0.03902648389339447, -0.01676422543823719, 0.02294553816318512, -0.01940201036632061, -0.005565564148128033, 0.020609669387340546, 0.02561510168015957, -0.005271594505757093, 0.01590615138411522, -0.003817635355517268, 0.020005838945508003, -0.011989202350378036, 0.03279750049114227, -0.006876511033624411, -0.006252817809581757, 0.02207157388329506, -0.01717737317085266, -0.0008943238062784076, 0.011131128296256065, -0.020371316000819206, 0.0027629192918539047, 0.021896781399846077, -0.02887260541319847, -0.019147764891386032, 0.018432702869176865, 0.019513241946697235, -0.0025166203267872334, -0.0026357972528785467, -0.01521492563188076, 0.020005838945508003, 0.003813662799075246, -0.01488122995942831, 0.02383539266884327, -0.0484653003513813, 0.020832132548093796, -0.03845443204045296, -0.0017201208975166082, 0.013832473196089268, 0.001922721741721034, -0.001358617446385324, -0.0007696845568716526, 0.00958182755857706, 0.04496943950653076, 0.0029635338578373194, -0.006078025326132774, 0.017256824299693108, -0.000867508992087096, -0.03816840797662735, -0.00020086283620912582, 0.0036547603085637093, -0.009152790531516075, 0.016231901943683624, 0.014277400448918343, 0.006808977108448744, 0.00816759467124939, 0.01987871713936329, -0.010185658000409603, 0.0025940851774066687, 0.0024153199046850204, -3.3984055335167795e-05, 0.0019455639412626624, -0.010447846725583076, -0.03346488997340202, 0.006773224100470543, 0.008239100687205791, 0.011957421898841858, 0.007472395431250334, -0.0024649768602102995, -0.009788401424884796, 0.015143419615924358, -0.011822354979813099, 0.011933586560189724, -0.02450278401374817, -0.04427026957273483, 0.008310606703162193, 0.021674316376447678, 0.030318619683384895, 0.0026794953737407923, 0.033846259117126465, -0.022119244560599327, -0.0068050045520067215, 0.0001555507624289021, 0.015786975622177124, -0.019703924655914307, -0.006340214516967535, -0.004854475148022175, 0.016891349107027054, 0.0007086063851602376, -0.016700664535164833, 0.020212413743138313, -0.01654176227748394, -0.0004928464186377823, 0.002651687478646636, 0.03511747717857361, -0.005311320070177317, -0.011877970770001411, 0.027474261820316315, -0.008652247488498688, 0.00556159159168601, -0.011480714194476604, -0.017590519040822983, 0.0057681649923324585, -0.012807550840079784, 0.1339866816997528, 0.004910090938210487, -0.037151433527469635, -0.013141246512532234, 0.011798519641160965, -0.010471682995557785, -0.012283172458410263, -0.009073339402675629, -0.024884149432182312, 0.004270507954061031, -0.001170913688838482, -0.04570039361715317, 0.014094661921262741, -0.009200461208820343, -0.01013004221022129, 0.006403775420039892, -5.989883720758371e-05, -0.03556240722537041, 0.00783787202090025, -0.03575308993458748, 0.02844356931746006, 0.00949443131685257, 0.02693399414420128, -0.031669292598962784, -0.026425505056977272, -0.024343881756067276, -0.0010005899239331484, -0.014905065298080444, 0.07398506253957748, 0.0446198545396328, 0.04236343875527382, -0.010376340709626675, -0.009311693720519543, 0.0293493140488863, -0.022421158850193024, 0.008437729440629482, -0.02118171937763691, -0.041568923741579056, -0.007420752197504044, -0.0529145710170269, -0.011385372839868069, 0.004628038965165615, 0.015469170175492764, 0.0036587328650057316, 0.004886255599558353, -0.007961020804941654, -0.0023775803856551647, -0.04249056056141853, 0.009685114957392216, -0.02447100356221199, 0.02604413963854313, 0.022452939301729202, 0.010201548226177692, 0.006356104742735624, 0.038676898926496506, -0.054376475512981415, -0.004782968666404486, 0.02291375771164894, 0.05688713863492012, -0.01700258068740368, -0.00298141036182642, 0.01499246247112751, 0.003493871307000518, 0.006344187073409557, 0.007134727668017149, 0.007142672780901194, 0.036293357610702515, 0.030366290360689163, 0.001619813614524901, -0.0027609330136328936, -0.01631929911673069, -0.016120670363307, -0.022993208840489388, -0.010519353672862053, -0.003213805379346013, -0.004310233518481255, 0.0010775583796203136, -0.011846190318465233, -0.008962107822299004, 0.05710960179567337, 0.007667051162570715, 0.007702804636210203, 0.03858155757188797, 0.008318551816046238, 0.0187505092471838, -0.046240661293268204, 0.0008372181910090148, -0.015103694051504135, 0.021229390054941177, -0.015254651196300983, 0.013649734668433666, 0.0015016297111287713, 0.023755941540002823, -0.016160396859049797, -0.01786065474152565, 0.012156050652265549, -0.02693399414420128, 0.029015617445111275, 0.0024749082513153553, -0.02996903471648693, -0.008660192601382732, 0.0009951276006177068, 0.013776857405900955, -0.009319638833403587, 0.01744750700891018, 0.0017320385668426752, -0.046431344002485275, -0.00803252775222063, 0.007619380485266447, 0.0036666779778897762, -0.012442074716091156, -0.009049504064023495, -0.0030409989412873983, 0.010233328677713871, 0.03765992075204849, -0.037405677139759064, -0.02356525883078575, 0.031224364414811134, -0.027680836617946625, -0.01068620104342699, 0.025710443034768105, 0.010320724919438362, 0.01720915362238884, -0.018925301730632782, 0.025472089648246765, -0.005827753338962793, 0.015818756073713303, 0.04325329139828682, 0.02356525883078575, -0.03198709711432457, 0.015691634267568588, 0.006403775420039892, 0.027235908433794975, 0.0032952430192381144, 0.004131468012928963, -0.015413554385304451, -0.015119584277272224, -0.026584407314658165, -0.01720915362238884, -0.03553062677383423, 0.019958168268203735, 0.007857734337449074, 0.009867852553725243, -0.029031507670879364, 0.02996903471648693, -0.01739983633160591, 0.017812984064221382, 0.009756620973348618, 0.0009256077464669943, -0.05577481910586357, -0.044206708669662476, -0.004759133327752352, -0.017066141590476036, 0.0218650009483099, 0.014309180900454521, 0.05873040854930878, -0.046653807163238525, -0.06635773181915283, 0.005986656062304974, 0.005716521758586168, 0.007512121461331844, -0.026822762563824654, 0.0016585460398346186, -0.012489745393395424, 0.01698669046163559, 0.009883742779493332, -0.010487573221325874, 0.00562912505120039, -0.0379459448158741, -0.003547501051798463, -0.00710294721648097, -0.01101989671587944, -0.03489501401782036, -0.0007120823720470071, -0.00767499627545476, -0.004763105884194374, -0.015612182207405567, 0.01983104646205902, -0.0392807275056839, 0.0251225046813488, -0.00981223676353693, -0.03667472302913666, 0.015969712287187576, -0.013355765491724014, 0.007472395431250334, 0.01894119195640087, -0.006769251544028521, -0.014348906464874744, -0.010590859688818455, -0.041123997420072556, 0.029683008790016174, -0.0370560921728611, 0.0026020302902907133, -0.005823780782520771, -0.004042085260152817, 0.029063288122415543, -0.00905744917690754, -0.05278744921088219, -0.008096088655292988, 0.024137306958436966, 0.01719326339662075, -0.015596291981637478, 0.011131128296256065, 0.03533994406461716, 0.003561404999345541, -0.012728099711239338, -0.002228609286248684, -0.02251650020480156, 0.007289657834917307, -0.014213839545845985, 0.011734958738088608, -0.02161075547337532, 0.0124500198289752, -0.0024014157243072987, 0.020148852840065956, -0.0021551167592406273, 0.00568474130704999, 0.02094336599111557, -0.0067930868826806545, 0.02779206819832325, -0.021928561851382256, 0.022325817495584488, -0.032320793718099594, 0.0007438628817908466, -0.021769659593701363, 0.006078025326132774, -0.0002686447405721992, -0.0030012731440365314, -0.01981515623629093, 0.00883498601615429, 0.014984517358243465, 0.012728099711239338, -0.02958766743540764, -0.0033210646361112595, 0.007671023719012737, 0.008517180569469929, -0.008036499843001366, -0.03149449825286865, -0.00023264336050488055, -0.019258996471762657, -0.059683822095394135, 0.03832731023430824, -0.043285071849823, 0.03193942457437515, -0.03645225986838341, 0.05520277097821236, 0.033210646361112595, -0.0003307160804979503, 0.0011391331208869815, 0.025662772357463837, 0.009923468343913555, 0.011957421898841858, 0.008437729440629482, -0.03686540573835373, -0.03594377264380455, -0.000954408838879317, 0.010217438451945782, -0.030270949006080627, 0.003650787752121687, 0.014539589174091816, 0.008541015908122063, -0.040933314710855484, -0.003845443483442068, -0.041791386902332306, -0.009764566086232662, -0.010948390699923038, 0.014452192932367325, -0.029063288122415543, -0.005545701365917921, 0.036261577159166336, -0.007877597585320473, 0.005271594505757093, 0.004655846860259771, 0.020784461870789528, -0.01554862130433321, -2.7870029953191988e-05, 0.013308093883097172, -0.012036873027682304, -0.013244532980024815, 0.004485026467591524, -0.025249626487493515, 0.013737130910158157, -0.014380686916410923, -0.007885542698204517, -0.002677509095519781, -0.029285753145813942, -0.03769170120358467, 0.0280145313590765, 0.006228982470929623, -0.018559826537966728, -0.022103354334831238, 0.005362963303923607, 0.023024989292025566, -0.028936166316270828, -0.006312406621873379, 0.012608923017978668, 0.011973312124609947, -0.03327420726418495, -0.012569197453558445, 0.0031581895891577005, 0.00231799203902483, 0.0014331029960885644, 0.0033746943809092045, 0.006840757559984922, -0.015119584277272224, -0.017765313386917114, 0.01766997203230858, -4.391546826809645e-05, -0.004560505039989948, 0.02313622087240219, 0.02539263851940632, -0.009438815526664257, 0.0027867546305060387, -0.014356851577758789, -0.015810810029506683, 0.023676490411162376, 0.029667118564248085, -0.007742530200630426, -0.02753782458603382, 0.013316038995981216, -0.007857734337449074, -0.003994414582848549, -0.007325410842895508, 0.00767499627545476, 0.002665591426193714, -0.04080619290471077, -0.038899362087249756, 0.011147018522024155, 0.031351488083601, 0.03422762453556061, 0.012529471889138222, 0.003406474832445383, 0.02162664569914341, -0.05739562585949898, 0.018575716763734818, -0.024169087409973145, -0.009414980188012123, 0.014190004207193851, 0.012736044824123383, -0.041568923741579056, -0.02952410653233528, -0.0069122640416026115, -0.005422551650553942, -0.010829213075339794, 0.01679600588977337, 0.016637103632092476, 0.04010701924562454, -0.006002546288073063, 0.01679600588977337, -0.026075920090079308, -0.02075268141925335, 0.011901806108653545, -0.020832132548093796, -0.00519214291125536, 0.01179057452827692, 0.0005799945793114603, -0.000761739443987608, -0.009121010079979897, -0.011711123399436474, 0.0262348223477602, 0.021960342302918434, -0.01222755666822195, -0.015222870744764805, -0.006530897691845894, -0.0038077039644122124, 0.010670310817658901, 0.027299469336867332, -0.0032872979063540697, 0.034831453114748, 0.01631929911673069, -0.04217275604605675, -0.003420378779992461, -0.0223099272698164, -0.014833559282124043, 0.03546706587076187, -0.029714789241552353, 0.000107259264041204, -0.04141002148389816, 0.006371994968503714, -0.03197120502591133, 0.0148971201851964, -0.0007652154308743775, 0.009994974359869957, -0.014078771695494652, 0.038073066622018814, 0.012402349151670933, -0.02385128289461136, -0.022357597947120667, 0.011377427726984024]
+,
+ "blue shoes": [0.032725803554058075, -0.00856518279761076, 0.0026553513016551733, 0.004054524470120668, 0.02584759332239628, -0.03982122242450714, 0.01059244479984045, 0.03136464208364487, -0.03663552179932594, -0.05088428035378456, 0.01734757237136364, -0.026339927688241005, -0.005716155283153057, -0.027382519096136093, -0.013119282200932503, 0.015580957755446434, -0.0010208713356405497, -0.060296569019556046, -0.0047532059252262115, 0.03530332073569298, -0.012786231935024261, -0.026730898767709732, 0.019954051822423935, 0.010563483461737633, 0.016797315329313278, 0.010454880073666573, 0.05056570842862129, -0.03533228486776352, 0.01595744863152504, 0.002733183791860938, 0.01585608534514904, -0.046366382390260696, 0.02298046462237835, -0.02425474300980568, 0.022430207580327988, -0.04503418132662773, -0.00317845749668777, 0.018129516392946243, 0.03631695359945297, -0.002984781516715884, -0.012409740127623081, -0.03492683172225952, 0.048364683985710144, 0.013662299141287804, 0.03669344633817673, -0.004840088542550802, -0.002823686460033059, 0.004007462877780199, 0.008869271725416183, 0.03255203738808632, 0.04118238389492035, 0.03498475253582001, -0.01420531515032053, 0.09186393767595291, 0.02363208495080471, -0.0008407707791775465, -0.03950265049934387, 0.04468664899468422, 0.05873268097639084, 0.0035911500453948975, -0.021793067455291748, -0.04839364439249039, -0.015421672724187374, 0.07634089887142181, 0.008695506490767002, 0.010838611982762814, -0.05928293615579605, 0.022734297439455986, -0.04341237246990204, 0.02361760474741459, 0.0032526697032153606, 0.02810654230415821, -0.007594992872327566, 0.0330154113471508, -0.0007864690851420164, 0.020373985171318054, 0.028454072773456573, -0.0023295413702726364, -0.0045396191999316216, 0.014437003061175346, -0.014596287161111832, 0.033681511878967285, -0.02820790559053421, -0.012909315526485443, -0.011034098453819752, -0.02597791701555252, 0.011642277240753174, -0.022111637517809868, -0.023052867501974106, -0.025615904480218887, -0.04871221259236336, -0.0050391945987939835, -0.017304129898548126, -0.001102323760278523, -0.011562634259462357, 0.051984794437885284, 0.036461759358644485, 0.01830328069627285, -0.013365449383854866, -0.015682321041822433, -0.0029612507205456495, -0.05873268097639084, 0.0258620735257864, -0.005723395384848118, 0.002970301080495119, 0.05363556370139122, 0.08439202606678009, -0.03255203738808632, -0.011881204321980476, -0.0027911055367439985, -0.0839286521077156, -0.009861182421445847, 0.0018191058188676834, 0.03064061887562275, -0.006686345208436251, 0.040400438010692596, 0.038575902581214905, -0.03339190408587456, 0.03770707547664642, -0.04393366724252701, 0.034000083804130554, -0.013235125690698624, -0.0570819117128849, -0.046134695410728455, -0.06440901756286621, 0.023501761257648468, 0.0048256078734993935, -0.030582698062062263, 0.005886300466954708, -0.01659458875656128, 0.00409796554595232, -0.03087230585515499, -0.02190891094505787, 0.00836245622485876, -0.004000222776085138, 0.024080978706479073, -0.03808356821537018, -0.02342935837805271, -0.021981313824653625, 0.009064758196473122, 0.02140209637582302, -0.007413987070322037, 0.01766614243388176, -0.009636735543608665, -0.005723395384848118, 0.007066456601023674, -0.008152489550411701, 0.03565085306763649, -0.008999596349895, 0.013350969180464745, 0.017506856471300125, 0.015798164531588554, 0.029540104791522026, -0.00012659076310228556, 0.019751325249671936, -0.042398739606142044, 0.02713635191321373, -0.007710836362093687, 0.011070298962295055, -0.01915762759745121, 0.00781943928450346, 0.03941576927900314, -0.02980075404047966, -0.025891033932566643, 0.0064401775598526, -0.002630010712891817, -0.016435304656624794, 0.018578410148620605, -0.05589451268315315, -0.00411968631669879, -0.028714720159769058, 0.009434008970856667, 0.06191837787628174, 0.02341487817466259, 0.008963394910097122, 0.01905626431107521, 0.018925940617918968, -0.029554586857557297, -0.04248562082648277, 0.016218097880482674, 0.020591191947460175, 0.07141754776239395, 0.04471560940146446, -0.0027368038427084684, -0.024964286014437675, 0.021344173699617386, -0.01191016472876072, -0.03608526661992073, 0.014046031050384045, 0.013075840659439564, -0.014444243162870407, 0.03776499629020691, 0.01863633096218109, 0.02127177268266678, -0.02096768282353878, -0.0071786800399422646, -0.01031731627881527, 0.006950613111257553, -0.037504348903894424, 0.029829714447259903, -0.0029232397209852934, 0.0036979434080421925, -0.01070104818791151, -0.010042187757790089, 0.005947842262685299, 0.005882680416107178, -0.02329903468489647, -0.012728310190141201, -0.007251082453876734, 0.006831149570643902, 0.044426001608371735, -0.023878252133727074, 0.023284554481506348, -0.025789670646190643, -0.05128973349928856, 0.017695102840662003, 0.08804109692573547, -0.008854791522026062, 0.013893986120820045, -0.022299883887171745, -0.015609918162226677, 0.002800155896693468, 0.053809329867362976, -0.005803037900477648, 0.0037250942550599575, -0.009571573697030544, 0.05586555227637291, -0.01980924792587757, -0.047408971935510635, 0.022589491680264473, 0.008261092938482761, 0.018216397613286972, 0.013792622834444046, 0.015421672724187374, 0.015479594469070435, -0.001123139401897788, -0.04320964589715004, -0.0013638768577948213, 0.01245318166911602, -0.05989111587405205, -0.07703596353530884, -0.009571573697030544, 0.05320115014910698, -0.026470251381397247, -0.0423697791993618, -0.0011267595691606402, -0.013416131027042866, -0.018578410148620605, -0.059311896562576294, 0.07315520197153091, -0.011946366168558598, 0.018766654655337334, -0.007156959269195795, 0.0213876161724329, 0.004818367771804333, 0.018129516392946243, 0.02001197263598442, 0.003033652901649475, -0.016551148146390915, 0.028975367546081543, -0.012540064752101898, 0.003401094349101186, -0.048799097537994385, -0.06336642056703568, -0.005900781136006117, 0.024645715951919556, 0.04022667184472084, -0.0330154113471508, 0.0258620735257864, -0.003638211637735367, 0.009701897390186787, 0.019418274983763695, 0.02331351488828659, -0.005690814461559057, 0.010599684901535511, -0.011837762780487537, 0.017941270023584366, -0.01682627573609352, -0.006266412325203419, -0.014074991457164288, 0.03223346918821335, 0.03599838539958, 0.011359908618032932, -0.054880883544683456, 0.04998649284243584, 0.014516645111143589, -0.009274723939597607, 0.020938722416758537, 0.03405800461769104, 0.011186143383383751, -0.01474109198898077, -0.049957532435655594, -0.002126815263181925, 0.016362901777029037, -0.0026987928431481123, -0.005509809125214815, 0.026846742257475853, -0.01463972870260477, -0.028034139424562454, -0.08838862925767899, 0.048799097537994385, 0.05375140905380249, 0.005553250201046467, 0.03064061887562275, -0.021126966923475266, -0.0072221215814352036, 0.023125268518924713, -0.011294745840132236, 0.02086631953716278, -0.024341626092791557, -0.03860486298799515, 0.035824619233608246, 0.009057518094778061, -0.043035879731178284, -0.00422828970476985, -0.08166970312595367, -0.035824619233608246, -0.001702357199974358, -0.00040432115201838315, -0.08219099789857864, -0.01723172888159752, -0.010621405206620693, 0.02574623003602028, 0.013054120354354382, -0.024240262806415558, -0.02215507999062538, 0.002747664228081703, -0.012692108750343323, 0.02661505527794361, -0.004912490490823984, -0.06666796654462814, -0.05745840072631836, 0.02468915656208992, 0.015189985744655132, 0.039213042706251144, -0.057429440319538116, -0.006527060177177191, 0.01936035417020321, 0.008608624339103699, 0.008210411295294762, -0.04352821409702301, -0.044657688587903976, 0.0027621446643024683, 0.023067347705364227, 0.015378231182694435, -0.0395895354449749, 0.055402178317308426, -0.05291154235601425, 0.025717267766594887, 0.0011059439275413752, -0.046771835535764694, -0.007033875677734613, 0.010816891677677631, 0.03136464208364487, 0.0121852932497859, -0.00792080257087946, -0.014509405009448528, -0.011410590261220932, -0.04821987822651863, 0.06278720498085022, 0.02980075404047966, 0.03556397184729576, 0.011178902350366116, -0.037504348903894424, 0.007493629585951567, -0.01991061121225357, -0.038460057228803635, -0.04706144332885742, 0.021648263558745384, -0.006899931468069553, -0.04141407087445259, -0.0004837373271584511, -0.032609958201646805, 0.02085183933377266, -0.0211848895996809, 0.06116539612412453, -0.006599462125450373, 0.0014082231791689992, -0.030061401426792145, 0.013684019446372986, -0.025036687031388283, 0.017376532778143883, 0.043354447931051254, -0.041037578135728836, -0.0032635300885885954, -0.025282854214310646, -0.011504712514579296, 0.005339663475751877, 0.03625903278589249, -0.012482143007218838, 0.009245763532817364, -0.04903078451752663, 0.03307333216071129, -0.03237827122211456, 0.013090320862829685, 0.049117665737867355, 0.03159632906317711, -0.006805808749049902, -0.03605630621314049, 0.03550604730844498, -0.012996198609471321, -0.030582698062062263, -0.024718116968870163, -0.043701980262994766, 0.006936132442206144, -0.015407192520797253, -0.04552651569247246, 0.012431461364030838, -0.002521407324820757, -0.014219796285033226, 0.04077693074941635, -0.017217248678207397, 0.04457080736756325, 0.06875314563512802, 0.016420822590589523, -0.014770052395761013, 0.024819480255246162, -0.001840826473198831, -0.02075047604739666, 0.04306484013795853, 0.05525737255811691, -0.05001545324921608, -0.045816123485565186, -0.016102254390716553, 0.015088622458279133, 0.00867378618568182, 0.010874813422560692, 0.0009692847379483283, 0.01058520469814539, -0.04987064749002457, -0.03631695359945297, -0.040400438010692596, 0.008166970685124397, -0.034955792129039764, 0.01575472392141819, 0.007486389484256506, -0.042398739606142044, -0.03854694217443466, -0.005444646812975407, -0.022531570866703987, -0.012909315526485443, -0.01733309216797352, -0.024877402931451797, 0.014704890549182892, -0.016247058287262917, -0.006154188886284828, -0.0200988557189703, -0.04769858345389366, -0.03515851870179176, -0.030988149344921112, 0.000758865789975971, -0.00398936215788126, -0.06828977167606354, 0.01882457733154297, -0.01036799792200327, 0.050276100635528564, -0.025138050317764282, 0.010824131779372692, 0.020243661478161812, -0.026050318032503128, 0.02733907848596573, 0.042948998510837555, 0.039850182831287384, 0.03727266192436218, -0.0014688600786030293, -0.009245763532817364, -0.01511758379638195, 0.011736399494111538, 0.033247098326683044, -0.05971734970808029, 0.0011303796200081706, -0.031335681676864624, -0.012004287913441658, -0.0210835263133049, 0.015450634062290192, -0.013705739751458168, 0.010570724494755268, -0.0015666030813008547, 0.01969340443611145, 0.04074797034263611, 0.014987259171903133, -0.0018254409078508615, 0.0052093397825956345, -0.044541846960783005, -0.02127177268266678, 0.016782835125923157, 0.024442989379167557, 0.015899527817964554, -0.09209562093019485, 0.027179792523384094, -0.012163572944700718, 0.013227885589003563, 0.027831412851810455, -0.017709583044052124, -0.06435108929872513, 0.04908870533108711, -0.01629049889743328, -0.009506411850452423, -0.008319014683365822, -0.02160482294857502, 0.011729159392416477, -0.023342475295066833, 0.00396040128543973, 0.016087772324681282, -0.008818590082228184, -0.04141407087445259, -0.04604781046509743, 0.021981313824653625, -0.005994903855025768, 0.02626752480864525, 0.020243661478161812, 0.003690703073516488, -0.0023548821918666363, -0.0065343002788722515, -0.03428969159722328, -0.008116289041936398, -0.007232981733977795, 0.004793026950210333, -0.017955750226974487, -0.026875704526901245, 0.012699349783360958, 0.00304451328702271, -0.0015457874396815896, 0.02395065501332283, 0.006961473263800144, 0.002720513381063938, -0.008072847500443459, -0.04268834739923477, 0.013307527638971806, 0.021662743762135506, -0.02810654230415821, -0.026426810771226883, -0.028656799346208572, 0.008528981357812881, -0.0029522005934268236, -0.022082677111029625, -0.016261538490653038, 0.048799097537994385, 0.013430612161755562, 0.03486890718340874, -0.007298143580555916, 0.002143105724826455, 0.020084375515580177, -0.03472410514950752, -0.018042633309960365, -0.0019367593340575695, 0.027498362585902214, -0.0011919215321540833, 0.020359504967927933, 0.04801715165376663, 0.028338229283690453, 0.0012018767884001136, -0.02702050842344761, -0.022908061742782593, 0.007529831025749445, -0.0026951725594699383, 0.0027150833047926426, 0.005734256003051996, -0.0003323714481666684, 0.006099887192249298, 0.006154188886284828, 0.008101808838546276, 0.003873518668115139, 0.027266675606369972, -0.024747079238295555, 0.05786385387182236, 0.013995349407196045, -0.0245733130723238, 0.045294828712940216, -0.03136464208364487, -0.0640614852309227, 0.026846742257475853, 0.029655948281288147, -0.09603430330753326, -0.00033146640635095537, 0.026122720912098885, 0.036664482206106186, -0.021228330209851265, 0.01714484579861164, -0.04225393384695053, 0.00199468107894063, -0.0026209603529423475, -5.41885347047355e-05, 0.005187619011849165, -0.02224196121096611, -0.008862031623721123, 0.04827779904007912, -0.03695409372448921, 0.0199974924325943, 0.025731747969985008, -0.02396513521671295, -0.010925495065748692, 0.0060600657016038895, 0.007352445274591446, -0.023038385435938835, 0.005484468303620815, -0.00797872431576252, -0.011577114462852478, 0.030293088406324387, -0.038662783801555634, 0.05100012198090553, -0.04413639381527901, 0.0033323122188448906, -0.0011602456215769053, 0.036230072379112244, 0.0006271842285059392, 0.007754277903586626, -0.030698541551828384, 0.02172066643834114, 0.021344173699617386, 0.024457469582557678, 0.01925899088382721, 0.014914857223629951, -0.022314364090561867, -0.013604377396404743, -0.024196822196245193, -0.0005081730778329074, 0.006110747344791889, 0.004467216785997152, -0.022792218253016472, -0.0604703351855278, -0.030177244916558266, 0.04396262764930725, -0.03588254004716873, 0.024515392258763313, 0.012807952240109444, -0.019418274983763695, -0.019432755187153816, 0.03333398327231407, 0.012366299517452717, 0.007127998396754265, -0.04714832454919815, 0.02011333592236042, -0.011736399494111538, 0.024964286014437675, 0.05386725068092346, 0.010027707554399967, 0.011989807710051537, -0.000986480270512402, 0.008058367297053337, 0.02321215160191059, 0.00861586444079876, 0.0071243783459067345, -0.043151721358299255, 0.053896211087703705, -0.000496407737955451, -0.046974558383226395, -0.02649921178817749, -0.024848442524671555, -0.005144177936017513, -0.003004692029207945, 0.0015276868361979723, -0.0040291836485266685, -0.014379080384969711, -0.008159730583429337, 0.0011828712886199355, -0.009853942319750786, -0.015189985744655132, 0.011330947279930115, 0.005017473828047514, -0.01595744863152504, 0.005404825787991285, -0.00018258934142068028, -0.02852647379040718, 0.01463972870260477, 0.0025539882481098175, 0.003887999104335904, 0.050623632967472076, -0.0395895354449749, -0.008290054276585579, 0.014603527262806892, -0.0027965358458459377, -0.015465114265680313, -0.004782166797667742, 0.06023864820599556, 0.02523941360414028, 0.02629648707807064, -0.019114185124635696, -0.01010734960436821, -0.01947619765996933, -0.01872321404516697, -0.014827974140644073, -0.03712785989046097, -0.03119087591767311, 0.009593294002115726, -0.042833153158426285, -0.005451886914670467, 0.034637220203876495, -0.016044331714510918, 0.0057197753340005875, -0.03339190408587456, 0.01617465540766716, 0.002510546939447522, -0.007638433948159218, 0.004011082928627729, -0.013517494313418865, -0.0199974924325943, 0.015363750979304314, -0.02607928030192852, -0.057516321539878845, 0.007906322367489338, -0.006957853212952614, -0.011048578657209873, -0.024602273479104042, -0.011359908618032932, -0.0334208644926548, -0.010100109502673149, 0.028338229283690453, 0.03373943269252777, -0.015016220510005951, 0.014668690040707588, -0.006863730493932962, -0.03148048371076584, 0.014827974140644073, 0.011627796106040478, -0.0009891953086480498, 0.0035802896600216627, 0.022531570866703987, 0.02799069881439209, -0.013517494313418865, 0.0050500547513365746, 0.0028797981794923544, -0.044339120388031006, -0.0031169154681265354, -0.02733907848596573, -0.049233511090278625, 0.025195972993969917, -0.0076891155913472176, 1.5470317521248944e-05, -0.005082635674625635, -0.01064312644302845, 0.036548640578985214, 0.01831776089966297, 0.007848400622606277, 0.015595437958836555, -0.01293827686458826, -0.01457456685602665, 0.015349270775914192, 0.009144400246441364, -0.05128973349928856, 0.020909760147333145, -0.031972821801900864, 0.016261538490653038, -0.03692513331770897, 0.000362689868779853, 0.009926344268023968, 0.0073379650712013245, 0.021850990131497383, -0.008746188133955002, -0.00016381002205889672, -0.018969381228089333, 0.013553695753216743, -0.03249411657452583, 0.01479177363216877, -0.012004287913441658, -0.04150095209479332, -0.009274723939597607, -0.0029630607459694147, 0.011555394157767296, 0.0255579836666584, 0.0244719497859478, 0.031856976449489594, -0.02765764854848385, -0.02906225062906742, 0.013828824274241924, -0.009658455848693848, 0.026310967281460762, -0.04349925369024277, -0.03599838539958, 0.02382032945752144, -0.0302351675927639, -0.005042814649641514, -0.00046269543236121535, -0.03744642809033394, 0.029308417811989784, 0.023154228925704956, 0.01037523802369833, -0.025615904480218887, -0.0038300773594528437, -0.009173361584544182, 0.00442015565931797, 0.013792622834444046, -0.012054969556629658, 0.014241516590118408, 0.032523076981306076, -0.03790980204939842, 0.02671641856431961, -0.035071633756160736, -0.021662743762135506, -0.03373943269252777, 0.031161915510892868, -0.026325447484850883, -0.019664442166686058, -0.04404950886964798, 0.0011973517248407006, 0.02906225062906742, -0.00878962967544794, -0.04428119584918022, 0.020156778395175934, 0.01217805314809084, 0.011678477749228477, 6.832959479652345e-05, 0.01292379666119814, 0.006592222023755312, 0.02542765997350216, -0.007280043326318264, -0.009318165481090546, -0.014089471660554409, 0.038460057228803635, -0.022792218253016472, -0.03486890718340874, 0.05514153093099594, -0.0011430500308051705, 0.012699349783360958, 0.011338187381625175, -0.048596370965242386, -0.006016624625772238, 0.013756421394646168, 0.0028924685902893543, -0.030814385041594505, -0.03854694217443466, -0.026861224323511124, 0.041761599481105804, -0.023385917767882347, 0.0128296734765172, 0.009303685277700424, -0.01533479057252407, 0.039328884333372116, 0.03446345776319504, 0.02012781798839569, 0.019027303904294968, -0.0018100554589182138, 0.00438757473602891, -0.019114185124635696, -0.01629049889743328, -0.007185920141637325, 0.030061401426792145, -0.004850948695093393, 0.0027349938172847033, -0.009752579033374786, 0.02832374908030033, 0.0003339552495162934, -0.04726416990160942, -0.01895490102469921, -0.008478299714624882, -0.012373539619147778, -0.017391012981534004, -0.021025605499744415, 0.0015014410018920898, 0.030032441020011902, 0.026224084198474884, -0.025992397218942642, 0.00873170793056488, -0.0026082899421453476, 0.006867350544780493, -0.031103992834687233, 0.006132468115538359, 0.002467105630785227, -0.019114185124635696, -0.01733309216797352, 0.020909760147333145, 0.013618857599794865, 0.017593739554286003, -0.04074797034263611, 0.002861697692424059, -0.009933584369719028, -0.024240262806415558, 0.010244914330542088, 0.013372690416872501, 0.002767574740573764, -0.012416980229318142, -0.02095320262014866, -0.014009829610586166, -0.01298895850777626, 0.007149719167500734, 0.0013312958180904388, -0.03727266192436218, 0.02235780470073223, 0.0035495187621563673, 0.00792804267257452, 0.039213042706251144, -0.0585009939968586, 0.0020562231075018644, 0.010042187757790089, 0.03553500771522522, 0.00595508236438036, -0.02807758003473282, 0.014017069712281227, -0.03405800461769104, 0.00867378618568182, 0.01766614243388176, -0.04161679744720459, 0.004738725256174803, -0.054098937660455704, 0.031046072021126747, 0.023892732337117195, -0.013010678812861443, 0.01798471063375473, 0.01005666796118021, -0.019244510680437088, 0.008644824847579002, 0.009629495441913605, -0.026991548016667366, -0.0007339774747379124, -0.00315311667509377, -0.0332181379199028, 0.021199369803071022, 0.00210328446701169, 0.006899931468069553, 0.030814385041594505, 0.02616616152226925, -0.05195583403110504, -0.015523036010563374, 0.014610768295824528, 0.03854694217443466, 0.01069380808621645, -0.001148480223491788, 0.009318165481090546, 0.0007914467714726925, -0.013626097701489925, 0.006324334070086479, -0.007710836362093687, 0.011309226974844933, -0.01969340443611145, 0.01616017520427704, -0.019027303904294968, -0.015233427286148071, 0.0028689380269497633, -0.010186992585659027, 0.013510254211723804, -0.027006028220057487, -0.0037142338696867228, -0.002584759145975113, -0.004412915091961622, 0.03617214784026146, 0.006165049038827419, 0.03174113482236862, -0.00434051314368844, 0.026672977954149246, -0.01011458970606327, -0.0033956640399992466, -0.00304451328702271, 0.010621405206620693, 0.01885353773832321, -0.022719817236065865, -0.029945557937026024, 0.015247907489538193, 0.05821138620376587, 0.0035422786604613066, -0.017709583044052124, -0.0007208546157926321, -0.037504348903894424, 0.01304688025265932, -0.032609958201646805, -0.02872920036315918, 0.009043036960065365, 0.026021357625722885, 0.002854457590728998, 0.007841160520911217, -0.005748736206442118, -0.04862533137202263, -0.015103102661669254, -0.018491527065634727, 0.01425599679350853, -0.01004942785948515, -0.029221536591649055, 0.0035296082496643066, -0.01885353773832321, -0.030988149344921112, -0.01831776089966297, -0.006154188886284828, 0.02267637476325035, 0.049117665737867355, -0.02510908991098404, 0.00787012092769146, 0.011750880628824234, 0.011468512006103992, 0.042080171406269073, 0.01587056741118431, -0.03350774571299553, -0.05479399859905243, 0.05884852260351181, 0.0004448211402632296, 0.010657606646418571, -0.015103102661669254, 0.005520669277757406, 0.049233511090278625, 0.022314364090561867, -0.012163572944700718, 0.018868017941713333, -0.0031223457772284746, 0.018143996596336365, 0.018708733841776848, 0.016551148146390915, 0.002597429556772113, 0.0060600657016038895, -0.013676779344677925, 0.0082538528367877, -0.036461759358644485, -0.03892343491315842, -0.02371896803379059, -0.003924200311303139, 0.0011267595691606402, 0.015436152927577496, -0.036027345806360245, 0.017825426533818245, -0.014444243162870407, -0.048596370965242386, 0.008948914706707, -0.019968532025814056, 0.002257139189168811, 0.007240221835672855, -0.001595563953742385, 0.0064908592030406, -0.01250386331230402, -0.0245733130723238, 0.049436233937740326, 0.02958354726433754, -0.0059804231859743595, 0.01743445359170437, 0.058790601789951324, -0.003444535657763481, -0.03567981347441673, 0.04961000010371208, -0.002204647520557046, 0.0121346116065979, 0.030264127999544144, 0.029511144384741783, 0.04552651569247246, -0.000951184134464711, 5.707331365556456e-05, 0.02736803889274597, -0.01605881191790104, 0.017897829413414, 0.03087230585515499, 0.014784533530473709, 0.025818631052970886, -0.008159730583429337, -0.00630261329934001, 0.012373539619147778, 0.007142479065805674, -0.010288355872035027, -0.021126966923475266, 0.00814524944871664, -0.010034947656095028, 0.04584508389234543, 0.013452332466840744, 0.004412915091961622, -0.008644824847579002, -0.014762812294065952, -0.0049957530573010445, -0.02584759332239628, -0.023458318784832954, -0.01495829876512289, -0.006161428987979889, -0.01574024185538292, 0.011627796106040478, -0.00036744127282872796, 0.011439550668001175, -0.016797315329313278, -0.033594630658626556, -0.0011765360832214355, -0.007587752770632505, 0.002114144852384925, -0.006049205549061298, 0.03712785989046097, 0.015914008021354675, 0.03269684314727783, 0.005723395384848118, -0.029887637123465538, 0.013162723742425442, 0.013705739751458168, 0.04746689647436142, 0.0064546577632427216, -0.041037578135728836, 0.013256845995783806, -0.033681511878967285, -0.018245359882712364, 0.015667840838432312, -0.00433689309284091, 0.011736399494111538, -0.008442099206149578, 0.024428509175777435, 0.0014027929864823818, -0.01293827686458826, 0.0045396191999316216, 0.02948218397796154, 0.03194385766983032, 0.01565336063504219, -0.0068383896723389626, 0.018534967675805092, -0.012902075424790382, -0.015045180916786194, -0.0013240555999800563, -0.004695283714681864, -0.012894835323095322, -0.017521336674690247, 0.02822238579392433, 0.03764915466308594, -0.0017032622126862407, 0.033160217106342316, 0.023458318784832954, -0.041761599481105804, 0.006056445650756359, -0.008290054276585579, 0.012909315526485443, 0.0083552161231637, 0.023226631805300713, -0.027382519096136093, -0.012597986496984959, -0.013213405385613441, 0.012865873984992504, -0.017825426533818245, -0.01420531515032053, -0.024920843541622162, 0.050826359540224075, -0.0063279541209340096, -0.020258141681551933, 0.004264490678906441, 0.017825426533818245, 0.0056039318442344666, 0.005755976308137178, 0.006556021049618721, 0.014929337427020073, -0.002164826262742281, 0.0014353740261867642, 0.011729159392416477, 0.013857784681022167, 0.025369737297296524, -0.001010010950267315, -0.0245733130723238, -0.03817044943571091, 0.02374792844057083, -0.027643168345093727, 0.004470836836844683, -0.02169170416891575, 0.001173820928670466, 0.005260021425783634, -0.004876289516687393, 0.000725379737559706, -0.005813898053020239, 0.04193536564707756, -0.005998523905873299, 0.03353670984506607, -0.00803664606064558, -0.01484245527535677, -0.016116734594106674, 0.10356413573026657, -0.003920580260455608, 0.003602010430768132, -0.014560086652636528, -0.013394410721957684, 0.004061764571815729, 0.032523076981306076, 0.011866724118590355, -0.030582698062062263, 0.044107433408498764, -0.013322008773684502, 0.01505966205149889, 0.013155483640730381, -0.0025720889680087566, -0.04034251719713211, 0.013763661496341228, 0.00841313786804676, 0.031103992834687233, 0.01004942785948515, -0.04703248292207718, 0.016406342387199402, 0.011468512006103992, -0.005567730870097876, -0.01863633096218109, 0.0015150164254009724, -0.01755029894411564, 0.020489828661084175, -0.029409781098365784, 0.000310198258375749, 0.03872070834040642, 0.014082231558859348, -0.022632934153079987, 0.008898233063519001, 0.0047532059252262115, -0.0461636558175087, -0.001064312644302845, -0.011345427483320236, -0.012959997169673443, -0.040487322956323624, -0.01629049889743328, -0.023559682071208954, 0.02733907848596573, 0.009231283329427242, -0.00220826780423522, 0.051144927740097046, 0.022618453949689865, 0.029641468077898026, 0.003046323312446475, 0.0038409377448260784, -0.0016951169818639755, -0.005509809125214815, -0.004901630338281393, -0.026310967281460762, 0.015551996417343616, -0.014248756691813469, 0.003783015999943018, 0.019287951290607452, 0.006005764007568359, 0.026571614667773247, -0.011164422146975994, -0.003229138907045126, 0.0025811390951275826, -0.01479901373386383, 0.01883905753493309, 0.015711281448602676, -0.0029503905680030584, 0.013119282200932503, -0.0047097643837332726, 0.002034502336755395, 0.04590300843119621, 0.01852048747241497, 0.017057962715625763, -0.00328706088475883, -0.01605881191790104, -0.012438701465725899, 0.0035802896600216627, 0.024949805811047554, -0.021879950538277626, -0.005658233538269997, -0.06440901756286621, 0.007319864351302385, 0.015074142254889011, -0.006773227825760841, -0.06661004573106766, 0.0005927931633777916, -0.0198961291462183, -0.007421227637678385, -0.009629495441913605, 0.0429779589176178, -0.018042633309960365, 0.008275574073195457, -0.017202766612172127, 0.0069976747035980225, 0.01217805314809084, 0.010657606646418571, 0.004612021613866091, -0.0084058977663517, 0.03066958114504814, -0.016420822590589523, 0.008818590082228184, 0.004684423562139273, -0.030177244916558266, -0.030611658468842506, -0.0023711726535111666, 0.019128667190670967, 0.023863771930336952, 0.014871415682137012, -0.02309630811214447, -0.011287505738437176, -0.0011213293764740229, -0.002854457590728998, 0.026745380833745003, 0.004742345307022333, 0.025717267766594887, 0.032523076981306076, 0.015436152927577496, 0.019939571619033813, 0.011367148719727993, -0.021749626845121384, 0.004210188984870911, 0.00044414238072931767, 0.00836245622485876, -0.004955932032316923, -0.00316578708589077, -0.0018969381926581264, 0.0399949848651886, -0.008796869777143002, 0.038894470781087875, -0.011939126066863537, -0.014067751355469227, 0.00021799856040161103, -0.005857339594513178, 0.017796466127038002, 0.017608219757676125, -0.0033685131929814816, -0.005198479164391756, 0.01831776089966297, -0.0062374514527618885, 0.02373344823718071, -0.008376936428248882, 0.019114185124635696, 0.03043789230287075, -0.007395886816084385, -0.004970412235707045, 0.05936982110142708, -0.033681511878967285, 7.008308602962643e-05, 0.015132063999772072, 5.113406587042846e-05, 0.022169560194015503, -0.008688266389071941, 0.0242692232131958, -0.04564236104488373, -0.01505966205149889, 0.03344982489943504, -0.03420281037688255, -0.005556870251893997, 0.008941673673689365, 0.0018770275637507439, -0.00103716179728508, -0.016362901777029037, 0.015132063999772072, -0.004492557607591152, -0.03214658424258232, -0.03327605873346329, 0.010498321615159512, -0.027440441772341728, -0.0020978543907403946, -0.03136464208364487, 0.03808356821537018, 0.00021109772205818444, -0.02651369199156761, -0.013220645487308502, 0.03515851870179176, -0.005842858925461769, 0.002678882097825408, 0.04141407087445259, 0.02169170416891575, 0.03744642809033394, 0.002468915656208992, -0.019765805453062057, -0.01917210780084133, 0.03799668326973915, 0.05734255909919739, -0.016131214797496796, -0.018143996596336365, 0.017796466127038002, -0.022850140929222107, -0.01038247812539339, -0.011099260300397873, -0.027223234996199608, -0.0006384970620274544, 0.006125228013843298, -0.016971079632639885, -0.00043215075857006013, 0.00034368428168818355, -0.035592932254076004, 0.011526433750987053, -0.009933584369719028, 0.002284290036186576, -0.009470210410654545, 0.0028852284885942936, 0.0006937037687748671, 0.035390205681324005, -0.015523036010563374, -0.019432755187153816, 0.008919953368604183, 0.012214254587888718, -0.03130672127008438, -0.006074546370655298, 0.026658497750759125, 0.028149982914328575, -0.004289831500500441, 0.015132063999772072, -0.025094609707593918, -0.005839238874614239, -0.013922946527600288, -0.009723617695271969, 0.03136464208364487, 0.01107753999531269, 0.03617214784026146, -0.013872264884412289, -0.011881204321980476, 0.01797023043036461, -0.07072249054908752, -0.0063641550950706005, -0.009520892053842545, -0.0020707035437226295, -0.003705183509737253, -0.009535372257232666, 0.03417384624481201, 2.3657990823267028e-05, 0.011801562272012234, -0.0214889794588089, 0.023574162274599075, 0.005741496104747057, 0.024428509175777435, 0.009977025911211967, -0.03000348061323166, -0.0005516143864952028, -0.021358653903007507, -0.022821180522441864, -0.030930228531360626, 0.02309630811214447, -0.013141002506017685, 0.015276867896318436, 0.01287311501801014, -0.023545201867818832, -0.003493407042697072, 0.011193383485078812, 0.014270477928221226, 0.008659305050969124, 0.009434008970856667, -0.012431461364030838, -0.03990810364484787, -0.05713983252644539, 0.03808356821537018, -0.006581361871212721, 0.0038554181810468435, -0.0029503905680030584, 0.008376936428248882, 0.005864579696208239, 0.0016055192099884152, 0.004767686128616333, 0.004398434888571501, -0.022372286766767502, -0.005531529430299997, 0.006798568647354841, 0.01293827686458826, 0.004470836836844683, -0.036114227026700974, 0.007681875489652157, -0.03107503242790699, -0.017622699961066246, -0.0399949848651886, 0.019765805453062057, 0.03287060931324959, -0.0023223012685775757, -0.0031893178820610046, -0.013336488977074623, 0.022517090663313866, -0.01256178505718708, -0.02425474300980568, -0.023139748722314835, -0.027831412851810455, -0.031103992834687233, -0.01627601869404316, 0.05502568557858467, -0.034318652004003525, 0.004800267051905394, 0.0028997089248150587, 0.0033811836037784815, -0.0041450271382927895, 0.009600534103810787, -0.015450634062290192, -0.001180156134068966, 0.006718926131725311, -0.03553500771522522, -0.012330098077654839, -0.012431461364030838, 0.007330724969506264, -0.012228734791278839, -0.012706589885056019, -0.006110747344791889, -0.006635663565248251, 0.04042939841747284, 0.00994082447141409, 0.03153840824961662, -0.0024761559907346964, -0.009303685277700424, 0.004503418225795031, 0.03064061887562275, -0.021793067455291748, -0.042746271938085556, 0.0084058977663517, 0.009137160144746304, 0.020258141681551933, -0.03599838539958, -0.00020283935009501874, 0.0013584466651082039, 0.013264087028801441, -0.028454072773456573, 0.01430667843669653, -0.003743194742128253, -0.011895684525370598, 0.017159326002001762, -0.023907212540507317, 0.03246515616774559, 0.026629535481333733, 0.005031954497098923, 0.029554586857557297, -0.004662702791392803, -0.027266675606369972, -0.02893192693591118, 0.026788821443915367, 0.0023566922172904015, -0.01798471063375473, -0.01925899088382721, -0.014820734038949013, -0.021213850006461143, -0.0007823964697308838, -0.0011068489402532578, -0.03376839682459831, 0.01282243337482214, 0.012214254587888718, 0.03892343491315842, 0.03541916608810425, 0.03796772286295891, -0.001229932764545083, 0.026672977954149246, 0.033160217106342316, 0.0010507372207939625, -0.034637220203876495, 0.017115885391831398, -0.026441290974617004, 0.004535999149084091, -0.00104349700268358, -0.05299842357635498, -0.001929519115947187, -0.004858188796788454, 0.01873769424855709, -0.01904178410768509, -0.01702900230884552, 0.004412915091961622, -0.03906823694705963, 0.02822238579392433, 0.02405201829969883, 0.011149941943585873, -0.00444549648091197, 0.006751507055014372, -0.009868422523140907, -0.01565336063504219, 0.007156959269195795, -0.010244914330542088, 0.0031621670350432396, -0.04541067034006119, -0.024862922728061676, 0.024095458909869194, -0.018071593716740608, -0.006975953932851553, 0.01978028565645218, -0.036461759358644485, 0.018143996596336365, -0.001918658846989274, -0.012250456027686596, 0.003440915374085307, -0.005654613487422466, 0.0035513287875801325, -0.025369737297296524]
+}
\ No newline at end of file
diff --git a/tests/managers/test_agent_manager.py b/tests/managers/test_agent_manager.py
new file mode 100644
index 000000000..149724405
--- /dev/null
+++ b/tests/managers/test_agent_manager.py
@@ -0,0 +1,855 @@
+import json
+import logging
+import os
+import random
+import re
+import string
+import time
+import uuid
+from datetime import datetime, timedelta, timezone
+from typing import List
+from unittest.mock import AsyncMock, Mock, patch
+
+import pytest
+from _pytest.python_api import approx
+from anthropic.types.beta import BetaMessage
+from anthropic.types.beta.messages import BetaMessageBatchIndividualResponse, BetaMessageBatchSucceededResult
+
+# Import shared fixtures and constants from conftest
+from conftest import (
+ CREATE_DELAY_SQLITE,
+ DEFAULT_EMBEDDING_CONFIG,
+ USING_SQLITE,
+)
+from openai.types.chat.chat_completion_message_tool_call import ChatCompletionMessageToolCall as OpenAIToolCall, Function as OpenAIFunction
+from sqlalchemy import func, select
+from sqlalchemy.exc import IntegrityError, InvalidRequestError
+from sqlalchemy.orm.exc import StaleDataError
+
+from letta.config import LettaConfig
+from letta.constants import (
+ BASE_MEMORY_TOOLS,
+ BASE_SLEEPTIME_TOOLS,
+ BASE_TOOLS,
+ BASE_VOICE_SLEEPTIME_CHAT_TOOLS,
+ BASE_VOICE_SLEEPTIME_TOOLS,
+ BUILTIN_TOOLS,
+ DEFAULT_ORG_ID,
+ DEFAULT_ORG_NAME,
+ FILES_TOOLS,
+ LETTA_TOOL_EXECUTION_DIR,
+ LETTA_TOOL_SET,
+ LOCAL_ONLY_MULTI_AGENT_TOOLS,
+ MCP_TOOL_TAG_NAME_PREFIX,
+ MULTI_AGENT_TOOLS,
+)
+from letta.data_sources.redis_client import NoopAsyncRedisClient, get_redis_client
+from letta.errors import LettaAgentNotFoundError
+from letta.functions.functions import derive_openai_json_schema, parse_source_code
+from letta.functions.mcp_client.types import MCPTool
+from letta.helpers import ToolRulesSolver
+from letta.helpers.datetime_helpers import AsyncTimer
+from letta.jobs.types import ItemUpdateInfo, RequestStatusUpdateInfo, StepStatusUpdateInfo
+from letta.orm import Base, Block
+from letta.orm.block_history import BlockHistory
+from letta.orm.errors import NoResultFound, UniqueConstraintViolationError
+from letta.orm.file import FileContent as FileContentModel, FileMetadata as FileMetadataModel
+from letta.schemas.agent import CreateAgent, UpdateAgent
+from letta.schemas.block import Block as PydanticBlock, BlockUpdate, CreateBlock
+from letta.schemas.embedding_config import EmbeddingConfig
+from letta.schemas.enums import (
+ ActorType,
+ AgentStepStatus,
+ FileProcessingStatus,
+ JobStatus,
+ JobType,
+ MessageRole,
+ ProviderType,
+ SandboxType,
+ StepStatus,
+ TagMatchMode,
+ ToolType,
+ VectorDBProvider,
+)
+from letta.schemas.environment_variables import SandboxEnvironmentVariableCreate, SandboxEnvironmentVariableUpdate
+from letta.schemas.file import FileMetadata, FileMetadata as PydanticFileMetadata
+from letta.schemas.identity import IdentityCreate, IdentityProperty, IdentityPropertyType, IdentityType, IdentityUpdate, IdentityUpsert
+from letta.schemas.job import BatchJob, Job, Job as PydanticJob, JobUpdate, LettaRequestConfig
+from letta.schemas.letta_message import UpdateAssistantMessage, UpdateReasoningMessage, UpdateSystemMessage, UpdateUserMessage
+from letta.schemas.letta_message_content import TextContent
+from letta.schemas.letta_stop_reason import LettaStopReason, StopReasonType
+from letta.schemas.llm_batch_job import AgentStepState, LLMBatchItem
+from letta.schemas.llm_config import LLMConfig
+from letta.schemas.message import Message as PydanticMessage, MessageCreate, MessageUpdate
+from letta.schemas.openai.chat_completion_response import UsageStatistics
+from letta.schemas.organization import Organization, Organization as PydanticOrganization, OrganizationUpdate
+from letta.schemas.passage import Passage as PydanticPassage
+from letta.schemas.pip_requirement import PipRequirement
+from letta.schemas.run import Run as PydanticRun
+from letta.schemas.sandbox_config import E2BSandboxConfig, LocalSandboxConfig, SandboxConfigCreate, SandboxConfigUpdate
+from letta.schemas.source import Source as PydanticSource, SourceUpdate
+from letta.schemas.tool import Tool as PydanticTool, ToolCreate, ToolUpdate
+from letta.schemas.tool_rule import InitToolRule
+from letta.schemas.user import User as PydanticUser, UserUpdate
+from letta.server.db import db_registry
+from letta.server.server import SyncServer
+from letta.services.block_manager import BlockManager
+from letta.services.helpers.agent_manager_helper import calculate_base_tools, calculate_multi_agent_tools, validate_agent_exists_async
+from letta.services.step_manager import FeedbackType
+from letta.settings import settings, tool_settings
+from letta.utils import calculate_file_defaults_based_on_context_window
+from tests.helpers.utils import comprehensive_agent_checks, validate_context_window_overview
+from tests.utils import random_string
+
+# ======================================================================================================================
+# Helper Functions
+# ======================================================================================================================
+
+
+async def _count_file_content_rows(session, file_id: str) -> int:
+ q = select(func.count()).select_from(FileContentModel).where(FileContentModel.file_id == file_id)
+ result = await session.execute(q)
+ return result.scalar_one()
+
+
+# ======================================================================================================================
+# AgentManager Tests - Basic
+# ======================================================================================================================
+async def test_validate_agent_exists_async(server: SyncServer, comprehensive_test_agent_fixture, default_user):
+ """Test the validate_agent_exists_async helper function"""
+ created_agent, _ = comprehensive_test_agent_fixture
+
+ # test with valid agent
+ async with db_registry.async_session() as session:
+ # should not raise exception
+ await validate_agent_exists_async(session, created_agent.id, default_user)
+
+ # test with non-existent agent
+ async with db_registry.async_session() as session:
+ with pytest.raises(LettaAgentNotFoundError):
+ await validate_agent_exists_async(session, "non-existent-id", default_user)
+
+
+async def test_create_get_list_agent(server: SyncServer, comprehensive_test_agent_fixture, default_user):
+ # Test agent creation
+ created_agent, create_agent_request = comprehensive_test_agent_fixture
+ comprehensive_agent_checks(created_agent, create_agent_request, actor=default_user)
+
+ # Test get agent
+ get_agent = await server.agent_manager.get_agent_by_id_async(agent_id=created_agent.id, actor=default_user)
+ comprehensive_agent_checks(get_agent, create_agent_request, actor=default_user)
+
+ # Test get agent name
+ agents = await server.agent_manager.list_agents_async(name=created_agent.name, actor=default_user)
+ get_agent_name = agents[0]
+ comprehensive_agent_checks(get_agent_name, create_agent_request, actor=default_user)
+
+ # Test list agent
+ list_agents = await server.agent_manager.list_agents_async(actor=default_user)
+ assert len(list_agents) == 1
+ comprehensive_agent_checks(list_agents[0], create_agent_request, actor=default_user)
+
+ # Test deleting the agent
+ await server.agent_manager.delete_agent_async(get_agent.id, default_user)
+ list_agents = await server.agent_manager.list_agents_async(actor=default_user)
+ assert len(list_agents) == 0
+
+
+async def test_create_agent_include_base_tools(server: SyncServer, default_user):
+ """Test agent creation with include_default_source=True"""
+ # Upsert base tools
+ await server.tool_manager.upsert_base_tools_async(actor=default_user)
+
+ memory_blocks = [CreateBlock(label="human", value="TestUser"), CreateBlock(label="persona", value="I am a test assistant")]
+
+ create_agent_request = CreateAgent(
+ name="test_default_source_agent",
+ system="test system",
+ memory_blocks=memory_blocks,
+ llm_config=LLMConfig.default_config("gpt-4o-mini"),
+ embedding_config=EmbeddingConfig.default_config(provider="openai"),
+ include_base_tools=True,
+ )
+
+ # Create the agent
+ created_agent = await server.agent_manager.create_agent_async(
+ create_agent_request,
+ actor=default_user,
+ )
+
+ # Assert the tools exist
+ tool_names = [t.name for t in created_agent.tools]
+ expected_tools = calculate_base_tools(is_v2=True)
+ assert sorted(tool_names) == sorted(expected_tools)
+
+
+async def test_create_agent_base_tool_rules_excluded_providers(server: SyncServer, default_user):
+ """Test that include_base_tool_rules is overridden to False for excluded providers"""
+ # Upsert base tools
+ await server.tool_manager.upsert_base_tools_async(actor=default_user)
+
+ memory_blocks = [CreateBlock(label="human", value="TestUser"), CreateBlock(label="persona", value="I am a test assistant")]
+
+ # Test with excluded provider (openai)
+ create_agent_request = CreateAgent(
+ name="test_excluded_provider_agent",
+ system="test system",
+ memory_blocks=memory_blocks,
+ llm_config=LLMConfig.default_config("gpt-4o-mini"), # This has model_endpoint_type="openai"
+ embedding_config=EmbeddingConfig.default_config(provider="openai"),
+ include_base_tool_rules=False,
+ )
+
+ # Create the agent
+ created_agent = await server.agent_manager.create_agent_async(
+ create_agent_request,
+ actor=default_user,
+ )
+
+ # Assert that no base tool rules were added (since include_base_tool_rules was overridden to False)
+ print(created_agent.tool_rules)
+ assert created_agent.tool_rules is None or len(created_agent.tool_rules) == 0
+
+
+async def test_create_agent_base_tool_rules_non_excluded_providers(server: SyncServer, default_user):
+ """Test that include_base_tool_rules is NOT overridden for non-excluded providers"""
+ # Upsert base tools
+ await server.tool_manager.upsert_base_tools_async(actor=default_user)
+
+ memory_blocks = [CreateBlock(label="human", value="TestUser"), CreateBlock(label="persona", value="I am a test assistant")]
+
+ # Test with non-excluded provider (together)
+ create_agent_request = CreateAgent(
+ name="test_non_excluded_provider_agent",
+ system="test system",
+ memory_blocks=memory_blocks,
+ llm_config=LLMConfig(
+ model="llama-3.1-8b-instruct",
+ model_endpoint_type="together", # Model doesn't match EXCLUDE_MODEL_KEYWORDS_FROM_BASE_TOOL_RULES
+ model_endpoint="https://api.together.xyz",
+ context_window=8192,
+ ),
+ embedding_config=EmbeddingConfig.default_config(provider="openai"),
+ include_base_tool_rules=True, # Should remain True
+ )
+
+ # Create the agent
+ created_agent = await server.agent_manager.create_agent_async(
+ create_agent_request,
+ actor=default_user,
+ )
+
+ # Assert that base tool rules were added (since include_base_tool_rules remained True)
+ assert created_agent.tool_rules is not None
+ assert len(created_agent.tool_rules) > 0
+
+
+async def test_calculate_multi_agent_tools(set_letta_environment):
+ """Test that calculate_multi_agent_tools excludes local-only tools in production."""
+ result = calculate_multi_agent_tools()
+
+ if settings.environment == "PRODUCTION":
+ # Production environment should exclude local-only tools
+ expected_tools = set(MULTI_AGENT_TOOLS) - set(LOCAL_ONLY_MULTI_AGENT_TOOLS)
+ assert result == expected_tools, "Production should exclude local-only multi-agent tools"
+ assert not set(LOCAL_ONLY_MULTI_AGENT_TOOLS).intersection(result), "Production should not include local-only tools"
+
+ # Verify specific tools
+ assert "send_message_to_agent_and_wait_for_reply" in result, "Standard multi-agent tools should be in production"
+ assert "send_message_to_agents_matching_tags" in result, "Standard multi-agent tools should be in production"
+ assert "send_message_to_agent_async" not in result, "Local-only tools should not be in production"
+ else:
+ # Non-production environment should include all multi-agent tools
+ assert result == set(MULTI_AGENT_TOOLS), "Non-production should include all multi-agent tools"
+ assert set(LOCAL_ONLY_MULTI_AGENT_TOOLS).issubset(result), "Non-production should include local-only tools"
+
+ # Verify specific tools
+ assert "send_message_to_agent_and_wait_for_reply" in result, "All multi-agent tools should be in non-production"
+ assert "send_message_to_agents_matching_tags" in result, "All multi-agent tools should be in non-production"
+ assert "send_message_to_agent_async" in result, "Local-only tools should be in non-production"
+
+
+async def test_upsert_base_tools_excludes_local_only_in_production(server: SyncServer, default_user, set_letta_environment):
+ """Test that upsert_base_tools excludes local-only multi-agent tools in production."""
+ # Upsert all base tools
+ tools = await server.tool_manager.upsert_base_tools_async(actor=default_user)
+ tool_names = {tool.name for tool in tools}
+
+ if settings.environment == "PRODUCTION":
+ # Production environment should exclude local-only multi-agent tools
+ for local_only_tool in LOCAL_ONLY_MULTI_AGENT_TOOLS:
+ assert local_only_tool not in tool_names, f"Local-only tool '{local_only_tool}' should not be upserted in production"
+
+ # But should include standard multi-agent tools
+ standard_multi_agent_tools = set(MULTI_AGENT_TOOLS) - set(LOCAL_ONLY_MULTI_AGENT_TOOLS)
+ for standard_tool in standard_multi_agent_tools:
+ assert standard_tool in tool_names, f"Standard multi-agent tool '{standard_tool}' should be upserted in production"
+ else:
+ # Non-production environment should include all multi-agent tools
+ for tool in MULTI_AGENT_TOOLS:
+ assert tool in tool_names, f"Multi-agent tool '{tool}' should be upserted in non-production"
+
+
+async def test_upsert_multi_agent_tools_only(server: SyncServer, default_user, set_letta_environment):
+ """Test that upserting only multi-agent tools respects production filtering."""
+ from letta.schemas.enums import ToolType
+
+ # Upsert only multi-agent tools
+ tools = await server.tool_manager.upsert_base_tools_async(actor=default_user, allowed_types={ToolType.LETTA_MULTI_AGENT_CORE})
+ tool_names = {tool.name for tool in tools}
+
+ if settings.environment == "PRODUCTION":
+ # Should only have non-local multi-agent tools
+ expected_tools = set(MULTI_AGENT_TOOLS) - set(LOCAL_ONLY_MULTI_AGENT_TOOLS)
+ assert tool_names == expected_tools, "Production multi-agent upsert should exclude local-only tools"
+ assert "send_message_to_agent_async" not in tool_names, "Local-only async tool should not be upserted in production"
+ else:
+ # Should have all multi-agent tools
+ assert tool_names == set(MULTI_AGENT_TOOLS), "Non-production multi-agent upsert should include all tools"
+ assert "send_message_to_agent_async" in tool_names, "Local-only async tool should be upserted in non-production"
+
+
+async def test_create_agent_with_default_source(server: SyncServer, default_user, print_tool, default_block):
+ """Test agent creation with include_default_source=True"""
+ memory_blocks = [CreateBlock(label="human", value="TestUser"), CreateBlock(label="persona", value="I am a test assistant")]
+
+ create_agent_request = CreateAgent(
+ name="test_default_source_agent",
+ system="test system",
+ memory_blocks=memory_blocks,
+ llm_config=LLMConfig.default_config("gpt-4o-mini"),
+ embedding_config=EmbeddingConfig.default_config(provider="openai"),
+ block_ids=[default_block.id],
+ tool_ids=[print_tool.id],
+ include_default_source=True, # This is the key field we're testing
+ include_base_tools=False,
+ )
+
+ # Create the agent
+ created_agent = await server.agent_manager.create_agent_async(
+ create_agent_request,
+ actor=default_user,
+ )
+
+ # Verify agent was created
+ assert created_agent is not None
+ assert created_agent.name == "test_default_source_agent"
+
+ # Verify that a default source was created and attached
+ attached_sources = await server.agent_manager.list_attached_sources_async(agent_id=created_agent.id, actor=default_user)
+
+ # Should have exactly one source (the default one)
+ assert len(attached_sources) == 1
+ auto_default_source = attached_sources[0]
+
+ # Verify the default source properties
+ assert created_agent.name in auto_default_source.name
+ assert auto_default_source.embedding_config.embedding_endpoint_type == "openai"
+
+ # Test with include_default_source=False
+ create_agent_request_no_source = CreateAgent(
+ name="test_no_default_source_agent",
+ system="test system",
+ memory_blocks=memory_blocks,
+ llm_config=LLMConfig.default_config("gpt-4o-mini"),
+ embedding_config=EmbeddingConfig.default_config(provider="openai"),
+ block_ids=[default_block.id],
+ tool_ids=[print_tool.id],
+ include_default_source=False, # Explicitly set to False
+ include_base_tools=False,
+ )
+
+ created_agent_no_source = await server.agent_manager.create_agent_async(
+ create_agent_request_no_source,
+ actor=default_user,
+ )
+
+ # Verify no sources are attached
+ attached_sources_no_source = await server.agent_manager.list_attached_sources_async(
+ agent_id=created_agent_no_source.id, actor=default_user
+ )
+
+ assert len(attached_sources_no_source) == 0
+
+ # Clean up
+ await server.agent_manager.delete_agent_async(created_agent.id, default_user)
+ await server.agent_manager.delete_agent_async(created_agent_no_source.id, default_user)
+
+
+async def test_get_context_window_basic(
+ server: SyncServer, comprehensive_test_agent_fixture, default_user, default_file, set_letta_environment
+):
+ # Test agent creation
+ created_agent, create_agent_request = comprehensive_test_agent_fixture
+
+ # Attach a file
+ assoc, closed_files = await server.file_agent_manager.attach_file(
+ agent_id=created_agent.id,
+ file_id=default_file.id,
+ file_name=default_file.file_name,
+ source_id=default_file.source_id,
+ actor=default_user,
+ visible_content="hello",
+ max_files_open=created_agent.max_files_open,
+ )
+
+ # Get context window and check for basic appearances
+ context_window_overview = await server.agent_manager.get_context_window(agent_id=created_agent.id, actor=default_user)
+ validate_context_window_overview(created_agent, context_window_overview, assoc)
+
+ # Test deleting the agent
+ await server.agent_manager.delete_agent_async(created_agent.id, default_user)
+ list_agents = await server.agent_manager.list_agents_async(actor=default_user)
+ assert len(list_agents) == 0
+
+
+async def test_create_agent_passed_in_initial_messages(server: SyncServer, default_user, default_block):
+ memory_blocks = [CreateBlock(label="human", value="BananaBoy"), CreateBlock(label="persona", value="I am a helpful assistant")]
+ create_agent_request = CreateAgent(
+ system="test system",
+ memory_blocks=memory_blocks,
+ llm_config=LLMConfig.default_config("gpt-4o-mini"),
+ embedding_config=EmbeddingConfig.default_config(provider="openai"),
+ block_ids=[default_block.id],
+ tags=["a", "b"],
+ description="test_description",
+ initial_message_sequence=[MessageCreate(role=MessageRole.user, content="hello world")],
+ include_base_tools=False,
+ )
+ agent_state = await server.agent_manager.create_agent_async(
+ create_agent_request,
+ actor=default_user,
+ )
+ assert await server.message_manager.size_async(agent_id=agent_state.id, actor=default_user) == 2
+ init_messages = await server.message_manager.get_messages_by_ids_async(message_ids=agent_state.message_ids, actor=default_user)
+
+ # Check that the system appears in the first initial message
+ assert create_agent_request.system in init_messages[0].content[0].text
+ assert create_agent_request.memory_blocks[0].value in init_messages[0].content[0].text
+ # Check that the second message is the passed in initial message seq
+ assert create_agent_request.initial_message_sequence[0].role == init_messages[1].role
+ assert create_agent_request.initial_message_sequence[0].content in init_messages[1].content[0].text
+
+
+async def test_create_agent_default_initial_message(server: SyncServer, default_user, default_block):
+ memory_blocks = [CreateBlock(label="human", value="BananaBoy"), CreateBlock(label="persona", value="I am a helpful assistant")]
+ create_agent_request = CreateAgent(
+ system="test system",
+ memory_blocks=memory_blocks,
+ llm_config=LLMConfig.default_config("gpt-4o-mini"),
+ embedding_config=EmbeddingConfig.default_config(provider="openai"),
+ block_ids=[default_block.id],
+ tags=["a", "b"],
+ description="test_description",
+ include_base_tools=False,
+ )
+ agent_state = await server.agent_manager.create_agent_async(
+ create_agent_request,
+ actor=default_user,
+ )
+ assert await server.message_manager.size_async(agent_id=agent_state.id, actor=default_user) == 4
+ init_messages = await server.message_manager.get_messages_by_ids_async(message_ids=agent_state.message_ids, actor=default_user)
+ # Check that the system appears in the first initial message
+ assert create_agent_request.system in init_messages[0].content[0].text
+ assert create_agent_request.memory_blocks[0].value in init_messages[0].content[0].text
+
+
+async def test_create_agent_with_json_in_system_message(server: SyncServer, default_user, default_block):
+ system_prompt = (
+ "You are an expert teaching agent with encyclopedic knowledge. "
+ "When you receive a topic, query the external database for more "
+ "information. Format the queries as a JSON list of queries making "
+ "sure to include your reasoning for that query, e.g. "
+ "{'query1' : 'reason1', 'query2' : 'reason2'}"
+ )
+ create_agent_request = CreateAgent(
+ system=system_prompt,
+ llm_config=LLMConfig.default_config("gpt-4o-mini"),
+ embedding_config=EmbeddingConfig.default_config(provider="openai"),
+ block_ids=[default_block.id],
+ tags=["a", "b"],
+ description="test_description",
+ include_base_tools=False,
+ )
+ agent_state = await server.agent_manager.create_agent_async(
+ create_agent_request,
+ actor=default_user,
+ )
+ assert agent_state is not None
+ system_message_id = agent_state.message_ids[0]
+ system_message = await server.message_manager.get_message_by_id_async(message_id=system_message_id, actor=default_user)
+ assert system_prompt in system_message.content[0].text
+ assert default_block.value in system_message.content[0].text
+ await server.agent_manager.delete_agent_async(agent_id=agent_state.id, actor=default_user)
+
+
+async def test_update_agent(server: SyncServer, comprehensive_test_agent_fixture, other_tool, other_source, other_block, default_user):
+ agent, _ = comprehensive_test_agent_fixture
+ update_agent_request = UpdateAgent(
+ name="train_agent",
+ description="train description",
+ tool_ids=[other_tool.id],
+ source_ids=[other_source.id],
+ block_ids=[other_block.id],
+ tool_rules=[InitToolRule(tool_name=other_tool.name)],
+ tags=["c", "d"],
+ system="train system",
+ llm_config=LLMConfig.default_config("gpt-4o-mini"),
+ embedding_config=EmbeddingConfig.default_config(model_name="letta"),
+ message_ids=["10", "20"],
+ metadata={"train_key": "train_value"},
+ tool_exec_environment_variables={"test_env_var_key_a": "a", "new_tool_exec_key": "n"},
+ message_buffer_autoclear=False,
+ )
+
+ last_updated_timestamp = agent.updated_at
+ updated_agent = await server.agent_manager.update_agent_async(agent.id, update_agent_request, actor=default_user)
+ comprehensive_agent_checks(updated_agent, update_agent_request, actor=default_user)
+ assert updated_agent.message_ids == update_agent_request.message_ids
+ assert updated_agent.updated_at > last_updated_timestamp
+
+
+async def test_agent_file_defaults_based_on_context_window(server: SyncServer, default_user, default_block):
+ """Test that file-related defaults are set based on the model's context window size"""
+
+ # test with small context window model (8k)
+ llm_config_small = LLMConfig.default_config("gpt-4o-mini")
+ llm_config_small.context_window = 8000
+ create_agent_request = CreateAgent(
+ name="test_agent_small_context",
+ llm_config=llm_config_small,
+ embedding_config=EmbeddingConfig.default_config(provider="openai"),
+ block_ids=[default_block.id],
+ include_base_tools=False,
+ )
+ agent_state = await server.agent_manager.create_agent_async(
+ create_agent_request,
+ actor=default_user,
+ )
+ assert agent_state.max_files_open == 3
+ assert (
+ agent_state.per_file_view_window_char_limit == calculate_file_defaults_based_on_context_window(llm_config_small.context_window)[1]
+ )
+ await server.agent_manager.delete_agent_async(agent_id=agent_state.id, actor=default_user)
+
+ # test with medium context window model (32k)
+ llm_config_medium = LLMConfig.default_config("gpt-4o-mini")
+ llm_config_medium.context_window = 32000
+ create_agent_request = CreateAgent(
+ name="test_agent_medium_context",
+ llm_config=llm_config_medium,
+ embedding_config=EmbeddingConfig.default_config(provider="openai"),
+ block_ids=[default_block.id],
+ include_base_tools=False,
+ )
+ agent_state = await server.agent_manager.create_agent_async(
+ create_agent_request,
+ actor=default_user,
+ )
+ assert agent_state.max_files_open == 5
+ assert (
+ agent_state.per_file_view_window_char_limit == calculate_file_defaults_based_on_context_window(llm_config_medium.context_window)[1]
+ )
+ await server.agent_manager.delete_agent_async(agent_id=agent_state.id, actor=default_user)
+
+ # test with large context window model (128k)
+ llm_config_large = LLMConfig.default_config("gpt-4o-mini")
+ llm_config_large.context_window = 128000
+ create_agent_request = CreateAgent(
+ name="test_agent_large_context",
+ llm_config=llm_config_large,
+ embedding_config=EmbeddingConfig.default_config(provider="openai"),
+ block_ids=[default_block.id],
+ include_base_tools=False,
+ )
+ agent_state = await server.agent_manager.create_agent_async(
+ create_agent_request,
+ actor=default_user,
+ )
+ assert agent_state.max_files_open == 10
+ assert (
+ agent_state.per_file_view_window_char_limit == calculate_file_defaults_based_on_context_window(llm_config_large.context_window)[1]
+ )
+ await server.agent_manager.delete_agent_async(agent_id=agent_state.id, actor=default_user)
+
+
+async def test_agent_file_defaults_explicit_values(server: SyncServer, default_user, default_block):
+ """Test that explicitly set file-related values are respected"""
+
+ llm_config_explicit = LLMConfig.default_config("gpt-4o-mini")
+ llm_config_explicit.context_window = 32000 # would normally get defaults of 5 and 30k
+ create_agent_request = CreateAgent(
+ name="test_agent_explicit_values",
+ llm_config=llm_config_explicit,
+ embedding_config=EmbeddingConfig.default_config(provider="openai"),
+ block_ids=[default_block.id],
+ include_base_tools=False,
+ max_files_open=20, # explicit value
+ per_file_view_window_char_limit=500_000, # explicit value
+ )
+ agent_state = await server.agent_manager.create_agent_async(
+ create_agent_request,
+ actor=default_user,
+ )
+ # verify explicit values are used instead of defaults
+ assert agent_state.max_files_open == 20
+ assert agent_state.per_file_view_window_char_limit == 500_000
+ await server.agent_manager.delete_agent_async(agent_id=agent_state.id, actor=default_user)
+
+
+async def test_update_agent_file_fields(server: SyncServer, comprehensive_test_agent_fixture, default_user):
+ """Test updating file-related fields on an existing agent"""
+
+ agent, _ = comprehensive_test_agent_fixture
+
+ # update file-related fields
+ update_request = UpdateAgent(
+ max_files_open=15,
+ per_file_view_window_char_limit=150_000,
+ )
+ updated_agent = await server.agent_manager.update_agent_async(agent.id, update_request, actor=default_user)
+
+ assert updated_agent.max_files_open == 15
+ assert updated_agent.per_file_view_window_char_limit == 150_000
+
+
+# ======================================================================================================================
+# AgentManager Tests - Listing
+# ======================================================================================================================
+
+
+async def test_list_agents_select_fields_empty(server: SyncServer, comprehensive_test_agent_fixture, default_user):
+ # Create an agent using the comprehensive fixture.
+ created_agent, create_agent_request = comprehensive_test_agent_fixture
+
+ # List agents using an empty list for select_fields.
+ agents = await server.agent_manager.list_agents_async(actor=default_user, include_relationships=[])
+ # Assert that the agent is returned and basic fields are present.
+ assert len(agents) >= 1
+ agent = agents[0]
+ assert agent.id is not None
+ assert agent.name is not None
+
+ # Assert no relationships were loaded
+ assert len(agent.tools) == 0
+ assert len(agent.tags) == 0
+
+
+async def test_list_agents_select_fields_none(server: SyncServer, comprehensive_test_agent_fixture, default_user):
+ # Create an agent using the comprehensive fixture.
+ created_agent, create_agent_request = comprehensive_test_agent_fixture
+
+ # List agents using an empty list for select_fields.
+ agents = await server.agent_manager.list_agents_async(actor=default_user, include_relationships=None)
+ # Assert that the agent is returned and basic fields are present.
+ assert len(agents) >= 1
+ agent = agents[0]
+ assert agent.id is not None
+ assert agent.name is not None
+
+ # Assert no relationships were loaded
+ assert len(agent.tools) > 0
+ assert len(agent.tags) > 0
+
+
+async def test_list_agents_select_fields_specific(server: SyncServer, comprehensive_test_agent_fixture, default_user):
+ created_agent, create_agent_request = comprehensive_test_agent_fixture
+
+ # Choose a subset of valid relationship fields.
+ valid_fields = ["tools", "tags"]
+ agents = await server.agent_manager.list_agents_async(actor=default_user, include_relationships=valid_fields)
+ assert len(agents) >= 1
+ agent = agents[0]
+ # Depending on your to_pydantic() implementation,
+ # verify that the fields exist in the returned pydantic model.
+ # (Note: These assertions may require that your CreateAgent fixture sets up these relationships.)
+ assert agent.tools
+ assert sorted(agent.tags) == ["a", "b"]
+ assert not agent.memory.blocks
+
+
+async def test_list_agents_select_fields_invalid(server: SyncServer, comprehensive_test_agent_fixture, default_user):
+ created_agent, create_agent_request = comprehensive_test_agent_fixture
+
+ # Provide field names that are not recognized.
+ invalid_fields = ["foobar", "nonexistent_field"]
+ # The expectation is that these fields are simply ignored.
+ agents = await server.agent_manager.list_agents_async(actor=default_user, include_relationships=invalid_fields)
+ assert len(agents) >= 1
+ agent = agents[0]
+ # Verify that standard fields are still present.c
+ assert agent.id is not None
+ assert agent.name is not None
+
+
+async def test_list_agents_select_fields_duplicates(server: SyncServer, comprehensive_test_agent_fixture, default_user):
+ created_agent, create_agent_request = comprehensive_test_agent_fixture
+
+ # Provide duplicate valid field names.
+ duplicate_fields = ["tools", "tools", "tags", "tags"]
+ agents = await server.agent_manager.list_agents_async(actor=default_user, include_relationships=duplicate_fields)
+ assert len(agents) >= 1
+ agent = agents[0]
+ # Verify that the agent pydantic representation includes the relationships.
+ # Even if duplicates were provided, the query should not break.
+ assert isinstance(agent.tools, list)
+ assert isinstance(agent.tags, list)
+
+
+async def test_list_agents_select_fields_mixed(server: SyncServer, comprehensive_test_agent_fixture, default_user):
+ created_agent, create_agent_request = comprehensive_test_agent_fixture
+
+ # Mix valid fields with an invalid one.
+ mixed_fields = ["tools", "invalid_field"]
+ agents = await server.agent_manager.list_agents_async(actor=default_user, include_relationships=mixed_fields)
+ assert len(agents) >= 1
+ agent = agents[0]
+ # Valid fields should be loaded and accessible.
+ assert agent.tools
+ # Since "invalid_field" is not recognized, it should have no adverse effect.
+ # You might optionally check that no extra attribute is created on the pydantic model.
+ assert not hasattr(agent, "invalid_field")
+
+
+async def test_list_agents_ascending(server: SyncServer, default_user):
+ # Create two agents with known names
+ agent1 = await server.agent_manager.create_agent_async(
+ agent_create=CreateAgent(
+ name="agent_oldest",
+ llm_config=LLMConfig.default_config("gpt-4o-mini"),
+ embedding_config=EmbeddingConfig.default_config(provider="openai"),
+ memory_blocks=[],
+ include_base_tools=False,
+ ),
+ actor=default_user,
+ )
+
+ if USING_SQLITE:
+ time.sleep(CREATE_DELAY_SQLITE)
+
+ agent2 = await server.agent_manager.create_agent_async(
+ agent_create=CreateAgent(
+ name="agent_newest",
+ llm_config=LLMConfig.default_config("gpt-4o-mini"),
+ embedding_config=EmbeddingConfig.default_config(provider="openai"),
+ memory_blocks=[],
+ include_base_tools=False,
+ ),
+ actor=default_user,
+ )
+
+ agents = await server.agent_manager.list_agents_async(actor=default_user, ascending=True)
+ names = [agent.name for agent in agents]
+ assert names.index("agent_oldest") < names.index("agent_newest")
+
+
+async def test_list_agents_descending(server: SyncServer, default_user):
+ # Create two agents with known names
+ agent1 = await server.agent_manager.create_agent_async(
+ agent_create=CreateAgent(
+ name="agent_oldest",
+ llm_config=LLMConfig.default_config("gpt-4o-mini"),
+ embedding_config=EmbeddingConfig.default_config(provider="openai"),
+ memory_blocks=[],
+ include_base_tools=False,
+ ),
+ actor=default_user,
+ )
+
+ if USING_SQLITE:
+ time.sleep(CREATE_DELAY_SQLITE)
+
+ agent2 = await server.agent_manager.create_agent_async(
+ agent_create=CreateAgent(
+ name="agent_newest",
+ llm_config=LLMConfig.default_config("gpt-4o-mini"),
+ embedding_config=EmbeddingConfig.default_config(provider="openai"),
+ memory_blocks=[],
+ include_base_tools=False,
+ ),
+ actor=default_user,
+ )
+
+ agents = await server.agent_manager.list_agents_async(actor=default_user, ascending=False)
+ names = [agent.name for agent in agents]
+ assert names.index("agent_newest") < names.index("agent_oldest")
+
+
+async def test_list_agents_ordering_and_pagination(server: SyncServer, default_user):
+ names = ["alpha_agent", "beta_agent", "gamma_agent"]
+ created_agents = []
+
+ # Create agents in known order
+ for name in names:
+ agent = await server.agent_manager.create_agent_async(
+ agent_create=CreateAgent(
+ name=name,
+ memory_blocks=[],
+ llm_config=LLMConfig.default_config("gpt-4o-mini"),
+ embedding_config=EmbeddingConfig.default_config(provider="openai"),
+ include_base_tools=False,
+ ),
+ actor=default_user,
+ )
+ created_agents.append(agent)
+ if USING_SQLITE:
+ time.sleep(CREATE_DELAY_SQLITE)
+
+ agent_ids = {agent.name: agent.id for agent in created_agents}
+
+ # Ascending (oldest to newest)
+ agents_asc = await server.agent_manager.list_agents_async(actor=default_user, ascending=True)
+ asc_names = [agent.name for agent in agents_asc]
+ assert asc_names.index("alpha_agent") < asc_names.index("beta_agent") < asc_names.index("gamma_agent")
+
+ # Descending (newest to oldest)
+ agents_desc = await server.agent_manager.list_agents_async(actor=default_user, ascending=False)
+ desc_names = [agent.name for agent in agents_desc]
+ assert desc_names.index("gamma_agent") < desc_names.index("beta_agent") < desc_names.index("alpha_agent")
+
+ # After: Get agents after alpha_agent in ascending order (should exclude alpha)
+ after_alpha = await server.agent_manager.list_agents_async(actor=default_user, after=agent_ids["alpha_agent"], ascending=True)
+ after_names = [a.name for a in after_alpha]
+ assert "alpha_agent" not in after_names
+ assert "beta_agent" in after_names
+ assert "gamma_agent" in after_names
+ assert after_names == ["beta_agent", "gamma_agent"]
+
+ # Before: Get agents before gamma_agent in ascending order (should exclude gamma)
+ before_gamma = await server.agent_manager.list_agents_async(actor=default_user, before=agent_ids["gamma_agent"], ascending=True)
+ before_names = [a.name for a in before_gamma]
+ assert "gamma_agent" not in before_names
+ assert "alpha_agent" in before_names
+ assert "beta_agent" in before_names
+ assert before_names == ["alpha_agent", "beta_agent"]
+
+ # After: Get agents after gamma_agent in descending order (should exclude gamma, return beta then alpha)
+ after_gamma_desc = await server.agent_manager.list_agents_async(actor=default_user, after=agent_ids["gamma_agent"], ascending=False)
+ after_names_desc = [a.name for a in after_gamma_desc]
+ assert after_names_desc == ["beta_agent", "alpha_agent"]
+
+ # Before: Get agents before alpha_agent in descending order (should exclude alpha)
+ before_alpha_desc = await server.agent_manager.list_agents_async(actor=default_user, before=agent_ids["alpha_agent"], ascending=False)
+ before_names_desc = [a.name for a in before_alpha_desc]
+ assert before_names_desc == ["gamma_agent", "beta_agent"]
diff --git a/tests/managers/test_agent_tag_manager.py b/tests/managers/test_agent_tag_manager.py
new file mode 100644
index 000000000..6eaf8809e
--- /dev/null
+++ b/tests/managers/test_agent_tag_manager.py
@@ -0,0 +1,421 @@
+import asyncio
+import json
+import logging
+import os
+import random
+import re
+import string
+import time
+import uuid
+from datetime import datetime, timedelta, timezone
+from typing import List
+from unittest.mock import AsyncMock, Mock, patch
+
+import pytest
+from _pytest.python_api import approx
+from anthropic.types.beta import BetaMessage
+from anthropic.types.beta.messages import BetaMessageBatchIndividualResponse, BetaMessageBatchSucceededResult
+
+# Import shared fixtures and constants from conftest
+from conftest import (
+ CREATE_DELAY_SQLITE,
+ DEFAULT_EMBEDDING_CONFIG,
+ USING_SQLITE,
+)
+from openai.types.chat.chat_completion_message_tool_call import ChatCompletionMessageToolCall as OpenAIToolCall, Function as OpenAIFunction
+from sqlalchemy import func, select
+from sqlalchemy.exc import IntegrityError, InvalidRequestError
+from sqlalchemy.orm.exc import StaleDataError
+
+from letta.config import LettaConfig
+from letta.constants import (
+ BASE_MEMORY_TOOLS,
+ BASE_SLEEPTIME_TOOLS,
+ BASE_TOOLS,
+ BASE_VOICE_SLEEPTIME_CHAT_TOOLS,
+ BASE_VOICE_SLEEPTIME_TOOLS,
+ BUILTIN_TOOLS,
+ DEFAULT_ORG_ID,
+ DEFAULT_ORG_NAME,
+ FILES_TOOLS,
+ LETTA_TOOL_EXECUTION_DIR,
+ LETTA_TOOL_SET,
+ LOCAL_ONLY_MULTI_AGENT_TOOLS,
+ MCP_TOOL_TAG_NAME_PREFIX,
+ MULTI_AGENT_TOOLS,
+)
+from letta.data_sources.redis_client import NoopAsyncRedisClient, get_redis_client
+from letta.errors import LettaAgentNotFoundError
+from letta.functions.functions import derive_openai_json_schema, parse_source_code
+from letta.functions.mcp_client.types import MCPTool
+from letta.helpers import ToolRulesSolver
+from letta.helpers.datetime_helpers import AsyncTimer
+from letta.jobs.types import ItemUpdateInfo, RequestStatusUpdateInfo, StepStatusUpdateInfo
+from letta.orm import Base, Block
+from letta.orm.block_history import BlockHistory
+from letta.orm.errors import NoResultFound, UniqueConstraintViolationError
+from letta.orm.file import FileContent as FileContentModel, FileMetadata as FileMetadataModel
+from letta.schemas.agent import CreateAgent, UpdateAgent
+from letta.schemas.block import Block as PydanticBlock, BlockUpdate, CreateBlock
+from letta.schemas.embedding_config import EmbeddingConfig
+from letta.schemas.enums import (
+ ActorType,
+ AgentStepStatus,
+ FileProcessingStatus,
+ JobStatus,
+ JobType,
+ MessageRole,
+ ProviderType,
+ SandboxType,
+ StepStatus,
+ TagMatchMode,
+ ToolType,
+ VectorDBProvider,
+)
+from letta.schemas.environment_variables import SandboxEnvironmentVariableCreate, SandboxEnvironmentVariableUpdate
+from letta.schemas.file import FileMetadata, FileMetadata as PydanticFileMetadata
+from letta.schemas.identity import IdentityCreate, IdentityProperty, IdentityPropertyType, IdentityType, IdentityUpdate, IdentityUpsert
+from letta.schemas.job import BatchJob, Job, Job as PydanticJob, JobUpdate, LettaRequestConfig
+from letta.schemas.letta_message import UpdateAssistantMessage, UpdateReasoningMessage, UpdateSystemMessage, UpdateUserMessage
+from letta.schemas.letta_message_content import TextContent
+from letta.schemas.letta_stop_reason import LettaStopReason, StopReasonType
+from letta.schemas.llm_batch_job import AgentStepState, LLMBatchItem
+from letta.schemas.llm_config import LLMConfig
+from letta.schemas.message import Message as PydanticMessage, MessageCreate, MessageUpdate
+from letta.schemas.openai.chat_completion_response import UsageStatistics
+from letta.schemas.organization import Organization, Organization as PydanticOrganization, OrganizationUpdate
+from letta.schemas.passage import Passage as PydanticPassage
+from letta.schemas.pip_requirement import PipRequirement
+from letta.schemas.run import Run as PydanticRun
+from letta.schemas.sandbox_config import E2BSandboxConfig, LocalSandboxConfig, SandboxConfigCreate, SandboxConfigUpdate
+from letta.schemas.source import Source as PydanticSource, SourceUpdate
+from letta.schemas.tool import Tool as PydanticTool, ToolCreate, ToolUpdate
+from letta.schemas.tool_rule import InitToolRule
+from letta.schemas.user import User as PydanticUser, UserUpdate
+from letta.server.db import db_registry
+from letta.server.server import SyncServer
+from letta.services.block_manager import BlockManager
+from letta.services.helpers.agent_manager_helper import calculate_base_tools, calculate_multi_agent_tools, validate_agent_exists_async
+from letta.services.step_manager import FeedbackType
+from letta.settings import settings, tool_settings
+from letta.utils import calculate_file_defaults_based_on_context_window
+from tests.helpers.utils import comprehensive_agent_checks, validate_context_window_overview
+from tests.utils import random_string
+
+# ======================================================================================================================
+# AgentManager Tests - Tags Relationship
+# ======================================================================================================================
+
+
+async def test_list_agents_matching_all_tags(server: SyncServer, default_user, agent_with_tags):
+ agents = await server.agent_manager.list_agents_matching_tags_async(
+ actor=default_user,
+ match_all=["primary_agent", "benefit_1"],
+ match_some=[],
+ )
+ assert len(agents) == 2 # agent1 and agent3 match
+ assert {a.name for a in agents} == {"agent1", "agent3"}
+
+
+async def test_list_agents_matching_some_tags(server: SyncServer, default_user, agent_with_tags):
+ agents = await server.agent_manager.list_agents_matching_tags_async(
+ actor=default_user,
+ match_all=["primary_agent"],
+ match_some=["benefit_1", "benefit_2"],
+ )
+ assert len(agents) == 3 # All agents match
+ assert {a.name for a in agents} == {"agent1", "agent2", "agent3"}
+
+
+async def test_list_agents_matching_all_and_some_tags(server: SyncServer, default_user, agent_with_tags):
+ agents = await server.agent_manager.list_agents_matching_tags_async(
+ actor=default_user,
+ match_all=["primary_agent", "benefit_1"],
+ match_some=["benefit_2", "nonexistent"],
+ )
+ assert len(agents) == 1 # Only agent3 matches
+ assert agents[0].name == "agent3"
+
+
+async def test_list_agents_matching_no_tags(server: SyncServer, default_user, agent_with_tags):
+ agents = await server.agent_manager.list_agents_matching_tags_async(
+ actor=default_user,
+ match_all=["primary_agent", "nonexistent_tag"],
+ match_some=["benefit_1", "benefit_2"],
+ )
+ assert len(agents) == 0 # No agent should match
+
+
+async def test_list_agents_by_tags_match_all(server: SyncServer, sarah_agent, charles_agent, default_user):
+ """Test listing agents that have ALL specified tags."""
+ # Create agents with multiple tags
+ await server.agent_manager.update_agent_async(sarah_agent.id, UpdateAgent(tags=["test", "production", "gpt4"]), actor=default_user)
+ await server.agent_manager.update_agent_async(charles_agent.id, UpdateAgent(tags=["test", "development", "gpt4"]), actor=default_user)
+
+ # Search for agents with all specified tags
+ agents = await server.agent_manager.list_agents_async(actor=default_user, tags=["test", "gpt4"], match_all_tags=True)
+ assert len(agents) == 2
+ agent_ids = [a.id for a in agents]
+ assert sarah_agent.id in agent_ids
+ assert charles_agent.id in agent_ids
+
+ # Search for tags that only sarah_agent has
+ agents = await server.agent_manager.list_agents_async(actor=default_user, tags=["test", "production"], match_all_tags=True)
+ assert len(agents) == 1
+ assert agents[0].id == sarah_agent.id
+
+
+async def test_list_agents_by_tags_match_any(server: SyncServer, sarah_agent, charles_agent, default_user):
+ """Test listing agents that have ANY of the specified tags."""
+ # Create agents with different tags
+ await server.agent_manager.update_agent_async(sarah_agent.id, UpdateAgent(tags=["production", "gpt4"]), actor=default_user)
+ await server.agent_manager.update_agent_async(charles_agent.id, UpdateAgent(tags=["development", "gpt3"]), actor=default_user)
+
+ # Search for agents with any of the specified tags
+ agents = await server.agent_manager.list_agents_async(actor=default_user, tags=["production", "development"], match_all_tags=False)
+ assert len(agents) == 2
+ agent_ids = [a.id for a in agents]
+ assert sarah_agent.id in agent_ids
+ assert charles_agent.id in agent_ids
+
+ # Search for tags where only sarah_agent matches
+ agents = await server.agent_manager.list_agents_async(actor=default_user, tags=["production", "nonexistent"], match_all_tags=False)
+ assert len(agents) == 1
+ assert agents[0].id == sarah_agent.id
+
+
+async def test_list_agents_by_tags_no_matches(server: SyncServer, sarah_agent, charles_agent, default_user):
+ """Test listing agents when no tags match."""
+ # Create agents with tags
+ await server.agent_manager.update_agent_async(sarah_agent.id, UpdateAgent(tags=["production", "gpt4"]), actor=default_user)
+ await server.agent_manager.update_agent_async(charles_agent.id, UpdateAgent(tags=["development", "gpt3"]), actor=default_user)
+
+ # Search for nonexistent tags
+ agents = await server.agent_manager.list_agents_async(actor=default_user, tags=["nonexistent1", "nonexistent2"], match_all_tags=True)
+ assert len(agents) == 0
+
+ agents = await server.agent_manager.list_agents_async(actor=default_user, tags=["nonexistent1", "nonexistent2"], match_all_tags=False)
+ assert len(agents) == 0
+
+
+async def test_list_agents_by_tags_with_other_filters(server: SyncServer, sarah_agent, charles_agent, default_user):
+ """Test combining tag search with other filters."""
+ # Create agents with specific names and tags
+ await server.agent_manager.update_agent_async(
+ sarah_agent.id, UpdateAgent(name="production_agent", tags=["production", "gpt4"]), actor=default_user
+ )
+ await server.agent_manager.update_agent_async(
+ charles_agent.id, UpdateAgent(name="test_agent", tags=["production", "gpt3"]), actor=default_user
+ )
+
+ # List agents with specific tag and name pattern
+ agents = await server.agent_manager.list_agents_async(
+ actor=default_user, tags=["production"], match_all_tags=True, name="production_agent"
+ )
+ assert len(agents) == 1
+ assert agents[0].id == sarah_agent.id
+
+
+async def test_list_agents_by_tags_pagination(server: SyncServer, default_user, default_organization):
+ """Test pagination when listing agents by tags."""
+ # Create first agent
+ agent1 = await server.agent_manager.create_agent_async(
+ agent_create=CreateAgent(
+ name="agent1",
+ tags=["pagination_test", "tag1"],
+ llm_config=LLMConfig.default_config("gpt-4o-mini"),
+ embedding_config=EmbeddingConfig.default_config(provider="openai"),
+ memory_blocks=[],
+ include_base_tools=False,
+ ),
+ actor=default_user,
+ )
+
+ if USING_SQLITE:
+ time.sleep(CREATE_DELAY_SQLITE) # Ensure distinct created_at timestamps
+
+ # Create second agent
+ agent2 = await server.agent_manager.create_agent_async(
+ agent_create=CreateAgent(
+ name="agent2",
+ tags=["pagination_test", "tag2"],
+ llm_config=LLMConfig.default_config("gpt-4o-mini"),
+ embedding_config=EmbeddingConfig.default_config(provider="openai"),
+ memory_blocks=[],
+ include_base_tools=False,
+ ),
+ actor=default_user,
+ )
+
+ # Get first page
+ first_page = await server.agent_manager.list_agents_async(actor=default_user, tags=["pagination_test"], match_all_tags=True, limit=1)
+ assert len(first_page) == 1
+ first_agent_id = first_page[0].id
+
+ # Get second page using cursor
+ second_page = await server.agent_manager.list_agents_async(
+ actor=default_user, tags=["pagination_test"], match_all_tags=True, after=first_agent_id, limit=1
+ )
+ assert len(second_page) == 1
+ assert second_page[0].id != first_agent_id
+
+ # Get previous page using before
+ prev_page = await server.agent_manager.list_agents_async(
+ actor=default_user, tags=["pagination_test"], match_all_tags=True, before=second_page[0].id, limit=1
+ )
+ assert len(prev_page) == 1
+ assert prev_page[0].id == first_agent_id
+
+ # Verify we got both agents with no duplicates
+ all_ids = {first_page[0].id, second_page[0].id}
+ assert len(all_ids) == 2
+ assert agent1.id in all_ids
+ assert agent2.id in all_ids
+
+
+async def test_list_agents_query_text_pagination(server: SyncServer, default_user, default_organization):
+ """Test listing agents with query text filtering and pagination."""
+ # Create test agents with specific names and descriptions
+ agent1 = await server.agent_manager.create_agent_async(
+ agent_create=CreateAgent(
+ name="Search Agent One",
+ memory_blocks=[],
+ description="This is a search agent for testing",
+ llm_config=LLMConfig.default_config("gpt-4o-mini"),
+ embedding_config=EmbeddingConfig.default_config(provider="openai"),
+ include_base_tools=False,
+ ),
+ actor=default_user,
+ )
+
+ # at least 1 second to force unique timestamps in sqlite for deterministic pagination assertions
+ await asyncio.sleep(1.1)
+
+ agent2 = await server.agent_manager.create_agent_async(
+ agent_create=CreateAgent(
+ name="Search Agent Two",
+ memory_blocks=[],
+ description="Another search agent for testing",
+ llm_config=LLMConfig.default_config("gpt-4o-mini"),
+ embedding_config=EmbeddingConfig.default_config(provider="openai"),
+ include_base_tools=False,
+ ),
+ actor=default_user,
+ )
+
+ # at least 1 second to force unique timestamps in sqlite for deterministic pagination assertions
+ await asyncio.sleep(1.1)
+
+ agent3 = await server.agent_manager.create_agent_async(
+ agent_create=CreateAgent(
+ name="Different Agent",
+ memory_blocks=[],
+ description="This is a different agent",
+ llm_config=LLMConfig.default_config("gpt-4o-mini"),
+ embedding_config=EmbeddingConfig.default_config(provider="openai"),
+ include_base_tools=False,
+ ),
+ actor=default_user,
+ )
+
+ # Test query text filtering
+ search_results = await server.agent_manager.list_agents_async(actor=default_user, query_text="search agent")
+ assert len(search_results) == 2
+ search_agent_ids = {agent.id for agent in search_results}
+ assert agent1.id in search_agent_ids
+ assert agent2.id in search_agent_ids
+ assert agent3.id not in search_agent_ids
+
+ different_results = await server.agent_manager.list_agents_async(actor=default_user, query_text="different agent")
+ assert len(different_results) == 1
+ assert different_results[0].id == agent3.id
+
+ # Test pagination with query text
+ first_page = await server.agent_manager.list_agents_async(actor=default_user, query_text="search agent", limit=1)
+ assert len(first_page) == 1
+ first_agent_id = first_page[0].id
+
+ # Get second page using cursor
+ second_page = await server.agent_manager.list_agents_async(actor=default_user, query_text="search agent", after=first_agent_id, limit=1)
+ assert len(second_page) == 1
+ assert second_page[0].id != first_agent_id
+
+ # Test before and after
+ all_agents = await server.agent_manager.list_agents_async(actor=default_user, query_text="agent")
+ assert len(all_agents) == 3
+ first_agent, second_agent, third_agent = all_agents
+ middle_agent = await server.agent_manager.list_agents_async(
+ actor=default_user, query_text="search agent", before=third_agent.id, after=first_agent.id
+ )
+ assert len(middle_agent) == 1
+ assert middle_agent[0].id == second_agent.id
+
+ # Verify we got both search agents with no duplicates
+ all_ids = {first_page[0].id, second_page[0].id}
+ assert len(all_ids) == 2
+ assert all_ids == {agent1.id, agent2.id}
+
+
+async def test_list_tags(server: SyncServer, default_user, default_organization):
+ """Test listing tags functionality."""
+ # Create multiple agents with different tags
+ agents = []
+ tags = ["alpha", "beta", "gamma", "delta", "epsilon"]
+
+ # Create agents with different combinations of tags
+ for i in range(3):
+ agent = await server.agent_manager.create_agent_async(
+ actor=default_user,
+ agent_create=CreateAgent(
+ name="tag_agent_" + str(i),
+ memory_blocks=[],
+ llm_config=LLMConfig.default_config("gpt-4o-mini"),
+ embedding_config=EmbeddingConfig.default_config(provider="openai"),
+ tags=tags[i : i + 3], # Each agent gets 3 consecutive tags
+ include_base_tools=False,
+ ),
+ )
+ agents.append(agent)
+
+ # Test basic listing - should return all unique tags in alphabetical order
+ all_tags = await server.agent_manager.list_tags_async(actor=default_user)
+ assert all_tags == sorted(tags[:5]) # All tags should be present and sorted
+
+ # Test pagination with limit
+ limited_tags = await server.agent_manager.list_tags_async(actor=default_user, limit=2)
+ assert limited_tags == tags[:2] # Should return first 2 tags
+
+ # Test pagination with cursor
+ cursor_tags = await server.agent_manager.list_tags_async(actor=default_user, after="beta")
+ assert cursor_tags == ["delta", "epsilon", "gamma"] # Tags after "beta"
+
+ # Test text search
+ search_tags = await server.agent_manager.list_tags_async(actor=default_user, query_text="ta")
+ assert search_tags == ["beta", "delta"] # Only tags containing "ta"
+
+ # Test with non-matching search
+ no_match_tags = await server.agent_manager.list_tags_async(actor=default_user, query_text="xyz")
+ assert no_match_tags == [] # Should return empty list
+
+ # Test with different organization
+ other_org = await server.organization_manager.create_organization_async(pydantic_org=PydanticOrganization(name="Other Org"))
+ other_user = await server.user_manager.create_actor_async(PydanticUser(name="Other User", organization_id=other_org.id))
+
+ # Other org's tags should be empty
+ other_org_tags = await server.agent_manager.list_tags_async(actor=other_user)
+ assert other_org_tags == []
+
+ # Cleanup
+ for agent in agents:
+ await server.agent_manager.delete_agent_async(agent.id, actor=default_user)
diff --git a/tests/managers/test_archive_manager.py b/tests/managers/test_archive_manager.py
new file mode 100644
index 000000000..19707f890
--- /dev/null
+++ b/tests/managers/test_archive_manager.py
@@ -0,0 +1,272 @@
+import json
+import logging
+import os
+import random
+import re
+import string
+import time
+import uuid
+from datetime import datetime, timedelta, timezone
+from typing import List
+from unittest.mock import AsyncMock, Mock, patch
+
+import pytest
+from _pytest.python_api import approx
+from anthropic.types.beta import BetaMessage
+from anthropic.types.beta.messages import BetaMessageBatchIndividualResponse, BetaMessageBatchSucceededResult
+
+# Import shared fixtures and constants from conftest
+from conftest import (
+ CREATE_DELAY_SQLITE,
+ DEFAULT_EMBEDDING_CONFIG,
+ USING_SQLITE,
+)
+from openai.types.chat.chat_completion_message_tool_call import ChatCompletionMessageToolCall as OpenAIToolCall, Function as OpenAIFunction
+from sqlalchemy import func, select
+from sqlalchemy.exc import IntegrityError, InvalidRequestError
+from sqlalchemy.orm.exc import StaleDataError
+
+from letta.config import LettaConfig
+from letta.constants import (
+ BASE_MEMORY_TOOLS,
+ BASE_SLEEPTIME_TOOLS,
+ BASE_TOOLS,
+ BASE_VOICE_SLEEPTIME_CHAT_TOOLS,
+ BASE_VOICE_SLEEPTIME_TOOLS,
+ BUILTIN_TOOLS,
+ DEFAULT_ORG_ID,
+ DEFAULT_ORG_NAME,
+ FILES_TOOLS,
+ LETTA_TOOL_EXECUTION_DIR,
+ LETTA_TOOL_SET,
+ LOCAL_ONLY_MULTI_AGENT_TOOLS,
+ MCP_TOOL_TAG_NAME_PREFIX,
+ MULTI_AGENT_TOOLS,
+)
+from letta.data_sources.redis_client import NoopAsyncRedisClient, get_redis_client
+from letta.errors import LettaAgentNotFoundError
+from letta.functions.functions import derive_openai_json_schema, parse_source_code
+from letta.functions.mcp_client.types import MCPTool
+from letta.helpers import ToolRulesSolver
+from letta.helpers.datetime_helpers import AsyncTimer
+from letta.jobs.types import ItemUpdateInfo, RequestStatusUpdateInfo, StepStatusUpdateInfo
+from letta.orm import Base, Block
+from letta.orm.block_history import BlockHistory
+from letta.orm.errors import NoResultFound, UniqueConstraintViolationError
+from letta.orm.file import FileContent as FileContentModel, FileMetadata as FileMetadataModel
+from letta.schemas.agent import CreateAgent, UpdateAgent
+from letta.schemas.block import Block as PydanticBlock, BlockUpdate, CreateBlock
+from letta.schemas.embedding_config import EmbeddingConfig
+from letta.schemas.enums import (
+ ActorType,
+ AgentStepStatus,
+ FileProcessingStatus,
+ JobStatus,
+ JobType,
+ MessageRole,
+ ProviderType,
+ SandboxType,
+ StepStatus,
+ TagMatchMode,
+ ToolType,
+ VectorDBProvider,
+)
+from letta.schemas.environment_variables import SandboxEnvironmentVariableCreate, SandboxEnvironmentVariableUpdate
+from letta.schemas.file import FileMetadata, FileMetadata as PydanticFileMetadata
+from letta.schemas.identity import IdentityCreate, IdentityProperty, IdentityPropertyType, IdentityType, IdentityUpdate, IdentityUpsert
+from letta.schemas.job import BatchJob, Job, Job as PydanticJob, JobUpdate, LettaRequestConfig
+from letta.schemas.letta_message import UpdateAssistantMessage, UpdateReasoningMessage, UpdateSystemMessage, UpdateUserMessage
+from letta.schemas.letta_message_content import TextContent
+from letta.schemas.letta_stop_reason import LettaStopReason, StopReasonType
+from letta.schemas.llm_batch_job import AgentStepState, LLMBatchItem
+from letta.schemas.llm_config import LLMConfig
+from letta.schemas.message import Message as PydanticMessage, MessageCreate, MessageUpdate
+from letta.schemas.openai.chat_completion_response import UsageStatistics
+from letta.schemas.organization import Organization, Organization as PydanticOrganization, OrganizationUpdate
+from letta.schemas.passage import Passage as PydanticPassage
+from letta.schemas.pip_requirement import PipRequirement
+from letta.schemas.run import Run as PydanticRun
+from letta.schemas.sandbox_config import E2BSandboxConfig, LocalSandboxConfig, SandboxConfigCreate, SandboxConfigUpdate
+from letta.schemas.source import Source as PydanticSource, SourceUpdate
+from letta.schemas.tool import Tool as PydanticTool, ToolCreate, ToolUpdate
+from letta.schemas.tool_rule import InitToolRule
+from letta.schemas.user import User as PydanticUser, UserUpdate
+from letta.server.db import db_registry
+from letta.server.server import SyncServer
+from letta.services.block_manager import BlockManager
+from letta.services.helpers.agent_manager_helper import calculate_base_tools, calculate_multi_agent_tools, validate_agent_exists_async
+from letta.services.step_manager import FeedbackType
+from letta.settings import settings, tool_settings
+from letta.utils import calculate_file_defaults_based_on_context_window
+from tests.helpers.utils import comprehensive_agent_checks, validate_context_window_overview
+from tests.utils import random_string
+
+
+# ======================================================================================================================
+# Archive Manager Tests
+# ======================================================================================================================
+async def test_archive_manager_delete_archive_async(server: SyncServer, default_user):
+ """Test the delete_archive_async function."""
+ archive = await server.archive_manager.create_archive_async(
+ name="test_archive_to_delete", description="This archive will be deleted", actor=default_user
+ )
+
+ retrieved_archive = await server.archive_manager.get_archive_by_id_async(archive_id=archive.id, actor=default_user)
+ assert retrieved_archive.id == archive.id
+
+ await server.archive_manager.delete_archive_async(archive_id=archive.id, actor=default_user)
+
+ with pytest.raises(Exception):
+ await server.archive_manager.get_archive_by_id_async(archive_id=archive.id, actor=default_user)
+
+
+async def test_archive_manager_get_agents_for_archive_async(server: SyncServer, default_user, sarah_agent):
+ """Test getting all agents that have access to an archive."""
+ archive = await server.archive_manager.create_archive_async(
+ name="shared_archive", description="Archive shared by multiple agents", actor=default_user
+ )
+
+ agent2 = await server.agent_manager.create_agent_async(
+ agent_create=CreateAgent(
+ name="test_agent_2",
+ memory_blocks=[],
+ llm_config=LLMConfig.default_config("gpt-4o-mini"),
+ embedding_config=EmbeddingConfig.default_config(provider="openai"),
+ include_base_tools=False,
+ ),
+ actor=default_user,
+ )
+
+ await server.archive_manager.attach_agent_to_archive_async(
+ agent_id=sarah_agent.id, archive_id=archive.id, is_owner=True, actor=default_user
+ )
+
+ await server.archive_manager.attach_agent_to_archive_async(
+ agent_id=agent2.id, archive_id=archive.id, is_owner=False, actor=default_user
+ )
+
+ agent_ids = await server.archive_manager.get_agents_for_archive_async(archive_id=archive.id, actor=default_user)
+
+ assert len(agent_ids) == 2
+ assert sarah_agent.id in agent_ids
+ assert agent2.id in agent_ids
+
+ # Cleanup
+ await server.agent_manager.delete_agent_async(agent2.id, actor=default_user)
+ await server.archive_manager.delete_archive_async(archive.id, actor=default_user)
+
+
+async def test_archive_manager_race_condition_handling(server: SyncServer, default_user, sarah_agent):
+ """Test that the race condition fix in get_or_create_default_archive_for_agent_async works."""
+ from unittest.mock import patch
+
+ from sqlalchemy.exc import IntegrityError
+
+ agent = await server.agent_manager.create_agent_async(
+ agent_create=CreateAgent(
+ name="test_agent_race_condition",
+ memory_blocks=[],
+ llm_config=LLMConfig.default_config("gpt-4o-mini"),
+ embedding_config=EmbeddingConfig.default_config(provider="openai"),
+ include_base_tools=False,
+ ),
+ actor=default_user,
+ )
+
+ created_archives = []
+ original_create = server.archive_manager.create_archive_async
+
+ async def track_create(*args, **kwargs):
+ result = await original_create(*args, **kwargs)
+ created_archives.append(result)
+ return result
+
+ # First, create an archive that will be attached by a "concurrent" request
+ concurrent_archive = await server.archive_manager.create_archive_async(
+ name=f"{agent.name}'s Archive", description="Default archive created automatically", actor=default_user
+ )
+
+ call_count = 0
+ original_attach = server.archive_manager.attach_agent_to_archive_async
+
+ async def failing_attach(*args, **kwargs):
+ nonlocal call_count
+ call_count += 1
+ if call_count == 1:
+ # Simulate another request already attached the agent to an archive
+ await original_attach(agent_id=agent.id, archive_id=concurrent_archive.id, is_owner=True, actor=default_user)
+ # Now raise the IntegrityError as if our attempt failed
+ raise IntegrityError("duplicate key value violates unique constraint", None, None)
+ # This shouldn't be called since we already have an archive
+ raise Exception("Should not reach here")
+
+ with patch.object(server.archive_manager, "create_archive_async", side_effect=track_create):
+ with patch.object(server.archive_manager, "attach_agent_to_archive_async", side_effect=failing_attach):
+ archive = await server.archive_manager.get_or_create_default_archive_for_agent_async(
+ agent_id=agent.id, agent_name=agent.name, actor=default_user
+ )
+
+ assert archive is not None
+ assert archive.id == concurrent_archive.id # Should return the existing archive
+ assert archive.name == f"{agent.name}'s Archive"
+
+ # One archive was created in our attempt (but then deleted)
+ assert len(created_archives) == 1
+
+ # Verify only one archive is attached to the agent
+ archive_ids = await server.agent_manager.get_agent_archive_ids_async(agent_id=agent.id, actor=default_user)
+ assert len(archive_ids) == 1
+ assert archive_ids[0] == concurrent_archive.id
+
+ # Cleanup
+ await server.agent_manager.delete_agent_async(agent.id, actor=default_user)
+ await server.archive_manager.delete_archive_async(concurrent_archive.id, actor=default_user)
+
+
+async def test_archive_manager_get_agent_from_passage_async(server: SyncServer, default_user, sarah_agent):
+ """Test getting the agent ID that owns a passage through its archive."""
+ archive = await server.archive_manager.get_or_create_default_archive_for_agent_async(
+ agent_id=sarah_agent.id, agent_name=sarah_agent.name, actor=default_user
+ )
+
+ passage = await server.passage_manager.create_agent_passage_async(
+ PydanticPassage(
+ text="Test passage for agent ownership",
+ archive_id=archive.id,
+ organization_id=default_user.organization_id,
+ embedding=[0.1],
+ embedding_config=DEFAULT_EMBEDDING_CONFIG,
+ ),
+ actor=default_user,
+ )
+
+ agent_id = await server.archive_manager.get_agent_from_passage_async(passage_id=passage.id, actor=default_user)
+
+ assert agent_id == sarah_agent.id
+
+ orphan_archive = await server.archive_manager.create_archive_async(
+ name="orphan_archive", description="Archive with no agents", actor=default_user
+ )
+
+ orphan_passage = await server.passage_manager.create_agent_passage_async(
+ PydanticPassage(
+ text="Orphan passage",
+ archive_id=orphan_archive.id,
+ organization_id=default_user.organization_id,
+ embedding=[0.1],
+ embedding_config=DEFAULT_EMBEDDING_CONFIG,
+ ),
+ actor=default_user,
+ )
+
+ agent_id = await server.archive_manager.get_agent_from_passage_async(passage_id=orphan_passage.id, actor=default_user)
+ assert agent_id is None
+
+ # Cleanup
+ await server.passage_manager.delete_passage_by_id_async(passage.id, actor=default_user)
+ await server.passage_manager.delete_passage_by_id_async(orphan_passage.id, actor=default_user)
+ await server.archive_manager.delete_archive_async(orphan_archive.id, actor=default_user)
diff --git a/tests/managers/test_block_manager.py b/tests/managers/test_block_manager.py
new file mode 100644
index 000000000..3d5aa801d
--- /dev/null
+++ b/tests/managers/test_block_manager.py
@@ -0,0 +1,1270 @@
+import json
+import logging
+import os
+import random
+import re
+import string
+import time
+import uuid
+from datetime import datetime, timedelta, timezone
+from typing import List
+from unittest.mock import AsyncMock, Mock, patch
+
+import pytest
+from _pytest.python_api import approx
+from anthropic.types.beta import BetaMessage
+from anthropic.types.beta.messages import BetaMessageBatchIndividualResponse, BetaMessageBatchSucceededResult
+
+# Import shared fixtures and constants from conftest
+from conftest import (
+ CREATE_DELAY_SQLITE,
+ DEFAULT_EMBEDDING_CONFIG,
+ USING_SQLITE,
+)
+from openai.types.chat.chat_completion_message_tool_call import ChatCompletionMessageToolCall as OpenAIToolCall, Function as OpenAIFunction
+from sqlalchemy import func, select
+from sqlalchemy.exc import IntegrityError, InvalidRequestError
+from sqlalchemy.orm.exc import StaleDataError
+
+from letta.config import LettaConfig
+from letta.constants import (
+ BASE_MEMORY_TOOLS,
+ BASE_SLEEPTIME_TOOLS,
+ BASE_TOOLS,
+ BASE_VOICE_SLEEPTIME_CHAT_TOOLS,
+ BASE_VOICE_SLEEPTIME_TOOLS,
+ BUILTIN_TOOLS,
+ DEFAULT_ORG_ID,
+ DEFAULT_ORG_NAME,
+ FILES_TOOLS,
+ LETTA_TOOL_EXECUTION_DIR,
+ LETTA_TOOL_SET,
+ LOCAL_ONLY_MULTI_AGENT_TOOLS,
+ MCP_TOOL_TAG_NAME_PREFIX,
+ MULTI_AGENT_TOOLS,
+)
+from letta.data_sources.redis_client import NoopAsyncRedisClient, get_redis_client
+from letta.errors import LettaAgentNotFoundError
+from letta.functions.functions import derive_openai_json_schema, parse_source_code
+from letta.functions.mcp_client.types import MCPTool
+from letta.helpers import ToolRulesSolver
+from letta.helpers.datetime_helpers import AsyncTimer
+from letta.jobs.types import ItemUpdateInfo, RequestStatusUpdateInfo, StepStatusUpdateInfo
+from letta.orm import Base, Block
+from letta.orm.block_history import BlockHistory
+from letta.orm.errors import NoResultFound, UniqueConstraintViolationError
+from letta.orm.file import FileContent as FileContentModel, FileMetadata as FileMetadataModel
+from letta.schemas.agent import CreateAgent, UpdateAgent
+from letta.schemas.block import Block as PydanticBlock, BlockUpdate, CreateBlock
+from letta.schemas.embedding_config import EmbeddingConfig
+from letta.schemas.enums import (
+ ActorType,
+ AgentStepStatus,
+ FileProcessingStatus,
+ JobStatus,
+ JobType,
+ MessageRole,
+ ProviderType,
+ SandboxType,
+ StepStatus,
+ TagMatchMode,
+ ToolType,
+ VectorDBProvider,
+)
+from letta.schemas.environment_variables import SandboxEnvironmentVariableCreate, SandboxEnvironmentVariableUpdate
+from letta.schemas.file import FileMetadata, FileMetadata as PydanticFileMetadata
+from letta.schemas.identity import IdentityCreate, IdentityProperty, IdentityPropertyType, IdentityType, IdentityUpdate, IdentityUpsert
+from letta.schemas.job import BatchJob, Job, Job as PydanticJob, JobUpdate, LettaRequestConfig
+from letta.schemas.letta_message import UpdateAssistantMessage, UpdateReasoningMessage, UpdateSystemMessage, UpdateUserMessage
+from letta.schemas.letta_message_content import TextContent
+from letta.schemas.letta_stop_reason import LettaStopReason, StopReasonType
+from letta.schemas.llm_batch_job import AgentStepState, LLMBatchItem
+from letta.schemas.llm_config import LLMConfig
+from letta.schemas.message import Message as PydanticMessage, MessageCreate, MessageUpdate
+from letta.schemas.openai.chat_completion_response import UsageStatistics
+from letta.schemas.organization import Organization, Organization as PydanticOrganization, OrganizationUpdate
+from letta.schemas.passage import Passage as PydanticPassage
+from letta.schemas.pip_requirement import PipRequirement
+from letta.schemas.run import Run as PydanticRun
+from letta.schemas.sandbox_config import E2BSandboxConfig, LocalSandboxConfig, SandboxConfigCreate, SandboxConfigUpdate
+from letta.schemas.source import Source as PydanticSource, SourceUpdate
+from letta.schemas.tool import Tool as PydanticTool, ToolCreate, ToolUpdate
+from letta.schemas.tool_rule import InitToolRule
+from letta.schemas.user import User as PydanticUser, UserUpdate
+from letta.server.db import db_registry
+from letta.server.server import SyncServer
+from letta.services.block_manager import BlockManager
+from letta.services.helpers.agent_manager_helper import calculate_base_tools, calculate_multi_agent_tools, validate_agent_exists_async
+from letta.services.step_manager import FeedbackType
+from letta.settings import settings, tool_settings
+from letta.utils import calculate_file_defaults_based_on_context_window
+from tests.helpers.utils import comprehensive_agent_checks, validate_context_window_overview
+from tests.utils import random_string
+
+# ======================================================================================================================
+# AgentManager Tests - Blocks Relationship
+# ======================================================================================================================
+
+
+async def test_attach_block(server: SyncServer, sarah_agent, default_block, default_user):
+ """Test attaching a block to an agent."""
+ # Attach block
+ await server.agent_manager.attach_block_async(agent_id=sarah_agent.id, block_id=default_block.id, actor=default_user)
+
+ # Verify attachment
+ agent = await server.agent_manager.get_agent_by_id_async(sarah_agent.id, actor=default_user)
+ assert len(agent.memory.blocks) == 1
+ assert agent.memory.blocks[0].id == default_block.id
+ assert agent.memory.blocks[0].label == default_block.label
+
+
+# Test should work with both SQLite and PostgreSQL
+async def test_attach_block_duplicate_label(server: SyncServer, sarah_agent, default_block, other_block, default_user):
+ """Test attempting to attach a block with a duplicate label."""
+ # Set up both blocks with same label
+ await server.block_manager.update_block_async(default_block.id, BlockUpdate(label="same_label"), actor=default_user)
+ await server.block_manager.update_block_async(other_block.id, BlockUpdate(label="same_label"), actor=default_user)
+
+ # Attach first block
+ await server.agent_manager.attach_block_async(agent_id=sarah_agent.id, block_id=default_block.id, actor=default_user)
+
+ # Attempt to attach second block with same label
+ with pytest.raises(UniqueConstraintViolationError):
+ await server.agent_manager.attach_block_async(agent_id=sarah_agent.id, block_id=other_block.id, actor=default_user)
+
+
+async def test_detach_block(server: SyncServer, sarah_agent, default_block, default_user):
+ """Test detaching a block by ID."""
+ # Set up: attach block
+ await server.agent_manager.attach_block_async(agent_id=sarah_agent.id, block_id=default_block.id, actor=default_user)
+
+ # Detach block
+ await server.agent_manager.detach_block_async(agent_id=sarah_agent.id, block_id=default_block.id, actor=default_user)
+
+ # Verify detachment
+ agent = await server.agent_manager.get_agent_by_id_async(sarah_agent.id, actor=default_user)
+ assert len(agent.memory.blocks) == 0
+
+ # Check that block still exists
+ block = await server.block_manager.get_block_by_id_async(block_id=default_block.id, actor=default_user)
+ assert block
+
+
+async def test_detach_nonexistent_block(server: SyncServer, sarah_agent, default_user):
+ """Test detaching a block that isn't attached."""
+ with pytest.raises(NoResultFound):
+ await server.agent_manager.detach_block_async(agent_id=sarah_agent.id, block_id="nonexistent-block-id", actor=default_user)
+
+
+async def test_update_block_label(server: SyncServer, sarah_agent, default_block, default_user):
+ """Test updating a block's label updates the relationship."""
+ # Attach block
+ await server.agent_manager.attach_block_async(agent_id=sarah_agent.id, block_id=default_block.id, actor=default_user)
+
+ # Update block label
+ new_label = "new_label"
+ await server.block_manager.update_block_async(default_block.id, BlockUpdate(label=new_label), actor=default_user)
+
+ # Verify relationship is updated
+ agent = await server.agent_manager.get_agent_by_id_async(sarah_agent.id, actor=default_user)
+ block = agent.memory.blocks[0]
+ assert block.id == default_block.id
+ assert block.label == new_label
+
+
+async def test_update_block_label_multiple_agents(server: SyncServer, sarah_agent, charles_agent, default_block, default_user):
+ """Test updating a block's label updates relationships for all agents."""
+ # Attach block to both agents
+ await server.agent_manager.attach_block_async(agent_id=sarah_agent.id, block_id=default_block.id, actor=default_user)
+ await server.agent_manager.attach_block_async(agent_id=charles_agent.id, block_id=default_block.id, actor=default_user)
+
+ # Update block label
+ new_label = "new_label"
+ await server.block_manager.update_block_async(default_block.id, BlockUpdate(label=new_label), actor=default_user)
+
+ # Verify both relationships are updated
+ for agent_id in [sarah_agent.id, charles_agent.id]:
+ agent = await server.agent_manager.get_agent_by_id_async(agent_id, actor=default_user)
+ # Find our specific block by ID
+ block = next(b for b in agent.memory.blocks if b.id == default_block.id)
+ assert block.label == new_label
+
+
+async def test_get_block_with_label(server: SyncServer, sarah_agent, default_block, default_user):
+ """Test retrieving a block by its label."""
+ # Attach block
+ await server.agent_manager.attach_block_async(agent_id=sarah_agent.id, block_id=default_block.id, actor=default_user)
+
+ # Get block by label
+ block = await server.agent_manager.get_block_with_label_async(
+ agent_id=sarah_agent.id, block_label=default_block.label, actor=default_user
+ )
+
+ assert block.id == default_block.id
+ assert block.label == default_block.label
+
+
+async def test_refresh_memory_async(server: SyncServer, default_user):
+ block = await server.block_manager.create_or_update_block_async(
+ PydanticBlock(
+ label="test",
+ value="test",
+ limit=1000,
+ ),
+ actor=default_user,
+ )
+ block_human = await server.block_manager.create_or_update_block_async(
+ PydanticBlock(
+ label="human",
+ value="name: caren",
+ limit=1000,
+ ),
+ actor=default_user,
+ )
+ agent = await server.agent_manager.create_agent_async(
+ CreateAgent(
+ name="test",
+ llm_config=LLMConfig.default_config("gpt-4o-mini"),
+ embedding_config=EmbeddingConfig.default_config(provider="openai"),
+ include_base_tools=False,
+ block_ids=[block.id, block_human.id],
+ ),
+ actor=default_user,
+ )
+ block = await server.block_manager.update_block_async(
+ block_id=block.id,
+ block_update=BlockUpdate(
+ value="test2",
+ ),
+ actor=default_user,
+ )
+ assert len(agent.memory.blocks) == 2
+ agent = await server.agent_manager.refresh_memory_async(agent_state=agent, actor=default_user)
+ assert len(agent.memory.blocks) == 2
+ assert any([block.value == "test2" for block in agent.memory.blocks])
+
+
+# ======================================================================================================================
+# Block Manager Tests - Basic
+# ======================================================================================================================
+
+
+async def test_create_block(server: SyncServer, default_user):
+ block_manager = BlockManager()
+ block_create = PydanticBlock(
+ label="human",
+ is_template=True,
+ value="Sample content",
+ template_name="sample_template_name",
+ template_id="sample_template",
+ description="A test block",
+ limit=1000,
+ metadata={"example": "data"},
+ )
+
+ block = await block_manager.create_or_update_block_async(block_create, actor=default_user)
+
+ # Assertions to ensure the created block matches the expected values
+ assert block.label == block_create.label
+ assert block.is_template == block_create.is_template
+ assert block.value == block_create.value
+ assert block.template_name == block_create.template_name
+ assert block.template_id == block_create.template_id
+ assert block.description == block_create.description
+ assert block.limit == block_create.limit
+ assert block.metadata == block_create.metadata
+
+
+async def test_batch_create_blocks_async(server: SyncServer, default_user):
+ """Test batch creating multiple blocks at once"""
+ block_manager = BlockManager()
+
+ # create multiple test blocks
+ blocks_data = []
+ for i in range(5):
+ block = PydanticBlock(
+ label=f"test_block_{i}",
+ is_template=False,
+ value=f"Content for block {i}",
+ description=f"Test block {i} for batch operations",
+ limit=1000 + i * 100, # varying limits
+ metadata={"index": i, "batch": "test"},
+ )
+ blocks_data.append(block)
+
+ # batch create all blocks at once
+ created_blocks = await block_manager.batch_create_blocks_async(blocks_data, default_user)
+
+ # verify all blocks were created
+ assert len(created_blocks) == 5
+ assert all(b.label.startswith("test_block_") for b in created_blocks)
+
+ # verify block properties were preserved
+ for i, block in enumerate(created_blocks):
+ assert block.label == f"test_block_{i}"
+ assert block.value == f"Content for block {i}"
+ assert block.description == f"Test block {i} for batch operations"
+ assert block.limit == 1000 + i * 100
+ assert block.metadata["index"] == i
+ assert block.metadata["batch"] == "test"
+ assert block.id is not None # should have generated ids
+ # blocks have organization_id at the orm level, not in the pydantic model
+
+ # verify blocks can be retrieved individually
+ for created_block in created_blocks:
+ retrieved = await block_manager.get_block_by_id_async(created_block.id, default_user)
+ assert retrieved.id == created_block.id
+ assert retrieved.label == created_block.label
+ assert retrieved.value == created_block.value
+
+ # test with empty list
+ empty_result = await block_manager.batch_create_blocks_async([], default_user)
+ assert empty_result == []
+
+ # test creating blocks with same labels (should create separate blocks since no unique constraint)
+ duplicate_blocks = [
+ PydanticBlock(label="duplicate_label", value="Block 1"),
+ PydanticBlock(label="duplicate_label", value="Block 2"),
+ PydanticBlock(label="duplicate_label", value="Block 3"),
+ ]
+
+ created_duplicates = await block_manager.batch_create_blocks_async(duplicate_blocks, default_user)
+ assert len(created_duplicates) == 3
+ assert all(b.label == "duplicate_label" for b in created_duplicates)
+ # all should have different ids
+ ids = [b.id for b in created_duplicates]
+ assert len(set(ids)) == 3 # all unique ids
+ # but different values
+ values = [b.value for b in created_duplicates]
+ assert set(values) == {"Block 1", "Block 2", "Block 3"}
+
+
+async def test_get_blocks(server, default_user):
+ block_manager = BlockManager()
+
+ # Create blocks to retrieve later
+ await block_manager.create_or_update_block_async(PydanticBlock(label="human", value="Block 1"), actor=default_user)
+ await block_manager.create_or_update_block_async(PydanticBlock(label="persona", value="Block 2"), actor=default_user)
+
+ # Retrieve blocks by different filters
+ all_blocks = await block_manager.get_blocks_async(actor=default_user)
+ assert len(all_blocks) == 2
+
+ human_blocks = await block_manager.get_blocks_async(actor=default_user, label="human")
+ assert len(human_blocks) == 1
+ assert human_blocks[0].label == "human"
+
+ persona_blocks = await block_manager.get_blocks_async(actor=default_user, label="persona")
+ assert len(persona_blocks) == 1
+ assert persona_blocks[0].label == "persona"
+
+
+async def test_get_blocks_comprehensive(server, default_user, other_user_different_org):
+ def random_label(prefix="label"):
+ return f"{prefix}_{''.join(random.choices(string.ascii_lowercase, k=6))}"
+
+ def random_value():
+ return "".join(random.choices(string.ascii_letters + string.digits, k=12))
+
+ block_manager = BlockManager()
+
+ # Create 10 blocks for default_user
+ default_user_blocks = []
+ for _ in range(10):
+ label = random_label("default")
+ value = random_value()
+ await block_manager.create_or_update_block_async(PydanticBlock(label=label, value=value), actor=default_user)
+ default_user_blocks.append((label, value))
+
+ # Create 3 blocks for other_user
+ other_user_blocks = []
+ for _ in range(3):
+ label = random_label("other")
+ value = random_value()
+ await block_manager.create_or_update_block_async(PydanticBlock(label=label, value=value), actor=other_user_different_org)
+ other_user_blocks.append((label, value))
+
+ # Check default_user sees only their blocks
+ retrieved_default_blocks = await block_manager.get_blocks_async(actor=default_user)
+ assert len(retrieved_default_blocks) == 10
+ retrieved_labels = {b.label for b in retrieved_default_blocks}
+ for label, value in default_user_blocks:
+ assert label in retrieved_labels
+
+ # Check individual filtering for default_user
+ for label, value in default_user_blocks:
+ filtered = await block_manager.get_blocks_async(actor=default_user, label=label)
+ assert len(filtered) == 1
+ assert filtered[0].label == label
+ assert filtered[0].value == value
+
+ # Check other_user sees only their blocks
+ retrieved_other_blocks = await block_manager.get_blocks_async(actor=other_user_different_org)
+ assert len(retrieved_other_blocks) == 3
+ retrieved_labels = {b.label for b in retrieved_other_blocks}
+ for label, value in other_user_blocks:
+ assert label in retrieved_labels
+
+ # Other user shouldn't see default_user's blocks
+ for label, _ in default_user_blocks:
+ assert (await block_manager.get_blocks_async(actor=other_user_different_org, label=label)) == []
+
+ # Default user shouldn't see other_user's blocks
+ for label, _ in other_user_blocks:
+ assert (await block_manager.get_blocks_async(actor=default_user, label=label)) == []
+
+
+async def test_update_block(server: SyncServer, default_user):
+ block_manager = BlockManager()
+ block = await block_manager.create_or_update_block_async(PydanticBlock(label="persona", value="Original Content"), actor=default_user)
+
+ # Update block's content
+ update_data = BlockUpdate(value="Updated Content", description="Updated description")
+ await block_manager.update_block_async(block_id=block.id, block_update=update_data, actor=default_user)
+
+ # Retrieve the updated block
+ updated_block = await block_manager.get_block_by_id_async(actor=default_user, block_id=block.id)
+
+ # Assertions to verify the update
+ assert updated_block.value == "Updated Content"
+ assert updated_block.description == "Updated description"
+
+
+async def test_update_block_limit(server: SyncServer, default_user):
+ block_manager = BlockManager()
+ block = await block_manager.create_or_update_block_async(PydanticBlock(label="persona", value="Original Content"), actor=default_user)
+
+ limit = len("Updated Content") * 2000
+ update_data = BlockUpdate(value="Updated Content" * 2000, description="Updated description")
+
+ # Check that exceeding the block limit raises an exception
+ with pytest.raises(ValueError):
+ await block_manager.update_block_async(block_id=block.id, block_update=update_data, actor=default_user)
+
+ # Ensure the update works when within limits
+ update_data = BlockUpdate(value="Updated Content" * 2000, description="Updated description", limit=limit)
+ await block_manager.update_block_async(block_id=block.id, block_update=update_data, actor=default_user)
+
+ # Retrieve the updated block and validate the update
+ updated_block = await block_manager.get_block_by_id_async(actor=default_user, block_id=block.id)
+
+ assert updated_block.value == "Updated Content" * 2000
+ assert updated_block.description == "Updated description"
+
+
+async def test_update_block_limit_does_not_reset(server: SyncServer, default_user):
+ block_manager = BlockManager()
+ new_content = "Updated Content" * 2000
+ limit = len(new_content)
+ block = await block_manager.create_or_update_block_async(
+ PydanticBlock(label="persona", value="Original Content", limit=limit), actor=default_user
+ )
+
+ # Ensure the update works
+ update_data = BlockUpdate(value=new_content)
+ await block_manager.update_block_async(block_id=block.id, block_update=update_data, actor=default_user)
+
+ # Retrieve the updated block and validate the update
+ updated_block = await block_manager.get_block_by_id_async(actor=default_user, block_id=block.id)
+ assert updated_block.value == new_content
+
+
+async def test_delete_block(server: SyncServer, default_user):
+ block_manager = BlockManager()
+
+ # Create and delete a block
+ block = await block_manager.create_or_update_block_async(PydanticBlock(label="human", value="Sample content"), actor=default_user)
+ await block_manager.delete_block_async(block_id=block.id, actor=default_user)
+
+ # Verify that the block was deleted
+ blocks = await block_manager.get_blocks_async(actor=default_user)
+ assert len(blocks) == 0
+
+
+async def test_delete_block_detaches_from_agent(server: SyncServer, sarah_agent, default_user):
+ # Create and delete a block
+ block = await server.block_manager.create_or_update_block_async(
+ PydanticBlock(label="human", value="Sample content"), actor=default_user
+ )
+ agent_state = await server.agent_manager.attach_block_async(agent_id=sarah_agent.id, block_id=block.id, actor=default_user)
+
+ # Check that block has been attached
+ assert block.id in [b.id for b in agent_state.memory.blocks]
+
+ # Now attempt to delete the block
+ await server.block_manager.delete_block_async(block_id=block.id, actor=default_user)
+
+ # Verify that the block was deleted
+ blocks = await server.block_manager.get_blocks_async(actor=default_user)
+ assert len(blocks) == 0
+
+ # Check that block has been detached too
+ agent_state = await server.agent_manager.get_agent_by_id_async(agent_id=sarah_agent.id, actor=default_user)
+ assert block.id not in [b.id for b in agent_state.memory.blocks]
+
+
+async def test_get_agents_for_block(server: SyncServer, sarah_agent, charles_agent, default_user):
+ # Create and delete a block
+ block = await server.block_manager.create_or_update_block_async(
+ PydanticBlock(label="alien", value="Sample content"), actor=default_user
+ )
+ sarah_agent = await server.agent_manager.attach_block_async(agent_id=sarah_agent.id, block_id=block.id, actor=default_user)
+ charles_agent = await server.agent_manager.attach_block_async(agent_id=charles_agent.id, block_id=block.id, actor=default_user)
+
+ # Check that block has been attached to both
+ assert block.id in [b.id for b in sarah_agent.memory.blocks]
+ assert block.id in [b.id for b in charles_agent.memory.blocks]
+
+ # Get the agents for that block
+ agent_states = await server.block_manager.get_agents_for_block_async(block_id=block.id, actor=default_user)
+ assert len(agent_states) == 2
+
+ # Check both agents are in the list
+ agent_state_ids = [a.id for a in agent_states]
+ assert sarah_agent.id in agent_state_ids
+ assert charles_agent.id in agent_state_ids
+
+
+async def test_batch_create_multiple_blocks(server: SyncServer, default_user):
+ block_manager = BlockManager()
+ num_blocks = 10
+
+ # Prepare distinct blocks
+ blocks_to_create = [PydanticBlock(label=f"batch_label_{i}", value=f"batch_value_{i}") for i in range(num_blocks)]
+
+ # Create the blocks
+ created_blocks = await block_manager.batch_create_blocks_async(blocks_to_create, actor=default_user)
+ assert len(created_blocks) == num_blocks
+
+ # Map created blocks by label for lookup
+ created_by_label = {blk.label: blk for blk in created_blocks}
+
+ # Assert all blocks were created correctly
+ for i in range(num_blocks):
+ label = f"batch_label_{i}"
+ value = f"batch_value_{i}"
+ assert label in created_by_label, f"Missing label: {label}"
+ blk = created_by_label[label]
+ assert blk.value == value
+ assert blk.id is not None
+
+ # Confirm all created blocks exist in the full list from get_blocks
+ all_labels = {blk.label for blk in await block_manager.get_blocks_async(actor=default_user)}
+ expected_labels = {f"batch_label_{i}" for i in range(num_blocks)}
+ assert expected_labels.issubset(all_labels)
+
+
+async def test_bulk_update_skips_missing_and_truncates_then_returns_none(server: SyncServer, default_user: PydanticUser, caplog):
+ mgr = BlockManager()
+
+ # create one block with a small limit
+ b = await mgr.create_or_update_block_async(
+ PydanticBlock(label="human", value="orig", limit=5),
+ actor=default_user,
+ )
+
+ # prepare updates: one real id with an over‐limit value, plus one missing id
+ long_val = random_string(10) # length > limit==5
+ updates = {
+ b.id: long_val,
+ "nonexistent-id": "whatever",
+ }
+
+ caplog.set_level(logging.WARNING)
+ result = await mgr.bulk_update_block_values_async(updates, actor=default_user)
+ # default return_hydrated=False → should be None
+ assert result is None
+
+ # warnings should mention skipping the missing ID and truncation
+ assert "skipping during bulk update" in caplog.text
+ assert "truncating" in caplog.text
+
+ # confirm the value was truncated to `limit` characters
+ reloaded = await mgr.get_block_by_id_async(actor=default_user, block_id=b.id)
+ assert len(reloaded.value) == 5
+ assert reloaded.value == long_val[:5]
+
+
[email protected](reason="TODO: implement for async")
+async def test_bulk_update_return_hydrated_true(server: SyncServer, default_user: PydanticUser):
+ mgr = BlockManager()
+
+ # create a block
+ b = await mgr.create_or_update_block_async(
+ PydanticBlock(label="persona", value="foo", limit=20),
+ actor=default_user,
+ )
+
+ updates = {b.id: "new-val"}
+ updated = await mgr.bulk_update_block_values_async(updates, actor=default_user, return_hydrated=True)
+
+ # with return_hydrated=True, we get back a list of schemas
+ assert isinstance(updated, list) and len(updated) == 1
+ assert updated[0].id == b.id
+ assert updated[0].value == "new-val"
+
+
+async def test_bulk_update_respects_org_scoping(
+ server: SyncServer, default_user: PydanticUser, other_user_different_org: PydanticUser, caplog
+):
+ mgr = BlockManager()
+
+ # one block in each org
+ mine = await mgr.create_or_update_block_async(
+ PydanticBlock(label="human", value="mine", limit=100),
+ actor=default_user,
+ )
+ theirs = await mgr.create_or_update_block_async(
+ PydanticBlock(label="human", value="theirs", limit=100),
+ actor=other_user_different_org,
+ )
+
+ updates = {
+ mine.id: "updated-mine",
+ theirs.id: "updated-theirs",
+ }
+
+ caplog.set_level(logging.WARNING)
+ await mgr.bulk_update_block_values_async(updates, actor=default_user)
+
+ # mine should be updated...
+ reloaded_mine = await mgr.get_block_by_id_async(actor=default_user, block_id=mine.id)
+ assert reloaded_mine.value == "updated-mine"
+
+ # ...theirs should remain untouched
+ reloaded_theirs = await mgr.get_block_by_id_async(actor=other_user_different_org, block_id=theirs.id)
+ assert reloaded_theirs.value == "theirs"
+
+ # warning should mention skipping the other-org ID
+ assert "skipping during bulk update" in caplog.text
+
+
+# ======================================================================================================================
+# Block Manager Tests - Checkpointing
+# ======================================================================================================================
+
+
+async def test_checkpoint_creates_history(server: SyncServer, default_user):
+ """
+ Ensures that calling checkpoint_block creates a BlockHistory row and updates
+ the block's current_history_entry_id appropriately.
+ """
+
+ block_manager = BlockManager()
+
+ # Create a block
+ initial_value = "Initial block content"
+ created_block = await block_manager.create_or_update_block_async(
+ PydanticBlock(label="test_checkpoint", value=initial_value), actor=default_user
+ )
+
+ # Act: checkpoint it
+ await block_manager.checkpoint_block_async(block_id=created_block.id, actor=default_user)
+
+ async with db_registry.async_session() as session:
+ # Get BlockHistory entries for this block
+ from sqlalchemy import select
+
+ stmt = select(BlockHistory).filter(BlockHistory.block_id == created_block.id)
+ result = await session.execute(stmt)
+ history_entries = list(result.scalars().all())
+ assert len(history_entries) == 1, "Exactly one history entry should be created"
+ hist = history_entries[0]
+
+ # Fetch ORM block for internal checks
+ db_block = await session.get(Block, created_block.id)
+
+ assert hist.sequence_number == 1
+ assert hist.value == initial_value
+ assert hist.actor_type == ActorType.LETTA_USER
+ assert hist.actor_id == default_user.id
+ assert db_block.current_history_entry_id == hist.id
+
+
+async def test_multiple_checkpoints(server: SyncServer, default_user):
+ block_manager = BlockManager()
+
+ # Create a block
+ block = await block_manager.create_or_update_block_async(PydanticBlock(label="test_multi_checkpoint", value="v1"), actor=default_user)
+
+ # 1) First checkpoint
+ await block_manager.checkpoint_block_async(block_id=block.id, actor=default_user)
+
+ # 2) Update block content
+ updated_block_data = PydanticBlock(**block.model_dump())
+ updated_block_data.value = "v2"
+ await block_manager.create_or_update_block_async(updated_block_data, actor=default_user)
+
+ # 3) Second checkpoint
+ await block_manager.checkpoint_block_async(block_id=block.id, actor=default_user)
+
+ async with db_registry.async_session() as session:
+ from sqlalchemy import select
+
+ stmt = select(BlockHistory).filter(BlockHistory.block_id == block.id).order_by(BlockHistory.sequence_number.asc())
+ result = await session.execute(stmt)
+ history_entries = list(result.scalars().all())
+ assert len(history_entries) == 2, "Should have two history entries"
+
+ # First is seq=1, value='v1'
+ assert history_entries[0].sequence_number == 1
+ assert history_entries[0].value == "v1"
+
+ # Second is seq=2, value='v2'
+ assert history_entries[1].sequence_number == 2
+ assert history_entries[1].value == "v2"
+
+ # The block should now point to the second entry
+ db_block = await session.get(Block, block.id)
+ assert db_block.current_history_entry_id == history_entries[1].id
+
+
+async def test_checkpoint_with_agent_id(server: SyncServer, default_user, sarah_agent):
+ """
+ Ensures that if we pass agent_id to checkpoint_block, we get
+ actor_type=LETTA_AGENT, actor_id=<agent.id> in BlockHistory.
+ """
+ block_manager = BlockManager()
+
+ # Create a block
+ block = await block_manager.create_or_update_block_async(
+ PydanticBlock(label="test_agent_checkpoint", value="Agent content"), actor=default_user
+ )
+
+ # Checkpoint with agent_id
+ await block_manager.checkpoint_block_async(block_id=block.id, actor=default_user, agent_id=sarah_agent.id)
+
+ # Verify
+ async with db_registry.async_session() as session:
+ from sqlalchemy import select
+
+ stmt = select(BlockHistory).filter(BlockHistory.block_id == block.id)
+ result = await session.execute(stmt)
+ hist_entry = result.scalar_one()
+ assert hist_entry.actor_type == ActorType.LETTA_AGENT
+ assert hist_entry.actor_id == sarah_agent.id
+
+
+async def test_checkpoint_with_no_state_change(server: SyncServer, default_user):
+ """
+ If we call checkpoint_block twice without any edits,
+ we expect two entries or only one, depending on your policy.
+ """
+ block_manager = BlockManager()
+
+ # Create block
+ block = await block_manager.create_or_update_block_async(PydanticBlock(label="test_no_change", value="original"), actor=default_user)
+
+ # 1) checkpoint
+ await block_manager.checkpoint_block_async(block_id=block.id, actor=default_user)
+ # 2) checkpoint again (no changes)
+ await block_manager.checkpoint_block_async(block_id=block.id, actor=default_user)
+
+ async with db_registry.async_session() as session:
+ from sqlalchemy import select
+
+ stmt = select(BlockHistory).filter(BlockHistory.block_id == block.id)
+ result = await session.execute(stmt)
+ all_hist = list(result.scalars().all())
+ assert len(all_hist) == 2
+
+
+async def test_checkpoint_concurrency_stale(server: SyncServer, default_user):
+ block_manager = BlockManager()
+
+ # create block
+ block = await block_manager.create_or_update_block_async(
+ PydanticBlock(label="test_stale_checkpoint", value="hello"), actor=default_user
+ )
+
+ # session1 loads
+ async with db_registry.async_session() as s1:
+ block_s1 = await s1.get(Block, block.id) # version=1
+
+ # session2 loads
+ async with db_registry.async_session() as s2:
+ block_s2 = await s2.get(Block, block.id) # also version=1
+
+ # session1 checkpoint => version=2
+ async with db_registry.async_session() as s1:
+ block_s1 = await s1.merge(block_s1)
+ await block_manager.checkpoint_block_async(
+ block_id=block_s1.id,
+ actor=default_user,
+ use_preloaded_block=block_s1, # let manager use the object in memory
+ )
+ # commits inside checkpoint_block => version goes to 2
+
+ # session2 tries to checkpoint => sees old version=1 => stale error
+ with pytest.raises(StaleDataError):
+ async with db_registry.async_session() as s2:
+ block_s2 = await s2.merge(block_s2)
+ await block_manager.checkpoint_block_async(
+ block_id=block_s2.id,
+ actor=default_user,
+ use_preloaded_block=block_s2,
+ )
+
+
+async def test_checkpoint_no_future_states(server: SyncServer, default_user):
+ """
+ Ensures that if the block is already at the highest sequence,
+ creating a new checkpoint does NOT delete anything.
+ """
+
+ block_manager = BlockManager()
+
+ # 1) Create block with "v1" and checkpoint => seq=1
+ block_v1 = await block_manager.create_or_update_block_async(PydanticBlock(label="no_future_test", value="v1"), actor=default_user)
+ await block_manager.checkpoint_block_async(block_id=block_v1.id, actor=default_user)
+
+ # 2) Create "v2" and checkpoint => seq=2
+ updated_data = PydanticBlock(**block_v1.model_dump())
+ updated_data.value = "v2"
+ await block_manager.create_or_update_block_async(updated_data, actor=default_user)
+ await block_manager.checkpoint_block_async(block_id=block_v1.id, actor=default_user)
+
+ # So we have seq=1: v1, seq=2: v2. No "future" states.
+ # 3) Another checkpoint (no changes made) => should become seq=3, not delete anything
+ await block_manager.checkpoint_block_async(block_id=block_v1.id, actor=default_user)
+
+ async with db_registry.async_session() as session:
+ # We expect 3 rows in block_history, none removed
+ from sqlalchemy import select
+
+ stmt = select(BlockHistory).filter(BlockHistory.block_id == block_v1.id).order_by(BlockHistory.sequence_number.asc())
+ result = await session.execute(stmt)
+ history_rows = list(result.scalars().all())
+ # Should be seq=1, seq=2, seq=3
+ assert len(history_rows) == 3
+ assert history_rows[0].value == "v1"
+ assert history_rows[1].value == "v2"
+ # The last is also "v2" if we didn't change it, or the same current fields
+ assert history_rows[2].sequence_number == 3
+ # There's no leftover row that was deleted
+
+
+# ======================================================================================================================
+# Block Manager Tests - Undo
+# ======================================================================================================================
+
+
+async def test_undo_checkpoint_block(server: SyncServer, default_user):
+ """
+ Verifies that we can undo to the previous checkpoint:
+ 1) Create a block and checkpoint -> sequence_number=1
+ 2) Update block content and checkpoint -> sequence_number=2
+ 3) Undo -> should revert block to sequence_number=1's content
+ """
+ block_manager = BlockManager()
+
+ # 1) Create block
+ initial_value = "Version 1 content"
+ created_block = await block_manager.create_or_update_block_async(
+ PydanticBlock(label="undo_test", value=initial_value), actor=default_user
+ )
+
+ # 2) First checkpoint => seq=1
+ await block_manager.checkpoint_block_async(block_id=created_block.id, actor=default_user)
+
+ # 3) Update block content to "Version 2"
+ updated_data = PydanticBlock(**created_block.model_dump())
+ updated_data.value = "Version 2 content"
+ await block_manager.create_or_update_block_async(updated_data, actor=default_user)
+
+ # 4) Second checkpoint => seq=2
+ await block_manager.checkpoint_block_async(block_id=created_block.id, actor=default_user)
+
+ # 5) Undo => revert to seq=1
+ undone_block = await block_manager.undo_checkpoint_block(block_id=created_block.id, actor=default_user)
+
+ # 6) Verify the block is now restored to "Version 1" content
+ assert undone_block.value == initial_value, "Block should revert to version 1 content"
+ assert undone_block.label == "undo_test", "Label should also revert if changed (or remain the same if unchanged)"
+
+
+#@pytest.mark.asyncio
+#async def test_checkpoint_deletes_future_states_after_undo(server: SyncServer, default_user):
+# """
+# Verifies that once we've undone to an earlier checkpoint, creating a new
+# checkpoint removes any leftover 'future' states that existed beyond that sequence.
+# """
+# block_manager = BlockManager()
+#
+# # 1) Create block
+# block_init = PydanticBlock(label="test_truncation", value="v1")
+# block_v1 = await block_manager.create_or_update_block_async(block_init, actor=default_user)
+# # Checkpoint => seq=1
+# await block_manager.checkpoint_block_async(block_id=block_v1.id, actor=default_user)
+#
+# # 2) Update to "v2", checkpoint => seq=2
+# block_v2 = PydanticBlock(**block_v1.model_dump())
+# block_v2.value = "v2"
+# await block_manager.create_or_update_block_async(block_v2, actor=default_user)
+# await block_manager.checkpoint_block_async(block_id=block_v1.id, actor=default_user)
+#
+# # 3) Update to "v3", checkpoint => seq=3
+# block_v3 = PydanticBlock(**block_v1.model_dump())
+# block_v3.value = "v3"
+# await block_manager.create_or_update_block_async(block_v3, actor=default_user)
+# await block_manager.checkpoint_block_async(block_id=block_v1.id, actor=default_user)
+#
+# # We now have three states in history: seq=1 (v1), seq=2 (v2), seq=3 (v3).
+#
+# # Undo from seq=3 -> seq=2
+# block_undo_1 = await block_manager.undo_checkpoint_block(block_v1.id, actor=default_user)
+# assert block_undo_1.value == "v2"
+#
+# # Undo from seq=2 -> seq=1
+# block_undo_2 = await block_manager.undo_checkpoint_block(block_v1.id, actor=default_user)
+# assert block_undo_2.value == "v1"
+#
+# # 4) Now we are at seq=1. If we checkpoint again, we should remove the old seq=2,3
+# # because the new code truncates future states beyond seq=1.
+#
+# # Let's do a new edit: "v1.5"
+# block_v1_5 = PydanticBlock(**block_undo_2.model_dump())
+# block_v1_5.value = "v1.5"
+# await block_manager.create_or_update_block_async(block_v1_5, actor=default_user)
+#
+# # 5) Checkpoint => new seq=2, removing the old seq=2 and seq=3
+# await block_manager.checkpoint_block_async(block_id=block_v1.id, actor=default_user)
+#
+# async with db_registry.async_session() as session:
+# # Let's see which BlockHistory rows remain
+# from sqlalchemy import select
+#
+# stmt = select(BlockHistory).filter(BlockHistory.block_id == block_v1.id).order_by(BlockHistory.sequence_number.asc())
+# result = await session.execute(stmt)
+# history_entries = list(result.scalars().all())
+#
+# # We expect two rows: seq=1 => "v1", seq=2 => "v1.5"
+# assert len(history_entries) == 2, f"Expected 2 entries, got {len(history_entries)}"
+# assert history_entries[0].sequence_number == 1
+# assert history_entries[0].value == "v1"
+# assert history_entries[1].sequence_number == 2
+# assert history_entries[1].value == "v1.5"
+#
+# # No row should contain "v2" or "v3"
+# existing_values = {h.value for h in history_entries}
+# assert "v2" not in existing_values, "Old seq=2 should have been removed."
+# assert "v3" not in existing_values, "Old seq=3 should have been removed."
+
+
+async def test_undo_no_history(server: SyncServer, default_user):
+ """
+ If a block has never been checkpointed (no current_history_entry_id),
+ undo_checkpoint_block should raise a ValueError.
+ """
+ block_manager = BlockManager()
+
+ # Create a block but don't checkpoint it
+ block = await block_manager.create_or_update_block_async(PydanticBlock(label="no_history_test", value="initial"), actor=default_user)
+
+ # Attempt to undo
+ with pytest.raises(ValueError, match="has no history entry - cannot undo"):
+ await block_manager.undo_checkpoint_block(block_id=block.id, actor=default_user)
+
+
+async def test_undo_first_checkpoint(server: SyncServer, default_user):
+ """
+ If the block is at the first checkpoint (sequence_number=1),
+ undo should fail because there's no prior checkpoint.
+ """
+ block_manager = BlockManager()
+
+ # 1) Create the block
+ block_data = PydanticBlock(label="first_checkpoint", value="Version1")
+ block = await block_manager.create_or_update_block_async(block_data, actor=default_user)
+
+ # 2) First checkpoint => seq=1
+ await block_manager.checkpoint_block_async(block_id=block.id, actor=default_user)
+
+ # Attempt undo -> expect ValueError
+ with pytest.raises(ValueError, match="Cannot undo further"):
+ await block_manager.undo_checkpoint_block(block_id=block.id, actor=default_user)
+
+
+async def test_undo_multiple_checkpoints(server: SyncServer, default_user):
+ """
+ Tests multiple checkpoints in a row, then undo repeatedly
+ from seq=3 -> seq=2 -> seq=1, verifying each revert.
+ """
+ block_manager = BlockManager()
+
+ # Step 1: Create block
+ block_data = PydanticBlock(label="multi_checkpoint", value="v1")
+ block_v1 = await block_manager.create_or_update_block_async(block_data, actor=default_user)
+ # checkpoint => seq=1
+ await block_manager.checkpoint_block_async(block_id=block_v1.id, actor=default_user)
+
+ # Step 2: Update to v2, checkpoint => seq=2
+ block_data_v2 = PydanticBlock(**block_v1.model_dump())
+ block_data_v2.value = "v2"
+ await block_manager.create_or_update_block_async(block_data_v2, actor=default_user)
+ await block_manager.checkpoint_block_async(block_id=block_v1.id, actor=default_user)
+
+ # Step 3: Update to v3, checkpoint => seq=3
+ block_data_v3 = PydanticBlock(**block_v1.model_dump())
+ block_data_v3.value = "v3"
+ await block_manager.create_or_update_block_async(block_data_v3, actor=default_user)
+ await block_manager.checkpoint_block_async(block_id=block_v1.id, actor=default_user)
+
+ # Now we have 3 seq: v1, v2, v3
+ # Undo from seq=3 -> seq=2
+ undone_block = await block_manager.undo_checkpoint_block(block_v1.id, actor=default_user)
+ assert undone_block.value == "v2"
+
+ # Undo from seq=2 -> seq=1
+ undone_block = await block_manager.undo_checkpoint_block(block_v1.id, actor=default_user)
+ assert undone_block.value == "v1"
+
+ # Try once more -> fails because seq=1 is the earliest
+ with pytest.raises(ValueError, match="Cannot undo further"):
+ await block_manager.undo_checkpoint_block(block_v1.id, actor=default_user)
+
+
+async def test_undo_concurrency_stale(server: SyncServer, default_user):
+ """
+ Demonstrate concurrency: both sessions start with the block at seq=2,
+ one session undoes first -> block now seq=1, version increments,
+ the other session tries to undo with stale data -> StaleDataError.
+ """
+ block_manager = BlockManager()
+
+ # 1) create block
+ block_data = PydanticBlock(label="concurrency_undo", value="v1")
+ block_v1 = await block_manager.create_or_update_block_async(block_data, actor=default_user)
+ # checkpoint => seq=1
+ await block_manager.checkpoint_block_async(block_v1.id, actor=default_user)
+
+ # 2) update to v2
+ block_data_v2 = PydanticBlock(**block_v1.model_dump())
+ block_data_v2.value = "v2"
+ await block_manager.create_or_update_block_async(block_data_v2, actor=default_user)
+ # checkpoint => seq=2
+ await block_manager.checkpoint_block_async(block_v1.id, actor=default_user)
+
+ # Now block is at seq=2
+
+ # session1 preloads the block
+ async with db_registry.async_session() as s1:
+ block_s1 = await s1.get(Block, block_v1.id) # version=? let's say 2 in memory
+
+ # session2 also preloads the block
+ async with db_registry.async_session() as s2:
+ block_s2 = await s2.get(Block, block_v1.id) # also version=2
+
+ # Session1 -> undo to seq=1
+ await block_manager.undo_checkpoint_block(
+ block_id=block_v1.id,
+ actor=default_user,
+ use_preloaded_block=block_s1, # stale object from session1
+ )
+ # This commits first => block now points to seq=1, version increments
+
+ # Session2 tries the same undo, but it's stale
+ with pytest.raises(StaleDataError):
+ await block_manager.undo_checkpoint_block(
+ block_id=block_v1.id, actor=default_user, use_preloaded_block=block_s2
+ ) # also seq=2 in memory
+
+
+# ======================================================================================================================
+# Block Manager Tests - Redo
+# ======================================================================================================================
+
+
+async def test_redo_checkpoint_block(server: SyncServer, default_user):
+ """
+ 1) Create a block with value v1 -> checkpoint => seq=1
+ 2) Update to v2 -> checkpoint => seq=2
+ 3) Update to v3 -> checkpoint => seq=3
+ 4) Undo once (seq=3 -> seq=2)
+ 5) Redo once (seq=2 -> seq=3)
+ """
+
+ block_manager = BlockManager()
+
+ # 1) Create block, set value='v1'; checkpoint => seq=1
+ block_v1 = await block_manager.create_or_update_block_async(PydanticBlock(label="redo_test", value="v1"), actor=default_user)
+ await block_manager.checkpoint_block_async(block_id=block_v1.id, actor=default_user)
+
+ # 2) Update to 'v2'; checkpoint => seq=2
+ block_v2 = PydanticBlock(**block_v1.model_dump())
+ block_v2.value = "v2"
+ await block_manager.create_or_update_block_async(block_v2, actor=default_user)
+ await block_manager.checkpoint_block_async(block_id=block_v1.id, actor=default_user)
+
+ # 3) Update to 'v3'; checkpoint => seq=3
+ block_v3 = PydanticBlock(**block_v1.model_dump())
+ block_v3.value = "v3"
+ await block_manager.create_or_update_block_async(block_v3, actor=default_user)
+ await block_manager.checkpoint_block_async(block_id=block_v1.id, actor=default_user)
+
+ # Undo from seq=3 -> seq=2
+ undone_block = await block_manager.undo_checkpoint_block(block_v1.id, actor=default_user)
+ assert undone_block.value == "v2", "After undo, block should revert to v2"
+
+ # Redo from seq=2 -> seq=3
+ redone_block = await block_manager.redo_checkpoint_block(block_v1.id, actor=default_user)
+ assert redone_block.value == "v3", "After redo, block should go back to v3"
+
+
+async def test_redo_no_history(server: SyncServer, default_user):
+ """
+ If a block has no current_history_entry_id (never checkpointed),
+ then redo_checkpoint_block should raise ValueError.
+ """
+ block_manager = BlockManager()
+
+ # Create block with no checkpoint
+ block = await block_manager.create_or_update_block_async(PydanticBlock(label="redo_no_history", value="v0"), actor=default_user)
+
+ # Attempt to redo => expect ValueError
+ with pytest.raises(ValueError, match="no history entry - cannot redo"):
+ await block_manager.redo_checkpoint_block(block.id, actor=default_user)
+
+
+async def test_redo_at_highest_checkpoint(server: SyncServer, default_user):
+ """
+ If the block is at the maximum sequence number, there's no higher checkpoint to move to.
+ redo_checkpoint_block should raise ValueError.
+ """
+ block_manager = BlockManager()
+
+ # 1) Create block => checkpoint => seq=1
+ b_init = await block_manager.create_or_update_block_async(PydanticBlock(label="redo_highest", value="v1"), actor=default_user)
+ await block_manager.checkpoint_block_async(b_init.id, actor=default_user)
+
+ # 2) Another edit => seq=2
+ b_next = PydanticBlock(**b_init.model_dump())
+ b_next.value = "v2"
+ await block_manager.create_or_update_block_async(b_next, actor=default_user)
+ await block_manager.checkpoint_block_async(b_init.id, actor=default_user)
+
+ # We are at seq=2, which is the highest checkpoint.
+ # Attempt redo => there's no seq=3
+ with pytest.raises(ValueError, match="Cannot redo further"):
+ await block_manager.redo_checkpoint_block(b_init.id, actor=default_user)
+
+
+async def test_redo_after_multiple_undo(server: SyncServer, default_user):
+ """
+ 1) Create and checkpoint versions: v1 -> seq=1, v2 -> seq=2, v3 -> seq=3, v4 -> seq=4
+ 2) Undo thrice => from seq=4 to seq=1
+ 3) Redo thrice => from seq=1 back to seq=4
+ """
+ block_manager = BlockManager()
+
+ # Step 1: create initial block => seq=1
+ b_init = await block_manager.create_or_update_block_async(PydanticBlock(label="redo_multi", value="v1"), actor=default_user)
+ await block_manager.checkpoint_block_async(b_init.id, actor=default_user)
+
+ # seq=2
+ b_v2 = PydanticBlock(**b_init.model_dump())
+ b_v2.value = "v2"
+ await block_manager.create_or_update_block_async(b_v2, actor=default_user)
+ await block_manager.checkpoint_block_async(b_init.id, actor=default_user)
+
+ # seq=3
+ b_v3 = PydanticBlock(**b_init.model_dump())
+ b_v3.value = "v3"
+ await block_manager.create_or_update_block_async(b_v3, actor=default_user)
+ await block_manager.checkpoint_block_async(b_init.id, actor=default_user)
+
+ # seq=4
+ b_v4 = PydanticBlock(**b_init.model_dump())
+ b_v4.value = "v4"
+ await block_manager.create_or_update_block_async(b_v4, actor=default_user)
+ await block_manager.checkpoint_block_async(b_init.id, actor=default_user)
+
+ # We have 4 checkpoints: v1...v4. Current is seq=4.
+
+ # 2) Undo thrice => from seq=4 -> seq=1
+ for expected_value in ["v3", "v2", "v1"]:
+ undone_block = await block_manager.undo_checkpoint_block(b_init.id, actor=default_user)
+ assert undone_block.value == expected_value, f"Undo should get us back to {expected_value}"
+
+ # 3) Redo thrice => from seq=1 -> seq=4
+ for expected_value in ["v2", "v3", "v4"]:
+ redone_block = await block_manager.redo_checkpoint_block(b_init.id, actor=default_user)
+ assert redone_block.value == expected_value, f"Redo should get us forward to {expected_value}"
+
+
+async def test_redo_concurrency_stale(server: SyncServer, default_user):
+ block_manager = BlockManager()
+
+ # 1) Create block => checkpoint => seq=1
+ block = await block_manager.create_or_update_block_async(PydanticBlock(label="redo_concurrency", value="v1"), actor=default_user)
+ await block_manager.checkpoint_block_async(block.id, actor=default_user)
+
+ # 2) Another edit => checkpoint => seq=2
+ block_v2 = PydanticBlock(**block.model_dump())
+ block_v2.value = "v2"
+ await block_manager.create_or_update_block_async(block_v2, actor=default_user)
+ await block_manager.checkpoint_block_async(block.id, actor=default_user)
+
+ # 3) Another edit => checkpoint => seq=3
+ block_v3 = PydanticBlock(**block.model_dump())
+ block_v3.value = "v3"
+ await block_manager.create_or_update_block_async(block_v3, actor=default_user)
+ await block_manager.checkpoint_block_async(block.id, actor=default_user)
+ # Now the block is at seq=3 in the DB
+
+ # 4) Undo from seq=3 -> seq=2 so that we have a known future state at seq=3
+ undone_block = await block_manager.undo_checkpoint_block(block.id, actor=default_user)
+ assert undone_block.value == "v2"
+
+ # At this point the block is physically at seq=2 in DB,
+ # but there's a valid row for seq=3 in block_history (the 'v3' state).
+
+ # 5) Simulate concurrency: two sessions each read the block at seq=2
+ async with db_registry.async_session() as s1:
+ block_s1 = await s1.get(Block, block.id)
+ async with db_registry.async_session() as s2:
+ block_s2 = await s2.get(Block, block.id)
+
+ # 6) Session1 redoes to seq=3 first -> success
+ await block_manager.redo_checkpoint_block(block_id=block.id, actor=default_user, use_preloaded_block=block_s1)
+ # commits => block is now seq=3 in DB, version increments
+
+ # 7) Session2 tries to do the same from stale version
+ # => we expect StaleDataError, because the second session is using
+ # an out-of-date version of the block
+ with pytest.raises(StaleDataError):
+ await block_manager.redo_checkpoint_block(block_id=block.id, actor=default_user, use_preloaded_block=block_s2)
diff --git a/tests/managers/test_file_manager.py b/tests/managers/test_file_manager.py
new file mode 100644
index 000000000..7a1284b8a
--- /dev/null
+++ b/tests/managers/test_file_manager.py
@@ -0,0 +1,1268 @@
+import asyncio
+import json
+import logging
+import os
+import random
+import re
+import string
+import time
+import uuid
+from datetime import datetime, timedelta, timezone
+from typing import List
+from unittest.mock import AsyncMock, Mock, patch
+
+import pytest
+from _pytest.python_api import approx
+from anthropic.types.beta import BetaMessage
+from anthropic.types.beta.messages import BetaMessageBatchIndividualResponse, BetaMessageBatchSucceededResult
+
+# Import shared fixtures and constants from conftest
+from conftest import (
+ CREATE_DELAY_SQLITE,
+ DEFAULT_EMBEDDING_CONFIG,
+ USING_SQLITE,
+)
+from openai.types.chat.chat_completion_message_tool_call import ChatCompletionMessageToolCall as OpenAIToolCall, Function as OpenAIFunction
+from sqlalchemy import func, select
+from sqlalchemy.exc import IntegrityError, InvalidRequestError
+from sqlalchemy.orm.exc import StaleDataError
+
+from letta.config import LettaConfig
+from letta.constants import (
+ BASE_MEMORY_TOOLS,
+ BASE_SLEEPTIME_TOOLS,
+ BASE_TOOLS,
+ BASE_VOICE_SLEEPTIME_CHAT_TOOLS,
+ BASE_VOICE_SLEEPTIME_TOOLS,
+ BUILTIN_TOOLS,
+ DEFAULT_ORG_ID,
+ DEFAULT_ORG_NAME,
+ FILES_TOOLS,
+ LETTA_TOOL_EXECUTION_DIR,
+ LETTA_TOOL_SET,
+ LOCAL_ONLY_MULTI_AGENT_TOOLS,
+ MCP_TOOL_TAG_NAME_PREFIX,
+ MULTI_AGENT_TOOLS,
+)
+from letta.data_sources.redis_client import NoopAsyncRedisClient, get_redis_client
+from letta.errors import LettaAgentNotFoundError
+from letta.functions.functions import derive_openai_json_schema, parse_source_code
+from letta.functions.mcp_client.types import MCPTool
+from letta.helpers import ToolRulesSolver
+from letta.helpers.datetime_helpers import AsyncTimer
+from letta.jobs.types import ItemUpdateInfo, RequestStatusUpdateInfo, StepStatusUpdateInfo
+from letta.orm import Base, Block
+from letta.orm.block_history import BlockHistory
+from letta.orm.errors import NoResultFound, UniqueConstraintViolationError
+from letta.orm.file import FileContent as FileContentModel, FileMetadata as FileMetadataModel
+from letta.schemas.agent import CreateAgent, UpdateAgent
+from letta.schemas.block import Block as PydanticBlock, BlockUpdate, CreateBlock
+from letta.schemas.embedding_config import EmbeddingConfig
+from letta.schemas.enums import (
+ ActorType,
+ AgentStepStatus,
+ FileProcessingStatus,
+ JobStatus,
+ JobType,
+ MessageRole,
+ ProviderType,
+ SandboxType,
+ StepStatus,
+ TagMatchMode,
+ ToolType,
+ VectorDBProvider,
+)
+from letta.schemas.environment_variables import SandboxEnvironmentVariableCreate, SandboxEnvironmentVariableUpdate
+from letta.schemas.file import FileMetadata, FileMetadata as PydanticFileMetadata
+from letta.schemas.identity import IdentityCreate, IdentityProperty, IdentityPropertyType, IdentityType, IdentityUpdate, IdentityUpsert
+from letta.schemas.job import BatchJob, Job, Job as PydanticJob, JobUpdate, LettaRequestConfig
+from letta.schemas.letta_message import UpdateAssistantMessage, UpdateReasoningMessage, UpdateSystemMessage, UpdateUserMessage
+from letta.schemas.letta_message_content import TextContent
+from letta.schemas.letta_stop_reason import LettaStopReason, StopReasonType
+from letta.schemas.llm_batch_job import AgentStepState, LLMBatchItem
+from letta.schemas.llm_config import LLMConfig
+from letta.schemas.message import Message as PydanticMessage, MessageCreate, MessageUpdate
+from letta.schemas.openai.chat_completion_response import UsageStatistics
+from letta.schemas.organization import Organization, Organization as PydanticOrganization, OrganizationUpdate
+from letta.schemas.passage import Passage as PydanticPassage
+from letta.schemas.pip_requirement import PipRequirement
+from letta.schemas.run import Run as PydanticRun
+from letta.schemas.sandbox_config import E2BSandboxConfig, LocalSandboxConfig, SandboxConfigCreate, SandboxConfigUpdate
+from letta.schemas.source import Source as PydanticSource, SourceUpdate
+from letta.schemas.tool import Tool as PydanticTool, ToolCreate, ToolUpdate
+from letta.schemas.tool_rule import InitToolRule
+from letta.schemas.user import User as PydanticUser, UserUpdate
+from letta.server.db import db_registry
+from letta.server.server import SyncServer
+from letta.services.block_manager import BlockManager
+from letta.services.helpers.agent_manager_helper import calculate_base_tools, calculate_multi_agent_tools, validate_agent_exists_async
+from letta.services.step_manager import FeedbackType
+from letta.settings import settings, tool_settings
+from letta.utils import calculate_file_defaults_based_on_context_window
+from tests.helpers.utils import comprehensive_agent_checks, validate_context_window_overview
+from tests.utils import random_string
+
+# ======================================================================================================================
+# FileAgent Tests
+# ======================================================================================================================
+
+
+async def test_attach_creates_association(server, default_user, sarah_agent, default_file):
+ assoc, closed_files = await server.file_agent_manager.attach_file(
+ agent_id=sarah_agent.id,
+ file_id=default_file.id,
+ file_name=default_file.file_name,
+ source_id=default_file.source_id,
+ actor=default_user,
+ visible_content="hello",
+ max_files_open=sarah_agent.max_files_open,
+ )
+
+ assert assoc.file_id == default_file.id
+ assert assoc.is_open is True
+ assert assoc.visible_content == "hello"
+
+ sarah_agent = await server.agent_manager.get_agent_by_id_async(agent_id=sarah_agent.id, actor=default_user)
+ file_blocks = sarah_agent.memory.file_blocks
+ assert len(file_blocks) == 1
+ assert file_blocks[0].value == assoc.visible_content
+ assert file_blocks[0].label == default_file.file_name
+
+
+async def test_attach_is_idempotent(server, default_user, sarah_agent, default_file):
+ a1, closed_files = await server.file_agent_manager.attach_file(
+ agent_id=sarah_agent.id,
+ file_id=default_file.id,
+ file_name=default_file.file_name,
+ source_id=default_file.source_id,
+ actor=default_user,
+ visible_content="first",
+ max_files_open=sarah_agent.max_files_open,
+ )
+
+ # second attach with different params
+ a2, closed_files = await server.file_agent_manager.attach_file(
+ agent_id=sarah_agent.id,
+ file_id=default_file.id,
+ file_name=default_file.file_name,
+ source_id=default_file.source_id,
+ actor=default_user,
+ is_open=False,
+ visible_content="second",
+ max_files_open=sarah_agent.max_files_open,
+ )
+
+ assert a1.id == a2.id
+ assert a2.is_open is False
+ assert a2.visible_content == "second"
+
+ sarah_agent = await server.agent_manager.get_agent_by_id_async(agent_id=sarah_agent.id, actor=default_user)
+ file_blocks = sarah_agent.memory.file_blocks
+ assert len(file_blocks) == 1
+ assert file_blocks[0].value == "" # not open
+ assert file_blocks[0].label == default_file.file_name
+
+
+async def test_update_file_agent(server, file_attachment, default_user):
+ updated = await server.file_agent_manager.update_file_agent_by_id(
+ agent_id=file_attachment.agent_id,
+ file_id=file_attachment.file_id,
+ actor=default_user,
+ is_open=False,
+ visible_content="updated",
+ )
+ assert updated.is_open is False
+ assert updated.visible_content == "updated"
+
+
+async def test_update_file_agent_by_file_name(server, file_attachment, default_user):
+ updated = await server.file_agent_manager.update_file_agent_by_name(
+ agent_id=file_attachment.agent_id,
+ file_name=file_attachment.file_name,
+ actor=default_user,
+ is_open=False,
+ visible_content="updated",
+ )
+ assert updated.is_open is False
+ assert updated.visible_content == "updated"
+ assert updated.start_line is None # start_line should default to None
+ assert updated.end_line is None # end_line should default to None
+
+
+async def test_file_agent_line_tracking(server, default_user, sarah_agent, default_source):
+ """Test that line information is captured when opening files with line ranges"""
+ from letta.schemas.file import FileMetadata as PydanticFileMetadata
+
+ # Create a test file with multiple lines
+ test_content = "line 1\nline 2\nline 3\nline 4\nline 5"
+ file_metadata = PydanticFileMetadata(
+ file_name="test_lines.txt",
+ organization_id=default_user.organization_id,
+ source_id=default_source.id,
+ )
+ file = await server.file_manager.create_file(file_metadata=file_metadata, actor=default_user, text=test_content)
+
+ # Test opening with line range using enforce_max_open_files_and_open
+ closed_files, was_already_open, previous_ranges = await server.file_agent_manager.enforce_max_open_files_and_open(
+ agent_id=sarah_agent.id,
+ file_id=file.id,
+ file_name=file.file_name,
+ source_id=file.source_id,
+ actor=default_user,
+ visible_content="2: line 2\n3: line 3",
+ max_files_open=sarah_agent.max_files_open,
+ start_line=2, # 1-indexed
+ end_line=4, # exclusive
+ )
+
+ # Retrieve and verify line tracking
+ retrieved = await server.file_agent_manager.get_file_agent_by_id(
+ agent_id=sarah_agent.id,
+ file_id=file.id,
+ actor=default_user,
+ )
+
+ assert retrieved.start_line == 2
+ assert retrieved.end_line == 4
+ assert previous_ranges == {} # No previous range since it wasn't open before
+
+ # Test opening without line range - should clear line info and capture previous range
+ closed_files, was_already_open, previous_ranges = await server.file_agent_manager.enforce_max_open_files_and_open(
+ agent_id=sarah_agent.id,
+ file_id=file.id,
+ file_name=file.file_name,
+ source_id=file.source_id,
+ actor=default_user,
+ visible_content="full file content",
+ max_files_open=sarah_agent.max_files_open,
+ start_line=None,
+ end_line=None,
+ )
+
+ # Retrieve and verify line info is cleared
+ retrieved = await server.file_agent_manager.get_file_agent_by_id(
+ agent_id=sarah_agent.id,
+ file_id=file.id,
+ actor=default_user,
+ )
+
+ assert retrieved.start_line is None
+ assert retrieved.end_line is None
+ assert previous_ranges == {file.file_name: (2, 4)} # Should capture the previous range
+
+
+async def test_mark_access(server, file_attachment, default_user):
+ old_ts = file_attachment.last_accessed_at
+ if USING_SQLITE:
+ time.sleep(CREATE_DELAY_SQLITE)
+ else:
+ await asyncio.sleep(0.01)
+
+ await server.file_agent_manager.mark_access(
+ agent_id=file_attachment.agent_id,
+ file_id=file_attachment.file_id,
+ actor=default_user,
+ )
+ refreshed = await server.file_agent_manager.get_file_agent_by_id(
+ agent_id=file_attachment.agent_id,
+ file_id=file_attachment.file_id,
+ actor=default_user,
+ )
+ assert refreshed.last_accessed_at > old_ts
+
+
+async def test_list_files_and_agents(
+ server,
+ default_user,
+ sarah_agent,
+ charles_agent,
+ default_file,
+ another_file,
+):
+ # default_file ↔ charles (open)
+ await server.file_agent_manager.attach_file(
+ agent_id=charles_agent.id,
+ file_id=default_file.id,
+ file_name=default_file.file_name,
+ source_id=default_file.source_id,
+ actor=default_user,
+ max_files_open=charles_agent.max_files_open,
+ )
+ # default_file ↔ sarah (open)
+ await server.file_agent_manager.attach_file(
+ agent_id=sarah_agent.id,
+ file_id=default_file.id,
+ file_name=default_file.file_name,
+ source_id=default_file.source_id,
+ actor=default_user,
+ max_files_open=sarah_agent.max_files_open,
+ )
+ # another_file ↔ sarah (closed)
+ await server.file_agent_manager.attach_file(
+ agent_id=sarah_agent.id,
+ file_id=another_file.id,
+ file_name=another_file.file_name,
+ source_id=another_file.source_id,
+ actor=default_user,
+ is_open=False,
+ max_files_open=sarah_agent.max_files_open,
+ )
+
+ files_for_sarah = await server.file_agent_manager.list_files_for_agent(
+ sarah_agent.id, per_file_view_window_char_limit=sarah_agent.per_file_view_window_char_limit, actor=default_user
+ )
+ assert {f.file_id for f in files_for_sarah} == {default_file.id, another_file.id}
+
+ open_only = await server.file_agent_manager.list_files_for_agent(
+ sarah_agent.id, per_file_view_window_char_limit=sarah_agent.per_file_view_window_char_limit, actor=default_user, is_open_only=True
+ )
+ assert {f.file_id for f in open_only} == {default_file.id}
+
+ agents_for_default = await server.file_agent_manager.list_agents_for_file(default_file.id, actor=default_user)
+ assert {a.agent_id for a in agents_for_default} == {sarah_agent.id, charles_agent.id}
+
+ sarah_agent = await server.agent_manager.get_agent_by_id_async(agent_id=sarah_agent.id, actor=default_user)
+ file_blocks = sarah_agent.memory.file_blocks
+ assert len(file_blocks) == 2
+ charles_agent = await server.agent_manager.get_agent_by_id_async(agent_id=charles_agent.id, actor=default_user)
+ file_blocks = charles_agent.memory.file_blocks
+ assert len(file_blocks) == 1
+ assert file_blocks[0].value == ""
+ assert file_blocks[0].label == default_file.file_name
+
+
+async def test_list_files_for_agent_paginated_basic(
+ server,
+ default_user,
+ sarah_agent,
+ default_source,
+):
+ """Test basic pagination functionality."""
+ # create 5 files and attach them to sarah
+ for i in range(5):
+ file_metadata = PydanticFileMetadata(
+ file_name=f"paginated_file_{i}.txt",
+ source_id=default_source.id,
+ organization_id=default_user.organization_id,
+ )
+ file = await server.file_manager.create_file(file_metadata, actor=default_user)
+ await server.file_agent_manager.attach_file(
+ agent_id=sarah_agent.id,
+ file_id=file.id,
+ file_name=file.file_name,
+ source_id=file.source_id,
+ actor=default_user,
+ max_files_open=sarah_agent.max_files_open,
+ )
+
+ # get first page
+ page1, cursor1, has_more1 = await server.file_agent_manager.list_files_for_agent_paginated(
+ agent_id=sarah_agent.id,
+ actor=default_user,
+ limit=3,
+ )
+ assert len(page1) == 3
+ assert has_more1 is True
+ assert cursor1 is not None
+
+ # get second page using cursor
+ page2, cursor2, has_more2 = await server.file_agent_manager.list_files_for_agent_paginated(
+ agent_id=sarah_agent.id,
+ actor=default_user,
+ cursor=cursor1,
+ limit=3,
+ )
+ assert len(page2) == 2 # only 2 files left (5 total - 3 already fetched)
+ assert has_more2 is False
+ assert cursor2 is not None
+
+ # verify no overlap between pages
+ page1_ids = {fa.id for fa in page1}
+ page2_ids = {fa.id for fa in page2}
+ assert page1_ids.isdisjoint(page2_ids)
+
+
+async def test_list_files_for_agent_paginated_filter_open(
+ server,
+ default_user,
+ sarah_agent,
+ default_source,
+):
+ """Test pagination with is_open=True filter."""
+ # create files: 3 open, 2 closed
+ for i in range(5):
+ file_metadata = PydanticFileMetadata(
+ file_name=f"filter_file_{i}.txt",
+ source_id=default_source.id,
+ organization_id=default_user.organization_id,
+ )
+ file = await server.file_manager.create_file(file_metadata, actor=default_user)
+ await server.file_agent_manager.attach_file(
+ agent_id=sarah_agent.id,
+ file_id=file.id,
+ file_name=file.file_name,
+ source_id=file.source_id,
+ actor=default_user,
+ is_open=(i < 3), # first 3 are open
+ max_files_open=sarah_agent.max_files_open,
+ )
+
+ # get only open files
+ open_files, cursor, has_more = await server.file_agent_manager.list_files_for_agent_paginated(
+ agent_id=sarah_agent.id,
+ actor=default_user,
+ is_open=True,
+ limit=10,
+ )
+ assert len(open_files) == 3
+ assert has_more is False
+ assert all(fa.is_open for fa in open_files)
+
+
+async def test_list_files_for_agent_paginated_filter_closed(
+ server,
+ default_user,
+ sarah_agent,
+ default_source,
+):
+ """Test pagination with is_open=False filter."""
+ # create files: 2 open, 4 closed
+ for i in range(6):
+ file_metadata = PydanticFileMetadata(
+ file_name=f"closed_file_{i}.txt",
+ source_id=default_source.id,
+ organization_id=default_user.organization_id,
+ )
+ file = await server.file_manager.create_file(file_metadata, actor=default_user)
+ await server.file_agent_manager.attach_file(
+ agent_id=sarah_agent.id,
+ file_id=file.id,
+ file_name=file.file_name,
+ source_id=file.source_id,
+ actor=default_user,
+ is_open=(i < 2), # first 2 are open, rest are closed
+ max_files_open=sarah_agent.max_files_open,
+ )
+
+ # paginate through closed files
+ page1, cursor1, has_more1 = await server.file_agent_manager.list_files_for_agent_paginated(
+ agent_id=sarah_agent.id,
+ actor=default_user,
+ is_open=False,
+ limit=2,
+ )
+ assert len(page1) == 2
+ assert has_more1 is True
+ assert all(not fa.is_open for fa in page1)
+
+ # get second page of closed files
+ page2, cursor2, has_more2 = await server.file_agent_manager.list_files_for_agent_paginated(
+ agent_id=sarah_agent.id,
+ actor=default_user,
+ is_open=False,
+ cursor=cursor1,
+ limit=3,
+ )
+ assert len(page2) == 2 # only 2 closed files left
+ assert has_more2 is False
+ assert all(not fa.is_open for fa in page2)
+
+
+async def test_list_files_for_agent_paginated_empty(
+ server,
+ default_user,
+ charles_agent,
+):
+ """Test pagination with agent that has no files."""
+ # charles_agent has no files attached in this test
+ result, cursor, has_more = await server.file_agent_manager.list_files_for_agent_paginated(
+ agent_id=charles_agent.id,
+ actor=default_user,
+ limit=10,
+ )
+ assert len(result) == 0
+ assert cursor is None
+ assert has_more is False
+
+
+async def test_list_files_for_agent_paginated_large_limit(
+ server,
+ default_user,
+ sarah_agent,
+ default_source,
+):
+ """Test that large limit returns all files without pagination."""
+ # create 3 files
+ for i in range(3):
+ file_metadata = PydanticFileMetadata(
+ file_name=f"all_files_{i}.txt",
+ source_id=default_source.id,
+ organization_id=default_user.organization_id,
+ )
+ file = await server.file_manager.create_file(file_metadata, actor=default_user)
+ await server.file_agent_manager.attach_file(
+ agent_id=sarah_agent.id,
+ file_id=file.id,
+ file_name=file.file_name,
+ source_id=file.source_id,
+ actor=default_user,
+ max_files_open=sarah_agent.max_files_open,
+ )
+
+ # request with large limit
+ all_files, cursor, has_more = await server.file_agent_manager.list_files_for_agent_paginated(
+ agent_id=sarah_agent.id,
+ actor=default_user,
+ limit=100,
+ )
+ assert len(all_files) == 3
+ assert has_more is False
+ assert cursor is not None # cursor is still set to last item
+
+
+async def test_detach_file(server, file_attachment, default_user):
+ await server.file_agent_manager.detach_file(
+ agent_id=file_attachment.agent_id,
+ file_id=file_attachment.file_id,
+ actor=default_user,
+ )
+ res = await server.file_agent_manager.get_file_agent_by_id(
+ agent_id=file_attachment.agent_id,
+ file_id=file_attachment.file_id,
+ actor=default_user,
+ )
+ assert res is None
+
+
+async def test_detach_file_bulk(
+ server,
+ default_user,
+ sarah_agent,
+ charles_agent,
+ default_source,
+):
+ """Test bulk deletion of multiple agent-file associations."""
+ # Create multiple files
+ files = []
+ for i in range(3):
+ file_metadata = PydanticFileMetadata(
+ file_name=f"test_file_{i}.txt",
+ source_id=default_source.id,
+ organization_id=default_user.organization_id,
+ )
+ file = await server.file_manager.create_file(file_metadata, actor=default_user)
+ files.append(file)
+
+ # Attach all files to both agents
+ for file in files:
+ await server.file_agent_manager.attach_file(
+ agent_id=sarah_agent.id,
+ file_id=file.id,
+ file_name=file.file_name,
+ source_id=file.source_id,
+ actor=default_user,
+ max_files_open=sarah_agent.max_files_open,
+ )
+ await server.file_agent_manager.attach_file(
+ agent_id=charles_agent.id,
+ file_id=file.id,
+ file_name=file.file_name,
+ source_id=file.source_id,
+ actor=default_user,
+ max_files_open=charles_agent.max_files_open,
+ )
+
+ # Verify all files are attached to both agents
+ sarah_files = await server.file_agent_manager.list_files_for_agent(
+ sarah_agent.id, per_file_view_window_char_limit=sarah_agent.per_file_view_window_char_limit, actor=default_user
+ )
+ charles_files = await server.file_agent_manager.list_files_for_agent(
+ charles_agent.id, per_file_view_window_char_limit=charles_agent.per_file_view_window_char_limit, actor=default_user
+ )
+ assert len(sarah_files) == 3
+ assert len(charles_files) == 3
+
+ # Test 1: Bulk delete specific files from specific agents
+ agent_file_pairs = [
+ (sarah_agent.id, files[0].id), # Remove file 0 from sarah
+ (sarah_agent.id, files[1].id), # Remove file 1 from sarah
+ (charles_agent.id, files[1].id), # Remove file 1 from charles
+ ]
+
+ deleted_count = await server.file_agent_manager.detach_file_bulk(agent_file_pairs=agent_file_pairs, actor=default_user)
+ assert deleted_count == 3
+
+ # Verify the correct files were deleted
+ sarah_files = await server.file_agent_manager.list_files_for_agent(
+ sarah_agent.id, per_file_view_window_char_limit=sarah_agent.per_file_view_window_char_limit, actor=default_user
+ )
+ charles_files = await server.file_agent_manager.list_files_for_agent(
+ charles_agent.id, per_file_view_window_char_limit=charles_agent.per_file_view_window_char_limit, actor=default_user
+ )
+
+ # Sarah should only have file 2 left
+ assert len(sarah_files) == 1
+ assert sarah_files[0].file_id == files[2].id
+
+ # Charles should have files 0 and 2 left
+ assert len(charles_files) == 2
+ charles_file_ids = {f.file_id for f in charles_files}
+ assert charles_file_ids == {files[0].id, files[2].id}
+
+ # Test 2: Empty list should return 0 and not fail
+ deleted_count = await server.file_agent_manager.detach_file_bulk(agent_file_pairs=[], actor=default_user)
+ assert deleted_count == 0
+
+ # Test 3: Attempting to delete already deleted associations should return 0
+ agent_file_pairs = [
+ (sarah_agent.id, files[0].id), # Already deleted
+ (sarah_agent.id, files[1].id), # Already deleted
+ ]
+ deleted_count = await server.file_agent_manager.detach_file_bulk(agent_file_pairs=agent_file_pairs, actor=default_user)
+ assert deleted_count == 0
+
+
+async def test_org_scoping(
+ server,
+ default_user,
+ other_user_different_org,
+ sarah_agent,
+ default_file,
+):
+ # attach as default_user
+ await server.file_agent_manager.attach_file(
+ agent_id=sarah_agent.id,
+ file_id=default_file.id,
+ file_name=default_file.file_name,
+ source_id=default_file.source_id,
+ actor=default_user,
+ max_files_open=sarah_agent.max_files_open,
+ )
+
+ # other org should see nothing
+ files = await server.file_agent_manager.list_files_for_agent(
+ sarah_agent.id, per_file_view_window_char_limit=sarah_agent.per_file_view_window_char_limit, actor=other_user_different_org
+ )
+ assert files == []
+
+
+# ======================================================================================================================
+# LRU File Management Tests
+# ======================================================================================================================
+
+
+async def test_mark_access_bulk(server, default_user, sarah_agent, default_source):
+ """Test that mark_access_bulk updates last_accessed_at for multiple files."""
+ import time
+
+ # Create multiple files and attach them
+ files = []
+ for i in range(3):
+ file_metadata = PydanticFileMetadata(
+ file_name=f"test_file_{i}.txt",
+ organization_id=default_user.organization_id,
+ source_id=default_source.id,
+ )
+ file = await server.file_manager.create_file(file_metadata=file_metadata, actor=default_user, text=f"test content {i}")
+ files.append(file)
+
+ # Attach all files (they'll be open by default)
+ attached_files = []
+ for file in files:
+ file_agent, closed_files = await server.file_agent_manager.attach_file(
+ agent_id=sarah_agent.id,
+ file_id=file.id,
+ file_name=file.file_name,
+ source_id=file.source_id,
+ actor=default_user,
+ visible_content=f"content for {file.file_name}",
+ max_files_open=sarah_agent.max_files_open,
+ )
+ attached_files.append(file_agent)
+
+ # Get initial timestamps
+ initial_times = {}
+ for file_agent in attached_files:
+ fa = await server.file_agent_manager.get_file_agent_by_id(agent_id=sarah_agent.id, file_id=file_agent.file_id, actor=default_user)
+ initial_times[fa.file_name] = fa.last_accessed_at
+
+ # Wait a moment to ensure timestamp difference
+ time.sleep(1.1)
+
+ # Use mark_access_bulk on subset of files
+ file_names_to_mark = [files[0].file_name, files[2].file_name]
+ await server.file_agent_manager.mark_access_bulk(agent_id=sarah_agent.id, file_names=file_names_to_mark, actor=default_user)
+
+ # Check that only marked files have updated timestamps
+ for i, file in enumerate(files):
+ fa = await server.file_agent_manager.get_file_agent_by_id(agent_id=sarah_agent.id, file_id=file.id, actor=default_user)
+
+ if file.file_name in file_names_to_mark:
+ assert fa.last_accessed_at > initial_times[file.file_name], f"File {file.file_name} should have updated timestamp"
+ else:
+ assert fa.last_accessed_at == initial_times[file.file_name], f"File {file.file_name} should not have updated timestamp"
+
+
+async def test_lru_eviction_on_attach(server, default_user, sarah_agent, default_source):
+ """Test that attaching files beyond max_files_open triggers LRU eviction."""
+ import time
+
+ # Use the agent's configured max_files_open
+ max_files_open = sarah_agent.max_files_open
+
+ # Create more files than the limit
+ files = []
+ for i in range(max_files_open + 2): # e.g., 7 files for max_files_open=5
+ file_metadata = PydanticFileMetadata(
+ file_name=f"lru_test_file_{i}.txt",
+ organization_id=default_user.organization_id,
+ source_id=default_source.id,
+ )
+ file = await server.file_manager.create_file(file_metadata=file_metadata, actor=default_user, text=f"test content {i}")
+ files.append(file)
+
+ # Attach files one by one with small delays to ensure different timestamps
+ attached_files = []
+ all_closed_files = []
+
+ for i, file in enumerate(files):
+ if i > 0:
+ time.sleep(0.1) # Small delay to ensure different timestamps
+
+ file_agent, closed_files = await server.file_agent_manager.attach_file(
+ agent_id=sarah_agent.id,
+ file_id=file.id,
+ file_name=file.file_name,
+ source_id=file.source_id,
+ actor=default_user,
+ visible_content=f"content for {file.file_name}",
+ max_files_open=sarah_agent.max_files_open,
+ )
+ attached_files.append(file_agent)
+ all_closed_files.extend(closed_files)
+
+ # Check that we never exceed max_files_open
+ open_files = await server.file_agent_manager.list_files_for_agent(
+ sarah_agent.id,
+ per_file_view_window_char_limit=sarah_agent.per_file_view_window_char_limit,
+ actor=default_user,
+ is_open_only=True,
+ )
+ assert len(open_files) <= max_files_open, f"Should never exceed {max_files_open} open files"
+
+ # Should have closed exactly 2 files (e.g., 7 - 5 = 2 for max_files_open=5)
+ expected_closed_count = len(files) - max_files_open
+ assert len(all_closed_files) == expected_closed_count, (
+ f"Should have closed {expected_closed_count} files, but closed: {all_closed_files}"
+ )
+
+ # Check that the oldest files were closed (first N files attached)
+ expected_closed = [files[i].file_name for i in range(expected_closed_count)]
+ assert set(all_closed_files) == set(expected_closed), f"Wrong files closed. Expected {expected_closed}, got {all_closed_files}"
+
+ # Check that exactly max_files_open files are open
+ open_files = await server.file_agent_manager.list_files_for_agent(
+ sarah_agent.id, per_file_view_window_char_limit=sarah_agent.per_file_view_window_char_limit, actor=default_user, is_open_only=True
+ )
+ assert len(open_files) == max_files_open
+
+ # Check that the most recently attached files are still open
+ open_file_names = {f.file_name for f in open_files}
+ expected_open = {files[i].file_name for i in range(expected_closed_count, len(files))} # last max_files_open files
+ assert open_file_names == expected_open
+
+
+async def test_lru_eviction_on_open_file(server, default_user, sarah_agent, default_source):
+ """Test that opening a file beyond max_files_open triggers LRU eviction."""
+ import time
+
+ max_files_open = sarah_agent.max_files_open
+
+ # Create files equal to the limit
+ files = []
+ for i in range(max_files_open + 1): # 6 files for max_files_open=5
+ file_metadata = PydanticFileMetadata(
+ file_name=f"open_test_file_{i}.txt",
+ organization_id=default_user.organization_id,
+ source_id=default_source.id,
+ )
+ file = await server.file_manager.create_file(file_metadata=file_metadata, actor=default_user, text=f"test content {i}")
+ files.append(file)
+
+ # Attach first max_files_open files
+ for i in range(max_files_open):
+ time.sleep(0.1) # Small delay for different timestamps
+ await server.file_agent_manager.attach_file(
+ agent_id=sarah_agent.id,
+ file_id=files[i].id,
+ file_name=files[i].file_name,
+ source_id=files[i].source_id,
+ actor=default_user,
+ visible_content=f"content for {files[i].file_name}",
+ max_files_open=sarah_agent.max_files_open,
+ )
+
+ # Attach the last file as closed
+ await server.file_agent_manager.attach_file(
+ agent_id=sarah_agent.id,
+ file_id=files[-1].id,
+ file_name=files[-1].file_name,
+ source_id=files[-1].source_id,
+ actor=default_user,
+ is_open=False,
+ visible_content=f"content for {files[-1].file_name}",
+ max_files_open=sarah_agent.max_files_open,
+ )
+
+ # All files should be attached but only max_files_open should be open
+ all_files = await server.file_agent_manager.list_files_for_agent(
+ sarah_agent.id, per_file_view_window_char_limit=sarah_agent.per_file_view_window_char_limit, actor=default_user
+ )
+ open_files = await server.file_agent_manager.list_files_for_agent(
+ sarah_agent.id, per_file_view_window_char_limit=sarah_agent.per_file_view_window_char_limit, actor=default_user, is_open_only=True
+ )
+ assert len(all_files) == max_files_open + 1
+ assert len(open_files) == max_files_open
+
+ # Wait a moment
+ time.sleep(0.1)
+
+ # Now "open" the last file using the efficient method
+ closed_files, was_already_open, _ = await server.file_agent_manager.enforce_max_open_files_and_open(
+ agent_id=sarah_agent.id,
+ file_id=files[-1].id,
+ file_name=files[-1].file_name,
+ source_id=files[-1].source_id,
+ actor=default_user,
+ visible_content="updated content",
+ max_files_open=sarah_agent.max_files_open,
+ )
+
+ # Should have closed 1 file (the oldest one)
+ assert len(closed_files) == 1, f"Should have closed 1 file, got: {closed_files}"
+ assert closed_files[0] == files[0].file_name, f"Should have closed oldest file {files[0].file_name}"
+
+ # Check that exactly max_files_open files are still open
+ open_files = await server.file_agent_manager.list_files_for_agent(
+ sarah_agent.id, per_file_view_window_char_limit=sarah_agent.per_file_view_window_char_limit, actor=default_user, is_open_only=True
+ )
+ assert len(open_files) == max_files_open
+
+ # Check that the newly opened file is open and the oldest is closed
+ last_file_agent = await server.file_agent_manager.get_file_agent_by_id(
+ agent_id=sarah_agent.id, file_id=files[-1].id, actor=default_user
+ )
+ first_file_agent = await server.file_agent_manager.get_file_agent_by_id(
+ agent_id=sarah_agent.id, file_id=files[0].id, actor=default_user
+ )
+
+ assert last_file_agent.is_open is True, "Last file should be open"
+ assert first_file_agent.is_open is False, "First file should be closed"
+
+
+async def test_lru_no_eviction_when_reopening_same_file(server, default_user, sarah_agent, default_source):
+ """Test that reopening an already open file doesn't trigger unnecessary eviction."""
+ import time
+
+ max_files_open = sarah_agent.max_files_open
+
+ # Create files equal to the limit
+ files = []
+ for i in range(max_files_open):
+ file_metadata = PydanticFileMetadata(
+ file_name=f"reopen_test_file_{i}.txt",
+ organization_id=default_user.organization_id,
+ source_id=default_source.id,
+ )
+ file = await server.file_manager.create_file(file_metadata=file_metadata, actor=default_user, text=f"test content {i}")
+ files.append(file)
+
+ # Attach all files (they'll be open)
+ for i, file in enumerate(files):
+ time.sleep(0.1) # Small delay for different timestamps
+ await server.file_agent_manager.attach_file(
+ agent_id=sarah_agent.id,
+ file_id=file.id,
+ file_name=file.file_name,
+ source_id=file.source_id,
+ actor=default_user,
+ visible_content=f"content for {file.file_name}",
+ max_files_open=sarah_agent.max_files_open,
+ )
+
+ # All files should be open
+ open_files = await server.file_agent_manager.list_files_for_agent(
+ sarah_agent.id, per_file_view_window_char_limit=sarah_agent.per_file_view_window_char_limit, actor=default_user, is_open_only=True
+ )
+ assert len(open_files) == max_files_open
+ initial_open_names = {f.file_name for f in open_files}
+
+ # Wait a moment
+ time.sleep(0.1)
+
+ # "Reopen" the last file (which is already open)
+ closed_files, was_already_open, _ = await server.file_agent_manager.enforce_max_open_files_and_open(
+ agent_id=sarah_agent.id,
+ file_id=files[-1].id,
+ file_name=files[-1].file_name,
+ source_id=files[-1].source_id,
+ actor=default_user,
+ visible_content="updated content",
+ max_files_open=sarah_agent.max_files_open,
+ )
+
+ # Should not have closed any files since we're within the limit
+ assert len(closed_files) == 0, f"Should not have closed any files when reopening, got: {closed_files}"
+ assert was_already_open is True, "File should have been detected as already open"
+
+ # All the same files should still be open
+ open_files = await server.file_agent_manager.list_files_for_agent(
+ sarah_agent.id, per_file_view_window_char_limit=sarah_agent.per_file_view_window_char_limit, actor=default_user, is_open_only=True
+ )
+ assert len(open_files) == max_files_open
+ final_open_names = {f.file_name for f in open_files}
+ assert initial_open_names == final_open_names, "Same files should remain open"
+
+
+async def test_last_accessed_at_updates_correctly(server, default_user, sarah_agent, default_source):
+ """Test that last_accessed_at is updated in the correct scenarios."""
+ import time
+
+ # Create and attach a file
+ file_metadata = PydanticFileMetadata(
+ file_name="timestamp_test.txt",
+ organization_id=default_user.organization_id,
+ source_id=default_source.id,
+ )
+ file = await server.file_manager.create_file(file_metadata=file_metadata, actor=default_user, text="test content")
+
+ file_agent, closed_files = await server.file_agent_manager.attach_file(
+ agent_id=sarah_agent.id,
+ file_id=file.id,
+ file_name=file.file_name,
+ source_id=file.source_id,
+ actor=default_user,
+ visible_content="initial content",
+ max_files_open=sarah_agent.max_files_open,
+ )
+
+ initial_time = file_agent.last_accessed_at
+ time.sleep(1.1)
+
+ # Test update_file_agent_by_id updates timestamp
+ updated_agent = await server.file_agent_manager.update_file_agent_by_id(
+ agent_id=sarah_agent.id, file_id=file.id, actor=default_user, visible_content="updated content"
+ )
+ assert updated_agent.last_accessed_at > initial_time, "update_file_agent_by_id should update timestamp"
+
+ time.sleep(1.1)
+ prev_time = updated_agent.last_accessed_at
+
+ # Test update_file_agent_by_name updates timestamp
+ updated_agent2 = await server.file_agent_manager.update_file_agent_by_name(
+ agent_id=sarah_agent.id, file_name=file.file_name, actor=default_user, is_open=False
+ )
+ assert updated_agent2.last_accessed_at > prev_time, "update_file_agent_by_name should update timestamp"
+
+ time.sleep(1.1)
+ prev_time = updated_agent2.last_accessed_at
+
+ # Test mark_access updates timestamp
+ await server.file_agent_manager.mark_access(agent_id=sarah_agent.id, file_id=file.id, actor=default_user)
+
+ final_agent = await server.file_agent_manager.get_file_agent_by_id(agent_id=sarah_agent.id, file_id=file.id, actor=default_user)
+ assert final_agent.last_accessed_at > prev_time, "mark_access should update timestamp"
+
+
+async def test_attach_files_bulk_basic(server, default_user, sarah_agent, default_source):
+ """Test basic functionality of attach_files_bulk method."""
+ # Create multiple files
+ files = []
+ for i in range(3):
+ file_metadata = PydanticFileMetadata(
+ file_name=f"bulk_test_{i}.txt",
+ organization_id=default_user.organization_id,
+ source_id=default_source.id,
+ )
+ file = await server.file_manager.create_file(file_metadata=file_metadata, actor=default_user, text=f"content {i}")
+ files.append(file)
+
+ # Create visible content map
+ visible_content_map = {f"bulk_test_{i}.txt": f"visible content {i}" for i in range(3)}
+
+ # Bulk attach files
+ closed_files = await server.file_agent_manager.attach_files_bulk(
+ agent_id=sarah_agent.id,
+ files_metadata=files,
+ visible_content_map=visible_content_map,
+ actor=default_user,
+ max_files_open=sarah_agent.max_files_open,
+ )
+
+ # Should not close any files since we're under the limit
+ assert closed_files == []
+
+ # Verify all files are attached and open
+ attached_files = await server.file_agent_manager.list_files_for_agent(
+ sarah_agent.id, per_file_view_window_char_limit=sarah_agent.per_file_view_window_char_limit, actor=default_user, is_open_only=True
+ )
+ assert len(attached_files) == 3
+
+ attached_file_names = {f.file_name for f in attached_files}
+ expected_names = {f"bulk_test_{i}.txt" for i in range(3)}
+ assert attached_file_names == expected_names
+
+ # Verify visible content is set correctly
+ for i, attached_file in enumerate(attached_files):
+ if attached_file.file_name == f"bulk_test_{i}.txt":
+ assert attached_file.visible_content == f"visible content {i}"
+
+
+async def test_attach_files_bulk_deduplication(server, default_user, sarah_agent, default_source):
+ """Test that attach_files_bulk properly deduplicates files with same names."""
+ # Create files with same name (different IDs)
+ file_metadata_1 = PydanticFileMetadata(
+ file_name="duplicate_test.txt",
+ organization_id=default_user.organization_id,
+ source_id=default_source.id,
+ )
+ file1 = await server.file_manager.create_file(file_metadata=file_metadata_1, actor=default_user, text="content 1")
+
+ file_metadata_2 = PydanticFileMetadata(
+ file_name="duplicate_test.txt",
+ organization_id=default_user.organization_id,
+ source_id=default_source.id,
+ )
+ file2 = await server.file_manager.create_file(file_metadata=file_metadata_2, actor=default_user, text="content 2")
+
+ # Try to attach both files (same name, different IDs)
+ files_to_attach = [file1, file2]
+ visible_content_map = {"duplicate_test.txt": "visible content"}
+
+ # Bulk attach should deduplicate
+ closed_files = await server.file_agent_manager.attach_files_bulk(
+ agent_id=sarah_agent.id,
+ files_metadata=files_to_attach,
+ visible_content_map=visible_content_map,
+ actor=default_user,
+ max_files_open=sarah_agent.max_files_open,
+ )
+
+ # Should only attach one file (deduplicated)
+ attached_files = await server.file_agent_manager.list_files_for_agent(
+ sarah_agent.id, per_file_view_window_char_limit=sarah_agent.per_file_view_window_char_limit, actor=default_user
+ )
+ assert len(attached_files) == 1
+ assert attached_files[0].file_name == "duplicate_test.txt"
+
+
+async def test_attach_files_bulk_lru_eviction(server, default_user, sarah_agent, default_source):
+ """Test that attach_files_bulk properly handles LRU eviction without duplicates."""
+ import time
+
+ max_files_open = sarah_agent.max_files_open
+
+ # First, fill up to the max with individual files
+ existing_files = []
+ for i in range(max_files_open):
+ file_metadata = PydanticFileMetadata(
+ file_name=f"existing_{i}.txt",
+ organization_id=default_user.organization_id,
+ source_id=default_source.id,
+ )
+ file = await server.file_manager.create_file(file_metadata=file_metadata, actor=default_user, text=f"existing {i}")
+ existing_files.append(file)
+
+ time.sleep(0.05) # Small delay for different timestamps
+ await server.file_agent_manager.attach_file(
+ agent_id=sarah_agent.id,
+ file_id=file.id,
+ file_name=file.file_name,
+ source_id=file.source_id,
+ actor=default_user,
+ visible_content=f"existing content {i}",
+ max_files_open=sarah_agent.max_files_open,
+ )
+
+ # Verify we're at the limit
+ open_files = await server.file_agent_manager.list_files_for_agent(
+ sarah_agent.id, per_file_view_window_char_limit=sarah_agent.per_file_view_window_char_limit, actor=default_user, is_open_only=True
+ )
+ assert len(open_files) == max_files_open
+
+ # Now bulk attach 3 new files (should trigger LRU eviction)
+ new_files = []
+ for i in range(3):
+ file_metadata = PydanticFileMetadata(
+ file_name=f"new_bulk_{i}.txt",
+ organization_id=default_user.organization_id,
+ source_id=default_source.id,
+ )
+ file = await server.file_manager.create_file(file_metadata=file_metadata, actor=default_user, text=f"new content {i}")
+ new_files.append(file)
+
+ visible_content_map = {f"new_bulk_{i}.txt": f"new visible {i}" for i in range(3)}
+
+ # Bulk attach should evict oldest files
+ closed_files = await server.file_agent_manager.attach_files_bulk(
+ agent_id=sarah_agent.id,
+ files_metadata=new_files,
+ visible_content_map=visible_content_map,
+ actor=default_user,
+ max_files_open=sarah_agent.max_files_open,
+ )
+
+ # Should have closed exactly 3 files (oldest ones)
+ assert len(closed_files) == 3
+
+ # CRITICAL: Verify no duplicates in closed_files list
+ assert len(closed_files) == len(set(closed_files)), f"Duplicate file names in closed_files: {closed_files}"
+
+ # Verify expected files were closed (oldest 3)
+ expected_closed = {f"existing_{i}.txt" for i in range(3)}
+ actual_closed = set(closed_files)
+ assert actual_closed == expected_closed
+
+ # Verify we still have exactly max_files_open files open
+ open_files_after = await server.file_agent_manager.list_files_for_agent(
+ sarah_agent.id, per_file_view_window_char_limit=sarah_agent.per_file_view_window_char_limit, actor=default_user, is_open_only=True
+ )
+ assert len(open_files_after) == max_files_open
+
+ # Verify the new files are open
+ open_file_names = {f.file_name for f in open_files_after}
+ for i in range(3):
+ assert f"new_bulk_{i}.txt" in open_file_names
+
+
+async def test_attach_files_bulk_mixed_existing_new(server, default_user, sarah_agent, default_source):
+ """Test bulk attach with mix of existing and new files."""
+ # Create and attach one file individually first
+ existing_file_metadata = PydanticFileMetadata(
+ file_name="existing_file.txt",
+ organization_id=default_user.organization_id,
+ source_id=default_source.id,
+ )
+ existing_file = await server.file_manager.create_file(file_metadata=existing_file_metadata, actor=default_user, text="existing")
+
+ await server.file_agent_manager.attach_file(
+ agent_id=sarah_agent.id,
+ file_id=existing_file.id,
+ file_name=existing_file.file_name,
+ source_id=existing_file.source_id,
+ actor=default_user,
+ visible_content="old content",
+ is_open=False, # Start as closed
+ max_files_open=sarah_agent.max_files_open,
+ )
+
+ # Create new files
+ new_files = []
+ for i in range(2):
+ file_metadata = PydanticFileMetadata(
+ file_name=f"new_file_{i}.txt",
+ organization_id=default_user.organization_id,
+ source_id=default_source.id,
+ )
+ file = await server.file_manager.create_file(file_metadata=file_metadata, actor=default_user, text=f"new {i}")
+ new_files.append(file)
+
+ # Bulk attach: existing file + new files
+ files_to_attach = [existing_file] + new_files
+ visible_content_map = {
+ "existing_file.txt": "updated content",
+ "new_file_0.txt": "new content 0",
+ "new_file_1.txt": "new content 1",
+ }
+
+ closed_files = await server.file_agent_manager.attach_files_bulk(
+ agent_id=sarah_agent.id,
+ files_metadata=files_to_attach,
+ visible_content_map=visible_content_map,
+ actor=default_user,
+ max_files_open=sarah_agent.max_files_open,
+ )
+
+ # Should not close any files
+ assert closed_files == []
+
+ # Verify all files are now open
+ open_files = await server.file_agent_manager.list_files_for_agent(
+ sarah_agent.id, per_file_view_window_char_limit=sarah_agent.per_file_view_window_char_limit, actor=default_user, is_open_only=True
+ )
+ assert len(open_files) == 3
+
+ # Verify existing file was updated
+ existing_file_agent = await server.file_agent_manager.get_file_agent_by_file_name(
+ agent_id=sarah_agent.id, file_name="existing_file.txt", actor=default_user
+ )
+ assert existing_file_agent.is_open is True
+ assert existing_file_agent.visible_content == "updated content"
+
+
+async def test_attach_files_bulk_empty_list(server, default_user, sarah_agent):
+ """Test attach_files_bulk with empty file list."""
+ closed_files = await server.file_agent_manager.attach_files_bulk(
+ agent_id=sarah_agent.id, files_metadata=[], visible_content_map={}, actor=default_user, max_files_open=sarah_agent.max_files_open
+ )
+
+ assert closed_files == []
+
+ # Verify no files are attached
+ attached_files = await server.file_agent_manager.list_files_for_agent(
+ sarah_agent.id, per_file_view_window_char_limit=sarah_agent.per_file_view_window_char_limit, actor=default_user
+ )
+ assert len(attached_files) == 0
+
+
+async def test_attach_files_bulk_oversized_bulk(server, default_user, sarah_agent, default_source):
+ """Test bulk attach when trying to attach more files than max_files_open allows."""
+ max_files_open = sarah_agent.max_files_open
+
+ # Create more files than the limit allows
+ oversized_files = []
+ for i in range(max_files_open + 3): # 3 more than limit
+ file_metadata = PydanticFileMetadata(
+ file_name=f"oversized_{i}.txt",
+ organization_id=default_user.organization_id,
+ source_id=default_source.id,
+ )
+ file = await server.file_manager.create_file(file_metadata=file_metadata, actor=default_user, text=f"oversized {i}")
+ oversized_files.append(file)
+
+ visible_content_map = {f"oversized_{i}.txt": f"oversized visible {i}" for i in range(max_files_open + 3)}
+
+ # Bulk attach all files (more than limit)
+ closed_files = await server.file_agent_manager.attach_files_bulk(
+ agent_id=sarah_agent.id,
+ files_metadata=oversized_files,
+ visible_content_map=visible_content_map,
+ actor=default_user,
+ max_files_open=sarah_agent.max_files_open,
+ )
+
+ # Should have closed exactly 3 files (the excess)
+ assert len(closed_files) == 3
+
+ # CRITICAL: Verify no duplicates in closed_files list
+ assert len(closed_files) == len(set(closed_files)), f"Duplicate file names in closed_files: {closed_files}"
+
+ # Should have exactly max_files_open files open
+ open_files_after = await server.file_agent_manager.list_files_for_agent(
+ sarah_agent.id, per_file_view_window_char_limit=sarah_agent.per_file_view_window_char_limit, actor=default_user, is_open_only=True
+ )
+ assert len(open_files_after) == max_files_open
+
+ # All files should be attached (some open, some closed)
+ all_files_after = await server.file_agent_manager.list_files_for_agent(
+ sarah_agent.id, per_file_view_window_char_limit=sarah_agent.per_file_view_window_char_limit, actor=default_user
+ )
+ assert len(all_files_after) == max_files_open + 3
diff --git a/tests/managers/test_group_manager.py b/tests/managers/test_group_manager.py
new file mode 100644
index 000000000..08bf3d608
--- /dev/null
+++ b/tests/managers/test_group_manager.py
@@ -0,0 +1,175 @@
+import json
+import logging
+import os
+import random
+import re
+import string
+import time
+import uuid
+from datetime import datetime, timedelta, timezone
+from typing import List
+from unittest.mock import AsyncMock, Mock, patch
+
+import pytest
+from _pytest.python_api import approx
+from anthropic.types.beta import BetaMessage
+from anthropic.types.beta.messages import BetaMessageBatchIndividualResponse, BetaMessageBatchSucceededResult
+
+# Import shared fixtures and constants from conftest
+from conftest import (
+ CREATE_DELAY_SQLITE,
+ DEFAULT_EMBEDDING_CONFIG,
+ USING_SQLITE,
+)
+from openai.types.chat.chat_completion_message_tool_call import ChatCompletionMessageToolCall as OpenAIToolCall, Function as OpenAIFunction
+from sqlalchemy import func, select
+from sqlalchemy.exc import IntegrityError, InvalidRequestError
+from sqlalchemy.orm.exc import StaleDataError
+
+from letta.config import LettaConfig
+from letta.constants import (
+ BASE_MEMORY_TOOLS,
+ BASE_SLEEPTIME_TOOLS,
+ BASE_TOOLS,
+ BASE_VOICE_SLEEPTIME_CHAT_TOOLS,
+ BASE_VOICE_SLEEPTIME_TOOLS,
+ BUILTIN_TOOLS,
+ DEFAULT_ORG_ID,
+ DEFAULT_ORG_NAME,
+ FILES_TOOLS,
+ LETTA_TOOL_EXECUTION_DIR,
+ LETTA_TOOL_SET,
+ LOCAL_ONLY_MULTI_AGENT_TOOLS,
+ MCP_TOOL_TAG_NAME_PREFIX,
+ MULTI_AGENT_TOOLS,
+)
+from letta.data_sources.redis_client import NoopAsyncRedisClient, get_redis_client
+from letta.errors import LettaAgentNotFoundError
+from letta.functions.functions import derive_openai_json_schema, parse_source_code
+from letta.functions.mcp_client.types import MCPTool
+from letta.helpers import ToolRulesSolver
+from letta.helpers.datetime_helpers import AsyncTimer
+from letta.jobs.types import ItemUpdateInfo, RequestStatusUpdateInfo, StepStatusUpdateInfo
+from letta.orm import Base, Block
+from letta.orm.block_history import BlockHistory
+from letta.orm.errors import NoResultFound, UniqueConstraintViolationError
+from letta.orm.file import FileContent as FileContentModel, FileMetadata as FileMetadataModel
+from letta.schemas.agent import CreateAgent, UpdateAgent
+from letta.schemas.block import Block as PydanticBlock, BlockUpdate, CreateBlock
+from letta.schemas.embedding_config import EmbeddingConfig
+from letta.schemas.enums import (
+ ActorType,
+ AgentStepStatus,
+ FileProcessingStatus,
+ JobStatus,
+ JobType,
+ MessageRole,
+ ProviderType,
+ SandboxType,
+ StepStatus,
+ TagMatchMode,
+ ToolType,
+ VectorDBProvider,
+)
+from letta.schemas.environment_variables import SandboxEnvironmentVariableCreate, SandboxEnvironmentVariableUpdate
+from letta.schemas.file import FileMetadata, FileMetadata as PydanticFileMetadata
+from letta.schemas.identity import IdentityCreate, IdentityProperty, IdentityPropertyType, IdentityType, IdentityUpdate, IdentityUpsert
+from letta.schemas.job import BatchJob, Job, Job as PydanticJob, JobUpdate, LettaRequestConfig
+from letta.schemas.letta_message import UpdateAssistantMessage, UpdateReasoningMessage, UpdateSystemMessage, UpdateUserMessage
+from letta.schemas.letta_message_content import TextContent
+from letta.schemas.letta_stop_reason import LettaStopReason, StopReasonType
+from letta.schemas.llm_batch_job import AgentStepState, LLMBatchItem
+from letta.schemas.llm_config import LLMConfig
+from letta.schemas.message import Message as PydanticMessage, MessageCreate, MessageUpdate
+from letta.schemas.openai.chat_completion_response import UsageStatistics
+from letta.schemas.organization import Organization, Organization as PydanticOrganization, OrganizationUpdate
+from letta.schemas.passage import Passage as PydanticPassage
+from letta.schemas.pip_requirement import PipRequirement
+from letta.schemas.run import Run as PydanticRun
+from letta.schemas.sandbox_config import E2BSandboxConfig, LocalSandboxConfig, SandboxConfigCreate, SandboxConfigUpdate
+from letta.schemas.source import Source as PydanticSource, SourceUpdate
+from letta.schemas.tool import Tool as PydanticTool, ToolCreate, ToolUpdate
+from letta.schemas.tool_rule import InitToolRule
+from letta.schemas.user import User as PydanticUser, UserUpdate
+from letta.server.db import db_registry
+from letta.server.server import SyncServer
+from letta.services.block_manager import BlockManager
+from letta.services.helpers.agent_manager_helper import calculate_base_tools, calculate_multi_agent_tools, validate_agent_exists_async
+from letta.services.step_manager import FeedbackType
+from letta.settings import settings, tool_settings
+from letta.utils import calculate_file_defaults_based_on_context_window
+from tests.helpers.utils import comprehensive_agent_checks, validate_context_window_overview
+from tests.utils import random_string
+
+
+async def test_create_internal_template_objects(server: SyncServer, default_user):
+ """Test creating agents, groups, and blocks with template-related fields."""
+ from letta.schemas.agent import InternalTemplateAgentCreate
+ from letta.schemas.block import Block, InternalTemplateBlockCreate
+ from letta.schemas.group import InternalTemplateGroupCreate, RoundRobinManager
+
+ base_template_id = "base_123"
+ template_id = "template_456"
+ deployment_id = "deploy_789"
+ entity_id = "entity_012"
+
+ # Create agent with template fields (use sarah_agent as base, then create new one)
+ agent = await server.agent_manager.create_agent_async(
+ InternalTemplateAgentCreate(
+ name="template-agent",
+ base_template_id=base_template_id,
+ template_id=template_id,
+ deployment_id=deployment_id,
+ entity_id=entity_id,
+ llm_config=LLMConfig.default_config("gpt-4o-mini"),
+ embedding_config=EmbeddingConfig.default_config(provider="openai"),
+ include_base_tools=False,
+ ),
+ actor=default_user,
+ )
+ # Verify agent template fields
+ assert agent.base_template_id == base_template_id
+ assert agent.template_id == template_id
+ assert agent.deployment_id == deployment_id
+ assert agent.entity_id == entity_id
+
+ # Create block with template fields
+ block_create = InternalTemplateBlockCreate(
+ label="template_block",
+ value="Test block",
+ base_template_id=base_template_id,
+ template_id=template_id,
+ deployment_id=deployment_id,
+ entity_id=entity_id,
+ )
+ block = await server.block_manager.create_or_update_block_async(Block(**block_create.model_dump()), actor=default_user)
+ # Verify block template fields
+ assert block.base_template_id == base_template_id
+ assert block.template_id == template_id
+ assert block.deployment_id == deployment_id
+ assert block.entity_id == entity_id
+
+ # Create group with template fields (no entity_id for groups)
+ group = await server.group_manager.create_group_async(
+ InternalTemplateGroupCreate(
+ agent_ids=[agent.id],
+ description="Template group",
+ base_template_id=base_template_id,
+ template_id=template_id,
+ deployment_id=deployment_id,
+ manager_config=RoundRobinManager(),
+ ),
+ actor=default_user,
+ )
+ # Verify group template fields and basic functionality
+ assert group.description == "Template group"
+ assert agent.id in group.agent_ids
+ assert group.base_template_id == base_template_id
+ assert group.template_id == template_id
+ assert group.deployment_id == deployment_id
+
+ # Clean up
+ await server.group_manager.delete_group_async(group.id, actor=default_user)
+ await server.block_manager.delete_block_async(block.id, actor=default_user)
+ await server.agent_manager.delete_agent_async(agent.id, actor=default_user)
diff --git a/tests/managers/test_identity_manager.py b/tests/managers/test_identity_manager.py
new file mode 100644
index 000000000..44a70beb6
--- /dev/null
+++ b/tests/managers/test_identity_manager.py
@@ -0,0 +1,425 @@
+import json
+import logging
+import os
+import random
+import re
+import string
+import time
+import uuid
+from datetime import datetime, timedelta, timezone
+from typing import List
+from unittest.mock import AsyncMock, Mock, patch
+
+import pytest
+from _pytest.python_api import approx
+from anthropic.types.beta import BetaMessage
+from anthropic.types.beta.messages import BetaMessageBatchIndividualResponse, BetaMessageBatchSucceededResult
+
+# Import shared fixtures and constants from conftest
+from conftest import (
+ CREATE_DELAY_SQLITE,
+ DEFAULT_EMBEDDING_CONFIG,
+ USING_SQLITE,
+)
+from openai.types.chat.chat_completion_message_tool_call import ChatCompletionMessageToolCall as OpenAIToolCall, Function as OpenAIFunction
+from sqlalchemy import func, select
+from sqlalchemy.exc import IntegrityError, InvalidRequestError
+from sqlalchemy.orm.exc import StaleDataError
+
+from letta.config import LettaConfig
+from letta.constants import (
+ BASE_MEMORY_TOOLS,
+ BASE_SLEEPTIME_TOOLS,
+ BASE_TOOLS,
+ BASE_VOICE_SLEEPTIME_CHAT_TOOLS,
+ BASE_VOICE_SLEEPTIME_TOOLS,
+ BUILTIN_TOOLS,
+ DEFAULT_ORG_ID,
+ DEFAULT_ORG_NAME,
+ FILES_TOOLS,
+ LETTA_TOOL_EXECUTION_DIR,
+ LETTA_TOOL_SET,
+ LOCAL_ONLY_MULTI_AGENT_TOOLS,
+ MCP_TOOL_TAG_NAME_PREFIX,
+ MULTI_AGENT_TOOLS,
+)
+from letta.data_sources.redis_client import NoopAsyncRedisClient, get_redis_client
+from letta.errors import LettaAgentNotFoundError
+from letta.functions.functions import derive_openai_json_schema, parse_source_code
+from letta.functions.mcp_client.types import MCPTool
+from letta.helpers import ToolRulesSolver
+from letta.helpers.datetime_helpers import AsyncTimer
+from letta.jobs.types import ItemUpdateInfo, RequestStatusUpdateInfo, StepStatusUpdateInfo
+from letta.orm import Base, Block
+from letta.orm.block_history import BlockHistory
+from letta.orm.errors import NoResultFound, UniqueConstraintViolationError
+from letta.orm.file import FileContent as FileContentModel, FileMetadata as FileMetadataModel
+from letta.schemas.agent import CreateAgent, UpdateAgent
+from letta.schemas.block import Block as PydanticBlock, BlockUpdate, CreateBlock
+from letta.schemas.embedding_config import EmbeddingConfig
+from letta.schemas.enums import (
+ ActorType,
+ AgentStepStatus,
+ FileProcessingStatus,
+ JobStatus,
+ JobType,
+ MessageRole,
+ ProviderType,
+ SandboxType,
+ StepStatus,
+ TagMatchMode,
+ ToolType,
+ VectorDBProvider,
+)
+from letta.schemas.environment_variables import SandboxEnvironmentVariableCreate, SandboxEnvironmentVariableUpdate
+from letta.schemas.file import FileMetadata, FileMetadata as PydanticFileMetadata
+from letta.schemas.identity import IdentityCreate, IdentityProperty, IdentityPropertyType, IdentityType, IdentityUpdate, IdentityUpsert
+from letta.schemas.job import BatchJob, Job, Job as PydanticJob, JobUpdate, LettaRequestConfig
+from letta.schemas.letta_message import UpdateAssistantMessage, UpdateReasoningMessage, UpdateSystemMessage, UpdateUserMessage
+from letta.schemas.letta_message_content import TextContent
+from letta.schemas.letta_stop_reason import LettaStopReason, StopReasonType
+from letta.schemas.llm_batch_job import AgentStepState, LLMBatchItem
+from letta.schemas.llm_config import LLMConfig
+from letta.schemas.message import Message as PydanticMessage, MessageCreate, MessageUpdate
+from letta.schemas.openai.chat_completion_response import UsageStatistics
+from letta.schemas.organization import Organization, Organization as PydanticOrganization, OrganizationUpdate
+from letta.schemas.passage import Passage as PydanticPassage
+from letta.schemas.pip_requirement import PipRequirement
+from letta.schemas.run import Run as PydanticRun
+from letta.schemas.sandbox_config import E2BSandboxConfig, LocalSandboxConfig, SandboxConfigCreate, SandboxConfigUpdate
+from letta.schemas.source import Source as PydanticSource, SourceUpdate
+from letta.schemas.tool import Tool as PydanticTool, ToolCreate, ToolUpdate
+from letta.schemas.tool_rule import InitToolRule
+from letta.schemas.user import User as PydanticUser, UserUpdate
+from letta.server.db import db_registry
+from letta.server.server import SyncServer
+from letta.services.block_manager import BlockManager
+from letta.services.helpers.agent_manager_helper import calculate_base_tools, calculate_multi_agent_tools, validate_agent_exists_async
+from letta.services.step_manager import FeedbackType
+from letta.settings import settings, tool_settings
+from letta.utils import calculate_file_defaults_based_on_context_window
+from tests.helpers.utils import comprehensive_agent_checks, validate_context_window_overview
+from tests.utils import random_string
+
+# ======================================================================================================================
+# Identity Manager Tests
+# ======================================================================================================================
+
+
+async def test_create_and_upsert_identity(server: SyncServer, default_user):
+ identity_create = IdentityCreate(
+ identifier_key="1234",
+ name="caren",
+ identity_type=IdentityType.user,
+ properties=[
+ IdentityProperty(key="email", value="[email protected]", type=IdentityPropertyType.string),
+ IdentityProperty(key="age", value=28, type=IdentityPropertyType.number),
+ ],
+ )
+
+ identity = await server.identity_manager.create_identity_async(identity_create, actor=default_user)
+
+ # Assertions to ensure the created identity matches the expected values
+ assert identity.identifier_key == identity_create.identifier_key
+ assert identity.name == identity_create.name
+ assert identity.identity_type == identity_create.identity_type
+ assert identity.properties == identity_create.properties
+ assert identity.agent_ids == []
+ assert identity.project_id is None
+
+ with pytest.raises(UniqueConstraintViolationError):
+ await server.identity_manager.create_identity_async(
+ IdentityCreate(identifier_key="1234", name="sarah", identity_type=IdentityType.user),
+ actor=default_user,
+ )
+
+ identity_create.properties = [IdentityProperty(key="age", value=29, type=IdentityPropertyType.number)]
+
+ identity = await server.identity_manager.upsert_identity_async(
+ identity=IdentityUpsert(**identity_create.model_dump()), actor=default_user
+ )
+
+ identity = await server.identity_manager.get_identity_async(identity_id=identity.id, actor=default_user)
+ assert len(identity.properties) == 1
+ assert identity.properties[0].key == "age"
+ assert identity.properties[0].value == 29
+
+ await server.identity_manager.delete_identity_async(identity_id=identity.id, actor=default_user)
+
+
+async def test_get_identities(server, default_user):
+ # Create identities to retrieve later
+ user = await server.identity_manager.create_identity_async(
+ IdentityCreate(name="caren", identifier_key="1234", identity_type=IdentityType.user), actor=default_user
+ )
+ org = await server.identity_manager.create_identity_async(
+ IdentityCreate(name="letta", identifier_key="0001", identity_type=IdentityType.org), actor=default_user
+ )
+
+ # Retrieve identities by different filters
+ all_identities = await server.identity_manager.list_identities_async(actor=default_user)
+ assert len(all_identities) == 2
+
+ user_identities = await server.identity_manager.list_identities_async(actor=default_user, identity_type=IdentityType.user)
+ assert len(user_identities) == 1
+ assert user_identities[0].name == user.name
+
+ org_identities = await server.identity_manager.list_identities_async(actor=default_user, identity_type=IdentityType.org)
+ assert len(org_identities) == 1
+ assert org_identities[0].name == org.name
+
+ await server.identity_manager.delete_identity_async(identity_id=user.id, actor=default_user)
+ await server.identity_manager.delete_identity_async(identity_id=org.id, actor=default_user)
+
+
+async def test_update_identity(server: SyncServer, sarah_agent, charles_agent, default_user):
+ identity = await server.identity_manager.create_identity_async(
+ IdentityCreate(name="caren", identifier_key="1234", identity_type=IdentityType.user), actor=default_user
+ )
+
+ # Update identity fields
+ update_data = IdentityUpdate(
+ agent_ids=[sarah_agent.id, charles_agent.id],
+ properties=[IdentityProperty(key="email", value="[email protected]", type=IdentityPropertyType.string)],
+ )
+ await server.identity_manager.update_identity_async(identity_id=identity.id, identity=update_data, actor=default_user)
+
+ # Retrieve the updated identity
+ updated_identity = await server.identity_manager.get_identity_async(identity_id=identity.id, actor=default_user)
+
+ # Assertions to verify the update
+ assert updated_identity.agent_ids.sort() == update_data.agent_ids.sort()
+ assert updated_identity.properties == update_data.properties
+
+ agent_state = await server.agent_manager.get_agent_by_id_async(agent_id=sarah_agent.id, actor=default_user)
+ assert identity.id in agent_state.identity_ids
+ agent_state = await server.agent_manager.get_agent_by_id_async(agent_id=charles_agent.id, actor=default_user)
+ assert identity.id in agent_state.identity_ids
+
+ await server.identity_manager.delete_identity_async(identity_id=identity.id, actor=default_user)
+
+
+async def test_attach_detach_identity_from_agent(server: SyncServer, sarah_agent, default_user):
+ # Create an identity
+ identity = await server.identity_manager.create_identity_async(
+ IdentityCreate(name="caren", identifier_key="1234", identity_type=IdentityType.user), actor=default_user
+ )
+ agent_state = await server.agent_manager.update_agent_async(
+ agent_id=sarah_agent.id, agent_update=UpdateAgent(identity_ids=[identity.id]), actor=default_user
+ )
+
+ # Check that identity has been attached
+ assert identity.id in agent_state.identity_ids
+
+ # Now attempt to delete the identity
+ await server.identity_manager.delete_identity_async(identity_id=identity.id, actor=default_user)
+
+ # Verify that the identity was deleted
+ identities = await server.identity_manager.list_identities_async(actor=default_user)
+ assert len(identities) == 0
+
+ # Check that block has been detached too
+ agent_state = await server.agent_manager.get_agent_by_id_async(agent_id=sarah_agent.id, actor=default_user)
+ assert identity.id not in agent_state.identity_ids
+
+
+async def test_get_set_agents_for_identities(server: SyncServer, sarah_agent, charles_agent, default_user):
+ identity = await server.identity_manager.create_identity_async(
+ IdentityCreate(name="caren", identifier_key="1234", identity_type=IdentityType.user, agent_ids=[sarah_agent.id, charles_agent.id]),
+ actor=default_user,
+ )
+
+ agent_with_identity = await server.create_agent_async(
+ CreateAgent(
+ memory_blocks=[],
+ llm_config=LLMConfig.default_config("gpt-4o-mini"),
+ embedding_config=EmbeddingConfig.default_config(provider="openai"),
+ identity_ids=[identity.id],
+ include_base_tools=False,
+ ),
+ actor=default_user,
+ )
+ agent_without_identity = await server.create_agent_async(
+ CreateAgent(
+ memory_blocks=[],
+ llm_config=LLMConfig.default_config("gpt-4o-mini"),
+ embedding_config=EmbeddingConfig.default_config(provider="openai"),
+ include_base_tools=False,
+ ),
+ actor=default_user,
+ )
+
+ # Get the agents for identity id
+ agent_states = await server.agent_manager.list_agents_async(identity_id=identity.id, actor=default_user)
+ assert len(agent_states) == 3
+
+ # Check all agents are in the list
+ agent_state_ids = [a.id for a in agent_states]
+ assert sarah_agent.id in agent_state_ids
+ assert charles_agent.id in agent_state_ids
+ assert agent_with_identity.id in agent_state_ids
+ assert agent_without_identity.id not in agent_state_ids
+
+ # Get the agents for identifier key
+ agent_states = await server.agent_manager.list_agents_async(identifier_keys=[identity.identifier_key], actor=default_user)
+ assert len(agent_states) == 3
+
+ # Check all agents are in the list
+ agent_state_ids = [a.id for a in agent_states]
+ assert sarah_agent.id in agent_state_ids
+ assert charles_agent.id in agent_state_ids
+ assert agent_with_identity.id in agent_state_ids
+ assert agent_without_identity.id not in agent_state_ids
+
+ # Delete new agents
+ await server.agent_manager.delete_agent_async(agent_id=agent_with_identity.id, actor=default_user)
+ await server.agent_manager.delete_agent_async(agent_id=agent_without_identity.id, actor=default_user)
+
+ # Get the agents for identity id
+ agent_states = await server.agent_manager.list_agents_async(identity_id=identity.id, actor=default_user)
+ assert len(agent_states) == 2
+
+ # Check only initial agents are in the list
+ agent_state_ids = [a.id for a in agent_states]
+ assert sarah_agent.id in agent_state_ids
+ assert charles_agent.id in agent_state_ids
+
+ await server.identity_manager.delete_identity_async(identity_id=identity.id, actor=default_user)
+
+
+async def test_upsert_properties(server: SyncServer, default_user):
+ identity_create = IdentityCreate(
+ identifier_key="1234",
+ name="caren",
+ identity_type=IdentityType.user,
+ properties=[
+ IdentityProperty(key="email", value="[email protected]", type=IdentityPropertyType.string),
+ IdentityProperty(key="age", value=28, type=IdentityPropertyType.number),
+ ],
+ )
+
+ identity = await server.identity_manager.create_identity_async(identity_create, actor=default_user)
+ properties = [
+ IdentityProperty(key="email", value="[email protected]", type=IdentityPropertyType.string),
+ IdentityProperty(key="age", value="28", type=IdentityPropertyType.string),
+ IdentityProperty(key="test", value=123, type=IdentityPropertyType.number),
+ ]
+
+ updated_identity = await server.identity_manager.upsert_identity_properties_async(
+ identity_id=identity.id,
+ properties=properties,
+ actor=default_user,
+ )
+ assert updated_identity.properties == properties
+
+ await server.identity_manager.delete_identity_async(identity_id=identity.id, actor=default_user)
+
+
+async def test_attach_detach_identity_from_block(server: SyncServer, default_block, default_user):
+ # Create an identity
+ identity = await server.identity_manager.create_identity_async(
+ IdentityCreate(name="caren", identifier_key="1234", identity_type=IdentityType.user, block_ids=[default_block.id]),
+ actor=default_user,
+ )
+
+ # Check that identity has been attached
+ blocks = await server.block_manager.get_blocks_async(identity_id=identity.id, actor=default_user)
+ assert len(blocks) == 1 and blocks[0].id == default_block.id
+
+ # Now attempt to delete the identity
+ await server.identity_manager.delete_identity_async(identity_id=identity.id, actor=default_user)
+
+ # Verify that the identity was deleted
+ identities = await server.identity_manager.list_identities_async(actor=default_user)
+ assert len(identities) == 0
+
+ # Check that block has been detached too
+ blocks = await server.block_manager.get_blocks_async(identity_id=identity.id, actor=default_user)
+ assert len(blocks) == 0
+
+
+async def test_get_set_blocks_for_identities(server: SyncServer, default_block, default_user):
+ block_manager = BlockManager()
+ block_with_identity = await block_manager.create_or_update_block_async(
+ PydanticBlock(label="persona", value="Original Content"), actor=default_user
+ )
+ block_without_identity = await block_manager.create_or_update_block_async(
+ PydanticBlock(label="user", value="Original Content"), actor=default_user
+ )
+ identity = await server.identity_manager.create_identity_async(
+ IdentityCreate(
+ name="caren", identifier_key="1234", identity_type=IdentityType.user, block_ids=[default_block.id, block_with_identity.id]
+ ),
+ actor=default_user,
+ )
+
+ # Get the blocks for identity id
+ blocks = await server.block_manager.get_blocks_async(identity_id=identity.id, actor=default_user)
+ assert len(blocks) == 2
+
+ # Check blocks are in the list
+ block_ids = [b.id for b in blocks]
+ assert default_block.id in block_ids
+ assert block_with_identity.id in block_ids
+ assert block_without_identity.id not in block_ids
+
+ # Get the blocks for identifier key
+ blocks = await server.block_manager.get_blocks_async(identifier_keys=[identity.identifier_key], actor=default_user)
+ assert len(blocks) == 2
+
+ # Check blocks are in the list
+ block_ids = [b.id for b in blocks]
+ assert default_block.id in block_ids
+ assert block_with_identity.id in block_ids
+ assert block_without_identity.id not in block_ids
+
+ # Delete new agents
+ await server.block_manager.delete_block_async(block_id=block_with_identity.id, actor=default_user)
+ await server.block_manager.delete_block_async(block_id=block_without_identity.id, actor=default_user)
+
+ # Get the blocks for identity id
+ blocks = await server.block_manager.get_blocks_async(identity_id=identity.id, actor=default_user)
+ assert len(blocks) == 1
+
+ # Check only initial block in the list
+ block_ids = [b.id for b in blocks]
+ assert default_block.id in block_ids
+ assert block_with_identity.id not in block_ids
+ assert block_without_identity.id not in block_ids
+
+ await server.identity_manager.delete_identity_async(identity_id=identity.id, actor=default_user)
+
+
+async def test_upsert_properties(server: SyncServer, default_user):
+ identity_create = IdentityCreate(
+ identifier_key="1234",
+ name="caren",
+ identity_type=IdentityType.user,
+ properties=[
+ IdentityProperty(key="email", value="[email protected]", type=IdentityPropertyType.string),
+ IdentityProperty(key="age", value=28, type=IdentityPropertyType.number),
+ ],
+ )
+
+ identity = await server.identity_manager.create_identity_async(identity_create, actor=default_user)
+ properties = [
+ IdentityProperty(key="email", value="[email protected]", type=IdentityPropertyType.string),
+ IdentityProperty(key="age", value="28", type=IdentityPropertyType.string),
+ IdentityProperty(key="test", value=123, type=IdentityPropertyType.number),
+ ]
+
+ updated_identity = await server.identity_manager.upsert_identity_properties_async(
+ identity_id=identity.id,
+ properties=properties,
+ actor=default_user,
+ )
+ assert updated_identity.properties == properties
+
+ await server.identity_manager.delete_identity_async(identity_id=identity.id, actor=default_user)
diff --git a/tests/managers/test_job_manager.py b/tests/managers/test_job_manager.py
new file mode 100644
index 000000000..58502147b
--- /dev/null
+++ b/tests/managers/test_job_manager.py
@@ -0,0 +1,1036 @@
+import json
+import logging
+import os
+import random
+import re
+import string
+import time
+import uuid
+from datetime import datetime, timedelta, timezone
+from typing import List
+from unittest.mock import AsyncMock, Mock, patch
+
+import pytest
+from _pytest.python_api import approx
+from anthropic.types.beta import BetaMessage
+from anthropic.types.beta.messages import BetaMessageBatchIndividualResponse, BetaMessageBatchSucceededResult
+
+# Import shared fixtures and constants from conftest
+from conftest import (
+ CREATE_DELAY_SQLITE,
+ DEFAULT_EMBEDDING_CONFIG,
+ USING_SQLITE,
+)
+from openai.types.chat.chat_completion_message_tool_call import ChatCompletionMessageToolCall as OpenAIToolCall, Function as OpenAIFunction
+from sqlalchemy import func, select
+from sqlalchemy.exc import IntegrityError, InvalidRequestError
+from sqlalchemy.orm.exc import StaleDataError
+
+from letta.config import LettaConfig
+from letta.constants import (
+ BASE_MEMORY_TOOLS,
+ BASE_SLEEPTIME_TOOLS,
+ BASE_TOOLS,
+ BASE_VOICE_SLEEPTIME_CHAT_TOOLS,
+ BASE_VOICE_SLEEPTIME_TOOLS,
+ BUILTIN_TOOLS,
+ DEFAULT_ORG_ID,
+ DEFAULT_ORG_NAME,
+ FILES_TOOLS,
+ LETTA_TOOL_EXECUTION_DIR,
+ LETTA_TOOL_SET,
+ LOCAL_ONLY_MULTI_AGENT_TOOLS,
+ MCP_TOOL_TAG_NAME_PREFIX,
+ MULTI_AGENT_TOOLS,
+)
+from letta.data_sources.redis_client import NoopAsyncRedisClient, get_redis_client
+from letta.errors import LettaAgentNotFoundError
+from letta.functions.functions import derive_openai_json_schema, parse_source_code
+from letta.functions.mcp_client.types import MCPTool
+from letta.helpers import ToolRulesSolver
+from letta.helpers.datetime_helpers import AsyncTimer
+from letta.jobs.types import ItemUpdateInfo, RequestStatusUpdateInfo, StepStatusUpdateInfo
+from letta.orm import Base, Block
+from letta.orm.block_history import BlockHistory
+from letta.orm.errors import NoResultFound, UniqueConstraintViolationError
+from letta.orm.file import FileContent as FileContentModel, FileMetadata as FileMetadataModel
+from letta.schemas.agent import CreateAgent, UpdateAgent
+from letta.schemas.block import Block as PydanticBlock, BlockUpdate, CreateBlock
+from letta.schemas.embedding_config import EmbeddingConfig
+from letta.schemas.enums import (
+ ActorType,
+ AgentStepStatus,
+ FileProcessingStatus,
+ JobStatus,
+ JobType,
+ MessageRole,
+ ProviderType,
+ SandboxType,
+ StepStatus,
+ TagMatchMode,
+ ToolType,
+ VectorDBProvider,
+)
+from letta.schemas.environment_variables import SandboxEnvironmentVariableCreate, SandboxEnvironmentVariableUpdate
+from letta.schemas.file import FileMetadata, FileMetadata as PydanticFileMetadata
+from letta.schemas.identity import IdentityCreate, IdentityProperty, IdentityPropertyType, IdentityType, IdentityUpdate, IdentityUpsert
+from letta.schemas.job import BatchJob, Job, Job as PydanticJob, JobUpdate, LettaRequestConfig
+from letta.schemas.letta_message import UpdateAssistantMessage, UpdateReasoningMessage, UpdateSystemMessage, UpdateUserMessage
+from letta.schemas.letta_message_content import TextContent
+from letta.schemas.letta_stop_reason import LettaStopReason, StopReasonType
+from letta.schemas.llm_batch_job import AgentStepState, LLMBatchItem
+from letta.schemas.llm_config import LLMConfig
+from letta.schemas.message import Message as PydanticMessage, MessageCreate, MessageUpdate
+from letta.schemas.openai.chat_completion_response import UsageStatistics
+from letta.schemas.organization import Organization, Organization as PydanticOrganization, OrganizationUpdate
+from letta.schemas.passage import Passage as PydanticPassage
+from letta.schemas.pip_requirement import PipRequirement
+from letta.schemas.run import Run as PydanticRun
+from letta.schemas.sandbox_config import E2BSandboxConfig, LocalSandboxConfig, SandboxConfigCreate, SandboxConfigUpdate
+from letta.schemas.source import Source as PydanticSource, SourceUpdate
+from letta.schemas.tool import Tool as PydanticTool, ToolCreate, ToolUpdate
+from letta.schemas.tool_rule import InitToolRule
+from letta.schemas.user import User as PydanticUser, UserUpdate
+from letta.server.db import db_registry
+from letta.server.server import SyncServer
+from letta.services.block_manager import BlockManager
+from letta.services.helpers.agent_manager_helper import calculate_base_tools, calculate_multi_agent_tools, validate_agent_exists_async
+from letta.services.step_manager import FeedbackType
+from letta.settings import settings, tool_settings
+from letta.utils import calculate_file_defaults_based_on_context_window
+from tests.helpers.utils import comprehensive_agent_checks, validate_context_window_overview
+from tests.utils import random_string
+
+# ======================================================================================================================
+# JobManager Tests
+# ======================================================================================================================
+
+
+async def test_create_job(server: SyncServer, default_user):
+ """Test creating a job."""
+ job_data = PydanticJob(
+ status=JobStatus.created,
+ metadata={"type": "test"},
+ )
+
+ created_job = await server.job_manager.create_job_async(pydantic_job=job_data, actor=default_user)
+
+ # Assertions to ensure the created job matches the expected values
+ assert created_job.user_id == default_user.id
+ assert created_job.status == JobStatus.created
+ assert created_job.metadata == {"type": "test"}
+
+
+async def test_get_job_by_id(server: SyncServer, default_user):
+ """Test fetching a job by ID."""
+ # Create a job
+ job_data = PydanticJob(
+ status=JobStatus.created,
+ metadata={"type": "test"},
+ )
+ created_job = await server.job_manager.create_job_async(pydantic_job=job_data, actor=default_user)
+
+ # Fetch the job by ID
+ fetched_job = await server.job_manager.get_job_by_id_async(created_job.id, actor=default_user)
+
+ # Assertions to ensure the fetched job matches the created job
+ assert fetched_job.id == created_job.id
+ assert fetched_job.status == JobStatus.created
+ assert fetched_job.metadata == {"type": "test"}
+
+
+async def test_list_jobs(server: SyncServer, default_user):
+ """Test listing jobs."""
+ # Create multiple jobs
+ for i in range(3):
+ job_data = PydanticJob(
+ status=JobStatus.created,
+ metadata={"type": f"test-{i}"},
+ )
+ await server.job_manager.create_job_async(pydantic_job=job_data, actor=default_user)
+
+ # List jobs
+ jobs = await server.job_manager.list_jobs_async(actor=default_user)
+
+ # Assertions to check that the created jobs are listed
+ assert len(jobs) == 3
+ assert all(job.user_id == default_user.id for job in jobs)
+ assert all(job.metadata["type"].startswith("test") for job in jobs)
+
+
+async def test_list_jobs_with_metadata(server: SyncServer, default_user):
+ for i in range(3):
+ job_data = PydanticJob(status=JobStatus.created, metadata={"source_id": f"source-test-{i}"})
+ await server.job_manager.create_job_async(pydantic_job=job_data, actor=default_user)
+ jobs = await server.job_manager.list_jobs_async(actor=default_user, source_id="source-test-2")
+ assert len(jobs) == 1
+ assert jobs[0].metadata["source_id"] == "source-test-2"
+
+
+async def test_update_job_by_id(server: SyncServer, default_user):
+ """Test updating a job by its ID."""
+ # Create a job
+ job_data = PydanticJob(
+ status=JobStatus.created,
+ metadata={"type": "test"},
+ )
+ created_job = await server.job_manager.create_job_async(pydantic_job=job_data, actor=default_user)
+ assert created_job.metadata == {"type": "test"}
+
+ # Update the job
+ update_data = JobUpdate(status=JobStatus.completed, metadata={"type": "updated"})
+ updated_job = await server.job_manager.update_job_by_id_async(created_job.id, update_data, actor=default_user)
+
+ # Assertions to ensure the job was updated
+ assert updated_job.status == JobStatus.completed
+ assert updated_job.metadata == {"type": "updated"}
+ assert updated_job.completed_at is not None
+
+
+async def test_delete_job_by_id(server: SyncServer, default_user):
+ """Test deleting a job by its ID."""
+ # Create a job
+ job_data = PydanticJob(
+ status=JobStatus.created,
+ metadata={"type": "test"},
+ )
+ created_job = await server.job_manager.create_job_async(pydantic_job=job_data, actor=default_user)
+
+ # Delete the job
+ await server.job_manager.delete_job_by_id_async(created_job.id, actor=default_user)
+
+ # List jobs to ensure the job was deleted
+ jobs = await server.job_manager.list_jobs_async(actor=default_user)
+ assert len(jobs) == 0
+
+
+async def test_update_job_auto_complete(server: SyncServer, default_user):
+ """Test that updating a job's status to 'completed' automatically sets completed_at."""
+ # Create a job
+ job_data = PydanticJob(
+ status=JobStatus.created,
+ metadata={"type": "test"},
+ )
+ created_job = await server.job_manager.create_job_async(pydantic_job=job_data, actor=default_user)
+
+ # Update the job's status to 'completed'
+ update_data = JobUpdate(status=JobStatus.completed)
+ updated_job = await server.job_manager.update_job_by_id_async(created_job.id, update_data, actor=default_user)
+
+ # Assertions to check that completed_at was set
+ assert updated_job.status == JobStatus.completed
+ assert updated_job.completed_at is not None
+
+
+async def test_get_job_not_found(server: SyncServer, default_user):
+ """Test fetching a non-existent job."""
+ non_existent_job_id = "nonexistent-id"
+ with pytest.raises(NoResultFound):
+ await server.job_manager.get_job_by_id_async(non_existent_job_id, actor=default_user)
+
+
+async def test_delete_job_not_found(server: SyncServer, default_user):
+ """Test deleting a non-existent job."""
+ non_existent_job_id = "nonexistent-id"
+ with pytest.raises(NoResultFound):
+ await server.job_manager.delete_job_by_id_async(non_existent_job_id, actor=default_user)
+
+
+async def test_list_jobs_pagination(server: SyncServer, default_user):
+ """Test listing jobs with pagination."""
+ # Create multiple jobs
+ for i in range(10):
+ job_data = PydanticJob(
+ status=JobStatus.created,
+ metadata={"type": f"test-{i}"},
+ )
+ await server.job_manager.create_job_async(pydantic_job=job_data, actor=default_user)
+
+ # List jobs with a limit
+ jobs = await server.job_manager.list_jobs_async(actor=default_user, limit=5)
+ assert len(jobs) == 5
+ assert all(job.user_id == default_user.id for job in jobs)
+
+ # Test cursor-based pagination
+ first_page = await server.job_manager.list_jobs_async(actor=default_user, limit=3, ascending=True) # [J0, J1, J2]
+ assert len(first_page) == 3
+ assert first_page[0].created_at <= first_page[1].created_at <= first_page[2].created_at
+
+ last_page = await server.job_manager.list_jobs_async(actor=default_user, limit=3, ascending=False) # [J9, J8, J7]
+ assert len(last_page) == 3
+ assert last_page[0].created_at >= last_page[1].created_at >= last_page[2].created_at
+ first_page_ids = set(job.id for job in first_page)
+ last_page_ids = set(job.id for job in last_page)
+ assert first_page_ids.isdisjoint(last_page_ids)
+
+ # Test middle page using both before and after
+ middle_page = await server.job_manager.list_jobs_async(
+ actor=default_user, before=last_page[-1].id, after=first_page[-1].id, ascending=True
+ ) # [J3, J4, J5, J6]
+ assert len(middle_page) == 4 # Should include jobs between first and second page
+ head_tail_jobs = first_page_ids.union(last_page_ids)
+ assert all(job.id not in head_tail_jobs for job in middle_page)
+
+ # Test descending order
+ middle_page_desc = await server.job_manager.list_jobs_async(
+ actor=default_user, before=last_page[-1].id, after=first_page[-1].id, ascending=False
+ ) # [J6, J5, J4, J3]
+ assert len(middle_page_desc) == 4
+ assert middle_page_desc[0].id == middle_page[-1].id
+ assert middle_page_desc[1].id == middle_page[-2].id
+ assert middle_page_desc[2].id == middle_page[-3].id
+ assert middle_page_desc[3].id == middle_page[-4].id
+
+ # BONUS
+ job_7 = last_page[-1].id
+ earliest_jobs = await server.job_manager.list_jobs_async(actor=default_user, ascending=False, before=job_7)
+ assert len(earliest_jobs) == 7
+ assert all(j.id not in last_page_ids for j in earliest_jobs)
+ assert all(earliest_jobs[i].created_at >= earliest_jobs[i + 1].created_at for i in range(len(earliest_jobs) - 1))
+
+
+async def test_list_jobs_by_status(server: SyncServer, default_user):
+ """Test listing jobs filtered by status."""
+ # Create multiple jobs with different statuses
+ job_data_created = PydanticJob(
+ status=JobStatus.created,
+ metadata={"type": "test-created"},
+ )
+ job_data_in_progress = PydanticJob(
+ status=JobStatus.running,
+ metadata={"type": "test-running"},
+ )
+ job_data_completed = PydanticJob(
+ status=JobStatus.completed,
+ metadata={"type": "test-completed"},
+ )
+
+ await server.job_manager.create_job_async(pydantic_job=job_data_created, actor=default_user)
+ await server.job_manager.create_job_async(pydantic_job=job_data_in_progress, actor=default_user)
+ await server.job_manager.create_job_async(pydantic_job=job_data_completed, actor=default_user)
+
+ # List jobs filtered by status
+ created_jobs = await server.job_manager.list_jobs_async(actor=default_user, statuses=[JobStatus.created])
+ in_progress_jobs = await server.job_manager.list_jobs_async(actor=default_user, statuses=[JobStatus.running])
+ completed_jobs = await server.job_manager.list_jobs_async(actor=default_user, statuses=[JobStatus.completed])
+
+ # Assertions
+ assert len(created_jobs) == 1
+ assert created_jobs[0].metadata["type"] == job_data_created.metadata["type"]
+
+ assert len(in_progress_jobs) == 1
+ assert in_progress_jobs[0].metadata["type"] == job_data_in_progress.metadata["type"]
+
+ assert len(completed_jobs) == 1
+ assert completed_jobs[0].metadata["type"] == job_data_completed.metadata["type"]
+
+
+async def test_list_jobs_filter_by_type(server: SyncServer, default_user, default_job):
+ """Test that list_jobs correctly filters by job_type."""
+ # Create a run job
+ run_pydantic = PydanticJob(
+ user_id=default_user.id,
+ status=JobStatus.pending,
+ job_type=JobType.RUN,
+ )
+ run = await server.job_manager.create_job_async(pydantic_job=run_pydantic, actor=default_user)
+
+ # List only regular jobs
+ jobs = await server.job_manager.list_jobs_async(actor=default_user)
+ assert len(jobs) == 1
+ assert jobs[0].id == default_job.id
+
+ # List only run jobs
+ jobs = await server.job_manager.list_jobs_async(actor=default_user, job_type=JobType.RUN)
+ assert len(jobs) == 1
+ assert jobs[0].id == run.id
+
+
+async def test_list_jobs_by_stop_reason(server: SyncServer, sarah_agent, default_user):
+ """Test listing jobs by stop reason."""
+
+ run_pydantic = PydanticRun(
+ user_id=default_user.id,
+ status=JobStatus.pending,
+ job_type=JobType.RUN,
+ stop_reason=StopReasonType.requires_approval,
+ agent_id=sarah_agent.id,
+ background=True,
+ )
+ run = await server.job_manager.create_job_async(pydantic_job=run_pydantic, actor=default_user)
+ assert run.stop_reason == StopReasonType.requires_approval
+ assert run.background == True
+ assert run.agent_id == sarah_agent.id
+
+ # list jobs by stop reason
+ jobs = await server.job_manager.list_jobs_async(actor=default_user, job_type=JobType.RUN, stop_reason=StopReasonType.requires_approval)
+ assert len(jobs) == 1
+ assert jobs[0].id == run.id
+
+ # list jobs by background
+ jobs = await server.job_manager.list_jobs_async(actor=default_user, job_type=JobType.RUN, background=True)
+ assert len(jobs) == 1
+ assert jobs[0].id == run.id
+
+ # list jobs by agent_id
+ jobs = await server.job_manager.list_jobs_async(actor=default_user, job_type=JobType.RUN, agent_ids=[sarah_agent.id])
+ assert len(jobs) == 1
+ assert jobs[0].id == run.id
+
+
+async def test_e2e_job_callback(monkeypatch, server: SyncServer, default_user):
+ """Test that job callbacks are properly dispatched when a job is completed."""
+ captured = {}
+
+ # Create a simple mock for the async HTTP client
+ class MockAsyncResponse:
+ status_code = 202
+
+ async def mock_post(url, json, timeout):
+ captured["url"] = url
+ captured["json"] = json
+ return MockAsyncResponse()
+
+ class MockAsyncClient:
+ async def __aenter__(self):
+ return self
+
+ async def __aexit__(self, *args):
+ pass
+
+ async def post(self, url, json, timeout):
+ return await mock_post(url, json, timeout)
+
+ # Patch the AsyncClient
+ import letta.services.job_manager as job_manager_module
+
+ monkeypatch.setattr(job_manager_module, "AsyncClient", MockAsyncClient)
+
+ job_in = PydanticJob(status=JobStatus.created, metadata={"foo": "bar"}, callback_url="http://example.test/webhook/jobs")
+ created = await server.job_manager.create_job_async(pydantic_job=job_in, actor=default_user)
+ assert created.callback_url == "http://example.test/webhook/jobs"
+
+ # Update the job status to completed, which should trigger the callback
+ update = JobUpdate(status=JobStatus.completed)
+ updated = await server.job_manager.update_job_by_id_async(created.id, update, actor=default_user)
+
+ # Verify the callback was triggered with the correct parameters
+ assert captured["url"] == created.callback_url, "Callback URL doesn't match"
+ assert captured["json"]["job_id"] == created.id, "Job ID in callback doesn't match"
+ assert captured["json"]["status"] == JobStatus.completed.value, "Job status in callback doesn't match"
+
+ # Verify the completed_at timestamp is reasonable
+ actual_dt = datetime.fromisoformat(captured["json"]["completed_at"]).replace(tzinfo=None)
+ assert abs((actual_dt - updated.completed_at).total_seconds()) < 1, "Timestamp difference is too large"
+
+ assert isinstance(updated.callback_sent_at, datetime)
+ assert updated.callback_status_code == 202
+
+
+# ======================================================================================================================
+# JobManager Tests - Messages
+# ======================================================================================================================
+
+
+async def test_job_messages_add(server: SyncServer, default_run, hello_world_message_fixture, default_user):
+ """Test adding a message to a job."""
+ # Add message to job
+ await server.job_manager.add_messages_to_job_async(
+ job_id=default_run.id,
+ message_ids=[hello_world_message_fixture.id],
+ actor=default_user,
+ )
+
+ # Verify message was added
+ messages = await server.job_manager.get_job_messages(
+ job_id=default_run.id,
+ actor=default_user,
+ )
+ assert len(messages) == 1
+ assert messages[0].id == hello_world_message_fixture.id
+ assert messages[0].content[0].text == hello_world_message_fixture.content[0].text
+
+
+async def test_job_messages_pagination(server: SyncServer, default_run, default_user, sarah_agent):
+ """Test pagination of job messages."""
+ # Create multiple messages
+ message_ids = []
+ for i in range(5):
+ message = PydanticMessage(
+ agent_id=sarah_agent.id,
+ role=MessageRole.user,
+ content=[TextContent(text=f"Test message {i}")],
+ )
+ msg = await server.message_manager.create_many_messages_async([message], actor=default_user)
+ message_ids.append(msg[0].id)
+
+ # Add message to job
+ await server.job_manager.add_messages_to_job_async(
+ job_id=default_run.id,
+ message_ids=[msg[0].id],
+ actor=default_user,
+ )
+
+ # Test pagination with limit
+ messages = await server.job_manager.get_job_messages(
+ job_id=default_run.id,
+ actor=default_user,
+ limit=2,
+ )
+ assert len(messages) == 2
+ assert messages[0].id == message_ids[0]
+ assert messages[1].id == message_ids[1]
+
+ # Test pagination with cursor
+ first_page = await server.job_manager.get_job_messages(
+ job_id=default_run.id,
+ actor=default_user,
+ limit=2,
+ ascending=True, # [M0, M1]
+ )
+ assert len(first_page) == 2
+ assert first_page[0].id == message_ids[0]
+ assert first_page[1].id == message_ids[1]
+ assert first_page[0].created_at <= first_page[1].created_at
+
+ last_page = await server.job_manager.get_job_messages(
+ job_id=default_run.id,
+ actor=default_user,
+ limit=2,
+ ascending=False, # [M4, M3]
+ )
+ assert len(last_page) == 2
+ assert last_page[0].id == message_ids[4]
+ assert last_page[1].id == message_ids[3]
+ assert last_page[0].created_at >= last_page[1].created_at
+
+ first_page_ids = set(msg.id for msg in first_page)
+ last_page_ids = set(msg.id for msg in last_page)
+ assert first_page_ids.isdisjoint(last_page_ids)
+
+ # Test middle page using both before and after
+ middle_page = await server.job_manager.get_job_messages(
+ job_id=default_run.id,
+ actor=default_user,
+ before=last_page[-1].id, # M3
+ after=first_page[0].id, # M0
+ ascending=True, # [M1, M2]
+ )
+ assert len(middle_page) == 2 # Should include message between first and last pages
+ assert middle_page[0].id == message_ids[1]
+ assert middle_page[1].id == message_ids[2]
+ head_tail_msgs = first_page_ids.union(last_page_ids)
+ assert middle_page[1].id not in head_tail_msgs
+ assert middle_page[0].id in first_page_ids
+
+ # Test descending order for middle page
+ middle_page = await server.job_manager.get_job_messages(
+ job_id=default_run.id,
+ actor=default_user,
+ before=last_page[-1].id, # M3
+ after=first_page[0].id, # M0
+ ascending=False, # [M2, M1]
+ )
+ assert len(middle_page) == 2 # Should include message between first and last pages
+ assert middle_page[0].id == message_ids[2]
+ assert middle_page[1].id == message_ids[1]
+
+ # Test getting earliest messages
+ msg_3 = last_page[-1].id
+ earliest_msgs = await server.job_manager.get_job_messages(
+ job_id=default_run.id,
+ actor=default_user,
+ ascending=False,
+ before=msg_3, # Get messages after M3 in descending order
+ )
+ assert len(earliest_msgs) == 3 # Should get M2, M1, M0
+ assert all(m.id not in last_page_ids for m in earliest_msgs)
+ assert earliest_msgs[0].created_at > earliest_msgs[1].created_at > earliest_msgs[2].created_at
+
+ # Test getting earliest messages with ascending order
+ earliest_msgs_ascending = await server.job_manager.get_job_messages(
+ job_id=default_run.id,
+ actor=default_user,
+ ascending=True,
+ before=msg_3, # Get messages before M3 in ascending order
+ )
+ assert len(earliest_msgs_ascending) == 3 # Should get M0, M1, M2
+ assert all(m.id not in last_page_ids for m in earliest_msgs_ascending)
+ assert earliest_msgs_ascending[0].created_at < earliest_msgs_ascending[1].created_at < earliest_msgs_ascending[2].created_at
+
+
+async def test_job_messages_ordering(server: SyncServer, default_run, default_user, sarah_agent):
+ """Test that messages are ordered by created_at."""
+ # Create messages with different timestamps
+ base_time = datetime.now(timezone.utc)
+ message_times = [
+ base_time - timedelta(minutes=2),
+ base_time - timedelta(minutes=1),
+ base_time,
+ ]
+
+ for i, created_at in enumerate(message_times):
+ message = PydanticMessage(
+ role=MessageRole.user,
+ content=[TextContent(text="Test message")],
+ agent_id=sarah_agent.id,
+ created_at=created_at,
+ )
+ msg = await server.message_manager.create_many_messages_async([message], actor=default_user)
+
+ # Add message to job
+ await server.job_manager.add_messages_to_job_async(
+ job_id=default_run.id,
+ message_ids=[msg[0].id],
+ actor=default_user,
+ )
+
+ # Verify messages are returned in chronological order
+ returned_messages = await server.job_manager.get_job_messages(
+ job_id=default_run.id,
+ actor=default_user,
+ )
+
+ assert len(returned_messages) == 3
+ assert returned_messages[0].created_at < returned_messages[1].created_at
+ assert returned_messages[1].created_at < returned_messages[2].created_at
+
+ # Verify messages are returned in descending order
+ returned_messages = await server.job_manager.get_job_messages(
+ job_id=default_run.id,
+ actor=default_user,
+ ascending=False,
+ )
+
+ assert len(returned_messages) == 3
+ assert returned_messages[0].created_at > returned_messages[1].created_at
+ assert returned_messages[1].created_at > returned_messages[2].created_at
+
+
+async def test_job_messages_empty(server: SyncServer, default_run, default_user):
+ """Test getting messages for a job with no messages."""
+ messages = await server.job_manager.get_job_messages(
+ job_id=default_run.id,
+ actor=default_user,
+ )
+ assert len(messages) == 0
+
+
+async def test_job_messages_add_duplicate(server: SyncServer, default_run, hello_world_message_fixture, default_user):
+ """Test adding the same message to a job twice."""
+ # Add message to job first time
+ await server.job_manager.add_messages_to_job_async(
+ job_id=default_run.id,
+ message_ids=[hello_world_message_fixture.id],
+ actor=default_user,
+ )
+
+ # Attempt to add same message again
+ with pytest.raises(IntegrityError):
+ await server.job_manager.add_messages_to_job_async(
+ job_id=default_run.id,
+ message_ids=[hello_world_message_fixture.id],
+ actor=default_user,
+ )
+
+
+async def test_job_messages_filter(server: SyncServer, default_run, default_user, sarah_agent):
+ """Test getting messages associated with a job."""
+ # Create test messages with different roles and tool calls
+ messages = [
+ PydanticMessage(
+ role=MessageRole.user,
+ content=[TextContent(text="Hello")],
+ agent_id=sarah_agent.id,
+ ),
+ PydanticMessage(
+ role=MessageRole.assistant,
+ content=[TextContent(text="Hi there!")],
+ agent_id=sarah_agent.id,
+ ),
+ PydanticMessage(
+ role=MessageRole.assistant,
+ content=[TextContent(text="Let me help you with that")],
+ agent_id=sarah_agent.id,
+ tool_calls=[
+ OpenAIToolCall(
+ id="call_1",
+ type="function",
+ function=OpenAIFunction(
+ name="test_tool",
+ arguments='{"arg1": "value1"}',
+ ),
+ )
+ ],
+ ),
+ ]
+
+ # Add messages to job
+ for msg in messages:
+ created_msg = await server.message_manager.create_many_messages_async([msg], actor=default_user)
+ await server.job_manager.add_messages_to_job_async(
+ job_id=default_run.id,
+ message_ids=[created_msg[0].id],
+ actor=default_user,
+ )
+
+ # Test getting all messages
+ all_messages = await server.job_manager.get_job_messages(
+ job_id=default_run.id,
+ actor=default_user,
+ )
+ assert len(all_messages) == 3
+
+ # Test filtering by role
+ user_messages = await server.job_manager.get_job_messages(job_id=default_run.id, actor=default_user, role=MessageRole.user)
+ assert len(user_messages) == 1
+ assert user_messages[0].role == MessageRole.user
+
+ # Test limit
+ limited_messages = await server.job_manager.get_job_messages(job_id=default_run.id, actor=default_user, limit=2)
+ assert len(limited_messages) == 2
+
+
+async def test_get_run_messages(server: SyncServer, default_user: PydanticUser, sarah_agent):
+ """Test getting messages for a run with request config."""
+ # Create a run with custom request config
+ run = await server.job_manager.create_job_async(
+ pydantic_job=PydanticRun(
+ user_id=default_user.id,
+ status=JobStatus.created,
+ request_config=LettaRequestConfig(
+ use_assistant_message=False, assistant_message_tool_name="custom_tool", assistant_message_tool_kwarg="custom_arg"
+ ),
+ ),
+ actor=default_user,
+ )
+
+ # Add some messages
+ messages = [
+ PydanticMessage(
+ agent_id=sarah_agent.id,
+ role=MessageRole.tool if i % 2 == 0 else MessageRole.assistant,
+ content=[TextContent(text=f"Test message {i}" if i % 2 == 1 else '{"status": "OK"}')],
+ tool_calls=(
+ [{"type": "function", "id": f"call_{i // 2}", "function": {"name": "custom_tool", "arguments": '{"custom_arg": "test"}'}}]
+ if i % 2 == 1
+ else None
+ ),
+ tool_call_id=f"call_{i // 2}" if i % 2 == 0 else None,
+ )
+ for i in range(4)
+ ]
+
+ created_msg = await server.message_manager.create_many_messages_async(messages, actor=default_user)
+ for msg in created_msg:
+ await server.job_manager.add_messages_to_job_async(
+ job_id=run.id,
+ message_ids=[msg.id],
+ actor=default_user,
+ )
+
+ # Get messages and verify they're converted correctly
+ result = await server.job_manager.get_run_messages(run_id=run.id, actor=default_user)
+
+ # Verify correct number of messages. Assistant messages should be parsed
+ assert len(result) == 6
+
+ # Verify assistant messages are parsed according to request config
+ tool_call_messages = [msg for msg in result if msg.message_type == "tool_call_message"]
+ reasoning_messages = [msg for msg in result if msg.message_type == "reasoning_message"]
+ assert len(tool_call_messages) == 2
+ assert len(reasoning_messages) == 2
+ for msg in tool_call_messages:
+ assert msg.tool_call is not None
+ assert msg.tool_call.name == "custom_tool"
+
+
+async def test_get_run_messages_with_assistant_message(server: SyncServer, default_user: PydanticUser, sarah_agent):
+ """Test getting messages for a run with request config."""
+ # Create a run with custom request config
+ run = await server.job_manager.create_job_async(
+ pydantic_job=PydanticRun(
+ user_id=default_user.id,
+ status=JobStatus.created,
+ request_config=LettaRequestConfig(
+ use_assistant_message=True, assistant_message_tool_name="custom_tool", assistant_message_tool_kwarg="custom_arg"
+ ),
+ ),
+ actor=default_user,
+ )
+
+ # Add some messages
+ messages = [
+ PydanticMessage(
+ agent_id=sarah_agent.id,
+ role=MessageRole.tool if i % 2 == 0 else MessageRole.assistant,
+ content=[TextContent(text=f"Test message {i}" if i % 2 == 1 else '{"status": "OK"}')],
+ tool_calls=(
+ [{"type": "function", "id": f"call_{i // 2}", "function": {"name": "custom_tool", "arguments": '{"custom_arg": "test"}'}}]
+ if i % 2 == 1
+ else None
+ ),
+ tool_call_id=f"call_{i // 2}" if i % 2 == 0 else None,
+ )
+ for i in range(4)
+ ]
+
+ created_msg = await server.message_manager.create_many_messages_async(messages, actor=default_user)
+ for msg in created_msg:
+ await server.job_manager.add_messages_to_job_async(
+ job_id=run.id,
+ message_ids=[msg.id],
+ actor=default_user,
+ )
+
+ # Get messages and verify they're converted correctly
+ result = await server.job_manager.get_run_messages(run_id=run.id, actor=default_user)
+
+ # Verify correct number of messages. Assistant messages should be parsed
+ assert len(result) == 4
+
+ # Verify assistant messages are parsed according to request config
+ assistant_messages = [msg for msg in result if msg.message_type == "assistant_message"]
+ reasoning_messages = [msg for msg in result if msg.message_type == "reasoning_message"]
+ assert len(assistant_messages) == 2
+ assert len(reasoning_messages) == 2
+ for msg in assistant_messages:
+ assert msg.content == "test"
+ for msg in reasoning_messages:
+ assert "Test message" in msg.reasoning
+
+
+# ======================================================================================================================
+# JobManager Tests - Usage Statistics -
+# ======================================================================================================================
+
+# TODO: add these back after runs refactor
+
+# @pytest.mark.asyncio
+# async def test_job_usage_stats_add_and_get(server: SyncServer, sarah_agent, default_job, default_user):
+# """Test adding and retrieving job usage statistics."""
+# job_manager = server.job_manager
+# step_manager = server.step_manager
+#
+# # Add usage statistics
+# await step_manager.log_step_async(
+# agent_id=sarah_agent.id,
+# provider_name="openai",
+# provider_category="base",
+# model="gpt-4o-mini",
+# model_endpoint="https://api.openai.com/v1",
+# context_window_limit=8192,
+# job_id=default_job.id,
+# usage=UsageStatistics(
+# completion_tokens=100,
+# prompt_tokens=50,
+# total_tokens=150,
+# ),
+# actor=default_user,
+# project_id=sarah_agent.project_id,
+# )
+#
+# # Get usage statistics
+# usage_stats = await job_manager.get_job_usage_async(job_id=default_job.id, actor=default_user)
+#
+# # Verify the statistics
+# assert usage_stats.completion_tokens == 100
+# assert usage_stats.prompt_tokens == 50
+# assert usage_stats.total_tokens == 150
+#
+# # get steps
+# steps = job_manager.get_job_steps(job_id=default_job.id, actor=default_user)
+# assert len(steps) == 1
+#
+#
+# @pytest.mark.asyncio
+# async def test_job_usage_stats_get_no_stats(server: SyncServer, default_job, default_user):
+# """Test getting usage statistics for a job with no stats."""
+# job_manager = server.job_manager
+#
+# # Get usage statistics for a job with no stats
+# usage_stats = await job_manager.get_job_usage(job_id=default_job.id, actor=default_user)
+#
+# # Verify default values
+# assert usage_stats.completion_tokens == 0
+# assert usage_stats.prompt_tokens == 0
+# assert usage_stats.total_tokens == 0
+#
+# # get steps
+# steps = job_manager.get_job_steps(job_id=default_job.id, actor=default_user)
+# assert len(steps) == 0
+#
+#
+# @pytest.mark.asyncio
+# async def test_job_usage_stats_add_multiple(server: SyncServer, sarah_agent, default_job, default_user):
+# """Test adding multiple usage statistics entries for a job."""
+# job_manager = server.job_manager
+# step_manager = server.step_manager
+#
+# # Add first usage statistics entry
+# await step_manager.log_step_async(
+# agent_id=sarah_agent.id,
+# provider_name="openai",
+# provider_category="base",
+# model="gpt-4o-mini",
+# model_endpoint="https://api.openai.com/v1",
+# context_window_limit=8192,
+# job_id=default_job.id,
+# usage=UsageStatistics(
+# completion_tokens=100,
+# prompt_tokens=50,
+# total_tokens=150,
+# ),
+# actor=default_user,
+# project_id=sarah_agent.project_id,
+# )
+#
+# # Add second usage statistics entry
+# await step_manager.log_step_async(
+# agent_id=sarah_agent.id,
+# provider_name="openai",
+# provider_category="base",
+# model="gpt-4o-mini",
+# model_endpoint="https://api.openai.com/v1",
+# context_window_limit=8192,
+# job_id=default_job.id,
+# usage=UsageStatistics(
+# completion_tokens=200,
+# prompt_tokens=100,
+# total_tokens=300,
+# ),
+# actor=default_user,
+# project_id=sarah_agent.project_id,
+# )
+#
+# # Get usage statistics (should return the latest entry)
+# usage_stats = job_manager.get_job_usage(job_id=default_job.id, actor=default_user)
+#
+# # Verify we get the most recent statistics
+# assert usage_stats.completion_tokens == 300
+# assert usage_stats.prompt_tokens == 150
+# assert usage_stats.total_tokens == 450
+# assert usage_stats.step_count == 2
+#
+# # get steps
+# steps = job_manager.get_job_steps(job_id=default_job.id, actor=default_user)
+# assert len(steps) == 2
+#
+# # get agent steps
+# steps = await step_manager.list_steps_async(agent_id=sarah_agent.id, actor=default_user)
+# assert len(steps) == 2
+#
+# # add step feedback
+# step_manager = server.step_manager
+#
+# # Add feedback to first step
+# await step_manager.add_feedback_async(step_id=steps[0].id, feedback=FeedbackType.POSITIVE, actor=default_user)
+#
+# # Test has_feedback filtering
+# steps_with_feedback = await step_manager.list_steps_async(agent_id=sarah_agent.id, has_feedback=True, actor=default_user)
+# assert len(steps_with_feedback) == 1
+#
+# steps_without_feedback = await step_manager.list_steps_async(agent_id=sarah_agent.id, actor=default_user)
+# assert len(steps_without_feedback) == 2
+#
+
+
+# @pytest.mark.asyncio
+# async def test_job_usage_stats_get_nonexistent_job(server: SyncServer, default_user):
+# """Test getting usage statistics for a nonexistent job."""
+# job_manager = server.job_manager
+#
+# with pytest.raises(NoResultFound):
+# job_manager.get_job_usage(job_id="nonexistent_job", actor=default_user)
+
+
+async def test_record_ttft(server: SyncServer, default_user):
+ """Test recording time to first token for a job."""
+ # Create a job
+ job_data = PydanticJob(
+ status=JobStatus.created,
+ metadata={"type": "test_timing"},
+ )
+ created_job = await server.job_manager.create_job_async(pydantic_job=job_data, actor=default_user)
+
+ # Record TTFT
+ ttft_ns = 1_500_000_000 # 1.5 seconds in nanoseconds
+ await server.job_manager.record_ttft(created_job.id, ttft_ns, default_user)
+
+ # Fetch the job and verify TTFT was recorded
+ updated_job = await server.job_manager.get_job_by_id_async(created_job.id, default_user)
+ assert updated_job.ttft_ns == ttft_ns
+
+
+async def test_record_response_duration(server: SyncServer, default_user):
+ """Test recording total response duration for a job."""
+ # Create a job
+ job_data = PydanticJob(
+ status=JobStatus.created,
+ metadata={"type": "test_timing"},
+ )
+ created_job = await server.job_manager.create_job_async(pydantic_job=job_data, actor=default_user)
+
+ # Record response duration
+ duration_ns = 5_000_000_000 # 5 seconds in nanoseconds
+ await server.job_manager.record_response_duration(created_job.id, duration_ns, default_user)
+
+ # Fetch the job and verify duration was recorded
+ updated_job = await server.job_manager.get_job_by_id_async(created_job.id, default_user)
+ assert updated_job.total_duration_ns == duration_ns
+
+
+async def test_record_timing_metrics_together(server: SyncServer, default_user):
+ """Test recording both TTFT and response duration for a job."""
+ # Create a job
+ job_data = PydanticJob(
+ status=JobStatus.created,
+ metadata={"type": "test_timing_combined"},
+ )
+ created_job = await server.job_manager.create_job_async(pydantic_job=job_data, actor=default_user)
+
+ # Record both metrics
+ ttft_ns = 2_000_000_000 # 2 seconds in nanoseconds
+ duration_ns = 8_500_000_000 # 8.5 seconds in nanoseconds
+
+ await server.job_manager.record_ttft(created_job.id, ttft_ns, default_user)
+ await server.job_manager.record_response_duration(created_job.id, duration_ns, default_user)
+
+ # Fetch the job and verify both metrics were recorded
+ updated_job = await server.job_manager.get_job_by_id_async(created_job.id, default_user)
+ assert updated_job.ttft_ns == ttft_ns
+ assert updated_job.total_duration_ns == duration_ns
+
+
+async def test_record_timing_invalid_job(server: SyncServer, default_user):
+ """Test recording timing metrics for non-existent job fails gracefully."""
+ # Try to record TTFT for non-existent job - should not raise exception but log warning
+ await server.job_manager.record_ttft("nonexistent_job_id", 1_000_000_000, default_user)
+
+ # Try to record response duration for non-existent job - should not raise exception but log warning
+ await server.job_manager.record_response_duration("nonexistent_job_id", 2_000_000_000, default_user)
diff --git a/tests/managers/test_mcp_manager.py b/tests/managers/test_mcp_manager.py
new file mode 100644
index 000000000..d86660895
--- /dev/null
+++ b/tests/managers/test_mcp_manager.py
@@ -0,0 +1,720 @@
+import json
+import logging
+import os
+import random
+import re
+import string
+import time
+import uuid
+from datetime import datetime, timedelta, timezone
+from typing import List
+from unittest.mock import AsyncMock, Mock, patch
+
+import pytest
+from _pytest.python_api import approx
+from anthropic.types.beta import BetaMessage
+from anthropic.types.beta.messages import BetaMessageBatchIndividualResponse, BetaMessageBatchSucceededResult
+
+# Import shared fixtures and constants from conftest
+from conftest import (
+ CREATE_DELAY_SQLITE,
+ DEFAULT_EMBEDDING_CONFIG,
+ USING_SQLITE,
+)
+from openai.types.chat.chat_completion_message_tool_call import ChatCompletionMessageToolCall as OpenAIToolCall, Function as OpenAIFunction
+from sqlalchemy import func, select
+from sqlalchemy.exc import IntegrityError, InvalidRequestError
+from sqlalchemy.orm.exc import StaleDataError
+
+from letta.config import LettaConfig
+from letta.constants import (
+ BASE_MEMORY_TOOLS,
+ BASE_SLEEPTIME_TOOLS,
+ BASE_TOOLS,
+ BASE_VOICE_SLEEPTIME_CHAT_TOOLS,
+ BASE_VOICE_SLEEPTIME_TOOLS,
+ BUILTIN_TOOLS,
+ DEFAULT_ORG_ID,
+ DEFAULT_ORG_NAME,
+ FILES_TOOLS,
+ LETTA_TOOL_EXECUTION_DIR,
+ LETTA_TOOL_SET,
+ LOCAL_ONLY_MULTI_AGENT_TOOLS,
+ MCP_TOOL_TAG_NAME_PREFIX,
+ MULTI_AGENT_TOOLS,
+)
+from letta.data_sources.redis_client import NoopAsyncRedisClient, get_redis_client
+from letta.errors import LettaAgentNotFoundError
+from letta.functions.functions import derive_openai_json_schema, parse_source_code
+from letta.functions.mcp_client.types import MCPTool
+from letta.helpers import ToolRulesSolver
+from letta.helpers.datetime_helpers import AsyncTimer
+from letta.jobs.types import ItemUpdateInfo, RequestStatusUpdateInfo, StepStatusUpdateInfo
+from letta.orm import Base, Block
+from letta.orm.block_history import BlockHistory
+from letta.orm.errors import NoResultFound, UniqueConstraintViolationError
+from letta.orm.file import FileContent as FileContentModel, FileMetadata as FileMetadataModel
+from letta.schemas.agent import CreateAgent, UpdateAgent
+from letta.schemas.block import Block as PydanticBlock, BlockUpdate, CreateBlock
+from letta.schemas.embedding_config import EmbeddingConfig
+from letta.schemas.enums import (
+ ActorType,
+ AgentStepStatus,
+ FileProcessingStatus,
+ JobStatus,
+ JobType,
+ MessageRole,
+ ProviderType,
+ SandboxType,
+ StepStatus,
+ TagMatchMode,
+ ToolType,
+ VectorDBProvider,
+)
+from letta.schemas.environment_variables import SandboxEnvironmentVariableCreate, SandboxEnvironmentVariableUpdate
+from letta.schemas.file import FileMetadata, FileMetadata as PydanticFileMetadata
+from letta.schemas.identity import IdentityCreate, IdentityProperty, IdentityPropertyType, IdentityType, IdentityUpdate, IdentityUpsert
+from letta.schemas.job import BatchJob, Job, Job as PydanticJob, JobUpdate, LettaRequestConfig
+from letta.schemas.letta_message import UpdateAssistantMessage, UpdateReasoningMessage, UpdateSystemMessage, UpdateUserMessage
+from letta.schemas.letta_message_content import TextContent
+from letta.schemas.letta_stop_reason import LettaStopReason, StopReasonType
+from letta.schemas.llm_batch_job import AgentStepState, LLMBatchItem
+from letta.schemas.llm_config import LLMConfig
+from letta.schemas.message import Message as PydanticMessage, MessageCreate, MessageUpdate
+from letta.schemas.openai.chat_completion_response import UsageStatistics
+from letta.schemas.organization import Organization, Organization as PydanticOrganization, OrganizationUpdate
+from letta.schemas.passage import Passage as PydanticPassage
+from letta.schemas.pip_requirement import PipRequirement
+from letta.schemas.run import Run as PydanticRun
+from letta.schemas.sandbox_config import E2BSandboxConfig, LocalSandboxConfig, SandboxConfigCreate, SandboxConfigUpdate
+from letta.schemas.source import Source as PydanticSource, SourceUpdate
+from letta.schemas.tool import Tool as PydanticTool, ToolCreate, ToolUpdate
+from letta.schemas.tool_rule import InitToolRule
+from letta.schemas.user import User as PydanticUser, UserUpdate
+from letta.server.db import db_registry
+from letta.server.server import SyncServer
+from letta.services.block_manager import BlockManager
+from letta.services.helpers.agent_manager_helper import calculate_base_tools, calculate_multi_agent_tools, validate_agent_exists_async
+from letta.services.step_manager import FeedbackType
+from letta.settings import settings, tool_settings
+from letta.utils import calculate_file_defaults_based_on_context_window
+from tests.helpers.utils import comprehensive_agent_checks, validate_context_window_overview
+from tests.utils import random_string
+
+# ======================================================================================================================
+# MCPManager Tests
+# ======================================================================================================================
+
+
+@patch("letta.services.mcp_manager.MCPManager.get_mcp_client")
+async def test_create_mcp_server(mock_get_client, server, default_user):
+ from letta.schemas.mcp import MCPServer, MCPServerType, SSEServerConfig, StdioServerConfig
+ from letta.settings import tool_settings
+
+ if tool_settings.mcp_read_from_config:
+ return
+
+ # create mock client with required methods
+ mock_client = AsyncMock()
+ mock_client.connect_to_server = AsyncMock()
+ mock_client.list_tools = AsyncMock(
+ return_value=[
+ MCPTool(
+ name="get_simple_price",
+ inputSchema={
+ "type": "object",
+ "properties": {
+ "ids": {"type": "string"},
+ "vs_currencies": {"type": "string"},
+ "include_market_cap": {"type": "boolean"},
+ "include_24hr_vol": {"type": "boolean"},
+ "include_24hr_change": {"type": "boolean"},
+ },
+ "required": ["ids", "vs_currencies"],
+ "additionalProperties": False,
+ },
+ )
+ ]
+ )
+ mock_client.execute_tool = AsyncMock(
+ return_value=(
+ '{"bitcoin": {"usd": 50000, "usd_market_cap": 900000000000, "usd_24h_vol": 30000000000, "usd_24h_change": 2.5}}',
+ True,
+ )
+ )
+ mock_get_client.return_value = mock_client
+
+ # Test with a valid StdioServerConfig
+ server_config = StdioServerConfig(
+ server_name="test_server", type=MCPServerType.STDIO, command="echo 'test'", args=["arg1", "arg2"], env={"ENV1": "value1"}
+ )
+ mcp_server = MCPServer(server_name="test_server", server_type=MCPServerType.STDIO, stdio_config=server_config)
+ created_server = await server.mcp_manager.create_or_update_mcp_server(mcp_server, actor=default_user)
+ print(created_server)
+ assert created_server.server_name == server_config.server_name
+ assert created_server.server_type == server_config.type
+
+ # Test with a valid SSEServerConfig
+ mcp_server_name = "coingecko"
+ server_url = "https://mcp.api.coingecko.com/sse"
+ sse_mcp_config = SSEServerConfig(server_name=mcp_server_name, server_url=server_url)
+ mcp_sse_server = MCPServer(server_name=mcp_server_name, server_type=MCPServerType.SSE, server_url=server_url)
+ created_server = await server.mcp_manager.create_or_update_mcp_server(mcp_sse_server, actor=default_user)
+ print(created_server)
+ assert created_server.server_name == mcp_server_name
+ assert created_server.server_type == MCPServerType.SSE
+
+ # list mcp servers
+ servers = await server.mcp_manager.list_mcp_servers(actor=default_user)
+ print(servers)
+ assert len(servers) > 0, "No MCP servers found"
+
+ # list tools from sse server
+ tools = await server.mcp_manager.list_mcp_server_tools(created_server.server_name, actor=default_user)
+ print(tools)
+
+ # call a tool from the sse server
+ tool_name = "get_simple_price"
+ tool_args = {
+ "ids": "bitcoin",
+ "vs_currencies": "usd",
+ "include_market_cap": True,
+ "include_24hr_vol": True,
+ "include_24hr_change": True,
+ }
+ result = await server.mcp_manager.execute_mcp_server_tool(
+ created_server.server_name, tool_name=tool_name, tool_args=tool_args, actor=default_user, environment_variables={}
+ )
+ print(result)
+
+ # add a tool
+ tool = await server.mcp_manager.add_tool_from_mcp_server(created_server.server_name, tool_name, actor=default_user)
+ print(tool)
+ assert tool.name == tool_name
+ assert f"mcp:{created_server.server_name}" in tool.tags, f"Expected tag {f'mcp:{created_server.server_name}'}, got {tool.tags}"
+ print("TAGS", tool.tags)
+
+
+@patch("letta.services.mcp_manager.MCPManager.get_mcp_client")
+async def test_create_mcp_server_with_tools(mock_get_client, server, default_user):
+ """Test that creating an MCP server automatically syncs and persists its tools."""
+ from letta.functions.mcp_client.types import MCPToolHealth
+ from letta.schemas.mcp import MCPServer, MCPServerType, SSEServerConfig
+ from letta.settings import tool_settings
+
+ if tool_settings.mcp_read_from_config:
+ return
+
+ # Create mock tools with different health statuses
+ mock_tools = [
+ MCPTool(
+ name="valid_tool_1",
+ description="A valid tool",
+ inputSchema={
+ "type": "object",
+ "properties": {
+ "param1": {"type": "string"},
+ },
+ "required": ["param1"],
+ },
+ health=MCPToolHealth(status="VALID", reasons=[]),
+ ),
+ MCPTool(
+ name="valid_tool_2",
+ description="Another valid tool",
+ inputSchema={
+ "type": "object",
+ "properties": {
+ "param2": {"type": "number"},
+ },
+ },
+ health=MCPToolHealth(status="VALID", reasons=[]),
+ ),
+ MCPTool(
+ name="invalid_tool",
+ description="An invalid tool that should be skipped",
+ inputSchema={
+ "type": "invalid_type", # Invalid schema
+ },
+ health=MCPToolHealth(status="INVALID", reasons=["Invalid schema type"]),
+ ),
+ MCPTool(
+ name="warning_tool",
+ description="A tool with warnings but should still be persisted",
+ inputSchema={
+ "type": "object",
+ "properties": {},
+ },
+ health=MCPToolHealth(status="WARNING", reasons=["No properties defined"]),
+ ),
+ ]
+
+ # Create mock client
+ mock_client = AsyncMock()
+ mock_client.connect_to_server = AsyncMock()
+ mock_client.list_tools = AsyncMock(return_value=mock_tools)
+ mock_client.cleanup = AsyncMock()
+ mock_get_client.return_value = mock_client
+
+ # Create MCP server config
+ server_name = f"test_server_{uuid.uuid4().hex[:8]}"
+ server_url = "https://test-with-tools.example.com/sse"
+ mcp_server = MCPServer(server_name=server_name, server_type=MCPServerType.SSE, server_url=server_url)
+
+ # Create server with tools using the new method
+ created_server = await server.mcp_manager.create_mcp_server_with_tools(mcp_server, actor=default_user)
+
+ # Verify server was created
+ assert created_server.server_name == server_name
+ assert created_server.server_type == MCPServerType.SSE
+ assert created_server.server_url == server_url
+
+ # Verify tools were persisted (all except the invalid one)
+ # Get all tools and filter by checking metadata
+ all_tools = await server.tool_manager.list_tools_async(
+ actor=default_user, names=["valid_tool_1", "valid_tool_2", "warning_tool", "invalid_tool"]
+ )
+
+ # Filter tools that belong to our MCP server
+ persisted_tools = [
+ tool
+ for tool in all_tools
+ if tool.metadata_
+ and MCP_TOOL_TAG_NAME_PREFIX in tool.metadata_
+ and tool.metadata_[MCP_TOOL_TAG_NAME_PREFIX].get("server_name") == server_name
+ ]
+
+ # Should have 3 tools (2 valid + 1 warning, but not the invalid one)
+ assert len(persisted_tools) == 3, f"Expected 3 tools, got {len(persisted_tools)}"
+
+ # Check tool names
+ tool_names = {tool.name for tool in persisted_tools}
+ assert "valid_tool_1" in tool_names
+ assert "valid_tool_2" in tool_names
+ assert "warning_tool" in tool_names
+ assert "invalid_tool" not in tool_names # Invalid tool should be filtered out
+
+ # Verify each tool has correct metadata
+ for tool in persisted_tools:
+ assert tool.metadata_ is not None
+ assert MCP_TOOL_TAG_NAME_PREFIX in tool.metadata_
+ assert tool.metadata_[MCP_TOOL_TAG_NAME_PREFIX]["server_name"] == server_name
+ assert tool.metadata_[MCP_TOOL_TAG_NAME_PREFIX]["server_id"] == created_server.id
+ assert tool.tool_type == ToolType.EXTERNAL_MCP
+
+ # Clean up - delete the server
+ await server.mcp_manager.delete_mcp_server_by_id(created_server.id, actor=default_user)
+
+ # Verify tools were also deleted (cascade) by trying to get them again
+ remaining_tools = await server.tool_manager.list_tools_async(actor=default_user, names=["valid_tool_1", "valid_tool_2", "warning_tool"])
+
+ # Filter to see if any still belong to our deleted server
+ remaining_mcp_tools = [
+ tool
+ for tool in remaining_tools
+ if tool.metadata_
+ and MCP_TOOL_TAG_NAME_PREFIX in tool.metadata_
+ and tool.metadata_[MCP_TOOL_TAG_NAME_PREFIX].get("server_name") == server_name
+ ]
+ assert len(remaining_mcp_tools) == 0, "Tools should be deleted when server is deleted"
+
+
+@patch("letta.services.mcp_manager.MCPManager.get_mcp_client")
+async def test_create_mcp_server_with_tools_connection_failure(mock_get_client, server, default_user):
+ """Test that MCP server creation succeeds even when tool sync fails (optimistic approach)."""
+ from letta.schemas.mcp import MCPServer, MCPServerType
+ from letta.settings import tool_settings
+
+ if tool_settings.mcp_read_from_config:
+ return
+
+ # Create mock client that fails to connect
+ mock_client = AsyncMock()
+ mock_client.connect_to_server = AsyncMock(side_effect=Exception("Connection failed"))
+ mock_client.cleanup = AsyncMock()
+ mock_get_client.return_value = mock_client
+
+ # Create MCP server config
+ server_name = f"test_server_fail_{uuid.uuid4().hex[:8]}"
+ server_url = "https://test-fail.example.com/sse"
+ mcp_server = MCPServer(server_name=server_name, server_type=MCPServerType.SSE, server_url=server_url)
+
+ # Create server with tools - should succeed despite connection failure
+ created_server = await server.mcp_manager.create_mcp_server_with_tools(mcp_server, actor=default_user)
+
+ # Verify server was created successfully
+ assert created_server.server_name == server_name
+ assert created_server.server_type == MCPServerType.SSE
+ assert created_server.server_url == server_url
+
+ # Verify no tools were persisted (due to connection failure)
+ # Try to get tools by the names we would have expected
+ all_tools = await server.tool_manager.list_tools_async(
+ actor=default_user,
+ names=["tool1", "tool2", "tool3"], # Generic names since we don't know what tools would have been listed
+ )
+
+ # Filter to see if any belong to our server (there shouldn't be any)
+ persisted_tools = [
+ tool
+ for tool in all_tools
+ if tool.metadata_
+ and MCP_TOOL_TAG_NAME_PREFIX in tool.metadata_
+ and tool.metadata_[MCP_TOOL_TAG_NAME_PREFIX].get("server_name") == server_name
+ ]
+ assert len(persisted_tools) == 0, "No tools should be persisted when connection fails"
+
+ # Clean up
+ await server.mcp_manager.delete_mcp_server_by_id(created_server.id, actor=default_user)
+
+
+async def test_get_mcp_servers_by_ids(server, default_user):
+ from letta.schemas.mcp import MCPServer, MCPServerType, SSEServerConfig, StdioServerConfig
+ from letta.settings import tool_settings
+
+ if tool_settings.mcp_read_from_config:
+ return
+
+ # Create multiple MCP servers for testing
+ servers_data = [
+ {
+ "name": "test_server_1",
+ "config": StdioServerConfig(
+ server_name="test_server_1", type=MCPServerType.STDIO, command="echo 'test1'", args=["arg1"], env={"ENV1": "value1"}
+ ),
+ "type": MCPServerType.STDIO,
+ },
+ {
+ "name": "test_server_2",
+ "config": SSEServerConfig(server_name="test_server_2", server_url="https://test2.example.com/sse"),
+ "type": MCPServerType.SSE,
+ },
+ {
+ "name": "test_server_3",
+ "config": SSEServerConfig(server_name="test_server_3", server_url="https://test3.example.com/mcp"),
+ "type": MCPServerType.STREAMABLE_HTTP,
+ },
+ ]
+
+ created_servers = []
+ for server_data in servers_data:
+ if server_data["type"] == MCPServerType.STDIO:
+ mcp_server = MCPServer(server_name=server_data["name"], server_type=server_data["type"], stdio_config=server_data["config"])
+ else:
+ mcp_server = MCPServer(
+ server_name=server_data["name"], server_type=server_data["type"], server_url=server_data["config"].server_url
+ )
+
+ created = await server.mcp_manager.create_or_update_mcp_server(mcp_server, actor=default_user)
+ created_servers.append(created)
+
+ # Test fetching multiple servers by IDs
+ server_ids = [s.id for s in created_servers]
+ fetched_servers = await server.mcp_manager.get_mcp_servers_by_ids(server_ids, actor=default_user)
+
+ assert len(fetched_servers) == len(created_servers)
+ fetched_ids = {s.id for s in fetched_servers}
+ expected_ids = {s.id for s in created_servers}
+ assert fetched_ids == expected_ids
+
+ # Test fetching subset of servers
+ subset_ids = server_ids[:2]
+ subset_servers = await server.mcp_manager.get_mcp_servers_by_ids(subset_ids, actor=default_user)
+ assert len(subset_servers) == 2
+ assert all(s.id in subset_ids for s in subset_servers)
+
+ # Test fetching with empty list
+ empty_result = await server.mcp_manager.get_mcp_servers_by_ids([], actor=default_user)
+ assert empty_result == []
+
+ # Test fetching with non-existent ID mixed with valid IDs
+ mixed_ids = [server_ids[0], "non-existent-id", server_ids[1]]
+ mixed_result = await server.mcp_manager.get_mcp_servers_by_ids(mixed_ids, actor=default_user)
+ # Should only return the existing servers
+ assert len(mixed_result) == 2
+ assert all(s.id in server_ids for s in mixed_result)
+
+ # Test that servers from different organizations are not returned
+ # This would require creating another user/org, but for now we'll just verify
+ # that the function respects the actor's organization
+ all_servers = await server.mcp_manager.list_mcp_servers(actor=default_user)
+ all_server_ids = [s.id for s in all_servers]
+ bulk_fetched = await server.mcp_manager.get_mcp_servers_by_ids(all_server_ids, actor=default_user)
+
+ # All fetched servers should belong to the same organization
+ assert all(s.organization_id == default_user.organization_id for s in bulk_fetched)
+
+
+# Additional MCPManager OAuth session tests
+async def test_mcp_server_deletion_cascades_oauth_sessions(server, default_organization, default_user):
+ """Deleting an MCP server deletes associated OAuth sessions (same user + URL)."""
+
+ from letta.schemas.mcp import MCPOAuthSessionCreate, MCPServer as PydanticMCPServer, MCPServerType
+
+ test_server_url = "https://test.example.com/mcp"
+
+ # Create orphaned OAuth sessions (no server id) for same user and URL
+ created_session_ids: list[str] = []
+ for i in range(3):
+ session = await server.mcp_manager.create_oauth_session(
+ MCPOAuthSessionCreate(
+ server_url=test_server_url,
+ server_name=f"test_mcp_server_{i}",
+ user_id=default_user.id,
+ organization_id=default_organization.id,
+ ),
+ actor=default_user,
+ )
+ created_session_ids.append(session.id)
+
+ # Create the MCP server with the same URL
+ created_server = await server.mcp_manager.create_mcp_server(
+ PydanticMCPServer(
+ server_name=f"test_mcp_server_{str(uuid.uuid4().hex[:8])}", # ensure unique name
+ server_type=MCPServerType.SSE,
+ server_url=test_server_url,
+ organization_id=default_organization.id,
+ ),
+ actor=default_user,
+ )
+
+ # Now delete the server via manager
+ await server.mcp_manager.delete_mcp_server_by_id(created_server.id, actor=default_user)
+
+ # Verify all sessions are gone
+ for sid in created_session_ids:
+ session = await server.mcp_manager.get_oauth_session_by_id(sid, actor=default_user)
+ assert session is None, f"OAuth session {sid} should be deleted"
+
+
+async def test_oauth_sessions_with_different_url_persist(server, default_organization, default_user):
+ """Sessions with different URL should not be deleted when deleting the server for another URL."""
+
+ from letta.schemas.mcp import MCPOAuthSessionCreate, MCPServer as PydanticMCPServer, MCPServerType
+
+ server_url = "https://test.example.com/mcp"
+ other_url = "https://other.example.com/mcp"
+
+ # Create a session for other_url (should persist)
+ other_session = await server.mcp_manager.create_oauth_session(
+ MCPOAuthSessionCreate(
+ server_url=other_url,
+ server_name="standalone_oauth",
+ user_id=default_user.id,
+ organization_id=default_organization.id,
+ ),
+ actor=default_user,
+ )
+
+ # Create the MCP server at server_url
+ created_server = await server.mcp_manager.create_mcp_server(
+ PydanticMCPServer(
+ server_name=f"test_mcp_server_{str(uuid.uuid4().hex[:8])}",
+ server_type=MCPServerType.SSE,
+ server_url=server_url,
+ organization_id=default_organization.id,
+ ),
+ actor=default_user,
+ )
+
+ # Delete the server at server_url
+ await server.mcp_manager.delete_mcp_server_by_id(created_server.id, actor=default_user)
+
+ # Verify the session at other_url still exists
+ persisted = await server.mcp_manager.get_oauth_session_by_id(other_session.id, actor=default_user)
+ assert persisted is not None, "OAuth session with different URL should persist"
+
+
+async def test_mcp_server_creation_links_orphaned_sessions(server, default_organization, default_user):
+ """Creating a server should link any existing orphaned sessions (same user + URL)."""
+
+ from letta.schemas.mcp import MCPOAuthSessionCreate, MCPServer as PydanticMCPServer, MCPServerType
+
+ server_url = "https://test-atomic-create.example.com/mcp"
+
+ # Pre-create orphaned sessions (no server_id) for same user + URL
+ orphaned_ids: list[str] = []
+ for i in range(3):
+ session = await server.mcp_manager.create_oauth_session(
+ MCPOAuthSessionCreate(
+ server_url=server_url,
+ server_name=f"atomic_session_{i}",
+ user_id=default_user.id,
+ organization_id=default_organization.id,
+ ),
+ actor=default_user,
+ )
+ orphaned_ids.append(session.id)
+
+ # Create server
+ created_server = await server.mcp_manager.create_mcp_server(
+ PydanticMCPServer(
+ server_name=f"test_atomic_server_{str(uuid.uuid4().hex[:8])}",
+ server_type=MCPServerType.SSE,
+ server_url=server_url,
+ organization_id=default_organization.id,
+ ),
+ actor=default_user,
+ )
+
+ # Sessions should still be retrievable via manager API
+ for sid in orphaned_ids:
+ s = await server.mcp_manager.get_oauth_session_by_id(sid, actor=default_user)
+ assert s is not None
+
+ # Indirect verification: deleting the server removes sessions for that URL+user
+ await server.mcp_manager.delete_mcp_server_by_id(created_server.id, actor=default_user)
+ for sid in orphaned_ids:
+ assert await server.mcp_manager.get_oauth_session_by_id(sid, actor=default_user) is None
+
+
+async def test_mcp_server_delete_removes_all_sessions_for_url_and_user(server, default_organization, default_user):
+ """Deleting a server removes both linked and orphaned sessions for same user+URL."""
+
+ from letta.schemas.mcp import MCPOAuthSessionCreate, MCPServer as PydanticMCPServer, MCPServerType
+
+ server_url = "https://test-atomic-cleanup.example.com/mcp"
+
+ # Create orphaned session
+ orphaned = await server.mcp_manager.create_oauth_session(
+ MCPOAuthSessionCreate(
+ server_url=server_url,
+ server_name="orphaned",
+ user_id=default_user.id,
+ organization_id=default_organization.id,
+ ),
+ actor=default_user,
+ )
+
+ # Create server
+ created_server = await server.mcp_manager.create_mcp_server(
+ PydanticMCPServer(
+ server_name=f"cleanup_server_{str(uuid.uuid4().hex[:8])}",
+ server_type=MCPServerType.SSE,
+ server_url=server_url,
+ organization_id=default_organization.id,
+ ),
+ actor=default_user,
+ )
+
+ # Delete server
+ await server.mcp_manager.delete_mcp_server_by_id(created_server.id, actor=default_user)
+
+ # Both orphaned and any linked sessions for that URL+user should be gone
+ assert await server.mcp_manager.get_oauth_session_by_id(orphaned.id, actor=default_user) is None
+
+
+async def test_mcp_server_resync_tools(server, default_user, default_organization):
+ """Test that resyncing MCP server tools correctly handles added, deleted, and updated tools."""
+ from unittest.mock import AsyncMock, MagicMock, patch
+
+ from letta.functions.mcp_client.types import MCPTool, MCPToolHealth
+ from letta.schemas.mcp import MCPServer as PydanticMCPServer, MCPServerType
+ from letta.schemas.tool import ToolCreate
+
+ # Create MCP server
+ mcp_server = await server.mcp_manager.create_mcp_server(
+ PydanticMCPServer(
+ server_name=f"test_resync_{uuid.uuid4().hex[:8]}",
+ server_type=MCPServerType.SSE,
+ server_url="https://test-resync.example.com/mcp",
+ organization_id=default_organization.id,
+ ),
+ actor=default_user,
+ )
+ mcp_server_id = mcp_server.id
+
+ try:
+ # Create initial persisted tools (simulating previously added tools)
+ # Use sync method like in the existing mcp_tool fixture
+ tool1_create = ToolCreate.from_mcp(
+ mcp_server_name=mcp_server.server_name,
+ mcp_tool=MCPTool(
+ name="tool1",
+ description="Tool 1",
+ inputSchema={"type": "object", "properties": {"param1": {"type": "string"}}},
+ ),
+ )
+ tool1 = await server.tool_manager.create_or_update_mcp_tool_async(
+ tool_create=tool1_create,
+ mcp_server_name=mcp_server.server_name,
+ mcp_server_id=mcp_server_id,
+ actor=default_user,
+ )
+
+ tool2_create = ToolCreate.from_mcp(
+ mcp_server_name=mcp_server.server_name,
+ mcp_tool=MCPTool(
+ name="tool2",
+ description="Tool 2 to be deleted",
+ inputSchema={"type": "object", "properties": {"param2": {"type": "number"}}},
+ ),
+ )
+ tool2 = await server.tool_manager.create_or_update_mcp_tool_async(
+ tool_create=tool2_create,
+ mcp_server_name=mcp_server.server_name,
+ mcp_server_id=mcp_server_id,
+ actor=default_user,
+ )
+
+ # Mock the list_mcp_server_tools to return updated tools from server
+ # tool1 is updated, tool2 is deleted, tool3 is added
+ updated_tools = [
+ MCPTool(
+ name="tool1",
+ description="Tool 1 Updated",
+ inputSchema={"type": "object", "properties": {"param1": {"type": "string"}, "param1b": {"type": "boolean"}}},
+ health=MCPToolHealth(status="VALID", reasons=[]),
+ ),
+ MCPTool(
+ name="tool3",
+ description="Tool 3 New",
+ inputSchema={"type": "object", "properties": {"param3": {"type": "array"}}},
+ health=MCPToolHealth(status="VALID", reasons=[]),
+ ),
+ ]
+
+ with patch.object(server.mcp_manager, "list_mcp_server_tools", new_callable=AsyncMock) as mock_list_tools:
+ mock_list_tools.return_value = updated_tools
+
+ # Run resync
+ result = await server.mcp_manager.resync_mcp_server_tools(
+ mcp_server_name=mcp_server.server_name,
+ actor=default_user,
+ )
+
+ # Verify the resync result
+ assert len(result.deleted) == 1
+ assert "tool2" in result.deleted
+
+ assert len(result.updated) == 1
+ assert "tool1" in result.updated
+
+ assert len(result.added) == 1
+ assert "tool3" in result.added
+
+ # Verify tool2 was actually deleted
+ try:
+ deleted_tool = await server.tool_manager.get_tool_by_id_async(tool_id=tool2.id, actor=default_user)
+ assert False, "Tool2 should have been deleted"
+ except Exception:
+ pass # Expected - tool should be deleted
+
+ # Verify tool1 was updated with new schema
+ updated_tool1 = await server.tool_manager.get_tool_by_id_async(tool_id=tool1.id, actor=default_user)
+ assert "param1b" in updated_tool1.json_schema["parameters"]["properties"]
+
+ # Verify tool3 was added
+ tools = await server.tool_manager.list_tools_async(actor=default_user, names=["tool3"])
+ assert len(tools) == 1
+ assert tools[0].name == "tool3"
+
+ finally:
+ # Clean up
+ await server.mcp_manager.delete_mcp_server_by_id(mcp_server_id, actor=default_user)
diff --git a/tests/managers/test_message_manager.py b/tests/managers/test_message_manager.py
new file mode 100644
index 000000000..cd1a6e027
--- /dev/null
+++ b/tests/managers/test_message_manager.py
@@ -0,0 +1,786 @@
+import json
+import logging
+import os
+import random
+import re
+import string
+import time
+import uuid
+from datetime import datetime, timedelta, timezone
+from typing import List
+from unittest.mock import AsyncMock, Mock, patch
+
+import pytest
+from _pytest.python_api import approx
+from anthropic.types.beta import BetaMessage
+from anthropic.types.beta.messages import BetaMessageBatchIndividualResponse, BetaMessageBatchSucceededResult
+
+# Import shared fixtures and constants from conftest
+from conftest import (
+ CREATE_DELAY_SQLITE,
+ DEFAULT_EMBEDDING_CONFIG,
+ USING_SQLITE,
+)
+from openai.types.chat.chat_completion_message_tool_call import ChatCompletionMessageToolCall as OpenAIToolCall, Function as OpenAIFunction
+from sqlalchemy import func, select
+from sqlalchemy.exc import IntegrityError, InvalidRequestError
+from sqlalchemy.orm.exc import StaleDataError
+
+from letta.config import LettaConfig
+from letta.constants import (
+ BASE_MEMORY_TOOLS,
+ BASE_SLEEPTIME_TOOLS,
+ BASE_TOOLS,
+ BASE_VOICE_SLEEPTIME_CHAT_TOOLS,
+ BASE_VOICE_SLEEPTIME_TOOLS,
+ BUILTIN_TOOLS,
+ DEFAULT_ORG_ID,
+ DEFAULT_ORG_NAME,
+ FILES_TOOLS,
+ LETTA_TOOL_EXECUTION_DIR,
+ LETTA_TOOL_SET,
+ LOCAL_ONLY_MULTI_AGENT_TOOLS,
+ MCP_TOOL_TAG_NAME_PREFIX,
+ MULTI_AGENT_TOOLS,
+)
+from letta.data_sources.redis_client import NoopAsyncRedisClient, get_redis_client
+from letta.errors import LettaAgentNotFoundError
+from letta.functions.functions import derive_openai_json_schema, parse_source_code
+from letta.functions.mcp_client.types import MCPTool
+from letta.helpers import ToolRulesSolver
+from letta.helpers.datetime_helpers import AsyncTimer
+from letta.jobs.types import ItemUpdateInfo, RequestStatusUpdateInfo, StepStatusUpdateInfo
+from letta.orm import Base, Block
+from letta.orm.block_history import BlockHistory
+from letta.orm.errors import NoResultFound, UniqueConstraintViolationError
+from letta.orm.file import FileContent as FileContentModel, FileMetadata as FileMetadataModel
+from letta.schemas.agent import CreateAgent, UpdateAgent
+from letta.schemas.block import Block as PydanticBlock, BlockUpdate, CreateBlock
+from letta.schemas.embedding_config import EmbeddingConfig
+from letta.schemas.enums import (
+ ActorType,
+ AgentStepStatus,
+ FileProcessingStatus,
+ JobStatus,
+ JobType,
+ MessageRole,
+ ProviderType,
+ SandboxType,
+ StepStatus,
+ TagMatchMode,
+ ToolType,
+ VectorDBProvider,
+)
+from letta.schemas.environment_variables import SandboxEnvironmentVariableCreate, SandboxEnvironmentVariableUpdate
+from letta.schemas.file import FileMetadata, FileMetadata as PydanticFileMetadata
+from letta.schemas.identity import IdentityCreate, IdentityProperty, IdentityPropertyType, IdentityType, IdentityUpdate, IdentityUpsert
+from letta.schemas.job import BatchJob, Job, Job as PydanticJob, JobUpdate, LettaRequestConfig
+from letta.schemas.letta_message import UpdateAssistantMessage, UpdateReasoningMessage, UpdateSystemMessage, UpdateUserMessage
+from letta.schemas.letta_message_content import TextContent
+from letta.schemas.letta_stop_reason import LettaStopReason, StopReasonType
+from letta.schemas.llm_batch_job import AgentStepState, LLMBatchItem
+from letta.schemas.llm_config import LLMConfig
+from letta.schemas.message import Message as PydanticMessage, MessageCreate, MessageUpdate
+from letta.schemas.openai.chat_completion_response import UsageStatistics
+from letta.schemas.organization import Organization, Organization as PydanticOrganization, OrganizationUpdate
+from letta.schemas.passage import Passage as PydanticPassage
+from letta.schemas.pip_requirement import PipRequirement
+from letta.schemas.run import Run as PydanticRun
+from letta.schemas.sandbox_config import E2BSandboxConfig, LocalSandboxConfig, SandboxConfigCreate, SandboxConfigUpdate
+from letta.schemas.source import Source as PydanticSource, SourceUpdate
+from letta.schemas.tool import Tool as PydanticTool, ToolCreate, ToolUpdate
+from letta.schemas.tool_rule import InitToolRule
+from letta.schemas.user import User as PydanticUser, UserUpdate
+from letta.server.db import db_registry
+from letta.server.server import SyncServer
+from letta.services.block_manager import BlockManager
+from letta.services.helpers.agent_manager_helper import calculate_base_tools, calculate_multi_agent_tools, validate_agent_exists_async
+from letta.services.step_manager import FeedbackType
+from letta.settings import settings, tool_settings
+from letta.utils import calculate_file_defaults_based_on_context_window
+from tests.helpers.utils import comprehensive_agent_checks, validate_context_window_overview
+from tests.utils import random_string
+
+# ======================================================================================================================
+# AgentManager Tests - Messages Relationship
+# ======================================================================================================================
+
+
+async def test_reset_messages_no_messages(server: SyncServer, sarah_agent, default_user):
+ """
+ Test that resetting messages on an agent that has zero messages
+ does not fail and clears out message_ids if somehow it's non-empty.
+ """
+ assert len(sarah_agent.message_ids) == 4
+ og_message_ids = sarah_agent.message_ids
+
+ # Reset messages
+ reset_agent = await server.agent_manager.reset_messages_async(agent_id=sarah_agent.id, actor=default_user)
+ assert len(reset_agent.message_ids) == 1
+ assert og_message_ids[0] == reset_agent.message_ids[0]
+ # Double check that physically no messages exist
+ assert await server.message_manager.size_async(agent_id=sarah_agent.id, actor=default_user) == 1
+
+
+async def test_reset_messages_default_messages(server: SyncServer, sarah_agent, default_user):
+ """
+ Test that resetting messages on an agent that has zero messages
+ does not fail and clears out message_ids if somehow it's non-empty.
+ """
+ assert len(sarah_agent.message_ids) == 4
+ og_message_ids = sarah_agent.message_ids
+
+ # Reset messages
+ reset_agent = await server.agent_manager.reset_messages_async(
+ agent_id=sarah_agent.id, actor=default_user, add_default_initial_messages=True
+ )
+ assert len(reset_agent.message_ids) == 4
+ assert og_message_ids[0] == reset_agent.message_ids[0]
+ assert og_message_ids[1] != reset_agent.message_ids[1]
+ assert og_message_ids[2] != reset_agent.message_ids[2]
+ assert og_message_ids[3] != reset_agent.message_ids[3]
+ # Double check that physically no messages exist
+ assert await server.message_manager.size_async(agent_id=sarah_agent.id, actor=default_user) == 4
+
+
+async def test_reset_messages_with_existing_messages(server: SyncServer, sarah_agent, default_user):
+ """
+ Test that resetting messages on an agent with actual messages
+ deletes them from the database and clears message_ids.
+ """
+ # 1. Create multiple messages for the agent
+ msg1 = await server.message_manager.create_many_messages_async(
+ [
+ PydanticMessage(
+ agent_id=sarah_agent.id,
+ role="user",
+ content=[TextContent(text="Hello, Sarah!")],
+ ),
+ ],
+ actor=default_user,
+ )
+ msg1 = msg1[0]
+
+ msg2 = await server.message_manager.create_many_messages_async(
+ [
+ PydanticMessage(
+ agent_id=sarah_agent.id,
+ role="assistant",
+ content=[TextContent(text="Hello, user!")],
+ ),
+ ],
+ actor=default_user,
+ )
+ msg2 = msg2[0]
+
+ # Verify the messages were created
+ agent_before = await server.agent_manager.get_agent_by_id_async(sarah_agent.id, default_user)
+ # This is 4 because creating the message does not necessarily add it to the in context message ids
+ assert len(agent_before.message_ids) == 4
+ assert await server.message_manager.size_async(agent_id=sarah_agent.id, actor=default_user) == 6
+
+ # 2. Reset all messages
+ reset_agent = await server.agent_manager.reset_messages_async(agent_id=sarah_agent.id, actor=default_user)
+
+ # 3. Verify the agent now has zero message_ids
+ assert len(reset_agent.message_ids) == 1
+
+ # 4. Verify the messages are physically removed
+ assert await server.message_manager.size_async(agent_id=sarah_agent.id, actor=default_user) == 1
+
+
+async def test_reset_messages_idempotency(server: SyncServer, sarah_agent, default_user):
+ """
+ Test that calling reset_messages multiple times has no adverse effect.
+ """
+ # Clear messages first
+ await server.message_manager.delete_messages_by_ids_async(message_ids=sarah_agent.message_ids[1:], actor=default_user)
+
+ # Create a single message
+ await server.message_manager.create_many_messages_async(
+ [
+ PydanticMessage(
+ agent_id=sarah_agent.id,
+ role="user",
+ content=[TextContent(text="Hello, Sarah!")],
+ ),
+ ],
+ actor=default_user,
+ )
+ # First reset
+ reset_agent = await server.agent_manager.reset_messages_async(agent_id=sarah_agent.id, actor=default_user)
+ assert len(reset_agent.message_ids) == 1
+ assert await server.message_manager.size_async(agent_id=sarah_agent.id, actor=default_user) == 1
+
+ # Second reset should do nothing new
+ reset_agent_again = await server.agent_manager.reset_messages_async(agent_id=sarah_agent.id, actor=default_user)
+ assert len(reset_agent.message_ids) == 1
+ assert await server.message_manager.size_async(agent_id=sarah_agent.id, actor=default_user) == 1
+
+
+async def test_reset_messages_preserves_system_message_id(server: SyncServer, sarah_agent, default_user):
+ """
+ Test that resetting messages preserves the original system message ID.
+ """
+ # Get the original system message ID
+ original_agent = await server.agent_manager.get_agent_by_id_async(sarah_agent.id, default_user)
+ original_system_message_id = original_agent.message_ids[0]
+
+ # Add some user messages
+ await server.message_manager.create_many_messages_async(
+ [
+ PydanticMessage(
+ agent_id=sarah_agent.id,
+ role="user",
+ content=[TextContent(text="Hello!")],
+ ),
+ ],
+ actor=default_user,
+ )
+
+ # Reset messages
+ reset_agent = await server.agent_manager.reset_messages_async(agent_id=sarah_agent.id, actor=default_user)
+
+ # Verify the system message ID is preserved
+ assert len(reset_agent.message_ids) == 1
+ assert reset_agent.message_ids[0] == original_system_message_id
+
+ # Verify the system message still exists in the database
+ system_message = await server.message_manager.get_message_by_id_async(message_id=original_system_message_id, actor=default_user)
+ assert system_message.role == "system"
+
+
+async def test_reset_messages_preserves_system_message_content(server: SyncServer, sarah_agent, default_user):
+ """
+ Test that resetting messages preserves the original system message content.
+ """
+ # Get the original system message
+ original_agent = await server.agent_manager.get_agent_by_id_async(sarah_agent.id, default_user)
+ original_system_message = await server.message_manager.get_message_by_id_async(
+ message_id=original_agent.message_ids[0], actor=default_user
+ )
+
+ # Add some messages and reset
+ await server.message_manager.create_many_messages_async(
+ [
+ PydanticMessage(
+ agent_id=sarah_agent.id,
+ role="user",
+ content=[TextContent(text="Hello!")],
+ ),
+ ],
+ actor=default_user,
+ )
+
+ reset_agent = await server.agent_manager.reset_messages_async(agent_id=sarah_agent.id, actor=default_user)
+
+ # Verify the system message content is unchanged
+ preserved_system_message = await server.message_manager.get_message_by_id_async(
+ message_id=reset_agent.message_ids[0], actor=default_user
+ )
+
+ assert preserved_system_message.content == original_system_message.content
+ assert preserved_system_message.role == "system"
+ assert preserved_system_message.id == original_system_message.id
+
+
+async def test_modify_letta_message(server: SyncServer, sarah_agent, default_user):
+ """
+ Test updating a message.
+ """
+
+ messages = await server.message_manager.list_messages_for_agent_async(agent_id=sarah_agent.id, actor=default_user)
+ letta_messages = PydanticMessage.to_letta_messages_from_list(messages=messages)
+
+ system_message = [msg for msg in letta_messages if msg.message_type == "system_message"][0]
+ assistant_message = [msg for msg in letta_messages if msg.message_type == "assistant_message"][0]
+ user_message = [msg for msg in letta_messages if msg.message_type == "user_message"][0]
+ reasoning_message = [msg for msg in letta_messages if msg.message_type == "reasoning_message"][0]
+
+ # user message
+ update_user_message = UpdateUserMessage(content="Hello, Sarah!")
+ original_user_message = await server.message_manager.get_message_by_id_async(message_id=user_message.id, actor=default_user)
+ assert original_user_message.content[0].text != update_user_message.content
+ await server.message_manager.update_message_by_letta_message_async(
+ message_id=user_message.id, letta_message_update=update_user_message, actor=default_user
+ )
+ updated_user_message = await server.message_manager.get_message_by_id_async(message_id=user_message.id, actor=default_user)
+ assert updated_user_message.content[0].text == update_user_message.content
+
+ # system message
+ update_system_message = UpdateSystemMessage(content="You are a friendly assistant!")
+ original_system_message = await server.message_manager.get_message_by_id_async(message_id=system_message.id, actor=default_user)
+ assert original_system_message.content[0].text != update_system_message.content
+ await server.message_manager.update_message_by_letta_message_async(
+ message_id=system_message.id, letta_message_update=update_system_message, actor=default_user
+ )
+ updated_system_message = await server.message_manager.get_message_by_id_async(message_id=system_message.id, actor=default_user)
+ assert updated_system_message.content[0].text == update_system_message.content
+
+ # reasoning message
+ update_reasoning_message = UpdateReasoningMessage(reasoning="I am thinking")
+ original_reasoning_message = await server.message_manager.get_message_by_id_async(message_id=reasoning_message.id, actor=default_user)
+ assert original_reasoning_message.content[0].text != update_reasoning_message.reasoning
+ await server.message_manager.update_message_by_letta_message_async(
+ message_id=reasoning_message.id, letta_message_update=update_reasoning_message, actor=default_user
+ )
+ updated_reasoning_message = await server.message_manager.get_message_by_id_async(message_id=reasoning_message.id, actor=default_user)
+ assert updated_reasoning_message.content[0].text == update_reasoning_message.reasoning
+
+ # assistant message
+ def parse_send_message(tool_call):
+ import json
+
+ function_call = tool_call.function
+ arguments = json.loads(function_call.arguments)
+ return arguments["message"]
+
+ update_assistant_message = UpdateAssistantMessage(content="I am an agent!")
+ original_assistant_message = await server.message_manager.get_message_by_id_async(message_id=assistant_message.id, actor=default_user)
+ print("ORIGINAL", original_assistant_message.tool_calls)
+ print("MESSAGE", parse_send_message(original_assistant_message.tool_calls[0]))
+ assert parse_send_message(original_assistant_message.tool_calls[0]) != update_assistant_message.content
+ await server.message_manager.update_message_by_letta_message_async(
+ message_id=assistant_message.id, letta_message_update=update_assistant_message, actor=default_user
+ )
+ updated_assistant_message = await server.message_manager.get_message_by_id_async(message_id=assistant_message.id, actor=default_user)
+ print("UPDATED", updated_assistant_message.tool_calls)
+ print("MESSAGE", parse_send_message(updated_assistant_message.tool_calls[0]))
+ assert parse_send_message(updated_assistant_message.tool_calls[0]) == update_assistant_message.content
+
+ # TODO: tool calls/responses
+
+
+async def test_message_create(server: SyncServer, hello_world_message_fixture, default_user):
+ """Test creating a message using hello_world_message_fixture fixture"""
+ assert hello_world_message_fixture.id is not None
+ assert hello_world_message_fixture.content[0].text == "Hello, world!"
+ assert hello_world_message_fixture.role == "user"
+
+ # Verify we can retrieve it
+ retrieved = await server.message_manager.get_message_by_id_async(
+ hello_world_message_fixture.id,
+ actor=default_user,
+ )
+ assert retrieved is not None
+ assert retrieved.id == hello_world_message_fixture.id
+ assert retrieved.content[0].text == hello_world_message_fixture.content[0].text
+ assert retrieved.role == hello_world_message_fixture.role
+
+
+async def test_message_get_by_id(server: SyncServer, hello_world_message_fixture, default_user):
+ """Test retrieving a message by ID"""
+ retrieved = await server.message_manager.get_message_by_id_async(hello_world_message_fixture.id, actor=default_user)
+ assert retrieved is not None
+ assert retrieved.id == hello_world_message_fixture.id
+ assert retrieved.content[0].text == hello_world_message_fixture.content[0].text
+
+
+async def test_message_update(server: SyncServer, hello_world_message_fixture, default_user, other_user):
+ """Test updating a message"""
+ new_text = "Updated text"
+ updated = await server.message_manager.update_message_by_id_async(
+ hello_world_message_fixture.id, MessageUpdate(content=new_text), actor=other_user
+ )
+ assert updated is not None
+ assert updated.content[0].text == new_text
+ retrieved = await server.message_manager.get_message_by_id_async(hello_world_message_fixture.id, actor=default_user)
+ assert retrieved.content[0].text == new_text
+
+ # Assert that orm metadata fields are populated
+ assert retrieved.created_by_id == default_user.id
+ assert retrieved.last_updated_by_id == other_user.id
+
+
+async def test_message_delete(server: SyncServer, hello_world_message_fixture, default_user):
+ """Test deleting a message"""
+ await server.message_manager.delete_message_by_id_async(hello_world_message_fixture.id, actor=default_user)
+ retrieved = await server.message_manager.get_message_by_id_async(hello_world_message_fixture.id, actor=default_user)
+ assert retrieved is None
+
+
+async def test_message_size(server: SyncServer, hello_world_message_fixture, default_user):
+ """Test counting messages with filters"""
+ base_message = hello_world_message_fixture
+
+ # Create additional test messages
+ messages = [
+ PydanticMessage(
+ agent_id=base_message.agent_id,
+ role=base_message.role,
+ content=[TextContent(text=f"Test message {i}")],
+ )
+ for i in range(4)
+ ]
+ await server.message_manager.create_many_messages_async(messages, actor=default_user)
+
+ # Test total count
+ total = await server.message_manager.size_async(actor=default_user, role=MessageRole.user)
+ assert total == 6 # login message + base message + 4 test messages
+ # TODO: change login message to be a system not user message
+
+ # Test count with agent filter
+ agent_count = await server.message_manager.size_async(actor=default_user, agent_id=base_message.agent_id, role=MessageRole.user)
+ assert agent_count == 6
+
+ # Test count with role filter
+ role_count = await server.message_manager.size_async(actor=default_user, role=base_message.role)
+ assert role_count == 6
+
+ # Test count with non-existent filter
+ empty_count = await server.message_manager.size_async(actor=default_user, agent_id="non-existent", role=MessageRole.user)
+ assert empty_count == 0
+
+
+async def create_test_messages(server: SyncServer, base_message: PydanticMessage, default_user) -> list[PydanticMessage]:
+ """Helper function to create test messages for all tests"""
+ messages = [
+ PydanticMessage(
+ agent_id=base_message.agent_id,
+ role=base_message.role,
+ content=[TextContent(text=f"Test message {i}")],
+ )
+ for i in range(4)
+ ]
+ await server.message_manager.create_many_messages_async(messages, actor=default_user)
+ return messages
+
+
+async def test_get_messages_by_ids(server: SyncServer, hello_world_message_fixture, default_user, sarah_agent):
+ """Test basic message listing with limit"""
+ messages = await create_test_messages(server, hello_world_message_fixture, default_user)
+ message_ids = [m.id for m in messages]
+
+ results = await server.message_manager.get_messages_by_ids_async(message_ids=message_ids, actor=default_user)
+ assert sorted(message_ids) == sorted([r.id for r in results])
+
+
+async def test_message_listing_basic(server: SyncServer, hello_world_message_fixture, default_user, sarah_agent):
+ """Test basic message listing with limit"""
+ await create_test_messages(server, hello_world_message_fixture, default_user)
+
+ results = await server.message_manager.list_user_messages_for_agent_async(agent_id=sarah_agent.id, limit=3, actor=default_user)
+ assert len(results) == 3
+
+
+async def test_message_listing_cursor(server: SyncServer, hello_world_message_fixture, default_user, sarah_agent):
+ """Test cursor-based pagination functionality"""
+ await create_test_messages(server, hello_world_message_fixture, default_user)
+
+ # Make sure there are 6 messages
+ assert await server.message_manager.size_async(actor=default_user, role=MessageRole.user) == 6
+
+ # Get first page
+ first_page = await server.message_manager.list_user_messages_for_agent_async(agent_id=sarah_agent.id, actor=default_user, limit=3)
+ assert len(first_page) == 3
+
+ last_id_on_first_page = first_page[-1].id
+
+ # Get second page
+ second_page = await server.message_manager.list_user_messages_for_agent_async(
+ agent_id=sarah_agent.id, actor=default_user, after=last_id_on_first_page, limit=3
+ )
+ assert len(second_page) == 3 # Should have 3 remaining messages
+ assert all(r1.id != r2.id for r1 in first_page for r2 in second_page)
+
+ # Get the middle
+ middle_page = await server.message_manager.list_user_messages_for_agent_async(
+ agent_id=sarah_agent.id, actor=default_user, before=second_page[1].id, after=first_page[0].id
+ )
+ assert len(middle_page) == 3
+ assert middle_page[0].id == first_page[1].id
+ assert middle_page[1].id == first_page[-1].id
+ assert middle_page[-1].id == second_page[0].id
+
+ middle_page_desc = await server.message_manager.list_user_messages_for_agent_async(
+ agent_id=sarah_agent.id, actor=default_user, before=second_page[1].id, after=first_page[0].id, ascending=False
+ )
+ assert len(middle_page_desc) == 3
+ assert middle_page_desc[0].id == second_page[0].id
+ assert middle_page_desc[1].id == first_page[-1].id
+ assert middle_page_desc[-1].id == first_page[1].id
+
+
+async def test_message_listing_filtering(server: SyncServer, hello_world_message_fixture, default_user, sarah_agent):
+ """Test filtering messages by agent ID"""
+ await create_test_messages(server, hello_world_message_fixture, default_user)
+
+ agent_results = await server.message_manager.list_user_messages_for_agent_async(agent_id=sarah_agent.id, actor=default_user, limit=10)
+ assert len(agent_results) == 6 # login message + base message + 4 test messages
+ assert all(msg.agent_id == hello_world_message_fixture.agent_id for msg in agent_results)
+
+
+async def test_message_listing_text_search(server: SyncServer, hello_world_message_fixture, default_user, sarah_agent):
+ """Test searching messages by text content"""
+ await create_test_messages(server, hello_world_message_fixture, default_user)
+
+ search_results = await server.message_manager.list_user_messages_for_agent_async(
+ agent_id=sarah_agent.id, actor=default_user, query_text="Test message", limit=10
+ )
+ assert len(search_results) == 4
+ assert all("Test message" in msg.content[0].text for msg in search_results)
+
+ # Test no results
+ search_results = await server.message_manager.list_user_messages_for_agent_async(
+ agent_id=sarah_agent.id, actor=default_user, query_text="Letta", limit=10
+ )
+ assert len(search_results) == 0
+
+
+async def test_create_many_messages_async_basic(server: SyncServer, sarah_agent, default_user):
+ """Test basic batch creation of messages"""
+ message_manager = server.message_manager
+
+ messages = []
+ for i in range(5):
+ msg = PydanticMessage(
+ agent_id=sarah_agent.id,
+ role=MessageRole.user,
+ content=[TextContent(text=f"Test message {i}")],
+ name=None,
+ tool_calls=None,
+ tool_call_id=None,
+ )
+ messages.append(msg)
+
+ created_messages = await message_manager.create_many_messages_async(pydantic_msgs=messages, actor=default_user)
+
+ assert len(created_messages) == 5
+ for i, msg in enumerate(created_messages):
+ assert msg.id is not None
+ assert msg.content[0].text == f"Test message {i}"
+ assert msg.agent_id == sarah_agent.id
+
+
+async def test_create_many_messages_async_allow_partial_false(server: SyncServer, sarah_agent, default_user):
+ """Test that allow_partial=False (default) fails on duplicate IDs"""
+ message_manager = server.message_manager
+
+ initial_msg = PydanticMessage(
+ agent_id=sarah_agent.id,
+ role=MessageRole.user,
+ content=[TextContent(text="Initial message")],
+ )
+
+ created = await message_manager.create_many_messages_async(pydantic_msgs=[initial_msg], actor=default_user)
+ assert len(created) == 1
+ created_msg = created[0]
+
+ duplicate_msg = PydanticMessage(
+ id=created_msg.id,
+ agent_id=sarah_agent.id,
+ role=MessageRole.user,
+ content=[TextContent(text="Duplicate message")],
+ )
+
+ with pytest.raises(UniqueConstraintViolationError):
+ await message_manager.create_many_messages_async(pydantic_msgs=[duplicate_msg], actor=default_user, allow_partial=False)
+
+
+async def test_create_many_messages_async_allow_partial_true_some_duplicates(server: SyncServer, sarah_agent, default_user):
+ """Test that allow_partial=True handles partial duplicates correctly"""
+ message_manager = server.message_manager
+
+ initial_messages = []
+ for i in range(3):
+ msg = PydanticMessage(
+ agent_id=sarah_agent.id,
+ role=MessageRole.user,
+ content=[TextContent(text=f"Existing message {i}")],
+ )
+ initial_messages.append(msg)
+
+ created_initial = await message_manager.create_many_messages_async(pydantic_msgs=initial_messages, actor=default_user)
+ assert len(created_initial) == 3
+ existing_ids = [msg.id for msg in created_initial]
+
+ mixed_messages = []
+ for created_msg in created_initial:
+ duplicate_msg = PydanticMessage(
+ id=created_msg.id,
+ agent_id=sarah_agent.id,
+ role=MessageRole.user,
+ content=created_msg.content,
+ )
+ mixed_messages.append(duplicate_msg)
+ for i in range(3, 6):
+ msg = PydanticMessage(
+ agent_id=sarah_agent.id,
+ role=MessageRole.user,
+ content=[TextContent(text=f"New message {i}")],
+ )
+ mixed_messages.append(msg)
+
+ result = await message_manager.create_many_messages_async(pydantic_msgs=mixed_messages, actor=default_user, allow_partial=True)
+
+ assert len(result) == 6
+
+ result_ids = {msg.id for msg in result}
+ for existing_id in existing_ids:
+ assert existing_id in result_ids
+
+
+async def test_create_many_messages_async_allow_partial_true_all_duplicates(server: SyncServer, sarah_agent, default_user):
+ """Test that allow_partial=True handles all duplicates correctly"""
+ message_manager = server.message_manager
+
+ initial_messages = []
+ for i in range(3):
+ msg = PydanticMessage(
+ agent_id=sarah_agent.id,
+ role=MessageRole.user,
+ content=[TextContent(text=f"Message {i}")],
+ )
+ initial_messages.append(msg)
+
+ created_initial = await message_manager.create_many_messages_async(pydantic_msgs=initial_messages, actor=default_user)
+ assert len(created_initial) == 3
+
+ duplicate_messages = []
+ for created_msg in created_initial:
+ duplicate_msg = PydanticMessage(
+ id=created_msg.id,
+ agent_id=sarah_agent.id,
+ role=MessageRole.user,
+ content=created_msg.content,
+ )
+ duplicate_messages.append(duplicate_msg)
+
+ result = await message_manager.create_many_messages_async(pydantic_msgs=duplicate_messages, actor=default_user, allow_partial=True)
+
+ assert len(result) == 3
+ for i, msg in enumerate(result):
+ assert msg.id == created_initial[i].id
+ assert msg.content[0].text == f"Message {i}"
+
+
+async def test_create_many_messages_async_empty_list(server: SyncServer, default_user):
+ """Test that empty list returns empty list"""
+ message_manager = server.message_manager
+
+ result = await message_manager.create_many_messages_async(pydantic_msgs=[], actor=default_user)
+
+ assert result == []
+
+
+async def test_check_existing_message_ids(server: SyncServer, sarah_agent, default_user):
+ """Test the check_existing_message_ids convenience function"""
+ message_manager = server.message_manager
+
+ messages = []
+ for i in range(3):
+ msg = PydanticMessage(
+ agent_id=sarah_agent.id,
+ role=MessageRole.user,
+ content=[TextContent(text=f"Message {i}")],
+ )
+ messages.append(msg)
+
+ created_messages = await message_manager.create_many_messages_async(pydantic_msgs=messages, actor=default_user)
+ existing_ids = [msg.id for msg in created_messages]
+
+ non_existent_ids = [f"message-{uuid.uuid4().hex[:8]}" for _ in range(3)]
+ all_ids = existing_ids + non_existent_ids
+
+ existing = await message_manager.check_existing_message_ids(message_ids=all_ids, actor=default_user)
+
+ assert existing == set(existing_ids)
+ for non_existent_id in non_existent_ids:
+ assert non_existent_id not in existing
+
+
+async def test_filter_existing_messages(server: SyncServer, sarah_agent, default_user):
+ """Test the filter_existing_messages helper function"""
+ message_manager = server.message_manager
+
+ initial_messages = []
+ for i in range(3):
+ msg = PydanticMessage(
+ agent_id=sarah_agent.id,
+ role=MessageRole.user,
+ content=[TextContent(text=f"Existing {i}")],
+ )
+ initial_messages.append(msg)
+
+ created_existing = await message_manager.create_many_messages_async(pydantic_msgs=initial_messages, actor=default_user)
+
+ existing_messages = []
+ for created_msg in created_existing:
+ msg = PydanticMessage(
+ id=created_msg.id,
+ agent_id=sarah_agent.id,
+ role=MessageRole.user,
+ content=created_msg.content,
+ )
+ existing_messages.append(msg)
+
+ new_messages = []
+ for i in range(3):
+ msg = PydanticMessage(
+ agent_id=sarah_agent.id,
+ role=MessageRole.user,
+ content=[TextContent(text=f"New {i}")],
+ )
+ new_messages.append(msg)
+
+ all_messages = existing_messages + new_messages
+
+ new_filtered, existing_filtered = await message_manager.filter_existing_messages(messages=all_messages, actor=default_user)
+
+ assert len(new_filtered) == 3
+ assert len(existing_filtered) == 3
+
+ existing_filtered_ids = {msg.id for msg in existing_filtered}
+ for created_msg in created_existing:
+ assert created_msg.id in existing_filtered_ids
+
+ for msg in new_filtered:
+ assert msg.id not in existing_filtered_ids
+
+
+async def test_create_many_messages_async_with_turbopuffer(server: SyncServer, sarah_agent, default_user):
+ """Test batch creation with turbopuffer embedding (if enabled)"""
+ message_manager = server.message_manager
+
+ messages = []
+ for i in range(3):
+ msg = PydanticMessage(
+ agent_id=sarah_agent.id,
+ role=MessageRole.user,
+ content=[TextContent(text=f"Important information about topic {i}")],
+ )
+ messages.append(msg)
+
+ created_messages = await message_manager.create_many_messages_async(
+ pydantic_msgs=messages, actor=default_user, strict_mode=True, project_id="test_project", template_id="test_template"
+ )
+
+ assert len(created_messages) == 3
+ for msg in created_messages:
+ assert msg.id is not None
+ assert msg.agent_id == sarah_agent.id
diff --git a/tests/managers/test_organization_manager.py b/tests/managers/test_organization_manager.py
new file mode 100644
index 000000000..d21d7f9ed
--- /dev/null
+++ b/tests/managers/test_organization_manager.py
@@ -0,0 +1,163 @@
+import json
+import logging
+import os
+import random
+import re
+import string
+import time
+import uuid
+from datetime import datetime, timedelta, timezone
+from typing import List
+from unittest.mock import AsyncMock, Mock, patch
+
+import pytest
+from _pytest.python_api import approx
+from anthropic.types.beta import BetaMessage
+from anthropic.types.beta.messages import BetaMessageBatchIndividualResponse, BetaMessageBatchSucceededResult
+
+# Import shared fixtures and constants from conftest
+from conftest import (
+ CREATE_DELAY_SQLITE,
+ DEFAULT_EMBEDDING_CONFIG,
+ USING_SQLITE,
+)
+from openai.types.chat.chat_completion_message_tool_call import ChatCompletionMessageToolCall as OpenAIToolCall, Function as OpenAIFunction
+from sqlalchemy import func, select
+from sqlalchemy.exc import IntegrityError, InvalidRequestError
+from sqlalchemy.orm.exc import StaleDataError
+
+from letta.config import LettaConfig
+from letta.constants import (
+ BASE_MEMORY_TOOLS,
+ BASE_SLEEPTIME_TOOLS,
+ BASE_TOOLS,
+ BASE_VOICE_SLEEPTIME_CHAT_TOOLS,
+ BASE_VOICE_SLEEPTIME_TOOLS,
+ BUILTIN_TOOLS,
+ DEFAULT_ORG_ID,
+ DEFAULT_ORG_NAME,
+ FILES_TOOLS,
+ LETTA_TOOL_EXECUTION_DIR,
+ LETTA_TOOL_SET,
+ LOCAL_ONLY_MULTI_AGENT_TOOLS,
+ MCP_TOOL_TAG_NAME_PREFIX,
+ MULTI_AGENT_TOOLS,
+)
+from letta.data_sources.redis_client import NoopAsyncRedisClient, get_redis_client
+from letta.errors import LettaAgentNotFoundError
+from letta.functions.functions import derive_openai_json_schema, parse_source_code
+from letta.functions.mcp_client.types import MCPTool
+from letta.helpers import ToolRulesSolver
+from letta.helpers.datetime_helpers import AsyncTimer
+from letta.jobs.types import ItemUpdateInfo, RequestStatusUpdateInfo, StepStatusUpdateInfo
+from letta.orm import Base, Block
+from letta.orm.block_history import BlockHistory
+from letta.orm.errors import NoResultFound, UniqueConstraintViolationError
+from letta.orm.file import FileContent as FileContentModel, FileMetadata as FileMetadataModel
+from letta.schemas.agent import CreateAgent, UpdateAgent
+from letta.schemas.block import Block as PydanticBlock, BlockUpdate, CreateBlock
+from letta.schemas.embedding_config import EmbeddingConfig
+from letta.schemas.enums import (
+ ActorType,
+ AgentStepStatus,
+ FileProcessingStatus,
+ JobStatus,
+ JobType,
+ MessageRole,
+ ProviderType,
+ SandboxType,
+ StepStatus,
+ TagMatchMode,
+ ToolType,
+ VectorDBProvider,
+)
+from letta.schemas.environment_variables import SandboxEnvironmentVariableCreate, SandboxEnvironmentVariableUpdate
+from letta.schemas.file import FileMetadata, FileMetadata as PydanticFileMetadata
+from letta.schemas.identity import IdentityCreate, IdentityProperty, IdentityPropertyType, IdentityType, IdentityUpdate, IdentityUpsert
+from letta.schemas.job import BatchJob, Job, Job as PydanticJob, JobUpdate, LettaRequestConfig
+from letta.schemas.letta_message import UpdateAssistantMessage, UpdateReasoningMessage, UpdateSystemMessage, UpdateUserMessage
+from letta.schemas.letta_message_content import TextContent
+from letta.schemas.letta_stop_reason import LettaStopReason, StopReasonType
+from letta.schemas.llm_batch_job import AgentStepState, LLMBatchItem
+from letta.schemas.llm_config import LLMConfig
+from letta.schemas.message import Message as PydanticMessage, MessageCreate, MessageUpdate
+from letta.schemas.openai.chat_completion_response import UsageStatistics
+from letta.schemas.organization import Organization, Organization as PydanticOrganization, OrganizationUpdate
+from letta.schemas.passage import Passage as PydanticPassage
+from letta.schemas.pip_requirement import PipRequirement
+from letta.schemas.run import Run as PydanticRun
+from letta.schemas.sandbox_config import E2BSandboxConfig, LocalSandboxConfig, SandboxConfigCreate, SandboxConfigUpdate
+from letta.schemas.source import Source as PydanticSource, SourceUpdate
+from letta.schemas.tool import Tool as PydanticTool, ToolCreate, ToolUpdate
+from letta.schemas.tool_rule import InitToolRule
+from letta.schemas.user import User as PydanticUser, UserUpdate
+from letta.server.db import db_registry
+from letta.server.server import SyncServer
+from letta.services.block_manager import BlockManager
+from letta.services.helpers.agent_manager_helper import calculate_base_tools, calculate_multi_agent_tools, validate_agent_exists_async
+from letta.services.step_manager import FeedbackType
+from letta.settings import settings, tool_settings
+from letta.utils import calculate_file_defaults_based_on_context_window
+from tests.helpers.utils import comprehensive_agent_checks, validate_context_window_overview
+from tests.utils import random_string
+
+
+# ======================================================================================================================
+# Organization Manager Tests
+# ======================================================================================================================
+async def test_list_organizations(server: SyncServer):
+ # Create a new org and confirm that it is created correctly
+ org_name = "test"
+ org = await server.organization_manager.create_organization_async(pydantic_org=PydanticOrganization(name=org_name))
+
+ orgs = await server.organization_manager.list_organizations_async()
+ assert len(orgs) == 1
+ assert orgs[0].name == org_name
+
+ # Delete it after
+ await server.organization_manager.delete_organization_by_id_async(org.id)
+ orgs = await server.organization_manager.list_organizations_async()
+ assert len(orgs) == 0
+
+
+async def test_create_default_organization(server: SyncServer):
+ await server.organization_manager.create_default_organization_async()
+ retrieved = await server.organization_manager.get_default_organization_async()
+ assert retrieved.name == DEFAULT_ORG_NAME
+
+
+async def test_update_organization_name(server: SyncServer):
+ org_name_a = "a"
+ org_name_b = "b"
+ org = await server.organization_manager.create_organization_async(pydantic_org=PydanticOrganization(name=org_name_a))
+ assert org.name == org_name_a
+ org = await server.organization_manager.update_organization_name_using_id_async(org_id=org.id, name=org_name_b)
+ assert org.name == org_name_b
+
+
+async def test_update_organization_privileged_tools(server: SyncServer):
+ org_name = "test"
+ org = await server.organization_manager.create_organization_async(pydantic_org=PydanticOrganization(name=org_name))
+ assert org.privileged_tools == False
+ org = await server.organization_manager.update_organization_async(org_id=org.id, org_update=OrganizationUpdate(privileged_tools=True))
+ assert org.privileged_tools == True
+
+
+async def test_list_organizations_pagination(server: SyncServer):
+ await server.organization_manager.create_organization_async(pydantic_org=PydanticOrganization(name="a"))
+ await server.organization_manager.create_organization_async(pydantic_org=PydanticOrganization(name="b"))
+
+ orgs_x = await server.organization_manager.list_organizations_async(limit=1)
+ assert len(orgs_x) == 1
+
+ orgs_y = await server.organization_manager.list_organizations_async(after=orgs_x[0].id, limit=1)
+ assert len(orgs_y) == 1
+ assert orgs_y[0].name != orgs_x[0].name
+
+ orgs = await server.organization_manager.list_organizations_async(after=orgs_y[0].id, limit=1)
+ assert len(orgs) == 0
diff --git a/tests/managers/test_passage_manager.py b/tests/managers/test_passage_manager.py
new file mode 100644
index 000000000..477db1171
--- /dev/null
+++ b/tests/managers/test_passage_manager.py
@@ -0,0 +1,1391 @@
+import json
+import logging
+import os
+import random
+import re
+import string
+import time
+import uuid
+from datetime import datetime, timedelta, timezone
+from typing import List
+from unittest.mock import AsyncMock, Mock, patch
+
+import pytest
+from _pytest.python_api import approx
+from anthropic.types.beta import BetaMessage
+from anthropic.types.beta.messages import BetaMessageBatchIndividualResponse, BetaMessageBatchSucceededResult
+
+# Import shared fixtures and constants from conftest
+from conftest import (
+ CREATE_DELAY_SQLITE,
+ DEFAULT_EMBEDDING_CONFIG,
+ USING_SQLITE,
+)
+from openai.types.chat.chat_completion_message_tool_call import ChatCompletionMessageToolCall as OpenAIToolCall, Function as OpenAIFunction
+from sqlalchemy import func, select
+from sqlalchemy.exc import IntegrityError, InvalidRequestError
+from sqlalchemy.orm.exc import StaleDataError
+
+from letta.config import LettaConfig
+from letta.constants import (
+ BASE_MEMORY_TOOLS,
+ BASE_SLEEPTIME_TOOLS,
+ BASE_TOOLS,
+ BASE_VOICE_SLEEPTIME_CHAT_TOOLS,
+ BASE_VOICE_SLEEPTIME_TOOLS,
+ BUILTIN_TOOLS,
+ DEFAULT_ORG_ID,
+ DEFAULT_ORG_NAME,
+ FILES_TOOLS,
+ LETTA_TOOL_EXECUTION_DIR,
+ LETTA_TOOL_SET,
+ LOCAL_ONLY_MULTI_AGENT_TOOLS,
+ MCP_TOOL_TAG_NAME_PREFIX,
+ MULTI_AGENT_TOOLS,
+)
+from letta.data_sources.redis_client import NoopAsyncRedisClient, get_redis_client
+from letta.errors import LettaAgentNotFoundError
+from letta.functions.functions import derive_openai_json_schema, parse_source_code
+from letta.functions.mcp_client.types import MCPTool
+from letta.helpers import ToolRulesSolver
+from letta.helpers.datetime_helpers import AsyncTimer
+from letta.jobs.types import ItemUpdateInfo, RequestStatusUpdateInfo, StepStatusUpdateInfo
+from letta.orm import Base, Block
+from letta.orm.block_history import BlockHistory
+from letta.orm.errors import NoResultFound, UniqueConstraintViolationError
+from letta.orm.file import FileContent as FileContentModel, FileMetadata as FileMetadataModel
+from letta.schemas.agent import CreateAgent, UpdateAgent
+from letta.schemas.block import Block as PydanticBlock, BlockUpdate, CreateBlock
+from letta.schemas.embedding_config import EmbeddingConfig
+from letta.schemas.enums import (
+ ActorType,
+ AgentStepStatus,
+ FileProcessingStatus,
+ JobStatus,
+ JobType,
+ MessageRole,
+ ProviderType,
+ SandboxType,
+ StepStatus,
+ TagMatchMode,
+ ToolType,
+ VectorDBProvider,
+)
+from letta.schemas.environment_variables import SandboxEnvironmentVariableCreate, SandboxEnvironmentVariableUpdate
+from letta.schemas.file import FileMetadata, FileMetadata as PydanticFileMetadata
+from letta.schemas.identity import IdentityCreate, IdentityProperty, IdentityPropertyType, IdentityType, IdentityUpdate, IdentityUpsert
+from letta.schemas.job import BatchJob, Job, Job as PydanticJob, JobUpdate, LettaRequestConfig
+from letta.schemas.letta_message import UpdateAssistantMessage, UpdateReasoningMessage, UpdateSystemMessage, UpdateUserMessage
+from letta.schemas.letta_message_content import TextContent
+from letta.schemas.letta_stop_reason import LettaStopReason, StopReasonType
+from letta.schemas.llm_batch_job import AgentStepState, LLMBatchItem
+from letta.schemas.llm_config import LLMConfig
+from letta.schemas.message import Message as PydanticMessage, MessageCreate, MessageUpdate
+from letta.schemas.openai.chat_completion_response import UsageStatistics
+from letta.schemas.organization import Organization, Organization as PydanticOrganization, OrganizationUpdate
+from letta.schemas.passage import Passage as PydanticPassage
+from letta.schemas.pip_requirement import PipRequirement
+from letta.schemas.run import Run as PydanticRun
+from letta.schemas.sandbox_config import E2BSandboxConfig, LocalSandboxConfig, SandboxConfigCreate, SandboxConfigUpdate
+from letta.schemas.source import Source as PydanticSource, SourceUpdate
+from letta.schemas.tool import Tool as PydanticTool, ToolCreate, ToolUpdate
+from letta.schemas.tool_rule import InitToolRule
+from letta.schemas.user import User as PydanticUser, UserUpdate
+from letta.server.db import db_registry
+from letta.server.server import SyncServer
+from letta.services.block_manager import BlockManager
+from letta.services.helpers.agent_manager_helper import calculate_base_tools, calculate_multi_agent_tools, validate_agent_exists_async
+from letta.services.step_manager import FeedbackType
+from letta.settings import settings, tool_settings
+from letta.utils import calculate_file_defaults_based_on_context_window
+from tests.helpers.utils import comprehensive_agent_checks, validate_context_window_overview
+from tests.utils import random_string
+
+# ======================================================================================================================
+# Agent Manager - Passages Tests
+# ======================================================================================================================
+
+
+async def test_agent_list_passages_basic(server, default_user, sarah_agent, agent_passages_setup, disable_turbopuffer):
+ """Test basic listing functionality of agent passages"""
+
+ all_passages = await server.agent_manager.list_passages_async(actor=default_user, agent_id=sarah_agent.id)
+ assert len(all_passages) == 5 # 3 source + 2 agent passages
+
+ source_passages = await server.agent_manager.query_source_passages_async(actor=default_user, agent_id=sarah_agent.id)
+ assert len(source_passages) == 3 # 3 source + 2 agent passages
+
+
+async def test_agent_list_passages_ordering(server, default_user, sarah_agent, agent_passages_setup, disable_turbopuffer):
+ """Test ordering of agent passages"""
+
+ # Test ascending order
+ asc_passages = await server.agent_manager.list_passages_async(actor=default_user, agent_id=sarah_agent.id, ascending=True)
+ assert len(asc_passages) == 5
+ for i in range(1, len(asc_passages)):
+ assert asc_passages[i - 1].created_at <= asc_passages[i].created_at
+
+ # Test descending order
+ desc_passages = await server.agent_manager.list_passages_async(actor=default_user, agent_id=sarah_agent.id, ascending=False)
+ assert len(desc_passages) == 5
+ for i in range(1, len(desc_passages)):
+ assert desc_passages[i - 1].created_at >= desc_passages[i].created_at
+
+
+async def test_agent_list_passages_pagination(server, default_user, sarah_agent, agent_passages_setup, disable_turbopuffer):
+ """Test pagination of agent passages"""
+
+ # Test limit
+ limited_passages = await server.agent_manager.list_passages_async(actor=default_user, agent_id=sarah_agent.id, limit=3)
+ assert len(limited_passages) == 3
+
+ # Test cursor-based pagination
+ first_page = await server.agent_manager.list_passages_async(actor=default_user, agent_id=sarah_agent.id, limit=2, ascending=True)
+ assert len(first_page) == 2
+
+ second_page = await server.agent_manager.list_passages_async(
+ actor=default_user, agent_id=sarah_agent.id, after=first_page[-1].id, limit=2, ascending=True
+ )
+ assert len(second_page) == 2
+ assert first_page[-1].id != second_page[0].id
+ assert first_page[-1].created_at <= second_page[0].created_at
+
+ """
+ [1] [2]
+ * * | * *
+
+ [mid]
+ * | * * | *
+ """
+ middle_page = await server.agent_manager.list_passages_async(
+ actor=default_user, agent_id=sarah_agent.id, before=second_page[-1].id, after=first_page[0].id, ascending=True
+ )
+ assert len(middle_page) == 2
+ assert middle_page[0].id == first_page[-1].id
+ assert middle_page[1].id == second_page[0].id
+
+ middle_page_desc = await server.agent_manager.list_passages_async(
+ actor=default_user, agent_id=sarah_agent.id, before=second_page[-1].id, after=first_page[0].id, ascending=False
+ )
+ assert len(middle_page_desc) == 2
+ assert middle_page_desc[0].id == second_page[0].id
+ assert middle_page_desc[1].id == first_page[-1].id
+
+
+async def test_agent_list_passages_text_search(server, default_user, sarah_agent, agent_passages_setup, disable_turbopuffer):
+ """Test text search functionality of agent passages"""
+
+ # Test text search for source passages
+ source_text_passages = await server.agent_manager.list_passages_async(
+ actor=default_user, agent_id=sarah_agent.id, query_text="Source passage"
+ )
+ assert len(source_text_passages) == 3
+
+ # Test text search for agent passages
+ agent_text_passages = await server.agent_manager.list_passages_async(
+ actor=default_user, agent_id=sarah_agent.id, query_text="Agent passage"
+ )
+ assert len(agent_text_passages) == 2
+
+
+async def test_agent_list_passages_agent_only(server, default_user, sarah_agent, agent_passages_setup, disable_turbopuffer):
+ """Test text search functionality of agent passages"""
+
+ # Test text search for agent passages
+ agent_text_passages = await server.agent_manager.list_passages_async(actor=default_user, agent_id=sarah_agent.id, agent_only=True)
+ assert len(agent_text_passages) == 2
+
+
+async def test_agent_list_passages_filtering(server, default_user, sarah_agent, default_source, agent_passages_setup, disable_turbopuffer):
+ """Test filtering functionality of agent passages"""
+
+ # Test source filtering
+ source_filtered = await server.agent_manager.list_passages_async(
+ actor=default_user, agent_id=sarah_agent.id, source_id=default_source.id
+ )
+ assert len(source_filtered) == 3
+
+ # Test date filtering
+ now = datetime.now(timezone.utc)
+ future_date = now + timedelta(days=1)
+ past_date = now - timedelta(days=1)
+
+ date_filtered = await server.agent_manager.list_passages_async(
+ actor=default_user, agent_id=sarah_agent.id, start_date=past_date, end_date=future_date
+ )
+ assert len(date_filtered) == 5
+
+
+def mock_embeddings():
+ """Load mock embeddings from JSON file"""
+ fixture_path = os.path.join(os.path.dirname(__file__), "data", "test_embeddings.json")
+ with open(fixture_path, "r") as f:
+ return json.load(f)
+
+
+def mock_embed_model(mock_embeddings):
+ """Mock embedding model that returns predefined embeddings"""
+ mock_model = Mock()
+ mock_model.get_text_embedding = lambda text: mock_embeddings.get(text, [0.0] * 1536)
+ return mock_model
+
+
+async def test_agent_list_passages_vector_search(
+ server, default_user, sarah_agent, default_source, default_file, mock_embed_model, disable_turbopuffer
+):
+ """Test vector search functionality of agent passages"""
+ embed_model = mock_embed_model
+
+ # Get or create default archive for the agent
+ archive = await server.archive_manager.get_or_create_default_archive_for_agent_async(
+ agent_id=sarah_agent.id, agent_name=sarah_agent.name, actor=default_user
+ )
+
+ # Create passages with known embeddings
+ passages = []
+
+ # Create passages with different embeddings
+ test_passages = [
+ "I like red",
+ "random text",
+ "blue shoes",
+ ]
+
+ await server.agent_manager.attach_source_async(agent_id=sarah_agent.id, source_id=default_source.id, actor=default_user)
+
+ for i, text in enumerate(test_passages):
+ embedding = embed_model.get_text_embedding(text)
+ if i % 2 == 0:
+ # Create agent passage
+ passage = PydanticPassage(
+ text=text,
+ organization_id=default_user.organization_id,
+ archive_id=archive.id,
+ embedding_config=DEFAULT_EMBEDDING_CONFIG,
+ embedding=embedding,
+ )
+ created_passage = await server.passage_manager.create_agent_passage_async(passage, default_user)
+ else:
+ # Create source passage
+ passage = PydanticPassage(
+ text=text,
+ organization_id=default_user.organization_id,
+ source_id=default_source.id,
+ file_id=default_file.id,
+ embedding_config=DEFAULT_EMBEDDING_CONFIG,
+ embedding=embedding,
+ )
+ created_passage = await server.passage_manager.create_source_passage_async(passage, default_file, default_user)
+ passages.append(created_passage)
+
+ # Query vector similar to "red" embedding
+ query_key = "What's my favorite color?"
+
+ # Test vector search with all passages
+ results = await server.agent_manager.list_passages_async(
+ actor=default_user,
+ agent_id=sarah_agent.id,
+ query_text=query_key,
+ embedding_config=DEFAULT_EMBEDDING_CONFIG,
+ embed_query=True,
+ )
+
+ # Verify results are ordered by similarity
+ assert len(results) == 3
+ assert results[0].text == "I like red"
+ assert "random" in results[1].text or "random" in results[2].text
+ assert "blue" in results[1].text or "blue" in results[2].text
+
+ # Test vector search with agent_only=True
+ agent_only_results = await server.agent_manager.list_passages_async(
+ actor=default_user,
+ agent_id=sarah_agent.id,
+ query_text=query_key,
+ embedding_config=DEFAULT_EMBEDDING_CONFIG,
+ embed_query=True,
+ agent_only=True,
+ )
+
+ # Verify agent-only results
+ assert len(agent_only_results) == 2
+ assert agent_only_results[0].text == "I like red"
+ assert agent_only_results[1].text == "blue shoes"
+
+
+async def test_list_source_passages_only(server: SyncServer, default_user, default_source, agent_passages_setup):
+ """Test listing passages from a source without specifying an agent."""
+
+ # List passages by source_id without agent_id
+ source_passages = await server.agent_manager.list_passages_async(
+ actor=default_user,
+ source_id=default_source.id,
+ )
+
+ # Verify we get only source passages (3 from agent_passages_setup)
+ assert len(source_passages) == 3
+ assert all(p.source_id == default_source.id for p in source_passages)
+ assert all(p.archive_id is None for p in source_passages)
+
+
+# ======================================================================================================================
+# Passage Manager Tests
+# ======================================================================================================================
+
+
+async def test_passage_create_agentic(server: SyncServer, agent_passage_fixture, default_user):
+ """Test creating a passage using agent_passage_fixture fixture"""
+ assert agent_passage_fixture.id is not None
+ assert agent_passage_fixture.text == "Hello, I am an agent passage"
+
+ # Verify we can retrieve it
+ retrieved = await server.passage_manager.get_passage_by_id_async(
+ agent_passage_fixture.id,
+ actor=default_user,
+ )
+ assert retrieved is not None
+ assert retrieved.id == agent_passage_fixture.id
+ assert retrieved.text == agent_passage_fixture.text
+
+
+async def test_passage_create_source(server: SyncServer, source_passage_fixture, default_user):
+ """Test creating a source passage."""
+ assert source_passage_fixture is not None
+ assert source_passage_fixture.text == "Hello, I am a source passage"
+
+ # Verify we can retrieve it
+ retrieved = await server.passage_manager.get_passage_by_id_async(
+ source_passage_fixture.id,
+ actor=default_user,
+ )
+ assert retrieved is not None
+ assert retrieved.id == source_passage_fixture.id
+ assert retrieved.text == source_passage_fixture.text
+
+
+async def test_passage_create_invalid(server: SyncServer, agent_passage_fixture, default_user):
+ """Test creating an agent passage."""
+ assert agent_passage_fixture is not None
+ assert agent_passage_fixture.text == "Hello, I am an agent passage"
+
+ # Try to create an invalid passage (with both archive_id and source_id)
+ with pytest.raises(AssertionError):
+ await server.passage_manager.create_passage_async(
+ PydanticPassage(
+ text="Invalid passage",
+ archive_id="123",
+ source_id="456",
+ organization_id=default_user.organization_id,
+ embedding=[0.1] * 1024,
+ embedding_config=DEFAULT_EMBEDDING_CONFIG,
+ ),
+ actor=default_user,
+ )
+
+
+async def test_passage_get_by_id(server: SyncServer, agent_passage_fixture, source_passage_fixture, default_user):
+ """Test retrieving a passage by ID"""
+ retrieved = await server.passage_manager.get_passage_by_id_async(agent_passage_fixture.id, actor=default_user)
+ assert retrieved is not None
+ assert retrieved.id == agent_passage_fixture.id
+ assert retrieved.text == agent_passage_fixture.text
+
+ retrieved = await server.passage_manager.get_passage_by_id_async(source_passage_fixture.id, actor=default_user)
+ assert retrieved is not None
+ assert retrieved.id == source_passage_fixture.id
+ assert retrieved.text == source_passage_fixture.text
+
+
+async def test_passage_cascade_deletion(
+ server: SyncServer, agent_passage_fixture, source_passage_fixture, default_user, default_source, sarah_agent
+):
+ """Test that passages are deleted when their parent (agent or source) is deleted."""
+ # Verify passages exist
+ agent_passage = await server.passage_manager.get_passage_by_id_async(agent_passage_fixture.id, default_user)
+ source_passage = await server.passage_manager.get_passage_by_id_async(source_passage_fixture.id, default_user)
+ assert agent_passage is not None
+ assert source_passage is not None
+
+ # Delete agent and verify its passages are deleted
+ await server.agent_manager.delete_agent_async(sarah_agent.id, default_user)
+ agentic_passages = await server.agent_manager.list_passages_async(actor=default_user, agent_id=sarah_agent.id, agent_only=True)
+ assert len(agentic_passages) == 0
+
+
+async def test_create_agent_passage_specific(server: SyncServer, default_user, sarah_agent):
+ """Test creating an agent passage using the new agent-specific method."""
+ # Get or create default archive for the agent
+ archive = await server.archive_manager.get_or_create_default_archive_for_agent_async(
+ agent_id=sarah_agent.id, agent_name=sarah_agent.name, actor=default_user
+ )
+
+ passage = await server.passage_manager.create_agent_passage_async(
+ PydanticPassage(
+ text="Test agent passage via specific method",
+ archive_id=archive.id,
+ organization_id=default_user.organization_id,
+ embedding=[0.1],
+ embedding_config=DEFAULT_EMBEDDING_CONFIG,
+ metadata={"type": "test_specific"},
+ tags=["python", "test", "agent"],
+ ),
+ actor=default_user,
+ )
+
+ assert passage.id is not None
+ assert passage.text == "Test agent passage via specific method"
+ assert passage.archive_id == archive.id
+ assert passage.source_id is None
+ assert sorted(passage.tags) == sorted(["python", "test", "agent"])
+
+
+async def test_create_source_passage_specific(server: SyncServer, default_user, default_file, default_source):
+ """Test creating a source passage using the new source-specific method."""
+ passage = await server.passage_manager.create_source_passage_async(
+ PydanticPassage(
+ text="Test source passage via specific method",
+ source_id=default_source.id,
+ file_id=default_file.id,
+ organization_id=default_user.organization_id,
+ embedding=[0.1],
+ embedding_config=DEFAULT_EMBEDDING_CONFIG,
+ metadata={"type": "test_specific"},
+ tags=["document", "test", "source"],
+ ),
+ file_metadata=default_file,
+ actor=default_user,
+ )
+
+ assert passage.id is not None
+ assert passage.text == "Test source passage via specific method"
+ assert passage.source_id == default_source.id
+ assert passage.archive_id is None
+ assert sorted(passage.tags) == sorted(["document", "test", "source"])
+
+
+async def test_create_agent_passage_validation(server: SyncServer, default_user, default_source, sarah_agent):
+ """Test that agent passage creation validates inputs correctly."""
+ # Should fail if archive_id is missing
+ with pytest.raises(ValueError, match="Agent passage must have archive_id"):
+ await server.passage_manager.create_agent_passage_async(
+ PydanticPassage(
+ text="Invalid agent passage",
+ organization_id=default_user.organization_id,
+ embedding=[0.1],
+ embedding_config=DEFAULT_EMBEDDING_CONFIG,
+ ),
+ actor=default_user,
+ )
+
+ # Get or create default archive for the agent
+ archive = await server.archive_manager.get_or_create_default_archive_for_agent_async(
+ agent_id=sarah_agent.id, agent_name=sarah_agent.name, actor=default_user
+ )
+
+ # Should fail if source_id is present
+ with pytest.raises(ValueError, match="Agent passage cannot have source_id"):
+ await server.passage_manager.create_agent_passage_async(
+ PydanticPassage(
+ text="Invalid agent passage",
+ archive_id=archive.id,
+ source_id=default_source.id,
+ organization_id=default_user.organization_id,
+ embedding=[0.1],
+ embedding_config=DEFAULT_EMBEDDING_CONFIG,
+ ),
+ actor=default_user,
+ )
+
+
+async def test_create_source_passage_validation(server: SyncServer, default_user, default_file, default_source, sarah_agent):
+ """Test that source passage creation validates inputs correctly."""
+ # Should fail if source_id is missing
+ with pytest.raises(ValueError, match="Source passage must have source_id"):
+ await server.passage_manager.create_source_passage_async(
+ PydanticPassage(
+ text="Invalid source passage",
+ organization_id=default_user.organization_id,
+ embedding=[0.1],
+ embedding_config=DEFAULT_EMBEDDING_CONFIG,
+ ),
+ file_metadata=default_file,
+ actor=default_user,
+ )
+
+ # Get or create default archive for the agent
+ archive = await server.archive_manager.get_or_create_default_archive_for_agent_async(
+ agent_id=sarah_agent.id, agent_name=sarah_agent.name, actor=default_user
+ )
+
+ # Should fail if archive_id is present
+ with pytest.raises(ValueError, match="Source passage cannot have archive_id"):
+ await server.passage_manager.create_source_passage_async(
+ PydanticPassage(
+ text="Invalid source passage",
+ source_id=default_source.id,
+ archive_id=archive.id,
+ organization_id=default_user.organization_id,
+ embedding=[0.1],
+ embedding_config=DEFAULT_EMBEDDING_CONFIG,
+ ),
+ file_metadata=default_file,
+ actor=default_user,
+ )
+
+
+async def test_get_agent_passage_by_id_specific(server: SyncServer, default_user, sarah_agent):
+ """Test retrieving an agent passage using the new agent-specific method."""
+ # Get or create default archive for the agent
+ archive = await server.archive_manager.get_or_create_default_archive_for_agent_async(
+ agent_id=sarah_agent.id, agent_name=sarah_agent.name, actor=default_user
+ )
+
+ # Create an agent passage
+ passage = await server.passage_manager.create_agent_passage_async(
+ PydanticPassage(
+ text="Agent passage for retrieval test",
+ archive_id=archive.id,
+ organization_id=default_user.organization_id,
+ embedding=[0.1],
+ embedding_config=DEFAULT_EMBEDDING_CONFIG,
+ ),
+ actor=default_user,
+ )
+
+ # Retrieve it using the specific method
+ retrieved = await server.passage_manager.get_agent_passage_by_id_async(passage.id, actor=default_user)
+ assert retrieved is not None
+ assert retrieved.id == passage.id
+ assert retrieved.text == passage.text
+ assert retrieved.archive_id == archive.id
+
+
+async def test_get_source_passage_by_id_specific(server: SyncServer, default_user, default_file, default_source):
+ """Test retrieving a source passage using the new source-specific method."""
+ # Create a source passage
+ passage = await server.passage_manager.create_source_passage_async(
+ PydanticPassage(
+ text="Source passage for retrieval test",
+ source_id=default_source.id,
+ file_id=default_file.id,
+ organization_id=default_user.organization_id,
+ embedding=[0.1],
+ embedding_config=DEFAULT_EMBEDDING_CONFIG,
+ ),
+ file_metadata=default_file,
+ actor=default_user,
+ )
+
+ # Retrieve it using the specific method
+ retrieved = await server.passage_manager.get_source_passage_by_id_async(passage.id, actor=default_user)
+ assert retrieved is not None
+ assert retrieved.id == passage.id
+ assert retrieved.text == passage.text
+ assert retrieved.source_id == default_source.id
+
+
+async def test_get_wrong_passage_type_fails(server: SyncServer, default_user, sarah_agent, default_file, default_source):
+ """Test that trying to get the wrong passage type with specific methods fails."""
+ # Create an agent passage
+ # Get or create default archive for the agent
+ archive = await server.archive_manager.get_or_create_default_archive_for_agent_async(
+ agent_id=sarah_agent.id, agent_name=sarah_agent.name, actor=default_user
+ )
+
+ agent_passage = await server.passage_manager.create_agent_passage_async(
+ PydanticPassage(
+ text="Agent passage",
+ archive_id=archive.id,
+ organization_id=default_user.organization_id,
+ embedding=[0.1],
+ embedding_config=DEFAULT_EMBEDDING_CONFIG,
+ ),
+ actor=default_user,
+ )
+
+ # Create a source passage
+ source_passage = await server.passage_manager.create_source_passage_async(
+ PydanticPassage(
+ text="Source passage",
+ source_id=default_source.id,
+ file_id=default_file.id,
+ organization_id=default_user.organization_id,
+ embedding=[0.1],
+ embedding_config=DEFAULT_EMBEDDING_CONFIG,
+ ),
+ file_metadata=default_file,
+ actor=default_user,
+ )
+
+ # Trying to get agent passage with source method should fail
+ with pytest.raises(NoResultFound):
+ await server.passage_manager.get_source_passage_by_id_async(agent_passage.id, actor=default_user)
+
+ # Trying to get source passage with agent method should fail
+ with pytest.raises(NoResultFound):
+ await server.passage_manager.get_agent_passage_by_id_async(source_passage.id, actor=default_user)
+
+
+async def test_update_agent_passage_specific(server: SyncServer, default_user, sarah_agent):
+ """Test updating an agent passage using the new agent-specific method."""
+ # Get or create default archive for the agent
+ archive = await server.archive_manager.get_or_create_default_archive_for_agent_async(
+ agent_id=sarah_agent.id, agent_name=sarah_agent.name, actor=default_user
+ )
+
+ # Create an agent passage
+ passage = await server.passage_manager.create_agent_passage_async(
+ PydanticPassage(
+ text="Original agent passage text",
+ archive_id=archive.id,
+ organization_id=default_user.organization_id,
+ embedding=[0.1],
+ embedding_config=DEFAULT_EMBEDDING_CONFIG,
+ ),
+ actor=default_user,
+ )
+
+ # Update it
+ updated_passage = await server.passage_manager.update_agent_passage_by_id_async(
+ passage.id,
+ PydanticPassage(
+ text="Updated agent passage text",
+ archive_id=archive.id,
+ organization_id=default_user.organization_id,
+ embedding=[0.2],
+ embedding_config=DEFAULT_EMBEDDING_CONFIG,
+ ),
+ actor=default_user,
+ )
+
+ assert updated_passage.text == "Updated agent passage text"
+ assert updated_passage.embedding[0] == approx(0.2)
+ assert updated_passage.id == passage.id
+
+
+async def test_update_source_passage_specific(server: SyncServer, default_user, default_file, default_source):
+ """Test updating a source passage using the new source-specific method."""
+ # Create a source passage
+ passage = await server.passage_manager.create_source_passage_async(
+ PydanticPassage(
+ text="Original source passage text",
+ source_id=default_source.id,
+ file_id=default_file.id,
+ organization_id=default_user.organization_id,
+ embedding=[0.1],
+ embedding_config=DEFAULT_EMBEDDING_CONFIG,
+ ),
+ file_metadata=default_file,
+ actor=default_user,
+ )
+
+ # Update it
+ updated_passage = await server.passage_manager.update_source_passage_by_id_async(
+ passage.id,
+ PydanticPassage(
+ text="Updated source passage text",
+ source_id=default_source.id,
+ file_id=default_file.id,
+ organization_id=default_user.organization_id,
+ embedding=[0.2],
+ embedding_config=DEFAULT_EMBEDDING_CONFIG,
+ ),
+ actor=default_user,
+ )
+
+ assert updated_passage.text == "Updated source passage text"
+ assert updated_passage.embedding[0] == approx(0.2)
+ assert updated_passage.id == passage.id
+
+
+async def test_delete_agent_passage_specific(server: SyncServer, default_user, sarah_agent):
+ """Test deleting an agent passage using the new agent-specific method."""
+ # Get or create default archive for the agent
+ archive = await server.archive_manager.get_or_create_default_archive_for_agent_async(
+ agent_id=sarah_agent.id, agent_name=sarah_agent.name, actor=default_user
+ )
+
+ # Create an agent passage
+ passage = await server.passage_manager.create_agent_passage_async(
+ PydanticPassage(
+ text="Agent passage to delete",
+ archive_id=archive.id,
+ organization_id=default_user.organization_id,
+ embedding=[0.1],
+ embedding_config=DEFAULT_EMBEDDING_CONFIG,
+ ),
+ actor=default_user,
+ )
+
+ # Verify it exists
+ retrieved = await server.passage_manager.get_agent_passage_by_id_async(passage.id, actor=default_user)
+ assert retrieved is not None
+
+ # Delete it
+ result = await server.passage_manager.delete_agent_passage_by_id_async(passage.id, actor=default_user)
+ assert result is True
+
+ # Verify it's gone
+ with pytest.raises(NoResultFound):
+ await server.passage_manager.get_agent_passage_by_id_async(passage.id, actor=default_user)
+
+
+async def test_delete_source_passage_specific(server: SyncServer, default_user, default_file, default_source):
+ """Test deleting a source passage using the new source-specific method."""
+ # Create a source passage
+ passage = await server.passage_manager.create_source_passage_async(
+ PydanticPassage(
+ text="Source passage to delete",
+ source_id=default_source.id,
+ file_id=default_file.id,
+ organization_id=default_user.organization_id,
+ embedding=[0.1],
+ embedding_config=DEFAULT_EMBEDDING_CONFIG,
+ ),
+ file_metadata=default_file,
+ actor=default_user,
+ )
+
+ # Verify it exists
+ retrieved = await server.passage_manager.get_source_passage_by_id_async(passage.id, actor=default_user)
+ assert retrieved is not None
+
+ # Delete it
+ result = await server.passage_manager.delete_source_passage_by_id_async(passage.id, actor=default_user)
+ assert result is True
+
+ # Verify it's gone
+ with pytest.raises(NoResultFound):
+ await server.passage_manager.get_source_passage_by_id_async(passage.id, actor=default_user)
+
+
+async def test_create_many_agent_passages_async(server: SyncServer, default_user, sarah_agent):
+ """Test creating multiple agent passages using the new batch method."""
+ # Get or create default archive for the agent
+ archive = await server.archive_manager.get_or_create_default_archive_for_agent_async(
+ agent_id=sarah_agent.id, agent_name=sarah_agent.name, actor=default_user
+ )
+
+ passages = [
+ PydanticPassage(
+ text=f"Batch agent passage {i}",
+ archive_id=archive.id, # Now archive is a PydanticArchive object
+ organization_id=default_user.organization_id,
+ embedding=[0.1 * i],
+ embedding_config=DEFAULT_EMBEDDING_CONFIG,
+ tags=["batch", f"item{i}"] if i % 2 == 0 else ["batch", "odd"],
+ )
+ for i in range(3)
+ ]
+
+ created_passages = await server.passage_manager.create_many_archival_passages_async(passages, actor=default_user)
+
+ assert len(created_passages) == 3
+ for i, passage in enumerate(created_passages):
+ assert passage.text == f"Batch agent passage {i}"
+ assert passage.archive_id == archive.id
+ assert passage.source_id is None
+ expected_tags = ["batch", f"item{i}"] if i % 2 == 0 else ["batch", "odd"]
+ assert passage.tags == expected_tags
+
+
+async def test_create_many_source_passages_async(server: SyncServer, default_user, default_file, default_source):
+ """Test creating multiple source passages using the new batch method."""
+ passages = [
+ PydanticPassage(
+ text=f"Batch source passage {i}",
+ source_id=default_source.id,
+ file_id=default_file.id,
+ organization_id=default_user.organization_id,
+ embedding=[0.1 * i],
+ embedding_config=DEFAULT_EMBEDDING_CONFIG,
+ )
+ for i in range(3)
+ ]
+
+ created_passages = await server.passage_manager.create_many_source_passages_async(
+ passages, file_metadata=default_file, actor=default_user
+ )
+
+ assert len(created_passages) == 3
+ for i, passage in enumerate(created_passages):
+ assert passage.text == f"Batch source passage {i}"
+ assert passage.source_id == default_source.id
+ assert passage.archive_id is None
+
+
+async def test_agent_passage_size(server: SyncServer, default_user, sarah_agent):
+ """Test counting agent passages using the new agent-specific size method."""
+ initial_size = await server.passage_manager.agent_passage_size_async(actor=default_user, agent_id=sarah_agent.id)
+
+ # Get or create default archive for the agent
+ archive = await server.archive_manager.get_or_create_default_archive_for_agent_async(
+ agent_id=sarah_agent.id, agent_name=sarah_agent.name, actor=default_user
+ )
+
+ # Create some agent passages
+ for i in range(3):
+ await server.passage_manager.create_agent_passage_async(
+ PydanticPassage(
+ text=f"Agent passage {i} for size test",
+ archive_id=archive.id,
+ organization_id=default_user.organization_id,
+ embedding=[0.1],
+ embedding_config=DEFAULT_EMBEDDING_CONFIG,
+ ),
+ actor=default_user,
+ )
+
+ final_size = await server.passage_manager.agent_passage_size_async(actor=default_user, agent_id=sarah_agent.id)
+ assert final_size == initial_size + 3
+
+
+async def test_passage_tags_functionality(disable_turbopuffer, server: SyncServer, default_user, sarah_agent):
+ """Test comprehensive tag functionality for passages."""
+ from letta.schemas.enums import TagMatchMode
+
+ # Get or create default archive for the agent
+ archive = await server.archive_manager.get_or_create_default_archive_for_agent_async(
+ agent_id=sarah_agent.id, agent_name=sarah_agent.name, actor=default_user
+ )
+
+ # Create passages with different tag combinations
+ test_passages = [
+ {"text": "Python programming tutorial", "tags": ["python", "tutorial", "programming"]},
+ {"text": "Machine learning with Python", "tags": ["python", "ml", "ai"]},
+ {"text": "JavaScript web development", "tags": ["javascript", "web", "frontend"]},
+ {"text": "Python data science guide", "tags": ["python", "tutorial", "data"]},
+ {"text": "No tags passage", "tags": None},
+ ]
+
+ created_passages = []
+ for test_data in test_passages:
+ passage = await server.passage_manager.create_agent_passage_async(
+ PydanticPassage(
+ text=test_data["text"],
+ archive_id=archive.id,
+ organization_id=default_user.organization_id,
+ embedding=[0.1, 0.2, 0.3],
+ embedding_config=DEFAULT_EMBEDDING_CONFIG,
+ tags=test_data["tags"],
+ ),
+ actor=default_user,
+ )
+ created_passages.append(passage)
+
+ # Test that tags are properly stored (deduplicated)
+ for i, passage in enumerate(created_passages):
+ expected_tags = test_passages[i]["tags"]
+ if expected_tags:
+ assert set(passage.tags) == set(expected_tags)
+ else:
+ assert passage.tags is None
+
+ # Test querying with tag filtering (if Turbopuffer is enabled)
+ if hasattr(server.agent_manager, "query_agent_passages_async"):
+ # Test querying with python tag (should find 3 passages)
+ python_results = await server.agent_manager.query_agent_passages_async(
+ actor=default_user,
+ agent_id=sarah_agent.id,
+ tags=["python"],
+ tag_match_mode=TagMatchMode.ANY,
+ )
+
+ python_texts = [p.text for p, _, _ in python_results]
+ assert len([t for t in python_texts if "Python" in t]) >= 2
+
+ # Test querying with multiple tags using ALL mode
+ tutorial_python_results = await server.agent_manager.query_agent_passages_async(
+ actor=default_user,
+ agent_id=sarah_agent.id,
+ tags=["python", "tutorial"],
+ tag_match_mode=TagMatchMode.ALL,
+ )
+
+ tutorial_texts = [p.text for p, _, _ in tutorial_python_results]
+ expected_matches = [t for t in tutorial_texts if "tutorial" in t and "Python" in t]
+ assert len(expected_matches) >= 1
+
+
+async def test_comprehensive_tag_functionality(disable_turbopuffer, server: SyncServer, sarah_agent, default_user):
+ """Comprehensive test for tag functionality including dual storage and junction table."""
+
+ # Test 1: Create passages with tags and verify they're stored in both places
+ passages_with_tags = []
+ test_tags = {
+ "passage1": ["important", "documentation", "python"],
+ "passage2": ["important", "testing"],
+ "passage3": ["documentation", "api"],
+ "passage4": ["python", "testing", "api"],
+ "passage5": [], # Test empty tags
+ }
+
+ for i, (passage_key, tags) in enumerate(test_tags.items(), 1):
+ text = f"Test passage {i} for comprehensive tag testing"
+ created_passages = await server.passage_manager.insert_passage(
+ agent_state=sarah_agent,
+ text=text,
+ actor=default_user,
+ tags=tags if tags else None,
+ )
+ assert len(created_passages) == 1
+ passage = created_passages[0]
+
+ # Verify tags are stored in the JSON column (deduplicated)
+ if tags:
+ assert set(passage.tags) == set(tags)
+ else:
+ assert passage.tags is None
+ passages_with_tags.append(passage)
+
+ # Test 2: Verify unique tags for archive
+ archive = await server.archive_manager.get_or_create_default_archive_for_agent_async(
+ agent_id=sarah_agent.id,
+ agent_name=sarah_agent.name,
+ actor=default_user,
+ )
+
+ unique_tags = await server.passage_manager.get_unique_tags_for_archive_async(
+ archive_id=archive.id,
+ actor=default_user,
+ )
+
+ # Should have all unique tags: "important", "documentation", "python", "testing", "api"
+ expected_unique_tags = {"important", "documentation", "python", "testing", "api"}
+ assert set(unique_tags) == expected_unique_tags
+ assert len(unique_tags) == 5
+
+ # Test 3: Verify tag counts
+ tag_counts = await server.passage_manager.get_tag_counts_for_archive_async(
+ archive_id=archive.id,
+ actor=default_user,
+ )
+
+ # Verify counts
+ assert tag_counts["important"] == 2 # passage1 and passage2
+ assert tag_counts["documentation"] == 2 # passage1 and passage3
+ assert tag_counts["python"] == 2 # passage1 and passage4
+ assert tag_counts["testing"] == 2 # passage2 and passage4
+ assert tag_counts["api"] == 2 # passage3 and passage4
+
+ # Test 4: Query passages with ANY tag matching
+ any_results = await server.agent_manager.query_agent_passages_async(
+ agent_id=sarah_agent.id,
+ query_text="test",
+ limit=10,
+ tags=["important", "api"],
+ tag_match_mode=TagMatchMode.ANY,
+ actor=default_user,
+ )
+
+ # Should match passages with "important" OR "api" tags (passages 1, 2, 3, 4)
+ [p.text for p, _, _ in any_results]
+ assert len(any_results) >= 4
+
+ # Test 5: Query passages with ALL tag matching
+ all_results = await server.agent_manager.query_agent_passages_async(
+ agent_id=sarah_agent.id,
+ query_text="test",
+ limit=10,
+ tags=["python", "testing"],
+ tag_match_mode=TagMatchMode.ALL,
+ actor=default_user,
+ )
+
+ # Should only match passage4 which has both "python" AND "testing"
+ all_passage_texts = [p.text for p, _, _ in all_results]
+ assert any("Test passage 4" in text for text in all_passage_texts)
+
+ # Test 6: Query with non-existent tags
+ no_results = await server.agent_manager.query_agent_passages_async(
+ agent_id=sarah_agent.id,
+ query_text="test",
+ limit=10,
+ tags=["nonexistent", "missing"],
+ tag_match_mode=TagMatchMode.ANY,
+ actor=default_user,
+ )
+
+ # Should return no results
+ assert len(no_results) == 0
+
+ # Test 7: Verify tags CAN be updated (with junction table properly maintained)
+ first_passage = passages_with_tags[0]
+ new_tags = ["updated", "modified", "changed"]
+ update_data = PydanticPassage(
+ id=first_passage.id,
+ text="Updated text",
+ tags=new_tags,
+ organization_id=first_passage.organization_id,
+ archive_id=first_passage.archive_id,
+ embedding=first_passage.embedding,
+ embedding_config=first_passage.embedding_config,
+ )
+
+ # Update should work and tags should be updated
+ updated = await server.passage_manager.update_agent_passage_by_id_async(
+ passage_id=first_passage.id,
+ passage=update_data,
+ actor=default_user,
+ )
+
+ # Both text and tags should be updated
+ assert updated.text == "Updated text"
+ assert set(updated.tags) == set(new_tags)
+
+ # Verify tags are properly updated in junction table
+ updated_unique_tags = await server.passage_manager.get_unique_tags_for_archive_async(
+ archive_id=archive.id,
+ actor=default_user,
+ )
+
+ # Should include new tags and not include old "important", "documentation", "python" from passage1
+ # But still have tags from other passages
+ assert "updated" in updated_unique_tags
+ assert "modified" in updated_unique_tags
+ assert "changed" in updated_unique_tags
+
+ # Test 8: Delete a passage and verify cascade deletion of tags
+ passage_to_delete = passages_with_tags[1] # passage2 with ["important", "testing"]
+
+ await server.passage_manager.delete_agent_passage_by_id_async(
+ passage_id=passage_to_delete.id,
+ actor=default_user,
+ )
+
+ # Get updated tag counts
+ updated_tag_counts = await server.passage_manager.get_tag_counts_for_archive_async(
+ archive_id=archive.id,
+ actor=default_user,
+ )
+
+ # "important" no longer exists (was in passage1 which was updated and passage2 which was deleted)
+ assert "important" not in updated_tag_counts
+ # "testing" count should decrease from 2 to 1 (only in passage4 now)
+ assert updated_tag_counts["testing"] == 1
+
+ # Test 9: Batch create passages with tags
+ batch_texts = [
+ "Batch passage 1",
+ "Batch passage 2",
+ "Batch passage 3",
+ ]
+ batch_tags = ["batch", "test", "multiple"]
+
+ batch_passages = []
+ for text in batch_texts:
+ passages = await server.passage_manager.insert_passage(
+ agent_state=sarah_agent,
+ text=text,
+ actor=default_user,
+ tags=batch_tags,
+ )
+ batch_passages.extend(passages)
+
+ # Verify all batch passages have the same tags
+ for passage in batch_passages:
+ assert set(passage.tags) == set(batch_tags)
+
+ # Test 10: Verify tag counts include batch passages
+ final_tag_counts = await server.passage_manager.get_tag_counts_for_archive_async(
+ archive_id=archive.id,
+ actor=default_user,
+ )
+
+ assert final_tag_counts["batch"] == 3
+ assert final_tag_counts["test"] == 3
+ assert final_tag_counts["multiple"] == 3
+
+ # Test 11: Complex query with multiple tags and ALL matching
+ complex_all_results = await server.agent_manager.query_agent_passages_async(
+ agent_id=sarah_agent.id,
+ query_text="batch",
+ limit=10,
+ tags=["batch", "test", "multiple"],
+ tag_match_mode=TagMatchMode.ALL,
+ actor=default_user,
+ )
+
+ # Should match all 3 batch passages
+ assert len(complex_all_results) >= 3
+
+ # Test 12: Empty tag list should return all passages
+ all_passages = await server.agent_manager.query_agent_passages_async(
+ agent_id=sarah_agent.id,
+ query_text="passage",
+ limit=50,
+ tags=[],
+ tag_match_mode=TagMatchMode.ANY,
+ actor=default_user,
+ )
+
+ # Should return passages based on text search only
+ assert len(all_passages) > 0
+
+
+async def test_tag_edge_cases(disable_turbopuffer, server: SyncServer, sarah_agent, default_user):
+ """Test edge cases for tag functionality."""
+
+ # Test 1: Very long tag names
+ long_tag = "a" * 500 # 500 character tag
+ passages = await server.passage_manager.insert_passage(
+ agent_state=sarah_agent,
+ text="Testing long tag names",
+ actor=default_user,
+ tags=[long_tag, "normal_tag"],
+ )
+
+ assert len(passages) == 1
+ assert long_tag in passages[0].tags
+
+ # Test 2: Special characters in tags
+ special_tags = [
+ "tag-with-dash",
+ "tag_with_underscore",
+ "tag.with.dots",
+ "tag/with/slash",
+ "tag:with:colon",
+ "tag@with@at",
+ "tag#with#hash",
+ "tag with spaces",
+ "CamelCaseTag",
+ "数字标签",
+ ]
+
+ passages_special = await server.passage_manager.insert_passage(
+ agent_state=sarah_agent,
+ text="Testing special character tags",
+ actor=default_user,
+ tags=special_tags,
+ )
+
+ assert len(passages_special) == 1
+ assert set(passages_special[0].tags) == set(special_tags)
+
+ # Verify unique tags includes all special character tags
+ archive = await server.archive_manager.get_or_create_default_archive_for_agent_async(
+ agent_id=sarah_agent.id,
+ agent_name=sarah_agent.name,
+ actor=default_user,
+ )
+
+ unique_tags = await server.passage_manager.get_unique_tags_for_archive_async(
+ archive_id=archive.id,
+ actor=default_user,
+ )
+
+ for tag in special_tags:
+ assert tag in unique_tags
+
+ # Test 3: Duplicate tags in input (should be deduplicated)
+ duplicate_tags = ["tag1", "tag2", "tag1", "tag3", "tag2", "tag1"]
+ passages_dup = await server.passage_manager.insert_passage(
+ agent_state=sarah_agent,
+ text="Testing duplicate tags",
+ actor=default_user,
+ tags=duplicate_tags,
+ )
+
+ # Should only have unique tags (duplicates removed)
+ assert len(passages_dup) == 1
+ assert set(passages_dup[0].tags) == {"tag1", "tag2", "tag3"}
+ assert len(passages_dup[0].tags) == 3 # Should be deduplicated
+
+ # Test 4: Case sensitivity in tags
+ case_tags = ["Tag", "tag", "TAG", "tAg"]
+ passages_case = await server.passage_manager.insert_passage(
+ agent_state=sarah_agent,
+ text="Testing case sensitive tags",
+ actor=default_user,
+ tags=case_tags,
+ )
+
+ # All variations should be preserved (case-sensitive)
+ assert len(passages_case) == 1
+ assert set(passages_case[0].tags) == set(case_tags)
+
+
+async def test_search_agent_archival_memory_async(disable_turbopuffer, server: SyncServer, default_user, sarah_agent):
+ """Test the search_agent_archival_memory_async method that powers both the agent tool and API endpoint."""
+ # Get or create default archive for the agent
+ archive = await server.archive_manager.get_or_create_default_archive_for_agent_async(
+ agent_id=sarah_agent.id, agent_name=sarah_agent.name, actor=default_user
+ )
+
+ # Create test passages with various content and tags
+ test_data = [
+ {
+ "text": "Python is a powerful programming language used for data science and web development.",
+ "tags": ["python", "programming", "data-science", "web"],
+ "created_at": datetime(2024, 1, 15, 10, 30, tzinfo=timezone.utc),
+ },
+ {
+ "text": "Machine learning algorithms can be implemented in Python using libraries like scikit-learn.",
+ "tags": ["python", "machine-learning", "algorithms"],
+ "created_at": datetime(2024, 1, 16, 14, 45, tzinfo=timezone.utc),
+ },
+ {
+ "text": "JavaScript is essential for frontend web development and modern web applications.",
+ "tags": ["javascript", "frontend", "web"],
+ "created_at": datetime(2024, 1, 17, 9, 15, tzinfo=timezone.utc),
+ },
+ {
+ "text": "Database design principles are important for building scalable applications.",
+ "tags": ["database", "design", "scalability"],
+ "created_at": datetime(2024, 1, 18, 16, 20, tzinfo=timezone.utc),
+ },
+ {
+ "text": "The weather today is sunny and warm, perfect for outdoor activities.",
+ "tags": ["weather", "outdoor"],
+ "created_at": datetime(2024, 1, 19, 11, 0, tzinfo=timezone.utc),
+ },
+ ]
+
+ # Create passages in the database
+ created_passages = []
+ for data in test_data:
+ passage = await server.passage_manager.create_agent_passage_async(
+ PydanticPassage(
+ text=data["text"],
+ archive_id=archive.id,
+ organization_id=default_user.organization_id,
+ embedding=[0.1, 0.2, 0.3], # Mock embedding
+ embedding_config=DEFAULT_EMBEDDING_CONFIG,
+ tags=data["tags"],
+ created_at=data["created_at"],
+ ),
+ actor=default_user,
+ )
+ created_passages.append(passage)
+
+ # Test 1: Basic search by query text
+ results = await server.agent_manager.search_agent_archival_memory_async(
+ agent_id=sarah_agent.id, actor=default_user, query="Python programming"
+ )
+
+ assert len(results) > 0
+
+ # Check structure of results
+ for result in results:
+ assert "timestamp" in result
+ assert "content" in result
+ assert "tags" in result
+ assert isinstance(result["tags"], list)
+
+ # Test 2: Search with tag filtering - single tag
+ results = await server.agent_manager.search_agent_archival_memory_async(
+ agent_id=sarah_agent.id, actor=default_user, query="programming", tags=["python"]
+ )
+
+ assert len(results) > 0
+ # All results should have "python" tag
+ for result in results:
+ assert "python" in result["tags"]
+
+ # Test 3: Search with tag filtering - multiple tags with "any" mode
+ results = await server.agent_manager.search_agent_archival_memory_async(
+ agent_id=sarah_agent.id, actor=default_user, query="development", tags=["web", "database"], tag_match_mode="any"
+ )
+
+ assert len(results) > 0
+ # All results should have at least one of the specified tags
+ for result in results:
+ assert any(tag in result["tags"] for tag in ["web", "database"])
+
+ # Test 4: Search with tag filtering - multiple tags with "all" mode
+ results = await server.agent_manager.search_agent_archival_memory_async(
+ agent_id=sarah_agent.id, actor=default_user, query="Python", tags=["python", "web"], tag_match_mode="all"
+ )
+
+ # Should only return results that have BOTH tags
+ for result in results:
+ assert "python" in result["tags"]
+ assert "web" in result["tags"]
+
+ # Test 5: Search with top_k limit
+ results = await server.agent_manager.search_agent_archival_memory_async(
+ agent_id=sarah_agent.id, actor=default_user, query="programming", top_k=2
+ )
+
+ assert len(results) <= 2
+
+ # Test 6: Search with datetime filtering
+ results = await server.agent_manager.search_agent_archival_memory_async(
+ agent_id=sarah_agent.id, actor=default_user, query="programming", start_datetime="2024-01-16", end_datetime="2024-01-17"
+ )
+
+ # Should only include passages created between those dates
+ for result in results:
+ # Parse timestamp to verify it's in range
+ timestamp_str = result["timestamp"]
+ # Basic validation that timestamp exists and has expected format
+ assert "2024-01-16" in timestamp_str or "2024-01-17" in timestamp_str
+
+ # Test 7: Search with ISO datetime format
+ results = await server.agent_manager.search_agent_archival_memory_async(
+ agent_id=sarah_agent.id,
+ actor=default_user,
+ query="algorithms",
+ start_datetime="2024-01-16T14:00:00",
+ end_datetime="2024-01-16T15:00:00",
+ )
+
+ # Should include the machine learning passage created at 14:45
+ assert len(results) >= 0 # Might be 0 if no results, but shouldn't error
+
+ # Test 8: Search with non-existent agent should raise error
+ non_existent_agent_id = "agent-00000000-0000-4000-8000-000000000000"
+
+ with pytest.raises(Exception): # Should raise NoResultFound or similar
+ await server.agent_manager.search_agent_archival_memory_async(agent_id=non_existent_agent_id, actor=default_user, query="test")
+
+ # Test 9: Search with invalid datetime format should raise ValueError
+ with pytest.raises(ValueError, match="Invalid start_datetime format"):
+ await server.agent_manager.search_agent_archival_memory_async(
+ agent_id=sarah_agent.id, actor=default_user, query="test", start_datetime="invalid-date"
+ )
+
+ # Test 10: Empty query should return empty results
+ results = await server.agent_manager.search_agent_archival_memory_async(agent_id=sarah_agent.id, actor=default_user, query="")
+
+ assert len(results) == 0 # Empty query should return 0 results
+
+ # Test 11: Whitespace-only query should also return empty results
+ results = await server.agent_manager.search_agent_archival_memory_async(agent_id=sarah_agent.id, actor=default_user, query=" \n\t ")
+
+ assert len(results) == 0 # Whitespace-only query should return 0 results
+
+ # Cleanup - delete the created passages
+ for passage in created_passages:
+ await server.passage_manager.delete_agent_passage_by_id_async(passage_id=passage.id, actor=default_user)
diff --git a/tests/managers/test_sandbox_manager.py b/tests/managers/test_sandbox_manager.py
new file mode 100644
index 000000000..be1dc56b5
--- /dev/null
+++ b/tests/managers/test_sandbox_manager.py
@@ -0,0 +1,301 @@
+import json
+import logging
+import os
+import random
+import re
+import string
+import time
+import uuid
+from datetime import datetime, timedelta, timezone
+from typing import List
+from unittest.mock import AsyncMock, Mock, patch
+
+import pytest
+from _pytest.python_api import approx
+from anthropic.types.beta import BetaMessage
+from anthropic.types.beta.messages import BetaMessageBatchIndividualResponse, BetaMessageBatchSucceededResult
+
+# Import shared fixtures and constants from conftest
+from conftest import (
+ CREATE_DELAY_SQLITE,
+ DEFAULT_EMBEDDING_CONFIG,
+ USING_SQLITE,
+)
+from openai.types.chat.chat_completion_message_tool_call import ChatCompletionMessageToolCall as OpenAIToolCall, Function as OpenAIFunction
+from sqlalchemy import func, select
+from sqlalchemy.exc import IntegrityError, InvalidRequestError
+from sqlalchemy.orm.exc import StaleDataError
+
+from letta.config import LettaConfig
+from letta.constants import (
+ BASE_MEMORY_TOOLS,
+ BASE_SLEEPTIME_TOOLS,
+ BASE_TOOLS,
+ BASE_VOICE_SLEEPTIME_CHAT_TOOLS,
+ BASE_VOICE_SLEEPTIME_TOOLS,
+ BUILTIN_TOOLS,
+ DEFAULT_ORG_ID,
+ DEFAULT_ORG_NAME,
+ FILES_TOOLS,
+ LETTA_TOOL_EXECUTION_DIR,
+ LETTA_TOOL_SET,
+ LOCAL_ONLY_MULTI_AGENT_TOOLS,
+ MCP_TOOL_TAG_NAME_PREFIX,
+ MULTI_AGENT_TOOLS,
+)
+from letta.data_sources.redis_client import NoopAsyncRedisClient, get_redis_client
+from letta.errors import LettaAgentNotFoundError
+from letta.functions.functions import derive_openai_json_schema, parse_source_code
+from letta.functions.mcp_client.types import MCPTool
+from letta.helpers import ToolRulesSolver
+from letta.helpers.datetime_helpers import AsyncTimer
+from letta.jobs.types import ItemUpdateInfo, RequestStatusUpdateInfo, StepStatusUpdateInfo
+from letta.orm import Base, Block
+from letta.orm.block_history import BlockHistory
+from letta.orm.errors import NoResultFound, UniqueConstraintViolationError
+from letta.orm.file import FileContent as FileContentModel, FileMetadata as FileMetadataModel
+from letta.schemas.agent import CreateAgent, UpdateAgent
+from letta.schemas.block import Block as PydanticBlock, BlockUpdate, CreateBlock
+from letta.schemas.embedding_config import EmbeddingConfig
+from letta.schemas.enums import (
+ ActorType,
+ AgentStepStatus,
+ FileProcessingStatus,
+ JobStatus,
+ JobType,
+ MessageRole,
+ ProviderType,
+ SandboxType,
+ StepStatus,
+ TagMatchMode,
+ ToolType,
+ VectorDBProvider,
+)
+from letta.schemas.environment_variables import SandboxEnvironmentVariableCreate, SandboxEnvironmentVariableUpdate
+from letta.schemas.file import FileMetadata, FileMetadata as PydanticFileMetadata
+from letta.schemas.identity import IdentityCreate, IdentityProperty, IdentityPropertyType, IdentityType, IdentityUpdate, IdentityUpsert
+from letta.schemas.job import BatchJob, Job, Job as PydanticJob, JobUpdate, LettaRequestConfig
+from letta.schemas.letta_message import UpdateAssistantMessage, UpdateReasoningMessage, UpdateSystemMessage, UpdateUserMessage
+from letta.schemas.letta_message_content import TextContent
+from letta.schemas.letta_stop_reason import LettaStopReason, StopReasonType
+from letta.schemas.llm_batch_job import AgentStepState, LLMBatchItem
+from letta.schemas.llm_config import LLMConfig
+from letta.schemas.message import Message as PydanticMessage, MessageCreate, MessageUpdate
+from letta.schemas.openai.chat_completion_response import UsageStatistics
+from letta.schemas.organization import Organization, Organization as PydanticOrganization, OrganizationUpdate
+from letta.schemas.passage import Passage as PydanticPassage
+from letta.schemas.pip_requirement import PipRequirement
+from letta.schemas.run import Run as PydanticRun
+from letta.schemas.sandbox_config import E2BSandboxConfig, LocalSandboxConfig, SandboxConfigCreate, SandboxConfigUpdate
+from letta.schemas.source import Source as PydanticSource, SourceUpdate
+from letta.schemas.tool import Tool as PydanticTool, ToolCreate, ToolUpdate
+from letta.schemas.tool_rule import InitToolRule
+from letta.schemas.user import User as PydanticUser, UserUpdate
+from letta.server.db import db_registry
+from letta.server.server import SyncServer
+from letta.services.block_manager import BlockManager
+from letta.services.helpers.agent_manager_helper import calculate_base_tools, calculate_multi_agent_tools, validate_agent_exists_async
+from letta.services.step_manager import FeedbackType
+from letta.settings import settings, tool_settings
+from letta.utils import calculate_file_defaults_based_on_context_window
+from tests.helpers.utils import comprehensive_agent_checks, validate_context_window_overview
+from tests.utils import random_string
+
+# ======================================================================================================================
+# SandboxConfigManager Tests - Sandbox Configs
+# ======================================================================================================================
+
+
+async def test_create_or_update_sandbox_config(server: SyncServer, default_user):
+ sandbox_config_create = SandboxConfigCreate(
+ config=E2BSandboxConfig(),
+ )
+ created_config = await server.sandbox_config_manager.create_or_update_sandbox_config_async(sandbox_config_create, actor=default_user)
+
+ # Assertions
+ assert created_config.type == SandboxType.E2B
+ assert created_config.get_e2b_config() == sandbox_config_create.config
+ assert created_config.organization_id == default_user.organization_id
+
+
+async def test_create_local_sandbox_config_defaults(server: SyncServer, default_user):
+ sandbox_config_create = SandboxConfigCreate(
+ config=LocalSandboxConfig(),
+ )
+ created_config = await server.sandbox_config_manager.create_or_update_sandbox_config_async(sandbox_config_create, actor=default_user)
+
+ # Assertions
+ assert created_config.type == SandboxType.LOCAL
+ assert created_config.get_local_config() == sandbox_config_create.config
+ assert created_config.get_local_config().sandbox_dir in {LETTA_TOOL_EXECUTION_DIR, tool_settings.tool_exec_dir}
+ assert created_config.organization_id == default_user.organization_id
+
+
+async def test_default_e2b_settings_sandbox_config(server: SyncServer, default_user):
+ created_config = await server.sandbox_config_manager.get_or_create_default_sandbox_config_async(
+ sandbox_type=SandboxType.E2B, actor=default_user
+ )
+ e2b_config = created_config.get_e2b_config()
+
+ # Assertions
+ assert e2b_config.timeout == 5 * 60
+ assert e2b_config.template == tool_settings.e2b_sandbox_template_id
+
+
+async def test_update_existing_sandbox_config(server: SyncServer, sandbox_config_fixture, default_user):
+ update_data = SandboxConfigUpdate(config=E2BSandboxConfig(template="template_2", timeout=120))
+ updated_config = await server.sandbox_config_manager.update_sandbox_config_async(
+ sandbox_config_fixture.id, update_data, actor=default_user
+ )
+
+ # Assertions
+ assert updated_config.config["template"] == "template_2"
+ assert updated_config.config["timeout"] == 120
+
+
+async def test_delete_sandbox_config(server: SyncServer, sandbox_config_fixture, default_user):
+ deleted_config = await server.sandbox_config_manager.delete_sandbox_config_async(sandbox_config_fixture.id, actor=default_user)
+
+ # Assertions to verify deletion
+ assert deleted_config.id == sandbox_config_fixture.id
+
+ # Verify it no longer exists
+ config_list = await server.sandbox_config_manager.list_sandbox_configs_async(actor=default_user)
+ assert sandbox_config_fixture.id not in [config.id for config in config_list]
+
+
+async def test_get_sandbox_config_by_type(server: SyncServer, sandbox_config_fixture, default_user):
+ retrieved_config = await server.sandbox_config_manager.get_sandbox_config_by_type_async(sandbox_config_fixture.type, actor=default_user)
+
+ # Assertions to verify correct retrieval
+ assert retrieved_config.id == sandbox_config_fixture.id
+ assert retrieved_config.type == sandbox_config_fixture.type
+
+
+async def test_list_sandbox_configs(server: SyncServer, default_user):
+ # Creating multiple sandbox configs
+ config_e2b_create = SandboxConfigCreate(
+ config=E2BSandboxConfig(),
+ )
+ config_local_create = SandboxConfigCreate(
+ config=LocalSandboxConfig(sandbox_dir=""),
+ )
+ config_e2b = await server.sandbox_config_manager.create_or_update_sandbox_config_async(config_e2b_create, actor=default_user)
+ if USING_SQLITE:
+ time.sleep(CREATE_DELAY_SQLITE)
+ config_local = await server.sandbox_config_manager.create_or_update_sandbox_config_async(config_local_create, actor=default_user)
+
+ # List configs without pagination
+ configs = await server.sandbox_config_manager.list_sandbox_configs_async(actor=default_user)
+ assert len(configs) >= 2
+
+ # List configs with pagination
+ paginated_configs = await server.sandbox_config_manager.list_sandbox_configs_async(actor=default_user, limit=1)
+ assert len(paginated_configs) == 1
+
+ next_page = await server.sandbox_config_manager.list_sandbox_configs_async(actor=default_user, after=paginated_configs[-1].id, limit=1)
+ assert len(next_page) == 1
+ assert next_page[0].id != paginated_configs[0].id
+
+ # List configs using sandbox_type filter
+ configs = await server.sandbox_config_manager.list_sandbox_configs_async(actor=default_user, sandbox_type=SandboxType.E2B)
+ assert len(configs) == 1
+ assert configs[0].id == config_e2b.id
+
+ configs = await server.sandbox_config_manager.list_sandbox_configs_async(actor=default_user, sandbox_type=SandboxType.LOCAL)
+ assert len(configs) == 1
+ assert configs[0].id == config_local.id
+
+
+# ======================================================================================================================
+# SandboxConfigManager Tests - Environment Variables
+# ======================================================================================================================
+
+
+async def test_create_sandbox_env_var(server: SyncServer, sandbox_config_fixture, default_user):
+ env_var_create = SandboxEnvironmentVariableCreate(key="TEST_VAR", value="test_value", description="A test environment variable.")
+ created_env_var = await server.sandbox_config_manager.create_sandbox_env_var_async(
+ env_var_create, sandbox_config_id=sandbox_config_fixture.id, actor=default_user
+ )
+
+ # Assertions
+ assert created_env_var.key == env_var_create.key
+ assert created_env_var.value == env_var_create.value
+ assert created_env_var.organization_id == default_user.organization_id
+
+
+async def test_update_sandbox_env_var(server: SyncServer, sandbox_env_var_fixture, default_user):
+ update_data = SandboxEnvironmentVariableUpdate(value="updated_value")
+ updated_env_var = await server.sandbox_config_manager.update_sandbox_env_var_async(
+ sandbox_env_var_fixture.id, update_data, actor=default_user
+ )
+
+ # Assertions
+ assert updated_env_var.value == "updated_value"
+ assert updated_env_var.id == sandbox_env_var_fixture.id
+
+
+async def test_delete_sandbox_env_var(server: SyncServer, sandbox_config_fixture, sandbox_env_var_fixture, default_user):
+ deleted_env_var = await server.sandbox_config_manager.delete_sandbox_env_var_async(sandbox_env_var_fixture.id, actor=default_user)
+
+ # Assertions to verify deletion
+ assert deleted_env_var.id == sandbox_env_var_fixture.id
+
+ # Verify it no longer exists
+ env_vars = await server.sandbox_config_manager.list_sandbox_env_vars_async(
+ sandbox_config_id=sandbox_config_fixture.id, actor=default_user
+ )
+ assert sandbox_env_var_fixture.id not in [env_var.id for env_var in env_vars]
+
+
+async def test_list_sandbox_env_vars(server: SyncServer, sandbox_config_fixture, default_user):
+ # Creating multiple environment variables
+ env_var_create_a = SandboxEnvironmentVariableCreate(key="VAR1", value="value1")
+ env_var_create_b = SandboxEnvironmentVariableCreate(key="VAR2", value="value2")
+ await server.sandbox_config_manager.create_sandbox_env_var_async(
+ env_var_create_a, sandbox_config_id=sandbox_config_fixture.id, actor=default_user
+ )
+ if USING_SQLITE:
+ time.sleep(CREATE_DELAY_SQLITE)
+ await server.sandbox_config_manager.create_sandbox_env_var_async(
+ env_var_create_b, sandbox_config_id=sandbox_config_fixture.id, actor=default_user
+ )
+
+ # List env vars without pagination
+ env_vars = await server.sandbox_config_manager.list_sandbox_env_vars_async(
+ sandbox_config_id=sandbox_config_fixture.id, actor=default_user
+ )
+ assert len(env_vars) >= 2
+
+ # List env vars with pagination
+ paginated_env_vars = await server.sandbox_config_manager.list_sandbox_env_vars_async(
+ sandbox_config_id=sandbox_config_fixture.id, actor=default_user, limit=1
+ )
+ assert len(paginated_env_vars) == 1
+
+ next_page = await server.sandbox_config_manager.list_sandbox_env_vars_async(
+ sandbox_config_id=sandbox_config_fixture.id, actor=default_user, after=paginated_env_vars[-1].id, limit=1
+ )
+ assert len(next_page) == 1
+ assert next_page[0].id != paginated_env_vars[0].id
+
+
+async def test_get_sandbox_env_var_by_key(server: SyncServer, sandbox_env_var_fixture, default_user):
+ retrieved_env_var = await server.sandbox_config_manager.get_sandbox_env_var_by_key_and_sandbox_config_id_async(
+ sandbox_env_var_fixture.key, sandbox_env_var_fixture.sandbox_config_id, actor=default_user
+ )
+
+ # Assertions to verify correct retrieval
+ assert retrieved_env_var.id == sandbox_env_var_fixture.id
diff --git a/tests/managers/test_source_manager.py b/tests/managers/test_source_manager.py
new file mode 100644
index 000000000..dc45d7c53
--- /dev/null
+++ b/tests/managers/test_source_manager.py
@@ -0,0 +1,1663 @@
+import json
+import logging
+import os
+import random
+import re
+import string
+import time
+import uuid
+from datetime import datetime, timedelta, timezone
+from typing import List
+from unittest.mock import AsyncMock, Mock, patch
+
+import pytest
+from _pytest.python_api import approx
+from anthropic.types.beta import BetaMessage
+from anthropic.types.beta.messages import BetaMessageBatchIndividualResponse, BetaMessageBatchSucceededResult
+
+# Import shared fixtures and constants from conftest
+from conftest import (
+ CREATE_DELAY_SQLITE,
+ DEFAULT_EMBEDDING_CONFIG,
+ USING_SQLITE,
+)
+from openai.types.chat.chat_completion_message_tool_call import ChatCompletionMessageToolCall as OpenAIToolCall, Function as OpenAIFunction
+from sqlalchemy import func, select
+from sqlalchemy.exc import IntegrityError, InvalidRequestError
+from sqlalchemy.orm.exc import StaleDataError
+
+from letta.config import LettaConfig
+from letta.constants import (
+ BASE_MEMORY_TOOLS,
+ BASE_SLEEPTIME_TOOLS,
+ BASE_TOOLS,
+ BASE_VOICE_SLEEPTIME_CHAT_TOOLS,
+ BASE_VOICE_SLEEPTIME_TOOLS,
+ BUILTIN_TOOLS,
+ DEFAULT_ORG_ID,
+ DEFAULT_ORG_NAME,
+ FILES_TOOLS,
+ LETTA_TOOL_EXECUTION_DIR,
+ LETTA_TOOL_SET,
+ LOCAL_ONLY_MULTI_AGENT_TOOLS,
+ MCP_TOOL_TAG_NAME_PREFIX,
+ MULTI_AGENT_TOOLS,
+)
+from letta.data_sources.redis_client import NoopAsyncRedisClient, get_redis_client
+from letta.errors import LettaAgentNotFoundError
+from letta.functions.functions import derive_openai_json_schema, parse_source_code
+from letta.functions.mcp_client.types import MCPTool
+from letta.helpers import ToolRulesSolver
+from letta.helpers.datetime_helpers import AsyncTimer
+from letta.jobs.types import ItemUpdateInfo, RequestStatusUpdateInfo, StepStatusUpdateInfo
+from letta.orm import Base, Block
+from letta.orm.block_history import BlockHistory
+from letta.orm.errors import NoResultFound, UniqueConstraintViolationError
+from letta.orm.file import FileContent as FileContentModel, FileMetadata as FileMetadataModel
+from letta.schemas.agent import CreateAgent, UpdateAgent
+from letta.schemas.block import Block as PydanticBlock, BlockUpdate, CreateBlock
+from letta.schemas.embedding_config import EmbeddingConfig
+from letta.schemas.enums import (
+ ActorType,
+ AgentStepStatus,
+ FileProcessingStatus,
+ JobStatus,
+ JobType,
+ MessageRole,
+ ProviderType,
+ SandboxType,
+ StepStatus,
+ TagMatchMode,
+ ToolType,
+ VectorDBProvider,
+)
+from letta.schemas.environment_variables import SandboxEnvironmentVariableCreate, SandboxEnvironmentVariableUpdate
+from letta.schemas.file import FileMetadata, FileMetadata as PydanticFileMetadata
+from letta.schemas.identity import IdentityCreate, IdentityProperty, IdentityPropertyType, IdentityType, IdentityUpdate, IdentityUpsert
+from letta.schemas.job import BatchJob, Job, Job as PydanticJob, JobUpdate, LettaRequestConfig
+from letta.schemas.letta_message import UpdateAssistantMessage, UpdateReasoningMessage, UpdateSystemMessage, UpdateUserMessage
+from letta.schemas.letta_message_content import TextContent
+from letta.schemas.letta_stop_reason import LettaStopReason, StopReasonType
+from letta.schemas.llm_batch_job import AgentStepState, LLMBatchItem
+from letta.schemas.llm_config import LLMConfig
+from letta.schemas.message import Message as PydanticMessage, MessageCreate, MessageUpdate
+from letta.schemas.openai.chat_completion_response import UsageStatistics
+from letta.schemas.organization import Organization, Organization as PydanticOrganization, OrganizationUpdate
+from letta.schemas.passage import Passage as PydanticPassage
+from letta.schemas.pip_requirement import PipRequirement
+from letta.schemas.run import Run as PydanticRun
+from letta.schemas.sandbox_config import E2BSandboxConfig, LocalSandboxConfig, SandboxConfigCreate, SandboxConfigUpdate
+from letta.schemas.source import Source as PydanticSource, SourceUpdate
+from letta.schemas.tool import Tool as PydanticTool, ToolCreate, ToolUpdate
+from letta.schemas.tool_rule import InitToolRule
+from letta.schemas.user import User as PydanticUser, UserUpdate
+from letta.server.db import db_registry
+from letta.server.server import SyncServer
+from letta.services.block_manager import BlockManager
+from letta.services.helpers.agent_manager_helper import calculate_base_tools, calculate_multi_agent_tools, validate_agent_exists_async
+from letta.services.step_manager import FeedbackType
+from letta.settings import settings, tool_settings
+from letta.utils import calculate_file_defaults_based_on_context_window
+from tests.helpers.utils import comprehensive_agent_checks, validate_context_window_overview
+from tests.utils import random_string
+
+
+# Helper function for file content tests
+async def _count_file_content_rows(session, file_id: str) -> int:
+ q = select(func.count()).select_from(FileContentModel).where(FileContentModel.file_id == file_id)
+ result = await session.execute(q)
+ return result.scalar_one()
+
+
+# ======================================================================================================================
+# AgentManager Tests - Sources Relationship
+# ======================================================================================================================
+
+
+async def test_attach_source(server: SyncServer, sarah_agent, default_source, default_user):
+ """Test attaching a source to an agent."""
+ # Attach the source
+ await server.agent_manager.attach_source_async(agent_id=sarah_agent.id, source_id=default_source.id, actor=default_user)
+
+ # Verify attachment through get_agent_by_id
+ agent = await server.agent_manager.get_agent_by_id_async(sarah_agent.id, actor=default_user)
+ assert default_source.id in [s.id for s in agent.sources]
+
+ # Verify that attaching the same source again doesn't cause issues
+ await server.agent_manager.attach_source_async(agent_id=sarah_agent.id, source_id=default_source.id, actor=default_user)
+ agent = await server.agent_manager.get_agent_by_id_async(sarah_agent.id, actor=default_user)
+ assert len([s for s in agent.sources if s.id == default_source.id]) == 1
+
+
+async def test_list_attached_source_ids(server: SyncServer, sarah_agent, default_source, other_source, default_user):
+ """Test listing source IDs attached to an agent."""
+ # Initially should have no sources
+ sources = await server.agent_manager.list_attached_sources_async(sarah_agent.id, actor=default_user)
+ assert len(sources) == 0
+
+ # Attach sources
+ await server.agent_manager.attach_source_async(sarah_agent.id, default_source.id, actor=default_user)
+ await server.agent_manager.attach_source_async(sarah_agent.id, other_source.id, actor=default_user)
+
+ # List sources and verify
+ sources = await server.agent_manager.list_attached_sources_async(sarah_agent.id, actor=default_user)
+ assert len(sources) == 2
+ source_ids = [s.id for s in sources]
+ assert default_source.id in source_ids
+ assert other_source.id in source_ids
+
+
+async def test_detach_source(server: SyncServer, sarah_agent, default_source, default_user):
+ """Test detaching a source from an agent."""
+ # Attach source
+ await server.agent_manager.attach_source_async(sarah_agent.id, default_source.id, actor=default_user)
+
+ # Verify it's attached
+ agent = await server.agent_manager.get_agent_by_id_async(sarah_agent.id, actor=default_user)
+ assert default_source.id in [s.id for s in agent.sources]
+
+ # Detach source
+ await server.agent_manager.detach_source_async(sarah_agent.id, default_source.id, actor=default_user)
+
+ # Verify it's detached
+ agent = await server.agent_manager.get_agent_by_id_async(sarah_agent.id, actor=default_user)
+ assert default_source.id not in [s.id for s in agent.sources]
+
+ # Verify that detaching an already detached source doesn't cause issues
+ await server.agent_manager.detach_source_async(sarah_agent.id, default_source.id, actor=default_user)
+
+
+async def test_attach_source_nonexistent_agent(server: SyncServer, default_source, default_user):
+ """Test attaching a source to a nonexistent agent."""
+ with pytest.raises(NoResultFound):
+ await server.agent_manager.attach_source_async(agent_id="nonexistent-agent-id", source_id=default_source.id, actor=default_user)
+
+
+async def test_attach_source_nonexistent_source(server: SyncServer, sarah_agent, default_user):
+ """Test attaching a nonexistent source to an agent."""
+ with pytest.raises(NoResultFound):
+ await server.agent_manager.attach_source_async(agent_id=sarah_agent.id, source_id="nonexistent-source-id", actor=default_user)
+
+
+async def test_detach_source_nonexistent_agent(server: SyncServer, default_source, default_user):
+ """Test detaching a source from a nonexistent agent."""
+ with pytest.raises(LettaAgentNotFoundError):
+ await server.agent_manager.detach_source_async(agent_id="nonexistent-agent-id", source_id=default_source.id, actor=default_user)
+
+
+async def test_list_attached_source_ids_nonexistent_agent(server: SyncServer, default_user):
+ """Test listing sources for a nonexistent agent."""
+ with pytest.raises(LettaAgentNotFoundError):
+ await server.agent_manager.list_attached_sources_async(agent_id="nonexistent-agent-id", actor=default_user)
+
+
+async def test_list_attached_agents(server: SyncServer, sarah_agent, charles_agent, default_source, default_user):
+ """Test listing agents that have a particular source attached."""
+ # Initially should have no attached agents
+ attached_agents = await server.source_manager.list_attached_agents(source_id=default_source.id, actor=default_user)
+ assert len(attached_agents) == 0
+
+ # Attach source to first agent
+ await server.agent_manager.attach_source_async(agent_id=sarah_agent.id, source_id=default_source.id, actor=default_user)
+
+ # Verify one agent is now attached
+ attached_agents = await server.source_manager.list_attached_agents(source_id=default_source.id, actor=default_user)
+ assert len(attached_agents) == 1
+ assert sarah_agent.id in [a.id for a in attached_agents]
+
+ # Attach source to second agent
+ await server.agent_manager.attach_source_async(agent_id=charles_agent.id, source_id=default_source.id, actor=default_user)
+
+ # Verify both agents are now attached
+ attached_agents = await server.source_manager.list_attached_agents(source_id=default_source.id, actor=default_user)
+ assert len(attached_agents) == 2
+ attached_agent_ids = [a.id for a in attached_agents]
+ assert sarah_agent.id in attached_agent_ids
+ assert charles_agent.id in attached_agent_ids
+
+ # Detach source from first agent
+ await server.agent_manager.detach_source_async(agent_id=sarah_agent.id, source_id=default_source.id, actor=default_user)
+
+ # Verify only second agent remains attached
+ attached_agents = await server.source_manager.list_attached_agents(source_id=default_source.id, actor=default_user)
+ assert len(attached_agents) == 1
+ assert charles_agent.id in [a.id for a in attached_agents]
+
+
+async def test_list_attached_agents_nonexistent_source(server: SyncServer, default_user):
+ """Test listing agents for a nonexistent source."""
+ with pytest.raises(NoResultFound):
+ await server.source_manager.list_attached_agents(source_id="nonexistent-source-id", actor=default_user)
+
+
+# ======================================================================================================================
+# SourceManager Tests - Sources
+# ======================================================================================================================
+
+
+async def test_get_existing_source_names(server: SyncServer, default_user):
+ """Test the fast batch check for existing source names."""
+ # Create some test sources
+ source1 = PydanticSource(
+ name="test_source_1",
+ embedding_config=EmbeddingConfig(
+ embedding_endpoint_type="openai",
+ embedding_endpoint="https://api.openai.com/v1",
+ embedding_model="text-embedding-ada-002",
+ embedding_dim=1536,
+ embedding_chunk_size=300,
+ ),
+ )
+ source2 = PydanticSource(
+ name="test_source_2",
+ embedding_config=EmbeddingConfig(
+ embedding_endpoint_type="openai",
+ embedding_endpoint="https://api.openai.com/v1",
+ embedding_model="text-embedding-ada-002",
+ embedding_dim=1536,
+ embedding_chunk_size=300,
+ ),
+ )
+
+ # Create the sources
+ created_source1 = await server.source_manager.create_source(source1, default_user)
+ created_source2 = await server.source_manager.create_source(source2, default_user)
+
+ # Test batch check - mix of existing and non-existing names
+ names_to_check = ["test_source_1", "test_source_2", "non_existent_source", "another_non_existent"]
+ existing_names = await server.source_manager.get_existing_source_names(names_to_check, default_user)
+
+ # Verify results
+ assert len(existing_names) == 2
+ assert "test_source_1" in existing_names
+ assert "test_source_2" in existing_names
+ assert "non_existent_source" not in existing_names
+ assert "another_non_existent" not in existing_names
+
+ # Test with empty list
+ empty_result = await server.source_manager.get_existing_source_names([], default_user)
+ assert len(empty_result) == 0
+
+ # Test with all non-existing names
+ non_existing_result = await server.source_manager.get_existing_source_names(["fake1", "fake2"], default_user)
+ assert len(non_existing_result) == 0
+
+ # Cleanup
+ await server.source_manager.delete_source(created_source1.id, default_user)
+ await server.source_manager.delete_source(created_source2.id, default_user)
+
+
+async def test_create_source(server: SyncServer, default_user):
+ """Test creating a new source."""
+ source_pydantic = PydanticSource(
+ name="Test Source",
+ description="This is a test source.",
+ metadata={"type": "test"},
+ embedding_config=DEFAULT_EMBEDDING_CONFIG,
+ )
+ source = await server.source_manager.create_source(source=source_pydantic, actor=default_user)
+
+ # Assertions to check the created source
+ assert source.name == source_pydantic.name
+ assert source.description == source_pydantic.description
+ assert source.metadata == source_pydantic.metadata
+ assert source.organization_id == default_user.organization_id
+
+
+async def test_source_vector_db_provider_with_tpuf(server: SyncServer, default_user):
+ """Test that vector_db_provider is correctly set based on should_use_tpuf."""
+ from letta.settings import settings
+
+ # save original values
+ original_use_tpuf = settings.use_tpuf
+ original_tpuf_api_key = settings.tpuf_api_key
+
+ try:
+ # test when should_use_tpuf returns True (expect TPUF provider)
+ settings.use_tpuf = True
+ settings.tpuf_api_key = "test_key"
+
+ # need to mock it in source_manager since it's already imported
+ with patch("letta.services.source_manager.should_use_tpuf", return_value=True):
+ source_pydantic = PydanticSource(
+ name="Test Source TPUF",
+ description="Source with TPUF provider",
+ metadata={"type": "test"},
+ embedding_config=DEFAULT_EMBEDDING_CONFIG,
+ vector_db_provider=VectorDBProvider.TPUF, # explicitly set it
+ )
+ assert source_pydantic.vector_db_provider == VectorDBProvider.TPUF
+
+ # create source and verify it's saved with TPUF provider
+ source = await server.source_manager.create_source(source=source_pydantic, actor=default_user)
+ assert source.vector_db_provider == VectorDBProvider.TPUF
+
+ # test when should_use_tpuf returns False (expect NATIVE provider)
+ settings.use_tpuf = False
+ settings.tpuf_api_key = None
+
+ with patch("letta.services.source_manager.should_use_tpuf", return_value=False):
+ source_pydantic = PydanticSource(
+ name="Test Source Native",
+ description="Source with Native provider",
+ metadata={"type": "test"},
+ embedding_config=DEFAULT_EMBEDDING_CONFIG,
+ vector_db_provider=VectorDBProvider.NATIVE, # explicitly set it
+ )
+ assert source_pydantic.vector_db_provider == VectorDBProvider.NATIVE
+
+ # create source and verify it's saved with NATIVE provider
+ source = await server.source_manager.create_source(source=source_pydantic, actor=default_user)
+ assert source.vector_db_provider == VectorDBProvider.NATIVE
+ finally:
+ # restore original values
+ settings.use_tpuf = original_use_tpuf
+ settings.tpuf_api_key = original_tpuf_api_key
+
+
+async def test_create_sources_with_same_name_raises_error(server: SyncServer, default_user):
+ """Test that creating sources with the same name raises an IntegrityError due to unique constraint."""
+ name = "Test Source"
+ source_pydantic = PydanticSource(
+ name=name,
+ description="This is a test source.",
+ metadata={"type": "medical"},
+ embedding_config=DEFAULT_EMBEDDING_CONFIG,
+ )
+ source = await server.source_manager.create_source(source=source_pydantic, actor=default_user)
+
+ # Attempting to create another source with the same name should raise an IntegrityError
+ source_pydantic = PydanticSource(
+ name=name,
+ description="This is a different test source.",
+ metadata={"type": "legal"},
+ embedding_config=DEFAULT_EMBEDDING_CONFIG,
+ )
+ with pytest.raises(UniqueConstraintViolationError):
+ await server.source_manager.create_source(source=source_pydantic, actor=default_user)
+
+
+async def test_update_source(server: SyncServer, default_user):
+ """Test updating an existing source."""
+ source_pydantic = PydanticSource(name="Original Source", description="Original description", embedding_config=DEFAULT_EMBEDDING_CONFIG)
+ source = await server.source_manager.create_source(source=source_pydantic, actor=default_user)
+
+ # Update the source
+ update_data = SourceUpdate(name="Updated Source", description="Updated description", metadata={"type": "updated"})
+ updated_source = await server.source_manager.update_source(source_id=source.id, source_update=update_data, actor=default_user)
+
+ # Assertions to verify update
+ assert updated_source.name == update_data.name
+ assert updated_source.description == update_data.description
+ assert updated_source.metadata == update_data.metadata
+
+
+async def test_delete_source(server: SyncServer, default_user):
+ """Test deleting a source."""
+ source_pydantic = PydanticSource(
+ name="To Delete", description="This source will be deleted.", embedding_config=DEFAULT_EMBEDDING_CONFIG
+ )
+ source = await server.source_manager.create_source(source=source_pydantic, actor=default_user)
+
+ # Delete the source
+ deleted_source = await server.source_manager.delete_source(source_id=source.id, actor=default_user)
+
+ # Assertions to verify deletion
+ assert deleted_source.id == source.id
+
+ # Verify that the source no longer appears in list_sources
+ sources = await server.source_manager.list_sources(actor=default_user)
+ assert len(sources) == 0
+
+
+async def test_delete_attached_source(server: SyncServer, sarah_agent, default_user):
+ """Test deleting a source."""
+ source_pydantic = PydanticSource(
+ name="To Delete", description="This source will be deleted.", embedding_config=DEFAULT_EMBEDDING_CONFIG
+ )
+ source = await server.source_manager.create_source(source=source_pydantic, actor=default_user)
+
+ await server.agent_manager.attach_source_async(agent_id=sarah_agent.id, source_id=source.id, actor=default_user)
+
+ # Delete the source
+ deleted_source = await server.source_manager.delete_source(source_id=source.id, actor=default_user)
+
+ # Assertions to verify deletion
+ assert deleted_source.id == source.id
+
+ # Verify that the source no longer appears in list_sources
+ sources = await server.source_manager.list_sources(actor=default_user)
+ assert len(sources) == 0
+
+ # Verify that agent is not deleted
+ agent = await server.agent_manager.get_agent_by_id_async(sarah_agent.id, actor=default_user)
+ assert agent is not None
+
+
+async def test_list_sources(server: SyncServer, default_user):
+ """Test listing sources with pagination."""
+ # Create multiple sources
+ await server.source_manager.create_source(
+ PydanticSource(name="Source 1", embedding_config=DEFAULT_EMBEDDING_CONFIG), actor=default_user
+ )
+ if USING_SQLITE:
+ time.sleep(CREATE_DELAY_SQLITE)
+ await server.source_manager.create_source(
+ PydanticSource(name="Source 2", embedding_config=DEFAULT_EMBEDDING_CONFIG), actor=default_user
+ )
+
+ # List sources without pagination
+ sources = await server.source_manager.list_sources(actor=default_user)
+ assert len(sources) == 2
+
+ # List sources with pagination
+ paginated_sources = await server.source_manager.list_sources(actor=default_user, limit=1)
+ assert len(paginated_sources) == 1
+
+ # Ensure cursor-based pagination works
+ next_page = await server.source_manager.list_sources(actor=default_user, after=paginated_sources[-1].id, limit=1)
+ assert len(next_page) == 1
+ assert next_page[0].name != paginated_sources[0].name
+
+
+async def test_get_source_by_id(server: SyncServer, default_user):
+ """Test retrieving a source by ID."""
+ source_pydantic = PydanticSource(
+ name="Retrieve by ID", description="Test source for ID retrieval", embedding_config=DEFAULT_EMBEDDING_CONFIG
+ )
+ source = await server.source_manager.create_source(source=source_pydantic, actor=default_user)
+
+ # Retrieve the source by ID
+ retrieved_source = await server.source_manager.get_source_by_id(source_id=source.id, actor=default_user)
+
+ # Assertions to verify the retrieved source matches the created one
+ assert retrieved_source.id == source.id
+ assert retrieved_source.name == source.name
+ assert retrieved_source.description == source.description
+
+
+async def test_get_source_by_name(server: SyncServer, default_user):
+ """Test retrieving a source by name."""
+ source_pydantic = PydanticSource(
+ name="Unique Source", description="Test source for name retrieval", embedding_config=DEFAULT_EMBEDDING_CONFIG
+ )
+ source = await server.source_manager.create_source(source=source_pydantic, actor=default_user)
+
+ # Retrieve the source by name
+ retrieved_source = await server.source_manager.get_source_by_name(source_name=source.name, actor=default_user)
+
+ # Assertions to verify the retrieved source matches the created one
+ assert retrieved_source.name == source.name
+ assert retrieved_source.description == source.description
+
+
+async def test_update_source_no_changes(server: SyncServer, default_user):
+ """Test update_source with no actual changes to verify logging and response."""
+ source_pydantic = PydanticSource(name="No Change Source", description="No changes", embedding_config=DEFAULT_EMBEDDING_CONFIG)
+ source = await server.source_manager.create_source(source=source_pydantic, actor=default_user)
+
+ # Attempt to update the source with identical data
+ update_data = SourceUpdate(name="No Change Source", description="No changes")
+ updated_source = await server.source_manager.update_source(source_id=source.id, source_update=update_data, actor=default_user)
+
+ # Assertions to ensure the update returned the source but made no modifications
+ assert updated_source.id == source.id
+ assert updated_source.name == source.name
+ assert updated_source.description == source.description
+
+
+async def test_bulk_upsert_sources_async(server: SyncServer, default_user):
+ """Test bulk upserting sources."""
+ sources_data = [
+ PydanticSource(
+ name="Bulk Source 1",
+ description="First bulk source",
+ embedding_config=DEFAULT_EMBEDDING_CONFIG,
+ ),
+ PydanticSource(
+ name="Bulk Source 2",
+ description="Second bulk source",
+ embedding_config=DEFAULT_EMBEDDING_CONFIG,
+ ),
+ PydanticSource(
+ name="Bulk Source 3",
+ description="Third bulk source",
+ embedding_config=DEFAULT_EMBEDDING_CONFIG,
+ ),
+ ]
+
+ # Bulk upsert sources
+ created_sources = await server.source_manager.bulk_upsert_sources_async(sources_data, default_user)
+
+ # Verify all sources were created
+ assert len(created_sources) == 3
+
+ # Verify source details
+ created_names = {source.name for source in created_sources}
+ expected_names = {"Bulk Source 1", "Bulk Source 2", "Bulk Source 3"}
+ assert created_names == expected_names
+
+ # Verify organization assignment
+ for source in created_sources:
+ assert source.organization_id == default_user.organization_id
+
+
+async def test_bulk_upsert_sources_name_conflict(server: SyncServer, default_user):
+ """Test bulk upserting sources with name conflicts."""
+ # Create an existing source
+ existing_source = await server.source_manager.create_source(
+ PydanticSource(
+ name="Existing Source",
+ description="Already exists",
+ embedding_config=DEFAULT_EMBEDDING_CONFIG,
+ ),
+ default_user,
+ )
+
+ # Try to bulk upsert with the same name
+ sources_data = [
+ PydanticSource(
+ name="Existing Source", # Same name as existing
+ description="Updated description",
+ metadata={"updated": True},
+ embedding_config=DEFAULT_EMBEDDING_CONFIG,
+ ),
+ PydanticSource(
+ name="New Bulk Source",
+ description="Completely new",
+ embedding_config=DEFAULT_EMBEDDING_CONFIG,
+ ),
+ ]
+
+ # Bulk upsert should update existing and create new
+ result_sources = await server.source_manager.bulk_upsert_sources_async(sources_data, default_user)
+
+ # Should return 2 sources
+ assert len(result_sources) == 2
+
+ # Find the updated source
+ updated_source = next(s for s in result_sources if s.name == "Existing Source")
+
+ # Verify the existing source was updated, not replaced
+ assert updated_source.id == existing_source.id # ID should be preserved
+ assert updated_source.description == "Updated description"
+ assert updated_source.metadata == {"updated": True}
+
+ # Verify new source was created
+ new_source = next(s for s in result_sources if s.name == "New Bulk Source")
+ assert new_source.description == "Completely new"
+
+
+async def test_bulk_upsert_sources_mixed_create_update(server: SyncServer, default_user):
+ """Test bulk upserting with a mix of creates and updates."""
+ # Create some existing sources
+ existing1 = await server.source_manager.create_source(
+ PydanticSource(
+ name="Mixed Source 1",
+ description="Original 1",
+ embedding_config=DEFAULT_EMBEDDING_CONFIG,
+ ),
+ default_user,
+ )
+ existing2 = await server.source_manager.create_source(
+ PydanticSource(
+ name="Mixed Source 2",
+ description="Original 2",
+ embedding_config=DEFAULT_EMBEDDING_CONFIG,
+ ),
+ default_user,
+ )
+
+ # Bulk upsert with updates and new sources
+ sources_data = [
+ PydanticSource(
+ name="Mixed Source 1", # Update existing
+ description="Updated 1",
+ instructions="New instructions 1",
+ embedding_config=DEFAULT_EMBEDDING_CONFIG,
+ ),
+ PydanticSource(
+ name="Mixed Source 3", # Create new
+ description="New 3",
+ embedding_config=DEFAULT_EMBEDDING_CONFIG,
+ ),
+ PydanticSource(
+ name="Mixed Source 2", # Update existing
+ description="Updated 2",
+ metadata={"version": 2},
+ embedding_config=DEFAULT_EMBEDDING_CONFIG,
+ ),
+ PydanticSource(
+ name="Mixed Source 4", # Create new
+ description="New 4",
+ embedding_config=DEFAULT_EMBEDDING_CONFIG,
+ ),
+ ]
+
+ # Perform bulk upsert
+ result_sources = await server.source_manager.bulk_upsert_sources_async(sources_data, default_user)
+
+ # Should return 4 sources
+ assert len(result_sources) == 4
+
+ # Verify updates preserved IDs
+ source1 = next(s for s in result_sources if s.name == "Mixed Source 1")
+ assert source1.id == existing1.id
+ assert source1.description == "Updated 1"
+ assert source1.instructions == "New instructions 1"
+
+ source2 = next(s for s in result_sources if s.name == "Mixed Source 2")
+ assert source2.id == existing2.id
+ assert source2.description == "Updated 2"
+ assert source2.metadata == {"version": 2}
+
+ # Verify new sources were created
+ source3 = next(s for s in result_sources if s.name == "Mixed Source 3")
+ assert source3.description == "New 3"
+ assert source3.id != existing1.id and source3.id != existing2.id
+
+ source4 = next(s for s in result_sources if s.name == "Mixed Source 4")
+ assert source4.description == "New 4"
+ assert source4.id != existing1.id and source4.id != existing2.id
+
+
+# ======================================================================================================================
+# Source Manager Tests - Files
+# ======================================================================================================================
+
+
+async def test_get_file_by_id(server: SyncServer, default_user, default_source):
+ """Test retrieving a file by ID."""
+ file_metadata = PydanticFileMetadata(
+ file_name="Retrieve File",
+ file_path="/path/to/retrieve_file.txt",
+ file_type="text/plain",
+ file_size=2048,
+ source_id=default_source.id,
+ )
+ created_file = await server.file_manager.create_file(file_metadata=file_metadata, actor=default_user)
+
+ # Retrieve the file by ID
+ retrieved_file = await server.file_manager.get_file_by_id(file_id=created_file.id, actor=default_user)
+
+ # Assertions to verify the retrieved file matches the created one
+ assert retrieved_file.id == created_file.id
+ assert retrieved_file.file_name == created_file.file_name
+ assert retrieved_file.file_path == created_file.file_path
+ assert retrieved_file.file_type == created_file.file_type
+
+
+async def test_create_and_retrieve_file_with_content(server, default_user, default_source, async_session):
+ text_body = "Line 1\nLine 2\nLine 3"
+
+ meta = PydanticFileMetadata(
+ file_name="with_body.txt",
+ file_path="/tmp/with_body.txt",
+ file_type="text/plain",
+ file_size=len(text_body),
+ source_id=default_source.id,
+ )
+
+ created = await server.file_manager.create_file(
+ file_metadata=meta,
+ actor=default_user,
+ text=text_body,
+ )
+
+ # -- metadata-only return: content is NOT present
+ assert created.content is None
+
+ # body row exists
+ assert await _count_file_content_rows(async_session, created.id) == 1
+
+ # -- now fetch WITH the body
+ loaded = await server.file_manager.get_file_by_id(created.id, actor=default_user, include_content=True)
+ assert loaded.content == text_body
+
+
+async def test_create_file_without_content(server, default_user, default_source, async_session):
+ meta = PydanticFileMetadata(
+ file_name="no_body.txt",
+ file_path="/tmp/no_body.txt",
+ file_type="text/plain",
+ file_size=123,
+ source_id=default_source.id,
+ )
+ created = await server.file_manager.create_file(file_metadata=meta, actor=default_user)
+
+ # no content row
+ assert await _count_file_content_rows(async_session, created.id) == 0
+
+ # include_content=True still works, returns None
+ loaded = await server.file_manager.get_file_by_id(created.id, actor=default_user, include_content=True)
+ assert loaded.content is None
+
+
+async def test_lazy_raise_guard(server, default_user, default_source, async_session):
+ text_body = "lazy-raise"
+
+ meta = PydanticFileMetadata(
+ file_name="lazy_raise.txt",
+ file_path="/tmp/lazy_raise.txt",
+ file_type="text/plain",
+ file_size=len(text_body),
+ source_id=default_source.id,
+ )
+ created = await server.file_manager.create_file(file_metadata=meta, actor=default_user, text=text_body)
+
+ # Grab ORM instance WITHOUT selectinload(FileMetadata.content)
+ orm = await async_session.get(FileMetadataModel, created.id)
+
+ # to_pydantic(include_content=True) should raise – guard works
+ with pytest.raises(InvalidRequestError):
+ await orm.to_pydantic_async(include_content=True)
+
+
+async def test_list_files_content_none(server, default_user, default_source):
+ files = await server.file_manager.list_files(source_id=default_source.id, actor=default_user)
+ assert all(f.content is None for f in files)
+
+
+async def test_delete_cascades_to_content(server, default_user, default_source, async_session):
+ text_body = "to be deleted"
+ meta = PydanticFileMetadata(
+ file_name="delete_me.txt",
+ file_path="/tmp/delete_me.txt",
+ file_type="text/plain",
+ file_size=len(text_body),
+ source_id=default_source.id,
+ )
+ created = await server.file_manager.create_file(file_metadata=meta, actor=default_user, text=text_body)
+
+ # ensure row exists first
+ assert await _count_file_content_rows(async_session, created.id) == 1
+
+ # delete
+ await server.file_manager.delete_file(created.id, actor=default_user)
+
+ # content row gone
+ assert await _count_file_content_rows(async_session, created.id) == 0
+
+
+async def test_get_file_by_original_name_and_source_found(server: SyncServer, default_user, default_source):
+ """Test retrieving a file by original filename and source when it exists."""
+ original_filename = "test_original_file.txt"
+ file_metadata = PydanticFileMetadata(
+ file_name="some_generated_name.txt",
+ original_file_name=original_filename,
+ file_path="/path/to/test_file.txt",
+ file_type="text/plain",
+ file_size=1024,
+ source_id=default_source.id,
+ )
+ created_file = await server.file_manager.create_file(file_metadata=file_metadata, actor=default_user)
+
+ # Retrieve the file by original name and source
+ retrieved_file = await server.file_manager.get_file_by_original_name_and_source(
+ original_filename=original_filename, source_id=default_source.id, actor=default_user
+ )
+
+ # Assertions to verify the retrieved file matches the created one
+ assert retrieved_file is not None
+ assert retrieved_file.id == created_file.id
+ assert retrieved_file.original_file_name == original_filename
+ assert retrieved_file.source_id == default_source.id
+
+
+async def test_get_file_by_original_name_and_source_not_found(server: SyncServer, default_user, default_source):
+ """Test retrieving a file by original filename and source when it doesn't exist."""
+ non_existent_filename = "does_not_exist.txt"
+
+ # Try to retrieve a non-existent file
+ retrieved_file = await server.file_manager.get_file_by_original_name_and_source(
+ original_filename=non_existent_filename, source_id=default_source.id, actor=default_user
+ )
+
+ # Should return None for non-existent file
+ assert retrieved_file is None
+
+
+async def test_get_file_by_original_name_and_source_different_sources(server: SyncServer, default_user, default_source):
+ """Test that files with same original name in different sources are handled correctly."""
+ from letta.schemas.source import Source as PydanticSource
+
+ # Create a second source
+ second_source_pydantic = PydanticSource(
+ name="second_test_source",
+ description="This is a test source.",
+ metadata={"type": "test"},
+ embedding_config=DEFAULT_EMBEDDING_CONFIG,
+ )
+ second_source = await server.source_manager.create_source(source=second_source_pydantic, actor=default_user)
+
+ original_filename = "shared_filename.txt"
+
+ # Create file in first source
+ file_metadata_1 = PydanticFileMetadata(
+ file_name="file_in_source_1.txt",
+ original_file_name=original_filename,
+ file_path="/path/to/file1.txt",
+ file_type="text/plain",
+ file_size=1024,
+ source_id=default_source.id,
+ )
+ created_file_1 = await server.file_manager.create_file(file_metadata=file_metadata_1, actor=default_user)
+
+ # Create file with same original name in second source
+ file_metadata_2 = PydanticFileMetadata(
+ file_name="file_in_source_2.txt",
+ original_file_name=original_filename,
+ file_path="/path/to/file2.txt",
+ file_type="text/plain",
+ file_size=2048,
+ source_id=second_source.id,
+ )
+ created_file_2 = await server.file_manager.create_file(file_metadata=file_metadata_2, actor=default_user)
+
+ # Retrieve file from first source
+ retrieved_file_1 = await server.file_manager.get_file_by_original_name_and_source(
+ original_filename=original_filename, source_id=default_source.id, actor=default_user
+ )
+
+ # Retrieve file from second source
+ retrieved_file_2 = await server.file_manager.get_file_by_original_name_and_source(
+ original_filename=original_filename, source_id=second_source.id, actor=default_user
+ )
+
+ # Should retrieve different files
+ assert retrieved_file_1 is not None
+ assert retrieved_file_2 is not None
+ assert retrieved_file_1.id == created_file_1.id
+ assert retrieved_file_2.id == created_file_2.id
+ assert retrieved_file_1.id != retrieved_file_2.id
+ assert retrieved_file_1.source_id == default_source.id
+ assert retrieved_file_2.source_id == second_source.id
+
+
+async def test_get_file_by_original_name_and_source_ignores_deleted(server: SyncServer, default_user, default_source):
+ """Test that deleted files are ignored when searching by original name and source."""
+ original_filename = "to_be_deleted.txt"
+ file_metadata = PydanticFileMetadata(
+ file_name="deletable_file.txt",
+ original_file_name=original_filename,
+ file_path="/path/to/deletable.txt",
+ file_type="text/plain",
+ file_size=512,
+ source_id=default_source.id,
+ )
+ created_file = await server.file_manager.create_file(file_metadata=file_metadata, actor=default_user)
+
+ # Verify file can be found before deletion
+ retrieved_file = await server.file_manager.get_file_by_original_name_and_source(
+ original_filename=original_filename, source_id=default_source.id, actor=default_user
+ )
+ assert retrieved_file is not None
+ assert retrieved_file.id == created_file.id
+
+ # Delete the file
+ await server.file_manager.delete_file(created_file.id, actor=default_user)
+
+ # Try to retrieve the deleted file
+ retrieved_file_after_delete = await server.file_manager.get_file_by_original_name_and_source(
+ original_filename=original_filename, source_id=default_source.id, actor=default_user
+ )
+
+ # Should return None for deleted file
+ assert retrieved_file_after_delete is None
+
+
+async def test_list_files(server: SyncServer, default_user, default_source):
+ """Test listing files with pagination."""
+ # Create multiple files
+ await server.file_manager.create_file(
+ PydanticFileMetadata(file_name="File 1", file_path="/path/to/file1.txt", file_type="text/plain", source_id=default_source.id),
+ actor=default_user,
+ )
+ if USING_SQLITE:
+ time.sleep(CREATE_DELAY_SQLITE)
+ await server.file_manager.create_file(
+ PydanticFileMetadata(file_name="File 2", file_path="/path/to/file2.txt", file_type="text/plain", source_id=default_source.id),
+ actor=default_user,
+ )
+
+ # List files without pagination
+ files = await server.file_manager.list_files(source_id=default_source.id, actor=default_user)
+ assert len(files) == 2
+
+ # List files with pagination
+ paginated_files = await server.file_manager.list_files(source_id=default_source.id, actor=default_user, limit=1)
+ assert len(paginated_files) == 1
+
+ # Ensure cursor-based pagination works
+ next_page = await server.file_manager.list_files(source_id=default_source.id, actor=default_user, after=paginated_files[-1].id, limit=1)
+ assert len(next_page) == 1
+ assert next_page[0].file_name != paginated_files[0].file_name
+
+
+async def test_delete_file(server: SyncServer, default_user, default_source):
+ """Test deleting a file."""
+ file_metadata = PydanticFileMetadata(
+ file_name="Delete File", file_path="/path/to/delete_file.txt", file_type="text/plain", source_id=default_source.id
+ )
+ created_file = await server.file_manager.create_file(file_metadata=file_metadata, actor=default_user)
+
+ # Delete the file
+ deleted_file = await server.file_manager.delete_file(file_id=created_file.id, actor=default_user)
+
+ # Assertions to verify deletion
+ assert deleted_file.id == created_file.id
+
+ # Verify that the file no longer appears in list_files
+ files = await server.file_manager.list_files(source_id=default_source.id, actor=default_user)
+ assert len(files) == 0
+
+
+async def test_update_file_status_basic(server, default_user, default_source):
+ """Update processing status and error message for a file."""
+ meta = PydanticFileMetadata(
+ file_name="status_test.txt",
+ file_path="/tmp/status_test.txt",
+ file_type="text/plain",
+ file_size=100,
+ source_id=default_source.id,
+ )
+ created = await server.file_manager.create_file(file_metadata=meta, actor=default_user)
+
+ # Update status only
+ updated = await server.file_manager.update_file_status(
+ file_id=created.id,
+ actor=default_user,
+ processing_status=FileProcessingStatus.PARSING,
+ )
+ assert updated.processing_status == FileProcessingStatus.PARSING
+ assert updated.error_message is None
+
+ # Update both status and error message
+ updated = await server.file_manager.update_file_status(
+ file_id=created.id,
+ actor=default_user,
+ processing_status=FileProcessingStatus.ERROR,
+ error_message="Parse failed",
+ )
+ assert updated.processing_status == FileProcessingStatus.ERROR
+ assert updated.error_message == "Parse failed"
+
+
+async def test_update_file_status_error_only(server, default_user, default_source):
+ """Update just the error message, leave status unchanged."""
+ meta = PydanticFileMetadata(
+ file_name="error_only.txt",
+ file_path="/tmp/error_only.txt",
+ file_type="text/plain",
+ file_size=123,
+ source_id=default_source.id,
+ )
+ created = await server.file_manager.create_file(file_metadata=meta, actor=default_user)
+
+ updated = await server.file_manager.update_file_status(
+ file_id=created.id,
+ actor=default_user,
+ error_message="Timeout while embedding",
+ )
+ assert updated.error_message == "Timeout while embedding"
+ assert updated.processing_status == FileProcessingStatus.PENDING # default from creation
+
+
+async def test_update_file_status_with_chunks(server, default_user, default_source):
+ """Update chunk progress fields along with status."""
+ meta = PydanticFileMetadata(
+ file_name="chunks_test.txt",
+ file_path="/tmp/chunks_test.txt",
+ file_type="text/plain",
+ file_size=500,
+ source_id=default_source.id,
+ )
+ created = await server.file_manager.create_file(file_metadata=meta, actor=default_user)
+
+ # First transition: PENDING -> PARSING
+ updated = await server.file_manager.update_file_status(
+ file_id=created.id,
+ actor=default_user,
+ processing_status=FileProcessingStatus.PARSING,
+ )
+ assert updated.processing_status == FileProcessingStatus.PARSING
+
+ # Next transition: PARSING -> EMBEDDING with chunk progress
+ updated = await server.file_manager.update_file_status(
+ file_id=created.id,
+ actor=default_user,
+ processing_status=FileProcessingStatus.EMBEDDING,
+ total_chunks=100,
+ chunks_embedded=50,
+ )
+ assert updated.processing_status == FileProcessingStatus.EMBEDDING
+ assert updated.total_chunks == 100
+ assert updated.chunks_embedded == 50
+
+ # Update only chunk progress
+ updated = await server.file_manager.update_file_status(
+ file_id=created.id,
+ actor=default_user,
+ chunks_embedded=100,
+ )
+ assert updated.chunks_embedded == 100
+ assert updated.total_chunks == 100 # unchanged
+ assert updated.processing_status == FileProcessingStatus.EMBEDDING # unchanged
+
+
+async def test_file_status_valid_transitions(server, default_user, default_source):
+ """Test valid state transitions follow the expected flow."""
+ meta = PydanticFileMetadata(
+ file_name="valid_transitions.txt",
+ file_path="/tmp/valid_transitions.txt",
+ file_type="text/plain",
+ file_size=100,
+ source_id=default_source.id,
+ )
+ created = await server.file_manager.create_file(file_metadata=meta, actor=default_user)
+ assert created.processing_status == FileProcessingStatus.PENDING
+
+ # PENDING -> PARSING
+ updated = await server.file_manager.update_file_status(
+ file_id=created.id,
+ actor=default_user,
+ processing_status=FileProcessingStatus.PARSING,
+ )
+ assert updated.processing_status == FileProcessingStatus.PARSING
+
+ # PARSING -> EMBEDDING
+ updated = await server.file_manager.update_file_status(
+ file_id=created.id,
+ actor=default_user,
+ processing_status=FileProcessingStatus.EMBEDDING,
+ )
+ assert updated.processing_status == FileProcessingStatus.EMBEDDING
+
+ # EMBEDDING -> COMPLETED
+ updated = await server.file_manager.update_file_status(
+ file_id=created.id,
+ actor=default_user,
+ processing_status=FileProcessingStatus.COMPLETED,
+ )
+ assert updated.processing_status == FileProcessingStatus.COMPLETED
+
+
+async def test_file_status_invalid_transitions(server, default_user, default_source):
+ """Test that invalid state transitions are blocked."""
+ # Test PENDING -> COMPLETED (skipping PARSING and EMBEDDING)
+ meta = PydanticFileMetadata(
+ file_name="invalid_pending_to_completed.txt",
+ file_path="/tmp/invalid1.txt",
+ file_type="text/plain",
+ file_size=100,
+ source_id=default_source.id,
+ )
+ created = await server.file_manager.create_file(file_metadata=meta, actor=default_user)
+
+ with pytest.raises(ValueError, match="Invalid state transition.*pending.*COMPLETED"):
+ await server.file_manager.update_file_status(
+ file_id=created.id,
+ actor=default_user,
+ processing_status=FileProcessingStatus.COMPLETED,
+ )
+
+ # Test PARSING -> COMPLETED (skipping EMBEDDING)
+ meta2 = PydanticFileMetadata(
+ file_name="invalid_parsing_to_completed.txt",
+ file_path="/tmp/invalid2.txt",
+ file_type="text/plain",
+ file_size=100,
+ source_id=default_source.id,
+ )
+ created2 = await server.file_manager.create_file(file_metadata=meta2, actor=default_user)
+ await server.file_manager.update_file_status(
+ file_id=created2.id,
+ actor=default_user,
+ processing_status=FileProcessingStatus.PARSING,
+ )
+
+ with pytest.raises(ValueError, match="Invalid state transition.*parsing.*COMPLETED"):
+ await server.file_manager.update_file_status(
+ file_id=created2.id,
+ actor=default_user,
+ processing_status=FileProcessingStatus.COMPLETED,
+ )
+
+ # Test PENDING -> EMBEDDING (skipping PARSING)
+ meta3 = PydanticFileMetadata(
+ file_name="invalid_pending_to_embedding.txt",
+ file_path="/tmp/invalid3.txt",
+ file_type="text/plain",
+ file_size=100,
+ source_id=default_source.id,
+ )
+ created3 = await server.file_manager.create_file(file_metadata=meta3, actor=default_user)
+
+ with pytest.raises(ValueError, match="Invalid state transition.*pending.*EMBEDDING"):
+ await server.file_manager.update_file_status(
+ file_id=created3.id,
+ actor=default_user,
+ processing_status=FileProcessingStatus.EMBEDDING,
+ )
+
+
+async def test_file_status_terminal_states(server, default_user, default_source):
+ """Test that terminal states (COMPLETED and ERROR) cannot be updated."""
+ # Test COMPLETED is terminal
+ meta = PydanticFileMetadata(
+ file_name="completed_terminal.txt",
+ file_path="/tmp/completed_terminal.txt",
+ file_type="text/plain",
+ file_size=100,
+ source_id=default_source.id,
+ )
+ created = await server.file_manager.create_file(file_metadata=meta, actor=default_user)
+
+ # Move through valid transitions to COMPLETED
+ await server.file_manager.update_file_status(file_id=created.id, actor=default_user, processing_status=FileProcessingStatus.PARSING)
+ await server.file_manager.update_file_status(file_id=created.id, actor=default_user, processing_status=FileProcessingStatus.EMBEDDING)
+ await server.file_manager.update_file_status(file_id=created.id, actor=default_user, processing_status=FileProcessingStatus.COMPLETED)
+
+ # Cannot transition from COMPLETED to any state
+ with pytest.raises(ValueError, match="Cannot update.*terminal state completed"):
+ await server.file_manager.update_file_status(
+ file_id=created.id,
+ actor=default_user,
+ processing_status=FileProcessingStatus.EMBEDDING,
+ )
+
+ with pytest.raises(ValueError, match="Cannot update.*terminal state completed"):
+ await server.file_manager.update_file_status(
+ file_id=created.id,
+ actor=default_user,
+ processing_status=FileProcessingStatus.ERROR,
+ error_message="Should not work",
+ )
+
+ # Test ERROR is terminal
+ meta2 = PydanticFileMetadata(
+ file_name="error_terminal.txt",
+ file_path="/tmp/error_terminal.txt",
+ file_type="text/plain",
+ file_size=100,
+ source_id=default_source.id,
+ )
+ created2 = await server.file_manager.create_file(file_metadata=meta2, actor=default_user)
+
+ await server.file_manager.update_file_status(
+ file_id=created2.id,
+ actor=default_user,
+ processing_status=FileProcessingStatus.ERROR,
+ error_message="Test error",
+ )
+
+ # Cannot transition from ERROR to any state
+ with pytest.raises(ValueError, match="Cannot update.*terminal state error"):
+ await server.file_manager.update_file_status(
+ file_id=created2.id,
+ actor=default_user,
+ processing_status=FileProcessingStatus.PARSING,
+ )
+
+
+async def test_file_status_error_transitions(server, default_user, default_source):
+ """Test that any non-terminal state can transition to ERROR."""
+ # PENDING -> ERROR
+ meta1 = PydanticFileMetadata(
+ file_name="pending_to_error.txt",
+ file_path="/tmp/pending_error.txt",
+ file_type="text/plain",
+ file_size=100,
+ source_id=default_source.id,
+ )
+ created1 = await server.file_manager.create_file(file_metadata=meta1, actor=default_user)
+
+ updated1 = await server.file_manager.update_file_status(
+ file_id=created1.id,
+ actor=default_user,
+ processing_status=FileProcessingStatus.ERROR,
+ error_message="Failed at PENDING",
+ )
+ assert updated1.processing_status == FileProcessingStatus.ERROR
+ assert updated1.error_message == "Failed at PENDING"
+
+ # PARSING -> ERROR
+ meta2 = PydanticFileMetadata(
+ file_name="parsing_to_error.txt",
+ file_path="/tmp/parsing_error.txt",
+ file_type="text/plain",
+ file_size=100,
+ source_id=default_source.id,
+ )
+ created2 = await server.file_manager.create_file(file_metadata=meta2, actor=default_user)
+ await server.file_manager.update_file_status(
+ file_id=created2.id,
+ actor=default_user,
+ processing_status=FileProcessingStatus.PARSING,
+ )
+
+ updated2 = await server.file_manager.update_file_status(
+ file_id=created2.id,
+ actor=default_user,
+ processing_status=FileProcessingStatus.ERROR,
+ error_message="Failed at PARSING",
+ )
+ assert updated2.processing_status == FileProcessingStatus.ERROR
+ assert updated2.error_message == "Failed at PARSING"
+
+ # EMBEDDING -> ERROR
+ meta3 = PydanticFileMetadata(
+ file_name="embedding_to_error.txt",
+ file_path="/tmp/embedding_error.txt",
+ file_type="text/plain",
+ file_size=100,
+ source_id=default_source.id,
+ )
+ created3 = await server.file_manager.create_file(file_metadata=meta3, actor=default_user)
+ await server.file_manager.update_file_status(file_id=created3.id, actor=default_user, processing_status=FileProcessingStatus.PARSING)
+ await server.file_manager.update_file_status(file_id=created3.id, actor=default_user, processing_status=FileProcessingStatus.EMBEDDING)
+
+ updated3 = await server.file_manager.update_file_status(
+ file_id=created3.id,
+ actor=default_user,
+ processing_status=FileProcessingStatus.ERROR,
+ error_message="Failed at EMBEDDING",
+ )
+ assert updated3.processing_status == FileProcessingStatus.ERROR
+ assert updated3.error_message == "Failed at EMBEDDING"
+
+
+async def test_file_status_terminal_state_non_status_updates(server, default_user, default_source):
+ """Test that terminal states block ALL updates, not just status changes."""
+ # Create file and move to COMPLETED
+ meta = PydanticFileMetadata(
+ file_name="terminal_blocks_all.txt",
+ file_path="/tmp/terminal_all.txt",
+ file_type="text/plain",
+ file_size=100,
+ source_id=default_source.id,
+ )
+ created = await server.file_manager.create_file(file_metadata=meta, actor=default_user)
+
+ await server.file_manager.update_file_status(file_id=created.id, actor=default_user, processing_status=FileProcessingStatus.PARSING)
+ await server.file_manager.update_file_status(file_id=created.id, actor=default_user, processing_status=FileProcessingStatus.EMBEDDING)
+ await server.file_manager.update_file_status(file_id=created.id, actor=default_user, processing_status=FileProcessingStatus.COMPLETED)
+
+ # Cannot update chunks_embedded in COMPLETED state
+ with pytest.raises(ValueError, match="Cannot update.*terminal state completed"):
+ await server.file_manager.update_file_status(
+ file_id=created.id,
+ actor=default_user,
+ chunks_embedded=50,
+ )
+
+ # Cannot update total_chunks in COMPLETED state
+ with pytest.raises(ValueError, match="Cannot update.*terminal state completed"):
+ await server.file_manager.update_file_status(
+ file_id=created.id,
+ actor=default_user,
+ total_chunks=100,
+ )
+
+ # Cannot update error_message in COMPLETED state
+ with pytest.raises(ValueError, match="Cannot update.*terminal state completed"):
+ await server.file_manager.update_file_status(
+ file_id=created.id,
+ actor=default_user,
+ error_message="This should fail",
+ )
+
+ # Test same for ERROR state
+ meta2 = PydanticFileMetadata(
+ file_name="error_blocks_all.txt",
+ file_path="/tmp/error_all.txt",
+ file_type="text/plain",
+ file_size=100,
+ source_id=default_source.id,
+ )
+ created2 = await server.file_manager.create_file(file_metadata=meta2, actor=default_user)
+ await server.file_manager.update_file_status(
+ file_id=created2.id,
+ actor=default_user,
+ processing_status=FileProcessingStatus.ERROR,
+ error_message="Initial error",
+ )
+
+ # Cannot update chunks_embedded in ERROR state
+ with pytest.raises(ValueError, match="Cannot update.*terminal state error"):
+ await server.file_manager.update_file_status(
+ file_id=created2.id,
+ actor=default_user,
+ chunks_embedded=25,
+ )
+
+
+async def test_file_status_race_condition_prevention(server, default_user, default_source):
+ """Test that race conditions are prevented when multiple updates happen."""
+ meta = PydanticFileMetadata(
+ file_name="race_condition_test.txt",
+ file_path="/tmp/race_test.txt",
+ file_type="text/plain",
+ file_size=100,
+ source_id=default_source.id,
+ )
+ created = await server.file_manager.create_file(file_metadata=meta, actor=default_user)
+
+ # Move to PARSING
+ await server.file_manager.update_file_status(
+ file_id=created.id,
+ actor=default_user,
+ processing_status=FileProcessingStatus.PARSING,
+ )
+
+ # Simulate race condition: Try to update from PARSING to PARSING again (stale read)
+ # This should now be allowed (same-state transition) to prevent race conditions
+ updated_again = await server.file_manager.update_file_status(
+ file_id=created.id,
+ actor=default_user,
+ processing_status=FileProcessingStatus.PARSING,
+ )
+ assert updated_again.processing_status == FileProcessingStatus.PARSING
+
+ # Move to ERROR
+ await server.file_manager.update_file_status(
+ file_id=created.id,
+ actor=default_user,
+ processing_status=FileProcessingStatus.ERROR,
+ error_message="Simulated error",
+ )
+
+ # Try to continue with EMBEDDING as if error didn't happen (race condition)
+ # This should fail because file is in ERROR state
+ with pytest.raises(ValueError, match="Cannot update.*terminal state error"):
+ await server.file_manager.update_file_status(
+ file_id=created.id,
+ actor=default_user,
+ processing_status=FileProcessingStatus.EMBEDDING,
+ )
+
+
+async def test_file_status_backwards_transitions(server, default_user, default_source):
+ """Test that backwards transitions are not allowed."""
+ meta = PydanticFileMetadata(
+ file_name="backwards_transitions.txt",
+ file_path="/tmp/backwards.txt",
+ file_type="text/plain",
+ file_size=100,
+ source_id=default_source.id,
+ )
+ created = await server.file_manager.create_file(file_metadata=meta, actor=default_user)
+
+ # Move to EMBEDDING
+ await server.file_manager.update_file_status(file_id=created.id, actor=default_user, processing_status=FileProcessingStatus.PARSING)
+ await server.file_manager.update_file_status(file_id=created.id, actor=default_user, processing_status=FileProcessingStatus.EMBEDDING)
+
+ # Cannot go back to PARSING
+ with pytest.raises(ValueError, match="Invalid state transition.*embedding.*PARSING"):
+ await server.file_manager.update_file_status(
+ file_id=created.id,
+ actor=default_user,
+ processing_status=FileProcessingStatus.PARSING,
+ )
+
+ # Cannot go back to PENDING
+ with pytest.raises(ValueError, match="Cannot transition to PENDING state.*PENDING is only valid as initial state"):
+ await server.file_manager.update_file_status(
+ file_id=created.id,
+ actor=default_user,
+ processing_status=FileProcessingStatus.PENDING,
+ )
+
+
+async def test_file_status_update_with_chunks_progress(server, default_user, default_source):
+ """Test updating chunk progress during EMBEDDING state."""
+ meta = PydanticFileMetadata(
+ file_name="chunk_progress.txt",
+ file_path="/tmp/chunks.txt",
+ file_type="text/plain",
+ file_size=1000,
+ source_id=default_source.id,
+ )
+ created = await server.file_manager.create_file(file_metadata=meta, actor=default_user)
+
+ # Move to EMBEDDING with initial chunk info
+ await server.file_manager.update_file_status(file_id=created.id, actor=default_user, processing_status=FileProcessingStatus.PARSING)
+ updated = await server.file_manager.update_file_status(
+ file_id=created.id,
+ actor=default_user,
+ processing_status=FileProcessingStatus.EMBEDDING,
+ total_chunks=100,
+ chunks_embedded=0,
+ )
+ assert updated.total_chunks == 100
+ assert updated.chunks_embedded == 0
+
+ # Update chunk progress without changing status
+ updated = await server.file_manager.update_file_status(
+ file_id=created.id,
+ actor=default_user,
+ chunks_embedded=50,
+ )
+ assert updated.chunks_embedded == 50
+ assert updated.processing_status == FileProcessingStatus.EMBEDDING
+
+ # Update to completion
+ updated = await server.file_manager.update_file_status(
+ file_id=created.id,
+ actor=default_user,
+ chunks_embedded=100,
+ )
+ assert updated.chunks_embedded == 100
+
+ # Move to COMPLETED
+ updated = await server.file_manager.update_file_status(
+ file_id=created.id,
+ actor=default_user,
+ processing_status=FileProcessingStatus.COMPLETED,
+ )
+ assert updated.processing_status == FileProcessingStatus.COMPLETED
+ assert updated.chunks_embedded == 100 # preserved
+
+
+async def test_same_state_transitions_allowed(server, default_user, default_source):
+ """Test that same-state transitions are allowed to prevent race conditions."""
+ # Create file
+ created = await server.file_manager.create_file(
+ FileMetadata(
+ file_name="same_state_test.txt",
+ source_id=default_source.id,
+ processing_status=FileProcessingStatus.PENDING,
+ ),
+ default_user,
+ )
+
+ # Test PARSING -> PARSING
+ await server.file_manager.update_file_status(file_id=created.id, actor=default_user, processing_status=FileProcessingStatus.PARSING)
+ updated = await server.file_manager.update_file_status(
+ file_id=created.id, actor=default_user, processing_status=FileProcessingStatus.PARSING
+ )
+ assert updated.processing_status == FileProcessingStatus.PARSING
+
+ # Test EMBEDDING -> EMBEDDING
+ await server.file_manager.update_file_status(file_id=created.id, actor=default_user, processing_status=FileProcessingStatus.EMBEDDING)
+ updated = await server.file_manager.update_file_status(
+ file_id=created.id, actor=default_user, processing_status=FileProcessingStatus.EMBEDDING, chunks_embedded=5
+ )
+ assert updated.processing_status == FileProcessingStatus.EMBEDDING
+ assert updated.chunks_embedded == 5
+
+ # Test COMPLETED -> COMPLETED
+ await server.file_manager.update_file_status(file_id=created.id, actor=default_user, processing_status=FileProcessingStatus.COMPLETED)
+ updated = await server.file_manager.update_file_status(
+ file_id=created.id, actor=default_user, processing_status=FileProcessingStatus.COMPLETED, total_chunks=10
+ )
+ assert updated.processing_status == FileProcessingStatus.COMPLETED
+ assert updated.total_chunks == 10
+
+
+async def test_upsert_file_content_basic(server: SyncServer, default_user, default_source, async_session):
+ """Test creating and updating file content with upsert_file_content()."""
+ initial_text = "Initial content"
+ updated_text = "Updated content"
+
+ # Step 1: Create file with no content
+ meta = PydanticFileMetadata(
+ file_name="upsert_body.txt",
+ file_path="/tmp/upsert_body.txt",
+ file_type="text/plain",
+ file_size=len(initial_text),
+ source_id=default_source.id,
+ )
+ created = await server.file_manager.create_file(file_metadata=meta, actor=default_user)
+ assert created.content is None
+
+ # Step 2: Insert new content
+ file_with_content = await server.file_manager.upsert_file_content(
+ file_id=created.id,
+ text=initial_text,
+ actor=default_user,
+ )
+ assert file_with_content.content == initial_text
+
+ # Verify body row exists
+ count = await _count_file_content_rows(async_session, created.id)
+ assert count == 1
+
+ # Step 3: Update existing content
+ file_with_updated_content = await server.file_manager.upsert_file_content(
+ file_id=created.id,
+ text=updated_text,
+ actor=default_user,
+ )
+ assert file_with_updated_content.content == updated_text
+
+ # Ensure still only 1 row in content table
+ count = await _count_file_content_rows(async_session, created.id)
+ assert count == 1
+
+ # Ensure `updated_at` is bumped
+ orm_file = await async_session.get(FileMetadataModel, created.id)
+ assert orm_file.updated_at >= orm_file.created_at
+
+
+async def test_get_organization_sources_metadata(server, default_user):
+ """Test getting organization sources metadata with aggregated file information."""
+ # Create test sources
+ source1 = await server.source_manager.create_source(
+ source=PydanticSource(
+ name="test_source_1",
+ embedding_config=DEFAULT_EMBEDDING_CONFIG,
+ ),
+ actor=default_user,
+ )
+
+ source2 = await server.source_manager.create_source(
+ source=PydanticSource(
+ name="test_source_2",
+ embedding_config=DEFAULT_EMBEDDING_CONFIG,
+ ),
+ actor=default_user,
+ )
+
+ # Create test files for source1
+ file1_meta = PydanticFileMetadata(
+ source_id=source1.id,
+ file_name="file1.txt",
+ file_type="text/plain",
+ file_size=1024,
+ )
+ file1 = await server.file_manager.create_file(file_metadata=file1_meta, actor=default_user)
+
+ file2_meta = PydanticFileMetadata(
+ source_id=source1.id,
+ file_name="file2.txt",
+ file_type="text/plain",
+ file_size=2048,
+ )
+ file2 = await server.file_manager.create_file(file_metadata=file2_meta, actor=default_user)
+
+ # Create test file for source2
+ file3_meta = PydanticFileMetadata(
+ source_id=source2.id,
+ file_name="file3.txt",
+ file_type="text/plain",
+ file_size=512,
+ )
+ file3 = await server.file_manager.create_file(file_metadata=file3_meta, actor=default_user)
+
+ # Test 1: Get organization metadata without detailed per-source metadata (default behavior)
+ metadata_summary = await server.file_manager.get_organization_sources_metadata(
+ actor=default_user, include_detailed_per_source_metadata=False
+ )
+
+ # Verify top-level aggregations are present
+ assert metadata_summary.total_sources >= 2 # May have other sources from other tests
+ assert metadata_summary.total_files >= 3
+ assert metadata_summary.total_size >= 3584
+
+ # Verify sources list is empty when include_detailed_per_source_metadata=False
+ assert len(metadata_summary.sources) == 0
+
+ # Test 2: Get organization metadata with detailed per-source metadata
+ metadata_detailed = await server.file_manager.get_organization_sources_metadata(
+ actor=default_user, include_detailed_per_source_metadata=True
+ )
+
+ # Verify top-level aggregations are the same
+ assert metadata_detailed.total_sources == metadata_summary.total_sources
+ assert metadata_detailed.total_files == metadata_summary.total_files
+ assert metadata_detailed.total_size == metadata_summary.total_size
+
+ # Find our test sources in the detailed results
+ source1_meta = next((s for s in metadata_detailed.sources if s.source_id == source1.id), None)
+ source2_meta = next((s for s in metadata_detailed.sources if s.source_id == source2.id), None)
+
+ assert source1_meta is not None
+ assert source1_meta.source_name == "test_source_1"
+ assert source1_meta.file_count == 2
+ assert source1_meta.total_size == 3072 # 1024 + 2048
+ assert len(source1_meta.files) == 2
+
+ # Verify file details in source1
+ file1_stats = next((f for f in source1_meta.files if f.file_id == file1.id), None)
+ file2_stats = next((f for f in source1_meta.files if f.file_id == file2.id), None)
+
+ assert file1_stats is not None
+ assert file1_stats.file_name == "file1.txt"
+ assert file1_stats.file_size == 1024
+
+ assert file2_stats is not None
+ assert file2_stats.file_name == "file2.txt"
+ assert file2_stats.file_size == 2048
+
+ assert source2_meta is not None
+ assert source2_meta.source_name == "test_source_2"
+ assert source2_meta.file_count == 1
+ assert source2_meta.total_size == 512
+ assert len(source2_meta.files) == 1
+
+ # Verify file details in source2
+ file3_stats = source2_meta.files[0]
+ assert file3_stats.file_id == file3.id
+ assert file3_stats.file_name == "file3.txt"
+ assert file3_stats.file_size == 512
diff --git a/tests/managers/test_tool_manager.py b/tests/managers/test_tool_manager.py
new file mode 100644
index 000000000..bf3e21259
--- /dev/null
+++ b/tests/managers/test_tool_manager.py
@@ -0,0 +1,1861 @@
+import json
+import logging
+import os
+import random
+import re
+import string
+import time
+import uuid
+from datetime import datetime, timedelta, timezone
+from typing import List
+from unittest.mock import AsyncMock, Mock, patch
+
+import pytest
+from _pytest.python_api import approx
+from anthropic.types.beta import BetaMessage
+from anthropic.types.beta.messages import BetaMessageBatchIndividualResponse, BetaMessageBatchSucceededResult
+
+# Import shared fixtures and constants from conftest
+from conftest import (
+ CREATE_DELAY_SQLITE,
+ DEFAULT_EMBEDDING_CONFIG,
+ USING_SQLITE,
+)
+from openai.types.chat.chat_completion_message_tool_call import ChatCompletionMessageToolCall as OpenAIToolCall, Function as OpenAIFunction
+from sqlalchemy import func, select
+from sqlalchemy.exc import IntegrityError, InvalidRequestError
+from sqlalchemy.orm.exc import StaleDataError
+
+from letta.config import LettaConfig
+from letta.constants import (
+ BASE_MEMORY_TOOLS,
+ BASE_SLEEPTIME_TOOLS,
+ BASE_TOOLS,
+ BASE_VOICE_SLEEPTIME_CHAT_TOOLS,
+ BASE_VOICE_SLEEPTIME_TOOLS,
+ BUILTIN_TOOLS,
+ DEFAULT_ORG_ID,
+ DEFAULT_ORG_NAME,
+ FILES_TOOLS,
+ LETTA_TOOL_EXECUTION_DIR,
+ LETTA_TOOL_SET,
+ LOCAL_ONLY_MULTI_AGENT_TOOLS,
+ MCP_TOOL_TAG_NAME_PREFIX,
+ MULTI_AGENT_TOOLS,
+)
+from letta.data_sources.redis_client import NoopAsyncRedisClient, get_redis_client
+from letta.errors import LettaAgentNotFoundError
+from letta.functions.functions import derive_openai_json_schema, parse_source_code
+from letta.functions.mcp_client.types import MCPTool
+from letta.helpers import ToolRulesSolver
+from letta.helpers.datetime_helpers import AsyncTimer
+from letta.jobs.types import ItemUpdateInfo, RequestStatusUpdateInfo, StepStatusUpdateInfo
+from letta.orm import Base, Block
+from letta.orm.block_history import BlockHistory
+from letta.orm.errors import NoResultFound, UniqueConstraintViolationError
+from letta.orm.file import FileContent as FileContentModel, FileMetadata as FileMetadataModel
+from letta.schemas.agent import CreateAgent, UpdateAgent
+from letta.schemas.block import Block as PydanticBlock, BlockUpdate, CreateBlock
+from letta.schemas.embedding_config import EmbeddingConfig
+from letta.schemas.enums import (
+ ActorType,
+ AgentStepStatus,
+ FileProcessingStatus,
+ JobStatus,
+ JobType,
+ MessageRole,
+ ProviderType,
+ SandboxType,
+ StepStatus,
+ TagMatchMode,
+ ToolType,
+ VectorDBProvider,
+)
+from letta.schemas.environment_variables import SandboxEnvironmentVariableCreate, SandboxEnvironmentVariableUpdate
+from letta.schemas.file import FileMetadata, FileMetadata as PydanticFileMetadata
+from letta.schemas.identity import IdentityCreate, IdentityProperty, IdentityPropertyType, IdentityType, IdentityUpdate, IdentityUpsert
+from letta.schemas.job import BatchJob, Job, Job as PydanticJob, JobUpdate, LettaRequestConfig
+from letta.schemas.letta_message import UpdateAssistantMessage, UpdateReasoningMessage, UpdateSystemMessage, UpdateUserMessage
+from letta.schemas.letta_message_content import TextContent
+from letta.schemas.letta_stop_reason import LettaStopReason, StopReasonType
+from letta.schemas.llm_batch_job import AgentStepState, LLMBatchItem
+from letta.schemas.llm_config import LLMConfig
+from letta.schemas.message import Message as PydanticMessage, MessageCreate, MessageUpdate
+from letta.schemas.openai.chat_completion_response import UsageStatistics
+from letta.schemas.organization import Organization, Organization as PydanticOrganization, OrganizationUpdate
+from letta.schemas.passage import Passage as PydanticPassage
+from letta.schemas.pip_requirement import PipRequirement
+from letta.schemas.run import Run as PydanticRun
+from letta.schemas.sandbox_config import E2BSandboxConfig, LocalSandboxConfig, SandboxConfigCreate, SandboxConfigUpdate
+from letta.schemas.source import Source as PydanticSource, SourceUpdate
+from letta.schemas.tool import Tool as PydanticTool, ToolCreate, ToolUpdate
+from letta.schemas.tool_rule import InitToolRule
+from letta.schemas.user import User as PydanticUser, UserUpdate
+from letta.server.db import db_registry
+from letta.server.server import SyncServer
+from letta.services.block_manager import BlockManager
+from letta.services.helpers.agent_manager_helper import calculate_base_tools, calculate_multi_agent_tools, validate_agent_exists_async
+from letta.services.step_manager import FeedbackType
+from letta.settings import settings, tool_settings
+from letta.utils import calculate_file_defaults_based_on_context_window
+from tests.helpers.utils import comprehensive_agent_checks, validate_context_window_overview
+from tests.utils import random_string
+
+# ======================================================================================================================
+# AgentManager Tests - Tools Relationship
+# ======================================================================================================================
+
+
+async def test_attach_tool(server: SyncServer, sarah_agent, print_tool, default_user):
+ """Test attaching a tool to an agent."""
+ # Attach the tool
+ await server.agent_manager.attach_tool_async(agent_id=sarah_agent.id, tool_id=print_tool.id, actor=default_user)
+
+ # Verify attachment through get_agent_by_id
+ agent = await server.agent_manager.get_agent_by_id_async(agent_id=sarah_agent.id, actor=default_user)
+ assert print_tool.id in [t.id for t in agent.tools]
+
+ # Verify that attaching the same tool again doesn't cause duplication
+ await server.agent_manager.attach_tool_async(agent_id=sarah_agent.id, tool_id=print_tool.id, actor=default_user)
+ agent = await server.agent_manager.get_agent_by_id_async(agent_id=sarah_agent.id, actor=default_user)
+ assert len([t for t in agent.tools if t.id == print_tool.id]) == 1
+
+
+async def test_detach_tool(server: SyncServer, sarah_agent, print_tool, default_user):
+ """Test detaching a tool from an agent."""
+ # Attach the tool first
+ await server.agent_manager.attach_tool_async(agent_id=sarah_agent.id, tool_id=print_tool.id, actor=default_user)
+
+ # Verify it's attached
+ agent = await server.agent_manager.get_agent_by_id_async(agent_id=sarah_agent.id, actor=default_user)
+ assert print_tool.id in [t.id for t in agent.tools]
+
+ # Detach the tool
+ await server.agent_manager.detach_tool_async(agent_id=sarah_agent.id, tool_id=print_tool.id, actor=default_user)
+
+ # Verify it's detached
+ agent = await server.agent_manager.get_agent_by_id_async(agent_id=sarah_agent.id, actor=default_user)
+ assert print_tool.id not in [t.id for t in agent.tools]
+
+ # Verify that detaching an already detached tool doesn't cause issues
+ await server.agent_manager.detach_tool_async(agent_id=sarah_agent.id, tool_id=print_tool.id, actor=default_user)
+
+
+async def test_bulk_detach_tools(server: SyncServer, sarah_agent, print_tool, other_tool, default_user):
+ """Test bulk detaching multiple tools from an agent."""
+ # First attach both tools
+ tool_ids = [print_tool.id, other_tool.id]
+ await server.agent_manager.bulk_attach_tools_async(agent_id=sarah_agent.id, tool_ids=tool_ids, actor=default_user)
+
+ # Verify both tools are attached
+ agent = await server.agent_manager.get_agent_by_id_async(agent_id=sarah_agent.id, actor=default_user)
+ assert print_tool.id in [t.id for t in agent.tools]
+ assert other_tool.id in [t.id for t in agent.tools]
+
+ # Bulk detach both tools
+ await server.agent_manager.bulk_detach_tools_async(agent_id=sarah_agent.id, tool_ids=tool_ids, actor=default_user)
+
+ # Verify both tools are detached
+ agent = await server.agent_manager.get_agent_by_id_async(agent_id=sarah_agent.id, actor=default_user)
+ assert print_tool.id not in [t.id for t in agent.tools]
+ assert other_tool.id not in [t.id for t in agent.tools]
+
+
+async def test_bulk_detach_tools_partial(server: SyncServer, sarah_agent, print_tool, other_tool, default_user):
+ """Test bulk detaching tools when some are not attached."""
+ # Only attach one tool
+ await server.agent_manager.attach_tool_async(agent_id=sarah_agent.id, tool_id=print_tool.id, actor=default_user)
+
+ # Try to bulk detach both tools (one attached, one not)
+ tool_ids = [print_tool.id, other_tool.id]
+ await server.agent_manager.bulk_detach_tools_async(agent_id=sarah_agent.id, tool_ids=tool_ids, actor=default_user)
+
+ # Verify the attached tool was detached
+ agent = await server.agent_manager.get_agent_by_id_async(agent_id=sarah_agent.id, actor=default_user)
+ assert print_tool.id not in [t.id for t in agent.tools]
+ assert other_tool.id not in [t.id for t in agent.tools]
+
+
+async def test_bulk_detach_tools_empty_list(server: SyncServer, sarah_agent, print_tool, default_user):
+ """Test bulk detaching empty list of tools."""
+ # Attach a tool first
+ await server.agent_manager.attach_tool_async(agent_id=sarah_agent.id, tool_id=print_tool.id, actor=default_user)
+
+ # Bulk detach empty list
+ await server.agent_manager.bulk_detach_tools_async(agent_id=sarah_agent.id, tool_ids=[], actor=default_user)
+
+ # Verify the tool is still attached
+ agent = await server.agent_manager.get_agent_by_id_async(agent_id=sarah_agent.id, actor=default_user)
+ assert print_tool.id in [t.id for t in agent.tools]
+
+
+async def test_bulk_detach_tools_idempotent(server: SyncServer, sarah_agent, print_tool, other_tool, default_user):
+ """Test that bulk detach is idempotent."""
+ # Attach both tools
+ tool_ids = [print_tool.id, other_tool.id]
+ await server.agent_manager.bulk_attach_tools_async(agent_id=sarah_agent.id, tool_ids=tool_ids, actor=default_user)
+
+ # Bulk detach once
+ await server.agent_manager.bulk_detach_tools_async(agent_id=sarah_agent.id, tool_ids=tool_ids, actor=default_user)
+
+ # Verify tools are detached
+ agent = await server.agent_manager.get_agent_by_id_async(agent_id=sarah_agent.id, actor=default_user)
+ assert len(agent.tools) == 0
+
+ # Bulk detach again (should be no-op, no errors)
+ await server.agent_manager.bulk_detach_tools_async(agent_id=sarah_agent.id, tool_ids=tool_ids, actor=default_user)
+
+ # Verify still no tools
+ agent = await server.agent_manager.get_agent_by_id_async(agent_id=sarah_agent.id, actor=default_user)
+ assert len(agent.tools) == 0
+
+
+async def test_bulk_detach_tools_nonexistent_agent(server: SyncServer, print_tool, other_tool, default_user):
+ """Test bulk detaching tools from a nonexistent agent."""
+ nonexistent_agent_id = "nonexistent-agent-id"
+ tool_ids = [print_tool.id, other_tool.id]
+
+ with pytest.raises(LettaAgentNotFoundError):
+ await server.agent_manager.bulk_detach_tools_async(agent_id=nonexistent_agent_id, tool_ids=tool_ids, actor=default_user)
+
+
+async def test_attach_tool_nonexistent_agent(server: SyncServer, print_tool, default_user):
+ """Test attaching a tool to a nonexistent agent."""
+ with pytest.raises(LettaAgentNotFoundError):
+ await server.agent_manager.attach_tool_async(agent_id="nonexistent-agent-id", tool_id=print_tool.id, actor=default_user)
+
+
+async def test_attach_tool_nonexistent_tool(server: SyncServer, sarah_agent, default_user):
+ """Test attaching a nonexistent tool to an agent."""
+ with pytest.raises(NoResultFound):
+ await server.agent_manager.attach_tool_async(agent_id=sarah_agent.id, tool_id="nonexistent-tool-id", actor=default_user)
+
+
+async def test_detach_tool_nonexistent_agent(server: SyncServer, print_tool, default_user):
+ """Test detaching a tool from a nonexistent agent."""
+ with pytest.raises(LettaAgentNotFoundError):
+ await server.agent_manager.detach_tool_async(agent_id="nonexistent-agent-id", tool_id=print_tool.id, actor=default_user)
+
+
+async def test_list_attached_tools(server: SyncServer, sarah_agent, print_tool, other_tool, default_user):
+ """Test listing tools attached to an agent."""
+ # Initially should have no tools
+ agent = await server.agent_manager.get_agent_by_id_async(sarah_agent.id, actor=default_user)
+ assert len(agent.tools) == 0
+
+ # Attach tools
+ await server.agent_manager.attach_tool_async(agent_id=sarah_agent.id, tool_id=print_tool.id, actor=default_user)
+ await server.agent_manager.attach_tool_async(agent_id=sarah_agent.id, tool_id=other_tool.id, actor=default_user)
+
+ # List tools and verify
+ agent = await server.agent_manager.get_agent_by_id_async(sarah_agent.id, actor=default_user)
+ attached_tool_ids = [t.id for t in agent.tools]
+ assert len(attached_tool_ids) == 2
+ assert print_tool.id in attached_tool_ids
+ assert other_tool.id in attached_tool_ids
+
+
+async def test_bulk_attach_tools(server: SyncServer, sarah_agent, print_tool, other_tool, default_user):
+ """Test bulk attaching multiple tools to an agent."""
+ # Bulk attach both tools
+ tool_ids = [print_tool.id, other_tool.id]
+ await server.agent_manager.bulk_attach_tools_async(agent_id=sarah_agent.id, tool_ids=tool_ids, actor=default_user)
+
+ # Verify both tools are attached
+ agent = await server.agent_manager.get_agent_by_id_async(agent_id=sarah_agent.id, actor=default_user)
+ attached_tool_ids = [t.id for t in agent.tools]
+ assert print_tool.id in attached_tool_ids
+ assert other_tool.id in attached_tool_ids
+
+
+async def test_bulk_attach_tools_with_duplicates(server: SyncServer, sarah_agent, print_tool, other_tool, default_user):
+ """Test bulk attaching tools handles duplicates correctly."""
+ # First attach one tool
+ await server.agent_manager.attach_tool_async(agent_id=sarah_agent.id, tool_id=print_tool.id, actor=default_user)
+
+ # Bulk attach both tools (one already attached)
+ tool_ids = [print_tool.id, other_tool.id]
+ await server.agent_manager.bulk_attach_tools_async(agent_id=sarah_agent.id, tool_ids=tool_ids, actor=default_user)
+
+ # Verify both tools are attached and no duplicates
+ agent = await server.agent_manager.get_agent_by_id_async(agent_id=sarah_agent.id, actor=default_user)
+ attached_tool_ids = [t.id for t in agent.tools]
+ assert len(attached_tool_ids) == 2
+ assert print_tool.id in attached_tool_ids
+ assert other_tool.id in attached_tool_ids
+ # Ensure no duplicates
+ assert len(set(attached_tool_ids)) == len(attached_tool_ids)
+
+
+async def test_bulk_attach_tools_empty_list(server: SyncServer, sarah_agent, default_user):
+ """Test bulk attaching empty list of tools."""
+ # Bulk attach empty list
+ await server.agent_manager.bulk_attach_tools_async(agent_id=sarah_agent.id, tool_ids=[], actor=default_user)
+
+ # Verify no tools are attached
+ agent = await server.agent_manager.get_agent_by_id_async(agent_id=sarah_agent.id, actor=default_user)
+ assert len(agent.tools) == 0
+
+
+async def test_bulk_attach_tools_nonexistent_tool(server: SyncServer, sarah_agent, print_tool, default_user):
+ """Test bulk attaching tools with a nonexistent tool ID."""
+ # Try to bulk attach with one valid and one invalid tool ID
+ nonexistent_id = "nonexistent-tool-id"
+ tool_ids = [print_tool.id, nonexistent_id]
+
+ with pytest.raises(NoResultFound) as exc_info:
+ await server.agent_manager.bulk_attach_tools_async(agent_id=sarah_agent.id, tool_ids=tool_ids, actor=default_user)
+
+ # Verify error message contains the missing tool ID
+ assert nonexistent_id in str(exc_info.value)
+
+ # Verify no tools were attached (transaction should have rolled back)
+ agent = await server.agent_manager.get_agent_by_id_async(agent_id=sarah_agent.id, actor=default_user)
+ assert len(agent.tools) == 0
+
+
+async def test_bulk_attach_tools_nonexistent_agent(server: SyncServer, print_tool, other_tool, default_user):
+ """Test bulk attaching tools to a nonexistent agent."""
+ nonexistent_agent_id = "nonexistent-agent-id"
+ tool_ids = [print_tool.id, other_tool.id]
+
+ with pytest.raises(LettaAgentNotFoundError):
+ await server.agent_manager.bulk_attach_tools_async(agent_id=nonexistent_agent_id, tool_ids=tool_ids, actor=default_user)
+
+
+async def test_attach_missing_files_tools_async(server: SyncServer, sarah_agent, default_user):
+ """Test attaching missing file tools to an agent."""
+ # First ensure file tools exist in the system
+ await server.tool_manager.upsert_base_tools_async(actor=default_user, allowed_types={ToolType.LETTA_FILES_CORE})
+
+ # Get initial agent state (should have no file tools)
+ agent_state = await server.agent_manager.get_agent_by_id_async(agent_id=sarah_agent.id, actor=default_user)
+ initial_tool_count = len(agent_state.tools)
+
+ # Attach missing file tools
+ updated_agent_state = await server.agent_manager.attach_missing_files_tools_async(agent_state=agent_state, actor=default_user)
+
+ # Verify all file tools are now attached
+ file_tool_names = {tool.name for tool in updated_agent_state.tools if tool.tool_type == ToolType.LETTA_FILES_CORE}
+ assert file_tool_names == set(FILES_TOOLS)
+
+ # Verify the total tool count increased by the number of file tools
+ assert len(updated_agent_state.tools) == initial_tool_count + len(FILES_TOOLS)
+
+
+async def test_attach_missing_files_tools_async_partial(server: SyncServer, sarah_agent, default_user):
+ """Test attaching missing file tools when some are already attached."""
+ # First ensure file tools exist in the system
+ await server.tool_manager.upsert_base_tools_async(actor=default_user, allowed_types={ToolType.LETTA_FILES_CORE})
+
+ # Get file tool IDs
+ all_tools = await server.tool_manager.list_tools_async(actor=default_user)
+ file_tools = [tool for tool in all_tools if tool.tool_type == ToolType.LETTA_FILES_CORE and tool.name in FILES_TOOLS]
+
+ # Manually attach just the first file tool
+ await server.agent_manager.attach_tool_async(agent_id=sarah_agent.id, tool_id=file_tools[0].id, actor=default_user)
+
+ # Get agent state with one file tool already attached
+ agent_state = await server.agent_manager.get_agent_by_id_async(agent_id=sarah_agent.id, actor=default_user)
+ assert len([t for t in agent_state.tools if t.tool_type == ToolType.LETTA_FILES_CORE]) == 1
+
+ # Attach missing file tools
+ updated_agent_state = await server.agent_manager.attach_missing_files_tools_async(agent_state=agent_state, actor=default_user)
+
+ # Verify all file tools are now attached
+ file_tool_names = {tool.name for tool in updated_agent_state.tools if tool.tool_type == ToolType.LETTA_FILES_CORE}
+ assert file_tool_names == set(FILES_TOOLS)
+
+ # Verify no duplicates
+ all_tool_ids = [tool.id for tool in updated_agent_state.tools]
+ assert len(all_tool_ids) == len(set(all_tool_ids))
+
+
+async def test_attach_missing_files_tools_async_idempotent(server: SyncServer, sarah_agent, default_user):
+ """Test that attach_missing_files_tools is idempotent."""
+ # First ensure file tools exist in the system
+ await server.tool_manager.upsert_base_tools_async(actor=default_user, allowed_types={ToolType.LETTA_FILES_CORE})
+
+ # Get initial agent state
+ agent_state = await server.agent_manager.get_agent_by_id_async(agent_id=sarah_agent.id, actor=default_user)
+
+ # Attach missing file tools the first time
+ updated_agent_state = await server.agent_manager.attach_missing_files_tools_async(agent_state=agent_state, actor=default_user)
+ first_tool_count = len(updated_agent_state.tools)
+
+ # Call attach_missing_files_tools again (should be no-op)
+ final_agent_state = await server.agent_manager.attach_missing_files_tools_async(agent_state=updated_agent_state, actor=default_user)
+
+ # Verify tool count didn't change
+ assert len(final_agent_state.tools) == first_tool_count
+
+ # Verify still have all file tools
+ file_tool_names = {tool.name for tool in final_agent_state.tools if tool.tool_type == ToolType.LETTA_FILES_CORE}
+ assert file_tool_names == set(FILES_TOOLS)
+
+
+async def test_detach_all_files_tools_async(server: SyncServer, sarah_agent, default_user):
+ """Test detaching all file tools from an agent."""
+ # First ensure file tools exist and attach them
+ await server.tool_manager.upsert_base_tools_async(actor=default_user, allowed_types={ToolType.LETTA_FILES_CORE})
+
+ # Get initial agent state and attach file tools
+ agent_state = await server.agent_manager.get_agent_by_id_async(agent_id=sarah_agent.id, actor=default_user)
+ agent_state = await server.agent_manager.attach_missing_files_tools_async(agent_state=agent_state, actor=default_user)
+
+ # Verify file tools are attached
+ file_tool_count_before = len([t for t in agent_state.tools if t.tool_type == ToolType.LETTA_FILES_CORE])
+ assert file_tool_count_before == len(FILES_TOOLS)
+
+ # Detach all file tools
+ updated_agent_state = await server.agent_manager.detach_all_files_tools_async(agent_state=agent_state, actor=default_user)
+
+ # Verify all file tools are detached
+ file_tool_count_after = len([t for t in updated_agent_state.tools if t.tool_type == ToolType.LETTA_FILES_CORE])
+ assert file_tool_count_after == 0
+
+ # Verify the returned state was modified in-place (no DB reload)
+ assert updated_agent_state.id == agent_state.id
+ assert len(updated_agent_state.tools) == len(agent_state.tools) - file_tool_count_before
+
+
+async def test_detach_all_files_tools_async_empty(server: SyncServer, sarah_agent, default_user):
+ """Test detaching all file tools when no file tools are attached."""
+ # Get agent state (should have no file tools initially)
+ agent_state = await server.agent_manager.get_agent_by_id_async(agent_id=sarah_agent.id, actor=default_user)
+ initial_tool_count = len(agent_state.tools)
+
+ # Verify no file tools attached
+ file_tool_count = len([t for t in agent_state.tools if t.tool_type == ToolType.LETTA_FILES_CORE])
+ assert file_tool_count == 0
+
+ # Call detach_all_files_tools (should be no-op)
+ updated_agent_state = await server.agent_manager.detach_all_files_tools_async(agent_state=agent_state, actor=default_user)
+
+ # Verify nothing changed
+ assert len(updated_agent_state.tools) == initial_tool_count
+ assert updated_agent_state == agent_state # Should be the same object since no changes
+
+
+async def test_detach_all_files_tools_async_with_other_tools(server: SyncServer, sarah_agent, print_tool, default_user):
+ """Test detaching all file tools preserves non-file tools."""
+ # First ensure file tools exist
+ await server.tool_manager.upsert_base_tools_async(actor=default_user, allowed_types={ToolType.LETTA_FILES_CORE})
+
+ # Attach a non-file tool
+ await server.agent_manager.attach_tool_async(agent_id=sarah_agent.id, tool_id=print_tool.id, actor=default_user)
+
+ # Get agent state and attach file tools
+ agent_state = await server.agent_manager.get_agent_by_id_async(agent_id=sarah_agent.id, actor=default_user)
+ agent_state = await server.agent_manager.attach_missing_files_tools_async(agent_state=agent_state, actor=default_user)
+
+ # Verify both file tools and print tool are attached
+ file_tools = [t for t in agent_state.tools if t.tool_type == ToolType.LETTA_FILES_CORE]
+ assert len(file_tools) == len(FILES_TOOLS)
+ assert print_tool.id in [t.id for t in agent_state.tools]
+
+ # Detach all file tools
+ updated_agent_state = await server.agent_manager.detach_all_files_tools_async(agent_state=agent_state, actor=default_user)
+
+ # Verify only file tools were removed, print tool remains
+ remaining_file_tools = [t for t in updated_agent_state.tools if t.tool_type == ToolType.LETTA_FILES_CORE]
+ assert len(remaining_file_tools) == 0
+ assert print_tool.id in [t.id for t in updated_agent_state.tools]
+ assert len(updated_agent_state.tools) == 1
+
+
+async def test_detach_all_files_tools_async_idempotent(server: SyncServer, sarah_agent, default_user):
+ """Test that detach_all_files_tools is idempotent."""
+ # First ensure file tools exist and attach them
+ await server.tool_manager.upsert_base_tools_async(actor=default_user, allowed_types={ToolType.LETTA_FILES_CORE})
+
+ # Get initial agent state and attach file tools
+ agent_state = await server.agent_manager.get_agent_by_id_async(agent_id=sarah_agent.id, actor=default_user)
+ agent_state = await server.agent_manager.attach_missing_files_tools_async(agent_state=agent_state, actor=default_user)
+
+ # Detach all file tools once
+ agent_state = await server.agent_manager.detach_all_files_tools_async(agent_state=agent_state, actor=default_user)
+
+ # Verify no file tools
+ assert len([t for t in agent_state.tools if t.tool_type == ToolType.LETTA_FILES_CORE]) == 0
+ tool_count_after_first = len(agent_state.tools)
+
+ # Detach all file tools again (should be no-op)
+ final_agent_state = await server.agent_manager.detach_all_files_tools_async(agent_state=agent_state, actor=default_user)
+
+ # Verify still no file tools and same tool count
+ assert len([t for t in final_agent_state.tools if t.tool_type == ToolType.LETTA_FILES_CORE]) == 0
+ assert len(final_agent_state.tools) == tool_count_after_first
+
+
+async def test_attach_tool_with_default_requires_approval(server: SyncServer, sarah_agent, bash_tool, default_user):
+ """Test that attaching a tool with default requires_approval adds associated tool rule."""
+ # Attach the tool
+ await server.agent_manager.attach_tool_async(agent_id=sarah_agent.id, tool_id=bash_tool.id, actor=default_user)
+
+ # Verify attachment through get_agent_by_id
+ agent = await server.agent_manager.get_agent_by_id_async(agent_id=sarah_agent.id, actor=default_user)
+ assert bash_tool.id in [t.id for t in agent.tools]
+ tool_rules = [rule for rule in agent.tool_rules if rule.tool_name == bash_tool.name]
+ assert len(tool_rules) == 1
+ assert tool_rules[0].type == "requires_approval"
+
+ # Verify that attaching the same tool again doesn't cause duplication
+ await server.agent_manager.attach_tool_async(agent_id=sarah_agent.id, tool_id=bash_tool.id, actor=default_user)
+ agent = await server.agent_manager.get_agent_by_id_async(agent_id=sarah_agent.id, actor=default_user)
+ assert len([t for t in agent.tools if t.id == bash_tool.id]) == 1
+ tool_rules = [rule for rule in agent.tool_rules if rule.tool_name == bash_tool.name]
+ assert len(tool_rules) == 1
+ assert tool_rules[0].type == "requires_approval"
+
+
+async def test_attach_tool_with_default_requires_approval_on_creation(server: SyncServer, bash_tool, default_user):
+ """Test that attaching a tool with default requires_approval adds associated tool rule."""
+ # Create agent with tool
+ agent = await server.agent_manager.create_agent_async(
+ agent_create=CreateAgent(
+ name="agent11",
+ llm_config=LLMConfig.default_config("gpt-4o-mini"),
+ embedding_config=EmbeddingConfig.default_config(provider="openai"),
+ tools=[bash_tool.name],
+ include_base_tools=False,
+ ),
+ actor=default_user,
+ )
+
+ assert bash_tool.id in [t.id for t in agent.tools]
+ tool_rules = [rule for rule in agent.tool_rules if rule.tool_name == bash_tool.name]
+ assert len(tool_rules) == 1
+ assert tool_rules[0].type == "requires_approval"
+
+ # Verify that attaching the same tool again doesn't cause duplication
+ await server.agent_manager.attach_tool_async(agent_id=agent.id, tool_id=bash_tool.id, actor=default_user)
+ agent = await server.agent_manager.get_agent_by_id_async(agent_id=agent.id, actor=default_user)
+ assert len([t for t in agent.tools if t.id == bash_tool.id]) == 1
+ tool_rules = [rule for rule in agent.tool_rules if rule.tool_name == bash_tool.name]
+ assert len(tool_rules) == 1
+ assert tool_rules[0].type == "requires_approval"
+
+ # Modify approval on tool after attach
+ await server.agent_manager.modify_approvals_async(
+ agent_id=agent.id, tool_name=bash_tool.name, requires_approval=False, actor=default_user
+ )
+ agent = await server.agent_manager.get_agent_by_id_async(agent_id=agent.id, actor=default_user)
+ assert len([t for t in agent.tools if t.id == bash_tool.id]) == 1
+ tool_rules = [rule for rule in agent.tool_rules if rule.tool_name == bash_tool.name]
+ assert len(tool_rules) == 0
+
+ # Revert override
+ await server.agent_manager.modify_approvals_async(
+ agent_id=agent.id, tool_name=bash_tool.name, requires_approval=True, actor=default_user
+ )
+ agent = await server.agent_manager.get_agent_by_id_async(agent_id=agent.id, actor=default_user)
+ assert len([t for t in agent.tools if t.id == bash_tool.id]) == 1
+ tool_rules = [rule for rule in agent.tool_rules if rule.tool_name == bash_tool.name]
+ assert len(tool_rules) == 1
+ assert tool_rules[0].type == "requires_approval"
+
+
+# ======================================================================================================================
+# ToolManager Tests
+# ======================================================================================================================
+
+
+async def test_create_tool(server: SyncServer, print_tool, default_user, default_organization):
+ # Assertions to ensure the created tool matches the expected values
+ assert print_tool.created_by_id == default_user.id
+ assert print_tool.tool_type == ToolType.CUSTOM
+
+
+async def test_create_mcp_tool(server: SyncServer, mcp_tool, default_user, default_organization):
+ # Assertions to ensure the created tool matches the expected values
+ assert mcp_tool.created_by_id == default_user.id
+ assert mcp_tool.tool_type == ToolType.EXTERNAL_MCP
+ assert mcp_tool.metadata_[MCP_TOOL_TAG_NAME_PREFIX]["server_name"] == "test"
+ assert mcp_tool.metadata_[MCP_TOOL_TAG_NAME_PREFIX]["server_id"] == "test-server-id"
+
+
+# Test should work with both SQLite and PostgreSQL
+async def test_create_tool_duplicate_name(server: SyncServer, print_tool, default_user, default_organization):
+ data = print_tool.model_dump(exclude=["id"])
+ tool = PydanticTool(**data)
+
+ with pytest.raises(UniqueConstraintViolationError):
+ await server.tool_manager.create_tool_async(tool, actor=default_user)
+
+
+async def test_create_tool_requires_approval(server: SyncServer, bash_tool, default_user, default_organization):
+ # Assertions to ensure the created tool matches the expected values
+ assert bash_tool.created_by_id == default_user.id
+ assert bash_tool.tool_type == ToolType.CUSTOM
+ assert bash_tool.default_requires_approval == True
+
+
+async def test_get_tool_by_id(server: SyncServer, print_tool, default_user):
+ # Fetch the tool by ID using the manager method
+ fetched_tool = await server.tool_manager.get_tool_by_id_async(print_tool.id, actor=default_user)
+
+ # Assertions to check if the fetched tool matches the created tool
+ assert fetched_tool.id == print_tool.id
+ assert fetched_tool.name == print_tool.name
+ assert fetched_tool.description == print_tool.description
+ assert fetched_tool.tags == print_tool.tags
+ assert fetched_tool.metadata_ == print_tool.metadata_
+ assert fetched_tool.source_code == print_tool.source_code
+ assert fetched_tool.source_type == print_tool.source_type
+ assert fetched_tool.tool_type == ToolType.CUSTOM
+
+
+async def test_get_tool_with_actor(server: SyncServer, print_tool, default_user):
+ # Fetch the print_tool by name and organization ID
+ fetched_tool = await server.tool_manager.get_tool_by_name_async(print_tool.name, actor=default_user)
+
+ # Assertions to check if the fetched tool matches the created tool
+ assert fetched_tool.id == print_tool.id
+ assert fetched_tool.name == print_tool.name
+ assert fetched_tool.created_by_id == default_user.id
+ assert fetched_tool.description == print_tool.description
+ assert fetched_tool.tags == print_tool.tags
+ assert fetched_tool.source_code == print_tool.source_code
+ assert fetched_tool.source_type == print_tool.source_type
+ assert fetched_tool.tool_type == ToolType.CUSTOM
+
+
+async def test_list_tools(server: SyncServer, print_tool, default_user):
+ # List tools (should include the one created by the fixture)
+ tools = await server.tool_manager.list_tools_async(actor=default_user, upsert_base_tools=False)
+
+ # Assertions to check that the created tool is listed
+ assert len(tools) == 1
+ assert any(t.id == print_tool.id for t in tools)
+
+
+async def test_list_tools_with_tool_types(server: SyncServer, default_user):
+ """Test filtering tools by tool_types parameter."""
+
+ # create tools with different types
+ def calculator_tool(a: int, b: int) -> int:
+ """Add two numbers.
+
+ Args:
+ a: First number
+ b: Second number
+
+ Returns:
+ Sum of a and b
+ """
+ return a + b
+
+ def weather_tool(city: str) -> str:
+ """Get weather for a city.
+
+ Args:
+ city: Name of the city
+
+ Returns:
+ Weather information
+ """
+ return f"Weather in {city}"
+
+ # create custom tools
+ custom_tool1 = PydanticTool(
+ name="calculator",
+ description="Math tool",
+ source_code=parse_source_code(calculator_tool),
+ source_type="python",
+ tool_type=ToolType.CUSTOM,
+ )
+ custom_tool1.json_schema = derive_openai_json_schema(source_code=custom_tool1.source_code, name=custom_tool1.name)
+ custom_tool1 = await server.tool_manager.create_or_update_tool_async(custom_tool1, actor=default_user)
+
+ custom_tool2 = PydanticTool(
+ name="weather",
+ description="Weather tool",
+ source_code=parse_source_code(weather_tool),
+ source_type="python",
+ tool_type=ToolType.CUSTOM,
+ )
+ custom_tool2.json_schema = derive_openai_json_schema(source_code=custom_tool2.source_code, name=custom_tool2.name)
+ custom_tool2 = await server.tool_manager.create_or_update_tool_async(custom_tool2, actor=default_user)
+
+ # test filtering by single tool type
+ tools = await server.tool_manager.list_tools_async(actor=default_user, tool_types=[ToolType.CUSTOM.value], upsert_base_tools=False)
+ assert len(tools) == 2
+ assert all(t.tool_type == ToolType.CUSTOM for t in tools)
+
+ # test filtering by multiple tool types (should get same result since we only have CUSTOM)
+ tools = await server.tool_manager.list_tools_async(
+ actor=default_user, tool_types=[ToolType.CUSTOM.value, ToolType.LETTA_CORE.value], upsert_base_tools=False
+ )
+ assert len(tools) == 2
+
+ # test filtering by non-existent tool type
+ tools = await server.tool_manager.list_tools_async(
+ actor=default_user, tool_types=[ToolType.EXTERNAL_MCP.value], upsert_base_tools=False
+ )
+ assert len(tools) == 0
+
+
+async def test_list_tools_with_exclude_tool_types(server: SyncServer, default_user, print_tool):
+ """Test excluding tools by exclude_tool_types parameter."""
+ # we already have print_tool which is CUSTOM type
+
+ # create a tool with a different type (simulate by updating tool type directly)
+ def special_tool(msg: str) -> str:
+ """Special tool.
+
+ Args:
+ msg: Message to return
+
+ Returns:
+ The message
+ """
+ return msg
+
+ special = PydanticTool(
+ name="special",
+ description="Special tool",
+ source_code=parse_source_code(special_tool),
+ source_type="python",
+ tool_type=ToolType.CUSTOM,
+ )
+ special.json_schema = derive_openai_json_schema(source_code=special.source_code, name=special.name)
+ special = await server.tool_manager.create_or_update_tool_async(special, actor=default_user)
+
+ # test excluding EXTERNAL_MCP (should get all tools since none are MCP)
+ tools = await server.tool_manager.list_tools_async(
+ actor=default_user, exclude_tool_types=[ToolType.EXTERNAL_MCP.value], upsert_base_tools=False
+ )
+ assert len(tools) == 2 # print_tool and special
+
+ # test excluding CUSTOM (should get no tools)
+ tools = await server.tool_manager.list_tools_async(
+ actor=default_user, exclude_tool_types=[ToolType.CUSTOM.value], upsert_base_tools=False
+ )
+ assert len(tools) == 0
+
+
+async def test_list_tools_with_names(server: SyncServer, default_user):
+ """Test filtering tools by names parameter."""
+
+ # create tools with specific names
+ def alpha_tool() -> str:
+ """Alpha tool.
+
+ Returns:
+ Alpha string
+ """
+ return "alpha"
+
+ def beta_tool() -> str:
+ """Beta tool.
+
+ Returns:
+ Beta string
+ """
+ return "beta"
+
+ def gamma_tool() -> str:
+ """Gamma tool.
+
+ Returns:
+ Gamma string
+ """
+ return "gamma"
+
+ alpha = PydanticTool(name="alpha_tool", description="Alpha", source_code=parse_source_code(alpha_tool), source_type="python")
+ alpha.json_schema = derive_openai_json_schema(source_code=alpha.source_code, name=alpha.name)
+ alpha = await server.tool_manager.create_or_update_tool_async(alpha, actor=default_user)
+
+ beta = PydanticTool(name="beta_tool", description="Beta", source_code=parse_source_code(beta_tool), source_type="python")
+ beta.json_schema = derive_openai_json_schema(source_code=beta.source_code, name=beta.name)
+ beta = await server.tool_manager.create_or_update_tool_async(beta, actor=default_user)
+
+ gamma = PydanticTool(name="gamma_tool", description="Gamma", source_code=parse_source_code(gamma_tool), source_type="python")
+ gamma.json_schema = derive_openai_json_schema(source_code=gamma.source_code, name=gamma.name)
+ gamma = await server.tool_manager.create_or_update_tool_async(gamma, actor=default_user)
+
+ # test filtering by single name
+ tools = await server.tool_manager.list_tools_async(actor=default_user, names=["alpha_tool"], upsert_base_tools=False)
+ assert len(tools) == 1
+ assert tools[0].name == "alpha_tool"
+
+ # test filtering by multiple names
+ tools = await server.tool_manager.list_tools_async(actor=default_user, names=["alpha_tool", "gamma_tool"], upsert_base_tools=False)
+ assert len(tools) == 2
+ assert set(t.name for t in tools) == {"alpha_tool", "gamma_tool"}
+
+ # test filtering by non-existent name
+ tools = await server.tool_manager.list_tools_async(actor=default_user, names=["non_existent_tool"], upsert_base_tools=False)
+ assert len(tools) == 0
+
+
+async def test_list_tools_with_tool_ids(server: SyncServer, default_user):
+ """Test filtering tools by tool_ids parameter."""
+
+ # create multiple tools
+ def tool1() -> str:
+ """Tool 1.
+
+ Returns:
+ String 1
+ """
+ return "1"
+
+ def tool2() -> str:
+ """Tool 2.
+
+ Returns:
+ String 2
+ """
+ return "2"
+
+ def tool3() -> str:
+ """Tool 3.
+
+ Returns:
+ String 3
+ """
+ return "3"
+
+ t1 = PydanticTool(name="tool1", description="First", source_code=parse_source_code(tool1), source_type="python")
+ t1.json_schema = derive_openai_json_schema(source_code=t1.source_code, name=t1.name)
+ t1 = await server.tool_manager.create_or_update_tool_async(t1, actor=default_user)
+
+ t2 = PydanticTool(name="tool2", description="Second", source_code=parse_source_code(tool2), source_type="python")
+ t2.json_schema = derive_openai_json_schema(source_code=t2.source_code, name=t2.name)
+ t2 = await server.tool_manager.create_or_update_tool_async(t2, actor=default_user)
+
+ t3 = PydanticTool(name="tool3", description="Third", source_code=parse_source_code(tool3), source_type="python")
+ t3.json_schema = derive_openai_json_schema(source_code=t3.source_code, name=t3.name)
+ t3 = await server.tool_manager.create_or_update_tool_async(t3, actor=default_user)
+
+ # test filtering by single id
+ tools = await server.tool_manager.list_tools_async(actor=default_user, tool_ids=[t1.id], upsert_base_tools=False)
+ assert len(tools) == 1
+ assert tools[0].id == t1.id
+
+ # test filtering by multiple ids
+ tools = await server.tool_manager.list_tools_async(actor=default_user, tool_ids=[t1.id, t3.id], upsert_base_tools=False)
+ assert len(tools) == 2
+ assert set(t.id for t in tools) == {t1.id, t3.id}
+
+ # test filtering by non-existent id
+ tools = await server.tool_manager.list_tools_async(actor=default_user, tool_ids=["non-existent-id"], upsert_base_tools=False)
+ assert len(tools) == 0
+
+
+async def test_list_tools_with_search(server: SyncServer, default_user):
+ """Test searching tools by partial name match."""
+
+ # create tools with searchable names
+ def calculator_add() -> str:
+ """Calculator add.
+
+ Returns:
+ Add operation
+ """
+ return "add"
+
+ def calculator_subtract() -> str:
+ """Calculator subtract.
+
+ Returns:
+ Subtract operation
+ """
+ return "subtract"
+
+ def weather_forecast() -> str:
+ """Weather forecast.
+
+ Returns:
+ Forecast data
+ """
+ return "forecast"
+
+ calc_add = PydanticTool(
+ name="calculator_add", description="Add numbers", source_code=parse_source_code(calculator_add), source_type="python"
+ )
+ calc_add.json_schema = derive_openai_json_schema(source_code=calc_add.source_code, name=calc_add.name)
+ calc_add = await server.tool_manager.create_or_update_tool_async(calc_add, actor=default_user)
+
+ calc_sub = PydanticTool(
+ name="calculator_subtract", description="Subtract numbers", source_code=parse_source_code(calculator_subtract), source_type="python"
+ )
+ calc_sub.json_schema = derive_openai_json_schema(source_code=calc_sub.source_code, name=calc_sub.name)
+ calc_sub = await server.tool_manager.create_or_update_tool_async(calc_sub, actor=default_user)
+
+ weather = PydanticTool(
+ name="weather_forecast", description="Weather", source_code=parse_source_code(weather_forecast), source_type="python"
+ )
+ weather.json_schema = derive_openai_json_schema(source_code=weather.source_code, name=weather.name)
+ weather = await server.tool_manager.create_or_update_tool_async(weather, actor=default_user)
+
+ # test searching for "calculator" (should find both calculator tools)
+ tools = await server.tool_manager.list_tools_async(actor=default_user, search="calculator", upsert_base_tools=False)
+ assert len(tools) == 2
+ assert all("calculator" in t.name for t in tools)
+
+ # test case-insensitive search
+ tools = await server.tool_manager.list_tools_async(actor=default_user, search="CALCULATOR", upsert_base_tools=False)
+ assert len(tools) == 2
+
+ # test partial match
+ tools = await server.tool_manager.list_tools_async(actor=default_user, search="calc", upsert_base_tools=False)
+ assert len(tools) == 2
+
+ # test search with no matches
+ tools = await server.tool_manager.list_tools_async(actor=default_user, search="nonexistent", upsert_base_tools=False)
+ assert len(tools) == 0
+
+
+async def test_list_tools_return_only_letta_tools(server: SyncServer, default_user):
+ """Test filtering for only Letta tools."""
+ # first, upsert base tools to ensure we have Letta tools
+ await server.tool_manager.upsert_base_tools_async(actor=default_user)
+
+ # create a custom tool
+ def custom_tool() -> str:
+ """Custom tool.
+
+ Returns:
+ Custom string
+ """
+ return "custom"
+
+ custom = PydanticTool(
+ name="custom_tool",
+ description="Custom",
+ source_code=parse_source_code(custom_tool),
+ source_type="python",
+ tool_type=ToolType.CUSTOM,
+ )
+ custom.json_schema = derive_openai_json_schema(source_code=custom.source_code, name=custom.name)
+ custom = await server.tool_manager.create_or_update_tool_async(custom, actor=default_user)
+
+ # test without filter (should get custom tool + all letta tools)
+ tools = await server.tool_manager.list_tools_async(actor=default_user, return_only_letta_tools=False, upsert_base_tools=False)
+ # should have at least the custom tool and some letta tools
+ assert len(tools) > 1
+ assert any(t.name == "custom_tool" for t in tools)
+
+ # test with filter (should only get letta tools)
+ tools = await server.tool_manager.list_tools_async(actor=default_user, return_only_letta_tools=True, upsert_base_tools=False)
+ assert len(tools) > 0
+ # all tools should have tool_type starting with "letta_"
+ assert all(t.tool_type.value.startswith("letta_") for t in tools)
+ # custom tool should not be in the list
+ assert not any(t.name == "custom_tool" for t in tools)
+
+
+async def test_list_tools_combined_filters(server: SyncServer, default_user):
+ """Test combining multiple filters."""
+
+ # create various tools
+ def calc_add() -> str:
+ """Calculator add.
+
+ Returns:
+ Add result
+ """
+ return "add"
+
+ def calc_multiply() -> str:
+ """Calculator multiply.
+
+ Returns:
+ Multiply result
+ """
+ return "multiply"
+
+ def weather_tool() -> str:
+ """Weather tool.
+
+ Returns:
+ Weather data
+ """
+ return "weather"
+
+ calc1 = PydanticTool(
+ name="calculator_add", description="Add", source_code=parse_source_code(calc_add), source_type="python", tool_type=ToolType.CUSTOM
+ )
+ calc1.json_schema = derive_openai_json_schema(source_code=calc1.source_code, name=calc1.name)
+ calc1 = await server.tool_manager.create_or_update_tool_async(calc1, actor=default_user)
+
+ calc2 = PydanticTool(
+ name="calculator_multiply",
+ description="Multiply",
+ source_code=parse_source_code(calc_multiply),
+ source_type="python",
+ tool_type=ToolType.CUSTOM,
+ )
+ calc2.json_schema = derive_openai_json_schema(source_code=calc2.source_code, name=calc2.name)
+ calc2 = await server.tool_manager.create_or_update_tool_async(calc2, actor=default_user)
+
+ weather = PydanticTool(
+ name="weather_current",
+ description="Weather",
+ source_code=parse_source_code(weather_tool),
+ source_type="python",
+ tool_type=ToolType.CUSTOM,
+ )
+ weather.json_schema = derive_openai_json_schema(source_code=weather.source_code, name=weather.name)
+ weather = await server.tool_manager.create_or_update_tool_async(weather, actor=default_user)
+
+ # combine search with tool_types
+ tools = await server.tool_manager.list_tools_async(
+ actor=default_user, search="calculator", tool_types=[ToolType.CUSTOM.value], upsert_base_tools=False
+ )
+ assert len(tools) == 2
+ assert all("calculator" in t.name and t.tool_type == ToolType.CUSTOM for t in tools)
+
+ # combine names with tool_ids
+ tools = await server.tool_manager.list_tools_async(
+ actor=default_user, names=["calculator_add"], tool_ids=[calc1.id], upsert_base_tools=False
+ )
+ assert len(tools) == 1
+ assert tools[0].id == calc1.id
+
+ # combine search with exclude_tool_types
+ tools = await server.tool_manager.list_tools_async(
+ actor=default_user, search="calculator", exclude_tool_types=[ToolType.EXTERNAL_MCP.value], upsert_base_tools=False
+ )
+ assert len(tools) == 2
+
+
+async def test_count_tools_async(server: SyncServer, default_user):
+ """Test counting tools with various filters."""
+
+ # create multiple tools
+ def tool_a() -> str:
+ """Tool A.
+
+ Returns:
+ String a
+ """
+ return "a"
+
+ def tool_b() -> str:
+ """Tool B.
+
+ Returns:
+ String b
+ """
+ return "b"
+
+ def search_tool() -> str:
+ """Search tool.
+
+ Returns:
+ Search result
+ """
+ return "search"
+
+ ta = PydanticTool(
+ name="tool_a", description="A", source_code=parse_source_code(tool_a), source_type="python", tool_type=ToolType.CUSTOM
+ )
+ ta.json_schema = derive_openai_json_schema(source_code=ta.source_code, name=ta.name)
+ ta = await server.tool_manager.create_or_update_tool_async(ta, actor=default_user)
+
+ tb = PydanticTool(
+ name="tool_b", description="B", source_code=parse_source_code(tool_b), source_type="python", tool_type=ToolType.CUSTOM
+ )
+ tb.json_schema = derive_openai_json_schema(source_code=tb.source_code, name=tb.name)
+ tb = await server.tool_manager.create_or_update_tool_async(tb, actor=default_user)
+
+ # upsert base tools to ensure we have Letta tools for counting
+ await server.tool_manager.upsert_base_tools_async(actor=default_user)
+
+ # count all tools (should have 2 custom tools + letta tools)
+ count = await server.tool_manager.count_tools_async(actor=default_user)
+ assert count > 2 # at least our 2 custom tools + letta tools
+
+ # count with tool_types filter
+ count = await server.tool_manager.count_tools_async(actor=default_user, tool_types=[ToolType.CUSTOM.value])
+ assert count == 2 # only our custom tools
+
+ # count with search filter
+ count = await server.tool_manager.count_tools_async(actor=default_user, search="tool")
+ # should at least find our 2 tools (tool_a, tool_b)
+ assert count >= 2
+
+ # count with names filter
+ count = await server.tool_manager.count_tools_async(actor=default_user, names=["tool_a", "tool_b"])
+ assert count == 2
+
+ # count with return_only_letta_tools
+ count = await server.tool_manager.count_tools_async(actor=default_user, return_only_letta_tools=True)
+ assert count > 0 # should have letta tools
+
+ # count with exclude_tool_types (exclude all letta tool types)
+ count = await server.tool_manager.count_tools_async(
+ actor=default_user,
+ exclude_tool_types=[
+ ToolType.LETTA_CORE.value,
+ ToolType.LETTA_MEMORY_CORE.value,
+ ToolType.LETTA_MULTI_AGENT_CORE.value,
+ ToolType.LETTA_SLEEPTIME_CORE.value,
+ ToolType.LETTA_VOICE_SLEEPTIME_CORE.value,
+ ToolType.LETTA_BUILTIN.value,
+ ToolType.LETTA_FILES_CORE.value,
+ ],
+ )
+ assert count == 2 # only our custom tools
+
+
+async def test_update_tool_by_id(server: SyncServer, print_tool, default_user):
+ updated_description = "updated_description"
+ return_char_limit = 10000
+
+ # Create a ToolUpdate object to modify the print_tool's description
+ tool_update = ToolUpdate(description=updated_description, return_char_limit=return_char_limit)
+
+ # Update the tool using the manager method
+ await server.tool_manager.update_tool_by_id_async(print_tool.id, tool_update, actor=default_user)
+
+ # Fetch the updated tool to verify the changes
+ updated_tool = await server.tool_manager.get_tool_by_id_async(print_tool.id, actor=default_user)
+
+ # Assertions to check if the update was successful
+ assert updated_tool.description == updated_description
+ assert updated_tool.return_char_limit == return_char_limit
+ assert updated_tool.tool_type == ToolType.CUSTOM
+
+ # Dangerous: we bypass safety to give it another tool type
+ await server.tool_manager.update_tool_by_id_async(
+ print_tool.id, tool_update, actor=default_user, updated_tool_type=ToolType.EXTERNAL_MCP
+ )
+ updated_tool = await server.tool_manager.get_tool_by_id_async(print_tool.id, actor=default_user)
+ assert updated_tool.tool_type == ToolType.EXTERNAL_MCP
+
+
+#@pytest.mark.asyncio
+#async def test_update_tool_source_code_refreshes_schema_and_name(server: SyncServer, print_tool, default_user):
+# def counter_tool(counter: int):
+# """
+# Args:
+# counter (int): The counter to count to.
+#
+# Returns:
+# bool: If it successfully counted to the counter.
+# """
+# for c in range(counter):
+# print(c)
+#
+# return True
+#
+# # Test begins
+# og_json_schema = print_tool.json_schema
+#
+# source_code = parse_source_code(counter_tool)
+#
+# # Create a ToolUpdate object to modify the tool's source_code
+# tool_update = ToolUpdate(source_code=source_code)
+#
+# # Update the tool using the manager method
+# await server.tool_manager.update_tool_by_id_async(print_tool.id, tool_update, actor=default_user)
+#
+# # Fetch the updated tool to verify the changes
+# updated_tool = await server.tool_manager.get_tool_by_id_async(print_tool.id, actor=default_user)
+#
+# # Assertions to check if the update was successful, and json_schema is updated as well
+# assert updated_tool.source_code == source_code
+# assert updated_tool.json_schema != og_json_schema
+#
+# new_schema = derive_openai_json_schema(source_code=updated_tool.source_code)
+# assert updated_tool.json_schema == new_schema
+# assert updated_tool.tool_type == ToolType.CUSTOM
+
+
+#@pytest.mark.asyncio
+#async def test_update_tool_source_code_refreshes_schema_only(server: SyncServer, print_tool, default_user):
+# def counter_tool(counter: int):
+# """
+# Args:
+# counter (int): The counter to count to.
+#
+# Returns:
+# bool: If it successfully counted to the counter.
+# """
+# for c in range(counter):
+# print(c)
+#
+# return True
+#
+# # Test begins
+# og_json_schema = print_tool.json_schema
+#
+# source_code = parse_source_code(counter_tool)
+# name = "counter_tool"
+#
+# # Create a ToolUpdate object to modify the tool's source_code
+# tool_update = ToolUpdate(source_code=source_code)
+#
+# # Update the tool using the manager method
+# await server.tool_manager.update_tool_by_id_async(print_tool.id, tool_update, actor=default_user)
+#
+# # Fetch the updated tool to verify the changes
+# updated_tool = await server.tool_manager.get_tool_by_id_async(print_tool.id, actor=default_user)
+#
+# # Assertions to check if the update was successful, and json_schema is updated as well
+# assert updated_tool.source_code == source_code
+# assert updated_tool.json_schema != og_json_schema
+#
+# new_schema = derive_openai_json_schema(source_code=updated_tool.source_code, name=updated_tool.name)
+# assert updated_tool.json_schema == new_schema
+# assert updated_tool.name == name
+# assert updated_tool.tool_type == ToolType.CUSTOM
+
+
+async def test_update_tool_multi_user(server: SyncServer, print_tool, default_user, other_user):
+ updated_description = "updated_description"
+
+ # Create a ToolUpdate object to modify the print_tool's description
+ tool_update = ToolUpdate(description=updated_description)
+
+ # Update the print_tool using the manager method, but WITH THE OTHER USER'S ID!
+ await server.tool_manager.update_tool_by_id_async(print_tool.id, tool_update, actor=other_user)
+
+ # Check that the created_by and last_updated_by fields are correct
+ # Fetch the updated print_tool to verify the changes
+ updated_tool = await server.tool_manager.get_tool_by_id_async(print_tool.id, actor=default_user)
+
+ assert updated_tool.last_updated_by_id == other_user.id
+ assert updated_tool.created_by_id == default_user.id
+
+
+async def test_delete_tool_by_id(server: SyncServer, print_tool, default_user):
+ # Delete the print_tool using the manager method
+ await server.tool_manager.delete_tool_by_id_async(print_tool.id, actor=default_user)
+
+ tools = await server.tool_manager.list_tools_async(actor=default_user, upsert_base_tools=False)
+ assert len(tools) == 0
+
+
+async def test_upsert_base_tools(server: SyncServer, default_user):
+ tools = await server.tool_manager.upsert_base_tools_async(actor=default_user)
+
+ # Calculate expected tools accounting for production filtering
+ if settings.environment == "PRODUCTION":
+ expected_tool_names = sorted(LETTA_TOOL_SET - set(LOCAL_ONLY_MULTI_AGENT_TOOLS))
+ else:
+ expected_tool_names = sorted(LETTA_TOOL_SET)
+
+ assert sorted([t.name for t in tools]) == expected_tool_names
+
+ # Call it again to make sure it doesn't create duplicates
+ tools = await server.tool_manager.upsert_base_tools_async(actor=default_user)
+ assert sorted([t.name for t in tools]) == expected_tool_names
+
+ # Confirm that the return tools have no source_code, but a json_schema
+ for t in tools:
+ if t.name in BASE_TOOLS:
+ assert t.tool_type == ToolType.LETTA_CORE
+ elif t.name in BASE_MEMORY_TOOLS:
+ assert t.tool_type == ToolType.LETTA_MEMORY_CORE
+ elif t.name in MULTI_AGENT_TOOLS:
+ assert t.tool_type == ToolType.LETTA_MULTI_AGENT_CORE
+ elif t.name in BASE_SLEEPTIME_TOOLS:
+ assert t.tool_type == ToolType.LETTA_SLEEPTIME_CORE
+ elif t.name in BASE_VOICE_SLEEPTIME_TOOLS:
+ assert t.tool_type == ToolType.LETTA_VOICE_SLEEPTIME_CORE
+ elif t.name in BASE_VOICE_SLEEPTIME_CHAT_TOOLS:
+ assert t.tool_type == ToolType.LETTA_VOICE_SLEEPTIME_CORE
+ elif t.name in BUILTIN_TOOLS:
+ assert t.tool_type == ToolType.LETTA_BUILTIN
+ elif t.name in FILES_TOOLS:
+ assert t.tool_type == ToolType.LETTA_FILES_CORE
+ else:
+ pytest.fail(f"The tool name is unrecognized as a base tool: {t.name}")
+ assert t.source_code is None
+ assert t.json_schema
+
+
+ "tool_type,expected_names",
+ [
+ (ToolType.LETTA_CORE, BASE_TOOLS),
+ (ToolType.LETTA_MEMORY_CORE, BASE_MEMORY_TOOLS),
+ (ToolType.LETTA_MULTI_AGENT_CORE, MULTI_AGENT_TOOLS),
+ (ToolType.LETTA_SLEEPTIME_CORE, BASE_SLEEPTIME_TOOLS),
+ (ToolType.LETTA_VOICE_SLEEPTIME_CORE, sorted(set(BASE_VOICE_SLEEPTIME_TOOLS + BASE_VOICE_SLEEPTIME_CHAT_TOOLS) - {"send_message"})),
+ (ToolType.LETTA_BUILTIN, BUILTIN_TOOLS),
+ (ToolType.LETTA_FILES_CORE, FILES_TOOLS),
+ ],
+)
+async def test_upsert_filtered_base_tools(server: SyncServer, default_user, tool_type, expected_names):
+ tools = await server.tool_manager.upsert_base_tools_async(actor=default_user, allowed_types={tool_type})
+ tool_names = sorted([t.name for t in tools])
+
+ # Adjust expected names for multi-agent tools in production
+ if tool_type == ToolType.LETTA_MULTI_AGENT_CORE and settings.environment == "PRODUCTION":
+ expected_sorted = sorted(set(expected_names) - set(LOCAL_ONLY_MULTI_AGENT_TOOLS))
+ else:
+ expected_sorted = sorted(expected_names)
+
+ assert tool_names == expected_sorted
+ assert all(t.tool_type == tool_type for t in tools)
+
+
+async def test_upsert_multiple_tool_types(server: SyncServer, default_user):
+ allowed = {ToolType.LETTA_CORE, ToolType.LETTA_BUILTIN, ToolType.LETTA_FILES_CORE}
+ tools = await server.tool_manager.upsert_base_tools_async(actor=default_user, allowed_types=allowed)
+ tool_names = {t.name for t in tools}
+ expected = set(BASE_TOOLS + BUILTIN_TOOLS + FILES_TOOLS)
+
+ assert tool_names == expected
+ assert all(t.tool_type in allowed for t in tools)
+
+
+async def test_upsert_base_tools_with_empty_type_filter(server: SyncServer, default_user):
+ tools = await server.tool_manager.upsert_base_tools_async(actor=default_user, allowed_types=set())
+ assert tools == []
+
+
+async def test_bulk_upsert_tools_async(server: SyncServer, default_user):
+ """Test bulk upserting multiple tools at once"""
+ # create multiple test tools
+ tools_data = []
+ for i in range(5):
+ tool = PydanticTool(
+ name=f"bulk_test_tool_{i}",
+ description=f"Test tool {i} for bulk operations",
+ tags=["bulk", "test"],
+ source_code=f"def bulk_test_tool_{i}():\n '''Test tool {i} function'''\n return 'result_{i}'",
+ source_type="python",
+ )
+ tools_data.append(tool)
+
+ # initial bulk upsert - should create all tools
+ created_tools = await server.tool_manager.bulk_upsert_tools_async(tools_data, default_user)
+ assert len(created_tools) == 5
+ assert all(t.name.startswith("bulk_test_tool_") for t in created_tools)
+ assert all(t.description for t in created_tools)
+
+ # verify all tools were created
+ for i in range(5):
+ tool = await server.tool_manager.get_tool_by_name_async(f"bulk_test_tool_{i}", default_user)
+ assert tool is not None
+ assert tool.description == f"Test tool {i} for bulk operations"
+
+ # modify some tools and upsert again - should update existing tools
+ tools_data[0].description = "Updated description for tool 0"
+ tools_data[2].tags = ["bulk", "test", "updated"]
+
+ updated_tools = await server.tool_manager.bulk_upsert_tools_async(tools_data, default_user)
+ assert len(updated_tools) == 5
+
+ # verify updates were applied
+ tool_0 = await server.tool_manager.get_tool_by_name_async("bulk_test_tool_0", default_user)
+ assert tool_0.description == "Updated description for tool 0"
+
+ tool_2 = await server.tool_manager.get_tool_by_name_async("bulk_test_tool_2", default_user)
+ assert "updated" in tool_2.tags
+
+ # test with empty list
+ empty_result = await server.tool_manager.bulk_upsert_tools_async([], default_user)
+ assert empty_result == []
+
+ # test with tools missing descriptions (should auto-generate from json schema)
+ no_desc_tool = PydanticTool(
+ name="no_description_tool",
+ tags=["test"],
+ source_code="def no_description_tool():\n '''This is a docstring description'''\n return 'result'",
+ source_type="python",
+ )
+ result = await server.tool_manager.bulk_upsert_tools_async([no_desc_tool], default_user)
+ assert len(result) == 1
+ assert result[0].description is not None # should be auto-generated from docstring
+
+
+async def test_bulk_upsert_tools_name_conflict(server: SyncServer, default_user):
+ """Test bulk upserting tools handles name+org_id unique constraint correctly"""
+
+ # create a tool with a specific name
+ original_tool = PydanticTool(
+ name="unique_name_tool",
+ description="Original description",
+ tags=["original"],
+ source_code="def unique_name_tool():\n '''Original function'''\n return 'original'",
+ source_type="python",
+ )
+
+ # create it
+ created = await server.tool_manager.create_tool_async(original_tool, default_user)
+ original_id = created.id
+
+ # now try to bulk upsert with same name but different id
+ conflicting_tool = PydanticTool(
+ name="unique_name_tool", # same name
+ description="Updated via bulk upsert",
+ tags=["updated", "bulk"],
+ source_code="def unique_name_tool():\n '''Updated function'''\n return 'updated'",
+ source_type="python",
+ )
+
+ # bulk upsert should update the existing tool based on name conflict
+ result = await server.tool_manager.bulk_upsert_tools_async([conflicting_tool], default_user)
+ assert len(result) == 1
+ assert result[0].name == "unique_name_tool"
+ assert result[0].description == "Updated via bulk upsert"
+ assert "updated" in result[0].tags
+ assert "bulk" in result[0].tags
+
+ # verify only one tool exists with this name
+ all_tools = await server.tool_manager.list_tools_async(actor=default_user)
+ tools_with_name = [t for t in all_tools if t.name == "unique_name_tool"]
+ assert len(tools_with_name) == 1
+
+ # the id should remain the same as the original
+ assert tools_with_name[0].id == original_id
+
+
+async def test_bulk_upsert_tools_mixed_create_update(server: SyncServer, default_user):
+ """Test bulk upserting with mix of new tools and updates to existing ones"""
+
+ # create some existing tools
+ existing_tools = []
+ for i in range(3):
+ tool = PydanticTool(
+ name=f"existing_tool_{i}",
+ description=f"Existing tool {i}",
+ tags=["existing"],
+ source_code=f"def existing_tool_{i}():\n '''Existing {i}'''\n return 'existing_{i}'",
+ source_type="python",
+ )
+ created = await server.tool_manager.create_tool_async(tool, default_user)
+ existing_tools.append(created)
+
+ # prepare bulk upsert with mix of updates and new tools
+ bulk_tools = []
+
+ # update existing tool 0 by name
+ bulk_tools.append(
+ PydanticTool(
+ name="existing_tool_0", # matches by name
+ description="Updated existing tool 0",
+ tags=["existing", "updated"],
+ source_code="def existing_tool_0():\n '''Updated 0'''\n return 'updated_0'",
+ source_type="python",
+ )
+ )
+
+ # update existing tool 1 by name (since bulk upsert matches by name, not id)
+ bulk_tools.append(
+ PydanticTool(
+ name="existing_tool_1", # matches by name
+ description="Updated existing tool 1",
+ tags=["existing", "updated"],
+ source_code="def existing_tool_1():\n '''Updated 1'''\n return 'updated_1'",
+ source_type="python",
+ )
+ )
+
+ # add completely new tools
+ for i in range(3, 6):
+ bulk_tools.append(
+ PydanticTool(
+ name=f"new_tool_{i}",
+ description=f"New tool {i}",
+ tags=["new"],
+ source_code=f"def new_tool_{i}():\n '''New {i}'''\n return 'new_{i}'",
+ source_type="python",
+ )
+ )
+
+ # perform bulk upsert
+ result = await server.tool_manager.bulk_upsert_tools_async(bulk_tools, default_user)
+ assert len(result) == 5 # 2 updates + 3 new
+
+ # verify updates
+ tool_0 = await server.tool_manager.get_tool_by_name_async("existing_tool_0", default_user)
+ assert tool_0.description == "Updated existing tool 0"
+ assert "updated" in tool_0.tags
+ assert tool_0.id == existing_tools[0].id # id should remain same
+
+ # verify tool 1 was updated
+ tool_1 = await server.tool_manager.get_tool_by_id_async(existing_tools[1].id, default_user)
+ assert tool_1.name == "existing_tool_1" # name stays same
+ assert tool_1.description == "Updated existing tool 1"
+ assert "updated" in tool_1.tags
+
+ # verify new tools were created
+ for i in range(3, 6):
+ new_tool = await server.tool_manager.get_tool_by_name_async(f"new_tool_{i}", default_user)
+ assert new_tool is not None
+ assert new_tool.description == f"New tool {i}"
+ assert "new" in new_tool.tags
+
+ # verify existing_tool_2 was not affected
+ tool_2 = await server.tool_manager.get_tool_by_id_async(existing_tools[2].id, default_user)
+ assert tool_2.name == "existing_tool_2"
+ assert tool_2.description == "Existing tool 2"
+ assert tool_2.tags == ["existing"]
+
+
+async def test_bulk_upsert_tools_override_existing_true(server: SyncServer, default_user):
+ """Test bulk_upsert_tools_async with override_existing_tools=True (default behavior)"""
+
+ # create some existing tools
+ existing_tool = PydanticTool(
+ name="test_override_tool",
+ description="Original description",
+ tags=["original"],
+ source_code="def test_override_tool():\n '''Original'''\n return 'original'",
+ source_type="python",
+ )
+ created = await server.tool_manager.create_tool_async(existing_tool, default_user)
+ original_id = created.id
+
+ # prepare updated version of the tool
+ updated_tool = PydanticTool(
+ name="test_override_tool",
+ description="Updated description",
+ tags=["updated"],
+ source_code="def test_override_tool():\n '''Updated'''\n return 'updated'",
+ source_type="python",
+ )
+
+ # bulk upsert with override_existing_tools=True (default)
+ result = await server.tool_manager.bulk_upsert_tools_async([updated_tool], default_user, override_existing_tools=True)
+
+ assert len(result) == 1
+ assert result[0].id == original_id # id should remain the same
+ assert result[0].description == "Updated description" # description should be updated
+ assert result[0].tags == ["updated"] # tags should be updated
+
+ # verify the tool was actually updated in the database
+ fetched = await server.tool_manager.get_tool_by_id_async(original_id, default_user)
+ assert fetched.description == "Updated description"
+ assert fetched.tags == ["updated"]
+
+
+async def test_bulk_upsert_tools_override_existing_false(server: SyncServer, default_user):
+ """Test bulk_upsert_tools_async with override_existing_tools=False (skip existing)"""
+
+ # create some existing tools
+ existing_tool = PydanticTool(
+ name="test_no_override_tool",
+ description="Original description",
+ tags=["original"],
+ source_code="def test_no_override_tool():\n '''Original'''\n return 'original'",
+ source_type="python",
+ )
+ created = await server.tool_manager.create_tool_async(existing_tool, default_user)
+ original_id = created.id
+
+ # prepare updated version of the tool
+ updated_tool = PydanticTool(
+ name="test_no_override_tool",
+ description="Should not be updated",
+ tags=["should_not_update"],
+ source_code="def test_no_override_tool():\n '''Should not update'''\n return 'should_not_update'",
+ source_type="python",
+ )
+
+ # bulk upsert with override_existing_tools=False
+ result = await server.tool_manager.bulk_upsert_tools_async([updated_tool], default_user, override_existing_tools=False)
+
+ assert len(result) == 1
+ assert result[0].id == original_id # id should remain the same
+ assert result[0].description == "Original description" # description should NOT be updated
+ assert result[0].tags == ["original"] # tags should NOT be updated
+
+ # verify the tool was NOT updated in the database
+ fetched = await server.tool_manager.get_tool_by_id_async(original_id, default_user)
+ assert fetched.description == "Original description"
+ assert fetched.tags == ["original"]
+
+
+async def test_bulk_upsert_tools_override_mixed_scenario(server: SyncServer, default_user):
+ """Test bulk_upsert_tools_async with override_existing_tools=False in mixed create/update scenario"""
+
+ # create some existing tools
+ existing_tools = []
+ for i in range(2):
+ tool = PydanticTool(
+ name=f"mixed_existing_{i}",
+ description=f"Original {i}",
+ tags=["original"],
+ source_code=f"def mixed_existing_{i}():\n '''Original {i}'''\n return 'original_{i}'",
+ source_type="python",
+ )
+ created = await server.tool_manager.create_tool_async(tool, default_user)
+ existing_tools.append(created)
+
+ # prepare bulk tools: 2 updates (that should be skipped) + 3 new creations
+ bulk_tools = []
+
+ # these should be skipped when override_existing_tools=False
+ for i in range(2):
+ bulk_tools.append(
+ PydanticTool(
+ name=f"mixed_existing_{i}",
+ description=f"Should not update {i}",
+ tags=["should_not_update"],
+ source_code=f"def mixed_existing_{i}():\n '''Should not update {i}'''\n return 'should_not_update_{i}'",
+ source_type="python",
+ )
+ )
+
+ # these should be created
+ for i in range(3):
+ bulk_tools.append(
+ PydanticTool(
+ name=f"mixed_new_{i}",
+ description=f"New tool {i}",
+ tags=["new"],
+ source_code=f"def mixed_new_{i}():\n '''New {i}'''\n return 'new_{i}'",
+ source_type="python",
+ )
+ )
+
+ # bulk upsert with override_existing_tools=False
+ result = await server.tool_manager.bulk_upsert_tools_async(bulk_tools, default_user, override_existing_tools=False)
+
+ assert len(result) == 5 # 2 existing (not updated) + 3 new
+
+ # verify existing tools were NOT updated
+ for i in range(2):
+ tool = await server.tool_manager.get_tool_by_name_async(f"mixed_existing_{i}", default_user)
+ assert tool.description == f"Original {i}" # should remain original
+ assert tool.tags == ["original"] # should remain original
+ assert tool.id == existing_tools[i].id # id should remain same
+
+ # verify new tools were created
+ for i in range(3):
+ new_tool = await server.tool_manager.get_tool_by_name_async(f"mixed_new_{i}", default_user)
+ assert new_tool is not None
+ assert new_tool.description == f"New tool {i}"
+ assert new_tool.tags == ["new"]
+
+
+async def test_create_tool_with_pip_requirements(server: SyncServer, default_user, default_organization):
+ def test_tool_with_deps():
+ """
+ A test tool with pip dependencies.
+
+ Returns:
+ str: Hello message.
+ """
+ return "hello"
+
+ # Create pip requirements
+ pip_reqs = [
+ PipRequirement(name="requests", version="2.28.0"),
+ PipRequirement(name="numpy"), # No version specified
+ ]
+
+ # Set up tool details
+ source_code = parse_source_code(test_tool_with_deps)
+ source_type = "python"
+ description = "A test tool with pip dependencies"
+ tags = ["test"]
+ metadata = {"test": "pip_requirements"}
+
+ tool = PydanticTool(
+ description=description, tags=tags, source_code=source_code, source_type=source_type, metadata_=metadata, pip_requirements=pip_reqs
+ )
+ derived_json_schema = derive_openai_json_schema(source_code=tool.source_code, name=tool.name)
+ derived_name = derived_json_schema["name"]
+ tool.json_schema = derived_json_schema
+ tool.name = derived_name
+
+ created_tool = await server.tool_manager.create_or_update_tool_async(tool, actor=default_user)
+
+ # Assertions
+ assert created_tool.pip_requirements is not None
+ assert len(created_tool.pip_requirements) == 2
+ assert created_tool.pip_requirements[0].name == "requests"
+ assert created_tool.pip_requirements[0].version == "2.28.0"
+ assert created_tool.pip_requirements[1].name == "numpy"
+ assert created_tool.pip_requirements[1].version is None
+
+
+async def test_create_tool_without_pip_requirements(server: SyncServer, print_tool):
+ # Verify that tools without pip_requirements have the field as None
+ assert print_tool.pip_requirements is None
+
+
+async def test_update_tool_pip_requirements(server: SyncServer, print_tool, default_user):
+ # Add pip requirements to existing tool
+ pip_reqs = [
+ PipRequirement(name="pandas", version="1.5.0"),
+ PipRequirement(name="sumy"),
+ ]
+
+ tool_update = ToolUpdate(pip_requirements=pip_reqs)
+ await server.tool_manager.update_tool_by_id_async(print_tool.id, tool_update, actor=default_user)
+
+ # Fetch the updated tool
+ updated_tool = await server.tool_manager.get_tool_by_id_async(print_tool.id, actor=default_user)
+
+ # Assertions
+ assert updated_tool.pip_requirements is not None
+ assert len(updated_tool.pip_requirements) == 2
+ assert updated_tool.pip_requirements[0].name == "pandas"
+ assert updated_tool.pip_requirements[0].version == "1.5.0"
+ assert updated_tool.pip_requirements[1].name == "sumy"
+ assert updated_tool.pip_requirements[1].version is None
+
+
+async def test_update_tool_clear_pip_requirements(server: SyncServer, default_user, default_organization):
+ def test_tool_clear_deps():
+ """
+ A test tool to clear dependencies.
+
+ Returns:
+ str: Hello message.
+ """
+ return "hello"
+
+ # Create a tool with pip requirements
+ pip_reqs = [PipRequirement(name="requests")]
+
+ # Set up tool details
+ source_code = parse_source_code(test_tool_clear_deps)
+ source_type = "python"
+ description = "A test tool to clear dependencies"
+ tags = ["test"]
+ metadata = {"test": "clear_deps"}
+
+ tool = PydanticTool(
+ description=description, tags=tags, source_code=source_code, source_type=source_type, metadata_=metadata, pip_requirements=pip_reqs
+ )
+ derived_json_schema = derive_openai_json_schema(source_code=tool.source_code, name=tool.name)
+ derived_name = derived_json_schema["name"]
+ tool.json_schema = derived_json_schema
+ tool.name = derived_name
+
+ created_tool = await server.tool_manager.create_or_update_tool_async(tool, actor=default_user)
+
+ # Verify it has requirements
+ assert created_tool.pip_requirements is not None
+ assert len(created_tool.pip_requirements) == 1
+
+ # Clear the requirements
+ tool_update = ToolUpdate(pip_requirements=[])
+ await server.tool_manager.update_tool_by_id_async(created_tool.id, tool_update, actor=default_user)
+
+ # Fetch the updated tool
+ updated_tool = await server.tool_manager.get_tool_by_id_async(created_tool.id, actor=default_user)
+
+ # Assertions
+ assert updated_tool.pip_requirements == []
+
+
+async def test_pip_requirements_roundtrip(server: SyncServer, default_user, default_organization):
+ def roundtrip_test_tool():
+ """
+ Test pip requirements roundtrip.
+
+ Returns:
+ str: Test message.
+ """
+ return "test"
+
+ # Create pip requirements with various version formats
+ pip_reqs = [
+ PipRequirement(name="requests", version="2.28.0"),
+ PipRequirement(name="flask", version="2.0"),
+ PipRequirement(name="django", version="4.1.0-beta"),
+ PipRequirement(name="numpy"), # No version
+ ]
+
+ # Set up tool details
+ source_code = parse_source_code(roundtrip_test_tool)
+ source_type = "python"
+ description = "Test pip requirements roundtrip"
+ tags = ["test"]
+ metadata = {"test": "roundtrip"}
+
+ tool = PydanticTool(
+ description=description, tags=tags, source_code=source_code, source_type=source_type, metadata_=metadata, pip_requirements=pip_reqs
+ )
+ derived_json_schema = derive_openai_json_schema(source_code=tool.source_code, name=tool.name)
+ derived_name = derived_json_schema["name"]
+ tool.json_schema = derived_json_schema
+ tool.name = derived_name
+
+ created_tool = await server.tool_manager.create_or_update_tool_async(tool, actor=default_user)
+
+ # Fetch by ID
+ fetched_tool = await server.tool_manager.get_tool_by_id_async(created_tool.id, actor=default_user)
+
+ # Verify all requirements match exactly
+ assert fetched_tool.pip_requirements is not None
+ assert len(fetched_tool.pip_requirements) == 4
+
+ # Check each requirement
+ reqs_dict = {req.name: req.version for req in fetched_tool.pip_requirements}
+ assert reqs_dict["requests"] == "2.28.0"
+ assert reqs_dict["flask"] == "2.0"
+ assert reqs_dict["django"] == "4.1.0-beta"
+ assert reqs_dict["numpy"] is None
+
+
+async def test_update_default_requires_approval(server: SyncServer, bash_tool, default_user):
+ # Update field
+ tool_update = ToolUpdate(default_requires_approval=False)
+ await server.tool_manager.update_tool_by_id_async(bash_tool.id, tool_update, actor=default_user)
+
+ # Fetch the updated tool
+ updated_tool = await server.tool_manager.get_tool_by_id_async(bash_tool.id, actor=default_user)
+
+ # Assertions
+ assert updated_tool.default_requires_approval == False
+
+ # Revert update
+ tool_update = ToolUpdate(default_requires_approval=True)
+ await server.tool_manager.update_tool_by_id_async(bash_tool.id, tool_update, actor=default_user)
+
+ # Fetch the updated tool
+ updated_tool = await server.tool_manager.get_tool_by_id_async(bash_tool.id, actor=default_user)
+
+ # Assertions
+ assert updated_tool.default_requires_approval == True
diff --git a/tests/managers/test_user_manager.py b/tests/managers/test_user_manager.py
new file mode 100644
index 000000000..eac55f196
--- /dev/null
+++ b/tests/managers/test_user_manager.py
@@ -0,0 +1,189 @@
+import logging
+import os
+import random
+import re
+import string
+import time
+import uuid
+from datetime import datetime, timedelta, timezone
+from typing import List
+from unittest.mock import AsyncMock, Mock, patch
+
+import pytest
+from _pytest.python_api import approx
+from anthropic.types.beta import BetaMessage
+from anthropic.types.beta.messages import BetaMessageBatchIndividualResponse, BetaMessageBatchSucceededResult
+
+# Import shared fixtures and constants from conftest
+from conftest import (
+ CREATE_DELAY_SQLITE,
+ DEFAULT_EMBEDDING_CONFIG,
+ USING_SQLITE,
+)
+from openai.types.chat.chat_completion_message_tool_call import ChatCompletionMessageToolCall as OpenAIToolCall, Function as OpenAIFunction
+from sqlalchemy import func, select
+from sqlalchemy.exc import IntegrityError, InvalidRequestError
+from sqlalchemy.orm.exc import StaleDataError
+
+from letta.config import LettaConfig
+from letta.constants import (
+ BASE_MEMORY_TOOLS,
+ BASE_SLEEPTIME_TOOLS,
+ BASE_TOOLS,
+ BASE_VOICE_SLEEPTIME_CHAT_TOOLS,
+ BASE_VOICE_SLEEPTIME_TOOLS,
+ BUILTIN_TOOLS,
+ DEFAULT_ORG_ID,
+ DEFAULT_ORG_NAME,
+ FILES_TOOLS,
+ LETTA_TOOL_EXECUTION_DIR,
+ LETTA_TOOL_SET,
+ LOCAL_ONLY_MULTI_AGENT_TOOLS,
+ MCP_TOOL_TAG_NAME_PREFIX,
+ MULTI_AGENT_TOOLS,
+)
+from letta.data_sources.redis_client import NoopAsyncRedisClient, get_redis_client
+from letta.errors import LettaAgentNotFoundError
+from letta.functions.functions import derive_openai_json_schema, parse_source_code
+from letta.functions.mcp_client.types import MCPTool
+from letta.helpers import ToolRulesSolver
+from letta.helpers.datetime_helpers import AsyncTimer
+from letta.jobs.types import ItemUpdateInfo, RequestStatusUpdateInfo, StepStatusUpdateInfo
+from letta.orm import Base, Block
+from letta.orm.block_history import BlockHistory
+from letta.orm.errors import NoResultFound, UniqueConstraintViolationError
+from letta.orm.file import FileContent as FileContentModel, FileMetadata as FileMetadataModel
+from letta.schemas.agent import CreateAgent, UpdateAgent
+from letta.schemas.block import Block as PydanticBlock, BlockUpdate, CreateBlock
+from letta.schemas.embedding_config import EmbeddingConfig
+from letta.schemas.enums import (
+ ActorType,
+ AgentStepStatus,
+ FileProcessingStatus,
+ JobStatus,
+ JobType,
+ MessageRole,
+ ProviderType,
+ SandboxType,
+ StepStatus,
+ TagMatchMode,
+ ToolType,
+ VectorDBProvider,
+)
+from letta.schemas.environment_variables import SandboxEnvironmentVariableCreate, SandboxEnvironmentVariableUpdate
+from letta.schemas.file import FileMetadata, FileMetadata as PydanticFileMetadata
+from letta.schemas.identity import IdentityCreate, IdentityProperty, IdentityPropertyType, IdentityType, IdentityUpdate, IdentityUpsert
+from letta.schemas.job import BatchJob, Job, Job as PydanticJob, JobUpdate, LettaRequestConfig
+from letta.schemas.letta_message import UpdateAssistantMessage, UpdateReasoningMessage, UpdateSystemMessage, UpdateUserMessage
+from letta.schemas.letta_message_content import TextContent
+from letta.schemas.letta_stop_reason import LettaStopReason, StopReasonType
+from letta.schemas.llm_batch_job import AgentStepState, LLMBatchItem
+from letta.schemas.llm_config import LLMConfig
+from letta.schemas.message import Message as PydanticMessage, MessageCreate, MessageUpdate
+from letta.schemas.openai.chat_completion_response import UsageStatistics
+from letta.schemas.organization import Organization, Organization as PydanticOrganization, OrganizationUpdate
+from letta.schemas.passage import Passage as PydanticPassage
+from letta.schemas.pip_requirement import PipRequirement
+from letta.schemas.run import Run as PydanticRun
+from letta.schemas.sandbox_config import E2BSandboxConfig, LocalSandboxConfig, SandboxConfigCreate, SandboxConfigUpdate
+from letta.schemas.source import Source as PydanticSource, SourceUpdate
+from letta.schemas.tool import Tool as PydanticTool, ToolCreate, ToolUpdate
+from letta.schemas.tool_rule import InitToolRule
+from letta.schemas.user import User as PydanticUser, UserUpdate
+from letta.server.db import db_registry
+from letta.server.server import SyncServer
+from letta.services.block_manager import BlockManager
+from letta.services.helpers.agent_manager_helper import calculate_base_tools, calculate_multi_agent_tools, validate_agent_exists_async
+from letta.services.step_manager import FeedbackType
+from letta.settings import settings, tool_settings
+from letta.utils import calculate_file_defaults_based_on_context_window
+from tests.helpers.utils import comprehensive_agent_checks, validate_context_window_overview
+from tests.utils import random_string
+
+
+# ======================================================================================================================
+# User Manager Tests
+# ======================================================================================================================
+async def test_list_users(server: SyncServer):
+ # Create default organization
+ org = await server.organization_manager.create_default_organization_async()
+
+ user_name = "user"
+ user = await server.user_manager.create_actor_async(PydanticUser(name=user_name, organization_id=org.id))
+
+ users = await server.user_manager.list_actors_async()
+ assert len(users) == 1
+ assert users[0].name == user_name
+
+ # Delete it after
+ await server.user_manager.delete_actor_by_id_async(user.id)
+ assert len(await server.user_manager.list_actors_async()) == 0
+
+
+async def test_create_default_user(server: SyncServer):
+ org = await server.organization_manager.create_default_organization_async()
+ await server.user_manager.create_default_actor_async(org_id=org.id)
+ retrieved = await server.user_manager.get_default_actor_async()
+ assert retrieved.name == server.user_manager.DEFAULT_USER_NAME
+
+
+async def test_update_user(server: SyncServer):
+ # Create default organization
+ default_org = await server.organization_manager.create_default_organization_async()
+ test_org = await server.organization_manager.create_organization_async(PydanticOrganization(name="test_org"))
+
+ user_name_a = "a"
+ user_name_b = "b"
+
+ # Assert it's been created
+ user = await server.user_manager.create_actor_async(PydanticUser(name=user_name_a, organization_id=default_org.id))
+ assert user.name == user_name_a
+
+ # Adjust name
+ user = await server.user_manager.update_actor_async(UserUpdate(id=user.id, name=user_name_b))
+ assert user.name == user_name_b
+ assert user.organization_id == DEFAULT_ORG_ID
+
+ # Adjust org id
+ user = await server.user_manager.update_actor_async(UserUpdate(id=user.id, organization_id=test_org.id))
+ assert user.name == user_name_b
+ assert user.organization_id == test_org.id
+
+
+async def test_user_caching(server: SyncServer, default_user, performance_pct=0.4):
+ if isinstance(await get_redis_client(), NoopAsyncRedisClient):
+ pytest.skip("redis not available")
+ # Invalidate previous cache behavior.
+ await server.user_manager._invalidate_actor_cache(default_user.id)
+ before_stats = server.user_manager.get_actor_by_id_async.cache_stats
+ before_cache_misses = before_stats.misses
+ before_cache_hits = before_stats.hits
+
+ # First call (expected to miss the cache)
+ async with AsyncTimer() as timer:
+ actor = await server.user_manager.get_actor_by_id_async(default_user.id)
+ duration_first = timer.elapsed_ns
+ print(f"Call 1: {duration_first:.2e}ns")
+ assert actor.id == default_user.id
+ assert duration_first > 0 # Sanity check: took non-zero time
+ cached_hits = 10
+ durations = []
+ for i in range(cached_hits):
+ async with AsyncTimer() as timer:
+ actor_cached = await server.user_manager.get_actor_by_id_async(default_user.id)
+ duration = timer.elapsed_ns
+ durations.append(duration)
+ print(f"Call {i + 2}: {duration:.2e}ns")
+ assert actor_cached == actor
+ for d in durations:
+ assert d < duration_first * performance_pct
+ stats = server.user_manager.get_actor_by_id_async.cache_stats
+
+ print(f"Before calls: {before_stats}")
+ print(f"After calls: {stats}")
+ # Assert cache stats
+ assert stats.misses - before_cache_misses == 1
+ assert stats.hits - before_cache_hits == cached_hits
diff --git a/tests/test_managers.py b/tests/test_managers.py
index 9c66d7096..9ebd44b97 100644
--- a/tests/test_managers.py
+++ b/tests/test_managers.py
@@ -137,21 +137,21 @@ async def _clear_tables(async_session):
@pytest.fixture
async def default_organization(server: SyncServer):
"""Fixture to create and return the default organization."""
- org = server.organization_manager.create_default_organization()
+ org = await server.organization_manager.create_default_organization_async()
yield org
@pytest.fixture
async def other_organization(server: SyncServer):
"""Fixture to create and return the default organization."""
- org = server.organization_manager.create_organization(pydantic_org=Organization(name="letta"))
+ org = await server.organization_manager.create_organization_async(pydantic_org=Organization(name="letta"))
yield org
@pytest.fixture
-def default_user(server: SyncServer, default_organization):
+async def default_user(server: SyncServer, default_organization):
"""Fixture to create and return the default user within the default organization."""
- user = server.user_manager.create_default_user(org_id=default_organization.id)
+ user = await server.user_manager.create_default_actor_async(org_id=default_organization.id)
yield user
@@ -169,8 +169,16 @@ async def other_user_different_org(server: SyncServer, other_organization):
yield user
+async def other_user_different_org(server: SyncServer, other_organization):
+ """Fixture to create and return the default user within the default organization."""
+ user = await server.user_manager.create_actor_async(PydanticUser(name="other", organization_id=other_organization.id))
+ yield user
+
+
@pytest.fixture
async def default_source(server: SyncServer, default_user):
+ """Fixture to create and return the default source."""
source_pydantic = PydanticSource(
name="Test Source",
description="This is a test source.",
@@ -183,18 +191,20 @@ async def default_source(server: SyncServer, default_user):
@pytest.fixture
async def other_source(server: SyncServer, default_user):
+ """Fixture to create and return another source."""
source_pydantic = PydanticSource(
name="Another Test Source",
description="This is yet another test source.",
metadata={"type": "another_test"},
embedding_config=DEFAULT_EMBEDDING_CONFIG,
)
- source = await server.source_manager.create_source(source=source_pydantic, actor=default_user)
+ source = await server.source_manager.create_source_async(source=source_pydantic, actor=default_user)
yield source
@pytest.fixture
async def default_file(server: SyncServer, default_source, default_user, default_organization):
+ """Fixture to create and return the default file."""
file = await server.file_manager.create_file(
PydanticFileMetadata(file_name="test_file", organization_id=default_organization.id, source_id=default_source.id),
actor=default_user,
@@ -204,6 +214,7 @@ async def default_file(server: SyncServer, default_source, default_user, default
@pytest.fixture
async def print_tool(server: SyncServer, default_user, default_organization):
+ """Fixture to create and return a tool with default settings and clean up after the test."""
"""Fixture to create a tool with default settings and clean up after the test."""
def print_tool(message: str):
@@ -239,7 +250,7 @@ async def print_tool(server: SyncServer, default_user, default_organization):
@pytest.fixture
async def bash_tool(server: SyncServer, default_user, default_organization):
- """Fixture to create a bash tool with requires_approval and clean up after the test."""
+ """Fixture to create and return a bash tool with requires_approval and clean up after the test."""
def bash_tool(operation: str):
"""
@@ -273,13 +284,6 @@ async def bash_tool(server: SyncServer, default_user, default_organization):
yield tool
-def composio_github_star_tool(server, default_user):
- tool_create = ToolCreate.from_composio(action_name="GITHUB_STAR_A_REPOSITORY_FOR_THE_AUTHENTICATED_USER")
- tool = server.tool_manager.create_or_update_composio_tool(tool_create=tool_create, actor=default_user)
- yield tool
-
-
@pytest.fixture
def mcp_tool(server, default_user):
mcp_tool = MCPTool(
@@ -330,14 +334,14 @@ async def default_run(server: SyncServer, default_user):
@pytest.fixture
-def agent_passage_fixture(server: SyncServer, default_user, sarah_agent):
+async def agent_passage_fixture(server: SyncServer, default_user, sarah_agent):
"""Fixture to create an agent passage."""
# Get or create default archive for the agent
- archive = server.archive_manager.get_or_create_default_archive_for_agent(
+ archive = await server.archive_manager.get_or_create_default_archive_for_agent(
agent_id=sarah_agent.id, agent_name=sarah_agent.name, actor=default_user
)
- passage = server.passage_manager.create_agent_passage(
+ passage = await server.passage_manager.create_agent_passage(
PydanticPassage(
text="Hello, I am an agent passage",
archive_id=archive.id,
@@ -352,9 +356,9 @@ def agent_passage_fixture(server: SyncServer, default_user, sarah_agent):
@pytest.fixture
-def source_passage_fixture(server: SyncServer, default_user, default_file, default_source):
+async def source_passage_fixture(server: SyncServer, default_user, default_file, default_source):
"""Fixture to create a source passage."""
- passage = server.passage_manager.create_source_passage(
+ passage = await server.passage_manager.create_source_passage(
PydanticPassage(
text="Hello, I am a source passage",
source_id=default_source.id,
@@ -371,17 +375,17 @@ def source_passage_fixture(server: SyncServer, default_user, default_file, defau
@pytest.fixture
-def create_test_passages(server: SyncServer, default_file, default_user, sarah_agent, default_source):
+async def create_test_passages(server: SyncServer, default_file, default_user, sarah_agent, default_source):
"""Helper function to create test passages for all tests."""
# Get or create default archive for the agent
- archive = server.archive_manager.get_or_create_default_archive_for_agent(
+ archive = await server.archive_manager.get_or_create_default_archive_for_agent(
agent_id=sarah_agent.id, agent_name=sarah_agent.name, actor=default_user
)
# Create agent passages
passages = []
for i in range(5):
- passage = server.passage_manager.create_agent_passage(
+ passage = await server.passage_manager.create_agent_passage(
PydanticPassage(
text=f"Agent passage {i}",
archive_id=archive.id,
@@ -398,7 +402,7 @@ def create_test_passages(server: SyncServer, default_file, default_user, sarah_a
# Create source passages
for i in range(5):
- passage = server.passage_manager.create_source_passage(
+ passage = await server.passage_manager.create_source_passage(
PydanticPassage(
text=f"Source passage {i}",
source_id=default_source.id,
@@ -419,7 +423,7 @@ def create_test_passages(server: SyncServer, default_file, default_user, sarah_a
@pytest.fixture
-def hello_world_message_fixture(server: SyncServer, default_user, sarah_agent):
+async def hello_world_message_fixture(server: SyncServer, default_user, sarah_agent):
"""Fixture to create a tool with default settings and clean up after the test."""
# Set up message
message = PydanticMessage(
@@ -428,34 +432,34 @@ def hello_world_message_fixture(server: SyncServer, default_user, sarah_agent):
content=[TextContent(text="Hello, world!")],
)
- msg = server.message_manager.create_message(message, actor=default_user)
+ msg = await server.message_manager.create_message(message, actor=default_user)
yield msg
@pytest.fixture
-def sandbox_config_fixture(server: SyncServer, default_user):
+async def sandbox_config_fixture(server: SyncServer, default_user):
sandbox_config_create = SandboxConfigCreate(
config=E2BSandboxConfig(),
)
- created_config = server.sandbox_config_manager.create_or_update_sandbox_config(sandbox_config_create, actor=default_user)
+ created_config = await server.sandbox_config_manager.create_or_update_sandbox_config_async(sandbox_config_create, actor=default_user)
yield created_config
@pytest.fixture
-def sandbox_env_var_fixture(server: SyncServer, sandbox_config_fixture, default_user):
+async def sandbox_env_var_fixture(server: SyncServer, sandbox_config_fixture, default_user):
env_var_create = SandboxEnvironmentVariableCreate(
key="SAMPLE_VAR",
value="sample_value",
description="A sample environment variable for testing.",
)
- created_env_var = server.sandbox_config_manager.create_sandbox_env_var(
+ created_env_var = await server.sandbox_config_manager.create_sandbox_env_var_async(
env_var_create, sandbox_config_id=sandbox_config_fixture.id, actor=default_user
)
yield created_env_var
@pytest.fixture
-def default_block(server: SyncServer, default_user):
+async def default_block(server: SyncServer, default_user):
"""Fixture to create and return a default block."""
block_manager = BlockManager()
block_data = PydanticBlock(
@@ -465,12 +469,12 @@ def default_block(server: SyncServer, default_user):
limit=1000,
metadata={"type": "test"},
)
- block = block_manager.create_or_update_block(block_data, actor=default_user)
+ block = await block_manager.create_or_update_block_async(block_data, actor=default_user)
yield block
@pytest.fixture
-def other_block(server: SyncServer, default_user):
+async def other_block(server: SyncServer, default_user):
"""Fixture to create and return another block."""
block_manager = BlockManager()
block_data = PydanticBlock(
@@ -480,12 +484,14 @@ def other_block(server: SyncServer, default_user):
limit=500,
metadata={"type": "test"},
)
- block = block_manager.create_or_update_block(block_data, actor=default_user)
+ block = await block_manager.create_or_update_block_async(block_data, actor=default_user)
yield block
@pytest.fixture
async def other_tool(server: SyncServer, default_user, default_organization):
+ """Fixture to create and return another tool."""
+
def print_other_tool(message: str):
"""
Args:
@@ -787,7 +793,7 @@ async def test_create_get_list_agent(server: SyncServer, comprehensive_test_agen
comprehensive_agent_checks(list_agents[0], create_agent_request, actor=default_user)
# Test deleting the agent
- server.agent_manager.delete_agent(get_agent.id, default_user)
+ await server.agent_manager.delete_agent(get_agent.id, default_user)
list_agents = await server.agent_manager.list_agents_async(actor=default_user)
assert len(list_agents) == 0
@@ -884,7 +890,8 @@ async def test_create_agent_base_tool_rules_non_excluded_providers(server: SyncS
assert len(created_agent.tool_rules) > 0
-def test_calculate_multi_agent_tools(set_letta_environment):
+async def test_calculate_multi_agent_tools(set_letta_environment):
"""Test that calculate_multi_agent_tools excludes local-only tools in production."""
result = calculate_multi_agent_tools()
@@ -1013,8 +1020,8 @@ async def test_create_agent_with_default_source(server: SyncServer, default_user
assert len(attached_sources_no_source) == 0
# Clean up
- server.agent_manager.delete_agent(created_agent.id, default_user)
- server.agent_manager.delete_agent(created_agent_no_source.id, default_user)
+ await server.agent_manager.delete_agent(created_agent.id, default_user)
+ await server.agent_manager.delete_agent(created_agent_no_source.id, default_user)
@pytest.fixture(params=[None, "PRODUCTION"])
@@ -1049,7 +1056,7 @@ async def test_get_context_window_basic(
validate_context_window_overview(created_agent, context_window_overview, assoc)
# Test deleting the agent
- server.agent_manager.delete_agent(created_agent.id, default_user)
+ await server.agent_manager.delete_agent(created_agent.id, default_user)
list_agents = await server.agent_manager.list_agents_async(actor=default_user)
assert len(list_agents) == 0
@@ -1134,7 +1141,7 @@ async def test_create_agent_with_json_in_system_message(server: SyncServer, defa
system_message = await server.message_manager.get_message_by_id_async(message_id=system_message_id, actor=default_user)
assert system_prompt in system_message.content[0].text
assert default_block.value in system_message.content[0].text
- server.agent_manager.delete_agent(agent_id=agent_state.id, actor=default_user)
+ await server.agent_manager.delete_agent(agent_id=agent_state.id, actor=default_user)
async def test_update_agent(server: SyncServer, comprehensive_test_agent_fixture, other_tool, other_source, other_block, default_user):
@@ -1185,7 +1192,7 @@ async def test_agent_file_defaults_based_on_context_window(server: SyncServer, d
assert (
agent_state.per_file_view_window_char_limit == calculate_file_defaults_based_on_context_window(llm_config_small.context_window)[1]
)
- server.agent_manager.delete_agent(agent_id=agent_state.id, actor=default_user)
+ await server.agent_manager.delete_agent(agent_id=agent_state.id, actor=default_user)
# test with medium context window model (32k)
llm_config_medium = LLMConfig.default_config("gpt-4o-mini")
@@ -1205,7 +1212,7 @@ async def test_agent_file_defaults_based_on_context_window(server: SyncServer, d
assert (
agent_state.per_file_view_window_char_limit == calculate_file_defaults_based_on_context_window(llm_config_medium.context_window)[1]
)
- server.agent_manager.delete_agent(agent_id=agent_state.id, actor=default_user)
+ await server.agent_manager.delete_agent(agent_id=agent_state.id, actor=default_user)
# test with large context window model (128k)
llm_config_large = LLMConfig.default_config("gpt-4o-mini")
@@ -1225,7 +1232,7 @@ async def test_agent_file_defaults_based_on_context_window(server: SyncServer, d
assert (
agent_state.per_file_view_window_char_limit == calculate_file_defaults_based_on_context_window(llm_config_large.context_window)[1]
)
- server.agent_manager.delete_agent(agent_id=agent_state.id, actor=default_user)
+ await server.agent_manager.delete_agent(agent_id=agent_state.id, actor=default_user)
@pytest.mark.asyncio
@@ -1250,7 +1257,7 @@ async def test_agent_file_defaults_explicit_values(server: SyncServer, default_u
# verify explicit values are used instead of defaults
assert agent_state.max_files_open == 20
assert agent_state.per_file_view_window_char_limit == 500_000
- server.agent_manager.delete_agent(agent_id=agent_state.id, actor=default_user)
+ await server.agent_manager.delete_agent(agent_id=agent_state.id, actor=default_user)
@pytest.mark.asyncio
@@ -2421,7 +2428,7 @@ async def test_reset_messages_with_existing_messages(server: SyncServer, sarah_a
deletes them from the database and clears message_ids.
"""
# 1. Create multiple messages for the agent
- msg1 = server.message_manager.create_message(
+ msg1 = await server.message_manager.create_message(
PydanticMessage(
agent_id=sarah_agent.id,
role="user",
@@ -2429,7 +2436,7 @@ async def test_reset_messages_with_existing_messages(server: SyncServer, sarah_a
),
actor=default_user,
)
- msg2 = server.message_manager.create_message(
+ msg2 = await server.message_manager.create_message(
PydanticMessage(
agent_id=sarah_agent.id,
role="assistant",
@@ -2463,7 +2470,7 @@ async def test_reset_messages_idempotency(server: SyncServer, sarah_agent, defau
await server.message_manager.delete_messages_by_ids_async(message_ids=sarah_agent.message_ids[1:], actor=default_user)
# Create a single message
- server.message_manager.create_message(
+ await server.message_manager.create_message(
PydanticMessage(
agent_id=sarah_agent.id,
role="user",
@@ -2492,7 +2499,7 @@ async def test_reset_messages_preserves_system_message_id(server: SyncServer, sa
original_system_message_id = original_agent.message_ids[0]
# Add some user messages
- server.message_manager.create_message(
+ await server.message_manager.create_message(
PydanticMessage(
agent_id=sarah_agent.id,
role="user",
@@ -2525,7 +2532,7 @@ async def test_reset_messages_preserves_system_message_content(server: SyncServe
)
# Add some messages and reset
- server.message_manager.create_message(
+ await server.message_manager.create_message(
PydanticMessage(
agent_id=sarah_agent.id,
role="user",
@@ -2623,7 +2630,7 @@ async def test_modify_letta_message(server: SyncServer, sarah_agent, default_use
async def test_attach_block(server: SyncServer, sarah_agent, default_block, default_user):
"""Test attaching a block to an agent."""
# Attach block
- server.agent_manager.attach_block(agent_id=sarah_agent.id, block_id=default_block.id, actor=default_user)
+ await server.agent_manager.attach_block(agent_id=sarah_agent.id, block_id=default_block.id, actor=default_user)
# Verify attachment
agent = await server.agent_manager.get_agent_by_id_async(sarah_agent.id, actor=default_user)
@@ -2633,53 +2640,55 @@ async def test_attach_block(server: SyncServer, sarah_agent, default_block, defa
# Test should work with both SQLite and PostgreSQL
-def test_attach_block_duplicate_label(server: SyncServer, sarah_agent, default_block, other_block, default_user):
+async def test_attach_block_duplicate_label(server: SyncServer, sarah_agent, default_block, other_block, default_user):
"""Test attempting to attach a block with a duplicate label."""
# Set up both blocks with same label
- server.block_manager.update_block(default_block.id, BlockUpdate(label="same_label"), actor=default_user)
- server.block_manager.update_block(other_block.id, BlockUpdate(label="same_label"), actor=default_user)
+ await server.block_manager.update_block(default_block.id, BlockUpdate(label="same_label"), actor=default_user)
+ await server.block_manager.update_block(other_block.id, BlockUpdate(label="same_label"), actor=default_user)
# Attach first block
- server.agent_manager.attach_block(agent_id=sarah_agent.id, block_id=default_block.id, actor=default_user)
+ await server.agent_manager.attach_block(agent_id=sarah_agent.id, block_id=default_block.id, actor=default_user)
# Attempt to attach second block with same label
with pytest.raises(IntegrityError):
- server.agent_manager.attach_block(agent_id=sarah_agent.id, block_id=other_block.id, actor=default_user)
+ await server.agent_manager.attach_block(agent_id=sarah_agent.id, block_id=other_block.id, actor=default_user)
@pytest.mark.asyncio
async def test_detach_block(server: SyncServer, sarah_agent, default_block, default_user):
"""Test detaching a block by ID."""
# Set up: attach block
- server.agent_manager.attach_block(agent_id=sarah_agent.id, block_id=default_block.id, actor=default_user)
+ await server.agent_manager.attach_block(agent_id=sarah_agent.id, block_id=default_block.id, actor=default_user)
# Detach block
- server.agent_manager.detach_block(agent_id=sarah_agent.id, block_id=default_block.id, actor=default_user)
+ await server.agent_manager.detach_block(agent_id=sarah_agent.id, block_id=default_block.id, actor=default_user)
# Verify detachment
agent = await server.agent_manager.get_agent_by_id_async(sarah_agent.id, actor=default_user)
assert len(agent.memory.blocks) == 0
# Check that block still exists
- block = server.block_manager.get_block_by_id(block_id=default_block.id, actor=default_user)
+ block = await server.block_manager.get_block_by_id(block_id=default_block.id, actor=default_user)
assert block
-def test_detach_nonexistent_block(server: SyncServer, sarah_agent, default_user):
+async def test_detach_nonexistent_block(server: SyncServer, sarah_agent, default_user):
"""Test detaching a block that isn't attached."""
with pytest.raises(NoResultFound):
- server.agent_manager.detach_block(agent_id=sarah_agent.id, block_id="nonexistent-block-id", actor=default_user)
+ await server.agent_manager.detach_block(agent_id=sarah_agent.id, block_id="nonexistent-block-id", actor=default_user)
@pytest.mark.asyncio
async def test_update_block_label(server: SyncServer, sarah_agent, default_block, default_user):
"""Test updating a block's label updates the relationship."""
# Attach block
- server.agent_manager.attach_block(agent_id=sarah_agent.id, block_id=default_block.id, actor=default_user)
+ await server.agent_manager.attach_block(agent_id=sarah_agent.id, block_id=default_block.id, actor=default_user)
# Update block label
new_label = "new_label"
- server.block_manager.update_block(default_block.id, BlockUpdate(label=new_label), actor=default_user)
+ await server.block_manager.update_block(default_block.id, BlockUpdate(label=new_label), actor=default_user)
# Verify relationship is updated
agent = await server.agent_manager.get_agent_by_id_async(sarah_agent.id, actor=default_user)
@@ -2692,12 +2701,12 @@ async def test_update_block_label(server: SyncServer, sarah_agent, default_block
async def test_update_block_label_multiple_agents(server: SyncServer, sarah_agent, charles_agent, default_block, default_user):
"""Test updating a block's label updates relationships for all agents."""
# Attach block to both agents
- server.agent_manager.attach_block(agent_id=sarah_agent.id, block_id=default_block.id, actor=default_user)
- server.agent_manager.attach_block(agent_id=charles_agent.id, block_id=default_block.id, actor=default_user)
+ await server.agent_manager.attach_block(agent_id=sarah_agent.id, block_id=default_block.id, actor=default_user)
+ await server.agent_manager.attach_block(agent_id=charles_agent.id, block_id=default_block.id, actor=default_user)
# Update block label
new_label = "new_label"
- server.block_manager.update_block(default_block.id, BlockUpdate(label=new_label), actor=default_user)
+ await server.block_manager.update_block(default_block.id, BlockUpdate(label=new_label), actor=default_user)
# Verify both relationships are updated
for agent_id in [sarah_agent.id, charles_agent.id]:
@@ -2707,10 +2716,11 @@ async def test_update_block_label_multiple_agents(server: SyncServer, sarah_agen
assert block.label == new_label
-def test_get_block_with_label(server: SyncServer, sarah_agent, default_block, default_user):
+async def test_get_block_with_label(server: SyncServer, sarah_agent, default_block, default_user):
"""Test retrieving a block by its label."""
# Attach block
- server.agent_manager.attach_block(agent_id=sarah_agent.id, block_id=default_block.id, actor=default_user)
+ await server.agent_manager.attach_block(agent_id=sarah_agent.id, block_id=default_block.id, actor=default_user)
# Get block by label
block = server.agent_manager.get_block_with_label(agent_id=sarah_agent.id, block_label=default_block.label, actor=default_user)
@@ -2747,7 +2757,7 @@ async def test_refresh_memory_async(server: SyncServer, default_user):
),
actor=default_user,
)
- block = server.block_manager.update_block(
+ block = await server.block_manager.update_block(
block_id=block.id,
block_update=BlockUpdate(
value="test2",
@@ -3061,13 +3071,14 @@ async def test_list_organizations_pagination(server: SyncServer):
# ======================================================================================================================
-def test_passage_create_agentic(server: SyncServer, agent_passage_fixture, default_user):
+async def test_passage_create_agentic(server: SyncServer, agent_passage_fixture, default_user):
"""Test creating a passage using agent_passage_fixture fixture"""
assert agent_passage_fixture.id is not None
assert agent_passage_fixture.text == "Hello, I am an agent passage"
# Verify we can retrieve it
- retrieved = server.passage_manager.get_passage_by_id(
+ retrieved = await server.passage_manager.get_passage_by_id(
agent_passage_fixture.id,
actor=default_user,
)
@@ -3076,13 +3087,14 @@ def test_passage_create_agentic(server: SyncServer, agent_passage_fixture, defau
assert retrieved.text == agent_passage_fixture.text
-def test_passage_create_source(server: SyncServer, source_passage_fixture, default_user):
+async def test_passage_create_source(server: SyncServer, source_passage_fixture, default_user):
"""Test creating a source passage."""
assert source_passage_fixture is not None
assert source_passage_fixture.text == "Hello, I am a source passage"
# Verify we can retrieve it
- retrieved = server.passage_manager.get_passage_by_id(
+ retrieved = await server.passage_manager.get_passage_by_id(
source_passage_fixture.id,
actor=default_user,
)
@@ -3112,43 +3124,46 @@ async def test_passage_create_invalid(server: SyncServer, agent_passage_fixture,
)
-def test_passage_get_by_id(server: SyncServer, agent_passage_fixture, source_passage_fixture, default_user):
+async def test_passage_get_by_id(server: SyncServer, agent_passage_fixture, source_passage_fixture, default_user):
"""Test retrieving a passage by ID"""
- retrieved = server.passage_manager.get_passage_by_id(agent_passage_fixture.id, actor=default_user)
+ retrieved = await server.passage_manager.get_passage_by_id(agent_passage_fixture.id, actor=default_user)
assert retrieved is not None
assert retrieved.id == agent_passage_fixture.id
assert retrieved.text == agent_passage_fixture.text
- retrieved = server.passage_manager.get_passage_by_id(source_passage_fixture.id, actor=default_user)
+ retrieved = await server.passage_manager.get_passage_by_id(source_passage_fixture.id, actor=default_user)
assert retrieved is not None
assert retrieved.id == source_passage_fixture.id
assert retrieved.text == source_passage_fixture.text
async def test_passage_cascade_deletion(
server: SyncServer, agent_passage_fixture, source_passage_fixture, default_user, default_source, sarah_agent
):
"""Test that passages are deleted when their parent (agent or source) is deleted."""
# Verify passages exist
- agent_passage = server.passage_manager.get_passage_by_id(agent_passage_fixture.id, default_user)
- source_passage = server.passage_manager.get_passage_by_id(source_passage_fixture.id, default_user)
+ agent_passage = await server.passage_manager.get_passage_by_id(agent_passage_fixture.id, default_user)
+ source_passage = await server.passage_manager.get_passage_by_id(source_passage_fixture.id, default_user)
assert agent_passage is not None
assert source_passage is not None
# Delete agent and verify its passages are deleted
- server.agent_manager.delete_agent(sarah_agent.id, default_user)
+ await server.agent_manager.delete_agent(sarah_agent.id, default_user)
agentic_passages = await server.agent_manager.list_passages_async(actor=default_user, agent_id=sarah_agent.id, agent_only=True)
assert len(agentic_passages) == 0
-def test_create_agent_passage_specific(server: SyncServer, default_user, sarah_agent):
+async def test_create_agent_passage_specific(server: SyncServer, default_user, sarah_agent):
"""Test creating an agent passage using the new agent-specific method."""
# Get or create default archive for the agent
- archive = server.archive_manager.get_or_create_default_archive_for_agent(
+ archive = await server.archive_manager.get_or_create_default_archive_for_agent(
agent_id=sarah_agent.id, agent_name=sarah_agent.name, actor=default_user
)
- passage = server.passage_manager.create_agent_passage(
+ passage = await server.passage_manager.create_agent_passage(
PydanticPassage(
text="Test agent passage via specific method",
archive_id=archive.id,
@@ -3168,9 +3183,10 @@ def test_create_agent_passage_specific(server: SyncServer, default_user, sarah_a
assert sorted(passage.tags) == sorted(["python", "test", "agent"])
-def test_create_source_passage_specific(server: SyncServer, default_user, default_file, default_source):
+async def test_create_source_passage_specific(server: SyncServer, default_user, default_file, default_source):
"""Test creating a source passage using the new source-specific method."""
- passage = server.passage_manager.create_source_passage(
+ passage = await server.passage_manager.create_source_passage(
PydanticPassage(
text="Test source passage via specific method",
source_id=default_source.id,
@@ -3192,11 +3208,12 @@ def test_create_source_passage_specific(server: SyncServer, default_user, defaul
assert sorted(passage.tags) == sorted(["document", "test", "source"])
-def test_create_agent_passage_validation(server: SyncServer, default_user, default_source, sarah_agent):
+async def test_create_agent_passage_validation(server: SyncServer, default_user, default_source, sarah_agent):
"""Test that agent passage creation validates inputs correctly."""
# Should fail if archive_id is missing
with pytest.raises(ValueError, match="Agent passage must have archive_id"):
- server.passage_manager.create_agent_passage(
+ await server.passage_manager.create_agent_passage(
PydanticPassage(
text="Invalid agent passage",
organization_id=default_user.organization_id,
@@ -3207,13 +3224,13 @@ def test_create_agent_passage_validation(server: SyncServer, default_user, defau
)
# Get or create default archive for the agent
- archive = server.archive_manager.get_or_create_default_archive_for_agent(
+ archive = await server.archive_manager.get_or_create_default_archive_for_agent(
agent_id=sarah_agent.id, agent_name=sarah_agent.name, actor=default_user
)
# Should fail if source_id is present
with pytest.raises(ValueError, match="Agent passage cannot have source_id"):
- server.passage_manager.create_agent_passage(
+ await server.passage_manager.create_agent_passage(
PydanticPassage(
text="Invalid agent passage",
archive_id=archive.id,
@@ -3226,11 +3243,12 @@ def test_create_agent_passage_validation(server: SyncServer, default_user, defau
)
-def test_create_source_passage_validation(server: SyncServer, default_user, default_file, default_source, sarah_agent):
+async def test_create_source_passage_validation(server: SyncServer, default_user, default_file, default_source, sarah_agent):
"""Test that source passage creation validates inputs correctly."""
# Should fail if source_id is missing
with pytest.raises(ValueError, match="Source passage must have source_id"):
- server.passage_manager.create_source_passage(
+ await server.passage_manager.create_source_passage(
PydanticPassage(
text="Invalid source passage",
organization_id=default_user.organization_id,
@@ -3242,13 +3260,13 @@ def test_create_source_passage_validation(server: SyncServer, default_user, defa
)
# Get or create default archive for the agent
- archive = server.archive_manager.get_or_create_default_archive_for_agent(
+ archive = await server.archive_manager.get_or_create_default_archive_for_agent(
agent_id=sarah_agent.id, agent_name=sarah_agent.name, actor=default_user
)
# Should fail if archive_id is present
with pytest.raises(ValueError, match="Source passage cannot have archive_id"):
- server.passage_manager.create_source_passage(
+ await server.passage_manager.create_source_passage(
PydanticPassage(
text="Invalid source passage",
source_id=default_source.id,
@@ -3262,15 +3280,16 @@ def test_create_source_passage_validation(server: SyncServer, default_user, defa
)
-def test_get_agent_passage_by_id_specific(server: SyncServer, default_user, sarah_agent):
+async def test_get_agent_passage_by_id_specific(server: SyncServer, default_user, sarah_agent):
"""Test retrieving an agent passage using the new agent-specific method."""
# Get or create default archive for the agent
- archive = server.archive_manager.get_or_create_default_archive_for_agent(
+ archive = await server.archive_manager.get_or_create_default_archive_for_agent(
agent_id=sarah_agent.id, agent_name=sarah_agent.name, actor=default_user
)
# Create an agent passage
- passage = server.passage_manager.create_agent_passage(
+ passage = await server.passage_manager.create_agent_passage(
PydanticPassage(
text="Agent passage for retrieval test",
archive_id=archive.id,
@@ -3289,10 +3308,11 @@ def test_get_agent_passage_by_id_specific(server: SyncServer, default_user, sara
assert retrieved.archive_id == archive.id
-def test_get_source_passage_by_id_specific(server: SyncServer, default_user, default_file, default_source):
+async def test_get_source_passage_by_id_specific(server: SyncServer, default_user, default_file, default_source):
"""Test retrieving a source passage using the new source-specific method."""
# Create a source passage
- passage = server.passage_manager.create_source_passage(
+ passage = await server.passage_manager.create_source_passage(
PydanticPassage(
text="Source passage for retrieval test",
source_id=default_source.id,
@@ -3313,15 +3333,16 @@ def test_get_source_passage_by_id_specific(server: SyncServer, default_user, def
assert retrieved.source_id == default_source.id
-def test_get_wrong_passage_type_fails(server: SyncServer, default_user, sarah_agent, default_file, default_source):
+async def test_get_wrong_passage_type_fails(server: SyncServer, default_user, sarah_agent, default_file, default_source):
"""Test that trying to get the wrong passage type with specific methods fails."""
# Create an agent passage
# Get or create default archive for the agent
- archive = server.archive_manager.get_or_create_default_archive_for_agent(
+ archive = await server.archive_manager.get_or_create_default_archive_for_agent(
agent_id=sarah_agent.id, agent_name=sarah_agent.name, actor=default_user
)
- agent_passage = server.passage_manager.create_agent_passage(
+ agent_passage = await server.passage_manager.create_agent_passage(
PydanticPassage(
text="Agent passage",
archive_id=archive.id,
@@ -3333,7 +3354,7 @@ def test_get_wrong_passage_type_fails(server: SyncServer, default_user, sarah_ag
)
# Create a source passage
- source_passage = server.passage_manager.create_source_passage(
+ source_passage = await server.passage_manager.create_source_passage(
PydanticPassage(
text="Source passage",
source_id=default_source.id,
@@ -3355,15 +3376,16 @@ def test_get_wrong_passage_type_fails(server: SyncServer, default_user, sarah_ag
server.passage_manager.get_agent_passage_by_id(source_passage.id, actor=default_user)
-def test_update_agent_passage_specific(server: SyncServer, default_user, sarah_agent):
+async def test_update_agent_passage_specific(server: SyncServer, default_user, sarah_agent):
"""Test updating an agent passage using the new agent-specific method."""
# Get or create default archive for the agent
- archive = server.archive_manager.get_or_create_default_archive_for_agent(
+ archive = await server.archive_manager.get_or_create_default_archive_for_agent(
agent_id=sarah_agent.id, agent_name=sarah_agent.name, actor=default_user
)
# Create an agent passage
- passage = server.passage_manager.create_agent_passage(
+ passage = await server.passage_manager.create_agent_passage(
PydanticPassage(
text="Original agent passage text",
archive_id=archive.id,
@@ -3392,10 +3414,11 @@ def test_update_agent_passage_specific(server: SyncServer, default_user, sarah_a
assert updated_passage.id == passage.id
-def test_update_source_passage_specific(server: SyncServer, default_user, default_file, default_source):
+async def test_update_source_passage_specific(server: SyncServer, default_user, default_file, default_source):
"""Test updating a source passage using the new source-specific method."""
# Create a source passage
- passage = server.passage_manager.create_source_passage(
+ passage = await server.passage_manager.create_source_passage(
PydanticPassage(
text="Original source passage text",
source_id=default_source.id,
@@ -3427,15 +3450,16 @@ def test_update_source_passage_specific(server: SyncServer, default_user, defaul
assert updated_passage.id == passage.id
-def test_delete_agent_passage_specific(server: SyncServer, default_user, sarah_agent):
+async def test_delete_agent_passage_specific(server: SyncServer, default_user, sarah_agent):
"""Test deleting an agent passage using the new agent-specific method."""
# Get or create default archive for the agent
- archive = server.archive_manager.get_or_create_default_archive_for_agent(
+ archive = await server.archive_manager.get_or_create_default_archive_for_agent(
agent_id=sarah_agent.id, agent_name=sarah_agent.name, actor=default_user
)
# Create an agent passage
- passage = server.passage_manager.create_agent_passage(
+ passage = await server.passage_manager.create_agent_passage(
PydanticPassage(
text="Agent passage to delete",
archive_id=archive.id,
@@ -3459,10 +3483,11 @@ def test_delete_agent_passage_specific(server: SyncServer, default_user, sarah_a
server.passage_manager.get_agent_passage_by_id(passage.id, actor=default_user)
-def test_delete_source_passage_specific(server: SyncServer, default_user, default_file, default_source):
+async def test_delete_source_passage_specific(server: SyncServer, default_user, default_file, default_source):
"""Test deleting a source passage using the new source-specific method."""
# Create a source passage
- passage = server.passage_manager.create_source_passage(
+ passage = await server.passage_manager.create_source_passage(
PydanticPassage(
text="Source passage to delete",
source_id=default_source.id,
@@ -3545,18 +3570,19 @@ async def test_create_many_source_passages_async(server: SyncServer, default_use
assert passage.archive_id is None
-def test_agent_passage_size(server: SyncServer, default_user, sarah_agent):
+async def test_agent_passage_size(server: SyncServer, default_user, sarah_agent):
"""Test counting agent passages using the new agent-specific size method."""
initial_size = server.passage_manager.agent_passage_size(actor=default_user, agent_id=sarah_agent.id)
# Get or create default archive for the agent
- archive = server.archive_manager.get_or_create_default_archive_for_agent(
+ archive = await server.archive_manager.get_or_create_default_archive_for_agent(
agent_id=sarah_agent.id, agent_name=sarah_agent.name, actor=default_user
)
# Create some agent passages
for i in range(3):
- server.passage_manager.create_agent_passage(
+ await server.passage_manager.create_agent_passage(
PydanticPassage(
text=f"Agent passage {i} for size test",
archive_id=archive.id,
@@ -3571,12 +3597,13 @@ def test_agent_passage_size(server: SyncServer, default_user, sarah_agent):
assert final_size == initial_size + 3
-def test_deprecated_methods_show_warnings(server: SyncServer, default_user, sarah_agent):
+async def test_deprecated_methods_show_warnings(server: SyncServer, default_user, sarah_agent):
"""Test that deprecated methods show deprecation warnings."""
import warnings
# Get or create default archive for the agent
- archive = server.archive_manager.get_or_create_default_archive_for_agent(
+ archive = await server.archive_manager.get_or_create_default_archive_for_agent(
agent_id=sarah_agent.id, agent_name=sarah_agent.name, actor=default_user
)
@@ -3596,7 +3623,7 @@ def test_deprecated_methods_show_warnings(server: SyncServer, default_user, sara
)
# Test deprecated get_passage_by_id
- server.passage_manager.get_passage_by_id(passage.id, actor=default_user)
+ await server.passage_manager.get_passage_by_id(passage.id, actor=default_user)
# Test deprecated size
server.passage_manager.size(actor=default_user, agent_id=sarah_agent.id)
@@ -4395,19 +4422,15 @@ async def test_user_caching(server: SyncServer, default_user, performance_pct=0.
# ======================================================================================================================
-def test_create_tool(server: SyncServer, print_tool, default_user, default_organization):
+async def test_create_tool(server: SyncServer, print_tool, default_user, default_organization):
# Assertions to ensure the created tool matches the expected values
assert print_tool.created_by_id == default_user.id
assert print_tool.tool_type == ToolType.CUSTOM
-def test_create_composio_tool(server: SyncServer, composio_github_star_tool, default_user, default_organization):
- # Assertions to ensure the created tool matches the expected values
- assert composio_github_star_tool.created_by_id == default_user.id
- assert composio_github_star_tool.tool_type == ToolType.EXTERNAL_COMPOSIO
-
-
-def test_create_mcp_tool(server: SyncServer, mcp_tool, default_user, default_organization):
+async def test_create_mcp_tool(server: SyncServer, mcp_tool, default_user, default_organization):
# Assertions to ensure the created tool matches the expected values
assert mcp_tool.created_by_id == default_user.id
assert mcp_tool.tool_type == ToolType.EXTERNAL_MCP
@@ -4416,7 +4439,8 @@ def test_create_mcp_tool(server: SyncServer, mcp_tool, default_user, default_org
# Test should work with both SQLite and PostgreSQL
-def test_create_tool_duplicate_name(server: SyncServer, print_tool, default_user, default_organization):
+async def test_create_tool_duplicate_name(server: SyncServer, print_tool, default_user, default_organization):
data = print_tool.model_dump(exclude=["id"])
tool = PydanticTool(**data)
@@ -4424,14 +4448,16 @@ def test_create_tool_duplicate_name(server: SyncServer, print_tool, default_user
server.tool_manager.create_tool(tool, actor=default_user)
-def test_create_tool_requires_approval(server: SyncServer, bash_tool, default_user, default_organization):
+async def test_create_tool_requires_approval(server: SyncServer, bash_tool, default_user, default_organization):
# Assertions to ensure the created tool matches the expected values
assert bash_tool.created_by_id == default_user.id
assert bash_tool.tool_type == ToolType.CUSTOM
assert bash_tool.default_requires_approval == True
-def test_get_tool_by_id(server: SyncServer, print_tool, default_user):
+async def test_get_tool_by_id(server: SyncServer, print_tool, default_user):
# Fetch the tool by ID using the manager method
fetched_tool = server.tool_manager.get_tool_by_id(print_tool.id, actor=default_user)
@@ -4446,7 +4472,8 @@ def test_get_tool_by_id(server: SyncServer, print_tool, default_user):
assert fetched_tool.tool_type == ToolType.CUSTOM
-def test_get_tool_with_actor(server: SyncServer, print_tool, default_user):
+async def test_get_tool_with_actor(server: SyncServer, print_tool, default_user):
# Fetch the print_tool by name and organization ID
fetched_tool = server.tool_manager.get_tool_by_name(print_tool.name, actor=default_user)
@@ -4952,7 +4979,8 @@ async def test_count_tools_async(server: SyncServer, default_user):
assert count == 2 # only our custom tools
-def test_update_tool_by_id(server: SyncServer, print_tool, default_user):
+async def test_update_tool_by_id(server: SyncServer, print_tool, default_user):
updated_description = "updated_description"
return_char_limit = 10000
@@ -4976,7 +5004,8 @@ def test_update_tool_by_id(server: SyncServer, print_tool, default_user):
assert updated_tool.tool_type == ToolType.EXTERNAL_MCP
-def test_update_tool_source_code_refreshes_schema_and_name(server: SyncServer, print_tool, default_user):
+async def test_update_tool_source_code_refreshes_schema_and_name(server: SyncServer, print_tool, default_user):
def counter_tool(counter: int):
"""
Args:
@@ -5013,7 +5042,8 @@ def test_update_tool_source_code_refreshes_schema_and_name(server: SyncServer, p
assert updated_tool.tool_type == ToolType.CUSTOM
-def test_update_tool_source_code_refreshes_schema_only(server: SyncServer, print_tool, default_user):
+async def test_update_tool_source_code_refreshes_schema_only(server: SyncServer, print_tool, default_user):
def counter_tool(counter: int):
"""
Args:
@@ -5052,7 +5082,8 @@ def test_update_tool_source_code_refreshes_schema_only(server: SyncServer, print
assert updated_tool.tool_type == ToolType.CUSTOM
-def test_update_tool_multi_user(server: SyncServer, print_tool, default_user, other_user):
+async def test_update_tool_multi_user(server: SyncServer, print_tool, default_user, other_user):
updated_description = "updated_description"
# Create a ToolUpdate object to modify the print_tool's description
@@ -5672,14 +5703,15 @@ async def test_update_default_requires_approval(server: SyncServer, bash_tool, d
# ======================================================================================================================
-def test_message_create(server: SyncServer, hello_world_message_fixture, default_user):
+async def test_message_create(server: SyncServer, hello_world_message_fixture, default_user):
"""Test creating a message using hello_world_message_fixture fixture"""
assert hello_world_message_fixture.id is not None
assert hello_world_message_fixture.content[0].text == "Hello, world!"
assert hello_world_message_fixture.role == "user"
# Verify we can retrieve it
- retrieved = server.message_manager.get_message_by_id(
+ retrieved = await server.message_manager.get_message_by_id(
hello_world_message_fixture.id,
actor=default_user,
)
@@ -5689,21 +5721,23 @@ def test_message_create(server: SyncServer, hello_world_message_fixture, default
assert retrieved.role == hello_world_message_fixture.role
-def test_message_get_by_id(server: SyncServer, hello_world_message_fixture, default_user):
+async def test_message_get_by_id(server: SyncServer, hello_world_message_fixture, default_user):
"""Test retrieving a message by ID"""
- retrieved = server.message_manager.get_message_by_id(hello_world_message_fixture.id, actor=default_user)
+ retrieved = await server.message_manager.get_message_by_id(hello_world_message_fixture.id, actor=default_user)
assert retrieved is not None
assert retrieved.id == hello_world_message_fixture.id
assert retrieved.content[0].text == hello_world_message_fixture.content[0].text
-def test_message_update(server: SyncServer, hello_world_message_fixture, default_user, other_user):
+async def test_message_update(server: SyncServer, hello_world_message_fixture, default_user, other_user):
"""Test updating a message"""
new_text = "Updated text"
updated = server.message_manager.update_message_by_id(hello_world_message_fixture.id, MessageUpdate(content=new_text), actor=other_user)
assert updated is not None
assert updated.content[0].text == new_text
- retrieved = server.message_manager.get_message_by_id(hello_world_message_fixture.id, actor=default_user)
+ retrieved = await server.message_manager.get_message_by_id(hello_world_message_fixture.id, actor=default_user)
assert retrieved.content[0].text == new_text
# Assert that orm metadata fields are populated
@@ -5711,14 +5745,16 @@ def test_message_update(server: SyncServer, hello_world_message_fixture, default
assert retrieved.last_updated_by_id == other_user.id
-def test_message_delete(server: SyncServer, hello_world_message_fixture, default_user):
+async def test_message_delete(server: SyncServer, hello_world_message_fixture, default_user):
"""Test deleting a message"""
server.message_manager.delete_message_by_id(hello_world_message_fixture.id, actor=default_user)
- retrieved = server.message_manager.get_message_by_id(hello_world_message_fixture.id, actor=default_user)
+ retrieved = await server.message_manager.get_message_by_id(hello_world_message_fixture.id, actor=default_user)
assert retrieved is None
-def test_message_size(server: SyncServer, hello_world_message_fixture, default_user):
+async def test_message_size(server: SyncServer, hello_world_message_fixture, default_user):
"""Test counting messages with filters"""
base_message = hello_world_message_fixture
@@ -5765,7 +5801,8 @@ def create_test_messages(server: SyncServer, base_message: PydanticMessage, defa
return messages
-def test_get_messages_by_ids(server: SyncServer, hello_world_message_fixture, default_user, sarah_agent):
+async def test_get_messages_by_ids(server: SyncServer, hello_world_message_fixture, default_user, sarah_agent):
"""Test basic message listing with limit"""
messages = create_test_messages(server, hello_world_message_fixture, default_user)
message_ids = [m.id for m in messages]
@@ -5774,7 +5811,8 @@ def test_get_messages_by_ids(server: SyncServer, hello_world_message_fixture, de
assert sorted(message_ids) == sorted([r.id for r in results])
-def test_message_listing_basic(server: SyncServer, hello_world_message_fixture, default_user, sarah_agent):
+async def test_message_listing_basic(server: SyncServer, hello_world_message_fixture, default_user, sarah_agent):
"""Test basic message listing with limit"""
create_test_messages(server, hello_world_message_fixture, default_user)
@@ -5782,7 +5820,8 @@ def test_message_listing_basic(server: SyncServer, hello_world_message_fixture,
assert len(results) == 3
-def test_message_listing_cursor(server: SyncServer, hello_world_message_fixture, default_user, sarah_agent):
+async def test_message_listing_cursor(server: SyncServer, hello_world_message_fixture, default_user, sarah_agent):
"""Test cursor-based pagination functionality"""
create_test_messages(server, hello_world_message_fixture, default_user)
@@ -5820,7 +5859,8 @@ def test_message_listing_cursor(server: SyncServer, hello_world_message_fixture,
assert middle_page_desc[-1].id == first_page[1].id
-def test_message_listing_filtering(server: SyncServer, hello_world_message_fixture, default_user, sarah_agent):
+async def test_message_listing_filtering(server: SyncServer, hello_world_message_fixture, default_user, sarah_agent):
"""Test filtering messages by agent ID"""
create_test_messages(server, hello_world_message_fixture, default_user)
@@ -5829,7 +5869,8 @@ def test_message_listing_filtering(server: SyncServer, hello_world_message_fixtu
assert all(msg.agent_id == hello_world_message_fixture.agent_id for msg in agent_results)
-def test_message_listing_text_search(server: SyncServer, hello_world_message_fixture, default_user, sarah_agent):
+async def test_message_listing_text_search(server: SyncServer, hello_world_message_fixture, default_user, sarah_agent):
"""Test searching messages by text content"""
create_test_messages(server, hello_world_message_fixture, default_user)
@@ -6093,7 +6134,8 @@ async def test_create_many_messages_async_with_turbopuffer(server: SyncServer, s
# ======================================================================================================================
-def test_create_block(server: SyncServer, default_user):
+async def test_create_block(server: SyncServer, default_user):
block_manager = BlockManager()
block_create = PydanticBlock(
label="human",
@@ -6260,7 +6302,8 @@ async def test_get_blocks_comprehensive(server, default_user, other_user_differe
assert (await block_manager.get_blocks_async(actor=default_user, label=label)) == []
-def test_update_block(server: SyncServer, default_user):
+async def test_update_block(server: SyncServer, default_user):
block_manager = BlockManager()
block = block_manager.create_or_update_block(PydanticBlock(label="persona", value="Original Content"), actor=default_user)
@@ -6276,7 +6319,8 @@ def test_update_block(server: SyncServer, default_user):
assert updated_block.description == "Updated description"
-def test_update_block_limit(server: SyncServer, default_user):
+async def test_update_block_limit(server: SyncServer, default_user):
block_manager = BlockManager()
block = block_manager.create_or_update_block(PydanticBlock(label="persona", value="Original Content"), actor=default_user)
@@ -6298,7 +6342,8 @@ def test_update_block_limit(server: SyncServer, default_user):
assert updated_block.description == "Updated description"
-def test_update_block_limit_does_not_reset(server: SyncServer, default_user):
+async def test_update_block_limit_does_not_reset(server: SyncServer, default_user):
block_manager = BlockManager()
new_content = "Updated Content" * 2000
limit = len(new_content)
@@ -6330,7 +6375,7 @@ async def test_delete_block(server: SyncServer, default_user):
async def test_delete_block_detaches_from_agent(server: SyncServer, sarah_agent, default_user):
# Create and delete a block
block = server.block_manager.create_or_update_block(PydanticBlock(label="human", value="Sample content"), actor=default_user)
- agent_state = server.agent_manager.attach_block(agent_id=sarah_agent.id, block_id=block.id, actor=default_user)
+ agent_state = await server.agent_manager.attach_block(agent_id=sarah_agent.id, block_id=block.id, actor=default_user)
# Check that block has been attached
assert block.id in [b.id for b in agent_state.memory.blocks]
@@ -6351,8 +6396,8 @@ async def test_delete_block_detaches_from_agent(server: SyncServer, sarah_agent,
async def test_get_agents_for_block(server: SyncServer, sarah_agent, charles_agent, default_user):
# Create and delete a block
block = server.block_manager.create_or_update_block(PydanticBlock(label="alien", value="Sample content"), actor=default_user)
- sarah_agent = server.agent_manager.attach_block(agent_id=sarah_agent.id, block_id=block.id, actor=default_user)
- charles_agent = server.agent_manager.attach_block(agent_id=charles_agent.id, block_id=block.id, actor=default_user)
+ sarah_agent = await server.agent_manager.attach_block(agent_id=sarah_agent.id, block_id=block.id, actor=default_user)
+ charles_agent = await server.agent_manager.attach_block(agent_id=charles_agent.id, block_id=block.id, actor=default_user)
# Check that block has been attached to both
assert block.id in [b.id for b in sarah_agent.memory.blocks]
@@ -6488,7 +6533,8 @@ async def test_bulk_update_respects_org_scoping(
# ======================================================================================================================
-def test_checkpoint_creates_history(server: SyncServer, default_user):
+async def test_checkpoint_creates_history(server: SyncServer, default_user):
"""
Ensures that calling checkpoint_block creates a BlockHistory row and updates
the block's current_history_entry_id appropriately.
@@ -6519,7 +6565,8 @@ def test_checkpoint_creates_history(server: SyncServer, default_user):
assert db_block.current_history_entry_id == hist.id
-def test_multiple_checkpoints(server: SyncServer, default_user):
+async def test_multiple_checkpoints(server: SyncServer, default_user):
block_manager = BlockManager()
# Create a block
@@ -6555,7 +6602,8 @@ def test_multiple_checkpoints(server: SyncServer, default_user):
assert db_block.current_history_entry_id == history_entries[1].id
-def test_checkpoint_with_agent_id(server: SyncServer, default_user, sarah_agent):
+async def test_checkpoint_with_agent_id(server: SyncServer, default_user, sarah_agent):
"""
Ensures that if we pass agent_id to checkpoint_block, we get
actor_type=LETTA_AGENT, actor_id=<agent.id> in BlockHistory.
@@ -6575,7 +6623,8 @@ def test_checkpoint_with_agent_id(server: SyncServer, default_user, sarah_agent)
assert hist_entry.actor_id == sarah_agent.id
-def test_checkpoint_with_no_state_change(server: SyncServer, default_user):
+async def test_checkpoint_with_no_state_change(server: SyncServer, default_user):
"""
If we call checkpoint_block twice without any edits,
we expect two entries or only one, depending on your policy.
@@ -6595,7 +6644,8 @@ def test_checkpoint_with_no_state_change(server: SyncServer, default_user):
assert len(all_hist) == 2
-def test_checkpoint_concurrency_stale(server: SyncServer, default_user):
+async def test_checkpoint_concurrency_stale(server: SyncServer, default_user):
block_manager = BlockManager()
# create block
@@ -6630,7 +6680,8 @@ def test_checkpoint_concurrency_stale(server: SyncServer, default_user):
)
-def test_checkpoint_no_future_states(server: SyncServer, default_user):
+async def test_checkpoint_no_future_states(server: SyncServer, default_user):
"""
Ensures that if the block is already at the highest sequence,
creating a new checkpoint does NOT delete anything.
@@ -6671,7 +6722,8 @@ def test_checkpoint_no_future_states(server: SyncServer, default_user):
# ======================================================================================================================
-def test_undo_checkpoint_block(server: SyncServer, default_user):
+async def test_undo_checkpoint_block(server: SyncServer, default_user):
"""
Verifies that we can undo to the previous checkpoint:
1) Create a block and checkpoint -> sequence_number=1
@@ -6703,7 +6755,8 @@ def test_undo_checkpoint_block(server: SyncServer, default_user):
assert undone_block.label == "undo_test", "Label should also revert if changed (or remain the same if unchanged)"
-def test_checkpoint_deletes_future_states_after_undo(server: SyncServer, default_user):
+async def test_checkpoint_deletes_future_states_after_undo(server: SyncServer, default_user):
"""
Verifies that once we've undone to an earlier checkpoint, creating a new
checkpoint removes any leftover 'future' states that existed beyond that sequence.
@@ -6768,7 +6821,8 @@ def test_checkpoint_deletes_future_states_after_undo(server: SyncServer, default
assert "v3" not in existing_values, "Old seq=3 should have been removed."
-def test_undo_no_history(server: SyncServer, default_user):
+async def test_undo_no_history(server: SyncServer, default_user):
"""
If a block has never been checkpointed (no current_history_entry_id),
undo_checkpoint_block should raise a ValueError.
@@ -6783,7 +6837,8 @@ def test_undo_no_history(server: SyncServer, default_user):
block_manager.undo_checkpoint_block(block_id=block.id, actor=default_user)
-def test_undo_first_checkpoint(server: SyncServer, default_user):
+async def test_undo_first_checkpoint(server: SyncServer, default_user):
"""
If the block is at the first checkpoint (sequence_number=1),
undo should fail because there's no prior checkpoint.
@@ -6802,7 +6857,8 @@ def test_undo_first_checkpoint(server: SyncServer, default_user):
block_manager.undo_checkpoint_block(block_id=block.id, actor=default_user)
-def test_undo_multiple_checkpoints(server: SyncServer, default_user):
+async def test_undo_multiple_checkpoints(server: SyncServer, default_user):
"""
Tests multiple checkpoints in a row, then undo repeatedly
from seq=3 -> seq=2 -> seq=1, verifying each revert.
@@ -6841,7 +6897,8 @@ def test_undo_multiple_checkpoints(server: SyncServer, default_user):
block_manager.undo_checkpoint_block(block_v1.id, actor=default_user)
-def test_undo_concurrency_stale(server: SyncServer, default_user):
+async def test_undo_concurrency_stale(server: SyncServer, default_user):
"""
Demonstrate concurrency: both sessions start with the block at seq=2,
one session undoes first -> block now seq=1, version increments,
@@ -6890,7 +6947,8 @@ def test_undo_concurrency_stale(server: SyncServer, default_user):
# ======================================================================================================================
-def test_redo_checkpoint_block(server: SyncServer, default_user):
+async def test_redo_checkpoint_block(server: SyncServer, default_user):
"""
1) Create a block with value v1 -> checkpoint => seq=1
2) Update to v2 -> checkpoint => seq=2
@@ -6926,7 +6984,8 @@ def test_redo_checkpoint_block(server: SyncServer, default_user):
assert redone_block.value == "v3", "After redo, block should go back to v3"
-def test_redo_no_history(server: SyncServer, default_user):
+async def test_redo_no_history(server: SyncServer, default_user):
"""
If a block has no current_history_entry_id (never checkpointed),
then redo_checkpoint_block should raise ValueError.
@@ -6941,7 +7000,8 @@ def test_redo_no_history(server: SyncServer, default_user):
block_manager.redo_checkpoint_block(block.id, actor=default_user)
-def test_redo_at_highest_checkpoint(server: SyncServer, default_user):
+async def test_redo_at_highest_checkpoint(server: SyncServer, default_user):
"""
If the block is at the maximum sequence number, there's no higher checkpoint to move to.
redo_checkpoint_block should raise ValueError.
@@ -6964,7 +7024,8 @@ def test_redo_at_highest_checkpoint(server: SyncServer, default_user):
block_manager.redo_checkpoint_block(b_init.id, actor=default_user)
-def test_redo_after_multiple_undo(server: SyncServer, default_user):
+async def test_redo_after_multiple_undo(server: SyncServer, default_user):
"""
1) Create and checkpoint versions: v1 -> seq=1, v2 -> seq=2, v3 -> seq=3, v4 -> seq=4
2) Undo thrice => from seq=4 to seq=1
@@ -7007,7 +7068,8 @@ def test_redo_after_multiple_undo(server: SyncServer, default_user):
assert redone_block.value == expected_value, f"Redo should get us forward to {expected_value}"
-def test_redo_concurrency_stale(server: SyncServer, default_user):
+async def test_redo_concurrency_stale(server: SyncServer, default_user):
block_manager = BlockManager()
# 1) Create block => checkpoint => seq=1
@@ -7226,8 +7288,8 @@ async def test_get_set_agents_for_identities(server: SyncServer, sarah_agent, ch
assert agent_without_identity.id not in agent_state_ids
# Delete new agents
- server.agent_manager.delete_agent(agent_id=agent_with_identity.id, actor=default_user)
- server.agent_manager.delete_agent(agent_id=agent_without_identity.id, actor=default_user)
+ await server.agent_manager.delete_agent(agent_id=agent_with_identity.id, actor=default_user)
+ await server.agent_manager.delete_agent(agent_id=agent_without_identity.id, actor=default_user)
# Get the agents for identity id
agent_states = server.agent_manager.list_agents(identity_id=identity.id, actor=default_user)
@@ -9341,7 +9403,8 @@ async def test_e2e_job_callback(monkeypatch, server: SyncServer, default_user):
# ======================================================================================================================
-def test_job_messages_add(server: SyncServer, default_run, hello_world_message_fixture, default_user):
+async def test_job_messages_add(server: SyncServer, default_run, hello_world_message_fixture, default_user):
"""Test adding a message to a job."""
# Add message to job
server.job_manager.add_message_to_job(
@@ -9360,7 +9423,8 @@ def test_job_messages_add(server: SyncServer, default_run, hello_world_message_f
assert messages[0].content[0].text == hello_world_message_fixture.content[0].text
-def test_job_messages_pagination(server: SyncServer, default_run, default_user, sarah_agent):
+async def test_job_messages_pagination(server: SyncServer, default_run, default_user, sarah_agent):
"""Test pagination of job messages."""
# Create multiple messages
message_ids = []
@@ -9370,7 +9434,7 @@ def test_job_messages_pagination(server: SyncServer, default_run, default_user,
role=MessageRole.user,
content=[TextContent(text=f"Test message {i}")],
)
- msg = server.message_manager.create_message(message, actor=default_user)
+ msg = await server.message_manager.create_message(message, actor=default_user)
message_ids.append(msg.id)
# Add message to job
@@ -9468,7 +9532,8 @@ def test_job_messages_pagination(server: SyncServer, default_run, default_user,
assert earliest_msgs_ascending[0].created_at < earliest_msgs_ascending[1].created_at < earliest_msgs_ascending[2].created_at
-def test_job_messages_ordering(server: SyncServer, default_run, default_user, sarah_agent):
+async def test_job_messages_ordering(server: SyncServer, default_run, default_user, sarah_agent):
"""Test that messages are ordered by created_at."""
# Create messages with different timestamps
base_time = datetime.now(timezone.utc)
@@ -9485,7 +9550,7 @@ def test_job_messages_ordering(server: SyncServer, default_run, default_user, sa
agent_id=sarah_agent.id,
created_at=created_at,
)
- msg = server.message_manager.create_message(message, actor=default_user)
+ msg = await server.message_manager.create_message(message, actor=default_user)
# Add message to job
server.job_manager.add_message_to_job(
@@ -9516,7 +9581,8 @@ def test_job_messages_ordering(server: SyncServer, default_run, default_user, sa
assert returned_messages[1].created_at > returned_messages[2].created_at
-def test_job_messages_empty(server: SyncServer, default_run, default_user):
+async def test_job_messages_empty(server: SyncServer, default_run, default_user):
"""Test getting messages for a job with no messages."""
messages = server.job_manager.get_job_messages(
job_id=default_run.id,
@@ -9525,7 +9591,8 @@ def test_job_messages_empty(server: SyncServer, default_run, default_user):
assert len(messages) == 0
-def test_job_messages_add_duplicate(server: SyncServer, default_run, hello_world_message_fixture, default_user):
+async def test_job_messages_add_duplicate(server: SyncServer, default_run, hello_world_message_fixture, default_user):
"""Test adding the same message to a job twice."""
# Add message to job first time
server.job_manager.add_message_to_job(
@@ -9543,7 +9610,8 @@ def test_job_messages_add_duplicate(server: SyncServer, default_run, hello_world
)
-def test_job_messages_filter(server: SyncServer, default_run, default_user, sarah_agent):
+async def test_job_messages_filter(server: SyncServer, default_run, default_user, sarah_agent):
"""Test getting messages associated with a job."""
# Create test messages with different roles and tool calls
messages = [
@@ -9576,7 +9644,7 @@ def test_job_messages_filter(server: SyncServer, default_run, default_user, sara
# Add messages to job
for msg in messages:
- created_msg = server.message_manager.create_message(msg, actor=default_user)
+ created_msg = await server.message_manager.create_message(msg, actor=default_user)
server.job_manager.add_message_to_job(default_run.id, created_msg.id, actor=default_user)
# Test getting all messages
@@ -9593,7 +9661,8 @@ def test_job_messages_filter(server: SyncServer, default_run, default_user, sara
assert len(limited_messages) == 2
-def test_get_run_messages(server: SyncServer, default_user: PydanticUser, sarah_agent):
+async def test_get_run_messages(server: SyncServer, default_user: PydanticUser, sarah_agent):
"""Test getting messages for a run with request config."""
# Create a run with custom request config
run = server.job_manager.create_job(
@@ -9624,7 +9693,7 @@ def test_get_run_messages(server: SyncServer, default_user: PydanticUser, sarah_
]
for msg in messages:
- created_msg = server.message_manager.create_message(msg, actor=default_user)
+ created_msg = await server.message_manager.create_message(msg, actor=default_user)
server.job_manager.add_message_to_job(job_id=run.id, message_id=created_msg.id, actor=default_user)
# Get messages and verify they're converted correctly
@@ -9643,7 +9712,8 @@ def test_get_run_messages(server: SyncServer, default_user: PydanticUser, sarah_
assert msg.tool_call.name == "custom_tool"
-def test_get_run_messages_with_assistant_message(server: SyncServer, default_user: PydanticUser, sarah_agent):
+async def test_get_run_messages_with_assistant_message(server: SyncServer, default_user: PydanticUser, sarah_agent):
"""Test getting messages for a run with request config."""
# Create a run with custom request config
run = server.job_manager.create_job(
@@ -9674,7 +9744,7 @@ def test_get_run_messages_with_assistant_message(server: SyncServer, default_use
]
for msg in messages:
- created_msg = server.message_manager.create_message(msg, actor=default_user)
+ created_msg = await server.message_manager.create_message(msg, actor=default_user)
server.job_manager.add_message_to_job(job_id=run.id, message_id=created_msg.id, actor=default_user)
# Get messages and verify they're converted correctly
@@ -9736,7 +9806,8 @@ async def test_job_usage_stats_add_and_get(server: SyncServer, sarah_agent, defa
assert len(steps) == 1
-def test_job_usage_stats_get_no_stats(server: SyncServer, default_job, default_user):
+async def test_job_usage_stats_get_no_stats(server: SyncServer, default_job, default_user):
"""Test getting usage statistics for a job with no stats."""
job_manager = server.job_manager
@@ -10196,7 +10267,8 @@ async def test_step_manager_record_metrics_nonexistent_step(server: SyncServer,
)
-def test_job_usage_stats_get_nonexistent_job(server: SyncServer, default_user):
+async def test_job_usage_stats_get_nonexistent_job(server: SyncServer, default_user):
"""Test getting usage statistics for a nonexistent job."""
job_manager = server.job_manager
@@ -10275,7 +10347,8 @@ async def test_record_timing_invalid_job(server: SyncServer, default_user):
await server.job_manager.record_response_duration("nonexistent_job_id", 2_000_000_000, default_user)
-def test_list_tags(server: SyncServer, default_user, default_organization):
+async def test_list_tags(server: SyncServer, default_user, default_organization):
"""Test listing tags functionality."""
# Create multiple agents with different tags
agents = []
@@ -10326,7 +10399,7 @@ def test_list_tags(server: SyncServer, default_user, default_organization):
# Cleanup
for agent in agents:
- server.agent_manager.delete_agent(agent.id, actor=default_user)
+ await server.agent_manager.delete_agent(agent.id, actor=default_user)
# ======================================================================================================================
@@ -12887,7 +12960,8 @@ FAILED tests/test_managers.py::test_high_concurrency_stress_test - AssertionErro
# await server.block_manager.delete_block_async(block.id, actor=default_user)
-def test_create_internal_template_objects(server: SyncServer, default_user):
+async def test_create_internal_template_objects(server: SyncServer, default_user):
"""Test creating agents, groups, and blocks with template-related fields."""
from letta.schemas.agent import InternalTemplateAgentCreate
from letta.schemas.block import Block, InternalTemplateBlockCreate
@@ -12956,7 +13030,7 @@ def test_create_internal_template_objects(server: SyncServer, default_user):
# Clean up
server.group_manager.delete_group(group.id, actor=default_user)
server.block_manager.delete_block(block.id, actor=default_user)
- server.agent_manager.delete_agent(agent.id, actor=default_user)
+ await server.agent_manager.delete_agent(agent.id, actor=default_user)
# TODO: I use this as a way to easily wipe my local db lol sorry