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: prevent unbounded file queries (#8285)

cthomascfde95531361
diff --git a/letta/server/rest_api/routers/v1/agents.py b/letta/server/rest_api/routers/v1/agents.py
index 8c7d68e01..50181fb3d 100644
--- a/letta/server/rest_api/routers/v1/agents.py
+++ b/letta/server/rest_api/routers/v1/agents.py
@@ -745,9 +745,10 @@ async def detach_source(
if not agent_state.sources:
agent_state = await server.agent_manager.detach_all_files_tools_async(agent_state=agent_state, actor=actor)
- files = await server.file_manager.list_files(source_id, actor)
- file_ids = [f.id for f in files]
- await server.remove_files_from_context_window(agent_state=agent_state, file_ids=file_ids, actor=actor)
+ # Query files_agents directly to get exactly what was attached, regardless of source changes
+ file_ids = await server.file_agent_manager.get_file_ids_for_agent_by_source(agent_id=agent_id, source_id=source_id, actor=actor)
+ if file_ids:
+ await server.remove_files_from_context_window(agent_state=agent_state, file_ids=file_ids, actor=actor)
if agent_state.enable_sleeptime:
try:
@@ -776,9 +777,10 @@ async def detach_folder_from_agent(
if not agent_state.sources:
agent_state = await server.agent_manager.detach_all_files_tools_async(agent_state=agent_state, actor=actor)
- files = await server.file_manager.list_files(folder_id, actor)
- file_ids = [f.id for f in files]
- await server.remove_files_from_context_window(agent_state=agent_state, file_ids=file_ids, actor=actor)
+ # Query files_agents directly to get exactly what was attached, regardless of source changes
+ file_ids = await server.file_agent_manager.get_file_ids_for_agent_by_source(agent_id=agent_id, source_id=folder_id, actor=actor)
+ if file_ids:
+ await server.remove_files_from_context_window(agent_state=agent_state, file_ids=file_ids, actor=actor)
if agent_state.enable_sleeptime:
try:
diff --git a/letta/server/rest_api/routers/v1/folders.py b/letta/server/rest_api/routers/v1/folders.py
index 2faec95d3..d925454c5 100644
--- a/letta/server/rest_api/routers/v1/folders.py
+++ b/letta/server/rest_api/routers/v1/folders.py
@@ -204,8 +204,6 @@ async def delete_folder(
actor = await server.user_manager.get_actor_or_default_async(actor_id=headers.actor_id)
folder = await server.source_manager.get_source_by_id(source_id=folder_id, actor=actor)
agent_states = await server.source_manager.list_attached_agents(source_id=folder_id, actor=actor)
- files = await server.file_manager.list_files(folder_id, actor)
- file_ids = [f.id for f in files]
if should_use_tpuf():
logger.info(f"Deleting folder {folder_id} from Turbopuffer")
@@ -218,7 +216,12 @@ async def delete_folder(
await delete_source_records_from_pinecone_index(source_id=folder_id, actor=actor)
for agent_state in agent_states:
- await server.remove_files_from_context_window(agent_state=agent_state, file_ids=file_ids, actor=actor)
+ # Query files_agents directly to get exactly what was attached to this agent
+ file_ids = await server.file_agent_manager.get_file_ids_for_agent_by_source(
+ agent_id=agent_state.id, source_id=folder_id, actor=actor
+ )
+ if file_ids:
+ await server.remove_files_from_context_window(agent_state=agent_state, file_ids=file_ids, actor=actor)
if agent_state.enable_sleeptime:
block = await server.agent_manager.get_block_with_label_async(agent_id=agent_state.id, block_label=folder.name, actor=actor)
diff --git a/letta/server/rest_api/routers/v1/sources.py b/letta/server/rest_api/routers/v1/sources.py
index 7e63c508d..aad280748 100644
--- a/letta/server/rest_api/routers/v1/sources.py
+++ b/letta/server/rest_api/routers/v1/sources.py
@@ -184,8 +184,6 @@ async def delete_source(
actor = await server.user_manager.get_actor_or_default_async(actor_id=headers.actor_id)
source = await server.source_manager.get_source_by_id(source_id=source_id, actor=actor)
agent_states = await server.source_manager.list_attached_agents(source_id=source_id, actor=actor)
- files = await server.file_manager.list_files(source_id, actor)
- file_ids = [f.id for f in files]
if should_use_tpuf():
logger.info(f"Deleting source {source_id} from Turbopuffer")
@@ -198,7 +196,12 @@ async def delete_source(
await delete_source_records_from_pinecone_index(source_id=source_id, actor=actor)
for agent_state in agent_states:
- await server.remove_files_from_context_window(agent_state=agent_state, file_ids=file_ids, actor=actor)
+ # Query files_agents directly to get exactly what was attached to this agent
+ file_ids = await server.file_agent_manager.get_file_ids_for_agent_by_source(
+ agent_id=agent_state.id, source_id=source_id, actor=actor
+ )
+ if file_ids:
+ await server.remove_files_from_context_window(agent_state=agent_state, file_ids=file_ids, actor=actor)
if agent_state.enable_sleeptime:
block = await server.agent_manager.get_block_with_label_async(agent_id=agent_state.id, block_label=source.name, actor=actor)
diff --git a/letta/services/file_manager.py b/letta/services/file_manager.py
index e8833f823..a0d60136b 100644
--- a/letta/services/file_manager.py
+++ b/letta/services/file_manager.py
@@ -414,7 +414,7 @@ class FileManager:
actor: PydanticUser,
before: Optional[str] = None,
after: Optional[str] = None,
- limit: Optional[int] = None,
+ limit: Optional[int] = 1000,
ascending: Optional[bool] = True,
include_content: bool = False,
strip_directory_prefix: bool = False,
diff --git a/letta/services/files_agents_manager.py b/letta/services/files_agents_manager.py
index 13013f702..d45947a9f 100644
--- a/letta/services/files_agents_manager.py
+++ b/letta/services/files_agents_manager.py
@@ -292,6 +292,32 @@ class FileAgentManager:
else:
return [r.to_pydantic() for r in rows]
+ @enforce_types
+ @trace_method
+ async def get_file_ids_for_agent_by_source(
+ self,
+ agent_id: str,
+ source_id: str,
+ actor: PydanticUser,
+ ) -> List[str]:
+ """
+ Get all file IDs attached to an agent from a specific source.
+
+ This queries the files_agents junction table directly, ensuring we get
+ exactly the files that were attached, regardless of any changes to the
+ source's file list.
+ """
+ async with db_registry.async_session() as session:
+ stmt = select(FileAgentModel.file_id).where(
+ and_(
+ FileAgentModel.agent_id == agent_id,
+ FileAgentModel.source_id == source_id,
+ FileAgentModel.organization_id == actor.organization_id,
+ )
+ )
+ result = await session.execute(stmt)
+ return list(result.scalars().all())
+
@enforce_types
@trace_method
async def list_files_for_agent_paginated(