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

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

import {
connectModulePath,
createConnectHarness,
requireDist,
} from "../../../../test/support/connect-flow-test-harness";

describe("Hermes sandbox connect light terminal skin", () => {
let exitSpy: MockInstance;
const originalStdoutIsTty = process.stdout.isTTY;

beforeEach(() => {
process.env.NEMOCLAW_TEST_NO_SLEEP = "1";
Object.defineProperty(process.stdout, "isTTY", { configurable: true, value: true });
exitSpy = vi.spyOn(process, "exit").mockImplementation(((code?: number | string | null) => {
throw new Error(`process.exit(${code ?? 0})`);
}) as never);
});

afterEach(() => {
vi.restoreAllMocks();
vi.unstubAllEnvs();
Object.defineProperty(process.stdout, "isTTY", {
configurable: true,
value: originalStdoutIsTty,
});
delete process.env.NEMOCLAW_TEST_NO_SLEEP;
delete require.cache[requireDist.resolve(connectModulePath)];
});

it("prepares the NemoClaw Hermes light skin inside the sandbox on light macOS Terminal.app (#6380)", async () => {
vi.stubEnv("TERM_PROGRAM", "Apple_Terminal");
vi.stubEnv("COLORFGBG", "0;15");
vi.stubEnv("HERMES_TUI_LIGHT", "");
const harness = createConnectHarness({
agentName: "hermes",
sessionAgent: {
name: "hermes",
runtime: { kind: "terminal", interactive_command: "hermes" },
},
});

await expect(harness.connectSandbox("alpha")).rejects.toThrow("process.exit(0)");

expect(harness.captureOpenshellSpy).toHaveBeenCalledWith(
[
"sandbox",
"exec",
"--name",
"alpha",
"--",
"sh",
"-c",
'hermes_home="${HERMES_HOME:-/sandbox/.hermes}"; test -f "$hermes_home/config.yaml" && cat "$hermes_home/config.yaml" || true',
],
expect.objectContaining({ ignoreError: true }),
);
const skinWriteCall = harness.runOpenshellSpy.mock.calls.find(
([args]) =>
Array.isArray(args) &&
args.slice(0, 6).join(" ") === "sandbox exec --name alpha -- sh" &&
String(args[7] ?? "").includes("nemoclaw-light.yaml"),
);
expect(skinWriteCall?.[0][7]).toContain("/skins/nemoclaw-light.yaml");
expect(skinWriteCall?.[0][7]).toContain("config.yaml");

const connectCall = harness.spawnSyncSpy.mock.calls.find(
([command, args]) =>
command === "openshell" &&
Array.isArray(args) &&
args.join(" ") === "sandbox connect alpha",
);
expect(connectCall?.[2]).toEqual(
expect.objectContaining({
env: expect.objectContaining({
COLORFGBG: "0;15",
TERM_PROGRAM: "Apple_Terminal",
}),
}),
);
expect(connectCall?.[2]?.env).not.toEqual(expect.objectContaining({ HERMES_TUI_LIGHT: "1" }));
expect(exitSpy).toHaveBeenCalledWith(0);
});

it("does not prepare the NemoClaw Hermes light skin when the sandbox Hermes config already sets display.skin (#6380)", async () => {
vi.stubEnv("TERM_PROGRAM", "Apple_Terminal");
vi.stubEnv("COLORFGBG", "0;15");
const harness = createConnectHarness({
agentName: "hermes",
sessionAgent: {
name: "hermes",
runtime: { kind: "terminal", interactive_command: "hermes" },
},
});
harness.captureOpenshellSpy.mockImplementation((args: unknown) => {
const argv = Array.isArray(args) ? args : [];
if (
argv[0] === "sandbox" &&
argv[1] === "exec" &&
argv.join(" ").includes("/sandbox/.hermes")
) {
return { status: 0, output: "display:\n skin: solarized-light\n" };
}
if (argv[0] === "sandbox" && argv[1] === "list") {
return { status: 0, output: "alpha Ready" };
}
if (argv[0] === "inference" && argv[1] === "get") {
return { status: 0, output: "Provider: unknown\nModel: unknown\n" };
}
return { status: 0, output: "" };
});

await expect(harness.connectSandbox("alpha")).rejects.toThrow("process.exit(0)");

const skinWriteCall = harness.runOpenshellSpy.mock.calls.find(
([args]) =>
Array.isArray(args) &&
args.slice(0, 6).join(" ") === "sandbox exec --name alpha -- sh" &&
String(args[7] ?? "").includes("nemoclaw-light.yaml"),
);
expect(skinWriteCall).toBeUndefined();
expect(exitSpy).toHaveBeenCalledWith(0);
});
});
74 changes: 73 additions & 1 deletion src/lib/actions/sandbox/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@ import {
import * as agentRuntime from "../../agent/runtime";
import { CLI_NAME } from "../../cli/branding";
import { D, G, R, YW } from "../../cli/terminal-style";
import { shellQuote } from "../../core/shell-quote";
import { spawnExitCode } from "../../core/process-exit";
import {
buildHermesLightSkinConfig,
buildSandboxConnectEnv,
hostTerminalLooksLight,
NEMOCLAW_HERMES_LIGHT_SKIN_YAML,
shouldPrepareHermesLightSkin,
} from "../../domain/sandbox/connect-env";
import { getNamedGatewayLifecycleState } from "../../gateway-runtime-action";
import {
parseGatewayInference,
Expand Down Expand Up @@ -81,6 +89,69 @@ type SpawnLikeResult = {
signal?: NodeJS.Signals | null;
};

function encodeForSandboxWrite(content: string): string {
return Buffer.from(content, "utf8").toString("base64");
}

function readSandboxHermesConfig(sandboxName: string): string | null {
const result = captureOpenshell(
[
"sandbox",
"exec",
"--name",
sandboxName,
"--",
"sh",
"-c",
'hermes_home="${HERMES_HOME:-/sandbox/.hermes}"; test -f "$hermes_home/config.yaml" && cat "$hermes_home/config.yaml" || true',
],
{
ignoreError: true,
timeout: OPENSHELL_PROBE_TIMEOUT_MS,
maxBuffer: 1024 * 1024,
},
);
if (result.status !== 0) return null;
return result.output ?? "";
}

function prepareHermesLightTerminalSkin(
sandboxName: string,
agent: { name?: string } | null | undefined,
env: NodeJS.ProcessEnv,
): void {
if (
agent?.name !== "hermes" ||
!hostTerminalLooksLight(env) ||
String(env.HERMES_TUI_LIGHT ?? "").trim() ||
String(env.HERMES_TUI_THEME ?? "").trim()
) {
return;
}

const currentConfig = readSandboxHermesConfig(sandboxName);
if (currentConfig === null || !shouldPrepareHermesLightSkin(agent, env, currentConfig)) return;

const updatedConfig = buildHermesLightSkinConfig(currentConfig);
if (!updatedConfig) return;

const skinB64 = encodeForSandboxWrite(NEMOCLAW_HERMES_LIGHT_SKIN_YAML);
const configB64 = encodeForSandboxWrite(updatedConfig);
const script = [
"set -eu",
'hermes_home="${HERMES_HOME:-/sandbox/.hermes}"',
'mkdir -p "$hermes_home/skins"',
`printf %s ${shellQuote(skinB64)} | base64 -d > "$hermes_home/skins/nemoclaw-light.yaml"`,
`printf %s ${shellQuote(configB64)} | base64 -d > "$hermes_home/config.yaml"`,
].join("\n");

runOpenshell(["sandbox", "exec", "--name", sandboxName, "--", "sh", "-c", script], {
ignoreError: true,
stdio: "ignore",
timeout: OPENSHELL_PROBE_TIMEOUT_MS,
});
}

type SandboxListProbe = {
status: number | null;
output: string;
Expand Down Expand Up @@ -1089,10 +1160,11 @@ export async function connectSandbox(
// OPENSHELL_SANDBOX) and covers every other interactive entry path too.
console.log("");
}
prepareHermesLightTerminalSkin(sandboxName, agent, process.env);
const result = spawnSync(getOpenshellBinary(), ["sandbox", "connect", sandboxName], {
stdio: "inherit",
cwd: ROOT,
env: process.env,
env: buildSandboxConnectEnv(agent, process.env),
});
exitWithConnectSpawnResult(sandboxName, result);
}
96 changes: 96 additions & 0 deletions src/lib/domain/sandbox/connect-env.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

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

import {
buildHermesLightSkinConfig,
buildSandboxConnectEnv,
NEMOCLAW_HERMES_LIGHT_SKIN_NAME,
shouldPrepareHermesLightSkin,
} from "./connect-env";
Comment thread
github-code-quality[bot] marked this conversation as resolved.
Fixed
Comment thread
github-advanced-security[bot] marked this conversation as resolved.
Fixed

describe("sandbox connect environment helpers", () => {
it("keeps the connect environment unchanged; Hermes light skin is prepared in sandbox config (#6380)", () => {
expect(
buildSandboxConnectEnv(
{ name: "hermes" },
{ COLORFGBG: "0;15", TERM_PROGRAM: "Apple_Terminal" },
),
).toEqual(
expect.objectContaining({
COLORFGBG: "0;15",
TERM_PROGRAM: "Apple_Terminal",
}),
);
});

it("plans the NemoClaw Hermes light skin only for light terminals without user theme or skin (#6380)", () => {
expect(
shouldPrepareHermesLightSkin(
{ name: "hermes" },
{ COLORFGBG: "0;15", TERM_PROGRAM: "Apple_Terminal" },
"model: test\n",
),
).toBe(true);
});

it("does not plan the NemoClaw Hermes light skin when the terminal is dark (#6380)", () => {
expect(
shouldPrepareHermesLightSkin(
{ name: "hermes" },
{ COLORFGBG: "0;0", TERM_PROGRAM: "Apple_Terminal" },
"model: test\n",
),
).toBe(false);
});

it("does not plan the NemoClaw Hermes light skin when the user set Hermes theme env (#6380)", () => {
for (const env of [{ HERMES_TUI_LIGHT: "0" }, { HERMES_TUI_THEME: "dark" }]) {
expect(
shouldPrepareHermesLightSkin({ name: "hermes" }, { COLORFGBG: "0;15", ...env }, ""),
).toBe(false);
}
});

it("does not plan the NemoClaw Hermes light skin when config already has display.skin (#6380)", () => {
expect(
shouldPrepareHermesLightSkin(
{ name: "hermes" },
{ COLORFGBG: "0;15" },
"display:\n skin: solarized-light\n",
),
).toBe(false);
});

it("does not plan the NemoClaw Hermes light skin for non-Hermes agents (#6380)", () => {
expect(
shouldPrepareHermesLightSkin({ name: "openclaw" }, { COLORFGBG: "0;15" }, "model: test\n"),
).toBe(false);
});

it("does not infer light mode from Apple Terminal without usable COLORFGBG (#6380)", () => {
expect(
shouldPrepareHermesLightSkin({ name: "hermes" }, { TERM_PROGRAM: "Apple_Terminal" }, ""),
).toBe(false);
expect(
shouldPrepareHermesLightSkin(
{ name: "hermes" },
{ COLORFGBG: "not-a-color", TERM_PROGRAM: "Apple_Terminal" },
"",
),
).toBe(false);
});

it("adds display.skin to Hermes config without changing existing fields (#6380)", () => {
expect(buildHermesLightSkinConfig("model: test\n")).toBe(
`model: test\ndisplay:\n skin: ${NEMOCLAW_HERMES_LIGHT_SKIN_NAME}\n`,
);
});

it("does not rewrite Hermes config when display.skin is already explicit (#6380)", () => {
expect(
buildHermesLightSkinConfig("display:\n skin: solarized-light\nmodel: test\n"),
).toBeNull();
});
});
Loading
Loading