UAT: Production code in application/container.py imports from mocks/ test directory #3852

Open
opened 2026-04-06 06:56:08 +00:00 by freemo · 0 comments
Owner

Metadata

  • Branch: fix/remove-mock-import-from-production-container
  • Commit Message: fix(container): remove mock AI provider import from production container code
  • Milestone: Backlog
  • Parent Epic: #946

Background

The project specification and CONTRIBUTING.md strictly forbid production code from containing mock implementations or test-only logic:

"All mocking code, fakes, stubs, and other test doubles are permitted only in unit tests and must be located within the features/ directory (e.g., features/mocks/). Production code must not contain any mock implementations or test-only logic."

Violation

File: src/cleveragents/application/container.py
Lines: 196–202

features_path = Path(__file__).parent.parent.parent.parent / "features"
if features_path.exists() and str(features_path) not in sys.path:
    sys.path.insert(0, str(features_path))

from mocks.mock_ai_provider import MockAIProvider  # type: ignore

return MockAIProvider()  # type: ignore

The production DI container (container.py) directly imports MockAIProvider from the features/mocks/ test directory and returns it as the AI provider when CLEVERAGENTS_TESTING_USE_MOCK_AI=true or mock_providers=true is set. This:

  1. Violates the spec: Production code must not contain mock implementations or test-only logic.
  2. Requires # type: ignore: The import is suppressed with # type: ignore because the mock module is not part of the typed production codebase.
  3. Creates a runtime dependency on test infrastructure: The production container manipulates sys.path to reach the features/ directory.
  4. Is a security concern: The CLEVERAGENTS_TESTING_USE_MOCK_AI environment variable can accidentally activate mock behavior in production.

Steps to Reproduce

grep -n "mock_ai_provider\|MockAIProvider\|CLEVERAGENTS_TESTING_USE_MOCK_AI" src/cleveragents/application/container.py

Output:

187:    use_mock_ai = resolved_settings.mock_providers or (
188:        os.environ.get("CLEVERAGENTS_TESTING_USE_MOCK_AI", "").lower()
189:        in ("true", "1", "yes")
190:    )
191:    if use_mock_ai:
200:        from mocks.mock_ai_provider import MockAIProvider  # type: ignore
202:        return MockAIProvider()  # type: ignore

Expected Behavior

Production code in src/cleveragents/ must not import from features/mocks/ or any other test directory. If a mock AI provider is needed for testing, it should be injected via the test harness (e.g., Behave environment.py or step definitions), not baked into the production container.

Actual Behavior

The production DI container imports MockAIProvider from features/mocks/mock_ai_provider.py at runtime when a testing flag is set.

Remove the mock import from container.py. Instead:

  1. Define a proper AIProvider protocol/interface in the production codebase
  2. Have the Behave test environment inject a mock implementation via the DI container's override mechanism
  3. Remove the mock_providers setting from production Settings or move it to a test-only configuration layer

Subtasks

  • Remove from mocks.mock_ai_provider import MockAIProvider and surrounding sys.path manipulation from container.py
  • Remove or relocate the mock_providers setting from production Settings class
  • Define a proper AIProvider protocol/interface in the production codebase (if not already present)
  • Update Behave features/environment.py to inject the mock AI provider via the DI container's override mechanism
  • Verify nox -e typecheck passes with zero errors
  • Verify nox -e unit_tests passes
  • Run full nox suite and fix any errors
  • Verify coverage >= 97% via nox -s coverage_report

Definition of Done

This issue is complete when:

  • from mocks.mock_ai_provider import MockAIProvider removed from container.py
  • sys.path manipulation for features/ directory removed from container.py
  • Mock AI provider injection moved to test infrastructure (features/environment.py or similar)
  • nox -e typecheck passes with zero errors
  • nox -e unit_tests passes
  • 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.6.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/remove-mock-import-from-production-container` - **Commit Message**: `fix(container): remove mock AI provider import from production container code` - **Milestone**: Backlog - **Parent Epic**: #946 ## Background The project specification and CONTRIBUTING.md strictly forbid production code from containing mock implementations or test-only logic: > "All mocking code, fakes, stubs, and other test doubles are permitted only in unit tests and must be located within the `features/` directory (e.g., `features/mocks/`). Production code must not contain any mock implementations or test-only logic." ## Violation **File**: `src/cleveragents/application/container.py` **Lines**: 196–202 ```python features_path = Path(__file__).parent.parent.parent.parent / "features" if features_path.exists() and str(features_path) not in sys.path: sys.path.insert(0, str(features_path)) from mocks.mock_ai_provider import MockAIProvider # type: ignore return MockAIProvider() # type: ignore ``` The production DI container (`container.py`) directly imports `MockAIProvider` from the `features/mocks/` test directory and returns it as the AI provider when `CLEVERAGENTS_TESTING_USE_MOCK_AI=true` or `mock_providers=true` is set. This: 1. **Violates the spec**: Production code must not contain mock implementations or test-only logic. 2. **Requires `# type: ignore`**: The import is suppressed with `# type: ignore` because the mock module is not part of the typed production codebase. 3. **Creates a runtime dependency on test infrastructure**: The production container manipulates `sys.path` to reach the `features/` directory. 4. **Is a security concern**: The `CLEVERAGENTS_TESTING_USE_MOCK_AI` environment variable can accidentally activate mock behavior in production. ## Steps to Reproduce ```bash grep -n "mock_ai_provider\|MockAIProvider\|CLEVERAGENTS_TESTING_USE_MOCK_AI" src/cleveragents/application/container.py ``` **Output**: ``` 187: use_mock_ai = resolved_settings.mock_providers or ( 188: os.environ.get("CLEVERAGENTS_TESTING_USE_MOCK_AI", "").lower() 189: in ("true", "1", "yes") 190: ) 191: if use_mock_ai: 200: from mocks.mock_ai_provider import MockAIProvider # type: ignore 202: return MockAIProvider() # type: ignore ``` ## Expected Behavior Production code in `src/cleveragents/` must not import from `features/mocks/` or any other test directory. If a mock AI provider is needed for testing, it should be injected via the test harness (e.g., Behave `environment.py` or step definitions), not baked into the production container. ## Actual Behavior The production DI container imports `MockAIProvider` from `features/mocks/mock_ai_provider.py` at runtime when a testing flag is set. ## Recommended Fix Remove the mock import from `container.py`. Instead: 1. Define a proper `AIProvider` protocol/interface in the production codebase 2. Have the Behave test environment inject a mock implementation via the DI container's override mechanism 3. Remove the `mock_providers` setting from production `Settings` or move it to a test-only configuration layer ## Subtasks - [ ] Remove `from mocks.mock_ai_provider import MockAIProvider` and surrounding `sys.path` manipulation from `container.py` - [ ] Remove or relocate the `mock_providers` setting from production `Settings` class - [ ] Define a proper `AIProvider` protocol/interface in the production codebase (if not already present) - [ ] Update Behave `features/environment.py` to inject the mock AI provider via the DI container's override mechanism - [ ] Verify `nox -e typecheck` passes with zero errors - [ ] Verify `nox -e unit_tests` passes - [ ] Run full `nox` suite and fix any errors - [ ] Verify coverage >= 97% via `nox -s coverage_report` ## Definition of Done This issue is complete when: - [ ] `from mocks.mock_ai_provider import MockAIProvider` removed from `container.py` - [ ] `sys.path` manipulation for `features/` directory removed from `container.py` - [ ] Mock AI provider injection moved to test infrastructure (`features/environment.py` or similar) - [ ] `nox -e typecheck` passes with zero errors - [ ] `nox -e unit_tests` passes - [ ] 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.6.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.

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