Skip to content

Refactor UserOutput to remove Arc<Mutex<>> pattern #164

Description

@josecelano

Overview

Remove the Arc<Mutex<UserOutput>> pattern throughout the codebase and replace it with direct ownership/borrowing to eliminate deadlock risks and simplify the architecture.

Specification

See detailed specification: docs/issues/164-refactor-useroutput-remove-mutex.md

📋 Background

This refactoring addresses a critical reentrancy deadlock issue discovered in CreateTemplateCommandController and implements the architectural decision documented in ADR: Remove UserOutput Mutex.

Current Problems

  • Reentrancy Deadlock: Tests hang indefinitely when UserOutput mutex is acquired through nested calls
  • Architectural Mismatch: Complex concurrency mechanisms for a sequential, single-user application
  • Timeout Workarounds: Defensive programming patterns that shouldn't be necessary
  • Complex Lock Management: Error-prone manual lock scoping throughout codebase

🏗️ Architecture Requirements

DDD Layer: Presentation (primary), Application (secondary)
Module Path: src/presentation/user_output/, command controllers, progress reporters
Pattern: Direct ownership model with mutable borrowing

Module Structure Requirements

Architectural Constraints

  • No business logic in presentation layer
  • Error handling follows project conventions (see docs/contributing/error-handling.md)
  • Testing strategy aligns with layer responsibilities

Anti-Patterns to Avoid

  • ❌ Mixing concerns across layers
  • ❌ Domain layer depending on infrastructure
  • ❌ Monolithic modules with multiple responsibilities

Implementation Plan

Phase 1: Core UserOutput Refactor

  • Remove Arc<Mutex<>> wrapper from UserOutput
  • Update ExecutionContext to own UserOutput directly
  • Modify command signatures to accept &mut UserOutput
  • Remove timeout mechanisms and deadlock prevention code

Phase 2: ProgressReporter Simplification

  • Refactor ProgressReporter to use direct &mut UserOutput access
  • Remove mutex-related error handling in progress reporting
  • Simplify progress update methods

Phase 3: Command Controller Updates

  • Update all command controllers to use &mut UserOutput
  • Remove scoped lock management code
  • Simplify error handling paths

Phase 4: Test Updates

  • Remove mutex mocking in tests
  • Update test fixtures to use direct UserOutput
  • Verify no deadlock scenarios can occur in new architecture

Acceptance Criteria

Note for Contributors: These criteria define what the PR reviewer will check. Use this as your pre-review checklist before submitting the PR to minimize back-and-forth iterations.

Quality Checks:

  • Pre-commit checks pass: ./scripts/pre-commit.sh

Architecture Validation:

  • Zero Arc<Mutex<UserOutput>> usage in codebase
  • All command handlers use &mut UserOutput or owned UserOutput
  • No mutex-related timeout or deadlock prevention code
  • ProgressReporter uses direct access patterns

Functional Validation:

  • All existing functionality preserved (user experience unchanged)
  • All tests pass with no mutex mocking
  • E2E tests demonstrate no deadlock scenarios
  • Performance improved (no lock contention overhead)

Documentation:

  • ADR implementation section updated with results
  • Code comments explain new ownership patterns
  • Any breaking changes documented

Related Issues

References

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions