Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 56 additions & 1 deletion core/src/main/java/com/google/adk/flows/llmflows/StepResume.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@
import com.google.adk.agents.Role;
import com.google.adk.events.Event;
import com.google.adk.tools.BaseTool;
import com.google.adk.workflow.NodeInfo;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.HashMultiset;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Multiset;
import com.google.common.collect.SetMultimap;
import com.google.common.collect.Sets;
import com.google.genai.types.Content;
import com.google.genai.types.FunctionCall;
import com.google.genai.types.FunctionResponse;
Expand All @@ -46,7 +49,8 @@
* Decides how a resumable flow's next step continues: carry on to the model, stop because a call is
* still unanswered, or run calls a previous run never executed.
*
* <p>Ports Python ADK's resume decision. Of the sub-branch cases Python recognizes, only a user's
* <p>Ports Python ADK's resume decision, except that a step does not wait for calls that failed
* workflow node attempts left unanswered. Of the sub-branch cases Python recognizes, only a user's
* answer on the branch a single-turn agent tool opened is handled, as in ADK Kotlin.
*/
final class StepResume {
Expand Down Expand Up @@ -100,6 +104,25 @@ static Decision decide(InvocationContext context, Map<String, BaseTool> tools) {
}
ImmutableList<Event> events =
context.events(/* currentInvocation= */ true, /* currentBranch= */ true);
Decision decision = decideFromEvents(context, tools, hasAgentTools, events);
if (decision.action != Action.PAUSE || context.nodePath().isEmpty()) {
return decision;
}
// Exclude failed attempts only when they would make the step pause.
ImmutableList<Event> withoutFailed =
withoutFailedAttempts(
context.events(/* currentInvocation= */ true, /* currentBranch= */ false), events);
return withoutFailed.size() == events.size()
? decision
: decideFromEvents(context, tools, hasAgentTools, withoutFailed);
}

/** Decides how the next step resumes from the branch's {@code events}. */
private static Decision decideFromEvents(
InvocationContext context,
Map<String, BaseTool> tools,
boolean hasAgentTools,
List<Event> events) {
if (hasAgentTools) {
Optional<Event> subBranchCall = callThatOpenedBranchOfLatestAnswer(context, events);
if (subBranchCall.isPresent()) {
Expand All @@ -126,7 +149,39 @@ static Decision decide(InvocationContext context, Map<String, BaseTool> tools) {
return CONTINUE;
}

/**
* Returns {@code branchEvents} without the events of failed workflow node attempts. Each node
* failure in {@code invocationEvents} removes the earlier events of that node and the nodes below
* it, because retrying the node runs them again.
*/
@VisibleForTesting
static ImmutableList<Event> withoutFailedAttempts(
List<Event> invocationEvents, List<Event> branchEvents) {
Set<String> failedPaths = new HashSet<>();
Set<Event> failedAttemptEvents = Sets.newIdentityHashSet();
for (int i = invocationEvents.size() - 1; i >= 0; i--) {
Event event = invocationEvents.get(i);
String path = event.nodeInfo().map(NodeInfo::path).orElse("");
boolean inFailedAttempt =
failedPaths.stream().anyMatch(failed -> isSameOrBelow(path, failed));
// An error without calls is a node failure, so error responses calling tools are kept.
boolean nodeFailure =
!path.isEmpty() && event.errorCode().isPresent() && event.functionCalls().isEmpty();
if (inFailedAttempt) {
failedAttemptEvents.add(event);
} else if (nodeFailure) {
failedPaths.add(path);
}
}
return branchEvents.stream()
.filter(event -> !failedAttemptEvents.contains(event))
.collect(toImmutableList());
}

/** Returns whether {@code path} is the node path {@code ancestor} or a path below it. */
private static boolean isSameOrBelow(String path, String ancestor) {
return path.equals(ancestor) || path.startsWith(ancestor + "/");
}

/**
* Returns the agent's call event to replay when a user's answer came back on the {@code
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@
import com.google.adk.sessions.InMemorySessionService;
import com.google.adk.sessions.Session;
import com.google.adk.tools.BaseTool;
import com.google.adk.workflow.NodeInfo;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.genai.types.Content;
import com.google.genai.types.FinishReason;
import com.google.genai.types.FunctionCall;
import com.google.genai.types.FunctionResponse;
import com.google.genai.types.Part;
Expand Down Expand Up @@ -228,6 +230,80 @@ public void decide_answerOnTheBranchOfACallAlreadyAnswered_continues() {
assertThat(decision.action).isEqualTo(StepResume.Action.CONTINUE);
}

@Test
public void withoutFailedAttempts_failureOfTheNode_dropsItsEarlierEvents() {
Event failedAttempt = nodeEvent("wf/worker", callPart("x", "c1")).build();
Event failure =
nodeEvent("wf/worker").errorCode(new FinishReason("IllegalStateException")).build();
Event retry = nodeEvent("wf/worker", Part.fromText("retrying")).build();
ImmutableList<Event> history = ImmutableList.of(failedAttempt, failure, retry);

ImmutableList<Event> events = StepResume.withoutFailedAttempts(history, history);

assertThat(events).containsExactly(failure, retry).inOrder();
}

@Test
public void withoutFailedAttempts_modelErrorResponseWithCalls_keepsEveryEvent() {
ImmutableList<Event> history =
ImmutableList.of(
nodeEvent("wf/worker", Part.fromText("calling")).build(),
nodeEvent("wf/worker", callPart("y", "c2"))
.errorCode(new FinishReason(FinishReason.Known.MAX_TOKENS))
.build());

ImmutableList<Event> events = StepResume.withoutFailedAttempts(history, history);

assertThat(events).isEqualTo(history);
}

@Test
public void withoutFailedAttempts_failureOfAnotherNode_dropsEarlierEventsOfThatNodeAndBelow() {
Event ownCall = nodeEvent("wf/worker", callPart("x", "c1")).build();
Event otherCall = nodeEvent("wf/other", callPart("y", "c2")).build();
Event innerCall = nodeEvent("wf/other/inner", callPart("z", "c3")).build();
Event otherFailure =
nodeEvent("wf/other").errorCode(new FinishReason("IllegalStateException")).build();
Event ownText = nodeEvent("wf/worker", Part.fromText("still waiting")).build();
ImmutableList<Event> history =
ImmutableList.of(ownCall, otherCall, innerCall, otherFailure, ownText);

ImmutableList<Event> events = StepResume.withoutFailedAttempts(history, history);

assertThat(events).containsExactly(ownCall, otherFailure, ownText).inOrder();
}

@Test
public void withoutFailedAttempts_failureOfAnEnclosingNodeOnAnotherBranch_dropsEarlierEvents() {
Event failedAttempt =
nodeEvent("wf/inner/worker", callPart("x", "c1")).branch("worker@1").build();
Event enclosingFailure =
nodeEvent("wf/inner").errorCode(new FinishReason("NodeTimeoutException")).build();
Event retry =
nodeEvent("wf/inner/worker", Part.fromText("retrying")).branch("worker@1").build();

ImmutableList<Event> events =
StepResume.withoutFailedAttempts(
ImmutableList.of(failedAttempt, enclosingFailure, retry),
ImmutableList.of(failedAttempt, retry));

assertThat(events).containsExactly(retry);
}

@Test
public void withoutFailedAttempts_failureOfANodeWhosePathIsAStringPrefix_keepsEveryEvent() {
ImmutableList<Event> history =
ImmutableList.of(
nodeEvent("wf@1/worker@10", callPart("x", "c1")).build(),
nodeEvent("wf@1/worker@1")
.errorCode(new FinishReason("IllegalStateException"))
.build());

ImmutableList<Event> events = StepResume.withoutFailedAttempts(history, history);

assertThat(events).isEqualTo(history);
}

private static LlmAgent coordinatorWithSingleTurnWorker() {
return LlmAgent.builder()
.name("coordinator")
Expand Down Expand Up @@ -307,6 +383,10 @@ private static Event event(String author, Part... parts) {
.build();
}

private static Event.Builder nodeEvent(String path, Part... parts) {
return event(AGENT, parts).toBuilder().nodeInfo(NodeInfo.builder().path(path).build());
}

private static FunctionCall call(String name, @Nullable String id) {
FunctionCall.Builder call = FunctionCall.builder().name(name);
if (id != null) {
Expand Down
Loading
Loading