Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
38 changes: 37 additions & 1 deletion src/lib/actions/sandbox/status.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import { describe, expect, it } from "vitest";

import type { ProviderHealthProbeOptions } from "../../../../dist/lib/inference/health";
import { getSandboxStatusInferenceHealth } from "../../../../dist/lib/actions/sandbox/status";
import {
getSandboxStatusInferenceHealth,
isDockerDaemonUnreachableForStatus,
} from "../../../../dist/lib/actions/sandbox/status";

describe("sandbox status inference health", () => {
it("passes the current model with the current provider", () => {
Expand Down Expand Up @@ -50,3 +53,36 @@ describe("sandbox status inference health", () => {
expect(called).toBe(false);
});
});

describe("isDockerDaemonUnreachableForStatus", () => {
it("returns false when sandbox entry is null", () => {
expect(isDockerDaemonUnreachableForStatus(null, () => false)).toBe(false);
});

it("returns false when the openshell driver is not docker", () => {
expect(
isDockerDaemonUnreachableForStatus(
{ name: "alpha", openshellDriver: "vm" } as never,
() => false,
),
).toBe(false);
});

it("returns true when driver is docker and the probe reports unreachable", () => {
expect(
isDockerDaemonUnreachableForStatus(
{ name: "alpha", openshellDriver: "docker" } as never,
() => false,
),
).toBe(true);
});

it("returns false when driver is docker and the probe reports reachable", () => {
expect(
isDockerDaemonUnreachableForStatus(
{ name: "alpha", openshellDriver: "docker" } as never,
() => true,
),
).toBe(false);
});
});
42 changes: 37 additions & 5 deletions src/lib/actions/sandbox/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0


import { dockerInfo } from "../../adapters/docker/info";
import {
detectOpenShellStateRpcResultIssue,
printOpenShellStateRpcIssue,
Expand Down Expand Up @@ -111,10 +112,31 @@ async function printGatewayFailureLayerHeader(sandboxName: string): Promise<void
console.log(` ${getLayerHeader(failure.layer)}`);
}

const DOCKER_INFO_PROBE_TIMEOUT_MS = 3000;

type DockerInfoProbe = () => boolean;

const defaultDockerInfoProbe: DockerInfoProbe = () =>
dockerInfo({ ignoreError: true, timeout: DOCKER_INFO_PROBE_TIMEOUT_MS }).length > 0;

export function isDockerDaemonUnreachableForStatus(
sb: registry.SandboxEntry | null,
probe: DockerInfoProbe = defaultDockerInfoProbe,
): boolean {
if (!sb || sb.openshellDriver !== "docker") return false;
return !probe();
}

// eslint-disable-next-line complexity
export async function showSandboxStatus(sandboxName: string): Promise<void> {
const sb = registry.getSandbox(sandboxName);
maybeEnsureHermesToolGatewayBroker(sb);
// #4313: surface docker_unreachable header upfront when the host Docker
// daemon is stopped on a docker-driver sandbox. Without this, the cached
// sandbox metadata renders as a "healthy" report even though the local
// container stack is down, and the Inference probe hits the remote provider
// directly so it falsely shows healthy too.
const dockerUnreachable = isDockerDaemonUnreachableForStatus(sb);
// #2666: never let an unexpected throw from the gateway probe (e.g. openshell
// hanging when its container is stopped and the published port is held by a
// foreign listener) suppress the sandbox header. The downstream switch
Expand Down Expand Up @@ -154,11 +176,17 @@ export async function showSandboxStatus(sandboxName: string): Promise<void> {
liveResult && !isCommandTimeout(liveResult) ? parseGatewayInference(liveResult.output) : null;
const currentModel = (live && live.model) || (sb && sb.model) || "unknown";
const currentProvider = (live && live.provider) || (sb && sb.provider) || "unknown";
const inferenceHealth = getSandboxStatusInferenceHealth(
lookup.state === "present",
currentProvider,
currentModel,
);
// When docker is unreachable on a docker-driver sandbox, host-side probes
// misrepresent the sandbox state — the remote-provider reachability check
// doesn't go through the local stack. Suppress the probe so the output
// doesn't conflict with the failure-layer header.
const inferenceHealth = dockerUnreachable
? null
: getSandboxStatusInferenceHealth(
lookup.state === "present",
currentProvider,
currentModel,
);
// #3265 optional 3rd line: probe the full inference chain (openclaw gateway
// → auth proxy → backend) from inside the sandbox so a broken hop the
// host-side probes can't see still surfaces in `status`.
Expand All @@ -183,6 +211,10 @@ export async function showSandboxStatus(sandboxName: string): Promise<void> {
}
if (sb) {
console.log("");
if (dockerUnreachable) {
console.log(` ${getLayerHeader("docker_unreachable")}`);
process.exitCode = 1;
}
console.log(` Sandbox: ${sb.name}`);
console.log(` Model: ${currentModel}`);
console.log(` Provider: ${currentProvider}`);
Expand Down
108 changes: 108 additions & 0 deletions test/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -943,6 +943,114 @@ describe("CLI dispatch", () => {
});
});

it("sandbox <name> status surfaces docker_unreachable header and suppresses stale Inference probe", () => {
const home = fs.mkdtempSync(
path.join(os.tmpdir(), "nemoclaw-cli-sandbox-status-docker-unreachable-"),
);
const localBin = path.join(home, "bin");
fs.mkdirSync(localBin, { recursive: true });
writeSandboxRegistry(home, "alpha", {
provider: "openai-api",
model: "gpt-4o-mini",
openshellDriver: "docker",
} as unknown as Partial<SandboxEntry>);

fs.writeFileSync(
path.join(localBin, "docker"),
["#!/usr/bin/env bash", "exit 1"].join("\n"),
{ mode: 0o755 },
);
fs.writeFileSync(
path.join(localBin, "openshell"),
[
"#!/usr/bin/env bash",
'if [ "$1" = "inference" ] && [ "$2" = "get" ]; then',
" echo 'Gateway inference:'",
" echo ' Provider: openai-api'",
" echo ' Model: gpt-4o-mini'",
" exit 0",
"fi",
'if [ "$1" = "status" ]; then',
" echo 'Gateway: nemoclaw'",
" echo 'Status: Connected'",
" exit 0",
"fi",
'if [ "$1" = "gateway" ] && [ "$2" = "info" ]; then',
" echo 'Gateway: nemoclaw'",
" exit 0",
"fi",
"exit 0",
].join("\n"),
{ mode: 0o755 },
);

const r = runWithEnv("alpha status", {
HOME: home,
PATH: `${localBin}:${process.env.PATH || ""}`,
});

expect(r.code).toBe(1);
expect(r.out).toContain(
"Failure layer: docker_unreachable — Docker daemon is not reachable.",
);
expect(r.out).not.toContain("Inference: healthy");
const headerIdx = r.out.indexOf("Failure layer: docker_unreachable");
const sandboxIdx = r.out.indexOf("Sandbox: alpha");
expect(headerIdx).toBeGreaterThanOrEqual(0);
expect(sandboxIdx).toBeGreaterThan(headerIdx);
});

it("sandbox <name> status preserves Inference probe when openshellDriver is not docker", () => {
const home = fs.mkdtempSync(
path.join(os.tmpdir(), "nemoclaw-cli-sandbox-status-non-docker-driver-"),
);
const localBin = path.join(home, "bin");
fs.mkdirSync(localBin, { recursive: true });
writeSandboxRegistry(home, "alpha", {
provider: "openai-api",
model: "gpt-4o-mini",
openshellDriver: "vm",
} as unknown as Partial<SandboxEntry>);

fs.writeFileSync(
path.join(localBin, "docker"),
["#!/usr/bin/env bash", "exit 1"].join("\n"),
{ mode: 0o755 },
);
fs.writeFileSync(
path.join(localBin, "openshell"),
[
"#!/usr/bin/env bash",
'if [ "$1" = "inference" ] && [ "$2" = "get" ]; then',
" echo 'Gateway inference:'",
" echo ' Provider: openai-api'",
" echo ' Model: gpt-4o-mini'",
" exit 0",
"fi",
'if [ "$1" = "status" ]; then',
" echo 'Gateway: nemoclaw'",
" echo 'Status: Connected'",
" exit 0",
"fi",
'if [ "$1" = "gateway" ] && [ "$2" = "info" ]; then',
" echo 'Gateway: nemoclaw'",
" exit 0",
"fi",
"exit 0",
].join("\n"),
{ mode: 0o755 },
);

const r = runWithEnv("alpha status", {
HOME: home,
PATH: `${localBin}:${process.env.PATH || ""}`,
});

expect(r.out).not.toContain(
"Failure layer: docker_unreachable",
);
});

it("status rejects unknown flags through current dispatch path", () => {
const r = run("status --bogus");
expect(r.code).toBe(2);
Expand Down
Loading