Skip to content

Commit d432c64

Browse files
google-genai-botcopybara-github
authored andcommitted
fix: Making stepsCompleted thread-safe
PiperOrigin-RevId: 853058219
1 parent e01df11 commit d432c64

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

core/src/main/java/com/google/adk/flows/llmflows/BaseLlmFlow.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ public abstract class BaseLlmFlow implements BaseFlow {
6868

6969
// Warning: This is local, in-process state that won't be preserved if the runtime is restarted.
7070
// "Max steps" is experimental and may evolve in the future (e.g., to support persistence).
71-
protected int stepsCompleted = 0;
7271
protected final int maxSteps;
7372

7473
public BaseLlmFlow(
@@ -393,8 +392,12 @@ private Flowable<Event> runOneStep(InvocationContext context) {
393392
*/
394393
@Override
395394
public Flowable<Event> run(InvocationContext invocationContext) {
395+
return run(invocationContext, 0);
396+
}
397+
398+
private Flowable<Event> run(InvocationContext invocationContext, int stepsCompleted) {
396399
Flowable<Event> currentStepEvents = runOneStep(invocationContext).cache();
397-
if (++stepsCompleted >= maxSteps) {
400+
if (stepsCompleted + 1 >= maxSteps) {
398401
logger.debug("Ending flow execution because max steps reached.");
399402
return currentStepEvents;
400403
}
@@ -413,7 +416,7 @@ public Flowable<Event> run(InvocationContext invocationContext) {
413416
return Flowable.empty();
414417
} else {
415418
logger.debug("Continuing to next step of the flow.");
416-
return Flowable.defer(() -> run(invocationContext));
419+
return Flowable.defer(() -> run(invocationContext, stepsCompleted + 1));
417420
}
418421
}));
419422
}

0 commit comments

Comments
 (0)