Declare closing events in Cratis Chronicle
A closing event in Cratis Chronicle is a domain fact that tells the kernel to close a stream scope after storing the event. Use one when the fact itself ends a reporting period or another part of an entity’s history, so the log records why further appends are refused.
For example, ReportDelivered can close a report’s stream. Unlike a separate CompleteStream call, the declaration leaves no opportunity for another append between the closing fact and its closure. The event and closure are separate durable writes, though; Guarantees and limits explains recovery after a kernel stop.
Declare a closing event
Section titled “Declare a closing event”Place [ClosesStream] on the event type to keep the closing rule with the fact it describes:
using Cratis.Chronicle.EventSequences;using Cratis.Chronicle.Events;using Cratis.Chronicle.Events.Constraints;
[EventType][ClosesStream(Dimensions = ClosedStreamDimensions.EventSourceId | ClosedStreamDimensions.EventStreamType)]public record ClosingEventsReportDelivered(string Recipient);defmodule MyApp.Events.ClosingEventsReportDelivered do use Chronicle.Events.EventType, id: "closing-events-report-delivered"
defstruct [:recipient]
# Appending this event closes the stream scope made of the event source and the stream type. closes_stream dimensions: [:event_source_id, :event_stream_type]endThis declaration selects the event source id and stream type. Delivery therefore closes every stream id of that type for the event source that received the event.
Use the builder when you keep constraints in a separate class. This example closes a version identified by the event’s payload and allows a reinstatement event to reopen it:
using Cratis.Chronicle.Events;using Cratis.Chronicle.Events.Constraints;
[EventType]public record ClosingEventsVersionSuperseded(string Version);
[EventType]public record ClosingEventsVersionReinstated(string Version);
public class ClosingEventsVersionClosing : IConstraint{ public void Define(IConstraintBuilder builder) => builder.ClosesStreamOn<ClosingEventsVersionSuperseded>( closes => closes .PerEventSourceId() .EventStreamIdFrom(@event => @event.Version) .ReopenedBy<ClosingEventsVersionReinstated>(), name: "close-superseded-version");}defmodule MyApp.Events.ClosingEventsVersionReinstated do use Chronicle.Events.EventType, id: "closing-events-version-reinstated"
defstruct [:version]end
# The Elixir client declares a closing event on the event type itself. Declarations# that share a name merge into one constraint.defmodule MyApp.Events.ClosingEventsVersionSuperseded do use Chronicle.Events.EventType, id: "closing-events-version-superseded"
defstruct [:version]
closes_stream name: "close-superseded-version", dimensions: [:event_source_id], event_stream_id_from: :version, reopened_by: [MyApp.Events.ClosingEventsVersionReinstated]endBoth .NET styles declare closing constraints. Use the model-bound reference for attribute options or the declarative reference for builder options.
Choose the scope
Section titled “Choose the scope”By default, the closure matches the event source id, stream type and stream id supplied with the append. All three must match for a later append to be blocked. To close a different scope, select the dimensions with the attribute’s Dimensions value or the builder’s PerEventSourceId(), PerEventSourceType(), PerEventStreamType() and PerEventStreamId() methods.
To take the stream id from the event’s payload instead of the append, use EventStreamIdFrom (attribute) or EventStreamIdFrom(@event => @event.Period) (builder). That implies the stream id dimension. This is useful when the closing fact names the period it closes while it is appended somewhere else:
using Cratis.Chronicle.Events;using Cratis.Chronicle.Events.Constraints;
[EventType][ClosesStream(EventStreamIdFrom = nameof(Period), ReopenedBy = [typeof(ClosingEventsBooksReopened)])]public record ClosingEventsBooksClosed(string Period, decimal ClosingBalance);
[EventType]public record ClosingEventsBooksReopened(string Period, string Reason);defmodule MyApp.Events.ClosingEventsBooksReopened do use Chronicle.Events.EventType, id: "closing-events-books-reopened"
defstruct [:period, :reason]end
defmodule MyApp.Events.ClosingEventsBooksClosed do use Chronicle.Events.EventType, id: "closing-events-books-closed"
defstruct [:period, :closing_balance]
# The stream id comes from the event's own period field, and the reopening event lifts the closure. closes_stream event_stream_id_from: :period, reopened_by: [MyApp.Events.ClosingEventsBooksReopened]endA scope resolving to only default-stream values is refused for both closing and reopening events; it cannot close the default stream for every event source.
A property-sourced value must be present. If it is missing, null or empty, the kernel refuses the append with a violation naming the property rather than closing something broader. Make the property non-nullable. The analyzer CHR0012 warns on nullable event properties.
What happens on append
Section titled “What happens on append”Once the closing event is accepted, later appends inside its scope fail with StreamClosed. The closure does not affect appends outside that scope:
using Cratis.Chronicle.EventSequences;using Cratis.Chronicle.Events;using Cratis.Chronicle.Events.Constraints;
[EventType]public record ClosingEventsEntryPosted(string Description);
public class ClosingEventsAfterClose(IEventLog eventLog){ public async Task<bool> Run(EventSourceId ledgerId) { await eventLog.Append( ledgerId, new ClosingEventsBooksClosed("2026-04", 1200m), new EventStreamType("ledger"), new EventStreamId("2026-04"));
var late = await eventLog.Append( ledgerId, new ClosingEventsEntryPosted("Late invoice"), new EventStreamType("ledger"), new EventStreamId("2026-04"));
// The late entry is refused; the next period is unaffected. return late.ConstraintViolations.Any(violation => violation.ConstraintType == ConstraintType.StreamClosed); }}defmodule MyApp.Events.ClosingEventsLedgerPeriodClosed do use Chronicle.Events.EventType, id: "closing-events-ledger-period-closed"
defstruct [:period, :closing_balance]
closes_stream event_stream_id_from: :periodend
defmodule MyApp.Events.ClosingEventsEntryPosted do use Chronicle.Events.EventType, id: "closing-events-entry-posted"
defstruct [:description]end
defmodule MyApp.ClosingEventsAfterClose do alias Chronicle.EventSequences.EventLog alias Chronicle.Events.ConstraintViolation alias MyApp.Events.{ClosingEventsEntryPosted, ClosingEventsLedgerPeriodClosed}
def run(ledger_id) do :ok = EventLog.append( ledger_id, %ClosingEventsLedgerPeriodClosed{period: "2026-04", closing_balance: 1200}, event_stream_type: "ledger", event_stream_id: "2026-04" )
late = EventLog.append_with_result( ledger_id, %ClosingEventsEntryPosted{description: "Late invoice"}, event_stream_type: "ledger", event_stream_id: "2026-04" )
# The late entry is refused; the next period is unaffected. case late do {:error, %{constraint_violations: violations}} -> Enum.any?(violations, &stream_closed?/1) _ -> false end end
# A rejection caused by a closed stream carries the closure's details. defp stream_closed?(violation), do: Map.has_key?(ConstraintViolation.details(violation), :closed_by)endAppendMany evaluates events in order. Events after a closing event are refused if they fall inside its scope, and the batch result identifies them. If the batch closes, reopens and then appends to that scope, the closure no longer blocks the final event.
A closing event cannot be appended into a scope that is already closed. The closed-stream check applies to it like to any other event.
Reopen with an event
Section titled “Reopen with an event”Name the event types that lift the closure with ReopenedBy (attribute) or ReopenedBy<T>() (builder). This keeps reopening in the domain history; choose an event whose payload expresses the reason when that matters to the business.
A ReopenedBy event removes only its own constraint’s closure for the exact scope it resolves to. It is exempt from that constraint’s closure, so it can be appended inside the scope it reopens. Manual closures and other constraints’ closures still apply. If an operator also closed the scope with CompleteStream, that manual closure blocks the reopening event.
When the declaration uses EventStreamIdFrom, every reopening event type must have that property too. The .NET client throws PropertyDoesNotExistOnEventType during constraint discovery if it is missing.
Ownership and names
Section titled “Ownership and names”Each closure belongs to its constraint. The constraint name defaults to the event type name. Set Name on the attribute or pass name to the builder to choose another. Two declarations that share a name merge into one constraint, with their closing and reopening event types combined. They must use the same dimensions and stream id property, otherwise the client throws ConflictingClosesStreamDeclarations. The empty name is reserved for manual closures.
Set EventSequences on the attribute to restrict a declaration to specific event sequences. Empty means all.
Not retroactive
Section titled “Not retroactive”Registering or changing a declaration applies it to future appends; it does not close scopes based on events already stored. To rebuild closures from history, run a reindex that names the closing constraint. Read the rebuild and redaction limits before relying on it for recovery.
Appends can continue during a rebuild. The job hands its progress back to the event sequence, which restores later claims and closures overwritten by the rebuild. Closing-constraint commits write only differences instead of first deleting every closure owned by the constraint. Rebuilding unique constraints still has a window between clearing a scope’s values and restoring its claims.
Streams of an event source definition
Section titled “Streams of an event source definition”When the stream belongs to an event source definition, append the closing event through the definition to that stream. Close a stream of a definition shows that path. For decisions based on the closed scope, see how closure relates to a Dynamic Consistency Boundary.
Test it
Section titled “Test it”EventScenario supports closing and reopening events. See EventScenario.