fix(relay): count in-flight queue jobs in the running-box gate#35
Merged
Conversation
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).
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 1both started immediately, creating two boxes.Root cause in
packages/relay/src/queue.ts: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 saw0 running, so the scheduler's per-tick "start as many as fit" loop re-selected the same free slot for the next job.--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
countInFlightCreateJobs(jobs, accountedBoxIds)— the in-flight term the working-agent gate already used — and share it across both gates.defaultCountRunningBoxesnow adds that term, computed fresh each call (cheaploadQueueread), 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.defaultCountWorkingBoxesis behavior-preserving (now calls the shared helper).Verification
pnpm --filter @agentbox/relay test): +5countInFlightCreateJobstests (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-workspacetypecheckclean.-ijobs with--max-running 1→ submit-time count shows1/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-isupport). This fix is independent and lands onmain; 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-runningover-starting when multiple queued jobs are picked in one scheduler tick: the running-box gate only looked atstate.json, so a job just flipped torunningstill looked like zero occupancy until provisioning finished.defaultCountRunningBoxesnow adds the same in-flight term the working-agent gate already used, via a sharedcountInFlightCreateJobs. 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.defaultCountWorkingBoxesis 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.