Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,26 @@
}
}
},
{
"includes": [
"src/lib/actions/sandbox/doctor.ts",
"src/lib/actions/sandbox/doctor-messaging.ts",
"src/lib/actions/sandbox/doctor-report.ts",
"src/lib/actions/sandbox/doctor-system-checks.ts"
],
"linter": {
"rules": {
"complexity": {
"noExcessiveCognitiveComplexity": {
"level": "error",
"options": {
"maxAllowedComplexity": 10
}
}
}
}
}
},
{
"includes": ["nemoclaw/src/**/*.ts"],
"linter": {
Expand Down
209 changes: 173 additions & 36 deletions src/lib/actions/sandbox/doctor-flow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,17 @@
const doctorModulePath = "./doctor.js";

function createDoctorHarness(): {
buildToolScopeChecksSpy: MockInstance;
captureOpenShellSpy: MockInstance;
captureHostCommandSpy: MockInstance;
getSandboxSpy: MockInstance;
getNamedGatewayLifecycleStateSpy: MockInstance;
healthProbeSpy: MockInstance;
inspectMutableConfigPermsSpy: MockInstance;
logSpy: MockInstance;
recoverNamedGatewayRuntimeSpy: MockInstance;
repairMutableConfigPermsSpy: MockInstance;
resolveOpenShellSpy: MockInstance;
runSandboxDoctor: RunSandboxDoctor;
} {
delete require.cache[requireDist.resolve(doctorModulePath)];
Expand Down Expand Up @@ -52,25 +59,39 @@
});
vi.spyOn(registry, "getConfiguredMessagingChannelsFromEntry").mockReturnValue([]);
vi.spyOn(registry, "getDisabledMessagingChannelsFromEntry").mockReturnValue([]);
vi.spyOn(resolve, "resolveOpenshell").mockReturnValue("/usr/bin/openshell");
const resolveOpenShellSpy = vi
.spyOn(resolve, "resolveOpenshell")
.mockReturnValue("/usr/bin/openshell");
vi.spyOn(gatewayBinding, "resolveSandboxGatewayName").mockReturnValue("nemoclaw-19080");
vi.spyOn(gatewayBinding, "resolveGatewayName").mockReturnValue("nemoclaw-19080");
vi.spyOn(dockerDriverPlatform, "isLinuxDockerDriverGatewayEnabled").mockReturnValue(true);
vi.spyOn(gatewayRuntime, "recoverNamedGatewayRuntime").mockResolvedValue({
before: { state: "healthy_named", status: "Status: Connected", gatewayInfo: "" },
after: { state: "healthy_named", status: "Status: Connected", gatewayInfo: "" },
recovered: false,
});
vi.spyOn(runtime, "captureOpenshell").mockImplementation((args: unknown) => {
const argv = Array.isArray(args) ? args : [];
if (argv[0] === "sandbox" && argv[1] === "list") {
return { status: 0, output: "alpha Ready" };
}
if (argv[0] === "inference" && argv[1] === "get") {
return { status: 0, output: "Provider: ollama-local\nModel: live-model\n" };
}
return { status: 0, output: "" };
});
const recoverNamedGatewayRuntimeSpy = vi
.spyOn(gatewayRuntime, "recoverNamedGatewayRuntime")
.mockResolvedValue({
before: { state: "healthy_named", status: "Status: Connected", gatewayInfo: "" },
after: { state: "healthy_named", status: "Status: Connected", gatewayInfo: "" },
recovered: false,
});
const getNamedGatewayLifecycleStateSpy = vi
.spyOn(gatewayRuntime, "getNamedGatewayLifecycleState")
.mockReturnValue({
state: "healthy_named",
status: "Status: Connected",
gatewayInfo: "Gateway: nemoclaw-19080",
activeGateway: "nemoclaw-19080",
});
const captureOpenShellSpy = vi
.spyOn(runtime, "captureOpenshell")
.mockImplementation((args: unknown) => {
const argv = Array.isArray(args) ? args : [];
if (argv[0] === "sandbox" && argv[1] === "list") {
return { status: 0, output: "alpha Ready" };
}
if (argv[0] === "inference" && argv[1] === "get") {
return { status: 0, output: "Provider: ollama-local\nModel: live-model\n" };
}
return { status: 0, output: "" };
});
const captureHostCommandSpy = vi
.spyOn(doctorHostCommand, "captureHostCommand")
.mockImplementation((command: unknown) => {
Expand All @@ -80,7 +101,7 @@
}
return { status: 0, stdout: "", stderr: "" };
});
vi.spyOn(health, "probeProviderHealth").mockReturnValue({
const healthProbeSpy = vi.spyOn(health, "probeProviderHealth").mockReturnValue({
ok: true,
probed: true,
providerLabel: "Ollama",
Expand All @@ -107,17 +128,19 @@
mode: "temporarily_unlocked",
detail: "temporarily unlocked for maintenance",
});
vi.spyOn(shields, "inspectMutableConfigPerms").mockReturnValue({
applies: true,
ok: true,
dirMode: "2770",
dirOwner: "sandbox:sandbox",
fileMode: "660",
fileOwner: "sandbox:sandbox",
configDir: "/sandbox/.openclaw",
configFile: "openclaw.json",
issues: [],
});
const inspectMutableConfigPermsSpy = vi
.spyOn(shields, "inspectMutableConfigPerms")
.mockReturnValue({
applies: true,
ok: true,
dirMode: "2770",
dirOwner: "sandbox:sandbox",
fileMode: "660",
fileOwner: "sandbox:sandbox",
configDir: "/sandbox/.openclaw",
configFile: "openclaw.json",
issues: [],
});
const repairMutableConfigPermsSpy = vi
.spyOn(shields, "repairMutableConfigPerms")
.mockReturnValue({
Expand All @@ -132,22 +155,31 @@
stdout: "ok",
stderr: "",
});
vi.spyOn(doctorToolScope, "buildToolScopeChecks").mockReturnValue([
{
group: "Sandbox",
label: "Tool scope approvals",
status: "ok",
detail: "no pending approvals",
},
]);
const buildToolScopeChecksSpy = vi
.spyOn(doctorToolScope, "buildToolScopeChecks")
.mockReturnValue([
{
group: "Sandbox",
label: "Tool scope approvals",
status: "ok",
detail: "no pending approvals",
},
]);

logSpy.mockClear();

return {
buildToolScopeChecksSpy,
captureOpenShellSpy,
captureHostCommandSpy,
getSandboxSpy,
getNamedGatewayLifecycleStateSpy,
healthProbeSpy,
inspectMutableConfigPermsSpy,
logSpy,
recoverNamedGatewayRuntimeSpy,
repairMutableConfigPermsSpy,
resolveOpenShellSpy,
runSandboxDoctor: requireDist(doctorModulePath).runSandboxDoctor,
};
}
Expand All @@ -166,7 +198,7 @@
delete require.cache[requireDist.resolve(doctorModulePath)];
});

it("builds a JSON report with host, gateway, sandbox, inference, messaging, and local-service checks", async () => {

Check failure on line 201 in src/lib/actions/sandbox/doctor-flow.test.ts

View workflow job for this annotation

GitHub Actions / cli-test-shards (2)

[cli] src/lib/actions/sandbox/doctor-flow.test.ts > runSandboxDoctor flow > builds a JSON report with host, gateway, sandbox, inference, messaging, and local-service checks

Error: Test timed out in 5000ms. If this is a long-running test, pass a timeout value as the last argument or configure it globally with "testTimeout". ❯ src/lib/actions/sandbox/doctor-flow.test.ts:201:3
const harness = createDoctorHarness();

const report = await harness.runSandboxDoctor("alpha", ["--json"], { quietJson: true });
Expand Down Expand Up @@ -212,4 +244,109 @@
expect(harness.captureHostCommandSpy).not.toHaveBeenCalled();
expect(harness.repairMutableConfigPermsSpy).not.toHaveBeenCalled();
});

it("does not run live or tool-scope probes when OpenShell is unavailable", async () => {
const harness = createDoctorHarness();
harness.resolveOpenShellSpy.mockReturnValue(null);

await harness.runSandboxDoctor("alpha", ["--json"], { quietJson: true });

expect(harness.recoverNamedGatewayRuntimeSpy).not.toHaveBeenCalled();
expect(harness.captureOpenShellSpy).not.toHaveBeenCalled();
expect(harness.buildToolScopeChecksSpy).not.toHaveBeenCalled();
});

it("does not run live or tool-scope probes when the named gateway is disconnected", async () => {
const harness = createDoctorHarness();
harness.getNamedGatewayLifecycleStateSpy.mockReturnValue({
state: "missing_named",
status: "Status: Disconnected",
gatewayInfo: "",
activeGateway: null,
});

await harness.runSandboxDoctor("alpha", ["--json"], { quietJson: true });

expect(harness.captureOpenShellSpy).not.toHaveBeenCalled();
expect(harness.buildToolScopeChecksSpy).not.toHaveBeenCalled();
});

it("keeps JSON gateway diagnostics read-only", async () => {
const harness = createDoctorHarness();

await harness.runSandboxDoctor("alpha", ["--json"], { quietJson: true });

expect(harness.getNamedGatewayLifecycleStateSpy).toHaveBeenCalledWith("nemoclaw-19080");
expect(harness.recoverNamedGatewayRuntimeSpy).not.toHaveBeenCalled();
});

it("does not enable repairs for plain or JSON diagnostics", async () => {
const harness = createDoctorHarness();
harness.inspectMutableConfigPermsSpy.mockReturnValue({
applies: true,
ok: false,
dirMode: "700",
dirOwner: "sandbox:sandbox",
fileMode: "600",
fileOwner: "sandbox:sandbox",
configDir: "/sandbox/.openclaw",
configFile: "openclaw.json",
issues: ["directory mode is 700"],
});
const processRecovery = requireDist("./process-recovery.js");
vi.mocked(processRecovery.probeSandboxInferenceGatewayHealth).mockResolvedValue({
ok: true,
endpoint: "http://127.0.0.1:19000/v1/chat/completions",
detail: "healthy",
});

await harness.runSandboxDoctor("alpha");
await harness.runSandboxDoctor("alpha", ["--json"], { quietJson: true });

expect(harness.repairMutableConfigPermsSpy).not.toHaveBeenCalled();
expect(harness.buildToolScopeChecksSpy).toHaveBeenCalledTimes(2);
expect(harness.buildToolScopeChecksSpy.mock.calls.map((call) => call[2])).toEqual([
false,
false,
]);
});

it("skips OpenClaw tool-scope checks for other agents", async () => {
const harness = createDoctorHarness();
harness.getSandboxSpy.mockReturnValue({
name: "alpha",
agent: "hermes",
model: "registry-model",
provider: "ollama-local",
openshellDriver: "docker",
gatewayName: "nemoclaw-19080",
gatewayPort: 19080,
});

await harness.runSandboxDoctor("alpha", ["--json"], { quietJson: true });

expect(harness.buildToolScopeChecksSpy).not.toHaveBeenCalled();
});

it("appends the local gateway result without mutating provider health", async () => {
const harness = createDoctorHarness();
const providerHealth = {
ok: true,
probed: true,
providerLabel: "Ollama",
endpoint: "http://127.0.0.1:11434/v1/chat/completions",
detail: "healthy",
};
harness.healthProbeSpy.mockReturnValue(providerHealth);

const report = await harness.runSandboxDoctor("alpha", ["--json"], { quietJson: true });

expect(providerHealth).not.toHaveProperty("subprobes");
expect(report?.checks).toContainEqual(
expect.objectContaining({
group: "Inference",
label: "Provider health (gateway)",
}),
);
});
});
Loading
Loading