Skip to content

Commit 259a14b

Browse files
authored
feat(nodes): derive worker capacity from CPU cores and make it configurable (#128352)
Node session hosting was capped at a hardcoded 2 concurrent worker sessions. The default is now one worker slot per available CPU core (clamped to 1..1024), and nodeHost.workerRuns.capacity overrides it explicitly. The capacity option was already plumbed through the supervisor; this wires it from config and deletes the hardcoded constant. Docs and config baselines regenerated.
1 parent 388e83d commit 259a14b

14 files changed

Lines changed: 86 additions & 15 deletions
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"core": 2335,
2+
"core": 2339,
33
"channel": 3584,
44
"plugin": 3978
55
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
a485e23c0c1db18814fbc38e78cac7650ad2d309285bd679ade63d05e153da8a config-baseline.json
2-
4896e6e4826ea96182e717ae5e719a82611f42a565608384bc1f1a2ab0d6be6e config-baseline.core.json
1+
6768a9d1083664a60bea75df4ac606b761760ed1deb4ffee00d39a8de8bcac88 config-baseline.json
2+
e25fb219d62550bc05314e320cd66d0d9e4161e50ac89ac84606dcaae8719084 config-baseline.core.json
33
3300f931abce160c1d6768af1a358683b8f77af479e8b728aeefd38039a61507 config-baseline.channel.json
44
ed7c7e8dfe9d676ebbf60b5ee55f72d1f9e0286ffb37743f40f6c337816b166d config-baseline.plugin.json

‎docs/nodes/index.md‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -491,10 +491,11 @@ Gateway does not fall back to the node's local OpenClaw package or an older
491491
supervisor dialect.
492492

493493
This setting enables supervised session turns on the paired device, including
494-
Gateway-owned workspace transfer and result reconciliation. Each node runs at
495-
most two worker processes by default. A third launch waits up to 10 seconds for
496-
a durable slot; while both slots are occupied, the node remains available for
497-
status and cancellation but is not selected for a new session turn.
494+
Gateway-owned workspace transfer and result reconciliation. By default, each
495+
node has one worker slot per available CPU core. Configure the slot count with
496+
`nodeHost.workerRuns.capacity`. Launches beyond capacity wait up to 10 seconds
497+
for a durable slot; while all slots are occupied, the node remains available
498+
for status and cancellation but is not selected for a new session turn.
498499

499500
The picker derives every device row from `environments.list`. Every selected
500501
runtime requires an available, connected paired session host. OpenClaw worker

‎docs/plan/runners.md‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,9 @@ stated honestly (revision 1 undersold this):
205205
`{ profileId } | { deviceId }`; the device → environment mapping resolves
206206
server-side. Devices are not smuggled through synthesized
207207
`cloudWorkers.profiles` entries.
208-
- **Concurrency slots.** The node supervisor admits two physical worker
209-
processes by default. Durable `pending` and `running` launches consume those
208+
- **Concurrency slots.** The node supervisor defaults to one physical worker
209+
process per available CPU core; `nodeHost.workerRuns.capacity` overrides the
210+
slot count. Durable `pending` and `running` launches consume those
210211
slots atomically; same-launch replay consumes no additional slot. The node
211212
publishes exact bounded `{ total, available }` capacity after restart
212213
reconciliation and every occupancy transition. New launches require
@@ -235,7 +236,7 @@ makes clients refetch without exposing hashes, paths, or receipt details. Status
235236
and cancellation reacquire the current supervisor proof and use the durable
236237
launch identity so an upgrade cannot strand an existing worker. Node-local
237238
opt-in advertises capacity; default nodes remain non-hosts. The supervisor owns
238-
two atomic durable capacity slots, bounded 10-second admission, restart
239+
configurable atomic durable capacity slots, bounded 10-second admission, restart
239240
reconciliation, and exact occupancy publication.
240241
Device dormancy expiry and terminal launch/environment retention bound durable
241242
rows. Node workspace cleanup waits for a full reconnect-scoped Gateway retain

‎src/config/config.node-agent-runs.test.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,20 @@ describe("node agent-runs config", () => {
3232
expect(validateConfigObject({ nodeHost: { workerRuns: { enabled } } }).ok).toBe(true);
3333
});
3434

35+
it.each([1, 5, 1024])("accepts worker session hosting capacity=%s", (capacity) => {
36+
expect(validateConfigObject({ nodeHost: { workerRuns: { capacity } } }).ok).toBe(true);
37+
});
38+
39+
it.each([0, -1, 1.5, 1025])("rejects invalid worker session hosting capacity=%s", (capacity) => {
40+
const result = validateConfigObject({ nodeHost: { workerRuns: { capacity } } });
41+
expect(result.ok).toBe(false);
42+
if (!result.ok) {
43+
expect(result.issues.some((issue) => issue.path === "nodeHost.workerRuns.capacity")).toBe(
44+
true,
45+
);
46+
}
47+
});
48+
3549
it("rejects non-boolean worker session hosting enablement", () => {
3650
const result = validateConfigObject({ nodeHost: { workerRuns: { enabled: "yes" } } });
3751
expect(result.ok).toBe(false);

‎src/config/schema.help.runtime.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,8 @@ export const RUNTIME_FIELD_HELP: Record<string, string> = {
284284
"Opt in to full OpenClaw worker session hosting from Gateway-managed bundles. Disabled by default.",
285285
"nodeHost.workerRuns.enabled":
286286
"Allow this paired node to host sessions from exact bundles installed by its Gateway (default: false).",
287+
"nodeHost.workerRuns.capacity":
288+
"Maximum concurrent worker sessions hosted by this node (default: the number of available CPU cores). Must be an integer from 1 through 1024.",
287289
"nodeHost.browserProxy":
288290
"Groups browser-proxy settings for exposing local browser control through node routing. Enable only when remote node workflows need your local browser profiles.",
289291
"nodeHost.browserProxy.enabled":

‎src/config/schema.labels.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,7 @@ export const FIELD_LABELS: Record<string, string> = {
409409
"nodeHost.agentRuns.claude.enabled": "Node Claude Agent Runs Enabled",
410410
"nodeHost.workerRuns": "Node Worker Runs",
411411
"nodeHost.workerRuns.enabled": "Node Worker Runs Enabled",
412+
"nodeHost.workerRuns.capacity": "Node Worker Run Capacity",
412413
"nodeHost.browserProxy": "Node Browser Proxy",
413414
"nodeHost.browserProxy.enabled": "Node Browser Proxy Enabled",
414415
"nodeHost.browserProxy.allowProfiles": "Node Browser Proxy Allowed Profiles",

‎src/config/types.node-host.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ export type NodeHostConfig = {
1919
workerRuns?: {
2020
/** Allow this paired node to host worker sessions (default: false). */
2121
enabled?: boolean;
22+
/** Integer worker slots (default: one per available CPU core). */
23+
capacity?: number;
2224
};
2325
/** Browser proxy settings for node hosts. */
2426
browserProxy?: NodeHostBrowserProxyConfig;

‎src/config/zod-schema.node-host.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { z } from "zod";
2+
import { NODE_WORKER_CAPACITY_MAX } from "../infra/node-runner-inventory.js";
23

34
export const BrowserSnapshotDefaultsSchema = z
45
.object({
@@ -22,6 +23,7 @@ export const NodeHostAgentRunsSchema = z
2223
export const NodeHostWorkerRunsSchema = z
2324
.object({
2425
enabled: z.boolean().optional(),
26+
capacity: z.number().int().min(1).max(NODE_WORKER_CAPACITY_MAX).optional(),
2527
})
2628
.strict()
2729
.optional();

‎src/node-host/node-worker-capacity.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os from "node:os";
12
import { NODE_WORKER_CAPACITY_EXHAUSTED_ERROR_CODE } from "../infra/node-commands.js";
23
import {
34
NODE_WORKER_CAPACITY_MAX,
@@ -14,7 +15,6 @@ import {
1415
type NodeWorkerProcessIdentity,
1516
} from "./node-worker-process-identity.js";
1617

17-
const DEFAULT_WORKER_CAPACITY = 2;
1818
const DEFAULT_CAPACITY_WAIT_MS = 10_000;
1919
const CAPACITY_POLL_MS = 100;
2020

@@ -30,6 +30,12 @@ function capacityAbortReason(signal: AbortSignal): Error {
3030
: new Error("node worker admission aborted");
3131
}
3232

33+
function resolveDefaultWorkerCapacity(): number {
34+
const availableParallelism =
35+
typeof os.availableParallelism === "function" ? os.availableParallelism() : os.cpus().length;
36+
return Math.min(NODE_WORKER_CAPACITY_MAX, Math.max(1, availableParallelism));
37+
}
38+
3339
export class NodeWorkerCapacityExhaustedError extends Error {
3440
readonly code = NODE_WORKER_CAPACITY_EXHAUSTED_ERROR_CODE;
3541

@@ -52,7 +58,7 @@ export class NodeWorkerCapacity {
5258
private readonly store: NodeWorkerLaunchStore,
5359
options: NodeWorkerCapacityOptions = {},
5460
) {
55-
this.capacity = options.capacity ?? DEFAULT_WORKER_CAPACITY;
61+
this.capacity = options.capacity ?? resolveDefaultWorkerCapacity();
5662
this.waitMs = options.capacityWaitMs ?? DEFAULT_CAPACITY_WAIT_MS;
5763
this.onCapacityChanged = options.onCapacityChanged;
5864
if (

0 commit comments

Comments
 (0)