Skip to content

fix(dispatcher): add jitter to the deferral backoff - #773

Merged
simonrosenberg merged 1 commit into
mainfrom
defer-backoff-jitter
Nov 18, 2026
Merged

simonrosenberg merged 1 commit into
mainfrom
defer-backoff-jitter

Conversation

@simonrosenberg

Copy link
Copy Markdown
Member

Summary

  • The delay before a deferred run is retried is now the exponential backoff (AUTOMATION_DEFER_BACKOFF_BASE_SECONDS * 2**n) multiplied by a random factor between 0.5 and 1.5. It is still capped by the time left before AUTOMATION_DEFER_MAX_AGE_SECONDS.
  • This uses the same random.uniform(0.5, 1.5) as the sandbox-provisioning retry in _acquire_execution_context.
  • The average delay doesn't change. No config or schema change.

Why

Follow-up to #772. That PR stops deferring runs blocked by the daily sandbox limit, which removes the nightly burst described in #771. Runs blocked by the organization concurrency limit are still deferred.

Without jitter, runs blocked at the same moment wait exactly the same time, so they all retry together and hit the limit together again. This is common, for example when several automations share a cron minute. Last night, from 23:40 to 23:59 UTC, prod deferred about 100 runs a minute, with retry counts spread over 1–9. Jitter spreads these retries out so they don't arrive together.

Tests

  • New test test_backoff_delay_is_jittered, run with factors 0.5 and 1.5. It checks that the delay is the backoff times the random factor, and that random.uniform(0.5, 1.5) is called. Both cases fail against the origin/main dispatcher (uniform called 0 times).
  • test_backoff_delay_grows_per_attempt now fixes the factor at 1.0. With jitter, delays are no longer strictly increasing (for example 14s then 11s), so the test would be flaky otherwise.
  • test_backoff_bounded_by_remaining_age passes unchanged: the cap applies after jitter.
  • tests/test_dispatcher.py and tests/test_daily_sandbox_limit.py: 109 passed. pre-commit passes.

🤖 Generated with Claude Code

Runs blocked by the concurrency limit at the same moment were deferred
by the same 60 * 2**n delay, so they all retried together and hit the
limit together again. Scale each delay by a random factor in [0.5, 1.5],
as the sandbox-provisioning retry already does, so they spread out.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added the type: fix A bug fix label Nov 18, 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.

@ak684 ak684 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.

Approving. Small, safe change, and it merges cleanly with #772. The dispatcher tests pass with both together (109 passed in a local run).

One note on expectations: this would not have prevented the midnight burst. That was lined up by the quota reset, not by retry timers. It does help runs blocked together by the concurrency limit, such as a shared cron minute.

@all-hands-bot all-hands-bot 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.

This review was posted by an AI agent (OpenHands).

Approving. The change is in scope for this repository (dispatcher deferral / run-state scheduling) and is a small, localized fix with adequate context in the description.

What I verified on head 58ce8c1:

  • The delay is now min(base * 2**n * random.uniform(0.5, 1.5), remaining) on the deferral branch only; the terminal skip branch and the race-guarded requeue_deferred_run write are untouched, so run-state transitions stay idempotent and runs near defer_max_age_seconds still cannot be rescheduled past their deadline. I executed the real _defer_or_skip_blocked_run with factors 0.5/1.0/1.5 across attempt counts and age-cap boundary cases (including age 21590s of 21600s) and the persisted delays match the formula exactly.
  • The ±50% jitter matches the pre-existing provisioning-retry jitter in _acquire_execution_context, and random was already imported in this module.
  • deferred_until is honored by the dispatch poll (deferred_until IS NULL OR <= now()), so jittered delays are applied as written.
  • Tests are sound: test_backoff_delay_is_jittered pins the factor at both endpoints and fails against main (uniform never called), and pinning test_backoff_delay_grows_per_attempt at factor 1.0 removes the real flakiness jitter would introduce into a strictly-increasing assertion. test_backoff_bounded_by_remaining_age passing unchanged is consistent with the cap applying after jitter.
  • All GitHub Actions checks are green on this exact head, including unit-tests. Local note: the Docker/PostgreSQL test fixture cannot run in this sandbox, so instead of the container-backed suite I exercised the deferral path directly against the changed code (above) and ran ruff format/ruff check/pyright on the three changed files — all clean; the non-container tests in tests/test_daily_sandbox_limit.py pass locally.

No material correctness, security, or compatibility findings. The relationship to #772 is compatible: that PR remains open, and with or without it, runs still blocked by the organization concurrency limit now retry with spread-out delays rather than in lockstep.

✅ APPROVED

@simonrosenberg
simonrosenberg merged commit efa65d0 into main Nov 18, 2026
10 checks passed
@openhands-release-bot openhands-release-bot Bot added the released: 1.34.0 Shipped in 1.34.0 label Nov 19, 2026
@openhands-release-bot

Copy link
Copy Markdown
Contributor

🚀 Released in 1.34.0.

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

Labels

released: 1.34.0 Shipped in 1.34.0 type: fix A bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants