Skip to content
Closed
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
33 changes: 22 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 @@ -99,8 +100,16 @@ const defaultRunners: GatewayFailureRunners = {
portProbe: defaultPortProbe,
};

function resolveGatewayFailureTarget(sandboxName: string): { container: string; port: number } {
const gatewayName = getSandboxTargetGatewayName(sandboxName);
return {
container: `openshell-cluster-${gatewayName}`,
port: resolveGatewayPortFromName(gatewayName) ?? GATEWAY_PORT,
};
}

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

if (runners.dockerIsRunning(DEFAULT_CONTAINER)) {
const target = resolveGatewayFailureTarget(sandboxName);

if (runners.dockerIsRunning(target.container)) {
return {
layer: "gateway_unreachable",
detail: `Container '${DEFAULT_CONTAINER}' is running but the gateway API is not responding.`,
detail: `Container '${target.container}' 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(target.container)) {
return {
layer: "container_missing",
detail: `Container '${DEFAULT_CONTAINER}' is not present (never created or removed).`,
detail: `Container '${target.container}' is not present (never created or removed).`,
};
}

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

Expand Down Expand Up @@ -232,8 +243,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 container
// scoped to the sandbox's target gateway (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
41 changes: 40 additions & 1 deletion test/gateway-failure-classifier.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

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

import {
classifyGatewayFailure,
Expand All @@ -12,6 +12,7 @@ import {
printDockerRuntimeDownGuidance,
type SandboxContainerFailureRunners,
} from "../src/lib/actions/sandbox/gateway-failure-classifier.js";
import * as registry from "../src/lib/state/registry.js";

function makeRunners(overrides: Partial<GatewayFailureRunners> = {}): GatewayFailureRunners {
return {
Expand Down Expand Up @@ -78,6 +79,44 @@ describe("classifyGatewayFailure", () => {
expect(result.detail).toContain("exited");
});

it("uses the sandbox owning gateway container and port for non-default gateway sandboxes", async () => {
const getSandbox = vi.spyOn(registry, "getSandbox").mockReturnValue({
name: "my-sandbox",
gatewayName: "nemoclaw-8081",
gatewayPort: 8081,
} as never);
const checkedRunning: string[] = [];
const checkedExisting: string[] = [];
const probedPorts: number[] = [];

try {
const result = await classifyGatewayFailure("my-sandbox", {
runners: makeRunners({
dockerIsRunning: (container) => {
checkedRunning.push(container);
return false;
},
dockerExists: (container) => {
checkedExisting.push(container);
return container === "openshell-cluster-nemoclaw-8081";
},
portProbe: async (port) => {
probedPorts.push(port);
return false;
},
}),
});

expect(result.layer).toBe("container_exited");
expect(result.detail).toContain("openshell-cluster-nemoclaw-8081");
expect(checkedRunning).toEqual(["openshell-cluster-nemoclaw-8081"]);
expect(checkedExisting).toEqual(["openshell-cluster-nemoclaw-8081"]);
expect(probedPorts).toEqual([8081]);
} finally {
getSandbox.mockRestore();
}
});

it("does not call dockerIsRunning / dockerExists / portProbe when docker info fails", async () => {
let dockerIsRunningCalled = false;
let dockerExistsCalled = false;
Expand Down
Loading