open-webui/backend/open_webui/models
Classic298 d67bc4ffcd
perf: batch the file access check queries (#27383)
has_access_to_file runs for every non-owner file GET, per RAG file check and per shared-chat or model-attached file. Its final step called Models.get_models_by_user_id, which issued one grant query per non-owned workspace model, so a single file check on an instance with M workspace models cost M grant queries plus a group query, with the deny path always paying full price. Its collection_name step listed every knowledge base the user can access (itself one grant query per knowledge base) just to scan the list for one id. And get_accessible_folder_files repeated the whole pipeline per folder entry, refetching the caller's group memberships every time.

Three changes, all using parameters and helpers that already exist:
- Models.get_models_by_user_id resolves grants for all non-owned models in one get_accessible_resource_ids call and accepts prefetched user_group_ids.
- The collection_name check fetches the one referenced knowledge base and performs a single owner-or-grant check with the already-resolved group ids, preserving the write-requires-owner guard exactly (including its short-circuit before any grant query).
- get_accessible_folder_files resolves group ids once and threads them through every per-entry check.

Benchmark:

| metric | before | after |
| --- | --- | --- |
| filter loop CPU, 300 workspace models (queries stubbed) | 47 us | 19 us |
| grant queries per file-access check, M workspace models | M | 1 |
| group membership queries per folder listing, F files | F | 1 |

The stubbed CPU row understates the win: each removed query in the other two rows was a real database round trip.

Functionally verified with stubbed accessors: owned plus granted models are returned with owned ids excluded from the batch query; model-attached file access resolves through the batched path; the collection_name path does one KB fetch and one grant check with no full listing; a missing KB falls through; write access via a KB still requires the KB owner to own the file and short-circuits before the grant query; folder listings fetch groups exactly once.
2026-07-23 17:50:08 -05:00
..
access_grants.py refac: modernize type annotations (PEP 604 / PEP 585) 2026-05-12 17:10:15 +09:00
auths.py refac 2026-06-29 03:16:59 -05:00
automations.py refac: modernize type annotations (PEP 604 / PEP 585) 2026-05-12 17:10:15 +09:00
calendar.py refac 2026-06-01 14:09:54 -07:00
channels.py refac: modernize type annotations (PEP 604 / PEP 585) 2026-05-12 17:10:15 +09:00
chat_messages.py refac 2026-07-20 01:33:47 -04:00
chats.py refac 2026-07-23 04:01:30 -04:00
config.py refac 2026-07-23 04:16:14 -04:00
feedbacks.py refac 2026-06-29 13:03:14 -05:00
files.py feat(ui): show total file count in FilesModal title (#25873) 2026-06-17 02:55:13 +02:00
folders.py refac 2026-06-29 13:03:14 -05:00
functions.py chore: format 2026-07-23 13:41:16 -04:00
groups.py refac: modernize type annotations (PEP 604 / PEP 585) 2026-05-12 17:10:15 +09:00
knowledge.py refac 2026-07-17 04:48:21 -04:00
memories.py refac 2026-06-29 13:03:14 -05:00
messages.py refac 2026-06-29 13:03:14 -05:00
models.py perf: batch the file access check queries (#27383) 2026-07-23 17:50:08 -05:00
notes.py refac 2026-07-17 06:50:16 -04:00
oauth_sessions.py refac: modernize type annotations (PEP 604 / PEP 585) 2026-05-12 17:10:15 +09:00
prompt_history.py fix: bind prompt history/version ops to the authorized prompt (#25056) 2026-06-01 12:07:52 -07:00
prompts.py chore: format 2026-06-01 13:56:55 -07:00
shared_chats.py feat(ui): add 'Unshare All Shared Chats' button to Shared Chats modal (#25848) 2026-06-17 03:06:41 +02:00
skills.py refac 2026-07-17 04:11:11 -04:00
tags.py chore: format 2026-06-01 13:56:55 -07:00
tools.py refac 2026-06-29 13:03:14 -05:00
users.py refac 2026-07-23 14:29:27 -04:00