Skip to content

Commit d581993

Browse files
committed
feat(doctor): report unusable cloud worker profiles
1 parent 0e7f320 commit d581993

9 files changed

Lines changed: 524 additions & 6 deletions
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import path from "node:path";
2+
import { fileURLToPath } from "node:url";
3+
import type { HealthCheck } from "openclaw/plugin-sdk/health";
4+
import { resolveOpenClawRoot } from "./src/crabbox-worker-profile.js";
5+
import {
6+
CRABBOX_CLOUD_WORKER_PROFILE_CHECK_ID,
7+
registerCrabboxWorkerProviderDoctorChecks as registerChecks,
8+
} from "./src/doctor.js";
9+
10+
const CRABBOX_PLUGIN_ROOT = path.dirname(fileURLToPath(import.meta.url));
11+
12+
export { CRABBOX_CLOUD_WORKER_PROFILE_CHECK_ID };
13+
14+
export function registerWorkerProviderDoctorChecks(host: {
15+
registerHealthCheck(check: HealthCheck): void;
16+
}): void {
17+
registerChecks({
18+
...host,
19+
openclawRoot: resolveOpenClawRoot(CRABBOX_PLUGIN_ROOT),
20+
});
21+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { runCommandWithTimeout, type SpawnResult } from "openclaw/plugin-sdk/process-runtime";
2+
3+
const CRABBOX_VERSION_TIMEOUT_MS = 2_000;
4+
const CRABBOX_VERSION_MAX_OUTPUT_BYTES = 64 * 1024;
5+
6+
type CrabboxVersionProbe =
7+
| { status: "supported"; version: string }
8+
| { status: "outdated"; version: string }
9+
| { status: "indeterminate"; reason: string };
10+
11+
export async function probeCrabboxVersion(binary: string): Promise<CrabboxVersionProbe> {
12+
let result: SpawnResult;
13+
try {
14+
result = await runCommandWithTimeout([binary, "--version"], {
15+
killProcessTree: true,
16+
maxOutputBytes: CRABBOX_VERSION_MAX_OUTPUT_BYTES,
17+
timeoutMs: CRABBOX_VERSION_TIMEOUT_MS,
18+
});
19+
} catch {
20+
return { status: "indeterminate", reason: "version command could not start" };
21+
}
22+
if (result.termination !== "exit" || result.code !== 0 || result.outputLimitExceeded) {
23+
const reason =
24+
result.termination === "timeout"
25+
? `version command timed out after ${CRABBOX_VERSION_TIMEOUT_MS} ms`
26+
: result.outputLimitExceeded
27+
? "version output exceeded 64 KiB"
28+
: result.termination !== "exit"
29+
? `version command did not exit normally (${result.termination})`
30+
: `version command exited with code ${result.code ?? "unknown"}`;
31+
return { status: "indeterminate", reason };
32+
}
33+
const match = /(?:^|\s)v?(\d+)\.(\d+)\.(\d+)(?:\s|$)/u.exec(
34+
`${result.stdout}\n${result.stderr}`.trim(),
35+
);
36+
if (!match) {
37+
return { status: "indeterminate", reason: "version output was not recognized" };
38+
}
39+
const major = Number(match[1]);
40+
const minor = Number(match[2]);
41+
const patch = Number(match[3]);
42+
const version = `${major}.${minor}.${patch}`;
43+
const supported = major > 0 || (major === 0 && (minor > 41 || (minor === 41 && patch >= 1)));
44+
return supported ? { status: "supported", version } : { status: "outdated", version };
45+
}

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ const CRABBOX_MACHINE_OPTIONS = [
5050

5151
type IsExecutable = (candidate: string) => boolean;
5252

53+
export const CRABBOX_WORKER_PROVIDER_ID = "crabbox";
54+
5355
function requirePositiveDuration(value: unknown, key: string): string {
5456
const duration = nonEmptyString(value);
5557
if (!duration || parsePositiveGoDurationNanoseconds(duration) === undefined) {
@@ -230,9 +232,22 @@ export function resolveCrabboxBinary(params: {
230232
if (params.explicit) {
231233
return params.explicit;
232234
}
235+
return findCrabboxBinary(params) ?? "crabbox";
236+
}
237+
238+
export function findCrabboxBinary(params: {
239+
explicit?: string;
240+
isExecutable?: IsExecutable;
241+
openclawRoot: string;
242+
pathEnv?: string;
243+
platform?: NodeJS.Platform;
244+
}): string | undefined {
233245
const platform = params.platform ?? process.platform;
234246
const isExecutable =
235247
params.isExecutable ?? ((candidate) => defaultIsExecutable(candidate, platform));
248+
if (params.explicit) {
249+
return isExecutable(params.explicit) ? params.explicit : undefined;
250+
}
236251
const siblingBase = path.resolve(params.openclawRoot, "../crabbox/bin/crabbox");
237252
for (const candidate of binaryCandidates(siblingBase, platform)) {
238253
if (isExecutable(candidate)) {
@@ -252,7 +267,7 @@ export function resolveCrabboxBinary(params: {
252267
}
253268
}
254269
}
255-
return "crabbox";
270+
return undefined;
256271
}
257272

258273
export function resolveOpenClawRoot(pluginRoot: string | undefined): string {

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

Lines changed: 173 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
11
import path from "node:path";
2+
import type { HealthCheck } from "openclaw/plugin-sdk/health";
23
import type { WorkerProfile, WorkerProvider } from "openclaw/plugin-sdk/plugin-entry";
4+
import * as processRuntime from "openclaw/plugin-sdk/process-runtime";
35
import type { SpawnResult } from "openclaw/plugin-sdk/process-runtime";
46
import { describe, expect, it, vi } from "vitest";
5-
import { operationLeaseId, resolveCrabboxBinary } from "./crabbox-worker-profile.js";
7+
import * as doctorRuntime from "./crabbox-worker-doctor-runtime.js";
8+
import {
9+
findCrabboxBinary,
10+
operationLeaseId,
11+
resolveCrabboxBinary,
12+
} from "./crabbox-worker-profile.js";
613
import { createCrabboxWorkerProvider, resolveOpenClawRoot } from "./crabbox-worker-provider.js";
14+
import {
15+
CRABBOX_CLOUD_WORKER_PROFILE_CHECK_ID,
16+
registerCrabboxWorkerProviderDoctorChecks,
17+
} from "./doctor.js";
718

819
const OPERATION_ID = `provision:v2:${"0".repeat(64)}`;
920
const LEASE_ID = "cbx_6071fc2062a6";
@@ -1675,6 +1686,25 @@ describe("Crabbox binary resolution", () => {
16751686
).toBe("crabbox");
16761687
});
16771688

1689+
it("distinguishes executable discovery from the dispatch fallback", () => {
1690+
const explicitBinary = path.resolve(path.sep, "custom", "crabbox");
1691+
1692+
expect(
1693+
findCrabboxBinary({
1694+
explicit: explicitBinary,
1695+
openclawRoot: OPENCLAW_ROOT,
1696+
isExecutable: () => false,
1697+
}),
1698+
).toBeUndefined();
1699+
expect(
1700+
findCrabboxBinary({
1701+
openclawRoot: OPENCLAW_ROOT,
1702+
pathEnv: path.resolve(path.sep, "not-executable"),
1703+
isExecutable: () => false,
1704+
}),
1705+
).toBeUndefined();
1706+
});
1707+
16781708
it("derives the package root from source and bundled plugin roots", () => {
16791709
expect(resolveOpenClawRoot(path.join(OPENCLAW_ROOT, "extensions", "crabbox"))).toBe(
16801710
OPENCLAW_ROOT,
@@ -1684,3 +1714,145 @@ describe("Crabbox binary resolution", () => {
16841714
);
16851715
});
16861716
});
1717+
1718+
function captureCrabboxDoctorCheck(): HealthCheck {
1719+
let check: HealthCheck | undefined;
1720+
registerCrabboxWorkerProviderDoctorChecks({
1721+
openclawRoot: OPENCLAW_ROOT,
1722+
registerHealthCheck(value) {
1723+
check = value;
1724+
},
1725+
});
1726+
if (!check) {
1727+
throw new Error("Crabbox doctor check was not registered");
1728+
}
1729+
return check;
1730+
}
1731+
1732+
describe("Crabbox worker doctor", () => {
1733+
it("reports a configured non-executable binary with a profile-specific repair", async () => {
1734+
const probe = vi.spyOn(doctorRuntime, "probeCrabboxVersion");
1735+
const binary = path.resolve(path.sep, "nonexistent", "crabbox");
1736+
try {
1737+
await expect(
1738+
captureCrabboxDoctorCheck().detect({
1739+
cfg: {
1740+
cloudWorkers: {
1741+
profiles: {
1742+
aws: { provider: "crabbox", settings: { binary } },
1743+
},
1744+
},
1745+
},
1746+
env: { PATH: "" },
1747+
} as never),
1748+
).resolves.toEqual([
1749+
expect.objectContaining({
1750+
checkId: CRABBOX_CLOUD_WORKER_PROFILE_CHECK_ID,
1751+
severity: "warning",
1752+
message: expect.stringContaining('profile "aws"'),
1753+
path: binary,
1754+
target: "aws",
1755+
fixHint: expect.stringContaining("cloudWorkers.profiles.aws.settings.binary"),
1756+
}),
1757+
]);
1758+
expect(probe).not.toHaveBeenCalled();
1759+
} finally {
1760+
probe.mockRestore();
1761+
}
1762+
});
1763+
1764+
it("emits no finding for a supported configured binary", async () => {
1765+
const probe = vi
1766+
.spyOn(doctorRuntime, "probeCrabboxVersion")
1767+
.mockResolvedValue({ status: "supported", version: "0.41.6" });
1768+
try {
1769+
await expect(
1770+
captureCrabboxDoctorCheck().detect({
1771+
cfg: {
1772+
cloudWorkers: {
1773+
profiles: {
1774+
aws: { provider: "crabbox", settings: { binary: process.execPath } },
1775+
},
1776+
},
1777+
},
1778+
} as never),
1779+
).resolves.toEqual([]);
1780+
expect(probe).toHaveBeenCalledOnce();
1781+
} finally {
1782+
probe.mockRestore();
1783+
}
1784+
});
1785+
1786+
it("reports an indeterminate version probe without asserting failure", async () => {
1787+
const probe = vi.spyOn(doctorRuntime, "probeCrabboxVersion").mockResolvedValue({
1788+
status: "indeterminate",
1789+
reason: "version command timed out after 2000 ms",
1790+
});
1791+
try {
1792+
await expect(
1793+
captureCrabboxDoctorCheck().detect({
1794+
cfg: {
1795+
cloudWorkers: {
1796+
profiles: {
1797+
aws: { provider: "crabbox", settings: { binary: process.execPath } },
1798+
},
1799+
},
1800+
},
1801+
} as never),
1802+
).resolves.toEqual([
1803+
expect.objectContaining({
1804+
severity: "info",
1805+
message: expect.stringContaining("could not determine its version"),
1806+
fixHint: expect.stringContaining(`${process.execPath} --version`),
1807+
}),
1808+
]);
1809+
} finally {
1810+
probe.mockRestore();
1811+
}
1812+
});
1813+
1814+
it("does not probe when no Crabbox cloud worker profile is configured", async () => {
1815+
const probe = vi.spyOn(doctorRuntime, "probeCrabboxVersion");
1816+
try {
1817+
await expect(captureCrabboxDoctorCheck().detect({ cfg: {} } as never)).resolves.toEqual([]);
1818+
expect(probe).not.toHaveBeenCalled();
1819+
} finally {
1820+
probe.mockRestore();
1821+
}
1822+
});
1823+
});
1824+
1825+
describe("Crabbox version probe", () => {
1826+
it.each([
1827+
{ output: "0.41.1\n", expected: { status: "supported", version: "0.41.1" } },
1828+
{ output: "crabbox 0.41.6\n", expected: { status: "supported", version: "0.41.6" } },
1829+
{ output: "0.40.9\n", expected: { status: "outdated", version: "0.40.9" } },
1830+
])("classifies $output", async ({ output, expected }) => {
1831+
const run = vi
1832+
.spyOn(processRuntime, "runCommandWithTimeout")
1833+
.mockResolvedValue(commandResult({ stdout: output }));
1834+
try {
1835+
await expect(doctorRuntime.probeCrabboxVersion("/opt/crabbox")).resolves.toEqual(expected);
1836+
expect(run).toHaveBeenCalledWith(
1837+
["/opt/crabbox", "--version"],
1838+
expect.objectContaining({ timeoutMs: 2_000, killProcessTree: true }),
1839+
);
1840+
} finally {
1841+
run.mockRestore();
1842+
}
1843+
});
1844+
1845+
it("turns timeout into an indeterminate result", async () => {
1846+
const run = vi
1847+
.spyOn(processRuntime, "runCommandWithTimeout")
1848+
.mockResolvedValue(commandResult({ code: 124, termination: "timeout" }));
1849+
try {
1850+
await expect(doctorRuntime.probeCrabboxVersion("/opt/crabbox")).resolves.toEqual({
1851+
status: "indeterminate",
1852+
reason: "version command timed out after 2000 ms",
1853+
});
1854+
} finally {
1855+
run.mockRestore();
1856+
}
1857+
});
1858+
});

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { createCrabboxHeartbeatManager } from "./crabbox-worker-heartbeat.js";
2323
import { parseInspectJson, type ParsedInspect } from "./crabbox-worker-inspect.js";
2424
import {
2525
buildCrabboxWarmupArgs,
26+
CRABBOX_WORKER_PROVIDER_ID,
2627
listCrabboxMachineOptions,
2728
nonEmptyString,
2829
operationLeaseId,
@@ -42,8 +43,6 @@ import {
4243

4344
export { resolveOpenClawRoot } from "./crabbox-worker-profile.js";
4445

45-
const CRABBOX_WORKER_PROVIDER_ID = "crabbox";
46-
4746
const READY_POLL_INTERVAL_MS = 2_000;
4847
const MAX_ERROR_DETAIL_CHARS = 512;
4948
const CLOUD_SETUP_CODE_ENV = "CRABBOX_WORKER_SETUP_CODE";

0 commit comments

Comments
 (0)