Skip to content

Commit d1cc634

Browse files
committed
fix(runner): keep callbackContextData on a resumed invocation
1 parent 84bae34 commit d1cc634

2 files changed

Lines changed: 238 additions & 9 deletions

File tree

‎core/src/main/java/com/google/adk/runner/Runner.java‎

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,10 +1118,10 @@ private Flowable<Event> resumeCore(
11181118
.flatMapPublisher(
11191119
userEvent ->
11201120
runResumedAgent(
1121+
initialContext,
11211122
session,
11221123
resolvedInvocationId,
1123-
userEvent.content().orElse(null),
1124-
runConfig));
1124+
userEvent.content().orElse(null)));
11251125
} else if (delta != null && !delta.isEmpty()) {
11261126
// No message to carry the delta, so persist a content-less event, as Python does.
11271127
Event stateDeltaEvent =
@@ -1139,10 +1139,14 @@ private Flowable<Event> resumeCore(
11391139
.ignoreElement()
11401140
.andThen(
11411141
runResumedAgent(
1142-
session, resolvedInvocationId, /* userContent= */ null, runConfig));
1142+
initialContext,
1143+
session,
1144+
resolvedInvocationId,
1145+
/* userContent= */ null));
11431146
} else {
11441147
events =
1145-
runResumedAgent(session, resolvedInvocationId, /* userContent= */ null, runConfig);
1148+
runResumedAgent(
1149+
initialContext, session, resolvedInvocationId, /* userContent= */ null);
11461150
}
11471151

11481152
return events
@@ -1155,19 +1159,26 @@ private Flowable<Event> resumeCore(
11551159
* Runs the resolved agent for a resumed invocation with the same before-run / after-run plugin
11561160
* bracket as the new-invocation path. Rehydrates checkpoints, skips a completed invocation, and
11571161
* persists each event.
1162+
*
1163+
* @param initialContext the context created for the resumed invocation's pre-run callbacks, used
1164+
* to preserve metadata and callback data.
1165+
* @param session the session the invocation runs in.
1166+
* @param resolvedInvocationId the id of the invocation being resumed.
1167+
* @param userContent the message that resumed the invocation, or {@code null} to restore its
1168+
* original user message.
1169+
* @return a stream of events from the resumed run and its plugin callbacks.
11581170
*/
11591171
private Flowable<Event> runResumedAgent(
1172+
InvocationContext initialContext,
11601173
Session session,
11611174
String resolvedInvocationId,
1162-
@Nullable Content userContent,
1163-
RunConfig runConfig) {
1175+
@Nullable Content userContent) {
11641176
return Flowable.defer(
11651177
() -> {
11661178
BaseAgent rootAgent = this.agent;
11671179
InvocationContext rootContext =
1168-
newInvocationContextBuilder(session, rootAgent)
1169-
.invocationId(resolvedInvocationId)
1170-
.runConfig(runConfig)
1180+
initialContext.toBuilder()
1181+
.agent(rootAgent)
11711182
.userContent(userContentFor(session, resolvedInvocationId, userContent))
11721183
.build();
11731184
// Rehydrate before routing, as Python does, so a root that checkpointed re-enters itself.

‎core/src/test/java/com/google/adk/runner/RunnerResumabilityTest.java‎

Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@
4949
import static org.mockito.Mockito.verify;
5050
import static org.mockito.Mockito.when;
5151

52+
import com.google.adk.agents.BaseAgent;
53+
import com.google.adk.agents.CallbackContext;
5254
import com.google.adk.agents.InvocationContext;
5355
import com.google.adk.agents.LlmAgent;
5456
import com.google.adk.agents.LoopAgent;
@@ -77,10 +79,13 @@
7779
import com.google.genai.types.Part;
7880
import io.opentelemetry.api.trace.Tracer;
7981
import io.opentelemetry.sdk.testing.junit4.OpenTelemetryRule;
82+
import io.reactivex.rxjava3.core.Completable;
8083
import io.reactivex.rxjava3.core.Flowable;
8184
import io.reactivex.rxjava3.core.Maybe;
8285
import java.time.Instant;
86+
import java.util.ArrayList;
8387
import java.util.List;
88+
import java.util.Map;
8489
import java.util.Objects;
8590
import java.util.Optional;
8691
import java.util.concurrent.atomic.AtomicInteger;
@@ -486,6 +491,219 @@ public void runAsync_resume_runsFullPluginBracket() {
486491
verify(resumePlugin, atLeastOnce()).onEventCallback(any(), any());
487492
}
488493

494+
// A resume hands beforeRunCallback and the agent the callback data stored in
495+
// onUserMessageCallback, as a new invocation does.
496+
@Test
497+
public void runAsync_resume_keepsCallbackContextDataFromOnUserMessage() {
498+
CallbackDataRelayPlugin plugin = new CallbackDataRelayPlugin();
499+
TestLlm testLlm =
500+
createTestLlm(
501+
createFunctionCallLlmResponse(
502+
"lro_call_id", "pendingTool", ImmutableMap.of("message", "hello")),
503+
createTextLlmResponse("resumed and summarized"));
504+
LlmAgent agent =
505+
createTestAgentBuilder(testLlm).name("agent").tools(pendingFunctionTool()).build();
506+
Runner runner = resumableRunner(agent, plugin);
507+
Session session = newSession(runner);
508+
509+
runTurn(runner, session, "start");
510+
answerCall(runner, session, "lro_call_id", "pendingTool", ImmutableMap.of("message", "hello"));
511+
512+
// First the new invocation, then the resume, whose callbacks used to see an empty map.
513+
assertThat(plugin.seenBeforeRun).containsExactly("message-1", "message-2").inOrder();
514+
assertThat(plugin.seenBeforeAgent)
515+
.containsExactly("agent:message-1", "agent:message-2")
516+
.inOrder();
517+
}
518+
519+
// A resume routed past the root to the sub-agent that paused hands it the same callback data.
520+
@Test
521+
public void runAsync_resumeRoutedToSubAgent_keepsCallbackContextDataFromOnUserMessage() {
522+
CallbackDataRelayPlugin plugin = new CallbackDataRelayPlugin();
523+
Content transferCall =
524+
Content.fromParts(
525+
Part.fromFunctionCall(
526+
"transfer_to_agent", ImmutableMap.of("agent_name", "sub_agent_1")));
527+
TestLlm testLlm =
528+
createTestLlm(
529+
createLlmResponse(transferCall),
530+
createTextLlmResponse("sub-agent answer"),
531+
createFunctionCallLlmResponse(
532+
"lro_call_id", "pendingTool", ImmutableMap.of("message", "hello")),
533+
createTextLlmResponse("resumed answer"));
534+
LlmAgent subAgent1 =
535+
createTestAgentBuilder(testLlm).name("sub_agent_1").tools(pendingFunctionTool()).build();
536+
LlmAgent rootAgent =
537+
createTestAgentBuilder(testLlm)
538+
.name("root_agent")
539+
.subAgents(ImmutableList.of(subAgent1))
540+
.build();
541+
Runner runner = resumableRunner(rootAgent, plugin);
542+
Session session = newSession(runner);
543+
544+
// The root transfers and the sub-agent answers; the next message goes to the sub-agent alone,
545+
// which pauses, so the resume routes to it rather than re-entering the root.
546+
runTurn(runner, session, "hi");
547+
runTurn(runner, session, "start the job");
548+
answerCall(runner, session, "lro_call_id", "pendingTool", ImmutableMap.of("message", "hello"));
549+
550+
assertThat(plugin.seenBeforeAgent)
551+
.containsExactly(
552+
"root_agent:message-1",
553+
"sub_agent_1:message-1",
554+
"sub_agent_1:message-2",
555+
"sub_agent_1:message-3")
556+
.inOrder();
557+
}
558+
559+
// A plugin that ends the invocation in onUserMessageCallback ends a resume too, as a new
560+
// invocation.
561+
@Test
562+
public void runAsync_resume_endInvocationFromOnUserMessage_skipsTheModel() {
563+
TestLlm testLlm =
564+
createTestLlm(
565+
createFunctionCallLlmResponse(
566+
"lro_call_id", "pendingTool", ImmutableMap.of("message", "hello")),
567+
createTextLlmResponse("should not be reached"));
568+
LlmAgent agent =
569+
createTestAgentBuilder(testLlm).name("agent").tools(pendingFunctionTool()).build();
570+
Runner runner = resumableRunner(agent, new EndOnAnswerPlugin());
571+
Session session = newSession(runner);
572+
573+
runTurn(runner, session, "start");
574+
assertThat(testLlm.getRequests()).hasSize(1);
575+
576+
answerCall(runner, session, "lro_call_id", "pendingTool", ImmutableMap.of("message", "hello"));
577+
578+
// The plugin ended the resumed invocation, so the model is not asked again.
579+
assertThat(testLlm.getRequests()).hasSize(1);
580+
}
581+
582+
// A resume without a new message hands onRunErrorCallback the callback data stored in
583+
// beforeRunCallback, as a new invocation does.
584+
@Test
585+
public void runAsync_resumeWithoutMessage_onRunErrorSeesBeforeRunData() {
586+
assertThat(onRunErrorReadsOnFailedResume(/* stateDelta= */ null)).containsExactly("run-2");
587+
}
588+
589+
// The same with only a state delta, which the resume stores as a content-less event first.
590+
@Test
591+
public void runAsync_resumeWithStateDeltaOnly_onRunErrorSeesBeforeRunData() {
592+
assertThat(onRunErrorReadsOnFailedResume(ImmutableMap.<String, Object>of("key", "value")))
593+
.containsExactly("run-2");
594+
}
595+
596+
/** What onRunErrorCallback reads when a paused invocation resumes without a message and fails. */
597+
private static ImmutableList<String> onRunErrorReadsOnFailedResume(
598+
@Nullable Map<String, Object> stateDelta) {
599+
FailAfterFirstRunPlugin plugin = new FailAfterFirstRunPlugin();
600+
TestLlm testLlm =
601+
createTestLlm(
602+
createFunctionCallLlmResponse(
603+
"lro_call_id", "pendingTool", ImmutableMap.of("message", "hello")));
604+
LlmAgent agent =
605+
createTestAgentBuilder(testLlm).name("agent").tools(pendingFunctionTool()).build();
606+
Runner runner = resumableRunner(agent, plugin);
607+
Session session = newSession(runner);
608+
String invocationId = runTurn(runner, session, "start").get(0).invocationId();
609+
610+
resumeFlowable(runner, session, invocationId, /* newMessage= */ null, stateDelta)
611+
.test()
612+
.assertError(IllegalStateException.class);
613+
614+
return ImmutableList.copyOf(plugin.seenOnRunError);
615+
}
616+
617+
/**
618+
* Stores a numbered value before each run, fails every run after the first, and records what
619+
* onRunErrorCallback reads back.
620+
*/
621+
private static final class FailAfterFirstRunPlugin extends BasePlugin {
622+
private static final String KEY = "run";
623+
private static final String VALUE = "run-%d";
624+
625+
private final AtomicInteger runs = new AtomicInteger();
626+
private final List<String> seenOnRunError = new ArrayList<>();
627+
628+
FailAfterFirstRunPlugin() {
629+
super("fail-after-first-run");
630+
}
631+
632+
@Override
633+
public Maybe<Content> beforeRunCallback(InvocationContext invocationContext) {
634+
int run = runs.incrementAndGet();
635+
invocationContext.callbackContextData().put(KEY, VALUE.formatted(run));
636+
return run == 1 ? Maybe.empty() : Maybe.error(new IllegalStateException("run failed"));
637+
}
638+
639+
@Override
640+
public Completable onRunErrorCallback(InvocationContext invocationContext, Throwable error) {
641+
seenOnRunError.add(String.valueOf(invocationContext.callbackContextData().get(KEY)));
642+
return Completable.complete();
643+
}
644+
}
645+
646+
/** Ends the invocation when the message answers a call, so the pausing turn runs normally. */
647+
private static final class EndOnAnswerPlugin extends BasePlugin {
648+
649+
EndOnAnswerPlugin() {
650+
super("end-on-answer");
651+
}
652+
653+
@Override
654+
public Maybe<Content> onUserMessageCallback(
655+
InvocationContext invocationContext, Content userMessage) {
656+
if (answersACall(userMessage)) {
657+
invocationContext.setEndInvocation(true);
658+
}
659+
return Maybe.empty();
660+
}
661+
662+
private static boolean answersACall(Content message) {
663+
return message.parts().stream()
664+
.flatMap(List::stream)
665+
.anyMatch(part -> part.functionResponse().isPresent());
666+
}
667+
}
668+
669+
/** Stores a numbered value for each user message and records what later callbacks read back. */
670+
private static final class CallbackDataRelayPlugin extends BasePlugin {
671+
private static final String KEY = "relay";
672+
private static final String VALUE = "message-%d";
673+
private static final String SEEN = "%s:%s";
674+
675+
private final AtomicInteger userMessages = new AtomicInteger();
676+
private final List<String> seenBeforeRun = new ArrayList<>();
677+
private final List<String> seenBeforeAgent = new ArrayList<>();
678+
679+
CallbackDataRelayPlugin() {
680+
super("callback-data-relay");
681+
}
682+
683+
@Override
684+
public Maybe<Content> onUserMessageCallback(
685+
InvocationContext invocationContext, Content userMessage) {
686+
invocationContext
687+
.callbackContextData()
688+
.put(KEY, VALUE.formatted(userMessages.incrementAndGet()));
689+
return Maybe.empty();
690+
}
691+
692+
@Override
693+
public Maybe<Content> beforeRunCallback(InvocationContext invocationContext) {
694+
seenBeforeRun.add(String.valueOf(invocationContext.callbackContextData().get(KEY)));
695+
return Maybe.empty();
696+
}
697+
698+
@Override
699+
public Maybe<Content> beforeAgentCallback(BaseAgent agent, CallbackContext callbackContext) {
700+
seenBeforeAgent.add(
701+
SEEN.formatted(
702+
agent.name(), callbackContext.invocationContext().callbackContextData().get(KEY)));
703+
return Maybe.empty();
704+
}
705+
}
706+
489707
// An unanswered call keeps the invocation paused: plain text and no message are both inert.
490708
@Test
491709
public void resume_pausedCallUnanswered_staysPausedForPlainTextAndNoMessage() {

0 commit comments

Comments
 (0)