import Base import ./deferred_command.bend as Deferred import ./trace_facts.bend as TraceFacts import entity_key.bend as K import bundle_delivery.bend as B import world.bend as W import transaction.bend as T import commands.bend as Cmd # Prepared payload owners remain typed until successful body completion. # This insert-only journal introduces no reservation/cancellation policy. type Prepared is Type: Prepared{target:W.Handle,payload:P,position:Nat} ReservationReceipt{target:W.Handle} type Batch is Type: Batch{tx:T.Tx,E>,prepared:List>} type Inserted is Type: Inserted{batch:Batch} Refused{batch:Batch,payload:P,error:W.WorldError} type Spawned is Type: Spawned{batch:Batch,target:W.Handle} SpawnRefused{batch:Batch,payload:P,error:W.WorldError} type Finished is Type: Committed{world:W.World} Failed{world:W.World,payloads:List

,error:Error} def begin(~S:Data,~Store:Type,~R:Type,~E:Data,~P:Type,tx:T.Tx,E>) -> Batch: Batch{tx,[]} # Count returns the very same affine closures in the same reverse queue order. def counted(~World:Type,command:Deferred.Command,result:List> & Nat) -> List> & Nat: match result: case (commands,count): (command <> commands,1n+count) def count_commands(~World:Type,commands:List>) -> List> & Nat: match commands: case []: ([],0n) case command <> tail: counted(~World,command,count_commands(~World,tail)) def positioned(~S:Data,~Store:Type,~R:Type,~E:Data,~P:Type,prepared:List>,world:W.World,undo:List<(W.World -> W.World)>,events:List<&2,E>,trace:TraceFacts.State,target:W.Handle,payload:P,result:List>> & Nat) -> Inserted: match result: case (commands,position): Inserted{Batch{T.Tx{world,undo,commands,events,trace},Prepared{target,payload,position} <> prepared}} # Receipt rows are created only from this Batch's actual Cmd.tx_spawn result. # Public gameplay cannot construct the opaque Batch; raw engine constructors # and provider callbacks remain explicitly trusted setup boundaries. def receipt_join(~S:Data,~P:Type,entry:Prepared,seen:Bool,result:List> & Bool) -> List> & Bool: match result: case (tail,found): (entry <> tail,Bool.or(seen,found)) def receipt_find(~S:Data,~P:Type,prepared:List>,+target:W.Handle) -> List> & Bool: match prepared: case []: ([],False{}) case Prepared{handle,payload,position} <> tail: receipt_join(~S,~P,Prepared{handle,payload,position},False{},receipt_find(~S,~P,tail,target)) case ReservationReceipt{W.Handle{+ns,+id}} <> tail: match target: case W.Handle{+otherNs,+otherId}: receipt_join(~S,~P,ReservationReceipt{W.Handle{ns,id}},Bool.and(U32.is_eq(ns,otherNs),K.equal(id,otherId)),receipt_find(~S,~P,tail,W.Handle{otherNs,otherId})) def receipt_checked(~S:Data,~Store:Type,~R:Type,~E:Data,~P:Type,world:W.World,undo:List<(W.World -> W.World)>,commands:List>>,events:List<&2,E>,trace:TraceFacts.State,target:W.Handle,payload:P,result:List> & Bool) -> Inserted: match result: case (prepared,False{}): Refused{Batch{T.Tx{world,undo,commands,events,trace},prepared},payload,W.MissingEntity{}} case (prepared,True{}): positioned(~S,~Store,~R,~E,~P,prepared,world,undo,events,trace,target,payload,count_commands(~W.World,commands)) def checked(~S:Data,~Store:Type,~R:Type,~E:Data,~P:Type,prepared:List>,undo:List<(W.World -> W.World)>,commands:List>>,events:List<&2,E>,trace:TraceFacts.State,+target:W.Handle,payload:P,observed:W.World & Bool) -> Inserted: match observed: case (world,False{}): receipt_checked(~S,~Store,~R,~E,~P,world,undo,commands,events,trace,target,payload,receipt_find(~S,~P,prepared,target)) case (world,True{}): positioned(~S,~Store,~R,~E,~P,prepared,world,undo,events,trace,target,payload,count_commands(~W.World,commands)) def insert(~S:Data,~Store:Type,~R:Type,~E:Data,~P:Type,batch:Batch,+target:W.Handle,payload:P) -> Inserted: match batch: case Batch{T.Tx{world,undo,commands,events,trace},prepared}: checked(~S,~Store,~R,~E,~P,prepared,undo,commands,events,trace,target,payload,W.valid(~S,~Store,~R,~E,world,target)) # Replay only closures preceding this authored insertion. Position is generated # by count_commands, not supplied by gameplay; raw constructors remain trusted. def preceding(~World:Type,~E:Data,count:Nat,commands:List>,tx:T.Tx) -> List> & T.Tx: match count commands: case 0n _: (commands,tx) case 1n+rest Nil{}: ([],tx) case 1n+rest Con{command,tail}: preceding(~World,~E,rest,tail,T.stage_command(~World,~E,tx,command)) def remaining(~World:Type,~E:Data,commands:List>,tx:T.Tx) -> T.Tx: match commands: case []: tx case command <> tail: remaining(~World,~E,tail,T.stage_command(~World,~E,tx,command)) def staged_after(~S:Data,~Store:Type,~R:Type,~E:Data,~P:Type,~attempt:W.World -> W.Handle -> P -> B.Delivery,P>,~recover:W.World -> P -> W.WorldError -> W.World,target:W.Handle,payload:P,next:List>> -> T.Tx,E> -> T.Tx,E>,result:List>> & T.Tx,E>) -> T.Tx,E>: match result: case (after,before): next(after,B.enqueue(~S,~Store,~R,~E,~P,~attempt,~recover,before,target,payload)) def staged(~S:Data,~Store:Type,~R:Type,~E:Data,~P:Type,~attempt:W.World -> W.Handle -> P -> B.Delivery,P>,~recover:W.World -> P -> W.WorldError -> W.World,prepared:List>,commands:List>>,+current:Nat,tx:T.Tx,E>) -> T.Tx,E>: match prepared: case []: remaining(~W.World,~E,commands,tx) case ReservationReceipt{target} <> tail: staged(~S,~Store,~R,~E,~P,~attempt,~recover,tail,commands,current,tx) case Prepared{+target,payload,+position} <> tail: staged_after(~S,~Store,~R,~E,~P,~attempt,~recover,target,payload,after => before => staged(~S,~Store,~R,~E,~P,~attempt,~recover,tail,after,position,before),preceding(~W.World,~E,Nat.sub(position,current),commands,tx)) def stage(~S:Data,~Store:Type,~R:Type,~E:Data,~P:Type,~attempt:W.World -> W.Handle -> P -> B.Delivery,P>,~recover:W.World -> P -> W.WorldError -> W.World,prepared:List>,tx:T.Tx,E>) -> T.Tx,E>: match tx: case T.Tx{world,undo,commands,events,trace}: staged(~S,~Store,~R,~E,~P,~attempt,~recover,prepared,List.reverse(&1,Deferred.Command>,commands),0n,T.Tx{world,undo,[],events,trace}) def originals(~S:Data,~P:Type,prepared:List>) -> List

: match prepared: case []: [] case ReservationReceipt{target} <> tail: originals(~S,~P,tail) case Prepared{target,payload,position} <> tail: payload <> originals(~S,~P,tail) def committed(~S:Data,~Store:Type,~R:Type,~E:Data,~P:Type,~Error:Data,observed:T.Returned,Error>) -> Finished: match observed: case T.Returned{world,outcome}: Committed{world} def failed(~S:Data,~Store:Type,~R:Type,~E:Data,~P:Type,~Error:Data,payloads:List

,error:Error,observed:T.Returned,Error>) -> Finished: match observed: case T.Returned{world,outcome}: Failed{world,payloads,error} def finish(~S:Data,~Store:Type,~R:Type,~E:Data,~P:Type,~Error:Data,~attempt:W.World -> W.Handle -> P -> B.Delivery,P>,~recover:W.World -> P -> W.WorldError -> W.World,batch:Batch,outcome:T.Outcome) -> Finished: match batch outcome: case Batch{tx,prepared} T.Success{}: committed(~S,~Store,~R,~E,~P,~Error,Cmd.tx_finish(~S,~Store,~R,~E,~Error,stage(~S,~Store,~R,~E,~P,~attempt,~recover,List.reverse(&1,Prepared,prepared),tx),T.Success{})) case Batch{tx,prepared} T.Failure{+error}: failed(~S,~Store,~R,~E,~P,~Error,originals(~S,~P,List.reverse(&1,Prepared,prepared)),error,Cmd.tx_finish(~S,~Store,~R,~E,~Error,tx,T.Failure{error})) def empty_population(~S:Data,~Store:Type,~R:Type,~E:Data,world:W.World,handle:W.Handle,unit:Unit) -> W.World: world def spawn_positioned(~S:Data,~Store:Type,~R:Type,~E:Data,~P:Type,target:W.Handle,result:Inserted) -> Spawned: match result: case Inserted{batch}: Spawned{batch,target} case Refused{batch,payload,error}: SpawnRefused{batch,payload,error} def spawn_reserved(~S:Data,~Store:Type,~R:Type,~E:Data,~P:Type,prepared:List>,payload:P,result:T.Tx,E> & W.Reservation) -> Spawned: match result: case (T.Tx{world,undo,commands,events,trace},W.Reserved{+target}): spawn_positioned(~S,~Store,~R,~E,~P,target,positioned(~S,~Store,~R,~E,~P,ReservationReceipt{target} <> prepared,world,undo,events,trace,target,payload,count_commands(~W.World,commands))) case (tx,W.ReservationRejected{error}): SpawnRefused{Batch{tx,prepared},payload,error} def spawn(~S:Data,~Store:Type,~R:Type,~E:Data,~P:Type,batch:Batch,payload:P) -> Spawned: match batch: case Batch{tx,prepared}: spawn_reserved(~S,~Store,~R,~E,~P,prepared,payload,Cmd.tx_spawn(~S,~Store,~R,~E,~Unit,~(world => handle => unit => world),tx,Unit{}))