feat: add termination conditions for multi-agent workflows#193
feat: add termination conditions for multi-agent workflows#193davidmonterocrespo24 wants to merge 5 commits intogoogle:mainfrom
Conversation
Introduces a TerminationCondition system that lets developers control
when LoopAgent loops and Runner invocations stop.
Built-in conditions:
- MaxIterationsTermination: stops after N events
- TextMentionTermination: stops when a keyword appears (e.g. 'TERMINATE')
- TokenUsageTermination: stops when prompt/completion/total token limits exceeded
- TimeoutTermination: stops after a wall-clock duration
- FunctionCallTermination: stops when a named tool is executed
- ExternalTermination: programmatic stop via .set() (e.g. UI stop button)
Conditions compose with .and() / .or() for combined logic:
new MaxIterationsTermination(10).or(new TextMentionTermination('DONE'))
Integration:
- LoopAgentConfig.terminationCondition: checked after every sub-agent event
- Runner.runAsync / runEphemeral support terminationCondition param
- EventActions gains terminationReason field
- All types exported from common.ts / @google/adk
- Full test coverage in core/test/termination/
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces a robust and flexible termination condition system for multi-agent workflows. It empowers developers to precisely define when agent loops and runner invocations should cease, preventing unintended infinite execution and providing clear reasons for termination. This system offers a variety of pre-built conditions and the ability to combine them, significantly enhancing control and predictability in agent-based applications. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces a comprehensive and well-designed TerminationCondition system, allowing for fine-grained control over when agent loops and runner invocations stop. The implementation is clean, includes a variety of useful built-in conditions, and supports composition through .and() and .or() operators. The integration into LoopAgent and Runner is thoughtfully done, and the addition of thorough unit tests is commendable. I have one minor suggestion for a potential performance improvement.
# Conflicts: # core/src/agents/loop_agent.ts
7759b76 to
2c7e325
Compare
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Hi @kalenkevich , if I do it for Python too, wouldn't it be aligned? Or would something else be missing? |
Introduces a TerminationCondition system that lets developers control when LoopAgent loops and Runner invocations stop.
Built-in conditions:
Conditions compose with .and() / .or() for combined logic:
new MaxIterationsTermination(10).or(new TextMentionTermination('DONE'))
Integration:
Closes #29
Summary
Implements a
TerminationConditionsystem that gives developers fine-grainedcontrol over when
LoopAgentloops andRunnerinvocations stop, alignedwith the Python and Java ADK equivalents.
Built-in conditions
MaxIterationsTerminationTextMentionTermination'TERMINATE')TokenUsageTerminationTimeoutTerminationFunctionCallTerminationExternalTermination.set()is called programmatically (e.g. UI stop button)Composition
Conditions compose with .and() / .or() for combined logic:
new MaxIterationsTermination(10).or(new TextMentionTermination('DONE'))
Changes
New core/src/termination/ module with all built-in conditions and base class
LoopAgentConfig.terminationCondition — checked after every sub-agent event
Runner.runAsync / runEphemeral — accept optional terminationCondition param
EventActions.terminationReason — surfaces the stop reason in the final event
All types exported from common.ts / @google/adk
Full unit test coverage in core/test/termination/
Testing
npm test
All existing tests pass. New tests cover each condition individually, edge cases (already-terminated state, reset behavior), and .and() / .or() composition.