Skip to content

Python: fix: bound background_agents_wait_for_first_completion with a timeout - #7464

Draft
HUAN2022A wants to merge 2 commits into
microsoft:mainfrom
HUAN2022A:codex/parallel/background-agent-wait-timeout
Draft

Python: fix: bound background_agents_wait_for_first_completion with a timeout#7464
HUAN2022A wants to merge 2 commits into
microsoft:mainfrom
HUAN2022A:codex/parallel/background-agent-wait-timeout

Conversation

@HUAN2022A

Copy link
Copy Markdown

Summary

background_agents_wait_for_first_completion could suspend the calling agent's run forever when a child task never completes. This PR adds an optional bounded timeout: a wait_timeout_seconds provider default (300s) and a timeout_seconds argument on the tool itself. None restores the previous unbounded behavior. When the wait elapses, the provider refreshes task state through its existing LOST-detection path and returns the current statuses to the model.

Fixes #7454

Validation

  • Background-agents test file: 30/30 passed, including 3 new tests
  • Related harness/loop/observability test files pass
  • ruff check, ruff format, and pyright clean on the changed module

Copilot AI review requested due to automatic review settings July 31, 2026 18:04
@HUAN2022A
HUAN2022A temporarily deployed to github-app-auth July 31, 2026 18:04 — with GitHub Actions Inactive
@HUAN2022A
HUAN2022A temporarily deployed to github-app-auth July 31, 2026 18:04 — with GitHub Actions Inactive
@HUAN2022A
HUAN2022A temporarily deployed to github-app-auth July 31, 2026 18:04 — with GitHub Actions Inactive
@agent-framework-automation agent-framework-automation Bot added the python Usage: [Issues, PRs], Target: Python label Jul 31, 2026
@github-actions github-actions Bot changed the title fix: bound background_agents_wait_for_first_completion with a timeout Python: fix: bound background_agents_wait_for_first_completion with a timeout Jul 31, 2026
@HUAN2022A
HUAN2022A temporarily deployed to github-app-auth July 31, 2026 18:06 — with GitHub Actions Inactive

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR prevents background_agents_wait_for_first_completion from potentially suspending a parent agent run indefinitely by adding a configurable timeout: a provider-level wait_timeout_seconds default and a per-call timeout_seconds override, plus new tests covering timeout behavior and defaulting.

Changes:

  • Add wait_timeout_seconds to BackgroundAgentsProvider (default 300s) and apply it in background_agents_wait_for_first_completion.
  • Add an optional timeout_seconds parameter to the wait tool and return a status summary when the wait times out.
  • Add tests for timeout behavior, provider-default timeout behavior, and explicit timeout behavior when the task completes before the deadline.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
python/packages/core/agent_framework/_harness/_background_agents.py Adds provider- and tool-level timeout support to the wait-for-completion tool, including a timeout return path that refreshes task state.
python/packages/core/tests/core/test_harness_background_agents.py Adds new hanging-agent and timeout-focused tests to ensure waits are bounded and return status summaries.
Suppressed comments (1)

python/packages/core/agent_framework/_harness/_background_agents.py:415

  • asyncio.wait requires a non-negative timeout (or None). With the new parameters, a negative timeout_seconds (or a negative provider default) will raise and fail the tool invocation rather than returning a normal error/result string. Validate and return an error message before calling asyncio.wait.
            effective_timeout = self._wait_timeout_seconds if timeout_seconds is None else timeout_seconds
            done, _ = await asyncio.wait(
                [t for _, t in waitable],
                return_when=asyncio.FIRST_COMPLETED,
                timeout=effective_timeout,

Comment on lines +373 to +386
async def background_agents_wait_for_first_completion(
task_ids: list[int],
timeout_seconds: float | None = None,
) -> str:
"""Block until the first of the specified background tasks completes, up to a timeout.

Returns the completed task's ID, or the current task statuses if the timeout elapses
first (in which case call this tool again or check task results to proceed).

Args:
task_ids: IDs of background tasks to wait on.
timeout_seconds: Maximum time to wait in seconds. When omitted, the provider's
``wait_timeout_seconds`` is used.
"""
Comment on lines 301 to +302
self._agents = _validate_and_build_agent_dict(agents)
self._wait_timeout_seconds = wait_timeout_seconds
@HUAN2022A
HUAN2022A temporarily deployed to github-app-auth July 31, 2026 18:19 — with GitHub Actions Inactive
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Usage: [Issues, PRs], Target: Python

Projects

None yet

2 participants