Skip to main content

Aggregate design canvas

Synced from exception-management

This page is a copy of docs/docs/ddd/aggregate-design-canvas.md on develop, derived from that repository's code. Edit it there, then re-sync.

Two aggregates, both pure Go under internal/domain (no I/O, no clock reads; they import nothing outside internal/domain). ADR 0002 is the decision record.

ExceptionCase (internal/domain/exceptioncase)​

Aggregate root. One owned anomaly.

Canvas sectionValue
Identitycase_id (case-<uuid>, minted by system.CaseIDs at open — the real IDGen, never a test double in cmd/).
Natural key (dedup)(source type, source subject, site) — the dedup key; at most one OPEN case per key (partial unique index uq_exception_cases_one_open_per_key backs the rule across cases).
ContainsThe frozen source payload (deep copy, JSONB), the dedup key parts, status, severity, queue, assignee, escalated flag, occurrence count, openedAt/lastSeenAt, the SLA clock (slaMinutes, slaDueAt), the resolution (code, root cause, note) or the auto-resolution evidence (rule id, correlated type + event id), and the audit trail (exactly one entry per accepted transition).
Does not containAny producer state (no order copy, no task copy, no stock level — ADR 0003), any reference to re-fetch, any clock or randomness.
InvariantsOne direction only OPEN -> TRIAGED -> ASSIGNED -> RESOLVED; ESCALATED is a flag; RESOLVED requires resolution code + root cause and is terminal (ErrCaseClosed, 409 at the edge, the aggregate raises nothing); occurrence >= 1 and lastSeenAt >= openedAt; the frozen payload never changes after open; the aggregate refuses to open a second case for a key it holds open.
ConsistencyInvariants enforced immediately inside the aggregate; the cross-case one-open-per-key rule enforced by the use case plus the partial unique index (the loser of a race gets ErrOpenCaseExists); persistence guarded by optimistic concurrency (version, +1 per accepted transition, WHERE version = excluded.version - 1).
LifecycleOpen (from a consumed fact) → maybe Touch many times → Triage / Assign / reassign in any order while unresolved → Resolve (operator) or AutoResolve (policy rule) terminal. A Touch after resolution counts the occurrence and never reopens.
CommandsOpen, Touch, Triage, Assign, Reassign, Resolve, AutoResolve, BreachesAt(now) (pure query).
Events raisedExceptionOpened, ExceptionTriaged, ExceptionAssigned, ExceptionEscalated, ExceptionResolved, ExceptionAutoResolved — plain structs with OccurredAt(), no CloudEvents knowledge; Touch raises nothing on the wire.
Cross-aggregate ruleNone at the aggregate level by design: policies are read at open (use case), so a case never holds a policy reference; a new policy version governs cases opened after its declaration.

ExceptionPolicy (internal/domain/exceptionpolicy)​

Aggregate root. The handling policy of ONE source type.

Canvas sectionValue
Identity(source type, version) — versioned, append-only: Declare creates version 1, Redeclare returns the NEXT version as a new value, the receiver is never mutated, history is never rewritten.
ContainsSeverity, queue, slaMinutes, the optional AutoResolveRule{OnType, WithinMinutes} (data, never code), declaredAt.
InvariantsSource type is a full CloudEvents type and non-blank; severity valid; queue non-blank; SLAMinutes >= 1; Version >= 1; WithinMinutes >= 1 when the rule exists.
ConsistencyThe version is part of the primary key; two writers racing the same next version collide into ErrConcurrentModification (409 at the edge).
LifecycleAppend-only version chain; no update in place, no delete.
CommandsDeclare, Redeclare; the pure matcher Matches(correlatedType, subject, case, now) evaluates the rule (same subject, inclusive window now <= lastSeenAt + withinMinutes).
Events raisedExceptionPolicyDeclared (subject and Kafka key = the source type).
Why the rule is dataAn operator declares it through PUT /exception-policies/{sourceType} without a deploy, every version is auditable, and the ops-agent may READ it and recommend but never execute it (ADR 0002). The rule id carries the derived deterministic form <sourceType>-><onType>.

Shared kernel (internal/domain/shared)​

Value objects only: DedupKey, Actor, severity, CaseID/PolicyID types, and ParseWorkUnitRef — the reversal of WES's frozen work-unit formula {order_id}-line-{line_no} (order-management ADR 0018) used by the correlation.