Skip to main content

Aggregate design canvas

ddd-crew Aggregate Design Canvas v1.1 for the two aggregates, filled from internal/domain/stationtrip, internal/domain/policy and ADR 0002. Both aggregates are on develop, persisted by internal/adapters/outbound/postgres (the in-memory adapter is a test double).

StationTrip​

SectionContent
NameStationTrip (package stationtrip), root type *StationTrip
DescriptionWhere one station stands (the end of the last completed task) and the most recent hint issued there. One per station. Dispatch in this fleet is per station and the hint is consumed by the same key.
IdentityStationID: a token of 1 to 64 runes, no whitespace, control characters or /.
Stateposition (site, location code, point when known), lastTask, lastAt, hint (most recent, in any state), version, and the not-yet-published events.
Lifecycle of a hintActive goes to exactly one terminal state: Consumed, Expired, Superseded.
CommandsRecordCompletion(task, at, Position), IssueHint(id, candidates, Policy, now), RecordClaim(task, now), Expire(now).
Events raisedHintIssued, HintConsumed, HintExpired. Domain facts, not wire messages; the domain imports nothing from CloudEvents or Kafka.
Version0 for a new trip, +1 per accepted transition. A refused or ignored command changes nothing.
SizeSmall and bounded: a position, one task, one hint with at most five candidates.

Source: internal/domain/stationtrip/values.go (HintState, Terminal), internal/domain/stationtrip/trip.go (IssueHint, RecordClaim, Expire).

Invariants​

#InvariantEnforced byTest
1Position only moves forward in time: a completion not strictly newer than the recorded one returns false, with no event and no version change. A zero instant, blank task id or invalid position is an error, checked before ignoring.RecordCompletionTestRecordCompletionIgnoresAnOlderOrEqualCompletion, TestRecordCompletionValidatesBeforeIgnoring
2At most one Active hint per station: IssueHint marks an Active hint Superseded and the new one Active. A terminal hint is not touched.IssueHintTestAtMostOneActiveHintAcrossManyIssues, TestIssueHintDoesNotSupersedeATerminalHint
3A hint is valid: distinct candidates; the policy belongs to the station's site; each candidate's type is eligible and its detour is finite, not negative and at most MaxDetourM; 1 <= len <= MaxCandidates; ranks are 1..n in detour_m then task_id order. An empty set issues no hint. A station with no recorded completion cannot be hinted.IssueHint, validateCandidatesTestIssueHintCandidateRules, TestIssueHintRefusesAnEmptyCandidateSet, TestIssueHintPreconditions
4A hint expires: expires_at = now + HintTTL at issue; Expire(now) marks an Active hint Expired at or after expires_at, and changes nothing before it.IssueHint, ExpireTestExpireMarksTheHintExpiredAtOrAfterItsExpiry, TestExpireBeforeExpiryChangesNothing
5Consumption: RecordClaim marks the Active hint Consumed only when the task is one of its candidates, the claim is not before the issue instant and now is strictly before expires_at; any other claim changes nothing.RecordClaimTestRecordClaimTimeWindow, TestRecordClaimOfANonCandidateLeavesTheHintActive
6Terminal states are final: no transition leaves Consumed, Expired or Superseded.state checks in RecordClaim, Expire, IssueHintTestRecordClaimConsumesAtMostOnce, TestExpireTwiceExpiresOnce

"The task is currently in the open-task pool" is a property of the recommender's input, not of the aggregate: the aggregate re-checks the shape and bounds of what it is handed.

Concurrency and consistency​

A version counter exists on the aggregate. Writers are told apart by the repository: Save(trip, loadedVersion) updates WHERE version = <loaded> and a lost race is repository.ErrConcurrentModification (409 concurrent-modification on REST, a retry in a consumer). The unique partial index uq_hints_one_active_per_station backs the one-Active-hint invariant in the database. Hint events of one station share the station id as Kafka key, so they stay ordered on one partition (ADR 0004; events.Sink sets the key).

InterleavingPolicy​

SectionContent
NamePolicy (package policy); concept InterleavingPolicy
DescriptionThe settings of one site, versioned and immutable. A site that never declared one is inert.
IdentitySiteID: a token of 1 to 64 runes, no whitespace, control characters or /.
StateSettings{MaxDetourM, MaxCandidates, HintTTL, EligibleTaskTypes, VerticalWeight} and version.
CommandsNew(site, Settings) (version 1), WithChanges(Settings) (returns the next version, replacing all settings), Restore(site, Settings, version).
Events raisednone in the package. The DeclarePolicy use case enqueues InterleavingPolicyDeclared (analytics only) through EventSink.PublishPolicyDeclared.
ConcurrencyReplacing a policy requires If-Match with the current version over REST (412 version-mismatch), 409 concurrent-modification on a lost race. Implemented in usecases.DeclarePolicy and the policy repository (Save(rec, loadedVersion)).
#InvariantError
1SiteID is a valid tokenErrInvalidSiteID
2MaxDetourM is finite and greater than 0ErrInvalidMaxDetour
3MaxCandidates is between 1 and 5ErrInvalidCandidates
4HintTTL is greater than 0ErrInvalidHintTTL
5EligibleTaskTypes is a non-empty set of known types; duplicates collapse, order is normalised, an unknown value is refused. PICK is a known type and needs nothing else: a site gets PICK hints by listing it here (with MaxCandidates of at least 4 for the batching default), ADR 0012ErrInvalidTaskTypes
6VerticalWeight is finite and at least 0ErrInvalidWeight
7Version >= 1, +1 per changeErrInvalidVersion (Restore only)

Source: internal/domain/policy/policy.go, ADR 0002 field table.

The recommender is not an aggregate​

recommend.Recommend(Input) is pure domain logic: no state, no I/O, no clock, no randomness. It reads the station's site, origin, the open-task pool, the policy, the completed task id and a travel.Model, and returns the ranked candidates. The aggregate StationTrip trusts but re-validates its output (invariant 3). See Use cases for the rules.