Files
praxis/docs/praxis-implementation-blueprint.md
T
kiskaadee 8f7297c0e3 docs(blueprint): add Phase 0 implementation blueprint for Python project
architecture

- Translate SRS v1.0.2 and Domain Model v1.0.1 into concrete Python
  design choices
- Define package structure, strict dependency rules, and dataclass
  immutability patterns.
- Document resolutions for Phase 0 ambiguities (metric field rules,
  completion policy, snapshotting)
- Specify repository port protocols, policy interfaces, and the phased
  implementation roadmap.
2026-09-24 15:52:23 -05:00

23 KiB

title, description, version, status, date, tags
title description version status date tags
Implementation Blueprint Translates the approved SRS (v1.0.2) and Domain Model (v1.0.1) into concrete Python implementation decisions. Records all design choices made during the Phase 0 implementation-readiness pass. 0.1.0 Active 2026-09-24
implementation
blueprint
architecture
python

Implementation Blueprint

Translates the approved SRS v1.0.2 and Domain Model v1.0.1 into concrete Python implementation decisions. This document records how the approved model will be represented in Python. It does not redefine the domain.

Document Attribute Specification
Document Version 0.1.0
Status Active
Last Updated 2026-09-24
SRS Baseline praxis-srs-v1.0.2.md
Domain Model Baseline praxis-domain-model-v1.0.1.md

Important

Document purpose: Every decision recorded here was made to resolve an implementation ambiguity from the approved specs. If a decision here conflicts with SRS or Domain Model, the specs take precedence and this document must be updated.


1. Package Structure

src/praxis/
├── domain/
│   ├── __init__.py
│   ├── aggregates/
│   │   ├── __init__.py
│   │   ├── exercise.py          # Exercise aggregate root
│   │   ├── routine.py           # Routine + WorkoutDay + ExercisePrescription + SessionActivity + Phase
│   │   └── workout_session.py   # WorkoutSession + ExercisePerformance + SetPerformance + ActivityPerformance
│   ├── value_objects/
│   │   ├── __init__.py
│   │   ├── identifiers.py       # UserId, ExerciseId, RoutineId, SessionId, PrescriptionId, …
│   │   ├── measurement.py       # Weight, Duration, Distance, RIR, RepRange
│   │   ├── enums.py             # Weekday, DayKind, MetricType, SessionStatus, MuscleGroup, Equipment, Unit
│   │   └── snapshot.py          # SessionPrescriptionSnapshot, PrescriptionSnapshotItem, ActivitySnapshotItem, SubstitutionContext
│   ├── policies/
│   │   ├── __init__.py
│   │   ├── completion_policy.py     # CompletionPolicy, CompletionResult
│   │   ├── metric_validation.py     # MetricValidationPolicy (governs SetPerformance creation)
│   │   ├── workload_calculator.py   # WorkloadCalculator, Workload result type
│   │   └── adherence_calculator.py  # AdherenceCalculator, AdherenceResult
│   └── factories/
│       ├── __init__.py
│       └── session_snapshot_factory.py  # SessionSnapshotFactory
│
├── application/
│   ├── __init__.py
│   ├── ports/
│   │   ├── __init__.py
│   │   ├── repositories.py      # RoutineRepository, WorkoutSessionRepository, ExerciseRepository, UserRepository (protocols)
│   │   ├── auth_provider.py     # IdentityAdapter protocol
│   │   └── clock.py             # ClockPort protocol
│   ├── use_cases/
│   │   ├── __init__.py
│   │   ├── routine/
│   │   │   ├── create_routine.py
│   │   │   └── activate_routine.py
│   │   ├── session/
│   │   │   ├── initiate_workout.py
│   │   │   ├── record_set.py
│   │   │   ├── remove_set.py
│   │   │   ├── substitute_exercise.py
│   │   │   ├── add_ad_hoc_exercise.py
│   │   │   ├── record_activity.py
│   │   │   ├── finish_workout.py
│   │   │   └── skip_workout.py
│   │   └── query/
│   │       ├── query_weekly_calendar.py
│   │       ├── query_exercise_history.py
│   │       └── calculate_adherence.py
│   └── dtos/
│       ├── __init__.py
│       ├── routine_dtos.py
│       ├── session_dtos.py
│       └── exercise_dtos.py
│
└── infrastructure/           # Phase 3 — not started yet
    ├── adapters/
    └── persistence/

tests/
├── unit/
│   ├── domain/
│   │   ├── test_value_objects.py
│   │   ├── test_exercise.py
│   │   ├── test_routine.py
│   │   ├── test_workout_session.py
│   │   ├── test_session_snapshot_factory.py
│   │   ├── test_completion_policy.py
│   │   ├── test_metric_validation.py
│   │   ├── test_workload_calculator.py
│   │   └── test_adherence_calculator.py
│   └── application/
│       └── (use case tests against in-memory fakes — Phase 2)
└── integration/
    └── (database adapter tests — Phase 3)

2. Dependency Direction

tests/unit/domain/          → domain/ only (no mocks, no fakes)
tests/unit/application/     → application/ + domain/ (in-memory fakes for ports)
tests/integration/          → infrastructure/ (real DB)

interfaces/fastapi/         → application/use_cases/ (Phase 4)
application/use_cases/      → domain/ + application/ports/
domain/                     → stdlib only (dataclasses, enum, uuid, datetime, decimal)
infrastructure/adapters/    → application/ports/ (implements them)

Absolute prohibition: domain/ must not import from application/, infrastructure/, or any third-party library.


3. Dataclass & Immutability Strategy

Value Objects → @dataclass(frozen=True)

All value objects are frozen dataclasses. They cannot be mutated after construction. Equality is structural (all fields must match).

from dataclasses import dataclass
from decimal import Decimal
from enum import Enum

@dataclass(frozen=True)
class Weight:
    value: Decimal
    unit: Unit  # KG | LBS

    def __post_init__(self) -> None:
        if self.value < Decimal("0"):
            raise ValueError("Weight cannot be negative")

    def to_kg(self) -> Decimal:
        if self.unit == Unit.KG:
            return self.value
        return (self.value * Decimal("0.453592")).quantize(Decimal("0.001"))

Entities (non-root) → @dataclass(eq=False)

Entities have identity, not structural equality. eq=False disables auto-generated __eq__ so the default identity comparison (is) applies unless an explicit __eq__ based on id is added.

@dataclass(eq=False)
class SetPerformance:
    id: SetPerformanceId
    actual_reps: int | None
    actual_weight: Weight | None
    actual_duration: Duration | None
    reps_in_reserve: RIR | None
    notes: str

    def __eq__(self, other: object) -> bool:
        if not isinstance(other, SetPerformance):
            return NotImplemented
        return self.id == other.id

Aggregate Roots → @dataclass(eq=False)

Same pattern as entities. Internal mutable state (list of sets, session status) is held in regular Python lists and primitive fields.

SessionPrescriptionSnapshot → @dataclass(frozen=True) with tuple children

Because the snapshot is a value object containing lists, use tuple (not list) for exercise_prescriptions and activities to guarantee physical immutability.

@dataclass(frozen=True)
class SessionPrescriptionSnapshot:
    routine_id: RoutineId
    routine_name: str
    phase_name: str
    exercise_prescriptions: tuple[PrescriptionSnapshotItem, ...]
    activities: tuple[ActivitySnapshotItem, ...]

4. ID Representation

Decision: All IDs are newtype-style frozen dataclasses wrapping uuid.UUID.

import uuid
from dataclasses import dataclass

@dataclass(frozen=True)
class ExerciseId:
    value: uuid.UUID

    @classmethod
    def generate(cls) -> "ExerciseId":
        return cls(uuid.uuid4())

    def __str__(self) -> str:
        return str(self.value)

Rationale: Strong typing prevents passing a RoutineId where an ExerciseId is expected. Wrapping uuid.UUID (not raw str) gives canonical comparison semantics and clean serialisation.

All IDs are defined in domain/value_objects/identifiers.py.


5. Enum Strategy

All enums inherit from both str and Enum for convenient serialization:

from enum import Enum

class MetricType(str, Enum):
    REPS_WEIGHT = "RepsWeight"
    DURATION = "Duration"
    DISTANCE_DURATION = "DistanceDuration"

class SessionStatus(str, Enum):
    IN_PROGRESS = "In_Progress"
    COMPLETED = "Completed"
    PARTIALLY_COMPLETED = "Partially_Completed"
    SKIPPED = "Skipped"

6. Exception Strategy

Domain exceptions live in domain/exceptions.py. No third-party libraries.

class PraxisDomainError(Exception):
    """Base class for all domain rule violations."""

class SessionFinalizedError(PraxisDomainError):
    """Raised when a mutation is attempted on a finalized WorkoutSession."""

class InvalidMetricDataError(PraxisDomainError):
    """Raised when SetPerformance data does not match the exercise MetricType."""

class SubstitutionNotAllowedError(PraxisDomainError):
    """Raised when substitution is attempted after sets have been logged."""

class PerformanceNotFoundError(PraxisDomainError):
    """Raised when performance_id is not found within the session."""

Application-level errors (e.g. "routine not found") are separate and live in application/exceptions.py.


7. Clock / Time Abstraction

Decision: Inject a ClockPort protocol into use cases. Never call datetime.now() directly in domain or application code.

# application/ports/clock.py
from typing import Protocol
from datetime import datetime, date

class ClockPort(Protocol):
    def now(self) -> datetime: ...
    def today(self) -> date: ...

Domain factories and aggregate methods that need the current time receive now: datetime as an argument, not the clock directly. The use case resolves the clock and passes the concrete value down.


8. Aggregate Construction

Aggregate roots are constructed via factory classmethods, not bare __init__ calls, to make intent explicit and enforce invariants at construction:

@classmethod
def create(
    cls,
    owner_id: UserId,
    name: str,
    schedule: dict[Weekday, WorkoutDay],
    phases: list[Phase],
) -> "Routine":
    if not name.strip():
        raise ValueError("Routine name must not be blank")
    return cls(
        id=RoutineId.generate(),
        owner_id=owner_id,
        name=name,
        is_active=False,
        activated_at=None,
        phases=phases,
        schedule=schedule,
    )

SessionSnapshotFactory is a standalone domain service (not a method on Routine) so that it can cross aggregate boundaries under domain control without coupling the aggregates to each other.


9. Resolved Pre-Implementation Ambiguities

9.1. SetPerformance — Metric Field Requirements (Decision)

The domain model lists actual_reps, actual_weight, actual_duration, and the SRS workload rules reference actual_distance. The DistanceDuration metric type is handled via ActivityPerformance.actual_distance, not SetPerformance.

Decision:

Metric Type Required on SetPerformance Optional on SetPerformance
RepsWeight actual_reps (int ≥ 0) actual_weight (may be bodyweight = null)
Duration actual_duration (Duration) —
DistanceDuration at least one of actual_duration or actual_distance the other

actual_distance is added as Distance | None to SetPerformance to handle interval-style distance sets (e.g., 400m sprints logged as a set). ActivityPerformance.actual_distance handles continuous cardio.

Rationale: The SRS FR-ANL-2 workload formula references distance. The domain model diagram shows Distance actual_distance on ActivityPerformance. Sprint-style distance sets are discrete efforts that fit SetPerformance. Both are supported.

The MetricValidationPolicy enforces these rules in domain/policies/metric_validation.py.


9.2. Completion Semantics — CompletionPolicy vs. SRS "Honest Effort" (Decision)

SRS FR-STAT-2 says: "An exercise is evaluated as complete when all prescribed sets have been attempted with honest effort (logging reps within target range or completing full sets short of failure with RIR/notes)."

Domain Model §6.2 reduces this to: "Exercise complete if count(valid sets logged) >= target_sets from snapshot."

Decision: The domain's CompletionPolicy uses the count-based rule exclusively. "Honest effort" is a UX concern surfaced via the Smart Completion Suggestion (FR-STAT-3), not a domain invariant.

Concretely:

  • A logged set is always counted (no effort gating in the domain).
  • The policy counts sets per performance against the snapshot's target_sets.
  • "Honest effort" language is surfaced in the suggestion UI's copy, not encoded as a predicate.

Rationale: RIR and notes are optional (FR-LOG-2). Gating completion on their presence would prevent valid sessions from completing. The count rule is deterministic, testable, and honours user autonomy (FR-LOG-3 "session autonomy").

@dataclass(frozen=True)
class CompletionResult:
    suggested_status: SessionStatus
    completed_exercises: int
    total_prescribed_exercises: int
    set_adherence_rate: Decimal  # total_logged / total_prescribed

class CompletionPolicy:
    def evaluate(self, session: "WorkoutSession") -> CompletionResult:
        ...

The domain model permits an Optional[override_status] parameter. The question is which states are valid overrides.

Decision: Legal override values are Completed and Partially_Completed only.

  • In_Progress is not a legal override (you cannot "finish" into In_Progress).
  • Skipped is not a legal finish override; use session.skip(reason) instead.
  • Passing an illegal override raises InvalidSessionStatusError(PraxisDomainError).
_LEGAL_FINISH_OVERRIDES: frozenset[SessionStatus] = frozenset({
    SessionStatus.COMPLETED,
    SessionStatus.PARTIALLY_COMPLETED,
})

This is enforced inside WorkoutSession.finish() before any state transition occurs.


9.4. Metric Snapshotting — What Gets Frozen (Decision)

The snapshot must make each session historically stable even if the live Exercise aggregate is later updated (e.g., name changed, category corrected).

Decision: PrescriptionSnapshotItem freezes:

@dataclass(frozen=True)
class PrescriptionSnapshotItem:
    prescription_id: PrescriptionId
    exercise_id: ExerciseId
    exercise_name: str          # frozen name at snapshot time
    metric_type: MetricType     # frozen metric type at snapshot time
    target_sets: int
    rep_range: RepRange | None
    target_duration: Duration | None
    target_weight: Weight | None
    is_unilateral: bool
    order_index: int
    notes: str

metric_type is explicitly snapshotted so that MetricValidationPolicy during the session uses the metric type the exercise had at the time the session started, not whatever it might be if the exercise is later recategorised.


9.5. ScheduledWorkout — Aggregate vs. Read Model (Decision)

SRS FR-CAL-1/CAL-2 references ScheduledWorkout as a projection. Domain Model §7 use case #11 (QueryWeeklyCalendarUseCase) returns projected items.

Decision: ScheduledWorkout is a read model / application DTO, not a domain aggregate.

It is never persisted independently. QueryWeeklyCalendarUseCase constructs it on the fly by:

  1. Loading the user's active Routine.
  2. Computing which WorkoutDay falls on each date in the requested window.
  3. Resolving the active Phase for each date.
  4. Merging with any existing WorkoutSession records for those dates.
  5. Returning a WeeklyCalendarView DTO.
# application/dtos/session_dtos.py
@dataclass(frozen=True)
class ScheduledWorkoutProjection:
    date: date
    weekday: Weekday
    day_kind: DayKind
    routine_id: RoutineId
    workout_day_id: WorkoutDayId
    phase_name: str
    session: WorkoutSessionSummary | None  # None if not yet initiated

Rationale: ScheduledWorkout has no identity across time, no invariants to enforce, and no lifecycle of its own. Treating it as an aggregate would require persisting it and managing its state transitions — adding complexity with no domain benefit.


10. Repository Port Shapes

Ports are declared as Protocol classes in application/ports/repositories.py. No base classes, no generics superclass — each port is explicit and minimal.

from typing import Protocol
from domain.aggregates.routine import Routine
from domain.value_objects.identifiers import UserId, RoutineId

class RoutineRepository(Protocol):
    def save(self, routine: Routine) -> None: ...
    def get_by_id(self, routine_id: RoutineId) -> Routine | None: ...
    def get_active(self, user_id: UserId) -> Routine | None: ...
    def list_for_user(self, user_id: UserId) -> list[Routine]: ...
    def delete(self, routine_id: RoutineId) -> None: ...

class WorkoutSessionRepository(Protocol):
    def save(self, session: WorkoutSession) -> None: ...
    def get_by_id(self, session_id: SessionId) -> WorkoutSession | None: ...
    def get_by_date(self, user_id: UserId, date: date) -> WorkoutSession | None: ...
    def list_for_user_in_range(self, user_id: UserId, start: date, end: date) -> list[WorkoutSession]: ...

class ExerciseRepository(Protocol):
    def save(self, exercise: Exercise) -> None: ...
    def get_by_id(self, exercise_id: ExerciseId) -> Exercise | None: ...
    def list_all(self, user_id: UserId) -> list[Exercise]: ...

class UserRepository(Protocol):
    def save(self, user: User) -> None: ...
    def get_by_id(self, user_id: UserId) -> User | None: ...
    def get_by_external_id(self, external_id: str) -> User | None: ...

11. Policy Interfaces

Domain policies are plain classes (not abstract base classes). They receive domain objects and return value-object results. They do not interact with repositories.

Policy Input Output
MetricValidationPolicy.validate(metric_type, set_data) MetricType, SetData None or raises InvalidMetricDataError
CompletionPolicy.evaluate(session) WorkoutSession CompletionResult
WorkloadCalculator.calculate(performances) list[ExercisePerformance] Workload
AdherenceCalculator.calculate(sessions, scheduled_count) list[WorkoutSession], int AdherenceResult
SessionSnapshotFactory.create(routine, workout_day, session_date, now) domain objects WorkoutSession

Policies are stateless. Use cases instantiate them directly (no dependency injection needed at this layer).


12. Testing Strategy

Order of implementation (test-first per layer)

1. Value objects & enums             → tests/unit/domain/test_value_objects.py
2. Exercise aggregate                → tests/unit/domain/test_exercise.py
3. Routine aggregate                 → tests/unit/domain/test_routine.py
4. MetricValidationPolicy            → tests/unit/domain/test_metric_validation.py
5. SessionSnapshotFactory            → tests/unit/domain/test_session_snapshot_factory.py
6. WorkoutSession aggregate          → tests/unit/domain/test_workout_session.py
7. CompletionPolicy                  → tests/unit/domain/test_completion_policy.py
8. WorkloadCalculator                → tests/unit/domain/test_workload_calculator.py
9. AdherenceCalculator               → tests/unit/domain/test_adherence_calculator.py

Domain test rules

  • No mocks. No unittest.mock. No fakes.
  • No database. No SQLite, no SQLAlchemy, no fixtures.
  • No HTTP. No FastAPI test client.
  • Pure Python construction of domain objects and assertion against their state.
  • Use pytest with pytest-cov. Coverage gate: 90% on src/praxis/domain/.

Application test rules (Phase 2)

  • Use simple in-memory fakes implementing the repository ports:
    class InMemoryRoutineRepository:
        def __init__(self) -> None:
            self._store: dict[RoutineId, Routine] = {}
        def save(self, routine: Routine) -> None:
            self._store[routine.id] = routine
        def get_by_id(self, routine_id: RoutineId) -> Routine | None:
            return self._store.get(routine_id)
        ...
    
  • Fakes live in tests/unit/application/fakes.py.
  • No mocks. Fakes implement the port Protocol fully.

13. Serialisation Boundaries

The domain layer has zero serialisation code. No to_dict(), no model_dump(), no JSON methods on domain objects.

Serialisation happens exclusively at:

  1. Application DTOs (application/dtos/) — translate domain objects into output structures.
  2. Infrastructure persistence (infrastructure/persistence/) — map domain objects to/from ORM models.
  3. Interface layer (interfaces/fastapi/) — Pydantic schemas for HTTP request/response.

This means SetPerformance has no to_dict(). The persistence adapter is responsible for knowing how to store and restore it.


14. Implementation Phase Sequence

Phase Scope Entry Criterion Done When
Phase 0 Implementation Readiness Specs baselined All ambiguities in §9 resolved (this document)
Phase 1 Pure Domain Foundation Phase 0 complete All domain tests green, ≥90% coverage, mypy clean
Phase 2 Application Layer Phase 1 complete All use case tests green with in-memory fakes
Phase 3 Infrastructure Phase 2 complete Integration tests pass with SQLite
Phase 4 FastAPI Interface Phase 3 complete OpenAPI spec generated, integration round-trips pass
Phase 5 Containerisation Phase 4 complete Docker Compose stack runs, Authelia header adapter wired

15. Open Questions (Not Blocking Phase 1)

These are deferred — they do not affect the domain layer and will be resolved in later phases.

# Question Blocking
OQ-1 WorkoutSession.routine_id is nullable for ad-hoc sessions (no routine). Should session_date uniqueness be enforced at application or DB level? Phase 2
OQ-2 Phase.end_week = None means "open-ended". What happens when get_current_phase finds no matching phase? Return last phase or raise? Phase 1 (Routine)
OQ-3 Weight unit preference — stored on User.profile or derived per-request? Phase 2
OQ-4 ExercisePrescription.target_weight = None is valid (bodyweight). How is this communicated in the snapshot / UI suggestion? Phase 4
OQ-5 Exercise catalog seeding (FR-EX-1) — migration fixture or Python data file? Phase 3

Note

OQ-2 must be resolved before implementing Routine.get_current_phase() in Phase 1. Proposed resolution: return the last defined phase (highest start_week) when the current week exceeds all end_week values. This matches the reference program's "Week 3+" semantics.