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
18 changes: 17 additions & 1 deletion src/lib/actions/sandbox/gateway-select.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,26 @@ describe("selectSandboxOwningGateway", () => {
expect(selected).toEqual({ outcome: "selected", gatewayName: "nemoclaw-8091" });
expect(run).toHaveBeenCalledWith(
["gateway", "select", "nemoclaw-8091"],
expect.objectContaining({ ignoreError: true }),
expect.objectContaining({
ignoreError: true,
stdio: ["inherit", "pipe", "inherit"],
}),
);
});

it("replays selection output through the stdio adapter", () => {
vi.spyOn(registry, "getSandbox").mockReturnValue({ gatewayPort: 8080 } as never);
const output = "\u001b[32m✓ Active gateway set to 'nemoclaw'\u001b[0m\n";
const run = vi.fn(() => ({ status: 0, stdout: output }) as never);
const write = vi.fn();

expect(selectSandboxOwningGateway("alpha", run, write)).toEqual({
outcome: "selected",
gatewayName: "nemoclaw",
});
expect(write).toHaveBeenCalledWith(output);
});

it("keeps the bare default gateway name for a default-port sandbox", () => {
vi.spyOn(registry, "getSandbox").mockReturnValue({ gatewayPort: 8080 } as never);
const run = vi.fn(() => ({ status: 0 }) as never);
Expand Down
5 changes: 5 additions & 0 deletions src/lib/actions/sandbox/gateway-select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@

import { runOpenshell } from "../../adapters/openshell/runtime";
import { OPENSHELL_OPERATION_TIMEOUT_MS } from "../../adapters/openshell/timeouts";
import { writeStderr } from "../../adapters/stdio";
import { getKnownSandboxTargetGatewayName } from "./gateway-target";

export type GatewaySelectRunner = typeof runOpenshell;
export type GatewaySelectOutputWriter = typeof writeStderr;

export type GatewaySelectResult =
| { outcome: "selected"; gatewayName: string }
Expand All @@ -15,13 +17,16 @@ export type GatewaySelectResult =
export function selectSandboxOwningGateway(
sandboxName: string,
run: GatewaySelectRunner = runOpenshell,
writeOutput: GatewaySelectOutputWriter = writeStderr,
): GatewaySelectResult {
const targetGatewayName = getKnownSandboxTargetGatewayName(sandboxName);
if (!targetGatewayName) return { outcome: "unregistered", gatewayName: null };
const result = run(["gateway", "select", targetGatewayName], {
ignoreError: true,
stdio: ["inherit", "pipe", "inherit"],
timeout: OPENSHELL_OPERATION_TIMEOUT_MS,
});
if (result.stdout) writeOutput(result.stdout);
if (result.error || result.status !== 0) {
return { outcome: "failed", gatewayName: targetGatewayName };
}
Expand Down
6 changes: 6 additions & 0 deletions src/lib/adapters/stdio.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

export function writeStderr(output: string): void {
process.stderr.write(output);
}
5 changes: 5 additions & 0 deletions test/cli/sandbox-status-json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ describe("CLI sandbox status JSON output", testTimeoutOptions(20_000), () => {
path.join(localBin, "openshell"),
[
"#!/usr/bin/env bash",
'if [ "$1" = "gateway" ] && [ "$2" = "select" ]; then',
" printf \"\\033[32m✓ Active gateway set to 'nemoclaw'\\033[0m\\n\"",
" exit 0",
"fi",
'if [ "$1" = "inference" ] && [ "$2" = "get" ]; then',
" echo 'Gateway inference:'",
" echo",
Expand Down Expand Up @@ -168,6 +172,7 @@ describe("CLI sandbox status JSON output", testTimeoutOptions(20_000), () => {
expect(r.out.trim().endsWith("}")).toBe(true);
expect(r.out).not.toContain("Sandbox: ");
expect(r.out).not.toContain("Nonexistent flag: --json");
expect(r.out).not.toContain("Active gateway set");

const parsed = JSON.parse(r.out);
expect(parsed).toMatchObject({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,15 +257,9 @@ if (JSON.parse(process.env.CONFIG_MODEL_JSON) !== "openai:" + process.env.MODEL_
' || fail "keyed config get did not return model B after re-onboard"
pass "keyed config get reports model B after re-onboard"

status_output="$("$CLI" "$SANDBOX_NAME" status --json 2>&1)" || fail "nemoclaw status failed after re-onboard: $status_output"
# gateway-select.ts invokes OpenShell with inherited stdio. Remove this exception when owning-gateway
# selection is quiet for machine-readable status output.
STATUS_OUTPUT="$status_output" SANDBOX_NAME="$SANDBOX_NAME" MODEL_B="$model_b" node -e '
const lines = process.env.STATUS_OUTPUT.split(/\r?\n/);
const banner = "✓ Active gateway set to '\''nemoclaw'\''";
const plainFirstLine = lines[0].replace(/\u001B\[[0-?]*[ -/]*[@-~]/g, "");
if (plainFirstLine === banner) lines.shift();
const status = JSON.parse(lines.join("\n"));
status_json="$("$CLI" "$SANDBOX_NAME" status --json)" || fail "nemoclaw status failed after re-onboard: ${status_json:-<no stdout>}"
STATUS_JSON="$status_json" SANDBOX_NAME="$SANDBOX_NAME" MODEL_B="$model_b" node -e '
const status = JSON.parse(process.env.STATUS_JSON);
if (status.name !== process.env.SANDBOX_NAME ||
status.model !== process.env.MODEL_B ||
status.provider !== "compatible-endpoint") process.exit(1);
Expand Down