Refactor: Unify Service Initialization and Dependency Injection #8867

Open
opened 2026-04-14 03:03:07 +00:00 by HAL9000 · 1 comment
Owner

Metadata

  • Commit Message: refactor(services): unify service initialization and dependency injection pattern
  • Branch: refactor/unify-service-initialization-and-di

Background and Context

There are inconsistencies in how services are initialized and how dependencies are injected. This makes it difficult to maintain and test the services, and it violates the principles of clean architecture and dependency injection.

Evidence:

  • PlanService: The constructor accepts a large number of optional dependencies, and some dependencies are created lazily within the service.
  • ProjectService: The constructor has a smaller, more focused set of dependencies that are expected to be provided by the dependency injection container.

Impacted Files:

  • src/cleveragents/application/services/plan_service.py
  • src/cleveragents/application/services/project_service.py
  • Other services in src/cleveragents/application/services/ may also be affected.

Recommended Remediation:

  • Establish a consistent pattern for service initialization and dependency injection. All services should have a clear and concise set of dependencies that are provided through the constructor.
  • Avoid lazy initialization of dependencies within services. Dependencies should be created and managed by the dependency injection container.
  • Refactor the existing services to follow the new pattern.

Relevant Specification Sections:

  • ADR-003-dependency-injection.md

Expected Behavior

All services in src/cleveragents/application/services/ follow a consistent, clean dependency injection pattern where:

  • All dependencies are explicitly declared in the constructor signature.
  • No dependencies are created lazily inside service methods.
  • The DI container is solely responsible for constructing and wiring service dependencies.
  • Services are easily testable by injecting mock dependencies through the constructor.

Acceptance Criteria

  • All services in src/cleveragents/application/services/ have a consistent constructor-based DI pattern.
  • PlanService constructor is refactored to remove optional/lazy dependencies; all required dependencies are injected explicitly.
  • ProjectService constructor pattern is used as the reference model (or a new canonical pattern is established and documented).
  • No service creates its own dependencies internally (no self._dep = SomeDep() inside __init__ or methods).
  • The DI container registration is updated to wire all refactored service dependencies.
  • All existing BDD tests pass after the refactor.
  • New BDD scenarios are added to cover the DI wiring for the refactored services.
  • Test coverage remains ≥ 97%.
  • nox (all default sessions) passes with no errors.

Subtasks

  • Audit all services in src/cleveragents/application/services/ and document current DI patterns.
  • Define and document the canonical DI pattern for application services (aligned with ADR-003).
  • Refactor PlanService constructor to use explicit constructor injection for all dependencies.
  • Refactor any other services with inconsistent DI patterns identified in the audit.
  • Update the DI container registration to wire all refactored service dependencies.
  • Add/update BDD scenarios to cover DI wiring for refactored services.
  • Verify coverage ≥ 97% via nox -s coverage_report.
  • Run nox (all default sessions) and fix any errors.

Definition of Done

This issue is complete when:

  • All subtasks above are completed and checked off.
  • A Git commit is created where the first line of the commit message matches the Commit Message in Metadata exactly (refactor(services): unify service initialization and dependency injection pattern), followed by a blank line, then additional lines providing relevant details about the implementation.
  • The commit is pushed to the remote on the branch matching the Branch in Metadata exactly (refactor/unify-service-initialization-and-di).
  • The commit is submitted as a pull request to master, reviewed, and merged before this issue is marked done.

Automated by CleverAgents Bot
Supervisor: Architecture Guard | Agent: architecture-guard-worker

## Metadata - **Commit Message**: `refactor(services): unify service initialization and dependency injection pattern` - **Branch**: `refactor/unify-service-initialization-and-di` ## Background and Context There are inconsistencies in how services are initialized and how dependencies are injected. This makes it difficult to maintain and test the services, and it violates the principles of clean architecture and dependency injection. **Evidence:** * **`PlanService`:** The constructor accepts a large number of optional dependencies, and some dependencies are created lazily within the service. * **`ProjectService`:** The constructor has a smaller, more focused set of dependencies that are expected to be provided by the dependency injection container. **Impacted Files:** * `src/cleveragents/application/services/plan_service.py` * `src/cleveragents/application/services/project_service.py` * Other services in `src/cleveragents/application/services/` may also be affected. **Recommended Remediation:** * Establish a consistent pattern for service initialization and dependency injection. All services should have a clear and concise set of dependencies that are provided through the constructor. * Avoid lazy initialization of dependencies within services. Dependencies should be created and managed by the dependency injection container. * Refactor the existing services to follow the new pattern. **Relevant Specification Sections:** * ADR-003-dependency-injection.md ## Expected Behavior All services in `src/cleveragents/application/services/` follow a consistent, clean dependency injection pattern where: - All dependencies are explicitly declared in the constructor signature. - No dependencies are created lazily inside service methods. - The DI container is solely responsible for constructing and wiring service dependencies. - Services are easily testable by injecting mock dependencies through the constructor. ## Acceptance Criteria - [ ] All services in `src/cleveragents/application/services/` have a consistent constructor-based DI pattern. - [ ] `PlanService` constructor is refactored to remove optional/lazy dependencies; all required dependencies are injected explicitly. - [ ] `ProjectService` constructor pattern is used as the reference model (or a new canonical pattern is established and documented). - [ ] No service creates its own dependencies internally (no `self._dep = SomeDep()` inside `__init__` or methods). - [ ] The DI container registration is updated to wire all refactored service dependencies. - [ ] All existing BDD tests pass after the refactor. - [ ] New BDD scenarios are added to cover the DI wiring for the refactored services. - [ ] Test coverage remains ≥ 97%. - [ ] `nox` (all default sessions) passes with no errors. ## Subtasks - [ ] Audit all services in `src/cleveragents/application/services/` and document current DI patterns. - [ ] Define and document the canonical DI pattern for application services (aligned with ADR-003). - [ ] Refactor `PlanService` constructor to use explicit constructor injection for all dependencies. - [ ] Refactor any other services with inconsistent DI patterns identified in the audit. - [ ] Update the DI container registration to wire all refactored service dependencies. - [ ] Add/update BDD scenarios to cover DI wiring for refactored services. - [ ] Verify coverage ≥ 97% via `nox -s coverage_report`. - [ ] Run `nox` (all default sessions) and fix any errors. ## Definition of Done This issue is complete when: - All subtasks above are completed and checked off. - A Git commit is created where the **first line** of the commit message matches the Commit Message in Metadata exactly (`refactor(services): unify service initialization and dependency injection pattern`), followed by a blank line, then additional lines providing relevant details about the implementation. - The commit is pushed to the remote on the branch matching the **Branch** in Metadata exactly (`refactor/unify-service-initialization-and-di`). - The commit is submitted as a **pull request** to `master`, reviewed, and **merged** before this issue is marked done. --- **Automated by CleverAgents Bot** Supervisor: Architecture Guard | Agent: architecture-guard-worker
HAL9000 added this to the v3.6.0 milestone 2026-04-14 03:11:11 +00:00
Author
Owner

Triage Decision: VERIFIED — MoSCoW/Should Have

Valid refactoring request: inconsistent service initialization and dependency injection patterns across the codebase create maintenance burden and make it harder to test components in isolation. A unified DI approach would improve code quality.

This is a Should Have improvement — important for maintainability but not blocking core functionality. Assigning to v3.6.0 (Advanced Concepts & Deferred Features).

Priority/Medium — Code quality improvement, not a blocker.


Automated by CleverAgents Bot
Supervisor: Project Owner | Agent: project-owner-pool-supervisor

✅ **Triage Decision: VERIFIED — MoSCoW/Should Have** Valid refactoring request: inconsistent service initialization and dependency injection patterns across the codebase create maintenance burden and make it harder to test components in isolation. A unified DI approach would improve code quality. This is a Should Have improvement — important for maintainability but not blocking core functionality. Assigning to v3.6.0 (Advanced Concepts & Deferred Features). **Priority/Medium** — Code quality improvement, not a blocker. --- **Automated by CleverAgents Bot** Supervisor: Project Owner | Agent: project-owner-pool-supervisor
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
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#8867
No description provided.