import Base import ./change_tick.bend as Tick import ./decode_requests.bend as Request import ./decode_owned_journal.bend as Journal import ./decode_immediate.bend as Immediate import ./bundle_install.bend as Install import ./decode_data.bend as D import ./compose.bend as Query import ./bundle_batch.bend as Batch import ./world.bend as World import ./capabilities.bend as Cap import ./component.bend as Component import ./transaction.bend as T import ./resource.bend as Resource import ./ordinary_declaration.bend as Identity import ./persistence_declaration.bend as Declaration # Trusted provisioning frame. Gameplay receives only universal H and its grants. # Query and decode operate on the same Tx; prepared owners stay in this frame. type Frame,-project:P -> P & D.Raw> is Type: Frame{owner:Request.Owner,handle:World.Handle,error:Maybe<&2,World.WorldError>,cursor:Maybe<&2,Tick.Tick>,receipts:List,Request.Packet

,Q>>,pending:List>} def read_join(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~Q:Type,~V:Data,~identity:Identity.Declaration,~project:P -> P & D.Raw,declaration:Declaration.Constructed,prepared:List>>,receipts:List,Request.Packet

,Q>>,pending:List>,observed:Query.Frame & Component.Access) -> Frame & Component.Access: match observed: case (Query.Frame{tx,handle,error,cursor},value):(Frame{Request.Owner{Batch.Batch{tx,prepared},declaration},handle,error,cursor,receipts,pending},value) def read(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~Q:Type,~V:Data,~identity:Identity.Declaration,~project:P -> P & D.Raw,~get:Query.Frame -> Query.Frame & Component.Access,frame:Frame) -> Frame & Component.Access: match frame: case Frame{Request.Owner{Batch.Batch{tx,prepared},declaration},handle,error,cursor,receipts,pending}:read_join(~S,~C,~R,~E,~P,~Q,~V,~identity,~project,declaration,prepared,receipts,pending,get(Query.Frame{tx,handle,error,cursor})) def read_grant(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~Q:Type,~V:Data,~identity:Identity.Declaration,~project:P -> P & D.Raw,~grant:Cap.Read,V>) -> Cap.Read,V>: Cap.Read{read(~S,~C,~R,~E,~P,~Q,~V,~identity,~project,~Cap.read_field(~Query.Frame,~V,grant))} def request_join(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~Q:Type,~identity:Identity.Declaration,~project:P -> P & D.Raw,handle:World.Handle,error:Maybe<&2,World.WorldError>,cursor:Maybe<&2,Tick.Tick>,receipts:List,Request.Packet

,Q>>,pending:List>,result:Request.Owner & Request.Output) -> Frame & Request.Output: match result: case (owner,output):(Frame{owner,handle,error,cursor,receipts,pending},output) def request(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~Q:Type,~identity:Identity.Declaration,~project:P -> P & D.Raw,frame:Frame,input:Request.Input) -> Frame & Request.Output: match frame: case Frame{owner,handle,error,cursor,receipts,pending}:request_join(~S,~C,~R,~E,~P,~Q,~identity,~project,handle,error,cursor,receipts,pending,Request.request(~S,~C,~R,~E,~P,~identity,~project,owner,input)) def request_grant(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~Q:Type,~identity:Identity.Declaration,~project:P -> P & D.Raw) -> Cap.OwnedRequest,Request.Input,Request.Output>: Cap.OwnedRequest{frame => input => request(~S,~C,~R,~E,~P,~Q,~identity,~project,frame,input)} def selected_join(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~Q:Type,~identity:Identity.Declaration,~project:P -> P & D.Raw,declaration:Declaration.Constructed,prepared:List>>,receipts:List,Request.Packet

,Q>>,pending:List>,observed:Query.Frame & Bool) -> Frame & Bool: match observed: case (Query.Frame{tx,handle,error,cursor},selected):(Frame{Request.Owner{Batch.Batch{tx,prepared},declaration},handle,error,cursor,receipts,pending},selected) # Membership is evaluated on the current transaction for every row, never on a # separate pre-scan world. The closed selector comes from the query declaration. def select(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~Q:Type,~identity:Identity.Declaration,~project:P -> P & D.Raw,~selector:Query.Frame -> Query.Frame & Bool,frame:Frame) -> Frame & Bool: match frame: case Frame{Request.Owner{Batch.Batch{tx,prepared},declaration},handle,error,cursor,receipts,pending}:selected_join(~S,~C,~R,~E,~P,~Q,~identity,~project,declaration,prepared,receipts,pending,selector(Query.Frame{tx,handle,error,cursor})) def receipt_added(~W:Type,~P:Type,~Q:Type,receipts:List>,receipt:Maybe<&1,Journal.Receipt>) -> List>: match receipt: case None{}:receipts case Some{receipt}:receipt <> receipts def pending_added(~Q:Type,pending:List>,next:Maybe<&1,Journal.Pending>) -> List>: match next: case None{}:pending case Some{next}:next <> pending def spawn_output(~S:Data,~P:Type,canonical:D.Raw,result:Result<&2,&2,World.WorldError,World.Handle>,packet:Maybe<&1,Request.Packet

>) -> Request.Output: match result packet: case Done{target} _:Request.Accepted{target,True{},canonical} case Fail{error} None{}:Request.Refused{None{},Request.Entity{error}} case Fail{error} Some{Request.Packet{owner,_,_}}:Request.Refused{owner,Request.Entity{error}} def spawned(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~Q:Type,~identity:Identity.Declaration,~project:P -> P & D.Raw,declaration:Declaration.Constructed,prepared:List>>,handle:World.Handle,error:Maybe<&2,World.WorldError>,cursor:Maybe<&2,Tick.Tick>,receipts:List,Request.Packet

,Q>>,pending:List>,canonical:D.Raw,result:T.Tx,E> & (Result<&2,&2,World.WorldError,World.Handle> & (Maybe<&1,Request.Packet

> & (Maybe<&1,Journal.Receipt,Request.Packet

,Q>> & Maybe<&1,Journal.Pending>)))) -> Frame & Request.Output: match result: case (tx,(result,(packet,(receipt,next)))):(Frame{Request.Owner{Batch.Batch{tx,prepared},declaration},handle,error,cursor,receipt_added(~World.World,~Request.Packet

,~Q,receipts,receipt),pending_added(~Q,pending,next)},spawn_output(~S,~P,canonical,result,packet)) def spawn_projected(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~Q:Type,~identity:Identity.Declaration,~project:P -> P & D.Raw,~install:World.World -> World.Handle -> Request.Packet

-> Install.Attempt,Request.Packet

,Q>,frame:Frame,+canonical:D.Raw,result:P & D.Raw) -> Frame & Request.Output: match frame result: case Frame{Request.Owner{Batch.Batch{tx,prepared},declaration},handle,error,cursor,receipts,pending}, (owner,original):spawned(~S,~C,~R,~E,~P,~Q,~identity,~project,declaration,prepared,handle,error,cursor,receipts,pending,canonical,Immediate.spawn(~S,~C,~R,~E,~P,~Q,~install,tx,Request.Packet{Some{owner},original,canonical})) def spawn_validated(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~Q:Type,~identity:Identity.Declaration,~project:P -> P & D.Raw,~install:World.World -> World.Handle -> Request.Packet

-> Install.Attempt,Request.Packet

,Q>,frame:Frame,receipt:D.Receipt

) -> Frame & Request.Output: match receipt: case D.Receipt{owner,D.Rejected{error}}:(frame,Request.Refused{Some{owner},Request.Validation{error}}) case D.Receipt{owner,D.Accepted{canonical}}:spawn_projected(~S,~C,~R,~E,~P,~Q,~identity,~project,~install,frame,canonical,project(owner)) def spawn(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~Q:Type,~identity:Identity.Declaration,~project:P -> P & D.Raw,~install:World.World -> World.Handle -> Request.Packet

-> Install.Attempt,Request.Packet

,Q>,frame:Frame,owner:P) -> Frame & Request.Output: match frame: case Frame{Request.Owner{batch,+declaration},handle,error,cursor,receipts,pending}:spawn_validated(~S,~C,~R,~E,~P,~Q,~identity,~project,~install,Frame{Request.Owner{batch,declaration},handle,error,cursor,receipts,pending},Declaration.validate(~S,~P,~identity,~project,declaration,owner)) def spawn_grant(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~Q:Type,~identity:Identity.Declaration,~project:P -> P & D.Raw,~install:World.World -> World.Handle -> Request.Packet

-> Install.Attempt,Request.Packet

,Q>) -> Cap.OwnedRequest,P,Request.Output>: Cap.OwnedRequest{frame => owner => spawn(~S,~C,~R,~E,~P,~Q,~identity,~project,~install,frame,owner)} def resource_join(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~Q:Type,~identity:Identity.Declaration,~project:P -> P & D.Raw,declaration:Declaration.Constructed,prepared:List>>,handle:World.Handle,error:Maybe<&2,World.WorldError>,cursor:Maybe<&2,Tick.Tick>,receipts:List,Request.Packet

,Q>>,pending:List>,tx:T.Tx,E>) -> Frame & Unit: (Frame{Request.Owner{Batch.Batch{tx,prepared},declaration},handle,error,cursor,receipts,pending},Unit{}) def resource_set(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~Q:Type,~identity:Identity.Declaration,~project:P -> P & D.Raw,frame:Frame,replacement:R) -> Frame & Unit: match frame: case Frame{Request.Owner{Batch.Batch{tx,prepared},declaration},handle,error,cursor,receipts,pending}:resource_join(~S,~C,~R,~E,~P,~Q,~identity,~project,declaration,prepared,handle,error,cursor,receipts,pending,Resource.tx_replace(~S,~C,~R,~E,tx,replacement)) def resource_grant(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~Q:Type,~identity:Identity.Declaration,~project:P -> P & D.Raw) -> Cap.OwnedRequest,R,Unit>: Cap.OwnedRequest{frame => replacement => resource_set(~S,~C,~R,~E,~P,~Q,~identity,~project,frame,replacement)}