import Base import ./resource.bend as Resource import ./bundle_batch.bend as Batch import ./bundle_delivery.bend as Delivery import ./bundle_requests.bend as Commands import ./world.bend as W import ./transaction.bend as T import ./capabilities.bend as Cap import ./decode_data.bend as D import ./persistence_declaration.bend as Decl import ./ordinary_declaration.bend as Identity # The packet owns original P plus detached canonical input. Typed schema delivery # constructs its actual component; no generic Raw -> arbitrary Type inverse. type Packet is Type: Packet{owner:Maybe<&1,P>,original:D.Raw,canonical:D.Raw} type Input is Type: Input{operation:Commands.Operation,owner:P} type Error is Data: Validation{error:D.Error} Entity{error:W.WorldError} type Output is Type: Accepted{target:W.Handle,spawned:Bool,canonical:D.Raw} Refused{owner:Maybe<&1,P>,error:Error} type Owner,-project:P -> P & D.Raw> is Type: Owner{batch:Batch.Batch>,declaration:Decl.Constructed} def inserted(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~identity:Identity.Declaration,~project:P -> P & D.Raw,declaration:Decl.Constructed,target:W.Handle,canonical:D.Raw,result:Batch.Inserted>) -> Owner & Output: match result: case Batch.Inserted{batch}:(Owner{batch,declaration},Accepted{target,False{},canonical}) case Batch.Refused{batch,Packet{owner,_,_},error}:(Owner{batch,declaration},Refused{owner,Entity{error}}) def spawned(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~identity:Identity.Declaration,~project:P -> P & D.Raw,declaration:Decl.Constructed,canonical:D.Raw,result:Batch.Spawned>) -> Owner & Output: match result: case Batch.Spawned{batch,target}:(Owner{batch,declaration},Accepted{target,True{},canonical}) case Batch.SpawnRefused{batch,Packet{owner,_,_},error}:(Owner{batch,declaration},Refused{owner,Entity{error}}) def enqueue_packet(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~identity:Identity.Declaration,~project:P -> P & D.Raw,declaration:Decl.Constructed,batch:Batch.Batch>,operation:Commands.Operation,owner:P,original:D.Raw,+canonical:D.Raw) -> Owner & Output: match operation: case Commands.Insert{+target}:inserted(~S,~C,~R,~E,~P,~identity,~project,declaration,target,canonical,Batch.insert(~S,~C,~R,~E,~Packet

,batch,target,Packet{Some{owner},original,canonical})) case Commands.Spawn{}:spawned(~S,~C,~R,~E,~P,~identity,~project,declaration,canonical,Batch.spawn(~S,~C,~R,~E,~Packet

,batch,Packet{Some{owner},original,canonical})) def enqueue(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~identity:Identity.Declaration,~project:P -> P & D.Raw,declaration:Decl.Constructed,batch:Batch.Batch>,operation:Commands.Operation,canonical:D.Raw,projected:P & D.Raw) -> Owner & Output: match projected: case (owner,original):enqueue_packet(~S,~C,~R,~E,~P,~identity,~project,declaration,batch,operation,owner,original,canonical) def validated(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~identity:Identity.Declaration,~project:P -> P & D.Raw,declaration:Decl.Constructed,batch:Batch.Batch>,operation:Commands.Operation,receipt:D.Receipt

) -> Owner & Output: match receipt: case D.Receipt{owner,D.Rejected{error}}:(Owner{batch,declaration},Refused{Some{owner},Validation{error}}) case D.Receipt{owner,D.Accepted{canonical}}:enqueue(~S,~C,~R,~E,~P,~identity,~project,declaration,batch,operation,canonical,project(owner)) def request(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~identity:Identity.Declaration,~project:P -> P & D.Raw,frame:Owner,input:Input) -> Owner & Output: match frame input: case Owner{batch,+declaration} Input{operation,owner}:validated(~S,~C,~R,~E,~P,~identity,~project,declaration,batch,operation,Decl.validate(~S,~P,~identity,~project,declaration,owner)) def grant(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~identity:Identity.Declaration,~project:P -> P & D.Raw) -> Cap.OwnedRequest,Input,Output>: Cap.OwnedRequest{frame => input => request(~S,~C,~R,~E,~P,~identity,~project,frame,input)} def invoke(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~A:Type,~U:Type,~identity:Identity.Declaration,~project:P -> P & D.Raw,~body:@-H:Type -> @-cap:Cap.OwnedRequest,Output> -> A -> H -> H & U,owner:Owner,args:A) -> Owner & U: Cap.invoke_owned(~(H => Cap.OwnedRequest,Output>),~A,~U,~body,~Owner,~grant(~S,~C,~R,~E,~P,~identity,~project),args,owner) def finish(~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 -> Packet

-> Delivery.Delivery,Packet

>,~recover:W.World -> Packet

-> W.WorldError -> W.World,owner:Owner,outcome:T.Outcome) -> Batch.Finished,Error>: match owner: case Owner{batch,_}:Batch.finish(~S,~C,~R,~E,~Packet

,~Error,~attempt,~recover,batch,outcome) # Additive trusted resource provider over the SAME transaction; gameplay can use # it only when provisioning explicitly includes this owned grant in its Ops. def resource_replaced(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~identity:Identity.Declaration,~project:P -> P & D.Raw,declaration:Decl.Constructed,prepared:List>>,tx:T.Tx,E>) -> Owner & Unit: (Owner{Batch.Batch{tx,prepared},declaration},Unit{}) def replace_resource(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~identity:Identity.Declaration,~project:P -> P & D.Raw,owner:Owner,replacement:R) -> Owner & Unit: match owner: case Owner{Batch.Batch{tx,prepared},declaration}:resource_replaced(~S,~C,~R,~E,~P,~identity,~project,declaration,prepared,Resource.tx_replace(~S,~C,~R,~E,tx,replacement)) def resource_grant(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~identity:Identity.Declaration,~project:P -> P & D.Raw) -> Cap.OwnedRequest,R,Unit>: Cap.OwnedRequest{owner => replacement => replace_resource(~S,~C,~R,~E,~P,~identity,~project,owner,replacement)}