Repository navigation
Add Policy automation: react to domain events with commands - #179
Merged
Merged
Conversation
A policy is the second form of an automation: it reacts to a triggering domain event itself, with no to-do list in between, and maps it onto zero or one command. Registration states where it starts (fromNowOn / fromTheBeginning) and what a business rejection does (stallOnRejection / skipRejections), both mandatory. A redelivered event is not reacted to again: the reaction's trace (x-caused-by plus the policy as actor) is looked up first, and the command runs under the key policy:<name>@<event id>. A stalled policy can be moved past the event it is stalled on by an operator (skipStalledEvent, SkipStalledEvent). SliceType.derive gains a triggering-event count, shared with the modeler; PolicyTest is the published test base. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CbFecBFD17EgzYnoszEgfX
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Implements Policies, the second form of automation in Event Modeling: a simpler alternative to the to-do list pattern where a policy reacts directly to a domain event by issuing a command, without an intermediate read model.
A policy answers the event storming pattern "whenever X happens, then Y" — when a single event type directly triggers a single action with no need for a to-do list, deadline, or aggregation across multiple facts.
Key Changes
Core Policy Framework
Policy<DOMAIN_EVENT_TYPE>interface: defineseventQuery()to match triggering events andreact(Event)to answer the command to issuePolicyModule: runs one processor per registered policy, projecting the domain stream one event at a time on a single elected leaderReaction: executes the policy's command with idempotency keys, tracing (correlation id + causation), and duplicate detection via trace lookupPolicyStartandPolicyRejectionHandlingenums: registration choices (where to start reading, how to handle business rejections)Builder Integration
BoundedContextBuilder.policy(Policy)registration method with fluent API for configuration choicesPolicySpecificationandPolicyRejectionSpecificationfor the two required registration decisionsAUTOMATIONaspectObservability & Management
PolicyStarted,PolicyFailed,PolicySkippedevents inBoundedContextEventto report processor lifecycle and stallsReactionResultoutcome type for policy reactions (executed, already reacted, rejected)Reactionobservation scope for monitoring individual policy reactionsskipStalledEvent(EventId)inProcessorAdminCapabilityto let operators skip a stalled policy eventTesting Support
PolicyTest<D,I,O>base class: fluent API to seed domain events, trigger reactions, and assert on resulting commandsSTALLandSKIPrejection handling modesPolicyTestRunsOnEveryBackendTest: validates the test harness against all event storage backendsSupporting Changes
Tracingnow includescausationIdto name the event that caused a reactionSliceType.derive()updated to recognize policies asAUTOMATIONslicesProjectorProcessorenhancements:retryRequestedflag for operator skip,batchSizeandstartAtHeadWhenUnbookmarkedconfigurationSecondFromFirstPolicy,RecordSecondCommandImplementation Details
Documentation
Updated CLAUDE.md with comprehensive policy documentation including the gap it fills (avoiding artificial to-do lists for simple event→command reactions), registration API, and lifecycle.
https://claude.ai/code/session_01CbFecBFD17EgzYnoszEgfX