import Base import ./world.bend as W import ./system.bend as Sys import ./transaction.bend as T import ./commands.bend as Cmd import ./owned_event_storage.bend as Storage # Ordinary identity derives publication metadata and the confined grant. # Raw Frame and runner constructors are trusted engine administration. # Schema/payload-indexed owned event name. Contains no live log or payload; # raw names alone do not confer runtime publication or read authority. type Domain<-S:Data,-P:Type> is Data: Domain{key:String} # Declare a domain for arbitrary affine Type payload P without creating # log storage, publishing a payload or allocating a reader. def domain(~S:Data,~P:Type,key:String) -> Domain:Domain{key} # Return the authored event domain key without accessing a log or payload. def name(~S:Data,~P:Type,domain:Domain) -> String: match domain: case Domain{key}:key # Return authored domain name plus suffix for registration metadata. # This string does not replace the typed grant or runtime World checks. def access(~S:Data,~P:Type,domain:Domain,suffix:String) -> String: match domain: case Domain{key}:String.concat([key,suffix]) # Trusted single-domain engine stage owning transaction and unpublished # payloads. Gameplay receives abstract H through Write, not this raw constructor. type Frame is Type: Frame{tx:T.Tx,newest:List

} # Nominal publication grant consuming P and returning opaque H. It exposes # no live payload alias; bound operation defines staging/commit/abort behavior. type Write<-S:Data,-P:Type,-identity:Domain,-H:Type> is Type: Write{emit:H -> P -> H} # Advanced projection extracting the closed emission operation. Does not # publish, validate a World or transfer any payload by itself. def emit_field(~S:Data,~P:Type,~identity:Domain,~H:Type,grant:Write) -> H -> P -> H: match grant: case Write{emit}:emit # Consume an affine payload through its typed domain grant and return H. # Canonical staged grants retain payload until commit or abort recovery; this # helper itself adds no admission result or arbitrary mutation recovery. def emit(~S:Data,~P:Type,~identity:Domain,~H:Type,~grant:Write,owner:H,payload:P) -> H: emit_field(~S,~P,~identity,~H,grant)(owner,payload) def staged(~World:Type,~P:Type,owner:Frame,payload:P) -> Frame: match owner: case Frame{tx,newest}:Frame{tx,payload <> newest} # Single-domain publication reply retaining body output and staged payload # recovery. Failure returns available staged owners in emission order; success # commits them and returns an empty recovery list. Inspect the inner outcome. type Reply is Type: Reply{outcome:T.Outcome,output:Out,recovered:List

} # Only available independently staged owners are recovered, after ECS rollback. def finished(~S:Data,~C:Type,~R:Type,~P:Type,~Error:Data,~Out:Type,newest:List

,output:Out,result:T.Returned,Nat>,Error>) -> Sys.Outcome,Nat>,Unit,Reply>: match result: case T.Returned{world,T.Success{}}:Sys.Succeeded{Storage.commit_owned(~S,~C,~R,~P,world,List.reverse(&1,P,newest)),Reply{T.Success{},output,[]}} case T.Returned{world,T.Failure{error}}:Sys.Succeeded{world,Reply{T.Failure{error},output,List.reverse(&1,P,newest)}} def returned(~S:Data,~C:Type,~R:Type,~P:Type,~Error:Data,~Out:Type,result:Frame,Nat>,P> & (T.Outcome & Out)) -> Sys.Outcome,Nat>,Unit,Reply>: match result: case (Frame{tx,newest},(outcome,output)):finished(~S,~C,~R,~P,~Error,~Out,newest,output,T.finish(~W.World,Nat>,~Nat,~Error,~Cmd.enqueue(~S,~C,~Storage.Resources,~Nat),~Cmd.publish(~S,~C,~Storage.Resources,~Nat),tx,outcome)) # Advanced single-domain publisher body with opaque staged-owner grant. # Runs once, including an empty World. Body failure rolls back recorded ECS # changes and recovers separately staged payloads, not arbitrary consumed data. def runner(~S:Data,~C:Type,~R:Type,~P:Type,~identity:Domain,~Args:Type,~Error:Data,~Out:Type,~body:@-H:Type -> @-grant:Write -> Args -> H -> H & (T.Outcome & Out),world:W.World,Nat>,args:Args) -> Sys.Outcome,Nat>,Unit,Reply>: returned(~S,~C,~R,~P,~Error,~Out,body(Frame,Nat>,P>,Write{staged(~W.World,Nat>,~P)},args,Frame{T.begin(~W.World,Nat>,~Nat,world),[]})) # Advanced closed publisher registration type. Runtime namespace/live # metadata checks still occur through run; this alias owns no registration. def Registry(~S:Data,~C:Type,~R:Type,~P:Type,~identity:Domain,~Args:Type,~Error:Data,~Out:Type,~body:@-H:Type -> @-grant:Write -> Args -> H -> H & (T.Outcome & Out)) -> Type: Sys.Registry,Nat,Args,Unit,Reply,runner(~S,~C,~R,~P,~identity,~Args,~Error,~Out,~body)> # Register the single-domain publisher with declaration-derived write # metadata. Retains World on registration failure and invokes no body here. def define(~S:Data,~C:Type,~R:Type,~P:Type,~identity:Domain,~Args:Type,~Error:Data,~Out:Type,~body:@-H:Type -> @-grant:Write -> Args -> H -> H & (T.Outcome & Out),world:W.World,Nat>,name:String) -> Sys.RegistrationResult,Nat,Args,Unit,Reply,runner(~S,~C,~R,~P,~identity,~Args,~Error,~Out,~body)>: Sys.register(~S,~C,~Storage.Resources,~Nat,~Args,~Unit,~Reply,~runner(~S,~C,~R,~P,~identity,~Args,~Error,~Out,~body),world,name,[access(~S,~P,identity,":write")]) # Run the checked single-domain publisher. Admission refusal returns original # Args/Registry/World; completed execution returns outer Succeeded with Reply # carrying the actual body outcome and available staged payload recovery. # This distinguishes safe recovery from an outer failure that discards Out. def run(~S:Data,~C:Type,~R:Type,~P:Type,~identity:Domain,~Args:Type,~Error:Data,~Out:Type,~body:@-H:Type -> @-grant:Write -> Args -> H -> H & (T.Outcome & Out),registry:Sys.Registry,Nat,Args,Unit,Reply,runner(~S,~C,~R,~P,~identity,~Args,~Error,~Out,~body)>,world:W.World,Nat>,args:Args) -> Sys.RunResult,Nat,Args,Unit,Reply,runner(~S,~C,~R,~P,~identity,~Args,~Error,~Out,~body)>: Sys.run(~S,~C,~Storage.Resources,~Nat,~Args,~Unit,~Reply,~runner(~S,~C,~R,~P,~identity,~Args,~Error,~Out,~body),registry,world,args)