import ./persistence_declaration.bend as Decl import ./bundle_delivery.bend as Delivery import Base import entity_key.bend as Key import ./decode_frame.bend as F import ./decode_requests.bend as Request import ./decode_owned_journal.bend as Journal import ./ordinary_declaration.bend as Identity import ./decode_data.bend as D import ./world.bend as W import ./local.bend as Local import ./transaction.bend as T import ./commands.bend as Commands import ./bundle_batch.bend as Batch # State/sink adapters are application-selected, closed typed functions. A failed # run returns its Local owner and no output, exactly like existing Local.Outcome. def registered(~S:Data,~C:Type,~R:Type,~E:Data,~L:Type,~A:Type,~Error:Data,~Out:Type,~runner:W.World -> L -> A -> Local.Outcome,L,Error,Out>,world:W.World,name:String,access:List<&2,String>,local:L) -> Local.Registered: Local.register(~S,~C,~R,~E,~L,~A,~Error,~Out,~runner,world,name,access,local) def succeeded(~World:Type,~L:Type,~Error:Data,~Out:Type,world:World,result:L & Out) -> Local.Outcome: match result: case (local,output):Local.Succeeded{world,local,output} def committed(~World:Type,~L:Type,~A:Type,~Rows:Data,~Error:Data,~Out:Type,~success:L -> A -> Rows -> L & Out,~failure:L -> A -> L,local:L,args:A,rows:Rows,result:T.Returned) -> Local.Outcome: match result: case T.Returned{world,T.Success{}}:succeeded(~World,~L,~Error,~Out,world,success(local,args,rows)) case T.Returned{world,T.Failure{error}}:Local.Failed{world,failure(local,args),error} def recovered(~S:Data,~World:Type,~L:Type,~A:Type,~P:Type,~Q:Type,~Error:Data,~Out:Type,~recover:L -> A -> List> -> List> -> L,error:Error,local:L,args:A,prepared:List>>,pending:List>,result:Journal.Returned,Q>) -> Local.Outcome: match result: case Journal.Returned{world,owners,more}:Local.Failed{world,recover(local,args,List.append(&1,Request.Packet

,owners,Batch.originals(~S,~Request.Packet

,prepared)),List.append(&1,Journal.Pending,pending,more)),error} def abort(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~Q:Type,~identity:Identity.Declaration,~project:P -> P & D.Raw,~L:Type,~A:Type,~Error:Data,~Out:Type,~recover:L -> A -> List> -> List> -> L,error:Error,local:L,args:A,frame:F.Frame) -> Local.Outcome,L,Error,Out>: match frame: case F.Frame{Request.Owner{Batch.Batch{tx,prepared},_},_,_,_,receipts,pending}:recovered(~S,~W.World,~L,~A,~P,~Q,~Error,~Out,~recover,error,local,args,prepared,pending,Journal.abort(~W.World,~E,~Request.Packet

,~Q,receipts,tx)) # Non-empty deferred packets require the separate deferred completion route. # Pending is never silently accepted as a completed rollback. def traversal(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~Q:Type,~identity:Identity.Declaration,~project:P -> P & D.Raw,~L:Type,~A:Type,~Rows:Data,~Error:Data,~Out:Type,~success:L -> A -> Rows -> L & Out,~failure:L -> A -> L,~recover:L -> A -> List> -> List> -> L,rejected:Error,local:L,result:F.Frame & (A & (T.Outcome & Rows))) -> Local.Outcome,L,Error,Out>: match result: case (F.Frame{Request.Owner{Batch.Batch{tx,[]},_},_,_,_,_,[]},(args,(T.Success{},rows))):committed(~W.World,~L,~A,~Rows,~Error,~Out,~success,~failure,local,args,rows,Commands.tx_finish(~S,~C,~R,~E,~Error,tx,T.Success{})) case (frame,(args,(T.Success{},_))):abort(~S,~C,~R,~E,~P,~Q,~identity,~project,~L,~A,~Error,~Out,~recover,rejected,local,args,frame) case (frame,(args,(T.Failure{error},_))):abort(~S,~C,~R,~E,~P,~Q,~identity,~project,~L,~A,~Error,~Out,~recover,error,local,args,frame) def deferred(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~Error:Data,~L:Type,~Out:Type,~recover:L -> Out -> List> -> L,local:L,output:Out,result:Batch.Finished,Error>) -> Local.Outcome,L,Error,Out>: match result: case Batch.Committed{world}:Local.Succeeded{world,local,output} case Batch.Failed{world,packets,error}:Local.Failed{world,recover(local,output,packets),error} # Construction uses the same retained nominal descriptor as the grants. def begin_owner(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~identity:Identity.Declaration,~project:P -> P & D.Raw,world:W.World,codec:Decl.Constructed) -> Request.Owner: Request.Owner{Batch.begin(~S,~C,~R,~E,~Request.Packet

,T.begin(~W.World,~E,world)),codec} def begin_frame(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~Q:Type,~identity:Identity.Declaration,~project:P -> P & D.Raw,world:W.World,codec:Decl.Constructed) -> F.Frame: F.Frame{begin_owner(~S,~C,~R,~E,~P,~identity,~project,world,codec),W.Handle{0,Key.EntityKey{0,0}},None{},None{},[],[]} def finish_deferred(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~Error:Data,~identity:Identity.Declaration,~project:P -> P & D.Raw,~attempt:W.World -> W.Handle -> Request.Packet

-> Delivery.Delivery,Request.Packet

>,~delivery_recover:W.World -> Request.Packet

-> W.WorldError -> W.World,~L:Type,~Out:Type,~recover:L -> Out -> List> -> L,local:L,output:Out,owner:Request.Owner,outcome:T.Outcome) -> Local.Outcome,L,Error,Out>: deferred(~S,~C,~R,~E,~P,~Error,~L,~Out,~recover,local,output,Request.finish(~S,~C,~R,~E,~P,~Error,~identity,~project,~attempt,~delivery_recover,owner,outcome))