import Base import ./change_tick.bend as Tick import ./event_runtime.bend as Ev import ./relation_commands.bend as A import ./capabilities.bend as Cap import ./inspector_stream_frame.bend as F import ./inspector_transition_projection.bend as T import ./inspector_failure_projection.bend as Fail import ./machine_stream.bend as St # Detached machine transition log and declaration-scoped relation failure view. # Does not contain mutable World ownership or a new reader acknowledgement. type Values is Data: Values{transitions:T.TransitionLog,failures:Fail.FailureView} # Detached transition name plus projected stream values, not a restorable save. type Description is Data: Description{name:String,values:Values} # Project the authored transition declaration name without reading runtime state. def transition_name(~S:Data,~K:Data,~V:Data,~R:Type,~take:R -> R & St.Stream,declaration:T.Declaration) -> String: match declaration: case T.Declaration{name}:name # Actual event Runtime and heterogeneous owners with a separate U32 observation # cursor/debug flag. Transition/failure declarations index the available views; # raw projection adapters remain trusted. This is not a second stream registry. type Application R & St.Stream,-transition:T.Declaration,-failure:Fail.Declaration> is Type: Application{runtime:Ev.Runtime>,owners:H,cursor:Maybe<&2,Tick.Tick>,debug:Bool} def failure_done(~S:Data,~K:Data,~V:Data,~E:Data,~H:Type,transitions:T.TransitionLog,returned:H & Fail.FailureView) -> H & Values: (owner,failures)=returned (owner,Values{transitions,failures}) def transition_done(~S:Data,~K:Data,~V:Data,~E:Data,~H:Type,~grant:Cap.ValueRead>,returned:H & T.TransitionLog) -> H & Values: (owner,transitions)=returned failure_done(~S,~K,~V,~E,~H,transitions,Cap.value_read(~H,~Fail.FailureView,~grant,owner)) def transition_grant(~S:Data,~K:Data,~V:Data,~E:Data,~H:Type,grants:Cap.ValueRead> & Cap.ValueRead>) -> Cap.ValueRead>: (transition,_)=grants transition def failure_grant(~S:Data,~K:Data,~V:Data,~E:Data,~H:Type,grants:Cap.ValueRead> & Cap.ValueRead>) -> Cap.ValueRead>: (_,failure)=grants failure def selected(~S:Data,~K:Data,~V:Data,~E:Data,~H:Type,~grants:Cap.ValueRead> & Cap.ValueRead>,args:Unit,owner:H) -> H & Values: transition_done(~S,~K,~V,~E,~H,~failure_grant(~S,~K,~V,~E,~H,grants),Cap.value_read(~H,~T.TransitionLog,~transition_grant(~S,~K,~V,~E,~H,grants),owner)) # Retain the actual runtime/owners and supplied observation cursor with debug off. # Does not register a reader, synchronize publications or advance the cursor. def bind(~S:Data,~K:Data,~V:Data,~C:Type,~R:Type,~E:Data,~H:Type,~take:R -> R & St.Stream,~transition:T.Declaration,~failure:Fail.Declaration,runtime:Ev.Runtime>,owners:H,cursor:Maybe<&2,Tick.Tick>) -> Application: Application{runtime,owners,cursor,False{}} def read_finished(~S:Data,~K:Data,~V:Data,~C:Type,~R:Type,~E:Data,~H:Type,~take:R -> R & St.Stream,~transition:T.Declaration,~failure:Fail.Declaration,owners:H,debug:Bool,returned:F.Frame & Values) -> Application & Values: match returned: case (F.Frame{runtime,cursor},values):(Application{runtime,owners,cursor,debug},values) # Read transition projection then selected relation failures through confined # inspector grants, returning actual runtime/owners and detached Values. This entry # is explicit reading regardless of debug flag; it preserves the supplied cursor # and does not advance/acknowledge event readers or change retention boundaries. # The transition resource projector must return its actual resource owner. def read_app(~S:Data,~K:Data,~V:Data,~C:Type,~R:Type,~E:Data,~H:Type,~take:R -> R & St.Stream,~transition:T.Declaration,~failure:Fail.Declaration,app:Application) -> Application & Values: match app: case Application{runtime,owners,cursor,debug}:read_finished(~S,~K,~V,~C,~R,~E,~H,~take,~transition,~failure,owners,debug,Cap.invoke(~(Owner => Cap.ValueRead> & Cap.ValueRead>),~Unit,~Values,~selected(~S,~K,~V,~E),~F.Frame,~(T.inspect(~S,~K,~V,~C,~R,~E,~take,~transition),Fail.inspect(~S,~C,~R,~E,~failure)),Unit{},F.Frame{runtime,cursor})) # Set observation enablement, retaining runtime, owners and cursor unchanged. def enabled(~S:Data,~K:Data,~V:Data,~C:Type,~R:Type,~E:Data,~H:Type,~take:R -> R & St.Stream,~transition:T.Declaration,~failure:Fail.Declaration,app:Application,flag:Bool) -> Application: match app: case Application{runtime,owners,cursor,_}:Application{runtime,owners,cursor,flag} def snapshot_read(~S:Data,~K:Data,~V:Data,~C:Type,~R:Type,~E:Data,~H:Type,~take:R -> R & St.Stream,~transition:T.Declaration,~failure:Fail.Declaration,returned:Application & Values) -> Application & Maybe<&2,Description>: (app,values)=returned (app,Some{Description{transition_name(~S,~K,~V,~R,~take,transition),values}}) # Return App and optional stream Description. Disabled returns None before either # projection. Enabled uses read_app without advancing cursor, running gameplay or # flushing commands. Failure view filters its declaration relation, not all shared # Notice payloads. See inspector-transition/failure-projection.bend. def snapshot(~S:Data,~K:Data,~V:Data,~C:Type,~R:Type,~E:Data,~H:Type,~take:R -> R & St.Stream,~transition:T.Declaration,~failure:Fail.Declaration,app:Application) -> Application & Maybe<&2,Description>: match app: case Application{runtime,owners,cursor,False{}}:(Application{runtime,owners,cursor,False{}},None{}) case Application{runtime,owners,cursor,True{}}:snapshot_read(~S,~K,~V,~C,~R,~E,~H,~take,~transition,~failure,read_app(~S,~K,~V,~C,~R,~E,~H,~take,~transition,~failure,Application{runtime,owners,cursor,True{}})) def with_runtime_finished(~S:Data,~K:Data,~V:Data,~C:Type,~R:Type,~E:Data,~H:Type,~take:R -> R & St.Stream,~transition:T.Declaration,~failure:Fail.Declaration,~O:Type,owners:H,cursor:Maybe<&2,Tick.Tick>,debug:Bool,returned:Ev.Runtime> & O) -> Application & O: (runtime,out)=returned (Application{runtime,owners,cursor,debug},out) # Advanced effect adapter threading actual Runtime and returning Out while retaining # App owners/cursor/debug. The effect chooses any synchronization/frame changes; # this wrapper supplies no transaction, implicit acknowledgement or cleanup. def with_runtime(~S:Data,~K:Data,~V:Data,~C:Type,~R:Type,~E:Data,~H:Type,~take:R -> R & St.Stream,~transition:T.Declaration,~failure:Fail.Declaration,~O:Type,effect:Ev.Runtime> -> Ev.Runtime> & O,app:Application) -> Application & O: match app: case Application{runtime,owners,cursor,debug}:with_runtime_finished(~S,~K,~V,~C,~R,~E,~H,~take,~transition,~failure,~O,owners,cursor,debug,effect(runtime))