Skip to content

Commit ab05cfe

Browse files
committed
fix(workflow): resume the suspended continuation exactly once
Self-review of the agentic-ai-java#274 branch found that resuming a suspended workflow invoked the wrapped agent continuation twice: continueWith already resumed the continuation, and the composed step lambdas of executeSteps and Loop resumed it again. A real agent would reject the second call with the same resume token. The composed step now receives the resumed result through a ContinuedStep callback, and the resume tests assert a single invocation. Also fixes the indentation of the runSafely/run block (format:apply).
1 parent fafecf7 commit ab05cfe

2 files changed

Lines changed: 63 additions & 52 deletions

File tree

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

Lines changed: 53 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -261,51 +261,51 @@ public synchronized void cancel() {
261261
}
262262
}
263263

264-
private void runSafely() {
265-
try {
266-
ExecutionResult result = run();
267-
if (!cancelled) {
268-
if (result.suspension != null) {
269-
String token = register(result.continuation);
270-
AgentOutcome outcome = new AgentOutcome(AgentOutcome.Status.SUSPENDED, result.data.messages,
271-
result.data.state, result.data.usage(),
272-
new AgentOutcome.Suspension(token, result.suspension.prompts()));
273-
emit(new AgentEvent.Finished(outcome));
274-
}
275-
else {
276-
// The output boundary: a completed delivery must match the declared
277-
// output schema, whatever path (fresh run or resume) produced it.
278-
validate(result.data.state, outputSchema, "output");
279-
AgentOutcome outcome = AgentOutcome.completed(result.data.messages, result.data.state,
280-
result.data.usage());
281-
emit(new AgentEvent.Finished(outcome));
264+
private void runSafely() {
265+
try {
266+
ExecutionResult result = run();
267+
if (!cancelled) {
268+
if (result.suspension != null) {
269+
String token = register(result.continuation);
270+
AgentOutcome outcome = new AgentOutcome(AgentOutcome.Status.SUSPENDED, result.data.messages,
271+
result.data.state, result.data.usage(),
272+
new AgentOutcome.Suspension(token, result.suspension.prompts()));
273+
emit(new AgentEvent.Finished(outcome));
274+
}
275+
else {
276+
// The output boundary: a completed delivery must match the declared
277+
// output schema, whatever path (fresh run or resume) produced it.
278+
validate(result.data.state, outputSchema, "output");
279+
AgentOutcome outcome = AgentOutcome.completed(result.data.messages, result.data.state,
280+
result.data.usage());
281+
emit(new AgentEvent.Finished(outcome));
282+
}
283+
subscriber.onComplete();
282284
}
283-
subscriber.onComplete();
284285
}
285-
}
286-
catch (Throwable error) {
287-
if (!cancelled) {
288-
subscriber.onError(error);
286+
catch (Throwable error) {
287+
if (!cancelled) {
288+
subscriber.onError(error);
289+
}
289290
}
290291
}
291-
}
292292

293-
private ExecutionResult run() {
294-
context.control().assertActive();
295-
if (request.resumeToken() != null) {
296-
StoredContinuation stored = continuations.remove(request.resumeToken());
297-
if (stored == null || !stored.matches(context)) {
298-
throw new IllegalArgumentException("Unknown or unauthorized workflow resume token");
293+
private ExecutionResult run() {
294+
context.control().assertActive();
295+
if (request.resumeToken() != null) {
296+
StoredContinuation stored = continuations.remove(request.resumeToken());
297+
if (stored == null || !stored.matches(context)) {
298+
throw new IllegalArgumentException("Unknown or unauthorized workflow resume token");
299+
}
300+
// Inputs were validated on the fresh run that produced this continuation;
301+
// a resume request carries the continuation's state forward.
302+
return stored.continuation.resume(request, context);
299303
}
300-
// Inputs were validated on the fresh run that produced this continuation;
301-
// a resume request carries the continuation's state forward.
302-
return stored.continuation.resume(request, context);
303-
}
304-
RunData data = new RunData(request);
305-
// The input boundary: refuse before any step, tool or agent side effect.
306-
validate(data.inputs, inputSchema, "input");
307-
return executeSteps(definition.steps(), 0, data, context);
308-
}
304+
RunData data = new RunData(request);
305+
// The input boundary: refuse before any step, tool or agent side effect.
306+
validate(data.inputs, inputSchema, "input");
307+
return executeSteps(definition.steps(), 0, data, context);
308+
}
309309

310310
private String register(Continuation continuation) {
311311
String token = definition.id() + ":" + definition.version() + ":" + UUID.randomUUID();
@@ -330,10 +330,8 @@ private ExecutionResult executeSteps(List<WorkflowStep> steps, int index, RunDat
330330
if (result.suspension == null) {
331331
return executeSteps(steps, index + 1, data, context);
332332
}
333-
return result.continueWith((request, resumeContext) -> {
334-
ExecutionResult resumed = result.continuation.resume(request, resumeContext);
335-
return resumed.suspension == null ? executeSteps(steps, index + 1, data, resumeContext) : resumed;
336-
});
333+
return result.continueWith((resumed, request, resumeContext) ->
334+
resumed.suspension == null ? executeSteps(steps, index + 1, data, resumeContext) : resumed);
337335
}
338336

339337
private ExecutionResult executeStep(WorkflowStep step, RunData data, AgentExecutionContext context) {
@@ -363,8 +361,7 @@ private ExecutionResult executeStep(WorkflowStep step, RunData data, AgentExecut
363361
ExecutionResult result = executeStep(loop.body(), data, context);
364362
if (result.suspension != null) {
365363
int nextIteration = iteration + 1;
366-
return result.continueWith((request, resumeContext) -> {
367-
ExecutionResult resumed = result.continuation.resume(request, resumeContext);
364+
return result.continueWith((resumed, request, resumeContext) -> {
368365
if (resumed.suspension != null) {
369366
return resumed;
370367
}
@@ -705,6 +702,12 @@ private interface Continuation {
705702
ExecutionResult resume(AgentRequest request, AgentExecutionContext context);
706703
}
707704

705+
/** The work that continues after the wrapped continuation has resumed exactly once. */
706+
@FunctionalInterface
707+
private interface ContinuedStep {
708+
ExecutionResult after(ExecutionResult resumed, AgentRequest request, AgentExecutionContext context);
709+
}
710+
708711
private record SuspensionData(Map<String, Object> prompts) {
709712
}
710713

@@ -717,12 +720,13 @@ private static ExecutionResult suspended(RunData data, Map<String, Object> promp
717720
return new ExecutionResult(data, new SuspensionData(Map.copyOf(prompts)), continuation);
718721
}
719722

720-
private ExecutionResult continueWith(Continuation next) {
723+
private ExecutionResult continueWith(ContinuedStep next) {
721724
// The wrapper keeps the suspension-point snapshot: the suspended outcome is
722-
// built from this data before the continuation ever runs.
725+
// built from this data before the continuation ever runs. The continuation
726+
// itself runs exactly once; the step after the resume receives its result.
723727
return new ExecutionResult(data, suspension, (request, context) -> {
724-
ExecutionResult result = continuation.resume(request, context);
725-
return result.suspension == null ? next.resume(request, context) : result;
728+
ExecutionResult resumed = continuation.resume(request, context);
729+
return next.after(resumed, request, context);
726730
});
727731
}
728732
}

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,8 @@ void outputViolationFailsInsteadOfCompleting() {
163163

164164
@Test
165165
void resumeSkipsInputRevalidationButStillAcceptsValidOutputs() throws Exception {
166-
WorkflowAgent workflow = suspendingWorkflow("done");
166+
AtomicInteger resumedInvocations = new AtomicInteger();
167+
WorkflowAgent workflow = suspendingWorkflow("done", resumedInvocations);
167168
try (AgentExecutionContext first = context()) {
168169
AgentOutcome suspended = workflow
169170
.invoke(new AgentRequest(List.of(ChatMessage.user("hello")), Map.of("question", "why"), null),
@@ -178,13 +179,16 @@ void resumeSkipsInputRevalidationButStillAcceptsValidOutputs() throws Exception
178179
// The resume request carries no inputs at all: the schema was enforced
179180
// on the fresh run, the continuation owns the state afterwards.
180181
assertEquals(AgentOutcome.Status.COMPLETED, outcome.status());
182+
// The continuation must resume the agent exactly once: a second
183+
// invocation with the same token would be rejected by a real agent.
184+
assertEquals(1, resumedInvocations.get());
181185
}
182186
}
183187
}
184188

185189
@Test
186190
void resumeOutputViolationStillFails() throws Exception {
187-
WorkflowAgent workflow = suspendingWorkflow(42);
191+
WorkflowAgent workflow = suspendingWorkflow(42, new AtomicInteger());
188192
try (AgentExecutionContext first = context()) {
189193
AgentOutcome suspended = workflow
190194
.invoke(new AgentRequest(List.of(ChatMessage.user("hello")), Map.of("question", "why"), null),
@@ -227,9 +231,12 @@ void subflowSchemasAreValidatedAtPublishAndDoNotGateParentInputs() throws Except
227231
}
228232
}
229233

230-
private WorkflowAgent suspendingWorkflow(Object resumedValue) {
234+
private WorkflowAgent suspendingWorkflow(Object resumedValue, AtomicInteger resumedInvocations) {
231235
AtomicReference<Object> secondCallValue = new AtomicReference<>(resumedValue);
232236
Agent agent = agent(request -> {
237+
if (request.resumeToken() != null) {
238+
resumedInvocations.incrementAndGet();
239+
}
233240
if (request.resumeToken() == null) {
234241
return new AgentOutcome(AgentOutcome.Status.SUSPENDED, List.of(ChatMessage.assistant("need input")),
235242
Map.of(), new Usage(1L, 1L, 2L), new AgentOutcome.Suspension("agent-token",

0 commit comments

Comments
 (0)