UAT: ProjectService.delete_project emits domain event outside transaction — event fired after commit, not atomically #3704

Open
opened 2026-04-05 22:14:59 +00:00 by freemo · 0 comments
Owner

Bug Report

What Was Tested

src/cleveragents/application/services/project_service.py delete_project method (lines 473-494) was analyzed for correct event emission ordering.

Expected Behavior (from spec)

Domain events should be emitted atomically with the database operation, or at minimum before the transaction is committed, so that if event emission fails, the operation can be rolled back. The event should be emitted as part of the delete operation's lifecycle.

Actual Behavior (from code analysis)

delete_project emits the ENTITY_DELETED event after the transaction has already been committed and the with block has exited:

def delete_project(self, project: Project) -> None:
    with self.unit_of_work.transaction() as ctx:
        if project.id:
            ctx.projects.delete(project.id)
    # Transaction is COMMITTED here — project is already deleted
    if project.id and self._event_bus is not None:
        try:
            self._event_bus.emit(  # Event emitted AFTER commit
                DomainEvent(
                    event_type=EventType.ENTITY_DELETED,
                    ...
                )
            )
        except Exception:
            _logger.warning("audit_emit_failed", event_type="ENTITY_DELETED")

The event emission is outside the transaction block. If the event bus fails, the project is already deleted but no audit event was recorded. The except Exception: _logger.warning(...) silently swallows the failure.

Impact

  • Audit trail is unreliable: project deletions may not be recorded in the audit log
  • Event emission failure is silently swallowed — no way to detect missed audit events
  • Inconsistent with the spec's requirement for reliable event-driven audit trails
  • Compare with initialize_project which doesn't emit any event at all (no ENTITY_CREATED event)

Code Locations

  • src/cleveragents/application/services/project_service.py lines 473-494

Steps to Reproduce

  1. Review delete_project — event emission is outside the with self.unit_of_work.transaction() block
  2. Simulate event bus failure — project is deleted but no audit event is recorded

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

## Bug Report ### What Was Tested `src/cleveragents/application/services/project_service.py` `delete_project` method (lines 473-494) was analyzed for correct event emission ordering. ### Expected Behavior (from spec) Domain events should be emitted atomically with the database operation, or at minimum before the transaction is committed, so that if event emission fails, the operation can be rolled back. The event should be emitted as part of the delete operation's lifecycle. ### Actual Behavior (from code analysis) `delete_project` emits the `ENTITY_DELETED` event **after** the transaction has already been committed and the `with` block has exited: ```python def delete_project(self, project: Project) -> None: with self.unit_of_work.transaction() as ctx: if project.id: ctx.projects.delete(project.id) # Transaction is COMMITTED here — project is already deleted if project.id and self._event_bus is not None: try: self._event_bus.emit( # Event emitted AFTER commit DomainEvent( event_type=EventType.ENTITY_DELETED, ... ) ) except Exception: _logger.warning("audit_emit_failed", event_type="ENTITY_DELETED") ``` The event emission is outside the transaction block. If the event bus fails, the project is already deleted but no audit event was recorded. The `except Exception: _logger.warning(...)` silently swallows the failure. ### Impact - Audit trail is unreliable: project deletions may not be recorded in the audit log - Event emission failure is silently swallowed — no way to detect missed audit events - Inconsistent with the spec's requirement for reliable event-driven audit trails - Compare with `initialize_project` which doesn't emit any event at all (no `ENTITY_CREATED` event) ### Code Locations - `src/cleveragents/application/services/project_service.py` lines 473-494 ### Steps to Reproduce 1. Review `delete_project` — event emission is outside the `with self.unit_of_work.transaction()` block 2. Simulate event bus failure — project is deleted but no audit event is recorded --- **Automated by CleverAgents Bot** Supervisor: UAT Testing | Agent: ca-uat-tester
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#3704
No description provided.