[AUTO-GUARD] Direct database access in resource.py CLI commands #6746

Closed
opened 2026-04-10 00:57:14 +00:00 by HAL9000 · 0 comments
Owner

The resource_add and resource_remove commands in src/cleveragents/cli/commands/resource.py directly access the database session to perform queries and deletions. This is a violation of the repository pattern that is used elsewhere in the codebase.

All database access should be encapsulated within repository classes in the src/cleveragents/infrastructure/database directory. The CLI commands should only interact with the ResourceRegistryService.

The following code blocks should be refactored into the ResourceRegistryService:

resource_add:

                session = service._session()
                try:
                    from cleveragents.infrastructure.database.models import (
                        ResourceEdgeModel,
                        ResourceModel,
                    )

                    # Remove edges first
                    session.query(ResourceEdgeModel).filter(
                        (ResourceEdgeModel.parent_id == existing.resource_id)
                        | (ResourceEdgeModel.child_id == existing.resource_id)
                    ).delete(synchronize_session="fetch")
                    session.query(ResourceModel).filter_by(
                        resource_id=existing.resource_id
                    ).delete(synchronize_session="fetch")
                    session.commit()
                except Exception:
                    session.rollback()
                    raise
                finally:
                    session.close()

resource_remove:

        session = service._session()
        try:
            from cleveragents.infrastructure.database.models import (
                ResourceEdgeModel,
                ResourceModel,
            )

            # Check for edges
            edge_count: int = (
                session.query(ResourceEdgeModel)
                .filter(
                    (ResourceEdgeModel.parent_id == res.resource_id)
                    | (ResourceEdgeModel.child_id == res.resource_id)
                )
                .count()
            )
            if edge_count > 0:
                console.print(
                    f"[red]Cannot remove resource '{res.name or res.resource_id}': "
                    f"{edge_count} edge(s) still reference it.[/red]"
                )
                raise typer.Abort()

            row = (
                session.query(ResourceModel)
                .filter_by(resource_id=res.resource_id)
                .first()
            )
            if row is not None:
                session.delete(row)
                session.commit()
        except typer.Abort:
            raise
        except Exception:
            session.rollback()
            raise
        finally:
            session.close()

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

The `resource_add` and `resource_remove` commands in `src/cleveragents/cli/commands/resource.py` directly access the database session to perform queries and deletions. This is a violation of the repository pattern that is used elsewhere in the codebase. All database access should be encapsulated within repository classes in the `src/cleveragents/infrastructure/database` directory. The CLI commands should only interact with the `ResourceRegistryService`. The following code blocks should be refactored into the `ResourceRegistryService`: **`resource_add`:** ```python session = service._session() try: from cleveragents.infrastructure.database.models import ( ResourceEdgeModel, ResourceModel, ) # Remove edges first session.query(ResourceEdgeModel).filter( (ResourceEdgeModel.parent_id == existing.resource_id) | (ResourceEdgeModel.child_id == existing.resource_id) ).delete(synchronize_session="fetch") session.query(ResourceModel).filter_by( resource_id=existing.resource_id ).delete(synchronize_session="fetch") session.commit() except Exception: session.rollback() raise finally: session.close() ``` **`resource_remove`:** ```python session = service._session() try: from cleveragents.infrastructure.database.models import ( ResourceEdgeModel, ResourceModel, ) # Check for edges edge_count: int = ( session.query(ResourceEdgeModel) .filter( (ResourceEdgeModel.parent_id == res.resource_id) | (ResourceEdgeModel.child_id == res.resource_id) ) .count() ) if edge_count > 0: console.print( f"[red]Cannot remove resource '{res.name or res.resource_id}': " f"{edge_count} edge(s) still reference it.[/red]" ) raise typer.Abort() row = ( session.query(ResourceModel) .filter_by(resource_id=res.resource_id) .first() ) if row is not None: session.delete(row) session.commit() except typer.Abort: raise except Exception: session.rollback() raise finally: session.close() ``` --- **Automated by CleverAgents Bot** Supervisor: Architecture Guard | Agent: architecture-guard
HAL9000 2026-04-10 20:49:02 +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#6746
No description provided.