@@ -255,6 +255,66 @@ void aTwiceSuspendedChildStillRunsTheFollowingSteps() throws Exception {
255255 }
256256 }
257257
258+ @ Test
259+ void aThriceSuspendedChildKeepsItsResumeTokenUntilCompletion () throws Exception {
260+ AtomicInteger invocations = new AtomicInteger ();
261+ AtomicInteger resumedInvocations = new AtomicInteger ();
262+ Agent thrice = agent (request -> {
263+ int call = invocations .incrementAndGet ();
264+ if (request .resumeToken () != null ) {
265+ resumedInvocations .incrementAndGet ();
266+ }
267+ if (call <= 3 ) {
268+ return new AgentOutcome (AgentOutcome .Status .SUSPENDED , List .of (ChatMessage .assistant ("need more" )),
269+ Map .of (), new Usage (1L , 1L , 2L ), new AgentOutcome .Suspension ("agent-token-" + call ,
270+ Map .of ("prompt" , "provide confirmation " + call )));
271+ }
272+ return AgentOutcome .completed (List .of (ChatMessage .assistant ("done" )),
273+ Map .of ("value" , "finally done" ), new Usage (1L , 1L , 2L ));
274+ });
275+ AtomicInteger toolExecutions = new AtomicInteger ();
276+ WorkflowDefinition definition = new WorkflowDefinition ("thrice" , "1" ,
277+ List .of (new WorkflowStep .Agent ("answer" , "ref" ),
278+ new WorkflowStep .Tool ("mark" , "mark" , Map .of (), "marked" , null )));
279+ WorkflowAgent workflow = WorkflowAgent .builder (definition ).agents (Map .of ("ref" , thrice ))
280+ .tools (new ToolRegistry (List .of (countingTool (toolExecutions )))).build ();
281+
282+ try (AgentExecutionContext first = context ()) {
283+ AgentOutcome suspendedOnce = workflow
284+ .invoke (new AgentRequest (List .of (ChatMessage .user ("go" )), Map .of ("question" , "why" ), null ), first )
285+ .get (3 , TimeUnit .SECONDS );
286+ assertEquals (AgentOutcome .Status .SUSPENDED , suspendedOnce .status ());
287+ String firstToken = suspendedOnce .suspension ().resumeToken ();
288+ try (AgentExecutionContext second = context ()) {
289+ AgentOutcome suspendedTwice = workflow
290+ .invoke (new AgentRequest (List .of (), Map .of (), firstToken ), second )
291+ .get (3 , TimeUnit .SECONDS );
292+ assertEquals (AgentOutcome .Status .SUSPENDED , suspendedTwice .status ());
293+ String secondToken = suspendedTwice .suspension ().resumeToken ();
294+ try (AgentExecutionContext third = context ()) {
295+ AgentOutcome suspendedThrice = workflow
296+ .invoke (new AgentRequest (List .of (), Map .of (), secondToken ), third )
297+ .get (3 , TimeUnit .SECONDS );
298+ assertEquals (AgentOutcome .Status .SUSPENDED , suspendedThrice .status ());
299+ String thirdToken = suspendedThrice .suspension ().resumeToken ();
300+ try (AgentExecutionContext fourth = context ()) {
301+ AgentOutcome outcome = workflow
302+ .invoke (new AgentRequest (List .of (), Map .of (), thirdToken ), fourth )
303+ .get (3 , TimeUnit .SECONDS );
304+ assertEquals (AgentOutcome .Status .COMPLETED , outcome .status ());
305+ // The third suspension keeps its token: the fourth invocation
306+ // completes the child and its outputs reach the workflow state.
307+ assertEquals (4 , invocations .get ());
308+ assertEquals (3 , resumedInvocations .get ());
309+ assertEquals (Map .of ("value" , "finally done" ), outcome .outputs ().get ("answer" ));
310+ // The step after the thrice-suspended child still runs.
311+ assertEquals (1 , toolExecutions .get ());
312+ }
313+ }
314+ }
315+ }
316+ }
317+
258318 @ Test
259319 void aLoopBodySuspendingTwiceStillCompletesTheRemainingIterations () throws Exception {
260320 AtomicInteger invocations = new AtomicInteger ();
0 commit comments