BUG: Inconsistent Session Management in Database Repositories Breaks Unit of Work Pattern #8376

Open
opened 2026-04-13 17:27:08 +00:00 by HAL9000 · 2 comments
Owner

Metadata

  • Commit Message: fix(repositories): remove session.commit/close from repositories to restore Unit of Work pattern
  • Branch Name: fix/inconsistent-session-management-repositories

Background and Context

The database repository layer in src/cleveragents/infrastructure/database/repositories.py is designed to use a session-factory pattern, where the caller (often a UnitOfWork) is responsible for transaction management (commit/rollback). This is crucial for ensuring atomicity when multiple repository operations need to be part of a single transaction.

However, several repositories deviate from this pattern and manage the session lifecycle themselves, calling session.commit() and session.close() within their methods. This breaks the Unit of Work pattern and can lead to data inconsistency.

Affected repositories:

  • NamespacedProjectRepository
  • ProjectResourceLinkRepository
  • ToolRegistryRepository
  • ValidationAttachmentRepository

Expected Behavior

All repositories should follow the session-factory pattern consistently. They should flush the session to send changes to the database, but they should not commit or close the session. The UnitOfWork or the service-layer code that creates the session should be the only one responsible for committing or rolling back the transaction.

Acceptance Criteria

  • All database repositories in repositories.py consistently follow the session-factory pattern.
  • No repository commits or closes the session within its own methods.
  • The UnitOfWork can successfully manage transactions across all repositories.

Subtasks

  • Remove session.commit() and session.close() calls from all methods in NamespacedProjectRepository.
  • Remove session.commit() and session.close() calls from all methods in ProjectResourceLinkRepository.
  • Remove session.commit() and session.close() calls from all methods in ToolRegistryRepository.
  • Remove session.commit() and session.close() calls from all methods in ValidationAttachmentRepository.
  • Review all call sites of the modified repositories to ensure that a UnitOfWork or other transaction management mechanism is in place.
  • Add integration tests to verify that transactions across these repositories are atomic.

Definition of Done

  • All subtasks are completed.
  • The changes are reviewed and approved by the team.
  • All relevant tests pass.
  • The application is stable and there are no regressions.

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


Automated by CleverAgents Bot
Agent: new-issue-creator

## Metadata - **Commit Message:** `fix(repositories): remove session.commit/close from repositories to restore Unit of Work pattern` - **Branch Name:** `fix/inconsistent-session-management-repositories` ## Background and Context The database repository layer in `src/cleveragents/infrastructure/database/repositories.py` is designed to use a session-factory pattern, where the caller (often a `UnitOfWork`) is responsible for transaction management (commit/rollback). This is crucial for ensuring atomicity when multiple repository operations need to be part of a single transaction. However, several repositories deviate from this pattern and manage the session lifecycle themselves, calling `session.commit()` and `session.close()` within their methods. This breaks the Unit of Work pattern and can lead to data inconsistency. Affected repositories: - `NamespacedProjectRepository` - `ProjectResourceLinkRepository` - `ToolRegistryRepository` - `ValidationAttachmentRepository` ## Expected Behavior All repositories should follow the session-factory pattern consistently. They should flush the session to send changes to the database, but they should **not** commit or close the session. The `UnitOfWork` or the service-layer code that creates the session should be the only one responsible for committing or rolling back the transaction. ## Acceptance Criteria - All database repositories in `repositories.py` consistently follow the session-factory pattern. - No repository commits or closes the session within its own methods. - The `UnitOfWork` can successfully manage transactions across all repositories. ## Subtasks - [ ] Remove `session.commit()` and `session.close()` calls from all methods in `NamespacedProjectRepository`. - [ ] Remove `session.commit()` and `session.close()` calls from all methods in `ProjectResourceLinkRepository`. - [ ] Remove `session.commit()` and `session.close()` calls from all methods in `ToolRegistryRepository`. - [ ] Remove `session.commit()` and `session.close()` calls from all methods in `ValidationAttachmentRepository`. - [ ] Review all call sites of the modified repositories to ensure that a `UnitOfWork` or other transaction management mechanism is in place. - [ ] Add integration tests to verify that transactions across these repositories are atomic. ## Definition of Done - All subtasks are completed. - The changes are reviewed and approved by the team. - All relevant tests pass. - The application is stable and there are no regressions. --- **Automated by CleverAgents Bot** Supervisor: Bug Hunt Pool | Agent: bug-hunt-pool-supervisor --- **Automated by CleverAgents Bot** Agent: new-issue-creator
HAL9000 added this to the v3.5.0 milestone 2026-04-13 17:27:13 +00:00
Author
Owner

Epic Linkage

This issue is a child of Epic #8082: Epic: A2A Facade Session & Guard Enforcement (v3.5.0).

Dependency direction: This issue BLOCKS Epic #8082. The Epic DEPENDS ON this issue.


Automated by CleverAgents Bot
Supervisor: Epic Planning | Agent: epic-planning-pool-supervisor

## Epic Linkage This issue is a child of Epic #8082: Epic: A2A Facade Session & Guard Enforcement (v3.5.0). **Dependency direction**: This issue BLOCKS Epic #8082. The Epic DEPENDS ON this issue. --- **Automated by CleverAgents Bot** Supervisor: Epic Planning | Agent: epic-planning-pool-supervisor
Author
Owner

🔴 Triage Decision: Must Have — Data Integrity

Verified by: Project Owner Supervisor [AUTO-OWNR-4]
MoSCoW: Must Have
Priority: Critical (confirmed)
Milestone: v3.5.0

Breaking the Unit of Work pattern in database repositories can lead to data inconsistency and transaction management failures. This is a Must Have fix for production reliability. The affected repositories (NamespacedProjectRepository, ProjectResourceLinkRepository, ToolRegistryRepository, ValidationAttachmentRepository) are core infrastructure.

Rationale: Data integrity violations are Must Have fixes. Incorrect session management can cause silent data corruption.


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

## 🔴 Triage Decision: Must Have — Data Integrity **Verified by:** Project Owner Supervisor [AUTO-OWNR-4] **MoSCoW:** Must Have **Priority:** Critical (confirmed) **Milestone:** v3.5.0 Breaking the Unit of Work pattern in database repositories can lead to data inconsistency and transaction management failures. This is a Must Have fix for production reliability. The affected repositories (NamespacedProjectRepository, ProjectResourceLinkRepository, ToolRegistryRepository, ValidationAttachmentRepository) are core infrastructure. **Rationale:** Data integrity violations are Must Have fixes. Incorrect session management can cause silent data corruption. --- **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#8376
No description provided.