import Base import ./change_tick.bend as Tick import ./deferred_command.bend as Deferred import ./event_stream.bend as Leaf import ./world.bend as W import ./relation_commands.bend as Notice import ./relation_failure_runtime.bend as K import ./internal/relation_failures/domains.bend as D import ./internal/relation_failures/routing.bend as FailureRoute import ./internal/relation_failures/barrier.bend as FailureBarrier import ./event_runtime.bend as Ev import ./event_family_runtime.bend as Family import ./event_family_routing.bend as Route # Descriptor metadata owns no second World. Typed publication ticks remain separate. type Failures is Data: Failures{domains:List<&2,D.Domain>>,positions:List<&2,Ev.Position>,tick:Nat,frameStart:Nat,boundary:Nat,capacity:Nat,nextReader:U32} type State is Type: State{typed:Family.State,failures:Failures} def originals(~S:Data,~P:Data,records:List<&2,Notice.Notice>) -> List<&2,P>: match records: case []:[] case Notice.Original{event} <> rest:event <> originals(~S,~P,rest) case Notice.RelationFailed{_} <> rest:originals(~S,~P,rest) case Notice.ComponentRemoved{_} <> rest:originals(~S,~P,rest) case Notice.CleanupPaused{_} <> rest:originals(~S,~P,rest) def started(~S:Data,~C:Type,~R:Type,~P:Data,~F:Type,~H:Type,typed:Family.State,runtime:K.Runtime>) -> Route.Runtime,State>: match runtime: case K.Runtime{world,domains,positions,tick,frameStart,boundary,capacity,nextReader}:Route.Runtime{world,State{typed,Failures{domains,positions,tick,frameStart,boundary,capacity,nextReader}},0n} def start(~S:Data,~C:Type,~R:Type,~P:Data,~F:Type,~H:Type,world:W.World>,family:F,owners:H) -> Route.Runtime,State>: started(~S,~C,~R,~P,~F,~H,Family.State{family,owners,[],0n},K.start(~S,~C,~R,~Notice.Notice,world)) def synchronized(~S:Data,~C:Type,~R:Type,~P:Data,~F:Type,~H:Type,~sync:F -> Nat -> List<&2,P> -> F,state:State,+tick:Nat,observed:W.World> & List<&2,Notice.Notice>) -> W.World> & State: match state: case State{Family.State{family,owners,+journal,frameStart},failures}: (world,records)=observed +published=List.drop(&2,P,originals(~S,~P,records),Ev.size(~P,journal)) (world,State{Family.State{sync(family,tick,published),owners,Ev.sync_checked(~P,Nat.is_lt(0n,Ev.count(~P,published)),journal,tick,published),frameStart},failures}) def synchronize(~S:Data,~C:Type,~R:Type,~P:Data,~F:Type,~H:Type,~sync:F -> Nat -> List<&2,P> -> F,world:W.World>,state:State,tick:Nat) -> W.World> & State: synchronized(~S,~C,~R,~P,~F,~H,~sync,state,tick,W.events_view(~S,~C,~R,~Notice.Notice,world)) # The detached typed owner contains no World; K operations receive the sole World. def failure_runtime(~S:Data,~C:Type,~R:Type,~P:Data,~F:Type,~H:Type,runtime:Route.Runtime,State>) -> K.Runtime> & (Family.State & Nat): match runtime: case Route.Runtime{world,State{typed,Failures{domains,positions,tick,frameStart,boundary,capacity,nextReader}},typedTick}:(K.Runtime{world,domains,positions,tick,frameStart,boundary,capacity,nextReader},(typed,typedTick)) def failure_returned(~S:Data,~C:Type,~R:Type,~P:Data,~F:Type,~H:Type,held:Family.State & Nat,runtime:K.Runtime>) -> Route.Runtime,State>: match held runtime: case (typed,typedTick) K.Runtime{world,domains,positions,tick,frameStart,boundary,capacity,nextReader}: Route.Runtime{world,State{typed,Failures{domains,positions,tick,frameStart,boundary,capacity,nextReader}},typedTick} # Slots preserve the original mixed-log positions, including partially retained Publications. type Retention is Type: Retention{family:F,journal:List<&2,Ev.Batch

>,slots:List<&2,List<&2,P>>} def retained_record(~P:Data,~F:Type,+head:List<&2,P>,returned:F & (List<&2,P> & List<&2,List<&2,P>>)) -> F & (List<&2,P> & List<&2,List<&2,P>>): match returned: case (family,(records,slots)):(family,(Ev.append(~P,head,records),head <> slots)) def retained_record_head(~P:Data,~F:Type,rest:F -> F & (List<&2,P> & List<&2,List<&2,P>>),returned:F & List<&2,P>) -> F & (List<&2,P> & List<&2,List<&2,P>>): match returned: case (family,+head):retained_record(~P,~F,head,rest(family)) def retained_records(~P:Data,~F:Type,~retain:F -> Nat -> List<&2,P> -> F & List<&2,P>,records:List<&2,P>,family:F,+tick:Nat) -> F & (List<&2,P> & List<&2,List<&2,P>>): match records: case []:(family,([],[])) case head <> rest:retained_record_head(~P,~F,(family => retained_records(~P,~F,~retain,rest,family,tick)),retain(family,tick,[head])) def retained_batch_join(~P:Data,~F:Type,tick:Nat,+records:List<&2,P>,slots:List<&2,List<&2,P>>,returned:Retention) -> Retention: match returned: case Retention{family,journal,tail}:Retention{family,Family.kept_batch(~P,Nat.is_lt(0n,Ev.count(~P,records)),tick,records,journal),List.append(&2,List<&2,P>,slots,tail)} def retained_batch_head(~P:Data,~F:Type,rest:F -> Retention,tick:Nat,returned:F & (List<&2,P> & List<&2,List<&2,P>>)) -> Retention: match returned: case (family,(+records,slots)):retained_batch_join(~P,~F,tick,records,slots,rest(family)) def retention(~P:Data,~F:Type,~retain:F -> Nat -> List<&2,P> -> F & List<&2,P>,journal:List<&2,Ev.Batch

>,family:F) -> Retention: match journal: case []:Retention{family,[],[]} case Ev.Batch{+tick,records} <> rest:retained_batch_head(~P,~F,(family => retention(~P,~F,~retain,rest,family)),tick,retained_records(~P,~F,~retain,records,family,tick)) def original_notices(~S:Data,~P:Data,records:List<&2,P>) -> List<&2,Notice.Notice>: match records: case []:[] case head <> rest:Notice.Original{head} <> original_notices(~S,~P,rest) def rebuilt(~S:Data,~P:Data,notices:List<&2,Notice.Notice>,slots:List<&2,List<&2,P>>) -> List<&2,Notice.Notice>: match notices slots: case Nil{} _:[] case Con{Notice.Original{_},rest} Nil{}:rebuilt(~S,~P,rest,[]) case Con{Notice.Original{_},rest} Con{head,tail}:List.append(&2,Notice.Notice,original_notices(~S,~P,head),rebuilt(~S,~P,rest,tail)) case Con{Notice.RelationFailed{failure},rest} slots:Notice.RelationFailed{failure} <> rebuilt(~S,~P,rest,slots) case Con{Notice.ComponentRemoved{id},rest} slots:Notice.ComponentRemoved{id} <> rebuilt(~S,~P,rest,slots) case Con{Notice.CleanupPaused{frames},rest} slots:Notice.CleanupPaused{frames} <> rebuilt(~S,~P,rest,slots) def framed(~S:Data,~C:Type,~R:Type,~P:Data,~F:Type,~H:Type,owners:H,failures:Failures,+tick:Nat,observed:W.World> & List<&2,Notice.Notice>,returned:Retention) -> Route.Runtime,State>: match observed returned: case (world,notices) Retention{family,journal,slots}: Route.Runtime{W.events_replace(~S,~C,~R,~Notice.Notice,world,rebuilt(~S,~P,notices,slots)),State{Family.State{family,owners,journal,tick},failures},tick} def frame(~S:Data,~C:Type,~R:Type,~P:Data,~F:Type,~H:Type,~frame:F -> Nat -> F,~retain:F -> Nat -> List<&2,P> -> F & List<&2,P>,runtime:Route.Runtime,State>) -> Route.Runtime,State>: match runtime: case Route.Runtime{world,State{Family.State{family,owners,journal,_},failures},+tick}:framed(~S,~C,~R,~P,~F,~H,owners,failures,tick,W.events_view(~S,~C,~R,~Notice.Notice,world),retention(~P,~F,~retain,journal,frame(family,tick))) def failure_framed(~S:Data,~C:Type,~R:Type,~P:Data,~F:Type,~H:Type,detached:K.Runtime> & (Family.State & Nat)) -> Route.Runtime,State>: match detached: case (runtime,held):failure_returned(~S,~C,~R,~P,~F,~H,held,K.frame(~S,~C,~R,~Notice.Notice,runtime)) def frame_failures(~S:Data,~C:Type,~R:Type,~P:Data,~F:Type,~H:Type,runtime:Route.Runtime,State>) -> Route.Runtime,State>: failure_framed(~S,~C,~R,~P,~F,~H,failure_runtime(~S,~C,~R,~P,~F,~H,runtime)) def inventoried(~S:Data,~C:Type,~R:Type,~P:Data,~F:Type,~H:Type,world:W.World>,owners:H,journal:List<&2,Ev.Batch

>,frameStart:Nat,failures:Failures,tick:Nat,returned:F & List<&2,String>) -> Route.Runtime,State> & Maybe<&2,List<&2,String>>: match returned: case (family,names):(Route.Runtime{world,State{Family.State{family,owners,journal,frameStart},failures},tick},Some{names}) def inventory_enabled(~S:Data,~C:Type,~R:Type,~P:Data,~F:Type,~H:Type,~inventory:F -> F & List<&2,String>,enabled:Bool,runtime:Route.Runtime,State>) -> Route.Runtime,State> & Maybe<&2,List<&2,String>>: match enabled runtime: case False{} runtime:(runtime,None{}) case True{} Route.Runtime{world,State{Family.State{family,owners,journal,frameStart},failures},tick}:inventoried(~S,~C,~R,~P,~F,~H,world,owners,journal,frameStart,failures,tick,inventory(family)) def selected(~S:Data,~E:Data,~P:Data,~F:Type,~H:Type,failures:Failures,returned:Family.State & Leaf.Stream) -> State & Leaf.Stream: match returned: case (typed,stream):(State{typed,failures},stream) def take(~S:Data,~E:Data,~P:Data,~F:Type,~H:Type,~take:F -> F & Leaf.Stream,state:State) -> State & Leaf.Stream: match state: case State{typed,failures}:selected(~S,~E,~P,~F,~H,failures,Family.take(~S,~E,~P,~F,~H,~take,typed)) def put(~S:Data,~E:Data,~P:Data,~F:Type,~H:Type,~put:F -> Leaf.Stream -> F,state:State,stream:Leaf.Stream) -> State: match state: case State{typed,failures}:State{Family.put(~S,~E,~P,~F,~H,~put,typed,stream),failures} # Reserve from the same failure-reader pool used by standalone K.register. def reserved_failure_reader(~S:Data,~C:Type,~R:Type,~P:Data,~F:Type,~H:Type,held:Family.State & Nat,result:K.Runtime> & Maybe<&2,U32>) -> Route.Runtime,State> & Maybe<&2,U32>: match result: case (runtime,id):(failure_returned(~S,~C,~R,~P,~F,~H,held,runtime),id) def reserve_failure_reader_detached(~S:Data,~C:Type,~R:Type,~P:Data,~F:Type,~H:Type,detached:K.Runtime> & (Family.State & Nat)) -> Route.Runtime,State> & Maybe<&2,U32>: match detached: case (runtime,held):reserved_failure_reader(~S,~C,~R,~P,~F,~H,held,K.reserve_reader(~S,~C,~R,~Notice.Notice,runtime)) def reserve_failure_reader(~S:Data,~C:Type,~R:Type,~P:Data,~F:Type,~H:Type,runtime:Route.Runtime,State>) -> Route.Runtime,State> & Maybe<&2,U32>: reserve_failure_reader_detached(~S,~C,~R,~P,~F,~H,failure_runtime(~S,~C,~R,~P,~F,~H,runtime)) # One outer ingestion boundary, shared by every parameter in an invocation. def ingested_failures(~S:Data,~C:Type,~R:Type,~P:Data,~F:Type,~H:Type,typed:Family.State,positions:List<&2,Ev.Position>,+tick:Nat,start:Nat,boundary:Nat,capacity:Nat,nextReader:U32,returned:W.World> & List<&2,D.Domain>>) -> Route.Runtime,State>: match returned: case (world,domains):Route.Runtime{world,State{typed,Failures{domains,positions,tick,start,boundary,capacity,nextReader}},tick} def ingested_typed(~S:Data,~C:Type,~R:Type,~P:Data,~F:Type,~H:Type,~classify:Notice.Notice -> Maybe>,+tick:Nat,returned:W.World> & State) -> Route.Runtime,State>: match returned: case (world,State{typed,Failures{domains,positions,_,start,boundary,capacity,nextReader}}): ingested_failures(~S,~C,~R,~P,~F,~H,typed,positions,tick,start,boundary,capacity,nextReader,FailureRoute.collect(~S,~C,~R,~Notice.Notice,~classify,world,tick,domains)) def ingest(~S:Data,~C:Type,~R:Type,~P:Data,~F:Type,~H:Type,~sync:F -> Nat -> List<&2,P> -> F,~classify:Notice.Notice -> Maybe>,+tick:Nat,runtime:Route.Runtime,State>) -> Route.Runtime,State>: match runtime: case Route.Runtime{world,state,_}:ingested_typed(~S,~C,~R,~P,~F,~H,~classify,tick,synchronize(~S,~C,~R,~P,~F,~H,~sync,world,state,tick)) def attempt_clocked(~S:Data,~C:Type,~R:Type,~P:Data,~F:Type,~H:Type,state:State,tick:Nat,clocked:W.World> & Tick.Tick) -> Route.Runtime,State>: match clocked: case (world,_):Route.Runtime{world,state,1n+tick} def attempt_ingested(~S:Data,~C:Type,~R:Type,~P:Data,~F:Type,~H:Type,runtime:Route.Runtime,State>) -> Route.Runtime,State>: match runtime: case Route.Runtime{world,state,tick}:attempt_clocked(~S,~C,~R,~P,~F,~H,state,tick,W.execution_clock(~S,~C,~R,~Notice.Notice,world)) # Standalone and composed operations share histories but may advance different # administrative clocks. Never publish below any already observed boundary. def before_state(~S:Data,~C:Type,~R:Type,~P:Data,~F:Type,~H:Type,~sync:F -> Nat -> List<&2,P> -> F,~classify:Notice.Notice -> Maybe>,state:State,+tick:Nat,world:W.World>,clock:Tick.Tick) -> Route.Runtime,State>: match state: case State{typed,Failures{domains,positions,+failureTick,start,boundary,capacity,nextReader}}: attempt_ingested(~S,~C,~R,~P,~F,~H,ingest(~S,~C,~R,~P,~F,~H,~sync,~classify,Nat.max(1n,Nat.max(tick,Nat.max(failureTick,Tick.total(clock)))),Route.Runtime{world,State{typed,Failures{domains,positions,failureTick,start,boundary,capacity,nextReader}},tick})) def before_clocked(~S:Data,~C:Type,~R:Type,~P:Data,~F:Type,~H:Type,~sync:F -> Nat -> List<&2,P> -> F,~classify:Notice.Notice -> Maybe>,state:State,tick:Nat,clocked:W.World> & Tick.Tick) -> Route.Runtime,State>: match clocked: case (world,clock):before_state(~S,~C,~R,~P,~F,~H,~sync,~classify,state,tick,world,clock) # Zero is the initial unread cursor, so startup publications need a positive epoch. # Called only after registration/parameter validation; preparation follows it. def before_attempt(~S:Data,~C:Type,~R:Type,~P:Data,~F:Type,~H:Type,~sync:F -> Nat -> List<&2,P> -> F,~classify:Notice.Notice -> Maybe>,runtime:Route.Runtime,State>) -> Route.Runtime,State>: match runtime: case Route.Runtime{world,state,tick}:before_clocked(~S,~C,~R,~P,~F,~H,~sync,~classify,state,tick,W.clock(~S,~C,~R,~Notice.Notice,world)) # Called once after successful cursor commits, without another clock advance. def after_success(~S:Data,~C:Type,~R:Type,~P:Data,~F:Type,~H:Type,~sync:F -> Nat -> List<&2,P> -> F,~classify:Notice.Notice -> Maybe>,runtime:Route.Runtime,State>) -> Route.Runtime,State>: match runtime: case Route.Runtime{world,state,+tick}:ingest(~S,~C,~R,~P,~F,~H,~sync,~classify,tick,Route.Runtime{world,state,tick}) # Internal invocation transport: detached metadata retains all shared owners. def split(~S:Data,~C:Type,~R:Type,~P:Data,~F:Type,~H:Type,runtime:Route.Runtime,State>) -> W.World> & (State & Nat): match runtime: case Route.Runtime{world,state,tick}:(world,(state,tick)) def join(~S:Data,~C:Type,~R:Type,~P:Data,~F:Type,~H:Type,world:W.World>,metadata:State & Nat) -> Route.Runtime,State>: match metadata: case (state,tick):Route.Runtime{world,state,tick} # The command-aware barrier keeps each command publication as its own batch. # Typed owners/journal, reader positions and membership remain unchanged. def barrier_drained(~S:Data,~C:Type,~R:Type,~P:Data,~F:Type,~H:Type,typed:Family.State,positions:List<&2,Ev.Position>,frameStart:Nat,boundary:Nat,capacity:Nat,nextReader:U32,returned:FailureBarrier.BarrierState>) -> Route.Runtime,State>: match returned: case FailureBarrier.BarrierState{world,domains,+tick}:Route.Runtime{world,State{typed,Failures{domains,positions,tick,frameStart,boundary,capacity,nextReader}},tick} def barrier_advanced(~S:Data,~C:Type,~R:Type,~P:Data,~F:Type,~H:Type,~classify:Notice.Notice -> Maybe>,typed:Family.State,domains:List<&2,D.Domain>>,positions:List<&2,Ev.Position>,previous:Nat,frameStart:Nat,boundary:Nat,capacity:Nat,nextReader:U32,advanced:W.World> & Tick.Tick) -> Route.Runtime,State>: match advanced: case (W.World{namespace,allocator,store,resource,events,pending,registrations,nextSystemId,clock},actual): barrier_drained(~S,~C,~R,~P,~F,~H,typed,positions,frameStart,boundary,capacity,nextReader, FailureBarrier.drain(~S,~C,~R,~Notice.Notice,~classify,List.reverse(&1,Deferred.Command>>,pending), FailureBarrier.BarrierState{W.World{namespace,allocator,store,resource,events,[],registrations,nextSystemId,clock},domains,Nat.max(previous,Tick.total(actual))})) def barrier(~S:Data,~C:Type,~R:Type,~P:Data,~F:Type,~H:Type,~classify:Notice.Notice -> Maybe>,context:Route.Runtime,State>) -> Route.Runtime,State>: match context: case Route.Runtime{world,State{typed,Failures{domains,positions,failureTick,frameStart,boundary,capacity,nextReader}},tick}: barrier_advanced(~S,~C,~R,~P,~F,~H,~classify,typed,domains,positions,Nat.max(tick,failureTick),frameStart,boundary,capacity,nextReader,W.enter_run(~S,~C,~R,~Notice.Notice,world))