Skip to content

fix(dispatcher): give a deferred run its final attempt before it expires - #804

Draft
neubig wants to merge 2 commits into
mainfrom
fix/798-deferred-final-attempt
Draft

neubig wants to merge 2 commits into
mainfrom
fix/798-deferred-final-attempt

Conversation

@neubig

@neubig neubig commented Nov 24, 2026

Copy link
Copy Markdown
Member

This PR was created by an AI agent (OpenHands) on behalf of @neubig.

Why

When the organization concurrency limit blocks a run, the run is deferred with exponential backoff. The deferral stops at AUTOMATION_DEFER_MAX_AGE_SECONDS after the run was created. When the next backoff would go past that deadline, _defer_or_skip_blocked_run sets deferred_until to exactly the deadline. The intent is one last attempt at that time (see test_backoff_bounded_by_remaining_age).

That last attempt never happened:

  • dispatch_pending_runs runs expire_aged_deferred_runs before it claims runs.
  • The sweep selects deferred_until <= now AND created_at <= now - max_age. For the capped row, both conditions become true at the same moment.
  • So on the first poll after the run came due, the sweep marked it SKIPPED / defer_expired without a sandbox attempt, even when capacity had come back. For an event run, the event is lost.

The sweep's docstring says the dispatch attempt should normally expire a deferred run, and that the sweep is for polls that are at the AUTOMATION_MAX_CONCURRENT_RUNS cap. The code did not do that.

Summary of the change

  • expire_aged_deferred_runs takes a new claim_grace_seconds argument (default 0). It reaps a run only after the run has been due for that long (deferred_until <= now - claim_grace_seconds). The age condition does not change.
  • dispatch_pending_runs passes two dispatcher poll intervals (2 * AUTOMATION_DISPATCHER_INTERVAL_SECONDS).
  • The result: a run that just came due is claimed by the next poll and gets its final attempt.
    • If capacity is back, the run starts.
    • If it is still blocked, _defer_or_skip_blocked_run sees that the age has passed and marks it defer_expired, as before.
  • A poll at the AUTOMATION_MAX_CONCURRENT_RUNS cap still reaps aged deferred runs once the grace has passed.
  • No schema or config change. Quotas are not bypassed: the final attempt goes through the same sandbox create, and that call can still return 429.

Fixes #798

Out of scope: issue #798 also proposes an upper limit on the deferral backoff, so runs are retried more often during their window. That change adds sandbox-API load when many runs are blocked, which #771 and #773 cared about. I left it for a separate discussion.

How to Test

uv sync --frozen
uv run python -m pytest tests/test_dispatcher.py -k 'FinalAttempt or ClaimGrace or ExpireAged or expires_aged_deferred_run_even_at_capacity' -v

Result on this branch: 11 passed.

The new tests in TestDeferredRunFinalAttempt run the real dispatch_pending_runs, _execute_run, _defer_or_skip_blocked_run, expire_aged_deferred_runs and CloudSandboxBackend with the default deferral settings. Only the sandbox API (an httpx.MockTransport) and the key mint are stubbed.

  1. A run that has been blocked for most of its window is dispatched. The sandbox API returns 429 CONCURRENCY_LIMIT_REACHED, and the run is deferred to its deadline.
  2. The run's timestamps are moved back, so the deadline has passed.
  3. One more poll runs:
    • test_run_starts_when_capacity_is_back_at_its_deadline: with capacity back, the run is RUNNING with a sandbox.
    • test_still_blocked_final_attempt_expires_the_run: still blocked, the run makes one more sandbox attempt and is then SKIPPED / defer_expired.

TestExpireAgedDeferredRunsClaimGrace checks that the sweep keeps a run that came due 5 s ago and reaps one that came due 30 s ago, with a 20 s grace.

I changed one existing test. test_expires_aged_deferred_run_even_at_capacity now seeds a run that came due 5 minutes ago instead of 1 s ago, because a run that just came due is now left to the claim first.

I also ran every test file that calls _execute_run or dispatch_pending_runs, plus tests/test_conversations.py and tests/test_run_utils.py: all pass. Pre-commit (ruff, pycodestyle, pyright) is clean on the changed files.

Video/Screenshots

Before: on main, the run is skipped as defer_expired on the first poll after its deadline. No final sandbox attempt is made, even though capacity is back.

Before: regression tests fail on main

After: on this branch, the final attempt happens. The run starts when capacity is back, and expires only if it is still blocked. The existing sweep tests still pass.

After: regression tests pass on the fix branch

Fixes #798

Co-authored-by: openhands <openhands@all-hands.dev>
Co-authored-by: openhands <openhands@all-hands.dev>
@github-actions

Copy link
Copy Markdown
Contributor

📁 PR Artifacts Notice

This PR adds or changes temporary PR-specific documents under .pr/. They stay on this branch for review. After this PR merges, the next release PR removes them from main.

@github-actions github-actions Bot added the type: fix A bug fix label Nov 24, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Coverage

Warning

Your comment is too long (maximum is 65536 characters), so the coverage report was not added. See the job log for how to reduce it.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: fix A bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Deferred runs are expired as defer_expired without their final dispatch attempt

2 participants