import Base import ./world.bend as W import ./system.bend as Sys import ./transaction.bend as T import ./owner_product.bend as Product import ./ordinary_recovery_declaration.bend as D import ./ordinary_recoverable_array_resource.bend as F import ./recovery_journal.bend as Journal # Affine invocation input pairing caller Args with the retained capture. # Admission refusal returns both through the registered request protocol. type Request is Type:Request{args:Args,capture:Capture} # Semantic outcome plus retained Out, capture and typed recovery receipts. # Body errors stay inside Reply so transport success does not discard owners. # Only explicitly journaled recoverable operations participate in restoration. type Reply is Type:Reply{outcome:T.Outcome,output:Out,capture:Capture,recovery:Recovery} # Affine recoverable-system owner pairing actual Registry and capture. # Metadata or a printable name cannot substitute for the live registration. type Owner is Type:Owner{registry:Registry,capture:Capture} # Completed retains World, owner, semantic outcome, Out and recovery. # RegistrationRejected returns original Args and owner without running body. # TransportFailed retains only World and is unreachable through the canonical # closed runner, which returns semantic errors in Reply. type Run is Type: Completed{world:World,owner:Owner,outcome:T.Outcome,output:Out,recovery:Recovery} RegistrationRejected{world:World,owner:Owner,args:Args} TransportFailed{world:World} # Defined returns World and registered capture owner; DefinitionRejected # returns World and initial capture, without invoking the body. type Defined is Type: Defined{world:World,owner:Owner} DefinitionRejected{world:World,capture:Capture} def finished(~S:Data,~C:Type,~R:Type,~E:Data,~Capture:Type,~Recovery:Type,~Ops:Type -> Type,~Args:Type,~Error:Data,~Out:Type,~builder:D.Builder(~Product.Owner,Capture>,~Recovery,~Ops),~declaration:D.Declaration,Capture>,Recovery,Ops,builder>,~body:@-H:Type -> @-caps:Ops(H) -> Args -> H -> H & (T.Outcome & Out),outcome:T.Outcome,output:Out,result:Product.Owner,Capture> & Recovery) -> Sys.Outcome,Unit,Reply>: match result: case (Product.Owner{world,capture},recovery):Sys.Succeeded{world,Reply{outcome,output,capture,recovery}} def completing(~S:Data,~C:Type,~R:Type,~E:Data,~Capture:Type,~Recovery:Type,~Ops:Type -> Type,~Args:Type,~Error:Data,~Out:Type,~builder:D.Builder(~Product.Owner,Capture>,~Recovery,~Ops),~declaration:D.Declaration,Capture>,Recovery,Ops,builder>,~body:@-H:Type -> @-caps:Ops(H) -> Args -> H -> H & (T.Outcome & Out),state:Product.Owner,Capture>,journal:List,Capture>,~Recovery)>,result:T.Outcome & Out) -> Sys.Outcome,Unit,Reply>: match result: case (+outcome,output):finished(~S,~C,~R,~E,~Capture,~Recovery,~Ops,~Args,~Error,~Out,~builder,~declaration,~body,outcome,output,Journal.finish(~Product.Owner,Capture>,~Recovery,journal,F.succeeded(~Error,outcome),state,D.empty(~S,~Product.Owner,Capture>,~Recovery,~Ops,~builder,declaration)(Unit{}))) def returned(~S:Data,~C:Type,~R:Type,~E:Data,~Capture:Type,~Recovery:Type,~Ops:Type -> Type,~Args:Type,~Error:Data,~Out:Type,~builder:D.Builder(~Product.Owner,Capture>,~Recovery,~Ops),~declaration:D.Declaration,Capture>,Recovery,Ops,builder>,~body:@-H:Type -> @-caps:Ops(H) -> Args -> H -> H & (T.Outcome & Out),result:F.Frame,Capture>,Recovery> & (T.Outcome & Out)) -> Sys.Outcome,Unit,Reply>: match result: case (F.Frame{state,journal},result):completing(~S,~C,~R,~E,~Capture,~Recovery,~Ops,~Args,~Error,~Out,~builder,~declaration,~body,state,journal,result) # Advanced recoverable runner over World/capture product and shared journal. # Canonical declared inverses determine commit/rollback receipts; arbitrary # external effects or undeclared mutations are not made reversible. Returns # outer Succeeded with semantic outcome and actual owners in Reply. def runner(~S:Data,~C:Type,~R:Type,~E:Data,~Capture:Type,~Recovery:Type,~Ops:Type -> Type,~Args:Type,~Error:Data,~Out:Type,~builder:D.Builder(~Product.Owner,Capture>,~Recovery,~Ops),~declaration:D.Declaration,Capture>,Recovery,Ops,builder>,~body:@-H:Type -> @-caps:Ops(H) -> Args -> H -> H & (T.Outcome & Out),world:W.World,request:Request) -> Sys.Outcome,Unit,Reply>: match request: case Request{args,capture}:returned(~S,~C,~R,~E,~Capture,~Recovery,~Ops,~Args,~Error,~Out,~builder,~declaration,~body,body(F.Frame,Capture>,Recovery>,D.root_grant(~S,~Product.Owner,Capture>,~Recovery,~Ops,~builder),args,F.Frame{Product.Owner{world,capture},[]})) # Advanced type alias for the matching Registry/capture owner. # Does not create a registration or validate a World by itself. def Owned(~S:Data,~C:Type,~R:Type,~E:Data,~Capture:Type,~Recovery:Type,~Ops:Type -> Type,~Args:Type,~Error:Data,~Out:Type,~builder:D.Builder(~Product.Owner,Capture>,~Recovery,~Ops),~declaration:D.Declaration,Capture>,Recovery,Ops,builder>,~body:@-H:Type -> @-caps:Ops(H) -> Args -> H -> H & (T.Outcome & Out)) -> Type:Owner,Unit,Reply,runner(~S,~C,~R,~E,~Capture,~Recovery,~Ops,~Args,~Error,~Out,~builder,~declaration,~body)>,Capture> def registered(~S:Data,~C:Type,~R:Type,~E:Data,~Capture:Type,~Recovery:Type,~Ops:Type -> Type,~Args:Type,~Error:Data,~Out:Type,~builder:D.Builder(~Product.Owner,Capture>,~Recovery,~Ops),~declaration:D.Declaration,Capture>,Recovery,Ops,builder>,~body:@-H:Type -> @-caps:Ops(H) -> Args -> H -> H & (T.Outcome & Out),capture:Capture,result:Sys.RegistrationResult,Unit,Reply,runner(~S,~C,~R,~E,~Capture,~Recovery,~Ops,~Args,~Error,~Out,~builder,~declaration,~body)>) -> Defined,Owner,Unit,Reply,runner(~S,~C,~R,~E,~Capture,~Recovery,~Ops,~Args,~Error,~Out,~builder,~declaration,~body)>,Capture>,Capture>: match result: case Sys.Registered{world,registry}:Defined{world,Owner{registry,capture}} case Sys.RegistrationFailed{world}:DefinitionRejected{world,capture} # Register a recoverable body with access derived from its declaration. # Consumes capture on success; definition refusal returns it with World. # No body or inverse operation runs at registration. # Contract and consumers: docs/parity/recovery.md. def define(~S:Data,~C:Type,~R:Type,~E:Data,~Capture:Type,~Recovery:Type,~Ops:Type -> Type,~Args:Type,~Error:Data,~Out:Type,~builder:D.Builder(~Product.Owner,Capture>,~Recovery,~Ops),~declaration:D.Declaration,Capture>,Recovery,Ops,builder>,~body:@-H:Type -> @-caps:Ops(H) -> Args -> H -> H & (T.Outcome & Out),world:W.World,name:String,capture:Capture) -> Defined,Owner,Unit,Reply,runner(~S,~C,~R,~E,~Capture,~Recovery,~Ops,~Args,~Error,~Out,~builder,~declaration,~body)>,Capture>,Capture>: registered(~S,~C,~R,~E,~Capture,~Recovery,~Ops,~Args,~Error,~Out,~builder,~declaration,~body,capture,Sys.register(~S,~C,~R,~E,~Request,~Unit,~Reply,~runner(~S,~C,~R,~E,~Capture,~Recovery,~Ops,~Args,~Error,~Out,~builder,~declaration,~body),world,name,D.access(~S,~Product.Owner,Capture>,~Recovery,~Ops,~builder,declaration))) # Completion retains the Registry already advanced to its captured run tick. # Semantic failure still returns its explicit Reply and recovery owners. def done(~S:Data,~C:Type,~R:Type,~E:Data,~Capture:Type,~Recovery:Type,~Ops:Type -> Type,~Args:Type,~Error:Data,~Out:Type,~builder:D.Builder(~Product.Owner,Capture>,~Recovery,~Ops),~declaration:D.Declaration,Capture>,Recovery,Ops,builder>,~body:@-H:Type -> @-caps:Ops(H) -> Args -> H -> H & (T.Outcome & Out),outcome:T.Outcome,output:Out,capture:Capture,recovery:Recovery,result:W.World & Sys.Registry,Unit,Reply,runner(~S,~C,~R,~E,~Capture,~Recovery,~Ops,~Args,~Error,~Out,~builder,~declaration,~body)>) -> Run,Owner,Unit,Reply,runner(~S,~C,~R,~E,~Capture,~Recovery,~Ops,~Args,~Error,~Out,~builder,~declaration,~body)>,Capture>,Args,Error,Out,Recovery>: match result: case (world,registry):Completed{world,Owner{registry,capture},outcome,output,recovery} def completed(~S:Data,~C:Type,~R:Type,~E:Data,~Capture:Type,~Recovery:Type,~Ops:Type -> Type,~Args:Type,~Error:Data,~Out:Type,~builder:D.Builder(~Product.Owner,Capture>,~Recovery,~Ops),~declaration:D.Declaration,Capture>,Recovery,Ops,builder>,~body:@-H:Type -> @-caps:Ops(H) -> Args -> H -> H & (T.Outcome & Out),result:Sys.RunResult,Unit,Reply,runner(~S,~C,~R,~E,~Capture,~Recovery,~Ops,~Args,~Error,~Out,~builder,~declaration,~body)>) -> Run,Owner,Unit,Reply,runner(~S,~C,~R,~E,~Capture,~Recovery,~Ops,~Args,~Error,~Out,~builder,~declaration,~body)>,Capture>,Args,Error,Out,Recovery>: match result: case Sys.RegistrationRejected{registry,world,Request{args,capture}}:RegistrationRejected{world,Owner{registry,capture},args} case Sys.Completed{registry,Sys.Succeeded{world,Reply{+outcome,output,capture,recovery}}}:done(~S,~C,~R,~E,~Capture,~Recovery,~Ops,~Args,~Error,~Out,~builder,~declaration,~body,outcome,output,capture,recovery,(world,registry)) # Unreachable through the closed runner, which retains semantic errors in Reply. case Sys.Completed{_,Sys.Failed{world,_}}:TransportFailed{world} # Validate registration and execute with retained capture and affine Args. # Admission refusal returns original owners/Args. Completed returns semantic # outcome, Out and typed recovery on both body success and failure; inspect # outcome instead of assuming outer transport means commit. Every admitted completion consumes its captured lifecycle tick, # including semantic failure; recovery ownership remains in Reply. def run(~S:Data,~C:Type,~R:Type,~E:Data,~Capture:Type,~Recovery:Type,~Ops:Type -> Type,~Args:Type,~Error:Data,~Out:Type,~builder:D.Builder(~Product.Owner,Capture>,~Recovery,~Ops),~declaration:D.Declaration,Capture>,Recovery,Ops,builder>,~body:@-H:Type -> @-caps:Ops(H) -> Args -> H -> H & (T.Outcome & Out),owner:Owner,Unit,Reply,runner(~S,~C,~R,~E,~Capture,~Recovery,~Ops,~Args,~Error,~Out,~builder,~declaration,~body)>,Capture>,world:W.World,args:Args) -> Run,Owner,Unit,Reply,runner(~S,~C,~R,~E,~Capture,~Recovery,~Ops,~Args,~Error,~Out,~builder,~declaration,~body)>,Capture>,Args,Error,Out,Recovery>: match owner: case Owner{registry,capture}:completed(~S,~C,~R,~E,~Capture,~Recovery,~Ops,~Args,~Error,~Out,~builder,~declaration,~body,Sys.run(~S,~C,~R,~E,~Request,~Unit,~Reply,~runner(~S,~C,~R,~E,~Capture,~Recovery,~Ops,~Args,~Error,~Out,~builder,~declaration,~body),registry,world,Request{args,capture}))