Skip to content

Fix exclude file filters leaking into query in FileFilter.defilter - #1345

Open
JSap0914 wants to merge 1 commit into
khoj-ai:masterfrom
JSap0914:fix-file-filter-defilter-exclude
Open

Fix exclude file filters leaking into query in FileFilter.defilter#1345
JSap0914 wants to merge 1 commit into
khoj-ai:masterfrom
JSap0914:fix-file-filter-defilter-exclude

Conversation

@JSap0914

Copy link
Copy Markdown

Bug

FileFilter.defilter() only removes include file filters (file:"...") from a query. Its file_filter_regex carries a (?<!-) negative lookbehind, so exclude filters of the form -file:"..." are never matched and stay in the query.

defilter_query() (khoj/processor/conversation/utils.py) runs every filter's defilter() to strip filter syntax before the query is sent to search and the chat model. Because exclude file filters aren't stripped, raw filter syntax like -file:"notes.org" leaks into the search/LLM query. This is inconsistent with WordFilter.defilter(), which removes both required (+"...") and blocked (-"...") terms.

Fix

Strip the excluded file filter regex in addition to the include regex, so both file:"..." and -file:"..." are removed from the defiltered query.

Verification

Added unit tests covering include, exclude, and mixed file filters in tests/test_file_filter.py, then ran them offline:

python -m pytest tests/test_file_filter.py
...
13 passed

Before the fix the two new exclude/mixed cases failed ('head -file:"file 1.org" tail' != 'head tail'); after the fix all 13 pass.

FileFilter.defilter only stripped include file filters (file:"...")
because file_filter_regex carries a (?<!-) lookbehind. Exclude filters
(-file:"...") were left behind, leaking filter syntax into the
defiltered query sent to search and chat models.

Strip the excluded file filter regex as well, mirroring WordFilter
which removes both required and blocked terms.
Copilot AI review requested due to automatic review settings June 16, 2026 04:06

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants