import Base import ./system.bend as Sys import ./schedule.bend as Sch import ./schedule_provision.bend as SP import ./world.bend as W import ./relation_commands.bend as A import ./ordinary_schema.bend as Schema import ./ordinary_app_relation.bend as Relation import ./ordinary_app_recipe.bend as Tree import ./ordinary_app_composer.bend as Build import ./ordinary_app_schema.bend as App import ./ordinary_app_schedule.bend as Scheduled import ./ordinary_app_observation_data.bend as DTO import ./ordinary_app_registration_fields.bend as Fields import ./relation_failure_reader.bend as Ord import ./ordinary_app_relation_system.bend as Registered # Advanced schema wrapper adapting storage/event types to the relation graph # World without changing entries. Raw schema/projection coherence is trusted setup. def graph_schema(~S:Data,~C:Type,~R:Type,schema:Schema.Schema) -> Schema.Schema,R>: match schema: case Schema.Schema{entries}:Schema.Schema{entries} # Single-schedule relation App retaining actual graph World and relation registry. # Observation derives from the same operational relation declaration. def Application(~S:Data,~C:Type,~R:Type,~E:Data,~declaration:Ord.Declaration,~target:W.Handle) -> Type: Scheduled.Application,R,A.Notice,Registered.Owner(~S,~C,~R,~E,~declaration,~target),Relation.View,Relation.observe(~S,~C,~R,~E,~declaration,~Relation.builtin_row(~S)),Registered.empty_fold(~S,~C,~R,~E,~declaration,~target)> # Advanced relation-view observation recipe; it owns no second graph or registry. def Recipe(~S:Data,~C:Type,~R:Type,~E:Data,~declaration:Ord.Declaration,~target:W.Handle) -> Type: Tree.Recipe,Relation.observe(~S,~C,~R,~E,~declaration,~Relation.builtin_row(~S))> def selected_phase(allowed:Bool,current:String,later:Unit -> String) -> String: match allowed: case True{}:current case False{}:later(Unit{}) def phase_for(steps:List<&2,Sch.Step>,+id:U32,+current:String) -> String: match steps: case []:"" case Sch.Phase{name} <> rest:phase_for(rest,id,name) case Sch.Barrier{} <> rest:phase_for(rest,id,current) case Sch.System{candidate,_} <> rest: selected_phase(U32.is_eq(candidate,id),current,unused=>phase_for(rest,id,current)) def described(~S:Data,~C:Type,~R:Type,~E:Data,~declaration:Ord.Declaration,~target:W.Handle,plan:SP.Plan,namespace:U32,name:String,steps:List<&2,Sch.Step>,needs:List<&2,SP.Requirement>,schema:Schema.Schema,R>,world:Relation.World(~S,~C,~R,~E),recipe:Recipe(~S,~C,~R,~E,~declaration,~target),debug:Bool,description:Scheduled.Description>,result:Registered.Owner(~S,~C,~R,~E,~declaration,~target) & DTO.Binding) -> Application(~S,~C,~R,~E,~declaration,~target) & Maybe<&2,Scheduled.Description>>: match description: case Scheduled.Description{App.Description{schemaEntries,DTO.Product{_,observation}},ns,label,planned}: match result: case (owners,binding):(Scheduled.Application{App.Application{schema,Build.Registered{world,owners,recipe,debug}},plan,namespace,name,steps,needs},Some{Scheduled.Description{App.Description{schemaEntries,DTO.Product{[binding],observation}},ns,label,planned}}) def snapshot_result(~S:Data,~C:Type,~R:Type,~E:Data,~declaration:Ord.Declaration,~target:W.Handle,result:Application(~S,~C,~R,~E,~declaration,~target) & Maybe<&2,Scheduled.Description>>) -> Application(~S,~C,~R,~E,~declaration,~target) & Maybe<&2,Scheduled.Description>>: match result: case (app,None{}):(app,None{}) case (Scheduled.Application{App.Application{schema,Build.Registered{world,Sys.Registry{+ns,+id,+label,+access,+cursor},recipe,debug}},plan,namespace,name,+steps,needs},Some{description}):described(~S,~C,~R,~E,~declaration,~target,plan,namespace,name,steps,needs,schema,world,recipe,debug,description,Registered.binding(~S,~C,~R,~E,~declaration,~target,Sys.Registry{ns,id,label,access,cursor},phase_for(steps,id,""))) # Set debug flag then derive the existing scheduled snapshot and declaration-bound # relation binding/phase label. Disabled bypasses observation; missing phase lookup # yields an empty label, not invented execution evidence. No graph commands apply. def snapshot(~S:Data,~C:Type,~R:Type,~E:Data,~declaration:Ord.Declaration,~target:W.Handle,app:Application(~S,~C,~R,~E,~declaration,~target),flag:Bool) -> Application(~S,~C,~R,~E,~declaration,~target) & Maybe<&2,Scheduled.Description>>: snapshot_result(~S,~C,~R,~E,~declaration,~target,Scheduled.snapshot(~S,~A.Store,~R,~A.Notice,~Registered.Owner(~S,~C,~R,~E,~declaration,~target),~Relation.View,~Relation.observe(~S,~C,~R,~E,~declaration,~Relation.builtin_row(~S)),~Registered.empty_fold(~S,~C,~R,~E,~declaration,~target),Scheduled.enabled(~S,~A.Store,~R,~A.Notice,~Registered.Owner(~S,~C,~R,~E,~declaration,~target),~Relation.View,~Relation.observe(~S,~C,~R,~E,~declaration,~Relation.builtin_row(~S)),~Registered.empty_fold(~S,~C,~R,~E,~declaration,~target),app,flag))) # Register the declaration-bound relation system on the supplied graph World. # Return its actual registry/World registration result; does not attach/run a plan. def install(~S:Data,~C:Type,~R:Type,~E:Data,~declaration:Ord.Declaration,~target:W.Handle,schema:Schema.Schema,world:Relation.World(~S,~C,~R,~E),name:String) -> Sys.RegistrationResult,R,A.Notice,Unit,Unit,Unit,Registered.reader(~S,~C,~R,~E,~declaration,~target)>: Registered.register(~S,~C,~R,~E,~declaration,~target,world,name) def attach_allowed(~S:Data,~C:Type,~R:Type,~E:Data,~declaration:Ord.Declaration,~target:W.Handle,schema:Schema.Schema,owner:Registered.Owner(~S,~C,~R,~E,~declaration,~target),name:String,plan:SP.Plan,result:Relation.World(~S,~C,~R,~E) & Bool) -> Scheduled.Attachment,R,A.Notice,Registered.Owner(~S,~C,~R,~E,~declaration,~target),Relation.View,Relation.observe(~S,~C,~R,~E,~declaration,~Relation.builtin_row(~S)),Registered.empty_fold(~S,~C,~R,~E,~declaration,~target)>,Application(~S,~C,~R,~E,~declaration,~target)>: match result: case (world,allowed): match allowed: case False{}:Scheduled.Invalid{App.Application{graph_schema(~S,~C,~R,schema),Build.Registered{world,owner,Tree.Recipe{},False{}}}} case True{}:Scheduled.attach(~S,~A.Store,~R,~A.Notice,~Registered.Owner(~S,~C,~R,~E,~declaration,~target),~Relation.View,~Relation.observe(~S,~C,~R,~E,~declaration,~Relation.builtin_row(~S)),~Registered.empty_fold(~S,~C,~R,~E,~declaration,~target),App.Application{graph_schema(~S,~C,~R,schema),Build.Registered{world,owner,Tree.Recipe{},False{}}},name,plan) def attach_namespace_checked(~S:Data,~C:Type,~R:Type,~E:Data,~declaration:Ord.Declaration,~target:W.Handle,schema:Schema.Schema,name:String,plan:SP.Plan,allowed:Bool,owner:Registered.Owner(~S,~C,~R,~E,~declaration,~target),world:Relation.World(~S,~C,~R,~E)) -> Scheduled.Attachment,R,A.Notice,Registered.Owner(~S,~C,~R,~E,~declaration,~target),Relation.View,Relation.observe(~S,~C,~R,~E,~declaration,~Relation.builtin_row(~S)),Registered.empty_fold(~S,~C,~R,~E,~declaration,~target)>,Application(~S,~C,~R,~E,~declaration,~target)>: match allowed owner: case False{} owner:Scheduled.Invalid{App.Application{graph_schema(~S,~C,~R,schema),Build.Registered{world,owner,Tree.Recipe{},False{}}}} case True{} Sys.Registry{+namespace,+id,+label,+access,+cursor}:attach_allowed(~S,~C,~R,~E,~declaration,~target,schema,Sys.Registry{namespace,id,label,access,cursor},name,plan,W.registration_matches(~S,~A.Store,~R,~A.Notice,world,id,label,access)) def attach_namespace(~S:Data,~C:Type,~R:Type,~E:Data,~declaration:Ord.Declaration,~target:W.Handle,schema:Schema.Schema,name:String,plan:SP.Plan,owner:Registered.Owner(~S,~C,~R,~E,~declaration,~target),result:Relation.World(~S,~C,~R,~E) & U32) -> Scheduled.Attachment,R,A.Notice,Registered.Owner(~S,~C,~R,~E,~declaration,~target),Relation.View,Relation.observe(~S,~C,~R,~E,~declaration,~Relation.builtin_row(~S)),Registered.empty_fold(~S,~C,~R,~E,~declaration,~target)>,Application(~S,~C,~R,~E,~declaration,~target)>: match owner: case Sys.Registry{+namespace,id,label,access,cursor}: match result: case (world,actual):attach_namespace_checked(~S,~C,~R,~E,~declaration,~target,schema,name,plan,U32.is_eq(namespace,actual),Sys.Registry{namespace,id,label,access,cursor},world) # Validate registry namespace and exact retained registration metadata before # attaching the supplied plan. Refusal returns the schema App with actual World # and owner; no system is executed. Plan/condition/provision coherence remains # ordinary trusted setup rather than authority inferred from a phase name. def attach(~S:Data,~C:Type,~R:Type,~E:Data,~declaration:Ord.Declaration,~target:W.Handle,schema:Schema.Schema,owner:Registered.Owner(~S,~C,~R,~E,~declaration,~target),world:Relation.World(~S,~C,~R,~E),name:String,plan:SP.Plan) -> Scheduled.Attachment,R,A.Notice,Registered.Owner(~S,~C,~R,~E,~declaration,~target),Relation.View,Relation.observe(~S,~C,~R,~E,~declaration,~Relation.builtin_row(~S)),Registered.empty_fold(~S,~C,~R,~E,~declaration,~target)>,Application(~S,~C,~R,~E,~declaration,~target)>: attach_namespace(~S,~C,~R,~E,~declaration,~target,schema,name,plan,owner,W.namespace(~S,~A.Store,~R,~A.Notice,world)) # Run the attached relation schedule through its registered dispatcher and supplied # condition/provision adapters, retaining App in every result. Pending graph work # applies at explicit barriers; queue admission and structural application differ. def run(~S:Data,~C:Type,~R:Type,~E:Data,~declaration:Ord.Declaration,~target:W.Handle,~condition:Relation.World(~S,~C,~R,~E) -> U32 -> Relation.World(~S,~C,~R,~E) & Bool,~provision:Relation.World(~S,~C,~R,~E) -> Relation.World(~S,~C,~R,~E) & List<&2,SP.Requirement>,app:Application(~S,~C,~R,~E,~declaration,~target)) -> Scheduled.RunResult>: Scheduled.run(~S,~A.Store,~R,~A.Notice,~Registered.Owner(~S,~C,~R,~E,~declaration,~target),~Relation.View,~Relation.observe(~S,~C,~R,~E,~declaration,~Relation.builtin_row(~S)),~Registered.empty_fold(~S,~C,~R,~E,~declaration,~target),~Maybe<&2,Unit>,~Registered.dispatch(~S,~C,~R,~E,~declaration,~target),~condition,~provision,app)