import Base import ./execution_save_plan.bend as Plan import ./execution_save_traversal.bend as Traverse import ./execution_save_rows.bend as Rows import ./execution_save_data.bend as Saved import ./execution_save_recipe.bend as Recipe import ./execution_save_metadata.bend as Metadata import ./execution_save_gate.bend as Gate import ./execution_composer_layout.bend as Layout import ./snapshot_data.bend as Legacy import ./world.bend as W import ./entity_allocator.bend as Alloc import ./clock_state.bend as Clock import ./execution_lifecycle_notices.bend as Notice import ./deferred_command.bend as Deferred type Remainder<-S:Data,-C:Type,-R:Type,-E:Data> is Type: Remainder{namespace:U32,allocator:Alloc.Allocator,events:List<&2,E>,pending:List>>,registrations:List<&2,W.RegistrationMeta>,nextSystemId:U32,clock:Clock.State,journal:Notice.Journal} def assembled(~S:Data,~C:Type,~R:Type,~E:Data,rest:Remainder,columns:C,resources:R) -> W.World: match rest: case Remainder{namespace,allocator,events,pending,registrations,nextSystemId,clock,journal}:W.World{namespace,allocator,columns,resources,events,pending,registrations,nextSystemId,clock,journal} def exported(~S:Data,~cp:Plan.Plan,~rp:Plan.Plan,~E:Data,componentRecipes:Plan.Recipes(~S,cp),columns:Layout.columns(~S,Plan.layout(cp)),rest:Remainder,metadata:Metadata.Metadata,rows:List<&2,Saved.Entity>,result:Plan.Recipes(~S,rp) & (Layout.resources(~S,Plan.layout(rp)) & Saved.Fields)) -> Plan.Schema & (W.World & Gate.Outcome,Recipe.Refusal>): match result: case (resourceRecipes,(resources,Saved.Fields{_,Some{error}})):(Plan.Schema{componentRecipes,resourceRecipes},(assembled(~S,~Layout.columns(~S,Plan.layout(cp)),~Layout.resources(~S,Plan.layout(rp)),~E,rest,columns,resources),Gate.Refused{error})) case (resourceRecipes,(resources,Saved.Fields{fields,None{}})):(Plan.Schema{componentRecipes,resourceRecipes},(assembled(~S,~Layout.columns(~S,Plan.layout(cp)),~Layout.resources(~S,Plan.layout(rp)),~E,rest,columns,resources),Gate.Saved{Saved.Snapshot{2,metadata,rows,fields}})) def rows_returned(~S:Data,~cp:Plan.Plan,~rp:Plan.Plan,~E:Data,resourceRecipes:Plan.Recipes(~S,rp),resources:Layout.resources(~S,Plan.layout(rp)),rest:Remainder,metadata:Metadata.Metadata,result:Plan.Recipes(~S,cp) & (Layout.columns(~S,Plan.layout(cp)) & (List<&2,Saved.Entity> & Maybe<&2,Recipe.Refusal>))) -> Plan.Schema & (W.World & Gate.Outcome,Recipe.Refusal>): match result: case (componentRecipes,(columns,(_,Some{error}))):(Plan.Schema{componentRecipes,resourceRecipes},(assembled(~S,~Layout.columns(~S,Plan.layout(cp)),~Layout.resources(~S,Plan.layout(rp)),~E,rest,columns,resources),Gate.Refused{error})) case (componentRecipes,(columns,(rows,None{}))):exported(~S,~cp,~rp,~E,componentRecipes,columns,rest,metadata,rows,Traverse.resources(~S,rp)(resourceRecipes,resources)) def handles_returned(~S:Data,~cp:Plan.Plan,~rp:Plan.Plan,~E:Data,schema:Plan.Schema,metadata:Metadata.Metadata,result:W.World & List<&2,W.Handle>) -> Plan.Schema & (W.World & Gate.Outcome,Recipe.Refusal>): match schema result: case Plan.Schema{componentRecipes,resourceRecipes}, (W.World{namespace,allocator,columns,resources,events,pending,registrations,nextSystemId,clock,journal},handles):rows_returned(~S,~cp,~rp,~E,resourceRecipes,resources,Remainder{namespace,allocator,events,pending,registrations,nextSystemId,clock,journal},metadata,Rows.rows(~S,~cp,handles,componentRecipes,columns)) def metadata_returned(~S:Data,~cp:Plan.Plan,~rp:Plan.Plan,~E:Data,schema:Plan.Schema,result:W.World & Metadata.Metadata) -> Plan.Schema & (W.World & Gate.Outcome,Recipe.Refusal>): match result: case (world,metadata):handles_returned(~S,~cp,~rp,~E,schema,metadata,W.handles(~S,~Layout.columns(~S,Plan.layout(cp)),~Layout.resources(~S,Plan.layout(rp)),~E,world)) def unique(valid:Bool) -> Maybe<&2,Recipe.Refusal>: match valid: case True{}:None{} case False{}:Some{Recipe.DescriptorMismatch{"unique layout keys","duplicate key"}} def eligibility_result(result:Maybe<&2,String>,valid:Bool) -> Maybe<&2,Recipe.Refusal>: match result: case Some{key}:Some{Recipe.PlainUnserializable{key}} case None{}:unique(valid) def eligibility(~cp:Plan.Plan,~rp:Plan.Plan) -> Maybe<&2,Recipe.Refusal>: eligibility_result(Plan.first(Plan.ineligible(cp),Plan.ineligible(rp)),Bool.and(Layout.unique(Plan.layout(cp)),Layout.unique(Plan.layout(rp)))) def eligible(~S:Data,~cp:Plan.Plan,~rp:Plan.Plan,~E:Data,schema:Plan.Schema,world:W.World,error:Maybe<&2,Recipe.Refusal>) -> Plan.Schema & (W.World & Gate.Outcome,Recipe.Refusal>): match error: case Some{error}:(schema,(world,Gate.Refused{error})) case None{}:metadata_returned(~S,~cp,~rp,~E,schema,Metadata.capture(~S,~Layout.columns(~S,Plan.layout(cp)),~Layout.resources(~S,Plan.layout(rp)),~E,world)) def ready(~S:Data,~cp:Plan.Plan,~rp:Plan.Plan,~E:Data,schema:Plan.Schema,result:W.World & Bool) -> Plan.Schema & (W.World & Gate.Outcome,Recipe.Refusal>): match result: case (world,False{}):(schema,(world,Gate.RefusedPending{})) case (world,True{}):eligible(~S,~cp,~rp,~E,schema,world,eligibility(~cp,~rp)) # Internal World phase. Ordinary applications must first inventory their actual # command owners with execution-save-gate; legacy World.pending is additional. # Eligibility of the entire closed schema precedes metadata and field projection. def save(~S:Data,~cp:Plan.Plan,~rp:Plan.Plan,~E:Data,schema:Plan.Schema,world:W.World) -> Plan.Schema & (W.World & Gate.Outcome,Recipe.Refusal>): ready(~S,~cp,~rp,~E,schema,Legacy.ready(~S,~Layout.columns(~S,Plan.layout(cp)),~Layout.resources(~S,Plan.layout(rp)),~E,world))