Skip to content

fix(workflow): answer a failed task delegation with the error - #8245

Open
burrows99 wants to merge 2 commits into
google:mainfrom
burrows99:fix/task-delegation-error-response
Open

burrows99 wants to merge 2 commits into
google:mainfrom
burrows99:fix/task-delegation-error-response

Conversation

@burrows99

Copy link
Copy Markdown

Please ensure you have read the contribution guide before creating a pull request.

Link to Issue or Description of Change

1. Link to an existing issue (if applicable):

Problem:

When a task-mode sub-agent raised (for example its model call failed), _dispatch_task_fc let the child's DynamicNodeFailError escape. The parent's turn failed, and the delegation's function call stayed in the session with no response. _find_unresolved_task_delegations then dispatched that call again at the start of every later turn, so every later message in the session failed the same way, even one that had nothing to do with the sub-agent.

Solution:

_dispatch_task_fc catches DynamicNodeFailError and returns {"error": "<Type>: <message>", "is_error": True}, which _synthesize_task_fr_event turns into the call's function response. The parent's model reads the error and replies, and the call is answered, so later turns do not dispatch it again.

What still propagates, as before:

  • WorkflowConfigurationError and WorkflowInvariantError from the task (a miswired agent tree is not the task's answer).
  • NodeInterruptedError (a task waiting on the user), which is a BaseException and is not caught.
  • Cancellation.

Behavior change: a task sub-agent that raises no longer fails the parent's turn. The parent's model gets the error as the call's response instead.

flowchart TD
    A[Task sub-agent raises] --> B{Parent turn}
    B -->|Before| C[Turn fails and every later message fails too]
    B -->|After| D[Model reads the error and later messages work]
Loading

Narrated recording, before then after (sound on):

evidence.mp4

Testing Plan

Unit Tests:

  • I have added or updated unit tests for my change.
  • All unit tests pass locally.

New test test_failed_task_answers_its_call_with_the_error in tests/unittests/workflow/test_task_api_e2e.py: a task sub-agent whose model raises; it checks the call's function response is the error, that the parent's model replies, and that the next message reaches the model.

$ pytest tests/unittests/workflow/test_task_api_e2e.py -k failed_task   # on main
1 failed (RuntimeError: model call failed)

$ pytest tests/unittests/workflow tests/unittests/agents tests/unittests/test_runners.py --continue-on-collection-errors   # this branch
2136 passed, 1 skipped, 3 xfailed, 5 errors

After merging current main (c38ecda) into the branch:

$ pytest tests/unittests/workflow
1453 passed, 2 xfailed

The 5 errors are collection errors in modules that need optional extras not installed here (workflow/utils/test_workflow_graph_utils.py, agents/test_agent_config.py, agents/test_llm_agent_fields.py, agents/test_mcp_instruction_provider.py, test_runners.py). pyink and isort with the repo config leave the changed files unchanged.

Manual End-to-End (E2E) Tests:

chat.py is the issue's script with a scripted BaseLlm (no API key): a root chat agent with one task sub-agent helper, sent three messages. The argument picks what the helper's model does: fails raises RuntimeError, works calls finish_task, miswired raises WorkflowConfigurationError.

On main:

$ python chat.py fails
please delegate  -> TURN FAILED: RuntimeError: helper model is down
hello            -> TURN FAILED: RuntimeError: helper model is down
hello again      -> TURN FAILED: RuntimeError: helper model is down

On this branch:

$ python chat.py fails
please delegate  -> helper said {'error': 'RuntimeError: helper model is down', 'is_error': True}
hello            -> you said 'hello'
hello again      -> you said 'hello again'

Edge cases and unchanged behaviour checked (in the recording, on this branch):

  • A helper that succeeds still returns its result (helper said {'result': '42'}), and plain messages are unchanged.
  • A WorkflowConfigurationError from the helper still fails the run, as before.

Checklist

  • I have read the CONTRIBUTING.md document.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.
  • I have manually tested my changes end-to-end. (the scripted runs above and in the recording were made by the AI assistant)
  • Any dependent changes have been merged and published in downstream modules.

Additional context

This change was prepared with AI assistance (Claude Code), which also ran the tests and recorded the runs shown above.

🤖 Generated with Claude Code

A task sub-agent that raised ended the parent's turn and left its function
call unanswered in the session, so every later message dispatched it again
and failed the same way. The call now gets an error function response that
the parent's model can read. Wiring errors and interrupts still propagate.

Fixes google#8241
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A task sub-agent that raises ends the parent's turn, and the saved delegation is dispatched again on every later message of the session

2 participants