Skip to content

Commit f19f62c

Browse files
authored
perf(node-host): prewarm worker bundles (#124427)
* perf(node-host): prewarm worker bundles * test(gateway): gate worker finalization responsiveness * fix(node-host): negotiate bundle prewarming * fix(protocol): refresh worker prewarm models * fix(node-host): preserve prewarm upgrade compatibility
1 parent a2e27de commit f19f62c

23 files changed

Lines changed: 612 additions & 31 deletions

‎apps/shared/OpenClawKit/Sources/OpenClawProtocol/GatewayModels.swift‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1207,21 +1207,25 @@ public struct WorkerAdmissionHandshake: Codable, Sendable {
12071207
public let bundlehash: String
12081208
public let openclawversion: String
12091209
public let protocolfeatures: [String]
1210+
public let bundleprewarm: Int?
12101211

12111212
public init(
12121213
bundlehash: String,
12131214
openclawversion: String,
1214-
protocolfeatures: [String])
1215+
protocolfeatures: [String],
1216+
bundleprewarm: Int? = nil)
12151217
{
12161218
self.bundlehash = bundlehash
12171219
self.openclawversion = openclawversion
12181220
self.protocolfeatures = protocolfeatures
1221+
self.bundleprewarm = bundleprewarm
12191222
}
12201223

12211224
private enum CodingKeys: String, CodingKey {
12221225
case bundlehash = "bundleHash"
12231226
case openclawversion = "openclawVersion"
12241227
case protocolfeatures = "protocolFeatures"
1228+
case bundleprewarm = "bundlePrewarm"
12251229
}
12261230
}
12271231

‎packages/gateway-protocol/src/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ export {
108108
WorkerTranscriptCommitResponseFrameSchema,
109109
WorkerTranscriptCommitResultSchema,
110110
WorkerTranscriptMessageSchema,
111+
WORKER_BUNDLE_PREWARM_VERSION,
111112
WORKER_HEARTBEAT_INTERVAL_MS,
112113
WORKER_LAUNCH_V2_PROTOCOL_FEATURE,
113114
WORKER_LIVE_EVENT_PROTOCOL_FEATURE,

‎packages/gateway-protocol/src/schema/frames.worker-runs.test.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,28 @@ describe("node worker-runs connect manifest", () => {
1919
},
2020
}),
2121
).toBe(true);
22+
expect(
23+
validateConnectParams({
24+
...connect,
25+
workerRuns: {
26+
bundleHash: "a".repeat(64),
27+
openclawVersion: "2026.8.12",
28+
protocolFeatures: ["worker-heartbeat-v1"],
29+
bundlePrewarm: 2,
30+
},
31+
}),
32+
).toBe(true);
2233
expect(validateConnectParams({ ...connect, workerRuns: { enabled: true } })).toBe(false);
34+
expect(
35+
validateConnectParams({
36+
...connect,
37+
workerRuns: {
38+
bundleHash: "a".repeat(64),
39+
openclawVersion: "2026.8.12",
40+
protocolFeatures: ["worker-heartbeat-v1"],
41+
bundlePrewarm: 0,
42+
},
43+
}),
44+
).toBe(false);
2345
});
2446
});

‎packages/gateway-protocol/src/schema/worker-admission.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export {
2929

3030
// Additive RPCs require exact build-bound features; bump only for an incompatible base set.
3131
export const WORKER_RPC_SET_VERSION = 1;
32+
export const WORKER_BUNDLE_PREWARM_VERSION = 1;
3233
export const WORKER_HEARTBEAT_INTERVAL_MS = 15_000;
3334
export const WORKER_PROTOCOL_METHODS = [
3435
"worker.heartbeat",
@@ -86,6 +87,7 @@ export const WorkerAdmissionHandshakeSchema = withSince(
8687
maxItems: WORKER_PROTOCOL_MAX_FEATURES,
8788
uniqueItems: true,
8889
}),
90+
bundlePrewarm: Type.Optional(Type.Integer({ minimum: 1, maximum: Number.MAX_SAFE_INTEGER })),
8991
}),
9092
);
9193

‎scripts/check-gateway-cpu-scenarios.mts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,23 @@ async function runGatewayCpuScenarios(
488488
privateQaBuildFailed = privateQaBuild.status !== 0;
489489
}
490490

491+
if (!options.skipQa) {
492+
steps.push(
493+
privateQaBuildFailed
494+
? { name: "node worker finalization gate", signal: null, status: 1 }
495+
: runStep(
496+
"node worker finalization gate",
497+
process.execPath,
498+
[
499+
"scripts/run-vitest.mjs",
500+
"test/e2e/qa-lab/runtime/node-worker-launch-wire.e2e.test.ts",
501+
],
502+
{ env: qaBuildEnv },
503+
params,
504+
),
505+
);
506+
}
507+
491508
let qaStep = null;
492509
if (!options.skipQa) {
493510
const qaCommand = pnpmCommand(

‎src/gateway/node-registry-private.ts‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,13 @@ function sameOptionalWorkerBuild(
204204
return left === undefined || right === undefined ? left === right : sameWorkerBuild(left, right);
205205
}
206206

207+
function sameOptionalWorkerRuns(
208+
left: WorkerAdmissionHandshake | undefined,
209+
right: WorkerAdmissionHandshake | undefined,
210+
): boolean {
211+
return sameOptionalWorkerBuild(left, right) && left?.bundlePrewarm === right?.bundlePrewarm;
212+
}
213+
207214
function resolveWorkerSupervisorProof(
208215
node: NodeRegistryPrivateSession,
209216
runnerInventoryByConn: ReadonlyMap<string, NodeRunnerInventoryRecord>,
@@ -276,7 +283,7 @@ function isWorkerSupervisorProofCurrent(
276283
(!requireLaunchEligibility ||
277284
(current.workerRuns !== undefined &&
278285
proof.workerRuns !== undefined &&
279-
sameWorkerBuild(current.workerRuns, proof.workerRuns)))
286+
sameOptionalWorkerRuns(current.workerRuns, proof.workerRuns)))
280287
);
281288
}
282289

@@ -322,7 +329,7 @@ function updateWorkerRunnerInventory(
322329
const changed =
323330
!previous ||
324331
!sameWorkerProtocolFeatures(previous.protocolFeatures, next.protocolFeatures) ||
325-
!sameOptionalWorkerBuild(previous.workerRuns, next.workerRuns);
332+
!sameOptionalWorkerRuns(previous.workerRuns, next.workerRuns);
326333
if (changed) {
327334
state.runnerInventoryByConn.set(node.connId, next);
328335
state.context.publishActiveNodeContext();

‎src/gateway/node-registry.test.ts‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,51 @@ describe("gateway/node-registry", () => {
736736
await expect(invocation).resolves.toMatchObject({ ok: true, payloadJSON: "null" });
737737
});
738738

739+
it("promotes post-hello worker capabilities without changing build identity", async () => {
740+
const { nodeRegistry, nodeWorkerSupervisorTransport } = createPrivateNodeRegistryRuntime();
741+
registerNodeSession(
742+
nodeRegistry,
743+
makeClient("conn-1", "node-1", [], {
744+
clientId: GATEWAY_CLIENT_IDS.NODE_HOST,
745+
commands: ["system.run"],
746+
workerRuns: WORKER_RUNS,
747+
}),
748+
{ pairingIdentity: "identity-a", pairingGeneration: "generation-a" },
749+
);
750+
const declaration = {
751+
protocolFeatures: [NODE_WORKER_SUPERVISOR_PROTOCOL_FEATURE] as const,
752+
workerRuns: WORKER_RUNS,
753+
};
754+
expect(
755+
updateNodeRunnerInventory({
756+
registry: nodeRegistry,
757+
nodeId: "node-1",
758+
connId: "conn-1",
759+
declaration,
760+
}),
761+
).toEqual({ changed: true });
762+
const [legacyProof] = await nodeWorkerSupervisorTransport.listCurrentNodes();
763+
764+
expect(
765+
updateNodeRunnerInventory({
766+
registry: nodeRegistry,
767+
nodeId: "node-1",
768+
connId: "conn-1",
769+
declaration: {
770+
...declaration,
771+
workerRuns: { ...WORKER_RUNS, bundlePrewarm: 1 },
772+
},
773+
}),
774+
).toEqual({ changed: true });
775+
const [negotiatedProof] = await nodeWorkerSupervisorTransport.listCurrentNodes();
776+
777+
expect(legacyProof && nodeWorkerSupervisorTransport.isCurrent(legacyProof, true)).toBe(false);
778+
expect(negotiatedProof?.workerRuns).toEqual({ ...WORKER_RUNS, bundlePrewarm: 1 });
779+
expect(negotiatedProof && nodeWorkerSupervisorTransport.isCurrent(negotiatedProof, true)).toBe(
780+
true,
781+
);
782+
});
783+
739784
it("rejects generation-mismatched lookup and dispatch without invalidating the session", async () => {
740785
const registry = new NodeRegistry();
741786
const frames: string[] = [];

‎src/gateway/worker-environments/node-worker-bundle-installer.test.ts‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,24 @@ const artifact = {
2929
tarballPath: "/gateway/bundle.tgz",
3030
};
3131

32+
function nodeProof(nodeId: string, bundlePrewarm?: number): NodeWorkerSupervisorNodeProof {
33+
return {
34+
...node,
35+
nodeId,
36+
connId: `conn-${nodeId}`,
37+
...(bundlePrewarm === undefined
38+
? {}
39+
: {
40+
workerRuns: {
41+
bundleHash: artifact.bundleHash,
42+
openclawVersion: artifact.openclawVersion,
43+
protocolFeatures: artifact.protocolFeatures,
44+
bundlePrewarm,
45+
},
46+
}),
47+
};
48+
}
49+
3250
describe("Gateway node worker bundle installer", () => {
3351
it("binds install dispatch to the current node proof and exact receipt", async () => {
3452
const transfer = createNodeWorkerBundleTransferService({
@@ -96,4 +114,37 @@ describe("Gateway node worker bundle installer", () => {
96114

97115
await expect(ensure({ deviceId: node.nodeId })).rejects.toThrow("mismatched build receipt");
98116
});
117+
118+
it("negotiates prewarming independently across a mixed node fleet", async () => {
119+
const transfer = createNodeWorkerBundleTransferService({
120+
generateToken: () => String.fromCharCode(65 + invoke.mock.calls.length).repeat(43),
121+
});
122+
const advertising = nodeProof("advertising", 7);
123+
const legacy = nodeProof("legacy");
124+
const invoke = vi.fn<NodeWorkerSupervisorTransport["invoke"]>(async (request) => ({
125+
ok: true,
126+
payloadJSON: JSON.stringify((request.params as { build: typeof artifact }).build),
127+
}));
128+
const transport: NodeWorkerSupervisorTransport = {
129+
listCurrentNodes: async () => [advertising, legacy],
130+
isCurrent: () => true,
131+
invoke,
132+
};
133+
const ensure = createGatewayNodeWorkerBundleInstaller({
134+
gatewayNamespace: "gateway-test",
135+
getTransport: () => transport,
136+
prepareBundle: async () => artifact,
137+
transfer,
138+
});
139+
140+
await expect(ensure({ deviceId: advertising.nodeId })).resolves.toMatchObject({
141+
bundleHash: artifact.bundleHash,
142+
});
143+
await expect(ensure({ deviceId: legacy.nodeId })).resolves.toMatchObject({
144+
bundleHash: artifact.bundleHash,
145+
});
146+
147+
expect(invoke.mock.calls[0]?.[0].params).toMatchObject({ bundlePrewarm: 1 });
148+
expect(invoke.mock.calls[1]?.[0].params).not.toHaveProperty("bundlePrewarm");
149+
});
99150
});

‎src/gateway/worker-environments/node-worker-bundle-installer.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { WORKER_BUNDLE_PREWARM_VERSION } from "../../../packages/gateway-protocol/src/schema/worker-admission.js";
12
import { NODE_WORKER_BUNDLE_INSTALL_COMMAND } from "../../infra/node-commands.js";
23
import { parseNodeWorkerBundleInstallResult } from "../../worker/node-bundle-install-protocol.js";
34
import type { NodeWorkerSupervisorTransport } from "../node-registry-private.js";
@@ -27,10 +28,15 @@ export function createGatewayNodeWorkerBundleInstaller(options: {
2728
}
2829
const artifact = await options.prepareBundle();
2930
const isAuthorized = () => transport.isCurrent(node);
31+
const bundlePrewarm =
32+
(node.workerRuns?.bundlePrewarm ?? 0) >= WORKER_BUNDLE_PREWARM_VERSION
33+
? WORKER_BUNDLE_PREWARM_VERSION
34+
: undefined;
3035
const prepared = options.transfer.prepare({
3136
node,
3237
gatewayNamespace: options.gatewayNamespace,
3338
artifact,
39+
...(bundlePrewarm ? { bundlePrewarm } : {}),
3440
isAuthorized,
3541
signal: params.signal,
3642
});

‎src/gateway/worker-environments/node-worker-bundle-transfer-service.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ export function createNodeWorkerBundleTransferService(
6666
node: NodeWorkerSupervisorNodeProof;
6767
gatewayNamespace: string;
6868
artifact: WorkerBundleArtifact;
69+
bundlePrewarm?: 1;
6970
isAuthorized: () => boolean;
7071
signal?: AbortSignal;
7172
}): { token: string; input: NodeWorkerBundleInstallInput } {
@@ -109,6 +110,7 @@ export function createNodeWorkerBundleTransferService(
109110
token,
110111
input: {
111112
gatewayNamespace: params.gatewayNamespace,
113+
...(params.bundlePrewarm ? { bundlePrewarm: params.bundlePrewarm } : {}),
112114
build: {
113115
bundleHash: params.artifact.bundleHash,
114116
openclawVersion: params.artifact.openclawVersion,

0 commit comments

Comments
 (0)