Repository navigation
Conversation
The host daemon now sets nice 10 (below-normal on Windows) on provider processes, terminal shells and environment lifecycle scripts right after spawning them, so the builds and tests agents run inherit it. When many agents saturate the CPU on Linux, the server keeps its share instead of being preempted for most of each event-loop turn. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…-workloads-thr_vw834vqyax # Conflicts: # packages/process-utils/src/index.ts
Collaborator
Author
ymichael
added a commit
that referenced
this pull request
Nov 9, 2026
…periment) (#6215) ## 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. - `taskpolicy` execs in place, so the PID, stdio, the IPC channel and signal handling are unchanged. - The policy is not inherited by children. - The command is resolved through the host toolchain first (#6194) and then wrapped. That keeps #6194's replacement of a removed or upgraded Node, its network flags, and the loopback `NO_PROXY` entries #6394 adds for `--use-env-proxy`. With the experiment off, the launcher spawns exactly as on main. - **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](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5.5 <[email protected]>
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.
Human comments
What was wrong
When many agents saturate the CPU, the server's main thread is preempted for most of each event-loop turn. On 2026-10-23 at 12:30–12:50 it got 0.06 CPU-seconds per wall second while busy about 88% of the time; see #6215 for the analysis. On macOS, #6215 fixes this with the application scheduling policy. On Linux, the scheduler weights threads by
nice, so the lever there is to run agent workloads at a lower priority than bb itself. Today provider processes and lifecycle scripts run at the same priority as the server.What changed
@bb/process-utilsgainsdeprioritizeAgentWorkload(pid), which sets nice 10 viaos.setPriority. On Windows this maps to the below-normal priority class. It ignores a process that has already exited.packages/agent-runtime/src/runtime-provider-process.ts), meaning the bridge worker and therefore the provider CLI and every tool, build and test it runsapps/host-daemon/src/environment-lifecycle-script.ts)pnpm test/tsc/vitest workers run at nice 10. Terminal shells keep normal priority, including terminals created throughbb terminal createor the SDK. The daemon, its plugin hosts and watchers, and the server keep their priority too.docs/debugging-and-qa.mddocuments the behavior and how to recognize CPU starvation in slow-DB logs.niceonly matters when the CPU is contended; an idle machine still gives agents all of it. No configuration knob: when agents and bb compete for CPU, bb should win.How you verified
Linux (OrbStack VM, 16 vCPUs,
node:22-slim): a server-like probe (a 10 ms tick plus a 20 ms synchronous chunk every 100 ms) running against 64 CPU burners:setsid) at nice 0setsid) at nice 10On macOS,
nicehas no measurable effect between processes (48 burners at nice 0/10/19 all stretched 20 ms chunks to 40–80 ms). There this change is harmless, and #6215 is the fix.Real stack (
pnpm start:worktreeon this branch):ps -o nishowsbb-provider-bridge-workerprocesses at nice 10, while the daemon's plugin hosts and watcher keep the daemon's own value.pnpm exec turbo run typecheck test --filter=@bb/process-utils --filter=@bb/agent-runtime --filter=@bb/host-daemonpasses. The new test checks that a lowered process's later children inherit the priority, and that an exited PID is ignored.🤖 Generated with Claude Code