Skip to content

Implement hierarchical concurrency limits for automation runs #72

Open

Description

@jpshackelford

Summary

Implement concurrency limits to control how many automation runs can execute simultaneously, with a hierarchical system of defaults and caps.

Motivation

  1. System Protection: Prevent a single automation from overwhelming backend services (e.g., key-value store) with unbounded concurrent runs
  2. Monetization: Enable billing tiers based on org-level concurrency limits
  3. Fair Resource Sharing: Ensure no single org monopolizes system resources

Current Behavior

  • No concurrency guards exist - multiple runs of the same automation can run concurrently
  • Scheduled automations create new runs even if previous runs are still executing
  • Event-triggered automations queue runs but dispatch without limit checks
  • Manual dispatch API creates runs without checking for existing active runs

Proposed Hierarchy

GLOBAL (config/environment)
├── max_org_aggregate_cap = 500        (hard upper bound for any org)
├── max_per_automation_cap = 20        (hard upper bound for any automation)
├── default_org_aggregate = 50         (default org aggregate if not set)
├── default_per_automation = 1         (default per-automation if not set)
│
└── ORG (DB table or fetched from OpenHands API)
    ├── aggregate_max                  (total concurrent runs for this org)
    ├── default_per_automation         (default for automations in this org)
    │
    └── AUTOMATION (existing table, new column)
        └── max_concurrent_runs        (limit for this specific automation)

Two Distinct Limit Types

  1. Per-Automation Max: How many concurrent runs can this specific automation have?
  2. Org Aggregate Max: How many total concurrent runs can this org have across ALL automations?

Resolution Chain

Per-Automation Limit:
  automation.max_concurrent_runs 
    ?? org.default_per_automation 
    ?? settings.default_per_automation
  (capped by settings.max_per_automation_cap)

Org Aggregate Limit:
  org.aggregate_max 
    ?? settings.default_org_aggregate
  (capped by settings.max_org_aggregate_cap)

Behavior by Trigger Type

Trigger Type Behavior When At Limit Recommended Default
Cron (scheduled) Skip creating run, log the skip 1 (no overlapping runs)
Manual dispatch API Return HTTP 409 Conflict 1
Event-triggered Create PENDING run (preserve payload), but don't dispatch until under limit TBD - needs discussion. Events can burst (e.g., many commits pushed).

Open Questions

  1. Where should org settings live?

    • Local org_settings table (simpler, self-contained)
    • Fetch from OpenHands API (single source of truth)
    • Hybrid with caching?
  2. Should PENDING runs count toward limits?

    • Current proposal: only RUNNING counts
    • Alternative: count PENDING too to prevent queue buildup
  3. What happens when limits change mid-flight?

    • Proposed: Let running jobs finish, only affect new dispatches
  4. What's a reasonable default for event-triggered automations?

    • Events can arrive in bursts (e.g., webhook replay, mass commits)
    • Too low = long queue backlogs
    • Too high = potential resource issues

Activity

  1. linear commented on Apr 25, 2026

    @linear
  2. jpshackelford commented on Apr 25, 2026

    @jpshackelford
    MemberAuthor

    Implementation Tasks

    1. Schema Changes

    • Add max_concurrent_runs column to automations table
    • Create org_settings table (or decide to fetch from OpenHands API)
    • Add migration

    2. Config Changes

    • Add global settings to config.py:
      • max_org_aggregate_cap
      • max_per_automation_cap
      • default_org_aggregate
      • default_per_automation

    3. Validation

    • Validate max_concurrent_runs on automation create/update (can't exceed org/global caps)
    • Validate org settings (can't exceed global caps)

    4. Scheduler Changes

    • Before creating PENDING run for cron triggers, check if automation already has PENDING/RUNNING runs
    • Log skipped scheduled runs

    5. Dispatcher Changes

    • Query running counts (by automation, by org) before dispatching
    • Skip dispatching runs that would exceed limits (leave as PENDING)
    • Log skipped dispatches with reason

    6. API Changes

    • Manual dispatch endpoint returns 409 if at limit
    • Expose max_concurrent_runs in automation create/update/response schemas

    7. Database Index

    • Add partial index for efficient RUNNING count queries:
      CREATE INDEX ix_automation_runs_running 
      ON automation_runs (automation_id) 
      WHERE status = 'RUNNING';
  3. jpshackelford commented on Apr 25, 2026

    @jpshackelford
    MemberAuthor

    Why hard concurrency caps are needed despite scaling

    Q: If sandboxes auto-scale and we can add automation service pods, do we still need hard concurrency caps (system-configurable limits)?

    A: Yes. Scaling and concurrency caps solve different problems:

    Concern Scaling Helps? Hard Cap Needed?
    Automation service capacity ✅ Yes (add pods) No
    Sandbox availability ✅ Yes (Cloud auto-scales) No
    External resource protection ❌ No Yes
    Cost control ❌ No (more sandboxes = more cost) Yes
    Org billing/fairness ❌ No Yes

    Key insight: Sandboxes auto-scaling means more concurrent runs hit external resources (APIs, databases, services the automation interacts with), not fewer. Hard caps protect those downstream dependencies.

    Additionally, without caps:

    • An event burst could spin up hundreds of sandboxes simultaneously
    • Org fairness requires bounding how much of the system any single org can consume
    • Cost control requires capping concurrent sandbox usage

    Hard caps are orthogonal to scaling — they bound how many runs execute simultaneously for a given automation/org, regardless of infrastructure capacity.

  4. all-hands-bot commented on Sep 15, 2026

    @all-hands-bot
    Contributor

    [Automatic Post]: This issue has had no activity for 142 days (labels: none). Could someone take a look when they have a chance and help figure out next steps?

    Generated by OpenHands automation on behalf of the user.

  5. all-hands-bot commented on Oct 23, 2026

    @all-hands-bot
    Contributor

    [Automatic Post]: This issue has had no activity for 37 days (labels: none). Could someone take a look when they have a chance and help figure out next steps?

    Generated by OpenHands automation on behalf of the user.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions