UAT: Application services directly import concrete infrastructure classes - clean architecture boundary violation #3924

Open
opened 2026-04-06 07:28:52 +00:00 by freemo · 0 comments
Owner

Metadata

  • Branch: fix/app-services-infrastructure-import-boundary-violation
  • Commit Message: fix(application): replace direct infrastructure imports with domain protocol abstractions
  • Milestone: None (backlog — see note below)
  • Parent Epic: #397

Bug Report

Feature Area: Dependency Injection and Service Layer

Severity: Medium — violates clean architecture layer boundaries; application layer should depend on abstractions (protocols/interfaces), not concrete infrastructure implementations

What Was Tested

Code-level analysis of imports in src/cleveragents/application/services/ against clean architecture principles specified in the project specification.

Expected Behavior (from spec)

Per the specification, the project follows Clean Architecture with clear layer boundaries. The Application Layer should depend on domain abstractions (protocols/interfaces), not on concrete infrastructure implementations. Infrastructure concerns (database models, concrete repositories, sandbox implementations) should be injected via DI, not imported directly.

The spec states: "The Infrastructure layer contains components like the LSP Runtime" and the architecture emphasizes "separation of concerns and clear boundaries between layers."

Actual Behavior

Multiple application services directly import concrete infrastructure classes, bypassing the abstraction layer:

Direct infrastructure model imports (should use domain models or protocols):

  • tool_registry_service.py line 14: from cleveragents.infrastructure.database.repositories import ToolRegistryRepository, ValidationAttachmentRepository
  • session_service.py lines 30-31: from cleveragents.infrastructure.database.repositories import SessionMessageRepository, SessionRepository
  • audit_service.py line 54: from cleveragents.infrastructure.database.models import AuditLogModel, Base
  • repo_indexing_service.py line 41: from cleveragents.infrastructure.database.models import ...
  • resource_registry_service.py line 80: from cleveragents.infrastructure.database.models import ...
  • lock_service.py line 40: from cleveragents.infrastructure.database.models import LockModel
  • skill_registry_service.py line 16: from cleveragents.infrastructure.database.repositories import ...

Direct infrastructure sandbox imports:

  • plan_apply_service.py line 38: from cleveragents.infrastructure.sandbox.checkpoint import CheckpointManager
  • subplan_merge_service.py line 20: from cleveragents.infrastructure.sandbox.merge import ...
  • resource_handler_service.py lines 37-38: from cleveragents.infrastructure.sandbox.factory import SandboxStrategyStr and from cleveragents.infrastructure.sandbox.manager import SandboxManager

Impact

  1. Layer boundary violation: Application services are tightly coupled to infrastructure implementations, making it impossible to swap infrastructure without modifying application code.
  2. Testability: Tests must use real database models/repositories instead of domain-level mocks.
  3. Spec violation: Clean architecture requires the application layer to depend on abstractions, not concrete implementations.

Steps to Reproduce (Code Analysis)

  1. Open any of the listed service files
  2. Observe direct imports from cleveragents.infrastructure.database.models, cleveragents.infrastructure.database.repositories, or cleveragents.infrastructure.sandbox.*

Code Locations

See the list above. Most critical violations:

  • src/cleveragents/application/services/tool_registry_service.py (lines 14-16)
  • src/cleveragents/application/services/audit_service.py (line 54)
  • src/cleveragents/application/services/plan_apply_service.py (line 38)
  • src/cleveragents/application/services/resource_handler_service.py (lines 37-38)
  1. Define Protocol interfaces in the domain layer for infrastructure concerns (e.g., ToolRepositoryProtocol, SandboxManagerProtocol).
  2. Inject concrete implementations via the DI container.
  3. Application services should only import from cleveragents.domain.* and cleveragents.application.*.

Note: Some imports (e.g., UnitOfWork, EventBus protocol, DomainEvent) are acceptable as they represent cross-cutting infrastructure concerns that are already abstracted. The violations listed above are for concrete model classes and repository implementations.

Subtasks

  • Audit all files in src/cleveragents/application/services/ for direct cleveragents.infrastructure.* imports
  • Define ToolRepositoryProtocol and ValidationAttachmentRepositoryProtocol in cleveragents.domain
  • Define SessionRepositoryProtocol and SessionMessageRepositoryProtocol in cleveragents.domain
  • Define AuditLogRepositoryProtocol in cleveragents.domain (replace direct model/Base imports)
  • Define SandboxManagerProtocol and CheckpointManagerProtocol in cleveragents.domain
  • Refactor tool_registry_service.py to depend on domain protocols, injected via DI
  • Refactor session_service.py to depend on domain protocols, injected via DI
  • Refactor audit_service.py to depend on domain protocols, injected via DI
  • Refactor repo_indexing_service.py to depend on domain protocols, injected via DI
  • Refactor resource_registry_service.py to depend on domain protocols, injected via DI
  • Refactor lock_service.py to depend on domain protocols, injected via DI
  • Refactor skill_registry_service.py to depend on domain protocols, injected via DI
  • Refactor plan_apply_service.py to depend on domain protocols, injected via DI
  • Refactor subplan_merge_service.py to depend on domain protocols, injected via DI
  • Refactor resource_handler_service.py to depend on domain protocols, injected via DI
  • Register concrete infrastructure implementations in the DI container for all new protocols
  • Tests (Behave): Add/update scenarios verifying application services accept protocol abstractions
  • Tests (Robot): Add integration test verifying DI container resolves all refactored services
  • Verify coverage >= 97% via nox -s coverage_report
  • Run nox (all default sessions), fix any errors

Definition of Done

  • All subtasks above are completed and checked off.
  • No cleveragents.infrastructure.* imports remain in src/cleveragents/application/services/ (except via DI container wiring).
  • All application services depend only on cleveragents.domain.* and cleveragents.application.* abstractions.
  • Concrete infrastructure implementations are registered and injected via the DI container.
  • A Git commit is created where the first line of the commit message matches the Commit Message in Metadata exactly, 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.
  • The commit is submitted as a pull request to master, reviewed, and merged before this issue is marked done.
  • All nox stages pass
  • Coverage >= 97%

Backlog note: This issue was discovered during autonomous operation
on milestone v3.2.0. It does not block milestone completion and has been
placed in the backlog for human review and future milestone assignment.


Automated by CleverAgents Bot
Supervisor: UAT Testing | Agent: ca-new-issue-creator

## Metadata - **Branch**: `fix/app-services-infrastructure-import-boundary-violation` - **Commit Message**: `fix(application): replace direct infrastructure imports with domain protocol abstractions` - **Milestone**: None (backlog — see note below) - **Parent Epic**: #397 ## Bug Report **Feature Area:** Dependency Injection and Service Layer **Severity:** Medium — violates clean architecture layer boundaries; application layer should depend on abstractions (protocols/interfaces), not concrete infrastructure implementations ### What Was Tested Code-level analysis of imports in `src/cleveragents/application/services/` against clean architecture principles specified in the project specification. ### Expected Behavior (from spec) Per the specification, the project follows **Clean Architecture** with clear layer boundaries. The Application Layer should depend on **domain abstractions** (protocols/interfaces), not on concrete infrastructure implementations. Infrastructure concerns (database models, concrete repositories, sandbox implementations) should be injected via DI, not imported directly. The spec states: "The Infrastructure layer contains components like the LSP Runtime" and the architecture emphasizes "separation of concerns and clear boundaries between layers." ### Actual Behavior Multiple application services directly import concrete infrastructure classes, bypassing the abstraction layer: **Direct infrastructure model imports (should use domain models or protocols):** - `tool_registry_service.py` line 14: `from cleveragents.infrastructure.database.repositories import ToolRegistryRepository, ValidationAttachmentRepository` - `session_service.py` lines 30-31: `from cleveragents.infrastructure.database.repositories import SessionMessageRepository, SessionRepository` - `audit_service.py` line 54: `from cleveragents.infrastructure.database.models import AuditLogModel, Base` - `repo_indexing_service.py` line 41: `from cleveragents.infrastructure.database.models import ...` - `resource_registry_service.py` line 80: `from cleveragents.infrastructure.database.models import ...` - `lock_service.py` line 40: `from cleveragents.infrastructure.database.models import LockModel` - `skill_registry_service.py` line 16: `from cleveragents.infrastructure.database.repositories import ...` **Direct infrastructure sandbox imports:** - `plan_apply_service.py` line 38: `from cleveragents.infrastructure.sandbox.checkpoint import CheckpointManager` - `subplan_merge_service.py` line 20: `from cleveragents.infrastructure.sandbox.merge import ...` - `resource_handler_service.py` lines 37-38: `from cleveragents.infrastructure.sandbox.factory import SandboxStrategyStr` and `from cleveragents.infrastructure.sandbox.manager import SandboxManager` ### Impact 1. **Layer boundary violation**: Application services are tightly coupled to infrastructure implementations, making it impossible to swap infrastructure without modifying application code. 2. **Testability**: Tests must use real database models/repositories instead of domain-level mocks. 3. **Spec violation**: Clean architecture requires the application layer to depend on abstractions, not concrete implementations. ### Steps to Reproduce (Code Analysis) 1. Open any of the listed service files 2. Observe direct imports from `cleveragents.infrastructure.database.models`, `cleveragents.infrastructure.database.repositories`, or `cleveragents.infrastructure.sandbox.*` ### Code Locations See the list above. Most critical violations: - `src/cleveragents/application/services/tool_registry_service.py` (lines 14-16) - `src/cleveragents/application/services/audit_service.py` (line 54) - `src/cleveragents/application/services/plan_apply_service.py` (line 38) - `src/cleveragents/application/services/resource_handler_service.py` (lines 37-38) ### Recommended Fix 1. Define Protocol interfaces in the domain layer for infrastructure concerns (e.g., `ToolRepositoryProtocol`, `SandboxManagerProtocol`). 2. Inject concrete implementations via the DI container. 3. Application services should only import from `cleveragents.domain.*` and `cleveragents.application.*`. Note: Some imports (e.g., `UnitOfWork`, `EventBus` protocol, `DomainEvent`) are acceptable as they represent cross-cutting infrastructure concerns that are already abstracted. The violations listed above are for concrete model classes and repository implementations. ## Subtasks - [ ] Audit all files in `src/cleveragents/application/services/` for direct `cleveragents.infrastructure.*` imports - [ ] Define `ToolRepositoryProtocol` and `ValidationAttachmentRepositoryProtocol` in `cleveragents.domain` - [ ] Define `SessionRepositoryProtocol` and `SessionMessageRepositoryProtocol` in `cleveragents.domain` - [ ] Define `AuditLogRepositoryProtocol` in `cleveragents.domain` (replace direct model/Base imports) - [ ] Define `SandboxManagerProtocol` and `CheckpointManagerProtocol` in `cleveragents.domain` - [ ] Refactor `tool_registry_service.py` to depend on domain protocols, injected via DI - [ ] Refactor `session_service.py` to depend on domain protocols, injected via DI - [ ] Refactor `audit_service.py` to depend on domain protocols, injected via DI - [ ] Refactor `repo_indexing_service.py` to depend on domain protocols, injected via DI - [ ] Refactor `resource_registry_service.py` to depend on domain protocols, injected via DI - [ ] Refactor `lock_service.py` to depend on domain protocols, injected via DI - [ ] Refactor `skill_registry_service.py` to depend on domain protocols, injected via DI - [ ] Refactor `plan_apply_service.py` to depend on domain protocols, injected via DI - [ ] Refactor `subplan_merge_service.py` to depend on domain protocols, injected via DI - [ ] Refactor `resource_handler_service.py` to depend on domain protocols, injected via DI - [ ] Register concrete infrastructure implementations in the DI container for all new protocols - [ ] Tests (Behave): Add/update scenarios verifying application services accept protocol abstractions - [ ] Tests (Robot): Add integration test verifying DI container resolves all refactored services - [ ] Verify coverage >= 97% via `nox -s coverage_report` - [ ] Run `nox` (all default sessions), fix any errors ## Definition of Done - [ ] All subtasks above are completed and checked off. - [ ] No `cleveragents.infrastructure.*` imports remain in `src/cleveragents/application/services/` (except via DI container wiring). - [ ] All application services depend only on `cleveragents.domain.*` and `cleveragents.application.*` abstractions. - [ ] Concrete infrastructure implementations are registered and injected via the DI container. - [ ] A Git commit is created where the **first line** of the commit message matches the Commit Message in Metadata exactly, 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. - [ ] The commit is submitted as a **pull request** to `master`, reviewed, and **merged** before this issue is marked done. - All nox stages pass - Coverage >= 97% > **Backlog note:** This issue was discovered during autonomous operation > on milestone v3.2.0. It does not block milestone completion and has been > placed in the backlog for human review and future milestone assignment. --- **Automated by CleverAgents Bot** Supervisor: UAT Testing | Agent: ca-new-issue-creator
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.

Blocks
#397 Epic: Server & Autonomy Infrastructure
cleveragents/cleveragents-core
Reference
cleveragents/cleveragents-core#3924
No description provided.