fix(agents): refuse a second inline AgentTask on the same activity - #6860
Open
u9g wants to merge 9 commits into
Open
fix(agents): refuse a second inline AgentTask on the same activity#6860u9g wants to merge 9 commits into
u9g wants to merge 9 commits into
Conversation
…nish callback Hoists the context-var reads and the already-interrupted check above the _handle_task_done definition in AgentTask.__await_impl, so every path that rejects the await outright runs before the callback is registered. The callback exists to complete the task when its asyncio.Task finishes first. An early raise was tripping it, reporting a task that never started as having finished prematurely.
An LLM turn with parallel tool calls can await an AgentTask from each call. Every one of them pauses the same activity and hands off, but only the last handoff survives: the losers are never active again, so nothing completes them. Each stays parked on its result, its function call never returns, the speech never finishes, and the session hangs until close times out - reported by the caller as the agent going silent. The activity now records the inline task that paused it, and a second one awaited while it is still in flight raises a ToolError instead. The returned AgentTask path already refuses a turn that yields more than one; this is the same rule for the awaited path. A nested AgentTask pauses a different activity, so the existing nesting flow is untouched. Replaces the TODO asking for a global lock. A lock covering the task's whole lifetime would deadlock that nesting: an outer task holds it while awaiting an inner one, whose own tool needs it to make progress.
agent_activity imports Agent at module scope, so hoisting this one fails on either entry point with a partially-initialized-module ImportError.
…e mode Both comments around the one-paused-inline-task rule spent most of their lines tracing what goes wrong when it is violated. Keeps the invariant and the placement constraints, drops the walkthrough.
The reason the registration sits below the raising checks is in the commit that moved it.
The field declaration already states the lifetime this repeated.
CancelledError derives from BaseException, so an except Exception handler let a cancellation during the pause escape with the inline-task claim still held. The activity then refuses every later inline task for the rest of the session. Reachable through a forced interrupt, which bypasses the disallowed interruptions the claim is held under and cancels the tool task. The same handler's pre-existing __inactive_ev release was escaping too.
The check and its error message carry the rule.
u9g
force-pushed
the
jason/inline-agent-task-concurrency-guard
branch
from
August 14, 2026 21:02
a596f06 to
647108e
Compare
u9g
added a commit
that referenced
this pull request
Aug 14, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
An LLM turn with parallel tool calls can await an
AgentTaskfrom more than one call; each pauses the same activity, only the last handoff survives, and the losers are never active again so nothing completes them — their function calls never return and the session hangs until close times out.AgentActivitynow records the inline task that paused it, and a second one awaited while the first is still in flight raisesToolError, which is the rule the returned-AgentTaskpath already enforces viaexpected to receive only one AgentTask from the tool executions. The replaced TODO asked for a global lock, but one held across the task's lifetime would deadlock the supported nesting pattern, where an outer task holds it while awaiting an inner one whose own tool needs it to progress.