[AUTO-GUARD-1] Deduplicate SQLAlchemy service factory helpers in application.container #8054

Closed
opened 2026-04-13 01:39:30 +00:00 by HAL9000 · 1 comment
Owner

Summary

  • _build_repo_indexing_service, _build_resource_registry_service, _build_namespaced_project_repo, and _build_project_resource_link_repo in application.container all repeat the same engine/session factory wiring.
  • Each function reimplements create_engine/sessionmaker and returns a repository or service with identical scaffolding, which is over 70% identical code.
  • The surrounding module already defines _build_session_factory, but these helpers bypass it and duplicate the same steps.

Evidence

  • src/cleveragents/application/container.py lines 237-255, 270-303 show the repeated blocks (engine creation, sessionmaker, repository instantiation).
  • File comment "Factory-per-resolution matches the existing project pattern" indicates the intent to share logic but the implementation is still copy/paste.

Recommendation

  • Extract a shared helper that builds a session factory once and accepts the repository/service constructor, or reuse _build_session_factory with thin wrappers.
  • Collapse the four helpers into parameterized calls so future persistence wiring changes (engine flags, session options) happen in one place.
## Summary - `_build_repo_indexing_service`, `_build_resource_registry_service`, `_build_namespaced_project_repo`, and `_build_project_resource_link_repo` in `application.container` all repeat the same engine/session factory wiring. - Each function reimplements `create_engine`/`sessionmaker` and returns a repository or service with identical scaffolding, which is over 70% identical code. - The surrounding module already defines `_build_session_factory`, but these helpers bypass it and duplicate the same steps. ## Evidence - `src/cleveragents/application/container.py` lines 237-255, 270-303 show the repeated blocks (engine creation, `sessionmaker`, repository instantiation). - File comment "Factory-per-resolution matches the existing project pattern" indicates the intent to share logic but the implementation is still copy/paste. ## Recommendation - Extract a shared helper that builds a session factory once and accepts the repository/service constructor, or reuse `_build_session_factory` with thin wrappers. - Collapse the four helpers into parameterized calls so future persistence wiring changes (engine flags, session options) happen in one place.
Owner

superseded by next cycle

superseded by next cycle
Sign in to join this conversation.
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
cleveragents/cleveragents-core#8054
No description provided.