Path: redb-route/redb.Route/Definitions/ReplayableDefinition.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route/Definitions/ReplayableDefinition.cs
----
using Microsoft.Extensions.Logging;
using redb.Route.Abstractions;
using redb.Route.Core;
using redb.Route.Processors;
namespace redb.Route.Definitions;
///
/// Scope-opener definition for a replay checkpoint (save-point) — .Replayable("name").
/// Its are the body of the marker (the tail
/// after it). wraps that body in a
/// (which snapshots the exchange on each pass) and registers the compiled body in the context's
/// checkpoint registry so
/// can re-run it — no synthetic endpoint.
///
/// At the very start of a route the body is the whole pipeline and no End is needed (an
/// unclosed scope is structurally valid); in the middle or when several markers are used, close with
/// / the universal , or use the lambda-body DSL overload.
///
///
public class ReplayableDefinition : RouteDefinitionBase, IRouteScope, IScopeNestingRule
{
private readonly string _markerName;
private readonly bool _exposed;
/// The save-point name (unique within the route; key of the checkpoint registry).
public string MarkerName => _markerName;
///
/// When true, the marker is also an addressable public entry (for .To(...) from
/// other routes and tests) — the direct:__replay:{routeId}:{name} endpoint is registered
/// lazily. Default false: internal replay only, via ReplayAsync.
///
public bool Exposed => _exposed;
internal ReplayableDefinition(string markerName, bool exposed)
{
_markerName = markerName;
_exposed = exposed;
}
/// Closes this replay scope and returns the parent route definition.
/// Thrown if no parent route is set.
public IRouteDefinition EndReplayable()
=> (IRouteDefinition)(Parent ?? throw new InvalidOperationException(
"EndReplayable() called without a parent route. Ensure Replayable() was called on a route definition."));
///
public IRouteDefinition End() => EndReplayable();
///
/// Nesting policy (see ): a checkpoint's tail cannot span a
/// branching scope (§6 — build error), and inside a durable transaction a manual replay runs
/// outside it (§10 — warning). Every other nesting is allowed (including nested checkpoints).
///
public NestingVerdict CheckAncestor(IProcessorDefinition ancestor) => ancestor switch
{
ICompositeScope => NestingVerdict.Forbid(
$"checkpoint '{_markerName}' cannot cross a {ancestor.GetType().Name} boundary; " +
"extract the segment into a direct: sub-route."),
IDurableScope => NestingVerdict.Warn(
$"checkpoint '{_markerName}' is inside a {ancestor.GetType().Name}: a manual replay runs " +
"OUTSIDE that transaction and may duplicate processing — ensure idempotency (§10)."),
_ => NestingVerdict.Allowed
};
///
public override IProcessor CreateProcessor(IRouteContext context)
{
var body = BuildPipeline(Outputs, context);
var logger = context.GetService()?.CreateLogger();
// Register the compiled body so ReplayAsync re-runs it directly (no synthetic endpoint).
// The context knows which route it is compiling right now.
(context as RouteContext)?.RegisterCheckpoint(_markerName, body, _exposed);
return new CheckpointProcessor(_markerName, body, logger);
}
private static IProcessor BuildPipeline(IList outputs, IRouteContext context)
=> NodePipeline.Body(context, outputs);
}