ordinary_machine_handler_bundle.bend

bendvy-ecs-core@0.1.0.0 · 81 lines · 0 definitions

1import Base
2import bendvy-ecs-core@0.1.0.0/machine_handler_phases.bend as Phase
3import bendvy-ecs-core@0.1.0.0/ordinary_machine_handler_bundle_context.bend as Context
4import bendvy-ecs-core@0.1.0.0/trace_facts.bend as Facts
5import bendvy-ecs-core@0.1.0.0/world.bend as W
6import bendvy-ecs-core@0.1.0.0/system.bend as Sys
7import bendvy-ecs-core@0.1.0.0/machine.bend as M
8import bendvy-ecs-core@0.1.0.0/machine_handlers.bend as H
9import bendvy-ecs-core@0.1.0.0/machine_handler_bundle.bend as B
10import bendvy-ecs-core@0.1.0.0/schedule.bend as Sch
11import bendvy-ecs-core@0.1.0.0/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.
17type Owners<S:Data,K:Data,V:Data,Q:Data,C:Type,R:Type,E:Data,-runner:W.World<S,C,R,E> -> H.Invocation<V> -> Sys.Outcome<W.World<S,C,R,E>,String,Unit>> is Type:
18  Pending{entries:List<&1,B.Scheduled<S,K,V,Q,C,R,E,runner>>}
19  Unavailable{entries:List<&1,B.Scheduled<S,K,V,Q,C,R,E,runner>>}
20  Selected{positions:List<&2,B.Position<S,K,V,Q>>,kept:List<&1,H.Entry<S,C,R,E,V,runner>>,active:Phase.Owners<S,K,V,C,R,E,runner>,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.
24type MarkerResult<S:Data,K:Data,V:Data,Q:Data,C:Type,R:Type,E:Data,-runner:W.World<S,C,R,E> -> H.Invocation<V> -> Sys.Outcome<W.World<S,C,R,E>,String,Unit>> is Type:
25  MarkerResult{result:B.BundleResult<S,K,V,Q,C,R,E,runner>,steps:List<&2,Sch.Step>,observations:List<&2,Sch.Observation>}
26def unobserved(~S:Data,~C:Type,~R:Type,~E:Data,~V:Data,~runner:W.World<S,C,R,E> -> H.Invocation<V> -> Sys.Outcome<W.World<S,C,R,E>,String,Unit>,trace:Facts.State,world:W.World<S,C,R,E>,args:H.Invocation<V>) -> Sys.Outcome<W.World<S,C,R,E>,String,Unit> & Facts.State:
27  (runner(world,args),trace)
28def returned(~S:Data,~K:Data,~V:Data,~Q:Data,~C:Type,~R:Type,~E:Data,~runner:W.World<S,C,R,E> -> H.Invocation<V> -> Sys.Outcome<W.World<S,C,R,E>,String,Unit>,value:Context.MarkerResult<S,K,V,Q,C,R,E,runner>) -> MarkerResult<S,K,V,Q,C,R,E,runner>:
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.
38def run(~S:Data,~K:Data,~V:Data,~Q:Data,~C:Type,~R:Type,~E:Data,~runner:W.World<S,C,R,E> -> H.Invocation<V> -> Sys.Outcome<W.World<S,C,R,E>,String,Unit>,~take:W.World<S,C,R,E> -> W.World<S,C,R,E> & M.Slot<S,K,V>,~put:W.World<S,C,R,E> -> M.Slot<S,K,V> -> W.World<S,C,R,E>,~publish:W.World<S,C,R,E> -> M.Transition<V> -> W.World<S,C,R,E>,~same:V -> V -> Bool,~requirement_same:Q -> Q -> Bool,~has:W.World<S,C,R,E> -> Q -> W.World<S,C,R,E> & Bool,~frame:W.World<S,C,R,E> -> W.World<S,C,R,E>,+declaration:Ordinary.Declaration<S>,bundle:B.Bundle<S,K,V,Q,C,R,E,runner>,world:W.World<S,C,R,E>) -> MarkerResult<S,K,V,Q,C,R,E,runner>:
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{}))
40def context_owners(~S:Data,~K:Data,~V:Data,~Q:Data,~C:Type,~R:Type,~E:Data,~runner:W.World<S,C,R,E> -> H.Invocation<V> -> Sys.Outcome<W.World<S,C,R,E>,String,Unit>,owners:Owners<S,K,V,Q,C,R,E,runner>) -> Context.Owners<S,K,V,Q,C,R,E,runner>:
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{}}
45def classic_owners(~S:Data,~K:Data,~V:Data,~Q:Data,~C:Type,~R:Type,~E:Data,~runner:W.World<S,C,R,E> -> H.Invocation<V> -> Sys.Outcome<W.World<S,C,R,E>,String,Unit>,owners:Context.Owners<S,K,V,Q,C,R,E,runner>) -> Owners<S,K,V,Q,C,R,E,runner>:
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}
50def phase_returned(~S:Data,~K:Data,~V:Data,~Q:Data,~C:Type,~R:Type,~E:Data,~runner:W.World<S,C,R,E> -> H.Invocation<V> -> Sys.Outcome<W.World<S,C,R,E>,String,Unit>,result:Context.Owners<S,K,V,Q,C,R,E,runner> & Sys.Outcome<W.World<S,C,R,E>,String,Unit>) -> Owners<S,K,V,Q,C,R,E,runner> & Sys.Outcome<W.World<S,C,R,E>,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.
57def apply(~S:Data,~K:Data,~V:Data,~Q:Data,~C:Type,~R:Type,~E:Data,~runner:W.World<S,C,R,E> -> H.Invocation<V> -> Sys.Outcome<W.World<S,C,R,E>,String,Unit>,~take:W.World<S,C,R,E> -> W.World<S,C,R,E> & M.Slot<S,K,V>,~put:W.World<S,C,R,E> -> M.Slot<S,K,V> -> W.World<S,C,R,E>,~publish:W.World<S,C,R,E> -> M.Transition<V> -> W.World<S,C,R,E>,~same:V -> V -> Bool,~requirement_same:Q -> Q -> Bool,~has:W.World<S,C,R,E> -> Q -> W.World<S,C,R,E> & Bool,~frame:W.World<S,C,R,E> -> W.World<S,C,R,E>,owners:Owners<S,K,V,Q,C,R,E,runner>,world:W.World<S,C,R,E>) -> Owners<S,K,V,Q,C,R,E,runner> & Sys.Outcome<W.World<S,C,R,E>,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.
62def exit(~S:Data,~K:Data,~V:Data,~Q:Data,~C:Type,~R:Type,~E:Data,~runner:W.World<S,C,R,E> -> H.Invocation<V> -> Sys.Outcome<W.World<S,C,R,E>,String,Unit>,~take:W.World<S,C,R,E> -> W.World<S,C,R,E> & M.Slot<S,K,V>,~put:W.World<S,C,R,E> -> M.Slot<S,K,V> -> W.World<S,C,R,E>,~publish:W.World<S,C,R,E> -> M.Transition<V> -> W.World<S,C,R,E>,~same:V -> V -> Bool,~requirement_same:Q -> Q -> Bool,~has:W.World<S,C,R,E> -> Q -> W.World<S,C,R,E> & Bool,~frame:W.World<S,C,R,E> -> W.World<S,C,R,E>,owners:Owners<S,K,V,Q,C,R,E,runner>,world:W.World<S,C,R,E>) -> Owners<S,K,V,Q,C,R,E,runner> & Sys.Outcome<W.World<S,C,R,E>,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.
67def transition(~S:Data,~K:Data,~V:Data,~Q:Data,~C:Type,~R:Type,~E:Data,~runner:W.World<S,C,R,E> -> H.Invocation<V> -> Sys.Outcome<W.World<S,C,R,E>,String,Unit>,~take:W.World<S,C,R,E> -> W.World<S,C,R,E> & M.Slot<S,K,V>,~put:W.World<S,C,R,E> -> M.Slot<S,K,V> -> W.World<S,C,R,E>,~publish:W.World<S,C,R,E> -> M.Transition<V> -> W.World<S,C,R,E>,~same:V -> V -> Bool,~requirement_same:Q -> Q -> Bool,~has:W.World<S,C,R,E> -> Q -> W.World<S,C,R,E> & Bool,~frame:W.World<S,C,R,E> -> W.World<S,C,R,E>,owners:Owners<S,K,V,Q,C,R,E,runner>,world:W.World<S,C,R,E>) -> Owners<S,K,V,Q,C,R,E,runner> & Sys.Outcome<W.World<S,C,R,E>,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.
72def enter(~S:Data,~K:Data,~V:Data,~Q:Data,~C:Type,~R:Type,~E:Data,~runner:W.World<S,C,R,E> -> H.Invocation<V> -> Sys.Outcome<W.World<S,C,R,E>,String,Unit>,~take:W.World<S,C,R,E> -> W.World<S,C,R,E> & M.Slot<S,K,V>,~put:W.World<S,C,R,E> -> M.Slot<S,K,V> -> W.World<S,C,R,E>,~publish:W.World<S,C,R,E> -> M.Transition<V> -> W.World<S,C,R,E>,~same:V -> V -> Bool,~requirement_same:Q -> Q -> Bool,~has:W.World<S,C,R,E> -> Q -> W.World<S,C,R,E> & Bool,~frame:W.World<S,C,R,E> -> W.World<S,C,R,E>,owners:Owners<S,K,V,Q,C,R,E,runner>,world:W.World<S,C,R,E>) -> Owners<S,K,V,Q,C,R,E,runner> & Sys.Outcome<W.World<S,C,R,E>,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))
74def finished_returned(~S:Data,~K:Data,~V:Data,~Q:Data,~C:Type,~R:Type,~E:Data,~runner:W.World<S,C,R,E> -> H.Invocation<V> -> Sys.Outcome<W.World<S,C,R,E>,String,Unit>,result:B.BundleResult<S,K,V,Q,C,R,E,runner> & Facts.State) -> B.BundleResult<S,K,V,Q,C,R,E,runner>:
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.
80def finished(~S:Data,~K:Data,~V:Data,~Q:Data,~C:Type,~R:Type,~E:Data,~runner:W.World<S,C,R,E> -> H.Invocation<V> -> Sys.Outcome<W.World<S,C,R,E>,String,Unit>,~take:W.World<S,C,R,E> -> W.World<S,C,R,E> & M.Slot<S,K,V>,~put:W.World<S,C,R,E> -> M.Slot<S,K,V> -> W.World<S,C,R,E>,~publish:W.World<S,C,R,E> -> M.Transition<V> -> W.World<S,C,R,E>,~same:V -> V -> Bool,~requirement_same:Q -> Q -> Bool,~has:W.World<S,C,R,E> -> Q -> W.World<S,C,R,E> & Bool,~frame:W.World<S,C,R,E> -> W.World<S,C,R,E>,namespace:U32,owners:Owners<S,K,V,Q,C,R,E,runner>,world:W.World<S,C,R,E>,error:Maybe<&2,String>) -> B.BundleResult<S,K,V,Q,C,R,E,runner>:
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))