import Base import ./change_tick.bend as Tick import ./world.bend as W import ./system.bend as Sys import ./machine.bend as M import ./machine_handlers.bend as H import ./machine_handler_bundle.bend as B import ./ordinary_machine_handler_bundle.bend as Operation import ./ordinary_machine_schedule.bend as Ordinary import ./schedule.bend as Sch # Detached handler selector/requirements and actual registry metadata for display. # Does not own a handler, grant capabilities or duplicate Local/capture state. type EntryView is Data: EntryView{selector:B.Selector,requirements:List<&2,Q>,namespace:U32,entryId:U32,id:U32,name:String,access:List<&2,String>,cursor:Maybe<&2,Tick.Tick>} # Detached marker schedule phases and ordered handler entry views. type Description is Data: Description{namespace:U32,name:String,steps:List<&2,Sch.Step>,entries:List<&2,EntryView>} # Affine World, handler bundle and extra owners with declaration/debug. Recovering # retains an existing MarkerResult rather than silently starting another marker. type Application -> H.Invocation -> Sys.Outcome,String,Unit>,Extra:Type> is Type: Application{world:W.World,bundle:B.Bundle,declaration:Ordinary.Declaration,extra:Extra,debug:Bool} Recovering{result:Operation.MarkerResult,declaration:Ordinary.Declaration,extra:Extra,debug:Bool} def entries_returned(~S:Data,~K:Data,~V:Data,~Q:Data,~C:Type,~R:Type,~E:Data,~runner:W.World -> H.Invocation -> Sys.Outcome,String,Unit>,+selector:B.Selector,+requirements:List<&2,Q>,+namespace:U32,+entryId:U32,+id:U32,+name:String,+access:List<&2,String>,+cursor:Maybe<&2,Tick.Tick>,result:List<&1,B.Scheduled> & List<&2,EntryView>) -> List<&1,B.Scheduled> & List<&2,EntryView>: (rest,views)=result (B.Scheduled{selector,requirements,H.Entry{entryId,Sys.Registry{namespace,id,name,access,cursor}}} <> rest,EntryView{selector,requirements,namespace,entryId,id,name,access,cursor} <> views) # Describe scheduled handlers in list order while returning all actual owners. # Detached selector/requirements/registry fields do not execute handlers or prove # that requirements are satisfied. This advanced helper has no debug bypass. def entries(~S:Data,~K:Data,~V:Data,~Q:Data,~C:Type,~R:Type,~E:Data,~runner:W.World -> H.Invocation -> Sys.Outcome,String,Unit>,items:List<&1,B.Scheduled>) -> List<&1,B.Scheduled> & List<&2,EntryView>: match items: case []:([],[]) case B.Scheduled{selector,requirements,H.Entry{entryId,Sys.Registry{namespace,id,name,access,cursor}}} <> rest:entries_returned(~S,~K,~V,~Q,~C,~R,~E,~runner,selector,requirements,namespace,entryId,id,name,access,cursor,entries(~S,~K,~V,~Q,~C,~R,~E,~runner,rest)) # Retain supplied actual World/bundle/extra owners as an App with debug off. # No preflight, phase execution or payload cleanup occurs at binding. def bind(~S:Data,~K:Data,~V:Data,~Q:Data,~C:Type,~R:Type,~E:Data,~runner:W.World -> H.Invocation -> Sys.Outcome,String,Unit>,~Extra:Type,world:W.World,bundle:B.Bundle,declaration:Ordinary.Declaration,extra:Extra) -> Application: Application{world,bundle,declaration,extra,False{}} # Change only observation enablement, retaining normal or recovering owner state. def enabled(~S:Data,~K:Data,~V:Data,~Q:Data,~C:Type,~R:Type,~E:Data,~runner:W.World -> H.Invocation -> Sys.Outcome,String,Unit>,~Extra:Type,app:Application,debug:Bool) -> Application: match app: case Application{world,bundle,declaration,extra,_}:Application{world,bundle,declaration,extra,debug} case Recovering{result,declaration,extra,_}:Recovering{result,declaration,extra,debug} def described(~S:Data,~K:Data,~V:Data,~Q:Data,~C:Type,~R:Type,~E:Data,~runner:W.World -> H.Invocation -> Sys.Outcome,String,Unit>,~Extra:Type,world:W.World,+namespace:U32,+name:String,extra:Extra,result:List<&1,B.Scheduled> & List<&2,EntryView>) -> Application & Maybe<&2,Description>: (items,views)=result (Application{world,B.Bundle{namespace,items},Ordinary.Declaration{name},extra,True{}},Some{Description{namespace,name,Ordinary.steps(),views}}) # Describe normal enabled App entries, returning all actual handler owners. # Disabled or Recovering returns None without entry derivation; does not run # handlers, apply queued transitions or validate requirements. def snapshot(~S:Data,~K:Data,~V:Data,~Q:Data,~C:Type,~R:Type,~E:Data,~runner:W.World -> H.Invocation -> Sys.Outcome,String,Unit>,~Extra:Type,app:Application) -> Application & Maybe<&2,Description>: match app: case Application{world,B.Bundle{namespace,items},Ordinary.Declaration{name},extra,True{}}:described(~S,~K,~V,~Q,~C,~R,~E,~runner,~Extra,world,namespace,name,extra,entries(~S,~K,~V,~Q,~C,~R,~E,~runner,items)) case app:(app,None{}) # Retained marker result plus original declaration, extra owners and debug. # Inspect its BundleResult for status rather than inferring success from observations. type RunResult -> H.Invocation -> Sys.Outcome,String,Unit>,Extra:Type> is Type: RunResult{result:Operation.MarkerResult,declaration:Ordinary.Declaration,extra:Extra,debug:Bool} # Normal App executes ordinary marker preflight/barrier/phase machinery. Recovering # passes through its retained result without rerun. Returned marker keeps actual # owners; exit/transition failure leaves retryable pending work, while enter failure # retains committed current/publication. No schedule-wide transaction is added. def run_app(~S:Data,~K:Data,~V:Data,~Q:Data,~C:Type,~R:Type,~E:Data,~runner:W.World -> H.Invocation -> Sys.Outcome,String,Unit>,~take:W.World -> W.World & M.Slot,~put:W.World -> M.Slot -> W.World,~publish:W.World -> M.Transition -> W.World,~same:V -> V -> Bool,~requirement_same:Q -> Q -> Bool,~has:W.World -> Q -> W.World & Bool,~frame:W.World -> W.World,~Extra:Type,app:Application) -> RunResult: match app: case Recovering{result,declaration,extra,debug}:RunResult{result,declaration,extra,debug} case Application{world,bundle,+declaration,extra,debug}:RunResult{Operation.run(~S,~K,~V,~Q,~C,~R,~E,~runner,~take,~put,~publish,~same,~requirement_same,~has,~frame,declaration,bundle,world),declaration,extra,debug}