Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 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
70 changes: 70 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,70 @@
// 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 environment", () => {
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();
if (originalStdoutIsTty === undefined) {
Object.defineProperty(process.stdout, "isTTY", { configurable: true, value: undefined });
} else {
Object.defineProperty(process.stdout, "isTTY", {
configurable: true,
value: originalStdoutIsTty,
});
}
delete process.env.NEMOCLAW_TEST_NO_SLEEP;
delete require.cache[requireDist.resolve(connectModulePath)];
});
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

it("marks Hermes connect sessions as light-mode when launched from 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)");

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",
HERMES_TUI_LIGHT: "1",
TERM_PROGRAM: "Apple_Terminal",
}),
}),
);
expect(exitSpy).toHaveBeenCalledWith(0);
});
});
3 changes: 2 additions & 1 deletion src/lib/actions/sandbox/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import * as agentRuntime from "../../agent/runtime";
import { CLI_NAME } from "../../cli/branding";
import { D, G, R, YW } from "../../cli/terminal-style";
import { spawnExitCode } from "../../core/process-exit";
import { buildSandboxConnectEnv } from "../../domain/sandbox/connect-env";
import { getNamedGatewayLifecycleState } from "../../gateway-runtime-action";
import {
parseGatewayInference,
Expand Down Expand Up @@ -1092,7 +1093,7 @@ export async function connectSandbox(
const result = spawnSync(getOpenshellBinary(), ["sandbox", "connect", sandboxName], {
stdio: "inherit",
cwd: ROOT,
env: process.env,
env: buildSandboxConnectEnv(agent),
});
exitWithConnectSpawnResult(sandboxName, result);
}
65 changes: 65 additions & 0 deletions src/lib/domain/sandbox/connect-env.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

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

import { buildSandboxConnectEnv } from "./connect-env";

describe("sandbox connect environment helpers", () => {
it("marks Hermes connect sessions as light when COLORFGBG reports a light background (#6380)", () => {
expect(
buildSandboxConnectEnv(
{ name: "hermes" },
{ COLORFGBG: "0;15", TERM_PROGRAM: "Apple_Terminal" },
),
).toEqual(
expect.objectContaining({
COLORFGBG: "0;15",
HERMES_TUI_LIGHT: "1",
TERM_PROGRAM: "Apple_Terminal",
}),
);
});

it("does not force Hermes light mode when COLORFGBG reports a dark background (#6380)", () => {
expect(
buildSandboxConnectEnv(
{ name: "hermes" },
{ COLORFGBG: "0;0", TERM_PROGRAM: "Apple_Terminal" },
),
).not.toHaveProperty("HERMES_TUI_LIGHT");
});

it("preserves explicit Hermes light-mode overrides (#6380)", () => {
for (const value of ["0", "false", "no", "off"]) {
expect(
buildSandboxConnectEnv({ name: "hermes" }, { COLORFGBG: "0;15", HERMES_TUI_LIGHT: value })
.HERMES_TUI_LIGHT,
).toBe(value);
}
});

it("preserves explicit Hermes theme overrides (#6380)", () => {
expect(
buildSandboxConnectEnv({ name: "hermes" }, { COLORFGBG: "0;15", HERMES_TUI_THEME: "dark" }),
).not.toHaveProperty("HERMES_TUI_LIGHT");
});

it("does not set Hermes light mode for non-Hermes agents (#6380)", () => {
expect(buildSandboxConnectEnv({ name: "openclaw" }, { COLORFGBG: "0;15" })).not.toHaveProperty(
"HERMES_TUI_LIGHT",
);
});

it("does not infer light mode from Apple Terminal without usable COLORFGBG (#6380)", () => {
expect(
buildSandboxConnectEnv({ name: "hermes" }, { TERM_PROGRAM: "Apple_Terminal" }),
).not.toHaveProperty("HERMES_TUI_LIGHT");
expect(
buildSandboxConnectEnv(
{ name: "hermes" },
{ COLORFGBG: "not-a-color", TERM_PROGRAM: "Apple_Terminal" },
),
).not.toHaveProperty("HERMES_TUI_LIGHT");
});
});
38 changes: 38 additions & 0 deletions src/lib/domain/sandbox/connect-env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

const TRUE_VALUES = new Set(["1", "true", "yes", "on"]);
const FALSE_VALUES = new Set(["0", "false", "no", "off"]);

function hasExplicitBoolean(value: string | undefined): boolean {
const normalized = String(value ?? "")
.trim()
.toLowerCase();
return TRUE_VALUES.has(normalized) || FALSE_VALUES.has(normalized);
}

function hostTerminalLooksLight(env: NodeJS.ProcessEnv): boolean {
const colorfgbg = String(env.COLORFGBG ?? "").trim();
if (!colorfgbg) return false;

const lastField = colorfgbg.split(";").at(-1) ?? "";
const bg = Number(lastField);
if (!Number.isInteger(bg) || bg < 0 || bg > 15) return false;
return bg === 7 || bg === 15;
}

export function buildSandboxConnectEnv(
agent: { name?: string } | null | undefined,
env: NodeJS.ProcessEnv = process.env,
): NodeJS.ProcessEnv {
Comment on lines +24 to +27

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📐 Maintainability & Code Quality | 🟠 Major | ⚡ Quick win

Domain helper directly references process.env, breaking layer purity.

src/lib/README.md requires domain modules to stay pure with no direct process/fs/Docker/network calls, and this PR explicitly calls for the connect action to delegate to "a pure domain helper... rather than manipulating process environment... inside the action." The default parameter env: NodeJS.ProcessEnv = process.env embeds a direct process reference inside the domain module itself, and the actual call site (connect.ts) never passes process.env explicitly — so the domain layer, not the action, ends up owning the process boundary access.

Make env a required parameter and have the action pass process.env explicitly, keeping the process boundary owned by the action layer as intended.

♻️ Proposed fix
 export function buildSandboxConnectEnv(
   agent: { name?: string } | null | undefined,
-  env: NodeJS.ProcessEnv = process.env,
+  env: NodeJS.ProcessEnv,
 ): NodeJS.ProcessEnv {

And update the call site in src/lib/actions/sandbox/connect.ts:

-    env: buildSandboxConnectEnv(agent),
+    env: buildSandboxConnectEnv(agent, process.env),
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
export function buildSandboxConnectEnv(
agent: { name?: string } | null | undefined,
env: NodeJS.ProcessEnv = process.env,
): NodeJS.ProcessEnv {
export function buildSandboxConnectEnv(
agent: { name?: string } | null | undefined,
env: NodeJS.ProcessEnv,
): NodeJS.ProcessEnv {
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/lib/domain/sandbox/connect-env.ts` around lines 24 - 27, The sandbox
domain helper is still crossing the process boundary by defaulting `env` to
`process.env`, which violates domain purity. Update `buildSandboxConnectEnv` in
`connect-env.ts` so `env` is a required argument with no `process` reference,
then change the `connect.ts` call site to pass `process.env` explicitly. Keep
the process access in the action layer and let the domain helper only transform
the provided environment.

Source: Path instructions

const nextEnv = { ...env };
if (
agent?.name === "hermes" &&
!hasExplicitBoolean(nextEnv.HERMES_TUI_LIGHT) &&
!String(nextEnv.HERMES_TUI_THEME ?? "").trim() &&
hostTerminalLooksLight(nextEnv)
) {
nextEnv.HERMES_TUI_LIGHT = "1";
}
return nextEnv;
}
Loading