Skip to content

Extract File CRUD out of the crud/__init__.py monolith - #2456

Merged
akwasigroch merged 1 commit into
mainfrom
refactor/extract-file-crud
Aug 12, 2026
Merged

Extract File CRUD out of the crud/__init__.py monolith#2456
akwasigroch merged 1 commit into
mainfrom
refactor/extract-file-crud

Conversation

@akwasigroch

@akwasigroch akwasigroch commented Aug 12, 2026

Copy link
Copy Markdown
Collaborator

Purpose

crud/__init__.py is a 1946-line monolith that the codebase is splitting one entity at a time, following #2411, #2412, #2438, #2439, #2441, #2442, #2449, #2450 and #2451. This takes the File block out. Per apps/backend/AGENTS.md the package only shrinks from here — nothing new goes back into __init__.py.

What Changed

  • Moved create_file, get_file, link_file_to_entity, get_files_for_entity, get_entity_files_total_size, get_entity_files_max_position and delete_file into a new crud/file.py (151 lines), unchanged. The # File CRUD operations banner in __init__.py goes with them.
  • crud/__init__.py drops 121 lines.
  • Nothing is re-exported, matching the earlier splits. All 27 call sites across 9 files now use from rhesis.backend.app.crud import file as file_crud.
  • Nothing dropped — all seven functions have callers.
  • routers/telemetry.py and routers/file.py no longer need from rhesis.backend.app import crud at all. services/invokers/tracing.py and services/telemetry/conversation_linking.py had function-local from rhesis.backend.app import crud as _crud imports, now direct submodule imports.
  • Removing the block left func with no module-scope use in __init__.py, which turned the pre-existing function-local from sqlalchemy import func inside get_user_by_email into a ruff F811. func is therefore dropped from the top-level from sqlalchemy import and_, func, select, text. and_, select and text are still used.

Additional Context

Testing

Pure move — behaviour is unchanged, so the existing suite is the check.

cd apps/backend
uv run pytest ../../tests/backend/services/test_file_execution.py ../../tests/backend/services/test_file_integration.py ../../tests/backend/routes/test_file.py
# 35 passed

uv run pytest ../../tests/backend/crud ../../tests/backend/routes/test_telemetry.py ../../tests/backend/routes/test_trace_linking_timing.py ../../tests/backend/tasks
# 546 passed, 3 skipped

uv run pytest ../../tests/backend/services
# 1862 passed, 1 skipped, 1 xfailed

uv run pytest ../../tests/backend/routes ../../tests/backend/security
# 2514 passed, 39 skipped

Skips are pre-existing @pytest.mark.skips. A repo-wide grep across apps/, tests/, sdk/, ee/, packages/ and penelope/ for the seven old crud.* names returns nothing, and there is no getattr(crud, ...)/hasattr(crud, ...) reflection anywhere that could reach them dynamically.

Two pre-existing ruff findings remain in routers/test_result.py (I001, and unused typing.Optional). Both were confirmed against a stashed baseline and left alone rather than mixed into this refactor.

Worth a reviewer's eye: the two file test modules patch by module attribute, so "...results.crud" became "...results.file_crud" and mock_crud became mock_file_crud. Left unchanged these would have failed loudly on assert_called_once() rather than silently passing.

Move the seven file functions out of crud/__init__.py into crud/file.py,
following the split already done for comment, token, metric and others.
Consumers import the module directly; nothing is re-exported.

All seven have callers, so nothing was dropped. Removing the block left
func unused at module scope in crud/__init__.py, which turned the local
"from sqlalchemy import func" inside get_user_by_email into an F811, so
the module-level import of func goes too.

telemetry.py and routers/file.py no longer need "from rhesis.backend.app
import crud" at all; tracing.py and conversation_linking.py swap their
function-local "crud as _crud" imports for the file module.
@peqy

peqy Bot commented Aug 12, 2026

Copy link
Copy Markdown

Looks good. Clean extraction into crud/file.py with call sites + tests updated consistently; the func import cleanup in crud/__init__.py makes sense. Ship it.

@akwasigroch
akwasigroch merged commit 7a4c9cf into main Aug 12, 2026
16 of 17 checks passed
@akwasigroch
akwasigroch deleted the refactor/extract-file-crud branch August 12, 2026 15:16
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.

1 participant