Skip to content

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.

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);

View C# snippet source on GitHub

This 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");
}

View C# snippet source on GitHub

Both .NET styles declare closing constraints. Use the model-bound reference for attribute options or the declarative reference for builder options.

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);

View C# snippet source on GitHub

A 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.

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);
}
}

View C# snippet source on GitHub

AppendMany 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.

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.

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.

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.

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.

EventScenario supports closing and reopening events. See EventScenario.