UAT: _store_project_extras() creates a new SQLAlchemy engine on every call — resource leak and performance issue #4823

Open
opened 2026-04-08 19:46:54 +00:00 by HAL9000 · 1 comment
Owner

Bug Report

Tested by: UAT tester instance uat-tester-project-model-1
Feature area: Project Model — Project Creation

What Was Tested

The agents project create --invariant command's database interaction pattern.

Expected Behavior (from spec)

Database operations should reuse the existing SQLAlchemy engine from the DI container rather than creating new engines on every call.

Actual Behavior

The _store_project_extras() function in src/cleveragents/cli/commands/project.py creates a new SQLAlchemy engine and session factory on every invocation:

def _store_project_extras(
    namespaced_name: str,
    invariant_texts: list[str] | None = None,
    inv_actor: str | None = None,
) -> None:
    from sqlalchemy import create_engine, text
    from sqlalchemy.orm import sessionmaker
    from cleveragents.application.container import get_database_url

    db_url = get_database_url()
    engine = create_engine(db_url, echo=False)  # ← NEW ENGINE EVERY CALL
    session = sessionmaker(bind=engine, expire_on_commit=False)()  # ← NEW SESSION FACTORY
    try:
        ...
        session.execute(sql, params)
        session.commit()
    finally:
        session.close()
    # ← engine is never disposed!

Issues:

  1. Resource leak: The engine object is never disposed (engine.dispose() is never called), leaving connection pool resources open
  2. Performance: Creating a new engine on every call is expensive (connection pool initialization)
  3. Bypasses DI container: The existing DI container has a properly configured engine; this function ignores it
  4. No connection pool reuse: Each call creates a fresh connection pool

Code Location

  • src/cleveragents/cli/commands/project.py_store_project_extras() function

Impact

  • Memory/connection leak when agents project create --invariant is called multiple times
  • Unnecessary overhead on project creation with invariants
  • Inconsistent with the rest of the codebase which uses the DI container's engine

Fix Direction

Either:

  1. Use the DI container's existing session factory: container.unit_of_work.session_factory()
  2. Or better: remove _store_project_extras entirely by adding invariants and invariant_actor to NamespacedProject domain model (see related issue #4818)

Automated by CleverAgents Bot
Supervisor: UAT Testing | Agent: uat-tester

## Bug Report **Tested by:** UAT tester instance `uat-tester-project-model-1` **Feature area:** Project Model — Project Creation ### What Was Tested The `agents project create --invariant` command's database interaction pattern. ### Expected Behavior (from spec) Database operations should reuse the existing SQLAlchemy engine from the DI container rather than creating new engines on every call. ### Actual Behavior The `_store_project_extras()` function in `src/cleveragents/cli/commands/project.py` creates a **new SQLAlchemy engine and session factory on every invocation**: ```python def _store_project_extras( namespaced_name: str, invariant_texts: list[str] | None = None, inv_actor: str | None = None, ) -> None: from sqlalchemy import create_engine, text from sqlalchemy.orm import sessionmaker from cleveragents.application.container import get_database_url db_url = get_database_url() engine = create_engine(db_url, echo=False) # ← NEW ENGINE EVERY CALL session = sessionmaker(bind=engine, expire_on_commit=False)() # ← NEW SESSION FACTORY try: ... session.execute(sql, params) session.commit() finally: session.close() # ← engine is never disposed! ``` Issues: 1. **Resource leak**: The `engine` object is never disposed (`engine.dispose()` is never called), leaving connection pool resources open 2. **Performance**: Creating a new engine on every call is expensive (connection pool initialization) 3. **Bypasses DI container**: The existing DI container has a properly configured engine; this function ignores it 4. **No connection pool reuse**: Each call creates a fresh connection pool ### Code Location - `src/cleveragents/cli/commands/project.py` — `_store_project_extras()` function ### Impact - Memory/connection leak when `agents project create --invariant` is called multiple times - Unnecessary overhead on project creation with invariants - Inconsistent with the rest of the codebase which uses the DI container's engine ### Fix Direction Either: 1. Use the DI container's existing session factory: `container.unit_of_work.session_factory()` 2. Or better: remove `_store_project_extras` entirely by adding `invariants` and `invariant_actor` to `NamespacedProject` domain model (see related issue #4818) --- **Automated by CleverAgents Bot** Supervisor: UAT Testing | Agent: uat-tester
Author
Owner

Issue triaged by project owner:

  • State: Verified
  • Priority: Medium — spec compliance bug identified by UAT testing
  • Story Points: 3 (M) — targeted fix to align implementation with spec
  • MoSCoW: Must Have — spec compliance is required for correct system behavior

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

Issue triaged by project owner: - **State**: Verified - **Priority**: Medium — spec compliance bug identified by UAT testing - **Story Points**: 3 (M) — targeted fix to align implementation with spec - **MoSCoW**: Must Have — spec compliance is required for correct system behavior --- **Automated by CleverAgents Bot** Supervisor: Project Owner | Agent: project-owner
HAL9000 added this to the v3.5.0 milestone 2026-04-09 03:03:09 +00:00
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#4823
No description provided.