// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. // SPDX-License-Identifier: Apache-2.0 import fs from "node:fs"; import os from "node:os"; import { describe, expect, it, vi } from "vitest"; import { customNativeProvider } from "../inference/native-provider/custom"; import { HOSTED_NATIVE_PROVIDERS } from "../inference/native-provider/hosted"; import { nativeLocalIdentity, type NativeLocalBinding } from "../inference/native-local/contract"; import { buildHttpsPinRouteBaseUrl } from "../inference/https-pin-runtime"; import { managedLlamaCppStatePaths, reserveManagedLlamaCppOwner, } from "../inference/llama-cpp/managed-state"; import { ConfigCorruptError, ConfigPermissionError } from "../state/config-io"; import type { OpenShellInferenceRouteResult } from "../adapters/openshell/inference-route"; vi.mock("../adapters/openshell/runtime", () => ({ captureOpenshell: vi.fn(), })); vi.mock("../inference/local", () => ({ DEFAULT_OLLAMA_MODEL: "llama3.1", })); import { runInferenceGet, type InferenceEndpointStatus, type InferenceGetDeps, type InferenceGetResult, } from "./inference-get"; function configuredRoute(provider: string, model: string): OpenShellInferenceRouteResult { return { ok: true, value: { state: "configured", route: { provider, model } } }; } function nativeLocalSandbox( overrides: Record = {}, agent: "langchain-deepagents-code" | "hermes" | "openclaw" | "pi" = "langchain-deepagents-code", ) { const binding: NativeLocalBinding = { provider: "ollama-local", endpointUrl: "http://host.openshell.internal:11435/v1", credentialEnv: "NEMOCLAW_LOCAL_INFERENCE_TOKEN", authMode: "authenticated", gatewayName: "nemoclaw", sandboxName: "alpha", ...(agent !== "langchain-deepagents-code" ? { agent } : {}), }; return { name: "alpha", agent, provider: binding.provider, model: "qwen2.5:7b", gatewayName: binding.gatewayName, nativeLocalProviderAttachment: { ...binding, ...nativeLocalIdentity(binding), schemaVersion: 1 as const, providerId: "owned-provider-id", }, ...overrides, }; } function createDeps(fixture: OpenShellInferenceRouteResult): InferenceGetDeps & { log: ReturnType; observeInferenceRoute: ReturnType; getSandboxTargetGatewayName: ReturnType; listSandboxes: ReturnType; } { const observeInferenceRoute = vi.fn(async () => fixture); const getSandboxTargetGatewayName = vi.fn(() => "nemoclaw"); const listSandboxes = vi.fn(() => []); const log = vi.fn(); return { inferenceRouteObserver: { observeInferenceRoute }, observeInferenceRoute, getSandboxTargetGatewayName: getSandboxTargetGatewayName as unknown as InferenceGetDeps["getSandboxTargetGatewayName"] & ReturnType, listSandboxes: listSandboxes as unknown as InferenceGetDeps["listSandboxes"] & ReturnType, log: log as unknown as InferenceGetDeps["log"] & ReturnType, }; } function recordRoute( deps: ReturnType, route: { endpointUrl: string; model?: string; name?: string; provider: string }, ): void { const name = route.name ?? "custom"; deps.listSandboxes.mockReturnValue([ { name, provider: route.provider, model: route.model ?? "custom/model", endpointUrl: route.endpointUrl, preferredInferenceApi: route.provider === "compatible-anthropic-endpoint" ? "anthropic-messages" : "openai-completions", credentialEnv: "CUSTOM_API_KEY", }, ]); } const ENDPOINT_OMISSION_CASES = [ { name: "conflicting same-gateway URLs", endpoints: ["https://inference-a.example.test/v1", "https://inference-b.example.test/v1"], status: "conflicting", affectedSandboxes: ["custom-1", "custom-2"], }, { name: "a non-HTTP URL", endpoints: ["ftp://inference.example.test/v1"], status: "invalid", affectedSandboxes: ["custom-1"], }, { name: "a URL containing a control character", endpoints: ["https://inference.example.test/v1\u0007"], status: "invalid", affectedSandboxes: ["custom-1"], }, ] as const; const ENDPOINT_RECOVERY: Record = { unavailable: "Restore registry access or record the trusted endpoint and API family again, then rerun inference get.", "registry-corrupt": "Restore a known-good sandbox registry backup, then rerun inference get; if no valid backup exists, stop and obtain recovery support.", "registry-unreadable": "Repair ownership and permissions for the existing NemoClaw state directory, then rerun inference get.", invalid: "Repair the named sandbox registrations' compatible-route metadata, then rerun inference get; repeat if additional affected registrations are reported.", conflicting: "Align or remove the named conflicting same-gateway sandbox routes, then rerun inference get; repeat if additional affected registrations are reported.", withheld: "Use a credential-free root or /v1 API base if endpoint readback is required.", "adapter-managed": "For a same-provider model change, omit endpoint options so NemoClaw reuses the recorded route.", }; function expectedEndpointOmission( endpointStatus: InferenceEndpointStatus, model = "custom/model", affectedSandboxes: string[] = [], affectedSandboxesTruncated = false, ): { provider: string; model: string; endpointStatus: InferenceEndpointStatus; endpointRecovery: string; affectedSandboxes?: string[]; affectedSandboxesTruncated?: boolean; } { return { provider: "compatible-endpoint", model, endpointStatus, endpointRecovery: ENDPOINT_RECOVERY[endpointStatus], ...(affectedSandboxes.length > 0 ? { affectedSandboxes } : {}), ...(affectedSandboxesTruncated ? { affectedSandboxesTruncated } : {}), }; } const BOUNDED_AFFECTED_SANDBOXES = [ "sandbox-00", "sandbox-01", "sandbox-02", "sandbox-03", "sandbox-04", "sandbox-05", "sandbox-06", "sandbox-07", ]; function createBoundedAffectedDeps(): ReturnType { const deps = createDeps(configuredRoute("compatible-endpoint", "live/model")); deps.listSandboxes.mockReturnValue([ ...Array.from({ length: 10 }, (_, index) => ({ name: `sandbox-${String(index).padStart(2, "0")}`, provider: "compatible-endpoint", model: "stale/model", endpointUrl: "https://stale.example.test/v1", })), { name: "unsafe\nregistry-name", provider: "compatible-endpoint", model: "stale/model", endpointUrl: "https://stale.example.test/v1", }, ]); return deps; } function expectBoundedAffectedDiagnostics(result: InferenceGetResult, output: string): void { expect(result.affectedSandboxes).toEqual(BOUNDED_AFFECTED_SANDBOXES); expect(result.affectedSandboxesTruncated).toBe(true); expect(output).not.toContain("unsafe"); expect(output).not.toContain("stale.example.test"); } describe("runInferenceGet", () => { it.each([ { agent: "langchain-deepagents-code", provider: "ollama-local", sandboxName: undefined, endpointUrl: "http://host.openshell.internal:11435/v1", }, { agent: "openclaw", provider: "ollama-local", sandboxName: "alpha", endpointUrl: "http://host.openshell.internal:11435/v1", }, { agent: "langchain-deepagents-code", provider: "compatible-endpoint", sandboxName: "alpha", endpointUrl: "http://10.42.0.3:8999/v1", }, { agent: "hermes", provider: "ollama-local", sandboxName: "alpha", endpointUrl: "http://host.openshell.internal:11435/v1", }, { agent: "hermes", provider: "compatible-endpoint", sandboxName: undefined, endpointUrl: "http://host.openshell.internal:11434/v1", }, { agent: "pi", provider: "compatible-endpoint", sandboxName: "alpha", endpointUrl: "http://10.42.0.3:8999/v1", }, ] as const)( "reads a recorded native-local $agent route without consulting the shared gateway (#12975)", async ({ agent, provider, sandboxName, endpointUrl }) => { const deps = createDeps(configuredRoute("stale-shared-provider", "stale-shared-model")); const sandbox = nativeLocalSandbox({}, agent); const binding = { ...sandbox.nativeLocalProviderAttachment, provider, endpointUrl }; deps.getSandbox = () => nativeLocalSandbox({ agent, provider, nativeLocalProviderAttachment: { ...binding, ...nativeLocalIdentity(binding) }, }); deps.getDefaultSandbox = () => "alpha"; await expect(runInferenceGet({ json: true, sandboxName }, deps)).resolves.toEqual({ provider, model: "qwen2.5:7b", endpointUrl, }); expect(deps.observeInferenceRoute).not.toHaveBeenCalled(); expect(JSON.parse(deps.log.mock.calls[0][0])).toEqual({ provider, model: "qwen2.5:7b", endpointUrl, }); }, ); it("prints the selected native-local provider, model, and endpoint (#12975)", async () => { const deps = createDeps(configuredRoute("stale-shared-provider", "stale-shared-model")); deps.getSandbox = () => nativeLocalSandbox(); await runInferenceGet({ sandboxName: "alpha" }, deps); expect(deps.log.mock.calls.map(([line]) => line)).toEqual([ "Provider: ollama-local", "Model: qwen2.5:7b", "Endpoint: http://host.openshell.internal:11435/v1", ]); expect(deps.observeInferenceRoute).not.toHaveBeenCalled(); }); it.each([{ json: true }, { json: false }])( "withholds an opaque native-local endpoint path in json=$json output (#12975)", async ({ json }) => { const deps = createDeps(configuredRoute("stale-shared-provider", "stale-shared-model")); const sandbox = nativeLocalSandbox(); const endpointUrl = "http://host.openshell.internal:11435/opaque-tenant-credential/v1"; const binding = { ...sandbox.nativeLocalProviderAttachment, endpointUrl }; deps.getSandbox = () => nativeLocalSandbox({ nativeLocalProviderAttachment: { ...binding, ...nativeLocalIdentity(binding) }, }); await expect(runInferenceGet({ sandboxName: "alpha", json }, deps)).resolves.toEqual({ provider: "ollama-local", model: "qwen2.5:7b", endpointStatus: "withheld", endpointRecovery: ENDPOINT_RECOVERY.withheld, }); expect(deps.log.mock.calls.flat().join("\n")).not.toContain("opaque-tenant-credential"); expect(deps.observeInferenceRoute).not.toHaveBeenCalled(); }, ); it.each([ { label: "invalid attachment", overrides: { nativeLocalProviderAttachment: {} } }, { label: "provider mismatch", overrides: { provider: "vllm-local" } }, { label: "Hermes provider mismatch", overrides: nativeLocalSandbox({ provider: "vllm-local" }, "hermes"), }, { label: "sandbox mismatch", overrides: { name: "beta" } }, { label: "gateway mismatch", overrides: { gatewayName: "nemoclaw-19090" } }, { label: "missing recorded gateway", overrides: { gatewayName: undefined, gatewayPort: 8080 }, }, { label: "agent mismatch", overrides: { agent: "hermes" } }, { label: "OpenClaw with an untagged DCode attachment", overrides: { agent: "openclaw" } }, { label: "Hermes attachment assigned to another agent", overrides: nativeLocalSandbox({ agent: "langchain-deepagents-code" }, "hermes"), }, { label: "credential boundary mismatch", overrides: { nativeLocalProviderAttachment: (() => { const attachment = nativeLocalSandbox().nativeLocalProviderAttachment; const binding = { ...attachment, credentialEnv: "UNRELATED_TOKEN" }; return { ...binding, ...nativeLocalIdentity(binding) }; })(), }, }, ])( "rejects a native-local $label without reporting the shared route (#12975)", async ({ overrides }) => { const deps = createDeps(configuredRoute("stale-shared-provider", "stale-shared-model")); deps.getSandbox = () => nativeLocalSandbox(overrides); await expect(runInferenceGet({ sandboxName: "alpha" }, deps)).rejects.toThrow( "Invalid native local provider attachment for the recorded inference selection", ); expect(deps.observeInferenceRoute).not.toHaveBeenCalled(); expect(deps.log).not.toHaveBeenCalled(); }, ); it("reads a recorded native NVIDIA route without consulting the shared gateway route", async () => { const deps = createDeps(configuredRoute("stale-provider", "stale-model")); const sandbox = { name: "alpha", agent: "openclaw", provider: "nvidia-prod", model: "nvidia/nemotron-3-super-120b-a12b", nativeNvidiaProviderAttachment: { schemaVersion: 1 as const, profileId: "nemoclaw-nvidia-inference-v1" as const, providerName: "nemoclaw-nvidia-prod-v1" as const, providerId: "11111111-2222-4333-8444-555555555555", }, }; deps.getDefaultSandbox = () => "alpha"; deps.getSandbox = () => sandbox; await expect(runInferenceGet({}, deps)).resolves.toEqual({ provider: "nvidia-prod", model: "nvidia/nemotron-3-super-120b-a12b", endpointUrl: "https://integrate.api.nvidia.com/v1", }); expect(deps.observeInferenceRoute).not.toHaveBeenCalled(); expect(deps.log.mock.calls.map(([line]) => line)).toEqual([ "Provider: nvidia-prod", "Model: nvidia/nemotron-3-super-120b-a12b", "Endpoint: https://integrate.api.nvidia.com/v1", ]); }); it("prints the live provider and model", async () => { const deps = createDeps(configuredRoute("nvidia-prod", "nvidia/model")); await expect(runInferenceGet({}, deps)).resolves.toEqual({ provider: "nvidia-prod", model: "nvidia/model", }); expect(deps.observeInferenceRoute).toHaveBeenCalledWith({ target: { kind: "named", gatewayName: "nemoclaw" }, timeoutMs: expect.any(Number), }); expect(deps.log.mock.calls.map(([line]) => line)).toEqual([ "Provider: nvidia-prod", "Model: nvidia/model", ]); }); it("supports JSON output", async () => { const deps = createDeps(configuredRoute("openai-api", "gpt-5.4")); await runInferenceGet({ json: true }, deps); expect(JSON.parse(deps.log.mock.calls[0][0])).toEqual({ provider: "openai-api", model: "gpt-5.4", }); }); it("prints the persisted compatible endpoint in human-readable output (#10784)", async () => { const deps = createDeps(configuredRoute("compatible-endpoint", "custom/model")); recordRoute(deps, { provider: "compatible-endpoint", endpointUrl: "https://inference.example.test/v1", }); await expect(runInferenceGet({ sandboxName: "custom" }, deps)).resolves.toEqual({ provider: "compatible-endpoint", model: "custom/model", endpointUrl: "https://inference.example.test/v1", }); expect(deps.log.mock.calls.map(([line]) => line)).toEqual([ "Provider: compatible-endpoint", "Model: custom/model", "Endpoint: https://inference.example.test/v1", ]); }); it.each([ { label: "direct lookup with a stale model", sandboxName: undefined, persistedModel: "stale/model", }, { label: "sandbox lookup with a stale model", sandboxName: "custom", persistedModel: "stale/model", }, { label: "direct lookup with missing model metadata", sandboxName: undefined }, ])("omits a persisted endpoint for $label (#10784)", async ({ sandboxName, persistedModel }) => { const deps = createDeps(configuredRoute("compatible-endpoint", "live/model")); deps.listSandboxes.mockReturnValue([ { name: "custom", provider: "compatible-endpoint", ...(persistedModel === undefined ? {} : { model: persistedModel }), endpointUrl: "https://stale.example.test/v1", }, ]); await expect(runInferenceGet({ json: true, sandboxName }, deps)).resolves.toEqual( expectedEndpointOmission("invalid", "live/model", ["custom"]), ); expect(deps.log.mock.calls[0][0]).not.toContain("stale.example.test"); }); it("bounds and validates affected sandbox identities in human-readable output", async () => { const deps = createBoundedAffectedDeps(); const result = await runInferenceGet({}, deps); const output = deps.log.mock.calls.flat().join("\n"); expectBoundedAffectedDiagnostics(result, output); expect(output).toContain("Affected: sandbox-00, sandbox-01, sandbox-02, sandbox-03"); expect(output).toContain("(additional output-safe names not shown)"); }); it("bounds and validates affected sandbox identities in JSON output", async () => { const deps = createBoundedAffectedDeps(); const result = await runInferenceGet({ json: true }, deps); const output = deps.log.mock.calls[0][0]; expectBoundedAffectedDiagnostics(result, output); expect(JSON.parse(output)).toMatchObject({ affectedSandboxesTruncated: true, }); }); it.each(["compatible-endpoint", "compatible-anthropic-endpoint"])( "includes the persisted endpoint in JSON output for %s (#10784)", async (provider) => { const deps = createDeps(configuredRoute(provider, "custom/model")); recordRoute(deps, { provider, endpointUrl: "https://inference.example.test/v1", }); await runInferenceGet({ json: true }, deps); expect(JSON.parse(deps.log.mock.calls[0][0])).toEqual({ provider, model: "custom/model", endpointUrl: "https://inference.example.test/v1", }); }, ); it("omits a persisted endpoint for a managed provider (#10784)", async () => { const deps = createDeps(configuredRoute("nvidia-prod", "nvidia/model")); recordRoute(deps, { name: "managed", provider: "nvidia-prod", model: "nvidia/model", endpointUrl: "https://managed.example.test/v1", }); await expect(runInferenceGet({ json: true }, deps)).resolves.toEqual({ provider: "nvidia-prod", model: "nvidia/model", }); expect(JSON.parse(deps.log.mock.calls[0][0])).toEqual({ provider: "nvidia-prod", model: "nvidia/model", }); expect(deps.listSandboxes).not.toHaveBeenCalled(); }); it("omits a credential-bearing compatible endpoint (#10784)", async () => { const deps = createDeps(configuredRoute("compatible-endpoint", "custom/model")); recordRoute(deps, { provider: "compatible-endpoint", endpointUrl: "https://operator:secret@inference.example.test/v1?token=secret", }); await expect(runInferenceGet({ json: true, sandboxName: "custom" }, deps)).resolves.toEqual( expectedEndpointOmission("invalid", "custom/model", ["custom"]), ); expect(deps.log.mock.calls[0][0]).not.toContain("secret"); expect(JSON.parse(deps.log.mock.calls[0][0])).toEqual( expectedEndpointOmission("invalid", "custom/model", ["custom"]), ); }); it.each([ { json: false, label: "human-readable" }, { json: true, label: "JSON" }, ])("omits a recognized path credential from $label output", async ({ json }) => { const deps = createDeps(configuredRoute("compatible-endpoint", "custom/model")); const pathCredential = "sk-proj-" + "A".repeat(10); recordRoute(deps, { provider: "compatible-endpoint", endpointUrl: `https://inference.example.test/${pathCredential}/v1`, }); await expect(runInferenceGet({ json }, deps)).resolves.toEqual( expectedEndpointOmission("withheld"), ); expect(deps.log.mock.calls.flat().join("\n")).not.toContain(pathCredential); }); it.each([ { json: false, label: "human-readable" }, { json: true, label: "JSON" }, ])("omits an opaque path credential from $label output", async ({ json }) => { const deps = createDeps(configuredRoute("compatible-endpoint", "custom/model")); const pathCredential = ["opaque", "tenant", "credential"].join("-"); recordRoute(deps, { provider: "compatible-endpoint", endpointUrl: `https://inference.example.test/${pathCredential}/v1`, }); await expect(runInferenceGet({ json }, deps)).resolves.toEqual( expectedEndpointOmission("withheld"), ); expect(deps.log.mock.calls.flat().join("\n")).not.toContain(pathCredential); }); it.each([ { json: false, label: "human-readable" }, { json: true, label: "JSON" }, ])("omits a non-reusable HTTPS-pin adapter endpoint from $label output", async ({ json }) => { const deps = createDeps(configuredRoute("compatible-endpoint", "custom/model")); const adapterEndpoint = buildHttpsPinRouteBaseUrl("a".repeat(64)); recordRoute(deps, { provider: "compatible-endpoint", endpointUrl: adapterEndpoint, }); await expect(runInferenceGet({ json }, deps)).resolves.toEqual( expectedEndpointOmission("adapter-managed"), ); expect(deps.log.mock.calls.flat().join("\n")).not.toContain(adapterEndpoint); }); it.each(ENDPOINT_OMISSION_CASES)( "omits $name from human-readable output", async ({ affectedSandboxes, endpoints, status }) => { const deps = createDeps(configuredRoute("compatible-endpoint", "custom/model")); deps.listSandboxes.mockReturnValue( endpoints.map((endpointUrl, index) => ({ name: `custom-${String(index + 1)}`, provider: "compatible-endpoint", model: "custom/model", endpointUrl, preferredInferenceApi: "openai-completions", credentialEnv: "CUSTOM_API_KEY", })), ); await expect(runInferenceGet({}, deps)).resolves.toEqual( expectedEndpointOmission(status, "custom/model", [...affectedSandboxes]), ); expect(deps.log.mock.calls.map(([line]) => line)).toEqual([ "Provider: compatible-endpoint", "Model: custom/model", `Endpoint: unavailable (${status})`, `Affected: ${affectedSandboxes.join(", ")}`, `Action: ${ENDPOINT_RECOVERY[status]}`, ]); const output = deps.log.mock.calls.flat().join("\n"); expect(output).not.toContain(endpoints[0]); expect(output).not.toContain(endpoints[1] ?? endpoints[0]); }, ); it.each(ENDPOINT_OMISSION_CASES)( "omits $name from JSON output", async ({ affectedSandboxes, endpoints, status }) => { const deps = createDeps(configuredRoute("compatible-endpoint", "custom/model")); deps.listSandboxes.mockReturnValue( endpoints.map((endpointUrl, index) => ({ name: `custom-${String(index + 1)}`, provider: "compatible-endpoint", model: "custom/model", endpointUrl, preferredInferenceApi: "openai-completions", credentialEnv: "CUSTOM_API_KEY", })), ); const expected = expectedEndpointOmission(status, "custom/model", [...affectedSandboxes]); await expect(runInferenceGet({ json: true }, deps)).resolves.toEqual(expected); expect(JSON.parse(deps.log.mock.calls[0][0])).toEqual(expected); expect(deps.log.mock.calls[0][0]).not.toContain(endpoints[0]); expect(deps.log.mock.calls[0][0]).not.toContain(endpoints[1] ?? endpoints[0]); }, ); it.each([ { json: false, label: "human-readable" }, { json: true, label: "JSON" }, ])( "reports a sandbox-first same-gateway endpoint conflict in $label output", async ({ json }) => { const deps = createDeps(configuredRoute("compatible-endpoint", "custom/model")); deps.listSandboxes.mockReturnValue([ { name: "custom", provider: "compatible-endpoint", model: "custom/model", endpointUrl: "https://selected.example.test/v1", preferredInferenceApi: "openai-completions", credentialEnv: "CUSTOM_API_KEY", }, { name: "peer", provider: "compatible-endpoint", model: "custom/model", endpointUrl: "https://peer.example.test/v1", preferredInferenceApi: "openai-completions", credentialEnv: "CUSTOM_API_KEY", }, ]); await expect(runInferenceGet({ json, sandboxName: "custom" }, deps)).resolves.toEqual( expectedEndpointOmission("conflicting", "custom/model", ["custom", "peer"]), ); const output = deps.log.mock.calls.flat().join("\n"); expect(output).toContain("custom"); expect(output).toContain("peer"); expect(output).not.toContain("selected.example.test"); expect(output).not.toContain("peer.example.test"); }, ); it.each([ { label: "API-family", peer: { preferredInferenceApi: "openai-responses", credentialEnv: "CUSTOM_API_KEY" }, }, { label: "credential-identity", peer: { preferredInferenceApi: "openai-completions", credentialEnv: "OTHER_API_KEY" }, }, ])("omits an endpoint for a same-gateway $label conflict", async ({ peer }) => { const deps = createDeps(configuredRoute("compatible-endpoint", "custom/model")); deps.listSandboxes.mockReturnValue([ { name: "custom", provider: "compatible-endpoint", model: "custom/model", endpointUrl: "https://inference.example.test/v1", preferredInferenceApi: "openai-completions", credentialEnv: "CUSTOM_API_KEY", }, { name: "peer", provider: "compatible-endpoint", model: "custom/model", endpointUrl: "https://inference.example.test/v1", ...peer, }, ]); await expect(runInferenceGet({ json: true }, deps)).resolves.toEqual( expectedEndpointOmission("conflicting", "custom/model", ["custom", "peer"]), ); }); function recordGatewayRoutes(deps: ReturnType): void { deps.getSandboxTargetGatewayName.mockReturnValue("nemoclaw-19090"); deps.listSandboxes.mockReturnValue([ { name: "selected-gateway", provider: "compatible-endpoint", model: "custom/model", endpointUrl: "https://selected.example.test/v1", preferredInferenceApi: "openai-completions", credentialEnv: "CUSTOM_API_KEY", gatewayName: "nemoclaw-19090", gatewayPort: 19090, }, { name: "other-gateway", provider: "compatible-endpoint", model: "custom/model", endpointUrl: "https://other.example.test/v1", preferredInferenceApi: "openai-completions", credentialEnv: "CUSTOM_API_KEY", gatewayName: "nemoclaw", gatewayPort: 8080, }, ]); } function recordEquivalentEndpointRoutes(deps: ReturnType): void { deps.listSandboxes.mockReturnValue([ { name: "custom-a", provider: "compatible-endpoint", model: "custom/model", endpointUrl: "https://inference.example.test/v1", preferredInferenceApi: "openai-completions", credentialEnv: "CUSTOM_API_KEY", }, { name: "custom-b", provider: "compatible-endpoint", model: "custom/model", endpointUrl: "https://inference.example.test/v1/", preferredInferenceApi: "openai-completions", credentialEnv: "CUSTOM_API_KEY", }, ]); } it("reports only the selected non-default gateway endpoint in human-readable output (#10784)", async () => { const deps = createDeps(configuredRoute("compatible-endpoint", "custom/model")); recordGatewayRoutes(deps); await expect(runInferenceGet({}, deps)).resolves.toEqual({ provider: "compatible-endpoint", model: "custom/model", endpointUrl: "https://selected.example.test/v1", }); expect(deps.log.mock.calls.map(([line]) => line)).toEqual([ "Provider: compatible-endpoint", "Model: custom/model", "Endpoint: https://selected.example.test/v1", ]); expect(deps.log.mock.calls.flat().join("\n")).not.toContain("other.example.test"); }); it("reports only the selected non-default gateway endpoint in JSON output (#10784)", async () => { const deps = createDeps(configuredRoute("compatible-endpoint", "custom/model")); recordGatewayRoutes(deps); const expected = { provider: "compatible-endpoint", model: "custom/model", endpointUrl: "https://selected.example.test/v1", }; await expect(runInferenceGet({ json: true }, deps)).resolves.toEqual(expected); expect(JSON.parse(deps.log.mock.calls[0][0])).toEqual(expected); expect(deps.log.mock.calls[0][0]).not.toContain("other.example.test"); }); it("treats trailing-slash variants as one endpoint in human-readable output (#10784)", async () => { const deps = createDeps(configuredRoute("compatible-endpoint", "custom/model")); recordEquivalentEndpointRoutes(deps); await expect(runInferenceGet({}, deps)).resolves.toEqual({ provider: "compatible-endpoint", model: "custom/model", endpointUrl: "https://inference.example.test/v1", }); expect(deps.log.mock.calls.map(([line]) => line)).toEqual([ "Provider: compatible-endpoint", "Model: custom/model", "Endpoint: https://inference.example.test/v1", ]); }); it("treats trailing-slash variants as one endpoint in JSON output (#10784)", async () => { const deps = createDeps(configuredRoute("compatible-endpoint", "custom/model")); recordEquivalentEndpointRoutes(deps); const expected = { provider: "compatible-endpoint", model: "custom/model", endpointUrl: "https://inference.example.test/v1", }; await expect(runInferenceGet({ json: true }, deps)).resolves.toEqual(expected); expect(JSON.parse(deps.log.mock.calls[0][0])).toEqual(expected); }); it("omits an endpoint longer than the canonical endpoint boundary", async () => { const deps = createDeps(configuredRoute("compatible-endpoint", "custom/model")); recordRoute(deps, { provider: "compatible-endpoint", endpointUrl: `https://inference.example.test/${"a".repeat(2048)}`, }); await expect(runInferenceGet({ json: true }, deps)).resolves.toEqual( expectedEndpointOmission("withheld"), ); expect(JSON.parse(deps.log.mock.calls[0][0])).toEqual(expectedEndpointOmission("withheld")); }); it("ignores a pending route reservation when selecting the endpoint (#10784)", async () => { const deps = createDeps(configuredRoute("compatible-endpoint", "custom/model")); deps.listSandboxes.mockReturnValue([ { name: "published", provider: "compatible-endpoint", model: "custom/model", endpointUrl: "https://published.example.test/v1", preferredInferenceApi: "openai-completions", credentialEnv: "CUSTOM_API_KEY", }, { name: "pending-create", provider: "compatible-endpoint", model: "custom/model", endpointUrl: "https://unpublished.example.test/v1", preferredInferenceApi: "openai-completions", credentialEnv: "CUSTOM_API_KEY", pendingRouteReservation: true, }, ]); await expect(runInferenceGet({ json: true }, deps)).resolves.toEqual({ provider: "compatible-endpoint", model: "custom/model", endpointUrl: "https://published.example.test/v1", }); expect(deps.log.mock.calls[0][0]).not.toContain("unpublished.example.test"); }); it("omits an endpoint from an invalid persisted gateway binding", async () => { const deps = createDeps(configuredRoute("compatible-endpoint", "custom/model")); deps.listSandboxes.mockReturnValue([ { name: "broken-binding", provider: "compatible-endpoint", model: "custom/model", endpointUrl: "https://inference.example.test/v1", gatewayName: "secret-invalid-gateway", gatewayPort: null, }, ]); await expect(runInferenceGet({ json: true }, deps)).resolves.toEqual( expectedEndpointOmission("invalid", "custom/model", ["broken-binding"]), ); expect(deps.log.mock.calls[0][0]).not.toContain("secret-invalid-gateway"); expect(JSON.parse(deps.log.mock.calls[0][0])).toEqual( expectedEndpointOmission("invalid", "custom/model", ["broken-binding"]), ); }); it.each([ { label: "a generic registry failure in human-readable output", options: {}, error: new Error("secret registry path and contents"), hiddenDetail: "secret registry path and contents", expectedStatus: "unavailable" as const, }, { label: "a generic registry failure in JSON output", options: { json: true }, error: new Error("secret registry path and contents"), hiddenDetail: "secret registry path and contents", expectedStatus: "unavailable" as const, }, { label: "a corrupt registry in human-readable output", options: {}, error: new ConfigCorruptError("/safe/state/sandboxes.json"), hiddenDetail: "/safe/state/sandboxes.json", expectedStatus: "registry-corrupt" as const, }, { label: "a corrupt registry in JSON output", options: { json: true }, error: new ConfigCorruptError("/safe/state/sandboxes.json"), hiddenDetail: "/safe/state/sandboxes.json", expectedStatus: "registry-corrupt" as const, }, { label: "an unreadable registry in human-readable output", options: {}, error: new ConfigPermissionError("/safe/state/sandboxes.json", "read"), hiddenDetail: "/safe/state/sandboxes.json", expectedStatus: "registry-unreadable" as const, }, { label: "an unreadable registry in JSON output", options: { json: true }, error: new ConfigPermissionError("/safe/state/sandboxes.json", "read"), hiddenDetail: "/safe/state/sandboxes.json", expectedStatus: "registry-unreadable" as const, }, ])( "retains the live route and omits optional endpoint metadata after $label", async ({ options, error, hiddenDetail, expectedStatus }) => { const deps = createDeps(configuredRoute("compatible-endpoint", "custom/model")); deps.listSandboxes.mockImplementation(() => { throw error; }); await expect(runInferenceGet(options, deps)).resolves.toEqual( expectedEndpointOmission(expectedStatus), ); const output = deps.log.mock.calls.flat().join("\n"); expect(output).toContain("compatible-endpoint"); expect(output).toContain("custom/model"); expect(output).not.toContain("endpointUrl"); expect(output).not.toContain(hiddenDetail); expect(output).not.toContain("writable HOME"); expect(output).toContain(expectedStatus); expect(output).toContain(ENDPOINT_RECOVERY[expectedStatus]); }, ); it("reports an attached llama.cpp endpoint for an aligned sandbox route", async () => { const deps = { ...createDeps(configuredRoute("llama-cpp-local", "muse-glimmer")), getSandbox: () => ({ name: "llamacpp-env", provider: "llama-cpp-local", model: "muse-glimmer", endpointUrl: "http://127.0.0.1:8081/v1", }) as never, }; await expect(runInferenceGet({ sandboxName: "llamacpp-env" }, deps)).resolves.toEqual({ provider: "llama-cpp-local", model: "muse-glimmer", llamaCpp: { kind: "attached", endpointUrl: "http://127.0.0.1:8081/v1" }, }); expect(deps.log.mock.calls.map(([line]) => line)).toEqual([ "Provider: llama-cpp-local", "Model: muse-glimmer", "Llama.cpp: attached", "Endpoint: http://127.0.0.1:8081/v1", ]); }); it("reports unavailable managed ownership with safe recovery output", async () => { const deps = { ...createDeps(configuredRoute("llama-cpp-local", "muse-glimmer")), getSandbox: () => ({ name: "llamacpp-env", provider: "llama-cpp-local", model: "muse-glimmer", endpointUrl: "http://127.0.0.1:8081/v1", }) as never, inspectManagedLlamaCppOwnership: () => "unknown" as const, }; const result = await runInferenceGet({ sandboxName: "llamacpp-env" }, deps); expect(result.llamaCpp).toEqual({ kind: "unavailable", diagnostic: "Managed llama.cpp ownership state is unavailable.", recovery: "Run nemoclaw llamacpp-env doctor. Rerun onboarding for that sandbox if the managed llama.cpp runtime check fails.", }); expect(deps.log.mock.calls.map(([line]) => line)).toContain( "Recovery: Run nemoclaw llamacpp-env doctor. Rerun onboarding for that sandbox if the managed llama.cpp runtime check fails.", ); expect(JSON.stringify(result)).not.toContain("endpointUrl"); }); it("reads a private owner receipt before reporting managed ownership (#10256)", async () => { const home = fs.realpathSync(fs.mkdtempSync(`${os.tmpdir()}/nemoclaw-inference-get-owner-`)); vi.stubEnv("HOME", home); try { const paths = managedLlamaCppStatePaths(home); fs.mkdirSync(paths.stateDir, { recursive: true, mode: 0o700 }); reserveManagedLlamaCppOwner(paths, { schemaVersion: 1, sandboxName: "llamacpp-managed", catalogDigest: `sha256:${"1".repeat(64)}`, presetDigest: `sha256:${"2".repeat(64)}`, recipeDigest: `sha256:${"3".repeat(64)}`, recipeId: "llama-cpp.managed", }); const deps = createDeps(configuredRoute("llama-cpp-local", "muse-glimmer")); deps.getSandbox = () => ({ name: "llamacpp-managed", provider: "llama-cpp-local", model: "muse-glimmer", }) as never; const result = await runInferenceGet({ sandboxName: "llamacpp-managed" }, deps); expect(result.llamaCpp).toEqual({ kind: "managed" }); } finally { vi.unstubAllEnvs(); fs.rmSync(home, { recursive: true, force: true }); } }); it("does not attribute a sandbox route when the gateway route drifted", async () => { const deps = { ...createDeps(configuredRoute("nvidia-prod", "nvidia/model")), getSandbox: () => ({ name: "llamacpp-env", provider: "llama-cpp-local", model: "muse-glimmer", endpointUrl: "http://127.0.0.1:8081/v1", }) as never, }; await expect( runInferenceGet({ sandboxName: "llamacpp-env", quiet: true }, deps), ).resolves.toEqual({ provider: "nvidia-prod", model: "nvidia/model", }); }); it("queries the gateway recorded for the sandbox (#10671)", async () => { const deps = createDeps(configuredRoute("compatible-endpoint", "custom/model")); deps.getSandboxTargetGatewayName.mockReturnValue("nemoclaw-19090"); await expect(runInferenceGet({ quiet: true, sandboxName: "beta" }, deps)).resolves.toEqual( expectedEndpointOmission("invalid", "custom/model", ["beta"]), ); expect(deps.getSandboxTargetGatewayName).toHaveBeenCalledWith("beta"); expect(deps.observeInferenceRoute).toHaveBeenCalledWith({ target: { kind: "named", gatewayName: "nemoclaw-19090" }, timeoutMs: expect.any(Number), }); }); it("fails closed when a named sandbox has an invalid gateway binding", async () => { const deps = createDeps({ ok: true, value: { state: "unconfigured" } }); deps.getSandboxTargetGatewayName.mockImplementation(() => { throw new Error("invalid gatewayName secret-invalid-gateway and gatewayPort 31337"); }); const lookup = runInferenceGet({ sandboxName: "beta" }, deps); await expect(lookup).rejects.toMatchObject({ message: "NemoClaw could not resolve the sandbox's recorded gateway.\n\nRepair or remove the 'beta' sandbox registration, then rerun inference get.", }); await expect(lookup).rejects.not.toThrow(/secret-invalid-gateway|31337/); expect(deps.observeInferenceRoute).not.toHaveBeenCalled(); }); it.each([ { label: "corrupt", error: new ConfigCorruptError("/safe/state/sandboxes.json"), recovery: "Configuration file is present but is not valid JSON: /safe/state/sandboxes.json", }, { label: "unreadable", error: new ConfigPermissionError("/safe/state/sandboxes.json", "read"), recovery: "Cannot read config file: /safe/state/sandboxes.json", }, ])( "preserves safe recovery for a $label registry during gateway resolution", async ({ error, recovery }) => { const deps = createDeps({ ok: true, value: { state: "unconfigured" } }); deps.getSandboxTargetGatewayName.mockImplementation(() => { throw error; }); await expect(runInferenceGet({ sandboxName: "beta" }, deps)).rejects.toMatchObject({ message: expect.stringContaining(recovery), }); expect(deps.observeInferenceRoute).not.toHaveBeenCalled(); }, ); it("sanitizes route values only for human-readable output", async () => { const deps = createDeps(configuredRoute("openai\u001b[2J", "gpt\u0007-5.4")); await expect(runInferenceGet({}, deps)).resolves.toEqual({ provider: "openai\u001b[2J", model: "gpt\u0007-5.4", }); expect(deps.log.mock.calls.map(([line]) => line)).toEqual([ "Provider: openai[2J", "Model: gpt-5.4", ]); }); it("can return the route without rendering output for oclif JSON handling", async () => { const deps = createDeps(configuredRoute("openai-api", "gpt-5.4")); await expect(runInferenceGet({ quiet: true }, deps)).resolves.toEqual({ provider: "openai-api", model: "gpt-5.4", }); expect(deps.log).not.toHaveBeenCalled(); }); it("fails when no route is configured", async () => { const deps = createDeps({ ok: true, value: { state: "unconfigured" } }); deps.getSandboxTargetGatewayName.mockReturnValue("nemoclaw-19090"); await expect(runInferenceGet({}, deps)).rejects.toThrow( "OpenShell inference route is not configured for gateway 'nemoclaw-19090'.", ); expect(deps.log).not.toHaveBeenCalled(); }); it("keeps the legacy unconfigured response in the route absence branch (#10671)", async () => { const deps = createDeps({ ok: true, value: { state: "unconfigured" } }); await expect(runInferenceGet({}, deps)).rejects.toThrow( "OpenShell inference route is not configured for gateway 'nemoclaw'.", ); expect(deps.log).not.toHaveBeenCalled(); }); it("reports unrecognized gateway output without rendering it (#10671)", async () => { const deps = createDeps({ ok: false, error: { kind: "schema", reason: "malformed_output", message: "OpenShell returned an unrecognized inference route observation.", }, }); deps.getSandboxTargetGatewayName.mockReturnValue("nemoclaw-19090"); await expect(runInferenceGet({ sandboxName: "beta" }, deps)).rejects.toMatchObject({ message: "OpenShell inference route lookup for gateway 'nemoclaw-19090' returned output NemoClaw could not interpret. Run 'nemoclaw beta status' to diagnose the sandbox's recorded gateway.", }); expect(deps.log).not.toHaveBeenCalled(); }); it("reports a partial gateway route without rendering it (#10671)", async () => { const deps = createDeps({ ok: false, error: { kind: "schema", reason: "partial_route", message: "OpenShell returned an incomplete inference route.", }, }); deps.getSandboxTargetGatewayName.mockReturnValue("nemoclaw-19090"); await expect(runInferenceGet({ sandboxName: "beta" }, deps)).rejects.toMatchObject({ message: "OpenShell inference route lookup for gateway 'nemoclaw-19090' returned output NemoClaw could not interpret. Run 'nemoclaw beta status' to diagnose the sandbox's recorded gateway.", }); expect(deps.log).not.toHaveBeenCalled(); }); it("reports the gateway and timeout without command output (#10671)", async () => { const deps = createDeps({ ok: false, error: { kind: "timeout", message: "OpenShell inference route observation timed out.", }, }); deps.getSandboxTargetGatewayName.mockReturnValue("nemoclaw-19090"); await expect(runInferenceGet({ sandboxName: "beta" }, deps)).rejects.toMatchObject({ message: "OpenShell inference route lookup for gateway 'nemoclaw-19090' timed out. Run 'nemoclaw beta status' to diagnose the sandbox's recorded gateway.", }); expect(deps.log).not.toHaveBeenCalled(); }); it("reports the gateway and exit status without command output (#10671)", async () => { const deps = createDeps({ ok: false, error: { kind: "command", reason: "failed", message: "OpenShell inference route observation failed with exit status 7.", }, }); deps.getSandboxTargetGatewayName.mockReturnValue("nemoclaw-19090"); await expect(runInferenceGet({}, deps)).rejects.toMatchObject({ message: "OpenShell inference route lookup for gateway 'nemoclaw-19090' failed with exit status 7. Run 'nemoclaw status' to diagnose the selected gateway.", }); expect(deps.log).not.toHaveBeenCalled(); }); it("reports sandbox diagnosis guidance when a lookup has no exit status (#10671)", async () => { const deps = createDeps({ ok: false, error: { kind: "command", reason: "indeterminate", message: "OpenShell inference route observation ended before an exit status was available.", }, }); deps.getSandboxTargetGatewayName.mockReturnValue("nemoclaw-19090"); await expect(runInferenceGet({ sandboxName: "beta" }, deps)).rejects.toMatchObject({ message: "OpenShell inference route lookup for gateway 'nemoclaw-19090' failed before an exit status was available. Run 'nemoclaw beta status' to diagnose the sandbox's recorded gateway.", }); expect(deps.log).not.toHaveBeenCalled(); }); it("reports a rejected lookup without calling it an exit-status failure (#9809)", async () => { const deps = createDeps({ ok: false, error: { kind: "validation", message: "Invalid OpenShell inference route target or timeout.", }, }); deps.getSandboxTargetGatewayName.mockReturnValue("nemoclaw-19090"); await expect(runInferenceGet({ sandboxName: "beta" }, deps)).rejects.toMatchObject({ message: "NemoClaw rejected the inference route lookup for gateway 'nemoclaw-19090' before observation. Run 'nemoclaw beta status' to diagnose the sandbox's recorded gateway.", }); expect(deps.log).not.toHaveBeenCalled(); }); }); describe("native hosted inference get", () => { it.each([ ...HOSTED_NATIVE_PROVIDERS.map((definition) => ({ ...definition, endpointUrl: undefined, allowedIps: undefined, custom: undefined, })), customNativeProvider({ provider: "compatible-endpoint", endpointUrl: "https://custom.example/v1", api: "openai-completions", credentialEnv: "CUSTOM_KEY", allowedIps: ["8.8.8.8"], }), ])( "reports $logicalProvider from the selected sandbox rather than the shared route", async (definition) => { const deps = createDeps(configuredRoute("ollama-local", "other-sandbox-model")); deps.getSandbox = () => ({ name: "alpha", provider: definition.logicalProvider, model: "selected-model", nativeHostedProviderAttachment: { schemaVersion: 1, profileId: definition.profileId, providerName: definition.providerName, providerId: "owned-provider-id", endpointUrl: definition.endpointUrl, allowedIps: definition.allowedIps, custom: definition.custom, }, }); await expect(runInferenceGet({ sandboxName: "alpha", quiet: true }, deps)).resolves.toEqual({ provider: definition.logicalProvider, model: "selected-model", endpointUrl: definition.endpoint, }); expect(deps.observeInferenceRoute).not.toHaveBeenCalled(); }, ); });