import Base import entity_key_render.bend as KeyText import ./ordinary_app_composer.bend as Build import ./ordinary_app_recipe.bend as Tree import ./ordinary_app_observation_data.bend as DTO import ./ordinary_app_registration_fields.bend as Fields import ./world.bend as W import ./inspector.bend as I import ./relation_failure_reader.bend as Ord import ./relation_types.bend as G import ./relation_commands.bend as A import ./relation_query.bend as RQ import ./relation_providers.bend as P import ./inspector_relation_projection.bend as IR import ./inspector_query.bend as IQ import ./inspector_query_projection.bend as Select import ./transaction.bend as T import ./commands.bend as Cmd # One trusted ordinary relation declaration drives operational grants and view. # Trusted setup projection of the retained relation declaration. The descriptor # selects operational authority; presentation strings do not create another grant. def descriptor(~S:Data,declaration:Ord.Declaration) -> G.Descriptor: match declaration: case Ord.Declaration{value}:value def kind(value:G.RelationKind) -> String: match value: case G.Ordinary{}:"Ordinary" case G.Hierarchy{}:"Hierarchy" def descriptor_text(~S:Data,value:G.Descriptor) -> String: match value: case G.Descriptor{key,name,inverseName,tag}:"Relation{key=" ++ U32.show(key) ++ "|name=" ++ name ++ "|inverse=" ++ inverseName ++ "|kind=" ++ kind(tag) ++ "}" def declared_spec(~S:Data,+value:G.Descriptor) -> RQ.Spec: match value: case G.Descriptor{+key,+name,+inverseName,+tag}:RQ.Spec{[RQ.optional_relation(~S,name,G.Descriptor{key,name,inverseName,tag}),RQ.optional_related(~S,inverseName,G.Descriptor{key,name,inverseName,tag})]} # Inspector setup for optional outgoing and inverse cells of this declaration. # Optional cells retain unlinked entities rather than requiring an existing edge. def spec(~S:Data,declaration:Ord.Declaration) -> RQ.Spec: match declaration: case Ord.Declaration{+value}:declared_spec(~S,value) # Advanced storage projection: relation edges wrap C and relation notices wrap E. # The World remains an affine owner threaded through every App operation. def World(~S:Data,~C:Type,~R:Type,~E:Data) -> Type: W.World,R,A.Notice> # Detached inspector row containing an entity handle and optional relation cells. def Row(~S:Data) -> Data: IQ.ProjectedRow>>> # Detached relation observation: the declared descriptor and ordered inspector rows. # This Data value does not grant access to the underlying World or inverse storage. type View is Data: View{descriptor:G.Descriptor,rows:List<&2,Row(~S)>} # Advanced observation shape pairing prior rows with the added relation view. def ObservedRows(~S:Data,~Rows:Data) -> Data: DTO.Product> def handle(~S:Data,value:W.Handle) -> String: match value: case W.Handle{ns,id}:U32.show(ns) ++ ":" ++ KeyText.show(id) def relation_value(~S:Data,value:RQ.Value) -> String: match value: case RQ.Missing{}:"Missing" case RQ.Target{target}:"Target:" ++ handle(~S,target) case RQ.Sources{targets}:"Sources:" ++ List.show(&2,W.Handle,handle(~S),targets) def end(value:RQ.End) -> String: match value: case RQ.Outgoing{}:"Outgoing" case RQ.Incoming{}:"Incoming" def cell(~S:Data,value:RQ.Cell) -> String: match value: case RQ.Cell{key,direction,value}:key ++ ":" ++ end(direction) ++ ":" ++ relation_value(~S,value) # Default textual presentation of a detached row; it does not perform a query. def builtin_row(~S:Data,value:Row(~S)) -> String: match value: case IQ.ProjectedRow{entity,Select.Product{_,cells}}:handle(~S,entity) ++ ":" ++ List.show(&2,RQ.Cell,cell(~S),cells) def rendered(~S:Data,~render:Row(~S) -> String,rows:List<&2,Row(~S)>) -> List<&2,String>: match rows: case []:[] case first <> rest:render(first) <> rendered(~S,~render,rest) def presented(~S:Data,~C:Type,~R:Type,~E:Data,~declaration:Ord.Declaration,~render:Row(~S) -> String,result:I.Frame,R,A.Notice> & List<&2,Row(~S)>) -> World(~S,~C,~R,~E) & DTO.Observation>: match result: case (I.Frame{world,_},+rows):(world,DTO.Observation{View{descriptor(~S,declaration),rows},descriptor_text(~S,descriptor(~S,declaration)) <> rendered(~S,~render,rows)}) # Read a fresh relation inspector view while returning the World owner. # Uses a fresh inspector cursor; it does not advance a registered gameplay reader # or apply queued commands. The renderer consumes detached rows only. def observe(~S:Data,~C:Type,~R:Type,~E:Data,~declaration:Ord.Declaration,~render:Row(~S) -> String,world:World(~S,~C,~R,~E)) -> World(~S,~C,~R,~E) & DTO.Observation>: presented(~S,~C,~R,~E,~declaration,~render,IQ.each_composed(~I.Frame,R,A.Notice>,~S,~Select.Product>>,~IR.grant(~S,~C,~R,~E,~Unit,~Select.empty(~I.Frame,R,A.Notice>,~S),~spec(~S,declaration)),I.Frame{world,None{}})) # Advanced recipe adapter: retain the prior observation, then append this view. # The shared World owner is passed through both observers in that order. def observation(~S:Data,~C:Type,~R:Type,~E:Data,~declaration:Ord.Declaration,~render:Row(~S) -> String,~Owners:Type,~Rows:Data,~prior:World(~S,~C,~R,~E) -> World(~S,~C,~R,~E) & DTO.Observation,~fold:Owners -> Owners & List<&2,Fields.Fields>) -> World(~S,~C,~R,~E) -> World(~S,~C,~R,~E) & DTO.Observation: Tree.observe_pair(~World(~S,~C,~R,~E),~Rows,~View,~prior,~observe(~S,~C,~R,~E,~declaration,~render)) # Advanced App type with a caller-supplied detached-row renderer. World, # registration owners and the combined observation recipe remain inside the App. def presented_Application(~S:Data,~C:Type,~R:Type,~E:Data,~declaration:Ord.Declaration,~render:Row(~S) -> String,~Owners:Type,~Rows:Data,~prior:World(~S,~C,~R,~E) -> World(~S,~C,~R,~E) & DTO.Observation,~fold:Owners -> Owners & List<&2,Fields.Fields>) -> Type: Build.Registered,~prior,~observe(~S,~C,~R,~E,~declaration,~render)),fold> # Advanced recipe type for prior rows followed by the custom relation view. def presented_Recipe(~S:Data,~C:Type,~R:Type,~E:Data,~declaration:Ord.Declaration,~render:Row(~S) -> String,~Owners:Type,~Rows:Data,~prior:World(~S,~C,~R,~E) -> World(~S,~C,~R,~E) & DTO.Observation,~fold:Owners -> Owners & List<&2,Fields.Fields>) -> Type: Tree.Recipe,~prior,~observe(~S,~C,~R,~E,~declaration,~render))> # Install the custom relation observation recipe, preserving World, registration # owners and debug state. This does not create edges or register a gameplay system. def presented_install(~S:Data,~C:Type,~R:Type,~E:Data,~declaration:Ord.Declaration,~render:Row(~S) -> String,~Owners:Type,~Rows:Data,~prior:World(~S,~C,~R,~E) -> World(~S,~C,~R,~E) & DTO.Observation,~fold:Owners -> Owners & List<&2,Fields.Fields>,app:Build.Registered) -> presented_Application(~S,~C,~R,~E,~declaration,~render,~Owners,~Rows,~prior,~fold): match app: case Build.Registered{world,owners,_,debug}:Build.Registered{world,owners,Tree.Recipe{},debug} # Set observation enablement from flag, then snapshot through the installed recipe. # Returns the App in both cases; disabled observation returns None. def presented_snapshot_app(~S:Data,~C:Type,~R:Type,~E:Data,~declaration:Ord.Declaration,~render:Row(~S) -> String,~Owners:Type,~Rows:Data,~prior:World(~S,~C,~R,~E) -> World(~S,~C,~R,~E) & DTO.Observation,~fold:Owners -> Owners & List<&2,Fields.Fields>,app:presented_Application(~S,~C,~R,~E,~declaration,~render,~Owners,~Rows,~prior,~fold),flag:Bool) -> presented_Application(~S,~C,~R,~E,~declaration,~render,~Owners,~Rows,~prior,~fold) & Maybe<&2,DTO.Product>,DTO.Observation>>: Build.snapshot(~S,~World(~S,~C,~R,~E),~Owners,~ObservedRows(~S,~Rows),~Tree.observe_pair(~World(~S,~C,~R,~E),~Rows,~View,~prior,~observe(~S,~C,~R,~E,~declaration,~render)),~fold,Build.enabled(~S,~World(~S,~C,~R,~E),~Owners,~ObservedRows(~S,~Rows),~Tree.observe_pair(~World(~S,~C,~R,~E),~Rows,~View,~prior,~observe(~S,~C,~R,~E,~declaration,~render)),~fold,app,flag)) def returned(~S:Data,~C:Type,~R:Type,~E:Data,~declaration:Ord.Declaration,~render:Row(~S) -> String,~Owners:Type,~Rows:Data,~prior:World(~S,~C,~R,~E) -> World(~S,~C,~R,~E) & DTO.Observation,~fold:Owners -> Owners & List<&2,Fields.Fields>,~Error:Data,owners:Owners,recipe:presented_Recipe(~S,~C,~R,~E,~declaration,~render,~Owners,~Rows,~prior,~fold),debug:Bool,result:T.Returned) -> presented_Application(~S,~C,~R,~E,~declaration,~render,~Owners,~Rows,~prior,~fold) & T.Outcome: match result: case T.Returned{world,status}:(Build.Registered{world,owners,recipe,debug},status) def queue_finished(~S:Data,~C:Type,~R:Type,~E:Data,~declaration:Ord.Declaration,~render:Row(~S) -> String,~Owners:Type,~Rows:Data,~prior:World(~S,~C,~R,~E) -> World(~S,~C,~R,~E) & DTO.Observation,~fold:Owners -> Owners & List<&2,Fields.Fields>,~Error:Data,status:A.QueueStatus,result:presented_Application(~S,~C,~R,~E,~declaration,~render,~Owners,~Rows,~prior,~fold) & T.Outcome) -> presented_Application(~S,~C,~R,~E,~declaration,~render,~Owners,~Rows,~prior,~fold) & DTO.Product>: match result: case (app,outcome):(app,DTO.Product{status,outcome}) def queued(~S:Data,~C:Type,~R:Type,~E:Data,~declaration:Ord.Declaration,~render:Row(~S) -> String,~Owners:Type,~Rows:Data,~prior:World(~S,~C,~R,~E) -> World(~S,~C,~R,~E) & DTO.Observation,~fold:Owners -> Owners & List<&2,Fields.Fields>,~Error:Data,owners:Owners,recipe:presented_Recipe(~S,~C,~R,~E,~declaration,~render,~Owners,~Rows,~prior,~fold),debug:Bool,outcome:T.Outcome,result:T.Tx> & A.QueueStatus) -> presented_Application(~S,~C,~R,~E,~declaration,~render,~Owners,~Rows,~prior,~fold) & DTO.Product>: match result: case (frame,+status):queue_finished(~S,~C,~R,~E,~declaration,~render,~Owners,~Rows,~prior,~fold,~Error,status,returned(~S,~C,~R,~E,~declaration,~render,~Owners,~Rows,~prior,~fold,~Error,owners,recipe,debug,Cmd.tx_finish(~S,~A.Store,~R,~A.Notice,~Error,frame,outcome))) # Stage source-to-target replacement under this declaration, then finish the # transaction with the supplied outcome. Return the App, queue status and outcome # separately: Queued is not barrier application success. Failure discards staged # commands; barrier-time structural rejection is reported through relation notices. def presented_relate_app(~S:Data,~C:Type,~R:Type,~E:Data,~declaration:Ord.Declaration,~render:Row(~S) -> String,~Owners:Type,~Rows:Data,~prior:World(~S,~C,~R,~E) -> World(~S,~C,~R,~E) & DTO.Observation,~fold:Owners -> Owners & List<&2,Fields.Fields>,~Error:Data,app:presented_Application(~S,~C,~R,~E,~declaration,~render,~Owners,~Rows,~prior,~fold),targets:P.Targets,outcome:T.Outcome) -> presented_Application(~S,~C,~R,~E,~declaration,~render,~Owners,~Rows,~prior,~fold) & DTO.Product>: match app: case Build.Registered{world,owners,recipe,debug}:queued(~S,~C,~R,~E,~declaration,~render,~Owners,~Rows,~prior,~fold,~Error,owners,recipe,debug,outcome,P.relate(~T.Tx>,~S,~P.provide_write(~S,~C,~R,~E,~descriptor(~S,declaration)),T.begin(~World(~S,~C,~R,~E),~A.Notice,world),targets)) # Stage removal of this source relation and finish with the supplied outcome. # Returns App plus queue status and transaction outcome; successful staging remains # invisible to graph reads until a barrier, and transaction failure discards it. def presented_unrelate_app(~S:Data,~C:Type,~R:Type,~E:Data,~declaration:Ord.Declaration,~render:Row(~S) -> String,~Owners:Type,~Rows:Data,~prior:World(~S,~C,~R,~E) -> World(~S,~C,~R,~E) & DTO.Observation,~fold:Owners -> Owners & List<&2,Fields.Fields>,~Error:Data,app:presented_Application(~S,~C,~R,~E,~declaration,~render,~Owners,~Rows,~prior,~fold),source:W.Handle,outcome:T.Outcome) -> presented_Application(~S,~C,~R,~E,~declaration,~render,~Owners,~Rows,~prior,~fold) & DTO.Product>: match app: case Build.Registered{world,owners,recipe,debug}:queued(~S,~C,~R,~E,~declaration,~render,~Owners,~Rows,~prior,~fold,~Error,owners,recipe,debug,outcome,P.unrelate(~T.Tx>,~S,~P.provide_write(~S,~C,~R,~E,~descriptor(~S,declaration)),T.begin(~World(~S,~C,~R,~E),~A.Notice,world),source)) def inverse_finished(~S:Data,~C:Type,~R:Type,~E:Data,~declaration:Ord.Declaration,~render:Row(~S) -> String,~Owners:Type,~Rows:Data,~prior:World(~S,~C,~R,~E) -> World(~S,~C,~R,~E) & DTO.Observation,~fold:Owners -> Owners & List<&2,Fields.Fields>,value:Maybe<&2,List<&2,W.Handle>>,result:presented_Application(~S,~C,~R,~E,~declaration,~render,~Owners,~Rows,~prior,~fold) & T.Outcome) -> presented_Application(~S,~C,~R,~E,~declaration,~render,~Owners,~Rows,~prior,~fold) & Maybe<&2,List<&2,W.Handle>>: match result: case (app,_):(app,value) def inverse_taken(~S:Data,~C:Type,~R:Type,~E:Data,~declaration:Ord.Declaration,~render:Row(~S) -> String,~Owners:Type,~Rows:Data,~prior:World(~S,~C,~R,~E) -> World(~S,~C,~R,~E) & DTO.Observation,~fold:Owners -> Owners & List<&2,Fields.Fields>,owners:Owners,recipe:presented_Recipe(~S,~C,~R,~E,~declaration,~render,~Owners,~Rows,~prior,~fold),debug:Bool,result:T.Tx> & Maybe<&2,List<&2,W.Handle>>) -> presented_Application(~S,~C,~R,~E,~declaration,~render,~Owners,~Rows,~prior,~fold) & Maybe<&2,List<&2,W.Handle>>: match result: case (frame,value):inverse_finished(~S,~C,~R,~E,~declaration,~render,~Owners,~Rows,~prior,~fold,value,returned(~S,~C,~R,~E,~declaration,~render,~Owners,~Rows,~prior,~fold,~Unit,owners,recipe,debug,Cmd.tx_finish(~S,~A.Store,~R,~A.Notice,~Unit,frame,T.Success{}))) # Read the current inverse list in stored order and return the App owner. None # denotes an invalid target; Some may contain an empty list. Pending commands are # not applied, and this read does not offer direct inverse-list mutation. def presented_inverse_app(~S:Data,~C:Type,~R:Type,~E:Data,~declaration:Ord.Declaration,~render:Row(~S) -> String,~Owners:Type,~Rows:Data,~prior:World(~S,~C,~R,~E) -> World(~S,~C,~R,~E) & DTO.Observation,~fold:Owners -> Owners & List<&2,Fields.Fields>,app:presented_Application(~S,~C,~R,~E,~declaration,~render,~Owners,~Rows,~prior,~fold),target:W.Handle) -> presented_Application(~S,~C,~R,~E,~declaration,~render,~Owners,~Rows,~prior,~fold) & Maybe<&2,List<&2,W.Handle>>: match app: case Build.Registered{world,owners,recipe,debug}:inverse_taken(~S,~C,~R,~E,~declaration,~render,~Owners,~Rows,~prior,~fold,owners,recipe,debug,P.read(~T.Tx>,~S,~P.provide_read(~S,~C,~R,~E,~descriptor(~S,declaration)),T.begin(~World(~S,~C,~R,~E),~A.Notice,world),target)) # Apply the World structural barrier, returning the App with its owners and recipe. # This drains pending World commands, not only commands for this declaration; # structural relation failures are published as notices by their command handlers. def presented_barrier_app(~S:Data,~C:Type,~R:Type,~E:Data,~declaration:Ord.Declaration,~render:Row(~S) -> String,~Owners:Type,~Rows:Data,~prior:World(~S,~C,~R,~E) -> World(~S,~C,~R,~E) & DTO.Observation,~fold:Owners -> Owners & List<&2,Fields.Fields>,app:presented_Application(~S,~C,~R,~E,~declaration,~render,~Owners,~Rows,~prior,~fold)) -> presented_Application(~S,~C,~R,~E,~declaration,~render,~Owners,~Rows,~prior,~fold): match app: case Build.Registered{world,owners,recipe,debug}:Build.Registered{W.barrier(~S,~A.Store,~R,~A.Notice,world),owners,recipe,debug} # Ordinary relation App using builtin_row presentation and the retained declaration. def Application(~S:Data,~C:Type,~R:Type,~E:Data,~declaration:Ord.Declaration,~Owners:Type,~Rows:Data,~prior:World(~S,~C,~R,~E) -> World(~S,~C,~R,~E) & DTO.Observation,~fold:Owners -> Owners & List<&2,Fields.Fields>) -> Type: presented_Application(~S,~C,~R,~E,~declaration,~builtin_row(~S),~Owners,~Rows,~prior,~fold) # Advanced default-renderer recipe projection for composing typed App adapters. def Recipe(~S:Data,~C:Type,~R:Type,~E:Data,~declaration:Ord.Declaration,~Owners:Type,~Rows:Data,~prior:World(~S,~C,~R,~E) -> World(~S,~C,~R,~E) & DTO.Observation,~fold:Owners -> Owners & List<&2,Fields.Fields>) -> Type: presented_Recipe(~S,~C,~R,~E,~declaration,~builtin_row(~S),~Owners,~Rows,~prior,~fold) # Install the default relation observer without changing graph edges or owners. # Typical order: install, relate_app with a successful outcome, barrier_app, then # inverse_app or snapshot_app. See docs/parity/relations.md for accepted scenarios. def install(~S:Data,~C:Type,~R:Type,~E:Data,~declaration:Ord.Declaration,~Owners:Type,~Rows:Data,~prior:World(~S,~C,~R,~E) -> World(~S,~C,~R,~E) & DTO.Observation,~fold:Owners -> Owners & List<&2,Fields.Fields>,app:Build.Registered) -> Application(~S,~C,~R,~E,~declaration,~Owners,~Rows,~prior,~fold): presented_install(~S,~C,~R,~E,~declaration,~builtin_row(~S),~Owners,~Rows,~prior,~fold,app) # Default-renderer snapshot; flag controls observation and the App is returned # even when the observation is disabled (None). Does not apply pending commands. def snapshot_app(~S:Data,~C:Type,~R:Type,~E:Data,~declaration:Ord.Declaration,~Owners:Type,~Rows:Data,~prior:World(~S,~C,~R,~E) -> World(~S,~C,~R,~E) & DTO.Observation,~fold:Owners -> Owners & List<&2,Fields.Fields>,app:Application(~S,~C,~R,~E,~declaration,~Owners,~Rows,~prior,~fold),flag:Bool) -> Application(~S,~C,~R,~E,~declaration,~Owners,~Rows,~prior,~fold) & Maybe<&2,DTO.Product>,DTO.Observation>>: presented_snapshot_app(~S,~C,~R,~E,~declaration,~builtin_row(~S),~Owners,~Rows,~prior,~fold,app,flag) # Default-renderer source-to-target request. Returns queue status separately from # the supplied transaction outcome; call barrier_app before expecting graph changes. def relate_app(~S:Data,~C:Type,~R:Type,~E:Data,~declaration:Ord.Declaration,~Owners:Type,~Rows:Data,~prior:World(~S,~C,~R,~E) -> World(~S,~C,~R,~E) & DTO.Observation,~fold:Owners -> Owners & List<&2,Fields.Fields>,~Error:Data,app:Application(~S,~C,~R,~E,~declaration,~Owners,~Rows,~prior,~fold),targets:P.Targets,outcome:T.Outcome) -> Application(~S,~C,~R,~E,~declaration,~Owners,~Rows,~prior,~fold) & DTO.Product>: presented_relate_app(~S,~C,~R,~E,~declaration,~builtin_row(~S),~Owners,~Rows,~prior,~fold,~Error,app,targets,outcome) # Default-renderer source removal request. The App is returned on refusal or # transaction failure; staging and barrier-time relation validity remain distinct. def unrelate_app(~S:Data,~C:Type,~R:Type,~E:Data,~declaration:Ord.Declaration,~Owners:Type,~Rows:Data,~prior:World(~S,~C,~R,~E) -> World(~S,~C,~R,~E) & DTO.Observation,~fold:Owners -> Owners & List<&2,Fields.Fields>,~Error:Data,app:Application(~S,~C,~R,~E,~declaration,~Owners,~Rows,~prior,~fold),source:W.Handle,outcome:T.Outcome) -> Application(~S,~C,~R,~E,~declaration,~Owners,~Rows,~prior,~fold) & DTO.Product>: presented_unrelate_app(~S,~C,~R,~E,~declaration,~builtin_row(~S),~Owners,~Rows,~prior,~fold,~Error,app,source,outcome) # Default-renderer inverse read, returning the App and Maybe ordered source handles. # Reads the current graph; pending additions and removals require a barrier first. def inverse_app(~S:Data,~C:Type,~R:Type,~E:Data,~declaration:Ord.Declaration,~Owners:Type,~Rows:Data,~prior:World(~S,~C,~R,~E) -> World(~S,~C,~R,~E) & DTO.Observation,~fold:Owners -> Owners & List<&2,Fields.Fields>,app:Application(~S,~C,~R,~E,~declaration,~Owners,~Rows,~prior,~fold),target:W.Handle) -> Application(~S,~C,~R,~E,~declaration,~Owners,~Rows,~prior,~fold) & Maybe<&2,List<&2,W.Handle>>: presented_inverse_app(~S,~C,~R,~E,~declaration,~builtin_row(~S),~Owners,~Rows,~prior,~fold,app,target) # Default-renderer structural barrier for all pending commands in this World. def barrier_app(~S:Data,~C:Type,~R:Type,~E:Data,~declaration:Ord.Declaration,~Owners:Type,~Rows:Data,~prior:World(~S,~C,~R,~E) -> World(~S,~C,~R,~E) & DTO.Observation,~fold:Owners -> Owners & List<&2,Fields.Fields>,app:Application(~S,~C,~R,~E,~declaration,~Owners,~Rows,~prior,~fold)) -> Application(~S,~C,~R,~E,~declaration,~Owners,~Rows,~prior,~fold): presented_barrier_app(~S,~C,~R,~E,~declaration,~builtin_row(~S),~Owners,~Rows,~prior,~fold,app)