1 import Base 2 import ./world.bend as W 3 import ./system.bend as Sys 4 import ./transaction.bend as T 5 import ./owner_product.bend as Product 6 import ./ordinary_recovery_declaration.bend as D 7 import ./ordinary_recoverable_array_resource.bend as F 8 import ./recovery_journal.bend as Journal 9 # Affine invocation input pairing caller Args with the retained capture. 10 # Admission refusal returns both through the registered request protocol. 11 type Request is Type:Request{args:Args,capture:Capture} 12 # Semantic outcome plus retained Out, capture and typed recovery receipts. 13 # Body errors stay inside Reply so transport success does not discard owners. 14 # Only explicitly journaled recoverable operations participate in restoration. 15 type Reply is Type:Reply{outcome:T.Outcome,output:Out,capture:Capture,recovery:Recovery} 16 # Affine recoverable-system owner pairing actual Registry and capture. 17 # Metadata or a printable name cannot substitute for the live registration. 18 type Owner is Type:Owner{registry:Registry,capture:Capture} 19 # Completed retains World, owner, semantic outcome, Out and recovery. 20 # RegistrationRejected returns original Args and owner without running body. 21 # TransportFailed retains only World and is unreachable through the canonical 22 # closed runner, which returns semantic errors in Reply. 23 type Run is Type: 24 Completed{world:World,owner:Owner,outcome:T.Outcome,output:Out,recovery:Recovery} 25 RegistrationRejected{world:World,owner:Owner,args:Args} 26 TransportFailed{world:World} 27 # Defined returns World and registered capture owner; DefinitionRejected 28 # returns World and initial capture, without invoking the body. 29 type Defined is Type: 30 Defined{world:World,owner:Owner} 31 DefinitionRejected{world:World,capture:Capture} 32 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>: 33 match result: 34 case (Product.Owner{world,capture},recovery):Sys.Succeeded{world,Reply{outcome,output,capture,recovery}} 35 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>: 36 match result: 37 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{}))) 38 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>: 39 match result: 40 case (F.Frame{state,journal},result):completing(~S,~C,~R,~E,~Capture,~Recovery,~Ops,~Args,~Error,~Out,~builder,~declaration,~body,state,journal,result) 41 # Advanced recoverable runner over World/capture product and shared journal. 42 # Canonical declared inverses determine commit/rollback receipts; arbitrary 43 # external effects or undeclared mutations are not made reversible. Returns 44 # outer Succeeded with semantic outcome and actual owners in Reply. 45 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>: 46 match request: 47 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},[]})) 48 # Advanced type alias for the matching Registry/capture owner. 49 # Does not create a registration or validate a World by itself. 50 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> 51 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>: 52 match result: 53 case Sys.Registered{world,registry}:Defined{world,Owner{registry,capture}} 54 case Sys.RegistrationFailed{world}:DefinitionRejected{world,capture} 55 # Register a recoverable body with access derived from its declaration. 56 # Consumes capture on success; definition refusal returns it with World. 57 # No body or inverse operation runs at registration. 58 # Contract and consumers: docs/parity/recovery.md. 59 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>: 60 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))) 61 # Completion retains the Registry already advanced to its captured run tick. 62 # Semantic failure still returns its explicit Reply and recovery owners. 63 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>: 64 match result: 65 case (world,registry):Completed{world,Owner{registry,capture},outcome,output,recovery} 66 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>: 67 match result: 68 case Sys.RegistrationRejected{registry,world,Request{args,capture}}:RegistrationRejected{world,Owner{registry,capture},args} 69 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)) 70 # Unreachable through the closed runner, which retains semantic errors in Reply. 71 case Sys.Completed{_,Sys.Failed{world,_}}:TransportFailed{world} 72 # Validate registration and execute with retained capture and affine Args. 73 # Admission refusal returns original owners/Args. Completed returns semantic 74 # outcome, Out and typed recovery on both body success and failure; inspect 75 # outcome instead of assuming outer transport means commit. Every admitted completion consumes its captured lifecycle tick, 76 # including semantic failure; recovery ownership remains in Reply. 77 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>: 78 match owner: 79 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}))