Repository navigation
Sub-agents: fixes from the whole-branch review - #326
Merged
Merged
Conversation
On ADK, once a sub-agent run's stream has ended, _run_agent still awaits the child session's state to read back what the run changed. That read sat outside the handling of the run's own cancel, so a time limit or a Delegations.cancel landing during it raised CancelledError out of the hook, and the run ended cancelled with an empty result: its answer and its state changes were lost. RunControl gains finish(): the hook calls it as the run's work ends, and from then on a cancel of the run alone does nothing (Delegations.cancel returns False). ADK calls it right after its stream ends, before the state read; Deep Agents, which awaits nothing after its stream, calls it at the same point so the hook contract is one. The turn's own cancel (Ctrl+C) is not the control's and still stops the read. Tests: RunControl after finish ignores a stop and its time limit; a Delegations hook that finished keeps its answer and state; on ADK, a time limit or a parent's stop during the slowed state read leaves the run completed with its answer and the tasks it saved.
A sub-agent run waiting on the user lends its slot. Once the user had answered, it said running only after the take-back got a slot, so while another run worked in that slot its record stayed input_required: /agents showed "needs you", and the events box "Waiting for your answer", for as long as that other run took. SlotPool.lent gains on_wait (a defaulted keyword), called when the block's take-back has to wait for a slot. waiting_on_user passes it, so the run says queued then, and running once its slot is back; a take-back that gets a slot at once says running as before. A run says each working status once (_Working.say), and a question asked while an earlier one's take-back waits says input_required again, since the run waits on the user once more. Tests: the pool calls on_wait only when the take-back waits; the Delegations sequence input_required, queued, running (and input_required again for a second question); a contract scenario on both backends with one slot, the asker answered while the holder works in its slot. The two-prompts scenario now expects researcher-2 queued after its answer, as researcher-1 holds the slot then.
A failed envelope carries whatever text the run produced, but a run whose model call raised after it had written text ended with an empty result on both backends: the hooks let the provider's error out, and Delegations had no text to put in the envelope. RunFailed gains text (a defaulted keyword), which Delegations puts in the envelope's result and the run's end. The hooks now answer a failed model call through RunAnswer.error, with the provider's reason (failure_reason: the error's text, else its type's name) and the last text the run wrote, and raise a context overflow as RunFailed with that text: ADK from its processor's last answer, Deep Agents from its last finished step's messages. The warning is still "<instance> failed: <reason>"; as on the RunAnswer.error path before, it carries no error code, where a raised provider error gave its type's name. Tests: RunFailed's text reaches the envelope and the end event; contract scenarios on both backends: text, then a provider failure (failed, the provider's reason, the text as the result), and text, then an overflow.
A run's max_tokens is its input plus output tokens, and its usage record and its AGENT_RUN end count every call it makes. The limit counted only the agent's own model calls, so the side calls its tools made (a generate_simple summary such as web_fetch's) and Deep Agents' summaries of its conversation did not count: the limit was laxer than the usage the run reported. RunControl gains side_tokens (a defaulted keyword): what the run's other calls have used so far. Delegations passes one that reads the conversation's ledger: the run's rows that are no agent step (UsageLedger.by_purpose, which gains a run filter). The control still counts the run's own calls as each answer comes, so neither backend's order of recording a step's row matters and nothing is counted twice. The check stays after each model call, with the same rule for an answer with or without tool calls. ADK's plugin now checks after a call that reports no usage too, as Deep Agents' middleware did, so the run's side calls still count; such a call counts none of its own. Tests: by_purpose(run=); RunControl with side_tokens; Delegations counts a run's side call and summary rows, not its own step rows twice nor another run's; both backends' no-usage answer still counts side calls; a contract scenario on both backends: a tool's side call takes the run past max_tokens, and its next answer, which asks for a tool, is its last, cancelled by its limit.
validate_agent_graph's rule 10 gave reasons that no longer hold: Deep Agents' fork is gone, and ADK's task tool copies the caller's own session, not the main agent's. The rule stays, stated without them, and its two error messages and AgentConfig.inherit_context's docstring no longer name a version. INHERITED_CONTEXT_NOTE, which an inheriting run reads, said the conversation includes the call that handed it its task. ADK's copy keeps that call, but Deep Agents' leaves it out, so the note now says nothing of it. Smaller corrections: OpenCalls.answer and ADK's process() name the failed turn that keeps a call recorded; can_request_user_input says that a subclass asking through another channel overrides it too; Slot._back's comment says it may briefly name an entry already handed a slot; and SlotPool.lent says that a cancel that does not stop the run (an app tool's caught timeout around a question) gives its slot up for the rest of the run. The interrupted-calls tests' docstring names their failed-turn section. The graph tests pin the messages' guidance instead of their version; the note's test pins the sentence that is true on both backends.
CHANGELOG: the /agents and run-identity bullets list the cancelled end status; a sub-agent run fails on ADK for a reply that is no answer (blocked, or a finish reason other than stop with nothing in it), while a run that wrote no text completes; an inheriting run's conversation keeps the calling task message on ADK and leaves it out on Deep Agents; the inherit rule names no version; and, in the ADK delegation bullet and the migration notes, a sub-agent run shares only the plan, the tasks and the outside-content mark with its caller: any other session-state key an ADK tool writes there, user: and app: keys included, never reaches the conversation. README: several sub-agent runs work at once (max_active, four by default) rather than two; the same ADK state note; the rule section names no version; and the slots text says that a tool's timeout around a question gives its run's slot up for the rest of the run. CLAUDE.md: the inherit rule names no version, the inherited conversation's calling message is said per backend, and the slots paragraph says that a prompt no one can be shown lends nothing.
Since a failed model call came back through RunAnswer.error, the run's warning had no error code, where a raised provider error had given its type's name (the CLI showed it as " [BadRequestError]"). ADK's reply that is no answer had always lost its code on that path too. RunAnswer gains error_code (a defaulted field, after the others), which Delegations gives the run's recoverable ERROR. Both hooks pass the provider error's type name for a failed model call, and ADK passes a reply's no-answer code (MALFORMED_FUNCTION_CALL, MAX_TOKENS, ...). Tests: the contract's provider-failure scenarios check the warning's code, BadRequestError, on both backends; Delegations warns with the code an answer gives and with none without; on ADK a sub-agent's reply that is no answer warns with its code.
The re-review found five places that said a little more than the code does: - CHANGELOG: on ADK a sub-agent run fails for a reply with nothing in it only when its reason is none of stop, a refusal or the output limit (a malformed function call, say); a refused or blocked reply ends it refused, and an empty one cut at the output limit completed, partial. - README: only a wait on the user reports queued while the run takes its slot back; a wait on its children does not. - SlotPool.lent, README and CLAUDE.md: a run whose question a caught timeout ended says input_required until its next question is answered (then running, still without a slot), not always until it ends. - runs.py: RUN_QUEUED's comment says an answered run may wait queued to take its slot back. - The no-usage test in both backends' run-control tests is named for what it checks now: such a call counts none of its own tokens. No behaviour changes.
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.
Fixes from the whole-branch review of the sub-agent delegation work (
Delegations,RunControl,SlotPool, both backends).Delegations.cancel) that landed while the run read its shared state back, after its stream had ended, discarded the completed answer and its state changes.RunControl.finish()now marks a run done once its stream ends, and a later cancel does nothing. A turn's cancel still wins.queued, thenrunning. Before, it stayedinput_required, shown as "needs you".SlotPool.lent(on_wait=)is new.RunFailed(..., text=)andRunAnswer.error_codeare new and defaulted. A failed run's warning keeps the provider error's type ([BadRequestError]), and on ADK a no-answer reply passes its code.max_tokenscounts what the run's usage counts. This now includes the side calls its tools make and Deep Agents' summaries. A finishing answer still completes its run. A model that reports no usage counts none of its own tokens.validate_agent_graph's rule 10 states no stale reason and names no version.INHERITED_CONTEXT_NOTEsays only what holds on both backends.cancelledis listed among the end statuses.Tests: new tests were written first and seen failing. Contract scenarios on both backends cover:
input_required→queued→running;max_tokens;The full offline suite passes in the worktree, except
TestChildImportOrigin, which fails the same way on develop under the worktree wrapper.