Skip to content

Commit 41aa250

Browse files
Merge pull request #1570 from Laurianti:fix-loop-agent-non-positive-max-iterations
PiperOrigin-RevId: 1005378471
2 parents 9a15855 + 5feaae4 commit 41aa250

2 files changed

Lines changed: 70 additions & 0 deletions

File tree

‎core/src/main/java/com/google/adk/agents/LoopAgent.java‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ private LoopAgent(
9191
public static class Builder extends BaseAgent.Builder<Builder> {
9292
private @Nullable Integer maxIterations;
9393

94+
/**
95+
* Sets the maximum number of loop iterations. If not set, the loop runs until a sub-agent
96+
* escalates. A value of zero or less runs the sub-agents no times at all.
97+
*/
9498
@CanIgnoreReturnValue
9599
public Builder maxIterations(@Nullable Integer maxIterations) {
96100
this.maxIterations = maxIterations;
@@ -145,6 +149,12 @@ protected Flowable<Event> runAsyncImpl(InvocationContext invocationContext) {
145149
return Flowable.empty();
146150
}
147151

152+
// Flowable.repeat rejects a negative count, and the legacy flow checks the cap only after a
153+
// pass. The resumable path checks it first and records end-of-agent, so it is left to it.
154+
if (!invocationContext.isResumable() && maxIterations != null && maxIterations <= 0) {
155+
return Flowable.empty();
156+
}
157+
148158
if (invocationContext.isLegacyResumability()) {
149159
return runAsyncLegacyResumption(invocationContext, subAgents);
150160
}

‎core/src/test/java/com/google/adk/agents/LoopAgentTest.java‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import static org.junit.Assert.assertThrows;
2727

2828
import com.google.adk.agents.Callbacks.BeforeAgentCallback;
29+
import com.google.adk.apps.ResumabilityConfig;
2930
import com.google.adk.events.Event;
3031
import com.google.adk.testing.TestBaseAgent;
3132
import com.google.common.collect.ImmutableList;
@@ -381,4 +382,63 @@ public void runAsync_resumable_pausesOnLongRunningCall_doesNotEmitEndOfAgent() {
381382
assertThat(events.stream().anyMatch(event -> event.actions().endOfAgent())).isFalse();
382383
assertThat(events.stream().filter(event -> event.author().equals("sub")).count()).isEqualTo(1L);
383384
}
385+
386+
@Test
387+
public void runAsync_nonPositiveMaxIterations_notResumable_runsNoIterations() {
388+
for (int maxIterations : new int[] {0, -1}) {
389+
LoopAgent loopAgent = createLoopAgent(maxIterations);
390+
391+
List<Event> events =
392+
loopAgent.runAsync(createInvocationContext(loopAgent)).toList().blockingGet();
393+
394+
assertThat(events).isEmpty();
395+
}
396+
}
397+
398+
@Test
399+
public void runAsync_nonPositiveMaxIterations_legacyResumption_runsNoIterations() {
400+
for (int maxIterations : new int[] {0, -1}) {
401+
LoopAgent loopAgent = createLoopAgent(maxIterations);
402+
403+
List<Event> events =
404+
loopAgent
405+
.runAsync(createLegacyResumableInvocationContext(loopAgent))
406+
.toList()
407+
.blockingGet();
408+
409+
assertThat(events).isEmpty();
410+
}
411+
}
412+
413+
@Test
414+
public void runAsync_nonPositiveMaxIterations_resumable_onlyEmitsEndOfAgent() {
415+
for (int maxIterations : new int[] {0, -1}) {
416+
LoopAgent loopAgent = createLoopAgent(maxIterations);
417+
418+
List<Event> events =
419+
loopAgent.runAsync(createResumableInvocationContext(loopAgent)).toList().blockingGet();
420+
421+
assertThat(events).hasSize(1);
422+
assertThat(events.get(0).author()).isEqualTo("loop");
423+
assertThat(events.get(0).actions().endOfAgent()).isTrue();
424+
}
425+
}
426+
427+
private static LoopAgent createLoopAgent(int maxIterations) {
428+
TestBaseAgent subAgent =
429+
createSubAgent("sub", createEvent("e").toBuilder().author("sub").build());
430+
return LoopAgent.builder()
431+
.name("loop")
432+
.subAgents(subAgent)
433+
.maxIterations(maxIterations)
434+
.build();
435+
}
436+
437+
@SuppressWarnings("deprecation") // The legacy resumption flow is deprecated by design.
438+
private static InvocationContext createLegacyResumableInvocationContext(LoopAgent loopAgent) {
439+
return createInvocationContext(loopAgent).toBuilder()
440+
.resumabilityConfig(
441+
ResumabilityConfig.builder().plainTextContinuationAutoResume(true).build())
442+
.build();
443+
}
384444
}

0 commit comments

Comments
 (0)