1 import Base 2 import ./machine_handler_phases.bend as Phase 3 import ./ordinary_machine_handler_bundle_context.bend as Context 4 import ./trace_facts.bend as Facts 5 import ./world.bend as W 6 import ./system.bend as Sys 7 import ./machine.bend as M 8 import ./machine_handlers.bend as H 9 import ./machine_handler_bundle.bend as B 10 import ./schedule.bend as Sch 11 import ./ordinary_machine_schedule.bend as Ordinary 12 13 # Bundle preflight precedes ordinary barrier; selected registry owners recover authored order. 14 # Advanced marker-phase owner state retaining all registered handlers. 15 # Pending/Unavailable retain authored entries; Selected also retains positions 16 # needed to restore authored order. Thread these owners; never copy registries. 17 type Owners -> H.Invocation -> Sys.Outcome,String,Unit>> is Type: 18 Pending{entries:List<&1,B.Scheduled>} 19 Unavailable{entries:List<&1,B.Scheduled>} 20 Selected{positions:List<&2,B.Position>,kept:List<&1,H.Entry>,active:Phase.Owners,phase:H.Phase} 21 # Marker outcome retains the handler bundle and world inside BundleResult, 22 # with actual schedule steps and observations. Refusal is distinct from handler 23 # failure; use the result status rather than inferring success from step count. 24 type MarkerResult -> H.Invocation -> Sys.Outcome,String,Unit>> is Type: 25 MarkerResult{result:B.BundleResult,steps:List<&2,Sch.Step>,observations:List<&2,Sch.Observation>} 26 def unobserved(~S:Data,~C:Type,~R:Type,~E:Data,~V:Data,~runner:W.World -> H.Invocation -> Sys.Outcome,String,Unit>,trace:Facts.State,world:W.World,args:H.Invocation) -> Sys.Outcome,String,Unit> & Facts.State: 27 (runner(world,args),trace) 28 def returned(~S:Data,~K:Data,~V:Data,~Q:Data,~C:Type,~R:Type,~E:Data,~runner:W.World -> H.Invocation -> Sys.Outcome,String,Unit>,value:Context.MarkerResult) -> MarkerResult: 29 match value: 30 case Context.MarkerResult{result,steps,observations,_}:MarkerResult{result,steps,observations} 31 # Execute a complete ordinary transition marker with a registered handler 32 # bundle. Checks bundle world and requirements before the structural barrier, 33 # then applies the existing exit/transition/enter sequence. Owners return in 34 # authored order. Exit/transition failures retain retryable pending transition; 35 # enter failure retains committed current state/publication. Earlier successful 36 # handler transactions are not rolled back with the entire marker. 37 # Detailed contract: docs/parity/handlers.md. 38 def run(~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,+declaration:Ordinary.Declaration,bundle:B.Bundle,world:W.World) -> MarkerResult: 39 returned(~S,~K,~V,~Q,~C,~R,~E,~runner,Context.run(~S,~K,~V,~Q,~C,~R,~E,~runner,~unobserved(~S,~C,~R,~E,~V,~runner),~"",~take,~put,~publish,~same,~requirement_same,~has,~frame,declaration,bundle,world,Facts.Off{})) 40 def context_owners(~S:Data,~K:Data,~V:Data,~Q:Data,~C:Type,~R:Type,~E:Data,~runner:W.World -> H.Invocation -> Sys.Outcome,String,Unit>,owners:Owners) -> Context.Owners: 41 match owners: 42 case Pending{entries}:Context.Pending{entries,Facts.Off{}} 43 case Unavailable{entries}:Context.Unavailable{entries,Facts.Off{}} 44 case Selected{positions,kept,active,phase}:Context.Selected{positions,kept,active,phase,Facts.Off{}} 45 def classic_owners(~S:Data,~K:Data,~V:Data,~Q:Data,~C:Type,~R:Type,~E:Data,~runner:W.World -> H.Invocation -> Sys.Outcome,String,Unit>,owners:Context.Owners) -> Owners: 46 match owners: 47 case Context.Pending{entries,_}:Pending{entries} 48 case Context.Unavailable{entries,_}:Unavailable{entries} 49 case Context.Selected{positions,kept,active,phase,_}:Selected{positions,kept,active,phase} 50 def phase_returned(~S:Data,~K:Data,~V:Data,~Q:Data,~C:Type,~R:Type,~E:Data,~runner:W.World -> H.Invocation -> Sys.Outcome,String,Unit>,result:Context.Owners & Sys.Outcome,String,Unit>) -> Owners & Sys.Outcome,String,Unit>: 51 match result: 52 case (owners,outcome):(classic_owners(~S,~K,~V,~Q,~C,~R,~E,~runner,owners),outcome) 53 # Advanced schedule-phase adapter: begin processing a pending transition and 54 # select matching handlers, returning phase owners plus system outcome. 55 # Use run for the complete preflight/barrier/phase/recovery sequence; this 56 # individual adapter does not perform bundle admission on its own. 57 def apply(~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,owners:Owners,world:W.World) -> Owners & Sys.Outcome,String,Unit>: 58 phase_returned(~S,~K,~V,~Q,~C,~R,~E,~runner,Context.apply(~S,~K,~V,~Q,~C,~R,~E,~runner,~unobserved(~S,~C,~R,~E,~V,~runner),~"",~take,~put,~publish,~same,~requirement_same,~has,~frame,context_owners(~S,~K,~V,~Q,~C,~R,~E,~runner,owners),world)) 59 # Advanced adapter for the exit-handler phase, returning handler owners 60 # and world outcome. It does not imply whole-marker rollback on failure; 61 # prefer run to preserve the full validated phase sequence. 62 def exit(~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,owners:Owners,world:W.World) -> Owners & Sys.Outcome,String,Unit>: 63 phase_returned(~S,~K,~V,~Q,~C,~R,~E,~runner,Context.exit(~S,~K,~V,~Q,~C,~R,~E,~runner,~unobserved(~S,~C,~R,~E,~V,~runner),~"",~take,~put,~publish,~same,~requirement_same,~has,~frame,context_owners(~S,~K,~V,~Q,~C,~R,~E,~runner,owners),world)) 64 # Advanced adapter for transition handlers within the ordinary marker 65 # sequence. Returns owners and world outcome; earlier successful transactions 66 # remain committed. Use run for preflight and final bundle-owner recovery. 67 def transition(~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,owners:Owners,world:W.World) -> Owners & Sys.Outcome,String,Unit>: 68 phase_returned(~S,~K,~V,~Q,~C,~R,~E,~runner,Context.transition(~S,~K,~V,~Q,~C,~R,~E,~runner,~unobserved(~S,~C,~R,~E,~V,~runner),~"",~take,~put,~publish,~same,~requirement_same,~has,~frame,context_owners(~S,~K,~V,~Q,~C,~R,~E,~runner,owners),world)) 69 # Advanced adapter for enter handlers after transition commitment. Failure 70 # retains committed state and publication; it does not restore the previous 71 # machine state. Returns owners and world outcome for final recovery. 72 def enter(~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,owners:Owners,world:W.World) -> Owners & Sys.Outcome,String,Unit>: 73 phase_returned(~S,~K,~V,~Q,~C,~R,~E,~runner,Context.enter(~S,~K,~V,~Q,~C,~R,~E,~runner,~unobserved(~S,~C,~R,~E,~V,~runner),~"",~take,~put,~publish,~same,~requirement_same,~has,~frame,context_owners(~S,~K,~V,~Q,~C,~R,~E,~runner,owners),world)) 74 def finished_returned(~S:Data,~K:Data,~V:Data,~Q:Data,~C:Type,~R:Type,~E:Data,~runner:W.World -> H.Invocation -> Sys.Outcome,String,Unit>,result:B.BundleResult & Facts.State) -> B.BundleResult: 75 match result: 76 case (result,_):result 77 # Advanced finalization adapter recovering the registered bundle in authored 78 # order and reporting the supplied phase error/status. Returns world and all 79 # retained owners; does not undo already committed handler transactions. 80 def finished(~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,namespace:U32,owners:Owners,world:W.World,error:Maybe<&2,String>) -> B.BundleResult: 81 finished_returned(~S,~K,~V,~Q,~C,~R,~E,~runner,Context.finished(~S,~K,~V,~Q,~C,~R,~E,~runner,~unobserved(~S,~C,~R,~E,~V,~runner),~"",~take,~put,~publish,~same,~requirement_same,~has,~frame,namespace,context_owners(~S,~K,~V,~Q,~C,~R,~E,~runner,owners),world,error))