2020import io .github .agentic .ai .api .chat .ChatRole ;
2121import io .github .agentic .ai .api .chat .GenerationOptions ;
2222import io .github .agentic .ai .api .tool .ToolRegistry ;
23+ import io .github .agentic .ai .runtime .execution .ExecutionGuard ;
2324import io .github .agentic .ai .runtime .execution .TrustedExecutionContext ;
2425
2526import java .util .ArrayList ;
@@ -62,6 +63,12 @@ public class ModelRequest {
6263 // context map.
6364 private final TrustedExecutionContext trustedExecutionContext ;
6465
66+ // The guard of the run that owns this request. It carries the liveness and the
67+ // deadline of the run to the interceptors, so that a retry sequence can be
68+ // bounded by the run itself. It never leaves the process: the request attributes
69+ // are passed to the provider and must stay free of runtime objects.
70+ private final ExecutionGuard executionGuard ;
71+
6572 public ModelRequest (ChatMessage systemMessage , List <ChatMessage > messages , GenerationOptions options ,
6673 ChatClient chatClient , ToolRegistry toolRegistry , List <String > tools , boolean toolSelectionSpecified ,
6774 ToolRegistry dynamicToolRegistry , Map <String , String > toolDescriptions , Map <String , Object > context ,
@@ -74,6 +81,15 @@ public ModelRequest(ChatMessage systemMessage, List<ChatMessage> messages, Gener
7481 ChatClient chatClient , ToolRegistry toolRegistry , List <String > tools , boolean toolSelectionSpecified ,
7582 ToolRegistry dynamicToolRegistry , Map <String , String > toolDescriptions , Map <String , Object > context ,
7683 Map <String , Object > attributes , TrustedExecutionContext trustedExecutionContext ) {
84+ this (systemMessage , messages , options , chatClient , toolRegistry , tools , toolSelectionSpecified ,
85+ dynamicToolRegistry , toolDescriptions , context , attributes , trustedExecutionContext , null );
86+ }
87+
88+ public ModelRequest (ChatMessage systemMessage , List <ChatMessage > messages , GenerationOptions options ,
89+ ChatClient chatClient , ToolRegistry toolRegistry , List <String > tools , boolean toolSelectionSpecified ,
90+ ToolRegistry dynamicToolRegistry , Map <String , String > toolDescriptions , Map <String , Object > context ,
91+ Map <String , Object > attributes , TrustedExecutionContext trustedExecutionContext ,
92+ ExecutionGuard executionGuard ) {
7793 if (systemMessage != null && systemMessage .role () != ChatRole .SYSTEM ) {
7894 throw new IllegalArgumentException ("systemMessage must have the SYSTEM role" );
7995 }
@@ -90,6 +106,7 @@ public ModelRequest(ChatMessage systemMessage, List<ChatMessage> messages, Gener
90106 this .context = new HashMap <>(context );
91107 this .attributes = Map .copyOf (attributes );
92108 this .trustedExecutionContext = trustedExecutionContext ;
109+ this .executionGuard = executionGuard ;
93110 }
94111
95112 public static Builder builder () {
@@ -107,7 +124,8 @@ public static Builder builder(ModelRequest request) {
107124 .toolDescriptions (request .toolDescriptions )
108125 .context (request .context )
109126 .attributes (request .attributes )
110- .trustedExecutionContext (request .trustedExecutionContext );
127+ .trustedExecutionContext (request .trustedExecutionContext )
128+ .executionGuard (request .executionGuard );
111129 copy .toolSelectionSpecified = request .toolSelectionSpecified ;
112130 return copy ;
113131 }
@@ -167,6 +185,16 @@ public Optional<TrustedExecutionContext> getTrustedExecutionContext() {
167185 return Optional .ofNullable (trustedExecutionContext );
168186 }
169187
188+ /**
189+ * The guard of the run that issued this model call, when the run carries one. The
190+ * guard reports the liveness and the deadline of the run, so that an interceptor
191+ * that repeats the call can stop the sequence when the run is cancelled or out of
192+ * time. It is never passed to the provider.
193+ */
194+ public Optional <ExecutionGuard > getExecutionGuard () {
195+ return Optional .ofNullable (executionGuard );
196+ }
197+
170198 public Map <String , String > getToolDescriptions () {
171199 return toolDescriptions ;
172200 }
@@ -209,6 +237,8 @@ public static class Builder {
209237
210238 private TrustedExecutionContext trustedExecutionContext ;
211239
240+ private ExecutionGuard executionGuard ;
241+
212242 public Builder systemMessage (ChatMessage systemMessage ) {
213243 this .systemMessage = systemMessage ;
214244 return this ;
@@ -277,10 +307,19 @@ public Builder trustedExecutionContext(TrustedExecutionContext trustedExecutionC
277307 return this ;
278308 }
279309
310+ /**
311+ * Sets the guard of the run that owns this model call. The guard is propagated
312+ * by the engine and stays inside the process.
313+ */
314+ public Builder executionGuard (ExecutionGuard executionGuard ) {
315+ this .executionGuard = executionGuard ;
316+ return this ;
317+ }
318+
280319 public ModelRequest build () {
281320 return new ModelRequest (systemMessage , messages , options , chatClient , toolRegistry , tools ,
282321 toolSelectionSpecified , dynamicToolRegistry , toolDescriptions , context , attributes ,
283- trustedExecutionContext );
322+ trustedExecutionContext , executionGuard );
284323 }
285324
286325 }
0 commit comments