-
Notifications
You must be signed in to change notification settings - Fork 303
Agent and Action Early Termination #1537
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
abfc320
Agent and Action Termination
igordayen 94c236a
Addressing Review Feedback
igordayen d44bbc2
Fixed KDOC comments and applied smart cast by 'check'
igordayen 7b16aab
Reviews feedback - next round:
igordayen 42b7f19
Reviews feedback - next round -committed:
igordayen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
embabel-agent-api/src/main/kotlin/com/embabel/agent/api/common/TerminationSignal.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| /* | ||
| * Copyright 2024-2026 Embabel Pty Ltd. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package com.embabel.agent.api.common | ||
|
|
||
| /** | ||
| * Scope of termination - whether to terminate the current action only | ||
| * or the entire agent process. | ||
| */ | ||
| enum class TerminationScope(val value: String) { | ||
| /** | ||
| * Terminate the entire agent process. | ||
| */ | ||
| AGENT("agent"), | ||
|
|
||
| /** | ||
| * Terminate the current action only, allowing agent to continue with next action. | ||
| */ | ||
| ACTION("action"), | ||
| } | ||
|
|
||
| /** | ||
| * Signal for graceful termination. When placed on the blackboard, | ||
| * the agent or action will terminate at the next natural checkpoint. | ||
| * | ||
| * For agent termination: checked before each tick() in AbstractAgentProcess. | ||
| * For action termination: checked between tool calls in the tool loop. | ||
| * | ||
| * @param scope Whether to terminate agent or action | ||
| * @param reason Human-readable explanation for termination | ||
| */ | ||
| data class TerminationSignal( | ||
| val scope: TerminationScope, | ||
| val reason: String, | ||
| ) |
85 changes: 85 additions & 0 deletions
85
embabel-agent-api/src/main/kotlin/com/embabel/agent/api/termination/TerminationExtensions.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| /* | ||
| * Copyright 2024-2026 Embabel Pty Ltd. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| @file:JvmName("Termination") | ||
|
|
||
| package com.embabel.agent.api.termination | ||
|
|
||
| import com.embabel.agent.api.common.TerminationScope | ||
| import com.embabel.agent.api.common.TerminationSignal | ||
| import com.embabel.agent.core.AgentProcess | ||
| import com.embabel.agent.core.EarlyTermination | ||
| import com.embabel.agent.core.EarlyTerminationPolicy | ||
| import com.embabel.agent.core.ProcessContext | ||
| import com.embabel.agent.core.support.AbstractAgentProcess | ||
|
|
||
| /** | ||
| * Request graceful termination of the entire agent process. | ||
| * The agent will terminate at the next natural checkpoint (before next tick). | ||
| * | ||
| * This works for all action types (LLM-based and simple transformations). | ||
| * For immediate termination without waiting for a checkpoint, use | ||
| * [com.embabel.agent.api.tool.TerminateAgentException]. | ||
| * | ||
| * @param reason Human-readable explanation for termination | ||
| * @see com.embabel.agent.api.tool.TerminateAgentException for immediate termination | ||
| */ | ||
| fun ProcessContext.terminateAgent(reason: String) { | ||
| (agentProcess as AbstractAgentProcess).setTerminationRequest( | ||
| TerminationSignal(TerminationScope.AGENT, reason) | ||
| ) | ||
| } | ||
|
|
||
| /** | ||
| * Request graceful termination of the current action only. | ||
| * The action will terminate at the next natural checkpoint (between tool calls), | ||
| * and the agent will continue with the next planned action. | ||
| * | ||
| * **Important:** This graceful termination mechanism only works for LLM-based actions | ||
| * that use a tool loop. For simple agents actions (non-LLM), use | ||
| * [com.embabel.agent.api.tool.TerminateActionException] instead: | ||
| * ``` | ||
| * throw TerminateActionException("reason") | ||
| * ``` | ||
| * | ||
| * @param reason Human-readable explanation for termination | ||
| * @see com.embabel.agent.api.tool.TerminateActionException for immediate termination | ||
| */ | ||
| fun ProcessContext.terminateAction(reason: String) { | ||
| (agentProcess as AbstractAgentProcess).setTerminationRequest( | ||
| TerminationSignal(TerminationScope.ACTION, reason) | ||
| ) | ||
| } | ||
|
|
||
| /** | ||
| * Early termination policy that checks for API-driven termination signals. | ||
| * Terminates the agent process when a [TerminationSignal] with [TerminationScope.AGENT] scope is found. | ||
| */ | ||
| internal object TerminationSignalPolicy : EarlyTerminationPolicy { | ||
| override val name: String = "TerminationSignal" | ||
|
|
||
| override fun shouldTerminate(agentProcess: AgentProcess): EarlyTermination? { | ||
| val process = agentProcess as? AbstractAgentProcess ?: return null | ||
| val signal = process.terminationRequest | ||
| return if (signal != null && signal.scope == TerminationScope.AGENT) { | ||
| EarlyTermination( | ||
| agentProcess = agentProcess, | ||
| error = false, | ||
| reason = signal.reason, | ||
| policy = this, | ||
| ) | ||
| } else null | ||
| } | ||
| } |
67 changes: 67 additions & 0 deletions
67
embabel-agent-api/src/main/kotlin/com/embabel/agent/api/tool/TerminationExceptions.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| /* | ||
| * Copyright 2024-2026 Embabel Pty Ltd. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package com.embabel.agent.api.tool | ||
|
|
||
| /** | ||
| * Exception thrown to immediately terminate the entire agent process. | ||
| * | ||
| * When thrown from a tool, this exception propagates through the tool loop | ||
| * and action executor, causing the agent process to terminate immediately. | ||
| * | ||
| * Use this for immediate termination. For graceful termination that waits | ||
| * for natural checkpoints, use [com.embabel.agent.api.common.TerminationSignal] | ||
| * via the ProcessContext API. | ||
| * | ||
| * Example usage: | ||
| * ```kotlin | ||
| * @LlmTool(description = "Stops the agent when critical error detected") | ||
| * fun criticalStop(reason: String): String { | ||
| * throw TerminateAgentException("Critical error: $reason") | ||
| * } | ||
| * ``` | ||
| * | ||
| * @param reason Human-readable explanation for termination | ||
| */ | ||
| class TerminateAgentException( | ||
| val reason: String, | ||
| ) : RuntimeException(reason), ToolControlFlowSignal | ||
|
|
||
| /** | ||
| * Exception thrown to immediately terminate the current action only. | ||
| * | ||
| * When thrown from an action or tool, this exception terminates the current | ||
| * action's tool loop but allows the agent to continue with the next planned action. | ||
| * | ||
| * Use this for immediate action termination. For graceful termination that | ||
| * waits for natural checkpoints, use [com.embabel.agent.api.common.TerminationSignal] | ||
| * via the ProcessContext API. | ||
| * | ||
| * Example usage: | ||
| * ```kotlin | ||
| * @Action | ||
| * fun processStep(context: ActionContext): String { | ||
| * if (shouldSkip()) { | ||
| * throw TerminateActionException("Skipping: condition met") | ||
| * } | ||
| * // ... normal processing | ||
| * } | ||
| * ``` | ||
| * | ||
| * @param reason Human-readable explanation for termination | ||
| */ | ||
| class TerminateActionException( | ||
| val reason: String, | ||
| ) : RuntimeException(reason), ToolControlFlowSignal | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.