Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
95 changes: 95 additions & 0 deletions src/lib/actions/sandbox/gateway-failure-classifier.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

import { beforeEach, describe, expect, it, vi } from "vitest";

const getSandboxMock = vi.fn();

vi.mock("../../state/registry", () => ({
getSandbox: (...args: unknown[]) => getSandboxMock(...args),
listSandboxes: vi.fn(() => ({ sandboxes: [] })),
}));

import { classifyGatewayFailure, type GatewayFailureRunners } from "./gateway-failure-classifier";

function runners(overrides: Partial<GatewayFailureRunners> = {}): GatewayFailureRunners {
return {
dockerInfo: () => true,
dockerIsRunning: () => false,
dockerExists: () => false,
portProbe: async () => false,
...overrides,
};
}

describe("classifyGatewayFailure (#7348)", () => {
beforeEach(() => {
getSandboxMock.mockReset();
});
it("classifies an exited per-port gateway container as container_exited, not container_missing", async () => {
getSandboxMock.mockReturnValue({ name: "sb-1", gatewayPort: 8081 });
const portProbe = vi.fn(async () => false);

const result = await classifyGatewayFailure("sb-1", {
runners: runners({
dockerExists: (container) => container === "openshell-cluster-nemoclaw-8081",
portProbe,
}),
});

expect(result.layer).toBe("container_exited");
expect(result.detail).toContain("openshell-cluster-nemoclaw-8081");
expect(portProbe).toHaveBeenCalledWith(8081);
});

it("reports the per-port gateway port on a port conflict", async () => {
getSandboxMock.mockReturnValue({ name: "sb-1", gatewayPort: 8081 });

const result = await classifyGatewayFailure("sb-1", {
runners: runners({
dockerExists: (container) => container === "openshell-cluster-nemoclaw-8081",
portProbe: async (port) => port === 8081,
}),
});

expect(result.layer).toBe("container_exited_port_conflict");
expect(result.detail).toContain("port 8081");
expect(result.detail).toContain("openshell-cluster-nemoclaw-8081");
});

it("does not attribute a running default gateway container to a per-port sandbox", async () => {
getSandboxMock.mockReturnValue({ name: "sb-1", gatewayPort: 8081 });

const result = await classifyGatewayFailure("sb-1", {
runners: runners({
dockerIsRunning: (container) => container === "openshell-cluster-nemoclaw",
dockerExists: (container) => container.startsWith("openshell-cluster-nemoclaw"),
}),
});

expect(result.layer).toBe("container_exited");
expect(result.detail).toContain("openshell-cluster-nemoclaw-8081");
});

it("keeps probing the default gateway container for sandboxes without a registered gateway", async () => {
getSandboxMock.mockReturnValue(null);

const result = await classifyGatewayFailure("legacy-sb", {
runners: runners({
dockerIsRunning: (container) => container === "openshell-cluster-nemoclaw",
}),
});

expect(result.layer).toBe("gateway_unreachable");
expect(result.detail).toContain("'openshell-cluster-nemoclaw'");
});

it("short-circuits on an unreachable Docker daemon before resolving the gateway", async () => {
const result = await classifyGatewayFailure("sb-1", {
runners: runners({ dockerInfo: () => false }),
});

expect(result.layer).toBe("docker_unreachable");
expect(getSandboxMock).not.toHaveBeenCalled();
});
});
32 changes: 21 additions & 11 deletions src/lib/actions/sandbox/gateway-failure-classifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import { dockerInfo } from "../../adapters/docker/info";
import { dockerCapture } from "../../adapters/docker/run";
import { CLI_NAME } from "../../cli/branding";
import { GATEWAY_PORT } from "../../core/ports";
import { resolveGatewayPortFromName } from "../../onboard/gateway-binding";
import * as registry from "../../state/registry";
import { getSandboxTargetGatewayName } from "./gateway-target";
import { resolveSandboxContainerOwner } from "./sandbox-container-owner";

const DEFAULT_CONTAINER = "openshell-cluster-nemoclaw";
const DOCKER_TIMEOUT_MS = 3000;
const PORT_PROBE_TIMEOUT_MS = 2000;

Expand Down Expand Up @@ -100,7 +101,7 @@ const defaultRunners: GatewayFailureRunners = {
};

export async function classifyGatewayFailure(
_sandboxName: string,
sandboxName: string,
opts?: { runners?: GatewayFailureRunners },
): Promise<GatewayFailureResult> {
const runners = opts?.runners ?? defaultRunners;
Expand All @@ -112,34 +113,43 @@ export async function classifyGatewayFailure(
};
}

if (runners.dockerIsRunning(DEFAULT_CONTAINER)) {
// Probe the gateway container and port that the sandbox actually belongs
// to: a sandbox onboarded with a non-default NEMOCLAW_GATEWAY_PORT runs as
// `openshell-cluster-nemoclaw-<port>`, and classifying against the bare
// default container reports container_missing for a gateway that merely
// exited (or blames an unrelated default gateway).
const gatewayName = getSandboxTargetGatewayName(sandboxName);
const gatewayContainer = `openshell-cluster-${gatewayName}`;
const gatewayPort = resolveGatewayPortFromName(gatewayName) ?? GATEWAY_PORT;

if (runners.dockerIsRunning(gatewayContainer)) {
return {
layer: "gateway_unreachable",
detail: `Container '${DEFAULT_CONTAINER}' is running but the gateway API is not responding.`,
detail: `Container '${gatewayContainer}' is running but the gateway API is not responding.`,
};
}

// Container is not running. Distinguish "exited and still present" from
// "removed/never created" — only the former can hit container_exited*. Per
// issue #3271 AC: container_exited_port_conflict requires `docker ps -a` to
// confirm the container exited rather than being absent.
if (!runners.dockerExists(DEFAULT_CONTAINER)) {
if (!runners.dockerExists(gatewayContainer)) {
return {
layer: "container_missing",
detail: `Container '${DEFAULT_CONTAINER}' is not present (never created or removed).`,
detail: `Container '${gatewayContainer}' is not present (never created or removed).`,
};
}

const portInUse = await runners.portProbe(GATEWAY_PORT);
const portInUse = await runners.portProbe(gatewayPort);
if (portInUse) {
return {
layer: "container_exited_port_conflict",
detail: `Container '${DEFAULT_CONTAINER}' exited, and port ${GATEWAY_PORT} is held by another process.`,
detail: `Container '${gatewayContainer}' exited, and port ${gatewayPort} is held by another process.`,
};
}
return {
layer: "container_exited",
detail: `Container '${DEFAULT_CONTAINER}' exited.`,
detail: `Container '${gatewayContainer}' exited.`,
};
}

Expand Down Expand Up @@ -232,8 +242,8 @@ export async function classifySandboxContainerFailure(
type SandboxDriverLookup = (name: string) => { openshellDriver?: string | null } | null | undefined;

// Drivers whose sandbox runtime does NOT live in the local Docker daemon. Only
// `vm` qualifies: the NemoClaw gateway always runs as the local Docker
// container `openshell-cluster-nemoclaw` (see classifyGatewayFailure), so the
// `vm` qualifies: the NemoClaw gateway always runs as a local Docker
// `openshell-cluster-<gateway>` container (see classifyGatewayFailure), so the
// `docker` driver and the `kubernetes`/k3s driver (k3s-in-Docker, or Docker
// Desktop's Kubernetes — selected by `isLinuxDockerDriverGatewayEnabled()` for
// non-Linux/non-arm64 hosts) both depend on a reachable local Docker daemon. A
Expand Down
16 changes: 10 additions & 6 deletions test/repro-2666-silent-list-status.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
renderSandboxInventoryText,
} from "../src/lib/inventory/index.js";
import { recoverRegistryEntriesWithFallback } from "../src/lib/list-command-deps.js";
import { resolveGatewayName } from "../src/lib/onboard/gateway-binding.js";
import { nemoclawStateRoot } from "../src/lib/state/state-root.js";
import { testTimeoutOptions } from "./helpers/timeouts";

Expand Down Expand Up @@ -248,7 +249,7 @@ describe("simulated container-stopped and foreign-port-holder subprocess regress
fs.writeFileSync(path.join(binDir, "openshell"), lines.join("\n"), { mode: 0o755 });
}

function seedRegistry(stateDir: string, model = "test-model"): void {
function seedRegistry(stateDir: string, model = "test-model", gatewayPort?: number): void {
fs.mkdirSync(stateDir, { recursive: true });
fs.writeFileSync(
path.join(stateDir, "sandboxes.json"),
Expand All @@ -260,6 +261,9 @@ describe("simulated container-stopped and foreign-port-holder subprocess regress
provider: "nvidia-prod",
gpuEnabled: false,
policies: [],
...(gatewayPort === undefined
? {}
: { gatewayName: resolveGatewayName(gatewayPort), gatewayPort }),
},
},
defaultSandbox: "my-assist",
Expand Down Expand Up @@ -292,7 +296,7 @@ describe("simulated container-stopped and foreign-port-holder subprocess regress
it("nemoclaw list reads the registry scoped to a non-default gateway port (#3053)", () => {
const port = 9123;
seedRegistry(path.join(home, ".nemoclaw"), "default-root-model");
seedRegistry(nemoclawStateRoot(home, port), "selected-port-model");
seedRegistry(nemoclawStateRoot(home, port), "selected-port-model", port);

const { code, stdout, stderr } = runCli(["list"], {
NEMOCLAW_GATEWAY_PORT: String(port),
Expand All @@ -311,7 +315,7 @@ describe("simulated container-stopped and foreign-port-holder subprocess regress
() => {
const port = 9123;
seedRegistry(path.join(home, ".nemoclaw"), "default-root-model");
seedRegistry(nemoclawStateRoot(home, port), "selected-port-model");
seedRegistry(nemoclawStateRoot(home, port), "selected-port-model", port);

const { code, stdout, stderr } = runCli(["my-assist", "status"], {
NEMOCLAW_GATEWAY_PORT: String(port),
Expand Down Expand Up @@ -433,15 +437,15 @@ describe("simulated container-stopped and foreign-port-holder subprocess regress
await new Promise<void>((resolve) => listener.listen(0, "127.0.0.1", resolve));
const port = (listener.address() as { port: number }).port;
fs.rmSync(path.join(home, ".nemoclaw", "sandboxes.json"), { force: true });
seedRegistry(nemoclawStateRoot(home, port));
seedRegistry(nemoclawStateRoot(home, port), "test-model", port);

try {
// Fake docker: info OK, ps shows nothing running, ps -a shows the
// openshell-cluster-nemoclaw container (i.e. it exited cleanly).
writeFakeDocker([
"#!/usr/bin/env bash",
"if [ \"$1\" = info ]; then echo 'Server Version: 24.0.0'; exit 0; fi",
'if [ "$1" = ps ] && [ "$2" = -a ]; then echo \'openshell-cluster-nemoclaw\'; exit 0; fi',
`if [ "$1" = ps ] && [ "$2" = -a ]; then echo 'openshell-cluster-nemoclaw-${port}'; exit 0; fi`,
'if [ "$1" = ps ]; then exit 0; fi',
"exit 0",
]);
Expand All @@ -452,7 +456,7 @@ describe("simulated container-stopped and foreign-port-holder subprocess regress
writeFakeOpenshell([
"#!/usr/bin/env bash",
'case "$*" in',
' "sandbox get my-assist"|"sandbox get -g nemoclaw my-assist")',
` "sandbox get my-assist"|"sandbox get -g nemoclaw-${port} my-assist")`,
" echo 'transport error: unexpected EOF' >&2",
" exit 1",
" ;;",
Expand Down
Loading