import Base import ./world.bend as W import ./system.bend as Sys import ./event_runtime.bend as Ev import ./event_stream.bend as Stream import ./event_family_routing.bend as Route import ./ordinary_event_declaration.bend as Event import ./ordinary_app_event.bend as OrdinaryEvent import ./ordinary_app_registration_fields.bend as Records import ./ordinary_query_declaration.bend as Query import ./ordinary_app_observation_data.bend as DTO import ./ordinary_app_composer.bend as Build import ./ordinary_app_schema.bend as SchemaApp import ./ordinary_app_event_family_runtime.bend as App import ./ordinary_app_records.bend as Leaf import ./ordinary_app_recipe.bend as Tree import ./ordinary_schema.bend as Schema # One retained operational declaration and the actual returned reader/Registry. # Debug folds only registration fields and never reads or advances the Reader. # Registered App reader for one typed Data event stream E in family F. # Retain the actual declaration, reader and Registry returned after each run; # registration metadata alone cannot replace their runtime authority. Different # readers of the same stream keep independent positions. F may also contain # affine owners, but this reader's event payload E is Data, not an owned Type log. type Owner is Type: Owner{declaration:Event.Declaration,reader:Reader,registry:Registry} # Advanced App metadata projection: return this owner with its registration # fields and event-read clause. The reported Registry cursor is not a read of # the event stream's position. Debug collection does not deliver, advance or # trim events; the actual reader and Registry are returned inside the owner. def fields(~S:Data,~C:Type,~R:Type,~P:Data,~E:Data,~Reader:Type,~A:Type,~Error:Data,~Out:Type,~runner:W.World -> Ev.Input -> Sys.Outcome,Error,Out>,owner:Owner,Error,Out,runner>>) -> Owner,Error,Out,runner>> & List<&2,Records.Fields>: match owner: case Owner{Event.Declaration{Event.Domain{+key},+mode},reader,Sys.Registry{+namespace,+id,+name,+access,+cursor}}:(Owner{Event.Declaration{Event.Domain{key},mode},reader,Sys.Registry{namespace,id,name,access,cursor}},[Records.Fields{namespace,id,name,access,cursor,key,[Query.Clause{key,OrdinaryEvent.query_mode(mode)}],Records.EventKind{}}]) # Advanced App observer returning the same World with an empty Unit observation. # Event inventory comes from registration metadata; this function does not read # payloads or advance a stream cursor merely to display the App. def observe(~S:Data,~C:Type,~R:Type,~P:Data,world:W.World) -> W.World & DTO.Observation:(world,DTO.Observation{Unit{},[]}) # Reader registration result. Registered returns the runtime plus its actual # reader/Registry owner; RegistrationFailed returns the runtime without a new # owner. Thread the returned runtime on either branch instead of retaining an # old World or copying the family owners. type Registration F & Stream.Stream,-put:F -> Stream.Stream -> F,-runner:W.World -> Ev.Input -> Sys.Outcome,Error,Out>> is Type: Registered{runtime:Route.Runtime,owner:Owner,Sys.Registry,Error,Out,runner>>} RegistrationFailed{runtime:Route.Runtime} def registered(~S:Data,~C:Type,~R:Type,~P:Data,~E:Data,~F:Type,~A:Type,~Error:Data,~Out:Type,~take:F -> F & Stream.Stream,~put:F -> Stream.Stream -> F,~runner:W.World -> Ev.Input -> Sys.Outcome,Error,Out>,declaration:Event.Declaration,result:Route.Registration) -> Registration: match result: case Route.RegistrationFailed{runtime}:RegistrationFailed{runtime} case Route.Registered{runtime,reader,registry}:Registered{runtime,Owner{declaration,reader,registry}} def declared(~S:Data,~C:Type,~R:Type,~P:Data,~E:Data,~F:Type,~A:Type,~Error:Data,~Out:Type,~take:F -> F & Stream.Stream,~put:F -> Stream.Stream -> F,~runner:W.World -> Ev.Input -> Sys.Outcome,Error,Out>,family:F,stream:Stream.Stream,tick:Nat,name:String,+declaration:Event.Declaration,named:W.World & U32) -> Registration: registered(~S,~C,~R,~P,~E,~F,~A,~Error,~Out,~take,~put,~runner,declaration,Route.registration_named(~S,~C,~R,~P,~E,~F,~A,~Error,~Out,~take,~put,~runner,family,stream,tick,declaration,name,named)) def taken(~S:Data,~C:Type,~R:Type,~P:Data,~E:Data,~F:Type,~A:Type,~Error:Data,~Out:Type,~take:F -> F & Stream.Stream,~put:F -> Stream.Stream -> F,~runner:W.World -> Ev.Input -> Sys.Outcome,Error,Out>,world:W.World,tick:Nat,name:String,returned:F & Stream.Stream) -> Registration: match returned: case (family,+stream):declared(~S,~C,~R,~P,~E,~F,~A,~Error,~Out,~take,~put,~runner,family,stream,tick,name,Stream.read_declaration(~S,~E,stream),W.namespace(~S,~C,~R,~P,world)) # Register a reader against the stream selected by the closed take/put route. # Derives read authority from that stream's operational declaration and uses the # ordinary Registry registration rules. It allocates a reader identity without # delivering events. Refusal, including exhausted reader IDs or failed Registry # registration, returns RegistrationFailed with the returned runtime. # The runner receives event-runtime.Input{values, lagged, args}. This entry is # for Data streams; affine payload publication/reading uses the separate # ordinary-owned-event-* APIs. Qualified family setup and retention/refusal # scenarios are linked from docs/parity/typed-data-events.md. def register(~S:Data,~C:Type,~R:Type,~P:Data,~E:Data,~F:Type,~A:Type,~Error:Data,~Out:Type,~take:F -> F & Stream.Stream,~put:F -> Stream.Stream -> F,~runner:W.World -> Ev.Input -> Sys.Outcome,Error,Out>,runtime:Route.Runtime,name:String) -> Registration: match runtime: case Route.Runtime{world,family,tick}:taken(~S,~C,~R,~P,~E,~F,~A,~Error,~Out,~take,~put,~runner,world,tick,name,take(family)) # Advanced type alias for the retained reader and Registry for this exact # stream route and runner. Use it in App owner aliases, not as a numeric ID token. def OwnerType(~S:Data,~C:Type,~R:Type,~P:Data,~E:Data,~F:Type,~A:Type,~Error:Data,~Out:Type,~take:F -> F & Stream.Stream,~put:F -> Stream.Stream -> F,~runner:W.World -> Ev.Input -> Sys.Outcome,Error,Out>) -> Type:Owner,Sys.Registry,Error,Out,runner>> # App type after installing this family reader alongside prior owners. # Keeps the schema, runtime family and combined metadata observer; this leaf adds # Unit to observation rows rather than copying event payloads into debug output. # Use install to perform registration and construct the extended application. def Application(~S:Data,~C:Type,~R:Type,~P:Data,~E:Data,~F:Type,~A:Type,~Error:Data,~Out:Type,~take:F -> F & Stream.Stream,~put:F -> Stream.Stream -> F,~runner:W.World -> Ev.Input -> Sys.Outcome,Error,Out>,~Owners:Type,~Rows:Data,~prior:W.World -> W.World & DTO.Observation,~fold:Owners -> Owners & List<&2,Records.Fields>) -> Type:App.Application,Sys.Registry,Error,Out,runner>>,DTO.Product,Tree.observe_pair(~W.World,~Rows,~Unit,~prior,~observe(~S,~C,~R,~P)),Leaf.fold_pair(~S,~Owners,~Owner,Sys.Registry,Error,Out,runner>>,~fold,~fields(~S,~C,~R,~P,~E,~Route.Reader,~A,~Error,~Out,~runner))> # Advanced composition entry for an already-registered reader owner. # Consumes the composer and owner, returning their combined owners/observer. # Does not register a second reader or deliver events. Ordinary setup uses install. def append(~S:Data,~C:Type,~R:Type,~P:Data,~E:Data,~F:Type,~A:Type,~Error:Data,~Out:Type,~take:F -> F & Stream.Stream,~put:F -> Stream.Stream -> F,~runner:W.World -> Ev.Input -> Sys.Outcome,Error,Out>,~Owners:Type,~Rows:Data,~prior:W.World -> W.World & DTO.Observation,~fold:Owners -> Owners & List<&2,Records.Fields>,owner:Owner,Sys.Registry,Error,Out,runner>>,value:Build.Registered,Owners,Rows,prior,fold>) -> Build.Registered,Owners & Owner,Sys.Registry,Error,Out,runner>>,DTO.Product,Tree.observe_pair(~W.World,~Rows,~Unit,~prior,~observe(~S,~C,~R,~P)),Leaf.fold_pair(~S,~Owners,~Owner,Sys.Registry,Error,Out,runner>>,~fold,~fields(~S,~C,~R,~P,~E,~Route.Reader,~A,~Error,~Out,~runner))>: match value: case Build.Registered{world,owners,_,debug}:Build.appended(~S,~W.World,~Owners,~Rows,~prior,~fold,~Owner,Sys.Registry,Error,Out,runner>>,~Unit,~observe(~S,~C,~R,~P),~fields(~S,~C,~R,~P,~E,~Route.Reader,~A,~Error,~Out,~runner),world,owners,owner,debug) def appended(~S:Data,~C:Type,~R:Type,~P:Data,~E:Data,~F:Type,~A:Type,~Error:Data,~Out:Type,~take:F -> F & Stream.Stream,~put:F -> Stream.Stream -> F,~runner:W.World -> Ev.Input -> Sys.Outcome,Error,Out>,~Owners:Type,~Rows:Data,~prior:W.World -> W.World & DTO.Observation,~fold:Owners -> Owners & List<&2,Records.Fields>,schema:Schema.Schema,family:F,tick:Nat,owner:Owner,Sys.Registry,Error,Out,runner>>,value:Build.Registered,Owners,Rows,prior,fold>) -> Application(~S,~C,~R,~P,~E,~F,~A,~Error,~Out,~take,~put,~runner,~Owners,~Rows,~prior,~fold): App.attached(~S,~C,~R,~P,~F,~(Owners & Owner,Sys.Registry,Error,Out,runner>>),~DTO.Product,~Tree.observe_pair(~W.World,~Rows,~Unit,~prior,~observe(~S,~C,~R,~P)),~Leaf.fold_pair(~S,~Owners,~Owner,Sys.Registry,Error,Out,runner>>,~fold,~fields(~S,~C,~R,~P,~E,~Route.Reader,~A,~Error,~Out,~runner)),family,tick,SchemaApp.attached(~S,~C,~R,~P,~(Owners & Owner,Sys.Registry,Error,Out,runner>>),~DTO.Product,~Tree.observe_pair(~W.World,~Rows,~Unit,~prior,~observe(~S,~C,~R,~P)),~Leaf.fold_pair(~S,~Owners,~Owner,Sys.Registry,Error,Out,runner>>,~fold,~fields(~S,~C,~R,~P,~E,~Route.Reader,~A,~Error,~Out,~runner)),schema,append(~S,~C,~R,~P,~E,~F,~A,~Error,~Out,~take,~put,~runner,~Owners,~Rows,~prior,~fold,owner,value))) def installed(~S:Data,~C:Type,~R:Type,~P:Data,~E:Data,~F:Type,~A:Type,~Error:Data,~Out:Type,~take:F -> F & Stream.Stream,~put:F -> Stream.Stream -> F,~runner:W.World -> Ev.Input -> Sys.Outcome,Error,Out>,~Owners:Type,~Rows:Data,~prior:W.World -> W.World & DTO.Observation,~fold:Owners -> Owners & List<&2,Records.Fields>,~Output:Type,done:Application(~S,~C,~R,~P,~E,~F,~A,~Error,~Out,~take,~put,~runner,~Owners,~Rows,~prior,~fold) -> Output,failed:App.Application -> Output,schema:Schema.Schema,owners:Owners,recipe:Tree.Recipe,Rows,prior>,debug:Bool,result:Registration) -> Output: match result: case RegistrationFailed{runtime}:failed(App.Application{schema,runtime,owners,recipe,debug}) case Registered{Route.Runtime{world,family,tick},owner}:done(appended(~S,~C,~R,~P,~E,~F,~A,~Error,~Out,~take,~put,~runner,~Owners,~Rows,~prior,~fold,schema,family,tick,owner,Build.Registered{world,owners,recipe,debug})) # Register and append a typed Data-family reader to an existing family App. # Consumes value and calls done with the extended App on success, or failed # with the returned previous App on refusal. Schema, family, prior owners and # debug setting are threaded through both branches. name follows Registry rules. # This is setup only: it neither runs the reader nor advances its delivery cursor. def install(~S:Data,~C:Type,~R:Type,~P:Data,~E:Data,~F:Type,~A:Type,~Error:Data,~Out:Type,~take:F -> F & Stream.Stream,~put:F -> Stream.Stream -> F,~runner:W.World -> Ev.Input -> Sys.Outcome,Error,Out>,~Owners:Type,~Rows:Data,~prior:W.World -> W.World & DTO.Observation,~fold:Owners -> Owners & List<&2,Records.Fields>,~Output:Type,done:Application(~S,~C,~R,~P,~E,~F,~A,~Error,~Out,~take,~put,~runner,~Owners,~Rows,~prior,~fold) -> Output,failed:App.Application -> Output,value:App.Application,name:String) -> Output: match value: case App.Application{schema,runtime,owners,recipe,debug}:installed(~S,~C,~R,~P,~E,~F,~A,~Error,~Out,~take,~put,~runner,~Owners,~Rows,~prior,~fold,~Output,done,failed,schema,owners,recipe,debug,register(~S,~C,~R,~P,~E,~F,~A,~Error,~Out,~take,~put,~runner,runtime,name)) # Returned runtime and reader owner together with event-runtime.Completion. # Succeeded carries Out, Failed carries the runner's error, and Rejected returns # Args when execution authority is refused. Always retain the returned runtime # and owner; failure does not mean the input owners can be reused separately. type RunResult F & Stream.Stream,-put:F -> Stream.Stream -> F,-runner:W.World -> Ev.Input -> Sys.Outcome,Error,Out>> is Type: Ran{runtime:Route.Runtime,owner:Owner,Sys.Registry,Error,Out,runner>>,completion:Ev.Completion} def ran(~S:Data,~C:Type,~R:Type,~P:Data,~E:Data,~F:Type,~A:Type,~Error:Data,~Out:Type,~take:F -> F & Stream.Stream,~put:F -> Stream.Stream -> F,~runner:W.World -> Ev.Input -> Sys.Outcome,Error,Out>,declaration:Event.Declaration,result:Route.RunResult) -> RunResult: match result: case Route.Ran{runtime,reader,registry,completion}:Ran{runtime,Owner{declaration,reader,registry},completion} # Run this registered reader against its typed family stream. # The runner receives retained unread values in publication order, a lagged flag # and args. On Sys.Succeeded the reader advances and synchronize threads committed # World/family publication into the returned runtime. On Sys.Failed its stream # position does not advance; the runner's returned World is retained. This wrapper # does not itself undo arbitrary callback or host-IO effects. # A routing/registration rejection returns Rejected{args} without consuming the # read position. The completion and actual reader/Registry owners are returned # on every branch. Stream retention/capacity still bound later availability; # lagged reports dropped unread history, so failure is not an unlimited retry # retention promise. Supply the matching family synchronization route; merely # observing fields/metadata is not a run. See event-family-routing and the # qualified scenarios in docs/parity/typed-data-events.md. def run(~S:Data,~C:Type,~R:Type,~P:Data,~E:Data,~F:Type,~A:Type,~Error:Data,~Out:Type,~take:F -> F & Stream.Stream,~put:F -> Stream.Stream -> F,~runner:W.World -> Ev.Input -> Sys.Outcome,Error,Out>,~synchronize:W.World -> F -> Nat -> W.World & F,owner:Owner,Sys.Registry,Error,Out,runner>>,runtime:Route.Runtime,args:A) -> RunResult: match owner: case Owner{declaration,reader,registry}:ran(~S,~C,~R,~P,~E,~F,~A,~Error,~Out,~take,~put,~runner,declaration,Route.run(~S,~C,~R,~P,~E,~F,~A,~Error,~Out,~take,~put,~runner,~synchronize,runtime,reader,registry,args))