1 import Base 2 import ./ordinary_app_relation.bend as Relation 3 import ./ordinary_app_composer.bend as Build 4 import ./ordinary_app_observation_data.bend as DTO 5 import ./ordinary_app_registration_fields.bend as Fields 6 import ./relation_failure_reader.bend as Ord 7 import ./relation_types.bend as G 8 import ./relation_reorder.bend as Reorder 9 import ./hierarchy_traversal_provider.bend as H 10 import ./hierarchy_provider.bend as Path 11 import ./hierarchy_traversal.bend as Traversal 12 import ./relation_providers.bend as P 13 import ./relation_commands.bend as A 14 import ./transaction.bend as Tx 15 import ./world.bend as W 16 17 # One hierarchy declaration supplies the installed relation and traversal authority. 18 # Trusted setup projection of a hierarchy descriptor into its relation declaration. 19 # Hierarchy authority retains the descriptor, rather than relying on its display name. 20 def declaration(~S:Data,hierarchy:G.HierarchyDescriptor) -> Ord.Declaration: 21 Ord.relation(~S,Reorder.descriptor(~S,hierarchy)) 22 # Hierarchy App with the relation storage/notice wrappers and installed observer. 23 # Its World and registration owners are affine and returned by the operations below. 24 def Application(~S:Data,~C:Type,~R:Type,~E:Data,~hierarchy:G.HierarchyDescriptor,~Owners:Type,~Rows:Data,~prior:Relation.World(~S,~C,~R,~E) -> Relation.World(~S,~C,~R,~E) & DTO.Observation,~fold:Owners -> Owners & List<&2,Fields.Fields>) -> Type: 25 Relation.Application(~S,~C,~R,~E,~declaration(~S,hierarchy),~Owners,~Rows,~prior,~fold) 26 # Install hierarchy observation on the existing App, preserving World and owners. 27 # Does not create parent edges. See docs/parity/hierarchy.md for accepted App and 28 # traversal scenarios; relate_app followed by barrier_app establishes visible edges. 29 def install(~S:Data,~C:Type,~R:Type,~E:Data,~hierarchy:G.HierarchyDescriptor,~Owners:Type,~Rows:Data,~prior:Relation.World(~S,~C,~R,~E) -> Relation.World(~S,~C,~R,~E) & DTO.Observation,~fold:Owners -> Owners & List<&2,Fields.Fields>,app:Build.Registered) -> Application(~S,~C,~R,~E,~hierarchy,~Owners,~Rows,~prior,~fold): 30 Relation.install(~S,~C,~R,~E,~declaration(~S,hierarchy),~Owners,~Rows,~prior,~fold,app) 31 def returned(~S:Data,~C:Type,~R:Type,~E:Data,~hierarchy:G.HierarchyDescriptor,~Owners:Type,~Rows:Data,~prior:Relation.World(~S,~C,~R,~E) -> Relation.World(~S,~C,~R,~E) & DTO.Observation,~fold:Owners -> Owners & List<&2,Fields.Fields>,~Value:Data,owners:Owners,recipe:Relation.Recipe(~S,~C,~R,~E,~declaration(~S,hierarchy),~Owners,~Rows,~prior,~fold),debug:Bool,result:Relation.World(~S,~C,~R,~E) & Value) -> Application(~S,~C,~R,~E,~hierarchy,~Owners,~Rows,~prior,~fold) & Value: 32 match result: 33 case (world,value):(Build.Registered{world,owners,recipe,debug},value) 34 # Return the App and root of a valid target, including the target itself when it 35 # has no parent. Invalid targets or incomplete ancestor traversal return None. 36 # Reads current edges without applying queued changes. 37 def root_app(~S:Data,~C:Type,~R:Type,~E:Data,~hierarchy:G.HierarchyDescriptor,~Owners:Type,~Rows:Data,~prior:Relation.World(~S,~C,~R,~E) -> Relation.World(~S,~C,~R,~E) & DTO.Observation,~fold:Owners -> Owners & List<&2,Fields.Fields>,app:Application(~S,~C,~R,~E,~hierarchy,~Owners,~Rows,~prior,~fold),target:W.Handle) -> Application(~S,~C,~R,~E,~hierarchy,~Owners,~Rows,~prior,~fold) & Maybe<&2,W.Handle>: 38 match app: 39 case Build.Registered{world,owners,recipe,debug}:returned(~S,~C,~R,~E,~hierarchy,~Owners,~Rows,~prior,~fold,~Maybe<&2,W.Handle>,owners,recipe,debug,H.lookup_root(~S,~C,~R,~E,~hierarchy,world,target)) 40 # Return the App and raw ancestor path, nearest parent first, excluding target. 41 # None means invalid target; Some may be Complete or an Incomplete diagnostic. 42 # The seed is validated; this raw path is not a query that revalidates every member. 43 def ancestors_app(~S:Data,~C:Type,~R:Type,~E:Data,~hierarchy:G.HierarchyDescriptor,~Owners:Type,~Rows:Data,~prior:Relation.World(~S,~C,~R,~E) -> Relation.World(~S,~C,~R,~E) & DTO.Observation,~fold:Owners -> Owners & List<&2,Fields.Fields>,app:Application(~S,~C,~R,~E,~hierarchy,~Owners,~Rows,~prior,~fold),target:W.Handle) -> Application(~S,~C,~R,~E,~hierarchy,~Owners,~Rows,~prior,~fold) & Maybe<&2,Path.Path>: 44 match app: 45 case Build.Registered{world,owners,recipe,debug}:returned(~S,~C,~R,~E,~hierarchy,~Owners,~Rows,~prior,~fold,~Maybe<&2,Path.Path>,owners,recipe,debug,H.lookup_ancestors(~S,~C,~R,~E,~hierarchy,world,target)) 46 # Return the App and raw descendants, excluding target, in requested depth-first 47 # or breadth-first order using stored child order. None means invalid target. 48 # Some Incomplete preserves visited/pending diagnostics; forged cyclic graphs are 49 # not promised complete traversal. Queued reparenting needs a barrier first. 50 def descendants_app(~S:Data,~C:Type,~R:Type,~E:Data,~hierarchy:G.HierarchyDescriptor,~Owners:Type,~Rows:Data,~prior:Relation.World(~S,~C,~R,~E) -> Relation.World(~S,~C,~R,~E) & DTO.Observation,~fold:Owners -> Owners & List<&2,Fields.Fields>,app:Application(~S,~C,~R,~E,~hierarchy,~Owners,~Rows,~prior,~fold),target:W.Handle,order:Traversal.Order) -> Application(~S,~C,~R,~E,~hierarchy,~Owners,~Rows,~prior,~fold) & Maybe<&2,Path.Path>: 51 match app: 52 case Build.Registered{world,owners,recipe,debug}:returned(~S,~C,~R,~E,~hierarchy,~Owners,~Rows,~prior,~fold,~Maybe<&2,Path.Path>,owners,recipe,debug,H.lookup_descendants(~S,~C,~R,~E,~hierarchy,world,H.Descendants{target,order})) 53 # Stage a child (source) to parent (target) edge, returning App, queue status and 54 # transaction outcome separately. Applied hierarchy edges enforce one parent and 55 # reject self/cycle violations; barrier-time rejection appears as relation notices. 56 def relate_app(~S:Data,~C:Type,~R:Type,~E:Data,~hierarchy:G.HierarchyDescriptor,~Owners:Type,~Rows:Data,~prior:Relation.World(~S,~C,~R,~E) -> Relation.World(~S,~C,~R,~E) & DTO.Observation,~fold:Owners -> Owners & List<&2,Fields.Fields>,~Error:Data,app:Application(~S,~C,~R,~E,~hierarchy,~Owners,~Rows,~prior,~fold),targets:P.Targets,outcome:Tx.Outcome) -> Application(~S,~C,~R,~E,~hierarchy,~Owners,~Rows,~prior,~fold) & DTO.Product>: 57 Relation.relate_app(~S,~C,~R,~E,~declaration(~S,hierarchy),~Owners,~Rows,~prior,~fold,~Error,app,targets,outcome) 58 # Apply all pending World structural commands and return the hierarchy App. 59 # Use before traversal or observation when queued graph changes must be visible. 60 def barrier_app(~S:Data,~C:Type,~R:Type,~E:Data,~hierarchy:G.HierarchyDescriptor,~Owners:Type,~Rows:Data,~prior:Relation.World(~S,~C,~R,~E) -> Relation.World(~S,~C,~R,~E) & DTO.Observation,~fold:Owners -> Owners & List<&2,Fields.Fields>,app:Application(~S,~C,~R,~E,~hierarchy,~Owners,~Rows,~prior,~fold)) -> Application(~S,~C,~R,~E,~hierarchy,~Owners,~Rows,~prior,~fold): 61 Relation.barrier_app(~S,~C,~R,~E,~declaration(~S,hierarchy),~Owners,~Rows,~prior,~fold,app) 62 # Return the hierarchy App and optional combined snapshot under flag enablement. 63 # Observation does not itself apply pending graph commands. 64 def snapshot_app(~S:Data,~C:Type,~R:Type,~E:Data,~hierarchy:G.HierarchyDescriptor,~Owners:Type,~Rows:Data,~prior:Relation.World(~S,~C,~R,~E) -> Relation.World(~S,~C,~R,~E) & DTO.Observation,~fold:Owners -> Owners & List<&2,Fields.Fields>,app:Application(~S,~C,~R,~E,~hierarchy,~Owners,~Rows,~prior,~fold),flag:Bool) -> Application(~S,~C,~R,~E,~hierarchy,~Owners,~Rows,~prior,~fold) & Maybe<&2,DTO.Product>,DTO.Observation>>: 65 Relation.snapshot_app(~S,~C,~R,~E,~declaration(~S,hierarchy),~Owners,~Rows,~prior,~fold,app,flag) 66 67 # Stage an exact permutation of a parent's complete current child set, then finish 68 # with the supplied transaction outcome. Queue admission is not application success. 69 # Missing, duplicate, unrelated or mismatched child sets are rejected at application 70 # without changing edges, with a failure notice; apply through barrier_app. 71 def reorder_app(~S:Data,~C:Type,~R:Type,~E:Data,~hierarchy:G.HierarchyDescriptor,~Owners:Type,~Rows:Data,~prior:Relation.World(~S,~C,~R,~E) -> Relation.World(~S,~C,~R,~E) & DTO.Observation,~fold:Owners -> Owners & List<&2,Fields.Fields>,~Error:Data,app:Application(~S,~C,~R,~E,~hierarchy,~Owners,~Rows,~prior,~fold),args:Reorder.Args,outcome:Tx.Outcome) -> Application(~S,~C,~R,~E,~hierarchy,~Owners,~Rows,~prior,~fold) & DTO.Product>: 72 match app: 73 case Build.Registered{world,owners,recipe,debug}:Relation.queued(~S,~C,~R,~E,~declaration(~S,hierarchy),~Relation.builtin_row(~S),~Owners,~Rows,~prior,~fold,~Error,owners,recipe,debug,outcome,Reorder.invoke(~S,~C,~R,~E,~hierarchy,~A.QueueStatus,~Reorder.body(~S),args,Tx.begin(~Relation.World(~S,~C,~R,~E),~A.Notice,world)))