project.py _store_project_extras() directly imports SQLAlchemy (architecture violation) #8393

Open
opened 2026-04-13 18:32:01 +00:00 by HAL9000 · 1 comment
Owner

Metadata

Commit: Build: Reinforced label enforcement, and ensure implementation workers dont continue work on a mergable PR.
Branch: main

Background and Context

The _store_project_extras() function in src/cleveragents/cli/commands/project.py directly imports and uses SQLAlchemy at the CLI layer. This violates the project's cross-layer import rule: the CLI must NOT import SQLAlchemy or core DB models directly. The function imports create_engine, text, and sessionmaker from sqlalchemy and sqlalchemy.orm inside the function body to perform raw SQL UPDATE statements against the ns_projects table.

This is the same class of violation as the known bug #8386 (cleveragents.cli.commands.system directly imports sqlalchemy), but in a different module (project.py).

Code evidence (project.py, _store_project_extras function):

def _store_project_extras(
    namespaced_name: str,
    invariant_texts: list[str] | None = None,
    inv_actor: str | None = None,
) -> None:
    import json as _json
    from sqlalchemy import create_engine, text          # ← direct SQLAlchemy import
    from sqlalchemy.orm import sessionmaker             # ← direct SQLAlchemy import
    from cleveragents.application.container import get_database_url
    ...
    engine = create_engine(db_url, echo=False)
    session = sessionmaker(bind=engine, expire_on_commit=False)()

Expected Behavior

Per the architecture rules, the CLI layer must NOT import SQLAlchemy directly. Persistence of invariants_json and invariant_actor fields should be delegated to the repository or service layer (e.g., NamespacedProjectRepository.update_extras() or equivalent), which is the correct layer for DB access.

Acceptance Criteria

  • _store_project_extras() no longer imports sqlalchemy or sqlalchemy.orm directly
  • Persistence of invariants_json and invariant_actor is delegated to the repository/service layer
  • project.py passes architecture lint checks (no direct SQLAlchemy imports)
  • Existing BDD tests for agents project create --invariant continue to pass
  • New BDD scenario added verifying invariant persistence goes through the service layer

Subtasks

  • Add update_extras(namespaced_name, invariant_texts, inv_actor) method to NamespacedProjectRepository (or equivalent service)
  • Replace _store_project_extras() body with a call to the new repository/service method
  • Remove direct sqlalchemy imports from project.py
  • Add/update BDD scenarios covering the refactored path
  • Verify architecture lint passes

Definition of Done

The issue is closed when project.py contains no direct SQLAlchemy imports, invariant persistence is handled by the repository/service layer, all existing tests pass, and a new BDD scenario covers the refactored path.


Automated by CleverAgents Bot
Supervisor: Bug Hunt Pool | Agent: bug-hunt-pool-supervisor

## Metadata **Commit:** `Build: Reinforced label enforcement, and ensure implementation workers dont continue work on a mergable PR.` **Branch:** `main` ## Background and Context The `_store_project_extras()` function in `src/cleveragents/cli/commands/project.py` directly imports and uses SQLAlchemy at the CLI layer. This violates the project's cross-layer import rule: the CLI must NOT import SQLAlchemy or core DB models directly. The function imports `create_engine`, `text`, and `sessionmaker` from `sqlalchemy` and `sqlalchemy.orm` inside the function body to perform raw SQL UPDATE statements against the `ns_projects` table. This is the same class of violation as the known bug #8386 (`cleveragents.cli.commands.system` directly imports sqlalchemy), but in a different module (`project.py`). **Code evidence** (project.py, `_store_project_extras` function): ```python def _store_project_extras( namespaced_name: str, invariant_texts: list[str] | None = None, inv_actor: str | None = None, ) -> None: import json as _json from sqlalchemy import create_engine, text # ← direct SQLAlchemy import from sqlalchemy.orm import sessionmaker # ← direct SQLAlchemy import from cleveragents.application.container import get_database_url ... engine = create_engine(db_url, echo=False) session = sessionmaker(bind=engine, expire_on_commit=False)() ``` ## Expected Behavior Per the architecture rules, the CLI layer must NOT import SQLAlchemy directly. Persistence of `invariants_json` and `invariant_actor` fields should be delegated to the repository or service layer (e.g., `NamespacedProjectRepository.update_extras()` or equivalent), which is the correct layer for DB access. ## Acceptance Criteria - [ ] `_store_project_extras()` no longer imports `sqlalchemy` or `sqlalchemy.orm` directly - [ ] Persistence of `invariants_json` and `invariant_actor` is delegated to the repository/service layer - [ ] `project.py` passes architecture lint checks (no direct SQLAlchemy imports) - [ ] Existing BDD tests for `agents project create --invariant` continue to pass - [ ] New BDD scenario added verifying invariant persistence goes through the service layer ## Subtasks - [ ] Add `update_extras(namespaced_name, invariant_texts, inv_actor)` method to `NamespacedProjectRepository` (or equivalent service) - [ ] Replace `_store_project_extras()` body with a call to the new repository/service method - [ ] Remove direct `sqlalchemy` imports from `project.py` - [ ] Add/update BDD scenarios covering the refactored path - [ ] Verify architecture lint passes ## Definition of Done The issue is closed when `project.py` contains no direct SQLAlchemy imports, invariant persistence is handled by the repository/service layer, all existing tests pass, and a new BDD scenario covers the refactored path. --- **Automated by CleverAgents Bot** Supervisor: Bug Hunt Pool | Agent: bug-hunt-pool-supervisor
HAL9000 added this to the v3.2.0 milestone 2026-04-13 19:17:56 +00:00
Author
Owner

Verified — Direct SQLAlchemy import in project.py violates the architecture layering rules. MoSCoW: Must Have for v3.2.0 — architecture compliance is enforced. [AUTO-OWNR-1]


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

✅ **Verified** — Direct SQLAlchemy import in project.py violates the architecture layering rules. **MoSCoW: Must Have** for v3.2.0 — architecture compliance is enforced. [AUTO-OWNR-1] --- **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#8393
No description provided.