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
110 changes: 110 additions & 0 deletions src/lib/onboard/inference-selection-validation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

import assert from "node:assert/strict";
import { spawnSync } from "node:child_process";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { describe, it } from "vitest";

const REPO_ROOT = path.join(import.meta.dirname, "../../..");

describe("inference selection validation", () => {
it("preserves non-zero exit signaling when non-interactive endpoint validation fails (#5721)", () => {
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "nemoclaw-validation-exit-"));
const scriptPath = path.join(tmpDir, "validation-exit-check.js");
const validationPath = JSON.stringify(
path.join(REPO_ROOT, "dist", "lib", "onboard", "inference-selection-validation.js"),
);
const probesPath = JSON.stringify(
path.join(REPO_ROOT, "dist", "lib", "inference", "onboard-probes.js"),
);
const credentialsPath = JSON.stringify(
path.join(REPO_ROOT, "dist", "lib", "credentials", "store.js"),
);

const script = String.raw`
const probesPath = require.resolve(${probesPath});
const credentialsPath = require.resolve(${credentialsPath});
require.cache[probesPath] = {
id: probesPath,
filename: probesPath,
loaded: true,
exports: {
probeAnthropicEndpoint: () => {
throw new Error("unexpected anthropic probe");
},
probeOpenAiLikeEndpoint: () => ({
ok: false,
failures: [{ name: "Chat Completions API", httpStatus: 403 }],
}),
},
};
require.cache[credentialsPath] = {
id: credentialsPath,
filename: credentialsPath,
loaded: true,
exports: {
getCredential: () => "nvapi-invalid-key-12345",
},
};
const { createInferenceSelectionValidationHelpers } = require(${validationPath});

const lines = [];
const exitCalls = [];
let promptCalls = 0;
const originalLog = console.log;
console.error = (...args) => lines.push(args.join(" "));
process.exitCode = undefined;
process.exit = (code) => {
exitCalls.push(code);
return undefined;
};

(async () => {
const helpers = createInferenceSelectionValidationHelpers({
isNonInteractive: () => true,
agentProductName: () => "OpenClaw",
promptValidationRecovery: async () => {
promptCalls += 1;
return "selection";
},
});
let thrown = null;
try {
await helpers.validateOpenAiLikeSelection(
"NVIDIA Endpoints",
"https://integrate.api.nvidia.com/v1",
"meta/llama-3.3-70b-instruct",
"NVIDIA_INFERENCE_API_KEY",
);
} catch (error) {
thrown = error instanceof Error ? error.message : String(error);
}
originalLog(JSON.stringify({ thrown, exitCalls, exitCode: process.exitCode, promptCalls, lines }));
})().catch((error) => {
console.error(error);
process.exitCode = 1;
});
`;
fs.writeFileSync(scriptPath, script);

const result = spawnSync(process.execPath, [scriptPath], {
cwd: REPO_ROOT,
encoding: "utf-8",
});

assert.equal(result.status, 1, result.stderr);
const payload = JSON.parse(result.stdout.trim());
assert.equal(payload.thrown, "Non-interactive endpoint validation failed.");
assert.deepEqual(payload.exitCalls, [1]);
assert.equal(payload.exitCode, 1);
assert.equal(payload.promptCalls, 0);
assert.deepEqual(payload.lines, [
" NVIDIA Endpoints endpoint validation failed.",
" Validation probe summary: Chat Completions API: HTTP 403.",
" Validation details were omitted to avoid exposing credentials.",
]);
});
});
14 changes: 10 additions & 4 deletions src/lib/onboard/inference-selection-validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ export interface InferenceSelectionValidationHelpers {
export function createInferenceSelectionValidationHelpers(
deps: InferenceSelectionValidationDeps,
): InferenceSelectionValidationHelpers {
function exitNonInteractiveValidationFailure(): never {
process.exitCode = 1;
(process.exit as (code?: number) => void)(1);
throw new Error("Non-interactive endpoint validation failed.");
}

function printValidationFailure(
label: string,
probe?: { failures?: unknown[]; message?: unknown },
Expand Down Expand Up @@ -111,7 +117,7 @@ export function createInferenceSelectionValidationHelpers(
if (!probe.ok) {
printValidationFailure(label, probe);
if (deps.isNonInteractive()) {
process.exit(1);
exitNonInteractiveValidationFailure();
}
const retry = await deps.promptValidationRecovery(
label,
Expand Down Expand Up @@ -146,7 +152,7 @@ export function createInferenceSelectionValidationHelpers(
if (!probe.ok) {
printValidationFailure(label, probe);
if (deps.isNonInteractive()) {
process.exit(1);
exitNonInteractiveValidationFailure();
}
const retry = await deps.promptValidationRecovery(
label,
Expand Down Expand Up @@ -189,7 +195,7 @@ export function createInferenceSelectionValidationHelpers(
}
printValidationFailure(label, probe);
if (deps.isNonInteractive()) {
process.exit(1);
exitNonInteractiveValidationFailure();
}
const retry = await deps.promptValidationRecovery(
label,
Expand Down Expand Up @@ -219,7 +225,7 @@ export function createInferenceSelectionValidationHelpers(
}
printValidationFailure(label, probe);
if (deps.isNonInteractive()) {
process.exit(1);
exitNonInteractiveValidationFailure();
}
const retry = await deps.promptValidationRecovery(
label,
Expand Down