Skip to content

Commit a988b7c

Browse files
committed
fix(workflow): keep the child resume token across repeated suspensions (issue agentic-ai-java#274 review)
1 parent 3d3d12d commit a988b7c

2 files changed

Lines changed: 79 additions & 21 deletions

File tree

‎argi-orchestration/argi-workflow-core/src/main/java/io/github/agentic/ai/workflow/WorkflowAgent.java‎

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -453,29 +453,27 @@ private ExecutionResult executeAgent(WorkflowStep.Agent step, RunData data, Agen
453453
"agent resolver returned no agent for " + step.agentRef());
454454
Map<String, Object> inputs = resolveMap(step.inputs(), data);
455455
AgentOutcome outcome = await(agent.invoke(new AgentRequest(data.messages, inputs, null), context));
456+
return resumeUntilCompleted(step, agent, data, outcome);
457+
}
458+
459+
/**
460+
* Applies a completed child outcome, or suspends on the child's own resume token
461+
* and re-enters this same path on resume, so any number of child suspensions is
462+
* resumed until the child actually completes.
463+
*/
464+
private ExecutionResult resumeUntilCompleted(WorkflowStep.Agent step, Agent agent, RunData data,
465+
AgentOutcome outcome) {
456466
data.messages.addAll(outcome.messages());
457467
data.addUsage(outcome.usage());
458-
if (outcome.status() == AgentOutcome.Status.SUSPENDED) {
459-
return ExecutionResult.suspended(data, outcome.suspension().prompts(), (request, resumeContext) -> {
460-
AgentOutcome resumed = await(agent.invoke(new AgentRequest(request.messages(), request.inputs(),
461-
outcome.suspension().resumeToken()), resumeContext));
462-
data.messages.addAll(resumed.messages());
463-
data.addUsage(resumed.usage());
464-
if (resumed.status() == AgentOutcome.Status.SUSPENDED) {
465-
return ExecutionResult.suspended(data, resumed.suspension().prompts(), (next, nextContext) -> {
466-
AgentOutcome finalOutcome = await(agent.invoke(new AgentRequest(next.messages(), next.inputs(),
467-
resumed.suspension().resumeToken()), nextContext));
468-
data.messages.addAll(finalOutcome.messages());
469-
data.addUsage(finalOutcome.usage());
470-
return finalOutcome.status() == AgentOutcome.Status.SUSPENDED
471-
? ExecutionResult.suspended(data, finalOutcome.suspension().prompts(), (again, againContext) ->
472-
ExecutionResult.completed(data)) : applyAgentOutput(step, finalOutcome, data);
473-
});
474-
}
475-
return applyAgentOutput(step, resumed, data);
476-
});
477-
}
478-
return applyAgentOutput(step, outcome, data);
468+
if (outcome.status() != AgentOutcome.Status.SUSPENDED) {
469+
return applyAgentOutput(step, outcome, data);
470+
}
471+
AgentOutcome.Suspension suspension = outcome.suspension();
472+
return ExecutionResult.suspended(data, suspension.prompts(), (request, resumeContext) -> {
473+
AgentOutcome resumed = await(agent.invoke(
474+
new AgentRequest(request.messages(), request.inputs(), suspension.resumeToken()), resumeContext));
475+
return resumeUntilCompleted(step, agent, data, resumed);
476+
});
479477
}
480478

481479
private ExecutionResult applyAgentOutput(WorkflowStep.Agent step, AgentOutcome outcome, RunData data) {

‎argi-orchestration/argi-workflow-core/src/test/java/io/github/agentic/ai/workflow/WorkflowSchemaContractTest.java‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)