Skip to content

[Bug]: file_search never returns results for user-attached files when using a shared agent (entity_id filters them out) #13692

@msserpa

Description

@msserpa

What happened?

When a user attaches a file via "Upload for File Search" while chatting with a shared agent (an agent with its own file_search knowledge base), the file_search tool never returns results from the newly attached file — only chunks from the agent's knowledge base come back. The model then answers "I couldn't find this information", even though the file was uploaded and embedded successfully.

Steps to Reproduce

  1. Configure RAG (rag_api) and create an agent with at least one file in its file_search tool resource (the "knowledge base").
  2. Share the agent with another user (or use any flow where entity_id is set).
  3. As a regular user, start a chat with that agent, attach a small .txt via Upload for File Search, wait for embedding to finish.
  4. Ask a question whose answer is only in the attached file.

Expected: the attached file's chunks appear in the tool results.
Actual: only knowledge-base files are returned; the attached file yields zero results in every query.

Root cause

createFileSearchTool (api/app/clients/tools/util/fileSearch.js) sends the agent's entity_id in every /query request to the RAG API:

const body = { file_id: file.file_id, query, k: 5 };
if (!entity_id) {
  return body;
}
body.entity_id = entity_id;

But user-attached files are embedded by rag_api under the user id (no entity), while only knowledge-base files are embedded under the agent's entity_id. When the query carries entity_id, rag_api filters the vectors by that entity and returns [] for the user's file.

Verified by querying rag_api directly for a user-attached file:

  • without entity_id → 1 result (chunk found)
  • with entity_id=<agent_id> → 0 results

The HTTP layer hides this completely: both queries return 200, so the tool "succeeds" with an empty set for the attachment.

Proposed fix

primeFiles already knows which files belong to the agent's knowledge base (agentResourceIds). Tag each file with that origin and only send entity_id for knowledge-base files:

  • primeFiles: files.push({ file_id, filename, fromAgent: agentResourceIds.has(file.file_id) })
  • createQueryBody: if (!entity_id || file.fromAgent === false) return body;

Using an explicit fromAgent === false check keeps the previous behavior for any caller that doesn't set the flag. PR with tests incoming.

Version Information

Reproduced on v0.8.6 (the affected code in api/app/clients/tools/util/fileSearch.js is unchanged on current main as of 2026-06-11).

Relevant log output

# Direct RAG API query for a freshly user-attached file (embedding completed):
# without entity_id:
POST /query {"file_id":"2d6a3de3-...","query":"...","k":8}
-> 200, 1 result (chunk found)

# with the agent's entity_id (what file_search sends today):
POST /query {"file_id":"2d6a3de3-...","query":"...","k":8,"entity_id":"agent_..."}
-> 200, [] (empty)

Both requests return 200, so the tool reports "No results found" for the
attachment while the knowledge-base files keep answering — nothing in the
logs points at the entity filter.

What browsers are you seeing the problem on?

Not browser-related (server-side tool behavior).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions