Skip to content

Keep the server responsive when agents saturate the CPU (behind an experiment) - #6215

Merged
ymichael merged 4 commits into
mainfrom
bb/server-stalls-under-many-concurrent-agents-thr_vw834vqyax
Nov 9, 2026
Merged

ymichael merged 4 commits into
mainfrom
bb/server-stalls-under-many-concurrent-agents-thr_vw834vqyax

Conversation

@ymichael

@ymichael ymichael commented Oct 24, 2026 •

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

On 2026-10-23, from 12:16 to 12:53, 15–19 agent threads ran at once. The server main thread was busy for most of the window: event-loop utilization was 0.84–0.88, and the saved CPU profiles show the loop non-idle about 45% of wall time. Yet it consumed only 0.06 CPU-seconds per wall second. The stall logs for 12:30–12:50 show the same thing per task: the longest synchronous work in each stall got 6–7% of its wall time as CPU. A 30 ms timeline build took about 500 ms, and slow TRANSACTION IMMEDIATE queries ran at 5% CPU/wall with about 1,200–1,500 involuntary context switches per second. The server was runnable but not scheduled.

  • macOS: nice does not help. The clutch scheduler first picks among thread groups in the same QoS bucket, so a busy plain process gets only its fair share against dozens of agent, build and test processes. With 48 CPU burners at nice 0, 10 and 19, a server-like probe's 20 ms chunks stretched to 40–80 ms in every case. Clamping agents to utility QoS protected the server but made the agents' own parallel CPU work 2.3–3× slower, so that is not acceptable.
  • Linux: the scheduler weights threads by nice, but provider processes and lifecycle scripts ran at the same priority as the server.

This PR now also contains #6216, and both halves sit behind one experiment.

Timeline builds, Nov 7 2026. A separate investigation found the same cause behind production timeline builds running 4–7x slower than in-process. A build that needs about 160 ms of CPU took 663 ms in production, at 0.39 CPU/wall with 1,810 involuntary context switches and no page faults or disk reads. With 32 normal-priority CPU burners on 16 cores, the same build took 596–703 ms in-process. Under taskpolicy -a it took 220–232 ms, at 0.99 CPU/wall.

What changed

Experiment agentCpuPriority. It is off by default, including for early adopters, so it runs only where someone turns it on. It has a Settings → Experiments label and description, an app fallback, and entries in docs/configuration.md, docs/debugging-and-qa.md and the bb guide.

Agent processes at nice 10 (from #6216; below-normal priority class on Windows):

  • @bb/process-utils deprioritizeAgentWorkload(pid) sets nice 10, but only while setAgentWorkloadsLowered(true) is in effect.
  • The daemon calls it right after spawning provider processes (the bridge worker, and therefore the provider CLI and every tool, build and test it runs) and environment lifecycle scripts. Their children inherit it.
  • Terminal shells keep normal priority. That includes terminals created through bb terminal create or the SDK, which agents use for dev servers.
  • Delivery:
    • The server sends agentCpuPriority in the session-open response and in provider-session-policy.replace. That message is already pushed on every config-changed, which an experiment change triggers. It follows the same path as idleProviderSessionTimeoutMinutes.
    • This bumps HOST_DAEMON_PROTOCOL_VERSION to 265. Servers accept only the matching version, so an older daemon is rejected at session open until it updates, and older daemons never lower anything.
    • A policy message without the field fails validation and is ignored.
  • Toggling takes effect after restarting bb, including the restart an update performs. A running provider bridge worker serves new threads with the priority it started with, and a process already lowered can't be raised again, so it stays at nice 10 until it exits. Settings and the docs say so.

Server and daemon with app scheduling on macOS (taskpolicy -a):

  • What it does: the launcher starts the server and host daemon through /usr/sbin/taskpolicy -a, the application resource-management policy.
  • How the launcher learns the value: the launcher starts before any database is open.
    • The server writes {"enabled":…} to <data dir>/agent-cpu-priority.json at startup and whenever the resolved experiment value changes.
    • The daemon writes the last value it received into its own data dir, which covers a remote machine whose daemon has its own launcher.
    • The launcher reads the file each time it spawns the server or daemon. A missing or unreadable file means off, and nothing blocks startup.
    • This half therefore takes effect after restarting bb.
  • Before the handshake: the daemon also seeds its nice-10 setting from the same file when it starts. Without that, bridge workers spawned before the handshake would miss the flag.
  • On a single machine, the server and its local daemon write the same file with the same value.

Other platforms, or a missing taskpolicy, spawn directly as before.

Plugin SDK moves to 0.6.135: the new experiment key appears in the bundled Experiments type, and the npm version guard requires the bump.

How you verified

All numbers were re-measured on Nov 7 2026 at 940e378 (origin/main 55135d1 merged in) on an M4 Max (12P+4E). Each pnpm start:worktree run used this checkout's fresh, empty dev data dir.

This machine was busy with other threads throughout: load average 32–67 before any burners started. Rows marked "0 extra" include that background load.

Real worktree server, macOS. 4 concurrent keep-alive clients on GET /api/v1/system/config for 12 s, with and without 96 extra self-terminating CPU burners, two rounds each. The experiment was toggled with PUT /api/v1/settings/experiments and bb was restarted between arms.

The load client is a plain process, so under burners it gets starved too, and the first taskpolicy -a round with a plain client shows that. Real clients are browser or Electron windows, which macOS already schedules as apps. So the headline rows run the client under taskpolicy -a as well, to measure only the server.

server launch client burners req/s p50 p90 p99
plain app-scheduled 0 extra 664–809 2.2–2.3 ms 6.6–6.9 ms 74–97 ms
plain app-scheduled 96 181–268 2.2–2.5 ms 27–51 ms 252–338 ms
taskpolicy -a app-scheduled 0 extra 2460–2809 1.1–1.2 ms 2.1–2.3 ms 3.1–3.8 ms
taskpolicy -a app-scheduled 96 2020–2025 1.5 ms 2.8–2.9 ms 4.3 ms
plain plain 0 extra 1420–1766 1.9–2.2 ms 3.7–4.9 ms 7.8–14 ms
plain plain 96 158–342 2.3–2.8 ms 34–99 ms 98–303 ms
taskpolicy -a plain 0 extra 1832–2358 1.4–1.7 ms 2.4–3.1 ms 4.0–9.5 ms
taskpolicy -a plain 96 617–765 2.7–3.0 ms 11–15 ms 39–41 ms

The plain-client plain-server baseline ran first, while the background load was lowest, at 32–58.

Linux (OrbStack VM, 16 vCPUs, node:22-slim). A server-like probe does 20 ms CPU chunks every 50 ms, against 64 CPU burners, two rounds each:

burners probe CPU share chunk wall p50 chunk wall p99 loop delay p99
none 0.40 21 ms 21 ms 21–22 ms
64 at nice 0 0.17–0.19 81–94 ms 145–163 ms 132–151 ms
64 at nice 10 0.39–0.40 23–24 ms 39–49 ms 34–47 ms

Toggling on the real stack (ps -axo pid,ppid,pri,ni,command and ps -M). Threads ran in a project on a temp git repo:

state server daemon provider bridge workers + provider CLI agent tool command terminal (bb terminal create --command) plugin hosts / watcher
default (off), first start PRI 31, NI 0 PRI 31, NI 0 – – – PRI 31, NI 0
turned on, before restart unchanged unchanged – – – unchanged
on, after restart PRI 46 (main thread; workers 31) PRI 46 NI 10 (Claude Code and Codex) NI 10 (zsh -lc sleep 45 under codex) NI 0 NI 0
turned off, after restart PRI 31, NI 0 PRI 31, NI 0 NI 0 NI 0 (zsh -c sleep 44) – NI 0

<data dir>/agent-cpu-priority.json changed to {"enabled":true} and back to {"enabled":false} as soon as each PUT returned.

Tests:

  • logged-process:
    • Off spawns the command as on main, and on spawns /usr/sbin/taskpolicy -a <node> (macOS). Under the wrapper, the PID and IPC still work.
    • New: a --use-env-proxy child under the wrapper still gets the loopback NO_PROXY entries. This test fails on the pre-merge wrapper, which dropped the toolchain env.
  • workload-priority:
    • Off leaves processes at their inherited priority, and on lowers them, with grandchildren inheriting nice 10.
    • A missing, garbled, non-boolean or wrongly shaped file reads as off, and a failed write does not throw.
  • Server:
    • The handshake carries the experiment value.
    • Toggling pushes agentCpuPriority to connected daemons, parsed through the contract schema, and rewrites the launcher file.
    • Contract and experiment tests are updated for 265 and the new key.
  • Daemon:
    • The handshake, a live change and a reconnect all reach onAgentCpuPriority, and a policy without the field is ignored.
    • The app lowers workloads and saves the value while the server enables it, and stops lowering new processes and saves off when it is turned off.
    • The app seeds lowering from a saved file before the handshake.

Typecheck and tests pass via Turbo for @bb/server, @bb/host-daemon, bb-app, @bb/process-utils, @bb/app, @bb/agent-runtime, @bb/host-daemon-contract, @bb/domain and @bb/templates, run with env -u BB_ACCOUNT_POOL_PARENT_URL -u BB_SERVER_URL -u BB_CLI. The plugin SDK npm version guard passes at 0.6.134; after rebasing onto main, which took 0.6.134 for another change, the bump is 0.6.135.

Not re-measured here: agent throughput under nice 10, and Windows below-normal priority.

AGENT GENERATED

🤖 Generated with Claude Code

@ymichael ymichael changed the title Keep the server responsive on macOS when agents saturate the CPU Keep the server responsive when agents saturate the CPU (behind an experiment) Oct 27, 2026
@ymichael ymichael closed this Oct 27, 2026
@ymichael ymichael reopened this Nov 7, 2026
@ymichael
ymichael marked this pull request as draft November 7, 2026 19:42
@github-actions github-actions Bot closed this Nov 8, 2026
@github-actions

github-actions Bot commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

This is an automated message. We have closed this pull request because it has been open for more than two weeks.

ymichael and others added 3 commits November 9, 2026 10:01
…periment)

Behind the agentCpuPriority experiment, the host daemon starts provider
bridge workers and environment lifecycle scripts at nice 10 (below-normal
on Windows), and on macOS the launcher starts the server and daemon with
application scheduling (taskpolicy -a). The server sends the value in the
session handshake and provider-session-policy.replace (protocol 265) and
mirrors it to <data dir>/agent-cpu-priority.json for the launcher.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Provider bridge workers serve several threads, so a live toggle does not
reach new threads on a running worker; describe the restart requirement
for both halves in Settings and the docs.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
ymichael added a commit that referenced this pull request Nov 9, 2026
## Human comments

## What was wrong

The nightly stale-PR cleanup (`.github/workflows/close-stale-prs.yml`)
measured age from `created_at` only. A PR reopened after its first
closure was therefore closed again at the next scheduled run. #6215 was
created Oct 24, closed Oct 27, reopened Nov 7, and closed by the bot on
Nov 8 with "open for more than two weeks" after less than a day open.

## What changed

Before closing a candidate, the job reads the PR's issue events and
treats the PR as open since its latest `reopened` event (or `created_at`
if never reopened). PRs reopened within the fourteen-day window are
skipped and logged as "Recently reopened". The creation-date prefilter
is unchanged, so events are fetched only for PRs older than fourteen
days.

## How you verified

- Ran the embedded script's `open_since` against live data: #6215
resolves to its 2026-11-07T19:42:06Z reopen (so the Nov 8 run would have
skipped it); #6307 (never reopened, closed today) still resolves to its
creation time.
- Preview run (`APPLY=false`) of the full script against `get-bb/bb`
completes without errors.
- Checked the last five cleanup runs: #6215 is the only reopened PR that
was closed.
- `pnpm exec oxfmt .github/workflows/close-stale-prs.yml --check`, `git
diff --check`

> AGENT GENERATED

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 5.5 <[email protected]>
@ymichael ymichael reopened this Nov 9, 2026
@ymichael
ymichael force-pushed the bb/server-stalls-under-many-concurrent-agents-thr_vw834vqyax branch from 940e378 to 42e2796 Compare November 9, 2026 18:05
@ymichael
ymichael marked this pull request as ready for review November 9, 2026 18:06
@ymichael
ymichael merged commit d8898fa into main Nov 9, 2026
1 check passed
@ymichael
ymichael deleted the bb/server-stalls-under-many-concurrent-agents-thr_vw834vqyax branch November 9, 2026 18:25
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