fix: propagate search_settings filters through agent tools and system context builder - #2286
Open
octo-patch wants to merge 1 commit into
Open
Conversation
… context builder Fixes SciPhi-AI#2244 and SciPhi-AI#2245. Two related bugs caused search filter scoping (e.g. collection_ids) to be silently ignored, allowing agents to access documents outside the intended collection boundary: 1. _build_documents_context did not accept or forward filter_collection_ids to get_documents_overview, so the dynamic RAG system prompt listed all documents regardless of collection filters (SciPhi-AI#2245). 2. GetFileContentTool constructed a bare {"id": ...} filter, completely discarding any search_settings.filters (collection_ids etc.) set by the caller. A document_id returned by the LLM could therefore resolve to content from a different collection (SciPhi-AI#2244). Changes: - _build_documents_context now accepts filter_collection_ids and passes it to get_documents_overview. - _build_aware_system_instruction forwards filter_collection_ids when calling _build_documents_context. - GetFileContentTool.execute merges context.search_settings.filters into an $and compound filter alongside the document_id predicate.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2244
Fixes #2245
Problem
Two related bugs caused
search_settingsfilters (e.g.collection_ids) to be silently discarded, allowing agents to access documents outside the intended collection boundary:Issue #2245 —
_build_aware_system_instructionignores collection filtersWhen
use_system_context=True, the dynamic RAG system prompt is built by_build_documents_context, which callsget_documents_overviewwith onlyfilter_user_ids. Thefilter_collection_idsextracted fromsearch_settingswas never forwarded, so the system prompt enumerated all documents regardless of any collection restriction.Issue #2244 —
GetFileContentToolignoressearch_settings.filtersGetFileContentTool.executecreated its own filter dict{"id": {"$eq": doc_uuid}}and ignored thesearch_settings.filters(e.g.collection_ids) available on the agent context. Adocument_idsuggested by the LLM could therefore resolve to content from a different collection.Solution
_build_documents_contextnow accepts afilter_collection_idsparameter and passes it toget_documents_overview._build_aware_system_instructionforwardsfilter_collection_idswhen calling_build_documents_context.GetFileContentTool.executemergescontext.search_settings.filtersinto an$andcompound filter alongside the document-id predicate, so collection-scoped queries are enforced end-to-end.Testing
The changes are purely additive — existing callers that do not supply
filter_collection_idsretain the previous behavior (parameter defaults toNone). TheGetFileContentToolfix is backward-compatible: if nosearch_settingsfilters exist on the context, the tool behaves exactly as before.Manual testing: reproduce the scenarios described in #2244 and #2245 with a
collection_idsfilter and verify that only documents belonging to the specified collection are returned.