Skip to content

fix(relay): count in-flight queue jobs in the running-box gate#35

Merged
madarco merged 1 commit into
mainfrom
fix/queue-running-gate-inflight
May 31, 2026
Merged

fix(relay): count in-flight queue jobs in the running-box gate#35
madarco merged 1 commit into
mainfrom
fix/queue-running-gate-inflight

Conversation

@madarco
Copy link
Copy Markdown
Owner

@madarco madarco commented May 31, 2026

Problem

agentbox <agent> -i "<prompt>" --max-running N (the running-box concurrency gate) could start more jobs than the cap. Observed live on Vercel: two jobs submitted back to back with --max-running 1 both started immediately, creating two boxes.

Root cause in packages/relay/src/queue.ts:

  • The running-box gate counts boxes via defaultCountRunningBoxes, which reads ~/.agentbox/state.json. A just-started job's box doesn't appear there until the worker finishes provisioning (~25s cloud, image pull docker). During that window the gate saw 0 running, so the scheduler's per-tick "start as many as fit" loop re-selected the same free slot for the next job.
  • The 3s result cache compounded it by returning the stale pre-start count within a single tick.
  • The working-agent gate (--max-working) was already correct — it counts in-flight running jobs. Only the running-box gate lacked that term.

Provider-agnostic (docker affected identically; just easier to hit on cloud where provisioning is slower).

Fix

  • Extract countInFlightCreateJobs(jobs, accountedBoxIds) — the in-flight term the working-agent gate already used — and share it across both gates.
  • defaultCountRunningBoxes now adds that term, computed fresh each call (cheap loadQueue read), while only the expensive box-state portion (state.json + docker inspect) stays cached for 3s. Dedup by state.json box ids; dead-pid workers skipped.
  • defaultCountWorkingBoxes is behavior-preserving (now calls the shared helper).

Verification

  • Unit (pnpm --filter @agentbox/relay test): +5 countInFlightCreateJobs tests (in-flight counted, deduped once box accounted, non-running ignored, dead-pid skipped, multi-sum). Existing working-gate + selector tests stay green. 125 relay tests pass. lint + full-workspace typecheck clean.
  • Docker e2e: two -i jobs with --max-running 1 → submit-time count shows 1/1, job A runs (one container), job B stays queued; destroying A's box freed the slot and B started within a tick. No over-start, no leftover containers.

Note

The original cloud repro (--provider vercel) depends on #34 (cloud -i support). This fix is independent and lands on main; the docker path exhibits and is fixed for the identical bug.


Note

Medium Risk
Changes core queue scheduling and concurrency accounting; behavior is well-tested but affects when jobs start across providers.

Overview
Fixes --max-running over-starting when multiple queued jobs are picked in one scheduler tick: the running-box gate only looked at state.json, so a job just flipped to running still looked like zero occupancy until provisioning finished.

defaultCountRunningBoxes now adds the same in-flight term the working-agent gate already used, via a shared countInFlightCreateJobs. Only the expensive box-state read stays on the 3s cache; queue manifests are re-read each call so a freshly started job counts immediately. defaultCountWorkingBoxes is refactored to call the helper (behavior unchanged). Five unit tests cover dedup, dead PIDs, and status filtering.

Reviewed by Cursor Bugbot for commit 7c2e033. Configure here.

The --max-running gate could over-start: defaultCountRunningBoxes counts
boxes from state.json, but a just-started job's box isn't there until the
worker finishes provisioning (~25s cloud, image pull docker). During that
window the gate saw 0 running, so the per-tick 'start as many as fit' loop
re-selected the same free slot and started past the cap. The 3s result
cache compounded it by returning the stale pre-start count within a tick.

- Extract countInFlightCreateJobs (the in-flight term the working-agent
  gate already used) and share it across both gates.
- defaultCountRunningBoxes now adds that term, computed fresh each call
  (cheap loadQueue read) while only the expensive box-state portion stays
  cached for 3s. Dedup by state.json box ids; dead-pid workers skipped.

The working-agent gate is unchanged in behavior (helper extraction).
@vercel
Copy link
Copy Markdown

vercel Bot commented May 31, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
agentbox-web Ready Ready Preview, Comment May 31, 2026 1:02pm

Request Review

@madarco madarco merged commit 6f21b2e into main May 31, 2026
4 checks passed
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.

1 participant