Skip to content

Commit 9e6c155

Browse files
committed
fix: identify cloud worker bootstrap failures
1 parent d2948c7 commit 9e6c155

4 files changed

Lines changed: 35 additions & 26 deletions

File tree

‎extensions/crabbox/src/crabbox-worker-command-error.ts‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { redactSensitiveText } from "openclaw/plugin-sdk/logging-core";
2-
import { WorkerProviderError } from "openclaw/plugin-sdk/plugin-entry";
32
import type { SpawnResult } from "openclaw/plugin-sdk/process-runtime";
43
import { sliceUtf16Safe } from "openclaw/plugin-sdk/text-utility-runtime";
54

@@ -30,10 +29,3 @@ export function crabboxCommandError(action: string, result: SpawnResult): Error
3029
`Crabbox ${action} failed with exit code ${exitCode}${crabboxCommandDetail(result)}`,
3130
);
3231
}
33-
34-
export function permanentCrabboxCommandError(
35-
action: string,
36-
result: SpawnResult,
37-
): WorkerProviderError {
38-
return new WorkerProviderError(crabboxCommandError(action, result).message);
39-
}

‎extensions/crabbox/src/crabbox-worker-node-enrollment.test.ts‎

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ if (args[0] === "--version") {
5757
fs.appendFileSync(path.join(state, "enabled"), args[2] + "\\n");
5858
} else {
5959
process.title = "openclaw-connect";
60-
fs.writeFileSync(path.join(state, "launch.json"), JSON.stringify({ build: ${JSON.stringify(build)}, args, cli: process.argv[1], token: process.env.CRABBOX_WORKER_BOOTSTRAP_TOKEN, setupCode: process.env.CRABBOX_WORKER_SETUP_CODE, environment: { DISPLAY: process.env.DISPLAY, DBUS_SESSION_BUS_ADDRESS: process.env.DBUS_SESSION_BUS_ADDRESS, XDG_RUNTIME_DIR: process.env.XDG_RUNTIME_DIR }, enabledPlugins: fs.readFileSync(path.join(state, "enabled"), "utf8").trim().split("\\n") }));
60+
fs.writeFileSync(path.join(state, "launch.json.tmp"), JSON.stringify({ build: ${JSON.stringify(build)}, args, cli: process.argv[1], token: process.env.CRABBOX_WORKER_BOOTSTRAP_TOKEN, setupCode: process.env.CRABBOX_WORKER_SETUP_CODE, environment: { DISPLAY: process.env.DISPLAY, DBUS_SESSION_BUS_ADDRESS: process.env.DBUS_SESSION_BUS_ADDRESS, XDG_RUNTIME_DIR: process.env.XDG_RUNTIME_DIR }, enabledPlugins: fs.readFileSync(path.join(state, "enabled"), "utf8").trim().split("\\n") }));
61+
// Existence signals readiness only after the child publishes complete JSON.
62+
fs.renameSync(path.join(state, "launch.json.tmp"), path.join(state, "launch.json"));
6163
setInterval(() => {}, 60000);
6264
}
6365
`,
@@ -88,7 +90,10 @@ function testHome() {
8890
return { home, stateDir, stop };
8991
}
9092

91-
async function serveArtifact(archive: Buffer, options: { tls?: boolean; redirect?: boolean } = {}) {
93+
async function serveArtifact(
94+
archive: Buffer,
95+
options: { tls?: boolean; redirect?: boolean; truncate?: boolean } = {},
96+
) {
9297
let tls: { cert: Buffer; key: Buffer } | undefined;
9398
if (options.tls) {
9499
const directory = tempDirs.make("crabbox-bootstrap-tls-");
@@ -124,6 +129,10 @@ async function serveArtifact(archive: Buffer, options: { tls?: boolean; redirect
124129
return;
125130
}
126131
response.writeHead(200, { "content-length": archive.length });
132+
if (options.truncate) {
133+
response.write(archive.subarray(0, 1), () => response.destroy());
134+
return;
135+
}
127136
response.end(archive);
128137
};
129138
const server = tls ? https.createServer(tls, handle) : http.createServer(handle);
@@ -315,28 +324,33 @@ describe.skipIf(process.platform === "win32")("source node bootstrap", () => {
315324
30_000,
316325
);
317326

318-
it.each(["digest", "length", "redirect"] as const)(
327+
it.each([
328+
["digest", "integrity verification"],
329+
["length", "length does not match"],
330+
["redirect", "HTTP 302"],
331+
["truncated", "bootstrap download failed (ECONNRESET): aborted"],
332+
] as const)(
319333
"rejects a bad archive %s before installing or starting a node",
320-
async (failure) => {
334+
async (failure, diagnosis) => {
321335
const { home, stateDir } = testHome();
322336
const archive = await packageFixture("first");
323-
const { nodeBootstrap } = await serveArtifact(archive, { redirect: failure === "redirect" });
337+
const { nodeBootstrap, authorizations } = await serveArtifact(archive, {
338+
redirect: failure === "redirect",
339+
truncate: failure === "truncated",
340+
});
324341
const result = await enroll(home, {
325342
...nodeBootstrap,
326343
...(failure === "digest" ? { sha256: "f".repeat(64) } : {}),
327344
...(failure === "length" ? { bytes: archive.length + 1 } : {}),
328345
});
329346
expect(result.code).toBe(1);
330-
expect(result.output).toContain(
331-
failure === "digest"
332-
? "integrity verification"
333-
: failure === "length"
334-
? "length does not match"
335-
: "HTTP 302",
336-
);
347+
expect(result.output).toContain(diagnosis);
337348
expect(result.output).not.toContain(nodeBootstrap.token);
349+
expect(result.output).not.toContain(setupCode);
350+
expect(authorizations).toEqual([`Bearer ${nodeBootstrap.token}`]);
338351
expect(fs.existsSync(path.join(stateDir, "node.pid"))).toBe(false);
339352
expect(fs.readdirSync(stateDir)).toEqual([]);
353+
expect(fs.readdirSync(path.join(home, ".openclaw-worker", "node-runtimes"))).toEqual([]);
340354
},
341355
);
342356

‎extensions/crabbox/src/crabbox-worker-node-enrollment.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ const setupCode = process.env.${CLOUD_SETUP_CODE_ENV};
4747
delete process.env.${CLOUD_BOOTSTRAP_TOKEN_ENV};
4848
delete process.env.${CLOUD_SETUP_CODE_ENV};
4949
process.umask(0o077);
50+
let phase = "preparation";
5051
(async () => {
5152
const stateDir = path.join(os.homedir(), ".openclaw", "cloud-workers", leaseId);
5253
const runtimeRoot = path.join(os.homedir(), ".openclaw-worker", "node-runtimes");
@@ -85,6 +86,7 @@ process.umask(0o077);
8586
fs.unlinkSync(pidFile);
8687
}
8788
const verifyRuntime = (root) => {
89+
phase = "runtime verification";
8890
if (!fs.lstatSync(root).isDirectory() || fs.realpathSync(root) !== root) throw new Error("Cloud worker bootstrap runtime path is unsafe");
8991
const packageRoot = path.join(root, "node_modules", "openclaw");
9092
const manifest = JSON.parse(fs.readFileSync(path.join(packageRoot, "package.json"), "utf8"));
@@ -100,6 +102,7 @@ process.umask(0o077);
100102
} else {
101103
const stage = fs.mkdtempSync(path.join(stateDir, "node-bootstrap-"));
102104
try {
105+
phase = "download";
103106
if (!token) throw new Error("Cloud worker bootstrap download authority is unavailable");
104107
const url = new URL(bootstrap.url);
105108
if (!["http:", "https:"].includes(url.protocol) || url.username || url.password || url.search || url.hash || (bootstrap.tlsFingerprint && url.protocol !== "https:")) throw new Error("Cloud worker bootstrap artifact transport is invalid");
@@ -137,6 +140,7 @@ process.umask(0o077);
137140
}
138141
} finally { response.destroy(); await output.close(); }
139142
if (bytes !== bootstrap.bytes || hash.digest("hex") !== bootstrap.sha256) throw new Error("Cloud worker bootstrap archive failed integrity verification");
143+
phase = "installation";
140144
const installDir = path.join(stage, "runtime");
141145
fs.mkdirSync(installDir, { mode: 0o700 });
142146
// npm 12 requires a project policy even when ignore-scripts is false.
@@ -157,6 +161,7 @@ process.umask(0o077);
157161
fs.renameSync(installDir, runtimeDir);
158162
} finally { fs.rmSync(stage, { recursive: true, force: true }); }
159163
}
164+
phase = "activation";
160165
try {
161166
if (!fs.lstatSync(runtimeLink).isSymbolicLink()) throw new Error("Cloud worker runtime pointer is occupied");
162167
fs.unlinkSync(runtimeLink);
@@ -171,6 +176,7 @@ process.umask(0o077);
171176
fs.writeFileSync(setupFile, setupCode + "\\n", { mode: 0o600 });
172177
}
173178
const args = mode === "connect" ? ["connect", "--target-file", setupFile] : ["node", "run"];
179+
phase = "node launch";
174180
const log = fs.openSync(path.join(stateDir, "node.log"), "a", 0o600);
175181
let child;
176182
try {
@@ -180,7 +186,7 @@ process.umask(0o077);
180186
catch (error) { process.kill(-child.pid, "SIGTERM"); throw error; }
181187
child.unref();
182188
} finally { fs.closeSync(log); }
183-
})().catch((error) => { console.error(error.message); process.exitCode = 1; });
189+
})().catch((error) => { console.error("Cloud worker node bootstrap " + phase + " failed" + (error.code ? " (" + error.code + ")" : "") + ": " + error.message); process.exitCode = 1; });
184190
CRABBOX_NODE_ENROLLMENT_SCRIPT`;
185191
return {
186192
command,

‎extensions/crabbox/src/crabbox-worker-provider.ts‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@ import { createPluginStateSyncKeyedStore } from "openclaw/plugin-sdk/plugin-stat
1010
import { runCommandWithTimeout } from "openclaw/plugin-sdk/process-runtime";
1111
import { isRecord } from "openclaw/plugin-sdk/string-coerce-runtime";
1212
import { truncateUtf16Safe } from "openclaw/plugin-sdk/text-utility-runtime";
13-
import {
14-
crabboxCommandError,
15-
permanentCrabboxCommandError,
16-
} from "./crabbox-worker-command-error.js";
13+
import { crabboxCommandError } from "./crabbox-worker-command-error.js";
1714
import {
1815
type CrabboxCommandRunner,
1916
isUnrecognizedLease,
@@ -329,7 +326,7 @@ async function runProvisionSetupAndWaitReady(
329326
}),
330327
);
331328
if (result.termination !== "exit" || result.code !== 0) {
332-
throw permanentCrabboxCommandError(params.phase, result);
329+
throw new WorkerProviderError(crabboxCommandError(params.phase, result).message);
333330
}
334331
} catch (error) {
335332
return await failProvisionAfterCleanup({ ...params, id: params.inspect.id }, error);

0 commit comments

Comments
 (0)