1 import Base 2 import ./ordinary_app_execution_recipe.bend as Recipe 3 import ./ordinary_app_composer.bend as Build 4 import ./ordinary_app_recipe.bend as Tree 5 import ./ordinary_app_schema.bend as App 6 import ./ordinary_app_schedule.bend as Single 7 import ./ordinary_app_execution.bend as Execute 8 import ./ordinary_app_registration_fields.bend as Records 9 import ./ordinary_app_observation_data.bend as DTO 10 import ./ordinary_schema.bend as Schema 11 import ./captured_schedule_composition.bend as Identity 12 import ./execution_context.bend as Ctx 13 import ./world.bend as W 14 import ./system.bend as Sys 15 import ./schedule.bend as Sch 16 import ./schedule_provision.bend as SP 17 # Executable application owning one world, registered system owners and a 18 # declaration-derived execution recipe. Thread this affine value through run; 19 # its type binds schema, observations and dispatch together. 20 # Example: examples/scheduled-health/registration.bend, Application(). 21 type Application -> W.World & DTO.Observation,-fold:H -> H & List<&2,Records.Fields>,Error:Data,-plan:H -> H & SP.Plan,-dispatch:H -> Ctx.Context> -> U32 -> H & Sys.Outcome>,Error,Unit>,-provide:W.World -> W.World & List<&2,SP.Requirement>> is Type: 22 Application{app:Single.Application,execution:Recipe.Recipe,H,Error,plan,dispatch,provide>} 23 def initial(~S:Data,barrier:Bool,plan:SP.Plan) -> SP.Plan: 24 match barrier: 25 case False{}:plan 26 case True{}:SP.Sequence{SP.Entry{Sch.Barrier{},[]},plan} 27 def built(~S:Data,~C:Type,~R:Type,~E:Data,~H:Type,~Rows:Data,~observe:W.World -> W.World & DTO.Observation,~fold:H -> H & List<&2,Records.Fields>,~Error:Data,~plan:H -> H & SP.Plan,~dispatch:H -> Ctx.Context> -> U32 -> H & Sys.Outcome>,Error,Unit>,~provide:W.World -> W.World & List<&2,SP.Requirement>,schema:Schema.Schema,world:W.World,recipe:Tree.Recipe,Rows,observe>,debug:Bool,+selected:SP.Plan,result:SP.Build) -> Single.Attachment,H,Rows,observe,fold,Error,plan,dispatch,provide>,Application>: 28 match result: 29 case SP.Invalid{owners}:Single.Invalid{Recipe.Registered{Build.Registered{world,owners,recipe,debug},Recipe.Recipe{}}} 30 case SP.Built{SP.Provisioned{Sch.Schedule{namespace,name,steps,owners},needs}}:Single.Attached{Application{Single.Application{App.Application{schema,Build.Registered{world,owners,recipe,debug}},selected,namespace,name,steps,needs},Recipe.Recipe{}}} 31 def checked(~S:Data,~C:Type,~R:Type,~E:Data,~H:Type,~Rows:Data,~observe:W.World -> W.World & DTO.Observation,~fold:H -> H & List<&2,Records.Fields>,~Error:Data,~plan:H -> H & SP.Plan,~dispatch:H -> Ctx.Context> -> U32 -> H & Sys.Outcome>,Error,Unit>,~provide:W.World -> W.World & List<&2,SP.Requirement>,valid:Bool,schema:Schema.Schema,world:W.World,owners:H,recipe:Tree.Recipe,Rows,observe>,debug:Bool,+selected:SP.Plan,namespace:U32,name:String,ids:List<&2,U32>) -> Single.Attachment,H,Rows,observe,fold,Error,plan,dispatch,provide>,Application>: 32 match valid: 33 case False{}:Single.Invalid{Recipe.Registered{Build.Registered{world,owners,recipe,debug},Recipe.Recipe{}}} 34 case True{}:built(~S,~C,~R,~E,~H,~Rows,~observe,~fold,~Error,~plan,~dispatch,~provide,schema,world,recipe,debug,selected,SP.build(~S,~C,~R,~E,~H,namespace,name,selected,ids,owners)) 35 def folded(~S:Data,~C:Type,~R:Type,~E:Data,~H:Type,~Rows:Data,~observe:W.World -> W.World & DTO.Observation,~fold:H -> H & List<&2,Records.Fields>,~Error:Data,~plan:H -> H & SP.Plan,~dispatch:H -> Ctx.Context> -> U32 -> H & Sys.Outcome>,Error,Unit>,~provide:W.World -> W.World & List<&2,SP.Requirement>,schema:Schema.Schema,world:W.World,recipe:Tree.Recipe,Rows,observe>,debug:Bool,selected:SP.Plan,name:String,result:H & List<&2,Records.Fields>) -> Single.Attachment,H,Rows,observe,fold,Error,plan,dispatch,provide>,Application>: 36 match world: 37 case W.World{+namespace,allocator,store,resource,events,pending,+registrations,nextSystemId,clock}: 38 match result: 39 case (owners,+fields):checked(~S,~C,~R,~E,~H,~Rows,~observe,~fold,~Error,~plan,~dispatch,~provide,Identity.identities_valid(namespace,registrations,Recipe.identities(~S,fields),[]),schema,W.World{namespace,allocator,store,resource,events,pending,registrations,nextSystemId,clock},owners,recipe,debug,selected,namespace,name,Identity.ids(Recipe.identities(~S,fields))) 40 def planned(~S:Data,~C:Type,~R:Type,~E:Data,~H:Type,~Rows:Data,~observe:W.World -> W.World & DTO.Observation,~fold:H -> H & List<&2,Records.Fields>,~Error:Data,~plan:H -> H & SP.Plan,~dispatch:H -> Ctx.Context> -> U32 -> H & Sys.Outcome>,Error,Unit>,~provide:W.World -> W.World & List<&2,SP.Requirement>,schema:Schema.Schema,world:W.World,recipe:Tree.Recipe,Rows,observe>,debug:Bool,name:String,barrier:Bool,result:H & SP.Plan) -> Single.Attachment,H,Rows,observe,fold,Error,plan,dispatch,provide>,Application>: 41 match result: 42 case (owners,selected):folded(~S,~C,~R,~E,~H,~Rows,~observe,~fold,~Error,~plan,~dispatch,~provide,schema,world,recipe,debug,initial(~S,barrier,selected),name,fold(owners)) 43 # Derive and validate a schedule from the existing registered application. 44 # Returns Attached with the executable App, or Invalid with the original 45 # registered owner; world and registration owners are never copied. 46 # Validates registration identity and schedule structure before execution. 47 # barrier=True prepends an explicit barrier; False adds none. Registration 48 # order is retained by the derived plan. No system body runs here. 49 # Example: examples/scheduled-health/registration.bend, attach(). 50 def attach(~S:Data,~C:Type,~R:Type,~E:Data,~H:Type,~Rows:Data,~observe:W.World -> W.World & DTO.Observation,~fold:H -> H & List<&2,Records.Fields>,~Error:Data,~plan:H -> H & SP.Plan,~dispatch:H -> Ctx.Context> -> U32 -> H & Sys.Outcome>,Error,Unit>,~provide:W.World -> W.World & List<&2,SP.Requirement>,schema:Schema.Schema,value:Recipe.Registered,H,Rows,observe,fold,Error,plan,dispatch,provide>,name:String,barrier:Bool) -> Single.Attachment,H,Rows,observe,fold,Error,plan,dispatch,provide>,Application>: 51 match value: 52 case Recipe.Registered{Build.Registered{world,owners,recipe,debug},_}:planned(~S,~C,~R,~E,~H,~Rows,~observe,~fold,~Error,~plan,~dispatch,~provide,schema,world,recipe,debug,name,barrier,plan(owners)) 53 def returned(~S:Data,~C:Type,~R:Type,~E:Data,~H:Type,~Rows:Data,~observe:W.World -> W.World & DTO.Observation,~fold:H -> H & List<&2,Records.Fields>,~Error:Data,~plan:H -> H & SP.Plan,~dispatch:H -> Ctx.Context> -> U32 -> H & Sys.Outcome>,Error,Unit>,~provide:W.World -> W.World & List<&2,SP.Requirement>,result:Single.RunResult,S,Error>) -> Single.RunResult,S,Error>: 54 match result: 55 case Single.Finished{app,observations}:Single.Finished{Application{app,Recipe.Recipe{}},observations} 56 case Single.Failed{app,error,observations}:Single.Failed{Application{app,Recipe.Recipe{}},error,observations} 57 case Single.Rejected{app}:Single.Rejected{Application{app,Recipe.Recipe{}}} 58 case Single.Missing{app,requirements}:Single.Missing{Application{app,Recipe.Recipe{}},requirements} 59 def executed(~S:Data,~C:Type,~R:Type,~E:Data,~H:Type,~Rows:Data,~observe:W.World -> W.World & DTO.Observation,~fold:H -> H & List<&2,Records.Fields>,~Error:Data,~plan:H -> H & SP.Plan,~dispatch:H -> Ctx.Context> -> U32 -> H & Sys.Outcome>,Error,Unit>,~provide:W.World -> W.World & List<&2,SP.Requirement>,result:Execute.Run,S,Error>) -> Execute.Run,S,Error>: 60 match result: 61 case Execute.Run{result,batch}:Execute.Run{returned(~S,~C,~R,~E,~H,~Rows,~observe,~fold,~Error,~plan,~dispatch,~provide,result),batch} 62 # Run the derived schedule with a per-system condition(world, id), returning 63 # an Execute.Run containing the application result and detached trace batch. 64 # Finished/Failed/Rejected/Missing all retain the application owner. A failed 65 # system rolls back its ECS transaction, preserving earlier successful systems; 66 # retained local/capture state and host IO follow their own contracts. 67 # Structural commands become visible only at plan barriers, not at run end. 68 # A false condition bypasses dispatch and leaves its invocation/lifecycle cursor 69 # unchanged. condition is a trusted world-transforming callback: its returned 70 # world is retained even when false; it is not confined to read-only access. 71 def run_when(~S:Data,~C:Type,~R:Type,~E:Data,~H:Type,~Rows:Data,~observe:W.World -> W.World & DTO.Observation,~fold:H -> H & List<&2,Records.Fields>,~Error:Data,~plan:H -> H & SP.Plan,~dispatch:H -> Ctx.Context> -> U32 -> H & Sys.Outcome>,Error,Unit>,~provide:W.World -> W.World & List<&2,SP.Requirement>,~condition:W.World -> U32 -> W.World & Bool,value:Application) -> Execute.Run,S,Error>: 72 match value: 73 case Application{app,_}:executed(~S,~C,~R,~E,~H,~Rows,~observe,~fold,~Error,~plan,~dispatch,~provide,Execute.run(~S,~C,~R,~E,~H,~Rows,~observe,~fold,~Error,~dispatch,~condition,~provide,app)) 74 # Return the same application owner with debug enabled or disabled. 75 # Enabling derives observations from declarations; no repeated metadata input 76 # is required. This call neither runs systems nor advances their cursors. 77 def enabled(~S:Data,~C:Type,~R:Type,~E:Data,~H:Type,~Rows:Data,~observe:W.World -> W.World & DTO.Observation,~fold:H -> H & List<&2,Records.Fields>,~Error:Data,~plan:H -> H & SP.Plan,~dispatch:H -> Ctx.Context> -> U32 -> H & Sys.Outcome>,Error,Unit>,~provide:W.World -> W.World & List<&2,SP.Requirement>,value:Application,flag:Bool) -> Application: 78 match value: 79 case Application{app,execution}:Application{Single.enabled(~S,~C,~R,~E,~H,~Rows,~observe,~fold,app,flag),execution} 80 def observed(~S:Data,~C:Type,~R:Type,~E:Data,~H:Type,~Rows:Data,~observe:W.World -> W.World & DTO.Observation,~fold:H -> H & List<&2,Records.Fields>,~Error:Data,~plan:H -> H & SP.Plan,~dispatch:H -> Ctx.Context> -> U32 -> H & Sys.Outcome>,Error,Unit>,~provide:W.World -> W.World & List<&2,SP.Requirement>,result:Single.Application & Maybe<&2,Single.Description>) -> Application & Maybe<&2,Single.Description>: 81 match result: 82 case (app,view):(Application{app,Recipe.Recipe{}},view) 83 # Return the application owner with Some detached debug description when 84 # enabled, or None when disabled. This is inspection, not a restorable save. 85 # It does not execute systems or consume their lifecycle/event cursors. 86 # Example: examples/scheduled-health/registration.bend, snapshot(). 87 def snapshot(~S:Data,~C:Type,~R:Type,~E:Data,~H:Type,~Rows:Data,~observe:W.World -> W.World & DTO.Observation,~fold:H -> H & List<&2,Records.Fields>,~Error:Data,~plan:H -> H & SP.Plan,~dispatch:H -> Ctx.Context> -> U32 -> H & Sys.Outcome>,Error,Unit>,~provide:W.World -> W.World & List<&2,SP.Requirement>,value:Application) -> Application & Maybe<&2,Single.Description>: 88 match value: 89 case Application{app,_}:observed(~S,~C,~R,~E,~H,~Rows,~observe,~fold,~Error,~plan,~dispatch,~provide,Single.snapshot(~S,~C,~R,~E,~H,~Rows,~observe,~fold,app)) 90 # Rebuild the derived schedule from the same application registrations. 91 # Preserves the world and invocation owners; name and leading barrier replace 92 # the prior plan configuration. Returns Attached, or Invalid with the retained 93 # registered owner. Does not run bodies or recreate their registration state. 94 # Example: examples/scheduled-health/registration.bend, replan(). 95 def replan(~S:Data,~C:Type,~R:Type,~E:Data,~H:Type,~Rows:Data,~observe:W.World -> W.World & DTO.Observation,~fold:H -> H & List<&2,Records.Fields>,~Error:Data,~plan:H -> H & SP.Plan,~dispatch:H -> Ctx.Context> -> U32 -> H & Sys.Outcome>,Error,Unit>,~provide:W.World -> W.World & List<&2,SP.Requirement>,value:Application,name:String,barrier:Bool) -> Single.Attachment,H,Rows,observe,fold,Error,plan,dispatch,provide>,Application>: 96 match value: 97 case Application{Single.Application{App.Application{schema,app},_,_,_,_,_},_}:attach(~S,~C,~R,~E,~H,~Rows,~observe,~fold,~Error,~plan,~dispatch,~provide,schema,Recipe.Registered{app,Recipe.Recipe{}},name,barrier) 98 def always(~World:Type,world:World,id:U32) -> World & Bool: 99 (world,True{}) 100 # Execute one pass of the declaration-derived schedule with all systems 101 # eligible; returns Execute.Run with a retained App on every result variant 102 # (Finished, Failed, Rejected or Missing) plus a detached trace batch. 103 # Earlier successful systems remain committed if a later system fails. 104 # Deferred commands remain pending until an explicit schedule barrier. 105 # Example: examples/scheduled-health/application.bend, ticks(). 106 def run(~S:Data,~C:Type,~R:Type,~E:Data,~H:Type,~Rows:Data,~observe:W.World -> W.World & DTO.Observation,~fold:H -> H & List<&2,Records.Fields>,~Error:Data,~plan:H -> H & SP.Plan,~dispatch:H -> Ctx.Context> -> U32 -> H & Sys.Outcome>,Error,Unit>,~provide:W.World -> W.World & List<&2,SP.Requirement>,value:Application) -> Execute.Run,S,Error>: 107 run_when(~S,~C,~R,~E,~H,~Rows,~observe,~fold,~Error,~plan,~dispatch,~provide,~always(~W.World),value) 108 def inspected(~S:Data,~C:Type,~R:Type,~E:Data,~H:Type,~Rows:Data,~observe:W.World -> W.World & DTO.Observation,~fold:H -> H & List<&2,Records.Fields>,~Error:Data,~plan:H -> H & SP.Plan,~dispatch:H -> Ctx.Context> -> U32 -> H & Sys.Outcome>,Error,Unit>,~provide:W.World -> W.World & List<&2,SP.Requirement>,~Value:Data,schema:Schema.Schema,owners:H,recipe:Tree.Recipe,Rows,observe>,debug:Bool,selected:SP.Plan,namespace:U32,name:String,steps:List<&2,Sch.Step>,needs:List<&2,SP.Requirement>,result:W.World & Value) -> Application & Value: 109 match result: 110 case (world,value):(Application{Single.Application{App.Application{schema,Build.Registered{world,owners,recipe,debug}},selected,namespace,name,steps,needs},Recipe.Recipe{}},value) 111 # Advanced world projection callback: consumes and returns the application 112 # together with detached Data Value. project must return the world owner. 113 # This adapter does not itself restrict project to read-only operations; use 114 # public Inspector projections for noninterfering observation. No system is run. 115 def inspect(~S:Data,~C:Type,~R:Type,~E:Data,~H:Type,~Rows:Data,~observe:W.World -> W.World & DTO.Observation,~fold:H -> H & List<&2,Records.Fields>,~Error:Data,~plan:H -> H & SP.Plan,~dispatch:H -> Ctx.Context> -> U32 -> H & Sys.Outcome>,Error,Unit>,~provide:W.World -> W.World & List<&2,SP.Requirement>,~Value:Data,~project:W.World -> W.World & Value,value:Application) -> Application & Value: 116 match value: 117 case Application{Single.Application{App.Application{schema,Build.Registered{world,owners,recipe,debug}},selected,namespace,name,steps,needs},_}:inspected(~S,~C,~R,~E,~H,~Rows,~observe,~fold,~Error,~plan,~dispatch,~provide,~Value,schema,owners,recipe,debug,selected,namespace,name,steps,needs,project(world)) 118 119 # Advanced adapter to modify retained invocation owners with caller-supplied 120 # arguments, including affine Type values. 121 # Returns the application with the same world, plan and execution recipe; 122 # modify must return H and preserve registration identity/metadata invariants. 123 # It is not an ECS transaction and does not validate arbitrary owner changes. 124 def update_owners(~S:Data,~C:Type,~R:Type,~E:Data,~H:Type,~Rows:Data,~observe:W.World -> W.World & DTO.Observation,~fold:H -> H & List<&2,Records.Fields>,~Error:Data,~plan:H -> H & SP.Plan,~dispatch:H -> Ctx.Context> -> U32 -> H & Sys.Outcome>,Error,Unit>,~provide:W.World -> W.World & List<&2,SP.Requirement>,~Args:Type,~modify:H -> Args -> H,value:Application,args:Args) -> Application: 125 match value: 126 case Application{Single.Application{App.Application{schema,Build.Registered{world,owners,recipe,debug}},selected,namespace,name,steps,needs},execution}:Application{Single.Application{App.Application{schema,Build.Registered{world,modify(owners,args),recipe,debug}},selected,namespace,name,steps,needs},execution}