1 import Base 2 import ./change_tick.bend as Tick 3 import ./restore_clear.bend as Clear 4 import ./restore_transient.bend as Transient 5 import ./restore_data.bend as R 6 import ./restore_leaf.bend as Stage 7 import ./restore_storage.bend as Storage 8 import ./restore_product.bend as Product 9 import ./snapshot_schema.bend as Save 10 import ./snapshot_leaf.bend as Leaf 11 import ./ordinary_schema.bend as Ordinary 12 import ./ordinary_declaration.bend as Identity 13 import ./owner_product.bend as Owner 14 import ./decode_data.bend as D 15 import ./decode_construction.bend as Constructor 16 import ./bundle_construction.bend as Build 17 import ./world.bend as W 18 19 # Restore schema retaining the ordinary declaration inventory with closed staged 20 # construction/cleanup/insertion/recovery adapters in its type. Compose canonical 21 # builders rather than a second name registry. Raw schema/lens/supply callbacks 22 # remain trusted provisioning, not arbitrary descriptor validation by this wrapper. 23 type Schema<-S:Data,-C:Type,-Resources:Type,-State:Type,-Custom:Data,-stage:State -> R.Position -> D.Field -> State & R.Dispatch,-release:State -> State,-clear:C -> W.Handle -> C & List<&2,R.Change>,-insert:C -> Resources -> State -> List<&2,R.Binding> -> Tick.Tick -> (C & Resources) & State,-Recovery:Type,-commit:State -> State,-takeRecovery:State -> State & Recovery> is Data: 24 Schema{ordinary:Ordinary.Schema} 25 def component_kind() -> Stage.FamilyKind:Stage.Component{} 26 def resource_kind() -> Stage.FamilyKind:Stage.Resource{} 27 def component_stage(~S:Data,~Context:Type,~I:Type,~P:Type,~Custom:Data,~identity:Identity.Declaration,~project:P -> P & D.Raw,~incoming:I -> I & D.Raw,~build:I -> D.Raw -> Build.Construction,~factory:Constructor.FallibleFactory,~supply:Context -> D.Raw -> Context & I,state:Stage.State,position:R.Position,field:D.Field) -> Stage.State & R.Dispatch: 28 Stage.stage(~S,~Context,~I,~P,~Custom,~identity,~project,~incoming,~build,~factory,~supply,~component_kind(),state,position,field) 29 def resource_stage(~S:Data,~Context:Type,~I:Type,~P:Type,~Custom:Data,~identity:Identity.Declaration,~project:P -> P & D.Raw,~incoming:I -> I & D.Raw,~build:I -> D.Raw -> Build.Construction,~factory:Constructor.FallibleFactory,~supply:Context -> D.Raw -> Context & I,state:Stage.State,position:R.Position,field:D.Field) -> Stage.State & R.Dispatch: 30 Stage.stage(~S,~Context,~I,~P,~Custom,~identity,~project,~incoming,~build,~factory,~supply,~resource_kind(),state,position,field) 31 def component_clear(~S:Data,~P:Type,~identity:Identity.Declaration,~project:P -> P & D.Raw,owner:Leaf.Component,handle:W.Handle) -> Leaf.Component & List<&2,R.Change>: 32 Clear.component(~S,~P,~identity,~project,owner,handle) 33 def resource_clear(~S:Data,owner:Unit,handle:W.Handle) -> Unit & List<&2,R.Change>: 34 Clear.unit(~S,owner,handle) 35 def product_clear(~S:Data,~LC:Type,~RC:Type,~left:LC -> W.Handle -> LC & List<&2,R.Change>,~right:RC -> W.Handle -> RC & List<&2,R.Change>,owner:Owner.Owner,handle:W.Handle) -> Owner.Owner & List<&2,R.Change>: 36 Clear.product(~S,~LC,~RC,~left,~right,owner,handle) 37 def component_insert_join(~S:Data,~Context:Type,~I:Type,~P:Type,~identity:Identity.Declaration,~project:P -> P & D.Raw,observed:Leaf.Component & Stage.State) -> (Leaf.Component & Unit) & Stage.State: 38 match observed: 39 case (owner,state):((owner,Unit{}),state) 40 def component_insert(~S:Data,~Context:Type,~I:Type,~P:Type,~identity:Identity.Declaration,~project:P -> P & D.Raw,owner:Leaf.Component,unit:Unit,state:Stage.State,bindings:List<&2,R.Binding>,tick:Tick.Tick) -> (Leaf.Component & Unit) & Stage.State: 41 component_insert_join(~S,~Context,~I,~P,~identity,~project,Storage.components(~S,~Context,~I,~P,~identity,~project,owner,state,bindings,tick)) 42 def resource_insert_join(~S:Data,~Context:Type,~I:Type,~P:Type,~identity:Identity.Declaration,~project:P -> P & D.Raw,observed:Leaf.Resource & Stage.State) -> (Unit & Leaf.Resource) & Stage.State: 43 match observed: 44 case (owner,state):((Unit{},owner),state) 45 def resource_insert(~S:Data,~Context:Type,~I:Type,~P:Type,~identity:Identity.Declaration,~project:P -> P & D.Raw,unit:Unit,owner:Leaf.Resource,state:Stage.State,bindings:List<&2,R.Binding>,tick:Tick.Tick) -> (Unit & Leaf.Resource) & Stage.State: 46 resource_insert_join(~S,~Context,~I,~P,~identity,~project,Storage.resources(~S,~Context,~I,~P,~identity,~project,owner,state)) 47 # Build a persisted component restore leaf from its nominal identity, canonical 48 # codec/fallible factory and owned input supply. Stage incoming construction and 49 # validate the resulting stored P before World insertion; refusal returns actual 50 # inputs through schema release/recovery. No generic Raw-to-arbitrary-Type cast. 51 def component(~S:Data,~Context:Type,~I:Type,~P:Type,~Custom:Data,~identity:Identity.Declaration,~project:P -> P & D.Raw,~incoming:I -> I & D.Raw,~build:I -> D.Raw -> Build.Construction,~factory:Constructor.FallibleFactory,~supply:Context -> D.Raw -> Context & I) -> Schema,Unit,Stage.State,Custom,component_stage(~S,~Context,~I,~P,~Custom,~identity,~project,~incoming,~build,~factory,~supply),Stage.release(~Context,~I,~P),component_clear(~S,~P,~identity,~project),component_insert(~S,~Context,~I,~P,~identity,~project),List,Stage.commit(~Context,~I,~P),Stage.take_recovery(~Context,~I,~P)>: 52 Schema{Save.inventory(~S,~Leaf.Component,~Unit,~True{},~Leaf.component_fields(~S,~P,~identity,~project),~Save.unit_resource,Save.component(~S,~P,~identity,~project))} 53 # Build a persisted resource restore leaf using the same constructed declaration 54 # and factory contract. Omitted resources remain owned by World; a staged value 55 # replaces through schema insertion only after complete preparation/admission. 56 def resource(~S:Data,~Context:Type,~I:Type,~P:Type,~Custom:Data,~identity:Identity.Declaration,~project:P -> P & D.Raw,~incoming:I -> I & D.Raw,~build:I -> D.Raw -> Build.Construction,~factory:Constructor.FallibleFactory,~supply:Context -> D.Raw -> Context & I) -> Schema,Stage.State,Custom,resource_stage(~S,~Context,~I,~P,~Custom,~identity,~project,~incoming,~build,~factory,~supply),Stage.release(~Context,~I,~P),resource_clear(~S),resource_insert(~S,~Context,~I,~P,~identity,~project),List,Stage.commit(~Context,~I,~P),Stage.take_recovery(~Context,~I,~P)>: 57 Schema{Save.inventory(~S,~Unit,~Leaf.Resource,~True{},~Save.unit_component,~Leaf.resource_fields(~S,~P,~identity,~project),Save.resource(~S,~P,~identity,~project))} 58 def product_stage(~S:Data,~LC:Type,~LR:Type,~RC:Type,~RR:Type,~L:Type,~Right:Type,~Custom:Data,~stageLeft:L -> R.Position -> D.Field -> L & R.Dispatch,~releaseLeft:L -> L,~clearLeft:LC -> W.Handle -> LC & List<&2,R.Change>,~insertLeft:LC -> LR -> L -> List<&2,R.Binding> -> Tick.Tick -> (LC & LR) & L,~stageRight:Right -> R.Position -> D.Field -> Right & R.Dispatch,~releaseRight:Right -> Right,~clearRight:RC -> W.Handle -> RC & List<&2,R.Change>,~insertRight:RC -> RR -> Right -> List<&2,R.Binding> -> Tick.Tick -> (RC & RR) & Right,state:Owner.Owner,position:R.Position,field:D.Field) -> Owner.Owner & R.Dispatch: 59 Product.stage(~L,~Right,~Custom,~stageLeft,~stageRight,state,position,field) 60 def product_insert(~S:Data,~LC:Type,~LR:Type,~RC:Type,~RR:Type,~L:Type,~Right:Type,~Custom:Data,~stageLeft:L -> R.Position -> D.Field -> L & R.Dispatch,~releaseLeft:L -> L,~clearLeft:LC -> W.Handle -> LC & List<&2,R.Change>,~insertLeft:LC -> LR -> L -> List<&2,R.Binding> -> Tick.Tick -> (LC & LR) & L,~stageRight:Right -> R.Position -> D.Field -> Right & R.Dispatch,~releaseRight:Right -> Right,~clearRight:RC -> W.Handle -> RC & List<&2,R.Change>,~insertRight:RC -> RR -> Right -> List<&2,R.Binding> -> Tick.Tick -> (RC & RR) & Right,owner:Owner.Owner,resource:Owner.Owner,state:Owner.Owner,bindings:List<&2,R.Binding>,tick:Tick.Tick) -> (Owner.Owner & Owner.Owner) & Owner.Owner: 61 Product.insert(~S,~LC,~LR,~RC,~RR,~L,~Right,~insertLeft,~insertRight,owner,resource,state,bindings,tick) 62 def product_join(~S:Data,~LC:Type,~LR:Type,~RC:Type,~RR:Type,~L:Type,~Right:Type,~Custom:Data,~stageLeft:L -> R.Position -> D.Field -> L & R.Dispatch,~releaseLeft:L -> L,~clearLeft:LC -> W.Handle -> LC & List<&2,R.Change>,~insertLeft:LC -> LR -> L -> List<&2,R.Binding> -> Tick.Tick -> (LC & LR) & L,~stageRight:Right -> R.Position -> D.Field -> Right & R.Dispatch,~releaseRight:Right -> Right,~clearRight:RC -> W.Handle -> RC & List<&2,R.Change>,~insertRight:RC -> RR -> Right -> List<&2,R.Binding> -> Tick.Tick -> (RC & RR) & Right,~LRecovery:Type,~RRecovery:Type,~commitLeft:L -> L,~commitRight:Right -> Right,~takeLeft:L -> L & LRecovery,~takeRight:Right -> Right & RRecovery,left:Ordinary.Schema,right:Ordinary.Schema) -> Schema,Owner.Owner,Owner.Owner,Custom,product_stage(~S,~LC,~LR,~RC,~RR,~L,~Right,~Custom,~stageLeft,~releaseLeft,~clearLeft,~insertLeft,~stageRight,~releaseRight,~clearRight,~insertRight),Product.release(~L,~Right,~releaseLeft,~releaseRight),product_clear(~S,~LC,~RC,~clearLeft,~clearRight),product_insert(~S,~LC,~LR,~RC,~RR,~L,~Right,~Custom,~stageLeft,~releaseLeft,~clearLeft,~insertLeft,~stageRight,~releaseRight,~clearRight,~insertRight),Owner.Owner,Product.release(~L,~Right,~commitLeft,~commitRight),Product.take_recovery(~L,~Right,~LRecovery,~RRecovery,~takeLeft,~takeRight)>: 63 Schema{Ordinary.product(~S,~LC,~LR,~RC,~RR,left,right)} 64 # Compose schema inventories, staged field dispatch, owner cleanup/insertion and 65 # actual recovery products in left-before-right order. Canonical dispatch tries 66 # right only when left is Unhandled; this adds no new duplicate-field policy. 67 def product(~S:Data,~LC:Type,~LR:Type,~RC:Type,~RR:Type,~L:Type,~Right:Type,~Custom:Data,~stageLeft:L -> R.Position -> D.Field -> L & R.Dispatch,~releaseLeft:L -> L,~clearLeft:LC -> W.Handle -> LC & List<&2,R.Change>,~insertLeft:LC -> LR -> L -> List<&2,R.Binding> -> Tick.Tick -> (LC & LR) & L,~stageRight:Right -> R.Position -> D.Field -> Right & R.Dispatch,~releaseRight:Right -> Right,~clearRight:RC -> W.Handle -> RC & List<&2,R.Change>,~insertRight:RC -> RR -> Right -> List<&2,R.Binding> -> Tick.Tick -> (RC & RR) & Right,~LRecovery:Type,~RRecovery:Type,~commitLeft:L -> L,~commitRight:Right -> Right,~takeLeft:L -> L & LRecovery,~takeRight:Right -> Right & RRecovery,left:Schema,right:Schema) -> Schema,Owner.Owner,Owner.Owner,Custom,product_stage(~S,~LC,~LR,~RC,~RR,~L,~Right,~Custom,~stageLeft,~releaseLeft,~clearLeft,~insertLeft,~stageRight,~releaseRight,~clearRight,~insertRight),Product.release(~L,~Right,~releaseLeft,~releaseRight),product_clear(~S,~LC,~RC,~clearLeft,~clearRight),product_insert(~S,~LC,~LR,~RC,~RR,~L,~Right,~Custom,~stageLeft,~releaseLeft,~clearLeft,~insertLeft,~stageRight,~releaseRight,~clearRight,~insertRight),Owner.Owner,Product.release(~L,~Right,~commitLeft,~commitRight),Product.take_recovery(~L,~Right,~LRecovery,~RRecovery,~takeLeft,~takeRight)>: 68 match left right: 69 case Schema{left} Schema{right}:product_join(~S,~LC,~LR,~RC,~RR,~L,~Right,~Custom,~stageLeft,~releaseLeft,~clearLeft,~insertLeft,~stageRight,~releaseRight,~clearRight,~insertRight,~LRecovery,~RRecovery,~commitLeft,~commitRight,~takeLeft,~takeRight,left,right) 70 # Build an explicitly omitted transient component leaf. Snapshot input does not 71 # construct this payload; old entity cleanup still handles its existing storage 72 # when restoring the live entity set. No new arbitrary-Type factory is invented. 73 def transient_component(~S:Data,~P:Type,~Custom:Data,~identity:Identity.Declaration) -> Schema,Unit,Transient.State

,Custom,Transient.stage(~Transient.State

,~Custom),Transient.release(~Transient.State

),Clear.transient_component(~S,~P,~identity),Transient.insert(~S,~P,~identity),Unit,Transient.commit(~P),Transient.take_recovery(~Transient.State

)>: 74 Schema{Save.inventory(~S,~Leaf.TransientComponent,~Unit,~True{},~Leaf.transient_component_fields(~S,~P,~identity),~Save.unit_resource,Save.transient_component(~S,~P,~identity))} 75 # Build an omitted transient resource leaf, preserving its existing actual owner 76 # on insertion. Incoming named fields are not admitted through transient staging. 77 def transient_resource(~S:Data,~P:Type,~Custom:Data,~identity:Identity.Declaration) -> Schema,Unit,Custom,Transient.stage(~Unit,~Custom),Transient.release(~Unit),resource_clear(~S),Transient.resource_insert(~S,~P,~identity),Unit,Transient.release(~Unit),Transient.take_recovery(~Unit)>: 78 Schema{Save.inventory(~S,~Unit,~Leaf.TransientResource,~True{},~Save.unit_component,~Leaf.transient_resource_fields(~S,~P,~identity),Save.transient_resource(~S,~P,~identity))} 79 def events_insert_join(~S:Data,~C:Type,~Resources:Type,~Logs:Type,~State:Type,~Custom:Data,~stage:State -> R.Position -> D.Field -> State & R.Dispatch,~release:State -> State,~clear:C -> W.Handle -> C & List<&2,R.Change>,~insert:C -> Resources -> State -> List<&2,R.Binding> -> Tick.Tick -> (C & Resources) & State,logs:Logs,observed:(C & Resources) & State) -> (C & Owner.Owner) & State: 80 match observed: 81 case ((owner,resource),state):((owner,Owner.Owner{resource,logs}),state) 82 def events_insert(~S:Data,~C:Type,~Resources:Type,~Logs:Type,~State:Type,~Custom:Data,~stage:State -> R.Position -> D.Field -> State & R.Dispatch,~release:State -> State,~clear:C -> W.Handle -> C & List<&2,R.Change>,~insert:C -> Resources -> State -> List<&2,R.Binding> -> Tick.Tick -> (C & Resources) & State,owner:C,resource:Owner.Owner,state:State,bindings:List<&2,R.Binding>,tick:Tick.Tick) -> (C & Owner.Owner) & State: 83 match resource: 84 case Owner.Owner{resource,logs}:events_insert_join(~S,~C,~Resources,~Logs,~State,~Custom,~stage,~release,~clear,~insert,logs,insert(owner,resource,state,bindings,tick)) 85 # Lift user-resource restore over Product.Owner, retaining actual 86 # logs during basic schema insertion. Logs are not persisted fields; restore-runtime 87 # performs the separate typed drain/stream-clearing boundary. 88 def with_events(~S:Data,~C:Type,~Resources:Type,~Logs:Type,~State:Type,~Custom:Data,~stage:State -> R.Position -> D.Field -> State & R.Dispatch,~release:State -> State,~clear:C -> W.Handle -> C & List<&2,R.Change>,~insert:C -> Resources -> State -> List<&2,R.Binding> -> Tick.Tick -> (C & Resources) & State,~Recovery:Type,~commit:State -> State,~takeRecovery:State -> State & Recovery,schema:Schema) -> Schema,State,Custom,stage,release,clear,events_insert(~S,~C,~Resources,~Logs,~State,~Custom,~stage,~release,~clear,~insert),Recovery,commit,takeRecovery>: 89 match schema: 90 case Schema{Ordinary.Schema{entries}}:Schema{Ordinary.Schema{entries}}