Skip to content

Global concurrency cap with a run queue (#166) - #1574

Merged
currentbits merged 2 commits into
mainfrom
coder/global-concurrency-cap-with-a-run-queue-773de6
Oct 10, 2026
Merged

currentbits merged 2 commits into
mainfrom
coder/global-concurrency-cap-with-a-run-queue-773de6

Conversation

@currentbits

Copy link
Copy Markdown
Owner

Closes #166.

What

  • Setting: maxConcurrentRuns (Settings → Spending), default 4, empty/null = no cap.
  • Gate in startRun (after the budget and provider-binary checks, so a missing CLI still fails fast and pool failover still works): a start beyond the cap appends the user message, parks the fully-built turn on the thread as runQueue (persisted), posts a "Queued #N" event and returns { runId: null, queued: true }.
  • Order: strict first-come-first-served across all projects. A new start waits behind anything older in the queue.
  • Admission: clearRun → admitQueuedRuns starts the oldest turns while slots are free. Raising or clearing the cap admits right away. Turns queued at quit are admitted on boot.
  • Sending again to a queued thread adds the message to its waiting turn.
  • Stop on a queued thread only leaves the queue; a lead's crew is not touched.
  • Agents panel: "Queued for a run slot · #N of M · Leave queue" for the selected thread, and a queued #N badge plus stop button on queued worker rows.
  • Exempt: memory consolidation skips the queue, like the budget gate.

Not covered

  • Workflow (Build template) runs count toward the cap but are never queued themselves.
  • One global queue, not per-project fairness.

Tests

  • electron/test/run-queue.test.js: queue/FIFO admission, one user message per turn, Stop leaves the queue, clearing the cap admits.
  • test/teamView.test.tsx: queue line, worker badge, Leave queue.
  • Settings snapshot tests updated for the new key.

🤖 Generated with Claude Code

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 10, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-10T08:00:35.333600Z ee94e6b PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ee94e6b317

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread electron/runner.js
Comment on lines 1533 to +1535
active.delete(threadId);
// After the caller finishes its terminal bookkeeping.
queueMicrotask(admitQueuedRuns);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Disable queue admission while stopping runs on quit

When the app quits with both active and queued turns, stopAll() calls clearRun() for each active turn, and this microtask runs immediately after the synchronous stop phase yields in runAppCleanup. It can therefore launch queued agents during shutdown; those agents were not in stopAll()'s snapshot and may spawn detached provider processes or persist a new working state just before exit. Suppress admission while shutdown is in progress so queued turns remain parked for the documented boot-time admission.

Useful? React with 👍 / 👎.

Comment thread electron/runner.js
Comment on lines +1087 to +1089
// startRun is synchronous up to claiming its `active` slot, so the
// next iteration already sees this one counted.
startRun({ ...input, threadId: t.id, fromRunQueue: true }).catch(

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Reserve each slot before launching an admitted run

On a normal desktop boot with persisted queued turns, the login-shell PATH promise is still pending when this loop runs. Every startRun() call awaits that promise before reaching active.set, so this non-awaited loop sees the same free-slot count for every item, clears all their runQueue records, and schedules all of them; once PATH resolves, fromRunQueue bypasses the concurrency gate and the cap can be exceeded by the entire saved queue. Reserve a slot synchronously or serialize admissions until each start claims active.

Useful? React with 👍 / 👎.

Comment thread electron/runner.js
Comment on lines +1856 to +1858
if (
thread.memoryConsolidate !== true &&
!input.fromRunQueue &&

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Apply the concurrency gate before the Ask-mode return

When an Ask-mode thread is started while the cap is full or older turns are queued, execution has already returned through startAskRun() before reaching this gate. That function adds the Ask turn to the same active map counted by runSlotsFree, so Ask starts can exceed maxConcurrentRuns and jump the FIFO queue; only memory consolidation is declared exempt. Run the queue decision before the Ask-mode dispatch while retaining Ask's budget and worktree exemptions.

Useful? React with 👍 / 👎.

Settings-level maxConcurrentRuns (default 4, null = no cap). Starts beyond
the cap park their turn on the thread as runQueue and are admitted FIFO as
runs settle; Stop leaves the queue. Queue position shows in the Agents panel.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@currentbits
currentbits force-pushed the coder/global-concurrency-cap-with-a-run-queue-773de6 branch from ee94e6b to fb64850 Compare October 10, 2026 08:17
@currentbits
currentbits merged commit 4f4ed7c into main Oct 10, 2026
5 of 6 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.

Global concurrency cap with a run queue

1 participant