Skip to content

Commit a2d57a4

Browse files
committed
fix(agents): initialize branch in BaseAgent for proper propagation
- Ensure BaseAgent.createInvocationContext initializes the branch if empty. - Clean up ParallelAgent by removing unused setBranchForCurrentAgent method. - Fix NoSuchElementException in ParallelAgentTest.
1 parent 97cfac1 commit a2d57a4

2 files changed

Lines changed: 6 additions & 26 deletions

File tree

core/src/main/java/com/google/adk/agents/BaseAgent.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -285,10 +285,12 @@ private InvocationContext createInvocationContext(InvocationContext parentContex
285285
InvocationContext.Builder builder = parentContext.toBuilder();
286286
builder.agent(this);
287287
// Check for branch to be truthy (not None, not empty string),
288-
parentContext
289-
.branch()
290-
.filter(s -> !s.isEmpty())
291-
.ifPresent(branch -> builder.branch(branch + "." + name()));
288+
String parentBranch = parentContext.branch().filter(s -> !s.isEmpty()).orElse(null);
289+
if (parentBranch == null) {
290+
builder.branch(name());
291+
} else {
292+
builder.branch(parentBranch + "." + name());
293+
}
292294
return builder.build();
293295
}
294296

core/src/main/java/com/google/adk/agents/ParallelAgent.java

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@
1515
*/
1616
package com.google.adk.agents;
1717

18-
import static com.google.common.base.Strings.isNullOrEmpty;
19-
import static com.google.common.collect.ImmutableList.toImmutableList;
20-
2118
import com.google.adk.agents.ConfigAgentUtils.ConfigurationException;
2219
import com.google.adk.events.Event;
2320
import io.reactivex.rxjava3.core.Flowable;
@@ -108,25 +105,6 @@ public static ParallelAgent fromConfig(ParallelAgentConfig config, String config
108105
return agent;
109106
}
110107

111-
/**
112-
* Sets the branch for the current agent in the invocation context.
113-
*
114-
* <p>Appends the agent name to the current branch, or sets it if undefined.
115-
*
116-
* @param currentAgent Current agent.
117-
* @param invocationContext Invocation context to update.
118-
* @return A new invocation context with branch set.
119-
*/
120-
private static InvocationContext setBranchForCurrentAgent(
121-
BaseAgent currentAgent, InvocationContext invocationContext) {
122-
String branch = invocationContext.branch().orElse(null);
123-
if (isNullOrEmpty(branch)) {
124-
return invocationContext.toBuilder().branch(currentAgent.name()).build();
125-
} else {
126-
return invocationContext.toBuilder().branch(branch + "." + currentAgent.name()).build();
127-
}
128-
}
129-
130108
/**
131109
* Runs sub-agents in parallel and emits their events.
132110
*

0 commit comments

Comments
 (0)